├── 01_helloworld ├── Makefile ├── Module.symvers ├── helloworld.c ├── helloworld.ko ├── helloworld.mod.c ├── helloworld.mod.o ├── helloworld.o └── modules.order ├── 02_cdev ├── Makefile ├── Module.symvers ├── cdev.c ├── cdev.ko ├── cdev.mod.c ├── cdev.mod.o ├── cdev.o ├── modules.order ├── sg90.c └── test.c ├── 03_led ├── app │ ├── app │ └── app.c └── module │ ├── Makefile │ ├── Module.symvers │ ├── file.c │ ├── file.ko │ ├── file.mod.c │ ├── file.mod.o │ ├── file.o │ └── modules.order ├── 04_spinlock ├── app │ ├── app │ └── app.c └── module │ ├── Makefile │ ├── Module.symvers │ ├── modules.order │ ├── spinlock.c │ ├── spinlock.ko │ ├── spinlock.mod │ ├── spinlock.mod.c │ ├── spinlock.mod.o │ └── spinlock.o ├── 05_wait_queue ├── app │ ├── read │ ├── read.c │ ├── write │ └── write.c └── module │ ├── Makefile │ ├── Module.symvers │ ├── modules.order │ ├── wq.c │ ├── wq.ko │ ├── wq.mod.c │ ├── wq.mod.o │ └── wq.o ├── 06_ioctl ├── app │ ├── ioctl │ └── ioctl.c └── module │ ├── Makefile │ ├── Module.symvers │ ├── ioctl.c │ ├── ioctl.ko │ ├── ioctl.mod.c │ ├── ioctl.mod.o │ ├── ioctl.o │ └── modules.order ├── 07_interrupt ├── Makefile ├── Module.symvers ├── interrupt.c ├── interrupt.ko ├── interrupt.mod.c ├── interrupt.mod.o ├── interrupt.o └── modules.order ├── 08_tasklet └── module │ ├── Makefile │ ├── Module.symvers │ ├── interrupt.c │ ├── interrupt.ko │ ├── interrupt.mod.c │ ├── interrupt.mod.o │ ├── interrupt.o │ └── modules.order ├── 09_softirq └── module │ ├── Makefile │ ├── Module.symvers │ ├── interrupt.c │ ├── interrupt.ko │ ├── interrupt.mod.c │ ├── interrupt.mod.o │ ├── interrupt.o │ └── modules.order ├── 10_platform ├── platform_device │ ├── Makefile │ ├── Module.symvers │ ├── modules.order │ ├── platform_device.c │ ├── platform_device.ko │ ├── platform_device.mod.c │ ├── platform_device.mod.o │ └── platform_device.o └── platform_driver │ ├── Makefile │ ├── Module.symvers │ ├── modules.order │ ├── platform_driver.c │ ├── platform_driver.ko │ ├── platform_driver.mod.c │ ├── platform_driver.mod.o │ └── platform_driver.o ├── 11_probe ├── Makefile ├── Module.symvers ├── modules.order ├── probe.c ├── probe.ko ├── probe.mod.c ├── probe.mod.o └── probe.o ├── 12_platform_led ├── app │ ├── app │ └── app.c └── module │ ├── Makefile │ ├── Module.symvers │ ├── modules.order │ ├── platform_led.c │ ├── platform_led.ko │ ├── platform_led.mod.c │ ├── platform_led.mod.o │ └── platform_led.o ├── 13_devicetree_probe ├── dts │ ├── boot.img │ └── rk3568-evb1-ddr4-v10-linux.dts └── module │ ├── Makefile │ ├── Module.symvers │ ├── modules.order │ ├── platform_driver.c │ ├── platform_driver.ko │ ├── platform_driver.mod.c │ ├── platform_driver.mod.o │ └── platform_driver.o └── README.md /01_helloworld/Makefile: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export ARCH=arm64 3 | export CROSS_COMPILE=aarch64-linux-gnu- 4 | obj-m += helloworld.o #此处要和你的驱动源文件同名 5 | KDIR :=/home/topeet/Linux/rk356x_linux/kernel #这里是你的内核目录 6 | PWD ?= $(shell pwd) 7 | all: 8 | make -C $(KDIR) M=$(PWD) modules #make操作 9 | clean: 10 | make -C $(KDIR) M=$(PWD) clean #make clean操作 11 | -------------------------------------------------------------------------------- /01_helloworld/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/01_helloworld/Module.symvers -------------------------------------------------------------------------------- /01_helloworld/helloworld.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | static int __init helloworld_init(void) 4 | { 5 | printk(KERN_EMERG "helloworld_init\r\n"); 6 | return 0; 7 | } 8 | static void __exit helloworld_exit(void) 9 | { 10 | printk(KERN_EMERG "helloworld_exit\r\n"); 11 | } 12 | 13 | module_init(helloworld_init); //入口 14 | module_exit(helloworld_exit); //出口 15 | MODULE_LICENSE("GPL v2"); 16 | MODULE_AUTHOR("topeet"); 17 | -------------------------------------------------------------------------------- /01_helloworld/helloworld.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/01_helloworld/helloworld.ko -------------------------------------------------------------------------------- /01_helloworld/helloworld.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /01_helloworld/helloworld.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/01_helloworld/helloworld.mod.o -------------------------------------------------------------------------------- /01_helloworld/helloworld.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/01_helloworld/helloworld.o -------------------------------------------------------------------------------- /01_helloworld/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/01/helloworld.ko 2 | -------------------------------------------------------------------------------- /02_cdev/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m := cdev.o 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #这里是你的内核目录 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | -------------------------------------------------------------------------------- /02_cdev/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/02_cdev/Module.symvers -------------------------------------------------------------------------------- /02_cdev/cdev.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | static dev_t dev_num;//定义dev_t类型(32位大小)的变量dev_num,用来存放设备号 8 | static struct cdev cdev_test;//定义cdev结构体类型的变量cdev_test 9 | static struct file_operations cdev_test_ops = { 10 | .owner=THIS_MODULE,//将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 11 | };//定义file_operations结构体类型的变量cdev_test_ops 12 | 13 | static int __init module_cdev_init(void)//驱动入口函数 14 | { 15 | int ret;//定义int类型变量ret,进行函数返回值判断 16 | int major,minor;//定义int类型的主设备号major和次设备号minor 17 | ret = alloc_chrdev_region(&dev_num,0,1,"chrdev_name");//自动获取设备号,设备名为chrdev_name 18 | if (ret < 0){ 19 | printk("alloc_chrdev_region is error\n"); 20 | } 21 | printk("alloc_register_region is ok\n"); 22 | major = MAJOR(dev_num);//使用MAJOR()函数获取主设备号 23 | minor = MINOR(dev_num);//使用MINOR()函数获取次设备号 24 | printk("major is %d\n",major); 25 | printk("minor is %d\n",minor); 26 | cdev_init(&cdev_test,&cdev_test_ops);//使用cdev_init()函数初始化cdev_test结构体,并链接到cdev_test_ops结构体 27 | cdev_test.owner = THIS_MODULE;//将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 28 | ret = cdev_add(&cdev_test,dev_num,1);//使用cdev_add()函数进行字符设备的添加 29 | if(ret < 0 ){ 30 | printk("cdev_add is error\n"); 31 | } 32 | printk("cdev_add is ok\n"); 33 | return 0; 34 | } 35 | 36 | static void __exit module_cdev_exit(void)//驱动出口函数 37 | { 38 | cdev_del(&cdev_test);//使用cdev_del()函数进行字符设备的删除 39 | unregister_chrdev_region(dev_num,1);//释放字符驱动设备号 40 | printk("module exit \n"); 41 | } 42 | 43 | module_init(module_cdev_init);//注册入口函数 44 | module_exit(module_cdev_exit);//注册出口函数 45 | MODULE_LICENSE("GPL v2");//同意GPL开源协议 46 | MODULE_AUTHOR("topeet"); //作者信息 47 | 48 | -------------------------------------------------------------------------------- /02_cdev/cdev.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/02_cdev/cdev.ko -------------------------------------------------------------------------------- /02_cdev/cdev.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /02_cdev/cdev.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/02_cdev/cdev.mod.o -------------------------------------------------------------------------------- /02_cdev/cdev.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/02_cdev/cdev.o -------------------------------------------------------------------------------- /02_cdev/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/05/cdev.ko 2 | -------------------------------------------------------------------------------- /02_cdev/sg90.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | 16 | static dev_t dev_num;//定义dev_t类型(32位大小)的变量dev_num,用来存放设备号 17 | static struct cdev cdev_test;//定义cdev结构体类型的变量cdev_test 18 | 19 | struct pwm_led_data{ 20 | int sum_count; 21 | int hight_count; 22 | struct gpio_desc* gpiod; 23 | struct hrtimer pwm_timer; 24 | int time; 25 | }; 26 | 27 | struct pwm_led_data* data; 28 | 29 | enum hrtimer_restart pwm_timer_func(struct hrtimer* timer) 30 | { 31 | static int timer_count = 0; 32 | struct pwm_led_data* mydata = container_of(timer, struct pwm_led_data, pwm_timer); 33 | 34 | if(timer_count == mydata->sum_count){ 35 | gpiod_set_vcalue(mydata->gpiod, 1); 36 | timer_count = 0; 37 | } 38 | 39 | if(timer_count == mydata->hight_count){ 40 | gpiod_set_vcalue(mydata->gpiod, 0); 41 | timer_count = 0; 42 | } 43 | 44 | if(mydata->hight_count == 0){ 45 | timer_count = 0; 46 | } 47 | timer_count++; 48 | } 49 | 50 | static struct file_operations cdev_test_ops = { 51 | .owner=THIS_MODULE,//将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 52 | };//定义file_operations结构体类型的变量cdev_test_ops 53 | 54 | const struct of_device_id sg90_of_device_id[] = { 55 | {.compatible = "sg90"}, 56 | {} 57 | }; 58 | 59 | struct platform_driver sg90_platform_driver{ 60 | .driver = { 61 | .name = sg90, 62 | .of_match_table = sg90_of_device_id, 63 | }, 64 | 65 | .probe = sg90_probe, 66 | .remove = sg90_remove, 67 | 68 | }; 69 | 70 | int sg90_probe(struct platform_device* dev){ 71 | 72 | int ret;//定义int类型变量ret,进行函数返回值判断 73 | int major,minor;//定义int类型的主设备号major和次设备号minor 74 | ret = alloc_chrdev_region(&dev_num,0,1,"sg90");//自动获取设备号,设备名为chrdev_name 75 | if (ret < 0){ 76 | printk("alloc_chrdev_region is error\n"); 77 | } 78 | 79 | data = kmalloc(sizeof(struct pwm_led_data), GFP_KERNEl); 80 | data->sum_count = 20; 81 | data->hight_count = 10; 82 | printk("alloc_register_region is ok\n"); 83 | major = MAJOR(dev_num);//使用MAJOR()函数获取主设备号 84 | minor = MINOR(dev_num);//使用MINOR()函数获取次设备号 85 | printk("major is %d\n",major); 86 | printk("minor is %d\n",minor); 87 | cdev_init(&cdev_test,&cdev_test_ops);//使用cdev_init()函数初始化cdev_test结构体,并链接到cdev_test_ops结构体 88 | cdev_test.owner = THIS_MODULE;//将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 89 | ret = cdev_add(&cdev_test,dev_num,1);//使用cdev_add()函数进行字符设备的添加 90 | if(ret < 0 ){ 91 | printk("cdev_add is error\n"); 92 | } 93 | printk("cdev_add is ok\n"); 94 | 95 | 96 | data->gpiod = gpiod_get(&pdev->dev, "led", GPIOD_OUT_HIGH); 97 | gpiod_set_value(data->gpiod, 1); //输出高电平 98 | 99 | data->time = ktime_set(0, 1000000); 100 | 101 | hrtimer_init(&data->pwm_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 102 | data->pwm_timer.function = pwm_timer_func; 103 | 104 | hrtimer_start(&data->pwm_timer, data->time, HRTIMER_MODE_REL); 105 | return 0; 106 | } 107 | 108 | int sg90_remove(struct platform_device* dev){ 109 | return 0; 110 | } 111 | 112 | static int __init module_cdev_init(void)//驱动入口函数 113 | { 114 | i 115 | 116 | platform_driver_regisiter(&sg90_platform_driver); 117 | return 0; 118 | } 119 | 120 | static void __exit module_cdev_exit(void)//驱动出口函数 121 | { 122 | cdev_del(&cdev_test);//使用cdev_del()函数进行字符设备的删除 123 | unregister_chrdev_region(dev_num,1);//释放字符驱动设备号 124 | platform_driver_unregisiter(&sg90_platform_driver); 125 | printk("module exit \n"); 126 | 127 | } 128 | 129 | module_init(module_cdev_init);//注册入口函数 130 | module_exit(module_cdev_exit);//注册出口函数 131 | MODULE_LICENSE("GPL v2");//同意GPL开源协议 132 | MODULE_AUTHOR("topeet"); //作者信息 133 | 134 | -------------------------------------------------------------------------------- /02_cdev/test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/02_cdev/test.c -------------------------------------------------------------------------------- /03_led/app/app: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/03_led/app/app -------------------------------------------------------------------------------- /03_led/app/app.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | int fd; 11 | char buf[32] = {0}; 12 | fd = open("/dev/test", O_RDWR); //打开led驱动 13 | if (fd < 0) 14 | { 15 | perror("open error \n"); 16 | return fd; 17 | } 18 | buf[0] =atoi(argv[1]); // atoi()将字符串转为整型,这里将第一个参数转化为整型后,存放在 buf[0]中 19 | write(fd,buf,sizeof(buf)); //向/dev/test文件写入数据 20 | close(fd); //关闭文件 21 | return 0; 22 | } -------------------------------------------------------------------------------- /03_led/module/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64 2 | export CROSS_COMPILE=aarch64-linux-gnu- 3 | obj-m += file.o 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean 10 | -------------------------------------------------------------------------------- /03_led/module/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/03_led/module/Module.symvers -------------------------------------------------------------------------------- /03_led/module/file.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define GPIO_DR 0xFDD60000 10 | 11 | struct device_test{ 12 | 13 | dev_t dev_num; //设备号 14 | int major ; //主设备号 15 | int minor ; //次设备号 16 | struct cdev cdev_test; // cdev 17 | struct class *class; //类 18 | struct device *device; //设备 19 | char kbuf[32]; 20 | unsigned int *vir_gpio_dr; 21 | }; 22 | 23 | struct device_test dev1; 24 | 25 | 26 | /*打开设备函数*/ 27 | static int cdev_test_open(struct inode *inode, struct file *file) 28 | { 29 | file->private_data=&dev1;//设置私有数据 30 | printk("This is cdev_test_open\r\n"); 31 | 32 | return 0; 33 | } 34 | 35 | /*向设备写入数据函数*/ 36 | static ssize_t cdev_test_write(struct file *file, const char __user *buf, size_t size, loff_t *off) 37 | { 38 | struct device_test *test_dev=(struct device_test *)file->private_data; 39 | 40 | if (copy_from_user(test_dev->kbuf, buf, size) != 0) // copy_from_user:用户空间向内核空间传数据 41 | { 42 | printk("copy_from_user error\r\n"); 43 | return -1; 44 | } 45 | if(test_dev->kbuf[0]==1){ //如果应用层传入的数据是1,则打开灯 46 | *(test_dev->vir_gpio_dr) = 0x8000c040; //设置数据寄存器的地址 47 | printk("test_dev->kbuf [0] is %d\n",test_dev->kbuf[0]); //打印传入的数据 48 | } 49 | else if(test_dev->kbuf[0]==0) //如果应用层传入的数据是0,则关闭灯 50 | { 51 | *(test_dev->vir_gpio_dr) = 0x80004040; //设置数据寄存器的地址 52 | printk("test_dev->kbuf [0] is %d\n",test_dev->kbuf[0]); //打印传入的数据 53 | } 54 | return 0; 55 | } 56 | 57 | /**从设备读取数据*/ 58 | static ssize_t cdev_test_read(struct file *file, char __user *buf, size_t size, loff_t *off) 59 | { 60 | 61 | struct device_test *test_dev=(struct device_test *)file->private_data; 62 | 63 | if (copy_to_user(buf, test_dev->kbuf, strlen( test_dev->kbuf)) != 0) // copy_to_user:内核空间向用户空间传数据 64 | { 65 | printk("copy_to_user error\r\n"); 66 | return -1; 67 | } 68 | 69 | printk("This is cdev_test_read\r\n"); 70 | return 0; 71 | } 72 | 73 | static int cdev_test_release(struct inode *inode, struct file *file) 74 | { 75 | printk("This is cdev_test_release\r\n"); 76 | return 0; 77 | } 78 | 79 | /*设备操作函数*/ 80 | struct file_operations cdev_test_fops = { 81 | .owner = THIS_MODULE, //将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 82 | .open = cdev_test_open, //将open字段指向chrdev_open(...)函数 83 | .read = cdev_test_read, //将open字段指向chrdev_read(...)函数 84 | .write = cdev_test_write, //将open字段指向chrdev_write(...)函数 85 | .release = cdev_test_release, //将open字段指向chrdev_release(...)函数 86 | }; 87 | 88 | static int __init chr_fops_init(void) //驱动入口函数 89 | { 90 | /*注册字符设备驱动*/ 91 | int ret; 92 | /*1 创建设备号*/ 93 | ret = alloc_chrdev_region(&dev1.dev_num, 0, 1, "alloc_name"); //动态分配设备号 94 | if (ret < 0) 95 | { 96 | goto err_chrdev; 97 | } 98 | printk("alloc_chrdev_region is ok\n"); 99 | 100 | dev1.major = MAJOR(dev1.dev_num); //获取主设备号 101 | dev1.minor = MINOR(dev1.dev_num); //获取次设备号 102 | 103 | printk("major is %d \r\n", dev1.major); //打印主设备号 104 | printk("minor is %d \r\n", dev1.minor); //打印次设备号 105 | /*2 初始化cdev*/ 106 | dev1.cdev_test.owner = THIS_MODULE; 107 | cdev_init(&dev1.cdev_test, &cdev_test_fops); 108 | 109 | /*3 添加一个cdev,完成字符设备注册到内核*/ 110 | ret = cdev_add(&dev1.cdev_test, dev1.dev_num, 1); 111 | if(ret<0) 112 | { 113 | goto err_chr_add; 114 | } 115 | /*4 创建类*/ 116 | dev1. class = class_create(THIS_MODULE, "test"); 117 | if(IS_ERR(dev1.class)) 118 | { 119 | ret=PTR_ERR(dev1.class); 120 | goto err_class_create; 121 | } 122 | /*5 创建设备*/ 123 | dev1.device = device_create(dev1.class, NULL, dev1.dev_num, NULL, "test"); 124 | if(IS_ERR(dev1.device)) 125 | { 126 | ret=PTR_ERR(dev1.device); 127 | goto err_device_create; 128 | } 129 | /*本实验重点*****/ 130 | dev1.vir_gpio_dr=ioremap(GPIO_DR,4); //将物理地址转化为虚拟地址 131 | if(IS_ERR(dev1.vir_gpio_dr)) 132 | { 133 | ret=PTR_ERR(dev1.vir_gpio_dr); //PTR_ERR()来返回错误代码 134 | goto err_ioremap; 135 | } 136 | 137 | 138 | return 0; 139 | 140 | err_ioremap: 141 | iounmap(dev1.vir_gpio_dr); 142 | 143 | err_device_create: 144 | class_destroy(dev1.class); //删除类 145 | 146 | err_class_create: 147 | cdev_del(&dev1.cdev_test); //删除cdev 148 | 149 | err_chr_add: 150 | unregister_chrdev_region(dev1.dev_num, 1); //注销设备号 151 | 152 | err_chrdev: 153 | return ret; 154 | } 155 | 156 | 157 | 158 | 159 | static void __exit chr_fops_exit(void) //驱动出口函数 160 | { 161 | /*注销字符设备*/ 162 | unregister_chrdev_region(dev1.dev_num, 1); //注销设备号 163 | cdev_del(&dev1.cdev_test); //删除cdev 164 | device_destroy(dev1.class, dev1.dev_num); //删除设备 165 | class_destroy(dev1.class); //删除类 166 | } 167 | module_init(chr_fops_init); 168 | module_exit(chr_fops_exit); 169 | MODULE_LICENSE("GPL v2"); 170 | MODULE_AUTHOR("enlai"); 171 | -------------------------------------------------------------------------------- /03_led/module/file.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/03_led/module/file.ko -------------------------------------------------------------------------------- /03_led/module/file.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /03_led/module/file.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/03_led/module/file.mod.o -------------------------------------------------------------------------------- /03_led/module/file.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/03_led/module/file.o -------------------------------------------------------------------------------- /03_led/module/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/13/module/file.ko 2 | -------------------------------------------------------------------------------- /04_spinlock/app/app: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/04_spinlock/app/app -------------------------------------------------------------------------------- /04_spinlock/app/app.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | int main(int argc, char *argv[]) 8 | { 9 | int fd;//定义int类型的文件描述符 10 | char str1[10] = {0};//定义读取缓冲区str1 11 | fd = open(argv[1],O_RDWR);//调用open函数,打开输入的第一个参数文件,权限为可读可写 12 | if(fd < 0 ){ 13 | printf("file open failed \n"); 14 | return -1; 15 | } 16 | /*如果第二个参数为topeet,条件成立,调用write函数,写入topeet*/ 17 | if (strcmp(argv[2],"topeet") == 0 ){ 18 | write(fd,"topeet",10); 19 | } 20 | /*如果第二个参数为itop,条件成立,调用write函数,写入itop*/ 21 | else if (strcmp(argv[2],"itop") == 0 ){ 22 | write(fd,"itop",10); 23 | } 24 | close(fd); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /04_spinlock/module/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m := spinlock.o 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #这里是你的内核目录 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | -------------------------------------------------------------------------------- /04_spinlock/module/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/04_spinlock/module/Module.symvers -------------------------------------------------------------------------------- /04_spinlock/module/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/16/module/spinlock.ko 2 | -------------------------------------------------------------------------------- /04_spinlock/module/spinlock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | static spinlock_t spinlock_test;//定义spinlock_t类型的自旋锁变量spinlock_test 11 | static int flag = 1;//定义flag标准为,flag等于1表示设备没有被打开,等于0则证明设备已经被打开了 12 | static int open_test(struct inode *inode,struct file *file) 13 | { 14 | //printk("\nthis is open_test \n"); 15 | spin_lock(&spinlock_test);//自旋锁加锁 16 | if(flag != 1){//判断标志位flag的值是否等于1 17 | spin_unlock(&spinlock_test);//自旋锁解锁 18 | return -EBUSY; 19 | } 20 | flag = 0;//将标志位的值设置为0 21 | spin_unlock(&spinlock_test);//自旋锁解锁 22 | return 0; 23 | } 24 | 25 | static ssize_t read_test(struct file *file,char __user *ubuf,size_t len,loff_t *off) 26 | { 27 | int ret; 28 | char kbuf[10] = "topeet";//定义char类型字符串变量kbuf 29 | printk("\nthis is read_test \n"); 30 | ret = copy_to_user(ubuf,kbuf,strlen(kbuf));//使用copy_to_user接收用户空间传递的数据 31 | if (ret != 0){ 32 | printk("copy_to_user is error \n"); 33 | } 34 | printk("copy_to_user is ok \n"); 35 | return 0; 36 | } 37 | static char kbuf[10] = {0};//定义char类型字符串全局变量kbuf 38 | static ssize_t write_test(struct file *file,const char __user *ubuf,size_t len,loff_t *off) 39 | { 40 | int ret; 41 | ret = copy_from_user(kbuf,ubuf,len);//使用copy_from_user接收用户空间传递的数据 42 | if (ret != 0){ 43 | printk("copy_from_user is error\n"); 44 | } 45 | if(strcmp(kbuf,"topeet") == 0 ){//如果传递的kbuf是topeet就睡眠四秒钟 46 | ssleep(4); 47 | } 48 | else if(strcmp(kbuf,"itop") == 0){//如果传递的kbuf是itop就睡眠两秒钟 49 | ssleep(2); 50 | } 51 | printk("copy_from_user buf is %s \n",kbuf); 52 | return 0; 53 | } 54 | static int release_test(struct inode *inode,struct file *file) 55 | { 56 | printk("\nthis is release_test \n"); 57 | spin_lock(&spinlock_test);//自旋锁加锁 58 | flag = 1; 59 | spin_unlock(&spinlock_test);//自旋锁解锁 60 | return 0; 61 | } 62 | 63 | struct chrdev_test { 64 | dev_t dev_num;//定义dev_t类型变量dev_num来表示设备号 65 | int major,minor;//定义int类型的主设备号major和次设备号minor 66 | struct cdev cdev_test;//定义struct cdev 类型结构体变量cdev_test,表示要注册的字符设备 67 | struct class *class_test;//定于struct class *类型结构体变量class_test,表示要创建的类 68 | }; 69 | struct chrdev_test dev1;//创建chrdev_test类型的 70 | struct file_operations fops_test = { 71 | .owner = THIS_MODULE,//将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 72 | .open = open_test,//将open字段指向open_test(...)函数 73 | .read = read_test,//将read字段指向read_test(...)函数 74 | .write = write_test,//将write字段指向write_test(...)函数 75 | .release = release_test,//将release字段指向release_test(...)函数 76 | }; 77 | 78 | static int __init atomic_init(void) 79 | { 80 | spin_lock_init(&spinlock_test); 81 | if(alloc_chrdev_region(&dev1.dev_num,0,1,"chrdev_name") < 0 ){//自动获取设备号,设备名chrdev_name 82 | printk("alloc_chrdev_region is error \n"); 83 | } 84 | printk("alloc_chrdev_region is ok \n"); 85 | dev1.major = MAJOR(dev1.dev_num);//使用MAJOR()函数获取主设备号 86 | dev1.minor = MINOR(dev1.dev_num);//使用MINOR()函数获取次设备号 87 | printk("major is %d,minor is %d\n",dev1.major,dev1.minor); 88 | cdev_init(&dev1.cdev_test,&fops_test);//使用cdev_init()函数初始化cdev_test结构体,并链接到fops_test结构体 89 | dev1.cdev_test.owner = THIS_MODULE;//将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 90 | cdev_add(&dev1.cdev_test,dev1.dev_num,1);//使用cdev_add()函数进行字符设备的添加 91 | dev1.class_test = class_create(THIS_MODULE,"class_test");//使用class_create进行类的创建,类名称为class_test 92 | device_create(dev1.class_test,0,dev1.dev_num,0,"device_test");//使用device_create进行设备的创建,设备名称为device_test 93 | return 0; 94 | } 95 | 96 | static void __exit atomic_exit(void) 97 | { 98 | device_destroy(dev1.class_test,dev1.dev_num);//删除创建的设备 99 | class_destroy(dev1.class_test);//删除创建的类 100 | cdev_del(&dev1.cdev_test);//删除添加的字符设备cdev_test 101 | unregister_chrdev_region(dev1.dev_num,1);//释放字符设备所申请的设备号 102 | printk("module exit \n"); 103 | } 104 | module_init(atomic_init); 105 | module_exit(atomic_exit) 106 | MODULE_LICENSE("GPL v2"); 107 | MODULE_AUTHOR("enlai"); 108 | -------------------------------------------------------------------------------- /04_spinlock/module/spinlock.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/04_spinlock/module/spinlock.ko -------------------------------------------------------------------------------- /04_spinlock/module/spinlock.mod: -------------------------------------------------------------------------------- 1 | /home/topeet/work/16/module/spinlock.o 2 | 3 | -------------------------------------------------------------------------------- /04_spinlock/module/spinlock.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /04_spinlock/module/spinlock.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/04_spinlock/module/spinlock.mod.o -------------------------------------------------------------------------------- /04_spinlock/module/spinlock.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/04_spinlock/module/spinlock.o -------------------------------------------------------------------------------- /05_wait_queue/app/read: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/05_wait_queue/app/read -------------------------------------------------------------------------------- /05_wait_queue/app/read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | int fd; //定义int类型的文件描述符 11 | char buf1[32] = {0}; //定义读取缓冲区buf 12 | char buf2[32] = {0}; //定义读取缓冲区buf 13 | fd = open("/dev/test",O_RDWR| O_NONBLOCK); //打开led驱动 14 | if (fd < 0) 15 | { 16 | perror("open error \n"); 17 | return fd; 18 | } 19 | printf("read before \n"); 20 | while (1) 21 | { 22 | 23 | read(fd,buf1,sizeof(buf1)); //从/dev/test文件读取数据 24 | printf("buf is %s \n",buf1); //打印读取的数据 25 | sleep(1); 26 | 27 | } 28 | printf("read after\n"); 29 | 30 | close(fd); //关闭文件 31 | return 0; 32 | } -------------------------------------------------------------------------------- /05_wait_queue/app/write: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/05_wait_queue/app/write -------------------------------------------------------------------------------- /05_wait_queue/app/write.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | int fd; 12 | char buf1[32] = {0}; 13 | char buf2[32] = "nihao"; 14 | fd = open("/dev/test", O_RDWR|O_NONBLOCK); //打开led驱动 15 | if (fd < 0) 16 | { 17 | perror("open error \n"); 18 | return fd; 19 | } 20 | printf("write before \n"); 21 | write(fd,buf2,sizeof(buf2)); //向/dev/test文件写入数据 22 | printf("write after\n"); 23 | close(fd); //关闭文件 24 | return 0; 25 | } -------------------------------------------------------------------------------- /05_wait_queue/module/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64 2 | export CROSS_COMPILE=aarch64-linux-gnu- 3 | obj-m += wq.o 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean 10 | -------------------------------------------------------------------------------- /05_wait_queue/module/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/05_wait_queue/module/Module.symvers -------------------------------------------------------------------------------- /05_wait_queue/module/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/21/module/wq.ko 2 | -------------------------------------------------------------------------------- /05_wait_queue/module/wq.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | struct device_test{ 12 | 13 | dev_t dev_num; //设备号 14 | int major ; //主设备号 15 | int minor ; //次设备号 16 | struct cdev cdev_test; // cdev 17 | struct class *class; //类 18 | struct device *device; //设备 19 | char kbuf[32]; 20 | int flag; //标志位 21 | }; 22 | 23 | 24 | struct device_test dev1; 25 | 26 | DECLARE_WAIT_QUEUE_HEAD(read_wq); //定义并初始化等待队列头 27 | 28 | /*打开设备函数*/ 29 | static int cdev_test_open(struct inode *inode, struct file *file) 30 | { 31 | file->private_data=&dev1;//设置私有数据 32 | 33 | return 0; 34 | } 35 | 36 | /*向设备写入数据函数*/ 37 | static ssize_t cdev_test_write(struct file *file, const char __user *buf, size_t size, loff_t *off) 38 | { 39 | struct device_test *test_dev=(struct device_test *)file->private_data; 40 | 41 | if (copy_from_user(test_dev->kbuf, buf, size) != 0) // copy_from_user:用户空间向内核空间传数据 42 | { 43 | printk("copy_from_user error\r\n"); 44 | return -1; 45 | } 46 | test_dev->flag=1; //将条件置1,并使用wake_up_interruptible唤醒等待队列中的休眠进程 47 | wake_up_interruptible(&read_wq); 48 | 49 | return 0; 50 | } 51 | 52 | /**从设备读取数据*/ 53 | static ssize_t cdev_test_read(struct file *file, char __user *buf, size_t size, loff_t *off) 54 | { 55 | 56 | struct device_test *test_dev=(struct device_test *)file->private_data; 57 | if(file->f_flags & O_NONBLOCK ){ 58 | if (test_dev->flag !=1) 59 | return -EAGAIN; 60 | } 61 | wait_event_interruptible(read_wq,test_dev->flag); //可中断的阻塞等待,使进程进入休眠态 62 | 63 | if (copy_to_user(buf, test_dev->kbuf, strlen( test_dev->kbuf)) != 0) // copy_to_user:内核空间向用户空间传数据 64 | { 65 | printk("copy_to_user error\r\n"); 66 | return -1; 67 | } 68 | 69 | 70 | return 0; 71 | } 72 | 73 | static int cdev_test_release(struct inode *inode, struct file *file) 74 | { 75 | 76 | return 0; 77 | } 78 | 79 | /*设备操作函数*/ 80 | struct file_operations cdev_test_fops = { 81 | .owner = THIS_MODULE, //将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 82 | .open = cdev_test_open, //将open字段指向chrdev_open(...)函数 83 | .read = cdev_test_read, //将open字段指向chrdev_read(...)函数 84 | .write = cdev_test_write, //将open字段指向chrdev_write(...)函数 85 | .release = cdev_test_release, //将open字段指向chrdev_release(...)函数 86 | }; 87 | 88 | static int __init chr_fops_init(void) //驱动入口函数 89 | { 90 | /*注册字符设备驱动*/ 91 | int ret; 92 | /*1 创建设备号*/ 93 | ret = alloc_chrdev_region(&dev1.dev_num, 0, 1, "alloc_name"); //动态分配设备号 94 | if (ret < 0) 95 | { 96 | goto err_chrdev; 97 | } 98 | printk("alloc_chrdev_region is ok\n"); 99 | 100 | dev1.major = MAJOR(dev1.dev_num); //获取主设备号 101 | dev1.minor = MINOR(dev1.dev_num); //获取次设备号 102 | 103 | printk("major is %d \r\n", dev1.major); //打印主设备号 104 | printk("minor is %d \r\n", dev1.minor); //打印次设备号 105 | /*2 初始化cdev*/ 106 | dev1.cdev_test.owner = THIS_MODULE; 107 | cdev_init(&dev1.cdev_test, &cdev_test_fops); 108 | 109 | /*3 添加一个cdev,完成字符设备注册到内核*/ 110 | ret = cdev_add(&dev1.cdev_test, dev1.dev_num, 1); 111 | if(ret<0) 112 | { 113 | goto err_chr_add; 114 | } 115 | /*4 创建类*/ 116 | dev1. class = class_create(THIS_MODULE, "test"); 117 | if(IS_ERR(dev1.class)) 118 | { 119 | ret=PTR_ERR(dev1.class); 120 | goto err_class_create; 121 | } 122 | /*5 创建设备*/ 123 | dev1.device = device_create(dev1.class, NULL, dev1.dev_num, NULL, "test"); 124 | if(IS_ERR(dev1.device)) 125 | { 126 | ret=PTR_ERR(dev1.device); 127 | goto err_device_create; 128 | } 129 | 130 | return 0; 131 | 132 | err_device_create: 133 | class_destroy(dev1.class); //删除类 134 | 135 | err_class_create: 136 | cdev_del(&dev1.cdev_test); //删除cdev 137 | 138 | err_chr_add: 139 | unregister_chrdev_region(dev1.dev_num, 1); //注销设备号 140 | 141 | err_chrdev: 142 | return ret; 143 | } 144 | 145 | 146 | 147 | 148 | static void __exit chr_fops_exit(void) //驱动出口函数 149 | { 150 | /*注销字符设备*/ 151 | unregister_chrdev_region(dev1.dev_num, 1); //注销设备号 152 | cdev_del(&dev1.cdev_test); //删除cdev 153 | device_destroy(dev1.class, dev1.dev_num); //删除设备 154 | class_destroy(dev1.class); //删除类 155 | } 156 | module_init(chr_fops_init); 157 | module_exit(chr_fops_exit); 158 | MODULE_LICENSE("GPL v2"); 159 | MODULE_AUTHOR("enlai"); 160 | -------------------------------------------------------------------------------- /05_wait_queue/module/wq.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/05_wait_queue/module/wq.ko -------------------------------------------------------------------------------- /05_wait_queue/module/wq.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /05_wait_queue/module/wq.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/05_wait_queue/module/wq.mod.o -------------------------------------------------------------------------------- /05_wait_queue/module/wq.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/05_wait_queue/module/wq.o -------------------------------------------------------------------------------- /06_ioctl/app/ioctl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/06_ioctl/app/ioctl -------------------------------------------------------------------------------- /06_ioctl/app/ioctl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define CMD_TEST0 _IOW('L',0,int) 9 | struct args{//定义要传递的结构体 10 | int a; 11 | int b; 12 | int c; 13 | }; 14 | int main(int argc,char *argv[]){ 15 | int fd;//定义int类型文件描述符 16 | struct args test;//定义args类型的结构体变量test 17 | test.a = 1; 18 | test.b = 2; 19 | test.c = 3; 20 | fd = open("/dev/test",O_RDWR,0777);//打开/dev/test设备 21 | if(fd < 0){ 22 | printf("file open error \n"); 23 | } 24 | ioctl(fd,CMD_TEST0,&test);//使用ioctl函数传递结构体变量test地址 25 | close(fd); 26 | } 27 | -------------------------------------------------------------------------------- /06_ioctl/module/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m := ioctl.o 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #这里是你的内核目录 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | -------------------------------------------------------------------------------- /06_ioctl/module/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/06_ioctl/module/Module.symvers -------------------------------------------------------------------------------- /06_ioctl/module/ioctl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define CMD_TEST0 _IOW('L',0,int) 9 | struct args{ 10 | int a; 11 | int b; 12 | int c; 13 | }; 14 | struct device_test{ 15 | 16 | dev_t dev_num; //设备号 17 | int major ; //主设备号 18 | int minor ; //次设备号 19 | struct cdev cdev_test; // cdev 20 | struct class *class; //类 21 | struct device *device; //设备 22 | char kbuf[32]; 23 | }; 24 | static struct device_test dev1; 25 | 26 | static long cdev_test_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 27 | { 28 | struct args test; 29 | switch(cmd){ 30 | case CMD_TEST0: 31 | if(copy_from_user(&test,(int *)arg,sizeof(test)) != 0){ 32 | printk("copy_from_user error\n"); 33 | } 34 | printk("a = %d\n",test.a); 35 | printk("b = %d\n",test.b); 36 | printk("c = %d\n",test.c); 37 | break; 38 | default: 39 | break; 40 | } 41 | return 0; 42 | } 43 | /*设备操作函数*/ 44 | struct file_operations cdev_test_fops = { 45 | .owner = THIS_MODULE, //将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 46 | .unlocked_ioctl = cdev_test_ioctl, 47 | }; 48 | static int __init timer_dev_init(void) //驱动入口函数 49 | { 50 | /*注册字符设备驱动*/ 51 | int ret; 52 | /*1 创建设备号*/ 53 | ret = alloc_chrdev_region(&dev1.dev_num, 0, 1, "alloc_name"); //动态分配设备号 54 | if (ret < 0) 55 | { 56 | goto err_chrdev; 57 | } 58 | printk("alloc_chrdev_region is ok\n"); 59 | 60 | dev1.major = MAJOR(dev1.dev_num); //获取主设备号 61 | dev1.minor = MINOR(dev1.dev_num); //获取次设备号 62 | 63 | printk("major is %d \r\n", dev1.major); //打印主设备号 64 | printk("minor is %d \r\n", dev1.minor); //打印次设备号 65 | /*2 初始化cdev*/ 66 | dev1.cdev_test.owner = THIS_MODULE; 67 | cdev_init(&dev1.cdev_test, &cdev_test_fops); 68 | 69 | /*3 添加一个cdev,完成字符设备注册到内核*/ 70 | ret = cdev_add(&dev1.cdev_test, dev1.dev_num, 1); 71 | if(ret<0) 72 | { 73 | goto err_chr_add; 74 | } 75 | /*4 创建类*/ 76 | dev1. class = class_create(THIS_MODULE, "test"); 77 | if(IS_ERR(dev1.class)) 78 | { 79 | ret=PTR_ERR(dev1.class); 80 | goto err_class_create; 81 | } 82 | /*5 创建设备*/ 83 | dev1.device = device_create(dev1.class, NULL, dev1.dev_num, NULL, "test"); 84 | if(IS_ERR(dev1.device)) 85 | { 86 | ret=PTR_ERR(dev1.device); 87 | goto err_device_create; 88 | } 89 | 90 | return 0; 91 | 92 | err_device_create: 93 | class_destroy(dev1.class); //删除类 94 | 95 | err_class_create: 96 | cdev_del(&dev1.cdev_test); //删除cdev 97 | 98 | err_chr_add: 99 | unregister_chrdev_region(dev1.dev_num, 1); //注销设备号 100 | 101 | err_chrdev: 102 | return ret; 103 | } 104 | 105 | static void __exit timer_dev_exit(void) //驱动出口函数 106 | { 107 | /*注销字符设备*/ 108 | unregister_chrdev_region(dev1.dev_num, 1); //注销设备号 109 | cdev_del(&dev1.cdev_test); //删除cdev 110 | device_destroy(dev1.class, dev1.dev_num); //删除设备 111 | class_destroy(dev1.class); //删除类 112 | } 113 | module_init(timer_dev_init); 114 | module_exit(timer_dev_exit); 115 | MODULE_LICENSE("GPL v2"); 116 | MODULE_AUTHOR("enlai"); 117 | -------------------------------------------------------------------------------- /06_ioctl/module/ioctl.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/06_ioctl/module/ioctl.ko -------------------------------------------------------------------------------- /06_ioctl/module/ioctl.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /06_ioctl/module/ioctl.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/06_ioctl/module/ioctl.mod.o -------------------------------------------------------------------------------- /06_ioctl/module/ioctl.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/06_ioctl/module/ioctl.o -------------------------------------------------------------------------------- /06_ioctl/module/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/28/module/ioctl.ko 2 | -------------------------------------------------------------------------------- /07_interrupt/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m += interrupt.o #helloworld.c对应.o文件的名称。名称要保持一致。 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #内核源码所在虚拟机ubuntu的实际路径 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | 11 | -------------------------------------------------------------------------------- /07_interrupt/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/07_interrupt/Module.symvers -------------------------------------------------------------------------------- /07_interrupt/interrupt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define GPIO_PIN 101 7 | 8 | // 中断处理函数 9 | static irqreturn_t gpio_irq_handler(int irq, void *dev_id) 10 | { 11 | printk(KERN_INFO "Interrupt occurred on GPIO %d\n", GPIO_PIN); 12 | printk(KERN_INFO "This is irq_handler\n"); 13 | return IRQ_HANDLED; 14 | } 15 | 16 | static int __init interrupt_init(void) 17 | { 18 | int irq_num; 19 | 20 | printk(KERN_INFO "Initializing GPIO Interrupt Driver\n"); 21 | 22 | // 将GPIO引脚映射到中断号 23 | irq_num = gpio_to_irq(GPIO_PIN); 24 | printk(KERN_INFO "GPIO %d mapped to IRQ %d\n", GPIO_PIN, irq_num); 25 | 26 | // 请求中断 27 | if (request_irq(irq_num, gpio_irq_handler, IRQF_TRIGGER_RISING, "irq_test", NULL) != 0) { 28 | printk(KERN_ERR "Failed to request IRQ %d\n", irq_num); 29 | 30 | // 请求中断失败,释放GPIO引脚 31 | gpio_free(GPIO_PIN); 32 | return -ENODEV; 33 | } 34 | return 0; 35 | } 36 | 37 | static void __exit interrupt_exit(void) 38 | { 39 | int irq_num = gpio_to_irq(GPIO_PIN); 40 | 41 | // 释放中断 42 | free_irq(irq_num, NULL); 43 | printk(KERN_INFO "GPIO Interrupt Driver exited successfully\n"); 44 | } 45 | 46 | module_init(interrupt_init); 47 | module_exit(interrupt_exit); 48 | 49 | MODULE_LICENSE("GPL"); 50 | MODULE_AUTHOR("enlai"); 51 | -------------------------------------------------------------------------------- /07_interrupt/interrupt.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/07_interrupt/interrupt.ko -------------------------------------------------------------------------------- /07_interrupt/interrupt.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /07_interrupt/interrupt.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/07_interrupt/interrupt.mod.o -------------------------------------------------------------------------------- /07_interrupt/interrupt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/07_interrupt/interrupt.o -------------------------------------------------------------------------------- /07_interrupt/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/30_interrupt/interrupt.ko 2 | -------------------------------------------------------------------------------- /08_tasklet/module/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m := interrupt.o 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #这里是你的内核目录 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | -------------------------------------------------------------------------------- /08_tasklet/module/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/08_tasklet/module/Module.symvers -------------------------------------------------------------------------------- /08_tasklet/module/interrupt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | // #include 6 | 7 | int irq; 8 | struct tasklet_struct mytasklet; 9 | 10 | // 定义tasklet处理函数 11 | void mytasklet_func(unsigned long data) 12 | { 13 | printk("data is %ld\n", data); 14 | // msleep(3000); 15 | } 16 | 17 | // 中断处理函数 18 | irqreturn_t test_interrupt(int irq, void *args) 19 | { 20 | printk("This is test_interrupt\n"); 21 | tasklet_schedule(&mytasklet); // 调度tasklet执行 22 | return IRQ_RETVAL(IRQ_HANDLED); 23 | } 24 | // 模块初始化函数 25 | static int interrupt_irq_init(void) 26 | { 27 | int ret; 28 | irq = gpio_to_irq(101); // 将GPIO转换为中断号 29 | printk("irq is %d\n", irq); 30 | 31 | // 请求中断 32 | ret = request_irq(irq, test_interrupt, IRQF_TRIGGER_RISING, "test", NULL); 33 | if (ret < 0) 34 | { 35 | printk("request_irq is error\n"); 36 | return -1; 37 | } 38 | // 初始化tasklet 39 | tasklet_init(&mytasklet, mytasklet_func, 1); 40 | return 0; 41 | } 42 | // 模块退出函数 43 | static void interrupt_irq_exit(void) 44 | { 45 | 46 | free_irq(irq, NULL); 47 | tasklet_enable(&mytasklet); // 使能tasklet(可选) 48 | tasklet_kill(&mytasklet); // 销毁tasklet 49 | printk("bye bye\n"); 50 | } 51 | 52 | module_init(interrupt_irq_init); // 指定模块的初始化函数 53 | module_exit(interrupt_irq_exit); // 指定模块的退出函数 54 | 55 | MODULE_LICENSE("GPL"); // 模块使用的许可证 56 | MODULE_AUTHOR("enlai"); // 模块的作者 57 | -------------------------------------------------------------------------------- /08_tasklet/module/interrupt.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/08_tasklet/module/interrupt.ko -------------------------------------------------------------------------------- /08_tasklet/module/interrupt.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /08_tasklet/module/interrupt.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/08_tasklet/module/interrupt.mod.o -------------------------------------------------------------------------------- /08_tasklet/module/interrupt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/08_tasklet/module/interrupt.o -------------------------------------------------------------------------------- /08_tasklet/module/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/32_tasklet/module/interrupt.ko 2 | -------------------------------------------------------------------------------- /09_softirq/module/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m := interrupt.o 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #这里是你的内核目录 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | -------------------------------------------------------------------------------- /09_softirq/module/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/09_softirq/module/Module.symvers -------------------------------------------------------------------------------- /09_softirq/module/interrupt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | // #include 6 | 7 | int irq; 8 | 9 | // 软中断处理程序 10 | void testsoft_func(struct softirq_action *softirq_action) 11 | { 12 | printk("This is testsoft_func\n"); 13 | } 14 | 15 | irqreturn_t test_interrupt(int irq, void *args) 16 | { 17 | printk("This is test_interrupt\n"); 18 | raise_softirq(TEST_SOFTIRQ); // 触发软中断 19 | return IRQ_RETVAL(IRQ_HANDLED); 20 | } 21 | 22 | static int interrupt_irq_init(void) 23 | { 24 | int ret; 25 | irq = gpio_to_irq(101); // 将GPIO映射为中断号 26 | printk("irq is %d\n", irq); 27 | // 请求中断 28 | ret = request_irq(irq, test_interrupt, IRQF_TRIGGER_RISING, "test", NULL); 29 | if (ret < 0) 30 | { 31 | printk("request_irq is error\n"); 32 | return -1; 33 | } 34 | // 注册软中断处理函数 35 | open_softirq(TEST_SOFTIRQ, testsoft_func); 36 | return 0; 37 | } 38 | 39 | static void interrupt_irq_exit(void) 40 | { 41 | free_irq(irq, NULL); // 释放中断 42 | printk("bye bye\n"); 43 | } 44 | 45 | module_init(interrupt_irq_init); 46 | module_exit(interrupt_irq_exit); 47 | 48 | MODULE_LICENSE("GPL"); 49 | MODULE_AUTHOR("enlai"); -------------------------------------------------------------------------------- /09_softirq/module/interrupt.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/09_softirq/module/interrupt.ko -------------------------------------------------------------------------------- /09_softirq/module/interrupt.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /09_softirq/module/interrupt.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/09_softirq/module/interrupt.mod.o -------------------------------------------------------------------------------- /09_softirq/module/interrupt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/09_softirq/module/interrupt.o -------------------------------------------------------------------------------- /09_softirq/module/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/33_softirq/module/interrupt.ko 2 | -------------------------------------------------------------------------------- /10_platform/platform_device/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m += platform_device.o #platform_device.c对应.o文件的名称。名称要保持一致。 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #内核源码所在虚拟机ubuntu的实际路径 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | 11 | -------------------------------------------------------------------------------- /10_platform/platform_device/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/10_platform/platform_device/Module.symvers -------------------------------------------------------------------------------- /10_platform/platform_device/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/40_platform_device/platform_device.ko 2 | -------------------------------------------------------------------------------- /10_platform/platform_device/platform_device.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MEM_START_ADDR 0xFDD60000 6 | #define MEM_END_ADDR 0xFDD60004 7 | #define IRQ_NUMBER 101 8 | 9 | static struct resource my_resources[] = { 10 | { 11 | .start = MEM_START_ADDR, // 内存资源起始地址 12 | .end = MEM_END_ADDR, // 内存资源结束地址 13 | .flags = IORESOURCE_MEM, // 标记为内存资源 14 | }, 15 | { 16 | .start = IRQ_NUMBER, // 中断资源号 17 | .end = IRQ_NUMBER, // 中断资源号 18 | .flags = IORESOURCE_IRQ, // 标记为中断资源 19 | }, 20 | }; 21 | 22 | static void my_platform_device_release(struct device *dev) 23 | { 24 | // 释放资源的回调函数 25 | } 26 | 27 | static struct platform_device my_platform_device = { 28 | .name = "my_platform_device", // 设备名称 29 | .id = -1, // 设备ID 30 | .num_resources = ARRAY_SIZE(my_resources), // 资源数量 31 | .resource = my_resources, // 资源数组 32 | .dev.release = my_platform_device_release, // 释放资源的回调函数 33 | }; 34 | 35 | static int __init my_platform_device_init(void) 36 | { 37 | int ret; 38 | 39 | ret = platform_device_register(&my_platform_device); // 注册平台设备 40 | if (ret) { 41 | printk(KERN_ERR "Failed to register platform device\n"); 42 | return ret; 43 | } 44 | 45 | printk(KERN_INFO "Platform device registered\n"); 46 | return 0; 47 | } 48 | 49 | static void __exit my_platform_device_exit(void) 50 | { 51 | platform_device_unregister(&my_platform_device); // 注销平台设备 52 | printk(KERN_INFO "Platform device unregistered\n"); 53 | } 54 | 55 | module_init(my_platform_device_init); 56 | module_exit(my_platform_device_exit); 57 | 58 | MODULE_LICENSE("GPL"); 59 | MODULE_AUTHOR("topeet"); 60 | 61 | -------------------------------------------------------------------------------- /10_platform/platform_device/platform_device.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/10_platform/platform_device/platform_device.ko -------------------------------------------------------------------------------- /10_platform/platform_device/platform_device.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /10_platform/platform_device/platform_device.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/10_platform/platform_device/platform_device.mod.o -------------------------------------------------------------------------------- /10_platform/platform_device/platform_device.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/10_platform/platform_device/platform_device.o -------------------------------------------------------------------------------- /10_platform/platform_driver/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m += platform_driver.o #platform_device.c对应.o文件的名称。名称要保持一致。 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #内核源码所在虚拟机ubuntu的实际路径 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | 11 | -------------------------------------------------------------------------------- /10_platform/platform_driver/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/10_platform/platform_driver/Module.symvers -------------------------------------------------------------------------------- /10_platform/platform_driver/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/41_platform_driver/platform_driver.ko 2 | -------------------------------------------------------------------------------- /10_platform/platform_driver/platform_driver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 平台设备的探测函数 5 | static int my_platform_probe(struct platform_device *pdev) 6 | { 7 | printk(KERN_INFO "my_platform_probe: Probing platform device\n"); 8 | 9 | // 添加设备特定的操作 10 | // ... 11 | 12 | return 0; 13 | } 14 | 15 | // 平台设备的移除函数 16 | static int my_platform_remove(struct platform_device *pdev) 17 | { 18 | printk(KERN_INFO "my_platform_remove: Removing platform device\n"); 19 | 20 | // 清理设备特定的操作 21 | // ... 22 | 23 | return 0; 24 | } 25 | 26 | // 定义平台驱动结构体 27 | static struct platform_driver my_platform_driver = { 28 | .probe = my_platform_probe, 29 | .remove = my_platform_remove, 30 | .driver = { 31 | .name = "my_platform_device", 32 | .owner = THIS_MODULE, 33 | }, 34 | }; 35 | 36 | // 模块初始化函数 37 | static int __init my_platform_driver_init(void) 38 | { 39 | int ret; 40 | 41 | // 注册平台驱动 42 | ret = platform_driver_register(&my_platform_driver); 43 | if (ret) { 44 | printk(KERN_ERR "Failed to register platform driver\n"); 45 | return ret; 46 | } 47 | 48 | printk(KERN_INFO "my_platform_driver: Platform driver initialized\n"); 49 | 50 | return 0; 51 | } 52 | 53 | // 模块退出函数 54 | static void __exit my_platform_driver_exit(void) 55 | { 56 | // 注销平台驱动 57 | platform_driver_unregister(&my_platform_driver); 58 | 59 | printk(KERN_INFO "my_platform_driver: Platform driver exited\n"); 60 | } 61 | 62 | module_init(my_platform_driver_init); 63 | module_exit(my_platform_driver_exit); 64 | 65 | MODULE_LICENSE("GPL"); 66 | MODULE_AUTHOR("enlai"); 67 | -------------------------------------------------------------------------------- /10_platform/platform_driver/platform_driver.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/10_platform/platform_driver/platform_driver.ko -------------------------------------------------------------------------------- /10_platform/platform_driver/platform_driver.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /10_platform/platform_driver/platform_driver.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/10_platform/platform_driver/platform_driver.mod.o -------------------------------------------------------------------------------- /10_platform/platform_driver/platform_driver.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/10_platform/platform_driver/platform_driver.o -------------------------------------------------------------------------------- /11_probe/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m += probe.o #platform_device.c对应.o文件的名称。名称要保持一致。 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #内核源码所在虚拟机ubuntu的实际路径 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | 11 | -------------------------------------------------------------------------------- /11_probe/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/11_probe/Module.symvers -------------------------------------------------------------------------------- /11_probe/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/42_probe/probe.ko 2 | -------------------------------------------------------------------------------- /11_probe/probe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static int my_platform_driver_probe(struct platform_device *pdev) 6 | { 7 | struct resource *res_mem, *res_irq; 8 | 9 | // 方法1:直接访问 platform_device 结构体的资源数组 10 | if (pdev->num_resources >= 2) { 11 | struct resource *res_mem = &pdev->resource[0]; 12 | struct resource *res_irq = &pdev->resource[1]; 13 | 14 | // 使用获取到的硬件资源进行处理 15 | printk("Method 1: Memory Resource: start = 0x%llx, end = 0x%llx\n", 16 | res_mem->start, res_mem->end); 17 | printk("Method 1: IRQ Resource: number = %lld\n", res_irq->start); 18 | } 19 | 20 | // 方法2:使用 platform_get_resource() 获取硬件资源 21 | res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 22 | if (!res_mem) { 23 | dev_err(&pdev->dev, "Failed to get memory resource\n"); 24 | return -ENODEV; 25 | } 26 | 27 | res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 28 | if (!res_irq) { 29 | dev_err(&pdev->dev, "Failed to get IRQ resource\n"); 30 | return -ENODEV; 31 | } 32 | 33 | // 使用获取到的硬件资源进行处理 34 | printk("Method 2: Memory Resource: start = 0x%llx, end = 0x%llx\n", 35 | res_mem->start, res_mem->end); 36 | printk("Method 2: IRQ Resource: number = %lld\n", res_irq->start); 37 | 38 | return 0; 39 | } 40 | 41 | static int my_platform_driver_remove(struct platform_device *pdev) 42 | { 43 | // 设备移除操作 44 | return 0; 45 | } 46 | 47 | static struct platform_driver my_platform_driver = { 48 | .driver = { 49 | .name = "my_platform_device", // 与 platform_device.c 中的设备名称匹配 50 | .owner = THIS_MODULE, 51 | }, 52 | .probe = my_platform_driver_probe, 53 | .remove = my_platform_driver_remove, 54 | }; 55 | 56 | static int __init my_platform_driver_init(void) 57 | { 58 | int ret; 59 | 60 | ret = platform_driver_register(&my_platform_driver); // 注册平台驱动 61 | if (ret) { 62 | printk("Failed to register platform driver\n"); 63 | return ret; 64 | } 65 | 66 | printk("Platform driver registered\n"); 67 | return 0; 68 | } 69 | 70 | static void __exit my_platform_driver_exit(void) 71 | { 72 | platform_driver_unregister(&my_platform_driver); // 注销平台驱动 73 | printk("Platform driver unregistered\n"); 74 | } 75 | 76 | module_init(my_platform_driver_init); 77 | module_exit(my_platform_driver_exit); 78 | 79 | MODULE_LICENSE("GPL"); 80 | MODULE_AUTHOR("enlai"); 81 | -------------------------------------------------------------------------------- /11_probe/probe.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/11_probe/probe.ko -------------------------------------------------------------------------------- /11_probe/probe.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /11_probe/probe.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/11_probe/probe.mod.o -------------------------------------------------------------------------------- /11_probe/probe.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/11_probe/probe.o -------------------------------------------------------------------------------- /12_platform_led/app/app: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/12_platform_led/app/app -------------------------------------------------------------------------------- /12_platform_led/app/app.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | int fd; 11 | char buf[32] = {0}; 12 | fd = open("/dev/test", O_RDWR); //打开led驱动 13 | if (fd < 0) 14 | { 15 | perror("open error \n"); 16 | return fd; 17 | } 18 | buf[0] =atoi(argv[1]); // atoi()将字符串转为整型,这里将第一个参数转化为整型后,存放在 buf[0]中 19 | write(fd,buf,sizeof(buf)); //向/dev/test文件写入数据 20 | close(fd); //关闭文件 21 | return 0; 22 | } -------------------------------------------------------------------------------- /12_platform_led/module/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m += platform_led.o #platform_device.c对应.o文件的名称。名称要保持一致。 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #内核源码所在虚拟机ubuntu的实际路径 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | 11 | -------------------------------------------------------------------------------- /12_platform_led/module/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/12_platform_led/module/Module.symvers -------------------------------------------------------------------------------- /12_platform_led/module/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/43_platform_led/module/platform_led.ko 2 | -------------------------------------------------------------------------------- /12_platform_led/module/platform_led.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | struct device_test{ 11 | 12 | dev_t dev_num; //设备号 13 | int major ; //主设备号 14 | int minor ; //次设备号 15 | struct cdev cdev_test; // cdev 16 | struct class *class; //类 17 | struct device *device; //设备 18 | char kbuf[32]; 19 | unsigned int *vir_gpio_dr; 20 | }; 21 | 22 | struct device_test dev1; 23 | 24 | 25 | /*打开设备函数*/ 26 | static int cdev_test_open(struct inode *inode, struct file *file) 27 | { 28 | file->private_data=&dev1;//设置私有数据 29 | printk("This is cdev_test_open\r\n"); 30 | 31 | return 0; 32 | } 33 | 34 | /*向设备写入数据函数*/ 35 | static ssize_t cdev_test_write(struct file *file, const char __user *buf, size_t size, loff_t *off) 36 | { 37 | struct device_test *test_dev=(struct device_test *)file->private_data; 38 | 39 | if (copy_from_user(test_dev->kbuf, buf, size) != 0) // copy_from_user:用户空间向内核空间传数据 40 | { 41 | printk("copy_from_user error\r\n"); 42 | return -1; 43 | } 44 | if(test_dev->kbuf[0]==1){ //如果应用层传入的数据是1,则打开灯 45 | *(test_dev->vir_gpio_dr) = 0x8000c040; //设置数据寄存器的地址 46 | printk("test_dev->kbuf [0] is %d\n",test_dev->kbuf[0]); //打印传入的数据 47 | } 48 | else if(test_dev->kbuf[0]==0) //如果应用层传入的数据是0,则关闭灯 49 | { 50 | *(test_dev->vir_gpio_dr) = 0x80004040; //设置数据寄存器的地址 51 | printk("test_dev->kbuf [0] is %d\n",test_dev->kbuf[0]); //打印传入的数据 52 | } 53 | return 0; 54 | } 55 | 56 | /**从设备读取数据*/ 57 | static ssize_t cdev_test_read(struct file *file, char __user *buf, size_t size, loff_t *off) 58 | { 59 | 60 | struct device_test *test_dev=(struct device_test *)file->private_data; 61 | 62 | if (copy_to_user(buf, test_dev->kbuf, strlen( test_dev->kbuf)) != 0) // copy_to_user:内核空间向用户空间传数据 63 | { 64 | printk("copy_to_user error\r\n"); 65 | return -1; 66 | } 67 | 68 | printk("This is cdev_test_read\r\n"); 69 | return 0; 70 | } 71 | 72 | static int cdev_test_release(struct inode *inode, struct file *file) 73 | { 74 | printk("This is cdev_test_release\r\n"); 75 | return 0; 76 | } 77 | 78 | /*设备操作函数*/ 79 | struct file_operations cdev_test_fops = { 80 | .owner = THIS_MODULE, //将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 81 | .open = cdev_test_open, //将open字段指向chrdev_open(...)函数 82 | .read = cdev_test_read, //将open字段指向chrdev_read(...)函数 83 | .write = cdev_test_write, //将open字段指向chrdev_write(...)函数 84 | .release = cdev_test_release, //将open字段指向chrdev_release(...)函数 85 | }; 86 | 87 | static int my_platform_driver_probe(struct platform_device *pdev) 88 | { 89 | struct resource *res_mem; 90 | int ret; 91 | res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 92 | if (!res_mem) { 93 | dev_err(&pdev->dev, "Failed to get memory resource\n"); 94 | return -ENODEV; 95 | } 96 | 97 | /*注册字符设备驱动*/ 98 | /*1 创建设备号*/ 99 | ret = alloc_chrdev_region(&dev1.dev_num, 0, 1, "alloc_name"); //动态分配设备号 100 | if (ret < 0) 101 | { 102 | goto err_chrdev; 103 | } 104 | printk("alloc_chrdev_region is ok\n"); 105 | 106 | dev1.major = MAJOR(dev1.dev_num); //获取主设备号 107 | dev1.minor = MINOR(dev1.dev_num); //获取次设备号 108 | 109 | printk("major is %d \r\n", dev1.major); //打印主设备号 110 | printk("minor is %d \r\n", dev1.minor); //打印次设备号 111 | /*2 初始化cdev*/ 112 | dev1.cdev_test.owner = THIS_MODULE; 113 | cdev_init(&dev1.cdev_test, &cdev_test_fops); 114 | 115 | /*3 添加一个cdev,完成字符设备注册到内核*/ 116 | ret = cdev_add(&dev1.cdev_test, dev1.dev_num, 1); 117 | if(ret<0) 118 | { 119 | goto err_chr_add; 120 | } 121 | /*4 创建类*/ 122 | dev1. class = class_create(THIS_MODULE, "test"); 123 | if(IS_ERR(dev1.class)) 124 | { 125 | ret=PTR_ERR(dev1.class); 126 | goto err_class_create; 127 | } 128 | /*5 创建设备*/ 129 | dev1.device = device_create(dev1.class, NULL, dev1.dev_num, NULL, "test"); 130 | if(IS_ERR(dev1.device)) 131 | { 132 | ret=PTR_ERR(dev1.device); 133 | goto err_device_create; 134 | } 135 | dev1.vir_gpio_dr=ioremap(res_mem->start,4); //将物理地址转化为虚拟地址 136 | if(IS_ERR(dev1.vir_gpio_dr)) 137 | { 138 | ret=PTR_ERR(dev1.vir_gpio_dr); //PTR_ERR()来返回错误代码 139 | goto err_ioremap; 140 | } 141 | 142 | 143 | return 0; 144 | 145 | err_ioremap: 146 | iounmap(dev1.vir_gpio_dr); 147 | 148 | err_device_create: 149 | class_destroy(dev1.class); //删除类 150 | 151 | err_class_create: 152 | cdev_del(&dev1.cdev_test); //删除cdev 153 | 154 | err_chr_add: 155 | unregister_chrdev_region(dev1.dev_num, 1); //注销设备号 156 | 157 | err_chrdev: 158 | return ret; 159 | } 160 | 161 | static int my_platform_driver_remove(struct platform_device *pdev) 162 | { 163 | // 设备移除操作 164 | return 0; 165 | } 166 | 167 | static struct platform_driver my_platform_driver = { 168 | .driver = { 169 | .name = "my_platform_device", // 与 platform_device.c 中的设备名称匹配 170 | .owner = THIS_MODULE, 171 | }, 172 | .probe = my_platform_driver_probe, 173 | .remove = my_platform_driver_remove, 174 | }; 175 | 176 | static int __init my_platform_driver_init(void) 177 | { 178 | int ret; 179 | 180 | ret = platform_driver_register(&my_platform_driver); // 注册平台驱动 181 | if (ret) { 182 | printk("Failed to register platform driver\n"); 183 | return ret; 184 | } 185 | 186 | printk("Platform driver registered\n"); 187 | return 0; 188 | } 189 | 190 | static void __exit my_platform_driver_exit(void) 191 | { 192 | /*注销字符设备*/ 193 | unregister_chrdev_region(dev1.dev_num, 1); //注销设备号 194 | cdev_del(&dev1.cdev_test); //删除cdev 195 | device_destroy(dev1.class, dev1.dev_num); //删除设备 196 | class_destroy(dev1.class); //删除类 197 | platform_driver_unregister(&my_platform_driver); // 注销平台驱动 198 | printk("Platform driver unregistered\n"); 199 | } 200 | 201 | module_init(my_platform_driver_init); 202 | module_exit(my_platform_driver_exit); 203 | 204 | MODULE_LICENSE("GPL"); 205 | MODULE_AUTHOR("enlai"); 206 | -------------------------------------------------------------------------------- /12_platform_led/module/platform_led.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/12_platform_led/module/platform_led.ko -------------------------------------------------------------------------------- /12_platform_led/module/platform_led.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /12_platform_led/module/platform_led.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/12_platform_led/module/platform_led.mod.o -------------------------------------------------------------------------------- /12_platform_led/module/platform_led.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/12_platform_led/module/platform_led.o -------------------------------------------------------------------------------- /13_devicetree_probe/dts/boot.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/13_devicetree_probe/dts/boot.img -------------------------------------------------------------------------------- /13_devicetree_probe/dts/rk3568-evb1-ddr4-v10-linux.dts: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: (GPL-2.0+ OR MIT) 2 | /* 3 | * Copyright (c) 2020 Rockchip Electronics Co., Ltd. 4 | * 5 | */ 6 | 7 | #include "rk3568-evb1-ddr4-v10.dtsi" 8 | #include "rk3568-linux.dtsi" 9 | 10 | #include 11 | /{ 12 | topeet{ 13 | #address-cells = <1>; 14 | #size-cells = <1>; 15 | compatible = "simple-bus"; 16 | 17 | myLed{ 18 | compatible = "my devicetree"; 19 | reg = <0xFDD60000 0x00000004>; 20 | }; 21 | }; 22 | }; 23 | 24 | &vp0 { 25 | cursor-win-id = ; 26 | }; 27 | 28 | &vp1 { 29 | cursor-win-id = ; 30 | }; 31 | 32 | 33 | &uart7 { 34 | status ="okay"; 35 | pinctrl-name = "default"; 36 | pinctrl-0 = <&uart7m1_xfer>; 37 | }; 38 | &uart4 { 39 | status = "okay"; 40 | pinctrl-names = "default"; 41 | pinctrl-0 = <&uart4m1_xfer>; 42 | }; 43 | &uart9 { 44 | status = "okay"; 45 | pinctrl-names = "default"; 46 | pinctrl-0 = <&uart9m1_xfer>; 47 | }; 48 | &can1 { 49 | status = "okay"; 50 | compatible = "rockchip,canfd-1.0"; 51 | assigned-clocks = <&cru CLK_CAN1>; 52 | assigned-clock-rates = <150000000>; //If can bitrate lower than 3M,the clock-rates should set 100M,else set 200M. 53 | pinctrl-names = "default"; 54 | pinctrl-0 = <&can1m1_pins>; 55 | }; 56 | -------------------------------------------------------------------------------- /13_devicetree_probe/module/Makefile: -------------------------------------------------------------------------------- 1 | export ARCH=arm64#设置平台架构 2 | export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀 3 | obj-m += platform_driver.o #platform_device.c对应.o文件的名称。名称要保持一致。 4 | KDIR :=/home/topeet/Linux/linux_sdk/kernel #内核源码所在虚拟机ubuntu的实际路径 5 | PWD ?= $(shell pwd) 6 | all: 7 | make -C $(KDIR) M=$(PWD) modules #make操作 8 | clean: 9 | make -C $(KDIR) M=$(PWD) clean #make clean操作 10 | 11 | -------------------------------------------------------------------------------- /13_devicetree_probe/module/Module.symvers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/13_devicetree_probe/module/Module.symvers -------------------------------------------------------------------------------- /13_devicetree_probe/module/modules.order: -------------------------------------------------------------------------------- 1 | kernel//home/topeet/work/54_devicetree_probe/module/platform_driver.ko 2 | -------------------------------------------------------------------------------- /13_devicetree_probe/module/platform_driver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // 平台设备的初始化函数 5 | static int my_platform_probe(struct platform_device *pdev) 6 | { 7 | printk(KERN_INFO "my_platform_probe: Probing platform device\n"); 8 | 9 | // 添加设备特定的操作 10 | // ... 11 | 12 | return 0; 13 | } 14 | 15 | // 平台设备的移除函数 16 | static int my_platform_remove(struct platform_device *pdev) 17 | { 18 | printk(KERN_INFO "my_platform_remove: Removing platform device\n"); 19 | 20 | // 清理设备特定的操作 21 | // ... 22 | 23 | return 0; 24 | } 25 | 26 | 27 | const struct of_device_id of_match_table_id[] = { 28 | {.compatible="my devicetree"}, 29 | }; 30 | 31 | // 定义平台驱动结构体 32 | static struct platform_driver my_platform_driver = { 33 | .probe = my_platform_probe, 34 | .remove = my_platform_remove, 35 | .driver = { 36 | .name = "my_platform_device", 37 | .owner = THIS_MODULE, 38 | .of_match_table = of_match_table_id, 39 | }, 40 | }; 41 | 42 | // 模块初始化函数 43 | static int __init my_platform_driver_init(void) 44 | { 45 | int ret; 46 | 47 | // 注册平台驱动 48 | ret = platform_driver_register(&my_platform_driver); 49 | if (ret) { 50 | printk(KERN_ERR "Failed to register platform driver\n"); 51 | return ret; 52 | } 53 | 54 | printk(KERN_INFO "my_platform_driver: Platform driver initialized\n"); 55 | 56 | return 0; 57 | } 58 | 59 | // 模块退出函数 60 | static void __exit my_platform_driver_exit(void) 61 | { 62 | // 注销平台驱动 63 | platform_driver_unregister(&my_platform_driver); 64 | 65 | printk(KERN_INFO "my_platform_driver: Platform driver exited\n"); 66 | } 67 | 68 | module_init(my_platform_driver_init); 69 | module_exit(my_platform_driver_exit); 70 | 71 | MODULE_LICENSE("GPL"); 72 | MODULE_AUTHOR("enlai"); -------------------------------------------------------------------------------- /13_devicetree_probe/module/platform_driver.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/13_devicetree_probe/module/platform_driver.ko -------------------------------------------------------------------------------- /13_devicetree_probe/module/platform_driver.mod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | BUILD_SALT; 7 | 8 | MODULE_INFO(vermagic, VERMAGIC_STRING); 9 | MODULE_INFO(name, KBUILD_MODNAME); 10 | 11 | __visible struct module __this_module 12 | __attribute__((section(".gnu.linkonce.this_module"))) = { 13 | .name = KBUILD_MODNAME, 14 | .init = init_module, 15 | #ifdef CONFIG_MODULE_UNLOAD 16 | .exit = cleanup_module, 17 | #endif 18 | .arch = MODULE_ARCH_INIT, 19 | }; 20 | 21 | #ifdef CONFIG_RETPOLINE 22 | MODULE_INFO(retpoline, "Y"); 23 | #endif 24 | 25 | static const char __module_depends[] 26 | __used 27 | __attribute__((section(".modinfo"))) = 28 | "depends="; 29 | 30 | -------------------------------------------------------------------------------- /13_devicetree_probe/module/platform_driver.mod.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/13_devicetree_probe/module/platform_driver.mod.o -------------------------------------------------------------------------------- /13_devicetree_probe/module/platform_driver.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Keyman-ai/linux-driver-demo/d8201e193ccfd488df8c80827c838120b5659ce4/13_devicetree_probe/module/platform_driver.o -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # linux-driver-demo 2 | ## Linux驱动框架 3 | Linux 设备驱动程序是 Linux 内核中用于与硬件设备交互的模块。它们提供了硬件设备和操作系统之间的接口,使得应用程序能够通过标准的系统调用来访问硬件设备。以下是 Linux 设备驱动框架的概述,包括常见的驱动类型和驱动程序的基本结构。 4 | ### Linux 设备驱动框架 5 | 设备驱动类型 6 | ### 字符设备驱动程序(Character Device Drivers): 7 | 8 | 处理字符流,如串口设备、键盘、鼠标等。 9 | 提供了 open, read, write, close, ioctl 等操作。 10 | 11 | ### 块设备驱动程序(Block Device Drivers): 12 | 13 | 处理块数据,如硬盘、SSD 等。 14 | 提供了 open, close, ioctl, read, write, request 等操作。 15 | 使用缓冲区来优化读写性能。 16 | 17 | ### 网络设备驱动程序(Network Device Drivers): 18 | 19 | 处理网络数据包传输,如以太网卡、Wi-Fi 适配器等。 20 | 提供了 open, stop, hard_start_xmit, get_stats, set_mac_address, do_ioctl 等操作。 21 | 22 | ### 驱动程序的基本结构 23 | Linux 设备驱动程序通常包括以下几个部分: 24 | 25 | #### 初始化和清理: 26 | 27 | module_init 和 module_exit 宏用于指定模块的初始化和清理函数,也是驱动的出入口,当用户态执行insmod和rmmod命令时会触发module_init和module_exit函数 28 | 29 | #### 设备文件操作: 30 | 31 | 定义设备文件操作结构体 file_operations,包含打开、读取、写入、关闭等操作。 32 | 33 | #### 设备注册和注销: 34 | 35 | 注册字符设备 register_chrdev。 36 | 注册块设备 register_blkdev。 37 | 注册网络设备 register_netdev。 38 | 39 | #### 中断处理: 40 | 41 | 注册中断处理程序 request_irq。 42 | 43 | ### 示例代码 44 | 以下是一个简单的字符设备驱动程序示例: 45 | 46 | ```c 47 | #include 48 | #include 49 | #include 50 | 51 | #define DEVICE_NAME "mychardev" 52 | #define BUFFER_SIZE 1024 53 | 54 | static int major_number; 55 | static char buffer[BUFFER_SIZE]; 56 | static int buffer_size = 0; 57 | 58 | static int dev_open(struct inode *inodep, struct file *filep) { 59 | printk(KERN_INFO "Device opened\n"); 60 | return 0; 61 | } 62 | 63 | static int dev_release(struct inode *inodep, struct file *filep) { 64 | printk(KERN_INFO "Device closed\n"); 65 | return 0; 66 | } 67 | 68 | static ssize_t dev_read(struct file *filep, char *user_buffer, size_t len, loff_t *offset) { 69 | int bytes_to_read = len < buffer_size ? len : buffer_size; 70 | if (copy_to_user(user_buffer, buffer, bytes_to_read)) { 71 | return -EFAULT; 72 | } 73 | buffer_size = 0; // Clear the buffer after reading 74 | return bytes_to_read; 75 | } 76 | 77 | static ssize_t dev_write(struct file *filep, const char *user_buffer, size_t len, loff_t *offset) { 78 | int bytes_to_write = len < BUFFER_SIZE ? len : BUFFER_SIZE; 79 | if (copy_from_user(buffer, user_buffer, bytes_to_write)) { 80 | return -EFAULT; 81 | } 82 | buffer_size = bytes_to_write; 83 | return bytes_to_write; 84 | } 85 | 86 | static struct file_operations fops = { 87 | .open = dev_open, 88 | .read = dev_read, 89 | .write = dev_write, 90 | .release = dev_release, 91 | }; 92 | 93 | static int __init mychardev_init(void) { 94 | major_number = register_chrdev(0, DEVICE_NAME, &fops); 95 | if (major_number < 0) { 96 | printk(KERN_ALERT "Failed to register a major number\n"); 97 | return major_number; 98 | } 99 | printk(KERN_INFO "Registered correctly with major number %d\n", major_number); 100 | return 0; 101 | } 102 | 103 | static void __exit mychardev_exit(void) { 104 | unregister_chrdev(major_number, DEVICE_NAME); 105 | printk(KERN_INFO "Device unregistered\n"); 106 | } 107 | 108 | module_init(mychardev_init); 109 | module_exit(mychardev_exit); 110 | 111 | MODULE_LICENSE("GPL"); 112 | MODULE_AUTHOR("Your Name"); 113 | MODULE_DESCRIPTION("A simple Linux char driver"); 114 | MODULE_VERSION("0.1"); 115 | ``` 116 | 117 | ### 详细说明各部分内容: 118 | 初始化和清理函数: 119 | 120 | mychardev_init 是模块加载时调用的初始化函数,使用 module_init 宏指定。 121 | mychardev_exit 是模块卸载时调用的清理函数,使用 module_exit 宏指定。 122 | 设备文件操作函数: 123 | 124 | dev_open 和 dev_release 用于处理设备文件的打开和关闭操作。 125 | dev_read 和 dev_write 分别处理设备文件的读取和写入操作。 126 | 文件操作结构体: 127 | 128 | file_operations 结构体 fops 定义了设备文件的操作函数。 129 | 设备注册和注销: 130 | 131 | 在初始化函数中调用 register_chrdev 注册字符设备,并在清理函数中调用 unregister_chrdev 注销设备。 132 | 这个简单的字符设备驱动程序展示了如何实现基本的文件操作,包括打开、读取、写入和关闭设备文件。你可以根据实际需求扩展这个示例,添加更多的功能和处理逻辑。 133 | 134 | #### 其他具体的驱动代码请详见本仓库code 135 | 136 | 137 | 138 | 139 | --------------------------------------------------------------------------------