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/compiler.h>
9 #include <lk/debug.h>
10 #include <dev/usb.h>
11 #include <dev/usbc.h>
12 #include <lk/err.h>
13 #include <hw/usb.h>
14 #include <lk/init.h>
15 #include <stdio.h>
16 #include <target.h>
17 #include <lk/trace.h>
18 
19 #define LOCAL_TRACE 0
20 
21 #define W(w) (w & 0xff), (w >> 8)
22 #define W3(w) (w & 0xff), ((w >> 8) & 0xff), ((w >> 16) & 0xff)
23 
24 extern void append_usb_interfaces(void);
25 
26 /* top level device descriptor */
27 static const uint8_t dev_descr[] = {
28     0x12,           /* descriptor length */
29     DEVICE,         /* Device Descriptor type */
30     W(0x0200),      /* USB Version */
31     0xff,           /* class */
32     0xff,           /* subclass */
33     0xff,           /* protocol */
34     64,             /* max packet size, ept0 */
35     W(0x18D1),      /* vendor */
36     W(0xA010),      /* product */
37     W(0x9999),      /* release */
38     0x2,            /* manufacturer string */
39     0x1,            /* product string */
40     0x0,            /* serialno string */
41     0x1,            /* num configs */
42 };
43 
44 /* high/low speed device qualifier */
45 static const uint8_t devqual_descr[] = {
46     0x0a,           /* len */
47     DEVICE_QUALIFIER, /* Device Qualifier type */
48     W(0x0200),      /* USB version */
49     0x00,           /* class */
50     0x00,           /* subclass */
51     0x00,           /* protocol */
52     64,             /* max packet size, ept0 */
53     0x01,           /* num configs */
54     0x00            /* reserved */
55 };
56 
57 static const uint8_t cfg_descr[] = {
58     0x09,           /* Length of Cfg Descr */
59     CONFIGURATION,  /* Type of Cfg Descr */
60     W(0x09),        /* Total Length (incl ifc, ept) */
61     0x00,           /* # Interfaces */
62     0x01,           /* Cfg Value */
63     0x00,           /* Cfg String */
64     0xc0,           /* Attributes -- self powered */
65     250,            /* Power Consumption - 500mA */
66 };
67 
68 static const uchar langid[] = { 0x04, 0x03, 0x09, 0x04 };
69 
70 usb_config config = {
71     .lowspeed = {
72         .device = USB_DESC_STATIC(dev_descr),
73         .device_qual = USB_DESC_STATIC(devqual_descr),
74         .config = USB_DESC_STATIC(cfg_descr),
75     },
76     .highspeed = {
77         .device = USB_DESC_STATIC(dev_descr),
78         .device_qual = USB_DESC_STATIC(devqual_descr),
79         .config = USB_DESC_STATIC(cfg_descr),
80     },
81 
82     .langid = USB_DESC_STATIC(langid),
83 };
84 
target_usb_setup(void)85 void target_usb_setup(void) {
86     usb_setup(&config);
87 
88     append_usb_interfaces();
89 
90     usb_add_string("LK", 1);
91     usb_add_string("LK Industries", 2);
92 
93     usb_start();
94 }
95