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