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