1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * f_uac1.c -- USB Audio Class 1.0 Function (using u_audio API)
4 *
5 * Copyright (C) 2016 Ruslan Bilovol <ruslan.bilovol@gmail.com>
6 *
7 * This driver doesn't expect any real Audio codec to be present
8 * on the device - the audio streams are simply sinked to and
9 * sourced from a virtual ALSA sound card created.
10 *
11 * This file is based on f_uac1.c which is
12 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
13 * Copyright (C) 2008 Analog Devices, Inc
14 */
15
16 #include <linux/usb/audio.h>
17 #include <linux/module.h>
18
19 #include "u_audio.h"
20 #include "u_uac1.h"
21
22 /* UAC1 spec: 3.7.2.3 Audio Channel Cluster Format */
23 #define UAC1_CHANNEL_MASK 0x0FFF
24
25 struct f_uac1 {
26 struct g_audio g_audio;
27 u8 ac_intf, as_in_intf, as_out_intf;
28 u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */
29 };
30
func_to_uac1(struct usb_function * f)31 static inline struct f_uac1 *func_to_uac1(struct usb_function *f)
32 {
33 return container_of(f, struct f_uac1, g_audio.func);
34 }
35
g_audio_to_uac1_opts(struct g_audio * audio)36 static inline struct f_uac1_opts *g_audio_to_uac1_opts(struct g_audio *audio)
37 {
38 return container_of(audio->func.fi, struct f_uac1_opts, func_inst);
39 }
40
41 /*
42 * DESCRIPTORS ... most are static, but strings and full
43 * configuration descriptors are built on demand.
44 */
45
46 /*
47 * We have three interfaces - one AudioControl and two AudioStreaming
48 *
49 * The driver implements a simple UAC_1 topology.
50 * USB-OUT -> IT_1 -> OT_2 -> ALSA_Capture
51 * ALSA_Playback -> IT_3 -> OT_4 -> USB-IN
52 */
53 #define F_AUDIO_AC_INTERFACE 0
54 #define F_AUDIO_AS_OUT_INTERFACE 1
55 #define F_AUDIO_AS_IN_INTERFACE 2
56 /* Number of streaming interfaces */
57 #define F_AUDIO_NUM_INTERFACES 2
58
59 /* B.3.1 Standard AC Interface Descriptor */
60 static struct usb_interface_descriptor ac_interface_desc = {
61 .bLength = USB_DT_INTERFACE_SIZE,
62 .bDescriptorType = USB_DT_INTERFACE,
63 .bNumEndpoints = 0,
64 .bInterfaceClass = USB_CLASS_AUDIO,
65 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
66 };
67
68 /*
69 * The number of AudioStreaming and MIDIStreaming interfaces
70 * in the Audio Interface Collection
71 */
72 DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
73
74 #define UAC_DT_AC_HEADER_LENGTH UAC_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
75 /* 2 input terminals and 2 output terminals */
76 #define UAC_DT_TOTAL_LENGTH (UAC_DT_AC_HEADER_LENGTH \
77 + 2*UAC_DT_INPUT_TERMINAL_SIZE + 2*UAC_DT_OUTPUT_TERMINAL_SIZE)
78 /* B.3.2 Class-Specific AC Interface Descriptor */
79 static struct uac1_ac_header_descriptor_2 ac_header_desc = {
80 .bLength = UAC_DT_AC_HEADER_LENGTH,
81 .bDescriptorType = USB_DT_CS_INTERFACE,
82 .bDescriptorSubtype = UAC_HEADER,
83 .bcdADC = cpu_to_le16(0x0100),
84 .wTotalLength = cpu_to_le16(UAC_DT_TOTAL_LENGTH),
85 .bInCollection = F_AUDIO_NUM_INTERFACES,
86 .baInterfaceNr = {
87 /* Interface number of the AudioStream interfaces */
88 [0] = 1,
89 [1] = 2,
90 }
91 };
92
93 #define USB_OUT_IT_ID 1
94 static struct uac_input_terminal_descriptor usb_out_it_desc = {
95 .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
96 .bDescriptorType = USB_DT_CS_INTERFACE,
97 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
98 .bTerminalID = USB_OUT_IT_ID,
99 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
100 .bAssocTerminal = 0,
101 .wChannelConfig = cpu_to_le16(0x3),
102 };
103
104 #define IO_OUT_OT_ID 2
105 static struct uac1_output_terminal_descriptor io_out_ot_desc = {
106 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
107 .bDescriptorType = USB_DT_CS_INTERFACE,
108 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
109 .bTerminalID = IO_OUT_OT_ID,
110 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_SPEAKER),
111 .bAssocTerminal = 0,
112 .bSourceID = USB_OUT_IT_ID,
113 };
114
115 #define IO_IN_IT_ID 3
116 static struct uac_input_terminal_descriptor io_in_it_desc = {
117 .bLength = UAC_DT_INPUT_TERMINAL_SIZE,
118 .bDescriptorType = USB_DT_CS_INTERFACE,
119 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
120 .bTerminalID = IO_IN_IT_ID,
121 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_MICROPHONE),
122 .bAssocTerminal = 0,
123 .wChannelConfig = cpu_to_le16(0x3),
124 };
125
126 #define USB_IN_OT_ID 4
127 static struct uac1_output_terminal_descriptor usb_in_ot_desc = {
128 .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE,
129 .bDescriptorType = USB_DT_CS_INTERFACE,
130 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
131 .bTerminalID = USB_IN_OT_ID,
132 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
133 .bAssocTerminal = 0,
134 .bSourceID = IO_IN_IT_ID,
135 };
136
137 /* B.4.1 Standard AS Interface Descriptor */
138 static struct usb_interface_descriptor as_out_interface_alt_0_desc = {
139 .bLength = USB_DT_INTERFACE_SIZE,
140 .bDescriptorType = USB_DT_INTERFACE,
141 .bAlternateSetting = 0,
142 .bNumEndpoints = 0,
143 .bInterfaceClass = USB_CLASS_AUDIO,
144 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
145 };
146
147 static struct usb_interface_descriptor as_out_interface_alt_1_desc = {
148 .bLength = USB_DT_INTERFACE_SIZE,
149 .bDescriptorType = USB_DT_INTERFACE,
150 .bAlternateSetting = 1,
151 .bNumEndpoints = 1,
152 .bInterfaceClass = USB_CLASS_AUDIO,
153 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
154 };
155
156 static struct usb_interface_descriptor as_in_interface_alt_0_desc = {
157 .bLength = USB_DT_INTERFACE_SIZE,
158 .bDescriptorType = USB_DT_INTERFACE,
159 .bAlternateSetting = 0,
160 .bNumEndpoints = 0,
161 .bInterfaceClass = USB_CLASS_AUDIO,
162 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
163 };
164
165 static struct usb_interface_descriptor as_in_interface_alt_1_desc = {
166 .bLength = USB_DT_INTERFACE_SIZE,
167 .bDescriptorType = USB_DT_INTERFACE,
168 .bAlternateSetting = 1,
169 .bNumEndpoints = 1,
170 .bInterfaceClass = USB_CLASS_AUDIO,
171 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
172 };
173
174 /* B.4.2 Class-Specific AS Interface Descriptor */
175 static struct uac1_as_header_descriptor as_out_header_desc = {
176 .bLength = UAC_DT_AS_HEADER_SIZE,
177 .bDescriptorType = USB_DT_CS_INTERFACE,
178 .bDescriptorSubtype = UAC_AS_GENERAL,
179 .bTerminalLink = USB_OUT_IT_ID,
180 .bDelay = 1,
181 .wFormatTag = cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
182 };
183
184 static struct uac1_as_header_descriptor as_in_header_desc = {
185 .bLength = UAC_DT_AS_HEADER_SIZE,
186 .bDescriptorType = USB_DT_CS_INTERFACE,
187 .bDescriptorSubtype = UAC_AS_GENERAL,
188 .bTerminalLink = USB_IN_OT_ID,
189 .bDelay = 1,
190 .wFormatTag = cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
191 };
192
193 DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
194
195 static struct uac_format_type_i_discrete_descriptor_1 as_out_type_i_desc = {
196 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
197 .bDescriptorType = USB_DT_CS_INTERFACE,
198 .bDescriptorSubtype = UAC_FORMAT_TYPE,
199 .bFormatType = UAC_FORMAT_TYPE_I,
200 .bSubframeSize = 2,
201 .bBitResolution = 16,
202 .bSamFreqType = 1,
203 };
204
205 /* Standard ISO OUT Endpoint Descriptor */
206 static struct usb_endpoint_descriptor as_out_ep_desc = {
207 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
208 .bDescriptorType = USB_DT_ENDPOINT,
209 .bEndpointAddress = USB_DIR_OUT,
210 .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE
211 | USB_ENDPOINT_XFER_ISOC,
212 .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
213 .bInterval = 4,
214 };
215
216 /* Class-specific AS ISO OUT Endpoint Descriptor */
217 static struct uac_iso_endpoint_descriptor as_iso_out_desc = {
218 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
219 .bDescriptorType = USB_DT_CS_ENDPOINT,
220 .bDescriptorSubtype = UAC_EP_GENERAL,
221 .bmAttributes = 1,
222 .bLockDelayUnits = 1,
223 .wLockDelay = cpu_to_le16(1),
224 };
225
226 static struct uac_format_type_i_discrete_descriptor_1 as_in_type_i_desc = {
227 .bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
228 .bDescriptorType = USB_DT_CS_INTERFACE,
229 .bDescriptorSubtype = UAC_FORMAT_TYPE,
230 .bFormatType = UAC_FORMAT_TYPE_I,
231 .bSubframeSize = 2,
232 .bBitResolution = 16,
233 .bSamFreqType = 1,
234 };
235
236 /* Standard ISO OUT Endpoint Descriptor */
237 static struct usb_endpoint_descriptor as_in_ep_desc = {
238 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
239 .bDescriptorType = USB_DT_ENDPOINT,
240 .bEndpointAddress = USB_DIR_IN,
241 .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
242 | USB_ENDPOINT_XFER_ISOC,
243 .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE),
244 .bInterval = 4,
245 };
246
247 /* Class-specific AS ISO OUT Endpoint Descriptor */
248 static struct uac_iso_endpoint_descriptor as_iso_in_desc = {
249 .bLength = UAC_ISO_ENDPOINT_DESC_SIZE,
250 .bDescriptorType = USB_DT_CS_ENDPOINT,
251 .bDescriptorSubtype = UAC_EP_GENERAL,
252 .bmAttributes = 1,
253 .bLockDelayUnits = 0,
254 .wLockDelay = 0,
255 };
256
257 static struct usb_descriptor_header *f_audio_desc[] = {
258 (struct usb_descriptor_header *)&ac_interface_desc,
259 (struct usb_descriptor_header *)&ac_header_desc,
260
261 (struct usb_descriptor_header *)&usb_out_it_desc,
262 (struct usb_descriptor_header *)&io_out_ot_desc,
263 (struct usb_descriptor_header *)&io_in_it_desc,
264 (struct usb_descriptor_header *)&usb_in_ot_desc,
265
266 (struct usb_descriptor_header *)&as_out_interface_alt_0_desc,
267 (struct usb_descriptor_header *)&as_out_interface_alt_1_desc,
268 (struct usb_descriptor_header *)&as_out_header_desc,
269
270 (struct usb_descriptor_header *)&as_out_type_i_desc,
271
272 (struct usb_descriptor_header *)&as_out_ep_desc,
273 (struct usb_descriptor_header *)&as_iso_out_desc,
274
275 (struct usb_descriptor_header *)&as_in_interface_alt_0_desc,
276 (struct usb_descriptor_header *)&as_in_interface_alt_1_desc,
277 (struct usb_descriptor_header *)&as_in_header_desc,
278
279 (struct usb_descriptor_header *)&as_in_type_i_desc,
280
281 (struct usb_descriptor_header *)&as_in_ep_desc,
282 (struct usb_descriptor_header *)&as_iso_in_desc,
283 NULL,
284 };
285
286 enum {
287 STR_AC_IF,
288 STR_USB_OUT_IT,
289 STR_USB_OUT_IT_CH_NAMES,
290 STR_IO_OUT_OT,
291 STR_IO_IN_IT,
292 STR_IO_IN_IT_CH_NAMES,
293 STR_USB_IN_OT,
294 STR_AS_OUT_IF_ALT0,
295 STR_AS_OUT_IF_ALT1,
296 STR_AS_IN_IF_ALT0,
297 STR_AS_IN_IF_ALT1,
298 };
299
300 static struct usb_string strings_uac1[] = {
301 [STR_AC_IF].s = "AC Interface",
302 [STR_USB_OUT_IT].s = "Playback Input terminal",
303 [STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels",
304 [STR_IO_OUT_OT].s = "Playback Output terminal",
305 [STR_IO_IN_IT].s = "Capture Input terminal",
306 [STR_IO_IN_IT_CH_NAMES].s = "Capture Channels",
307 [STR_USB_IN_OT].s = "Capture Output terminal",
308 [STR_AS_OUT_IF_ALT0].s = "Playback Inactive",
309 [STR_AS_OUT_IF_ALT1].s = "Playback Active",
310 [STR_AS_IN_IF_ALT0].s = "Capture Inactive",
311 [STR_AS_IN_IF_ALT1].s = "Capture Active",
312 { },
313 };
314
315 static struct usb_gadget_strings str_uac1 = {
316 .language = 0x0409, /* en-us */
317 .strings = strings_uac1,
318 };
319
320 static struct usb_gadget_strings *uac1_strings[] = {
321 &str_uac1,
322 NULL,
323 };
324
325 /*
326 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
327 */
328
audio_set_endpoint_req(struct usb_function * f,const struct usb_ctrlrequest * ctrl)329 static int audio_set_endpoint_req(struct usb_function *f,
330 const struct usb_ctrlrequest *ctrl)
331 {
332 struct usb_composite_dev *cdev = f->config->cdev;
333 int value = -EOPNOTSUPP;
334 u16 ep = le16_to_cpu(ctrl->wIndex);
335 u16 len = le16_to_cpu(ctrl->wLength);
336 u16 w_value = le16_to_cpu(ctrl->wValue);
337
338 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
339 ctrl->bRequest, w_value, len, ep);
340
341 switch (ctrl->bRequest) {
342 case UAC_SET_CUR:
343 value = len;
344 break;
345
346 case UAC_SET_MIN:
347 break;
348
349 case UAC_SET_MAX:
350 break;
351
352 case UAC_SET_RES:
353 break;
354
355 case UAC_SET_MEM:
356 break;
357
358 default:
359 break;
360 }
361
362 return value;
363 }
364
audio_get_endpoint_req(struct usb_function * f,const struct usb_ctrlrequest * ctrl)365 static int audio_get_endpoint_req(struct usb_function *f,
366 const struct usb_ctrlrequest *ctrl)
367 {
368 struct usb_composite_dev *cdev = f->config->cdev;
369 int value = -EOPNOTSUPP;
370 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
371 u16 len = le16_to_cpu(ctrl->wLength);
372 u16 w_value = le16_to_cpu(ctrl->wValue);
373
374 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
375 ctrl->bRequest, w_value, len, ep);
376
377 switch (ctrl->bRequest) {
378 case UAC_GET_CUR:
379 case UAC_GET_MIN:
380 case UAC_GET_MAX:
381 case UAC_GET_RES:
382 value = len;
383 break;
384 case UAC_GET_MEM:
385 break;
386 default:
387 break;
388 }
389
390 return value;
391 }
392
393 static int
f_audio_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)394 f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
395 {
396 struct usb_composite_dev *cdev = f->config->cdev;
397 struct usb_request *req = cdev->req;
398 int value = -EOPNOTSUPP;
399 u16 w_index = le16_to_cpu(ctrl->wIndex);
400 u16 w_value = le16_to_cpu(ctrl->wValue);
401 u16 w_length = le16_to_cpu(ctrl->wLength);
402
403 /* composite driver infrastructure handles everything; interface
404 * activation uses set_alt().
405 */
406 switch (ctrl->bRequestType) {
407 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
408 value = audio_set_endpoint_req(f, ctrl);
409 break;
410
411 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
412 value = audio_get_endpoint_req(f, ctrl);
413 break;
414
415 default:
416 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
417 ctrl->bRequestType, ctrl->bRequest,
418 w_value, w_index, w_length);
419 }
420
421 /* respond with data transfer or status phase? */
422 if (value >= 0) {
423 DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
424 ctrl->bRequestType, ctrl->bRequest,
425 w_value, w_index, w_length);
426 req->zero = 0;
427 req->length = value;
428 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
429 if (value < 0)
430 ERROR(cdev, "audio response on err %d\n", value);
431 }
432
433 /* device either stalls (value < 0) or reports success */
434 return value;
435 }
436
f_audio_set_alt(struct usb_function * f,unsigned intf,unsigned alt)437 static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
438 {
439 struct usb_composite_dev *cdev = f->config->cdev;
440 struct usb_gadget *gadget = cdev->gadget;
441 struct device *dev = &gadget->dev;
442 struct f_uac1 *uac1 = func_to_uac1(f);
443 int ret = 0;
444
445 /* No i/f has more than 2 alt settings */
446 if (alt > 1) {
447 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
448 return -EINVAL;
449 }
450
451 if (intf == uac1->ac_intf) {
452 /* Control I/f has only 1 AltSetting - 0 */
453 if (alt) {
454 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
455 return -EINVAL;
456 }
457 return 0;
458 }
459
460 if (intf == uac1->as_out_intf) {
461 uac1->as_out_alt = alt;
462
463 if (alt)
464 ret = u_audio_start_capture(&uac1->g_audio);
465 else
466 u_audio_stop_capture(&uac1->g_audio);
467 } else if (intf == uac1->as_in_intf) {
468 uac1->as_in_alt = alt;
469
470 if (alt)
471 ret = u_audio_start_playback(&uac1->g_audio);
472 else
473 u_audio_stop_playback(&uac1->g_audio);
474 } else {
475 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
476 return -EINVAL;
477 }
478
479 return ret;
480 }
481
f_audio_get_alt(struct usb_function * f,unsigned intf)482 static int f_audio_get_alt(struct usb_function *f, unsigned intf)
483 {
484 struct usb_composite_dev *cdev = f->config->cdev;
485 struct usb_gadget *gadget = cdev->gadget;
486 struct device *dev = &gadget->dev;
487 struct f_uac1 *uac1 = func_to_uac1(f);
488
489 if (intf == uac1->ac_intf)
490 return uac1->ac_alt;
491 else if (intf == uac1->as_out_intf)
492 return uac1->as_out_alt;
493 else if (intf == uac1->as_in_intf)
494 return uac1->as_in_alt;
495 else
496 dev_err(dev, "%s:%d Invalid Interface %d!\n",
497 __func__, __LINE__, intf);
498
499 return -EINVAL;
500 }
501
502
f_audio_disable(struct usb_function * f)503 static void f_audio_disable(struct usb_function *f)
504 {
505 struct f_uac1 *uac1 = func_to_uac1(f);
506
507 uac1->as_out_alt = 0;
508 uac1->as_in_alt = 0;
509
510 u_audio_stop_playback(&uac1->g_audio);
511 u_audio_stop_capture(&uac1->g_audio);
512 }
513
514 /*-------------------------------------------------------------------------*/
515
f_audio_validate_opts(struct g_audio * audio,struct device * dev)516 static int f_audio_validate_opts(struct g_audio *audio, struct device *dev)
517 {
518 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
519
520 if (!opts->p_chmask && !opts->c_chmask) {
521 dev_err(dev, "Error: no playback and capture channels\n");
522 return -EINVAL;
523 } else if (opts->p_chmask & ~UAC1_CHANNEL_MASK) {
524 dev_err(dev, "Error: unsupported playback channels mask\n");
525 return -EINVAL;
526 } else if (opts->c_chmask & ~UAC1_CHANNEL_MASK) {
527 dev_err(dev, "Error: unsupported capture channels mask\n");
528 return -EINVAL;
529 } else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
530 dev_err(dev, "Error: incorrect playback sample size\n");
531 return -EINVAL;
532 } else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
533 dev_err(dev, "Error: incorrect capture sample size\n");
534 return -EINVAL;
535 } else if (!opts->p_srate) {
536 dev_err(dev, "Error: incorrect playback sampling rate\n");
537 return -EINVAL;
538 } else if (!opts->c_srate) {
539 dev_err(dev, "Error: incorrect capture sampling rate\n");
540 return -EINVAL;
541 }
542
543 return 0;
544 }
545
546 /* audio function driver setup/binding */
f_audio_bind(struct usb_configuration * c,struct usb_function * f)547 static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
548 {
549 struct usb_composite_dev *cdev = c->cdev;
550 struct usb_gadget *gadget = cdev->gadget;
551 struct device *dev = &gadget->dev;
552 struct f_uac1 *uac1 = func_to_uac1(f);
553 struct g_audio *audio = func_to_g_audio(f);
554 struct f_uac1_opts *audio_opts;
555 struct usb_ep *ep = NULL;
556 struct usb_string *us;
557 u8 *sam_freq;
558 int rate;
559 int status;
560
561 status = f_audio_validate_opts(audio, dev);
562 if (status)
563 return status;
564
565 audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst);
566
567 us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
568 if (IS_ERR(us))
569 return PTR_ERR(us);
570 ac_interface_desc.iInterface = us[STR_AC_IF].id;
571 usb_out_it_desc.iTerminal = us[STR_USB_OUT_IT].id;
572 usb_out_it_desc.iChannelNames = us[STR_USB_OUT_IT_CH_NAMES].id;
573 io_out_ot_desc.iTerminal = us[STR_IO_OUT_OT].id;
574 as_out_interface_alt_0_desc.iInterface = us[STR_AS_OUT_IF_ALT0].id;
575 as_out_interface_alt_1_desc.iInterface = us[STR_AS_OUT_IF_ALT1].id;
576 io_in_it_desc.iTerminal = us[STR_IO_IN_IT].id;
577 io_in_it_desc.iChannelNames = us[STR_IO_IN_IT_CH_NAMES].id;
578 usb_in_ot_desc.iTerminal = us[STR_USB_IN_OT].id;
579 as_in_interface_alt_0_desc.iInterface = us[STR_AS_IN_IF_ALT0].id;
580 as_in_interface_alt_1_desc.iInterface = us[STR_AS_IN_IF_ALT1].id;
581
582 /* Set channel numbers */
583 usb_out_it_desc.bNrChannels = num_channels(audio_opts->c_chmask);
584 usb_out_it_desc.wChannelConfig = cpu_to_le16(audio_opts->c_chmask);
585 as_out_type_i_desc.bNrChannels = num_channels(audio_opts->c_chmask);
586 as_out_type_i_desc.bSubframeSize = audio_opts->c_ssize;
587 as_out_type_i_desc.bBitResolution = audio_opts->c_ssize * 8;
588 io_in_it_desc.bNrChannels = num_channels(audio_opts->p_chmask);
589 io_in_it_desc.wChannelConfig = cpu_to_le16(audio_opts->p_chmask);
590 as_in_type_i_desc.bNrChannels = num_channels(audio_opts->p_chmask);
591 as_in_type_i_desc.bSubframeSize = audio_opts->p_ssize;
592 as_in_type_i_desc.bBitResolution = audio_opts->p_ssize * 8;
593
594 /* Set sample rates */
595 rate = audio_opts->c_srate;
596 sam_freq = as_out_type_i_desc.tSamFreq[0];
597 memcpy(sam_freq, &rate, 3);
598 rate = audio_opts->p_srate;
599 sam_freq = as_in_type_i_desc.tSamFreq[0];
600 memcpy(sam_freq, &rate, 3);
601
602 /* allocate instance-specific interface IDs, and patch descriptors */
603 status = usb_interface_id(c, f);
604 if (status < 0)
605 goto fail;
606 ac_interface_desc.bInterfaceNumber = status;
607 uac1->ac_intf = status;
608 uac1->ac_alt = 0;
609
610 status = usb_interface_id(c, f);
611 if (status < 0)
612 goto fail;
613 as_out_interface_alt_0_desc.bInterfaceNumber = status;
614 as_out_interface_alt_1_desc.bInterfaceNumber = status;
615 ac_header_desc.baInterfaceNr[0] = status;
616 uac1->as_out_intf = status;
617 uac1->as_out_alt = 0;
618
619 status = usb_interface_id(c, f);
620 if (status < 0)
621 goto fail;
622 as_in_interface_alt_0_desc.bInterfaceNumber = status;
623 as_in_interface_alt_1_desc.bInterfaceNumber = status;
624 ac_header_desc.baInterfaceNr[1] = status;
625 uac1->as_in_intf = status;
626 uac1->as_in_alt = 0;
627
628 audio->gadget = gadget;
629
630 status = -ENODEV;
631
632 /* allocate instance-specific endpoints */
633 ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
634 if (!ep)
635 goto fail;
636 audio->out_ep = ep;
637 audio->out_ep->desc = &as_out_ep_desc;
638
639 ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc);
640 if (!ep)
641 goto fail;
642 audio->in_ep = ep;
643 audio->in_ep->desc = &as_in_ep_desc;
644
645 /* copy descriptors, and track endpoint copies */
646 status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL,
647 NULL);
648 if (status)
649 goto fail;
650
651 audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize);
652 audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize);
653 audio->params.c_chmask = audio_opts->c_chmask;
654 audio->params.c_srate = audio_opts->c_srate;
655 audio->params.c_ssize = audio_opts->c_ssize;
656 audio->params.p_chmask = audio_opts->p_chmask;
657 audio->params.p_srate = audio_opts->p_srate;
658 audio->params.p_ssize = audio_opts->p_ssize;
659 audio->params.req_number = audio_opts->req_number;
660
661 status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget");
662 if (status)
663 goto err_card_register;
664
665 return 0;
666
667 err_card_register:
668 usb_free_all_descriptors(f);
669 fail:
670 return status;
671 }
672
673 /*-------------------------------------------------------------------------*/
674
to_f_uac1_opts(struct config_item * item)675 static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item)
676 {
677 return container_of(to_config_group(item), struct f_uac1_opts,
678 func_inst.group);
679 }
680
f_uac1_attr_release(struct config_item * item)681 static void f_uac1_attr_release(struct config_item *item)
682 {
683 struct f_uac1_opts *opts = to_f_uac1_opts(item);
684
685 usb_put_function_instance(&opts->func_inst);
686 }
687
688 static struct configfs_item_operations f_uac1_item_ops = {
689 .release = f_uac1_attr_release,
690 };
691
692 #define UAC1_ATTRIBUTE(name) \
693 static ssize_t f_uac1_opts_##name##_show( \
694 struct config_item *item, \
695 char *page) \
696 { \
697 struct f_uac1_opts *opts = to_f_uac1_opts(item); \
698 int result; \
699 \
700 mutex_lock(&opts->lock); \
701 result = sprintf(page, "%u\n", opts->name); \
702 mutex_unlock(&opts->lock); \
703 \
704 return result; \
705 } \
706 \
707 static ssize_t f_uac1_opts_##name##_store( \
708 struct config_item *item, \
709 const char *page, size_t len) \
710 { \
711 struct f_uac1_opts *opts = to_f_uac1_opts(item); \
712 int ret; \
713 u32 num; \
714 \
715 mutex_lock(&opts->lock); \
716 if (opts->refcnt) { \
717 ret = -EBUSY; \
718 goto end; \
719 } \
720 \
721 ret = kstrtou32(page, 0, &num); \
722 if (ret) \
723 goto end; \
724 \
725 opts->name = num; \
726 ret = len; \
727 \
728 end: \
729 mutex_unlock(&opts->lock); \
730 return ret; \
731 } \
732 \
733 CONFIGFS_ATTR(f_uac1_opts_, name)
734
735 UAC1_ATTRIBUTE(c_chmask);
736 UAC1_ATTRIBUTE(c_srate);
737 UAC1_ATTRIBUTE(c_ssize);
738 UAC1_ATTRIBUTE(p_chmask);
739 UAC1_ATTRIBUTE(p_srate);
740 UAC1_ATTRIBUTE(p_ssize);
741 UAC1_ATTRIBUTE(req_number);
742
743 static struct configfs_attribute *f_uac1_attrs[] = {
744 &f_uac1_opts_attr_c_chmask,
745 &f_uac1_opts_attr_c_srate,
746 &f_uac1_opts_attr_c_ssize,
747 &f_uac1_opts_attr_p_chmask,
748 &f_uac1_opts_attr_p_srate,
749 &f_uac1_opts_attr_p_ssize,
750 &f_uac1_opts_attr_req_number,
751 NULL,
752 };
753
754 static const struct config_item_type f_uac1_func_type = {
755 .ct_item_ops = &f_uac1_item_ops,
756 .ct_attrs = f_uac1_attrs,
757 .ct_owner = THIS_MODULE,
758 };
759
f_audio_free_inst(struct usb_function_instance * f)760 static void f_audio_free_inst(struct usb_function_instance *f)
761 {
762 struct f_uac1_opts *opts;
763
764 opts = container_of(f, struct f_uac1_opts, func_inst);
765 kfree(opts);
766 }
767
f_audio_alloc_inst(void)768 static struct usb_function_instance *f_audio_alloc_inst(void)
769 {
770 struct f_uac1_opts *opts;
771
772 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
773 if (!opts)
774 return ERR_PTR(-ENOMEM);
775
776 mutex_init(&opts->lock);
777 opts->func_inst.free_func_inst = f_audio_free_inst;
778
779 config_group_init_type_name(&opts->func_inst.group, "",
780 &f_uac1_func_type);
781
782 opts->c_chmask = UAC1_DEF_CCHMASK;
783 opts->c_srate = UAC1_DEF_CSRATE;
784 opts->c_ssize = UAC1_DEF_CSSIZE;
785 opts->p_chmask = UAC1_DEF_PCHMASK;
786 opts->p_srate = UAC1_DEF_PSRATE;
787 opts->p_ssize = UAC1_DEF_PSSIZE;
788 opts->req_number = UAC1_DEF_REQ_NUM;
789 return &opts->func_inst;
790 }
791
f_audio_free(struct usb_function * f)792 static void f_audio_free(struct usb_function *f)
793 {
794 struct g_audio *audio;
795 struct f_uac1_opts *opts;
796
797 audio = func_to_g_audio(f);
798 opts = container_of(f->fi, struct f_uac1_opts, func_inst);
799 kfree(audio);
800 mutex_lock(&opts->lock);
801 --opts->refcnt;
802 mutex_unlock(&opts->lock);
803 }
804
f_audio_unbind(struct usb_configuration * c,struct usb_function * f)805 static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
806 {
807 struct g_audio *audio = func_to_g_audio(f);
808
809 g_audio_cleanup(audio);
810 usb_free_all_descriptors(f);
811
812 audio->gadget = NULL;
813 }
814
f_audio_alloc(struct usb_function_instance * fi)815 static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
816 {
817 struct f_uac1 *uac1;
818 struct f_uac1_opts *opts;
819
820 /* allocate and initialize one new instance */
821 uac1 = kzalloc(sizeof(*uac1), GFP_KERNEL);
822 if (!uac1)
823 return ERR_PTR(-ENOMEM);
824
825 opts = container_of(fi, struct f_uac1_opts, func_inst);
826 mutex_lock(&opts->lock);
827 ++opts->refcnt;
828 mutex_unlock(&opts->lock);
829
830 uac1->g_audio.func.name = "uac1_func";
831 uac1->g_audio.func.bind = f_audio_bind;
832 uac1->g_audio.func.unbind = f_audio_unbind;
833 uac1->g_audio.func.set_alt = f_audio_set_alt;
834 uac1->g_audio.func.get_alt = f_audio_get_alt;
835 uac1->g_audio.func.setup = f_audio_setup;
836 uac1->g_audio.func.disable = f_audio_disable;
837 uac1->g_audio.func.free_func = f_audio_free;
838
839 return &uac1->g_audio.func;
840 }
841
842 DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc);
843 MODULE_LICENSE("GPL");
844 MODULE_AUTHOR("Ruslan Bilovol");
845