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