1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#ifndef __USBDCORE_H__
32#define __USBDCORE_H__
33
34#include <common.h>
35#include "usbdescriptors.h"
36
37
38#define MAX_URBS_QUEUED 5
39
40
41#if 1
42#define usberr(fmt,args...) serial_printf("ERROR: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
43#else
44#define usberr(fmt,args...) do{}while(0)
45#endif
46
47#if 0
48#define usbdbg(fmt,args...) serial_printf("debug: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
49#else
50#define usbdbg(fmt,args...) do{}while(0)
51#endif
52
53#if 0
54#define usbinfo(fmt,args...) serial_printf("info: %s(), %d: "fmt"\n",__FUNCTION__,__LINE__,##args)
55#else
56#define usbinfo(fmt,args...) do{}while(0)
57#endif
58
59#ifndef le16_to_cpu
60#define le16_to_cpu(x) (x)
61#endif
62
63#ifndef inb
64#define inb(p) (*(volatile u8*)(p))
65#endif
66
67#ifndef outb
68#define outb(val,p) (*(volatile u8*)(p) = (val))
69#endif
70
71#ifndef inw
72#define inw(p) (*(volatile u16*)(p))
73#endif
74
75#ifndef outw
76#define outw(val,p) (*(volatile u16*)(p) = (val))
77#endif
78
79#ifndef inl
80#define inl(p) (*(volatile u32*)(p))
81#endif
82
83#ifndef outl
84#define outl(val,p) (*(volatile u32*)(p) = (val))
85#endif
86
87#ifndef insw
88#define insw(p,to,len) mmio_insw(p,to,len)
89#endif
90
91#ifndef outsw
92#define outsw(p,from,len) mmio_outsw(p,from,len)
93#endif
94
95#ifndef insb
96#define insb(p,to,len) mmio_insb(p,to,len)
97#endif
98
99#ifndef mmio_insw
100#define mmio_insw(r,b,l) ({ int __i ; \
101 u16 *__b2; \
102 __b2 = (u16 *) b; \
103 for (__i = 0; __i < l; __i++) { \
104 *(__b2 + __i) = inw(r); \
105 }; \
106 })
107#endif
108
109#ifndef mmio_outsw
110#define mmio_outsw(r,b,l) ({ int __i; \
111 u16 *__b2; \
112 __b2 = (u16 *) b; \
113 for (__i = 0; __i < l; __i++) { \
114 outw( *(__b2 + __i), r); \
115 } \
116 })
117#endif
118
119#ifndef mmio_insb
120#define mmio_insb(r,b,l) ({ int __i ; \
121 u8 *__b2; \
122 __b2 = (u8 *) b; \
123 for (__i = 0; __i < l; __i++) { \
124 *(__b2 + __i) = inb(r); \
125 }; \
126 })
127#endif
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155#define _cv_(ptr) ((char*)(void*)(ptr))
156#define member_offset(type,memb) (_cv_(&(((type*)0)->memb))-(char*)0)
157#define p2surround(type,memb,ptr) ((type*)(void*)(_cv_(ptr)-member_offset(type,memb)))
158
159struct urb;
160
161struct usb_endpoint_instance;
162struct usb_interface_instance;
163struct usb_configuration_instance;
164struct usb_device_instance;
165struct usb_bus_instance;
166
167
168
169
170#define USB_CLASS_PER_INTERFACE 0
171#define USB_CLASS_AUDIO 1
172#define USB_CLASS_COMM 2
173#define USB_CLASS_HID 3
174#define USB_CLASS_PHYSICAL 5
175#define USB_CLASS_PRINTER 7
176#define USB_CLASS_MASS_STORAGE 8
177#define USB_CLASS_HUB 9
178#define USB_CLASS_DATA 10
179#define USB_CLASS_APP_SPEC 0xfe
180#define USB_CLASS_VENDOR_SPEC 0xff
181
182
183
184
185#define USB_TYPE_STANDARD (0x00 << 5)
186#define USB_TYPE_CLASS (0x01 << 5)
187#define USB_TYPE_VENDOR (0x02 << 5)
188#define USB_TYPE_RESERVED (0x03 << 5)
189
190
191
192
193#define USB_RECIP_DEVICE 0x00
194#define USB_RECIP_INTERFACE 0x01
195#define USB_RECIP_ENDPOINT 0x02
196#define USB_RECIP_OTHER 0x03
197
198
199
200
201#define USB_DIR_OUT 0
202#define USB_DIR_IN 0x80
203
204
205
206
207#define USB_DT_DEVICE 0x01
208#define USB_DT_CONFIG 0x02
209#define USB_DT_STRING 0x03
210#define USB_DT_INTERFACE 0x04
211#define USB_DT_ENDPOINT 0x05
212
213#define USB_DT_HID (USB_TYPE_CLASS | 0x01)
214#define USB_DT_REPORT (USB_TYPE_CLASS | 0x02)
215#define USB_DT_PHYSICAL (USB_TYPE_CLASS | 0x03)
216#define USB_DT_HUB (USB_TYPE_CLASS | 0x09)
217
218
219
220
221#define USB_DT_DEVICE_SIZE 18
222#define USB_DT_CONFIG_SIZE 9
223#define USB_DT_INTERFACE_SIZE 9
224#define USB_DT_ENDPOINT_SIZE 7
225#define USB_DT_ENDPOINT_AUDIO_SIZE 9
226#define USB_DT_HUB_NONVAR_SIZE 7
227#define USB_DT_HID_SIZE 9
228
229
230
231
232#define USB_ENDPOINT_NUMBER_MASK 0x0f
233#define USB_ENDPOINT_DIR_MASK 0x80
234
235#define USB_ENDPOINT_XFERTYPE_MASK 0x03
236#define USB_ENDPOINT_XFER_CONTROL 0
237#define USB_ENDPOINT_XFER_ISOC 1
238#define USB_ENDPOINT_XFER_BULK 2
239#define USB_ENDPOINT_XFER_INT 3
240
241
242
243
244#define USB_PID_UNDEF_0 0xf0
245#define USB_PID_OUT 0xe1
246#define USB_PID_ACK 0xd2
247#define USB_PID_DATA0 0xc3
248#define USB_PID_PING 0xb4
249#define USB_PID_SOF 0xa5
250#define USB_PID_NYET 0x96
251#define USB_PID_DATA2 0x87
252#define USB_PID_SPLIT 0x78
253#define USB_PID_IN 0x69
254#define USB_PID_NAK 0x5a
255#define USB_PID_DATA1 0x4b
256#define USB_PID_PREAMBLE 0x3c
257#define USB_PID_ERR 0x3c
258#define USB_PID_SETUP 0x2d
259#define USB_PID_STALL 0x1e
260#define USB_PID_MDATA 0x0f
261
262
263
264
265#define USB_REQ_GET_STATUS 0x00
266#define USB_REQ_CLEAR_FEATURE 0x01
267#define USB_REQ_SET_FEATURE 0x03
268#define USB_REQ_SET_ADDRESS 0x05
269#define USB_REQ_GET_DESCRIPTOR 0x06
270#define USB_REQ_SET_DESCRIPTOR 0x07
271#define USB_REQ_GET_CONFIGURATION 0x08
272#define USB_REQ_SET_CONFIGURATION 0x09
273#define USB_REQ_GET_INTERFACE 0x0A
274#define USB_REQ_SET_INTERFACE 0x0B
275#define USB_REQ_SYNCH_FRAME 0x0C
276
277#define USBD_DEVICE_REQUESTS(x) (((unsigned int)x <= USB_REQ_SYNCH_FRAME) ? usbd_device_requests[x] : "UNKNOWN")
278
279
280
281
282#define USB_REQ_GET_REPORT 0x01
283#define USB_REQ_GET_IDLE 0x02
284#define USB_REQ_GET_PROTOCOL 0x03
285#define USB_REQ_SET_REPORT 0x09
286#define USB_REQ_SET_IDLE 0x0A
287#define USB_REQ_SET_PROTOCOL 0x0B
288
289
290
291
292
293
294#define USB_BCD_VERSION 0x0110
295
296
297
298
299
300
301#define USB_REQ_DIRECTION_MASK 0x80
302#define USB_REQ_TYPE_MASK 0x60
303#define USB_REQ_RECIPIENT_MASK 0x1f
304
305#define USB_REQ_DEVICE2HOST 0x80
306#define USB_REQ_HOST2DEVICE 0x00
307
308#define USB_REQ_TYPE_STANDARD 0x00
309#define USB_REQ_TYPE_CLASS 0x20
310#define USB_REQ_TYPE_VENDOR 0x40
311
312#define USB_REQ_RECIPIENT_DEVICE 0x00
313#define USB_REQ_RECIPIENT_INTERFACE 0x01
314#define USB_REQ_RECIPIENT_ENDPOINT 0x02
315#define USB_REQ_RECIPIENT_OTHER 0x03
316
317
318
319
320
321#define USB_STATUS_SELFPOWERED 0x01
322#define USB_STATUS_REMOTEWAKEUP 0x02
323
324#define USB_STATUS_HALT 0x01
325
326
327
328
329
330#define USB_DESCRIPTOR_TYPE_DEVICE 0x01
331#define USB_DESCRIPTOR_TYPE_CONFIGURATION 0x02
332#define USB_DESCRIPTOR_TYPE_STRING 0x03
333#define USB_DESCRIPTOR_TYPE_INTERFACE 0x04
334#define USB_DESCRIPTOR_TYPE_ENDPOINT 0x05
335#define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 0x06
336#define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION 0x07
337#define USB_DESCRIPTOR_TYPE_INTERFACE_POWER 0x08
338#define USB_DESCRIPTOR_TYPE_HID 0x21
339#define USB_DESCRIPTOR_TYPE_REPORT 0x22
340
341#define USBD_DEVICE_DESCRIPTORS(x) (((unsigned int)x <= USB_DESCRIPTOR_TYPE_INTERFACE_POWER) ? \
342 usbd_device_descriptors[x] : "UNKNOWN")
343
344
345
346
347#define USB_ENDPOINT_HALT 0x00
348#define USB_DEVICE_REMOTE_WAKEUP 0x01
349#define USB_TEST_MODE 0x02
350
351
352
353
354
355
356struct usb_device_request {
357 u8 bmRequestType;
358 u8 bRequest;
359 u16 wValue;
360 u16 wIndex;
361 u16 wLength;
362} __attribute__ ((packed));
363
364
365
366
367
368typedef enum urb_send_status {
369 SEND_IN_PROGRESS,
370 SEND_FINISHED_OK,
371 SEND_FINISHED_ERROR,
372 RECV_READY,
373 RECV_OK,
374 RECV_ERROR
375} urb_send_status_t;
376
377
378
379
380
381
382
383
384
385
386typedef enum usb_device_state {
387 STATE_INIT,
388 STATE_CREATED,
389 STATE_ATTACHED,
390 STATE_POWERED,
391 STATE_DEFAULT,
392 STATE_ADDRESSED,
393 STATE_CONFIGURED,
394 STATE_UNKNOWN,
395} usb_device_state_t;
396
397#define USBD_DEVICE_STATE(x) (((unsigned int)x <= STATE_UNKNOWN) ? usbd_device_states[x] : "UNKNOWN")
398
399
400
401
402
403
404typedef enum usb_device_status {
405 USBD_OPENING,
406 USBD_OK,
407 USBD_SUSPENDED,
408 USBD_CLOSING,
409} usb_device_status_t;
410
411#define USBD_DEVICE_STATUS(x) (((unsigned int)x <= USBD_CLOSING) ? usbd_device_status[x] : "UNKNOWN")
412
413
414
415
416
417
418
419
420
421
422typedef enum usb_device_event {
423
424 DEVICE_UNKNOWN,
425 DEVICE_INIT,
426 DEVICE_CREATE,
427 DEVICE_HUB_CONFIGURED,
428 DEVICE_RESET,
429
430 DEVICE_ADDRESS_ASSIGNED,
431 DEVICE_CONFIGURED,
432 DEVICE_SET_INTERFACE,
433
434 DEVICE_SET_FEATURE,
435 DEVICE_CLEAR_FEATURE,
436
437 DEVICE_DE_CONFIGURED,
438
439 DEVICE_BUS_INACTIVE,
440 DEVICE_BUS_ACTIVITY,
441
442 DEVICE_POWER_INTERRUPTION,
443 DEVICE_HUB_RESET,
444 DEVICE_DESTROY,
445
446 DEVICE_HOTPLUG,
447
448 DEVICE_FUNCTION_PRIVATE,
449
450} usb_device_event_t;
451
452
453typedef struct urb_link {
454 struct urb_link *next;
455 struct urb_link *prev;
456} urb_link;
457
458
459
460
461
462
463
464
465
466
467
468
469
470#define URB_BUF_SIZE 128
471struct urb {
472
473 struct usb_endpoint_instance *endpoint;
474 struct usb_device_instance *device;
475
476 struct usb_device_request device_request;
477
478 struct urb_link link;
479
480 u8* buffer;
481 unsigned int buffer_length;
482 unsigned int actual_length;
483
484 urb_send_status_t status;
485 int data;
486
487 u16 buffer_data[URB_BUF_SIZE];
488};
489
490
491
492
493
494
495
496struct usb_endpoint_instance {
497 int endpoint_address;
498
499
500 int status;
501 int state;
502
503
504 struct urb_link rcv;
505 struct urb_link rdy;
506 struct urb *rcv_urb;
507 int rcv_attributes;
508 int rcv_packetSize;
509 int rcv_transferSize;
510 int rcv_queue;
511
512
513 struct urb_link tx;
514 struct urb_link done;
515 struct urb *tx_urb;
516 int tx_attributes;
517 int tx_packetSize;
518 int tx_transferSize;
519 int tx_queue;
520
521 int sent;
522 int last;
523};
524
525struct usb_alternate_instance {
526 struct usb_interface_descriptor *interface_descriptor;
527
528 int endpoints;
529 int *endpoint_transfersize_array;
530 struct usb_endpoint_descriptor **endpoints_descriptor_array;
531};
532
533struct usb_interface_instance {
534 int alternates;
535 struct usb_alternate_instance *alternates_instance_array;
536};
537
538struct usb_configuration_instance {
539 int interfaces;
540 struct usb_configuration_descriptor *configuration_descriptor;
541 struct usb_interface_instance *interface_instance_array;
542};
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563struct usb_device_instance {
564
565
566 char *name;
567 struct usb_device_descriptor *device_descriptor;
568
569 void (*event) (struct usb_device_instance *device, usb_device_event_t event, int data);
570
571
572 int (*cdc_recv_setup)(struct usb_device_request *request, struct urb *urb);
573
574
575 struct usb_bus_instance *bus;
576
577
578 int configurations;
579 struct usb_configuration_instance *configuration_instance_array;
580
581
582 usb_device_state_t device_state;
583 usb_device_state_t device_previous_state;
584
585 u8 address;
586 u8 configuration;
587 u8 interface;
588 u8 alternate;
589
590 usb_device_status_t status;
591
592 int urbs_queued;
593
594
595 unsigned long usbd_rxtx_timestamp;
596 unsigned long usbd_last_rxtx_timestamp;
597
598};
599
600
601
602
603
604
605
606
607struct usb_bus_instance {
608
609 struct usb_device_instance *device;
610 struct usb_endpoint_instance *endpoint_array;
611
612 int max_endpoints;
613 unsigned char maxpacketsize;
614
615 unsigned int serial_number;
616 char *serial_number_str;
617 void *privdata;
618
619};
620
621extern char *usbd_device_events[];
622extern char *usbd_device_states[];
623extern char *usbd_device_status[];
624extern char *usbd_device_requests[];
625extern char *usbd_device_descriptors[];
626
627void urb_link_init (urb_link * ul);
628void urb_detach (struct urb *urb);
629urb_link *first_urb_link (urb_link * hd);
630struct urb *first_urb (urb_link * hd);
631struct urb *first_urb_detached (urb_link * hd);
632void urb_append (urb_link * hd, struct urb *urb);
633
634struct urb *usbd_alloc_urb (struct usb_device_instance *device, struct usb_endpoint_instance *endpoint);
635void usbd_dealloc_urb (struct urb *urb);
636
637
638
639
640
641void usbd_device_event_irq (struct usb_device_instance *conf, usb_device_event_t, int);
642void usbd_device_event (struct usb_device_instance *conf, usb_device_event_t, int);
643
644
645
646
647
648
649struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct usb_device_instance *, int, int);
650struct usb_function_instance *usbd_device_function_instance (struct usb_device_instance *, unsigned int);
651struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *, int, int, int);
652struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *, int, int, int, int);
653struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance *, int, int, int, int);
654struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance *, int, int, int, int, int);
655struct usb_class_descriptor *usbd_device_class_descriptor_index (struct usb_device_instance *, int, int, int, int, int);
656struct usb_class_report_descriptor *usbd_device_class_report_descriptor_index( struct usb_device_instance *, int , int , int , int , int );
657struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *, int, int, int, int, int);
658int usbd_device_endpoint_transfersize (struct usb_device_instance *, int, int, int, int, int);
659struct usb_string_descriptor *usbd_get_string (u8);
660struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *, int);
661
662int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint);
663void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad);
664void usbd_tx_complete (struct usb_endpoint_instance *endpoint);
665
666
667#ifdef DEBUG
668static inline void print_urb(struct urb *u)
669{
670 serial_printf("urb %p\n", (u));
671 serial_printf("\tendpoint %p\n", u->endpoint);
672 serial_printf("\tdevice %p\n", u->device);
673 serial_printf("\tbuffer %p\n", u->buffer);
674 serial_printf("\tbuffer_length %d\n", u->buffer_length);
675 serial_printf("\tactual_length %d\n", u->actual_length);
676 serial_printf("\tstatus %d\n", u->status);
677 serial_printf("\tdata %d\n", u->data);
678}
679
680static inline void print_usb_device_request(struct usb_device_request *r)
681{
682 serial_printf("usb request\n");
683 serial_printf("\tbmRequestType 0x%2.2x\n", r->bmRequestType);
684 if ((r->bmRequestType & USB_REQ_DIRECTION_MASK) == 0)
685 serial_printf("\t\tDirection : To device\n");
686 else
687 serial_printf("\t\tDirection : To host\n");
688 if ((r->bmRequestType & USB_TYPE_STANDARD) == USB_TYPE_STANDARD)
689 serial_printf("\t\tType : Standard\n");
690 if ((r->bmRequestType & USB_TYPE_CLASS) == USB_TYPE_CLASS)
691 serial_printf("\t\tType : Standard\n");
692 if ((r->bmRequestType & USB_TYPE_VENDOR) == USB_TYPE_VENDOR)
693 serial_printf("\t\tType : Standard\n");
694 if ((r->bmRequestType & USB_TYPE_RESERVED) == USB_TYPE_RESERVED)
695 serial_printf("\t\tType : Standard\n");
696 if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
697 USB_REQ_RECIPIENT_DEVICE)
698 serial_printf("\t\tRecipient : Device\n");
699 if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
700 USB_REQ_RECIPIENT_INTERFACE)
701 serial_printf("\t\tRecipient : Interface\n");
702 if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
703 USB_REQ_RECIPIENT_ENDPOINT)
704 serial_printf("\t\tRecipient : Endpoint\n");
705 if ((r->bmRequestType & USB_REQ_RECIPIENT_MASK) ==
706 USB_REQ_RECIPIENT_OTHER)
707 serial_printf("\t\tRecipient : Other\n");
708 serial_printf("\tbRequest 0x%2.2x\n", r->bRequest);
709 if (r->bRequest == USB_REQ_GET_STATUS)
710 serial_printf("\t\tGET_STATUS\n");
711 else if (r->bRequest == USB_REQ_SET_ADDRESS)
712 serial_printf("\t\tSET_ADDRESS\n");
713 else if (r->bRequest == USB_REQ_SET_FEATURE)
714 serial_printf("\t\tSET_FEATURE\n");
715 else if (r->bRequest == USB_REQ_GET_DESCRIPTOR)
716 serial_printf("\t\tGET_DESCRIPTOR\n");
717 else if (r->bRequest == USB_REQ_SET_CONFIGURATION)
718 serial_printf("\t\tSET_CONFIGURATION\n");
719 else if (r->bRequest == USB_REQ_SET_INTERFACE)
720 serial_printf("\t\tUSB_REQ_SET_INTERFACE\n");
721 else
722 serial_printf("\tUNKNOWN %d\n", r->bRequest);
723 serial_printf("\twValue 0x%4.4x\n", r->wValue);
724 if (r->bRequest == USB_REQ_GET_DESCRIPTOR) {
725 switch (r->wValue >> 8) {
726 case USB_DESCRIPTOR_TYPE_DEVICE:
727 serial_printf("\tDEVICE\n");
728 break;
729 case USB_DESCRIPTOR_TYPE_CONFIGURATION:
730 serial_printf("\tCONFIGURATION\n");
731 break;
732 case USB_DESCRIPTOR_TYPE_STRING:
733 serial_printf("\tSTRING\n");
734 break;
735 case USB_DESCRIPTOR_TYPE_INTERFACE:
736 serial_printf("\tINTERFACE\n");
737 break;
738 case USB_DESCRIPTOR_TYPE_ENDPOINT:
739 serial_printf("\tENDPOINT\n");
740 break;
741 case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
742 serial_printf("\tDEVICE_QUALIFIER\n");
743 break;
744 case USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIGURATION:
745 serial_printf("\tOTHER_SPEED_CONFIGURATION\n");
746 break;
747 case USB_DESCRIPTOR_TYPE_INTERFACE_POWER:
748 serial_printf("\tINTERFACE_POWER\n");
749 break;
750 case USB_DESCRIPTOR_TYPE_HID:
751 serial_printf("\tHID\n");
752 break;
753 case USB_DESCRIPTOR_TYPE_REPORT:
754 serial_printf("\tREPORT\n");
755 break;
756 default:
757 serial_printf("\tUNKNOWN TYPE\n");
758 break;
759 }
760 }
761 serial_printf("\twIndex 0x%4.4x\n", r->wIndex);
762 serial_printf("\twLength 0x%4.4x\n", r->wLength);
763}
764#else
765
766#define print_urb(u)
767#define print_usb_device_request(r)
768#endif
769#endif
770