PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / usb / wusbcore / devconnect.c
blob3b959e83b28e4446085443b0e1a94ff3350f13b9
1 /*
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
20 * 02110-1301, USA.
23 * FIXME: docs
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.
47 * ASSUMPTIONS:
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.
52 * ROADMAP:
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 * wusb_devconnect_acked() Ack done, release resources.
62 * wusb_handle_dn_alive() Called by notif.c:wusb_handle_dn()
63 * for processing a DN_Alive pong from a device.
65 * wusb_handle_dn_disconnect()Called by notif.c:wusb_handle_dn() to
66 * process a disconenct request from a
67 * device.
69 * __wusb_dev_disable() Called by rh.c:wusbhc_rh_clear_port_feat() when
70 * disabling a port.
72 * wusb_devconnect_create() Called when creating the host by
73 * lc.c:wusbhc_create().
75 * wusb_devconnect_destroy() Cleanup called removing the host. Called
76 * by lc.c:wusbhc_destroy().
78 * Each Wireless USB host maintains a list of DN_Connect requests
79 * (actually we maintain a list of pending Connect Acks, the
80 * wusbhc->ca_list).
82 * LIFE CYCLE OF port->wusb_dev
84 * Before the @wusbhc structure put()s the reference it owns for
85 * port->wusb_dev [and clean the wusb_dev pointer], it needs to
86 * lock @wusbhc->mutex.
89 #include <linux/jiffies.h>
90 #include <linux/ctype.h>
91 #include <linux/slab.h>
92 #include <linux/workqueue.h>
93 #include <linux/export.h>
94 #include "wusbhc.h"
96 static void wusbhc_devconnect_acked_work(struct work_struct *work);
98 static void wusb_dev_free(struct wusb_dev *wusb_dev)
100 kfree(wusb_dev);
103 static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc)
105 struct wusb_dev *wusb_dev;
107 wusb_dev = kzalloc(sizeof(*wusb_dev), GFP_KERNEL);
108 if (wusb_dev == NULL)
109 goto err;
111 wusb_dev->wusbhc = wusbhc;
113 INIT_WORK(&wusb_dev->devconnect_acked_work, wusbhc_devconnect_acked_work);
115 return wusb_dev;
116 err:
117 wusb_dev_free(wusb_dev);
118 return NULL;
123 * Using the Connect-Ack list, fill out the @wusbhc Connect-Ack WUSB IE
124 * properly so that it can be added to the MMC.
126 * We just get the @wusbhc->ca_list and fill out the first four ones or
127 * less (per-spec WUSB1.0[7.5, before T7-38). If the ConnectAck WUSB
128 * IE is not allocated, we alloc it.
130 * @wusbhc->mutex must be taken
132 static void wusbhc_fill_cack_ie(struct wusbhc *wusbhc)
134 unsigned cnt;
135 struct wusb_dev *dev_itr;
136 struct wuie_connect_ack *cack_ie;
138 cack_ie = &wusbhc->cack_ie;
139 cnt = 0;
140 list_for_each_entry(dev_itr, &wusbhc->cack_list, cack_node) {
141 cack_ie->blk[cnt].CDID = dev_itr->cdid;
142 cack_ie->blk[cnt].bDeviceAddress = dev_itr->addr;
143 if (++cnt >= WUIE_ELT_MAX)
144 break;
146 cack_ie->hdr.bLength = sizeof(cack_ie->hdr)
147 + cnt * sizeof(cack_ie->blk[0]);
151 * Register a new device that wants to connect
153 * A new device wants to connect, so we add it to the Connect-Ack
154 * list. We give it an address in the unauthorized range (bit 8 set);
155 * user space will have to drive authorization further on.
157 * @dev_addr: address to use for the device (which is also the port
158 * number).
160 * @wusbhc->mutex must be taken
162 static struct wusb_dev *wusbhc_cack_add(struct wusbhc *wusbhc,
163 struct wusb_dn_connect *dnc,
164 const char *pr_cdid, u8 port_idx)
166 struct device *dev = wusbhc->dev;
167 struct wusb_dev *wusb_dev;
168 int new_connection = wusb_dn_connect_new_connection(dnc);
169 u8 dev_addr;
170 int result;
172 /* Is it registered already? */
173 list_for_each_entry(wusb_dev, &wusbhc->cack_list, cack_node)
174 if (!memcmp(&wusb_dev->cdid, &dnc->CDID,
175 sizeof(wusb_dev->cdid)))
176 return wusb_dev;
177 /* We don't have it, create an entry, register it */
178 wusb_dev = wusb_dev_alloc(wusbhc);
179 if (wusb_dev == NULL)
180 return NULL;
181 wusb_dev_init(wusb_dev);
182 wusb_dev->cdid = dnc->CDID;
183 wusb_dev->port_idx = port_idx;
186 * Devices are always available within the cluster reservation
187 * and since the hardware will take the intersection of the
188 * per-device availability and the cluster reservation, the
189 * per-device availability can simply be set to always
190 * available.
192 bitmap_fill(wusb_dev->availability.bm, UWB_NUM_MAS);
194 /* FIXME: handle reconnects instead of assuming connects are
195 always new. */
196 if (1 && new_connection == 0)
197 new_connection = 1;
198 if (new_connection) {
199 dev_addr = (port_idx + 2) | WUSB_DEV_ADDR_UNAUTH;
201 dev_info(dev, "Connecting new WUSB device to address %u, "
202 "port %u\n", dev_addr, port_idx);
204 result = wusb_set_dev_addr(wusbhc, wusb_dev, dev_addr);
205 if (result < 0)
206 return NULL;
208 wusb_dev->entry_ts = jiffies;
209 list_add_tail(&wusb_dev->cack_node, &wusbhc->cack_list);
210 wusbhc->cack_count++;
211 wusbhc_fill_cack_ie(wusbhc);
213 return wusb_dev;
217 * Remove a Connect-Ack context entry from the HCs view
219 * @wusbhc->mutex must be taken
221 static void wusbhc_cack_rm(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
223 list_del_init(&wusb_dev->cack_node);
224 wusbhc->cack_count--;
225 wusbhc_fill_cack_ie(wusbhc);
229 * @wusbhc->mutex must be taken */
230 static
231 void wusbhc_devconnect_acked(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
233 wusbhc_cack_rm(wusbhc, wusb_dev);
234 if (wusbhc->cack_count)
235 wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
236 else
237 wusbhc_mmcie_rm(wusbhc, &wusbhc->cack_ie.hdr);
240 static void wusbhc_devconnect_acked_work(struct work_struct *work)
242 struct wusb_dev *wusb_dev = container_of(work, struct wusb_dev,
243 devconnect_acked_work);
244 struct wusbhc *wusbhc = wusb_dev->wusbhc;
246 mutex_lock(&wusbhc->mutex);
247 wusbhc_devconnect_acked(wusbhc, wusb_dev);
248 mutex_unlock(&wusbhc->mutex);
250 wusb_dev_put(wusb_dev);
254 * Ack a device for connection
256 * FIXME: docs
258 * @pr_cdid: Printable CDID...hex Use @dnc->cdid for the real deal.
260 * So we get the connect ack IE (may have been allocated already),
261 * find an empty connect block, an empty virtual port, create an
262 * address with it (see below), make it an unauth addr [bit 7 set] and
263 * set the MMC.
265 * Addresses: because WUSB hosts have no downstream hubs, we can do a
266 * 1:1 mapping between 'port number' and device
267 * address. This simplifies many things, as during this
268 * initial connect phase the USB stack has no knowledge of
269 * the device and hasn't assigned an address yet--we know
270 * USB's choose_address() will use the same heuristics we
271 * use here, so we can assume which address will be assigned.
273 * USB stack always assigns address 1 to the root hub, so
274 * to the port number we add 2 (thus virtual port #0 is
275 * addr #2).
277 * @wusbhc shall be referenced
279 static
280 void wusbhc_devconnect_ack(struct wusbhc *wusbhc, struct wusb_dn_connect *dnc,
281 const char *pr_cdid)
283 int result;
284 struct device *dev = wusbhc->dev;
285 struct wusb_dev *wusb_dev;
286 struct wusb_port *port;
287 unsigned idx, devnum;
289 mutex_lock(&wusbhc->mutex);
291 /* Check we are not handling it already */
292 for (idx = 0; idx < wusbhc->ports_max; idx++) {
293 port = wusb_port_by_idx(wusbhc, idx);
294 if (port->wusb_dev
295 && memcmp(&dnc->CDID, &port->wusb_dev->cdid, sizeof(dnc->CDID)) == 0)
296 goto error_unlock;
298 /* Look up those fake ports we have for a free one */
299 for (idx = 0; idx < wusbhc->ports_max; idx++) {
300 port = wusb_port_by_idx(wusbhc, idx);
301 if ((port->status & USB_PORT_STAT_POWER)
302 && !(port->status & USB_PORT_STAT_CONNECTION))
303 break;
305 if (idx >= wusbhc->ports_max) {
306 dev_err(dev, "Host controller can't connect more devices "
307 "(%u already connected); device %s rejected\n",
308 wusbhc->ports_max, pr_cdid);
309 /* NOTE: we could send a WUIE_Disconnect here, but we haven't
310 * event acked, so the device will eventually timeout the
311 * connection, right? */
312 goto error_unlock;
315 devnum = idx + 2;
317 /* Make sure we are using no crypto on that "virtual port" */
318 wusbhc->set_ptk(wusbhc, idx, 0, NULL, 0);
320 /* Grab a filled in Connect-Ack context, fill out the
321 * Connect-Ack Wireless USB IE, set the MMC */
322 wusb_dev = wusbhc_cack_add(wusbhc, dnc, pr_cdid, idx);
323 if (wusb_dev == NULL)
324 goto error_unlock;
325 result = wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
326 if (result < 0)
327 goto error_unlock;
328 /* Give the device at least 2ms (WUSB1.0[7.5.1p3]), let's do
329 * three for a good measure */
330 msleep(3);
331 port->wusb_dev = wusb_dev;
332 port->status |= USB_PORT_STAT_CONNECTION;
333 port->change |= USB_PORT_STAT_C_CONNECTION;
334 /* Now the port status changed to connected; khubd will
335 * pick the change up and try to reset the port to bring it to
336 * the enabled state--so this process returns up to the stack
337 * and it calls back into wusbhc_rh_port_reset().
339 error_unlock:
340 mutex_unlock(&wusbhc->mutex);
341 return;
346 * Disconnect a Wireless USB device from its fake port
348 * Marks the port as disconnected so that khubd can pick up the change
349 * and drops our knowledge about the device.
351 * Assumes there is a device connected
353 * @port_index: zero based port number
355 * NOTE: @wusbhc->mutex is locked
357 * WARNING: From here it is not very safe to access anything hanging off
358 * wusb_dev
360 static void __wusbhc_dev_disconnect(struct wusbhc *wusbhc,
361 struct wusb_port *port)
363 struct wusb_dev *wusb_dev = port->wusb_dev;
365 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE
366 | USB_PORT_STAT_SUSPEND | USB_PORT_STAT_RESET
367 | USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
368 port->change |= USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE;
369 if (wusb_dev) {
370 dev_dbg(wusbhc->dev, "disconnecting device from port %d\n", wusb_dev->port_idx);
371 if (!list_empty(&wusb_dev->cack_node))
372 list_del_init(&wusb_dev->cack_node);
373 /* For the one in cack_add() */
374 wusb_dev_put(wusb_dev);
376 port->wusb_dev = NULL;
378 /* After a device disconnects, change the GTK (see [WUSB]
379 * section 6.2.11.2). */
380 if (wusbhc->active)
381 wusbhc_gtk_rekey(wusbhc);
383 /* The Wireless USB part has forgotten about the device already; now
384 * khubd's timer will pick up the disconnection and remove the USB
385 * device from the system
390 * Refresh the list of keep alives to emit in the MMC
392 * We only publish the first four devices that have a coming timeout
393 * condition. Then when we are done processing those, we go for the
394 * next ones. We ignore the ones that have timed out already (they'll
395 * be purged).
397 * This might cause the first devices to timeout the last devices in
398 * the port array...FIXME: come up with a better algorithm?
400 * Note we can't do much about MMC's ops errors; we hope next refresh
401 * will kind of handle it.
403 * NOTE: @wusbhc->mutex is locked
405 static void __wusbhc_keep_alive(struct wusbhc *wusbhc)
407 struct device *dev = wusbhc->dev;
408 unsigned cnt;
409 struct wusb_dev *wusb_dev;
410 struct wusb_port *wusb_port;
411 struct wuie_keep_alive *ie = &wusbhc->keep_alive_ie;
412 unsigned keep_alives, old_keep_alives;
414 old_keep_alives = ie->hdr.bLength - sizeof(ie->hdr);
415 keep_alives = 0;
416 for (cnt = 0;
417 keep_alives < WUIE_ELT_MAX && cnt < wusbhc->ports_max;
418 cnt++) {
419 unsigned tt = msecs_to_jiffies(wusbhc->trust_timeout);
421 wusb_port = wusb_port_by_idx(wusbhc, cnt);
422 wusb_dev = wusb_port->wusb_dev;
424 if (wusb_dev == NULL)
425 continue;
426 if (wusb_dev->usb_dev == NULL)
427 continue;
429 if (time_after(jiffies, wusb_dev->entry_ts + tt)) {
430 dev_err(dev, "KEEPALIVE: device %u timed out\n",
431 wusb_dev->addr);
432 __wusbhc_dev_disconnect(wusbhc, wusb_port);
433 } else if (time_after(jiffies, wusb_dev->entry_ts + tt/3)) {
434 /* Approaching timeout cut off, need to refresh */
435 ie->bDeviceAddress[keep_alives++] = wusb_dev->addr;
438 if (keep_alives & 0x1) /* pad to even number ([WUSB] section 7.5.9) */
439 ie->bDeviceAddress[keep_alives++] = 0x7f;
440 ie->hdr.bLength = sizeof(ie->hdr) +
441 keep_alives*sizeof(ie->bDeviceAddress[0]);
442 if (keep_alives > 0)
443 wusbhc_mmcie_set(wusbhc, 10, 5, &ie->hdr);
444 else if (old_keep_alives != 0)
445 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
449 * Do a run through all devices checking for timeouts
451 static void wusbhc_keep_alive_run(struct work_struct *ws)
453 struct delayed_work *dw = to_delayed_work(ws);
454 struct wusbhc *wusbhc = container_of(dw, struct wusbhc, keep_alive_timer);
456 mutex_lock(&wusbhc->mutex);
457 __wusbhc_keep_alive(wusbhc);
458 mutex_unlock(&wusbhc->mutex);
460 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
461 msecs_to_jiffies(wusbhc->trust_timeout / 2));
465 * Find the wusb_dev from its device address.
467 * The device can be found directly from the address (see
468 * wusb_cack_add() for where the device address is set to port_idx
469 * +2), except when the address is zero.
471 static struct wusb_dev *wusbhc_find_dev_by_addr(struct wusbhc *wusbhc, u8 addr)
473 int p;
475 if (addr == 0xff) /* unconnected */
476 return NULL;
478 if (addr > 0) {
479 int port = (addr & ~0x80) - 2;
480 if (port < 0 || port >= wusbhc->ports_max)
481 return NULL;
482 return wusb_port_by_idx(wusbhc, port)->wusb_dev;
485 /* Look for the device with address 0. */
486 for (p = 0; p < wusbhc->ports_max; p++) {
487 struct wusb_dev *wusb_dev = wusb_port_by_idx(wusbhc, p)->wusb_dev;
488 if (wusb_dev && wusb_dev->addr == addr)
489 return wusb_dev;
491 return NULL;
495 * Handle a DN_Alive notification (WUSB1.0[7.6.1])
497 * This just updates the device activity timestamp and then refreshes
498 * the keep alive IE.
500 * @wusbhc shall be referenced and unlocked
502 static void wusbhc_handle_dn_alive(struct wusbhc *wusbhc, u8 srcaddr)
504 struct wusb_dev *wusb_dev;
506 mutex_lock(&wusbhc->mutex);
507 wusb_dev = wusbhc_find_dev_by_addr(wusbhc, srcaddr);
508 if (wusb_dev == NULL) {
509 dev_dbg(wusbhc->dev, "ignoring DN_Alive from unconnected device %02x\n",
510 srcaddr);
511 } else {
512 wusb_dev->entry_ts = jiffies;
513 __wusbhc_keep_alive(wusbhc);
515 mutex_unlock(&wusbhc->mutex);
519 * Handle a DN_Connect notification (WUSB1.0[7.6.1])
521 * @wusbhc
522 * @pkt_hdr
523 * @size: Size of the buffer where the notification resides; if the
524 * notification data suggests there should be more data than
525 * available, an error will be signaled and the whole buffer
526 * consumed.
528 * @wusbhc->mutex shall be held
530 static void wusbhc_handle_dn_connect(struct wusbhc *wusbhc,
531 struct wusb_dn_hdr *dn_hdr,
532 size_t size)
534 struct device *dev = wusbhc->dev;
535 struct wusb_dn_connect *dnc;
536 char pr_cdid[WUSB_CKHDID_STRSIZE];
537 static const char *beacon_behaviour[] = {
538 "reserved",
539 "self-beacon",
540 "directed-beacon",
541 "no-beacon"
544 if (size < sizeof(*dnc)) {
545 dev_err(dev, "DN CONNECT: short notification (%zu < %zu)\n",
546 size, sizeof(*dnc));
547 return;
550 dnc = container_of(dn_hdr, struct wusb_dn_connect, hdr);
551 ckhdid_printf(pr_cdid, sizeof(pr_cdid), &dnc->CDID);
552 dev_info(dev, "DN CONNECT: device %s @ %x (%s) wants to %s\n",
553 pr_cdid,
554 wusb_dn_connect_prev_dev_addr(dnc),
555 beacon_behaviour[wusb_dn_connect_beacon_behavior(dnc)],
556 wusb_dn_connect_new_connection(dnc) ? "connect" : "reconnect");
557 /* ACK the connect */
558 wusbhc_devconnect_ack(wusbhc, dnc, pr_cdid);
562 * Handle a DN_Disconnect notification (WUSB1.0[7.6.1])
564 * Device is going down -- do the disconnect.
566 * @wusbhc shall be referenced and unlocked
568 static void wusbhc_handle_dn_disconnect(struct wusbhc *wusbhc, u8 srcaddr)
570 struct device *dev = wusbhc->dev;
571 struct wusb_dev *wusb_dev;
573 mutex_lock(&wusbhc->mutex);
574 wusb_dev = wusbhc_find_dev_by_addr(wusbhc, srcaddr);
575 if (wusb_dev == NULL) {
576 dev_dbg(dev, "ignoring DN DISCONNECT from unconnected device %02x\n",
577 srcaddr);
578 } else {
579 dev_info(dev, "DN DISCONNECT: device 0x%02x going down\n",
580 wusb_dev->addr);
581 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc,
582 wusb_dev->port_idx));
584 mutex_unlock(&wusbhc->mutex);
588 * Handle a Device Notification coming a host
590 * The Device Notification comes from a host (HWA, DWA or WHCI)
591 * wrapped in a set of headers. Somebody else has peeled off those
592 * headers for us and we just get one Device Notifications.
594 * Invalid DNs (e.g., too short) are discarded.
596 * @wusbhc shall be referenced
598 * FIXMES:
599 * - implement priorities as in WUSB1.0[Table 7-55]?
601 void wusbhc_handle_dn(struct wusbhc *wusbhc, u8 srcaddr,
602 struct wusb_dn_hdr *dn_hdr, size_t size)
604 struct device *dev = wusbhc->dev;
606 if (size < sizeof(struct wusb_dn_hdr)) {
607 dev_err(dev, "DN data shorter than DN header (%d < %d)\n",
608 (int)size, (int)sizeof(struct wusb_dn_hdr));
609 return;
611 switch (dn_hdr->bType) {
612 case WUSB_DN_CONNECT:
613 wusbhc_handle_dn_connect(wusbhc, dn_hdr, size);
614 break;
615 case WUSB_DN_ALIVE:
616 wusbhc_handle_dn_alive(wusbhc, srcaddr);
617 break;
618 case WUSB_DN_DISCONNECT:
619 wusbhc_handle_dn_disconnect(wusbhc, srcaddr);
620 break;
621 case WUSB_DN_MASAVAILCHANGED:
622 case WUSB_DN_RWAKE:
623 case WUSB_DN_SLEEP:
624 /* FIXME: handle these DNs. */
625 break;
626 case WUSB_DN_EPRDY:
627 /* The hardware handles these. */
628 break;
629 default:
630 dev_warn(dev, "unknown DN %u (%d octets) from %u\n",
631 dn_hdr->bType, (int)size, srcaddr);
634 EXPORT_SYMBOL_GPL(wusbhc_handle_dn);
637 * Disconnect a WUSB device from a the cluster
639 * @wusbhc
640 * @port Fake port where the device is (wusbhc index, not USB port number).
642 * In Wireless USB, a disconnect is basically telling the device he is
643 * being disconnected and forgetting about him.
645 * We send the device a Device Disconnect IE (WUSB1.0[7.5.11]) for 100
646 * ms and then keep going.
648 * We don't do much in case of error; we always pretend we disabled
649 * the port and disconnected the device. If physically the request
650 * didn't get there (many things can fail in the way there), the stack
651 * will reject the device's communication attempts.
653 * @wusbhc should be refcounted and locked
655 void __wusbhc_dev_disable(struct wusbhc *wusbhc, u8 port_idx)
657 int result;
658 struct device *dev = wusbhc->dev;
659 struct wusb_dev *wusb_dev;
660 struct wuie_disconnect *ie;
662 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
663 if (wusb_dev == NULL) {
664 /* reset no device? ignore */
665 dev_dbg(dev, "DISCONNECT: no device at port %u, ignoring\n",
666 port_idx);
667 return;
669 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
671 ie = kzalloc(sizeof(*ie), GFP_KERNEL);
672 if (ie == NULL)
673 return;
674 ie->hdr.bLength = sizeof(*ie);
675 ie->hdr.bIEIdentifier = WUIE_ID_DEVICE_DISCONNECT;
676 ie->bDeviceAddress = wusb_dev->addr;
677 result = wusbhc_mmcie_set(wusbhc, 0, 0, &ie->hdr);
678 if (result < 0)
679 dev_err(dev, "DISCONNECT: can't set MMC: %d\n", result);
680 else {
681 /* At least 6 MMCs, assuming at least 1 MMC per zone. */
682 msleep(7*4);
683 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
685 kfree(ie);
689 * Walk over the BOS descriptor, verify and grok it
691 * @usb_dev: referenced
692 * @wusb_dev: referenced and unlocked
694 * The BOS descriptor is defined at WUSB1.0[7.4.1], and it defines a
695 * "flexible" way to wrap all kinds of descriptors inside an standard
696 * descriptor (wonder why they didn't use normal descriptors,
697 * btw). Not like they lack code.
699 * At the end we go to look for the WUSB Device Capabilities
700 * (WUSB1.0[7.4.1.1]) that is wrapped in a device capability descriptor
701 * that is part of the BOS descriptor set. That tells us what does the
702 * device support (dual role, beacon type, UWB PHY rates).
704 static int wusb_dev_bos_grok(struct usb_device *usb_dev,
705 struct wusb_dev *wusb_dev,
706 struct usb_bos_descriptor *bos, size_t desc_size)
708 ssize_t result;
709 struct device *dev = &usb_dev->dev;
710 void *itr, *top;
712 /* Walk over BOS capabilities, verify them */
713 itr = (void *)bos + sizeof(*bos);
714 top = itr + desc_size - sizeof(*bos);
715 while (itr < top) {
716 struct usb_dev_cap_header *cap_hdr = itr;
717 size_t cap_size;
718 u8 cap_type;
719 if (top - itr < sizeof(*cap_hdr)) {
720 dev_err(dev, "Device BUG? premature end of BOS header "
721 "data [offset 0x%02x]: only %zu bytes left\n",
722 (int)(itr - (void *)bos), top - itr);
723 result = -ENOSPC;
724 goto error_bad_cap;
726 cap_size = cap_hdr->bLength;
727 cap_type = cap_hdr->bDevCapabilityType;
728 if (cap_size == 0)
729 break;
730 if (cap_size > top - itr) {
731 dev_err(dev, "Device BUG? premature end of BOS data "
732 "[offset 0x%02x cap %02x %zu bytes]: "
733 "only %zu bytes left\n",
734 (int)(itr - (void *)bos),
735 cap_type, cap_size, top - itr);
736 result = -EBADF;
737 goto error_bad_cap;
739 switch (cap_type) {
740 case USB_CAP_TYPE_WIRELESS_USB:
741 if (cap_size != sizeof(*wusb_dev->wusb_cap_descr))
742 dev_err(dev, "Device BUG? WUSB Capability "
743 "descriptor is %zu bytes vs %zu "
744 "needed\n", cap_size,
745 sizeof(*wusb_dev->wusb_cap_descr));
746 else
747 wusb_dev->wusb_cap_descr = itr;
748 break;
749 default:
750 dev_err(dev, "BUG? Unknown BOS capability 0x%02x "
751 "(%zu bytes) at offset 0x%02x\n", cap_type,
752 cap_size, (int)(itr - (void *)bos));
754 itr += cap_size;
756 result = 0;
757 error_bad_cap:
758 return result;
762 * Add information from the BOS descriptors to the device
764 * @usb_dev: referenced
765 * @wusb_dev: referenced and unlocked
767 * So what we do is we alloc a space for the BOS descriptor of 64
768 * bytes; read the first four bytes which include the wTotalLength
769 * field (WUSB1.0[T7-26]) and if it fits in those 64 bytes, read the
770 * whole thing. If not we realloc to that size.
772 * Then we call the groking function, that will fill up
773 * wusb_dev->wusb_cap_descr, which is what we'll need later on.
775 static int wusb_dev_bos_add(struct usb_device *usb_dev,
776 struct wusb_dev *wusb_dev)
778 ssize_t result;
779 struct device *dev = &usb_dev->dev;
780 struct usb_bos_descriptor *bos;
781 size_t alloc_size = 32, desc_size = 4;
783 bos = kmalloc(alloc_size, GFP_KERNEL);
784 if (bos == NULL)
785 return -ENOMEM;
786 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
787 if (result < 4) {
788 dev_err(dev, "Can't get BOS descriptor or too short: %zd\n",
789 result);
790 goto error_get_descriptor;
792 desc_size = le16_to_cpu(bos->wTotalLength);
793 if (desc_size >= alloc_size) {
794 kfree(bos);
795 alloc_size = desc_size;
796 bos = kmalloc(alloc_size, GFP_KERNEL);
797 if (bos == NULL)
798 return -ENOMEM;
800 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
801 if (result < 0 || result != desc_size) {
802 dev_err(dev, "Can't get BOS descriptor or too short (need "
803 "%zu bytes): %zd\n", desc_size, result);
804 goto error_get_descriptor;
806 if (result < sizeof(*bos)
807 || le16_to_cpu(bos->wTotalLength) != desc_size) {
808 dev_err(dev, "Can't get BOS descriptor or too short (need "
809 "%zu bytes): %zd\n", desc_size, result);
810 goto error_get_descriptor;
813 result = wusb_dev_bos_grok(usb_dev, wusb_dev, bos, result);
814 if (result < 0)
815 goto error_bad_bos;
816 wusb_dev->bos = bos;
817 return 0;
819 error_bad_bos:
820 error_get_descriptor:
821 kfree(bos);
822 wusb_dev->wusb_cap_descr = NULL;
823 return result;
826 static void wusb_dev_bos_rm(struct wusb_dev *wusb_dev)
828 kfree(wusb_dev->bos);
829 wusb_dev->wusb_cap_descr = NULL;
833 * USB stack's device addition Notifier Callback
835 * Called from drivers/usb/core/hub.c when a new device is added; we
836 * use this hook to perform certain WUSB specific setup work on the
837 * new device. As well, it is the first time we can connect the
838 * wusb_dev and the usb_dev. So we note it down in wusb_dev and take a
839 * reference that we'll drop.
841 * First we need to determine if the device is a WUSB device (else we
842 * ignore it). For that we use the speed setting (USB_SPEED_WIRELESS)
843 * [FIXME: maybe we'd need something more definitive]. If so, we track
844 * it's usb_busd and from there, the WUSB HC.
846 * Because all WUSB HCs are contained in a 'struct wusbhc', voila, we
847 * get the wusbhc for the device.
849 * We have a reference on @usb_dev (as we are called at the end of its
850 * enumeration).
852 * NOTE: @usb_dev locked
854 static void wusb_dev_add_ncb(struct usb_device *usb_dev)
856 int result = 0;
857 struct wusb_dev *wusb_dev;
858 struct wusbhc *wusbhc;
859 struct device *dev = &usb_dev->dev;
860 u8 port_idx;
862 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
863 return; /* skip non wusb and wusb RHs */
865 usb_set_device_state(usb_dev, USB_STATE_UNAUTHENTICATED);
867 wusbhc = wusbhc_get_by_usb_dev(usb_dev);
868 if (wusbhc == NULL)
869 goto error_nodev;
870 mutex_lock(&wusbhc->mutex);
871 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, usb_dev);
872 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
873 mutex_unlock(&wusbhc->mutex);
874 if (wusb_dev == NULL)
875 goto error_nodev;
876 wusb_dev->usb_dev = usb_get_dev(usb_dev);
877 usb_dev->wusb_dev = wusb_dev_get(wusb_dev);
878 result = wusb_dev_sec_add(wusbhc, usb_dev, wusb_dev);
879 if (result < 0) {
880 dev_err(dev, "Cannot enable security: %d\n", result);
881 goto error_sec_add;
883 /* Now query the device for it's BOS and attach it to wusb_dev */
884 result = wusb_dev_bos_add(usb_dev, wusb_dev);
885 if (result < 0) {
886 dev_err(dev, "Cannot get BOS descriptors: %d\n", result);
887 goto error_bos_add;
889 result = wusb_dev_sysfs_add(wusbhc, usb_dev, wusb_dev);
890 if (result < 0)
891 goto error_add_sysfs;
892 out:
893 wusb_dev_put(wusb_dev);
894 wusbhc_put(wusbhc);
895 error_nodev:
896 return;
898 wusb_dev_sysfs_rm(wusb_dev);
899 error_add_sysfs:
900 wusb_dev_bos_rm(wusb_dev);
901 error_bos_add:
902 wusb_dev_sec_rm(wusb_dev);
903 error_sec_add:
904 mutex_lock(&wusbhc->mutex);
905 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
906 mutex_unlock(&wusbhc->mutex);
907 goto out;
911 * Undo all the steps done at connection by the notifier callback
913 * NOTE: @usb_dev locked
915 static void wusb_dev_rm_ncb(struct usb_device *usb_dev)
917 struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
919 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
920 return; /* skip non wusb and wusb RHs */
922 wusb_dev_sysfs_rm(wusb_dev);
923 wusb_dev_bos_rm(wusb_dev);
924 wusb_dev_sec_rm(wusb_dev);
925 wusb_dev->usb_dev = NULL;
926 usb_dev->wusb_dev = NULL;
927 wusb_dev_put(wusb_dev);
928 usb_put_dev(usb_dev);
932 * Handle notifications from the USB stack (notifier call back)
934 * This is called when the USB stack does a
935 * usb_{bus,device}_{add,remove}() so we can do WUSB specific
936 * handling. It is called with [for the case of
937 * USB_DEVICE_{ADD,REMOVE} with the usb_dev locked.
939 int wusb_usb_ncb(struct notifier_block *nb, unsigned long val,
940 void *priv)
942 int result = NOTIFY_OK;
944 switch (val) {
945 case USB_DEVICE_ADD:
946 wusb_dev_add_ncb(priv);
947 break;
948 case USB_DEVICE_REMOVE:
949 wusb_dev_rm_ncb(priv);
950 break;
951 case USB_BUS_ADD:
952 /* ignore (for now) */
953 case USB_BUS_REMOVE:
954 break;
955 default:
956 WARN_ON(1);
957 result = NOTIFY_BAD;
959 return result;
963 * Return a referenced wusb_dev given a @wusbhc and @usb_dev
965 struct wusb_dev *__wusb_dev_get_by_usb_dev(struct wusbhc *wusbhc,
966 struct usb_device *usb_dev)
968 struct wusb_dev *wusb_dev;
969 u8 port_idx;
971 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
972 BUG_ON(port_idx > wusbhc->ports_max);
973 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
974 if (wusb_dev != NULL) /* ops, device is gone */
975 wusb_dev_get(wusb_dev);
976 return wusb_dev;
978 EXPORT_SYMBOL_GPL(__wusb_dev_get_by_usb_dev);
980 void wusb_dev_destroy(struct kref *_wusb_dev)
982 struct wusb_dev *wusb_dev = container_of(_wusb_dev, struct wusb_dev, refcnt);
984 list_del_init(&wusb_dev->cack_node);
985 wusb_dev_free(wusb_dev);
987 EXPORT_SYMBOL_GPL(wusb_dev_destroy);
990 * Create all the device connect handling infrastructure
992 * This is basically the device info array, Connect Acknowledgement
993 * (cack) lists, keep-alive timers (and delayed work thread).
995 int wusbhc_devconnect_create(struct wusbhc *wusbhc)
997 wusbhc->keep_alive_ie.hdr.bIEIdentifier = WUIE_ID_KEEP_ALIVE;
998 wusbhc->keep_alive_ie.hdr.bLength = sizeof(wusbhc->keep_alive_ie.hdr);
999 INIT_DELAYED_WORK(&wusbhc->keep_alive_timer, wusbhc_keep_alive_run);
1001 wusbhc->cack_ie.hdr.bIEIdentifier = WUIE_ID_CONNECTACK;
1002 wusbhc->cack_ie.hdr.bLength = sizeof(wusbhc->cack_ie.hdr);
1003 INIT_LIST_HEAD(&wusbhc->cack_list);
1005 return 0;
1009 * Release all resources taken by the devconnect stuff
1011 void wusbhc_devconnect_destroy(struct wusbhc *wusbhc)
1013 /* no op */
1017 * wusbhc_devconnect_start - start accepting device connections
1018 * @wusbhc: the WUSB HC
1020 * Sets the Host Info IE to accept all new connections.
1022 * FIXME: This also enables the keep alives but this is not necessary
1023 * until there are connected and authenticated devices.
1025 int wusbhc_devconnect_start(struct wusbhc *wusbhc)
1027 struct device *dev = wusbhc->dev;
1028 struct wuie_host_info *hi;
1029 int result;
1031 hi = kzalloc(sizeof(*hi), GFP_KERNEL);
1032 if (hi == NULL)
1033 return -ENOMEM;
1035 hi->hdr.bLength = sizeof(*hi);
1036 hi->hdr.bIEIdentifier = WUIE_ID_HOST_INFO;
1037 hi->attributes = cpu_to_le16((wusbhc->rsv->stream << 3) | WUIE_HI_CAP_ALL);
1038 hi->CHID = wusbhc->chid;
1039 result = wusbhc_mmcie_set(wusbhc, 0, 0, &hi->hdr);
1040 if (result < 0) {
1041 dev_err(dev, "Cannot add Host Info MMCIE: %d\n", result);
1042 goto error_mmcie_set;
1044 wusbhc->wuie_host_info = hi;
1046 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
1047 msecs_to_jiffies(wusbhc->trust_timeout / 2));
1049 return 0;
1051 error_mmcie_set:
1052 kfree(hi);
1053 return result;
1057 * wusbhc_devconnect_stop - stop managing connected devices
1058 * @wusbhc: the WUSB HC
1060 * Disconnects any devices still connected, stops the keep alives and
1061 * removes the Host Info IE.
1063 void wusbhc_devconnect_stop(struct wusbhc *wusbhc)
1065 int i;
1067 mutex_lock(&wusbhc->mutex);
1068 for (i = 0; i < wusbhc->ports_max; i++) {
1069 if (wusbhc->port[i].wusb_dev)
1070 __wusbhc_dev_disconnect(wusbhc, &wusbhc->port[i]);
1072 mutex_unlock(&wusbhc->mutex);
1074 cancel_delayed_work_sync(&wusbhc->keep_alive_timer);
1075 wusbhc_mmcie_rm(wusbhc, &wusbhc->wuie_host_info->hdr);
1076 kfree(wusbhc->wuie_host_info);
1077 wusbhc->wuie_host_info = NULL;
1081 * wusb_set_dev_addr - set the WUSB device address used by the host
1082 * @wusbhc: the WUSB HC the device is connect to
1083 * @wusb_dev: the WUSB device
1084 * @addr: new device address
1086 int wusb_set_dev_addr(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev, u8 addr)
1088 int result;
1090 wusb_dev->addr = addr;
1091 result = wusbhc->dev_info_set(wusbhc, wusb_dev);
1092 if (result < 0)
1093 dev_err(wusbhc->dev, "device %d: failed to set device "
1094 "address\n", wusb_dev->port_idx);
1095 else
1096 dev_info(wusbhc->dev, "device %d: %s addr %u\n",
1097 wusb_dev->port_idx,
1098 (addr & WUSB_DEV_ADDR_UNAUTH) ? "unauth" : "auth",
1099 wusb_dev->addr);
1101 return result;