Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
5f848137 | 30 | #include <linux/usb/ch9.h> |
9454a57a | 31 | #include <linux/usb/gadget.h> |
1da177e4 LT |
32 | |
33 | #include "gadget_chips.h" | |
34 | ||
35 | ||
36 | /* we must assign addresses for configurable endpoints (like net2280) */ | |
28824b18 | 37 | static unsigned epnum; |
1da177e4 LT |
38 | |
39 | // #define MANY_ENDPOINTS | |
40 | #ifdef MANY_ENDPOINTS | |
41 | /* more than 15 configurable endpoints */ | |
28824b18 | 42 | static unsigned in_epnum; |
1da177e4 LT |
43 | #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 | */ | |
28824b18 | 62 | static int |
1da177e4 LT |
63 | ep_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? */ | |
a9475226 | 74 | if (NULL != ep->driver_data) |
1da177e4 | 75 | return 0; |
a353678d | 76 | |
1da177e4 LT |
77 | /* 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 | ||
553fbcde JB |
131 | /* |
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 | ||
1da177e4 LT |
138 | /* 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 | */ | |
01ee7d70 | 142 | max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize); |
1da177e4 LT |
143 | 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 */ | |
551509d2 | 158 | if ((desc->wMaxPacketSize & cpu_to_le16(3<<11))) { |
1da177e4 LT |
159 | 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 */ | |
a4c39c41 | 169 | desc->bEndpointAddress &= USB_DIR_IN; |
1da177e4 | 170 | if (isdigit (ep->name [2])) { |
bb9496c6 | 171 | u8 num = simple_strtoul (&ep->name [2], NULL, 10); |
1da177e4 LT |
172 | 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 | } | |
48767a4e | 194 | ep->address = desc->bEndpointAddress; |
1da177e4 LT |
195 | return 1; |
196 | } | |
197 | ||
28824b18 | 198 | static struct usb_ep * |
1da177e4 LT |
199 | find_ep (struct usb_gadget *gadget, const char *name) |
200 | { | |
201 | struct usb_ep *ep; | |
202 | ||
203 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { | |
204 | if (0 == strcmp (ep->name, name)) | |
205 | return ep; | |
206 | } | |
207 | return NULL; | |
208 | } | |
209 | ||
210 | /** | |
211 | * usb_ep_autoconfig - choose an endpoint matching the descriptor | |
212 | * @gadget: The device to which the endpoint must belong. | |
213 | * @desc: Endpoint descriptor, with endpoint direction and transfer mode | |
214 | * initialized. For periodic transfers, the maximum packet | |
215 | * size must also be initialized. This is modified on success. | |
216 | * | |
217 | * By choosing an endpoint to use with the specified descriptor, this | |
218 | * routine simplifies writing gadget drivers that work with multiple | |
219 | * USB device controllers. The endpoint would be passed later to | |
220 | * usb_ep_enable(), along with some descriptor. | |
221 | * | |
222 | * That second descriptor won't always be the same as the first one. | |
223 | * For example, isochronous endpoints can be autoconfigured for high | |
224 | * bandwidth, and then used in several lower bandwidth altsettings. | |
225 | * Also, high and full speed descriptors will be different. | |
226 | * | |
227 | * Be sure to examine and test the results of autoconfiguration on your | |
228 | * hardware. This code may not make the best choices about how to use the | |
229 | * USB controller, and it can't know all the restrictions that may apply. | |
230 | * Some combinations of driver and hardware won't be able to autoconfigure. | |
231 | * | |
232 | * On success, this returns an un-claimed usb_ep, and modifies the endpoint | |
233 | * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value | |
234 | * is initialized as if the endpoint were used at full speed. To prevent | |
235 | * the endpoint from being returned by a later autoconfig call, claim it | |
236 | * by assigning ep->driver_data to some non-null value. | |
237 | * | |
238 | * On failure, this returns a null endpoint descriptor. | |
239 | */ | |
28824b18 | 240 | struct usb_ep *usb_ep_autoconfig ( |
1da177e4 LT |
241 | struct usb_gadget *gadget, |
242 | struct usb_endpoint_descriptor *desc | |
243 | ) | |
244 | { | |
245 | struct usb_ep *ep; | |
246 | u8 type; | |
247 | ||
248 | type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; | |
249 | ||
250 | /* First, apply chip-specific "best usage" knowledge. | |
251 | * This might make a good usb_gadget_ops hook ... | |
252 | */ | |
253 | if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) { | |
254 | /* ep-e, ep-f are PIO with only 64 byte fifos */ | |
255 | ep = find_ep (gadget, "ep-e"); | |
256 | if (ep && ep_matches (gadget, ep, desc)) | |
257 | return ep; | |
258 | ep = find_ep (gadget, "ep-f"); | |
259 | if (ep && ep_matches (gadget, ep, desc)) | |
260 | return ep; | |
261 | ||
262 | } else if (gadget_is_goku (gadget)) { | |
263 | if (USB_ENDPOINT_XFER_INT == type) { | |
264 | /* single buffering is enough */ | |
265 | ep = find_ep (gadget, "ep3-bulk"); | |
266 | if (ep && ep_matches (gadget, ep, desc)) | |
267 | return ep; | |
268 | } else if (USB_ENDPOINT_XFER_BULK == type | |
269 | && (USB_DIR_IN & desc->bEndpointAddress)) { | |
270 | /* DMA may be available */ | |
271 | ep = find_ep (gadget, "ep2-bulk"); | |
272 | if (ep && ep_matches (gadget, ep, desc)) | |
273 | return ep; | |
274 | } | |
275 | ||
3a8a3b1c | 276 | #ifdef CONFIG_BLACKFIN |
f2984a33 | 277 | } else if (gadget_is_musbhdrc(gadget)) { |
3a8a3b1c BW |
278 | if ((USB_ENDPOINT_XFER_BULK == type) || |
279 | (USB_ENDPOINT_XFER_ISOC == type)) { | |
280 | if (USB_DIR_IN & desc->bEndpointAddress) | |
281 | ep = find_ep (gadget, "ep5in"); | |
282 | else | |
283 | ep = find_ep (gadget, "ep6out"); | |
767ffec1 CC |
284 | } else if (USB_ENDPOINT_XFER_INT == type) { |
285 | if (USB_DIR_IN & desc->bEndpointAddress) | |
286 | ep = find_ep(gadget, "ep1in"); | |
287 | else | |
288 | ep = find_ep(gadget, "ep2out"); | |
3a8a3b1c BW |
289 | } else |
290 | ep = NULL; | |
291 | if (ep && ep_matches (gadget, ep, desc)) | |
292 | return ep; | |
293 | #endif | |
1da177e4 LT |
294 | } |
295 | ||
a353678d | 296 | /* Second, look at endpoints until an unclaimed one looks usable */ |
1da177e4 LT |
297 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { |
298 | if (ep_matches (gadget, ep, desc)) | |
299 | return ep; | |
300 | } | |
301 | ||
302 | /* Fail */ | |
303 | return NULL; | |
304 | } | |
305 | ||
306 | /** | |
307 | * usb_ep_autoconfig_reset - reset endpoint autoconfig state | |
308 | * @gadget: device for which autoconfig state will be reset | |
309 | * | |
310 | * Use this for devices where one configuration may need to assign | |
311 | * endpoint resources very differently from the next one. It clears | |
312 | * state such as ep->driver_data and the record of assigned endpoints | |
313 | * used by usb_ep_autoconfig(). | |
314 | */ | |
28824b18 | 315 | void usb_ep_autoconfig_reset (struct usb_gadget *gadget) |
1da177e4 LT |
316 | { |
317 | struct usb_ep *ep; | |
318 | ||
319 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { | |
320 | ep->driver_data = NULL; | |
321 | } | |
322 | #ifdef MANY_ENDPOINTS | |
323 | in_epnum = 0; | |
324 | #endif | |
325 | epnum = 0; | |
326 | } | |
327 |