1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* |
3 | * f_uac1.c -- USB Audio Class 1.0 Function (using u_audio API) |
4 | * |
5 | * Copyright (C) 2016 Ruslan Bilovol <ruslan.bilovol@gmail.com> |
6 | * Copyright (C) 2021 Julian Scheel <julian@jusst.de> |
7 | * |
8 | * This driver doesn't expect any real Audio codec to be present |
9 | * on the device - the audio streams are simply sinked to and |
10 | * sourced from a virtual ALSA sound card created. |
11 | * |
12 | * This file is based on f_uac1.c which is |
13 | * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org> |
14 | * Copyright (C) 2008 Analog Devices, Inc |
15 | */ |
16 | |
17 | #include <linux/usb/audio.h> |
18 | #include <linux/module.h> |
19 | |
20 | #include "u_audio.h" |
21 | #include "u_uac1.h" |
22 | |
23 | /* UAC1 spec: 3.7.2.3 Audio Channel Cluster Format */ |
24 | #define UAC1_CHANNEL_MASK 0x0FFF |
25 | |
26 | #define USB_OUT_FU_ID (out_feature_unit_desc->bUnitID) |
27 | #define USB_IN_FU_ID (in_feature_unit_desc->bUnitID) |
28 | |
29 | #define EPIN_EN(_opts) ((_opts)->p_chmask != 0) |
30 | #define EPOUT_EN(_opts) ((_opts)->c_chmask != 0) |
31 | #define FUIN_EN(_opts) ((_opts)->p_mute_present \ |
32 | || (_opts)->p_volume_present) |
33 | #define FUOUT_EN(_opts) ((_opts)->c_mute_present \ |
34 | || (_opts)->c_volume_present) |
35 | |
36 | struct f_uac1 { |
37 | struct g_audio g_audio; |
38 | u8 ac_intf, as_in_intf, as_out_intf; |
39 | u8 ac_alt, as_in_alt, as_out_alt; /* needed for get_alt() */ |
40 | |
41 | struct usb_ctrlrequest setup_cr; /* will be used in data stage */ |
42 | |
43 | /* Interrupt IN endpoint of AC interface */ |
44 | struct usb_ep *int_ep; |
45 | atomic_t int_count; |
46 | int ctl_id; /* EP id */ |
47 | int c_srate; /* current capture srate */ |
48 | int p_srate; /* current playback prate */ |
49 | }; |
50 | |
51 | static inline struct f_uac1 *func_to_uac1(struct usb_function *f) |
52 | { |
53 | return container_of(f, struct f_uac1, g_audio.func); |
54 | } |
55 | |
56 | static inline struct f_uac1_opts *g_audio_to_uac1_opts(struct g_audio *audio) |
57 | { |
58 | return container_of(audio->func.fi, struct f_uac1_opts, func_inst); |
59 | } |
60 | |
61 | /* |
62 | * DESCRIPTORS ... most are static, but strings and full |
63 | * configuration descriptors are built on demand. |
64 | */ |
65 | |
66 | /* |
67 | * We have three interfaces - one AudioControl and two AudioStreaming |
68 | * |
69 | * The driver implements a simple UAC_1 topology. |
70 | * USB-OUT -> IT_1 -> OT_2 -> ALSA_Capture |
71 | * ALSA_Playback -> IT_3 -> OT_4 -> USB-IN |
72 | */ |
73 | |
74 | /* B.3.1 Standard AC Interface Descriptor */ |
75 | static struct usb_interface_descriptor ac_interface_desc = { |
76 | .bLength = USB_DT_INTERFACE_SIZE, |
77 | .bDescriptorType = USB_DT_INTERFACE, |
78 | /* .bNumEndpoints = DYNAMIC */ |
79 | .bInterfaceClass = USB_CLASS_AUDIO, |
80 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL, |
81 | }; |
82 | |
83 | /* B.3.2 Class-Specific AC Interface Descriptor */ |
84 | static struct uac1_ac_header_descriptor *; |
85 | |
86 | static 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 = DYNAMIC */ |
91 | .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), |
92 | .bAssocTerminal = 0, |
93 | .wChannelConfig = cpu_to_le16(0x3), |
94 | }; |
95 | |
96 | static struct uac1_output_terminal_descriptor io_out_ot_desc = { |
97 | .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, |
98 | .bDescriptorType = USB_DT_CS_INTERFACE, |
99 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, |
100 | /* .bTerminalID = DYNAMIC */ |
101 | .wTerminalType = cpu_to_le16(UAC_OUTPUT_TERMINAL_SPEAKER), |
102 | .bAssocTerminal = 0, |
103 | /* .bSourceID = DYNAMIC */ |
104 | }; |
105 | |
106 | static struct uac_input_terminal_descriptor io_in_it_desc = { |
107 | .bLength = UAC_DT_INPUT_TERMINAL_SIZE, |
108 | .bDescriptorType = USB_DT_CS_INTERFACE, |
109 | .bDescriptorSubtype = UAC_INPUT_TERMINAL, |
110 | /* .bTerminalID = DYNAMIC */ |
111 | .wTerminalType = cpu_to_le16(UAC_INPUT_TERMINAL_MICROPHONE), |
112 | .bAssocTerminal = 0, |
113 | .wChannelConfig = cpu_to_le16(0x3), |
114 | }; |
115 | |
116 | static struct uac1_output_terminal_descriptor usb_in_ot_desc = { |
117 | .bLength = UAC_DT_OUTPUT_TERMINAL_SIZE, |
118 | .bDescriptorType = USB_DT_CS_INTERFACE, |
119 | .bDescriptorSubtype = UAC_OUTPUT_TERMINAL, |
120 | /* .bTerminalID = DYNAMIC */ |
121 | .wTerminalType = cpu_to_le16(UAC_TERMINAL_STREAMING), |
122 | .bAssocTerminal = 0, |
123 | /* .bSourceID = DYNAMIC */ |
124 | }; |
125 | |
126 | static struct uac_feature_unit_descriptor *in_feature_unit_desc; |
127 | static struct uac_feature_unit_descriptor *out_feature_unit_desc; |
128 | |
129 | /* AC IN Interrupt Endpoint */ |
130 | static struct usb_endpoint_descriptor ac_int_ep_desc = { |
131 | .bLength = USB_DT_ENDPOINT_SIZE, |
132 | .bDescriptorType = USB_DT_ENDPOINT, |
133 | .bEndpointAddress = USB_DIR_IN, |
134 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
135 | .wMaxPacketSize = cpu_to_le16(2), |
136 | .bInterval = 4, |
137 | }; |
138 | |
139 | /* B.4.1 Standard AS Interface Descriptor */ |
140 | static struct usb_interface_descriptor as_out_interface_alt_0_desc = { |
141 | .bLength = USB_DT_INTERFACE_SIZE, |
142 | .bDescriptorType = USB_DT_INTERFACE, |
143 | .bAlternateSetting = 0, |
144 | .bNumEndpoints = 0, |
145 | .bInterfaceClass = USB_CLASS_AUDIO, |
146 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
147 | }; |
148 | |
149 | static struct usb_interface_descriptor as_out_interface_alt_1_desc = { |
150 | .bLength = USB_DT_INTERFACE_SIZE, |
151 | .bDescriptorType = USB_DT_INTERFACE, |
152 | .bAlternateSetting = 1, |
153 | .bNumEndpoints = 1, |
154 | .bInterfaceClass = USB_CLASS_AUDIO, |
155 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
156 | }; |
157 | |
158 | static struct usb_interface_descriptor as_in_interface_alt_0_desc = { |
159 | .bLength = USB_DT_INTERFACE_SIZE, |
160 | .bDescriptorType = USB_DT_INTERFACE, |
161 | .bAlternateSetting = 0, |
162 | .bNumEndpoints = 0, |
163 | .bInterfaceClass = USB_CLASS_AUDIO, |
164 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
165 | }; |
166 | |
167 | static struct usb_interface_descriptor as_in_interface_alt_1_desc = { |
168 | .bLength = USB_DT_INTERFACE_SIZE, |
169 | .bDescriptorType = USB_DT_INTERFACE, |
170 | .bAlternateSetting = 1, |
171 | .bNumEndpoints = 1, |
172 | .bInterfaceClass = USB_CLASS_AUDIO, |
173 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, |
174 | }; |
175 | |
176 | /* B.4.2 Class-Specific AS Interface Descriptor */ |
177 | static struct uac1_as_header_descriptor = { |
178 | .bLength = UAC_DT_AS_HEADER_SIZE, |
179 | .bDescriptorType = USB_DT_CS_INTERFACE, |
180 | .bDescriptorSubtype = UAC_AS_GENERAL, |
181 | /* .bTerminalLink = DYNAMIC */ |
182 | .bDelay = 1, |
183 | .wFormatTag = cpu_to_le16(UAC_FORMAT_TYPE_I_PCM), |
184 | }; |
185 | |
186 | static struct uac1_as_header_descriptor = { |
187 | .bLength = UAC_DT_AS_HEADER_SIZE, |
188 | .bDescriptorType = USB_DT_CS_INTERFACE, |
189 | .bDescriptorSubtype = UAC_AS_GENERAL, |
190 | /* .bTerminalLink = DYNAMIC */ |
191 | .bDelay = 1, |
192 | .wFormatTag = cpu_to_le16(UAC_FORMAT_TYPE_I_PCM), |
193 | }; |
194 | |
195 | DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(UAC_MAX_RATES); |
196 | #define uac_format_type_i_discrete_descriptor \ |
197 | uac_format_type_i_discrete_descriptor_##UAC_MAX_RATES |
198 | |
199 | static struct uac_format_type_i_discrete_descriptor as_out_type_i_desc = { |
200 | .bLength = 0, /* filled on rate setup */ |
201 | .bDescriptorType = USB_DT_CS_INTERFACE, |
202 | .bDescriptorSubtype = UAC_FORMAT_TYPE, |
203 | .bFormatType = UAC_FORMAT_TYPE_I, |
204 | .bSubframeSize = 2, |
205 | .bBitResolution = 16, |
206 | .bSamFreqType = 0, /* filled on rate setup */ |
207 | }; |
208 | |
209 | /* Standard ISO OUT Endpoint Descriptor */ |
210 | static struct usb_endpoint_descriptor as_out_ep_desc = { |
211 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
212 | .bDescriptorType = USB_DT_ENDPOINT, |
213 | .bEndpointAddress = USB_DIR_OUT, |
214 | .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE |
215 | | USB_ENDPOINT_XFER_ISOC, |
216 | .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), |
217 | .bInterval = 4, |
218 | }; |
219 | |
220 | /* Class-specific AS ISO OUT Endpoint Descriptor */ |
221 | static struct uac_iso_endpoint_descriptor as_iso_out_desc = { |
222 | .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, |
223 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
224 | .bDescriptorSubtype = UAC_EP_GENERAL, |
225 | .bmAttributes = 1, |
226 | .bLockDelayUnits = 1, |
227 | .wLockDelay = cpu_to_le16(1), |
228 | }; |
229 | |
230 | static struct uac_format_type_i_discrete_descriptor as_in_type_i_desc = { |
231 | .bLength = 0, /* filled on rate setup */ |
232 | .bDescriptorType = USB_DT_CS_INTERFACE, |
233 | .bDescriptorSubtype = UAC_FORMAT_TYPE, |
234 | .bFormatType = UAC_FORMAT_TYPE_I, |
235 | .bSubframeSize = 2, |
236 | .bBitResolution = 16, |
237 | .bSamFreqType = 0, /* filled on rate setup */ |
238 | }; |
239 | |
240 | /* Standard ISO OUT Endpoint Descriptor */ |
241 | static struct usb_endpoint_descriptor as_in_ep_desc = { |
242 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
243 | .bDescriptorType = USB_DT_ENDPOINT, |
244 | .bEndpointAddress = USB_DIR_IN, |
245 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
246 | | USB_ENDPOINT_XFER_ISOC, |
247 | .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), |
248 | .bInterval = 4, |
249 | }; |
250 | |
251 | /* Class-specific AS ISO OUT Endpoint Descriptor */ |
252 | static struct uac_iso_endpoint_descriptor as_iso_in_desc = { |
253 | .bLength = UAC_ISO_ENDPOINT_DESC_SIZE, |
254 | .bDescriptorType = USB_DT_CS_ENDPOINT, |
255 | .bDescriptorSubtype = UAC_EP_GENERAL, |
256 | .bmAttributes = 1, |
257 | .bLockDelayUnits = 0, |
258 | .wLockDelay = 0, |
259 | }; |
260 | |
261 | static struct usb_descriptor_header *f_audio_desc[] = { |
262 | (struct usb_descriptor_header *)&ac_interface_desc, |
263 | (struct usb_descriptor_header *)&ac_header_desc, |
264 | |
265 | (struct usb_descriptor_header *)&usb_out_it_desc, |
266 | (struct usb_descriptor_header *)&io_out_ot_desc, |
267 | (struct usb_descriptor_header *)&out_feature_unit_desc, |
268 | |
269 | (struct usb_descriptor_header *)&io_in_it_desc, |
270 | (struct usb_descriptor_header *)&usb_in_ot_desc, |
271 | (struct usb_descriptor_header *)&in_feature_unit_desc, |
272 | |
273 | (struct usb_descriptor_header *)&ac_int_ep_desc, |
274 | |
275 | (struct usb_descriptor_header *)&as_out_interface_alt_0_desc, |
276 | (struct usb_descriptor_header *)&as_out_interface_alt_1_desc, |
277 | (struct usb_descriptor_header *)&as_out_header_desc, |
278 | |
279 | (struct usb_descriptor_header *)&as_out_type_i_desc, |
280 | |
281 | (struct usb_descriptor_header *)&as_out_ep_desc, |
282 | (struct usb_descriptor_header *)&as_iso_out_desc, |
283 | |
284 | (struct usb_descriptor_header *)&as_in_interface_alt_0_desc, |
285 | (struct usb_descriptor_header *)&as_in_interface_alt_1_desc, |
286 | (struct usb_descriptor_header *)&as_in_header_desc, |
287 | |
288 | (struct usb_descriptor_header *)&as_in_type_i_desc, |
289 | |
290 | (struct usb_descriptor_header *)&as_in_ep_desc, |
291 | (struct usb_descriptor_header *)&as_iso_in_desc, |
292 | NULL, |
293 | }; |
294 | |
295 | /* Standard ISO OUT Endpoint Descriptor */ |
296 | static struct usb_endpoint_descriptor ss_as_out_ep_desc = { |
297 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
298 | .bDescriptorType = USB_DT_ENDPOINT, |
299 | .bEndpointAddress = USB_DIR_OUT, |
300 | .bmAttributes = USB_ENDPOINT_SYNC_ADAPTIVE |
301 | | USB_ENDPOINT_XFER_ISOC, |
302 | .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), |
303 | .bInterval = 4, |
304 | }; |
305 | |
306 | static struct usb_ss_ep_comp_descriptor ss_as_out_ep_desc_comp = { |
307 | .bLength = sizeof(ss_as_out_ep_desc_comp), |
308 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
309 | .bMaxBurst = 0, |
310 | .bmAttributes = 0, |
311 | /* wBytesPerInterval = DYNAMIC */ |
312 | }; |
313 | |
314 | /* Standard ISO OUT Endpoint Descriptor */ |
315 | static struct usb_endpoint_descriptor ss_as_in_ep_desc = { |
316 | .bLength = USB_DT_ENDPOINT_AUDIO_SIZE, |
317 | .bDescriptorType = USB_DT_ENDPOINT, |
318 | .bEndpointAddress = USB_DIR_IN, |
319 | .bmAttributes = USB_ENDPOINT_SYNC_ASYNC |
320 | | USB_ENDPOINT_XFER_ISOC, |
321 | .wMaxPacketSize = cpu_to_le16(UAC1_OUT_EP_MAX_PACKET_SIZE), |
322 | .bInterval = 4, |
323 | }; |
324 | |
325 | static struct usb_ss_ep_comp_descriptor ss_as_in_ep_desc_comp = { |
326 | .bLength = sizeof(ss_as_in_ep_desc_comp), |
327 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
328 | .bMaxBurst = 0, |
329 | .bmAttributes = 0, |
330 | /* wBytesPerInterval = DYNAMIC */ |
331 | }; |
332 | |
333 | static struct usb_descriptor_header *f_audio_ss_desc[] = { |
334 | (struct usb_descriptor_header *)&ac_interface_desc, |
335 | (struct usb_descriptor_header *)&ac_header_desc, |
336 | |
337 | (struct usb_descriptor_header *)&usb_out_it_desc, |
338 | (struct usb_descriptor_header *)&io_out_ot_desc, |
339 | (struct usb_descriptor_header *)&io_in_it_desc, |
340 | (struct usb_descriptor_header *)&usb_in_ot_desc, |
341 | |
342 | (struct usb_descriptor_header *)&as_out_interface_alt_0_desc, |
343 | (struct usb_descriptor_header *)&as_out_interface_alt_1_desc, |
344 | (struct usb_descriptor_header *)&as_out_header_desc, |
345 | |
346 | (struct usb_descriptor_header *)&as_out_type_i_desc, |
347 | |
348 | //(struct usb_descriptor_header *)&as_out_ep_desc, |
349 | (struct usb_descriptor_header *)&ss_as_out_ep_desc, |
350 | (struct usb_descriptor_header *)&ss_as_out_ep_desc_comp, |
351 | (struct usb_descriptor_header *)&as_iso_out_desc, |
352 | |
353 | (struct usb_descriptor_header *)&as_in_interface_alt_0_desc, |
354 | (struct usb_descriptor_header *)&as_in_interface_alt_1_desc, |
355 | (struct usb_descriptor_header *)&as_in_header_desc, |
356 | |
357 | (struct usb_descriptor_header *)&as_in_type_i_desc, |
358 | |
359 | //(struct usb_descriptor_header *)&as_in_ep_desc, |
360 | (struct usb_descriptor_header *)&ss_as_in_ep_desc, |
361 | (struct usb_descriptor_header *)&ss_as_in_ep_desc_comp, |
362 | (struct usb_descriptor_header *)&as_iso_in_desc, |
363 | NULL, |
364 | }; |
365 | |
366 | enum { |
367 | STR_AC_IF, |
368 | STR_USB_OUT_IT, |
369 | STR_USB_OUT_IT_CH_NAMES, |
370 | STR_IO_OUT_OT, |
371 | STR_IO_IN_IT, |
372 | STR_IO_IN_IT_CH_NAMES, |
373 | STR_USB_IN_OT, |
374 | STR_FU_IN, |
375 | STR_FU_OUT, |
376 | STR_AS_OUT_IF_ALT0, |
377 | STR_AS_OUT_IF_ALT1, |
378 | STR_AS_IN_IF_ALT0, |
379 | STR_AS_IN_IF_ALT1, |
380 | }; |
381 | |
382 | static struct usb_string strings_uac1[] = { |
383 | /* [STR_AC_IF].s = DYNAMIC, */ |
384 | [STR_USB_OUT_IT].s = "Playback Input terminal" , |
385 | [STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels" , |
386 | [STR_IO_OUT_OT].s = "Playback Output terminal" , |
387 | [STR_IO_IN_IT].s = "Capture Input terminal" , |
388 | [STR_IO_IN_IT_CH_NAMES].s = "Capture Channels" , |
389 | [STR_USB_IN_OT].s = "Capture Output terminal" , |
390 | [STR_FU_IN].s = "Capture Volume" , |
391 | [STR_FU_OUT].s = "Playback Volume" , |
392 | [STR_AS_OUT_IF_ALT0].s = "Playback Inactive" , |
393 | [STR_AS_OUT_IF_ALT1].s = "Playback Active" , |
394 | [STR_AS_IN_IF_ALT0].s = "Capture Inactive" , |
395 | [STR_AS_IN_IF_ALT1].s = "Capture Active" , |
396 | { }, |
397 | }; |
398 | |
399 | static struct usb_gadget_strings str_uac1 = { |
400 | .language = 0x0409, /* en-us */ |
401 | .strings = strings_uac1, |
402 | }; |
403 | |
404 | static struct usb_gadget_strings *uac1_strings[] = { |
405 | &str_uac1, |
406 | NULL, |
407 | }; |
408 | |
409 | /* |
410 | * This function is an ALSA sound card following USB Audio Class Spec 1.0. |
411 | */ |
412 | |
413 | static void uac_cs_attr_sample_rate(struct usb_ep *ep, struct usb_request *req) |
414 | { |
415 | struct usb_function *fn = ep->driver_data; |
416 | struct usb_composite_dev *cdev = fn->config->cdev; |
417 | struct g_audio *agdev = func_to_g_audio(f: fn); |
418 | struct f_uac1 *uac1 = func_to_uac1(f: fn); |
419 | u8 *buf = (u8 *)req->buf; |
420 | u32 val = 0; |
421 | |
422 | if (req->actual != 3) { |
423 | WARN(cdev, "Invalid data size for UAC_EP_CS_ATTR_SAMPLE_RATE.\n" ); |
424 | return; |
425 | } |
426 | |
427 | val = buf[0] | (buf[1] << 8) | (buf[2] << 16); |
428 | if (uac1->ctl_id == (USB_DIR_IN | 2)) { |
429 | uac1->p_srate = val; |
430 | u_audio_set_playback_srate(audio_dev: agdev, srate: uac1->p_srate); |
431 | } else if (uac1->ctl_id == (USB_DIR_OUT | 1)) { |
432 | uac1->c_srate = val; |
433 | u_audio_set_capture_srate(audio_dev: agdev, srate: uac1->c_srate); |
434 | } |
435 | } |
436 | |
437 | static void audio_notify_complete(struct usb_ep *_ep, struct usb_request *req) |
438 | { |
439 | struct g_audio *audio = req->context; |
440 | struct f_uac1 *uac1 = func_to_uac1(f: &audio->func); |
441 | |
442 | atomic_dec(v: &uac1->int_count); |
443 | kfree(objp: req->buf); |
444 | usb_ep_free_request(ep: _ep, req); |
445 | } |
446 | |
447 | static int audio_notify(struct g_audio *audio, int unit_id, int cs) |
448 | { |
449 | struct f_uac1 *uac1 = func_to_uac1(f: &audio->func); |
450 | struct usb_request *req; |
451 | struct uac1_status_word *msg; |
452 | int ret; |
453 | |
454 | if (!uac1->int_ep->enabled) |
455 | return 0; |
456 | |
457 | if (atomic_inc_return(v: &uac1->int_count) > UAC1_DEF_INT_REQ_NUM) { |
458 | atomic_dec(v: &uac1->int_count); |
459 | return 0; |
460 | } |
461 | |
462 | req = usb_ep_alloc_request(ep: uac1->int_ep, GFP_ATOMIC); |
463 | if (req == NULL) { |
464 | ret = -ENOMEM; |
465 | goto err_dec_int_count; |
466 | } |
467 | |
468 | msg = kmalloc(size: sizeof(*msg), GFP_ATOMIC); |
469 | if (msg == NULL) { |
470 | ret = -ENOMEM; |
471 | goto err_free_request; |
472 | } |
473 | |
474 | msg->bStatusType = UAC1_STATUS_TYPE_IRQ_PENDING |
475 | | UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF; |
476 | msg->bOriginator = unit_id; |
477 | |
478 | req->length = sizeof(*msg); |
479 | req->buf = msg; |
480 | req->context = audio; |
481 | req->complete = audio_notify_complete; |
482 | |
483 | ret = usb_ep_queue(ep: uac1->int_ep, req, GFP_ATOMIC); |
484 | |
485 | if (ret) |
486 | goto err_free_msg; |
487 | |
488 | return 0; |
489 | |
490 | err_free_msg: |
491 | kfree(objp: msg); |
492 | err_free_request: |
493 | usb_ep_free_request(ep: uac1->int_ep, req); |
494 | err_dec_int_count: |
495 | atomic_dec(v: &uac1->int_count); |
496 | |
497 | return ret; |
498 | } |
499 | |
500 | static int |
501 | in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
502 | { |
503 | struct usb_request *req = fn->config->cdev->req; |
504 | struct g_audio *audio = func_to_g_audio(f: fn); |
505 | struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); |
506 | u16 w_length = le16_to_cpu(cr->wLength); |
507 | u16 w_index = le16_to_cpu(cr->wIndex); |
508 | u16 w_value = le16_to_cpu(cr->wValue); |
509 | u8 entity_id = (w_index >> 8) & 0xff; |
510 | u8 control_selector = w_value >> 8; |
511 | int value = -EOPNOTSUPP; |
512 | |
513 | if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || |
514 | (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { |
515 | unsigned int is_playback = 0; |
516 | |
517 | if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) |
518 | is_playback = 1; |
519 | |
520 | if (control_selector == UAC_FU_MUTE) { |
521 | unsigned int mute; |
522 | |
523 | u_audio_get_mute(g_audio: audio, playback: is_playback, val: &mute); |
524 | |
525 | *(u8 *)req->buf = mute; |
526 | value = min_t(unsigned int, w_length, 1); |
527 | } else if (control_selector == UAC_FU_VOLUME) { |
528 | __le16 c; |
529 | s16 volume; |
530 | |
531 | u_audio_get_volume(g_audio: audio, playback: is_playback, val: &volume); |
532 | |
533 | c = cpu_to_le16(volume); |
534 | |
535 | value = min_t(unsigned int, w_length, sizeof(c)); |
536 | memcpy(req->buf, &c, value); |
537 | } else { |
538 | dev_err(&audio->gadget->dev, |
539 | "%s:%d control_selector=%d TODO!\n" , |
540 | __func__, __LINE__, control_selector); |
541 | } |
542 | } else { |
543 | dev_err(&audio->gadget->dev, |
544 | "%s:%d entity_id=%d control_selector=%d TODO!\n" , |
545 | __func__, __LINE__, entity_id, control_selector); |
546 | } |
547 | |
548 | return value; |
549 | } |
550 | |
551 | static int |
552 | in_rq_min(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
553 | { |
554 | struct usb_request *req = fn->config->cdev->req; |
555 | struct g_audio *audio = func_to_g_audio(f: fn); |
556 | struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); |
557 | u16 w_length = le16_to_cpu(cr->wLength); |
558 | u16 w_index = le16_to_cpu(cr->wIndex); |
559 | u16 w_value = le16_to_cpu(cr->wValue); |
560 | u8 entity_id = (w_index >> 8) & 0xff; |
561 | u8 control_selector = w_value >> 8; |
562 | int value = -EOPNOTSUPP; |
563 | |
564 | if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || |
565 | (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { |
566 | unsigned int is_playback = 0; |
567 | |
568 | if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) |
569 | is_playback = 1; |
570 | |
571 | if (control_selector == UAC_FU_VOLUME) { |
572 | __le16 r; |
573 | s16 min_db; |
574 | |
575 | if (is_playback) |
576 | min_db = opts->p_volume_min; |
577 | else |
578 | min_db = opts->c_volume_min; |
579 | |
580 | r = cpu_to_le16(min_db); |
581 | |
582 | value = min_t(unsigned int, w_length, sizeof(r)); |
583 | memcpy(req->buf, &r, value); |
584 | } else { |
585 | dev_err(&audio->gadget->dev, |
586 | "%s:%d control_selector=%d TODO!\n" , |
587 | __func__, __LINE__, control_selector); |
588 | } |
589 | } else { |
590 | dev_err(&audio->gadget->dev, |
591 | "%s:%d entity_id=%d control_selector=%d TODO!\n" , |
592 | __func__, __LINE__, entity_id, control_selector); |
593 | } |
594 | |
595 | return value; |
596 | } |
597 | |
598 | static int |
599 | in_rq_max(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
600 | { |
601 | struct usb_request *req = fn->config->cdev->req; |
602 | struct g_audio *audio = func_to_g_audio(f: fn); |
603 | struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); |
604 | u16 w_length = le16_to_cpu(cr->wLength); |
605 | u16 w_index = le16_to_cpu(cr->wIndex); |
606 | u16 w_value = le16_to_cpu(cr->wValue); |
607 | u8 entity_id = (w_index >> 8) & 0xff; |
608 | u8 control_selector = w_value >> 8; |
609 | int value = -EOPNOTSUPP; |
610 | |
611 | if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || |
612 | (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { |
613 | unsigned int is_playback = 0; |
614 | |
615 | if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) |
616 | is_playback = 1; |
617 | |
618 | if (control_selector == UAC_FU_VOLUME) { |
619 | __le16 r; |
620 | s16 max_db; |
621 | |
622 | if (is_playback) |
623 | max_db = opts->p_volume_max; |
624 | else |
625 | max_db = opts->c_volume_max; |
626 | |
627 | r = cpu_to_le16(max_db); |
628 | |
629 | value = min_t(unsigned int, w_length, sizeof(r)); |
630 | memcpy(req->buf, &r, value); |
631 | } else { |
632 | dev_err(&audio->gadget->dev, |
633 | "%s:%d control_selector=%d TODO!\n" , |
634 | __func__, __LINE__, control_selector); |
635 | } |
636 | } else { |
637 | dev_err(&audio->gadget->dev, |
638 | "%s:%d entity_id=%d control_selector=%d TODO!\n" , |
639 | __func__, __LINE__, entity_id, control_selector); |
640 | } |
641 | |
642 | return value; |
643 | } |
644 | |
645 | static int |
646 | in_rq_res(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
647 | { |
648 | struct usb_request *req = fn->config->cdev->req; |
649 | struct g_audio *audio = func_to_g_audio(f: fn); |
650 | struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); |
651 | u16 w_length = le16_to_cpu(cr->wLength); |
652 | u16 w_index = le16_to_cpu(cr->wIndex); |
653 | u16 w_value = le16_to_cpu(cr->wValue); |
654 | u8 entity_id = (w_index >> 8) & 0xff; |
655 | u8 control_selector = w_value >> 8; |
656 | int value = -EOPNOTSUPP; |
657 | |
658 | if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || |
659 | (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { |
660 | unsigned int is_playback = 0; |
661 | |
662 | if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) |
663 | is_playback = 1; |
664 | |
665 | if (control_selector == UAC_FU_VOLUME) { |
666 | __le16 r; |
667 | s16 res_db; |
668 | |
669 | if (is_playback) |
670 | res_db = opts->p_volume_res; |
671 | else |
672 | res_db = opts->c_volume_res; |
673 | |
674 | r = cpu_to_le16(res_db); |
675 | |
676 | value = min_t(unsigned int, w_length, sizeof(r)); |
677 | memcpy(req->buf, &r, value); |
678 | } else { |
679 | dev_err(&audio->gadget->dev, |
680 | "%s:%d control_selector=%d TODO!\n" , |
681 | __func__, __LINE__, control_selector); |
682 | } |
683 | } else { |
684 | dev_err(&audio->gadget->dev, |
685 | "%s:%d entity_id=%d control_selector=%d TODO!\n" , |
686 | __func__, __LINE__, entity_id, control_selector); |
687 | } |
688 | |
689 | return value; |
690 | } |
691 | |
692 | static void |
693 | out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req) |
694 | { |
695 | struct g_audio *audio = req->context; |
696 | struct usb_composite_dev *cdev = audio->func.config->cdev; |
697 | struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); |
698 | struct f_uac1 *uac1 = func_to_uac1(f: &audio->func); |
699 | struct usb_ctrlrequest *cr = &uac1->setup_cr; |
700 | u16 w_index = le16_to_cpu(cr->wIndex); |
701 | u16 w_value = le16_to_cpu(cr->wValue); |
702 | u8 entity_id = (w_index >> 8) & 0xff; |
703 | u8 control_selector = w_value >> 8; |
704 | |
705 | if (req->status != 0) { |
706 | dev_dbg(&cdev->gadget->dev, "completion err %d\n" , req->status); |
707 | return; |
708 | } |
709 | |
710 | if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || |
711 | (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { |
712 | unsigned int is_playback = 0; |
713 | |
714 | if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) |
715 | is_playback = 1; |
716 | |
717 | if (control_selector == UAC_FU_MUTE) { |
718 | u8 mute = *(u8 *)req->buf; |
719 | |
720 | u_audio_set_mute(g_audio: audio, playback: is_playback, val: mute); |
721 | |
722 | return; |
723 | } else if (control_selector == UAC_FU_VOLUME) { |
724 | __le16 *c = req->buf; |
725 | s16 volume; |
726 | |
727 | volume = le16_to_cpu(*c); |
728 | u_audio_set_volume(g_audio: audio, playback: is_playback, val: volume); |
729 | |
730 | return; |
731 | } else { |
732 | dev_err(&audio->gadget->dev, |
733 | "%s:%d control_selector=%d TODO!\n" , |
734 | __func__, __LINE__, control_selector); |
735 | usb_ep_set_halt(ep); |
736 | } |
737 | } else { |
738 | dev_err(&audio->gadget->dev, |
739 | "%s:%d entity_id=%d control_selector=%d TODO!\n" , |
740 | __func__, __LINE__, entity_id, control_selector); |
741 | usb_ep_set_halt(ep); |
742 | |
743 | } |
744 | } |
745 | |
746 | static int |
747 | out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr) |
748 | { |
749 | struct usb_request *req = fn->config->cdev->req; |
750 | struct g_audio *audio = func_to_g_audio(f: fn); |
751 | struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); |
752 | struct f_uac1 *uac1 = func_to_uac1(f: &audio->func); |
753 | u16 w_length = le16_to_cpu(cr->wLength); |
754 | u16 w_index = le16_to_cpu(cr->wIndex); |
755 | u16 w_value = le16_to_cpu(cr->wValue); |
756 | u8 entity_id = (w_index >> 8) & 0xff; |
757 | u8 control_selector = w_value >> 8; |
758 | |
759 | if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) || |
760 | (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) { |
761 | memcpy(&uac1->setup_cr, cr, sizeof(*cr)); |
762 | req->context = audio; |
763 | req->complete = out_rq_cur_complete; |
764 | |
765 | return w_length; |
766 | } else { |
767 | dev_err(&audio->gadget->dev, |
768 | "%s:%d entity_id=%d control_selector=%d TODO!\n" , |
769 | __func__, __LINE__, entity_id, control_selector); |
770 | } |
771 | return -EOPNOTSUPP; |
772 | } |
773 | |
774 | static int ac_rq_in(struct usb_function *f, |
775 | const struct usb_ctrlrequest *ctrl) |
776 | { |
777 | struct usb_composite_dev *cdev = f->config->cdev; |
778 | int value = -EOPNOTSUPP; |
779 | u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); |
780 | u16 len = le16_to_cpu(ctrl->wLength); |
781 | u16 w_value = le16_to_cpu(ctrl->wValue); |
782 | |
783 | DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n" , |
784 | ctrl->bRequest, w_value, len, ep); |
785 | |
786 | switch (ctrl->bRequest) { |
787 | case UAC_GET_CUR: |
788 | return in_rq_cur(fn: f, cr: ctrl); |
789 | case UAC_GET_MIN: |
790 | return in_rq_min(fn: f, cr: ctrl); |
791 | case UAC_GET_MAX: |
792 | return in_rq_max(fn: f, cr: ctrl); |
793 | case UAC_GET_RES: |
794 | return in_rq_res(fn: f, cr: ctrl); |
795 | case UAC_GET_MEM: |
796 | break; |
797 | case UAC_GET_STAT: |
798 | value = len; |
799 | break; |
800 | default: |
801 | break; |
802 | } |
803 | |
804 | return value; |
805 | } |
806 | |
807 | static int audio_set_endpoint_req(struct usb_function *f, |
808 | const struct usb_ctrlrequest *ctrl) |
809 | { |
810 | struct usb_composite_dev *cdev = f->config->cdev; |
811 | struct usb_request *req = f->config->cdev->req; |
812 | struct f_uac1 *uac1 = func_to_uac1(f); |
813 | int value = -EOPNOTSUPP; |
814 | u16 ep = le16_to_cpu(ctrl->wIndex); |
815 | u16 len = le16_to_cpu(ctrl->wLength); |
816 | u16 w_value = le16_to_cpu(ctrl->wValue); |
817 | u8 cs = w_value >> 8; |
818 | |
819 | DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n" , |
820 | ctrl->bRequest, w_value, len, ep); |
821 | |
822 | switch (ctrl->bRequest) { |
823 | case UAC_SET_CUR: { |
824 | if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) { |
825 | cdev->gadget->ep0->driver_data = f; |
826 | uac1->ctl_id = ep; |
827 | req->complete = uac_cs_attr_sample_rate; |
828 | } |
829 | value = len; |
830 | break; |
831 | } |
832 | |
833 | case UAC_SET_MIN: |
834 | break; |
835 | |
836 | case UAC_SET_MAX: |
837 | break; |
838 | |
839 | case UAC_SET_RES: |
840 | break; |
841 | |
842 | case UAC_SET_MEM: |
843 | break; |
844 | |
845 | default: |
846 | break; |
847 | } |
848 | |
849 | return value; |
850 | } |
851 | |
852 | static int audio_get_endpoint_req(struct usb_function *f, |
853 | const struct usb_ctrlrequest *ctrl) |
854 | { |
855 | struct usb_composite_dev *cdev = f->config->cdev; |
856 | struct usb_request *req = f->config->cdev->req; |
857 | struct f_uac1 *uac1 = func_to_uac1(f); |
858 | u8 *buf = (u8 *)req->buf; |
859 | int value = -EOPNOTSUPP; |
860 | u8 ep = le16_to_cpu(ctrl->wIndex); |
861 | u16 len = le16_to_cpu(ctrl->wLength); |
862 | u16 w_value = le16_to_cpu(ctrl->wValue); |
863 | u8 cs = w_value >> 8; |
864 | u32 val = 0; |
865 | |
866 | DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n" , |
867 | ctrl->bRequest, w_value, len, ep); |
868 | |
869 | switch (ctrl->bRequest) { |
870 | case UAC_GET_CUR: { |
871 | if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) { |
872 | if (ep == (USB_DIR_IN | 2)) |
873 | val = uac1->p_srate; |
874 | else if (ep == (USB_DIR_OUT | 1)) |
875 | val = uac1->c_srate; |
876 | buf[2] = (val >> 16) & 0xff; |
877 | buf[1] = (val >> 8) & 0xff; |
878 | buf[0] = val & 0xff; |
879 | } |
880 | value = len; |
881 | break; |
882 | } |
883 | case UAC_GET_MIN: |
884 | case UAC_GET_MAX: |
885 | case UAC_GET_RES: |
886 | value = len; |
887 | break; |
888 | case UAC_GET_MEM: |
889 | break; |
890 | default: |
891 | break; |
892 | } |
893 | |
894 | return value; |
895 | } |
896 | |
897 | static int |
898 | f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
899 | { |
900 | struct usb_composite_dev *cdev = f->config->cdev; |
901 | struct usb_request *req = cdev->req; |
902 | int value = -EOPNOTSUPP; |
903 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
904 | u16 w_value = le16_to_cpu(ctrl->wValue); |
905 | u16 w_length = le16_to_cpu(ctrl->wLength); |
906 | |
907 | /* composite driver infrastructure handles everything; interface |
908 | * activation uses set_alt(). |
909 | */ |
910 | switch (ctrl->bRequestType) { |
911 | case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
912 | value = audio_set_endpoint_req(f, ctrl); |
913 | break; |
914 | |
915 | case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT: |
916 | value = audio_get_endpoint_req(f, ctrl); |
917 | break; |
918 | case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE: |
919 | if (ctrl->bRequest == UAC_SET_CUR) |
920 | value = out_rq_cur(fn: f, cr: ctrl); |
921 | break; |
922 | case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE: |
923 | value = ac_rq_in(f, ctrl); |
924 | break; |
925 | default: |
926 | ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n" , |
927 | ctrl->bRequestType, ctrl->bRequest, |
928 | w_value, w_index, w_length); |
929 | } |
930 | |
931 | /* respond with data transfer or status phase? */ |
932 | if (value >= 0) { |
933 | DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n" , |
934 | ctrl->bRequestType, ctrl->bRequest, |
935 | w_value, w_index, w_length); |
936 | req->zero = 0; |
937 | req->length = value; |
938 | value = usb_ep_queue(ep: cdev->gadget->ep0, req, GFP_ATOMIC); |
939 | if (value < 0) |
940 | ERROR(cdev, "audio response on err %d\n" , value); |
941 | } |
942 | |
943 | /* device either stalls (value < 0) or reports success */ |
944 | return value; |
945 | } |
946 | |
947 | static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
948 | { |
949 | struct usb_composite_dev *cdev = f->config->cdev; |
950 | struct usb_gadget *gadget = cdev->gadget; |
951 | struct device *dev = &gadget->dev; |
952 | struct g_audio *audio = func_to_g_audio(f); |
953 | struct f_uac1 *uac1 = func_to_uac1(f); |
954 | int ret = 0; |
955 | |
956 | /* No i/f has more than 2 alt settings */ |
957 | if (alt > 1) { |
958 | dev_err(dev, "%s:%d Error!\n" , __func__, __LINE__); |
959 | return -EINVAL; |
960 | } |
961 | |
962 | if (intf == uac1->ac_intf) { |
963 | /* Control I/f has only 1 AltSetting - 0 */ |
964 | if (alt) { |
965 | dev_err(dev, "%s:%d Error!\n" , __func__, __LINE__); |
966 | return -EINVAL; |
967 | } |
968 | |
969 | /* restart interrupt endpoint */ |
970 | if (uac1->int_ep) { |
971 | usb_ep_disable(ep: uac1->int_ep); |
972 | config_ep_by_speed(g: gadget, f: &audio->func, ep: uac1->int_ep); |
973 | usb_ep_enable(ep: uac1->int_ep); |
974 | } |
975 | |
976 | return 0; |
977 | } |
978 | |
979 | if (intf == uac1->as_out_intf) { |
980 | uac1->as_out_alt = alt; |
981 | |
982 | if (alt) |
983 | ret = u_audio_start_capture(g_audio: &uac1->g_audio); |
984 | else |
985 | u_audio_stop_capture(g_audio: &uac1->g_audio); |
986 | } else if (intf == uac1->as_in_intf) { |
987 | uac1->as_in_alt = alt; |
988 | |
989 | if (alt) |
990 | ret = u_audio_start_playback(g_audio: &uac1->g_audio); |
991 | else |
992 | u_audio_stop_playback(g_audio: &uac1->g_audio); |
993 | } else { |
994 | dev_err(dev, "%s:%d Error!\n" , __func__, __LINE__); |
995 | return -EINVAL; |
996 | } |
997 | |
998 | return ret; |
999 | } |
1000 | |
1001 | static int f_audio_get_alt(struct usb_function *f, unsigned intf) |
1002 | { |
1003 | struct usb_composite_dev *cdev = f->config->cdev; |
1004 | struct usb_gadget *gadget = cdev->gadget; |
1005 | struct device *dev = &gadget->dev; |
1006 | struct f_uac1 *uac1 = func_to_uac1(f); |
1007 | |
1008 | if (intf == uac1->ac_intf) |
1009 | return uac1->ac_alt; |
1010 | else if (intf == uac1->as_out_intf) |
1011 | return uac1->as_out_alt; |
1012 | else if (intf == uac1->as_in_intf) |
1013 | return uac1->as_in_alt; |
1014 | else |
1015 | dev_err(dev, "%s:%d Invalid Interface %d!\n" , |
1016 | __func__, __LINE__, intf); |
1017 | |
1018 | return -EINVAL; |
1019 | } |
1020 | |
1021 | |
1022 | static void f_audio_disable(struct usb_function *f) |
1023 | { |
1024 | struct f_uac1 *uac1 = func_to_uac1(f); |
1025 | |
1026 | uac1->as_out_alt = 0; |
1027 | uac1->as_in_alt = 0; |
1028 | |
1029 | u_audio_stop_playback(g_audio: &uac1->g_audio); |
1030 | u_audio_stop_capture(g_audio: &uac1->g_audio); |
1031 | if (uac1->int_ep) |
1032 | usb_ep_disable(ep: uac1->int_ep); |
1033 | } |
1034 | |
1035 | static void |
1036 | f_audio_suspend(struct usb_function *f) |
1037 | { |
1038 | struct f_uac1 *uac1 = func_to_uac1(f); |
1039 | |
1040 | u_audio_suspend(g_audio: &uac1->g_audio); |
1041 | } |
1042 | |
1043 | /*-------------------------------------------------------------------------*/ |
1044 | static struct uac_feature_unit_descriptor *build_fu_desc(int chmask) |
1045 | { |
1046 | struct uac_feature_unit_descriptor *fu_desc; |
1047 | int channels = num_channels(chanmask: chmask); |
1048 | int fu_desc_size = UAC_DT_FEATURE_UNIT_SIZE(channels); |
1049 | |
1050 | fu_desc = kzalloc(size: fu_desc_size, GFP_KERNEL); |
1051 | if (!fu_desc) |
1052 | return NULL; |
1053 | |
1054 | fu_desc->bLength = fu_desc_size; |
1055 | fu_desc->bDescriptorType = USB_DT_CS_INTERFACE; |
1056 | |
1057 | fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT; |
1058 | fu_desc->bControlSize = 2; |
1059 | |
1060 | /* bUnitID, bSourceID and bmaControls will be defined later */ |
1061 | |
1062 | return fu_desc; |
1063 | } |
1064 | |
1065 | /* B.3.2 Class-Specific AC Interface Descriptor */ |
1066 | static struct |
1067 | uac1_ac_header_descriptor *(struct f_uac1_opts *opts) |
1068 | { |
1069 | struct uac1_ac_header_descriptor *ac_desc; |
1070 | int ; |
1071 | int num_ifaces = 0; |
1072 | |
1073 | if (EPOUT_EN(opts)) |
1074 | num_ifaces++; |
1075 | if (EPIN_EN(opts)) |
1076 | num_ifaces++; |
1077 | |
1078 | ac_header_desc_size = UAC_DT_AC_HEADER_SIZE(num_ifaces); |
1079 | |
1080 | ac_desc = kzalloc(size: ac_header_desc_size, GFP_KERNEL); |
1081 | if (!ac_desc) |
1082 | return NULL; |
1083 | |
1084 | ac_desc->bLength = ac_header_desc_size; |
1085 | ac_desc->bDescriptorType = USB_DT_CS_INTERFACE; |
1086 | ac_desc->bDescriptorSubtype = UAC_HEADER; |
1087 | ac_desc->bcdADC = cpu_to_le16(0x0100); |
1088 | ac_desc->bInCollection = num_ifaces; |
1089 | |
1090 | /* wTotalLength and baInterfaceNr will be defined later */ |
1091 | |
1092 | return ac_desc; |
1093 | } |
1094 | |
1095 | /* Use macro to overcome line length limitation */ |
1096 | #define USBDHDR(p) (struct usb_descriptor_header *)(p) |
1097 | |
1098 | static void setup_descriptor(struct f_uac1_opts *opts) |
1099 | { |
1100 | /* patch descriptors */ |
1101 | int i = 1; /* ID's start with 1 */ |
1102 | |
1103 | if (EPOUT_EN(opts)) |
1104 | usb_out_it_desc.bTerminalID = i++; |
1105 | if (EPIN_EN(opts)) |
1106 | io_in_it_desc.bTerminalID = i++; |
1107 | if (EPOUT_EN(opts)) |
1108 | io_out_ot_desc.bTerminalID = i++; |
1109 | if (EPIN_EN(opts)) |
1110 | usb_in_ot_desc.bTerminalID = i++; |
1111 | if (FUOUT_EN(opts)) |
1112 | out_feature_unit_desc->bUnitID = i++; |
1113 | if (FUIN_EN(opts)) |
1114 | in_feature_unit_desc->bUnitID = i++; |
1115 | |
1116 | if (FUIN_EN(opts)) { |
1117 | usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID; |
1118 | in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID; |
1119 | } else { |
1120 | usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID; |
1121 | } |
1122 | if (FUOUT_EN(opts)) { |
1123 | io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID; |
1124 | out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID; |
1125 | } else { |
1126 | io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID; |
1127 | } |
1128 | |
1129 | as_out_header_desc.bTerminalLink = usb_out_it_desc.bTerminalID; |
1130 | as_in_header_desc.bTerminalLink = usb_in_ot_desc.bTerminalID; |
1131 | |
1132 | ac_header_desc->wTotalLength = cpu_to_le16(ac_header_desc->bLength); |
1133 | |
1134 | if (EPIN_EN(opts)) { |
1135 | u16 len = le16_to_cpu(ac_header_desc->wTotalLength); |
1136 | |
1137 | len += sizeof(usb_in_ot_desc); |
1138 | len += sizeof(io_in_it_desc); |
1139 | if (FUIN_EN(opts)) |
1140 | len += in_feature_unit_desc->bLength; |
1141 | ac_header_desc->wTotalLength = cpu_to_le16(len); |
1142 | } |
1143 | if (EPOUT_EN(opts)) { |
1144 | u16 len = le16_to_cpu(ac_header_desc->wTotalLength); |
1145 | |
1146 | len += sizeof(usb_out_it_desc); |
1147 | len += sizeof(io_out_ot_desc); |
1148 | if (FUOUT_EN(opts)) |
1149 | len += out_feature_unit_desc->bLength; |
1150 | ac_header_desc->wTotalLength = cpu_to_le16(len); |
1151 | } |
1152 | |
1153 | i = 0; |
1154 | f_audio_desc[i++] = USBDHDR(&ac_interface_desc); |
1155 | f_audio_desc[i++] = USBDHDR(ac_header_desc); |
1156 | |
1157 | if (EPOUT_EN(opts)) { |
1158 | f_audio_desc[i++] = USBDHDR(&usb_out_it_desc); |
1159 | f_audio_desc[i++] = USBDHDR(&io_out_ot_desc); |
1160 | if (FUOUT_EN(opts)) |
1161 | f_audio_desc[i++] = USBDHDR(out_feature_unit_desc); |
1162 | } |
1163 | |
1164 | if (EPIN_EN(opts)) { |
1165 | f_audio_desc[i++] = USBDHDR(&io_in_it_desc); |
1166 | f_audio_desc[i++] = USBDHDR(&usb_in_ot_desc); |
1167 | if (FUIN_EN(opts)) |
1168 | f_audio_desc[i++] = USBDHDR(in_feature_unit_desc); |
1169 | } |
1170 | |
1171 | if (FUOUT_EN(opts) || FUIN_EN(opts)) |
1172 | f_audio_desc[i++] = USBDHDR(&ac_int_ep_desc); |
1173 | |
1174 | if (EPOUT_EN(opts)) { |
1175 | f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_0_desc); |
1176 | f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_1_desc); |
1177 | f_audio_desc[i++] = USBDHDR(&as_out_header_desc); |
1178 | f_audio_desc[i++] = USBDHDR(&as_out_type_i_desc); |
1179 | f_audio_desc[i++] = USBDHDR(&as_out_ep_desc); |
1180 | f_audio_desc[i++] = USBDHDR(&as_iso_out_desc); |
1181 | } |
1182 | if (EPIN_EN(opts)) { |
1183 | f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_0_desc); |
1184 | f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_1_desc); |
1185 | f_audio_desc[i++] = USBDHDR(&as_in_header_desc); |
1186 | f_audio_desc[i++] = USBDHDR(&as_in_type_i_desc); |
1187 | f_audio_desc[i++] = USBDHDR(&as_in_ep_desc); |
1188 | f_audio_desc[i++] = USBDHDR(&as_iso_in_desc); |
1189 | } |
1190 | f_audio_desc[i] = NULL; |
1191 | } |
1192 | |
1193 | static int f_audio_validate_opts(struct g_audio *audio, struct device *dev) |
1194 | { |
1195 | struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); |
1196 | |
1197 | if (!opts->p_chmask && !opts->c_chmask) { |
1198 | dev_err(dev, "Error: no playback and capture channels\n" ); |
1199 | return -EINVAL; |
1200 | } else if (opts->p_chmask & ~UAC1_CHANNEL_MASK) { |
1201 | dev_err(dev, "Error: unsupported playback channels mask\n" ); |
1202 | return -EINVAL; |
1203 | } else if (opts->c_chmask & ~UAC1_CHANNEL_MASK) { |
1204 | dev_err(dev, "Error: unsupported capture channels mask\n" ); |
1205 | return -EINVAL; |
1206 | } else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) { |
1207 | dev_err(dev, "Error: incorrect playback sample size\n" ); |
1208 | return -EINVAL; |
1209 | } else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) { |
1210 | dev_err(dev, "Error: incorrect capture sample size\n" ); |
1211 | return -EINVAL; |
1212 | } else if (!opts->p_srates[0]) { |
1213 | dev_err(dev, "Error: incorrect playback sampling rate\n" ); |
1214 | return -EINVAL; |
1215 | } else if (!opts->c_srates[0]) { |
1216 | dev_err(dev, "Error: incorrect capture sampling rate\n" ); |
1217 | return -EINVAL; |
1218 | } |
1219 | |
1220 | if (opts->p_volume_max <= opts->p_volume_min) { |
1221 | dev_err(dev, "Error: incorrect playback volume max/min\n" ); |
1222 | return -EINVAL; |
1223 | } else if (opts->c_volume_max <= opts->c_volume_min) { |
1224 | dev_err(dev, "Error: incorrect capture volume max/min\n" ); |
1225 | return -EINVAL; |
1226 | } else if (opts->p_volume_res <= 0) { |
1227 | dev_err(dev, "Error: negative/zero playback volume resolution\n" ); |
1228 | return -EINVAL; |
1229 | } else if (opts->c_volume_res <= 0) { |
1230 | dev_err(dev, "Error: negative/zero capture volume resolution\n" ); |
1231 | return -EINVAL; |
1232 | } |
1233 | |
1234 | if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res) { |
1235 | dev_err(dev, "Error: incorrect playback volume resolution\n" ); |
1236 | return -EINVAL; |
1237 | } else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res) { |
1238 | dev_err(dev, "Error: incorrect capture volume resolution\n" ); |
1239 | return -EINVAL; |
1240 | } |
1241 | |
1242 | return 0; |
1243 | } |
1244 | |
1245 | /* audio function driver setup/binding */ |
1246 | static int f_audio_bind(struct usb_configuration *c, struct usb_function *f) |
1247 | { |
1248 | struct usb_composite_dev *cdev = c->cdev; |
1249 | struct usb_gadget *gadget = cdev->gadget; |
1250 | struct device *dev = &gadget->dev; |
1251 | struct f_uac1 *uac1 = func_to_uac1(f); |
1252 | struct g_audio *audio = func_to_g_audio(f); |
1253 | struct f_uac1_opts *audio_opts; |
1254 | struct usb_ep *ep = NULL; |
1255 | struct usb_string *us; |
1256 | int ba_iface_id; |
1257 | int status; |
1258 | int idx, i; |
1259 | |
1260 | status = f_audio_validate_opts(audio, dev); |
1261 | if (status) |
1262 | return status; |
1263 | |
1264 | audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst); |
1265 | |
1266 | strings_uac1[STR_AC_IF].s = audio_opts->function_name; |
1267 | |
1268 | us = usb_gstrings_attach(cdev, sp: uac1_strings, ARRAY_SIZE(strings_uac1)); |
1269 | if (IS_ERR(ptr: us)) |
1270 | return PTR_ERR(ptr: us); |
1271 | |
1272 | ac_header_desc = build_ac_header_desc(opts: audio_opts); |
1273 | if (!ac_header_desc) |
1274 | return -ENOMEM; |
1275 | |
1276 | if (FUOUT_EN(audio_opts)) { |
1277 | out_feature_unit_desc = build_fu_desc(chmask: audio_opts->c_chmask); |
1278 | if (!out_feature_unit_desc) { |
1279 | status = -ENOMEM; |
1280 | goto fail; |
1281 | } |
1282 | } |
1283 | if (FUIN_EN(audio_opts)) { |
1284 | in_feature_unit_desc = build_fu_desc(chmask: audio_opts->p_chmask); |
1285 | if (!in_feature_unit_desc) { |
1286 | status = -ENOMEM; |
1287 | goto err_free_fu; |
1288 | } |
1289 | } |
1290 | |
1291 | ac_interface_desc.iInterface = us[STR_AC_IF].id; |
1292 | usb_out_it_desc.iTerminal = us[STR_USB_OUT_IT].id; |
1293 | usb_out_it_desc.iChannelNames = us[STR_USB_OUT_IT_CH_NAMES].id; |
1294 | io_out_ot_desc.iTerminal = us[STR_IO_OUT_OT].id; |
1295 | as_out_interface_alt_0_desc.iInterface = us[STR_AS_OUT_IF_ALT0].id; |
1296 | as_out_interface_alt_1_desc.iInterface = us[STR_AS_OUT_IF_ALT1].id; |
1297 | io_in_it_desc.iTerminal = us[STR_IO_IN_IT].id; |
1298 | io_in_it_desc.iChannelNames = us[STR_IO_IN_IT_CH_NAMES].id; |
1299 | usb_in_ot_desc.iTerminal = us[STR_USB_IN_OT].id; |
1300 | as_in_interface_alt_0_desc.iInterface = us[STR_AS_IN_IF_ALT0].id; |
1301 | as_in_interface_alt_1_desc.iInterface = us[STR_AS_IN_IF_ALT1].id; |
1302 | |
1303 | if (FUOUT_EN(audio_opts)) { |
1304 | u8 *i_feature; |
1305 | |
1306 | i_feature = (u8 *)out_feature_unit_desc + |
1307 | out_feature_unit_desc->bLength - 1; |
1308 | *i_feature = us[STR_FU_OUT].id; |
1309 | } |
1310 | if (FUIN_EN(audio_opts)) { |
1311 | u8 *i_feature; |
1312 | |
1313 | i_feature = (u8 *)in_feature_unit_desc + |
1314 | in_feature_unit_desc->bLength - 1; |
1315 | *i_feature = us[STR_FU_IN].id; |
1316 | } |
1317 | |
1318 | /* Set channel numbers */ |
1319 | usb_out_it_desc.bNrChannels = num_channels(chanmask: audio_opts->c_chmask); |
1320 | usb_out_it_desc.wChannelConfig = cpu_to_le16(audio_opts->c_chmask); |
1321 | as_out_type_i_desc.bNrChannels = num_channels(chanmask: audio_opts->c_chmask); |
1322 | as_out_type_i_desc.bSubframeSize = audio_opts->c_ssize; |
1323 | as_out_type_i_desc.bBitResolution = audio_opts->c_ssize * 8; |
1324 | io_in_it_desc.bNrChannels = num_channels(chanmask: audio_opts->p_chmask); |
1325 | io_in_it_desc.wChannelConfig = cpu_to_le16(audio_opts->p_chmask); |
1326 | as_in_type_i_desc.bNrChannels = num_channels(chanmask: audio_opts->p_chmask); |
1327 | as_in_type_i_desc.bSubframeSize = audio_opts->p_ssize; |
1328 | as_in_type_i_desc.bBitResolution = audio_opts->p_ssize * 8; |
1329 | |
1330 | if (FUOUT_EN(audio_opts)) { |
1331 | __le16 *bma = (__le16 *)&out_feature_unit_desc->bmaControls[0]; |
1332 | u32 control = 0; |
1333 | |
1334 | if (audio_opts->c_mute_present) |
1335 | control |= UAC_FU_MUTE; |
1336 | if (audio_opts->c_volume_present) |
1337 | control |= UAC_FU_VOLUME; |
1338 | *bma = cpu_to_le16(control); |
1339 | } |
1340 | if (FUIN_EN(audio_opts)) { |
1341 | __le16 *bma = (__le16 *)&in_feature_unit_desc->bmaControls[0]; |
1342 | u32 control = 0; |
1343 | |
1344 | if (audio_opts->p_mute_present) |
1345 | control |= UAC_FU_MUTE; |
1346 | if (audio_opts->p_volume_present) |
1347 | control |= UAC_FU_VOLUME; |
1348 | *bma = cpu_to_le16(control); |
1349 | } |
1350 | |
1351 | /* Set sample rates */ |
1352 | for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) { |
1353 | if (audio_opts->c_srates[i] == 0) |
1354 | break; |
1355 | memcpy(as_out_type_i_desc.tSamFreq[idx++], |
1356 | &audio_opts->c_srates[i], 3); |
1357 | } |
1358 | as_out_type_i_desc.bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(idx); |
1359 | as_out_type_i_desc.bSamFreqType = idx; |
1360 | |
1361 | for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) { |
1362 | if (audio_opts->p_srates[i] == 0) |
1363 | break; |
1364 | memcpy(as_in_type_i_desc.tSamFreq[idx++], |
1365 | &audio_opts->p_srates[i], 3); |
1366 | } |
1367 | as_in_type_i_desc.bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(idx); |
1368 | as_in_type_i_desc.bSamFreqType = idx; |
1369 | uac1->p_srate = audio_opts->p_srates[0]; |
1370 | uac1->c_srate = audio_opts->c_srates[0]; |
1371 | |
1372 | /* allocate instance-specific interface IDs, and patch descriptors */ |
1373 | status = usb_interface_id(c, f); |
1374 | if (status < 0) |
1375 | goto err_free_fu; |
1376 | ac_interface_desc.bInterfaceNumber = status; |
1377 | uac1->ac_intf = status; |
1378 | uac1->ac_alt = 0; |
1379 | |
1380 | ba_iface_id = 0; |
1381 | |
1382 | if (EPOUT_EN(audio_opts)) { |
1383 | status = usb_interface_id(c, f); |
1384 | if (status < 0) |
1385 | goto err_free_fu; |
1386 | as_out_interface_alt_0_desc.bInterfaceNumber = status; |
1387 | as_out_interface_alt_1_desc.bInterfaceNumber = status; |
1388 | ac_header_desc->baInterfaceNr[ba_iface_id++] = status; |
1389 | uac1->as_out_intf = status; |
1390 | uac1->as_out_alt = 0; |
1391 | } |
1392 | |
1393 | if (EPIN_EN(audio_opts)) { |
1394 | status = usb_interface_id(c, f); |
1395 | if (status < 0) |
1396 | goto err_free_fu; |
1397 | as_in_interface_alt_0_desc.bInterfaceNumber = status; |
1398 | as_in_interface_alt_1_desc.bInterfaceNumber = status; |
1399 | ac_header_desc->baInterfaceNr[ba_iface_id++] = status; |
1400 | uac1->as_in_intf = status; |
1401 | uac1->as_in_alt = 0; |
1402 | } |
1403 | |
1404 | audio->gadget = gadget; |
1405 | |
1406 | status = -ENODEV; |
1407 | |
1408 | ac_interface_desc.bNumEndpoints = 0; |
1409 | |
1410 | /* allocate AC interrupt endpoint */ |
1411 | if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts)) { |
1412 | ep = usb_ep_autoconfig(cdev->gadget, &ac_int_ep_desc); |
1413 | if (!ep) |
1414 | goto err_free_fu; |
1415 | uac1->int_ep = ep; |
1416 | uac1->int_ep->desc = &ac_int_ep_desc; |
1417 | |
1418 | ac_interface_desc.bNumEndpoints = 1; |
1419 | } |
1420 | |
1421 | /* allocate instance-specific endpoints */ |
1422 | if (EPOUT_EN(audio_opts)) { |
1423 | ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc); |
1424 | if (!ep) |
1425 | goto err_free_fu; |
1426 | ss_as_out_ep_desc.bEndpointAddress = as_out_ep_desc.bEndpointAddress; |
1427 | audio->out_ep = ep; |
1428 | audio->out_ep->desc = &as_out_ep_desc; |
1429 | } |
1430 | |
1431 | if (EPIN_EN(audio_opts)) { |
1432 | ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc); |
1433 | if (!ep) |
1434 | goto err_free_fu; |
1435 | ss_as_in_ep_desc.bEndpointAddress = as_in_ep_desc.bEndpointAddress; |
1436 | audio->in_ep = ep; |
1437 | audio->in_ep->desc = &as_in_ep_desc; |
1438 | } |
1439 | |
1440 | setup_descriptor(audio_opts); |
1441 | |
1442 | /* copy descriptors, and track endpoint copies */ |
1443 | status = usb_assign_descriptors(f, fs: f_audio_desc, hs: f_audio_desc, ss: f_audio_ss_desc, |
1444 | ssp: f_audio_ss_desc); |
1445 | if (status) |
1446 | goto err_free_fu; |
1447 | |
1448 | audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize); |
1449 | audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize); |
1450 | audio->params.c_chmask = audio_opts->c_chmask; |
1451 | memcpy(audio->params.c_srates, audio_opts->c_srates, |
1452 | sizeof(audio->params.c_srates)); |
1453 | audio->params.c_ssize = audio_opts->c_ssize; |
1454 | if (FUIN_EN(audio_opts)) { |
1455 | audio->params.p_fu.id = USB_IN_FU_ID; |
1456 | audio->params.p_fu.mute_present = audio_opts->p_mute_present; |
1457 | audio->params.p_fu.volume_present = |
1458 | audio_opts->p_volume_present; |
1459 | audio->params.p_fu.volume_min = audio_opts->p_volume_min; |
1460 | audio->params.p_fu.volume_max = audio_opts->p_volume_max; |
1461 | audio->params.p_fu.volume_res = audio_opts->p_volume_res; |
1462 | } |
1463 | audio->params.p_chmask = audio_opts->p_chmask; |
1464 | memcpy(audio->params.p_srates, audio_opts->p_srates, |
1465 | sizeof(audio->params.p_srates)); |
1466 | audio->params.p_ssize = audio_opts->p_ssize; |
1467 | if (FUOUT_EN(audio_opts)) { |
1468 | audio->params.c_fu.id = USB_OUT_FU_ID; |
1469 | audio->params.c_fu.mute_present = audio_opts->c_mute_present; |
1470 | audio->params.c_fu.volume_present = |
1471 | audio_opts->c_volume_present; |
1472 | audio->params.c_fu.volume_min = audio_opts->c_volume_min; |
1473 | audio->params.c_fu.volume_max = audio_opts->c_volume_max; |
1474 | audio->params.c_fu.volume_res = audio_opts->c_volume_res; |
1475 | } |
1476 | audio->params.req_number = audio_opts->req_number; |
1477 | audio->params.fb_max = FBACK_FAST_MAX; |
1478 | if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts)) |
1479 | audio->notify = audio_notify; |
1480 | |
1481 | status = g_audio_setup(g_audio: audio, pcm_name: "UAC1_PCM" , card_name: "UAC1_Gadget" ); |
1482 | if (status) |
1483 | goto err_card_register; |
1484 | |
1485 | return 0; |
1486 | |
1487 | err_card_register: |
1488 | usb_free_all_descriptors(f); |
1489 | err_free_fu: |
1490 | kfree(objp: out_feature_unit_desc); |
1491 | out_feature_unit_desc = NULL; |
1492 | kfree(objp: in_feature_unit_desc); |
1493 | in_feature_unit_desc = NULL; |
1494 | fail: |
1495 | kfree(objp: ac_header_desc); |
1496 | ac_header_desc = NULL; |
1497 | return status; |
1498 | } |
1499 | |
1500 | /*-------------------------------------------------------------------------*/ |
1501 | |
1502 | static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item) |
1503 | { |
1504 | return container_of(to_config_group(item), struct f_uac1_opts, |
1505 | func_inst.group); |
1506 | } |
1507 | |
1508 | static void f_uac1_attr_release(struct config_item *item) |
1509 | { |
1510 | struct f_uac1_opts *opts = to_f_uac1_opts(item); |
1511 | |
1512 | usb_put_function_instance(fi: &opts->func_inst); |
1513 | } |
1514 | |
1515 | static struct configfs_item_operations f_uac1_item_ops = { |
1516 | .release = f_uac1_attr_release, |
1517 | }; |
1518 | |
1519 | #define uac1_kstrtou32 kstrtou32 |
1520 | #define uac1_kstrtos16 kstrtos16 |
1521 | #define uac1_kstrtobool(s, base, res) kstrtobool((s), (res)) |
1522 | |
1523 | static const char *u32_fmt = "%u\n" ; |
1524 | static const char *s16_fmt = "%hd\n" ; |
1525 | static const char *bool_fmt = "%u\n" ; |
1526 | |
1527 | #define UAC1_ATTRIBUTE(type, name) \ |
1528 | static ssize_t f_uac1_opts_##name##_show( \ |
1529 | struct config_item *item, \ |
1530 | char *page) \ |
1531 | { \ |
1532 | struct f_uac1_opts *opts = to_f_uac1_opts(item); \ |
1533 | int result; \ |
1534 | \ |
1535 | mutex_lock(&opts->lock); \ |
1536 | result = sprintf(page, type##_fmt, opts->name); \ |
1537 | mutex_unlock(&opts->lock); \ |
1538 | \ |
1539 | return result; \ |
1540 | } \ |
1541 | \ |
1542 | static ssize_t f_uac1_opts_##name##_store( \ |
1543 | struct config_item *item, \ |
1544 | const char *page, size_t len) \ |
1545 | { \ |
1546 | struct f_uac1_opts *opts = to_f_uac1_opts(item); \ |
1547 | int ret; \ |
1548 | type num; \ |
1549 | \ |
1550 | mutex_lock(&opts->lock); \ |
1551 | if (opts->refcnt) { \ |
1552 | ret = -EBUSY; \ |
1553 | goto end; \ |
1554 | } \ |
1555 | \ |
1556 | ret = uac1_kstrto##type(page, 0, &num); \ |
1557 | if (ret) \ |
1558 | goto end; \ |
1559 | \ |
1560 | opts->name = num; \ |
1561 | ret = len; \ |
1562 | \ |
1563 | end: \ |
1564 | mutex_unlock(&opts->lock); \ |
1565 | return ret; \ |
1566 | } \ |
1567 | \ |
1568 | CONFIGFS_ATTR(f_uac1_opts_, name) |
1569 | |
1570 | #define UAC1_RATE_ATTRIBUTE(name) \ |
1571 | static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ |
1572 | char *page) \ |
1573 | { \ |
1574 | struct f_uac1_opts *opts = to_f_uac1_opts(item); \ |
1575 | int result = 0; \ |
1576 | int i; \ |
1577 | \ |
1578 | mutex_lock(&opts->lock); \ |
1579 | page[0] = '\0'; \ |
1580 | for (i = 0; i < UAC_MAX_RATES; i++) { \ |
1581 | if (opts->name##s[i] == 0) \ |
1582 | break; \ |
1583 | result += sprintf(page + strlen(page), "%u,", \ |
1584 | opts->name##s[i]); \ |
1585 | } \ |
1586 | if (strlen(page) > 0) \ |
1587 | page[strlen(page) - 1] = '\n'; \ |
1588 | mutex_unlock(&opts->lock); \ |
1589 | \ |
1590 | return result; \ |
1591 | } \ |
1592 | \ |
1593 | static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ |
1594 | const char *page, size_t len) \ |
1595 | { \ |
1596 | struct f_uac1_opts *opts = to_f_uac1_opts(item); \ |
1597 | char *split_page = NULL; \ |
1598 | int ret = -EINVAL; \ |
1599 | char *token; \ |
1600 | u32 num; \ |
1601 | int i; \ |
1602 | \ |
1603 | mutex_lock(&opts->lock); \ |
1604 | if (opts->refcnt) { \ |
1605 | ret = -EBUSY; \ |
1606 | goto end; \ |
1607 | } \ |
1608 | \ |
1609 | i = 0; \ |
1610 | memset(opts->name##s, 0x00, sizeof(opts->name##s)); \ |
1611 | split_page = kstrdup(page, GFP_KERNEL); \ |
1612 | while ((token = strsep(&split_page, ",")) != NULL) { \ |
1613 | ret = kstrtou32(token, 0, &num); \ |
1614 | if (ret) \ |
1615 | goto end; \ |
1616 | \ |
1617 | opts->name##s[i++] = num; \ |
1618 | ret = len; \ |
1619 | }; \ |
1620 | \ |
1621 | end: \ |
1622 | kfree(split_page); \ |
1623 | mutex_unlock(&opts->lock); \ |
1624 | return ret; \ |
1625 | } \ |
1626 | \ |
1627 | CONFIGFS_ATTR(f_uac1_opts_, name) |
1628 | |
1629 | #define UAC1_ATTRIBUTE_STRING(name) \ |
1630 | static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \ |
1631 | char *page) \ |
1632 | { \ |
1633 | struct f_uac1_opts *opts = to_f_uac1_opts(item); \ |
1634 | int result; \ |
1635 | \ |
1636 | mutex_lock(&opts->lock); \ |
1637 | result = scnprintf(page, sizeof(opts->name), "%s", opts->name); \ |
1638 | mutex_unlock(&opts->lock); \ |
1639 | \ |
1640 | return result; \ |
1641 | } \ |
1642 | \ |
1643 | static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \ |
1644 | const char *page, size_t len) \ |
1645 | { \ |
1646 | struct f_uac1_opts *opts = to_f_uac1_opts(item); \ |
1647 | int ret = 0; \ |
1648 | \ |
1649 | mutex_lock(&opts->lock); \ |
1650 | if (opts->refcnt) { \ |
1651 | ret = -EBUSY; \ |
1652 | goto end; \ |
1653 | } \ |
1654 | \ |
1655 | ret = scnprintf(opts->name, min(sizeof(opts->name), len), \ |
1656 | "%s", page); \ |
1657 | \ |
1658 | end: \ |
1659 | mutex_unlock(&opts->lock); \ |
1660 | return ret; \ |
1661 | } \ |
1662 | \ |
1663 | CONFIGFS_ATTR(f_uac1_opts_, name) |
1664 | |
1665 | UAC1_ATTRIBUTE(u32, c_chmask); |
1666 | UAC1_RATE_ATTRIBUTE(c_srate); |
1667 | UAC1_ATTRIBUTE(u32, c_ssize); |
1668 | UAC1_ATTRIBUTE(u32, p_chmask); |
1669 | UAC1_RATE_ATTRIBUTE(p_srate); |
1670 | UAC1_ATTRIBUTE(u32, p_ssize); |
1671 | UAC1_ATTRIBUTE(u32, req_number); |
1672 | |
1673 | UAC1_ATTRIBUTE(bool, p_mute_present); |
1674 | UAC1_ATTRIBUTE(bool, p_volume_present); |
1675 | UAC1_ATTRIBUTE(s16, p_volume_min); |
1676 | UAC1_ATTRIBUTE(s16, p_volume_max); |
1677 | UAC1_ATTRIBUTE(s16, p_volume_res); |
1678 | |
1679 | UAC1_ATTRIBUTE(bool, c_mute_present); |
1680 | UAC1_ATTRIBUTE(bool, c_volume_present); |
1681 | UAC1_ATTRIBUTE(s16, c_volume_min); |
1682 | UAC1_ATTRIBUTE(s16, c_volume_max); |
1683 | UAC1_ATTRIBUTE(s16, c_volume_res); |
1684 | UAC1_ATTRIBUTE_STRING(function_name); |
1685 | |
1686 | static struct configfs_attribute *f_uac1_attrs[] = { |
1687 | &f_uac1_opts_attr_c_chmask, |
1688 | &f_uac1_opts_attr_c_srate, |
1689 | &f_uac1_opts_attr_c_ssize, |
1690 | &f_uac1_opts_attr_p_chmask, |
1691 | &f_uac1_opts_attr_p_srate, |
1692 | &f_uac1_opts_attr_p_ssize, |
1693 | &f_uac1_opts_attr_req_number, |
1694 | |
1695 | &f_uac1_opts_attr_p_mute_present, |
1696 | &f_uac1_opts_attr_p_volume_present, |
1697 | &f_uac1_opts_attr_p_volume_min, |
1698 | &f_uac1_opts_attr_p_volume_max, |
1699 | &f_uac1_opts_attr_p_volume_res, |
1700 | |
1701 | &f_uac1_opts_attr_c_mute_present, |
1702 | &f_uac1_opts_attr_c_volume_present, |
1703 | &f_uac1_opts_attr_c_volume_min, |
1704 | &f_uac1_opts_attr_c_volume_max, |
1705 | &f_uac1_opts_attr_c_volume_res, |
1706 | |
1707 | &f_uac1_opts_attr_function_name, |
1708 | |
1709 | NULL, |
1710 | }; |
1711 | |
1712 | static const struct config_item_type f_uac1_func_type = { |
1713 | .ct_item_ops = &f_uac1_item_ops, |
1714 | .ct_attrs = f_uac1_attrs, |
1715 | .ct_owner = THIS_MODULE, |
1716 | }; |
1717 | |
1718 | static void f_audio_free_inst(struct usb_function_instance *f) |
1719 | { |
1720 | struct f_uac1_opts *opts; |
1721 | |
1722 | opts = container_of(f, struct f_uac1_opts, func_inst); |
1723 | kfree(objp: opts); |
1724 | } |
1725 | |
1726 | static struct usb_function_instance *f_audio_alloc_inst(void) |
1727 | { |
1728 | struct f_uac1_opts *opts; |
1729 | |
1730 | opts = kzalloc(size: sizeof(*opts), GFP_KERNEL); |
1731 | if (!opts) |
1732 | return ERR_PTR(error: -ENOMEM); |
1733 | |
1734 | mutex_init(&opts->lock); |
1735 | opts->func_inst.free_func_inst = f_audio_free_inst; |
1736 | |
1737 | config_group_init_type_name(group: &opts->func_inst.group, name: "" , |
1738 | type: &f_uac1_func_type); |
1739 | |
1740 | opts->c_chmask = UAC1_DEF_CCHMASK; |
1741 | opts->c_srates[0] = UAC1_DEF_CSRATE; |
1742 | opts->c_ssize = UAC1_DEF_CSSIZE; |
1743 | opts->p_chmask = UAC1_DEF_PCHMASK; |
1744 | opts->p_srates[0] = UAC1_DEF_PSRATE; |
1745 | opts->p_ssize = UAC1_DEF_PSSIZE; |
1746 | |
1747 | opts->p_mute_present = UAC1_DEF_MUTE_PRESENT; |
1748 | opts->p_volume_present = UAC1_DEF_VOLUME_PRESENT; |
1749 | opts->p_volume_min = UAC1_DEF_MIN_DB; |
1750 | opts->p_volume_max = UAC1_DEF_MAX_DB; |
1751 | opts->p_volume_res = UAC1_DEF_RES_DB; |
1752 | |
1753 | opts->c_mute_present = UAC1_DEF_MUTE_PRESENT; |
1754 | opts->c_volume_present = UAC1_DEF_VOLUME_PRESENT; |
1755 | opts->c_volume_min = UAC1_DEF_MIN_DB; |
1756 | opts->c_volume_max = UAC1_DEF_MAX_DB; |
1757 | opts->c_volume_res = UAC1_DEF_RES_DB; |
1758 | |
1759 | opts->req_number = UAC1_DEF_REQ_NUM; |
1760 | |
1761 | scnprintf(buf: opts->function_name, size: sizeof(opts->function_name), fmt: "AC Interface" ); |
1762 | |
1763 | return &opts->func_inst; |
1764 | } |
1765 | |
1766 | static void f_audio_free(struct usb_function *f) |
1767 | { |
1768 | struct g_audio *audio; |
1769 | struct f_uac1_opts *opts; |
1770 | |
1771 | audio = func_to_g_audio(f); |
1772 | opts = container_of(f->fi, struct f_uac1_opts, func_inst); |
1773 | kfree(objp: audio); |
1774 | mutex_lock(&opts->lock); |
1775 | --opts->refcnt; |
1776 | mutex_unlock(lock: &opts->lock); |
1777 | } |
1778 | |
1779 | static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f) |
1780 | { |
1781 | struct g_audio *audio = func_to_g_audio(f); |
1782 | |
1783 | g_audio_cleanup(g_audio: audio); |
1784 | usb_free_all_descriptors(f); |
1785 | |
1786 | kfree(objp: out_feature_unit_desc); |
1787 | out_feature_unit_desc = NULL; |
1788 | kfree(objp: in_feature_unit_desc); |
1789 | in_feature_unit_desc = NULL; |
1790 | |
1791 | kfree(objp: ac_header_desc); |
1792 | ac_header_desc = NULL; |
1793 | |
1794 | audio->gadget = NULL; |
1795 | } |
1796 | |
1797 | static struct usb_function *f_audio_alloc(struct usb_function_instance *fi) |
1798 | { |
1799 | struct f_uac1 *uac1; |
1800 | struct f_uac1_opts *opts; |
1801 | |
1802 | /* allocate and initialize one new instance */ |
1803 | uac1 = kzalloc(size: sizeof(*uac1), GFP_KERNEL); |
1804 | if (!uac1) |
1805 | return ERR_PTR(error: -ENOMEM); |
1806 | |
1807 | opts = container_of(fi, struct f_uac1_opts, func_inst); |
1808 | mutex_lock(&opts->lock); |
1809 | ++opts->refcnt; |
1810 | mutex_unlock(lock: &opts->lock); |
1811 | |
1812 | uac1->g_audio.func.name = "uac1_func" ; |
1813 | uac1->g_audio.func.bind = f_audio_bind; |
1814 | uac1->g_audio.func.unbind = f_audio_unbind; |
1815 | uac1->g_audio.func.set_alt = f_audio_set_alt; |
1816 | uac1->g_audio.func.get_alt = f_audio_get_alt; |
1817 | uac1->g_audio.func.setup = f_audio_setup; |
1818 | uac1->g_audio.func.disable = f_audio_disable; |
1819 | uac1->g_audio.func.suspend = f_audio_suspend; |
1820 | uac1->g_audio.func.free_func = f_audio_free; |
1821 | |
1822 | return &uac1->g_audio.func; |
1823 | } |
1824 | |
1825 | DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc); |
1826 | MODULE_LICENSE("GPL" ); |
1827 | MODULE_AUTHOR("Ruslan Bilovol" ); |
1828 | |