2 * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
3 * Device Connect handling
5 * Copyright (C) 2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * FIXME: this file needs to be broken up, it's grown too big
27 * WUSB1.0[7.1, 7.5.1, ]
29 * WUSB device connection is kind of messy. Some background:
31 * When a device wants to connect it scans the UWB radio channels
32 * looking for a WUSB Channel; a WUSB channel is defined by MMCs
33 * (Micro Managed Commands or something like that) [see
34 * Design-overview for more on this] .
36 * So, device scans the radio, finds MMCs and thus a host and checks
37 * when the next DNTS is. It sends a Device Notification Connect
38 * (DN_Connect); the host picks it up (through nep.c and notif.c, ends
39 * up in wusb_devconnect_ack(), which creates a wusb_dev structure in
40 * wusbhc->port[port_number].wusb_dev), assigns an unauth address
41 * to the device (this means from 0x80 to 0xfe) and sends, in the MMC
42 * a Connect Ack Information Element (ConnAck IE).
44 * So now the device now has a WUSB address. From now on, we use
45 * that to talk to it in the RPipes.
49 * - We use the the as device address the port number where it is
50 * connected (port 0 doesn't exist). For unauth, it is 128 + that.
54 * This file contains the logic for doing that--entry points:
56 * wusb_devconnect_ack() Ack a device until _acked() called.
57 * Called by notif.c:wusb_handle_dn_connect()
58 * when a DN_Connect is received.
60 * 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
69 * __wusb_dev_disable() Called by rh.c:wusbhc_rh_clear_port_feat() when
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
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>
96 static void wusbhc_devconnect_acked_work(struct work_struct
*work
);
98 static void wusb_dev_free(struct wusb_dev
*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
)
111 wusb_dev
->wusbhc
= wusbhc
;
113 INIT_WORK(&wusb_dev
->devconnect_acked_work
, wusbhc_devconnect_acked_work
);
117 wusb_dev_free(wusb_dev
);
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
)
135 struct wusb_dev
*dev_itr
;
136 struct wuie_connect_ack
*cack_ie
;
138 cack_ie
= &wusbhc
->cack_ie
;
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
)
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
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
);
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
)))
177 /* We don't have it, create an entry, register it */
178 wusb_dev
= wusb_dev_alloc(wusbhc
);
179 if (wusb_dev
== 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
192 bitmap_fill(wusb_dev
->availability
.bm
, UWB_NUM_MAS
);
194 /* FIXME: handle reconnects instead of assuming connects are
196 if (1 && new_connection
== 0)
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
);
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
);
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 */
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
);
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
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
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
277 * @wusbhc shall be referenced
280 void wusbhc_devconnect_ack(struct wusbhc
*wusbhc
, struct wusb_dn_connect
*dnc
,
284 struct device
*dev
= wusbhc
->dev
;
285 struct wusb_dev
*wusb_dev
;
286 struct wusb_port
*port
;
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
);
295 && memcmp(&dnc
->CDID
, &port
->wusb_dev
->cdid
, sizeof(dnc
->CDID
)) == 0)
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
))
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? */
315 /* Make sure we are using no crypto on that "virtual port" */
316 wusbhc
->set_ptk(wusbhc
, idx
, 0, NULL
, 0);
318 /* Grab a filled in Connect-Ack context, fill out the
319 * Connect-Ack Wireless USB IE, set the MMC */
320 wusb_dev
= wusbhc_cack_add(wusbhc
, dnc
, pr_cdid
, idx
);
321 if (wusb_dev
== NULL
)
323 result
= wusbhc_mmcie_set(wusbhc
, 0, 0, &wusbhc
->cack_ie
.hdr
);
326 /* Give the device at least 2ms (WUSB1.0[7.5.1p3]), let's do
327 * three for a good measure */
329 port
->wusb_dev
= wusb_dev
;
330 port
->status
|= USB_PORT_STAT_CONNECTION
;
331 port
->change
|= USB_PORT_STAT_C_CONNECTION
;
332 /* Now the port status changed to connected; hub_wq will
333 * pick the change up and try to reset the port to bring it to
334 * the enabled state--so this process returns up to the stack
335 * and it calls back into wusbhc_rh_port_reset().
338 mutex_unlock(&wusbhc
->mutex
);
344 * Disconnect a Wireless USB device from its fake port
346 * Marks the port as disconnected so that hub_wq can pick up the change
347 * and drops our knowledge about the device.
349 * Assumes there is a device connected
351 * @port_index: zero based port number
353 * NOTE: @wusbhc->mutex is locked
355 * WARNING: From here it is not very safe to access anything hanging off
358 static void __wusbhc_dev_disconnect(struct wusbhc
*wusbhc
,
359 struct wusb_port
*port
)
361 struct wusb_dev
*wusb_dev
= port
->wusb_dev
;
363 port
->status
&= ~(USB_PORT_STAT_CONNECTION
| USB_PORT_STAT_ENABLE
364 | USB_PORT_STAT_SUSPEND
| USB_PORT_STAT_RESET
365 | USB_PORT_STAT_LOW_SPEED
| USB_PORT_STAT_HIGH_SPEED
);
366 port
->change
|= USB_PORT_STAT_C_CONNECTION
| USB_PORT_STAT_C_ENABLE
;
368 dev_dbg(wusbhc
->dev
, "disconnecting device from port %d\n", wusb_dev
->port_idx
);
369 if (!list_empty(&wusb_dev
->cack_node
))
370 list_del_init(&wusb_dev
->cack_node
);
371 /* For the one in cack_add() */
372 wusb_dev_put(wusb_dev
);
374 port
->wusb_dev
= NULL
;
376 /* After a device disconnects, change the GTK (see [WUSB]
377 * section 6.2.11.2). */
379 wusbhc_gtk_rekey(wusbhc
);
381 /* The Wireless USB part has forgotten about the device already; now
382 * hub_wq's timer will pick up the disconnection and remove the USB
383 * device from the system
388 * Refresh the list of keep alives to emit in the MMC
390 * We only publish the first four devices that have a coming timeout
391 * condition. Then when we are done processing those, we go for the
392 * next ones. We ignore the ones that have timed out already (they'll
395 * This might cause the first devices to timeout the last devices in
396 * the port array...FIXME: come up with a better algorithm?
398 * Note we can't do much about MMC's ops errors; we hope next refresh
399 * will kind of handle it.
401 * NOTE: @wusbhc->mutex is locked
403 static void __wusbhc_keep_alive(struct wusbhc
*wusbhc
)
405 struct device
*dev
= wusbhc
->dev
;
407 struct wusb_dev
*wusb_dev
;
408 struct wusb_port
*wusb_port
;
409 struct wuie_keep_alive
*ie
= &wusbhc
->keep_alive_ie
;
410 unsigned keep_alives
, old_keep_alives
;
412 old_keep_alives
= ie
->hdr
.bLength
- sizeof(ie
->hdr
);
415 keep_alives
< WUIE_ELT_MAX
&& cnt
< wusbhc
->ports_max
;
417 unsigned tt
= msecs_to_jiffies(wusbhc
->trust_timeout
);
419 wusb_port
= wusb_port_by_idx(wusbhc
, cnt
);
420 wusb_dev
= wusb_port
->wusb_dev
;
422 if (wusb_dev
== NULL
)
424 if (wusb_dev
->usb_dev
== NULL
)
427 if (time_after(jiffies
, wusb_dev
->entry_ts
+ tt
)) {
428 dev_err(dev
, "KEEPALIVE: device %u timed out\n",
430 __wusbhc_dev_disconnect(wusbhc
, wusb_port
);
431 } else if (time_after(jiffies
, wusb_dev
->entry_ts
+ tt
/3)) {
432 /* Approaching timeout cut off, need to refresh */
433 ie
->bDeviceAddress
[keep_alives
++] = wusb_dev
->addr
;
436 if (keep_alives
& 0x1) /* pad to even number ([WUSB] section 7.5.9) */
437 ie
->bDeviceAddress
[keep_alives
++] = 0x7f;
438 ie
->hdr
.bLength
= sizeof(ie
->hdr
) +
439 keep_alives
*sizeof(ie
->bDeviceAddress
[0]);
441 wusbhc_mmcie_set(wusbhc
, 10, 5, &ie
->hdr
);
442 else if (old_keep_alives
!= 0)
443 wusbhc_mmcie_rm(wusbhc
, &ie
->hdr
);
447 * Do a run through all devices checking for timeouts
449 static void wusbhc_keep_alive_run(struct work_struct
*ws
)
451 struct delayed_work
*dw
= to_delayed_work(ws
);
452 struct wusbhc
*wusbhc
= container_of(dw
, struct wusbhc
, keep_alive_timer
);
454 mutex_lock(&wusbhc
->mutex
);
455 __wusbhc_keep_alive(wusbhc
);
456 mutex_unlock(&wusbhc
->mutex
);
458 queue_delayed_work(wusbd
, &wusbhc
->keep_alive_timer
,
459 msecs_to_jiffies(wusbhc
->trust_timeout
/ 2));
463 * Find the wusb_dev from its device address.
465 * The device can be found directly from the address (see
466 * wusb_cack_add() for where the device address is set to port_idx
467 * +2), except when the address is zero.
469 static struct wusb_dev
*wusbhc_find_dev_by_addr(struct wusbhc
*wusbhc
, u8 addr
)
473 if (addr
== 0xff) /* unconnected */
477 int port
= (addr
& ~0x80) - 2;
478 if (port
< 0 || port
>= wusbhc
->ports_max
)
480 return wusb_port_by_idx(wusbhc
, port
)->wusb_dev
;
483 /* Look for the device with address 0. */
484 for (p
= 0; p
< wusbhc
->ports_max
; p
++) {
485 struct wusb_dev
*wusb_dev
= wusb_port_by_idx(wusbhc
, p
)->wusb_dev
;
486 if (wusb_dev
&& wusb_dev
->addr
== addr
)
493 * Handle a DN_Alive notification (WUSB1.0[7.6.1])
495 * This just updates the device activity timestamp and then refreshes
498 * @wusbhc shall be referenced and unlocked
500 static void wusbhc_handle_dn_alive(struct wusbhc
*wusbhc
, u8 srcaddr
)
502 struct wusb_dev
*wusb_dev
;
504 mutex_lock(&wusbhc
->mutex
);
505 wusb_dev
= wusbhc_find_dev_by_addr(wusbhc
, srcaddr
);
506 if (wusb_dev
== NULL
) {
507 dev_dbg(wusbhc
->dev
, "ignoring DN_Alive from unconnected device %02x\n",
510 wusb_dev
->entry_ts
= jiffies
;
511 __wusbhc_keep_alive(wusbhc
);
513 mutex_unlock(&wusbhc
->mutex
);
517 * Handle a DN_Connect notification (WUSB1.0[7.6.1])
521 * @size: Size of the buffer where the notification resides; if the
522 * notification data suggests there should be more data than
523 * available, an error will be signaled and the whole buffer
526 * @wusbhc->mutex shall be held
528 static void wusbhc_handle_dn_connect(struct wusbhc
*wusbhc
,
529 struct wusb_dn_hdr
*dn_hdr
,
532 struct device
*dev
= wusbhc
->dev
;
533 struct wusb_dn_connect
*dnc
;
534 char pr_cdid
[WUSB_CKHDID_STRSIZE
];
535 static const char *beacon_behaviour
[] = {
542 if (size
< sizeof(*dnc
)) {
543 dev_err(dev
, "DN CONNECT: short notification (%zu < %zu)\n",
548 dnc
= container_of(dn_hdr
, struct wusb_dn_connect
, hdr
);
549 ckhdid_printf(pr_cdid
, sizeof(pr_cdid
), &dnc
->CDID
);
550 dev_info(dev
, "DN CONNECT: device %s @ %x (%s) wants to %s\n",
552 wusb_dn_connect_prev_dev_addr(dnc
),
553 beacon_behaviour
[wusb_dn_connect_beacon_behavior(dnc
)],
554 wusb_dn_connect_new_connection(dnc
) ? "connect" : "reconnect");
555 /* ACK the connect */
556 wusbhc_devconnect_ack(wusbhc
, dnc
, pr_cdid
);
560 * Handle a DN_Disconnect notification (WUSB1.0[7.6.1])
562 * Device is going down -- do the disconnect.
564 * @wusbhc shall be referenced and unlocked
566 static void wusbhc_handle_dn_disconnect(struct wusbhc
*wusbhc
, u8 srcaddr
)
568 struct device
*dev
= wusbhc
->dev
;
569 struct wusb_dev
*wusb_dev
;
571 mutex_lock(&wusbhc
->mutex
);
572 wusb_dev
= wusbhc_find_dev_by_addr(wusbhc
, srcaddr
);
573 if (wusb_dev
== NULL
) {
574 dev_dbg(dev
, "ignoring DN DISCONNECT from unconnected device %02x\n",
577 dev_info(dev
, "DN DISCONNECT: device 0x%02x going down\n",
579 __wusbhc_dev_disconnect(wusbhc
, wusb_port_by_idx(wusbhc
,
580 wusb_dev
->port_idx
));
582 mutex_unlock(&wusbhc
->mutex
);
586 * Handle a Device Notification coming a host
588 * The Device Notification comes from a host (HWA, DWA or WHCI)
589 * wrapped in a set of headers. Somebody else has peeled off those
590 * headers for us and we just get one Device Notifications.
592 * Invalid DNs (e.g., too short) are discarded.
594 * @wusbhc shall be referenced
597 * - implement priorities as in WUSB1.0[Table 7-55]?
599 void wusbhc_handle_dn(struct wusbhc
*wusbhc
, u8 srcaddr
,
600 struct wusb_dn_hdr
*dn_hdr
, size_t size
)
602 struct device
*dev
= wusbhc
->dev
;
604 if (size
< sizeof(struct wusb_dn_hdr
)) {
605 dev_err(dev
, "DN data shorter than DN header (%d < %d)\n",
606 (int)size
, (int)sizeof(struct wusb_dn_hdr
));
609 switch (dn_hdr
->bType
) {
610 case WUSB_DN_CONNECT
:
611 wusbhc_handle_dn_connect(wusbhc
, dn_hdr
, size
);
614 wusbhc_handle_dn_alive(wusbhc
, srcaddr
);
616 case WUSB_DN_DISCONNECT
:
617 wusbhc_handle_dn_disconnect(wusbhc
, srcaddr
);
619 case WUSB_DN_MASAVAILCHANGED
:
622 /* FIXME: handle these DNs. */
625 /* The hardware handles these. */
628 dev_warn(dev
, "unknown DN %u (%d octets) from %u\n",
629 dn_hdr
->bType
, (int)size
, srcaddr
);
632 EXPORT_SYMBOL_GPL(wusbhc_handle_dn
);
635 * Disconnect a WUSB device from a the cluster
638 * @port Fake port where the device is (wusbhc index, not USB port number).
640 * In Wireless USB, a disconnect is basically telling the device he is
641 * being disconnected and forgetting about him.
643 * We send the device a Device Disconnect IE (WUSB1.0[7.5.11]) for 100
644 * ms and then keep going.
646 * We don't do much in case of error; we always pretend we disabled
647 * the port and disconnected the device. If physically the request
648 * didn't get there (many things can fail in the way there), the stack
649 * will reject the device's communication attempts.
651 * @wusbhc should be refcounted and locked
653 void __wusbhc_dev_disable(struct wusbhc
*wusbhc
, u8 port_idx
)
656 struct device
*dev
= wusbhc
->dev
;
657 struct wusb_dev
*wusb_dev
;
658 struct wuie_disconnect
*ie
;
660 wusb_dev
= wusb_port_by_idx(wusbhc
, port_idx
)->wusb_dev
;
661 if (wusb_dev
== NULL
) {
662 /* reset no device? ignore */
663 dev_dbg(dev
, "DISCONNECT: no device at port %u, ignoring\n",
667 __wusbhc_dev_disconnect(wusbhc
, wusb_port_by_idx(wusbhc
, port_idx
));
669 ie
= kzalloc(sizeof(*ie
), GFP_KERNEL
);
672 ie
->hdr
.bLength
= sizeof(*ie
);
673 ie
->hdr
.bIEIdentifier
= WUIE_ID_DEVICE_DISCONNECT
;
674 ie
->bDeviceAddress
= wusb_dev
->addr
;
675 result
= wusbhc_mmcie_set(wusbhc
, 0, 0, &ie
->hdr
);
677 dev_err(dev
, "DISCONNECT: can't set MMC: %d\n", result
);
679 /* At least 6 MMCs, assuming at least 1 MMC per zone. */
681 wusbhc_mmcie_rm(wusbhc
, &ie
->hdr
);
687 * Walk over the BOS descriptor, verify and grok it
689 * @usb_dev: referenced
690 * @wusb_dev: referenced and unlocked
692 * The BOS descriptor is defined at WUSB1.0[7.4.1], and it defines a
693 * "flexible" way to wrap all kinds of descriptors inside an standard
694 * descriptor (wonder why they didn't use normal descriptors,
695 * btw). Not like they lack code.
697 * At the end we go to look for the WUSB Device Capabilities
698 * (WUSB1.0[7.4.1.1]) that is wrapped in a device capability descriptor
699 * that is part of the BOS descriptor set. That tells us what does the
700 * device support (dual role, beacon type, UWB PHY rates).
702 static int wusb_dev_bos_grok(struct usb_device
*usb_dev
,
703 struct wusb_dev
*wusb_dev
,
704 struct usb_bos_descriptor
*bos
, size_t desc_size
)
707 struct device
*dev
= &usb_dev
->dev
;
710 /* Walk over BOS capabilities, verify them */
711 itr
= (void *)bos
+ sizeof(*bos
);
712 top
= itr
+ desc_size
- sizeof(*bos
);
714 struct usb_dev_cap_header
*cap_hdr
= itr
;
717 if (top
- itr
< sizeof(*cap_hdr
)) {
718 dev_err(dev
, "Device BUG? premature end of BOS header "
719 "data [offset 0x%02x]: only %zu bytes left\n",
720 (int)(itr
- (void *)bos
), top
- itr
);
724 cap_size
= cap_hdr
->bLength
;
725 cap_type
= cap_hdr
->bDevCapabilityType
;
728 if (cap_size
> top
- itr
) {
729 dev_err(dev
, "Device BUG? premature end of BOS data "
730 "[offset 0x%02x cap %02x %zu bytes]: "
731 "only %zu bytes left\n",
732 (int)(itr
- (void *)bos
),
733 cap_type
, cap_size
, top
- itr
);
738 case USB_CAP_TYPE_WIRELESS_USB
:
739 if (cap_size
!= sizeof(*wusb_dev
->wusb_cap_descr
))
740 dev_err(dev
, "Device BUG? WUSB Capability "
741 "descriptor is %zu bytes vs %zu "
742 "needed\n", cap_size
,
743 sizeof(*wusb_dev
->wusb_cap_descr
));
745 wusb_dev
->wusb_cap_descr
= itr
;
748 dev_err(dev
, "BUG? Unknown BOS capability 0x%02x "
749 "(%zu bytes) at offset 0x%02x\n", cap_type
,
750 cap_size
, (int)(itr
- (void *)bos
));
760 * Add information from the BOS descriptors to the device
762 * @usb_dev: referenced
763 * @wusb_dev: referenced and unlocked
765 * So what we do is we alloc a space for the BOS descriptor of 64
766 * bytes; read the first four bytes which include the wTotalLength
767 * field (WUSB1.0[T7-26]) and if it fits in those 64 bytes, read the
768 * whole thing. If not we realloc to that size.
770 * Then we call the groking function, that will fill up
771 * wusb_dev->wusb_cap_descr, which is what we'll need later on.
773 static int wusb_dev_bos_add(struct usb_device
*usb_dev
,
774 struct wusb_dev
*wusb_dev
)
777 struct device
*dev
= &usb_dev
->dev
;
778 struct usb_bos_descriptor
*bos
;
779 size_t alloc_size
= 32, desc_size
= 4;
781 bos
= kmalloc(alloc_size
, GFP_KERNEL
);
784 result
= usb_get_descriptor(usb_dev
, USB_DT_BOS
, 0, bos
, desc_size
);
786 dev_err(dev
, "Can't get BOS descriptor or too short: %zd\n",
788 goto error_get_descriptor
;
790 desc_size
= le16_to_cpu(bos
->wTotalLength
);
791 if (desc_size
>= alloc_size
) {
793 alloc_size
= desc_size
;
794 bos
= kmalloc(alloc_size
, GFP_KERNEL
);
798 result
= usb_get_descriptor(usb_dev
, USB_DT_BOS
, 0, bos
, desc_size
);
799 if (result
< 0 || result
!= desc_size
) {
800 dev_err(dev
, "Can't get BOS descriptor or too short (need "
801 "%zu bytes): %zd\n", desc_size
, result
);
802 goto error_get_descriptor
;
804 if (result
< sizeof(*bos
)
805 || le16_to_cpu(bos
->wTotalLength
) != desc_size
) {
806 dev_err(dev
, "Can't get BOS descriptor or too short (need "
807 "%zu bytes): %zd\n", desc_size
, result
);
808 goto error_get_descriptor
;
811 result
= wusb_dev_bos_grok(usb_dev
, wusb_dev
, bos
, result
);
818 error_get_descriptor
:
820 wusb_dev
->wusb_cap_descr
= NULL
;
824 static void wusb_dev_bos_rm(struct wusb_dev
*wusb_dev
)
826 kfree(wusb_dev
->bos
);
827 wusb_dev
->wusb_cap_descr
= NULL
;
831 * USB stack's device addition Notifier Callback
833 * Called from drivers/usb/core/hub.c when a new device is added; we
834 * use this hook to perform certain WUSB specific setup work on the
835 * new device. As well, it is the first time we can connect the
836 * wusb_dev and the usb_dev. So we note it down in wusb_dev and take a
837 * reference that we'll drop.
839 * First we need to determine if the device is a WUSB device (else we
840 * ignore it). For that we use the speed setting (USB_SPEED_WIRELESS)
841 * [FIXME: maybe we'd need something more definitive]. If so, we track
842 * it's usb_busd and from there, the WUSB HC.
844 * Because all WUSB HCs are contained in a 'struct wusbhc', voila, we
845 * get the wusbhc for the device.
847 * We have a reference on @usb_dev (as we are called at the end of its
850 * NOTE: @usb_dev locked
852 static void wusb_dev_add_ncb(struct usb_device
*usb_dev
)
855 struct wusb_dev
*wusb_dev
;
856 struct wusbhc
*wusbhc
;
857 struct device
*dev
= &usb_dev
->dev
;
860 if (usb_dev
->wusb
== 0 || usb_dev
->devnum
== 1)
861 return; /* skip non wusb and wusb RHs */
863 usb_set_device_state(usb_dev
, USB_STATE_UNAUTHENTICATED
);
865 wusbhc
= wusbhc_get_by_usb_dev(usb_dev
);
868 mutex_lock(&wusbhc
->mutex
);
869 wusb_dev
= __wusb_dev_get_by_usb_dev(wusbhc
, usb_dev
);
870 port_idx
= wusb_port_no_to_idx(usb_dev
->portnum
);
871 mutex_unlock(&wusbhc
->mutex
);
872 if (wusb_dev
== NULL
)
874 wusb_dev
->usb_dev
= usb_get_dev(usb_dev
);
875 usb_dev
->wusb_dev
= wusb_dev_get(wusb_dev
);
876 result
= wusb_dev_sec_add(wusbhc
, usb_dev
, wusb_dev
);
878 dev_err(dev
, "Cannot enable security: %d\n", result
);
881 /* Now query the device for it's BOS and attach it to wusb_dev */
882 result
= wusb_dev_bos_add(usb_dev
, wusb_dev
);
884 dev_err(dev
, "Cannot get BOS descriptors: %d\n", result
);
887 result
= wusb_dev_sysfs_add(wusbhc
, usb_dev
, wusb_dev
);
889 goto error_add_sysfs
;
891 wusb_dev_put(wusb_dev
);
896 wusb_dev_sysfs_rm(wusb_dev
);
898 wusb_dev_bos_rm(wusb_dev
);
900 wusb_dev_sec_rm(wusb_dev
);
902 mutex_lock(&wusbhc
->mutex
);
903 __wusbhc_dev_disconnect(wusbhc
, wusb_port_by_idx(wusbhc
, port_idx
));
904 mutex_unlock(&wusbhc
->mutex
);
909 * Undo all the steps done at connection by the notifier callback
911 * NOTE: @usb_dev locked
913 static void wusb_dev_rm_ncb(struct usb_device
*usb_dev
)
915 struct wusb_dev
*wusb_dev
= usb_dev
->wusb_dev
;
917 if (usb_dev
->wusb
== 0 || usb_dev
->devnum
== 1)
918 return; /* skip non wusb and wusb RHs */
920 wusb_dev_sysfs_rm(wusb_dev
);
921 wusb_dev_bos_rm(wusb_dev
);
922 wusb_dev_sec_rm(wusb_dev
);
923 wusb_dev
->usb_dev
= NULL
;
924 usb_dev
->wusb_dev
= NULL
;
925 wusb_dev_put(wusb_dev
);
926 usb_put_dev(usb_dev
);
930 * Handle notifications from the USB stack (notifier call back)
932 * This is called when the USB stack does a
933 * usb_{bus,device}_{add,remove}() so we can do WUSB specific
934 * handling. It is called with [for the case of
935 * USB_DEVICE_{ADD,REMOVE} with the usb_dev locked.
937 int wusb_usb_ncb(struct notifier_block
*nb
, unsigned long val
,
940 int result
= NOTIFY_OK
;
944 wusb_dev_add_ncb(priv
);
946 case USB_DEVICE_REMOVE
:
947 wusb_dev_rm_ncb(priv
);
950 /* ignore (for now) */
961 * Return a referenced wusb_dev given a @wusbhc and @usb_dev
963 struct wusb_dev
*__wusb_dev_get_by_usb_dev(struct wusbhc
*wusbhc
,
964 struct usb_device
*usb_dev
)
966 struct wusb_dev
*wusb_dev
;
969 port_idx
= wusb_port_no_to_idx(usb_dev
->portnum
);
970 BUG_ON(port_idx
> wusbhc
->ports_max
);
971 wusb_dev
= wusb_port_by_idx(wusbhc
, port_idx
)->wusb_dev
;
972 if (wusb_dev
!= NULL
) /* ops, device is gone */
973 wusb_dev_get(wusb_dev
);
976 EXPORT_SYMBOL_GPL(__wusb_dev_get_by_usb_dev
);
978 void wusb_dev_destroy(struct kref
*_wusb_dev
)
980 struct wusb_dev
*wusb_dev
= container_of(_wusb_dev
, struct wusb_dev
, refcnt
);
982 list_del_init(&wusb_dev
->cack_node
);
983 wusb_dev_free(wusb_dev
);
985 EXPORT_SYMBOL_GPL(wusb_dev_destroy
);
988 * Create all the device connect handling infrastructure
990 * This is basically the device info array, Connect Acknowledgement
991 * (cack) lists, keep-alive timers (and delayed work thread).
993 int wusbhc_devconnect_create(struct wusbhc
*wusbhc
)
995 wusbhc
->keep_alive_ie
.hdr
.bIEIdentifier
= WUIE_ID_KEEP_ALIVE
;
996 wusbhc
->keep_alive_ie
.hdr
.bLength
= sizeof(wusbhc
->keep_alive_ie
.hdr
);
997 INIT_DELAYED_WORK(&wusbhc
->keep_alive_timer
, wusbhc_keep_alive_run
);
999 wusbhc
->cack_ie
.hdr
.bIEIdentifier
= WUIE_ID_CONNECTACK
;
1000 wusbhc
->cack_ie
.hdr
.bLength
= sizeof(wusbhc
->cack_ie
.hdr
);
1001 INIT_LIST_HEAD(&wusbhc
->cack_list
);
1007 * Release all resources taken by the devconnect stuff
1009 void wusbhc_devconnect_destroy(struct wusbhc
*wusbhc
)
1015 * wusbhc_devconnect_start - start accepting device connections
1016 * @wusbhc: the WUSB HC
1018 * Sets the Host Info IE to accept all new connections.
1020 * FIXME: This also enables the keep alives but this is not necessary
1021 * until there are connected and authenticated devices.
1023 int wusbhc_devconnect_start(struct wusbhc
*wusbhc
)
1025 struct device
*dev
= wusbhc
->dev
;
1026 struct wuie_host_info
*hi
;
1029 hi
= kzalloc(sizeof(*hi
), GFP_KERNEL
);
1033 hi
->hdr
.bLength
= sizeof(*hi
);
1034 hi
->hdr
.bIEIdentifier
= WUIE_ID_HOST_INFO
;
1035 hi
->attributes
= cpu_to_le16((wusbhc
->rsv
->stream
<< 3) | WUIE_HI_CAP_ALL
);
1036 hi
->CHID
= wusbhc
->chid
;
1037 result
= wusbhc_mmcie_set(wusbhc
, 0, 0, &hi
->hdr
);
1039 dev_err(dev
, "Cannot add Host Info MMCIE: %d\n", result
);
1040 goto error_mmcie_set
;
1042 wusbhc
->wuie_host_info
= hi
;
1044 queue_delayed_work(wusbd
, &wusbhc
->keep_alive_timer
,
1045 msecs_to_jiffies(wusbhc
->trust_timeout
/ 2));
1055 * wusbhc_devconnect_stop - stop managing connected devices
1056 * @wusbhc: the WUSB HC
1058 * Disconnects any devices still connected, stops the keep alives and
1059 * removes the Host Info IE.
1061 void wusbhc_devconnect_stop(struct wusbhc
*wusbhc
)
1065 mutex_lock(&wusbhc
->mutex
);
1066 for (i
= 0; i
< wusbhc
->ports_max
; i
++) {
1067 if (wusbhc
->port
[i
].wusb_dev
)
1068 __wusbhc_dev_disconnect(wusbhc
, &wusbhc
->port
[i
]);
1070 mutex_unlock(&wusbhc
->mutex
);
1072 cancel_delayed_work_sync(&wusbhc
->keep_alive_timer
);
1073 wusbhc_mmcie_rm(wusbhc
, &wusbhc
->wuie_host_info
->hdr
);
1074 kfree(wusbhc
->wuie_host_info
);
1075 wusbhc
->wuie_host_info
= NULL
;
1079 * wusb_set_dev_addr - set the WUSB device address used by the host
1080 * @wusbhc: the WUSB HC the device is connect to
1081 * @wusb_dev: the WUSB device
1082 * @addr: new device address
1084 int wusb_set_dev_addr(struct wusbhc
*wusbhc
, struct wusb_dev
*wusb_dev
, u8 addr
)
1088 wusb_dev
->addr
= addr
;
1089 result
= wusbhc
->dev_info_set(wusbhc
, wusb_dev
);
1091 dev_err(wusbhc
->dev
, "device %d: failed to set device "
1092 "address\n", wusb_dev
->port_idx
);
1094 dev_info(wusbhc
->dev
, "device %d: %s addr %u\n",
1096 (addr
& WUSB_DEV_ADDR_UNAUTH
) ? "unauth" : "auth",