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