usb: musb: gadget: add musb_match_ep() function
[deliverable/linux.git] / drivers / usb / gadget / epautoconf.c
CommitLineData
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/types.h>
15#include <linux/device.h>
16
17#include <linux/ctype.h>
18#include <linux/string.h>
19
5f848137 20#include <linux/usb/ch9.h>
9454a57a 21#include <linux/usb/gadget.h>
1da177e4
LT
22
23#include "gadget_chips.h"
24
1da177e4 25/**
a59d6b91
TB
26 * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
27 * descriptor and ep companion descriptor
1da177e4
LT
28 * @gadget: The device to which the endpoint must belong.
29 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
a59d6b91
TB
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.
1da177e4 36 *
a59d6b91
TB
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.
1da177e4
LT
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 *
a59d6b91
TB
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.
1da177e4
LT
57 *
58 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
59 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
a59d6b91
TB
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
cc476b42 65 * assigning ep->claimed to true.
1da177e4
LT
66 *
67 * On failure, this returns a null endpoint descriptor.
68 */
a59d6b91 69struct usb_ep *usb_ep_autoconfig_ss(
1da177e4 70 struct usb_gadget *gadget,
a59d6b91
TB
71 struct usb_endpoint_descriptor *desc,
72 struct usb_ss_ep_comp_descriptor *ep_comp
1da177e4
LT
73)
74{
75 struct usb_ep *ep;
76 u8 type;
77
78 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
79
596c154d
RB
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
a353678d 86 /* Second, look at endpoints until an unclaimed one looks usable */
1da177e4 87 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
4278c687 88 if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp))
609ca228 89 goto found_ep;
1da177e4
LT
90 }
91
92 /* Fail */
93 return NULL;
609ca228 94found_ep:
5dbe135a
RB
95
96 /*
97 * If the protocol driver hasn't yet decided on wMaxPacketSize
98 * and wants to know the maximum possible, provide the info.
99 */
100 if (desc->wMaxPacketSize == 0)
101 desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket_limit);
102
103 /* report address */
104 desc->bEndpointAddress &= USB_DIR_IN;
105 if (isdigit(ep->name[2])) {
106 u8 num = simple_strtoul(&ep->name[2], NULL, 10);
107 desc->bEndpointAddress |= num;
108 } else if (desc->bEndpointAddress & USB_DIR_IN) {
109 if (++gadget->in_epnum > 15)
110 return NULL;
111 desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum;
112 } else {
113 if (++gadget->out_epnum > 15)
114 return NULL;
115 desc->bEndpointAddress |= gadget->out_epnum;
116 }
117
118 /* report (variable) full speed bulk maxpacket */
119 if ((type == USB_ENDPOINT_XFER_BULK) && !ep_comp) {
120 int size = ep->maxpacket_limit;
121
122 /* min() doesn't work on bitfields with gcc-3.5 */
123 if (size > 64)
124 size = 64;
125 desc->wMaxPacketSize = cpu_to_le16(size);
126 }
127
128 ep->address = desc->bEndpointAddress;
609ca228
SAS
129 ep->desc = NULL;
130 ep->comp_desc = NULL;
cc476b42 131 ep->claimed = true;
609ca228 132 return ep;
1da177e4 133}
dc995fc2 134EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss);
1da177e4 135
a59d6b91
TB
136/**
137 * usb_ep_autoconfig() - choose an endpoint matching the
138 * descriptor
139 * @gadget: The device to which the endpoint must belong.
140 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
141 * initialized. For periodic transfers, the maximum packet
142 * size must also be initialized. This is modified on success.
143 *
144 * By choosing an endpoint to use with the specified descriptor, this
145 * routine simplifies writing gadget drivers that work with multiple
146 * USB device controllers. The endpoint would be passed later to
147 * usb_ep_enable(), along with some descriptor.
148 *
149 * That second descriptor won't always be the same as the first one.
150 * For example, isochronous endpoints can be autoconfigured for high
151 * bandwidth, and then used in several lower bandwidth altsettings.
152 * Also, high and full speed descriptors will be different.
153 *
154 * Be sure to examine and test the results of autoconfiguration on your
155 * hardware. This code may not make the best choices about how to use the
156 * USB controller, and it can't know all the restrictions that may apply.
157 * Some combinations of driver and hardware won't be able to autoconfigure.
158 *
159 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
160 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
161 * is initialized as if the endpoint were used at full speed. To prevent
162 * the endpoint from being returned by a later autoconfig call, claim it
cc476b42 163 * by assigning ep->claimed to true.
a59d6b91
TB
164 *
165 * On failure, this returns a null endpoint descriptor.
166 */
167struct usb_ep *usb_ep_autoconfig(
168 struct usb_gadget *gadget,
169 struct usb_endpoint_descriptor *desc
170)
171{
172 return usb_ep_autoconfig_ss(gadget, desc, NULL);
173}
dc995fc2 174EXPORT_SYMBOL_GPL(usb_ep_autoconfig);
a59d6b91 175
1da177e4
LT
176/**
177 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
178 * @gadget: device for which autoconfig state will be reset
179 *
180 * Use this for devices where one configuration may need to assign
181 * endpoint resources very differently from the next one. It clears
cc476b42 182 * state such as ep->claimed and the record of assigned endpoints
1da177e4
LT
183 * used by usb_ep_autoconfig().
184 */
28824b18 185void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
1da177e4
LT
186{
187 struct usb_ep *ep;
188
189 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
cc476b42 190 ep->claimed = false;
1da177e4 191 }
e87bb711
SAS
192 gadget->in_epnum = 0;
193 gadget->out_epnum = 0;
1da177e4 194}
dc995fc2 195EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset);
This page took 0.82256 seconds and 5 git commands to generate.