Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Loopback soundcard |
| 3 | * |
| 4 | * Original code: |
| 5 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
| 6 | * |
| 7 | * More accurate positioning and full-duplex support: |
| 8 | * Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de> |
| 9 | * |
| 10 | * Major (almost complete) rewrite: |
| 11 | * Copyright (c) by Takashi Iwai <tiwai@suse.de> |
| 12 | * |
| 13 | * A next major update in 2010 (separate timers for playback and capture): |
| 14 | * Copyright (c) Jaroslav Kysela <perex@perex.cz> |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/jiffies.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/time.h> |
| 36 | #include <linux/wait.h> |
Paul Gortmaker | 65a7721 | 2011-07-15 13:13:37 -0400 | [diff] [blame] | 37 | #include <linux/module.h> |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 38 | #include <linux/platform_device.h> |
| 39 | #include <sound/core.h> |
| 40 | #include <sound/control.h> |
| 41 | #include <sound/pcm.h> |
Takashi Iwai | b088b53 | 2018-01-05 16:15:33 +0100 | [diff] [blame] | 42 | #include <sound/pcm_params.h> |
Jaroslav Kysela | e74670b | 2010-10-18 09:43:10 +0200 | [diff] [blame] | 43 | #include <sound/info.h> |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 44 | #include <sound/initval.h> |
| 45 | |
| 46 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
| 47 | MODULE_DESCRIPTION("A loopback soundcard"); |
| 48 | MODULE_LICENSE("GPL"); |
| 49 | MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}"); |
| 50 | |
| 51 | #define MAX_PCM_SUBSTREAMS 8 |
| 52 | |
| 53 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 54 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
Rusty Russell | a67ff6a | 2011-12-15 13:49:36 +1030 | [diff] [blame] | 55 | static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 56 | static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8}; |
| 57 | static int pcm_notify[SNDRV_CARDS]; |
| 58 | |
| 59 | module_param_array(index, int, NULL, 0444); |
| 60 | MODULE_PARM_DESC(index, "Index value for loopback soundcard."); |
| 61 | module_param_array(id, charp, NULL, 0444); |
| 62 | MODULE_PARM_DESC(id, "ID string for loopback soundcard."); |
| 63 | module_param_array(enable, bool, NULL, 0444); |
| 64 | MODULE_PARM_DESC(enable, "Enable this loopback soundcard."); |
| 65 | module_param_array(pcm_substreams, int, NULL, 0444); |
| 66 | MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver."); |
| 67 | module_param_array(pcm_notify, int, NULL, 0444); |
| 68 | MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes."); |
| 69 | |
| 70 | #define NO_PITCH 100000 |
| 71 | |
| 72 | struct loopback_pcm; |
| 73 | |
| 74 | struct loopback_cable { |
| 75 | spinlock_t lock; |
| 76 | struct loopback_pcm *streams[2]; |
| 77 | struct snd_pcm_hardware hw; |
| 78 | /* flags */ |
| 79 | unsigned int valid; |
| 80 | unsigned int running; |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 81 | unsigned int pause; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | struct loopback_setup { |
| 85 | unsigned int notify: 1; |
| 86 | unsigned int rate_shift; |
| 87 | unsigned int format; |
| 88 | unsigned int rate; |
| 89 | unsigned int channels; |
| 90 | struct snd_ctl_elem_id active_id; |
| 91 | struct snd_ctl_elem_id format_id; |
| 92 | struct snd_ctl_elem_id rate_id; |
| 93 | struct snd_ctl_elem_id channels_id; |
| 94 | }; |
| 95 | |
| 96 | struct loopback { |
| 97 | struct snd_card *card; |
| 98 | struct mutex cable_lock; |
| 99 | struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2]; |
| 100 | struct snd_pcm *pcm[2]; |
| 101 | struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2]; |
| 102 | }; |
| 103 | |
| 104 | struct loopback_pcm { |
| 105 | struct loopback *loopback; |
| 106 | struct snd_pcm_substream *substream; |
| 107 | struct loopback_cable *cable; |
| 108 | unsigned int pcm_buffer_size; |
| 109 | unsigned int buf_pos; /* position in buffer */ |
| 110 | unsigned int silent_size; |
| 111 | /* PCM parameters */ |
| 112 | unsigned int pcm_period_size; |
| 113 | unsigned int pcm_bps; /* bytes per second */ |
| 114 | unsigned int pcm_salign; /* bytes per sample * channels */ |
| 115 | unsigned int pcm_rate_shift; /* rate shift value */ |
| 116 | /* flags */ |
| 117 | unsigned int period_update_pending :1; |
| 118 | /* timer stuff */ |
| 119 | unsigned int irq_pos; /* fractional IRQ position */ |
| 120 | unsigned int period_size_frac; |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 121 | unsigned int last_drift; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 122 | unsigned long last_jiffies; |
| 123 | struct timer_list timer; |
| 124 | }; |
| 125 | |
| 126 | static struct platform_device *devices[SNDRV_CARDS]; |
| 127 | |
| 128 | static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x) |
| 129 | { |
| 130 | if (dpcm->pcm_rate_shift == NO_PITCH) { |
| 131 | x /= HZ; |
| 132 | } else { |
| 133 | x = div_u64(NO_PITCH * (unsigned long long)x, |
| 134 | HZ * (unsigned long long)dpcm->pcm_rate_shift); |
| 135 | } |
| 136 | return x - (x % dpcm->pcm_salign); |
| 137 | } |
| 138 | |
| 139 | static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x) |
| 140 | { |
| 141 | if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */ |
| 142 | return x * HZ; |
| 143 | } else { |
| 144 | x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ, |
| 145 | NO_PITCH); |
| 146 | } |
| 147 | return x; |
| 148 | } |
| 149 | |
| 150 | static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm) |
| 151 | { |
| 152 | int device = dpcm->substream->pstr->pcm->device; |
| 153 | |
| 154 | if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 155 | device ^= 1; |
| 156 | return &dpcm->loopback->setup[dpcm->substream->number][device]; |
| 157 | } |
| 158 | |
| 159 | static inline unsigned int get_notify(struct loopback_pcm *dpcm) |
| 160 | { |
| 161 | return get_setup(dpcm)->notify; |
| 162 | } |
| 163 | |
| 164 | static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm) |
| 165 | { |
| 166 | return get_setup(dpcm)->rate_shift; |
| 167 | } |
| 168 | |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 169 | /* call in cable->lock */ |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 170 | static void loopback_timer_start(struct loopback_pcm *dpcm) |
| 171 | { |
| 172 | unsigned long tick; |
| 173 | unsigned int rate_shift = get_rate_shift(dpcm); |
| 174 | |
| 175 | if (rate_shift != dpcm->pcm_rate_shift) { |
| 176 | dpcm->pcm_rate_shift = rate_shift; |
| 177 | dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size); |
| 178 | } |
Jaroslav Kysela | 0db7102 | 2010-10-14 21:46:12 +0200 | [diff] [blame] | 179 | if (dpcm->period_size_frac <= dpcm->irq_pos) { |
| 180 | dpcm->irq_pos %= dpcm->period_size_frac; |
| 181 | dpcm->period_update_pending = 1; |
| 182 | } |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 183 | tick = dpcm->period_size_frac - dpcm->irq_pos; |
| 184 | tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps; |
Takashi Iwai | db97455 | 2015-01-19 11:27:31 +0100 | [diff] [blame] | 185 | mod_timer(&dpcm->timer, jiffies + tick); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 188 | /* call in cable->lock */ |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 189 | static inline void loopback_timer_stop(struct loopback_pcm *dpcm) |
| 190 | { |
| 191 | del_timer(&dpcm->timer); |
Jaroslav Kysela | e74670b | 2010-10-18 09:43:10 +0200 | [diff] [blame] | 192 | dpcm->timer.expires = 0; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Takashi Iwai | 67a01af | 2018-03-22 08:56:06 +0100 | [diff] [blame] | 195 | static inline void loopback_timer_stop_sync(struct loopback_pcm *dpcm) |
| 196 | { |
| 197 | del_timer_sync(&dpcm->timer); |
| 198 | } |
| 199 | |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 200 | #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK) |
| 201 | #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE) |
| 202 | #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE) |
| 203 | |
| 204 | static int loopback_check_format(struct loopback_cable *cable, int stream) |
| 205 | { |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 206 | struct snd_pcm_runtime *runtime, *cruntime; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 207 | struct loopback_setup *setup; |
| 208 | struct snd_card *card; |
| 209 | int check; |
| 210 | |
| 211 | if (cable->valid != CABLE_VALID_BOTH) { |
| 212 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 213 | goto __notify; |
| 214 | return 0; |
| 215 | } |
| 216 | runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]-> |
| 217 | substream->runtime; |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 218 | cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]-> |
| 219 | substream->runtime; |
| 220 | check = runtime->format != cruntime->format || |
| 221 | runtime->rate != cruntime->rate || |
| 222 | runtime->channels != cruntime->channels; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 223 | if (!check) |
| 224 | return 0; |
| 225 | if (stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 226 | return -EIO; |
| 227 | } else { |
| 228 | snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]-> |
| 229 | substream, SNDRV_PCM_STATE_DRAINING); |
| 230 | __notify: |
| 231 | runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]-> |
| 232 | substream->runtime; |
| 233 | setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]); |
| 234 | card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card; |
| 235 | if (setup->format != runtime->format) { |
| 236 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 237 | &setup->format_id); |
| 238 | setup->format = runtime->format; |
| 239 | } |
| 240 | if (setup->rate != runtime->rate) { |
| 241 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 242 | &setup->rate_id); |
| 243 | setup->rate = runtime->rate; |
| 244 | } |
| 245 | if (setup->channels != runtime->channels) { |
| 246 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 247 | &setup->channels_id); |
| 248 | setup->channels = runtime->channels; |
| 249 | } |
| 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static void loopback_active_notify(struct loopback_pcm *dpcm) |
| 255 | { |
| 256 | snd_ctl_notify(dpcm->loopback->card, |
| 257 | SNDRV_CTL_EVENT_MASK_VALUE, |
| 258 | &get_setup(dpcm)->active_id); |
| 259 | } |
| 260 | |
| 261 | static int loopback_trigger(struct snd_pcm_substream *substream, int cmd) |
| 262 | { |
| 263 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 264 | struct loopback_pcm *dpcm = runtime->private_data; |
| 265 | struct loopback_cable *cable = dpcm->cable; |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 266 | int err, stream = 1 << substream->stream; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 267 | |
| 268 | switch (cmd) { |
| 269 | case SNDRV_PCM_TRIGGER_START: |
| 270 | err = loopback_check_format(cable, substream->stream); |
| 271 | if (err < 0) |
| 272 | return err; |
| 273 | dpcm->last_jiffies = jiffies; |
| 274 | dpcm->pcm_rate_shift = 0; |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 275 | dpcm->last_drift = 0; |
Jaroslav Kysela | dd04bb1 | 2010-10-20 08:27:02 +0200 | [diff] [blame] | 276 | spin_lock(&cable->lock); |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 277 | cable->running |= stream; |
| 278 | cable->pause &= ~stream; |
Jaroslav Kysela | dd04bb1 | 2010-10-20 08:27:02 +0200 | [diff] [blame] | 279 | loopback_timer_start(dpcm); |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 280 | spin_unlock(&cable->lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 281 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 282 | loopback_active_notify(dpcm); |
| 283 | break; |
| 284 | case SNDRV_PCM_TRIGGER_STOP: |
Jaroslav Kysela | dd04bb1 | 2010-10-20 08:27:02 +0200 | [diff] [blame] | 285 | spin_lock(&cable->lock); |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 286 | cable->running &= ~stream; |
| 287 | cable->pause &= ~stream; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 288 | loopback_timer_stop(dpcm); |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 289 | spin_unlock(&cable->lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 290 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 291 | loopback_active_notify(dpcm); |
| 292 | break; |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 293 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Takashi Iwai | edac894 | 2013-02-04 10:28:15 +0100 | [diff] [blame] | 294 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 295 | spin_lock(&cable->lock); |
| 296 | cable->pause |= stream; |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 297 | loopback_timer_stop(dpcm); |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 298 | spin_unlock(&cable->lock); |
Robert Rosengren | 306a4f3 | 2018-03-26 07:24:49 +0200 | [diff] [blame] | 299 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 300 | loopback_active_notify(dpcm); |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 301 | break; |
| 302 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Takashi Iwai | edac894 | 2013-02-04 10:28:15 +0100 | [diff] [blame] | 303 | case SNDRV_PCM_TRIGGER_RESUME: |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 304 | spin_lock(&cable->lock); |
| 305 | dpcm->last_jiffies = jiffies; |
| 306 | cable->pause &= ~stream; |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 307 | loopback_timer_start(dpcm); |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 308 | spin_unlock(&cable->lock); |
Robert Rosengren | 306a4f3 | 2018-03-26 07:24:49 +0200 | [diff] [blame] | 309 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 310 | loopback_active_notify(dpcm); |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 311 | break; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 312 | default: |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | return 0; |
| 316 | } |
| 317 | |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 318 | static void params_change(struct snd_pcm_substream *substream) |
| 319 | { |
| 320 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 321 | struct loopback_pcm *dpcm = runtime->private_data; |
| 322 | struct loopback_cable *cable = dpcm->cable; |
| 323 | |
Eldad Zack | 74c34ca | 2013-04-23 01:00:41 +0200 | [diff] [blame] | 324 | cable->hw.formats = pcm_format_to_bits(runtime->format); |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 325 | cable->hw.rate_min = runtime->rate; |
| 326 | cable->hw.rate_max = runtime->rate; |
| 327 | cable->hw.channels_min = runtime->channels; |
| 328 | cable->hw.channels_max = runtime->channels; |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 331 | static int loopback_prepare(struct snd_pcm_substream *substream) |
| 332 | { |
| 333 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 334 | struct loopback_pcm *dpcm = runtime->private_data; |
| 335 | struct loopback_cable *cable = dpcm->cable; |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 336 | int bps, salign; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 337 | |
Takashi Iwai | 67a01af | 2018-03-22 08:56:06 +0100 | [diff] [blame] | 338 | loopback_timer_stop_sync(dpcm); |
| 339 | |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 340 | salign = (snd_pcm_format_width(runtime->format) * |
| 341 | runtime->channels) / 8; |
| 342 | bps = salign * runtime->rate; |
| 343 | if (bps <= 0 || salign <= 0) |
| 344 | return -EINVAL; |
| 345 | |
| 346 | dpcm->buf_pos = 0; |
| 347 | dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size); |
| 348 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 349 | /* clear capture buffer */ |
| 350 | dpcm->silent_size = dpcm->pcm_buffer_size; |
| 351 | snd_pcm_format_set_silence(runtime->format, runtime->dma_area, |
| 352 | runtime->buffer_size * runtime->channels); |
| 353 | } |
| 354 | |
| 355 | dpcm->irq_pos = 0; |
| 356 | dpcm->period_update_pending = 0; |
| 357 | dpcm->pcm_bps = bps; |
| 358 | dpcm->pcm_salign = salign; |
| 359 | dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size); |
| 360 | |
| 361 | mutex_lock(&dpcm->loopback->cable_lock); |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 362 | if (!(cable->valid & ~(1 << substream->stream)) || |
| 363 | (get_setup(dpcm)->notify && |
| 364 | substream->stream == SNDRV_PCM_STREAM_PLAYBACK)) |
| 365 | params_change(substream); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 366 | cable->valid |= 1 << substream->stream; |
| 367 | mutex_unlock(&dpcm->loopback->cable_lock); |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes) |
| 373 | { |
| 374 | struct snd_pcm_runtime *runtime = dpcm->substream->runtime; |
| 375 | char *dst = runtime->dma_area; |
| 376 | unsigned int dst_off = dpcm->buf_pos; |
| 377 | |
| 378 | if (dpcm->silent_size >= dpcm->pcm_buffer_size) |
| 379 | return; |
| 380 | if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size) |
| 381 | bytes = dpcm->pcm_buffer_size - dpcm->silent_size; |
| 382 | |
| 383 | for (;;) { |
| 384 | unsigned int size = bytes; |
| 385 | if (dst_off + size > dpcm->pcm_buffer_size) |
| 386 | size = dpcm->pcm_buffer_size - dst_off; |
| 387 | snd_pcm_format_set_silence(runtime->format, dst + dst_off, |
| 388 | bytes_to_frames(runtime, size) * |
| 389 | runtime->channels); |
| 390 | dpcm->silent_size += size; |
| 391 | bytes -= size; |
| 392 | if (!bytes) |
| 393 | break; |
| 394 | dst_off = 0; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | static void copy_play_buf(struct loopback_pcm *play, |
| 399 | struct loopback_pcm *capt, |
| 400 | unsigned int bytes) |
| 401 | { |
| 402 | struct snd_pcm_runtime *runtime = play->substream->runtime; |
Jaroslav Kysela | 20d9a26 | 2010-09-30 00:16:50 +0200 | [diff] [blame] | 403 | char *src = runtime->dma_area; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 404 | char *dst = capt->substream->runtime->dma_area; |
| 405 | unsigned int src_off = play->buf_pos; |
| 406 | unsigned int dst_off = capt->buf_pos; |
| 407 | unsigned int clear_bytes = 0; |
| 408 | |
| 409 | /* check if playback is draining, trim the capture copy size |
| 410 | * when our pointer is at the end of playback ring buffer */ |
| 411 | if (runtime->status->state == SNDRV_PCM_STATE_DRAINING && |
| 412 | snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) { |
| 413 | snd_pcm_uframes_t appl_ptr, appl_ptr1, diff; |
| 414 | appl_ptr = appl_ptr1 = runtime->control->appl_ptr; |
| 415 | appl_ptr1 -= appl_ptr1 % runtime->buffer_size; |
| 416 | appl_ptr1 += play->buf_pos / play->pcm_salign; |
| 417 | if (appl_ptr < appl_ptr1) |
| 418 | appl_ptr1 -= runtime->buffer_size; |
| 419 | diff = (appl_ptr - appl_ptr1) * play->pcm_salign; |
| 420 | if (diff < bytes) { |
| 421 | clear_bytes = bytes - diff; |
| 422 | bytes = diff; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | for (;;) { |
| 427 | unsigned int size = bytes; |
| 428 | if (src_off + size > play->pcm_buffer_size) |
| 429 | size = play->pcm_buffer_size - src_off; |
| 430 | if (dst_off + size > capt->pcm_buffer_size) |
| 431 | size = capt->pcm_buffer_size - dst_off; |
| 432 | memcpy(dst + dst_off, src + src_off, size); |
| 433 | capt->silent_size = 0; |
| 434 | bytes -= size; |
| 435 | if (!bytes) |
| 436 | break; |
| 437 | src_off = (src_off + size) % play->pcm_buffer_size; |
| 438 | dst_off = (dst_off + size) % capt->pcm_buffer_size; |
| 439 | } |
| 440 | |
Jaroslav Kysela | 20d9a26 | 2010-09-30 00:16:50 +0200 | [diff] [blame] | 441 | if (clear_bytes > 0) { |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 442 | clear_capture_buf(capt, clear_bytes); |
Jaroslav Kysela | 20d9a26 | 2010-09-30 00:16:50 +0200 | [diff] [blame] | 443 | capt->silent_size = 0; |
| 444 | } |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 447 | static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm, |
| 448 | unsigned int jiffies_delta) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 449 | { |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 450 | unsigned long last_pos; |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 451 | unsigned int delta; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 452 | |
| 453 | last_pos = byte_pos(dpcm, dpcm->irq_pos); |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 454 | dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps; |
| 455 | delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos; |
| 456 | if (delta >= dpcm->last_drift) |
| 457 | delta -= dpcm->last_drift; |
| 458 | dpcm->last_drift = 0; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 459 | if (dpcm->irq_pos >= dpcm->period_size_frac) { |
| 460 | dpcm->irq_pos %= dpcm->period_size_frac; |
| 461 | dpcm->period_update_pending = 1; |
| 462 | } |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 463 | return delta; |
| 464 | } |
| 465 | |
| 466 | static inline void bytepos_finish(struct loopback_pcm *dpcm, |
| 467 | unsigned int delta) |
| 468 | { |
| 469 | dpcm->buf_pos += delta; |
| 470 | dpcm->buf_pos %= dpcm->pcm_buffer_size; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 473 | /* call in cable->lock */ |
Jaroslav Kysela | dd04bb1 | 2010-10-20 08:27:02 +0200 | [diff] [blame] | 474 | static unsigned int loopback_pos_update(struct loopback_cable *cable) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 475 | { |
| 476 | struct loopback_pcm *dpcm_play = |
| 477 | cable->streams[SNDRV_PCM_STREAM_PLAYBACK]; |
| 478 | struct loopback_pcm *dpcm_capt = |
| 479 | cable->streams[SNDRV_PCM_STREAM_CAPTURE]; |
| 480 | unsigned long delta_play = 0, delta_capt = 0; |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 481 | unsigned int running, count1, count2; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 482 | |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 483 | running = cable->running ^ cable->pause; |
Jaroslav Kysela | dd04bb1 | 2010-10-20 08:27:02 +0200 | [diff] [blame] | 484 | if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) { |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 485 | delta_play = jiffies - dpcm_play->last_jiffies; |
| 486 | dpcm_play->last_jiffies += delta_play; |
| 487 | } |
| 488 | |
Jaroslav Kysela | dd04bb1 | 2010-10-20 08:27:02 +0200 | [diff] [blame] | 489 | if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) { |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 490 | delta_capt = jiffies - dpcm_capt->last_jiffies; |
| 491 | dpcm_capt->last_jiffies += delta_capt; |
| 492 | } |
| 493 | |
Takashi Iwai | 98d21df | 2011-03-18 07:31:53 +0100 | [diff] [blame] | 494 | if (delta_play == 0 && delta_capt == 0) |
| 495 | goto unlock; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 496 | |
| 497 | if (delta_play > delta_capt) { |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 498 | count1 = bytepos_delta(dpcm_play, delta_play - delta_capt); |
| 499 | bytepos_finish(dpcm_play, count1); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 500 | delta_play = delta_capt; |
| 501 | } else if (delta_play < delta_capt) { |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 502 | count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play); |
| 503 | clear_capture_buf(dpcm_capt, count1); |
| 504 | bytepos_finish(dpcm_capt, count1); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 505 | delta_capt = delta_play; |
| 506 | } |
| 507 | |
Takashi Iwai | 98d21df | 2011-03-18 07:31:53 +0100 | [diff] [blame] | 508 | if (delta_play == 0 && delta_capt == 0) |
| 509 | goto unlock; |
| 510 | |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 511 | /* note delta_capt == delta_play at this moment */ |
Jaroslav Kysela | b012513 | 2012-05-13 13:39:45 +0200 | [diff] [blame] | 512 | count1 = bytepos_delta(dpcm_play, delta_play); |
| 513 | count2 = bytepos_delta(dpcm_capt, delta_capt); |
| 514 | if (count1 < count2) { |
| 515 | dpcm_capt->last_drift = count2 - count1; |
| 516 | count1 = count2; |
| 517 | } else if (count1 > count2) { |
| 518 | dpcm_play->last_drift = count1 - count2; |
| 519 | } |
| 520 | copy_play_buf(dpcm_play, dpcm_capt, count1); |
| 521 | bytepos_finish(dpcm_play, count1); |
| 522 | bytepos_finish(dpcm_capt, count1); |
Takashi Iwai | 98d21df | 2011-03-18 07:31:53 +0100 | [diff] [blame] | 523 | unlock: |
Jaroslav Kysela | dd04bb1 | 2010-10-20 08:27:02 +0200 | [diff] [blame] | 524 | return running; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Kees Cook | bc47ba9 | 2017-10-24 08:34:29 -0700 | [diff] [blame] | 527 | static void loopback_timer_function(struct timer_list *t) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 528 | { |
Kees Cook | bc47ba9 | 2017-10-24 08:34:29 -0700 | [diff] [blame] | 529 | struct loopback_pcm *dpcm = from_timer(dpcm, t, timer); |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 530 | unsigned long flags; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 531 | |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 532 | spin_lock_irqsave(&dpcm->cable->lock, flags); |
| 533 | if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) { |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 534 | loopback_timer_start(dpcm); |
Jaroslav Kysela | dd04bb1 | 2010-10-20 08:27:02 +0200 | [diff] [blame] | 535 | if (dpcm->period_update_pending) { |
| 536 | dpcm->period_update_pending = 0; |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 537 | spin_unlock_irqrestore(&dpcm->cable->lock, flags); |
| 538 | /* need to unlock before calling below */ |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 539 | snd_pcm_period_elapsed(dpcm->substream); |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 540 | return; |
Jaroslav Kysela | dd04bb1 | 2010-10-20 08:27:02 +0200 | [diff] [blame] | 541 | } |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 542 | } |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 543 | spin_unlock_irqrestore(&dpcm->cable->lock, flags); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream) |
| 547 | { |
| 548 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 549 | struct loopback_pcm *dpcm = runtime->private_data; |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 550 | snd_pcm_uframes_t pos; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 551 | |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 552 | spin_lock(&dpcm->cable->lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 553 | loopback_pos_update(dpcm->cable); |
Takashi Iwai | 999fc9f | 2012-10-21 10:53:14 +0200 | [diff] [blame] | 554 | pos = dpcm->buf_pos; |
| 555 | spin_unlock(&dpcm->cable->lock); |
| 556 | return bytes_to_frames(runtime, pos); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Bhumika Goyal | b6c0b71 | 2017-08-17 14:45:51 +0530 | [diff] [blame] | 559 | static const struct snd_pcm_hardware loopback_pcm_hardware = |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 560 | { |
| 561 | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP | |
Takashi Iwai | edac894 | 2013-02-04 10:28:15 +0100 | [diff] [blame] | 562 | SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE | |
| 563 | SNDRV_PCM_INFO_RESUME), |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 564 | .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | |
| 565 | SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE | |
| 566 | SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE), |
| 567 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000, |
| 568 | .rate_min = 8000, |
| 569 | .rate_max = 192000, |
| 570 | .channels_min = 1, |
| 571 | .channels_max = 32, |
| 572 | .buffer_bytes_max = 2 * 1024 * 1024, |
| 573 | .period_bytes_min = 64, |
Jaroslav Kysela | 0db7102 | 2010-10-14 21:46:12 +0200 | [diff] [blame] | 574 | /* note check overflow in frac_pos() using pcm_rate_shift before |
| 575 | changing period_bytes_max value */ |
| 576 | .period_bytes_max = 1024 * 1024, |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 577 | .periods_min = 1, |
| 578 | .periods_max = 1024, |
| 579 | .fifo_size = 0, |
| 580 | }; |
| 581 | |
| 582 | static void loopback_runtime_free(struct snd_pcm_runtime *runtime) |
| 583 | { |
| 584 | struct loopback_pcm *dpcm = runtime->private_data; |
| 585 | kfree(dpcm); |
| 586 | } |
| 587 | |
| 588 | static int loopback_hw_params(struct snd_pcm_substream *substream, |
| 589 | struct snd_pcm_hw_params *params) |
| 590 | { |
Takashi Iwai | 6b69a0e | 2011-09-24 12:16:29 +0200 | [diff] [blame] | 591 | return snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 592 | params_buffer_bytes(params)); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static int loopback_hw_free(struct snd_pcm_substream *substream) |
| 596 | { |
| 597 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 598 | struct loopback_pcm *dpcm = runtime->private_data; |
| 599 | struct loopback_cable *cable = dpcm->cable; |
| 600 | |
| 601 | mutex_lock(&dpcm->loopback->cable_lock); |
| 602 | cable->valid &= ~(1 << substream->stream); |
| 603 | mutex_unlock(&dpcm->loopback->cable_lock); |
Takashi Iwai | 6b69a0e | 2011-09-24 12:16:29 +0200 | [diff] [blame] | 604 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static unsigned int get_cable_index(struct snd_pcm_substream *substream) |
| 608 | { |
| 609 | if (!substream->pcm->device) |
| 610 | return substream->stream; |
| 611 | else |
| 612 | return !substream->stream; |
| 613 | } |
| 614 | |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 615 | static int rule_format(struct snd_pcm_hw_params *params, |
| 616 | struct snd_pcm_hw_rule *rule) |
| 617 | { |
Takashi Iwai | 898dfe4 | 2018-01-04 17:38:54 +0100 | [diff] [blame] | 618 | struct loopback_pcm *dpcm = rule->private; |
| 619 | struct loopback_cable *cable = dpcm->cable; |
Takashi Iwai | b088b53 | 2018-01-05 16:15:33 +0100 | [diff] [blame] | 620 | struct snd_mask m; |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 621 | |
Takashi Iwai | b088b53 | 2018-01-05 16:15:33 +0100 | [diff] [blame] | 622 | snd_mask_none(&m); |
Takashi Iwai | 898dfe4 | 2018-01-04 17:38:54 +0100 | [diff] [blame] | 623 | mutex_lock(&dpcm->loopback->cable_lock); |
| 624 | m.bits[0] = (u_int32_t)cable->hw.formats; |
| 625 | m.bits[1] = (u_int32_t)(cable->hw.formats >> 32); |
| 626 | mutex_unlock(&dpcm->loopback->cable_lock); |
Takashi Iwai | b088b53 | 2018-01-05 16:15:33 +0100 | [diff] [blame] | 627 | return snd_mask_refine(hw_param_mask(params, rule->var), &m); |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static int rule_rate(struct snd_pcm_hw_params *params, |
| 631 | struct snd_pcm_hw_rule *rule) |
| 632 | { |
Takashi Iwai | 898dfe4 | 2018-01-04 17:38:54 +0100 | [diff] [blame] | 633 | struct loopback_pcm *dpcm = rule->private; |
| 634 | struct loopback_cable *cable = dpcm->cable; |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 635 | struct snd_interval t; |
| 636 | |
Takashi Iwai | 898dfe4 | 2018-01-04 17:38:54 +0100 | [diff] [blame] | 637 | mutex_lock(&dpcm->loopback->cable_lock); |
| 638 | t.min = cable->hw.rate_min; |
| 639 | t.max = cable->hw.rate_max; |
| 640 | mutex_unlock(&dpcm->loopback->cable_lock); |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 641 | t.openmin = t.openmax = 0; |
| 642 | t.integer = 0; |
| 643 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 644 | } |
| 645 | |
| 646 | static int rule_channels(struct snd_pcm_hw_params *params, |
| 647 | struct snd_pcm_hw_rule *rule) |
| 648 | { |
Takashi Iwai | 898dfe4 | 2018-01-04 17:38:54 +0100 | [diff] [blame] | 649 | struct loopback_pcm *dpcm = rule->private; |
| 650 | struct loopback_cable *cable = dpcm->cable; |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 651 | struct snd_interval t; |
| 652 | |
Takashi Iwai | 898dfe4 | 2018-01-04 17:38:54 +0100 | [diff] [blame] | 653 | mutex_lock(&dpcm->loopback->cable_lock); |
| 654 | t.min = cable->hw.channels_min; |
| 655 | t.max = cable->hw.channels_max; |
| 656 | mutex_unlock(&dpcm->loopback->cable_lock); |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 657 | t.openmin = t.openmax = 0; |
| 658 | t.integer = 0; |
| 659 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); |
| 660 | } |
| 661 | |
Takashi Iwai | 9685347 | 2018-01-05 16:09:47 +0100 | [diff] [blame] | 662 | static void free_cable(struct snd_pcm_substream *substream) |
| 663 | { |
| 664 | struct loopback *loopback = substream->private_data; |
| 665 | int dev = get_cable_index(substream); |
| 666 | struct loopback_cable *cable; |
| 667 | |
| 668 | cable = loopback->cables[substream->number][dev]; |
| 669 | if (!cable) |
| 670 | return; |
| 671 | if (cable->streams[!substream->stream]) { |
| 672 | /* other stream is still alive */ |
Takashi Iwai | 8e6b1a7 | 2018-03-22 10:40:27 +0100 | [diff] [blame] | 673 | spin_lock_irq(&cable->lock); |
Takashi Iwai | 9685347 | 2018-01-05 16:09:47 +0100 | [diff] [blame] | 674 | cable->streams[substream->stream] = NULL; |
Takashi Iwai | 8e6b1a7 | 2018-03-22 10:40:27 +0100 | [diff] [blame] | 675 | spin_unlock_irq(&cable->lock); |
Takashi Iwai | 9685347 | 2018-01-05 16:09:47 +0100 | [diff] [blame] | 676 | } else { |
| 677 | /* free the cable */ |
| 678 | loopback->cables[substream->number][dev] = NULL; |
| 679 | kfree(cable); |
| 680 | } |
| 681 | } |
| 682 | |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 683 | static int loopback_open(struct snd_pcm_substream *substream) |
| 684 | { |
| 685 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 686 | struct loopback *loopback = substream->private_data; |
| 687 | struct loopback_pcm *dpcm; |
Takashi Iwai | 9685347 | 2018-01-05 16:09:47 +0100 | [diff] [blame] | 688 | struct loopback_cable *cable = NULL; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 689 | int err = 0; |
| 690 | int dev = get_cable_index(substream); |
| 691 | |
| 692 | mutex_lock(&loopback->cable_lock); |
| 693 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
| 694 | if (!dpcm) { |
| 695 | err = -ENOMEM; |
| 696 | goto unlock; |
| 697 | } |
| 698 | dpcm->loopback = loopback; |
| 699 | dpcm->substream = substream; |
Kees Cook | bc47ba9 | 2017-10-24 08:34:29 -0700 | [diff] [blame] | 700 | timer_setup(&dpcm->timer, loopback_timer_function, 0); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 701 | |
| 702 | cable = loopback->cables[substream->number][dev]; |
| 703 | if (!cable) { |
| 704 | cable = kzalloc(sizeof(*cable), GFP_KERNEL); |
| 705 | if (!cable) { |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 706 | err = -ENOMEM; |
| 707 | goto unlock; |
| 708 | } |
| 709 | spin_lock_init(&cable->lock); |
| 710 | cable->hw = loopback_pcm_hardware; |
| 711 | loopback->cables[substream->number][dev] = cable; |
| 712 | } |
| 713 | dpcm->cable = cable; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 714 | |
| 715 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
| 716 | |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 717 | /* use dynamic rules based on actual runtime->hw values */ |
| 718 | /* note that the default rules created in the PCM midlevel code */ |
| 719 | /* are cached -> they do not reflect the actual state */ |
| 720 | err = snd_pcm_hw_rule_add(runtime, 0, |
| 721 | SNDRV_PCM_HW_PARAM_FORMAT, |
Takashi Iwai | 898dfe4 | 2018-01-04 17:38:54 +0100 | [diff] [blame] | 722 | rule_format, dpcm, |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 723 | SNDRV_PCM_HW_PARAM_FORMAT, -1); |
| 724 | if (err < 0) |
| 725 | goto unlock; |
| 726 | err = snd_pcm_hw_rule_add(runtime, 0, |
| 727 | SNDRV_PCM_HW_PARAM_RATE, |
Takashi Iwai | 898dfe4 | 2018-01-04 17:38:54 +0100 | [diff] [blame] | 728 | rule_rate, dpcm, |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 729 | SNDRV_PCM_HW_PARAM_RATE, -1); |
| 730 | if (err < 0) |
| 731 | goto unlock; |
| 732 | err = snd_pcm_hw_rule_add(runtime, 0, |
| 733 | SNDRV_PCM_HW_PARAM_CHANNELS, |
Takashi Iwai | 898dfe4 | 2018-01-04 17:38:54 +0100 | [diff] [blame] | 734 | rule_channels, dpcm, |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 735 | SNDRV_PCM_HW_PARAM_CHANNELS, -1); |
| 736 | if (err < 0) |
| 737 | goto unlock; |
| 738 | |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 739 | runtime->private_data = dpcm; |
| 740 | runtime->private_free = loopback_runtime_free; |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 741 | if (get_notify(dpcm)) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 742 | runtime->hw = loopback_pcm_hardware; |
Jaroslav Kysela | b1c73fc | 2010-10-11 10:45:00 +0200 | [diff] [blame] | 743 | else |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 744 | runtime->hw = cable->hw; |
Takashi Iwai | 8e6b1a7 | 2018-03-22 10:40:27 +0100 | [diff] [blame] | 745 | |
| 746 | spin_lock_irq(&cable->lock); |
| 747 | cable->streams[substream->stream] = dpcm; |
| 748 | spin_unlock_irq(&cable->lock); |
| 749 | |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 750 | unlock: |
Takashi Iwai | 9685347 | 2018-01-05 16:09:47 +0100 | [diff] [blame] | 751 | if (err < 0) { |
| 752 | free_cable(substream); |
| 753 | kfree(dpcm); |
| 754 | } |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 755 | mutex_unlock(&loopback->cable_lock); |
| 756 | return err; |
| 757 | } |
| 758 | |
| 759 | static int loopback_close(struct snd_pcm_substream *substream) |
| 760 | { |
| 761 | struct loopback *loopback = substream->private_data; |
| 762 | struct loopback_pcm *dpcm = substream->runtime->private_data; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 763 | |
Takashi Iwai | 67a01af | 2018-03-22 08:56:06 +0100 | [diff] [blame] | 764 | loopback_timer_stop_sync(dpcm); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 765 | mutex_lock(&loopback->cable_lock); |
Takashi Iwai | 9685347 | 2018-01-05 16:09:47 +0100 | [diff] [blame] | 766 | free_cable(substream); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 767 | mutex_unlock(&loopback->cable_lock); |
| 768 | return 0; |
| 769 | } |
| 770 | |
Takashi Iwai | 9f88058e | 2018-05-27 13:55:01 +0200 | [diff] [blame] | 771 | static const struct snd_pcm_ops loopback_pcm_ops = { |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 772 | .open = loopback_open, |
| 773 | .close = loopback_close, |
| 774 | .ioctl = snd_pcm_lib_ioctl, |
| 775 | .hw_params = loopback_hw_params, |
| 776 | .hw_free = loopback_hw_free, |
| 777 | .prepare = loopback_prepare, |
| 778 | .trigger = loopback_trigger, |
| 779 | .pointer = loopback_pointer, |
Takashi Iwai | 6b69a0e | 2011-09-24 12:16:29 +0200 | [diff] [blame] | 780 | .page = snd_pcm_lib_get_vmalloc_page, |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 781 | }; |
| 782 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 783 | static int loopback_pcm_new(struct loopback *loopback, |
| 784 | int device, int substreams) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 785 | { |
| 786 | struct snd_pcm *pcm; |
| 787 | int err; |
| 788 | |
| 789 | err = snd_pcm_new(loopback->card, "Loopback PCM", device, |
| 790 | substreams, substreams, &pcm); |
| 791 | if (err < 0) |
| 792 | return err; |
Takashi Iwai | 9f88058e | 2018-05-27 13:55:01 +0200 | [diff] [blame] | 793 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops); |
| 794 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 795 | |
| 796 | pcm->private_data = loopback; |
| 797 | pcm->info_flags = 0; |
| 798 | strcpy(pcm->name, "Loopback PCM"); |
| 799 | |
| 800 | loopback->pcm[device] = pcm; |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol, |
| 805 | struct snd_ctl_elem_info *uinfo) |
| 806 | { |
| 807 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 808 | uinfo->count = 1; |
| 809 | uinfo->value.integer.min = 80000; |
| 810 | uinfo->value.integer.max = 120000; |
| 811 | uinfo->value.integer.step = 1; |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol, |
| 816 | struct snd_ctl_elem_value *ucontrol) |
| 817 | { |
| 818 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
| 819 | |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 820 | mutex_lock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 821 | ucontrol->value.integer.value[0] = |
| 822 | loopback->setup[kcontrol->id.subdevice] |
| 823 | [kcontrol->id.device].rate_shift; |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 824 | mutex_unlock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol, |
| 829 | struct snd_ctl_elem_value *ucontrol) |
| 830 | { |
| 831 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
| 832 | unsigned int val; |
| 833 | int change = 0; |
| 834 | |
| 835 | val = ucontrol->value.integer.value[0]; |
| 836 | if (val < 80000) |
| 837 | val = 80000; |
| 838 | if (val > 120000) |
| 839 | val = 120000; |
| 840 | mutex_lock(&loopback->cable_lock); |
| 841 | if (val != loopback->setup[kcontrol->id.subdevice] |
| 842 | [kcontrol->id.device].rate_shift) { |
| 843 | loopback->setup[kcontrol->id.subdevice] |
| 844 | [kcontrol->id.device].rate_shift = val; |
| 845 | change = 1; |
| 846 | } |
| 847 | mutex_unlock(&loopback->cable_lock); |
| 848 | return change; |
| 849 | } |
| 850 | |
| 851 | static int loopback_notify_get(struct snd_kcontrol *kcontrol, |
| 852 | struct snd_ctl_elem_value *ucontrol) |
| 853 | { |
| 854 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
| 855 | |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 856 | mutex_lock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 857 | ucontrol->value.integer.value[0] = |
| 858 | loopback->setup[kcontrol->id.subdevice] |
| 859 | [kcontrol->id.device].notify; |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 860 | mutex_unlock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | static int loopback_notify_put(struct snd_kcontrol *kcontrol, |
| 865 | struct snd_ctl_elem_value *ucontrol) |
| 866 | { |
| 867 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
| 868 | unsigned int val; |
| 869 | int change = 0; |
| 870 | |
| 871 | val = ucontrol->value.integer.value[0] ? 1 : 0; |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 872 | mutex_lock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 873 | if (val != loopback->setup[kcontrol->id.subdevice] |
| 874 | [kcontrol->id.device].notify) { |
| 875 | loopback->setup[kcontrol->id.subdevice] |
| 876 | [kcontrol->id.device].notify = val; |
| 877 | change = 1; |
| 878 | } |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 879 | mutex_unlock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 880 | return change; |
| 881 | } |
| 882 | |
| 883 | static int loopback_active_get(struct snd_kcontrol *kcontrol, |
| 884 | struct snd_ctl_elem_value *ucontrol) |
| 885 | { |
| 886 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 887 | struct loopback_cable *cable; |
| 888 | |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 889 | unsigned int val = 0; |
| 890 | |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 891 | mutex_lock(&loopback->cable_lock); |
| 892 | cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1]; |
Robert Rosengren | 306a4f3 | 2018-03-26 07:24:49 +0200 | [diff] [blame] | 893 | if (cable != NULL) { |
| 894 | unsigned int running = cable->running ^ cable->pause; |
| 895 | |
| 896 | val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0; |
| 897 | } |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 898 | mutex_unlock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 899 | ucontrol->value.integer.value[0] = val; |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | static int loopback_format_info(struct snd_kcontrol *kcontrol, |
| 904 | struct snd_ctl_elem_info *uinfo) |
| 905 | { |
| 906 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 907 | uinfo->count = 1; |
| 908 | uinfo->value.integer.min = 0; |
| 909 | uinfo->value.integer.max = SNDRV_PCM_FORMAT_LAST; |
| 910 | uinfo->value.integer.step = 1; |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | static int loopback_format_get(struct snd_kcontrol *kcontrol, |
| 915 | struct snd_ctl_elem_value *ucontrol) |
| 916 | { |
| 917 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
| 918 | |
| 919 | ucontrol->value.integer.value[0] = |
| 920 | loopback->setup[kcontrol->id.subdevice] |
| 921 | [kcontrol->id.device].format; |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | static int loopback_rate_info(struct snd_kcontrol *kcontrol, |
| 926 | struct snd_ctl_elem_info *uinfo) |
| 927 | { |
| 928 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 929 | uinfo->count = 1; |
| 930 | uinfo->value.integer.min = 0; |
| 931 | uinfo->value.integer.max = 192000; |
| 932 | uinfo->value.integer.step = 1; |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | static int loopback_rate_get(struct snd_kcontrol *kcontrol, |
| 937 | struct snd_ctl_elem_value *ucontrol) |
| 938 | { |
| 939 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
| 940 | |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 941 | mutex_lock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 942 | ucontrol->value.integer.value[0] = |
| 943 | loopback->setup[kcontrol->id.subdevice] |
| 944 | [kcontrol->id.device].rate; |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 945 | mutex_unlock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | static int loopback_channels_info(struct snd_kcontrol *kcontrol, |
| 950 | struct snd_ctl_elem_info *uinfo) |
| 951 | { |
| 952 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 953 | uinfo->count = 1; |
| 954 | uinfo->value.integer.min = 1; |
| 955 | uinfo->value.integer.max = 1024; |
| 956 | uinfo->value.integer.step = 1; |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | static int loopback_channels_get(struct snd_kcontrol *kcontrol, |
| 961 | struct snd_ctl_elem_value *ucontrol) |
| 962 | { |
| 963 | struct loopback *loopback = snd_kcontrol_chip(kcontrol); |
| 964 | |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 965 | mutex_lock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 966 | ucontrol->value.integer.value[0] = |
| 967 | loopback->setup[kcontrol->id.subdevice] |
Jaroslav Kysela | 1446c5f | 2010-09-15 08:01:57 +0200 | [diff] [blame] | 968 | [kcontrol->id.device].channels; |
Takashi Iwai | 76b3421 | 2018-04-30 10:06:48 +0200 | [diff] [blame] | 969 | mutex_unlock(&loopback->cable_lock); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 970 | return 0; |
| 971 | } |
| 972 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 973 | static struct snd_kcontrol_new loopback_controls[] = { |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 974 | { |
| 975 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 976 | .name = "PCM Rate Shift 100000", |
| 977 | .info = loopback_rate_shift_info, |
| 978 | .get = loopback_rate_shift_get, |
| 979 | .put = loopback_rate_shift_put, |
| 980 | }, |
| 981 | { |
| 982 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 983 | .name = "PCM Notify", |
| 984 | .info = snd_ctl_boolean_mono_info, |
| 985 | .get = loopback_notify_get, |
| 986 | .put = loopback_notify_put, |
| 987 | }, |
| 988 | #define ACTIVE_IDX 2 |
| 989 | { |
| 990 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 991 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 992 | .name = "PCM Slave Active", |
| 993 | .info = snd_ctl_boolean_mono_info, |
| 994 | .get = loopback_active_get, |
| 995 | }, |
| 996 | #define FORMAT_IDX 3 |
| 997 | { |
| 998 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 999 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1000 | .name = "PCM Slave Format", |
| 1001 | .info = loopback_format_info, |
| 1002 | .get = loopback_format_get |
| 1003 | }, |
| 1004 | #define RATE_IDX 4 |
| 1005 | { |
| 1006 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1007 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1008 | .name = "PCM Slave Rate", |
| 1009 | .info = loopback_rate_info, |
| 1010 | .get = loopback_rate_get |
| 1011 | }, |
| 1012 | #define CHANNELS_IDX 5 |
| 1013 | { |
| 1014 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1015 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1016 | .name = "PCM Slave Channels", |
| 1017 | .info = loopback_channels_info, |
| 1018 | .get = loopback_channels_get |
| 1019 | } |
| 1020 | }; |
| 1021 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 1022 | static int loopback_mixer_new(struct loopback *loopback, int notify) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1023 | { |
| 1024 | struct snd_card *card = loopback->card; |
| 1025 | struct snd_pcm *pcm; |
| 1026 | struct snd_kcontrol *kctl; |
| 1027 | struct loopback_setup *setup; |
| 1028 | int err, dev, substr, substr_count, idx; |
| 1029 | |
| 1030 | strcpy(card->mixername, "Loopback Mixer"); |
| 1031 | for (dev = 0; dev < 2; dev++) { |
| 1032 | pcm = loopback->pcm[dev]; |
| 1033 | substr_count = |
| 1034 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count; |
| 1035 | for (substr = 0; substr < substr_count; substr++) { |
| 1036 | setup = &loopback->setup[substr][dev]; |
| 1037 | setup->notify = notify; |
| 1038 | setup->rate_shift = NO_PITCH; |
| 1039 | setup->format = SNDRV_PCM_FORMAT_S16_LE; |
| 1040 | setup->rate = 48000; |
| 1041 | setup->channels = 2; |
| 1042 | for (idx = 0; idx < ARRAY_SIZE(loopback_controls); |
| 1043 | idx++) { |
| 1044 | kctl = snd_ctl_new1(&loopback_controls[idx], |
| 1045 | loopback); |
| 1046 | if (!kctl) |
| 1047 | return -ENOMEM; |
| 1048 | kctl->id.device = dev; |
| 1049 | kctl->id.subdevice = substr; |
| 1050 | switch (idx) { |
| 1051 | case ACTIVE_IDX: |
| 1052 | setup->active_id = kctl->id; |
| 1053 | break; |
| 1054 | case FORMAT_IDX: |
| 1055 | setup->format_id = kctl->id; |
| 1056 | break; |
| 1057 | case RATE_IDX: |
| 1058 | setup->rate_id = kctl->id; |
| 1059 | break; |
| 1060 | case CHANNELS_IDX: |
| 1061 | setup->channels_id = kctl->id; |
| 1062 | break; |
| 1063 | default: |
| 1064 | break; |
| 1065 | } |
| 1066 | err = snd_ctl_add(card, kctl); |
| 1067 | if (err < 0) |
| 1068 | return err; |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
Jaroslav Kysela | e74670b | 2010-10-18 09:43:10 +0200 | [diff] [blame] | 1075 | static void print_dpcm_info(struct snd_info_buffer *buffer, |
| 1076 | struct loopback_pcm *dpcm, |
| 1077 | const char *id) |
| 1078 | { |
| 1079 | snd_iprintf(buffer, " %s\n", id); |
| 1080 | if (dpcm == NULL) { |
| 1081 | snd_iprintf(buffer, " inactive\n"); |
| 1082 | return; |
| 1083 | } |
| 1084 | snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size); |
| 1085 | snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos); |
| 1086 | snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size); |
| 1087 | snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size); |
| 1088 | snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps); |
| 1089 | snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign); |
| 1090 | snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift); |
| 1091 | snd_iprintf(buffer, " update_pending:\t%u\n", |
| 1092 | dpcm->period_update_pending); |
| 1093 | snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos); |
| 1094 | snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac); |
| 1095 | snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n", |
| 1096 | dpcm->last_jiffies, jiffies); |
| 1097 | snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires); |
| 1098 | } |
| 1099 | |
| 1100 | static void print_substream_info(struct snd_info_buffer *buffer, |
| 1101 | struct loopback *loopback, |
| 1102 | int sub, |
| 1103 | int num) |
| 1104 | { |
| 1105 | struct loopback_cable *cable = loopback->cables[sub][num]; |
| 1106 | |
| 1107 | snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub); |
| 1108 | if (cable == NULL) { |
| 1109 | snd_iprintf(buffer, " inactive\n"); |
| 1110 | return; |
| 1111 | } |
| 1112 | snd_iprintf(buffer, " valid: %u\n", cable->valid); |
| 1113 | snd_iprintf(buffer, " running: %u\n", cable->running); |
Jaroslav Kysela | 5de9e45 | 2010-10-20 09:33:03 +0200 | [diff] [blame] | 1114 | snd_iprintf(buffer, " pause: %u\n", cable->pause); |
Jaroslav Kysela | e74670b | 2010-10-18 09:43:10 +0200 | [diff] [blame] | 1115 | print_dpcm_info(buffer, cable->streams[0], "Playback"); |
| 1116 | print_dpcm_info(buffer, cable->streams[1], "Capture"); |
| 1117 | } |
| 1118 | |
| 1119 | static void print_cable_info(struct snd_info_entry *entry, |
| 1120 | struct snd_info_buffer *buffer) |
| 1121 | { |
| 1122 | struct loopback *loopback = entry->private_data; |
| 1123 | int sub, num; |
| 1124 | |
| 1125 | mutex_lock(&loopback->cable_lock); |
| 1126 | num = entry->name[strlen(entry->name)-1]; |
| 1127 | num = num == '0' ? 0 : 1; |
| 1128 | for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++) |
| 1129 | print_substream_info(buffer, loopback, sub, num); |
| 1130 | mutex_unlock(&loopback->cable_lock); |
| 1131 | } |
| 1132 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 1133 | static int loopback_proc_new(struct loopback *loopback, int cidx) |
Jaroslav Kysela | e74670b | 2010-10-18 09:43:10 +0200 | [diff] [blame] | 1134 | { |
| 1135 | char name[32]; |
Jaroslav Kysela | e74670b | 2010-10-18 09:43:10 +0200 | [diff] [blame] | 1136 | |
| 1137 | snprintf(name, sizeof(name), "cable#%d", cidx); |
Takashi Iwai | 815d808 | 2019-02-04 15:58:33 +0100 | [diff] [blame^] | 1138 | return snd_card_ro_proc_new(loopback->card, name, loopback, |
| 1139 | print_cable_info); |
Jaroslav Kysela | e74670b | 2010-10-18 09:43:10 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 1142 | static int loopback_probe(struct platform_device *devptr) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1143 | { |
| 1144 | struct snd_card *card; |
| 1145 | struct loopback *loopback; |
| 1146 | int dev = devptr->id; |
| 1147 | int err; |
| 1148 | |
Takashi Iwai | 5872f3f | 2014-01-29 12:59:08 +0100 | [diff] [blame] | 1149 | err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE, |
| 1150 | sizeof(struct loopback), &card); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1151 | if (err < 0) |
| 1152 | return err; |
| 1153 | loopback = card->private_data; |
| 1154 | |
| 1155 | if (pcm_substreams[dev] < 1) |
| 1156 | pcm_substreams[dev] = 1; |
| 1157 | if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) |
| 1158 | pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; |
| 1159 | |
| 1160 | loopback->card = card; |
| 1161 | mutex_init(&loopback->cable_lock); |
| 1162 | |
| 1163 | err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]); |
| 1164 | if (err < 0) |
| 1165 | goto __nodev; |
| 1166 | err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]); |
| 1167 | if (err < 0) |
| 1168 | goto __nodev; |
| 1169 | err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0); |
| 1170 | if (err < 0) |
| 1171 | goto __nodev; |
Jaroslav Kysela | e74670b | 2010-10-18 09:43:10 +0200 | [diff] [blame] | 1172 | loopback_proc_new(loopback, 0); |
| 1173 | loopback_proc_new(loopback, 1); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1174 | strcpy(card->driver, "Loopback"); |
| 1175 | strcpy(card->shortname, "Loopback"); |
| 1176 | sprintf(card->longname, "Loopback %i", dev + 1); |
| 1177 | err = snd_card_register(card); |
| 1178 | if (!err) { |
| 1179 | platform_set_drvdata(devptr, card); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | __nodev: |
| 1183 | snd_card_free(card); |
| 1184 | return err; |
| 1185 | } |
| 1186 | |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 1187 | static int loopback_remove(struct platform_device *devptr) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1188 | { |
| 1189 | snd_card_free(platform_get_drvdata(devptr)); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
Takashi Iwai | d34e4e0 | 2012-08-09 15:47:15 +0200 | [diff] [blame] | 1193 | #ifdef CONFIG_PM_SLEEP |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 1194 | static int loopback_suspend(struct device *pdev) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1195 | { |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 1196 | struct snd_card *card = dev_get_drvdata(pdev); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1197 | |
| 1198 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 1202 | static int loopback_resume(struct device *pdev) |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1203 | { |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 1204 | struct snd_card *card = dev_get_drvdata(pdev); |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1205 | |
| 1206 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); |
| 1207 | return 0; |
| 1208 | } |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 1209 | |
| 1210 | static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume); |
| 1211 | #define LOOPBACK_PM_OPS &loopback_pm |
| 1212 | #else |
| 1213 | #define LOOPBACK_PM_OPS NULL |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1214 | #endif |
| 1215 | |
| 1216 | #define SND_LOOPBACK_DRIVER "snd_aloop" |
| 1217 | |
| 1218 | static struct platform_driver loopback_driver = { |
| 1219 | .probe = loopback_probe, |
Bill Pemberton | fbbb01a | 2012-12-06 12:35:27 -0500 | [diff] [blame] | 1220 | .remove = loopback_remove, |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1221 | .driver = { |
Takashi Iwai | 8bf01d8 | 2012-07-02 10:50:24 +0200 | [diff] [blame] | 1222 | .name = SND_LOOPBACK_DRIVER, |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 1223 | .pm = LOOPBACK_PM_OPS, |
Jaroslav Kysela | 597603d | 2010-08-09 14:21:11 +0200 | [diff] [blame] | 1224 | }, |
| 1225 | }; |
| 1226 | |
| 1227 | static void loopback_unregister_all(void) |
| 1228 | { |
| 1229 | int i; |
| 1230 | |
| 1231 | for (i = 0; i < ARRAY_SIZE(devices); ++i) |
| 1232 | platform_device_unregister(devices[i]); |
| 1233 | platform_driver_unregister(&loopback_driver); |
| 1234 | } |
| 1235 | |
| 1236 | static int __init alsa_card_loopback_init(void) |
| 1237 | { |
| 1238 | int i, err, cards; |
| 1239 | |
| 1240 | err = platform_driver_register(&loopback_driver); |
| 1241 | if (err < 0) |
| 1242 | return err; |
| 1243 | |
| 1244 | |
| 1245 | cards = 0; |
| 1246 | for (i = 0; i < SNDRV_CARDS; i++) { |
| 1247 | struct platform_device *device; |
| 1248 | if (!enable[i]) |
| 1249 | continue; |
| 1250 | device = platform_device_register_simple(SND_LOOPBACK_DRIVER, |
| 1251 | i, NULL, 0); |
| 1252 | if (IS_ERR(device)) |
| 1253 | continue; |
| 1254 | if (!platform_get_drvdata(device)) { |
| 1255 | platform_device_unregister(device); |
| 1256 | continue; |
| 1257 | } |
| 1258 | devices[i] = device; |
| 1259 | cards++; |
| 1260 | } |
| 1261 | if (!cards) { |
| 1262 | #ifdef MODULE |
| 1263 | printk(KERN_ERR "aloop: No loopback enabled\n"); |
| 1264 | #endif |
| 1265 | loopback_unregister_all(); |
| 1266 | return -ENODEV; |
| 1267 | } |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | static void __exit alsa_card_loopback_exit(void) |
| 1272 | { |
| 1273 | loopback_unregister_all(); |
| 1274 | } |
| 1275 | |
| 1276 | module_init(alsa_card_loopback_init) |
| 1277 | module_exit(alsa_card_loopback_exit) |