Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers |
| 4 | * |
| 5 | * Copyright (C) 2004 David Brownell |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
Sebastian Andrzej Siewior | dc995fc | 2012-09-06 20:11:12 +0200 | [diff] [blame] | 9 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/types.h> |
| 11 | #include <linux/device.h> |
| 12 | |
| 13 | #include <linux/ctype.h> |
| 14 | #include <linux/string.h> |
| 15 | |
David Brownell | 5f84813 | 2006-12-16 15:34:53 -0800 | [diff] [blame] | 16 | #include <linux/usb/ch9.h> |
David Brownell | 9454a57 | 2007-10-04 18:05:17 -0700 | [diff] [blame] | 17 | #include <linux/usb/gadget.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | /** |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 20 | * usb_ep_autoconfig_ss() - choose an endpoint matching the ep |
| 21 | * descriptor and ep companion descriptor |
| 22 | * @gadget: The device to which the endpoint must belong. |
| 23 | * @desc: Endpoint descriptor, with endpoint direction and transfer mode |
| 24 | * initialized. For periodic transfers, the maximum packet |
| 25 | * size must also be initialized. This is modified on |
| 26 | * success. |
| 27 | * @ep_comp: Endpoint companion descriptor, with the required |
| 28 | * number of streams. Will be modified when the chosen EP |
| 29 | * supports a different number of streams. |
| 30 | * |
| 31 | * This routine replaces the usb_ep_autoconfig when needed |
| 32 | * superspeed enhancments. If such enhancemnets are required, |
| 33 | * the FD should call usb_ep_autoconfig_ss directly and provide |
| 34 | * the additional ep_comp parameter. |
| 35 | * |
| 36 | * By choosing an endpoint to use with the specified descriptor, |
| 37 | * this routine simplifies writing gadget drivers that work with |
| 38 | * multiple USB device controllers. The endpoint would be |
| 39 | * passed later to usb_ep_enable(), along with some descriptor. |
| 40 | * |
| 41 | * That second descriptor won't always be the same as the first one. |
| 42 | * For example, isochronous endpoints can be autoconfigured for high |
| 43 | * bandwidth, and then used in several lower bandwidth altsettings. |
| 44 | * Also, high and full speed descriptors will be different. |
| 45 | * |
| 46 | * Be sure to examine and test the results of autoconfiguration |
| 47 | * on your hardware. This code may not make the best choices |
| 48 | * about how to use the USB controller, and it can't know all |
| 49 | * the restrictions that may apply. Some combinations of driver |
| 50 | * and hardware won't be able to autoconfigure. |
| 51 | * |
Robert Baldyga | f871cb9 | 2015-09-16 12:10:39 +0200 | [diff] [blame] | 52 | * On success, this returns an claimed usb_ep, and modifies the endpoint |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 53 | * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value |
| 54 | * is initialized as if the endpoint were used at full speed and |
| 55 | * the bmAttribute field in the ep companion descriptor is |
| 56 | * updated with the assigned number of streams if it is |
| 57 | * different from the original value. To prevent the endpoint |
Robert Baldyga | f871cb9 | 2015-09-16 12:10:39 +0200 | [diff] [blame] | 58 | * from being returned by a later autoconfig call, claims it by |
Robert Baldyga | cc476b4 | 2015-07-31 16:00:13 +0200 | [diff] [blame] | 59 | * assigning ep->claimed to true. |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 60 | * |
| 61 | * On failure, this returns a null endpoint descriptor. |
| 62 | */ |
| 63 | struct usb_ep *usb_ep_autoconfig_ss( |
| 64 | struct usb_gadget *gadget, |
| 65 | struct usb_endpoint_descriptor *desc, |
| 66 | struct usb_ss_ep_comp_descriptor *ep_comp |
| 67 | ) |
| 68 | { |
| 69 | struct usb_ep *ep; |
| 70 | u8 type; |
| 71 | |
| 72 | type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
| 73 | |
Robert Baldyga | 596c154 | 2015-08-06 14:11:10 +0200 | [diff] [blame] | 74 | if (gadget->ops->match_ep) { |
| 75 | ep = gadget->ops->match_ep(gadget, desc, ep_comp); |
| 76 | if (ep) |
| 77 | goto found_ep; |
| 78 | } |
| 79 | |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 80 | /* Second, look at endpoints until an unclaimed one looks usable */ |
| 81 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { |
Robert Baldyga | 4278c68 | 2015-08-06 14:11:11 +0200 | [diff] [blame] | 82 | if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp)) |
Sebastian Andrzej Siewior | 609ca22 | 2012-02-06 18:46:35 +0100 | [diff] [blame] | 83 | goto found_ep; |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* Fail */ |
| 87 | return NULL; |
Sebastian Andrzej Siewior | 609ca22 | 2012-02-06 18:46:35 +0100 | [diff] [blame] | 88 | found_ep: |
Robert Baldyga | 5dbe135 | 2015-07-31 16:00:51 +0200 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * If the protocol driver hasn't yet decided on wMaxPacketSize |
| 92 | * and wants to know the maximum possible, provide the info. |
| 93 | */ |
| 94 | if (desc->wMaxPacketSize == 0) |
| 95 | desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket_limit); |
| 96 | |
| 97 | /* report address */ |
| 98 | desc->bEndpointAddress &= USB_DIR_IN; |
| 99 | if (isdigit(ep->name[2])) { |
| 100 | u8 num = simple_strtoul(&ep->name[2], NULL, 10); |
| 101 | desc->bEndpointAddress |= num; |
| 102 | } else if (desc->bEndpointAddress & USB_DIR_IN) { |
| 103 | if (++gadget->in_epnum > 15) |
| 104 | return NULL; |
| 105 | desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum; |
| 106 | } else { |
| 107 | if (++gadget->out_epnum > 15) |
| 108 | return NULL; |
| 109 | desc->bEndpointAddress |= gadget->out_epnum; |
| 110 | } |
| 111 | |
| 112 | /* report (variable) full speed bulk maxpacket */ |
| 113 | if ((type == USB_ENDPOINT_XFER_BULK) && !ep_comp) { |
| 114 | int size = ep->maxpacket_limit; |
| 115 | |
| 116 | /* min() doesn't work on bitfields with gcc-3.5 */ |
| 117 | if (size > 64) |
| 118 | size = 64; |
| 119 | desc->wMaxPacketSize = cpu_to_le16(size); |
| 120 | } |
| 121 | |
| 122 | ep->address = desc->bEndpointAddress; |
Sebastian Andrzej Siewior | 609ca22 | 2012-02-06 18:46:35 +0100 | [diff] [blame] | 123 | ep->desc = NULL; |
| 124 | ep->comp_desc = NULL; |
Robert Baldyga | cc476b4 | 2015-07-31 16:00:13 +0200 | [diff] [blame] | 125 | ep->claimed = true; |
Sebastian Andrzej Siewior | 609ca22 | 2012-02-06 18:46:35 +0100 | [diff] [blame] | 126 | return ep; |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 127 | } |
Sebastian Andrzej Siewior | dc995fc | 2012-09-06 20:11:12 +0200 | [diff] [blame] | 128 | EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss); |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * usb_ep_autoconfig() - choose an endpoint matching the |
| 132 | * descriptor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | * @gadget: The device to which the endpoint must belong. |
| 134 | * @desc: Endpoint descriptor, with endpoint direction and transfer mode |
| 135 | * initialized. For periodic transfers, the maximum packet |
| 136 | * size must also be initialized. This is modified on success. |
| 137 | * |
| 138 | * By choosing an endpoint to use with the specified descriptor, this |
| 139 | * routine simplifies writing gadget drivers that work with multiple |
| 140 | * USB device controllers. The endpoint would be passed later to |
| 141 | * usb_ep_enable(), along with some descriptor. |
| 142 | * |
| 143 | * That second descriptor won't always be the same as the first one. |
| 144 | * For example, isochronous endpoints can be autoconfigured for high |
| 145 | * bandwidth, and then used in several lower bandwidth altsettings. |
| 146 | * Also, high and full speed descriptors will be different. |
| 147 | * |
| 148 | * Be sure to examine and test the results of autoconfiguration on your |
| 149 | * hardware. This code may not make the best choices about how to use the |
| 150 | * USB controller, and it can't know all the restrictions that may apply. |
| 151 | * Some combinations of driver and hardware won't be able to autoconfigure. |
| 152 | * |
Robert Baldyga | f871cb9 | 2015-09-16 12:10:39 +0200 | [diff] [blame] | 153 | * On success, this returns an claimed usb_ep, and modifies the endpoint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value |
| 155 | * is initialized as if the endpoint were used at full speed. To prevent |
Robert Baldyga | f871cb9 | 2015-09-16 12:10:39 +0200 | [diff] [blame] | 156 | * the endpoint from being returned by a later autoconfig call, claims it |
Robert Baldyga | cc476b4 | 2015-07-31 16:00:13 +0200 | [diff] [blame] | 157 | * by assigning ep->claimed to true. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | * |
| 159 | * On failure, this returns a null endpoint descriptor. |
| 160 | */ |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 161 | struct usb_ep *usb_ep_autoconfig( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | struct usb_gadget *gadget, |
| 163 | struct usb_endpoint_descriptor *desc |
| 164 | ) |
| 165 | { |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 166 | return usb_ep_autoconfig_ss(gadget, desc, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Sebastian Andrzej Siewior | dc995fc | 2012-09-06 20:11:12 +0200 | [diff] [blame] | 168 | EXPORT_SYMBOL_GPL(usb_ep_autoconfig); |
Tatyana Brokhman | a59d6b9 | 2011-06-28 16:33:53 +0300 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | /** |
Robert Baldyga | b67f628 | 2015-09-16 12:10:41 +0200 | [diff] [blame] | 171 | * usb_ep_autoconfig_release - releases endpoint and set it to initial state |
| 172 | * @ep: endpoint which should be released |
| 173 | * |
| 174 | * This function can be used during function bind for endpoints obtained |
| 175 | * from usb_ep_autoconfig(). It unclaims endpoint claimed by |
| 176 | * usb_ep_autoconfig() to make it available for other functions. Endpoint |
| 177 | * which was released is no longer invalid and shouldn't be used in |
| 178 | * context of function which released it. |
| 179 | */ |
| 180 | void usb_ep_autoconfig_release(struct usb_ep *ep) |
| 181 | { |
| 182 | ep->claimed = false; |
| 183 | ep->driver_data = NULL; |
| 184 | } |
| 185 | EXPORT_SYMBOL_GPL(usb_ep_autoconfig_release); |
| 186 | |
| 187 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | * usb_ep_autoconfig_reset - reset endpoint autoconfig state |
| 189 | * @gadget: device for which autoconfig state will be reset |
| 190 | * |
| 191 | * Use this for devices where one configuration may need to assign |
| 192 | * endpoint resources very differently from the next one. It clears |
Robert Baldyga | cc476b4 | 2015-07-31 16:00:13 +0200 | [diff] [blame] | 193 | * state such as ep->claimed and the record of assigned endpoints |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | * used by usb_ep_autoconfig(). |
| 195 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 196 | void usb_ep_autoconfig_reset (struct usb_gadget *gadget) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
| 198 | struct usb_ep *ep; |
| 199 | |
| 200 | list_for_each_entry (ep, &gadget->ep_list, ep_list) { |
Robert Baldyga | cc476b4 | 2015-07-31 16:00:13 +0200 | [diff] [blame] | 201 | ep->claimed = false; |
Robert Baldyga | e4c1b1b | 2015-08-21 11:01:29 +0200 | [diff] [blame] | 202 | ep->driver_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
Sebastian Andrzej Siewior | e87bb71 | 2012-09-06 20:11:11 +0200 | [diff] [blame] | 204 | gadget->in_epnum = 0; |
| 205 | gadget->out_epnum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
Sebastian Andrzej Siewior | dc995fc | 2012-09-06 20:11:12 +0200 | [diff] [blame] | 207 | EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset); |