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