LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 584|回复: 2

[求助]如何使字符设备模块响应应用程序的读取?

[复制链接]
发表于 2005-4-18 21:25:00 | 显示全部楼层 |阅读模式
小弟想撰写一个虚拟的字符设备模块,设想cat设备文件时能显示"Hi,Everyone!"。以下是代码,编译能够通过,并且顺利挂载了设备文件,但是cat的时候却显示"cat: /dev/mdev: 没有那个设备或者地址",能否指点其中迷津?谢谢!

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

  7. static dev_t dev_num;

  8. static struct cdev chardev;

  9. ssize_t dev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) {
  10.   if (copy_to_user(buf, "Hi, everyone!", 14)) {
  11.     printk (KERN_ALERT "Read part!");
  12.     return 12;
  13.   }
  14.   printk(KERN_ALERT "Read whole!");
  15.   return 0;
  16. }

  17. int dev_open(struct inode *inode, struct file *filp) {
  18.   return 0;
  19. }


  20. struct file_operations fops = {
  21.   .owner = THIS_MODULE,
  22.   .read = dev_read,
  23.   .open = dev_open,
  24. };


  25. static int __init dev_init(void)
  26. {
  27.   if (alloc_chrdev_region(&dev_num,0,1,"dev1")<0){
  28.     printk(KERN_ALERT "Error in allocating Device Number!");
  29.     return -1;
  30.   }
  31.   cdev_init(&chardev,&fops);
  32.   chardev.owner = THIS_MODULE;
  33.   chardev.ops = &fops;
  34.   if (cdev_add(&chardev,dev_num,1) < 0) {
  35.     printk(KERN_ALERT "Error in adding cdev structure!");
  36.     return -1;
  37.   }
  38.   printk (KERN_ALERT "Succeed in adding cdev!");

  39.   printk(KERN_ALERT "Hello, device is on!");
  40.   return 0;
  41. }

  42. static void dev_exit(void)
  43. {
  44.   unregister_chrdev_region(dev_num,1);
  45.   cdev_del(&chardev);
  46.   printk(KERN_ALERT "Goodbye!");
  47. }

  48. module_init(dev_init);
  49. module_exit(dev_exit);
复制代码
发表于 2005-4-24 09:49:55 | 显示全部楼层
你有没有建立正确的设备文件? 你的代码中好像没有指定设备号。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-27 23:41:07 | 显示全部楼层
设备文件不是在代码中建立的,问题已经找到,是设备号指定的错误
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表