diff options
Diffstat (limited to 'sound/usb/misc/ua101.c')
| -rw-r--r-- | sound/usb/misc/ua101.c | 325 |
1 files changed, 133 insertions, 192 deletions
diff --git a/sound/usb/misc/ua101.c b/sound/usb/misc/ua101.c index c19a5dd05631..76b6eb55dcc2 100644 --- a/sound/usb/misc/ua101.c +++ b/sound/usb/misc/ua101.c @@ -1,17 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0-only /* * Edirol UA-101/UA-1000 driver * Copyright (c) Clemens Ladisch <clemens@ladisch.de> - * - * This driver is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2. - * - * This driver is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this driver. If not, see <http://www.gnu.org/licenses/>. */ #include <linux/init.h> @@ -29,7 +19,6 @@ MODULE_DESCRIPTION("Edirol UA-101/1000 driver"); MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); MODULE_LICENSE("GPL v2"); -MODULE_SUPPORTED_DEVICE("{{Edirol,UA-101},{Edirol,UA-1000}}"); /* * Should not be lower than the minimum scheduling delay of the host @@ -106,7 +95,7 @@ struct ua101 { u8 rate_feedback[MAX_QUEUE_LENGTH]; struct list_head ready_playback_urbs; - struct tasklet_struct playback_tasklet; + struct work_struct playback_work; wait_queue_head_t alsa_capture_wait; wait_queue_head_t rate_feedback_wait; wait_queue_head_t alsa_playback_wait; @@ -182,7 +171,6 @@ static void playback_urb_complete(struct urb *usb_urb) { struct ua101_urb *urb = (struct ua101_urb *)usb_urb; struct ua101 *ua = urb->urb.context; - unsigned long flags; if (unlikely(urb->urb.status == -ENOENT || /* unlinked */ urb->urb.status == -ENODEV || /* device removed */ @@ -195,14 +183,13 @@ static void playback_urb_complete(struct urb *usb_urb) if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) { /* append URB to FIFO */ - spin_lock_irqsave(&ua->lock, flags); + guard(spinlock_irqsave)(&ua->lock); list_add_tail(&urb->ready_list, &ua->ready_playback_urbs); if (ua->rate_feedback_count > 0) - tasklet_schedule(&ua->playback_tasklet); + queue_work(system_highpri_wq, &ua->playback_work); ua->playback.substream->runtime->delay -= urb->urb.iso_frame_desc[0].length / ua->playback.frame_bytes; - spin_unlock_irqrestore(&ua->lock, flags); } } @@ -257,10 +244,9 @@ static inline void add_with_wraparound(struct ua101 *ua, *value -= ua->playback.queue_length; } -static void playback_tasklet(unsigned long data) +static void playback_work(struct work_struct *work) { - struct ua101 *ua = (void *)data; - unsigned long flags; + struct ua101 *ua = container_of(work, struct ua101, playback_work); unsigned int frames; struct ua101_urb *urb; bool do_period_elapsed = false; @@ -280,43 +266,43 @@ static void playback_tasklet(unsigned long data) * submitting playback URBs is possible as long as both FIFOs are * nonempty. */ - spin_lock_irqsave(&ua->lock, flags); - while (ua->rate_feedback_count > 0 && - !list_empty(&ua->ready_playback_urbs)) { - /* take packet size out of FIFO */ - frames = ua->rate_feedback[ua->rate_feedback_start]; - add_with_wraparound(ua, &ua->rate_feedback_start, 1); - ua->rate_feedback_count--; - - /* take URB out of FIFO */ - urb = list_first_entry(&ua->ready_playback_urbs, - struct ua101_urb, ready_list); - list_del(&urb->ready_list); - - /* fill packet with data or silence */ - urb->urb.iso_frame_desc[0].length = - frames * ua->playback.frame_bytes; - if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) - do_period_elapsed |= copy_playback_data(&ua->playback, - &urb->urb, - frames); - else - memset(urb->urb.transfer_buffer, 0, - urb->urb.iso_frame_desc[0].length); - - /* and off you go ... */ - err = usb_submit_urb(&urb->urb, GFP_ATOMIC); - if (unlikely(err < 0)) { - spin_unlock_irqrestore(&ua->lock, flags); - abort_usb_playback(ua); - abort_alsa_playback(ua); - dev_err(&ua->dev->dev, "USB request error %d: %s\n", - err, usb_error_string(err)); - return; + scoped_guard(spinlock_irqsave, &ua->lock) { + while (ua->rate_feedback_count > 0 && + !list_empty(&ua->ready_playback_urbs)) { + /* take packet size out of FIFO */ + frames = ua->rate_feedback[ua->rate_feedback_start]; + add_with_wraparound(ua, &ua->rate_feedback_start, 1); + ua->rate_feedback_count--; + + /* take URB out of FIFO */ + urb = list_first_entry(&ua->ready_playback_urbs, + struct ua101_urb, ready_list); + list_del(&urb->ready_list); + + /* fill packet with data or silence */ + urb->urb.iso_frame_desc[0].length = + frames * ua->playback.frame_bytes; + if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) + do_period_elapsed |= copy_playback_data(&ua->playback, + &urb->urb, + frames); + else + memset(urb->urb.transfer_buffer, 0, + urb->urb.iso_frame_desc[0].length); + + /* and off you go ... */ + err = usb_submit_urb(&urb->urb, GFP_ATOMIC); + if (unlikely(err < 0)) { + abort_usb_playback(ua); + abort_alsa_playback(ua); + dev_err(&ua->dev->dev, "USB request error %d: %s\n", + err, usb_error_string(err)); + return; + } + ua->playback.substream->runtime->delay += frames; } - ua->playback.substream->runtime->delay += frames; } - spin_unlock_irqrestore(&ua->lock, flags); + if (do_period_elapsed) snd_pcm_period_elapsed(ua->playback.substream); } @@ -358,7 +344,6 @@ static void capture_urb_complete(struct urb *urb) { struct ua101 *ua = urb->context; struct ua101_stream *stream = &ua->capture; - unsigned long flags; unsigned int frames, write_ptr; bool do_period_elapsed; int err; @@ -375,47 +360,45 @@ static void capture_urb_complete(struct urb *urb) else frames = 0; - spin_lock_irqsave(&ua->lock, flags); + scoped_guard(spinlock_irqsave, &ua->lock) { - if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) - do_period_elapsed = copy_capture_data(stream, urb, frames); - else - do_period_elapsed = false; - - if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) { - err = usb_submit_urb(urb, GFP_ATOMIC); - if (unlikely(err < 0)) { - spin_unlock_irqrestore(&ua->lock, flags); - dev_err(&ua->dev->dev, "USB request error %d: %s\n", - err, usb_error_string(err)); - goto stream_stopped; - } + if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) + do_period_elapsed = copy_capture_data(stream, urb, frames); + else + do_period_elapsed = false; + + if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) { + err = usb_submit_urb(urb, GFP_ATOMIC); + if (unlikely(err < 0)) { + dev_err(&ua->dev->dev, "USB request error %d: %s\n", + err, usb_error_string(err)); + goto stream_stopped; + } - /* append packet size to FIFO */ - write_ptr = ua->rate_feedback_start; - add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count); - ua->rate_feedback[write_ptr] = frames; - if (ua->rate_feedback_count < ua->playback.queue_length) { - ua->rate_feedback_count++; - if (ua->rate_feedback_count == - ua->playback.queue_length) - wake_up(&ua->rate_feedback_wait); - } else { - /* - * Ring buffer overflow; this happens when the playback - * stream is not running. Throw away the oldest entry, - * so that the playback stream, when it starts, sees - * the most recent packet sizes. - */ - add_with_wraparound(ua, &ua->rate_feedback_start, 1); + /* append packet size to FIFO */ + write_ptr = ua->rate_feedback_start; + add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count); + ua->rate_feedback[write_ptr] = frames; + if (ua->rate_feedback_count < ua->playback.queue_length) { + ua->rate_feedback_count++; + if (ua->rate_feedback_count == + ua->playback.queue_length) + wake_up(&ua->rate_feedback_wait); + } else { + /* + * Ring buffer overflow; this happens when the playback + * stream is not running. Throw away the oldest entry, + * so that the playback stream, when it starts, sees + * the most recent packet sizes. + */ + add_with_wraparound(ua, &ua->rate_feedback_start, 1); + } + if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) && + !list_empty(&ua->ready_playback_urbs)) + queue_work(system_highpri_wq, &ua->playback_work); } - if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) && - !list_empty(&ua->ready_playback_urbs)) - tasklet_schedule(&ua->playback_tasklet); } - spin_unlock_irqrestore(&ua->lock, flags); - if (do_period_elapsed) snd_pcm_period_elapsed(stream->substream); @@ -542,7 +525,7 @@ static void stop_usb_playback(struct ua101 *ua) kill_stream_urbs(&ua->playback); - tasklet_kill(&ua->playback_tasklet); + cancel_work_sync(&ua->playback_work); disable_iso_interface(ua, INTF_PLAYBACK); } @@ -560,7 +543,7 @@ static int start_usb_playback(struct ua101 *ua) return 0; kill_stream_urbs(&ua->playback); - tasklet_kill(&ua->playback_tasklet); + cancel_work_sync(&ua->playback_work); err = enable_iso_interface(ua, INTF_PLAYBACK); if (err < 0) @@ -569,9 +552,9 @@ static int start_usb_playback(struct ua101 *ua) clear_bit(PLAYBACK_URB_COMPLETED, &ua->states); ua->playback.urbs[0]->urb.complete = first_playback_urb_complete; - spin_lock_irq(&ua->lock); - INIT_LIST_HEAD(&ua->ready_playback_urbs); - spin_unlock_irq(&ua->lock); + scoped_guard(spinlock_irq, &ua->lock) { + INIT_LIST_HEAD(&ua->ready_playback_urbs); + } /* * We submit the initial URBs all at once, so we have to wait for the @@ -592,11 +575,11 @@ static int start_usb_playback(struct ua101 *ua) for (i = 0; i < ua->playback.queue_length; ++i) { /* all initial URBs contain silence */ - spin_lock_irq(&ua->lock); - frames = ua->rate_feedback[ua->rate_feedback_start]; - add_with_wraparound(ua, &ua->rate_feedback_start, 1); - ua->rate_feedback_count--; - spin_unlock_irq(&ua->lock); + scoped_guard(spinlock_irq, &ua->lock) { + frames = ua->rate_feedback[ua->rate_feedback_start]; + add_with_wraparound(ua, &ua->rate_feedback_start, 1); + ua->rate_feedback_count--; + } urb = &ua->playback.urbs[i]->urb; urb->iso_frame_desc[0].length = frames * ua->playback.frame_bytes; @@ -669,11 +652,10 @@ static int capture_pcm_open(struct snd_pcm_substream *substream) DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second); substream->runtime->delay = substream->runtime->hw.fifo_size; - mutex_lock(&ua->mutex); + guard(mutex)(&ua->mutex); err = start_usb_capture(ua); if (err >= 0) set_bit(ALSA_CAPTURE_OPEN, &ua->states); - mutex_unlock(&ua->mutex); return err; } @@ -690,31 +672,28 @@ static int playback_pcm_open(struct snd_pcm_substream *substream) DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length, ua->packets_per_second); - mutex_lock(&ua->mutex); + guard(mutex)(&ua->mutex); err = start_usb_capture(ua); if (err < 0) - goto error; + return err; err = start_usb_playback(ua); if (err < 0) { if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) stop_usb_capture(ua); - goto error; + return err; } set_bit(ALSA_PLAYBACK_OPEN, &ua->states); -error: - mutex_unlock(&ua->mutex); - return err; + return 0; } static int capture_pcm_close(struct snd_pcm_substream *substream) { struct ua101 *ua = substream->private_data; - mutex_lock(&ua->mutex); + guard(mutex)(&ua->mutex); clear_bit(ALSA_CAPTURE_OPEN, &ua->states); if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states)) stop_usb_capture(ua); - mutex_unlock(&ua->mutex); return 0; } @@ -722,12 +701,11 @@ static int playback_pcm_close(struct snd_pcm_substream *substream) { struct ua101 *ua = substream->private_data; - mutex_lock(&ua->mutex); + guard(mutex)(&ua->mutex); stop_usb_playback(ua); clear_bit(ALSA_PLAYBACK_OPEN, &ua->states); if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) stop_usb_capture(ua); - mutex_unlock(&ua->mutex); return 0; } @@ -735,16 +713,9 @@ static int capture_pcm_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { struct ua101 *ua = substream->private_data; - int err; - - mutex_lock(&ua->mutex); - err = start_usb_capture(ua); - mutex_unlock(&ua->mutex); - if (err < 0) - return err; - return snd_pcm_lib_alloc_vmalloc_buffer(substream, - params_buffer_bytes(hw_params)); + guard(mutex)(&ua->mutex); + return start_usb_capture(ua); } static int playback_pcm_hw_params(struct snd_pcm_substream *substream, @@ -753,21 +724,11 @@ static int playback_pcm_hw_params(struct snd_pcm_substream *substream, struct ua101 *ua = substream->private_data; int err; - mutex_lock(&ua->mutex); + guard(mutex)(&ua->mutex); err = start_usb_capture(ua); if (err >= 0) err = start_usb_playback(ua); - mutex_unlock(&ua->mutex); - if (err < 0) - return err; - - return snd_pcm_lib_alloc_vmalloc_buffer(substream, - params_buffer_bytes(hw_params)); -} - -static int ua101_pcm_hw_free(struct snd_pcm_substream *substream) -{ - return snd_pcm_lib_free_vmalloc_buffer(substream); + return err; } static int capture_pcm_prepare(struct snd_pcm_substream *substream) @@ -775,9 +736,9 @@ static int capture_pcm_prepare(struct snd_pcm_substream *substream) struct ua101 *ua = substream->private_data; int err; - mutex_lock(&ua->mutex); - err = start_usb_capture(ua); - mutex_unlock(&ua->mutex); + scoped_guard(mutex, &ua->mutex) { + err = start_usb_capture(ua); + } if (err < 0) return err; @@ -805,11 +766,11 @@ static int playback_pcm_prepare(struct snd_pcm_substream *substream) struct ua101 *ua = substream->private_data; int err; - mutex_lock(&ua->mutex); - err = start_usb_capture(ua); - if (err >= 0) - err = start_usb_playback(ua); - mutex_unlock(&ua->mutex); + scoped_guard(mutex, &ua->mutex) { + err = start_usb_capture(ua); + if (err >= 0) + err = start_usb_playback(ua); + } if (err < 0) return err; @@ -867,13 +828,8 @@ static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd) static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua, struct ua101_stream *stream) { - unsigned long flags; - unsigned int pos; - - spin_lock_irqsave(&ua->lock, flags); - pos = stream->buffer_pos; - spin_unlock_irqrestore(&ua->lock, flags); - return pos; + guard(spinlock_irqsave)(&ua->lock); + return stream->buffer_pos; } static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs) @@ -890,30 +846,22 @@ static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs) return ua101_pcm_pointer(ua, &ua->playback); } -static struct snd_pcm_ops capture_pcm_ops = { +static const struct snd_pcm_ops capture_pcm_ops = { .open = capture_pcm_open, .close = capture_pcm_close, - .ioctl = snd_pcm_lib_ioctl, .hw_params = capture_pcm_hw_params, - .hw_free = ua101_pcm_hw_free, .prepare = capture_pcm_prepare, .trigger = capture_pcm_trigger, .pointer = capture_pcm_pointer, - .page = snd_pcm_lib_get_vmalloc_page, - .mmap = snd_pcm_lib_mmap_vmalloc, }; -static struct snd_pcm_ops playback_pcm_ops = { +static const struct snd_pcm_ops playback_pcm_ops = { .open = playback_pcm_open, .close = playback_pcm_close, - .ioctl = snd_pcm_lib_ioctl, .hw_params = playback_pcm_hw_params, - .hw_free = ua101_pcm_hw_free, .prepare = playback_pcm_prepare, .trigger = playback_pcm_trigger, .pointer = playback_pcm_pointer, - .page = snd_pcm_lib_get_vmalloc_page, - .mmap = snd_pcm_lib_mmap_vmalloc, }; static const struct uac_format_type_i_discrete_descriptor * @@ -1032,7 +980,7 @@ static int detect_usb_format(struct ua101 *ua) fmt_playback->bSubframeSize * ua->playback.channels; epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc; - if (!usb_endpoint_is_isoc_in(epd)) { + if (!usb_endpoint_is_isoc_in(epd) || usb_endpoint_maxp(epd) == 0) { dev_err(&ua->dev->dev, "invalid capture endpoint\n"); return -ENXIO; } @@ -1040,7 +988,7 @@ static int detect_usb_format(struct ua101 *ua) ua->capture.max_packet_bytes = usb_endpoint_maxp(epd); epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc; - if (!usb_endpoint_is_isoc_out(epd)) { + if (!usb_endpoint_is_isoc_out(epd) || usb_endpoint_maxp(epd) == 0) { dev_err(&ua->dev->dev, "invalid playback endpoint\n"); return -ENXIO; } @@ -1159,18 +1107,18 @@ static void free_usb_related_resources(struct ua101 *ua, unsigned int i; struct usb_interface *intf; - mutex_lock(&ua->mutex); - free_stream_urbs(&ua->capture); - free_stream_urbs(&ua->playback); - mutex_unlock(&ua->mutex); + scoped_guard(mutex, &ua->mutex) { + free_stream_urbs(&ua->capture); + free_stream_urbs(&ua->playback); + } free_stream_buffers(ua, &ua->capture); free_stream_buffers(ua, &ua->playback); for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) { - mutex_lock(&ua->mutex); - intf = ua->intf[i]; - ua->intf[i] = NULL; - mutex_unlock(&ua->mutex); + scoped_guard(mutex, &ua->mutex) { + intf = ua->intf[i]; + ua->intf[i] = NULL; + } if (intf) { usb_set_intfdata(intf, NULL); if (intf != interface) @@ -1224,22 +1172,18 @@ static int ua101_probe(struct usb_interface *interface, intf_numbers[is_ua1000][0]) return -ENODEV; - mutex_lock(&devices_mutex); + guard(mutex)(&devices_mutex); for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) if (enable[card_index] && !(devices_used & (1 << card_index))) break; - if (card_index >= SNDRV_CARDS) { - mutex_unlock(&devices_mutex); + if (card_index >= SNDRV_CARDS) return -ENOENT; - } err = snd_card_new(&interface->dev, index[card_index], id[card_index], THIS_MODULE, sizeof(*ua), &card); - if (err < 0) { - mutex_unlock(&devices_mutex); + if (err < 0) return err; - } card->private_free = ua101_card_free; ua = card->private_data; ua->dev = interface_to_usbdev(interface); @@ -1249,8 +1193,7 @@ static int ua101_probe(struct usb_interface *interface, spin_lock_init(&ua->lock); mutex_init(&ua->mutex); INIT_LIST_HEAD(&ua->ready_playback_urbs); - tasklet_init(&ua->playback_tasklet, - playback_tasklet, (unsigned long)ua); + INIT_WORK(&ua->playback_work, playback_work); init_waitqueue_head(&ua->alsa_capture_wait); init_waitqueue_head(&ua->rate_feedback_wait); init_waitqueue_head(&ua->alsa_playback_wait); @@ -1279,8 +1222,8 @@ static int ua101_probe(struct usb_interface *interface, goto probe_error; name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101"; - strcpy(card->driver, "UA-101"); - strcpy(card->shortname, name); + strscpy(card->driver, "UA-101"); + strscpy(card->shortname, name); usb_make_path(ua->dev, usb_path, sizeof(usb_path)); snprintf(ua->card->longname, sizeof(ua->card->longname), "EDIROL %s (serial %s), %u Hz at %s, %s speed", name, @@ -1305,9 +1248,11 @@ static int ua101_probe(struct usb_interface *interface, if (err < 0) goto probe_error; ua->pcm->private_data = ua; - strcpy(ua->pcm->name, name); + strscpy(ua->pcm->name, name); snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops); snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops); + snd_pcm_set_managed_buffer_all(ua->pcm, SNDRV_DMA_TYPE_VMALLOC, + NULL, 0, 0); err = snd_usbmidi_create(card, ua->intf[INTF_MIDI], &ua->midi_list, &midi_quirk); @@ -1321,13 +1266,11 @@ static int ua101_probe(struct usb_interface *interface, usb_set_intfdata(interface, ua); devices_used |= 1 << card_index; - mutex_unlock(&devices_mutex); return 0; probe_error: free_usb_related_resources(ua, interface); snd_card_free(card); - mutex_unlock(&devices_mutex); return err; } @@ -1339,7 +1282,7 @@ static void ua101_disconnect(struct usb_interface *interface) if (!ua) return; - mutex_lock(&devices_mutex); + guard(mutex)(&devices_mutex); set_bit(DISCONNECTED, &ua->states); wake_up(&ua->rate_feedback_wait); @@ -1352,21 +1295,19 @@ static void ua101_disconnect(struct usb_interface *interface) snd_usbmidi_disconnect(midi); abort_alsa_playback(ua); abort_alsa_capture(ua); - mutex_lock(&ua->mutex); - stop_usb_playback(ua); - stop_usb_capture(ua); - mutex_unlock(&ua->mutex); + scoped_guard(mutex, &ua->mutex) { + stop_usb_playback(ua); + stop_usb_capture(ua); + } free_usb_related_resources(ua, interface); devices_used &= ~(1 << ua->card_index); snd_card_free_when_closed(ua->card); - - mutex_unlock(&devices_mutex); } -static struct usb_device_id ua101_ids[] = { +static const struct usb_device_id ua101_ids[] = { { USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */ { USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */ { USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */ |
