1 #include <lk/err.h>
2 #include <lk/debug.h>
3 #include <stdio.h>
4 #include <target.h>
5 #include <lk/compiler.h>
6 #include <dev/usb.h>
7 #include <dev/usbc.h>
8 #include <hw/usb.h>
9 
10 #define W(w) (w & 0xff), (w >> 8)
11 #define W3(w) (w & 0xff), ((w >> 8) & 0xff), ((w >> 16) & 0xff)
12 
13 /* top level device descriptor */
14 static const uint8_t dev_descr[] = {
15     0x12,           /* descriptor length */
16     DEVICE,         /* Device Descriptor type */
17     W(0x0200),      /* USB Version */
18     0xff,           /* class */
19     0xff,           /* subclass */
20     0xff,           /* protocol */
21     64,             /* max packet size, ept0 */
22     W(0x9999),      /* vendor */
23     W(0x9999),      /* product */
24     W(0x9999),      /* release */
25     0x0,            /* manufacturer string */
26     0x0,            /* product string */
27     0x0,            /* serialno string */
28     0x1,            /* num configs */
29 };
30 
31 /* high/low speed device qualifier */
32 static const uint8_t devqual_descr[] = {
33     0x0a,           /* len */
34     DEVICE_QUALIFIER, /* Device Qualifier type */
35     W(0x0200),      /* USB version */
36     0x00,           /* class */
37     0x00,           /* subclass */
38     0x00,           /* protocol */
39     64,             /* max packet size, ept0 */
40     0x01,           /* num configs */
41     0x00            /* reserved */
42 };
43 
44 static const uint8_t cfg_descr[] = {
45     0x09,           /* Length of Cfg Descr */
46     CONFIGURATION,  /* Type of Cfg Descr */
47     W(0x09),        /* Total Length (incl ifc, ept) */
48     0x00,           /* # Interfaces */
49     0x01,           /* Cfg Value */
50     0x00,           /* Cfg String */
51     0xc0,           /* Attributes -- self powered */
52     250,            /* Power Consumption - 500mA */
53 };
54 
55 static const uchar langid[] = { 0x04, 0x03, 0x09, 0x04 };
56 
57 static const uint8_t if_descriptor_lowspeed[] = {
58     0x09,           /* length */
59     INTERFACE,      /* type */
60     0x01,           /* interface num */
61     0x00,           /* alternates */
62     0x02,           /* endpoint count */
63     0xff,           /* interface class */
64     0xff,           /* interface subclass */
65     0x00,           /* interface protocol */
66     0x00,           /* string index */
67 
68     /* endpoint 1 IN */
69     0x07,           /* length */
70     ENDPOINT,       /* type */
71     0x81,           /* address: 1 IN */
72     0x02,           /* type: bulk */
73     W(64),          /* max packet size: 64 */
74     00,             /* interval */
75 
76     /* endpoint 1 OUT */
77     0x07,           /* length */
78     ENDPOINT,       /* type */
79     0x01,           /* address: 1 OUT */
80     0x02,           /* type: bulk */
81     W(64),          /* max packet size: 64 */
82     00,             /* interval */
83 };
84 
85 static usb_config config = {
86     .lowspeed = {
87         .device = USB_DESC_STATIC(dev_descr),
88         .device_qual = USB_DESC_STATIC(devqual_descr),
89         .config = USB_DESC_STATIC(cfg_descr),
90     },
91     .highspeed = {
92         .device = USB_DESC_STATIC(dev_descr),
93         .device_qual = USB_DESC_STATIC(devqual_descr),
94         .config = USB_DESC_STATIC(cfg_descr),
95     },
96 
97     .langid = USB_DESC_STATIC(langid),
98 };
99 
usbtest_usb_setup(void)100 void usbtest_usb_setup(void) {
101     usb_setup(&config);
102     printf("appending interfaces\n");
103     usb_append_interface_lowspeed(if_descriptor_lowspeed, sizeof(if_descriptor_lowspeed));
104     usb_append_interface_highspeed(if_descriptor_lowspeed, sizeof(if_descriptor_lowspeed));
105     usbc_setup_endpoint(1, USB_OUT, 64, USB_BULK);
106     usbc_setup_endpoint(1, USB_IN, 64, USB_BULK);
107 }
108