^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * audio.c -- Audio gadget driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2008 Analog Devices, Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /* #define VERBOSE_DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/usb/composite.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define DRIVER_DESC "Linux USB Audio Gadget"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define DRIVER_VERSION "Feb 2, 2012"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) USB_GADGET_COMPOSITE_OPTIONS();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #ifndef CONFIG_GADGET_UAC1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "u_uac2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* Playback(USB-IN) Default Stereo - Fl/Fr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int p_chmask = UAC2_DEF_PCHMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) module_param(p_chmask, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* Playback Default 48 KHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int p_srates[UAC_MAX_RATES] = {UAC2_DEF_PSRATE};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int p_srates_cnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) module_param_array_named(p_srate, p_srates, uint, &p_srates_cnt, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MODULE_PARM_DESC(p_srate, "Playback Sampling Rates (array)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Playback Default 16bits/sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int p_ssize = UAC2_DEF_PSSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) module_param(p_ssize, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Capture(USB-OUT) Default Stereo - Fl/Fr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int c_chmask = UAC2_DEF_CCHMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) module_param(c_chmask, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Capture Default 64 KHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int c_srates[UAC_MAX_RATES] = {UAC2_DEF_CSRATE};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static int c_srates_cnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) module_param_array_named(c_srate, c_srates, uint, &c_srates_cnt, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) MODULE_PARM_DESC(c_srate, "Capture Sampling Rates (array)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Capture Default 16bits/sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int c_ssize = UAC2_DEF_CSSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) module_param(c_ssize, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #ifndef CONFIG_GADGET_UAC1_LEGACY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include "u_uac1.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Playback(USB-IN) Default Stereo - Fl/Fr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int p_chmask = UAC1_DEF_PCHMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) module_param(p_chmask, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* Playback Default 48 KHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int p_srates[UAC_MAX_RATES] = {UAC1_DEF_PSRATE};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int p_srates_cnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) module_param_array_named(p_srate, p_srates, uint, &p_srates_cnt, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) MODULE_PARM_DESC(p_srate, "Playback Sampling Rates (array)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* Playback Default 16bits/sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static int p_ssize = UAC1_DEF_PSSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) module_param(p_ssize, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* Capture(USB-OUT) Default Stereo - Fl/Fr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static int c_chmask = UAC1_DEF_CCHMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) module_param(c_chmask, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* Capture Default 48 KHz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int c_srates[UAC_MAX_RATES] = {UAC1_DEF_CSRATE};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int c_srates_cnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) module_param_array_named(c_srate, c_srates, uint, &c_srates_cnt, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) MODULE_PARM_DESC(c_srate, "Capture Sampling Rates (array)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* Capture Default 16bits/sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int c_ssize = UAC1_DEF_CSSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) module_param(c_ssize, uint, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #else /* CONFIG_GADGET_UAC1_LEGACY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #include "u_uac1_legacy.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static char *fn_play = FILE_PCM_PLAYBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) module_param(fn_play, charp, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static char *fn_cap = FILE_PCM_CAPTURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) module_param(fn_cap, charp, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static char *fn_cntl = FILE_CONTROL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) module_param(fn_cntl, charp, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) MODULE_PARM_DESC(fn_cntl, "Control device file name");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) module_param(req_buf_size, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int req_count = UAC1_REQ_COUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) module_param(req_count, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int audio_buf_size = UAC1_AUDIO_BUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) module_param(audio_buf_size, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #endif /* CONFIG_GADGET_UAC1_LEGACY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* string IDs are assigned dynamically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static struct usb_string strings_dev[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) [USB_GADGET_MANUFACTURER_IDX].s = "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) [USB_GADGET_SERIAL_IDX].s = "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) { } /* end of list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static struct usb_gadget_strings stringtab_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .language = 0x0409, /* en-us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .strings = strings_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static struct usb_gadget_strings *audio_strings[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) &stringtab_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #ifndef CONFIG_GADGET_UAC1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static struct usb_function_instance *fi_uac2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static struct usb_function *f_uac2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static struct usb_function_instance *fi_uac1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static struct usb_function *f_uac1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Instead: allocate your own, using normal USB-IF procedures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Thanks to Linux Foundation for donating this product ID. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static struct usb_device_descriptor device_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .bLength = sizeof device_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .bDescriptorType = USB_DT_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* .bcdUSB = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) #ifdef CONFIG_GADGET_UAC1_LEGACY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .bDeviceClass = USB_CLASS_PER_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .bDeviceSubClass = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .bDeviceProtocol = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .bDeviceClass = USB_CLASS_MISC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .bDeviceSubClass = 0x02,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .bDeviceProtocol = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* .bMaxPacketSize0 = f(hardware) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* Vendor and product id defaults change according to what configs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * we support. (As does bNumConfigurations.) These values can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * also be overridden by module parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .idVendor = cpu_to_le16(AUDIO_VENDOR_NUM),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .idProduct = cpu_to_le16(AUDIO_PRODUCT_NUM),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* .bcdDevice = f(hardware) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* .iManufacturer = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* .iProduct = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* NO SERIAL NUMBER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .bNumConfigurations = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static const struct usb_descriptor_header *otg_desc[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static int audio_do_config(struct usb_configuration *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /* FIXME alloc iConfiguration string, set it in c->strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (gadget_is_otg(c->cdev->gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) c->descriptors = otg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) #ifdef CONFIG_GADGET_UAC1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) f_uac1 = usb_get_function(fi_uac1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (IS_ERR(f_uac1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) status = PTR_ERR(f_uac1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) status = usb_add_function(c, f_uac1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) usb_put_function(f_uac1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) f_uac2 = usb_get_function(fi_uac2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (IS_ERR(f_uac2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) status = PTR_ERR(f_uac2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) status = usb_add_function(c, f_uac2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) usb_put_function(f_uac2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static struct usb_configuration audio_config_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .label = DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .bConfigurationValue = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* .iConfiguration = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static int audio_bind(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) #ifndef CONFIG_GADGET_UAC1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct f_uac2_opts *uac2_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) #ifndef CONFIG_GADGET_UAC1_LEGACY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct f_uac1_opts *uac1_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct f_uac1_legacy_opts *uac1_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) #ifndef CONFIG_GADGET_UAC1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) fi_uac2 = usb_get_function_instance("uac2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (IS_ERR(fi_uac2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return PTR_ERR(fi_uac2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #ifndef CONFIG_GADGET_UAC1_LEGACY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) fi_uac1 = usb_get_function_instance("uac1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) fi_uac1 = usb_get_function_instance("uac1_legacy");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (IS_ERR(fi_uac1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return PTR_ERR(fi_uac1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) #ifndef CONFIG_GADGET_UAC1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) uac2_opts = container_of(fi_uac2, struct f_uac2_opts, func_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) uac2_opts->p_chmask = p_chmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) for (i = 0; i < p_srates_cnt; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) uac2_opts->p_srates[i] = p_srates[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) uac2_opts->p_ssize = p_ssize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) uac2_opts->c_chmask = c_chmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) for (i = 0; i < c_srates_cnt; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) uac2_opts->c_srates[i] = c_srates[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) uac2_opts->c_ssize = c_ssize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) uac2_opts->req_number = UAC2_DEF_REQ_NUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) #ifndef CONFIG_GADGET_UAC1_LEGACY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) uac1_opts = container_of(fi_uac1, struct f_uac1_opts, func_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) uac1_opts->p_chmask = p_chmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) for (i = 0; i < p_srates_cnt; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) uac1_opts->p_srates[i] = p_srates[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) uac1_opts->p_ssize = p_ssize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) uac1_opts->c_chmask = c_chmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) for (i = 0; i < c_srates_cnt; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) uac1_opts->c_srates[i] = c_srates[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) uac1_opts->c_ssize = c_ssize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) uac1_opts->req_number = UAC1_DEF_REQ_NUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) #else /* CONFIG_GADGET_UAC1_LEGACY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) uac1_opts = container_of(fi_uac1, struct f_uac1_legacy_opts, func_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) uac1_opts->fn_play = fn_play;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) uac1_opts->fn_cap = fn_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) uac1_opts->fn_cntl = fn_cntl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) uac1_opts->req_buf_size = req_buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) uac1_opts->req_count = req_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) uac1_opts->audio_buf_size = audio_buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) #endif /* CONFIG_GADGET_UAC1_LEGACY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) status = usb_string_ids_tab(cdev, strings_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct usb_descriptor_header *usb_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (!usb_desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) usb_otg_descriptor_init(cdev->gadget, usb_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) otg_desc[0] = usb_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) otg_desc[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) status = usb_add_config(cdev, &audio_config_driver, audio_do_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) goto fail_otg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) usb_composite_overwrite_options(cdev, &coverwrite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) fail_otg_desc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) kfree(otg_desc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) otg_desc[0] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) #ifndef CONFIG_GADGET_UAC1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) usb_put_function_instance(fi_uac2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) usb_put_function_instance(fi_uac1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static int audio_unbind(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) #ifdef CONFIG_GADGET_UAC1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!IS_ERR_OR_NULL(f_uac1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) usb_put_function(f_uac1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (!IS_ERR_OR_NULL(fi_uac1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) usb_put_function_instance(fi_uac1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!IS_ERR_OR_NULL(f_uac2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) usb_put_function(f_uac2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!IS_ERR_OR_NULL(fi_uac2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) usb_put_function_instance(fi_uac2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) kfree(otg_desc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) otg_desc[0] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static struct usb_composite_driver audio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) .name = "g_audio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) .dev = &device_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) .strings = audio_strings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) .max_speed = USB_SPEED_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) .bind = audio_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) .unbind = audio_unbind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) module_usb_composite_driver(audio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)