資源簡介
很好的東西和大家分享,是基于linux2.6內核的。很好用的!
代碼片段和文件信息
//模塊定義
#include?
#include?
#include?
#include?
#include?
#include?
#include?
MODULE_LICENSE(“GPL“);
static?int?chdr_major?=?150;//主設備號
static?int?chdr_minor?=?0;//次設備號
static?int?chdr_nr_devs?=?1;
static?int?open_nr=0;//打開設備的進程數
static?struct?cdev?*chdr_cdev;//設備定義聲明
static?char?buffer[100];//緩沖區
//函數聲明
static?int?chdr_open(struct?inode?*inode?struct?file?*filp);
static?int?chdr_release(struct?inode?*inode?struct?file*?filp);
static?ssize_t?chdr_read(struct?file?*file?char?__user?*buf?size_t?count?loff_t?*f_pos);
static?ssize_t?chdr_write(struct?file?*file?const?char?__user?*buf?size_t?count?loff_t?*f_pos);
//設備模塊接口
static?struct?file_operations?chdr_fops?=?{
????.owner???=?THIS_MODULE
????.read????=?chdr_read
????.write???=?chdr_write
????.open????=?chdr_open
????.release?=?chdr_release
};???
//打開函數
static?int?chdr_open(struct?inode?*inode?struct?file?*filp)
{
????if?(open_nr?==?0)?{
????????open_nr++;
????}?else?{
????????printk(KERN_ALERT?“Another?process?open?the?char?device.\n“);//進程掛起
????????return?-1;
????}
????return?0;
}
//閱讀函數
static?ssize_t?chdr_read(struct?file?*file?char?__user?*buf?size_t?count?loff_t?*f_pos)
{
????if?(buf?==?NULL)
????????return?0;
????if?(count?>?100)
????????count?=?100;
????copy_to_user(buf?buffer?count);//讀緩沖
????return?count;
}
//寫入函數
static?ssize_t?chdr_write(struct?file?*file?const?char?__user?*buf?size_t?count?loff_t?*f_pos)
{
????if?(buf?==?NULL)
????????return?0;
????if?(count?>?100)
????????count?=?100;
????copy_from_user(buffer?buf?count);//寫緩沖
????return?count;
}
//釋放設備函數
static?int?chdr_release(struct?inode?*inode?struct?file*?filp)
{
????open_nr--;//進程數減1
????return?0;
}
//注冊設備函數
static?int?__init?chdr_init(void)
{
????int?result?err;
????dev_t?dev?=?0;
????printk(KERN_ALERT?“Begin?to?init?Char?Device!\n%d“open_nr);
//注冊設備
????if?(chdr_major)?{
????????dev?=?MKDEV(chdr_major?chdr_minor);
????????result?=?register_chrdev_region(dev?chdr_nr_devs?“chdr“);
????}?else?{
????????result?=?alloc_chrdev_region(&dev?chdr_minor?chdr_nr_devs?“chdr“);
????????chdr_major?=?MAJOR(dev);
????}
????if?(result?0)?{
????????printk(KERN_WARNING?“chdr:?cannot?get?major?%d\n“?chdr_major);
????????return?result;
????}???
???
????chdr_cdev?=?cdev_alloc();
????if?(chdr_cdev?==?NULL)?{
????????printk(KERN_WARNING?“chdr:?alloc?cdev?failed.\n“);
????????return?-1;
????}
//初始化設備
????chdr_cdev->owner?=?THIS_MODULE;
????chdr_cdev->ops?=?&chdr_fops;
????cdev_init(chdr_cdev?&chdr_fops);
????err?=?cdev_add(chdr_cdev?dev?1);
????if?(err)?{
????????printk(KERN_NOTICE?“Error?%d?adding?chdr.\n“?err);
????}
????open_nr?=?0;
printk(“%d“open_nr);
????return?0;
}
//注銷設備函數
static?void?__exit?chdr_exit(void)
{
????dev_t?dev;
????printk(KERN_ALERT?“Unloading...\n“);
????dev?=?MKDEV(chdr_major?chdr_minor);
????unregister_chrdev_region(dev?chdr_nr_devs);//注銷設備
????cdev_del(chdr_cd
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????206??2009-06-03?13:21??驅動設計\Makefile
?????文件???????3294??2009-06-03?13:31??驅動設計\mydrive.c
?????文件????????875??2009-06-03?13:32??驅動設計\test.c
?????文件??????24251??2009-06-15?23:34??驅動設計\設備驅動實習報告.docx
?????目錄??????????0??2009-06-15?23:35??驅動設計
-----------?---------??----------?-----??----
????????????????28626????????????????????5
評論
共有 條評論