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