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