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