1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58#include <linux/slab.h>
59#include <linux/export.h>
60#include "wusbhc.h"
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85static int wusbhc_rh_port_reset(struct wusbhc *wusbhc, u8 port_idx)
86{
87 int result = 0;
88 struct wusb_port *port = wusb_port_by_idx(wusbhc, port_idx);
89 struct wusb_dev *wusb_dev = port->wusb_dev;
90
91 if (wusb_dev == NULL)
92 return -ENOTCONN;
93
94 port->status |= USB_PORT_STAT_RESET;
95 port->change |= USB_PORT_STAT_C_RESET;
96
97 if (wusb_dev->addr & WUSB_DEV_ADDR_UNAUTH)
98 result = 0;
99 else
100 result = wusb_dev_update_address(wusbhc, wusb_dev);
101
102 port->status &= ~USB_PORT_STAT_RESET;
103 port->status |= USB_PORT_STAT_ENABLE;
104 port->change |= USB_PORT_STAT_C_RESET | USB_PORT_STAT_C_ENABLE;
105
106 return result;
107}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127int wusbhc_rh_status_data(struct usb_hcd *usb_hcd, char *_buf)
128{
129 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
130 size_t cnt, size, bits_set = 0;
131
132
133
134 size = DIV_ROUND_UP(wusbhc->ports_max + 1, 8);
135
136
137 memset(_buf, 0, size);
138
139 for (cnt = 0; cnt < wusbhc->ports_max; cnt++) {
140
141 if (wusb_port_by_idx(wusbhc, cnt)->change) {
142 const int bitpos = cnt+1;
143
144 _buf[bitpos/8] |= (1 << (bitpos % 8));
145 bits_set++;
146 }
147 }
148
149 return bits_set ? size : 0;
150}
151EXPORT_SYMBOL_GPL(wusbhc_rh_status_data);
152
153
154
155
156
157
158
159
160static int wusbhc_rh_get_hub_descr(struct wusbhc *wusbhc, u16 wValue,
161 u16 wIndex,
162 struct usb_hub_descriptor *descr,
163 u16 wLength)
164{
165 u16 temp = 1 + (wusbhc->ports_max / 8);
166 u8 length = 7 + 2 * temp;
167
168 if (wLength < length)
169 return -ENOSPC;
170 descr->bDescLength = 7 + 2 * temp;
171 descr->bDescriptorType = USB_DT_HUB;
172 descr->bNbrPorts = wusbhc->ports_max;
173 descr->wHubCharacteristics = cpu_to_le16(
174 HUB_CHAR_COMMON_LPSM
175 | 0x00
176 | HUB_CHAR_NO_OCPM
177 | 0x00
178 | 0x00);
179 descr->bPwrOn2PwrGood = 0;
180 descr->bHubContrCurrent = 0;
181
182 memset(&descr->u.hs.DeviceRemovable[0], 0, temp);
183 memset(&descr->u.hs.DeviceRemovable[temp], 0xff, temp);
184 return 0;
185}
186
187
188
189
190
191
192
193
194static int wusbhc_rh_clear_hub_feat(struct wusbhc *wusbhc, u16 feature)
195{
196 int result;
197
198 switch (feature) {
199 case C_HUB_LOCAL_POWER:
200
201
202
203 case C_HUB_OVER_CURRENT:
204 result = 0;
205 break;
206 default:
207 result = -EPIPE;
208 }
209 return result;
210}
211
212
213
214
215
216
217
218
219static int wusbhc_rh_get_hub_status(struct wusbhc *wusbhc, u32 *buf,
220 u16 wLength)
221{
222
223 *buf = 0;
224 return 0;
225}
226
227
228
229
230
231
232static int wusbhc_rh_set_port_feat(struct wusbhc *wusbhc, u16 feature,
233 u8 selector, u8 port_idx)
234{
235 struct device *dev = wusbhc->dev;
236
237 if (port_idx > wusbhc->ports_max)
238 return -EINVAL;
239
240 switch (feature) {
241
242
243 case USB_PORT_FEAT_C_OVER_CURRENT:
244 case USB_PORT_FEAT_C_ENABLE:
245 case USB_PORT_FEAT_C_SUSPEND:
246 case USB_PORT_FEAT_C_CONNECTION:
247 case USB_PORT_FEAT_C_RESET:
248 return 0;
249 case USB_PORT_FEAT_POWER:
250
251 mutex_lock(&wusbhc->mutex);
252 wusb_port_by_idx(wusbhc, port_idx)->status |= USB_PORT_STAT_POWER;
253 mutex_unlock(&wusbhc->mutex);
254 return 0;
255 case USB_PORT_FEAT_RESET:
256 return wusbhc_rh_port_reset(wusbhc, port_idx);
257 case USB_PORT_FEAT_ENABLE:
258 case USB_PORT_FEAT_SUSPEND:
259 dev_err(dev, "(port_idx %d) set feat %d/%d UNIMPLEMENTED\n",
260 port_idx, feature, selector);
261 return -ENOSYS;
262 default:
263 dev_err(dev, "(port_idx %d) set feat %d/%d UNKNOWN\n",
264 port_idx, feature, selector);
265 return -EPIPE;
266 }
267
268 return 0;
269}
270
271
272
273
274
275
276static int wusbhc_rh_clear_port_feat(struct wusbhc *wusbhc, u16 feature,
277 u8 selector, u8 port_idx)
278{
279 int result = 0;
280 struct device *dev = wusbhc->dev;
281
282 if (port_idx > wusbhc->ports_max)
283 return -EINVAL;
284
285 mutex_lock(&wusbhc->mutex);
286 switch (feature) {
287 case USB_PORT_FEAT_POWER:
288
289 case USB_PORT_FEAT_C_OVER_CURRENT:
290 break;
291 case USB_PORT_FEAT_C_RESET:
292 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_RESET;
293 break;
294 case USB_PORT_FEAT_C_CONNECTION:
295 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_CONNECTION;
296 break;
297 case USB_PORT_FEAT_ENABLE:
298 __wusbhc_dev_disable(wusbhc, port_idx);
299 break;
300 case USB_PORT_FEAT_C_ENABLE:
301 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_ENABLE;
302 break;
303 case USB_PORT_FEAT_SUSPEND:
304 case USB_PORT_FEAT_C_SUSPEND:
305 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNIMPLEMENTED\n",
306 port_idx, feature, selector);
307 result = -ENOSYS;
308 break;
309 default:
310 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
311 port_idx, feature, selector);
312 result = -EPIPE;
313 break;
314 }
315 mutex_unlock(&wusbhc->mutex);
316
317 return result;
318}
319
320
321
322
323
324
325static int wusbhc_rh_get_port_status(struct wusbhc *wusbhc, u16 port_idx,
326 u32 *_buf, u16 wLength)
327{
328 __le16 *buf = (__le16 *)_buf;
329
330 if (port_idx > wusbhc->ports_max)
331 return -EINVAL;
332
333 mutex_lock(&wusbhc->mutex);
334 buf[0] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->status);
335 buf[1] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->change);
336 mutex_unlock(&wusbhc->mutex);
337
338 return 0;
339}
340
341
342
343
344
345
346int wusbhc_rh_control(struct usb_hcd *usb_hcd, u16 reqntype, u16 wValue,
347 u16 wIndex, char *buf, u16 wLength)
348{
349 int result = -ENOSYS;
350 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
351
352 switch (reqntype) {
353 case GetHubDescriptor:
354 result = wusbhc_rh_get_hub_descr(
355 wusbhc, wValue, wIndex,
356 (struct usb_hub_descriptor *) buf, wLength);
357 break;
358 case ClearHubFeature:
359 result = wusbhc_rh_clear_hub_feat(wusbhc, wValue);
360 break;
361 case GetHubStatus:
362 result = wusbhc_rh_get_hub_status(wusbhc, (u32 *)buf, wLength);
363 break;
364
365 case SetPortFeature:
366 result = wusbhc_rh_set_port_feat(wusbhc, wValue, wIndex >> 8,
367 (wIndex & 0xff) - 1);
368 break;
369 case ClearPortFeature:
370 result = wusbhc_rh_clear_port_feat(wusbhc, wValue, wIndex >> 8,
371 (wIndex & 0xff) - 1);
372 break;
373 case GetPortStatus:
374 result = wusbhc_rh_get_port_status(wusbhc, wIndex - 1,
375 (u32 *)buf, wLength);
376 break;
377
378 case SetHubFeature:
379 default:
380 dev_err(wusbhc->dev, "%s (%p [%p], %x, %x, %x, %p, %x) "
381 "UNIMPLEMENTED\n", __func__, usb_hcd, wusbhc, reqntype,
382 wValue, wIndex, buf, wLength);
383
384 result = -ENOSYS;
385 }
386 return result;
387}
388EXPORT_SYMBOL_GPL(wusbhc_rh_control);
389
390int wusbhc_rh_start_port_reset(struct usb_hcd *usb_hcd, unsigned port_idx)
391{
392 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
393 dev_err(wusbhc->dev, "%s (%p [%p], port_idx %u) UNIMPLEMENTED\n",
394 __func__, usb_hcd, wusbhc, port_idx);
395 WARN_ON(1);
396 return -ENOSYS;
397}
398EXPORT_SYMBOL_GPL(wusbhc_rh_start_port_reset);
399
400static void wusb_port_init(struct wusb_port *port)
401{
402 port->status |= USB_PORT_STAT_HIGH_SPEED;
403}
404
405
406
407
408int wusbhc_rh_create(struct wusbhc *wusbhc)
409{
410 int result = -ENOMEM;
411 size_t port_size, itr;
412 port_size = wusbhc->ports_max * sizeof(wusbhc->port[0]);
413 wusbhc->port = kzalloc(port_size, GFP_KERNEL);
414 if (wusbhc->port == NULL)
415 goto error_port_alloc;
416 for (itr = 0; itr < wusbhc->ports_max; itr++)
417 wusb_port_init(&wusbhc->port[itr]);
418 result = 0;
419error_port_alloc:
420 return result;
421}
422
423void wusbhc_rh_destroy(struct wusbhc *wusbhc)
424{
425 kfree(wusbhc->port);
426}
427