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]; |
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 | 1027f47 | 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) |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 54 | line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 55 | else |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 56 | line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE); |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 57 | return 1; |
Markus Grabner | 1027f47 | 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 | 1027f47 | 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 | 1027f47 | 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 | 1027f47 | 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 | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 78 | } |
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 | 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 | 1027f47 | 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 | e90576c | 2015-01-23 16:25:03 +0100 | [diff] [blame] | 135 | static int line6_alloc_stream_buffer(struct snd_line6_pcm *line6pcm, |
| 136 | struct line6_pcm_stream *pcms) |
| 137 | { |
| 138 | /* Invoked multiple times in a row so allocate once only */ |
| 139 | if (pcms->buffer) |
| 140 | return 0; |
| 141 | |
| 142 | pcms->buffer = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS * |
| 143 | line6pcm->max_packet_size, GFP_KERNEL); |
| 144 | if (!pcms->buffer) |
| 145 | return -ENOMEM; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static void line6_free_stream_buffer(struct snd_line6_pcm *line6pcm, |
| 150 | struct line6_pcm_stream *pcms) |
| 151 | { |
| 152 | kfree(pcms->buffer); |
| 153 | pcms->buffer = NULL; |
| 154 | } |
| 155 | |
Markus Grabner | 6b02a17e | 2011-12-10 02:12:32 +0100 | [diff] [blame] | 156 | static bool test_flags(unsigned long flags0, unsigned long flags1, |
| 157 | unsigned long mask) |
| 158 | { |
| 159 | return ((flags0 & mask) == 0) && ((flags1 & mask) != 0); |
| 160 | } |
| 161 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 162 | int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 163 | { |
Arnd Bergmann | 9f61360 | 2013-06-21 21:55:35 +0200 | [diff] [blame] | 164 | unsigned long flags_old, flags_new, flags_final; |
| 165 | int err; |
| 166 | |
| 167 | do { |
| 168 | flags_old = ACCESS_ONCE(line6pcm->flags); |
| 169 | flags_new = flags_old | channels; |
| 170 | } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old); |
| 171 | |
Takashi Iwai | 5343ecf4 | 2015-01-23 14:34:42 +0100 | [diff] [blame] | 172 | flags_final = 0; |
Johannes Thumshirn | 82a74d4 | 2012-05-05 16:31:52 +0200 | [diff] [blame] | 173 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 174 | if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) { |
Takashi Iwai | e90576c | 2015-01-23 16:25:03 +0100 | [diff] [blame] | 175 | err = line6_alloc_stream_buffer(line6pcm, &line6pcm->in); |
| 176 | if (err < 0) |
| 177 | goto pcm_acquire_error; |
Takashi Iwai | 5343ecf4 | 2015-01-23 14:34:42 +0100 | [diff] [blame] | 178 | flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER; |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 182 | /* |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 183 | Waiting for completion of active URBs in the stop handler is |
| 184 | a bug, we therefore report an error if capturing is restarted |
| 185 | too soon. |
| 186 | */ |
Takashi Iwai | ad0119a | 2015-01-23 16:10:57 +0100 | [diff] [blame] | 187 | if (line6pcm->in.active_urbs || line6pcm->in.unlink_urbs) { |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 188 | dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n"); |
Takashi Iwai | eab22e4 | 2015-01-23 13:00:03 +0100 | [diff] [blame] | 189 | err = -EBUSY; |
| 190 | goto pcm_acquire_error; |
Markus Grabner | 6b02a17e | 2011-12-10 02:12:32 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Takashi Iwai | ad0119a | 2015-01-23 16:10:57 +0100 | [diff] [blame] | 193 | line6pcm->in.count = 0; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 194 | err = line6_submit_audio_in_all_urbs(line6pcm); |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 195 | |
Markus Grabner | 6b02a17e | 2011-12-10 02:12:32 +0100 | [diff] [blame] | 196 | if (err < 0) |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 197 | goto pcm_acquire_error; |
| 198 | |
| 199 | flags_final |= channels & LINE6_BITS_CAPTURE_STREAM; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 200 | } |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 201 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 202 | if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) { |
Takashi Iwai | e90576c | 2015-01-23 16:25:03 +0100 | [diff] [blame] | 203 | err = line6_alloc_stream_buffer(line6pcm, &line6pcm->out); |
| 204 | if (err < 0) |
| 205 | goto pcm_acquire_error; |
Takashi Iwai | 5343ecf4 | 2015-01-23 14:34:42 +0100 | [diff] [blame] | 206 | flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER; |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 210 | /* |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 211 | See comment above regarding PCM restart. |
| 212 | */ |
Takashi Iwai | ad0119a | 2015-01-23 16:10:57 +0100 | [diff] [blame] | 213 | if (line6pcm->out.active_urbs || line6pcm->out.unlink_urbs) { |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 214 | dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n"); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 215 | return -EBUSY; |
Markus Grabner | 6b02a17e | 2011-12-10 02:12:32 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Takashi Iwai | ad0119a | 2015-01-23 16:10:57 +0100 | [diff] [blame] | 218 | line6pcm->out.count = 0; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 219 | err = line6_submit_audio_out_all_urbs(line6pcm); |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 220 | |
Markus Grabner | 6b02a17e | 2011-12-10 02:12:32 +0100 | [diff] [blame] | 221 | if (err < 0) |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 222 | goto pcm_acquire_error; |
| 223 | |
| 224 | flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 225 | } |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 226 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 227 | return 0; |
Markus Grabner | 6b02a17e | 2011-12-10 02:12:32 +0100 | [diff] [blame] | 228 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 229 | pcm_acquire_error: |
| 230 | /* |
| 231 | If not all requested resources/streams could be obtained, release |
| 232 | those which were successfully obtained (if any). |
| 233 | */ |
Takashi Iwai | 5343ecf4 | 2015-01-23 14:34:42 +0100 | [diff] [blame] | 234 | line6_pcm_release(line6pcm, flags_final); |
Markus Grabner | 6b02a17e | 2011-12-10 02:12:32 +0100 | [diff] [blame] | 235 | return err; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 236 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 237 | EXPORT_SYMBOL_GPL(line6_pcm_acquire); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 238 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 239 | int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 240 | { |
Arnd Bergmann | 9f61360 | 2013-06-21 21:55:35 +0200 | [diff] [blame] | 241 | unsigned long flags_old, flags_new; |
| 242 | |
| 243 | do { |
| 244 | flags_old = ACCESS_ONCE(line6pcm->flags); |
| 245 | flags_new = flags_old & ~channels; |
| 246 | } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 247 | |
Takashi Iwai | f2bb614 | 2015-01-27 16:17:26 +0100 | [diff] [blame^] | 248 | if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM)) { |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 249 | line6_unlink_audio_urbs(line6pcm, &line6pcm->in); |
Takashi Iwai | f2bb614 | 2015-01-27 16:17:26 +0100 | [diff] [blame^] | 250 | line6pcm->prev_fbuf = NULL; |
| 251 | line6pcm->prev_fsize = 0; |
| 252 | } |
Markus Grabner | 6b02a17e | 2011-12-10 02:12:32 +0100 | [diff] [blame] | 253 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 254 | if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) { |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 255 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in); |
Takashi Iwai | e90576c | 2015-01-23 16:25:03 +0100 | [diff] [blame] | 256 | line6_free_stream_buffer(line6pcm, &line6pcm->in); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 259 | if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM)) |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 260 | line6_unlink_audio_urbs(line6pcm, &line6pcm->out); |
Markus Grabner | 6b02a17e | 2011-12-10 02:12:32 +0100 | [diff] [blame] | 261 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 262 | if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) { |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 263 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out); |
Takashi Iwai | e90576c | 2015-01-23 16:25:03 +0100 | [diff] [blame] | 264 | line6_free_stream_buffer(line6pcm, &line6pcm->out); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 265 | } |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 269 | EXPORT_SYMBOL_GPL(line6_pcm_release); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 270 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 271 | /* trigger callback */ |
| 272 | int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd) |
| 273 | { |
| 274 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 275 | struct snd_pcm_substream *s; |
Takashi Iwai | f2a7622 | 2015-01-23 14:49:22 +0100 | [diff] [blame] | 276 | int err = 0; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 277 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 278 | clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 279 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 280 | snd_pcm_group_for_each_entry(s, substream) { |
Takashi Iwai | 7d70c81 | 2015-01-19 15:11:17 +0100 | [diff] [blame] | 281 | if (s->pcm->card != substream->pcm->card) |
| 282 | continue; |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 283 | switch (s->stream) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 284 | case SNDRV_PCM_STREAM_PLAYBACK: |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 285 | err = snd_line6_playback_trigger(line6pcm, cmd); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 286 | break; |
| 287 | |
| 288 | case SNDRV_PCM_STREAM_CAPTURE: |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 289 | err = snd_line6_capture_trigger(line6pcm, cmd); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 290 | break; |
| 291 | |
| 292 | default: |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 293 | dev_err(line6pcm->line6->ifcdev, |
| 294 | "Unknown stream direction %d\n", s->stream); |
Takashi Iwai | f2a7622 | 2015-01-23 14:49:22 +0100 | [diff] [blame] | 295 | err = -EINVAL; |
| 296 | break; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 297 | } |
Takashi Iwai | f2a7622 | 2015-01-23 14:49:22 +0100 | [diff] [blame] | 298 | if (err < 0) |
| 299 | break; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Takashi Iwai | f2a7622 | 2015-01-23 14:49:22 +0100 | [diff] [blame] | 302 | return err; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* control info callback */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 306 | static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol, |
| 307 | struct snd_ctl_elem_info *uinfo) |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 308 | { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 309 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 310 | uinfo->count = 2; |
| 311 | uinfo->value.integer.min = 0; |
| 312 | uinfo->value.integer.max = 256; |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* control get callback */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 317 | static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol, |
| 318 | struct snd_ctl_elem_value *ucontrol) |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 319 | { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 320 | int i; |
| 321 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
| 322 | |
Takashi Iwai | 9fb754b | 2015-01-23 15:08:40 +0100 | [diff] [blame] | 323 | for (i = 0; i < 2; i++) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 324 | ucontrol->value.integer.value[i] = line6pcm->volume_playback[i]; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /* control put callback */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 330 | static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol, |
| 331 | struct snd_ctl_elem_value *ucontrol) |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 332 | { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 333 | int i, changed = 0; |
| 334 | struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol); |
| 335 | |
Takashi Iwai | 9fb754b | 2015-01-23 15:08:40 +0100 | [diff] [blame] | 336 | for (i = 0; i < 2; i++) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 337 | if (line6pcm->volume_playback[i] != |
| 338 | ucontrol->value.integer.value[i]) { |
| 339 | line6pcm->volume_playback[i] = |
| 340 | ucontrol->value.integer.value[i]; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 341 | changed = 1; |
| 342 | } |
| 343 | |
| 344 | return changed; |
| 345 | } |
| 346 | |
| 347 | /* control definition */ |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 348 | static struct snd_kcontrol_new line6_controls[] = { |
| 349 | { |
| 350 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 351 | .name = "PCM Playback Volume", |
| 352 | .info = snd_line6_control_playback_info, |
| 353 | .get = snd_line6_control_playback_get, |
| 354 | .put = snd_line6_control_playback_put |
| 355 | }, |
| 356 | { |
| 357 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 358 | .name = "Impulse Response Volume", |
| 359 | .info = snd_line6_impulse_volume_info, |
| 360 | .get = snd_line6_impulse_volume_get, |
| 361 | .put = snd_line6_impulse_volume_put |
| 362 | }, |
| 363 | { |
| 364 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 365 | .name = "Impulse Response Period", |
| 366 | .info = snd_line6_impulse_period_info, |
| 367 | .get = snd_line6_impulse_period_get, |
| 368 | .put = snd_line6_impulse_period_put |
| 369 | }, |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | /* |
| 373 | Cleanup the PCM device. |
| 374 | */ |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 375 | static void cleanup_urbs(struct line6_pcm_stream *pcms) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 376 | { |
| 377 | int i; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 378 | |
Takashi Iwai | 9fb754b | 2015-01-23 15:08:40 +0100 | [diff] [blame] | 379 | for (i = 0; i < LINE6_ISO_BUFFERS; i++) { |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 380 | if (pcms->urbs[i]) { |
| 381 | usb_kill_urb(pcms->urbs[i]); |
| 382 | usb_free_urb(pcms->urbs[i]); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static void line6_cleanup_pcm(struct snd_pcm *pcm) |
| 388 | { |
| 389 | struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm); |
| 390 | |
| 391 | cleanup_urbs(&line6pcm->out); |
| 392 | cleanup_urbs(&line6pcm->in); |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 393 | kfree(line6pcm); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* create a PCM device */ |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 397 | 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] | 398 | { |
| 399 | struct snd_pcm *pcm; |
| 400 | int err; |
| 401 | |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 402 | err = snd_pcm_new(line6->card, (char *)line6->properties->name, |
| 403 | 0, 1, 1, pcm_ret); |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 404 | if (err < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 405 | return err; |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 406 | pcm = *pcm_ret; |
| 407 | strcpy(pcm->name, line6->properties->name); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 408 | |
| 409 | /* set operators */ |
Shawn Bohrer | afb9091 | 2009-11-15 22:17:57 -0600 | [diff] [blame] | 410 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 411 | &snd_line6_playback_ops); |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 412 | 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] | 413 | |
| 414 | /* pre-allocation of buffers */ |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 415 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 416 | snd_dma_continuous_data |
| 417 | (GFP_KERNEL), 64 * 1024, |
| 418 | 128 * 1024); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | /* |
Takashi Iwai | 5a47531 | 2015-01-19 16:15:54 +0100 | [diff] [blame] | 423 | Sync with PCM stream stops. |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 424 | */ |
| 425 | void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm) |
| 426 | { |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 427 | line6_unlink_audio_urbs(line6pcm, &line6pcm->out); |
| 428 | line6_unlink_audio_urbs(line6pcm, &line6pcm->in); |
| 429 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out); |
| 430 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 434 | Create and register the PCM device and mixer entries. |
| 435 | Create URBs for playback and capture. |
| 436 | */ |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 437 | int line6_init_pcm(struct usb_line6 *line6, |
| 438 | struct line6_pcm_properties *properties) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 439 | { |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 440 | int i, err; |
Chris Rorvick | 16d603d | 2015-01-12 12:42:55 -0800 | [diff] [blame] | 441 | unsigned ep_read = line6->properties->ep_audio_r; |
| 442 | unsigned ep_write = line6->properties->ep_audio_w; |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 443 | struct snd_pcm *pcm; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 444 | struct snd_line6_pcm *line6pcm; |
| 445 | |
Chris Rorvick | 4cb1a4a | 2015-01-12 12:42:45 -0800 | [diff] [blame] | 446 | if (!(line6->properties->capabilities & LINE6_CAP_PCM)) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 447 | return 0; /* skip PCM initialization and report success */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 448 | |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 449 | err = snd_line6_new_pcm(line6, &pcm); |
| 450 | if (err < 0) |
| 451 | return err; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 452 | |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 453 | line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL); |
| 454 | if (!line6pcm) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 455 | return -ENOMEM; |
| 456 | |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 457 | line6pcm->pcm = pcm; |
| 458 | line6pcm->properties = properties; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 459 | line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255; |
| 460 | line6pcm->volume_monitor = 255; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 461 | line6pcm->line6 = line6; |
Stefan Hajnoczi | 3b08db3 | 2011-11-23 08:20:44 +0000 | [diff] [blame] | 462 | |
| 463 | /* Read and write buffers are sized identically, so choose minimum */ |
| 464 | line6pcm->max_packet_size = min( |
| 465 | usb_maxpacket(line6->usbdev, |
| 466 | usb_rcvisocpipe(line6->usbdev, ep_read), 0), |
| 467 | usb_maxpacket(line6->usbdev, |
| 468 | usb_sndisocpipe(line6->usbdev, ep_write), 1)); |
| 469 | |
Takashi Iwai | ad0119a | 2015-01-23 16:10:57 +0100 | [diff] [blame] | 470 | spin_lock_init(&line6pcm->out.lock); |
| 471 | spin_lock_init(&line6pcm->in.lock); |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 472 | line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 473 | |
Takashi Iwai | b45a7c5 | 2015-01-19 14:41:57 +0100 | [diff] [blame] | 474 | line6->line6pcm = line6pcm; |
| 475 | |
| 476 | pcm->private_data = line6pcm; |
| 477 | pcm->private_free = line6_cleanup_pcm; |
| 478 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 479 | err = line6_create_audio_out_urbs(line6pcm); |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 480 | if (err < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 481 | return err; |
| 482 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 483 | err = line6_create_audio_in_urbs(line6pcm); |
Greg Kroah-Hartman | 68dc3dd | 2009-02-27 22:43:30 -0800 | [diff] [blame] | 484 | if (err < 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 485 | return err; |
| 486 | |
| 487 | /* mixer: */ |
Takashi Iwai | 075587b | 2015-01-19 14:28:25 +0100 | [diff] [blame] | 488 | for (i = 0; i < ARRAY_SIZE(line6_controls); i++) { |
| 489 | err = snd_ctl_add(line6->card, |
| 490 | snd_ctl_new1(&line6_controls[i], line6pcm)); |
| 491 | if (err < 0) |
| 492 | return err; |
| 493 | } |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 494 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 495 | return 0; |
| 496 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 497 | EXPORT_SYMBOL_GPL(line6_init_pcm); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 498 | |
| 499 | /* prepare pcm callback */ |
| 500 | int snd_line6_prepare(struct snd_pcm_substream *substream) |
| 501 | { |
| 502 | struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream); |
| 503 | |
Stefan Hajnoczi | 665f3f5 | 2011-12-10 02:12:31 +0100 | [diff] [blame] | 504 | switch (substream->stream) { |
| 505 | case SNDRV_PCM_STREAM_PLAYBACK: |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 506 | if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0) { |
| 507 | line6_unlink_audio_urbs(line6pcm, &line6pcm->out); |
| 508 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out); |
| 509 | } |
Stefan Hajnoczi | 665f3f5 | 2011-12-10 02:12:31 +0100 | [diff] [blame] | 510 | break; |
| 511 | |
| 512 | case SNDRV_PCM_STREAM_CAPTURE: |
Takashi Iwai | d8131e6 | 2015-01-23 16:18:42 +0100 | [diff] [blame] | 513 | if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0) { |
| 514 | line6_unlink_audio_urbs(line6pcm, &line6pcm->in); |
| 515 | line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in); |
| 516 | } |
Stefan Hajnoczi | 665f3f5 | 2011-12-10 02:12:31 +0100 | [diff] [blame] | 517 | break; |
Stefan Hajnoczi | 665f3f5 | 2011-12-10 02:12:31 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Markus Grabner | 0ca5488 | 2012-01-20 00:09:09 +0100 | [diff] [blame] | 520 | if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) { |
Takashi Iwai | ad0119a | 2015-01-23 16:10:57 +0100 | [diff] [blame] | 521 | line6pcm->out.count = 0; |
| 522 | line6pcm->out.pos = 0; |
| 523 | line6pcm->out.pos_done = 0; |
| 524 | line6pcm->out.bytes = 0; |
| 525 | line6pcm->in.count = 0; |
| 526 | line6pcm->in.pos_done = 0; |
| 527 | line6pcm->in.bytes = 0; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |