Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 1 | /* |
Chris Rorvick | c078a4a | 2015-01-20 02:20:50 -0600 | [diff] [blame] | 2 | * Line 6 Linux USB driver |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 3 | * |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 4 | * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation, version 2. |
| 9 | * |
| 10 | */ |
| 11 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 13 | #include <linux/export.h> |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 14 | #include <sound/core.h> |
| 15 | #include <sound/control.h> |
| 16 | #include <sound/pcm.h> |
| 17 | #include <sound/pcm_params.h> |
| 18 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 19 | #include "capture.h" |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 20 | #include "driver.h" |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 21 | #include "playback.h" |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 22 | |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 23 | /* impulse response volume controls */ |
| 24 | static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol, |
| 25 | struct snd_ctl_elem_info *uinfo) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 26 | { |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 27 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 28 | uinfo->count = 1; |
| 29 | uinfo->value.integer.min = 0; |
| 30 | uinfo->value.integer.max = 255; |
| 31 | return 0; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 34 | static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol, |
| 35 | struct snd_ctl_elem_value *ucontrol) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 36 | { |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 37 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
| 38 | |
| 39 | ucontrol->value.integer.value[0] = line6pcm->impulse_volume; |
| 40 | return 0; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 43 | static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol, |
| 44 | struct snd_ctl_elem_value *ucontrol) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 45 | { |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 46 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
| 47 | int value = ucontrol->value.integer.value[0]; |
Takashi Iwai | 247d95e | 2015-01-27 16:42:14 +0100 | [diff] [blame] | 48 | int err; |
Johannes Thumshirn | cdf5e55 | 2012-08-06 14:08:50 +0200 | [diff] [blame] | 49 | |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 50 | if (line6pcm->impulse_volume == value) |
| 51 | return 0; |
Johannes Thumshirn | cdf5e55 | 2012-08-06 14:08:50 +0200 | [diff] [blame] | 52 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 53 | line6pcm->impulse_volume = value; |
Takashi Iwai | 247d95e | 2015-01-27 16:42:14 +0100 | [diff] [blame] | 54 | if (value > 0) { |
| 55 | err = line6_pcm_acquire(line6pcm, LINE6_STREAM_IMPULSE); |
| 56 | if (err < 0) { |
| 57 | line6pcm->impulse_volume = 0; |
Takashi Iwai | 247d95e | 2015-01-27 16:42:14 +0100 | [diff] [blame] | 58 | return err; |
| 59 | } |
| 60 | } else { |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 61 | line6_pcm_release(line6pcm, LINE6_STREAM_IMPULSE); |
Takashi Iwai | 247d95e | 2015-01-27 16:42:14 +0100 | [diff] [blame] | 62 | } |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 63 | return 1; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 66 | /* impulse response period controls */ |
| 67 | static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol, |
| 68 | struct snd_ctl_elem_info *uinfo) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 69 | { |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 70 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 71 | uinfo->count = 1; |
| 72 | uinfo->value.integer.min = 0; |
| 73 | uinfo->value.integer.max = 2000; |
| 74 | return 0; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 77 | static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol, |
| 78 | struct snd_ctl_elem_value *ucontrol) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 79 | { |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 80 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
Laurent Navet | a376290 | 2012-11-30 11:57:32 +0100 | [diff] [blame] | 81 | |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 82 | ucontrol->value.integer.value[0] = line6pcm->impulse_period; |
| 83 | return 0; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 84 | } |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 85 | |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 86 | static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol, |
| 87 | struct snd_ctl_elem_value *ucontrol) |
| 88 | { |
| 89 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
| 90 | int value = ucontrol->value.integer.value[0]; |
| 91 | |
| 92 | if (line6pcm->impulse_period == value) |
| 93 | return 0; |
| 94 | |
| 95 | line6pcm->impulse_period = value; |
| 96 | return 1; |
| 97 | } |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 98 | |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 99 | /* |
| 100 | Unlink all currently active URBs. |
| 101 | */ |
| 102 | static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm, |
| 103 | struct line6_pcm_stream *pcms) |
| 104 | { |
| 105 | int i; |
| 106 | |
Andrej Krutak | b2233d9 | 2016-09-18 20:59:21 +0200 | [diff] [blame] | 107 | for (i = 0; i < line6pcm->line6->iso_buffers; i++) { |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 108 | if (test_bit(i, &pcms->active_urbs)) { |
| 109 | if (!test_and_set_bit(i, &pcms->unlink_urbs)) |
| 110 | usb_unlink_urb(pcms->urbs[i]); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | Wait until unlinking of all currently active URBs has been finished. |
| 117 | */ |
| 118 | static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm, |
| 119 | struct line6_pcm_stream *pcms) |
| 120 | { |
| 121 | int timeout = HZ; |
| 122 | int i; |
| 123 | int alive; |
| 124 | |
| 125 | do { |
| 126 | alive = 0; |
Andrej Krutak | b2233d9 | 2016-09-18 20:59:21 +0200 | [diff] [blame] | 127 | for (i = 0; i < line6pcm->line6->iso_buffers; i++) { |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 128 | if (test_bit(i, &pcms->active_urbs)) |
| 129 | alive++; |
| 130 | } |
| 131 | if (!alive) |
| 132 | break; |
| 133 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 134 | schedule_timeout(1); |
| 135 | } while (--timeout > 0); |
| 136 | if (alive) |
Takashi Iwai | ccaac9e | 2015-01-23 16:20:50 +0100 | [diff] [blame] | 137 | dev_err(line6pcm->line6->ifcdev, |
| 138 | "timeout: still %d active urbs..\n", alive); |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 141 | static inline struct line6_pcm_stream * |
| 142 | get_stream(struct snd_line6_pcm *line6pcm, int direction) |
| 143 | { |
| 144 | return (direction == SNDRV_PCM_STREAM_PLAYBACK) ? |
| 145 | &line6pcm->out : &line6pcm->in; |
| 146 | } |
| 147 | |
| 148 | /* allocate a buffer if not opened yet; |
Andrej Krutak | 7a0f55a | 2016-09-18 20:59:23 +0200 | [diff] [blame^] | 149 | * call this in line6pcm.state_mutex |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 150 | */ |
| 151 | static int line6_buffer_acquire(struct snd_line6_pcm *line6pcm, |
Andrej Krutak | 7a0f55a | 2016-09-18 20:59:23 +0200 | [diff] [blame^] | 152 | struct line6_pcm_stream *pstr, int direction, int type) |
Takashi Iwai | e90576c | 2015-01-23 16:25:03 +0100 | [diff] [blame] | 153 | { |
Andrej Krutak | 7a0f55a | 2016-09-18 20:59:23 +0200 | [diff] [blame^] | 154 | const int pkt_size = |
| 155 | (direction == SNDRV_PCM_STREAM_PLAYBACK) ? |
| 156 | line6pcm->max_packet_size_out : |
| 157 | line6pcm->max_packet_size_in; |
| 158 | |
Takashi Iwai | e90576c | 2015-01-23 16:25:03 +0100 | [diff] [blame] | 159 | /* Invoked multiple times in a row so allocate once only */ |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 160 | if (!test_and_set_bit(type, &pstr->opened) && !pstr->buffer) { |
Andrej Krutak | b2233d9 | 2016-09-18 20:59:21 +0200 | [diff] [blame] | 161 | pstr->buffer = kmalloc(line6pcm->line6->iso_buffers * |
Andrej Krutak | 7a0f55a | 2016-09-18 20:59:23 +0200 | [diff] [blame^] | 162 | LINE6_ISO_PACKETS * pkt_size, GFP_KERNEL); |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 163 | if (!pstr->buffer) |
| 164 | return -ENOMEM; |
| 165 | } |
Takashi Iwai | e90576c | 2015-01-23 16:25:03 +0100 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 169 | /* free a buffer if all streams are closed; |
Andrej Krutak | 7a0f55a | 2016-09-18 20:59:23 +0200 | [diff] [blame^] | 170 | * call this in line6pcm.state_mutex |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 171 | */ |
| 172 | static void line6_buffer_release(struct snd_line6_pcm *line6pcm, |
| 173 | struct line6_pcm_stream *pstr, int type) |
Takashi Iwai | e90576c | 2015-01-23 16:25:03 +0100 | [diff] [blame] | 174 | { |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 175 | clear_bit(type, &pstr->opened); |
| 176 | if (!pstr->opened) { |
| 177 | line6_wait_clear_audio_urbs(line6pcm, pstr); |
| 178 | kfree(pstr->buffer); |
| 179 | pstr->buffer = NULL; |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 180 | } |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 181 | } |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 182 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 183 | /* start a PCM stream */ |
| 184 | static int line6_stream_start(struct snd_line6_pcm *line6pcm, int direction, |
| 185 | int type) |
| 186 | { |
| 187 | unsigned long flags; |
| 188 | struct line6_pcm_stream *pstr = get_stream(line6pcm, direction); |
| 189 | int ret = 0; |
| 190 | |
| 191 | spin_lock_irqsave(&pstr->lock, flags); |
Takashi Iwai | 4d0e677 | 2015-07-14 15:19:37 +0200 | [diff] [blame] | 192 | if (!test_and_set_bit(type, &pstr->running) && |
| 193 | !(pstr->active_urbs || pstr->unlink_urbs)) { |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 194 | pstr->count = 0; |
| 195 | /* Submit all currently available URBs */ |
| 196 | if (direction == SNDRV_PCM_STREAM_PLAYBACK) |
| 197 | ret = line6_submit_audio_out_all_urbs(line6pcm); |
| 198 | else |
| 199 | ret = line6_submit_audio_in_all_urbs(line6pcm); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 200 | } |
Andrej Krutak | 7a0f55a | 2016-09-18 20:59:23 +0200 | [diff] [blame^] | 201 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 202 | if (ret < 0) |
| 203 | clear_bit(type, &pstr->running); |
| 204 | spin_unlock_irqrestore(&pstr->lock, flags); |
| 205 | return ret; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 208 | /* stop a PCM stream; this doesn't sync with the unlinked URBs */ |
| 209 | static void line6_stream_stop(struct snd_line6_pcm *line6pcm, int direction, |
| 210 | int type) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 211 | { |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 212 | unsigned long flags; |
| 213 | struct line6_pcm_stream *pstr = get_stream(line6pcm, direction); |
Arnd Bergmann | 9f61360 | 2013-06-21 21:55:35 +0200 | [diff] [blame] | 214 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 215 | spin_lock_irqsave(&pstr->lock, flags); |
| 216 | clear_bit(type, &pstr->running); |
| 217 | if (!pstr->running) { |
Andrej Krutak | adc8a43 | 2016-08-18 23:52:11 +0200 | [diff] [blame] | 218 | spin_unlock_irqrestore(&pstr->lock, flags); |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 219 | line6_unlink_audio_urbs(line6pcm, pstr); |
Andrej Krutak | adc8a43 | 2016-08-18 23:52:11 +0200 | [diff] [blame] | 220 | spin_lock_irqsave(&pstr->lock, flags); |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 221 | if (direction == SNDRV_PCM_STREAM_CAPTURE) { |
| 222 | line6pcm->prev_fbuf = NULL; |
| 223 | line6pcm->prev_fsize = 0; |
| 224 | } |
Takashi Iwai | f2bb614 | 2015-01-27 16:17:26 +0100 | [diff] [blame] | 225 | } |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 226 | spin_unlock_irqrestore(&pstr->lock, flags); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 229 | /* common PCM trigger callback */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 230 | int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd) |
| 231 | { |
| 232 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 233 | struct snd_pcm_substream *s; |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 234 | int err; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 235 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 236 | clear_bit(LINE6_FLAG_PREPARED, &line6pcm->flags); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 237 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 238 | snd_pcm_group_for_each_entry(s, substream) { |
Takashi Iwai | 7d70c81 | 2015-01-19 15:11:17 +0100 | [diff] [blame] | 239 | if (s->pcm->card != substream->pcm->card) |
| 240 | continue; |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 241 | |
| 242 | switch (cmd) { |
| 243 | case SNDRV_PCM_TRIGGER_START: |
| 244 | case SNDRV_PCM_TRIGGER_RESUME: |
| 245 | err = line6_stream_start(line6pcm, s->stream, |
| 246 | LINE6_STREAM_PCM); |
| 247 | if (err < 0) |
| 248 | return err; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 249 | break; |
| 250 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 251 | case SNDRV_PCM_TRIGGER_STOP: |
| 252 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 253 | line6_stream_stop(line6pcm, s->stream, |
| 254 | LINE6_STREAM_PCM); |
| 255 | break; |
| 256 | |
| 257 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 258 | if (s->stream != SNDRV_PCM_STREAM_PLAYBACK) |
| 259 | return -EINVAL; |
| 260 | set_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags); |
| 261 | break; |
| 262 | |
| 263 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 264 | if (s->stream != SNDRV_PCM_STREAM_PLAYBACK) |
| 265 | return -EINVAL; |
| 266 | clear_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 267 | break; |
| 268 | |
| 269 | default: |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 270 | return -EINVAL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 274 | return 0; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Takashi Iwai | 2954f91 | 2015-01-27 15:41:27 +0100 | [diff] [blame] | 277 | /* common PCM pointer callback */ |
| 278 | snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream) |
| 279 | { |
| 280 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 281 | struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream); |
| 282 | |
| 283 | return pstr->pos_done; |
| 284 | } |
| 285 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 286 | /* Acquire and start duplex streams: |
| 287 | * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR |
| 288 | */ |
| 289 | int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type) |
| 290 | { |
| 291 | struct line6_pcm_stream *pstr; |
| 292 | int ret = 0, dir; |
| 293 | |
| 294 | mutex_lock(&line6pcm->state_mutex); |
| 295 | for (dir = 0; dir < 2; dir++) { |
| 296 | pstr = get_stream(line6pcm, dir); |
Andrej Krutak | 7a0f55a | 2016-09-18 20:59:23 +0200 | [diff] [blame^] | 297 | ret = line6_buffer_acquire(line6pcm, pstr, dir, type); |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 298 | if (ret < 0) |
| 299 | goto error; |
| 300 | if (!pstr->running) |
| 301 | line6_wait_clear_audio_urbs(line6pcm, pstr); |
| 302 | } |
| 303 | for (dir = 0; dir < 2; dir++) { |
| 304 | ret = line6_stream_start(line6pcm, dir, type); |
| 305 | if (ret < 0) |
| 306 | goto error; |
| 307 | } |
| 308 | error: |
| 309 | mutex_unlock(&line6pcm->state_mutex); |
| 310 | if (ret < 0) |
| 311 | line6_pcm_release(line6pcm, type); |
| 312 | return ret; |
| 313 | } |
| 314 | EXPORT_SYMBOL_GPL(line6_pcm_acquire); |
| 315 | |
| 316 | /* Stop and release duplex streams */ |
| 317 | void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type) |
| 318 | { |
| 319 | struct line6_pcm_stream *pstr; |
| 320 | int dir; |
| 321 | |
| 322 | mutex_lock(&line6pcm->state_mutex); |
| 323 | for (dir = 0; dir < 2; dir++) |
| 324 | line6_stream_stop(line6pcm, dir, type); |
| 325 | for (dir = 0; dir < 2; dir++) { |
| 326 | pstr = get_stream(line6pcm, dir); |
| 327 | line6_buffer_release(line6pcm, pstr, type); |
| 328 | } |
| 329 | mutex_unlock(&line6pcm->state_mutex); |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(line6_pcm_release); |
| 332 | |
| 333 | /* common PCM hw_params callback */ |
| 334 | int snd_line6_hw_params(struct snd_pcm_substream *substream, |
| 335 | struct snd_pcm_hw_params *hw_params) |
| 336 | { |
| 337 | int ret; |
| 338 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 339 | struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream); |
| 340 | |
| 341 | mutex_lock(&line6pcm->state_mutex); |
Andrej Krutak | 7a0f55a | 2016-09-18 20:59:23 +0200 | [diff] [blame^] | 342 | ret = line6_buffer_acquire(line6pcm, pstr, substream->stream, |
| 343 | LINE6_STREAM_PCM); |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 344 | if (ret < 0) |
| 345 | goto error; |
| 346 | |
| 347 | ret = snd_pcm_lib_malloc_pages(substream, |
| 348 | params_buffer_bytes(hw_params)); |
| 349 | if (ret < 0) { |
| 350 | line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM); |
| 351 | goto error; |
| 352 | } |
| 353 | |
| 354 | pstr->period = params_period_bytes(hw_params); |
| 355 | error: |
| 356 | mutex_unlock(&line6pcm->state_mutex); |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | /* common PCM hw_free callback */ |
| 361 | int snd_line6_hw_free(struct snd_pcm_substream *substream) |
| 362 | { |
| 363 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 364 | struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream); |
| 365 | |
| 366 | mutex_lock(&line6pcm->state_mutex); |
| 367 | line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM); |
| 368 | mutex_unlock(&line6pcm->state_mutex); |
| 369 | return snd_pcm_lib_free_pages(substream); |
| 370 | } |
| 371 | |
| 372 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 373 | /* control info callback */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 374 | static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol, |
| 375 | struct snd_ctl_elem_info *uinfo) |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 376 | { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 377 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 378 | uinfo->count = 2; |
| 379 | uinfo->value.integer.min = 0; |
| 380 | uinfo->value.integer.max = 256; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /* control get callback */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 385 | static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol, |
| 386 | struct snd_ctl_elem_value *ucontrol) |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 387 | { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 388 | int i; |
| 389 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
| 390 | |
Takashi Iwai | 9fb754b | 2015-01-23 15:08:40 +0100 | [diff] [blame] | 391 | for (i = 0; i < 2; i++) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 392 | ucontrol->value.integer.value[i] = line6pcm->volume_playback[i]; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* control put callback */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 398 | static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol, |
| 399 | struct snd_ctl_elem_value *ucontrol) |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 400 | { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 401 | int i, changed = 0; |
| 402 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
| 403 | |
Takashi Iwai | 9fb754b | 2015-01-23 15:08:40 +0100 | [diff] [blame] | 404 | for (i = 0; i < 2; i++) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 405 | if (line6pcm->volume_playback[i] != |
| 406 | ucontrol->value.integer.value[i]) { |
| 407 | line6pcm->volume_playback[i] = |
| 408 | ucontrol->value.integer.value[i]; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 409 | changed = 1; |
| 410 | } |
| 411 | |
| 412 | return changed; |
| 413 | } |
| 414 | |
| 415 | /* control definition */ |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 416 | static struct snd_kcontrol_new line6_controls[] = { |
| 417 | { |
| 418 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 419 | .name = "PCM Playback Volume", |
| 420 | .info = snd_line6_control_playback_info, |
| 421 | .get = snd_line6_control_playback_get, |
| 422 | .put = snd_line6_control_playback_put |
| 423 | }, |
| 424 | { |
| 425 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 426 | .name = "Impulse Response Volume", |
| 427 | .info = snd_line6_impulse_volume_info, |
| 428 | .get = snd_line6_impulse_volume_get, |
| 429 | .put = snd_line6_impulse_volume_put |
| 430 | }, |
| 431 | { |
| 432 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 433 | .name = "Impulse Response Period", |
| 434 | .info = snd_line6_impulse_period_info, |
| 435 | .get = snd_line6_impulse_period_get, |
| 436 | .put = snd_line6_impulse_period_put |
| 437 | }, |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 438 | }; |
| 439 | |
| 440 | /* |
| 441 | Cleanup the PCM device. |
| 442 | */ |
Andrej Krutak | b2233d9 | 2016-09-18 20:59:21 +0200 | [diff] [blame] | 443 | static void cleanup_urbs(struct line6_pcm_stream *pcms, int iso_buffers) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 444 | { |
| 445 | int i; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 446 | |
Andrej Krutak | b2233d9 | 2016-09-18 20:59:21 +0200 | [diff] [blame] | 447 | /* Most likely impossible in current code... */ |
| 448 | if (pcms->urbs == NULL) |
| 449 | return; |
| 450 | |
| 451 | for (i = 0; i < iso_buffers; i++) { |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 452 | if (pcms->urbs[i]) { |
| 453 | usb_kill_urb(pcms->urbs[i]); |
| 454 | usb_free_urb(pcms->urbs[i]); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 455 | } |
| 456 | } |
Andrej Krutak | b2233d9 | 2016-09-18 20:59:21 +0200 | [diff] [blame] | 457 | kfree(pcms->urbs); |
| 458 | pcms->urbs = NULL; |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | static void line6_cleanup_pcm(struct snd_pcm *pcm) |
| 462 | { |
| 463 | struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm); |
| 464 | |
Andrej Krutak | b2233d9 | 2016-09-18 20:59:21 +0200 | [diff] [blame] | 465 | cleanup_urbs(&line6pcm->out, line6pcm->line6->iso_buffers); |
| 466 | cleanup_urbs(&line6pcm->in, line6pcm->line6->iso_buffers); |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 467 | kfree(line6pcm); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* create a PCM device */ |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 471 | static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 472 | { |
| 473 | struct snd_pcm *pcm; |
| 474 | int err; |
| 475 | |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 476 | err = snd_pcm_new(line6->card, (char *)line6->properties->name, |
| 477 | 0, 1, 1, pcm_ret); |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 478 | if (err < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 479 | return err; |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 480 | pcm = *pcm_ret; |
| 481 | strcpy(pcm->name, line6->properties->name); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 482 | |
| 483 | /* set operators */ |
Shawn Bohrer | afb9091 | 2009-11-15 22:17:57 -0600 | [diff] [blame] | 484 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 485 | &snd_line6_playback_ops); |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 486 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 487 | |
| 488 | /* pre-allocation of buffers */ |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 489 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 490 | snd_dma_continuous_data |
| 491 | (GFP_KERNEL), 64 * 1024, |
| 492 | 128 * 1024); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | /* |
Takashi Iwai | 5a47531 | 2015-01-19 16:15:54 +0100 | [diff] [blame] | 497 | Sync with PCM stream stops. |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 498 | */ |
| 499 | void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm) |
| 500 | { |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 501 | line6_unlink_audio_urbs(line6pcm, &line6pcm->out); |
| 502 | line6_unlink_audio_urbs(line6pcm, &line6pcm->in); |
| 503 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out); |
| 504 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 508 | Create and register the PCM device and mixer entries. |
| 509 | Create URBs for playback and capture. |
| 510 | */ |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 511 | int line6_init_pcm(struct usb_line6 *line6, |
| 512 | struct line6_pcm_properties *properties) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 513 | { |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 514 | int i, err; |
Chris Rorvick | 16d603d | 2015-01-12 12:42:55 -0800 | [diff] [blame] | 515 | unsigned ep_read = line6->properties->ep_audio_r; |
| 516 | unsigned ep_write = line6->properties->ep_audio_w; |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 517 | struct snd_pcm *pcm; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 518 | struct snd_line6_pcm *line6pcm; |
| 519 | |
Chris Rorvick | 4cb1a4a | 2015-01-12 12:42:45 -0800 | [diff] [blame] | 520 | if (!(line6->properties->capabilities & LINE6_CAP_PCM)) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 521 | return 0; /* skip PCM initialization and report success */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 522 | |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 523 | err = snd_line6_new_pcm(line6, &pcm); |
| 524 | if (err < 0) |
| 525 | return err; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 526 | |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 527 | line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL); |
| 528 | if (!line6pcm) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 529 | return -ENOMEM; |
| 530 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 531 | mutex_init(&line6pcm->state_mutex); |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 532 | line6pcm->pcm = pcm; |
| 533 | line6pcm->properties = properties; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 534 | line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255; |
| 535 | line6pcm->volume_monitor = 255; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 536 | line6pcm->line6 = line6; |
Stefan Hajnoczi | 3b08db3 | 2011-11-23 08:20:44 +0000 | [diff] [blame] | 537 | |
Andrej Krutak | 7a0f55a | 2016-09-18 20:59:23 +0200 | [diff] [blame^] | 538 | line6pcm->max_packet_size_in = |
| 539 | usb_maxpacket(line6->usbdev, |
| 540 | usb_rcvisocpipe(line6->usbdev, ep_read), 0); |
| 541 | line6pcm->max_packet_size_out = |
| 542 | usb_maxpacket(line6->usbdev, |
| 543 | usb_sndisocpipe(line6->usbdev, ep_write), 1); |
Stefan Hajnoczi | 3b08db3 | 2011-11-23 08:20:44 +0000 | [diff] [blame] | 544 | |
Takashi Iwai | ad0119a | 2015-01-23 16:10:57 +0100 | [diff] [blame] | 545 | spin_lock_init(&line6pcm->out.lock); |
| 546 | spin_lock_init(&line6pcm->in.lock); |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 547 | line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 548 | |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 549 | line6->line6pcm = line6pcm; |
| 550 | |
| 551 | pcm->private_data = line6pcm; |
| 552 | pcm->private_free = line6_cleanup_pcm; |
| 553 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 554 | err = line6_create_audio_out_urbs(line6pcm); |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 555 | if (err < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 556 | return err; |
| 557 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 558 | err = line6_create_audio_in_urbs(line6pcm); |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 559 | if (err < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 560 | return err; |
| 561 | |
| 562 | /* mixer: */ |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 563 | for (i = 0; i < ARRAY_SIZE(line6_controls); i++) { |
| 564 | err = snd_ctl_add(line6->card, |
| 565 | snd_ctl_new1(&line6_controls[i], line6pcm)); |
| 566 | if (err < 0) |
| 567 | return err; |
| 568 | } |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 569 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 570 | return 0; |
| 571 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 572 | EXPORT_SYMBOL_GPL(line6_init_pcm); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 573 | |
| 574 | /* prepare pcm callback */ |
| 575 | int snd_line6_prepare(struct snd_pcm_substream *substream) |
| 576 | { |
| 577 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 578 | struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 579 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 580 | mutex_lock(&line6pcm->state_mutex); |
| 581 | if (!pstr->running) |
| 582 | line6_wait_clear_audio_urbs(line6pcm, pstr); |
Stefan Hajnoczi | 665f3f5 | 2011-12-10 02:12:31 +0100 | [diff] [blame] | 583 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 584 | if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) { |
Takashi Iwai | ad0119a | 2015-01-23 16:10:57 +0100 | [diff] [blame] | 585 | line6pcm->out.count = 0; |
| 586 | line6pcm->out.pos = 0; |
| 587 | line6pcm->out.pos_done = 0; |
| 588 | line6pcm->out.bytes = 0; |
| 589 | line6pcm->in.count = 0; |
| 590 | line6pcm->in.pos_done = 0; |
| 591 | line6pcm->in.bytes = 0; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Takashi Iwai | 63e20df | 2015-01-27 15:24:09 +0100 | [diff] [blame] | 594 | mutex_unlock(&line6pcm->state_mutex); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 595 | return 0; |
| 596 | } |