]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/usb/wusbcore/devconnect.c
USB: allow libusb to talk to unauthenticated WUSB devices
[net-next-2.6.git] / drivers / usb / wusbcore / devconnect.c
CommitLineData
b69fada6
IPG
1/*
2 * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
3 * Device Connect handling
4 *
5 * Copyright (C) 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 * FIXME: docs
24 * FIXME: this file needs to be broken up, it's grown too big
25 *
26 *
27 * WUSB1.0[7.1, 7.5.1, ]
28 *
29 * WUSB device connection is kind of messy. Some background:
30 *
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] .
35 *
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).
43 *
44 * So now the device now has a WUSB address. From now on, we use
45 * that to talk to it in the RPipes.
46 *
47 * ASSUMPTIONS:
48 *
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.
51 *
52 * ROADMAP:
53 *
54 * This file contains the logic for doing that--entry points:
55 *
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.
59 *
b69fada6
IPG
60 * wusb_devconnect_acked() Ack done, release resources.
61 *
62 * wusb_handle_dn_alive() Called by notif.c:wusb_handle_dn()
63 * for processing a DN_Alive pong from a device.
64 *
65 * wusb_handle_dn_disconnect()Called by notif.c:wusb_handle_dn() to
66 * process a disconenct request from a
67 * device.
68 *
b69fada6
IPG
69 * __wusb_dev_disable() Called by rh.c:wusbhc_rh_clear_port_feat() when
70 * disabling a port.
71 *
72 * wusb_devconnect_create() Called when creating the host by
73 * lc.c:wusbhc_create().
74 *
75 * wusb_devconnect_destroy() Cleanup called removing the host. Called
76 * by lc.c:wusbhc_destroy().
77 *
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).
81 *
82 * LIFE CYCLE OF port->wusb_dev
83 *
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.
87 */
88
89#include <linux/jiffies.h>
90#include <linux/ctype.h>
91#include <linux/workqueue.h>
92#include "wusbhc.h"
93
b69fada6
IPG
94static void wusbhc_devconnect_acked_work(struct work_struct *work);
95
96static void wusb_dev_free(struct wusb_dev *wusb_dev)
97{
98 if (wusb_dev) {
99 kfree(wusb_dev->set_gtk_req);
100 usb_free_urb(wusb_dev->set_gtk_urb);
101 kfree(wusb_dev);
102 }
103}
104
105static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc)
106{
107 struct wusb_dev *wusb_dev;
108 struct urb *urb;
109 struct usb_ctrlrequest *req;
110
111 wusb_dev = kzalloc(sizeof(*wusb_dev), GFP_KERNEL);
112 if (wusb_dev == NULL)
113 goto err;
114
115 wusb_dev->wusbhc = wusbhc;
116
117 INIT_WORK(&wusb_dev->devconnect_acked_work, wusbhc_devconnect_acked_work);
118
119 urb = usb_alloc_urb(0, GFP_KERNEL);
120 if (urb == NULL)
121 goto err;
122
123 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
124 if (req == NULL)
125 goto err;
126
127 req->bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
128 req->bRequest = USB_REQ_SET_DESCRIPTOR;
129 req->wValue = cpu_to_le16(USB_DT_KEY << 8 | wusbhc->gtk_index);
130 req->wIndex = 0;
131 req->wLength = cpu_to_le16(wusbhc->gtk.descr.bLength);
132
133 wusb_dev->set_gtk_urb = urb;
134 wusb_dev->set_gtk_req = req;
135
136 return wusb_dev;
137err:
138 wusb_dev_free(wusb_dev);
139 return NULL;
140}
141
142
143/*
144 * Using the Connect-Ack list, fill out the @wusbhc Connect-Ack WUSB IE
145 * properly so that it can be added to the MMC.
146 *
147 * We just get the @wusbhc->ca_list and fill out the first four ones or
148 * less (per-spec WUSB1.0[7.5, before T7-38). If the ConnectAck WUSB
149 * IE is not allocated, we alloc it.
150 *
151 * @wusbhc->mutex must be taken
152 */
153static void wusbhc_fill_cack_ie(struct wusbhc *wusbhc)
154{
155 unsigned cnt;
156 struct wusb_dev *dev_itr;
157 struct wuie_connect_ack *cack_ie;
158
159 cack_ie = &wusbhc->cack_ie;
160 cnt = 0;
161 list_for_each_entry(dev_itr, &wusbhc->cack_list, cack_node) {
162 cack_ie->blk[cnt].CDID = dev_itr->cdid;
163 cack_ie->blk[cnt].bDeviceAddress = dev_itr->addr;
164 if (++cnt >= WUIE_ELT_MAX)
165 break;
166 }
167 cack_ie->hdr.bLength = sizeof(cack_ie->hdr)
168 + cnt * sizeof(cack_ie->blk[0]);
169}
170
171/*
172 * Register a new device that wants to connect
173 *
174 * A new device wants to connect, so we add it to the Connect-Ack
175 * list. We give it an address in the unauthorized range (bit 8 set);
176 * user space will have to drive authorization further on.
177 *
178 * @dev_addr: address to use for the device (which is also the port
179 * number).
180 *
181 * @wusbhc->mutex must be taken
182 */
183static struct wusb_dev *wusbhc_cack_add(struct wusbhc *wusbhc,
184 struct wusb_dn_connect *dnc,
185 const char *pr_cdid, u8 port_idx)
186{
187 struct device *dev = wusbhc->dev;
188 struct wusb_dev *wusb_dev;
189 int new_connection = wusb_dn_connect_new_connection(dnc);
190 u8 dev_addr;
191 int result;
192
b69fada6
IPG
193 /* Is it registered already? */
194 list_for_each_entry(wusb_dev, &wusbhc->cack_list, cack_node)
195 if (!memcmp(&wusb_dev->cdid, &dnc->CDID,
196 sizeof(wusb_dev->cdid)))
197 return wusb_dev;
198 /* We don't have it, create an entry, register it */
199 wusb_dev = wusb_dev_alloc(wusbhc);
8092d7c9 200 if (wusb_dev == NULL)
b69fada6 201 return NULL;
b69fada6
IPG
202 wusb_dev_init(wusb_dev);
203 wusb_dev->cdid = dnc->CDID;
204 wusb_dev->port_idx = port_idx;
205
206 /*
207 * Devices are always available within the cluster reservation
208 * and since the hardware will take the intersection of the
209 * per-device availability and the cluster reservation, the
210 * per-device availability can simply be set to always
211 * available.
212 */
213 bitmap_fill(wusb_dev->availability.bm, UWB_NUM_MAS);
214
215 /* FIXME: handle reconnects instead of assuming connects are
216 always new. */
217 if (1 && new_connection == 0)
218 new_connection = 1;
219 if (new_connection) {
220 dev_addr = (port_idx + 2) | WUSB_DEV_ADDR_UNAUTH;
221
222 dev_info(dev, "Connecting new WUSB device to address %u, "
223 "port %u\n", dev_addr, port_idx);
224
225 result = wusb_set_dev_addr(wusbhc, wusb_dev, dev_addr);
f51c23ba
AL
226 if (result < 0)
227 return NULL;
b69fada6
IPG
228 }
229 wusb_dev->entry_ts = jiffies;
230 list_add_tail(&wusb_dev->cack_node, &wusbhc->cack_list);
231 wusbhc->cack_count++;
232 wusbhc_fill_cack_ie(wusbhc);
bce83697 233
b69fada6
IPG
234 return wusb_dev;
235}
236
237/*
238 * Remove a Connect-Ack context entry from the HCs view
239 *
240 * @wusbhc->mutex must be taken
241 */
242static void wusbhc_cack_rm(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
243{
b69fada6
IPG
244 list_del_init(&wusb_dev->cack_node);
245 wusbhc->cack_count--;
246 wusbhc_fill_cack_ie(wusbhc);
b69fada6
IPG
247}
248
249/*
250 * @wusbhc->mutex must be taken */
251static
252void wusbhc_devconnect_acked(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
253{
b69fada6
IPG
254 wusbhc_cack_rm(wusbhc, wusb_dev);
255 if (wusbhc->cack_count)
256 wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
257 else
258 wusbhc_mmcie_rm(wusbhc, &wusbhc->cack_ie.hdr);
b69fada6
IPG
259}
260
261static void wusbhc_devconnect_acked_work(struct work_struct *work)
262{
263 struct wusb_dev *wusb_dev = container_of(work, struct wusb_dev,
264 devconnect_acked_work);
265 struct wusbhc *wusbhc = wusb_dev->wusbhc;
266
267 mutex_lock(&wusbhc->mutex);
268 wusbhc_devconnect_acked(wusbhc, wusb_dev);
269 mutex_unlock(&wusbhc->mutex);
270}
271
272/*
273 * Ack a device for connection
274 *
275 * FIXME: docs
276 *
277 * @pr_cdid: Printable CDID...hex Use @dnc->cdid for the real deal.
278 *
279 * So we get the connect ack IE (may have been allocated already),
280 * find an empty connect block, an empty virtual port, create an
281 * address with it (see below), make it an unauth addr [bit 7 set] and
282 * set the MMC.
283 *
284 * Addresses: because WUSB hosts have no downstream hubs, we can do a
285 * 1:1 mapping between 'port number' and device
286 * address. This simplifies many things, as during this
287 * initial connect phase the USB stack has no knoledge of
288 * the device and hasn't assigned an address yet--we know
289 * USB's choose_address() will use the same euristics we
290 * use here, so we can assume which address will be assigned.
291 *
292 * USB stack always assigns address 1 to the root hub, so
293 * to the port number we add 2 (thus virtual port #0 is
294 * addr #2).
295 *
296 * @wusbhc shall be referenced
297 */
298static
299void wusbhc_devconnect_ack(struct wusbhc *wusbhc, struct wusb_dn_connect *dnc,
300 const char *pr_cdid)
301{
302 int result;
303 struct device *dev = wusbhc->dev;
304 struct wusb_dev *wusb_dev;
305 struct wusb_port *port;
306 unsigned idx, devnum;
307
b69fada6
IPG
308 mutex_lock(&wusbhc->mutex);
309
310 /* Check we are not handling it already */
311 for (idx = 0; idx < wusbhc->ports_max; idx++) {
312 port = wusb_port_by_idx(wusbhc, idx);
313 if (port->wusb_dev
8092d7c9 314 && memcmp(&dnc->CDID, &port->wusb_dev->cdid, sizeof(dnc->CDID)) == 0)
b69fada6 315 goto error_unlock;
b69fada6
IPG
316 }
317 /* Look up those fake ports we have for a free one */
318 for (idx = 0; idx < wusbhc->ports_max; idx++) {
319 port = wusb_port_by_idx(wusbhc, idx);
320 if ((port->status & USB_PORT_STAT_POWER)
321 && !(port->status & USB_PORT_STAT_CONNECTION))
322 break;
323 }
324 if (idx >= wusbhc->ports_max) {
325 dev_err(dev, "Host controller can't connect more devices "
326 "(%u already connected); device %s rejected\n",
327 wusbhc->ports_max, pr_cdid);
328 /* NOTE: we could send a WUIE_Disconnect here, but we haven't
329 * event acked, so the device will eventually timeout the
330 * connection, right? */
331 goto error_unlock;
332 }
333
334 devnum = idx + 2;
335
336 /* Make sure we are using no crypto on that "virtual port" */
337 wusbhc->set_ptk(wusbhc, idx, 0, NULL, 0);
338
339 /* Grab a filled in Connect-Ack context, fill out the
340 * Connect-Ack Wireless USB IE, set the MMC */
341 wusb_dev = wusbhc_cack_add(wusbhc, dnc, pr_cdid, idx);
342 if (wusb_dev == NULL)
343 goto error_unlock;
344 result = wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
345 if (result < 0)
346 goto error_unlock;
347 /* Give the device at least 2ms (WUSB1.0[7.5.1p3]), let's do
348 * three for a good measure */
349 msleep(3);
350 port->wusb_dev = wusb_dev;
351 port->status |= USB_PORT_STAT_CONNECTION;
352 port->change |= USB_PORT_STAT_C_CONNECTION;
b69fada6
IPG
353 /* Now the port status changed to connected; khubd will
354 * pick the change up and try to reset the port to bring it to
355 * the enabled state--so this process returns up to the stack
4656d5de 356 * and it calls back into wusbhc_rh_port_reset().
b69fada6
IPG
357 */
358error_unlock:
359 mutex_unlock(&wusbhc->mutex);
b69fada6
IPG
360 return;
361
362}
363
364/*
365 * Disconnect a Wireless USB device from its fake port
366 *
367 * Marks the port as disconnected so that khubd can pick up the change
368 * and drops our knowledge about the device.
369 *
370 * Assumes there is a device connected
371 *
372 * @port_index: zero based port number
373 *
374 * NOTE: @wusbhc->mutex is locked
375 *
376 * WARNING: From here it is not very safe to access anything hanging off
377 * wusb_dev
378 */
379static void __wusbhc_dev_disconnect(struct wusbhc *wusbhc,
380 struct wusb_port *port)
381{
b69fada6
IPG
382 struct wusb_dev *wusb_dev = port->wusb_dev;
383
b69fada6
IPG
384 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE
385 | USB_PORT_STAT_SUSPEND | USB_PORT_STAT_RESET
386 | USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
387 port->change |= USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE;
388 if (wusb_dev) {
9a9b1d17 389 dev_dbg(wusbhc->dev, "disconnecting device from port %d\n", wusb_dev->port_idx);
b69fada6
IPG
390 if (!list_empty(&wusb_dev->cack_node))
391 list_del_init(&wusb_dev->cack_node);
392 /* For the one in cack_add() */
393 wusb_dev_put(wusb_dev);
394 }
395 port->wusb_dev = NULL;
b69fada6
IPG
396
397 /* After a device disconnects, change the GTK (see [WUSB]
398 * section 6.2.11.2). */
399 wusbhc_gtk_rekey(wusbhc);
400
b69fada6
IPG
401 /* The Wireless USB part has forgotten about the device already; now
402 * khubd's timer will pick up the disconnection and remove the USB
403 * device from the system
404 */
405}
406
b69fada6
IPG
407/*
408 * Refresh the list of keep alives to emit in the MMC
409 *
410 * Some devices don't respond to keep alives unless they've been
411 * authenticated, so skip unauthenticated devices.
412 *
413 * We only publish the first four devices that have a coming timeout
414 * condition. Then when we are done processing those, we go for the
415 * next ones. We ignore the ones that have timed out already (they'll
416 * be purged).
417 *
418 * This might cause the first devices to timeout the last devices in
419 * the port array...FIXME: come up with a better algorithm?
420 *
421 * Note we can't do much about MMC's ops errors; we hope next refresh
422 * will kind of handle it.
423 *
424 * NOTE: @wusbhc->mutex is locked
425 */
426static void __wusbhc_keep_alive(struct wusbhc *wusbhc)
427{
b69fada6
IPG
428 struct device *dev = wusbhc->dev;
429 unsigned cnt;
430 struct wusb_dev *wusb_dev;
431 struct wusb_port *wusb_port;
432 struct wuie_keep_alive *ie = &wusbhc->keep_alive_ie;
433 unsigned keep_alives, old_keep_alives;
434
b69fada6
IPG
435 old_keep_alives = ie->hdr.bLength - sizeof(ie->hdr);
436 keep_alives = 0;
437 for (cnt = 0;
438 keep_alives <= WUIE_ELT_MAX && cnt < wusbhc->ports_max;
439 cnt++) {
440 unsigned tt = msecs_to_jiffies(wusbhc->trust_timeout);
441
442 wusb_port = wusb_port_by_idx(wusbhc, cnt);
443 wusb_dev = wusb_port->wusb_dev;
444
445 if (wusb_dev == NULL)
446 continue;
447 if (wusb_dev->usb_dev == NULL || !wusb_dev->usb_dev->authenticated)
448 continue;
449
450 if (time_after(jiffies, wusb_dev->entry_ts + tt)) {
451 dev_err(dev, "KEEPALIVE: device %u timed out\n",
452 wusb_dev->addr);
453 __wusbhc_dev_disconnect(wusbhc, wusb_port);
454 } else if (time_after(jiffies, wusb_dev->entry_ts + tt/2)) {
455 /* Approaching timeout cut out, need to refresh */
456 ie->bDeviceAddress[keep_alives++] = wusb_dev->addr;
457 }
458 }
459 if (keep_alives & 0x1) /* pad to even number ([WUSB] section 7.5.9) */
460 ie->bDeviceAddress[keep_alives++] = 0x7f;
461 ie->hdr.bLength = sizeof(ie->hdr) +
462 keep_alives*sizeof(ie->bDeviceAddress[0]);
8092d7c9
DV
463 if (keep_alives > 0)
464 wusbhc_mmcie_set(wusbhc, 10, 5, &ie->hdr);
465 else if (old_keep_alives != 0)
b69fada6 466 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
b69fada6
IPG
467}
468
469/*
470 * Do a run through all devices checking for timeouts
471 */
472static void wusbhc_keep_alive_run(struct work_struct *ws)
473{
56968d0c
DV
474 struct delayed_work *dw = container_of(ws, struct delayed_work, work);
475 struct wusbhc *wusbhc = container_of(dw, struct wusbhc, keep_alive_timer);
476
477 mutex_lock(&wusbhc->mutex);
478 __wusbhc_keep_alive(wusbhc);
479 mutex_unlock(&wusbhc->mutex);
480
481 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
482 msecs_to_jiffies(wusbhc->trust_timeout / 2));
b69fada6
IPG
483}
484
485/*
486 * Find the wusb_dev from its device address.
487 *
488 * The device can be found directly from the address (see
489 * wusb_cack_add() for where the device address is set to port_idx
490 * +2), except when the address is zero.
491 */
492static struct wusb_dev *wusbhc_find_dev_by_addr(struct wusbhc *wusbhc, u8 addr)
493{
494 int p;
495
496 if (addr == 0xff) /* unconnected */
497 return NULL;
498
499 if (addr > 0) {
500 int port = (addr & ~0x80) - 2;
501 if (port < 0 || port >= wusbhc->ports_max)
502 return NULL;
503 return wusb_port_by_idx(wusbhc, port)->wusb_dev;
504 }
505
506 /* Look for the device with address 0. */
507 for (p = 0; p < wusbhc->ports_max; p++) {
508 struct wusb_dev *wusb_dev = wusb_port_by_idx(wusbhc, p)->wusb_dev;
509 if (wusb_dev && wusb_dev->addr == addr)
510 return wusb_dev;
511 }
512 return NULL;
513}
514
515/*
516 * Handle a DN_Alive notification (WUSB1.0[7.6.1])
517 *
518 * This just updates the device activity timestamp and then refreshes
519 * the keep alive IE.
520 *
521 * @wusbhc shall be referenced and unlocked
522 */
523static void wusbhc_handle_dn_alive(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
524{
b69fada6
IPG
525 mutex_lock(&wusbhc->mutex);
526 wusb_dev->entry_ts = jiffies;
527 __wusbhc_keep_alive(wusbhc);
528 mutex_unlock(&wusbhc->mutex);
529}
530
531/*
532 * Handle a DN_Connect notification (WUSB1.0[7.6.1])
533 *
534 * @wusbhc
535 * @pkt_hdr
536 * @size: Size of the buffer where the notification resides; if the
537 * notification data suggests there should be more data than
538 * available, an error will be signaled and the whole buffer
539 * consumed.
540 *
541 * @wusbhc->mutex shall be held
542 */
543static void wusbhc_handle_dn_connect(struct wusbhc *wusbhc,
544 struct wusb_dn_hdr *dn_hdr,
545 size_t size)
546{
547 struct device *dev = wusbhc->dev;
548 struct wusb_dn_connect *dnc;
549 char pr_cdid[WUSB_CKHDID_STRSIZE];
550 static const char *beacon_behaviour[] = {
551 "reserved",
552 "self-beacon",
553 "directed-beacon",
554 "no-beacon"
555 };
556
b69fada6
IPG
557 if (size < sizeof(*dnc)) {
558 dev_err(dev, "DN CONNECT: short notification (%zu < %zu)\n",
559 size, sizeof(*dnc));
bce83697 560 return;
b69fada6
IPG
561 }
562
563 dnc = container_of(dn_hdr, struct wusb_dn_connect, hdr);
564 ckhdid_printf(pr_cdid, sizeof(pr_cdid), &dnc->CDID);
565 dev_info(dev, "DN CONNECT: device %s @ %x (%s) wants to %s\n",
566 pr_cdid,
567 wusb_dn_connect_prev_dev_addr(dnc),
568 beacon_behaviour[wusb_dn_connect_beacon_behavior(dnc)],
569 wusb_dn_connect_new_connection(dnc) ? "connect" : "reconnect");
570 /* ACK the connect */
571 wusbhc_devconnect_ack(wusbhc, dnc, pr_cdid);
b69fada6
IPG
572}
573
574/*
575 * Handle a DN_Disconnect notification (WUSB1.0[7.6.1])
576 *
577 * Device is going down -- do the disconnect.
578 *
579 * @wusbhc shall be referenced and unlocked
580 */
581static void wusbhc_handle_dn_disconnect(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
582{
583 struct device *dev = wusbhc->dev;
584
585 dev_info(dev, "DN DISCONNECT: device 0x%02x going down\n", wusb_dev->addr);
586
587 mutex_lock(&wusbhc->mutex);
588 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, wusb_dev->port_idx));
589 mutex_unlock(&wusbhc->mutex);
590}
591
b69fada6
IPG
592/*
593 * Handle a Device Notification coming a host
594 *
595 * The Device Notification comes from a host (HWA, DWA or WHCI)
596 * wrapped in a set of headers. Somebody else has peeled off those
597 * headers for us and we just get one Device Notifications.
598 *
599 * Invalid DNs (e.g., too short) are discarded.
600 *
601 * @wusbhc shall be referenced
602 *
603 * FIXMES:
604 * - implement priorities as in WUSB1.0[Table 7-55]?
605 */
606void wusbhc_handle_dn(struct wusbhc *wusbhc, u8 srcaddr,
607 struct wusb_dn_hdr *dn_hdr, size_t size)
608{
609 struct device *dev = wusbhc->dev;
610 struct wusb_dev *wusb_dev;
611
b69fada6
IPG
612 if (size < sizeof(struct wusb_dn_hdr)) {
613 dev_err(dev, "DN data shorter than DN header (%d < %d)\n",
614 (int)size, (int)sizeof(struct wusb_dn_hdr));
bce83697 615 return;
b69fada6
IPG
616 }
617
618 wusb_dev = wusbhc_find_dev_by_addr(wusbhc, srcaddr);
619 if (wusb_dev == NULL && dn_hdr->bType != WUSB_DN_CONNECT) {
620 dev_dbg(dev, "ignoring DN %d from unconnected device %02x\n",
621 dn_hdr->bType, srcaddr);
bce83697 622 return;
b69fada6
IPG
623 }
624
625 switch (dn_hdr->bType) {
626 case WUSB_DN_CONNECT:
627 wusbhc_handle_dn_connect(wusbhc, dn_hdr, size);
628 break;
629 case WUSB_DN_ALIVE:
630 wusbhc_handle_dn_alive(wusbhc, wusb_dev);
631 break;
632 case WUSB_DN_DISCONNECT:
633 wusbhc_handle_dn_disconnect(wusbhc, wusb_dev);
634 break;
635 case WUSB_DN_MASAVAILCHANGED:
636 case WUSB_DN_RWAKE:
637 case WUSB_DN_SLEEP:
638 /* FIXME: handle these DNs. */
639 break;
640 case WUSB_DN_EPRDY:
641 /* The hardware handles these. */
642 break;
643 default:
644 dev_warn(dev, "unknown DN %u (%d octets) from %u\n",
645 dn_hdr->bType, (int)size, srcaddr);
646 }
b69fada6
IPG
647}
648EXPORT_SYMBOL_GPL(wusbhc_handle_dn);
649
650/*
651 * Disconnect a WUSB device from a the cluster
652 *
653 * @wusbhc
654 * @port Fake port where the device is (wusbhc index, not USB port number).
655 *
656 * In Wireless USB, a disconnect is basically telling the device he is
657 * being disconnected and forgetting about him.
658 *
659 * We send the device a Device Disconnect IE (WUSB1.0[7.5.11]) for 100
660 * ms and then keep going.
661 *
662 * We don't do much in case of error; we always pretend we disabled
663 * the port and disconnected the device. If physically the request
664 * didn't get there (many things can fail in the way there), the stack
665 * will reject the device's communication attempts.
666 *
667 * @wusbhc should be refcounted and locked
668 */
669void __wusbhc_dev_disable(struct wusbhc *wusbhc, u8 port_idx)
670{
671 int result;
672 struct device *dev = wusbhc->dev;
673 struct wusb_dev *wusb_dev;
674 struct wuie_disconnect *ie;
675
b69fada6
IPG
676 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
677 if (wusb_dev == NULL) {
678 /* reset no device? ignore */
679 dev_dbg(dev, "DISCONNECT: no device at port %u, ignoring\n",
680 port_idx);
bce83697 681 return;
b69fada6
IPG
682 }
683 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
684
b69fada6
IPG
685 ie = kzalloc(sizeof(*ie), GFP_KERNEL);
686 if (ie == NULL)
bce83697 687 return;
b69fada6
IPG
688 ie->hdr.bLength = sizeof(*ie);
689 ie->hdr.bIEIdentifier = WUIE_ID_DEVICE_DISCONNECT;
690 ie->bDeviceAddress = wusb_dev->addr;
691 result = wusbhc_mmcie_set(wusbhc, 0, 0, &ie->hdr);
bce83697 692 if (result < 0)
b69fada6 693 dev_err(dev, "DISCONNECT: can't set MMC: %d\n", result);
bce83697
DV
694 else {
695 /* At least 6 MMCs, assuming at least 1 MMC per zone. */
696 msleep(7*4);
697 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
b69fada6 698 }
b69fada6 699 kfree(ie);
b69fada6
IPG
700}
701
702/*
703 * Walk over the BOS descriptor, verify and grok it
704 *
705 * @usb_dev: referenced
706 * @wusb_dev: referenced and unlocked
707 *
708 * The BOS descriptor is defined at WUSB1.0[7.4.1], and it defines a
709 * "flexible" way to wrap all kinds of descriptors inside an standard
710 * descriptor (wonder why they didn't use normal descriptors,
711 * btw). Not like they lack code.
712 *
713 * At the end we go to look for the WUSB Device Capabilities
714 * (WUSB1.0[7.4.1.1]) that is wrapped in a device capability descriptor
715 * that is part of the BOS descriptor set. That tells us what does the
716 * device support (dual role, beacon type, UWB PHY rates).
717 */
718static int wusb_dev_bos_grok(struct usb_device *usb_dev,
719 struct wusb_dev *wusb_dev,
720 struct usb_bos_descriptor *bos, size_t desc_size)
721{
722 ssize_t result;
723 struct device *dev = &usb_dev->dev;
724 void *itr, *top;
725
726 /* Walk over BOS capabilities, verify them */
727 itr = (void *)bos + sizeof(*bos);
728 top = itr + desc_size - sizeof(*bos);
729 while (itr < top) {
730 struct usb_dev_cap_header *cap_hdr = itr;
731 size_t cap_size;
732 u8 cap_type;
733 if (top - itr < sizeof(*cap_hdr)) {
734 dev_err(dev, "Device BUG? premature end of BOS header "
735 "data [offset 0x%02x]: only %zu bytes left\n",
736 (int)(itr - (void *)bos), top - itr);
737 result = -ENOSPC;
738 goto error_bad_cap;
739 }
740 cap_size = cap_hdr->bLength;
741 cap_type = cap_hdr->bDevCapabilityType;
b69fada6
IPG
742 if (cap_size == 0)
743 break;
744 if (cap_size > top - itr) {
745 dev_err(dev, "Device BUG? premature end of BOS data "
746 "[offset 0x%02x cap %02x %zu bytes]: "
747 "only %zu bytes left\n",
748 (int)(itr - (void *)bos),
749 cap_type, cap_size, top - itr);
750 result = -EBADF;
751 goto error_bad_cap;
752 }
b69fada6
IPG
753 switch (cap_type) {
754 case USB_CAP_TYPE_WIRELESS_USB:
755 if (cap_size != sizeof(*wusb_dev->wusb_cap_descr))
756 dev_err(dev, "Device BUG? WUSB Capability "
757 "descriptor is %zu bytes vs %zu "
758 "needed\n", cap_size,
759 sizeof(*wusb_dev->wusb_cap_descr));
bce83697 760 else
b69fada6 761 wusb_dev->wusb_cap_descr = itr;
b69fada6
IPG
762 break;
763 default:
764 dev_err(dev, "BUG? Unknown BOS capability 0x%02x "
765 "(%zu bytes) at offset 0x%02x\n", cap_type,
766 cap_size, (int)(itr - (void *)bos));
767 }
768 itr += cap_size;
769 }
770 result = 0;
771error_bad_cap:
772 return result;
773}
774
775/*
776 * Add information from the BOS descriptors to the device
777 *
778 * @usb_dev: referenced
779 * @wusb_dev: referenced and unlocked
780 *
781 * So what we do is we alloc a space for the BOS descriptor of 64
782 * bytes; read the first four bytes which include the wTotalLength
783 * field (WUSB1.0[T7-26]) and if it fits in those 64 bytes, read the
784 * whole thing. If not we realloc to that size.
785 *
786 * Then we call the groking function, that will fill up
787 * wusb_dev->wusb_cap_descr, which is what we'll need later on.
788 */
789static int wusb_dev_bos_add(struct usb_device *usb_dev,
790 struct wusb_dev *wusb_dev)
791{
792 ssize_t result;
793 struct device *dev = &usb_dev->dev;
794 struct usb_bos_descriptor *bos;
795 size_t alloc_size = 32, desc_size = 4;
796
797 bos = kmalloc(alloc_size, GFP_KERNEL);
798 if (bos == NULL)
799 return -ENOMEM;
800 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
801 if (result < 4) {
802 dev_err(dev, "Can't get BOS descriptor or too short: %zd\n",
803 result);
804 goto error_get_descriptor;
805 }
806 desc_size = le16_to_cpu(bos->wTotalLength);
807 if (desc_size >= alloc_size) {
808 kfree(bos);
809 alloc_size = desc_size;
810 bos = kmalloc(alloc_size, GFP_KERNEL);
811 if (bos == NULL)
812 return -ENOMEM;
813 }
814 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
815 if (result < 0 || result != desc_size) {
816 dev_err(dev, "Can't get BOS descriptor or too short (need "
817 "%zu bytes): %zd\n", desc_size, result);
818 goto error_get_descriptor;
819 }
820 if (result < sizeof(*bos)
821 || le16_to_cpu(bos->wTotalLength) != desc_size) {
822 dev_err(dev, "Can't get BOS descriptor or too short (need "
823 "%zu bytes): %zd\n", desc_size, result);
824 goto error_get_descriptor;
825 }
bce83697 826
b69fada6
IPG
827 result = wusb_dev_bos_grok(usb_dev, wusb_dev, bos, result);
828 if (result < 0)
829 goto error_bad_bos;
830 wusb_dev->bos = bos;
831 return 0;
832
833error_bad_bos:
834error_get_descriptor:
835 kfree(bos);
836 wusb_dev->wusb_cap_descr = NULL;
837 return result;
838}
839
840static void wusb_dev_bos_rm(struct wusb_dev *wusb_dev)
841{
842 kfree(wusb_dev->bos);
843 wusb_dev->wusb_cap_descr = NULL;
844};
845
846static struct usb_wireless_cap_descriptor wusb_cap_descr_default = {
847 .bLength = sizeof(wusb_cap_descr_default),
848 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
849 .bDevCapabilityType = USB_CAP_TYPE_WIRELESS_USB,
850
851 .bmAttributes = USB_WIRELESS_BEACON_NONE,
852 .wPHYRates = cpu_to_le16(USB_WIRELESS_PHY_53),
853 .bmTFITXPowerInfo = 0,
854 .bmFFITXPowerInfo = 0,
855 .bmBandGroup = cpu_to_le16(0x0001), /* WUSB1.0[7.4.1] bottom */
856 .bReserved = 0
857};
858
859/*
860 * USB stack's device addition Notifier Callback
861 *
862 * Called from drivers/usb/core/hub.c when a new device is added; we
863 * use this hook to perform certain WUSB specific setup work on the
864 * new device. As well, it is the first time we can connect the
865 * wusb_dev and the usb_dev. So we note it down in wusb_dev and take a
866 * reference that we'll drop.
867 *
868 * First we need to determine if the device is a WUSB device (else we
869 * ignore it). For that we use the speed setting (USB_SPEED_VARIABLE)
870 * [FIXME: maybe we'd need something more definitive]. If so, we track
871 * it's usb_busd and from there, the WUSB HC.
872 *
873 * Because all WUSB HCs are contained in a 'struct wusbhc', voila, we
874 * get the wusbhc for the device.
875 *
876 * We have a reference on @usb_dev (as we are called at the end of its
877 * enumeration).
878 *
879 * NOTE: @usb_dev locked
880 */
881static void wusb_dev_add_ncb(struct usb_device *usb_dev)
882{
883 int result = 0;
884 struct wusb_dev *wusb_dev;
885 struct wusbhc *wusbhc;
886 struct device *dev = &usb_dev->dev;
887 u8 port_idx;
888
889 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
890 return; /* skip non wusb and wusb RHs */
891
6da9c990
DV
892 usb_set_device_state(usb_dev, USB_STATE_UNAUTHENTICATED);
893
b69fada6
IPG
894 wusbhc = wusbhc_get_by_usb_dev(usb_dev);
895 if (wusbhc == NULL)
896 goto error_nodev;
897 mutex_lock(&wusbhc->mutex);
898 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, usb_dev);
899 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
900 mutex_unlock(&wusbhc->mutex);
901 if (wusb_dev == NULL)
902 goto error_nodev;
903 wusb_dev->usb_dev = usb_get_dev(usb_dev);
904 usb_dev->wusb_dev = wusb_dev_get(wusb_dev);
905 result = wusb_dev_sec_add(wusbhc, usb_dev, wusb_dev);
906 if (result < 0) {
907 dev_err(dev, "Cannot enable security: %d\n", result);
908 goto error_sec_add;
909 }
910 /* Now query the device for it's BOS and attach it to wusb_dev */
911 result = wusb_dev_bos_add(usb_dev, wusb_dev);
912 if (result < 0) {
913 dev_err(dev, "Cannot get BOS descriptors: %d\n", result);
914 goto error_bos_add;
915 }
916 result = wusb_dev_sysfs_add(wusbhc, usb_dev, wusb_dev);
917 if (result < 0)
918 goto error_add_sysfs;
919out:
920 wusb_dev_put(wusb_dev);
921 wusbhc_put(wusbhc);
922error_nodev:
b69fada6
IPG
923 return;
924
925 wusb_dev_sysfs_rm(wusb_dev);
926error_add_sysfs:
927 wusb_dev_bos_rm(wusb_dev);
928error_bos_add:
929 wusb_dev_sec_rm(wusb_dev);
930error_sec_add:
931 mutex_lock(&wusbhc->mutex);
932 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
933 mutex_unlock(&wusbhc->mutex);
934 goto out;
935}
936
937/*
938 * Undo all the steps done at connection by the notifier callback
939 *
940 * NOTE: @usb_dev locked
941 */
942static void wusb_dev_rm_ncb(struct usb_device *usb_dev)
943{
944 struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
945
946 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
947 return; /* skip non wusb and wusb RHs */
948
949 wusb_dev_sysfs_rm(wusb_dev);
950 wusb_dev_bos_rm(wusb_dev);
951 wusb_dev_sec_rm(wusb_dev);
952 wusb_dev->usb_dev = NULL;
953 usb_dev->wusb_dev = NULL;
954 wusb_dev_put(wusb_dev);
955 usb_put_dev(usb_dev);
956}
957
958/*
959 * Handle notifications from the USB stack (notifier call back)
960 *
961 * This is called when the USB stack does a
962 * usb_{bus,device}_{add,remove}() so we can do WUSB specific
963 * handling. It is called with [for the case of
964 * USB_DEVICE_{ADD,REMOVE} with the usb_dev locked.
965 */
966int wusb_usb_ncb(struct notifier_block *nb, unsigned long val,
967 void *priv)
968{
969 int result = NOTIFY_OK;
970
971 switch (val) {
972 case USB_DEVICE_ADD:
973 wusb_dev_add_ncb(priv);
974 break;
975 case USB_DEVICE_REMOVE:
976 wusb_dev_rm_ncb(priv);
977 break;
978 case USB_BUS_ADD:
979 /* ignore (for now) */
980 case USB_BUS_REMOVE:
981 break;
982 default:
983 WARN_ON(1);
984 result = NOTIFY_BAD;
985 };
986 return result;
987}
988
989/*
990 * Return a referenced wusb_dev given a @wusbhc and @usb_dev
991 */
992struct wusb_dev *__wusb_dev_get_by_usb_dev(struct wusbhc *wusbhc,
993 struct usb_device *usb_dev)
994{
995 struct wusb_dev *wusb_dev;
996 u8 port_idx;
997
998 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
999 BUG_ON(port_idx > wusbhc->ports_max);
1000 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
1001 if (wusb_dev != NULL) /* ops, device is gone */
1002 wusb_dev_get(wusb_dev);
1003 return wusb_dev;
1004}
1005EXPORT_SYMBOL_GPL(__wusb_dev_get_by_usb_dev);
1006
1007void wusb_dev_destroy(struct kref *_wusb_dev)
1008{
bce83697
DV
1009 struct wusb_dev *wusb_dev = container_of(_wusb_dev, struct wusb_dev, refcnt);
1010
b69fada6
IPG
1011 list_del_init(&wusb_dev->cack_node);
1012 wusb_dev_free(wusb_dev);
b69fada6
IPG
1013}
1014EXPORT_SYMBOL_GPL(wusb_dev_destroy);
1015
1016/*
1017 * Create all the device connect handling infrastructure
1018 *
1019 * This is basically the device info array, Connect Acknowledgement
1020 * (cack) lists, keep-alive timers (and delayed work thread).
1021 */
1022int wusbhc_devconnect_create(struct wusbhc *wusbhc)
1023{
b69fada6
IPG
1024 wusbhc->keep_alive_ie.hdr.bIEIdentifier = WUIE_ID_KEEP_ALIVE;
1025 wusbhc->keep_alive_ie.hdr.bLength = sizeof(wusbhc->keep_alive_ie.hdr);
1026 INIT_DELAYED_WORK(&wusbhc->keep_alive_timer, wusbhc_keep_alive_run);
1027
1028 wusbhc->cack_ie.hdr.bIEIdentifier = WUIE_ID_CONNECTACK;
1029 wusbhc->cack_ie.hdr.bLength = sizeof(wusbhc->cack_ie.hdr);
1030 INIT_LIST_HEAD(&wusbhc->cack_list);
1031
b69fada6
IPG
1032 return 0;
1033}
1034
1035/*
1036 * Release all resources taken by the devconnect stuff
1037 */
1038void wusbhc_devconnect_destroy(struct wusbhc *wusbhc)
1039{
bce83697 1040 /* no op */
b69fada6
IPG
1041}
1042
1043/*
1044 * wusbhc_devconnect_start - start accepting device connections
1045 * @wusbhc: the WUSB HC
1046 *
1047 * Sets the Host Info IE to accept all new connections.
1048 *
1049 * FIXME: This also enables the keep alives but this is not necessary
1050 * until there are connected and authenticated devices.
1051 */
6fae35f9 1052int wusbhc_devconnect_start(struct wusbhc *wusbhc)
b69fada6
IPG
1053{
1054 struct device *dev = wusbhc->dev;
1055 struct wuie_host_info *hi;
1056 int result;
1057
1058 hi = kzalloc(sizeof(*hi), GFP_KERNEL);
1059 if (hi == NULL)
1060 return -ENOMEM;
1061
1062 hi->hdr.bLength = sizeof(*hi);
1063 hi->hdr.bIEIdentifier = WUIE_ID_HOST_INFO;
1064 hi->attributes = cpu_to_le16((wusbhc->rsv->stream << 3) | WUIE_HI_CAP_ALL);
6fae35f9 1065 hi->CHID = wusbhc->chid;
b69fada6
IPG
1066 result = wusbhc_mmcie_set(wusbhc, 0, 0, &hi->hdr);
1067 if (result < 0) {
1068 dev_err(dev, "Cannot add Host Info MMCIE: %d\n", result);
1069 goto error_mmcie_set;
1070 }
1071 wusbhc->wuie_host_info = hi;
1072
1073 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
1074 (wusbhc->trust_timeout*CONFIG_HZ)/1000/2);
1075
1076 return 0;
1077
1078error_mmcie_set:
1079 kfree(hi);
1080 return result;
1081}
1082
1083/*
1084 * wusbhc_devconnect_stop - stop managing connected devices
1085 * @wusbhc: the WUSB HC
1086 *
1087 * Removes the Host Info IE and stops the keep alives.
1088 *
1089 * FIXME: should this disconnect all devices?
1090 */
1091void wusbhc_devconnect_stop(struct wusbhc *wusbhc)
1092{
1093 cancel_delayed_work_sync(&wusbhc->keep_alive_timer);
1094 WARN_ON(!list_empty(&wusbhc->cack_list));
1095
1096 wusbhc_mmcie_rm(wusbhc, &wusbhc->wuie_host_info->hdr);
1097 kfree(wusbhc->wuie_host_info);
1098 wusbhc->wuie_host_info = NULL;
1099}
1100
1101/*
1102 * wusb_set_dev_addr - set the WUSB device address used by the host
1103 * @wusbhc: the WUSB HC the device is connect to
1104 * @wusb_dev: the WUSB device
1105 * @addr: new device address
1106 */
1107int wusb_set_dev_addr(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev, u8 addr)
1108{
1109 int result;
1110
1111 wusb_dev->addr = addr;
1112 result = wusbhc->dev_info_set(wusbhc, wusb_dev);
f51c23ba 1113 if (result < 0)
b69fada6
IPG
1114 dev_err(wusbhc->dev, "device %d: failed to set device "
1115 "address\n", wusb_dev->port_idx);
1116 else
1117 dev_info(wusbhc->dev, "device %d: %s addr %u\n",
1118 wusb_dev->port_idx,
1119 (addr & WUSB_DEV_ADDR_UNAUTH) ? "unauth" : "auth",
1120 wusb_dev->addr);
1121
1122 return result;
1123}