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