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