usb_driver.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_serial_usb.c
  15. * @brief Serial over USB Driver code.
  16. *
  17. * @addtogroup SERIAL_USB
  18. * @{
  19. */
  20. #include <hal.h>
  21. #include "usb_driver.h"
  22. #include <string.h>
  23. /*===========================================================================*/
  24. /* Driver local definitions. */
  25. /*===========================================================================*/
  26. /*===========================================================================*/
  27. /* Driver exported variables. */
  28. /*===========================================================================*/
  29. /*===========================================================================*/
  30. /* Driver local variables and types. */
  31. /*===========================================================================*/
  32. /*
  33. * Current Line Coding.
  34. */
  35. static cdc_linecoding_t linecoding = {{0x00, 0x96, 0x00, 0x00}, /* 38400. */
  36. LC_STOP_1,
  37. LC_PARITY_NONE,
  38. 8};
  39. /*===========================================================================*/
  40. /* Driver local functions. */
  41. /*===========================================================================*/
  42. static bool qmkusb_start_receive(QMKUSBDriver *qmkusbp) {
  43. uint8_t *buf;
  44. /* If the USB driver is not in the appropriate state then transactions
  45. must not be started.*/
  46. if ((usbGetDriverStateI(qmkusbp->config->usbp) != USB_ACTIVE) || (qmkusbp->state != QMKUSB_READY)) {
  47. return true;
  48. }
  49. /* Checking if there is already a transaction ongoing on the endpoint.*/
  50. if (usbGetReceiveStatusI(qmkusbp->config->usbp, qmkusbp->config->bulk_out)) {
  51. return true;
  52. }
  53. /* Checking if there is a buffer ready for incoming data.*/
  54. buf = ibqGetEmptyBufferI(&qmkusbp->ibqueue);
  55. if (buf == NULL) {
  56. return true;
  57. }
  58. /* Buffer found, starting a new transaction.*/
  59. usbStartReceiveI(qmkusbp->config->usbp, qmkusbp->config->bulk_out, buf, qmkusbp->ibqueue.bsize - sizeof(size_t));
  60. return false;
  61. }
  62. /*
  63. * Interface implementation.
  64. */
  65. static size_t _write(void *ip, const uint8_t *bp, size_t n) {
  66. return obqWriteTimeout(&((QMKUSBDriver *)ip)->obqueue, bp, n, TIME_INFINITE);
  67. }
  68. static size_t _read(void *ip, uint8_t *bp, size_t n) {
  69. return ibqReadTimeout(&((QMKUSBDriver *)ip)->ibqueue, bp, n, TIME_INFINITE);
  70. }
  71. static msg_t _put(void *ip, uint8_t b) {
  72. return obqPutTimeout(&((QMKUSBDriver *)ip)->obqueue, b, TIME_INFINITE);
  73. }
  74. static msg_t _get(void *ip) {
  75. return ibqGetTimeout(&((QMKUSBDriver *)ip)->ibqueue, TIME_INFINITE);
  76. }
  77. static msg_t _putt(void *ip, uint8_t b, sysinterval_t timeout) {
  78. return obqPutTimeout(&((QMKUSBDriver *)ip)->obqueue, b, timeout);
  79. }
  80. static msg_t _gett(void *ip, sysinterval_t timeout) {
  81. return ibqGetTimeout(&((QMKUSBDriver *)ip)->ibqueue, timeout);
  82. }
  83. static size_t _writet(void *ip, const uint8_t *bp, size_t n, sysinterval_t timeout) {
  84. return obqWriteTimeout(&((QMKUSBDriver *)ip)->obqueue, bp, n, timeout);
  85. }
  86. static size_t _readt(void *ip, uint8_t *bp, size_t n, sysinterval_t timeout) {
  87. return ibqReadTimeout(&((QMKUSBDriver *)ip)->ibqueue, bp, n, timeout);
  88. }
  89. static const struct QMKUSBDriverVMT vmt = {0, _write, _read, _put, _get, _putt, _gett, _writet, _readt};
  90. /**
  91. * @brief Notification of empty buffer released into the input buffers queue.
  92. *
  93. * @param[in] bqp the buffers queue pointer.
  94. */
  95. static void ibnotify(io_buffers_queue_t *bqp) {
  96. QMKUSBDriver *qmkusbp = bqGetLinkX(bqp);
  97. (void)qmkusb_start_receive(qmkusbp);
  98. }
  99. /**
  100. * @brief Notification of filled buffer inserted into the output buffers queue.
  101. *
  102. * @param[in] bqp the buffers queue pointer.
  103. */
  104. static void obnotify(io_buffers_queue_t *bqp) {
  105. size_t n;
  106. QMKUSBDriver *qmkusbp = bqGetLinkX(bqp);
  107. /* If the USB driver is not in the appropriate state then transactions
  108. must not be started.*/
  109. if ((usbGetDriverStateI(qmkusbp->config->usbp) != USB_ACTIVE) || (qmkusbp->state != QMKUSB_READY)) {
  110. return;
  111. }
  112. /* Checking if there is already a transaction ongoing on the endpoint.*/
  113. if (!usbGetTransmitStatusI(qmkusbp->config->usbp, qmkusbp->config->bulk_in)) {
  114. /* Trying to get a full buffer.*/
  115. uint8_t *buf = obqGetFullBufferI(&qmkusbp->obqueue, &n);
  116. if (buf != NULL) {
  117. /* Buffer found, starting a new transaction.*/
  118. usbStartTransmitI(qmkusbp->config->usbp, qmkusbp->config->bulk_in, buf, n);
  119. }
  120. }
  121. }
  122. /*===========================================================================*/
  123. /* Driver exported functions. */
  124. /*===========================================================================*/
  125. /**
  126. * @brief Serial Driver initialization.
  127. * @note This function is implicitly invoked by @p halInit(), there is
  128. * no need to explicitly initialize the driver.
  129. *
  130. * @init
  131. */
  132. void qmkusbInit(void) {}
  133. /**
  134. * @brief Initializes a generic full duplex driver object.
  135. * @details The HW dependent part of the initialization has to be performed
  136. * outside, usually in the hardware initialization code.
  137. *
  138. * @param[out] qmkusbp pointer to a @p QMKUSBDriver structure
  139. *
  140. * @init
  141. */
  142. void qmkusbObjectInit(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config) {
  143. qmkusbp->vmt = &vmt;
  144. osalEventObjectInit(&qmkusbp->event);
  145. qmkusbp->state = QMKUSB_STOP;
  146. // Note that the config uses the USB direction naming
  147. ibqObjectInit(&qmkusbp->ibqueue, true, config->ob, config->out_size, config->out_buffers, ibnotify, qmkusbp);
  148. obqObjectInit(&qmkusbp->obqueue, true, config->ib, config->in_size, config->in_buffers, obnotify, qmkusbp);
  149. }
  150. /**
  151. * @brief Configures and starts the driver.
  152. *
  153. * @param[in] qmkusbp pointer to a @p QMKUSBDriver object
  154. * @param[in] config the serial over USB driver configuration
  155. *
  156. * @api
  157. */
  158. void qmkusbStart(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config) {
  159. USBDriver *usbp = config->usbp;
  160. osalDbgCheck(qmkusbp != NULL);
  161. osalSysLock();
  162. osalDbgAssert((qmkusbp->state == QMKUSB_STOP) || (qmkusbp->state == QMKUSB_READY), "invalid state");
  163. usbp->in_params[config->bulk_in - 1U] = qmkusbp;
  164. usbp->out_params[config->bulk_out - 1U] = qmkusbp;
  165. if (config->int_in > 0U) {
  166. usbp->in_params[config->int_in - 1U] = qmkusbp;
  167. }
  168. qmkusbp->config = config;
  169. qmkusbp->state = QMKUSB_READY;
  170. osalSysUnlock();
  171. }
  172. /**
  173. * @brief Stops the driver.
  174. * @details Any thread waiting on the driver's queues will be awakened with
  175. * the message @p MSG_RESET.
  176. *
  177. * @param[in] qmkusbp pointer to a @p QMKUSBDriver object
  178. *
  179. * @api
  180. */
  181. void qmkusbStop(QMKUSBDriver *qmkusbp) {
  182. USBDriver *usbp = qmkusbp->config->usbp;
  183. osalDbgCheck(qmkusbp != NULL);
  184. osalSysLock();
  185. osalDbgAssert((qmkusbp->state == QMKUSB_STOP) || (qmkusbp->state == QMKUSB_READY), "invalid state");
  186. /* Driver in stopped state.*/
  187. usbp->in_params[qmkusbp->config->bulk_in - 1U] = NULL;
  188. usbp->out_params[qmkusbp->config->bulk_out - 1U] = NULL;
  189. if (qmkusbp->config->int_in > 0U) {
  190. usbp->in_params[qmkusbp->config->int_in - 1U] = NULL;
  191. }
  192. qmkusbp->config = NULL;
  193. qmkusbp->state = QMKUSB_STOP;
  194. /* Enforces a disconnection.*/
  195. chnAddFlagsI(qmkusbp, CHN_DISCONNECTED);
  196. ibqResetI(&qmkusbp->ibqueue);
  197. obqResetI(&qmkusbp->obqueue);
  198. osalOsRescheduleS();
  199. osalSysUnlock();
  200. }
  201. /**
  202. * @brief USB device suspend handler.
  203. * @details Generates a @p CHN_DISCONNECT event and puts queues in
  204. * non-blocking mode, this way the application cannot get stuck
  205. * in the middle of an I/O operations.
  206. * @note If this function is not called from an ISR then an explicit call
  207. * to @p osalOsRescheduleS() in necessary afterward.
  208. *
  209. * @param[in] qmkusbp pointer to a @p QMKUSBDriver object
  210. *
  211. * @iclass
  212. */
  213. void qmkusbSuspendHookI(QMKUSBDriver *qmkusbp) {
  214. chnAddFlagsI(qmkusbp, CHN_DISCONNECTED);
  215. bqSuspendI(&qmkusbp->ibqueue);
  216. bqSuspendI(&qmkusbp->obqueue);
  217. }
  218. /**
  219. * @brief USB device wakeup handler.
  220. * @details Generates a @p CHN_CONNECT event and resumes normal queues
  221. * operations.
  222. *
  223. * @note If this function is not called from an ISR then an explicit call
  224. * to @p osalOsRescheduleS() in necessary afterward.
  225. *
  226. * @param[in] qmkusbp pointer to a @p QMKUSBDriver object
  227. *
  228. * @iclass
  229. */
  230. void qmkusbWakeupHookI(QMKUSBDriver *qmkusbp) {
  231. chnAddFlagsI(qmkusbp, CHN_CONNECTED);
  232. bqResumeX(&qmkusbp->ibqueue);
  233. bqResumeX(&qmkusbp->obqueue);
  234. }
  235. /**
  236. * @brief USB device configured handler.
  237. *
  238. * @param[in] qmkusbp pointer to a @p QMKUSBDriver object
  239. *
  240. * @iclass
  241. */
  242. void qmkusbConfigureHookI(QMKUSBDriver *qmkusbp) {
  243. ibqResetI(&qmkusbp->ibqueue);
  244. bqResumeX(&qmkusbp->ibqueue);
  245. obqResetI(&qmkusbp->obqueue);
  246. bqResumeX(&qmkusbp->obqueue);
  247. chnAddFlagsI(qmkusbp, CHN_CONNECTED);
  248. (void)qmkusb_start_receive(qmkusbp);
  249. }
  250. /**
  251. * @brief Default requests hook.
  252. * @details Applications wanting to use the Serial over USB driver can use
  253. * this function as requests hook in the USB configuration.
  254. * The following requests are emulated:
  255. * - CDC_GET_LINE_CODING.
  256. * - CDC_SET_LINE_CODING.
  257. * - CDC_SET_CONTROL_LINE_STATE.
  258. * .
  259. *
  260. * @param[in] usbp pointer to the @p USBDriver object
  261. * @return The hook status.
  262. * @retval true Message handled internally.
  263. * @retval false Message not handled.
  264. */
  265. bool qmkusbRequestsHook(USBDriver *usbp) {
  266. if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) {
  267. switch (usbp->setup[1]) {
  268. case CDC_GET_LINE_CODING:
  269. usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
  270. return true;
  271. case CDC_SET_LINE_CODING:
  272. usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
  273. return true;
  274. case CDC_SET_CONTROL_LINE_STATE:
  275. /* Nothing to do, there are no control lines.*/
  276. usbSetupTransfer(usbp, NULL, 0, NULL);
  277. return true;
  278. default:
  279. return false;
  280. }
  281. }
  282. return false;
  283. }
  284. /**
  285. * @brief SOF handler.
  286. * @details The SOF interrupt is used for automatic flushing of incomplete
  287. * buffers pending in the output queue.
  288. *
  289. * @param[in] qmkusbp pointer to a @p QMKUSBDriver object
  290. *
  291. * @iclass
  292. */
  293. void qmkusbSOFHookI(QMKUSBDriver *qmkusbp) {
  294. /* If the USB driver is not in the appropriate state then transactions
  295. must not be started.*/
  296. if ((usbGetDriverStateI(qmkusbp->config->usbp) != USB_ACTIVE) || (qmkusbp->state != QMKUSB_READY)) {
  297. return;
  298. }
  299. /* If there is already a transaction ongoing then another one cannot be
  300. started.*/
  301. if (usbGetTransmitStatusI(qmkusbp->config->usbp, qmkusbp->config->bulk_in)) {
  302. return;
  303. }
  304. /* Checking if there only a buffer partially filled, if so then it is
  305. enforced in the queue and transmitted.*/
  306. if (obqTryFlushI(&qmkusbp->obqueue)) {
  307. size_t n;
  308. uint8_t *buf = obqGetFullBufferI(&qmkusbp->obqueue, &n);
  309. /* For fixed size drivers, fill the end with zeros */
  310. if (qmkusbp->config->fixed_size) {
  311. memset(buf + n, 0, qmkusbp->config->in_size - n);
  312. n = qmkusbp->config->in_size;
  313. }
  314. osalDbgAssert(buf != NULL, "queue is empty");
  315. usbStartTransmitI(qmkusbp->config->usbp, qmkusbp->config->bulk_in, buf, n);
  316. }
  317. }
  318. /**
  319. * @brief Default data transmitted callback.
  320. * @details The application must use this function as callback for the IN
  321. * data endpoint.
  322. *
  323. * @param[in] usbp pointer to the @p USBDriver object
  324. * @param[in] ep IN endpoint number
  325. */
  326. void qmkusbDataTransmitted(USBDriver *usbp, usbep_t ep) {
  327. uint8_t * buf;
  328. size_t n;
  329. QMKUSBDriver *qmkusbp = usbp->in_params[ep - 1U];
  330. if (qmkusbp == NULL) {
  331. return;
  332. }
  333. osalSysLockFromISR();
  334. /* Signaling that space is available in the output queue.*/
  335. chnAddFlagsI(qmkusbp, CHN_OUTPUT_EMPTY);
  336. /* Freeing the buffer just transmitted, if it was not a zero size packet.*/
  337. if (usbp->epc[ep]->in_state->txsize > 0U) {
  338. obqReleaseEmptyBufferI(&qmkusbp->obqueue);
  339. }
  340. /* Checking if there is a buffer ready for transmission.*/
  341. buf = obqGetFullBufferI(&qmkusbp->obqueue, &n);
  342. if (buf != NULL) {
  343. /* The endpoint cannot be busy, we are in the context of the callback,
  344. so it is safe to transmit without a check.*/
  345. usbStartTransmitI(usbp, ep, buf, n);
  346. } else if ((usbp->epc[ep]->in_state->txsize > 0U) && ((usbp->epc[ep]->in_state->txsize & ((size_t)usbp->epc[ep]->in_maxsize - 1U)) == 0U)) {
  347. /* Transmit zero sized packet in case the last one has maximum allowed
  348. size. Otherwise the recipient may expect more data coming soon and
  349. not return buffered data to app. See section 5.8.3 Bulk Transfer
  350. Packet Size Constraints of the USB Specification document.*/
  351. if (!qmkusbp->config->fixed_size) {
  352. usbStartTransmitI(usbp, ep, usbp->setup, 0);
  353. }
  354. } else {
  355. /* Nothing to transmit.*/
  356. }
  357. osalSysUnlockFromISR();
  358. }
  359. /**
  360. * @brief Default data received callback.
  361. * @details The application must use this function as callback for the OUT
  362. * data endpoint.
  363. *
  364. * @param[in] usbp pointer to the @p USBDriver object
  365. * @param[in] ep OUT endpoint number
  366. */
  367. void qmkusbDataReceived(USBDriver *usbp, usbep_t ep) {
  368. QMKUSBDriver *qmkusbp = usbp->out_params[ep - 1U];
  369. if (qmkusbp == NULL) {
  370. return;
  371. }
  372. osalSysLockFromISR();
  373. /* Signaling that data is available in the input queue.*/
  374. chnAddFlagsI(qmkusbp, CHN_INPUT_AVAILABLE);
  375. /* Posting the filled buffer in the queue.*/
  376. ibqPostFullBufferI(&qmkusbp->ibqueue, usbGetReceiveTransactionSizeX(qmkusbp->config->usbp, qmkusbp->config->bulk_out));
  377. /* The endpoint cannot be busy, we are in the context of the callback,
  378. so a packet is in the buffer for sure. Trying to get a free buffer
  379. for the next transaction.*/
  380. (void)qmkusb_start_receive(qmkusbp);
  381. osalSysUnlockFromISR();
  382. }
  383. /**
  384. * @brief Default data received callback.
  385. * @details The application must use this function as callback for the IN
  386. * interrupt endpoint.
  387. *
  388. * @param[in] usbp pointer to the @p USBDriver object
  389. * @param[in] ep endpoint number
  390. */
  391. void qmkusbInterruptTransmitted(USBDriver *usbp, usbep_t ep) {
  392. (void)usbp;
  393. (void)ep;
  394. }
  395. /** @} */