usb.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * fw/usb//usb.h - USB hardware setup and standard device requests
  3. *
  4. * Written 2008, 2009, 2011, 2013, 2015 by Werner Almesberger
  5. * Copyright 2008, 2009, 2011, 2013, 2015 Werner Almesberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #ifndef USB_H
  13. #define USB_H
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. /*
  17. * Packet identifier types
  18. */
  19. #define PID_OUT 0x1
  20. #define PID_IN 0x9
  21. #define PID_SOF 0x5
  22. #define PID_SETUP 0xd
  23. #define PID_DATA0 0x3
  24. #define PID_DATA1 0xb
  25. #define PID_ACK 0x2
  26. #define PID_NAK 0xa
  27. #define PID_STALL 0xe
  28. /*
  29. * Descriptor types
  30. *
  31. * Reuse libusb naming scheme (/usr/include/usb.h)
  32. */
  33. #define USB_DT_DEVICE 1
  34. #define USB_DT_CONFIG 2
  35. #define USB_DT_STRING 3
  36. #define USB_DT_INTERFACE 4
  37. #define USB_DT_ENDPOINT 5
  38. /*
  39. * Device classes
  40. *
  41. * Reuse libusb naming scheme (/usr/include/usb.h)
  42. */
  43. #define USB_CLASS_PER_INTERFACE 0
  44. #define USB_CLASS_COMM 2
  45. #define USB_CLASS_HID 3
  46. #define USB_CLASS_MASS_STORAGE 8
  47. #define USB_CLASS_HUB 9
  48. #define USB_CLASS_DATA 10
  49. #define USB_CLASS_APP_SPEC 0xfe
  50. #define USB_CLASS_VENDOR_SPEC 0xff
  51. /*
  52. * Configuration attributes
  53. */
  54. #define USB_ATTR_BUS_POWERED 0x80
  55. #define USB_ATTR_SELF_POWERED 0x40
  56. #define USB_ATTR_REMOTE_WAKEUP 0x20
  57. /*
  58. * Endpoint type
  59. */
  60. #define USB_ENDPOINT_TYPE_CONTROL 0
  61. #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1
  62. #define USB_ENDPOINT_TYPE_BULK 2
  63. #define USB_ENDPOINT_TYPE_INTERRUPT 3
  64. /*
  65. * Setup request types
  66. */
  67. #define TO_DEVICE(req) (0x00 | (req) << 8)
  68. #define FROM_DEVICE(req) (0x80 | (req) << 8)
  69. #define TO_INTERFACE(req) (0x01 | (req) << 8)
  70. #define FROM_INTERFACE(req) (0x81 | (req) << 8)
  71. #define TO_ENDPOINT(req) (0x02 | (req) << 8)
  72. #define FROM_ENDPOINT(req) (0x82 | (req) << 8)
  73. /*
  74. * Setup requests
  75. */
  76. #define GET_STATUS 0x00
  77. #define CLEAR_FEATURE 0x01
  78. #define SET_FEATURE 0x03
  79. #define SET_ADDRESS 0x05
  80. #define GET_DESCRIPTOR 0x06
  81. #define SET_DESCRIPTOR 0x07
  82. #define GET_CONFIGURATION 0x08
  83. #define SET_CONFIGURATION 0x09
  84. #define GET_INTERFACE 0x0a
  85. #define SET_INTERFACE 0x0b
  86. #define SYNCH_FRAME 0x0c
  87. /*
  88. * USB Language ID codes
  89. *
  90. * http://www.usb.org/developers/docs/USB_LANGIDs.pdf
  91. */
  92. #define USB_LANGID_ENGLISH_US 0x409
  93. /*
  94. * Odd. sdcc seems to think "x" assumes the size of the destination, i.e.,
  95. * uint8_t. Hence the cast.
  96. */
  97. #define LE(x) ((uint16_t) (x) & 0xff), ((uint16_t) (x) >> 8)
  98. #define LO(x) (((uint8_t *) &(x))[0])
  99. #define HI(x) (((uint8_t *) &(x))[1])
  100. #ifdef LOW_SPEED
  101. #define EP0_SIZE 8
  102. #else
  103. #define EP0_SIZE 64
  104. #endif
  105. #define EP1_SIZE 64 /* simplify */
  106. enum ep_state {
  107. EP_IDLE,
  108. EP_RX,
  109. EP_TX,
  110. EP_STALL,
  111. };
  112. struct ep_descr {
  113. enum ep_state state;
  114. uint8_t *buf;
  115. uint8_t *end;
  116. uint8_t size;
  117. void (*callback)(void *user);
  118. void *user;
  119. };
  120. struct setup_request {
  121. uint8_t bmRequestType;
  122. uint8_t bRequest;
  123. uint16_t wValue;
  124. uint16_t wIndex;
  125. uint16_t wLength;
  126. };
  127. extern const uint8_t device_descriptor[];
  128. extern const uint8_t config_descriptor[];
  129. extern struct ep_descr eps[];
  130. extern bool (*user_setup)(const struct setup_request *setup);
  131. extern void (*user_set_interface)(int nth);
  132. extern bool (*user_get_descriptor)(uint8_t type, uint8_t index,
  133. const uint8_t **reply, uint8_t *size);
  134. extern void (*user_reset)(void);
  135. #define usb_left(ep) ((ep)->end-(ep)->buf)
  136. #define usb_send(ep, buf, size, callback, user) \
  137. usb_io(ep, EP_TX, (void *) buf, size, callback, user)
  138. #define usb_recv(ep, buf, size, callback, user) \
  139. usb_io(ep, EP_RX, buf, size, callback, user)
  140. void usb_io(struct ep_descr *ep, enum ep_state state, uint8_t *buf,
  141. uint8_t size, void (*callback)(void *user), void *user);
  142. bool handle_setup(const struct setup_request *setup);
  143. void set_addr(uint8_t addr);
  144. void usb_ep_change(struct ep_descr *ep);
  145. void usb_reset(void);
  146. void usb_init(void);
  147. void ep_init(void);
  148. #endif /* !USB_H */