3 * Denis Peter, MPL AG Switzerland
5 * SPDX-License-Identifier: GPL-2.0+
6 * Note: Part of this code has been derived from linux
13 #include <linux/usb/ch9.h>
14 #include <asm/cache.h>
18 * The EHCI spec says that we must align to at least 32 bytes. However,
19 * some platforms require larger alignment.
21 #if ARCH_DMA_MINALIGN > 32
22 #define USB_DMA_MINALIGN ARCH_DMA_MINALIGN
24 #define USB_DMA_MINALIGN 32
27 /* Everything is aribtrary */
28 #define USB_ALTSETTINGALLOC 4
29 #define USB_MAXALTSETTING 128 /* Hard limit */
31 #define USB_MAX_DEVICE 32
32 #define USB_MAXCONFIG 8
33 #define USB_MAXINTERFACES 8
34 #define USB_MAXENDPOINTS 16
35 #define USB_MAXCHILDREN 8 /* This is arbitrary */
36 #define USB_MAX_HUB 16
38 #define USB_CNTL_TIMEOUT 100 /* 100ms timeout */
41 * This is the timeout to allow for submitting an urb in ms. We allow more
42 * time for a BULK device to react - some are slow.
44 #define USB_TIMEOUT_MS(pipe) (usb_pipebulk(pipe) ? 5000 : 1000)
46 /* device request (setup) */
48 unsigned char requesttype;
49 unsigned char request;
52 unsigned short length;
53 } __attribute__ ((packed));
56 struct usb_interface {
57 struct usb_interface_descriptor desc;
59 unsigned char no_of_ep;
60 unsigned char num_altsetting;
61 unsigned char act_altsetting;
63 struct usb_endpoint_descriptor ep_desc[USB_MAXENDPOINTS];
65 * Super Speed Device will have Super Speed Endpoint
66 * Companion Descriptor (section 9.6.7 of usb 3.0 spec)
67 * Revision 1.0 June 6th 2011
69 struct usb_ss_ep_comp_descriptor ss_ep_comp_desc[USB_MAXENDPOINTS];
70 } __attribute__ ((packed));
72 /* Configuration information.. */
74 struct usb_config_descriptor desc;
76 unsigned char no_of_if; /* number of interfaces */
77 struct usb_interface if_desc[USB_MAXINTERFACES];
78 } __attribute__ ((packed));
81 /* Maximum packet size; encoded as 0,1,2,3 = 8,16,32,64 */
89 int devnum; /* Device number on USB bus */
90 int speed; /* full/low/high */
91 char mf[32]; /* manufacturer */
92 char prod[32]; /* product */
93 char serial[32]; /* serial number */
95 /* Maximum packet size; one of: PACKET_SIZE_* */
97 /* one bit for each endpoint ([0] = IN, [1] = OUT) */
98 unsigned int toggle[2];
99 /* endpoint halts; one bit per endpoint # & direction;
100 * [0] = IN, [1] = OUT
102 unsigned int halted[2];
103 int epmaxpacketin[16]; /* INput endpoint specific maximums */
104 int epmaxpacketout[16]; /* OUTput endpoint specific maximums */
106 int configno; /* selected config number */
107 /* Device Descriptor */
108 struct usb_device_descriptor descriptor
109 __attribute__((aligned(ARCH_DMA_MINALIGN)));
110 struct usb_config config; /* config descriptor */
112 int have_langid; /* whether string_langid is valid yet */
113 int string_langid; /* language ID for strings */
114 int (*irq_handle)(struct usb_device *dev);
115 unsigned long irq_status;
116 int irq_act_len; /* transfered bytes */
119 * Child devices - if this is a hub device
120 * Each instance needs its own set of data structures.
122 unsigned long status;
123 int act_len; /* transfered bytes */
124 int maxchild; /* Number of ports if hub */
126 struct usb_device *parent;
127 struct usb_device *children[USB_MAXCHILDREN];
129 void *controller; /* hardware controller private data */
130 /* slot_id - for xHCI enabled devices */
131 unsigned int slot_id;
137 * You can initialize platform's USB host or device
138 * ports by passing this enum as an argument to
146 /**********************************************************************
147 * this is how the lowlevel part communicate with the outer world
150 #if defined(CONFIG_USB_UHCI) || defined(CONFIG_USB_OHCI) || \
151 defined(CONFIG_USB_EHCI) || defined(CONFIG_USB_OHCI_NEW) || \
152 defined(CONFIG_USB_SL811HS) || defined(CONFIG_USB_ISP116X_HCD) || \
153 defined(CONFIG_USB_R8A66597_HCD) || defined(CONFIG_USB_DAVINCI) || \
154 defined(CONFIG_USB_OMAP3) || defined(CONFIG_USB_DA8XX) || \
155 defined(CONFIG_USB_BLACKFIN) || defined(CONFIG_USB_AM35X) || \
156 defined(CONFIG_USB_MUSB_DSPS) || defined(CONFIG_USB_MUSB_AM35X) || \
157 defined(CONFIG_USB_MUSB_OMAP2PLUS) || defined(CONFIG_USB_XHCI) || \
158 defined(CONFIG_USB_DWC2)
160 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller);
161 int usb_lowlevel_stop(int index);
163 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
164 void *buffer, int transfer_len);
165 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
166 int transfer_len, struct devrequest *setup);
167 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
168 int transfer_len, int interval);
170 #ifdef CONFIG_USB_EHCI /* Only the ehci code has pollable int support */
171 struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe,
172 int queuesize, int elementsize, void *buffer);
173 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue);
174 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue);
178 #define USB_UHCI_VEND_ID 0x8086
179 #define USB_UHCI_DEV_ID 0x7112
182 * PXA25x can only act as USB device. There are drivers
183 * which works with USB CDC gadgets implementations.
184 * Some of them have common routines which can be used
185 * in boards init functions e.g. udc_disconnect() used for
186 * forced device disconnection from host.
188 #elif defined(CONFIG_USB_GADGET_PXA2XX)
190 extern void udc_disconnect(void);
195 * board-specific hardware initialization, called by
196 * usb drivers and u-boot commands
198 * @param index USB controller number
199 * @param init initializes controller as USB host or device
201 int board_usb_init(int index, enum usb_init_type init);
204 * can be used to clean up after failed USB initialization attempt
205 * vide: board_usb_init()
207 * @param index USB controller number for selective cleanup
208 * @param init usb_init_type passed to board_usb_init()
210 int board_usb_cleanup(int index, enum usb_init_type init);
212 #ifdef CONFIG_USB_STORAGE
214 #define USB_MAX_STOR_DEV 5
215 block_dev_desc_t *usb_stor_get_dev(int index);
216 int usb_stor_scan(int mode);
217 int usb_stor_info(void);
221 #ifdef CONFIG_USB_HOST_ETHER
223 #define USB_MAX_ETH_DEV 5
224 int usb_host_eth_scan(int mode);
228 #ifdef CONFIG_USB_KEYBOARD
230 int drv_usb_kbd_init(void);
231 int usb_kbd_deregister(int force);
235 int usb_init(void); /* initialize the USB Controller */
236 int usb_stop(void); /* stop the USB Controller */
239 int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol);
240 int usb_set_idle(struct usb_device *dev, int ifnum, int duration,
242 struct usb_device *usb_get_dev_index(int index);
243 int usb_control_msg(struct usb_device *dev, unsigned int pipe,
244 unsigned char request, unsigned char requesttype,
245 unsigned short value, unsigned short index,
246 void *data, unsigned short size, int timeout);
247 int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
248 void *data, int len, int *actual_length, int timeout);
249 int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
250 void *buffer, int transfer_len, int interval);
251 int usb_disable_asynch(int disable);
252 int usb_maxpacket(struct usb_device *dev, unsigned long pipe);
253 int usb_get_configuration_no(struct usb_device *dev, unsigned char *buffer,
255 int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
256 unsigned char id, void *buf, int size);
257 int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
258 unsigned char type, unsigned char id, void *buf,
260 int usb_clear_halt(struct usb_device *dev, int pipe);
261 int usb_string(struct usb_device *dev, int index, char *buf, size_t size);
262 int usb_set_interface(struct usb_device *dev, int interface, int alternate);
264 /* big endian -> little endian conversion */
265 /* some CPUs are already little endian e.g. the ARM920T */
266 #define __swap_16(x) \
267 ({ unsigned short x_ = (unsigned short)x; \
269 ((x_ & 0x00FFU) << 8) | ((x_ & 0xFF00U) >> 8)); \
271 #define __swap_32(x) \
272 ({ unsigned long x_ = (unsigned long)x; \
274 ((x_ & 0x000000FFUL) << 24) | \
275 ((x_ & 0x0000FF00UL) << 8) | \
276 ((x_ & 0x00FF0000UL) >> 8) | \
277 ((x_ & 0xFF000000UL) >> 24)); \
280 #ifdef __LITTLE_ENDIAN
281 # define swap_16(x) (x)
282 # define swap_32(x) (x)
284 # define swap_16(x) __swap_16(x)
285 # define swap_32(x) __swap_32(x)
289 * Calling this entity a "pipe" is glorifying it. A USB pipe
290 * is something embarrassingly simple: it basically consists
291 * of the following information:
292 * - device number (7 bits)
293 * - endpoint number (4 bits)
294 * - current Data0/1 state (1 bit)
295 * - direction (1 bit)
297 * - max packet size (2 bits: 8, 16, 32 or 64)
298 * - pipe type (2 bits: control, interrupt, bulk, isochronous)
300 * That's 18 bits. Really. Nothing more. And the USB people have
301 * documented these eighteen bits as some kind of glorious
302 * virtual data structure.
304 * Let's not fall in that trap. We'll just encode it as a simple
305 * unsigned int. The encoding is:
307 * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64)
308 * - direction: bit 7 (0 = Host-to-Device [Out],
309 * (1 = Device-to-Host [In])
310 * - device: bits 8-14
311 * - endpoint: bits 15-18
313 * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt,
314 * 10 = control, 11 = bulk)
316 * Why? Because it's arbitrary, and whatever encoding we select is really
317 * up to us. This one happens to share a lot of bit positions with the UHCI
318 * specification, so that much of the uhci driver can just mask the bits
321 /* Create various pipes... */
322 #define create_pipe(dev,endpoint) \
323 (((dev)->devnum << 8) | ((endpoint) << 15) | \
324 (dev)->maxpacketsize)
325 #define default_pipe(dev) ((dev)->speed << 26)
327 #define usb_sndctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
328 create_pipe(dev, endpoint))
329 #define usb_rcvctrlpipe(dev, endpoint) ((PIPE_CONTROL << 30) | \
330 create_pipe(dev, endpoint) | \
332 #define usb_sndisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
333 create_pipe(dev, endpoint))
334 #define usb_rcvisocpipe(dev, endpoint) ((PIPE_ISOCHRONOUS << 30) | \
335 create_pipe(dev, endpoint) | \
337 #define usb_sndbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
338 create_pipe(dev, endpoint))
339 #define usb_rcvbulkpipe(dev, endpoint) ((PIPE_BULK << 30) | \
340 create_pipe(dev, endpoint) | \
342 #define usb_sndintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
343 create_pipe(dev, endpoint))
344 #define usb_rcvintpipe(dev, endpoint) ((PIPE_INTERRUPT << 30) | \
345 create_pipe(dev, endpoint) | \
347 #define usb_snddefctrl(dev) ((PIPE_CONTROL << 30) | \
349 #define usb_rcvdefctrl(dev) ((PIPE_CONTROL << 30) | \
350 default_pipe(dev) | \
353 /* The D0/D1 toggle bits */
354 #define usb_gettoggle(dev, ep, out) (((dev)->toggle[out] >> ep) & 1)
355 #define usb_dotoggle(dev, ep, out) ((dev)->toggle[out] ^= (1 << ep))
356 #define usb_settoggle(dev, ep, out, bit) ((dev)->toggle[out] = \
357 ((dev)->toggle[out] & \
358 ~(1 << ep)) | ((bit) << ep))
360 /* Endpoint halt control/status */
361 #define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1)
362 #define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep)))
363 #define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))
364 #define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))
366 #define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : \
369 #define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1)
370 #define usb_pipein(pipe) (((pipe) >> 7) & 1)
371 #define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)
372 #define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff)
373 #define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)
374 #define usb_pipedata(pipe) (((pipe) >> 19) & 1)
375 #define usb_pipetype(pipe) (((pipe) >> 30) & 3)
376 #define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)
377 #define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)
378 #define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)
379 #define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)
381 #define usb_pipe_ep_index(pipe) \
382 usb_pipecontrol(pipe) ? (usb_pipeendpoint(pipe) * 2) : \
383 ((usb_pipeendpoint(pipe) * 2) - \
384 (usb_pipein(pipe) ? 0 : 1))
386 /*************************************************************************
389 struct usb_port_status {
390 unsigned short wPortStatus;
391 unsigned short wPortChange;
392 } __attribute__ ((packed));
394 struct usb_hub_status {
395 unsigned short wHubStatus;
396 unsigned short wHubChange;
397 } __attribute__ ((packed));
401 struct usb_hub_descriptor {
402 unsigned char bLength;
403 unsigned char bDescriptorType;
404 unsigned char bNbrPorts;
405 unsigned short wHubCharacteristics;
406 unsigned char bPwrOn2PwrGood;
407 unsigned char bHubContrCurrent;
408 unsigned char DeviceRemovable[(USB_MAXCHILDREN+1+7)/8];
409 unsigned char PortPowerCtrlMask[(USB_MAXCHILDREN+1+7)/8];
410 /* DeviceRemovable and PortPwrCtrlMask want to be variable-length
411 bitmaps that hold max 255 entries. (bit0 is ignored) */
412 } __attribute__ ((packed));
415 struct usb_hub_device {
416 struct usb_device *pusb_dev;
417 struct usb_hub_descriptor desc;
420 int usb_hub_probe(struct usb_device *dev, int ifnum);
421 void usb_hub_reset(void);
422 int hub_port_reset(struct usb_device *dev, int port,
423 unsigned short *portstat);
425 struct usb_device *usb_alloc_new_device(void *controller);
427 int usb_new_device(struct usb_device *dev);
428 void usb_free_device(void);
429 int usb_alloc_device(struct usb_device *dev);