1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
145struct urb;
146
147struct usb_endpoint_instance;
148struct usb_interface_instance;
149struct usb_configuration_instance;
150struct usb_device_instance;
151struct usb_bus_instance;
152
153
154
155
156#define USB_CLASS_PER_INTERFACE 0
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
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
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
186
187#define USB_DIR_OUT 0
188#define USB_DIR_IN 0x80
189
190
191
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
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
216#define USB_DT_HUB_NONVAR_SIZE 7
217#define USB_DT_HID_SIZE 9
218
219
220
221
222#define USB_ENDPOINT_NUMBER_MASK 0x0f
223#define USB_ENDPOINT_DIR_MASK 0x80
224
225#define USB_ENDPOINT_XFERTYPE_MASK 0x03
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
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
239#define USB_PID_SOF 0xa5
240#define USB_PID_NYET 0x96
241#define USB_PID_DATA2 0x87
242#define USB_PID_SPLIT 0x78
243#define USB_PID_IN 0x69
244#define USB_PID_NAK 0x5a
245#define USB_PID_DATA1 0x4b
246#define USB_PID_PREAMBLE 0x3c
247#define USB_PID_ERR 0x3c
248#define USB_PID_SETUP 0x2d
249#define USB_PID_STALL 0x1e
250#define USB_PID_MDATA 0x0f
251
252
253
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
268
269
270#define USB_REQ_GET_REPORT 0x01
271#define USB_REQ_GET_IDLE 0x02
272#define USB_REQ_GET_PROTOCOL 0x03
273#define USB_REQ_SET_REPORT 0x09
274#define USB_REQ_SET_IDLE 0x0A
275#define USB_REQ_SET_PROTOCOL 0x0B
276
277
278
279
280
281
282#if defined(CONFIG_USBD_HS)
283#define USB_BCD_VERSION 0x0200
284#else
285#define USB_BCD_VERSION 0x0110
286#endif
287
288
289
290
291
292
293#define USB_REQ_DIRECTION_MASK 0x80
294#define USB_REQ_TYPE_MASK 0x60
295#define USB_REQ_RECIPIENT_MASK 0x1f
296
297#define USB_REQ_DEVICE2HOST 0x80
298#define USB_REQ_HOST2DEVICE 0x00
299
300#define USB_REQ_TYPE_STANDARD 0x00
301#define USB_REQ_TYPE_CLASS 0x20
302#define USB_REQ_TYPE_VENDOR 0x40
303
304#define USB_REQ_RECIPIENT_DEVICE 0x00
305#define USB_REQ_RECIPIENT_INTERFACE 0x01
306#define USB_REQ_RECIPIENT_ENDPOINT 0x02
307#define USB_REQ_RECIPIENT_OTHER 0x03
308
309
310
311
312
313#define USB_STATUS_SELFPOWERED 0x01
314#define USB_STATUS_REMOTEWAKEUP 0x02
315
316#define USB_STATUS_HALT 0x01
317
318
319
320
321
322#define USB_DESCRIPTOR_TYPE_DEVICE 0x01
323#define USB_DESCRIPTOR_TYPE_CONFIGURATION 0x02
324#define USB_DESCRIPTOR_TYPE_STRING 0x03
325#define USB_DESCRIPTOR_TYPE_INTERFACE 0x04
326#define USB_DESCRIPTOR_TYPE_ENDPOINT 0x05
327#define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 0x06
328#define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION 0x07
329#define USB_DESCRIPTOR_TYPE_INTERFACE_POWER 0x08
330#define USB_DESCRIPTOR_TYPE_HID 0x21
331#define USB_DESCRIPTOR_TYPE_REPORT 0x22
332
333
334
335
336#define USB_ENDPOINT_HALT 0x00
337#define USB_DEVICE_REMOTE_WAKEUP 0x01
338#define USB_TEST_MODE 0x02
339
340
341
342
343
344
345struct usb_device_request {
346 u8 bmRequestType;
347 u8 bRequest;
348 u16 wValue;
349 u16 wIndex;
350 u16 wLength;
351} __attribute__ ((packed));
352
353
354
355
356
357typedef enum urb_send_status {
358 SEND_IN_PROGRESS,
359 SEND_FINISHED_OK,
360 SEND_FINISHED_ERROR,
361 RECV_READY,
362 RECV_OK,
363 RECV_ERROR
364} urb_send_status_t;
365
366
367
368
369
370
371
372
373
374
375typedef enum usb_device_state {
376 STATE_INIT,
377 STATE_CREATED,
378 STATE_ATTACHED,
379 STATE_POWERED,
380 STATE_DEFAULT,
381 STATE_ADDRESSED,
382 STATE_CONFIGURED,
383 STATE_UNKNOWN,
384} usb_device_state_t;
385
386
387
388
389
390
391typedef enum usb_device_status {
392 USBD_OPENING,
393 USBD_OK,
394 USBD_SUSPENDED,
395 USBD_CLOSING,
396} usb_device_status_t;
397
398
399
400
401
402
403
404
405
406
407typedef enum usb_device_event {
408
409 DEVICE_UNKNOWN,
410 DEVICE_INIT,
411 DEVICE_CREATE,
412 DEVICE_HUB_CONFIGURED,
413 DEVICE_RESET,
414
415 DEVICE_ADDRESS_ASSIGNED,
416 DEVICE_CONFIGURED,
417 DEVICE_SET_INTERFACE,
418
419 DEVICE_SET_FEATURE,
420 DEVICE_CLEAR_FEATURE,
421
422 DEVICE_DE_CONFIGURED,
423
424 DEVICE_BUS_INACTIVE,
425 DEVICE_BUS_ACTIVITY,
426
427 DEVICE_POWER_INTERRUPTION,
428 DEVICE_HUB_RESET,
429 DEVICE_DESTROY,
430
431 DEVICE_HOTPLUG,
432
433 DEVICE_FUNCTION_PRIVATE,
434
435} usb_device_event_t;
436
437
438typedef struct urb_link {
439 struct urb_link *next;
440 struct urb_link *prev;
441} urb_link;
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456#define URB_BUF_SIZE 512
457
458struct urb {
459
460 struct usb_endpoint_instance *endpoint;
461 struct usb_device_instance *device;
462
463 struct usb_device_request device_request;
464
465 struct urb_link link;
466
467 u8* buffer;
468 unsigned int buffer_length;
469 unsigned int actual_length;
470
471 urb_send_status_t status;
472 int data;
473
474 u16 buffer_data[URB_BUF_SIZE];
475};
476
477
478
479
480
481
482
483struct usb_endpoint_instance {
484 int endpoint_address;
485
486
487 int status;
488 int state;
489
490
491 struct urb_link rcv;
492 struct urb_link rdy;
493 struct urb *rcv_urb;
494 int rcv_attributes;
495 int rcv_packetSize;
496 int rcv_transferSize;
497 int rcv_queue;
498
499
500 struct urb_link tx;
501 struct urb_link done;
502 struct urb *tx_urb;
503 int tx_attributes;
504 int tx_packetSize;
505 int tx_transferSize;
506 int tx_queue;
507
508 int sent;
509 int last;
510};
511
512struct usb_alternate_instance {
513 struct usb_interface_descriptor *interface_descriptor;
514
515 int endpoints;
516 int *endpoint_transfersize_array;
517 struct usb_endpoint_descriptor **endpoints_descriptor_array;
518};
519
520struct usb_interface_instance {
521 int alternates;
522 struct usb_alternate_instance *alternates_instance_array;
523};
524
525struct usb_configuration_instance {
526 int interfaces;
527 struct usb_configuration_descriptor *configuration_descriptor;
528 struct usb_interface_instance *interface_instance_array;
529};
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550struct usb_device_instance {
551
552
553 char *name;
554 struct usb_device_descriptor *device_descriptor;
555#if defined(CONFIG_USBD_HS)
556 struct usb_qualifier_descriptor *qualifier_descriptor;
557#endif
558
559 void (*event) (struct usb_device_instance *device, usb_device_event_t event, int data);
560
561
562 int (*cdc_recv_setup)(struct usb_device_request *request, struct urb *urb);
563
564
565 struct usb_bus_instance *bus;
566
567
568 int configurations;
569 struct usb_configuration_instance *configuration_instance_array;
570
571
572 usb_device_state_t device_state;
573 usb_device_state_t device_previous_state;
574
575 u8 address;
576 u8 configuration;
577 u8 interface;
578 u8 alternate;
579
580 usb_device_status_t status;
581
582 int urbs_queued;
583
584
585 unsigned long usbd_rxtx_timestamp;
586 unsigned long usbd_last_rxtx_timestamp;
587
588};
589
590
591
592
593
594
595
596
597struct usb_bus_instance {
598
599 struct usb_device_instance *device;
600 struct usb_endpoint_instance *endpoint_array;
601
602 int max_endpoints;
603 unsigned char maxpacketsize;
604
605 unsigned int serial_number;
606 char *serial_number_str;
607 void *privdata;
608
609};
610
611void urb_link_init (urb_link * ul);
612void urb_detach (struct urb *urb);
613urb_link *first_urb_link (urb_link * hd);
614struct urb *first_urb (urb_link * hd);
615struct urb *first_urb_detached (urb_link * hd);
616void urb_append (urb_link * hd, struct urb *urb);
617
618struct urb *usbd_alloc_urb (struct usb_device_instance *device, struct usb_endpoint_instance *endpoint);
619void usbd_dealloc_urb (struct urb *urb);
620
621
622
623
624
625void usbd_device_event_irq (struct usb_device_instance *conf, usb_device_event_t, int);
626void usbd_device_event (struct usb_device_instance *conf, usb_device_event_t, int);
627
628
629
630
631
632
633struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct usb_device_instance *, int, int);
634struct usb_function_instance *usbd_device_function_instance (struct usb_device_instance *, unsigned int);
635struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *, int, int, int);
636struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *, int, int, int, int);
637struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance *, int, int, int, int);
638struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance *, int, int, int, int, int);
639struct usb_class_descriptor *usbd_device_class_descriptor_index (struct usb_device_instance *, int, int, int, int, int);
640struct usb_class_report_descriptor *usbd_device_class_report_descriptor_index( struct usb_device_instance *, int , int , int , int , int );
641struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *, int, int, int, int, int);
642int usbd_device_endpoint_transfersize (struct usb_device_instance *, int, int, int, int, int);
643struct usb_string_descriptor *usbd_get_string (u8);
644struct usb_device_descriptor *usbd_device_device_descriptor(struct
645 usb_device_instance *, int);
646
647#if defined(CONFIG_USBD_HS)
648
649
650
651
652
653int is_usbd_high_speed(void);
654#endif
655int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint);
656void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad);
657void usbd_tx_complete (struct usb_endpoint_instance *endpoint);
658
659
660#ifdef DEBUG
661static inline void print_urb(struct urb *u)
662{
663 serial_printf("urb %p\n", (u));
664 serial_printf("\tendpoint %p\n", u->endpoint);
665 serial_printf("\tdevice %p\n", u->device);
666 serial_printf("\tbuffer %p\n", u->buffer);
667 serial_printf("\tbuffer_length %d\n", u->buffer_length);
668 serial_printf("\tactual_length %d\n", u->actual_length);
669 serial_printf("\tstatus %d\n", u->status);
670 serial_printf("\tdata %d\n", u->data);
671}
672
673static inline void print_usb_device_request(struct usb_device_request *r)
674{
675 serial_printf("usb request\n");
676 serial_printf("\tbmRequestType 0x%2.2x\n", r->bmRequestType);
677 if ((r->bmRequestType & USB_REQ_DIRECTION_MASK) == 0)
678 serial_printf("\t\tDirection : To device\n");
679 else
680 serial_printf("\t\tDirection : To host\n");
681 if ((r->bmRequestType & USB_TYPE_STANDARD) == USB_TYPE_STANDARD)
682 serial_printf("\t\tType : Standard\n");
683 if ((r->bmRequestType & USB_TYPE_CLASS) == USB_TYPE_CLASS)
684 serial_printf("\t\tType : Standard\n");
685 if ((r->bmRequestType & USB_TYPE_VENDOR) == USB_TYPE_VENDOR)
686 serial_printf("\t\tType : Standard\n");
687 if ((r->bmRequestType & USB_TYPE_RESERVED) == USB_TYPE_RESERVED)
688 serial_printf("\t\tType : Standard\n");
689 if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
690 USB_REQ_RECIPIENT_DEVICE)
691 serial_printf("\t\tRecipient : Device\n");
692 if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
693 USB_REQ_RECIPIENT_INTERFACE)
694 serial_printf("\t\tRecipient : Interface\n");
695 if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
696 USB_REQ_RECIPIENT_ENDPOINT)
697 serial_printf("\t\tRecipient : Endpoint\n");
698 if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
699 USB_REQ_RECIPIENT_OTHER)
700 serial_printf("\t\tRecipient : Other\n");
701 serial_printf("\tbRequest 0x%2.2x\n", r->bRequest);
702 if (r->bRequest == USB_REQ_GET_STATUS)
703 serial_printf("\t\tGET_STATUS\n");
704 else if (r->bRequest == USB_REQ_SET_ADDRESS)
705 serial_printf("\t\tSET_ADDRESS\n");
706 else if (r->bRequest == USB_REQ_SET_FEATURE)
707 serial_printf("\t\tSET_FEATURE\n");
708 else if (r->bRequest == USB_REQ_GET_DESCRIPTOR)
709 serial_printf("\t\tGET_DESCRIPTOR\n");
710 else if (r->bRequest == USB_REQ_SET_CONFIGURATION)
711 serial_printf("\t\tSET_CONFIGURATION\n");
712 else if (r->bRequest == USB_REQ_SET_INTERFACE)
713 serial_printf("\t\tUSB_REQ_SET_INTERFACE\n");
714 else
715 serial_printf("\tUNKNOWN %d\n", r->bRequest);
716 serial_printf("\twValue 0x%4.4x\n", r->wValue);
717 if (r->bRequest == USB_REQ_GET_DESCRIPTOR) {
718 switch (r->wValue >> 8) {
719 case USB_DESCRIPTOR_TYPE_DEVICE:
720 serial_printf("\tDEVICE\n");
721 break;
722 case USB_DESCRIPTOR_TYPE_CONFIGURATION:
723 serial_printf("\tCONFIGURATION\n");
724 break;
725 case USB_DESCRIPTOR_TYPE_STRING:
726 serial_printf("\tSTRING\n");
727 break;
728 case USB_DESCRIPTOR_TYPE_INTERFACE:
729 serial_printf("\tINTERFACE\n");
730 break;
731 case USB_DESCRIPTOR_TYPE_ENDPOINT:
732 serial_printf("\tENDPOINT\n");
733 break;
734 case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
735 serial_printf("\tDEVICE_QUALIFIER\n");
736 break;
737 case USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION:
738 serial_printf("\tOTHER_SPEED_CONFIGURATION\n");
739 break;
740 case USB_DESCRIPTOR_TYPE_INTERFACE_POWER:
741 serial_printf("\tINTERFACE_POWER\n");
742 break;
743 case USB_DESCRIPTOR_TYPE_HID:
744 serial_printf("\tHID\n");
745 break;
746 case USB_DESCRIPTOR_TYPE_REPORT:
747 serial_printf("\tREPORT\n");
748 break;
749 default:
750 serial_printf("\tUNKNOWN TYPE\n");
751 break;
752 }
753 }
754 serial_printf("\twIndex 0x%4.4x\n", r->wIndex);
755 serial_printf("\twLength 0x%4.4x\n", r->wLength);
756}
757#else
758
759#define print_urb(u)
760#define print_usb_device_request(r)
761#endif
762#endif
763