1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2 * Modifications by Jacob Alexander 2014 <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/>.
21 // ----- Compiler Includes -----
23 #include <sys/types.h>
27 // ----- Local Includes -----
30 #include "usb-common.h"
34 // ----- Defines -----
36 #define USB_CTRL_REQ_DIR_SHIFT 0
37 #define USB_CTRL_REQ_TYPE_SHIFT 1
38 #define USB_CTRL_REQ_RECP_SHIFT 3
39 #define USB_CTRL_REQ_CODE_SHIFT 8
40 #define USB_CTRL_REQ(req_inout, req_type, req_code) \
42 ((USB_CTRL_REQ_##req_inout << USB_CTRL_REQ_DIR_SHIFT) \
43 |(USB_CTRL_REQ_##req_type << USB_CTRL_REQ_TYPE_SHIFT) \
44 |(USB_CTRL_REQ_##req_code << USB_CTRL_REQ_CODE_SHIFT))
50 #define USB_DESC_STRING(s) \
51 (const void *)&(const struct { \
52 struct usb_desc_string_t dsc; \
53 char16_t str[sizeof(s) / 2 - 1]; \
55 .bLength = sizeof(struct usb_desc_string_t) + \
57 .bDescriptorType = USB_DESC_STRING, \
61 #define USB_DESC_STRING_LANG_ENUS USB_DESC_STRING(u"\x0409")
62 #define USB_DESC_STRING_SERIALNO ((const void *)1)
64 #define USB_FUNCTION_IFACE(iface, iface_off, tx_ep_off, rx_ep_off) \
65 ((iface_off) + (iface))
66 #define USB_FUNCTION_TX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \
68 #define USB_FUNCTION_RX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \
72 #define USB__INCREMENT(i, _0) (i + 1)
73 #define USB__COUNT_IFACE_EP(i, e) \
74 __DEFER(USB__COUNT_IFACE_EP_)(__EXPAND i, e)
75 #define USB__COUNT_IFACE_EP_(iface, tx_ep, rx_ep, func) \
76 (iface + USB_FUNCTION_ ## func ## _IFACE_COUNT, \
77 tx_ep + USB_FUNCTION_ ## func ## _TX_EP_COUNT, \
78 rx_ep + USB_FUNCTION_ ## func ## _RX_EP_COUNT)
79 #define USB__GET_FUNCTION_IFACE_COUNT(iter, func) \
80 USB_FUNCTION_ ## func ## _IFACE_COUNT +
82 #define USB__DEFINE_FUNCTION_DESC(iter, func) \
83 USB_FUNCTION_DESC_ ## func ## _DECL __CAT(__usb_func_desc, __COUNTER__);
84 #define USB__INIT_FUNCTION_DESC(iter, func) \
85 USB_FUNCTION_DESC_ ## func iter,
87 #define USB__DEFINE_CONFIG_DESC(confignum, name, ...) \
88 &((const struct name { \
89 struct usb_desc_config_t config; \
90 __REPEAT_INNER(, __EAT, USB__DEFINE_FUNCTION_DESC, __VA_ARGS__) \
93 .bLength = sizeof(struct usb_desc_config_t), \
94 .bDescriptorType = USB_DESC_CONFIG, \
95 .wTotalLength = sizeof(struct name), \
96 .bNumInterfaces = __REPEAT_INNER(, __EAT, USB__GET_FUNCTION_IFACE_COUNT, __VA_ARGS__) 0, \
97 .bConfigurationValue = confignum, \
98 .iConfiguration = 0, \
102 __REPEAT_INNER((0, 0, 0), USB__COUNT_IFACE_EP, USB__INIT_FUNCTION_DESC, __VA_ARGS__) \
106 #define USB__DEFINE_CONFIG(iter, args) \
107 __DEFER(USB__DEFINE_CONFIG_)(iter, __EXPAND args)
109 #define USB__DEFINE_CONFIG_(confignum, initfun, ...) \
110 &(const struct usbd_config){ \
112 .desc = USB__DEFINE_CONFIG_DESC( \
114 __CAT(__usb_desc, __COUNTER__), \
118 #define USB_INIT_DEVICE(vid, pid, manuf, product, ...) \
120 .dev_desc = &(const struct usb_desc_dev_t){ \
121 .bLength = sizeof(struct usb_desc_dev_t), \
122 .bDescriptorType = USB_DESC_DEV, \
123 .bcdUSB = { .maj = 2 }, \
124 .bDeviceClass = USB_DEV_CLASS_SEE_IFACE, \
125 .bDeviceSubClass = USB_DEV_SUBCLASS_SEE_IFACE, \
126 .bDeviceProtocol = USB_DEV_PROTO_SEE_IFACE, \
127 .bMaxPacketSize0 = EP0_BUFSIZE, \
130 .bcdDevice = { .raw = 0 }, \
131 .iManufacturer = 1, \
133 .iSerialNumber = 3, \
134 .bNumConfigurations = __PP_NARG(__VA_ARGS__), \
136 .string_descs = (const struct usb_desc_string_t * const []){ \
137 USB_DESC_STRING_LANG_ENUS, \
138 USB_DESC_STRING(manuf), \
139 USB_DESC_STRING(product), \
140 USB_DESC_STRING_SERIALNO, \
144 __REPEAT(1, USB__INCREMENT, USB__DEFINE_CONFIG, __VA_ARGS__) \
151 // ----- Structs & Enumerations -----
154 * Note: bitfields ahead.
155 * GCC fills the fields lsb-to-msb on little endian.
168 USB_DESC_DEVQUAL = 6,
169 USB_DESC_OTHERSPEED = 7,
173 struct usb_desc_type_t {
174 UNION_STRUCT_START(8);
175 enum usb_desc_type id : 5;
176 enum usb_desc_type_type {
177 USB_DESC_TYPE_STD = 0,
178 USB_DESC_TYPE_CLASS = 1,
179 USB_DESC_TYPE_VENDOR = 2
184 CTASSERT_SIZE_BYTE(struct usb_desc_type_t, 1);
187 USB_DEV_CLASS_SEE_IFACE = 0,
188 USB_DEV_CLASS_APP = 0xfe,
189 USB_DEV_CLASS_VENDOR = 0xff
192 enum usb_dev_subclass {
193 USB_DEV_SUBCLASS_SEE_IFACE = 0,
194 USB_DEV_SUBCLASS_VENDOR = 0xff
198 USB_DEV_PROTO_SEE_IFACE = 0,
199 USB_DEV_PROTO_VENDOR = 0xff
203 UNION_STRUCT_START(16);
211 CTASSERT_SIZE_BYTE(struct usb_bcd_t, 2);
213 struct usb_desc_generic_t {
215 struct usb_desc_type_t bDescriptorType;
218 CTASSERT_SIZE_BYTE(struct usb_desc_generic_t, 2);
220 struct usb_desc_dev_t {
222 enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_DEV */
223 struct usb_bcd_t bcdUSB; /* = 0x0200 */
224 enum usb_dev_class bDeviceClass : 8;
225 enum usb_dev_subclass bDeviceSubClass : 8;
226 enum usb_dev_proto bDeviceProtocol : 8;
227 uint8_t bMaxPacketSize0;
230 struct usb_bcd_t bcdDevice;
231 uint8_t iManufacturer;
233 uint8_t iSerialNumber;
234 uint8_t bNumConfigurations;
236 CTASSERT_SIZE_BYTE(struct usb_desc_dev_t, 18);
238 struct usb_desc_ep_t {
240 enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_EP */
247 uint8_t bEndpointAddress;
256 enum usb_ep_iso_synctype {
257 USB_EP_ISO_NOSYNC = 0,
258 USB_EP_ISO_ASYNC = 1,
259 USB_EP_ISO_ADAPTIVE = 2,
262 enum usb_ep_iso_usagetype {
264 USB_EP_ISO_FEEDBACK = 1,
265 USB_EP_ISO_IMPLICIT = 2
270 uint16_t wMaxPacketSize : 11;
275 CTASSERT_SIZE_BYTE(struct usb_desc_ep_t, 7);
277 struct usb_desc_iface_t {
279 enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_IFACE */
280 uint8_t bInterfaceNumber;
281 uint8_t bAlternateSetting;
282 uint8_t bNumEndpoints;
283 enum usb_dev_class bInterfaceClass : 8;
284 enum usb_dev_subclass bInterfaceSubClass: 8;
285 enum usb_dev_proto bInterfaceProtocol : 8;
288 CTASSERT_SIZE_BYTE(struct usb_desc_iface_t, 9);
290 struct usb_desc_config_t {
292 enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_CONFIG */
293 uint16_t wTotalLength; /* size of config, iface, ep */
294 uint8_t bNumInterfaces;
295 uint8_t bConfigurationValue;
296 uint8_t iConfiguration;
299 uint8_t remote_wakeup : 1;
300 uint8_t self_powered : 1;
301 uint8_t one : 1; /* = 1 for historical reasons */
303 uint8_t bMaxPower; /* units of 2mA */
305 CTASSERT_SIZE_BYTE(struct usb_desc_config_t, 9);
307 struct usb_desc_string_t {
309 enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_STRING */
310 const char16_t bString[];
312 CTASSERT_SIZE_BYTE(struct usb_desc_string_t, 2);
314 struct usb_ctrl_req_t {
315 union /* reqtype and request & u16 */ {
316 struct /* reqtype and request */ {
317 union /* reqtype in bitfield & u8 */ {
318 struct /* reqtype */ {
319 enum usb_ctrl_req_recp {
320 USB_CTRL_REQ_DEV = 0,
321 USB_CTRL_REQ_IFACE = 1,
323 USB_CTRL_REQ_OTHER = 3
325 enum usb_ctrl_req_type {
326 USB_CTRL_REQ_STD = 0,
327 USB_CTRL_REQ_CLASS = 1,
328 USB_CTRL_REQ_VENDOR = 2
330 enum usb_ctrl_req_dir {
331 USB_CTRL_REQ_OUT = 0,
335 uint8_t bmRequestType;
337 enum usb_ctrl_req_code {
338 USB_CTRL_REQ_GET_STATUS = 0,
339 USB_CTRL_REQ_CLEAR_FEATURE = 1,
340 USB_CTRL_REQ_SET_FEATURE = 3,
341 USB_CTRL_REQ_SET_ADDRESS = 5,
342 USB_CTRL_REQ_GET_DESCRIPTOR = 6,
343 USB_CTRL_REQ_SET_DESCRIPTOR = 7,
344 USB_CTRL_REQ_GET_CONFIGURATION = 8,
345 USB_CTRL_REQ_SET_CONFIGURATION = 9,
346 USB_CTRL_REQ_GET_INTERFACE = 10,
347 USB_CTRL_REQ_SET_INTERFACE = 11,
348 USB_CTRL_REQ_SYNC_FRAME = 12
351 uint16_t type_and_req;
363 CTASSERT_SIZE_BYTE(struct usb_ctrl_req_t, 8);
366 * status replies for GET_STATUS.
369 struct usb_ctrl_req_status_dev_t {
370 uint16_t self_powered : 1;
371 uint16_t remote_wakeup : 1;
374 CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_dev_t, 16);
376 struct usb_ctrl_req_status_iface_t {
379 CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_iface_t, 16);
381 struct usb_ctrl_req_status_ep_t {
385 CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_ep_t, 16);
388 * Descriptor type (in req->value) for GET_DESCRIPTOR.
390 struct usb_ctrl_req_desc_t {
392 enum usb_desc_type type : 8;
394 CTASSERT_SIZE_BIT(struct usb_ctrl_req_desc_t, 16);
397 * Feature selector (in req->value) for CLEAR_FEATURE.
399 enum usb_ctrl_req_feature {
400 USB_CTRL_REQ_FEAT_EP_HALT = 0,
401 USB_CTRL_REQ_FEAT_DEV_REMOTE_WKUP = 1,
402 USB_CTRL_REQ_FEAT_TEST_MODE = 2
406 struct usb_xfer_info;
407 typedef void (*ep_callback_t)(void *buf, ssize_t len, void *data);
410 * (Artificial) function. Aggregates one or more interfaces.
412 struct usbd_function {
413 int (*configure)(int orig_iface, int iface, int altsetting, void *data);
414 int (*control)(struct usb_ctrl_req_t *, void *);
420 struct usbd_function_ctx_header {
421 struct usbd_function_ctx_header *next;
422 const struct usbd_function *function;
423 int interface_offset;
429 typedef void (usbd_init_fun_t)(int);
430 typedef void (usbd_suspend_resume_fun_t)(void);
433 * Configuration. Contains one or more functions which all will be
434 * active concurrently.
437 usbd_init_fun_t *init;
438 usbd_suspend_resume_fun_t *suspend;
439 usbd_suspend_resume_fun_t *resume;
441 * We will not set a config for now, because there is not much to
442 * configure, except for power
444 * const struct usb_desc_config_t *config_desc;
446 const struct usb_desc_config_t *desc;
447 const struct usbd_function *function[];
452 * Device. Contains one or more configurations, out of which only one
453 * is active at a time.
456 const struct usb_desc_dev_t *dev_desc;
457 const struct usb_desc_string_t * const *string_descs;
458 const struct usbd_config *configs[];
463 /* Provided by MD code */
464 struct usbd_ep_pipe_state_t;
468 // ----- Functions -----
470 void *usb_get_xfer_data(struct usb_xfer_info *);
471 enum usb_tok_pid usb_get_xfer_pid(struct usb_xfer_info *);
472 int usb_get_xfer_ep(struct usb_xfer_info *);
473 enum usb_ep_dir usb_get_xfer_dir(struct usb_xfer_info *);
474 void usb_enable_xfers(void);
475 void usb_set_addr(int);
476 void usb_ep_stall(int);
477 size_t usb_ep_get_transfer_size(struct usbd_ep_pipe_state_t *);
478 void usb_queue_next(struct usbd_ep_pipe_state_t *, void *, size_t);
479 void usb_pipe_stall(struct usbd_ep_pipe_state_t *);
480 void usb_pipe_unstall(struct usbd_ep_pipe_state_t *);
481 void usb_pipe_enable(struct usbd_ep_pipe_state_t *s);
482 void usb_pipe_disable(struct usbd_ep_pipe_state_t *s);
484 void vusb_main_loop(void);
488 int usb_tx_serialno(size_t reqlen);
490 /* Provided by MI code */
491 void usb_init(const struct usbd_device *);
492 void usb_attach_function(const struct usbd_function *function, struct usbd_function_ctx_header *ctx);
493 void usb_handle_transaction(struct usb_xfer_info *);
494 void usb_setup_control(void);
495 void usb_handle_control_status_cb(ep_callback_t cb);
496 void usb_handle_control_status(int);
497 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);
498 int usb_rx(struct usbd_ep_pipe_state_t *, void *, size_t, ep_callback_t, void *);
499 int usb_tx(struct usbd_ep_pipe_state_t *, const void *, size_t, size_t, ep_callback_t, void *);
501 int usb_ep0_rx(void *, size_t, ep_callback_t, void *);
502 void *usb_ep0_tx_inplace_prepare(size_t len);
503 int usb_ep0_tx(void *buf, size_t len, size_t reqlen, ep_callback_t cb, void *cb_data);
504 int usb_ep0_tx_cp(const void *, size_t, size_t, ep_callback_t, void *);
508 // ----- DFU USB Additional Includes -----