wusb: add the Wireless USB core (protocol)
[linux-2.6] / drivers / usb / wusbcore / rh.c
1 /*
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
74 #define D_LOCAL 0
75 #include <linux/uwb/debug.h>
76
77 /*
78  * Reset a fake port
79  *
80  * This can be called to reset a port from any other state or to reset
81  * it when connecting. In Wireless USB they are different; when doing
82  * a new connect that involves going over the authentication. When
83  * just reseting, its a different story.
84  *
85  * The Linux USB stack resets a port twice before it considers it
86  * enabled, so we have to detect and ignore that.
87  *
88  * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
89  *
90  * Supposedly we are the only thread accesing @wusbhc->port; in any
91  * case, maybe we should move the mutex locking from
92  * wusbhc_devconnect_auth() to here.
93  *
94  * @port_idx refers to the wusbhc's port index, not the USB port number
95  */
96 static int wusbhc_rh_port_reset(struct wusbhc *wusbhc, u8 port_idx)
97 {
98         int result = 0;
99         struct wusb_port *port = wusb_port_by_idx(wusbhc, port_idx);
100
101         d_fnstart(3, wusbhc->dev, "(wusbhc %p port_idx %u)\n",
102                   wusbhc, port_idx);
103         if (port->reset_count == 0) {
104                 wusbhc_devconnect_auth(wusbhc, port_idx);
105                 port->reset_count++;
106         } else if (port->reset_count == 1)
107                 /* see header */
108                 d_printf(2, wusbhc->dev, "Ignoring second reset on port_idx "
109                         "%u\n", port_idx);
110         else
111                 result = wusbhc_dev_reset(wusbhc, port_idx);
112         d_fnend(3, wusbhc->dev, "(wusbhc %p port_idx %u) = %d\n",
113                 wusbhc, port_idx, result);
114         return result;
115 }
116
117 /*
118  * Return the hub change status bitmap
119  *
120  * The bits in the change status bitmap are cleared when a
121  * ClearPortFeature request is issued (USB2.0[11.12.3,11.12.4].
122  *
123  * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
124  *
125  * WARNING!! This gets called from atomic context; we cannot get the
126  *           mutex--the only race condition we can find is some bit
127  *           changing just after we copy it, which shouldn't be too
128  *           big of a problem [and we can't make it an spinlock
129  *           because other parts need to take it and sleep] .
130  *
131  *           @usb_hcd is refcounted, so it won't dissapear under us
132  *           and before killing a host, the polling of the root hub
133  *           would be stopped anyway.
134  */
135 int wusbhc_rh_status_data(struct usb_hcd *usb_hcd, char *_buf)
136 {
137         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
138         size_t cnt, size;
139         unsigned long *buf = (unsigned long *) _buf;
140
141         d_fnstart(1, wusbhc->dev, "(wusbhc %p)\n", wusbhc);
142         /* 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);
150         d_fnend(1, wusbhc->dev, "(wusbhc %p) %u, buffer:\n", wusbhc, (int)size);
151         d_dump(1, wusbhc->dev, _buf, size);
152         return size;
153 }
154 EXPORT_SYMBOL_GPL(wusbhc_rh_status_data);
155
156 /*
157  * Return the hub's desciptor
158  *
159  * NOTE: almost cut and paste from ehci-hub.c
160  *
161  * @wusbhc is assumed referenced and @wusbhc->mutex unlocked
162  */
163 static int wusbhc_rh_get_hub_descr(struct wusbhc *wusbhc, u16 wValue,
164                                    u16 wIndex,
165                                    struct usb_hub_descriptor *descr,
166                                    u16 wLength)
167 {
168         u16 temp = 1 + (wusbhc->ports_max / 8);
169         u8 length = 7 + 2 * temp;
170
171         if (wLength < length)
172                 return -ENOSPC;
173         descr->bDescLength = 7 + 2 * temp;
174         descr->bDescriptorType = 0x29;  /* HUB type */
175         descr->bNbrPorts = wusbhc->ports_max;
176         descr->wHubCharacteristics = cpu_to_le16(
177                 0x00                    /* All ports power at once */
178                 | 0x00                  /* not part of compound device */
179                 | 0x10                  /* No overcurrent protection */
180                 | 0x00                  /* 8 FS think time FIXME ?? */
181                 | 0x00);                /* No port indicators */
182         descr->bPwrOn2PwrGood = 0;
183         descr->bHubContrCurrent = 0;
184         /* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
185         memset(&descr->bitmap[0], 0, temp);
186         memset(&descr->bitmap[temp], 0xff, temp);
187         return 0;
188 }
189
190 /*
191  * Clear a hub feature
192  *
193  * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
194  *
195  * Nothing to do, so no locking needed ;)
196  */
197 static int wusbhc_rh_clear_hub_feat(struct wusbhc *wusbhc, u16 feature)
198 {
199         int result;
200         struct device *dev = wusbhc->dev;
201
202         d_fnstart(4, dev, "(%p, feature 0x%04u)\n", wusbhc, feature);
203         switch (feature) {
204         case C_HUB_LOCAL_POWER:
205                 /* FIXME: maybe plug bit 0 to the power input status,
206                  * if any?
207                  * see wusbhc_rh_get_hub_status() */
208         case C_HUB_OVER_CURRENT:
209                 result = 0;
210                 break;
211         default:
212                 result = -EPIPE;
213         }
214         d_fnend(4, dev, "(%p, feature 0x%04u), %d\n", wusbhc, feature, result);
215         return result;
216 }
217
218 /*
219  * Return hub status (it is always zero...)
220  *
221  * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
222  *
223  * Nothing to do, so no locking needed ;)
224  */
225 static int wusbhc_rh_get_hub_status(struct wusbhc *wusbhc, u32 *buf,
226                                     u16 wLength)
227 {
228         /* FIXME: maybe plug bit 0 to the power input status (if any)? */
229         *buf = 0;
230         return 0;
231 }
232
233 /*
234  * Set a port feature
235  *
236  * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
237  */
238 static int wusbhc_rh_set_port_feat(struct wusbhc *wusbhc, u16 feature,
239                                    u8 selector, u8 port_idx)
240 {
241         int result = -EINVAL;
242         struct device *dev = wusbhc->dev;
243
244         d_fnstart(4, dev, "(feat 0x%04u, selector 0x%u, port_idx %d)\n",
245                   feature, selector, port_idx);
246
247         if (port_idx > wusbhc->ports_max)
248                 goto error;
249
250         switch (feature) {
251                 /* According to USB2.0[11.24.2.13]p2, these features
252                  * are not required to be implemented. */
253         case USB_PORT_FEAT_C_OVER_CURRENT:
254         case USB_PORT_FEAT_C_ENABLE:
255         case USB_PORT_FEAT_C_SUSPEND:
256         case USB_PORT_FEAT_C_CONNECTION:
257         case USB_PORT_FEAT_C_RESET:
258                 result = 0;
259                 break;
260
261         case USB_PORT_FEAT_POWER:
262                 /* No such thing, but we fake it works */
263                 mutex_lock(&wusbhc->mutex);
264                 wusb_port_by_idx(wusbhc, port_idx)->status |= USB_PORT_STAT_POWER;
265                 mutex_unlock(&wusbhc->mutex);
266                 result = 0;
267                 break;
268         case USB_PORT_FEAT_RESET:
269                 result = wusbhc_rh_port_reset(wusbhc, port_idx);
270                 break;
271         case USB_PORT_FEAT_ENABLE:
272         case USB_PORT_FEAT_SUSPEND:
273                 dev_err(dev, "(port_idx %d) set feat %d/%d UNIMPLEMENTED\n",
274                         port_idx, feature, selector);
275                 result = -ENOSYS;
276                 break;
277         default:
278                 dev_err(dev, "(port_idx %d) set feat %d/%d UNKNOWN\n",
279                         port_idx, feature, selector);
280                 result = -EPIPE;
281                 break;
282         }
283 error:
284         d_fnend(4, dev, "(feat 0x%04u, selector 0x%u, port_idx %d) = %d\n",
285                 feature, selector, port_idx, result);
286         return result;
287 }
288
289 /*
290  * Clear a port feature...
291  *
292  * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
293  */
294 static int wusbhc_rh_clear_port_feat(struct wusbhc *wusbhc, u16 feature,
295                                      u8 selector, u8 port_idx)
296 {
297         int result = -EINVAL;
298         struct device *dev = wusbhc->dev;
299
300         d_fnstart(4, dev, "(wusbhc %p feat 0x%04x selector %d port_idx %d)\n",
301                   wusbhc, feature, selector, port_idx);
302
303         if (port_idx > wusbhc->ports_max)
304                 goto error;
305
306         mutex_lock(&wusbhc->mutex);
307         result = 0;
308         switch (feature) {
309         case USB_PORT_FEAT_POWER:       /* fake port always on */
310                 /* According to USB2.0[11.24.2.7.1.4], no need to implement? */
311         case USB_PORT_FEAT_C_OVER_CURRENT:
312                 break;
313         case USB_PORT_FEAT_C_RESET:
314                 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_RESET;
315                 break;
316         case USB_PORT_FEAT_C_CONNECTION:
317                 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_CONNECTION;
318                 break;
319         case USB_PORT_FEAT_ENABLE:
320                 __wusbhc_dev_disable(wusbhc, port_idx);
321                 break;
322         case USB_PORT_FEAT_C_ENABLE:
323                 wusb_port_by_idx(wusbhc, port_idx)->change &= ~USB_PORT_STAT_C_ENABLE;
324                 break;
325         case USB_PORT_FEAT_SUSPEND:
326         case USB_PORT_FEAT_C_SUSPEND:
327         case 0xffff:            /* ??? FIXME */
328                 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNIMPLEMENTED\n",
329                         port_idx, feature, selector);
330                 /* dump_stack(); */
331                 result = -ENOSYS;
332                 break;
333         default:
334                 dev_err(dev, "(port_idx %d) Clear feat %d/%d UNKNOWN\n",
335                         port_idx, feature, selector);
336                 result = -EPIPE;
337                 break;
338         }
339         mutex_unlock(&wusbhc->mutex);
340 error:
341         d_fnend(4, dev, "(wusbhc %p feat 0x%04x selector %d port_idx %d) = "
342                 "%d\n", wusbhc, feature, selector, port_idx, result);
343         return result;
344 }
345
346 /*
347  * Return the port's status
348  *
349  * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
350  */
351 static int wusbhc_rh_get_port_status(struct wusbhc *wusbhc, u16 port_idx,
352                                      u32 *_buf, u16 wLength)
353 {
354         int result = -EINVAL;
355         u16 *buf = (u16 *) _buf;
356
357         d_fnstart(1, wusbhc->dev, "(wusbhc %p port_idx %u wLength %u)\n",
358                   wusbhc, port_idx, wLength);
359         if (port_idx > wusbhc->ports_max)
360                 goto error;
361         mutex_lock(&wusbhc->mutex);
362         buf[0] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->status);
363         buf[1] = cpu_to_le16(wusb_port_by_idx(wusbhc, port_idx)->change);
364         result = 0;
365         mutex_unlock(&wusbhc->mutex);
366 error:
367         d_fnend(1, wusbhc->dev, "(wusbhc %p) = %d, buffer:\n", wusbhc, result);
368         d_dump(1, wusbhc->dev, _buf, wLength);
369         return result;
370 }
371
372 /*
373  * Entry point for Root Hub operations
374  *
375  * @wusbhc is assumed referenced and @wusbhc->mutex unlocked.
376  */
377 int wusbhc_rh_control(struct usb_hcd *usb_hcd, u16 reqntype, u16 wValue,
378                       u16 wIndex, char *buf, u16 wLength)
379 {
380         int result = -ENOSYS;
381         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
382
383         switch (reqntype) {
384         case GetHubDescriptor:
385                 result = wusbhc_rh_get_hub_descr(
386                         wusbhc, wValue, wIndex,
387                         (struct usb_hub_descriptor *) buf, wLength);
388                 break;
389         case ClearHubFeature:
390                 result = wusbhc_rh_clear_hub_feat(wusbhc, wValue);
391                 break;
392         case GetHubStatus:
393                 result = wusbhc_rh_get_hub_status(wusbhc, (u32 *)buf, wLength);
394                 break;
395
396         case SetPortFeature:
397                 result = wusbhc_rh_set_port_feat(wusbhc, wValue, wIndex >> 8,
398                                                  (wIndex & 0xff) - 1);
399                 break;
400         case ClearPortFeature:
401                 result = wusbhc_rh_clear_port_feat(wusbhc, wValue, wIndex >> 8,
402                                                    (wIndex & 0xff) - 1);
403                 break;
404         case GetPortStatus:
405                 result = wusbhc_rh_get_port_status(wusbhc, wIndex - 1,
406                                                    (u32 *)buf, wLength);
407                 break;
408
409         case SetHubFeature:
410         default:
411                 dev_err(wusbhc->dev, "%s (%p [%p], %x, %x, %x, %p, %x) "
412                         "UNIMPLEMENTED\n", __func__, usb_hcd, wusbhc, reqntype,
413                         wValue, wIndex, buf, wLength);
414                 /* dump_stack(); */
415                 result = -ENOSYS;
416         }
417         return result;
418 }
419 EXPORT_SYMBOL_GPL(wusbhc_rh_control);
420
421 int wusbhc_rh_suspend(struct usb_hcd *usb_hcd)
422 {
423         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
424         dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
425                 usb_hcd, wusbhc);
426         /* dump_stack(); */
427         return -ENOSYS;
428 }
429 EXPORT_SYMBOL_GPL(wusbhc_rh_suspend);
430
431 int wusbhc_rh_resume(struct usb_hcd *usb_hcd)
432 {
433         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
434         dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
435                 usb_hcd, wusbhc);
436         /* dump_stack(); */
437         return -ENOSYS;
438 }
439 EXPORT_SYMBOL_GPL(wusbhc_rh_resume);
440
441 int wusbhc_rh_start_port_reset(struct usb_hcd *usb_hcd, unsigned port_idx)
442 {
443         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
444         dev_err(wusbhc->dev, "%s (%p [%p], port_idx %u) UNIMPLEMENTED\n",
445                 __func__, usb_hcd, wusbhc, port_idx);
446         WARN_ON(1);
447         return -ENOSYS;
448 }
449 EXPORT_SYMBOL_GPL(wusbhc_rh_start_port_reset);
450
451 static void wusb_port_init(struct wusb_port *port)
452 {
453         port->status |= USB_PORT_STAT_HIGH_SPEED;
454 }
455
456 /*
457  * Alloc fake port specific fields and status.
458  */
459 int wusbhc_rh_create(struct wusbhc *wusbhc)
460 {
461         int result = -ENOMEM;
462         size_t port_size, itr;
463         port_size = wusbhc->ports_max * sizeof(wusbhc->port[0]);
464         wusbhc->port = kzalloc(port_size, GFP_KERNEL);
465         if (wusbhc->port == NULL)
466                 goto error_port_alloc;
467         for (itr = 0; itr < wusbhc->ports_max; itr++)
468                 wusb_port_init(&wusbhc->port[itr]);
469         result = 0;
470 error_port_alloc:
471         return result;
472 }
473
474 void wusbhc_rh_destroy(struct wusbhc *wusbhc)
475 {
476         kfree(wusbhc->port);
477 }