硬件原理图:
笔记:
USB总线驱动:(内核实现)
UHCI: intel ,USB1.1/2.0 (低速,全速)
OHCI: microsoft ,USB1.1/2.0 (低速,全速)
EHCI: 高速
usb总线驱动干了什么:
如何找到对应usb总线驱动程序所在文件?
在开发板上插拔usb设备:
插上:
usb 1-1.2: new full-speed USB device number 5 using ci_hdrc
input: YICHIP Wireless Device as /devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.2/1-1.2:1.0/0003:3151:3020.0001/input/input2
hid-generic 0003:3151:3020.0001: input: USB HID v2.00 Keyboard [YICHIP Wireless Device] on usb-ci_hdrc.1-1.2/input0
input: YICHIP Wireless Device as /devices/platform/soc/2100000.aips-bus/2184200.usb/ci_hdrc.1/usb1/1-1/1-1.2/1-1.2:1.1/0003:3151:3020.0002/input/input3
hid-generic 0003:3151:3020.0002: input: USB HID v2.00 Mouse [YICHIP Wireless Device] on usb-ci_hdrc.1-1.2/input1
拔出:
usb 1-1.2: USB disconnect, device number 5
根据关键词在内核drivers下搜索:
**grep “USB device number %d using” * -nr **
或者直接在sourceinsight下搜,最终确定在**/drivers/usb/core/hub.c**中
usb总线驱动调用链:
hub_probe:中
INIT_WORK(&hub->events, hub_event);说明hub_event是一个工作队列,它被绑定到&hub->event这个工作项中,那么又在哪里注册这个工作项呢?
hub_event:
port_event:
hub_port_connect_change:
hub_port_connect:
hub_port_init://插上时打印信息
hub_irq:
kick_hub_wq:
queue_work(hub_wq, &hub->events):
而hub_irq又是怎么触发的呢?那得看USB主从设备连接的原理图了。
没有接usb从设备时,D-/D+为低电平,接上之后,变为高电平。那么主设备就能判断是否接了。
仔细看看hub_port_connect中干了什么:
hub_port_connect:
->hub_port_init
-> hub_set_address(udev, devnum);//设置从设备地址
获取描述符
->usb_get_device_descriptor(udev, 8);//一开始不知道这个设备描述符一次最大多少个字节
->usb_get_bos_descriptor(udev);
->usb_new_device
->err = usb_enumerate_device(udev); /* Read descriptors */读出获取的描述符
->err = device_add(&udev->dev);//在将这个从设备加入到usb bus中
usb_bus在中定义linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek\drivers\usb\core\driver.c
struct bus_type usb_bus_type = {
.name = "usb",
.match = usb_device_match,
.uevent = usb_uevent,
};
USB的各种描述符:(Ch9.h下定义)
设备描述符:
/* USB_DT_DEVICE: Device descriptor */
struct usb_device_descriptor {
__u8 bLength;
__u8 bDescriptorType;
__le16 bcdUSB;
__u8 bDeviceClass;
__u8 bDeviceSubClass;
__u8 bDeviceProtocol;
__u8 bMaxPacketSize0;
__le16 idVendor;
__le16 idProduct;
__le16 bcdDevice;
__u8 iManufacturer;
__u8 iProduct;
__u8 iSerialNumber;
__u8 bNumConfigurations;//这个从设备有多少种配置
} __attribute__ ((packed));
配置描述符:
struct usb_config_descriptor {
__u8 bLength;
__u8 bDescriptorType;
__le16 wTotalLength;
__u8 bNumInterfaces;//这种配置有多少个接口(一个接口表示一个逻辑设备)
__u8 bConfigurationValue;
__u8 iConfiguration;
__u8 bmAttributes;
__u8 bMaxPower;
} __attribute__ ((packed));
接口描述符:
struct usb_interface_descriptor {
__u8 bLength;
__u8 bDescriptorType;
__u8 bInterfaceNumber;
__u8 bAlternateSetting;
__u8 bNumEndpoints;//这个接口下面有多少端点
__u8 bInterfaceClass;
__u8 bInterfaceSubClass;
__u8 bInterfaceProtocol;
__u8 iInterface;
} __attribute__ ((packed));
端点描述符:
/* USB_DT_ENDPOINT: Endpoint descriptor */
struct usb_endpoint_descriptor {
__u8 bLength;
__u8 bDescriptorType;
__u8 bEndpointAddress;
__u8 bmAttributes;//描述端点属性,比如是输入端点还是输出端点
__le16 wMaxPacketSize;
__u8 bInterval;
/* NOTE: these two are _only_ in audio endpoints. */
/* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */
__u8 bRefresh;
__u8 bSynchAddress;
} __attribute__ ((packed));
可以看出usb控制器驱动,最主要的作用还是从硬件上识别到插入的从设备,并且获取从设备的各种描述符,然后在usb_bus上注册这个device,而对应在usb_bus上的driver则是我们需要完成的驱动。
usb_bus_type提供的match:
static int usb_device_match(struct device *dev, struct device_driver *drv)
{
/* devices and interfaces are handled separately */
if (is_usb_device(dev)) {
/* interface drivers never match devices */
if (!is_usb_device_driver(drv))
return 0;
/* TODO: Add real matching code */
return 1;
} else if (is_usb_interface(dev)) {
struct usb_interface *intf;
struct usb_driver *usb_drv;
const struct usb_device_id *id;
/* device drivers never match interfaces */
if (is_usb_device_driver(drv))
return 0;
intf = to_usb_interface(dev);
usb_drv = to_usb_driver(drv);
id = usb_match_id(intf, usb_drv->id_table);
if (id)
return 1;
id = usb_match_dynamic_id(intf, usb_drv);
if (id)
return 1;
}
return 0;
}
如果匹配到了就执行usb_driver的probe:
usb_driver编写:
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/usb/input.h>
#include <linux/hid.h>
static struct *input_dev usb_key;
int len;
static char *usb_buf;//接收从机缓冲区
static struct urb *uk_urb;//usb数据请求结构体
static const struct usb_device_id my_usb_id_table[] = {
//{USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT, USB_INTERFACE_PROTOCOL_MOUSE)},
{USB_DEVICE(0x3152, 0x3020) },//满足厂家id,设备id
{ } /* Terminating entry */
};
static void usbmouse_as_key_irq(struct urb *urb)
{
//根据从机发过来的数据执行相应的操作(如:输入子系统发送信号)
}
int my_usb_probe(struct usb_interface *intf,const struct usb_device_id *id)//intf表示usb接口也即一个usb逻辑设备
{
printk("found usbmouse!\n");
//首先获取根据接口获取usb_device
struct usb_device *dev = interface_to_usbdev(intf);
struct usb_host_interface *interface;//接口
struct usb_endpoint_descriptor *endpoint;//端点
int pipe;//端点上的管道
//获取接口与端点与管道
interface = intf->cur_altsetting;
endpoint = &interface->endpoint[0].desc;
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
//申请输入子系统
usb_key = input_allocate_device(void);
//初始化usb_key的输入子系统
set_bit(EV_KEY, usb_key->evbit);
set_bit(EV_REP, usb_key->evbit);
set_bit(KEY_L, usb_key->keybit);
set_bit(KEY_S, usb_key->keybit);
set_bit(KEY_ENTER, usb_key->keybit);
//注册输入子系统
input_register_device(&usb_key);
//申请接收缓冲区
len = endpoint->wMaxPacketSize;
usb_buf = usb_buffer_alloc(dev, len, GFP_ATOMIC, &usb_buf_phys);//usb_buf_phys是值缓冲区对应的物理地址,因为usb数据传输是利用DMA的
//申请usb数据请求结构体
uk_urb = usb_alloc_urb(0, GFP_KERNEL);
//填充urb
usb_fill_int_urb(uk_urb, dev, pipe, usb_buf, len, usbmouse_as_key_irq, NULL, endpoint->bInterval);//其中usbmouse_as_key_irq并不是真实的中断,而是主机会在一定时间内去主动查询而执行的函数。因为usb从机并不具有中断的能力
uk_urb->transfer_dma = usb_buf_phys;
uk_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
//提交urb
usb_submit_urb(uk_urb, GFP_KERNEL);
return 0;
}
void my_usb_disconnect(struct usb_interface *intf)
{
printk("usb disconnected!\n");
struct usb_device *dev = interface_to_usbdev(intf);
//printk("disconnect usbmouse!\n");
usb_kill_urb(uk_urb);
usb_free_urb(uk_urb);
usb_buffer_free(dev, len, usb_buf, usb_buf_phys);
input_unregister_device(uk_dev);
input_free_device(uk_dev);
}
static struct usb_driver my_usb_driver = {
.name = "my_usb_driver",
.probe = my_usb_probe,
.disconnect = my_usb_disconnect,
.id_table = my_usb_id_table,
};
static int my_usb_init(void)
{
//注册usb_driver
int reval = usb_register(&my_usb_driver);
return 0;
}
static void my_usb_exit(void)
{
usb_deregister(&my_usb_driver);
}
module_init(my_usb_init);
module_exit(my_usb_exit);
MODULE_LICENSE("GPL");