blob: 9082ce261e70a7fa00979abacf11d84c94940261 [file] [log] [blame]
Jassi Brar132fcb42012-02-02 22:01:34 +05301/*
2 * f_uac2.c -- USB Audio Class 2.0 Function
3 *
4 * Copyright (C) 2011
5 * Yadwinder Singh (yadi.brar01@gmail.com)
6 * Jaswinder Singh (jaswinder.singh@linaro.org)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/usb/audio.h>
15#include <linux/usb/audio-v2.h>
Jassi Brar132fcb42012-02-02 22:01:34 +053016#include <linux/module.h>
17
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030018#include "u_audio.h"
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +020019#include "u_uac2.h"
20
Jassi Brar132fcb42012-02-02 22:01:34 +053021/*
22 * The driver implements a simple UAC_2 topology.
23 * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture
24 * ALSA_Playback -> IT_2 -> OT_4 -> USB-IN
25 * Capture and Playback sampling rates are independently
26 * controlled by two clock sources :
27 * CLK_5 := c_srate, and CLK_6 := p_srate
28 */
29#define USB_OUT_IT_ID 1
30#define IO_IN_IT_ID 2
31#define IO_OUT_OT_ID 3
32#define USB_IN_OT_ID 4
33#define USB_OUT_CLK_ID 5
34#define USB_IN_CLK_ID 6
35
36#define CONTROL_ABSENT 0
37#define CONTROL_RDONLY 1
38#define CONTROL_RDWR 3
39
40#define CLK_FREQ_CTRL 0
41#define CLK_VLD_CTRL 2
42
43#define COPY_CTRL 0
44#define CONN_CTRL 2
45#define OVRLD_CTRL 4
46#define CLSTR_CTRL 6
47#define UNFLW_CTRL 8
48#define OVFLW_CTRL 10
49
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030050struct f_uac2 {
51 struct g_audio g_audio;
52 u8 ac_intf, as_in_intf, as_out_intf;
53 u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */
Jassi Brar132fcb42012-02-02 22:01:34 +053054};
55
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030056static inline struct f_uac2 *func_to_uac2(struct usb_function *f)
Jassi Brar132fcb42012-02-02 22:01:34 +053057{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030058 return container_of(f, struct f_uac2, g_audio.func);
Jassi Brar132fcb42012-02-02 22:01:34 +053059}
60
61static inline
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +030062struct f_uac2_opts *g_audio_to_uac2_opts(struct g_audio *agdev)
Daniel Mack254b3bf2014-08-27 19:09:05 +020063{
64 return container_of(agdev->func.fi, struct f_uac2_opts, func_inst);
65}
66
Jassi Brar132fcb42012-02-02 22:01:34 +053067/* --------- USB Function Interface ------------- */
68
69enum {
70 STR_ASSOC,
71 STR_IF_CTRL,
72 STR_CLKSRC_IN,
73 STR_CLKSRC_OUT,
74 STR_USB_IT,
75 STR_IO_IT,
76 STR_USB_OT,
77 STR_IO_OT,
78 STR_AS_OUT_ALT0,
79 STR_AS_OUT_ALT1,
80 STR_AS_IN_ALT0,
81 STR_AS_IN_ALT1,
82};
83
Jassi Brar132fcb42012-02-02 22:01:34 +053084static char clksrc_in[8];
85static char clksrc_out[8];
Jassi Brar132fcb42012-02-02 22:01:34 +053086
87static struct usb_string strings_fn[] = {
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +020088 [STR_ASSOC].s = "Source/Sink",
89 [STR_IF_CTRL].s = "Topology Control",
Jassi Brar132fcb42012-02-02 22:01:34 +053090 [STR_CLKSRC_IN].s = clksrc_in,
91 [STR_CLKSRC_OUT].s = clksrc_out,
Sebastian Andrzej Siewiorb36c3472012-10-22 22:15:09 +020092 [STR_USB_IT].s = "USBH Out",
93 [STR_IO_IT].s = "USBD Out",
94 [STR_USB_OT].s = "USBH In",
95 [STR_IO_OT].s = "USBD In",
96 [STR_AS_OUT_ALT0].s = "Playback Inactive",
97 [STR_AS_OUT_ALT1].s = "Playback Active",
98 [STR_AS_IN_ALT0].s = "Capture Inactive",
99 [STR_AS_IN_ALT1].s = "Capture Active",
Jassi Brar132fcb42012-02-02 22:01:34 +0530100 { },
101};
102
103static struct usb_gadget_strings str_fn = {
104 .language = 0x0409, /* en-us */
105 .strings = strings_fn,
106};
107
108static struct usb_gadget_strings *fn_strings[] = {
109 &str_fn,
110 NULL,
111};
112
Jassi Brar132fcb42012-02-02 22:01:34 +0530113static struct usb_interface_assoc_descriptor iad_desc = {
114 .bLength = sizeof iad_desc,
115 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
116
117 .bFirstInterface = 0,
118 .bInterfaceCount = 3,
119 .bFunctionClass = USB_CLASS_AUDIO,
120 .bFunctionSubClass = UAC2_FUNCTION_SUBCLASS_UNDEFINED,
121 .bFunctionProtocol = UAC_VERSION_2,
122};
123
124/* Audio Control Interface */
125static struct usb_interface_descriptor std_ac_if_desc = {
126 .bLength = sizeof std_ac_if_desc,
127 .bDescriptorType = USB_DT_INTERFACE,
128
129 .bAlternateSetting = 0,
130 .bNumEndpoints = 0,
131 .bInterfaceClass = USB_CLASS_AUDIO,
132 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
133 .bInterfaceProtocol = UAC_VERSION_2,
134};
135
136/* Clock source for IN traffic */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000137static struct uac_clock_source_descriptor in_clk_src_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530138 .bLength = sizeof in_clk_src_desc,
139 .bDescriptorType = USB_DT_CS_INTERFACE,
140
141 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
142 .bClockID = USB_IN_CLK_ID,
143 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
144 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
145 .bAssocTerminal = 0,
146};
147
148/* Clock source for OUT traffic */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000149static struct uac_clock_source_descriptor out_clk_src_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530150 .bLength = sizeof out_clk_src_desc,
151 .bDescriptorType = USB_DT_CS_INTERFACE,
152
153 .bDescriptorSubtype = UAC2_CLOCK_SOURCE,
154 .bClockID = USB_OUT_CLK_ID,
155 .bmAttributes = UAC_CLOCK_SOURCE_TYPE_INT_FIXED,
156 .bmControls = (CONTROL_RDONLY << CLK_FREQ_CTRL),
157 .bAssocTerminal = 0,
158};
159
160/* Input Terminal for USB_OUT */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000161static struct uac2_input_terminal_descriptor usb_out_it_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530162 .bLength = sizeof usb_out_it_desc,
163 .bDescriptorType = USB_DT_CS_INTERFACE,
164
165 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
166 .bTerminalID = USB_OUT_IT_ID,
167 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
168 .bAssocTerminal = 0,
169 .bCSourceID = USB_OUT_CLK_ID,
170 .iChannelNames = 0,
171 .bmControls = (CONTROL_RDWR << COPY_CTRL),
172};
173
174/* Input Terminal for I/O-In */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000175static struct uac2_input_terminal_descriptor io_in_it_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530176 .bLength = sizeof io_in_it_desc,
177 .bDescriptorType = USB_DT_CS_INTERFACE,
178
179 .bDescriptorSubtype = UAC_INPUT_TERMINAL,
180 .bTerminalID = IO_IN_IT_ID,
181 .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_UNDEFINED),
182 .bAssocTerminal = 0,
183 .bCSourceID = USB_IN_CLK_ID,
184 .iChannelNames = 0,
185 .bmControls = (CONTROL_RDWR << COPY_CTRL),
186};
187
188/* Ouput Terminal for USB_IN */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000189static struct uac2_output_terminal_descriptor usb_in_ot_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530190 .bLength = sizeof usb_in_ot_desc,
191 .bDescriptorType = USB_DT_CS_INTERFACE,
192
193 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
194 .bTerminalID = USB_IN_OT_ID,
195 .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING),
196 .bAssocTerminal = 0,
197 .bSourceID = IO_IN_IT_ID,
198 .bCSourceID = USB_IN_CLK_ID,
199 .bmControls = (CONTROL_RDWR << COPY_CTRL),
200};
201
202/* Ouput Terminal for I/O-Out */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000203static struct uac2_output_terminal_descriptor io_out_ot_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530204 .bLength = sizeof io_out_ot_desc,
205 .bDescriptorType = USB_DT_CS_INTERFACE,
206
207 .bDescriptorSubtype = UAC_OUTPUT_TERMINAL,
208 .bTerminalID = IO_OUT_OT_ID,
209 .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_UNDEFINED),
210 .bAssocTerminal = 0,
211 .bSourceID = USB_OUT_IT_ID,
212 .bCSourceID = USB_OUT_CLK_ID,
213 .bmControls = (CONTROL_RDWR << COPY_CTRL),
214};
215
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000216static struct uac2_ac_header_descriptor ac_hdr_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530217 .bLength = sizeof ac_hdr_desc,
218 .bDescriptorType = USB_DT_CS_INTERFACE,
219
220 .bDescriptorSubtype = UAC_MS_HEADER,
221 .bcdADC = cpu_to_le16(0x200),
222 .bCategory = UAC2_FUNCTION_IO_BOX,
223 .wTotalLength = sizeof in_clk_src_desc + sizeof out_clk_src_desc
224 + sizeof usb_out_it_desc + sizeof io_in_it_desc
225 + sizeof usb_in_ot_desc + sizeof io_out_ot_desc,
226 .bmControls = 0,
227};
228
229/* Audio Streaming OUT Interface - Alt0 */
230static struct usb_interface_descriptor std_as_out_if0_desc = {
231 .bLength = sizeof std_as_out_if0_desc,
232 .bDescriptorType = USB_DT_INTERFACE,
233
234 .bAlternateSetting = 0,
235 .bNumEndpoints = 0,
236 .bInterfaceClass = USB_CLASS_AUDIO,
237 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
238 .bInterfaceProtocol = UAC_VERSION_2,
239};
240
241/* Audio Streaming OUT Interface - Alt1 */
242static struct usb_interface_descriptor std_as_out_if1_desc = {
243 .bLength = sizeof std_as_out_if1_desc,
244 .bDescriptorType = USB_DT_INTERFACE,
245
246 .bAlternateSetting = 1,
247 .bNumEndpoints = 1,
248 .bInterfaceClass = USB_CLASS_AUDIO,
249 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
250 .bInterfaceProtocol = UAC_VERSION_2,
251};
252
253/* Audio Stream OUT Intface Desc */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000254static struct uac2_as_header_descriptor as_out_hdr_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530255 .bLength = sizeof as_out_hdr_desc,
256 .bDescriptorType = USB_DT_CS_INTERFACE,
257
258 .bDescriptorSubtype = UAC_AS_GENERAL,
259 .bTerminalLink = USB_OUT_IT_ID,
260 .bmControls = 0,
261 .bFormatType = UAC_FORMAT_TYPE_I,
262 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
263 .iChannelNames = 0,
264};
265
266/* Audio USB_OUT Format */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000267static struct uac2_format_type_i_descriptor as_out_fmt1_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530268 .bLength = sizeof as_out_fmt1_desc,
269 .bDescriptorType = USB_DT_CS_INTERFACE,
270 .bDescriptorSubtype = UAC_FORMAT_TYPE,
271 .bFormatType = UAC_FORMAT_TYPE_I,
272};
273
274/* STD AS ISO OUT Endpoint */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000275static struct usb_endpoint_descriptor fs_epout_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530276 .bLength = USB_DT_ENDPOINT_SIZE,
277 .bDescriptorType = USB_DT_ENDPOINT,
278
279 .bEndpointAddress = USB_DIR_OUT,
280 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
Felipe Balbi703a3032014-09-29 14:18:14 -0500281 .wMaxPacketSize = cpu_to_le16(1023),
Jassi Brar132fcb42012-02-02 22:01:34 +0530282 .bInterval = 1,
283};
284
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000285static struct usb_endpoint_descriptor hs_epout_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530286 .bLength = USB_DT_ENDPOINT_SIZE,
287 .bDescriptorType = USB_DT_ENDPOINT,
288
289 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
Felipe Balbi703a3032014-09-29 14:18:14 -0500290 .wMaxPacketSize = cpu_to_le16(1024),
Jassi Brar132fcb42012-02-02 22:01:34 +0530291 .bInterval = 4,
292};
293
294/* CS AS ISO OUT Endpoint */
295static struct uac2_iso_endpoint_descriptor as_iso_out_desc = {
296 .bLength = sizeof as_iso_out_desc,
297 .bDescriptorType = USB_DT_CS_ENDPOINT,
298
299 .bDescriptorSubtype = UAC_EP_GENERAL,
300 .bmAttributes = 0,
301 .bmControls = 0,
302 .bLockDelayUnits = 0,
303 .wLockDelay = 0,
304};
305
306/* Audio Streaming IN Interface - Alt0 */
307static struct usb_interface_descriptor std_as_in_if0_desc = {
308 .bLength = sizeof std_as_in_if0_desc,
309 .bDescriptorType = USB_DT_INTERFACE,
310
311 .bAlternateSetting = 0,
312 .bNumEndpoints = 0,
313 .bInterfaceClass = USB_CLASS_AUDIO,
314 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
315 .bInterfaceProtocol = UAC_VERSION_2,
316};
317
318/* Audio Streaming IN Interface - Alt1 */
319static struct usb_interface_descriptor std_as_in_if1_desc = {
320 .bLength = sizeof std_as_in_if1_desc,
321 .bDescriptorType = USB_DT_INTERFACE,
322
323 .bAlternateSetting = 1,
324 .bNumEndpoints = 1,
325 .bInterfaceClass = USB_CLASS_AUDIO,
326 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
327 .bInterfaceProtocol = UAC_VERSION_2,
328};
329
330/* Audio Stream IN Intface Desc */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000331static struct uac2_as_header_descriptor as_in_hdr_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530332 .bLength = sizeof as_in_hdr_desc,
333 .bDescriptorType = USB_DT_CS_INTERFACE,
334
335 .bDescriptorSubtype = UAC_AS_GENERAL,
336 .bTerminalLink = USB_IN_OT_ID,
337 .bmControls = 0,
338 .bFormatType = UAC_FORMAT_TYPE_I,
339 .bmFormats = cpu_to_le32(UAC_FORMAT_TYPE_I_PCM),
340 .iChannelNames = 0,
341};
342
343/* Audio USB_IN Format */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000344static struct uac2_format_type_i_descriptor as_in_fmt1_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530345 .bLength = sizeof as_in_fmt1_desc,
346 .bDescriptorType = USB_DT_CS_INTERFACE,
347 .bDescriptorSubtype = UAC_FORMAT_TYPE,
348 .bFormatType = UAC_FORMAT_TYPE_I,
349};
350
351/* STD AS ISO IN Endpoint */
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000352static struct usb_endpoint_descriptor fs_epin_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530353 .bLength = USB_DT_ENDPOINT_SIZE,
354 .bDescriptorType = USB_DT_ENDPOINT,
355
356 .bEndpointAddress = USB_DIR_IN,
357 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
Felipe Balbi703a3032014-09-29 14:18:14 -0500358 .wMaxPacketSize = cpu_to_le16(1023),
Jassi Brar132fcb42012-02-02 22:01:34 +0530359 .bInterval = 1,
360};
361
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000362static struct usb_endpoint_descriptor hs_epin_desc = {
Jassi Brar132fcb42012-02-02 22:01:34 +0530363 .bLength = USB_DT_ENDPOINT_SIZE,
364 .bDescriptorType = USB_DT_ENDPOINT,
365
366 .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC,
Felipe Balbi703a3032014-09-29 14:18:14 -0500367 .wMaxPacketSize = cpu_to_le16(1024),
Jassi Brar132fcb42012-02-02 22:01:34 +0530368 .bInterval = 4,
369};
370
371/* CS AS ISO IN Endpoint */
372static struct uac2_iso_endpoint_descriptor as_iso_in_desc = {
373 .bLength = sizeof as_iso_in_desc,
374 .bDescriptorType = USB_DT_CS_ENDPOINT,
375
376 .bDescriptorSubtype = UAC_EP_GENERAL,
377 .bmAttributes = 0,
378 .bmControls = 0,
379 .bLockDelayUnits = 0,
380 .wLockDelay = 0,
381};
382
383static struct usb_descriptor_header *fs_audio_desc[] = {
384 (struct usb_descriptor_header *)&iad_desc,
385 (struct usb_descriptor_header *)&std_ac_if_desc,
386
387 (struct usb_descriptor_header *)&ac_hdr_desc,
388 (struct usb_descriptor_header *)&in_clk_src_desc,
389 (struct usb_descriptor_header *)&out_clk_src_desc,
390 (struct usb_descriptor_header *)&usb_out_it_desc,
391 (struct usb_descriptor_header *)&io_in_it_desc,
392 (struct usb_descriptor_header *)&usb_in_ot_desc,
393 (struct usb_descriptor_header *)&io_out_ot_desc,
394
395 (struct usb_descriptor_header *)&std_as_out_if0_desc,
396 (struct usb_descriptor_header *)&std_as_out_if1_desc,
397
398 (struct usb_descriptor_header *)&as_out_hdr_desc,
399 (struct usb_descriptor_header *)&as_out_fmt1_desc,
400 (struct usb_descriptor_header *)&fs_epout_desc,
401 (struct usb_descriptor_header *)&as_iso_out_desc,
402
403 (struct usb_descriptor_header *)&std_as_in_if0_desc,
404 (struct usb_descriptor_header *)&std_as_in_if1_desc,
405
406 (struct usb_descriptor_header *)&as_in_hdr_desc,
407 (struct usb_descriptor_header *)&as_in_fmt1_desc,
408 (struct usb_descriptor_header *)&fs_epin_desc,
409 (struct usb_descriptor_header *)&as_iso_in_desc,
410 NULL,
411};
412
413static struct usb_descriptor_header *hs_audio_desc[] = {
414 (struct usb_descriptor_header *)&iad_desc,
415 (struct usb_descriptor_header *)&std_ac_if_desc,
416
417 (struct usb_descriptor_header *)&ac_hdr_desc,
418 (struct usb_descriptor_header *)&in_clk_src_desc,
419 (struct usb_descriptor_header *)&out_clk_src_desc,
420 (struct usb_descriptor_header *)&usb_out_it_desc,
421 (struct usb_descriptor_header *)&io_in_it_desc,
422 (struct usb_descriptor_header *)&usb_in_ot_desc,
423 (struct usb_descriptor_header *)&io_out_ot_desc,
424
425 (struct usb_descriptor_header *)&std_as_out_if0_desc,
426 (struct usb_descriptor_header *)&std_as_out_if1_desc,
427
428 (struct usb_descriptor_header *)&as_out_hdr_desc,
429 (struct usb_descriptor_header *)&as_out_fmt1_desc,
430 (struct usb_descriptor_header *)&hs_epout_desc,
431 (struct usb_descriptor_header *)&as_iso_out_desc,
432
433 (struct usb_descriptor_header *)&std_as_in_if0_desc,
434 (struct usb_descriptor_header *)&std_as_in_if1_desc,
435
436 (struct usb_descriptor_header *)&as_in_hdr_desc,
437 (struct usb_descriptor_header *)&as_in_fmt1_desc,
438 (struct usb_descriptor_header *)&hs_epin_desc,
439 (struct usb_descriptor_header *)&as_iso_in_desc,
440 NULL,
441};
442
443struct cntrl_cur_lay3 {
444 __u32 dCUR;
445};
446
447struct cntrl_range_lay3 {
448 __u16 wNumSubRanges;
449 __u32 dMIN;
450 __u32 dMAX;
451 __u32 dRES;
452} __packed;
453
Peter Chen913e4a92015-07-30 13:13:03 +0800454static void set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts,
455 struct usb_endpoint_descriptor *ep_desc,
456 unsigned int factor, bool is_playback)
457{
458 int chmask, srate, ssize;
459 u16 max_packet_size;
460
461 if (is_playback) {
462 chmask = uac2_opts->p_chmask;
463 srate = uac2_opts->p_srate;
464 ssize = uac2_opts->p_ssize;
465 } else {
466 chmask = uac2_opts->c_chmask;
467 srate = uac2_opts->c_srate;
468 ssize = uac2_opts->c_ssize;
469 }
470
471 max_packet_size = num_channels(chmask) * ssize *
472 DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1)));
Felipe Balbi0f4315a2015-08-04 11:02:45 -0500473 ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_packet_size,
Peter Chen913e4a92015-07-30 13:13:03 +0800474 le16_to_cpu(ep_desc->wMaxPacketSize)));
475}
476
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200477static int
Jassi Brar132fcb42012-02-02 22:01:34 +0530478afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
479{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300480 struct f_uac2 *uac2 = func_to_uac2(fn);
481 struct g_audio *agdev = func_to_g_audio(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +0530482 struct usb_composite_dev *cdev = cfg->cdev;
483 struct usb_gadget *gadget = cdev->gadget;
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300484 struct device *dev = &gadget->dev;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200485 struct f_uac2_opts *uac2_opts;
Andrzej Pietrasiewiczf4087572014-07-22 19:58:33 +0200486 struct usb_string *us;
Jassi Brar132fcb42012-02-02 22:01:34 +0530487 int ret;
488
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200489 uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200490
Andrzej Pietrasiewiczf4087572014-07-22 19:58:33 +0200491 us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn));
492 if (IS_ERR(us))
493 return PTR_ERR(us);
494 iad_desc.iFunction = us[STR_ASSOC].id;
495 std_ac_if_desc.iInterface = us[STR_IF_CTRL].id;
496 in_clk_src_desc.iClockSource = us[STR_CLKSRC_IN].id;
497 out_clk_src_desc.iClockSource = us[STR_CLKSRC_OUT].id;
498 usb_out_it_desc.iTerminal = us[STR_USB_IT].id;
499 io_in_it_desc.iTerminal = us[STR_IO_IT].id;
500 usb_in_ot_desc.iTerminal = us[STR_USB_OT].id;
501 io_out_ot_desc.iTerminal = us[STR_IO_OT].id;
502 std_as_out_if0_desc.iInterface = us[STR_AS_OUT_ALT0].id;
503 std_as_out_if1_desc.iInterface = us[STR_AS_OUT_ALT1].id;
504 std_as_in_if0_desc.iInterface = us[STR_AS_IN_ALT0].id;
505 std_as_in_if1_desc.iInterface = us[STR_AS_IN_ALT1].id;
506
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200507
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200508 /* Initialize the configurable parameters */
509 usb_out_it_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
510 usb_out_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
511 io_in_it_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
512 io_in_it_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
513 as_out_hdr_desc.bNrChannels = num_channels(uac2_opts->c_chmask);
514 as_out_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->c_chmask);
515 as_in_hdr_desc.bNrChannels = num_channels(uac2_opts->p_chmask);
516 as_in_hdr_desc.bmChannelConfig = cpu_to_le32(uac2_opts->p_chmask);
517 as_out_fmt1_desc.bSubslotSize = uac2_opts->c_ssize;
518 as_out_fmt1_desc.bBitResolution = uac2_opts->c_ssize * 8;
519 as_in_fmt1_desc.bSubslotSize = uac2_opts->p_ssize;
520 as_in_fmt1_desc.bBitResolution = uac2_opts->p_ssize * 8;
521
522 snprintf(clksrc_in, sizeof(clksrc_in), "%uHz", uac2_opts->p_srate);
523 snprintf(clksrc_out, sizeof(clksrc_out), "%uHz", uac2_opts->c_srate);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200524
Jassi Brar132fcb42012-02-02 22:01:34 +0530525 ret = usb_interface_id(cfg, fn);
526 if (ret < 0) {
Daniel Macka8147da2014-08-27 19:09:04 +0200527 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +0530528 return ret;
529 }
530 std_ac_if_desc.bInterfaceNumber = ret;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300531 uac2->ac_intf = ret;
532 uac2->ac_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530533
534 ret = usb_interface_id(cfg, fn);
535 if (ret < 0) {
Daniel Macka8147da2014-08-27 19:09:04 +0200536 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +0530537 return ret;
538 }
539 std_as_out_if0_desc.bInterfaceNumber = ret;
540 std_as_out_if1_desc.bInterfaceNumber = ret;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300541 uac2->as_out_intf = ret;
542 uac2->as_out_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530543
544 ret = usb_interface_id(cfg, fn);
545 if (ret < 0) {
Daniel Macka8147da2014-08-27 19:09:04 +0200546 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +0530547 return ret;
548 }
549 std_as_in_if0_desc.bInterfaceNumber = ret;
550 std_as_in_if1_desc.bInterfaceNumber = ret;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300551 uac2->as_in_intf = ret;
552 uac2->as_in_alt = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530553
Sekhar Nori0db56e42017-05-17 13:45:17 +0530554 /* Calculate wMaxPacketSize according to audio bandwidth */
555 set_ep_max_packet_size(uac2_opts, &fs_epin_desc, 1000, true);
556 set_ep_max_packet_size(uac2_opts, &fs_epout_desc, 1000, false);
557 set_ep_max_packet_size(uac2_opts, &hs_epin_desc, 8000, true);
558 set_ep_max_packet_size(uac2_opts, &hs_epout_desc, 8000, false);
559
Jassi Brar132fcb42012-02-02 22:01:34 +0530560 agdev->out_ep = usb_ep_autoconfig(gadget, &fs_epout_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200561 if (!agdev->out_ep) {
Daniel Macka8147da2014-08-27 19:09:04 +0200562 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Peter Chenf1d38612016-11-08 10:10:44 +0800563 return ret;
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200564 }
Jassi Brar132fcb42012-02-02 22:01:34 +0530565
566 agdev->in_ep = usb_ep_autoconfig(gadget, &fs_epin_desc);
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200567 if (!agdev->in_ep) {
Daniel Macka8147da2014-08-27 19:09:04 +0200568 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Peter Chenf1d38612016-11-08 10:10:44 +0800569 return ret;
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200570 }
Jassi Brar132fcb42012-02-02 22:01:34 +0530571
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300572 agdev->in_ep_maxpsize = max(fs_epin_desc.wMaxPacketSize,
573 hs_epin_desc.wMaxPacketSize);
574 agdev->out_ep_maxpsize = max(fs_epout_desc.wMaxPacketSize,
575 hs_epout_desc.wMaxPacketSize);
Jassi Brareb127cb2013-05-30 18:23:33 +0530576
Jassi Brar132fcb42012-02-02 22:01:34 +0530577 hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress;
Jassi Brar132fcb42012-02-02 22:01:34 +0530578 hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress;
Jassi Brar132fcb42012-02-02 22:01:34 +0530579
John Youneaef50c2016-02-05 17:06:07 -0800580 ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, NULL,
581 NULL);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200582 if (ret)
Peter Chenf1d38612016-11-08 10:10:44 +0800583 return ret;
Jassi Brar132fcb42012-02-02 22:01:34 +0530584
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300585 agdev->gadget = gadget;
586
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300587 agdev->params.p_chmask = uac2_opts->p_chmask;
588 agdev->params.p_srate = uac2_opts->p_srate;
589 agdev->params.p_ssize = uac2_opts->p_ssize;
590 agdev->params.c_chmask = uac2_opts->c_chmask;
591 agdev->params.c_srate = uac2_opts->c_srate;
592 agdev->params.c_ssize = uac2_opts->c_ssize;
593 agdev->params.req_number = uac2_opts->req_number;
594 ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200595 if (ret)
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300596 goto err_free_descs;
Sebastian Andrzej Siewior391aa852012-10-22 22:14:59 +0200597 return 0;
Pavitrakumar Managutted12a8722014-10-22 19:24:58 +0530598
Peter Chenf1d38612016-11-08 10:10:44 +0800599err_free_descs:
600 usb_free_all_descriptors(fn);
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300601 agdev->gadget = NULL;
Peter Chen88f950a2017-01-04 10:19:22 +0800602 return ret;
Jassi Brar132fcb42012-02-02 22:01:34 +0530603}
604
Jassi Brar132fcb42012-02-02 22:01:34 +0530605static int
606afunc_set_alt(struct usb_function *fn, unsigned intf, unsigned alt)
607{
608 struct usb_composite_dev *cdev = fn->config->cdev;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300609 struct f_uac2 *uac2 = func_to_uac2(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +0530610 struct usb_gadget *gadget = cdev->gadget;
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300611 struct device *dev = &gadget->dev;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300612 int ret = 0;
Jassi Brar132fcb42012-02-02 22:01:34 +0530613
614 /* No i/f has more than 2 alt settings */
615 if (alt > 1) {
Daniel Macka8147da2014-08-27 19:09:04 +0200616 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +0530617 return -EINVAL;
618 }
619
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300620 if (intf == uac2->ac_intf) {
Jassi Brar132fcb42012-02-02 22:01:34 +0530621 /* Control I/f has only 1 AltSetting - 0 */
622 if (alt) {
Daniel Macka8147da2014-08-27 19:09:04 +0200623 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +0530624 return -EINVAL;
625 }
626 return 0;
627 }
628
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300629 if (intf == uac2->as_out_intf) {
630 uac2->as_out_alt = alt;
Daniel Mack9bb87f12014-08-27 19:09:07 +0200631
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300632 if (alt)
633 ret = u_audio_start_capture(&uac2->g_audio);
Daniel Mack9bb87f12014-08-27 19:09:07 +0200634 else
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300635 u_audio_stop_capture(&uac2->g_audio);
636 } else if (intf == uac2->as_in_intf) {
637 uac2->as_in_alt = alt;
Daniel Mack9bb87f12014-08-27 19:09:07 +0200638
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300639 if (alt)
640 ret = u_audio_start_playback(&uac2->g_audio);
641 else
642 u_audio_stop_playback(&uac2->g_audio);
Jassi Brar132fcb42012-02-02 22:01:34 +0530643 } else {
Daniel Macka8147da2014-08-27 19:09:04 +0200644 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +0530645 return -EINVAL;
646 }
647
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300648 return ret;
Jassi Brar132fcb42012-02-02 22:01:34 +0530649}
650
651static int
652afunc_get_alt(struct usb_function *fn, unsigned intf)
653{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300654 struct f_uac2 *uac2 = func_to_uac2(fn);
655 struct g_audio *agdev = func_to_g_audio(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +0530656
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300657 if (intf == uac2->ac_intf)
658 return uac2->ac_alt;
659 else if (intf == uac2->as_out_intf)
660 return uac2->as_out_alt;
661 else if (intf == uac2->as_in_intf)
662 return uac2->as_in_alt;
Jassi Brar132fcb42012-02-02 22:01:34 +0530663 else
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300664 dev_err(&agdev->gadget->dev,
Jassi Brar132fcb42012-02-02 22:01:34 +0530665 "%s:%d Invalid Interface %d!\n",
666 __func__, __LINE__, intf);
667
668 return -EINVAL;
669}
670
671static void
672afunc_disable(struct usb_function *fn)
673{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300674 struct f_uac2 *uac2 = func_to_uac2(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +0530675
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300676 uac2->as_in_alt = 0;
677 uac2->as_out_alt = 0;
678 u_audio_stop_capture(&uac2->g_audio);
679 u_audio_stop_playback(&uac2->g_audio);
Jassi Brar132fcb42012-02-02 22:01:34 +0530680}
681
682static int
683in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
684{
685 struct usb_request *req = fn->config->cdev->req;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300686 struct g_audio *agdev = func_to_g_audio(fn);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200687 struct f_uac2_opts *opts;
Jassi Brar132fcb42012-02-02 22:01:34 +0530688 u16 w_length = le16_to_cpu(cr->wLength);
689 u16 w_index = le16_to_cpu(cr->wIndex);
690 u16 w_value = le16_to_cpu(cr->wValue);
691 u8 entity_id = (w_index >> 8) & 0xff;
692 u8 control_selector = w_value >> 8;
693 int value = -EOPNOTSUPP;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200694 int p_srate, c_srate;
695
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300696 opts = g_audio_to_uac2_opts(agdev);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200697 p_srate = opts->p_srate;
698 c_srate = opts->c_srate;
Jassi Brar132fcb42012-02-02 22:01:34 +0530699
700 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
701 struct cntrl_cur_lay3 c;
Heinrich Schuchardtffeee832016-05-08 23:20:59 +0200702 memset(&c, 0, sizeof(struct cntrl_cur_lay3));
Jassi Brar132fcb42012-02-02 22:01:34 +0530703
704 if (entity_id == USB_IN_CLK_ID)
705 c.dCUR = p_srate;
706 else if (entity_id == USB_OUT_CLK_ID)
707 c.dCUR = c_srate;
708
709 value = min_t(unsigned, w_length, sizeof c);
710 memcpy(req->buf, &c, value);
711 } else if (control_selector == UAC2_CS_CONTROL_CLOCK_VALID) {
712 *(u8 *)req->buf = 1;
713 value = min_t(unsigned, w_length, 1);
714 } else {
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300715 dev_err(&agdev->gadget->dev,
Jassi Brar132fcb42012-02-02 22:01:34 +0530716 "%s:%d control_selector=%d TODO!\n",
717 __func__, __LINE__, control_selector);
718 }
719
720 return value;
721}
722
723static int
724in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
725{
726 struct usb_request *req = fn->config->cdev->req;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300727 struct g_audio *agdev = func_to_g_audio(fn);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200728 struct f_uac2_opts *opts;
Jassi Brar132fcb42012-02-02 22:01:34 +0530729 u16 w_length = le16_to_cpu(cr->wLength);
730 u16 w_index = le16_to_cpu(cr->wIndex);
731 u16 w_value = le16_to_cpu(cr->wValue);
732 u8 entity_id = (w_index >> 8) & 0xff;
733 u8 control_selector = w_value >> 8;
734 struct cntrl_range_lay3 r;
735 int value = -EOPNOTSUPP;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200736 int p_srate, c_srate;
737
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300738 opts = g_audio_to_uac2_opts(agdev);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200739 p_srate = opts->p_srate;
740 c_srate = opts->c_srate;
Jassi Brar132fcb42012-02-02 22:01:34 +0530741
742 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ) {
743 if (entity_id == USB_IN_CLK_ID)
744 r.dMIN = p_srate;
745 else if (entity_id == USB_OUT_CLK_ID)
746 r.dMIN = c_srate;
747 else
748 return -EOPNOTSUPP;
749
750 r.dMAX = r.dMIN;
751 r.dRES = 0;
752 r.wNumSubRanges = 1;
753
754 value = min_t(unsigned, w_length, sizeof r);
755 memcpy(req->buf, &r, value);
756 } else {
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300757 dev_err(&agdev->gadget->dev,
Jassi Brar132fcb42012-02-02 22:01:34 +0530758 "%s:%d control_selector=%d TODO!\n",
759 __func__, __LINE__, control_selector);
760 }
761
762 return value;
763}
764
765static int
766ac_rq_in(struct usb_function *fn, const struct usb_ctrlrequest *cr)
767{
768 if (cr->bRequest == UAC2_CS_CUR)
769 return in_rq_cur(fn, cr);
770 else if (cr->bRequest == UAC2_CS_RANGE)
771 return in_rq_range(fn, cr);
772 else
773 return -EOPNOTSUPP;
774}
775
776static int
777out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
778{
779 u16 w_length = le16_to_cpu(cr->wLength);
780 u16 w_value = le16_to_cpu(cr->wValue);
781 u8 control_selector = w_value >> 8;
782
783 if (control_selector == UAC2_CS_CONTROL_SAM_FREQ)
784 return w_length;
785
786 return -EOPNOTSUPP;
787}
788
789static int
790setup_rq_inf(struct usb_function *fn, const struct usb_ctrlrequest *cr)
791{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300792 struct f_uac2 *uac2 = func_to_uac2(fn);
793 struct g_audio *agdev = func_to_g_audio(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +0530794 u16 w_index = le16_to_cpu(cr->wIndex);
795 u8 intf = w_index & 0xff;
796
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300797 if (intf != uac2->ac_intf) {
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300798 dev_err(&agdev->gadget->dev,
Jassi Brar132fcb42012-02-02 22:01:34 +0530799 "%s:%d Error!\n", __func__, __LINE__);
800 return -EOPNOTSUPP;
801 }
802
803 if (cr->bRequestType & USB_DIR_IN)
804 return ac_rq_in(fn, cr);
805 else if (cr->bRequest == UAC2_CS_CUR)
806 return out_rq_cur(fn, cr);
807
808 return -EOPNOTSUPP;
809}
810
811static int
812afunc_setup(struct usb_function *fn, const struct usb_ctrlrequest *cr)
813{
814 struct usb_composite_dev *cdev = fn->config->cdev;
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300815 struct g_audio *agdev = func_to_g_audio(fn);
Jassi Brar132fcb42012-02-02 22:01:34 +0530816 struct usb_request *req = cdev->req;
817 u16 w_length = le16_to_cpu(cr->wLength);
818 int value = -EOPNOTSUPP;
819
820 /* Only Class specific requests are supposed to reach here */
821 if ((cr->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
822 return -EOPNOTSUPP;
823
824 if ((cr->bRequestType & USB_RECIP_MASK) == USB_RECIP_INTERFACE)
825 value = setup_rq_inf(fn, cr);
826 else
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300827 dev_err(&agdev->gadget->dev, "%s:%d Error!\n",
828 __func__, __LINE__);
Jassi Brar132fcb42012-02-02 22:01:34 +0530829
830 if (value >= 0) {
831 req->length = value;
832 req->zero = value < w_length;
833 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
834 if (value < 0) {
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300835 dev_err(&agdev->gadget->dev,
Jassi Brar132fcb42012-02-02 22:01:34 +0530836 "%s:%d Error!\n", __func__, __LINE__);
837 req->status = 0;
838 }
839 }
840
841 return value;
842}
843
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200844static inline struct f_uac2_opts *to_f_uac2_opts(struct config_item *item)
845{
846 return container_of(to_config_group(item), struct f_uac2_opts,
847 func_inst.group);
848}
849
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200850static void f_uac2_attr_release(struct config_item *item)
851{
852 struct f_uac2_opts *opts = to_f_uac2_opts(item);
853
854 usb_put_function_instance(&opts->func_inst);
855}
856
857static struct configfs_item_operations f_uac2_item_ops = {
858 .release = f_uac2_attr_release,
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200859};
860
861#define UAC2_ATTRIBUTE(name) \
Christoph Hellwig495702b2015-10-03 15:32:49 +0200862static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200863 char *page) \
864{ \
Christoph Hellwig495702b2015-10-03 15:32:49 +0200865 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200866 int result; \
867 \
868 mutex_lock(&opts->lock); \
869 result = sprintf(page, "%u\n", opts->name); \
870 mutex_unlock(&opts->lock); \
871 \
872 return result; \
873} \
874 \
Christoph Hellwig495702b2015-10-03 15:32:49 +0200875static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200876 const char *page, size_t len) \
877{ \
Christoph Hellwig495702b2015-10-03 15:32:49 +0200878 struct f_uac2_opts *opts = to_f_uac2_opts(item); \
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200879 int ret; \
880 u32 num; \
881 \
882 mutex_lock(&opts->lock); \
883 if (opts->refcnt) { \
884 ret = -EBUSY; \
885 goto end; \
886 } \
887 \
888 ret = kstrtou32(page, 0, &num); \
889 if (ret) \
890 goto end; \
891 \
892 opts->name = num; \
893 ret = len; \
894 \
895end: \
896 mutex_unlock(&opts->lock); \
897 return ret; \
898} \
899 \
Christoph Hellwig495702b2015-10-03 15:32:49 +0200900CONFIGFS_ATTR(f_uac2_opts_, name)
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200901
902UAC2_ATTRIBUTE(p_chmask);
903UAC2_ATTRIBUTE(p_srate);
904UAC2_ATTRIBUTE(p_ssize);
905UAC2_ATTRIBUTE(c_chmask);
906UAC2_ATTRIBUTE(c_srate);
907UAC2_ATTRIBUTE(c_ssize);
Peter Chene92b9d42017-01-04 10:19:23 +0800908UAC2_ATTRIBUTE(req_number);
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200909
910static struct configfs_attribute *f_uac2_attrs[] = {
Christoph Hellwig495702b2015-10-03 15:32:49 +0200911 &f_uac2_opts_attr_p_chmask,
912 &f_uac2_opts_attr_p_srate,
913 &f_uac2_opts_attr_p_ssize,
914 &f_uac2_opts_attr_c_chmask,
915 &f_uac2_opts_attr_c_srate,
916 &f_uac2_opts_attr_c_ssize,
Peter Chene92b9d42017-01-04 10:19:23 +0800917 &f_uac2_opts_attr_req_number,
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200918 NULL,
919};
920
921static struct config_item_type f_uac2_func_type = {
922 .ct_item_ops = &f_uac2_item_ops,
923 .ct_attrs = f_uac2_attrs,
924 .ct_owner = THIS_MODULE,
925};
926
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200927static void afunc_free_inst(struct usb_function_instance *f)
928{
929 struct f_uac2_opts *opts;
930
931 opts = container_of(f, struct f_uac2_opts, func_inst);
932 kfree(opts);
933}
934
935static struct usb_function_instance *afunc_alloc_inst(void)
936{
937 struct f_uac2_opts *opts;
938
939 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
940 if (!opts)
941 return ERR_PTR(-ENOMEM);
942
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200943 mutex_init(&opts->lock);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200944 opts->func_inst.free_func_inst = afunc_free_inst;
945
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200946 config_group_init_type_name(&opts->func_inst.group, "",
947 &f_uac2_func_type);
948
949 opts->p_chmask = UAC2_DEF_PCHMASK;
950 opts->p_srate = UAC2_DEF_PSRATE;
951 opts->p_ssize = UAC2_DEF_PSSIZE;
952 opts->c_chmask = UAC2_DEF_CCHMASK;
953 opts->c_srate = UAC2_DEF_CSRATE;
954 opts->c_ssize = UAC2_DEF_CSSIZE;
Peter Chene92b9d42017-01-04 10:19:23 +0800955 opts->req_number = UAC2_DEF_REQ_NUM;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200956 return &opts->func_inst;
957}
958
959static void afunc_free(struct usb_function *f)
960{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300961 struct g_audio *agdev;
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200962 struct f_uac2_opts *opts;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200963
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300964 agdev = func_to_g_audio(f);
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200965 opts = container_of(f->fi, struct f_uac2_opts, func_inst);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200966 kfree(agdev);
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200967 mutex_lock(&opts->lock);
968 --opts->refcnt;
969 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200970}
971
972static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
973{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300974 struct g_audio *agdev = func_to_g_audio(f);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200975
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300976 g_audio_cleanup(agdev);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200977 usb_free_all_descriptors(f);
Ruslan Bilovol7158b572017-06-18 16:23:51 +0300978
979 agdev->gadget = NULL;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200980}
981
Lad, Prabhakaref16e7c2015-02-04 18:01:11 +0000982static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200983{
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300984 struct f_uac2 *uac2;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200985 struct f_uac2_opts *opts;
986
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300987 uac2 = kzalloc(sizeof(*uac2), GFP_KERNEL);
988 if (uac2 == NULL)
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200989 return ERR_PTR(-ENOMEM);
990
991 opts = container_of(fi, struct f_uac2_opts, func_inst);
Andrzej Pietrasiewicz3aeea3c2014-07-22 19:58:35 +0200992 mutex_lock(&opts->lock);
993 ++opts->refcnt;
994 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +0200995
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +0300996 uac2->g_audio.func.name = "uac2_func";
997 uac2->g_audio.func.bind = afunc_bind;
998 uac2->g_audio.func.unbind = afunc_unbind;
999 uac2->g_audio.func.set_alt = afunc_set_alt;
1000 uac2->g_audio.func.get_alt = afunc_get_alt;
1001 uac2->g_audio.func.disable = afunc_disable;
1002 uac2->g_audio.func.setup = afunc_setup;
1003 uac2->g_audio.func.free_func = afunc_free;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001004
Ruslan Bilovoleb9fecb2017-06-18 16:23:52 +03001005 return &uac2->g_audio.func;
Andrzej Pietrasiewiczf8f93d22014-07-22 19:58:30 +02001006}
1007
1008DECLARE_USB_FUNCTION_INIT(uac2, afunc_alloc_inst, afunc_alloc);
1009MODULE_LICENSE("GPL");
1010MODULE_AUTHOR("Yadwinder Singh");
1011MODULE_AUTHOR("Jaswinder Singh");