Keyboard firmwares for Atmel AVR and Cortex-M
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.

USBDevice.h 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef USBDEVICE_H
  19. #define USBDEVICE_H
  20. #include "mbed.h"
  21. #include "USBDevice_Types.h"
  22. #include "USBHAL.h"
  23. class USBDevice: public USBHAL
  24. {
  25. public:
  26. USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
  27. /*
  28. * Check if the device is configured
  29. *
  30. * @returns true if configured, false otherwise
  31. */
  32. bool configured(void);
  33. /*
  34. * Connect a device
  35. *
  36. * @param blocking: block if not configured
  37. */
  38. void connect(bool blocking = true);
  39. /*
  40. * Disconnect a device
  41. */
  42. void disconnect(void);
  43. /*
  44. * Add an endpoint
  45. *
  46. * @param endpoint endpoint which will be added
  47. * @param maxPacket Maximum size of a packet which can be sent for this endpoint
  48. * @returns true if successful, false otherwise
  49. */
  50. bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
  51. /*
  52. * Start a reading on a certain endpoint.
  53. * You can access the result of the reading by USBDevice_read
  54. *
  55. * @param endpoint endpoint which will be read
  56. * @param maxSize the maximum length that can be read
  57. * @return true if successful
  58. */
  59. bool readStart(uint8_t endpoint, uint32_t maxSize);
  60. /*
  61. * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
  62. * must be called.
  63. *
  64. * Warning: blocking
  65. *
  66. * @param endpoint endpoint which will be read
  67. * @param buffer buffer will be filled with the data received
  68. * @param size the number of bytes read will be stored in *size
  69. * @param maxSize the maximum length that can be read
  70. * @returns true if successful
  71. */
  72. bool readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
  73. /*
  74. * Read a certain endpoint.
  75. *
  76. * Warning: non blocking
  77. *
  78. * @param endpoint endpoint which will be read
  79. * @param buffer buffer will be filled with the data received (if data are available)
  80. * @param size the number of bytes read will be stored in *size
  81. * @param maxSize the maximum length that can be read
  82. * @returns true if successful
  83. */
  84. bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
  85. /*
  86. * Write a certain endpoint.
  87. *
  88. * Warning: blocking
  89. *
  90. * @param endpoint endpoint to write
  91. * @param buffer data contained in buffer will be write
  92. * @param size the number of bytes to write
  93. * @param maxSize the maximum length that can be written on this endpoint
  94. */
  95. bool write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
  96. /*
  97. * Write a certain endpoint.
  98. *
  99. * Warning: non blocking
  100. *
  101. * @param endpoint endpoint to write
  102. * @param buffer data contained in buffer will be write
  103. * @param size the number of bytes to write
  104. * @param maxSize the maximum length that can be written on this endpoint
  105. */
  106. bool writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
  107. /*
  108. * Called by USBDevice layer on bus reset. Warning: Called in ISR context
  109. *
  110. * May be used to reset state
  111. */
  112. virtual void USBCallback_busReset(void) {};
  113. /*
  114. * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
  115. * This is used to handle extensions to standard requests
  116. * and class specific requests
  117. *
  118. * @returns true if class handles this request
  119. */
  120. virtual bool USBCallback_request() { return false; };
  121. /*
  122. * Called by USBDevice on Endpoint0 request completion
  123. * if the 'notify' flag has been set to true. Warning: Called in ISR context
  124. *
  125. * In this case it is used to indicate that a HID report has
  126. * been received from the host on endpoint 0
  127. *
  128. * @param buf buffer received on endpoint 0
  129. * @param length length of this buffer
  130. */
  131. virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {};
  132. /*
  133. * Called by USBDevice layer. Set configuration of the device.
  134. * For instance, you can add all endpoints that you need on this function.
  135. *
  136. * @param configuration Number of the configuration
  137. */
  138. virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; };
  139. /*
  140. * Called by USBDevice layer. Set interface/alternate of the device.
  141. *
  142. * @param interface Number of the interface to be configured
  143. * @param alternate Number of the alternate to be configured
  144. * @returns true if class handles this request
  145. */
  146. virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
  147. /*
  148. * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
  149. *
  150. * @returns pointer to the device descriptor
  151. */
  152. virtual uint8_t * deviceDesc();
  153. /*
  154. * Get configuration descriptor
  155. *
  156. * @returns pointer to the configuration descriptor
  157. */
  158. virtual uint8_t * configurationDesc(){return NULL;};
  159. /*
  160. * Get string lang id descriptor
  161. *
  162. * @return pointer to the string lang id descriptor
  163. */
  164. virtual uint8_t * stringLangidDesc();
  165. /*
  166. * Get string manufacturer descriptor
  167. *
  168. * @returns pointer to the string manufacturer descriptor
  169. */
  170. virtual uint8_t * stringImanufacturerDesc();
  171. /*
  172. * Get string product descriptor
  173. *
  174. * @returns pointer to the string product descriptor
  175. */
  176. virtual uint8_t * stringIproductDesc();
  177. /*
  178. * Get string serial descriptor
  179. *
  180. * @returns pointer to the string serial descriptor
  181. */
  182. virtual uint8_t * stringIserialDesc();
  183. /*
  184. * Get string configuration descriptor
  185. *
  186. * @returns pointer to the string configuration descriptor
  187. */
  188. virtual uint8_t * stringIConfigurationDesc();
  189. /*
  190. * Get string interface descriptor
  191. *
  192. * @returns pointer to the string interface descriptor
  193. */
  194. virtual uint8_t * stringIinterfaceDesc();
  195. /*
  196. * Get the length of the report descriptor
  197. *
  198. * @returns length of the report descriptor
  199. */
  200. virtual uint16_t reportDescLength() { return 0; };
  201. protected:
  202. virtual void busReset(void);
  203. virtual void EP0setupCallback(void);
  204. virtual void EP0out(void);
  205. virtual void EP0in(void);
  206. virtual void connectStateChanged(unsigned int connected);
  207. virtual void suspendStateChanged(unsigned int suspended);
  208. uint8_t * findDescriptor(uint8_t descriptorType);
  209. CONTROL_TRANSFER * getTransferPtr(void);
  210. uint16_t VENDOR_ID;
  211. uint16_t PRODUCT_ID;
  212. uint16_t PRODUCT_RELEASE;
  213. private:
  214. bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
  215. bool requestGetDescriptor(void);
  216. bool controlOut(void);
  217. bool controlIn(void);
  218. bool requestSetAddress(void);
  219. bool requestSetConfiguration(void);
  220. bool requestSetFeature(void);
  221. bool requestClearFeature(void);
  222. bool requestGetStatus(void);
  223. bool requestSetup(void);
  224. bool controlSetup(void);
  225. void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);
  226. bool requestGetConfiguration(void);
  227. bool requestGetInterface(void);
  228. bool requestSetInterface(void);
  229. CONTROL_TRANSFER transfer;
  230. USB_DEVICE device;
  231. uint16_t currentInterface;
  232. uint8_t currentAlternate;
  233. };
  234. #endif