Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1 | /* |
| 2 | * f_audio.c -- USB Audio class function driver |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2012, The Linux Foundation. All rights reserved. |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 5 | * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> |
| 6 | * Copyright (C) 2008 Analog Devices, Inc |
| 7 | * |
| 8 | * Enter bugs at http://blackfin.uclinux.org/ |
| 9 | * |
| 10 | * Licensed under the GPL-2 or later. |
| 11 | */ |
| 12 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 13 | #ifdef pr_fmt |
| 14 | #undef pr_fmt |
| 15 | #endif |
| 16 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 17 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | #include <linux/device.h> |
Arun Sharma | 6006349 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 21 | #include <linux/atomic.h> |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 22 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 23 | #include <sound/core.h> |
| 24 | #include <sound/initval.h> |
| 25 | |
Jassi Brar | 18b5b3b | 2012-02-02 21:59:53 +0530 | [diff] [blame] | 26 | #include "u_uac1.h" |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 27 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 28 | /* |
| 29 | * DESCRIPTORS ... most are static, but strings and full |
| 30 | * configuration descriptors are built on demand. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * We have two interfaces- AudioControl and AudioStreaming |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 35 | */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 36 | #define PLAYBACK_EP_MAX_PACKET_SIZE 32 |
| 37 | static int req_playback_buf_size = PLAYBACK_EP_MAX_PACKET_SIZE; |
| 38 | module_param(req_playback_buf_size, int, S_IRUGO); |
| 39 | MODULE_PARM_DESC(req_playback_buf_size, "ISO OUT endpoint (playback) request buffer size"); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 40 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 41 | static int req_playback_count = 48; |
| 42 | module_param(req_playback_count, int, S_IRUGO); |
| 43 | MODULE_PARM_DESC(req_playback_count, "ISO OUT endpoint (playback) request count"); |
| 44 | |
| 45 | static int audio_playback_buf_size = 256*32; |
| 46 | module_param(audio_playback_buf_size, int, S_IRUGO); |
| 47 | MODULE_PARM_DESC(audio_playback_buf_size, "Audio buffer size"); |
| 48 | |
| 49 | #define CAPTURE_EP_MAX_PACKET_SIZE 32 |
| 50 | static int req_capture_buf_size = CAPTURE_EP_MAX_PACKET_SIZE; |
| 51 | module_param(req_capture_buf_size, int, S_IRUGO); |
| 52 | MODULE_PARM_DESC(req_capture_buf_size, "ISO IN endpoint (capture) request buffer size"); |
| 53 | |
| 54 | static int req_capture_count = 48; |
| 55 | module_param(req_capture_count, int, S_IRUGO); |
| 56 | MODULE_PARM_DESC(req_capture_count, "ISO IN endpoint (capture) request count"); |
| 57 | |
| 58 | static int audio_capture_buf_size = 256*32; |
| 59 | module_param(audio_capture_buf_size, int, S_IRUGO); |
| 60 | MODULE_PARM_DESC(audio_capture_buf_size, "Microphone Audio buffer size"); |
| 61 | |
| 62 | static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value); |
| 63 | static int generic_get_cmd(struct usb_audio_control *con, u8 cmd); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 64 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 65 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 66 | #define SPEAKER_INPUT_TERMINAL_ID 3 |
| 67 | #define SPEAKER_OUTPUT_TERMINAL_ID 4 |
| 68 | |
| 69 | #define MICROPHONE_INPUT_TERMINAL_ID 1 |
| 70 | #define MICROPHONE_OUTPUT_TERMINAL_ID 2 |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | * We have two interfaces- AudioControl and AudioStreaming |
| 75 | */ |
| 76 | |
| 77 | #define F_AUDIO_INTERFACE_MICROPHONE 2 |
| 78 | #define F_AUDIO_INTERFACE_SPEAKER 3 |
| 79 | #define F_AUDIO_NUM_INTERFACES 2 |
| 80 | |
| 81 | /* B.3.1 Standard AC Interface Descriptor */ |
| 82 | struct usb_interface_descriptor ac_interface_desc = { |
| 83 | .bLength = USB_DT_INTERFACE_SIZE, |
| 84 | .bDescriptorType = USB_DT_INTERFACE, |
| 85 | .bNumEndpoints = 0, |
| 86 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 87 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 88 | }; |
| 89 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 90 | #define TOTAL_LENGTH ( \ |
| 91 | UAC_DT_AC_HEADER_SIZE(2) + \ |
| 92 | UAC_DT_INPUT_TERMINAL_SIZE + \ |
| 93 | UAC_DT_OUTPUT_TERMINAL_SIZE + \ |
| 94 | UAC_DT_INPUT_TERMINAL_SIZE + \ |
| 95 | UAC_DT_OUTPUT_TERMINAL_SIZE \ |
| 96 | ) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 97 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 98 | /* B.3.2 Class-Specific AC Interface Descriptor */ |
| 99 | struct uac1_ac_header_descriptor_2 ac_header_desc = { |
| 100 | .bLength = UAC_DT_AC_HEADER_SIZE(2), |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 101 | .bDescriptorType = USB_DT_CS_INTERFACE, |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 102 | .bDescriptorSubtype = UAC_HEADER, |
| 103 | .bcdADC = __constant_cpu_to_le16(0x0100), |
| 104 | .wTotalLength = __constant_cpu_to_le16(TOTAL_LENGTH), |
| 105 | .bInCollection = F_AUDIO_NUM_INTERFACES, |
| 106 | /*.baInterfaceNr = { |
| 107 | [0] = F_AUDIO_INTERFACE_MICROPHONE, |
| 108 | [1] = F_AUDIO_INTERFACE_SPEAKER, |
| 109 | } |
| 110 | */ |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 111 | }; |
| 112 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 113 | /*---------------------------------*/ |
| 114 | |
| 115 | struct uac_input_terminal_descriptor speaker_input_terminal_desc = { |
| 116 | .bLength = UAC_DT_INPUT_TERMINAL_SIZE, |
| 117 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 118 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, |
| 119 | .bTerminalID = SPEAKER_INPUT_TERMINAL_ID, |
| 120 | .wTerminalType = UAC_TERMINAL_STREAMING, |
| 121 | .bAssocTerminal = SPEAKER_OUTPUT_TERMINAL_ID, |
| 122 | .wChannelConfig = 0x3, |
| 123 | }; |
| 124 | |
| 125 | struct uac1_output_terminal_descriptor speaker_output_terminal_desc = { |
| 126 | .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, |
| 127 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 128 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, |
| 129 | .bTerminalID = SPEAKER_OUTPUT_TERMINAL_ID, |
| 130 | .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER, |
| 131 | .bAssocTerminal = SPEAKER_INPUT_TERMINAL_ID, |
| 132 | .bSourceID = SPEAKER_INPUT_TERMINAL_ID, |
| 133 | }; |
| 134 | |
| 135 | static struct usb_audio_control speaker_mute_control = { |
| 136 | .list = LIST_HEAD_INIT(speaker_mute_control.list), |
| 137 | .name = "Speaker Mute Control", |
Takashi Iwai | f07ff97 | 2010-06-01 07:42:03 +0200 | [diff] [blame] | 138 | .type = UAC_FU_MUTE, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 139 | /* Todo: add real Mute control code */ |
| 140 | .set = generic_set_cmd, |
| 141 | .get = generic_get_cmd, |
| 142 | }; |
| 143 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 144 | static struct usb_audio_control speaker_volume_control = { |
| 145 | .list = LIST_HEAD_INIT(speaker_volume_control.list), |
| 146 | .name = "Speaker Volume Control", |
Takashi Iwai | f07ff97 | 2010-06-01 07:42:03 +0200 | [diff] [blame] | 147 | .type = UAC_FU_VOLUME, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 148 | /* Todo: add real Volume control code */ |
| 149 | .set = generic_set_cmd, |
| 150 | .get = generic_get_cmd, |
| 151 | }; |
| 152 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 153 | static struct usb_audio_control_selector speaker_fu_controls = { |
| 154 | .list = LIST_HEAD_INIT(speaker_fu_controls.list), |
| 155 | .name = "Speaker Function Unit Controls", |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 156 | }; |
| 157 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 158 | static struct uac_input_terminal_descriptor microphone_input_terminal_desc = { |
| 159 | .bLength = UAC_DT_INPUT_TERMINAL_SIZE, |
| 160 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 161 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, |
| 162 | .bTerminalID = MICROPHONE_INPUT_TERMINAL_ID, |
| 163 | .wTerminalType = UAC_INPUT_TERMINAL_MICROPHONE, |
| 164 | .bAssocTerminal = MICROPHONE_OUTPUT_TERMINAL_ID, |
| 165 | .bNrChannels = 1, |
| 166 | .wChannelConfig = 0x3, |
| 167 | }; |
| 168 | |
| 169 | static struct |
| 170 | uac1_output_terminal_descriptor microphone_output_terminal_desc = { |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 171 | .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 172 | .bDescriptorType = USB_DT_CS_INTERFACE, |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 173 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 174 | .bTerminalID = MICROPHONE_OUTPUT_TERMINAL_ID, |
| 175 | .wTerminalType = UAC_TERMINAL_STREAMING, |
| 176 | .bAssocTerminal = MICROPHONE_INPUT_TERMINAL_ID, |
| 177 | .bSourceID = MICROPHONE_INPUT_TERMINAL_ID, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 178 | }; |
| 179 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 180 | static struct usb_audio_control microphone_mute_control = { |
| 181 | .list = LIST_HEAD_INIT(microphone_mute_control.list), |
| 182 | .name = "Microphone Mute Control", |
| 183 | .type = UAC_FU_MUTE, |
| 184 | /* Todo: add real Mute control code */ |
| 185 | .set = generic_set_cmd, |
| 186 | .get = generic_get_cmd, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 187 | }; |
| 188 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 189 | static struct usb_audio_control microphone_volume_control = { |
| 190 | .list = LIST_HEAD_INIT(microphone_volume_control.list), |
| 191 | .name = "Microphone Volume Control", |
| 192 | .type = UAC_FU_VOLUME, |
| 193 | /* Todo: add real Volume control code */ |
| 194 | .set = generic_set_cmd, |
| 195 | .get = generic_get_cmd, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 196 | }; |
| 197 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 198 | static struct usb_audio_control_selector microphone_fu_controls = { |
| 199 | .list = LIST_HEAD_INIT(microphone_fu_controls.list), |
| 200 | .name = "Microphone Feature Unit Controls", |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 201 | }; |
| 202 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 203 | /*---------------------------------*/ |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 204 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 205 | /* B.4.1 Standard AS Interface Descriptor */ |
| 206 | static struct usb_interface_descriptor speaker_as_interface_alt_0_desc = { |
| 207 | .bLength = USB_DT_INTERFACE_SIZE, |
| 208 | .bDescriptorType = USB_DT_INTERFACE, |
| 209 | .bAlternateSetting = 0, |
| 210 | .bNumEndpoints = 0, |
| 211 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 212 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 213 | }; |
| 214 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 215 | static struct usb_interface_descriptor speaker_as_interface_alt_1_desc = { |
| 216 | .bLength = USB_DT_INTERFACE_SIZE, |
| 217 | .bDescriptorType = USB_DT_INTERFACE, |
| 218 | .bAlternateSetting = 1, |
| 219 | .bNumEndpoints = 1, |
| 220 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 221 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 222 | }; |
| 223 | |
| 224 | /* B.4.2 Class-Specific AS Interface Descriptor */ |
| 225 | static struct uac1_as_header_descriptor speaker_as_header_desc = { |
| 226 | .bLength = UAC_DT_AS_HEADER_SIZE, |
| 227 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 228 | .bDescriptorSubtype = UAC_AS_GENERAL, |
| 229 | .bTerminalLink = SPEAKER_INPUT_TERMINAL_ID, |
| 230 | .bDelay = 1, |
| 231 | .wFormatTag = UAC_FORMAT_TYPE_I_PCM, |
| 232 | }; |
| 233 | |
| 234 | static struct uac_format_type_i_discrete_descriptor_1 speaker_as_type_i_desc = { |
| 235 | .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), |
| 236 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 237 | .bDescriptorSubtype = UAC_FORMAT_TYPE, |
| 238 | .bFormatType = UAC_FORMAT_TYPE_I, |
| 239 | .bSubframeSize = 2, |
| 240 | .bBitResolution = 16, |
| 241 | .bSamFreqType = 1, |
| 242 | }; |
| 243 | |
| 244 | /* Standard ISO OUT Endpoint Descriptor */ |
| 245 | static struct usb_endpoint_descriptor speaker_as_ep_out_desc = { |
| 246 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
| 247 | .bDescriptorType = USB_DT_ENDPOINT, |
| 248 | .bEndpointAddress = USB_DIR_OUT, |
| 249 | .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE | |
| 250 | USB_ENDPOINT_XFER_ISOC, |
| 251 | .wMaxPacketSize = |
| 252 | __constant_cpu_to_le16(PLAYBACK_EP_MAX_PACKET_SIZE), |
| 253 | .bInterval = 4, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | /* Class-specific AS ISO OUT Endpoint Descriptor */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 257 | static struct uac_iso_endpoint_descriptor speaker_as_iso_out_desc = { |
| 258 | .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, |
| 259 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
| 260 | .bDescriptorSubtype = UAC_EP_GENERAL, |
| 261 | .bmAttributes = 1, |
| 262 | .bLockDelayUnits = 1, |
| 263 | .wLockDelay = __constant_cpu_to_le16(1), |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 264 | }; |
| 265 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 266 | static struct usb_audio_control speaker_sample_freq_control = { |
| 267 | .list = LIST_HEAD_INIT(speaker_sample_freq_control.list), |
| 268 | .name = "Speaker Sampling Frequency Control", |
| 269 | .type = UAC_EP_CS_ATTR_SAMPLE_RATE, |
| 270 | .set = generic_set_cmd, |
| 271 | .get = generic_get_cmd, |
| 272 | }; |
| 273 | |
| 274 | static struct usb_audio_control_selector speaker_as_iso_out = { |
| 275 | .list = LIST_HEAD_INIT(speaker_as_iso_out.list), |
| 276 | .name = "Speaker Iso-out Endpoint Control", |
| 277 | .type = UAC_EP_GENERAL, |
| 278 | .desc = (struct usb_descriptor_header *)&speaker_as_iso_out_desc, |
| 279 | }; |
| 280 | |
| 281 | /*---------------------------------*/ |
| 282 | |
| 283 | /* B.4.1 Standard AS Interface Descriptor */ |
| 284 | static struct usb_interface_descriptor microphone_as_interface_alt_0_desc = { |
| 285 | .bLength = USB_DT_INTERFACE_SIZE, |
| 286 | .bDescriptorType = USB_DT_INTERFACE, |
| 287 | .bAlternateSetting = 0, |
| 288 | .bNumEndpoints = 0, |
| 289 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 290 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 291 | }; |
| 292 | |
| 293 | static struct usb_interface_descriptor microphone_as_interface_alt_1_desc = { |
| 294 | .bLength = USB_DT_INTERFACE_SIZE, |
| 295 | .bDescriptorType = USB_DT_INTERFACE, |
| 296 | .bAlternateSetting = 1, |
| 297 | .bNumEndpoints = 1, |
| 298 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 299 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 300 | }; |
| 301 | |
| 302 | /* B.4.2 Class-Specific AS Interface Descriptor */ |
| 303 | static struct uac1_as_header_descriptor microphone_as_header_desc = { |
| 304 | .bLength = UAC_DT_AS_HEADER_SIZE, |
| 305 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 306 | .bDescriptorSubtype = UAC_AS_GENERAL, |
| 307 | .bTerminalLink = MICROPHONE_OUTPUT_TERMINAL_ID, |
| 308 | .bDelay = 1, |
| 309 | .wFormatTag = UAC_FORMAT_TYPE_I_PCM, |
| 310 | }; |
| 311 | |
| 312 | static struct |
| 313 | uac_format_type_i_discrete_descriptor_1 microphone_as_type_i_desc = { |
| 314 | .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), |
| 315 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 316 | .bDescriptorSubtype = UAC_FORMAT_TYPE, |
| 317 | .bFormatType = UAC_FORMAT_TYPE_I, |
| 318 | .bNrChannels = 1, |
| 319 | .bSubframeSize = 2, |
| 320 | .bBitResolution = 16, |
| 321 | .bSamFreqType = 1, |
| 322 | }; |
| 323 | |
| 324 | /* Standard ISO IN Endpoint Descriptor */ |
| 325 | static struct usb_endpoint_descriptor microphone_as_ep_in_desc = { |
| 326 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
| 327 | .bDescriptorType = USB_DT_ENDPOINT, |
| 328 | .bEndpointAddress = USB_DIR_IN, |
| 329 | .bmAttributes = |
| 330 | USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, |
| 331 | .wMaxPacketSize = |
| 332 | __constant_cpu_to_le16(CAPTURE_EP_MAX_PACKET_SIZE), |
| 333 | .bInterval = 4, |
| 334 | }; |
| 335 | |
| 336 | /* Class-specific AS ISO OUT Endpoint Descriptor */ |
| 337 | static struct uac_iso_endpoint_descriptor microphone_as_iso_in_desc = { |
| 338 | .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, |
| 339 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
| 340 | .bDescriptorSubtype = UAC_EP_GENERAL, |
| 341 | .bmAttributes = 1, |
| 342 | .bLockDelayUnits = 1, |
| 343 | .wLockDelay = __constant_cpu_to_le16(1), |
| 344 | }; |
| 345 | |
| 346 | static struct usb_audio_control microphone_sample_freq_control = { |
| 347 | .list = LIST_HEAD_INIT(microphone_sample_freq_control.list), |
| 348 | .name = "Microphone Sampling Frequency Control", |
| 349 | .type = UAC_EP_CS_ATTR_SAMPLE_RATE, |
| 350 | .set = generic_set_cmd, |
| 351 | .get = generic_get_cmd, |
| 352 | }; |
| 353 | |
| 354 | static struct usb_audio_control_selector microphone_as_iso_in = { |
| 355 | .list = LIST_HEAD_INIT(microphone_as_iso_in.list), |
| 356 | .name = "Microphone Iso-IN Endpoint Control", |
| 357 | .type = UAC_EP_GENERAL, |
| 358 | .desc = (struct usb_descriptor_header *)µphone_as_iso_in_desc, |
| 359 | }; |
| 360 | |
| 361 | /*--------------------------------- */ |
| 362 | |
| 363 | static struct usb_descriptor_header *f_audio_desc[] = { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 364 | (struct usb_descriptor_header *)&ac_interface_desc, |
| 365 | (struct usb_descriptor_header *)&ac_header_desc, |
| 366 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 367 | (struct usb_descriptor_header *)µphone_input_terminal_desc, |
| 368 | (struct usb_descriptor_header *)µphone_output_terminal_desc, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 369 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 370 | (struct usb_descriptor_header *)&speaker_input_terminal_desc, |
| 371 | (struct usb_descriptor_header *)&speaker_output_terminal_desc, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 372 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 373 | (struct usb_descriptor_header *)µphone_as_interface_alt_0_desc, |
| 374 | (struct usb_descriptor_header *)µphone_as_interface_alt_1_desc, |
| 375 | (struct usb_descriptor_header *)µphone_as_header_desc, |
| 376 | (struct usb_descriptor_header *)µphone_as_type_i_desc, |
| 377 | (struct usb_descriptor_header *)µphone_as_ep_in_desc, |
| 378 | (struct usb_descriptor_header *)µphone_as_iso_in_desc, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 379 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 380 | (struct usb_descriptor_header *)&speaker_as_interface_alt_0_desc, |
| 381 | (struct usb_descriptor_header *)&speaker_as_interface_alt_1_desc, |
| 382 | (struct usb_descriptor_header *)&speaker_as_header_desc, |
| 383 | (struct usb_descriptor_header *)&speaker_as_type_i_desc, |
| 384 | (struct usb_descriptor_header *)&speaker_as_ep_out_desc, |
| 385 | (struct usb_descriptor_header *)&speaker_as_iso_out_desc, |
| 386 | |
| 387 | NULL, |
| 388 | }; |
| 389 | |
| 390 | /* string IDs are assigned dynamically */ |
| 391 | |
| 392 | static struct usb_string audio_string_defs[] = { |
| 393 | { } /* end of list */ |
| 394 | }; |
| 395 | |
| 396 | static struct usb_gadget_strings audio_stringtab_dev = { |
| 397 | .language = 0x0409, /* en-us */ |
| 398 | .strings = audio_string_defs, |
| 399 | }; |
| 400 | |
| 401 | static struct usb_gadget_strings *audio_strings[] = { |
| 402 | &audio_stringtab_dev, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 403 | NULL, |
| 404 | }; |
| 405 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 406 | /* |
| 407 | * This function is an ALSA sound card following USB Audio Class Spec 1.0. |
| 408 | */ |
| 409 | |
| 410 | /*-------------------------------------------------------------------------*/ |
| 411 | struct f_audio_buf { |
| 412 | u8 *buf; |
| 413 | int actual; |
| 414 | struct list_head list; |
| 415 | }; |
| 416 | |
| 417 | static struct f_audio_buf *f_audio_buffer_alloc(int buf_size) |
| 418 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 419 | struct f_audio_buf *playback_copy_buf; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 420 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 421 | playback_copy_buf = kzalloc(sizeof *playback_copy_buf, GFP_ATOMIC); |
| 422 | if (!playback_copy_buf) { |
| 423 | pr_err("Failed to allocate playback_copy_buf"); |
Julia Lawall | ff3b968 | 2009-12-09 14:23:32 +0100 | [diff] [blame] | 424 | return ERR_PTR(-ENOMEM); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 427 | playback_copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC); |
| 428 | if (!playback_copy_buf->buf) { |
| 429 | pr_err("Failed to allocate playback_copy_buf buffer"); |
| 430 | kfree(playback_copy_buf); |
| 431 | return ERR_PTR(-ENOMEM); |
| 432 | } |
| 433 | |
| 434 | return playback_copy_buf; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static void f_audio_buffer_free(struct f_audio_buf *audio_buf) |
| 438 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 439 | if (audio_buf) { |
| 440 | kfree(audio_buf->buf); |
| 441 | kfree(audio_buf); |
| 442 | audio_buf->buf = NULL; |
| 443 | audio_buf = NULL; |
| 444 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 445 | } |
| 446 | /*-------------------------------------------------------------------------*/ |
| 447 | |
| 448 | struct f_audio { |
| 449 | struct gaudio card; |
| 450 | |
| 451 | /* endpoints handle full and/or high speeds */ |
| 452 | struct usb_ep *out_ep; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 453 | struct usb_ep *in_ep; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 454 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 455 | spinlock_t playback_lock; |
| 456 | struct f_audio_buf *playback_copy_buf; |
| 457 | struct work_struct playback_work; |
| 458 | struct list_head play_queue; |
| 459 | |
| 460 | spinlock_t capture_lock; |
| 461 | struct f_audio_buf *capture_copy_buf; |
| 462 | struct work_struct capture_work; |
| 463 | struct list_head capture_queue; |
| 464 | struct usb_request *capture_req; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 465 | |
Bar Weiner | 33e0dbe | 2013-03-05 10:30:52 +0200 | [diff] [blame] | 466 | u8 alt_intf[F_AUDIO_NUM_INTERFACES]; |
| 467 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 468 | /* Control Set command */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 469 | struct list_head fu_cs; |
| 470 | struct list_head ep_cs; |
| 471 | u8 set_cmd; |
| 472 | struct usb_audio_control *set_con; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | static inline struct f_audio *func_to_audio(struct usb_function *f) |
| 476 | { |
| 477 | return container_of(f, struct f_audio, card.func); |
| 478 | } |
| 479 | |
| 480 | /*-------------------------------------------------------------------------*/ |
| 481 | |
| 482 | static void f_audio_playback_work(struct work_struct *data) |
| 483 | { |
| 484 | struct f_audio *audio = container_of(data, struct f_audio, |
| 485 | playback_work); |
| 486 | struct f_audio_buf *play_buf; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 487 | unsigned long flags; |
| 488 | int res = 0; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 489 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 490 | spin_lock_irqsave(&audio->playback_lock, flags); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 491 | if (list_empty(&audio->play_queue)) { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 492 | pr_err("playback_buf is empty"); |
| 493 | spin_unlock_irqrestore(&audio->playback_lock, flags); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | play_buf = list_first_entry(&audio->play_queue, |
| 497 | struct f_audio_buf, list); |
| 498 | list_del(&play_buf->list); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 499 | spin_unlock_irqrestore(&audio->playback_lock, flags); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 500 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 501 | pr_debug("play_buf->actual = %d", play_buf->actual); |
| 502 | |
| 503 | res = u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); |
| 504 | if (res) |
| 505 | pr_err("copying failed"); |
| 506 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 507 | f_audio_buffer_free(play_buf); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 508 | } |
| 509 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 510 | static int |
| 511 | f_audio_playback_ep_complete(struct usb_ep *ep, struct usb_request *req) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 512 | { |
| 513 | struct f_audio *audio = req->context; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 514 | struct f_audio_buf *copy_buf = audio->playback_copy_buf; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 515 | int err; |
| 516 | |
| 517 | if (!copy_buf) |
| 518 | return -EINVAL; |
| 519 | |
| 520 | /* Copy buffer is full, add it to the play_queue */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 521 | if (audio_playback_buf_size - copy_buf->actual < req->actual) { |
| 522 | pr_debug("audio_playback_buf_size %d - copy_buf->actual %d, req->actual %d", |
| 523 | audio_playback_buf_size, copy_buf->actual, req->actual); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 524 | list_add_tail(©_buf->list, &audio->play_queue); |
| 525 | schedule_work(&audio->playback_work); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 526 | copy_buf = f_audio_buffer_alloc(audio_playback_buf_size); |
| 527 | if (IS_ERR(copy_buf)) { |
| 528 | pr_err("Failed to allocate playback_copy_buf"); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 529 | return -ENOMEM; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 530 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual); |
| 534 | copy_buf->actual += req->actual; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 535 | audio->playback_copy_buf = copy_buf; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 536 | |
| 537 | err = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 538 | if (err) |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 539 | pr_err("Failed to queue %s req: err - %d\n", ep->name, err); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 540 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 541 | return err; |
| 542 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 543 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 544 | static void f_audio_capture_work(struct work_struct *data) |
| 545 | { |
| 546 | struct f_audio *audio = |
| 547 | container_of(data, struct f_audio, capture_work); |
| 548 | struct f_audio_buf *capture_buf; |
| 549 | unsigned long flags; |
| 550 | int res = 0; |
| 551 | |
| 552 | capture_buf = f_audio_buffer_alloc(audio_capture_buf_size); |
| 553 | if (capture_buf <= 0) { |
| 554 | pr_err("%s: buffer alloc failed\n", __func__); |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | res = u_audio_capture(&audio->card, capture_buf->buf, |
| 559 | audio_capture_buf_size); |
| 560 | if (res) |
| 561 | pr_err("copying failed"); |
| 562 | |
| 563 | pr_debug("Queue capture packet: size %d", audio_capture_buf_size); |
| 564 | spin_lock_irqsave(&audio->capture_lock, flags); |
| 565 | list_add_tail(&capture_buf->list, &audio->capture_queue); |
| 566 | spin_unlock_irqrestore(&audio->capture_lock, flags); |
| 567 | } |
| 568 | |
| 569 | static int |
| 570 | f_audio_capture_ep_complete(struct usb_ep *ep, struct usb_request *req) |
| 571 | { |
| 572 | struct f_audio *audio = req->context; |
| 573 | struct f_audio_buf *copy_buf = audio->capture_copy_buf; |
| 574 | unsigned long flags; |
| 575 | int err = 0; |
| 576 | |
| 577 | if (copy_buf == 0) { |
| 578 | pr_debug("copy_buf == 0"); |
| 579 | spin_lock_irqsave(&audio->capture_lock, flags); |
| 580 | if (list_empty(&audio->capture_queue)) { |
| 581 | spin_unlock_irqrestore(&audio->capture_lock, flags); |
| 582 | schedule_work(&audio->capture_work); |
| 583 | goto done; |
| 584 | } |
| 585 | copy_buf = list_first_entry(&audio->capture_queue, |
| 586 | struct f_audio_buf, list); |
| 587 | list_del(©_buf->list); |
| 588 | audio->capture_copy_buf = copy_buf; |
| 589 | spin_unlock_irqrestore(&audio->capture_lock, flags); |
| 590 | } |
| 591 | |
| 592 | pr_debug("Copy %d bytes", req->actual); |
| 593 | memcpy(req->buf, copy_buf->buf + copy_buf->actual, req->actual); |
| 594 | copy_buf->actual += req->actual; |
| 595 | |
| 596 | if (audio_capture_buf_size - copy_buf->actual < req->actual) { |
| 597 | f_audio_buffer_free(copy_buf); |
| 598 | audio->capture_copy_buf = 0; |
| 599 | schedule_work(&audio->capture_work); |
| 600 | } |
| 601 | done: |
| 602 | err = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 603 | if (err) |
| 604 | pr_err("Failed to queue %s req: err - %d\n", ep->name, err); |
| 605 | |
| 606 | return err; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static void f_audio_complete(struct usb_ep *ep, struct usb_request *req) |
| 610 | { |
| 611 | struct f_audio *audio = req->context; |
| 612 | int status = req->status; |
| 613 | u32 data = 0; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 614 | |
| 615 | switch (status) { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 616 | case 0: /* normal completion? */ |
| 617 | if (ep == audio->out_ep) { |
| 618 | f_audio_playback_ep_complete(ep, req); |
| 619 | } else if (ep == audio->in_ep) { |
| 620 | f_audio_capture_ep_complete(ep, req); |
| 621 | } else if (audio->set_con) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 622 | memcpy(&data, req->buf, req->length); |
| 623 | audio->set_con->set(audio->set_con, audio->set_cmd, |
| 624 | le16_to_cpu(data)); |
| 625 | audio->set_con = NULL; |
| 626 | } |
| 627 | break; |
| 628 | default: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 629 | pr_err("Failed completion: status %d", status); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 630 | break; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | static int audio_set_intf_req(struct usb_function *f, |
| 635 | const struct usb_ctrlrequest *ctrl) |
| 636 | { |
| 637 | struct f_audio *audio = func_to_audio(f); |
| 638 | struct usb_composite_dev *cdev = f->config->cdev; |
| 639 | struct usb_request *req = cdev->req; |
| 640 | u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); |
| 641 | u16 len = le16_to_cpu(ctrl->wLength); |
| 642 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 643 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 644 | u8 cmd = (ctrl->bRequest & 0x0F); |
| 645 | struct usb_audio_control_selector *cs; |
| 646 | struct usb_audio_control *con; |
| 647 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 648 | pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 649 | ctrl->bRequest, w_value, len, id); |
| 650 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 651 | list_for_each_entry(cs, &audio->fu_cs, list) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 652 | if (cs->id == id) { |
| 653 | list_for_each_entry(con, &cs->control, list) { |
| 654 | if (con->type == con_sel) { |
| 655 | audio->set_con = con; |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | audio->set_cmd = cmd; |
| 664 | req->context = audio; |
| 665 | req->complete = f_audio_complete; |
| 666 | |
| 667 | return len; |
| 668 | } |
| 669 | |
| 670 | static int audio_get_intf_req(struct usb_function *f, |
| 671 | const struct usb_ctrlrequest *ctrl) |
| 672 | { |
| 673 | struct f_audio *audio = func_to_audio(f); |
| 674 | struct usb_composite_dev *cdev = f->config->cdev; |
| 675 | struct usb_request *req = cdev->req; |
| 676 | int value = -EOPNOTSUPP; |
| 677 | u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); |
| 678 | u16 len = le16_to_cpu(ctrl->wLength); |
| 679 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 680 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 681 | u8 cmd = (ctrl->bRequest & 0x0F); |
| 682 | struct usb_audio_control_selector *cs; |
| 683 | struct usb_audio_control *con; |
| 684 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 685 | pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 686 | ctrl->bRequest, w_value, len, id); |
| 687 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 688 | list_for_each_entry(cs, &audio->fu_cs, list) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 689 | if (cs->id == id) { |
| 690 | list_for_each_entry(con, &cs->control, list) { |
| 691 | if (con->type == con_sel && con->get) { |
| 692 | value = con->get(con, cmd); |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | req->context = audio; |
| 701 | req->complete = f_audio_complete; |
| 702 | memcpy(req->buf, &value, len); |
| 703 | |
| 704 | return len; |
| 705 | } |
| 706 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 707 | static void audio_set_endpoint_complete(struct usb_ep *ep, |
| 708 | struct usb_request *req) |
| 709 | { |
| 710 | struct f_audio *audio = req->context; |
| 711 | u32 data = 0; |
| 712 | |
| 713 | if (req->status == 0 && audio->set_con) { |
| 714 | memcpy(&data, req->buf, req->length); |
| 715 | audio->set_con->set(audio->set_con, audio->set_cmd, |
| 716 | le32_to_cpu(data)); |
| 717 | audio->set_con = NULL; |
| 718 | } |
| 719 | } |
| 720 | |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 721 | static int audio_set_endpoint_req(struct usb_function *f, |
| 722 | const struct usb_ctrlrequest *ctrl) |
| 723 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 724 | int value = -EOPNOTSUPP; |
| 725 | u16 ep = le16_to_cpu(ctrl->wIndex); |
| 726 | u16 len = le16_to_cpu(ctrl->wLength); |
| 727 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 728 | |
| 729 | struct f_audio *audio = func_to_audio(f); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 730 | struct usb_composite_dev *cdev = f->config->cdev; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 731 | struct usb_request *req = cdev->req; |
| 732 | struct usb_audio_control_selector *cs; |
| 733 | struct usb_audio_control *con; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 734 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 735 | u8 epnum = ep & ~0x80; |
| 736 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 737 | u8 cmd = (ctrl->bRequest & 0x0F); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 738 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 739 | pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endp %d, epnum %d\n", |
| 740 | ctrl->bRequest, w_value, len, ep, epnum); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 741 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 742 | list_for_each_entry(cs, &audio->ep_cs, list) { |
| 743 | if (cs->id != epnum) |
| 744 | continue; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 745 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 746 | list_for_each_entry(con, &cs->control, list) { |
| 747 | if (con->type != con_sel) |
| 748 | continue; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 749 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 750 | switch (cmd) { |
| 751 | case UAC__CUR: |
| 752 | case UAC__MIN: |
| 753 | case UAC__MAX: |
| 754 | case UAC__RES: |
| 755 | audio->set_con = con; |
| 756 | audio->set_cmd = cmd; |
| 757 | req->context = audio; |
| 758 | req->complete = audio_set_endpoint_complete; |
| 759 | value = len; |
| 760 | break; |
| 761 | case UAC__MEM: |
| 762 | break; |
| 763 | default: |
| 764 | pr_err("Unknown command"); |
| 765 | break; |
| 766 | } |
| 767 | break; |
| 768 | } |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 769 | break; |
| 770 | } |
| 771 | |
| 772 | return value; |
| 773 | } |
| 774 | |
| 775 | static int audio_get_endpoint_req(struct usb_function *f, |
| 776 | const struct usb_ctrlrequest *ctrl) |
| 777 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 778 | struct f_audio *audio = func_to_audio(f); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 779 | struct usb_composite_dev *cdev = f->config->cdev; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 780 | struct usb_request *req = cdev->req; |
| 781 | struct usb_audio_control_selector *cs; |
| 782 | struct usb_audio_control *con; |
| 783 | int data; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 784 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 785 | int value = -EOPNOTSUPP; |
| 786 | u8 ep = (le16_to_cpu(ctrl->wIndex) & 0x7F); |
| 787 | u8 epnum = ep & ~0x80; |
| 788 | u16 len = le16_to_cpu(ctrl->wLength); |
| 789 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 790 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 791 | u8 cmd = (ctrl->bRequest & 0x0F); |
| 792 | |
| 793 | pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, ep %d\n", |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 794 | ctrl->bRequest, w_value, len, ep); |
| 795 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 796 | list_for_each_entry(cs, &audio->ep_cs, list) { |
| 797 | if (cs->id != epnum) |
| 798 | continue; |
| 799 | |
| 800 | list_for_each_entry(con, &cs->control, list) { |
| 801 | if (con->type != con_sel) |
| 802 | continue; |
| 803 | |
| 804 | switch (cmd) { |
| 805 | case UAC__CUR: |
| 806 | case UAC__MIN: |
| 807 | case UAC__MAX: |
| 808 | case UAC__RES: |
| 809 | data = cpu_to_le32(generic_get_cmd(con, cmd)); |
| 810 | memcpy(req->buf, &data, len); |
| 811 | value = len; |
| 812 | break; |
| 813 | case UAC__MEM: |
| 814 | break; |
| 815 | default: |
| 816 | break; |
| 817 | } |
| 818 | break; |
| 819 | } |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 820 | break; |
| 821 | } |
| 822 | |
| 823 | return value; |
| 824 | } |
| 825 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 826 | static int |
| 827 | f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 828 | { |
| 829 | struct usb_composite_dev *cdev = f->config->cdev; |
| 830 | struct usb_request *req = cdev->req; |
| 831 | int value = -EOPNOTSUPP; |
| 832 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 833 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 834 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 835 | |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 836 | /* composite driver infrastructure handles everything; interface |
| 837 | * activation uses set_alt(). |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 838 | */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 839 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 840 | switch (ctrl->bRequestType) { |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 841 | case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 842 | pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE"); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 843 | value = audio_set_intf_req(f, ctrl); |
| 844 | break; |
| 845 | |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 846 | case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 847 | pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE"); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 848 | value = audio_get_intf_req(f, ctrl); |
| 849 | break; |
| 850 | |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 851 | case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 852 | pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT"); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 853 | value = audio_set_endpoint_req(f, ctrl); |
| 854 | break; |
| 855 | |
| 856 | case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 857 | pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT"); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 858 | value = audio_get_endpoint_req(f, ctrl); |
| 859 | break; |
| 860 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 861 | default: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 862 | pr_err("Unknown control request %02x.%02x v%04x i%04x l%d\n", |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 863 | ctrl->bRequestType, ctrl->bRequest, |
| 864 | w_value, w_index, w_length); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 865 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* respond with data transfer or status phase? */ |
| 869 | if (value >= 0) { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 870 | pr_debug("audio req %02x.%02x v%04x i%04x l%d\n", |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 871 | ctrl->bRequestType, ctrl->bRequest, |
| 872 | w_value, w_index, w_length); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 873 | req->zero = 1; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 874 | req->length = value; |
| 875 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 876 | if (value < 0) |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 877 | pr_err("audio response failed on err %d\n", value); |
| 878 | } else { |
| 879 | pr_err("STALL\n"); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /* device either stalls (value < 0) or reports success */ |
| 883 | return value; |
| 884 | } |
| 885 | |
Bar Weiner | 33e0dbe | 2013-03-05 10:30:52 +0200 | [diff] [blame] | 886 | static int f_audio_get_alt(struct usb_function *f, unsigned intf) |
| 887 | { |
| 888 | struct f_audio *audio = func_to_audio(f); |
| 889 | |
| 890 | if (intf == ac_header_desc.baInterfaceNr[0]) |
| 891 | return audio->alt_intf[0]; |
| 892 | if (intf == ac_header_desc.baInterfaceNr[1]) |
| 893 | return audio->alt_intf[1]; |
| 894 | |
| 895 | return 0; |
| 896 | } |
| 897 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 898 | static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 899 | { |
| 900 | struct f_audio *audio = func_to_audio(f); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 901 | struct usb_ep *out_ep = audio->out_ep; |
| 902 | struct usb_ep *in_ep = audio->in_ep; |
| 903 | struct usb_request *req; |
| 904 | unsigned long flags; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 905 | int i = 0, err = 0; |
| 906 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 907 | pr_debug("intf %d, alt %d\n", intf, alt); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 908 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 909 | if (intf == ac_header_desc.baInterfaceNr[0]) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 910 | if (alt == 1) { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 911 | err = usb_ep_enable(in_ep); |
| 912 | if (err) { |
| 913 | pr_err("Failed to enable capture ep"); |
| 914 | return err; |
| 915 | } |
| 916 | in_ep->driver_data = audio; |
| 917 | audio->capture_copy_buf = 0; |
| 918 | |
| 919 | /* Allocate a write buffer */ |
| 920 | req = usb_ep_alloc_request(in_ep, GFP_ATOMIC); |
| 921 | if (!req) { |
| 922 | pr_err("request allocation failed\n"); |
Julia Lawall | ff3b968 | 2009-12-09 14:23:32 +0100 | [diff] [blame] | 923 | return -ENOMEM; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 924 | } |
| 925 | req->buf = kzalloc(req_capture_buf_size, |
| 926 | GFP_ATOMIC); |
| 927 | if (!req->buf) { |
| 928 | pr_err("request buffer allocation failed\n"); |
| 929 | return -ENOMEM; |
| 930 | } |
| 931 | |
| 932 | req->length = req_capture_buf_size; |
| 933 | req->context = audio; |
| 934 | req->complete = f_audio_complete; |
| 935 | audio->capture_req = req; |
| 936 | err = usb_ep_queue(in_ep, req, GFP_ATOMIC); |
| 937 | if (err) |
| 938 | pr_err("Failed to queue %s req: err %d\n", |
| 939 | in_ep->name, err); |
| 940 | schedule_work(&audio->capture_work); |
| 941 | } else { |
| 942 | struct f_audio_buf *capture_buf; |
| 943 | spin_lock_irqsave(&audio->capture_lock, flags); |
| 944 | while (!list_empty(&audio->capture_queue)) { |
| 945 | capture_buf = |
| 946 | list_first_entry( |
| 947 | &audio->capture_queue, |
| 948 | struct f_audio_buf, |
| 949 | list); |
| 950 | list_del(&capture_buf->list); |
| 951 | f_audio_buffer_free(capture_buf); |
| 952 | } |
| 953 | spin_unlock_irqrestore(&audio->capture_lock, flags); |
| 954 | } |
Bar Weiner | 33e0dbe | 2013-03-05 10:30:52 +0200 | [diff] [blame] | 955 | audio->alt_intf[0] = alt; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 956 | } else if (intf == ac_header_desc.baInterfaceNr[1]) { |
| 957 | if (alt == 1) { |
| 958 | err = usb_ep_enable(out_ep); |
| 959 | if (err) { |
| 960 | pr_err("Failed to enable playback ep"); |
| 961 | return err; |
| 962 | } |
| 963 | out_ep->driver_data = audio; |
| 964 | audio->playback_copy_buf = |
| 965 | f_audio_buffer_alloc(audio_playback_buf_size); |
| 966 | if (IS_ERR(audio->playback_copy_buf)) { |
| 967 | pr_err("Failed to allocate playback_copy_buf"); |
| 968 | return -ENOMEM; |
| 969 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 970 | |
| 971 | /* |
| 972 | * allocate a bunch of read buffers |
| 973 | * and queue them all at once. |
| 974 | */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 975 | for (i = 0; i < req_playback_count && err == 0; i++) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 976 | req = usb_ep_alloc_request(out_ep, GFP_ATOMIC); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 977 | if (!req) { |
| 978 | pr_err("request allocation failed\n"); |
| 979 | return -ENOMEM; |
| 980 | } |
| 981 | req->buf = kzalloc(req_playback_buf_size, |
| 982 | GFP_ATOMIC); |
| 983 | if (!req->buf) { |
| 984 | pr_err("request buffer allocation failed\n"); |
| 985 | return -ENOMEM; |
| 986 | } |
| 987 | req->length = req_playback_buf_size; |
| 988 | req->context = audio; |
| 989 | req->complete = f_audio_complete; |
| 990 | err = usb_ep_queue(out_ep, req, GFP_ATOMIC); |
| 991 | if (err) |
| 992 | pr_err("Failed to queue %s queue req: err %d\n", |
| 993 | out_ep->name, err); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 994 | } |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 995 | pr_debug("Allocated %d requests\n", req_playback_count); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 996 | } else { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 997 | struct f_audio_buf *playback_copy_buf = |
| 998 | audio->playback_copy_buf; |
| 999 | if (playback_copy_buf) { |
| 1000 | pr_err("Schedule playback_work"); |
| 1001 | list_add_tail(&playback_copy_buf->list, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1002 | &audio->play_queue); |
| 1003 | schedule_work(&audio->playback_work); |
Bar Weiner | af8ef11 | 2013-03-05 10:40:26 +0200 | [diff] [blame] | 1004 | audio->playback_copy_buf = NULL; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1005 | } else { |
| 1006 | pr_err("playback_buf is empty. Stop."); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1007 | } |
| 1008 | } |
Bar Weiner | 33e0dbe | 2013-03-05 10:30:52 +0200 | [diff] [blame] | 1009 | audio->alt_intf[1] = alt; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1010 | } else { |
| 1011 | pr_err("Interface %d. Do nothing. Return %d\n", intf, err); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | return err; |
| 1015 | } |
| 1016 | |
| 1017 | static void f_audio_disable(struct usb_function *f) |
| 1018 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1019 | u_audio_clear(); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /*-------------------------------------------------------------------------*/ |
| 1023 | |
| 1024 | static void f_audio_build_desc(struct f_audio *audio) |
| 1025 | { |
| 1026 | struct gaudio *card = &audio->card; |
| 1027 | u8 *sam_freq; |
| 1028 | int rate; |
| 1029 | |
| 1030 | /* Set channel numbers */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1031 | speaker_input_terminal_desc.bNrChannels = |
| 1032 | u_audio_get_playback_channels(card); |
| 1033 | speaker_as_type_i_desc.bNrChannels = |
| 1034 | u_audio_get_playback_channels(card); |
| 1035 | |
| 1036 | microphone_input_terminal_desc.bNrChannels = |
| 1037 | u_audio_get_capture_channels(card); |
| 1038 | microphone_as_type_i_desc.bNrChannels = |
| 1039 | u_audio_get_capture_channels(card); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1040 | |
| 1041 | /* Set sample rates */ |
| 1042 | rate = u_audio_get_playback_rate(card); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1043 | sam_freq = speaker_as_type_i_desc.tSamFreq[0]; |
| 1044 | memcpy(sam_freq, &rate, 3); |
| 1045 | |
| 1046 | rate = u_audio_get_capture_rate(card); |
| 1047 | sam_freq = microphone_as_type_i_desc.tSamFreq[0]; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1048 | memcpy(sam_freq, &rate, 3); |
| 1049 | |
| 1050 | /* Todo: Set Sample bits and other parameters */ |
| 1051 | |
| 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | /* audio function driver setup/binding */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1056 | static int |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1057 | f_audio_bind(struct usb_configuration *c, struct usb_function *f) |
| 1058 | { |
| 1059 | struct usb_composite_dev *cdev = c->cdev; |
| 1060 | struct f_audio *audio = func_to_audio(f); |
| 1061 | int status; |
| 1062 | struct usb_ep *ep; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1063 | u8 epaddr; |
| 1064 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1065 | |
| 1066 | f_audio_build_desc(audio); |
| 1067 | |
| 1068 | /* allocate instance-specific interface IDs, and patch descriptors */ |
| 1069 | status = usb_interface_id(c, f); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1070 | if (status < 0) { |
| 1071 | pr_err("%s: failed to allocate desc interface", __func__); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1072 | goto fail; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1073 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1074 | ac_interface_desc.bInterfaceNumber = status; |
| 1075 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1076 | status = -ENOMEM; |
| 1077 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1078 | status = usb_interface_id(c, f); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1079 | if (status < 0) { |
| 1080 | pr_err("%s: failed to allocate alt interface", __func__); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1081 | goto fail; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1082 | } |
| 1083 | microphone_as_interface_alt_0_desc.bInterfaceNumber = status; |
| 1084 | microphone_as_interface_alt_1_desc.bInterfaceNumber = status; |
| 1085 | ac_header_desc.baInterfaceNr[0] = status; |
Bar Weiner | 33e0dbe | 2013-03-05 10:30:52 +0200 | [diff] [blame] | 1086 | audio->alt_intf[0] = 0; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1087 | |
| 1088 | status = -ENODEV; |
| 1089 | |
| 1090 | status = usb_interface_id(c, f); |
| 1091 | if (status < 0) { |
| 1092 | pr_err("%s: failed to allocate alt interface", __func__); |
| 1093 | goto fail; |
| 1094 | } |
| 1095 | speaker_as_interface_alt_0_desc.bInterfaceNumber = status; |
| 1096 | speaker_as_interface_alt_1_desc.bInterfaceNumber = status; |
| 1097 | ac_header_desc.baInterfaceNr[1] = status; |
Bar Weiner | 33e0dbe | 2013-03-05 10:30:52 +0200 | [diff] [blame] | 1098 | audio->alt_intf[1] = 0; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1099 | |
| 1100 | status = -ENODEV; |
| 1101 | |
| 1102 | /* allocate instance-specific endpoints */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1103 | ep = usb_ep_autoconfig(cdev->gadget, µphone_as_ep_in_desc); |
| 1104 | if (!ep) { |
| 1105 | pr_err("%s: failed to autoconfig in endpoint", __func__); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1106 | goto fail; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1107 | } |
| 1108 | audio->in_ep = ep; |
| 1109 | ep->desc = µphone_as_ep_in_desc; |
| 1110 | ep->driver_data = cdev; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1111 | |
| 1112 | status = -ENOMEM; |
| 1113 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1114 | ep = usb_ep_autoconfig(cdev->gadget, &speaker_as_ep_out_desc); |
| 1115 | if (!ep) { |
| 1116 | pr_err("%s: failed to autoconfig out endpoint", __func__); |
| 1117 | goto fail; |
| 1118 | } |
| 1119 | audio->out_ep = ep; |
| 1120 | ep->desc = &speaker_as_ep_out_desc; |
| 1121 | ep->driver_data = cdev; |
Felipe Balbi | ef7f584 | 2011-08-26 12:48:15 +0300 | [diff] [blame] | 1122 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1123 | /* associate bEndpointAddress with usb_function */ |
| 1124 | epaddr = microphone_as_ep_in_desc.bEndpointAddress & ~USB_DIR_IN; |
| 1125 | microphone_as_iso_in.id = epaddr; |
| 1126 | |
| 1127 | epaddr = speaker_as_ep_out_desc.bEndpointAddress & ~USB_DIR_IN; |
| 1128 | speaker_as_iso_out.id = epaddr; |
| 1129 | |
| 1130 | /* support all relevant hardware speeds. we expect that when |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1131 | * hardware is dual speed, all bulk-capable endpoints work at |
| 1132 | * both speeds |
| 1133 | */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1134 | |
| 1135 | /* copy descriptors, and track endpoint copies */ |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1136 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 1137 | c->highspeed = true; |
| 1138 | f->hs_descriptors = usb_copy_descriptors(f_audio_desc); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1139 | } else { |
| 1140 | f->descriptors = usb_copy_descriptors(f_audio_desc); |
Felipe Balbi | ef7f584 | 2011-08-26 12:48:15 +0300 | [diff] [blame] | 1141 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1142 | |
| 1143 | return 0; |
| 1144 | |
| 1145 | fail: |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1146 | return status; |
| 1147 | } |
| 1148 | |
| 1149 | static void |
| 1150 | f_audio_unbind(struct usb_configuration *c, struct usb_function *f) |
| 1151 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1152 | struct f_audio *audio = func_to_audio(f); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1153 | |
| 1154 | usb_free_descriptors(f->descriptors); |
Sebastian Andrzej Siewior | 16a2f97 | 2011-04-11 20:44:30 +0200 | [diff] [blame] | 1155 | usb_free_descriptors(f->hs_descriptors); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1156 | kfree(audio); |
| 1157 | } |
| 1158 | |
| 1159 | /*-------------------------------------------------------------------------*/ |
| 1160 | |
Laurent Pinchart | b95cd7e | 2009-06-21 23:21:55 +0200 | [diff] [blame] | 1161 | static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value) |
| 1162 | { |
| 1163 | con->data[cmd] = value; |
| 1164 | |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | static int generic_get_cmd(struct usb_audio_control *con, u8 cmd) |
| 1169 | { |
| 1170 | return con->data[cmd]; |
| 1171 | } |
| 1172 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1173 | /* Todo: add more control selecotor dynamically */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1174 | int control_selector_init(struct f_audio *audio) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1175 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1176 | INIT_LIST_HEAD(&audio->fu_cs); |
| 1177 | list_add(µphone_fu_controls.list, &audio->fu_cs); |
| 1178 | list_add(&speaker_fu_controls.list, &audio->fu_cs); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1179 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1180 | INIT_LIST_HEAD(µphone_fu_controls.control); |
| 1181 | list_add(µphone_mute_control.list, |
| 1182 | µphone_fu_controls.control); |
| 1183 | list_add(µphone_volume_control.list, |
| 1184 | µphone_fu_controls.control); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1185 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1186 | INIT_LIST_HEAD(&speaker_fu_controls.control); |
| 1187 | list_add(&speaker_mute_control.list, |
| 1188 | &speaker_fu_controls.control); |
| 1189 | list_add(&speaker_volume_control.list, |
| 1190 | &speaker_fu_controls.control); |
| 1191 | |
| 1192 | microphone_volume_control.data[UAC__CUR] = 0xffc0; |
| 1193 | microphone_volume_control.data[UAC__MIN] = 0xe3a0; |
| 1194 | microphone_volume_control.data[UAC__MAX] = 0xfff0; |
| 1195 | microphone_volume_control.data[UAC__RES] = 0x0030; |
| 1196 | |
| 1197 | speaker_volume_control.data[UAC__CUR] = 0xffc0; |
| 1198 | speaker_volume_control.data[UAC__MIN] = 0xe3a0; |
| 1199 | speaker_volume_control.data[UAC__MAX] = 0xfff0; |
| 1200 | speaker_volume_control.data[UAC__RES] = 0x0030; |
| 1201 | |
| 1202 | INIT_LIST_HEAD(&audio->ep_cs); |
| 1203 | list_add(&speaker_as_iso_out.list, &audio->ep_cs); |
| 1204 | list_add(µphone_as_iso_in.list, &audio->ep_cs); |
| 1205 | |
| 1206 | INIT_LIST_HEAD(µphone_as_iso_in.control); |
| 1207 | list_add(µphone_sample_freq_control.list, |
| 1208 | µphone_as_iso_in.control); |
| 1209 | |
| 1210 | INIT_LIST_HEAD(&speaker_as_iso_out.control); |
| 1211 | list_add(&speaker_sample_freq_control.list, |
| 1212 | &speaker_as_iso_out.control); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1213 | |
| 1214 | return 0; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1215 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1219 | * audio_bind_config - add USB audio function to a configuration |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1220 | * @c: the configuration to support the USB audio function |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1221 | * Context: single threaded during gadget setup |
| 1222 | * |
| 1223 | * Returns zero on success, else negative errno. |
| 1224 | */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1225 | int audio_bind_config(struct usb_configuration *c) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1226 | { |
| 1227 | struct f_audio *audio; |
| 1228 | int status; |
| 1229 | |
| 1230 | /* allocate and initialize one new instance */ |
| 1231 | audio = kzalloc(sizeof *audio, GFP_KERNEL); |
| 1232 | if (!audio) |
| 1233 | return -ENOMEM; |
| 1234 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1235 | audio->card.gadget = c->cdev->gadget; |
| 1236 | |
| 1237 | INIT_LIST_HEAD(&audio->play_queue); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1238 | spin_lock_init(&audio->playback_lock); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1239 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1240 | INIT_LIST_HEAD(&audio->capture_queue); |
| 1241 | spin_lock_init(&audio->capture_lock); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1242 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1243 | audio->card.func.name = "audio"; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1244 | audio->card.func.strings = audio_strings; |
| 1245 | audio->card.func.bind = f_audio_bind; |
| 1246 | audio->card.func.unbind = f_audio_unbind; |
Bar Weiner | 33e0dbe | 2013-03-05 10:30:52 +0200 | [diff] [blame] | 1247 | audio->card.func.get_alt = f_audio_get_alt; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1248 | audio->card.func.set_alt = f_audio_set_alt; |
| 1249 | audio->card.func.setup = f_audio_setup; |
| 1250 | audio->card.func.disable = f_audio_disable; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1251 | |
| 1252 | control_selector_init(audio); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1253 | INIT_WORK(&audio->playback_work, f_audio_playback_work); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1254 | INIT_WORK(&audio->capture_work, f_audio_capture_work); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1255 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1256 | /* set up ASLA audio devices */ |
| 1257 | status = gaudio_setup(&audio->card); |
| 1258 | if (status < 0) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1259 | goto add_fail; |
| 1260 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1261 | status = usb_add_function(c, &audio->card.func); |
| 1262 | if (status) { |
| 1263 | pr_err("%s: Failed to add usb audio function, err = %d", |
| 1264 | __func__, status); |
| 1265 | goto setup_fail; |
| 1266 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1267 | |
| 1268 | return status; |
| 1269 | |
| 1270 | add_fail: |
Cliff Cai | feef1d9 | 2009-12-09 22:28:39 -0500 | [diff] [blame] | 1271 | gaudio_cleanup(); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1272 | setup_fail: |
| 1273 | kfree(audio); |
| 1274 | return status; |
| 1275 | } |