1 // SPDX-License-Identifier: GPL-2.0
3 * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
4 * Device Connect handling
6 * Copyright (C) 2006 Intel Corporation
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * FIXME: this file needs to be broken up, it's grown too big
28 * WUSB1.0[7.1, 7.5.1, ]
30 * WUSB device connection is kind of messy. Some background:
32 * When a device wants to connect it scans the UWB radio channels
33 * looking for a WUSB Channel; a WUSB channel is defined by MMCs
34 * (Micro Managed Commands or something like that) [see
35 * Design-overview for more on this] .
37 * So, device scans the radio, finds MMCs and thus a host and checks
38 * when the next DNTS is. It sends a Device Notification Connect
39 * (DN_Connect); the host picks it up (through nep.c and notif.c, ends
40 * up in wusb_devconnect_ack(), which creates a wusb_dev structure in
41 * wusbhc->port[port_number].wusb_dev), assigns an unauth address
42 * to the device (this means from 0x80 to 0xfe) and sends, in the MMC
43 * a Connect Ack Information Element (ConnAck IE).
45 * So now the device now has a WUSB address. From now on, we use
46 * that to talk to it in the RPipes.
50 * - We use the the as device address the port number where it is
51 * connected (port 0 doesn't exist). For unauth, it is 128 + that.
55 * This file contains the logic for doing that--entry points:
57 * wusb_devconnect_ack() Ack a device until _acked() called.
58 * Called by notif.c:wusb_handle_dn_connect()
59 * when a DN_Connect is received.
61 * wusb_devconnect_acked() Ack done, release resources.
63 * wusb_handle_dn_alive() Called by notif.c:wusb_handle_dn()
64 * for processing a DN_Alive pong from a device.
66 * wusb_handle_dn_disconnect()Called by notif.c:wusb_handle_dn() to
67 * process a disconenct request from a
70 * __wusb_dev_disable() Called by rh.c:wusbhc_rh_clear_port_feat() when
73 * wusb_devconnect_create() Called when creating the host by
74 * lc.c:wusbhc_create().
76 * wusb_devconnect_destroy() Cleanup called removing the host. Called
77 * by lc.c:wusbhc_destroy().
79 * Each Wireless USB host maintains a list of DN_Connect requests
80 * (actually we maintain a list of pending Connect Acks, the
83 * LIFE CYCLE OF port->wusb_dev
85 * Before the @wusbhc structure put()s the reference it owns for
86 * port->wusb_dev [and clean the wusb_dev pointer], it needs to
87 * lock @wusbhc->mutex.
90 #include <linux/jiffies.h>
91 #include <linux/ctype.h>
92 #include <linux/slab.h>
93 #include <linux/workqueue.h>
94 #include <linux/export.h>
97 static void wusbhc_devconnect_acked_work(struct work_struct
*work
);
99 static void wusb_dev_free(struct wusb_dev
*wusb_dev
)
104 static struct wusb_dev
*wusb_dev_alloc(struct wusbhc
*wusbhc
)
106 struct wusb_dev
*wusb_dev
;
108 wusb_dev
= kzalloc(sizeof(*wusb_dev
), GFP_KERNEL
);
109 if (wusb_dev
== NULL
)
112 wusb_dev
->wusbhc
= wusbhc
;
114 INIT_WORK(&wusb_dev
->devconnect_acked_work
, wusbhc_devconnect_acked_work
);
118 wusb_dev_free(wusb_dev
);
124 * Using the Connect-Ack list, fill out the @wusbhc Connect-Ack WUSB IE
125 * properly so that it can be added to the MMC.
127 * We just get the @wusbhc->ca_list and fill out the first four ones or
128 * less (per-spec WUSB1.0[7.5, before T7-38). If the ConnectAck WUSB
129 * IE is not allocated, we alloc it.
131 * @wusbhc->mutex must be taken
133 static void wusbhc_fill_cack_ie(struct wusbhc
*wusbhc
)
136 struct wusb_dev
*dev_itr
;
137 struct wuie_connect_ack
*cack_ie
;
139 cack_ie
= &wusbhc
->cack_ie
;
141 list_for_each_entry(dev_itr
, &wusbhc
->cack_list
, cack_node
) {
142 cack_ie
->blk
[cnt
].CDID
= dev_itr
->cdid
;
143 cack_ie
->blk
[cnt
].bDeviceAddress
= dev_itr
->addr
;
144 if (++cnt
>= WUIE_ELT_MAX
)
147 cack_ie
->hdr
.bLength
= sizeof(cack_ie
->hdr
)
148 + cnt
* sizeof(cack_ie
->blk
[0]);
152 * Register a new device that wants to connect
154 * A new device wants to connect, so we add it to the Connect-Ack
155 * list. We give it an address in the unauthorized range (bit 8 set);
156 * user space will have to drive authorization further on.
158 * @dev_addr: address to use for the device (which is also the port
161 * @wusbhc->mutex must be taken
163 static struct wusb_dev
*wusbhc_cack_add(struct wusbhc
*wusbhc
,
164 struct wusb_dn_connect
*dnc
,
165 const char *pr_cdid
, u8 port_idx
)
167 struct device
*dev
= wusbhc
->dev
;
168 struct wusb_dev
*wusb_dev
;
169 int new_connection
= wusb_dn_connect_new_connection(dnc
);
173 /* Is it registered already? */
174 list_for_each_entry(wusb_dev
, &wusbhc
->cack_list
, cack_node
)
175 if (!memcmp(&wusb_dev
->cdid
, &dnc
->CDID
,
176 sizeof(wusb_dev
->cdid
)))
178 /* We don't have it, create an entry, register it */
179 wusb_dev
= wusb_dev_alloc(wusbhc
);
180 if (wusb_dev
== NULL
)
182 wusb_dev_init(wusb_dev
);
183 wusb_dev
->cdid
= dnc
->CDID
;
184 wusb_dev
->port_idx
= port_idx
;
187 * Devices are always available within the cluster reservation
188 * and since the hardware will take the intersection of the
189 * per-device availability and the cluster reservation, the
190 * per-device availability can simply be set to always
193 bitmap_fill(wusb_dev
->availability
.bm
, UWB_NUM_MAS
);
195 /* FIXME: handle reconnects instead of assuming connects are
197 if (1 && new_connection
== 0)
199 if (new_connection
) {
200 dev_addr
= (port_idx
+ 2) | WUSB_DEV_ADDR_UNAUTH
;
202 dev_info(dev
, "Connecting new WUSB device to address %u, "
203 "port %u\n", dev_addr
, port_idx
);
205 result
= wusb_set_dev_addr(wusbhc
, wusb_dev
, dev_addr
);
209 wusb_dev
->entry_ts
= jiffies
;
210 list_add_tail(&wusb_dev
->cack_node
, &wusbhc
->cack_list
);
211 wusbhc
->cack_count
++;
212 wusbhc_fill_cack_ie(wusbhc
);
218 * Remove a Connect-Ack context entry from the HCs view
220 * @wusbhc->mutex must be taken
222 static void wusbhc_cack_rm(struct wusbhc
*wusbhc
, struct wusb_dev
*wusb_dev
)
224 list_del_init(&wusb_dev
->cack_node
);
225 wusbhc
->cack_count
--;
226 wusbhc_fill_cack_ie(wusbhc
);
230 * @wusbhc->mutex must be taken */
232 void wusbhc_devconnect_acked(struct wusbhc
*wusbhc
, struct wusb_dev
*wusb_dev
)
234 wusbhc_cack_rm(wusbhc
, wusb_dev
);
235 if (wusbhc
->cack_count
)
236 wusbhc_mmcie_set(wusbhc
, 0, 0, &wusbhc
->cack_ie
.hdr
);
238 wusbhc_mmcie_rm(wusbhc
, &wusbhc
->cack_ie
.hdr
);
241 static void wusbhc_devconnect_acked_work(struct work_struct
*work
)
243 struct wusb_dev
*wusb_dev
= container_of(work
, struct wusb_dev
,
244 devconnect_acked_work
);
245 struct wusbhc
*wusbhc
= wusb_dev
->wusbhc
;
247 mutex_lock(&wusbhc
->mutex
);
248 wusbhc_devconnect_acked(wusbhc
, wusb_dev
);
249 mutex_unlock(&wusbhc
->mutex
);
251 wusb_dev_put(wusb_dev
);
255 * Ack a device for connection
259 * @pr_cdid: Printable CDID...hex Use @dnc->cdid for the real deal.
261 * So we get the connect ack IE (may have been allocated already),
262 * find an empty connect block, an empty virtual port, create an
263 * address with it (see below), make it an unauth addr [bit 7 set] and
266 * Addresses: because WUSB hosts have no downstream hubs, we can do a
267 * 1:1 mapping between 'port number' and device
268 * address. This simplifies many things, as during this
269 * initial connect phase the USB stack has no knowledge of
270 * the device and hasn't assigned an address yet--we know
271 * USB's choose_address() will use the same heuristics we
272 * use here, so we can assume which address will be assigned.
274 * USB stack always assigns address 1 to the root hub, so
275 * to the port number we add 2 (thus virtual port #0 is
278 * @wusbhc shall be referenced
281 void wusbhc_devconnect_ack(struct wusbhc
*wusbhc
, struct wusb_dn_connect
*dnc
,
285 struct device
*dev
= wusbhc
->dev
;
286 struct wusb_dev
*wusb_dev
;
287 struct wusb_port
*port
;
290 mutex_lock(&wusbhc
->mutex
);
292 /* Check we are not handling it already */
293 for (idx
= 0; idx
< wusbhc
->ports_max
; idx
++) {
294 port
= wusb_port_by_idx(wusbhc
, idx
);
296 && memcmp(&dnc
->CDID
, &port
->wusb_dev
->cdid
, sizeof(dnc
->CDID
)) == 0)
299 /* Look up those fake ports we have for a free one */
300 for (idx
= 0; idx
< wusbhc
->ports_max
; idx
++) {
301 port
= wusb_port_by_idx(wusbhc
, idx
);
302 if ((port
->status
& USB_PORT_STAT_POWER
)
303 && !(port
->status
& USB_PORT_STAT_CONNECTION
))
306 if (idx
>= wusbhc
->ports_max
) {
307 dev_err(dev
, "Host controller can't connect more devices "
308 "(%u already connected); device %s rejected\n",
309 wusbhc
->ports_max
, pr_cdid
);
310 /* NOTE: we could send a WUIE_Disconnect here, but we haven't
311 * event acked, so the device will eventually timeout the
312 * connection, right? */
316 /* Make sure we are using no crypto on that "virtual port" */
317 wusbhc
->set_ptk(wusbhc
, idx
, 0, NULL
, 0);
319 /* Grab a filled in Connect-Ack context, fill out the
320 * Connect-Ack Wireless USB IE, set the MMC */
321 wusb_dev
= wusbhc_cack_add(wusbhc
, dnc
, pr_cdid
, idx
);
322 if (wusb_dev
== NULL
)
324 result
= wusbhc_mmcie_set(wusbhc
, 0, 0, &wusbhc
->cack_ie
.hdr
);
327 /* Give the device at least 2ms (WUSB1.0[7.5.1p3]), let's do
328 * three for a good measure */
330 port
->wusb_dev
= wusb_dev
;
331 port
->status
|= USB_PORT_STAT_CONNECTION
;
332 port
->change
|= USB_PORT_STAT_C_CONNECTION
;
333 /* Now the port status changed to connected; hub_wq will
334 * pick the change up and try to reset the port to bring it to
335 * the enabled state--so this process returns up to the stack
336 * and it calls back into wusbhc_rh_port_reset().
339 mutex_unlock(&wusbhc
->mutex
);
345 * Disconnect a Wireless USB device from its fake port
347 * Marks the port as disconnected so that hub_wq can pick up the change
348 * and drops our knowledge about the device.
350 * Assumes there is a device connected
352 * @port_index: zero based port number
354 * NOTE: @wusbhc->mutex is locked
356 * WARNING: From here it is not very safe to access anything hanging off
359 static void __wusbhc_dev_disconnect(struct wusbhc
*wusbhc
,
360 struct wusb_port
*port
)
362 struct wusb_dev
*wusb_dev
= port
->wusb_dev
;
364 port
->status
&= ~(USB_PORT_STAT_CONNECTION
| USB_PORT_STAT_ENABLE
365 | USB_PORT_STAT_SUSPEND
| USB_PORT_STAT_RESET
366 | USB_PORT_STAT_LOW_SPEED
| USB_PORT_STAT_HIGH_SPEED
);
367 port
->change
|= USB_PORT_STAT_C_CONNECTION
| USB_PORT_STAT_C_ENABLE
;
369 dev_dbg(wusbhc
->dev
, "disconnecting device from port %d\n", wusb_dev
->port_idx
);
370 if (!list_empty(&wusb_dev
->cack_node
))
371 list_del_init(&wusb_dev
->cack_node
);
372 /* For the one in cack_add() */
373 wusb_dev_put(wusb_dev
);
375 port
->wusb_dev
= NULL
;
377 /* After a device disconnects, change the GTK (see [WUSB]
378 * section 6.2.11.2). */
380 wusbhc_gtk_rekey(wusbhc
);
382 /* The Wireless USB part has forgotten about the device already; now
383 * hub_wq's timer will pick up the disconnection and remove the USB
384 * device from the system
389 * Refresh the list of keep alives to emit in the MMC
391 * We only publish the first four devices that have a coming timeout
392 * condition. Then when we are done processing those, we go for the
393 * next ones. We ignore the ones that have timed out already (they'll
396 * This might cause the first devices to timeout the last devices in
397 * the port array...FIXME: come up with a better algorithm?
399 * Note we can't do much about MMC's ops errors; we hope next refresh
400 * will kind of handle it.
402 * NOTE: @wusbhc->mutex is locked
404 static void __wusbhc_keep_alive(struct wusbhc
*wusbhc
)
406 struct device
*dev
= wusbhc
->dev
;
408 struct wusb_dev
*wusb_dev
;
409 struct wusb_port
*wusb_port
;
410 struct wuie_keep_alive
*ie
= &wusbhc
->keep_alive_ie
;
411 unsigned keep_alives
, old_keep_alives
;
413 old_keep_alives
= ie
->hdr
.bLength
- sizeof(ie
->hdr
);
416 keep_alives
< WUIE_ELT_MAX
&& cnt
< wusbhc
->ports_max
;
418 unsigned tt
= msecs_to_jiffies(wusbhc
->trust_timeout
);
420 wusb_port
= wusb_port_by_idx(wusbhc
, cnt
);
421 wusb_dev
= wusb_port
->wusb_dev
;
423 if (wusb_dev
== NULL
)
425 if (wusb_dev
->usb_dev
== NULL
)
428 if (time_after(jiffies
, wusb_dev
->entry_ts
+ tt
)) {
429 dev_err(dev
, "KEEPALIVE: device %u timed out\n",
431 __wusbhc_dev_disconnect(wusbhc
, wusb_port
);
432 } else if (time_after(jiffies
, wusb_dev
->entry_ts
+ tt
/3)) {
433 /* Approaching timeout cut off, need to refresh */
434 ie
->bDeviceAddress
[keep_alives
++] = wusb_dev
->addr
;
437 if (keep_alives
& 0x1) /* pad to even number ([WUSB] section 7.5.9) */
438 ie
->bDeviceAddress
[keep_alives
++] = 0x7f;
439 ie
->hdr
.bLength
= sizeof(ie
->hdr
) +
440 keep_alives
*sizeof(ie
->bDeviceAddress
[0]);
442 wusbhc_mmcie_set(wusbhc
, 10, 5, &ie
->hdr
);
443 else if (old_keep_alives
!= 0)
444 wusbhc_mmcie_rm(wusbhc
, &ie
->hdr
);
448 * Do a run through all devices checking for timeouts
450 static void wusbhc_keep_alive_run(struct work_struct
*ws
)
452 struct delayed_work
*dw
= to_delayed_work(ws
);
453 struct wusbhc
*wusbhc
= container_of(dw
, struct wusbhc
, keep_alive_timer
);
455 mutex_lock(&wusbhc
->mutex
);
456 __wusbhc_keep_alive(wusbhc
);
457 mutex_unlock(&wusbhc
->mutex
);
459 queue_delayed_work(wusbd
, &wusbhc
->keep_alive_timer
,
460 msecs_to_jiffies(wusbhc
->trust_timeout
/ 2));
464 * Find the wusb_dev from its device address.
466 * The device can be found directly from the address (see
467 * wusb_cack_add() for where the device address is set to port_idx
468 * +2), except when the address is zero.
470 static struct wusb_dev
*wusbhc_find_dev_by_addr(struct wusbhc
*wusbhc
, u8 addr
)
474 if (addr
== 0xff) /* unconnected */
478 int port
= (addr
& ~0x80) - 2;
479 if (port
< 0 || port
>= wusbhc
->ports_max
)
481 return wusb_port_by_idx(wusbhc
, port
)->wusb_dev
;
484 /* Look for the device with address 0. */
485 for (p
= 0; p
< wusbhc
->ports_max
; p
++) {
486 struct wusb_dev
*wusb_dev
= wusb_port_by_idx(wusbhc
, p
)->wusb_dev
;
487 if (wusb_dev
&& wusb_dev
->addr
== addr
)
494 * Handle a DN_Alive notification (WUSB1.0[7.6.1])
496 * This just updates the device activity timestamp and then refreshes
499 * @wusbhc shall be referenced and unlocked
501 static void wusbhc_handle_dn_alive(struct wusbhc
*wusbhc
, u8 srcaddr
)
503 struct wusb_dev
*wusb_dev
;
505 mutex_lock(&wusbhc
->mutex
);
506 wusb_dev
= wusbhc_find_dev_by_addr(wusbhc
, srcaddr
);
507 if (wusb_dev
== NULL
) {
508 dev_dbg(wusbhc
->dev
, "ignoring DN_Alive from unconnected device %02x\n",
511 wusb_dev
->entry_ts
= jiffies
;
512 __wusbhc_keep_alive(wusbhc
);
514 mutex_unlock(&wusbhc
->mutex
);
518 * Handle a DN_Connect notification (WUSB1.0[7.6.1])
522 * @size: Size of the buffer where the notification resides; if the
523 * notification data suggests there should be more data than
524 * available, an error will be signaled and the whole buffer
527 * @wusbhc->mutex shall be held
529 static void wusbhc_handle_dn_connect(struct wusbhc
*wusbhc
,
530 struct wusb_dn_hdr
*dn_hdr
,
533 struct device
*dev
= wusbhc
->dev
;
534 struct wusb_dn_connect
*dnc
;
535 char pr_cdid
[WUSB_CKHDID_STRSIZE
];
536 static const char *beacon_behaviour
[] = {
543 if (size
< sizeof(*dnc
)) {
544 dev_err(dev
, "DN CONNECT: short notification (%zu < %zu)\n",
549 dnc
= container_of(dn_hdr
, struct wusb_dn_connect
, hdr
);
550 ckhdid_printf(pr_cdid
, sizeof(pr_cdid
), &dnc
->CDID
);
551 dev_info(dev
, "DN CONNECT: device %s @ %x (%s) wants to %s\n",
553 wusb_dn_connect_prev_dev_addr(dnc
),
554 beacon_behaviour
[wusb_dn_connect_beacon_behavior(dnc
)],
555 wusb_dn_connect_new_connection(dnc
) ? "connect" : "reconnect");
556 /* ACK the connect */
557 wusbhc_devconnect_ack(wusbhc
, dnc
, pr_cdid
);
561 * Handle a DN_Disconnect notification (WUSB1.0[7.6.1])
563 * Device is going down -- do the disconnect.
565 * @wusbhc shall be referenced and unlocked
567 static void wusbhc_handle_dn_disconnect(struct wusbhc
*wusbhc
, u8 srcaddr
)
569 struct device
*dev
= wusbhc
->dev
;
570 struct wusb_dev
*wusb_dev
;
572 mutex_lock(&wusbhc
->mutex
);
573 wusb_dev
= wusbhc_find_dev_by_addr(wusbhc
, srcaddr
);
574 if (wusb_dev
== NULL
) {
575 dev_dbg(dev
, "ignoring DN DISCONNECT from unconnected device %02x\n",
578 dev_info(dev
, "DN DISCONNECT: device 0x%02x going down\n",
580 __wusbhc_dev_disconnect(wusbhc
, wusb_port_by_idx(wusbhc
,
581 wusb_dev
->port_idx
));
583 mutex_unlock(&wusbhc
->mutex
);
587 * Handle a Device Notification coming a host
589 * The Device Notification comes from a host (HWA, DWA or WHCI)
590 * wrapped in a set of headers. Somebody else has peeled off those
591 * headers for us and we just get one Device Notifications.
593 * Invalid DNs (e.g., too short) are discarded.
595 * @wusbhc shall be referenced
598 * - implement priorities as in WUSB1.0[Table 7-55]?
600 void wusbhc_handle_dn(struct wusbhc
*wusbhc
, u8 srcaddr
,
601 struct wusb_dn_hdr
*dn_hdr
, size_t size
)
603 struct device
*dev
= wusbhc
->dev
;
605 if (size
< sizeof(struct wusb_dn_hdr
)) {
606 dev_err(dev
, "DN data shorter than DN header (%d < %d)\n",
607 (int)size
, (int)sizeof(struct wusb_dn_hdr
));
610 switch (dn_hdr
->bType
) {
611 case WUSB_DN_CONNECT
:
612 wusbhc_handle_dn_connect(wusbhc
, dn_hdr
, size
);
615 wusbhc_handle_dn_alive(wusbhc
, srcaddr
);
617 case WUSB_DN_DISCONNECT
:
618 wusbhc_handle_dn_disconnect(wusbhc
, srcaddr
);
620 case WUSB_DN_MASAVAILCHANGED
:
623 /* FIXME: handle these DNs. */
626 /* The hardware handles these. */
629 dev_warn(dev
, "unknown DN %u (%d octets) from %u\n",
630 dn_hdr
->bType
, (int)size
, srcaddr
);
633 EXPORT_SYMBOL_GPL(wusbhc_handle_dn
);
636 * Disconnect a WUSB device from a the cluster
639 * @port Fake port where the device is (wusbhc index, not USB port number).
641 * In Wireless USB, a disconnect is basically telling the device he is
642 * being disconnected and forgetting about him.
644 * We send the device a Device Disconnect IE (WUSB1.0[7.5.11]) for 100
645 * ms and then keep going.
647 * We don't do much in case of error; we always pretend we disabled
648 * the port and disconnected the device. If physically the request
649 * didn't get there (many things can fail in the way there), the stack
650 * will reject the device's communication attempts.
652 * @wusbhc should be refcounted and locked
654 void __wusbhc_dev_disable(struct wusbhc
*wusbhc
, u8 port_idx
)
657 struct device
*dev
= wusbhc
->dev
;
658 struct wusb_dev
*wusb_dev
;
659 struct wuie_disconnect
*ie
;
661 wusb_dev
= wusb_port_by_idx(wusbhc
, port_idx
)->wusb_dev
;
662 if (wusb_dev
== NULL
) {
663 /* reset no device? ignore */
664 dev_dbg(dev
, "DISCONNECT: no device at port %u, ignoring\n",
668 __wusbhc_dev_disconnect(wusbhc
, wusb_port_by_idx(wusbhc
, port_idx
));
670 ie
= kzalloc(sizeof(*ie
), GFP_KERNEL
);
673 ie
->hdr
.bLength
= sizeof(*ie
);
674 ie
->hdr
.bIEIdentifier
= WUIE_ID_DEVICE_DISCONNECT
;
675 ie
->bDeviceAddress
= wusb_dev
->addr
;
676 result
= wusbhc_mmcie_set(wusbhc
, 0, 0, &ie
->hdr
);
678 dev_err(dev
, "DISCONNECT: can't set MMC: %d\n", result
);
680 /* At least 6 MMCs, assuming at least 1 MMC per zone. */
682 wusbhc_mmcie_rm(wusbhc
, &ie
->hdr
);
688 * Walk over the BOS descriptor, verify and grok it
690 * @usb_dev: referenced
691 * @wusb_dev: referenced and unlocked
693 * The BOS descriptor is defined at WUSB1.0[7.4.1], and it defines a
694 * "flexible" way to wrap all kinds of descriptors inside an standard
695 * descriptor (wonder why they didn't use normal descriptors,
696 * btw). Not like they lack code.
698 * At the end we go to look for the WUSB Device Capabilities
699 * (WUSB1.0[7.4.1.1]) that is wrapped in a device capability descriptor
700 * that is part of the BOS descriptor set. That tells us what does the
701 * device support (dual role, beacon type, UWB PHY rates).
703 static int wusb_dev_bos_grok(struct usb_device
*usb_dev
,
704 struct wusb_dev
*wusb_dev
,
705 struct usb_bos_descriptor
*bos
, size_t desc_size
)
708 struct device
*dev
= &usb_dev
->dev
;
711 /* Walk over BOS capabilities, verify them */
712 itr
= (void *)bos
+ sizeof(*bos
);
713 top
= itr
+ desc_size
- sizeof(*bos
);
715 struct usb_dev_cap_header
*cap_hdr
= itr
;
718 if (top
- itr
< sizeof(*cap_hdr
)) {
719 dev_err(dev
, "Device BUG? premature end of BOS header "
720 "data [offset 0x%02x]: only %zu bytes left\n",
721 (int)(itr
- (void *)bos
), top
- itr
);
725 cap_size
= cap_hdr
->bLength
;
726 cap_type
= cap_hdr
->bDevCapabilityType
;
729 if (cap_size
> top
- itr
) {
730 dev_err(dev
, "Device BUG? premature end of BOS data "
731 "[offset 0x%02x cap %02x %zu bytes]: "
732 "only %zu bytes left\n",
733 (int)(itr
- (void *)bos
),
734 cap_type
, cap_size
, top
- itr
);
739 case USB_CAP_TYPE_WIRELESS_USB
:
740 if (cap_size
!= sizeof(*wusb_dev
->wusb_cap_descr
))
741 dev_err(dev
, "Device BUG? WUSB Capability "
742 "descriptor is %zu bytes vs %zu "
743 "needed\n", cap_size
,
744 sizeof(*wusb_dev
->wusb_cap_descr
));
746 wusb_dev
->wusb_cap_descr
= itr
;
749 dev_err(dev
, "BUG? Unknown BOS capability 0x%02x "
750 "(%zu bytes) at offset 0x%02x\n", cap_type
,
751 cap_size
, (int)(itr
- (void *)bos
));
761 * Add information from the BOS descriptors to the device
763 * @usb_dev: referenced
764 * @wusb_dev: referenced and unlocked
766 * So what we do is we alloc a space for the BOS descriptor of 64
767 * bytes; read the first four bytes which include the wTotalLength
768 * field (WUSB1.0[T7-26]) and if it fits in those 64 bytes, read the
769 * whole thing. If not we realloc to that size.
771 * Then we call the groking function, that will fill up
772 * wusb_dev->wusb_cap_descr, which is what we'll need later on.
774 static int wusb_dev_bos_add(struct usb_device
*usb_dev
,
775 struct wusb_dev
*wusb_dev
)
778 struct device
*dev
= &usb_dev
->dev
;
779 struct usb_bos_descriptor
*bos
;
780 size_t alloc_size
= 32, desc_size
= 4;
782 bos
= kmalloc(alloc_size
, GFP_KERNEL
);
785 result
= usb_get_descriptor(usb_dev
, USB_DT_BOS
, 0, bos
, desc_size
);
787 dev_err(dev
, "Can't get BOS descriptor or too short: %zd\n",
789 goto error_get_descriptor
;
791 desc_size
= le16_to_cpu(bos
->wTotalLength
);
792 if (desc_size
>= alloc_size
) {
794 alloc_size
= desc_size
;
795 bos
= kmalloc(alloc_size
, GFP_KERNEL
);
799 result
= usb_get_descriptor(usb_dev
, USB_DT_BOS
, 0, bos
, desc_size
);
800 if (result
< 0 || result
!= desc_size
) {
801 dev_err(dev
, "Can't get BOS descriptor or too short (need "
802 "%zu bytes): %zd\n", desc_size
, result
);
803 goto error_get_descriptor
;
805 if (result
< sizeof(*bos
)
806 || le16_to_cpu(bos
->wTotalLength
) != desc_size
) {
807 dev_err(dev
, "Can't get BOS descriptor or too short (need "
808 "%zu bytes): %zd\n", desc_size
, result
);
809 goto error_get_descriptor
;
812 result
= wusb_dev_bos_grok(usb_dev
, wusb_dev
, bos
, result
);
819 error_get_descriptor
:
821 wusb_dev
->wusb_cap_descr
= NULL
;
825 static void wusb_dev_bos_rm(struct wusb_dev
*wusb_dev
)
827 kfree(wusb_dev
->bos
);
828 wusb_dev
->wusb_cap_descr
= NULL
;
832 * USB stack's device addition Notifier Callback
834 * Called from drivers/usb/core/hub.c when a new device is added; we
835 * use this hook to perform certain WUSB specific setup work on the
836 * new device. As well, it is the first time we can connect the
837 * wusb_dev and the usb_dev. So we note it down in wusb_dev and take a
838 * reference that we'll drop.
840 * First we need to determine if the device is a WUSB device (else we
841 * ignore it). For that we use the speed setting (USB_SPEED_WIRELESS)
842 * [FIXME: maybe we'd need something more definitive]. If so, we track
843 * it's usb_busd and from there, the WUSB HC.
845 * Because all WUSB HCs are contained in a 'struct wusbhc', voila, we
846 * get the wusbhc for the device.
848 * We have a reference on @usb_dev (as we are called at the end of its
851 * NOTE: @usb_dev locked
853 static void wusb_dev_add_ncb(struct usb_device
*usb_dev
)
856 struct wusb_dev
*wusb_dev
;
857 struct wusbhc
*wusbhc
;
858 struct device
*dev
= &usb_dev
->dev
;
861 if (usb_dev
->wusb
== 0 || usb_dev
->devnum
== 1)
862 return; /* skip non wusb and wusb RHs */
864 usb_set_device_state(usb_dev
, USB_STATE_UNAUTHENTICATED
);
866 wusbhc
= wusbhc_get_by_usb_dev(usb_dev
);
869 mutex_lock(&wusbhc
->mutex
);
870 wusb_dev
= __wusb_dev_get_by_usb_dev(wusbhc
, usb_dev
);
871 port_idx
= wusb_port_no_to_idx(usb_dev
->portnum
);
872 mutex_unlock(&wusbhc
->mutex
);
873 if (wusb_dev
== NULL
)
875 wusb_dev
->usb_dev
= usb_get_dev(usb_dev
);
876 usb_dev
->wusb_dev
= wusb_dev_get(wusb_dev
);
877 result
= wusb_dev_sec_add(wusbhc
, usb_dev
, wusb_dev
);
879 dev_err(dev
, "Cannot enable security: %d\n", result
);
882 /* Now query the device for it's BOS and attach it to wusb_dev */
883 result
= wusb_dev_bos_add(usb_dev
, wusb_dev
);
885 dev_err(dev
, "Cannot get BOS descriptors: %d\n", result
);
888 result
= wusb_dev_sysfs_add(wusbhc
, usb_dev
, wusb_dev
);
890 goto error_add_sysfs
;
892 wusb_dev_put(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",