1 /*
2  * Copyright (c) 2013-2015 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #include <lk/err.h>
9 #include <lk/debug.h>
10 #include <stdio.h>
11 #include <lk/trace.h>
12 #include <target.h>
13 #include <lk/compiler.h>
14 #include <dev/usb.h>
15 #include <dev/usbc.h>
16 #include <dev/usb/class/bulktest.h>
17 #include <hw/usb.h>
18 #include <lk/init.h>
19 
20 #define LOCAL_TRACE 0
21 
22 #define W(w) (w & 0xff), (w >> 8)
23 #define W3(w) (w & 0xff), ((w >> 8) & 0xff), ((w >> 16) & 0xff)
24 
25 /* top level device descriptor */
26 static const uint8_t dev_descr[] = {
27     0x12,           /* descriptor length */
28     DEVICE,         /* Device Descriptor type */
29     W(0x0200),      /* USB Version */
30     0xff,           /* class */
31     0xff,           /* subclass */
32     0xff,           /* protocol */
33     64,             /* max packet size, ept0 */
34     W(0x9999),      /* vendor */
35     W(0x9999),      /* product */
36     W(0x9999),      /* release */
37     0x2,            /* manufacturer string */
38     0x1,            /* product string */
39     0x0,            /* serialno string */
40     0x1,            /* num configs */
41 };
42 
43 /* high/low speed device qualifier */
44 static const uint8_t devqual_descr[] = {
45     0x0a,           /* len */
46     DEVICE_QUALIFIER, /* Device Qualifier type */
47     W(0x0200),      /* USB version */
48     0x00,           /* class */
49     0x00,           /* subclass */
50     0x00,           /* protocol */
51     64,             /* max packet size, ept0 */
52     0x01,           /* num configs */
53     0x00            /* reserved */
54 };
55 
56 static const uint8_t cfg_descr[] = {
57     0x09,           /* Length of Cfg Descr */
58     CONFIGURATION,  /* Type of Cfg Descr */
59     W(0x09),        /* Total Length (incl ifc, ept) */
60     0x00,           /* # Interfaces */
61     0x01,           /* Cfg Value */
62     0x00,           /* Cfg String */
63     0xc0,           /* Attributes -- self powered */
64     250,            /* Power Consumption - 500mA */
65 };
66 
67 static const uchar langid[] = { 0x04, 0x03, 0x09, 0x04 };
68 
69 usb_config config = {
70     .lowspeed = {
71         .device = USB_DESC_STATIC(dev_descr),
72         .device_qual = USB_DESC_STATIC(devqual_descr),
73         .config = USB_DESC_STATIC(cfg_descr),
74     },
75     .highspeed = {
76         .device = USB_DESC_STATIC(dev_descr),
77         .device_qual = USB_DESC_STATIC(devqual_descr),
78         .config = USB_DESC_STATIC(cfg_descr),
79     },
80 
81     .langid = USB_DESC_STATIC(langid),
82 };
83 
target_usb_setup(void)84 void target_usb_setup(void) {
85     usb_setup(&config);
86     printf("appending interfaces\n");
87 
88     usb_add_string("LK", 1);
89     usb_add_string("LK Industries", 2);
90 
91     /* add our bulk endpoint class device */
92     usb_class_bulktest_init(1, 1, 1);
93 
94     usb_start();
95 }
96