点击(此处)折叠或打开
- static int __init usb_skel_init(void)
- {
- int result;
- /* register this driver with the USB subsystem */
- result = usb_register(&skel_driver);
- if (result)
- err("usb_register failed. Error number %d", result);
- return result;
- }
- static void __exit usb_skel_exit(void)
- {
- /* deregister this driver with the USB subsystem */
- usb_deregister(&skel_driver);
- }
- module_init (usb_skel_init);
- module_exit (usb_skel_exit);
- MODULE_LICENSE("GPL");
这个注册与销毁使用了usb_register(struct *usb_driver)以及usb_deregister(struct *usb_driver);那这个结构体需要做些什么呢?他要向系统提供几个函数入口,跟驱动的名字:
点击(此处)折叠或打开
- static struct usb_driver skel_driver = {
- .owner = THIS_MODULE,
- .name = "skeleton",
- .id_table = skel_table,
- .probe = skel_probe,
- .disconnect = skel_disconnect,
- };
点击(此处)折叠或打开
- /* Define these values to match your devices */
- #define USB_SKEL_VENDOR_ID 0xfff0
- #define USB_SKEL_PRODUCT_ID 0xfff0
- /* table of devices that work with this driver */
- static struct usb_device_id skel_table [] = {
- { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
- { } /* Terminating entry */
- };
- MODULE_DEVICE_TABLE (usb, skel_table);
点击(此处)折叠或打开
- static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id)
- {
- struct usb_skel *dev = NULL;
- struct usb_host_interface *iface_desc;
- struct usb_endpoint_descriptor *endpoint;
- size_t buffer_size;
- int i;
- int retval = -ENOMEM;
- /* allocate memory for our device state and initialize it */
- dev = kmalloc(sizeof(struct usb_skel), GFP_KERNEL);
- if (dev == NULL) {
- err("Out of memory");
- goto error;
- }
- memset(dev, 0x00, sizeof (*dev));
- kref_init(&dev->kref);
- dev->udev = usb_get_dev(interface_to_usbdev(interface));
- dev->interface = interface;
- /* set up the endpoint information */
- /* use only the first bulk-in and bulk-out endpoints */
- iface_desc = interface->cur_altsetting;
- for (i = 0; i < iface_desc->desc.bNumEndpoints; i) {
- endpoint = &iface_desc->endpoint[i].desc;
- if (!dev->bulk_in_endpointAddr &&
- (endpoint->bEndpointAddress & USB_DIR_IN) &&
- ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
- == USB_ENDPOINT_XFER_BULK)) {
- /* we found a bulk in endpoint */
- buffer_size = 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 &&
- !(endpoint->bEndpointAddress & USB_DIR_IN) &&
- ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
- == USB_ENDPOINT_XFER_BULK)) {
- /* 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;
- }
- /* save our data pointer in this interface device */
- usb_set_intfdata(interface, dev);
- /* we can register the device now, as it is ready */
- retval = usb_register_dev(interface, &skel_class);
- if (retval) {
- /* something prevented us from registering this driver */
- 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)
- kref_put(&dev->kref, skel_delete);
- return retval;
- }
点击(此处)折叠或打开
- /* Structure to hold all of our device specific stuff */
- struct usb_skel {
- struct usb_device * udev; /* the usb device for this device */
- struct usb_interface * interface; /* the interface for this device */
- unsigned char * bulk_in_buffer; /* the buffer to receive data */
- size_t bulk_in_size; /* the size of the receive buffer */
- __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
- __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
- struct kref kref;
- };
20:dev->interface = interface;
在17行,我们对usb_skel中的kref结构体进行初始化,设置这个引用计数器的值,这个值用来说明对模块的引用次数,初始化函数如下:
点击(此处)折叠或打开
- /***
- * kref_init - initialize object.
- * @kref: object in question.
- */
- void kref_init(struct kref *kref)
- {
- atomic_set(&kref->refcount,1);
- smp_mb();
- }
点击(此处)折叠或打开
- #define interface_to_usbdev(intf) \
- container_of(intf->dev.parent, struct usb_device, dev)
56-58:向系统注册一些以后会用的的信息。首先我们来说明一下usb_set_intfdata(),他向内核注册一个data,这个data的结构可以是任意的,这段程序向内核注册了一个usb_skel结构。就是我们刚刚看到的被初始化的那个。之所以要把他注册,是因为我们定义的usb_skel结构不是全局变量,其他的函数需要使用的时候,可以后用usb_get_intfdata来得到。我们可以再内核源码中找到对它的定义:(/linux/usb.h)
点击(此处)折叠或打开
- static inline void usb_set_intfdata(struct usb_interface *intf, void *data)
- {
- dev_set_drvdata(&intf->dev, data);
- }
点击(此处)折叠或打开
- static inline unsigned int dev_set_drvdata(struct device *dev, void *data)
- {
- dev->driver_data = data;;
- }
这下我们就明白,我们是把dev的设备信息保存到了interface->dev->driver_data中。在以后使用的时候,只需要调用usb_get_intfdata就可以得到dev的信息。
点击(此处)折叠或打开
- static struct usb_class_driver skel_class = {
- .name = "usb/skel%d",
- .fops = &skel_fops,
- .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
- .minor_base = USB_SKEL_MINOR_BASE,
- };
点击(此处)折叠或打开
- static struct file_operations skel_fops = {
- .owner = THIS_MODULE,
- .read = skel_read,
- .write = skel_write,
- .open = skel_open,
- .release = skel_release,
- };
点击(此处)折叠或打开
- /**
- * kref_put - decrement refcount for object.
- * @kref: object.
- * @release: pointer to the function that will clean up the object when the
- * last reference to the object is released.
- * This pointer is required, and it is not acceptable to pass kfree
- * in as this function.
- *
- * Decrement the refcount, and if 0, call release().
- * Return 1 if the object was removed, otherwise return 0. Beware, if this
- * function returns 0, you still can not count on the kref from remaining in
- * memory. Only use the return value if you want to see if the kref is now
- * gone, not present.
- */
- int kref_put(struct kref *kref, void (*release)(struct kref *kref))
- {
- WARN_ON(release == NULL);
- WARN_ON(release == (void (*)(struct kref *))kfree);
- /*
- * if current count is one, we are the last user and can release object
- * right now, avoiding an atomic operation on 'refcount'
- */
- if ((atomic_read(&kref->refcount) == 1) ||
- (atomic_dec_and_test(&kref->refcount))) {
- release(kref);
- return 1;
- }
- return 0;
- }
当我们执行打开操作时,我们要增加kref的计数,我们可以用kref_get,来完成。所有对struct kref的操作都有内核代码确保其原子性。
点击(此处)折叠或打开
- static void skel_disconnect(struct usb_interface *interface)
- {
- struct usb_skel *dev;
- int minor = interface->minor;
- /* prevent skel_open() from racing skel_disconnect() */
- lock_kernel();
- dev = usb_get_intfdata(interface);
- usb_set_intfdata(interface, NULL);
- /* give back our minor */
- usb_deregister_dev(interface, &skel_class);
- unlock_kernel();
- /* decrement our usage count */
- kref_put(&dev->kref, skel_delete);
- info("USB Skeleton #%d now disconnected", minor);
- }
当设备被拔出集线器时,usb子系统会自动地调用disconnect,他做的事情不多,最重要的是注销class_driver(交还次设备号)和interface的data:
点击(此处)折叠或打开
- static int skel_open(struct inode *inode, struct file *file)
- {
- struct usb_skel *dev;
- struct usb_interface *interface;
- int subminor;
- int retval = 0;
- subminor = iminor(inode); //获取设备的次设备号
- interface = usb_find_interface(&skel_driver, subminor);
- if (!interface) {
- err ("%s - error, can't find device for minor %d",
- __FUNCTION__, subminor);
- retval = -ENODEV;
- goto exit;
- }
- dev = usb_get_intfdata(interface); //获取注册到接口的usb——skel数据。
- if (!dev) {
- retval = -ENODEV;
- goto exit;
- }
- /* increment our usage count for the device */
- kref_get(&dev->kref); //对usb_skel模块的使用计数
- /* save our object in the file's private structure */
- file->private_data = dev; //保存usb_skel结构的数据以便read,write函数使用
- exit:
- return retval;
- }
open函数很简单。主要是递增usb_skel的kref,并把该结构体存入file的private_data中,以便其他函数(如read、write)调用。
点击(此处)折叠或打开
- /**
- * usb_find_interface - find usb_interface pointer for driver and device
- * @drv: the driver whose current configuration is considered
- * @minor: the minor number of the desired device
- *
- * This walks the bus device list and returns a pointer to the interface
- * with the matching minor and driver. Note, this only works for devices
- * that share the USB major number.
- */
- struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
- {
- struct find_interface_arg argb;
- struct device *dev;
- argb.minor = minor;
- argb.drv = &drv->drvwrap.driver;
- dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface);
- /* Drop reference count from bus_find_device */
- put_device(dev);
- return dev ? to_usb_interface(dev) : NULL;
- }
- EXPORT_SYMBOL_GPL(usb_find_interface);
由于在open函数中,无法得到在prode函数中注册的设备的详细信息,而我们只能得到注册到接口的次设备号(通过subminor = iminor(inode);)以及我们注册的驱动skel_driver。
点击(此处)折叠或打开
- struct find_interface_arg {
- int minor;
- struct device_driver *drv;
- };
点击(此处)折叠或打开
- static int skel_release(struct inode *inode, struct file *file)
- {
- struct usb_skel *dev;
- dev = (struct usb_skel *)file->private_data;
- if (dev == NULL)
- return -ENODEV;
- /* decrement the count on our device */
- kref_put(&dev->kref, skel_delete);
- return 0;
- }
点击(此处)折叠或打开
- #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
- static struct usb_driver skel_driver;
- static void skel_delete(struct kref *kref)
- {
- struct usb_skel *dev = to_skel_dev(kref);
- usb_put_dev(dev->udev);
- kfree (dev->bulk_in_buffer);
- kfree (dev);
- }
5-12行,当引用计数的值(kref->recount)为0值,就会调用这个skel_delete函数,做相应的清理工作。
点击(此处)折叠或打开
- static ssize_t skel_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
- {
- struct usb_skel *dev;
- int retval = 0;
- dev = (struct usb_skel *)file->private_data; //获取设备的信息
- /* do a blocking bulk read to get data from the device */
- retval = usb_bulk_msg(dev->udev,
- usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
- dev->bulk_in_buffer,
- min(dev->bulk_in_size, count),
- &count, HZ*10);
- /* if the read was successful, copy the data to userspace */
- if (!retval) {
- if (copy_to_user(buffer, dev->bulk_in_buffer, count))
- retval = -EFAULT;
- else
- retval = count;
- }
- return retval;
- }
这个read函数,就是获取设备的信息(6行)以及读设备的信息,传送到用户空间(copy_to_user成功返回0,失败返回没有成功拷贝的字节数)。
值得我们看是8-13行。
/* do a blocking bulk read to get data from the device */
retval = usb_bulk_msg(dev->udev, //需要发送的设备
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr), //设置端点的信息
dev->bulk_in_buffer, //缓存的位置
min(dev->bulk_in_size, count), //缓存的大小
&count, HZ*10); //实际传送数据的大小,以及阻塞超时时间
程序使用了usb_bulk_msg来传送数据,它的原型如下:
该函数建立一个“receive、bulk类型”的pipe。
pipe是一个32位的值,记录了如下内容:
bit31~30,表示类型,bulk、interrupt、control或isochronous
bit23~16,记录usb_device-> devnum,它表示USB总线上的地址。
bit15~8,表示目标(要发送给谁)的endpoint地址
bit7~0,表示方向,USB_DIR_IN或USB_DIR_OUT
这里要说明一下IN和OUT:
在USB中,一切都是以Host为中心的,所以,在Host一方,IN是用来收数据的,而在Device一方正好相反,它的IN endpoint是用来发送数据的,OUT endpoint用来接受数据。