linux总线设备驱动模型
本篇文章通过平台总线设备模型为例,讲解总线设备驱动模型:
struct resource led_resource[] = { [0] = { .start = 0x56000000, .end = 0x56000000 + 8 - 1, .flags = IORESOURCE_MEM, }, [1] = { .start = 5, .end = 5, .flags = IORESOUCE_IRQ, }, };
struct platform_device led_dev = { .name = "myled"; .id = -1; .num_resource = ARRAY_SIZE(led_resource); .resource = led_resource, .dev = { .release = led_release, }, }; /* release函数可以什么都不做,但是不能为空 */ static void led_release(struct device * dev) { }
platform_device_register(&led_dev);
struct platform_driver led_drv = { .probe = led_probe, .remove = led_remove, .driver = { .name = "myled", }, };
static struct file_operations led_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = led_open, .write = led_write, }; static int led_open(struct inode *inode, struct file *file) { //printk("first_drv_open\n"); /* 配置为输出 */ *gpio_con &= ~(0x3<<(pin*2)); *gpio_con |= (0x1<<(pin*2)); return 0; } static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) { int val; //printk("first_drv_write\n"); copy_from_user(&val, buf, count); // copy_to_user(); if (val == 1) { // 点灯 *gpio_dat &= ~(1<<pin); } else { // 灭灯 *gpio_dat |= (1<<pin); } return 0; }
static int led_probe(struct platform_device *pdev) { struct resource *res; *platform_get_resource *@dev:platformdevice *@type:resourcetype *@num:resourceindex res = platform_get_resource(pdev, IORESOURCE_MEM, 0); gpio_con = ioremap(res->start,res->end-res->start+1); gpio_dat = gpio_con + 1; res = platform_get_resource(pdev, IORESOURCE_IRQ, 0) pin = res->start; major = register_chrdev(0, "myled", &led_fops); cls = class_create(THIS_MODULE, "myled"); device_create(cls, NULL, MKDEV(major,0), NULL, "led"); }
platform_driver_register(&led_drv);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。