blob: 596a0b464e61f069f93c8278a3c8193ab44a7873 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
3 *
4 * Copyright (C) 2004 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/types.h>
15#include <linux/device.h>
16
17#include <linux/ctype.h>
18#include <linux/string.h>
19
David Brownell5f848132006-12-16 15:34:53 -080020#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070021#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "gadget_chips.h"
24
25
26/* we must assign addresses for configurable endpoints (like net2280) */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020027static unsigned epnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29// #define MANY_ENDPOINTS
30#ifdef MANY_ENDPOINTS
31/* more than 15 configurable endpoints */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020032static unsigned in_epnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#endif
34
35
36/*
37 * This should work with endpoints from controller drivers sharing the
38 * same endpoint naming convention. By example:
39 *
40 * - ep1, ep2, ... address is fixed, not direction or type
41 * - ep1in, ep2out, ... address and direction are fixed, not type
42 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
43 * - ep1in-bulk, ep2out-iso, ... all three are fixed
44 * - ep-* ... no functionality restrictions
45 *
46 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
47 * Less common restrictions are implied by gadget_is_*().
48 *
49 * NOTE: each endpoint is unidirectional, as specified by its USB
50 * descriptor; and isn't specific to a configuration or altsetting.
51 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020052static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070053ep_matches (
54 struct usb_gadget *gadget,
55 struct usb_ep *ep,
Tatyana Brokhmana59d6b92011-06-28 16:33:53 +030056 struct usb_endpoint_descriptor *desc,
57 struct usb_ss_ep_comp_descriptor *ep_comp
Linus Torvalds1da177e2005-04-16 15:20:36 -070058)
59{
60 u8 type;
61 const char *tmp;
62 u16 max;
63
Tatyana Brokhmana59d6b92011-06-28 16:33:53 +030064 int num_req_streams = 0;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /* endpoint already claimed? */
David Brownella9475222007-07-30 12:31:07 -070067 if (NULL != ep->driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return 0;
David Brownella3536782006-07-06 15:48:53 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* only support ep0 for portable CONTROL traffic */
71 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
72 if (USB_ENDPOINT_XFER_CONTROL == type)
73 return 0;
74
75 /* some other naming convention */
76 if ('e' != ep->name[0])
77 return 0;
78
79 /* type-restriction: "-iso", "-bulk", or "-int".
80 * direction-restriction: "in", "out".
81 */
82 if ('-' != ep->name[2]) {
83 tmp = strrchr (ep->name, '-');
84 if (tmp) {
85 switch (type) {
86 case USB_ENDPOINT_XFER_INT:
87 /* bulk endpoints handle interrupt transfers,
88 * except the toggle-quirky iso-synch kind
89 */
90 if ('s' == tmp[2]) // == "-iso"
91 return 0;
92 /* for now, avoid PXA "interrupt-in";
93 * it's documented as never using DATA1.
94 */
95 if (gadget_is_pxa (gadget)
96 && 'i' == tmp [1])
97 return 0;
98 break;
99 case USB_ENDPOINT_XFER_BULK:
100 if ('b' != tmp[1]) // != "-bulk"
101 return 0;
102 break;
103 case USB_ENDPOINT_XFER_ISOC:
104 if ('s' != tmp[2]) // != "-iso"
105 return 0;
106 }
107 } else {
108 tmp = ep->name + strlen (ep->name);
109 }
110
111 /* direction-restriction: "..in-..", "out-.." */
112 tmp--;
113 if (!isdigit (*tmp)) {
114 if (desc->bEndpointAddress & USB_DIR_IN) {
115 if ('n' != *tmp)
116 return 0;
117 } else {
118 if ('t' != *tmp)
119 return 0;
120 }
121 }
122 }
123
Jassi Brar553fbcd2011-01-14 11:55:53 +0900124 /*
Tatyana Brokhmana59d6b92011-06-28 16:33:53 +0300125 * Get the number of required streams from the EP companion
126 * descriptor and see if the EP matches it
127 */
128 if (usb_endpoint_xfer_bulk(desc)) {
129 if (ep_comp) {
130 num_req_streams = ep_comp->bmAttributes & 0x1f;
131 if (num_req_streams > ep->max_streams)
132 return 0;
133 /* Update the ep_comp descriptor if needed */
134 if (num_req_streams != ep->max_streams)
135 ep_comp->bmAttributes = ep->max_streams;
136 }
137
138 }
139
140 /*
Jassi Brar553fbcd2011-01-14 11:55:53 +0900141 * If the protocol driver hasn't yet decided on wMaxPacketSize
142 * and wants to know the maximum possible, provide the info.
143 */
144 if (desc->wMaxPacketSize == 0)
145 desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* endpoint maxpacket size is an input parameter, except for bulk
148 * where it's an output parameter representing the full speed limit.
149 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
150 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700151 max = 0x7ff & usb_endpoint_maxp(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 switch (type) {
153 case USB_ENDPOINT_XFER_INT:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300154 /* INT: limit 64 bytes full speed, 1024 high/super speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!gadget->is_dualspeed && max > 64)
156 return 0;
157 /* FALLTHROUGH */
158
159 case USB_ENDPOINT_XFER_ISOC:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300160 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (ep->maxpacket < max)
162 return 0;
163 if (!gadget->is_dualspeed && max > 1023)
164 return 0;
165
166 /* BOTH: "high bandwidth" works only at high speed */
Harvey Harrison551509d2009-02-11 14:11:36 -0800167 if ((desc->wMaxPacketSize & cpu_to_le16(3<<11))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (!gadget->is_dualspeed)
169 return 0;
170 /* configure your hardware with enough buffering!! */
171 }
172 break;
173 }
174
175 /* MATCH!! */
176
177 /* report address */
David Brownella4c39c42008-06-19 17:52:25 -0700178 desc->bEndpointAddress &= USB_DIR_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (isdigit (ep->name [2])) {
Julia Lawallbb9496c2008-11-25 14:15:19 +0100180 u8 num = simple_strtoul (&ep->name [2], NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 desc->bEndpointAddress |= num;
182#ifdef MANY_ENDPOINTS
183 } else if (desc->bEndpointAddress & USB_DIR_IN) {
184 if (++in_epnum > 15)
185 return 0;
186 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
187#endif
188 } else {
189 if (++epnum > 15)
190 return 0;
191 desc->bEndpointAddress |= epnum;
192 }
193
194 /* report (variable) full speed bulk maxpacket */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300195 if ((USB_ENDPOINT_XFER_BULK == type) && !ep_comp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int size = ep->maxpacket;
197
198 /* min() doesn't work on bitfields with gcc-3.5 */
199 if (size > 64)
200 size = 64;
201 desc->wMaxPacketSize = cpu_to_le16(size);
202 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300203 ep->address = desc->bEndpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return 1;
205}
206
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200207static struct usb_ep *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208find_ep (struct usb_gadget *gadget, const char *name)
209{
210 struct usb_ep *ep;
211
212 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
213 if (0 == strcmp (ep->name, name))
214 return ep;
215 }
216 return NULL;
217}
218
219/**
Tatyana Brokhmana59d6b92011-06-28 16:33:53 +0300220 * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
221 * descriptor and ep companion descriptor
222 * @gadget: The device to which the endpoint must belong.
223 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
224 * initialized. For periodic transfers, the maximum packet
225 * size must also be initialized. This is modified on
226 * success.
227 * @ep_comp: Endpoint companion descriptor, with the required
228 * number of streams. Will be modified when the chosen EP
229 * supports a different number of streams.
230 *
231 * This routine replaces the usb_ep_autoconfig when needed
232 * superspeed enhancments. If such enhancemnets are required,
233 * the FD should call usb_ep_autoconfig_ss directly and provide
234 * the additional ep_comp parameter.
235 *
236 * By choosing an endpoint to use with the specified descriptor,
237 * this routine simplifies writing gadget drivers that work with
238 * multiple USB device controllers. The endpoint would be
239 * passed later to usb_ep_enable(), along with some descriptor.
240 *
241 * That second descriptor won't always be the same as the first one.
242 * For example, isochronous endpoints can be autoconfigured for high
243 * bandwidth, and then used in several lower bandwidth altsettings.
244 * Also, high and full speed descriptors will be different.
245 *
246 * Be sure to examine and test the results of autoconfiguration
247 * on your hardware. This code may not make the best choices
248 * about how to use the USB controller, and it can't know all
249 * the restrictions that may apply. Some combinations of driver
250 * and hardware won't be able to autoconfigure.
251 *
252 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
253 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
254 * is initialized as if the endpoint were used at full speed and
255 * the bmAttribute field in the ep companion descriptor is
256 * updated with the assigned number of streams if it is
257 * different from the original value. To prevent the endpoint
258 * from being returned by a later autoconfig call, claim it by
259 * assigning ep->driver_data to some non-null value.
260 *
261 * On failure, this returns a null endpoint descriptor.
262 */
263struct usb_ep *usb_ep_autoconfig_ss(
264 struct usb_gadget *gadget,
265 struct usb_endpoint_descriptor *desc,
266 struct usb_ss_ep_comp_descriptor *ep_comp
267)
268{
269 struct usb_ep *ep;
270 u8 type;
271
272 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
273
274 /* First, apply chip-specific "best usage" knowledge.
275 * This might make a good usb_gadget_ops hook ...
276 */
277 if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) {
278 /* ep-e, ep-f are PIO with only 64 byte fifos */
279 ep = find_ep (gadget, "ep-e");
280 if (ep && ep_matches(gadget, ep, desc, ep_comp))
281 return ep;
282 ep = find_ep (gadget, "ep-f");
283 if (ep && ep_matches(gadget, ep, desc, ep_comp))
284 return ep;
285
286 } else if (gadget_is_goku (gadget)) {
287 if (USB_ENDPOINT_XFER_INT == type) {
288 /* single buffering is enough */
289 ep = find_ep(gadget, "ep3-bulk");
290 if (ep && ep_matches(gadget, ep, desc, ep_comp))
291 return ep;
292 } else if (USB_ENDPOINT_XFER_BULK == type
293 && (USB_DIR_IN & desc->bEndpointAddress)) {
294 /* DMA may be available */
295 ep = find_ep(gadget, "ep2-bulk");
296 if (ep && ep_matches(gadget, ep, desc,
297 ep_comp))
298 return ep;
299 }
300
301#ifdef CONFIG_BLACKFIN
302 } else if (gadget_is_musbhdrc(gadget)) {
303 if ((USB_ENDPOINT_XFER_BULK == type) ||
304 (USB_ENDPOINT_XFER_ISOC == type)) {
305 if (USB_DIR_IN & desc->bEndpointAddress)
306 ep = find_ep (gadget, "ep5in");
307 else
308 ep = find_ep (gadget, "ep6out");
309 } else if (USB_ENDPOINT_XFER_INT == type) {
310 if (USB_DIR_IN & desc->bEndpointAddress)
311 ep = find_ep(gadget, "ep1in");
312 else
313 ep = find_ep(gadget, "ep2out");
314 } else
315 ep = NULL;
316 if (ep && ep_matches(gadget, ep, desc, ep_comp))
317 return ep;
318#endif
319 }
320
321 /* Second, look at endpoints until an unclaimed one looks usable */
322 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
323 if (ep_matches(gadget, ep, desc, ep_comp))
324 return ep;
325 }
326
327 /* Fail */
328 return NULL;
329}
330
331/**
332 * usb_ep_autoconfig() - choose an endpoint matching the
333 * descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * @gadget: The device to which the endpoint must belong.
335 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
336 * initialized. For periodic transfers, the maximum packet
337 * size must also be initialized. This is modified on success.
338 *
339 * By choosing an endpoint to use with the specified descriptor, this
340 * routine simplifies writing gadget drivers that work with multiple
341 * USB device controllers. The endpoint would be passed later to
342 * usb_ep_enable(), along with some descriptor.
343 *
344 * That second descriptor won't always be the same as the first one.
345 * For example, isochronous endpoints can be autoconfigured for high
346 * bandwidth, and then used in several lower bandwidth altsettings.
347 * Also, high and full speed descriptors will be different.
348 *
349 * Be sure to examine and test the results of autoconfiguration on your
350 * hardware. This code may not make the best choices about how to use the
351 * USB controller, and it can't know all the restrictions that may apply.
352 * Some combinations of driver and hardware won't be able to autoconfigure.
353 *
354 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
355 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
356 * is initialized as if the endpoint were used at full speed. To prevent
357 * the endpoint from being returned by a later autoconfig call, claim it
358 * by assigning ep->driver_data to some non-null value.
359 *
360 * On failure, this returns a null endpoint descriptor.
361 */
Tatyana Brokhmana59d6b92011-06-28 16:33:53 +0300362struct usb_ep *usb_ep_autoconfig(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct usb_gadget *gadget,
364 struct usb_endpoint_descriptor *desc
365)
366{
Tatyana Brokhmana59d6b92011-06-28 16:33:53 +0300367 return usb_ep_autoconfig_ss(gadget, desc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Tatyana Brokhmana59d6b92011-06-28 16:33:53 +0300370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/**
372 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
373 * @gadget: device for which autoconfig state will be reset
374 *
375 * Use this for devices where one configuration may need to assign
376 * endpoint resources very differently from the next one. It clears
377 * state such as ep->driver_data and the record of assigned endpoints
378 * used by usb_ep_autoconfig().
379 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200380void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct usb_ep *ep;
383
384 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
385 ep->driver_data = NULL;
386 }
387#ifdef MANY_ENDPOINTS
388 in_epnum = 0;
389#endif
390 epnum = 0;
391}
392