1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60#include <linux/atomic.h>
61#include <linux/bitmap.h>
62#include <linux/slab.h>
63#include <linux/export.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
75
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 USB_CTRL_GET_TIMEOUT);
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
104
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
113
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 USB_CTRL_SET_TIMEOUT);
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 INIT_LIST_HEAD(&rpipe->list_node);
146}
147
148static unsigned rpipe_get_idx(struct wahc *wa, unsigned rpipe_idx)
149{
150 unsigned long flags;
151
152 spin_lock_irqsave(&wa->rpipe_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_lock, flags);
157
158 return rpipe_idx;
159}
160
161static void rpipe_put_idx(struct wahc *wa, unsigned rpipe_idx)
162{
163 unsigned long flags;
164
165 spin_lock_irqsave(&wa->rpipe_lock, flags);
166 clear_bit(rpipe_idx, wa->rpipe_bm);
167 spin_unlock_irqrestore(&wa->rpipe_lock, flags);
168}
169
170void rpipe_destroy(struct kref *_rpipe)
171{
172 struct wa_rpipe *rpipe = container_of(_rpipe, struct wa_rpipe, refcnt);
173 u8 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
174
175 if (rpipe->ep)
176 rpipe->ep->hcpriv = NULL;
177 rpipe_put_idx(rpipe->wa, index);
178 wa_put(rpipe->wa);
179 kfree(rpipe);
180}
181EXPORT_SYMBOL_GPL(rpipe_destroy);
182
183
184
185
186
187
188
189
190
191
192
193static int rpipe_get_idle(struct wa_rpipe **prpipe, struct wahc *wa, u8 crs,
194 gfp_t gfp)
195{
196 int result;
197 unsigned rpipe_idx;
198 struct wa_rpipe *rpipe;
199 struct device *dev = &wa->usb_iface->dev;
200
201 rpipe = kzalloc(sizeof(*rpipe), gfp);
202 if (rpipe == NULL)
203 return -ENOMEM;
204 rpipe_init(rpipe);
205
206
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)
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);
218 }
219 *prpipe = NULL;
220 kfree(rpipe);
221 return -ENXIO;
222
223found:
224 set_bit(rpipe_idx, wa->rpipe_bm);
225 rpipe->wa = wa_get(wa);
226 *prpipe = rpipe;
227 return 0;
228}
229
230static int __rpipe_reset(struct wahc *wa, unsigned index)
231{
232 int result;
233 struct device *dev = &wa->usb_iface->dev;
234
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, USB_CTRL_SET_TIMEOUT);
240 if (result < 0)
241 dev_err(dev, "rpipe %u: reset failed: %d\n",
242 index, result);
243 return result;
244}
245
246
247
248
249
250
251static struct usb_wireless_ep_comp_descriptor epc0 = {
252 .bLength = sizeof(epc0),
253 .bDescriptorType = USB_DT_WIRELESS_ENDPOINT_COMP,
254 .bMaxBurst = 1,
255 .bMaxSequence = 2,
256};
257
258
259
260
261
262
263static struct usb_wireless_ep_comp_descriptor *rpipe_epc_find(
264 struct device *dev, struct usb_host_endpoint *ep)
265{
266 void *itr;
267 size_t itr_size;
268 struct usb_descriptor_header *hdr;
269 struct usb_wireless_ep_comp_descriptor *epcd;
270
271 if (ep->desc.bEndpointAddress == 0) {
272 epcd = &epc0;
273 goto out;
274 }
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;
285 }
286 hdr = itr;
287 if (hdr->bDescriptorType == USB_DT_WIRELESS_ENDPOINT_COMP) {
288 epcd = itr;
289 break;
290 }
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;
299 }
300 itr += hdr->bLength;
301 itr_size -= hdr->bLength;
302 }
303out:
304 return epcd;
305}
306
307
308
309
310
311
312
313static int rpipe_aim(struct wa_rpipe *rpipe, struct wahc *wa,
314 struct usb_host_endpoint *ep, struct urb *urb, gfp_t gfp)
315{
316 int result = -ENOMSG;
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 u32 ack_window, epcd_max_sequence;
321 u8 unauth;
322
323 epcd = rpipe_epc_find(dev, ep);
324 if (epcd == NULL) {
325 dev_err(dev, "ep 0x%02x: can't find companion descriptor\n",
326 ep->desc.bEndpointAddress);
327 goto error;
328 }
329 unauth = usb_dev->wusb && !usb_dev->authenticated ? 0x80 : 0;
330 __rpipe_reset(wa, le16_to_cpu(rpipe->descr.wRPipeIndex));
331 atomic_set(&rpipe->segs_available,
332 le16_to_cpu(rpipe->descr.wRequests));
333
334
335 rpipe->descr.wBlocks = cpu_to_le16(16);
336
337 if (usb_endpoint_xfer_isoc(&ep->desc))
338 rpipe->descr.wMaxPacketSize = epcd->wOverTheAirPacketSize;
339 else
340 rpipe->descr.wMaxPacketSize = ep->desc.wMaxPacketSize;
341
342 rpipe->descr.hwa_bMaxBurst = max(min_t(unsigned int,
343 epcd->bMaxBurst, 16U), 1U);
344 rpipe->descr.hwa_bDeviceInfoIndex =
345 wusb_port_no_to_idx(urb->dev->portnum);
346
347 rpipe->descr.bSpeed = usb_pipeendpoint(urb->pipe) == 0 ?
348 UWB_PHY_RATE_53 : UWB_PHY_RATE_200;
349
350 dev_dbg(dev, "addr %u (0x%02x) rpipe #%u ep# %u speed %d\n",
351 urb->dev->devnum, urb->dev->devnum | unauth,
352 le16_to_cpu(rpipe->descr.wRPipeIndex),
353 usb_pipeendpoint(urb->pipe), rpipe->descr.bSpeed);
354
355 rpipe->descr.hwa_reserved = 0;
356
357 rpipe->descr.bEndpointAddress = ep->desc.bEndpointAddress;
358
359 rpipe->descr.bDataSequence = 0;
360
361
362 ack_window = 0xFFFFFFFF >> (32 - rpipe->descr.hwa_bMaxBurst);
363 rpipe->descr.dwCurrentWindow = cpu_to_le32(ack_window);
364 epcd_max_sequence = max(min_t(unsigned int,
365 epcd->bMaxSequence, 32U), 2U);
366 rpipe->descr.bMaxDataSequence = epcd_max_sequence - 1;
367 rpipe->descr.bInterval = ep->desc.bInterval;
368 if (usb_endpoint_xfer_isoc(&ep->desc))
369 rpipe->descr.bOverTheAirInterval = epcd->bOverTheAirInterval;
370 else
371 rpipe->descr.bOverTheAirInterval = 0;
372
373 rpipe->descr.bmAttribute = (ep->desc.bmAttributes &
374 USB_ENDPOINT_XFERTYPE_MASK);
375
376 rpipe->descr.bmRetryOptions = (wa->wusb->retry_count & 0xF);
377
378 rpipe->descr.wNumTransactionErrors = 0;
379 result = __rpipe_set_descr(wa, &rpipe->descr,
380 le16_to_cpu(rpipe->descr.wRPipeIndex));
381 if (result < 0) {
382 dev_err(dev, "Cannot aim rpipe: %d\n", result);
383 goto error;
384 }
385 result = 0;
386error:
387 return result;
388}
389
390
391
392
393
394
395
396static int rpipe_check_aim(const struct wa_rpipe *rpipe, const struct wahc *wa,
397 const struct usb_host_endpoint *ep,
398 const struct urb *urb, gfp_t gfp)
399{
400 int result = 0;
401 struct device *dev = &wa->usb_iface->dev;
402 u8 portnum = wusb_port_no_to_idx(urb->dev->portnum);
403
404#define AIM_CHECK(rdf, val, text) \
405 do { \
406 if (rpipe->descr.rdf != (val)) { \
407 dev_err(dev, \
408 "rpipe aim discrepancy: " #rdf " " text "\n", \
409 rpipe->descr.rdf, (val)); \
410 result = -EINVAL; \
411 WARN_ON(1); \
412 } \
413 } while (0)
414 AIM_CHECK(hwa_bDeviceInfoIndex, portnum, "(%u vs %u)");
415 AIM_CHECK(bSpeed, usb_pipeendpoint(urb->pipe) == 0 ?
416 UWB_PHY_RATE_53 : UWB_PHY_RATE_200,
417 "(%u vs %u)");
418 AIM_CHECK(bEndpointAddress, ep->desc.bEndpointAddress, "(%u vs %u)");
419 AIM_CHECK(bInterval, ep->desc.bInterval, "(%u vs %u)");
420 AIM_CHECK(bmAttribute, ep->desc.bmAttributes & 0x03, "(%u vs %u)");
421#undef AIM_CHECK
422 return result;
423}
424
425#ifndef CONFIG_BUG
426#define CONFIG_BUG 0
427#endif
428
429
430
431
432
433
434
435
436
437int rpipe_get_by_ep(struct wahc *wa, struct usb_host_endpoint *ep,
438 struct urb *urb, gfp_t gfp)
439{
440 int result = 0;
441 struct device *dev = &wa->usb_iface->dev;
442 struct wa_rpipe *rpipe;
443 u8 eptype;
444
445 mutex_lock(&wa->rpipe_mutex);
446 rpipe = ep->hcpriv;
447 if (rpipe != NULL) {
448 if (CONFIG_BUG == 1) {
449 result = rpipe_check_aim(rpipe, wa, ep, urb, gfp);
450 if (result < 0)
451 goto error;
452 }
453 __rpipe_get(rpipe);
454 dev_dbg(dev, "ep 0x%02x: reusing rpipe %u\n",
455 ep->desc.bEndpointAddress,
456 le16_to_cpu(rpipe->descr.wRPipeIndex));
457 } else {
458
459 result = -ENOBUFS;
460 eptype = ep->desc.bmAttributes & 0x03;
461 result = rpipe_get_idle(&rpipe, wa, 1 << eptype, gfp);
462 if (result < 0)
463 goto error;
464 result = rpipe_aim(rpipe, wa, ep, urb, gfp);
465 if (result < 0) {
466 rpipe_put(rpipe);
467 goto error;
468 }
469 ep->hcpriv = rpipe;
470 rpipe->ep = ep;
471 __rpipe_get(rpipe);
472 dev_dbg(dev, "ep 0x%02x: using rpipe %u\n",
473 ep->desc.bEndpointAddress,
474 le16_to_cpu(rpipe->descr.wRPipeIndex));
475 }
476error:
477 mutex_unlock(&wa->rpipe_mutex);
478 return result;
479}
480
481
482
483
484int wa_rpipes_create(struct wahc *wa)
485{
486 wa->rpipes = le16_to_cpu(wa->wa_descr->wNumRPipes);
487 wa->rpipe_bm = kzalloc(BITS_TO_LONGS(wa->rpipes)*sizeof(unsigned long),
488 GFP_KERNEL);
489 if (wa->rpipe_bm == NULL)
490 return -ENOMEM;
491 return 0;
492}
493
494void wa_rpipes_destroy(struct wahc *wa)
495{
496 struct device *dev = &wa->usb_iface->dev;
497
498 if (!bitmap_empty(wa->rpipe_bm, wa->rpipes)) {
499 WARN_ON(1);
500 dev_err(dev, "BUG: pipes not released on exit: %*pb\n",
501 wa->rpipes, wa->rpipe_bm);
502 }
503 kfree(wa->rpipe_bm);
504}
505
506
507
508
509
510
511
512
513
514
515
516void rpipe_ep_disable(struct wahc *wa, struct usb_host_endpoint *ep)
517{
518 struct wa_rpipe *rpipe;
519
520 mutex_lock(&wa->rpipe_mutex);
521 rpipe = ep->hcpriv;
522 if (rpipe != NULL) {
523 u16 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
524
525 usb_control_msg(
526 wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
527 USB_REQ_RPIPE_ABORT,
528 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
529 0, index, NULL, 0, USB_CTRL_SET_TIMEOUT);
530 rpipe_put(rpipe);
531 }
532 mutex_unlock(&wa->rpipe_mutex);
533}
534EXPORT_SYMBOL_GPL(rpipe_ep_disable);
535
536
537void rpipe_clear_feature_stalled(struct wahc *wa, struct usb_host_endpoint *ep)
538{
539 struct wa_rpipe *rpipe;
540
541 mutex_lock(&wa->rpipe_mutex);
542 rpipe = ep->hcpriv;
543 if (rpipe != NULL) {
544 u16 index = le16_to_cpu(rpipe->descr.wRPipeIndex);
545
546 usb_control_msg(
547 wa->usb_dev, usb_sndctrlpipe(wa->usb_dev, 0),
548 USB_REQ_CLEAR_FEATURE,
549 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_RPIPE,
550 RPIPE_STALL, index, NULL, 0, USB_CTRL_SET_TIMEOUT);
551 }
552 mutex_unlock(&wa->rpipe_mutex);
553}
554EXPORT_SYMBOL_GPL(rpipe_clear_feature_stalled);
555