1) 最小框架
/* Example Minimal Character Device Driver */ #include <linux/module.h> static int __init hello_init(void) { printk("Hello Example Init/n"); return 0; } static void __exit hello_exit(void) { printk("Hello Example Exit/n"); } module_init(hello_init); module_exit(hello_exit); MODULE_AUTHOR("OOO"); MODULE_DESCRIPTION("Hello Example"); MODULE_LICENSE("GPL");
2) 增加调用接口
/* Example Minimal Character Device Driver */ #include <linux/module.h> #include <linux/fs.h> #define HELLO_MAJOR 242 static int debug_enable = 0; module_param(debug_enable, int, 0); MODULE_PARM_DESC(debug_enable, "Enable module debug mode."); struct file_operations hello_fops; static int hello_open(struct inode *inode, struct file *file) { printk("hello_open: successful/n"); return 0; } static int hello_release(struct inode *inode, struct file *file) { printk("hello_release: successful/n"); return 0; } static ssize_t hello_read(struct file *file, char *buf, size_t count, loff_t *ptr) { printk("hello_read: returning zero bytes/n"); return 0; } static ssize_t hello_write(struct file *file, const char *buf, size_t count, loff_t *ppos) { printk("hello_write: accepting zero bytes/n"); return 0; } static int hello_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { printk("hello_ioctl: cmd=%d, arg=%ld/n", cmd, arg); return 0; } static int __init hello_init(void) { int ret; printk("Hello Example Init - debug mode is %s/n", debug_enable ? "enabled" : "disabled"); ret = register_chrdev(HELLO_MAJOR, "hello", &hello_fops); if(ret < 0) { printk("Error registering hello device/n"); goto hello_fail; } printk("Hello: registered module successfully!/n"); /* Init processing here... */ return 0; hello_fail: return ret; } static void __exit hello_exit(void) { printk("Hello Exaple Exit/n"); } struct file_operations hello_fops = { owner: THIS_MODULE, read: hello_read, write: hello_write, ioctl: hello_ioctl, open: hello_open, release: hello_release, }; module_init(hello_init); module_exit(hello_exit); MODULE_DESCRIPTION("Hello Module Example"); MODULE_LICENSE("GPL"); MODULE_AUTHOR("OOO");
在linux源码树添加相关配置文件,生成hello.ko,安装到/lib/modules
3) 测试该模块
在目标平台上,
# insmod hello.ko
创建设备节点,比如/dev/hello
# mknod /dev/hello c 242 0
然后,编写测试程序:
/* test hello.ko * you should insmod hello.ko and mknod /dev/hello before */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main(int argc, char **argv) { int fd; int rc = 0; char *rd_buf[16]; printf("%s: entered/n", argv[0]); fd = open("/dev/hello", O_RDWR); if(fd == -1) { printf("open failed"); rc = fd; exit(-1); } printf("%s: open successful: %d/n", argv[0], fd); close(fd); }
交叉编译后,bin档(比如hello-test)放到rootfs/bin目录,在目标平台上运行
# hello-test
您还没有登录,请您登录后再发表评论
Linux设备驱动程序框架 linux的设备驱动程序与外界的接口可以分为三个部分: 1.驱动程序与操作系统内核的接口。通过file_operations(include/linux/fs.h)数据结构来完成的。 2.驱动程序与系统引导的接口。这...
可重用Linux设备驱动程序框架可重用Linux设备驱动程序框架可重用Linux设备驱动程序框架
《Linux设备驱动开发详解》是一本介绍Linux设备驱动开发理论、框架与实例的书,《Linux设备驱动开发详解(第2版)》基于LDD6410开发板,以Linux2.6 版本内核为蓝本,详细介绍自旋锁、信号量、完成量、中断顶/底半部、...
可重用Linux设备驱动程序框架.pdf
Linux采用的是整体式的内核结构,这种结构的内核一般不能动态的增加新的功能。为此,Linux提供了一种全新的机制,叫(可安装)“模块”...正是这种机制,使得内核的内存映像保持最小,但却具有很大的灵活性和可扩充性。
Linux下PCI设备驱动程序开发基本框架
Linux设备驱动程序原理及框架
认识目标板各设备型号及开发中注意事项 ...Linux下设备驱动程序开发框架和流程 Linux下模块化驱动程序设计(动态加载) 设备驱动加入Linux内核中 实验:编写驱动程序框架+测试用例(字符型设备)两种初始化方式
Linux下的PCI设备开发以及字符驱动程序框架的研究.pdf
事实上,Linux的设备驱动都遵循一个惯例——表征驱动程序(用driver更贴切一些,应该称为驱动器比较好吧)的结构体,结构体里面应该包含了驱动程序所需要的所有资源。用术语来说,是这个驱动器对象所拥有的属性及...
Linux 字符设备 驱动 基本框架 AT91SAM9G20驱动程序设计
介绍了USB的相关概念及Linux中USB设备驱动程序的框架和重要数据结构。通过设计和实现一个典型的例子,总结了Linux下编写一个USB设备驱动程序的一般方法和技巧。
一、linux的设备驱动程序与外界的接口可以分为三个部分: 1.驱动程序与操作系统内核的接口。通过file_operations(include/linux/fs.h)数据结构来完成的。 2.驱动程序与系统引导的接口。这部分利用驱动程序对...
PCI是一种广泛采用的总线标准,它提供了许多优于其它总线标准(如EISA)的新特性,目前已经成为...Linux的内核能较好地支持PCI总线,本文以Intel 386体系结构为主,探讨了在Linux下开发PCI设备驱动程序的基本框架。
在系统分析蓝牙协议和驱动层框架的基础上,定义了 Linux 下蓝牙核心数据结构,给出了 Linux 下蓝牙 设备驱动程序架构,编写了 Linux 系统下蓝牙设备 UART 驱动程序的开发。
这样的代码不应该需要太多积分,但时间久了积分上去了,特意来减少所需积分,希望大家喜欢。 嵌入式开发框架之模块化代码实现 仿linux设备驱动框架编写 linux模块化编程 代码开发框架 linux驱动学习
设备驱动程序在 Linux 内核中起着重要作用,它使某个 硬件能响应一个定义良好的内部编程接口。这些接口隐藏了 设备的工作细节,用户通过一组独立于特定驱动程序的标准 调用来操作设备。而将这些调用映射到作用于实际...
linux kernel I2C device driver, pdf document.
介绍Linux系统中的设备,Linux下设备驱动程序的基本结构,结合USB驱动程序的具体开发实例,介绍了USB的相关 概念,分析Linux中USB核心子系统的框架构成,描述了Linux下USB设备驱动开发的一般方法和技巧。
相关推荐
Linux设备驱动程序框架 linux的设备驱动程序与外界的接口可以分为三个部分: 1.驱动程序与操作系统内核的接口。通过file_operations(include/linux/fs.h)数据结构来完成的。 2.驱动程序与系统引导的接口。这...
可重用Linux设备驱动程序框架可重用Linux设备驱动程序框架可重用Linux设备驱动程序框架
《Linux设备驱动开发详解》是一本介绍Linux设备驱动开发理论、框架与实例的书,《Linux设备驱动开发详解(第2版)》基于LDD6410开发板,以Linux2.6 版本内核为蓝本,详细介绍自旋锁、信号量、完成量、中断顶/底半部、...
可重用Linux设备驱动程序框架.pdf
Linux采用的是整体式的内核结构,这种结构的内核一般不能动态的增加新的功能。为此,Linux提供了一种全新的机制,叫(可安装)“模块”...正是这种机制,使得内核的内存映像保持最小,但却具有很大的灵活性和可扩充性。
Linux下PCI设备驱动程序开发基本框架
Linux设备驱动程序原理及框架
认识目标板各设备型号及开发中注意事项 ...Linux下设备驱动程序开发框架和流程 Linux下模块化驱动程序设计(动态加载) 设备驱动加入Linux内核中 实验:编写驱动程序框架+测试用例(字符型设备)两种初始化方式
Linux下的PCI设备开发以及字符驱动程序框架的研究.pdf
事实上,Linux的设备驱动都遵循一个惯例——表征驱动程序(用driver更贴切一些,应该称为驱动器比较好吧)的结构体,结构体里面应该包含了驱动程序所需要的所有资源。用术语来说,是这个驱动器对象所拥有的属性及...
Linux 字符设备 驱动 基本框架 AT91SAM9G20驱动程序设计
介绍了USB的相关概念及Linux中USB设备驱动程序的框架和重要数据结构。通过设计和实现一个典型的例子,总结了Linux下编写一个USB设备驱动程序的一般方法和技巧。
一、linux的设备驱动程序与外界的接口可以分为三个部分: 1.驱动程序与操作系统内核的接口。通过file_operations(include/linux/fs.h)数据结构来完成的。 2.驱动程序与系统引导的接口。这部分利用驱动程序对...
PCI是一种广泛采用的总线标准,它提供了许多优于其它总线标准(如EISA)的新特性,目前已经成为...Linux的内核能较好地支持PCI总线,本文以Intel 386体系结构为主,探讨了在Linux下开发PCI设备驱动程序的基本框架。
在系统分析蓝牙协议和驱动层框架的基础上,定义了 Linux 下蓝牙核心数据结构,给出了 Linux 下蓝牙 设备驱动程序架构,编写了 Linux 系统下蓝牙设备 UART 驱动程序的开发。
这样的代码不应该需要太多积分,但时间久了积分上去了,特意来减少所需积分,希望大家喜欢。 嵌入式开发框架之模块化代码实现 仿linux设备驱动框架编写 linux模块化编程 代码开发框架 linux驱动学习
设备驱动程序在 Linux 内核中起着重要作用,它使某个 硬件能响应一个定义良好的内部编程接口。这些接口隐藏了 设备的工作细节,用户通过一组独立于特定驱动程序的标准 调用来操作设备。而将这些调用映射到作用于实际...
linux kernel I2C device driver, pdf document.
介绍Linux系统中的设备,Linux下设备驱动程序的基本结构,结合USB驱动程序的具体开发实例,介绍了USB的相关 概念,分析Linux中USB核心子系统的框架构成,描述了Linux下USB设备驱动开发的一般方法和技巧。