usb: gadget: goku_udc: add goku_match_ep() function
[deliverable/linux.git] / drivers / usb / gadget / epautoconf.c
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
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/device.h>
16
17 #include <linux/ctype.h>
18 #include <linux/string.h>
19
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22
23 #include "gadget_chips.h"
24
25 /**
26 * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
27 * descriptor and ep companion descriptor
28 * @gadget: The device to which the endpoint must belong.
29 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
30 * initialized. For periodic transfers, the maximum packet
31 * size must also be initialized. This is modified on
32 * success.
33 * @ep_comp: Endpoint companion descriptor, with the required
34 * number of streams. Will be modified when the chosen EP
35 * supports a different number of streams.
36 *
37 * This routine replaces the usb_ep_autoconfig when needed
38 * superspeed enhancments. If such enhancemnets are required,
39 * the FD should call usb_ep_autoconfig_ss directly and provide
40 * the additional ep_comp parameter.
41 *
42 * By choosing an endpoint to use with the specified descriptor,
43 * this routine simplifies writing gadget drivers that work with
44 * multiple USB device controllers. The endpoint would be
45 * passed later to usb_ep_enable(), along with some descriptor.
46 *
47 * That second descriptor won't always be the same as the first one.
48 * For example, isochronous endpoints can be autoconfigured for high
49 * bandwidth, and then used in several lower bandwidth altsettings.
50 * Also, high and full speed descriptors will be different.
51 *
52 * Be sure to examine and test the results of autoconfiguration
53 * on your hardware. This code may not make the best choices
54 * about how to use the USB controller, and it can't know all
55 * the restrictions that may apply. Some combinations of driver
56 * and hardware won't be able to autoconfigure.
57 *
58 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
59 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
60 * is initialized as if the endpoint were used at full speed and
61 * the bmAttribute field in the ep companion descriptor is
62 * updated with the assigned number of streams if it is
63 * different from the original value. To prevent the endpoint
64 * from being returned by a later autoconfig call, claim it by
65 * assigning ep->claimed to true.
66 *
67 * On failure, this returns a null endpoint descriptor.
68 */
69 struct usb_ep *usb_ep_autoconfig_ss(
70 struct usb_gadget *gadget,
71 struct usb_endpoint_descriptor *desc,
72 struct usb_ss_ep_comp_descriptor *ep_comp
73 )
74 {
75 struct usb_ep *ep;
76 u8 type;
77
78 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
79
80 if (gadget->ops->match_ep) {
81 ep = gadget->ops->match_ep(gadget, desc, ep_comp);
82 if (ep)
83 goto found_ep;
84 }
85
86 /* First, apply chip-specific "best usage" knowledge.
87 * This might make a good usb_gadget_ops hook ...
88 */
89 #ifdef CONFIG_BLACKFIN
90 if (gadget_is_musbhdrc(gadget)) {
91 if ((USB_ENDPOINT_XFER_BULK == type) ||
92 (USB_ENDPOINT_XFER_ISOC == type)) {
93 if (USB_DIR_IN & desc->bEndpointAddress)
94 ep = gadget_find_ep_by_name(gadget, "ep5in");
95 else
96 ep = gadget_find_ep_by_name(gadget, "ep6out");
97 } else if (USB_ENDPOINT_XFER_INT == type) {
98 if (USB_DIR_IN & desc->bEndpointAddress)
99 ep = gadget_find_ep_by_name(gadget, "ep1in");
100 else
101 ep = gadget_find_ep_by_name(gadget, "ep2out");
102 } else
103 ep = NULL;
104 if (ep && usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp))
105 goto found_ep;
106 }
107 #endif
108
109 /* Second, look at endpoints until an unclaimed one looks usable */
110 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
111 if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp))
112 goto found_ep;
113 }
114
115 /* Fail */
116 return NULL;
117 found_ep:
118
119 /*
120 * If the protocol driver hasn't yet decided on wMaxPacketSize
121 * and wants to know the maximum possible, provide the info.
122 */
123 if (desc->wMaxPacketSize == 0)
124 desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket_limit);
125
126 /* report address */
127 desc->bEndpointAddress &= USB_DIR_IN;
128 if (isdigit(ep->name[2])) {
129 u8 num = simple_strtoul(&ep->name[2], NULL, 10);
130 desc->bEndpointAddress |= num;
131 } else if (desc->bEndpointAddress & USB_DIR_IN) {
132 if (++gadget->in_epnum > 15)
133 return NULL;
134 desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum;
135 } else {
136 if (++gadget->out_epnum > 15)
137 return NULL;
138 desc->bEndpointAddress |= gadget->out_epnum;
139 }
140
141 /* report (variable) full speed bulk maxpacket */
142 if ((type == USB_ENDPOINT_XFER_BULK) && !ep_comp) {
143 int size = ep->maxpacket_limit;
144
145 /* min() doesn't work on bitfields with gcc-3.5 */
146 if (size > 64)
147 size = 64;
148 desc->wMaxPacketSize = cpu_to_le16(size);
149 }
150
151 ep->address = desc->bEndpointAddress;
152 ep->desc = NULL;
153 ep->comp_desc = NULL;
154 ep->claimed = true;
155 return ep;
156 }
157 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss);
158
159 /**
160 * usb_ep_autoconfig() - choose an endpoint matching the
161 * descriptor
162 * @gadget: The device to which the endpoint must belong.
163 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
164 * initialized. For periodic transfers, the maximum packet
165 * size must also be initialized. This is modified on success.
166 *
167 * By choosing an endpoint to use with the specified descriptor, this
168 * routine simplifies writing gadget drivers that work with multiple
169 * USB device controllers. The endpoint would be passed later to
170 * usb_ep_enable(), along with some descriptor.
171 *
172 * That second descriptor won't always be the same as the first one.
173 * For example, isochronous endpoints can be autoconfigured for high
174 * bandwidth, and then used in several lower bandwidth altsettings.
175 * Also, high and full speed descriptors will be different.
176 *
177 * Be sure to examine and test the results of autoconfiguration on your
178 * hardware. This code may not make the best choices about how to use the
179 * USB controller, and it can't know all the restrictions that may apply.
180 * Some combinations of driver and hardware won't be able to autoconfigure.
181 *
182 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
183 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
184 * is initialized as if the endpoint were used at full speed. To prevent
185 * the endpoint from being returned by a later autoconfig call, claim it
186 * by assigning ep->claimed to true.
187 *
188 * On failure, this returns a null endpoint descriptor.
189 */
190 struct usb_ep *usb_ep_autoconfig(
191 struct usb_gadget *gadget,
192 struct usb_endpoint_descriptor *desc
193 )
194 {
195 return usb_ep_autoconfig_ss(gadget, desc, NULL);
196 }
197 EXPORT_SYMBOL_GPL(usb_ep_autoconfig);
198
199 /**
200 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
201 * @gadget: device for which autoconfig state will be reset
202 *
203 * Use this for devices where one configuration may need to assign
204 * endpoint resources very differently from the next one. It clears
205 * state such as ep->claimed and the record of assigned endpoints
206 * used by usb_ep_autoconfig().
207 */
208 void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
209 {
210 struct usb_ep *ep;
211
212 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
213 ep->claimed = false;
214 }
215 gadget->in_epnum = 0;
216 gadget->out_epnum = 0;
217 }
218 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset);
This page took 0.04237 seconds and 5 git commands to generate.