usbcfg.c 16 KB

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