epautoconf.c 10.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
 *
 * Copyright (C) 2004 David Brownell
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/kernel.h>
13
#include <linux/module.h>
L
Linus Torvalds 已提交
14 15 16 17 18 19
#include <linux/types.h>
#include <linux/device.h>

#include <linux/ctype.h>
#include <linux/string.h>

20
#include <linux/usb/ch9.h>
21
#include <linux/usb/gadget.h>
L
Linus Torvalds 已提交
22 23 24

#include "gadget_chips.h"

25
static int
L
Linus Torvalds 已提交
26 27 28
ep_matches (
	struct usb_gadget		*gadget,
	struct usb_ep			*ep,
29 30
	struct usb_endpoint_descriptor	*desc,
	struct usb_ss_ep_comp_descriptor *ep_comp
L
Linus Torvalds 已提交
31 32 33 34
)
{
	u8		type;
	u16		max;
35 36
	int		num_req_streams = 0;

L
Linus Torvalds 已提交
37
	/* endpoint already claimed? */
38
	if (ep->claimed)
L
Linus Torvalds 已提交
39
		return 0;
40

41
	type = usb_endpoint_type(desc);
42 43 44 45 46 47 48 49 50 51 52 53 54 55
	max = 0x7ff & usb_endpoint_maxp(desc);

	if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
		return 0;
	if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
		return 0;

	if (max > ep->maxpacket_limit)
		return 0;

	/* "high bandwidth" works only at high speed */
	if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp(desc) & (3<<11))
		return 0;

56 57 58
	switch (type) {
	case USB_ENDPOINT_XFER_CONTROL:
		/* only support ep0 for portable CONTROL traffic */
L
Linus Torvalds 已提交
59
		return 0;
60 61 62
	case USB_ENDPOINT_XFER_ISOC:
		if (!ep->caps.type_iso)
			return 0;
63 64 65 66 67
		/* ISO:  limit 1023 bytes full speed,
		 * 1024 high/super speed
		 */
		if (!gadget_is_dualspeed(gadget) && max > 1023)
			return 0;
68 69 70 71
		break;
	case USB_ENDPOINT_XFER_BULK:
		if (!ep->caps.type_bulk)
			return 0;
72 73 74 75 76 77 78 79 80
		if (ep_comp && gadget_is_superspeed(gadget)) {
			/* Get the number of required streams from the
			 * EP companion descriptor and see if the EP
			 * matches it
			 */
			num_req_streams = ep_comp->bmAttributes & 0x1f;
			if (num_req_streams > ep->max_streams)
				return 0;
		}
81 82
		break;
	case USB_ENDPOINT_XFER_INT:
83
		/* Bulk endpoints handle interrupt transfers,
84 85 86 87
		 * except the toggle-quirky iso-synch kind
		 */
		if (!ep->caps.type_int && !ep->caps.type_bulk)
			return 0;
88 89 90
		/* INT:  limit 64 bytes full speed,
		 * 1024 high/super speed
		 */
91
		if (!gadget_is_dualspeed(gadget) && max > 64)
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100
			return 0;
		break;
	}

	/* MATCH!! */

	return 1;
}

101
static struct usb_ep *
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110 111 112 113
find_ep (struct usb_gadget *gadget, const char *name)
{
	struct usb_ep	*ep;

	list_for_each_entry (ep, &gadget->ep_list, ep_list) {
		if (0 == strcmp (ep->name, name))
			return ep;
	}
	return NULL;
}

/**
114 115
 * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
 * descriptor and ep companion descriptor
L
Linus Torvalds 已提交
116 117
 * @gadget: The device to which the endpoint must belong.
 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
118 119 120 121 122 123
 *    initialized.  For periodic transfers, the maximum packet
 *    size must also be initialized.  This is modified on
 *    success.
 * @ep_comp: Endpoint companion descriptor, with the required
 *    number of streams. Will be modified when the chosen EP
 *    supports a different number of streams.
L
Linus Torvalds 已提交
124
 *
125 126 127 128 129 130 131 132 133
 * This routine replaces the usb_ep_autoconfig when needed
 * superspeed enhancments. If such enhancemnets are required,
 * the FD should call usb_ep_autoconfig_ss directly and provide
 * the additional ep_comp parameter.
 *
 * By choosing an endpoint to use with the specified descriptor,
 * this routine simplifies writing gadget drivers that work with
 * multiple USB device controllers.  The endpoint would be
 * passed later to usb_ep_enable(), along with some descriptor.
L
Linus Torvalds 已提交
134 135 136 137 138 139
 *
 * That second descriptor won't always be the same as the first one.
 * For example, isochronous endpoints can be autoconfigured for high
 * bandwidth, and then used in several lower bandwidth altsettings.
 * Also, high and full speed descriptors will be different.
 *
140 141 142 143 144
 * Be sure to examine and test the results of autoconfiguration
 * on your hardware.  This code may not make the best choices
 * about how to use the USB controller, and it can't know all
 * the restrictions that may apply. Some combinations of driver
 * and hardware won't be able to autoconfigure.
L
Linus Torvalds 已提交
145 146 147
 *
 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
 * descriptor bEndpointAddress.  For bulk endpoints, the wMaxPacket value
148 149 150 151 152
 * is initialized as if the endpoint were used at full speed and
 * the bmAttribute field in the ep companion descriptor is
 * updated with the assigned number of streams if it is
 * different from the original value. To prevent the endpoint
 * from being returned by a later autoconfig call, claim it by
153
 * assigning ep->claimed to true.
L
Linus Torvalds 已提交
154 155 156
 *
 * On failure, this returns a null endpoint descriptor.
 */
157
struct usb_ep *usb_ep_autoconfig_ss(
L
Linus Torvalds 已提交
158
	struct usb_gadget		*gadget,
159 160
	struct usb_endpoint_descriptor	*desc,
	struct usb_ss_ep_comp_descriptor *ep_comp
L
Linus Torvalds 已提交
161 162 163 164 165 166 167
)
{
	struct usb_ep	*ep;
	u8		type;

	type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;

168 169 170 171 172 173
	if (gadget->ops->match_ep) {
		ep = gadget->ops->match_ep(gadget, desc, ep_comp);
		if (ep)
			goto found_ep;
	}

L
Linus Torvalds 已提交
174 175 176
	/* First, apply chip-specific "best usage" knowledge.
	 * This might make a good usb_gadget_ops hook ...
	 */
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	if (gadget_is_net2280(gadget)) {
		char name[8];

		if (type == USB_ENDPOINT_XFER_INT) {
			/* ep-e, ep-f are PIO with only 64 byte fifos */
			ep = find_ep(gadget, "ep-e");
			if (ep && ep_matches(gadget, ep, desc, ep_comp))
				goto found_ep;
			ep = find_ep(gadget, "ep-f");
			if (ep && ep_matches(gadget, ep, desc, ep_comp))
				goto found_ep;
		}

		/* USB3380: use same address for usb and hardware endpoints */
		snprintf(name, sizeof(name), "ep%d%s", usb_endpoint_num(desc),
				usb_endpoint_dir_in(desc) ? "in" : "out");
		ep = find_ep(gadget, name);
194
		if (ep && ep_matches(gadget, ep, desc, ep_comp))
195
			goto found_ep;
L
Linus Torvalds 已提交
196 197 198
	} else if (gadget_is_goku (gadget)) {
		if (USB_ENDPOINT_XFER_INT == type) {
			/* single buffering is enough */
199 200
			ep = find_ep(gadget, "ep3-bulk");
			if (ep && ep_matches(gadget, ep, desc, ep_comp))
201
				goto found_ep;
L
Linus Torvalds 已提交
202 203 204
		} else if (USB_ENDPOINT_XFER_BULK == type
				&& (USB_DIR_IN & desc->bEndpointAddress)) {
			/* DMA may be available */
205 206 207
			ep = find_ep(gadget, "ep2-bulk");
			if (ep && ep_matches(gadget, ep, desc,
					      ep_comp))
208
				goto found_ep;
L
Linus Torvalds 已提交
209 210
		}

211
#ifdef CONFIG_BLACKFIN
212
	} else if (gadget_is_musbhdrc(gadget)) {
213 214 215 216 217 218
		if ((USB_ENDPOINT_XFER_BULK == type) ||
		    (USB_ENDPOINT_XFER_ISOC == type)) {
			if (USB_DIR_IN & desc->bEndpointAddress)
				ep = find_ep (gadget, "ep5in");
			else
				ep = find_ep (gadget, "ep6out");
219 220 221 222 223
		} else if (USB_ENDPOINT_XFER_INT == type) {
			if (USB_DIR_IN & desc->bEndpointAddress)
				ep = find_ep(gadget, "ep1in");
			else
				ep = find_ep(gadget, "ep2out");
224 225
		} else
			ep = NULL;
226
		if (ep && ep_matches(gadget, ep, desc, ep_comp))
227
			goto found_ep;
228
#endif
L
Linus Torvalds 已提交
229 230
	}

231
	/* Second, look at endpoints until an unclaimed one looks usable */
L
Linus Torvalds 已提交
232
	list_for_each_entry (ep, &gadget->ep_list, ep_list) {
233
		if (ep_matches(gadget, ep, desc, ep_comp))
234
			goto found_ep;
L
Linus Torvalds 已提交
235 236 237 238
	}

	/* Fail */
	return NULL;
239
found_ep:
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

	/*
	 * If the protocol driver hasn't yet decided on wMaxPacketSize
	 * and wants to know the maximum possible, provide the info.
	 */
	if (desc->wMaxPacketSize == 0)
		desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket_limit);

	/* report address */
	desc->bEndpointAddress &= USB_DIR_IN;
	if (isdigit(ep->name[2])) {
		u8 num = simple_strtoul(&ep->name[2], NULL, 10);
		desc->bEndpointAddress |= num;
	} else if (desc->bEndpointAddress & USB_DIR_IN) {
		if (++gadget->in_epnum > 15)
			return NULL;
		desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum;
	} else {
		if (++gadget->out_epnum > 15)
			return NULL;
		desc->bEndpointAddress |= gadget->out_epnum;
	}

	/* report (variable) full speed bulk maxpacket */
	if ((type == USB_ENDPOINT_XFER_BULK) && !ep_comp) {
		int size = ep->maxpacket_limit;

		/* min() doesn't work on bitfields with gcc-3.5 */
		if (size > 64)
			size = 64;
		desc->wMaxPacketSize = cpu_to_le16(size);
	}

	ep->address = desc->bEndpointAddress;
274 275
	ep->desc = NULL;
	ep->comp_desc = NULL;
276
	ep->claimed = true;
277
	return ep;
L
Linus Torvalds 已提交
278
}
279
EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss);
L
Linus Torvalds 已提交
280

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/**
 * usb_ep_autoconfig() - choose an endpoint matching the
 * descriptor
 * @gadget: The device to which the endpoint must belong.
 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
 *	initialized.  For periodic transfers, the maximum packet
 *	size must also be initialized.  This is modified on success.
 *
 * By choosing an endpoint to use with the specified descriptor, this
 * routine simplifies writing gadget drivers that work with multiple
 * USB device controllers.  The endpoint would be passed later to
 * usb_ep_enable(), along with some descriptor.
 *
 * That second descriptor won't always be the same as the first one.
 * For example, isochronous endpoints can be autoconfigured for high
 * bandwidth, and then used in several lower bandwidth altsettings.
 * Also, high and full speed descriptors will be different.
 *
 * Be sure to examine and test the results of autoconfiguration on your
 * hardware.  This code may not make the best choices about how to use the
 * USB controller, and it can't know all the restrictions that may apply.
 * Some combinations of driver and hardware won't be able to autoconfigure.
 *
 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
 * descriptor bEndpointAddress.  For bulk endpoints, the wMaxPacket value
 * is initialized as if the endpoint were used at full speed.  To prevent
 * the endpoint from being returned by a later autoconfig call, claim it
308
 * by assigning ep->claimed to true.
309 310 311 312 313 314 315 316 317 318
 *
 * On failure, this returns a null endpoint descriptor.
 */
struct usb_ep *usb_ep_autoconfig(
	struct usb_gadget		*gadget,
	struct usb_endpoint_descriptor	*desc
)
{
	return usb_ep_autoconfig_ss(gadget, desc, NULL);
}
319
EXPORT_SYMBOL_GPL(usb_ep_autoconfig);
320

L
Linus Torvalds 已提交
321 322 323 324 325 326
/**
 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
 * @gadget: device for which autoconfig state will be reset
 *
 * Use this for devices where one configuration may need to assign
 * endpoint resources very differently from the next one.  It clears
327
 * state such as ep->claimed and the record of assigned endpoints
L
Linus Torvalds 已提交
328 329
 * used by usb_ep_autoconfig().
 */
330
void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
L
Linus Torvalds 已提交
331 332 333 334
{
	struct usb_ep	*ep;

	list_for_each_entry (ep, &gadget->ep_list, ep_list) {
335
		ep->claimed = false;
L
Linus Torvalds 已提交
336
	}
337 338
	gadget->in_epnum = 0;
	gadget->out_epnum = 0;
L
Linus Torvalds 已提交
339
}
340
EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset);