]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/usb/gadget/function/f_uac2.c
Merge tag 'configfs-for-4.15' of git://git.infradead.org/users/hch/configfs
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / gadget / function / f_uac2.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * f_uac2.c -- USB Audio Class 2.0 Function
4 *
5 * Copyright (C) 2011
6 * Yadwinder Singh (yadi.brar01@gmail.com)
7 * Jaswinder Singh (jaswinder.singh@linaro.org)
8 */
9
10 #include <linux/usb/audio.h>
11 #include <linux/usb/audio-v2.h>
12 #include <linux/module.h>
13
14 #include "u_audio.h"
15 #include "u_uac2.h"
16
17 /*
18 * The driver implements a simple UAC_2 topology.
19 * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
20 * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
21 * Capture and Playback sampling rates are independently
22 * controlled by two clock sources :
23 * CLK_5 := c_srate, and CLK_6 := p_srate
24 */
25 #define USB_OUT_IT_ID 1
26 #define IO_IN_IT_ID 2
27 #define IO_OUT_OT_ID 3
28 #define USB_IN_OT_ID 4
29 #define USB_OUT_CLK_ID 5
30 #define USB_IN_CLK_ID 6
31
32 #define CONTROL_ABSENT 0
33 #define CONTROL_RDONLY 1
34 #define CONTROL_RDWR 3
35
36 #define CLK_FREQ_CTRL 0
37 #define CLK_VLD_CTRL 2
38
39 #define COPY_CTRL 0
40 #define CONN_CTRL 2
41 #define OVRLD_CTRL 4
42 #define CLSTR_CTRL 6
43 #define UNFLW_CTRL 8
44 #define OVFLW_CTRL 10
45
46 struct f_uac2 {
47 struct g_audio g_audio;
48 u8 ac_intf, as_in_intf, as_out_intf;
49 u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */
50 };
51
52 static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
53 {
54 return container_of(f, struct f_uac2, g_audio.func);
55 }
56
57 static inline
58 struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
59 {
60 return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
61 }
62
63 /* --------- USB Function Interface ------------- */
64
65 enum {
66 STR_ASSOC,
67 STR_IF_CTRL,
68 STR_CLKSRC_IN,
69 STR_CLKSRC_OUT,
70 STR_USB_IT,
71 STR_IO_IT,
72 STR_USB_OT,
73 STR_IO_OT,
74 STR_AS_OUT_ALT0,
75 STR_AS_OUT_ALT1,
76 STR_AS_IN_ALT0,
77 STR_AS_IN_ALT1,
78 };
79
80 static char clksrc_in[8];
81 static char clksrc_out[8];
82
83 static struct usb_string strings_fn[] = {
84 [STR_ASSOC].s = "Source/Sink",
85 [STR_IF_CTRL].s = "Topology Control",
86 [STR_CLKSRC_IN].s = clksrc_in,
87 [STR_CLKSRC_OUT].s = clksrc_out,
88 [STR_USB_IT].s = "USBH Out",
89 [STR_IO_IT].s = "USBD Out",
90 [STR_USB_OT].s = "USBH In",
91 [STR_IO_OT].s = "USBD In",
92 [STR_AS_OUT_ALT0].s = "Playback Inactive",
93 [STR_AS_OUT_ALT1].s = "Playback Active",
94 [STR_AS_IN_ALT0].s = "Capture Inactive",
95 [STR_AS_IN_ALT1].s = "Capture Active",
96 { },
97 };
98
99 static struct usb_gadget_strings str_fn = {
100 .language = 0x0409, /* en-us */
101 .strings = strings_fn,
102 };
103
104 static struct usb_gadget_strings *fn_strings[] = {
105 &str_fn,
106 NULL,
107 };
108
109 static struct usb_interface_assoc_descriptor iad_desc = {
110 .bLength = sizeof iad_desc,
111 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
112
113 .bFirstInterface = 0,
114 .bInterfaceCount = 3,
115 .bFunctionClass = USB_CLASS_AUDIO,
116 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
117 .bFunctionProtocol = UAC_VERSION_2,
118 };
119
120 /* Audio Control Interface */
121 static struct usb_interface_descriptor std_ac_if_desc = {
122 .bLength = sizeof std_ac_if_desc,
123 .bDescriptorType = USB_DT_INTERFACE,
124
125 .bAlternateSetting = 0,
126 .bNumEndpoints = 0,
127 .bInterfaceClass = USB_CLASS_AUDIO,
128 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
129 .bInterfaceProtocol = UAC_VERSION_2,
130 };
131
132 /* Clock source for IN traffic */
133 static struct uac_clock_source_descriptor in_clk_src_desc = {
134 .bLength = sizeof in_clk_src_desc,
135 .bDescriptorType = USB_DT_CS_INTERFACE,
136
137 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
138 .bClockID = USB_IN_CLK_ID,
139 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
140 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
141 .bAssocTerminal = 0,
142 };
143
144 /* Clock source for OUT traffic */
145 static struct uac_clock_source_descriptor out_clk_src_desc = {
146 .bLength = sizeof out_clk_src_desc,
147 .bDescriptorType = USB_DT_CS_INTERFACE,
148
149 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
150 .bClockID = USB_OUT_CLK_ID,
151 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
152 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
153 .bAssocTerminal = 0,
154 };
155
156 /* Input Terminal for USB_OUT */
157 static struct uac2_input_terminal_descriptor usb_out_it_desc = {
158 .bLength = sizeof usb_out_it_desc,
159 .bDescriptorType = USB_DT_CS_INTERFACE,
160
161 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
162 .bTerminalID = USB_OUT_IT_ID,
163 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
164 .bAssocTerminal = 0,
165 .bCSourceID = USB_OUT_CLK_ID,
166 .iChannelNames = 0,
167 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
168 };
169
170 /* Input Terminal for I/O-In */
171 static struct uac2_input_terminal_descriptor io_in_it_desc = {
172 .bLength = sizeof io_in_it_desc,
173 .bDescriptorType = USB_DT_CS_INTERFACE,
174
175 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
176 .bTerminalID = IO_IN_IT_ID,
177 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
178 .bAssocTerminal = 0,
179 .bCSourceID = USB_IN_CLK_ID,
180 .iChannelNames = 0,
181 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
182 };
183
184 /* Ouput Terminal for USB_IN */
185 static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
186 .bLength = sizeof usb_in_ot_desc,
187 .bDescriptorType = USB_DT_CS_INTERFACE,
188
189 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
190 .bTerminalID = USB_IN_OT_ID,
191 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
192 .bAssocTerminal = 0,
193 .bSourceID = IO_IN_IT_ID,
194 .bCSourceID = USB_IN_CLK_ID,
195 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
196 };
197
198 /* Ouput Terminal for I/O-Out */
199 static struct uac2_output_terminal_descriptor io_out_ot_desc = {
200 .bLength = sizeof io_out_ot_desc,
201 .bDescriptorType = USB_DT_CS_INTERFACE,
202
203 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
204 .bTerminalID = IO_OUT_OT_ID,
205 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
206 .bAssocTerminal = 0,
207 .bSourceID = USB_OUT_IT_ID,
208 .bCSourceID = USB_OUT_CLK_ID,
209 .bmControls = cpu_to_le16(CONTROL_RDWR << COPY_CTRL),
210 };
211
212 static struct uac2_ac_header_descriptor ac_hdr_desc = {
213 .bLength = sizeof ac_hdr_desc,
214 .bDescriptorType = USB_DT_CS_INTERFACE,
215
216 .bDescriptorSubtype = UAC_MS_HEADER,
217 .bcdADC = cpu_to_le16(0x200),
218 .bCategory = UAC2_FUNCTION_IO_BOX,
219 .wTotalLength = cpu_to_le16(sizeof in_clk_src_desc
220 + sizeof out_clk_src_desc + sizeof usb_out_it_desc
221 + sizeof io_in_it_desc + sizeof usb_in_ot_desc
222 + sizeof io_out_ot_desc),
223 .bmControls = 0,
224 };
225
226 /* Audio Streaming OUT Interface - Alt0 */
227 static struct usb_interface_descriptor std_as_out_if0_desc = {
228 .bLength = sizeof std_as_out_if0_desc,
229 .bDescriptorType = USB_DT_INTERFACE,
230
231 .bAlternateSetting = 0,
232 .bNumEndpoints = 0,
233 .bInterfaceClass = USB_CLASS_AUDIO,
234 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
235 .bInterfaceProtocol = UAC_VERSION_2,
236 };
237
238 /* Audio Streaming OUT Interface - Alt1 */
239 static struct usb_interface_descriptor std_as_out_if1_desc = {
240 .bLength = sizeof std_as_out_if1_desc,
241 .bDescriptorType = USB_DT_INTERFACE,
242
243 .bAlternateSetting = 1,
244 .bNumEndpoints = 1,
245 .bInterfaceClass = USB_CLASS_AUDIO,
246 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
247 .bInterfaceProtocol = UAC_VERSION_2,
248 };
249
250 /* Audio Stream OUT Intface Desc */
251 static struct uac2_as_header_descriptor as_out_hdr_desc = {
252 .bLength = sizeof as_out_hdr_desc,
253 .bDescriptorType = USB_DT_CS_INTERFACE,
254
255 .bDescriptorSubtype = UAC_AS_GENERAL,
256 .bTerminalLink = USB_OUT_IT_ID,
257 .bmControls = 0,
258 .bFormatType = UAC_FORMAT_TYPE_I,
259 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
260 .iChannelNames = 0,
261 };
262
263 /* Audio USB_OUT Format */
264 static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
265 .bLength = sizeof as_out_fmt1_desc,
266 .bDescriptorType = USB_DT_CS_INTERFACE,
267 .bDescriptorSubtype = UAC_FORMAT_TYPE,
268 .bFormatType = UAC_FORMAT_TYPE_I,
269 };
270
271 /* STD AS ISO OUT Endpoint */
272 static struct usb_endpoint_descriptor fs_epout_desc = {
273 .bLength = USB_DT_ENDPOINT_SIZE,
274 .bDescriptorType = USB_DT_ENDPOINT,
275
276 .bEndpointAddress = USB_DIR_OUT,
277 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
278 .wMaxPacketSize = cpu_to_le16(1023),
279 .bInterval = 1,
280 };
281
282 static struct usb_endpoint_descriptor hs_epout_desc = {
283 .bLength = USB_DT_ENDPOINT_SIZE,
284 .bDescriptorType = USB_DT_ENDPOINT,
285
286 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
287 .wMaxPacketSize = cpu_to_le16(1024),
288 .bInterval = 4,
289 };
290
291 /* CS AS ISO OUT Endpoint */
292 static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
293 .bLength = sizeof as_iso_out_desc,
294 .bDescriptorType = USB_DT_CS_ENDPOINT,
295
296 .bDescriptorSubtype = UAC_EP_GENERAL,
297 .bmAttributes = 0,
298 .bmControls = 0,
299 .bLockDelayUnits = 0,
300 .wLockDelay = 0,
301 };
302
303 /* Audio Streaming IN Interface - Alt0 */
304 static struct usb_interface_descriptor std_as_in_if0_desc = {
305 .bLength = sizeof std_as_in_if0_desc,
306 .bDescriptorType = USB_DT_INTERFACE,
307
308 .bAlternateSetting = 0,
309 .bNumEndpoints = 0,
310 .bInterfaceClass = USB_CLASS_AUDIO,
311 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
312 .bInterfaceProtocol = UAC_VERSION_2,
313 };
314
315 /* Audio Streaming IN Interface - Alt1 */
316 static struct usb_interface_descriptor std_as_in_if1_desc = {
317 .bLength = sizeof std_as_in_if1_desc,
318 .bDescriptorType = USB_DT_INTERFACE,
319
320 .bAlternateSetting = 1,
321 .bNumEndpoints = 1,
322 .bInterfaceClass = USB_CLASS_AUDIO,
323 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
324 .bInterfaceProtocol = UAC_VERSION_2,
325 };
326
327 /* Audio Stream IN Intface Desc */
328 static struct uac2_as_header_descriptor as_in_hdr_desc = {
329 .bLength = sizeof as_in_hdr_desc,
330 .bDescriptorType = USB_DT_CS_INTERFACE,
331
332 .bDescriptorSubtype = UAC_AS_GENERAL,
333 .bTerminalLink = USB_IN_OT_ID,
334 .bmControls = 0,
335 .bFormatType = UAC_FORMAT_TYPE_I,
336 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
337 .iChannelNames = 0,
338 };
339
340 /* Audio USB_IN Format */
341 static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
342 .bLength = sizeof as_in_fmt1_desc,
343 .bDescriptorType = USB_DT_CS_INTERFACE,
344 .bDescriptorSubtype = UAC_FORMAT_TYPE,
345 .bFormatType = UAC_FORMAT_TYPE_I,
346 };
347
348 /* STD AS ISO IN Endpoint */
349 static struct usb_endpoint_descriptor fs_epin_desc = {
350 .bLength = USB_DT_ENDPOINT_SIZE,
351 .bDescriptorType = USB_DT_ENDPOINT,
352
353 .bEndpointAddress = USB_DIR_IN,
354 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
355 .wMaxPacketSize = cpu_to_le16(1023),
356 .bInterval = 1,
357 };
358
359 static struct usb_endpoint_descriptor hs_epin_desc = {
360 .bLength = USB_DT_ENDPOINT_SIZE,
361 .bDescriptorType = USB_DT_ENDPOINT,
362
363 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
364 .wMaxPacketSize = cpu_to_le16(1024),
365 .bInterval = 4,
366 };
367
368 /* CS AS ISO IN Endpoint */
369 static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
370 .bLength = sizeof as_iso_in_desc,
371 .bDescriptorType = USB_DT_CS_ENDPOINT,
372
373 .bDescriptorSubtype = UAC_EP_GENERAL,
374 .bmAttributes = 0,
375 .bmControls = 0,
376 .bLockDelayUnits = 0,
377 .wLockDelay = 0,
378 };
379
380 static struct usb_descriptor_header *fs_audio_desc[] = {
381 (struct usb_descriptor_header *)&iad_desc,
382 (struct usb_descriptor_header *)&std_ac_if_desc,
383
384 (struct usb_descriptor_header *)&ac_hdr_desc,
385 (struct usb_descriptor_header *)&in_clk_src_desc,
386 (struct usb_descriptor_header *)&out_clk_src_desc,
387 (struct usb_descriptor_header *)&usb_out_it_desc,
388 (struct usb_descriptor_header *)&io_in_it_desc,
389 (struct usb_descriptor_header *)&usb_in_ot_desc,
390 (struct usb_descriptor_header *)&io_out_ot_desc,
391
392 (struct usb_descriptor_header *)&std_as_out_if0_desc,
393 (struct usb_descriptor_header *)&std_as_out_if1_desc,
394
395 (struct usb_descriptor_header *)&as_out_hdr_desc,
396 (struct usb_descriptor_header *)&as_out_fmt1_desc,
397 (struct usb_descriptor_header *)&fs_epout_desc,
398 (struct usb_descriptor_header *)&as_iso_out_desc,
399
400 (struct usb_descriptor_header *)&std_as_in_if0_desc,
401 (struct usb_descriptor_header *)&std_as_in_if1_desc,
402
403 (struct usb_descriptor_header *)&as_in_hdr_desc,
404 (struct usb_descriptor_header *)&as_in_fmt1_desc,
405 (struct usb_descriptor_header *)&fs_epin_desc,
406 (struct usb_descriptor_header *)&as_iso_in_desc,
407 NULL,
408 };
409
410 static struct usb_descriptor_header *hs_audio_desc[] = {
411 (struct usb_descriptor_header *)&iad_desc,
412 (struct usb_descriptor_header *)&std_ac_if_desc,
413
414 (struct usb_descriptor_header *)&ac_hdr_desc,
415 (struct usb_descriptor_header *)&in_clk_src_desc,
416 (struct usb_descriptor_header *)&out_clk_src_desc,
417 (struct usb_descriptor_header *)&usb_out_it_desc,
418 (struct usb_descriptor_header *)&io_in_it_desc,
419 (struct usb_descriptor_header *)&usb_in_ot_desc,
420 (struct usb_descriptor_header *)&io_out_ot_desc,
421
422 (struct usb_descriptor_header *)&std_as_out_if0_desc,
423 (struct usb_descriptor_header *)&std_as_out_if1_desc,
424
425 (struct usb_descriptor_header *)&as_out_hdr_desc,
426 (struct usb_descriptor_header *)&as_out_fmt1_desc,
427 (struct usb_descriptor_header *)&hs_epout_desc,
428 (struct usb_descriptor_header *)&as_iso_out_desc,
429
430 (struct usb_descriptor_header *)&std_as_in_if0_desc,
431 (struct usb_descriptor_header *)&std_as_in_if1_desc,
432
433 (struct usb_descriptor_header *)&as_in_hdr_desc,
434 (struct usb_descriptor_header *)&as_in_fmt1_desc,
435 (struct usb_descriptor_header *)&hs_epin_desc,
436 (struct usb_descriptor_header *)&as_iso_in_desc,
437 NULL,
438 };
439
440 struct cntrl_cur_lay3 {
441 __u32 dCUR;
442 };
443
444 struct cntrl_range_lay3 {
445 __u16 wNumSubRanges;
446 __u32 dMIN;
447 __u32 dMAX;
448 __u32 dRES;
449 } __packed;
450
451 static void set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
452 struct usb_endpoint_descriptor *ep_desc,
453 unsigned int factor, bool is_playback)
454 {
455 int chmask, srate, ssize;
456 u16 max_packet_size;
457
458 if (is_playback) {
459 chmask = uac2_opts->p_chmask;
460 srate = uac2_opts->p_srate;
461 ssize = uac2_opts->p_ssize;
462 } else {
463 chmask = uac2_opts->c_chmask;
464 srate = uac2_opts->c_srate;
465 ssize = uac2_opts->c_ssize;
466 }
467
468 max_packet_size = num_channels(chmask) * ssize *
469 DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
470 ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_packet_size,
471 le16_to_cpu(ep_desc->wMaxPacketSize)));
472 }
473
474 static int
475 afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
476 {
477 struct f_uac2 *uac2 = func_to_uac2(fn);
478 struct g_audio *agdev = func_to_g_audio(fn);
479 struct usb_composite_dev *cdev = cfg->cdev;
480 struct usb_gadget *gadget = cdev->gadget;
481 struct device *dev = &gadget->dev;
482 struct f_uac2_opts *uac2_opts;
483 struct usb_string *us;
484 int ret;
485
486 uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
487
488 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
489 if (IS_ERR(us))
490 return PTR_ERR(us);
491 iad_desc.iFunction = us[STR_ASSOC].id;
492 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
493 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
494 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
495 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
496 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
497 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
498 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
499 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
500 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
501 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
502 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
503
504
505 /* Initialize the configurable parameters */
506 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
507 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
508 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
509 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
510 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
511 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
512 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
513 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
514 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
515 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
516 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
517 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
518
519 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
520 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
521
522 ret = usb_interface_id(cfg, fn);
523 if (ret < 0) {
524 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
525 return ret;
526 }
527 std_ac_if_desc.bInterfaceNumber = ret;
528 uac2->ac_intf = ret;
529 uac2->ac_alt = 0;
530
531 ret = usb_interface_id(cfg, fn);
532 if (ret < 0) {
533 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
534 return ret;
535 }
536 std_as_out_if0_desc.bInterfaceNumber = ret;
537 std_as_out_if1_desc.bInterfaceNumber = ret;
538 uac2->as_out_intf = ret;
539 uac2->as_out_alt = 0;
540
541 ret = usb_interface_id(cfg, fn);
542 if (ret < 0) {
543 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
544 return ret;
545 }
546 std_as_in_if0_desc.bInterfaceNumber = ret;
547 std_as_in_if1_desc.bInterfaceNumber = ret;
548 uac2->as_in_intf = ret;
549 uac2->as_in_alt = 0;
550
551 /* Calculate wMaxPacketSize according to audio bandwidth */
552 set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true);
553 set_ep_max_packet_size(uac2_opts, &fs_epout_desc, 1000, false);
554 set_ep_max_packet_size(uac2_opts, &hs_epin_desc, 8000, true);
555 set_ep_max_packet_size(uac2_opts, &hs_epout_desc, 8000, false);
556
557 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
558 if (!agdev->out_ep) {
559 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
560 return ret;
561 }
562
563 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
564 if (!agdev->in_ep) {
565 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
566 return ret;
567 }
568
569 agdev->in_ep_maxpsize = max_t(u16,
570 le16_to_cpu(fs_epin_desc.wMaxPacketSize),
571 le16_to_cpu(hs_epin_desc.wMaxPacketSize));
572 agdev->out_ep_maxpsize = max_t(u16,
573 le16_to_cpu(fs_epout_desc.wMaxPacketSize),
574 le16_to_cpu(hs_epout_desc.wMaxPacketSize));
575
576 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
577 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
578
579 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL,
580 NULL);
581 if (ret)
582 return ret;
583
584 agdev->gadget = gadget;
585
586 agdev->params.p_chmask = uac2_opts->p_chmask;
587 agdev->params.p_srate = uac2_opts->p_srate;
588 agdev->params.p_ssize = uac2_opts->p_ssize;
589 agdev->params.c_chmask = uac2_opts->c_chmask;
590 agdev->params.c_srate = uac2_opts->c_srate;
591 agdev->params.c_ssize = uac2_opts->c_ssize;
592 agdev->params.req_number = uac2_opts->req_number;
593 ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
594 if (ret)
595 goto err_free_descs;
596 return 0;
597
598 err_free_descs:
599 usb_free_all_descriptors(fn);
600 agdev->gadget = NULL;
601 return ret;
602 }
603
604 static int
605 afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
606 {
607 struct usb_composite_dev *cdev = fn->config->cdev;
608 struct f_uac2 *uac2 = func_to_uac2(fn);
609 struct usb_gadget *gadget = cdev->gadget;
610 struct device *dev = &gadget->dev;
611 int ret = 0;
612
613 /* No i/f has more than 2 alt settings */
614 if (alt > 1) {
615 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
616 return -EINVAL;
617 }
618
619 if (intf == uac2->ac_intf) {
620 /* Control I/f has only 1 AltSetting - 0 */
621 if (alt) {
622 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
623 return -EINVAL;
624 }
625 return 0;
626 }
627
628 if (intf == uac2->as_out_intf) {
629 uac2->as_out_alt = alt;
630
631 if (alt)
632 ret = u_audio_start_capture(&uac2->g_audio);
633 else
634 u_audio_stop_capture(&uac2->g_audio);
635 } else if (intf == uac2->as_in_intf) {
636 uac2->as_in_alt = alt;
637
638 if (alt)
639 ret = u_audio_start_playback(&uac2->g_audio);
640 else
641 u_audio_stop_playback(&uac2->g_audio);
642 } else {
643 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
644 return -EINVAL;
645 }
646
647 return ret;
648 }
649
650 static int
651 afunc_get_alt(struct usb_function *fn, unsigned intf)
652 {
653 struct f_uac2 *uac2 = func_to_uac2(fn);
654 struct g_audio *agdev = func_to_g_audio(fn);
655
656 if (intf == uac2->ac_intf)
657 return uac2->ac_alt;
658 else if (intf == uac2->as_out_intf)
659 return uac2->as_out_alt;
660 else if (intf == uac2->as_in_intf)
661 return uac2->as_in_alt;
662 else
663 dev_err(&agdev->gadget->dev,
664 "%s:%d Invalid Interface %d!\n",
665 __func__, __LINE__, intf);
666
667 return -EINVAL;
668 }
669
670 static void
671 afunc_disable(struct usb_function *fn)
672 {
673 struct f_uac2 *uac2 = func_to_uac2(fn);
674
675 uac2->as_in_alt = 0;
676 uac2->as_out_alt = 0;
677 u_audio_stop_capture(&uac2->g_audio);
678 u_audio_stop_playback(&uac2->g_audio);
679 }
680
681 static int
682 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
683 {
684 struct usb_request *req = fn->config->cdev->req;
685 struct g_audio *agdev = func_to_g_audio(fn);
686 struct f_uac2_opts *opts;
687 u16 w_length = le16_to_cpu(cr->wLength);
688 u16 w_index = le16_to_cpu(cr->wIndex);
689 u16 w_value = le16_to_cpu(cr->wValue);
690 u8 entity_id = (w_index >> 8) & 0xff;
691 u8 control_selector = w_value >> 8;
692 int value = -EOPNOTSUPP;
693 int p_srate, c_srate;
694
695 opts = g_audio_to_uac2_opts(agdev);
696 p_srate = opts->p_srate;
697 c_srate = opts->c_srate;
698
699 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
700 struct cntrl_cur_lay3 c;
701 memset(&c, 0, sizeof(struct cntrl_cur_lay3));
702
703 if (entity_id == USB_IN_CLK_ID)
704 c.dCUR = p_srate;
705 else if (entity_id == USB_OUT_CLK_ID)
706 c.dCUR = c_srate;
707
708 value = min_t(unsigned, w_length, sizeof c);
709 memcpy(req->buf, &c, value);
710 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
711 *(u8 *)req->buf = 1;
712 value = min_t(unsigned, w_length, 1);
713 } else {
714 dev_err(&agdev->gadget->dev,
715 "%s:%d control_selector=%d TODO!\n",
716 __func__, __LINE__, control_selector);
717 }
718
719 return value;
720 }
721
722 static int
723 in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
724 {
725 struct usb_request *req = fn->config->cdev->req;
726 struct g_audio *agdev = func_to_g_audio(fn);
727 struct f_uac2_opts *opts;
728 u16 w_length = le16_to_cpu(cr->wLength);
729 u16 w_index = le16_to_cpu(cr->wIndex);
730 u16 w_value = le16_to_cpu(cr->wValue);
731 u8 entity_id = (w_index >> 8) & 0xff;
732 u8 control_selector = w_value >> 8;
733 struct cntrl_range_lay3 r;
734 int value = -EOPNOTSUPP;
735 int p_srate, c_srate;
736
737 opts = g_audio_to_uac2_opts(agdev);
738 p_srate = opts->p_srate;
739 c_srate = opts->c_srate;
740
741 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
742 if (entity_id == USB_IN_CLK_ID)
743 r.dMIN = p_srate;
744 else if (entity_id == USB_OUT_CLK_ID)
745 r.dMIN = c_srate;
746 else
747 return -EOPNOTSUPP;
748
749 r.dMAX = r.dMIN;
750 r.dRES = 0;
751 r.wNumSubRanges = 1;
752
753 value = min_t(unsigned, w_length, sizeof r);
754 memcpy(req->buf, &r, value);
755 } else {
756 dev_err(&agdev->gadget->dev,
757 "%s:%d control_selector=%d TODO!\n",
758 __func__, __LINE__, control_selector);
759 }
760
761 return value;
762 }
763
764 static int
765 ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
766 {
767 if (cr->bRequest == UAC2_CS_CUR)
768 return in_rq_cur(fn, cr);
769 else if (cr->bRequest == UAC2_CS_RANGE)
770 return in_rq_range(fn, cr);
771 else
772 return -EOPNOTSUPP;
773 }
774
775 static int
776 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
777 {
778 u16 w_length = le16_to_cpu(cr->wLength);
779 u16 w_value = le16_to_cpu(cr->wValue);
780 u8 control_selector = w_value >> 8;
781
782 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
783 return w_length;
784
785 return -EOPNOTSUPP;
786 }
787
788 static int
789 setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
790 {
791 struct f_uac2 *uac2 = func_to_uac2(fn);
792 struct g_audio *agdev = func_to_g_audio(fn);
793 u16 w_index = le16_to_cpu(cr->wIndex);
794 u8 intf = w_index & 0xff;
795
796 if (intf != uac2->ac_intf) {
797 dev_err(&agdev->gadget->dev,
798 "%s:%d Error!\n", __func__, __LINE__);
799 return -EOPNOTSUPP;
800 }
801
802 if (cr->bRequestType & USB_DIR_IN)
803 return ac_rq_in(fn, cr);
804 else if (cr->bRequest == UAC2_CS_CUR)
805 return out_rq_cur(fn, cr);
806
807 return -EOPNOTSUPP;
808 }
809
810 static int
811 afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
812 {
813 struct usb_composite_dev *cdev = fn->config->cdev;
814 struct g_audio *agdev = func_to_g_audio(fn);
815 struct usb_request *req = cdev->req;
816 u16 w_length = le16_to_cpu(cr->wLength);
817 int value = -EOPNOTSUPP;
818
819 /* Only Class specific requests are supposed to reach here */
820 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
821 return -EOPNOTSUPP;
822
823 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
824 value = setup_rq_inf(fn, cr);
825 else
826 dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
827 __func__, __LINE__);
828
829 if (value >= 0) {
830 req->length = value;
831 req->zero = value < w_length;
832 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
833 if (value < 0) {
834 dev_err(&agdev->gadget->dev,
835 "%s:%d Error!\n", __func__, __LINE__);
836 req->status = 0;
837 }
838 }
839
840 return value;
841 }
842
843 static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
844 {
845 return container_of(to_config_group(item), struct f_uac2_opts,
846 func_inst.group);
847 }
848
849 static void f_uac2_attr_release(struct config_item *item)
850 {
851 struct f_uac2_opts *opts = to_f_uac2_opts(item);
852
853 usb_put_function_instance(&opts->func_inst);
854 }
855
856 static struct configfs_item_operations f_uac2_item_ops = {
857 .release = f_uac2_attr_release,
858 };
859
860 #define UAC2_ATTRIBUTE(name) \
861 static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
862 char *page) \
863 { \
864 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
865 int result; \
866 \
867 mutex_lock(&opts->lock); \
868 result = sprintf(page, "%u\n", opts->name); \
869 mutex_unlock(&opts->lock); \
870 \
871 return result; \
872 } \
873 \
874 static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
875 const char *page, size_t len) \
876 { \
877 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
878 int ret; \
879 u32 num; \
880 \
881 mutex_lock(&opts->lock); \
882 if (opts->refcnt) { \
883 ret = -EBUSY; \
884 goto end; \
885 } \
886 \
887 ret = kstrtou32(page, 0, &num); \
888 if (ret) \
889 goto end; \
890 \
891 opts->name = num; \
892 ret = len; \
893 \
894 end: \
895 mutex_unlock(&opts->lock); \
896 return ret; \
897 } \
898 \
899 CONFIGFS_ATTR(f_uac2_opts_, name)
900
901 UAC2_ATTRIBUTE(p_chmask);
902 UAC2_ATTRIBUTE(p_srate);
903 UAC2_ATTRIBUTE(p_ssize);
904 UAC2_ATTRIBUTE(c_chmask);
905 UAC2_ATTRIBUTE(c_srate);
906 UAC2_ATTRIBUTE(c_ssize);
907 UAC2_ATTRIBUTE(req_number);
908
909 static struct configfs_attribute *f_uac2_attrs[] = {
910 &f_uac2_opts_attr_p_chmask,
911 &f_uac2_opts_attr_p_srate,
912 &f_uac2_opts_attr_p_ssize,
913 &f_uac2_opts_attr_c_chmask,
914 &f_uac2_opts_attr_c_srate,
915 &f_uac2_opts_attr_c_ssize,
916 &f_uac2_opts_attr_req_number,
917 NULL,
918 };
919
920 static const struct config_item_type f_uac2_func_type = {
921 .ct_item_ops = &f_uac2_item_ops,
922 .ct_attrs = f_uac2_attrs,
923 .ct_owner = THIS_MODULE,
924 };
925
926 static void afunc_free_inst(struct usb_function_instance *f)
927 {
928 struct f_uac2_opts *opts;
929
930 opts = container_of(f, struct f_uac2_opts, func_inst);
931 kfree(opts);
932 }
933
934 static struct usb_function_instance *afunc_alloc_inst(void)
935 {
936 struct f_uac2_opts *opts;
937
938 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
939 if (!opts)
940 return ERR_PTR(-ENOMEM);
941
942 mutex_init(&opts->lock);
943 opts->func_inst.free_func_inst = afunc_free_inst;
944
945 config_group_init_type_name(&opts->func_inst.group, "",
946 &f_uac2_func_type);
947
948 opts->p_chmask = UAC2_DEF_PCHMASK;
949 opts->p_srate = UAC2_DEF_PSRATE;
950 opts->p_ssize = UAC2_DEF_PSSIZE;
951 opts->c_chmask = UAC2_DEF_CCHMASK;
952 opts->c_srate = UAC2_DEF_CSRATE;
953 opts->c_ssize = UAC2_DEF_CSSIZE;
954 opts->req_number = UAC2_DEF_REQ_NUM;
955 return &opts->func_inst;
956 }
957
958 static void afunc_free(struct usb_function *f)
959 {
960 struct g_audio *agdev;
961 struct f_uac2_opts *opts;
962
963 agdev = func_to_g_audio(f);
964 opts = container_of(f->fi, struct f_uac2_opts, func_inst);
965 kfree(agdev);
966 mutex_lock(&opts->lock);
967 --opts->refcnt;
968 mutex_unlock(&opts->lock);
969 }
970
971 static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
972 {
973 struct g_audio *agdev = func_to_g_audio(f);
974
975 g_audio_cleanup(agdev);
976 usb_free_all_descriptors(f);
977
978 agdev->gadget = NULL;
979 }
980
981 static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
982 {
983 struct f_uac2 *uac2;
984 struct f_uac2_opts *opts;
985
986 uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
987 if (uac2 == NULL)
988 return ERR_PTR(-ENOMEM);
989
990 opts = container_of(fi, struct f_uac2_opts, func_inst);
991 mutex_lock(&opts->lock);
992 ++opts->refcnt;
993 mutex_unlock(&opts->lock);
994
995 uac2->g_audio.func.name = "uac2_func";
996 uac2->g_audio.func.bind = afunc_bind;
997 uac2->g_audio.func.unbind = afunc_unbind;
998 uac2->g_audio.func.set_alt = afunc_set_alt;
999 uac2->g_audio.func.get_alt = afunc_get_alt;
1000 uac2->g_audio.func.disable = afunc_disable;
1001 uac2->g_audio.func.setup = afunc_setup;
1002 uac2->g_audio.func.free_func = afunc_free;
1003
1004 return &uac2->g_audio.func;
1005 }
1006
1007 DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1008 MODULE_LICENSE("GPL");
1009 MODULE_AUTHOR("Yadwinder Singh");
1010 MODULE_AUTHOR("Jaswinder Singh");