2 * Wireless USB Host Controller
6 * Copyright (C) 2005-2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
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).
31 * Note this is common to WHCI and HWA host controllers.
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:
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
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.
54 * FIXME: this process will change in the future
59 * Our entry points into here are, as in hcd.c, the USB stack root hub
60 * ops defined in the usb_hcd struct:
62 * wusbhc_rh_status_data() Provide hub and port status data bitmap
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).
68 * wusbhc_rh_[suspend|resume]() That
70 * wusbhc_rh_start_port_reset() ??? unimplemented
75 #include <linux/uwb/debug.h>
80 * Using a Reset Device IE is too heavyweight as it causes the device
81 * to enter the UnConnected state and leave the cluster, this can mean
82 * that when the device reconnects it is connected to a different fake
85 * Instead, reset authenticated devices with a SetAddress(0), followed
86 * by a SetAddresss(AuthAddr).
88 * For unauthenticated devices just pretend to reset but do nothing.
89 * If the device initialization continues to fail it will eventually
90 * time out after TrustTimeout and enter the UnConnected state.
92 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
94 * Supposedly we are the only thread accesing @wusbhc->port; in any
95 * case, maybe we should move the mutex locking from
96 * wusbhc_devconnect_auth() to here.
98 * @port_idx refers to the wusbhc's port index, not the USB port number
100 static int wusbhc_rh_port_reset(struct wusbhc *wusbhc, u8 port_idx)
103 struct wusb_port *port = wusb_port_by_idx(wusbhc, port_idx);
104 struct wusb_dev *wusb_dev = port->wusb_dev;
106 port->status |= USB_PORT_STAT_RESET;
107 port->change |= USB_PORT_STAT_C_RESET;
109 if (wusb_dev->addr & WUSB_DEV_ADDR_UNAUTH)
112 result = wusb_dev_update_address(wusbhc, wusb_dev);
114 port->status &= ~USB_PORT_STAT_RESET;
115 port->status |= USB_PORT_STAT_ENABLE;
116 port->change |= USB_PORT_STAT_C_RESET | USB_PORT_STAT_C_ENABLE;
122 * Return the hub change status bitmap
124 * The bits in the change status bitmap are cleared when a
125 * ClearPortFeature request is issued (USB2.0[11.12.3,11.12.4].
127 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
129 * WARNING!! This gets called from atomic context; we cannot get the
130 * mutex--the only race condition we can find is some bit
131 * changing just after we copy it, which shouldn't be too
132 * big of a problem [and we can't make it an spinlock
133 * because other parts need to take it and sleep] .
135 * @usb_hcd is refcounted, so it won't dissapear under us
136 * and before killing a host, the polling of the root hub
137 * would be stopped anyway.
139 int wusbhc_rh_status_data(struct usb_hcd *usb_hcd, char *_buf)
141 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
143 unsigned long *buf = (unsigned long *) _buf;
145 d_fnstart(1, wusbhc->dev, "(wusbhc %p)\n", wusbhc);
146 /* WE DON'T LOCK, see comment */
147 size = wusbhc->ports_max + 1 /* hub bit */;
148 size = (size + 8 - 1) / 8; /* round to bytes */
149 for (cnt = 0; cnt < wusbhc->ports_max; cnt++)
150 if (wusb_port_by_idx(wusbhc, cnt)->change)
151 set_bit(cnt + 1, buf);
153 clear_bit(cnt + 1, buf);
154 d_fnend(1, wusbhc->dev, "(wusbhc %p) %u, buffer:\n", wusbhc, (int)size);
155 d_dump(1, wusbhc->dev, _buf, size);
158 EXPORT_SYMBOL_GPL(wusbhc_rh_status_data);
161 * Return the hub's desciptor
163 * NOTE: almost cut and paste from ehci-hub.c
165 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked
167 static int wusbhc_rh_get_hub_descr(struct wusbhc *wusbhc, u16 wValue,
169 struct usb_hub_descriptor *descr,
172 u16 temp = 1 + (wusbhc->ports_max / 8);
173 u8 length = 7 + 2 * temp;
175 if (wLength < length)
177 descr->bDescLength = 7 + 2 * temp;
178 descr->bDescriptorType = 0x29; /* HUB type */
179 descr->bNbrPorts = wusbhc->ports_max;
180 descr->wHubCharacteristics = cpu_to_le16(
181 0x00 /* All ports power at once */
182 | 0x00 /* not part of compound device */
183 | 0x10 /* No overcurrent protection */
184 | 0x00 /* 8 FS think time FIXME ?? */
185 | 0x00); /* No port indicators */
186 descr->bPwrOn2PwrGood = 0;
187 descr->bHubContrCurrent = 0;
188 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
189 memset(&descr->bitmap[0], 0, temp);
190 memset(&descr->bitmap[temp], 0xff, temp);
195 * Clear a hub feature
197 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
199 * Nothing to do, so no locking needed ;)
201 static int wusbhc_rh_clear_hub_feat(struct wusbhc *wusbhc, u16 feature)
204 struct device *dev = wusbhc->dev;
206 d_fnstart(4, dev, "(%p, feature 0x%04u)\n", wusbhc, feature);
208 case C_HUB_LOCAL_POWER:
209 /* FIXME: maybe plug bit 0 to the power input status,
211 * see wusbhc_rh_get_hub_status() */
212 case C_HUB_OVER_CURRENT:
218 d_fnend(4, dev, "(%p, feature 0x%04u), %d\n", wusbhc, feature, result);
223 * Return hub status (it is always zero...)
225 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
227 * Nothing to do, so no locking needed ;)
229 static int wusbhc_rh_get_hub_status(struct wusbhc *wusbhc, u32 *buf,
232 /* FIXME: maybe plug bit 0 to the power input status (if any)? */
240 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
242 static int wusbhc_rh_set_port_feat(struct wusbhc *wusbhc, u16 feature,
243 u8 selector, u8 port_idx)
245 int result = -EINVAL;
246 struct device *dev = wusbhc->dev;
248 d_fnstart(4, dev, "(feat 0x%04u, selector 0x%u, port_idx %d)\n",
249 feature, selector, port_idx);
251 if (port_idx > wusbhc->ports_max)
255 /* According to USB2.0[11.24.2.13]p2, these features
256 * are not required to be implemented. */
257 case USB_PORT_FEAT_C_OVER_CURRENT:
258 case USB_PORT_FEAT_C_ENABLE:
259 case USB_PORT_FEAT_C_SUSPEND:
260 case USB_PORT_FEAT_C_CONNECTION:
261 case USB_PORT_FEAT_C_RESET:
265 case USB_PORT_FEAT_POWER:
266 /* No such thing, but we fake it works */
267 mutex_lock(&wusbhc->mutex);
268 wusb_port_by_idx(wusbhc, port_idx)->status |= USB_PORT_STAT_POWER;
269 mutex_unlock(&wusbhc->mutex);
272 case USB_PORT_FEAT_RESET:
273 result = wusbhc_rh_port_reset(wusbhc, port_idx);
275 case USB_PORT_FEAT_ENABLE:
276 case USB_PORT_FEAT_SUSPEND:
277 dev_err(dev, "(port_idx %d) set feat %d/%d UNIMPLEMENTED\n",
278 port_idx, feature, selector);
282 dev_err(dev, "(port_idx %d) set feat %d/%d UNKNOWN\n",
283 port_idx, feature, selector);
288 d_fnend(4, dev, "(feat 0x%04u, selector 0x%u, port_idx %d) = %d\n",
289 feature, selector, port_idx, result);
294 * Clear a port feature...
296 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
298 static int wusbhc_rh_clear_port_feat(struct wusbhc *wusbhc, u16 feature,
299 u8 selector, u8 port_idx)
301 int result = -EINVAL;
302 struct device *dev = wusbhc->dev;
304 d_fnstart(4, dev, "(wusbhc %p feat 0x%04x selector %d port_idx %d)\n",
305 wusbhc, feature, selector, port_idx);
307 if (port_idx > wusbhc->ports_max)
310 mutex_lock(&wusbhc->mutex);
313 case USB_PORT_FEAT_POWER: /* fake port always on */
314 /* According to USB2.0[11.24.2.7.1.4], no need to implement? */
315 case USB_PORT_FEAT_C_OVER_CURRENT:
317 case USB_PORT_FEAT_C_RESET:
318 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_RESET;
320 case USB_PORT_FEAT_C_CONNECTION:
321 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_CONNECTION;
323 case USB_PORT_FEAT_ENABLE:
324 __wusbhc_dev_disable(wusbhc, port_idx);
326 case USB_PORT_FEAT_C_ENABLE:
327 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_ENABLE;
329 case USB_PORT_FEAT_SUSPEND:
330 case USB_PORT_FEAT_C_SUSPEND:
331 case 0xffff: /* ??? FIXME */
332 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNIMPLEMENTED\n",
333 port_idx, feature, selector);
338 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
339 port_idx, feature, selector);
343 mutex_unlock(&wusbhc->mutex);
345 d_fnend(4, dev, "(wusbhc %p feat 0x%04x selector %d port_idx %d) = "
346 "%d\n", wusbhc, feature, selector, port_idx, result);
351 * Return the port's status
353 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
355 static int wusbhc_rh_get_port_status(struct wusbhc *wusbhc, u16 port_idx,
356 u32 *_buf, u16 wLength)
358 int result = -EINVAL;
359 u16 *buf = (u16 *) _buf;
361 d_fnstart(1, wusbhc->dev, "(wusbhc %p port_idx %u wLength %u)\n",
362 wusbhc, port_idx, wLength);
363 if (port_idx > wusbhc->ports_max)
365 mutex_lock(&wusbhc->mutex);
366 buf[0] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->status);
367 buf[1] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->change);
369 mutex_unlock(&wusbhc->mutex);
371 d_fnend(1, wusbhc->dev, "(wusbhc %p) = %d, buffer:\n", wusbhc, result);
372 d_dump(1, wusbhc->dev, _buf, wLength);
377 * Entry point for Root Hub operations
379 * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
381 int wusbhc_rh_control(struct usb_hcd *usb_hcd, u16 reqntype, u16 wValue,
382 u16 wIndex, char *buf, u16 wLength)
384 int result = -ENOSYS;
385 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
388 case GetHubDescriptor:
389 result = wusbhc_rh_get_hub_descr(
390 wusbhc, wValue, wIndex,
391 (struct usb_hub_descriptor *) buf, wLength);
393 case ClearHubFeature:
394 result = wusbhc_rh_clear_hub_feat(wusbhc, wValue);
397 result = wusbhc_rh_get_hub_status(wusbhc, (u32 *)buf, wLength);
401 result = wusbhc_rh_set_port_feat(wusbhc, wValue, wIndex >> 8,
402 (wIndex & 0xff) - 1);
404 case ClearPortFeature:
405 result = wusbhc_rh_clear_port_feat(wusbhc, wValue, wIndex >> 8,
406 (wIndex & 0xff) - 1);
409 result = wusbhc_rh_get_port_status(wusbhc, wIndex - 1,
410 (u32 *)buf, wLength);
415 dev_err(wusbhc->dev, "%s (%p [%p], %x, %x, %x, %p, %x) "
416 "UNIMPLEMENTED\n", __func__, usb_hcd, wusbhc, reqntype,
417 wValue, wIndex, buf, wLength);
423 EXPORT_SYMBOL_GPL(wusbhc_rh_control);
425 int wusbhc_rh_suspend(struct usb_hcd *usb_hcd)
427 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
428 dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
433 EXPORT_SYMBOL_GPL(wusbhc_rh_suspend);
435 int wusbhc_rh_resume(struct usb_hcd *usb_hcd)
437 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
438 dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
443 EXPORT_SYMBOL_GPL(wusbhc_rh_resume);
445 int wusbhc_rh_start_port_reset(struct usb_hcd *usb_hcd, unsigned port_idx)
447 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
448 dev_err(wusbhc->dev, "%s (%p [%p], port_idx %u) UNIMPLEMENTED\n",
449 __func__, usb_hcd, wusbhc, port_idx);
453 EXPORT_SYMBOL_GPL(wusbhc_rh_start_port_reset);
455 static void wusb_port_init(struct wusb_port *port)
457 port->status |= USB_PORT_STAT_HIGH_SPEED;
461 * Alloc fake port specific fields and status.
463 int wusbhc_rh_create(struct wusbhc *wusbhc)
465 int result = -ENOMEM;
466 size_t port_size, itr;
467 port_size = wusbhc->ports_max * sizeof(wusbhc->port[0]);
468 wusbhc->port = kzalloc(port_size, GFP_KERNEL);
469 if (wusbhc->port == NULL)
470 goto error_port_alloc;
471 for (itr = 0; itr < wusbhc->ports_max; itr++)
472 wusb_port_init(&wusbhc->port[itr]);
478 void wusbhc_rh_destroy(struct wusbhc *wusbhc)