Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Loopback soundcard
0004  *
0005  *  Original code:
0006  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
0007  *
0008  *  More accurate positioning and full-duplex support:
0009  *  Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
0010  *
0011  *  Major (almost complete) rewrite:
0012  *  Copyright (c) by Takashi Iwai <tiwai@suse.de>
0013  *
0014  *  A next major update in 2010 (separate timers for playback and capture):
0015  *  Copyright (c) Jaroslav Kysela <perex@perex.cz>
0016  */
0017 
0018 #include <linux/init.h>
0019 #include <linux/jiffies.h>
0020 #include <linux/slab.h>
0021 #include <linux/time.h>
0022 #include <linux/wait.h>
0023 #include <linux/module.h>
0024 #include <linux/platform_device.h>
0025 #include <sound/core.h>
0026 #include <sound/control.h>
0027 #include <sound/pcm.h>
0028 #include <sound/pcm_params.h>
0029 #include <sound/info.h>
0030 #include <sound/initval.h>
0031 #include <sound/timer.h>
0032 
0033 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0034 MODULE_DESCRIPTION("A loopback soundcard");
0035 MODULE_LICENSE("GPL");
0036 
0037 #define MAX_PCM_SUBSTREAMS  8
0038 
0039 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;  /* Index 0-MAX */
0040 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;   /* ID for this card */
0041 static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
0042 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
0043 static int pcm_notify[SNDRV_CARDS];
0044 static char *timer_source[SNDRV_CARDS];
0045 
0046 module_param_array(index, int, NULL, 0444);
0047 MODULE_PARM_DESC(index, "Index value for loopback soundcard.");
0048 module_param_array(id, charp, NULL, 0444);
0049 MODULE_PARM_DESC(id, "ID string for loopback soundcard.");
0050 module_param_array(enable, bool, NULL, 0444);
0051 MODULE_PARM_DESC(enable, "Enable this loopback soundcard.");
0052 module_param_array(pcm_substreams, int, NULL, 0444);
0053 MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-8) for loopback driver.");
0054 module_param_array(pcm_notify, int, NULL, 0444);
0055 MODULE_PARM_DESC(pcm_notify, "Break capture when PCM format/rate/channels changes.");
0056 module_param_array(timer_source, charp, NULL, 0444);
0057 MODULE_PARM_DESC(timer_source, "Sound card name or number and device/subdevice number of timer to be used. Empty string for jiffies timer [default].");
0058 
0059 #define NO_PITCH 100000
0060 
0061 #define CABLE_VALID_PLAYBACK    BIT(SNDRV_PCM_STREAM_PLAYBACK)
0062 #define CABLE_VALID_CAPTURE BIT(SNDRV_PCM_STREAM_CAPTURE)
0063 #define CABLE_VALID_BOTH    (CABLE_VALID_PLAYBACK | CABLE_VALID_CAPTURE)
0064 
0065 struct loopback_cable;
0066 struct loopback_pcm;
0067 
0068 struct loopback_ops {
0069     /* optional
0070      * call in loopback->cable_lock
0071      */
0072     int (*open)(struct loopback_pcm *dpcm);
0073     /* required
0074      * call in cable->lock
0075      */
0076     int (*start)(struct loopback_pcm *dpcm);
0077     /* required
0078      * call in cable->lock
0079      */
0080     int (*stop)(struct loopback_pcm *dpcm);
0081     /* optional */
0082     int (*stop_sync)(struct loopback_pcm *dpcm);
0083     /* optional */
0084     int (*close_substream)(struct loopback_pcm *dpcm);
0085     /* optional
0086      * call in loopback->cable_lock
0087      */
0088     int (*close_cable)(struct loopback_pcm *dpcm);
0089     /* optional
0090      * call in cable->lock
0091      */
0092     unsigned int (*pos_update)(struct loopback_cable *cable);
0093     /* optional */
0094     void (*dpcm_info)(struct loopback_pcm *dpcm,
0095               struct snd_info_buffer *buffer);
0096 };
0097 
0098 struct loopback_cable {
0099     spinlock_t lock;
0100     struct loopback_pcm *streams[2];
0101     struct snd_pcm_hardware hw;
0102     /* flags */
0103     unsigned int valid;
0104     unsigned int running;
0105     unsigned int pause;
0106     /* timer specific */
0107     const struct loopback_ops *ops;
0108     /* If sound timer is used */
0109     struct {
0110         int stream;
0111         struct snd_timer_id id;
0112         struct work_struct event_work;
0113         struct snd_timer_instance *instance;
0114     } snd_timer;
0115 };
0116 
0117 struct loopback_setup {
0118     unsigned int notify: 1;
0119     unsigned int rate_shift;
0120     snd_pcm_format_t format;
0121     unsigned int rate;
0122     unsigned int channels;
0123     struct snd_ctl_elem_id active_id;
0124     struct snd_ctl_elem_id format_id;
0125     struct snd_ctl_elem_id rate_id;
0126     struct snd_ctl_elem_id channels_id;
0127 };
0128 
0129 struct loopback {
0130     struct snd_card *card;
0131     struct mutex cable_lock;
0132     struct loopback_cable *cables[MAX_PCM_SUBSTREAMS][2];
0133     struct snd_pcm *pcm[2];
0134     struct loopback_setup setup[MAX_PCM_SUBSTREAMS][2];
0135     const char *timer_source;
0136 };
0137 
0138 struct loopback_pcm {
0139     struct loopback *loopback;
0140     struct snd_pcm_substream *substream;
0141     struct loopback_cable *cable;
0142     unsigned int pcm_buffer_size;
0143     unsigned int buf_pos;   /* position in buffer */
0144     unsigned int silent_size;
0145     /* PCM parameters */
0146     unsigned int pcm_period_size;
0147     unsigned int pcm_bps;       /* bytes per second */
0148     unsigned int pcm_salign;    /* bytes per sample * channels */
0149     unsigned int pcm_rate_shift;    /* rate shift value */
0150     /* flags */
0151     unsigned int period_update_pending :1;
0152     /* timer stuff */
0153     unsigned int irq_pos;       /* fractional IRQ position in jiffies
0154                      * ticks
0155                      */
0156     unsigned int period_size_frac;  /* period size in jiffies ticks */
0157     unsigned int last_drift;
0158     unsigned long last_jiffies;
0159     /* If jiffies timer is used */
0160     struct timer_list timer;
0161 };
0162 
0163 static struct platform_device *devices[SNDRV_CARDS];
0164 
0165 static inline unsigned int byte_pos(struct loopback_pcm *dpcm, unsigned int x)
0166 {
0167     if (dpcm->pcm_rate_shift == NO_PITCH) {
0168         x /= HZ;
0169     } else {
0170         x = div_u64(NO_PITCH * (unsigned long long)x,
0171                 HZ * (unsigned long long)dpcm->pcm_rate_shift);
0172     }
0173     return x - (x % dpcm->pcm_salign);
0174 }
0175 
0176 static inline unsigned int frac_pos(struct loopback_pcm *dpcm, unsigned int x)
0177 {
0178     if (dpcm->pcm_rate_shift == NO_PITCH) { /* no pitch */
0179         return x * HZ;
0180     } else {
0181         x = div_u64(dpcm->pcm_rate_shift * (unsigned long long)x * HZ,
0182                 NO_PITCH);
0183     }
0184     return x;
0185 }
0186 
0187 static inline struct loopback_setup *get_setup(struct loopback_pcm *dpcm)
0188 {
0189     int device = dpcm->substream->pstr->pcm->device;
0190     
0191     if (dpcm->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0192         device ^= 1;
0193     return &dpcm->loopback->setup[dpcm->substream->number][device];
0194 }
0195 
0196 static inline unsigned int get_notify(struct loopback_pcm *dpcm)
0197 {
0198     return get_setup(dpcm)->notify;
0199 }
0200 
0201 static inline unsigned int get_rate_shift(struct loopback_pcm *dpcm)
0202 {
0203     return get_setup(dpcm)->rate_shift;
0204 }
0205 
0206 /* call in cable->lock */
0207 static int loopback_jiffies_timer_start(struct loopback_pcm *dpcm)
0208 {
0209     unsigned long tick;
0210     unsigned int rate_shift = get_rate_shift(dpcm);
0211 
0212     if (rate_shift != dpcm->pcm_rate_shift) {
0213         dpcm->pcm_rate_shift = rate_shift;
0214         dpcm->period_size_frac = frac_pos(dpcm, dpcm->pcm_period_size);
0215     }
0216     if (dpcm->period_size_frac <= dpcm->irq_pos) {
0217         dpcm->irq_pos %= dpcm->period_size_frac;
0218         dpcm->period_update_pending = 1;
0219     }
0220     tick = dpcm->period_size_frac - dpcm->irq_pos;
0221     tick = DIV_ROUND_UP(tick, dpcm->pcm_bps);
0222     mod_timer(&dpcm->timer, jiffies + tick);
0223 
0224     return 0;
0225 }
0226 
0227 /* call in cable->lock */
0228 static int loopback_snd_timer_start(struct loopback_pcm *dpcm)
0229 {
0230     struct loopback_cable *cable = dpcm->cable;
0231     int err;
0232 
0233     /* Loopback device has to use same period as timer card. Therefore
0234      * wake up for each snd_pcm_period_elapsed() call of timer card.
0235      */
0236     err = snd_timer_start(cable->snd_timer.instance, 1);
0237     if (err < 0) {
0238         /* do not report error if trying to start but already
0239          * running. For example called by opposite substream
0240          * of the same cable
0241          */
0242         if (err == -EBUSY)
0243             return 0;
0244 
0245         pcm_err(dpcm->substream->pcm,
0246             "snd_timer_start(%d,%d,%d) failed with %d",
0247             cable->snd_timer.id.card,
0248             cable->snd_timer.id.device,
0249             cable->snd_timer.id.subdevice,
0250             err);
0251     }
0252 
0253     return err;
0254 }
0255 
0256 /* call in cable->lock */
0257 static inline int loopback_jiffies_timer_stop(struct loopback_pcm *dpcm)
0258 {
0259     del_timer(&dpcm->timer);
0260     dpcm->timer.expires = 0;
0261 
0262     return 0;
0263 }
0264 
0265 /* call in cable->lock */
0266 static int loopback_snd_timer_stop(struct loopback_pcm *dpcm)
0267 {
0268     struct loopback_cable *cable = dpcm->cable;
0269     int err;
0270 
0271     /* only stop if both devices (playback and capture) are not running */
0272     if (cable->running ^ cable->pause)
0273         return 0;
0274 
0275     err = snd_timer_stop(cable->snd_timer.instance);
0276     if (err < 0) {
0277         pcm_err(dpcm->substream->pcm,
0278             "snd_timer_stop(%d,%d,%d) failed with %d",
0279             cable->snd_timer.id.card,
0280             cable->snd_timer.id.device,
0281             cable->snd_timer.id.subdevice,
0282             err);
0283     }
0284 
0285     return err;
0286 }
0287 
0288 static inline int loopback_jiffies_timer_stop_sync(struct loopback_pcm *dpcm)
0289 {
0290     del_timer_sync(&dpcm->timer);
0291 
0292     return 0;
0293 }
0294 
0295 /* call in loopback->cable_lock */
0296 static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm)
0297 {
0298     struct loopback_cable *cable = dpcm->cable;
0299 
0300     /* snd_timer was not opened */
0301     if (!cable->snd_timer.instance)
0302         return 0;
0303 
0304     /* will only be called from free_cable() when other stream was
0305      * already closed. Other stream cannot be reopened as long as
0306      * loopback->cable_lock is locked. Therefore no need to lock
0307      * cable->lock;
0308      */
0309     snd_timer_close(cable->snd_timer.instance);
0310 
0311     /* wait till drain work has finished if requested */
0312     cancel_work_sync(&cable->snd_timer.event_work);
0313 
0314     snd_timer_instance_free(cable->snd_timer.instance);
0315     memset(&cable->snd_timer, 0, sizeof(cable->snd_timer));
0316 
0317     return 0;
0318 }
0319 
0320 static int loopback_check_format(struct loopback_cable *cable, int stream)
0321 {
0322     struct snd_pcm_runtime *runtime, *cruntime;
0323     struct loopback_setup *setup;
0324     struct snd_card *card;
0325     int check;
0326 
0327     if (cable->valid != CABLE_VALID_BOTH) {
0328         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
0329             goto __notify;
0330         return 0;
0331     }
0332     runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
0333                             substream->runtime;
0334     cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
0335                             substream->runtime;
0336     check = runtime->format != cruntime->format ||
0337         runtime->rate != cruntime->rate ||
0338         runtime->channels != cruntime->channels;
0339     if (!check)
0340         return 0;
0341     if (stream == SNDRV_PCM_STREAM_CAPTURE) {
0342         return -EIO;
0343     } else {
0344         snd_pcm_stop(cable->streams[SNDRV_PCM_STREAM_CAPTURE]->
0345                     substream, SNDRV_PCM_STATE_DRAINING);
0346           __notify:
0347         runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->
0348                             substream->runtime;
0349         setup = get_setup(cable->streams[SNDRV_PCM_STREAM_PLAYBACK]);
0350         card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK]->loopback->card;
0351         if (setup->format != runtime->format) {
0352             snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
0353                             &setup->format_id);
0354             setup->format = runtime->format;
0355         }
0356         if (setup->rate != runtime->rate) {
0357             snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
0358                             &setup->rate_id);
0359             setup->rate = runtime->rate;
0360         }
0361         if (setup->channels != runtime->channels) {
0362             snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
0363                             &setup->channels_id);
0364             setup->channels = runtime->channels;
0365         }
0366     }
0367     return 0;
0368 }
0369 
0370 static void loopback_active_notify(struct loopback_pcm *dpcm)
0371 {
0372     snd_ctl_notify(dpcm->loopback->card,
0373                SNDRV_CTL_EVENT_MASK_VALUE,
0374                &get_setup(dpcm)->active_id);
0375 }
0376 
0377 static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
0378 {
0379     struct snd_pcm_runtime *runtime = substream->runtime;
0380     struct loopback_pcm *dpcm = runtime->private_data;
0381     struct loopback_cable *cable = dpcm->cable;
0382     int err = 0, stream = 1 << substream->stream;
0383 
0384     switch (cmd) {
0385     case SNDRV_PCM_TRIGGER_START:
0386         err = loopback_check_format(cable, substream->stream);
0387         if (err < 0)
0388             return err;
0389         dpcm->last_jiffies = jiffies;
0390         dpcm->pcm_rate_shift = 0;
0391         dpcm->last_drift = 0;
0392         spin_lock(&cable->lock);    
0393         cable->running |= stream;
0394         cable->pause &= ~stream;
0395         err = cable->ops->start(dpcm);
0396         spin_unlock(&cable->lock);
0397         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0398             loopback_active_notify(dpcm);
0399         break;
0400     case SNDRV_PCM_TRIGGER_STOP:
0401         spin_lock(&cable->lock);    
0402         cable->running &= ~stream;
0403         cable->pause &= ~stream;
0404         err = cable->ops->stop(dpcm);
0405         spin_unlock(&cable->lock);
0406         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0407             loopback_active_notify(dpcm);
0408         break;
0409     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0410     case SNDRV_PCM_TRIGGER_SUSPEND:
0411         spin_lock(&cable->lock);    
0412         cable->pause |= stream;
0413         err = cable->ops->stop(dpcm);
0414         spin_unlock(&cable->lock);
0415         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0416             loopback_active_notify(dpcm);
0417         break;
0418     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0419     case SNDRV_PCM_TRIGGER_RESUME:
0420         spin_lock(&cable->lock);
0421         dpcm->last_jiffies = jiffies;
0422         cable->pause &= ~stream;
0423         err = cable->ops->start(dpcm);
0424         spin_unlock(&cable->lock);
0425         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0426             loopback_active_notify(dpcm);
0427         break;
0428     default:
0429         return -EINVAL;
0430     }
0431     return err;
0432 }
0433 
0434 static void params_change(struct snd_pcm_substream *substream)
0435 {
0436     struct snd_pcm_runtime *runtime = substream->runtime;
0437     struct loopback_pcm *dpcm = runtime->private_data;
0438     struct loopback_cable *cable = dpcm->cable;
0439 
0440     cable->hw.formats = pcm_format_to_bits(runtime->format);
0441     cable->hw.rate_min = runtime->rate;
0442     cable->hw.rate_max = runtime->rate;
0443     cable->hw.channels_min = runtime->channels;
0444     cable->hw.channels_max = runtime->channels;
0445 
0446     if (cable->snd_timer.instance) {
0447         cable->hw.period_bytes_min =
0448                 frames_to_bytes(runtime, runtime->period_size);
0449         cable->hw.period_bytes_max = cable->hw.period_bytes_min;
0450     }
0451 
0452 }
0453 
0454 static int loopback_prepare(struct snd_pcm_substream *substream)
0455 {
0456     struct snd_pcm_runtime *runtime = substream->runtime;
0457     struct loopback_pcm *dpcm = runtime->private_data;
0458     struct loopback_cable *cable = dpcm->cable;
0459     int err, bps, salign;
0460 
0461     if (cable->ops->stop_sync) {
0462         err = cable->ops->stop_sync(dpcm);
0463         if (err < 0)
0464             return err;
0465     }
0466 
0467     salign = (snd_pcm_format_physical_width(runtime->format) *
0468                         runtime->channels) / 8;
0469     bps = salign * runtime->rate;
0470     if (bps <= 0 || salign <= 0)
0471         return -EINVAL;
0472 
0473     dpcm->buf_pos = 0;
0474     dpcm->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
0475     if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
0476         /* clear capture buffer */
0477         dpcm->silent_size = dpcm->pcm_buffer_size;
0478         snd_pcm_format_set_silence(runtime->format, runtime->dma_area,
0479                        runtime->buffer_size * runtime->channels);
0480     }
0481 
0482     dpcm->irq_pos = 0;
0483     dpcm->period_update_pending = 0;
0484     dpcm->pcm_bps = bps;
0485     dpcm->pcm_salign = salign;
0486     dpcm->pcm_period_size = frames_to_bytes(runtime, runtime->period_size);
0487 
0488     mutex_lock(&dpcm->loopback->cable_lock);
0489     if (!(cable->valid & ~(1 << substream->stream)) ||
0490             (get_setup(dpcm)->notify &&
0491          substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
0492         params_change(substream);
0493     cable->valid |= 1 << substream->stream;
0494     mutex_unlock(&dpcm->loopback->cable_lock);
0495 
0496     return 0;
0497 }
0498 
0499 static void clear_capture_buf(struct loopback_pcm *dpcm, unsigned int bytes)
0500 {
0501     struct snd_pcm_runtime *runtime = dpcm->substream->runtime;
0502     char *dst = runtime->dma_area;
0503     unsigned int dst_off = dpcm->buf_pos;
0504 
0505     if (dpcm->silent_size >= dpcm->pcm_buffer_size)
0506         return;
0507     if (dpcm->silent_size + bytes > dpcm->pcm_buffer_size)
0508         bytes = dpcm->pcm_buffer_size - dpcm->silent_size;
0509 
0510     for (;;) {
0511         unsigned int size = bytes;
0512         if (dst_off + size > dpcm->pcm_buffer_size)
0513             size = dpcm->pcm_buffer_size - dst_off;
0514         snd_pcm_format_set_silence(runtime->format, dst + dst_off,
0515                        bytes_to_frames(runtime, size) *
0516                         runtime->channels);
0517         dpcm->silent_size += size;
0518         bytes -= size;
0519         if (!bytes)
0520             break;
0521         dst_off = 0;
0522     }
0523 }
0524 
0525 static void copy_play_buf(struct loopback_pcm *play,
0526               struct loopback_pcm *capt,
0527               unsigned int bytes)
0528 {
0529     struct snd_pcm_runtime *runtime = play->substream->runtime;
0530     char *src = runtime->dma_area;
0531     char *dst = capt->substream->runtime->dma_area;
0532     unsigned int src_off = play->buf_pos;
0533     unsigned int dst_off = capt->buf_pos;
0534     unsigned int clear_bytes = 0;
0535 
0536     /* check if playback is draining, trim the capture copy size
0537      * when our pointer is at the end of playback ring buffer */
0538     if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
0539         snd_pcm_playback_hw_avail(runtime) < runtime->buffer_size) { 
0540             snd_pcm_uframes_t appl_ptr, appl_ptr1, diff;
0541         appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
0542         appl_ptr1 -= appl_ptr1 % runtime->buffer_size;
0543         appl_ptr1 += play->buf_pos / play->pcm_salign;
0544         if (appl_ptr < appl_ptr1)
0545             appl_ptr1 -= runtime->buffer_size;
0546         diff = (appl_ptr - appl_ptr1) * play->pcm_salign;
0547         if (diff < bytes) {
0548             clear_bytes = bytes - diff;
0549             bytes = diff;
0550         }
0551     }
0552 
0553     for (;;) {
0554         unsigned int size = bytes;
0555         if (src_off + size > play->pcm_buffer_size)
0556             size = play->pcm_buffer_size - src_off;
0557         if (dst_off + size > capt->pcm_buffer_size)
0558             size = capt->pcm_buffer_size - dst_off;
0559         memcpy(dst + dst_off, src + src_off, size);
0560         capt->silent_size = 0;
0561         bytes -= size;
0562         if (!bytes)
0563             break;
0564         src_off = (src_off + size) % play->pcm_buffer_size;
0565         dst_off = (dst_off + size) % capt->pcm_buffer_size;
0566     }
0567 
0568     if (clear_bytes > 0) {
0569         clear_capture_buf(capt, clear_bytes);
0570         capt->silent_size = 0;
0571     }
0572 }
0573 
0574 static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
0575                      unsigned int jiffies_delta)
0576 {
0577     unsigned long last_pos;
0578     unsigned int delta;
0579 
0580     last_pos = byte_pos(dpcm, dpcm->irq_pos);
0581     dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
0582     delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
0583     if (delta >= dpcm->last_drift)
0584         delta -= dpcm->last_drift;
0585     dpcm->last_drift = 0;
0586     if (dpcm->irq_pos >= dpcm->period_size_frac) {
0587         dpcm->irq_pos %= dpcm->period_size_frac;
0588         dpcm->period_update_pending = 1;
0589     }
0590     return delta;
0591 }
0592 
0593 static inline void bytepos_finish(struct loopback_pcm *dpcm,
0594                   unsigned int delta)
0595 {
0596     dpcm->buf_pos += delta;
0597     dpcm->buf_pos %= dpcm->pcm_buffer_size;
0598 }
0599 
0600 /* call in cable->lock */
0601 static unsigned int loopback_jiffies_timer_pos_update
0602         (struct loopback_cable *cable)
0603 {
0604     struct loopback_pcm *dpcm_play =
0605             cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
0606     struct loopback_pcm *dpcm_capt =
0607             cable->streams[SNDRV_PCM_STREAM_CAPTURE];
0608     unsigned long delta_play = 0, delta_capt = 0, cur_jiffies;
0609     unsigned int running, count1, count2;
0610 
0611     cur_jiffies = jiffies;
0612     running = cable->running ^ cable->pause;
0613     if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
0614         delta_play = cur_jiffies - dpcm_play->last_jiffies;
0615         dpcm_play->last_jiffies += delta_play;
0616     }
0617 
0618     if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
0619         delta_capt = cur_jiffies - dpcm_capt->last_jiffies;
0620         dpcm_capt->last_jiffies += delta_capt;
0621     }
0622 
0623     if (delta_play == 0 && delta_capt == 0)
0624         goto unlock;
0625         
0626     if (delta_play > delta_capt) {
0627         count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
0628         bytepos_finish(dpcm_play, count1);
0629         delta_play = delta_capt;
0630     } else if (delta_play < delta_capt) {
0631         count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
0632         clear_capture_buf(dpcm_capt, count1);
0633         bytepos_finish(dpcm_capt, count1);
0634         delta_capt = delta_play;
0635     }
0636 
0637     if (delta_play == 0 && delta_capt == 0)
0638         goto unlock;
0639 
0640     /* note delta_capt == delta_play at this moment */
0641     count1 = bytepos_delta(dpcm_play, delta_play);
0642     count2 = bytepos_delta(dpcm_capt, delta_capt);
0643     if (count1 < count2) {
0644         dpcm_capt->last_drift = count2 - count1;
0645         count1 = count2;
0646     } else if (count1 > count2) {
0647         dpcm_play->last_drift = count1 - count2;
0648     }
0649     copy_play_buf(dpcm_play, dpcm_capt, count1);
0650     bytepos_finish(dpcm_play, count1);
0651     bytepos_finish(dpcm_capt, count1);
0652  unlock:
0653     return running;
0654 }
0655 
0656 static void loopback_jiffies_timer_function(struct timer_list *t)
0657 {
0658     struct loopback_pcm *dpcm = from_timer(dpcm, t, timer);
0659     unsigned long flags;
0660 
0661     spin_lock_irqsave(&dpcm->cable->lock, flags);
0662     if (loopback_jiffies_timer_pos_update(dpcm->cable) &
0663             (1 << dpcm->substream->stream)) {
0664         loopback_jiffies_timer_start(dpcm);
0665         if (dpcm->period_update_pending) {
0666             dpcm->period_update_pending = 0;
0667             spin_unlock_irqrestore(&dpcm->cable->lock, flags);
0668             /* need to unlock before calling below */
0669             snd_pcm_period_elapsed(dpcm->substream);
0670             return;
0671         }
0672     }
0673     spin_unlock_irqrestore(&dpcm->cable->lock, flags);
0674 }
0675 
0676 /* call in cable->lock */
0677 static int loopback_snd_timer_check_resolution(struct snd_pcm_runtime *runtime,
0678                            unsigned long resolution)
0679 {
0680     if (resolution != runtime->timer_resolution) {
0681         struct loopback_pcm *dpcm = runtime->private_data;
0682         struct loopback_cable *cable = dpcm->cable;
0683         /* Worst case estimation of possible values for resolution
0684          * resolution <= (512 * 1024) frames / 8kHz in nsec
0685          * resolution <= 65.536.000.000 nsec
0686          *
0687          * period_size <= 65.536.000.000 nsec / 1000nsec/usec * 192kHz +
0688          *  500.000
0689          * period_size <= 12.582.912.000.000  <64bit
0690          *  / 1.000.000 usec/sec
0691          */
0692         snd_pcm_uframes_t period_size_usec =
0693                 resolution / 1000 * runtime->rate;
0694         /* round to nearest sample rate */
0695         snd_pcm_uframes_t period_size =
0696                 (period_size_usec + 500 * 1000) / (1000 * 1000);
0697 
0698         pcm_err(dpcm->substream->pcm,
0699             "Period size (%lu frames) of loopback device is not corresponding to timer resolution (%lu nsec = %lu frames) of card timer %d,%d,%d. Use period size of %lu frames for loopback device.",
0700             runtime->period_size, resolution, period_size,
0701             cable->snd_timer.id.card,
0702             cable->snd_timer.id.device,
0703             cable->snd_timer.id.subdevice,
0704             period_size);
0705         return -EINVAL;
0706     }
0707     return 0;
0708 }
0709 
0710 static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,
0711                           int event,
0712                           unsigned long resolution)
0713 {
0714     struct loopback_pcm *dpcm_play, *dpcm_capt;
0715     struct snd_pcm_substream *substream_play, *substream_capt;
0716     struct snd_pcm_runtime *valid_runtime;
0717     unsigned int running, elapsed_bytes;
0718     unsigned long flags;
0719 
0720     spin_lock_irqsave(&cable->lock, flags);
0721     running = cable->running ^ cable->pause;
0722     /* no need to do anything if no stream is running */
0723     if (!running) {
0724         spin_unlock_irqrestore(&cable->lock, flags);
0725         return;
0726     }
0727 
0728     dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
0729     dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE];
0730 
0731     if (event == SNDRV_TIMER_EVENT_MSTOP) {
0732         if (!dpcm_play ||
0733             dpcm_play->substream->runtime->status->state !=
0734                 SNDRV_PCM_STATE_DRAINING) {
0735             spin_unlock_irqrestore(&cable->lock, flags);
0736             return;
0737         }
0738     }
0739 
0740     substream_play = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
0741             dpcm_play->substream : NULL;
0742     substream_capt = (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) ?
0743             dpcm_capt->substream : NULL;
0744     valid_runtime = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
0745                 dpcm_play->substream->runtime :
0746                 dpcm_capt->substream->runtime;
0747 
0748     /* resolution is only valid for SNDRV_TIMER_EVENT_TICK events */
0749     if (event == SNDRV_TIMER_EVENT_TICK) {
0750         /* The hardware rules guarantee that playback and capture period
0751          * are the same. Therefore only one device has to be checked
0752          * here.
0753          */
0754         if (loopback_snd_timer_check_resolution(valid_runtime,
0755                             resolution) < 0) {
0756             spin_unlock_irqrestore(&cable->lock, flags);
0757             if (substream_play)
0758                 snd_pcm_stop_xrun(substream_play);
0759             if (substream_capt)
0760                 snd_pcm_stop_xrun(substream_capt);
0761             return;
0762         }
0763     }
0764 
0765     elapsed_bytes = frames_to_bytes(valid_runtime,
0766                     valid_runtime->period_size);
0767     /* The same timer interrupt is used for playback and capture device */
0768     if ((running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
0769         (running & (1 << SNDRV_PCM_STREAM_CAPTURE))) {
0770         copy_play_buf(dpcm_play, dpcm_capt, elapsed_bytes);
0771         bytepos_finish(dpcm_play, elapsed_bytes);
0772         bytepos_finish(dpcm_capt, elapsed_bytes);
0773     } else if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
0774         bytepos_finish(dpcm_play, elapsed_bytes);
0775     } else if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
0776         clear_capture_buf(dpcm_capt, elapsed_bytes);
0777         bytepos_finish(dpcm_capt, elapsed_bytes);
0778     }
0779     spin_unlock_irqrestore(&cable->lock, flags);
0780 
0781     if (substream_play)
0782         snd_pcm_period_elapsed(substream_play);
0783     if (substream_capt)
0784         snd_pcm_period_elapsed(substream_capt);
0785 }
0786 
0787 static void loopback_snd_timer_function(struct snd_timer_instance *timeri,
0788                     unsigned long resolution,
0789                     unsigned long ticks)
0790 {
0791     struct loopback_cable *cable = timeri->callback_data;
0792 
0793     loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_TICK,
0794                       resolution);
0795 }
0796 
0797 static void loopback_snd_timer_work(struct work_struct *work)
0798 {
0799     struct loopback_cable *cable;
0800 
0801     cable = container_of(work, struct loopback_cable, snd_timer.event_work);
0802     loopback_snd_timer_period_elapsed(cable, SNDRV_TIMER_EVENT_MSTOP, 0);
0803 }
0804 
0805 static void loopback_snd_timer_event(struct snd_timer_instance *timeri,
0806                      int event,
0807                      struct timespec64 *tstamp,
0808                      unsigned long resolution)
0809 {
0810     /* Do not lock cable->lock here because timer->lock is already hold.
0811      * There are other functions which first lock cable->lock and than
0812      * timer->lock e.g.
0813      * loopback_trigger()
0814      * spin_lock(&cable->lock)
0815      * loopback_snd_timer_start()
0816      * snd_timer_start()
0817      * spin_lock(&timer->lock)
0818      * Therefore when using the oposit order of locks here it could result
0819      * in a deadlock.
0820      */
0821 
0822     if (event == SNDRV_TIMER_EVENT_MSTOP) {
0823         struct loopback_cable *cable = timeri->callback_data;
0824 
0825         /* sound card of the timer was stopped. Therefore there will not
0826          * be any further timer callbacks. Due to this forward audio
0827          * data from here if in draining state. When still in running
0828          * state the streaming will be aborted by the usual timeout. It
0829          * should not be aborted here because may be the timer sound
0830          * card does only a recovery and the timer is back soon.
0831          * This work triggers loopback_snd_timer_work()
0832          */
0833         schedule_work(&cable->snd_timer.event_work);
0834     }
0835 }
0836 
0837 static void loopback_jiffies_timer_dpcm_info(struct loopback_pcm *dpcm,
0838                          struct snd_info_buffer *buffer)
0839 {
0840     snd_iprintf(buffer, "    update_pending:\t%u\n",
0841             dpcm->period_update_pending);
0842     snd_iprintf(buffer, "    irq_pos:\t\t%u\n", dpcm->irq_pos);
0843     snd_iprintf(buffer, "    period_frac:\t%u\n", dpcm->period_size_frac);
0844     snd_iprintf(buffer, "    last_jiffies:\t%lu (%lu)\n",
0845             dpcm->last_jiffies, jiffies);
0846     snd_iprintf(buffer, "    timer_expires:\t%lu\n", dpcm->timer.expires);
0847 }
0848 
0849 static void loopback_snd_timer_dpcm_info(struct loopback_pcm *dpcm,
0850                      struct snd_info_buffer *buffer)
0851 {
0852     struct loopback_cable *cable = dpcm->cable;
0853 
0854     snd_iprintf(buffer, "    sound timer:\thw:%d,%d,%d\n",
0855             cable->snd_timer.id.card,
0856             cable->snd_timer.id.device,
0857             cable->snd_timer.id.subdevice);
0858     snd_iprintf(buffer, "    timer open:\t\t%s\n",
0859             (cable->snd_timer.stream == SNDRV_PCM_STREAM_CAPTURE) ?
0860                 "capture" : "playback");
0861 }
0862 
0863 static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
0864 {
0865     struct snd_pcm_runtime *runtime = substream->runtime;
0866     struct loopback_pcm *dpcm = runtime->private_data;
0867     snd_pcm_uframes_t pos;
0868 
0869     spin_lock(&dpcm->cable->lock);
0870     if (dpcm->cable->ops->pos_update)
0871         dpcm->cable->ops->pos_update(dpcm->cable);
0872     pos = dpcm->buf_pos;
0873     spin_unlock(&dpcm->cable->lock);
0874     return bytes_to_frames(runtime, pos);
0875 }
0876 
0877 static const struct snd_pcm_hardware loopback_pcm_hardware =
0878 {
0879     .info =     (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
0880              SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
0881              SNDRV_PCM_INFO_RESUME),
0882     .formats =  (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
0883              SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
0884              SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
0885              SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
0886              SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
0887     .rates =    SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_192000,
0888     .rate_min =     8000,
0889     .rate_max =     192000,
0890     .channels_min =     1,
0891     .channels_max =     32,
0892     .buffer_bytes_max = 2 * 1024 * 1024,
0893     .period_bytes_min = 64,
0894     /* note check overflow in frac_pos() using pcm_rate_shift before
0895        changing period_bytes_max value */
0896     .period_bytes_max = 1024 * 1024,
0897     .periods_min =      1,
0898     .periods_max =      1024,
0899     .fifo_size =        0,
0900 };
0901 
0902 static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
0903 {
0904     struct loopback_pcm *dpcm = runtime->private_data;
0905     kfree(dpcm);
0906 }
0907 
0908 static int loopback_hw_free(struct snd_pcm_substream *substream)
0909 {
0910     struct snd_pcm_runtime *runtime = substream->runtime;
0911     struct loopback_pcm *dpcm = runtime->private_data;
0912     struct loopback_cable *cable = dpcm->cable;
0913 
0914     mutex_lock(&dpcm->loopback->cable_lock);
0915     cable->valid &= ~(1 << substream->stream);
0916     mutex_unlock(&dpcm->loopback->cable_lock);
0917     return 0;
0918 }
0919 
0920 static unsigned int get_cable_index(struct snd_pcm_substream *substream)
0921 {
0922     if (!substream->pcm->device)
0923         return substream->stream;
0924     else
0925         return !substream->stream;
0926 }
0927 
0928 static int rule_format(struct snd_pcm_hw_params *params,
0929                struct snd_pcm_hw_rule *rule)
0930 {
0931     struct loopback_pcm *dpcm = rule->private;
0932     struct loopback_cable *cable = dpcm->cable;
0933     struct snd_mask m;
0934 
0935     snd_mask_none(&m);
0936     mutex_lock(&dpcm->loopback->cable_lock);
0937     m.bits[0] = (u_int32_t)cable->hw.formats;
0938     m.bits[1] = (u_int32_t)(cable->hw.formats >> 32);
0939     mutex_unlock(&dpcm->loopback->cable_lock);
0940     return snd_mask_refine(hw_param_mask(params, rule->var), &m);
0941 }
0942 
0943 static int rule_rate(struct snd_pcm_hw_params *params,
0944              struct snd_pcm_hw_rule *rule)
0945 {
0946     struct loopback_pcm *dpcm = rule->private;
0947     struct loopback_cable *cable = dpcm->cable;
0948     struct snd_interval t;
0949 
0950     mutex_lock(&dpcm->loopback->cable_lock);
0951     t.min = cable->hw.rate_min;
0952     t.max = cable->hw.rate_max;
0953     mutex_unlock(&dpcm->loopback->cable_lock);
0954         t.openmin = t.openmax = 0;
0955         t.integer = 0;
0956     return snd_interval_refine(hw_param_interval(params, rule->var), &t);
0957 }
0958 
0959 static int rule_channels(struct snd_pcm_hw_params *params,
0960              struct snd_pcm_hw_rule *rule)
0961 {
0962     struct loopback_pcm *dpcm = rule->private;
0963     struct loopback_cable *cable = dpcm->cable;
0964     struct snd_interval t;
0965 
0966     mutex_lock(&dpcm->loopback->cable_lock);
0967     t.min = cable->hw.channels_min;
0968     t.max = cable->hw.channels_max;
0969     mutex_unlock(&dpcm->loopback->cable_lock);
0970         t.openmin = t.openmax = 0;
0971         t.integer = 0;
0972     return snd_interval_refine(hw_param_interval(params, rule->var), &t);
0973 }
0974 
0975 static int rule_period_bytes(struct snd_pcm_hw_params *params,
0976                  struct snd_pcm_hw_rule *rule)
0977 {
0978     struct loopback_pcm *dpcm = rule->private;
0979     struct loopback_cable *cable = dpcm->cable;
0980     struct snd_interval t;
0981 
0982     mutex_lock(&dpcm->loopback->cable_lock);
0983     t.min = cable->hw.period_bytes_min;
0984     t.max = cable->hw.period_bytes_max;
0985     mutex_unlock(&dpcm->loopback->cable_lock);
0986     t.openmin = 0;
0987     t.openmax = 0;
0988     t.integer = 0;
0989     return snd_interval_refine(hw_param_interval(params, rule->var), &t);
0990 }
0991 
0992 static void free_cable(struct snd_pcm_substream *substream)
0993 {
0994     struct loopback *loopback = substream->private_data;
0995     int dev = get_cable_index(substream);
0996     struct loopback_cable *cable;
0997 
0998     cable = loopback->cables[substream->number][dev];
0999     if (!cable)
1000         return;
1001     if (cable->streams[!substream->stream]) {
1002         /* other stream is still alive */
1003         spin_lock_irq(&cable->lock);
1004         cable->streams[substream->stream] = NULL;
1005         spin_unlock_irq(&cable->lock);
1006     } else {
1007         struct loopback_pcm *dpcm = substream->runtime->private_data;
1008 
1009         if (cable->ops && cable->ops->close_cable && dpcm)
1010             cable->ops->close_cable(dpcm);
1011         /* free the cable */
1012         loopback->cables[substream->number][dev] = NULL;
1013         kfree(cable);
1014     }
1015 }
1016 
1017 static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm)
1018 {
1019     timer_setup(&dpcm->timer, loopback_jiffies_timer_function, 0);
1020 
1021     return 0;
1022 }
1023 
1024 static const struct loopback_ops loopback_jiffies_timer_ops = {
1025     .open = loopback_jiffies_timer_open,
1026     .start = loopback_jiffies_timer_start,
1027     .stop = loopback_jiffies_timer_stop,
1028     .stop_sync = loopback_jiffies_timer_stop_sync,
1029     .close_substream = loopback_jiffies_timer_stop_sync,
1030     .pos_update = loopback_jiffies_timer_pos_update,
1031     .dpcm_info = loopback_jiffies_timer_dpcm_info,
1032 };
1033 
1034 static int loopback_parse_timer_id(const char *str,
1035                    struct snd_timer_id *tid)
1036 {
1037     /* [<pref>:](<card name>|<card idx>)[{.,}<dev idx>[{.,}<subdev idx>]] */
1038     const char * const sep_dev = ".,";
1039     const char * const sep_pref = ":";
1040     const char *name = str;
1041     char *sep, save = '\0';
1042     int card_idx = 0, dev = 0, subdev = 0;
1043     int err;
1044 
1045     sep = strpbrk(str, sep_pref);
1046     if (sep)
1047         name = sep + 1;
1048     sep = strpbrk(name, sep_dev);
1049     if (sep) {
1050         save = *sep;
1051         *sep = '\0';
1052     }
1053     err = kstrtoint(name, 0, &card_idx);
1054     if (err == -EINVAL) {
1055         /* Must be the name, not number */
1056         for (card_idx = 0; card_idx < snd_ecards_limit; card_idx++) {
1057             struct snd_card *card = snd_card_ref(card_idx);
1058 
1059             if (card) {
1060                 if (!strcmp(card->id, name))
1061                     err = 0;
1062                 snd_card_unref(card);
1063             }
1064             if (!err)
1065                 break;
1066         }
1067     }
1068     if (sep) {
1069         *sep = save;
1070         if (!err) {
1071             char *sep2, save2 = '\0';
1072 
1073             sep2 = strpbrk(sep + 1, sep_dev);
1074             if (sep2) {
1075                 save2 = *sep2;
1076                 *sep2 = '\0';
1077             }
1078             err = kstrtoint(sep + 1, 0, &dev);
1079             if (sep2) {
1080                 *sep2 = save2;
1081                 if (!err)
1082                     err = kstrtoint(sep2 + 1, 0, &subdev);
1083             }
1084         }
1085     }
1086     if (!err && tid) {
1087         tid->card = card_idx;
1088         tid->device = dev;
1089         tid->subdevice = subdev;
1090     }
1091     return err;
1092 }
1093 
1094 /* call in loopback->cable_lock */
1095 static int loopback_snd_timer_open(struct loopback_pcm *dpcm)
1096 {
1097     int err = 0;
1098     struct snd_timer_id tid = {
1099         .dev_class = SNDRV_TIMER_CLASS_PCM,
1100         .dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION,
1101     };
1102     struct snd_timer_instance *timeri;
1103     struct loopback_cable *cable = dpcm->cable;
1104 
1105     /* check if timer was already opened. It is only opened once
1106      * per playback and capture subdevice (aka cable).
1107      */
1108     if (cable->snd_timer.instance)
1109         goto exit;
1110 
1111     err = loopback_parse_timer_id(dpcm->loopback->timer_source, &tid);
1112     if (err < 0) {
1113         pcm_err(dpcm->substream->pcm,
1114             "Parsing timer source \'%s\' failed with %d",
1115             dpcm->loopback->timer_source, err);
1116         goto exit;
1117     }
1118 
1119     cable->snd_timer.stream = dpcm->substream->stream;
1120     cable->snd_timer.id = tid;
1121 
1122     timeri = snd_timer_instance_new(dpcm->loopback->card->id);
1123     if (!timeri) {
1124         err = -ENOMEM;
1125         goto exit;
1126     }
1127     /* The callback has to be called from another work. If
1128      * SNDRV_TIMER_IFLG_FAST is specified it will be called from the
1129      * snd_pcm_period_elapsed() call of the selected sound card.
1130      * snd_pcm_period_elapsed() helds snd_pcm_stream_lock_irqsave().
1131      * Due to our callback loopback_snd_timer_function() also calls
1132      * snd_pcm_period_elapsed() which calls snd_pcm_stream_lock_irqsave().
1133      * This would end up in a dead lock.
1134      */
1135     timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
1136     timeri->callback = loopback_snd_timer_function;
1137     timeri->callback_data = (void *)cable;
1138     timeri->ccallback = loopback_snd_timer_event;
1139 
1140     /* initialise a work used for draining */
1141     INIT_WORK(&cable->snd_timer.event_work, loopback_snd_timer_work);
1142 
1143     /* The mutex loopback->cable_lock is kept locked.
1144      * Therefore snd_timer_open() cannot be called a second time
1145      * by the other device of the same cable.
1146      * Therefore the following issue cannot happen:
1147      * [proc1] Call loopback_timer_open() ->
1148      *     Unlock cable->lock for snd_timer_close/open() call
1149      * [proc2] Call loopback_timer_open() -> snd_timer_open(),
1150      *     snd_timer_start()
1151      * [proc1] Call snd_timer_open() and overwrite running timer
1152      *     instance
1153      */
1154     err = snd_timer_open(timeri, &cable->snd_timer.id, current->pid);
1155     if (err < 0) {
1156         pcm_err(dpcm->substream->pcm,
1157             "snd_timer_open (%d,%d,%d) failed with %d",
1158             cable->snd_timer.id.card,
1159             cable->snd_timer.id.device,
1160             cable->snd_timer.id.subdevice,
1161             err);
1162         snd_timer_instance_free(timeri);
1163         goto exit;
1164     }
1165 
1166     cable->snd_timer.instance = timeri;
1167 
1168 exit:
1169     return err;
1170 }
1171 
1172 /* stop_sync() is not required for sound timer because it does not need to be
1173  * restarted in loopback_prepare() on Xrun recovery
1174  */
1175 static const struct loopback_ops loopback_snd_timer_ops = {
1176     .open = loopback_snd_timer_open,
1177     .start = loopback_snd_timer_start,
1178     .stop = loopback_snd_timer_stop,
1179     .close_cable = loopback_snd_timer_close_cable,
1180     .dpcm_info = loopback_snd_timer_dpcm_info,
1181 };
1182 
1183 static int loopback_open(struct snd_pcm_substream *substream)
1184 {
1185     struct snd_pcm_runtime *runtime = substream->runtime;
1186     struct loopback *loopback = substream->private_data;
1187     struct loopback_pcm *dpcm;
1188     struct loopback_cable *cable = NULL;
1189     int err = 0;
1190     int dev = get_cable_index(substream);
1191 
1192     mutex_lock(&loopback->cable_lock);
1193     dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1194     if (!dpcm) {
1195         err = -ENOMEM;
1196         goto unlock;
1197     }
1198     dpcm->loopback = loopback;
1199     dpcm->substream = substream;
1200 
1201     cable = loopback->cables[substream->number][dev];
1202     if (!cable) {
1203         cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1204         if (!cable) {
1205             err = -ENOMEM;
1206             goto unlock;
1207         }
1208         spin_lock_init(&cable->lock);
1209         cable->hw = loopback_pcm_hardware;
1210         if (loopback->timer_source)
1211             cable->ops = &loopback_snd_timer_ops;
1212         else
1213             cable->ops = &loopback_jiffies_timer_ops;
1214         loopback->cables[substream->number][dev] = cable;
1215     }
1216     dpcm->cable = cable;
1217     runtime->private_data = dpcm;
1218 
1219     if (cable->ops->open) {
1220         err = cable->ops->open(dpcm);
1221         if (err < 0)
1222             goto unlock;
1223     }
1224 
1225     snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1226 
1227     /* use dynamic rules based on actual runtime->hw values */
1228     /* note that the default rules created in the PCM midlevel code */
1229     /* are cached -> they do not reflect the actual state */
1230     err = snd_pcm_hw_rule_add(runtime, 0,
1231                   SNDRV_PCM_HW_PARAM_FORMAT,
1232                   rule_format, dpcm,
1233                   SNDRV_PCM_HW_PARAM_FORMAT, -1);
1234     if (err < 0)
1235         goto unlock;
1236     err = snd_pcm_hw_rule_add(runtime, 0,
1237                   SNDRV_PCM_HW_PARAM_RATE,
1238                   rule_rate, dpcm,
1239                   SNDRV_PCM_HW_PARAM_RATE, -1);
1240     if (err < 0)
1241         goto unlock;
1242     err = snd_pcm_hw_rule_add(runtime, 0,
1243                   SNDRV_PCM_HW_PARAM_CHANNELS,
1244                   rule_channels, dpcm,
1245                   SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1246     if (err < 0)
1247         goto unlock;
1248 
1249     /* In case of sound timer the period time of both devices of the same
1250      * loop has to be the same.
1251      * This rule only takes effect if a sound timer was chosen
1252      */
1253     if (cable->snd_timer.instance) {
1254         err = snd_pcm_hw_rule_add(runtime, 0,
1255                       SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1256                       rule_period_bytes, dpcm,
1257                       SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1);
1258         if (err < 0)
1259             goto unlock;
1260     }
1261 
1262     /* loopback_runtime_free() has not to be called if kfree(dpcm) was
1263      * already called here. Otherwise it will end up with a double free.
1264      */
1265     runtime->private_free = loopback_runtime_free;
1266     if (get_notify(dpcm))
1267         runtime->hw = loopback_pcm_hardware;
1268     else
1269         runtime->hw = cable->hw;
1270 
1271     spin_lock_irq(&cable->lock);
1272     cable->streams[substream->stream] = dpcm;
1273     spin_unlock_irq(&cable->lock);
1274 
1275  unlock:
1276     if (err < 0) {
1277         free_cable(substream);
1278         kfree(dpcm);
1279     }
1280     mutex_unlock(&loopback->cable_lock);
1281     return err;
1282 }
1283 
1284 static int loopback_close(struct snd_pcm_substream *substream)
1285 {
1286     struct loopback *loopback = substream->private_data;
1287     struct loopback_pcm *dpcm = substream->runtime->private_data;
1288     int err = 0;
1289 
1290     if (dpcm->cable->ops->close_substream)
1291         err = dpcm->cable->ops->close_substream(dpcm);
1292     mutex_lock(&loopback->cable_lock);
1293     free_cable(substream);
1294     mutex_unlock(&loopback->cable_lock);
1295     return err;
1296 }
1297 
1298 static const struct snd_pcm_ops loopback_pcm_ops = {
1299     .open =     loopback_open,
1300     .close =    loopback_close,
1301     .hw_free =  loopback_hw_free,
1302     .prepare =  loopback_prepare,
1303     .trigger =  loopback_trigger,
1304     .pointer =  loopback_pointer,
1305 };
1306 
1307 static int loopback_pcm_new(struct loopback *loopback,
1308                 int device, int substreams)
1309 {
1310     struct snd_pcm *pcm;
1311     int err;
1312 
1313     err = snd_pcm_new(loopback->card, "Loopback PCM", device,
1314               substreams, substreams, &pcm);
1315     if (err < 0)
1316         return err;
1317     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &loopback_pcm_ops);
1318     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &loopback_pcm_ops);
1319     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
1320 
1321     pcm->private_data = loopback;
1322     pcm->info_flags = 0;
1323     strcpy(pcm->name, "Loopback PCM");
1324 
1325     loopback->pcm[device] = pcm;
1326     return 0;
1327 }
1328 
1329 static int loopback_rate_shift_info(struct snd_kcontrol *kcontrol,   
1330                     struct snd_ctl_elem_info *uinfo) 
1331 {
1332     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1333     uinfo->count = 1;
1334     uinfo->value.integer.min = 80000;
1335     uinfo->value.integer.max = 120000;
1336     uinfo->value.integer.step = 1;
1337     return 0;
1338 }                                  
1339 
1340 static int loopback_rate_shift_get(struct snd_kcontrol *kcontrol,
1341                    struct snd_ctl_elem_value *ucontrol)
1342 {
1343     struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1344     
1345     mutex_lock(&loopback->cable_lock);
1346     ucontrol->value.integer.value[0] =
1347         loopback->setup[kcontrol->id.subdevice]
1348                    [kcontrol->id.device].rate_shift;
1349     mutex_unlock(&loopback->cable_lock);
1350     return 0;
1351 }
1352 
1353 static int loopback_rate_shift_put(struct snd_kcontrol *kcontrol,
1354                    struct snd_ctl_elem_value *ucontrol)
1355 {
1356     struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1357     unsigned int val;
1358     int change = 0;
1359 
1360     val = ucontrol->value.integer.value[0];
1361     if (val < 80000)
1362         val = 80000;
1363     if (val > 120000)
1364         val = 120000;   
1365     mutex_lock(&loopback->cable_lock);
1366     if (val != loopback->setup[kcontrol->id.subdevice]
1367                   [kcontrol->id.device].rate_shift) {
1368         loopback->setup[kcontrol->id.subdevice]
1369                    [kcontrol->id.device].rate_shift = val;
1370         change = 1;
1371     }
1372     mutex_unlock(&loopback->cable_lock);
1373     return change;
1374 }
1375 
1376 static int loopback_notify_get(struct snd_kcontrol *kcontrol,
1377                    struct snd_ctl_elem_value *ucontrol)
1378 {
1379     struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1380     
1381     mutex_lock(&loopback->cable_lock);
1382     ucontrol->value.integer.value[0] =
1383         loopback->setup[kcontrol->id.subdevice]
1384                    [kcontrol->id.device].notify;
1385     mutex_unlock(&loopback->cable_lock);
1386     return 0;
1387 }
1388 
1389 static int loopback_notify_put(struct snd_kcontrol *kcontrol,
1390                    struct snd_ctl_elem_value *ucontrol)
1391 {
1392     struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1393     unsigned int val;
1394     int change = 0;
1395 
1396     val = ucontrol->value.integer.value[0] ? 1 : 0;
1397     mutex_lock(&loopback->cable_lock);
1398     if (val != loopback->setup[kcontrol->id.subdevice]
1399                 [kcontrol->id.device].notify) {
1400         loopback->setup[kcontrol->id.subdevice]
1401             [kcontrol->id.device].notify = val;
1402         change = 1;
1403     }
1404     mutex_unlock(&loopback->cable_lock);
1405     return change;
1406 }
1407 
1408 static int loopback_active_get(struct snd_kcontrol *kcontrol,
1409                    struct snd_ctl_elem_value *ucontrol)
1410 {
1411     struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1412     struct loopback_cable *cable;
1413 
1414     unsigned int val = 0;
1415 
1416     mutex_lock(&loopback->cable_lock);
1417     cable = loopback->cables[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
1418     if (cable != NULL) {
1419         unsigned int running = cable->running ^ cable->pause;
1420 
1421         val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
1422     }
1423     mutex_unlock(&loopback->cable_lock);
1424     ucontrol->value.integer.value[0] = val;
1425     return 0;
1426 }
1427 
1428 static int loopback_format_info(struct snd_kcontrol *kcontrol,   
1429                 struct snd_ctl_elem_info *uinfo) 
1430 {
1431     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1432     uinfo->count = 1;
1433     uinfo->value.integer.min = 0;
1434     uinfo->value.integer.max = (__force int)SNDRV_PCM_FORMAT_LAST;
1435     uinfo->value.integer.step = 1;
1436     return 0;
1437 }                                  
1438 
1439 static int loopback_format_get(struct snd_kcontrol *kcontrol,
1440                    struct snd_ctl_elem_value *ucontrol)
1441 {
1442     struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1443     
1444     ucontrol->value.integer.value[0] =
1445         (__force int)loopback->setup[kcontrol->id.subdevice]
1446                    [kcontrol->id.device].format;
1447     return 0;
1448 }
1449 
1450 static int loopback_rate_info(struct snd_kcontrol *kcontrol,   
1451                   struct snd_ctl_elem_info *uinfo) 
1452 {
1453     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1454     uinfo->count = 1;
1455     uinfo->value.integer.min = 0;
1456     uinfo->value.integer.max = 192000;
1457     uinfo->value.integer.step = 1;
1458     return 0;
1459 }                                  
1460 
1461 static int loopback_rate_get(struct snd_kcontrol *kcontrol,
1462                  struct snd_ctl_elem_value *ucontrol)
1463 {
1464     struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1465     
1466     mutex_lock(&loopback->cable_lock);
1467     ucontrol->value.integer.value[0] =
1468         loopback->setup[kcontrol->id.subdevice]
1469                    [kcontrol->id.device].rate;
1470     mutex_unlock(&loopback->cable_lock);
1471     return 0;
1472 }
1473 
1474 static int loopback_channels_info(struct snd_kcontrol *kcontrol,   
1475                   struct snd_ctl_elem_info *uinfo) 
1476 {
1477     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1478     uinfo->count = 1;
1479     uinfo->value.integer.min = 1;
1480     uinfo->value.integer.max = 1024;
1481     uinfo->value.integer.step = 1;
1482     return 0;
1483 }                                  
1484 
1485 static int loopback_channels_get(struct snd_kcontrol *kcontrol,
1486                  struct snd_ctl_elem_value *ucontrol)
1487 {
1488     struct loopback *loopback = snd_kcontrol_chip(kcontrol);
1489     
1490     mutex_lock(&loopback->cable_lock);
1491     ucontrol->value.integer.value[0] =
1492         loopback->setup[kcontrol->id.subdevice]
1493                    [kcontrol->id.device].channels;
1494     mutex_unlock(&loopback->cable_lock);
1495     return 0;
1496 }
1497 
1498 static const struct snd_kcontrol_new loopback_controls[]  = {
1499 {
1500     .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1501     .name =         "PCM Rate Shift 100000",
1502     .info =         loopback_rate_shift_info,
1503     .get =          loopback_rate_shift_get,
1504     .put =          loopback_rate_shift_put,
1505 },
1506 {
1507     .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1508     .name =         "PCM Notify",
1509     .info =         snd_ctl_boolean_mono_info,
1510     .get =          loopback_notify_get,
1511     .put =          loopback_notify_put,
1512 },
1513 #define ACTIVE_IDX 2
1514 {
1515     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1516     .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1517     .name =         "PCM Slave Active",
1518     .info =         snd_ctl_boolean_mono_info,
1519     .get =          loopback_active_get,
1520 },
1521 #define FORMAT_IDX 3
1522 {
1523     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1524     .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1525     .name =         "PCM Slave Format",
1526     .info =         loopback_format_info,
1527     .get =          loopback_format_get
1528 },
1529 #define RATE_IDX 4
1530 {
1531     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1532     .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1533     .name =         "PCM Slave Rate",
1534     .info =         loopback_rate_info,
1535     .get =          loopback_rate_get
1536 },
1537 #define CHANNELS_IDX 5
1538 {
1539     .access =   SNDRV_CTL_ELEM_ACCESS_READ,
1540     .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1541     .name =         "PCM Slave Channels",
1542     .info =         loopback_channels_info,
1543     .get =          loopback_channels_get
1544 }
1545 };
1546 
1547 static int loopback_mixer_new(struct loopback *loopback, int notify)
1548 {
1549     struct snd_card *card = loopback->card;
1550     struct snd_pcm *pcm;
1551     struct snd_kcontrol *kctl;
1552     struct loopback_setup *setup;
1553     int err, dev, substr, substr_count, idx;
1554 
1555     strcpy(card->mixername, "Loopback Mixer");
1556     for (dev = 0; dev < 2; dev++) {
1557         pcm = loopback->pcm[dev];
1558         substr_count =
1559             pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
1560         for (substr = 0; substr < substr_count; substr++) {
1561             setup = &loopback->setup[substr][dev];
1562             setup->notify = notify;
1563             setup->rate_shift = NO_PITCH;
1564             setup->format = SNDRV_PCM_FORMAT_S16_LE;
1565             setup->rate = 48000;
1566             setup->channels = 2;
1567             for (idx = 0; idx < ARRAY_SIZE(loopback_controls);
1568                                     idx++) {
1569                 kctl = snd_ctl_new1(&loopback_controls[idx],
1570                             loopback);
1571                 if (!kctl)
1572                     return -ENOMEM;
1573                 kctl->id.device = dev;
1574                 kctl->id.subdevice = substr;
1575 
1576                 /* Add the control before copying the id so that
1577                  * the numid field of the id is set in the copy.
1578                  */
1579                 err = snd_ctl_add(card, kctl);
1580                 if (err < 0)
1581                     return err;
1582 
1583                 switch (idx) {
1584                 case ACTIVE_IDX:
1585                     setup->active_id = kctl->id;
1586                     break;
1587                 case FORMAT_IDX:
1588                     setup->format_id = kctl->id;
1589                     break;
1590                 case RATE_IDX:
1591                     setup->rate_id = kctl->id;
1592                     break;
1593                 case CHANNELS_IDX:
1594                     setup->channels_id = kctl->id;
1595                     break;
1596                 default:
1597                     break;
1598                 }
1599             }
1600         }
1601     }
1602     return 0;
1603 }
1604 
1605 static void print_dpcm_info(struct snd_info_buffer *buffer,
1606                 struct loopback_pcm *dpcm,
1607                 const char *id)
1608 {
1609     snd_iprintf(buffer, "  %s\n", id);
1610     if (dpcm == NULL) {
1611         snd_iprintf(buffer, "    inactive\n");
1612         return;
1613     }
1614     snd_iprintf(buffer, "    buffer_size:\t%u\n", dpcm->pcm_buffer_size);
1615     snd_iprintf(buffer, "    buffer_pos:\t\t%u\n", dpcm->buf_pos);
1616     snd_iprintf(buffer, "    silent_size:\t%u\n", dpcm->silent_size);
1617     snd_iprintf(buffer, "    period_size:\t%u\n", dpcm->pcm_period_size);
1618     snd_iprintf(buffer, "    bytes_per_sec:\t%u\n", dpcm->pcm_bps);
1619     snd_iprintf(buffer, "    sample_align:\t%u\n", dpcm->pcm_salign);
1620     snd_iprintf(buffer, "    rate_shift:\t\t%u\n", dpcm->pcm_rate_shift);
1621     if (dpcm->cable->ops->dpcm_info)
1622         dpcm->cable->ops->dpcm_info(dpcm, buffer);
1623 }
1624 
1625 static void print_substream_info(struct snd_info_buffer *buffer,
1626                  struct loopback *loopback,
1627                  int sub,
1628                  int num)
1629 {
1630     struct loopback_cable *cable = loopback->cables[sub][num];
1631 
1632     snd_iprintf(buffer, "Cable %i substream %i:\n", num, sub);
1633     if (cable == NULL) {
1634         snd_iprintf(buffer, "  inactive\n");
1635         return;
1636     }
1637     snd_iprintf(buffer, "  valid: %u\n", cable->valid);
1638     snd_iprintf(buffer, "  running: %u\n", cable->running);
1639     snd_iprintf(buffer, "  pause: %u\n", cable->pause);
1640     print_dpcm_info(buffer, cable->streams[0], "Playback");
1641     print_dpcm_info(buffer, cable->streams[1], "Capture");
1642 }
1643 
1644 static void print_cable_info(struct snd_info_entry *entry,
1645                  struct snd_info_buffer *buffer)
1646 {
1647     struct loopback *loopback = entry->private_data;
1648     int sub, num;
1649 
1650     mutex_lock(&loopback->cable_lock);
1651     num = entry->name[strlen(entry->name)-1];
1652     num = num == '0' ? 0 : 1;
1653     for (sub = 0; sub < MAX_PCM_SUBSTREAMS; sub++)
1654         print_substream_info(buffer, loopback, sub, num);
1655     mutex_unlock(&loopback->cable_lock);
1656 }
1657 
1658 static int loopback_cable_proc_new(struct loopback *loopback, int cidx)
1659 {
1660     char name[32];
1661 
1662     snprintf(name, sizeof(name), "cable#%d", cidx);
1663     return snd_card_ro_proc_new(loopback->card, name, loopback,
1664                     print_cable_info);
1665 }
1666 
1667 static void loopback_set_timer_source(struct loopback *loopback,
1668                       const char *value)
1669 {
1670     if (loopback->timer_source) {
1671         devm_kfree(loopback->card->dev, loopback->timer_source);
1672         loopback->timer_source = NULL;
1673     }
1674     if (value && *value)
1675         loopback->timer_source = devm_kstrdup(loopback->card->dev,
1676                               value, GFP_KERNEL);
1677 }
1678 
1679 static void print_timer_source_info(struct snd_info_entry *entry,
1680                     struct snd_info_buffer *buffer)
1681 {
1682     struct loopback *loopback = entry->private_data;
1683 
1684     mutex_lock(&loopback->cable_lock);
1685     snd_iprintf(buffer, "%s\n",
1686             loopback->timer_source ? loopback->timer_source : "");
1687     mutex_unlock(&loopback->cable_lock);
1688 }
1689 
1690 static void change_timer_source_info(struct snd_info_entry *entry,
1691                      struct snd_info_buffer *buffer)
1692 {
1693     struct loopback *loopback = entry->private_data;
1694     char line[64];
1695 
1696     mutex_lock(&loopback->cable_lock);
1697     if (!snd_info_get_line(buffer, line, sizeof(line)))
1698         loopback_set_timer_source(loopback, strim(line));
1699     mutex_unlock(&loopback->cable_lock);
1700 }
1701 
1702 static int loopback_timer_source_proc_new(struct loopback *loopback)
1703 {
1704     return snd_card_rw_proc_new(loopback->card, "timer_source", loopback,
1705                     print_timer_source_info,
1706                     change_timer_source_info);
1707 }
1708 
1709 static int loopback_probe(struct platform_device *devptr)
1710 {
1711     struct snd_card *card;
1712     struct loopback *loopback;
1713     int dev = devptr->id;
1714     int err;
1715 
1716     err = snd_devm_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE,
1717                 sizeof(struct loopback), &card);
1718     if (err < 0)
1719         return err;
1720     loopback = card->private_data;
1721 
1722     if (pcm_substreams[dev] < 1)
1723         pcm_substreams[dev] = 1;
1724     if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS)
1725         pcm_substreams[dev] = MAX_PCM_SUBSTREAMS;
1726     
1727     loopback->card = card;
1728     loopback_set_timer_source(loopback, timer_source[dev]);
1729 
1730     mutex_init(&loopback->cable_lock);
1731 
1732     err = loopback_pcm_new(loopback, 0, pcm_substreams[dev]);
1733     if (err < 0)
1734         return err;
1735     err = loopback_pcm_new(loopback, 1, pcm_substreams[dev]);
1736     if (err < 0)
1737         return err;
1738     err = loopback_mixer_new(loopback, pcm_notify[dev] ? 1 : 0);
1739     if (err < 0)
1740         return err;
1741     loopback_cable_proc_new(loopback, 0);
1742     loopback_cable_proc_new(loopback, 1);
1743     loopback_timer_source_proc_new(loopback);
1744     strcpy(card->driver, "Loopback");
1745     strcpy(card->shortname, "Loopback");
1746     sprintf(card->longname, "Loopback %i", dev + 1);
1747     err = snd_card_register(card);
1748     if (err < 0)
1749         return err;
1750     platform_set_drvdata(devptr, card);
1751     return 0;
1752 }
1753 
1754 #ifdef CONFIG_PM_SLEEP
1755 static int loopback_suspend(struct device *pdev)
1756 {
1757     struct snd_card *card = dev_get_drvdata(pdev);
1758 
1759     snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1760     return 0;
1761 }
1762     
1763 static int loopback_resume(struct device *pdev)
1764 {
1765     struct snd_card *card = dev_get_drvdata(pdev);
1766 
1767     snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1768     return 0;
1769 }
1770 
1771 static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
1772 #define LOOPBACK_PM_OPS &loopback_pm
1773 #else
1774 #define LOOPBACK_PM_OPS NULL
1775 #endif
1776 
1777 #define SND_LOOPBACK_DRIVER "snd_aloop"
1778 
1779 static struct platform_driver loopback_driver = {
1780     .probe      = loopback_probe,
1781     .driver     = {
1782         .name   = SND_LOOPBACK_DRIVER,
1783         .pm = LOOPBACK_PM_OPS,
1784     },
1785 };
1786 
1787 static void loopback_unregister_all(void)
1788 {
1789     int i;
1790 
1791     for (i = 0; i < ARRAY_SIZE(devices); ++i)
1792         platform_device_unregister(devices[i]);
1793     platform_driver_unregister(&loopback_driver);
1794 }
1795 
1796 static int __init alsa_card_loopback_init(void)
1797 {
1798     int i, err, cards;
1799 
1800     err = platform_driver_register(&loopback_driver);
1801     if (err < 0)
1802         return err;
1803 
1804 
1805     cards = 0;
1806     for (i = 0; i < SNDRV_CARDS; i++) {
1807         struct platform_device *device;
1808         if (!enable[i])
1809             continue;
1810         device = platform_device_register_simple(SND_LOOPBACK_DRIVER,
1811                              i, NULL, 0);
1812         if (IS_ERR(device))
1813             continue;
1814         if (!platform_get_drvdata(device)) {
1815             platform_device_unregister(device);
1816             continue;
1817         }
1818         devices[i] = device;
1819         cards++;
1820     }
1821     if (!cards) {
1822 #ifdef MODULE
1823         printk(KERN_ERR "aloop: No loopback enabled\n");
1824 #endif
1825         loopback_unregister_all();
1826         return -ENODEV;
1827     }
1828     return 0;
1829 }
1830 
1831 static void __exit alsa_card_loopback_exit(void)
1832 {
1833     loopback_unregister_all();
1834 }
1835 
1836 module_init(alsa_card_loopback_init)
1837 module_exit(alsa_card_loopback_exit)