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