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