深入浅出Linux设备驱动编程--复杂设备驱动
作者 佚名技术
来源 Linux系统
浏览
发布时间 2012-05-16
3, 1, 2), driver_info: (unsigned long)"mouse" } , { 0, /* no more matches */ } }; static struct usb_driver sample_usb_driver = { name: "sample", probe: sample_probe, disconnect: sample_disconnect, id_table: sample_id_table, }; 当一个USB 设备从系统拔掉后,设备驱动程序的disconnect 函数会自动被调用,在执行了disconnect 函数后,所有为USB 设备分配的数据结构,内存空间都会被释放: static void sample_disconnect(struct usb_device *udev, void *clientdata) { /* the clientdata is the sample_device we passed originally */ struct sample_device *sample = clientdata; /* remove the URB, remove the input device, free memory */ usb_unlink_urb(&sample->urb); kfree(sample); printk(KERN_INFO "sample: USB %s disconnectedn", sample->name); /* * here you might MOD_DEC_USE_COUNT, but only if you increment * the count in sample_probe() below */ return; } 当驱动程序向子系统注册后,插入一个新的USB设备后总是要自动进入probe函数.驱动程序会为这个新加入系统的设备向内部的数据结构建立一个新的实例.通常情况下,probe 函数执行一些功能来检测新加入的USB 设备硬件中的生产厂商和产品定义以及设备所属的类或子类定义是否与驱动程序相符,若相符,再比较接口的数目与本驱动程序支持设备的接口数目是否相符.一般在probe 函数中也会解析USB 设备的说明,从而确认新加入的USB 设备会使用这个驱动程序: static void *sample_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id) { /* * The probe procedure is pretty standard. Device matching has already * been performed based on the id_table structure (defined later) */ struct usb_interface *iface; struct usb_interface_descriptor *interface; struct usb_endpoint_descriptor *endpoint; struct sample_device *sample; printk(KERN_INFO "usbsample: probe called for %s devicen", (char *)id->driver_info /* "mouse" or "keyboard" */ ); iface = &udev->actconfig->interface[ifnum]; interface = &iface->altsetting[iface->act_altsetting]; if (interface->bNumEndpoints != 1) return NULL; endpoint = interface->endpoint 0; if (!(endpoint->bEndpointAddress & 0x80)) return NULL; if ((endpoint->bmAttributes & 3) != 3) return NULL; usb_set_protocol(udev, interface->bInterfaceNumber, 0); usb_set_idle(udev, interface->bInterfaceNumber, 0, 0); /* allocate and zero a new data structure for the new device */ sample = kmalloc(sizeof(struct sample_device), GFP_KERNEL); if (!sample) return NULL; /* failure */ memset(sample, 0, sizeof(*sample)); sample->name = (char *)id->driver_info; /* fill the URB data structure using the FILL_INT_URB macro */ { int pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress); int maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe)); if (maxp > 8) maxp = 8; sample->maxp = maxp; /* remember for later */ FILL_INT_URB(&sample->urb, udev, pipe, sample->data, maxp, sample_irq, sample, endpoint->bInterval); } /* register |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于深入浅出Linux设备驱动编程--复杂设备驱动的所有评论