Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Gadget Function Driver for USB audio source device |
| 3 | * |
| 4 | * Copyright (C) 2012 Google, Inc. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/usb/audio.h> |
| 19 | #include <linux/wait.h> |
| 20 | #include <sound/core.h> |
| 21 | #include <sound/initval.h> |
| 22 | #include <sound/pcm.h> |
| 23 | |
| 24 | #define SAMPLE_RATE 44100 |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 25 | #define FRAMES_PER_MSEC (SAMPLE_RATE / 1000) |
Mike Lockwood | db2e707 | 2012-05-27 15:41:53 -0700 | [diff] [blame] | 26 | |
Vijayavardhan Vennapusa | e02f8f3 | 2012-10-05 15:45:16 +0530 | [diff] [blame] | 27 | #define IN_EP_MAX_PACKET_SIZE 256 |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 28 | |
| 29 | /* Number of requests to allocate */ |
Mike Lockwood | db2e707 | 2012-05-27 15:41:53 -0700 | [diff] [blame] | 30 | #define IN_EP_REQ_COUNT 4 |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 31 | |
| 32 | #define AUDIO_AC_INTERFACE 0 |
| 33 | #define AUDIO_AS_INTERFACE 1 |
| 34 | #define AUDIO_NUM_INTERFACES 2 |
| 35 | |
| 36 | /* B.3.1 Standard AC Interface Descriptor */ |
| 37 | static struct usb_interface_descriptor audio_source_ac_interface_desc = { |
| 38 | .bLength = USB_DT_INTERFACE_SIZE, |
| 39 | .bDescriptorType = USB_DT_INTERFACE, |
| 40 | .bNumEndpoints = 0, |
| 41 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 42 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(AUDIO_NUM_INTERFACES) |
| 47 | /* 1 input terminal, 1 output terminal and 1 feature unit */ |
| 48 | #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH \ |
| 49 | + UAC_DT_INPUT_TERMINAL_SIZE + UAC_DT_OUTPUT_TERMINAL_SIZE \ |
| 50 | + UAC_DT_FEATURE_UNIT_SIZE(0)) |
| 51 | /* B.3.2 Class-Specific AC Interface Descriptor */ |
| 52 | static struct uac1_ac_header_descriptor_2 audio_source_ac_header_desc = { |
| 53 | .bLength = UAC_DT_AC_HEADER_LENGTH, |
| 54 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 55 | .bDescriptorSubtype = UAC_HEADER, |
| 56 | .bcdADC = __constant_cpu_to_le16(0x0100), |
| 57 | .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH), |
| 58 | .bInCollection = AUDIO_NUM_INTERFACES, |
| 59 | .baInterfaceNr = { |
| 60 | [0] = AUDIO_AC_INTERFACE, |
| 61 | [1] = AUDIO_AS_INTERFACE, |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | #define INPUT_TERMINAL_ID 1 |
| 66 | static struct uac_input_terminal_descriptor input_terminal_desc = { |
| 67 | .bLength = UAC_DT_INPUT_TERMINAL_SIZE, |
| 68 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 69 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, |
| 70 | .bTerminalID = INPUT_TERMINAL_ID, |
| 71 | .wTerminalType = UAC_INPUT_TERMINAL_MICROPHONE, |
| 72 | .bAssocTerminal = 0, |
| 73 | .wChannelConfig = 0x3, |
| 74 | }; |
| 75 | |
| 76 | DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0); |
| 77 | |
| 78 | #define FEATURE_UNIT_ID 2 |
| 79 | static struct uac_feature_unit_descriptor_0 feature_unit_desc = { |
| 80 | .bLength = UAC_DT_FEATURE_UNIT_SIZE(0), |
| 81 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 82 | .bDescriptorSubtype = UAC_FEATURE_UNIT, |
| 83 | .bUnitID = FEATURE_UNIT_ID, |
| 84 | .bSourceID = INPUT_TERMINAL_ID, |
| 85 | .bControlSize = 2, |
| 86 | }; |
| 87 | |
| 88 | #define OUTPUT_TERMINAL_ID 3 |
| 89 | static struct uac1_output_terminal_descriptor output_terminal_desc = { |
| 90 | .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, |
| 91 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 92 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, |
| 93 | .bTerminalID = OUTPUT_TERMINAL_ID, |
| 94 | .wTerminalType = UAC_TERMINAL_STREAMING, |
| 95 | .bAssocTerminal = FEATURE_UNIT_ID, |
| 96 | .bSourceID = FEATURE_UNIT_ID, |
| 97 | }; |
| 98 | |
| 99 | /* B.4.1 Standard AS Interface Descriptor */ |
| 100 | static struct usb_interface_descriptor as_interface_alt_0_desc = { |
| 101 | .bLength = USB_DT_INTERFACE_SIZE, |
| 102 | .bDescriptorType = USB_DT_INTERFACE, |
| 103 | .bAlternateSetting = 0, |
| 104 | .bNumEndpoints = 0, |
| 105 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 106 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 107 | }; |
| 108 | |
| 109 | static struct usb_interface_descriptor as_interface_alt_1_desc = { |
| 110 | .bLength = USB_DT_INTERFACE_SIZE, |
| 111 | .bDescriptorType = USB_DT_INTERFACE, |
| 112 | .bAlternateSetting = 1, |
| 113 | .bNumEndpoints = 1, |
| 114 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 115 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
| 116 | }; |
| 117 | |
| 118 | /* B.4.2 Class-Specific AS Interface Descriptor */ |
| 119 | static struct uac1_as_header_descriptor as_header_desc = { |
| 120 | .bLength = UAC_DT_AS_HEADER_SIZE, |
| 121 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 122 | .bDescriptorSubtype = UAC_AS_GENERAL, |
| 123 | .bTerminalLink = INPUT_TERMINAL_ID, |
| 124 | .bDelay = 1, |
| 125 | .wFormatTag = UAC_FORMAT_TYPE_I_PCM, |
| 126 | }; |
| 127 | |
| 128 | static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = { |
| 129 | .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1), |
| 130 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 131 | .bDescriptorSubtype = UAC_FORMAT_TYPE, |
| 132 | .bFormatType = UAC_FORMAT_TYPE_I, |
| 133 | .bSubframeSize = 2, |
| 134 | .bBitResolution = 16, |
| 135 | .bSamFreqType = 1, |
| 136 | }; |
| 137 | |
| 138 | /* Standard ISO IN Endpoint Descriptor for highspeed */ |
| 139 | static struct usb_endpoint_descriptor hs_as_in_ep_desc = { |
| 140 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
| 141 | .bDescriptorType = USB_DT_ENDPOINT, |
| 142 | .bEndpointAddress = USB_DIR_IN, |
| 143 | .bmAttributes = USB_ENDPOINT_SYNC_SYNC |
| 144 | | USB_ENDPOINT_XFER_ISOC, |
| 145 | .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE), |
| 146 | .bInterval = 4, /* poll 1 per millisecond */ |
| 147 | }; |
| 148 | |
| 149 | /* Standard ISO IN Endpoint Descriptor for highspeed */ |
| 150 | static struct usb_endpoint_descriptor fs_as_in_ep_desc = { |
| 151 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
| 152 | .bDescriptorType = USB_DT_ENDPOINT, |
| 153 | .bEndpointAddress = USB_DIR_IN, |
| 154 | .bmAttributes = USB_ENDPOINT_SYNC_SYNC |
| 155 | | USB_ENDPOINT_XFER_ISOC, |
| 156 | .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE), |
| 157 | .bInterval = 1, /* poll 1 per millisecond */ |
| 158 | }; |
| 159 | |
| 160 | /* Class-specific AS ISO OUT Endpoint Descriptor */ |
| 161 | static struct uac_iso_endpoint_descriptor as_iso_in_desc = { |
| 162 | .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, |
| 163 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
| 164 | .bDescriptorSubtype = UAC_EP_GENERAL, |
| 165 | .bmAttributes = 1, |
| 166 | .bLockDelayUnits = 1, |
| 167 | .wLockDelay = __constant_cpu_to_le16(1), |
| 168 | }; |
| 169 | |
| 170 | static struct usb_descriptor_header *hs_audio_desc[] = { |
| 171 | (struct usb_descriptor_header *)&audio_source_ac_interface_desc, |
| 172 | (struct usb_descriptor_header *)&audio_source_ac_header_desc, |
| 173 | |
| 174 | (struct usb_descriptor_header *)&input_terminal_desc, |
| 175 | (struct usb_descriptor_header *)&output_terminal_desc, |
| 176 | (struct usb_descriptor_header *)&feature_unit_desc, |
| 177 | |
| 178 | (struct usb_descriptor_header *)&as_interface_alt_0_desc, |
| 179 | (struct usb_descriptor_header *)&as_interface_alt_1_desc, |
| 180 | (struct usb_descriptor_header *)&as_header_desc, |
| 181 | |
| 182 | (struct usb_descriptor_header *)&as_type_i_desc, |
| 183 | |
| 184 | (struct usb_descriptor_header *)&hs_as_in_ep_desc, |
| 185 | (struct usb_descriptor_header *)&as_iso_in_desc, |
| 186 | NULL, |
| 187 | }; |
| 188 | |
| 189 | static struct usb_descriptor_header *fs_audio_desc[] = { |
| 190 | (struct usb_descriptor_header *)&audio_source_ac_interface_desc, |
| 191 | (struct usb_descriptor_header *)&audio_source_ac_header_desc, |
| 192 | |
| 193 | (struct usb_descriptor_header *)&input_terminal_desc, |
| 194 | (struct usb_descriptor_header *)&output_terminal_desc, |
| 195 | (struct usb_descriptor_header *)&feature_unit_desc, |
| 196 | |
| 197 | (struct usb_descriptor_header *)&as_interface_alt_0_desc, |
| 198 | (struct usb_descriptor_header *)&as_interface_alt_1_desc, |
| 199 | (struct usb_descriptor_header *)&as_header_desc, |
| 200 | |
| 201 | (struct usb_descriptor_header *)&as_type_i_desc, |
| 202 | |
| 203 | (struct usb_descriptor_header *)&fs_as_in_ep_desc, |
| 204 | (struct usb_descriptor_header *)&as_iso_in_desc, |
| 205 | NULL, |
| 206 | }; |
| 207 | |
| 208 | static struct snd_pcm_hardware audio_hw_info = { |
| 209 | .info = SNDRV_PCM_INFO_MMAP | |
| 210 | SNDRV_PCM_INFO_MMAP_VALID | |
| 211 | SNDRV_PCM_INFO_BATCH | |
| 212 | SNDRV_PCM_INFO_INTERLEAVED | |
| 213 | SNDRV_PCM_INFO_BLOCK_TRANSFER, |
| 214 | |
| 215 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 216 | .channels_min = 2, |
| 217 | .channels_max = 2, |
| 218 | .rate_min = SAMPLE_RATE, |
| 219 | .rate_max = SAMPLE_RATE, |
| 220 | |
| 221 | .buffer_bytes_max = 1024 * 1024, |
| 222 | .period_bytes_min = 64, |
| 223 | .period_bytes_max = 512 * 1024, |
| 224 | .periods_min = 2, |
| 225 | .periods_max = 1024, |
| 226 | }; |
| 227 | |
| 228 | /*-------------------------------------------------------------------------*/ |
| 229 | |
| 230 | struct audio_source_config { |
| 231 | int card; |
| 232 | int device; |
| 233 | }; |
| 234 | |
| 235 | struct audio_dev { |
| 236 | struct usb_function func; |
| 237 | struct snd_card *card; |
| 238 | struct snd_pcm *pcm; |
| 239 | struct snd_pcm_substream *substream; |
| 240 | |
| 241 | struct list_head idle_reqs; |
| 242 | struct usb_ep *in_ep; |
| 243 | |
| 244 | spinlock_t lock; |
| 245 | |
| 246 | /* beginning, end and current position in our buffer */ |
| 247 | void *buffer_start; |
| 248 | void *buffer_end; |
| 249 | void *buffer_pos; |
| 250 | |
| 251 | /* byte size of a "period" */ |
| 252 | unsigned int period; |
| 253 | /* bytes sent since last call to snd_pcm_period_elapsed */ |
| 254 | unsigned int period_offset; |
| 255 | /* time we started playing */ |
| 256 | ktime_t start_time; |
| 257 | /* number of frames sent since start_time */ |
| 258 | s64 frames_sent; |
Vijayavardhan Vennapusa | 15285e6 | 2013-01-10 12:27:16 +0530 | [diff] [blame] | 259 | |
| 260 | bool audio_ep_enabled; |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | static inline struct audio_dev *func_to_audio_source(struct usb_function *f) |
| 264 | { |
| 265 | return container_of(f, struct audio_dev, func); |
| 266 | } |
| 267 | |
| 268 | /*-------------------------------------------------------------------------*/ |
| 269 | |
| 270 | static struct usb_request *audio_request_new(struct usb_ep *ep, int buffer_size) |
| 271 | { |
| 272 | struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL); |
| 273 | if (!req) |
| 274 | return NULL; |
| 275 | |
| 276 | req->buf = kmalloc(buffer_size, GFP_KERNEL); |
| 277 | if (!req->buf) { |
| 278 | usb_ep_free_request(ep, req); |
| 279 | return NULL; |
| 280 | } |
| 281 | req->length = buffer_size; |
| 282 | return req; |
| 283 | } |
| 284 | |
| 285 | static void audio_request_free(struct usb_request *req, struct usb_ep *ep) |
| 286 | { |
| 287 | if (req) { |
| 288 | kfree(req->buf); |
| 289 | usb_ep_free_request(ep, req); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | static void audio_req_put(struct audio_dev *audio, struct usb_request *req) |
| 294 | { |
| 295 | unsigned long flags; |
| 296 | |
| 297 | spin_lock_irqsave(&audio->lock, flags); |
| 298 | list_add_tail(&req->list, &audio->idle_reqs); |
| 299 | spin_unlock_irqrestore(&audio->lock, flags); |
| 300 | } |
| 301 | |
| 302 | static struct usb_request *audio_req_get(struct audio_dev *audio) |
| 303 | { |
| 304 | unsigned long flags; |
| 305 | struct usb_request *req; |
| 306 | |
| 307 | spin_lock_irqsave(&audio->lock, flags); |
| 308 | if (list_empty(&audio->idle_reqs)) { |
| 309 | req = 0; |
| 310 | } else { |
| 311 | req = list_first_entry(&audio->idle_reqs, struct usb_request, |
| 312 | list); |
| 313 | list_del(&req->list); |
| 314 | } |
| 315 | spin_unlock_irqrestore(&audio->lock, flags); |
| 316 | return req; |
| 317 | } |
| 318 | |
| 319 | /* send the appropriate number of packets to match our bitrate */ |
| 320 | static void audio_send(struct audio_dev *audio) |
| 321 | { |
| 322 | struct snd_pcm_runtime *runtime; |
| 323 | struct usb_request *req; |
| 324 | int length, length1, length2, ret; |
| 325 | s64 msecs; |
| 326 | s64 frames; |
| 327 | ktime_t now; |
| 328 | |
| 329 | /* audio->substream will be null if we have been closed */ |
| 330 | if (!audio->substream) |
| 331 | return; |
| 332 | /* audio->buffer_pos will be null if we have been stopped */ |
| 333 | if (!audio->buffer_pos) |
| 334 | return; |
| 335 | |
| 336 | runtime = audio->substream->runtime; |
| 337 | |
Mike Lockwood | db2e707 | 2012-05-27 15:41:53 -0700 | [diff] [blame] | 338 | /* compute number of frames to send */ |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 339 | now = ktime_get(); |
| 340 | msecs = ktime_to_ns(now) - ktime_to_ns(audio->start_time); |
| 341 | do_div(msecs, 1000000); |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 342 | frames = msecs * SAMPLE_RATE; |
| 343 | do_div(frames, 1000); |
| 344 | |
| 345 | /* Readjust our frames_sent if we fall too far behind. |
| 346 | * If we get too far behind it is better to drop some frames than |
| 347 | * to keep sending data too fast in an attempt to catch up. |
| 348 | */ |
Mike Lockwood | db2e707 | 2012-05-27 15:41:53 -0700 | [diff] [blame] | 349 | if (frames - audio->frames_sent > 10 * FRAMES_PER_MSEC) |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 350 | audio->frames_sent = frames - FRAMES_PER_MSEC; |
| 351 | |
| 352 | frames -= audio->frames_sent; |
| 353 | |
Mike Lockwood | db2e707 | 2012-05-27 15:41:53 -0700 | [diff] [blame] | 354 | /* We need to send something to keep the pipeline going */ |
| 355 | if (frames <= 0) |
| 356 | frames = FRAMES_PER_MSEC; |
| 357 | |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 358 | while (frames > 0) { |
| 359 | req = audio_req_get(audio); |
| 360 | if (!req) |
| 361 | break; |
| 362 | |
| 363 | length = frames_to_bytes(runtime, frames); |
| 364 | if (length > IN_EP_MAX_PACKET_SIZE) |
| 365 | length = IN_EP_MAX_PACKET_SIZE; |
| 366 | |
| 367 | if (audio->buffer_pos + length > audio->buffer_end) |
| 368 | length1 = audio->buffer_end - audio->buffer_pos; |
| 369 | else |
| 370 | length1 = length; |
| 371 | memcpy(req->buf, audio->buffer_pos, length1); |
| 372 | if (length1 < length) { |
| 373 | /* Wrap around and copy remaining length |
| 374 | * at beginning of buffer. |
| 375 | */ |
| 376 | length2 = length - length1; |
| 377 | memcpy(req->buf + length1, audio->buffer_start, |
| 378 | length2); |
| 379 | audio->buffer_pos = audio->buffer_start + length2; |
| 380 | } else { |
| 381 | audio->buffer_pos += length1; |
| 382 | if (audio->buffer_pos >= audio->buffer_end) |
| 383 | audio->buffer_pos = audio->buffer_start; |
| 384 | } |
| 385 | |
| 386 | req->length = length; |
| 387 | ret = usb_ep_queue(audio->in_ep, req, GFP_ATOMIC); |
| 388 | if (ret < 0) { |
| 389 | pr_err("usb_ep_queue failed ret: %d\n", ret); |
| 390 | audio_req_put(audio, req); |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | frames -= bytes_to_frames(runtime, length); |
| 395 | audio->frames_sent += bytes_to_frames(runtime, length); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static void audio_control_complete(struct usb_ep *ep, struct usb_request *req) |
| 400 | { |
| 401 | /* nothing to do here */ |
| 402 | } |
| 403 | |
| 404 | static void audio_data_complete(struct usb_ep *ep, struct usb_request *req) |
| 405 | { |
| 406 | struct audio_dev *audio = req->context; |
| 407 | |
| 408 | pr_debug("audio_data_complete req->status %d req->actual %d\n", |
| 409 | req->status, req->actual); |
| 410 | |
| 411 | audio_req_put(audio, req); |
| 412 | |
Mike Lockwood | cbc8ed8 | 2012-08-02 11:22:05 -0700 | [diff] [blame] | 413 | if (!audio->buffer_start || req->status) |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 414 | return; |
| 415 | |
| 416 | audio->period_offset += req->actual; |
| 417 | if (audio->period_offset >= audio->period) { |
| 418 | snd_pcm_period_elapsed(audio->substream); |
| 419 | audio->period_offset = 0; |
| 420 | } |
| 421 | audio_send(audio); |
| 422 | } |
| 423 | |
| 424 | static int audio_source_set_endpoint_req(struct usb_function *f, |
| 425 | const struct usb_ctrlrequest *ctrl) |
| 426 | { |
| 427 | int value = -EOPNOTSUPP; |
| 428 | u16 ep = le16_to_cpu(ctrl->wIndex); |
| 429 | u16 len = le16_to_cpu(ctrl->wLength); |
| 430 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 431 | |
| 432 | pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", |
| 433 | ctrl->bRequest, w_value, len, ep); |
| 434 | |
| 435 | switch (ctrl->bRequest) { |
| 436 | case UAC_SET_CUR: |
| 437 | case UAC_SET_MIN: |
| 438 | case UAC_SET_MAX: |
| 439 | case UAC_SET_RES: |
| 440 | value = len; |
| 441 | break; |
| 442 | default: |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | return value; |
| 447 | } |
| 448 | |
| 449 | static int audio_source_get_endpoint_req(struct usb_function *f, |
| 450 | const struct usb_ctrlrequest *ctrl) |
| 451 | { |
| 452 | struct usb_composite_dev *cdev = f->config->cdev; |
| 453 | int value = -EOPNOTSUPP; |
| 454 | u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); |
| 455 | u16 len = le16_to_cpu(ctrl->wLength); |
| 456 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 457 | u8 *buf = cdev->req->buf; |
| 458 | |
| 459 | pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", |
| 460 | ctrl->bRequest, w_value, len, ep); |
| 461 | |
| 462 | if (w_value == UAC_EP_CS_ATTR_SAMPLE_RATE << 8) { |
| 463 | switch (ctrl->bRequest) { |
| 464 | case UAC_GET_CUR: |
| 465 | case UAC_GET_MIN: |
| 466 | case UAC_GET_MAX: |
| 467 | case UAC_GET_RES: |
| 468 | /* return our sample rate */ |
| 469 | buf[0] = (u8)SAMPLE_RATE; |
| 470 | buf[1] = (u8)(SAMPLE_RATE >> 8); |
| 471 | buf[2] = (u8)(SAMPLE_RATE >> 16); |
| 472 | value = 3; |
| 473 | break; |
| 474 | default: |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return value; |
| 480 | } |
| 481 | |
| 482 | static int |
| 483 | audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 484 | { |
| 485 | struct usb_composite_dev *cdev = f->config->cdev; |
| 486 | struct usb_request *req = cdev->req; |
| 487 | int value = -EOPNOTSUPP; |
| 488 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 489 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 490 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 491 | |
| 492 | /* composite driver infrastructure handles everything; interface |
| 493 | * activation uses set_alt(). |
| 494 | */ |
| 495 | switch (ctrl->bRequestType) { |
| 496 | case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
| 497 | value = audio_source_set_endpoint_req(f, ctrl); |
| 498 | break; |
| 499 | |
| 500 | case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
| 501 | value = audio_source_get_endpoint_req(f, ctrl); |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | /* respond with data transfer or status phase? */ |
| 506 | if (value >= 0) { |
| 507 | pr_debug("audio req%02x.%02x v%04x i%04x l%d\n", |
| 508 | ctrl->bRequestType, ctrl->bRequest, |
| 509 | w_value, w_index, w_length); |
| 510 | req->zero = 0; |
| 511 | req->length = value; |
| 512 | req->complete = audio_control_complete; |
| 513 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 514 | if (value < 0) |
| 515 | pr_err("audio response on err %d\n", value); |
| 516 | } |
| 517 | |
| 518 | /* device either stalls (value < 0) or reports success */ |
| 519 | return value; |
| 520 | } |
| 521 | |
| 522 | static int audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 523 | { |
| 524 | struct audio_dev *audio = func_to_audio_source(f); |
| 525 | struct usb_composite_dev *cdev = f->config->cdev; |
| 526 | int ret; |
| 527 | |
| 528 | pr_debug("audio_set_alt intf %d, alt %d\n", intf, alt); |
| 529 | |
Vijayavardhan Vennapusa | 15285e6 | 2013-01-10 12:27:16 +0530 | [diff] [blame] | 530 | if (intf == as_interface_alt_1_desc.bInterfaceNumber) { |
| 531 | if (alt && !audio->audio_ep_enabled) { |
| 532 | ret = config_ep_by_speed(cdev->gadget, f, audio->in_ep); |
| 533 | if (ret) { |
| 534 | audio->in_ep->desc = NULL; |
| 535 | ERROR(cdev, "config_ep fail ep %s, result %d\n", |
| 536 | audio->in_ep->name, ret); |
| 537 | return ret; |
| 538 | } |
| 539 | ret = usb_ep_enable(audio->in_ep); |
| 540 | if (ret) { |
| 541 | ERROR(cdev, "failedto enable ep%s, result %d\n", |
| 542 | audio->in_ep->name, ret); |
| 543 | return ret; |
| 544 | } |
| 545 | audio->audio_ep_enabled = true; |
| 546 | } else if (!alt && audio->audio_ep_enabled) { |
| 547 | usb_ep_disable(audio->in_ep); |
| 548 | audio->audio_ep_enabled = false; |
| 549 | } |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 550 | } |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static void audio_disable(struct usb_function *f) |
| 555 | { |
| 556 | struct audio_dev *audio = func_to_audio_source(f); |
| 557 | |
| 558 | pr_debug("audio_disable\n"); |
Vijayavardhan Vennapusa | 15285e6 | 2013-01-10 12:27:16 +0530 | [diff] [blame] | 559 | if (audio->audio_ep_enabled) { |
| 560 | usb_ep_disable(audio->in_ep); |
| 561 | audio->audio_ep_enabled = false; |
| 562 | } |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /*-------------------------------------------------------------------------*/ |
| 566 | |
| 567 | static void audio_build_desc(struct audio_dev *audio) |
| 568 | { |
| 569 | u8 *sam_freq; |
| 570 | int rate; |
| 571 | |
| 572 | /* Set channel numbers */ |
| 573 | input_terminal_desc.bNrChannels = 2; |
| 574 | as_type_i_desc.bNrChannels = 2; |
| 575 | |
| 576 | /* Set sample rates */ |
| 577 | rate = SAMPLE_RATE; |
| 578 | sam_freq = as_type_i_desc.tSamFreq[0]; |
| 579 | memcpy(sam_freq, &rate, 3); |
| 580 | } |
| 581 | |
| 582 | /* audio function driver setup/binding */ |
| 583 | static int |
| 584 | audio_bind(struct usb_configuration *c, struct usb_function *f) |
| 585 | { |
| 586 | struct usb_composite_dev *cdev = c->cdev; |
| 587 | struct audio_dev *audio = func_to_audio_source(f); |
| 588 | int status; |
| 589 | struct usb_ep *ep; |
| 590 | struct usb_request *req; |
| 591 | int i; |
| 592 | |
| 593 | audio_build_desc(audio); |
| 594 | |
| 595 | /* allocate instance-specific interface IDs, and patch descriptors */ |
| 596 | status = usb_interface_id(c, f); |
| 597 | if (status < 0) |
| 598 | goto fail; |
| 599 | audio_source_ac_interface_desc.bInterfaceNumber = status; |
| 600 | |
| 601 | status = usb_interface_id(c, f); |
| 602 | if (status < 0) |
| 603 | goto fail; |
| 604 | as_interface_alt_0_desc.bInterfaceNumber = status; |
| 605 | as_interface_alt_1_desc.bInterfaceNumber = status; |
| 606 | |
| 607 | status = -ENODEV; |
| 608 | |
| 609 | /* allocate our endpoint */ |
| 610 | ep = usb_ep_autoconfig(cdev->gadget, &fs_as_in_ep_desc); |
| 611 | if (!ep) |
| 612 | goto fail; |
| 613 | audio->in_ep = ep; |
| 614 | ep->driver_data = audio; /* claim */ |
| 615 | |
| 616 | if (gadget_is_dualspeed(c->cdev->gadget)) |
| 617 | hs_as_in_ep_desc.bEndpointAddress = |
| 618 | fs_as_in_ep_desc.bEndpointAddress; |
| 619 | |
| 620 | for (i = 0, status = 0; i < IN_EP_REQ_COUNT && status == 0; i++) { |
| 621 | req = audio_request_new(ep, IN_EP_MAX_PACKET_SIZE); |
| 622 | if (req) { |
| 623 | req->context = audio; |
| 624 | req->complete = audio_data_complete; |
| 625 | audio_req_put(audio, req); |
| 626 | } else |
| 627 | status = -ENOMEM; |
| 628 | } |
| 629 | |
| 630 | fail: |
| 631 | return status; |
| 632 | } |
| 633 | |
| 634 | static void |
| 635 | audio_unbind(struct usb_configuration *c, struct usb_function *f) |
| 636 | { |
| 637 | struct audio_dev *audio = func_to_audio_source(f); |
| 638 | struct usb_request *req; |
| 639 | |
| 640 | while ((req = audio_req_get(audio))) |
| 641 | audio_request_free(req, audio->in_ep); |
| 642 | |
| 643 | snd_card_free_when_closed(audio->card); |
Mike Lockwood | 785e1af | 2012-09-05 15:23:57 +0530 | [diff] [blame] | 644 | audio->card = NULL; |
| 645 | audio->pcm = NULL; |
| 646 | audio->substream = NULL; |
| 647 | audio->in_ep = NULL; |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static void audio_pcm_playback_start(struct audio_dev *audio) |
| 651 | { |
| 652 | audio->start_time = ktime_get(); |
| 653 | audio->frames_sent = 0; |
| 654 | audio_send(audio); |
| 655 | } |
| 656 | |
| 657 | static void audio_pcm_playback_stop(struct audio_dev *audio) |
| 658 | { |
| 659 | unsigned long flags; |
| 660 | |
| 661 | spin_lock_irqsave(&audio->lock, flags); |
| 662 | audio->buffer_start = 0; |
| 663 | audio->buffer_end = 0; |
| 664 | audio->buffer_pos = 0; |
| 665 | spin_unlock_irqrestore(&audio->lock, flags); |
| 666 | } |
| 667 | |
| 668 | static int audio_pcm_open(struct snd_pcm_substream *substream) |
| 669 | { |
| 670 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 671 | struct audio_dev *audio = substream->private_data; |
| 672 | |
| 673 | runtime->private_data = audio; |
| 674 | runtime->hw = audio_hw_info; |
| 675 | snd_pcm_limit_hw_rates(runtime); |
| 676 | runtime->hw.channels_max = 2; |
| 677 | |
| 678 | audio->substream = substream; |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static int audio_pcm_close(struct snd_pcm_substream *substream) |
| 683 | { |
| 684 | struct audio_dev *audio = substream->private_data; |
| 685 | unsigned long flags; |
| 686 | |
| 687 | spin_lock_irqsave(&audio->lock, flags); |
| 688 | audio->substream = NULL; |
| 689 | spin_unlock_irqrestore(&audio->lock, flags); |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | static int audio_pcm_hw_params(struct snd_pcm_substream *substream, |
| 695 | struct snd_pcm_hw_params *params) |
| 696 | { |
| 697 | unsigned int channels = params_channels(params); |
| 698 | unsigned int rate = params_rate(params); |
| 699 | |
| 700 | if (rate != SAMPLE_RATE) |
| 701 | return -EINVAL; |
| 702 | if (channels != 2) |
| 703 | return -EINVAL; |
| 704 | |
| 705 | return snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 706 | params_buffer_bytes(params)); |
| 707 | } |
| 708 | |
| 709 | static int audio_pcm_hw_free(struct snd_pcm_substream *substream) |
| 710 | { |
| 711 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
| 712 | } |
| 713 | |
| 714 | static int audio_pcm_prepare(struct snd_pcm_substream *substream) |
| 715 | { |
| 716 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 717 | struct audio_dev *audio = runtime->private_data; |
| 718 | |
| 719 | audio->period = snd_pcm_lib_period_bytes(substream); |
| 720 | audio->period_offset = 0; |
| 721 | audio->buffer_start = runtime->dma_area; |
| 722 | audio->buffer_end = audio->buffer_start |
| 723 | + snd_pcm_lib_buffer_bytes(substream); |
| 724 | audio->buffer_pos = audio->buffer_start; |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | static snd_pcm_uframes_t audio_pcm_pointer(struct snd_pcm_substream *substream) |
| 730 | { |
| 731 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 732 | struct audio_dev *audio = runtime->private_data; |
| 733 | ssize_t bytes = audio->buffer_pos - audio->buffer_start; |
| 734 | |
| 735 | /* return offset of next frame to fill in our buffer */ |
| 736 | return bytes_to_frames(runtime, bytes); |
| 737 | } |
| 738 | |
| 739 | static int audio_pcm_playback_trigger(struct snd_pcm_substream *substream, |
| 740 | int cmd) |
| 741 | { |
| 742 | struct audio_dev *audio = substream->runtime->private_data; |
| 743 | int ret = 0; |
| 744 | |
| 745 | switch (cmd) { |
| 746 | case SNDRV_PCM_TRIGGER_START: |
| 747 | case SNDRV_PCM_TRIGGER_RESUME: |
| 748 | audio_pcm_playback_start(audio); |
| 749 | break; |
| 750 | |
| 751 | case SNDRV_PCM_TRIGGER_STOP: |
| 752 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 753 | audio_pcm_playback_stop(audio); |
| 754 | break; |
| 755 | |
| 756 | default: |
| 757 | ret = -EINVAL; |
| 758 | } |
| 759 | |
| 760 | return ret; |
| 761 | } |
| 762 | |
Mike Lockwood | 785e1af | 2012-09-05 15:23:57 +0530 | [diff] [blame] | 763 | static struct audio_dev _audio_dev = { |
| 764 | .func = { |
| 765 | .name = "audio_source", |
| 766 | .bind = audio_bind, |
| 767 | .unbind = audio_unbind, |
| 768 | .set_alt = audio_set_alt, |
| 769 | .setup = audio_setup, |
| 770 | .disable = audio_disable, |
| 771 | .descriptors = fs_audio_desc, |
| 772 | .hs_descriptors = hs_audio_desc, |
| 773 | }, |
| 774 | .lock = __SPIN_LOCK_UNLOCKED(_audio_dev.lock), |
| 775 | .idle_reqs = LIST_HEAD_INIT(_audio_dev.idle_reqs), |
| 776 | }; |
| 777 | |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 778 | static struct snd_pcm_ops audio_playback_ops = { |
| 779 | .open = audio_pcm_open, |
| 780 | .close = audio_pcm_close, |
| 781 | .ioctl = snd_pcm_lib_ioctl, |
| 782 | .hw_params = audio_pcm_hw_params, |
| 783 | .hw_free = audio_pcm_hw_free, |
| 784 | .prepare = audio_pcm_prepare, |
| 785 | .trigger = audio_pcm_playback_trigger, |
| 786 | .pointer = audio_pcm_pointer, |
| 787 | }; |
| 788 | |
| 789 | int audio_source_bind_config(struct usb_configuration *c, |
| 790 | struct audio_source_config *config) |
| 791 | { |
| 792 | struct audio_dev *audio; |
| 793 | struct snd_card *card; |
| 794 | struct snd_pcm *pcm; |
| 795 | int err; |
| 796 | |
| 797 | config->card = -1; |
| 798 | config->device = -1; |
| 799 | |
Mike Lockwood | 785e1af | 2012-09-05 15:23:57 +0530 | [diff] [blame] | 800 | audio = &_audio_dev; |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 801 | |
| 802 | err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, |
| 803 | THIS_MODULE, 0, &card); |
| 804 | if (err) |
Mike Lockwood | 785e1af | 2012-09-05 15:23:57 +0530 | [diff] [blame] | 805 | return err; |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 806 | |
| 807 | snd_card_set_dev(card, &c->cdev->gadget->dev); |
| 808 | |
| 809 | err = snd_pcm_new(card, "USB audio source", 0, 1, 0, &pcm); |
| 810 | if (err) |
| 811 | goto pcm_fail; |
| 812 | pcm->private_data = audio; |
| 813 | pcm->info_flags = 0; |
| 814 | audio->pcm = pcm; |
| 815 | |
| 816 | strlcpy(pcm->name, "USB gadget audio", sizeof(pcm->name)); |
| 817 | |
| 818 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &audio_playback_ops); |
| 819 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, |
| 820 | NULL, 0, 64 * 1024); |
| 821 | |
| 822 | strlcpy(card->driver, "audio_source", sizeof(card->driver)); |
| 823 | strlcpy(card->shortname, card->driver, sizeof(card->shortname)); |
| 824 | strlcpy(card->longname, "USB accessory audio source", |
| 825 | sizeof(card->longname)); |
| 826 | |
| 827 | err = snd_card_register(card); |
| 828 | if (err) |
| 829 | goto register_fail; |
| 830 | |
| 831 | err = usb_add_function(c, &audio->func); |
| 832 | if (err) |
| 833 | goto add_fail; |
| 834 | |
| 835 | config->card = pcm->card->number; |
| 836 | config->device = pcm->device; |
| 837 | audio->card = card; |
| 838 | return 0; |
| 839 | |
| 840 | add_fail: |
| 841 | register_fail: |
| 842 | pcm_fail: |
| 843 | snd_card_free(audio->card); |
Mike Lockwood | 1187482 | 2012-08-27 16:43:53 +0530 | [diff] [blame] | 844 | return err; |
| 845 | } |