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. | |
1da177e4 LT |
10 | */ |
11 | ||
12 | #include <linux/kernel.h> | |
dc995fc2 | 13 | #include <linux/module.h> |
1da177e4 LT |
14 | #include <linux/init.h> |
15 | #include <linux/types.h> | |
16 | #include <linux/device.h> | |
17 | ||
18 | #include <linux/ctype.h> | |
19 | #include <linux/string.h> | |
20 | ||
5f848137 | 21 | #include <linux/usb/ch9.h> |
9454a57a | 22 | #include <linux/usb/gadget.h> |
1da177e4 LT |
23 | |
24 | #include "gadget_chips.h" | |
25 | ||
1da177e4 LT |
26 | /* |
27 | * This should work with endpoints from controller drivers sharing the | |
28 | * same endpoint naming convention. By example: | |
29 | * | |
30 | * - ep1, ep2, ... address is fixed, not direction or type | |
31 | * - ep1in, ep2out, ... address and direction are fixed, not type | |
32 | * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction | |
33 | * - ep1in-bulk, ep2out-iso, ... all three are fixed | |
34 | * - ep-* ... no functionality restrictions | |
35 | * | |
36 | * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal. | |
37 | * Less common restrictions are implied by gadget_is_*(). | |
38 | * | |
39 | * NOTE: each endpoint is unidirectional, as specified by its USB | |
40 | * descriptor; and isn't specific to a configuration or altsetting. | |
41 | */ | |
28824b18 | 42 | static int |
1da177e4 LT |
43 | ep_matches ( |
44 | struct usb_gadget *gadget, | |
45 | struct usb_ep *ep, | |
a59d6b91 TB |
46 | struct usb_endpoint_descriptor *desc, |
47 | struct usb_ss_ep_comp_descriptor *ep_comp | |
1da177e4 LT |
48 | ) |
49 | { | |
50 | u8 type; | |
51 | const char *tmp; | |
52 | u16 max; | |
53 | ||
a59d6b91 TB |
54 | int num_req_streams = 0; |
55 | ||
1da177e4 | 56 | /* endpoint already claimed? */ |
a9475226 | 57 | if (NULL != ep->driver_data) |
1da177e4 | 58 | return 0; |
a353678d | 59 | |
1da177e4 LT |
60 | /* only support ep0 for portable CONTROL traffic */ |
61 | type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; | |
62 | if (USB_ENDPOINT_XFER_CONTROL == type) | |
63 | return 0; | |
64 | ||
65 | /* some other naming convention */ | |
66 | if ('e' != ep->name[0]) | |
67 | return 0; | |
68 | ||
69 | /* type-restriction: "-iso", "-bulk", or "-int". | |
70 | * direction-restriction: "in", "out". | |
71 | */ | |
72 | if ('-' != ep->name[2]) { | |
73 | tmp = strrchr (ep->name, '-'); | |
74 | if (tmp) { | |
75 | switch (type) { | |
76 | case USB_ENDPOINT_XFER_INT: | |
77 | /* bulk endpoints handle interrupt transfers, | |
78 | * except the toggle-quirky iso-synch kind | |
79 | */ | |
80 | if ('s' == tmp[2]) // == "-iso" | |
81 | return 0; | |
82 | /* for now, avoid PXA "interrupt-in"; | |
83 | * it's documented as never using DATA1. | |
84 | */ | |
85 | if (gadget_is_pxa (gadget) | |
86 | && 'i' == tmp [1]) | |
87 | return 0; | |
88 | break; | |
89 | case USB_ENDPOINT_XFER_BULK: | |
90 | if ('b' != tmp[1]) // != "-bulk" | |
91 | return 0; | |
92 | break; | |
93 | case USB_ENDPOINT_XFER_ISOC: | |
94 | if ('s' != tmp[2]) // != "-iso" | |
95 | return 0; | |
96 | } | |
97 | } else { | |
98 | tmp = ep->name + strlen (ep->name); | |
99 | } | |
100 | ||
101 | /* direction-restriction: "..in-..", "out-.." */ | |
102 | tmp--; | |
103 | if (!isdigit (*tmp)) { | |
104 | if (desc->bEndpointAddress & USB_DIR_IN) { | |
105 | if ('n' != *tmp) | |
106 | return 0; | |
107 | } else { | |
108 | if ('t' != *tmp) | |
109 | return 0; | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
a59d6b91 TB |
114 | /* |
115 | * Get the number of required streams from the EP companion | |
116 | * descriptor and see if the EP matches it | |
117 | */ | |
118 | if (usb_endpoint_xfer_bulk(desc)) { | |
c74c9300 | 119 | if (ep_comp && gadget->max_speed >= USB_SPEED_SUPER) { |
a59d6b91 TB |
120 | num_req_streams = ep_comp->bmAttributes & 0x1f; |
121 | if (num_req_streams > ep->max_streams) | |
122 | return 0; | |
a59d6b91 TB |
123 | } |
124 | ||
125 | } | |
126 | ||
553fbcde JB |
127 | /* |
128 | * If the protocol driver hasn't yet decided on wMaxPacketSize | |
129 | * and wants to know the maximum possible, provide the info. | |
130 | */ | |
131 | if (desc->wMaxPacketSize == 0) | |
132 | desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket); | |
133 | ||
1da177e4 LT |
134 | /* endpoint maxpacket size is an input parameter, except for bulk |
135 | * where it's an output parameter representing the full speed limit. | |
136 | * the usb spec fixes high speed bulk maxpacket at 512 bytes. | |
137 | */ | |
29cc8897 | 138 | max = 0x7ff & usb_endpoint_maxp(desc); |
1da177e4 LT |
139 | switch (type) { |
140 | case USB_ENDPOINT_XFER_INT: | |
bdb64d72 | 141 | /* INT: limit 64 bytes full speed, 1024 high/super speed */ |
d327ab5b | 142 | if (!gadget_is_dualspeed(gadget) && max > 64) |
1da177e4 LT |
143 | return 0; |
144 | /* FALLTHROUGH */ | |
145 | ||
146 | case USB_ENDPOINT_XFER_ISOC: | |
bdb64d72 | 147 | /* ISO: limit 1023 bytes full speed, 1024 high/super speed */ |
1da177e4 LT |
148 | if (ep->maxpacket < max) |
149 | return 0; | |
d327ab5b | 150 | if (!gadget_is_dualspeed(gadget) && max > 1023) |
1da177e4 LT |
151 | return 0; |
152 | ||
153 | /* BOTH: "high bandwidth" works only at high speed */ | |
551509d2 | 154 | if ((desc->wMaxPacketSize & cpu_to_le16(3<<11))) { |
d327ab5b | 155 | if (!gadget_is_dualspeed(gadget)) |
1da177e4 LT |
156 | return 0; |
157 | /* configure your hardware with enough buffering!! */ | |
158 | } | |
159 | break; | |
160 | } | |
161 | ||
162 | /* MATCH!! */ | |
163 | ||
164 | /* report address */ | |
a4c39c41 | 165 | desc->bEndpointAddress &= USB_DIR_IN; |
1da177e4 | 166 | if (isdigit (ep->name [2])) { |
bb9496c6 | 167 | u8 num = simple_strtoul (&ep->name [2], NULL, 10); |
1da177e4 | 168 | desc->bEndpointAddress |= num; |
1da177e4 | 169 | } else if (desc->bEndpointAddress & USB_DIR_IN) { |
e87bb711 | 170 | if (++gadget->in_epnum > 15) |
1da177e4 | 171 | return 0; |
e87bb711 | 172 | desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum; |
1da177e4 | 173 | } else { |
e87bb711 | 174 | if (++gadget->out_epnum > 15) |
1da177e4 | 175 | return 0; |
e87bb711 | 176 | desc->bEndpointAddress |= gadget->out_epnum; |
1da177e4 LT |
177 | } |
178 | ||
179 | /* report (variable) full speed bulk maxpacket */ | |
bdb64d72 | 180 | if ((USB_ENDPOINT_XFER_BULK == type) && !ep_comp) { |
1da177e4 LT |
181 | int size = ep->maxpacket; |
182 | ||
183 | /* min() doesn't work on bitfields with gcc-3.5 */ | |
184 | if (size > 64) | |
185 | size = 64; | |
186 | desc->wMaxPacketSize = cpu_to_le16(size); | |
187 | } | |
48767a4e | 188 | ep->address = desc->bEndpointAddress; |
1da177e4 LT |
189 | return 1; |
190 | } | |
191 | ||
28824b18 | 192 | static struct usb_ep * |
1da177e4 LT |
193 | find_ep (struct usb_gadget *gadget, const char *name) |
194 | { | |
195 | struct usb_ep *ep; | |
196 | ||
197 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { | |
198 | if (0 == strcmp (ep->name, name)) | |
199 | return ep; | |
200 | } | |
201 | return NULL; | |
202 | } | |
203 | ||
204 | /** | |
a59d6b91 TB |
205 | * usb_ep_autoconfig_ss() - choose an endpoint matching the ep |
206 | * descriptor and ep companion descriptor | |
1da177e4 LT |
207 | * @gadget: The device to which the endpoint must belong. |
208 | * @desc: Endpoint descriptor, with endpoint direction and transfer mode | |
a59d6b91 TB |
209 | * initialized. For periodic transfers, the maximum packet |
210 | * size must also be initialized. This is modified on | |
211 | * success. | |
212 | * @ep_comp: Endpoint companion descriptor, with the required | |
213 | * number of streams. Will be modified when the chosen EP | |
214 | * supports a different number of streams. | |
1da177e4 | 215 | * |
a59d6b91 TB |
216 | * This routine replaces the usb_ep_autoconfig when needed |
217 | * superspeed enhancments. If such enhancemnets are required, | |
218 | * the FD should call usb_ep_autoconfig_ss directly and provide | |
219 | * the additional ep_comp parameter. | |
220 | * | |
221 | * By choosing an endpoint to use with the specified descriptor, | |
222 | * this routine simplifies writing gadget drivers that work with | |
223 | * multiple USB device controllers. The endpoint would be | |
224 | * passed later to usb_ep_enable(), along with some descriptor. | |
1da177e4 LT |
225 | * |
226 | * That second descriptor won't always be the same as the first one. | |
227 | * For example, isochronous endpoints can be autoconfigured for high | |
228 | * bandwidth, and then used in several lower bandwidth altsettings. | |
229 | * Also, high and full speed descriptors will be different. | |
230 | * | |
a59d6b91 TB |
231 | * Be sure to examine and test the results of autoconfiguration |
232 | * on your hardware. This code may not make the best choices | |
233 | * about how to use the USB controller, and it can't know all | |
234 | * the restrictions that may apply. Some combinations of driver | |
235 | * and hardware won't be able to autoconfigure. | |
1da177e4 LT |
236 | * |
237 | * On success, this returns an un-claimed usb_ep, and modifies the endpoint | |
238 | * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value | |
a59d6b91 TB |
239 | * is initialized as if the endpoint were used at full speed and |
240 | * the bmAttribute field in the ep companion descriptor is | |
241 | * updated with the assigned number of streams if it is | |
242 | * different from the original value. To prevent the endpoint | |
243 | * from being returned by a later autoconfig call, claim it by | |
244 | * assigning ep->driver_data to some non-null value. | |
1da177e4 LT |
245 | * |
246 | * On failure, this returns a null endpoint descriptor. | |
247 | */ | |
a59d6b91 | 248 | struct usb_ep *usb_ep_autoconfig_ss( |
1da177e4 | 249 | struct usb_gadget *gadget, |
a59d6b91 TB |
250 | struct usb_endpoint_descriptor *desc, |
251 | struct usb_ss_ep_comp_descriptor *ep_comp | |
1da177e4 LT |
252 | ) |
253 | { | |
254 | struct usb_ep *ep; | |
255 | u8 type; | |
256 | ||
257 | type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; | |
258 | ||
259 | /* First, apply chip-specific "best usage" knowledge. | |
260 | * This might make a good usb_gadget_ops hook ... | |
261 | */ | |
262 | if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) { | |
263 | /* ep-e, ep-f are PIO with only 64 byte fifos */ | |
264 | ep = find_ep (gadget, "ep-e"); | |
a59d6b91 | 265 | if (ep && ep_matches(gadget, ep, desc, ep_comp)) |
609ca228 | 266 | goto found_ep; |
1da177e4 | 267 | ep = find_ep (gadget, "ep-f"); |
a59d6b91 | 268 | if (ep && ep_matches(gadget, ep, desc, ep_comp)) |
609ca228 | 269 | goto found_ep; |
1da177e4 LT |
270 | |
271 | } else if (gadget_is_goku (gadget)) { | |
272 | if (USB_ENDPOINT_XFER_INT == type) { | |
273 | /* single buffering is enough */ | |
a59d6b91 TB |
274 | ep = find_ep(gadget, "ep3-bulk"); |
275 | if (ep && ep_matches(gadget, ep, desc, ep_comp)) | |
609ca228 | 276 | goto found_ep; |
1da177e4 LT |
277 | } else if (USB_ENDPOINT_XFER_BULK == type |
278 | && (USB_DIR_IN & desc->bEndpointAddress)) { | |
279 | /* DMA may be available */ | |
a59d6b91 TB |
280 | ep = find_ep(gadget, "ep2-bulk"); |
281 | if (ep && ep_matches(gadget, ep, desc, | |
282 | ep_comp)) | |
609ca228 | 283 | goto found_ep; |
1da177e4 LT |
284 | } |
285 | ||
3a8a3b1c | 286 | #ifdef CONFIG_BLACKFIN |
f2984a33 | 287 | } else if (gadget_is_musbhdrc(gadget)) { |
3a8a3b1c BW |
288 | if ((USB_ENDPOINT_XFER_BULK == type) || |
289 | (USB_ENDPOINT_XFER_ISOC == type)) { | |
290 | if (USB_DIR_IN & desc->bEndpointAddress) | |
291 | ep = find_ep (gadget, "ep5in"); | |
292 | else | |
293 | ep = find_ep (gadget, "ep6out"); | |
767ffec1 CC |
294 | } else if (USB_ENDPOINT_XFER_INT == type) { |
295 | if (USB_DIR_IN & desc->bEndpointAddress) | |
296 | ep = find_ep(gadget, "ep1in"); | |
297 | else | |
298 | ep = find_ep(gadget, "ep2out"); | |
3a8a3b1c BW |
299 | } else |
300 | ep = NULL; | |
a59d6b91 | 301 | if (ep && ep_matches(gadget, ep, desc, ep_comp)) |
609ca228 | 302 | goto found_ep; |
3a8a3b1c | 303 | #endif |
1da177e4 LT |
304 | } |
305 | ||
a353678d | 306 | /* Second, look at endpoints until an unclaimed one looks usable */ |
1da177e4 | 307 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { |
a59d6b91 | 308 | if (ep_matches(gadget, ep, desc, ep_comp)) |
609ca228 | 309 | goto found_ep; |
1da177e4 LT |
310 | } |
311 | ||
312 | /* Fail */ | |
313 | return NULL; | |
609ca228 SAS |
314 | found_ep: |
315 | ep->desc = NULL; | |
316 | ep->comp_desc = NULL; | |
317 | return ep; | |
1da177e4 | 318 | } |
dc995fc2 | 319 | EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss); |
1da177e4 | 320 | |
a59d6b91 TB |
321 | /** |
322 | * usb_ep_autoconfig() - choose an endpoint matching the | |
323 | * descriptor | |
324 | * @gadget: The device to which the endpoint must belong. | |
325 | * @desc: Endpoint descriptor, with endpoint direction and transfer mode | |
326 | * initialized. For periodic transfers, the maximum packet | |
327 | * size must also be initialized. This is modified on success. | |
328 | * | |
329 | * By choosing an endpoint to use with the specified descriptor, this | |
330 | * routine simplifies writing gadget drivers that work with multiple | |
331 | * USB device controllers. The endpoint would be passed later to | |
332 | * usb_ep_enable(), along with some descriptor. | |
333 | * | |
334 | * That second descriptor won't always be the same as the first one. | |
335 | * For example, isochronous endpoints can be autoconfigured for high | |
336 | * bandwidth, and then used in several lower bandwidth altsettings. | |
337 | * Also, high and full speed descriptors will be different. | |
338 | * | |
339 | * Be sure to examine and test the results of autoconfiguration on your | |
340 | * hardware. This code may not make the best choices about how to use the | |
341 | * USB controller, and it can't know all the restrictions that may apply. | |
342 | * Some combinations of driver and hardware won't be able to autoconfigure. | |
343 | * | |
344 | * On success, this returns an un-claimed usb_ep, and modifies the endpoint | |
345 | * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value | |
346 | * is initialized as if the endpoint were used at full speed. To prevent | |
347 | * the endpoint from being returned by a later autoconfig call, claim it | |
348 | * by assigning ep->driver_data to some non-null value. | |
349 | * | |
350 | * On failure, this returns a null endpoint descriptor. | |
351 | */ | |
352 | struct usb_ep *usb_ep_autoconfig( | |
353 | struct usb_gadget *gadget, | |
354 | struct usb_endpoint_descriptor *desc | |
355 | ) | |
356 | { | |
357 | return usb_ep_autoconfig_ss(gadget, desc, NULL); | |
358 | } | |
dc995fc2 | 359 | EXPORT_SYMBOL_GPL(usb_ep_autoconfig); |
a59d6b91 | 360 | |
1da177e4 LT |
361 | /** |
362 | * usb_ep_autoconfig_reset - reset endpoint autoconfig state | |
363 | * @gadget: device for which autoconfig state will be reset | |
364 | * | |
365 | * Use this for devices where one configuration may need to assign | |
366 | * endpoint resources very differently from the next one. It clears | |
367 | * state such as ep->driver_data and the record of assigned endpoints | |
368 | * used by usb_ep_autoconfig(). | |
369 | */ | |
28824b18 | 370 | void usb_ep_autoconfig_reset (struct usb_gadget *gadget) |
1da177e4 LT |
371 | { |
372 | struct usb_ep *ep; | |
373 | ||
374 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { | |
375 | ep->driver_data = NULL; | |
376 | } | |
e87bb711 SAS |
377 | gadget->in_epnum = 0; |
378 | gadget->out_epnum = 0; | |
1da177e4 | 379 | } |
dc995fc2 | 380 | EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset); |