Gadget Framework 代码分析(一) structure
struct usb_request {//表示一个传输的请求,与host端的urb类似
void *buf; //存放的数据
unsigned length; //数据长度
dma_addr_t dma; //数据的DMA地址
unsigned no_interrupt:1;
unsigned zero:1;
unsigned short_not_ok:1;
void (*complete)(struct usb_ep *ep,
struct usb_request *req); //一次request完成的回调函数
void *context;
struct list_head list; //gadget_driver用到
int status;
unsigned actual; //实际传输的数据长度
};
struct usb_ep_ops {//表示端点的操作集
...
int (*queue) (struct usb_ep *ep, struct usb_request *req,
gfp_t gfp_flags); //将一个usb_request提交给endpoint
//是数据传输的关键函数
...
};
struct usb_ep {//代表一个端点
void *driver_data //给gadget driver用
...
const struct usb_ep_ops *ops; //端点的操作集,如上
struct list_head ep_list; //gadget的所有ep的list
...
};
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);
};
struct usb_gadget {//代表一个usb从设备,在UDC(USB Device Controller) Driver中初始化
//他对gadget driver是只读的
const struct usb_gadget_ops *ops; //设备的操作集,如上
struct usb_ep *ep0; //ep0, 处理setup()请求
struct list_head ep_list; //设备支持的ep的list
enum usb_device_speed speed; //当前连接到host的速度
unsigned is_dualspeed:1; //支持full/high speed
//OTG的特性
unsigned is_otg:1; //用到mini-AB接口,所以gadget driver必须提供USB OTG descriptor
unsigned is_a_peripheral:1; //当前角色是A-peripheral,而不是A-host
//以下这三个OTG Device feature flags只有在is_otg:1并且is_a_peripheral:0(device作为B-peripheral)时有效
//他们的值会在以下情况更新:1. USB_REQ_SET_CONFIGURATION的setup请求之前 2.driver的setup调用之前
unsigned b_hnp_enable:1;
unsigned a_hnp_support:1;
unsigned a_alt_hnp_support:1;
const char *name;
struct device dev;
};
struct usb_gadget_driver {//代表一个gadget设备的driver,比如zero.c中的zero_driver
char *function;
enum usb_device_speed speed;
/*
绑定dev与driver,在gadget driver初始化时被
usb_gadget_register_driver调用,绑定之后driver才能处理setup请求
进而进行枚举,枚举时如果gadget->is_otg == 1,gadget driver必须提供OTG descriptor
*/
int (*bind)(struct usb_gadget *);
void (*unbind)(struct usb_gadget *);
/*
usb controller driver要处理部分来自host的usb requests. 包括有set_address, and feature flags for devices,
interfaces, and endpoints (the get_status, set_feature, and clear_feature requests).
(*setup)处理host端发来的request,如处理host端发来的get_descriptor请求时,至少要返回一个device descriptor
和configuration descriptor.
*/
int (*setup)(struct usb_gadget *,
const struct usb_ctrlrequest *);
void (*disconnect)(struct usb_gadget *);
void (*suspend)(struct usb_gadget *);
void (*resume)(struct usb_gadget *);
// FIXME support safe rmmod
struct device_driver driver;
};