]> Pileus Git - ~andy/linux/blob - drivers/usb/host/hwa-hc.c
Merge branch 'nfsd-next' of git://linux-nfs.org/~bfields/linux
[~andy/linux] / 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/slab.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, USB_CTRL_SET_TIMEOUT);
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, USB_CTRL_SET_TIMEOUT);
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         set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
162         usb_hcd->state = HC_STATE_RUNNING;
163
164         /*
165          * prevent USB core from suspending the root hub since
166          * bus_suspend and bus_resume are not yet supported.
167          */
168         pm_runtime_get_noresume(&usb_hcd->self.root_hub->dev);
169
170         result = 0;
171 out:
172         mutex_unlock(&wusbhc->mutex);
173         return result;
174
175 error_set_cluster_id:
176         wusb_cluster_id_put(wusbhc->cluster_id);
177 error_cluster_id_get:
178         goto out;
179
180 }
181
182 /*
183  * No need to abort pipes, as when this is called, all the children
184  * has been disconnected and that has done it [through
185  * usb_disable_interface() -> usb_disable_endpoint() ->
186  * hwahc_op_ep_disable() - >rpipe_ep_disable()].
187  */
188 static void hwahc_op_stop(struct usb_hcd *usb_hcd)
189 {
190         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
191
192         mutex_lock(&wusbhc->mutex);
193         wusb_cluster_id_put(wusbhc->cluster_id);
194         mutex_unlock(&wusbhc->mutex);
195 }
196
197 static int hwahc_op_get_frame_number(struct usb_hcd *usb_hcd)
198 {
199         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
200         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
201         struct wahc *wa = &hwahc->wa;
202
203         /*
204          * We cannot query the HWA for the WUSB time since that requires sending
205          * a synchronous URB and this function can be called in_interrupt.
206          * Instead, query the USB frame number for our parent and use that.
207          */
208         return usb_get_current_frame_number(wa->usb_dev);
209 }
210
211 static int hwahc_op_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
212                                 gfp_t gfp)
213 {
214         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
215         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
216
217         return wa_urb_enqueue(&hwahc->wa, urb->ep, urb, gfp);
218 }
219
220 static int hwahc_op_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb,
221                                 int status)
222 {
223         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
224         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
225
226         return wa_urb_dequeue(&hwahc->wa, urb, status);
227 }
228
229 /*
230  * Release resources allocated for an endpoint
231  *
232  * If there is an associated rpipe to this endpoint, go ahead and put it.
233  */
234 static void hwahc_op_endpoint_disable(struct usb_hcd *usb_hcd,
235                                       struct usb_host_endpoint *ep)
236 {
237         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
238         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
239
240         rpipe_ep_disable(&hwahc->wa, ep);
241 }
242
243 static int __hwahc_op_wusbhc_start(struct wusbhc *wusbhc)
244 {
245         int result;
246         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
247         struct device *dev = &hwahc->wa.usb_iface->dev;
248
249         result = __wa_set_feature(&hwahc->wa, WA_ENABLE);
250         if (result < 0) {
251                 dev_err(dev, "error commanding HC to start: %d\n", result);
252                 goto error_stop;
253         }
254         result = __wa_wait_status(&hwahc->wa, WA_ENABLE, WA_ENABLE);
255         if (result < 0) {
256                 dev_err(dev, "error waiting for HC to start: %d\n", result);
257                 goto error_stop;
258         }
259         result = wa_nep_arm(&hwahc->wa, GFP_KERNEL);
260         if (result < 0) {
261                 dev_err(dev, "cannot listen to notifications: %d\n", result);
262                 goto error_stop;
263         }
264         return result;
265
266 error_stop:
267         __wa_clear_feature(&hwahc->wa, WA_ENABLE);
268         return result;
269 }
270
271 static void __hwahc_op_wusbhc_stop(struct wusbhc *wusbhc, int delay)
272 {
273         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
274         struct wahc *wa = &hwahc->wa;
275         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
276         int ret;
277
278         ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
279                               WUSB_REQ_CHAN_STOP,
280                               USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
281                               delay * 1000,
282                               iface_no,
283                               NULL, 0, USB_CTRL_SET_TIMEOUT);
284         if (ret == 0)
285                 msleep(delay);
286
287         wa_nep_disarm(&hwahc->wa);
288         __wa_stop(&hwahc->wa);
289 }
290
291 /*
292  * Set the UWB MAS allocation for the WUSB cluster
293  *
294  * @stream_index: stream to use (-1 for cancelling the allocation)
295  * @mas: mas bitmap to use
296  */
297 static int __hwahc_op_bwa_set(struct wusbhc *wusbhc, s8 stream_index,
298                               const struct uwb_mas_bm *mas)
299 {
300         int result;
301         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
302         struct wahc *wa = &hwahc->wa;
303         struct device *dev = &wa->usb_iface->dev;
304         u8 mas_le[UWB_NUM_MAS/8];
305
306         /* Set the stream index */
307         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
308                         WUSB_REQ_SET_STREAM_IDX,
309                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
310                         stream_index,
311                         wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
312                         NULL, 0, USB_CTRL_SET_TIMEOUT);
313         if (result < 0) {
314                 dev_err(dev, "Cannot set WUSB stream index: %d\n", result);
315                 goto out;
316         }
317         uwb_mas_bm_copy_le(mas_le, mas);
318         /* Set the MAS allocation */
319         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
320                         WUSB_REQ_SET_WUSB_MAS,
321                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
322                         0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
323                         mas_le, 32, USB_CTRL_SET_TIMEOUT);
324         if (result < 0)
325                 dev_err(dev, "Cannot set WUSB MAS allocation: %d\n", result);
326 out:
327         return result;
328 }
329
330 /*
331  * Add an IE to the host's MMC
332  *
333  * @interval:    See WUSB1.0[8.5.3.1]
334  * @repeat_cnt:  See WUSB1.0[8.5.3.1]
335  * @handle:      See WUSB1.0[8.5.3.1]
336  * @wuie:        Pointer to the header of the WUSB IE data to add.
337  *               MUST BE allocated in a kmalloc buffer (no stack or
338  *               vmalloc).
339  *
340  * NOTE: the format of the WUSB IEs for MMCs are different to the
341  *       normal MBOA MAC IEs (IE Id + Length in MBOA MAC vs. Length +
342  *       Id in WUSB IEs). Standards...you gotta love'em.
343  */
344 static int __hwahc_op_mmcie_add(struct wusbhc *wusbhc, u8 interval,
345                                 u8 repeat_cnt, u8 handle,
346                                 struct wuie_hdr *wuie)
347 {
348         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
349         struct wahc *wa = &hwahc->wa;
350         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
351
352         return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
353                         WUSB_REQ_ADD_MMC_IE,
354                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
355                         interval << 8 | repeat_cnt,
356                         handle << 8 | iface_no,
357                         wuie, wuie->bLength, USB_CTRL_SET_TIMEOUT);
358 }
359
360 /*
361  * Remove an IE to the host's MMC
362  *
363  * @handle:      See WUSB1.0[8.5.3.1]
364  */
365 static int __hwahc_op_mmcie_rm(struct wusbhc *wusbhc, u8 handle)
366 {
367         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
368         struct wahc *wa = &hwahc->wa;
369         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
370         return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
371                         WUSB_REQ_REMOVE_MMC_IE,
372                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
373                         0, handle << 8 | iface_no,
374                         NULL, 0, USB_CTRL_SET_TIMEOUT);
375 }
376
377 /*
378  * Update device information for a given fake port
379  *
380  * @port_idx: Fake port to which device is connected (wusbhc index, not
381  *            USB port number).
382  */
383 static int __hwahc_op_dev_info_set(struct wusbhc *wusbhc,
384                                    struct wusb_dev *wusb_dev)
385 {
386         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
387         struct wahc *wa = &hwahc->wa;
388         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
389         struct hwa_dev_info *dev_info;
390         int ret;
391
392         /* fill out the Device Info buffer and send it */
393         dev_info = kzalloc(sizeof(struct hwa_dev_info), GFP_KERNEL);
394         if (!dev_info)
395                 return -ENOMEM;
396         uwb_mas_bm_copy_le(dev_info->bmDeviceAvailability,
397                            &wusb_dev->availability);
398         dev_info->bDeviceAddress = wusb_dev->addr;
399
400         /*
401          * If the descriptors haven't been read yet, use a default PHY
402          * rate of 53.3 Mbit/s only.  The correct value will be used
403          * when this will be called again as part of the
404          * authentication process (which occurs after the descriptors
405          * have been read).
406          */
407         if (wusb_dev->wusb_cap_descr)
408                 dev_info->wPHYRates = wusb_dev->wusb_cap_descr->wPHYRates;
409         else
410                 dev_info->wPHYRates = cpu_to_le16(USB_WIRELESS_PHY_53);
411
412         ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
413                         WUSB_REQ_SET_DEV_INFO,
414                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
415                         0, wusb_dev->port_idx << 8 | iface_no,
416                         dev_info, sizeof(struct hwa_dev_info),
417                         USB_CTRL_SET_TIMEOUT);
418         kfree(dev_info);
419         return ret;
420 }
421
422 /*
423  * Set host's idea of which encryption (and key) method to use when
424  * talking to ad evice on a given port.
425  *
426  * If key is NULL, it means disable encryption for that "virtual port"
427  * (used when we disconnect).
428  */
429 static int __hwahc_dev_set_key(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
430                                const void *key, size_t key_size,
431                                u8 key_idx)
432 {
433         int result = -ENOMEM;
434         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
435         struct wahc *wa = &hwahc->wa;
436         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
437         struct usb_key_descriptor *keyd;
438         size_t keyd_len;
439
440         keyd_len = sizeof(*keyd) + key_size;
441         keyd = kzalloc(keyd_len, GFP_KERNEL);
442         if (keyd == NULL)
443                 return -ENOMEM;
444
445         keyd->bLength = keyd_len;
446         keyd->bDescriptorType = USB_DT_KEY;
447         keyd->tTKID[0] = (tkid >>  0) & 0xff;
448         keyd->tTKID[1] = (tkid >>  8) & 0xff;
449         keyd->tTKID[2] = (tkid >> 16) & 0xff;
450         memcpy(keyd->bKeyData, key, key_size);
451
452         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
453                         USB_REQ_SET_DESCRIPTOR,
454                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
455                         USB_DT_KEY << 8 | key_idx,
456                         port_idx << 8 | iface_no,
457                         keyd, keyd_len, USB_CTRL_SET_TIMEOUT);
458
459         kzfree(keyd); /* clear keys etc. */
460         return result;
461 }
462
463 /*
464  * Set host's idea of which encryption (and key) method to use when
465  * talking to ad evice on a given port.
466  *
467  * If key is NULL, it means disable encryption for that "virtual port"
468  * (used when we disconnect).
469  */
470 static int __hwahc_op_set_ptk(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
471                               const void *key, size_t key_size)
472 {
473         int result = -ENOMEM;
474         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
475         struct wahc *wa = &hwahc->wa;
476         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
477         u8 encryption_value;
478
479         /* Tell the host which key to use to talk to the device */
480         if (key) {
481                 u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_PTK,
482                                             WUSB_KEY_INDEX_ORIGINATOR_HOST);
483
484                 result = __hwahc_dev_set_key(wusbhc, port_idx, tkid,
485                                              key, key_size, key_idx);
486                 if (result < 0)
487                         goto error_set_key;
488                 encryption_value = wusbhc->ccm1_etd->bEncryptionValue;
489         } else {
490                 /* FIXME: this should come from wusbhc->etd[UNSECURE].value */
491                 encryption_value = 0;
492         }
493
494         /* Set the encryption type for communicating with the device */
495         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
496                         USB_REQ_SET_ENCRYPTION,
497                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
498                         encryption_value, port_idx << 8 | iface_no,
499                         NULL, 0, USB_CTRL_SET_TIMEOUT);
500         if (result < 0)
501                 dev_err(wusbhc->dev, "Can't set host's WUSB encryption for "
502                         "port index %u to %s (value %d): %d\n", port_idx,
503                         wusb_et_name(wusbhc->ccm1_etd->bEncryptionType),
504                         wusbhc->ccm1_etd->bEncryptionValue, result);
505 error_set_key:
506         return result;
507 }
508
509 /*
510  * Set host's GTK key
511  */
512 static int __hwahc_op_set_gtk(struct wusbhc *wusbhc, u32 tkid,
513                               const void *key, size_t key_size)
514 {
515         u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_GTK,
516                                     WUSB_KEY_INDEX_ORIGINATOR_HOST);
517
518         return __hwahc_dev_set_key(wusbhc, 0, tkid, key, key_size, key_idx);
519 }
520
521 /*
522  * Get the Wire Adapter class-specific descriptor
523  *
524  * NOTE: this descriptor comes with the big bundled configuration
525  *       descriptor that includes the interfaces' and endpoints', so
526  *       we just look for it in the cached copy kept by the USB stack.
527  *
528  * NOTE2: We convert LE fields to CPU order.
529  */
530 static int wa_fill_descr(struct wahc *wa)
531 {
532         int result;
533         struct device *dev = &wa->usb_iface->dev;
534         char *itr;
535         struct usb_device *usb_dev = wa->usb_dev;
536         struct usb_descriptor_header *hdr;
537         struct usb_wa_descriptor *wa_descr;
538         size_t itr_size, actconfig_idx;
539
540         actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
541                         sizeof(usb_dev->config[0]);
542         itr = usb_dev->rawdescriptors[actconfig_idx];
543         itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
544         while (itr_size >= sizeof(*hdr)) {
545                 hdr = (struct usb_descriptor_header *) itr;
546                 dev_dbg(dev, "Extra device descriptor: "
547                         "type %02x/%u bytes @ %zu (%zu left)\n",
548                         hdr->bDescriptorType, hdr->bLength,
549                         (itr - usb_dev->rawdescriptors[actconfig_idx]),
550                         itr_size);
551                 if (hdr->bDescriptorType == USB_DT_WIRE_ADAPTER)
552                         goto found;
553                 itr += hdr->bLength;
554                 itr_size -= hdr->bLength;
555         }
556         dev_err(dev, "cannot find Wire Adapter Class descriptor\n");
557         return -ENODEV;
558
559 found:
560         result = -EINVAL;
561         if (hdr->bLength > itr_size) {  /* is it available? */
562                 dev_err(dev, "incomplete Wire Adapter Class descriptor "
563                         "(%zu bytes left, %u needed)\n",
564                         itr_size, hdr->bLength);
565                 goto error;
566         }
567         if (hdr->bLength < sizeof(*wa->wa_descr)) {
568                 dev_err(dev, "short Wire Adapter Class descriptor\n");
569                 goto error;
570         }
571         wa->wa_descr = wa_descr = (struct usb_wa_descriptor *) hdr;
572         if (le16_to_cpu(wa_descr->bcdWAVersion) > 0x0100)
573                 dev_warn(dev, "Wire Adapter v%d.%d newer than groked v1.0\n",
574                          le16_to_cpu(wa_descr->bcdWAVersion) & 0xff00 >> 8,
575                          le16_to_cpu(wa_descr->bcdWAVersion) & 0x00ff);
576         result = 0;
577 error:
578         return result;
579 }
580
581 static struct hc_driver hwahc_hc_driver = {
582         .description = "hwa-hcd",
583         .product_desc = "Wireless USB HWA host controller",
584         .hcd_priv_size = sizeof(struct hwahc) - sizeof(struct usb_hcd),
585         .irq = NULL,                    /* FIXME */
586         .flags = HCD_USB25,
587         .reset = hwahc_op_reset,
588         .start = hwahc_op_start,
589         .stop = hwahc_op_stop,
590         .get_frame_number = hwahc_op_get_frame_number,
591         .urb_enqueue = hwahc_op_urb_enqueue,
592         .urb_dequeue = hwahc_op_urb_dequeue,
593         .endpoint_disable = hwahc_op_endpoint_disable,
594
595         .hub_status_data = wusbhc_rh_status_data,
596         .hub_control = wusbhc_rh_control,
597         .start_port_reset = wusbhc_rh_start_port_reset,
598 };
599
600 static int hwahc_security_create(struct hwahc *hwahc)
601 {
602         int result;
603         struct wusbhc *wusbhc = &hwahc->wusbhc;
604         struct usb_device *usb_dev = hwahc->wa.usb_dev;
605         struct device *dev = &usb_dev->dev;
606         struct usb_security_descriptor *secd;
607         struct usb_encryption_descriptor *etd;
608         void *itr, *top;
609         size_t itr_size, needed, bytes;
610         u8 index;
611         char buf[64];
612
613         /* Find the host's security descriptors in the config descr bundle */
614         index = (usb_dev->actconfig - usb_dev->config) /
615                 sizeof(usb_dev->config[0]);
616         itr = usb_dev->rawdescriptors[index];
617         itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
618         top = itr + itr_size;
619         result = __usb_get_extra_descriptor(usb_dev->rawdescriptors[index],
620                         le16_to_cpu(usb_dev->actconfig->desc.wTotalLength),
621                         USB_DT_SECURITY, (void **) &secd);
622         if (result == -1) {
623                 dev_warn(dev, "BUG? WUSB host has no security descriptors\n");
624                 return 0;
625         }
626         needed = sizeof(*secd);
627         if (top - (void *)secd < needed) {
628                 dev_err(dev, "BUG? Not enough data to process security "
629                         "descriptor header (%zu bytes left vs %zu needed)\n",
630                         top - (void *) secd, needed);
631                 return 0;
632         }
633         needed = le16_to_cpu(secd->wTotalLength);
634         if (top - (void *)secd < needed) {
635                 dev_err(dev, "BUG? Not enough data to process security "
636                         "descriptors (%zu bytes left vs %zu needed)\n",
637                         top - (void *) secd, needed);
638                 return 0;
639         }
640         /* Walk over the sec descriptors and store CCM1's on wusbhc */
641         itr = (void *) secd + sizeof(*secd);
642         top = (void *) secd + le16_to_cpu(secd->wTotalLength);
643         index = 0;
644         bytes = 0;
645         while (itr < top) {
646                 etd = itr;
647                 if (top - itr < sizeof(*etd)) {
648                         dev_err(dev, "BUG: bad host security descriptor; "
649                                 "not enough data (%zu vs %zu left)\n",
650                                 top - itr, sizeof(*etd));
651                         break;
652                 }
653                 if (etd->bLength < sizeof(*etd)) {
654                         dev_err(dev, "BUG: bad host encryption descriptor; "
655                                 "descriptor is too short "
656                                 "(%zu vs %zu needed)\n",
657                                 (size_t)etd->bLength, sizeof(*etd));
658                         break;
659                 }
660                 itr += etd->bLength;
661                 bytes += snprintf(buf + bytes, sizeof(buf) - bytes,
662                                   "%s (0x%02x) ",
663                                   wusb_et_name(etd->bEncryptionType),
664                                   etd->bEncryptionValue);
665                 wusbhc->ccm1_etd = etd;
666         }
667         dev_info(dev, "supported encryption types: %s\n", buf);
668         if (wusbhc->ccm1_etd == NULL) {
669                 dev_err(dev, "E: host doesn't support CCM-1 crypto\n");
670                 return 0;
671         }
672         /* Pretty print what we support */
673         return 0;
674 }
675
676 static void hwahc_security_release(struct hwahc *hwahc)
677 {
678         /* nothing to do here so far... */
679 }
680
681 static int hwahc_create(struct hwahc *hwahc, struct usb_interface *iface,
682         kernel_ulong_t quirks)
683 {
684         int result;
685         struct device *dev = &iface->dev;
686         struct wusbhc *wusbhc = &hwahc->wusbhc;
687         struct wahc *wa = &hwahc->wa;
688         struct usb_device *usb_dev = interface_to_usbdev(iface);
689
690         wa->usb_dev = usb_get_dev(usb_dev);     /* bind the USB device */
691         wa->usb_iface = usb_get_intf(iface);
692         wusbhc->dev = dev;
693         /* defer getting the uwb_rc handle until it is needed since it
694          * may not have been registered by the hwa_rc driver yet. */
695         wusbhc->uwb_rc = NULL;
696         result = wa_fill_descr(wa);     /* Get the device descriptor */
697         if (result < 0)
698                 goto error_fill_descriptor;
699         if (wa->wa_descr->bNumPorts > USB_MAXCHILDREN) {
700                 dev_err(dev, "FIXME: USB_MAXCHILDREN too low for WUSB "
701                         "adapter (%u ports)\n", wa->wa_descr->bNumPorts);
702                 wusbhc->ports_max = USB_MAXCHILDREN;
703         } else {
704                 wusbhc->ports_max = wa->wa_descr->bNumPorts;
705         }
706         wusbhc->mmcies_max = wa->wa_descr->bNumMMCIEs;
707         wusbhc->start = __hwahc_op_wusbhc_start;
708         wusbhc->stop = __hwahc_op_wusbhc_stop;
709         wusbhc->mmcie_add = __hwahc_op_mmcie_add;
710         wusbhc->mmcie_rm = __hwahc_op_mmcie_rm;
711         wusbhc->dev_info_set = __hwahc_op_dev_info_set;
712         wusbhc->bwa_set = __hwahc_op_bwa_set;
713         wusbhc->set_num_dnts = __hwahc_op_set_num_dnts;
714         wusbhc->set_ptk = __hwahc_op_set_ptk;
715         wusbhc->set_gtk = __hwahc_op_set_gtk;
716         result = hwahc_security_create(hwahc);
717         if (result < 0) {
718                 dev_err(dev, "Can't initialize security: %d\n", result);
719                 goto error_security_create;
720         }
721         wa->wusb = wusbhc;      /* FIXME: ugly, need to fix */
722         result = wusbhc_create(&hwahc->wusbhc);
723         if (result < 0) {
724                 dev_err(dev, "Can't create WUSB HC structures: %d\n", result);
725                 goto error_wusbhc_create;
726         }
727         result = wa_create(&hwahc->wa, iface, quirks);
728         if (result < 0)
729                 goto error_wa_create;
730         return 0;
731
732 error_wa_create:
733         wusbhc_destroy(&hwahc->wusbhc);
734 error_wusbhc_create:
735         /* WA Descr fill allocs no resources */
736 error_security_create:
737 error_fill_descriptor:
738         usb_put_intf(iface);
739         usb_put_dev(usb_dev);
740         return result;
741 }
742
743 static void hwahc_destroy(struct hwahc *hwahc)
744 {
745         struct wusbhc *wusbhc = &hwahc->wusbhc;
746
747         mutex_lock(&wusbhc->mutex);
748         __wa_destroy(&hwahc->wa);
749         wusbhc_destroy(&hwahc->wusbhc);
750         hwahc_security_release(hwahc);
751         hwahc->wusbhc.dev = NULL;
752         uwb_rc_put(wusbhc->uwb_rc);
753         usb_put_intf(hwahc->wa.usb_iface);
754         usb_put_dev(hwahc->wa.usb_dev);
755         mutex_unlock(&wusbhc->mutex);
756 }
757
758 static void hwahc_init(struct hwahc *hwahc)
759 {
760         wa_init(&hwahc->wa);
761 }
762
763 static int hwahc_probe(struct usb_interface *usb_iface,
764                        const struct usb_device_id *id)
765 {
766         int result;
767         struct usb_hcd *usb_hcd;
768         struct wusbhc *wusbhc;
769         struct hwahc *hwahc;
770         struct device *dev = &usb_iface->dev;
771
772         result = -ENOMEM;
773         usb_hcd = usb_create_hcd(&hwahc_hc_driver, &usb_iface->dev, "wusb-hwa");
774         if (usb_hcd == NULL) {
775                 dev_err(dev, "unable to allocate instance\n");
776                 goto error_alloc;
777         }
778         usb_hcd->wireless = 1;
779         usb_hcd->self.sg_tablesize = ~0;
780         wusbhc = usb_hcd_to_wusbhc(usb_hcd);
781         hwahc = container_of(wusbhc, struct hwahc, wusbhc);
782         hwahc_init(hwahc);
783         result = hwahc_create(hwahc, usb_iface, id->driver_info);
784         if (result < 0) {
785                 dev_err(dev, "Cannot initialize internals: %d\n", result);
786                 goto error_hwahc_create;
787         }
788         result = usb_add_hcd(usb_hcd, 0, 0);
789         if (result < 0) {
790                 dev_err(dev, "Cannot add HCD: %d\n", result);
791                 goto error_add_hcd;
792         }
793         device_wakeup_enable(usb_hcd->self.controller);
794         result = wusbhc_b_create(&hwahc->wusbhc);
795         if (result < 0) {
796                 dev_err(dev, "Cannot setup phase B of WUSBHC: %d\n", result);
797                 goto error_wusbhc_b_create;
798         }
799         return 0;
800
801 error_wusbhc_b_create:
802         usb_remove_hcd(usb_hcd);
803 error_add_hcd:
804         hwahc_destroy(hwahc);
805 error_hwahc_create:
806         usb_put_hcd(usb_hcd);
807 error_alloc:
808         return result;
809 }
810
811 static void hwahc_disconnect(struct usb_interface *usb_iface)
812 {
813         struct usb_hcd *usb_hcd;
814         struct wusbhc *wusbhc;
815         struct hwahc *hwahc;
816
817         usb_hcd = usb_get_intfdata(usb_iface);
818         wusbhc = usb_hcd_to_wusbhc(usb_hcd);
819         hwahc = container_of(wusbhc, struct hwahc, wusbhc);
820
821         wusbhc_b_destroy(&hwahc->wusbhc);
822         usb_remove_hcd(usb_hcd);
823         hwahc_destroy(hwahc);
824         usb_put_hcd(usb_hcd);
825 }
826
827 static struct usb_device_id hwahc_id_table[] = {
828         /* Alereon 5310 */
829         { USB_DEVICE_AND_INTERFACE_INFO(0x13dc, 0x5310, 0xe0, 0x02, 0x01),
830           .driver_info = WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC },
831         /* Alereon 5611 */
832         { USB_DEVICE_AND_INTERFACE_INFO(0x13dc, 0x5611, 0xe0, 0x02, 0x01),
833           .driver_info = WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC },
834         /* FIXME: use class labels for this */
835         { USB_INTERFACE_INFO(0xe0, 0x02, 0x01), },
836         {},
837 };
838 MODULE_DEVICE_TABLE(usb, hwahc_id_table);
839
840 static struct usb_driver hwahc_driver = {
841         .name =         "hwa-hc",
842         .probe =        hwahc_probe,
843         .disconnect =   hwahc_disconnect,
844         .id_table =     hwahc_id_table,
845 };
846
847 module_usb_driver(hwahc_driver);
848
849 MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
850 MODULE_DESCRIPTION("Host Wired Adapter USB Host Control Driver");
851 MODULE_LICENSE("GPL");