usbcfg_dualcdc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. ChibiOS - Copyright (C) 2006..2015 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. * This file is free software: you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This file is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. * See the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. * Modified for use in AP_HAL by Andrew Tridgell and Siddharth Bharat Purohit
  28. */
  29. #include "hal.h"
  30. #include "hwdef.h"
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "usbcfg.h"
  34. #if defined(HAL_USB_PRODUCT_ID) && HAL_HAVE_DUAL_USB_CDC
  35. /*
  36. * Virtual serial ports over USB.
  37. */
  38. SerialUSBDriver SDU1;
  39. SerialUSBDriver SDU2;
  40. /*
  41. * Endpoints.
  42. */
  43. #define USB_INTERRUPT_REQUEST_EP_A 1
  44. #define USB_DATA_AVAILABLE_EP_A 2
  45. #define USB_DATA_REQUEST_EP_A 2
  46. #define USB_INTERRUPT_REQUEST_EP_B 3
  47. #define USB_DATA_AVAILABLE_EP_B 4
  48. #define USB_DATA_REQUEST_EP_B 4
  49. #define USB_INTERRUPT_REQUEST_SIZE 0x10
  50. #define USB_DATA_SIZE 0x40
  51. /*
  52. * Interfaces
  53. */
  54. #define USB_NUM_INTERFACES 4
  55. #define USB_CDC_CIF_NUM0 0
  56. #define USB_CDC_DIF_NUM0 1
  57. #define USB_CDC_CIF_NUM1 2
  58. #define USB_CDC_DIF_NUM1 3
  59. /*
  60. * USB Device Descriptor.
  61. */
  62. static const uint8_t vcom_device_descriptor_data[] = {
  63. USB_DESC_DEVICE(
  64. 0x0200, /* bcdUSB (1.1). */
  65. 0xEF, /* bDeviceClass (misc). */
  66. 0x02, /* bDeviceSubClass (common). */
  67. 0x01, /* bDeviceProtocol (IAD). */
  68. USB_DATA_SIZE, /* bMaxPacketSize. */
  69. HAL_USB_VENDOR_ID, /* idVendor. */
  70. HAL_USB_PRODUCT_ID, /* idProduct. */
  71. 0x0200, /* bcdDevice. */
  72. 1, /* iManufacturer. */
  73. 2, /* iProduct. */
  74. 3, /* iSerialNumber. */
  75. 1) /* bNumConfigurations. */
  76. };
  77. /*
  78. * Device Descriptor wrapper.
  79. */
  80. static const USBDescriptor vcom_device_descriptor = {
  81. sizeof vcom_device_descriptor_data,
  82. vcom_device_descriptor_data
  83. };
  84. #define CDC_IF_DESC_SET_SIZE \
  85. (USB_DESC_INTERFACE_SIZE + 5 + 5 + 4 + 5 + USB_DESC_ENDPOINT_SIZE + \
  86. USB_DESC_INTERFACE_SIZE + (USB_DESC_ENDPOINT_SIZE * 2))
  87. #define CDC_IF_DESC_SET(comIfNum, datIfNum, comInEp, datOutEp, datInEp) \
  88. /* Interface Descriptor.*/ \
  89. USB_DESC_INTERFACE( \
  90. comIfNum, /* bInterfaceNumber. */ \
  91. 0x00, /* bAlternateSetting. */ \
  92. 0x01, /* bNumEndpoints. */ \
  93. CDC_COMMUNICATION_INTERFACE_CLASS, /* bInterfaceClass. */ \
  94. CDC_ABSTRACT_CONTROL_MODEL, /* bInterfaceSubClass. */ \
  95. 0x01, /* bInterfaceProtocol (AT
  96. commands, CDC section
  97. 4.4). */ \
  98. 0), /* iInterface. */ \
  99. /* Header Functional Descriptor (CDC section 5.2.3).*/ \
  100. USB_DESC_BYTE (5), /* bLength. */ \
  101. USB_DESC_BYTE (CDC_CS_INTERFACE), /* bDescriptorType. */ \
  102. USB_DESC_BYTE (CDC_HEADER), /* bDescriptorSubtype. */ \
  103. USB_DESC_BCD (0x0110), /* bcdCDC. */ \
  104. /* Call Management Functional Descriptor.*/ \
  105. USB_DESC_BYTE (5), /* bFunctionLength. */ \
  106. USB_DESC_BYTE (CDC_CS_INTERFACE), /* bDescriptorType. */ \
  107. USB_DESC_BYTE (CDC_CALL_MANAGEMENT), /* bDescriptorSubtype. */ \
  108. USB_DESC_BYTE (0x03), /*******/ /* bmCapabilities. */ \
  109. USB_DESC_BYTE (datIfNum), /* bDataInterface. */ \
  110. /* Abstract Control Management Functional Descriptor.*/ \
  111. USB_DESC_BYTE (4), /* bFunctionLength. */ \
  112. USB_DESC_BYTE (CDC_CS_INTERFACE), /* bDescriptorType. */ \
  113. USB_DESC_BYTE (CDC_ABSTRACT_CONTROL_MANAGEMENT), \
  114. USB_DESC_BYTE (0x02), /* bmCapabilities. */ \
  115. /* Union Functional Descriptor.*/ \
  116. USB_DESC_BYTE (5), /* bFunctionLength. */ \
  117. USB_DESC_BYTE (CDC_CS_INTERFACE), /* bDescriptorType. */ \
  118. USB_DESC_BYTE (CDC_UNION), /* bDescriptorSubtype. */ \
  119. USB_DESC_BYTE (comIfNum), /* bMasterInterface. */ \
  120. USB_DESC_BYTE (datIfNum), /* bSlaveInterface. */ \
  121. /* Endpoint, Interrupt IN.*/ \
  122. USB_DESC_ENDPOINT ( \
  123. comInEp, \
  124. USB_EP_MODE_TYPE_INTR, /* bmAttributes. */ \
  125. USB_INTERRUPT_REQUEST_SIZE, /* wMaxPacketSize. */ \
  126. 0x01), /* bInterval. */ \
  127. \
  128. /* CDC Data Interface Descriptor.*/ \
  129. USB_DESC_INTERFACE( \
  130. datIfNum, /* bInterfaceNumber. */ \
  131. 0x00, /* bAlternateSetting. */ \
  132. 0x02, /* bNumEndpoints. */ \
  133. CDC_DATA_INTERFACE_CLASS, /* bInterfaceClass. */ \
  134. 0x00, /* bInterfaceSubClass (CDC
  135. section 4.6). */ \
  136. 0x00, /* bInterfaceProtocol (CDC
  137. section 4.7). */ \
  138. 0x00), /* iInterface. */ \
  139. /* Endpoint, Bulk OUT.*/ \
  140. USB_DESC_ENDPOINT( \
  141. datOutEp, /* bEndpointAddress. */ \
  142. USB_EP_MODE_TYPE_BULK, /* bmAttributes. */ \
  143. USB_DATA_SIZE, /* wMaxPacketSize. */ \
  144. 0x00), /* bInterval. */ \
  145. /* Endpoint, Bulk IN.*/ \
  146. USB_DESC_ENDPOINT( \
  147. datInEp, /* bEndpointAddress. */ \
  148. USB_EP_MODE_TYPE_BULK, /* bmAttributes. */ \
  149. USB_DATA_SIZE, /* wMaxPacketSize. */ \
  150. 0x00) /* bInterval. */
  151. #define IAD_CDC_IF_DESC_SET_SIZE \
  152. (USB_DESC_INTERFACE_ASSOCIATION_SIZE + CDC_IF_DESC_SET_SIZE)
  153. #define IAD_CDC_IF_DESC_SET(comIfNum, datIfNum, comInEp, datOutEp, datInEp) \
  154. /* Interface Association Descriptor.*/ \
  155. USB_DESC_INTERFACE_ASSOCIATION( \
  156. comIfNum, /* bFirstInterface. */ \
  157. 2, /* bInterfaceCount. */ \
  158. CDC_COMMUNICATION_INTERFACE_CLASS, /* bFunctionClass. */ \
  159. CDC_ABSTRACT_CONTROL_MODEL, /* bFunctionSubClass. */ \
  160. 1, /* bFunctionProcotol. */ \
  161. 0 /* iInterface. */ \
  162. ), \
  163. /* CDC Interface descriptor set */ \
  164. CDC_IF_DESC_SET(comIfNum, datIfNum, comInEp, datOutEp, datInEp)
  165. /* Configuration Descriptor tree for a CDC.*/
  166. static const uint8_t vcom_configuration_descriptor_data[] = {
  167. /* Configuration Descriptor.*/
  168. USB_DESC_CONFIGURATION(
  169. USB_DESC_CONFIGURATION_SIZE +
  170. (IAD_CDC_IF_DESC_SET_SIZE * 2), /* wTotalLength. */
  171. USB_NUM_INTERFACES, /* bNumInterfaces. */
  172. 0x01, /* bConfigurationValue. */
  173. 0, /* iConfiguration. */
  174. 0xC0, /* bmAttributes (self powered). */
  175. 50 /* bMaxPower (100mA). */
  176. ),
  177. IAD_CDC_IF_DESC_SET(
  178. USB_CDC_CIF_NUM0,
  179. USB_CDC_DIF_NUM0,
  180. USB_ENDPOINT_IN(USB_INTERRUPT_REQUEST_EP_A),
  181. USB_ENDPOINT_OUT(USB_DATA_AVAILABLE_EP_A),
  182. USB_ENDPOINT_IN(USB_DATA_REQUEST_EP_A)
  183. ),
  184. IAD_CDC_IF_DESC_SET(
  185. USB_CDC_CIF_NUM1,
  186. USB_CDC_DIF_NUM1,
  187. USB_ENDPOINT_IN(USB_INTERRUPT_REQUEST_EP_B),
  188. USB_ENDPOINT_OUT(USB_DATA_AVAILABLE_EP_B),
  189. USB_ENDPOINT_IN(USB_DATA_REQUEST_EP_B)
  190. ),
  191. };
  192. /*
  193. * Configuration Descriptor wrapper.
  194. */
  195. static const USBDescriptor vcom_configuration_descriptor = {
  196. sizeof vcom_configuration_descriptor_data,
  197. vcom_configuration_descriptor_data
  198. };
  199. /*
  200. * U.S. English language identifier.
  201. */
  202. static const uint8_t vcom_string0[] = {
  203. USB_DESC_BYTE(4), /* bLength. */
  204. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  205. USB_DESC_WORD(0x0409) /* wLANGID (U.S. English). */
  206. };
  207. /*
  208. * Strings wrappers array. The strings are created dynamically to
  209. * allow them to be setup with apj_tool
  210. */
  211. static USBDescriptor vcom_strings[] = {
  212. {sizeof vcom_string0, vcom_string0},
  213. {0, NULL}, // manufacturer
  214. {0, NULL}, // product
  215. {0, NULL}, // version
  216. };
  217. #define USB_DESC_MAX_STRLEN 100
  218. static uint8_t vcom_buffers[3][2+2*USB_DESC_MAX_STRLEN];
  219. /*
  220. dynamically allocate a USB descriptor string
  221. */
  222. static void setup_usb_string(USBDescriptor *desc, const char *str, uint8_t *b)
  223. {
  224. char str2[USB_DESC_MAX_STRLEN];
  225. string_substitute(str, str2);
  226. uint8_t len = strlen(str2);
  227. desc->ud_size = 2+2*len;
  228. desc->ud_string = (const uint8_t *)b;
  229. b[0] = USB_DESC_BYTE(desc->ud_size);
  230. b[1] = USB_DESC_BYTE(USB_DESCRIPTOR_STRING);
  231. uint8_t i;
  232. for (i=0; i<len; i++) {
  233. b[2+i*2] = str2[i];
  234. b[2+i*2+1] = 0;
  235. }
  236. }
  237. /*
  238. dynamically allocate a USB descriptor strings
  239. */
  240. void setup_usb_strings(void)
  241. {
  242. setup_usb_string(&vcom_strings[1], HAL_USB_STRING_MANUFACTURER, vcom_buffers[0]);
  243. setup_usb_string(&vcom_strings[2], HAL_USB_STRING_PRODUCT, vcom_buffers[1]);
  244. setup_usb_string(&vcom_strings[3], HAL_USB_STRING_SERIAL, vcom_buffers[2]);
  245. }
  246. /*
  247. * Handles the GET_DESCRIPTOR callback. All required descriptors must be
  248. * handled here.
  249. */
  250. static const USBDescriptor *get_descriptor(USBDriver *usbp,
  251. uint8_t dtype,
  252. uint8_t dindex,
  253. uint16_t lang) {
  254. (void)usbp;
  255. (void)lang;
  256. switch (dtype) {
  257. case USB_DESCRIPTOR_DEVICE:
  258. return &vcom_device_descriptor;
  259. case USB_DESCRIPTOR_CONFIGURATION:
  260. return &vcom_configuration_descriptor;
  261. case USB_DESCRIPTOR_STRING:
  262. if (dindex < 4)
  263. return &vcom_strings[dindex];
  264. }
  265. return NULL;
  266. }
  267. /**
  268. * @brief IN EP1 state.
  269. */
  270. static USBInEndpointState ep1instate;
  271. /**
  272. * @brief EP1 initialization structure (IN only).
  273. */
  274. static const USBEndpointConfig ep1config = {
  275. USB_EP_MODE_TYPE_INTR,
  276. NULL,
  277. sduInterruptTransmitted,
  278. NULL,
  279. USB_INTERRUPT_REQUEST_SIZE,
  280. 0x0000,
  281. &ep1instate,
  282. NULL,
  283. 1,
  284. NULL
  285. };
  286. /**
  287. * @brief IN EP2 state.
  288. */
  289. static USBInEndpointState ep2instate;
  290. /**
  291. * @brief OUT EP2 state.
  292. */
  293. static USBOutEndpointState ep2outstate;
  294. /**
  295. * @brief EP2 initialization structure (both IN and OUT).
  296. */
  297. static const USBEndpointConfig ep2config = {
  298. USB_EP_MODE_TYPE_BULK,
  299. NULL,
  300. sduDataTransmitted,
  301. sduDataReceived,
  302. USB_DATA_SIZE,
  303. USB_DATA_SIZE,
  304. &ep2instate,
  305. &ep2outstate,
  306. 2,
  307. NULL
  308. };
  309. /**
  310. * @brief IN EP3 state.
  311. */
  312. static USBInEndpointState ep3instate;
  313. /**
  314. * @brief EP3 initialization structure (IN only).
  315. */
  316. static const USBEndpointConfig ep3config = {
  317. USB_EP_MODE_TYPE_INTR,
  318. NULL,
  319. sduInterruptTransmitted,
  320. NULL,
  321. USB_INTERRUPT_REQUEST_SIZE,
  322. 0x0000,
  323. &ep3instate,
  324. NULL,
  325. 1,
  326. NULL
  327. };
  328. /**
  329. * @brief IN EP4 state.
  330. */
  331. static USBInEndpointState ep4instate;
  332. /**
  333. * @brief OUT EP4 state.
  334. */
  335. static USBOutEndpointState ep4outstate;
  336. /**
  337. * @brief EP4 initialization structure (both IN and OUT).
  338. */
  339. static const USBEndpointConfig ep4config = {
  340. USB_EP_MODE_TYPE_BULK,
  341. NULL,
  342. sduDataTransmitted,
  343. sduDataReceived,
  344. USB_DATA_SIZE,
  345. USB_DATA_SIZE,
  346. &ep4instate,
  347. &ep4outstate,
  348. 2,
  349. NULL
  350. };
  351. /*
  352. * Handles the USB driver global events.
  353. */
  354. static void usb_event(USBDriver *usbp, usbevent_t event) {
  355. extern SerialUSBDriver SDU1;
  356. extern SerialUSBDriver SDU2;
  357. switch (event) {
  358. case USB_EVENT_ADDRESS:
  359. return;
  360. case USB_EVENT_CONFIGURED:
  361. chSysLockFromISR();
  362. if (usbp->state == USB_ACTIVE) {
  363. /* Enables the endpoints specified into the configuration.
  364. Note, this callback is invoked from an ISR so I-Class functions
  365. must be used.*/
  366. usbInitEndpointI(usbp, USB_INTERRUPT_REQUEST_EP_A, &ep1config);
  367. usbInitEndpointI(usbp, USB_DATA_REQUEST_EP_A, &ep2config);
  368. usbInitEndpointI(usbp, USB_INTERRUPT_REQUEST_EP_B, &ep3config);
  369. usbInitEndpointI(usbp, USB_DATA_REQUEST_EP_B, &ep4config);
  370. /* Resetting the state of the CDC subsystem.*/
  371. sduConfigureHookI(&SDU1);
  372. sduConfigureHookI(&SDU2);
  373. }
  374. else if (usbp->state == USB_SELECTED) {
  375. usbDisableEndpointsI(usbp);
  376. }
  377. chSysUnlockFromISR();
  378. return;
  379. case USB_EVENT_RESET:
  380. /* Falls into.*/
  381. case USB_EVENT_UNCONFIGURED:
  382. /* Falls into.*/
  383. case USB_EVENT_SUSPEND:
  384. chSysLockFromISR();
  385. /* Disconnection event on suspend.*/
  386. sduSuspendHookI(&SDU1);
  387. sduSuspendHookI(&SDU2);
  388. chSysUnlockFromISR();
  389. return;
  390. case USB_EVENT_WAKEUP:
  391. chSysLockFromISR();
  392. /* Connection event on wakeup.*/
  393. sduWakeupHookI(&SDU1);
  394. sduWakeupHookI(&SDU2);
  395. chSysUnlockFromISR();
  396. return;
  397. case USB_EVENT_STALLED:
  398. return;
  399. }
  400. return;
  401. }
  402. /*
  403. * Handling messages not implemented in the default handler nor in the
  404. * SerialUSB handler.
  405. */
  406. static bool requests_hook(USBDriver *usbp) {
  407. if (((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE) &&
  408. (usbp->setup[1] == USB_REQ_SET_INTERFACE)) {
  409. usbSetupTransfer(usbp, NULL, 0, NULL);
  410. return true;
  411. }
  412. return sduRequestsHook(usbp);
  413. }
  414. /*
  415. * Handles the USB driver global events.
  416. */
  417. static void sof_handler(USBDriver *usbp) {
  418. (void)usbp;
  419. osalSysLockFromISR();
  420. sduSOFHookI(&SDU1);
  421. sduSOFHookI(&SDU2);
  422. osalSysUnlockFromISR();
  423. }
  424. /*
  425. * USB driver configuration.
  426. */
  427. const USBConfig usbcfg = {
  428. usb_event,
  429. get_descriptor,
  430. requests_hook,
  431. sof_handler
  432. };
  433. /*
  434. * Serial over USB driver configuration 1.
  435. */
  436. const SerialUSBConfig serusbcfg1 = {
  437. &USBD1,
  438. USB_DATA_REQUEST_EP_A,
  439. USB_DATA_AVAILABLE_EP_A,
  440. USB_INTERRUPT_REQUEST_EP_A
  441. };
  442. /*
  443. * Serial over USB driver configuration 2.
  444. */
  445. const SerialUSBConfig serusbcfg2 = {
  446. &USBD1,
  447. USB_DATA_REQUEST_EP_B,
  448. USB_DATA_AVAILABLE_EP_B,
  449. USB_INTERRUPT_REQUEST_EP_B
  450. };
  451. #endif