usbd_audio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_audio.h"
  8. struct audio_entity_param {
  9. uint32_t wCur;
  10. uint32_t wMin;
  11. uint32_t wMax;
  12. uint32_t wRes;
  13. };
  14. struct usbd_audio_priv {
  15. struct audio_entity_info *table;
  16. uint8_t num;
  17. uint16_t uac_version;
  18. } g_usbd_audio[CONFIG_USBDEV_MAX_BUS];
  19. static int audio_class_endpoint_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  20. {
  21. uint8_t control_selector;
  22. uint32_t sampling_freq = 0;
  23. uint8_t ep;
  24. control_selector = HI_BYTE(setup->wValue);
  25. ep = LO_BYTE(setup->wIndex);
  26. switch (control_selector) {
  27. case AUDIO_EP_CONTROL_SAMPLING_FEQ:
  28. switch (setup->bRequest) {
  29. case AUDIO_REQUEST_SET_CUR:
  30. memcpy((uint8_t *)&sampling_freq, *data, *len);
  31. USB_LOG_DBG("Set ep:0x%02x %d Hz\r\n", ep, (int)sampling_freq);
  32. usbd_audio_set_sampling_freq(busid, ep, sampling_freq);
  33. break;
  34. case AUDIO_REQUEST_GET_CUR:
  35. case AUDIO_REQUEST_GET_MIN:
  36. case AUDIO_REQUEST_GET_MAX:
  37. case AUDIO_REQUEST_GET_RES:
  38. sampling_freq = usbd_audio_get_sampling_freq(busid, ep);
  39. memcpy(*data, &sampling_freq, 3);
  40. USB_LOG_DBG("Get ep:0x%02x %d Hz\r\n", ep, (int)sampling_freq);
  41. *len = 3;
  42. break;
  43. }
  44. break;
  45. default:
  46. USB_LOG_WRN("Unhandled Audio Class control selector 0x%02x\r\n", control_selector);
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. static int audio_class_interface_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  52. {
  53. USB_LOG_DBG("Audio Class request: "
  54. "bRequest 0x%02x\r\n",
  55. setup->bRequest);
  56. uint8_t entity_id;
  57. uint8_t ep = 0;
  58. uint8_t subtype = 0x01;
  59. uint8_t control_selector;
  60. uint8_t ch;
  61. uint8_t mute;
  62. uint16_t volume;
  63. int volume_db = 0;
  64. uint32_t sampling_freq = 0;
  65. const char *mute_string[2] = { "off", "on" };
  66. entity_id = HI_BYTE(setup->wIndex);
  67. control_selector = HI_BYTE(setup->wValue);
  68. ch = LO_BYTE(setup->wValue);
  69. ARG_UNUSED(mute_string);
  70. for (uint8_t i = 0; i < g_usbd_audio[busid].num; i++) {
  71. if (g_usbd_audio[busid].table[i].bEntityId == entity_id) {
  72. subtype = g_usbd_audio[busid].table[i].bDescriptorSubtype;
  73. ep = g_usbd_audio[busid].table[i].ep;
  74. break;
  75. }
  76. }
  77. if (subtype == 0x01) {
  78. USB_LOG_ERR("Do not find subtype for 0x%02x\r\n", entity_id);
  79. return -1;
  80. }
  81. USB_LOG_DBG("Audio entity_id:%02x, subtype:%02x, cs:%02x\r\n", entity_id, subtype, control_selector);
  82. switch (subtype) {
  83. case AUDIO_CONTROL_FEATURE_UNIT:
  84. switch (control_selector) {
  85. case AUDIO_FU_CONTROL_MUTE:
  86. if (g_usbd_audio[busid].uac_version < 0x0200) {
  87. switch (setup->bRequest) {
  88. case AUDIO_REQUEST_SET_CUR:
  89. mute = (*data)[0];
  90. usbd_audio_set_mute(busid, ep, ch, mute);
  91. break;
  92. case AUDIO_REQUEST_GET_CUR:
  93. (*data)[0] = usbd_audio_get_mute(busid, ep, ch);
  94. *len = 1;
  95. break;
  96. default:
  97. USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x in cs 0x%02x\r\n", setup->bRequest, control_selector);
  98. return -1;
  99. }
  100. } else {
  101. switch (setup->bRequest) {
  102. case AUDIO_REQUEST_CUR:
  103. if (setup->bmRequestType & USB_REQUEST_DIR_MASK) {
  104. (*data)[0] = usbd_audio_get_mute(busid, ep, ch);
  105. *len = 1;
  106. } else {
  107. mute = (*data)[0];
  108. usbd_audio_set_mute(busid, ep, ch, mute);
  109. }
  110. break;
  111. default:
  112. //USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x in cs 0x%02x\r\n", setup->bRequest, control_selector);
  113. return -1;
  114. }
  115. }
  116. break;
  117. case AUDIO_FU_CONTROL_VOLUME:
  118. if (g_usbd_audio[busid].uac_version < 0x0200) {
  119. switch (setup->bRequest) {
  120. case AUDIO_REQUEST_SET_CUR:
  121. memcpy(&volume, *data, *len);
  122. if (volume < 0x8000) {
  123. volume_db = volume / 256;
  124. } else {
  125. volume_db = (volume - 0x10000) / 256;
  126. }
  127. USB_LOG_DBG("Set ep:0x%02x ch:%d vol_hex:0x%04x, vol_db:%d dB\r\n", ep, ch, volume, volume_db);
  128. usbd_audio_set_volume(busid, ep, ch, volume_db);
  129. break;
  130. case AUDIO_REQUEST_GET_CUR:
  131. volume_db = usbd_audio_get_volume(busid, ep, ch);
  132. if (volume_db >= 0) {
  133. volume = volume_db * 256;
  134. } else {
  135. volume = volume_db * 256 + 0x10000;
  136. }
  137. USB_LOG_DBG("Get ep:0x%02x ch:%d vol_hex:0x%04x, vol_db:%d dB\r\n", ep, ch, volume, volume_db);
  138. memcpy(*data, &volume, 2);
  139. *len = 2;
  140. break;
  141. case AUDIO_REQUEST_GET_MIN:
  142. (*data)[0] = 0x00; /* -100 dB */
  143. (*data)[1] = 0x9c;
  144. *len = 2;
  145. break;
  146. case AUDIO_REQUEST_GET_MAX:
  147. (*data)[0] = 0x00; /* 0 dB */
  148. (*data)[1] = 0x00;
  149. *len = 2;
  150. break;
  151. case AUDIO_REQUEST_GET_RES:
  152. (*data)[0] = 0x00; /* 1 dB */
  153. (*data)[1] = 0x01;
  154. *len = 2;
  155. break;
  156. default:
  157. USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x in cs 0x%02x\r\n", setup->bRequest, control_selector);
  158. return -1;
  159. }
  160. } else {
  161. switch (setup->bRequest) {
  162. case AUDIO_REQUEST_CUR:
  163. if (setup->bmRequestType & USB_REQUEST_DIR_MASK) {
  164. volume_db = usbd_audio_get_volume(busid, ep, ch);
  165. if (volume_db >= 0) {
  166. volume = volume_db * 256;
  167. } else {
  168. volume = volume_db * 256 + 0x10000;
  169. }
  170. USB_LOG_DBG("Get ep:0x%02x ch:%d vol_hex:0x%04x, vol_db:%d dB\r\n", ep, ch, volume, volume_db);
  171. memcpy(*data, &volume, 2);
  172. *len = 2;
  173. } else {
  174. memcpy(&volume, *data, *len);
  175. if (volume < 0x8000) {
  176. volume_db = volume / 256;
  177. } else {
  178. volume_db = (volume - 0x10000) / 256;
  179. }
  180. USB_LOG_DBG("Set ep:0x%02x ch:%d vol_hex:0x%04x, vol_db:%d dB\r\n", ep, ch, volume, volume_db);
  181. usbd_audio_set_volume(busid, ep, ch, volume_db);
  182. }
  183. break;
  184. case AUDIO_REQUEST_RANGE:
  185. if (setup->bmRequestType & USB_REQUEST_DIR_MASK) {
  186. *((uint16_t *)(*data + 0)) = 1;
  187. *((uint16_t *)(*data + 2)) = 0x9c00; /* MIN -100 dB */
  188. *((uint16_t *)(*data + 4)) = 0x0000; /* MAX 0 dB */
  189. *((uint16_t *)(*data + 6)) = 0x100; /* RES 1 dB */
  190. *len = 8;
  191. } else {
  192. }
  193. break;
  194. default:
  195. //USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x in cs 0x%02x\r\n", setup->bRequest, control_selector);
  196. return -1;
  197. }
  198. }
  199. break;
  200. default:
  201. USB_LOG_WRN("Unhandled Audio Class cs 0x%02x \r\n", control_selector);
  202. return -1;
  203. }
  204. break;
  205. case AUDIO_CONTROL_CLOCK_SOURCE:
  206. switch (control_selector) {
  207. case AUDIO_CS_CONTROL_SAM_FREQ:
  208. switch (setup->bRequest) {
  209. case AUDIO_REQUEST_CUR:
  210. if (setup->bmRequestType & USB_REQUEST_DIR_MASK) {
  211. sampling_freq = usbd_audio_get_sampling_freq(busid, ep);
  212. memcpy(*data, &sampling_freq, 4);
  213. USB_LOG_DBG("Get ep:0x%02x %d Hz\r\n", ep, (int)sampling_freq);
  214. *len = 4;
  215. } else {
  216. memcpy(&sampling_freq, *data, setup->wLength);
  217. USB_LOG_DBG("Set ep:0x%02x %d Hz\r\n", ep, (int)sampling_freq);
  218. usbd_audio_set_sampling_freq(busid, ep, sampling_freq);
  219. }
  220. break;
  221. case AUDIO_REQUEST_RANGE:
  222. if (setup->bmRequestType & USB_REQUEST_DIR_MASK) {
  223. uint8_t *sampling_freq_table = NULL;
  224. uint16_t num;
  225. usbd_audio_get_sampling_freq_table(busid, ep, &sampling_freq_table);
  226. num = (uint16_t)((uint16_t)(sampling_freq_table[1] << 8) | ((uint16_t)sampling_freq_table[0]));
  227. memcpy(*data, sampling_freq_table, (12 * num + 2));
  228. *len = (12 * num + 2);
  229. } else {
  230. }
  231. break;
  232. default:
  233. //USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x in cs 0x%02x\r\n", setup->bRequest, control_selector);
  234. return -1;
  235. }
  236. break;
  237. case AUDIO_CS_CONTROL_CLOCK_VALID:
  238. if (setup->bmRequestType & USB_REQUEST_DIR_MASK) {
  239. (*data)[0] = 1;
  240. *len = 1;
  241. } else {
  242. return -1;
  243. }
  244. break;
  245. default:
  246. //USB_LOG_WRN("Unhandled Audio Class cs 0x%02x \r\n", control_selector);
  247. return -1;
  248. }
  249. break;
  250. default:
  251. break;
  252. }
  253. return 0;
  254. }
  255. static void audio_notify_handler(uint8_t busid, uint8_t event, void *arg)
  256. {
  257. switch (event) {
  258. case USBD_EVENT_RESET:
  259. break;
  260. case USBD_EVENT_SET_INTERFACE: {
  261. struct usb_interface_descriptor *intf = (struct usb_interface_descriptor *)arg;
  262. if (intf->bAlternateSetting) {
  263. usbd_audio_open(busid, intf->bInterfaceNumber);
  264. } else {
  265. usbd_audio_close(busid, intf->bInterfaceNumber);
  266. }
  267. }
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. struct usbd_interface *usbd_audio_init_intf(uint8_t busid,
  274. struct usbd_interface *intf,
  275. uint16_t uac_version,
  276. struct audio_entity_info *table,
  277. uint8_t num)
  278. {
  279. if (uac_version < 0x0200) {
  280. intf->class_interface_handler = audio_class_interface_request_handler;
  281. intf->class_endpoint_handler = audio_class_endpoint_request_handler;
  282. intf->vendor_handler = NULL;
  283. intf->notify_handler = audio_notify_handler;
  284. } else {
  285. intf->class_interface_handler = audio_class_interface_request_handler;
  286. intf->class_endpoint_handler = NULL;
  287. intf->vendor_handler = NULL;
  288. intf->notify_handler = audio_notify_handler;
  289. }
  290. g_usbd_audio[busid].uac_version = uac_version;
  291. g_usbd_audio[busid].table = table;
  292. g_usbd_audio[busid].num = num;
  293. return intf;
  294. }
  295. __WEAK void usbd_audio_set_volume(uint8_t busid, uint8_t ep, uint8_t ch, int volume_db)
  296. {
  297. (void)busid;
  298. (void)ep;
  299. (void)ch;
  300. (void)volume_db;
  301. }
  302. __WEAK int usbd_audio_get_volume(uint8_t busid, uint8_t ep, uint8_t ch)
  303. {
  304. (void)busid;
  305. (void)ep;
  306. (void)ch;
  307. return 0;
  308. }
  309. __WEAK void usbd_audio_set_mute(uint8_t busid, uint8_t ep, uint8_t ch, bool mute)
  310. {
  311. (void)busid;
  312. (void)ep;
  313. (void)ch;
  314. (void)mute;
  315. }
  316. __WEAK bool usbd_audio_get_mute(uint8_t busid, uint8_t ep, uint8_t ch)
  317. {
  318. (void)busid;
  319. (void)ep;
  320. (void)ch;
  321. return 0;
  322. }
  323. __WEAK void usbd_audio_set_sampling_freq(uint8_t busid, uint8_t ep, uint32_t sampling_freq)
  324. {
  325. (void)busid;
  326. (void)ep;
  327. (void)sampling_freq;
  328. }
  329. __WEAK uint32_t usbd_audio_get_sampling_freq(uint8_t busid, uint8_t ep)
  330. {
  331. (void)busid;
  332. (void)ep;
  333. return 0;
  334. }
  335. __WEAK void usbd_audio_get_sampling_freq_table(uint8_t busid, uint8_t ep, uint8_t **sampling_freq_table)
  336. {
  337. (void)busid;
  338. (void)ep;
  339. (void)sampling_freq_table;
  340. }