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 | |
| 466 | /* Control Set command */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 467 | struct list_head fu_cs; |
| 468 | struct list_head ep_cs; |
| 469 | u8 set_cmd; |
| 470 | struct usb_audio_control *set_con; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 471 | }; |
| 472 | |
| 473 | static inline struct f_audio *func_to_audio(struct usb_function *f) |
| 474 | { |
| 475 | return container_of(f, struct f_audio, card.func); |
| 476 | } |
| 477 | |
| 478 | /*-------------------------------------------------------------------------*/ |
| 479 | |
| 480 | static void f_audio_playback_work(struct work_struct *data) |
| 481 | { |
| 482 | struct f_audio *audio = container_of(data, struct f_audio, |
| 483 | playback_work); |
| 484 | struct f_audio_buf *play_buf; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 485 | unsigned long flags; |
| 486 | int res = 0; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 487 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 488 | spin_lock_irqsave(&audio->playback_lock, flags); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 489 | if (list_empty(&audio->play_queue)) { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 490 | pr_err("playback_buf is empty"); |
| 491 | spin_unlock_irqrestore(&audio->playback_lock, flags); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | play_buf = list_first_entry(&audio->play_queue, |
| 495 | struct f_audio_buf, list); |
| 496 | list_del(&play_buf->list); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 497 | spin_unlock_irqrestore(&audio->playback_lock, flags); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 498 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 499 | pr_debug("play_buf->actual = %d", play_buf->actual); |
| 500 | |
| 501 | res = u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); |
| 502 | if (res) |
| 503 | pr_err("copying failed"); |
| 504 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 505 | f_audio_buffer_free(play_buf); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 508 | static int |
| 509 | 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] | 510 | { |
| 511 | struct f_audio *audio = req->context; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 512 | struct f_audio_buf *copy_buf = audio->playback_copy_buf; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 513 | int err; |
| 514 | |
| 515 | if (!copy_buf) |
| 516 | return -EINVAL; |
| 517 | |
| 518 | /* Copy buffer is full, add it to the play_queue */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 519 | if (audio_playback_buf_size - copy_buf->actual < req->actual) { |
| 520 | pr_debug("audio_playback_buf_size %d - copy_buf->actual %d, req->actual %d", |
| 521 | audio_playback_buf_size, copy_buf->actual, req->actual); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 522 | list_add_tail(©_buf->list, &audio->play_queue); |
| 523 | schedule_work(&audio->playback_work); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 524 | copy_buf = f_audio_buffer_alloc(audio_playback_buf_size); |
| 525 | if (IS_ERR(copy_buf)) { |
| 526 | pr_err("Failed to allocate playback_copy_buf"); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 527 | return -ENOMEM; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 528 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual); |
| 532 | copy_buf->actual += req->actual; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 533 | audio->playback_copy_buf = copy_buf; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 534 | |
| 535 | err = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 536 | if (err) |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 537 | 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] | 538 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 539 | return err; |
| 540 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 541 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 542 | static void f_audio_capture_work(struct work_struct *data) |
| 543 | { |
| 544 | struct f_audio *audio = |
| 545 | container_of(data, struct f_audio, capture_work); |
| 546 | struct f_audio_buf *capture_buf; |
| 547 | unsigned long flags; |
| 548 | int res = 0; |
| 549 | |
| 550 | capture_buf = f_audio_buffer_alloc(audio_capture_buf_size); |
| 551 | if (capture_buf <= 0) { |
| 552 | pr_err("%s: buffer alloc failed\n", __func__); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | res = u_audio_capture(&audio->card, capture_buf->buf, |
| 557 | audio_capture_buf_size); |
| 558 | if (res) |
| 559 | pr_err("copying failed"); |
| 560 | |
| 561 | pr_debug("Queue capture packet: size %d", audio_capture_buf_size); |
| 562 | spin_lock_irqsave(&audio->capture_lock, flags); |
| 563 | list_add_tail(&capture_buf->list, &audio->capture_queue); |
| 564 | spin_unlock_irqrestore(&audio->capture_lock, flags); |
| 565 | } |
| 566 | |
| 567 | static int |
| 568 | f_audio_capture_ep_complete(struct usb_ep *ep, struct usb_request *req) |
| 569 | { |
| 570 | struct f_audio *audio = req->context; |
| 571 | struct f_audio_buf *copy_buf = audio->capture_copy_buf; |
| 572 | unsigned long flags; |
| 573 | int err = 0; |
| 574 | |
| 575 | if (copy_buf == 0) { |
| 576 | pr_debug("copy_buf == 0"); |
| 577 | spin_lock_irqsave(&audio->capture_lock, flags); |
| 578 | if (list_empty(&audio->capture_queue)) { |
| 579 | spin_unlock_irqrestore(&audio->capture_lock, flags); |
| 580 | schedule_work(&audio->capture_work); |
| 581 | goto done; |
| 582 | } |
| 583 | copy_buf = list_first_entry(&audio->capture_queue, |
| 584 | struct f_audio_buf, list); |
| 585 | list_del(©_buf->list); |
| 586 | audio->capture_copy_buf = copy_buf; |
| 587 | spin_unlock_irqrestore(&audio->capture_lock, flags); |
| 588 | } |
| 589 | |
| 590 | pr_debug("Copy %d bytes", req->actual); |
| 591 | memcpy(req->buf, copy_buf->buf + copy_buf->actual, req->actual); |
| 592 | copy_buf->actual += req->actual; |
| 593 | |
| 594 | if (audio_capture_buf_size - copy_buf->actual < req->actual) { |
| 595 | f_audio_buffer_free(copy_buf); |
| 596 | audio->capture_copy_buf = 0; |
| 597 | schedule_work(&audio->capture_work); |
| 598 | } |
| 599 | done: |
| 600 | err = usb_ep_queue(ep, req, GFP_ATOMIC); |
| 601 | if (err) |
| 602 | pr_err("Failed to queue %s req: err - %d\n", ep->name, err); |
| 603 | |
| 604 | return err; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static void f_audio_complete(struct usb_ep *ep, struct usb_request *req) |
| 608 | { |
| 609 | struct f_audio *audio = req->context; |
| 610 | int status = req->status; |
| 611 | u32 data = 0; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 612 | |
| 613 | switch (status) { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 614 | case 0: /* normal completion? */ |
| 615 | if (ep == audio->out_ep) { |
| 616 | f_audio_playback_ep_complete(ep, req); |
| 617 | } else if (ep == audio->in_ep) { |
| 618 | f_audio_capture_ep_complete(ep, req); |
| 619 | } else if (audio->set_con) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 620 | memcpy(&data, req->buf, req->length); |
| 621 | audio->set_con->set(audio->set_con, audio->set_cmd, |
| 622 | le16_to_cpu(data)); |
| 623 | audio->set_con = NULL; |
| 624 | } |
| 625 | break; |
| 626 | default: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 627 | pr_err("Failed completion: status %d", status); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 628 | break; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | static int audio_set_intf_req(struct usb_function *f, |
| 633 | const struct usb_ctrlrequest *ctrl) |
| 634 | { |
| 635 | struct f_audio *audio = func_to_audio(f); |
| 636 | struct usb_composite_dev *cdev = f->config->cdev; |
| 637 | struct usb_request *req = cdev->req; |
| 638 | u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); |
| 639 | u16 len = le16_to_cpu(ctrl->wLength); |
| 640 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 641 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 642 | u8 cmd = (ctrl->bRequest & 0x0F); |
| 643 | struct usb_audio_control_selector *cs; |
| 644 | struct usb_audio_control *con; |
| 645 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 646 | 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] | 647 | ctrl->bRequest, w_value, len, id); |
| 648 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 649 | list_for_each_entry(cs, &audio->fu_cs, list) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 650 | if (cs->id == id) { |
| 651 | list_for_each_entry(con, &cs->control, list) { |
| 652 | if (con->type == con_sel) { |
| 653 | audio->set_con = con; |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | audio->set_cmd = cmd; |
| 662 | req->context = audio; |
| 663 | req->complete = f_audio_complete; |
| 664 | |
| 665 | return len; |
| 666 | } |
| 667 | |
| 668 | static int audio_get_intf_req(struct usb_function *f, |
| 669 | const struct usb_ctrlrequest *ctrl) |
| 670 | { |
| 671 | struct f_audio *audio = func_to_audio(f); |
| 672 | struct usb_composite_dev *cdev = f->config->cdev; |
| 673 | struct usb_request *req = cdev->req; |
| 674 | int value = -EOPNOTSUPP; |
| 675 | u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); |
| 676 | u16 len = le16_to_cpu(ctrl->wLength); |
| 677 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 678 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 679 | u8 cmd = (ctrl->bRequest & 0x0F); |
| 680 | struct usb_audio_control_selector *cs; |
| 681 | struct usb_audio_control *con; |
| 682 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 683 | 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] | 684 | ctrl->bRequest, w_value, len, id); |
| 685 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 686 | list_for_each_entry(cs, &audio->fu_cs, list) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 687 | if (cs->id == id) { |
| 688 | list_for_each_entry(con, &cs->control, list) { |
| 689 | if (con->type == con_sel && con->get) { |
| 690 | value = con->get(con, cmd); |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | req->context = audio; |
| 699 | req->complete = f_audio_complete; |
| 700 | memcpy(req->buf, &value, len); |
| 701 | |
| 702 | return len; |
| 703 | } |
| 704 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 705 | static void audio_set_endpoint_complete(struct usb_ep *ep, |
| 706 | struct usb_request *req) |
| 707 | { |
| 708 | struct f_audio *audio = req->context; |
| 709 | u32 data = 0; |
| 710 | |
| 711 | if (req->status == 0 && audio->set_con) { |
| 712 | memcpy(&data, req->buf, req->length); |
| 713 | audio->set_con->set(audio->set_con, audio->set_cmd, |
| 714 | le32_to_cpu(data)); |
| 715 | audio->set_con = NULL; |
| 716 | } |
| 717 | } |
| 718 | |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 719 | static int audio_set_endpoint_req(struct usb_function *f, |
| 720 | const struct usb_ctrlrequest *ctrl) |
| 721 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 722 | int value = -EOPNOTSUPP; |
| 723 | u16 ep = le16_to_cpu(ctrl->wIndex); |
| 724 | u16 len = le16_to_cpu(ctrl->wLength); |
| 725 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 726 | |
| 727 | struct f_audio *audio = func_to_audio(f); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 728 | struct usb_composite_dev *cdev = f->config->cdev; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 729 | struct usb_request *req = cdev->req; |
| 730 | struct usb_audio_control_selector *cs; |
| 731 | struct usb_audio_control *con; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 732 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 733 | u8 epnum = ep & ~0x80; |
| 734 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 735 | u8 cmd = (ctrl->bRequest & 0x0F); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 736 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 737 | pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endp %d, epnum %d\n", |
| 738 | ctrl->bRequest, w_value, len, ep, epnum); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 739 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 740 | list_for_each_entry(cs, &audio->ep_cs, list) { |
| 741 | if (cs->id != epnum) |
| 742 | continue; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 743 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 744 | list_for_each_entry(con, &cs->control, list) { |
| 745 | if (con->type != con_sel) |
| 746 | continue; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 747 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 748 | switch (cmd) { |
| 749 | case UAC__CUR: |
| 750 | case UAC__MIN: |
| 751 | case UAC__MAX: |
| 752 | case UAC__RES: |
| 753 | audio->set_con = con; |
| 754 | audio->set_cmd = cmd; |
| 755 | req->context = audio; |
| 756 | req->complete = audio_set_endpoint_complete; |
| 757 | value = len; |
| 758 | break; |
| 759 | case UAC__MEM: |
| 760 | break; |
| 761 | default: |
| 762 | pr_err("Unknown command"); |
| 763 | break; |
| 764 | } |
| 765 | break; |
| 766 | } |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 767 | break; |
| 768 | } |
| 769 | |
| 770 | return value; |
| 771 | } |
| 772 | |
| 773 | static int audio_get_endpoint_req(struct usb_function *f, |
| 774 | const struct usb_ctrlrequest *ctrl) |
| 775 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 776 | struct f_audio *audio = func_to_audio(f); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 777 | struct usb_composite_dev *cdev = f->config->cdev; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 778 | struct usb_request *req = cdev->req; |
| 779 | struct usb_audio_control_selector *cs; |
| 780 | struct usb_audio_control *con; |
| 781 | int data; |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 782 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 783 | int value = -EOPNOTSUPP; |
| 784 | u8 ep = (le16_to_cpu(ctrl->wIndex) & 0x7F); |
| 785 | u8 epnum = ep & ~0x80; |
| 786 | u16 len = le16_to_cpu(ctrl->wLength); |
| 787 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 788 | u8 con_sel = (w_value >> 8) & 0xFF; |
| 789 | u8 cmd = (ctrl->bRequest & 0x0F); |
| 790 | |
| 791 | 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] | 792 | ctrl->bRequest, w_value, len, ep); |
| 793 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 794 | list_for_each_entry(cs, &audio->ep_cs, list) { |
| 795 | if (cs->id != epnum) |
| 796 | continue; |
| 797 | |
| 798 | list_for_each_entry(con, &cs->control, list) { |
| 799 | if (con->type != con_sel) |
| 800 | continue; |
| 801 | |
| 802 | switch (cmd) { |
| 803 | case UAC__CUR: |
| 804 | case UAC__MIN: |
| 805 | case UAC__MAX: |
| 806 | case UAC__RES: |
| 807 | data = cpu_to_le32(generic_get_cmd(con, cmd)); |
| 808 | memcpy(req->buf, &data, len); |
| 809 | value = len; |
| 810 | break; |
| 811 | case UAC__MEM: |
| 812 | break; |
| 813 | default: |
| 814 | break; |
| 815 | } |
| 816 | break; |
| 817 | } |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | |
| 821 | return value; |
| 822 | } |
| 823 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 824 | static int |
| 825 | f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 826 | { |
| 827 | struct usb_composite_dev *cdev = f->config->cdev; |
| 828 | struct usb_request *req = cdev->req; |
| 829 | int value = -EOPNOTSUPP; |
| 830 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 831 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 832 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 833 | |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 834 | /* composite driver infrastructure handles everything; interface |
| 835 | * activation uses set_alt(). |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 836 | */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 837 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 838 | switch (ctrl->bRequestType) { |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 839 | case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 840 | pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE"); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 841 | value = audio_set_intf_req(f, ctrl); |
| 842 | break; |
| 843 | |
Laurent Pinchart | 512ad27 | 2009-06-21 23:23:05 +0200 | [diff] [blame] | 844 | case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 845 | pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE"); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 846 | value = audio_get_intf_req(f, ctrl); |
| 847 | break; |
| 848 | |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 849 | case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 850 | pr_debug("USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT"); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 851 | value = audio_set_endpoint_req(f, ctrl); |
| 852 | break; |
| 853 | |
| 854 | case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 855 | pr_debug("USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT"); |
Laurent Pinchart | 0ad7252 | 2009-10-21 00:03:39 +0200 | [diff] [blame] | 856 | value = audio_get_endpoint_req(f, ctrl); |
| 857 | break; |
| 858 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 859 | default: |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 860 | 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] | 861 | ctrl->bRequestType, ctrl->bRequest, |
| 862 | w_value, w_index, w_length); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 863 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* respond with data transfer or status phase? */ |
| 867 | if (value >= 0) { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 868 | 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] | 869 | ctrl->bRequestType, ctrl->bRequest, |
| 870 | w_value, w_index, w_length); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 871 | req->zero = 1; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 872 | req->length = value; |
| 873 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 874 | if (value < 0) |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 875 | pr_err("audio response failed on err %d\n", value); |
| 876 | } else { |
| 877 | pr_err("STALL\n"); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* device either stalls (value < 0) or reports success */ |
| 881 | return value; |
| 882 | } |
| 883 | |
| 884 | static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 885 | { |
| 886 | struct f_audio *audio = func_to_audio(f); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 887 | struct usb_ep *out_ep = audio->out_ep; |
| 888 | struct usb_ep *in_ep = audio->in_ep; |
| 889 | struct usb_request *req; |
| 890 | unsigned long flags; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 891 | int i = 0, err = 0; |
| 892 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 893 | pr_debug("intf %d, alt %d\n", intf, alt); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 894 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 895 | if (intf == ac_header_desc.baInterfaceNr[0]) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 896 | if (alt == 1) { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 897 | err = usb_ep_enable(in_ep); |
| 898 | if (err) { |
| 899 | pr_err("Failed to enable capture ep"); |
| 900 | return err; |
| 901 | } |
| 902 | in_ep->driver_data = audio; |
| 903 | audio->capture_copy_buf = 0; |
| 904 | |
| 905 | /* Allocate a write buffer */ |
| 906 | req = usb_ep_alloc_request(in_ep, GFP_ATOMIC); |
| 907 | if (!req) { |
| 908 | pr_err("request allocation failed\n"); |
Julia Lawall | ff3b968 | 2009-12-09 14:23:32 +0100 | [diff] [blame] | 909 | return -ENOMEM; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 910 | } |
| 911 | req->buf = kzalloc(req_capture_buf_size, |
| 912 | GFP_ATOMIC); |
| 913 | if (!req->buf) { |
| 914 | pr_err("request buffer allocation failed\n"); |
| 915 | return -ENOMEM; |
| 916 | } |
| 917 | |
| 918 | req->length = req_capture_buf_size; |
| 919 | req->context = audio; |
| 920 | req->complete = f_audio_complete; |
| 921 | audio->capture_req = req; |
| 922 | err = usb_ep_queue(in_ep, req, GFP_ATOMIC); |
| 923 | if (err) |
| 924 | pr_err("Failed to queue %s req: err %d\n", |
| 925 | in_ep->name, err); |
| 926 | schedule_work(&audio->capture_work); |
| 927 | } else { |
| 928 | struct f_audio_buf *capture_buf; |
| 929 | spin_lock_irqsave(&audio->capture_lock, flags); |
| 930 | while (!list_empty(&audio->capture_queue)) { |
| 931 | capture_buf = |
| 932 | list_first_entry( |
| 933 | &audio->capture_queue, |
| 934 | struct f_audio_buf, |
| 935 | list); |
| 936 | list_del(&capture_buf->list); |
| 937 | f_audio_buffer_free(capture_buf); |
| 938 | } |
| 939 | spin_unlock_irqrestore(&audio->capture_lock, flags); |
| 940 | } |
| 941 | } else if (intf == ac_header_desc.baInterfaceNr[1]) { |
| 942 | if (alt == 1) { |
| 943 | err = usb_ep_enable(out_ep); |
| 944 | if (err) { |
| 945 | pr_err("Failed to enable playback ep"); |
| 946 | return err; |
| 947 | } |
| 948 | out_ep->driver_data = audio; |
| 949 | audio->playback_copy_buf = |
| 950 | f_audio_buffer_alloc(audio_playback_buf_size); |
| 951 | if (IS_ERR(audio->playback_copy_buf)) { |
| 952 | pr_err("Failed to allocate playback_copy_buf"); |
| 953 | return -ENOMEM; |
| 954 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 955 | |
| 956 | /* |
| 957 | * allocate a bunch of read buffers |
| 958 | * and queue them all at once. |
| 959 | */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 960 | for (i = 0; i < req_playback_count && err == 0; i++) { |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 961 | req = usb_ep_alloc_request(out_ep, GFP_ATOMIC); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 962 | if (!req) { |
| 963 | pr_err("request allocation failed\n"); |
| 964 | return -ENOMEM; |
| 965 | } |
| 966 | req->buf = kzalloc(req_playback_buf_size, |
| 967 | GFP_ATOMIC); |
| 968 | if (!req->buf) { |
| 969 | pr_err("request buffer allocation failed\n"); |
| 970 | return -ENOMEM; |
| 971 | } |
| 972 | req->length = req_playback_buf_size; |
| 973 | req->context = audio; |
| 974 | req->complete = f_audio_complete; |
| 975 | err = usb_ep_queue(out_ep, req, GFP_ATOMIC); |
| 976 | if (err) |
| 977 | pr_err("Failed to queue %s queue req: err %d\n", |
| 978 | out_ep->name, err); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 979 | } |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 980 | pr_debug("Allocated %d requests\n", req_playback_count); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 981 | } else { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 982 | struct f_audio_buf *playback_copy_buf = |
| 983 | audio->playback_copy_buf; |
| 984 | if (playback_copy_buf) { |
| 985 | pr_err("Schedule playback_work"); |
| 986 | list_add_tail(&playback_copy_buf->list, |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 987 | &audio->play_queue); |
| 988 | schedule_work(&audio->playback_work); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 989 | } else { |
| 990 | pr_err("playback_buf is empty. Stop."); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 991 | } |
| 992 | } |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 993 | } else { |
| 994 | pr_err("Interface %d. Do nothing. Return %d\n", intf, err); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | return err; |
| 998 | } |
| 999 | |
| 1000 | static void f_audio_disable(struct usb_function *f) |
| 1001 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1002 | u_audio_clear(); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /*-------------------------------------------------------------------------*/ |
| 1006 | |
| 1007 | static void f_audio_build_desc(struct f_audio *audio) |
| 1008 | { |
| 1009 | struct gaudio *card = &audio->card; |
| 1010 | u8 *sam_freq; |
| 1011 | int rate; |
| 1012 | |
| 1013 | /* Set channel numbers */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1014 | speaker_input_terminal_desc.bNrChannels = |
| 1015 | u_audio_get_playback_channels(card); |
| 1016 | speaker_as_type_i_desc.bNrChannels = |
| 1017 | u_audio_get_playback_channels(card); |
| 1018 | |
| 1019 | microphone_input_terminal_desc.bNrChannels = |
| 1020 | u_audio_get_capture_channels(card); |
| 1021 | microphone_as_type_i_desc.bNrChannels = |
| 1022 | u_audio_get_capture_channels(card); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1023 | |
| 1024 | /* Set sample rates */ |
| 1025 | rate = u_audio_get_playback_rate(card); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1026 | sam_freq = speaker_as_type_i_desc.tSamFreq[0]; |
| 1027 | memcpy(sam_freq, &rate, 3); |
| 1028 | |
| 1029 | rate = u_audio_get_capture_rate(card); |
| 1030 | sam_freq = microphone_as_type_i_desc.tSamFreq[0]; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1031 | memcpy(sam_freq, &rate, 3); |
| 1032 | |
| 1033 | /* Todo: Set Sample bits and other parameters */ |
| 1034 | |
| 1035 | return; |
| 1036 | } |
| 1037 | |
| 1038 | /* audio function driver setup/binding */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1039 | static int |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1040 | f_audio_bind(struct usb_configuration *c, struct usb_function *f) |
| 1041 | { |
| 1042 | struct usb_composite_dev *cdev = c->cdev; |
| 1043 | struct f_audio *audio = func_to_audio(f); |
| 1044 | int status; |
| 1045 | struct usb_ep *ep; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1046 | u8 epaddr; |
| 1047 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1048 | |
| 1049 | f_audio_build_desc(audio); |
| 1050 | |
| 1051 | /* allocate instance-specific interface IDs, and patch descriptors */ |
| 1052 | status = usb_interface_id(c, f); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1053 | if (status < 0) { |
| 1054 | pr_err("%s: failed to allocate desc interface", __func__); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1055 | goto fail; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1056 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1057 | ac_interface_desc.bInterfaceNumber = status; |
| 1058 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1059 | status = -ENOMEM; |
| 1060 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1061 | status = usb_interface_id(c, f); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1062 | if (status < 0) { |
| 1063 | pr_err("%s: failed to allocate alt interface", __func__); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1064 | goto fail; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1065 | } |
| 1066 | microphone_as_interface_alt_0_desc.bInterfaceNumber = status; |
| 1067 | microphone_as_interface_alt_1_desc.bInterfaceNumber = status; |
| 1068 | ac_header_desc.baInterfaceNr[0] = status; |
| 1069 | |
| 1070 | status = -ENODEV; |
| 1071 | |
| 1072 | status = usb_interface_id(c, f); |
| 1073 | if (status < 0) { |
| 1074 | pr_err("%s: failed to allocate alt interface", __func__); |
| 1075 | goto fail; |
| 1076 | } |
| 1077 | speaker_as_interface_alt_0_desc.bInterfaceNumber = status; |
| 1078 | speaker_as_interface_alt_1_desc.bInterfaceNumber = status; |
| 1079 | ac_header_desc.baInterfaceNr[1] = status; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1080 | |
| 1081 | status = -ENODEV; |
| 1082 | |
| 1083 | /* allocate instance-specific endpoints */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1084 | ep = usb_ep_autoconfig(cdev->gadget, µphone_as_ep_in_desc); |
| 1085 | if (!ep) { |
| 1086 | pr_err("%s: failed to autoconfig in endpoint", __func__); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1087 | goto fail; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1088 | } |
| 1089 | audio->in_ep = ep; |
| 1090 | ep->desc = µphone_as_ep_in_desc; |
| 1091 | ep->driver_data = cdev; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1092 | |
| 1093 | status = -ENOMEM; |
| 1094 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1095 | ep = usb_ep_autoconfig(cdev->gadget, &speaker_as_ep_out_desc); |
| 1096 | if (!ep) { |
| 1097 | pr_err("%s: failed to autoconfig out endpoint", __func__); |
| 1098 | goto fail; |
| 1099 | } |
| 1100 | audio->out_ep = ep; |
| 1101 | ep->desc = &speaker_as_ep_out_desc; |
| 1102 | ep->driver_data = cdev; |
Felipe Balbi | ef7f584 | 2011-08-26 12:48:15 +0300 | [diff] [blame] | 1103 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1104 | /* associate bEndpointAddress with usb_function */ |
| 1105 | epaddr = microphone_as_ep_in_desc.bEndpointAddress & ~USB_DIR_IN; |
| 1106 | microphone_as_iso_in.id = epaddr; |
| 1107 | |
| 1108 | epaddr = speaker_as_ep_out_desc.bEndpointAddress & ~USB_DIR_IN; |
| 1109 | speaker_as_iso_out.id = epaddr; |
| 1110 | |
| 1111 | /* support all relevant hardware speeds. we expect that when |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1112 | * hardware is dual speed, all bulk-capable endpoints work at |
| 1113 | * both speeds |
| 1114 | */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1115 | |
| 1116 | /* copy descriptors, and track endpoint copies */ |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1117 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 1118 | c->highspeed = true; |
| 1119 | f->hs_descriptors = usb_copy_descriptors(f_audio_desc); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1120 | } else { |
| 1121 | f->descriptors = usb_copy_descriptors(f_audio_desc); |
Felipe Balbi | ef7f584 | 2011-08-26 12:48:15 +0300 | [diff] [blame] | 1122 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1123 | |
| 1124 | return 0; |
| 1125 | |
| 1126 | fail: |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1127 | return status; |
| 1128 | } |
| 1129 | |
| 1130 | static void |
| 1131 | f_audio_unbind(struct usb_configuration *c, struct usb_function *f) |
| 1132 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1133 | struct f_audio *audio = func_to_audio(f); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1134 | |
| 1135 | usb_free_descriptors(f->descriptors); |
Sebastian Andrzej Siewior | 16a2f97 | 2011-04-11 20:44:30 +0200 | [diff] [blame] | 1136 | usb_free_descriptors(f->hs_descriptors); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1137 | kfree(audio); |
| 1138 | } |
| 1139 | |
| 1140 | /*-------------------------------------------------------------------------*/ |
| 1141 | |
Laurent Pinchart | b95cd7e | 2009-06-21 23:21:55 +0200 | [diff] [blame] | 1142 | static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value) |
| 1143 | { |
| 1144 | con->data[cmd] = value; |
| 1145 | |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | static int generic_get_cmd(struct usb_audio_control *con, u8 cmd) |
| 1150 | { |
| 1151 | return con->data[cmd]; |
| 1152 | } |
| 1153 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1154 | /* Todo: add more control selecotor dynamically */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1155 | int control_selector_init(struct f_audio *audio) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1156 | { |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1157 | INIT_LIST_HEAD(&audio->fu_cs); |
| 1158 | list_add(µphone_fu_controls.list, &audio->fu_cs); |
| 1159 | list_add(&speaker_fu_controls.list, &audio->fu_cs); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1160 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1161 | INIT_LIST_HEAD(µphone_fu_controls.control); |
| 1162 | list_add(µphone_mute_control.list, |
| 1163 | µphone_fu_controls.control); |
| 1164 | list_add(µphone_volume_control.list, |
| 1165 | µphone_fu_controls.control); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1166 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1167 | INIT_LIST_HEAD(&speaker_fu_controls.control); |
| 1168 | list_add(&speaker_mute_control.list, |
| 1169 | &speaker_fu_controls.control); |
| 1170 | list_add(&speaker_volume_control.list, |
| 1171 | &speaker_fu_controls.control); |
| 1172 | |
| 1173 | microphone_volume_control.data[UAC__CUR] = 0xffc0; |
| 1174 | microphone_volume_control.data[UAC__MIN] = 0xe3a0; |
| 1175 | microphone_volume_control.data[UAC__MAX] = 0xfff0; |
| 1176 | microphone_volume_control.data[UAC__RES] = 0x0030; |
| 1177 | |
| 1178 | speaker_volume_control.data[UAC__CUR] = 0xffc0; |
| 1179 | speaker_volume_control.data[UAC__MIN] = 0xe3a0; |
| 1180 | speaker_volume_control.data[UAC__MAX] = 0xfff0; |
| 1181 | speaker_volume_control.data[UAC__RES] = 0x0030; |
| 1182 | |
| 1183 | INIT_LIST_HEAD(&audio->ep_cs); |
| 1184 | list_add(&speaker_as_iso_out.list, &audio->ep_cs); |
| 1185 | list_add(µphone_as_iso_in.list, &audio->ep_cs); |
| 1186 | |
| 1187 | INIT_LIST_HEAD(µphone_as_iso_in.control); |
| 1188 | list_add(µphone_sample_freq_control.list, |
| 1189 | µphone_as_iso_in.control); |
| 1190 | |
| 1191 | INIT_LIST_HEAD(&speaker_as_iso_out.control); |
| 1192 | list_add(&speaker_sample_freq_control.list, |
| 1193 | &speaker_as_iso_out.control); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1194 | |
| 1195 | return 0; |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1196 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1200 | * audio_bind_config - add USB audio function to a configuration |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1201 | * @c: the configuration to support the USB audio function |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1202 | * Context: single threaded during gadget setup |
| 1203 | * |
| 1204 | * Returns zero on success, else negative errno. |
| 1205 | */ |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1206 | int audio_bind_config(struct usb_configuration *c) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1207 | { |
| 1208 | struct f_audio *audio; |
| 1209 | int status; |
| 1210 | |
| 1211 | /* allocate and initialize one new instance */ |
| 1212 | audio = kzalloc(sizeof *audio, GFP_KERNEL); |
| 1213 | if (!audio) |
| 1214 | return -ENOMEM; |
| 1215 | |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1216 | audio->card.gadget = c->cdev->gadget; |
| 1217 | |
| 1218 | INIT_LIST_HEAD(&audio->play_queue); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1219 | spin_lock_init(&audio->playback_lock); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1220 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1221 | INIT_LIST_HEAD(&audio->capture_queue); |
| 1222 | spin_lock_init(&audio->capture_lock); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1223 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1224 | audio->card.func.name = "audio"; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1225 | audio->card.func.strings = audio_strings; |
| 1226 | audio->card.func.bind = f_audio_bind; |
| 1227 | audio->card.func.unbind = f_audio_unbind; |
| 1228 | audio->card.func.set_alt = f_audio_set_alt; |
| 1229 | audio->card.func.setup = f_audio_setup; |
| 1230 | audio->card.func.disable = f_audio_disable; |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1231 | |
| 1232 | control_selector_init(audio); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1233 | INIT_WORK(&audio->playback_work, f_audio_playback_work); |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1234 | INIT_WORK(&audio->capture_work, f_audio_capture_work); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1235 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1236 | /* set up ASLA audio devices */ |
| 1237 | status = gaudio_setup(&audio->card); |
| 1238 | if (status < 0) |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1239 | goto add_fail; |
| 1240 | |
Anna Perel | 432367a | 2012-09-20 10:55:32 +0300 | [diff] [blame] | 1241 | status = usb_add_function(c, &audio->card.func); |
| 1242 | if (status) { |
| 1243 | pr_err("%s: Failed to add usb audio function, err = %d", |
| 1244 | __func__, status); |
| 1245 | goto setup_fail; |
| 1246 | } |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1247 | |
| 1248 | return status; |
| 1249 | |
| 1250 | add_fail: |
Cliff Cai | feef1d9 | 2009-12-09 22:28:39 -0500 | [diff] [blame] | 1251 | gaudio_cleanup(); |
Bryan Wu | c6994e6 | 2009-06-03 09:17:58 -0400 | [diff] [blame] | 1252 | setup_fail: |
| 1253 | kfree(audio); |
| 1254 | return status; |
| 1255 | } |