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