|
cat >/dev/dpchr
ls
cat: write error: No space left on device
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/module.h>
- #include <asm/uaccess.h>
- #include <linux/cdev.h>
- #define DP_MAJOR 50
- #define DP_MINOR 0
- #define MYMEMSIZE 2000
- static int char_read(struct file *filp,char __user *buffer,size_t,loff_t *);
- static int char_open(struct inode *,struct file *);
- static int char_write(struct file *filp,const char __user *buffer,size_t ,loff_t*);
- static int char_release(struct inode *,struct file *);
- static char *dp_chrp;
- static char *arr;
- static int chropen;
- struct cdev *my_cdev;
- static int len;
- struct file_operations Fops = {
- .read = char_read,
- .write = char_write,
- .open = char_open,
- .release = char_release, /* a.k.a. close */
- };
- static int __init char_init(void)
- {
- printk(KERN_ALERT"Initing......\n");
- dev_t dev;
- dev=MKDEV(DP_MAJOR,DP_MINOR);
- my_cdev = cdev_alloc( );
- arr=kmalloc(1024,GFP_KERNEL);
- if(arr==NULL){
- printk(KERN_ALERT"kmalloc error\n");
- }
- if(my_cdev==NULL){
- return -1;
- }
- if(register_chrdev_region(dev,10,"dpchr")<0){
- printk(KERN_ALERT"Register char dev error\n");
- return -1;
- }
- //dp_chrp=kmalloc(MYMEMSIZE,GFP_KERNEL);
- dp_chrp=arr;
- if(!dp_chrp){
- printk(KERN_ALERT"Kmalloc error\n");
- return -1;
- }
- chropen=0;
- len=0;
- my_cdev->ops = &Fops;
- cdev_init(my_cdev,&Fops);
- cdev_add(my_cdev,dev,1);
- return 0;
- }
- static int char_open(struct inode *inode,struct file *file)
- {
- if(chropen==0)
- chropen++;
- else{
- printk(KERN_ALERT"Another process open the char device\n");
- return -1;
- }
- printk(KERN_ALERT"Device open!\n");
- try_module_get(THIS_MODULE);
- return 0;
- }
- static int char_release(struct inode *inode,struct file *file)
- {
- chropen--;
- module_put(THIS_MODULE);
- printk(KERN_ALERT"Device closeing......\n");
- return 0;
- }
- static int char_read(struct file *filp,char __user *buffer,size_t length,loff_t *offset)
- {
- int i=len;
- int j=0;
- // while(i>0&&j<100){
- //put_user(arr[j++],buffer++);
- copy_to_user(arr,buffer,len);
- // }
- return i;
- }
- static int char_write(struct file *filp,const char __user *buffer,size_t length,loff_t *offset)
- {
- int i;
- // for(i=0;i<length&&i<100;i++){
- // get_user(arr[i],buffer+i);
- // }
- len =copy_from_user(arr,buffer,10);
- //len=i;
- return len;
- }
-
- static void module_close()
- {
- len=0;
- printk(KERN_ALERT"Unloading..........\n");
- unregister_chrdev_region(MKDEV(DP_MAJOR,DP_MINOR),10);
- cdev_del(my_cdev);
- }
- module_init(char_init);
- module_exit(module_close);
复制代码 |
|