创建字符设备并创建文件系统接口

    科技2022-08-14  91

    定义全局变量: static int tftloadopen; struct cdev * tftloaddev; struct class * tftload_class; static dev_t tftload_dev; struct device *ub948_dev; #define TFTLOAD_MAJOR MAJOR(tftload_dev) #define TFTLOAD_MINOR MINOR(tftload_dev)

    文件系统接口: static char semiskybuf[100] = “Semsiky Attribute Example”; void disp948_enable_backlight(unsigned int en) { int ret; if(en==1) ret=disp948_reg_write(g_client,0x1F,0x09);//enable gpio5 output 1 else ret=disp948_reg_write(g_client,0x1F,0x01); //enable gpio5 output 0 } 2、文件系统属性接口定义

    /*************************************************************/ //inputing cat command will call this function static ssize_t ub948_enbacklight_show(struct device *dev, struct device_attribute *attr, char *buf) { return sprintf(buf, "%s\n", semiskybuf); //return sprintf(buf, "%s\n", enbacklight_status); } //inputing echo command will call this function static ssize_t ub948_enbacklight_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { sprintf(semiskybuf, "%s", buf); if (!strncmp(buf, "enblk:0", 7)) { printk("disable backlight\n"); disp948_enable_backlight(0); msleep(20); }else if(!strncmp(buf, "enblk:1", 7)){ printk("enable backlight\n"); disp948_enable_backlight(1); msleep(20); } return len; } static DEVICE_ATTR(enbacklight, S_IWUSR | S_IRUSR, ub948_enbacklight_show, ub948_enbacklight_store); /*********************************************************************/

    注册文件系统接口:

    static const struct file_operations tftload_fops = { .owner = THIS_MODULE, /* owner */ .open = tftload_open, /* open */ .release = tftload_close, /* release */ .poll = tftload_poll, .read = tftload_read, .write = tftload_write, .unlocked_ioctl = tftload_ioctl, };

    init初始化函数:

    static int __init disp948_init(void) { printk("[disp948]:%s\n",__FUNCTION__); //分配一个字符设备 tftloaddev = cdev_alloc(); if(tftloaddev == NULL) { return -1; } if(alloc_chrdev_region(&tftload_dev, 0, 1, TFTLOAD_DEVICE_NAME)) { printk("Register tftload char dev error\n"); return -1; } tftloadopen = 0; //初始化 cdev_init(tftloaddev, &tftload_fops); if(cdev_add(tftloaddev, tftload_dev, 1)) { printk("Add tftload char dev error!\n"); } //创建一个字符设备 tftload_class = class_create(THIS_MODULE, "tftload_class"); if(IS_ERR(tftload_class)) { printk("Err: failed in creating tftload_class.\n"); return -1; } /* register your own device in sysfs, and this will cause udevd to create corresponding device node */ ub948_dev=device_create(tftload_class, NULL, tftload_dev, NULL, TFTLOAD_DEVICE_NAME); //创建字符属性接口 //ctreat attr if(device_create_file(ub948_dev, &dev_attr_enbacklight)) { return -1; } //end creat attr return i2c_add_driver(&disp948_driver); }

    退出函数:

    static void __exit disp948_exit(void) { printk("[disp928]:%s",__FUNCTION__); i2c_del_driver(&disp948_driver); unregister_chrdev_region(MKDEV(TFTLOAD_MAJOR,TFTLOAD_MINOR),10); cdev_del(tftloaddev); device_destroy(tftload_class, tftload_dev); class_destroy(tftload_class); } MODULE_AUTHOR("czwyle"); MODULE_DESCRIPTION("disp948 chip driver"); MODULE_LICENSE("GPL"); module_init(disp948_init); module_exit(disp948_exit);
    Processed: 0.026, SQL: 9