1 #ifndef __USB_HOST_HUB_H__
2 #define __USB_HOST_HUB_H__
3 /*
4  * Hub protocol and driver data structures.
5  *
6  * Some of these are known to the "virtual root hub" code
7  * in host controller drivers.
8  */
9 
10 
11 #include <hal_osal.h>
12 #include "usb_host_common.h"
13 
14 /*
15  * Hub request types
16  */
17 
18 #define USB_RT_HUB  (USB_TYPE_CLASS | USB_RECIP_DEVICE)
19 #define USB_RT_PORT (USB_TYPE_CLASS | USB_RECIP_OTHER)
20 
21 /*
22  * Hub class requests
23  * See USB 2.0 spec Table 11-16
24  */
25 #define HUB_CLEAR_TT_BUFFER             8
26 #define HUB_RESET_TT                    9
27 #define HUB_GET_TT_STATE                10
28 #define HUB_STOP_TT                     11
29 
30 /*
31  * Hub Class feature numbers
32  * See USB 2.0 spec Table 11-17
33  */
34 #define C_HUB_LOCAL_POWER               0
35 #define C_HUB_OVER_CURRENT              1
36 
37 /*
38  * Port feature numbers
39  * See USB 2.0 spec Table 11-17
40  */
41 #define USB_PORT_FEAT_CONNECTION        0
42 #define USB_PORT_FEAT_ENABLE            1
43 #define USB_PORT_FEAT_SUSPEND           2
44 #define USB_PORT_FEAT_OVER_CURRENT      3
45 #define USB_PORT_FEAT_RESET             4
46 #define USB_PORT_FEAT_POWER             8
47 #define USB_PORT_FEAT_LOWSPEED          9
48 #define USB_PORT_FEAT_HIGHSPEED         10
49 #define USB_PORT_FEAT_C_CONNECTION      16
50 #define USB_PORT_FEAT_C_ENABLE          17
51 #define USB_PORT_FEAT_C_SUSPEND         18
52 #define USB_PORT_FEAT_C_OVER_CURRENT    19
53 #define USB_PORT_FEAT_C_RESET           20
54 #define USB_PORT_FEAT_TEST              21
55 #define USB_PORT_FEAT_INDICATOR         22
56 
57 /*
58  * wPortStatus bit field
59  * See USB 2.0 spec Table 11-21
60  */
61 #define USB_PORT_STAT_CONNECTION        0x0001  //当前的连接状态
62 #define USB_PORT_STAT_ENABLE            0x0002
63 #define USB_PORT_STAT_SUSPEND           0x0004
64 #define USB_PORT_STAT_OVERCURRENT       0x0008
65 #define USB_PORT_STAT_RESET             0x0010
66 
67 /* bits 5 to 7 are reserved */
68 #define USB_PORT_STAT_POWER             0x0100
69 #define USB_PORT_STAT_LOW_SPEED         0x0200
70 #define USB_PORT_STAT_HIGH_SPEED        0x0400
71 #define USB_PORT_STAT_TEST              0x0800
72 #define USB_PORT_STAT_INDICATOR         0x1000
73 
74 /* bits 13 to 15 are reserved */
75 
76 /*
77  * wPortChange bit field
78  * See USB 2.0 spec Table 11-22
79  * Bits 0 to 4 shown, bits 5 to 15 are reserved
80  */
81 #define USB_PORT_STAT_C_CONNECTION      0x0001  //连接状态变化
82 #define USB_PORT_STAT_C_ENABLE          0x0002
83 #define USB_PORT_STAT_C_SUSPEND         0x0004
84 #define USB_PORT_STAT_C_OVERCURRENT     0x0008
85 #define USB_PORT_STAT_C_RESET           0x0010
86 
87 /*
88  * wHubCharacteristics (masks)
89  * See USB 2.0 spec Table 11-13, offset 3
90  */
91 #define HUB_CHAR_LPSM                   0x0003 /* D1 .. D0 */
92 #define HUB_CHAR_COMPOUND               0x0004 /* D2       */
93 #define HUB_CHAR_OCPM                   0x0018 /* D4 .. D3 */
94 #define HUB_CHAR_TTTT                   0x0060 /* D6 .. D5 */
95 #define HUB_CHAR_PORTIND                0x0080 /* D7       */
96 
97 
98 
99 /*
100  * Hub Status & Hub Change bit masks
101  * See USB 2.0 spec Table 11-19 and Table 11-20
102  * Bits 0 and 1 for wHubStatus and wHubChange
103  * Bits 2 to 15 are reserved for both
104  */
105 #define HUB_STATUS_LOCAL_POWER          0x0001
106 #define HUB_STATUS_OVERCURRENT          0x0002
107 
108 #define HUB_CHANGE_LOCAL_POWER          0x0001
109 #define HUB_CHANGE_OVERCURRENT          0x0002
110 
111 
112 /*
113  * Hub descriptor
114  * See USB 2.0 spec Table 11-13
115  */
116 
117 #define USB_DT_HUB                      (USB_TYPE_CLASS | 0x09)
118 #define USB_DT_HUB_NONVAR_SIZE          7
119 
120 
121 /*
122  * Hub Status and Hub Change results
123  * See USB 2.0 spec Table 11-19 and Table 11-20
124  */
125 struct __attribute__((packed)) usb_port_status
126 {
127     __u16 wPortStatus;
128     __u16 wPortChange;
129 } ;
130 
131 
132 struct __attribute__((packed)) usb_hub_status
133 {
134     __u16 wHubStatus;
135     __u16 wHubChange;
136 };
137 
138 
139 struct __attribute__((packed)) usb_hub_descriptor
140 {
141     __u8  bDescLength;
142     __u8  bDescriptorType;
143     __u8  bNbrPorts;
144     __u16 wHubCharacteristics;
145     __u8  bPwrOn2PwrGood;
146     __u8  bHubContrCurrent;
147 
148     /* add 1 bit for hub status change; round to bytes */
149     __u8  DeviceRemovable[(USB_MAXCHILDREN + 1 + 7) / 8];
150     __u8  PortPwrCtrlMask[(USB_MAXCHILDREN + 1 + 7) / 8];
151 } ;
152 
153 
154 /* port indicator status selectors, tables 11-7 and 11-25 */
155 #define HUB_LED_AUTO            0
156 #define HUB_LED_AMBER           1
157 #define HUB_LED_GREEN           2
158 #define HUB_LED_OFF             3
159 
160 enum hub_led_mode
161 {
162     INDICATOR_AUTO = 0,
163     INDICATOR_CYCLE,
164     /* software blinks for attention:  software, hardware, reserved */
165     INDICATOR_GREEN_BLINK, INDICATOR_GREEN_BLINK_OFF,
166     INDICATOR_AMBER_BLINK, INDICATOR_AMBER_BLINK_OFF,
167     INDICATOR_ALT_BLINK, INDICATOR_ALT_BLINK_OFF
168 };
169 
170 struct usb_host_virt_dev;
171 
172 /*
173  * As of USB 2.0, full/low speed devices are segregated into trees.
174  * One type grows from USB 1.1 host controllers (OHCI, UHCI etc).
175  * The other type grows from high speed hubs when they connect to
176  * full/low speed devices using "Transaction Translators" (TTs).
177  *
178  * TTs should only be known to the hub driver, and high speed bus
179  * drivers (only EHCI for now).  They affect periodic scheduling and
180  * sometimes control/bulk error recovery.
181  */
182 struct usb_tt
183 {
184     struct usb_host_virt_dev    *hub;   /* upstream highspeed hub */
185     int multi;                          /* true means one TT per port */
186 
187     /* for control/bulk error recovery (CLEAR_TT_BUFFER) */
188     __u32 lock;
189     struct list_head clear_list;        /* of usb_tt_clear */
190     //struct work_struct kevent;
191 };
192 
193 struct usb_tt_clear
194 {
195     struct list_head clear_list;
196     unsigned tt;
197     u16 devinfo;
198 };
199 
200 /// extern void usb_hub_tt_clear_buffer (struct usb_host_virt_dev *dev, int pipe);
201 #define STATUS_URB_BUFF_LEN         8
202 struct usb_hub
203 {
204     //struct device     *intfdev;   /* the "interface" device */
205     struct usb_host_virt_dev *hdev;
206     struct urb *urb;                //就是用来查询status的urb//for interrupt polling pipe
207 
208     /* buffer for urb ... with extra space in case of babble */
209     //char (*buffer)[8];        //是status urb和 hub交互status的中介
210     char status_urb_ubff[STATUS_URB_BUFF_LEN];
211     u32 buffer_dma;                 /* DMA address for buffer */
212 
213     union
214     {
215         struct usb_hub_status   hub;
216         struct usb_port_status  port;
217     } *status;                      /* buffer for status reports    */
218 
219     int error;                      /* last reported error          */
220     int nerrors;                    /* track consecutive errors     */
221 
222     //  struct list_head    event_list; /* hubs w/data or errs ready    */
223     u32 event_bits[1];              /* status change bitmask        */
224     u32 change_bits[1];             /* ports with logical connect status change */
225     u32 busy_bits[1];               /* ports being reset            */
226 
227     struct usb_hub_descriptor *descriptor;  /* class descriptor         */
228     struct usb_tt tt;                       /* Transaction Translator   */
229 
230     u8 power_budget;                        /* in 2mA units; or zero    */
231 
232     unsigned quiescing: 1;
233     unsigned activating: 1;
234     unsigned resume_root_hub: 1;
235 
236     unsigned has_indicators: 1;
237     enum hub_led_mode indicator[USB_MAXCHILDREN];
238     //struct work_struct    leds;
239 
240     //USB_OS_KERNEL_EVENT *HubStatusSemi;     /* hub status thread的睡眠和唤醒 */
241     //USB_OS_KERNEL_EVENT *notify_complete;   /* 同步thread创建/删除           */
242     hal_sem_t   HubStatusSemi;     /* hub status thread的睡眠和唤醒 */
243     hal_sem_t   notify_complete;   /* 同步thread创建/删除           */
244 
245     rt_thread_t thread;
246     __u32 HubStatusThrdId;                  /* hub线程ID                     */
247     __u8 HubRemove;                         /* flag. Hub是否拔出             */
248     __u32 hub_req_busy;                     /* 正在发送urb请求               */
249 };
250 
251 
252 #endif
253 
254 
255 
256 
257 
258