sc92031: add a link to the datasheet
[linux-2.6] / drivers / usb / host / hwa-hc.c
1 /*
2  * Host Wire Adapter:
3  * Driver glue, HWA-specific functions, bridges to WAHC and WUSBHC
4  *
5  * Copyright (C) 2005-2006 Intel Corporation
6  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version
10  * 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  *
22  *
23  * The HWA driver is a simple layer that forwards requests to the WAHC
24  * (Wire Adater Host Controller) or WUSBHC (Wireless USB Host
25  * Controller) layers.
26  *
27  * Host Wire Adapter is the 'WUSB 1.0 standard' name for Wireless-USB
28  * Host Controller that is connected to your system via USB (a USB
29  * dongle that implements a USB host...). There is also a Device Wired
30  * Adaptor, DWA (Wireless USB hub) that uses the same mechanism for
31  * transferring data (it is after all a USB host connected via
32  * Wireless USB), we have a common layer called Wire Adapter Host
33  * Controller that does all the hard work. The WUSBHC (Wireless USB
34  * Host Controller) is the part common to WUSB Host Controllers, the
35  * HWA and the PCI-based one, that is implemented following the WHCI
36  * spec. All these layers are implemented in ../wusbcore.
37  *
38  * The main functions are hwahc_op_urb_{en,de}queue(), that pass the
39  * job of converting a URB to a Wire Adapter
40  *
41  * Entry points:
42  *
43  *   hwahc_driver_*()   Driver initialization, registration and
44  *                      teardown.
45  *
46  *   hwahc_probe()      New device came up, create an instance for
47  *                      it [from device enumeration].
48  *
49  *   hwahc_disconnect() Remove device instance [from device
50  *                      enumeration].
51  *
52  *   [__]hwahc_op_*()   Host-Wire-Adaptor specific functions for
53  *                      starting/stopping/etc (some might be made also
54  *                      DWA).
55  */
56 #include <linux/kernel.h>
57 #include <linux/init.h>
58 #include <linux/module.h>
59 #include <linux/workqueue.h>
60 #include <linux/wait.h>
61 #include <linux/completion.h>
62 #include "../wusbcore/wa-hc.h"
63 #include "../wusbcore/wusbhc.h"
64
65 struct hwahc {
66         struct wusbhc wusbhc;   /* has to be 1st */
67         struct wahc wa;
68 };
69
70 /*
71  * FIXME should be wusbhc
72  *
73  * NOTE: we need to cache the Cluster ID because later...there is no
74  *       way to get it :)
75  */
76 static int __hwahc_set_cluster_id(struct hwahc *hwahc, u8 cluster_id)
77 {
78         int result;
79         struct wusbhc *wusbhc = &hwahc->wusbhc;
80         struct wahc *wa = &hwahc->wa;
81         struct device *dev = &wa->usb_iface->dev;
82
83         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
84                         WUSB_REQ_SET_CLUSTER_ID,
85                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
86                         cluster_id,
87                         wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
88                         NULL, 0, 1000 /* FIXME: arbitrary */);
89         if (result < 0)
90                 dev_err(dev, "Cannot set WUSB Cluster ID to 0x%02x: %d\n",
91                         cluster_id, result);
92         else
93                 wusbhc->cluster_id = cluster_id;
94         dev_info(dev, "Wireless USB Cluster ID set to 0x%02x\n", cluster_id);
95         return result;
96 }
97
98 static int __hwahc_op_set_num_dnts(struct wusbhc *wusbhc, u8 interval, u8 slots)
99 {
100         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
101         struct wahc *wa = &hwahc->wa;
102
103         return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
104                         WUSB_REQ_SET_NUM_DNTS,
105                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
106                         interval << 8 | slots,
107                         wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
108                         NULL, 0, 1000 /* FIXME: arbitrary */);
109 }
110
111 /*
112  * Reset a WUSB host controller and wait for it to complete doing it.
113  *
114  * @usb_hcd:    Pointer to WUSB Host Controller instance.
115  *
116  */
117 static int hwahc_op_reset(struct usb_hcd *usb_hcd)
118 {
119         int result;
120         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
121         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
122         struct device *dev = &hwahc->wa.usb_iface->dev;
123
124         mutex_lock(&wusbhc->mutex);
125         wa_nep_disarm(&hwahc->wa);
126         result = __wa_set_feature(&hwahc->wa, WA_RESET);
127         if (result < 0) {
128                 dev_err(dev, "error commanding HC to reset: %d\n", result);
129                 goto error_unlock;
130         }
131         result = __wa_wait_status(&hwahc->wa, WA_STATUS_RESETTING, 0);
132         if (result < 0) {
133                 dev_err(dev, "error waiting for HC to reset: %d\n", result);
134                 goto error_unlock;
135         }
136 error_unlock:
137         mutex_unlock(&wusbhc->mutex);
138         return result;
139 }
140
141 /*
142  * FIXME: break this function up
143  */
144 static int hwahc_op_start(struct usb_hcd *usb_hcd)
145 {
146         u8 addr;
147         int result;
148         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
149         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
150
151         result = -ENOSPC;
152         mutex_lock(&wusbhc->mutex);
153         addr = wusb_cluster_id_get();
154         if (addr == 0)
155                 goto error_cluster_id_get;
156         result = __hwahc_set_cluster_id(hwahc, addr);
157         if (result < 0)
158                 goto error_set_cluster_id;
159
160         usb_hcd->uses_new_polling = 1;
161         usb_hcd->poll_rh = 1;
162         usb_hcd->state = HC_STATE_RUNNING;
163         result = 0;
164 out:
165         mutex_unlock(&wusbhc->mutex);
166         return result;
167
168 error_set_cluster_id:
169         wusb_cluster_id_put(wusbhc->cluster_id);
170 error_cluster_id_get:
171         goto out;
172
173 }
174
175 static int hwahc_op_suspend(struct usb_hcd *usb_hcd, pm_message_t msg)
176 {
177         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
178         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
179         dev_err(wusbhc->dev, "%s (%p [%p], 0x%lx) UNIMPLEMENTED\n", __func__,
180                 usb_hcd, hwahc, *(unsigned long *) &msg);
181         return -ENOSYS;
182 }
183
184 static int hwahc_op_resume(struct usb_hcd *usb_hcd)
185 {
186         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
187         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
188
189         dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
190                 usb_hcd, hwahc);
191         return -ENOSYS;
192 }
193
194 /*
195  * No need to abort pipes, as when this is called, all the children
196  * has been disconnected and that has done it [through
197  * usb_disable_interface() -> usb_disable_endpoint() ->
198  * hwahc_op_ep_disable() - >rpipe_ep_disable()].
199  */
200 static void hwahc_op_stop(struct usb_hcd *usb_hcd)
201 {
202         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
203
204         mutex_lock(&wusbhc->mutex);
205         wusb_cluster_id_put(wusbhc->cluster_id);
206         mutex_unlock(&wusbhc->mutex);
207 }
208
209 static int hwahc_op_get_frame_number(struct usb_hcd *usb_hcd)
210 {
211         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
212         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
213
214         dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
215                 usb_hcd, hwahc);
216         return -ENOSYS;
217 }
218
219 static int hwahc_op_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
220                                 gfp_t gfp)
221 {
222         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
223         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
224
225         return wa_urb_enqueue(&hwahc->wa, urb->ep, urb, gfp);
226 }
227
228 static int hwahc_op_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb,
229                                 int status)
230 {
231         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
232         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
233
234         return wa_urb_dequeue(&hwahc->wa, urb);
235 }
236
237 /*
238  * Release resources allocated for an endpoint
239  *
240  * If there is an associated rpipe to this endpoint, go ahead and put it.
241  */
242 static void hwahc_op_endpoint_disable(struct usb_hcd *usb_hcd,
243                                       struct usb_host_endpoint *ep)
244 {
245         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
246         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
247
248         rpipe_ep_disable(&hwahc->wa, ep);
249 }
250
251 static int __hwahc_op_wusbhc_start(struct wusbhc *wusbhc)
252 {
253         int result;
254         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
255         struct device *dev = &hwahc->wa.usb_iface->dev;
256
257         result = __wa_set_feature(&hwahc->wa, WA_ENABLE);
258         if (result < 0) {
259                 dev_err(dev, "error commanding HC to start: %d\n", result);
260                 goto error_stop;
261         }
262         result = __wa_wait_status(&hwahc->wa, WA_ENABLE, WA_ENABLE);
263         if (result < 0) {
264                 dev_err(dev, "error waiting for HC to start: %d\n", result);
265                 goto error_stop;
266         }
267         result = wa_nep_arm(&hwahc->wa, GFP_KERNEL);
268         if (result < 0) {
269                 dev_err(dev, "cannot listen to notifications: %d\n", result);
270                 goto error_stop;
271         }
272         return result;
273
274 error_stop:
275         __wa_clear_feature(&hwahc->wa, WA_ENABLE);
276         return result;
277 }
278
279 static void __hwahc_op_wusbhc_stop(struct wusbhc *wusbhc, int delay)
280 {
281         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
282         struct wahc *wa = &hwahc->wa;
283         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
284         int ret;
285
286         ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
287                               WUSB_REQ_CHAN_STOP,
288                               USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
289                               delay * 1000,
290                               iface_no,
291                               NULL, 0, 1000 /* FIXME: arbitrary */);
292         if (ret == 0)
293                 msleep(delay);
294
295         wa_nep_disarm(&hwahc->wa);
296         __wa_stop(&hwahc->wa);
297 }
298
299 /*
300  * Set the UWB MAS allocation for the WUSB cluster
301  *
302  * @stream_index: stream to use (-1 for cancelling the allocation)
303  * @mas: mas bitmap to use
304  */
305 static int __hwahc_op_bwa_set(struct wusbhc *wusbhc, s8 stream_index,
306                               const struct uwb_mas_bm *mas)
307 {
308         int result;
309         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
310         struct wahc *wa = &hwahc->wa;
311         struct device *dev = &wa->usb_iface->dev;
312         u8 mas_le[UWB_NUM_MAS/8];
313
314         /* Set the stream index */
315         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
316                         WUSB_REQ_SET_STREAM_IDX,
317                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
318                         stream_index,
319                         wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
320                         NULL, 0, 1000 /* FIXME: arbitrary */);
321         if (result < 0) {
322                 dev_err(dev, "Cannot set WUSB stream index: %d\n", result);
323                 goto out;
324         }
325         uwb_mas_bm_copy_le(mas_le, mas);
326         /* Set the MAS allocation */
327         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
328                         WUSB_REQ_SET_WUSB_MAS,
329                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
330                         0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
331                         mas_le, 32, 1000 /* FIXME: arbitrary */);
332         if (result < 0)
333                 dev_err(dev, "Cannot set WUSB MAS allocation: %d\n", result);
334 out:
335         return result;
336 }
337
338 /*
339  * Add an IE to the host's MMC
340  *
341  * @interval:    See WUSB1.0[8.5.3.1]
342  * @repeat_cnt:  See WUSB1.0[8.5.3.1]
343  * @handle:      See WUSB1.0[8.5.3.1]
344  * @wuie:        Pointer to the header of the WUSB IE data to add.
345  *               MUST BE allocated in a kmalloc buffer (no stack or
346  *               vmalloc).
347  *
348  * NOTE: the format of the WUSB IEs for MMCs are different to the
349  *       normal MBOA MAC IEs (IE Id + Length in MBOA MAC vs. Length +
350  *       Id in WUSB IEs). Standards...you gotta love'em.
351  */
352 static int __hwahc_op_mmcie_add(struct wusbhc *wusbhc, u8 interval,
353                                 u8 repeat_cnt, u8 handle,
354                                 struct wuie_hdr *wuie)
355 {
356         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
357         struct wahc *wa = &hwahc->wa;
358         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
359
360         return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
361                         WUSB_REQ_ADD_MMC_IE,
362                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
363                         interval << 8 | repeat_cnt,
364                         handle << 8 | iface_no,
365                         wuie, wuie->bLength, 1000 /* FIXME: arbitrary */);
366 }
367
368 /*
369  * Remove an IE to the host's MMC
370  *
371  * @handle:      See WUSB1.0[8.5.3.1]
372  */
373 static int __hwahc_op_mmcie_rm(struct wusbhc *wusbhc, u8 handle)
374 {
375         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
376         struct wahc *wa = &hwahc->wa;
377         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
378         return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
379                         WUSB_REQ_REMOVE_MMC_IE,
380                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
381                         0, handle << 8 | iface_no,
382                         NULL, 0, 1000 /* FIXME: arbitrary */);
383 }
384
385 /*
386  * Update device information for a given fake port
387  *
388  * @port_idx: Fake port to which device is connected (wusbhc index, not
389  *            USB port number).
390  */
391 static int __hwahc_op_dev_info_set(struct wusbhc *wusbhc,
392                                    struct wusb_dev *wusb_dev)
393 {
394         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
395         struct wahc *wa = &hwahc->wa;
396         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
397         struct hwa_dev_info *dev_info;
398         int ret;
399
400         /* fill out the Device Info buffer and send it */
401         dev_info = kzalloc(sizeof(struct hwa_dev_info), GFP_KERNEL);
402         if (!dev_info)
403                 return -ENOMEM;
404         uwb_mas_bm_copy_le(dev_info->bmDeviceAvailability,
405                            &wusb_dev->availability);
406         dev_info->bDeviceAddress = wusb_dev->addr;
407
408         /*
409          * If the descriptors haven't been read yet, use a default PHY
410          * rate of 53.3 Mbit/s only.  The correct value will be used
411          * when this will be called again as part of the
412          * authentication process (which occurs after the descriptors
413          * have been read).
414          */
415         if (wusb_dev->wusb_cap_descr)
416                 dev_info->wPHYRates = wusb_dev->wusb_cap_descr->wPHYRates;
417         else
418                 dev_info->wPHYRates = cpu_to_le16(USB_WIRELESS_PHY_53);
419
420         ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
421                         WUSB_REQ_SET_DEV_INFO,
422                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
423                         0, wusb_dev->port_idx << 8 | iface_no,
424                         dev_info, sizeof(struct hwa_dev_info),
425                         1000 /* FIXME: arbitrary */);
426         kfree(dev_info);
427         return ret;
428 }
429
430 /*
431  * Set host's idea of which encryption (and key) method to use when
432  * talking to ad evice on a given port.
433  *
434  * If key is NULL, it means disable encryption for that "virtual port"
435  * (used when we disconnect).
436  */
437 static int __hwahc_dev_set_key(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
438                                const void *key, size_t key_size,
439                                u8 key_idx)
440 {
441         int result = -ENOMEM;
442         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
443         struct wahc *wa = &hwahc->wa;
444         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
445         struct usb_key_descriptor *keyd;
446         size_t keyd_len;
447
448         keyd_len = sizeof(*keyd) + key_size;
449         keyd = kzalloc(keyd_len, GFP_KERNEL);
450         if (keyd == NULL)
451                 return -ENOMEM;
452
453         keyd->bLength = keyd_len;
454         keyd->bDescriptorType = USB_DT_KEY;
455         keyd->tTKID[0] = (tkid >>  0) & 0xff;
456         keyd->tTKID[1] = (tkid >>  8) & 0xff;
457         keyd->tTKID[2] = (tkid >> 16) & 0xff;
458         memcpy(keyd->bKeyData, key, key_size);
459
460         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
461                         USB_REQ_SET_DESCRIPTOR,
462                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
463                         USB_DT_KEY << 8 | key_idx,
464                         port_idx << 8 | iface_no,
465                         keyd, keyd_len, 1000 /* FIXME: arbitrary */);
466
467         memset(keyd, 0, sizeof(*keyd)); /* clear keys etc. */
468         kfree(keyd);
469         return result;
470 }
471
472 /*
473  * Set host's idea of which encryption (and key) method to use when
474  * talking to ad evice on a given port.
475  *
476  * If key is NULL, it means disable encryption for that "virtual port"
477  * (used when we disconnect).
478  */
479 static int __hwahc_op_set_ptk(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
480                               const void *key, size_t key_size)
481 {
482         int result = -ENOMEM;
483         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
484         struct wahc *wa = &hwahc->wa;
485         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
486         u8 encryption_value;
487
488         /* Tell the host which key to use to talk to the device */
489         if (key) {
490                 u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_PTK,
491                                             WUSB_KEY_INDEX_ORIGINATOR_HOST);
492
493                 result = __hwahc_dev_set_key(wusbhc, port_idx, tkid,
494                                              key, key_size, key_idx);
495                 if (result < 0)
496                         goto error_set_key;
497                 encryption_value = wusbhc->ccm1_etd->bEncryptionValue;
498         } else {
499                 /* FIXME: this should come from wusbhc->etd[UNSECURE].value */
500                 encryption_value = 0;
501         }
502
503         /* Set the encryption type for commmunicating with the device */
504         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
505                         USB_REQ_SET_ENCRYPTION,
506                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
507                         encryption_value, port_idx << 8 | iface_no,
508                         NULL, 0, 1000 /* FIXME: arbitrary */);
509         if (result < 0)
510                 dev_err(wusbhc->dev, "Can't set host's WUSB encryption for "
511                         "port index %u to %s (value %d): %d\n", port_idx,
512                         wusb_et_name(wusbhc->ccm1_etd->bEncryptionType),
513                         wusbhc->ccm1_etd->bEncryptionValue, result);
514 error_set_key:
515         return result;
516 }
517
518 /*
519  * Set host's GTK key
520  */
521 static int __hwahc_op_set_gtk(struct wusbhc *wusbhc, u32 tkid,
522                               const void *key, size_t key_size)
523 {
524         u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_GTK,
525                                     WUSB_KEY_INDEX_ORIGINATOR_HOST);
526
527         return __hwahc_dev_set_key(wusbhc, 0, tkid, key, key_size, key_idx);
528 }
529
530 /*
531  * Get the Wire Adapter class-specific descriptor
532  *
533  * NOTE: this descriptor comes with the big bundled configuration
534  *       descriptor that includes the interfaces' and endpoints', so
535  *       we just look for it in the cached copy kept by the USB stack.
536  *
537  * NOTE2: We convert LE fields to CPU order.
538  */
539 static int wa_fill_descr(struct wahc *wa)
540 {
541         int result;
542         struct device *dev = &wa->usb_iface->dev;
543         char *itr;
544         struct usb_device *usb_dev = wa->usb_dev;
545         struct usb_descriptor_header *hdr;
546         struct usb_wa_descriptor *wa_descr;
547         size_t itr_size, actconfig_idx;
548
549         actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
550                         sizeof(usb_dev->config[0]);
551         itr = usb_dev->rawdescriptors[actconfig_idx];
552         itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
553         while (itr_size >= sizeof(*hdr)) {
554                 hdr = (struct usb_descriptor_header *) itr;
555                 dev_dbg(dev, "Extra device descriptor: "
556                         "type %02x/%u bytes @ %zu (%zu left)\n",
557                         hdr->bDescriptorType, hdr->bLength,
558                         (itr - usb_dev->rawdescriptors[actconfig_idx]),
559                         itr_size);
560                 if (hdr->bDescriptorType == USB_DT_WIRE_ADAPTER)
561                         goto found;
562                 itr += hdr->bLength;
563                 itr_size -= hdr->bLength;
564         }
565         dev_err(dev, "cannot find Wire Adapter Class descriptor\n");
566         return -ENODEV;
567
568 found:
569         result = -EINVAL;
570         if (hdr->bLength > itr_size) {  /* is it available? */
571                 dev_err(dev, "incomplete Wire Adapter Class descriptor "
572                         "(%zu bytes left, %u needed)\n",
573                         itr_size, hdr->bLength);
574                 goto error;
575         }
576         if (hdr->bLength < sizeof(*wa->wa_descr)) {
577                 dev_err(dev, "short Wire Adapter Class descriptor\n");
578                 goto error;
579         }
580         wa->wa_descr = wa_descr = (struct usb_wa_descriptor *) hdr;
581         /* Make LE fields CPU order */
582         wa_descr->bcdWAVersion = le16_to_cpu(wa_descr->bcdWAVersion);
583         wa_descr->wNumRPipes = le16_to_cpu(wa_descr->wNumRPipes);
584         wa_descr->wRPipeMaxBlock = le16_to_cpu(wa_descr->wRPipeMaxBlock);
585         if (wa_descr->bcdWAVersion > 0x0100)
586                 dev_warn(dev, "Wire Adapter v%d.%d newer than groked v1.0\n",
587                          wa_descr->bcdWAVersion & 0xff00 >> 8,
588                          wa_descr->bcdWAVersion & 0x00ff);
589         result = 0;
590 error:
591         return result;
592 }
593
594 static struct hc_driver hwahc_hc_driver = {
595         .description = "hwa-hcd",
596         .product_desc = "Wireless USB HWA host controller",
597         .hcd_priv_size = sizeof(struct hwahc) - sizeof(struct usb_hcd),
598         .irq = NULL,                    /* FIXME */
599         .flags = HCD_USB2,              /* FIXME */
600         .reset = hwahc_op_reset,
601         .start = hwahc_op_start,
602         .pci_suspend = hwahc_op_suspend,
603         .pci_resume = hwahc_op_resume,
604         .stop = hwahc_op_stop,
605         .get_frame_number = hwahc_op_get_frame_number,
606         .urb_enqueue = hwahc_op_urb_enqueue,
607         .urb_dequeue = hwahc_op_urb_dequeue,
608         .endpoint_disable = hwahc_op_endpoint_disable,
609
610         .hub_status_data = wusbhc_rh_status_data,
611         .hub_control = wusbhc_rh_control,
612         .bus_suspend = wusbhc_rh_suspend,
613         .bus_resume = wusbhc_rh_resume,
614         .start_port_reset = wusbhc_rh_start_port_reset,
615 };
616
617 static int hwahc_security_create(struct hwahc *hwahc)
618 {
619         int result;
620         struct wusbhc *wusbhc = &hwahc->wusbhc;
621         struct usb_device *usb_dev = hwahc->wa.usb_dev;
622         struct device *dev = &usb_dev->dev;
623         struct usb_security_descriptor *secd;
624         struct usb_encryption_descriptor *etd;
625         void *itr, *top;
626         size_t itr_size, needed, bytes;
627         u8 index;
628         char buf[64];
629
630         /* Find the host's security descriptors in the config descr bundle */
631         index = (usb_dev->actconfig - usb_dev->config) /
632                 sizeof(usb_dev->config[0]);
633         itr = usb_dev->rawdescriptors[index];
634         itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
635         top = itr + itr_size;
636         result = __usb_get_extra_descriptor(usb_dev->rawdescriptors[index],
637                         le16_to_cpu(usb_dev->actconfig->desc.wTotalLength),
638                         USB_DT_SECURITY, (void **) &secd);
639         if (result == -1) {
640                 dev_warn(dev, "BUG? WUSB host has no security descriptors\n");
641                 return 0;
642         }
643         needed = sizeof(*secd);
644         if (top - (void *)secd < needed) {
645                 dev_err(dev, "BUG? Not enough data to process security "
646                         "descriptor header (%zu bytes left vs %zu needed)\n",
647                         top - (void *) secd, needed);
648                 return 0;
649         }
650         needed = le16_to_cpu(secd->wTotalLength);
651         if (top - (void *)secd < needed) {
652                 dev_err(dev, "BUG? Not enough data to process security "
653                         "descriptors (%zu bytes left vs %zu needed)\n",
654                         top - (void *) secd, needed);
655                 return 0;
656         }
657         /* Walk over the sec descriptors and store CCM1's on wusbhc */
658         itr = (void *) secd + sizeof(*secd);
659         top = (void *) secd + le16_to_cpu(secd->wTotalLength);
660         index = 0;
661         bytes = 0;
662         while (itr < top) {
663                 etd = itr;
664                 if (top - itr < sizeof(*etd)) {
665                         dev_err(dev, "BUG: bad host security descriptor; "
666                                 "not enough data (%zu vs %zu left)\n",
667                                 top - itr, sizeof(*etd));
668                         break;
669                 }
670                 if (etd->bLength < sizeof(*etd)) {
671                         dev_err(dev, "BUG: bad host encryption descriptor; "
672                                 "descriptor is too short "
673                                 "(%zu vs %zu needed)\n",
674                                 (size_t)etd->bLength, sizeof(*etd));
675                         break;
676                 }
677                 itr += etd->bLength;
678                 bytes += snprintf(buf + bytes, sizeof(buf) - bytes,
679                                   "%s (0x%02x) ",
680                                   wusb_et_name(etd->bEncryptionType),
681                                   etd->bEncryptionValue);
682                 wusbhc->ccm1_etd = etd;
683         }
684         dev_info(dev, "supported encryption types: %s\n", buf);
685         if (wusbhc->ccm1_etd == NULL) {
686                 dev_err(dev, "E: host doesn't support CCM-1 crypto\n");
687                 return 0;
688         }
689         /* Pretty print what we support */
690         return 0;
691 }
692
693 static void hwahc_security_release(struct hwahc *hwahc)
694 {
695         /* nothing to do here so far... */
696 }
697
698 static int hwahc_create(struct hwahc *hwahc, struct usb_interface *iface)
699 {
700         int result;
701         struct device *dev = &iface->dev;
702         struct wusbhc *wusbhc = &hwahc->wusbhc;
703         struct wahc *wa = &hwahc->wa;
704         struct usb_device *usb_dev = interface_to_usbdev(iface);
705
706         wa->usb_dev = usb_get_dev(usb_dev);     /* bind the USB device */
707         wa->usb_iface = usb_get_intf(iface);
708         wusbhc->dev = dev;
709         wusbhc->uwb_rc = uwb_rc_get_by_grandpa(iface->dev.parent);
710         if (wusbhc->uwb_rc == NULL) {
711                 result = -ENODEV;
712                 dev_err(dev, "Cannot get associated UWB Host Controller\n");
713                 goto error_rc_get;
714         }
715         result = wa_fill_descr(wa);     /* Get the device descriptor */
716         if (result < 0)
717                 goto error_fill_descriptor;
718         if (wa->wa_descr->bNumPorts > USB_MAXCHILDREN) {
719                 dev_err(dev, "FIXME: USB_MAXCHILDREN too low for WUSB "
720                         "adapter (%u ports)\n", wa->wa_descr->bNumPorts);
721                 wusbhc->ports_max = USB_MAXCHILDREN;
722         } else {
723                 wusbhc->ports_max = wa->wa_descr->bNumPorts;
724         }
725         wusbhc->mmcies_max = wa->wa_descr->bNumMMCIEs;
726         wusbhc->start = __hwahc_op_wusbhc_start;
727         wusbhc->stop = __hwahc_op_wusbhc_stop;
728         wusbhc->mmcie_add = __hwahc_op_mmcie_add;
729         wusbhc->mmcie_rm = __hwahc_op_mmcie_rm;
730         wusbhc->dev_info_set = __hwahc_op_dev_info_set;
731         wusbhc->bwa_set = __hwahc_op_bwa_set;
732         wusbhc->set_num_dnts = __hwahc_op_set_num_dnts;
733         wusbhc->set_ptk = __hwahc_op_set_ptk;
734         wusbhc->set_gtk = __hwahc_op_set_gtk;
735         result = hwahc_security_create(hwahc);
736         if (result < 0) {
737                 dev_err(dev, "Can't initialize security: %d\n", result);
738                 goto error_security_create;
739         }
740         wa->wusb = wusbhc;      /* FIXME: ugly, need to fix */
741         result = wusbhc_create(&hwahc->wusbhc);
742         if (result < 0) {
743                 dev_err(dev, "Can't create WUSB HC structures: %d\n", result);
744                 goto error_wusbhc_create;
745         }
746         result = wa_create(&hwahc->wa, iface);
747         if (result < 0)
748                 goto error_wa_create;
749         return 0;
750
751 error_wa_create:
752         wusbhc_destroy(&hwahc->wusbhc);
753 error_wusbhc_create:
754         /* WA Descr fill allocs no resources */
755 error_security_create:
756 error_fill_descriptor:
757         uwb_rc_put(wusbhc->uwb_rc);
758 error_rc_get:
759         usb_put_intf(iface);
760         usb_put_dev(usb_dev);
761         return result;
762 }
763
764 static void hwahc_destroy(struct hwahc *hwahc)
765 {
766         struct wusbhc *wusbhc = &hwahc->wusbhc;
767
768         mutex_lock(&wusbhc->mutex);
769         __wa_destroy(&hwahc->wa);
770         wusbhc_destroy(&hwahc->wusbhc);
771         hwahc_security_release(hwahc);
772         hwahc->wusbhc.dev = NULL;
773         uwb_rc_put(wusbhc->uwb_rc);
774         usb_put_intf(hwahc->wa.usb_iface);
775         usb_put_dev(hwahc->wa.usb_dev);
776         mutex_unlock(&wusbhc->mutex);
777 }
778
779 static void hwahc_init(struct hwahc *hwahc)
780 {
781         wa_init(&hwahc->wa);
782 }
783
784 static int hwahc_probe(struct usb_interface *usb_iface,
785                        const struct usb_device_id *id)
786 {
787         int result;
788         struct usb_hcd *usb_hcd;
789         struct wusbhc *wusbhc;
790         struct hwahc *hwahc;
791         struct device *dev = &usb_iface->dev;
792
793         result = -ENOMEM;
794         usb_hcd = usb_create_hcd(&hwahc_hc_driver, &usb_iface->dev, "wusb-hwa");
795         if (usb_hcd == NULL) {
796                 dev_err(dev, "unable to allocate instance\n");
797                 goto error_alloc;
798         }
799         usb_hcd->wireless = 1;
800         usb_hcd->flags |= HCD_FLAG_SAW_IRQ;
801         wusbhc = usb_hcd_to_wusbhc(usb_hcd);
802         hwahc = container_of(wusbhc, struct hwahc, wusbhc);
803         hwahc_init(hwahc);
804         result = hwahc_create(hwahc, usb_iface);
805         if (result < 0) {
806                 dev_err(dev, "Cannot initialize internals: %d\n", result);
807                 goto error_hwahc_create;
808         }
809         result = usb_add_hcd(usb_hcd, 0, 0);
810         if (result < 0) {
811                 dev_err(dev, "Cannot add HCD: %d\n", result);
812                 goto error_add_hcd;
813         }
814         result = wusbhc_b_create(&hwahc->wusbhc);
815         if (result < 0) {
816                 dev_err(dev, "Cannot setup phase B of WUSBHC: %d\n", result);
817                 goto error_wusbhc_b_create;
818         }
819         return 0;
820
821 error_wusbhc_b_create:
822         usb_remove_hcd(usb_hcd);
823 error_add_hcd:
824         hwahc_destroy(hwahc);
825 error_hwahc_create:
826         usb_put_hcd(usb_hcd);
827 error_alloc:
828         return result;
829 }
830
831 static void hwahc_disconnect(struct usb_interface *usb_iface)
832 {
833         struct usb_hcd *usb_hcd;
834         struct wusbhc *wusbhc;
835         struct hwahc *hwahc;
836
837         usb_hcd = usb_get_intfdata(usb_iface);
838         wusbhc = usb_hcd_to_wusbhc(usb_hcd);
839         hwahc = container_of(wusbhc, struct hwahc, wusbhc);
840
841         wusbhc_b_destroy(&hwahc->wusbhc);
842         usb_remove_hcd(usb_hcd);
843         hwahc_destroy(hwahc);
844         usb_put_hcd(usb_hcd);
845 }
846
847 static struct usb_device_id hwahc_id_table[] = {
848         /* FIXME: use class labels for this */
849         { USB_INTERFACE_INFO(0xe0, 0x02, 0x01), },
850         {},
851 };
852 MODULE_DEVICE_TABLE(usb, hwahc_id_table);
853
854 static struct usb_driver hwahc_driver = {
855         .name =         "hwa-hc",
856         .probe =        hwahc_probe,
857         .disconnect =   hwahc_disconnect,
858         .id_table =     hwahc_id_table,
859 };
860
861 static int __init hwahc_driver_init(void)
862 {
863         return usb_register(&hwahc_driver);
864 }
865 module_init(hwahc_driver_init);
866
867 static void __exit hwahc_driver_exit(void)
868 {
869         usb_deregister(&hwahc_driver);
870 }
871 module_exit(hwahc_driver_exit);
872
873
874 MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
875 MODULE_DESCRIPTION("Host Wired Adapter USB Host Control Driver");
876 MODULE_LICENSE("GPL");