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