epautoconf.c 7.2 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 25
 * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
 * descriptor and ep companion descriptor
L
Linus Torvalds 已提交
26 27
 * @gadget: The device to which the endpoint must belong.
 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
28 29 30 31 32 33
 *    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 已提交
34
 *
35 36 37 38 39 40 41 42 43
 * 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 已提交
44 45 46 47 48 49
 *
 * 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.
 *
50 51 52 53 54
 * 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 已提交
55
 *
56
 * On success, this returns an claimed usb_ep, and modifies the endpoint
L
Linus Torvalds 已提交
57
 * descriptor bEndpointAddress.  For bulk endpoints, the wMaxPacket value
58 59 60 61
 * 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
62
 * from being returned by a later autoconfig call, claims it by
63
 * assigning ep->claimed to true.
L
Linus Torvalds 已提交
64 65 66
 *
 * On failure, this returns a null endpoint descriptor.
 */
67
struct usb_ep *usb_ep_autoconfig_ss(
L
Linus Torvalds 已提交
68
	struct usb_gadget		*gadget,
69 70
	struct usb_endpoint_descriptor	*desc,
	struct usb_ss_ep_comp_descriptor *ep_comp
L
Linus Torvalds 已提交
71 72 73 74 75 76 77
)
{
	struct usb_ep	*ep;
	u8		type;

	type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;

78 79 80 81 82 83
	if (gadget->ops->match_ep) {
		ep = gadget->ops->match_ep(gadget, desc, ep_comp);
		if (ep)
			goto found_ep;
	}

84
	/* Second, look at endpoints until an unclaimed one looks usable */
L
Linus Torvalds 已提交
85
	list_for_each_entry (ep, &gadget->ep_list, ep_list) {
86
		if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp))
87
			goto found_ep;
L
Linus Torvalds 已提交
88 89 90 91
	}

	/* Fail */
	return NULL;
92
found_ep:
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

	/*
	 * 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;
127 128
	ep->desc = NULL;
	ep->comp_desc = NULL;
129
	ep->claimed = true;
130
	return ep;
L
Linus Torvalds 已提交
131
}
132
EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss);
L
Linus Torvalds 已提交
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/**
 * 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.
 *
157
 * On success, this returns an claimed usb_ep, and modifies the endpoint
158 159
 * descriptor bEndpointAddress.  For bulk endpoints, the wMaxPacket value
 * is initialized as if the endpoint were used at full speed.  To prevent
160
 * the endpoint from being returned by a later autoconfig call, claims it
161
 * by assigning ep->claimed to true.
162 163 164 165 166 167 168 169 170 171
 *
 * 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);
}
172
EXPORT_SYMBOL_GPL(usb_ep_autoconfig);
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/**
 * usb_ep_autoconfig_release - releases endpoint and set it to initial state
 * @ep: endpoint which should be released
 *
 * This function can be used during function bind for endpoints obtained
 * from usb_ep_autoconfig(). It unclaims endpoint claimed by
 * usb_ep_autoconfig() to make it available for other functions. Endpoint
 * which was released is no longer invalid and shouldn't be used in
 * context of function which released it.
 */
void usb_ep_autoconfig_release(struct usb_ep *ep)
{
	ep->claimed = false;
	ep->driver_data = NULL;
}
EXPORT_SYMBOL_GPL(usb_ep_autoconfig_release);

L
Linus Torvalds 已提交
191 192 193 194 195 196
/**
 * 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
197
 * state such as ep->claimed and the record of assigned endpoints
L
Linus Torvalds 已提交
198 199
 * used by usb_ep_autoconfig().
 */
200
void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
L
Linus Torvalds 已提交
201 202 203 204
{
	struct usb_ep	*ep;

	list_for_each_entry (ep, &gadget->ep_list, ep_list) {
205
		ep->claimed = false;
206
		ep->driver_data = NULL;
L
Linus Torvalds 已提交
207
	}
208 209
	gadget->in_epnum = 0;
	gadget->out_epnum = 0;
L
Linus Torvalds 已提交
210
}
211
EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset);