Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * usb_desc_app.c |
| 3 | * |
| 4 | * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com> |
| 5 | * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <no2usb/usb_proto.h> |
Sylvain Munaut | 4ea7d27 | 2020-10-29 13:17:11 +0100 | [diff] [blame] | 9 | #include <no2usb/usb_dfu_proto.h> |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 10 | #include <no2usb/usb.h> |
| 11 | |
| 12 | #define NULL ((void*)0) |
| 13 | #define num_elem(a) (sizeof(a) / sizeof(a[0])) |
| 14 | |
| 15 | |
| 16 | static const struct { |
| 17 | /* Configuration */ |
| 18 | struct usb_conf_desc conf; |
| 19 | |
| 20 | /* E1 */ |
| 21 | struct { |
Harald Welte | 30fc560 | 2020-12-14 15:56:28 +0100 | [diff] [blame] | 22 | /* Two altsettings are required, as isochronous |
| 23 | * interfaces must have a setting where they don't |
| 24 | * transceive any data. wMaxPacketSize is 0 for |
| 25 | * all endpoints in the 'off' altsetting */ |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 26 | struct { |
| 27 | struct usb_intf_desc intf; |
| 28 | struct usb_ep_desc ep_data_in; |
| 29 | struct usb_ep_desc ep_data_out; |
| 30 | struct usb_ep_desc ep_fb; |
| 31 | } __attribute__ ((packed)) off; |
| 32 | struct { |
| 33 | struct usb_intf_desc intf; |
| 34 | struct usb_ep_desc ep_data_in; |
| 35 | struct usb_ep_desc ep_data_out; |
| 36 | struct usb_ep_desc ep_fb; |
| 37 | } __attribute__ ((packed)) on; |
| 38 | } __attribute__ ((packed)) e1; |
| 39 | |
| 40 | /* CDC */ |
| 41 | #if 0 |
| 42 | struct { |
| 43 | struct usb_intf_desc intf_ctl; |
| 44 | struct usb_cs_intf_hdr_desc cs_intf_hdr; |
| 45 | struct usb_cs_intf_acm_desc cs_intf_acm; |
| 46 | struct usb_cs_intf_union_desc cs_intf_union; |
| 47 | uint8_t cs_intf_union_slave; |
| 48 | struct usb_ep_desc ep_ctl; |
| 49 | struct usb_intf_desc intf_data; |
| 50 | struct usb_ep_desc ep_data_out; |
| 51 | struct usb_ep_desc ep_data_in; |
| 52 | } __attribute__ ((packed)) cdc; |
| 53 | #endif |
| 54 | |
| 55 | /* DFU Runtime */ |
| 56 | struct { |
| 57 | struct usb_intf_desc intf; |
Sylvain Munaut | 4ea7d27 | 2020-10-29 13:17:11 +0100 | [diff] [blame] | 58 | struct usb_dfu_func_desc func; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 59 | } __attribute__ ((packed)) dfu; |
| 60 | } __attribute__ ((packed)) _app_conf_desc = { |
| 61 | .conf = { |
| 62 | .bLength = sizeof(struct usb_conf_desc), |
| 63 | .bDescriptorType = USB_DT_CONF, |
| 64 | .wTotalLength = sizeof(_app_conf_desc), |
| 65 | #if 0 |
| 66 | .bNumInterfaces = 4, |
| 67 | #else |
| 68 | .bNumInterfaces = 2, |
| 69 | #endif |
| 70 | .bConfigurationValue = 1, |
| 71 | .iConfiguration = 4, |
| 72 | .bmAttributes = 0x80, |
| 73 | .bMaxPower = 0x32, /* 100 mA */ |
| 74 | }, |
| 75 | .e1 = { |
| 76 | .off = { |
| 77 | .intf = { |
| 78 | .bLength = sizeof(struct usb_intf_desc), |
| 79 | .bDescriptorType = USB_DT_INTF, |
| 80 | .bInterfaceNumber = 0, |
| 81 | .bAlternateSetting = 0, |
| 82 | .bNumEndpoints = 3, |
| 83 | .bInterfaceClass = 0xff, |
| 84 | .bInterfaceSubClass = 0xe1, |
| 85 | .bInterfaceProtocol = 0x00, |
| 86 | .iInterface = 5, |
| 87 | }, |
| 88 | .ep_data_in = { |
| 89 | .bLength = sizeof(struct usb_ep_desc), |
| 90 | .bDescriptorType = USB_DT_EP, |
| 91 | .bEndpointAddress = 0x82, |
| 92 | .bmAttributes = 0x05, |
| 93 | .wMaxPacketSize = 0, |
| 94 | .bInterval = 1, |
| 95 | }, |
| 96 | .ep_data_out = { |
| 97 | .bLength = sizeof(struct usb_ep_desc), |
| 98 | .bDescriptorType = USB_DT_EP, |
| 99 | .bEndpointAddress = 0x01, |
| 100 | .bmAttributes = 0x05, |
| 101 | .wMaxPacketSize = 0, |
| 102 | .bInterval = 1, |
| 103 | }, |
| 104 | .ep_fb = { |
| 105 | .bLength = sizeof(struct usb_ep_desc), |
| 106 | .bDescriptorType = USB_DT_EP, |
| 107 | .bEndpointAddress = 0x81, |
| 108 | .bmAttributes = 0x11, |
| 109 | .wMaxPacketSize = 0, |
| 110 | .bInterval = 3, |
| 111 | }, |
| 112 | }, |
| 113 | .on = { |
| 114 | .intf = { |
| 115 | .bLength = sizeof(struct usb_intf_desc), |
| 116 | .bDescriptorType = USB_DT_INTF, |
| 117 | .bInterfaceNumber = 0, |
| 118 | .bAlternateSetting = 1, |
| 119 | .bNumEndpoints = 3, |
| 120 | .bInterfaceClass = 0xff, |
| 121 | .bInterfaceSubClass = 0xe1, |
| 122 | .bInterfaceProtocol = 0x00, |
| 123 | .iInterface = 5, |
| 124 | }, |
| 125 | .ep_data_in = { |
| 126 | .bLength = sizeof(struct usb_ep_desc), |
| 127 | .bDescriptorType = USB_DT_EP, |
| 128 | .bEndpointAddress = 0x82, |
| 129 | .bmAttributes = 0x05, |
| 130 | .wMaxPacketSize = 388, |
| 131 | .bInterval = 1, |
| 132 | }, |
| 133 | .ep_data_out = { |
| 134 | .bLength = sizeof(struct usb_ep_desc), |
| 135 | .bDescriptorType = USB_DT_EP, |
| 136 | .bEndpointAddress = 0x01, |
| 137 | .bmAttributes = 0x05, |
| 138 | .wMaxPacketSize = 388, |
| 139 | .bInterval = 1, |
| 140 | }, |
| 141 | .ep_fb = { |
| 142 | .bLength = sizeof(struct usb_ep_desc), |
| 143 | .bDescriptorType = USB_DT_EP, |
| 144 | .bEndpointAddress = 0x81, |
| 145 | .bmAttributes = 0x11, |
| 146 | .wMaxPacketSize = 8, |
| 147 | .bInterval = 3, |
| 148 | }, |
| 149 | }, |
| 150 | }, |
| 151 | #if 0 |
| 152 | .cdc = { |
| 153 | .intf_ctl = { |
| 154 | .bLength = sizeof(struct usb_intf_desc), |
| 155 | .bDescriptorType = USB_DT_INTF, |
| 156 | .bInterfaceNumber = 1, |
| 157 | .bAlternateSetting = 0, |
| 158 | .bNumEndpoints = 1, |
| 159 | .bInterfaceClass = 0x02, |
| 160 | .bInterfaceSubClass = 0x02, |
| 161 | .bInterfaceProtocol = 0x00, |
| 162 | .iInterface = 6, |
| 163 | }, |
| 164 | .cs_intf_hdr = { |
| 165 | .bLength = sizeof(struct usb_cs_intf_hdr_desc), |
| 166 | .bDescriptorType = USB_DT_CS_INTF, |
| 167 | .bDescriptorsubtype = 0x00, |
| 168 | .bcdCDC = 0x0110, |
| 169 | }, |
| 170 | .cs_intf_acm = { |
| 171 | .bLength = sizeof(struct usb_cs_intf_acm_desc), |
| 172 | .bDescriptorType = USB_DT_CS_INTF, |
| 173 | .bDescriptorsubtype = 0x02, |
| 174 | .bmCapabilities = 0x02, |
| 175 | }, |
| 176 | .cs_intf_union = { |
| 177 | .bLength = sizeof(struct usb_cs_intf_union_desc) + 1, |
| 178 | .bDescriptorType = USB_DT_CS_INTF, |
| 179 | .bDescriptorsubtype = 0x06, |
| 180 | .bMasterInterface = 1, |
| 181 | }, |
| 182 | .cs_intf_union_slave = 2, |
| 183 | .ep_ctl = { |
| 184 | .bLength = sizeof(struct usb_ep_desc), |
| 185 | .bDescriptorType = USB_DT_EP, |
| 186 | .bEndpointAddress = 0x84, |
| 187 | .bmAttributes = 0x03, |
| 188 | .wMaxPacketSize = 64, |
| 189 | .bInterval = 0x40, |
| 190 | }, |
| 191 | .intf_data = { |
| 192 | .bLength = sizeof(struct usb_intf_desc), |
| 193 | .bDescriptorType = USB_DT_INTF, |
| 194 | .bInterfaceNumber = 2, |
| 195 | .bAlternateSetting = 0, |
| 196 | .bNumEndpoints = 2, |
| 197 | .bInterfaceClass = 0x0a, |
| 198 | .bInterfaceSubClass = 0x00, |
| 199 | .bInterfaceProtocol = 0x00, |
| 200 | .iInterface = 7, |
| 201 | }, |
| 202 | .ep_data_out = { |
| 203 | .bLength = sizeof(struct usb_ep_desc), |
| 204 | .bDescriptorType = USB_DT_EP, |
| 205 | .bEndpointAddress = 0x05, |
| 206 | .bmAttributes = 0x02, |
| 207 | .wMaxPacketSize = 64, |
| 208 | .bInterval = 0x00, |
| 209 | }, |
| 210 | .ep_data_in = { |
| 211 | .bLength = sizeof(struct usb_ep_desc), |
| 212 | .bDescriptorType = USB_DT_EP, |
| 213 | .bEndpointAddress = 0x85, |
| 214 | .bmAttributes = 0x02, |
| 215 | .wMaxPacketSize = 64, |
| 216 | .bInterval = 0x00, |
| 217 | }, |
| 218 | }, |
| 219 | #endif |
| 220 | .dfu = { |
| 221 | .intf = { |
| 222 | .bLength = sizeof(struct usb_intf_desc), |
| 223 | .bDescriptorType = USB_DT_INTF, |
| 224 | #if 0 |
| 225 | .bInterfaceNumber = 3, |
| 226 | #else |
| 227 | .bInterfaceNumber = 1, |
| 228 | #endif |
| 229 | .bAlternateSetting = 0, |
| 230 | .bNumEndpoints = 0, |
| 231 | .bInterfaceClass = 0xfe, |
| 232 | .bInterfaceSubClass = 0x01, |
| 233 | .bInterfaceProtocol = 0x01, |
| 234 | .iInterface = 8, |
| 235 | }, |
| 236 | .func = { |
Sylvain Munaut | 4ea7d27 | 2020-10-29 13:17:11 +0100 | [diff] [blame] | 237 | .bLength = sizeof(struct usb_dfu_func_desc), |
| 238 | .bDescriptorType = USB_DFU_DT_FUNC, |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 239 | .bmAttributes = 0x0d, |
| 240 | .wDetachTimeOut = 1000, |
| 241 | .wTransferSize = 4096, |
| 242 | .bcdDFUVersion = 0x0101, |
| 243 | }, |
| 244 | }, |
| 245 | }; |
| 246 | |
| 247 | static const struct usb_conf_desc * const _conf_desc_array[] = { |
| 248 | &_app_conf_desc.conf, |
| 249 | }; |
| 250 | |
| 251 | static const struct usb_dev_desc _dev_desc = { |
| 252 | .bLength = sizeof(struct usb_dev_desc), |
| 253 | .bDescriptorType = USB_DT_DEV, |
| 254 | .bcdUSB = 0x0200, |
| 255 | .bDeviceClass = 0, |
| 256 | .bDeviceSubClass = 0, |
| 257 | .bDeviceProtocol = 0, |
| 258 | .bMaxPacketSize0 = 64, |
| 259 | .idVendor = 0x1d50, |
| 260 | .idProduct = 0x6145, |
| 261 | .bcdDevice = 0x0003, /* v0.3 */ |
| 262 | .iManufacturer = 2, |
| 263 | .iProduct = 3, |
| 264 | .iSerialNumber = 1, |
| 265 | .bNumConfigurations = num_elem(_conf_desc_array), |
| 266 | }; |
| 267 | |
| 268 | #include "usb_str_app.gen.h" |
| 269 | |
| 270 | const struct usb_stack_descriptors app_stack_desc = { |
| 271 | .dev = &_dev_desc, |
| 272 | .conf = _conf_desc_array, |
| 273 | .n_conf = num_elem(_conf_desc_array), |
| 274 | .str = _str_desc_array, |
| 275 | .n_str = num_elem(_str_desc_array), |
| 276 | }; |