usbc.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef __DEV_USBC_H
  24. #define __DEV_USBC_H
  25. #include <sys/types.h>
  26. #include <debug.h>
  27. #include <hw/usb.h>
  28. void usbc_init(void);
  29. typedef uint ep_t;
  30. typedef enum {
  31. IN = 0,
  32. OUT
  33. } ep_dir_t;
  34. typedef enum {
  35. CB_RESET,
  36. CB_SUSPEND,
  37. CB_RESUME,
  38. CB_DISCONNECT,
  39. CB_ONLINE,
  40. CB_OFFLINE,
  41. CB_SETUP_MSG,
  42. /* endpoint transfer stuff */
  43. CB_EP_RXCOMPLETE,
  44. CB_EP_TXCOMPLETE,
  45. CB_EP_TRANSFER_CANCELLED,
  46. } usbc_callback_op_t;
  47. typedef struct {
  48. void *buf;
  49. size_t buflen;
  50. uint bufpos;
  51. int result;
  52. void *extra; // extra pointer to store whatever you want
  53. } usbc_transfer;
  54. enum {
  55. USB_TRANSFER_RESULT_OK = 0,
  56. USB_TRANSFER_RESULT_ERR = -1,
  57. USB_TRANSFER_RESULT_CANCELLED = -2,
  58. };
  59. typedef int (*ep_callback)(ep_t endpoint, usbc_callback_op_t op, usbc_transfer *transfer);
  60. void usbc_setup_endpoint(ep_t ep, ep_dir_t dir, bool active, ep_callback callback, uint width, uint blocksize);
  61. int usbc_queue_rx(ep_t ep, usbc_transfer *transfer);
  62. int usbc_queue_tx(ep_t ep, usbc_transfer *transfer);
  63. /* setup arg is valid during CB_SETUP_MSG */
  64. union usb_callback_args {
  65. const struct usb_setup *setup;
  66. };
  67. typedef int (*usb_callback)(usbc_callback_op_t op, const union usb_callback_args *args);
  68. int usbc_set_callback(usb_callback);
  69. int usbc_set_active(bool active);
  70. /* called back from within a callback to handle setup responses */
  71. void usbc_ep0_ack(void);
  72. void usbc_ep0_stall(void);
  73. void usbc_ep0_send(const void *buf, size_t len, size_t maxlen);
  74. void usbc_ep0_recv(void *buf, size_t len, ep_callback);
  75. bool usbc_is_highspeed(void);
  76. static inline void usbc_dump_transfer(const usbc_transfer *t)
  77. {
  78. printf("usb transfer %p: buf %p, buflen %zd, bufpos %u, result %d\n", t, t->buf, t->buflen, t->bufpos, t->result);
  79. }
  80. #endif