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