Chinaunix首页 | 论坛 | 博客
  • 博客访问: 138896
  • 博文数量: 49
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 182
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-14 13:22
个人简介

Linux的热爱者

文章存档

2014年(7)

2013年(42)

我的朋友

分类: LINUX

2013-05-03 11:10:33

1. ldd3上的scull太难了,俺一个新手怎么能看得懂呢?没办法,自己写一个非常非常简单的字符设备。
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>

  6. dev_t scull_dev_num;
  7. struct cdev* p_scull_dev;

  8. static int scull_open(struct inode* node, struct file *filp)
  9. {
  10.     printk(KERN_ALERT "scull open!\n");
  11.     return 0;
  12. }

  13. static ssize_t scull_read(struct file* filp, char __user *buf, size_t count, loff_t *f_pos)
  14. {
  15.     printk(KERN_ALERT "scull read!\n");
  16.     return 0;
  17. }

  18. static ssize_t scull_write(struct file* filp, const char __user *buf, size_t count, loff_t *f_pos)
  19. {
  20.     printk(KERN_ALERT "scull write!\n");
  21.     return 0;
  22. }

  23. static int scull_release(struct inode *node, struct file* filp)
  24. {
  25.     printk(KERN_ALERT "scull release!\n");
  26.     return 0;
  27. }

  28. static const struct file_operations scull_fops =
  29. {
  30.     .owner = THIS_MODULE,
  31.     .open = scull_open,
  32.     .read = scull_read,
  33.     .write = scull_write,
  34.     .release = scull_release,
  35. };

  36. static int __init scull_init(void)
  37. {
  38.     int ret;
  39.     printk(KERN_ALERT "in scull driver\n");
  40.     //动态分配一个设备号,从0开始分配一个从设备号
  41.     ret = alloc_chrdev_region(&scull_dev_num, 0, 1, "scull");
  42.     if(ret < 0)
  43.     {
  44.         printk(KERN_ALERT "alloc_chrdev_region error!\n");
  45.     }
  46.     
  47.     //字符设备注册
  48.     p_scull_dev = cdev_alloc();
  49.     p_scull_dev->owner = THIS_MODULE;
  50.     p_scull_dev->ops = &scull_fops;
  51.     cdev_add(p_scull_dev, scull_dev_num, 1);

  52.     return 0;
  53. }

  54. static void __exit scull_exit(void)
  55. {
  56.     printk(KERN_ALERT "goodbye scull dirver\n");
  57.     unregister_chrdev_region(scull_dev_num, 1);
  58.     cdev_del(p_scull_dev);
  59.     return ;
  60. }

  61. MODULE_LICENSE("GPL");

  62. module_init(scull_init);
  63. module_exit(scull_exit);
2. Makefile
  1. ARCH=x86
  2. ifeq ($(ARCH), x86)
  3.     CROSS_COMPILE=
  4.     KDIR = /lib/modules/$(shell uname -r)/build
  5. else
  6.     CROSS_COMPILE= /opt/EmbedSky/4.3.3/bin/arm-none-linux-gnueabi-
  7.     KDIR =/root/kernel/linux-2.6.30.4
  8. endif
  9. PWD = $(shell pwd)

  10. obj-m := scull.o
  11. all:
  12.     make -C $(KDIR) SUBDIRS=$(PWD) modules
  13. clean:
  14.     rm -f *.ko *.o *.mod.o *.mod.c .*.cmd *.symvers *.order
3. test程序
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. int main ( int argc, char *argv[] )
  7. {
  8.     int fd;
  9.     fd = open("/dev/scull", O_RDWR);
  10.     if(fd < 0)
  11.     {
  12.         perror("open");
  13.     }
  14.     return EXIT_SUCCESS;
  15. }
4. 测试
4.1 创建设备文件
     root@ubuntu:~/driver/2scull# cat /proc/devices | grep "scull" 
     250 scull
     root@ubuntu:~/driver/2scull# mknod /dev/scull c 250 0  //动态分配的主设备号250
     root@ubuntu:~/driver/2scull/test# ll /dev/scull
     crw-r--r-- 1 root root 250, 0 2012-08-28 15:19 /dev/scull
4.2  运行测试程序
     root@ubuntu:~/driver/2scull/test# ./scull_test
     root@ubuntu:~/driver# dmesg 
     [ 2110.894651] in scull driver
     [ 2114.160516] scull open!
     [ 2114.160679] scull release!
 2scull.zip   (下载后改名为: 2scull.tar.gz)
阅读(732) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~