blob: 9b7360ff5aa70c2992256cda8b1533fa76d7f4b9 [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.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/types.h>
25#include <linux/device.h>
26
27#include <linux/ctype.h>
28#include <linux/string.h>
29
David Brownell5f848132006-12-16 15:34:53 -080030#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070031#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include "gadget_chips.h"
34
35
36/* we must assign addresses for configurable endpoints (like net2280) */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020037static unsigned epnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39// #define MANY_ENDPOINTS
40#ifdef MANY_ENDPOINTS
41/* more than 15 configurable endpoints */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020042static unsigned in_epnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#endif
44
45
46/*
47 * This should work with endpoints from controller drivers sharing the
48 * same endpoint naming convention. By example:
49 *
50 * - ep1, ep2, ... address is fixed, not direction or type
51 * - ep1in, ep2out, ... address and direction are fixed, not type
52 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
53 * - ep1in-bulk, ep2out-iso, ... all three are fixed
54 * - ep-* ... no functionality restrictions
55 *
56 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
57 * Less common restrictions are implied by gadget_is_*().
58 *
59 * NOTE: each endpoint is unidirectional, as specified by its USB
60 * descriptor; and isn't specific to a configuration or altsetting.
61 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020062static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070063ep_matches (
64 struct usb_gadget *gadget,
65 struct usb_ep *ep,
66 struct usb_endpoint_descriptor *desc
67)
68{
69 u8 type;
70 const char *tmp;
71 u16 max;
72
73 /* endpoint already claimed? */
David Brownella9475222007-07-30 12:31:07 -070074 if (NULL != ep->driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return 0;
David Brownella3536782006-07-06 15:48:53 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* only support ep0 for portable CONTROL traffic */
78 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
79 if (USB_ENDPOINT_XFER_CONTROL == type)
80 return 0;
81
82 /* some other naming convention */
83 if ('e' != ep->name[0])
84 return 0;
85
86 /* type-restriction: "-iso", "-bulk", or "-int".
87 * direction-restriction: "in", "out".
88 */
89 if ('-' != ep->name[2]) {
90 tmp = strrchr (ep->name, '-');
91 if (tmp) {
92 switch (type) {
93 case USB_ENDPOINT_XFER_INT:
94 /* bulk endpoints handle interrupt transfers,
95 * except the toggle-quirky iso-synch kind
96 */
97 if ('s' == tmp[2]) // == "-iso"
98 return 0;
99 /* for now, avoid PXA "interrupt-in";
100 * it's documented as never using DATA1.
101 */
102 if (gadget_is_pxa (gadget)
103 && 'i' == tmp [1])
104 return 0;
105 break;
106 case USB_ENDPOINT_XFER_BULK:
107 if ('b' != tmp[1]) // != "-bulk"
108 return 0;
109 break;
110 case USB_ENDPOINT_XFER_ISOC:
111 if ('s' != tmp[2]) // != "-iso"
112 return 0;
113 }
114 } else {
115 tmp = ep->name + strlen (ep->name);
116 }
117
118 /* direction-restriction: "..in-..", "out-.." */
119 tmp--;
120 if (!isdigit (*tmp)) {
121 if (desc->bEndpointAddress & USB_DIR_IN) {
122 if ('n' != *tmp)
123 return 0;
124 } else {
125 if ('t' != *tmp)
126 return 0;
127 }
128 }
129 }
130
Jassi Brar553fbcd2011-01-14 11:55:53 +0900131 /*
132 * If the protocol driver hasn't yet decided on wMaxPacketSize
133 * and wants to know the maximum possible, provide the info.
134 */
135 if (desc->wMaxPacketSize == 0)
136 desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* endpoint maxpacket size is an input parameter, except for bulk
139 * where it's an output parameter representing the full speed limit.
140 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
141 */
David Brownell01ee7d72007-05-25 20:40:14 -0700142 max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 switch (type) {
144 case USB_ENDPOINT_XFER_INT:
145 /* INT: limit 64 bytes full speed, 1024 high speed */
146 if (!gadget->is_dualspeed && max > 64)
147 return 0;
148 /* FALLTHROUGH */
149
150 case USB_ENDPOINT_XFER_ISOC:
151 /* ISO: limit 1023 bytes full speed, 1024 high speed */
152 if (ep->maxpacket < max)
153 return 0;
154 if (!gadget->is_dualspeed && max > 1023)
155 return 0;
156
157 /* BOTH: "high bandwidth" works only at high speed */
Harvey Harrison551509d2009-02-11 14:11:36 -0800158 if ((desc->wMaxPacketSize & cpu_to_le16(3<<11))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (!gadget->is_dualspeed)
160 return 0;
161 /* configure your hardware with enough buffering!! */
162 }
163 break;
164 }
165
166 /* MATCH!! */
167
168 /* report address */
David Brownella4c39c42008-06-19 17:52:25 -0700169 desc->bEndpointAddress &= USB_DIR_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (isdigit (ep->name [2])) {
Julia Lawallbb9496c2008-11-25 14:15:19 +0100171 u8 num = simple_strtoul (&ep->name [2], NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 desc->bEndpointAddress |= num;
173#ifdef MANY_ENDPOINTS
174 } else if (desc->bEndpointAddress & USB_DIR_IN) {
175 if (++in_epnum > 15)
176 return 0;
177 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
178#endif
179 } else {
180 if (++epnum > 15)
181 return 0;
182 desc->bEndpointAddress |= epnum;
183 }
184
185 /* report (variable) full speed bulk maxpacket */
186 if (USB_ENDPOINT_XFER_BULK == type) {
187 int size = ep->maxpacket;
188
189 /* min() doesn't work on bitfields with gcc-3.5 */
190 if (size > 64)
191 size = 64;
192 desc->wMaxPacketSize = cpu_to_le16(size);
193 }
194 return 1;
195}
196
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200197static struct usb_ep *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198find_ep (struct usb_gadget *gadget, const char *name)
199{
200 struct usb_ep *ep;
201
202 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
203 if (0 == strcmp (ep->name, name))
204 return ep;
205 }
206 return NULL;
207}
208
209/**
210 * usb_ep_autoconfig - choose an endpoint matching the descriptor
211 * @gadget: The device to which the endpoint must belong.
212 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
213 * initialized. For periodic transfers, the maximum packet
214 * size must also be initialized. This is modified on success.
215 *
216 * By choosing an endpoint to use with the specified descriptor, this
217 * routine simplifies writing gadget drivers that work with multiple
218 * USB device controllers. The endpoint would be passed later to
219 * usb_ep_enable(), along with some descriptor.
220 *
221 * That second descriptor won't always be the same as the first one.
222 * For example, isochronous endpoints can be autoconfigured for high
223 * bandwidth, and then used in several lower bandwidth altsettings.
224 * Also, high and full speed descriptors will be different.
225 *
226 * Be sure to examine and test the results of autoconfiguration on your
227 * hardware. This code may not make the best choices about how to use the
228 * USB controller, and it can't know all the restrictions that may apply.
229 * Some combinations of driver and hardware won't be able to autoconfigure.
230 *
231 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
232 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
233 * is initialized as if the endpoint were used at full speed. To prevent
234 * the endpoint from being returned by a later autoconfig call, claim it
235 * by assigning ep->driver_data to some non-null value.
236 *
237 * On failure, this returns a null endpoint descriptor.
238 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200239struct usb_ep *usb_ep_autoconfig (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct usb_gadget *gadget,
241 struct usb_endpoint_descriptor *desc
242)
243{
244 struct usb_ep *ep;
245 u8 type;
246
247 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
248
249 /* First, apply chip-specific "best usage" knowledge.
250 * This might make a good usb_gadget_ops hook ...
251 */
252 if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) {
253 /* ep-e, ep-f are PIO with only 64 byte fifos */
254 ep = find_ep (gadget, "ep-e");
255 if (ep && ep_matches (gadget, ep, desc))
256 return ep;
257 ep = find_ep (gadget, "ep-f");
258 if (ep && ep_matches (gadget, ep, desc))
259 return ep;
260
261 } else if (gadget_is_goku (gadget)) {
262 if (USB_ENDPOINT_XFER_INT == type) {
263 /* single buffering is enough */
264 ep = find_ep (gadget, "ep3-bulk");
265 if (ep && ep_matches (gadget, ep, desc))
266 return ep;
267 } else if (USB_ENDPOINT_XFER_BULK == type
268 && (USB_DIR_IN & desc->bEndpointAddress)) {
269 /* DMA may be available */
270 ep = find_ep (gadget, "ep2-bulk");
271 if (ep && ep_matches (gadget, ep, desc))
272 return ep;
273 }
274
Bryan Wu3a8a3b12009-12-21 10:43:06 -0500275#ifdef CONFIG_BLACKFIN
Mike Frysingerf2984a32010-03-09 00:35:22 -0500276 } else if (gadget_is_musbhdrc(gadget)) {
Bryan Wu3a8a3b12009-12-21 10:43:06 -0500277 if ((USB_ENDPOINT_XFER_BULK == type) ||
278 (USB_ENDPOINT_XFER_ISOC == type)) {
279 if (USB_DIR_IN & desc->bEndpointAddress)
280 ep = find_ep (gadget, "ep5in");
281 else
282 ep = find_ep (gadget, "ep6out");
Cliff Cai767ffec2009-12-21 10:42:39 -0500283 } else if (USB_ENDPOINT_XFER_INT == type) {
284 if (USB_DIR_IN & desc->bEndpointAddress)
285 ep = find_ep(gadget, "ep1in");
286 else
287 ep = find_ep(gadget, "ep2out");
Bryan Wu3a8a3b12009-12-21 10:43:06 -0500288 } else
289 ep = NULL;
290 if (ep && ep_matches (gadget, ep, desc))
291 return ep;
292#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
David Brownella3536782006-07-06 15:48:53 -0700295 /* Second, look at endpoints until an unclaimed one looks usable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
297 if (ep_matches (gadget, ep, desc))
298 return ep;
299 }
300
301 /* Fail */
302 return NULL;
303}
304
305/**
306 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
307 * @gadget: device for which autoconfig state will be reset
308 *
309 * Use this for devices where one configuration may need to assign
310 * endpoint resources very differently from the next one. It clears
311 * state such as ep->driver_data and the record of assigned endpoints
312 * used by usb_ep_autoconfig().
313 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200314void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct usb_ep *ep;
317
318 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
319 ep->driver_data = NULL;
320 }
321#ifdef MANY_ENDPOINTS
322 in_epnum = 0;
323#endif
324 epnum = 0;
325}
326