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