1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2 * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
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.
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.
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/>.
20 // ----- Compiler Includes -----
22 #include <sys/types.h>
26 // ----- Local Includes -----
29 #include "usb-common.h"
33 // ----- Defines -----
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) \
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))
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]; \
54 .bLength = sizeof(struct usb_desc_string_t) + \
56 .bDescriptorType = USB_DESC_STRING, \
60 #define USB_DESC_STRING_LANG_ENUS USB_DESC_STRING(u"\x0409")
61 #define USB_DESC_STRING_SERIALNO ((const void *)1)
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) \
67 #define USB_FUNCTION_RX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \
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 +
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,
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__) \
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, \
101 __REPEAT_INNER((0, 0, 0), USB__COUNT_IFACE_EP, USB__INIT_FUNCTION_DESC, __VA_ARGS__) \
105 #define USB__DEFINE_CONFIG(iter, args) \
106 __DEFER(USB__DEFINE_CONFIG_)(iter, __EXPAND args)
108 #define USB__DEFINE_CONFIG_(confignum, initfun, ...) \
109 &(const struct usbd_config){ \
111 .desc = USB__DEFINE_CONFIG_DESC( \
113 __CAT(__usb_desc, __COUNTER__), \
117 #define USB_INIT_DEVICE(vid, pid, manuf, product, ...) \
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, \
129 .bcdDevice = { .raw = 0 }, \
130 .iManufacturer = 1, \
132 .iSerialNumber = 3, \
133 .bNumConfigurations = __PP_NARG(__VA_ARGS__), \
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, \
143 __REPEAT(1, USB__INCREMENT, USB__DEFINE_CONFIG, __VA_ARGS__) \
150 // ----- Structs & Enumerations -----
153 * Note: bitfields ahead.
154 * GCC fills the fields lsb-to-msb on little endian.
167 USB_DESC_DEVQUAL = 6,
168 USB_DESC_OTHERSPEED = 7,
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
183 CTASSERT_SIZE_BYTE(struct usb_desc_type_t, 1);
186 USB_DEV_CLASS_SEE_IFACE = 0,
187 USB_DEV_CLASS_APP = 0xfe,
188 USB_DEV_CLASS_VENDOR = 0xff
191 enum usb_dev_subclass {
192 USB_DEV_SUBCLASS_SEE_IFACE = 0,
193 USB_DEV_SUBCLASS_VENDOR = 0xff
197 USB_DEV_PROTO_SEE_IFACE = 0,
198 USB_DEV_PROTO_VENDOR = 0xff
202 UNION_STRUCT_START(16);
210 CTASSERT_SIZE_BYTE(struct usb_bcd_t, 2);
212 struct usb_desc_generic_t {
214 struct usb_desc_type_t bDescriptorType;
217 CTASSERT_SIZE_BYTE(struct usb_desc_generic_t, 2);
219 struct usb_desc_dev_t {
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;
229 struct usb_bcd_t bcdDevice;
230 uint8_t iManufacturer;
232 uint8_t iSerialNumber;
233 uint8_t bNumConfigurations;
235 CTASSERT_SIZE_BYTE(struct usb_desc_dev_t, 18);
237 struct usb_desc_ep_t {
239 enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_EP */
246 uint8_t bEndpointAddress;
255 enum usb_ep_iso_synctype {
256 USB_EP_ISO_NOSYNC = 0,
257 USB_EP_ISO_ASYNC = 1,
258 USB_EP_ISO_ADAPTIVE = 2,
261 enum usb_ep_iso_usagetype {
263 USB_EP_ISO_FEEDBACK = 1,
264 USB_EP_ISO_IMPLICIT = 2
269 uint16_t wMaxPacketSize : 11;
274 CTASSERT_SIZE_BYTE(struct usb_desc_ep_t, 7);
276 struct usb_desc_iface_t {
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;
287 CTASSERT_SIZE_BYTE(struct usb_desc_iface_t, 9);
289 struct usb_desc_config_t {
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;
298 uint8_t remote_wakeup : 1;
299 uint8_t self_powered : 1;
300 uint8_t one : 1; /* = 1 for historical reasons */
302 uint8_t bMaxPower; /* units of 2mA */
304 CTASSERT_SIZE_BYTE(struct usb_desc_config_t, 9);
306 struct usb_desc_string_t {
308 enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_STRING */
309 const char16_t bString[];
311 CTASSERT_SIZE_BYTE(struct usb_desc_string_t, 2);
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,
322 USB_CTRL_REQ_OTHER = 3
324 enum usb_ctrl_req_type {
325 USB_CTRL_REQ_STD = 0,
326 USB_CTRL_REQ_CLASS = 1,
327 USB_CTRL_REQ_VENDOR = 2
329 enum usb_ctrl_req_dir {
330 USB_CTRL_REQ_OUT = 0,
334 uint8_t bmRequestType;
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
350 uint16_t type_and_req;
362 CTASSERT_SIZE_BYTE(struct usb_ctrl_req_t, 8);
365 * status replies for GET_STATUS.
368 struct usb_ctrl_req_status_dev_t {
369 uint16_t self_powered : 1;
370 uint16_t remote_wakeup : 1;
373 CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_dev_t, 16);
375 struct usb_ctrl_req_status_iface_t {
378 CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_iface_t, 16);
380 struct usb_ctrl_req_status_ep_t {
384 CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_ep_t, 16);
387 * Descriptor type (in req->value) for GET_DESCRIPTOR.
389 struct usb_ctrl_req_desc_t {
391 enum usb_desc_type type : 8;
393 CTASSERT_SIZE_BIT(struct usb_ctrl_req_desc_t, 16);
396 * Feature selector (in req->value) for CLEAR_FEATURE.
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
405 struct usb_xfer_info;
406 typedef void (*ep_callback_t)(void *buf, ssize_t len, void *data);
409 * (Artificial) function. Aggregates one or more interfaces.
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 *);
419 struct usbd_function_ctx_header {
420 struct usbd_function_ctx_header *next;
421 const struct usbd_function *function;
422 int interface_offset;
428 typedef void (usbd_init_fun_t)(int);
429 typedef void (usbd_suspend_resume_fun_t)(void);
432 * Configuration. Contains one or more functions which all will be
433 * active concurrently.
436 usbd_init_fun_t *init;
437 usbd_suspend_resume_fun_t *suspend;
438 usbd_suspend_resume_fun_t *resume;
440 * We will not set a config for now, because there is not much to
441 * configure, except for power
443 * const struct usb_desc_config_t *config_desc;
445 const struct usb_desc_config_t *desc;
446 const struct usbd_function *function[];
451 * Device. Contains one or more configurations, out of which only one
452 * is active at a time.
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[];
462 /* Provided by MD code */
463 struct usbd_ep_pipe_state_t;
467 // ----- Functions -----
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);
483 void vusb_main_loop(void);
487 int usb_tx_serialno(size_t reqlen);
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 *);
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 *);
507 // ----- DFU USB Additional Includes -----