Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 2 | /* |
| 3 | * f_audio.c -- USB Audio class function driver |
| 4 | * |
| 5 | * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> |
| 6 | * Copyright (C) 2008 Analog Devices, Inc |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 11 | #include <linux/module.h> |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 12 | #include <linux/device.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 13 | #include <linux/atomic.h> |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 14 | |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 15 | #include "u_uac1_legacy.h" |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 16 | |
Laurent Pinchart | b95cd7e | 2009-06-21 23:21:55 +0200 | [diff] [blame] | 17 | static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value); |
| 18 | static int generic_get_cmd(struct usb_audio_control *con, u8 cmd); |
| 19 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 20 | /* |
| 21 | * DESCRIPTORS ... most are static, but strings and full |
| 22 | * configuration descriptors are built on demand. |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * We have two interfaces- AudioControl and AudioStreaming |
| 27 | * TODO: only supcard playback currently |
| 28 | */ |
| 29 | #define F_AUDIO_AC_INTERFACE 0 |
| 30 | #define F_AUDIO_AS_INTERFACE 1 |
Xuebing Wang | 8d252db | 2014-12-10 16:28:15 +0800 | [diff] [blame] | 31 | #define F_AUDIO_NUM_INTERFACES 1 |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 32 | |
| 33 | /* B.3.1 Standard AC Interface Descriptor */ |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 34 | static struct usb_interface_descriptor ac_interface_desc = { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 35 | .bLength = USB_DT_INTERFACE_SIZE, |
| 36 | .bDescriptorType = USB_DT_INTERFACE, |
| 37 | .bNumEndpoints = 0, |
| 38 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 39 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, |
| 40 | }; |
| 41 | |
Xuebing Wang | 8d252db | 2014-12-10 16:28:15 +0800 | [diff] [blame] | 42 | /* |
| 43 | * The number of AudioStreaming and MIDIStreaming interfaces |
| 44 | * in the Audio Interface Collection |
| 45 | */ |
| 46 | DECLARE_UAC_AC_HEADER_DESCRIPTOR(1); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 47 | |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 48 | #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES) |
Cliff Cai | d16f172 | 2009-12-09 22:21:12 -0500 | [diff] [blame] | 49 | /* 1 input terminal, 1 output terminal and 1 feature unit */ |
| 50 | #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \ |
| 51 | + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0)) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 52 | /* B.3.2 Class-Specific AC Interface Descriptor */ |
Xuebing Wang | 8d252db | 2014-12-10 16:28:15 +0800 | [diff] [blame] | 53 | static struct uac1_ac_header_descriptor_1 ac_header_desc = { |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 54 | .bLength = UAC_DT_AC_HEADER_LENGTH, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 55 | .bDescriptorType = USB_DT_CS_INTERFACE, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 56 | .bDescriptorSubtype = UAC_HEADER, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 57 | .bcdADC = __constant_cpu_to_le16(0x0100), |
Cliff Cai | d16f172 | 2009-12-09 22:21:12 -0500 | [diff] [blame] | 58 | .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH), |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 59 | .bInCollection = F_AUDIO_NUM_INTERFACES, |
| 60 | .baInterfaceNr = { |
Xuebing Wang | 8d252db | 2014-12-10 16:28:15 +0800 | [diff] [blame] | 61 | /* Interface number of the first AudioStream interface */ |
| 62 | [0] = 1, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
| 66 | #define INPUT_TERMINAL_ID 1 |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 67 | static struct uac_input_terminal_descriptor input_terminal_desc = { |
| 68 | .bLength = UAC_DT_INPUT_TERMINAL_SIZE, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 69 | .bDescriptorType = USB_DT_CS_INTERFACE, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 70 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 71 | .bTerminalID = INPUT_TERMINAL_ID, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 72 | .wTerminalType = UAC_TERMINAL_STREAMING, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 73 | .bAssocTerminal = 0, |
| 74 | .wChannelConfig = 0x3, |
| 75 | }; |
| 76 | |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 77 | DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 78 | |
| 79 | #define FEATURE_UNIT_ID 2 |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 80 | static struct uac_feature_unit_descriptor_0 feature_unit_desc = { |
| 81 | .bLength = UAC_DT_FEATURE_UNIT_SIZE(0), |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 82 | .bDescriptorType = USB_DT_CS_INTERFACE, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 83 | .bDescriptorSubtype = UAC_FEATURE_UNIT, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 84 | .bUnitID = FEATURE_UNIT_ID, |
| 85 | .bSourceID = INPUT_TERMINAL_ID, |
| 86 | .bControlSize = 2, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 87 | .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME), |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static struct usb_audio_control mute_control = { |
| 91 | .list = LIST_HEAD_INIT(mute_control.list), |
| 92 | .name = "Mute Control", |
Takashi Iwai | f07ff97 | 2010-06-01 07:42:03 +0200 | [diff] [blame] | 93 | .type = UAC_FU_MUTE, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 94 | /* Todo: add real Mute control code */ |
| 95 | .set = generic_set_cmd, |
| 96 | .get = generic_get_cmd, |
| 97 | }; |
| 98 | |
| 99 | static struct usb_audio_control volume_control = { |
| 100 | .list = LIST_HEAD_INIT(volume_control.list), |
| 101 | .name = "Volume Control", |
Takashi Iwai | f07ff97 | 2010-06-01 07:42:03 +0200 | [diff] [blame] | 102 | .type = UAC_FU_VOLUME, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 103 | /* Todo: add real Volume control code */ |
| 104 | .set = generic_set_cmd, |
| 105 | .get = generic_get_cmd, |
| 106 | }; |
| 107 | |
| 108 | static struct usb_audio_control_selector feature_unit = { |
| 109 | .list = LIST_HEAD_INIT(feature_unit.list), |
| 110 | .id = FEATURE_UNIT_ID, |
| 111 | .name = "Mute & Volume Control", |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 112 | .type = UAC_FEATURE_UNIT, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 113 | .desc = (struct usb_descriptor_header *)&feature_unit_desc, |
| 114 | }; |
| 115 | |
| 116 | #define OUTPUT_TERMINAL_ID 3 |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 117 | static struct uac1_output_terminal_descriptor output_terminal_desc = { |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 118 | .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 119 | .bDescriptorType = USB_DT_CS_INTERFACE, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 120 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 121 | .bTerminalID = OUTPUT_TERMINAL_ID, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 122 | .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 123 | .bAssocTerminal = FEATURE_UNIT_ID, |
| 124 | .bSourceID = FEATURE_UNIT_ID, |
| 125 | }; |
| 126 | |
| 127 | /* B.4.1 Standard AS Interface Descriptor */ |
| 128 | static struct usb_interface_descriptor as_interface_alt_0_desc = { |
| 129 | .bLength = USB_DT_INTERFACE_SIZE, |
| 130 | .bDescriptorType = USB_DT_INTERFACE, |
| 131 | .bAlternateSetting = 0, |
| 132 | .bNumEndpoints = 0, |
| 133 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 134 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 135 | }; |
| 136 | |
| 137 | static struct usb_interface_descriptor as_interface_alt_1_desc = { |
| 138 | .bLength = USB_DT_INTERFACE_SIZE, |
| 139 | .bDescriptorType = USB_DT_INTERFACE, |
| 140 | .bAlternateSetting = 1, |
| 141 | .bNumEndpoints = 1, |
| 142 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 143 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 144 | }; |
| 145 | |
| 146 | /* B.4.2 Class-Specific AS Interface Descriptor */ |
Daniel Mack | 69da9bc | 2010-06-16 17:57:28 +0200 | [diff] [blame] | 147 | static struct uac1_as_header_descriptor as_header_desc = { |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 148 | .bLength = UAC_DT_AS_HEADER_SIZE, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 149 | .bDescriptorType = USB_DT_CS_INTERFACE, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 150 | .bDescriptorSubtype = UAC_AS_GENERAL, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 151 | .bTerminalLink = INPUT_TERMINAL_ID, |
| 152 | .bDelay = 1, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 153 | .wFormatTag = UAC_FORMAT_TYPE_I_PCM, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 154 | }; |
| 155 | |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 156 | DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 157 | |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 158 | static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = { |
| 159 | .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 160 | .bDescriptorType = USB_DT_CS_INTERFACE, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 161 | .bDescriptorSubtype = UAC_FORMAT_TYPE, |
| 162 | .bFormatType = UAC_FORMAT_TYPE_I, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 163 | .bSubframeSize = 2, |
| 164 | .bBitResolution = 16, |
| 165 | .bSamFreqType = 1, |
| 166 | }; |
| 167 | |
| 168 | /* Standard ISO OUT Endpoint Descriptor */ |
Martin Jackson | e6251a9 | 2011-05-17 11:02:06 +0200 | [diff] [blame] | 169 | static struct usb_endpoint_descriptor as_out_ep_desc = { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 170 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
| 171 | .bDescriptorType = USB_DT_ENDPOINT, |
| 172 | .bEndpointAddress = USB_DIR_OUT, |
Laurent Pinchart | 85e08ca | 2009-06-21 23:19:23 +0200 | [diff] [blame] | 173 | .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 174 | | USB_ENDPOINT_XFER_ISOC, |
Andrzej Pietrasiewicz | bcec978 | 2014-07-22 19:58:42 +0200 | [diff] [blame] | 175 | .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 176 | .bInterval = 4, |
| 177 | }; |
| 178 | |
| 179 | /* Class-specific AS ISO OUT Endpoint Descriptor */ |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 180 | static struct uac_iso_endpoint_descriptor as_iso_out_desc = { |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 181 | .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 182 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 183 | .bDescriptorSubtype = UAC_EP_GENERAL, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 184 | .bmAttributes = 1, |
| 185 | .bLockDelayUnits = 1, |
| 186 | .wLockDelay = __constant_cpu_to_le16(1), |
| 187 | }; |
| 188 | |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 189 | static struct usb_descriptor_header *f_audio_desc[] = { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 190 | (struct usb_descriptor_header *)&ac_interface_desc, |
| 191 | (struct usb_descriptor_header *)&ac_header_desc, |
| 192 | |
| 193 | (struct usb_descriptor_header *)&input_terminal_desc, |
| 194 | (struct usb_descriptor_header *)&output_terminal_desc, |
| 195 | (struct usb_descriptor_header *)&feature_unit_desc, |
| 196 | |
| 197 | (struct usb_descriptor_header *)&as_interface_alt_0_desc, |
| 198 | (struct usb_descriptor_header *)&as_interface_alt_1_desc, |
| 199 | (struct usb_descriptor_header *)&as_header_desc, |
| 200 | |
| 201 | (struct usb_descriptor_header *)&as_type_i_desc, |
| 202 | |
| 203 | (struct usb_descriptor_header *)&as_out_ep_desc, |
| 204 | (struct usb_descriptor_header *)&as_iso_out_desc, |
| 205 | NULL, |
| 206 | }; |
| 207 | |
Andrzej Pietrasiewicz | f73db69 | 2014-07-22 19:58:36 +0200 | [diff] [blame] | 208 | enum { |
| 209 | STR_AC_IF, |
| 210 | STR_INPUT_TERMINAL, |
| 211 | STR_INPUT_TERMINAL_CH_NAMES, |
| 212 | STR_FEAT_DESC_0, |
| 213 | STR_OUTPUT_TERMINAL, |
| 214 | STR_AS_IF_ALT0, |
| 215 | STR_AS_IF_ALT1, |
| 216 | }; |
| 217 | |
| 218 | static struct usb_string strings_uac1[] = { |
| 219 | [STR_AC_IF].s = "AC Interface", |
| 220 | [STR_INPUT_TERMINAL].s = "Input terminal", |
| 221 | [STR_INPUT_TERMINAL_CH_NAMES].s = "Channels", |
| 222 | [STR_FEAT_DESC_0].s = "Volume control & mute", |
| 223 | [STR_OUTPUT_TERMINAL].s = "Output terminal", |
| 224 | [STR_AS_IF_ALT0].s = "AS Interface", |
| 225 | [STR_AS_IF_ALT1].s = "AS Interface", |
| 226 | { }, |
| 227 | }; |
| 228 | |
| 229 | static struct usb_gadget_strings str_uac1 = { |
| 230 | .language = 0x0409, /* en-us */ |
| 231 | .strings = strings_uac1, |
| 232 | }; |
| 233 | |
| 234 | static struct usb_gadget_strings *uac1_strings[] = { |
| 235 | &str_uac1, |
| 236 | NULL, |
| 237 | }; |
| 238 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 239 | /* |
| 240 | * This function is an ALSA sound card following USB Audio Class Spec 1.0. |
| 241 | */ |
| 242 | |
| 243 | /*-------------------------------------------------------------------------*/ |
| 244 | struct f_audio_buf { |
| 245 | u8 *buf; |
| 246 | int actual; |
| 247 | struct list_head list; |
| 248 | }; |
| 249 | |
| 250 | static struct f_audio_buf *f_audio_buffer_alloc(int buf_size) |
| 251 | { |
| 252 | struct f_audio_buf *copy_buf; |
| 253 | |
| 254 | copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC); |
| 255 | if (!copy_buf) |
Julia Lawall | ff3b968 | 2009-12-09 14:23:32 +0100 | [diff] [blame] | 256 | return ERR_PTR(-ENOMEM); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 257 | |
| 258 | copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC); |
| 259 | if (!copy_buf->buf) { |
| 260 | kfree(copy_buf); |
Julia Lawall | ff3b968 | 2009-12-09 14:23:32 +0100 | [diff] [blame] | 261 | return ERR_PTR(-ENOMEM); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return copy_buf; |
| 265 | } |
| 266 | |
| 267 | static void f_audio_buffer_free(struct f_audio_buf *audio_buf) |
| 268 | { |
| 269 | kfree(audio_buf->buf); |
| 270 | kfree(audio_buf); |
| 271 | } |
| 272 | /*-------------------------------------------------------------------------*/ |
| 273 | |
| 274 | struct f_audio { |
| 275 | struct gaudio card; |
| 276 | |
Ruslan Bilovol | 1fc4926 | 2017-06-18 00:23:58 +0300 | [diff] [blame] | 277 | u8 ac_intf, ac_alt; |
| 278 | u8 as_intf, as_alt; |
| 279 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 280 | /* endpoints handle full and/or high speeds */ |
| 281 | struct usb_ep *out_ep; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 282 | |
| 283 | spinlock_t lock; |
| 284 | struct f_audio_buf *copy_buf; |
| 285 | struct work_struct playback_work; |
| 286 | struct list_head play_queue; |
| 287 | |
| 288 | /* Control Set command */ |
| 289 | struct list_head cs; |
| 290 | u8 set_cmd; |
| 291 | struct usb_audio_control *set_con; |
| 292 | }; |
| 293 | |
| 294 | static inline struct f_audio *func_to_audio(struct usb_function *f) |
| 295 | { |
| 296 | return container_of(f, struct f_audio, card.func); |
| 297 | } |
| 298 | |
| 299 | /*-------------------------------------------------------------------------*/ |
| 300 | |
| 301 | static void f_audio_playback_work(struct work_struct *data) |
| 302 | { |
| 303 | struct f_audio *audio = container_of(data, struct f_audio, |
| 304 | playback_work); |
| 305 | struct f_audio_buf *play_buf; |
| 306 | |
| 307 | spin_lock_irq(&audio->lock); |
| 308 | if (list_empty(&audio->play_queue)) { |
| 309 | spin_unlock_irq(&audio->lock); |
| 310 | return; |
| 311 | } |
| 312 | play_buf = list_first_entry(&audio->play_queue, |
| 313 | struct f_audio_buf, list); |
| 314 | list_del(&play_buf->list); |
| 315 | spin_unlock_irq(&audio->lock); |
| 316 | |
| 317 | u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); |
| 318 | f_audio_buffer_free(play_buf); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req) |
| 322 | { |
| 323 | struct f_audio *audio = req->context; |
| 324 | struct usb_composite_dev *cdev = audio->card.func.config->cdev; |
| 325 | struct f_audio_buf *copy_buf = audio->copy_buf; |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 326 | struct f_uac1_legacy_opts *opts; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 327 | int audio_buf_size; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 328 | int err; |
| 329 | |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 330 | opts = container_of(audio->card.func.fi, struct f_uac1_legacy_opts, |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 331 | func_inst); |
| 332 | audio_buf_size = opts->audio_buf_size; |
Andrzej Pietrasiewicz | 605ef83 | 2014-07-22 19:58:40 +0200 | [diff] [blame] | 333 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 334 | if (!copy_buf) |
| 335 | return -EINVAL; |
| 336 | |
| 337 | /* Copy buffer is full, add it to the play_queue */ |
| 338 | if (audio_buf_size - copy_buf->actual < req->actual) { |
| 339 | list_add_tail(©_buf->list, &audio->play_queue); |
| 340 | schedule_work(&audio->playback_work); |
| 341 | copy_buf = f_audio_buffer_alloc(audio_buf_size); |
Julia Lawall | ff3b968 | 2009-12-09 14:23:32 +0100 | [diff] [blame] | 342 | if (IS_ERR(copy_buf)) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 343 | return -ENOMEM; |
| 344 | } |
| 345 | |
| 346 | memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual); |
| 347 | copy_buf->actual += req->actual; |
| 348 | audio->copy_buf = copy_buf; |
| 349 | |
| 350 | err = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 351 | if (err) |
| 352 | ERROR(cdev, "%s queue req: %d\n", ep->name, err); |
| 353 | |
| 354 | return 0; |
| 355 | |
| 356 | } |
| 357 | |
| 358 | static void f_audio_complete(struct usb_ep *ep, struct usb_request *req) |
| 359 | { |
| 360 | struct f_audio *audio = req->context; |
| 361 | int status = req->status; |
| 362 | u32 data = 0; |
| 363 | struct usb_ep *out_ep = audio->out_ep; |
| 364 | |
| 365 | switch (status) { |
| 366 | |
| 367 | case 0: /* normal completion? */ |
| 368 | if (ep == out_ep) |
| 369 | f_audio_out_ep_complete(ep, req); |
| 370 | else if (audio->set_con) { |
| 371 | memcpy(&data, req->buf, req->length); |
| 372 | audio->set_con->set(audio->set_con, audio->set_cmd, |
| 373 | le16_to_cpu(data)); |
| 374 | audio->set_con = NULL; |
| 375 | } |
| 376 | break; |
| 377 | default: |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static int audio_set_intf_req(struct usb_function *f, |
| 383 | const struct usb_ctrlrequest *ctrl) |
| 384 | { |
| 385 | struct f_audio *audio = func_to_audio(f); |
| 386 | struct usb_composite_dev *cdev = f->config->cdev; |
| 387 | struct usb_request *req = cdev->req; |
| 388 | u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); |
| 389 | u16 len = le16_to_cpu(ctrl->wLength); |
| 390 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 391 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 392 | u8 cmd = (ctrl->bRequest & 0x0F); |
| 393 | struct usb_audio_control_selector *cs; |
| 394 | struct usb_audio_control *con; |
| 395 | |
| 396 | DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", |
| 397 | ctrl->bRequest, w_value, len, id); |
| 398 | |
| 399 | list_for_each_entry(cs, &audio->cs, list) { |
| 400 | if (cs->id == id) { |
| 401 | list_for_each_entry(con, &cs->control, list) { |
| 402 | if (con->type == con_sel) { |
| 403 | audio->set_con = con; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | audio->set_cmd = cmd; |
| 412 | req->context = audio; |
| 413 | req->complete = f_audio_complete; |
| 414 | |
| 415 | return len; |
| 416 | } |
| 417 | |
| 418 | static int audio_get_intf_req(struct usb_function *f, |
| 419 | const struct usb_ctrlrequest *ctrl) |
| 420 | { |
| 421 | struct f_audio *audio = func_to_audio(f); |
| 422 | struct usb_composite_dev *cdev = f->config->cdev; |
| 423 | struct usb_request *req = cdev->req; |
| 424 | int value = -EOPNOTSUPP; |
| 425 | u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); |
| 426 | u16 len = le16_to_cpu(ctrl->wLength); |
| 427 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 428 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 429 | u8 cmd = (ctrl->bRequest & 0x0F); |
| 430 | struct usb_audio_control_selector *cs; |
| 431 | struct usb_audio_control *con; |
| 432 | |
| 433 | DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", |
| 434 | ctrl->bRequest, w_value, len, id); |
| 435 | |
| 436 | list_for_each_entry(cs, &audio->cs, list) { |
| 437 | if (cs->id == id) { |
| 438 | list_for_each_entry(con, &cs->control, list) { |
| 439 | if (con->type == con_sel && con->get) { |
| 440 | value = con->get(con, cmd); |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | req->context = audio; |
| 449 | req->complete = f_audio_complete; |
Dan Carpenter | fddedd8 | 2013-02-28 07:43:12 +0300 | [diff] [blame] | 450 | len = min_t(size_t, sizeof(value), len); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 451 | memcpy(req->buf, &value, len); |
| 452 | |
| 453 | return len; |
| 454 | } |
| 455 | |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 456 | static int audio_set_endpoint_req(struct usb_function *f, |
| 457 | const struct usb_ctrlrequest *ctrl) |
| 458 | { |
| 459 | struct usb_composite_dev *cdev = f->config->cdev; |
| 460 | int value = -EOPNOTSUPP; |
| 461 | u16 ep = le16_to_cpu(ctrl->wIndex); |
| 462 | u16 len = le16_to_cpu(ctrl->wLength); |
| 463 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 464 | |
| 465 | DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", |
| 466 | ctrl->bRequest, w_value, len, ep); |
| 467 | |
| 468 | switch (ctrl->bRequest) { |
| 469 | case UAC_SET_CUR: |
Felipe Balbi | 7c5881d | 2011-08-29 11:54:08 +0300 | [diff] [blame] | 470 | value = len; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 471 | break; |
| 472 | |
| 473 | case UAC_SET_MIN: |
| 474 | break; |
| 475 | |
| 476 | case UAC_SET_MAX: |
| 477 | break; |
| 478 | |
| 479 | case UAC_SET_RES: |
| 480 | break; |
| 481 | |
| 482 | case UAC_SET_MEM: |
| 483 | break; |
| 484 | |
| 485 | default: |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | return value; |
| 490 | } |
| 491 | |
| 492 | static int audio_get_endpoint_req(struct usb_function *f, |
| 493 | const struct usb_ctrlrequest *ctrl) |
| 494 | { |
| 495 | struct usb_composite_dev *cdev = f->config->cdev; |
| 496 | int value = -EOPNOTSUPP; |
| 497 | u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); |
| 498 | u16 len = le16_to_cpu(ctrl->wLength); |
| 499 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 500 | |
| 501 | DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", |
| 502 | ctrl->bRequest, w_value, len, ep); |
| 503 | |
| 504 | switch (ctrl->bRequest) { |
| 505 | case UAC_GET_CUR: |
| 506 | case UAC_GET_MIN: |
| 507 | case UAC_GET_MAX: |
| 508 | case UAC_GET_RES: |
Felipe Balbi | 7c5881d | 2011-08-29 11:54:08 +0300 | [diff] [blame] | 509 | value = len; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 510 | break; |
| 511 | case UAC_GET_MEM: |
| 512 | break; |
| 513 | default: |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | return value; |
| 518 | } |
| 519 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 520 | static int |
| 521 | f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 522 | { |
| 523 | struct usb_composite_dev *cdev = f->config->cdev; |
| 524 | struct usb_request *req = cdev->req; |
| 525 | int value = -EOPNOTSUPP; |
| 526 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 527 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 528 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 529 | |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 530 | /* composite driver infrastructure handles everything; interface |
| 531 | * activation uses set_alt(). |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 532 | */ |
| 533 | switch (ctrl->bRequestType) { |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 534 | case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 535 | value = audio_set_intf_req(f, ctrl); |
| 536 | break; |
| 537 | |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 538 | case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE: |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 539 | value = audio_get_intf_req(f, ctrl); |
| 540 | break; |
| 541 | |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 542 | case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
| 543 | value = audio_set_endpoint_req(f, ctrl); |
| 544 | break; |
| 545 | |
| 546 | case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
| 547 | value = audio_get_endpoint_req(f, ctrl); |
| 548 | break; |
| 549 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 550 | default: |
| 551 | ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", |
| 552 | ctrl->bRequestType, ctrl->bRequest, |
| 553 | w_value, w_index, w_length); |
| 554 | } |
| 555 | |
| 556 | /* respond with data transfer or status phase? */ |
| 557 | if (value >= 0) { |
| 558 | DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n", |
| 559 | ctrl->bRequestType, ctrl->bRequest, |
| 560 | w_value, w_index, w_length); |
| 561 | req->zero = 0; |
| 562 | req->length = value; |
| 563 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 564 | if (value < 0) |
| 565 | ERROR(cdev, "audio response on err %d\n", value); |
| 566 | } |
| 567 | |
| 568 | /* device either stalls (value < 0) or reports success */ |
| 569 | return value; |
| 570 | } |
| 571 | |
| 572 | static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 573 | { |
| 574 | struct f_audio *audio = func_to_audio(f); |
| 575 | struct usb_composite_dev *cdev = f->config->cdev; |
| 576 | struct usb_ep *out_ep = audio->out_ep; |
| 577 | struct usb_request *req; |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 578 | struct f_uac1_legacy_opts *opts; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 579 | int req_buf_size, req_count, audio_buf_size; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 580 | int i = 0, err = 0; |
| 581 | |
| 582 | DBG(cdev, "intf %d, alt %d\n", intf, alt); |
| 583 | |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 584 | opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 585 | req_buf_size = opts->req_buf_size; |
| 586 | req_count = opts->req_count; |
| 587 | audio_buf_size = opts->audio_buf_size; |
| 588 | |
Ruslan Bilovol | 1fc4926 | 2017-06-18 00:23:58 +0300 | [diff] [blame] | 589 | /* No i/f has more than 2 alt settings */ |
| 590 | if (alt > 1) { |
| 591 | ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__); |
| 592 | return -EINVAL; |
| 593 | } |
| 594 | |
| 595 | if (intf == audio->ac_intf) { |
| 596 | /* Control I/f has only 1 AltSetting - 0 */ |
| 597 | if (alt) { |
| 598 | ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__); |
| 599 | return -EINVAL; |
| 600 | } |
| 601 | return 0; |
| 602 | } else if (intf == audio->as_intf) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 603 | if (alt == 1) { |
Michael Trimarchi | ca4de53 | 2015-05-18 17:28:58 +0200 | [diff] [blame] | 604 | err = config_ep_by_speed(cdev->gadget, f, out_ep); |
| 605 | if (err) |
| 606 | return err; |
| 607 | |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 608 | usb_ep_enable(out_ep); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 609 | audio->copy_buf = f_audio_buffer_alloc(audio_buf_size); |
Julia Lawall | ff3b968 | 2009-12-09 14:23:32 +0100 | [diff] [blame] | 610 | if (IS_ERR(audio->copy_buf)) |
| 611 | return -ENOMEM; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * allocate a bunch of read buffers |
| 615 | * and queue them all at once. |
| 616 | */ |
| 617 | for (i = 0; i < req_count && err == 0; i++) { |
| 618 | req = usb_ep_alloc_request(out_ep, GFP_ATOMIC); |
| 619 | if (req) { |
| 620 | req->buf = kzalloc(req_buf_size, |
| 621 | GFP_ATOMIC); |
| 622 | if (req->buf) { |
| 623 | req->length = req_buf_size; |
| 624 | req->context = audio; |
| 625 | req->complete = |
| 626 | f_audio_complete; |
| 627 | err = usb_ep_queue(out_ep, |
| 628 | req, GFP_ATOMIC); |
| 629 | if (err) |
| 630 | ERROR(cdev, |
| 631 | "%s queue req: %d\n", |
| 632 | out_ep->name, err); |
| 633 | } else |
| 634 | err = -ENOMEM; |
| 635 | } else |
| 636 | err = -ENOMEM; |
| 637 | } |
| 638 | |
| 639 | } else { |
| 640 | struct f_audio_buf *copy_buf = audio->copy_buf; |
| 641 | if (copy_buf) { |
| 642 | list_add_tail(©_buf->list, |
| 643 | &audio->play_queue); |
| 644 | schedule_work(&audio->playback_work); |
| 645 | } |
| 646 | } |
Ruslan Bilovol | 1fc4926 | 2017-06-18 00:23:58 +0300 | [diff] [blame] | 647 | audio->as_alt = alt; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | return err; |
| 651 | } |
| 652 | |
Ruslan Bilovol | 1fc4926 | 2017-06-18 00:23:58 +0300 | [diff] [blame] | 653 | static int f_audio_get_alt(struct usb_function *f, unsigned intf) |
| 654 | { |
| 655 | struct f_audio *audio = func_to_audio(f); |
| 656 | struct usb_composite_dev *cdev = f->config->cdev; |
| 657 | |
| 658 | if (intf == audio->ac_intf) |
| 659 | return audio->ac_alt; |
| 660 | else if (intf == audio->as_intf) |
| 661 | return audio->as_alt; |
| 662 | else |
| 663 | ERROR(cdev, "%s:%d Invalid Interface %d!\n", |
| 664 | __func__, __LINE__, intf); |
| 665 | |
| 666 | return -EINVAL; |
| 667 | } |
| 668 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 669 | static void f_audio_disable(struct usb_function *f) |
| 670 | { |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | /*-------------------------------------------------------------------------*/ |
| 675 | |
| 676 | static void f_audio_build_desc(struct f_audio *audio) |
| 677 | { |
| 678 | struct gaudio *card = &audio->card; |
| 679 | u8 *sam_freq; |
| 680 | int rate; |
| 681 | |
| 682 | /* Set channel numbers */ |
| 683 | input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card); |
| 684 | as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card); |
| 685 | |
| 686 | /* Set sample rates */ |
| 687 | rate = u_audio_get_playback_rate(card); |
| 688 | sam_freq = as_type_i_desc.tSamFreq[0]; |
| 689 | memcpy(sam_freq, &rate, 3); |
| 690 | |
| 691 | /* Todo: Set Sample bits and other parameters */ |
| 692 | |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | /* audio function driver setup/binding */ |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 697 | static int |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 698 | f_audio_bind(struct usb_configuration *c, struct usb_function *f) |
| 699 | { |
| 700 | struct usb_composite_dev *cdev = c->cdev; |
| 701 | struct f_audio *audio = func_to_audio(f); |
Andrzej Pietrasiewicz | 807dccd | 2014-07-22 19:58:41 +0200 | [diff] [blame] | 702 | struct usb_string *us; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 703 | int status; |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 704 | struct usb_ep *ep = NULL; |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 705 | struct f_uac1_legacy_opts *audio_opts; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 706 | |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 707 | audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 708 | audio->card.gadget = c->cdev->gadget; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 709 | /* set up ASLA audio devices */ |
| 710 | if (!audio_opts->bound) { |
| 711 | status = gaudio_setup(&audio->card); |
| 712 | if (status < 0) |
| 713 | return status; |
| 714 | audio_opts->bound = true; |
| 715 | } |
Andrzej Pietrasiewicz | 807dccd | 2014-07-22 19:58:41 +0200 | [diff] [blame] | 716 | us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1)); |
| 717 | if (IS_ERR(us)) |
| 718 | return PTR_ERR(us); |
| 719 | ac_interface_desc.iInterface = us[STR_AC_IF].id; |
| 720 | input_terminal_desc.iTerminal = us[STR_INPUT_TERMINAL].id; |
| 721 | input_terminal_desc.iChannelNames = us[STR_INPUT_TERMINAL_CH_NAMES].id; |
| 722 | feature_unit_desc.iFeature = us[STR_FEAT_DESC_0].id; |
| 723 | output_terminal_desc.iTerminal = us[STR_OUTPUT_TERMINAL].id; |
| 724 | as_interface_alt_0_desc.iInterface = us[STR_AS_IF_ALT0].id; |
| 725 | as_interface_alt_1_desc.iInterface = us[STR_AS_IF_ALT1].id; |
| 726 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 727 | |
| 728 | f_audio_build_desc(audio); |
| 729 | |
| 730 | /* allocate instance-specific interface IDs, and patch descriptors */ |
| 731 | status = usb_interface_id(c, f); |
| 732 | if (status < 0) |
| 733 | goto fail; |
| 734 | ac_interface_desc.bInterfaceNumber = status; |
Ruslan Bilovol | 1fc4926 | 2017-06-18 00:23:58 +0300 | [diff] [blame] | 735 | audio->ac_intf = status; |
| 736 | audio->ac_alt = 0; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 737 | |
| 738 | status = usb_interface_id(c, f); |
| 739 | if (status < 0) |
| 740 | goto fail; |
| 741 | as_interface_alt_0_desc.bInterfaceNumber = status; |
| 742 | as_interface_alt_1_desc.bInterfaceNumber = status; |
Ruslan Bilovol | 1fc4926 | 2017-06-18 00:23:58 +0300 | [diff] [blame] | 743 | audio->as_intf = status; |
| 744 | audio->as_alt = 0; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 745 | |
| 746 | status = -ENODEV; |
| 747 | |
| 748 | /* allocate instance-specific endpoints */ |
| 749 | ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc); |
| 750 | if (!ep) |
| 751 | goto fail; |
| 752 | audio->out_ep = ep; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 753 | audio->out_ep->desc = &as_out_ep_desc; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 754 | |
| 755 | status = -ENOMEM; |
| 756 | |
Felipe Balbi | ef7f584 | 2011-08-26 12:48:15 +0300 | [diff] [blame] | 757 | /* copy descriptors, and track endpoint copies */ |
John Youn | eaef50c | 2016-02-05 17:06:07 -0800 | [diff] [blame] | 758 | status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL, |
| 759 | NULL); |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 760 | if (status) |
| 761 | goto fail; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 762 | return 0; |
| 763 | |
| 764 | fail: |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 765 | gaudio_cleanup(&audio->card); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 766 | return status; |
| 767 | } |
| 768 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 769 | /*-------------------------------------------------------------------------*/ |
| 770 | |
Laurent Pinchart | b95cd7e | 2009-06-21 23:21:55 +0200 | [diff] [blame] | 771 | static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value) |
| 772 | { |
| 773 | con->data[cmd] = value; |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static int generic_get_cmd(struct usb_audio_control *con, u8 cmd) |
| 779 | { |
| 780 | return con->data[cmd]; |
| 781 | } |
| 782 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 783 | /* Todo: add more control selecotor dynamically */ |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 784 | static int control_selector_init(struct f_audio *audio) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 785 | { |
| 786 | INIT_LIST_HEAD(&audio->cs); |
| 787 | list_add(&feature_unit.list, &audio->cs); |
| 788 | |
| 789 | INIT_LIST_HEAD(&feature_unit.control); |
| 790 | list_add(&mute_control.list, &feature_unit.control); |
| 791 | list_add(&volume_control.list, &feature_unit.control); |
| 792 | |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 793 | volume_control.data[UAC__CUR] = 0xffc0; |
| 794 | volume_control.data[UAC__MIN] = 0xe3a0; |
| 795 | volume_control.data[UAC__MAX] = 0xfff0; |
| 796 | volume_control.data[UAC__RES] = 0x0030; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 801 | static inline |
| 802 | struct f_uac1_legacy_opts *to_f_uac1_opts(struct config_item *item) |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 803 | { |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 804 | return container_of(to_config_group(item), struct f_uac1_legacy_opts, |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 805 | func_inst.group); |
| 806 | } |
| 807 | |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 808 | static void f_uac1_attr_release(struct config_item *item) |
| 809 | { |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 810 | struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 811 | |
| 812 | usb_put_function_instance(&opts->func_inst); |
| 813 | } |
| 814 | |
| 815 | static struct configfs_item_operations f_uac1_item_ops = { |
| 816 | .release = f_uac1_attr_release, |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 817 | }; |
| 818 | |
| 819 | #define UAC1_INT_ATTRIBUTE(name) \ |
Christoph Hellwig | c6f89f1 | 2015-10-03 15:32:48 +0200 | [diff] [blame] | 820 | static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 821 | char *page) \ |
| 822 | { \ |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 823 | struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 824 | int result; \ |
| 825 | \ |
| 826 | mutex_lock(&opts->lock); \ |
| 827 | result = sprintf(page, "%u\n", opts->name); \ |
| 828 | mutex_unlock(&opts->lock); \ |
| 829 | \ |
| 830 | return result; \ |
| 831 | } \ |
| 832 | \ |
Christoph Hellwig | c6f89f1 | 2015-10-03 15:32:48 +0200 | [diff] [blame] | 833 | static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 834 | const char *page, size_t len) \ |
| 835 | { \ |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 836 | struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 837 | int ret; \ |
| 838 | u32 num; \ |
| 839 | \ |
| 840 | mutex_lock(&opts->lock); \ |
| 841 | if (opts->refcnt) { \ |
| 842 | ret = -EBUSY; \ |
| 843 | goto end; \ |
| 844 | } \ |
| 845 | \ |
| 846 | ret = kstrtou32(page, 0, &num); \ |
| 847 | if (ret) \ |
| 848 | goto end; \ |
| 849 | \ |
| 850 | opts->name = num; \ |
| 851 | ret = len; \ |
| 852 | \ |
| 853 | end: \ |
| 854 | mutex_unlock(&opts->lock); \ |
| 855 | return ret; \ |
| 856 | } \ |
| 857 | \ |
Christoph Hellwig | c6f89f1 | 2015-10-03 15:32:48 +0200 | [diff] [blame] | 858 | CONFIGFS_ATTR(f_uac1_opts_, name) |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 859 | |
| 860 | UAC1_INT_ATTRIBUTE(req_buf_size); |
| 861 | UAC1_INT_ATTRIBUTE(req_count); |
| 862 | UAC1_INT_ATTRIBUTE(audio_buf_size); |
| 863 | |
| 864 | #define UAC1_STR_ATTRIBUTE(name) \ |
Christoph Hellwig | c6f89f1 | 2015-10-03 15:32:48 +0200 | [diff] [blame] | 865 | static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 866 | char *page) \ |
| 867 | { \ |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 868 | struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 869 | int result; \ |
| 870 | \ |
| 871 | mutex_lock(&opts->lock); \ |
| 872 | result = sprintf(page, "%s\n", opts->name); \ |
| 873 | mutex_unlock(&opts->lock); \ |
| 874 | \ |
| 875 | return result; \ |
| 876 | } \ |
| 877 | \ |
Christoph Hellwig | c6f89f1 | 2015-10-03 15:32:48 +0200 | [diff] [blame] | 878 | static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 879 | const char *page, size_t len) \ |
| 880 | { \ |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 881 | struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \ |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 882 | int ret = -EBUSY; \ |
| 883 | char *tmp; \ |
| 884 | \ |
| 885 | mutex_lock(&opts->lock); \ |
| 886 | if (opts->refcnt) \ |
| 887 | goto end; \ |
| 888 | \ |
| 889 | tmp = kstrndup(page, len, GFP_KERNEL); \ |
| 890 | if (tmp) { \ |
| 891 | ret = -ENOMEM; \ |
| 892 | goto end; \ |
| 893 | } \ |
| 894 | if (opts->name##_alloc) \ |
| 895 | kfree(opts->name); \ |
| 896 | opts->name##_alloc = true; \ |
| 897 | opts->name = tmp; \ |
| 898 | ret = len; \ |
| 899 | \ |
| 900 | end: \ |
| 901 | mutex_unlock(&opts->lock); \ |
| 902 | return ret; \ |
| 903 | } \ |
| 904 | \ |
Christoph Hellwig | c6f89f1 | 2015-10-03 15:32:48 +0200 | [diff] [blame] | 905 | CONFIGFS_ATTR(f_uac1_opts_, name) |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 906 | |
| 907 | UAC1_STR_ATTRIBUTE(fn_play); |
| 908 | UAC1_STR_ATTRIBUTE(fn_cap); |
| 909 | UAC1_STR_ATTRIBUTE(fn_cntl); |
| 910 | |
| 911 | static struct configfs_attribute *f_uac1_attrs[] = { |
Christoph Hellwig | c6f89f1 | 2015-10-03 15:32:48 +0200 | [diff] [blame] | 912 | &f_uac1_opts_attr_req_buf_size, |
| 913 | &f_uac1_opts_attr_req_count, |
| 914 | &f_uac1_opts_attr_audio_buf_size, |
| 915 | &f_uac1_opts_attr_fn_play, |
| 916 | &f_uac1_opts_attr_fn_cap, |
| 917 | &f_uac1_opts_attr_fn_cntl, |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 918 | NULL, |
| 919 | }; |
| 920 | |
Bhumika Goyal | 9736390 | 2017-10-16 17:18:41 +0200 | [diff] [blame] | 921 | static const struct config_item_type f_uac1_func_type = { |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 922 | .ct_item_ops = &f_uac1_item_ops, |
| 923 | .ct_attrs = f_uac1_attrs, |
| 924 | .ct_owner = THIS_MODULE, |
| 925 | }; |
| 926 | |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 927 | static void f_audio_free_inst(struct usb_function_instance *f) |
| 928 | { |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 929 | struct f_uac1_legacy_opts *opts; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 930 | |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 931 | opts = container_of(f, struct f_uac1_legacy_opts, func_inst); |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 932 | if (opts->fn_play_alloc) |
| 933 | kfree(opts->fn_play); |
| 934 | if (opts->fn_cap_alloc) |
| 935 | kfree(opts->fn_cap); |
| 936 | if (opts->fn_cntl_alloc) |
| 937 | kfree(opts->fn_cntl); |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 938 | kfree(opts); |
| 939 | } |
| 940 | |
| 941 | static struct usb_function_instance *f_audio_alloc_inst(void) |
| 942 | { |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 943 | struct f_uac1_legacy_opts *opts; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 944 | |
| 945 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 946 | if (!opts) |
| 947 | return ERR_PTR(-ENOMEM); |
| 948 | |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 949 | mutex_init(&opts->lock); |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 950 | opts->func_inst.free_func_inst = f_audio_free_inst; |
| 951 | |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 952 | config_group_init_type_name(&opts->func_inst.group, "", |
| 953 | &f_uac1_func_type); |
| 954 | |
| 955 | opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE; |
| 956 | opts->req_count = UAC1_REQ_COUNT; |
| 957 | opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE; |
| 958 | opts->fn_play = FILE_PCM_PLAYBACK; |
| 959 | opts->fn_cap = FILE_PCM_CAPTURE; |
| 960 | opts->fn_cntl = FILE_CONTROL; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 961 | return &opts->func_inst; |
| 962 | } |
| 963 | |
| 964 | static void f_audio_free(struct usb_function *f) |
| 965 | { |
| 966 | struct f_audio *audio = func_to_audio(f); |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 967 | struct f_uac1_legacy_opts *opts; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 968 | |
Peter Chen | 4fde620 | 2014-12-01 16:09:27 +0800 | [diff] [blame] | 969 | gaudio_cleanup(&audio->card); |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 970 | opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 971 | kfree(audio); |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 972 | mutex_lock(&opts->lock); |
| 973 | --opts->refcnt; |
| 974 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f) |
| 978 | { |
| 979 | usb_free_all_descriptors(f); |
| 980 | } |
| 981 | |
| 982 | static struct usb_function *f_audio_alloc(struct usb_function_instance *fi) |
| 983 | { |
| 984 | struct f_audio *audio; |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 985 | struct f_uac1_legacy_opts *opts; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 986 | |
| 987 | /* allocate and initialize one new instance */ |
| 988 | audio = kzalloc(sizeof(*audio), GFP_KERNEL); |
| 989 | if (!audio) |
| 990 | return ERR_PTR(-ENOMEM); |
| 991 | |
| 992 | audio->card.func.name = "g_audio"; |
| 993 | |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 994 | opts = container_of(fi, struct f_uac1_legacy_opts, func_inst); |
Andrzej Pietrasiewicz | 0854611 | 2014-07-22 19:58:43 +0200 | [diff] [blame] | 995 | mutex_lock(&opts->lock); |
| 996 | ++opts->refcnt; |
| 997 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 998 | INIT_LIST_HEAD(&audio->play_queue); |
| 999 | spin_lock_init(&audio->lock); |
| 1000 | |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 1001 | audio->card.func.bind = f_audio_bind; |
| 1002 | audio->card.func.unbind = f_audio_unbind; |
| 1003 | audio->card.func.set_alt = f_audio_set_alt; |
Ruslan Bilovol | 1fc4926 | 2017-06-18 00:23:58 +0300 | [diff] [blame] | 1004 | audio->card.func.get_alt = f_audio_get_alt; |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 1005 | audio->card.func.setup = f_audio_setup; |
| 1006 | audio->card.func.disable = f_audio_disable; |
| 1007 | audio->card.func.free_func = f_audio_free; |
| 1008 | |
| 1009 | control_selector_init(audio); |
| 1010 | |
| 1011 | INIT_WORK(&audio->playback_work, f_audio_playback_work); |
| 1012 | |
| 1013 | return &audio->card.func; |
| 1014 | } |
| 1015 | |
Ruslan Bilovol | d355339 | 2017-06-18 16:23:53 +0300 | [diff] [blame] | 1016 | DECLARE_USB_FUNCTION_INIT(uac1_legacy, f_audio_alloc_inst, f_audio_alloc); |
Andrzej Pietrasiewicz | f3a3406 | 2014-07-22 19:58:38 +0200 | [diff] [blame] | 1017 | MODULE_LICENSE("GPL"); |
| 1018 | MODULE_AUTHOR("Bryan Wu"); |