]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/usb.h
Code cleanup
[kiibohd-controller.git] / Bootloader / usb.h
1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2  * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #pragma once
19
20 // ----- Compiler Includes -----
21
22 #include <sys/types.h>
23
24
25
26 // ----- Local Includes -----
27
28 #include "mchck.h"
29 #include "usb-common.h"
30
31
32
33 // ----- Defines -----
34
35 #define USB_CTRL_REQ_DIR_SHIFT 0
36 #define USB_CTRL_REQ_TYPE_SHIFT 1
37 #define USB_CTRL_REQ_RECP_SHIFT 3
38 #define USB_CTRL_REQ_CODE_SHIFT 8
39 #define USB_CTRL_REQ(req_inout, req_type, req_code)                     \
40         (uint16_t)                                                      \
41         ((USB_CTRL_REQ_##req_inout << USB_CTRL_REQ_DIR_SHIFT)           \
42          |(USB_CTRL_REQ_##req_type << USB_CTRL_REQ_TYPE_SHIFT)          \
43          |(USB_CTRL_REQ_##req_code << USB_CTRL_REQ_CODE_SHIFT))
44
45
46
47 // ----- Macros -----
48
49 #define USB_DESC_STRING(s)                                      \
50         (const void *)&(const struct {                          \
51                         struct usb_desc_string_t dsc;           \
52                         char16_t str[sizeof(s) / 2 - 1];        \
53         }) {{                                           \
54                         .bLength = sizeof(struct usb_desc_string_t) +   \
55                                 sizeof(s) - 2,                  \
56                         .bDescriptorType = USB_DESC_STRING,     \
57                         },                                      \
58                         s                                       \
59         }
60 #define USB_DESC_STRING_LANG_ENUS USB_DESC_STRING(u"\x0409")
61 #define USB_DESC_STRING_SERIALNO ((const void *)1)
62
63 #define USB_FUNCTION_IFACE(iface, iface_off, tx_ep_off, rx_ep_off)      \
64         ((iface_off) + (iface))
65 #define USB_FUNCTION_TX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \
66         ((tx_ep_off) + (ep))
67 #define USB_FUNCTION_RX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \
68         ((rx_ep_off) + (ep))
69
70
71 #define USB__INCREMENT(i, _0) (i + 1)
72 #define USB__COUNT_IFACE_EP(i, e)                       \
73         __DEFER(USB__COUNT_IFACE_EP_)(__EXPAND i, e)
74 #define USB__COUNT_IFACE_EP_(iface, tx_ep, rx_ep, func)   \
75         (iface + USB_FUNCTION_ ## func ## _IFACE_COUNT,   \
76          tx_ep + USB_FUNCTION_ ## func ## _TX_EP_COUNT,   \
77          rx_ep + USB_FUNCTION_ ## func ## _RX_EP_COUNT)
78 #define USB__GET_FUNCTION_IFACE_COUNT(iter, func)       \
79         USB_FUNCTION_ ## func ## _IFACE_COUNT +
80
81 #define USB__DEFINE_FUNCTION_DESC(iter, func)                           \
82         USB_FUNCTION_DESC_ ## func ## _DECL __CAT(__usb_func_desc, __COUNTER__);
83 #define USB__INIT_FUNCTION_DESC(iter, func)     \
84         USB_FUNCTION_DESC_ ## func iter,
85
86 #define USB__DEFINE_CONFIG_DESC(confignum, name, ...)                   \
87         &((const struct name {                                          \
88                 struct usb_desc_config_t config;                        \
89                 __REPEAT_INNER(, __EAT, USB__DEFINE_FUNCTION_DESC, __VA_ARGS__) \
90         }){                                                             \
91                 .config = {                                             \
92                         .bLength = sizeof(struct usb_desc_config_t),    \
93                         .bDescriptorType = USB_DESC_CONFIG,             \
94                         .wTotalLength = sizeof(struct name),            \
95                         .bNumInterfaces = __REPEAT_INNER(, __EAT, USB__GET_FUNCTION_IFACE_COUNT, __VA_ARGS__) 0, \
96                         .bConfigurationValue = confignum,               \
97                         .iConfiguration = 0,                            \
98                         .one = 1,                                       \
99                         .bMaxPower = 50                                 \
100                 },                                                      \
101                 __REPEAT_INNER((0, 0, 0), USB__COUNT_IFACE_EP, USB__INIT_FUNCTION_DESC, __VA_ARGS__) \
102         }).config
103
104
105 #define USB__DEFINE_CONFIG(iter, args)                          \
106         __DEFER(USB__DEFINE_CONFIG_)(iter, __EXPAND args)
107
108 #define USB__DEFINE_CONFIG_(confignum, initfun, ...)                    \
109         &(const struct usbd_config){                                    \
110                 .init = initfun,                                        \
111                 .desc = USB__DEFINE_CONFIG_DESC(                        \
112                         confignum,                                      \
113                         __CAT(__usb_desc, __COUNTER__),                 \
114                         __VA_ARGS__)                                    \
115         },
116
117 #define USB_INIT_DEVICE(vid, pid, manuf, product, ...)                  \
118         {                                                               \
119                 .dev_desc = &(const struct usb_desc_dev_t){             \
120                         .bLength = sizeof(struct usb_desc_dev_t),       \
121                         .bDescriptorType = USB_DESC_DEV,                \
122                         .bcdUSB = { .maj = 2 },                         \
123                         .bDeviceClass = USB_DEV_CLASS_SEE_IFACE,        \
124                         .bDeviceSubClass = USB_DEV_SUBCLASS_SEE_IFACE,  \
125                         .bDeviceProtocol = USB_DEV_PROTO_SEE_IFACE,     \
126                         .bMaxPacketSize0 = EP0_BUFSIZE,                 \
127                         .idVendor = vid,                                \
128                         .idProduct = pid,                               \
129                         .bcdDevice = { .raw = 0 },                      \
130                         .iManufacturer = 1,                             \
131                         .iProduct = 2,                                  \
132                         .iSerialNumber = 3,                             \
133                         .bNumConfigurations = __PP_NARG(__VA_ARGS__),   \
134                 },                                                      \
135                 .string_descs = (const struct usb_desc_string_t * const []){ \
136                         USB_DESC_STRING_LANG_ENUS,                      \
137                         USB_DESC_STRING(manuf),                         \
138                         USB_DESC_STRING(product),                       \
139                         USB_DESC_STRING_SERIALNO,                       \
140                         NULL                                            \
141                 },                                                      \
142                 .configs = {                                            \
143                         __REPEAT(1, USB__INCREMENT, USB__DEFINE_CONFIG, __VA_ARGS__) \
144                         NULL                                            \
145                 }                                                       \
146         }
147
148
149
150 // ----- Structs & Enumerations -----
151
152 /**
153  * Note: bitfields ahead.
154  * GCC fills the fields lsb-to-msb on little endian.
155  */
156
157 /**
158  * USB descriptors
159  */
160
161 enum usb_desc_type {
162         USB_DESC_DEV = 1,
163         USB_DESC_CONFIG = 2,
164         USB_DESC_STRING = 3,
165         USB_DESC_IFACE = 4,
166         USB_DESC_EP = 5,
167         USB_DESC_DEVQUAL = 6,
168         USB_DESC_OTHERSPEED = 7,
169         USB_DESC_POWER = 8
170 };
171
172 struct usb_desc_type_t {
173         UNION_STRUCT_START(8);
174         enum usb_desc_type id : 5;
175         enum usb_desc_type_type {
176                 USB_DESC_TYPE_STD = 0,
177                 USB_DESC_TYPE_CLASS = 1,
178                 USB_DESC_TYPE_VENDOR = 2
179         } type_type : 2;
180         uint8_t _rsvd0 : 1;
181         UNION_STRUCT_END;
182 };
183 CTASSERT_SIZE_BYTE(struct usb_desc_type_t, 1);
184
185 enum usb_dev_class {
186         USB_DEV_CLASS_SEE_IFACE = 0,
187         USB_DEV_CLASS_APP = 0xfe,
188         USB_DEV_CLASS_VENDOR = 0xff
189 };
190
191 enum usb_dev_subclass {
192         USB_DEV_SUBCLASS_SEE_IFACE = 0,
193         USB_DEV_SUBCLASS_VENDOR = 0xff
194 };
195
196 enum usb_dev_proto {
197         USB_DEV_PROTO_SEE_IFACE = 0,
198         USB_DEV_PROTO_VENDOR = 0xff
199 };
200
201 struct usb_bcd_t {
202         UNION_STRUCT_START(16);
203         struct {
204                 uint8_t sub : 4;
205                 uint8_t min : 4;
206                 uint16_t maj : 8;
207         };
208         UNION_STRUCT_END;
209 };
210 CTASSERT_SIZE_BYTE(struct usb_bcd_t, 2);
211
212 struct usb_desc_generic_t {
213         uint8_t bLength;
214         struct usb_desc_type_t bDescriptorType;
215         uint8_t data[];
216 };
217 CTASSERT_SIZE_BYTE(struct usb_desc_generic_t, 2);
218
219 struct usb_desc_dev_t {
220         uint8_t bLength;
221         enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_DEV */
222         struct usb_bcd_t bcdUSB;             /* = 0x0200 */
223         enum usb_dev_class bDeviceClass : 8;
224         enum usb_dev_subclass bDeviceSubClass : 8;
225         enum usb_dev_proto bDeviceProtocol : 8;
226         uint8_t bMaxPacketSize0;
227         uint16_t idVendor;
228         uint16_t idProduct;
229         struct usb_bcd_t bcdDevice;
230         uint8_t iManufacturer;
231         uint8_t iProduct;
232         uint8_t iSerialNumber;
233         uint8_t bNumConfigurations;
234 };
235 CTASSERT_SIZE_BYTE(struct usb_desc_dev_t, 18);
236
237 struct usb_desc_ep_t {
238         uint8_t bLength;
239         enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_EP */
240         union {
241                 struct {
242                         uint8_t ep_num : 4;
243                         uint8_t _rsvd0 : 3;
244                         uint8_t in : 1;
245                 };
246                 uint8_t bEndpointAddress;
247         };
248         struct {
249                 enum usb_ep_type {
250                         USB_EP_CONTROL = 0,
251                         USB_EP_ISO = 1,
252                         USB_EP_BULK = 2,
253                         USB_EP_INTR = 3
254                 } type : 2;
255                 enum usb_ep_iso_synctype {
256                         USB_EP_ISO_NOSYNC = 0,
257                         USB_EP_ISO_ASYNC = 1,
258                         USB_EP_ISO_ADAPTIVE = 2,
259                         USB_EP_ISO_SYNC = 3
260                 } sync_type : 2;
261                 enum usb_ep_iso_usagetype {
262                         USB_EP_ISO_DATA = 0,
263                         USB_EP_ISO_FEEDBACK = 1,
264                         USB_EP_ISO_IMPLICIT = 2
265                 } usage_type : 2;
266                 uint8_t _rsvd1 : 2;
267         };
268         struct {
269                 uint16_t wMaxPacketSize : 11;
270                 uint16_t _rsvd2 : 5;
271         };
272         uint8_t bInterval;
273 } __packed;
274 CTASSERT_SIZE_BYTE(struct usb_desc_ep_t, 7);
275
276 struct usb_desc_iface_t {
277         uint8_t bLength;
278         enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_IFACE */
279         uint8_t bInterfaceNumber;
280         uint8_t bAlternateSetting;
281         uint8_t bNumEndpoints;
282         enum usb_dev_class bInterfaceClass : 8;
283         enum usb_dev_subclass bInterfaceSubClass: 8;
284         enum usb_dev_proto bInterfaceProtocol : 8;
285         uint8_t iInterface;
286 };
287 CTASSERT_SIZE_BYTE(struct usb_desc_iface_t, 9);
288
289 struct usb_desc_config_t {
290         uint8_t bLength;
291         enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_CONFIG */
292         uint16_t wTotalLength;       /* size of config, iface, ep */
293         uint8_t bNumInterfaces;
294         uint8_t bConfigurationValue;
295         uint8_t iConfiguration;
296         struct {
297                 uint8_t _rsvd0 : 5;
298                 uint8_t remote_wakeup : 1;
299                 uint8_t self_powered : 1;
300                 uint8_t one : 1; /* = 1 for historical reasons */
301         };
302         uint8_t bMaxPower;      /* units of 2mA */
303 } __packed;
304 CTASSERT_SIZE_BYTE(struct usb_desc_config_t, 9);
305
306 struct usb_desc_string_t {
307         uint8_t bLength;
308         enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_STRING */
309         const char16_t bString[];
310 };
311 CTASSERT_SIZE_BYTE(struct usb_desc_string_t, 2);
312
313 struct usb_ctrl_req_t {
314         union /* reqtype and request & u16 */ {
315                 struct /* reqtype and request */ {
316                         union /* reqtype in bitfield & u8 */ {
317                                 struct /* reqtype */ {
318                                         enum usb_ctrl_req_recp {
319                                                 USB_CTRL_REQ_DEV = 0,
320                                                 USB_CTRL_REQ_IFACE = 1,
321                                                 USB_CTRL_REQ_EP = 2,
322                                                 USB_CTRL_REQ_OTHER = 3
323                                         } recp : 5;
324                                         enum usb_ctrl_req_type {
325                                                 USB_CTRL_REQ_STD = 0,
326                                                 USB_CTRL_REQ_CLASS = 1,
327                                                 USB_CTRL_REQ_VENDOR = 2
328                                         } type : 2;
329                                         enum usb_ctrl_req_dir {
330                                                 USB_CTRL_REQ_OUT = 0,
331                                                 USB_CTRL_REQ_IN = 1
332                                         } in : 1;
333                                 };
334                                 uint8_t bmRequestType;
335                         }; /* union */
336                         enum usb_ctrl_req_code {
337                                 USB_CTRL_REQ_GET_STATUS = 0,
338                                 USB_CTRL_REQ_CLEAR_FEATURE = 1,
339                                 USB_CTRL_REQ_SET_FEATURE = 3,
340                                 USB_CTRL_REQ_SET_ADDRESS = 5,
341                                 USB_CTRL_REQ_GET_DESCRIPTOR = 6,
342                                 USB_CTRL_REQ_SET_DESCRIPTOR = 7,
343                                 USB_CTRL_REQ_GET_CONFIGURATION = 8,
344                                 USB_CTRL_REQ_SET_CONFIGURATION = 9,
345                                 USB_CTRL_REQ_GET_INTERFACE = 10,
346                                 USB_CTRL_REQ_SET_INTERFACE = 11,
347                                 USB_CTRL_REQ_SYNC_FRAME = 12
348                         } bRequest : 8;
349                 }; /* struct */
350                 uint16_t type_and_req;
351         }; /* union */
352         union {
353                 uint16_t wValue;
354                 struct {
355                         uint8_t wValueLow;
356                         uint8_t wValueHigh;
357                 };
358         };
359         uint16_t wIndex;
360         uint16_t wLength;
361 };
362 CTASSERT_SIZE_BYTE(struct usb_ctrl_req_t, 8);
363
364 /**
365  * status replies for GET_STATUS.
366  */
367
368 struct usb_ctrl_req_status_dev_t {
369         uint16_t self_powered : 1;
370         uint16_t remote_wakeup : 1;
371         uint16_t _rsvd : 14;
372 };
373 CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_dev_t, 16);
374
375 struct usb_ctrl_req_status_iface_t {
376         uint16_t _rsvd;
377 };
378 CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_iface_t, 16);
379
380 struct usb_ctrl_req_status_ep_t {
381         uint16_t halt : 1;
382         uint16_t _rsvd : 15;
383 };
384 CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_ep_t, 16);
385
386 /**
387  * Descriptor type (in req->value) for GET_DESCRIPTOR.
388  */
389 struct usb_ctrl_req_desc_t {
390         uint8_t idx;
391         enum usb_desc_type type : 8;
392 };
393 CTASSERT_SIZE_BIT(struct usb_ctrl_req_desc_t, 16);
394
395 /**
396  * Feature selector (in req->value) for CLEAR_FEATURE.
397  */
398 enum usb_ctrl_req_feature {
399         USB_CTRL_REQ_FEAT_EP_HALT = 0,
400         USB_CTRL_REQ_FEAT_DEV_REMOTE_WKUP = 1,
401         USB_CTRL_REQ_FEAT_TEST_MODE = 2
402 };
403
404
405 struct usb_xfer_info;
406 typedef void (*ep_callback_t)(void *buf, ssize_t len, void *data);
407
408 /**
409  * (Artificial) function.  Aggregates one or more interfaces.
410  */
411 struct usbd_function {
412         int (*configure)(int orig_iface, int iface, int altsetting, void *data);
413         int (*control)(struct usb_ctrl_req_t *, void *);
414         int interface_count;
415         int ep_rx_count;
416         int ep_tx_count;
417 };
418
419 struct usbd_function_ctx_header {
420         struct usbd_function_ctx_header *next;
421         const struct usbd_function *function;
422         int interface_offset;
423         int ep_rx_offset;
424         int ep_tx_offset;
425 };
426
427
428 typedef void (usbd_init_fun_t)(int);
429 typedef void (usbd_suspend_resume_fun_t)(void);
430
431 /**
432  * Configuration.  Contains one or more functions which all will be
433  * active concurrently.
434  */
435 struct usbd_config {
436         usbd_init_fun_t *init;
437         usbd_suspend_resume_fun_t *suspend;
438         usbd_suspend_resume_fun_t *resume;
439         /**
440          * We will not set a config for now, because there is not much to
441          * configure, except for power
442          *
443          * const struct usb_desc_config_t *config_desc;
444          */
445         const struct usb_desc_config_t *desc;
446         const struct usbd_function *function[];
447 };
448
449
450 /**
451  * Device.  Contains one or more configurations, out of which only one
452  * is active at a time.
453  */
454 struct usbd_device {
455         const struct usb_desc_dev_t *dev_desc;
456         const struct usb_desc_string_t * const *string_descs;
457         const struct usbd_config *configs[];
458 };
459
460
461
462 /* Provided by MD code */
463 struct usbd_ep_pipe_state_t;
464
465
466
467 // ----- Functions -----
468
469 void *usb_get_xfer_data(struct usb_xfer_info *);
470 enum usb_tok_pid usb_get_xfer_pid(struct usb_xfer_info *);
471 int usb_get_xfer_ep(struct usb_xfer_info *);
472 enum usb_ep_dir usb_get_xfer_dir(struct usb_xfer_info *);
473 void usb_enable_xfers(void);
474 void usb_set_addr(int);
475 void usb_ep_stall(int);
476 size_t usb_ep_get_transfer_size(struct usbd_ep_pipe_state_t *);
477 void usb_queue_next(struct usbd_ep_pipe_state_t *, void *, size_t);
478 void usb_pipe_stall(struct usbd_ep_pipe_state_t *);
479 void usb_pipe_unstall(struct usbd_ep_pipe_state_t *);
480 void usb_pipe_enable(struct usbd_ep_pipe_state_t *s);
481 void usb_pipe_disable(struct usbd_ep_pipe_state_t *s);
482 #ifdef VUSB
483 void vusb_main_loop(void);
484 #else
485 void usb_poll(void);
486 #endif
487 int usb_tx_serialno(size_t reqlen);
488
489 /* Provided by MI code */
490 void usb_init(const struct usbd_device *);
491 void usb_attach_function(const struct usbd_function *function, struct usbd_function_ctx_header *ctx);
492 void usb_handle_transaction(struct usb_xfer_info *);
493 void usb_setup_control(void);
494 void usb_handle_control_status_cb(ep_callback_t cb);
495 void usb_handle_control_status(int);
496 struct usbd_ep_pipe_state_t *usb_init_ep(struct usbd_function_ctx_header *ctx, int ep, enum usb_ep_dir dir, size_t size);
497 int usb_rx(struct usbd_ep_pipe_state_t *, void *, size_t, ep_callback_t, void *);
498 int usb_tx(struct usbd_ep_pipe_state_t *, const void *, size_t, size_t, ep_callback_t, void *);
499
500 int usb_ep0_rx(void *, size_t, ep_callback_t, void *);
501 void *usb_ep0_tx_inplace_prepare(size_t len);
502 int usb_ep0_tx(void *buf, size_t len, size_t reqlen, ep_callback_t cb, void *cb_data);
503 int usb_ep0_tx_cp(const void *, size_t, size_t, ep_callback_t, void *);
504
505
506
507 // ----- DFU USB Additional Includes -----
508
509 #include "dfu.h"
510