You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb_private.h 5.0 KiB

1 jaar geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /** @defgroup usb_private_defines USB Private Structures
  2. @brief <b>Defined Constants and Types for the USB Private Structures</b>
  3. @ingroup USB_defines
  4. @version 1.0.0
  5. @author @htmlonly &copy; @endhtmlonly 2010
  6. Gareth McMullin <gareth@blacksphere.co.nz>
  7. @date 10 March 2013
  8. LGPL License Terms @ref lgpl_license
  9. */
  10. /*
  11. * This file is part of the libopencm3 project.
  12. *
  13. * Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
  14. *
  15. * This library is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Lesser General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This library is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public License
  26. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  27. */
  28. /**@{*/
  29. #ifndef __USB_PRIVATE_H
  30. #define __USB_PRIVATE_H
  31. #define MAX_USER_CONTROL_CALLBACK 4
  32. #define MAX_USER_SET_CONFIG_CALLBACK 4
  33. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  34. /** Internal collection of device information. */
  35. struct _usbd_device {
  36. const struct usb_device_descriptor *desc;
  37. const struct usb_config_descriptor *config;
  38. const char * const *strings;
  39. int num_strings;
  40. uint8_t *ctrl_buf; /**< Internal buffer used for control transfers */
  41. uint16_t ctrl_buf_len;
  42. uint8_t current_address;
  43. uint8_t current_config;
  44. uint16_t pm_top; /**< Top of allocated endpoint buffer memory */
  45. /* User callback functions for various USB events */
  46. void (*user_callback_reset)(void);
  47. void (*user_callback_suspend)(void);
  48. void (*user_callback_resume)(void);
  49. void (*user_callback_sof)(void);
  50. struct usb_control_state {
  51. enum {
  52. IDLE, STALLED,
  53. DATA_IN, LAST_DATA_IN, STATUS_IN,
  54. DATA_OUT, LAST_DATA_OUT, STATUS_OUT,
  55. } state;
  56. struct usb_setup_data req __attribute__((aligned(4)));
  57. uint8_t *ctrl_buf;
  58. uint16_t ctrl_len;
  59. usbd_control_complete_callback complete;
  60. bool needs_zlp;
  61. } control_state;
  62. struct user_control_callback {
  63. usbd_control_callback cb;
  64. uint8_t type;
  65. uint8_t type_mask;
  66. } user_control_callback[MAX_USER_CONTROL_CALLBACK];
  67. usbd_endpoint_callback user_callback_ctr[8][3];
  68. /* User callback function for some standard USB function hooks */
  69. usbd_set_config_callback user_callback_set_config[MAX_USER_SET_CONFIG_CALLBACK];
  70. usbd_set_altsetting_callback user_callback_set_altsetting;
  71. const struct _usbd_driver *driver;
  72. /* private driver data */
  73. uint16_t fifo_mem_top;
  74. uint16_t fifo_mem_top_ep0;
  75. uint8_t force_nak[4];
  76. /*
  77. * We keep a backup copy of the out endpoint size registers to restore
  78. * them after a transaction.
  79. */
  80. uint32_t doeptsiz[4];
  81. /*
  82. * Received packet size for each endpoint. This is assigned in
  83. * stm32f107_poll() which reads the packet status push register GRXSTSP
  84. * for use in stm32f107_ep_read_packet().
  85. */
  86. uint16_t rxbcnt;
  87. };
  88. enum _usbd_transaction {
  89. USB_TRANSACTION_IN,
  90. USB_TRANSACTION_OUT,
  91. USB_TRANSACTION_SETUP,
  92. };
  93. /* Do not appear to belong to the API, so are omitted from docs */
  94. /**@}*/
  95. void _usbd_control_in(usbd_device *usbd_dev, uint8_t ea);
  96. void _usbd_control_out(usbd_device *usbd_dev, uint8_t ea);
  97. void _usbd_control_setup(usbd_device *usbd_dev, uint8_t ea);
  98. enum usbd_request_return_codes _usbd_standard_request_device(usbd_device *usbd_dev,
  99. struct usb_setup_data *req, uint8_t **buf,
  100. uint16_t *len);
  101. enum usbd_request_return_codes _usbd_standard_request_interface(usbd_device *usbd_dev,
  102. struct usb_setup_data *req, uint8_t **buf,
  103. uint16_t *len);
  104. enum usbd_request_return_codes _usbd_standard_request_endpoint(usbd_device *usbd_dev,
  105. struct usb_setup_data *req, uint8_t **buf,
  106. uint16_t *len);
  107. enum usbd_request_return_codes _usbd_standard_request(usbd_device *usbd_dev, struct usb_setup_data *req,
  108. uint8_t **buf, uint16_t *len);
  109. void _usbd_reset(usbd_device *usbd_dev);
  110. /* Functions provided by the hardware abstraction. */
  111. struct _usbd_driver {
  112. usbd_device *(*init)(void);
  113. void (*set_address)(usbd_device *usbd_dev, uint8_t addr);
  114. void (*ep_setup)(usbd_device *usbd_dev, uint8_t addr, uint8_t type,
  115. uint16_t max_size, usbd_endpoint_callback cb);
  116. void (*ep_reset)(usbd_device *usbd_dev);
  117. void (*ep_stall_set)(usbd_device *usbd_dev, uint8_t addr,
  118. uint8_t stall);
  119. void (*ep_nak_set)(usbd_device *usbd_dev, uint8_t addr, uint8_t nak);
  120. uint8_t (*ep_stall_get)(usbd_device *usbd_dev, uint8_t addr);
  121. uint16_t (*ep_write_packet)(usbd_device *usbd_dev, uint8_t addr,
  122. const void *buf, uint16_t len);
  123. uint16_t (*ep_read_packet)(usbd_device *usbd_dev, uint8_t addr,
  124. void *buf, uint16_t len);
  125. void (*poll)(usbd_device *usbd_dev);
  126. void (*disconnect)(usbd_device *usbd_dev, bool disconnected);
  127. uint32_t base_address;
  128. bool set_address_before_status;
  129. uint16_t rx_fifo_size;
  130. };
  131. #endif