pdri.ko
#include <linux/init.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/mod_devicetable.h> #include <linux/of.h> #include <linux/of_gpio.h> struct resource *res; unsigned int irq1; struct gpio_desc *gpiono1; struct gpio_desc *gpiono2; struct gpio_desc *gpiono3; struct platform_device *pdev1; struct class *cls; struct device *dev; int major; char kbuf[128] = {0}; //Define operation method int mycdev_open(struct inode *inode, struct file *file) { printk("%s:%s:%d\ ", __FILE__, __func__, __LINE__); return 0; } ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof) { printk("%s:%s:%d\ ", __FILE__, __func__, __LINE__); int ret1=copy_to_user(ubuf,kbuf,size); if(ret1) { printk("copy_to_user filed\ "); return -EIO; } return 0; } ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof) { printk("%s:%s:%d\ ", __FILE__, __func__, __LINE__); int ret=copy_from_user(kbuf,ubuf,size); if(ret) { printk("copy_from_user filed\ "); return -EIO; } if(!strcmp(kbuf,"on")) { gpiod_set_value(gpiono1,1); gpiod_set_value(gpiono2,1); gpiod_set_value(gpiono3,1); } else if(!strcmp(kbuf,"off")) { gpiod_set_value(gpiono1,0); gpiod_set_value(gpiono2,0); gpiod_set_value(gpiono3,0); } return 0; } int mycdev_close(struct inode *inode, struct file *file) { printk("%s:%s:%d\ ", __FILE__, __func__, __LINE__); return 0; } //Define operation method structure variables and assign values struct file_operations fops = { .open = mycdev_open, .read = mycdev_read, .write = mycdev_write, .release = mycdev_close, }; int pdrv_probe(struct platform_device *pdev) { //Character device driver registration pdev1 = pdev; major = register_chrdev(0, "mychrdev", & amp;fops); if (major < 0) { printk("Character device driver registration failed\ "); return major; } printk("Character device driver registration successful: major=%d\ ", major); // Submit directory upwards cls = class_create(THIS_MODULE, "mychrdev"); if (IS_ERR(cls)) { printk("Failed to submit directory upward\ "); return -PTR_ERR(cls); } printk("Submit directory upward successfully\ "); //Submit device node information upwards int i; // Submit device node information upward three times for (i = 0; i < 3; i + + ) { dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i); if (IS_ERR(dev)) { printk("Failed to submit device node upward\ "); return -PTR_ERR(dev); } } printk("Submit device node upward successfully\ "); gpiono1 = gpiod_get_from_of_node(pdev1->dev.of_node,"led1-gpio-no",0,GPIOD_OUT_HIGH,NULL); gpiono2 = gpiod_get_from_of_node(pdev1->dev.of_node,"led2-gpio-no",0,GPIOD_OUT_LOW,NULL); gpiono3 = gpiod_get_from_of_node(pdev1->dev.of_node,"led3-gpio-no",0,GPIOD_OUT_HIGH,NULL); if(IS_ERR(gpiono1)) { printk("Failed to parse GPIO on resource 1\ "); return -PTR_ERR(gpiono1); } printk("Parse GPIO on resource 1 successfully\ "); if(IS_ERR(gpiono2)) { printk("Failed to parse GPIO on resource 2\ "); return -PTR_ERR(gpiono2); } printk("Parse GPIO on resource 2 successfully\ "); if(IS_ERR(gpiono3)) { printk("Failed to parse GPIO on resource 3\ "); return -PTR_ERR(gpiono3); } printk("Parse GPIO on resource 3 successfully\ "); printk("USB connection successful\ "); return 0; } int pdrv_remove(struct platform_device *pdev) { //Release resources gpiod_set_value(gpiono1,0); gpiod_put(gpiono1); gpiod_set_value(gpiono2,0); gpiod_put(gpiono2); gpiod_set_value(gpiono3,0); gpiod_put(gpiono3); printk("USB device has been ejected\ "); return 0; } struct of_device_id oftable[] = { {.compatible = "kpkp,myplatform",}, {.compatible = "kpkp,myplatform1",}, {}, }; struct platform_driver pdrv = { .probe = pdrv_probe, //This is an int type function name, the parameter type is struct platform_device* .remove = pdrv_remove, //This is an int type function name, and the parameter type is struct platform_device* .driver = { .name = "AK47", .of_match_table = oftable, }, }; //Register macro with one click module_platform_driver(pdrv); MODULE_LICENSE("GPL");
pdev.ko
#include <linux/init.h> #include <linux/module.h> #include <linux/platform_device.h> //This is a structure struct resource res[] = { [0]={ .start = 0x50006000, .end = 0x50006000 + 0x400, .flags = IORESOURCE_MEM, }, [1]={ .start = 71, .end = 71, .flags = IORESOURCE_IRQ, }, }; void pdev_release(struct device *dev) //This is a function name { printk("It has popped up, looking forward to the next link\ "); } struct platform_device pdev = { .name = "AK47", .id = PLATFORM_DEVID_AUTO, .dev.release = pdev_release, //This is a function name .num_resources = ARRAY_SIZE(res), .resource = res, //This is a structure }; \t static int __init mycdev_init(void) { //Define the device object and initialize it //Register device information into the kernel if(platform_device_register( & amp;pdev) < 0 ) { printk("Failed to register device information\ "); return -ENXIO; } return 0; } static void __exit mycdev_exit(void) { //Log out device information platform_device_unregister( & amp;pdev); } module_init(mycdev_init); module_exit(mycdev_exit); MODULE_LICENSE("GPL");
ledu.c
#include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main(int argc,const char * argv[]) { int a=0; char buf[128] = ""; char buf1[128] = ""; int fd1=open("/dev/myled0",O_RDWR); int fd2=open("/dev/myled1",O_RDWR); int fd3=open("/dev/myled2",O_RDWR); if(fd1<0) { printf("Failed to open device file 1\ "); exit(-1); } if(fd2<0) { printf("Failed to open device file 2\ "); exit(-1); } if(fd3<0) { printf("Failed to open device file 3\ "); exit(-1); } memset(buf,0,sizeof(buf)); while(1) { printf("Please enter the light control (1 to turn on the light/0 to turn off the light) => "); scanf("%d", & amp;a); write(fd1,buf,sizeof(buf)); write(fd2,buf,sizeof(buf)); write(fd3,buf,sizeof(buf)); if(a == 1) { strcpy(buf,"on"); write(fd1,buf,sizeof(buf)); write(fd2,buf,sizeof(buf)); write(fd3,buf,sizeof(buf)); } else if(a == 0) { strcpy(buf,"off"); write(fd1,buf,sizeof(buf)); write(fd2,buf,sizeof(buf)); write(fd3,buf,sizeof(buf)); } read(fd1,buf1,sizeof(buf)); printf("buf1:%s\ ",buf1); } return 0;