usb.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * usb.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 <stdbool.h>
  25. #include <stdint.h>
  26. #include "usb_proto.h"
  27. /* Types */
  28. /* ----- */
  29. struct usb_xfer;
  30. struct usb_stack_descriptors {
  31. const struct usb_dev_desc *dev;
  32. const struct usb_conf_desc * const *conf;
  33. int n_conf;
  34. const struct usb_str_desc * const *str;
  35. int n_str;
  36. };
  37. enum usb_dev_state {
  38. USB_DS_OFF = 0, /* Core is not initialize */
  39. USB_DS_DISCONNECTED = 1, /* Core is not connected */
  40. USB_DS_CONNECTED = 2, /* Core is connected awaiting reset */
  41. USB_DS_DEFAULT = 3,
  42. USB_DS_ADDRESS = 4,
  43. USB_DS_CONFIGURED = 5,
  44. USB_DS_SUSPENDED = 0x80, /* Bit marking suspend */
  45. USB_DS_RESUME = 0x81, /* Special value for set_state */
  46. };
  47. enum usb_fnd_resp {
  48. USB_FND_CONTINUE = 0, /* Not handled, continue to next */
  49. USB_FND_SUCCESS, /* Handled: Success */
  50. USB_FND_ERROR, /* Handled: Error */
  51. };
  52. typedef void (*usb_fnd_sof_cb)(void);
  53. typedef void (*usb_fnd_bus_reset_cb)(void);
  54. typedef void (*usb_fnd_state_chg_cb)(enum usb_dev_state state);
  55. typedef enum usb_fnd_resp (*usb_fnd_ctrl_req_cb)(struct usb_ctrl_req *req, struct usb_xfer *xfer);
  56. typedef enum usb_fnd_resp (*usb_fnd_set_conf_cb)(const struct usb_conf_desc *desc);
  57. typedef enum usb_fnd_resp (*usb_fnd_set_intf_cb)(const struct usb_intf_desc *base, const struct usb_intf_desc *sel);
  58. typedef enum usb_fnd_resp (*usb_fnd_get_intf_cb)(const struct usb_intf_desc *base, uint8_t *alt);
  59. struct usb_fn_drv {
  60. struct usb_fn_drv *next;
  61. usb_fnd_sof_cb sof;
  62. usb_fnd_bus_reset_cb bus_reset;
  63. usb_fnd_state_chg_cb state_chg;
  64. usb_fnd_ctrl_req_cb ctrl_req;
  65. usb_fnd_set_conf_cb set_conf;
  66. usb_fnd_set_intf_cb set_intf;
  67. usb_fnd_get_intf_cb get_intf;
  68. };
  69. typedef bool (*usb_xfer_cb)(struct usb_xfer *xfer);
  70. struct usb_xfer {
  71. /* Data buffer */
  72. uint8_t *data;
  73. int ofs;
  74. int len;
  75. /* Call backs */
  76. usb_xfer_cb cb_data; /* Data call back */
  77. usb_xfer_cb cb_done; /* Completion call back */
  78. void *cb_ctx;
  79. };
  80. /* API */
  81. void usb_init(const struct usb_stack_descriptors *stack_desc);
  82. void usb_poll(void);
  83. void usb_set_state(enum usb_dev_state new_state);
  84. enum usb_dev_state usb_get_state(void);
  85. uint32_t usb_get_tick(void);
  86. void usb_connect(void);
  87. void usb_disconnect(void);
  88. void usb_set_address(uint8_t addr);
  89. void usb_register_function_driver(struct usb_fn_drv *drv);
  90. void usb_unregister_function_driver(struct usb_fn_drv *drv);
  91. /* EP */
  92. bool usb_ep_is_configured(uint8_t ep);
  93. bool usb_ep_is_halted(uint8_t ep);
  94. bool usb_ep_halt(uint8_t ep);
  95. bool usb_ep_resume(uint8_t ep);
  96. /* Descriptors */
  97. const void *usb_desc_find(const void *sod, const void *eod, uint8_t dt);
  98. const void *usb_desc_next(const void *sod);
  99. const struct usb_conf_desc *usb_desc_find_conf(uint8_t cfg_value);
  100. const struct usb_intf_desc *usb_desc_find_intf(const struct usb_conf_desc *conf, uint8_t idx, uint8_t alt,
  101. const struct usb_intf_desc **base);
  102. /* Debug */
  103. void usb_debug_print_ep(int ep, int dir);
  104. void usb_debug_print_data(int ofs, int len);
  105. void usb_debug_print(void);