Lines Matching +full:ctrl +full:- +full:len
1 // SPDX-License-Identifier: GPL-2.0+
3 * f_audio.c -- USB Audio class function driver
26 * We have two interfaces- AudioControl and AudioStreaming
52 /* B.3.2 Class-Specific AC Interface Descriptor */
146 /* B.4.2 Class-Specific AS Interface Descriptor */
179 /* Class-specific AS ISO OUT Endpoint Descriptor */
230 .language = 0x0409, /* en-us */
243 /*-------------------------------------------------------------------------*/
256 return ERR_PTR(-ENOMEM); in f_audio_buffer_alloc()
258 copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC); in f_audio_buffer_alloc()
259 if (!copy_buf->buf) { in f_audio_buffer_alloc()
261 return ERR_PTR(-ENOMEM); in f_audio_buffer_alloc()
269 kfree(audio_buf->buf); in f_audio_buffer_free()
272 /*-------------------------------------------------------------------------*/
299 /*-------------------------------------------------------------------------*/
307 spin_lock_irq(&audio->lock); in f_audio_playback_work()
308 if (list_empty(&audio->play_queue)) { in f_audio_playback_work()
309 spin_unlock_irq(&audio->lock); in f_audio_playback_work()
312 play_buf = list_first_entry(&audio->play_queue, in f_audio_playback_work()
314 list_del(&play_buf->list); in f_audio_playback_work()
315 spin_unlock_irq(&audio->lock); in f_audio_playback_work()
317 u_audio_playback(&audio->card, play_buf->buf, play_buf->actual); in f_audio_playback_work()
323 struct f_audio *audio = req->context; in f_audio_out_ep_complete()
324 struct usb_composite_dev *cdev = audio->card.func.config->cdev; in f_audio_out_ep_complete()
325 struct f_audio_buf *copy_buf = audio->copy_buf; in f_audio_out_ep_complete()
330 opts = container_of(audio->card.func.fi, struct f_uac1_legacy_opts, in f_audio_out_ep_complete()
332 audio_buf_size = opts->audio_buf_size; in f_audio_out_ep_complete()
335 return -EINVAL; in f_audio_out_ep_complete()
338 if (audio_buf_size - copy_buf->actual < req->actual) { in f_audio_out_ep_complete()
339 spin_lock_irq(&audio->lock); in f_audio_out_ep_complete()
340 list_add_tail(©_buf->list, &audio->play_queue); in f_audio_out_ep_complete()
341 spin_unlock_irq(&audio->lock); in f_audio_out_ep_complete()
342 schedule_work(&audio->playback_work); in f_audio_out_ep_complete()
345 return -ENOMEM; in f_audio_out_ep_complete()
348 memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual); in f_audio_out_ep_complete()
349 copy_buf->actual += req->actual; in f_audio_out_ep_complete()
350 audio->copy_buf = copy_buf; in f_audio_out_ep_complete()
354 ERROR(cdev, "%s queue req: %d\n", ep->name, err); in f_audio_out_ep_complete()
362 struct f_audio *audio = req->context; in f_audio_complete()
363 int status = req->status; in f_audio_complete()
365 struct usb_ep *out_ep = audio->out_ep; in f_audio_complete()
372 else if (audio->set_con) { in f_audio_complete()
373 memcpy(&data, req->buf, req->length); in f_audio_complete()
374 audio->set_con->set(audio->set_con, audio->set_cmd, in f_audio_complete()
376 audio->set_con = NULL; in f_audio_complete()
385 const struct usb_ctrlrequest *ctrl) in audio_set_intf_req() argument
388 struct usb_composite_dev *cdev = f->config->cdev; in audio_set_intf_req()
389 struct usb_request *req = cdev->req; in audio_set_intf_req()
390 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); in audio_set_intf_req()
391 u16 len = le16_to_cpu(ctrl->wLength); in audio_set_intf_req() local
392 u16 w_value = le16_to_cpu(ctrl->wValue); in audio_set_intf_req()
394 u8 cmd = (ctrl->bRequest & 0x0F); in audio_set_intf_req()
398 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", in audio_set_intf_req()
399 ctrl->bRequest, w_value, len, id); in audio_set_intf_req()
401 list_for_each_entry(cs, &audio->cs, list) { in audio_set_intf_req()
402 if (cs->id == id) { in audio_set_intf_req()
403 list_for_each_entry(con, &cs->control, list) { in audio_set_intf_req()
404 if (con->type == con_sel) { in audio_set_intf_req()
405 audio->set_con = con; in audio_set_intf_req()
413 audio->set_cmd = cmd; in audio_set_intf_req()
414 req->context = audio; in audio_set_intf_req()
415 req->complete = f_audio_complete; in audio_set_intf_req()
417 return len; in audio_set_intf_req()
421 const struct usb_ctrlrequest *ctrl) in audio_get_intf_req() argument
424 struct usb_composite_dev *cdev = f->config->cdev; in audio_get_intf_req()
425 struct usb_request *req = cdev->req; in audio_get_intf_req()
426 int value = -EOPNOTSUPP; in audio_get_intf_req()
427 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); in audio_get_intf_req()
428 u16 len = le16_to_cpu(ctrl->wLength); in audio_get_intf_req() local
429 u16 w_value = le16_to_cpu(ctrl->wValue); in audio_get_intf_req()
431 u8 cmd = (ctrl->bRequest & 0x0F); in audio_get_intf_req()
435 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n", in audio_get_intf_req()
436 ctrl->bRequest, w_value, len, id); in audio_get_intf_req()
438 list_for_each_entry(cs, &audio->cs, list) { in audio_get_intf_req()
439 if (cs->id == id) { in audio_get_intf_req()
440 list_for_each_entry(con, &cs->control, list) { in audio_get_intf_req()
441 if (con->type == con_sel && con->get) { in audio_get_intf_req()
442 value = con->get(con, cmd); in audio_get_intf_req()
450 req->context = audio; in audio_get_intf_req()
451 req->complete = f_audio_complete; in audio_get_intf_req()
452 len = min_t(size_t, sizeof(value), len); in audio_get_intf_req()
453 memcpy(req->buf, &value, len); in audio_get_intf_req()
455 return len; in audio_get_intf_req()
459 const struct usb_ctrlrequest *ctrl) in audio_set_endpoint_req() argument
461 struct usb_composite_dev *cdev = f->config->cdev; in audio_set_endpoint_req()
462 int value = -EOPNOTSUPP; in audio_set_endpoint_req()
463 u16 ep = le16_to_cpu(ctrl->wIndex); in audio_set_endpoint_req()
464 u16 len = le16_to_cpu(ctrl->wLength); in audio_set_endpoint_req() local
465 u16 w_value = le16_to_cpu(ctrl->wValue); in audio_set_endpoint_req()
467 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", in audio_set_endpoint_req()
468 ctrl->bRequest, w_value, len, ep); in audio_set_endpoint_req()
470 switch (ctrl->bRequest) { in audio_set_endpoint_req()
472 value = len; in audio_set_endpoint_req()
495 const struct usb_ctrlrequest *ctrl) in audio_get_endpoint_req() argument
497 struct usb_composite_dev *cdev = f->config->cdev; in audio_get_endpoint_req()
498 int value = -EOPNOTSUPP; in audio_get_endpoint_req()
499 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF); in audio_get_endpoint_req()
500 u16 len = le16_to_cpu(ctrl->wLength); in audio_get_endpoint_req() local
501 u16 w_value = le16_to_cpu(ctrl->wValue); in audio_get_endpoint_req()
503 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n", in audio_get_endpoint_req()
504 ctrl->bRequest, w_value, len, ep); in audio_get_endpoint_req()
506 switch (ctrl->bRequest) { in audio_get_endpoint_req()
511 value = len; in audio_get_endpoint_req()
523 f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) in f_audio_setup() argument
525 struct usb_composite_dev *cdev = f->config->cdev; in f_audio_setup()
526 struct usb_request *req = cdev->req; in f_audio_setup()
527 int value = -EOPNOTSUPP; in f_audio_setup()
528 u16 w_index = le16_to_cpu(ctrl->wIndex); in f_audio_setup()
529 u16 w_value = le16_to_cpu(ctrl->wValue); in f_audio_setup()
530 u16 w_length = le16_to_cpu(ctrl->wLength); in f_audio_setup()
535 switch (ctrl->bRequestType) { in f_audio_setup()
537 value = audio_set_intf_req(f, ctrl); in f_audio_setup()
541 value = audio_get_intf_req(f, ctrl); in f_audio_setup()
545 value = audio_set_endpoint_req(f, ctrl); in f_audio_setup()
549 value = audio_get_endpoint_req(f, ctrl); in f_audio_setup()
554 ctrl->bRequestType, ctrl->bRequest, in f_audio_setup()
561 ctrl->bRequestType, ctrl->bRequest, in f_audio_setup()
563 req->zero = 0; in f_audio_setup()
564 req->length = value; in f_audio_setup()
565 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); in f_audio_setup()
577 struct usb_composite_dev *cdev = f->config->cdev; in f_audio_set_alt()
578 struct usb_ep *out_ep = audio->out_ep; in f_audio_set_alt()
586 opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); in f_audio_set_alt()
587 req_buf_size = opts->req_buf_size; in f_audio_set_alt()
588 req_count = opts->req_count; in f_audio_set_alt()
589 audio_buf_size = opts->audio_buf_size; in f_audio_set_alt()
594 return -EINVAL; in f_audio_set_alt()
597 if (intf == audio->ac_intf) { in f_audio_set_alt()
598 /* Control I/f has only 1 AltSetting - 0 */ in f_audio_set_alt()
601 return -EINVAL; in f_audio_set_alt()
604 } else if (intf == audio->as_intf) { in f_audio_set_alt()
606 err = config_ep_by_speed(cdev->gadget, f, out_ep); in f_audio_set_alt()
611 audio->copy_buf = f_audio_buffer_alloc(audio_buf_size); in f_audio_set_alt()
612 if (IS_ERR(audio->copy_buf)) in f_audio_set_alt()
613 return -ENOMEM; in f_audio_set_alt()
622 req->buf = kzalloc(req_buf_size, in f_audio_set_alt()
624 if (req->buf) { in f_audio_set_alt()
625 req->length = req_buf_size; in f_audio_set_alt()
626 req->context = audio; in f_audio_set_alt()
627 req->complete = in f_audio_set_alt()
634 out_ep->name, err); in f_audio_set_alt()
636 err = -ENOMEM; in f_audio_set_alt()
638 err = -ENOMEM; in f_audio_set_alt()
642 struct f_audio_buf *copy_buf = audio->copy_buf; in f_audio_set_alt()
644 list_add_tail(©_buf->list, in f_audio_set_alt()
645 &audio->play_queue); in f_audio_set_alt()
646 schedule_work(&audio->playback_work); in f_audio_set_alt()
649 audio->as_alt = alt; in f_audio_set_alt()
658 struct usb_composite_dev *cdev = f->config->cdev; in f_audio_get_alt()
660 if (intf == audio->ac_intf) in f_audio_get_alt()
661 return audio->ac_alt; in f_audio_get_alt()
662 else if (intf == audio->as_intf) in f_audio_get_alt()
663 return audio->as_alt; in f_audio_get_alt()
668 return -EINVAL; in f_audio_get_alt()
676 /*-------------------------------------------------------------------------*/
680 struct gaudio *card = &audio->card; in f_audio_build_desc()
702 struct usb_composite_dev *cdev = c->cdev; in f_audio_bind()
709 audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); in f_audio_bind()
710 audio->card.gadget = c->cdev->gadget; in f_audio_bind()
712 if (!audio_opts->bound) { in f_audio_bind()
713 status = gaudio_setup(&audio->card); in f_audio_bind()
716 audio_opts->bound = true; in f_audio_bind()
732 /* allocate instance-specific interface IDs, and patch descriptors */ in f_audio_bind()
737 audio->ac_intf = status; in f_audio_bind()
738 audio->ac_alt = 0; in f_audio_bind()
745 audio->as_intf = status; in f_audio_bind()
746 audio->as_alt = 0; in f_audio_bind()
748 status = -ENODEV; in f_audio_bind()
750 /* allocate instance-specific endpoints */ in f_audio_bind()
751 ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc); in f_audio_bind()
754 audio->out_ep = ep; in f_audio_bind()
755 audio->out_ep->desc = &as_out_ep_desc; in f_audio_bind()
765 gaudio_cleanup(&audio->card); in f_audio_bind()
769 /*-------------------------------------------------------------------------*/
773 con->data[cmd] = value; in generic_set_cmd()
780 return con->data[cmd]; in generic_get_cmd()
786 INIT_LIST_HEAD(&audio->cs); in control_selector_init()
787 list_add(&feature_unit.list, &audio->cs); in control_selector_init()
812 usb_put_function_instance(&opts->func_inst); in f_uac1_attr_release()
826 mutex_lock(&opts->lock); \
827 result = sprintf(page, "%u\n", opts->name); \
828 mutex_unlock(&opts->lock); \
834 const char *page, size_t len) \
840 mutex_lock(&opts->lock); \
841 if (opts->refcnt) { \
842 ret = -EBUSY; \
850 opts->name = num; \
851 ret = len; \
854 mutex_unlock(&opts->lock); \
871 mutex_lock(&opts->lock); \
872 result = sprintf(page, "%s\n", opts->name); \
873 mutex_unlock(&opts->lock); \
879 const char *page, size_t len) \
882 int ret = -EBUSY; \
885 mutex_lock(&opts->lock); \
886 if (opts->refcnt) \
889 tmp = kstrndup(page, len, GFP_KERNEL); \
891 ret = -ENOMEM; \
894 if (opts->name##_alloc) \
895 kfree(opts->name); \
896 opts->name##_alloc = true; \
897 opts->name = tmp; \
898 ret = len; \
901 mutex_unlock(&opts->lock); \
932 if (opts->fn_play_alloc) in f_audio_free_inst()
933 kfree(opts->fn_play); in f_audio_free_inst()
934 if (opts->fn_cap_alloc) in f_audio_free_inst()
935 kfree(opts->fn_cap); in f_audio_free_inst()
936 if (opts->fn_cntl_alloc) in f_audio_free_inst()
937 kfree(opts->fn_cntl); in f_audio_free_inst()
947 return ERR_PTR(-ENOMEM); in f_audio_alloc_inst()
949 mutex_init(&opts->lock); in f_audio_alloc_inst()
950 opts->func_inst.free_func_inst = f_audio_free_inst; in f_audio_alloc_inst()
952 config_group_init_type_name(&opts->func_inst.group, "", in f_audio_alloc_inst()
955 opts->req_buf_size = UAC1_OUT_EP_MAX_PACKET_SIZE; in f_audio_alloc_inst()
956 opts->req_count = UAC1_REQ_COUNT; in f_audio_alloc_inst()
957 opts->audio_buf_size = UAC1_AUDIO_BUF_SIZE; in f_audio_alloc_inst()
958 opts->fn_play = FILE_PCM_PLAYBACK; in f_audio_alloc_inst()
959 opts->fn_cap = FILE_PCM_CAPTURE; in f_audio_alloc_inst()
960 opts->fn_cntl = FILE_CONTROL; in f_audio_alloc_inst()
961 return &opts->func_inst; in f_audio_alloc_inst()
969 gaudio_cleanup(&audio->card); in f_audio_free()
970 opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst); in f_audio_free()
972 mutex_lock(&opts->lock); in f_audio_free()
973 --opts->refcnt; in f_audio_free()
974 mutex_unlock(&opts->lock); in f_audio_free()
990 return ERR_PTR(-ENOMEM); in f_audio_alloc()
992 audio->card.func.name = "g_audio"; in f_audio_alloc()
995 mutex_lock(&opts->lock); in f_audio_alloc()
996 ++opts->refcnt; in f_audio_alloc()
997 mutex_unlock(&opts->lock); in f_audio_alloc()
998 INIT_LIST_HEAD(&audio->play_queue); in f_audio_alloc()
999 spin_lock_init(&audio->lock); in f_audio_alloc()
1001 audio->card.func.bind = f_audio_bind; in f_audio_alloc()
1002 audio->card.func.unbind = f_audio_unbind; in f_audio_alloc()
1003 audio->card.func.set_alt = f_audio_set_alt; in f_audio_alloc()
1004 audio->card.func.get_alt = f_audio_get_alt; in f_audio_alloc()
1005 audio->card.func.setup = f_audio_setup; in f_audio_alloc()
1006 audio->card.func.disable = f_audio_disable; in f_audio_alloc()
1007 audio->card.func.free_func = f_audio_free; in f_audio_alloc()
1011 INIT_WORK(&audio->playback_work, f_audio_playback_work); in f_audio_alloc()
1013 return &audio->card.func; in f_audio_alloc()