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