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