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