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
27 * Targetted 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.
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 Addresss 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 <asm/atomic.h>
62 #include <linux/bitmap.h>
63 #include <linux/slab.h>
68 static int __rpipe_get_descr(struct wahc *wa,
69 struct usb_rpipe_descriptor *descr, u16 index)
72 struct device *dev = &wa->usb_iface->dev;
74 /* Get the RPIPE descriptor -- we cannot use the usb_get_descriptor()
75 * function because the arguments are different.
77 result = usb_control_msg(
78 wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0),
79 USB_REQ_GET_DESCRIPTOR,
80 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_RPIPE,
81 USB_DT_RPIPE<<8, index, descr, sizeof(*descr),
82 1000 /* FIXME: arbitrary */);
84 dev_err(dev, "rpipe %u: get descriptor failed: %d\n",
88 if (result < sizeof(*descr)) {
89 dev_err(dev, "rpipe %u: got short descriptor "
90 "(%zd vs %zd bytes needed)\n",
91 index, result, sizeof(*descr));
103 * The descriptor is assumed to be properly initialized (ie: you got
104 * it through __rpipe_get_descr()).
106 static int __rpipe_set_descr(struct wahc *wa,
107 struct usb_rpipe_descriptor *descr, u16 index)
110 struct device *dev = &wa->usb_iface->dev;
112 /* we cannot use the usb_get_descriptor() function because the
113 * arguments are different.
115 result = usb_control_msg(
116 wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
117 USB_REQ_SET_DESCRIPTOR,
118 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
119 USB_DT_RPIPE<<8, index, descr, sizeof(*descr),
122 dev_err(dev, "rpipe %u: set descriptor failed: %d\n",
126 if (result < sizeof(*descr)) {
127 dev_err(dev, "rpipe %u: sent short descriptor "
128 "(%zd vs %zd bytes required)\n",
129 index, result, sizeof(*descr));
140 static void rpipe_init(struct wa_rpipe *rpipe)
142 kref_init(&rpipe->refcnt);
143 spin_lock_init(&rpipe->seg_lock);
144 INIT_LIST_HEAD(&rpipe->seg_list);
147 static unsigned rpipe_get_idx(struct wahc *wa, unsigned rpipe_idx)
151 spin_lock_irqsave(&wa->rpipe_bm_lock, flags);
152 rpipe_idx = find_next_zero_bit(wa->rpipe_bm, wa->rpipes, rpipe_idx);
153 if (rpipe_idx < wa->rpipes)
154 set_bit(rpipe_idx, wa->rpipe_bm);
155 spin_unlock_irqrestore(&wa->rpipe_bm_lock, flags);
160 static void rpipe_put_idx(struct wahc *wa, unsigned rpipe_idx)
164 spin_lock_irqsave(&wa->rpipe_bm_lock, flags);
165 clear_bit(rpipe_idx, wa->rpipe_bm);
166 spin_unlock_irqrestore(&wa->rpipe_bm_lock, flags);
169 void rpipe_destroy(struct kref *_rpipe)
171 struct wa_rpipe *rpipe = container_of(_rpipe, struct wa_rpipe, refcnt);
172 u8 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
175 rpipe->ep->hcpriv = NULL;
176 rpipe_put_idx(rpipe->wa, index);
180 EXPORT_SYMBOL_GPL(rpipe_destroy);
183 * Locate an idle rpipe, create an structure for it and return it
185 * @wa is referenced and unlocked
186 * @crs enum rpipe_attr, required endpoint characteristics
188 * The rpipe can be used only sequentially (not in parallel).
190 * The rpipe is moved into the "ready" state.
192 static int rpipe_get_idle(struct wa_rpipe **prpipe, struct wahc *wa, u8 crs,
197 struct wa_rpipe *rpipe;
198 struct device *dev = &wa->usb_iface->dev;
200 rpipe = kzalloc(sizeof(*rpipe), gfp);
205 /* Look for an idle pipe */
206 for (rpipe_idx = 0; rpipe_idx < wa->rpipes; rpipe_idx++) {
207 rpipe_idx = rpipe_get_idx(wa, rpipe_idx);
208 if (rpipe_idx >= wa->rpipes) /* no more pipes :( */
210 result = __rpipe_get_descr(wa, &rpipe->descr, rpipe_idx);
212 dev_err(dev, "Can't get descriptor for rpipe %u: %d\n",
214 else if ((rpipe->descr.bmCharacteristics & crs) != 0)
216 rpipe_put_idx(wa, rpipe_idx);
223 set_bit(rpipe_idx, wa->rpipe_bm);
224 rpipe->wa = wa_get(wa);
229 static int __rpipe_reset(struct wahc *wa, unsigned index)
232 struct device *dev = &wa->usb_iface->dev;
234 result = usb_control_msg(
235 wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
237 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
238 0, index, NULL, 0, 1000 /* FIXME: arbitrary */);
240 dev_err(dev, "rpipe %u: reset failed: %d\n",
246 * Fake companion descriptor for ep0
248 * See WUSB1.0[7.4.4], most of this is zero for bulk/int/ctl
250 static struct usb_wireless_ep_comp_descriptor epc0 = {
251 .bLength = sizeof(epc0),
252 .bDescriptorType = USB_DT_WIRELESS_ENDPOINT_COMP,
253 /* .bMaxBurst = 1, */
258 * Look for EP companion descriptor
260 * Get there, look for Inara in the endpoint's extra descriptors
262 static struct usb_wireless_ep_comp_descriptor *rpipe_epc_find(
263 struct device *dev, struct usb_host_endpoint *ep)
267 struct usb_descriptor_header *hdr;
268 struct usb_wireless_ep_comp_descriptor *epcd;
270 if (ep->desc.bEndpointAddress == 0) {
275 itr_size = ep->extralen;
277 while (itr_size > 0) {
278 if (itr_size < sizeof(*hdr)) {
279 dev_err(dev, "HW Bug? ep 0x%02x: extra descriptors "
280 "at offset %zu: only %zu bytes left\n",
281 ep->desc.bEndpointAddress,
282 itr - (void *) ep->extra, itr_size);
286 if (hdr->bDescriptorType == USB_DT_WIRELESS_ENDPOINT_COMP) {
290 if (hdr->bLength > itr_size) {
291 dev_err(dev, "HW Bug? ep 0x%02x: extra descriptor "
292 "at offset %zu (type 0x%02x) "
293 "length %d but only %zu bytes left\n",
294 ep->desc.bEndpointAddress,
295 itr - (void *) ep->extra, hdr->bDescriptorType,
296 hdr->bLength, itr_size);
300 itr_size -= hdr->bDescriptorType;
307 * Aim an rpipe to its device & endpoint destination
309 * Make sure we change the address to unauthenticathed if the device
310 * is WUSB and it is not authenticated.
312 static int rpipe_aim(struct wa_rpipe *rpipe, struct wahc *wa,
313 struct usb_host_endpoint *ep, struct urb *urb, gfp_t gfp)
315 int result = -ENOMSG; /* better code for lack of companion? */
316 struct device *dev = &wa->usb_iface->dev;
317 struct usb_device *usb_dev = urb->dev;
318 struct usb_wireless_ep_comp_descriptor *epcd;
321 epcd = rpipe_epc_find(dev, ep);
323 dev_err(dev, "ep 0x%02x: can't find companion descriptor\n",
324 ep->desc.bEndpointAddress);
327 unauth = usb_dev->wusb && !usb_dev->authenticated ? 0x80 : 0;
328 __rpipe_reset(wa, le16_to_cpu(rpipe->descr.wRPipeIndex));
329 atomic_set(&rpipe->segs_available, le16_to_cpu(rpipe->descr.wRequests));
330 /* FIXME: block allocation system; request with queuing and timeout */
331 /* FIXME: compute so seg_size > ep->maxpktsize */
332 rpipe->descr.wBlocks = cpu_to_le16(16); /* given */
333 /* ep0 maxpktsize is 0x200 (WUSB1.0[4.8.1]) */
334 rpipe->descr.wMaxPacketSize = cpu_to_le16(ep->desc.wMaxPacketSize);
335 rpipe->descr.bHSHubAddress = 0; /* reserved: zero */
336 rpipe->descr.bHSHubPort = wusb_port_no_to_idx(urb->dev->portnum);
337 /* FIXME: use maximum speed as supported or recommended by device */
338 rpipe->descr.bSpeed = usb_pipeendpoint(urb->pipe) == 0 ?
339 UWB_PHY_RATE_53 : UWB_PHY_RATE_200;
341 dev_dbg(dev, "addr %u (0x%02x) rpipe #%u ep# %u speed %d\n",
342 urb->dev->devnum, urb->dev->devnum | unauth,
343 le16_to_cpu(rpipe->descr.wRPipeIndex),
344 usb_pipeendpoint(urb->pipe), rpipe->descr.bSpeed);
346 /* see security.c:wusb_update_address() */
347 if (unlikely(urb->dev->devnum == 0x80))
348 rpipe->descr.bDeviceAddress = 0;
350 rpipe->descr.bDeviceAddress = urb->dev->devnum | unauth;
351 rpipe->descr.bEndpointAddress = ep->desc.bEndpointAddress;
352 /* FIXME: bDataSequence */
353 rpipe->descr.bDataSequence = 0;
354 /* FIXME: dwCurrentWindow */
355 rpipe->descr.dwCurrentWindow = cpu_to_le32(1);
356 /* FIXME: bMaxDataSequence */
357 rpipe->descr.bMaxDataSequence = epcd->bMaxSequence - 1;
358 rpipe->descr.bInterval = ep->desc.bInterval;
359 /* FIXME: bOverTheAirInterval */
360 rpipe->descr.bOverTheAirInterval = 0; /* 0 if not isoc */
361 /* FIXME: xmit power & preamble blah blah */
362 rpipe->descr.bmAttribute = ep->desc.bmAttributes & 0x03;
363 /* rpipe->descr.bmCharacteristics RO */
364 /* FIXME: bmRetryOptions */
365 rpipe->descr.bmRetryOptions = 15;
366 /* FIXME: use for assessing link quality? */
367 rpipe->descr.wNumTransactionErrors = 0;
368 result = __rpipe_set_descr(wa, &rpipe->descr,
369 le16_to_cpu(rpipe->descr.wRPipeIndex));
371 dev_err(dev, "Cannot aim rpipe: %d\n", result);
380 * Check an aimed rpipe to make sure it points to where we want
382 * We use bit 19 of the Linux USB pipe bitmap for unauth vs auth
383 * space; when it is like that, we or 0x80 to make an unauth address.
385 static int rpipe_check_aim(const struct wa_rpipe *rpipe, const struct wahc *wa,
386 const struct usb_host_endpoint *ep,
387 const struct urb *urb, gfp_t gfp)
389 int result = 0; /* better code for lack of companion? */
390 struct device *dev = &wa->usb_iface->dev;
391 struct usb_device *usb_dev = urb->dev;
392 u8 unauth = (usb_dev->wusb && !usb_dev->authenticated) ? 0x80 : 0;
393 u8 portnum = wusb_port_no_to_idx(urb->dev->portnum);
395 #define AIM_CHECK(rdf, val, text) \
397 if (rpipe->descr.rdf != (val)) { \
399 "rpipe aim discrepancy: " #rdf " " text "\n", \
400 rpipe->descr.rdf, (val)); \
405 AIM_CHECK(wMaxPacketSize, cpu_to_le16(ep->desc.wMaxPacketSize),
407 AIM_CHECK(bHSHubPort, portnum, "(%u vs %u)");
408 AIM_CHECK(bSpeed, usb_pipeendpoint(urb->pipe) == 0 ?
409 UWB_PHY_RATE_53 : UWB_PHY_RATE_200,
411 AIM_CHECK(bDeviceAddress, urb->dev->devnum | unauth, "(%u vs %u)");
412 AIM_CHECK(bEndpointAddress, ep->desc.bEndpointAddress, "(%u vs %u)");
413 AIM_CHECK(bInterval, ep->desc.bInterval, "(%u vs %u)");
414 AIM_CHECK(bmAttribute, ep->desc.bmAttributes & 0x03, "(%u vs %u)");
424 * Make sure there is an rpipe allocated for an endpoint
426 * If already allocated, we just refcount it; if not, we get an
427 * idle one, aim it to the right location and take it.
429 * Attaches to ep->hcpriv and rpipe->ep to ep.
431 int rpipe_get_by_ep(struct wahc *wa, struct usb_host_endpoint *ep,
432 struct urb *urb, gfp_t gfp)
435 struct device *dev = &wa->usb_iface->dev;
436 struct wa_rpipe *rpipe;
439 mutex_lock(&wa->rpipe_mutex);
442 if (CONFIG_BUG == 1) {
443 result = rpipe_check_aim(rpipe, wa, ep, urb, gfp);
448 dev_dbg(dev, "ep 0x%02x: reusing rpipe %u\n",
449 ep->desc.bEndpointAddress,
450 le16_to_cpu(rpipe->descr.wRPipeIndex));
452 /* hmm, assign idle rpipe, aim it */
454 eptype = ep->desc.bmAttributes & 0x03;
455 result = rpipe_get_idle(&rpipe, wa, 1 << eptype, gfp);
458 result = rpipe_aim(rpipe, wa, ep, urb, gfp);
465 __rpipe_get(rpipe); /* for caching into ep->hcpriv */
466 dev_dbg(dev, "ep 0x%02x: using rpipe %u\n",
467 ep->desc.bEndpointAddress,
468 le16_to_cpu(rpipe->descr.wRPipeIndex));
471 mutex_unlock(&wa->rpipe_mutex);
476 * Allocate the bitmap for each rpipe.
478 int wa_rpipes_create(struct wahc *wa)
480 wa->rpipes = wa->wa_descr->wNumRPipes;
481 wa->rpipe_bm = kzalloc(BITS_TO_LONGS(wa->rpipes)*sizeof(unsigned long),
483 if (wa->rpipe_bm == NULL)
488 void wa_rpipes_destroy(struct wahc *wa)
490 struct device *dev = &wa->usb_iface->dev;
492 if (!bitmap_empty(wa->rpipe_bm, wa->rpipes)) {
495 bitmap_scnprintf(buf, sizeof(buf), wa->rpipe_bm, wa->rpipes);
496 dev_err(dev, "BUG: pipes not released on exit: %s\n", buf);
502 * Release resources allocated for an endpoint
504 * If there is an associated rpipe to this endpoint, Abort any pending
505 * transfers and put it. If the rpipe ends up being destroyed,
506 * __rpipe_destroy() will cleanup ep->hcpriv.
508 * This is called before calling hcd->stop(), so you don't need to do
509 * anything else in there.
511 void rpipe_ep_disable(struct wahc *wa, struct usb_host_endpoint *ep)
513 struct wa_rpipe *rpipe;
515 mutex_lock(&wa->rpipe_mutex);
518 u16 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
521 wa->usb_dev, usb_rcvctrlpipe(wa->usb_dev, 0),
523 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
524 0, index, NULL, 0, 1000 /* FIXME: arbitrary */);
527 mutex_unlock(&wa->rpipe_mutex);
529 EXPORT_SYMBOL_GPL(rpipe_ep_disable);