2 #include <linux/init.h>
3 #include <asm/atomic.h>
4 #include <linux/bitmap.h>
5 #include <linux/slab.h>
10 static int __rpipe_get_descr(struct wahc
*wa
,
11 struct usb_rpipe_descriptor
*descr
, u16 index
)
14 struct device
*dev
= &wa
->usb_iface
->dev
;
16 /* Get the RPIPE descriptor -- we cannot use the usb_get_descriptor()
17 * function because the arguments are different.
19 result
= usb_control_msg(
20 wa
->usb_dev
, usb_rcvctrlpipe(wa
->usb_dev
, 0),
21 USB_REQ_GET_DESCRIPTOR
,
22 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_RPIPE
,
23 USB_DT_RPIPE
<<8, index
, descr
, sizeof(*descr
),
26 dev_err(dev
, "rpipe %u: get descriptor failed: %d\n",
30 if (result
< sizeof(*descr
)) {
31 dev_err(dev
, "rpipe %u: got short descriptor "
32 "(%zd vs %zd bytes needed)\n",
33 index
, result
, sizeof(*descr
));
45 * The descriptor is assumed to be properly initialized (ie: you got
46 * it through __rpipe_get_descr()).
48 static int __rpipe_set_descr(struct wahc
*wa
,
49 struct usb_rpipe_descriptor
*descr
, u16 index
)
52 struct device
*dev
= &wa
->usb_iface
->dev
;
54 /* we cannot use the usb_get_descriptor() function because the
55 * arguments are different.
57 result
= usb_control_msg(
58 wa
->usb_dev
, usb_sndctrlpipe(wa
->usb_dev
, 0),
59 USB_REQ_SET_DESCRIPTOR
,
60 USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_RPIPE
,
61 USB_DT_RPIPE
<<8, index
, descr
, sizeof(*descr
),
64 dev_err(dev
, "rpipe %u: set descriptor failed: %d\n",
68 if (result
< sizeof(*descr
)) {
69 dev_err(dev
, "rpipe %u: sent short descriptor "
70 "(%zd vs %zd bytes required)\n",
71 index
, result
, sizeof(*descr
));
82 static void rpipe_init(struct wa_rpipe
*rpipe
)
84 kref_init(&rpipe
->refcnt
);
85 spin_lock_init(&rpipe
->seg_lock
);
86 INIT_LIST_HEAD(&rpipe
->seg_list
);
89 static unsigned rpipe_get_idx(struct wahc
*wa
, unsigned rpipe_idx
)
93 spin_lock_irqsave(&wa
->rpipe_bm_lock
, flags
);
94 rpipe_idx
= find_next_zero_bit(wa
->rpipe_bm
, wa
->rpipes
, rpipe_idx
);
95 if (rpipe_idx
< wa
->rpipes
)
96 set_bit(rpipe_idx
, wa
->rpipe_bm
);
97 spin_unlock_irqrestore(&wa
->rpipe_bm_lock
, flags
);
102 static void rpipe_put_idx(struct wahc
*wa
, unsigned rpipe_idx
)
106 spin_lock_irqsave(&wa
->rpipe_bm_lock
, flags
);
107 clear_bit(rpipe_idx
, wa
->rpipe_bm
);
108 spin_unlock_irqrestore(&wa
->rpipe_bm_lock
, flags
);
111 void rpipe_destroy(struct kref
*_rpipe
)
113 struct wa_rpipe
*rpipe
= container_of(_rpipe
, struct wa_rpipe
, refcnt
);
114 u8 index
= le16_to_cpu(rpipe
->descr
.wRPipeIndex
);
117 rpipe
->ep
->hcpriv
= NULL
;
118 rpipe_put_idx(rpipe
->wa
, index
);
122 EXPORT_SYMBOL_GPL(rpipe_destroy
);
125 * Locate an idle rpipe, create an structure for it and return it
127 * @wa is referenced and unlocked
128 * @crs enum rpipe_attr, required endpoint characteristics
130 * The rpipe can be used only sequentially (not in parallel).
132 * The rpipe is moved into the "ready" state.
134 static int rpipe_get_idle(struct wa_rpipe
**prpipe
, struct wahc
*wa
, u8 crs
,
139 struct wa_rpipe
*rpipe
;
140 struct device
*dev
= &wa
->usb_iface
->dev
;
142 rpipe
= kzalloc(sizeof(*rpipe
), gfp
);
147 /* Look for an idle pipe */
148 for (rpipe_idx
= 0; rpipe_idx
< wa
->rpipes
; rpipe_idx
++) {
149 rpipe_idx
= rpipe_get_idx(wa
, rpipe_idx
);
150 if (rpipe_idx
>= wa
->rpipes
) /* no more pipes :( */
152 result
= __rpipe_get_descr(wa
, &rpipe
->descr
, rpipe_idx
);
154 dev_err(dev
, "Can't get descriptor for rpipe %u: %d\n",
156 else if ((rpipe
->descr
.bmCharacteristics
& crs
) != 0)
158 rpipe_put_idx(wa
, rpipe_idx
);
165 set_bit(rpipe_idx
, wa
->rpipe_bm
);
166 rpipe
->wa
= wa_get(wa
);
171 static int __rpipe_reset(struct wahc
*wa
, unsigned index
)
174 struct device
*dev
= &wa
->usb_iface
->dev
;
176 result
= usb_control_msg(
177 wa
->usb_dev
, usb_sndctrlpipe(wa
->usb_dev
, 0),
179 USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_RPIPE
,
180 0, index
, NULL
, 0, 1000);
182 dev_err(dev
, "rpipe %u: reset failed: %d\n",
188 * Fake companion descriptor for ep0
190 * See WUSB1.0[7.4.4], most of this is zero for bulk/int/ctl
192 static struct usb_wireless_ep_comp_descriptor epc0
= {
193 .bLength
= sizeof(epc0
),
194 .bDescriptorType
= USB_DT_WIRELESS_ENDPOINT_COMP
,
195 /* .bMaxBurst = 1, */
200 * Look for EP companion descriptor
202 * Get there, look for Inara in the endpoint's extra descriptors
204 static struct usb_wireless_ep_comp_descriptor
*rpipe_epc_find(
205 struct device
*dev
, struct usb_host_endpoint
*ep
)
209 struct usb_descriptor_header
*hdr
;
210 struct usb_wireless_ep_comp_descriptor
*epcd
;
212 if (ep
->desc
.bEndpointAddress
== 0) {
217 itr_size
= ep
->extralen
;
219 while (itr_size
> 0) {
220 if (itr_size
< sizeof(*hdr
)) {
221 dev_err(dev
, "HW Bug? ep 0x%02x: extra descriptors "
222 "at offset %zu: only %zu bytes left\n",
223 ep
->desc
.bEndpointAddress
,
224 itr
- (void *) ep
->extra
, itr_size
);
228 if (hdr
->bDescriptorType
== USB_DT_WIRELESS_ENDPOINT_COMP
) {
232 if (hdr
->bLength
> itr_size
) {
233 dev_err(dev
, "HW Bug? ep 0x%02x: extra descriptor "
234 "at offset %zu (type 0x%02x) "
235 "length %d but only %zu bytes left\n",
236 ep
->desc
.bEndpointAddress
,
237 itr
- (void *) ep
->extra
, hdr
->bDescriptorType
,
238 hdr
->bLength
, itr_size
);
242 itr_size
-= hdr
->bDescriptorType
;
249 * Aim an rpipe to its device & endpoint destination
251 * Make sure we change the address to unauthenticathed if the device
252 * is WUSB and it is not authenticated.
254 static int rpipe_aim(struct wa_rpipe
*rpipe
, struct wahc
*wa
,
255 struct usb_host_endpoint
*ep
, struct urb
*urb
, gfp_t gfp
)
257 int result
= -ENOMSG
; /* better code for lack of companion? */
258 struct device
*dev
= &wa
->usb_iface
->dev
;
259 struct usb_device
*usb_dev
= urb
->dev
;
260 struct usb_wireless_ep_comp_descriptor
*epcd
;
263 epcd
= rpipe_epc_find(dev
, ep
);
265 dev_err(dev
, "ep 0x%02x: can't find companion descriptor\n",
266 ep
->desc
.bEndpointAddress
);
269 unauth
= usb_dev
->wusb
&& !usb_dev
->authenticated
? 0x80 : 0;
270 __rpipe_reset(wa
, le16_to_cpu(rpipe
->descr
.wRPipeIndex
));
271 atomic_set(&rpipe
->segs_available
, le16_to_cpu(rpipe
->descr
.wRequests
));
272 rpipe
->descr
.wBlocks
= cpu_to_le16(16); /* given */
273 /* ep0 maxpktsize is 0x200 (WUSB1.0[4.8.1]) */
274 rpipe
->descr
.wMaxPacketSize
= cpu_to_le16(ep
->desc
.wMaxPacketSize
);
275 rpipe
->descr
.bHSHubAddress
= 0; /* reserved: zero */
276 rpipe
->descr
.bHSHubPort
= wusb_port_no_to_idx(urb
->dev
->portnum
);
277 rpipe
->descr
.bSpeed
= usb_pipeendpoint(urb
->pipe
) == 0 ?
278 UWB_PHY_RATE_53
: UWB_PHY_RATE_200
;
280 dev_dbg(dev
, "addr %u (0x%02x) rpipe #%u ep# %u speed %d\n",
281 urb
->dev
->devnum
, urb
->dev
->devnum
| unauth
,
282 le16_to_cpu(rpipe
->descr
.wRPipeIndex
),
283 usb_pipeendpoint(urb
->pipe
), rpipe
->descr
.bSpeed
);
285 /* see security.c:wusb_update_address() */
286 if (unlikely(urb
->dev
->devnum
== 0x80))
287 rpipe
->descr
.bDeviceAddress
= 0;
289 rpipe
->descr
.bDeviceAddress
= urb
->dev
->devnum
| unauth
;
290 rpipe
->descr
.bEndpointAddress
= ep
->desc
.bEndpointAddress
;
291 rpipe
->descr
.bDataSequence
= 0;
292 rpipe
->descr
.dwCurrentWindow
= cpu_to_le32(1);
293 rpipe
->descr
.bMaxDataSequence
= epcd
->bMaxSequence
- 1;
294 rpipe
->descr
.bInterval
= ep
->desc
.bInterval
;
295 rpipe
->descr
.bOverTheAirInterval
= 0; /* 0 if not isoc */
296 rpipe
->descr
.bmAttribute
= ep
->desc
.bmAttributes
& 0x03;
297 /* rpipe->descr.bmCharacteristics RO */
298 rpipe
->descr
.bmRetryOptions
= 15;
299 rpipe
->descr
.wNumTransactionErrors
= 0;
300 result
= __rpipe_set_descr(wa
, &rpipe
->descr
,
301 le16_to_cpu(rpipe
->descr
.wRPipeIndex
));
303 dev_err(dev
, "Cannot aim rpipe: %d\n", result
);
312 * Check an aimed rpipe to make sure it points to where we want
314 * We use bit 19 of the Linux USB pipe bitmap for unauth vs auth
315 * space; when it is like that, we or 0x80 to make an unauth address.
317 static int rpipe_check_aim(const struct wa_rpipe
*rpipe
, const struct wahc
*wa
,
318 const struct usb_host_endpoint
*ep
,
319 const struct urb
*urb
, gfp_t gfp
)
321 int result
= 0; /* better code for lack of companion? */
322 struct device
*dev
= &wa
->usb_iface
->dev
;
323 struct usb_device
*usb_dev
= urb
->dev
;
324 u8 unauth
= (usb_dev
->wusb
&& !usb_dev
->authenticated
) ? 0x80 : 0;
325 u8 portnum
= wusb_port_no_to_idx(urb
->dev
->portnum
);
327 #define AIM_CHECK(rdf, val, text) \
329 if (rpipe->descr.rdf != (val)) { \
331 "rpipe aim discrepancy: " #rdf " " text "\n", \
332 rpipe->descr.rdf, (val)); \
337 AIM_CHECK(wMaxPacketSize
, cpu_to_le16(ep
->desc
.wMaxPacketSize
),
339 AIM_CHECK(bHSHubPort
, portnum
, "(%u vs %u)");
340 AIM_CHECK(bSpeed
, usb_pipeendpoint(urb
->pipe
) == 0 ?
341 UWB_PHY_RATE_53
: UWB_PHY_RATE_200
,
343 AIM_CHECK(bDeviceAddress
, urb
->dev
->devnum
| unauth
, "(%u vs %u)");
344 AIM_CHECK(bEndpointAddress
, ep
->desc
.bEndpointAddress
, "(%u vs %u)");
345 AIM_CHECK(bInterval
, ep
->desc
.bInterval
, "(%u vs %u)");
346 AIM_CHECK(bmAttribute
, ep
->desc
.bmAttributes
& 0x03, "(%u vs %u)");
356 * Make sure there is an rpipe allocated for an endpoint
358 * If already allocated, we just refcount it; if not, we get an
359 * idle one, aim it to the right location and take it.
361 * Attaches to ep->hcpriv and rpipe->ep to ep.
363 int rpipe_get_by_ep(struct wahc
*wa
, struct usb_host_endpoint
*ep
,
364 struct urb
*urb
, gfp_t gfp
)
367 struct device
*dev
= &wa
->usb_iface
->dev
;
368 struct wa_rpipe
*rpipe
;
371 mutex_lock(&wa
->rpipe_mutex
);
374 if (CONFIG_BUG
== 1) {
375 result
= rpipe_check_aim(rpipe
, wa
, ep
, urb
, gfp
);
380 dev_dbg(dev
, "ep 0x%02x: reusing rpipe %u\n",
381 ep
->desc
.bEndpointAddress
,
382 le16_to_cpu(rpipe
->descr
.wRPipeIndex
));
384 /* hmm, assign idle rpipe, aim it */
386 eptype
= ep
->desc
.bmAttributes
& 0x03;
387 result
= rpipe_get_idle(&rpipe
, wa
, 1 << eptype
, gfp
);
390 result
= rpipe_aim(rpipe
, wa
, ep
, urb
, gfp
);
397 __rpipe_get(rpipe
); /* for caching into ep->hcpriv */
398 dev_dbg(dev
, "ep 0x%02x: using rpipe %u\n",
399 ep
->desc
.bEndpointAddress
,
400 le16_to_cpu(rpipe
->descr
.wRPipeIndex
));
403 mutex_unlock(&wa
->rpipe_mutex
);
408 * Allocate the bitmap for each rpipe.
410 int wa_rpipes_create(struct wahc
*wa
)
412 wa
->rpipes
= wa
->wa_descr
->wNumRPipes
;
413 wa
->rpipe_bm
= kzalloc(BITS_TO_LONGS(wa
->rpipes
)*sizeof(unsigned long),
415 if (wa
->rpipe_bm
== NULL
)
420 void wa_rpipes_destroy(struct wahc
*wa
)
422 struct device
*dev
= &wa
->usb_iface
->dev
;
424 if (!bitmap_empty(wa
->rpipe_bm
, wa
->rpipes
)) {
427 bitmap_scnprintf(buf
, sizeof(buf
), wa
->rpipe_bm
, wa
->rpipes
);
428 dev_err(dev
, "BUG: pipes not released on exit: %s\n", buf
);
434 * Release resources allocated for an endpoint
436 * If there is an associated rpipe to this endpoint, Abort any pending
437 * transfers and put it. If the rpipe ends up being destroyed,
438 * __rpipe_destroy() will cleanup ep->hcpriv.
440 * This is called before calling hcd->stop(), so you don't need to do
441 * anything else in there.
443 void rpipe_ep_disable(struct wahc
*wa
, struct usb_host_endpoint
*ep
)
445 struct wa_rpipe
*rpipe
;
447 mutex_lock(&wa
->rpipe_mutex
);
450 u16 index
= le16_to_cpu(rpipe
->descr
.wRPipeIndex
);
453 wa
->usb_dev
, usb_rcvctrlpipe(wa
->usb_dev
, 0),
455 USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_RPIPE
,
456 0, index
, NULL
, 0, 1000);
459 mutex_unlock(&wa
->rpipe_mutex
);
461 EXPORT_SYMBOL_GPL(rpipe_ep_disable
);