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