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