56 #include <linux/kernel.h>
58 #include <linux/slab.h>
59 #include <linux/module.h>
61 #include <linux/wait.h>
63 #include "../wusbcore/wa-hc.h"
64 #include "../wusbcore/wusbhc.h"
77 static int __hwahc_set_cluster_id(
struct hwahc *
hwahc,
u8 cluster_id)
81 struct wahc *wa = &hwahc->
wa;
88 wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber,
91 dev_err(dev,
"Cannot set WUSB Cluster ID to 0x%02x: %d\n",
95 dev_info(dev,
"Wireless USB Cluster ID set to 0x%02x\n", cluster_id);
101 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
102 struct wahc *wa = &hwahc->
wa;
107 interval << 8 | slots,
108 wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber,
118 static int hwahc_op_reset(
struct usb_hcd *usb_hcd)
122 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
123 struct device *dev = &hwahc->
wa.usb_iface->dev;
126 wa_nep_disarm(&hwahc->
wa);
127 result = __wa_set_feature(&hwahc->
wa,
WA_RESET);
129 dev_err(dev,
"error commanding HC to reset: %d\n", result);
134 dev_err(dev,
"error waiting for HC to reset: %d\n", result);
145 static int hwahc_op_start(
struct usb_hcd *usb_hcd)
150 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
156 goto error_cluster_id_get;
157 result = __hwahc_set_cluster_id(hwahc, addr);
159 goto error_set_cluster_id;
161 usb_hcd->uses_new_polling = 1;
162 set_bit(HCD_FLAG_POLL_RH, &usb_hcd->flags);
163 usb_hcd->state = HC_STATE_RUNNING;
169 error_set_cluster_id:
171 error_cluster_id_get:
182 static void hwahc_op_stop(
struct usb_hcd *usb_hcd)
191 static int hwahc_op_get_frame_number(
struct usb_hcd *usb_hcd)
194 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
196 dev_err(wusbhc->
dev,
"%s (%p [%p]) UNIMPLEMENTED\n", __func__,
201 static int hwahc_op_urb_enqueue(
struct usb_hcd *usb_hcd,
struct urb *
urb,
205 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
210 static int hwahc_op_urb_dequeue(
struct usb_hcd *usb_hcd,
struct urb *
urb,
214 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
224 static void hwahc_op_endpoint_disable(
struct usb_hcd *usb_hcd,
225 struct usb_host_endpoint *ep)
228 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
233 static int __hwahc_op_wusbhc_start(
struct wusbhc *wusbhc)
236 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
237 struct device *dev = &hwahc->
wa.usb_iface->dev;
241 dev_err(dev,
"error commanding HC to start: %d\n", result);
246 dev_err(dev,
"error waiting for HC to start: %d\n", result);
251 dev_err(dev,
"cannot listen to notifications: %d\n", result);
261 static void __hwahc_op_wusbhc_stop(
struct wusbhc *wusbhc,
int delay)
263 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
264 struct wahc *wa = &hwahc->
wa;
265 u8 iface_no = wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber;
277 wa_nep_disarm(&hwahc->
wa);
278 __wa_stop(&hwahc->
wa);
287 static int __hwahc_op_bwa_set(
struct wusbhc *wusbhc,
s8 stream_index,
291 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
292 struct wahc *wa = &hwahc->
wa;
301 wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber,
304 dev_err(dev,
"Cannot set WUSB stream index: %d\n", result);
307 uwb_mas_bm_copy_le(mas_le, mas);
312 0, wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber,
315 dev_err(dev,
"Cannot set WUSB MAS allocation: %d\n", result);
334 static int __hwahc_op_mmcie_add(
struct wusbhc *wusbhc,
u8 interval,
338 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
339 struct wahc *wa = &hwahc->
wa;
340 u8 iface_no = wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber;
345 interval << 8 | repeat_cnt,
346 handle << 8 | iface_no,
355 static int __hwahc_op_mmcie_rm(
struct wusbhc *wusbhc,
u8 handle)
357 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
358 struct wahc *wa = &hwahc->
wa;
359 u8 iface_no = wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber;
363 0, handle << 8 | iface_no,
373 static int __hwahc_op_dev_info_set(
struct wusbhc *wusbhc,
376 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
377 struct wahc *wa = &hwahc->
wa;
378 u8 iface_no = wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber;
405 0, wusb_dev->
port_idx << 8 | iface_no,
419 static int __hwahc_dev_set_key(
struct wusbhc *wusbhc,
u8 port_idx,
u32 tkid,
424 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
425 struct wahc *wa = &hwahc->
wa;
426 u8 iface_no = wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber;
430 keyd_len =
sizeof(*keyd) +
key_size;
437 keyd->
tTKID[0] = (tkid >> 0) & 0xff;
438 keyd->
tTKID[1] = (tkid >> 8) & 0xff;
439 keyd->
tTKID[2] = (tkid >> 16) & 0xff;
446 port_idx << 8 | iface_no,
447 keyd, keyd_len, 1000 );
460 static int __hwahc_op_set_ptk(
struct wusbhc *wusbhc,
u8 port_idx,
u32 tkid,
461 const void *key,
size_t key_size)
464 struct hwahc *hwahc =
container_of(wusbhc,
struct hwahc, wusbhc);
465 struct wahc *wa = &hwahc->
wa;
466 u8 iface_no = wa->
usb_iface->cur_altsetting->desc.bInterfaceNumber;
474 result = __hwahc_dev_set_key(wusbhc, port_idx, tkid,
475 key, key_size, key_idx);
478 encryption_value = wusbhc->
ccm1_etd->bEncryptionValue;
481 encryption_value = 0;
488 encryption_value, port_idx << 8 | iface_no,
491 dev_err(wusbhc->
dev,
"Can't set host's WUSB encryption for "
492 "port index %u to %s (value %d): %d\n", port_idx,
494 wusbhc->
ccm1_etd->bEncryptionValue, result);
502 static int __hwahc_op_set_gtk(
struct wusbhc *wusbhc,
u32 tkid,
503 const void *key,
size_t key_size)
508 return __hwahc_dev_set_key(wusbhc, 0, tkid, key, key_size, key_idx);
520 static int wa_fill_descr(
struct wahc *wa)
525 struct usb_device *usb_dev = wa->
usb_dev;
528 size_t itr_size, actconfig_idx;
530 actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
531 sizeof(usb_dev->config[0]);
532 itr = usb_dev->rawdescriptors[actconfig_idx];
533 itr_size =
le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
534 while (itr_size >=
sizeof(*hdr)) {
536 dev_dbg(dev,
"Extra device descriptor: "
537 "type %02x/%u bytes @ %zu (%zu left)\n",
539 (itr - usb_dev->rawdescriptors[actconfig_idx]),
546 dev_err(dev,
"cannot find Wire Adapter Class descriptor\n");
552 dev_err(dev,
"incomplete Wire Adapter Class descriptor "
553 "(%zu bytes left, %u needed)\n",
558 dev_err(dev,
"short Wire Adapter Class descriptor\n");
567 dev_warn(dev,
"Wire Adapter v%d.%d newer than groked v1.0\n",
575 static struct hc_driver hwahc_hc_driver = {
576 .description =
"hwa-hcd",
577 .product_desc =
"Wireless USB HWA host controller",
578 .hcd_priv_size =
sizeof(
struct hwahc) -
sizeof(
struct usb_hcd),
581 .reset = hwahc_op_reset,
582 .start = hwahc_op_start,
583 .stop = hwahc_op_stop,
584 .get_frame_number = hwahc_op_get_frame_number,
585 .urb_enqueue = hwahc_op_urb_enqueue,
586 .urb_dequeue = hwahc_op_urb_dequeue,
587 .endpoint_disable = hwahc_op_endpoint_disable,
596 static int hwahc_security_create(
struct hwahc *hwahc)
599 struct wusbhc *wusbhc = &hwahc->
wusbhc;
600 struct usb_device *usb_dev = hwahc->
wa.usb_dev;
601 struct device *dev = &usb_dev->dev;
605 size_t itr_size, needed,
bytes;
610 index = (usb_dev->actconfig - usb_dev->config) /
611 sizeof(usb_dev->config[0]);
612 itr = usb_dev->rawdescriptors[
index];
613 itr_size =
le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
614 top = itr + itr_size;
616 le16_to_cpu(usb_dev->actconfig->desc.wTotalLength),
619 dev_warn(dev,
"BUG? WUSB host has no security descriptors\n");
622 needed =
sizeof(*secd);
623 if (top - (
void *)secd < needed) {
624 dev_err(dev,
"BUG? Not enough data to process security "
625 "descriptor header (%zu bytes left vs %zu needed)\n",
626 top - (
void *) secd, needed);
630 if (top - (
void *)secd < needed) {
631 dev_err(dev,
"BUG? Not enough data to process security "
632 "descriptors (%zu bytes left vs %zu needed)\n",
633 top - (
void *) secd, needed);
637 itr = (
void *) secd +
sizeof(*secd);
643 if (top - itr <
sizeof(*etd)) {
644 dev_err(dev,
"BUG: bad host security descriptor; "
645 "not enough data (%zu vs %zu left)\n",
646 top - itr,
sizeof(*etd));
649 if (etd->
bLength <
sizeof(*etd)) {
650 dev_err(dev,
"BUG: bad host encryption descriptor; "
651 "descriptor is too short "
652 "(%zu vs %zu needed)\n",
653 (
size_t)etd->
bLength,
sizeof(*etd));
657 bytes +=
snprintf(buf + bytes,
sizeof(buf) - bytes,
663 dev_info(dev,
"supported encryption types: %s\n", buf);
665 dev_err(dev,
"E: host doesn't support CCM-1 crypto\n");
672 static void hwahc_security_release(
struct hwahc *hwahc)
677 static int hwahc_create(
struct hwahc *hwahc,
struct usb_interface *iface)
680 struct device *dev = &iface->dev;
681 struct wusbhc *wusbhc = &hwahc->
wusbhc;
682 struct wahc *wa = &hwahc->
wa;
683 struct usb_device *usb_dev = interface_to_usbdev(iface);
691 dev_err(dev,
"Cannot get associated UWB Host Controller\n");
694 result = wa_fill_descr(wa);
696 goto error_fill_descriptor;
697 if (wa->
wa_descr->bNumPorts > USB_MAXCHILDREN) {
698 dev_err(dev,
"FIXME: USB_MAXCHILDREN too low for WUSB "
699 "adapter (%u ports)\n", wa->
wa_descr->bNumPorts);
705 wusbhc->
start = __hwahc_op_wusbhc_start;
706 wusbhc->
stop = __hwahc_op_wusbhc_stop;
707 wusbhc->
mmcie_add = __hwahc_op_mmcie_add;
708 wusbhc->
mmcie_rm = __hwahc_op_mmcie_rm;
710 wusbhc->
bwa_set = __hwahc_op_bwa_set;
712 wusbhc->
set_ptk = __hwahc_op_set_ptk;
713 wusbhc->
set_gtk = __hwahc_op_set_gtk;
714 result = hwahc_security_create(hwahc);
716 dev_err(dev,
"Can't initialize security: %d\n", result);
717 goto error_security_create;
722 dev_err(dev,
"Can't create WUSB HC structures: %d\n", result);
723 goto error_wusbhc_create;
727 goto error_wa_create;
734 error_security_create:
735 error_fill_descriptor:
743 static void hwahc_destroy(
struct hwahc *hwahc)
745 struct wusbhc *wusbhc = &hwahc->
wusbhc;
750 hwahc_security_release(hwahc);
758 static void hwahc_init(
struct hwahc *hwahc)
767 struct usb_hcd *usb_hcd;
768 struct wusbhc *wusbhc;
770 struct device *dev = &usb_iface->dev;
773 usb_hcd =
usb_create_hcd(&hwahc_hc_driver, &usb_iface->dev,
"wusb-hwa");
774 if (usb_hcd ==
NULL) {
775 dev_err(dev,
"unable to allocate instance\n");
778 usb_hcd->wireless = 1;
782 result = hwahc_create(hwahc, usb_iface);
784 dev_err(dev,
"Cannot initialize internals: %d\n", result);
785 goto error_hwahc_create;
789 dev_err(dev,
"Cannot add HCD: %d\n", result);
794 dev_err(dev,
"Cannot setup phase B of WUSBHC: %d\n", result);
795 goto error_wusbhc_b_create;
799 error_wusbhc_b_create:
802 hwahc_destroy(hwahc);
809 static void hwahc_disconnect(
struct usb_interface *usb_iface)
811 struct usb_hcd *usb_hcd;
812 struct wusbhc *wusbhc;
815 usb_hcd = usb_get_intfdata(usb_iface);
821 hwahc_destroy(hwahc);
827 { USB_INTERFACE_INFO(0xe0, 0x02, 0x01), },
832 static struct usb_driver hwahc_driver = {
834 .probe = hwahc_probe,
835 .disconnect = hwahc_disconnect,
836 .id_table = hwahc_id_table,