usb_priv.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * usb_priv.h
  3. *
  4. * Copyright (C) 2019 Sylvain Munaut
  5. * All rights reserved.
  6. *
  7. * LGPL v3+, see LICENSE.lgpl3
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #pragma once
  24. #include <stdint.h>
  25. #include "config.h"
  26. /* Hardware registers */
  27. /* ------------------ */
  28. struct usb_core {
  29. uint32_t csr;
  30. uint32_t ar;
  31. uint32_t evt;
  32. } __attribute__((packed,aligned(4)));
  33. #define USB_CSR_PU_ENA (1 << 15)
  34. #define USB_CSR_CEL_ENA (1 << 12)
  35. #define USB_CSR_CEL_ACTIVE (1 << 13)
  36. #define USB_AR_CEL_RELEASE (1 << 13)
  37. struct usb_ep {
  38. uint32_t status;
  39. uint32_t _rsvd[3];
  40. struct {
  41. uint32_t csr;
  42. uint32_t ptr;
  43. } bd[2];
  44. } __attribute__((packed,aligned(4)));
  45. struct usb_ep_pair {
  46. struct usb_ep out;
  47. struct usb_ep in;
  48. } __attribute__((packed,aligned(4)));
  49. #define USB_EP_TYPE_NONE 0x0000
  50. #define USB_EP_TYPE_ISOC 0x0001
  51. #define USB_EP_TYPE_INT 0x0002
  52. #define USB_EP_TYPE_BULK 0x0004
  53. #define USB_EP_TYPE_CTRL 0x0006
  54. #define USB_EP_TYPE_HALTED 0x0001
  55. #define USB_EP_DT_BIT 0x0080
  56. #define USB_EP_BD_IDX 0x0040
  57. #define USB_EP_BD_CTRL 0x0020
  58. #define USB_EP_BD_DUAL 0x0010
  59. #define USB_BD_STATE_MSK 0xe000
  60. #define USB_BD_STATE_NONE 0x0000
  61. #define USB_BD_STATE_RDY_DATA 0x4000
  62. #define USB_BD_STATE_RDY_STALL 0x6000
  63. #define USB_BD_STATE_DONE_OK 0x8000
  64. #define USB_BD_STATE_DONE_ERR 0xa000
  65. #define USB_BD_IS_SETUP 0x1000
  66. #define USB_BD_LEN(l) ((l) & 0x3ff)
  67. #define USB_BD_LEN_MSK 0x03ff
  68. static volatile struct usb_core * const usb_regs = (void*) (USB_CORE_BASE);
  69. static volatile struct usb_ep_pair * const usb_ep_regs = (void*)((USB_CORE_BASE) + (1 << 13));
  70. /* USB protocol */
  71. /* ------------ */
  72. struct usb_ctrl_req_hdr {
  73. uint8_t bmRequestType;
  74. uint8_t bRequest;
  75. uint16_t wValue;
  76. uint16_t wIndex;
  77. uint16_t wLength;
  78. } __attribute__((packed));
  79. #define USB_REQ_IS_READ(req) ( req->bmRequestType & 0x80 )
  80. #define USB_REQ_IS_WRITE(req) (!(req->bmRequestType & 0x80))
  81. #define USB_REQ_GET_STATUS 0
  82. #define USB_REQ_CLEAR_FEATURE 1
  83. #define USB_REQ_SET_FEATURE 3
  84. #define USB_REQ_SET_ADDRESS 5
  85. #define USB_REQ_GET_DESCRIPTOR 6
  86. #define USB_REQ_SET_DESCRIPTOR 7
  87. #define USB_REQ_GET_CONFIGURATION 8
  88. #define USB_REQ_SET_CONFIGURATION 9
  89. #define USB_REQ_GET_INTERFACE 10
  90. #define USB_REQ_SET_INTERFACE 11
  91. #define USB_REQ_SYNCHFRAME 12
  92. /* Internal functions */
  93. /* ------------------ */
  94. /* Internal state */
  95. struct usb_stack {
  96. struct {
  97. enum {
  98. IDLE,
  99. DATA_IN, /* Data stage via 'IN' */
  100. DATA_OUT, /* Data stage via 'OUT' */
  101. STATUS_DONE_OUT, /* Status sent via 'OUT' EP */
  102. STATUS_DONE_IN, /* Status sent via 'IN' EP */
  103. } state;
  104. struct usb_ctrl_req_hdr req;
  105. union {
  106. const uint8_t *out;
  107. uint8_t *in;
  108. } data;
  109. int len;
  110. int ofs;
  111. } ctrl;
  112. };
  113. extern struct usb_stack g_usb;
  114. /* Helpers for data access */
  115. void usb_data_write(int dst_ofs, const void *src, int len);
  116. void usb_data_read(void *dst, int src_ofs, int len);
  117. /* Descriptors retrieval */
  118. const void *usb_get_device_desc(int *len);
  119. const void *usb_get_config_desc(int *len, int idx);
  120. const void *usb_get_string_desc(int *len, int idx);
  121. /* EndPoint 0 */
  122. void usb_ep0_run(void);
  123. void usb_ep0_init(void);