一、USB设备状态
在USB 2.0协议中第 9.1 USB Device States 章节规定了USB设备的6种状态,包括:
Attached/Powered/Default/Address/Configured/Suspended
其状态迁移图如下:
在 Linux Kernel ch9.h 文件中用 enum usb_device_state 来标记这几种状态。
// <kernel_dir>/include/uapi/linux/usb/ch9.h
enum usb_device_state {
/* NOTATTACHED isn't in the USB spec, and this state acts
* the same as ATTACHED ... but it's clearer this way.
*/
USB_STATE_NOTATTACHED = 0,
/* chapter 9 and authentication (wireless) device states */
USB_STATE_ATTACHED,
USB_STATE_POWERED, /* wired */
USB_STATE_RECONNECTING, /* auth */
USB_STATE_UNAUTHENTICATED, /* auth */
USB_STATE_DEFAULT, /* limited function */
USB_STATE_ADDRESS,
USB_STATE_CONFIGURED, /* most functions */
USB_STATE_SUSPENDED
/* NOTE: there are actually four different SUSPENDED
* states, returning to POWERED, DEFAULT, ADDRESS, or
* CONFIGURED respectively when SOF tokens flow again.
* At this level there's no difference between L1 and L2
* suspend states. (L2 being original USB 1.1 suspend.)
*/
};
二、状态设置函数usb_gadget_set_state()
// <kernel_dir>/drivers/usb/gadget/udc/udc-core.c
void usb_gadget_set_state(struct usb_gadget *gadget,
enum usb_device_state state)
{
gadget->state = state;
schedule_work(&gadget->work);
}
EXPORT_SYMBOL_GPL(usb_gadget_set_state);
// <kernel_dir>/include/linux/usb/gadget.h
extern void usb_gadget_set_state(struct usb_gadget *gadget, enum usb_device_state state);
在 udc-core.c 文件中,会去定义usb_gadget_set_state()函数,将状态state的值赋值给gadget->state。其中struct usb_gadget是用来标记一个USB设备的信息。此时USB设备的状态就可以确定了。之后启动工作队列schedule_work(&gadget->work);将状态信息给到sysfs。
在USB的枚举阶段,会根据USB所处的状态调用usb_gadget_set_state()去设置USB设备的状态。
比如说在USB设备的枚举阶段,在composite_setup()函数中USB设备接收到了USB Host发过来的USB_REQ_SET_CONFIGURATION命令后调用set_config()设置相应的配置,这之后就会调用usb_gadget_set_state()去设置为USB_STATE_CONFIGURED状态。
三、usb_gadget_state_work()
// <kernel_dir>/drivers/usb/gadget/udc/udc-core.c
* usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
* @parent: the parent device to this udc. Usually the controller driver's
* device.
* @gadget: the gadget to be added to the list.
* @release: a gadget release function.
*
* Returns zero on success, negative errno otherwise.
*/
int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
void (*release)(struct device *dev))
在usb_add_gadget_udc_release()中会去绑定 gadget->work 到 usb_gadget_state_work() 函数。
static void usb_gadget_state_work(struct work_struct *work)
{
struct usb_gadget *gadget = work_to_gadget(work);
struct usb_udc *udc = gadget->udc;
if (udc)
sysfs_notify(&udc->dev.kobj, NULL, "state");
}
这个函数主要目的就是将当前的 state 信息写入到 sysfs 中去。这个信息可以cat出来。
#cat /sys/devices/platform/xxx_udc/state
路径不完全是这个,但是在 /sys/devices 目录下会有对应udc控制器 xxx_udc 的状态节点。不仅包含状态的节点,还包含其他的信息。
-r--r--r-- 0 0 4096 2017-05-01 16:17 a_alt_hnp_support
-r--r--r-- 0 0 4096 2017-05-01 16:17 a_hnp_support
-r--r--r-- 0 0 4096 2017-05-01 16:17 b_hnp_enable
-r--r--r-- 0 0 4096 2017-05-01 16:17 current_speed
lrwxrwxrwx 0 0 2017-05-01 16:17 device -> ../../../panasonic_udc.1
-r--r--r-- 0 0 4096 2017-05-01 16:17 is_a_peripheral
-r--r--r-- 0 0 4096 2017-05-01 16:17 is_otg
-r--r--r-- 0 0 4096 2017-05-01 16:17 maximum_speed
drwxr-xr-x 0 0 2017-05-01 16:17 power
--w------- 0 0 4096 2017-05-01 16:17 soft_connect
--w------- 0 0 4096 2017-05-01 16:17 srp
-r--r--r-- 0 0 4096 2017-05-01 16:17 state
lrwxrwxrwx 0 0 2017-05-01 16:17 subsystem -> ../../../../../class/udc
-rw-r--r-- 0 0 4096 2017-05-01 16:17 uevent
这里面的信息其实就是一个USB设备的信息,用 struct usb_gadget 来描述。关于USB Gadget的内容将在后续的文章中整理出来。