Linux驱动开发:USB驱动之usb(3)

/*如果所有这些探测都通过了,驱动程序就知道它已经发现了正确的端点类型,可以把该端点的相关信息保存到一个局部结构体中以便稍后用它来和端点进行通信*/

if (!dev->bulk_in_endpointAddr && usb_endpoint_is_bulk_in(endpoint)) {

/* 找到一个批量IN类型的端点 */

buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);

dev->bulk_in_size = buffer_size;

dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;

dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);

if (!dev->bulk_in_buffer) {

err("Could not allocate bulk_in_buffer");

goto error;

}

}

if (!dev->bulk_out_endpointAddr && usb_endpoint_is_bulk_out(endpoint)) {

/* we found a bulk out endpoint */

dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;

}

}

if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {

err("Could not find both bulk-in and bulk-out endpoints");

goto error;

}

/*因为USB驱动程序要在设备的生命周期的稍后时间获取和接口相关联的局部数据结构体,所以调用了usb_set_intfdata函数,把它保存到struct usb_interface结构体中以便后面的访问*/

/* 把数据指针保存到这个接口设备中 */

usb_set_intfdata(interface, dev);

/*我们以后调用usb_set_intfdata函数来获取数据。当这一切都完成后,USB驱动程序必须在探测函数中调用usb_register_dev函数来把该设备注册到USB核心里*/

/* 注册设备到USB核心 */

retval = usb_register_dev(interface, &skel_class);

if (retval) {

/* 有些情况下是不允许注册驱动程序的 */
                err("Not able to get a minor for this device.");

usb_set_intfdata(interface, NULL);

goto error;

}

/* let the user know what node this device is now attached to */

info("USB Skeleton device now attached to USBSkel-%d", interface->minor);

return 0;

error:

if (dev)

/* this frees allocated memory */

kref_put(&dev->kref, skel_delete);

return retval;

}

//9、骨架驱动断开函数

static void skel_disconnect(struct usb_interface *interface)

{

struct usb_skel *dev;

int minor = interface->minor;

/* prevent skel_open() from racing skel_disconnect() */

mutex_lock(&skel_open_lock);

dev = usb_get_intfdata(interface);

usb_set_intfdata(interface, NULL);

/* give back our minor */

usb_deregister_dev(interface, &skel_class);

mutex_unlock(&skel_open_lock);

/* prevent more I/O from starting */

mutex_lock(&dev->io_mutex);

dev->interface = NULL;

mutex_unlock(&dev->io_mutex);

/* decrement our usage count */

kref_put(&dev->kref, skel_delete);

info("USB Skeleton #%d now disconnected", minor);

}

//6、对USB骨架驱动结构体对象进行初始化

static struct usb_driver skel_driver = {

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wywgsz.html