]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/host/hwa-hc.c
Merge remote-tracking branch 'spi/fix/clps711x' into spi-linus
[karo-tx-linux.git] / 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/slab.h>
59 #include <linux/module.h>
60 #include <linux/workqueue.h>
61 #include <linux/wait.h>
62 #include <linux/completion.h>
63 #include "../wusbcore/wa-hc.h"
64 #include "../wusbcore/wusbhc.h"
65
66 struct hwahc {
67         struct wusbhc wusbhc;   /* has to be 1st */
68         struct wahc wa;
69 };
70
71 /*
72  * FIXME should be wusbhc
73  *
74  * NOTE: we need to cache the Cluster ID because later...there is no
75  *       way to get it :)
76  */
77 static int __hwahc_set_cluster_id(struct hwahc *hwahc, u8 cluster_id)
78 {
79         int result;
80         struct wusbhc *wusbhc = &hwahc->wusbhc;
81         struct wahc *wa = &hwahc->wa;
82         struct device *dev = &wa->usb_iface->dev;
83
84         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
85                         WUSB_REQ_SET_CLUSTER_ID,
86                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
87                         cluster_id,
88                         wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
89                         NULL, 0, 1000 /* FIXME: arbitrary */);
90         if (result < 0)
91                 dev_err(dev, "Cannot set WUSB Cluster ID to 0x%02x: %d\n",
92                         cluster_id, result);
93         else
94                 wusbhc->cluster_id = cluster_id;
95         dev_info(dev, "Wireless USB Cluster ID set to 0x%02x\n", cluster_id);
96         return result;
97 }
98
99 static int __hwahc_op_set_num_dnts(struct wusbhc *wusbhc, u8 interval, u8 slots)
100 {
101         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
102         struct wahc *wa = &hwahc->wa;
103
104         return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
105                         WUSB_REQ_SET_NUM_DNTS,
106                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
107                         interval << 8 | slots,
108                         wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
109                         NULL, 0, 1000 /* FIXME: arbitrary */);
110 }
111
112 /*
113  * Reset a WUSB host controller and wait for it to complete doing it.
114  *
115  * @usb_hcd:    Pointer to WUSB Host Controller instance.
116  *
117  */
118 static int hwahc_op_reset(struct usb_hcd *usb_hcd)
119 {
120         int result;
121         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
122         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
123         struct device *dev = &hwahc->wa.usb_iface->dev;
124
125         mutex_lock(&wusbhc->mutex);
126         wa_nep_disarm(&hwahc->wa);
127         result = __wa_set_feature(&hwahc->wa, WA_RESET);
128         if (result < 0) {
129                 dev_err(dev, "error commanding HC to reset: %d\n", result);
130                 goto error_unlock;
131         }
132         result = __wa_wait_status(&hwahc->wa, WA_STATUS_RESETTING, 0);
133         if (result < 0) {
134                 dev_err(dev, "error waiting for HC to reset: %d\n", result);
135                 goto error_unlock;
136         }
137 error_unlock:
138         mutex_unlock(&wusbhc->mutex);
139         return result;
140 }
141
142 /*
143  * FIXME: break this function up
144  */
145 static int hwahc_op_start(struct usb_hcd *usb_hcd)
146 {
147         u8 addr;
148         int result;
149         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
150         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
151
152         result = -ENOSPC;
153         mutex_lock(&wusbhc->mutex);
154         addr = wusb_cluster_id_get();
155         if (addr == 0)
156                 goto error_cluster_id_get;
157         result = __hwahc_set_cluster_id(hwahc, addr);
158         if (result < 0)
159                 goto error_set_cluster_id;
160
161         usb_hcd->uses_new_polling = 1;
162         set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
163         usb_hcd->state = HC_STATE_RUNNING;
164
165         /*
166          * prevent USB core from suspending the root hub since
167          * bus_suspend and bus_resume are not yet supported.
168          */
169         pm_runtime_get_noresume(&usb_hcd->self.root_hub->dev);
170
171         result = 0;
172 out:
173         mutex_unlock(&wusbhc->mutex);
174         return result;
175
176 error_set_cluster_id:
177         wusb_cluster_id_put(wusbhc->cluster_id);
178 error_cluster_id_get:
179         goto out;
180
181 }
182
183 /*
184  * No need to abort pipes, as when this is called, all the children
185  * has been disconnected and that has done it [through
186  * usb_disable_interface() -> usb_disable_endpoint() ->
187  * hwahc_op_ep_disable() - >rpipe_ep_disable()].
188  */
189 static void hwahc_op_stop(struct usb_hcd *usb_hcd)
190 {
191         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
192
193         mutex_lock(&wusbhc->mutex);
194         wusb_cluster_id_put(wusbhc->cluster_id);
195         mutex_unlock(&wusbhc->mutex);
196 }
197
198 static int hwahc_op_get_frame_number(struct usb_hcd *usb_hcd)
199 {
200         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
201         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
202
203         dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
204                 usb_hcd, hwahc);
205         return -ENOSYS;
206 }
207
208 static int hwahc_op_urb_enqueue(struct usb_hcd *usb_hcd, struct urb *urb,
209                                 gfp_t gfp)
210 {
211         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
212         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
213
214         return wa_urb_enqueue(&hwahc->wa, urb->ep, urb, gfp);
215 }
216
217 static int hwahc_op_urb_dequeue(struct usb_hcd *usb_hcd, struct urb *urb,
218                                 int status)
219 {
220         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
221         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
222
223         return wa_urb_dequeue(&hwahc->wa, urb);
224 }
225
226 /*
227  * Release resources allocated for an endpoint
228  *
229  * If there is an associated rpipe to this endpoint, go ahead and put it.
230  */
231 static void hwahc_op_endpoint_disable(struct usb_hcd *usb_hcd,
232                                       struct usb_host_endpoint *ep)
233 {
234         struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
235         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
236
237         rpipe_ep_disable(&hwahc->wa, ep);
238 }
239
240 static int __hwahc_op_wusbhc_start(struct wusbhc *wusbhc)
241 {
242         int result;
243         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
244         struct device *dev = &hwahc->wa.usb_iface->dev;
245
246         result = __wa_set_feature(&hwahc->wa, WA_ENABLE);
247         if (result < 0) {
248                 dev_err(dev, "error commanding HC to start: %d\n", result);
249                 goto error_stop;
250         }
251         result = __wa_wait_status(&hwahc->wa, WA_ENABLE, WA_ENABLE);
252         if (result < 0) {
253                 dev_err(dev, "error waiting for HC to start: %d\n", result);
254                 goto error_stop;
255         }
256         result = wa_nep_arm(&hwahc->wa, GFP_KERNEL);
257         if (result < 0) {
258                 dev_err(dev, "cannot listen to notifications: %d\n", result);
259                 goto error_stop;
260         }
261         return result;
262
263 error_stop:
264         __wa_clear_feature(&hwahc->wa, WA_ENABLE);
265         return result;
266 }
267
268 static void __hwahc_op_wusbhc_stop(struct wusbhc *wusbhc, int delay)
269 {
270         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
271         struct wahc *wa = &hwahc->wa;
272         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
273         int ret;
274
275         ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
276                               WUSB_REQ_CHAN_STOP,
277                               USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
278                               delay * 1000,
279                               iface_no,
280                               NULL, 0, 1000 /* FIXME: arbitrary */);
281         if (ret == 0)
282                 msleep(delay);
283
284         wa_nep_disarm(&hwahc->wa);
285         __wa_stop(&hwahc->wa);
286 }
287
288 /*
289  * Set the UWB MAS allocation for the WUSB cluster
290  *
291  * @stream_index: stream to use (-1 for cancelling the allocation)
292  * @mas: mas bitmap to use
293  */
294 static int __hwahc_op_bwa_set(struct wusbhc *wusbhc, s8 stream_index,
295                               const struct uwb_mas_bm *mas)
296 {
297         int result;
298         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
299         struct wahc *wa = &hwahc->wa;
300         struct device *dev = &wa->usb_iface->dev;
301         u8 mas_le[UWB_NUM_MAS/8];
302
303         /* Set the stream index */
304         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
305                         WUSB_REQ_SET_STREAM_IDX,
306                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
307                         stream_index,
308                         wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
309                         NULL, 0, 1000 /* FIXME: arbitrary */);
310         if (result < 0) {
311                 dev_err(dev, "Cannot set WUSB stream index: %d\n", result);
312                 goto out;
313         }
314         uwb_mas_bm_copy_le(mas_le, mas);
315         /* Set the MAS allocation */
316         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
317                         WUSB_REQ_SET_WUSB_MAS,
318                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
319                         0, wa->usb_iface->cur_altsetting->desc.bInterfaceNumber,
320                         mas_le, 32, 1000 /* FIXME: arbitrary */);
321         if (result < 0)
322                 dev_err(dev, "Cannot set WUSB MAS allocation: %d\n", result);
323 out:
324         return result;
325 }
326
327 /*
328  * Add an IE to the host's MMC
329  *
330  * @interval:    See WUSB1.0[8.5.3.1]
331  * @repeat_cnt:  See WUSB1.0[8.5.3.1]
332  * @handle:      See WUSB1.0[8.5.3.1]
333  * @wuie:        Pointer to the header of the WUSB IE data to add.
334  *               MUST BE allocated in a kmalloc buffer (no stack or
335  *               vmalloc).
336  *
337  * NOTE: the format of the WUSB IEs for MMCs are different to the
338  *       normal MBOA MAC IEs (IE Id + Length in MBOA MAC vs. Length +
339  *       Id in WUSB IEs). Standards...you gotta love'em.
340  */
341 static int __hwahc_op_mmcie_add(struct wusbhc *wusbhc, u8 interval,
342                                 u8 repeat_cnt, u8 handle,
343                                 struct wuie_hdr *wuie)
344 {
345         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
346         struct wahc *wa = &hwahc->wa;
347         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
348
349         return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
350                         WUSB_REQ_ADD_MMC_IE,
351                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
352                         interval << 8 | repeat_cnt,
353                         handle << 8 | iface_no,
354                         wuie, wuie->bLength, 1000 /* FIXME: arbitrary */);
355 }
356
357 /*
358  * Remove an IE to the host's MMC
359  *
360  * @handle:      See WUSB1.0[8.5.3.1]
361  */
362 static int __hwahc_op_mmcie_rm(struct wusbhc *wusbhc, u8 handle)
363 {
364         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
365         struct wahc *wa = &hwahc->wa;
366         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
367         return usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
368                         WUSB_REQ_REMOVE_MMC_IE,
369                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
370                         0, handle << 8 | iface_no,
371                         NULL, 0, 1000 /* FIXME: arbitrary */);
372 }
373
374 /*
375  * Update device information for a given fake port
376  *
377  * @port_idx: Fake port to which device is connected (wusbhc index, not
378  *            USB port number).
379  */
380 static int __hwahc_op_dev_info_set(struct wusbhc *wusbhc,
381                                    struct wusb_dev *wusb_dev)
382 {
383         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
384         struct wahc *wa = &hwahc->wa;
385         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
386         struct hwa_dev_info *dev_info;
387         int ret;
388
389         /* fill out the Device Info buffer and send it */
390         dev_info = kzalloc(sizeof(struct hwa_dev_info), GFP_KERNEL);
391         if (!dev_info)
392                 return -ENOMEM;
393         uwb_mas_bm_copy_le(dev_info->bmDeviceAvailability,
394                            &wusb_dev->availability);
395         dev_info->bDeviceAddress = wusb_dev->addr;
396
397         /*
398          * If the descriptors haven't been read yet, use a default PHY
399          * rate of 53.3 Mbit/s only.  The correct value will be used
400          * when this will be called again as part of the
401          * authentication process (which occurs after the descriptors
402          * have been read).
403          */
404         if (wusb_dev->wusb_cap_descr)
405                 dev_info->wPHYRates = wusb_dev->wusb_cap_descr->wPHYRates;
406         else
407                 dev_info->wPHYRates = cpu_to_le16(USB_WIRELESS_PHY_53);
408
409         ret = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
410                         WUSB_REQ_SET_DEV_INFO,
411                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
412                         0, wusb_dev->port_idx << 8 | iface_no,
413                         dev_info, sizeof(struct hwa_dev_info),
414                         1000 /* FIXME: arbitrary */);
415         kfree(dev_info);
416         return ret;
417 }
418
419 /*
420  * Set host's idea of which encryption (and key) method to use when
421  * talking to ad evice on a given port.
422  *
423  * If key is NULL, it means disable encryption for that "virtual port"
424  * (used when we disconnect).
425  */
426 static int __hwahc_dev_set_key(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
427                                const void *key, size_t key_size,
428                                u8 key_idx)
429 {
430         int result = -ENOMEM;
431         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
432         struct wahc *wa = &hwahc->wa;
433         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
434         struct usb_key_descriptor *keyd;
435         size_t keyd_len;
436
437         keyd_len = sizeof(*keyd) + key_size;
438         keyd = kzalloc(keyd_len, GFP_KERNEL);
439         if (keyd == NULL)
440                 return -ENOMEM;
441
442         keyd->bLength = keyd_len;
443         keyd->bDescriptorType = USB_DT_KEY;
444         keyd->tTKID[0] = (tkid >>  0) & 0xff;
445         keyd->tTKID[1] = (tkid >>  8) & 0xff;
446         keyd->tTKID[2] = (tkid >> 16) & 0xff;
447         memcpy(keyd->bKeyData, key, key_size);
448
449         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
450                         USB_REQ_SET_DESCRIPTOR,
451                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
452                         USB_DT_KEY << 8 | key_idx,
453                         port_idx << 8 | iface_no,
454                         keyd, keyd_len, 1000 /* FIXME: arbitrary */);
455
456         kzfree(keyd); /* clear keys etc. */
457         return result;
458 }
459
460 /*
461  * Set host's idea of which encryption (and key) method to use when
462  * talking to ad evice on a given port.
463  *
464  * If key is NULL, it means disable encryption for that "virtual port"
465  * (used when we disconnect).
466  */
467 static int __hwahc_op_set_ptk(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
468                               const void *key, size_t key_size)
469 {
470         int result = -ENOMEM;
471         struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
472         struct wahc *wa = &hwahc->wa;
473         u8 iface_no = wa->usb_iface->cur_altsetting->desc.bInterfaceNumber;
474         u8 encryption_value;
475
476         /* Tell the host which key to use to talk to the device */
477         if (key) {
478                 u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_PTK,
479                                             WUSB_KEY_INDEX_ORIGINATOR_HOST);
480
481                 result = __hwahc_dev_set_key(wusbhc, port_idx, tkid,
482                                              key, key_size, key_idx);
483                 if (result < 0)
484                         goto error_set_key;
485                 encryption_value = wusbhc->ccm1_etd->bEncryptionValue;
486         } else {
487                 /* FIXME: this should come from wusbhc->etd[UNSECURE].value */
488                 encryption_value = 0;
489         }
490
491         /* Set the encryption type for communicating with the device */
492         result = usb_control_msg(wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
493                         USB_REQ_SET_ENCRYPTION,
494                         USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
495                         encryption_value, port_idx << 8 | iface_no,
496                         NULL, 0, 1000 /* FIXME: arbitrary */);
497         if (result < 0)
498                 dev_err(wusbhc->dev, "Can't set host's WUSB encryption for "
499                         "port index %u to %s (value %d): %d\n", port_idx,
500                         wusb_et_name(wusbhc->ccm1_etd->bEncryptionType),
501                         wusbhc->ccm1_etd->bEncryptionValue, result);
502 error_set_key:
503         return result;
504 }
505
506 /*
507  * Set host's GTK key
508  */
509 static int __hwahc_op_set_gtk(struct wusbhc *wusbhc, u32 tkid,
510                               const void *key, size_t key_size)
511 {
512         u8 key_idx = wusb_key_index(0, WUSB_KEY_INDEX_TYPE_GTK,
513                                     WUSB_KEY_INDEX_ORIGINATOR_HOST);
514
515         return __hwahc_dev_set_key(wusbhc, 0, tkid, key, key_size, key_idx);
516 }
517
518 /*
519  * Get the Wire Adapter class-specific descriptor
520  *
521  * NOTE: this descriptor comes with the big bundled configuration
522  *       descriptor that includes the interfaces' and endpoints', so
523  *       we just look for it in the cached copy kept by the USB stack.
524  *
525  * NOTE2: We convert LE fields to CPU order.
526  */
527 static int wa_fill_descr(struct wahc *wa)
528 {
529         int result;
530         struct device *dev = &wa->usb_iface->dev;
531         char *itr;
532         struct usb_device *usb_dev = wa->usb_dev;
533         struct usb_descriptor_header *hdr;
534         struct usb_wa_descriptor *wa_descr;
535         size_t itr_size, actconfig_idx;
536
537         actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
538                         sizeof(usb_dev->config[0]);
539         itr = usb_dev->rawdescriptors[actconfig_idx];
540         itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
541         while (itr_size >= sizeof(*hdr)) {
542                 hdr = (struct usb_descriptor_header *) itr;
543                 dev_dbg(dev, "Extra device descriptor: "
544                         "type %02x/%u bytes @ %zu (%zu left)\n",
545                         hdr->bDescriptorType, hdr->bLength,
546                         (itr - usb_dev->rawdescriptors[actconfig_idx]),
547                         itr_size);
548                 if (hdr->bDescriptorType == USB_DT_WIRE_ADAPTER)
549                         goto found;
550                 itr += hdr->bLength;
551                 itr_size -= hdr->bLength;
552         }
553         dev_err(dev, "cannot find Wire Adapter Class descriptor\n");
554         return -ENODEV;
555
556 found:
557         result = -EINVAL;
558         if (hdr->bLength > itr_size) {  /* is it available? */
559                 dev_err(dev, "incomplete Wire Adapter Class descriptor "
560                         "(%zu bytes left, %u needed)\n",
561                         itr_size, hdr->bLength);
562                 goto error;
563         }
564         if (hdr->bLength < sizeof(*wa->wa_descr)) {
565                 dev_err(dev, "short Wire Adapter Class descriptor\n");
566                 goto error;
567         }
568         wa->wa_descr = wa_descr = (struct usb_wa_descriptor *) hdr;
569         /* Make LE fields CPU order */
570         wa_descr->bcdWAVersion = le16_to_cpu(wa_descr->bcdWAVersion);
571         wa_descr->wNumRPipes = le16_to_cpu(wa_descr->wNumRPipes);
572         wa_descr->wRPipeMaxBlock = le16_to_cpu(wa_descr->wRPipeMaxBlock);
573         if (wa_descr->bcdWAVersion > 0x0100)
574                 dev_warn(dev, "Wire Adapter v%d.%d newer than groked v1.0\n",
575                          wa_descr->bcdWAVersion & 0xff00 >> 8,
576                          wa_descr->bcdWAVersion & 0x00ff);
577         result = 0;
578 error:
579         return result;
580 }
581
582 static struct hc_driver hwahc_hc_driver = {
583         .description = "hwa-hcd",
584         .product_desc = "Wireless USB HWA host controller",
585         .hcd_priv_size = sizeof(struct hwahc) - sizeof(struct usb_hcd),
586         .irq = NULL,                    /* FIXME */
587         .flags = HCD_USB25,
588         .reset = hwahc_op_reset,
589         .start = hwahc_op_start,
590         .stop = hwahc_op_stop,
591         .get_frame_number = hwahc_op_get_frame_number,
592         .urb_enqueue = hwahc_op_urb_enqueue,
593         .urb_dequeue = hwahc_op_urb_dequeue,
594         .endpoint_disable = hwahc_op_endpoint_disable,
595
596         .hub_status_data = wusbhc_rh_status_data,
597         .hub_control = wusbhc_rh_control,
598         .start_port_reset = wusbhc_rh_start_port_reset,
599 };
600
601 static int hwahc_security_create(struct hwahc *hwahc)
602 {
603         int result;
604         struct wusbhc *wusbhc = &hwahc->wusbhc;
605         struct usb_device *usb_dev = hwahc->wa.usb_dev;
606         struct device *dev = &usb_dev->dev;
607         struct usb_security_descriptor *secd;
608         struct usb_encryption_descriptor *etd;
609         void *itr, *top;
610         size_t itr_size, needed, bytes;
611         u8 index;
612         char buf[64];
613
614         /* Find the host's security descriptors in the config descr bundle */
615         index = (usb_dev->actconfig - usb_dev->config) /
616                 sizeof(usb_dev->config[0]);
617         itr = usb_dev->rawdescriptors[index];
618         itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
619         top = itr + itr_size;
620         result = __usb_get_extra_descriptor(usb_dev->rawdescriptors[index],
621                         le16_to_cpu(usb_dev->actconfig->desc.wTotalLength),
622                         USB_DT_SECURITY, (void **) &secd);
623         if (result == -1) {
624                 dev_warn(dev, "BUG? WUSB host has no security descriptors\n");
625                 return 0;
626         }
627         needed = sizeof(*secd);
628         if (top - (void *)secd < needed) {
629                 dev_err(dev, "BUG? Not enough data to process security "
630                         "descriptor header (%zu bytes left vs %zu needed)\n",
631                         top - (void *) secd, needed);
632                 return 0;
633         }
634         needed = le16_to_cpu(secd->wTotalLength);
635         if (top - (void *)secd < needed) {
636                 dev_err(dev, "BUG? Not enough data to process security "
637                         "descriptors (%zu bytes left vs %zu needed)\n",
638                         top - (void *) secd, needed);
639                 return 0;
640         }
641         /* Walk over the sec descriptors and store CCM1's on wusbhc */
642         itr = (void *) secd + sizeof(*secd);
643         top = (void *) secd + le16_to_cpu(secd->wTotalLength);
644         index = 0;
645         bytes = 0;
646         while (itr < top) {
647                 etd = itr;
648                 if (top - itr < sizeof(*etd)) {
649                         dev_err(dev, "BUG: bad host security descriptor; "
650                                 "not enough data (%zu vs %zu left)\n",
651                                 top - itr, sizeof(*etd));
652                         break;
653                 }
654                 if (etd->bLength < sizeof(*etd)) {
655                         dev_err(dev, "BUG: bad host encryption descriptor; "
656                                 "descriptor is too short "
657                                 "(%zu vs %zu needed)\n",
658                                 (size_t)etd->bLength, sizeof(*etd));
659                         break;
660                 }
661                 itr += etd->bLength;
662                 bytes += snprintf(buf + bytes, sizeof(buf) - bytes,
663                                   "%s (0x%02x) ",
664                                   wusb_et_name(etd->bEncryptionType),
665                                   etd->bEncryptionValue);
666                 wusbhc->ccm1_etd = etd;
667         }
668         dev_info(dev, "supported encryption types: %s\n", buf);
669         if (wusbhc->ccm1_etd == NULL) {
670                 dev_err(dev, "E: host doesn't support CCM-1 crypto\n");
671                 return 0;
672         }
673         /* Pretty print what we support */
674         return 0;
675 }
676
677 static void hwahc_security_release(struct hwahc *hwahc)
678 {
679         /* nothing to do here so far... */
680 }
681
682 static int hwahc_create(struct hwahc *hwahc, struct usb_interface *iface)
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);
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);
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         result = wusbhc_b_create(&hwahc->wusbhc);
794         if (result < 0) {
795                 dev_err(dev, "Cannot setup phase B of WUSBHC: %d\n", result);
796                 goto error_wusbhc_b_create;
797         }
798         return 0;
799
800 error_wusbhc_b_create:
801         usb_remove_hcd(usb_hcd);
802 error_add_hcd:
803         hwahc_destroy(hwahc);
804 error_hwahc_create:
805         usb_put_hcd(usb_hcd);
806 error_alloc:
807         return result;
808 }
809
810 static void hwahc_disconnect(struct usb_interface *usb_iface)
811 {
812         struct usb_hcd *usb_hcd;
813         struct wusbhc *wusbhc;
814         struct hwahc *hwahc;
815
816         usb_hcd = usb_get_intfdata(usb_iface);
817         wusbhc = usb_hcd_to_wusbhc(usb_hcd);
818         hwahc = container_of(wusbhc, struct hwahc, wusbhc);
819
820         wusbhc_b_destroy(&hwahc->wusbhc);
821         usb_remove_hcd(usb_hcd);
822         hwahc_destroy(hwahc);
823         usb_put_hcd(usb_hcd);
824 }
825
826 static struct usb_device_id hwahc_id_table[] = {
827         /* FIXME: use class labels for this */
828         { USB_INTERFACE_INFO(0xe0, 0x02, 0x01), },
829         {},
830 };
831 MODULE_DEVICE_TABLE(usb, hwahc_id_table);
832
833 static struct usb_driver hwahc_driver = {
834         .name =         "hwa-hc",
835         .probe =        hwahc_probe,
836         .disconnect =   hwahc_disconnect,
837         .id_table =     hwahc_id_table,
838 };
839
840 module_usb_driver(hwahc_driver);
841
842 MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
843 MODULE_DESCRIPTION("Host Wired Adapter USB Host Control Driver");
844 MODULE_LICENSE("GPL");