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, |
| 66 | struct usb_endpoint_descriptor *desc |
| 67 | ) |
| 68 | { |
| 69 | u8 type; |
| 70 | const char *tmp; |
| 71 | u16 max; |
| 72 | |
| 73 | /* endpoint already claimed? */ |
David Brownell | a947522 | 2007-07-30 12:31:07 -0700 | [diff] [blame] | 74 | if (NULL != ep->driver_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return 0; |
David Brownell | a353678 | 2006-07-06 15:48:53 -0700 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 131 | /* endpoint maxpacket size is an input parameter, except for bulk |
| 132 | * where it's an output parameter representing the full speed limit. |
| 133 | * the usb spec fixes high speed bulk maxpacket at 512 bytes. |
| 134 | */ |
David Brownell | 01ee7d7 | 2007-05-25 20:40:14 -0700 | [diff] [blame] | 135 | max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | switch (type) { |
| 137 | case USB_ENDPOINT_XFER_INT: |
| 138 | /* INT: limit 64 bytes full speed, 1024 high speed */ |
| 139 | if (!gadget->is_dualspeed && max > 64) |
| 140 | return 0; |
| 141 | /* FALLTHROUGH */ |
| 142 | |
| 143 | case USB_ENDPOINT_XFER_ISOC: |
| 144 | /* ISO: limit 1023 bytes full speed, 1024 high speed */ |
| 145 | if (ep->maxpacket < max) |
| 146 | return 0; |
| 147 | if (!gadget->is_dualspeed && max > 1023) |
| 148 | return 0; |
| 149 | |
| 150 | /* BOTH: "high bandwidth" works only at high speed */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 151 | if ((desc->wMaxPacketSize & cpu_to_le16(3<<11))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (!gadget->is_dualspeed) |
| 153 | return 0; |
| 154 | /* configure your hardware with enough buffering!! */ |
| 155 | } |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | /* MATCH!! */ |
| 160 | |
| 161 | /* report address */ |
David Brownell | a4c39c4 | 2008-06-19 17:52:25 -0700 | [diff] [blame] | 162 | desc->bEndpointAddress &= USB_DIR_IN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | if (isdigit (ep->name [2])) { |
Julia Lawall | bb9496c | 2008-11-25 14:15:19 +0100 | [diff] [blame] | 164 | u8 num = simple_strtoul (&ep->name [2], NULL, 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | desc->bEndpointAddress |= num; |
| 166 | #ifdef MANY_ENDPOINTS |
| 167 | } else if (desc->bEndpointAddress & USB_DIR_IN) { |
| 168 | if (++in_epnum > 15) |
| 169 | return 0; |
| 170 | desc->bEndpointAddress = USB_DIR_IN | in_epnum; |
| 171 | #endif |
| 172 | } else { |
| 173 | if (++epnum > 15) |
| 174 | return 0; |
| 175 | desc->bEndpointAddress |= epnum; |
| 176 | } |
| 177 | |
| 178 | /* report (variable) full speed bulk maxpacket */ |
| 179 | if (USB_ENDPOINT_XFER_BULK == type) { |
| 180 | int size = ep->maxpacket; |
| 181 | |
| 182 | /* min() doesn't work on bitfields with gcc-3.5 */ |
| 183 | if (size > 64) |
| 184 | size = 64; |
| 185 | desc->wMaxPacketSize = cpu_to_le16(size); |
| 186 | } |
| 187 | return 1; |
| 188 | } |
| 189 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 190 | static struct usb_ep * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | find_ep (struct usb_gadget *gadget, const char *name) |
| 192 | { |
| 193 | struct usb_ep *ep; |
| 194 | |
| 195 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { |
| 196 | if (0 == strcmp (ep->name, name)) |
| 197 | return ep; |
| 198 | } |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * usb_ep_autoconfig - choose an endpoint matching the descriptor |
| 204 | * @gadget: The device to which the endpoint must belong. |
| 205 | * @desc: Endpoint descriptor, with endpoint direction and transfer mode |
| 206 | * initialized. For periodic transfers, the maximum packet |
| 207 | * size must also be initialized. This is modified on success. |
| 208 | * |
| 209 | * By choosing an endpoint to use with the specified descriptor, this |
| 210 | * routine simplifies writing gadget drivers that work with multiple |
| 211 | * USB device controllers. The endpoint would be passed later to |
| 212 | * usb_ep_enable(), along with some descriptor. |
| 213 | * |
| 214 | * That second descriptor won't always be the same as the first one. |
| 215 | * For example, isochronous endpoints can be autoconfigured for high |
| 216 | * bandwidth, and then used in several lower bandwidth altsettings. |
| 217 | * Also, high and full speed descriptors will be different. |
| 218 | * |
| 219 | * Be sure to examine and test the results of autoconfiguration on your |
| 220 | * hardware. This code may not make the best choices about how to use the |
| 221 | * USB controller, and it can't know all the restrictions that may apply. |
| 222 | * Some combinations of driver and hardware won't be able to autoconfigure. |
| 223 | * |
| 224 | * On success, this returns an un-claimed usb_ep, and modifies the endpoint |
| 225 | * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value |
| 226 | * is initialized as if the endpoint were used at full speed. To prevent |
| 227 | * the endpoint from being returned by a later autoconfig call, claim it |
| 228 | * by assigning ep->driver_data to some non-null value. |
| 229 | * |
| 230 | * On failure, this returns a null endpoint descriptor. |
| 231 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 232 | struct usb_ep *usb_ep_autoconfig ( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | struct usb_gadget *gadget, |
| 234 | struct usb_endpoint_descriptor *desc |
| 235 | ) |
| 236 | { |
| 237 | struct usb_ep *ep; |
| 238 | u8 type; |
| 239 | |
| 240 | type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
| 241 | |
| 242 | /* First, apply chip-specific "best usage" knowledge. |
| 243 | * This might make a good usb_gadget_ops hook ... |
| 244 | */ |
| 245 | if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) { |
| 246 | /* ep-e, ep-f are PIO with only 64 byte fifos */ |
| 247 | ep = find_ep (gadget, "ep-e"); |
| 248 | if (ep && ep_matches (gadget, ep, desc)) |
| 249 | return ep; |
| 250 | ep = find_ep (gadget, "ep-f"); |
| 251 | if (ep && ep_matches (gadget, ep, desc)) |
| 252 | return ep; |
| 253 | |
| 254 | } else if (gadget_is_goku (gadget)) { |
| 255 | if (USB_ENDPOINT_XFER_INT == type) { |
| 256 | /* single buffering is enough */ |
| 257 | ep = find_ep (gadget, "ep3-bulk"); |
| 258 | if (ep && ep_matches (gadget, ep, desc)) |
| 259 | return ep; |
| 260 | } else if (USB_ENDPOINT_XFER_BULK == type |
| 261 | && (USB_DIR_IN & desc->bEndpointAddress)) { |
| 262 | /* DMA may be available */ |
| 263 | ep = find_ep (gadget, "ep2-bulk"); |
| 264 | if (ep && ep_matches (gadget, ep, desc)) |
| 265 | return ep; |
| 266 | } |
| 267 | |
Bryan Wu | 3a8a3b1 | 2009-12-21 10:43:06 -0500 | [diff] [blame] | 268 | #ifdef CONFIG_BLACKFIN |
Mike Frysinger | f2984a3 | 2010-03-09 00:35:22 -0500 | [diff] [blame] | 269 | } else if (gadget_is_musbhdrc(gadget)) { |
Bryan Wu | 3a8a3b1 | 2009-12-21 10:43:06 -0500 | [diff] [blame] | 270 | if ((USB_ENDPOINT_XFER_BULK == type) || |
| 271 | (USB_ENDPOINT_XFER_ISOC == type)) { |
| 272 | if (USB_DIR_IN & desc->bEndpointAddress) |
| 273 | ep = find_ep (gadget, "ep5in"); |
| 274 | else |
| 275 | ep = find_ep (gadget, "ep6out"); |
Cliff Cai | 767ffec | 2009-12-21 10:42:39 -0500 | [diff] [blame] | 276 | } else if (USB_ENDPOINT_XFER_INT == type) { |
| 277 | if (USB_DIR_IN & desc->bEndpointAddress) |
| 278 | ep = find_ep(gadget, "ep1in"); |
| 279 | else |
| 280 | ep = find_ep(gadget, "ep2out"); |
Bryan Wu | 3a8a3b1 | 2009-12-21 10:43:06 -0500 | [diff] [blame] | 281 | } else |
| 282 | ep = NULL; |
| 283 | if (ep && ep_matches (gadget, ep, desc)) |
| 284 | return ep; |
| 285 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
David Brownell | a353678 | 2006-07-06 15:48:53 -0700 | [diff] [blame] | 288 | /* Second, look at endpoints until an unclaimed one looks usable */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { |
| 290 | if (ep_matches (gadget, ep, desc)) |
| 291 | return ep; |
| 292 | } |
| 293 | |
| 294 | /* Fail */ |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * usb_ep_autoconfig_reset - reset endpoint autoconfig state |
| 300 | * @gadget: device for which autoconfig state will be reset |
| 301 | * |
| 302 | * Use this for devices where one configuration may need to assign |
| 303 | * endpoint resources very differently from the next one. It clears |
| 304 | * state such as ep->driver_data and the record of assigned endpoints |
| 305 | * used by usb_ep_autoconfig(). |
| 306 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 307 | void usb_ep_autoconfig_reset (struct usb_gadget *gadget) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
| 309 | struct usb_ep *ep; |
| 310 | |
| 311 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { |
| 312 | ep->driver_data = NULL; |
| 313 | } |
| 314 | #ifdef MANY_ENDPOINTS |
| 315 | in_epnum = 0; |
| 316 | #endif |
| 317 | epnum = 0; |
| 318 | } |
| 319 | |