blob: 8124af33b7383b394974ae666ae5c230585653a9 [file] [log] [blame]
Mike Lockwood34a37f82012-05-11 09:01:08 -07001/*
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>
Konrad Leszczynski153770ec2016-02-24 10:47:12 +000020#include <linux/pm_qos.h>
Mike Lockwood34a37f82012-05-11 09:01:08 -070021#include <sound/core.h>
22#include <sound/initval.h>
23#include <sound/pcm.h>
24
Badhri Jagan Sridharan743a13c2014-11-23 13:51:28 -080025#include <linux/usb.h>
26#include <linux/usb_usual.h>
27#include <linux/usb/ch9.h>
28#include <linux/configfs.h>
29#include <linux/usb/composite.h>
30#include <linux/module.h>
31#include <linux/moduleparam.h>
Mike Lockwood34a37f82012-05-11 09:01:08 -070032#define SAMPLE_RATE 44100
33#define FRAMES_PER_MSEC (SAMPLE_RATE / 1000)
34
Anson Jacob2c4e8fe2014-07-01 18:17:20 +080035#define IN_EP_MAX_PACKET_SIZE 256
Mike Lockwood34a37f82012-05-11 09:01:08 -070036
37/* Number of requests to allocate */
38#define IN_EP_REQ_COUNT 4
39
40#define AUDIO_AC_INTERFACE 0
41#define AUDIO_AS_INTERFACE 1
42#define AUDIO_NUM_INTERFACES 2
Badhri Jagan Sridharan743a13c2014-11-23 13:51:28 -080043#define MAX_INST_NAME_LEN 40
Mike Lockwood34a37f82012-05-11 09:01:08 -070044
45/* B.3.1 Standard AC Interface Descriptor */
46static struct usb_interface_descriptor ac_interface_desc = {
47 .bLength = USB_DT_INTERFACE_SIZE,
48 .bDescriptorType = USB_DT_INTERFACE,
49 .bNumEndpoints = 0,
50 .bInterfaceClass = USB_CLASS_AUDIO,
51 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
52};
53
54DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
55
56#define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(AUDIO_NUM_INTERFACES)
57/* 1 input terminal, 1 output terminal and 1 feature unit */
58#define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH \
59 + UAC_DT_INPUT_TERMINAL_SIZE + UAC_DT_OUTPUT_TERMINAL_SIZE \
60 + UAC_DT_FEATURE_UNIT_SIZE(0))
61/* B.3.2 Class-Specific AC Interface Descriptor */
62static struct uac1_ac_header_descriptor_2 ac_header_desc = {
63 .bLength = UAC_DT_AC_HEADER_LENGTH,
64 .bDescriptorType = USB_DT_CS_INTERFACE,
65 .bDescriptorSubtype = UAC_HEADER,
66 .bcdADC = __constant_cpu_to_le16(0x0100),
67 .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
68 .bInCollection = AUDIO_NUM_INTERFACES,
69 .baInterfaceNr = {
70 [0] = AUDIO_AC_INTERFACE,
71 [1] = AUDIO_AS_INTERFACE,
72 }
73};
74
75#define INPUT_TERMINAL_ID 1
76static struct uac_input_terminal_descriptor input_terminal_desc = {
77 .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
78 .bDescriptorType = USB_DT_CS_INTERFACE,
79 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
80 .bTerminalID = INPUT_TERMINAL_ID,
81 .wTerminalType = UAC_INPUT_TERMINAL_MICROPHONE,
82 .bAssocTerminal = 0,
83 .wChannelConfig = 0x3,
84};
85
86DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
87
88#define FEATURE_UNIT_ID 2
89static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
90 .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
91 .bDescriptorType = USB_DT_CS_INTERFACE,
92 .bDescriptorSubtype = UAC_FEATURE_UNIT,
93 .bUnitID = FEATURE_UNIT_ID,
94 .bSourceID = INPUT_TERMINAL_ID,
95 .bControlSize = 2,
96};
97
98#define OUTPUT_TERMINAL_ID 3
99static struct uac1_output_terminal_descriptor output_terminal_desc = {
100 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
101 .bDescriptorType = USB_DT_CS_INTERFACE,
102 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
103 .bTerminalID = OUTPUT_TERMINAL_ID,
104 .wTerminalType = UAC_TERMINAL_STREAMING,
105 .bAssocTerminal = FEATURE_UNIT_ID,
106 .bSourceID = FEATURE_UNIT_ID,
107};
108
109/* B.4.1 Standard AS Interface Descriptor */
110static struct usb_interface_descriptor as_interface_alt_0_desc = {
111 .bLength = USB_DT_INTERFACE_SIZE,
112 .bDescriptorType = USB_DT_INTERFACE,
113 .bAlternateSetting = 0,
114 .bNumEndpoints = 0,
115 .bInterfaceClass = USB_CLASS_AUDIO,
116 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
117};
118
119static struct usb_interface_descriptor as_interface_alt_1_desc = {
120 .bLength = USB_DT_INTERFACE_SIZE,
121 .bDescriptorType = USB_DT_INTERFACE,
122 .bAlternateSetting = 1,
123 .bNumEndpoints = 1,
124 .bInterfaceClass = USB_CLASS_AUDIO,
125 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
126};
127
128/* B.4.2 Class-Specific AS Interface Descriptor */
129static struct uac1_as_header_descriptor as_header_desc = {
130 .bLength = UAC_DT_AS_HEADER_SIZE,
131 .bDescriptorType = USB_DT_CS_INTERFACE,
132 .bDescriptorSubtype = UAC_AS_GENERAL,
133 .bTerminalLink = INPUT_TERMINAL_ID,
134 .bDelay = 1,
135 .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
136};
137
138DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
139
140static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
141 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubtype = UAC_FORMAT_TYPE,
144 .bFormatType = UAC_FORMAT_TYPE_I,
145 .bSubframeSize = 2,
146 .bBitResolution = 16,
147 .bSamFreqType = 1,
148};
149
150/* Standard ISO IN Endpoint Descriptor for highspeed */
151static struct usb_endpoint_descriptor hs_as_in_ep_desc = {
152 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
153 .bDescriptorType = USB_DT_ENDPOINT,
154 .bEndpointAddress = USB_DIR_IN,
155 .bmAttributes = USB_ENDPOINT_SYNC_SYNC
156 | USB_ENDPOINT_XFER_ISOC,
157 .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE),
158 .bInterval = 4, /* poll 1 per millisecond */
159};
160
161/* Standard ISO IN Endpoint Descriptor for highspeed */
162static struct usb_endpoint_descriptor fs_as_in_ep_desc = {
163 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
164 .bDescriptorType = USB_DT_ENDPOINT,
165 .bEndpointAddress = USB_DIR_IN,
166 .bmAttributes = USB_ENDPOINT_SYNC_SYNC
167 | USB_ENDPOINT_XFER_ISOC,
168 .wMaxPacketSize = __constant_cpu_to_le16(IN_EP_MAX_PACKET_SIZE),
169 .bInterval = 1, /* poll 1 per millisecond */
170};
171
172/* Class-specific AS ISO OUT Endpoint Descriptor */
173static struct uac_iso_endpoint_descriptor as_iso_in_desc = {
174 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
175 .bDescriptorType = USB_DT_CS_ENDPOINT,
176 .bDescriptorSubtype = UAC_EP_GENERAL,
177 .bmAttributes = 1,
178 .bLockDelayUnits = 1,
179 .wLockDelay = __constant_cpu_to_le16(1),
180};
181
182static struct usb_descriptor_header *hs_audio_desc[] = {
183 (struct usb_descriptor_header *)&ac_interface_desc,
184 (struct usb_descriptor_header *)&ac_header_desc,
185
186 (struct usb_descriptor_header *)&input_terminal_desc,
187 (struct usb_descriptor_header *)&output_terminal_desc,
188 (struct usb_descriptor_header *)&feature_unit_desc,
189
190 (struct usb_descriptor_header *)&as_interface_alt_0_desc,
191 (struct usb_descriptor_header *)&as_interface_alt_1_desc,
192 (struct usb_descriptor_header *)&as_header_desc,
193
194 (struct usb_descriptor_header *)&as_type_i_desc,
195
196 (struct usb_descriptor_header *)&hs_as_in_ep_desc,
197 (struct usb_descriptor_header *)&as_iso_in_desc,
198 NULL,
199};
200
201static struct usb_descriptor_header *fs_audio_desc[] = {
202 (struct usb_descriptor_header *)&ac_interface_desc,
203 (struct usb_descriptor_header *)&ac_header_desc,
204
205 (struct usb_descriptor_header *)&input_terminal_desc,
206 (struct usb_descriptor_header *)&output_terminal_desc,
207 (struct usb_descriptor_header *)&feature_unit_desc,
208
209 (struct usb_descriptor_header *)&as_interface_alt_0_desc,
210 (struct usb_descriptor_header *)&as_interface_alt_1_desc,
211 (struct usb_descriptor_header *)&as_header_desc,
212
213 (struct usb_descriptor_header *)&as_type_i_desc,
214
215 (struct usb_descriptor_header *)&fs_as_in_ep_desc,
216 (struct usb_descriptor_header *)&as_iso_in_desc,
217 NULL,
218};
219
220static struct snd_pcm_hardware audio_hw_info = {
221 .info = SNDRV_PCM_INFO_MMAP |
222 SNDRV_PCM_INFO_MMAP_VALID |
223 SNDRV_PCM_INFO_BATCH |
224 SNDRV_PCM_INFO_INTERLEAVED |
225 SNDRV_PCM_INFO_BLOCK_TRANSFER,
226
227 .formats = SNDRV_PCM_FMTBIT_S16_LE,
228 .channels_min = 2,
229 .channels_max = 2,
230 .rate_min = SAMPLE_RATE,
231 .rate_max = SAMPLE_RATE,
232
233 .buffer_bytes_max = 1024 * 1024,
234 .period_bytes_min = 64,
235 .period_bytes_max = 512 * 1024,
236 .periods_min = 2,
237 .periods_max = 1024,
238};
239
240/*-------------------------------------------------------------------------*/
241
242struct audio_source_config {
243 int card;
244 int device;
245};
246
247struct audio_dev {
248 struct usb_function func;
249 struct snd_card *card;
250 struct snd_pcm *pcm;
251 struct snd_pcm_substream *substream;
252
253 struct list_head idle_reqs;
254 struct usb_ep *in_ep;
255
256 spinlock_t lock;
257
258 /* beginning, end and current position in our buffer */
259 void *buffer_start;
260 void *buffer_end;
261 void *buffer_pos;
262
263 /* byte size of a "period" */
264 unsigned int period;
265 /* bytes sent since last call to snd_pcm_period_elapsed */
266 unsigned int period_offset;
267 /* time we started playing */
268 ktime_t start_time;
269 /* number of frames sent since start_time */
270 s64 frames_sent;
Badhri Jagan Sridharan743a13c2014-11-23 13:51:28 -0800271 struct audio_source_config *config;
Konrad Leszczynski153770ec2016-02-24 10:47:12 +0000272 /* for creating and issuing QoS requests */
273 struct pm_qos_request pm_qos;
Mike Lockwood34a37f82012-05-11 09:01:08 -0700274};
275
276static inline struct audio_dev *func_to_audio(struct usb_function *f)
277{
278 return container_of(f, struct audio_dev, func);
279}
280
281/*-------------------------------------------------------------------------*/
282
Badhri Jagan Sridharan743a13c2014-11-23 13:51:28 -0800283struct audio_source_instance {
284 struct usb_function_instance func_inst;
285 const char *name;
286 struct audio_source_config *config;
287 struct device *audio_device;
288};
289
290static void audio_source_attr_release(struct config_item *item);
291
292static struct configfs_item_operations audio_source_item_ops = {
293 .release = audio_source_attr_release,
294};
295
296static struct config_item_type audio_source_func_type = {
297 .ct_item_ops = &audio_source_item_ops,
298 .ct_owner = THIS_MODULE,
299};
300
301static ssize_t audio_source_pcm_show(struct device *dev,
302 struct device_attribute *attr, char *buf);
303
304static DEVICE_ATTR(pcm, S_IRUGO, audio_source_pcm_show, NULL);
305
306static struct device_attribute *audio_source_function_attributes[] = {
307 &dev_attr_pcm,
308 NULL
309};
310
311/*--------------------------------------------------------------------------*/
312
Mike Lockwood34a37f82012-05-11 09:01:08 -0700313static struct usb_request *audio_request_new(struct usb_ep *ep, int buffer_size)
314{
315 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
Anson Jacob051584e2016-11-11 01:10:04 -0500316
Mike Lockwood34a37f82012-05-11 09:01:08 -0700317 if (!req)
318 return NULL;
319
320 req->buf = kmalloc(buffer_size, GFP_KERNEL);
321 if (!req->buf) {
322 usb_ep_free_request(ep, req);
323 return NULL;
324 }
325 req->length = buffer_size;
326 return req;
327}
328
329static void audio_request_free(struct usb_request *req, struct usb_ep *ep)
330{
331 if (req) {
332 kfree(req->buf);
333 usb_ep_free_request(ep, req);
334 }
335}
336
337static void audio_req_put(struct audio_dev *audio, struct usb_request *req)
338{
339 unsigned long flags;
340
341 spin_lock_irqsave(&audio->lock, flags);
342 list_add_tail(&req->list, &audio->idle_reqs);
343 spin_unlock_irqrestore(&audio->lock, flags);
344}
345
346static struct usb_request *audio_req_get(struct audio_dev *audio)
347{
348 unsigned long flags;
349 struct usb_request *req;
350
351 spin_lock_irqsave(&audio->lock, flags);
352 if (list_empty(&audio->idle_reqs)) {
353 req = 0;
354 } else {
355 req = list_first_entry(&audio->idle_reqs, struct usb_request,
356 list);
357 list_del(&req->list);
358 }
359 spin_unlock_irqrestore(&audio->lock, flags);
360 return req;
361}
362
363/* send the appropriate number of packets to match our bitrate */
364static void audio_send(struct audio_dev *audio)
365{
366 struct snd_pcm_runtime *runtime;
367 struct usb_request *req;
368 int length, length1, length2, ret;
369 s64 msecs;
370 s64 frames;
371 ktime_t now;
372
373 /* audio->substream will be null if we have been closed */
374 if (!audio->substream)
375 return;
376 /* audio->buffer_pos will be null if we have been stopped */
377 if (!audio->buffer_pos)
378 return;
379
380 runtime = audio->substream->runtime;
381
382 /* compute number of frames to send */
383 now = ktime_get();
Amit Pundir4de9e332016-09-15 16:05:40 +0530384 msecs = div_s64((ktime_to_ns(now) - ktime_to_ns(audio->start_time)),
385 1000000);
386 frames = div_s64((msecs * SAMPLE_RATE), 1000);
Mike Lockwood34a37f82012-05-11 09:01:08 -0700387
388 /* Readjust our frames_sent if we fall too far behind.
389 * If we get too far behind it is better to drop some frames than
390 * to keep sending data too fast in an attempt to catch up.
391 */
392 if (frames - audio->frames_sent > 10 * FRAMES_PER_MSEC)
393 audio->frames_sent = frames - FRAMES_PER_MSEC;
394
395 frames -= audio->frames_sent;
396
397 /* We need to send something to keep the pipeline going */
398 if (frames <= 0)
399 frames = FRAMES_PER_MSEC;
400
401 while (frames > 0) {
402 req = audio_req_get(audio);
403 if (!req)
404 break;
405
406 length = frames_to_bytes(runtime, frames);
407 if (length > IN_EP_MAX_PACKET_SIZE)
408 length = IN_EP_MAX_PACKET_SIZE;
409
410 if (audio->buffer_pos + length > audio->buffer_end)
411 length1 = audio->buffer_end - audio->buffer_pos;
412 else
413 length1 = length;
414 memcpy(req->buf, audio->buffer_pos, length1);
415 if (length1 < length) {
416 /* Wrap around and copy remaining length
417 * at beginning of buffer.
418 */
419 length2 = length - length1;
420 memcpy(req->buf + length1, audio->buffer_start,
421 length2);
422 audio->buffer_pos = audio->buffer_start + length2;
423 } else {
424 audio->buffer_pos += length1;
425 if (audio->buffer_pos >= audio->buffer_end)
426 audio->buffer_pos = audio->buffer_start;
427 }
428
429 req->length = length;
430 ret = usb_ep_queue(audio->in_ep, req, GFP_ATOMIC);
431 if (ret < 0) {
432 pr_err("usb_ep_queue failed ret: %d\n", ret);
433 audio_req_put(audio, req);
434 break;
435 }
436
437 frames -= bytes_to_frames(runtime, length);
438 audio->frames_sent += bytes_to_frames(runtime, length);
439 }
440}
441
442static void audio_control_complete(struct usb_ep *ep, struct usb_request *req)
443{
444 /* nothing to do here */
445}
446
447static void audio_data_complete(struct usb_ep *ep, struct usb_request *req)
448{
449 struct audio_dev *audio = req->context;
450
451 pr_debug("audio_data_complete req->status %d req->actual %d\n",
452 req->status, req->actual);
453
454 audio_req_put(audio, req);
455
456 if (!audio->buffer_start || req->status)
457 return;
458
459 audio->period_offset += req->actual;
460 if (audio->period_offset >= audio->period) {
461 snd_pcm_period_elapsed(audio->substream);
462 audio->period_offset = 0;
463 }
464 audio_send(audio);
465}
466
467static int audio_set_endpoint_req(struct usb_function *f,
468 const struct usb_ctrlrequest *ctrl)
469{
470 int value = -EOPNOTSUPP;
471 u16 ep = le16_to_cpu(ctrl->wIndex);
472 u16 len = le16_to_cpu(ctrl->wLength);
473 u16 w_value = le16_to_cpu(ctrl->wValue);
474
475 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
476 ctrl->bRequest, w_value, len, ep);
477
478 switch (ctrl->bRequest) {
479 case UAC_SET_CUR:
480 case UAC_SET_MIN:
481 case UAC_SET_MAX:
482 case UAC_SET_RES:
483 value = len;
484 break;
485 default:
486 break;
487 }
488
489 return value;
490}
491
492static int audio_get_endpoint_req(struct usb_function *f,
493 const struct usb_ctrlrequest *ctrl)
494{
495 struct usb_composite_dev *cdev = f->config->cdev;
496 int value = -EOPNOTSUPP;
497 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
498 u16 len = le16_to_cpu(ctrl->wLength);
499 u16 w_value = le16_to_cpu(ctrl->wValue);
500 u8 *buf = cdev->req->buf;
501
502 pr_debug("bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
503 ctrl->bRequest, w_value, len, ep);
504
505 if (w_value == UAC_EP_CS_ATTR_SAMPLE_RATE << 8) {
506 switch (ctrl->bRequest) {
507 case UAC_GET_CUR:
508 case UAC_GET_MIN:
509 case UAC_GET_MAX:
510 case UAC_GET_RES:
511 /* return our sample rate */
512 buf[0] = (u8)SAMPLE_RATE;
513 buf[1] = (u8)(SAMPLE_RATE >> 8);
514 buf[2] = (u8)(SAMPLE_RATE >> 16);
515 value = 3;
516 break;
517 default:
518 break;
519 }
520 }
521
522 return value;
523}
524
525static int
526audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
527{
528 struct usb_composite_dev *cdev = f->config->cdev;
529 struct usb_request *req = cdev->req;
530 int value = -EOPNOTSUPP;
531 u16 w_index = le16_to_cpu(ctrl->wIndex);
532 u16 w_value = le16_to_cpu(ctrl->wValue);
533 u16 w_length = le16_to_cpu(ctrl->wLength);
534
535 /* composite driver infrastructure handles everything; interface
536 * activation uses set_alt().
537 */
538 switch (ctrl->bRequestType) {
539 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
540 value = audio_set_endpoint_req(f, ctrl);
541 break;
542
543 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
544 value = audio_get_endpoint_req(f, ctrl);
545 break;
546 }
547
548 /* respond with data transfer or status phase? */
549 if (value >= 0) {
550 pr_debug("audio req%02x.%02x v%04x i%04x l%d\n",
551 ctrl->bRequestType, ctrl->bRequest,
552 w_value, w_index, w_length);
553 req->zero = 0;
554 req->length = value;
555 req->complete = audio_control_complete;
556 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
557 if (value < 0)
558 pr_err("audio response on err %d\n", value);
559 }
560
561 /* device either stalls (value < 0) or reports success */
562 return value;
563}
564
565static int audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
566{
567 struct audio_dev *audio = func_to_audio(f);
568 struct usb_composite_dev *cdev = f->config->cdev;
569 int ret;
570
571 pr_debug("audio_set_alt intf %d, alt %d\n", intf, alt);
572
573 ret = config_ep_by_speed(cdev->gadget, f, audio->in_ep);
574 if (ret)
575 return ret;
576
577 usb_ep_enable(audio->in_ep);
578 return 0;
579}
580
581static void audio_disable(struct usb_function *f)
582{
583 struct audio_dev *audio = func_to_audio(f);
584
585 pr_debug("audio_disable\n");
586 usb_ep_disable(audio->in_ep);
587}
588
Mark Kuo1aaf9972016-01-11 17:49:16 +0800589static void audio_free_func(struct usb_function *f)
590{
591 /* no-op */
592}
593
Mike Lockwood34a37f82012-05-11 09:01:08 -0700594/*-------------------------------------------------------------------------*/
595
596static void audio_build_desc(struct audio_dev *audio)
597{
598 u8 *sam_freq;
599 int rate;
600
601 /* Set channel numbers */
602 input_terminal_desc.bNrChannels = 2;
603 as_type_i_desc.bNrChannels = 2;
604
605 /* Set sample rates */
606 rate = SAMPLE_RATE;
607 sam_freq = as_type_i_desc.tSamFreq[0];
608 memcpy(sam_freq, &rate, 3);
609}
610
Badhri Jagan Sridharan743a13c2014-11-23 13:51:28 -0800611
612static int snd_card_setup(struct usb_configuration *c,
613 struct audio_source_config *config);
614static struct audio_source_instance *to_fi_audio_source(
615 const struct usb_function_instance *fi);
616
617
Mike Lockwood34a37f82012-05-11 09:01:08 -0700618/* audio function driver setup/binding */
619static int
620audio_bind(struct usb_configuration *c, struct usb_function *f)
621{
622 struct usb_composite_dev *cdev = c->cdev;
623 struct audio_dev *audio = func_to_audio(f);
624 int status;
625 struct usb_ep *ep;
626 struct usb_request *req;
627 int i;
Badhri Jagan Sridharan743a13c2014-11-23 13:51:28 -0800628 int err;
629
630 if (IS_ENABLED(CONFIG_USB_CONFIGFS)) {
631 struct audio_source_instance *fi_audio =
632 to_fi_audio_source(f->fi);
633 struct audio_source_config *config =
634 fi_audio->config;
635
636 err = snd_card_setup(c, config);
637 if (err)
638 return err;
639 }
Mike Lockwood34a37f82012-05-11 09:01:08 -0700640
641 audio_build_desc(audio);
642
643 /* allocate instance-specific interface IDs, and patch descriptors */
644 status = usb_interface_id(c, f);
645 if (status < 0)
646 goto fail;
647 ac_interface_desc.bInterfaceNumber = status;
648
Anson Jacobbe102d92014-06-23 19:14:01 +0800649 /* AUDIO_AC_INTERFACE */
650 ac_header_desc.baInterfaceNr[0] = status;
651
Mike Lockwood34a37f82012-05-11 09:01:08 -0700652 status = usb_interface_id(c, f);
653 if (status < 0)
654 goto fail;
655 as_interface_alt_0_desc.bInterfaceNumber = status;
656 as_interface_alt_1_desc.bInterfaceNumber = status;
657
Anson Jacobbe102d92014-06-23 19:14:01 +0800658 /* AUDIO_AS_INTERFACE */
659 ac_header_desc.baInterfaceNr[1] = status;
660
Mike Lockwood34a37f82012-05-11 09:01:08 -0700661 status = -ENODEV;
662
663 /* allocate our endpoint */
664 ep = usb_ep_autoconfig(cdev->gadget, &fs_as_in_ep_desc);
665 if (!ep)
666 goto fail;
667 audio->in_ep = ep;
668 ep->driver_data = audio; /* claim */
669
670 if (gadget_is_dualspeed(c->cdev->gadget))
671 hs_as_in_ep_desc.bEndpointAddress =
672 fs_as_in_ep_desc.bEndpointAddress;
673
Arve Hjønnevåge2754392012-11-27 19:29:04 -0800674 f->fs_descriptors = fs_audio_desc;
Mike Lockwood34a37f82012-05-11 09:01:08 -0700675 f->hs_descriptors = hs_audio_desc;
676
677 for (i = 0, status = 0; i < IN_EP_REQ_COUNT && status == 0; i++) {
678 req = audio_request_new(ep, IN_EP_MAX_PACKET_SIZE);
679 if (req) {
680 req->context = audio;
681 req->complete = audio_data_complete;
682 audio_req_put(audio, req);
683 } else
684 status = -ENOMEM;
685 }
686
687fail:
688 return status;
689}
690
691static void
692audio_unbind(struct usb_configuration *c, struct usb_function *f)
693{
694 struct audio_dev *audio = func_to_audio(f);
695 struct usb_request *req;
696
697 while ((req = audio_req_get(audio)))
698 audio_request_free(req, audio->in_ep);
699
700 snd_card_free_when_closed(audio->card);
701 audio->card = NULL;
702 audio->pcm = NULL;
703 audio->substream = NULL;
704 audio->in_ep = NULL;
Badhri Jagan Sridharan743a13c2014-11-23 13:51:28 -0800705
706 if (IS_ENABLED(CONFIG_USB_CONFIGFS)) {
707 struct audio_source_instance *fi_audio =
708 to_fi_audio_source(f->fi);
709 struct audio_source_config *config =
710 fi_audio->config;
711
712 config->card = -1;
713 config->device = -1;
714 }
Mike Lockwood34a37f82012-05-11 09:01:08 -0700715}
716
717static void audio_pcm_playback_start(struct audio_dev *audio)
718{
719 audio->start_time = ktime_get();
720 audio->frames_sent = 0;
721 audio_send(audio);
722}
723
724static void audio_pcm_playback_stop(struct audio_dev *audio)
725{
726 unsigned long flags;
727
728 spin_lock_irqsave(&audio->lock, flags);
729 audio->buffer_start = 0;
730 audio->buffer_end = 0;
731 audio->buffer_pos = 0;
732 spin_unlock_irqrestore(&audio->lock, flags);
733}
734
735static int audio_pcm_open(struct snd_pcm_substream *substream)
736{
737 struct snd_pcm_runtime *runtime = substream->runtime;
738 struct audio_dev *audio = substream->private_data;
739
740 runtime->private_data = audio;
741 runtime->hw = audio_hw_info;
742 snd_pcm_limit_hw_rates(runtime);
743 runtime->hw.channels_max = 2;
744
745 audio->substream = substream;
Konrad Leszczynski153770ec2016-02-24 10:47:12 +0000746
747 /* Add the QoS request and set the latency to 0 */
748 pm_qos_add_request(&audio->pm_qos, PM_QOS_CPU_DMA_LATENCY, 0);
749
Mike Lockwood34a37f82012-05-11 09:01:08 -0700750 return 0;
751}
752
753static int audio_pcm_close(struct snd_pcm_substream *substream)
754{
755 struct audio_dev *audio = substream->private_data;
756 unsigned long flags;
757
758 spin_lock_irqsave(&audio->lock, flags);
Konrad Leszczynski153770ec2016-02-24 10:47:12 +0000759
760 /* Remove the QoS request */
761 pm_qos_remove_request(&audio->pm_qos);
762
Mike Lockwood34a37f82012-05-11 09:01:08 -0700763 audio->substream = NULL;
764 spin_unlock_irqrestore(&audio->lock, flags);
765
766 return 0;
767}
768
769static int audio_pcm_hw_params(struct snd_pcm_substream *substream,
770 struct snd_pcm_hw_params *params)
771{
772 unsigned int channels = params_channels(params);
773 unsigned int rate = params_rate(params);
774
775 if (rate != SAMPLE_RATE)
776 return -EINVAL;
777 if (channels != 2)
778 return -EINVAL;
779
780 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
781 params_buffer_bytes(params));
782}
783
784static int audio_pcm_hw_free(struct snd_pcm_substream *substream)
785{
786 return snd_pcm_lib_free_vmalloc_buffer(substream);
787}
788
789static int audio_pcm_prepare(struct snd_pcm_substream *substream)
790{
791 struct snd_pcm_runtime *runtime = substream->runtime;
792 struct audio_dev *audio = runtime->private_data;
793
794 audio->period = snd_pcm_lib_period_bytes(substream);
795 audio->period_offset = 0;
796 audio->buffer_start = runtime->dma_area;
797 audio->buffer_end = audio->buffer_start
798 + snd_pcm_lib_buffer_bytes(substream);
799 audio->buffer_pos = audio->buffer_start;
800
801 return 0;
802}
803
804static snd_pcm_uframes_t audio_pcm_pointer(struct snd_pcm_substream *substream)
805{
806 struct snd_pcm_runtime *runtime = substream->runtime;
807 struct audio_dev *audio = runtime->private_data;
808 ssize_t bytes = audio->buffer_pos - audio->buffer_start;
809
810 /* return offset of next frame to fill in our buffer */
811 return bytes_to_frames(runtime, bytes);
812}
813
814static int audio_pcm_playback_trigger(struct snd_pcm_substream *substream,
815 int cmd)
816{
817 struct audio_dev *audio = substream->runtime->private_data;
818 int ret = 0;
819
820 switch (cmd) {
821 case SNDRV_PCM_TRIGGER_START:
822 case SNDRV_PCM_TRIGGER_RESUME:
823 audio_pcm_playback_start(audio);
824 break;
825
826 case SNDRV_PCM_TRIGGER_STOP:
827 case SNDRV_PCM_TRIGGER_SUSPEND:
828 audio_pcm_playback_stop(audio);
829 break;
830
831 default:
832 ret = -EINVAL;
833 }
834
835 return ret;
836}
837
838static struct audio_dev _audio_dev = {
839 .func = {
840 .name = "audio_source",
841 .bind = audio_bind,
842 .unbind = audio_unbind,
843 .set_alt = audio_set_alt,
844 .setup = audio_setup,
845 .disable = audio_disable,
Mark Kuo1aaf9972016-01-11 17:49:16 +0800846 .free_func = audio_free_func,
Mike Lockwood34a37f82012-05-11 09:01:08 -0700847 },
848 .lock = __SPIN_LOCK_UNLOCKED(_audio_dev.lock),
849 .idle_reqs = LIST_HEAD_INIT(_audio_dev.idle_reqs),
850};
851
852static struct snd_pcm_ops audio_playback_ops = {
853 .open = audio_pcm_open,
854 .close = audio_pcm_close,
855 .ioctl = snd_pcm_lib_ioctl,
856 .hw_params = audio_pcm_hw_params,
857 .hw_free = audio_pcm_hw_free,
858 .prepare = audio_pcm_prepare,
859 .trigger = audio_pcm_playback_trigger,
860 .pointer = audio_pcm_pointer,
861};
862
863int audio_source_bind_config(struct usb_configuration *c,
864 struct audio_source_config *config)
865{
866 struct audio_dev *audio;
Mike Lockwood34a37f82012-05-11 09:01:08 -0700867 int err;
868
869 config->card = -1;
870 config->device = -1;
871
872 audio = &_audio_dev;
873
Badhri Jagan Sridharan743a13c2014-11-23 13:51:28 -0800874 err = snd_card_setup(c, config);
875 if (err)
876 return err;
877
878 err = usb_add_function(c, &audio->func);
879 if (err)
880 goto add_fail;
881
882 return 0;
883
884add_fail:
885 snd_card_free(audio->card);
886 return err;
887}
888
889static int snd_card_setup(struct usb_configuration *c,
890 struct audio_source_config *config)
891{
892 struct audio_dev *audio;
893 struct snd_card *card;
894 struct snd_pcm *pcm;
895 int err;
896
897 audio = &_audio_dev;
898
Badhri Jagan Sridharan6d9285e2015-03-25 14:37:23 -0700899 err = snd_card_new(&c->cdev->gadget->dev,
900 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Mike Lockwood34a37f82012-05-11 09:01:08 -0700901 THIS_MODULE, 0, &card);
902 if (err)
903 return err;
904
Mike Lockwood34a37f82012-05-11 09:01:08 -0700905 err = snd_pcm_new(card, "USB audio source", 0, 1, 0, &pcm);
906 if (err)
907 goto pcm_fail;
Badhri Jagan Sridharan6d9285e2015-03-25 14:37:23 -0700908
Mike Lockwood34a37f82012-05-11 09:01:08 -0700909 pcm->private_data = audio;
910 pcm->info_flags = 0;
911 audio->pcm = pcm;
912
913 strlcpy(pcm->name, "USB gadget audio", sizeof(pcm->name));
914
915 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &audio_playback_ops);
916 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
917 NULL, 0, 64 * 1024);
918
919 strlcpy(card->driver, "audio_source", sizeof(card->driver));
920 strlcpy(card->shortname, card->driver, sizeof(card->shortname));
921 strlcpy(card->longname, "USB accessory audio source",
922 sizeof(card->longname));
923
924 err = snd_card_register(card);
925 if (err)
926 goto register_fail;
927
Mike Lockwood34a37f82012-05-11 09:01:08 -0700928 config->card = pcm->card->number;
929 config->device = pcm->device;
930 audio->card = card;
931 return 0;
932
Mike Lockwood34a37f82012-05-11 09:01:08 -0700933register_fail:
934pcm_fail:
935 snd_card_free(audio->card);
936 return err;
937}
Badhri Jagan Sridharan743a13c2014-11-23 13:51:28 -0800938
939static struct audio_source_instance *to_audio_source_instance(
940 struct config_item *item)
941{
942 return container_of(to_config_group(item), struct audio_source_instance,
943 func_inst.group);
944}
945
946static struct audio_source_instance *to_fi_audio_source(
947 const struct usb_function_instance *fi)
948{
949 return container_of(fi, struct audio_source_instance, func_inst);
950}
951
952static void audio_source_attr_release(struct config_item *item)
953{
954 struct audio_source_instance *fi_audio = to_audio_source_instance(item);
955
956 usb_put_function_instance(&fi_audio->func_inst);
957}
958
959static int audio_source_set_inst_name(struct usb_function_instance *fi,
960 const char *name)
961{
962 struct audio_source_instance *fi_audio;
963 char *ptr;
964 int name_len;
965
966 name_len = strlen(name) + 1;
967 if (name_len > MAX_INST_NAME_LEN)
968 return -ENAMETOOLONG;
969
970 ptr = kstrndup(name, name_len, GFP_KERNEL);
971 if (!ptr)
972 return -ENOMEM;
973
974 fi_audio = to_fi_audio_source(fi);
975 fi_audio->name = ptr;
976
977 return 0;
978}
979
980static void audio_source_free_inst(struct usb_function_instance *fi)
981{
982 struct audio_source_instance *fi_audio;
983
984 fi_audio = to_fi_audio_source(fi);
985 device_destroy(fi_audio->audio_device->class,
986 fi_audio->audio_device->devt);
987 kfree(fi_audio->name);
988 kfree(fi_audio->config);
989}
990
991static ssize_t audio_source_pcm_show(struct device *dev,
992 struct device_attribute *attr, char *buf)
993{
994 struct audio_source_instance *fi_audio = dev_get_drvdata(dev);
995 struct audio_source_config *config = fi_audio->config;
996
997 /* print PCM card and device numbers */
998 return sprintf(buf, "%d %d\n", config->card, config->device);
999}
1000
1001struct device *create_function_device(char *name);
1002
1003static struct usb_function_instance *audio_source_alloc_inst(void)
1004{
1005 struct audio_source_instance *fi_audio;
1006 struct device_attribute **attrs;
1007 struct device_attribute *attr;
1008 struct device *dev;
1009 void *err_ptr;
1010 int err = 0;
1011
1012 fi_audio = kzalloc(sizeof(*fi_audio), GFP_KERNEL);
1013 if (!fi_audio)
1014 return ERR_PTR(-ENOMEM);
1015
1016 fi_audio->func_inst.set_inst_name = audio_source_set_inst_name;
1017 fi_audio->func_inst.free_func_inst = audio_source_free_inst;
1018
1019 fi_audio->config = kzalloc(sizeof(struct audio_source_config),
1020 GFP_KERNEL);
1021 if (!fi_audio->config) {
1022 err_ptr = ERR_PTR(-ENOMEM);
1023 goto fail_audio;
1024 }
1025
1026 config_group_init_type_name(&fi_audio->func_inst.group, "",
1027 &audio_source_func_type);
1028 dev = create_function_device("f_audio_source");
1029
1030 if (IS_ERR(dev)) {
1031 err_ptr = dev;
1032 goto fail_audio_config;
1033 }
1034
1035 fi_audio->config->card = -1;
1036 fi_audio->config->device = -1;
1037 fi_audio->audio_device = dev;
1038
1039 attrs = audio_source_function_attributes;
1040 if (attrs) {
1041 while ((attr = *attrs++) && !err)
1042 err = device_create_file(dev, attr);
1043 if (err) {
1044 err_ptr = ERR_PTR(-EINVAL);
1045 goto fail_device;
1046 }
1047 }
1048
1049 dev_set_drvdata(dev, fi_audio);
1050 _audio_dev.config = fi_audio->config;
1051
1052 return &fi_audio->func_inst;
1053
1054fail_device:
1055 device_destroy(dev->class, dev->devt);
1056fail_audio_config:
1057 kfree(fi_audio->config);
1058fail_audio:
1059 kfree(fi_audio);
1060 return err_ptr;
1061
1062}
1063
1064static struct usb_function *audio_source_alloc(struct usb_function_instance *fi)
1065{
1066 return &_audio_dev.func;
1067}
1068
1069DECLARE_USB_FUNCTION_INIT(audio_source, audio_source_alloc_inst,
1070 audio_source_alloc);
1071MODULE_LICENSE("GPL");