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 * wusbhc_devconnect_auth() Called by rh.c:wusbhc_rh_port_reset() when
61 * doing the device connect sequence.
63 * wusb_devconnect_acked() Ack done, release resources.
65 * wusb_handle_dn_alive() Called by notif.c:wusb_handle_dn()
66 * for processing a DN_Alive pong from a device.
68 * wusb_handle_dn_disconnect()Called by notif.c:wusb_handle_dn() to
69 * process a disconenct request from a
72 * wusb_dev_reset() Called by rh.c:wusbhc_rh_port_reset() when
75 * __wusb_dev_disable() Called by rh.c:wusbhc_rh_clear_port_feat() when
78 * wusb_devconnect_create() Called when creating the host by
79 * lc.c:wusbhc_create().
81 * wusb_devconnect_destroy() Cleanup called removing the host. Called
82 * by lc.c:wusbhc_destroy().
84 * Each Wireless USB host maintains a list of DN_Connect requests
85 * (actually we maintain a list of pending Connect Acks, the
88 * LIFE CYCLE OF port->wusb_dev
90 * Before the @wusbhc structure put()s the reference it owns for
91 * port->wusb_dev [and clean the wusb_dev pointer], it needs to
92 * lock @wusbhc->mutex.
95 #include <linux/jiffies.h>
96 #include <linux/ctype.h>
97 #include <linux/workqueue.h>
102 #include <linux/uwb/debug.h>
104 static void wusbhc_devconnect_acked_work(struct work_struct
*work
);
106 static void wusb_dev_free(struct wusb_dev
*wusb_dev
)
109 kfree(wusb_dev
->set_gtk_req
);
110 usb_free_urb(wusb_dev
->set_gtk_urb
);
115 static struct wusb_dev
*wusb_dev_alloc(struct wusbhc
*wusbhc
)
117 struct wusb_dev
*wusb_dev
;
119 struct usb_ctrlrequest
*req
;
121 wusb_dev
= kzalloc(sizeof(*wusb_dev
), GFP_KERNEL
);
122 if (wusb_dev
== NULL
)
125 wusb_dev
->wusbhc
= wusbhc
;
127 INIT_WORK(&wusb_dev
->devconnect_acked_work
, wusbhc_devconnect_acked_work
);
129 urb
= usb_alloc_urb(0, GFP_KERNEL
);
133 req
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
137 req
->bRequestType
= USB_DIR_OUT
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
;
138 req
->bRequest
= USB_REQ_SET_DESCRIPTOR
;
139 req
->wValue
= cpu_to_le16(USB_DT_KEY
<< 8 | wusbhc
->gtk_index
);
141 req
->wLength
= cpu_to_le16(wusbhc
->gtk
.descr
.bLength
);
143 wusb_dev
->set_gtk_urb
= urb
;
144 wusb_dev
->set_gtk_req
= req
;
148 wusb_dev_free(wusb_dev
);
154 * Using the Connect-Ack list, fill out the @wusbhc Connect-Ack WUSB IE
155 * properly so that it can be added to the MMC.
157 * We just get the @wusbhc->ca_list and fill out the first four ones or
158 * less (per-spec WUSB1.0[7.5, before T7-38). If the ConnectAck WUSB
159 * IE is not allocated, we alloc it.
161 * @wusbhc->mutex must be taken
163 static void wusbhc_fill_cack_ie(struct wusbhc
*wusbhc
)
166 struct wusb_dev
*dev_itr
;
167 struct wuie_connect_ack
*cack_ie
;
169 cack_ie
= &wusbhc
->cack_ie
;
171 list_for_each_entry(dev_itr
, &wusbhc
->cack_list
, cack_node
) {
172 cack_ie
->blk
[cnt
].CDID
= dev_itr
->cdid
;
173 cack_ie
->blk
[cnt
].bDeviceAddress
= dev_itr
->addr
;
174 if (++cnt
>= WUIE_ELT_MAX
)
177 cack_ie
->hdr
.bLength
= sizeof(cack_ie
->hdr
)
178 + cnt
* sizeof(cack_ie
->blk
[0]);
182 * Register a new device that wants to connect
184 * A new device wants to connect, so we add it to the Connect-Ack
185 * list. We give it an address in the unauthorized range (bit 8 set);
186 * user space will have to drive authorization further on.
188 * @dev_addr: address to use for the device (which is also the port
191 * @wusbhc->mutex must be taken
193 static struct wusb_dev
*wusbhc_cack_add(struct wusbhc
*wusbhc
,
194 struct wusb_dn_connect
*dnc
,
195 const char *pr_cdid
, u8 port_idx
)
197 struct device
*dev
= wusbhc
->dev
;
198 struct wusb_dev
*wusb_dev
;
199 int new_connection
= wusb_dn_connect_new_connection(dnc
);
203 d_fnstart(3, dev
, "(wusbhc %p port_idx %d)\n", wusbhc
, port_idx
);
204 /* Is it registered already? */
205 list_for_each_entry(wusb_dev
, &wusbhc
->cack_list
, cack_node
)
206 if (!memcmp(&wusb_dev
->cdid
, &dnc
->CDID
,
207 sizeof(wusb_dev
->cdid
)))
209 /* We don't have it, create an entry, register it */
210 wusb_dev
= wusb_dev_alloc(wusbhc
);
211 if (wusb_dev
== NULL
) {
212 if (printk_ratelimit())
213 dev_err(dev
, "DN CONNECT: no memory to process %s's %s "
214 "request\n", pr_cdid
,
215 new_connection
? "connect" : "reconnect");
218 wusb_dev_init(wusb_dev
);
219 wusb_dev
->cdid
= dnc
->CDID
;
220 wusb_dev
->port_idx
= port_idx
;
223 * Devices are always available within the cluster reservation
224 * and since the hardware will take the intersection of the
225 * per-device availability and the cluster reservation, the
226 * per-device availability can simply be set to always
229 bitmap_fill(wusb_dev
->availability
.bm
, UWB_NUM_MAS
);
231 /* FIXME: handle reconnects instead of assuming connects are
233 if (1 && new_connection
== 0)
235 if (new_connection
) {
236 dev_addr
= (port_idx
+ 2) | WUSB_DEV_ADDR_UNAUTH
;
238 dev_info(dev
, "Connecting new WUSB device to address %u, "
239 "port %u\n", dev_addr
, port_idx
);
241 result
= wusb_set_dev_addr(wusbhc
, wusb_dev
, dev_addr
);
245 wusb_dev
->entry_ts
= jiffies
;
246 list_add_tail(&wusb_dev
->cack_node
, &wusbhc
->cack_list
);
247 wusbhc
->cack_count
++;
248 wusbhc_fill_cack_ie(wusbhc
);
249 d_fnend(3, dev
, "(wusbhc %p port_idx %d)\n", wusbhc
, port_idx
);
254 * Remove a Connect-Ack context entry from the HCs view
256 * @wusbhc->mutex must be taken
258 static void wusbhc_cack_rm(struct wusbhc
*wusbhc
, struct wusb_dev
*wusb_dev
)
260 struct device
*dev
= wusbhc
->dev
;
261 d_fnstart(3, dev
, "(wusbhc %p wusb_dev %p)\n", wusbhc
, wusb_dev
);
262 list_del_init(&wusb_dev
->cack_node
);
263 wusbhc
->cack_count
--;
264 wusbhc_fill_cack_ie(wusbhc
);
265 d_fnend(3, dev
, "(wusbhc %p wusb_dev %p) = void\n", wusbhc
, wusb_dev
);
269 * @wusbhc->mutex must be taken */
271 void wusbhc_devconnect_acked(struct wusbhc
*wusbhc
, struct wusb_dev
*wusb_dev
)
273 struct device
*dev
= wusbhc
->dev
;
274 d_fnstart(3, dev
, "(wusbhc %p wusb_dev %p)\n", wusbhc
, wusb_dev
);
275 wusbhc_cack_rm(wusbhc
, wusb_dev
);
276 if (wusbhc
->cack_count
)
277 wusbhc_mmcie_set(wusbhc
, 0, 0, &wusbhc
->cack_ie
.hdr
);
279 wusbhc_mmcie_rm(wusbhc
, &wusbhc
->cack_ie
.hdr
);
280 d_fnend(3, dev
, "(wusbhc %p wusb_dev %p) = void\n", wusbhc
, wusb_dev
);
283 static void wusbhc_devconnect_acked_work(struct work_struct
*work
)
285 struct wusb_dev
*wusb_dev
= container_of(work
, struct wusb_dev
,
286 devconnect_acked_work
);
287 struct wusbhc
*wusbhc
= wusb_dev
->wusbhc
;
289 mutex_lock(&wusbhc
->mutex
);
290 wusbhc_devconnect_acked(wusbhc
, wusb_dev
);
291 mutex_unlock(&wusbhc
->mutex
);
295 * Ack a device for connection
299 * @pr_cdid: Printable CDID...hex Use @dnc->cdid for the real deal.
301 * So we get the connect ack IE (may have been allocated already),
302 * find an empty connect block, an empty virtual port, create an
303 * address with it (see below), make it an unauth addr [bit 7 set] and
306 * Addresses: because WUSB hosts have no downstream hubs, we can do a
307 * 1:1 mapping between 'port number' and device
308 * address. This simplifies many things, as during this
309 * initial connect phase the USB stack has no knoledge of
310 * the device and hasn't assigned an address yet--we know
311 * USB's choose_address() will use the same euristics we
312 * use here, so we can assume which address will be assigned.
314 * USB stack always assigns address 1 to the root hub, so
315 * to the port number we add 2 (thus virtual port #0 is
318 * @wusbhc shall be referenced
321 void wusbhc_devconnect_ack(struct wusbhc
*wusbhc
, struct wusb_dn_connect
*dnc
,
325 struct device
*dev
= wusbhc
->dev
;
326 struct wusb_dev
*wusb_dev
;
327 struct wusb_port
*port
;
328 unsigned idx
, devnum
;
330 d_fnstart(3, dev
, "(%p, %p, %s)\n", wusbhc
, dnc
, pr_cdid
);
331 mutex_lock(&wusbhc
->mutex
);
333 /* Check we are not handling it already */
334 for (idx
= 0; idx
< wusbhc
->ports_max
; idx
++) {
335 port
= wusb_port_by_idx(wusbhc
, idx
);
337 && !memcmp(&dnc
->CDID
, &port
->wusb_dev
->cdid
,
338 sizeof(dnc
->CDID
))) {
339 if (printk_ratelimit())
340 dev_err(dev
, "Already handling dev %s "
341 " (it might be slow)\n", pr_cdid
);
345 /* Look up those fake ports we have for a free one */
346 for (idx
= 0; idx
< wusbhc
->ports_max
; idx
++) {
347 port
= wusb_port_by_idx(wusbhc
, idx
);
348 if ((port
->status
& USB_PORT_STAT_POWER
)
349 && !(port
->status
& USB_PORT_STAT_CONNECTION
))
352 if (idx
>= wusbhc
->ports_max
) {
353 dev_err(dev
, "Host controller can't connect more devices "
354 "(%u already connected); device %s rejected\n",
355 wusbhc
->ports_max
, pr_cdid
);
356 /* NOTE: we could send a WUIE_Disconnect here, but we haven't
357 * event acked, so the device will eventually timeout the
358 * connection, right? */
364 /* Make sure we are using no crypto on that "virtual port" */
365 wusbhc
->set_ptk(wusbhc
, idx
, 0, NULL
, 0);
367 /* Grab a filled in Connect-Ack context, fill out the
368 * Connect-Ack Wireless USB IE, set the MMC */
369 wusb_dev
= wusbhc_cack_add(wusbhc
, dnc
, pr_cdid
, idx
);
370 if (wusb_dev
== NULL
)
372 result
= wusbhc_mmcie_set(wusbhc
, 0, 0, &wusbhc
->cack_ie
.hdr
);
375 /* Give the device at least 2ms (WUSB1.0[7.5.1p3]), let's do
376 * three for a good measure */
378 port
->wusb_dev
= wusb_dev
;
379 port
->status
|= USB_PORT_STAT_CONNECTION
;
380 port
->change
|= USB_PORT_STAT_C_CONNECTION
;
381 port
->reset_count
= 0;
382 /* Now the port status changed to connected; khubd will
383 * pick the change up and try to reset the port to bring it to
384 * the enabled state--so this process returns up to the stack
385 * and it calls back into wusbhc_rh_port_reset() who will call
389 mutex_unlock(&wusbhc
->mutex
);
390 d_fnend(3, dev
, "(%p, %p, %s) = void\n", wusbhc
, dnc
, pr_cdid
);
396 * Disconnect a Wireless USB device from its fake port
398 * Marks the port as disconnected so that khubd can pick up the change
399 * and drops our knowledge about the device.
401 * Assumes there is a device connected
403 * @port_index: zero based port number
405 * NOTE: @wusbhc->mutex is locked
407 * WARNING: From here it is not very safe to access anything hanging off
410 static void __wusbhc_dev_disconnect(struct wusbhc
*wusbhc
,
411 struct wusb_port
*port
)
413 struct device
*dev
= wusbhc
->dev
;
414 struct wusb_dev
*wusb_dev
= port
->wusb_dev
;
416 d_fnstart(3, dev
, "(wusbhc %p, port %p)\n", wusbhc
, port
);
417 port
->status
&= ~(USB_PORT_STAT_CONNECTION
| USB_PORT_STAT_ENABLE
418 | USB_PORT_STAT_SUSPEND
| USB_PORT_STAT_RESET
419 | USB_PORT_STAT_LOW_SPEED
| USB_PORT_STAT_HIGH_SPEED
);
420 port
->change
|= USB_PORT_STAT_C_CONNECTION
| USB_PORT_STAT_C_ENABLE
;
422 if (!list_empty(&wusb_dev
->cack_node
))
423 list_del_init(&wusb_dev
->cack_node
);
424 /* For the one in cack_add() */
425 wusb_dev_put(wusb_dev
);
427 port
->wusb_dev
= NULL
;
428 /* don't reset the reset_count to zero or wusbhc_rh_port_reset will get
429 * confused! We only reset to zero when we connect a new device.
432 /* After a device disconnects, change the GTK (see [WUSB]
433 * section 6.2.11.2). */
434 wusbhc_gtk_rekey(wusbhc
);
436 d_fnend(3, dev
, "(wusbhc %p, port %p) = void\n", wusbhc
, port
);
437 /* The Wireless USB part has forgotten about the device already; now
438 * khubd's timer will pick up the disconnection and remove the USB
439 * device from the system
444 * Authenticate a device into the WUSB Cluster
446 * Called from the Root Hub code (rh.c:wusbhc_rh_port_reset()) when
447 * asking for a reset on a port that is not enabled (ie: first connect
450 * Performs the 4way handshake to allow the device to comunicate w/ the
451 * WUSB Cluster securely; once done, issue a request to the device for
452 * it to change to address 0.
454 * This mimics the reset step of Wired USB that once resetting a
455 * device, leaves the port in enabled state and the dev with the
456 * default address (0).
460 * @port_idx: port where the change happened--This is the index into
461 * the wusbhc port array, not the USB port number.
463 int wusbhc_devconnect_auth(struct wusbhc
*wusbhc
, u8 port_idx
)
465 struct device
*dev
= wusbhc
->dev
;
466 struct wusb_port
*port
= wusb_port_by_idx(wusbhc
, port_idx
);
468 d_fnstart(3, dev
, "(%p, %u)\n", wusbhc
, port_idx
);
469 port
->status
&= ~USB_PORT_STAT_RESET
;
470 port
->status
|= USB_PORT_STAT_ENABLE
;
471 port
->change
|= USB_PORT_STAT_C_RESET
| USB_PORT_STAT_C_ENABLE
;
472 d_fnend(3, dev
, "(%p, %u) = 0\n", wusbhc
, port_idx
);
477 * Refresh the list of keep alives to emit in the MMC
479 * Some devices don't respond to keep alives unless they've been
480 * authenticated, so skip unauthenticated devices.
482 * We only publish the first four devices that have a coming timeout
483 * condition. Then when we are done processing those, we go for the
484 * next ones. We ignore the ones that have timed out already (they'll
487 * This might cause the first devices to timeout the last devices in
488 * the port array...FIXME: come up with a better algorithm?
490 * Note we can't do much about MMC's ops errors; we hope next refresh
491 * will kind of handle it.
493 * NOTE: @wusbhc->mutex is locked
495 static void __wusbhc_keep_alive(struct wusbhc
*wusbhc
)
498 struct device
*dev
= wusbhc
->dev
;
500 struct wusb_dev
*wusb_dev
;
501 struct wusb_port
*wusb_port
;
502 struct wuie_keep_alive
*ie
= &wusbhc
->keep_alive_ie
;
503 unsigned keep_alives
, old_keep_alives
;
505 d_fnstart(5, dev
, "(wusbhc %p)\n", wusbhc
);
506 old_keep_alives
= ie
->hdr
.bLength
- sizeof(ie
->hdr
);
509 keep_alives
<= WUIE_ELT_MAX
&& cnt
< wusbhc
->ports_max
;
511 unsigned tt
= msecs_to_jiffies(wusbhc
->trust_timeout
);
513 wusb_port
= wusb_port_by_idx(wusbhc
, cnt
);
514 wusb_dev
= wusb_port
->wusb_dev
;
516 if (wusb_dev
== NULL
)
518 if (wusb_dev
->usb_dev
== NULL
|| !wusb_dev
->usb_dev
->authenticated
)
521 if (time_after(jiffies
, wusb_dev
->entry_ts
+ tt
)) {
522 dev_err(dev
, "KEEPALIVE: device %u timed out\n",
524 __wusbhc_dev_disconnect(wusbhc
, wusb_port
);
525 } else if (time_after(jiffies
, wusb_dev
->entry_ts
+ tt
/2)) {
526 /* Approaching timeout cut out, need to refresh */
527 ie
->bDeviceAddress
[keep_alives
++] = wusb_dev
->addr
;
530 if (keep_alives
& 0x1) /* pad to even number ([WUSB] section 7.5.9) */
531 ie
->bDeviceAddress
[keep_alives
++] = 0x7f;
532 ie
->hdr
.bLength
= sizeof(ie
->hdr
) +
533 keep_alives
*sizeof(ie
->bDeviceAddress
[0]);
534 if (keep_alives
> 0) {
535 result
= wusbhc_mmcie_set(wusbhc
, 10, 5, &ie
->hdr
);
536 if (result
< 0 && printk_ratelimit())
537 dev_err(dev
, "KEEPALIVE: can't set MMC: %d\n", result
);
538 } else if (old_keep_alives
!= 0)
539 wusbhc_mmcie_rm(wusbhc
, &ie
->hdr
);
540 d_fnend(5, dev
, "(wusbhc %p) = void\n", wusbhc
);
544 * Do a run through all devices checking for timeouts
546 static void wusbhc_keep_alive_run(struct work_struct
*ws
)
548 struct delayed_work
*dw
=
549 container_of(ws
, struct delayed_work
, work
);
550 struct wusbhc
*wusbhc
=
551 container_of(dw
, struct wusbhc
, keep_alive_timer
);
553 d_fnstart(5, wusbhc
->dev
, "(wusbhc %p)\n", wusbhc
);
554 if (wusbhc
->active
) {
555 mutex_lock(&wusbhc
->mutex
);
556 __wusbhc_keep_alive(wusbhc
);
557 mutex_unlock(&wusbhc
->mutex
);
558 queue_delayed_work(wusbd
, &wusbhc
->keep_alive_timer
,
559 (wusbhc
->trust_timeout
* CONFIG_HZ
)/1000/2);
561 d_fnend(5, wusbhc
->dev
, "(wusbhc %p) = void\n", wusbhc
);
566 * Find the wusb_dev from its device address.
568 * The device can be found directly from the address (see
569 * wusb_cack_add() for where the device address is set to port_idx
570 * +2), except when the address is zero.
572 static struct wusb_dev
*wusbhc_find_dev_by_addr(struct wusbhc
*wusbhc
, u8 addr
)
576 if (addr
== 0xff) /* unconnected */
580 int port
= (addr
& ~0x80) - 2;
581 if (port
< 0 || port
>= wusbhc
->ports_max
)
583 return wusb_port_by_idx(wusbhc
, port
)->wusb_dev
;
586 /* Look for the device with address 0. */
587 for (p
= 0; p
< wusbhc
->ports_max
; p
++) {
588 struct wusb_dev
*wusb_dev
= wusb_port_by_idx(wusbhc
, p
)->wusb_dev
;
589 if (wusb_dev
&& wusb_dev
->addr
== addr
)
596 * Handle a DN_Alive notification (WUSB1.0[7.6.1])
598 * This just updates the device activity timestamp and then refreshes
601 * @wusbhc shall be referenced and unlocked
603 static void wusbhc_handle_dn_alive(struct wusbhc
*wusbhc
, struct wusb_dev
*wusb_dev
)
605 struct device
*dev
= wusbhc
->dev
;
607 d_printf(2, dev
, "DN ALIVE: device 0x%02x pong\n", wusb_dev
->addr
);
609 mutex_lock(&wusbhc
->mutex
);
610 wusb_dev
->entry_ts
= jiffies
;
611 __wusbhc_keep_alive(wusbhc
);
612 mutex_unlock(&wusbhc
->mutex
);
616 * Handle a DN_Connect notification (WUSB1.0[7.6.1])
620 * @size: Size of the buffer where the notification resides; if the
621 * notification data suggests there should be more data than
622 * available, an error will be signaled and the whole buffer
625 * @wusbhc->mutex shall be held
627 static void wusbhc_handle_dn_connect(struct wusbhc
*wusbhc
,
628 struct wusb_dn_hdr
*dn_hdr
,
631 struct device
*dev
= wusbhc
->dev
;
632 struct wusb_dn_connect
*dnc
;
633 char pr_cdid
[WUSB_CKHDID_STRSIZE
];
634 static const char *beacon_behaviour
[] = {
641 d_fnstart(3, dev
, "(%p, %p, %zu)\n", wusbhc
, dn_hdr
, size
);
642 if (size
< sizeof(*dnc
)) {
643 dev_err(dev
, "DN CONNECT: short notification (%zu < %zu)\n",
648 dnc
= container_of(dn_hdr
, struct wusb_dn_connect
, hdr
);
649 ckhdid_printf(pr_cdid
, sizeof(pr_cdid
), &dnc
->CDID
);
650 dev_info(dev
, "DN CONNECT: device %s @ %x (%s) wants to %s\n",
652 wusb_dn_connect_prev_dev_addr(dnc
),
653 beacon_behaviour
[wusb_dn_connect_beacon_behavior(dnc
)],
654 wusb_dn_connect_new_connection(dnc
) ? "connect" : "reconnect");
655 /* ACK the connect */
656 wusbhc_devconnect_ack(wusbhc
, dnc
, pr_cdid
);
658 d_fnend(3, dev
, "(%p, %p, %zu) = void\n",
659 wusbhc
, dn_hdr
, size
);
664 * Handle a DN_Disconnect notification (WUSB1.0[7.6.1])
666 * Device is going down -- do the disconnect.
668 * @wusbhc shall be referenced and unlocked
670 static void wusbhc_handle_dn_disconnect(struct wusbhc
*wusbhc
, struct wusb_dev
*wusb_dev
)
672 struct device
*dev
= wusbhc
->dev
;
674 dev_info(dev
, "DN DISCONNECT: device 0x%02x going down\n", wusb_dev
->addr
);
676 mutex_lock(&wusbhc
->mutex
);
677 __wusbhc_dev_disconnect(wusbhc
, wusb_port_by_idx(wusbhc
, wusb_dev
->port_idx
));
678 mutex_unlock(&wusbhc
->mutex
);
682 * Reset a WUSB device on a HWA
685 * @port_idx Index of the port where the device is
687 * In Wireless USB, a reset is more or less equivalent to a full
688 * disconnect; so we just do a full disconnect and send the device a
689 * Device Reset IE (WUSB1.0[7.5.11]) giving it a few millisecs (6 MMCs).
691 * @wusbhc should be refcounted and unlocked
693 int wusbhc_dev_reset(struct wusbhc
*wusbhc
, u8 port_idx
)
696 struct device
*dev
= wusbhc
->dev
;
697 struct wusb_dev
*wusb_dev
;
698 struct wuie_reset
*ie
;
700 d_fnstart(3, dev
, "(%p, %u)\n", wusbhc
, port_idx
);
701 mutex_lock(&wusbhc
->mutex
);
703 wusb_dev
= wusb_port_by_idx(wusbhc
, port_idx
)->wusb_dev
;
704 if (wusb_dev
== NULL
) {
705 /* reset no device? ignore */
706 dev_dbg(dev
, "RESET: no device at port %u, ignoring\n",
711 ie
= kzalloc(sizeof(*ie
), GFP_KERNEL
);
714 ie
->hdr
.bLength
= sizeof(ie
->hdr
) + sizeof(ie
->CDID
);
715 ie
->hdr
.bIEIdentifier
= WUIE_ID_RESET_DEVICE
;
716 ie
->CDID
= wusb_dev
->cdid
;
717 result
= wusbhc_mmcie_set(wusbhc
, 0xff, 6, &ie
->hdr
);
719 dev_err(dev
, "RESET: cant's set MMC: %d\n", result
);
722 __wusbhc_dev_disconnect(wusbhc
, wusb_port_by_idx(wusbhc
, port_idx
));
724 /* 120ms, hopefully 6 MMCs (FIXME) */
726 wusbhc_mmcie_rm(wusbhc
, &ie
->hdr
);
730 mutex_unlock(&wusbhc
->mutex
);
731 d_fnend(3, dev
, "(%p, %u) = %d\n", wusbhc
, port_idx
, result
);
736 * Handle a Device Notification coming a host
738 * The Device Notification comes from a host (HWA, DWA or WHCI)
739 * wrapped in a set of headers. Somebody else has peeled off those
740 * headers for us and we just get one Device Notifications.
742 * Invalid DNs (e.g., too short) are discarded.
744 * @wusbhc shall be referenced
747 * - implement priorities as in WUSB1.0[Table 7-55]?
749 void wusbhc_handle_dn(struct wusbhc
*wusbhc
, u8 srcaddr
,
750 struct wusb_dn_hdr
*dn_hdr
, size_t size
)
752 struct device
*dev
= wusbhc
->dev
;
753 struct wusb_dev
*wusb_dev
;
755 d_fnstart(3, dev
, "(%p, %p)\n", wusbhc
, dn_hdr
);
757 if (size
< sizeof(struct wusb_dn_hdr
)) {
758 dev_err(dev
, "DN data shorter than DN header (%d < %d)\n",
759 (int)size
, (int)sizeof(struct wusb_dn_hdr
));
763 wusb_dev
= wusbhc_find_dev_by_addr(wusbhc
, srcaddr
);
764 if (wusb_dev
== NULL
&& dn_hdr
->bType
!= WUSB_DN_CONNECT
) {
765 dev_dbg(dev
, "ignoring DN %d from unconnected device %02x\n",
766 dn_hdr
->bType
, srcaddr
);
770 switch (dn_hdr
->bType
) {
771 case WUSB_DN_CONNECT
:
772 wusbhc_handle_dn_connect(wusbhc
, dn_hdr
, size
);
775 wusbhc_handle_dn_alive(wusbhc
, wusb_dev
);
777 case WUSB_DN_DISCONNECT
:
778 wusbhc_handle_dn_disconnect(wusbhc
, wusb_dev
);
780 case WUSB_DN_MASAVAILCHANGED
:
783 /* FIXME: handle these DNs. */
786 /* The hardware handles these. */
789 dev_warn(dev
, "unknown DN %u (%d octets) from %u\n",
790 dn_hdr
->bType
, (int)size
, srcaddr
);
793 d_fnend(3, dev
, "(%p, %p) = void\n", wusbhc
, dn_hdr
);
796 EXPORT_SYMBOL_GPL(wusbhc_handle_dn
);
799 * Disconnect a WUSB device from a the cluster
802 * @port Fake port where the device is (wusbhc index, not USB port number).
804 * In Wireless USB, a disconnect is basically telling the device he is
805 * being disconnected and forgetting about him.
807 * We send the device a Device Disconnect IE (WUSB1.0[7.5.11]) for 100
808 * ms and then keep going.
810 * We don't do much in case of error; we always pretend we disabled
811 * the port and disconnected the device. If physically the request
812 * didn't get there (many things can fail in the way there), the stack
813 * will reject the device's communication attempts.
815 * @wusbhc should be refcounted and locked
817 void __wusbhc_dev_disable(struct wusbhc
*wusbhc
, u8 port_idx
)
820 struct device
*dev
= wusbhc
->dev
;
821 struct wusb_dev
*wusb_dev
;
822 struct wuie_disconnect
*ie
;
824 d_fnstart(3, dev
, "(%p, %u)\n", wusbhc
, port_idx
);
826 wusb_dev
= wusb_port_by_idx(wusbhc
, port_idx
)->wusb_dev
;
827 if (wusb_dev
== NULL
) {
828 /* reset no device? ignore */
829 dev_dbg(dev
, "DISCONNECT: no device at port %u, ignoring\n",
833 __wusbhc_dev_disconnect(wusbhc
, wusb_port_by_idx(wusbhc
, port_idx
));
836 ie
= kzalloc(sizeof(*ie
), GFP_KERNEL
);
839 ie
->hdr
.bLength
= sizeof(*ie
);
840 ie
->hdr
.bIEIdentifier
= WUIE_ID_DEVICE_DISCONNECT
;
841 ie
->bDeviceAddress
= wusb_dev
->addr
;
842 result
= wusbhc_mmcie_set(wusbhc
, 0, 0, &ie
->hdr
);
844 dev_err(dev
, "DISCONNECT: can't set MMC: %d\n", result
);
848 /* 120ms, hopefully 6 MMCs */
850 wusbhc_mmcie_rm(wusbhc
, &ie
->hdr
);
854 d_fnend(3, dev
, "(%p, %u) = %d\n", wusbhc
, port_idx
, result
);
858 static void wusb_cap_descr_printf(const unsigned level
, struct device
*dev
,
859 const struct usb_wireless_cap_descriptor
*wcd
)
862 "WUSB Capability Descriptor\n"
863 " bDevCapabilityType 0x%02x\n"
864 " bmAttributes 0x%02x\n"
865 " wPhyRates 0x%04x\n"
866 " bmTFITXPowerInfo 0x%02x\n"
867 " bmFFITXPowerInfo 0x%02x\n"
868 " bmBandGroup 0x%04x\n"
869 " bReserved 0x%02x\n",
870 wcd
->bDevCapabilityType
,
872 le16_to_cpu(wcd
->wPHYRates
),
873 wcd
->bmTFITXPowerInfo
,
874 wcd
->bmFFITXPowerInfo
,
880 * Walk over the BOS descriptor, verify and grok it
882 * @usb_dev: referenced
883 * @wusb_dev: referenced and unlocked
885 * The BOS descriptor is defined at WUSB1.0[7.4.1], and it defines a
886 * "flexible" way to wrap all kinds of descriptors inside an standard
887 * descriptor (wonder why they didn't use normal descriptors,
888 * btw). Not like they lack code.
890 * At the end we go to look for the WUSB Device Capabilities
891 * (WUSB1.0[7.4.1.1]) that is wrapped in a device capability descriptor
892 * that is part of the BOS descriptor set. That tells us what does the
893 * device support (dual role, beacon type, UWB PHY rates).
895 static int wusb_dev_bos_grok(struct usb_device
*usb_dev
,
896 struct wusb_dev
*wusb_dev
,
897 struct usb_bos_descriptor
*bos
, size_t desc_size
)
900 struct device
*dev
= &usb_dev
->dev
;
903 /* Walk over BOS capabilities, verify them */
904 itr
= (void *)bos
+ sizeof(*bos
);
905 top
= itr
+ desc_size
- sizeof(*bos
);
907 struct usb_dev_cap_header
*cap_hdr
= itr
;
910 if (top
- itr
< sizeof(*cap_hdr
)) {
911 dev_err(dev
, "Device BUG? premature end of BOS header "
912 "data [offset 0x%02x]: only %zu bytes left\n",
913 (int)(itr
- (void *)bos
), top
- itr
);
917 cap_size
= cap_hdr
->bLength
;
918 cap_type
= cap_hdr
->bDevCapabilityType
;
919 d_printf(4, dev
, "BOS Capability: 0x%02x (%zu bytes)\n",
923 if (cap_size
> top
- itr
) {
924 dev_err(dev
, "Device BUG? premature end of BOS data "
925 "[offset 0x%02x cap %02x %zu bytes]: "
926 "only %zu bytes left\n",
927 (int)(itr
- (void *)bos
),
928 cap_type
, cap_size
, top
- itr
);
932 d_dump(3, dev
, itr
, cap_size
);
934 case USB_CAP_TYPE_WIRELESS_USB
:
935 if (cap_size
!= sizeof(*wusb_dev
->wusb_cap_descr
))
936 dev_err(dev
, "Device BUG? WUSB Capability "
937 "descriptor is %zu bytes vs %zu "
938 "needed\n", cap_size
,
939 sizeof(*wusb_dev
->wusb_cap_descr
));
941 wusb_dev
->wusb_cap_descr
= itr
;
942 wusb_cap_descr_printf(3, dev
, itr
);
946 dev_err(dev
, "BUG? Unknown BOS capability 0x%02x "
947 "(%zu bytes) at offset 0x%02x\n", cap_type
,
948 cap_size
, (int)(itr
- (void *)bos
));
958 * Add information from the BOS descriptors to the device
960 * @usb_dev: referenced
961 * @wusb_dev: referenced and unlocked
963 * So what we do is we alloc a space for the BOS descriptor of 64
964 * bytes; read the first four bytes which include the wTotalLength
965 * field (WUSB1.0[T7-26]) and if it fits in those 64 bytes, read the
966 * whole thing. If not we realloc to that size.
968 * Then we call the groking function, that will fill up
969 * wusb_dev->wusb_cap_descr, which is what we'll need later on.
971 static int wusb_dev_bos_add(struct usb_device
*usb_dev
,
972 struct wusb_dev
*wusb_dev
)
975 struct device
*dev
= &usb_dev
->dev
;
976 struct usb_bos_descriptor
*bos
;
977 size_t alloc_size
= 32, desc_size
= 4;
979 bos
= kmalloc(alloc_size
, GFP_KERNEL
);
982 result
= usb_get_descriptor(usb_dev
, USB_DT_BOS
, 0, bos
, desc_size
);
984 dev_err(dev
, "Can't get BOS descriptor or too short: %zd\n",
986 goto error_get_descriptor
;
988 desc_size
= le16_to_cpu(bos
->wTotalLength
);
989 if (desc_size
>= alloc_size
) {
991 alloc_size
= desc_size
;
992 bos
= kmalloc(alloc_size
, GFP_KERNEL
);
996 result
= usb_get_descriptor(usb_dev
, USB_DT_BOS
, 0, bos
, desc_size
);
997 if (result
< 0 || result
!= desc_size
) {
998 dev_err(dev
, "Can't get BOS descriptor or too short (need "
999 "%zu bytes): %zd\n", desc_size
, result
);
1000 goto error_get_descriptor
;
1002 if (result
< sizeof(*bos
)
1003 || le16_to_cpu(bos
->wTotalLength
) != desc_size
) {
1004 dev_err(dev
, "Can't get BOS descriptor or too short (need "
1005 "%zu bytes): %zd\n", desc_size
, result
);
1006 goto error_get_descriptor
;
1008 d_printf(2, dev
, "Got BOS descriptor %zd bytes, %u capabilities\n",
1009 result
, bos
->bNumDeviceCaps
);
1010 d_dump(2, dev
, bos
, result
);
1011 result
= wusb_dev_bos_grok(usb_dev
, wusb_dev
, bos
, result
);
1014 wusb_dev
->bos
= bos
;
1018 error_get_descriptor
:
1020 wusb_dev
->wusb_cap_descr
= NULL
;
1024 static void wusb_dev_bos_rm(struct wusb_dev
*wusb_dev
)
1026 kfree(wusb_dev
->bos
);
1027 wusb_dev
->wusb_cap_descr
= NULL
;
1030 static struct usb_wireless_cap_descriptor wusb_cap_descr_default
= {
1031 .bLength
= sizeof(wusb_cap_descr_default
),
1032 .bDescriptorType
= USB_DT_DEVICE_CAPABILITY
,
1033 .bDevCapabilityType
= USB_CAP_TYPE_WIRELESS_USB
,
1035 .bmAttributes
= USB_WIRELESS_BEACON_NONE
,
1036 .wPHYRates
= cpu_to_le16(USB_WIRELESS_PHY_53
),
1037 .bmTFITXPowerInfo
= 0,
1038 .bmFFITXPowerInfo
= 0,
1039 .bmBandGroup
= cpu_to_le16(0x0001), /* WUSB1.0[7.4.1] bottom */
1044 * USB stack's device addition Notifier Callback
1046 * Called from drivers/usb/core/hub.c when a new device is added; we
1047 * use this hook to perform certain WUSB specific setup work on the
1048 * new device. As well, it is the first time we can connect the
1049 * wusb_dev and the usb_dev. So we note it down in wusb_dev and take a
1050 * reference that we'll drop.
1052 * First we need to determine if the device is a WUSB device (else we
1053 * ignore it). For that we use the speed setting (USB_SPEED_VARIABLE)
1054 * [FIXME: maybe we'd need something more definitive]. If so, we track
1055 * it's usb_busd and from there, the WUSB HC.
1057 * Because all WUSB HCs are contained in a 'struct wusbhc', voila, we
1058 * get the wusbhc for the device.
1060 * We have a reference on @usb_dev (as we are called at the end of its
1063 * NOTE: @usb_dev locked
1065 static void wusb_dev_add_ncb(struct usb_device
*usb_dev
)
1068 struct wusb_dev
*wusb_dev
;
1069 struct wusbhc
*wusbhc
;
1070 struct device
*dev
= &usb_dev
->dev
;
1073 if (usb_dev
->wusb
== 0 || usb_dev
->devnum
== 1)
1074 return; /* skip non wusb and wusb RHs */
1076 d_fnstart(3, dev
, "(usb_dev %p)\n", usb_dev
);
1078 wusbhc
= wusbhc_get_by_usb_dev(usb_dev
);
1081 mutex_lock(&wusbhc
->mutex
);
1082 wusb_dev
= __wusb_dev_get_by_usb_dev(wusbhc
, usb_dev
);
1083 port_idx
= wusb_port_no_to_idx(usb_dev
->portnum
);
1084 mutex_unlock(&wusbhc
->mutex
);
1085 if (wusb_dev
== NULL
)
1087 wusb_dev
->usb_dev
= usb_get_dev(usb_dev
);
1088 usb_dev
->wusb_dev
= wusb_dev_get(wusb_dev
);
1089 result
= wusb_dev_sec_add(wusbhc
, usb_dev
, wusb_dev
);
1091 dev_err(dev
, "Cannot enable security: %d\n", result
);
1094 /* Now query the device for it's BOS and attach it to wusb_dev */
1095 result
= wusb_dev_bos_add(usb_dev
, wusb_dev
);
1097 dev_err(dev
, "Cannot get BOS descriptors: %d\n", result
);
1100 result
= wusb_dev_sysfs_add(wusbhc
, usb_dev
, wusb_dev
);
1102 goto error_add_sysfs
;
1104 wusb_dev_put(wusb_dev
);
1107 d_fnend(3, dev
, "(usb_dev %p) = void\n", usb_dev
);
1110 wusb_dev_sysfs_rm(wusb_dev
);
1112 wusb_dev_bos_rm(wusb_dev
);
1114 wusb_dev_sec_rm(wusb_dev
);
1116 mutex_lock(&wusbhc
->mutex
);
1117 __wusbhc_dev_disconnect(wusbhc
, wusb_port_by_idx(wusbhc
, port_idx
));
1118 mutex_unlock(&wusbhc
->mutex
);
1123 * Undo all the steps done at connection by the notifier callback
1125 * NOTE: @usb_dev locked
1127 static void wusb_dev_rm_ncb(struct usb_device
*usb_dev
)
1129 struct wusb_dev
*wusb_dev
= usb_dev
->wusb_dev
;
1131 if (usb_dev
->wusb
== 0 || usb_dev
->devnum
== 1)
1132 return; /* skip non wusb and wusb RHs */
1134 wusb_dev_sysfs_rm(wusb_dev
);
1135 wusb_dev_bos_rm(wusb_dev
);
1136 wusb_dev_sec_rm(wusb_dev
);
1137 wusb_dev
->usb_dev
= NULL
;
1138 usb_dev
->wusb_dev
= NULL
;
1139 wusb_dev_put(wusb_dev
);
1140 usb_put_dev(usb_dev
);
1144 * Handle notifications from the USB stack (notifier call back)
1146 * This is called when the USB stack does a
1147 * usb_{bus,device}_{add,remove}() so we can do WUSB specific
1148 * handling. It is called with [for the case of
1149 * USB_DEVICE_{ADD,REMOVE} with the usb_dev locked.
1151 int wusb_usb_ncb(struct notifier_block
*nb
, unsigned long val
,
1154 int result
= NOTIFY_OK
;
1157 case USB_DEVICE_ADD
:
1158 wusb_dev_add_ncb(priv
);
1160 case USB_DEVICE_REMOVE
:
1161 wusb_dev_rm_ncb(priv
);
1164 /* ignore (for now) */
1165 case USB_BUS_REMOVE
:
1169 result
= NOTIFY_BAD
;
1175 * Return a referenced wusb_dev given a @wusbhc and @usb_dev
1177 struct wusb_dev
*__wusb_dev_get_by_usb_dev(struct wusbhc
*wusbhc
,
1178 struct usb_device
*usb_dev
)
1180 struct wusb_dev
*wusb_dev
;
1183 port_idx
= wusb_port_no_to_idx(usb_dev
->portnum
);
1184 BUG_ON(port_idx
> wusbhc
->ports_max
);
1185 wusb_dev
= wusb_port_by_idx(wusbhc
, port_idx
)->wusb_dev
;
1186 if (wusb_dev
!= NULL
) /* ops, device is gone */
1187 wusb_dev_get(wusb_dev
);
1190 EXPORT_SYMBOL_GPL(__wusb_dev_get_by_usb_dev
);
1192 void wusb_dev_destroy(struct kref
*_wusb_dev
)
1194 struct wusb_dev
*wusb_dev
1195 = container_of(_wusb_dev
, struct wusb_dev
, refcnt
);
1196 list_del_init(&wusb_dev
->cack_node
);
1197 wusb_dev_free(wusb_dev
);
1198 d_fnend(1, NULL
, "%s (wusb_dev %p) = void\n", __func__
, wusb_dev
);
1200 EXPORT_SYMBOL_GPL(wusb_dev_destroy
);
1203 * Create all the device connect handling infrastructure
1205 * This is basically the device info array, Connect Acknowledgement
1206 * (cack) lists, keep-alive timers (and delayed work thread).
1208 int wusbhc_devconnect_create(struct wusbhc
*wusbhc
)
1210 d_fnstart(3, wusbhc
->dev
, "(wusbhc %p)\n", wusbhc
);
1212 wusbhc
->keep_alive_ie
.hdr
.bIEIdentifier
= WUIE_ID_KEEP_ALIVE
;
1213 wusbhc
->keep_alive_ie
.hdr
.bLength
= sizeof(wusbhc
->keep_alive_ie
.hdr
);
1214 INIT_DELAYED_WORK(&wusbhc
->keep_alive_timer
, wusbhc_keep_alive_run
);
1216 wusbhc
->cack_ie
.hdr
.bIEIdentifier
= WUIE_ID_CONNECTACK
;
1217 wusbhc
->cack_ie
.hdr
.bLength
= sizeof(wusbhc
->cack_ie
.hdr
);
1218 INIT_LIST_HEAD(&wusbhc
->cack_list
);
1220 d_fnend(3, wusbhc
->dev
, "(wusbhc %p) = void\n", wusbhc
);
1225 * Release all resources taken by the devconnect stuff
1227 void wusbhc_devconnect_destroy(struct wusbhc
*wusbhc
)
1229 d_fnstart(3, wusbhc
->dev
, "(wusbhc %p)\n", wusbhc
);
1230 d_fnend(3, wusbhc
->dev
, "(wusbhc %p) = void\n", wusbhc
);
1234 * wusbhc_devconnect_start - start accepting device connections
1235 * @wusbhc: the WUSB HC
1237 * Sets the Host Info IE to accept all new connections.
1239 * FIXME: This also enables the keep alives but this is not necessary
1240 * until there are connected and authenticated devices.
1242 int wusbhc_devconnect_start(struct wusbhc
*wusbhc
,
1243 const struct wusb_ckhdid
*chid
)
1245 struct device
*dev
= wusbhc
->dev
;
1246 struct wuie_host_info
*hi
;
1249 hi
= kzalloc(sizeof(*hi
), GFP_KERNEL
);
1253 hi
->hdr
.bLength
= sizeof(*hi
);
1254 hi
->hdr
.bIEIdentifier
= WUIE_ID_HOST_INFO
;
1255 hi
->attributes
= cpu_to_le16((wusbhc
->rsv
->stream
<< 3) | WUIE_HI_CAP_ALL
);
1257 result
= wusbhc_mmcie_set(wusbhc
, 0, 0, &hi
->hdr
);
1259 dev_err(dev
, "Cannot add Host Info MMCIE: %d\n", result
);
1260 goto error_mmcie_set
;
1262 wusbhc
->wuie_host_info
= hi
;
1264 queue_delayed_work(wusbd
, &wusbhc
->keep_alive_timer
,
1265 (wusbhc
->trust_timeout
*CONFIG_HZ
)/1000/2);
1275 * wusbhc_devconnect_stop - stop managing connected devices
1276 * @wusbhc: the WUSB HC
1278 * Removes the Host Info IE and stops the keep alives.
1280 * FIXME: should this disconnect all devices?
1282 void wusbhc_devconnect_stop(struct wusbhc
*wusbhc
)
1284 cancel_delayed_work_sync(&wusbhc
->keep_alive_timer
);
1285 WARN_ON(!list_empty(&wusbhc
->cack_list
));
1287 wusbhc_mmcie_rm(wusbhc
, &wusbhc
->wuie_host_info
->hdr
);
1288 kfree(wusbhc
->wuie_host_info
);
1289 wusbhc
->wuie_host_info
= NULL
;
1293 * wusb_set_dev_addr - set the WUSB device address used by the host
1294 * @wusbhc: the WUSB HC the device is connect to
1295 * @wusb_dev: the WUSB device
1296 * @addr: new device address
1298 int wusb_set_dev_addr(struct wusbhc
*wusbhc
, struct wusb_dev
*wusb_dev
, u8 addr
)
1302 wusb_dev
->addr
= addr
;
1303 result
= wusbhc
->dev_info_set(wusbhc
, wusb_dev
);
1305 dev_err(wusbhc
->dev
, "device %d: failed to set device "
1306 "address\n", wusb_dev
->port_idx
);
1308 dev_info(wusbhc
->dev
, "device %d: %s addr %u\n",
1310 (addr
& WUSB_DEV_ADDR_UNAUTH
) ? "unauth" : "auth",