usb_priv.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "usb.h"
  26. #include "usb_proto.h"
  27. /* Debug logging */
  28. /* ------------- */
  29. #define USB_LOG_LEVEL 1
  30. #ifdef USB_LOG_LEVEL
  31. # define USB_LOG(lvl,...) if ((lvl) >= USB_LOG_LEVEL) printf(__VA_ARGS__)
  32. #else
  33. # define USB_LOG(lvl,...) do {} while(0)
  34. #endif
  35. #define USB_LOG_ERR(...) USB_LOG(1, __VA_ARGS__)
  36. /* Internal functions */
  37. /* ------------------ */
  38. /* Stack */
  39. struct usb_stack {
  40. /* Driver config */
  41. const struct usb_stack_descriptors *stack_desc;
  42. /* Device state */
  43. enum usb_dev_state state;
  44. const struct usb_conf_desc *conf;
  45. uint32_t intf_alt;
  46. /* Timebase */
  47. uint32_t tick;
  48. /* EP0 control state */
  49. struct {
  50. enum {
  51. IDLE,
  52. DATA_IN, /* Data stage via 'IN' */
  53. DATA_OUT, /* Data stage via 'OUT' */
  54. STATUS_DONE_OUT, /* Status sent via 'OUT' EP */
  55. STATUS_DONE_IN, /* Status sent via 'IN' EP */
  56. STALL, /* Stalled until next `SETUP` */
  57. } state;
  58. uint8_t buf[64];
  59. struct usb_xfer xfer;
  60. struct usb_ctrl_req req;
  61. } ctrl;
  62. /* Function drivers */
  63. struct usb_fn_drv *fnd;
  64. };
  65. extern struct usb_stack g_usb;
  66. /* Helpers for data access */
  67. void usb_data_write(unsigned int dst_ofs, const void *src, int len);
  68. void usb_data_read(void *dst, unsigned int src_ofs, int len);
  69. void usb_dispatch_sof(void);
  70. void usb_dipatch_bus_reset(void);
  71. void usb_dispatch_state_chg(enum usb_dev_state state);
  72. enum usb_fnd_resp usb_dispatch_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer);
  73. enum usb_fnd_resp usb_dispatch_set_conf(const struct usb_conf_desc *desc);
  74. enum usb_fnd_resp usb_dispatch_set_intf(const struct usb_intf_desc *base, const struct usb_intf_desc *sel);
  75. enum usb_fnd_resp usb_dispatch_get_intf(const struct usb_intf_desc *base, uint8_t *sel);
  76. /* Control */
  77. void usb_ep0_reset(void);
  78. void usb_ep0_poll(void);
  79. extern struct usb_fn_drv usb_ctrl_std_drv;