Linux驱动开发:USB驱动之usb

在学习了这么些天的驱动之后,个人觉得驱动就是个架构的问题,只要把架构弄清楚了

然后往里面添砖加瓦就可以了,所以似乎看起来不是太困难,但也许是是我经验不足吧,这只能算是个人浅见了

这两天在学习USB驱动开发,奇怪的是老师居然不讲USB的代码,让人不理解,后来在网上找资料才发现原来内核已经给我们准备了一个usb_skel的代码向我们介绍几本的USB驱动的架构,于是自己分析了一下代码,画了一个我认为的代码架构(比较难看),写了一些注释

相关阅读:

Linux驱动开发:网络设备之虚拟网卡

Linux驱动开发:Nand Flash时序图分析

Linux驱动开发:网络设备之DM9000驱动架构分析

废话不多说啦,直接上图上代码,请高手们批评指正!

Linux驱动开发:USB驱动之usb

/*

* USB Skeleton driver - 2.2

*

* Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)

*

* This program is free software; you can redistribute it and/or

* modify it under the terms of the GNU General Public License as

* published by the Free Software Foundation, version 2.

*

* This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c

* but has been rewritten to be easier to read and use.

*

*/

#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 <asm/uaccess.h>

#include <linux/usb.h>

#include <linux/mutex.h>

/* Define these values to match your devices */

#define USB_SKEL_VENDOR_ID 0xfff0          //厂商ID

#define USB_SKEL_PRODUCT_ID 0xfff0      //产品ID

/* table of devices that work with this driver */

//驱动支持的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);

/* to prevent a race between open and disconnect */

static DEFINE_MUTEX(skel_open_lock);

/* Get a minor range for your devices from the usb maintainer */

#define USB_SKEL_MINOR_BASE 192      //USB主设备号

/* our private defines. if this grows any larger, use your own .h file */

#define MAX_TRANSFER (PAGE_SIZE - 512)

/* MAX_TRANSFER is chosen so that the VM is not stressed by allocations > PAGE_SIZE and the number of packets in a page is an integer 512 is the largest possible packet on EHCI */

#define WRITES_IN_FLIGHT 8

/* arbitrarily chosen */

//8、usb_skel结构体可以被看作一个私有数据结构体,应该根据具体的设备量身定制

struct usb_skel {

struct usb_device *udev;                    //该设备的usb_device指针

struct usb_interface *interface;         //该设备的usb_interface指针

struct semaphore limit_sem;            //限制进程写的数据量

unsigned char  *bulk_in_buffer;       //接收数据的缓冲区

size_t bulk_in_size;                            //接收缓冲区大小

__u8 bulk_in_endpointAddr;            //批量IN端点的地址

__u8 bulk_out_endpointAddr;         //批量OUT端点的地址

struct kref kref;                                    //sturct kref作为内核中最基本的引用计数而存在

struct mutex io_mutex;                     //同步的IO互斥锁,保证

};

//5、声明一个USB骨架驱动结构体对象

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

}

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);   //获取次设备号

mutex_lock(&skel_open_lock);   //上锁

interface = usb_find_interface(&skel_driver, subminor);   //获得接口数据

if (!interface) {

mutex_unlock(&skel_open_lock);  //解锁

err ("%s - error, can't find device for minor %d",  __FUNCTION__, subminor);

retval = -ENODEV;

goto exit;

}

dev = usb_get_intfdata(interface);

if (!dev) {

mutex_unlock(&skel_open_lock);

retval = -ENODEV;

goto exit;

}

/* increment our usage count for the device */

kref_get(&dev->kref);

/* now we can drop the lock */

mutex_unlock(&skel_open_lock);

/* prevent the device from being autosuspended */

retval = usb_autopm_get_interface(interface);

if (retval) {

kref_put(&dev->kref, skel_delete);

goto exit;

}

/* 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;

dev = (struct usb_skel *)file->private_data;

if (dev == NULL)

return -ENODEV;

/* allow the device to be autosuspended */

mutex_lock(&dev->io_mutex);

if (dev->interface)

usb_autopm_put_interface(dev->interface);

mutex_unlock(&dev->io_mutex);

/* decrement the count on our device */

kref_put(&dev->kref, skel_delete);

return 0;

}

static ssize_t skel_read(struct file *file, char *buffer, size_t count, loff_t *ppos)

{

struct usb_skel *dev;

int retval;

int bytes_read;

dev = (struct usb_skel *)file->private_data;

mutex_lock(&dev->io_mutex);

if (!dev->interface) { /* disconnect() was called */

retval = -ENODEV;

goto exit;

}

/* 进行阻塞的批量读以从设备获取数据 */

retval = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),  dev->bulk_in_buffer, min(dev->bulk_in_size, count), &bytes_read, 10000);

/* 如果读成功,复制到用户空间 */

if (!retval) {

if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))

retval = -EFAULT;

else

retval = bytes_read;

}

exit:

mutex_unlock(&dev->io_mutex);

return retval;

}

/*当urb被成功传输到USB设备之后,urb回调函数将被USB核心调用,在我们的例子中,我们初始化urb,使它指向skel_write_bulk_callback函数*/

static void skel_write_bulk_callback(struct urb *urb)

{

struct usb_skel *dev;

dev = (struct usb_skel *)urb->context;

/* sync/async unlink faults aren't errors */

if (urb->status && !(urb->status == -ENOENT || urb->status == -ECONNRESET || urb->status == -ESHUTDOWN)) {

err("%s - nonzero write bulk status received: %d",

__FUNCTION__, urb->status);

}

/ * free up our allocated buffer */

usb_buffer_free(urb->dev, urb->transfer_buffer_length,

urb->transfer_buffer, urb->transfer_dma);

up(&dev->limit_sem);

}

static ssize_t skel_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)

{

struct usb_skel *dev;

int retval = 0;

struct urb *urb = NULL;

char *buf = NULL;

size_t writesize = min(count, (size_t)MAX_TRANSFER);

dev = (struct usb_skel *)file->private_data;

/* verify that we actually have some data to write */

if (count == 0)

goto exit;

/* limit the number of URBs in flight to stop a user from using up all RAM */

if (down_interruptible(&dev->limit_sem)) {

retval = -ERESTARTSYS;

goto exit;

}

/* 创建一个urb,并且给它分配一个缓存*/
        urb = usb_alloc_urb(0, GFP_KERNEL);

if (!urb) {

retval = -ENOMEM;

goto error;

}

/*当urb被成功分配后,还要创建一个DMA缓冲区来以高效的方式发送数据到设备,传递给驱动程序的数据要复制到这块缓冲中去*/

buf = usb_buffer_alloc(dev->udev, writesize, GFP_KERNEL, &urb->transfer_dma);

if (!buf) {

retval = -ENOMEM;

goto error;

}

if (copy_from_user(buf, user_buffer, writesize)) {

retval = -EFAULT;

goto error;

}

/* this lock makes sure we don't submit URBs to gone devices */

mutex_lock(&dev->io_mutex);

if (!dev->interface) {

/* disconnect() was called */

mutex_unlock(&dev->io_mutex);

retval = -ENODEV;

goto error;

}

/*当数据从用户空间正确复制到局部缓冲区后,urb必须在可以被提交给USB核心之前被正确初始化*/

usb_fill_bulk_urb(urb, dev->udev,  usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),  buf, writesize, skel_write_bulk_callback, dev);

urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

/* 把数据从批量OUT端口发出 */

retval = usb_submit_urb(urb, GFP_KERNEL);

mutex_unlock(&dev->io_mutex);

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

return writesize;

error:

if (urb) {

usb_buffer_free(dev->udev, writesize, buf, urb->transfer_dma);

usb_free_urb(urb);

}

up(&dev->limit_sem);

exit:

return retval;

}

//字符设备的file_operations结构体,这个结构体中的成员实现

static const 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 the driver core

*/

static struct usb_class_driver skel_class = {

.name = "skel%d",

.fops  = &skel_fops,

.minor_base  = USB_SKEL_MINOR_BASE,

};

//7、探测函数skel_probe

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

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