/*驅動程式*/
#include#include#include#include#include#include#include#include#include#include module_license("gpl");
static struct class *firstdrv_class;
static struct device *firstdrv_class_dev;
static int first_drv_open(struct inode *inode,struct file *file)
static ssize_t first_drv_write(struct file *file,const char __user *buf,size_t count,loff_t * ppos)
/*頂乙個file_operation結構*/
static struct file_operations first_drv_fops = ;
int major;
/*驅動入口函式*/
int first_drv_init(void)
firstdrv_class_dev=device_create(firstdrv_class,null,mkdev(major,0),null,"xyz");
if(unlikely(is_err(firstdrv_class_dev)))
return ptr_err(firstdrv_class_dev);
return 0;
}void first_drv_exit(void)
/*宣告入口函式*/
module_init(first_drv_init);
module_exit(first_drv_exit);
#mkefile檔案
kern_dir=/work/system/linux-2.6.32.2
all:
make -c $(kern_dir) m=`pwd` modules
clean:
make -c $(kern_dir) m=`pwd` modules clean
obj-m +=first_drv.o
/*測試***/
#include#include#include#includeint main(int argc,char **argv)
期間碰到的問題:
1、fatal: modpost: gpl-incompatible module first_drv.ko uses gpl-only symbol 'device_create'
解決方法:在標頭檔案之後加上,加在其他位置無效
module_license("gpl");
2、 implicit declaration of function 'class_device_create'
解決方法:本次試驗所用核心為linux-2.6.32.2,已不支援此函式,改為device_create 標頭檔案為:device.h
3、./test: line 2: : not found
./test: line 2: 4: not found
./test: line 2: not found
./test: line 2: elf!$: not found
./test: line 1: syntax error: word unexpected (expecting ")")
解決方法:用錯編譯鏈,一定要用arm-linux-gcc 而不是gcc 即:arm-linux-gcc -o test test.c
現翻譯下**:
首先,實現裝置的open.write,read等程式,接著為這些方法找宿主,實現乙個file_operation結構,此結構相當於乙個一級容器,裡面實現了open等方法,接著為這個容器生成驅動模組,也就是register_chrdev函式,第乙個引數為主裝置號,如果為零,則核心為其分配主裝置號,即主裝置號大於0,第二個引數為裝置名,可以自行定義,第三個引數就是上面那個實現了open等操作方法的容器。接著建立類,通過類建立裝置(不大懂,現在只是照抄),最後宣告驅動入口函式,為了完整驅動解除安裝函式也宣告下啦 完畢!感謝韋東山老師!
簡單的乙個字元裝置驅動
include include include include include include include include include include include include 包含記憶體管理兩個核心函式 include memdev.h static int mem major me...
乙個字元驅動
實現乙個基本框架 define notice fmt,args.printk kern notice scull fmt,args define error fmt,args.printk kern err scull fmt,args static init int scull init void...
我的第乙個驅動程式
arm編譯器版本 3.32 核心版本 2.6.28.7 arm號 s3c2440 驅動原始碼 include include include include include include include include define device name leds define led majo...