?? main.c
字號:
/* * main.c -- the bare scull char module * * 此代碼為ldd3例子,自己加了些注釋;希望可以和更多有著同樣興趣的鳥兒們一塊學習討論。 * 哪有注釋的不對的地方請發mail給我,或留言; * * author : liyangth@gmail.com * * date: 2007-2-7 * * Note:注釋的每一個關鍵的段都以[tag00]作了標簽,大家可以按照tag的順序閱讀; * e.g: 搜索 "Tag000" */#include <linux/config.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/init.h>#include <linux/kernel.h> /* printk() */#include <linux/slab.h> /* kmalloc() */#include <linux/fs.h> /* everything... */#include <linux/errno.h> /* error codes */#include <linux/types.h> /* size_t */#include <linux/proc_fs.h>#include <linux/fcntl.h> /* O_ACCMODE */#include <linux/seq_file.h>#include <linux/cdev.h>#include <asm/system.h> /* cli(), *_flags */#include <asm/uaccess.h> /* copy_*_user */#include "scull.h" /* local definitions *//* * Our parameters which can be set at load time. */int scull_major = SCULL_MAJOR;int scull_minor = 0;int scull_nr_devs = SCULL_NR_DEVS; /* number of bare scull devices */int scull_quantum = SCULL_QUANTUM;int scull_qset = SCULL_QSET;/* * 模塊參數,可在模塊轉載時賦值,很靈活方便; * e.g: * insmod scull.ko scull_major=111 scull_nr_devs=3 scull_quantum=1000 * *[形參說明] * 1 -- 變量名; * 2 -- 變量類型; * 3 -- sysfs入口項的訪問許可掩碼(一般用S_IRUGO就成);*/module_param(scull_major, int, S_IRUGO); module_param(scull_nr_devs, int, S_IRUGO);module_param(scull_quantum, int, S_IRUGO);module_param(scull_qset, int, S_IRUGO);MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");MODULE_LICENSE("Dual BSD/GPL");struct scull_dev *scull_devices; /* allocated in scull_init_module *//* Note: 不要把它理解成一個指向scull_dev結構的指針, 它其實是一個scull_dev結構數組,等待下面kmalloc分配多個我們scull設備空間 *//* * Empty out the scull device; 就像銷毀鏈表,和理解如何編寫一個字符驅動沒有關系,可以不看; * * must be called with the device semaphore held. 要注意一下了,肯定是要同步的; * */int scull_trim(struct scull_dev *dev){ struct scull_qset *next, *dptr; int qset = dev->qset; /* "dev" is not-null */ int i; for (dptr = dev->data; dptr; dptr = next) { /* all the list items */ if (dptr->data) { for (i = 0; i < qset; i++) kfree(dptr->data[i]); kfree(dptr->data); dptr->data = NULL; } next = dptr->next; kfree(dptr); } dev->size = 0; dev->quantum = scull_quantum; dev->qset = scull_qset; dev->data = NULL; return 0;}//Start: [Tag003] proc的實現,可以先不看;#ifdef SCULL_DEBUG /* use proc only if debugging *///這個是老方法實現的proc/* * The proc filesystem: function to read and entry */int scull_read_procmem(char *buf, char **start, off_t offset, int count, int *eof, void *data){ int i, j, len = 0; int limit = count - 80; /* Don't print more than this */ for (i = 0; i < scull_nr_devs && len <= limit; i++) { struct scull_dev *d = &scull_devices[i]; struct scull_qset *qs = d->data; if (down_interruptible(&d->sem)) return -ERESTARTSYS; len += sprintf(buf+len,"\nDevice %i: qset %i, q %i, sz %li\n", i, d->qset, d->quantum, d->size); for (; qs && len <= limit; qs = qs->next) { /* scan the list */ len += sprintf(buf + len, " item at %p, qset at %p\n", qs, qs->data); if (qs->data && !qs->next) /* dump only the last item */ for (j = 0; j < d->qset; j++) { if (qs->data[j]) len += sprintf(buf + len, " % 4i: %8p\n", j, qs->data[j]); } } up(&scull_devices[i].sem); } *eof = 1; return len;}//下面的是用新方法實現的/* * For now, the seq_file implementation will exist in parallel. The * older read_procmem function should maybe go away, though. *//* * Here are our sequence iteration methods. Our "position" is * simply the device number. */static void *scull_seq_start(struct seq_file *s, loff_t *pos){ if (*pos >= scull_nr_devs) return NULL; /* No more to read */ return scull_devices + *pos;}static void *scull_seq_next(struct seq_file *s, void *v, loff_t *pos){ (*pos)++; if (*pos >= scull_nr_devs) return NULL; return scull_devices + *pos;}static void scull_seq_stop(struct seq_file *s, void *v){ /* Actually, there's nothing to do here */}static int scull_seq_show(struct seq_file *s, void *v){ struct scull_dev *dev = (struct scull_dev *) v; struct scull_qset *d; int i; if (down_interruptible(&dev->sem)) return -ERESTARTSYS; seq_printf(s, "\nDevice %i: qset %i, q %i, sz %li\n", (int) (dev - scull_devices), dev->qset, dev->quantum, dev->size); for (d = dev->data; d; d = d->next) { /* scan the list */ seq_printf(s, " item at %p, qset at %p\n", d, d->data); if (d->data && !d->next) /* dump only the last item */ for (i = 0; i < dev->qset; i++) { if (d->data[i]) seq_printf(s, " % 4i: %8p\n", i, d->data[i]); } } up(&dev->sem); return 0;} /* * Tie the sequence operators up. */static struct seq_operations scull_seq_ops = { .start = scull_seq_start, .next = scull_seq_next, .stop = scull_seq_stop, .show = scull_seq_show};/* * Now to implement the /proc file we need only make an open * method which sets up the sequence operators. */static int scull_proc_open(struct inode *inode, struct file *file){ return seq_open(file, &scull_seq_ops);}/* * Create a set of file operations for our proc file. */static struct file_operations scull_proc_ops = { .owner = THIS_MODULE, .open = scull_proc_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release}; /* * Actually create (and remove) the /proc file(s). *///分別用新老方法實現了二個proc文件static void scull_create_proc(void){ struct proc_dir_entry *entry; create_proc_read_entry("scullmem", 0 /* default mode */, NULL /* parent dir */, scull_read_procmem, NULL /* client data */); entry = create_proc_entry("scullseq", 0, NULL); if (entry) entry->proc_fops = &scull_proc_ops;}static void scull_remove_proc(void){ /* no problem if it was not registered */ remove_proc_entry("scullmem", NULL /* parent dir */); remove_proc_entry("scullseq", NULL);}#endif /* SCULL_DEBUG *///End/* 開始實現對設備操作的方法集了,關鍵!!! *//* * Open and close *///[Tag004]/*open應完成的工作有: 1.檢查設備特定的錯誤(諸如設備未就緒或類似的硬件問題) 2.如果設備是首次打開,則對其進行初始化; 3.如有必要,更新f_op指針; 4.分配并填寫filp->private_data;(在這里我們只實現這項即可)*//*[形參說明] struct inode *inode -- 用它的i_cdev成員得到dev; struct file *filp -- 將得到的dev存放到他的成員private_data中;*/int scull_open(struct inode *inode, struct file *filp){ struct scull_dev *dev; /* device information */ dev = container_of(inode->i_cdev, struct scull_dev, cdev); /* [說明] 1.我們要填充的應該是我們自己的特殊設備,而不是鉗在他里面的字符設備結構; 2.inode結構的i_cdev成員這能提供基本字符設備結構; 3.這里利用了定義在<linux/kernel.h>中的宏來實現通過cdev得到dev; */ /* 以后read , write ,等操作的實現中就靠他來得到dev了; */ filp->private_data = dev; /* for other methods */ /* now trim to 0 the length of the device if open was write-only */ if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) { if (down_interruptible(&dev->sem)) return -ERESTARTSYS; scull_trim(dev); /* ignore errors */ up(&dev->sem); } return 0; /* success */}/* close device file, in here we do nothing *//* * [Tag005] * close應完成的工作有: * 1.釋放由open分配的,保存在filp->private_data中的所有內容; * 2.在最后一次關閉操作時關閉設備; * [注意:]并不是每次的close系統調用都會去調用到release. 在open時,也僅在open時才會創建 * 一個新的數據結構;在fork, dup時只是增加了這個結構中維護的一個引用計數; * 所以當這個引用計數為0時,調用的close才意味著要釋放設備數據結構,此時release才會被調用; */int scull_release(struct inode *inode, struct file *filp){ return 0;}/* * Follow the list * * 第一次調用時用于創建鏈表; * 然后就是找到第n個節點; * 對編寫驅動程序關系不大; */struct scull_qset *scull_follow(struct scull_dev *dev, int n){ struct scull_qset *qs = dev->data; /* Allocate first qset explicitly if need be */ if (! qs) { qs = dev->data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL); if (qs == NULL) return NULL; /* Never mind */ memset(qs, 0, sizeof(struct scull_qset)); } /* Then follow the list */ while (n--) { if (!qs->next) { qs->next = kmalloc(sizeof(struct scull_qset), GFP_KERNEL); if (qs->next == NULL) return NULL; /* Never mind */ memset(qs->next, 0, sizeof(struct scull_qset)); } qs = qs->next; continue; } return qs;}/*[Tag006] * Data management: read and write * [read和write的參數] * 1] filp -- 文件指針;用它的成員filp->private_data得到dev; * 2] buf -- 都是來自用戶空間的指針; * 3] count -- 緩沖區大小;(希望傳輸的字節數目) * 4] f_pos -- 指向一個長偏移量對象的指針,這個對象指明了用戶在文件中進行存取 * 操作的位置; * *[返回值] * 1]如果返回值等于count,則完成了所請求數目的字節傳輸; * 2]如果返回值是正,但小于count,則繼續讀或寫余下的數據; * 3]如果為0,則證明已經到了文件尾; * 4]如果為負,則發生了錯誤。會返回一個錯誤碼,該值指明了發生了什么錯誤。 * 錯誤碼在<linux/errno.h>中定義; * 例如:-EINTR (系統調用被中斷) * -EFAULT (無效地址) */ssize_t scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos){ struct scull_dev *dev = filp->private_data; struct scull_qset *dptr; /* the first listitem */ int quantum = dev->quantum, qset = dev->qset; int itemsize = quantum * qset; /* how many bytes in the listitem */ int item, s_pos, q_pos, rest; ssize_t retval = 0; if (down_interruptible(&dev->sem)) return -ERESTARTSYS; if (*f_pos >= dev->size) //操作位置到文件尾,或超出文件尾了 goto out; if (*f_pos + count > dev->size) //在當前位置所要讀的數目超過文件尾了 count = dev->size - *f_pos; //減小這次的期望讀取數目 /* find listitem, qset index, and offset in the quantum */ item = (long)*f_pos / itemsize; //確定是哪個鏈表項下,即哪個節點下; rest = (long)*f_pos % itemsize; //在這個鏈表項的什么位置(偏移量),用于下面找qset索引和偏移量; s_pos = rest / quantum; //在這個節點里**data這個指針數組的第幾行; q_pos = rest % quantum; //在這行,即這個量子里的偏移量; /* follow the list up to the right position (defined elsewhere) */ dptr = scull_follow(dev, item); //找到這個鏈表項 if (dptr == NULL || !dptr->data || ! dptr->data[s_pos]) goto out; /* don't fill holes *///以一個量子為單位傳,簡化了代碼; /* read only up to the end of this quantum */ if (count > quantum - q_pos) count = quantum - q_pos;/*
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -