blob: d1af4e89111efd47da0ef7ee29c03b9ed2ae90f2 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090091#include <linux/slab.h>
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +010092#include <linux/workqueue.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040093#include <linux/export.h>
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +010094#include "wusbhc.h"
95
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +010096static void wusbhc_devconnect_acked_work(struct work_struct *work);
97
98static void wusb_dev_free(struct wusb_dev *wusb_dev)
99{
100 if (wusb_dev) {
101 kfree(wusb_dev->set_gtk_req);
102 usb_free_urb(wusb_dev->set_gtk_urb);
103 kfree(wusb_dev);
104 }
105}
106
107static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc)
108{
109 struct wusb_dev *wusb_dev;
110 struct urb *urb;
111 struct usb_ctrlrequest *req;
112
113 wusb_dev = kzalloc(sizeof(*wusb_dev), GFP_KERNEL);
114 if (wusb_dev == NULL)
115 goto err;
116
117 wusb_dev->wusbhc = wusbhc;
118
119 INIT_WORK(&wusb_dev->devconnect_acked_work, wusbhc_devconnect_acked_work);
120
121 urb = usb_alloc_urb(0, GFP_KERNEL);
122 if (urb == NULL)
123 goto err;
David Vrabel2e9729d2009-12-07 13:50:40 +0000124 wusb_dev->set_gtk_urb = urb;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100125
David Vrabel2e9729d2009-12-07 13:50:40 +0000126 req = kmalloc(sizeof(*req), GFP_KERNEL);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100127 if (req == NULL)
128 goto err;
David Vrabel2e9729d2009-12-07 13:50:40 +0000129 wusb_dev->set_gtk_req = req;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100130
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
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100137 return wusb_dev;
138err:
139 wusb_dev_free(wusb_dev);
140 return NULL;
141}
142
143
144/*
145 * Using the Connect-Ack list, fill out the @wusbhc Connect-Ack WUSB IE
146 * properly so that it can be added to the MMC.
147 *
148 * We just get the @wusbhc->ca_list and fill out the first four ones or
149 * less (per-spec WUSB1.0[7.5, before T7-38). If the ConnectAck WUSB
150 * IE is not allocated, we alloc it.
151 *
152 * @wusbhc->mutex must be taken
153 */
154static void wusbhc_fill_cack_ie(struct wusbhc *wusbhc)
155{
156 unsigned cnt;
157 struct wusb_dev *dev_itr;
158 struct wuie_connect_ack *cack_ie;
159
160 cack_ie = &wusbhc->cack_ie;
161 cnt = 0;
162 list_for_each_entry(dev_itr, &wusbhc->cack_list, cack_node) {
163 cack_ie->blk[cnt].CDID = dev_itr->cdid;
164 cack_ie->blk[cnt].bDeviceAddress = dev_itr->addr;
165 if (++cnt >= WUIE_ELT_MAX)
166 break;
167 }
168 cack_ie->hdr.bLength = sizeof(cack_ie->hdr)
169 + cnt * sizeof(cack_ie->blk[0]);
170}
171
172/*
173 * Register a new device that wants to connect
174 *
175 * A new device wants to connect, so we add it to the Connect-Ack
176 * list. We give it an address in the unauthorized range (bit 8 set);
177 * user space will have to drive authorization further on.
178 *
179 * @dev_addr: address to use for the device (which is also the port
180 * number).
181 *
182 * @wusbhc->mutex must be taken
183 */
184static struct wusb_dev *wusbhc_cack_add(struct wusbhc *wusbhc,
185 struct wusb_dn_connect *dnc,
186 const char *pr_cdid, u8 port_idx)
187{
188 struct device *dev = wusbhc->dev;
189 struct wusb_dev *wusb_dev;
190 int new_connection = wusb_dn_connect_new_connection(dnc);
191 u8 dev_addr;
192 int result;
193
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100194 /* Is it registered already? */
195 list_for_each_entry(wusb_dev, &wusbhc->cack_list, cack_node)
196 if (!memcmp(&wusb_dev->cdid, &dnc->CDID,
197 sizeof(wusb_dev->cdid)))
198 return wusb_dev;
199 /* We don't have it, create an entry, register it */
200 wusb_dev = wusb_dev_alloc(wusbhc);
David Vrabel8092d7c2008-10-16 13:56:53 +0100201 if (wusb_dev == NULL)
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100202 return NULL;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100203 wusb_dev_init(wusb_dev);
204 wusb_dev->cdid = dnc->CDID;
205 wusb_dev->port_idx = port_idx;
206
207 /*
208 * Devices are always available within the cluster reservation
209 * and since the hardware will take the intersection of the
210 * per-device availability and the cluster reservation, the
211 * per-device availability can simply be set to always
212 * available.
213 */
214 bitmap_fill(wusb_dev->availability.bm, UWB_NUM_MAS);
215
216 /* FIXME: handle reconnects instead of assuming connects are
217 always new. */
218 if (1 && new_connection == 0)
219 new_connection = 1;
220 if (new_connection) {
221 dev_addr = (port_idx + 2) | WUSB_DEV_ADDR_UNAUTH;
222
223 dev_info(dev, "Connecting new WUSB device to address %u, "
224 "port %u\n", dev_addr, port_idx);
225
226 result = wusb_set_dev_addr(wusbhc, wusb_dev, dev_addr);
Anderson Lizardof51c23b2008-09-17 16:34:31 +0100227 if (result < 0)
228 return NULL;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100229 }
230 wusb_dev->entry_ts = jiffies;
231 list_add_tail(&wusb_dev->cack_node, &wusbhc->cack_list);
232 wusbhc->cack_count++;
233 wusbhc_fill_cack_ie(wusbhc);
David Vrabelbce83692008-12-22 18:22:50 +0000234
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100235 return wusb_dev;
236}
237
238/*
239 * Remove a Connect-Ack context entry from the HCs view
240 *
241 * @wusbhc->mutex must be taken
242 */
243static void wusbhc_cack_rm(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
244{
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100245 list_del_init(&wusb_dev->cack_node);
246 wusbhc->cack_count--;
247 wusbhc_fill_cack_ie(wusbhc);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100248}
249
250/*
251 * @wusbhc->mutex must be taken */
252static
253void wusbhc_devconnect_acked(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
254{
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100255 wusbhc_cack_rm(wusbhc, wusb_dev);
256 if (wusbhc->cack_count)
257 wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
258 else
259 wusbhc_mmcie_rm(wusbhc, &wusbhc->cack_ie.hdr);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100260}
261
262static void wusbhc_devconnect_acked_work(struct work_struct *work)
263{
264 struct wusb_dev *wusb_dev = container_of(work, struct wusb_dev,
265 devconnect_acked_work);
266 struct wusbhc *wusbhc = wusb_dev->wusbhc;
267
268 mutex_lock(&wusbhc->mutex);
269 wusbhc_devconnect_acked(wusbhc, wusb_dev);
270 mutex_unlock(&wusbhc->mutex);
David Vrabel5936ac72009-04-08 17:36:32 +0000271
272 wusb_dev_put(wusb_dev);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100273}
274
275/*
276 * Ack a device for connection
277 *
278 * FIXME: docs
279 *
280 * @pr_cdid: Printable CDID...hex Use @dnc->cdid for the real deal.
281 *
282 * So we get the connect ack IE (may have been allocated already),
283 * find an empty connect block, an empty virtual port, create an
284 * address with it (see below), make it an unauth addr [bit 7 set] and
285 * set the MMC.
286 *
287 * Addresses: because WUSB hosts have no downstream hubs, we can do a
288 * 1:1 mapping between 'port number' and device
289 * address. This simplifies many things, as during this
290 * initial connect phase the USB stack has no knoledge of
291 * the device and hasn't assigned an address yet--we know
292 * USB's choose_address() will use the same euristics we
293 * use here, so we can assume which address will be assigned.
294 *
295 * USB stack always assigns address 1 to the root hub, so
296 * to the port number we add 2 (thus virtual port #0 is
297 * addr #2).
298 *
299 * @wusbhc shall be referenced
300 */
301static
302void wusbhc_devconnect_ack(struct wusbhc *wusbhc, struct wusb_dn_connect *dnc,
303 const char *pr_cdid)
304{
305 int result;
306 struct device *dev = wusbhc->dev;
307 struct wusb_dev *wusb_dev;
308 struct wusb_port *port;
309 unsigned idx, devnum;
310
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100311 mutex_lock(&wusbhc->mutex);
312
313 /* Check we are not handling it already */
314 for (idx = 0; idx < wusbhc->ports_max; idx++) {
315 port = wusb_port_by_idx(wusbhc, idx);
316 if (port->wusb_dev
David Vrabel8092d7c2008-10-16 13:56:53 +0100317 && memcmp(&dnc->CDID, &port->wusb_dev->cdid, sizeof(dnc->CDID)) == 0)
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100318 goto error_unlock;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100319 }
320 /* Look up those fake ports we have for a free one */
321 for (idx = 0; idx < wusbhc->ports_max; idx++) {
322 port = wusb_port_by_idx(wusbhc, idx);
323 if ((port->status & USB_PORT_STAT_POWER)
324 && !(port->status & USB_PORT_STAT_CONNECTION))
325 break;
326 }
327 if (idx >= wusbhc->ports_max) {
328 dev_err(dev, "Host controller can't connect more devices "
329 "(%u already connected); device %s rejected\n",
330 wusbhc->ports_max, pr_cdid);
331 /* NOTE: we could send a WUIE_Disconnect here, but we haven't
332 * event acked, so the device will eventually timeout the
333 * connection, right? */
334 goto error_unlock;
335 }
336
337 devnum = idx + 2;
338
339 /* Make sure we are using no crypto on that "virtual port" */
340 wusbhc->set_ptk(wusbhc, idx, 0, NULL, 0);
341
342 /* Grab a filled in Connect-Ack context, fill out the
343 * Connect-Ack Wireless USB IE, set the MMC */
344 wusb_dev = wusbhc_cack_add(wusbhc, dnc, pr_cdid, idx);
345 if (wusb_dev == NULL)
346 goto error_unlock;
347 result = wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
348 if (result < 0)
349 goto error_unlock;
350 /* Give the device at least 2ms (WUSB1.0[7.5.1p3]), let's do
351 * three for a good measure */
352 msleep(3);
353 port->wusb_dev = wusb_dev;
354 port->status |= USB_PORT_STAT_CONNECTION;
355 port->change |= USB_PORT_STAT_C_CONNECTION;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100356 /* Now the port status changed to connected; khubd will
357 * pick the change up and try to reset the port to bring it to
358 * the enabled state--so this process returns up to the stack
David Vrabel4656d5d2008-10-27 17:12:33 +0000359 * and it calls back into wusbhc_rh_port_reset().
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100360 */
361error_unlock:
362 mutex_unlock(&wusbhc->mutex);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100363 return;
364
365}
366
367/*
368 * Disconnect a Wireless USB device from its fake port
369 *
370 * Marks the port as disconnected so that khubd can pick up the change
371 * and drops our knowledge about the device.
372 *
373 * Assumes there is a device connected
374 *
375 * @port_index: zero based port number
376 *
377 * NOTE: @wusbhc->mutex is locked
378 *
379 * WARNING: From here it is not very safe to access anything hanging off
380 * wusb_dev
381 */
382static void __wusbhc_dev_disconnect(struct wusbhc *wusbhc,
383 struct wusb_port *port)
384{
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100385 struct wusb_dev *wusb_dev = port->wusb_dev;
386
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100387 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE
388 | USB_PORT_STAT_SUSPEND | USB_PORT_STAT_RESET
389 | USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
390 port->change |= USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE;
391 if (wusb_dev) {
David Vrabel9a9b1d12009-01-06 17:58:02 +0000392 dev_dbg(wusbhc->dev, "disconnecting device from port %d\n", wusb_dev->port_idx);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100393 if (!list_empty(&wusb_dev->cack_node))
394 list_del_init(&wusb_dev->cack_node);
395 /* For the one in cack_add() */
396 wusb_dev_put(wusb_dev);
397 }
398 port->wusb_dev = NULL;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100399
400 /* After a device disconnects, change the GTK (see [WUSB]
401 * section 6.2.11.2). */
David Vrabel8db324e2009-04-08 17:36:33 +0000402 if (wusbhc->active)
403 wusbhc_gtk_rekey(wusbhc);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100404
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100405 /* The Wireless USB part has forgotten about the device already; now
406 * khubd's timer will pick up the disconnection and remove the USB
407 * device from the system
408 */
409}
410
411/*
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100412 * Refresh the list of keep alives to emit in the MMC
413 *
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100414 * We only publish the first four devices that have a coming timeout
415 * condition. Then when we are done processing those, we go for the
416 * next ones. We ignore the ones that have timed out already (they'll
417 * be purged).
418 *
419 * This might cause the first devices to timeout the last devices in
420 * the port array...FIXME: come up with a better algorithm?
421 *
422 * Note we can't do much about MMC's ops errors; we hope next refresh
423 * will kind of handle it.
424 *
425 * NOTE: @wusbhc->mutex is locked
426 */
427static void __wusbhc_keep_alive(struct wusbhc *wusbhc)
428{
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100429 struct device *dev = wusbhc->dev;
430 unsigned cnt;
431 struct wusb_dev *wusb_dev;
432 struct wusb_port *wusb_port;
433 struct wuie_keep_alive *ie = &wusbhc->keep_alive_ie;
434 unsigned keep_alives, old_keep_alives;
435
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100436 old_keep_alives = ie->hdr.bLength - sizeof(ie->hdr);
437 keep_alives = 0;
438 for (cnt = 0;
David Vrabela23b6482010-03-22 14:50:14 +0000439 keep_alives < WUIE_ELT_MAX && cnt < wusbhc->ports_max;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100440 cnt++) {
441 unsigned tt = msecs_to_jiffies(wusbhc->trust_timeout);
442
443 wusb_port = wusb_port_by_idx(wusbhc, cnt);
444 wusb_dev = wusb_port->wusb_dev;
445
446 if (wusb_dev == NULL)
447 continue;
Thomas Pugliesef4042c02013-12-02 15:39:43 -0600448 if (wusb_dev->usb_dev == NULL)
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100449 continue;
450
451 if (time_after(jiffies, wusb_dev->entry_ts + tt)) {
452 dev_err(dev, "KEEPALIVE: device %u timed out\n",
453 wusb_dev->addr);
454 __wusbhc_dev_disconnect(wusbhc, wusb_port);
Thomas Pugliese7d9852a2013-06-06 10:40:49 -0500455 } else if (time_after(jiffies, wusb_dev->entry_ts + tt/3)) {
456 /* Approaching timeout cut off, need to refresh */
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100457 ie->bDeviceAddress[keep_alives++] = wusb_dev->addr;
458 }
459 }
460 if (keep_alives & 0x1) /* pad to even number ([WUSB] section 7.5.9) */
461 ie->bDeviceAddress[keep_alives++] = 0x7f;
462 ie->hdr.bLength = sizeof(ie->hdr) +
463 keep_alives*sizeof(ie->bDeviceAddress[0]);
David Vrabel8092d7c2008-10-16 13:56:53 +0100464 if (keep_alives > 0)
465 wusbhc_mmcie_set(wusbhc, 10, 5, &ie->hdr);
466 else if (old_keep_alives != 0)
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100467 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100468}
469
470/*
471 * Do a run through all devices checking for timeouts
472 */
473static void wusbhc_keep_alive_run(struct work_struct *ws)
474{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700475 struct delayed_work *dw = to_delayed_work(ws);
David Vrabel56968d02008-11-25 14:23:40 +0000476 struct wusbhc *wusbhc = container_of(dw, struct wusbhc, keep_alive_timer);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100477
David Vrabel56968d02008-11-25 14:23:40 +0000478 mutex_lock(&wusbhc->mutex);
479 __wusbhc_keep_alive(wusbhc);
480 mutex_unlock(&wusbhc->mutex);
481
482 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
483 msecs_to_jiffies(wusbhc->trust_timeout / 2));
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100484}
485
486/*
487 * Find the wusb_dev from its device address.
488 *
489 * The device can be found directly from the address (see
490 * wusb_cack_add() for where the device address is set to port_idx
491 * +2), except when the address is zero.
492 */
493static struct wusb_dev *wusbhc_find_dev_by_addr(struct wusbhc *wusbhc, u8 addr)
494{
495 int p;
496
497 if (addr == 0xff) /* unconnected */
498 return NULL;
499
500 if (addr > 0) {
501 int port = (addr & ~0x80) - 2;
502 if (port < 0 || port >= wusbhc->ports_max)
503 return NULL;
504 return wusb_port_by_idx(wusbhc, port)->wusb_dev;
505 }
506
507 /* Look for the device with address 0. */
508 for (p = 0; p < wusbhc->ports_max; p++) {
509 struct wusb_dev *wusb_dev = wusb_port_by_idx(wusbhc, p)->wusb_dev;
510 if (wusb_dev && wusb_dev->addr == addr)
511 return wusb_dev;
512 }
513 return NULL;
514}
515
516/*
517 * Handle a DN_Alive notification (WUSB1.0[7.6.1])
518 *
519 * This just updates the device activity timestamp and then refreshes
520 * the keep alive IE.
521 *
522 * @wusbhc shall be referenced and unlocked
523 */
524static void wusbhc_handle_dn_alive(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
525{
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100526 mutex_lock(&wusbhc->mutex);
527 wusb_dev->entry_ts = jiffies;
528 __wusbhc_keep_alive(wusbhc);
529 mutex_unlock(&wusbhc->mutex);
530}
531
532/*
533 * Handle a DN_Connect notification (WUSB1.0[7.6.1])
534 *
535 * @wusbhc
536 * @pkt_hdr
537 * @size: Size of the buffer where the notification resides; if the
538 * notification data suggests there should be more data than
539 * available, an error will be signaled and the whole buffer
540 * consumed.
541 *
542 * @wusbhc->mutex shall be held
543 */
544static void wusbhc_handle_dn_connect(struct wusbhc *wusbhc,
545 struct wusb_dn_hdr *dn_hdr,
546 size_t size)
547{
548 struct device *dev = wusbhc->dev;
549 struct wusb_dn_connect *dnc;
550 char pr_cdid[WUSB_CKHDID_STRSIZE];
551 static const char *beacon_behaviour[] = {
552 "reserved",
553 "self-beacon",
554 "directed-beacon",
555 "no-beacon"
556 };
557
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100558 if (size < sizeof(*dnc)) {
559 dev_err(dev, "DN CONNECT: short notification (%zu < %zu)\n",
560 size, sizeof(*dnc));
David Vrabelbce83692008-12-22 18:22:50 +0000561 return;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100562 }
563
564 dnc = container_of(dn_hdr, struct wusb_dn_connect, hdr);
565 ckhdid_printf(pr_cdid, sizeof(pr_cdid), &dnc->CDID);
566 dev_info(dev, "DN CONNECT: device %s @ %x (%s) wants to %s\n",
567 pr_cdid,
568 wusb_dn_connect_prev_dev_addr(dnc),
569 beacon_behaviour[wusb_dn_connect_beacon_behavior(dnc)],
570 wusb_dn_connect_new_connection(dnc) ? "connect" : "reconnect");
571 /* ACK the connect */
572 wusbhc_devconnect_ack(wusbhc, dnc, pr_cdid);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100573}
574
575/*
576 * Handle a DN_Disconnect notification (WUSB1.0[7.6.1])
577 *
578 * Device is going down -- do the disconnect.
579 *
580 * @wusbhc shall be referenced and unlocked
581 */
582static void wusbhc_handle_dn_disconnect(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
583{
584 struct device *dev = wusbhc->dev;
585
586 dev_info(dev, "DN DISCONNECT: device 0x%02x going down\n", wusb_dev->addr);
587
588 mutex_lock(&wusbhc->mutex);
589 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, wusb_dev->port_idx));
590 mutex_unlock(&wusbhc->mutex);
591}
592
593/*
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100594 * Handle a Device Notification coming a host
595 *
596 * The Device Notification comes from a host (HWA, DWA or WHCI)
597 * wrapped in a set of headers. Somebody else has peeled off those
598 * headers for us and we just get one Device Notifications.
599 *
600 * Invalid DNs (e.g., too short) are discarded.
601 *
602 * @wusbhc shall be referenced
603 *
604 * FIXMES:
605 * - implement priorities as in WUSB1.0[Table 7-55]?
606 */
607void wusbhc_handle_dn(struct wusbhc *wusbhc, u8 srcaddr,
608 struct wusb_dn_hdr *dn_hdr, size_t size)
609{
610 struct device *dev = wusbhc->dev;
611 struct wusb_dev *wusb_dev;
612
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100613 if (size < sizeof(struct wusb_dn_hdr)) {
614 dev_err(dev, "DN data shorter than DN header (%d < %d)\n",
615 (int)size, (int)sizeof(struct wusb_dn_hdr));
David Vrabelbce83692008-12-22 18:22:50 +0000616 return;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100617 }
618
619 wusb_dev = wusbhc_find_dev_by_addr(wusbhc, srcaddr);
620 if (wusb_dev == NULL && dn_hdr->bType != WUSB_DN_CONNECT) {
621 dev_dbg(dev, "ignoring DN %d from unconnected device %02x\n",
622 dn_hdr->bType, srcaddr);
David Vrabelbce83692008-12-22 18:22:50 +0000623 return;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100624 }
625
626 switch (dn_hdr->bType) {
627 case WUSB_DN_CONNECT:
628 wusbhc_handle_dn_connect(wusbhc, dn_hdr, size);
629 break;
630 case WUSB_DN_ALIVE:
631 wusbhc_handle_dn_alive(wusbhc, wusb_dev);
632 break;
633 case WUSB_DN_DISCONNECT:
634 wusbhc_handle_dn_disconnect(wusbhc, wusb_dev);
635 break;
636 case WUSB_DN_MASAVAILCHANGED:
637 case WUSB_DN_RWAKE:
638 case WUSB_DN_SLEEP:
639 /* FIXME: handle these DNs. */
640 break;
641 case WUSB_DN_EPRDY:
642 /* The hardware handles these. */
643 break;
644 default:
645 dev_warn(dev, "unknown DN %u (%d octets) from %u\n",
646 dn_hdr->bType, (int)size, srcaddr);
647 }
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100648}
649EXPORT_SYMBOL_GPL(wusbhc_handle_dn);
650
651/*
652 * Disconnect a WUSB device from a the cluster
653 *
654 * @wusbhc
655 * @port Fake port where the device is (wusbhc index, not USB port number).
656 *
657 * In Wireless USB, a disconnect is basically telling the device he is
658 * being disconnected and forgetting about him.
659 *
660 * We send the device a Device Disconnect IE (WUSB1.0[7.5.11]) for 100
661 * ms and then keep going.
662 *
663 * We don't do much in case of error; we always pretend we disabled
664 * the port and disconnected the device. If physically the request
665 * didn't get there (many things can fail in the way there), the stack
666 * will reject the device's communication attempts.
667 *
668 * @wusbhc should be refcounted and locked
669 */
670void __wusbhc_dev_disable(struct wusbhc *wusbhc, u8 port_idx)
671{
672 int result;
673 struct device *dev = wusbhc->dev;
674 struct wusb_dev *wusb_dev;
675 struct wuie_disconnect *ie;
676
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100677 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
678 if (wusb_dev == NULL) {
679 /* reset no device? ignore */
680 dev_dbg(dev, "DISCONNECT: no device at port %u, ignoring\n",
681 port_idx);
David Vrabelbce83692008-12-22 18:22:50 +0000682 return;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100683 }
684 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
685
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100686 ie = kzalloc(sizeof(*ie), GFP_KERNEL);
687 if (ie == NULL)
David Vrabelbce83692008-12-22 18:22:50 +0000688 return;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100689 ie->hdr.bLength = sizeof(*ie);
690 ie->hdr.bIEIdentifier = WUIE_ID_DEVICE_DISCONNECT;
691 ie->bDeviceAddress = wusb_dev->addr;
692 result = wusbhc_mmcie_set(wusbhc, 0, 0, &ie->hdr);
David Vrabelbce83692008-12-22 18:22:50 +0000693 if (result < 0)
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100694 dev_err(dev, "DISCONNECT: can't set MMC: %d\n", result);
David Vrabelbce83692008-12-22 18:22:50 +0000695 else {
696 /* At least 6 MMCs, assuming at least 1 MMC per zone. */
697 msleep(7*4);
698 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100699 }
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100700 kfree(ie);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100701}
702
703/*
704 * Walk over the BOS descriptor, verify and grok it
705 *
706 * @usb_dev: referenced
707 * @wusb_dev: referenced and unlocked
708 *
709 * The BOS descriptor is defined at WUSB1.0[7.4.1], and it defines a
710 * "flexible" way to wrap all kinds of descriptors inside an standard
711 * descriptor (wonder why they didn't use normal descriptors,
712 * btw). Not like they lack code.
713 *
714 * At the end we go to look for the WUSB Device Capabilities
715 * (WUSB1.0[7.4.1.1]) that is wrapped in a device capability descriptor
716 * that is part of the BOS descriptor set. That tells us what does the
717 * device support (dual role, beacon type, UWB PHY rates).
718 */
719static int wusb_dev_bos_grok(struct usb_device *usb_dev,
720 struct wusb_dev *wusb_dev,
721 struct usb_bos_descriptor *bos, size_t desc_size)
722{
723 ssize_t result;
724 struct device *dev = &usb_dev->dev;
725 void *itr, *top;
726
727 /* Walk over BOS capabilities, verify them */
728 itr = (void *)bos + sizeof(*bos);
729 top = itr + desc_size - sizeof(*bos);
730 while (itr < top) {
731 struct usb_dev_cap_header *cap_hdr = itr;
732 size_t cap_size;
733 u8 cap_type;
734 if (top - itr < sizeof(*cap_hdr)) {
735 dev_err(dev, "Device BUG? premature end of BOS header "
736 "data [offset 0x%02x]: only %zu bytes left\n",
737 (int)(itr - (void *)bos), top - itr);
738 result = -ENOSPC;
739 goto error_bad_cap;
740 }
741 cap_size = cap_hdr->bLength;
742 cap_type = cap_hdr->bDevCapabilityType;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100743 if (cap_size == 0)
744 break;
745 if (cap_size > top - itr) {
746 dev_err(dev, "Device BUG? premature end of BOS data "
747 "[offset 0x%02x cap %02x %zu bytes]: "
748 "only %zu bytes left\n",
749 (int)(itr - (void *)bos),
750 cap_type, cap_size, top - itr);
751 result = -EBADF;
752 goto error_bad_cap;
753 }
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100754 switch (cap_type) {
755 case USB_CAP_TYPE_WIRELESS_USB:
756 if (cap_size != sizeof(*wusb_dev->wusb_cap_descr))
757 dev_err(dev, "Device BUG? WUSB Capability "
758 "descriptor is %zu bytes vs %zu "
759 "needed\n", cap_size,
760 sizeof(*wusb_dev->wusb_cap_descr));
David Vrabelbce83692008-12-22 18:22:50 +0000761 else
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100762 wusb_dev->wusb_cap_descr = itr;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100763 break;
764 default:
765 dev_err(dev, "BUG? Unknown BOS capability 0x%02x "
766 "(%zu bytes) at offset 0x%02x\n", cap_type,
767 cap_size, (int)(itr - (void *)bos));
768 }
769 itr += cap_size;
770 }
771 result = 0;
772error_bad_cap:
773 return result;
774}
775
776/*
777 * Add information from the BOS descriptors to the device
778 *
779 * @usb_dev: referenced
780 * @wusb_dev: referenced and unlocked
781 *
782 * So what we do is we alloc a space for the BOS descriptor of 64
783 * bytes; read the first four bytes which include the wTotalLength
784 * field (WUSB1.0[T7-26]) and if it fits in those 64 bytes, read the
785 * whole thing. If not we realloc to that size.
786 *
787 * Then we call the groking function, that will fill up
788 * wusb_dev->wusb_cap_descr, which is what we'll need later on.
789 */
790static int wusb_dev_bos_add(struct usb_device *usb_dev,
791 struct wusb_dev *wusb_dev)
792{
793 ssize_t result;
794 struct device *dev = &usb_dev->dev;
795 struct usb_bos_descriptor *bos;
796 size_t alloc_size = 32, desc_size = 4;
797
798 bos = kmalloc(alloc_size, GFP_KERNEL);
799 if (bos == NULL)
800 return -ENOMEM;
801 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
802 if (result < 4) {
803 dev_err(dev, "Can't get BOS descriptor or too short: %zd\n",
804 result);
805 goto error_get_descriptor;
806 }
807 desc_size = le16_to_cpu(bos->wTotalLength);
808 if (desc_size >= alloc_size) {
809 kfree(bos);
810 alloc_size = desc_size;
811 bos = kmalloc(alloc_size, GFP_KERNEL);
812 if (bos == NULL)
813 return -ENOMEM;
814 }
815 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
816 if (result < 0 || result != desc_size) {
817 dev_err(dev, "Can't get BOS descriptor or too short (need "
818 "%zu bytes): %zd\n", desc_size, result);
819 goto error_get_descriptor;
820 }
821 if (result < sizeof(*bos)
822 || le16_to_cpu(bos->wTotalLength) != desc_size) {
823 dev_err(dev, "Can't get BOS descriptor or too short (need "
824 "%zu bytes): %zd\n", desc_size, result);
825 goto error_get_descriptor;
826 }
David Vrabelbce83692008-12-22 18:22:50 +0000827
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100828 result = wusb_dev_bos_grok(usb_dev, wusb_dev, bos, result);
829 if (result < 0)
830 goto error_bad_bos;
831 wusb_dev->bos = bos;
832 return 0;
833
834error_bad_bos:
835error_get_descriptor:
836 kfree(bos);
837 wusb_dev->wusb_cap_descr = NULL;
838 return result;
839}
840
841static void wusb_dev_bos_rm(struct wusb_dev *wusb_dev)
842{
843 kfree(wusb_dev->bos);
844 wusb_dev->wusb_cap_descr = NULL;
845};
846
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100847/*
848 * USB stack's device addition Notifier Callback
849 *
850 * Called from drivers/usb/core/hub.c when a new device is added; we
851 * use this hook to perform certain WUSB specific setup work on the
852 * new device. As well, it is the first time we can connect the
853 * wusb_dev and the usb_dev. So we note it down in wusb_dev and take a
854 * reference that we'll drop.
855 *
856 * First we need to determine if the device is a WUSB device (else we
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -0800857 * ignore it). For that we use the speed setting (USB_SPEED_WIRELESS)
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100858 * [FIXME: maybe we'd need something more definitive]. If so, we track
859 * it's usb_busd and from there, the WUSB HC.
860 *
861 * Because all WUSB HCs are contained in a 'struct wusbhc', voila, we
862 * get the wusbhc for the device.
863 *
864 * We have a reference on @usb_dev (as we are called at the end of its
865 * enumeration).
866 *
867 * NOTE: @usb_dev locked
868 */
869static void wusb_dev_add_ncb(struct usb_device *usb_dev)
870{
871 int result = 0;
872 struct wusb_dev *wusb_dev;
873 struct wusbhc *wusbhc;
874 struct device *dev = &usb_dev->dev;
875 u8 port_idx;
876
877 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
878 return; /* skip non wusb and wusb RHs */
879
David Vrabel6da9c992009-02-18 14:43:47 +0000880 usb_set_device_state(usb_dev, USB_STATE_UNAUTHENTICATED);
881
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100882 wusbhc = wusbhc_get_by_usb_dev(usb_dev);
883 if (wusbhc == NULL)
884 goto error_nodev;
885 mutex_lock(&wusbhc->mutex);
886 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, usb_dev);
887 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
888 mutex_unlock(&wusbhc->mutex);
889 if (wusb_dev == NULL)
890 goto error_nodev;
891 wusb_dev->usb_dev = usb_get_dev(usb_dev);
892 usb_dev->wusb_dev = wusb_dev_get(wusb_dev);
893 result = wusb_dev_sec_add(wusbhc, usb_dev, wusb_dev);
894 if (result < 0) {
895 dev_err(dev, "Cannot enable security: %d\n", result);
896 goto error_sec_add;
897 }
898 /* Now query the device for it's BOS and attach it to wusb_dev */
899 result = wusb_dev_bos_add(usb_dev, wusb_dev);
900 if (result < 0) {
901 dev_err(dev, "Cannot get BOS descriptors: %d\n", result);
902 goto error_bos_add;
903 }
904 result = wusb_dev_sysfs_add(wusbhc, usb_dev, wusb_dev);
905 if (result < 0)
906 goto error_add_sysfs;
907out:
908 wusb_dev_put(wusb_dev);
909 wusbhc_put(wusbhc);
910error_nodev:
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100911 return;
912
913 wusb_dev_sysfs_rm(wusb_dev);
914error_add_sysfs:
915 wusb_dev_bos_rm(wusb_dev);
916error_bos_add:
917 wusb_dev_sec_rm(wusb_dev);
918error_sec_add:
919 mutex_lock(&wusbhc->mutex);
920 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
921 mutex_unlock(&wusbhc->mutex);
922 goto out;
923}
924
925/*
926 * Undo all the steps done at connection by the notifier callback
927 *
928 * NOTE: @usb_dev locked
929 */
930static void wusb_dev_rm_ncb(struct usb_device *usb_dev)
931{
932 struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
933
934 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
935 return; /* skip non wusb and wusb RHs */
936
937 wusb_dev_sysfs_rm(wusb_dev);
938 wusb_dev_bos_rm(wusb_dev);
939 wusb_dev_sec_rm(wusb_dev);
940 wusb_dev->usb_dev = NULL;
941 usb_dev->wusb_dev = NULL;
942 wusb_dev_put(wusb_dev);
943 usb_put_dev(usb_dev);
944}
945
946/*
947 * Handle notifications from the USB stack (notifier call back)
948 *
949 * This is called when the USB stack does a
950 * usb_{bus,device}_{add,remove}() so we can do WUSB specific
951 * handling. It is called with [for the case of
952 * USB_DEVICE_{ADD,REMOVE} with the usb_dev locked.
953 */
954int wusb_usb_ncb(struct notifier_block *nb, unsigned long val,
955 void *priv)
956{
957 int result = NOTIFY_OK;
958
959 switch (val) {
960 case USB_DEVICE_ADD:
961 wusb_dev_add_ncb(priv);
962 break;
963 case USB_DEVICE_REMOVE:
964 wusb_dev_rm_ncb(priv);
965 break;
966 case USB_BUS_ADD:
967 /* ignore (for now) */
968 case USB_BUS_REMOVE:
969 break;
970 default:
971 WARN_ON(1);
972 result = NOTIFY_BAD;
Joe Perches2b84f922013-10-08 16:01:37 -0700973 }
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100974 return result;
975}
976
977/*
978 * Return a referenced wusb_dev given a @wusbhc and @usb_dev
979 */
980struct wusb_dev *__wusb_dev_get_by_usb_dev(struct wusbhc *wusbhc,
981 struct usb_device *usb_dev)
982{
983 struct wusb_dev *wusb_dev;
984 u8 port_idx;
985
986 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
987 BUG_ON(port_idx > wusbhc->ports_max);
988 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
989 if (wusb_dev != NULL) /* ops, device is gone */
990 wusb_dev_get(wusb_dev);
991 return wusb_dev;
992}
993EXPORT_SYMBOL_GPL(__wusb_dev_get_by_usb_dev);
994
995void wusb_dev_destroy(struct kref *_wusb_dev)
996{
David Vrabelbce83692008-12-22 18:22:50 +0000997 struct wusb_dev *wusb_dev = container_of(_wusb_dev, struct wusb_dev, refcnt);
998
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +0100999 list_del_init(&wusb_dev->cack_node);
1000 wusb_dev_free(wusb_dev);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001001}
1002EXPORT_SYMBOL_GPL(wusb_dev_destroy);
1003
1004/*
1005 * Create all the device connect handling infrastructure
1006 *
1007 * This is basically the device info array, Connect Acknowledgement
1008 * (cack) lists, keep-alive timers (and delayed work thread).
1009 */
1010int wusbhc_devconnect_create(struct wusbhc *wusbhc)
1011{
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001012 wusbhc->keep_alive_ie.hdr.bIEIdentifier = WUIE_ID_KEEP_ALIVE;
1013 wusbhc->keep_alive_ie.hdr.bLength = sizeof(wusbhc->keep_alive_ie.hdr);
1014 INIT_DELAYED_WORK(&wusbhc->keep_alive_timer, wusbhc_keep_alive_run);
1015
1016 wusbhc->cack_ie.hdr.bIEIdentifier = WUIE_ID_CONNECTACK;
1017 wusbhc->cack_ie.hdr.bLength = sizeof(wusbhc->cack_ie.hdr);
1018 INIT_LIST_HEAD(&wusbhc->cack_list);
1019
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001020 return 0;
1021}
1022
1023/*
1024 * Release all resources taken by the devconnect stuff
1025 */
1026void wusbhc_devconnect_destroy(struct wusbhc *wusbhc)
1027{
David Vrabelbce83692008-12-22 18:22:50 +00001028 /* no op */
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001029}
1030
1031/*
1032 * wusbhc_devconnect_start - start accepting device connections
1033 * @wusbhc: the WUSB HC
1034 *
1035 * Sets the Host Info IE to accept all new connections.
1036 *
1037 * FIXME: This also enables the keep alives but this is not necessary
1038 * until there are connected and authenticated devices.
1039 */
David Vrabel6fae35f2008-11-17 15:53:42 +00001040int wusbhc_devconnect_start(struct wusbhc *wusbhc)
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001041{
1042 struct device *dev = wusbhc->dev;
1043 struct wuie_host_info *hi;
1044 int result;
1045
1046 hi = kzalloc(sizeof(*hi), GFP_KERNEL);
1047 if (hi == NULL)
1048 return -ENOMEM;
1049
1050 hi->hdr.bLength = sizeof(*hi);
1051 hi->hdr.bIEIdentifier = WUIE_ID_HOST_INFO;
1052 hi->attributes = cpu_to_le16((wusbhc->rsv->stream << 3) | WUIE_HI_CAP_ALL);
David Vrabel6fae35f2008-11-17 15:53:42 +00001053 hi->CHID = wusbhc->chid;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001054 result = wusbhc_mmcie_set(wusbhc, 0, 0, &hi->hdr);
1055 if (result < 0) {
1056 dev_err(dev, "Cannot add Host Info MMCIE: %d\n", result);
1057 goto error_mmcie_set;
1058 }
1059 wusbhc->wuie_host_info = hi;
1060
1061 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
Thomas Pugliese7d9852a2013-06-06 10:40:49 -05001062 msecs_to_jiffies(wusbhc->trust_timeout / 2));
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001063
1064 return 0;
1065
1066error_mmcie_set:
1067 kfree(hi);
1068 return result;
1069}
1070
1071/*
1072 * wusbhc_devconnect_stop - stop managing connected devices
1073 * @wusbhc: the WUSB HC
1074 *
David Vrabel8db324e2009-04-08 17:36:33 +00001075 * Disconnects any devices still connected, stops the keep alives and
1076 * removes the Host Info IE.
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001077 */
1078void wusbhc_devconnect_stop(struct wusbhc *wusbhc)
1079{
David Vrabel8db324e2009-04-08 17:36:33 +00001080 int i;
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001081
David Vrabel8db324e2009-04-08 17:36:33 +00001082 mutex_lock(&wusbhc->mutex);
1083 for (i = 0; i < wusbhc->ports_max; i++) {
1084 if (wusbhc->port[i].wusb_dev)
1085 __wusbhc_dev_disconnect(wusbhc, &wusbhc->port[i]);
1086 }
1087 mutex_unlock(&wusbhc->mutex);
1088
1089 cancel_delayed_work_sync(&wusbhc->keep_alive_timer);
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001090 wusbhc_mmcie_rm(wusbhc, &wusbhc->wuie_host_info->hdr);
1091 kfree(wusbhc->wuie_host_info);
1092 wusbhc->wuie_host_info = NULL;
1093}
1094
1095/*
1096 * wusb_set_dev_addr - set the WUSB device address used by the host
1097 * @wusbhc: the WUSB HC the device is connect to
1098 * @wusb_dev: the WUSB device
1099 * @addr: new device address
1100 */
1101int wusb_set_dev_addr(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev, u8 addr)
1102{
1103 int result;
1104
1105 wusb_dev->addr = addr;
1106 result = wusbhc->dev_info_set(wusbhc, wusb_dev);
Anderson Lizardof51c23b2008-09-17 16:34:31 +01001107 if (result < 0)
Inaky Perez-Gonzalezb69fada2008-09-17 16:34:24 +01001108 dev_err(wusbhc->dev, "device %d: failed to set device "
1109 "address\n", wusb_dev->port_idx);
1110 else
1111 dev_info(wusbhc->dev, "device %d: %s addr %u\n",
1112 wusb_dev->port_idx,
1113 (addr & WUSB_DEV_ADDR_UNAUTH) ? "unauth" : "auth",
1114 wusb_dev->addr);
1115
1116 return result;
1117}