PD Buddy Sink Firmware
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.

usbcfg.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * PD Buddy Sink Firmware - Smart power jack for USB Power Delivery
  3. * Copyright (C) 2017-2018 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
  20. Licensed under the Apache License, Version 2.0 (the "License");
  21. you may not use this file except in compliance with the License.
  22. You may obtain a copy of the License at
  23. http://www.apache.org/licenses/LICENSE-2.0
  24. Unless required by applicable law or agreed to in writing, software
  25. distributed under the License is distributed on an "AS IS" BASIS,
  26. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. See the License for the specific language governing permissions and
  28. limitations under the License.
  29. */
  30. #include "usbcfg.h"
  31. /* Virtual serial port over USB.*/
  32. SerialUSBDriver SDU1;
  33. /*
  34. * Endpoints to be used for USBD1.
  35. */
  36. #define USBD1_DATA_REQUEST_EP 1
  37. #define USBD1_DATA_AVAILABLE_EP 1
  38. #define USBD1_INTERRUPT_REQUEST_EP 2
  39. /*
  40. * USB Device Descriptor.
  41. */
  42. static const uint8_t vcom_device_descriptor_data[18] = {
  43. USB_DESC_DEVICE (0x0110, /* bcdUSB (1.1). */
  44. 0x02, /* bDeviceClass (CDC). */
  45. 0x00, /* bDeviceSubClass. */
  46. 0x00, /* bDeviceProtocol. */
  47. 0x40, /* bMaxPacketSize. */
  48. 0x1209, /* idVendor (pid.codes). */
  49. 0x9DB5, /* idProduct (PD Buddy Sink). */
  50. 0x0200, /* bcdDevice. */
  51. 1, /* iManufacturer. */
  52. 2, /* iProduct. */
  53. 3, /* iSerialNumber. */
  54. 1) /* bNumConfigurations. */
  55. };
  56. /*
  57. * Device Descriptor wrapper.
  58. */
  59. static const USBDescriptor vcom_device_descriptor = {
  60. sizeof vcom_device_descriptor_data,
  61. vcom_device_descriptor_data
  62. };
  63. /* Configuration Descriptor tree for a CDC.*/
  64. static const uint8_t vcom_configuration_descriptor_data[76] = {
  65. /* Configuration Descriptor.*/
  66. USB_DESC_CONFIGURATION(76, /* wTotalLength. */
  67. 0x03, /* bNumInterfaces. */
  68. 0x01, /* bConfigurationValue. */
  69. 0, /* iConfiguration. */
  70. 0xC0, /* bmAttributes (self powered). */
  71. 50), /* bMaxPower (100mA). */
  72. /* Interface Descriptor.*/
  73. USB_DESC_INTERFACE (0x00, /* bInterfaceNumber. */
  74. 0x00, /* bAlternateSetting. */
  75. 0x01, /* bNumEndpoints. */
  76. 0x02, /* bInterfaceClass (Communications
  77. Interface Class, CDC section
  78. 4.2). */
  79. 0x02, /* bInterfaceSubClass (Abstract
  80. Control Model, CDC section 4.3). */
  81. 0x01, /* bInterfaceProtocol (AT commands,
  82. CDC section 4.4). */
  83. 0), /* iInterface. */
  84. /* Header Functional Descriptor (CDC section 5.2.3).*/
  85. USB_DESC_BYTE (5), /* bLength. */
  86. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  87. USB_DESC_BYTE (0x00), /* bDescriptorSubtype (Header
  88. Functional Descriptor. */
  89. USB_DESC_BCD (0x0110), /* bcdCDC. */
  90. /* Call Management Functional Descriptor. */
  91. USB_DESC_BYTE (5), /* bFunctionLength. */
  92. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  93. USB_DESC_BYTE (0x01), /* bDescriptorSubtype (Call Management
  94. Functional Descriptor). */
  95. USB_DESC_BYTE (0x00), /* bmCapabilities (D0+D1). */
  96. USB_DESC_BYTE (0x01), /* bDataInterface. */
  97. /* ACM Functional Descriptor.*/
  98. USB_DESC_BYTE (4), /* bFunctionLength. */
  99. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  100. USB_DESC_BYTE (0x02), /* bDescriptorSubtype (Abstract
  101. Control Management Descriptor). */
  102. USB_DESC_BYTE (0x02), /* bmCapabilities. */
  103. /* Union Functional Descriptor.*/
  104. USB_DESC_BYTE (5), /* bFunctionLength. */
  105. USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
  106. USB_DESC_BYTE (0x06), /* bDescriptorSubtype (Union
  107. Functional Descriptor). */
  108. USB_DESC_BYTE (0x00), /* bMasterInterface (Communication
  109. Class Interface). */
  110. USB_DESC_BYTE (0x01), /* bSlaveInterface0 (Data Class
  111. Interface). */
  112. /* Endpoint 2 Descriptor.*/
  113. USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80,
  114. 0x03, /* bmAttributes (Interrupt). */
  115. 0x0008, /* wMaxPacketSize. */
  116. 0xFF), /* bInterval. */
  117. /* Interface Descriptor.*/
  118. USB_DESC_INTERFACE (0x01, /* bInterfaceNumber. */
  119. 0x00, /* bAlternateSetting. */
  120. 0x02, /* bNumEndpoints. */
  121. 0x0A, /* bInterfaceClass (Data Class
  122. Interface, CDC section 4.5). */
  123. 0x00, /* bInterfaceSubClass (CDC section
  124. 4.6). */
  125. 0x00, /* bInterfaceProtocol (CDC section
  126. 4.7). */
  127. 0x00), /* iInterface. */
  128. /* Endpoint 3 Descriptor.*/
  129. USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
  130. 0x02, /* bmAttributes (Bulk). */
  131. 0x0040, /* wMaxPacketSize. */
  132. 0x00), /* bInterval. */
  133. /* Endpoint 1 Descriptor.*/
  134. USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
  135. 0x02, /* bmAttributes (Bulk). */
  136. 0x0040, /* wMaxPacketSize. */
  137. 0x00), /* bInterval. */
  138. /* Interface Descriptor.*/
  139. USB_DESC_INTERFACE (0x02, /* bInterfaceNumber. */
  140. 0x00, /* bAlternateSetting. */
  141. 0x00, /* bNumEndpoints. */
  142. 0xFF, /* bInterfaceClass (Communications
  143. Interface Class, CDC section
  144. 4.2). */
  145. 'F', /* bInterfaceSubClass (Abstract
  146. Control Model, CDC section 4.3). */
  147. 'W', /* bInterfaceProtocol (AT commands,
  148. CDC section 4.4). */
  149. 3), /* iInterface. */
  150. };
  151. /*
  152. * Configuration Descriptor wrapper.
  153. */
  154. static const USBDescriptor vcom_configuration_descriptor = {
  155. sizeof vcom_configuration_descriptor_data,
  156. vcom_configuration_descriptor_data
  157. };
  158. /*
  159. * U.S. English language identifier.
  160. */
  161. static const uint8_t vcom_string0[] = {
  162. USB_DESC_BYTE(4), /* bLength. */
  163. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  164. USB_DESC_WORD(0x0409) /* wLANGID (U.S. English). */
  165. };
  166. /*
  167. * Vendor string.
  168. */
  169. static const uint8_t vcom_string1[] = {
  170. USB_DESC_BYTE(18), /* bLength. */
  171. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  172. 'P', 0, 'D', 0, ' ', 0, 'B', 0, 'u', 0, 'd', 0, 'd', 0, 'y', 0,
  173. };
  174. /*
  175. * Device Description string.
  176. */
  177. static const uint8_t vcom_string2[] = {
  178. USB_DESC_BYTE(10), /* bLength. */
  179. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  180. 'S', 0, 'i', 0, 'n', 0, 'k', 0
  181. };
  182. /*
  183. * Serial Number string.
  184. */
  185. static const uint8_t vcom_string3[] = {
  186. USB_DESC_BYTE(20), /* bLength. */
  187. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
  188. '1', 0, '.', 0, '3', 0, '.', 0, '0', 0, '-', 0, 'd', 0, 'e', 0, 'v', 0
  189. };
  190. /*
  191. * Strings wrappers array.
  192. */
  193. static const USBDescriptor vcom_strings[] = {
  194. {sizeof vcom_string0, vcom_string0},
  195. {sizeof vcom_string1, vcom_string1},
  196. {sizeof vcom_string2, vcom_string2},
  197. {sizeof vcom_string3, vcom_string3}
  198. };
  199. /*
  200. * Handles the GET_DESCRIPTOR callback. All required descriptors must be
  201. * handled here.
  202. */
  203. static const USBDescriptor *get_descriptor(USBDriver *usbp,
  204. uint8_t dtype,
  205. uint8_t dindex,
  206. uint16_t lang) {
  207. (void)usbp;
  208. (void)lang;
  209. switch (dtype) {
  210. case USB_DESCRIPTOR_DEVICE:
  211. return &vcom_device_descriptor;
  212. case USB_DESCRIPTOR_CONFIGURATION:
  213. return &vcom_configuration_descriptor;
  214. case USB_DESCRIPTOR_STRING:
  215. if (dindex < 4)
  216. return &vcom_strings[dindex];
  217. }
  218. return NULL;
  219. }
  220. /**
  221. * @brief IN EP1 state.
  222. */
  223. static USBInEndpointState ep1instate;
  224. /**
  225. * @brief OUT EP1 state.
  226. */
  227. static USBOutEndpointState ep1outstate;
  228. /**
  229. * @brief EP1 initialization structure (both IN and OUT).
  230. */
  231. static const USBEndpointConfig ep1config = {
  232. USB_EP_MODE_TYPE_BULK,
  233. NULL,
  234. sduDataTransmitted,
  235. sduDataReceived,
  236. 0x0040,
  237. 0x0040,
  238. &ep1instate,
  239. &ep1outstate,
  240. 1,
  241. NULL
  242. };
  243. /**
  244. * @brief IN EP2 state.
  245. */
  246. static USBInEndpointState ep2instate;
  247. /**
  248. * @brief EP2 initialization structure (IN only).
  249. */
  250. static const USBEndpointConfig ep2config = {
  251. USB_EP_MODE_TYPE_INTR,
  252. NULL,
  253. sduInterruptTransmitted,
  254. NULL,
  255. 0x0010,
  256. 0x0000,
  257. &ep2instate,
  258. NULL,
  259. 1,
  260. NULL
  261. };
  262. /*
  263. * Handles the USB driver global events.
  264. */
  265. static void usb_event(USBDriver *usbp, usbevent_t event) {
  266. extern SerialUSBDriver SDU1;
  267. switch (event) {
  268. case USB_EVENT_ADDRESS:
  269. return;
  270. case USB_EVENT_CONFIGURED:
  271. chSysLockFromISR();
  272. /* Enables the endpoints specified into the configuration.
  273. Note, this callback is invoked from an ISR so I-Class functions
  274. must be used.*/
  275. usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config);
  276. usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config);
  277. /* Resetting the state of the CDC subsystem.*/
  278. sduConfigureHookI(&SDU1);
  279. chSysUnlockFromISR();
  280. return;
  281. case USB_EVENT_RESET:
  282. /* Falls into.*/
  283. case USB_EVENT_UNCONFIGURED:
  284. /* Falls into.*/
  285. case USB_EVENT_SUSPEND:
  286. chSysLockFromISR();
  287. /* Disconnection event on suspend.*/
  288. sduSuspendHookI(&SDU1);
  289. chSysUnlockFromISR();
  290. return;
  291. case USB_EVENT_WAKEUP:
  292. chSysLockFromISR();
  293. /* Disconnection event on suspend.*/
  294. sduWakeupHookI(&SDU1);
  295. chSysUnlockFromISR();
  296. return;
  297. case USB_EVENT_STALLED:
  298. return;
  299. }
  300. return;
  301. }
  302. /*
  303. * Handles the USB driver global events.
  304. */
  305. static void sof_handler(USBDriver *usbp) {
  306. (void)usbp;
  307. osalSysLockFromISR();
  308. sduSOFHookI(&SDU1);
  309. osalSysUnlockFromISR();
  310. }
  311. /*
  312. * USB driver configuration.
  313. */
  314. const USBConfig usbcfg = {
  315. usb_event,
  316. get_descriptor,
  317. sduRequestsHook,
  318. sof_handler
  319. };
  320. /*
  321. * Serial over USB driver configuration.
  322. */
  323. const SerialUSBConfig serusbcfg = {
  324. &USBD1,
  325. USBD1_DATA_REQUEST_EP,
  326. USBD1_DATA_AVAILABLE_EP,
  327. USBD1_INTERRUPT_REQUEST_EP
  328. };