2 * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
3 * Device Connect handling
5 * Copyright (C) 2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
24 * FIXME: this file needs to be broken up, it's grown too big
27 * WUSB1.0[7.1, 7.5.1, ]
29 * WUSB device connection is kind of messy. Some background:
31 * When a device wants to connect it scans the UWB radio channels
32 * looking for a WUSB Channel; a WUSB channel is defined by MMCs
33 * (Micro Managed Commands or something like that) [see
34 * Design-overview for more on this] .
36 * So, device scans the radio, finds MMCs and thus a host and checks
37 * when the next DNTS is. It sends a Device Notification Connect
38 * (DN_Connect); the host picks it up (through nep.c and notif.c, ends
39 * up in wusb_devconnect_ack(), which creates a wusb_dev structure in
40 * wusbhc->port[port_number].wusb_dev), assigns an unauth address
41 * to the device (this means from 0x80 to 0xfe) and sends, in the MMC
42 * a Connect Ack Information Element (ConnAck IE).
44 * So now the device now has a WUSB address. From now on, we use
45 * that to talk to it in the RPipes.
49 * - We use the the as device address the port number where it is
50 * connected (port 0 doesn't exist). For unauth, it is 128 + that.
54 * This file contains the logic for doing that--entry points:
56 * wusb_devconnect_ack() Ack a device until _acked() called.
57 * Called by notif.c:wusb_handle_dn_connect()
58 * when a DN_Connect is received.
60 * wusbhc_devconnect_auth() Called by rh.c:wusbhc_rh_port_reset() when
61 * doing the device connect sequence.
63 * wusb_devconnect_acked() Ack done, release resources.
65 * wusb_handle_dn_alive() Called by notif.c:wusb_handle_dn()
66 * for processing a DN_Alive pong from a device.
68 * wusb_handle_dn_disconnect()Called by notif.c:wusb_handle_dn() to
69 * process a disconenct request from a
72 * wusb_dev_reset() Called by rh.c:wusbhc_rh_port_reset() when
75 * __wusb_dev_disable() Called by rh.c:wusbhc_rh_clear_port_feat() when
78 * wusb_devconnect_create() Called when creating the host by
79 * lc.c:wusbhc_create().
81 * wusb_devconnect_destroy() Cleanup called removing the host. Called
82 * by lc.c:wusbhc_destroy().
84 * Each Wireless USB host maintains a list of DN_Connect requests
85 * (actually we maintain a list of pending Connect Acks, the
88 * LIFE CYCLE OF port->wusb_dev
90 * Before the @wusbhc structure put()s the reference it owns for
91 * port->wusb_dev [and clean the wusb_dev pointer], it needs to
92 * lock @wusbhc->mutex.
95 #include <linux/jiffies.h>
96 #include <linux/ctype.h>
97 #include <linux/workqueue.h>
102 #include <linux/uwb/debug.h>
104 static void wusbhc_devconnect_acked_work(struct work_struct *work);
106 static void wusb_dev_free(struct wusb_dev *wusb_dev)
109 kfree(wusb_dev->set_gtk_req);
110 usb_free_urb(wusb_dev->set_gtk_urb);
115 static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc)
117 struct wusb_dev *wusb_dev;
119 struct usb_ctrlrequest *req;
121 wusb_dev = kzalloc(sizeof(*wusb_dev), GFP_KERNEL);
122 if (wusb_dev == NULL)
125 wusb_dev->wusbhc = wusbhc;
127 INIT_WORK(&wusb_dev->devconnect_acked_work, wusbhc_devconnect_acked_work);
129 urb = usb_alloc_urb(0, GFP_KERNEL);
133 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
137 req->bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
138 req->bRequest = USB_REQ_SET_DESCRIPTOR;
139 req->wValue = cpu_to_le16(USB_DT_KEY << 8 | wusbhc->gtk_index);
141 req->wLength = cpu_to_le16(wusbhc->gtk.descr.bLength);
143 wusb_dev->set_gtk_urb = urb;
144 wusb_dev->set_gtk_req = req;
148 wusb_dev_free(wusb_dev);
154 * Using the Connect-Ack list, fill out the @wusbhc Connect-Ack WUSB IE
155 * properly so that it can be added to the MMC.
157 * We just get the @wusbhc->ca_list and fill out the first four ones or
158 * less (per-spec WUSB1.0[7.5, before T7-38). If the ConnectAck WUSB
159 * IE is not allocated, we alloc it.
161 * @wusbhc->mutex must be taken
163 static void wusbhc_fill_cack_ie(struct wusbhc *wusbhc)
166 struct wusb_dev *dev_itr;
167 struct wuie_connect_ack *cack_ie;
169 cack_ie = &wusbhc->cack_ie;
171 list_for_each_entry(dev_itr, &wusbhc->cack_list, cack_node) {
172 cack_ie->blk[cnt].CDID = dev_itr->cdid;
173 cack_ie->blk[cnt].bDeviceAddress = dev_itr->addr;
174 if (++cnt >= WUIE_ELT_MAX)
177 cack_ie->hdr.bLength = sizeof(cack_ie->hdr)
178 + cnt * sizeof(cack_ie->blk[0]);
182 * Register a new device that wants to connect
184 * A new device wants to connect, so we add it to the Connect-Ack
185 * list. We give it an address in the unauthorized range (bit 8 set);
186 * user space will have to drive authorization further on.
188 * @dev_addr: address to use for the device (which is also the port
191 * @wusbhc->mutex must be taken
193 static struct wusb_dev *wusbhc_cack_add(struct wusbhc *wusbhc,
194 struct wusb_dn_connect *dnc,
195 const char *pr_cdid, u8 port_idx)
197 struct device *dev = wusbhc->dev;
198 struct wusb_dev *wusb_dev;
199 int new_connection = wusb_dn_connect_new_connection(dnc);
203 /* Is it registered already? */
204 list_for_each_entry(wusb_dev, &wusbhc->cack_list, cack_node)
205 if (!memcmp(&wusb_dev->cdid, &dnc->CDID,
206 sizeof(wusb_dev->cdid)))
208 /* We don't have it, create an entry, register it */
209 wusb_dev = wusb_dev_alloc(wusbhc);
210 if (wusb_dev == NULL)
212 wusb_dev_init(wusb_dev);
213 wusb_dev->cdid = dnc->CDID;
214 wusb_dev->port_idx = port_idx;
217 * Devices are always available within the cluster reservation
218 * and since the hardware will take the intersection of the
219 * per-device availability and the cluster reservation, the
220 * per-device availability can simply be set to always
223 bitmap_fill(wusb_dev->availability.bm, UWB_NUM_MAS);
225 /* FIXME: handle reconnects instead of assuming connects are
227 if (1 && new_connection == 0)
229 if (new_connection) {
230 dev_addr = (port_idx + 2) | WUSB_DEV_ADDR_UNAUTH;
232 dev_info(dev, "Connecting new WUSB device to address %u, "
233 "port %u\n", dev_addr, port_idx);
235 result = wusb_set_dev_addr(wusbhc, wusb_dev, dev_addr);
239 wusb_dev->entry_ts = jiffies;
240 list_add_tail(&wusb_dev->cack_node, &wusbhc->cack_list);
241 wusbhc->cack_count++;
242 wusbhc_fill_cack_ie(wusbhc);
247 * Remove a Connect-Ack context entry from the HCs view
249 * @wusbhc->mutex must be taken
251 static void wusbhc_cack_rm(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
253 struct device *dev = wusbhc->dev;
254 d_fnstart(3, dev, "(wusbhc %p wusb_dev %p)\n", wusbhc, wusb_dev);
255 list_del_init(&wusb_dev->cack_node);
256 wusbhc->cack_count--;
257 wusbhc_fill_cack_ie(wusbhc);
258 d_fnend(3, dev, "(wusbhc %p wusb_dev %p) = void\n", wusbhc, wusb_dev);
262 * @wusbhc->mutex must be taken */
264 void wusbhc_devconnect_acked(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
266 struct device *dev = wusbhc->dev;
267 d_fnstart(3, dev, "(wusbhc %p wusb_dev %p)\n", wusbhc, wusb_dev);
268 wusbhc_cack_rm(wusbhc, wusb_dev);
269 if (wusbhc->cack_count)
270 wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
272 wusbhc_mmcie_rm(wusbhc, &wusbhc->cack_ie.hdr);
273 d_fnend(3, dev, "(wusbhc %p wusb_dev %p) = void\n", wusbhc, wusb_dev);
276 static void wusbhc_devconnect_acked_work(struct work_struct *work)
278 struct wusb_dev *wusb_dev = container_of(work, struct wusb_dev,
279 devconnect_acked_work);
280 struct wusbhc *wusbhc = wusb_dev->wusbhc;
282 mutex_lock(&wusbhc->mutex);
283 wusbhc_devconnect_acked(wusbhc, wusb_dev);
284 mutex_unlock(&wusbhc->mutex);
288 * Ack a device for connection
292 * @pr_cdid: Printable CDID...hex Use @dnc->cdid for the real deal.
294 * So we get the connect ack IE (may have been allocated already),
295 * find an empty connect block, an empty virtual port, create an
296 * address with it (see below), make it an unauth addr [bit 7 set] and
299 * Addresses: because WUSB hosts have no downstream hubs, we can do a
300 * 1:1 mapping between 'port number' and device
301 * address. This simplifies many things, as during this
302 * initial connect phase the USB stack has no knoledge of
303 * the device and hasn't assigned an address yet--we know
304 * USB's choose_address() will use the same euristics we
305 * use here, so we can assume which address will be assigned.
307 * USB stack always assigns address 1 to the root hub, so
308 * to the port number we add 2 (thus virtual port #0 is
311 * @wusbhc shall be referenced
314 void wusbhc_devconnect_ack(struct wusbhc *wusbhc, struct wusb_dn_connect *dnc,
318 struct device *dev = wusbhc->dev;
319 struct wusb_dev *wusb_dev;
320 struct wusb_port *port;
321 unsigned idx, devnum;
323 d_fnstart(3, dev, "(%p, %p, %s)\n", wusbhc, dnc, pr_cdid);
324 mutex_lock(&wusbhc->mutex);
326 /* Check we are not handling it already */
327 for (idx = 0; idx < wusbhc->ports_max; idx++) {
328 port = wusb_port_by_idx(wusbhc, idx);
330 && memcmp(&dnc->CDID, &port->wusb_dev->cdid, sizeof(dnc->CDID)) == 0)
333 /* Look up those fake ports we have for a free one */
334 for (idx = 0; idx < wusbhc->ports_max; idx++) {
335 port = wusb_port_by_idx(wusbhc, idx);
336 if ((port->status & USB_PORT_STAT_POWER)
337 && !(port->status & USB_PORT_STAT_CONNECTION))
340 if (idx >= wusbhc->ports_max) {
341 dev_err(dev, "Host controller can't connect more devices "
342 "(%u already connected); device %s rejected\n",
343 wusbhc->ports_max, pr_cdid);
344 /* NOTE: we could send a WUIE_Disconnect here, but we haven't
345 * event acked, so the device will eventually timeout the
346 * connection, right? */
352 /* Make sure we are using no crypto on that "virtual port" */
353 wusbhc->set_ptk(wusbhc, idx, 0, NULL, 0);
355 /* Grab a filled in Connect-Ack context, fill out the
356 * Connect-Ack Wireless USB IE, set the MMC */
357 wusb_dev = wusbhc_cack_add(wusbhc, dnc, pr_cdid, idx);
358 if (wusb_dev == NULL)
360 result = wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
363 /* Give the device at least 2ms (WUSB1.0[7.5.1p3]), let's do
364 * three for a good measure */
366 port->wusb_dev = wusb_dev;
367 port->status |= USB_PORT_STAT_CONNECTION;
368 port->change |= USB_PORT_STAT_C_CONNECTION;
369 port->reset_count = 0;
370 /* Now the port status changed to connected; khubd will
371 * pick the change up and try to reset the port to bring it to
372 * the enabled state--so this process returns up to the stack
373 * and it calls back into wusbhc_rh_port_reset() who will call
377 mutex_unlock(&wusbhc->mutex);
378 d_fnend(3, dev, "(%p, %p, %s) = void\n", wusbhc, dnc, pr_cdid);
384 * Disconnect a Wireless USB device from its fake port
386 * Marks the port as disconnected so that khubd can pick up the change
387 * and drops our knowledge about the device.
389 * Assumes there is a device connected
391 * @port_index: zero based port number
393 * NOTE: @wusbhc->mutex is locked
395 * WARNING: From here it is not very safe to access anything hanging off
398 static void __wusbhc_dev_disconnect(struct wusbhc *wusbhc,
399 struct wusb_port *port)
401 struct device *dev = wusbhc->dev;
402 struct wusb_dev *wusb_dev = port->wusb_dev;
404 d_fnstart(3, dev, "(wusbhc %p, port %p)\n", wusbhc, port);
405 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE
406 | USB_PORT_STAT_SUSPEND | USB_PORT_STAT_RESET
407 | USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
408 port->change |= USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE;
410 if (!list_empty(&wusb_dev->cack_node))
411 list_del_init(&wusb_dev->cack_node);
412 /* For the one in cack_add() */
413 wusb_dev_put(wusb_dev);
415 port->wusb_dev = NULL;
416 /* don't reset the reset_count to zero or wusbhc_rh_port_reset will get
417 * confused! We only reset to zero when we connect a new device.
420 /* After a device disconnects, change the GTK (see [WUSB]
421 * section 6.2.11.2). */
422 wusbhc_gtk_rekey(wusbhc);
424 d_fnend(3, dev, "(wusbhc %p, port %p) = void\n", wusbhc, port);
425 /* The Wireless USB part has forgotten about the device already; now
426 * khubd's timer will pick up the disconnection and remove the USB
427 * device from the system
432 * Authenticate a device into the WUSB Cluster
434 * Called from the Root Hub code (rh.c:wusbhc_rh_port_reset()) when
435 * asking for a reset on a port that is not enabled (ie: first connect
438 * Performs the 4way handshake to allow the device to comunicate w/ the
439 * WUSB Cluster securely; once done, issue a request to the device for
440 * it to change to address 0.
442 * This mimics the reset step of Wired USB that once resetting a
443 * device, leaves the port in enabled state and the dev with the
444 * default address (0).
448 * @port_idx: port where the change happened--This is the index into
449 * the wusbhc port array, not the USB port number.
451 int wusbhc_devconnect_auth(struct wusbhc *wusbhc, u8 port_idx)
453 struct device *dev = wusbhc->dev;
454 struct wusb_port *port = wusb_port_by_idx(wusbhc, port_idx);
456 d_fnstart(3, dev, "(%p, %u)\n", wusbhc, port_idx);
457 port->status &= ~USB_PORT_STAT_RESET;
458 port->status |= USB_PORT_STAT_ENABLE;
459 port->change |= USB_PORT_STAT_C_RESET | USB_PORT_STAT_C_ENABLE;
460 d_fnend(3, dev, "(%p, %u) = 0\n", wusbhc, port_idx);
465 * Refresh the list of keep alives to emit in the MMC
467 * Some devices don't respond to keep alives unless they've been
468 * authenticated, so skip unauthenticated devices.
470 * We only publish the first four devices that have a coming timeout
471 * condition. Then when we are done processing those, we go for the
472 * next ones. We ignore the ones that have timed out already (they'll
475 * This might cause the first devices to timeout the last devices in
476 * the port array...FIXME: come up with a better algorithm?
478 * Note we can't do much about MMC's ops errors; we hope next refresh
479 * will kind of handle it.
481 * NOTE: @wusbhc->mutex is locked
483 static void __wusbhc_keep_alive(struct wusbhc *wusbhc)
485 struct device *dev = wusbhc->dev;
487 struct wusb_dev *wusb_dev;
488 struct wusb_port *wusb_port;
489 struct wuie_keep_alive *ie = &wusbhc->keep_alive_ie;
490 unsigned keep_alives, old_keep_alives;
492 old_keep_alives = ie->hdr.bLength - sizeof(ie->hdr);
495 keep_alives <= WUIE_ELT_MAX && cnt < wusbhc->ports_max;
497 unsigned tt = msecs_to_jiffies(wusbhc->trust_timeout);
499 wusb_port = wusb_port_by_idx(wusbhc, cnt);
500 wusb_dev = wusb_port->wusb_dev;
502 if (wusb_dev == NULL)
504 if (wusb_dev->usb_dev == NULL || !wusb_dev->usb_dev->authenticated)
507 if (time_after(jiffies, wusb_dev->entry_ts + tt)) {
508 dev_err(dev, "KEEPALIVE: device %u timed out\n",
510 __wusbhc_dev_disconnect(wusbhc, wusb_port);
511 } else if (time_after(jiffies, wusb_dev->entry_ts + tt/2)) {
512 /* Approaching timeout cut out, need to refresh */
513 ie->bDeviceAddress[keep_alives++] = wusb_dev->addr;
516 if (keep_alives & 0x1) /* pad to even number ([WUSB] section 7.5.9) */
517 ie->bDeviceAddress[keep_alives++] = 0x7f;
518 ie->hdr.bLength = sizeof(ie->hdr) +
519 keep_alives*sizeof(ie->bDeviceAddress[0]);
521 wusbhc_mmcie_set(wusbhc, 10, 5, &ie->hdr);
522 else if (old_keep_alives != 0)
523 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
527 * Do a run through all devices checking for timeouts
529 static void wusbhc_keep_alive_run(struct work_struct *ws)
531 struct delayed_work *dw =
532 container_of(ws, struct delayed_work, work);
533 struct wusbhc *wusbhc =
534 container_of(dw, struct wusbhc, keep_alive_timer);
536 d_fnstart(5, wusbhc->dev, "(wusbhc %p)\n", wusbhc);
537 if (wusbhc->active) {
538 mutex_lock(&wusbhc->mutex);
539 __wusbhc_keep_alive(wusbhc);
540 mutex_unlock(&wusbhc->mutex);
541 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
542 (wusbhc->trust_timeout * CONFIG_HZ)/1000/2);
544 d_fnend(5, wusbhc->dev, "(wusbhc %p) = void\n", wusbhc);
549 * Find the wusb_dev from its device address.
551 * The device can be found directly from the address (see
552 * wusb_cack_add() for where the device address is set to port_idx
553 * +2), except when the address is zero.
555 static struct wusb_dev *wusbhc_find_dev_by_addr(struct wusbhc *wusbhc, u8 addr)
559 if (addr == 0xff) /* unconnected */
563 int port = (addr & ~0x80) - 2;
564 if (port < 0 || port >= wusbhc->ports_max)
566 return wusb_port_by_idx(wusbhc, port)->wusb_dev;
569 /* Look for the device with address 0. */
570 for (p = 0; p < wusbhc->ports_max; p++) {
571 struct wusb_dev *wusb_dev = wusb_port_by_idx(wusbhc, p)->wusb_dev;
572 if (wusb_dev && wusb_dev->addr == addr)
579 * Handle a DN_Alive notification (WUSB1.0[7.6.1])
581 * This just updates the device activity timestamp and then refreshes
584 * @wusbhc shall be referenced and unlocked
586 static void wusbhc_handle_dn_alive(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
588 struct device *dev = wusbhc->dev;
590 d_printf(2, dev, "DN ALIVE: device 0x%02x pong\n", wusb_dev->addr);
592 mutex_lock(&wusbhc->mutex);
593 wusb_dev->entry_ts = jiffies;
594 __wusbhc_keep_alive(wusbhc);
595 mutex_unlock(&wusbhc->mutex);
599 * Handle a DN_Connect notification (WUSB1.0[7.6.1])
603 * @size: Size of the buffer where the notification resides; if the
604 * notification data suggests there should be more data than
605 * available, an error will be signaled and the whole buffer
608 * @wusbhc->mutex shall be held
610 static void wusbhc_handle_dn_connect(struct wusbhc *wusbhc,
611 struct wusb_dn_hdr *dn_hdr,
614 struct device *dev = wusbhc->dev;
615 struct wusb_dn_connect *dnc;
616 char pr_cdid[WUSB_CKHDID_STRSIZE];
617 static const char *beacon_behaviour[] = {
624 d_fnstart(3, dev, "(%p, %p, %zu)\n", wusbhc, dn_hdr, size);
625 if (size < sizeof(*dnc)) {
626 dev_err(dev, "DN CONNECT: short notification (%zu < %zu)\n",
631 dnc = container_of(dn_hdr, struct wusb_dn_connect, hdr);
632 ckhdid_printf(pr_cdid, sizeof(pr_cdid), &dnc->CDID);
633 dev_info(dev, "DN CONNECT: device %s @ %x (%s) wants to %s\n",
635 wusb_dn_connect_prev_dev_addr(dnc),
636 beacon_behaviour[wusb_dn_connect_beacon_behavior(dnc)],
637 wusb_dn_connect_new_connection(dnc) ? "connect" : "reconnect");
638 /* ACK the connect */
639 wusbhc_devconnect_ack(wusbhc, dnc, pr_cdid);
641 d_fnend(3, dev, "(%p, %p, %zu) = void\n",
642 wusbhc, dn_hdr, size);
647 * Handle a DN_Disconnect notification (WUSB1.0[7.6.1])
649 * Device is going down -- do the disconnect.
651 * @wusbhc shall be referenced and unlocked
653 static void wusbhc_handle_dn_disconnect(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
655 struct device *dev = wusbhc->dev;
657 dev_info(dev, "DN DISCONNECT: device 0x%02x going down\n", wusb_dev->addr);
659 mutex_lock(&wusbhc->mutex);
660 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, wusb_dev->port_idx));
661 mutex_unlock(&wusbhc->mutex);
665 * Reset a WUSB device on a HWA
668 * @port_idx Index of the port where the device is
670 * In Wireless USB, a reset is more or less equivalent to a full
671 * disconnect; so we just do a full disconnect and send the device a
672 * Device Reset IE (WUSB1.0[7.5.11]) giving it a few millisecs (6 MMCs).
674 * @wusbhc should be refcounted and unlocked
676 int wusbhc_dev_reset(struct wusbhc *wusbhc, u8 port_idx)
679 struct device *dev = wusbhc->dev;
680 struct wusb_dev *wusb_dev;
681 struct wuie_reset *ie;
683 d_fnstart(3, dev, "(%p, %u)\n", wusbhc, port_idx);
684 mutex_lock(&wusbhc->mutex);
686 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
687 if (wusb_dev == NULL) {
688 /* reset no device? ignore */
689 dev_dbg(dev, "RESET: no device at port %u, ignoring\n",
694 ie = kzalloc(sizeof(*ie), GFP_KERNEL);
697 ie->hdr.bLength = sizeof(ie->hdr) + sizeof(ie->CDID);
698 ie->hdr.bIEIdentifier = WUIE_ID_RESET_DEVICE;
699 ie->CDID = wusb_dev->cdid;
700 result = wusbhc_mmcie_set(wusbhc, 0xff, 6, &ie->hdr);
702 dev_err(dev, "RESET: cant's set MMC: %d\n", result);
705 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
707 /* 120ms, hopefully 6 MMCs (FIXME) */
709 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
713 mutex_unlock(&wusbhc->mutex);
714 d_fnend(3, dev, "(%p, %u) = %d\n", wusbhc, port_idx, result);
719 * Handle a Device Notification coming a host
721 * The Device Notification comes from a host (HWA, DWA or WHCI)
722 * wrapped in a set of headers. Somebody else has peeled off those
723 * headers for us and we just get one Device Notifications.
725 * Invalid DNs (e.g., too short) are discarded.
727 * @wusbhc shall be referenced
730 * - implement priorities as in WUSB1.0[Table 7-55]?
732 void wusbhc_handle_dn(struct wusbhc *wusbhc, u8 srcaddr,
733 struct wusb_dn_hdr *dn_hdr, size_t size)
735 struct device *dev = wusbhc->dev;
736 struct wusb_dev *wusb_dev;
738 d_fnstart(3, dev, "(%p, %p)\n", wusbhc, dn_hdr);
740 if (size < sizeof(struct wusb_dn_hdr)) {
741 dev_err(dev, "DN data shorter than DN header (%d < %d)\n",
742 (int)size, (int)sizeof(struct wusb_dn_hdr));
746 wusb_dev = wusbhc_find_dev_by_addr(wusbhc, srcaddr);
747 if (wusb_dev == NULL && dn_hdr->bType != WUSB_DN_CONNECT) {
748 dev_dbg(dev, "ignoring DN %d from unconnected device %02x\n",
749 dn_hdr->bType, srcaddr);
753 switch (dn_hdr->bType) {
754 case WUSB_DN_CONNECT:
755 wusbhc_handle_dn_connect(wusbhc, dn_hdr, size);
758 wusbhc_handle_dn_alive(wusbhc, wusb_dev);
760 case WUSB_DN_DISCONNECT:
761 wusbhc_handle_dn_disconnect(wusbhc, wusb_dev);
763 case WUSB_DN_MASAVAILCHANGED:
766 /* FIXME: handle these DNs. */
769 /* The hardware handles these. */
772 dev_warn(dev, "unknown DN %u (%d octets) from %u\n",
773 dn_hdr->bType, (int)size, srcaddr);
776 d_fnend(3, dev, "(%p, %p) = void\n", wusbhc, dn_hdr);
779 EXPORT_SYMBOL_GPL(wusbhc_handle_dn);
782 * Disconnect a WUSB device from a the cluster
785 * @port Fake port where the device is (wusbhc index, not USB port number).
787 * In Wireless USB, a disconnect is basically telling the device he is
788 * being disconnected and forgetting about him.
790 * We send the device a Device Disconnect IE (WUSB1.0[7.5.11]) for 100
791 * ms and then keep going.
793 * We don't do much in case of error; we always pretend we disabled
794 * the port and disconnected the device. If physically the request
795 * didn't get there (many things can fail in the way there), the stack
796 * will reject the device's communication attempts.
798 * @wusbhc should be refcounted and locked
800 void __wusbhc_dev_disable(struct wusbhc *wusbhc, u8 port_idx)
803 struct device *dev = wusbhc->dev;
804 struct wusb_dev *wusb_dev;
805 struct wuie_disconnect *ie;
807 d_fnstart(3, dev, "(%p, %u)\n", wusbhc, port_idx);
809 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
810 if (wusb_dev == NULL) {
811 /* reset no device? ignore */
812 dev_dbg(dev, "DISCONNECT: no device at port %u, ignoring\n",
816 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
819 ie = kzalloc(sizeof(*ie), GFP_KERNEL);
822 ie->hdr.bLength = sizeof(*ie);
823 ie->hdr.bIEIdentifier = WUIE_ID_DEVICE_DISCONNECT;
824 ie->bDeviceAddress = wusb_dev->addr;
825 result = wusbhc_mmcie_set(wusbhc, 0, 0, &ie->hdr);
827 dev_err(dev, "DISCONNECT: can't set MMC: %d\n", result);
831 /* 120ms, hopefully 6 MMCs */
833 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
837 d_fnend(3, dev, "(%p, %u) = %d\n", wusbhc, port_idx, result);
841 static void wusb_cap_descr_printf(const unsigned level, struct device *dev,
842 const struct usb_wireless_cap_descriptor *wcd)
845 "WUSB Capability Descriptor\n"
846 " bDevCapabilityType 0x%02x\n"
847 " bmAttributes 0x%02x\n"
848 " wPhyRates 0x%04x\n"
849 " bmTFITXPowerInfo 0x%02x\n"
850 " bmFFITXPowerInfo 0x%02x\n"
851 " bmBandGroup 0x%04x\n"
852 " bReserved 0x%02x\n",
853 wcd->bDevCapabilityType,
855 le16_to_cpu(wcd->wPHYRates),
856 wcd->bmTFITXPowerInfo,
857 wcd->bmFFITXPowerInfo,
863 * Walk over the BOS descriptor, verify and grok it
865 * @usb_dev: referenced
866 * @wusb_dev: referenced and unlocked
868 * The BOS descriptor is defined at WUSB1.0[7.4.1], and it defines a
869 * "flexible" way to wrap all kinds of descriptors inside an standard
870 * descriptor (wonder why they didn't use normal descriptors,
871 * btw). Not like they lack code.
873 * At the end we go to look for the WUSB Device Capabilities
874 * (WUSB1.0[7.4.1.1]) that is wrapped in a device capability descriptor
875 * that is part of the BOS descriptor set. That tells us what does the
876 * device support (dual role, beacon type, UWB PHY rates).
878 static int wusb_dev_bos_grok(struct usb_device *usb_dev,
879 struct wusb_dev *wusb_dev,
880 struct usb_bos_descriptor *bos, size_t desc_size)
883 struct device *dev = &usb_dev->dev;
886 /* Walk over BOS capabilities, verify them */
887 itr = (void *)bos + sizeof(*bos);
888 top = itr + desc_size - sizeof(*bos);
890 struct usb_dev_cap_header *cap_hdr = itr;
893 if (top - itr < sizeof(*cap_hdr)) {
894 dev_err(dev, "Device BUG? premature end of BOS header "
895 "data [offset 0x%02x]: only %zu bytes left\n",
896 (int)(itr - (void *)bos), top - itr);
900 cap_size = cap_hdr->bLength;
901 cap_type = cap_hdr->bDevCapabilityType;
902 d_printf(4, dev, "BOS Capability: 0x%02x (%zu bytes)\n",
906 if (cap_size > top - itr) {
907 dev_err(dev, "Device BUG? premature end of BOS data "
908 "[offset 0x%02x cap %02x %zu bytes]: "
909 "only %zu bytes left\n",
910 (int)(itr - (void *)bos),
911 cap_type, cap_size, top - itr);
915 d_dump(3, dev, itr, cap_size);
917 case USB_CAP_TYPE_WIRELESS_USB:
918 if (cap_size != sizeof(*wusb_dev->wusb_cap_descr))
919 dev_err(dev, "Device BUG? WUSB Capability "
920 "descriptor is %zu bytes vs %zu "
921 "needed\n", cap_size,
922 sizeof(*wusb_dev->wusb_cap_descr));
924 wusb_dev->wusb_cap_descr = itr;
925 wusb_cap_descr_printf(3, dev, itr);
929 dev_err(dev, "BUG? Unknown BOS capability 0x%02x "
930 "(%zu bytes) at offset 0x%02x\n", cap_type,
931 cap_size, (int)(itr - (void *)bos));
941 * Add information from the BOS descriptors to the device
943 * @usb_dev: referenced
944 * @wusb_dev: referenced and unlocked
946 * So what we do is we alloc a space for the BOS descriptor of 64
947 * bytes; read the first four bytes which include the wTotalLength
948 * field (WUSB1.0[T7-26]) and if it fits in those 64 bytes, read the
949 * whole thing. If not we realloc to that size.
951 * Then we call the groking function, that will fill up
952 * wusb_dev->wusb_cap_descr, which is what we'll need later on.
954 static int wusb_dev_bos_add(struct usb_device *usb_dev,
955 struct wusb_dev *wusb_dev)
958 struct device *dev = &usb_dev->dev;
959 struct usb_bos_descriptor *bos;
960 size_t alloc_size = 32, desc_size = 4;
962 bos = kmalloc(alloc_size, GFP_KERNEL);
965 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
967 dev_err(dev, "Can't get BOS descriptor or too short: %zd\n",
969 goto error_get_descriptor;
971 desc_size = le16_to_cpu(bos->wTotalLength);
972 if (desc_size >= alloc_size) {
974 alloc_size = desc_size;
975 bos = kmalloc(alloc_size, GFP_KERNEL);
979 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
980 if (result < 0 || result != desc_size) {
981 dev_err(dev, "Can't get BOS descriptor or too short (need "
982 "%zu bytes): %zd\n", desc_size, result);
983 goto error_get_descriptor;
985 if (result < sizeof(*bos)
986 || le16_to_cpu(bos->wTotalLength) != desc_size) {
987 dev_err(dev, "Can't get BOS descriptor or too short (need "
988 "%zu bytes): %zd\n", desc_size, result);
989 goto error_get_descriptor;
991 d_printf(2, dev, "Got BOS descriptor %zd bytes, %u capabilities\n",
992 result, bos->bNumDeviceCaps);
993 d_dump(2, dev, bos, result);
994 result = wusb_dev_bos_grok(usb_dev, wusb_dev, bos, result);
1001 error_get_descriptor:
1003 wusb_dev->wusb_cap_descr = NULL;
1007 static void wusb_dev_bos_rm(struct wusb_dev *wusb_dev)
1009 kfree(wusb_dev->bos);
1010 wusb_dev->wusb_cap_descr = NULL;
1013 static struct usb_wireless_cap_descriptor wusb_cap_descr_default = {
1014 .bLength = sizeof(wusb_cap_descr_default),
1015 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1016 .bDevCapabilityType = USB_CAP_TYPE_WIRELESS_USB,
1018 .bmAttributes = USB_WIRELESS_BEACON_NONE,
1019 .wPHYRates = cpu_to_le16(USB_WIRELESS_PHY_53),
1020 .bmTFITXPowerInfo = 0,
1021 .bmFFITXPowerInfo = 0,
1022 .bmBandGroup = cpu_to_le16(0x0001), /* WUSB1.0[7.4.1] bottom */
1027 * USB stack's device addition Notifier Callback
1029 * Called from drivers/usb/core/hub.c when a new device is added; we
1030 * use this hook to perform certain WUSB specific setup work on the
1031 * new device. As well, it is the first time we can connect the
1032 * wusb_dev and the usb_dev. So we note it down in wusb_dev and take a
1033 * reference that we'll drop.
1035 * First we need to determine if the device is a WUSB device (else we
1036 * ignore it). For that we use the speed setting (USB_SPEED_VARIABLE)
1037 * [FIXME: maybe we'd need something more definitive]. If so, we track
1038 * it's usb_busd and from there, the WUSB HC.
1040 * Because all WUSB HCs are contained in a 'struct wusbhc', voila, we
1041 * get the wusbhc for the device.
1043 * We have a reference on @usb_dev (as we are called at the end of its
1046 * NOTE: @usb_dev locked
1048 static void wusb_dev_add_ncb(struct usb_device *usb_dev)
1051 struct wusb_dev *wusb_dev;
1052 struct wusbhc *wusbhc;
1053 struct device *dev = &usb_dev->dev;
1056 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
1057 return; /* skip non wusb and wusb RHs */
1059 d_fnstart(3, dev, "(usb_dev %p)\n", usb_dev);
1061 wusbhc = wusbhc_get_by_usb_dev(usb_dev);
1064 mutex_lock(&wusbhc->mutex);
1065 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, usb_dev);
1066 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
1067 mutex_unlock(&wusbhc->mutex);
1068 if (wusb_dev == NULL)
1070 wusb_dev->usb_dev = usb_get_dev(usb_dev);
1071 usb_dev->wusb_dev = wusb_dev_get(wusb_dev);
1072 result = wusb_dev_sec_add(wusbhc, usb_dev, wusb_dev);
1074 dev_err(dev, "Cannot enable security: %d\n", result);
1077 /* Now query the device for it's BOS and attach it to wusb_dev */
1078 result = wusb_dev_bos_add(usb_dev, wusb_dev);
1080 dev_err(dev, "Cannot get BOS descriptors: %d\n", result);
1083 result = wusb_dev_sysfs_add(wusbhc, usb_dev, wusb_dev);
1085 goto error_add_sysfs;
1087 wusb_dev_put(wusb_dev);
1090 d_fnend(3, dev, "(usb_dev %p) = void\n", usb_dev);
1093 wusb_dev_sysfs_rm(wusb_dev);
1095 wusb_dev_bos_rm(wusb_dev);
1097 wusb_dev_sec_rm(wusb_dev);
1099 mutex_lock(&wusbhc->mutex);
1100 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
1101 mutex_unlock(&wusbhc->mutex);
1106 * Undo all the steps done at connection by the notifier callback
1108 * NOTE: @usb_dev locked
1110 static void wusb_dev_rm_ncb(struct usb_device *usb_dev)
1112 struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
1114 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
1115 return; /* skip non wusb and wusb RHs */
1117 wusb_dev_sysfs_rm(wusb_dev);
1118 wusb_dev_bos_rm(wusb_dev);
1119 wusb_dev_sec_rm(wusb_dev);
1120 wusb_dev->usb_dev = NULL;
1121 usb_dev->wusb_dev = NULL;
1122 wusb_dev_put(wusb_dev);
1123 usb_put_dev(usb_dev);
1127 * Handle notifications from the USB stack (notifier call back)
1129 * This is called when the USB stack does a
1130 * usb_{bus,device}_{add,remove}() so we can do WUSB specific
1131 * handling. It is called with [for the case of
1132 * USB_DEVICE_{ADD,REMOVE} with the usb_dev locked.
1134 int wusb_usb_ncb(struct notifier_block *nb, unsigned long val,
1137 int result = NOTIFY_OK;
1140 case USB_DEVICE_ADD:
1141 wusb_dev_add_ncb(priv);
1143 case USB_DEVICE_REMOVE:
1144 wusb_dev_rm_ncb(priv);
1147 /* ignore (for now) */
1148 case USB_BUS_REMOVE:
1152 result = NOTIFY_BAD;
1158 * Return a referenced wusb_dev given a @wusbhc and @usb_dev
1160 struct wusb_dev *__wusb_dev_get_by_usb_dev(struct wusbhc *wusbhc,
1161 struct usb_device *usb_dev)
1163 struct wusb_dev *wusb_dev;
1166 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
1167 BUG_ON(port_idx > wusbhc->ports_max);
1168 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
1169 if (wusb_dev != NULL) /* ops, device is gone */
1170 wusb_dev_get(wusb_dev);
1173 EXPORT_SYMBOL_GPL(__wusb_dev_get_by_usb_dev);
1175 void wusb_dev_destroy(struct kref *_wusb_dev)
1177 struct wusb_dev *wusb_dev
1178 = container_of(_wusb_dev, struct wusb_dev, refcnt);
1179 list_del_init(&wusb_dev->cack_node);
1180 wusb_dev_free(wusb_dev);
1181 d_fnend(1, NULL, "%s (wusb_dev %p) = void\n", __func__, wusb_dev);
1183 EXPORT_SYMBOL_GPL(wusb_dev_destroy);
1186 * Create all the device connect handling infrastructure
1188 * This is basically the device info array, Connect Acknowledgement
1189 * (cack) lists, keep-alive timers (and delayed work thread).
1191 int wusbhc_devconnect_create(struct wusbhc *wusbhc)
1193 d_fnstart(3, wusbhc->dev, "(wusbhc %p)\n", wusbhc);
1195 wusbhc->keep_alive_ie.hdr.bIEIdentifier = WUIE_ID_KEEP_ALIVE;
1196 wusbhc->keep_alive_ie.hdr.bLength = sizeof(wusbhc->keep_alive_ie.hdr);
1197 INIT_DELAYED_WORK(&wusbhc->keep_alive_timer, wusbhc_keep_alive_run);
1199 wusbhc->cack_ie.hdr.bIEIdentifier = WUIE_ID_CONNECTACK;
1200 wusbhc->cack_ie.hdr.bLength = sizeof(wusbhc->cack_ie.hdr);
1201 INIT_LIST_HEAD(&wusbhc->cack_list);
1203 d_fnend(3, wusbhc->dev, "(wusbhc %p) = void\n", wusbhc);
1208 * Release all resources taken by the devconnect stuff
1210 void wusbhc_devconnect_destroy(struct wusbhc *wusbhc)
1212 d_fnstart(3, wusbhc->dev, "(wusbhc %p)\n", wusbhc);
1213 d_fnend(3, wusbhc->dev, "(wusbhc %p) = void\n", wusbhc);
1217 * wusbhc_devconnect_start - start accepting device connections
1218 * @wusbhc: the WUSB HC
1220 * Sets the Host Info IE to accept all new connections.
1222 * FIXME: This also enables the keep alives but this is not necessary
1223 * until there are connected and authenticated devices.
1225 int wusbhc_devconnect_start(struct wusbhc *wusbhc,
1226 const struct wusb_ckhdid *chid)
1228 struct device *dev = wusbhc->dev;
1229 struct wuie_host_info *hi;
1232 hi = kzalloc(sizeof(*hi), GFP_KERNEL);
1236 hi->hdr.bLength = sizeof(*hi);
1237 hi->hdr.bIEIdentifier = WUIE_ID_HOST_INFO;
1238 hi->attributes = cpu_to_le16((wusbhc->rsv->stream << 3) | WUIE_HI_CAP_ALL);
1240 result = wusbhc_mmcie_set(wusbhc, 0, 0, &hi->hdr);
1242 dev_err(dev, "Cannot add Host Info MMCIE: %d\n", result);
1243 goto error_mmcie_set;
1245 wusbhc->wuie_host_info = hi;
1247 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
1248 (wusbhc->trust_timeout*CONFIG_HZ)/1000/2);
1258 * wusbhc_devconnect_stop - stop managing connected devices
1259 * @wusbhc: the WUSB HC
1261 * Removes the Host Info IE and stops the keep alives.
1263 * FIXME: should this disconnect all devices?
1265 void wusbhc_devconnect_stop(struct wusbhc *wusbhc)
1267 cancel_delayed_work_sync(&wusbhc->keep_alive_timer);
1268 WARN_ON(!list_empty(&wusbhc->cack_list));
1270 wusbhc_mmcie_rm(wusbhc, &wusbhc->wuie_host_info->hdr);
1271 kfree(wusbhc->wuie_host_info);
1272 wusbhc->wuie_host_info = NULL;
1276 * wusb_set_dev_addr - set the WUSB device address used by the host
1277 * @wusbhc: the WUSB HC the device is connect to
1278 * @wusb_dev: the WUSB device
1279 * @addr: new device address
1281 int wusb_set_dev_addr(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev, u8 addr)
1285 wusb_dev->addr = addr;
1286 result = wusbhc->dev_info_set(wusbhc, wusb_dev);
1288 dev_err(wusbhc->dev, "device %d: failed to set device "
1289 "address\n", wusb_dev->port_idx);
1291 dev_info(wusbhc->dev, "device %d: %s addr %u\n",
1293 (addr & WUSB_DEV_ADDR_UNAUTH) ? "unauth" : "auth",