?? globalmem_two.c
字號(hào):
/*====================================================================== A globalmem driver as an example of char device drivers There are two same globalmems in this driver This example is to introduce the function of file->private_data The initial developer of the original code is Baohua Song <author@linuxdriver.cn>. All Rights Reserved.======================================================================*/#include <linux/module.h>#include <linux/types.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/sched.h>#include <linux/init.h>#include <linux/cdev.h>#include <asm/io.h>#include <asm/system.h>#include <asm/uaccess.h>#define GLOBALMEM_SIZE 0x1000 /*全局內(nèi)存最大4K字節(jié)*/#define MEM_CLEAR 0x1 /*清0全局內(nèi)存*/#define GLOBALMEM_MAJOR 254 /*預(yù)設(shè)的globalmem的主設(shè)備號(hào)*/static int globalmem_major = GLOBALMEM_MAJOR;/*globalmem設(shè)備結(jié)構(gòu)體*/struct globalmem_dev { struct cdev cdev; /*cdev結(jié)構(gòu)體*/ unsigned char mem[GLOBALMEM_SIZE]; /*全局內(nèi)存*/ };struct globalmem_dev *globalmem_devp; /*設(shè)備結(jié)構(gòu)體指針*//*文件打開函數(shù)*/int globalmem_open(struct inode *inode, struct file *filp){ /*將設(shè)備結(jié)構(gòu)體指針賦值給文件私有數(shù)據(jù)指針*/ struct globalmem_dev *dev; dev = container_of(inode->i_cdev,struct globalmem_dev,cdev); filp->private_data = dev; return 0;}/*文件釋放函數(shù)*/int globalmem_release(struct inode *inode, struct file *filp){ return 0;}/* ioctl設(shè)備控制函數(shù) */static int globalmem_ioctl(struct inode *inodep, struct file *filp, unsigned int cmd, unsigned long arg){ struct globalmem_dev *dev = filp->private_data;/*獲得設(shè)備結(jié)構(gòu)體指針*/ switch (cmd) { case MEM_CLEAR: memset(dev->mem, 0, GLOBALMEM_SIZE); printk(KERN_INFO "globalmem is set to zero\n"); break; default: return - EINVAL; } return 0;}/*讀函數(shù)*/static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos){ unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct globalmem_dev *dev = filp->private_data; /*獲得設(shè)備結(jié)構(gòu)體指針*/ /*分析和獲取有效的寫長(zhǎng)度*/ if (p >= GLOBALMEM_SIZE) return count ? - ENXIO: 0; if (count > GLOBALMEM_SIZE - p) count = GLOBALMEM_SIZE - p; /*內(nèi)核空間->用戶空間*/ if (copy_to_user(buf, (void*)(dev->mem + p), count)) { ret = - EFAULT; } else { *ppos += count; ret = count; printk(KERN_INFO "read %d bytes(s) from %d\n", count, p); } return ret;}/*寫函數(shù)*/static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos){ unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct globalmem_dev *dev = filp->private_data; /*獲得設(shè)備結(jié)構(gòu)體指針*/ /*分析和獲取有效的寫長(zhǎng)度*/ if (p >= GLOBALMEM_SIZE) return count ? - ENXIO: 0; if (count > GLOBALMEM_SIZE - p) count = GLOBALMEM_SIZE - p; /*用戶空間->內(nèi)核空間*/ if (copy_from_user(dev->mem + p, buf, count)) ret = - EFAULT; else { *ppos += count; ret = count; printk(KERN_INFO "written %d bytes(s) from %d\n", count, p); } return ret;}/* seek文件定位函數(shù) */static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig){ loff_t ret = 0; switch (orig) { case 0: /*相對(duì)文件開始位置偏移*/ if (offset < 0) { ret = - EINVAL; break; } if ((unsigned int)offset > GLOBALMEM_SIZE) { ret = - EINVAL; break; } filp->f_pos = (unsigned int)offset; ret = filp->f_pos; break; case 1: /*相對(duì)文件當(dāng)前位置偏移*/ if ((filp->f_pos + offset) > GLOBALMEM_SIZE) { ret = - EINVAL; break; } if ((filp->f_pos + offset) < 0) { ret = - EINVAL; break; } filp->f_pos += offset; ret = filp->f_pos; break; default: ret = - EINVAL; break; } return ret;}/*文件操作結(jié)構(gòu)體*/static const struct file_operations globalmem_fops ={ .owner = THIS_MODULE, .llseek = globalmem_llseek, .read = globalmem_read, .write = globalmem_write, .ioctl = globalmem_ioctl, .open = globalmem_open, .release = globalmem_release,};/*初始化并注冊(cè)cdev*/static void globalmem_setup_cdev(struct globalmem_dev *dev, int index){ int err, devno = MKDEV(globalmem_major, index); cdev_init(&dev->cdev, &globalmem_fops); dev->cdev.owner = THIS_MODULE; dev->cdev.ops = &globalmem_fops; err = cdev_add(&dev->cdev, devno, 1); if (err) printk(KERN_NOTICE "Error %d adding LED%d", err, index);}/*設(shè)備驅(qū)動(dòng)模塊加載函數(shù)*/int globalmem_init(void){ int result; dev_t devno = MKDEV(globalmem_major, 0); /* 申請(qǐng)?jiān)O(shè)備號(hào)*/ if (globalmem_major) result = register_chrdev_region(devno, 2, "globalmem"); else /* 動(dòng)態(tài)申請(qǐng)?jiān)O(shè)備號(hào) */ { result = alloc_chrdev_region(&devno, 0, 2, "globalmem"); globalmem_major = MAJOR(devno); } if (result < 0) return result; /* 動(dòng)態(tài)申請(qǐng)2個(gè)設(shè)備結(jié)構(gòu)體的內(nèi)存*/ globalmem_devp = kmalloc(2*sizeof(struct globalmem_dev), GFP_KERNEL); if (!globalmem_devp) /*申請(qǐng)失敗*/ { result = - ENOMEM; goto fail_malloc; } memset(globalmem_devp, 0, 2*sizeof(struct globalmem_dev)); globalmem_setup_cdev(&globalmem_devp[0], 0); globalmem_setup_cdev(&globalmem_devp[1], 1); return 0; fail_malloc: unregister_chrdev_region(devno, 2); return result;}/*模塊卸載函數(shù)*/void globalmem_exit(void){ cdev_del(&(globalmem_devp[0].cdev)); cdev_del(&(globalmem_devp[1].cdev)); /*注銷cdev*/ kfree(globalmem_devp); /*釋放設(shè)備結(jié)構(gòu)體內(nèi)存*/ unregister_chrdev_region(MKDEV(globalmem_major, 0), 2); /*釋放設(shè)備號(hào)*/}MODULE_AUTHOR("Song Baohua");MODULE_LICENSE("Dual BSD/GPL");module_param(globalmem_major, int, S_IRUGO);module_init(globalmem_init);module_exit(globalmem_exit);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -