資源簡介
設計和實現一個虛擬命名管道(FIFO)的字符設備。寫一個模塊化的字符設備驅動程序
通過一個簡單的設備驅動的實現過程。學會Linux中設備驅動程序的編寫

代碼片段和文件信息
#include??
#include
#include?
#include?
#include?
#include?
#include
#include?
#include?
#include?
#include?
MODULE_AUTHOR(“Me“);
MODULE_LICENSE(“GPL“);
#define?MYCDEV_MAJOR?231?/*給定的主設備號*/
#define?MYCDEV_SIZE?1000
static?int?mycdev_open(struct?inode?*inode?struct?file?*fp)
{
????return?0;
}
static?int?mycdev_release(struct?inode?*inode?struct?file?*fp)
{
????return?0;
}
static?ssize_t?mycdev_read(struct?file?*fp?char?__user?*buf?size_t?size?loff_t?*pos)
{
????unsigned?long?p?=?*pos;
????unsigned?int?count?=?size;
????//char?kernel_buf[MYCDEV_SIZE]=“This?is?mycdev!“;
????char?kernel_buf[MYCDEV_SIZE];
????
????memset(kernel_buf0MYCDEV_SIZE);
????memcpy(kernel_buf?“This?is?mycdev!“?MYCDEV_SIZE);
????if(p?>=?MYCDEV_SIZE)
????????return?-1;
????if(count?>?MYCDEV_SIZE)
????????count?=?MYCDEV_SIZE?-?p;
????if?(copy_to_user(buf?kernel_buf?count)?!=?0)?{
????????printk(“read?error!\n“);
????????return?-1;
????}
????/*
????for?(i?=?0;?i?????????__put_user(i?buf);//write?‘i‘?from?kernel?space?to?user?space‘s?buf;
????????buf++;
????}
????*/
????printk(“reader:?%d?bytes?was?read...\n“?count);
????return?count;
}
static?ssize_t?mycdev_write(struct?file?*fp?const?char?__user?*buf?size_t?size?loff_t?*pos)
{
????return?size;
}
/*?填充?mycdev的?file?operation?結構*/
static?const?struct?file_operations?mycdev_fops?=
{
????.owner?=?THIS_MODULE
????.read?=?mycdev_read
????.write?=?mycdev_write
????.open?=?mycdev_open
????.release?=?mycdev_release
};
/*模塊初始化函數*/
static?int?__init?mycdev_init(void)
{
????int?ret;
????printk(“mycdev?module?is?staring..\n“);
????ret=register_chrdev(MYCDEV_MAJOR“my_cdev“&mycdev_fops);?/*注冊驅動程序*/
????if(ret<0){
????????printk(“register?failed..\n“);
????????return?0;
????}else{
????????printk(“register?success..\n“);
????}
????return?0;
}
/*模塊卸載函數*/
static?void?__exit?mycdev_exit(void)
{
????printk(“mycdev?module?is?leaving..\n“);
????unregister_chrdev(MYCDEV_MAJOR“my_cdev“);?/*注銷驅動程序*/
}
module_init(mycdev_init);
module_exit(mycdev_exit);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-03-07?16:12??1400002100?楊永博?實驗五??設備驅動\
?????目錄???????????0??2018-03-07?16:12??1400002100?楊永博?實驗五??設備驅動\設備驅動\
?????文件??????102400??2018-03-07?16:12??1400002100?楊永博?實驗五??設備驅動\設備驅動\實驗五??設備驅動.doc
?????目錄???????????0??2018-03-07?15:48??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\
?????文件??????????91??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\.mycdev.ko.cmd
?????文件?????????222??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\.mycdev.ko.unsigned.cmd
?????文件???????23661??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\.mycdev.mod.o.cmd
?????文件???????28337??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\.mycdev.o.cmd
?????目錄???????????0??2018-03-07?15:48??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\.tmp_versions\
?????文件??????????47??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\.tmp_versions\mycdev.mod
?????文件?????????222??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\Makefile
?????文件???????????0??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\Module.symvers
?????文件??????????31??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\modules.order
?????文件????????2359??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\mycdev.c
?????文件??????175369??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\mycdev.ko
?????文件??????175369??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\mycdev.ko.unsigned
?????文件?????????937??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\mycdev.mod.c
?????文件???????73068??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\mycdev.mod.o
?????文件??????103488??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\mycdev.o
?????文件????????5195??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\test
?????文件?????????493??2017-04-27?14:45??1400002100?楊永博?實驗五??設備驅動\設備驅動\源代碼\test.c
?????文件?????????155??2017-04-27?14:41??1400002100?楊永博?實驗五??設備驅動\運行步驟.txt
- 上一篇:Linux內存分配與回收
- 下一篇:基于51單片機pwm控制的呼吸燈程序
評論
共有 條評論