USB: wusbcore: correct spelling mistakes in comments and error string
[deliverable/linux.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>
5a0e3ad6 91#include <linux/slab.h>
b69fada6 92#include <linux/workqueue.h>
f940fcd8 93#include <linux/export.h>
b69fada6
IPG
94#include "wusbhc.h"
95
b69fada6
IPG
96static void wusbhc_devconnect_acked_work(struct work_struct *work);
97
98static void wusb_dev_free(struct wusb_dev *wusb_dev)
99{
471e42ad 100 kfree(wusb_dev);
b69fada6
IPG
101}
102
103static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc)
104{
105 struct wusb_dev *wusb_dev;
b69fada6
IPG
106
107 wusb_dev = kzalloc(sizeof(*wusb_dev), GFP_KERNEL);
108 if (wusb_dev == NULL)
109 goto err;
110
111 wusb_dev->wusbhc = wusbhc;
112
113 INIT_WORK(&wusb_dev->devconnect_acked_work, wusbhc_devconnect_acked_work);
114
b69fada6
IPG
115 return wusb_dev;
116err:
117 wusb_dev_free(wusb_dev);
118 return NULL;
119}
120
121
122/*
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.
125 *
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.
129 *
130 * @wusbhc->mutex must be taken
131 */
132static void wusbhc_fill_cack_ie(struct wusbhc *wusbhc)
133{
134 unsigned cnt;
135 struct wusb_dev *dev_itr;
136 struct wuie_connect_ack *cack_ie;
137
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;
145 }
146 cack_ie->hdr.bLength = sizeof(cack_ie->hdr)
147 + cnt * sizeof(cack_ie->blk[0]);
148}
149
150/*
151 * Register a new device that wants to connect
152 *
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.
156 *
157 * @dev_addr: address to use for the device (which is also the port
158 * number).
159 *
160 * @wusbhc->mutex must be taken
161 */
162static struct wusb_dev *wusbhc_cack_add(struct wusbhc *wusbhc,
163 struct wusb_dn_connect *dnc,
164 const char *pr_cdid, u8 port_idx)
165{
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;
171
b69fada6
IPG
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);
8092d7c9 179 if (wusb_dev == NULL)
b69fada6 180 return NULL;
b69fada6
IPG
181 wusb_dev_init(wusb_dev);
182 wusb_dev->cdid = dnc->CDID;
183 wusb_dev->port_idx = port_idx;
184
185 /*
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.
191 */
192 bitmap_fill(wusb_dev->availability.bm, UWB_NUM_MAS);
193
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;
200
201 dev_info(dev, "Connecting new WUSB device to address %u, "
202 "port %u\n", dev_addr, port_idx);
203
204 result = wusb_set_dev_addr(wusbhc, wusb_dev, dev_addr);
f51c23ba
AL
205 if (result < 0)
206 return NULL;
b69fada6
IPG
207 }
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);
bce83697 212
b69fada6
IPG
213 return wusb_dev;
214}
215
216/*
217 * Remove a Connect-Ack context entry from the HCs view
218 *
219 * @wusbhc->mutex must be taken
220 */
221static void wusbhc_cack_rm(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
222{
b69fada6
IPG
223 list_del_init(&wusb_dev->cack_node);
224 wusbhc->cack_count--;
225 wusbhc_fill_cack_ie(wusbhc);
b69fada6
IPG
226}
227
228/*
229 * @wusbhc->mutex must be taken */
230static
231void wusbhc_devconnect_acked(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
232{
b69fada6
IPG
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);
b69fada6
IPG
238}
239
240static void wusbhc_devconnect_acked_work(struct work_struct *work)
241{
242 struct wusb_dev *wusb_dev = container_of(work, struct wusb_dev,
243 devconnect_acked_work);
244 struct wusbhc *wusbhc = wusb_dev->wusbhc;
245
246 mutex_lock(&wusbhc->mutex);
247 wusbhc_devconnect_acked(wusbhc, wusb_dev);
248 mutex_unlock(&wusbhc->mutex);
5936ac7f
DV
249
250 wusb_dev_put(wusb_dev);
b69fada6
IPG
251}
252
253/*
254 * Ack a device for connection
255 *
256 * FIXME: docs
257 *
258 * @pr_cdid: Printable CDID...hex Use @dnc->cdid for the real deal.
259 *
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.
264 *
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
1076e7a4 268 * initial connect phase the USB stack has no knowledge of
b69fada6 269 * the device and hasn't assigned an address yet--we know
1076e7a4 270 * USB's choose_address() will use the same heuristics we
b69fada6
IPG
271 * use here, so we can assume which address will be assigned.
272 *
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).
276 *
277 * @wusbhc shall be referenced
278 */
279static
280void wusbhc_devconnect_ack(struct wusbhc *wusbhc, struct wusb_dn_connect *dnc,
281 const char *pr_cdid)
282{
283 int result;
284 struct device *dev = wusbhc->dev;
285 struct wusb_dev *wusb_dev;
286 struct wusb_port *port;
287 unsigned idx, devnum;
288
b69fada6
IPG
289 mutex_lock(&wusbhc->mutex);
290
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
8092d7c9 295 && memcmp(&dnc->CDID, &port->wusb_dev->cdid, sizeof(dnc->CDID)) == 0)
b69fada6 296 goto error_unlock;
b69fada6
IPG
297 }
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;
304 }
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;
313 }
314
315 devnum = idx + 2;
316
317 /* Make sure we are using no crypto on that "virtual port" */
318 wusbhc->set_ptk(wusbhc, idx, 0, NULL, 0);
319
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;
b69fada6
IPG
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
4656d5de 337 * and it calls back into wusbhc_rh_port_reset().
b69fada6
IPG
338 */
339error_unlock:
340 mutex_unlock(&wusbhc->mutex);
b69fada6
IPG
341 return;
342
343}
344
345/*
346 * Disconnect a Wireless USB device from its fake port
347 *
348 * Marks the port as disconnected so that khubd can pick up the change
349 * and drops our knowledge about the device.
350 *
351 * Assumes there is a device connected
352 *
353 * @port_index: zero based port number
354 *
355 * NOTE: @wusbhc->mutex is locked
356 *
357 * WARNING: From here it is not very safe to access anything hanging off
358 * wusb_dev
359 */
360static void __wusbhc_dev_disconnect(struct wusbhc *wusbhc,
361 struct wusb_port *port)
362{
b69fada6
IPG
363 struct wusb_dev *wusb_dev = port->wusb_dev;
364
b69fada6
IPG
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) {
9a9b1d17 370 dev_dbg(wusbhc->dev, "disconnecting device from port %d\n", wusb_dev->port_idx);
b69fada6
IPG
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);
375 }
376 port->wusb_dev = NULL;
b69fada6
IPG
377
378 /* After a device disconnects, change the GTK (see [WUSB]
379 * section 6.2.11.2). */
8db324ec
DV
380 if (wusbhc->active)
381 wusbhc_gtk_rekey(wusbhc);
b69fada6 382
b69fada6
IPG
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
386 */
387}
388
b69fada6
IPG
389/*
390 * Refresh the list of keep alives to emit in the MMC
391 *
b69fada6
IPG
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).
396 *
397 * This might cause the first devices to timeout the last devices in
398 * the port array...FIXME: come up with a better algorithm?
399 *
400 * Note we can't do much about MMC's ops errors; we hope next refresh
401 * will kind of handle it.
402 *
403 * NOTE: @wusbhc->mutex is locked
404 */
405static void __wusbhc_keep_alive(struct wusbhc *wusbhc)
406{
b69fada6
IPG
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;
413
b69fada6
IPG
414 old_keep_alives = ie->hdr.bLength - sizeof(ie->hdr);
415 keep_alives = 0;
416 for (cnt = 0;
a23b6484 417 keep_alives < WUIE_ELT_MAX && cnt < wusbhc->ports_max;
b69fada6
IPG
418 cnt++) {
419 unsigned tt = msecs_to_jiffies(wusbhc->trust_timeout);
420
421 wusb_port = wusb_port_by_idx(wusbhc, cnt);
422 wusb_dev = wusb_port->wusb_dev;
423
424 if (wusb_dev == NULL)
425 continue;
f4042c06 426 if (wusb_dev->usb_dev == NULL)
b69fada6
IPG
427 continue;
428
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);
7d9852a8
TP
433 } else if (time_after(jiffies, wusb_dev->entry_ts + tt/3)) {
434 /* Approaching timeout cut off, need to refresh */
b69fada6
IPG
435 ie->bDeviceAddress[keep_alives++] = wusb_dev->addr;
436 }
437 }
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]);
8092d7c9
DV
442 if (keep_alives > 0)
443 wusbhc_mmcie_set(wusbhc, 10, 5, &ie->hdr);
444 else if (old_keep_alives != 0)
b69fada6 445 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
b69fada6
IPG
446}
447
448/*
449 * Do a run through all devices checking for timeouts
450 */
451static void wusbhc_keep_alive_run(struct work_struct *ws)
452{
bf6aede7 453 struct delayed_work *dw = to_delayed_work(ws);
56968d0c
DV
454 struct wusbhc *wusbhc = container_of(dw, struct wusbhc, keep_alive_timer);
455
456 mutex_lock(&wusbhc->mutex);
457 __wusbhc_keep_alive(wusbhc);
458 mutex_unlock(&wusbhc->mutex);
459
460 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
461 msecs_to_jiffies(wusbhc->trust_timeout / 2));
b69fada6
IPG
462}
463
464/*
465 * Find the wusb_dev from its device address.
466 *
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.
470 */
471static struct wusb_dev *wusbhc_find_dev_by_addr(struct wusbhc *wusbhc, u8 addr)
472{
473 int p;
474
475 if (addr == 0xff) /* unconnected */
476 return NULL;
477
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;
483 }
484
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;
490 }
491 return NULL;
492}
493
494/*
495 * Handle a DN_Alive notification (WUSB1.0[7.6.1])
496 *
497 * This just updates the device activity timestamp and then refreshes
498 * the keep alive IE.
499 *
500 * @wusbhc shall be referenced and unlocked
501 */
6161ae5f 502static void wusbhc_handle_dn_alive(struct wusbhc *wusbhc, u8 srcaddr)
b69fada6 503{
6161ae5f
TP
504 struct wusb_dev *wusb_dev;
505
b69fada6 506 mutex_lock(&wusbhc->mutex);
6161ae5f
TP
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);
514 }
b69fada6
IPG
515 mutex_unlock(&wusbhc->mutex);
516}
517
518/*
519 * Handle a DN_Connect notification (WUSB1.0[7.6.1])
520 *
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.
527 *
528 * @wusbhc->mutex shall be held
529 */
530static void wusbhc_handle_dn_connect(struct wusbhc *wusbhc,
531 struct wusb_dn_hdr *dn_hdr,
532 size_t size)
533{
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"
542 };
543
b69fada6
IPG
544 if (size < sizeof(*dnc)) {
545 dev_err(dev, "DN CONNECT: short notification (%zu < %zu)\n",
546 size, sizeof(*dnc));
bce83697 547 return;
b69fada6
IPG
548 }
549
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);
b69fada6
IPG
559}
560
561/*
562 * Handle a DN_Disconnect notification (WUSB1.0[7.6.1])
563 *
564 * Device is going down -- do the disconnect.
565 *
566 * @wusbhc shall be referenced and unlocked
567 */
6161ae5f 568static void wusbhc_handle_dn_disconnect(struct wusbhc *wusbhc, u8 srcaddr)
b69fada6
IPG
569{
570 struct device *dev = wusbhc->dev;
6161ae5f 571 struct wusb_dev *wusb_dev;
b69fada6
IPG
572
573 mutex_lock(&wusbhc->mutex);
6161ae5f
TP
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));
583 }
b69fada6
IPG
584 mutex_unlock(&wusbhc->mutex);
585}
586
b69fada6
IPG
587/*
588 * Handle a Device Notification coming a host
589 *
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.
593 *
594 * Invalid DNs (e.g., too short) are discarded.
595 *
596 * @wusbhc shall be referenced
597 *
598 * FIXMES:
599 * - implement priorities as in WUSB1.0[Table 7-55]?
600 */
601void wusbhc_handle_dn(struct wusbhc *wusbhc, u8 srcaddr,
602 struct wusb_dn_hdr *dn_hdr, size_t size)
603{
604 struct device *dev = wusbhc->dev;
b69fada6 605
b69fada6
IPG
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));
bce83697 609 return;
b69fada6 610 }
b69fada6
IPG
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:
6161ae5f 616 wusbhc_handle_dn_alive(wusbhc, srcaddr);
b69fada6
IPG
617 break;
618 case WUSB_DN_DISCONNECT:
6161ae5f 619 wusbhc_handle_dn_disconnect(wusbhc, srcaddr);
b69fada6
IPG
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);
632 }
b69fada6
IPG
633}
634EXPORT_SYMBOL_GPL(wusbhc_handle_dn);
635
636/*
637 * Disconnect a WUSB device from a the cluster
638 *
639 * @wusbhc
640 * @port Fake port where the device is (wusbhc index, not USB port number).
641 *
642 * In Wireless USB, a disconnect is basically telling the device he is
643 * being disconnected and forgetting about him.
644 *
645 * We send the device a Device Disconnect IE (WUSB1.0[7.5.11]) for 100
646 * ms and then keep going.
647 *
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.
652 *
653 * @wusbhc should be refcounted and locked
654 */
655void __wusbhc_dev_disable(struct wusbhc *wusbhc, u8 port_idx)
656{
657 int result;
658 struct device *dev = wusbhc->dev;
659 struct wusb_dev *wusb_dev;
660 struct wuie_disconnect *ie;
661
b69fada6
IPG
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);
bce83697 667 return;
b69fada6
IPG
668 }
669 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
670
b69fada6
IPG
671 ie = kzalloc(sizeof(*ie), GFP_KERNEL);
672 if (ie == NULL)
bce83697 673 return;
b69fada6
IPG
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);
bce83697 678 if (result < 0)
b69fada6 679 dev_err(dev, "DISCONNECT: can't set MMC: %d\n", result);
bce83697
DV
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);
b69fada6 684 }
b69fada6 685 kfree(ie);
b69fada6
IPG
686}
687
688/*
689 * Walk over the BOS descriptor, verify and grok it
690 *
691 * @usb_dev: referenced
692 * @wusb_dev: referenced and unlocked
693 *
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.
698 *
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).
703 */
704static 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)
707{
708 ssize_t result;
709 struct device *dev = &usb_dev->dev;
710 void *itr, *top;
711
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;
725 }
726 cap_size = cap_hdr->bLength;
727 cap_type = cap_hdr->bDevCapabilityType;
b69fada6
IPG
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;
738 }
b69fada6
IPG
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));
bce83697 746 else
b69fada6 747 wusb_dev->wusb_cap_descr = itr;
b69fada6
IPG
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));
753 }
754 itr += cap_size;
755 }
756 result = 0;
757error_bad_cap:
758 return result;
759}
760
761/*
762 * Add information from the BOS descriptors to the device
763 *
764 * @usb_dev: referenced
765 * @wusb_dev: referenced and unlocked
766 *
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.
771 *
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.
774 */
775static int wusb_dev_bos_add(struct usb_device *usb_dev,
776 struct wusb_dev *wusb_dev)
777{
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;
782
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;
791 }
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;
799 }
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;
805 }
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;
811 }
bce83697 812
b69fada6
IPG
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;
818
819error_bad_bos:
820error_get_descriptor:
821 kfree(bos);
822 wusb_dev->wusb_cap_descr = NULL;
823 return result;
824}
825
826static void wusb_dev_bos_rm(struct wusb_dev *wusb_dev)
827{
828 kfree(wusb_dev->bos);
829 wusb_dev->wusb_cap_descr = NULL;
830};
831
b69fada6
IPG
832/*
833 * USB stack's device addition Notifier Callback
834 *
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.
840 *
841 * First we need to determine if the device is a WUSB device (else we
551cdbbe 842 * ignore it). For that we use the speed setting (USB_SPEED_WIRELESS)
b69fada6
IPG
843 * [FIXME: maybe we'd need something more definitive]. If so, we track
844 * it's usb_busd and from there, the WUSB HC.
845 *
846 * Because all WUSB HCs are contained in a 'struct wusbhc', voila, we
847 * get the wusbhc for the device.
848 *
849 * We have a reference on @usb_dev (as we are called at the end of its
850 * enumeration).
851 *
852 * NOTE: @usb_dev locked
853 */
854static void wusb_dev_add_ncb(struct usb_device *usb_dev)
855{
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;
861
862 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
863 return; /* skip non wusb and wusb RHs */
864
6da9c990
DV
865 usb_set_device_state(usb_dev, USB_STATE_UNAUTHENTICATED);
866
b69fada6
IPG
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;
882 }
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;
888 }
889 result = wusb_dev_sysfs_add(wusbhc, usb_dev, wusb_dev);
890 if (result < 0)
891 goto error_add_sysfs;
892out:
893 wusb_dev_put(wusb_dev);
894 wusbhc_put(wusbhc);
895error_nodev:
b69fada6
IPG
896 return;
897
898 wusb_dev_sysfs_rm(wusb_dev);
899error_add_sysfs:
900 wusb_dev_bos_rm(wusb_dev);
901error_bos_add:
902 wusb_dev_sec_rm(wusb_dev);
903error_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;
908}
909
910/*
911 * Undo all the steps done at connection by the notifier callback
912 *
913 * NOTE: @usb_dev locked
914 */
915static void wusb_dev_rm_ncb(struct usb_device *usb_dev)
916{
917 struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
918
919 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
920 return; /* skip non wusb and wusb RHs */
921
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);
929}
930
931/*
932 * Handle notifications from the USB stack (notifier call back)
933 *
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.
938 */
939int wusb_usb_ncb(struct notifier_block *nb, unsigned long val,
940 void *priv)
941{
942 int result = NOTIFY_OK;
943
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;
2b84f92b 958 }
b69fada6
IPG
959 return result;
960}
961
962/*
963 * Return a referenced wusb_dev given a @wusbhc and @usb_dev
964 */
965struct wusb_dev *__wusb_dev_get_by_usb_dev(struct wusbhc *wusbhc,
966 struct usb_device *usb_dev)
967{
968 struct wusb_dev *wusb_dev;
969 u8 port_idx;
970
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;
977}
978EXPORT_SYMBOL_GPL(__wusb_dev_get_by_usb_dev);
979
980void wusb_dev_destroy(struct kref *_wusb_dev)
981{
bce83697
DV
982 struct wusb_dev *wusb_dev = container_of(_wusb_dev, struct wusb_dev, refcnt);
983
b69fada6
IPG
984 list_del_init(&wusb_dev->cack_node);
985 wusb_dev_free(wusb_dev);
b69fada6
IPG
986}
987EXPORT_SYMBOL_GPL(wusb_dev_destroy);
988
989/*
990 * Create all the device connect handling infrastructure
991 *
992 * This is basically the device info array, Connect Acknowledgement
993 * (cack) lists, keep-alive timers (and delayed work thread).
994 */
995int wusbhc_devconnect_create(struct wusbhc *wusbhc)
996{
b69fada6
IPG
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);
1000
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);
1004
b69fada6
IPG
1005 return 0;
1006}
1007
1008/*
1009 * Release all resources taken by the devconnect stuff
1010 */
1011void wusbhc_devconnect_destroy(struct wusbhc *wusbhc)
1012{
bce83697 1013 /* no op */
b69fada6
IPG
1014}
1015
1016/*
1017 * wusbhc_devconnect_start - start accepting device connections
1018 * @wusbhc: the WUSB HC
1019 *
1020 * Sets the Host Info IE to accept all new connections.
1021 *
1022 * FIXME: This also enables the keep alives but this is not necessary
1023 * until there are connected and authenticated devices.
1024 */
6fae35f9 1025int wusbhc_devconnect_start(struct wusbhc *wusbhc)
b69fada6
IPG
1026{
1027 struct device *dev = wusbhc->dev;
1028 struct wuie_host_info *hi;
1029 int result;
1030
1031 hi = kzalloc(sizeof(*hi), GFP_KERNEL);
1032 if (hi == NULL)
1033 return -ENOMEM;
1034
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);
6fae35f9 1038 hi->CHID = wusbhc->chid;
b69fada6
IPG
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;
1043 }
1044 wusbhc->wuie_host_info = hi;
1045
1046 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
7d9852a8 1047 msecs_to_jiffies(wusbhc->trust_timeout / 2));
b69fada6
IPG
1048
1049 return 0;
1050
1051error_mmcie_set:
1052 kfree(hi);
1053 return result;
1054}
1055
1056/*
1057 * wusbhc_devconnect_stop - stop managing connected devices
1058 * @wusbhc: the WUSB HC
1059 *
8db324ec
DV
1060 * Disconnects any devices still connected, stops the keep alives and
1061 * removes the Host Info IE.
b69fada6
IPG
1062 */
1063void wusbhc_devconnect_stop(struct wusbhc *wusbhc)
1064{
8db324ec 1065 int i;
b69fada6 1066
8db324ec
DV
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]);
1071 }
1072 mutex_unlock(&wusbhc->mutex);
1073
1074 cancel_delayed_work_sync(&wusbhc->keep_alive_timer);
b69fada6
IPG
1075 wusbhc_mmcie_rm(wusbhc, &wusbhc->wuie_host_info->hdr);
1076 kfree(wusbhc->wuie_host_info);
1077 wusbhc->wuie_host_info = NULL;
1078}
1079
1080/*
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
1085 */
1086int wusb_set_dev_addr(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev, u8 addr)
1087{
1088 int result;
1089
1090 wusb_dev->addr = addr;
1091 result = wusbhc->dev_info_set(wusbhc, wusb_dev);
f51c23ba 1092 if (result < 0)
b69fada6
IPG
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);
1100
1101 return result;
1102}
This page took 0.437034 seconds and 5 git commands to generate.