include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / usb / wusbcore / wa-rpipe.c
blobf0d546c5a089d3513f037b2eb4c63c841fff8cb5
1 /*
2 * WUSB Wire Adapter
3 * rpipe management
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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.
23 * FIXME: docs
25 * RPIPE
27 * Targeted at different downstream endpoints
29 * Descriptor: use to config the remote pipe.
31 * The number of blocks could be dynamic (wBlocks in descriptor is
32 * 0)--need to schedule them then.
34 * Each bit in wa->rpipe_bm represents if an rpipe is being used or
35 * not. Rpipes are represented with a 'struct wa_rpipe' that is
36 * attached to the hcpriv member of a 'struct usb_host_endpoint'.
38 * When you need to xfer data to an endpoint, you get an rpipe for it
39 * with wa_ep_rpipe_get(), which gives you a reference to the rpipe
40 * and keeps a single one (the first one) with the endpoint. When you
41 * are done transferring, you drop that reference. At the end the
42 * rpipe is always allocated and bound to the endpoint. There it might
43 * be recycled when not used.
45 * Addresses:
47 * We use a 1:1 mapping mechanism between port address (0 based
48 * index, actually) and the address. The USB stack knows about this.
50 * USB Stack port number 4 (1 based)
51 * WUSB code port index 3 (0 based)
52 * USB Address 5 (2 based -- 0 is for default, 1 for root hub)
54 * Now, because we don't use the concept as default address exactly
55 * like the (wired) USB code does, we need to kind of skip it. So we
56 * never take addresses from the urb->pipe, but from the
57 * urb->dev->devnum, to make sure that we always have the right
58 * destination address.
60 #include <linux/init.h>
61 #include <linux/atomic.h>
62 #include <linux/bitmap.h>
63 #include <linux/slab.h>
64 #include <linux/export.h>
66 #include "wusbhc.h"
67 #include "wa-hc.h"
69 static int __rpipe_get_descr(struct wahc *wa,
70 struct usb_rpipe_descriptor *descr, u16 index)
72 ssize_t result;
73 struct device *dev = &wa->usb_iface->dev;
75 /* Get the RPIPE descriptor -- we cannot use the usb_get_descriptor()
76 * function because the arguments are different.
78 result = usb_control_msg(
79 wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0),
80 USB_REQ_GET_DESCRIPTOR,
81 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_RPIPE,
82 USB_DT_RPIPE<<8, index, descr, sizeof(*descr),
83 1000 /* FIXME: arbitrary */);
84 if (result < 0) {
85 dev_err(dev, "rpipe %u: get descriptor failed: %d\n",
86 index, (int)result);
87 goto error;
89 if (result < sizeof(*descr)) {
90 dev_err(dev, "rpipe %u: got short descriptor "
91 "(%zd vs %zd bytes needed)\n",
92 index, result, sizeof(*descr));
93 result = -EINVAL;
94 goto error;
96 result = 0;
98 error:
99 return result;
104 * The descriptor is assumed to be properly initialized (ie: you got
105 * it through __rpipe_get_descr()).
107 static int __rpipe_set_descr(struct wahc *wa,
108 struct usb_rpipe_descriptor *descr, u16 index)
110 ssize_t result;
111 struct device *dev = &wa->usb_iface->dev;
113 /* we cannot use the usb_get_descriptor() function because the
114 * arguments are different.
116 result = usb_control_msg(
117 wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
118 USB_REQ_SET_DESCRIPTOR,
119 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
120 USB_DT_RPIPE<<8, index, descr, sizeof(*descr),
121 HZ / 10);
122 if (result < 0) {
123 dev_err(dev, "rpipe %u: set descriptor failed: %d\n",
124 index, (int)result);
125 goto error;
127 if (result < sizeof(*descr)) {
128 dev_err(dev, "rpipe %u: sent short descriptor "
129 "(%zd vs %zd bytes required)\n",
130 index, result, sizeof(*descr));
131 result = -EINVAL;
132 goto error;
134 result = 0;
136 error:
137 return result;
141 static void rpipe_init(struct wa_rpipe *rpipe)
143 kref_init(&rpipe->refcnt);
144 spin_lock_init(&rpipe->seg_lock);
145 INIT_LIST_HEAD(&rpipe->seg_list);
148 static unsigned rpipe_get_idx(struct wahc *wa, unsigned rpipe_idx)
150 unsigned long flags;
152 spin_lock_irqsave(&wa->rpipe_bm_lock, flags);
153 rpipe_idx = find_next_zero_bit(wa->rpipe_bm, wa->rpipes, rpipe_idx);
154 if (rpipe_idx < wa->rpipes)
155 set_bit(rpipe_idx, wa->rpipe_bm);
156 spin_unlock_irqrestore(&wa->rpipe_bm_lock, flags);
158 return rpipe_idx;
161 static void rpipe_put_idx(struct wahc *wa, unsigned rpipe_idx)
163 unsigned long flags;
165 spin_lock_irqsave(&wa->rpipe_bm_lock, flags);
166 clear_bit(rpipe_idx, wa->rpipe_bm);
167 spin_unlock_irqrestore(&wa->rpipe_bm_lock, flags);
170 void rpipe_destroy(struct kref *_rpipe)
172 struct wa_rpipe *rpipe = container_of(_rpipe, struct wa_rpipe, refcnt);
173 u8 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
175 if (rpipe->ep)
176 rpipe->ep->hcpriv = NULL;
177 rpipe_put_idx(rpipe->wa, index);
178 wa_put(rpipe->wa);
179 kfree(rpipe);
181 EXPORT_SYMBOL_GPL(rpipe_destroy);
184 * Locate an idle rpipe, create an structure for it and return it
186 * @wa is referenced and unlocked
187 * @crs enum rpipe_attr, required endpoint characteristics
189 * The rpipe can be used only sequentially (not in parallel).
191 * The rpipe is moved into the "ready" state.
193 static int rpipe_get_idle(struct wa_rpipe **prpipe, struct wahc *wa, u8 crs,
194 gfp_t gfp)
196 int result;
197 unsigned rpipe_idx;
198 struct wa_rpipe *rpipe;
199 struct device *dev = &wa->usb_iface->dev;
201 rpipe = kzalloc(sizeof(*rpipe), gfp);
202 if (rpipe == NULL)
203 return -ENOMEM;
204 rpipe_init(rpipe);
206 /* Look for an idle pipe */
207 for (rpipe_idx = 0; rpipe_idx < wa->rpipes; rpipe_idx++) {
208 rpipe_idx = rpipe_get_idx(wa, rpipe_idx);
209 if (rpipe_idx >= wa->rpipes) /* no more pipes :( */
210 break;
211 result = __rpipe_get_descr(wa, &rpipe->descr, rpipe_idx);
212 if (result < 0)
213 dev_err(dev, "Can't get descriptor for rpipe %u: %d\n",
214 rpipe_idx, result);
215 else if ((rpipe->descr.bmCharacteristics & crs) != 0)
216 goto found;
217 rpipe_put_idx(wa, rpipe_idx);
219 *prpipe = NULL;
220 kfree(rpipe);
221 return -ENXIO;
223 found:
224 set_bit(rpipe_idx, wa->rpipe_bm);
225 rpipe->wa = wa_get(wa);
226 *prpipe = rpipe;
227 return 0;
230 static int __rpipe_reset(struct wahc *wa, unsigned index)
232 int result;
233 struct device *dev = &wa->usb_iface->dev;
235 result = usb_control_msg(
236 wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
237 USB_REQ_RPIPE_RESET,
238 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
239 0, index, NULL, 0, 1000 /* FIXME: arbitrary */);
240 if (result < 0)
241 dev_err(dev, "rpipe %u: reset failed: %d\n",
242 index, result);
243 return result;
247 * Fake companion descriptor for ep0
249 * See WUSB1.0[7.4.4], most of this is zero for bulk/int/ctl
251 static struct usb_wireless_ep_comp_descriptor epc0 = {
252 .bLength = sizeof(epc0),
253 .bDescriptorType = USB_DT_WIRELESS_ENDPOINT_COMP,
254 /* .bMaxBurst = 1, */
255 .bMaxSequence = 31,
259 * Look for EP companion descriptor
261 * Get there, look for Inara in the endpoint's extra descriptors
263 static struct usb_wireless_ep_comp_descriptor *rpipe_epc_find(
264 struct device *dev, struct usb_host_endpoint *ep)
266 void *itr;
267 size_t itr_size;
268 struct usb_descriptor_header *hdr;
269 struct usb_wireless_ep_comp_descriptor *epcd;
271 if (ep->desc.bEndpointAddress == 0) {
272 epcd = &epc0;
273 goto out;
275 itr = ep->extra;
276 itr_size = ep->extralen;
277 epcd = NULL;
278 while (itr_size > 0) {
279 if (itr_size < sizeof(*hdr)) {
280 dev_err(dev, "HW Bug? ep 0x%02x: extra descriptors "
281 "at offset %zu: only %zu bytes left\n",
282 ep->desc.bEndpointAddress,
283 itr - (void *) ep->extra, itr_size);
284 break;
286 hdr = itr;
287 if (hdr->bDescriptorType == USB_DT_WIRELESS_ENDPOINT_COMP) {
288 epcd = itr;
289 break;
291 if (hdr->bLength > itr_size) {
292 dev_err(dev, "HW Bug? ep 0x%02x: extra descriptor "
293 "at offset %zu (type 0x%02x) "
294 "length %d but only %zu bytes left\n",
295 ep->desc.bEndpointAddress,
296 itr - (void *) ep->extra, hdr->bDescriptorType,
297 hdr->bLength, itr_size);
298 break;
300 itr += hdr->bLength;
301 itr_size -= hdr->bDescriptorType;
303 out:
304 return epcd;
308 * Aim an rpipe to its device & endpoint destination
310 * Make sure we change the address to unauthenticathed if the device
311 * is WUSB and it is not authenticated.
313 static int rpipe_aim(struct wa_rpipe *rpipe, struct wahc *wa,
314 struct usb_host_endpoint *ep, struct urb *urb, gfp_t gfp)
316 int result = -ENOMSG; /* better code for lack of companion? */
317 struct device *dev = &wa->usb_iface->dev;
318 struct usb_device *usb_dev = urb->dev;
319 struct usb_wireless_ep_comp_descriptor *epcd;
320 u8 unauth;
322 epcd = rpipe_epc_find(dev, ep);
323 if (epcd == NULL) {
324 dev_err(dev, "ep 0x%02x: can't find companion descriptor\n",
325 ep->desc.bEndpointAddress);
326 goto error;
328 unauth = usb_dev->wusb && !usb_dev->authenticated ? 0x80 : 0;
329 __rpipe_reset(wa, le16_to_cpu(rpipe->descr.wRPipeIndex));
330 atomic_set(&rpipe->segs_available, le16_to_cpu(rpipe->descr.wRequests));
331 /* FIXME: block allocation system; request with queuing and timeout */
332 /* FIXME: compute so seg_size > ep->maxpktsize */
333 rpipe->descr.wBlocks = cpu_to_le16(16); /* given */
334 /* ep0 maxpktsize is 0x200 (WUSB1.0[4.8.1]) */
335 rpipe->descr.wMaxPacketSize = cpu_to_le16(ep->desc.wMaxPacketSize);
336 rpipe->descr.bHSHubAddress = 0; /* reserved: zero */
337 rpipe->descr.bHSHubPort = wusb_port_no_to_idx(urb->dev->portnum);
338 /* FIXME: use maximum speed as supported or recommended by device */
339 rpipe->descr.bSpeed = usb_pipeendpoint(urb->pipe) == 0 ?
340 UWB_PHY_RATE_53 : UWB_PHY_RATE_200;
342 dev_dbg(dev, "addr %u (0x%02x) rpipe #%u ep# %u speed %d\n",
343 urb->dev->devnum, urb->dev->devnum | unauth,
344 le16_to_cpu(rpipe->descr.wRPipeIndex),
345 usb_pipeendpoint(urb->pipe), rpipe->descr.bSpeed);
347 /* see security.c:wusb_update_address() */
348 if (unlikely(urb->dev->devnum == 0x80))
349 rpipe->descr.bDeviceAddress = 0;
350 else
351 rpipe->descr.bDeviceAddress = urb->dev->devnum | unauth;
352 rpipe->descr.bEndpointAddress = ep->desc.bEndpointAddress;
353 /* FIXME: bDataSequence */
354 rpipe->descr.bDataSequence = 0;
355 /* FIXME: dwCurrentWindow */
356 rpipe->descr.dwCurrentWindow = cpu_to_le32(1);
357 /* FIXME: bMaxDataSequence */
358 rpipe->descr.bMaxDataSequence = epcd->bMaxSequence - 1;
359 rpipe->descr.bInterval = ep->desc.bInterval;
360 /* FIXME: bOverTheAirInterval */
361 rpipe->descr.bOverTheAirInterval = 0; /* 0 if not isoc */
362 /* FIXME: xmit power & preamble blah blah */
363 rpipe->descr.bmAttribute = ep->desc.bmAttributes & 0x03;
364 /* rpipe->descr.bmCharacteristics RO */
365 /* FIXME: bmRetryOptions */
366 rpipe->descr.bmRetryOptions = 15;
367 /* FIXME: use for assessing link quality? */
368 rpipe->descr.wNumTransactionErrors = 0;
369 result = __rpipe_set_descr(wa, &rpipe->descr,
370 le16_to_cpu(rpipe->descr.wRPipeIndex));
371 if (result < 0) {
372 dev_err(dev, "Cannot aim rpipe: %d\n", result);
373 goto error;
375 result = 0;
376 error:
377 return result;
381 * Check an aimed rpipe to make sure it points to where we want
383 * We use bit 19 of the Linux USB pipe bitmap for unauth vs auth
384 * space; when it is like that, we or 0x80 to make an unauth address.
386 static int rpipe_check_aim(const struct wa_rpipe *rpipe, const struct wahc *wa,
387 const struct usb_host_endpoint *ep,
388 const struct urb *urb, gfp_t gfp)
390 int result = 0; /* better code for lack of companion? */
391 struct device *dev = &wa->usb_iface->dev;
392 struct usb_device *usb_dev = urb->dev;
393 u8 unauth = (usb_dev->wusb && !usb_dev->authenticated) ? 0x80 : 0;
394 u8 portnum = wusb_port_no_to_idx(urb->dev->portnum);
396 #define AIM_CHECK(rdf, val, text) \
397 do { \
398 if (rpipe->descr.rdf != (val)) { \
399 dev_err(dev, \
400 "rpipe aim discrepancy: " #rdf " " text "\n", \
401 rpipe->descr.rdf, (val)); \
402 result = -EINVAL; \
403 WARN_ON(1); \
405 } while (0)
406 AIM_CHECK(wMaxPacketSize, cpu_to_le16(ep->desc.wMaxPacketSize),
407 "(%u vs %u)");
408 AIM_CHECK(bHSHubPort, portnum, "(%u vs %u)");
409 AIM_CHECK(bSpeed, usb_pipeendpoint(urb->pipe) == 0 ?
410 UWB_PHY_RATE_53 : UWB_PHY_RATE_200,
411 "(%u vs %u)");
412 AIM_CHECK(bDeviceAddress, urb->dev->devnum | unauth, "(%u vs %u)");
413 AIM_CHECK(bEndpointAddress, ep->desc.bEndpointAddress, "(%u vs %u)");
414 AIM_CHECK(bInterval, ep->desc.bInterval, "(%u vs %u)");
415 AIM_CHECK(bmAttribute, ep->desc.bmAttributes & 0x03, "(%u vs %u)");
416 #undef AIM_CHECK
417 return result;
420 #ifndef CONFIG_BUG
421 #define CONFIG_BUG 0
422 #endif
425 * Make sure there is an rpipe allocated for an endpoint
427 * If already allocated, we just refcount it; if not, we get an
428 * idle one, aim it to the right location and take it.
430 * Attaches to ep->hcpriv and rpipe->ep to ep.
432 int rpipe_get_by_ep(struct wahc *wa, struct usb_host_endpoint *ep,
433 struct urb *urb, gfp_t gfp)
435 int result = 0;
436 struct device *dev = &wa->usb_iface->dev;
437 struct wa_rpipe *rpipe;
438 u8 eptype;
440 mutex_lock(&wa->rpipe_mutex);
441 rpipe = ep->hcpriv;
442 if (rpipe != NULL) {
443 if (CONFIG_BUG == 1) {
444 result = rpipe_check_aim(rpipe, wa, ep, urb, gfp);
445 if (result < 0)
446 goto error;
448 __rpipe_get(rpipe);
449 dev_dbg(dev, "ep 0x%02x: reusing rpipe %u\n",
450 ep->desc.bEndpointAddress,
451 le16_to_cpu(rpipe->descr.wRPipeIndex));
452 } else {
453 /* hmm, assign idle rpipe, aim it */
454 result = -ENOBUFS;
455 eptype = ep->desc.bmAttributes & 0x03;
456 result = rpipe_get_idle(&rpipe, wa, 1 << eptype, gfp);
457 if (result < 0)
458 goto error;
459 result = rpipe_aim(rpipe, wa, ep, urb, gfp);
460 if (result < 0) {
461 rpipe_put(rpipe);
462 goto error;
464 ep->hcpriv = rpipe;
465 rpipe->ep = ep;
466 __rpipe_get(rpipe); /* for caching into ep->hcpriv */
467 dev_dbg(dev, "ep 0x%02x: using rpipe %u\n",
468 ep->desc.bEndpointAddress,
469 le16_to_cpu(rpipe->descr.wRPipeIndex));
471 error:
472 mutex_unlock(&wa->rpipe_mutex);
473 return result;
477 * Allocate the bitmap for each rpipe.
479 int wa_rpipes_create(struct wahc *wa)
481 wa->rpipes = wa->wa_descr->wNumRPipes;
482 wa->rpipe_bm = kzalloc(BITS_TO_LONGS(wa->rpipes)*sizeof(unsigned long),
483 GFP_KERNEL);
484 if (wa->rpipe_bm == NULL)
485 return -ENOMEM;
486 return 0;
489 void wa_rpipes_destroy(struct wahc *wa)
491 struct device *dev = &wa->usb_iface->dev;
493 if (!bitmap_empty(wa->rpipe_bm, wa->rpipes)) {
494 char buf[256];
495 WARN_ON(1);
496 bitmap_scnprintf(buf, sizeof(buf), wa->rpipe_bm, wa->rpipes);
497 dev_err(dev, "BUG: pipes not released on exit: %s\n", buf);
499 kfree(wa->rpipe_bm);
503 * Release resources allocated for an endpoint
505 * If there is an associated rpipe to this endpoint, Abort any pending
506 * transfers and put it. If the rpipe ends up being destroyed,
507 * __rpipe_destroy() will cleanup ep->hcpriv.
509 * This is called before calling hcd->stop(), so you don't need to do
510 * anything else in there.
512 void rpipe_ep_disable(struct wahc *wa, struct usb_host_endpoint *ep)
514 struct wa_rpipe *rpipe;
516 mutex_lock(&wa->rpipe_mutex);
517 rpipe = ep->hcpriv;
518 if (rpipe != NULL) {
519 u16 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
521 usb_control_msg(
522 wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0),
523 USB_REQ_RPIPE_ABORT,
524 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
525 0, index, NULL, 0, 1000 /* FIXME: arbitrary */);
526 rpipe_put(rpipe);
528 mutex_unlock(&wa->rpipe_mutex);
530 EXPORT_SYMBOL_GPL(rpipe_ep_disable);