免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1780 | 回复: 2
打印 上一主题 下一主题

求助: ldd3里边的scull 驱动的问题。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-04 12:13 |只看该作者 |倒序浏览

  1. #include <linux/cdev.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/types.h>
  5. #include <linux/fs.h>
  6. #include <linux/kernel.h>
  7. #include <asm/uaccess.h>

  8. MODULE_LICENSE("Dual BSD/GPL");
  9. /*scull simple character utility for loading localities*/

  10. struct scull_qset {
  11.         void **data;
  12.         struct scull_qset *next;
  13. };

  14. struct scull_dev {
  15.         struct scull_qset *data;
  16.         int quantum;
  17.         int qset;
  18.         unsigned long size;
  19.         unsigned int access_key;
  20.         struct semaphore sem;

  21.         struct cdev cdev;
  22. };

  23. int scull_open(struct inode *inode, struct file *filp);
  24. ssize_t scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos);
  25. ssize_t scull_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos);
  26. int scull_release(struct inode *inode, struct file *filp);

  27. int scull_major = 0;
  28. static dev_t dev_num;
  29. int scull_minor = 0;
  30. int scull_nr_devs = 0;
  31. struct file_operations scull_fops = {
  32.         .owner = THIS_MODULE,
  33.         .open = scull_open,
  34.         .read = scull_read,
  35.         .write = scull_write,
  36.         .release = scull_release,
  37.         .llseek = NULL,
  38.         .ioctl = NULL,
  39. };

  40. int scull_release(struct inode *inode, struct file *filp)
  41. {
  42.         return 0;
  43. }

  44. int scull_open(struct inode *inode, struct file *filp)
  45. {
  46.         printk("scull open\n");

  47.         return 0;
  48. }

  49. ssize_t scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  50. {
  51.         printk("scull read\n");
  52.         return 0;
  53. }

  54. ssize_t scull_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
  55. {
  56.         printk("scull write\n");
  57.         return 0;
  58. }

  59. static int scull_init(void)
  60. {
  61.         printk("scull init\n");
  62.         struct scull_dev scull_dev;
  63.         int result, err;
  64.         if (scull_major) {
  65.                 dev_num = MKDEV(scull_major, scull_minor);
  66.                 result = register_chrdev_region(dev_num, scull_nr_devs, "scull");
  67.         } else {
  68.                 result = alloc_chrdev_region(&dev_num, scull_minor, scull_nr_devs, "scull");
  69.                 scull_major = MAJOR(dev_num);
  70.         }
  71.         if (result < 0) {
  72.                 //printk(KERN_WARNING "scull: can't get major %d\n", scull_major);
  73.                 printk("scull: can't get major %d\n", scull_major);
  74.                 return result;
  75.         }
  76.         //scull_setup_cdev(&scull_dev, 1);
  77.         cdev_init(&scull_dev.cdev, &scull_fops);
  78.         scull_dev.cdev.owner = THIS_MODULE;
  79.         //dev->cdev.ops = &scull_fops;   //jason mark : not need.
  80.         printk("cdev num is %d\n", MAJOR(dev_num), MINOR(dev_num));
  81.         err = cdev_add(&scull_dev.cdev, dev_num, 1);

  82.         if (err)
  83.                 printk("--->JASON scull setup cdev err is %d\n", err);
  84.         return 0;
  85. }

  86. static void scull_exit(void)
  87. {
  88.         printk("dev major and minor is %d, %d\n", MAJOR(dev_num), MINOR(dev_num));
  89.         unregister_chrdev_region(dev_num, 1);
  90.         printk("scull exit\n");
  91. }

  92. module_init(scull_init);
  93. module_exit(scull_exit);

复制代码



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>

  8. int main(void)
  9. {
  10.         int fd, ret;
  11.         char *buf = "a scull test";
  12.         char readbuf[30];

  13.         memset(readbuf, 0, 30);
  14.         fd = open("dev/scull", O_RDWR);
  15.         if (fd < 0) {
  16.                 printf("fd is error\n");
  17.                 return 1;
  18.         }
  19.                
  20.         ret = write(fd, buf, strlen(buf) + 1);
  21.         if (ret < 0) {
  22.                 printf("ret is not goodd\n");
  23.                 return 1;
  24.         }
  25.         close(fd);

  26.         fd = open("dev/scull", O_RDONLY);
  27.         ret = read(fd, readbuf, 30);
  28.         printf("%s\n", readbuf);
  29.         close(fd);

  30.         return(0);
  31. }
复制代码


#makefile :

obj-m := scull.o
KERNELDIR := /lib/modules/2.6.20.20/build
PWD :=$(shell pwd)

modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

test_scull: test_scull.c
        @gcc -o test_scull test_scull.c

clean :
        rm -rf *.ko
        rm -rf *.cmd
        rm -rf *.o




我之行的过程是 :
1。 insmod scull.ko
2.   mknod /dev/scull 253 c 0
3.   ./test_scull


但结果是fd is error , log 信息里边看到 init 又成功,分配的major 是253, 但没有看到open 的信息。。
弄了两天了没弄出来,, 大家帮帮忙。。


原来代码很多的,我简化了一些,方便大家看,但也应该看到open的信息阿。。

[ 本帖最后由 ldy2534 于 2008-1-4 12:16 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-01-04 12:26 |只看该作者
fd = open("dev/scull", O_RDWR);
仔细看看你的路径, 是不是少了什么.

论坛徽章:
0
3 [报告]
发表于 2008-01-04 13:12 |只看该作者
谢谢了。。。。可以了。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP