字元裝置驅動
/*demo.c--動態註冊裝置號--自動建立裝置檔案*/
#include #include #include #include #include #define count 3
#define baseminor 0
#define name "demo_dev"
struct cdev *cdevp = null;
dev_t devno;
struct class *class = null;
struct device *device = null;
int major = 0;
int demo_open(struct inode *inode, struct file *file)
int demo_close(struct inode *inode, struct file *file)
ssize_t demo_read(struct file *file, char __user *user, size_t size, loff_t *lof)
ssize_t demo_write(struct file *fiel, const char __user *user, size_t size, loff_t *lof)
struct file_operations fops = ;
static int __init demo_init(void)
major = major(devno);
printk(kern_info "----major = %d----\n", major);
//1、申請裝置結構體cdev_alloc
cdevp = cdev_alloc();
if(null == cdevp)
//2、初始化裝置結構體cdev_init
cdev_init(cdevp, &fops);
//3、向核心註冊裝置結構體cdev_add
ret = cdev_add(cdevp, devno, count);
if(ret < 0)
//4、自動建立裝置檔案
class = class_create(this_module, name);
if(is_err(class))
for(i = 0;i < 3; i++)
} printk("--%d--%s--%s\n",__line__, __file__, __func__);
return 0;
err2:
for(--i;i>=0;i--)
class_destroy(class);
err1:
cdev_del(cdevp);
err:
unregister_chrdev_region(devno, count);
return ret;}
static void __exit demo_exit(void)
class_destroy(class);
cdev_del(cdevp);
unregister_chrdev_region(devno, count);
printk("--%d--%s--%s\n",__line__, __file__, __func__);
}module_init(demo_init);
module_exit(demo_exit);
module_license("gpl");
makefile
1 kerneldir:=/lib/modules/$(shell uname -r)/build/
2 #kerneldir:=/home/linux/yudw2018/1_test_yu/4_18051/kernel-3.4.39/
3 pwd:=$(shell pwd)
4 5 all:
6 make -c $(kerneldir) m=$(pwd) modules
7 clean:
8 make -c $(kerneldir) m=$(pwd) clean
9 obj-m:=demo.o
應用程式--測試呼叫open close 函式時,linux核心呼叫相應的demo_open,demo_close函式介面
/*demo_test.c*/
1 #include 2 #include 3 #include 4 #include 5
6 int main(int argc, const char *argv)
7 14
15 close(fd);
16 17 return 0;
18 }
執行步驟
linux@ubuntu:~/yudw2018/1_test_yu/4_18051/1_drivers/3_demo_dev$ make
linux@ubuntu:~/yudw2018/1_test_yu/4_18051/1_drivers/3_demo_dev$ sudo insmod demo.ko
linux@ubuntu:~/yudw2018/1_test_yu/4_18051/1_drivers/3_demo_dev$ sudo chmod 777 /dev/demo_dev0
linux@ubuntu:~/yudw2018/1_test_yu/4_18051/1_drivers$ gcc demo_test.c
linux@ubuntu:~/yudw2018/1_test_yu/4_18051/1_drivers$ dmesg
[24185.030974] ----major = 250----
[24185.033879] --100--/home/linux/yudw2018/1_test_yu/4_18051/1_drivers/3_demo_dev/demo.c--demo_init
[24284.165385] ----open----
[24284.165392] ----close----
linux@ubuntu:~/yudw2018/1_test_yu/4_18051/1_drivers$
//解除安裝
linux@ubuntu:~/yudw2018/1_test_yu/4_18051/1_drivers/3_demo_dev$ sudo rmmod demo
linux@ubuntu:~/yudw2018/1_test_yu/4_18051/1_drivers/3_demo_dev$ sudo dmesg -c
[24185.030974] ----major = 250----
[24185.033879] --100--/home/linux/yudw2018/1_test_yu/4_18051/1_drivers/3_demo_dev/demo.c--demo_init
[24284.165385] ----open----
[24284.165392] ----close----
[24353.027150] --128--/home/linux/yudw2018/1_test_yu/4_18051/1_drivers/3_demo_dev/demo.c--demo_exit
linux@ubuntu:~/yudw2018/1_test_yu/4_18051/1_drivers/3_demo_dev$
linux裝置驅動程式 字元裝置驅動程式
先留個 有一起學習驅動程式的加qq295699450 字元裝置驅動 這篇比較惱火。載入成功,但是讀不出來資料,有知道怎麼回事的,留個言,一起討論下 資料結構 struct scull mem struct scull dev dev 整個驅動程式 如下 include include include...
Linux裝置驅動程式 字元裝置驅動程式
1.檢視主裝置號,次裝置號 進入 dev目錄執行ls l,第四,五列分別為主次裝置號,10,180,1,5,這些是主裝置號,而60,63這些就是次裝置號 130 shell android dev ls l crw rw r system radio 10,60 1969 12 31 21 00 a...
裝置驅動 字元裝置驅動程式
linux下的應用程式在訪問字元裝置時,一般都是通過裝置檔案訪問的。裝置檔案一般都存放在 dev目錄下。字元裝置檔案的第乙個標誌是c,如下所示 總結 每乙個檔案代表乙個裝置,在時間前面有兩個用逗號隔開的數字,第乙個數字是主裝置號,第二個數字是次裝置號。一般認為乙個主裝置號對應乙個驅動程式,這裡列出的...