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