A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd

Add USB Audio 1.0 support

Original commit credit to Amaury Pouly, Moshe Piekarski
Pushed across the finish line by Dana Conrad

To enable, see setting under General Settings --> System --> USB-DAC.
On devices with few endpoints, this may not work while HID and/or
mass storage is enabled.

Adds new dedicated mixer channel.

setting usb-dac can have values:
- never (0)
- always (1)
- while_charge_only (2)
- while_mass_storage (3)

Relevant devices are DWC2 and ARC usb controller devices. That being:
x1000 Native targets (m3k, erosqnative, q1, others...?),
sansac200, creativezenxfi2, vibe500, ipodmini2g,
ipod4g, creativezenxfi, creativezenxfi3, sansaview, ipodcolor,
creativezenxfistyle, samsungypz5, sansafuzeplus, iriverh10_5gb,
tatungtpj1022, gigabeats, faketarget, samsungyh820, gogearhdd1630, samsungyh925, ipodmini1g, ipodvideo, creativezenmozaic, sonynwze370, creativezen, gogearsa9200, gogearhdd6330, sonynwze360, sansae200, mrobe100, iriverh10, creativezenv, ipodnano1g, samsungyh920

USB Driver-wise, it should be noted that this patch requires some
slight changes:
- proper blocking on control OUT transfers, to make sure the data is
received *before* using it, the usb_core should probably use that too
- drivers can now support interface alternate settings
- drivers can be notified of completion by a new fast handler, which
is called directly from the driver; this is is necessary for
isochronous transfers because going through the usb queue is way too
slow

Designware changes:

- enable for USBOTG_DESIGNWARE
- set maxpacketsize to 1023 for ISO endpoints

Change-Id: I570871884a4e4820b4312b203b07701f06ecacc6

authored by

Dana Conrad and committed by
Solomon Peachy
9ce66e08 af424280

+1867 -35
+50 -6
apps/debug_menu.c
··· 116 116 117 117 #ifdef HAVE_USBSTACK 118 118 #include "usb_core.h" 119 + #ifdef USB_ENABLE_AUDIO 120 + #include "../usbstack/usb_audio.h" 121 + #endif 119 122 #endif 120 123 121 124 #include "talk.h" ··· 2509 2512 } 2510 2513 2511 2514 #ifdef HAVE_USBSTACK 2512 - #if defined(ROCKBOX_HAS_LOGF) && defined(USB_ENABLE_SERIAL) 2513 - static bool toggle_usb_serial(void) 2515 + #if (defined(ROCKBOX_HAS_LOGF) && defined(USB_ENABLE_SERIAL)) 2516 + static bool toggle_usb_core_driver(int driver, char *msg) 2514 2517 { 2515 - bool enabled = !usb_core_driver_enabled(USB_DRIVER_SERIAL); 2518 + bool enabled = !usb_core_driver_enabled(driver); 2516 2519 2517 - usb_core_enable_driver(USB_DRIVER_SERIAL, enabled); 2518 - splashf(HZ, "USB Serial %sabled", enabled ? "en" : "dis"); 2520 + usb_core_enable_driver(driver,enabled); 2521 + splashf(HZ, "%s %s", msg, enabled ? "enabled" : "disabled"); 2519 2522 2520 2523 return false; 2521 2524 } 2522 - #endif 2525 + 2526 + #ifdef USB_ENABLE_SERIAL 2527 + static bool toggle_usb_serial(void) 2528 + { 2529 + return toggle_usb_core_driver(USB_DRIVER_SERIAL, "USB Serial"); 2530 + } 2531 + #endif /* USB_ENABLE_SERIAL */ 2523 2532 #endif 2533 + 2534 + #ifdef USB_ENABLE_AUDIO 2535 + static int dbg_usb_audio_cb(int action, struct gui_synclist *lists) 2536 + { 2537 + (void)lists; 2538 + simplelist_reset_lines(); 2539 + simplelist_addline("%sabled", usb_core_driver_enabled(USB_DRIVER_AUDIO)?"En":"Dis"); 2540 + simplelist_addline("%sPlaying", usb_audio_get_playing()?"":"Not "); 2541 + simplelist_addline("iface: %d alt: %d", usb_audio_get_main_intf(), usb_audio_get_alt_intf()); 2542 + simplelist_addline("out ep: 0x%X in ep: 0x%X", usb_audio_get_out_ep(), usb_audio_get_in_ep()); 2543 + simplelist_addline("Volume: %d", usb_audio_get_cur_volume()); 2544 + simplelist_addline("Playback Frequency: %lu", usb_audio_get_playback_sampling_frequency()); 2545 + simplelist_addline("Buffers filled: %d", usb_audio_get_prebuffering()); 2546 + simplelist_addline("%s", usb_audio_get_underflow()?"UNDERFLOW!":" "); 2547 + simplelist_addline("%s", usb_audio_get_overflow()?"OVERFLOW!":" "); 2548 + simplelist_addline("%s", usb_audio_get_alloc_failed()?"ALLOC FAILED!":" "); 2549 + if (action == ACTION_NONE) 2550 + { 2551 + action = ACTION_REDRAW; 2552 + } 2553 + return action; 2554 + } 2555 + static bool dbg_usb_audio(void) 2556 + { 2557 + struct simplelist_info info; 2558 + simplelist_info_init(&info, "USB Audio", 0, NULL); 2559 + info.scroll_all = true; 2560 + info.action_callback = dbg_usb_audio_cb; 2561 + return simplelist_show_list(&info); 2562 + } 2563 + #endif /* USB_ENABLE_AUDIO */ 2564 + #endif /* HAVE_USBSTACK */ 2524 2565 2525 2566 #if CONFIG_USBOTG == USBOTG_ISP1583 2526 2567 extern int dbg_usb_num_items(void); ··· 2846 2887 #if defined(HAVE_USBSTACK) 2847 2888 #if defined(ROCKBOX_HAS_LOGF) && defined(USB_ENABLE_SERIAL) 2848 2889 {"USB Serial driver (logf)", toggle_usb_serial }, 2890 + #endif 2891 + #if defined(USB_ENABLE_AUDIO) 2892 + {"USB-DAC", dbg_usb_audio}, 2849 2893 #endif 2850 2894 #endif /* HAVE_USBSTACK */ 2851 2895 #ifdef CPU_BOOST_LOGGING
+56
apps/lang/english.lang
··· 10371 10371 </voice> 10372 10372 </phrase> 10373 10373 <phrase> 10374 + id: LANG_USB_DAC 10375 + desc: in settings_menu 10376 + user: core 10377 + <source> 10378 + *: "USB-DAC" 10379 + </source> 10380 + <dest> 10381 + *: "USB-DAC" 10382 + </dest> 10383 + <voice> 10384 + *: "USB-DAC" 10385 + </voice> 10386 + </phrase> 10387 + <phrase> 10388 + id: LANG_WHILE_USB_CHARGE_ONLY 10389 + desc: in settings_menu 10390 + user: core 10391 + <source> 10392 + *: "While In USB Charge-Only Mode" 10393 + </source> 10394 + <dest> 10395 + *: "While In USB Charge-Only Mode" 10396 + </dest> 10397 + <voice> 10398 + *: "While In USB Charge-Only Mode" 10399 + </voice> 10400 + </phrase> 10401 + <phrase> 10402 + id: LANG_WHILE_MASS_STORAGE_USB_ONLY 10403 + desc: in settings_menu 10404 + user: core 10405 + <source> 10406 + *: "While In USB Mass-Storage Mode" 10407 + </source> 10408 + <dest> 10409 + *: "While In USB Mass-Storage Mode" 10410 + </dest> 10411 + <voice> 10412 + *: "While In USB Mass-Storage Mode" 10413 + </voice> 10414 + </phrase> 10415 + <phrase> 10416 + id: LANG_USB_DAC_ACTIVE 10417 + desc: for splash 10418 + user: core 10419 + <source> 10420 + *: "USB-DAC Active" 10421 + </source> 10422 + <dest> 10423 + *: "USB-DAC Active" 10424 + </dest> 10425 + <voice> 10426 + *: "USB-DAC Active" 10427 + </voice> 10428 + </phrase> 10429 + <phrase> 10374 10430 id: LANG_SCROLLBAR_WIDTH 10375 10431 desc: in Settings -> General -> Display -> Status-/Scrollbar 10376 10432 user: core
+6
apps/menus/settings_menu.c
··· 358 358 MENUITEM_SETTING(usb_hid, &global_settings.usb_hid, NULL); 359 359 MENUITEM_SETTING(usb_keypad_mode, &global_settings.usb_keypad_mode, NULL); 360 360 #endif 361 + #ifdef USB_ENABLE_AUDIO 362 + MENUITEM_SETTING(usb_audio, &global_settings.usb_audio, NULL); 363 + #endif 361 364 #if defined(USB_ENABLE_STORAGE) && defined(HAVE_MULTIDRIVE) 362 365 MENUITEM_SETTING(usb_skip_first_drive, &global_settings.usb_skip_first_drive, NULL); 363 366 #endif ··· 453 456 #ifdef USB_ENABLE_HID 454 457 &usb_hid, 455 458 &usb_keypad_mode, 459 + #endif 460 + #ifdef USB_ENABLE_AUDIO 461 + &usb_audio, 456 462 #endif 457 463 #if defined(USB_ENABLE_STORAGE) && defined(HAVE_MULTIDRIVE) 458 464 &usb_skip_first_drive,
+4
apps/settings.h
··· 823 823 int usb_keypad_mode; 824 824 #endif 825 825 826 + #ifdef USB_ENABLE_AUDIO 827 + int usb_audio; 828 + #endif 829 + 826 830 #if defined(USB_ENABLE_STORAGE) && defined(HAVE_MULTIDRIVE) 827 831 bool usb_skip_first_drive; 828 832 #endif
+5
apps/settings_list.c
··· 2315 2315 ), /* CHOICE_SETTING( usb_keypad_mode ) */ 2316 2316 #endif 2317 2317 2318 + #ifdef USB_ENABLE_AUDIO 2319 + CHOICE_SETTING(0, usb_audio, LANG_USB_DAC, 0, "usb-dac", "never,always,while_charge_only,while_mass_storage", usb_set_audio, 4, 2320 + ID2P(LANG_NEVER), ID2P(LANG_ALWAYS), ID2P(LANG_WHILE_USB_CHARGE_ONLY), ID2P(LANG_WHILE_MASS_STORAGE_USB_ONLY)), 2321 + #endif 2322 + 2318 2323 #if defined(USB_ENABLE_STORAGE) && defined(HAVE_MULTIDRIVE) 2319 2324 OFFON_SETTING(0, usb_skip_first_drive, LANG_USB_SKIP_FIRST_DRIVE, false, "usb skip first drive", usb_set_skip_first_drive), 2320 2325 #endif
+3
firmware/SOURCES
··· 935 935 #ifdef USB_ENABLE_SERIAL 936 936 usbstack/usb_serial.c 937 937 #endif 938 + #ifdef USB_ENABLE_AUDIO 939 + usbstack/usb_audio.c 940 + #endif 938 941 #ifdef USB_ENABLE_CHARGING_ONLY 939 942 usbstack/usb_charging_only.c 940 943 #endif
+20 -5
firmware/drivers/usb-designware.c
··· 231 231 else 232 232 { 233 233 DWC_EPCTL(epnum, epdir) &= ~STALL; 234 - DWC_EPCTL(epnum, epdir) |= SD0PID; 234 + DWC_EPCTL(epnum, epdir) |= SETD0PIDEF; 235 235 } 236 236 } 237 237 ··· 668 668 static int usb_dw_configure_ep(int epnum, 669 669 enum usb_dw_epdir epdir, int type, int maxpktsize) 670 670 { 671 - uint32_t epctl = SD0PID|EPTYP(type)|USBAEP|maxpktsize; 671 + uint32_t epctl = SETD0PIDEF|EPTYP(type)|USBAEP|maxpktsize; 672 672 673 673 if (epdir == USB_DW_EPDIR_IN) 674 674 { ··· 1259 1259 * FIFO and raises StatusRecvd | XferCompl. 1260 1260 * 1261 1261 * We do not need or want this -- we've already handled 1262 - * the data phase by this point -- but EP0 is stoppped 1262 + * the data phase by this point -- but EP0 is stopped 1263 1263 * as a side effect of XferCompl, so we need to restart 1264 1264 * it to keep receiving packets. */ 1265 1265 usb_dw_ep0_recv(); ··· 1528 1528 { 1529 1529 //usb_dw_flush_endpoint(ep, dir); 1530 1530 usb_dw_abort_endpoint(ep, dir); 1531 - DWC_EPCTL(ep, dir) |= SD0PID; 1531 + DWC_EPCTL(ep, dir) |= SETD0PIDEF; 1532 1532 } 1533 1533 usb_dw_target_enable_irq(); 1534 1534 } ··· 1606 1606 struct usb_dw_ep* dw_ep = usb_dw_get_ep(ep, epdir); 1607 1607 if (!dw_ep->active) 1608 1608 { 1609 + int maxpktsize = 64; 1610 + if (type == EPTYP_ISOCHRONOUS){ 1611 + maxpktsize = 1023; 1612 + } else { 1613 + maxpktsize = usb_drv_port_speed() ? 512 : 64; 1614 + } 1615 + 1609 1616 if (usb_dw_configure_ep(ep, epdir, type, 1610 - usb_drv_port_speed() ? 512 : 64) >= 0) 1617 + maxpktsize) >= 0) 1611 1618 { 1612 1619 dw_ep->active = true; 1613 1620 request_ep = ep | dir; ··· 1679 1686 usb_dw_control_response(resp, data, length); 1680 1687 usb_dw_target_enable_irq(); 1681 1688 } 1689 + 1690 + int usb_drv_get_frame_number() 1691 + { 1692 + // SOFFN is 14 bits, the least significant 3 appear to be some sort of microframe count. 1693 + // The USB spec says a frame number is 11 bits. This way we get 1 frame per millisecond, 1694 + // just like we're supposed to! 1695 + return (DWC_DSTS >> 11) & 0x7FF; 1696 + }
+8 -5
firmware/export/config.h
··· 1347 1347 #elif (CONFIG_USBOTG == USBOTG_DESIGNWARE) 1348 1348 #define USB_HAS_BULK 1349 1349 #define USB_HAS_INTERRUPT 1350 + #define USB_HAS_ISOCHRONOUS 1350 1351 #elif (CONFIG_USBOTG == USBOTG_ARC) || \ 1351 1352 (CONFIG_USBOTG == USBOTG_JZ4740) || \ 1352 1353 (CONFIG_USBOTG == USBOTG_JZ4760) || \ ··· 1356 1357 (CONFIG_USBOTG == USBOTG_TNETV105) 1357 1358 #define USB_HAS_BULK 1358 1359 #define USB_HAS_INTERRUPT 1360 + #if (CONFIG_USBOTG == USBOTG_ARC) 1361 + #define USB_HAS_ISOCHRONOUS 1362 + #endif 1359 1363 #define USB_LEGACY_CONTROL_API 1360 1364 #elif defined(CPU_TCC780X) 1361 1365 #define USB_HAS_BULK ··· 1365 1369 #define USB_LEGACY_CONTROL_API 1366 1370 //#define USB_HAS_INTERRUPT -- seems to be broken 1367 1371 #endif /* CONFIG_USBOTG */ 1368 - 1369 - #if (CONFIG_USBOTG == USBOTG_ARC) || \ 1370 - (CONFIG_USBOTG == USBOTG_AS3525) 1371 - #define USB_HAS_ISOCHRONOUS 1372 - #endif 1373 1372 1374 1373 /* define the class drivers to enable */ 1375 1374 #ifdef BOOTLOADER ··· 1396 1395 #else 1397 1396 #define USB_ENABLE_CHARGING_ONLY 1398 1397 #endif 1398 + #endif 1399 + 1400 + #ifdef USB_HAS_ISOCHRONOUS 1401 + #define USB_ENABLE_AUDIO 1399 1402 #endif 1400 1403 1401 1404 #endif /* BOOTLOADER */
+3
firmware/export/pcm_mixer.h
··· 72 72 enum pcm_mixer_channel 73 73 { 74 74 PCM_MIXER_CHAN_PLAYBACK = 0, 75 + #ifdef USB_ENABLE_AUDIO 76 + PCM_MIXER_CHAN_USBAUDIO, 77 + #endif 75 78 PCM_MIXER_CHAN_VOICE, 76 79 #ifndef HAVE_HARDWARE_BEEP 77 80 PCM_MIXER_CHAN_BEEP,
+2 -1
firmware/export/usb-designware.h
··· 200 200 #define DWC_DOEPCTL(x) (*((REG32_PTR_T)(OTGBASE + 0xb00 + 0x20*(x)))) 201 201 #define EPENA (1<<31) 202 202 #define EPDIS (1<<30) 203 - #define SD0PID (1<<28) 203 + #define SETD1PIDOF (1<<29) 204 + #define SETD0PIDEF (1<<28) 204 205 #define SNAK (1<<27) 205 206 #define CNAK (1<<26) 206 207 #define DTXFNUM(x) ((x)<<22)
+7
firmware/export/usb.h
··· 168 168 #ifdef USB_ENABLE_HID 169 169 USB_DRIVER_HID, 170 170 #endif 171 + #ifdef USB_ENABLE_AUDIO 172 + USB_DRIVER_AUDIO, 173 + #endif 171 174 USB_NUM_DRIVERS 172 175 }; 173 176 ··· 254 257 #ifdef USB_ENABLE_HID 255 258 /* enable or disable the HID driver */ 256 259 void usb_set_hid(bool enable); 260 + #endif 261 + 262 + #ifdef USB_ENABLE_AUDIO 263 + void usb_set_audio(int value); 257 264 #endif 258 265 259 266 #if defined(USB_ENABLE_STORAGE) && defined(HAVE_MULTIDRIVE)
+6
firmware/export/usb_ch9.h
··· 339 339 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */ 340 340 #define USB_ENDPOINT_DIR_MASK 0x80 341 341 342 + #define USB_ENDPOINT_SYNCTYPE_MASK 0x0c /* in bmAttributes */ 343 + #define USB_ENDPOINT_SYNC_NONE (0 << 2) 344 + #define USB_ENDPOINT_SYNC_ASYNC (1 << 2) 345 + #define USB_ENDPOINT_SYNC_ADAPTIVE (2 << 2) 346 + #define USB_ENDPOINT_SYNC_SYNC (3 << 2) 347 + 342 348 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */ 343 349 #define USB_ENDPOINT_XFER_CONTROL 0 344 350 #define USB_ENDPOINT_XFER_ISOC 1
+9
firmware/export/usb_drv.h
··· 74 74 bool usb_drv_stalled(int endpoint,bool in); 75 75 int usb_drv_send(int endpoint, void* ptr, int length); 76 76 int usb_drv_send_nonblocking(int endpoint, void* ptr, int length); 77 + int usb_drv_recv_blocking(int endpoint, void* ptr, int length); 77 78 int usb_drv_recv_nonblocking(int endpoint, void* ptr, int length); 78 79 void usb_drv_control_response(enum usb_control_response resp, 79 80 void* data, int length); ··· 86 87 bool usb_drv_connected(void); 87 88 int usb_drv_request_endpoint(int type, int dir); 88 89 void usb_drv_release_endpoint(int ep); 90 + #ifdef USB_HAS_ISOCHRONOUS 91 + /* returns the last received frame number (the 11-bit number contained in the last SOF): 92 + * - full-speed: the host sends one SOF every 1ms (so 1000 SOF/s) 93 + * - high-speed: the hosts sends one SOF every 125us but each consecutive 8 SOF have the same frame 94 + * number 95 + * thus in all mode, the frame number can be interpreted as the current millisecond *in USB time*. */ 96 + int usb_drv_get_frame_number(void); 97 + #endif 89 98 90 99 /* USB_STRING_INITIALIZER(u"Example String") */ 91 100 #define USB_STRING_INITIALIZER(S) { \
+14 -5
firmware/target/arm/usb-drv-arc.c
··· 599 599 return prime_transfer(EP_NUM(endpoint), ptr, length, false, false); 600 600 } 601 601 602 + int usb_drv_recv_blocking(int endpoint, void* ptr, int length) 603 + { 604 + return prime_transfer(EP_NUM(endpoint), ptr, length, false, true); 605 + } 606 + 602 607 int usb_drv_port_speed(void) 603 608 { 604 609 return (REG_PORTSC1 & 0x08000000) ? 1 : 0; 610 + } 611 + 612 + int usb_drv_get_frame_number(void) 613 + { 614 + /* the lower 3 bits store the microframe (in HS mode), discard them */ 615 + return (REG_FRINDEX & USB_FRINDEX_MASKS) >> 3; 605 616 } 606 617 607 618 bool usb_drv_connected(void) ··· 970 981 /* manual: 32.14.4.1 Queue Head Initialization */ 971 982 static void init_queue_heads(void) 972 983 { 973 - /* FIXME the packetsize for isochronous transfers is 1023 : 1024 but 974 - * the current code only support one type of packet size so we restrict 975 - * isochronous packet size for now also */ 976 984 int packetsize = (usb_drv_port_speed() ? 512 : 64); 985 + int isopacketsize = (usb_drv_port_speed() ? 1024 : 1024); 977 986 int i; 978 987 979 988 /* TODO: this should take ep_allocation into account */ ··· 982 991 /* OUT */ 983 992 if(endpoints[i].type[DIR_OUT] == USB_ENDPOINT_XFER_ISOC) 984 993 /* FIXME: we can adjust the number of packets per frame, currently use one */ 985 - qh_array[i*2].max_pkt_length = packetsize << QH_MAX_PKT_LEN_POS | QH_ZLT_SEL | 1 << QH_MULT_POS; 994 + qh_array[i*2].max_pkt_length = isopacketsize << QH_MAX_PKT_LEN_POS | QH_ZLT_SEL | 1 << QH_MULT_POS; 986 995 else 987 996 qh_array[i*2].max_pkt_length = packetsize << QH_MAX_PKT_LEN_POS | QH_ZLT_SEL; 988 997 ··· 991 1000 /* IN */ 992 1001 if(endpoints[i].type[DIR_IN] == USB_ENDPOINT_XFER_ISOC) 993 1002 /* FIXME: we can adjust the number of packets per frame, currently use one */ 994 - qh_array[i*2+1].max_pkt_length = packetsize << QH_MAX_PKT_LEN_POS | QH_ZLT_SEL | 1 << QH_MULT_POS; 1003 + qh_array[i*2+1].max_pkt_length = isopacketsize << QH_MAX_PKT_LEN_POS | QH_ZLT_SEL | 1 << QH_MULT_POS; 995 1004 else 996 1005 qh_array[i*2+1].max_pkt_length = packetsize << QH_MAX_PKT_LEN_POS | QH_ZLT_SEL; 997 1006
+20
firmware/usb.c
··· 97 97 #ifdef USB_ENABLE_HID 98 98 static bool usb_hid = true; 99 99 #endif 100 + #ifdef USB_ENABLE_AUDIO 101 + static int usb_audio = 0; 102 + #endif 100 103 101 104 #ifdef USB_FULL_INIT 102 105 static bool usb_host_present = false; ··· 194 197 195 198 static inline bool usb_configure_drivers(int for_state) 196 199 { 200 + #ifdef USB_ENABLE_AUDIO 201 + // FIXME: doesn't seem to get set when loaded at boot... 202 + usb_audio = global_settings.usb_audio; 203 + #endif 197 204 switch(for_state) 198 205 { 199 206 case USB_POWERED: ··· 207 214 usb_core_enable_driver(USB_DRIVER_HID, true); 208 215 #endif /* USB_ENABLE_CHARGING_ONLY */ 209 216 #endif /* USB_ENABLE_HID */ 217 + #ifdef USB_ENABLE_AUDIO 218 + usb_core_enable_driver(USB_DRIVER_AUDIO, (usb_audio == 1) || (usb_audio == 2)); // while "always" or "only in charge-only mode" 219 + #endif /* USB_ENABLE_AUDIO */ 210 220 211 221 #ifdef USB_ENABLE_CHARGING_ONLY 212 222 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, true); ··· 224 234 #ifdef USB_ENABLE_HID 225 235 usb_core_enable_driver(USB_DRIVER_HID, usb_hid); 226 236 #endif 237 + #ifdef USB_ENABLE_AUDIO 238 + usb_core_enable_driver(USB_DRIVER_AUDIO, (usb_audio == 1) || (usb_audio == 3)); // while "always" or "only in mass-storage mode" 239 + #endif /* USB_ENABLE_AUDIO */ 227 240 #ifdef USB_ENABLE_CHARGING_ONLY 228 241 usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY, false); 229 242 #endif ··· 844 857 usb_core_enable_driver(USB_DRIVER_HID, usb_hid); 845 858 } 846 859 #endif /* USB_ENABLE_HID */ 860 + 861 + #ifdef USB_ENABLE_AUDIO 862 + void usb_set_audio(int value) 863 + { 864 + usb_audio = value; 865 + } 866 + #endif /* USB_ENABLE_AUDIO */ 847 867 848 868 #ifdef HAVE_USB_POWER 849 869 bool usb_powered_only(void)
+1105
firmware/usbstack/usb_audio.c
··· 1 + /*************************************************************************** 2 + * __________ __ ___. 3 + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 + * \/ \/ \/ \/ \/ 8 + * $Id: $ 9 + * 10 + * Copyright (C) 2010 by Amaury Pouly 11 + * 12 + * All files in this archive are subject to the GNU General Public License. 13 + * See the file COPYING in the source tree root for full license agreement. 14 + * 15 + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 16 + * KIND, either express or implied. 17 + * 18 + ****************************************************************************/ 19 + #include "string.h" 20 + #include "system.h" 21 + #include "usb_core.h" 22 + #include "usb_drv.h" 23 + #include "kernel.h" 24 + #include "sound.h" 25 + #include "usb_class_driver.h" 26 + #include "usb_audio_def.h" 27 + #include "pcm_sampr.h" 28 + #include "audio.h" 29 + #include "sound.h" 30 + #include "stdlib.h" 31 + #include "fixedpoint.h" 32 + #include "misc.h" 33 + #include "settings.h" 34 + #include "core_alloc.h" 35 + #include "pcm_mixer.h" 36 + 37 + #define LOGF_ENABLE 38 + #include "logf.h" 39 + 40 + /* Audio Control Interface */ 41 + static struct usb_interface_descriptor 42 + ac_interface = 43 + { 44 + .bLength = sizeof(struct usb_interface_descriptor), 45 + .bDescriptorType = USB_DT_INTERFACE, 46 + .bInterfaceNumber = 0, 47 + .bAlternateSetting = 0, 48 + .bNumEndpoints = 0, 49 + .bInterfaceClass = USB_CLASS_AUDIO, 50 + .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL, 51 + .bInterfaceProtocol = 0, 52 + .iInterface = 0 53 + }; 54 + 55 + /* Audio Control Terminals/Units*/ 56 + static struct usb_ac_header ac_header = 57 + { 58 + .bLength = USB_AC_SIZEOF_HEADER(1), /* one interface */ 59 + .bDescriptorType = USB_DT_CS_INTERFACE, 60 + .bDescriptorSubType = USB_AC_HEADER, 61 + .bcdADC = 0x0100, 62 + .wTotalLength = 0, /* fill later */ 63 + .bInCollection = 1, /* one interface */ 64 + .baInterfaceNr = {0}, /* fill later */ 65 + }; 66 + 67 + enum 68 + { 69 + AC_PLAYBACK_INPUT_TERMINAL_ID = 1, 70 + AC_PLAYBACK_FEATURE_ID, 71 + AC_PLAYBACK_OUTPUT_TERMINAL_ID, 72 + }; 73 + 74 + static struct usb_ac_input_terminal ac_playback_input = 75 + { 76 + .bLength = sizeof(struct usb_ac_input_terminal), 77 + .bDescriptorType = USB_DT_CS_INTERFACE, 78 + .bDescriptorSubType = USB_AC_INPUT_TERMINAL, 79 + .bTerminalId = AC_PLAYBACK_INPUT_TERMINAL_ID, 80 + .wTerminalType = USB_AC_TERMINAL_STREAMING, 81 + .bAssocTerminal = 0, 82 + .bNrChannels = 2, 83 + .wChannelConfig = USB_AC_CHANNELS_LEFT_RIGHT_FRONT, 84 + .iChannelNames = 0, 85 + .iTerminal = 0, 86 + }; 87 + 88 + static struct usb_ac_output_terminal ac_playback_output = 89 + { 90 + .bLength = sizeof(struct usb_ac_output_terminal), 91 + .bDescriptorType = USB_DT_CS_INTERFACE, 92 + .bDescriptorSubType = USB_AC_OUTPUT_TERMINAL, 93 + .bTerminalId = AC_PLAYBACK_OUTPUT_TERMINAL_ID, 94 + .wTerminalType = USB_AC_OUTPUT_TERMINAL_HEADPHONES, 95 + .bAssocTerminal = 0, 96 + .bSourceId = AC_PLAYBACK_FEATURE_ID, 97 + .iTerminal = 0, 98 + }; 99 + 100 + /* Feature Unit with 2 logical channels and 1 byte(8 bits) per control */ 101 + DEFINE_USB_AC_FEATURE_UNIT(8, 2) 102 + 103 + static struct usb_ac_feature_unit_8_2 ac_playback_feature = 104 + { 105 + .bLength = sizeof(struct usb_ac_feature_unit_8_2), 106 + .bDescriptorType = USB_DT_CS_INTERFACE, 107 + .bDescriptorSubType = USB_AC_FEATURE_UNIT, 108 + .bUnitId = AC_PLAYBACK_FEATURE_ID, 109 + .bSourceId = AC_PLAYBACK_INPUT_TERMINAL_ID, 110 + .bControlSize = 1, /* by definition */ 111 + .bmaControls = { 112 + [0] = USB_AC_FU_MUTE | USB_AC_FU_VOLUME, 113 + [1] = 0, 114 + [2] = 0 115 + }, 116 + .iFeature = 0 117 + }; 118 + 119 + /* Audio Streaming Interface */ 120 + /* Alternative: no streaming */ 121 + static struct usb_interface_descriptor 122 + as_interface_alt_idle_playback = 123 + { 124 + .bLength = sizeof(struct usb_interface_descriptor), 125 + .bDescriptorType = USB_DT_INTERFACE, 126 + .bInterfaceNumber = 0, 127 + .bAlternateSetting = 0, 128 + .bNumEndpoints = 0, 129 + .bInterfaceClass = USB_CLASS_AUDIO, 130 + .bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING, 131 + .bInterfaceProtocol = 0, 132 + .iInterface = 0 133 + }; 134 + 135 + /* Alternative: output streaming */ 136 + static struct usb_interface_descriptor 137 + as_interface_alt_playback = 138 + { 139 + .bLength = sizeof(struct usb_interface_descriptor), 140 + .bDescriptorType = USB_DT_INTERFACE, 141 + .bInterfaceNumber = 0, 142 + .bAlternateSetting = 1, 143 + .bNumEndpoints = 1, 144 + .bInterfaceClass = USB_CLASS_AUDIO, 145 + .bInterfaceSubClass = USB_SUBCLASS_AUDIO_STREAMING, 146 + .bInterfaceProtocol = 0, 147 + .iInterface = 0 148 + }; 149 + 150 + /* Class Specific Audio Streaming Interface */ 151 + static struct usb_as_interface 152 + as_playback_cs_interface = 153 + { 154 + .bLength = sizeof(struct usb_as_interface), 155 + .bDescriptorType = USB_DT_CS_INTERFACE, 156 + .bDescriptorSubType = USB_AS_GENERAL, 157 + .bTerminalLink = AC_PLAYBACK_INPUT_TERMINAL_ID, 158 + .bDelay = 1, 159 + .wFormatTag = USB_AS_FORMAT_TYPE_I_PCM 160 + }; 161 + 162 + static struct usb_as_format_type_i_discrete 163 + as_playback_format_type_i = 164 + { 165 + .bLength = USB_AS_SIZEOF_FORMAT_TYPE_I_DISCRETE((HW_FREQ_44+1)), 166 + .bDescriptorType = USB_DT_CS_INTERFACE, 167 + .bDescriptorSubType = USB_AS_FORMAT_TYPE, 168 + .bFormatType = USB_AS_FORMAT_TYPE_I, 169 + .bNrChannels = 2, /* Stereo */ 170 + .bSubframeSize = 2, /* 2 bytes per sample */ 171 + .bBitResolution = 16, /* all 16-bits are used */ 172 + .bSamFreqType = (HW_FREQ_44+1), 173 + .tSamFreq = { 174 + // only values 44.1k and higher (array is in descending order) 175 + [0 ... HW_FREQ_44 ] = {0}, /* filled later */ 176 + } 177 + }; 178 + 179 + /* 180 + * TODO: 181 + * It appears that "Adaptive" sync mode means it it the device's duty 182 + * to adapt its consumption rate of data to whatever the host sends, which 183 + * has the possibility to cause trouble in underflows/overflows, etc. 184 + * In practice, this is probably not a large concern, as we have a fairly large 185 + * amount of buffering in the PCM system. 186 + * An improvement may be to use "Asynchronous", but this is more complicated 187 + * due to the need to inform the host about how fast the device will consume 188 + * the data. 189 + * So implementation of "Asynchronous" mode will be left for a later improvement. 190 + */ 191 + static struct usb_iso_audio_endpoint_descriptor 192 + out_iso_ep = 193 + { 194 + .bLength = sizeof(struct usb_iso_audio_endpoint_descriptor), 195 + .bDescriptorType = USB_DT_ENDPOINT, 196 + .bEndpointAddress = USB_DIR_OUT, /* filled later */ 197 + .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE, 198 + .wMaxPacketSize = 0, /* filled later */ 199 + .bInterval = 0, /* filled later */ 200 + .bRefresh = 0, 201 + .bSynchAddress = 0 /* filled later */ 202 + }; 203 + 204 + static struct usb_as_iso_endpoint 205 + as_out_iso_ep = 206 + { 207 + .bLength = sizeof(struct usb_as_iso_endpoint), 208 + .bDescriptorType = USB_DT_CS_ENDPOINT, 209 + .bDescriptorSubType = USB_AS_EP_GENERAL, 210 + .bmAttributes = USB_AS_EP_CS_SAMPLING_FREQ_CTL, 211 + .bLockDelayUnits = 0, /* undefined */ 212 + .wLockDelay = 0 /* undefined */ 213 + }; 214 + 215 + static const struct usb_descriptor_header* const ac_cs_descriptors_list[] = 216 + { 217 + (struct usb_descriptor_header *) &ac_header, 218 + (struct usb_descriptor_header *) &ac_playback_input, 219 + (struct usb_descriptor_header *) &ac_playback_output, 220 + (struct usb_descriptor_header *) &ac_playback_feature 221 + }; 222 + 223 + #define AC_CS_DESCRIPTORS_LIST_SIZE (sizeof(ac_cs_descriptors_list)/sizeof(ac_cs_descriptors_list[0])) 224 + 225 + static const struct usb_descriptor_header* const usb_descriptors_list[] = 226 + { 227 + /* Audio Control */ 228 + (struct usb_descriptor_header *) &ac_interface, 229 + (struct usb_descriptor_header *) &ac_header, 230 + (struct usb_descriptor_header *) &ac_playback_input, 231 + (struct usb_descriptor_header *) &ac_playback_feature, 232 + (struct usb_descriptor_header *) &ac_playback_output, 233 + /* Audio Streaming */ 234 + /* Idle Playback */ 235 + (struct usb_descriptor_header *) &as_interface_alt_idle_playback, 236 + /* Playback */ 237 + (struct usb_descriptor_header *) &as_interface_alt_playback, 238 + (struct usb_descriptor_header *) &as_playback_cs_interface, 239 + (struct usb_descriptor_header *) &as_playback_format_type_i, 240 + (struct usb_descriptor_header *) &out_iso_ep, 241 + (struct usb_descriptor_header *) &as_out_iso_ep, 242 + }; 243 + 244 + #define USB_DESCRIPTORS_LIST_SIZE (sizeof(usb_descriptors_list)/sizeof(usb_descriptors_list[0])) 245 + 246 + static int usb_interface; /* first interface */ 247 + static int usb_as_playback_intf_alt; /* playback streaming interface alternate setting */ 248 + 249 + static int as_playback_freq_idx; /* audio playback streaming frequency index (in hw_freq_sampr) */ 250 + 251 + static int out_iso_ep_adr; /* output isochronous endpoint */ 252 + static int in_iso_ep_adr; /* input isochronous endpoint */ 253 + 254 + /* small buffer used for control transfers */ 255 + static unsigned char usb_buffer[128] USB_DEVBSS_ATTR; 256 + 257 + /* number of buffers: 2 is double-buffering (one for usb, one for playback), 258 + * 3 is triple-buffering (one for usb, one for playback, one for queuing), ... */ 259 + 260 + /* Samples come in (maximum) 1023 byte chunks. Samples are also 16 bits per channel per sample. 261 + * 262 + * One buffer holds (1023 / (2Bx2ch)) = 255 (rounded down) samples 263 + * So the _maximum_ play time per buffer is (255 / sps). 264 + * For 44100 Hz: 5.7 mS 265 + * For 48000 Hz: 5.3 mS 266 + * For 192000 Hz: 1.3 mS 267 + * 268 + * From testing on MacOS (likely to be the toughest customer...) on Designware driver 269 + * we get data every Frame (so, every millisecond). 270 + * 271 + * If we get data every millisecond, we need 1mS to transfer 1.3mS of playback 272 + * in order to sustain 192 kHz playback! 273 + * At 44.1 kHz, the requirements are much less - 1mS of data transfer for 5.7mS of playback 274 + * At 48 kHz, 1mS can transfer 5.3mS of playback. 275 + * 276 + * It appears that this is "maximum", but we more likely get "enough for 1mS" every millisecond. 277 + * 278 + * Working backwards: 279 + * 44100 Hz: 45 samples transferred every frame (*2ch * 2bytes) = 180 bytes every frame 280 + * 48000 Hz: 48 samples transferred every frame (*2ch * 2bytes) = 192 bytes every frame 281 + * 192000 Hz: *2ch *2bytes = 768 bytes every frame 282 + * 283 + * We appear to be more limited by our PCM system's need to gobble up data at startup. 284 + * This may actually, contrary to intuition, make us need a higher number of buffers 285 + * for _lower_ sample rates, as we will need more buffers' worth of data up-front due to 286 + * lower amounts of data in each USB frame (assuming the mixer wants the same amount of data upfront 287 + * regardless of sample rate). 288 + * 289 + * Making the executive decision to only export frequencies 44.1k+. 290 + */ 291 + #define NR_BUFFERS 32 292 + #define MINIMUM_BUFFERS_QUEUED 16 293 + /* size of each buffer: must be smaller than 1023 (max isochronous packet size) */ 294 + #define BUFFER_SIZE 1023 295 + /* make sure each buffer size is actually a multiple of 32 bytes to avoid any 296 + * issue with strange alignements */ 297 + #define REAL_BUF_SIZE ALIGN_UP(BUFFER_SIZE, 32) 298 + 299 + bool alloc_failed = false; 300 + bool usb_audio_playing = false; 301 + int tmp_saved_vol; 302 + 303 + /* buffers used for usb, queuing and playback */ 304 + static unsigned char *rx_buffer; 305 + int rx_buffer_handle; 306 + /* buffer size */ 307 + static int rx_buf_size[NR_BUFFERS]; 308 + /* index of the next buffer to play */ 309 + static int rx_play_idx; 310 + /* index of the next buffer to fill */ 311 + static int rx_usb_idx; 312 + /* playback underflowed ? */ 313 + bool playback_audio_underflow; 314 + /* usb overflow ? */ 315 + bool usb_rx_overflow; 316 + 317 + /* Schematic view of the RX situation: 318 + * (in case NR_BUFFERS = 4) 319 + * 320 + * +--------+ +--------+ +--------+ +--------+ 321 + * | | | | | | | | 322 + * | buf[0] | ---> | buf[1] | ---> | buf[2] | ---> | buf[3] | ---> (back to buf[0]) 323 + * | | | | | | | | 324 + * +--------+ +--------+ +--------+ +--------+ 325 + * ^ ^ ^ ^ 326 + * | | | | 327 + * rx_play_idx (buffer rx_usb_idx (empty buffer) 328 + * (buffer being filled) (buffer being 329 + * played) filled) 330 + * 331 + * Error handling: 332 + * in the RX situation, there are two possible errors 333 + * - playback underflow: playback wants more data but we don't have any to 334 + * provide, so we have to stop audio and wait for some prebuffering before 335 + * starting again 336 + * - usb overflow: usb wants to send more data but don't have any more free buffers, 337 + * so we have to pause usb reception and wait for some playback buffer to become 338 + * free again 339 + */ 340 + 341 + /* USB Audio encodes frequencies with 3 bytes... */ 342 + static void encode3(uint8_t arr[3], unsigned long freq) 343 + { 344 + /* ugly */ 345 + arr[0] = freq & 0xff; 346 + arr[1] = (freq >> 8) & 0xff; 347 + arr[2] = (freq >> 16) & 0xff; 348 + } 349 + 350 + static unsigned long decode3(uint8_t arr[3]) 351 + { 352 + return arr[0] | (arr[1] << 8) | (arr[2] << 16); 353 + } 354 + 355 + static void set_playback_sampling_frequency(unsigned long f) 356 + { 357 + // only values 44.1k and higher (array is in descending order) 358 + for(int i = 0; i <= HW_FREQ_44; i++) 359 + { 360 + /* compare errors */ 361 + int err = abs((long)hw_freq_sampr[i] - (long)f); 362 + int best_err = abs((long)hw_freq_sampr[as_playback_freq_idx] - (long)f); 363 + if(err < best_err) 364 + as_playback_freq_idx = i; 365 + } 366 + 367 + logf("usbaudio: set playback sampling frequency to %lu Hz for a requested %lu Hz", 368 + hw_freq_sampr[as_playback_freq_idx], f); 369 + 370 + mixer_set_frequency(hw_freq_sampr[as_playback_freq_idx]); 371 + pcm_apply_settings(); 372 + } 373 + 374 + unsigned long usb_audio_get_playback_sampling_frequency(void) 375 + { 376 + logf("usbaudio: get playback sampl freq %lu Hz", 377 + hw_freq_sampr[as_playback_freq_idx]); 378 + return hw_freq_sampr[as_playback_freq_idx]; 379 + } 380 + 381 + void usb_audio_init(void) 382 + { 383 + unsigned int i; 384 + /* initialized tSamFreq array */ 385 + logf("usbaudio: supported frequencies"); 386 + // only values 44.1k and higher (array is in descending order) 387 + for(i = 0; i <= HW_FREQ_44; i++) 388 + { 389 + logf("usbaudio: %lu Hz", hw_freq_sampr[i]); 390 + encode3(as_playback_format_type_i.tSamFreq[i], hw_freq_sampr[i]); 391 + } 392 + } 393 + 394 + int usb_audio_request_buf(void) 395 + { 396 + 397 + // stop playback first thing 398 + audio_stop(); 399 + 400 + // attempt to allocate the receive buffers 401 + rx_buffer_handle = core_alloc(NR_BUFFERS * REAL_BUF_SIZE); 402 + if (rx_buffer_handle < 0) 403 + { 404 + alloc_failed = true; 405 + return -1; 406 + } 407 + else 408 + { 409 + alloc_failed = false; 410 + 411 + // "pin" the allocation so that the core does not move it in memory 412 + core_pin(rx_buffer_handle); 413 + 414 + // get the pointer to the actual buffer location 415 + rx_buffer = core_get_data(rx_buffer_handle); 416 + } 417 + // logf("usbaudio: got buffer"); 418 + return 0; 419 + } 420 + 421 + void usb_audio_free_buf(void) 422 + { 423 + // logf("usbaudio: free buffer"); 424 + rx_buffer_handle = core_free(rx_buffer_handle); 425 + rx_buffer = NULL; 426 + } 427 + 428 + int usb_audio_request_endpoints(struct usb_class_driver *drv) 429 + { 430 + // make sure we can get the buffers first... 431 + // return -1 if the allocation _failed_ 432 + if (usb_audio_request_buf()) 433 + return -1; 434 + 435 + out_iso_ep_adr = usb_core_request_endpoint(USB_ENDPOINT_XFER_ISOC, USB_DIR_OUT, drv); 436 + if(out_iso_ep_adr < 0) 437 + { 438 + logf("usbaudio: cannot get an out iso endpoint"); 439 + return -1; 440 + } 441 + 442 + in_iso_ep_adr = usb_core_request_endpoint(USB_ENDPOINT_XFER_ISOC, USB_DIR_IN, drv); 443 + if(in_iso_ep_adr < 0) 444 + { 445 + usb_core_release_endpoint(out_iso_ep_adr); 446 + logf("usbaudio: cannot get an out iso endpoint"); 447 + return -1; 448 + } 449 + 450 + logf("usbaudio: iso out ep is 0x%x, in ep is 0x%x", out_iso_ep_adr, in_iso_ep_adr); 451 + 452 + out_iso_ep.bEndpointAddress = out_iso_ep_adr; 453 + out_iso_ep.bSynchAddress = 0; 454 + 455 + return 0; 456 + } 457 + 458 + unsigned int usb_audio_get_out_ep(void) 459 + { 460 + return out_iso_ep_adr; 461 + } 462 + 463 + unsigned int usb_audio_get_in_ep(void) 464 + { 465 + return in_iso_ep_adr; 466 + } 467 + 468 + int usb_audio_set_first_interface(int interface) 469 + { 470 + usb_interface = interface; 471 + logf("usbaudio: usb_interface=%d", usb_interface); 472 + return interface + 2; /* Audio Control and Audio Streaming */ 473 + } 474 + 475 + int usb_audio_get_config_descriptor(unsigned char *dest, int max_packet_size) 476 + { 477 + (void)max_packet_size; 478 + unsigned int i; 479 + unsigned char *orig_dest = dest; 480 + 481 + // logf("get config descriptors"); 482 + 483 + /** Configuration */ 484 + 485 + /* header */ 486 + ac_header.baInterfaceNr[0] = usb_interface + 1; 487 + 488 + /* audio control interface */ 489 + ac_interface.bInterfaceNumber = usb_interface; 490 + 491 + /* compute total size of AC headers*/ 492 + ac_header.wTotalLength = 0; 493 + for(i = 0; i < AC_CS_DESCRIPTORS_LIST_SIZE; i++) 494 + ac_header.wTotalLength += ac_cs_descriptors_list[i]->bLength; 495 + 496 + /* audio streaming */ 497 + as_interface_alt_idle_playback.bInterfaceNumber = usb_interface + 1; 498 + as_interface_alt_playback.bInterfaceNumber = usb_interface + 1; 499 + 500 + /* endpoints */ 501 + out_iso_ep.wMaxPacketSize = 1023; /* one micro-frame per transaction */ 502 + /** Endpoint Interval calculation: 503 + * typically sampling frequency is 44100 Hz and top is 192000 Hz, which 504 + * account for typical 44100*2(stereo)*2(16-bit) ~= 180 kB/s 505 + * and top 770 kB/s. Since there are 1000 frames per seconds and maximum 506 + * packet size is set to 1023, one transaction per frame is good enough 507 + * for over 1 MB/s. At high-speed, add 3 to this value because there are 508 + * 8 = 2^3 micro-frames per frame. 509 + * Recall that actual is 2^(bInterval - 1) */ 510 + out_iso_ep.bInterval = usb_drv_port_speed() ? 4 : 1; 511 + 512 + /** Packing */ 513 + for(i = 0; i < USB_DESCRIPTORS_LIST_SIZE; i++) 514 + { 515 + memcpy(dest, usb_descriptors_list[i], usb_descriptors_list[i]->bLength); 516 + dest += usb_descriptors_list[i]->bLength; 517 + } 518 + 519 + return dest - orig_dest; 520 + } 521 + 522 + static void playback_audio_get_more(const void **start, size_t *size) 523 + { 524 + /* if there are no more filled buffers, playback has just underflowed */ 525 + if(rx_play_idx == rx_usb_idx) 526 + { 527 + logf("usbaudio: playback underflow"); 528 + playback_audio_underflow = true; 529 + *start = NULL; 530 + *size = 0; 531 + return; 532 + } 533 + /* give buffer and advance */ 534 + logf("usbaudio: buf adv"); 535 + *start = rx_buffer + (rx_play_idx * REAL_BUF_SIZE); 536 + *size = rx_buf_size[rx_play_idx]; 537 + rx_play_idx = (rx_play_idx + 1) % NR_BUFFERS; 538 + /* if usb RX buffers had overflowed, we can start to receive again 539 + * guard against IRQ to avoid race with completion usb completion (although 540 + * this function is probably running in IRQ context anyway) */ 541 + int oldlevel = disable_irq_save(); 542 + if(usb_rx_overflow) 543 + { 544 + logf("usbaudio: recover usb rx overflow"); 545 + usb_rx_overflow = false; 546 + usb_drv_recv_nonblocking(out_iso_ep_adr, rx_buffer + (rx_usb_idx * REAL_BUF_SIZE), BUFFER_SIZE); 547 + } 548 + restore_irq(oldlevel); 549 + } 550 + 551 + static void usb_audio_start_playback(void) 552 + { 553 + usb_audio_playing = true; 554 + usb_rx_overflow = false; 555 + playback_audio_underflow = true; 556 + rx_play_idx = 0; 557 + rx_usb_idx = 0; 558 + 559 + // TODO: implement recording from the USB stream 560 + #if (INPUT_SRC_CAPS != 0) 561 + audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK); 562 + audio_set_output_source(AUDIO_SRC_PLAYBACK); 563 + #endif 564 + logf("usbaudio: start playback at %lu Hz", hw_freq_sampr[as_playback_freq_idx]); 565 + mixer_set_frequency(hw_freq_sampr[as_playback_freq_idx]); 566 + pcm_apply_settings(); 567 + mixer_channel_set_amplitude(PCM_MIXER_CHAN_USBAUDIO, MIX_AMP_UNITY); 568 + usb_drv_recv_nonblocking(out_iso_ep_adr, rx_buffer + (rx_usb_idx * REAL_BUF_SIZE), BUFFER_SIZE); 569 + } 570 + 571 + static void usb_audio_stop_playback(void) 572 + { 573 + // logf("usbaudio: stop playback"); 574 + if(usb_audio_playing) 575 + { 576 + mixer_channel_stop(PCM_MIXER_CHAN_USBAUDIO); 577 + usb_audio_playing = false; 578 + } 579 + } 580 + 581 + int usb_audio_set_interface(int intf, int alt) 582 + { 583 + if(intf == usb_interface) 584 + { 585 + if(alt != 0) 586 + { 587 + logf("usbaudio: control interface has no alternate %d", alt); 588 + return -1; 589 + } 590 + 591 + return 0; 592 + } 593 + if(intf == (usb_interface + 1)) 594 + { 595 + if(alt < 0 || alt > 1) 596 + { 597 + logf("usbaudio: playback interface has no alternate %d", alt); 598 + return -1; 599 + } 600 + usb_as_playback_intf_alt = alt; 601 + 602 + if(usb_as_playback_intf_alt == 1) 603 + usb_audio_start_playback(); 604 + else 605 + usb_audio_stop_playback(); 606 + logf("usbaudio: use playback alternate %d", alt); 607 + 608 + return 0; 609 + } 610 + else 611 + { 612 + logf("usbaudio: interface %d has no alternate", intf); 613 + return -1; 614 + } 615 + } 616 + 617 + int usb_audio_get_interface(int intf) 618 + { 619 + if(intf == usb_interface) 620 + { 621 + logf("usbaudio: control interface alternate is 0"); 622 + return 0; 623 + } 624 + else if(intf == (usb_interface + 1)) 625 + { 626 + logf("usbaudio: playback interface alternate is %d", usb_as_playback_intf_alt); 627 + return usb_as_playback_intf_alt; 628 + } 629 + else 630 + { 631 + logf("usbaudio: unknown interface %d", intf); 632 + return -1; 633 + } 634 + } 635 + 636 + int usb_audio_get_main_intf(void) 637 + { 638 + return usb_interface; 639 + } 640 + 641 + int usb_audio_get_alt_intf(void) 642 + { 643 + return usb_as_playback_intf_alt; 644 + } 645 + 646 + static bool usb_audio_as_playback_endpoint_request(struct usb_ctrlrequest* req, void *reqdata) 647 + { 648 + /* only support sampling frequency */ 649 + if(req->wValue != (USB_AS_EP_CS_SAMPLING_FREQ_CTL << 8)) 650 + { 651 + logf("usbaudio: endpoint only handles sampling frequency control"); 652 + return false; 653 + } 654 + 655 + switch(req->bRequest) 656 + { 657 + case USB_AC_SET_CUR: 658 + if(req->wLength != 3) 659 + { 660 + logf("usbaudio: bad length for SET_CUR"); 661 + usb_drv_control_response(USB_CONTROL_STALL, NULL, 0); 662 + return true; 663 + } 664 + logf("usbaudio: SET_CUR sampling freq"); 665 + 666 + if (reqdata) { /* control write, second pass */ 667 + set_playback_sampling_frequency(decode3(reqdata)); 668 + usb_drv_control_response(USB_CONTROL_ACK, NULL, 0); 669 + return true; 670 + } else { /* control write, first pass */ 671 + bool error = false; 672 + 673 + if (req->wLength != 3) 674 + error = true; 675 + /* ... other validation? */ 676 + 677 + if (error) 678 + usb_drv_control_response(USB_CONTROL_STALL, NULL, 0); 679 + else 680 + usb_drv_control_response(USB_CONTROL_RECEIVE, usb_buffer, 3); 681 + 682 + return true; 683 + } 684 + 685 + case USB_AC_GET_CUR: 686 + if(req->wLength != 3) 687 + { 688 + logf("usbaudio: bad length for GET_CUR"); 689 + usb_drv_control_response(USB_CONTROL_STALL, NULL, 0); 690 + return true; 691 + } 692 + logf("usbaudio: GET_CUR sampling freq"); 693 + encode3(usb_buffer, usb_audio_get_playback_sampling_frequency()); 694 + usb_drv_control_response(USB_CONTROL_ACK, usb_buffer, req->wLength); 695 + 696 + return true; 697 + 698 + default: 699 + logf("usbaudio: unhandled ep req 0x%x", req->bRequest); 700 + } 701 + 702 + return true; 703 + } 704 + 705 + static bool usb_audio_endpoint_request(struct usb_ctrlrequest* req, void *reqdata) 706 + { 707 + int ep = req->wIndex & 0xff; 708 + 709 + if(ep == out_iso_ep_adr) 710 + return usb_audio_as_playback_endpoint_request(req, reqdata); 711 + else 712 + { 713 + logf("usbaudio: unhandled ep req (ep=%d)", ep); 714 + return false; 715 + } 716 + } 717 + 718 + static bool feature_unit_set_mute(int value, uint8_t cmd) 719 + { 720 + if(cmd != USB_AC_CUR_REQ) 721 + { 722 + logf("usbaudio: feature unit MUTE control only has a CUR setting"); 723 + return false; 724 + } 725 + 726 + if(value == 1) 727 + { 728 + logf("usbaudio: mute !"); 729 + tmp_saved_vol = sound_current(SOUND_VOLUME); 730 + // sound_set_volume(sound_min(SOUND_VOLUME)); 731 + // setvol does range checking for us! 732 + global_status.volume = sound_min(SOUND_VOLUME); 733 + setvol(); 734 + return true; 735 + } 736 + else if(value == 0) 737 + { 738 + logf("usbaudio: not muted !"); 739 + // sound_set_volume(tmp_saved_vol); 740 + // setvol does range checking for us! 741 + global_status.volume = tmp_saved_vol; 742 + setvol(); 743 + return true; 744 + } 745 + else 746 + { 747 + logf("usbaudio: invalid value for CUR setting of feature unit (%d)", value); 748 + return false; 749 + } 750 + } 751 + 752 + static bool feature_unit_get_mute(int *value, uint8_t cmd) 753 + { 754 + if(cmd != USB_AC_CUR_REQ) 755 + { 756 + logf("usbaudio: feature unit MUTE control only has a CUR setting"); 757 + return false; 758 + } 759 + 760 + *value = (sound_current(SOUND_VOLUME) == sound_min(SOUND_VOLUME)); 761 + return true; 762 + } 763 + 764 + /* 765 + * USB volume is a signed 16-bit value, -127.9961 dB (0x8001) to +127.9961 dB (0x7FFF) 766 + * in steps of 1/256 dB (0.00390625 dB) 767 + * 768 + * We need to account for different devices having different numbers of decimals 769 + */ 770 + // TODO: do we need to explicitly round these? Will we have a "walking" round conversion issue? 771 + // Step values of 1 dB (and multiples), and 0.5 dB should be able to be met exactly, 772 + // presuming that it starts on an even number. 773 + static int usb_audio_volume_to_db(int vol, int numdecimals) 774 + { 775 + int tmp = (signed long)((signed short)vol * ipow(10, numdecimals)) / 256; 776 + // logf("vol=0x%04X, numdecimals=%d, tmp=%d", vol, numdecimals, tmp); 777 + return tmp; 778 + } 779 + static int db_to_usb_audio_volume(int db, int numdecimals) 780 + { 781 + int tmp = (signed long)(db * 256) / ipow(10, numdecimals); 782 + // logf("db=%d, numdecimals=%d, tmpTodB=%d", db, numdecimals, usb_audio_volume_to_db(tmp, numdecimals)); 783 + return tmp; 784 + } 785 + 786 + #if defined(LOGF_ENABLE) && defined(ROCKBOX_HAS_LOGF) 787 + static const char *usb_audio_ac_ctl_req_str(uint8_t cmd) 788 + { 789 + switch(cmd) 790 + { 791 + case USB_AC_CUR_REQ: return "CUR"; 792 + case USB_AC_MIN_REQ: return "MIN"; 793 + case USB_AC_MAX_REQ: return "MAX"; 794 + case USB_AC_RES_REQ: return "RES"; 795 + case USB_AC_MEM_REQ: return "MEM"; 796 + default: return "<unknown>"; 797 + } 798 + } 799 + #endif 800 + 801 + static bool feature_unit_set_volume(int value, uint8_t cmd) 802 + { 803 + if(cmd != USB_AC_CUR_REQ) 804 + { 805 + logf("usbaudio: feature unit VOLUME doesn't support %s setting", usb_audio_ac_ctl_req_str(cmd)); 806 + return false; 807 + } 808 + 809 + logf("usbaudio: set volume=%d dB", usb_audio_volume_to_db(value, sound_numdecimals(SOUND_VOLUME))); 810 + 811 + // sound_set_volume(usb_audio_volume_to_db(value, sound_numdecimals(SOUND_VOLUME))); 812 + // setvol does range checking for us! 813 + // we cannot guarantee the host will send us a volume within our range 814 + global_status.volume = usb_audio_volume_to_db(value, sound_numdecimals(SOUND_VOLUME)); 815 + setvol(); 816 + return true; 817 + } 818 + 819 + static bool feature_unit_get_volume(int *value, uint8_t cmd) 820 + { 821 + switch(cmd) 822 + { 823 + case USB_AC_CUR_REQ: *value = db_to_usb_audio_volume(sound_current(SOUND_VOLUME), sound_numdecimals(SOUND_VOLUME)); break; 824 + case USB_AC_MIN_REQ: *value = db_to_usb_audio_volume(sound_min(SOUND_VOLUME), sound_numdecimals(SOUND_VOLUME)); break; 825 + case USB_AC_MAX_REQ: *value = db_to_usb_audio_volume(sound_max(SOUND_VOLUME), sound_numdecimals(SOUND_VOLUME)); break; 826 + case USB_AC_RES_REQ: *value = db_to_usb_audio_volume(sound_steps(SOUND_VOLUME), sound_numdecimals(SOUND_VOLUME)); break; 827 + default: 828 + logf("usbaudio: feature unit VOLUME doesn't support %s setting", usb_audio_ac_ctl_req_str(cmd)); 829 + return false; 830 + } 831 + 832 + logf("usbaudio: get %s volume=%d dB", usb_audio_ac_ctl_req_str(cmd), usb_audio_volume_to_db(*value, sound_numdecimals(SOUND_VOLUME))); 833 + return true; 834 + } 835 + 836 + int usb_audio_get_cur_volume(void) 837 + { 838 + int vol; 839 + feature_unit_get_volume(&vol, USB_AC_CUR_REQ); 840 + return usb_audio_volume_to_db(vol, sound_numdecimals(SOUND_VOLUME)); 841 + } 842 + 843 + static bool usb_audio_set_get_feature_unit(struct usb_ctrlrequest* req, void *reqdata) 844 + { 845 + int channel = req->wValue & 0xff; 846 + int selector = req->wValue >> 8; 847 + uint8_t cmd = (req->bRequest & ~USB_AC_GET_REQ); 848 + int value = 0; 849 + int i; 850 + bool handled; 851 + 852 + /* master channel only */ 853 + if(channel != 0) 854 + { 855 + logf("usbaudio: set/get on feature unit only apply to master channel (%d)", channel); 856 + return false; 857 + } 858 + /* selectors */ 859 + /* all send/received values are integers so already read data if necessary and store in it in an integer */ 860 + if(req->bRequest & USB_AC_GET_REQ) 861 + { 862 + /* get */ 863 + switch(selector) 864 + { 865 + case USB_AC_FU_MUTE: 866 + handled = (req->wLength == 1) && feature_unit_get_mute(&value, cmd); 867 + break; 868 + case USB_AC_VOLUME_CONTROL: 869 + handled = (req->wLength == 2) && feature_unit_get_volume(&value, cmd); 870 + break; 871 + default: 872 + handled = false; 873 + logf("usbaudio: unhandled control selector of feature unit (0x%x)", selector); 874 + break; 875 + } 876 + 877 + if(!handled) 878 + { 879 + logf("usbaudio: unhandled get control 0x%x selector 0x%x of feature unit", cmd, selector); 880 + usb_drv_control_response(USB_CONTROL_STALL, NULL, 0); 881 + return true; 882 + } 883 + 884 + if(req->wLength == 0 || req->wLength > 4) 885 + { 886 + logf("usbaudio: get data payload size is invalid (%d)", req->wLength); 887 + return false; 888 + } 889 + 890 + for(i = 0; i < req->wLength; i++) 891 + usb_buffer[i] = (value >> (8 * i)) & 0xff; 892 + 893 + usb_drv_control_response(USB_CONTROL_ACK, usb_buffer, req->wLength); 894 + return true; 895 + } 896 + else 897 + { 898 + /* set */ 899 + if(req->wLength == 0 || req->wLength > 4) 900 + { 901 + logf("usbaudio: set data payload size is invalid (%d)", req->wLength); 902 + return false; 903 + } 904 + 905 + if (reqdata) { 906 + 907 + for(i = 0; i < req->wLength; i++) 908 + value = value | (usb_buffer[i] << (i * 8)); 909 + 910 + switch(selector) 911 + { 912 + case USB_AC_FU_MUTE: 913 + handled = (req->wLength == 1) && feature_unit_set_mute(value, cmd); 914 + break; 915 + case USB_AC_VOLUME_CONTROL: 916 + handled = (req->wLength == 2) && feature_unit_set_volume(value, cmd); 917 + break; 918 + default: 919 + handled = false; 920 + logf("usbaudio: unhandled control selector of feature unit (0x%x)", selector); 921 + break; 922 + } 923 + 924 + if(!handled) 925 + { 926 + logf("usbaudio: unhandled set control 0x%x selector 0x%x of feature unit", cmd, selector); 927 + usb_drv_control_response(USB_CONTROL_STALL, NULL, 0); 928 + return true; 929 + } 930 + 931 + usb_drv_control_response(USB_CONTROL_ACK, NULL, 0); 932 + return true; 933 + } else { 934 + /* 935 + * should handle the following (req->wValue >> 8): 936 + * USB_AC_FU_MUTE 937 + * USB_AC_VOLUME_CONTROL 938 + */ 939 + 940 + bool error = false; 941 + 942 + if (error) 943 + usb_drv_control_response(USB_CONTROL_STALL, NULL, 0); 944 + else 945 + usb_drv_control_response(USB_CONTROL_RECEIVE, usb_buffer, 3); 946 + 947 + return true; 948 + } 949 + 950 + return true; 951 + } 952 + } 953 + 954 + static bool usb_audio_ac_set_get_request(struct usb_ctrlrequest* req, void *reqdata) 955 + { 956 + switch(req->wIndex >> 8) 957 + { 958 + case AC_PLAYBACK_FEATURE_ID: 959 + return usb_audio_set_get_feature_unit(req, reqdata); 960 + default: 961 + logf("usbaudio: unhandled set/get on entity %d", req->wIndex >> 8); 962 + return false; 963 + } 964 + } 965 + 966 + static bool usb_audio_interface_request(struct usb_ctrlrequest* req, void *reqdata) 967 + { 968 + int intf = req->wIndex & 0xff; 969 + 970 + if(intf == usb_interface) 971 + { 972 + switch(req->bRequest) 973 + { 974 + case USB_AC_SET_CUR: case USB_AC_SET_MIN: case USB_AC_SET_MAX: case USB_AC_SET_RES: 975 + case USB_AC_SET_MEM: case USB_AC_GET_CUR: case USB_AC_GET_MIN: case USB_AC_GET_MAX: 976 + case USB_AC_GET_RES: case USB_AC_GET_MEM: 977 + return usb_audio_ac_set_get_request(req, reqdata); 978 + default: 979 + logf("usbaudio: unhandled ac intf req 0x%x", req->bRequest); 980 + return false; 981 + } 982 + } 983 + else 984 + { 985 + logf("usbaudio: unhandled intf req (intf=%d)", intf); 986 + return false; 987 + } 988 + } 989 + 990 + bool usb_audio_control_request(struct usb_ctrlrequest* req, void *reqdata) 991 + { 992 + (void) reqdata; 993 + 994 + switch(req->bRequestType & USB_RECIP_MASK) 995 + { 996 + case USB_RECIP_ENDPOINT: 997 + return usb_audio_endpoint_request(req, reqdata); 998 + case USB_RECIP_INTERFACE: 999 + return usb_audio_interface_request(req, reqdata); 1000 + default: 1001 + logf("usbaudio: unhandled req type 0x%x", req->bRequestType); 1002 + return false; 1003 + } 1004 + } 1005 + 1006 + void usb_audio_init_connection(void) 1007 + { 1008 + logf("usbaudio: init connection"); 1009 + 1010 + usb_as_playback_intf_alt = 0; 1011 + set_playback_sampling_frequency(HW_SAMPR_DEFAULT); 1012 + tmp_saved_vol = sound_current(SOUND_VOLUME); 1013 + usb_audio_playing = false; 1014 + } 1015 + 1016 + void usb_audio_disconnect(void) 1017 + { 1018 + logf("usbaudio: disconnect"); 1019 + 1020 + usb_audio_stop_playback(); 1021 + usb_audio_free_buf(); 1022 + } 1023 + 1024 + bool usb_audio_get_alloc_failed(void) 1025 + { 1026 + return alloc_failed; 1027 + } 1028 + 1029 + bool usb_audio_get_playing(void) 1030 + { 1031 + return usb_audio_playing; 1032 + } 1033 + 1034 + /* determine if enough prebuffering has been done to restart audio */ 1035 + bool prebuffering_done(void) 1036 + { 1037 + /* restart audio if at least two buffers are filled */ 1038 + int diff = (rx_usb_idx - rx_play_idx + NR_BUFFERS) % NR_BUFFERS; 1039 + return diff >= MINIMUM_BUFFERS_QUEUED; 1040 + } 1041 + 1042 + int usb_audio_get_prebuffering(void) 1043 + { 1044 + return (rx_usb_idx - rx_play_idx + NR_BUFFERS) % NR_BUFFERS; 1045 + } 1046 + 1047 + bool usb_audio_get_underflow(void) 1048 + { 1049 + return playback_audio_underflow; 1050 + } 1051 + 1052 + bool usb_audio_get_overflow(void) 1053 + { 1054 + return usb_rx_overflow; 1055 + } 1056 + 1057 + void usb_audio_transfer_complete(int ep, int dir, int status, int length) 1058 + { 1059 + /* normal handler is too slow to handle the completion rate, because 1060 + * of the low thread schedule rate */ 1061 + (void) ep; 1062 + (void) dir; 1063 + (void) status; 1064 + (void) length; 1065 + } 1066 + 1067 + bool usb_audio_fast_transfer_complete(int ep, int dir, int status, int length) 1068 + { 1069 + (void) dir; 1070 + 1071 + if(ep == out_iso_ep_adr && usb_as_playback_intf_alt == 1) 1072 + { 1073 + // logf("usbaudio: frame: %d", usb_drv_get_frame_number()); 1074 + if(status != 0) 1075 + return true; /* FIXME how to handle error here ? */ 1076 + /* store length, queue buffer */ 1077 + rx_buf_size[rx_usb_idx] = length; 1078 + rx_usb_idx = (rx_usb_idx + 1) % NR_BUFFERS; 1079 + /* guard against IRQ to avoid race with completion audio completion */ 1080 + int oldlevel = disable_irq_save(); 1081 + /* setup a new transaction except if we ran out of buffers */ 1082 + if(rx_usb_idx != rx_play_idx) 1083 + { 1084 + logf("usbaudio: new transaction"); 1085 + usb_drv_recv_nonblocking(out_iso_ep_adr, rx_buffer + (rx_usb_idx*REAL_BUF_SIZE), BUFFER_SIZE); 1086 + } 1087 + else 1088 + { 1089 + logf("usbaudio: rx overflow"); 1090 + usb_rx_overflow = true; 1091 + } 1092 + /* if audio underflowed and prebuffering is done, restart audio */ 1093 + if(playback_audio_underflow && prebuffering_done()) 1094 + { 1095 + logf("usbaudio: prebuffering done"); 1096 + playback_audio_underflow = false; 1097 + usb_rx_overflow = false; 1098 + mixer_channel_play_data(PCM_MIXER_CHAN_USBAUDIO, playback_audio_get_more, NULL, 0); 1099 + } 1100 + restore_irq(oldlevel); 1101 + return true; 1102 + } 1103 + else 1104 + return false; 1105 + }
+223
firmware/usbstack/usb_audio.h
··· 1 + /*************************************************************************** 2 + * __________ __ ___. 3 + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 + * \/ \/ \/ \/ \/ 8 + * $Id: $ 9 + * 10 + * Copyright (C) 2014 by Amaury Pouly 11 + * 12 + * All files in this archive are subject to the GNU General Public License. 13 + * See the file COPYING in the source tree root for full license agreement. 14 + * 15 + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 16 + * KIND, either express or implied. 17 + * 18 + ****************************************************************************/ 19 + #ifndef USB_AUDIO_H 20 + #define USB_AUDIO_H 21 + 22 + #include "usb_ch9.h" 23 + 24 + /* 25 + * usb_audio_request_endpoints(): 26 + * 27 + * Calls usb_core_request_endpoint() to request one IN and one OUT 28 + * isochronous endpoint. 29 + * 30 + * Called by allocate_interfaces_and_endpoints(). 31 + * 32 + * Returns -1 if either request fails, returns 0 if success. 33 + * 34 + * Also requests buffer allocations. If allocation fails, 35 + * returns -1 so that the driver will be disabled by the USB core. 36 + */ 37 + int usb_audio_request_endpoints(struct usb_class_driver *); 38 + 39 + /* 40 + * usb_audio_set_first_interface(): 41 + * 42 + * Required function for the class driver. 43 + * 44 + * Called by allocate_interfaces_and_endpoints() to 45 + * tell the class driver what its first interface number is. 46 + * Returns the number of the interface available for the next 47 + * class driver to use. 48 + * 49 + * We need 2 interfaces, AudioControl and AudioStreaming. 50 + * Return interface+2. 51 + */ 52 + int usb_audio_set_first_interface(int interface); 53 + 54 + /* 55 + * usb_audio_get_config_descriptor(): 56 + * 57 + * Required function for the class driver. 58 + * 59 + * Called by request_handler_device_get_descriptor(), which expects 60 + * this function to fill *dest with the configuration descriptor for this 61 + * class driver. 62 + * 63 + * Return the size of this descriptor in bytes. 64 + */ 65 + int usb_audio_get_config_descriptor(unsigned char *dest,int max_packet_size); 66 + 67 + /* 68 + * usb_audio_init_connection(): 69 + * 70 + * Called by usb_core_do_set_config() when the 71 + * connection is ready to be used. Currently just sets 72 + * the audio sample rate to default. 73 + */ 74 + void usb_audio_init_connection(void); 75 + 76 + /* 77 + * usb_audio_init(): 78 + * 79 + * Initialize the driver. Called by usb_core_init(). 80 + * Currently initializes the sampling frequency values available 81 + * to the AudioStreaming interface. 82 + */ 83 + void usb_audio_init(void); 84 + 85 + /* 86 + * usb_audio_disconnect(): 87 + * 88 + * Called by usb_core_exit() AND usb_core_do_set_config(). 89 + * 90 + * Indicates to the Class driver that the connection is no 91 + * longer active. Currently just calls usb_audio_stop_playback(). 92 + */ 93 + void usb_audio_disconnect(void); 94 + 95 + /* 96 + * usb_audio_get_playing(): 97 + * 98 + * Returns playing/not playing status of usbaudio. 99 + */ 100 + bool usb_audio_get_playing(void); 101 + 102 + /* 103 + * usb_audio_get_alloc_failed(): 104 + * 105 + * Return whether the buffer allocation succeeded (0) 106 + * or failed (1). 107 + */ 108 + bool usb_audio_get_alloc_failed(void); 109 + 110 + /* 111 + * usb_audio_transfer_complete(): 112 + * 113 + * Dummy function. 114 + * 115 + * The fast_transfer_complete() function needs to be used instead. 116 + */ 117 + void usb_audio_transfer_complete(int ep,int dir, int status, int length); 118 + 119 + /* 120 + * usb_audio_fast_transfer_complete(): 121 + * 122 + * Called by usb_core_transfer_complete(). 123 + * The normal transfer complete handler system is too slow to deal with 124 + * ISO data at the rate required, so this is required. 125 + * 126 + * Return true if the transfer is handled, false otherwise. 127 + */ 128 + bool usb_audio_fast_transfer_complete(int ep,int dir, int status, int length); 129 + 130 + /* 131 + * usb_audio_control_request(): 132 + * 133 + * Called by control_request_handler_drivers(). 134 + * Pass control requests down to the appropriate functions. 135 + * 136 + * Return true if this driver handles the request, false otherwise. 137 + */ 138 + bool usb_audio_control_request(struct usb_ctrlrequest* req, void* reqdata, unsigned char* dest); 139 + 140 + /* 141 + * usb_audio_set_interface(): 142 + * 143 + * Called by control_request_handler_drivers(). 144 + * Deal with changing the interface between control and streaming. 145 + * 146 + * Return 0 for success, -1 otherwise. 147 + */ 148 + int usb_audio_set_interface(int intf, int alt); 149 + 150 + /* 151 + * usb_audio_get_interface(): 152 + * 153 + * Called by control_request_handler_drivers(). 154 + * Get the alternate of the given interface. 155 + * 156 + * Return the alternate of the given interface, -1 if unknown. 157 + */ 158 + int usb_audio_get_interface(int intf); 159 + 160 + /* 161 + * usb_audio_get_playback_sampling_frequency(): 162 + * 163 + * Return the sample rate currently set. 164 + */ 165 + unsigned long usb_audio_get_playback_sampling_frequency(void); 166 + 167 + /* 168 + * usb_audio_get_main_intf(): 169 + * 170 + * Return the main usb interface 171 + */ 172 + int usb_audio_get_main_intf(void); 173 + 174 + /* 175 + * usb_audio_get_alt_intf(): 176 + * 177 + * Return the alternate usb interface 178 + */ 179 + int usb_audio_get_alt_intf(void); 180 + 181 + /* 182 + * usb_audio_get_out_ep(): 183 + * 184 + * Return the out (to device) endpoint 185 + */ 186 + unsigned int usb_audio_get_out_ep(void); 187 + 188 + /* 189 + * usb_audio_get_in_ep(): 190 + * 191 + * Return the in (to host) endpoint 192 + */ 193 + unsigned int usb_audio_get_in_ep(void); 194 + 195 + /* 196 + * usb_audio_get_prebuffering(): 197 + * 198 + * Return number of buffers filled ahead of playback 199 + */ 200 + int usb_audio_get_prebuffering(void); 201 + 202 + /* 203 + * usb_audio_get_underflow(): 204 + * 205 + * Return whether playback is in "underflow" state 206 + */ 207 + bool usb_audio_get_underflow(void); 208 + 209 + /* 210 + * usb_audio_get_overflow(): 211 + * 212 + * Return whether usb is in "overflow" state 213 + */ 214 + bool usb_audio_get_overflow(void); 215 + 216 + /* 217 + * usb_audio_get_cur_volume(): 218 + * 219 + * Return current audio volume in db 220 + */ 221 + int usb_audio_get_cur_volume(void); 222 + 223 + #endif
+233
firmware/usbstack/usb_audio_def.h
··· 1 + /*************************************************************************** 2 + * __________ __ ___. 3 + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 + * \/ \/ \/ \/ \/ 8 + * $Id: $ 9 + * 10 + * Copyright (C) 2010 by Amaury Pouly 11 + * 12 + * All files in this archive are subject to the GNU General Public License. 13 + * See the file COPYING in the source tree root for full license agreement. 14 + * 15 + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 16 + * KIND, either express or implied. 17 + * 18 + ****************************************************************************/ 19 + /* Parts of this file are based on Frank Gevaerts work and on audio.h from linux */ 20 + 21 + #ifndef USB_AUDIO_DEF_H 22 + #define USB_AUDIO_DEF_H 23 + 24 + #include "usb_ch9.h" 25 + 26 + #define USB_SUBCLASS_AUDIO_CONTROL 1 27 + #define USB_SUBCLASS_AUDIO_STREAMING 2 28 + 29 + #define USB_AC_HEADER 1 30 + #define USB_AC_INPUT_TERMINAL 2 31 + #define USB_AC_OUTPUT_TERMINAL 3 32 + #define USB_AC_MIXER_UNIT 4 33 + #define USB_AC_SELECTOR_UNIT 5 34 + #define USB_AC_FEATURE_UNIT 6 35 + #define USB_AC_PROCESSING_UNIT 8 36 + #define USB_AC_EXTENSION_UNIT 9 37 + 38 + #define USB_AS_GENERAL 1 39 + #define USB_AS_FORMAT_TYPE 2 40 + 41 + #define USB_AC_CHANNEL_LEFT_FRONT 0x1 42 + #define USB_AC_CHANNEL_RIGHT_FRONT 0x2 43 + 44 + #define USB_AC_CHANNELS_LEFT_RIGHT_FRONT (USB_AC_CHANNEL_LEFT_FRONT | USB_AC_CHANNEL_RIGHT_FRONT) 45 + 46 + /* usb audio data structures */ 47 + struct usb_ac_header { 48 + uint8_t bLength; 49 + uint8_t bDescriptorType; /* USB_DT_CS_INTERFACE */ 50 + uint8_t bDescriptorSubType; /* USB_AC_HEADER */ 51 + uint16_t bcdADC; 52 + uint16_t wTotalLength; 53 + uint8_t bInCollection; 54 + uint8_t baInterfaceNr[]; 55 + } __attribute__ ((packed)); 56 + 57 + #define USB_AC_SIZEOF_HEADER(n) (8 + (n)) 58 + 59 + #define USB_AC_TERMINAL_UNDEFINED 0x100 60 + #define USB_AC_TERMINAL_STREAMING 0x101 61 + #define USB_AC_TERMINAL_VENDOR_SPEC 0x1ff 62 + 63 + #define USB_AC_EXT_TERMINAL_UNDEFINED 0x600 64 + #define USB_AC_EXT_TERMINAL_ANALOG 0x601 65 + #define USB_AC_EXT_TERMINAL_DIGITAL 0x602 66 + #define USB_AC_EXT_TERMINAL_LINE 0x603 67 + #define USB_AC_EXT_TERMINAL_LEGACY 0x604 68 + #define USB_AC_EXT_TERMINAL_SPDIF 0x605 69 + #define USB_AC_EXT_TERMINAL_1394_DA 0x606 70 + #define USB_AC_EXT_TERMINAL_1394_DV 0x607 71 + 72 + #define USB_AC_EMB_MINIDISK 0x706 73 + #define USB_AC_EMB_RADIO_RECV 0x710 74 + 75 + #define USB_AC_INPUT_TERMINAL_UNDEFINED 0x200 76 + #define USB_AC_INPUT_TERMINAL_MICROPHONE 0x201 77 + #define USB_AC_INPUT_TERMINAL_DESKTOP_MICROPHONE 0x202 78 + #define USB_AC_INPUT_TERMINAL_PERSONAL_MICROPHONE 0x203 79 + #define USB_AC_INPUT_TERMINAL_OMNI_DIR_MICROPHONE 0x204 80 + #define USB_AC_INPUT_TERMINAL_MICROPHONE_ARRAY 0x205 81 + #define USB_AC_INPUT_TERMINAL_PROC_MICROPHONE_ARRAY 0x206 82 + 83 + struct usb_ac_input_terminal { 84 + uint8_t bLength; 85 + uint8_t bDescriptorType; /* USB_DT_CS_INTERFACE */ 86 + uint8_t bDescriptorSubType; /* USB_AC_INPUT_TERMINAL */ 87 + uint8_t bTerminalId; 88 + uint16_t wTerminalType; /* USB_AC_INPUT_TERMINAL_* */ 89 + uint8_t bAssocTerminal; 90 + uint8_t bNrChannels; 91 + uint16_t wChannelConfig; 92 + uint8_t iChannelNames; 93 + uint8_t iTerminal; 94 + } __attribute__ ((packed)); 95 + 96 + #define USB_AC_OUTPUT_TERMINAL_UNDEFINED 0x300 97 + #define USB_AC_OUTPUT_TERMINAL_SPEAKER 0x301 98 + #define USB_AC_OUTPUT_TERMINAL_HEADPHONES 0x302 99 + #define USB_AC_OUTPUT_TERMINAL_HEAD_MOUNTED_DISPLAY_AUDIO 0x303 100 + #define USB_AC_OUTPUT_TERMINAL_DESKTOP_SPEAKER 0x304 101 + #define USB_AC_OUTPUT_TERMINAL_ROOM_SPEAKER 0x305 102 + #define USB_AC_OUTPUT_TERMINAL_COMMUNICATION_SPEAKER 0x306 103 + #define USB_AC_OUTPUT_TERMINAL_LOW_FREQ_EFFECTS_SPEAKER 0x307 104 + 105 + struct usb_ac_output_terminal { 106 + uint8_t bLength; 107 + uint8_t bDescriptorType; /* USB_DT_CS_INTERFACE */ 108 + uint8_t bDescriptorSubType; /* USB_AC_OUTPUT_TERMINAL */ 109 + uint8_t bTerminalId; 110 + uint16_t wTerminalType; /* USB_AC_OUTPUT_TERMINAL_* */ 111 + uint8_t bAssocTerminal; 112 + uint8_t bSourceId; 113 + uint8_t iTerminal; 114 + } __attribute__ ((packed)); 115 + 116 + #define USB_AC_FU_CONTROL_UNDEFINED 0x00 117 + #define USB_AC_MUTE_CONTROL 0x01 118 + #define USB_AC_VOLUME_CONTROL 0x02 119 + #define USB_AC_BASS_CONTROL 0x03 120 + #define USB_AC_MID_CONTROL 0x04 121 + #define USB_AC_TREBLE_CONTROL 0x05 122 + #define USB_AC_EQUALIZER_CONTROL 0x06 123 + #define USB_AC_AUTO_GAIN_CONTROL 0x07 124 + #define USB_AC_DELAY_CONTROL 0x08 125 + #define USB_AC_BASS_BOOST_CONTROL 0x09 126 + #define USB_AC_LOUDNESS_CONTROL 0x0a 127 + 128 + #define USB_AC_FU_MUTE (1 << (USB_AC_MUTE_CONTROL - 1)) 129 + #define USB_AC_FU_VOLUME (1 << (USB_AC_VOLUME_CONTROL - 1)) 130 + #define USB_AC_FU_BASS (1 << (USB_AC_BASS_CONTROL - 1)) 131 + #define USB_AC_FU_MID (1 << (USB_AC_MID_CONTROL - 1)) 132 + #define USB_AC_FU_TREBLE (1 << (USB_AC_TREBLE_CONTROL - 1)) 133 + #define USB_AC_FU_EQUILIZER (1 << (USB_AC_EQUALIZER_CONTROL - 1)) 134 + #define USB_AC_FU_AUTO_GAIN (1 << (USB_AC_AUTO_GAIN_CONTROL - 1)) 135 + #define USB_AC_FU_DELAY (1 << (USB_AC_DELAY_CONTROL - 1)) 136 + #define USB_AC_FU_BASS_BOOST (1 << (USB_AC_BASS_BOOST_CONTROL - 1)) 137 + #define USB_AC_FU_LOUDNESS (1 << (USB_AC_LOUDNESS_CONTROL - 1)) 138 + 139 + #define DEFINE_USB_AC_FEATURE_UNIT(n,ch) \ 140 + struct usb_ac_feature_unit_##n##_##ch { \ 141 + uint8_t bLength; \ 142 + uint8_t bDescriptorType; /* USB_DT_CS_INTERFACE */ \ 143 + uint8_t bDescriptorSubType; /* USB_AC_FEATURE_UNIT */ \ 144 + uint8_t bUnitId; \ 145 + uint8_t bSourceId; \ 146 + uint8_t bControlSize; \ 147 + uint##n##_t bmaControls[ch + 1]; /* FU_* ORed*/ \ 148 + uint8_t iFeature; \ 149 + } __attribute__ ((packed)); 150 + 151 + #define USB_AC_SET_REQ 0x00 152 + #define USB_AC_GET_REQ 0x80 153 + 154 + #define USB_AC_CUR_REQ 0x1 155 + #define USB_AC_MIN_REQ 0x2 156 + #define USB_AC_MAX_REQ 0x3 157 + #define USB_AC_RES_REQ 0x4 158 + #define USB_AC_MEM_REQ 0x5 159 + 160 + #define USB_AC_SET_CUR (USB_AC_SET_REQ | USB_AC_CUR_REQ) 161 + #define USB_AC_GET_CUR (USB_AC_GET_REQ | USB_AC_CUR_REQ) 162 + #define USB_AC_SET_MIN (USB_AC_SET_REQ | USB_AC_MIN_REQ) 163 + #define USB_AC_GET_MIN (USB_AC_GET_REQ | USB_AC_MIN_REQ) 164 + #define USB_AC_SET_MAX (USB_AC_SET_REQ | USB_AC_MAX_REQ) 165 + #define USB_AC_GET_MAX (USB_AC_GET_REQ | USB_AC_MAX_REQ) 166 + #define USB_AC_SET_RES (USB_AC_SET_REQ | USB_AC_RES_REQ) 167 + #define USB_AC_GET_RES (USB_AC_GET_REQ | USB_AC_RES_REQ) 168 + #define USB_AC_SET_MEM (USB_AC_SET_REQ | USB_AC_MEM_REQ) 169 + #define USB_AC_GET_MEM (USB_AC_GET_REQ | USB_AC_MEM_REQ) 170 + #define USB_AC_GET_STAT 0xff 171 + 172 + #define USB_AS_FORMAT_TYPE_UNDEFINED 0x0 173 + #define USB_AS_FORMAT_TYPE_I 0x1 174 + #define USB_AS_FORMAT_TYPE_II 0x2 175 + #define USB_AS_FORMAT_TYPE_III 0x3 176 + 177 + struct usb_as_interface { 178 + uint8_t bLength; 179 + uint8_t bDescriptorType; /* USB_DT_CS_INTERFACE */ 180 + uint8_t bDescriptorSubType; /* USB_AS_GENERAL */ 181 + uint8_t bTerminalLink; 182 + uint8_t bDelay; 183 + uint16_t wFormatTag; 184 + } __attribute__ ((packed)); 185 + 186 + #define USB_AS_EP_GENERAL 0x01 187 + 188 + #define USB_AS_EP_CS_SAMPLING_FREQ_CTL 0x01 189 + #define USB_AS_EP_CS_PITCH_CTL 0x02 190 + 191 + struct usb_iso_audio_endpoint_descriptor { 192 + uint8_t bLength; 193 + uint8_t bDescriptorType; 194 + 195 + uint8_t bEndpointAddress; 196 + uint8_t bmAttributes; 197 + uint16_t wMaxPacketSize; 198 + uint8_t bInterval; 199 + uint8_t bRefresh; 200 + uint8_t bSynchAddress; 201 + } __attribute__ ((packed)); 202 + 203 + struct usb_as_iso_endpoint { 204 + uint8_t bLength; 205 + uint8_t bDescriptorType; /* USB_DT_CS_ENDPOINT */ 206 + uint8_t bDescriptorSubType; /* USB_AS_EP_GENERAL */ 207 + uint8_t bmAttributes; 208 + uint8_t bLockDelayUnits; 209 + uint16_t wLockDelay; 210 + } __attribute__ ((packed)); 211 + 212 + #define USB_AS_FORMAT_TYPE_I_UNDEFINED 0x0 213 + #define USB_AS_FORMAT_TYPE_I_PCM 0x1 214 + #define USB_AS_FORMAT_TYPE_I_PCM8 0x2 215 + #define USB_AS_FORMAT_TYPE_I_IEEE_FLOAT 0x3 216 + #define USB_AS_FORMAT_TYPE_I_ALAW 0x4 217 + #define USB_AS_FORMAT_TYPE_I_MULAW 0x5 218 + 219 + struct usb_as_format_type_i_discrete { 220 + uint8_t bLength; 221 + uint8_t bDescriptorType; /* USB_DT_CS_INTERFACE */ 222 + uint8_t bDescriptorSubType; /* USB_AS_FORMAT_TYPE */ 223 + uint8_t bFormatType; /* USB_AS_FORMAT_TYPE_I */ 224 + uint8_t bNrChannels; 225 + uint8_t bSubframeSize; 226 + uint8_t bBitResolution; 227 + uint8_t bSamFreqType; /* Number of discrete frequencies */ 228 + uint8_t tSamFreq[][3]; 229 + } __attribute__ ((packed)); 230 + 231 + #define USB_AS_SIZEOF_FORMAT_TYPE_I_DISCRETE(n) (8 + (n * 3)) 232 + 233 + #endif /* USB_AUDIO_DEF_H */
+20 -1
firmware/usbstack/usb_class_driver.h
··· 74 74 /* Tells the driver that a usb transfer has been completed. Note that "dir" 75 75 is relative to the host 76 76 Optional function */ 77 - void (*transfer_complete)(int ep,int dir, int status, int length); 77 + void (*transfer_complete)(int ep, int dir, int status, int length); 78 + 79 + /* Similar to transfer_complete but called directly instead of going through 80 + * the usb queue. Since it might be called in an interrupt context, 81 + * processing should be kept to a minimum. This is mainly intended for 82 + * isochronous transfers. 83 + * The function must return true if it handled the completion, and false 84 + * otherwise so that it is dispatched to the normal handler 85 + * Optional function */ 86 + bool (*fast_transfer_complete)(int ep, int dir, int status, int length); 78 87 79 88 /* Tells the driver that a control request has come in. If the driver is 80 89 able to handle it, it should ack the request, and return true. Otherwise ··· 88 97 Optional function */ 89 98 void (*notify_hotswap)(int volume, bool inserted); 90 99 #endif 100 + 101 + /* Tells the driver to select an alternate setting for a specific interface. 102 + * Returns 0 on success and -1 on error. 103 + * Mandatory function if alternate interface support is needed */ 104 + int (*set_interface)(int interface, int alt_setting); 105 + 106 + /* Asks the driver what is the current alternate setting for a specific interface. 107 + * Returns value on success and -1 on error. 108 + * Mandatory function if alternate interface support is needed */ 109 + int (*get_interface)(int interface); 91 110 }; 92 111 93 112 #define PACK_DATA(dest, data) pack_data(dest, &(data), sizeof(data))
+73 -12
firmware/usbstack/usb_core.c
··· 48 48 #include "usb_hid.h" 49 49 #endif 50 50 51 + #ifdef USB_ENABLE_AUDIO 52 + #include "usb_audio.h" 53 + #include "usb_audio_def.h" // DEBUG 54 + #endif 55 + 51 56 /* TODO: Move target-specific stuff somewhere else (serial number reading) */ 52 57 53 58 #if defined(IPOD_ARCH) && defined(CPU_PP) ··· 166 171 static int usb_core_num_interfaces; 167 172 168 173 typedef void (*completion_handler_t)(int ep, int dir, int status, int length); 174 + typedef bool (*fast_completion_handler_t)(int ep, int dir, int status, int length); 169 175 typedef bool (*control_handler_t)(struct usb_ctrlrequest* req, void* reqdata, 170 176 unsigned char* dest); 171 177 172 178 static struct 173 179 { 174 180 completion_handler_t completion_handler[2]; 181 + fast_completion_handler_t fast_completion_handler[2]; 175 182 control_handler_t control_handler[2]; 176 183 struct usb_transfer_completion_event_data completion_event[2]; 177 184 } ep_data[USB_NUM_ENDPOINTS]; ··· 254 261 #endif 255 262 }, 256 263 #endif 264 + #ifdef USB_ENABLE_AUDIO 265 + [USB_DRIVER_AUDIO] = { 266 + .enabled = false, 267 + .needs_exclusive_storage = false, 268 + .first_interface = 0, 269 + .last_interface = 0, 270 + .request_endpoints = usb_audio_request_endpoints, 271 + .set_first_interface = usb_audio_set_first_interface, 272 + .get_config_descriptor = usb_audio_get_config_descriptor, 273 + .init_connection = usb_audio_init_connection, 274 + .init = usb_audio_init, 275 + .disconnect = usb_audio_disconnect, 276 + .transfer_complete = usb_audio_transfer_complete, 277 + .fast_transfer_complete = usb_audio_fast_transfer_complete, 278 + .control_request = usb_audio_control_request, 279 + #ifdef HAVE_HOTSWAP 280 + .notify_hotswap = NULL, 281 + #endif 282 + .set_interface = usb_audio_set_interface, 283 + .get_interface = usb_audio_get_interface, 284 + }, 285 + #endif 257 286 }; 258 287 259 288 #ifdef USB_LEGACY_CONTROL_API ··· 542 571 ep = EP_NUM(ret); 543 572 544 573 ep_data[ep].completion_handler[dir] = drv->transfer_complete; 574 + ep_data[ep].fast_completion_handler[dir] = drv->fast_transfer_complete; 545 575 ep_data[ep].control_handler[dir] = drv->control_request; 546 576 547 577 return ret; ··· 598 628 if(drivers[i].enabled && 599 629 drivers[i].control_request && 600 630 drivers[i].first_interface <= interface && 601 - drivers[i].last_interface > interface) 602 - { 631 + drivers[i].last_interface > interface) { 632 + /* Check for SET_INTERFACE and GET_INTERFACE */ 633 + if((req->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE && 634 + (req->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { 635 + 636 + if(req->bRequest == USB_REQ_SET_INTERFACE) { 637 + logf("usb_core: SET INTERFACE 0x%x 0x%x", req->wValue, req->wIndex); 638 + if(drivers[i].set_interface && 639 + drivers[i].set_interface(req->wIndex, req->wValue) >= 0) { 640 + 641 + usb_drv_control_response(USB_CONTROL_ACK, NULL, 0); 642 + handled = true; 643 + } 644 + break; 645 + } 646 + else if(req->bRequest == USB_REQ_GET_INTERFACE) { 647 + int alt = -1; 648 + logf("usb_core: GET INTERFACE 0x%x", req->wIndex); 649 + 650 + if(drivers[i].get_interface) 651 + alt = drivers[i].get_interface(req->wIndex); 652 + 653 + if(alt >= 0 && alt < 255) { 654 + response_data[0] = alt; 655 + usb_drv_control_response(USB_CONTROL_ACK, response_data, 1); 656 + handled = true; 657 + } 658 + break; 659 + } 660 + /* fallback */ 661 + } 662 + 603 663 handled = drivers[i].control_request(req, reqdata, response_data); 604 - if(handled) 605 - break; 664 + break; /* no other driver can handle it because it's interface specific */ 606 665 } 607 666 } 608 667 if(!handled) { 609 668 /* nope. flag error */ 610 - logf("bad req:desc %d:%d", req->bRequest, req->wValue >> 8); 669 + logf("bad req 0x%x:0x%x:0x%x:0x%x:0x%x", req->bRequestType,req->bRequest, 670 + req->wValue, req->wIndex, req->wLength); 611 671 usb_drv_control_response(USB_CONTROL_STALL, NULL, 0); 612 672 } 613 673 } ··· 804 864 { 805 865 case USB_REQ_SET_INTERFACE: 806 866 logf("usb_core: SET_INTERFACE"); 807 - usb_drv_control_response(USB_CONTROL_ACK, NULL, 0); 808 - break; 809 - 810 867 case USB_REQ_GET_INTERFACE: 811 - logf("usb_core: GET_INTERFACE"); 812 - response_data[0] = 0; 813 - usb_drv_control_response(USB_CONTROL_ACK, response_data, 1); 868 + control_request_handler_drivers(req, reqdata); 869 + break; 814 870 break; 815 871 case USB_REQ_GET_STATUS: 816 872 response_data[0] = 0; ··· 860 916 861 917 if(!handled) { 862 918 /* nope. flag error */ 863 - logf("usb bad req %d", req->bRequest); 919 + logf("bad req 0x%x:0x%x:0x%x:0x%x:0x%x", req->bRequestType,req->bRequest, 920 + req->wValue, req->wIndex, req->wLength); 864 921 usb_drv_control_response(USB_CONTROL_STALL, NULL, 0); 865 922 } 866 923 } ··· 970 1027 { 971 1028 struct usb_transfer_completion_event_data* completion_event = 972 1029 &ep_data[endpoint].completion_event[EP_DIR(dir)]; 1030 + /* Fast notification */ 1031 + fast_completion_handler_t handler = ep_data[endpoint].fast_completion_handler[EP_DIR(dir)]; 1032 + if(handler != NULL && handler(endpoint, dir, status, length)) 1033 + return; /* do not dispatch to the queue if handled */ 973 1034 974 1035 void* data0 = NULL; 975 1036 void* data1 = NULL;