|
| 1 | +/* |
| 2 | + * Copyright (c) 2013-2015 Travis Geiselbrecht |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | + * a copy of this software and associated documentation files |
| 6 | + * (the "Software"), to deal in the Software without restriction, |
| 7 | + * including without limitation the rights to use, copy, modify, merge, |
| 8 | + * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | + * and to permit persons to whom the Software is furnished to do so, |
| 10 | + * subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | +#include <err.h> |
| 24 | +#include <debug.h> |
| 25 | +#include <stdio.h> |
| 26 | +#include <trace.h> |
| 27 | +#include <target.h> |
| 28 | +#include <compiler.h> |
| 29 | +#include <dev/usb.h> |
| 30 | +#include <dev/usbc.h> |
| 31 | +#include <dev/usb/class/cdcserial.h> |
| 32 | +#include <hw/usb.h> |
| 33 | +#include <lk/init.h> |
| 34 | + |
| 35 | +#define LOCAL_TRACE 0 |
| 36 | + |
| 37 | +#define W(w) (w & 0xff), (w >> 8) |
| 38 | +#define W3(w) (w & 0xff), ((w >> 8) & 0xff), ((w >> 16) & 0xff) |
| 39 | + |
| 40 | +/* top level device descriptor */ |
| 41 | +static const uint8_t dev_descr[] = { |
| 42 | + 0x12, /* descriptor length */ |
| 43 | + DEVICE, /* Device Descriptor type */ |
| 44 | + W(0x0200), /* USB Version */ |
| 45 | + 239, /* class */ |
| 46 | + 2, /* subclass */ |
| 47 | + 1, /* protocol */ |
| 48 | + 64, /* max packet size, ept0 */ |
| 49 | + W(0x9999), /* vendor */ |
| 50 | + W(0x9999), /* product */ |
| 51 | + W(0x9999), /* release */ |
| 52 | + 0x2, /* manufacturer string */ |
| 53 | + 0x1, /* product string */ |
| 54 | + 0x0, /* serialno string */ |
| 55 | + 0x1, /* num configs */ |
| 56 | +}; |
| 57 | + |
| 58 | +/* high/low speed device qualifier */ |
| 59 | +static const uint8_t devqual_descr[] = { |
| 60 | + 0x0a, /* len */ |
| 61 | + DEVICE_QUALIFIER, /* Device Qualifier type */ |
| 62 | + W(0x0200), /* USB version */ |
| 63 | + 0x00, /* class */ |
| 64 | + 0x00, /* subclass */ |
| 65 | + 0x00, /* protocol */ |
| 66 | + 64, /* max packet size, ept0 */ |
| 67 | + 0x01, /* num configs */ |
| 68 | + 0x00 /* reserved */ |
| 69 | +}; |
| 70 | + |
| 71 | +static const uint8_t cfg_descr[] = { |
| 72 | + 0x09, /* Length of Cfg Descr */ |
| 73 | + CONFIGURATION, /* Type of Cfg Descr */ |
| 74 | + W(0x09), /* Total Length (incl ifc, ept) */ |
| 75 | + 0x00, /* # Interfaces */ |
| 76 | + 0x01, /* Cfg Value */ |
| 77 | + 0x00, /* Cfg String */ |
| 78 | + 0xc0, /* Attributes -- self powered */ |
| 79 | + 250, /* Power Consumption - 500mA */ |
| 80 | +}; |
| 81 | + |
| 82 | +static const uchar langid[] = { 0x04, 0x03, 0x09, 0x04 }; |
| 83 | + |
| 84 | +static const uint8_t if_descriptor_lowspeed[] = { |
| 85 | + 0x09, /* length */ |
| 86 | + INTERFACE, /* type */ |
| 87 | + 0x01, /* interface num */ |
| 88 | + 0x00, /* alternates */ |
| 89 | + 0x02, /* endpoint count */ |
| 90 | + 0xff, /* interface class */ |
| 91 | + 0xff, /* interface subclass */ |
| 92 | + 0x00, /* interface protocol */ |
| 93 | + 0x00, /* string index */ |
| 94 | + |
| 95 | + /* endpoint 1 IN */ |
| 96 | + 0x07, /* length */ |
| 97 | + ENDPOINT, /* type */ |
| 98 | + 0x83, /* address: 1 IN */ |
| 99 | + 0x02, /* type: bulk */ |
| 100 | + W(64), /* max packet size: 64 */ |
| 101 | + 00, /* interval */ |
| 102 | + |
| 103 | + /* endpoint 1 OUT */ |
| 104 | + 0x07, /* length */ |
| 105 | + ENDPOINT, /* type */ |
| 106 | + 0x03, /* address: 1 OUT */ |
| 107 | + 0x02, /* type: bulk */ |
| 108 | + W(64), /* max packet size: 64 */ |
| 109 | + 00, /* interval */ |
| 110 | +}; |
| 111 | + |
| 112 | +usb_config config = { |
| 113 | + .lowspeed = { |
| 114 | + .device = USB_DESC_STATIC(dev_descr), |
| 115 | + .device_qual = USB_DESC_STATIC(devqual_descr), |
| 116 | + .config = USB_DESC_STATIC(cfg_descr), |
| 117 | + }, |
| 118 | + .highspeed = { |
| 119 | + .device = USB_DESC_STATIC(dev_descr), |
| 120 | + .device_qual = USB_DESC_STATIC(devqual_descr), |
| 121 | + .config = USB_DESC_STATIC(cfg_descr), |
| 122 | + }, |
| 123 | + |
| 124 | + .langid = USB_DESC_STATIC(langid), |
| 125 | +}; |
| 126 | + |
| 127 | +static status_t ep_cb_rx(ep_t endpoint, usbc_transfer_t *t); |
| 128 | +static status_t ep_cb_tx(ep_t endpoint, usbc_transfer_t *t); |
| 129 | + |
| 130 | +static void queue_rx(void) |
| 131 | +{ |
| 132 | + static usbc_transfer_t transfer; |
| 133 | + static uint8_t buf[512]; |
| 134 | + |
| 135 | + transfer.callback = &ep_cb_rx; |
| 136 | + transfer.result = 0; |
| 137 | + transfer.buf = &buf; |
| 138 | + transfer.buflen = sizeof(buf); |
| 139 | + transfer.bufpos = 0; |
| 140 | + transfer.extra = 0; |
| 141 | + |
| 142 | + usbc_queue_rx(3, &transfer); |
| 143 | +} |
| 144 | + |
| 145 | +static void queue_tx(void) |
| 146 | +{ |
| 147 | + static usbc_transfer_t transfer; |
| 148 | + static uint8_t buf[512]; |
| 149 | + |
| 150 | + for (uint i = 0; i < sizeof(buf); i++) { |
| 151 | + buf[i] = ~i; |
| 152 | + } |
| 153 | + |
| 154 | + transfer.callback = &ep_cb_tx; |
| 155 | + transfer.result = 0; |
| 156 | + transfer.buf = &buf; |
| 157 | + transfer.buflen = sizeof(buf); |
| 158 | + transfer.bufpos = 0; |
| 159 | + transfer.extra = 0; |
| 160 | + |
| 161 | + usbc_queue_tx(3, &transfer); |
| 162 | +} |
| 163 | + |
| 164 | +static status_t ep_cb_rx(ep_t endpoint, usbc_transfer_t *t) |
| 165 | +{ |
| 166 | +#if LOCAL_TRACE |
| 167 | + LTRACEF("ep %u transfer %p\n", endpoint, t); |
| 168 | + usbc_dump_transfer(t); |
| 169 | + |
| 170 | + if (t->result >= 0) { |
| 171 | + hexdump8(t->buf, t->bufpos); |
| 172 | + } |
| 173 | +#endif |
| 174 | + |
| 175 | + if (t->result >= 0) |
| 176 | + queue_rx(); |
| 177 | + |
| 178 | + return NO_ERROR; |
| 179 | +} |
| 180 | + |
| 181 | +static status_t ep_cb_tx(ep_t endpoint, usbc_transfer_t *t) |
| 182 | +{ |
| 183 | +#if LOCAL_TRACE |
| 184 | + LTRACEF("ep %u transfer %p\n", endpoint, t); |
| 185 | + usbc_dump_transfer(t); |
| 186 | +#endif |
| 187 | + |
| 188 | + if (t->result >= 0) |
| 189 | + queue_tx(); |
| 190 | + |
| 191 | + return NO_ERROR; |
| 192 | +} |
| 193 | + |
| 194 | +static status_t usb_cb(void *cookie, usb_callback_op_t op, const union usb_callback_args *args) |
| 195 | +{ |
| 196 | + LTRACEF("cookie %p, op %u, args %p\n", cookie, op, args); |
| 197 | + |
| 198 | + if (op == USB_CB_ONLINE) { |
| 199 | + usbc_setup_endpoint(3, USB_IN, 0x40, USB_BULK); |
| 200 | + usbc_setup_endpoint(3, USB_OUT, 0x40, USB_BULK); |
| 201 | + |
| 202 | + queue_rx(); |
| 203 | + queue_tx(); |
| 204 | + } |
| 205 | + return NO_ERROR; |
| 206 | +} |
| 207 | + |
| 208 | +void target_usb_setup(void) |
| 209 | +{ |
| 210 | + usb_setup(&config); |
| 211 | + printf("appending interfaces\n"); |
| 212 | + cdcserial_create_channel(0x1, 0x2); |
| 213 | + cdcserial_init(); |
| 214 | + |
| 215 | + usb_append_interface_lowspeed(if_descriptor_lowspeed, sizeof(if_descriptor_lowspeed)); |
| 216 | + usb_append_interface_highspeed(if_descriptor_lowspeed, sizeof(if_descriptor_lowspeed)); |
| 217 | + usb_register_callback(&usb_cb, NULL); |
| 218 | + |
| 219 | + usb_add_string("LK", 1); |
| 220 | + usb_add_string("LK Industries", 2); |
| 221 | + |
| 222 | + usb_start(); |
| 223 | +} |
0 commit comments