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