]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/gadget/function/f_uac1_legacy.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / gadget / function / f_uac1_legacy.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
c6994e6f
BW
2/*
3 * f_audio.c -- USB Audio class function driver
4 *
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
5a0e3ad6 13#include <linux/slab.h>
c6994e6f 14#include <linux/kernel.h>
f3a3406b 15#include <linux/module.h>
c6994e6f 16#include <linux/device.h>
60063497 17#include <linux/atomic.h>
c6994e6f 18
d355339e 19#include "u_uac1_legacy.h"
c6994e6f 20
b95cd7ec
LP
21static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value);
22static int generic_get_cmd(struct usb_audio_control *con, u8 cmd);
23
c6994e6f
BW
24/*
25 * DESCRIPTORS ... most are static, but strings and full
26 * configuration descriptors are built on demand.
27 */
28
29/*
30 * We have two interfaces- AudioControl and AudioStreaming
31 * TODO: only supcard playback currently
32 */
33#define F_AUDIO_AC_INTERFACE 0
34#define F_AUDIO_AS_INTERFACE 1
8d252db1 35#define F_AUDIO_NUM_INTERFACES 1
c6994e6f
BW
36
37/* B.3.1 Standard AC Interface Descriptor */
f3a3406b 38static struct usb_interface_descriptor ac_interface_desc = {
c6994e6f
BW
39 .bLength = USB_DT_INTERFACE_SIZE,
40 .bDescriptorType = USB_DT_INTERFACE,
41 .bNumEndpoints = 0,
42 .bInterfaceClass = USB_CLASS_AUDIO,
43 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
44};
45
8d252db1
XW
46/*
47 * The number of AudioStreaming and MIDIStreaming interfaces
48 * in the Audio Interface Collection
49 */
50DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
c6994e6f 51
512ad27d 52#define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
d16f1726
CC
53/* 1 input terminal, 1 output terminal and 1 feature unit */
54#define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH + UAC_DT_INPUT_TERMINAL_SIZE \
55 + UAC_DT_OUTPUT_TERMINAL_SIZE + UAC_DT_FEATURE_UNIT_SIZE(0))
c6994e6f 56/* B.3.2 Class-Specific AC Interface Descriptor */
8d252db1 57static struct uac1_ac_header_descriptor_1 ac_header_desc = {
512ad27d 58 .bLength = UAC_DT_AC_HEADER_LENGTH,
c6994e6f 59 .bDescriptorType = USB_DT_CS_INTERFACE,
512ad27d 60 .bDescriptorSubtype = UAC_HEADER,
c6994e6f 61 .bcdADC = __constant_cpu_to_le16(0x0100),
d16f1726 62 .wTotalLength = __constant_cpu_to_le16(UAC_DT_TOTAL_LENGTH),
c6994e6f
BW
63 .bInCollection = F_AUDIO_NUM_INTERFACES,
64 .baInterfaceNr = {
8d252db1
XW
65 /* Interface number of the first AudioStream interface */
66 [0] = 1,
c6994e6f
BW
67 }
68};
69
70#define INPUT_TERMINAL_ID 1
512ad27d
LP
71static struct uac_input_terminal_descriptor input_terminal_desc = {
72 .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
c6994e6f 73 .bDescriptorType = USB_DT_CS_INTERFACE,
512ad27d 74 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
c6994e6f 75 .bTerminalID = INPUT_TERMINAL_ID,
512ad27d 76 .wTerminalType = UAC_TERMINAL_STREAMING,
c6994e6f
BW
77 .bAssocTerminal = 0,
78 .wChannelConfig = 0x3,
79};
80
512ad27d 81DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
c6994e6f
BW
82
83#define FEATURE_UNIT_ID 2
512ad27d
LP
84static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
85 .bLength = UAC_DT_FEATURE_UNIT_SIZE(0),
c6994e6f 86 .bDescriptorType = USB_DT_CS_INTERFACE,
512ad27d 87 .bDescriptorSubtype = UAC_FEATURE_UNIT,
c6994e6f
BW
88 .bUnitID = FEATURE_UNIT_ID,
89 .bSourceID = INPUT_TERMINAL_ID,
90 .bControlSize = 2,
512ad27d 91 .bmaControls[0] = (UAC_FU_MUTE | UAC_FU_VOLUME),
c6994e6f
BW
92};
93
94static struct usb_audio_control mute_control = {
95 .list = LIST_HEAD_INIT(mute_control.list),
96 .name = "Mute Control",
f07ff97b 97 .type = UAC_FU_MUTE,
c6994e6f
BW
98 /* Todo: add real Mute control code */
99 .set = generic_set_cmd,
100 .get = generic_get_cmd,
101};
102
103static struct usb_audio_control volume_control = {
104 .list = LIST_HEAD_INIT(volume_control.list),
105 .name = "Volume Control",
f07ff97b 106 .type = UAC_FU_VOLUME,
c6994e6f
BW
107 /* Todo: add real Volume control code */
108 .set = generic_set_cmd,
109 .get = generic_get_cmd,
110};
111
112static struct usb_audio_control_selector feature_unit = {
113 .list = LIST_HEAD_INIT(feature_unit.list),
114 .id = FEATURE_UNIT_ID,
115 .name = "Mute & Volume Control",
512ad27d 116 .type = UAC_FEATURE_UNIT,
c6994e6f
BW
117 .desc = (struct usb_descriptor_header *)&feature_unit_desc,
118};
119
120#define OUTPUT_TERMINAL_ID 3
69da9bcb 121static struct uac1_output_terminal_descriptor output_terminal_desc = {
512ad27d 122 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
c6994e6f 123 .bDescriptorType = USB_DT_CS_INTERFACE,
512ad27d 124 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
c6994e6f 125 .bTerminalID = OUTPUT_TERMINAL_ID,
512ad27d 126 .wTerminalType = UAC_OUTPUT_TERMINAL_SPEAKER,
c6994e6f
BW
127 .bAssocTerminal = FEATURE_UNIT_ID,
128 .bSourceID = FEATURE_UNIT_ID,
129};
130
131/* B.4.1 Standard AS Interface Descriptor */
132static struct usb_interface_descriptor as_interface_alt_0_desc = {
133 .bLength = USB_DT_INTERFACE_SIZE,
134 .bDescriptorType = USB_DT_INTERFACE,
135 .bAlternateSetting = 0,
136 .bNumEndpoints = 0,
137 .bInterfaceClass = USB_CLASS_AUDIO,
138 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
139};
140
141static struct usb_interface_descriptor as_interface_alt_1_desc = {
142 .bLength = USB_DT_INTERFACE_SIZE,
143 .bDescriptorType = USB_DT_INTERFACE,
144 .bAlternateSetting = 1,
145 .bNumEndpoints = 1,
146 .bInterfaceClass = USB_CLASS_AUDIO,
147 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
148};
149
150/* B.4.2 Class-Specific AS Interface Descriptor */
69da9bcb 151static struct uac1_as_header_descriptor as_header_desc = {
512ad27d 152 .bLength = UAC_DT_AS_HEADER_SIZE,
c6994e6f 153 .bDescriptorType = USB_DT_CS_INTERFACE,
512ad27d 154 .bDescriptorSubtype = UAC_AS_GENERAL,
c6994e6f
BW
155 .bTerminalLink = INPUT_TERMINAL_ID,
156 .bDelay = 1,
512ad27d 157 .wFormatTag = UAC_FORMAT_TYPE_I_PCM,
c6994e6f
BW
158};
159
512ad27d 160DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
c6994e6f 161
512ad27d
LP
162static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
163 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
c6994e6f 164 .bDescriptorType = USB_DT_CS_INTERFACE,
512ad27d
LP
165 .bDescriptorSubtype = UAC_FORMAT_TYPE,
166 .bFormatType = UAC_FORMAT_TYPE_I,
c6994e6f
BW
167 .bSubframeSize = 2,
168 .bBitResolution = 16,
169 .bSamFreqType = 1,
170};
171
172/* Standard ISO OUT Endpoint Descriptor */
e6251a92 173static struct usb_endpoint_descriptor as_out_ep_desc = {
c6994e6f
BW
174 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
175 .bDescriptorType = USB_DT_ENDPOINT,
176 .bEndpointAddress = USB_DIR_OUT,
85e08ca5 177 .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE
c6994e6f 178 | USB_ENDPOINT_XFER_ISOC,
bcec9784 179 .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
c6994e6f
BW
180 .bInterval = 4,
181};
182
183/* Class-specific AS ISO OUT Endpoint Descriptor */
f3a3406b 184static struct uac_iso_endpoint_descriptor as_iso_out_desc = {
512ad27d 185 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
c6994e6f 186 .bDescriptorType = USB_DT_CS_ENDPOINT,
512ad27d 187 .bDescriptorSubtype = UAC_EP_GENERAL,
c6994e6f
BW
188 .bmAttributes = 1,
189 .bLockDelayUnits = 1,
190 .wLockDelay = __constant_cpu_to_le16(1),
191};
192
f3a3406b 193static struct usb_descriptor_header *f_audio_desc[] = {
c6994e6f
BW
194 (struct usb_descriptor_header *)&ac_interface_desc,
195 (struct usb_descriptor_header *)&ac_header_desc,
196
197 (struct usb_descriptor_header *)&input_terminal_desc,
198 (struct usb_descriptor_header *)&output_terminal_desc,
199 (struct usb_descriptor_header *)&feature_unit_desc,
200
201 (struct usb_descriptor_header *)&as_interface_alt_0_desc,
202 (struct usb_descriptor_header *)&as_interface_alt_1_desc,
203 (struct usb_descriptor_header *)&as_header_desc,
204
205 (struct usb_descriptor_header *)&as_type_i_desc,
206
207 (struct usb_descriptor_header *)&as_out_ep_desc,
208 (struct usb_descriptor_header *)&as_iso_out_desc,
209 NULL,
210};
211
f73db69f
AP
212enum {
213 STR_AC_IF,
214 STR_INPUT_TERMINAL,
215 STR_INPUT_TERMINAL_CH_NAMES,
216 STR_FEAT_DESC_0,
217 STR_OUTPUT_TERMINAL,
218 STR_AS_IF_ALT0,
219 STR_AS_IF_ALT1,
220};
221
222static struct usb_string strings_uac1[] = {
223 [STR_AC_IF].s = "AC Interface",
224 [STR_INPUT_TERMINAL].s = "Input terminal",
225 [STR_INPUT_TERMINAL_CH_NAMES].s = "Channels",
226 [STR_FEAT_DESC_0].s = "Volume control & mute",
227 [STR_OUTPUT_TERMINAL].s = "Output terminal",
228 [STR_AS_IF_ALT0].s = "AS Interface",
229 [STR_AS_IF_ALT1].s = "AS Interface",
230 { },
231};
232
233static struct usb_gadget_strings str_uac1 = {
234 .language = 0x0409, /* en-us */
235 .strings = strings_uac1,
236};
237
238static struct usb_gadget_strings *uac1_strings[] = {
239 &str_uac1,
240 NULL,
241};
242
c6994e6f
BW
243/*
244 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
245 */
246
247/*-------------------------------------------------------------------------*/
248struct f_audio_buf {
249 u8 *buf;
250 int actual;
251 struct list_head list;
252};
253
254static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
255{
256 struct f_audio_buf *copy_buf;
257
258 copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
259 if (!copy_buf)
ff3b968c 260 return ERR_PTR(-ENOMEM);
c6994e6f
BW
261
262 copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
263 if (!copy_buf->buf) {
264 kfree(copy_buf);
ff3b968c 265 return ERR_PTR(-ENOMEM);
c6994e6f
BW
266 }
267
268 return copy_buf;
269}
270
271static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
272{
273 kfree(audio_buf->buf);
274 kfree(audio_buf);
275}
276/*-------------------------------------------------------------------------*/
277
278struct f_audio {
279 struct gaudio card;
280
1fc4926d
RB
281 u8 ac_intf, ac_alt;
282 u8 as_intf, as_alt;
283
c6994e6f
BW
284 /* endpoints handle full and/or high speeds */
285 struct usb_ep *out_ep;
c6994e6f
BW
286
287 spinlock_t lock;
288 struct f_audio_buf *copy_buf;
289 struct work_struct playback_work;
290 struct list_head play_queue;
291
292 /* Control Set command */
293 struct list_head cs;
294 u8 set_cmd;
295 struct usb_audio_control *set_con;
296};
297
298static inline struct f_audio *func_to_audio(struct usb_function *f)
299{
300 return container_of(f, struct f_audio, card.func);
301}
302
303/*-------------------------------------------------------------------------*/
304
305static void f_audio_playback_work(struct work_struct *data)
306{
307 struct f_audio *audio = container_of(data, struct f_audio,
308 playback_work);
309 struct f_audio_buf *play_buf;
310
311 spin_lock_irq(&audio->lock);
312 if (list_empty(&audio->play_queue)) {
313 spin_unlock_irq(&audio->lock);
314 return;
315 }
316 play_buf = list_first_entry(&audio->play_queue,
317 struct f_audio_buf, list);
318 list_del(&play_buf->list);
319 spin_unlock_irq(&audio->lock);
320
321 u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
322 f_audio_buffer_free(play_buf);
c6994e6f
BW
323}
324
325static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
326{
327 struct f_audio *audio = req->context;
328 struct usb_composite_dev *cdev = audio->card.func.config->cdev;
329 struct f_audio_buf *copy_buf = audio->copy_buf;
d355339e 330 struct f_uac1_legacy_opts *opts;
f3a3406b 331 int audio_buf_size;
c6994e6f
BW
332 int err;
333
d355339e 334 opts = container_of(audio->card.func.fi, struct f_uac1_legacy_opts,
f3a3406b
AP
335 func_inst);
336 audio_buf_size = opts->audio_buf_size;
605ef833 337
c6994e6f
BW
338 if (!copy_buf)
339 return -EINVAL;
340
341 /* Copy buffer is full, add it to the play_queue */
342 if (audio_buf_size - copy_buf->actual < req->actual) {
343 list_add_tail(&copy_buf->list, &audio->play_queue);
344 schedule_work(&audio->playback_work);
345 copy_buf = f_audio_buffer_alloc(audio_buf_size);
ff3b968c 346 if (IS_ERR(copy_buf))
c6994e6f
BW
347 return -ENOMEM;
348 }
349
350 memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
351 copy_buf->actual += req->actual;
352 audio->copy_buf = copy_buf;
353
354 err = usb_ep_queue(ep, req, GFP_ATOMIC);
355 if (err)
356 ERROR(cdev, "%s queue req: %d\n", ep->name, err);
357
358 return 0;
359
360}
361
362static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
363{
364 struct f_audio *audio = req->context;
365 int status = req->status;
366 u32 data = 0;
367 struct usb_ep *out_ep = audio->out_ep;
368
369 switch (status) {
370
371 case 0: /* normal completion? */
372 if (ep == out_ep)
373 f_audio_out_ep_complete(ep, req);
374 else if (audio->set_con) {
375 memcpy(&data, req->buf, req->length);
376 audio->set_con->set(audio->set_con, audio->set_cmd,
377 le16_to_cpu(data));
378 audio->set_con = NULL;
379 }
380 break;
381 default:
382 break;
383 }
384}
385
386static int audio_set_intf_req(struct usb_function *f,
387 const struct usb_ctrlrequest *ctrl)
388{
389 struct f_audio *audio = func_to_audio(f);
390 struct usb_composite_dev *cdev = f->config->cdev;
391 struct usb_request *req = cdev->req;
392 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
393 u16 len = le16_to_cpu(ctrl->wLength);
394 u16 w_value = le16_to_cpu(ctrl->wValue);
395 u8 con_sel = (w_value >> 8) & 0xFF;
396 u8 cmd = (ctrl->bRequest & 0x0F);
397 struct usb_audio_control_selector *cs;
398 struct usb_audio_control *con;
399
400 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
401 ctrl->bRequest, w_value, len, id);
402
403 list_for_each_entry(cs, &audio->cs, list) {
404 if (cs->id == id) {
405 list_for_each_entry(con, &cs->control, list) {
406 if (con->type == con_sel) {
407 audio->set_con = con;
408 break;
409 }
410 }
411 break;
412 }
413 }
414
415 audio->set_cmd = cmd;
416 req->context = audio;
417 req->complete = f_audio_complete;
418
419 return len;
420}
421
422static int audio_get_intf_req(struct usb_function *f,
423 const struct usb_ctrlrequest *ctrl)
424{
425 struct f_audio *audio = func_to_audio(f);
426 struct usb_composite_dev *cdev = f->config->cdev;
427 struct usb_request *req = cdev->req;
428 int value = -EOPNOTSUPP;
429 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
430 u16 len = le16_to_cpu(ctrl->wLength);
431 u16 w_value = le16_to_cpu(ctrl->wValue);
432 u8 con_sel = (w_value >> 8) & 0xFF;
433 u8 cmd = (ctrl->bRequest & 0x0F);
434 struct usb_audio_control_selector *cs;
435 struct usb_audio_control *con;
436
437 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
438 ctrl->bRequest, w_value, len, id);
439
440 list_for_each_entry(cs, &audio->cs, list) {
441 if (cs->id == id) {
442 list_for_each_entry(con, &cs->control, list) {
443 if (con->type == con_sel && con->get) {
444 value = con->get(con, cmd);
445 break;
446 }
447 }
448 break;
449 }
450 }
451
452 req->context = audio;
453 req->complete = f_audio_complete;
fddedd83 454 len = min_t(size_t, sizeof(value), len);
c6994e6f
BW
455 memcpy(req->buf, &value, len);
456
457 return len;
458}
459
0ad72524
LP
460static int audio_set_endpoint_req(struct usb_function *f,
461 const struct usb_ctrlrequest *ctrl)
462{
463 struct usb_composite_dev *cdev = f->config->cdev;
464 int value = -EOPNOTSUPP;
465 u16 ep = le16_to_cpu(ctrl->wIndex);
466 u16 len = le16_to_cpu(ctrl->wLength);
467 u16 w_value = le16_to_cpu(ctrl->wValue);
468
469 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
470 ctrl->bRequest, w_value, len, ep);
471
472 switch (ctrl->bRequest) {
473 case UAC_SET_CUR:
7c5881d1 474 value = len;
0ad72524
LP
475 break;
476
477 case UAC_SET_MIN:
478 break;
479
480 case UAC_SET_MAX:
481 break;
482
483 case UAC_SET_RES:
484 break;
485
486 case UAC_SET_MEM:
487 break;
488
489 default:
490 break;
491 }
492
493 return value;
494}
495
496static int audio_get_endpoint_req(struct usb_function *f,
497 const struct usb_ctrlrequest *ctrl)
498{
499 struct usb_composite_dev *cdev = f->config->cdev;
500 int value = -EOPNOTSUPP;
501 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
502 u16 len = le16_to_cpu(ctrl->wLength);
503 u16 w_value = le16_to_cpu(ctrl->wValue);
504
505 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
506 ctrl->bRequest, w_value, len, ep);
507
508 switch (ctrl->bRequest) {
509 case UAC_GET_CUR:
510 case UAC_GET_MIN:
511 case UAC_GET_MAX:
512 case UAC_GET_RES:
7c5881d1 513 value = len;
0ad72524
LP
514 break;
515 case UAC_GET_MEM:
516 break;
517 default:
518 break;
519 }
520
521 return value;
522}
523
c6994e6f
BW
524static int
525f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
526{
527 struct usb_composite_dev *cdev = f->config->cdev;
528 struct usb_request *req = cdev->req;
529 int value = -EOPNOTSUPP;
530 u16 w_index = le16_to_cpu(ctrl->wIndex);
531 u16 w_value = le16_to_cpu(ctrl->wValue);
532 u16 w_length = le16_to_cpu(ctrl->wLength);
533
0ad72524
LP
534 /* composite driver infrastructure handles everything; interface
535 * activation uses set_alt().
c6994e6f
BW
536 */
537 switch (ctrl->bRequestType) {
512ad27d 538 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
c6994e6f
BW
539 value = audio_set_intf_req(f, ctrl);
540 break;
541
512ad27d 542 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
c6994e6f
BW
543 value = audio_get_intf_req(f, ctrl);
544 break;
545
0ad72524
LP
546 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
547 value = audio_set_endpoint_req(f, ctrl);
548 break;
549
550 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
551 value = audio_get_endpoint_req(f, ctrl);
552 break;
553
c6994e6f
BW
554 default:
555 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
556 ctrl->bRequestType, ctrl->bRequest,
557 w_value, w_index, w_length);
558 }
559
560 /* respond with data transfer or status phase? */
561 if (value >= 0) {
562 DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
563 ctrl->bRequestType, ctrl->bRequest,
564 w_value, w_index, w_length);
565 req->zero = 0;
566 req->length = value;
567 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
568 if (value < 0)
569 ERROR(cdev, "audio response on err %d\n", value);
570 }
571
572 /* device either stalls (value < 0) or reports success */
573 return value;
574}
575
576static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
577{
578 struct f_audio *audio = func_to_audio(f);
579 struct usb_composite_dev *cdev = f->config->cdev;
580 struct usb_ep *out_ep = audio->out_ep;
581 struct usb_request *req;
d355339e 582 struct f_uac1_legacy_opts *opts;
f3a3406b 583 int req_buf_size, req_count, audio_buf_size;
c6994e6f
BW
584 int i = 0, err = 0;
585
586 DBG(cdev, "intf %d, alt %d\n", intf, alt);
587
d355339e 588 opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
f3a3406b
AP
589 req_buf_size = opts->req_buf_size;
590 req_count = opts->req_count;
591 audio_buf_size = opts->audio_buf_size;
592
1fc4926d
RB
593 /* No i/f has more than 2 alt settings */
594 if (alt > 1) {
595 ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__);
596 return -EINVAL;
597 }
598
599 if (intf == audio->ac_intf) {
600 /* Control I/f has only 1 AltSetting - 0 */
601 if (alt) {
602 ERROR(cdev, "%s:%d Error!\n", __func__, __LINE__);
603 return -EINVAL;
604 }
605 return 0;
606 } else if (intf == audio->as_intf) {
c6994e6f 607 if (alt == 1) {
ca4de53c
MT
608 err = config_ep_by_speed(cdev->gadget, f, out_ep);
609 if (err)
610 return err;
611
72c973dd 612 usb_ep_enable(out_ep);
c6994e6f 613 audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
ff3b968c
JL
614 if (IS_ERR(audio->copy_buf))
615 return -ENOMEM;
c6994e6f
BW
616
617 /*
618 * allocate a bunch of read buffers
619 * and queue them all at once.
620 */
621 for (i = 0; i < req_count && err == 0; i++) {
622 req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
623 if (req) {
624 req->buf = kzalloc(req_buf_size,
625 GFP_ATOMIC);
626 if (req->buf) {
627 req->length = req_buf_size;
628 req->context = audio;
629 req->complete =
630 f_audio_complete;
631 err = usb_ep_queue(out_ep,
632 req, GFP_ATOMIC);
633 if (err)
634 ERROR(cdev,
635 "%s queue req: %d\n",
636 out_ep->name, err);
637 } else
638 err = -ENOMEM;
639 } else
640 err = -ENOMEM;
641 }
642
643 } else {
644 struct f_audio_buf *copy_buf = audio->copy_buf;
645 if (copy_buf) {
646 list_add_tail(&copy_buf->list,
647 &audio->play_queue);
648 schedule_work(&audio->playback_work);
649 }
650 }
1fc4926d 651 audio->as_alt = alt;
c6994e6f
BW
652 }
653
654 return err;
655}
656
1fc4926d
RB
657static int f_audio_get_alt(struct usb_function *f, unsigned intf)
658{
659 struct f_audio *audio = func_to_audio(f);
660 struct usb_composite_dev *cdev = f->config->cdev;
661
662 if (intf == audio->ac_intf)
663 return audio->ac_alt;
664 else if (intf == audio->as_intf)
665 return audio->as_alt;
666 else
667 ERROR(cdev, "%s:%d Invalid Interface %d!\n",
668 __func__, __LINE__, intf);
669
670 return -EINVAL;
671}
672
c6994e6f
BW
673static void f_audio_disable(struct usb_function *f)
674{
675 return;
676}
677
678/*-------------------------------------------------------------------------*/
679
680static void f_audio_build_desc(struct f_audio *audio)
681{
682 struct gaudio *card = &audio->card;
683 u8 *sam_freq;
684 int rate;
685
686 /* Set channel numbers */
687 input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card);
688 as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card);
689
690 /* Set sample rates */
691 rate = u_audio_get_playback_rate(card);
692 sam_freq = as_type_i_desc.tSamFreq[0];
693 memcpy(sam_freq, &rate, 3);
694
695 /* Todo: Set Sample bits and other parameters */
696
697 return;
698}
699
700/* audio function driver setup/binding */
f3a3406b 701static int
c6994e6f
BW
702f_audio_bind(struct usb_configuration *c, struct usb_function *f)
703{
704 struct usb_composite_dev *cdev = c->cdev;
705 struct f_audio *audio = func_to_audio(f);
807dccdb 706 struct usb_string *us;
c6994e6f 707 int status;
10287bae 708 struct usb_ep *ep = NULL;
d355339e 709 struct f_uac1_legacy_opts *audio_opts;
f3a3406b 710
d355339e 711 audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
f3a3406b 712 audio->card.gadget = c->cdev->gadget;
f3a3406b
AP
713 /* set up ASLA audio devices */
714 if (!audio_opts->bound) {
715 status = gaudio_setup(&audio->card);
716 if (status < 0)
717 return status;
718 audio_opts->bound = true;
719 }
807dccdb
AP
720 us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
721 if (IS_ERR(us))
722 return PTR_ERR(us);
723 ac_interface_desc.iInterface = us[STR_AC_IF].id;
724 input_terminal_desc.iTerminal = us[STR_INPUT_TERMINAL].id;
725 input_terminal_desc.iChannelNames = us[STR_INPUT_TERMINAL_CH_NAMES].id;
726 feature_unit_desc.iFeature = us[STR_FEAT_DESC_0].id;
727 output_terminal_desc.iTerminal = us[STR_OUTPUT_TERMINAL].id;
728 as_interface_alt_0_desc.iInterface = us[STR_AS_IF_ALT0].id;
729 as_interface_alt_1_desc.iInterface = us[STR_AS_IF_ALT1].id;
730
c6994e6f
BW
731
732 f_audio_build_desc(audio);
733
734 /* allocate instance-specific interface IDs, and patch descriptors */
735 status = usb_interface_id(c, f);
736 if (status < 0)
737 goto fail;
738 ac_interface_desc.bInterfaceNumber = status;
1fc4926d
RB
739 audio->ac_intf = status;
740 audio->ac_alt = 0;
c6994e6f
BW
741
742 status = usb_interface_id(c, f);
743 if (status < 0)
744 goto fail;
745 as_interface_alt_0_desc.bInterfaceNumber = status;
746 as_interface_alt_1_desc.bInterfaceNumber = status;
1fc4926d
RB
747 audio->as_intf = status;
748 audio->as_alt = 0;
c6994e6f
BW
749
750 status = -ENODEV;
751
752 /* allocate instance-specific endpoints */
753 ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
754 if (!ep)
755 goto fail;
756 audio->out_ep = ep;
72c973dd 757 audio->out_ep->desc = &as_out_ep_desc;
c6994e6f
BW
758
759 status = -ENOMEM;
760
ef7f584c 761 /* copy descriptors, and track endpoint copies */
eaef50c7
JY
762 status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL,
763 NULL);
10287bae
SAS
764 if (status)
765 goto fail;
c6994e6f
BW
766 return 0;
767
768fail:
f3a3406b 769 gaudio_cleanup(&audio->card);
c6994e6f
BW
770 return status;
771}
772
c6994e6f
BW
773/*-------------------------------------------------------------------------*/
774
b95cd7ec
LP
775static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
776{
777 con->data[cmd] = value;
778
779 return 0;
780}
781
782static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
783{
784 return con->data[cmd];
785}
786
c6994e6f 787/* Todo: add more control selecotor dynamically */
f3a3406b 788static int control_selector_init(struct f_audio *audio)
c6994e6f
BW
789{
790 INIT_LIST_HEAD(&audio->cs);
791 list_add(&feature_unit.list, &audio->cs);
792
793 INIT_LIST_HEAD(&feature_unit.control);
794 list_add(&mute_control.list, &feature_unit.control);
795 list_add(&volume_control.list, &feature_unit.control);
796
512ad27d
LP
797 volume_control.data[UAC__CUR] = 0xffc0;
798 volume_control.data[UAC__MIN] = 0xe3a0;
799 volume_control.data[UAC__MAX] = 0xfff0;
800 volume_control.data[UAC__RES] = 0x0030;
c6994e6f
BW
801
802 return 0;
803}
804
d355339e
RB
805static inline
806struct f_uac1_legacy_opts *to_f_uac1_opts(struct config_item *item)
0854611a 807{
d355339e 808 return container_of(to_config_group(item), struct f_uac1_legacy_opts,
0854611a
AP
809 func_inst.group);
810}
811
0854611a
AP
812static void f_uac1_attr_release(struct config_item *item)
813{
d355339e 814 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item);
0854611a
AP
815
816 usb_put_function_instance(&opts->func_inst);
817}
818
819static struct configfs_item_operations f_uac1_item_ops = {
820 .release = f_uac1_attr_release,
0854611a
AP
821};
822
823#define UAC1_INT_ATTRIBUTE(name) \
c6f89f1c 824static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
0854611a
AP
825 char *page) \
826{ \
d355339e 827 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
0854611a
AP
828 int result; \
829 \
830 mutex_lock(&opts->lock); \
831 result = sprintf(page, "%u\n", opts->name); \
832 mutex_unlock(&opts->lock); \
833 \
834 return result; \
835} \
836 \
c6f89f1c 837static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
0854611a
AP
838 const char *page, size_t len) \
839{ \
d355339e 840 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
0854611a
AP
841 int ret; \
842 u32 num; \
843 \
844 mutex_lock(&opts->lock); \
845 if (opts->refcnt) { \
846 ret = -EBUSY; \
847 goto end; \
848 } \
849 \
850 ret = kstrtou32(page, 0, &num); \
851 if (ret) \
852 goto end; \
853 \
854 opts->name = num; \
855 ret = len; \
856 \
857end: \
858 mutex_unlock(&opts->lock); \
859 return ret; \
860} \
861 \
c6f89f1c 862CONFIGFS_ATTR(f_uac1_opts_, name)
0854611a
AP
863
864UAC1_INT_ATTRIBUTE(req_buf_size);
865UAC1_INT_ATTRIBUTE(req_count);
866UAC1_INT_ATTRIBUTE(audio_buf_size);
867
868#define UAC1_STR_ATTRIBUTE(name) \
c6f89f1c 869static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
0854611a
AP
870 char *page) \
871{ \
d355339e 872 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
0854611a
AP
873 int result; \
874 \
875 mutex_lock(&opts->lock); \
876 result = sprintf(page, "%s\n", opts->name); \
877 mutex_unlock(&opts->lock); \
878 \
879 return result; \
880} \
881 \
c6f89f1c 882static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
0854611a
AP
883 const char *page, size_t len) \
884{ \
d355339e 885 struct f_uac1_legacy_opts *opts = to_f_uac1_opts(item); \
0854611a
AP
886 int ret = -EBUSY; \
887 char *tmp; \
888 \
889 mutex_lock(&opts->lock); \
890 if (opts->refcnt) \
891 goto end; \
892 \
893 tmp = kstrndup(page, len, GFP_KERNEL); \
894 if (tmp) { \
895 ret = -ENOMEM; \
896 goto end; \
897 } \
898 if (opts->name##_alloc) \
899 kfree(opts->name); \
900 opts->name##_alloc = true; \
901 opts->name = tmp; \
902 ret = len; \
903 \
904end: \
905 mutex_unlock(&opts->lock); \
906 return ret; \
907} \
908 \
c6f89f1c 909CONFIGFS_ATTR(f_uac1_opts_, name)
0854611a
AP
910
911UAC1_STR_ATTRIBUTE(fn_play);
912UAC1_STR_ATTRIBUTE(fn_cap);
913UAC1_STR_ATTRIBUTE(fn_cntl);
914
915static struct configfs_attribute *f_uac1_attrs[] = {
c6f89f1c
CH
916 &f_uac1_opts_attr_req_buf_size,
917 &f_uac1_opts_attr_req_count,
918 &f_uac1_opts_attr_audio_buf_size,
919 &f_uac1_opts_attr_fn_play,
920 &f_uac1_opts_attr_fn_cap,
921 &f_uac1_opts_attr_fn_cntl,
0854611a
AP
922 NULL,
923};
924
925static struct config_item_type f_uac1_func_type = {
926 .ct_item_ops = &f_uac1_item_ops,
927 .ct_attrs = f_uac1_attrs,
928 .ct_owner = THIS_MODULE,
929};
930
f3a3406b
AP
931static void f_audio_free_inst(struct usb_function_instance *f)
932{
d355339e 933 struct f_uac1_legacy_opts *opts;
f3a3406b 934
d355339e 935 opts = container_of(f, struct f_uac1_legacy_opts, func_inst);
0854611a
AP
936 if (opts->fn_play_alloc)
937 kfree(opts->fn_play);
938 if (opts->fn_cap_alloc)
939 kfree(opts->fn_cap);
940 if (opts->fn_cntl_alloc)
941 kfree(opts->fn_cntl);
f3a3406b
AP
942 kfree(opts);
943}
944
945static struct usb_function_instance *f_audio_alloc_inst(void)
946{
d355339e 947 struct f_uac1_legacy_opts *opts;
f3a3406b
AP
948
949 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
950 if (!opts)
951 return ERR_PTR(-ENOMEM);
952
0854611a 953 mutex_init(&opts->lock);
f3a3406b
AP
954 opts->func_inst.free_func_inst = f_audio_free_inst;
955
0854611a
AP
956 config_group_init_type_name(&opts->func_inst.group, "",
957 &f_uac1_func_type);
958
959 opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE;
960 opts->req_count = UAC1_REQ_COUNT;
961 opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE;
962 opts->fn_play = FILE_PCM_PLAYBACK;
963 opts->fn_cap = FILE_PCM_CAPTURE;
964 opts->fn_cntl = FILE_CONTROL;
f3a3406b
AP
965 return &opts->func_inst;
966}
967
968static void f_audio_free(struct usb_function *f)
969{
970 struct f_audio *audio = func_to_audio(f);
d355339e 971 struct f_uac1_legacy_opts *opts;
f3a3406b 972
4fde6204 973 gaudio_cleanup(&audio->card);
d355339e 974 opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
f3a3406b 975 kfree(audio);
0854611a
AP
976 mutex_lock(&opts->lock);
977 --opts->refcnt;
978 mutex_unlock(&opts->lock);
f3a3406b
AP
979}
980
981static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
982{
983 usb_free_all_descriptors(f);
984}
985
986static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
987{
988 struct f_audio *audio;
d355339e 989 struct f_uac1_legacy_opts *opts;
f3a3406b
AP
990
991 /* allocate and initialize one new instance */
992 audio = kzalloc(sizeof(*audio), GFP_KERNEL);
993 if (!audio)
994 return ERR_PTR(-ENOMEM);
995
996 audio->card.func.name = "g_audio";
997
d355339e 998 opts = container_of(fi, struct f_uac1_legacy_opts, func_inst);
0854611a
AP
999 mutex_lock(&opts->lock);
1000 ++opts->refcnt;
1001 mutex_unlock(&opts->lock);
f3a3406b
AP
1002 INIT_LIST_HEAD(&audio->play_queue);
1003 spin_lock_init(&audio->lock);
1004
f3a3406b
AP
1005 audio->card.func.bind = f_audio_bind;
1006 audio->card.func.unbind = f_audio_unbind;
1007 audio->card.func.set_alt = f_audio_set_alt;
1fc4926d 1008 audio->card.func.get_alt = f_audio_get_alt;
f3a3406b
AP
1009 audio->card.func.setup = f_audio_setup;
1010 audio->card.func.disable = f_audio_disable;
1011 audio->card.func.free_func = f_audio_free;
1012
1013 control_selector_init(audio);
1014
1015 INIT_WORK(&audio->playback_work, f_audio_playback_work);
1016
1017 return &audio->card.func;
1018}
1019
d355339e 1020DECLARE_USB_FUNCTION_INIT(uac1_legacy, f_audio_alloc_inst, f_audio_alloc);
f3a3406b
AP
1021MODULE_LICENSE("GPL");
1022MODULE_AUTHOR("Bryan Wu");