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>
37 #include <linux/module.h>
38 #include <linux/platform_device.h>
39 #include <sound/core.h>
40 #include <sound/control.h>
41 #include <sound/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/info.h>
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 */
55 static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
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;
81 unsigned int pause;
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;
121 unsigned int last_drift;
122 unsigned long last_jiffies;
123 struct timer_list timer;
124 };
125
126 static struct platform_device *devices[SNDRV_CARDS];
127
byte_pos(struct loopback_pcm * dpcm,unsigned int x)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
frac_pos(struct loopback_pcm * dpcm,unsigned int x)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
get_setup(struct loopback_pcm * dpcm)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
get_notify(struct loopback_pcm * dpcm)159 static inline unsigned int get_notify(struct loopback_pcm *dpcm)
160 {
161 return get_setup(dpcm)->notify;
162 }
163
get_rate_shift(struct loopback_pcm * dpcm)164 static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
165 {
166 return get_setup(dpcm)->rate_shift;
167 }
168
169 /* call in cable->lock */
loopback_timer_start(struct loopback_pcm * dpcm)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 }
179 if (dpcm->period_size_frac <= dpcm->irq_pos) {
180 dpcm->irq_pos %= dpcm->period_size_frac;
181 dpcm->period_update_pending = 1;
182 }
183 tick = dpcm->period_size_frac - dpcm->irq_pos;
184 tick = (tick + dpcm->pcm_bps - 1) / dpcm->pcm_bps;
185 dpcm->timer.expires = jiffies + tick;
186 add_timer(&dpcm->timer);
187 }
188
189 /* call in cable->lock */
loopback_timer_stop(struct loopback_pcm * dpcm)190 static inline void loopback_timer_stop(struct loopback_pcm *dpcm)
191 {
192 del_timer(&dpcm->timer);
193 dpcm->timer.expires = 0;
194 }
195
196 #define CABLE_VALID_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
197 #define CABLE_VALID_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
198 #define CABLE_VALID_BOTH (CABLE_VALID_PLAYBACK|CABLE_VALID_CAPTURE)
199
loopback_check_format(struct loopback_cable * cable,int stream)200 static int loopback_check_format(struct loopback_cable *cable, int stream)
201 {
202 struct snd_pcm_runtime *runtime, *cruntime;
203 struct loopback_setup *setup;
204 struct snd_card *card;
205 int check;
206
207 if (cable->valid != CABLE_VALID_BOTH) {
208 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
209 goto __notify;
210 return 0;
211 }
212 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
213 substream->runtime;
214 cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
215 substream->runtime;
216 check = runtime->format != cruntime->format ||
217 runtime->rate != cruntime->rate ||
218 runtime->channels != cruntime->channels;
219 if (!check)
220 return 0;
221 if (stream == SNDRV_PCM_STREAM_CAPTURE) {
222 return -EIO;
223 } else {
224 snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
225 substream, SNDRV_PCM_STATE_DRAINING);
226 __notify:
227 runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
228 substream->runtime;
229 setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
230 card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
231 if (setup->format != runtime->format) {
232 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
233 &setup->format_id);
234 setup->format = runtime->format;
235 }
236 if (setup->rate != runtime->rate) {
237 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
238 &setup->rate_id);
239 setup->rate = runtime->rate;
240 }
241 if (setup->channels != runtime->channels) {
242 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
243 &setup->channels_id);
244 setup->channels = runtime->channels;
245 }
246 }
247 return 0;
248 }
249
loopback_active_notify(struct loopback_pcm * dpcm)250 static void loopback_active_notify(struct loopback_pcm *dpcm)
251 {
252 snd_ctl_notify(dpcm->loopback->card,
253 SNDRV_CTL_EVENT_MASK_VALUE,
254 &get_setup(dpcm)->active_id);
255 }
256
loopback_trigger(struct snd_pcm_substream * substream,int cmd)257 static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
258 {
259 struct snd_pcm_runtime *runtime = substream->runtime;
260 struct loopback_pcm *dpcm = runtime->private_data;
261 struct loopback_cable *cable = dpcm->cable;
262 int err, stream = 1 << substream->stream;
263
264 switch (cmd) {
265 case SNDRV_PCM_TRIGGER_START:
266 err = loopback_check_format(cable, substream->stream);
267 if (err < 0)
268 return err;
269 dpcm->last_jiffies = jiffies;
270 dpcm->pcm_rate_shift = 0;
271 dpcm->last_drift = 0;
272 spin_lock(&cable->lock);
273 cable->running |= stream;
274 cable->pause &= ~stream;
275 loopback_timer_start(dpcm);
276 spin_unlock(&cable->lock);
277 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
278 loopback_active_notify(dpcm);
279 break;
280 case SNDRV_PCM_TRIGGER_STOP:
281 spin_lock(&cable->lock);
282 cable->running &= ~stream;
283 cable->pause &= ~stream;
284 loopback_timer_stop(dpcm);
285 spin_unlock(&cable->lock);
286 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
287 loopback_active_notify(dpcm);
288 break;
289 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
290 case SNDRV_PCM_TRIGGER_SUSPEND:
291 spin_lock(&cable->lock);
292 cable->pause |= stream;
293 loopback_timer_stop(dpcm);
294 spin_unlock(&cable->lock);
295 break;
296 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
297 case SNDRV_PCM_TRIGGER_RESUME:
298 spin_lock(&cable->lock);
299 dpcm->last_jiffies = jiffies;
300 cable->pause &= ~stream;
301 loopback_timer_start(dpcm);
302 spin_unlock(&cable->lock);
303 break;
304 default:
305 return -EINVAL;
306 }
307 return 0;
308 }
309
params_change(struct snd_pcm_substream * substream)310 static void params_change(struct snd_pcm_substream *substream)
311 {
312 struct snd_pcm_runtime *runtime = substream->runtime;
313 struct loopback_pcm *dpcm = runtime->private_data;
314 struct loopback_cable *cable = dpcm->cable;
315
316 cable->hw.formats = pcm_format_to_bits(runtime->format);
317 cable->hw.rate_min = runtime->rate;
318 cable->hw.rate_max = runtime->rate;
319 cable->hw.channels_min = runtime->channels;
320 cable->hw.channels_max = runtime->channels;
321 }
322
loopback_prepare(struct snd_pcm_substream * substream)323 static int loopback_prepare(struct snd_pcm_substream *substream)
324 {
325 struct snd_pcm_runtime *runtime = substream->runtime;
326 struct loopback_pcm *dpcm = runtime->private_data;
327 struct loopback_cable *cable = dpcm->cable;
328 int bps, salign;
329
330 salign = (snd_pcm_format_width(runtime->format) *
331 runtime->channels) / 8;
332 bps = salign * runtime->rate;
333 if (bps <= 0 || salign <= 0)
334 return -EINVAL;
335
336 dpcm->buf_pos = 0;
337 dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
338 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
339 /* clear capture buffer */
340 dpcm->silent_size = dpcm->pcm_buffer_size;
341 snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
342 runtime->buffer_size * runtime->channels);
343 }
344
345 dpcm->irq_pos = 0;
346 dpcm->period_update_pending = 0;
347 dpcm->pcm_bps = bps;
348 dpcm->pcm_salign = salign;
349 dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
350
351 mutex_lock(&dpcm->loopback->cable_lock);
352 if (!(cable->valid & ~(1 << substream->stream)) ||
353 (get_setup(dpcm)->notify &&
354 substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
355 params_change(substream);
356 cable->valid |= 1 << substream->stream;
357 mutex_unlock(&dpcm->loopback->cable_lock);
358
359 return 0;
360 }
361
clear_capture_buf(struct loopback_pcm * dpcm,unsigned int bytes)362 static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
363 {
364 struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
365 char *dst = runtime->dma_area;
366 unsigned int dst_off = dpcm->buf_pos;
367
368 if (dpcm->silent_size >= dpcm->pcm_buffer_size)
369 return;
370 if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
371 bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
372
373 for (;;) {
374 unsigned int size = bytes;
375 if (dst_off + size > dpcm->pcm_buffer_size)
376 size = dpcm->pcm_buffer_size - dst_off;
377 snd_pcm_format_set_silence(runtime->format, dst + dst_off,
378 bytes_to_frames(runtime, size) *
379 runtime->channels);
380 dpcm->silent_size += size;
381 bytes -= size;
382 if (!bytes)
383 break;
384 dst_off = 0;
385 }
386 }
387
copy_play_buf(struct loopback_pcm * play,struct loopback_pcm * capt,unsigned int bytes)388 static void copy_play_buf(struct loopback_pcm *play,
389 struct loopback_pcm *capt,
390 unsigned int bytes)
391 {
392 struct snd_pcm_runtime *runtime = play->substream->runtime;
393 char *src = runtime->dma_area;
394 char *dst = capt->substream->runtime->dma_area;
395 unsigned int src_off = play->buf_pos;
396 unsigned int dst_off = capt->buf_pos;
397 unsigned int clear_bytes = 0;
398
399 /* check if playback is draining, trim the capture copy size
400 * when our pointer is at the end of playback ring buffer */
401 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
402 snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) {
403 snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
404 appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
405 appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
406 appl_ptr1 += play->buf_pos / play->pcm_salign;
407 if (appl_ptr < appl_ptr1)
408 appl_ptr1 -= runtime->buffer_size;
409 diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
410 if (diff < bytes) {
411 clear_bytes = bytes - diff;
412 bytes = diff;
413 }
414 }
415
416 for (;;) {
417 unsigned int size = bytes;
418 if (src_off + size > play->pcm_buffer_size)
419 size = play->pcm_buffer_size - src_off;
420 if (dst_off + size > capt->pcm_buffer_size)
421 size = capt->pcm_buffer_size - dst_off;
422 memcpy(dst + dst_off, src + src_off, size);
423 capt->silent_size = 0;
424 bytes -= size;
425 if (!bytes)
426 break;
427 src_off = (src_off + size) % play->pcm_buffer_size;
428 dst_off = (dst_off + size) % capt->pcm_buffer_size;
429 }
430
431 if (clear_bytes > 0) {
432 clear_capture_buf(capt, clear_bytes);
433 capt->silent_size = 0;
434 }
435 }
436
bytepos_delta(struct loopback_pcm * dpcm,unsigned int jiffies_delta)437 static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
438 unsigned int jiffies_delta)
439 {
440 unsigned long last_pos;
441 unsigned int delta;
442
443 last_pos = byte_pos(dpcm, dpcm->irq_pos);
444 dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
445 delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
446 if (delta >= dpcm->last_drift)
447 delta -= dpcm->last_drift;
448 dpcm->last_drift = 0;
449 if (dpcm->irq_pos >= dpcm->period_size_frac) {
450 dpcm->irq_pos %= dpcm->period_size_frac;
451 dpcm->period_update_pending = 1;
452 }
453 return delta;
454 }
455
bytepos_finish(struct loopback_pcm * dpcm,unsigned int delta)456 static inline void bytepos_finish(struct loopback_pcm *dpcm,
457 unsigned int delta)
458 {
459 dpcm->buf_pos += delta;
460 dpcm->buf_pos %= dpcm->pcm_buffer_size;
461 }
462
463 /* call in cable->lock */
loopback_pos_update(struct loopback_cable * cable)464 static unsigned int loopback_pos_update(struct loopback_cable *cable)
465 {
466 struct loopback_pcm *dpcm_play =
467 cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
468 struct loopback_pcm *dpcm_capt =
469 cable->streams[SNDRV_PCM_STREAM_CAPTURE];
470 unsigned long delta_play = 0, delta_capt = 0;
471 unsigned int running, count1, count2;
472
473 running = cable->running ^ cable->pause;
474 if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
475 delta_play = jiffies - dpcm_play->last_jiffies;
476 dpcm_play->last_jiffies += delta_play;
477 }
478
479 if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
480 delta_capt = jiffies - dpcm_capt->last_jiffies;
481 dpcm_capt->last_jiffies += delta_capt;
482 }
483
484 if (delta_play == 0 && delta_capt == 0)
485 goto unlock;
486
487 if (delta_play > delta_capt) {
488 count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
489 bytepos_finish(dpcm_play, count1);
490 delta_play = delta_capt;
491 } else if (delta_play < delta_capt) {
492 count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
493 clear_capture_buf(dpcm_capt, count1);
494 bytepos_finish(dpcm_capt, count1);
495 delta_capt = delta_play;
496 }
497
498 if (delta_play == 0 && delta_capt == 0)
499 goto unlock;
500
501 /* note delta_capt == delta_play at this moment */
502 count1 = bytepos_delta(dpcm_play, delta_play);
503 count2 = bytepos_delta(dpcm_capt, delta_capt);
504 if (count1 < count2) {
505 dpcm_capt->last_drift = count2 - count1;
506 count1 = count2;
507 } else if (count1 > count2) {
508 dpcm_play->last_drift = count1 - count2;
509 }
510 copy_play_buf(dpcm_play, dpcm_capt, count1);
511 bytepos_finish(dpcm_play, count1);
512 bytepos_finish(dpcm_capt, count1);
513 unlock:
514 return running;
515 }
516
loopback_timer_function(unsigned long data)517 static void loopback_timer_function(unsigned long data)
518 {
519 struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
520 unsigned long flags;
521
522 spin_lock_irqsave(&dpcm->cable->lock, flags);
523 if (loopback_pos_update(dpcm->cable) & (1 << dpcm->substream->stream)) {
524 loopback_timer_start(dpcm);
525 if (dpcm->period_update_pending) {
526 dpcm->period_update_pending = 0;
527 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
528 /* need to unlock before calling below */
529 snd_pcm_period_elapsed(dpcm->substream);
530 return;
531 }
532 }
533 spin_unlock_irqrestore(&dpcm->cable->lock, flags);
534 }
535
loopback_pointer(struct snd_pcm_substream * substream)536 static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
537 {
538 struct snd_pcm_runtime *runtime = substream->runtime;
539 struct loopback_pcm *dpcm = runtime->private_data;
540 snd_pcm_uframes_t pos;
541
542 spin_lock(&dpcm->cable->lock);
543 loopback_pos_update(dpcm->cable);
544 pos = dpcm->buf_pos;
545 spin_unlock(&dpcm->cable->lock);
546 return bytes_to_frames(runtime, pos);
547 }
548
549 static struct snd_pcm_hardware loopback_pcm_hardware =
550 {
551 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
552 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
553 SNDRV_PCM_INFO_RESUME),
554 .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
555 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
556 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
557 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
558 .rate_min = 8000,
559 .rate_max = 192000,
560 .channels_min = 1,
561 .channels_max = 32,
562 .buffer_bytes_max = 2 * 1024 * 1024,
563 .period_bytes_min = 64,
564 /* note check overflow in frac_pos() using pcm_rate_shift before
565 changing period_bytes_max value */
566 .period_bytes_max = 1024 * 1024,
567 .periods_min = 1,
568 .periods_max = 1024,
569 .fifo_size = 0,
570 };
571
loopback_runtime_free(struct snd_pcm_runtime * runtime)572 static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
573 {
574 struct loopback_pcm *dpcm = runtime->private_data;
575 kfree(dpcm);
576 }
577
loopback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)578 static int loopback_hw_params(struct snd_pcm_substream *substream,
579 struct snd_pcm_hw_params *params)
580 {
581 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
582 params_buffer_bytes(params));
583 }
584
loopback_hw_free(struct snd_pcm_substream * substream)585 static int loopback_hw_free(struct snd_pcm_substream *substream)
586 {
587 struct snd_pcm_runtime *runtime = substream->runtime;
588 struct loopback_pcm *dpcm = runtime->private_data;
589 struct loopback_cable *cable = dpcm->cable;
590
591 mutex_lock(&dpcm->loopback->cable_lock);
592 cable->valid &= ~(1 << substream->stream);
593 mutex_unlock(&dpcm->loopback->cable_lock);
594 return snd_pcm_lib_free_vmalloc_buffer(substream);
595 }
596
get_cable_index(struct snd_pcm_substream * substream)597 static unsigned int get_cable_index(struct snd_pcm_substream *substream)
598 {
599 if (!substream->pcm->device)
600 return substream->stream;
601 else
602 return !substream->stream;
603 }
604
rule_format(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)605 static int rule_format(struct snd_pcm_hw_params *params,
606 struct snd_pcm_hw_rule *rule)
607 {
608 struct loopback_pcm *dpcm = rule->private;
609 struct loopback_cable *cable = dpcm->cable;
610 struct snd_mask m;
611
612 snd_mask_none(&m);
613 mutex_lock(&dpcm->loopback->cable_lock);
614 m.bits[0] = (u_int32_t)cable->hw.formats;
615 m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
616 mutex_unlock(&dpcm->loopback->cable_lock);
617 return snd_mask_refine(hw_param_mask(params, rule->var), &m);
618 }
619
rule_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)620 static int rule_rate(struct snd_pcm_hw_params *params,
621 struct snd_pcm_hw_rule *rule)
622 {
623 struct loopback_pcm *dpcm = rule->private;
624 struct loopback_cable *cable = dpcm->cable;
625 struct snd_interval t;
626
627 mutex_lock(&dpcm->loopback->cable_lock);
628 t.min = cable->hw.rate_min;
629 t.max = cable->hw.rate_max;
630 mutex_unlock(&dpcm->loopback->cable_lock);
631 t.openmin = t.openmax = 0;
632 t.integer = 0;
633 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
634 }
635
rule_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)636 static int rule_channels(struct snd_pcm_hw_params *params,
637 struct snd_pcm_hw_rule *rule)
638 {
639 struct loopback_pcm *dpcm = rule->private;
640 struct loopback_cable *cable = dpcm->cable;
641 struct snd_interval t;
642
643 mutex_lock(&dpcm->loopback->cable_lock);
644 t.min = cable->hw.channels_min;
645 t.max = cable->hw.channels_max;
646 mutex_unlock(&dpcm->loopback->cable_lock);
647 t.openmin = t.openmax = 0;
648 t.integer = 0;
649 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
650 }
651
free_cable(struct snd_pcm_substream * substream)652 static void free_cable(struct snd_pcm_substream *substream)
653 {
654 struct loopback *loopback = substream->private_data;
655 int dev = get_cable_index(substream);
656 struct loopback_cable *cable;
657
658 cable = loopback->cables[substream->number][dev];
659 if (!cable)
660 return;
661 if (cable->streams[!substream->stream]) {
662 /* other stream is still alive */
663 cable->streams[substream->stream] = NULL;
664 } else {
665 /* free the cable */
666 loopback->cables[substream->number][dev] = NULL;
667 kfree(cable);
668 }
669 }
670
loopback_open(struct snd_pcm_substream * substream)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;
676 struct loopback_cable *cable = NULL;
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;
688 setup_timer(&dpcm->timer, loopback_timer_function,
689 (unsigned long)dpcm);
690
691 cable = loopback->cables[substream->number][dev];
692 if (!cable) {
693 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
694 if (!cable) {
695 err = -ENOMEM;
696 goto unlock;
697 }
698 spin_lock_init(&cable->lock);
699 cable->hw = loopback_pcm_hardware;
700 loopback->cables[substream->number][dev] = cable;
701 }
702 dpcm->cable = cable;
703 cable->streams[substream->stream] = dpcm;
704
705 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
706
707 /* use dynamic rules based on actual runtime->hw values */
708 /* note that the default rules created in the PCM midlevel code */
709 /* are cached -> they do not reflect the actual state */
710 err = snd_pcm_hw_rule_add(runtime, 0,
711 SNDRV_PCM_HW_PARAM_FORMAT,
712 rule_format, dpcm,
713 SNDRV_PCM_HW_PARAM_FORMAT, -1);
714 if (err < 0)
715 goto unlock;
716 err = snd_pcm_hw_rule_add(runtime, 0,
717 SNDRV_PCM_HW_PARAM_RATE,
718 rule_rate, dpcm,
719 SNDRV_PCM_HW_PARAM_RATE, -1);
720 if (err < 0)
721 goto unlock;
722 err = snd_pcm_hw_rule_add(runtime, 0,
723 SNDRV_PCM_HW_PARAM_CHANNELS,
724 rule_channels, dpcm,
725 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
726 if (err < 0)
727 goto unlock;
728
729 runtime->private_data = dpcm;
730 runtime->private_free = loopback_runtime_free;
731 if (get_notify(dpcm))
732 runtime->hw = loopback_pcm_hardware;
733 else
734 runtime->hw = cable->hw;
735 unlock:
736 if (err < 0) {
737 free_cable(substream);
738 kfree(dpcm);
739 }
740 mutex_unlock(&loopback->cable_lock);
741 return err;
742 }
743
loopback_close(struct snd_pcm_substream * substream)744 static int loopback_close(struct snd_pcm_substream *substream)
745 {
746 struct loopback *loopback = substream->private_data;
747 struct loopback_pcm *dpcm = substream->runtime->private_data;
748
749 loopback_timer_stop(dpcm);
750 mutex_lock(&loopback->cable_lock);
751 free_cable(substream);
752 mutex_unlock(&loopback->cable_lock);
753 return 0;
754 }
755
756 static struct snd_pcm_ops loopback_playback_ops = {
757 .open = loopback_open,
758 .close = loopback_close,
759 .ioctl = snd_pcm_lib_ioctl,
760 .hw_params = loopback_hw_params,
761 .hw_free = loopback_hw_free,
762 .prepare = loopback_prepare,
763 .trigger = loopback_trigger,
764 .pointer = loopback_pointer,
765 .page = snd_pcm_lib_get_vmalloc_page,
766 .mmap = snd_pcm_lib_mmap_vmalloc,
767 };
768
769 static struct snd_pcm_ops loopback_capture_ops = {
770 .open = loopback_open,
771 .close = loopback_close,
772 .ioctl = snd_pcm_lib_ioctl,
773 .hw_params = loopback_hw_params,
774 .hw_free = loopback_hw_free,
775 .prepare = loopback_prepare,
776 .trigger = loopback_trigger,
777 .pointer = loopback_pointer,
778 .page = snd_pcm_lib_get_vmalloc_page,
779 .mmap = snd_pcm_lib_mmap_vmalloc,
780 };
781
loopback_pcm_new(struct loopback * loopback,int device,int substreams)782 static int loopback_pcm_new(struct loopback *loopback,
783 int device, int substreams)
784 {
785 struct snd_pcm *pcm;
786 int err;
787
788 err = snd_pcm_new(loopback->card, "Loopback PCM", device,
789 substreams, substreams, &pcm);
790 if (err < 0)
791 return err;
792 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_playback_ops);
793 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_capture_ops);
794
795 pcm->private_data = loopback;
796 pcm->info_flags = 0;
797 strcpy(pcm->name, "Loopback PCM");
798
799 loopback->pcm[device] = pcm;
800 return 0;
801 }
802
loopback_rate_shift_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)803 static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,
804 struct snd_ctl_elem_info *uinfo)
805 {
806 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
807 uinfo->count = 1;
808 uinfo->value.integer.min = 80000;
809 uinfo->value.integer.max = 120000;
810 uinfo->value.integer.step = 1;
811 return 0;
812 }
813
loopback_rate_shift_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)814 static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
815 struct snd_ctl_elem_value *ucontrol)
816 {
817 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
818
819 ucontrol->value.integer.value[0] =
820 loopback->setup[kcontrol->id.subdevice]
821 [kcontrol->id.device].rate_shift;
822 return 0;
823 }
824
loopback_rate_shift_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)825 static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
826 struct snd_ctl_elem_value *ucontrol)
827 {
828 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
829 unsigned int val;
830 int change = 0;
831
832 val = ucontrol->value.integer.value[0];
833 if (val < 80000)
834 val = 80000;
835 if (val > 120000)
836 val = 120000;
837 mutex_lock(&loopback->cable_lock);
838 if (val != loopback->setup[kcontrol->id.subdevice]
839 [kcontrol->id.device].rate_shift) {
840 loopback->setup[kcontrol->id.subdevice]
841 [kcontrol->id.device].rate_shift = val;
842 change = 1;
843 }
844 mutex_unlock(&loopback->cable_lock);
845 return change;
846 }
847
loopback_notify_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)848 static int loopback_notify_get(struct snd_kcontrol *kcontrol,
849 struct snd_ctl_elem_value *ucontrol)
850 {
851 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
852
853 ucontrol->value.integer.value[0] =
854 loopback->setup[kcontrol->id.subdevice]
855 [kcontrol->id.device].notify;
856 return 0;
857 }
858
loopback_notify_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)859 static int loopback_notify_put(struct snd_kcontrol *kcontrol,
860 struct snd_ctl_elem_value *ucontrol)
861 {
862 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
863 unsigned int val;
864 int change = 0;
865
866 val = ucontrol->value.integer.value[0] ? 1 : 0;
867 if (val != loopback->setup[kcontrol->id.subdevice]
868 [kcontrol->id.device].notify) {
869 loopback->setup[kcontrol->id.subdevice]
870 [kcontrol->id.device].notify = val;
871 change = 1;
872 }
873 return change;
874 }
875
loopback_active_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)876 static int loopback_active_get(struct snd_kcontrol *kcontrol,
877 struct snd_ctl_elem_value *ucontrol)
878 {
879 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
880 struct loopback_cable *cable = loopback->cables
881 [kcontrol->id.subdevice][kcontrol->id.device ^ 1];
882 unsigned int val = 0;
883
884 if (cable != NULL)
885 val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
886 1 : 0;
887 ucontrol->value.integer.value[0] = val;
888 return 0;
889 }
890
loopback_format_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)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
loopback_format_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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
loopback_rate_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)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
loopback_rate_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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
929 ucontrol->value.integer.value[0] =
930 loopback->setup[kcontrol->id.subdevice]
931 [kcontrol->id.device].rate;
932 return 0;
933 }
934
loopback_channels_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)935 static int loopback_channels_info(struct snd_kcontrol *kcontrol,
936 struct snd_ctl_elem_info *uinfo)
937 {
938 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
939 uinfo->count = 1;
940 uinfo->value.integer.min = 1;
941 uinfo->value.integer.max = 1024;
942 uinfo->value.integer.step = 1;
943 return 0;
944 }
945
loopback_channels_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)946 static int loopback_channels_get(struct snd_kcontrol *kcontrol,
947 struct snd_ctl_elem_value *ucontrol)
948 {
949 struct loopback *loopback = snd_kcontrol_chip(kcontrol);
950
951 ucontrol->value.integer.value[0] =
952 loopback->setup[kcontrol->id.subdevice]
953 [kcontrol->id.device].channels;
954 return 0;
955 }
956
957 static struct snd_kcontrol_new loopback_controls[] = {
958 {
959 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
960 .name = "PCM Rate Shift 100000",
961 .info = loopback_rate_shift_info,
962 .get = loopback_rate_shift_get,
963 .put = loopback_rate_shift_put,
964 },
965 {
966 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
967 .name = "PCM Notify",
968 .info = snd_ctl_boolean_mono_info,
969 .get = loopback_notify_get,
970 .put = loopback_notify_put,
971 },
972 #define ACTIVE_IDX 2
973 {
974 .access = SNDRV_CTL_ELEM_ACCESS_READ,
975 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
976 .name = "PCM Slave Active",
977 .info = snd_ctl_boolean_mono_info,
978 .get = loopback_active_get,
979 },
980 #define FORMAT_IDX 3
981 {
982 .access = SNDRV_CTL_ELEM_ACCESS_READ,
983 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
984 .name = "PCM Slave Format",
985 .info = loopback_format_info,
986 .get = loopback_format_get
987 },
988 #define RATE_IDX 4
989 {
990 .access = SNDRV_CTL_ELEM_ACCESS_READ,
991 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
992 .name = "PCM Slave Rate",
993 .info = loopback_rate_info,
994 .get = loopback_rate_get
995 },
996 #define CHANNELS_IDX 5
997 {
998 .access = SNDRV_CTL_ELEM_ACCESS_READ,
999 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1000 .name = "PCM Slave Channels",
1001 .info = loopback_channels_info,
1002 .get = loopback_channels_get
1003 }
1004 };
1005
loopback_mixer_new(struct loopback * loopback,int notify)1006 static int loopback_mixer_new(struct loopback *loopback, int notify)
1007 {
1008 struct snd_card *card = loopback->card;
1009 struct snd_pcm *pcm;
1010 struct snd_kcontrol *kctl;
1011 struct loopback_setup *setup;
1012 int err, dev, substr, substr_count, idx;
1013
1014 strcpy(card->mixername, "Loopback Mixer");
1015 for (dev = 0; dev < 2; dev++) {
1016 pcm = loopback->pcm[dev];
1017 substr_count =
1018 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1019 for (substr = 0; substr < substr_count; substr++) {
1020 setup = &loopback->setup[substr][dev];
1021 setup->notify = notify;
1022 setup->rate_shift = NO_PITCH;
1023 setup->format = SNDRV_PCM_FORMAT_S16_LE;
1024 setup->rate = 48000;
1025 setup->channels = 2;
1026 for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1027 idx++) {
1028 kctl = snd_ctl_new1(&loopback_controls[idx],
1029 loopback);
1030 if (!kctl)
1031 return -ENOMEM;
1032 kctl->id.device = dev;
1033 kctl->id.subdevice = substr;
1034 switch (idx) {
1035 case ACTIVE_IDX:
1036 setup->active_id = kctl->id;
1037 break;
1038 case FORMAT_IDX:
1039 setup->format_id = kctl->id;
1040 break;
1041 case RATE_IDX:
1042 setup->rate_id = kctl->id;
1043 break;
1044 case CHANNELS_IDX:
1045 setup->channels_id = kctl->id;
1046 break;
1047 default:
1048 break;
1049 }
1050 err = snd_ctl_add(card, kctl);
1051 if (err < 0)
1052 return err;
1053 }
1054 }
1055 }
1056 return 0;
1057 }
1058
1059 #ifdef CONFIG_PROC_FS
1060
print_dpcm_info(struct snd_info_buffer * buffer,struct loopback_pcm * dpcm,const char * id)1061 static void print_dpcm_info(struct snd_info_buffer *buffer,
1062 struct loopback_pcm *dpcm,
1063 const char *id)
1064 {
1065 snd_iprintf(buffer, " %s\n", id);
1066 if (dpcm == NULL) {
1067 snd_iprintf(buffer, " inactive\n");
1068 return;
1069 }
1070 snd_iprintf(buffer, " buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1071 snd_iprintf(buffer, " buffer_pos:\t\t%u\n", dpcm->buf_pos);
1072 snd_iprintf(buffer, " silent_size:\t%u\n", dpcm->silent_size);
1073 snd_iprintf(buffer, " period_size:\t%u\n", dpcm->pcm_period_size);
1074 snd_iprintf(buffer, " bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1075 snd_iprintf(buffer, " sample_align:\t%u\n", dpcm->pcm_salign);
1076 snd_iprintf(buffer, " rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1077 snd_iprintf(buffer, " update_pending:\t%u\n",
1078 dpcm->period_update_pending);
1079 snd_iprintf(buffer, " irq_pos:\t\t%u\n", dpcm->irq_pos);
1080 snd_iprintf(buffer, " period_frac:\t%u\n", dpcm->period_size_frac);
1081 snd_iprintf(buffer, " last_jiffies:\t%lu (%lu)\n",
1082 dpcm->last_jiffies, jiffies);
1083 snd_iprintf(buffer, " timer_expires:\t%lu\n", dpcm->timer.expires);
1084 }
1085
print_substream_info(struct snd_info_buffer * buffer,struct loopback * loopback,int sub,int num)1086 static void print_substream_info(struct snd_info_buffer *buffer,
1087 struct loopback *loopback,
1088 int sub,
1089 int num)
1090 {
1091 struct loopback_cable *cable = loopback->cables[sub][num];
1092
1093 snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1094 if (cable == NULL) {
1095 snd_iprintf(buffer, " inactive\n");
1096 return;
1097 }
1098 snd_iprintf(buffer, " valid: %u\n", cable->valid);
1099 snd_iprintf(buffer, " running: %u\n", cable->running);
1100 snd_iprintf(buffer, " pause: %u\n", cable->pause);
1101 print_dpcm_info(buffer, cable->streams[0], "Playback");
1102 print_dpcm_info(buffer, cable->streams[1], "Capture");
1103 }
1104
print_cable_info(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1105 static void print_cable_info(struct snd_info_entry *entry,
1106 struct snd_info_buffer *buffer)
1107 {
1108 struct loopback *loopback = entry->private_data;
1109 int sub, num;
1110
1111 mutex_lock(&loopback->cable_lock);
1112 num = entry->name[strlen(entry->name)-1];
1113 num = num == '0' ? 0 : 1;
1114 for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1115 print_substream_info(buffer, loopback, sub, num);
1116 mutex_unlock(&loopback->cable_lock);
1117 }
1118
loopback_proc_new(struct loopback * loopback,int cidx)1119 static int loopback_proc_new(struct loopback *loopback, int cidx)
1120 {
1121 char name[32];
1122 struct snd_info_entry *entry;
1123 int err;
1124
1125 snprintf(name, sizeof(name), "cable#%d", cidx);
1126 err = snd_card_proc_new(loopback->card, name, &entry);
1127 if (err < 0)
1128 return err;
1129
1130 snd_info_set_text_ops(entry, loopback, print_cable_info);
1131 return 0;
1132 }
1133
1134 #else /* !CONFIG_PROC_FS */
1135
1136 #define loopback_proc_new(loopback, cidx) do { } while (0)
1137
1138 #endif
1139
loopback_probe(struct platform_device * devptr)1140 static int loopback_probe(struct platform_device *devptr)
1141 {
1142 struct snd_card *card;
1143 struct loopback *loopback;
1144 int dev = devptr->id;
1145 int err;
1146
1147 err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1148 sizeof(struct loopback), &card);
1149 if (err < 0)
1150 return err;
1151 loopback = card->private_data;
1152
1153 if (pcm_substreams[dev] < 1)
1154 pcm_substreams[dev] = 1;
1155 if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1156 pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1157
1158 loopback->card = card;
1159 mutex_init(&loopback->cable_lock);
1160
1161 err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1162 if (err < 0)
1163 goto __nodev;
1164 err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1165 if (err < 0)
1166 goto __nodev;
1167 err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1168 if (err < 0)
1169 goto __nodev;
1170 loopback_proc_new(loopback, 0);
1171 loopback_proc_new(loopback, 1);
1172 strcpy(card->driver, "Loopback");
1173 strcpy(card->shortname, "Loopback");
1174 sprintf(card->longname, "Loopback %i", dev + 1);
1175 err = snd_card_register(card);
1176 if (!err) {
1177 platform_set_drvdata(devptr, card);
1178 return 0;
1179 }
1180 __nodev:
1181 snd_card_free(card);
1182 return err;
1183 }
1184
loopback_remove(struct platform_device * devptr)1185 static int loopback_remove(struct platform_device *devptr)
1186 {
1187 snd_card_free(platform_get_drvdata(devptr));
1188 return 0;
1189 }
1190
1191 #ifdef CONFIG_PM_SLEEP
loopback_suspend(struct device * pdev)1192 static int loopback_suspend(struct device *pdev)
1193 {
1194 struct snd_card *card = dev_get_drvdata(pdev);
1195 struct loopback *loopback = card->private_data;
1196
1197 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1198
1199 snd_pcm_suspend_all(loopback->pcm[0]);
1200 snd_pcm_suspend_all(loopback->pcm[1]);
1201 return 0;
1202 }
1203
loopback_resume(struct device * pdev)1204 static int loopback_resume(struct device *pdev)
1205 {
1206 struct snd_card *card = dev_get_drvdata(pdev);
1207
1208 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1209 return 0;
1210 }
1211
1212 static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1213 #define LOOPBACK_PM_OPS &loopback_pm
1214 #else
1215 #define LOOPBACK_PM_OPS NULL
1216 #endif
1217
1218 #define SND_LOOPBACK_DRIVER "snd_aloop"
1219
1220 static struct platform_driver loopback_driver = {
1221 .probe = loopback_probe,
1222 .remove = loopback_remove,
1223 .driver = {
1224 .name = SND_LOOPBACK_DRIVER,
1225 .owner = THIS_MODULE,
1226 .pm = LOOPBACK_PM_OPS,
1227 },
1228 };
1229
loopback_unregister_all(void)1230 static void loopback_unregister_all(void)
1231 {
1232 int i;
1233
1234 for (i = 0; i < ARRAY_SIZE(devices); ++i)
1235 platform_device_unregister(devices[i]);
1236 platform_driver_unregister(&loopback_driver);
1237 }
1238
alsa_card_loopback_init(void)1239 static int __init alsa_card_loopback_init(void)
1240 {
1241 int i, err, cards;
1242
1243 err = platform_driver_register(&loopback_driver);
1244 if (err < 0)
1245 return err;
1246
1247
1248 cards = 0;
1249 for (i = 0; i < SNDRV_CARDS; i++) {
1250 struct platform_device *device;
1251 if (!enable[i])
1252 continue;
1253 device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1254 i, NULL, 0);
1255 if (IS_ERR(device))
1256 continue;
1257 if (!platform_get_drvdata(device)) {
1258 platform_device_unregister(device);
1259 continue;
1260 }
1261 devices[i] = device;
1262 cards++;
1263 }
1264 if (!cards) {
1265 #ifdef MODULE
1266 printk(KERN_ERR "aloop: No loopback enabled\n");
1267 #endif
1268 loopback_unregister_all();
1269 return -ENODEV;
1270 }
1271 return 0;
1272 }
1273
alsa_card_loopback_exit(void)1274 static void __exit alsa_card_loopback_exit(void)
1275 {
1276 loopback_unregister_all();
1277 }
1278
1279 module_init(alsa_card_loopback_init)
1280 module_exit(alsa_card_loopback_exit)
1281