lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1 | // Copyright lowRISC contributors. |
| 2 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | |
Miguel Young de la Sota | 960fd8e | 2020-01-14 13:52:13 -0500 | [diff] [blame] | 5 | #ifndef OPENTITAN_SW_DEVICE_LIB_USB_CONTROLEP_H_ |
| 6 | #define OPENTITAN_SW_DEVICE_LIB_USB_CONTROLEP_H_ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 7 | |
Miguel Young de la Sota | f225d99 | 2019-12-17 10:19:22 -0600 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | #include "sw/device/lib/usbdev.h" |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 12 | |
| 13 | typedef enum ctstate { |
| 14 | kCtIdle, |
| 15 | kCtWaitIn, // Queued IN data stage, waiting ack |
| 16 | kCtStatOut, // Waiting for OUT status stage |
| 17 | kCtAddrStatIn, // Queued status stage, waiting ack afterwhich set dev_addr |
| 18 | kCtStatIn, // Queued status stage, waiting ack |
| 19 | kCtError // Something bad |
| 20 | } ctstate_t; |
| 21 | |
| 22 | typedef struct usb_controlep_ctx { |
| 23 | usbdev_ctx_t *ctx; |
| 24 | int ep; |
| 25 | ctstate_t ctrlstate; |
| 26 | uint32_t new_dev; |
| 27 | uint8_t usb_config; |
| 28 | const uint8_t *cfg_dscr; |
| 29 | size_t cfg_dscr_len; |
| 30 | } usb_controlep_ctx_t; |
| 31 | |
| 32 | /** |
| 33 | * Initialize control endpoint |
| 34 | * |
| 35 | * @param ctctx uninitialized context for this instance |
| 36 | * @param ctx initialized context for usbdev driver |
| 37 | * @param ep endpoint (if this is other than 0 make sure you know why) |
| 38 | * @param cfg_dscr configuration descriptor for the device |
| 39 | * @param cfg_dscr_len length of cfg_dscr |
| 40 | */ |
| 41 | void usb_controlep_init(usb_controlep_ctx_t *ctctx, usbdev_ctx_t *ctx, int ep, |
| 42 | const uint8_t *cfg_dscr, size_t cfg_dscr_len); |
| 43 | |
| 44 | /********************************************************************/ |
| 45 | /* Below this point are macros used to construct the USB descriptor */ |
| 46 | /* Use them to initialize a uint8_t array for cfg_dscr */ |
| 47 | |
| 48 | #define USB_CFG_DSCR_LEN 9 |
Miguel Young de la Sota | e9a769b | 2020-02-25 11:19:04 -0500 | [diff] [blame] | 49 | #define USB_CFG_DSCR_HEAD(total_len, nint) \ |
| 50 | /* This is the actual configuration descriptor */ \ |
| 51 | USB_CFG_DSCR_LEN, /* bLength */ \ |
| 52 | 2, /* bDescriptorType */ \ |
| 53 | (total_len)&0xff, /* wTotalLength[0] */ \ |
| 54 | (total_len) >> 8, /* wTotalLength[1] */ \ |
| 55 | (nint), /* bNumInterfaces */ \ |
| 56 | 1, /* bConfigurationValu */ \ |
| 57 | 0, /* iConfiguration */ \ |
| 58 | 0xC0, /* bmAttributes: must-be-one, self-powered */ \ |
| 59 | 50 /* bMaxPower */ /* MUST be followed \ |
| 60 | by (nint) \ |
| 61 | Interface + \ |
| 62 | Endpoint \ |
| 63 | Descriptors */ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 64 | |
| 65 | // KEEP BLANK LINE ABOVE, it is in the macro! |
| 66 | |
| 67 | #define USB_INTERFACE_DSCR_LEN 9 |
Miguel Young de la Sota | e9a769b | 2020-02-25 11:19:04 -0500 | [diff] [blame] | 68 | #define VEND_INTERFACE_DSCR(inum, nep, subclass, protocol) \ |
| 69 | /* interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 */ \ |
| 70 | USB_INTERFACE_DSCR_LEN, /* bLength */ \ |
| 71 | 4, /* bDescriptorType */ \ |
| 72 | (inum), /* bInterfaceNumber */ \ |
| 73 | 0, /* bAlternateSetting */ \ |
| 74 | (nep), /* bNumEndpoints */ \ |
| 75 | 0xff, /* bInterfaceClass (Vendor Specific) */ \ |
| 76 | (subclass), /* bInterfaceSubClass */ \ |
| 77 | (protocol), /* bInterfaceProtocol */ \ |
| 78 | 0 /* iInterface */ /* MUST be followed by \ |
| 79 | (nep) Endpoint \ |
| 80 | Descriptors */ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 81 | |
| 82 | // KEEP BLANK LINE ABOVE, it is in the macro! |
| 83 | |
| 84 | #define USB_EP_DSCR_LEN 7 |
| 85 | #define USB_BULK_EP_DSCR(in, ep, maxsize, interval) \ |
| 86 | /* endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 */ \ |
| 87 | USB_EP_DSCR_LEN, /* bLength */ \ |
| 88 | 5, /* bDescriptorType */ \ |
| 89 | (ep) | (((in) << 7) & 0x80), /* bEndpointAddress, top bit set for IN */ \ |
| 90 | 0x02, /* bmAttributes (0x02=bulk, data) */ \ |
| 91 | (maxsize)&0xff, /* wMaxPacketSize[0] */ \ |
| 92 | (maxsize) >> 8, /* wMaxPacketSize[1] */ \ |
Miguel Young de la Sota | e9a769b | 2020-02-25 11:19:04 -0500 | [diff] [blame] | 93 | (interval) /* bInterval */ |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 94 | |
| 95 | // KEEP BLANK LINE ABOVE, it is in the macro! |
| 96 | |
Miguel Young de la Sota | 960fd8e | 2020-01-14 13:52:13 -0500 | [diff] [blame] | 97 | #endif // OPENTITAN_SW_DEVICE_LIB_USB_CONTROLEP_H_ |