Linux usb驱动程序全注释

/*
 * USB Skeleton driver - 2.0
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kref.h>
#include <linux/smp_lock.h>
#include <linux/usb.h>
#include <asm/uaccess.h>


/* Define these values to match your devices */
#define USB_SKEL_VENDOR_ID 0xfff0//定义厂商ID
#define USB_SKEL_PRODUCT_ID 0xfff0//定义产品ID

/* 描述驱动支持的USB设备结构 */
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);


/* Get a minor range for your devices from the usb maintainer */
#define USB_SKEL_MINOR_BASE 192

/* 定义一个结构包含所有我们所需要的设备成员 */
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;/*内核引用计数*/
};
#define to_skel_dev(d) container_of(d, struct usb_skel, kref)

static struct usb_driver skel_driver;//定义一个usb设备驱动

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);
}

 

/*设备打开函数*/
static int skel_open(struct inode *inode, struct file *file)
{
 struct usb_skel *dev;//定义一个本地结构体引用
 struct usb_interface *interface;//定义一个usb设备主结构体引用
 int subminor;
 int retval = 0;
 /*在inode中的i_rdev成员中存储着设备文件的设备号,通过iminor可以从inode中获取
 设备文件的次设备号*/

 subminor = iminor(inode);

 
 /*遍历驱动获取次设备号匹配的usb主设备结构体*/
 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;
 }

 /*从interface中获取数据保存到dev中*/
 dev = usb_get_intfdata(interface);
 if (!dev) {
  retval = -ENODEV;
  goto exit;
 }
 
 /* increment our usage count for the device */
 /*增加设备的内核使用计数*/
 kref_get(&dev->kref);

 /* save our object in the file's private structure */
 /*将获取的本地设备保存到文件私有数据当中*/
 file->private_data = dev;

exit:
 return retval;
}

static int skel_release(struct inode *inode, struct file *file)
{
 struct usb_skel *dev;

 /*获取本地usb设备*/
 dev = (struct usb_skel *)file->private_data;
 if (dev == NULL)
  return -ENODEV;

 /* decrement the count on our device */
 /*减少usb设备的内核使用计数*/
 kref_put(&dev->kref, skel_delete);
 return 0;
}


/*设备读函数*/
static ssize_t skel_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
{
/*这里在读函数中我们使用无urb的方法实现数据的传输*/
 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 */
 /*从usb设备中获取数据保存到dev的bulk_in_buffer,
 当然此函数也可以发送数据到usb设备当中,主要取决于第二参数是in端点还是out端点*/
 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;
}

 

/*urb写入完成回调函数*/
static void skel_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
{
 /* sync/async unlink faults aren't errors */
 /*通过检查urb的状态来确定urb是否成功完成*/
 if (urb->status &&
     !(urb->status == -ENOENT ||
       urb->status == -ECONNRESET ||
       urb->status == -ESHUTDOWN)) {
  dbg("%s - nonzero write bulk status received: %d",
      __FUNCTION__, urb->status);
 }

 /* free up our allocated buffer */
 /*释放安排给这个urb传送的已分配的缓冲*/
 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
   urb->transfer_buffer, urb->transfer_dma);
}

 

/*字符设备写函数*/
static ssize_t skel_write(struct file *file, const char __user *user_buffer, size_t count, loff_t *ppos)
{
 struct usb_skel *dev;
 int retval = 0;
 struct urb *urb = NULL;
 char *buf = NULL;

 /*获取usb本地设备结构*/
 dev = (struct usb_skel *)file->private_data;

 /* verify that we actually have some data to write */
 if (count == 0)
  goto exit;

 /* create a urb, and a buffer for it, and copy the data to the urb */
 /*创建一个urb*/
 urb = usb_alloc_urb(0, GFP_KERNEL);
 if (!urb) {
  retval = -ENOMEM;
  goto error;
 }

 /*分配一个dma缓冲buf*/
 buf = usb_buffer_alloc(dev->udev, count, GFP_KERNEL, &urb->transfer_dma);
 if (!buf) {
  retval = -ENOMEM;
  goto error;
 }
 /*将用户空间写入的数据拷贝到缓冲区buf中*/
 if (copy_from_user(buf, user_buffer, count)) {
  retval = -EFAULT;
  goto error;
 }

 /* initialize the urb properly */
 /*在urb被提交之前需要被初始化*/
 /*在这里传入写入完成回调函数指针skel_write_bulk_callback*/
 usb_fill_bulk_urb(urb, dev->udev,
     usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
     buf, count, skel_write_bulk_callback, dev);
 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

 
 
 /* send the data out the bulk port */
 /*当urb的分配,数据拷贝,初始完成之后,就可以将urb提交给设备*/
 retval = usb_submit_urb(urb, GFP_KERNEL);
 if (retval) {
  err("%s - failed submitting write urb, error %d", __FUNCTION__, retval);
  goto error;
 }

 /* release our reference to this urb, the USB core will eventually free it entirely */
 usb_free_urb(urb);

exit:
 return count;

error:
 usb_buffer_free(dev->udev, count, buf, urb->transfer_dma);
 usb_free_urb(urb);
 kfree(buf);
 return retval;
}

static struct file_operations skel_fops = {
 .owner = THIS_MODULE,
 .read =  skel_read,
 .write = skel_write,
 .open =  skel_open,
 .release = skel_release,
};

/*
 * usb class driver info in order to get a minor number from the usb core,
 * and to have the device registered with devfs and the driver core
 */
 /**usb 类驱动 信息 使我们 能 从usb core  usb核  得到一个次 设备
 从而可以 使 设备被 注册 到 驱动内核中**/

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 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);//初始化内核引用计数

 /*获取usb设备结构体*/
 dev->udev = usb_get_dev(interface_to_usbdev(interface));
 /*设置usb主设备结构体对象*/
 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;

  /*判断此端点方向是否为IN
  判断此端点类型是否为块bulk*/
  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 */
   /*如果发现一个OUT块端点,将端点地址付给本地设备结构体中的相关成员*/
   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设备*/
 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;
}

 

/*去连接函数*/
static void skel_disconnect(struct usb_interface *interface)
{
 struct usb_skel *dev;
 int minor = interface->minor;

 /* prevent skel_open() from racing skel_disconnect() */
 /*获取bigkernel锁,阻止open()调用*/
 lock_kernel();

 /*从usb主设备结构中获取信息到usb设备结构当中*/
 dev = usb_get_intfdata(interface);
 /*设置usb主设备为NULL*/
 usb_set_intfdata(interface, NULL);

 /* give back our minor */
 /*注销usb设备*/
 usb_deregister_dev(interface, &skel_class);

 /*解除锁*/
 unlock_kernel();

 /* decrement our usage count */
 /*减少usb使用计数,释放usb设备缓冲区*/
 kref_put(&dev->kref, skel_delete);

 /*打印相关信息*/
 info("USB Skeleton #%d now disconnected", minor);
}

 

//定义并初始化usb驱动,只需要初始化这五个成员就可以了
static struct usb_driver skel_driver = {
 .owner = THIS_MODULE,//指向驱动
 .name = "skeleton",//驱动名字
 .id_table = skel_table,//可匹配的USB类型列表
 .probe = skel_probe,//指向USB驱动程序中探测函数的指针
 .disconnect = skel_disconnect,//指向USB驱动中去连接函数的指针
};

 

/*usb模块初始化函数*/
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;
}

 

/*usb模块卸载函数*/
static void __exit usb_skel_exit(void)
{
 /* deregister this driver with the USB subsystem */
 /*注销usb驱动*/
 usb_deregister(&skel_driver);
}

module_init (usb_skel_init);
module_exit (usb_skel_exit);

MODULE_LICENSE("GPL");

你可能感兴趣的:(linux,struct,File,Module,buffer,interface)