Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99940
  • 博文数量: 28
  • 博客积分: 1435
  • 博客等级: 上尉
  • 技术积分: 265
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-26 11:40
文章分类

全部博文(28)

文章存档

2017年(1)

2012年(1)

2011年(6)

2010年(20)

我的朋友

分类:

2010-06-08 13:13:53

  • usb设备状态在usb2.0 spec ch9中描述,在include/linux/usb/ch9.h中定义:

782 enum usb_device_state {
783 /* NOTATTACHED isn't in the USB spec, and this state acts
784 * the same as ATTACHED ... but it's clearer this way.
785 */

786 USB_STATE_NOTATTACHED = 0,
787
788 /* chapter 9 and authentication (wireless) device states */
789 USB_STATE_ATTACHED,
790 USB_STATE_POWERED, /* wired */
791 USB_STATE_RECONNECTING, /* auth */
792 USB_STATE_UNAUTHENTICATED, /* auth */
793 USB_STATE_DEFAULT, /* limited function */
794 USB_STATE_ADDRESS,
795 USB_STATE_CONFIGURED, /* most functions */
796
797 USB_STATE_SUSPENDED
798
799 /* NOTE: there are actually four different SUSPENDED
800 * states, returning to POWERED, DEFAULT, ADDRESS, or
801 * CONFIGURED respectively when SOF tokens flow again.
802 * At this level there's no difference between L1 and L2
803 * suspend states. (L2 being original USB 1.1 suspend.)
804 */

805 };


这些就是一个usb设备可能所处在的状态,从插上->上电->分配地址->...

  • usb hub是一个特殊的usb设备,因此它也有自己的不同于usb_device_status的状态:

112 struct usb_hub_status {
113 __le16 wHubStatus;
114 __le16 wHubChange;
115 } __attribute__ ((packed));
116
117 /*
118 * Hub Status & Hub Change bit masks
119 * See USB 2.0 spec Table 11-19 and Table 11-20
120 * Bits 0 and 1 for wHubStatus and wHubChange
121 * Bits 2 to 15 are reserved for both
122 */

123 #define HUB_STATUS_LOCAL_POWER 0x0001
124 #define HUB_STATUS_OVERCURRENT 0x0002
125 #define HUB_CHANGE_LOCAL_POWER 0x0001
126 #define HUB_CHANGE_OVERCURRENT 0x0002


  • 每个hub有若干个Port,每个port都有自己的状态:

67 struct usb_port_status {
 68 __le16 wPortStatus;
 69 __le16 wPortChange;
 70 } __attribute__ ((packed));
 71
 72 /*
 73 * wPortStatus bit field
 74 * See USB 2.0 spec Table 11-21
 75 */

 76 #define USB_PORT_STAT_CONNECTION 0x0001
 77 #define USB_PORT_STAT_ENABLE 0x0002
 78 #define USB_PORT_STAT_SUSPEND 0x0004
 79 #define USB_PORT_STAT_OVERCURRENT 0x0008
 80 #define USB_PORT_STAT_RESET 0x0010
 81 #define USB_PORT_STAT_L1 0x0020
 82 /* bits 6 to 7 are reserved */
 83 #define USB_PORT_STAT_POWER 0x0100
 84 #define USB_PORT_STAT_LOW_SPEED 0x0200
 85 #define USB_PORT_STAT_HIGH_SPEED 0x0400
 86 #define USB_PORT_STAT_TEST 0x0800
 87 #define USB_PORT_STAT_INDICATOR 0x1000
 88 /* bits 13 to 15 are reserved */
 89
 90 /*
 91 * wPortChange bit field
 92 * See USB 2.0 spec Table 11-22
 93 * Bits 0 to 4 shown, bits 5 to 15 are reserved
 94 */

 95 #define USB_PORT_STAT_C_CONNECTION 0x0001
 96 #define USB_PORT_STAT_C_ENABLE 0x0002
 97 #define USB_PORT_STAT_C_SUSPEND 0x0004
 98 #define USB_PORT_STAT_C_OVERCURRENT 0x0008
 99 #define USB_PORT_STAT_C_RESET 0x0010
100 #define USB_PORT_STAT_C_L1 0x0020
101



102 /*
103 * wHubCharacteristics (masks)
104 * See USB 2.0 spec Table 11-13, offset 3
105 */

106 #define HUB_CHAR_LPSM 0x0003 /* D1 .. D0 */
107 #define HUB_CHAR_COMPOUND 0x0004 /* D2 */
108 #define HUB_CHAR_OCPM 0x0018 /* D4 .. D3 */
109 #define HUB_CHAR_TTTT 0x0060 /* D6 .. D5 */
110 #define HUB_CHAR_PORTIND 0x0080 /* D7 */


这些状态都可以通过发送相应的命令来得到,如:

 325 static int get_hub_status(struct usb_device *hdev,
 326 struct usb_hub_status *data)
 327 {
 328 int i, status = -ETIMEDOUT;
 329
 330 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
 331 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
 332 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
 333 data, sizeof(*data), USB_STS_TIMEOUT);
 334 }
 335 return status;
 336 }


usb_control_msg函数的定义上面有一段注释如下:

 104 /**
 105  * usb_control_msg - Builds a control urb, sends it off and waits for completion
 106  * @dev: pointer to the usb device to send the message to
 107  * @pipe: endpoint "pipe" to send the message to
 108  * @request: USB message request value
 109  * @requesttype: USB message request type value
 110  * @value: USB message value
 111  * @index: USB message index value
 112  * @data: pointer to the data to send
 113  * @size: length in bytes of the data to send
 114  * @timeout: time in msecs to wait for the message to complete before timing
 115  *  out (if 0 the wait is forever)
 116  *
 117  * Context: !in_interrupt ()
 118  *
 119  * This function sends a simple control message to a specified endpoint and
 120  * waits for the message to complete, or timeout.
 121  *
 122  * If successful, it returns the number of bytes transferred, otherwise a
 123  * negative error number.


这是一个阻塞函数,即当函数返回的时候,data中存放的就是我们想要的status了!


同样,如果要获得一个hub port的状态的话也是发送一个类似的命令,如下:

341 static int get_port_status(struct usb_device *hdev, int port1,
 342 struct usb_port_status *data)
 343 {
 344 int i, status = -ETIMEDOUT;
 345
 346 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
 347 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
 348 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
 349 data, sizeof(*data), USB_STS_TIMEOUT);
 350 }
 351 return status;
 352 }


阅读(8320) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-05-09 08:44:21

狗屁啊你,复制些源码下来有屁用啊!