1、前言
在Linux-USB驱动笔记(四)–USB整体框架中有説到设备侧的设备控制器(UDC),下面我们来具体看一下。
2、设备控制器(UDC)
USB设备控制器(UDC)驱动指的是作为其他USB主机控制器的USB硬件设备上的底层控制器驱动。该硬件和驱动负责将一个USB设备依附于一个USB主机控制器上。
UDC驱动位于/drivers/usb/gadget目录下。
2.1、usb_gadget – USB从机设备
struct usb_gadget {struct work_struct work; struct usb_udc *udc; /* readonly to gadget driver */const struct usb_gadget_ops *ops; struct usb_ep *ep0; struct list_head ep_list;enum usb_device_speed speed; enum usb_device_speed max_speed; enum usb_device_state state; const char *name;struct device dev;unsigned isoch_delay; unsigned out_epnum; unsigned in_epnum;unsigned mA; struct usb_otg_caps *otg_caps; //标志是否支持某些功能unsigned sg_supported:1;unsigned is_otg:1;unsigned is_a_peripheral:1;unsigned b_hnp_enable:1;unsigned a_hnp_support:1;unsigned a_alt_hnp_support:1;unsigned hnp_polling_support:1;unsigned host_request_flag:1;unsigned quirk_ep_out_aligned_size:1;unsigned quirk_altset_not_supp:1;unsigned quirk_stall_not_supp:1;unsigned quirk_zlp_not_supp:1;unsigned quirk_avoids_skb_reserve:1;unsigned is_selfpowered:1;unsigned deactivated:1;unsigned connected:1;unsigned lpm_capable:1;
};
字段 | 含义 |
---|---|
struct work_struct work; | sysfs_notify()使用的工作队列 |
struct usb_udc *udc; | usb设备控制器 |
const struct usb_gadget_ops *ops; | UDC操作函数 |
struct usb_ep *ep0; | 端点0,用于驱动设置请求应答 |
struct list_head ep_list; | 其他端口列表 |
enum usb_device_speed speed; | 当前连接的USB主机速率 |
enum usb_device_speed max_speed; | UDC能处理的最大速率 |
enum usb_device_state state; | UDC状态 |
const char *name; | 名称 |
struct device dev; | 设备 |
unsigned isoch_delay; | 设置等待请求 |
unsigned out_epnum; | 最近使用的输出端点号 |
unsigned in_epnum; | 最近使用的输入端点号 |
unsigned mA; | 最近设置的mA值 |
struct usb_otg_caps *otg_caps; | OTG功能 |
2.2、usb_gadget_ops – UDC操作函数
struct usb_gadget_ops {int (*get_frame)(struct usb_gadget *);int (*wakeup)(struct usb_gadget *);int (*set_selfpowered) (struct usb_gadget *, int is_selfpowered);int (*vbus_session) (struct usb_gadget *, int is_active);int (*vbus_draw) (struct usb_gadget *, unsigned mA);int (*pullup) (struct usb_gadget *, int is_on);int (*ioctl)(struct usb_gadget *,unsigned code, unsigned long param);void (*get_config_params)(struct usb_dcd_config_params *);int (*udc_start)(struct usb_gadget *,struct usb_gadget_driver *);int (*udc_stop)(struct usb_gadget *);void (*udc_set_speed)(struct usb_gadget *, enum usb_device_speed);struct usb_ep *(*match_ep)(struct usb_gadget *,struct usb_endpoint_descriptor *,struct usb_ss_ep_comp_descriptor *);
};
以面的操作函数不涉及端点操作。
2.3、usb_ep – 端点
struct usb_ep {void *driver_data;const char *name;const struct usb_ep_ops *ops; struct list_head ep_list;struct usb_ep_caps caps;bool claimed;bool enabled;unsigned maxpacket:16;unsigned maxpacket_limit:16;unsigned max_streams:16;unsigned mult:2;unsigned maxburst:5;u8 address;const struct usb_endpoint_descriptor *desc; const struct usb_ss_ep_comp_descriptor *comp_desc;
};
重要字段及其含义如下:
字段 | 含义 |
---|---|
const struct usb_ep_ops *ops | 端点操作函数集 |
const struct usb_endpoint_descriptor *desc | 端点描述符 |
2.4、usb_ep_ops – 端点操作函数
struct usb_ep_ops {int (*enable) (struct usb_ep *ep,const struct usb_endpoint_descriptor *desc);int (*disable) (struct usb_ep *ep);void (*dispose) (struct usb_ep *ep);struct usb_request *(*alloc_request) (struct usb_ep *ep,gfp_t gfp_flags);void (*free_request) (struct usb_ep *ep, struct usb_request *req);int (*queue) (struct usb_ep *ep, struct usb_request *req,gfp_t gfp_flags);int (*dequeue) (struct usb_ep *ep, struct usb_request *req);int (*set_halt) (struct usb_ep *ep, int value);int (*set_wedge) (struct usb_ep *ep);int (*fifo_status) (struct usb_ep *ep);void (*fifo_flush) (struct usb_ep *ep);
};
3、总结如下
- 1、usb_gadget表示一个从机设备(UDC + 端点),它包含一些端点(包括端点0和其他端点)。所以它有一个端点列表成员ep_list, 初始化过程中会将所有需要的端点放到该链表中。
- 2、 usb_gadget_ops主要用来操作UDC。
- 3、 usb_ep表示一个端点,每个端点都有自己的操作函数usb_ep_ops。
- 4、 usb_ep_ops主要用来操作端点。