blob: 95c6fa3bf6b24e7541d2185caabdfebd8c9781a3 [file] [log] [blame]
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +01001/*
2 * Wireless USB Host Controller
3 * Root Hub operations
4 *
5 *
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 *
23 *
24 * We fake a root hub that has fake ports (as many as simultaneous
25 * devices the Wireless USB Host Controller can deal with). For each
26 * port we keep an state in @wusbhc->port[index] identical to the one
27 * specified in the USB2.0[ch11] spec and some extra device
28 * information that complements the one in 'struct usb_device' (as
29 * this lacs a hcpriv pointer).
30 *
31 * Note this is common to WHCI and HWA host controllers.
32 *
33 * Through here we enable most of the state changes that the USB stack
34 * will use to connect or disconnect devices. We need to do some
35 * forced adaptation of Wireless USB device states vs. wired:
36 *
37 * USB: WUSB:
38 *
39 * Port Powered-off port slot n/a
40 * Powered-on port slot available
41 * Disconnected port slot available
42 * Connected port slot assigned device
43 * device sent DN_Connect
44 * device was authenticated
45 * Enabled device is authenticated, transitioned
46 * from unauth -> auth -> default address
47 * -> enabled
48 * Reset disconnect
49 * Disable disconnect
50 *
51 * This maps the standard USB port states with the WUSB device states
52 * so we can fake ports without having to modify the USB stack.
53 *
54 * FIXME: this process will change in the future
55 *
56 *
57 * ENTRY POINTS
58 *
59 * Our entry points into here are, as in hcd.c, the USB stack root hub
60 * ops defined in the usb_hcd struct:
61 *
62 * wusbhc_rh_status_data() Provide hub and port status data bitmap
63 *
64 * wusbhc_rh_control() Execution of all the major requests
65 * you can do to a hub (Set|Clear
66 * features, get descriptors, status, etc).
67 *
68 * wusbhc_rh_[suspend|resume]() That
69 *
70 * wusbhc_rh_start_port_reset() ??? unimplemented
71 */
72#include "wusbhc.h"
73
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010074/*
75 * Reset a fake port
76 *
David Vrabel4656d5d2008-10-27 17:12:33 +000077 * Using a Reset Device IE is too heavyweight as it causes the device
78 * to enter the UnConnected state and leave the cluster, this can mean
79 * that when the device reconnects it is connected to a different fake
80 * port.
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010081 *
David Vrabel4656d5d2008-10-27 17:12:33 +000082 * Instead, reset authenticated devices with a SetAddress(0), followed
83 * by a SetAddresss(AuthAddr).
84 *
85 * For unauthenticated devices just pretend to reset but do nothing.
86 * If the device initialization continues to fail it will eventually
87 * time out after TrustTimeout and enter the UnConnected state.
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +010088 *
89 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
90 *
91 * Supposedly we are the only thread accesing @wusbhc->port; in any
92 * case, maybe we should move the mutex locking from
93 * wusbhc_devconnect_auth() to here.
94 *
95 * @port_idx refers to the wusbhc's port index, not the USB port number
96 */
97static int wusbhc_rh_port_reset(struct wusbhc *wusbhc, u8 port_idx)
98{
99 int result = 0;
100 struct wusb_port *port = wusb_port_by_idx(wusbhc, port_idx);
David Vrabel4656d5d2008-10-27 17:12:33 +0000101 struct wusb_dev *wusb_dev = port->wusb_dev;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100102
David Vrabel4656d5d2008-10-27 17:12:33 +0000103 port->status |= USB_PORT_STAT_RESET;
104 port->change |= USB_PORT_STAT_C_RESET;
105
106 if (wusb_dev->addr & WUSB_DEV_ADDR_UNAUTH)
107 result = 0;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100108 else
David Vrabel4656d5d2008-10-27 17:12:33 +0000109 result = wusb_dev_update_address(wusbhc, wusb_dev);
110
111 port->status &= ~USB_PORT_STAT_RESET;
112 port->status |= USB_PORT_STAT_ENABLE;
113 port->change |= USB_PORT_STAT_C_RESET | USB_PORT_STAT_C_ENABLE;
114
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100115 return result;
116}
117
118/*
119 * Return the hub change status bitmap
120 *
121 * The bits in the change status bitmap are cleared when a
122 * ClearPortFeature request is issued (USB2.0[11.12.3,11.12.4].
123 *
124 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
125 *
126 * WARNING!! This gets called from atomic context; we cannot get the
127 * mutex--the only race condition we can find is some bit
128 * changing just after we copy it, which shouldn't be too
129 * big of a problem [and we can't make it an spinlock
130 * because other parts need to take it and sleep] .
131 *
132 * @usb_hcd is refcounted, so it won't dissapear under us
133 * and before killing a host, the polling of the root hub
134 * would be stopped anyway.
135 */
136int wusbhc_rh_status_data(struct usb_hcd *usb_hcd, char *_buf)
137{
138 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
139 size_t cnt, size;
140 unsigned long *buf = (unsigned long *) _buf;
141
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100142 /* WE DON'T LOCK, see comment */
143 size = wusbhc->ports_max + 1 /* hub bit */;
144 size = (size + 8 - 1) / 8; /* round to bytes */
145 for (cnt = 0; cnt < wusbhc->ports_max; cnt++)
146 if (wusb_port_by_idx(wusbhc, cnt)->change)
147 set_bit(cnt + 1, buf);
148 else
149 clear_bit(cnt + 1, buf);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100150 return size;
151}
152EXPORT_SYMBOL_GPL(wusbhc_rh_status_data);
153
154/*
155 * Return the hub's desciptor
156 *
157 * NOTE: almost cut and paste from ehci-hub.c
158 *
159 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked
160 */
161static int wusbhc_rh_get_hub_descr(struct wusbhc *wusbhc, u16 wValue,
162 u16 wIndex,
163 struct usb_hub_descriptor *descr,
164 u16 wLength)
165{
166 u16 temp = 1 + (wusbhc->ports_max / 8);
167 u8 length = 7 + 2 * temp;
168
169 if (wLength < length)
170 return -ENOSPC;
171 descr->bDescLength = 7 + 2 * temp;
172 descr->bDescriptorType = 0x29; /* HUB type */
173 descr->bNbrPorts = wusbhc->ports_max;
174 descr->wHubCharacteristics = cpu_to_le16(
175 0x00 /* All ports power at once */
176 | 0x00 /* not part of compound device */
177 | 0x10 /* No overcurrent protection */
178 | 0x00 /* 8 FS think time FIXME ?? */
179 | 0x00); /* No port indicators */
180 descr->bPwrOn2PwrGood = 0;
181 descr->bHubContrCurrent = 0;
182 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
183 memset(&descr->bitmap[0], 0, temp);
184 memset(&descr->bitmap[temp], 0xff, temp);
185 return 0;
186}
187
188/*
189 * Clear a hub feature
190 *
191 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
192 *
193 * Nothing to do, so no locking needed ;)
194 */
195static int wusbhc_rh_clear_hub_feat(struct wusbhc *wusbhc, u16 feature)
196{
197 int result;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100198
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100199 switch (feature) {
200 case C_HUB_LOCAL_POWER:
201 /* FIXME: maybe plug bit 0 to the power input status,
202 * if any?
203 * see wusbhc_rh_get_hub_status() */
204 case C_HUB_OVER_CURRENT:
205 result = 0;
206 break;
207 default:
208 result = -EPIPE;
209 }
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100210 return result;
211}
212
213/*
214 * Return hub status (it is always zero...)
215 *
216 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
217 *
218 * Nothing to do, so no locking needed ;)
219 */
220static int wusbhc_rh_get_hub_status(struct wusbhc *wusbhc, u32 *buf,
221 u16 wLength)
222{
223 /* FIXME: maybe plug bit 0 to the power input status (if any)? */
224 *buf = 0;
225 return 0;
226}
227
228/*
229 * Set a port feature
230 *
231 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
232 */
233static int wusbhc_rh_set_port_feat(struct wusbhc *wusbhc, u16 feature,
234 u8 selector, u8 port_idx)
235{
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100236 struct device *dev = wusbhc->dev;
237
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100238 if (port_idx > wusbhc->ports_max)
David Vrabelbce83692008-12-22 18:22:50 +0000239 return -EINVAL;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100240
241 switch (feature) {
242 /* According to USB2.0[11.24.2.13]p2, these features
243 * are not required to be implemented. */
244 case USB_PORT_FEAT_C_OVER_CURRENT:
245 case USB_PORT_FEAT_C_ENABLE:
246 case USB_PORT_FEAT_C_SUSPEND:
247 case USB_PORT_FEAT_C_CONNECTION:
248 case USB_PORT_FEAT_C_RESET:
David Vrabelbce83692008-12-22 18:22:50 +0000249 return 0;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100250 case USB_PORT_FEAT_POWER:
251 /* No such thing, but we fake it works */
252 mutex_lock(&wusbhc->mutex);
253 wusb_port_by_idx(wusbhc, port_idx)->status |= USB_PORT_STAT_POWER;
254 mutex_unlock(&wusbhc->mutex);
David Vrabelbce83692008-12-22 18:22:50 +0000255 return 0;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100256 case USB_PORT_FEAT_RESET:
David Vrabelbce83692008-12-22 18:22:50 +0000257 return wusbhc_rh_port_reset(wusbhc, port_idx);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100258 case USB_PORT_FEAT_ENABLE:
259 case USB_PORT_FEAT_SUSPEND:
260 dev_err(dev, "(port_idx %d) set feat %d/%d UNIMPLEMENTED\n",
261 port_idx, feature, selector);
David Vrabelbce83692008-12-22 18:22:50 +0000262 return -ENOSYS;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100263 default:
264 dev_err(dev, "(port_idx %d) set feat %d/%d UNKNOWN\n",
265 port_idx, feature, selector);
David Vrabelbce83692008-12-22 18:22:50 +0000266 return -EPIPE;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100267 }
David Vrabelbce83692008-12-22 18:22:50 +0000268
269 return 0;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100270}
271
272/*
273 * Clear a port feature...
274 *
275 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
276 */
277static int wusbhc_rh_clear_port_feat(struct wusbhc *wusbhc, u16 feature,
278 u8 selector, u8 port_idx)
279{
David Vrabelbce83692008-12-22 18:22:50 +0000280 int result = 0;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100281 struct device *dev = wusbhc->dev;
282
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100283 if (port_idx > wusbhc->ports_max)
David Vrabelbce83692008-12-22 18:22:50 +0000284 return -EINVAL;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100285
286 mutex_lock(&wusbhc->mutex);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100287 switch (feature) {
288 case USB_PORT_FEAT_POWER: /* fake port always on */
289 /* According to USB2.0[11.24.2.7.1.4], no need to implement? */
290 case USB_PORT_FEAT_C_OVER_CURRENT:
291 break;
292 case USB_PORT_FEAT_C_RESET:
293 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_RESET;
294 break;
295 case USB_PORT_FEAT_C_CONNECTION:
296 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_CONNECTION;
297 break;
298 case USB_PORT_FEAT_ENABLE:
299 __wusbhc_dev_disable(wusbhc, port_idx);
300 break;
301 case USB_PORT_FEAT_C_ENABLE:
302 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_ENABLE;
303 break;
304 case USB_PORT_FEAT_SUSPEND:
305 case USB_PORT_FEAT_C_SUSPEND:
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100306 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNIMPLEMENTED\n",
307 port_idx, feature, selector);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100308 result = -ENOSYS;
309 break;
310 default:
311 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
312 port_idx, feature, selector);
313 result = -EPIPE;
314 break;
315 }
316 mutex_unlock(&wusbhc->mutex);
David Vrabelbce83692008-12-22 18:22:50 +0000317
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100318 return result;
319}
320
321/*
322 * Return the port's status
323 *
324 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
325 */
326static int wusbhc_rh_get_port_status(struct wusbhc *wusbhc, u16 port_idx,
327 u32 *_buf, u16 wLength)
328{
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100329 u16 *buf = (u16 *) _buf;
330
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100331 if (port_idx > wusbhc->ports_max)
David Vrabelbce83692008-12-22 18:22:50 +0000332 return -EINVAL;
333
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100334 mutex_lock(&wusbhc->mutex);
335 buf[0] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->status);
336 buf[1] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->change);
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100337 mutex_unlock(&wusbhc->mutex);
David Vrabelbce83692008-12-22 18:22:50 +0000338
339 return 0;
Inaky Perez-Gonzalez90ff96f2008-09-17 16:34:23 +0100340}
341
342/*
343 * Entry point for Root Hub operations
344 *
345 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
346 */
347int wusbhc_rh_control(struct usb_hcd *usb_hcd, u16 reqntype, u16 wValue,
348 u16 wIndex, char *buf, u16 wLength)
349{
350 int result = -ENOSYS;
351 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
352
353 switch (reqntype) {
354 case GetHubDescriptor:
355 result = wusbhc_rh_get_hub_descr(
356 wusbhc, wValue, wIndex,
357 (struct usb_hub_descriptor *) buf, wLength);
358 break;
359 case ClearHubFeature:
360 result = wusbhc_rh_clear_hub_feat(wusbhc, wValue);
361 break;
362 case GetHubStatus:
363 result = wusbhc_rh_get_hub_status(wusbhc, (u32 *)buf, wLength);
364 break;
365
366 case SetPortFeature:
367 result = wusbhc_rh_set_port_feat(wusbhc, wValue, wIndex >> 8,
368 (wIndex & 0xff) - 1);
369 break;
370 case ClearPortFeature:
371 result = wusbhc_rh_clear_port_feat(wusbhc, wValue, wIndex >> 8,
372 (wIndex & 0xff) - 1);
373 break;
374 case GetPortStatus:
375 result = wusbhc_rh_get_port_status(wusbhc, wIndex - 1,
376 (u32 *)buf, wLength);
377 break;
378
379 case SetHubFeature:
380 default:
381 dev_err(wusbhc->dev, "%s (%p [%p], %x, %x, %x, %p, %x) "
382 "UNIMPLEMENTED\n", __func__, usb_hcd, wusbhc, reqntype,
383 wValue, wIndex, buf, wLength);
384 /* dump_stack(); */
385 result = -ENOSYS;
386 }
387 return result;
388}
389EXPORT_SYMBOL_GPL(wusbhc_rh_control);
390
391int wusbhc_rh_suspend(struct usb_hcd *usb_hcd)
392{
393 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
394 dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
395 usb_hcd, wusbhc);
396 /* dump_stack(); */
397 return -ENOSYS;
398}
399EXPORT_SYMBOL_GPL(wusbhc_rh_suspend);
400
401int wusbhc_rh_resume(struct usb_hcd *usb_hcd)
402{
403 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
404 dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
405 usb_hcd, wusbhc);
406 /* dump_stack(); */
407 return -ENOSYS;
408}
409EXPORT_SYMBOL_GPL(wusbhc_rh_resume);
410
411int wusbhc_rh_start_port_reset(struct usb_hcd *usb_hcd, unsigned port_idx)
412{
413 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
414 dev_err(wusbhc->dev, "%s (%p [%p], port_idx %u) UNIMPLEMENTED\n",
415 __func__, usb_hcd, wusbhc, port_idx);
416 WARN_ON(1);
417 return -ENOSYS;
418}
419EXPORT_SYMBOL_GPL(wusbhc_rh_start_port_reset);
420
421static void wusb_port_init(struct wusb_port *port)
422{
423 port->status |= USB_PORT_STAT_HIGH_SPEED;
424}
425
426/*
427 * Alloc fake port specific fields and status.
428 */
429int wusbhc_rh_create(struct wusbhc *wusbhc)
430{
431 int result = -ENOMEM;
432 size_t port_size, itr;
433 port_size = wusbhc->ports_max * sizeof(wusbhc->port[0]);
434 wusbhc->port = kzalloc(port_size, GFP_KERNEL);
435 if (wusbhc->port == NULL)
436 goto error_port_alloc;
437 for (itr = 0; itr < wusbhc->ports_max; itr++)
438 wusb_port_init(&wusbhc->port[itr]);
439 result = 0;
440error_port_alloc:
441 return result;
442}
443
444void wusbhc_rh_destroy(struct wusbhc *wusbhc)
445{
446 kfree(wusbhc->port);
447}