Chinaunix首页 | 论坛 | 博客
  • 博客访问: 331376
  • 博文数量: 47
  • 博客积分: 834
  • 博客等级: 军士长
  • 技术积分: 695
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-07 09:38
文章分类
文章存档

2018年(1)

2015年(1)

2014年(2)

2013年(2)

2012年(23)

2011年(18)

分类: 嵌入式

2012-01-03 18:56:30

1) 最小框架 

  1. #include <linux/module.h>
  2.   
  3. static int __init hello_init(void)
  4. {
  5.     printk("Hello Example Init/n");
  6.     return 0;
  7. }
  8. static void __exit hello_exit(void)
  9. {
  10.     printk("Hello Example Exit/n");
  11. }
  12.   
  13. module_init(hello_init); //模块加载
  14. module_exit(hello_exit); //模块卸载
  15.   
  16. MODULE_AUTHOR("OOO");
  17. MODULE_DESCRIPTION("Hello Example");
  18. MODULE_LICENSE("GPL");

2) 增加调用接口

  1. #include <linux/module.h>
  2. #include <linux/fs.h>

  3. #define HELLO_MAJOR 242
  4.   
  5. static int debug_enable = 0;
  6. module_param(debug_enable, int, 0);
  7. MODULE_PARM_DESC(debug_enable,"Enable module debug mode.");
  8.   
  9. struct file_operations hello_fops;
  10.   
  11. static int hello_open(struct inode *inode, struct file *file)
  12. {
  13.     printk("hello_open: successful/n");
  14.     return 0;
  15. }
  16.   
  17. static int hello_release(struct inode *inode, struct file *file)
  18. {
  19.     printk("hello_release: successful/n");
  20.     return 0;
  21. }
  22.   
  23. static ssize_t hello_read(struct file *file, char *buf, size_t count, loff_t *ptr)
  24. {
  25.     printk("hello_read: returning zero bytes/n");
  26.     return 0;
  27. }
  28.   
  29. static ssize_t hello_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
  30. {
  31.     printk("hello_write: accepting zero bytes/n");
  32.     return 0;
  33. }
  34.   
  35. static int hello_ioctl(struct inode *inode, struct file *file,unsigned int cmd, unsigned long arg)
  36. {
  37.     printk("hello_ioctl: cmd=%d, arg=%ld/n", cmd, arg);
  38.     return 0;
  39. }
  40.   
  41. static int __init hello_init(void)
  42. {
  43.     int ret;
  44.     printk("Hello Example Init - debug mode is %s/n",debug_enable ? "enabled" : "disabled");
  45.     ret = register_chrdev(HELLO_MAJOR, "hello", &hello_fops);
  46.     if(ret < 0)
  47.     {
  48.         printk("Error registering hello device/n");
  49.         goto hello_fail;
  50.     }
  51.     printk("Hello: registered module successfully!/n");
  52.     return 0;
  53.   
  54. hello_fail:
  55.     return ret;
  56. }
  57.   
  58. static void __exit hello_exit(void)
  59. {
  60.     printk("Hello Exaple Exit/n");
  61. }
  62.   
  63. struct file_operations hello_fops = {
  64.     owner: THIS_MODULE,
  65.     read: hello_read,
  66.     write: hello_write,
  67.     ioctl: hello_ioctl,
  68.     open: hello_open,
  69.     release: hello_release,
  70. };
  71.   
  72. module_init(hello_init);
  73. module_exit(hello_exit);
  74.   
  75. MODULE_DESCRIPTION("Hello Module Example");
  76. MODULE_LICENSE("GPL");
  77. MODULE_AUTHOR("OOO");

在linux源码树添加相关配置文件,生成hello.ko

3) 测试该模块

在目标平台上,创建设备节点(mknod **** , insmod ****)

然后,编写测试程序:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7.   
  8. int main(int argc, char **argv)
  9. {
  10.     int fd;
  11.     int rc = 0;
  12.     char *rd_buf[16];
  13.   
  14.     printf("%s: entered/n", argv[0]);
  15.   
  16.     fd = open("/dev/hello", O_RDWR);
  17.     if(fd == -1)
  18.     {
  19.         printf("open failed");
  20.         rc = fd;
  21.         exit(-1);
  22.     }
  23.     printf("%s: open successful: %d/n", argv[0], fd);
  24.     close(fd);
  25. }
阅读(1379) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~