usbcfg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 "ch.h"
  14. #include "hal.h"
  15. #include "usbcfg.h"
  16. /*
  17. * Endpoints to be used for USBD1.
  18. */
  19. #define USBD1_DATA_REQUEST_EP 1
  20. #define USBD1_DATA_AVAILABLE_EP 3
  21. #define USBD1_INTERRUPT_REQUEST_EP 2
  22. #if (AVR_USB_USE_NAMED_ADDRESS_SPACES == TRUE) && defined(__FLASH)
  23. # undef ROMCONST
  24. # define ROMCONST const __flash
  25. #endif
  26. /* Virtual serial port over USB.*/
  27. SerialUSBDriver SDU1;
  28. /*
  29. * USB Device Descriptor.
  30. */
  31. static ROMCONST uint8_t vcom_device_descriptor_data[18] = {
  32. USB_DESC_DEVICE (0x0110, /* bcdUSB (1.1). */
  33. 0x02, /* bDeviceClass (CDC). */
  34. 0x00, /* bDeviceSubClass. */
  35. 0x00, /* bDeviceProtocol. */
  36. 0x40, /* bMaxPacketSize. */
  37. 0x2341, /* idVendor (Arduino LLC). */
  38. 0x0036, /* idProduct. */
  39. 0x0200, /* bcdDevice. */
  40. 2, /* iManufacturer. */
  41. 1, /* iProduct. */
  42. 3, /* iSerialNumber. */
  43. 1) /* bNumConfigurations. */
  44. };
  45. /*
  46. * Device Descriptor wrapper.
  47. */
  48. static ROMCONST USBDescriptor vcom_device_descriptor = {
  49. sizeof vcom_device_descriptor_data,
  50. vcom_device_descriptor_data
  51. };
  52. /* Configuration Descriptor tree for a CDC.*/
  53. static ROMCONST uint8_t vcom_configuration_descriptor_data[67] = {
  54. /* Configuration Descriptor.*/
  55. USB_DESC_CONFIGURATION(67, /* wTotalLength. */
  56. 0x02, /* bNumInterfaces. */
  57. 0x01, /* bConfigurationValue. */
  58. 0, /* iConfiguration. */
  59. 0xC0, /* bmAttributes (self powered). */
  60. 50), /* bMaxPower (100mA). */
  61. /* Interface Descriptor.*/
  62. USB_DESC_INTERFACE (0x00, /* bInterfaceNumber. */
  63. 0x00, /* bAlternateSetting. */
  64. 0x01, /* bNumEndpoints. */
  65. 0x02, /* bInterfaceClass (Communications
  66. Interface Class, CDC section
  67. 4.2). */
  68. 0x02, /* bInterfaceSubClass (Abstract
  69. Control Model, CDC section 4.3). */
  70. 0x01, /* bInterfaceProtocol (AT commands,
  71. CDC section 4.4). */
  72. 0), /* iInterface. */
  73. /* Header Functional Descriptor (CDC section 5.2.3).*/
  74. USB_DESC_BYTE (5), /* bLength. */
  75. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  76. USB_DESC_BYTE (0x00), /* bDescriptorSubtype (Header
  77. Functional Descriptor. */
  78. USB_DESC_BCD (0x0110), /* bcdCDC. */
  79. /* Call Management Functional Descriptor. */
  80. USB_DESC_BYTE (5), /* bFunctionLength. */
  81. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  82. USB_DESC_BYTE (0x01), /* bDescriptorSubtype (Call Management
  83. Functional Descriptor). */
  84. USB_DESC_BYTE (0x00), /* bmCapabilities (D0+D1). */
  85. USB_DESC_BYTE (0x01), /* bDataInterface. */
  86. /* ACM Functional Descriptor.*/
  87. USB_DESC_BYTE (4), /* bFunctionLength. */
  88. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  89. USB_DESC_BYTE (0x02), /* bDescriptorSubtype (Abstract
  90. Control Management Descriptor). */
  91. USB_DESC_BYTE (0x02), /* bmCapabilities. */
  92. /* Union Functional Descriptor.*/
  93. USB_DESC_BYTE (5), /* bFunctionLength. */
  94. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  95. USB_DESC_BYTE (0x06), /* bDescriptorSubtype (Union
  96. Functional Descriptor). */
  97. USB_DESC_BYTE (0x00), /* bMasterInterface (Communication
  98. Class Interface). */
  99. USB_DESC_BYTE (0x01), /* bSlaveInterface0 (Data Class
  100. Interface). */
  101. /* Endpoint 2 Descriptor.*/
  102. USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80,
  103. 0x03, /* bmAttributes (Interrupt). */
  104. 0x0008, /* wMaxPacketSize. */
  105. 0xFF), /* bInterval. */
  106. /* Interface Descriptor.*/
  107. USB_DESC_INTERFACE (0x01, /* bInterfaceNumber. */
  108. 0x00, /* bAlternateSetting. */
  109. 0x02, /* bNumEndpoints. */
  110. 0x0A, /* bInterfaceClass (Data Class
  111. Interface, CDC section 4.5). */
  112. 0x00, /* bInterfaceSubClass (CDC section
  113. 4.6). */
  114. 0x00, /* bInterfaceProtocol (CDC section
  115. 4.7). */
  116. 0x00), /* iInterface. */
  117. /* Endpoint 3 Descriptor.*/
  118. USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
  119. 0x02, /* bmAttributes (Bulk). */
  120. 0x0040, /* wMaxPacketSize. */
  121. 0x00), /* bInterval. */
  122. /* Endpoint 1 Descriptor.*/
  123. USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
  124. 0x02, /* bmAttributes (Bulk). */
  125. 0x0040, /* wMaxPacketSize. */
  126. 0x00) /* bInterval. */
  127. };
  128. /*
  129. * Configuration Descriptor wrapper.
  130. */
  131. static ROMCONST USBDescriptor vcom_configuration_descriptor = {
  132. sizeof vcom_configuration_descriptor_data,
  133. vcom_configuration_descriptor_data
  134. };
  135. /*
  136. * U.S. English language identifier.
  137. */
  138. static ROMCONST uint8_t vcom_string0[] = {
  139. USB_DESC_BYTE(4), /* bLength. */
  140. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  141. USB_DESC_WORD(0x0409) /* wLANGID (U.S. English). */
  142. };
  143. /*
  144. * Vendor string.
  145. */
  146. static ROMCONST uint8_t vcom_string1[] = {
  147. USB_DESC_BYTE(24), /* bLength. */
  148. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  149. 'A', 0, 'r', 0, 'd', 0, 'u', 0, 'i', 0, 'n', 0, 'o', 0,
  150. ' ', 0, 'L', 0, 'L', 0, 'C', 0
  151. };
  152. /*
  153. * Device Description string.
  154. */
  155. static ROMCONST uint8_t vcom_string2[] = {
  156. USB_DESC_BYTE(56), /* bLength. */
  157. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  158. 'C', 0, 'h', 0, 'i', 0, 'b', 0, 'i', 0, 'O', 0, 'S', 0, '/', 0,
  159. 'R', 0, 'T', 0, ' ', 0, 'V', 0, 'i', 0, 'r', 0, 't', 0, 'u', 0,
  160. 'a', 0, 'l', 0, ' ', 0, 'C', 0, 'O', 0, 'M', 0, ' ', 0, 'P', 0,
  161. 'o', 0, 'r', 0, 't', 0
  162. };
  163. /*
  164. * Serial Number string.
  165. */
  166. static ROMCONST uint8_t vcom_string3[] = {
  167. USB_DESC_BYTE(8), /* bLength. */
  168. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  169. '0' + CH_KERNEL_MAJOR, 0,
  170. '0' + CH_KERNEL_MINOR, 0,
  171. '0' + CH_KERNEL_PATCH, 0
  172. };
  173. /*
  174. * Strings wrappers array.
  175. */
  176. static ROMCONST USBDescriptor vcom_strings[] = {
  177. {sizeof vcom_string0, vcom_string0},
  178. {sizeof vcom_string1, vcom_string1},
  179. {sizeof vcom_string2, vcom_string2},
  180. {sizeof vcom_string3, vcom_string3}
  181. };
  182. /*
  183. * Handles the GET_DESCRIPTOR callback. All required descriptors must be
  184. * handled here.
  185. */
  186. static const USBDescriptor *get_descriptor(USBDriver *usbp,
  187. uint8_t dtype,
  188. uint8_t dindex,
  189. uint16_t lang) {
  190. (void)usbp;
  191. (void)lang;
  192. switch (dtype) {
  193. case USB_DESCRIPTOR_DEVICE:
  194. return &vcom_device_descriptor;
  195. case USB_DESCRIPTOR_CONFIGURATION:
  196. return &vcom_configuration_descriptor;
  197. case USB_DESCRIPTOR_STRING:
  198. if (dindex < 4)
  199. return &vcom_strings[dindex];
  200. }
  201. return NULL;
  202. }
  203. /**
  204. * @brief IN EP1 state.
  205. */
  206. static USBInEndpointState ep1state;
  207. /**
  208. * @brief EP1 initialization structure (IN).
  209. */
  210. static const USBEndpointConfig ep1config = {
  211. USB_EP_MODE_TYPE_BULK,
  212. NULL,
  213. sduDataTransmitted,
  214. NULL,
  215. 0x0040,
  216. 0x0000,
  217. &ep1state,
  218. NULL,
  219. };
  220. /**
  221. * @brief INTR EP2 state.
  222. */
  223. static USBInEndpointState ep2state;
  224. /**
  225. * @brief EP2 initialization structure.
  226. */
  227. static const USBEndpointConfig ep2config = {
  228. USB_EP_MODE_TYPE_INTR,
  229. NULL,
  230. sduInterruptTransmitted,
  231. NULL,
  232. 0x0010,
  233. 0x0000,
  234. &ep2state,
  235. NULL
  236. };
  237. /**
  238. * @brief OUT EP3 state.
  239. */
  240. static USBOutEndpointState ep3state;
  241. /**
  242. * @brief EP3 initialization structure (OUT).
  243. */
  244. static const USBEndpointConfig ep3config = {
  245. USB_EP_MODE_TYPE_BULK,
  246. NULL,
  247. NULL,
  248. sduDataReceived,
  249. 0x0000,
  250. 0x0040,
  251. NULL,
  252. &ep3state,
  253. };
  254. /*
  255. * Handles the USB driver global events.
  256. */
  257. static void usb_event(USBDriver *usbp, usbevent_t event) {
  258. extern SerialUSBDriver SDU1;
  259. switch (event) {
  260. case USB_EVENT_ADDRESS:
  261. return;
  262. case USB_EVENT_CONFIGURED:
  263. chSysLockFromISR();
  264. /* Enables the endpoints specified into the configuration.
  265. Note, this callback is invoked from an ISR so I-Class functions
  266. must be used.*/
  267. usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config);
  268. usbInitEndpointI(usbp, USBD1_DATA_AVAILABLE_EP, &ep2config);
  269. usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep3config);
  270. /* Resetting the state of the CDC subsystem.*/
  271. sduConfigureHookI(&SDU1);
  272. chSysUnlockFromISR();
  273. return;
  274. case USB_EVENT_RESET:
  275. /* Falls into.*/
  276. case USB_EVENT_UNCONFIGURED:
  277. /* Falls into.*/
  278. case USB_EVENT_SUSPEND:
  279. chSysLockFromISR();
  280. /* Disconnection event on suspend.*/
  281. sduSuspendHookI(&SDU1);
  282. chSysUnlockFromISR();
  283. return;
  284. case USB_EVENT_WAKEUP:
  285. chSysLockFromISR();
  286. /* Connection event on wakeup.*/
  287. sduWakeupHookI(&SDU1);
  288. chSysUnlockFromISR();
  289. return;
  290. case USB_EVENT_STALLED:
  291. return;
  292. }
  293. return;
  294. }
  295. bool usb_setup_hook(USBDriver *usbp) {
  296. /* Override GET_DESCRIPTOR requests to return data from program memory */
  297. if ((usbp->setup[0] & (USB_RTYPE_RECIPIENT_MASK | USB_RTYPE_TYPE_MASK)) ==
  298. (USB_RTYPE_RECIPIENT_DEVICE | USB_RTYPE_TYPE_STD) &&
  299. usbp->setup[1] == USB_REQ_GET_DESCRIPTOR) {
  300. const uint8_t dtype = usbp->setup[3];
  301. const uint8_t dindex = usbp->setup[2];
  302. const AVR_USB_TX_BUF_ADDRESS_SPACE uint8_t *ddata = NULL;
  303. size_t dsize = 0;
  304. switch (dtype) {
  305. case USB_DESCRIPTOR_DEVICE:
  306. dsize = sizeof(vcom_device_descriptor_data);
  307. ddata = vcom_device_descriptor_data;
  308. break;
  309. case USB_DESCRIPTOR_CONFIGURATION:
  310. dsize = sizeof(vcom_configuration_descriptor_data);
  311. ddata = vcom_configuration_descriptor_data;
  312. break;
  313. case USB_DESCRIPTOR_STRING:
  314. if (dindex == 0) {
  315. dsize = sizeof(vcom_string0);
  316. ddata = vcom_string0;
  317. } else if (dindex == 1) {
  318. dsize = sizeof(vcom_string1);
  319. ddata = vcom_string1;
  320. } else if (dindex == 2) {
  321. dsize = sizeof(vcom_string2);
  322. ddata = vcom_string2;
  323. } else if (dindex == 3) {
  324. dsize = sizeof(vcom_string3);
  325. ddata = vcom_string3;
  326. }
  327. break;
  328. }
  329. if (ddata == NULL) {
  330. return false;
  331. }
  332. usbSetupTransfer(usbp, ddata, dsize, NULL);
  333. return true;
  334. }
  335. return sduRequestsHook(usbp);
  336. }
  337. /*
  338. * Handles the USB driver start of frame event.
  339. */
  340. static void sof_handler(USBDriver *usbp) {
  341. (void)usbp;
  342. osalSysLockFromISR();
  343. sduSOFHookI(&SDU1);
  344. osalSysUnlockFromISR();
  345. }
  346. /*
  347. * USB driver configuration.
  348. */
  349. const USBConfig usbcfg = {
  350. usb_event,
  351. get_descriptor,
  352. usb_setup_hook,
  353. sof_handler
  354. };
  355. /*
  356. * Serial over USB driver configuration.
  357. */
  358. const SerialUSBConfig serusbcfg = {
  359. &USBD1,
  360. USBD1_DATA_REQUEST_EP,
  361. USBD1_DATA_AVAILABLE_EP,
  362. USBD1_INTERRUPT_REQUEST_EP
  363. };