都市修真txt:linux字符设备驱动模版本 - hisashi的日志 - 网易博客

来源:百度文库 编辑:偶看新闻 时间:2024/05/05 02:00:48
  1 #include
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  9 #include
 10 #include
 11 #include
 12
 13
 14 #define MAJOR 123
 15
 16 static int XXXX_major=MAJOR;
 17
 18 struct XXXX_dev
 19 {struct cdev cdev;
 20  int data;
 21 };
 22
 23 struct  XXXX_dev* XXXX_devp;
 24
 25 int XXXX_open (struct inode *inode, struct file *filp )
 26 {filp->private_data=XXXX_devp;
 27  return 0;
 28 }
 29
 30 int globalmem_release(struct inode *inode,struct file *filp)
 31 {return 0;
 32 }
 33
 34 static int XXXX_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
 35 {struct XXXX*dev=filp->private_data;
 36     switch(cmd)
 37         {case 1: ~~~~~~~~~~~~~~~~~~~~~~~
 38                 break;
 39          default:
 40               return -EINVAL;
 41         }
 42 return 0;
 43 }
 44
 45 static ssize_t XXXX_read(struct file *filp,char __user *buf,size_t size,loff_t *ppos)
 46 {struct XXXX_dev *dev=filp->private_data;
 47
 48  ~~~~~~~~~~~~~~~~~~~~~~~~
 49  ~~~~~~~~~~~~~~~~~~~~~~~~
 50 }
 51
 52
 53 static ssize_t XXXX_write(struct file *filp ,const char __user *buf,size_t size,loff_t *ppos)
 54 {struct XXXX_dev *dev = filp->private_data;
 55 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 56 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 57 }
 58
 59
 60
 61 static loff_t XXXX_llseek(struct file *filp,loff_t offset,int orig)
 62 {
 63 }
 64
 65
 66
 67 static const struct file_operations XXXX_fops =
 68 {
 69     .owner=THIS_MODULE,
 70     .llseek=XXXX_llseek,
 71     .read=XXXX_read,
 72     .write=XXXX_write,
 73     .ioctl=XXXX_ioctl,
 74     .open=XXXX_open,
 75     .release=XXXX_release,
 76 };
 77
 78
 79 static void XXXX_setup_cdev(struct XXXX_dev *dev, int index)
 80 {int err,devno=MKDEV(XXXX_major,index);
 81 cdev_init(&dev->cdev,&XXXX_fops);
 82 dev->cdev.owner=THIS_MODULE;
 83 dev->cdev.ops=&XXXX_fops;
 84 err=cdev_add(&dev->cdev,devno,1);
 85 if(err) printk(KERN_NOTICE "Error %d adding led %d",err,index);
 86 }
 87
 88 int XXXX_init(void)
 89 {int result;
 90  dev_t devno=MKDEV(XXXX_major,0);
 91  if(XXXX_major) result=register_chrdev_region(devno,1,"globalmem");
 92  else
 93  {result=alloc_chrdev_region(&devno,0,1,"globalmem");
 94   XXXX_major=MAJOR(devno);
 95  }
 96  if(result<0)
 97      return result;
 98  XXXX_devp=kmalloc(sizeof(struct XXXX_dev),GFP_KERNEL);
 99  if(!XXXX_devp)
100  {return -ENOMEM;
101   goto fail_malloc;
102      }
103  memset(XXXX_devp,0,sizeof(struct XXXX_dev));
104  XXXX_setup_cdev(XXXX_devp,0);
105  return 0;
106  fail_malloc:unregister_chrdev_region(devno,1);
107  return result;
108 }
109
110
111 void globalmem_exit(void)
112 {cdev_del(&XXXX_devp->cdev);
113  kfree(XXXX_devp);
114  unregister_chrdev_region(MKDEV(XXXX_major,0),1);
115 }
116
117 MODULE_AUTHOR("YE Zongtai");
118 MODULE_LICENSE("Dual BSD/GPL");
119
120 module_param(XXXX_major,int,S_IRUGO);
121
122 module_init(XXXX_init);
123 module_exit(XXXX_exit);
~