xref: /openbmc/u-boot/include/usbdevice.h (revision cbd2fba1)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2003
4  * Gerry Hamel, geh@ti.com, Texas Instruments
5  *
6  * Based on linux/drivers/usbd/usbd.h
7  *
8  * Copyright (c) 2000, 2001, 2002 Lineo
9  * Copyright (c) 2001 Hewlett Packard
10  *
11  * By:
12  *	Stuart Lynne <sl@lineo.com>,
13  *	Tom Rushworth <tbr@lineo.com>,
14  *	Bruce Balden <balden@lineo.com>
15  */
16 
17 #ifndef __USBDCORE_H__
18 #define __USBDCORE_H__
19 
20 #include <common.h>
21 #include "usbdescriptors.h"
22 
23 
24 #define MAX_URBS_QUEUED 5
25 
26 
27 #if 1
28 #define usberr(fmt,args...) serial_printf("ERROR: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
29 #else
30 #define usberr(fmt,args...) do{}while(0)
31 #endif
32 
33 #if 0
34 #define usbdbg(fmt,args...) serial_printf("debug: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
35 #else
36 #define usbdbg(fmt,args...) do{}while(0)
37 #endif
38 
39 #if 0
40 #define usbinfo(fmt,args...) serial_printf("info: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
41 #else
42 #define usbinfo(fmt,args...) do{}while(0)
43 #endif
44 
45 #ifndef le16_to_cpu
46 #define le16_to_cpu(x)	(x)
47 #endif
48 
49 #ifndef inb
50 #define inb(p)	     (*(volatile u8*)(p))
51 #endif
52 
53 #ifndef outb
54 #define outb(val,p)  (*(volatile u8*)(p) = (val))
55 #endif
56 
57 #ifndef inw
58 #define inw(p)	     (*(volatile u16*)(p))
59 #endif
60 
61 #ifndef outw
62 #define outw(val,p)  (*(volatile u16*)(p) = (val))
63 #endif
64 
65 #ifndef inl
66 #define inl(p)	     (*(volatile u32*)(p))
67 #endif
68 
69 #ifndef outl
70 #define outl(val,p)  (*(volatile u32*)(p) = (val))
71 #endif
72 
73 #ifndef insw
74 #define insw(p,to,len)	   mmio_insw(p,to,len)
75 #endif
76 
77 #ifndef outsw
78 #define outsw(p,from,len)  mmio_outsw(p,from,len)
79 #endif
80 
81 #ifndef insb
82 #define insb(p,to,len)	   mmio_insb(p,to,len)
83 #endif
84 
85 #ifndef mmio_insw
86 #define mmio_insw(r,b,l)	({	int __i ;  \
87 					u16 *__b2;  \
88 					__b2 = (u16 *) b;  \
89 					for (__i = 0; __i < l; __i++) {	 \
90 					  *(__b2 + __i) = inw(r);  \
91 					};  \
92 				})
93 #endif
94 
95 #ifndef mmio_outsw
96 #define mmio_outsw(r,b,l)	({	int __i; \
97 					u16 *__b2; \
98 					__b2 = (u16 *) b; \
99 					for (__i = 0; __i < l; __i++) { \
100 					    outw( *(__b2 + __i), r); \
101 					} \
102 				})
103 #endif
104 
105 #ifndef mmio_insb
106 #define mmio_insb(r,b,l)	({	int __i ;  \
107 					u8 *__b2;  \
108 					__b2 = (u8 *) b;  \
109 					for (__i = 0; __i < l; __i++) {	 \
110 					  *(__b2 + __i) = inb(r);  \
111 					};  \
112 				})
113 #endif
114 
115 /*
116  * Structure member address manipulation macros.
117  * These are used by client code (code using the urb_link routines), since
118  * the urb_link structure is embedded in the client data structures.
119  *
120  * Note: a macro offsetof equivalent to member_offset is defined in stddef.h
121  *	 but this is kept here for the sake of portability.
122  *
123  * p2surround returns a pointer to the surrounding structure given
124  * type of the surrounding structure, the name memb of the structure
125  * member pointed at by ptr.  For example, if you have:
126  *
127  *	struct foo {
128  *	    int x;
129  *	    float y;
130  *	    char z;
131  *	} thingy;
132  *
133  *	char *cp = &thingy.z;
134  *
135  * then
136  *
137  *	&thingy == p2surround(struct foo, z, cp)
138  *
139  * Clear?
140  */
141 #define _cv_(ptr)		  ((char*)(void*)(ptr))
142 #define member_offset(type,memb)  (_cv_(&(((type*)0)->memb))-(char*)0)
143 #define p2surround(type,memb,ptr) ((type*)(void*)(_cv_(ptr)-member_offset(type,memb)))
144 
145 struct urb;
146 
147 struct usb_endpoint_instance;
148 struct usb_interface_instance;
149 struct usb_configuration_instance;
150 struct usb_device_instance;
151 struct usb_bus_instance;
152 
153 /*
154  * Device and/or Interface Class codes
155  */
156 #define USB_CLASS_PER_INTERFACE		0	/* for DeviceClass */
157 #define USB_CLASS_AUDIO			1
158 #define USB_CLASS_COMM			2
159 #define USB_CLASS_HID			3
160 #define USB_CLASS_PHYSICAL		5
161 #define USB_CLASS_PRINTER		7
162 #define USB_CLASS_MASS_STORAGE		8
163 #define USB_CLASS_HUB			9
164 #define USB_CLASS_DATA			10
165 #define USB_CLASS_APP_SPEC		0xfe
166 #define USB_CLASS_VENDOR_SPEC		0xff
167 
168 /*
169  * USB types
170  */
171 #define USB_TYPE_STANDARD		(0x00 << 5)
172 #define USB_TYPE_CLASS			(0x01 << 5)
173 #define USB_TYPE_VENDOR			(0x02 << 5)
174 #define USB_TYPE_RESERVED		(0x03 << 5)
175 
176 /*
177  * USB recipients
178  */
179 #define USB_RECIP_DEVICE		0x00
180 #define USB_RECIP_INTERFACE		0x01
181 #define USB_RECIP_ENDPOINT		0x02
182 #define USB_RECIP_OTHER			0x03
183 
184 /*
185  * USB directions
186  */
187 #define USB_DIR_OUT			0
188 #define USB_DIR_IN			0x80
189 
190 /*
191  * Descriptor types
192  */
193 #define USB_DT_DEVICE			0x01
194 #define USB_DT_CONFIG			0x02
195 #define USB_DT_STRING			0x03
196 #define USB_DT_INTERFACE		0x04
197 #define USB_DT_ENDPOINT			0x05
198 
199 #if defined(CONFIG_USBD_HS)
200 #define USB_DT_QUAL			0x06
201 #endif
202 
203 #define USB_DT_HID			(USB_TYPE_CLASS | 0x01)
204 #define USB_DT_REPORT			(USB_TYPE_CLASS | 0x02)
205 #define USB_DT_PHYSICAL			(USB_TYPE_CLASS | 0x03)
206 #define USB_DT_HUB			(USB_TYPE_CLASS | 0x09)
207 
208 /*
209  * Descriptor sizes per descriptor type
210  */
211 #define USB_DT_DEVICE_SIZE		18
212 #define USB_DT_CONFIG_SIZE		9
213 #define USB_DT_INTERFACE_SIZE		9
214 #define USB_DT_ENDPOINT_SIZE		7
215 #define USB_DT_ENDPOINT_AUDIO_SIZE	9	/* Audio extension */
216 #define USB_DT_HUB_NONVAR_SIZE		7
217 #define USB_DT_HID_SIZE			9
218 
219 /*
220  * Endpoints
221  */
222 #define USB_ENDPOINT_NUMBER_MASK	0x0f	/* in bEndpointAddress */
223 #define USB_ENDPOINT_DIR_MASK		0x80
224 
225 #define USB_ENDPOINT_XFERTYPE_MASK	0x03	/* in bmAttributes */
226 #define USB_ENDPOINT_XFER_CONTROL	0
227 #define USB_ENDPOINT_XFER_ISOC		1
228 #define USB_ENDPOINT_XFER_BULK		2
229 #define USB_ENDPOINT_XFER_INT		3
230 
231 /*
232  * USB Packet IDs (PIDs)
233  */
234 #define USB_PID_UNDEF_0			       0xf0
235 #define USB_PID_OUT			       0xe1
236 #define USB_PID_ACK			       0xd2
237 #define USB_PID_DATA0			       0xc3
238 #define USB_PID_PING			       0xb4	/* USB 2.0 */
239 #define USB_PID_SOF			       0xa5
240 #define USB_PID_NYET			       0x96	/* USB 2.0 */
241 #define USB_PID_DATA2			       0x87	/* USB 2.0 */
242 #define USB_PID_SPLIT			       0x78	/* USB 2.0 */
243 #define USB_PID_IN			       0x69
244 #define USB_PID_NAK			       0x5a
245 #define USB_PID_DATA1			       0x4b
246 #define USB_PID_PREAMBLE		       0x3c	/* Token mode */
247 #define USB_PID_ERR			       0x3c	/* USB 2.0: handshake mode */
248 #define USB_PID_SETUP			       0x2d
249 #define USB_PID_STALL			       0x1e
250 #define USB_PID_MDATA			       0x0f	/* USB 2.0 */
251 
252 /*
253  * Standard requests
254  */
255 #define USB_REQ_GET_STATUS		0x00
256 #define USB_REQ_CLEAR_FEATURE		0x01
257 #define USB_REQ_SET_FEATURE		0x03
258 #define USB_REQ_SET_ADDRESS		0x05
259 #define USB_REQ_GET_DESCRIPTOR		0x06
260 #define USB_REQ_SET_DESCRIPTOR		0x07
261 #define USB_REQ_GET_CONFIGURATION	0x08
262 #define USB_REQ_SET_CONFIGURATION	0x09
263 #define USB_REQ_GET_INTERFACE		0x0A
264 #define USB_REQ_SET_INTERFACE		0x0B
265 #define USB_REQ_SYNCH_FRAME		0x0C
266 
267 #define USBD_DEVICE_REQUESTS(x) (((unsigned int)x <= USB_REQ_SYNCH_FRAME) ? usbd_device_requests[x] : "UNKNOWN")
268 
269 /*
270  * HID requests
271  */
272 #define USB_REQ_GET_REPORT		0x01
273 #define USB_REQ_GET_IDLE		0x02
274 #define USB_REQ_GET_PROTOCOL		0x03
275 #define USB_REQ_SET_REPORT		0x09
276 #define USB_REQ_SET_IDLE		0x0A
277 #define USB_REQ_SET_PROTOCOL		0x0B
278 
279 
280 /*
281  * USB Spec Release number
282  */
283 
284 #if defined(CONFIG_USBD_HS)
285 #define USB_BCD_VERSION			0x0200
286 #else
287 #define USB_BCD_VERSION			0x0110
288 #endif
289 
290 
291 /*
292  * Device Requests	(c.f Table 9-2)
293  */
294 
295 #define USB_REQ_DIRECTION_MASK		0x80
296 #define USB_REQ_TYPE_MASK		0x60
297 #define USB_REQ_RECIPIENT_MASK		0x1f
298 
299 #define USB_REQ_DEVICE2HOST		0x80
300 #define USB_REQ_HOST2DEVICE		0x00
301 
302 #define USB_REQ_TYPE_STANDARD		0x00
303 #define USB_REQ_TYPE_CLASS		0x20
304 #define USB_REQ_TYPE_VENDOR		0x40
305 
306 #define USB_REQ_RECIPIENT_DEVICE	0x00
307 #define USB_REQ_RECIPIENT_INTERFACE	0x01
308 #define USB_REQ_RECIPIENT_ENDPOINT	0x02
309 #define USB_REQ_RECIPIENT_OTHER		0x03
310 
311 /*
312  * get status bits
313  */
314 
315 #define USB_STATUS_SELFPOWERED		0x01
316 #define USB_STATUS_REMOTEWAKEUP		0x02
317 
318 #define USB_STATUS_HALT			0x01
319 
320 /*
321  * descriptor types
322  */
323 
324 #define USB_DESCRIPTOR_TYPE_DEVICE			0x01
325 #define USB_DESCRIPTOR_TYPE_CONFIGURATION		0x02
326 #define USB_DESCRIPTOR_TYPE_STRING			0x03
327 #define USB_DESCRIPTOR_TYPE_INTERFACE			0x04
328 #define USB_DESCRIPTOR_TYPE_ENDPOINT			0x05
329 #define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER		0x06
330 #define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION	0x07
331 #define USB_DESCRIPTOR_TYPE_INTERFACE_POWER		0x08
332 #define USB_DESCRIPTOR_TYPE_HID				0x21
333 #define USB_DESCRIPTOR_TYPE_REPORT			0x22
334 
335 #define USBD_DEVICE_DESCRIPTORS(x) (((unsigned int)x <= USB_DESCRIPTOR_TYPE_INTERFACE_POWER) ? \
336 		usbd_device_descriptors[x] : "UNKNOWN")
337 
338 /*
339  * standard feature selectors
340  */
341 #define USB_ENDPOINT_HALT		0x00
342 #define USB_DEVICE_REMOTE_WAKEUP	0x01
343 #define USB_TEST_MODE			0x02
344 
345 
346 /* USB Requests
347  *
348  */
349 
350 struct usb_device_request {
351 	u8 bmRequestType;
352 	u8 bRequest;
353 	u16 wValue;
354 	u16 wIndex;
355 	u16 wLength;
356 } __attribute__ ((packed));
357 
358 
359 /* USB Status
360  *
361  */
362 typedef enum urb_send_status {
363 	SEND_IN_PROGRESS,
364 	SEND_FINISHED_OK,
365 	SEND_FINISHED_ERROR,
366 	RECV_READY,
367 	RECV_OK,
368 	RECV_ERROR
369 } urb_send_status_t;
370 
371 /*
372  * Device State (c.f USB Spec 2.0 Figure 9-1)
373  *
374  * What state the usb device is in.
375  *
376  * Note the state does not change if the device is suspended, we simply set a
377  * flag to show that it is suspended.
378  *
379  */
380 typedef enum usb_device_state {
381 	STATE_INIT,		/* just initialized */
382 	STATE_CREATED,		/* just created */
383 	STATE_ATTACHED,		/* we are attached */
384 	STATE_POWERED,		/* we have seen power indication (electrical bus signal) */
385 	STATE_DEFAULT,		/* we been reset */
386 	STATE_ADDRESSED,	/* we have been addressed (in default configuration) */
387 	STATE_CONFIGURED,	/* we have seen a set configuration device command */
388 	STATE_UNKNOWN,		/* destroyed */
389 } usb_device_state_t;
390 
391 #define USBD_DEVICE_STATE(x) (((unsigned int)x <= STATE_UNKNOWN) ? usbd_device_states[x] : "UNKNOWN")
392 
393 /*
394  * Device status
395  *
396  * Overall state
397  */
398 typedef enum usb_device_status {
399 	USBD_OPENING,		/* we are currently opening */
400 	USBD_OK,		/* ok to use */
401 	USBD_SUSPENDED,		/* we are currently suspended */
402 	USBD_CLOSING,		/* we are currently closing */
403 } usb_device_status_t;
404 
405 #define USBD_DEVICE_STATUS(x) (((unsigned int)x <= USBD_CLOSING) ? usbd_device_status[x] : "UNKNOWN")
406 
407 /*
408  * Device Events
409  *
410  * These are defined in the USB Spec (c.f USB Spec 2.0 Figure 9-1).
411  *
412  * There are additional events defined to handle some extra actions we need
413  * to have handled.
414  *
415  */
416 typedef enum usb_device_event {
417 
418 	DEVICE_UNKNOWN,		/* bi - unknown event */
419 	DEVICE_INIT,		/* bi  - initialize */
420 	DEVICE_CREATE,		/* bi  - */
421 	DEVICE_HUB_CONFIGURED,	/* bi  - bus has been plugged int */
422 	DEVICE_RESET,		/* bi  - hub has powered our port */
423 
424 	DEVICE_ADDRESS_ASSIGNED,	/* ep0 - set address setup received */
425 	DEVICE_CONFIGURED,	/* ep0 - set configure setup received */
426 	DEVICE_SET_INTERFACE,	/* ep0 - set interface setup received */
427 
428 	DEVICE_SET_FEATURE,	/* ep0 - set feature setup received */
429 	DEVICE_CLEAR_FEATURE,	/* ep0 - clear feature setup received */
430 
431 	DEVICE_DE_CONFIGURED,	/* ep0 - set configure setup received for ?? */
432 
433 	DEVICE_BUS_INACTIVE,	/* bi  - bus in inactive (no SOF packets) */
434 	DEVICE_BUS_ACTIVITY,	/* bi  - bus is active again */
435 
436 	DEVICE_POWER_INTERRUPTION,	/* bi  - hub has depowered our port */
437 	DEVICE_HUB_RESET,	/* bi  - bus has been unplugged */
438 	DEVICE_DESTROY,		/* bi  - device instance should be destroyed */
439 
440 	DEVICE_HOTPLUG,		/* bi  - a hotplug event has occurred */
441 
442 	DEVICE_FUNCTION_PRIVATE,	/* function - private */
443 
444 } usb_device_event_t;
445 
446 
447 typedef struct urb_link {
448 	struct urb_link *next;
449 	struct urb_link *prev;
450 } urb_link;
451 
452 /* USB Data structure - for passing data around.
453  *
454  * This is used for both sending and receiving data.
455  *
456  * The callback function is used to let the function driver know when
457  * transmitted data has been sent.
458  *
459  * The callback function is set by the alloc_recv function when an urb is
460  * allocated for receiving data for an endpoint and used to call the
461  * function driver to inform it that data has arrived.
462  */
463 
464 /* in linux we'd malloc this, but in u-boot we prefer static data */
465 #define URB_BUF_SIZE 512
466 
467 struct urb {
468 
469 	struct usb_endpoint_instance *endpoint;
470 	struct usb_device_instance *device;
471 
472 	struct usb_device_request device_request;	/* contents of received SETUP packet */
473 
474 	struct urb_link link;	/* embedded struct for circular doubly linked list of urbs */
475 
476 	u8* buffer;
477 	unsigned int buffer_length;
478 	unsigned int actual_length;
479 
480 	urb_send_status_t status;
481 	int data;
482 
483 	u16 buffer_data[URB_BUF_SIZE];	/* data received (OUT) or being sent (IN) */
484 };
485 
486 /* Endpoint configuration
487  *
488  * Per endpoint configuration data. Used to track which function driver owns
489  * an endpoint.
490  *
491  */
492 struct usb_endpoint_instance {
493 	int endpoint_address;	/* logical endpoint address */
494 
495 	/* control */
496 	int status;		/* halted */
497 	int state;		/* available for use by bus interface driver */
498 
499 	/* receive side */
500 	struct urb_link rcv;	/* received urbs */
501 	struct urb_link rdy;	/* empty urbs ready to receive */
502 	struct urb *rcv_urb;	/* active urb */
503 	int rcv_attributes;	/* copy of bmAttributes from endpoint descriptor */
504 	int rcv_packetSize;	/* maximum packet size from endpoint descriptor */
505 	int rcv_transferSize;	/* maximum transfer size from function driver */
506 	int rcv_queue;
507 
508 	/* transmit side */
509 	struct urb_link tx;	/* urbs ready to transmit */
510 	struct urb_link done;	/* transmitted urbs */
511 	struct urb *tx_urb;	/* active urb */
512 	int tx_attributes;	/* copy of bmAttributes from endpoint descriptor */
513 	int tx_packetSize;	/* maximum packet size from endpoint descriptor */
514 	int tx_transferSize;	/* maximum transfer size from function driver */
515 	int tx_queue;
516 
517 	int sent;		/* data already sent */
518 	int last;		/* data sent in last packet XXX do we need this */
519 };
520 
521 struct usb_alternate_instance {
522 	struct usb_interface_descriptor *interface_descriptor;
523 
524 	int endpoints;
525 	int *endpoint_transfersize_array;
526 	struct usb_endpoint_descriptor **endpoints_descriptor_array;
527 };
528 
529 struct usb_interface_instance {
530 	int alternates;
531 	struct usb_alternate_instance *alternates_instance_array;
532 };
533 
534 struct usb_configuration_instance {
535 	int interfaces;
536 	struct usb_configuration_descriptor *configuration_descriptor;
537 	struct usb_interface_instance *interface_instance_array;
538 };
539 
540 
541 /* USB Device Instance
542  *
543  * For each physical bus interface we create a logical device structure. This
544  * tracks all of the required state to track the USB HOST's view of the device.
545  *
546  * Keep track of the device configuration for a real physical bus interface,
547  * this includes the bus interface, multiple function drivers, the current
548  * configuration and the current state.
549  *
550  * This will show:
551  *	the specific bus interface driver
552  *	the default endpoint 0 driver
553  *	the configured function driver
554  *	device state
555  *	device status
556  *	endpoint list
557  */
558 
559 struct usb_device_instance {
560 
561 	/* generic */
562 	char *name;
563 	struct usb_device_descriptor *device_descriptor;	/* per device descriptor */
564 #if defined(CONFIG_USBD_HS)
565 	struct usb_qualifier_descriptor *qualifier_descriptor;
566 #endif
567 
568 	void (*event) (struct usb_device_instance *device, usb_device_event_t event, int data);
569 
570 	/* Do cdc device specific control requests */
571 	int (*cdc_recv_setup)(struct usb_device_request *request, struct urb *urb);
572 
573 	/* bus interface */
574 	struct usb_bus_instance *bus;	/* which bus interface driver */
575 
576 	/* configuration descriptors */
577 	int configurations;
578 	struct usb_configuration_instance *configuration_instance_array;
579 
580 	/* device state */
581 	usb_device_state_t device_state;	/* current USB Device state */
582 	usb_device_state_t device_previous_state;	/* current USB Device state */
583 
584 	u8 address;		/* current address (zero is default) */
585 	u8 configuration;	/* current show configuration (zero is default) */
586 	u8 interface;		/* current interface (zero is default) */
587 	u8 alternate;		/* alternate flag */
588 
589 	usb_device_status_t status;	/* device status */
590 
591 	int urbs_queued;	/* number of submitted urbs */
592 
593 	/* Shouldn't need to make this atomic, all we need is a change indicator */
594 	unsigned long usbd_rxtx_timestamp;
595 	unsigned long usbd_last_rxtx_timestamp;
596 
597 };
598 
599 /* Bus Interface configuration structure
600  *
601  * This is allocated for each configured instance of a bus interface driver.
602  *
603  * The privdata pointer may be used by the bus interface driver to store private
604  * per instance state information.
605  */
606 struct usb_bus_instance {
607 
608 	struct usb_device_instance *device;
609 	struct usb_endpoint_instance *endpoint_array;	/* array of available configured endpoints */
610 
611 	int max_endpoints;	/* maximimum number of rx enpoints */
612 	unsigned char			maxpacketsize;
613 
614 	unsigned int serial_number;
615 	char *serial_number_str;
616 	void *privdata;		/* private data for the bus interface */
617 
618 };
619 
620 extern char *usbd_device_events[];
621 extern char *usbd_device_states[];
622 extern char *usbd_device_status[];
623 extern char *usbd_device_requests[];
624 extern char *usbd_device_descriptors[];
625 
626 void urb_link_init (urb_link * ul);
627 void urb_detach (struct urb *urb);
628 urb_link *first_urb_link (urb_link * hd);
629 struct urb *first_urb (urb_link * hd);
630 struct urb *first_urb_detached (urb_link * hd);
631 void urb_append (urb_link * hd, struct urb *urb);
632 
633 struct urb *usbd_alloc_urb (struct usb_device_instance *device, struct usb_endpoint_instance *endpoint);
634 void	    usbd_dealloc_urb (struct urb *urb);
635 
636 /*
637  * usbd_device_event is used by bus interface drivers to tell the higher layers that
638  * certain events have taken place.
639  */
640 void usbd_device_event_irq (struct usb_device_instance *conf, usb_device_event_t, int);
641 void usbd_device_event (struct usb_device_instance *conf, usb_device_event_t, int);
642 
643 /* descriptors
644  *
645  * Various ways of finding descriptors based on the current device and any
646  * possible configuration / interface / endpoint for it.
647  */
648 struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct usb_device_instance *, int, int);
649 struct usb_function_instance *usbd_device_function_instance (struct usb_device_instance *, unsigned int);
650 struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *, int, int, int);
651 struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *, int, int, int, int);
652 struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance *, int, int, int, int);
653 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance *, int, int, int, int, int);
654 struct usb_class_descriptor *usbd_device_class_descriptor_index (struct usb_device_instance *, int, int, int, int, int);
655 struct usb_class_report_descriptor *usbd_device_class_report_descriptor_index( struct usb_device_instance *, int , int , int , int , int );
656 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *, int, int, int, int, int);
657 int				usbd_device_endpoint_transfersize (struct usb_device_instance *, int, int, int, int, int);
658 struct usb_string_descriptor *usbd_get_string (u8);
659 struct usb_device_descriptor *usbd_device_device_descriptor(struct
660 		usb_device_instance *, int);
661 
662 #if defined(CONFIG_USBD_HS)
663 /*
664  * is_usbd_high_speed routine needs to be defined by specific gadget driver
665  * It returns true if device enumerates at High speed
666  * Retuns false otherwise
667  */
668 int is_usbd_high_speed(void);
669 #endif
670 int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint);
671 void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad);
672 void usbd_tx_complete (struct usb_endpoint_instance *endpoint);
673 
674 /* These are macros used in debugging */
675 #ifdef DEBUG
676 static inline void print_urb(struct urb *u)
677 {
678 	serial_printf("urb %p\n", (u));
679 	serial_printf("\tendpoint %p\n", u->endpoint);
680 	serial_printf("\tdevice %p\n", u->device);
681 	serial_printf("\tbuffer %p\n", u->buffer);
682 	serial_printf("\tbuffer_length %d\n", u->buffer_length);
683 	serial_printf("\tactual_length %d\n", u->actual_length);
684 	serial_printf("\tstatus %d\n", u->status);
685 	serial_printf("\tdata %d\n", u->data);
686 }
687 
688 static inline void print_usb_device_request(struct usb_device_request *r)
689 {
690 	serial_printf("usb request\n");
691 	serial_printf("\tbmRequestType 0x%2.2x\n", r->bmRequestType);
692 	if ((r->bmRequestType & USB_REQ_DIRECTION_MASK) == 0)
693 		serial_printf("\t\tDirection : To device\n");
694 	else
695 		serial_printf("\t\tDirection : To host\n");
696 	if ((r->bmRequestType & USB_TYPE_STANDARD) == USB_TYPE_STANDARD)
697 		serial_printf("\t\tType      : Standard\n");
698 	if ((r->bmRequestType & USB_TYPE_CLASS) == USB_TYPE_CLASS)
699 		serial_printf("\t\tType      : Standard\n");
700 	if ((r->bmRequestType & USB_TYPE_VENDOR) == USB_TYPE_VENDOR)
701 		serial_printf("\t\tType      : Standard\n");
702 	if ((r->bmRequestType & USB_TYPE_RESERVED) == USB_TYPE_RESERVED)
703 		serial_printf("\t\tType      : Standard\n");
704 	if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
705 	    USB_REQ_RECIPIENT_DEVICE)
706 		serial_printf("\t\tRecipient : Device\n");
707 	if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
708 	    USB_REQ_RECIPIENT_INTERFACE)
709 		serial_printf("\t\tRecipient : Interface\n");
710 	if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
711 	    USB_REQ_RECIPIENT_ENDPOINT)
712 		serial_printf("\t\tRecipient : Endpoint\n");
713 	if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
714 	    USB_REQ_RECIPIENT_OTHER)
715 		serial_printf("\t\tRecipient : Other\n");
716 	serial_printf("\tbRequest      0x%2.2x\n", r->bRequest);
717 	if (r->bRequest == USB_REQ_GET_STATUS)
718 		serial_printf("\t\tGET_STATUS\n");
719 	else if (r->bRequest == USB_REQ_SET_ADDRESS)
720 		serial_printf("\t\tSET_ADDRESS\n");
721 	else if (r->bRequest == USB_REQ_SET_FEATURE)
722 		serial_printf("\t\tSET_FEATURE\n");
723 	else if (r->bRequest == USB_REQ_GET_DESCRIPTOR)
724 		serial_printf("\t\tGET_DESCRIPTOR\n");
725 	else if (r->bRequest == USB_REQ_SET_CONFIGURATION)
726 		serial_printf("\t\tSET_CONFIGURATION\n");
727 	else if (r->bRequest == USB_REQ_SET_INTERFACE)
728 		serial_printf("\t\tUSB_REQ_SET_INTERFACE\n");
729 	else
730 		serial_printf("\tUNKNOWN %d\n", r->bRequest);
731 	serial_printf("\twValue        0x%4.4x\n", r->wValue);
732 	if (r->bRequest == USB_REQ_GET_DESCRIPTOR) {
733 		switch (r->wValue >> 8) {
734 		case USB_DESCRIPTOR_TYPE_DEVICE:
735 			serial_printf("\tDEVICE\n");
736 			break;
737 		case USB_DESCRIPTOR_TYPE_CONFIGURATION:
738 			serial_printf("\tCONFIGURATION\n");
739 			break;
740 		case USB_DESCRIPTOR_TYPE_STRING:
741 			serial_printf("\tSTRING\n");
742 			break;
743 		case USB_DESCRIPTOR_TYPE_INTERFACE:
744 			serial_printf("\tINTERFACE\n");
745 			break;
746 		case USB_DESCRIPTOR_TYPE_ENDPOINT:
747 			serial_printf("\tENDPOINT\n");
748 			break;
749 		case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
750 			serial_printf("\tDEVICE_QUALIFIER\n");
751 			break;
752 		case USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION:
753 			serial_printf("\tOTHER_SPEED_CONFIGURATION\n");
754 			break;
755 		case USB_DESCRIPTOR_TYPE_INTERFACE_POWER:
756 			serial_printf("\tINTERFACE_POWER\n");
757 			break;
758 		case USB_DESCRIPTOR_TYPE_HID:
759 			serial_printf("\tHID\n");
760 			break;
761 		case USB_DESCRIPTOR_TYPE_REPORT:
762 			serial_printf("\tREPORT\n");
763 			break;
764 		default:
765 			serial_printf("\tUNKNOWN TYPE\n");
766 			break;
767 		}
768 	}
769 	serial_printf("\twIndex        0x%4.4x\n", r->wIndex);
770 	serial_printf("\twLength       0x%4.4x\n", r->wLength);
771 }
772 #else
773 /* stubs */
774 #define print_urb(u)
775 #define print_usb_device_request(r)
776 #endif /* DEBUG */
777 #endif
778