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