1 // SPDX-License-Identifier: GPL-2.0+
3 * f_uac1.c -- USB Audio Class 1.0 Function (using u_audio API)
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.
12 * This file is based on f_uac1.c which is
14 * Copyright (C) 2008 Analog Devices, Inc
17 #include <linux/usb/audio.h>
18 #include <linux/module.h>
23 /* UAC1 spec: 3.7.2.3 Audio Channel Cluster Format */
24 #define UAC1_CHANNEL_MASK 0x0FFF
26 #define USB_OUT_FU_ID (out_feature_unit_desc->bUnitID)
27 #define USB_IN_FU_ID (in_feature_unit_desc->bUnitID)
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)
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() */
41 struct usb_ctrlrequest setup_cr; /* will be used in data stage */
43 /* Interrupt IN endpoint of AC interface */
44 struct usb_ep *int_ep;
46 int ctl_id; /* EP id */
47 int c_srate; /* current capture srate */
48 int p_srate; /* current playback prate */
51 static inline struct f_uac1 *func_to_uac1(struct usb_function *f)
53 return container_of(f, struct f_uac1, g_audio.func);
56 static inline struct f_uac1_opts *g_audio_to_uac1_opts(struct g_audio *audio)
58 return container_of(audio->func.fi, struct f_uac1_opts, func_inst);
62 * DESCRIPTORS ... most are static, but strings and full
63 * configuration descriptors are built on demand.
67 * We have three interfaces - one AudioControl and two AudioStreaming
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
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,
83 /* B.3.2 Class-Specific AC Interface Descriptor */
84 static struct uac1_ac_header_descriptor *ac_header_desc;
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),
93 .wChannelConfig = cpu_to_le16(0x3),
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),
103 /* .bSourceID = DYNAMIC */
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),
113 .wChannelConfig = cpu_to_le16(0x3),
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),
123 /* .bSourceID = DYNAMIC */
126 static struct uac_feature_unit_descriptor *in_feature_unit_desc;
127 static struct uac_feature_unit_descriptor *out_feature_unit_desc;
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),
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,
145 .bInterfaceClass = USB_CLASS_AUDIO,
146 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
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,
154 .bInterfaceClass = USB_CLASS_AUDIO,
155 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
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,
163 .bInterfaceClass = USB_CLASS_AUDIO,
164 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
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,
172 .bInterfaceClass = USB_CLASS_AUDIO,
173 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
176 /* B.4.2 Class-Specific AS Interface Descriptor */
177 static struct uac1_as_header_descriptor as_out_header_desc = {
178 .bLength = UAC_DT_AS_HEADER_SIZE,
179 .bDescriptorType = USB_DT_CS_INTERFACE,
180 .bDescriptorSubtype = UAC_AS_GENERAL,
181 /* .bTerminalLink = DYNAMIC */
183 .wFormatTag = cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
186 static struct uac1_as_header_descriptor as_in_header_desc = {
187 .bLength = UAC_DT_AS_HEADER_SIZE,
188 .bDescriptorType = USB_DT_CS_INTERFACE,
189 .bDescriptorSubtype = UAC_AS_GENERAL,
190 /* .bTerminalLink = DYNAMIC */
192 .wFormatTag = cpu_to_le16(UAC_FORMAT_TYPE_I_PCM),
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
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,
205 .bBitResolution = 16,
206 .bSamFreqType = 0, /* filled on rate setup */
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),
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,
226 .bLockDelayUnits = 1,
227 .wLockDelay = cpu_to_le16(1),
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,
236 .bBitResolution = 16,
237 .bSamFreqType = 0, /* filled on rate setup */
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),
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,
257 .bLockDelayUnits = 0,
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,
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,
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,
273 (struct usb_descriptor_header *)&ac_int_ep_desc,
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,
279 (struct usb_descriptor_header *)&as_out_type_i_desc,
281 (struct usb_descriptor_header *)&as_out_ep_desc,
282 (struct usb_descriptor_header *)&as_iso_out_desc,
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,
288 (struct usb_descriptor_header *)&as_in_type_i_desc,
290 (struct usb_descriptor_header *)&as_in_ep_desc,
291 (struct usb_descriptor_header *)&as_iso_in_desc,
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),
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,
311 /* wBytesPerInterval = DYNAMIC */
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),
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,
330 /* wBytesPerInterval = DYNAMIC */
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,
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,
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,
346 (struct usb_descriptor_header *)&as_out_type_i_desc,
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,
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,
357 (struct usb_descriptor_header *)&as_in_type_i_desc,
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,
369 STR_USB_OUT_IT_CH_NAMES,
372 STR_IO_IN_IT_CH_NAMES,
383 static struct usb_string strings_uac1[NUM_STR_DESCRIPTORS + 1] = {};
385 static struct usb_gadget_strings str_uac1 = {
386 .language = 0x0409, /* en-us */
387 .strings = strings_uac1,
390 static struct usb_gadget_strings *uac1_strings[] = {
396 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
399 static void uac_cs_attr_sample_rate(struct usb_ep *ep, struct usb_request *req)
401 struct usb_function *fn = ep->driver_data;
402 struct usb_composite_dev *cdev = fn->config->cdev;
403 struct g_audio *agdev = func_to_g_audio(fn);
404 struct f_uac1 *uac1 = func_to_uac1(fn);
405 u8 *buf = (u8 *)req->buf;
408 if (req->actual != 3) {
409 WARN(cdev, "Invalid data size for UAC_EP_CS_ATTR_SAMPLE_RATE.\n");
413 val = buf[0] | (buf[1] << 8) | (buf[2] << 16);
414 if (uac1->ctl_id == (USB_DIR_IN | 2)) {
416 u_audio_set_playback_srate(agdev, uac1->p_srate);
417 } else if (uac1->ctl_id == (USB_DIR_OUT | 1)) {
419 u_audio_set_capture_srate(agdev, uac1->c_srate);
423 static void audio_notify_complete(struct usb_ep *_ep, struct usb_request *req)
425 struct g_audio *audio = req->context;
426 struct f_uac1 *uac1 = func_to_uac1(&audio->func);
428 atomic_dec(&uac1->int_count);
430 usb_ep_free_request(_ep, req);
433 static int audio_notify(struct g_audio *audio, int unit_id, int cs)
435 struct f_uac1 *uac1 = func_to_uac1(&audio->func);
436 struct usb_request *req;
437 struct uac1_status_word *msg;
440 if (!uac1->int_ep->enabled)
443 if (atomic_inc_return(&uac1->int_count) > UAC1_DEF_INT_REQ_NUM) {
444 atomic_dec(&uac1->int_count);
448 req = usb_ep_alloc_request(uac1->int_ep, GFP_ATOMIC);
451 goto err_dec_int_count;
454 msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
457 goto err_free_request;
460 msg->bStatusType = UAC1_STATUS_TYPE_IRQ_PENDING
461 | UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF;
462 msg->bOriginator = unit_id;
464 req->length = sizeof(*msg);
466 req->context = audio;
467 req->complete = audio_notify_complete;
469 ret = usb_ep_queue(uac1->int_ep, req, GFP_ATOMIC);
479 usb_ep_free_request(uac1->int_ep, req);
481 atomic_dec(&uac1->int_count);
487 in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
489 struct usb_request *req = fn->config->cdev->req;
490 struct g_audio *audio = func_to_g_audio(fn);
491 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
492 u16 w_length = le16_to_cpu(cr->wLength);
493 u16 w_index = le16_to_cpu(cr->wIndex);
494 u16 w_value = le16_to_cpu(cr->wValue);
495 u8 entity_id = (w_index >> 8) & 0xff;
496 u8 control_selector = w_value >> 8;
497 int value = -EOPNOTSUPP;
499 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
500 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
501 unsigned int is_playback = 0;
503 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
506 if (control_selector == UAC_FU_MUTE) {
509 u_audio_get_mute(audio, is_playback, &mute);
511 *(u8 *)req->buf = mute;
512 value = min_t(unsigned int, w_length, 1);
513 } else if (control_selector == UAC_FU_VOLUME) {
517 u_audio_get_volume(audio, is_playback, &volume);
519 c = cpu_to_le16(volume);
521 value = min_t(unsigned int, w_length, sizeof(c));
522 memcpy(req->buf, &c, value);
524 dev_err(&audio->gadget->dev,
525 "%s:%d control_selector=%d TODO!\n",
526 __func__, __LINE__, control_selector);
529 dev_err(&audio->gadget->dev,
530 "%s:%d entity_id=%d control_selector=%d TODO!\n",
531 __func__, __LINE__, entity_id, control_selector);
538 in_rq_min(struct usb_function *fn, const struct usb_ctrlrequest *cr)
540 struct usb_request *req = fn->config->cdev->req;
541 struct g_audio *audio = func_to_g_audio(fn);
542 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
543 u16 w_length = le16_to_cpu(cr->wLength);
544 u16 w_index = le16_to_cpu(cr->wIndex);
545 u16 w_value = le16_to_cpu(cr->wValue);
546 u8 entity_id = (w_index >> 8) & 0xff;
547 u8 control_selector = w_value >> 8;
548 int value = -EOPNOTSUPP;
550 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
551 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
552 unsigned int is_playback = 0;
554 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
557 if (control_selector == UAC_FU_VOLUME) {
562 min_db = opts->p_volume_min;
564 min_db = opts->c_volume_min;
566 r = cpu_to_le16(min_db);
568 value = min_t(unsigned int, w_length, sizeof(r));
569 memcpy(req->buf, &r, value);
571 dev_err(&audio->gadget->dev,
572 "%s:%d control_selector=%d TODO!\n",
573 __func__, __LINE__, control_selector);
576 dev_err(&audio->gadget->dev,
577 "%s:%d entity_id=%d control_selector=%d TODO!\n",
578 __func__, __LINE__, entity_id, control_selector);
585 in_rq_max(struct usb_function *fn, const struct usb_ctrlrequest *cr)
587 struct usb_request *req = fn->config->cdev->req;
588 struct g_audio *audio = func_to_g_audio(fn);
589 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
590 u16 w_length = le16_to_cpu(cr->wLength);
591 u16 w_index = le16_to_cpu(cr->wIndex);
592 u16 w_value = le16_to_cpu(cr->wValue);
593 u8 entity_id = (w_index >> 8) & 0xff;
594 u8 control_selector = w_value >> 8;
595 int value = -EOPNOTSUPP;
597 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
598 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
599 unsigned int is_playback = 0;
601 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
604 if (control_selector == UAC_FU_VOLUME) {
609 max_db = opts->p_volume_max;
611 max_db = opts->c_volume_max;
613 r = cpu_to_le16(max_db);
615 value = min_t(unsigned int, w_length, sizeof(r));
616 memcpy(req->buf, &r, value);
618 dev_err(&audio->gadget->dev,
619 "%s:%d control_selector=%d TODO!\n",
620 __func__, __LINE__, control_selector);
623 dev_err(&audio->gadget->dev,
624 "%s:%d entity_id=%d control_selector=%d TODO!\n",
625 __func__, __LINE__, entity_id, control_selector);
632 in_rq_res(struct usb_function *fn, const struct usb_ctrlrequest *cr)
634 struct usb_request *req = fn->config->cdev->req;
635 struct g_audio *audio = func_to_g_audio(fn);
636 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
637 u16 w_length = le16_to_cpu(cr->wLength);
638 u16 w_index = le16_to_cpu(cr->wIndex);
639 u16 w_value = le16_to_cpu(cr->wValue);
640 u8 entity_id = (w_index >> 8) & 0xff;
641 u8 control_selector = w_value >> 8;
642 int value = -EOPNOTSUPP;
644 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
645 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
646 unsigned int is_playback = 0;
648 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
651 if (control_selector == UAC_FU_VOLUME) {
656 res_db = opts->p_volume_res;
658 res_db = opts->c_volume_res;
660 r = cpu_to_le16(res_db);
662 value = min_t(unsigned int, w_length, sizeof(r));
663 memcpy(req->buf, &r, value);
665 dev_err(&audio->gadget->dev,
666 "%s:%d control_selector=%d TODO!\n",
667 __func__, __LINE__, control_selector);
670 dev_err(&audio->gadget->dev,
671 "%s:%d entity_id=%d control_selector=%d TODO!\n",
672 __func__, __LINE__, entity_id, control_selector);
679 out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
681 struct g_audio *audio = req->context;
682 struct usb_composite_dev *cdev = audio->func.config->cdev;
683 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
684 struct f_uac1 *uac1 = func_to_uac1(&audio->func);
685 struct usb_ctrlrequest *cr = &uac1->setup_cr;
686 u16 w_index = le16_to_cpu(cr->wIndex);
687 u16 w_value = le16_to_cpu(cr->wValue);
688 u8 entity_id = (w_index >> 8) & 0xff;
689 u8 control_selector = w_value >> 8;
691 if (req->status != 0) {
692 dev_dbg(&cdev->gadget->dev, "completion err %d\n", req->status);
696 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
697 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
698 unsigned int is_playback = 0;
700 if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
703 if (control_selector == UAC_FU_MUTE) {
704 u8 mute = *(u8 *)req->buf;
706 u_audio_set_mute(audio, is_playback, mute);
709 } else if (control_selector == UAC_FU_VOLUME) {
710 __le16 *c = req->buf;
713 volume = le16_to_cpu(*c);
714 u_audio_set_volume(audio, is_playback, volume);
718 dev_err(&audio->gadget->dev,
719 "%s:%d control_selector=%d TODO!\n",
720 __func__, __LINE__, control_selector);
724 dev_err(&audio->gadget->dev,
725 "%s:%d entity_id=%d control_selector=%d TODO!\n",
726 __func__, __LINE__, entity_id, control_selector);
733 out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
735 struct usb_request *req = fn->config->cdev->req;
736 struct g_audio *audio = func_to_g_audio(fn);
737 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
738 struct f_uac1 *uac1 = func_to_uac1(&audio->func);
739 u16 w_length = le16_to_cpu(cr->wLength);
740 u16 w_index = le16_to_cpu(cr->wIndex);
741 u16 w_value = le16_to_cpu(cr->wValue);
742 u8 entity_id = (w_index >> 8) & 0xff;
743 u8 control_selector = w_value >> 8;
745 if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
746 (FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
747 memcpy(&uac1->setup_cr, cr, sizeof(*cr));
748 req->context = audio;
749 req->complete = out_rq_cur_complete;
753 dev_err(&audio->gadget->dev,
754 "%s:%d entity_id=%d control_selector=%d TODO!\n",
755 __func__, __LINE__, entity_id, control_selector);
760 static int ac_rq_in(struct usb_function *f,
761 const struct usb_ctrlrequest *ctrl)
763 struct usb_composite_dev *cdev = f->config->cdev;
764 int value = -EOPNOTSUPP;
765 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
766 u16 len = le16_to_cpu(ctrl->wLength);
767 u16 w_value = le16_to_cpu(ctrl->wValue);
769 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
770 ctrl->bRequest, w_value, len, ep);
772 switch (ctrl->bRequest) {
774 return in_rq_cur(f, ctrl);
776 return in_rq_min(f, ctrl);
778 return in_rq_max(f, ctrl);
780 return in_rq_res(f, ctrl);
793 static int audio_set_endpoint_req(struct usb_function *f,
794 const struct usb_ctrlrequest *ctrl)
796 struct usb_composite_dev *cdev = f->config->cdev;
797 struct usb_request *req = f->config->cdev->req;
798 struct f_uac1 *uac1 = func_to_uac1(f);
799 int value = -EOPNOTSUPP;
800 u16 ep = le16_to_cpu(ctrl->wIndex);
801 u16 len = le16_to_cpu(ctrl->wLength);
802 u16 w_value = le16_to_cpu(ctrl->wValue);
803 u8 cs = w_value >> 8;
805 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
806 ctrl->bRequest, w_value, len, ep);
808 switch (ctrl->bRequest) {
810 if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) {
811 cdev->gadget->ep0->driver_data = f;
813 req->complete = uac_cs_attr_sample_rate;
838 static int audio_get_endpoint_req(struct usb_function *f,
839 const struct usb_ctrlrequest *ctrl)
841 struct usb_composite_dev *cdev = f->config->cdev;
842 struct usb_request *req = f->config->cdev->req;
843 struct f_uac1 *uac1 = func_to_uac1(f);
844 u8 *buf = (u8 *)req->buf;
845 int value = -EOPNOTSUPP;
846 u8 ep = le16_to_cpu(ctrl->wIndex);
847 u16 len = le16_to_cpu(ctrl->wLength);
848 u16 w_value = le16_to_cpu(ctrl->wValue);
849 u8 cs = w_value >> 8;
852 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
853 ctrl->bRequest, w_value, len, ep);
855 switch (ctrl->bRequest) {
857 if (cs == UAC_EP_CS_ATTR_SAMPLE_RATE) {
858 if (ep == (USB_DIR_IN | 2))
860 else if (ep == (USB_DIR_OUT | 1))
862 buf[2] = (val >> 16) & 0xff;
863 buf[1] = (val >> 8) & 0xff;
884 f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
886 struct usb_composite_dev *cdev = f->config->cdev;
887 struct usb_request *req = cdev->req;
888 int value = -EOPNOTSUPP;
889 u16 w_index = le16_to_cpu(ctrl->wIndex);
890 u16 w_value = le16_to_cpu(ctrl->wValue);
891 u16 w_length = le16_to_cpu(ctrl->wLength);
893 /* composite driver infrastructure handles everything; interface
894 * activation uses set_alt().
896 switch (ctrl->bRequestType) {
897 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
898 value = audio_set_endpoint_req(f, ctrl);
901 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
902 value = audio_get_endpoint_req(f, ctrl);
904 case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
905 if (ctrl->bRequest == UAC_SET_CUR)
906 value = out_rq_cur(f, ctrl);
908 case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
909 value = ac_rq_in(f, ctrl);
912 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
913 ctrl->bRequestType, ctrl->bRequest,
914 w_value, w_index, w_length);
917 /* respond with data transfer or status phase? */
919 DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
920 ctrl->bRequestType, ctrl->bRequest,
921 w_value, w_index, w_length);
924 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
926 ERROR(cdev, "audio response on err %d\n", value);
929 /* device either stalls (value < 0) or reports success */
933 static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
935 struct usb_composite_dev *cdev = f->config->cdev;
936 struct usb_gadget *gadget = cdev->gadget;
937 struct device *dev = &gadget->dev;
938 struct g_audio *audio = func_to_g_audio(f);
939 struct f_uac1 *uac1 = func_to_uac1(f);
942 /* No i/f has more than 2 alt settings */
944 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
948 if (intf == uac1->ac_intf) {
949 /* Control I/f has only 1 AltSetting - 0 */
951 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
955 /* restart interrupt endpoint */
957 usb_ep_disable(uac1->int_ep);
958 config_ep_by_speed(gadget, &audio->func, uac1->int_ep);
959 usb_ep_enable(uac1->int_ep);
965 if (intf == uac1->as_out_intf) {
966 uac1->as_out_alt = alt;
969 ret = u_audio_start_capture(&uac1->g_audio);
971 u_audio_stop_capture(&uac1->g_audio);
972 } else if (intf == uac1->as_in_intf) {
973 uac1->as_in_alt = alt;
976 ret = u_audio_start_playback(&uac1->g_audio);
978 u_audio_stop_playback(&uac1->g_audio);
980 dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
987 static int f_audio_get_alt(struct usb_function *f, unsigned intf)
989 struct usb_composite_dev *cdev = f->config->cdev;
990 struct usb_gadget *gadget = cdev->gadget;
991 struct device *dev = &gadget->dev;
992 struct f_uac1 *uac1 = func_to_uac1(f);
994 if (intf == uac1->ac_intf)
996 else if (intf == uac1->as_out_intf)
997 return uac1->as_out_alt;
998 else if (intf == uac1->as_in_intf)
999 return uac1->as_in_alt;
1001 dev_err(dev, "%s:%d Invalid Interface %d!\n",
1002 __func__, __LINE__, intf);
1008 static void f_audio_disable(struct usb_function *f)
1010 struct f_uac1 *uac1 = func_to_uac1(f);
1012 uac1->as_out_alt = 0;
1013 uac1->as_in_alt = 0;
1015 u_audio_stop_playback(&uac1->g_audio);
1016 u_audio_stop_capture(&uac1->g_audio);
1018 usb_ep_disable(uac1->int_ep);
1022 f_audio_suspend(struct usb_function *f)
1024 struct f_uac1 *uac1 = func_to_uac1(f);
1026 u_audio_suspend(&uac1->g_audio);
1029 /*-------------------------------------------------------------------------*/
1030 static struct uac_feature_unit_descriptor *build_fu_desc(int chmask)
1032 struct uac_feature_unit_descriptor *fu_desc;
1033 int channels = num_channels(chmask);
1034 int fu_desc_size = UAC_DT_FEATURE_UNIT_SIZE(channels);
1036 fu_desc = kzalloc(fu_desc_size, GFP_KERNEL);
1040 fu_desc->bLength = fu_desc_size;
1041 fu_desc->bDescriptorType = USB_DT_CS_INTERFACE;
1043 fu_desc->bDescriptorSubtype = UAC_FEATURE_UNIT;
1044 fu_desc->bControlSize = 2;
1046 /* bUnitID, bSourceID and bmaControls will be defined later */
1051 /* B.3.2 Class-Specific AC Interface Descriptor */
1053 uac1_ac_header_descriptor *build_ac_header_desc(struct f_uac1_opts *opts)
1055 struct uac1_ac_header_descriptor *ac_desc;
1056 int ac_header_desc_size;
1064 ac_header_desc_size = UAC_DT_AC_HEADER_SIZE(num_ifaces);
1066 ac_desc = kzalloc(ac_header_desc_size, GFP_KERNEL);
1070 ac_desc->bLength = ac_header_desc_size;
1071 ac_desc->bDescriptorType = USB_DT_CS_INTERFACE;
1072 ac_desc->bDescriptorSubtype = UAC_HEADER;
1073 ac_desc->bcdADC = cpu_to_le16(0x0100);
1074 ac_desc->bInCollection = num_ifaces;
1076 /* wTotalLength and baInterfaceNr will be defined later */
1081 /* Use macro to overcome line length limitation */
1082 #define USBDHDR(p) (struct usb_descriptor_header *)(p)
1084 static void setup_descriptor(struct f_uac1_opts *opts)
1086 /* patch descriptors */
1087 int i = 1; /* ID's start with 1 */
1090 usb_out_it_desc.bTerminalID = i++;
1092 io_in_it_desc.bTerminalID = i++;
1094 io_out_ot_desc.bTerminalID = i++;
1096 usb_in_ot_desc.bTerminalID = i++;
1098 out_feature_unit_desc->bUnitID = i++;
1100 in_feature_unit_desc->bUnitID = i++;
1102 if (FUIN_EN(opts)) {
1103 usb_in_ot_desc.bSourceID = in_feature_unit_desc->bUnitID;
1104 in_feature_unit_desc->bSourceID = io_in_it_desc.bTerminalID;
1106 usb_in_ot_desc.bSourceID = io_in_it_desc.bTerminalID;
1108 if (FUOUT_EN(opts)) {
1109 io_out_ot_desc.bSourceID = out_feature_unit_desc->bUnitID;
1110 out_feature_unit_desc->bSourceID = usb_out_it_desc.bTerminalID;
1112 io_out_ot_desc.bSourceID = usb_out_it_desc.bTerminalID;
1115 as_out_header_desc.bTerminalLink = usb_out_it_desc.bTerminalID;
1116 as_in_header_desc.bTerminalLink = usb_in_ot_desc.bTerminalID;
1118 ac_header_desc->wTotalLength = cpu_to_le16(ac_header_desc->bLength);
1120 if (EPIN_EN(opts)) {
1121 u16 len = le16_to_cpu(ac_header_desc->wTotalLength);
1123 len += sizeof(usb_in_ot_desc);
1124 len += sizeof(io_in_it_desc);
1126 len += in_feature_unit_desc->bLength;
1127 ac_header_desc->wTotalLength = cpu_to_le16(len);
1129 if (EPOUT_EN(opts)) {
1130 u16 len = le16_to_cpu(ac_header_desc->wTotalLength);
1132 len += sizeof(usb_out_it_desc);
1133 len += sizeof(io_out_ot_desc);
1135 len += out_feature_unit_desc->bLength;
1136 ac_header_desc->wTotalLength = cpu_to_le16(len);
1140 f_audio_desc[i++] = USBDHDR(&ac_interface_desc);
1141 f_audio_desc[i++] = USBDHDR(ac_header_desc);
1143 if (EPOUT_EN(opts)) {
1144 f_audio_desc[i++] = USBDHDR(&usb_out_it_desc);
1145 f_audio_desc[i++] = USBDHDR(&io_out_ot_desc);
1147 f_audio_desc[i++] = USBDHDR(out_feature_unit_desc);
1150 if (EPIN_EN(opts)) {
1151 f_audio_desc[i++] = USBDHDR(&io_in_it_desc);
1152 f_audio_desc[i++] = USBDHDR(&usb_in_ot_desc);
1154 f_audio_desc[i++] = USBDHDR(in_feature_unit_desc);
1157 if (FUOUT_EN(opts) || FUIN_EN(opts))
1158 f_audio_desc[i++] = USBDHDR(&ac_int_ep_desc);
1160 if (EPOUT_EN(opts)) {
1161 f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_0_desc);
1162 f_audio_desc[i++] = USBDHDR(&as_out_interface_alt_1_desc);
1163 f_audio_desc[i++] = USBDHDR(&as_out_header_desc);
1164 f_audio_desc[i++] = USBDHDR(&as_out_type_i_desc);
1165 f_audio_desc[i++] = USBDHDR(&as_out_ep_desc);
1166 f_audio_desc[i++] = USBDHDR(&as_iso_out_desc);
1168 if (EPIN_EN(opts)) {
1169 f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_0_desc);
1170 f_audio_desc[i++] = USBDHDR(&as_in_interface_alt_1_desc);
1171 f_audio_desc[i++] = USBDHDR(&as_in_header_desc);
1172 f_audio_desc[i++] = USBDHDR(&as_in_type_i_desc);
1173 f_audio_desc[i++] = USBDHDR(&as_in_ep_desc);
1174 f_audio_desc[i++] = USBDHDR(&as_iso_in_desc);
1176 f_audio_desc[i] = NULL;
1179 static int f_audio_validate_opts(struct g_audio *audio, struct device *dev)
1181 struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio);
1183 if (!opts->p_chmask && !opts->c_chmask) {
1184 dev_err(dev, "Error: no playback and capture channels\n");
1186 } else if (opts->p_chmask & ~UAC1_CHANNEL_MASK) {
1187 dev_err(dev, "Error: unsupported playback channels mask\n");
1189 } else if (opts->c_chmask & ~UAC1_CHANNEL_MASK) {
1190 dev_err(dev, "Error: unsupported capture channels mask\n");
1192 } else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) {
1193 dev_err(dev, "Error: incorrect playback sample size\n");
1195 } else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) {
1196 dev_err(dev, "Error: incorrect capture sample size\n");
1198 } else if (!opts->p_srates[0]) {
1199 dev_err(dev, "Error: incorrect playback sampling rate\n");
1201 } else if (!opts->c_srates[0]) {
1202 dev_err(dev, "Error: incorrect capture sampling rate\n");
1206 if (opts->p_volume_max <= opts->p_volume_min) {
1207 dev_err(dev, "Error: incorrect playback volume max/min\n");
1209 } else if (opts->c_volume_max <= opts->c_volume_min) {
1210 dev_err(dev, "Error: incorrect capture volume max/min\n");
1212 } else if (opts->p_volume_res <= 0) {
1213 dev_err(dev, "Error: negative/zero playback volume resolution\n");
1215 } else if (opts->c_volume_res <= 0) {
1216 dev_err(dev, "Error: negative/zero capture volume resolution\n");
1220 if ((opts->p_volume_max - opts->p_volume_min) % opts->p_volume_res) {
1221 dev_err(dev, "Error: incorrect playback volume resolution\n");
1223 } else if ((opts->c_volume_max - opts->c_volume_min) % opts->c_volume_res) {
1224 dev_err(dev, "Error: incorrect capture volume resolution\n");
1231 /* audio function driver setup/binding */
1232 static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
1234 struct usb_composite_dev *cdev = c->cdev;
1235 struct usb_gadget *gadget = cdev->gadget;
1236 struct device *dev = &gadget->dev;
1237 struct f_uac1 *uac1 = func_to_uac1(f);
1238 struct g_audio *audio = func_to_g_audio(f);
1239 struct f_uac1_opts *audio_opts;
1240 struct usb_ep *ep = NULL;
1241 struct usb_string *us;
1246 status = f_audio_validate_opts(audio, dev);
1250 audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst);
1252 strings_uac1[STR_AC_IF].s = audio_opts->function_name;
1254 strings_uac1[STR_USB_OUT_IT].s = audio_opts->c_it_name;
1255 strings_uac1[STR_USB_OUT_IT_CH_NAMES].s = audio_opts->c_it_ch_name;
1256 strings_uac1[STR_IO_OUT_OT].s = audio_opts->c_ot_name;
1257 strings_uac1[STR_FU_OUT].s = audio_opts->c_fu_vol_name;
1258 strings_uac1[STR_AS_OUT_IF_ALT0].s = "Playback Inactive";
1259 strings_uac1[STR_AS_OUT_IF_ALT1].s = "Playback Active";
1261 strings_uac1[STR_IO_IN_IT].s = audio_opts->p_it_name;
1262 strings_uac1[STR_IO_IN_IT_CH_NAMES].s = audio_opts->p_it_ch_name;
1263 strings_uac1[STR_USB_IN_OT].s = audio_opts->p_ot_name;
1264 strings_uac1[STR_FU_IN].s = audio_opts->p_fu_vol_name;
1265 strings_uac1[STR_AS_IN_IF_ALT0].s = "Capture Inactive";
1266 strings_uac1[STR_AS_IN_IF_ALT1].s = "Capture Active";
1268 us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
1272 ac_header_desc = build_ac_header_desc(audio_opts);
1273 if (!ac_header_desc)
1276 if (FUOUT_EN(audio_opts)) {
1277 out_feature_unit_desc = build_fu_desc(audio_opts->c_chmask);
1278 if (!out_feature_unit_desc) {
1283 if (FUIN_EN(audio_opts)) {
1284 in_feature_unit_desc = build_fu_desc(audio_opts->p_chmask);
1285 if (!in_feature_unit_desc) {
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;
1303 if (FUOUT_EN(audio_opts)) {
1306 i_feature = (u8 *)out_feature_unit_desc +
1307 out_feature_unit_desc->bLength - 1;
1308 *i_feature = us[STR_FU_OUT].id;
1310 if (FUIN_EN(audio_opts)) {
1313 i_feature = (u8 *)in_feature_unit_desc +
1314 in_feature_unit_desc->bLength - 1;
1315 *i_feature = us[STR_FU_IN].id;
1318 /* Set channel numbers */
1319 usb_out_it_desc.bNrChannels = num_channels(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(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(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(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;
1330 if (FUOUT_EN(audio_opts)) {
1331 __le16 *bma = (__le16 *)&out_feature_unit_desc->bmaControls[0];
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);
1340 if (FUIN_EN(audio_opts)) {
1341 __le16 *bma = (__le16 *)&in_feature_unit_desc->bmaControls[0];
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);
1351 /* Set sample rates */
1352 for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) {
1353 if (audio_opts->c_srates[i] == 0)
1355 memcpy(as_out_type_i_desc.tSamFreq[idx++],
1356 &audio_opts->c_srates[i], 3);
1358 as_out_type_i_desc.bLength = UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(idx);
1359 as_out_type_i_desc.bSamFreqType = idx;
1361 for (i = 0, idx = 0; i < UAC_MAX_RATES; i++) {
1362 if (audio_opts->p_srates[i] == 0)
1364 memcpy(as_in_type_i_desc.tSamFreq[idx++],
1365 &audio_opts->p_srates[i], 3);
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];
1372 /* allocate instance-specific interface IDs, and patch descriptors */
1373 status = usb_interface_id(c, f);
1376 ac_interface_desc.bInterfaceNumber = status;
1377 uac1->ac_intf = status;
1382 if (EPOUT_EN(audio_opts)) {
1383 status = usb_interface_id(c, f);
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;
1393 if (EPIN_EN(audio_opts)) {
1394 status = usb_interface_id(c, f);
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;
1404 audio->gadget = gadget;
1408 ac_interface_desc.bNumEndpoints = 0;
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);
1416 uac1->int_ep->desc = &ac_int_ep_desc;
1418 ac_interface_desc.bNumEndpoints = 1;
1421 /* allocate instance-specific endpoints */
1422 if (EPOUT_EN(audio_opts)) {
1423 ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
1426 ss_as_out_ep_desc.bEndpointAddress = as_out_ep_desc.bEndpointAddress;
1428 audio->out_ep->desc = &as_out_ep_desc;
1431 if (EPIN_EN(audio_opts)) {
1432 ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc);
1435 ss_as_in_ep_desc.bEndpointAddress = as_in_ep_desc.bEndpointAddress;
1437 audio->in_ep->desc = &as_in_ep_desc;
1440 setup_descriptor(audio_opts);
1442 /* copy descriptors, and track endpoint copies */
1443 status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, f_audio_ss_desc,
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;
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;
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;
1481 status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget");
1483 goto err_card_register;
1488 usb_free_all_descriptors(f);
1490 kfree(out_feature_unit_desc);
1491 out_feature_unit_desc = NULL;
1492 kfree(in_feature_unit_desc);
1493 in_feature_unit_desc = NULL;
1495 kfree(ac_header_desc);
1496 ac_header_desc = NULL;
1500 /*-------------------------------------------------------------------------*/
1502 static inline struct f_uac1_opts *to_f_uac1_opts(struct config_item *item)
1504 return container_of(to_config_group(item), struct f_uac1_opts,
1508 static void f_uac1_attr_release(struct config_item *item)
1510 struct f_uac1_opts *opts = to_f_uac1_opts(item);
1512 usb_put_function_instance(&opts->func_inst);
1515 static struct configfs_item_operations f_uac1_item_ops = {
1516 .release = f_uac1_attr_release,
1519 #define uac1_kstrtou32 kstrtou32
1520 #define uac1_kstrtos16 kstrtos16
1521 #define uac1_kstrtobool(s, base, res) kstrtobool((s), (res))
1523 static const char *u32_fmt = "%u\n";
1524 static const char *s16_fmt = "%hd\n";
1525 static const char *bool_fmt = "%u\n";
1527 #define UAC1_ATTRIBUTE(type, name) \
1528 static ssize_t f_uac1_opts_##name##_show( \
1529 struct config_item *item, \
1532 struct f_uac1_opts *opts = to_f_uac1_opts(item); \
1535 mutex_lock(&opts->lock); \
1536 result = sprintf(page, type##_fmt, opts->name); \
1537 mutex_unlock(&opts->lock); \
1542 static ssize_t f_uac1_opts_##name##_store( \
1543 struct config_item *item, \
1544 const char *page, size_t len) \
1546 struct f_uac1_opts *opts = to_f_uac1_opts(item); \
1550 mutex_lock(&opts->lock); \
1551 if (opts->refcnt) { \
1556 ret = uac1_kstrto##type(page, 0, &num); \
1564 mutex_unlock(&opts->lock); \
1568 CONFIGFS_ATTR(f_uac1_opts_, name)
1570 #define UAC1_RATE_ATTRIBUTE(name) \
1571 static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
1574 struct f_uac1_opts *opts = to_f_uac1_opts(item); \
1578 mutex_lock(&opts->lock); \
1580 for (i = 0; i < UAC_MAX_RATES; i++) { \
1581 if (opts->name##s[i] == 0) \
1583 result += sprintf(page + strlen(page), "%u,", \
1584 opts->name##s[i]); \
1586 if (strlen(page) > 0) \
1587 page[strlen(page) - 1] = '\n'; \
1588 mutex_unlock(&opts->lock); \
1593 static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
1594 const char *page, size_t len) \
1596 struct f_uac1_opts *opts = to_f_uac1_opts(item); \
1597 char *split_page = NULL; \
1598 int ret = -EINVAL; \
1603 mutex_lock(&opts->lock); \
1604 if (opts->refcnt) { \
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); \
1617 opts->name##s[i++] = num; \
1622 kfree(split_page); \
1623 mutex_unlock(&opts->lock); \
1627 CONFIGFS_ATTR(f_uac1_opts_, name)
1629 #define UAC1_ATTRIBUTE_STRING(name) \
1630 static ssize_t f_uac1_opts_##name##_show(struct config_item *item, \
1633 struct f_uac1_opts *opts = to_f_uac1_opts(item); \
1636 mutex_lock(&opts->lock); \
1637 result = scnprintf(page, sizeof(opts->name), "%s", opts->name); \
1638 mutex_unlock(&opts->lock); \
1643 static ssize_t f_uac1_opts_##name##_store(struct config_item *item, \
1644 const char *page, size_t len) \
1646 struct f_uac1_opts *opts = to_f_uac1_opts(item); \
1649 mutex_lock(&opts->lock); \
1650 if (opts->refcnt) { \
1655 ret = scnprintf(opts->name, min(sizeof(opts->name), len), \
1659 mutex_unlock(&opts->lock); \
1663 CONFIGFS_ATTR(f_uac1_opts_, name)
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);
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);
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);
1685 UAC1_ATTRIBUTE_STRING(function_name);
1687 UAC1_ATTRIBUTE_STRING(p_it_name);
1688 UAC1_ATTRIBUTE_STRING(p_it_ch_name);
1689 UAC1_ATTRIBUTE_STRING(p_ot_name);
1690 UAC1_ATTRIBUTE_STRING(p_fu_vol_name);
1692 UAC1_ATTRIBUTE_STRING(c_it_name);
1693 UAC1_ATTRIBUTE_STRING(c_it_ch_name);
1694 UAC1_ATTRIBUTE_STRING(c_ot_name);
1695 UAC1_ATTRIBUTE_STRING(c_fu_vol_name);
1697 static struct configfs_attribute *f_uac1_attrs[] = {
1698 &f_uac1_opts_attr_c_chmask,
1699 &f_uac1_opts_attr_c_srate,
1700 &f_uac1_opts_attr_c_ssize,
1701 &f_uac1_opts_attr_p_chmask,
1702 &f_uac1_opts_attr_p_srate,
1703 &f_uac1_opts_attr_p_ssize,
1704 &f_uac1_opts_attr_req_number,
1706 &f_uac1_opts_attr_p_mute_present,
1707 &f_uac1_opts_attr_p_volume_present,
1708 &f_uac1_opts_attr_p_volume_min,
1709 &f_uac1_opts_attr_p_volume_max,
1710 &f_uac1_opts_attr_p_volume_res,
1712 &f_uac1_opts_attr_c_mute_present,
1713 &f_uac1_opts_attr_c_volume_present,
1714 &f_uac1_opts_attr_c_volume_min,
1715 &f_uac1_opts_attr_c_volume_max,
1716 &f_uac1_opts_attr_c_volume_res,
1718 &f_uac1_opts_attr_function_name,
1720 &f_uac1_opts_attr_p_it_name,
1721 &f_uac1_opts_attr_p_it_ch_name,
1722 &f_uac1_opts_attr_p_ot_name,
1723 &f_uac1_opts_attr_p_fu_vol_name,
1725 &f_uac1_opts_attr_c_it_name,
1726 &f_uac1_opts_attr_c_it_ch_name,
1727 &f_uac1_opts_attr_c_ot_name,
1728 &f_uac1_opts_attr_c_fu_vol_name,
1733 static const struct config_item_type f_uac1_func_type = {
1734 .ct_item_ops = &f_uac1_item_ops,
1735 .ct_attrs = f_uac1_attrs,
1736 .ct_owner = THIS_MODULE,
1739 static void f_audio_free_inst(struct usb_function_instance *f)
1741 struct f_uac1_opts *opts;
1743 opts = container_of(f, struct f_uac1_opts, func_inst);
1747 static struct usb_function_instance *f_audio_alloc_inst(void)
1749 struct f_uac1_opts *opts;
1751 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1753 return ERR_PTR(-ENOMEM);
1755 mutex_init(&opts->lock);
1756 opts->func_inst.free_func_inst = f_audio_free_inst;
1758 config_group_init_type_name(&opts->func_inst.group, "",
1761 opts->c_chmask = UAC1_DEF_CCHMASK;
1762 opts->c_srates[0] = UAC1_DEF_CSRATE;
1763 opts->c_ssize = UAC1_DEF_CSSIZE;
1764 opts->p_chmask = UAC1_DEF_PCHMASK;
1765 opts->p_srates[0] = UAC1_DEF_PSRATE;
1766 opts->p_ssize = UAC1_DEF_PSSIZE;
1768 opts->p_mute_present = UAC1_DEF_MUTE_PRESENT;
1769 opts->p_volume_present = UAC1_DEF_VOLUME_PRESENT;
1770 opts->p_volume_min = UAC1_DEF_MIN_DB;
1771 opts->p_volume_max = UAC1_DEF_MAX_DB;
1772 opts->p_volume_res = UAC1_DEF_RES_DB;
1774 opts->c_mute_present = UAC1_DEF_MUTE_PRESENT;
1775 opts->c_volume_present = UAC1_DEF_VOLUME_PRESENT;
1776 opts->c_volume_min = UAC1_DEF_MIN_DB;
1777 opts->c_volume_max = UAC1_DEF_MAX_DB;
1778 opts->c_volume_res = UAC1_DEF_RES_DB;
1780 opts->req_number = UAC1_DEF_REQ_NUM;
1782 scnprintf(opts->function_name, sizeof(opts->function_name), "AC Interface");
1784 scnprintf(opts->p_it_name, sizeof(opts->p_it_name), "Capture Input terminal");
1785 scnprintf(opts->p_it_ch_name, sizeof(opts->p_it_ch_name), "Capture Channels");
1786 scnprintf(opts->p_ot_name, sizeof(opts->p_ot_name), "Capture Output terminal");
1787 scnprintf(opts->p_fu_vol_name, sizeof(opts->p_fu_vol_name), "Capture Volume");
1789 scnprintf(opts->c_it_name, sizeof(opts->c_it_name), "Playback Input terminal");
1790 scnprintf(opts->c_it_ch_name, sizeof(opts->c_it_ch_name), "Playback Channels");
1791 scnprintf(opts->c_ot_name, sizeof(opts->c_ot_name), "Playback Output terminal");
1792 scnprintf(opts->c_fu_vol_name, sizeof(opts->c_fu_vol_name), "Playback Volume");
1794 return &opts->func_inst;
1797 static void f_audio_free(struct usb_function *f)
1799 struct g_audio *audio;
1800 struct f_uac1_opts *opts;
1802 audio = func_to_g_audio(f);
1803 opts = container_of(f->fi, struct f_uac1_opts, func_inst);
1805 mutex_lock(&opts->lock);
1807 mutex_unlock(&opts->lock);
1810 static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
1812 struct g_audio *audio = func_to_g_audio(f);
1814 g_audio_cleanup(audio);
1815 usb_free_all_descriptors(f);
1817 kfree(out_feature_unit_desc);
1818 out_feature_unit_desc = NULL;
1819 kfree(in_feature_unit_desc);
1820 in_feature_unit_desc = NULL;
1822 kfree(ac_header_desc);
1823 ac_header_desc = NULL;
1825 audio->gadget = NULL;
1828 static struct usb_function *f_audio_alloc(struct usb_function_instance *fi)
1830 struct f_uac1 *uac1;
1831 struct f_uac1_opts *opts;
1833 /* allocate and initialize one new instance */
1834 uac1 = kzalloc(sizeof(*uac1), GFP_KERNEL);
1836 return ERR_PTR(-ENOMEM);
1838 opts = container_of(fi, struct f_uac1_opts, func_inst);
1839 mutex_lock(&opts->lock);
1841 mutex_unlock(&opts->lock);
1843 uac1->g_audio.func.name = "uac1_func";
1844 uac1->g_audio.func.bind = f_audio_bind;
1845 uac1->g_audio.func.unbind = f_audio_unbind;
1846 uac1->g_audio.func.set_alt = f_audio_set_alt;
1847 uac1->g_audio.func.get_alt = f_audio_get_alt;
1848 uac1->g_audio.func.setup = f_audio_setup;
1849 uac1->g_audio.func.disable = f_audio_disable;
1850 uac1->g_audio.func.suspend = f_audio_suspend;
1851 uac1->g_audio.func.free_func = f_audio_free;
1853 return &uac1->g_audio.func;
1856 DECLARE_USB_FUNCTION_INIT(uac1, f_audio_alloc_inst, f_audio_alloc);
1857 MODULE_DESCRIPTION("USB Audio Class 1.0 Function (using u_audio API)");
1858 MODULE_LICENSE("GPL");
1859 MODULE_AUTHOR("Ruslan Bilovol");