ALSA: platform: Check CONFIG_PM_SLEEP instead of CONFIG_PM
[linux-3.10.git] / sound / drivers / aloop.c
index 66786ea..5a34355 100644 (file)
@@ -34,7 +34,7 @@
 #include <linux/slab.h>
 #include <linux/time.h>
 #include <linux/wait.h>
-#include <linux/moduleparam.h>
+#include <linux/module.h>
 #include <linux/platform_device.h>
 #include <sound/core.h>
 #include <sound/control.h>
@@ -51,7 +51,7 @@ MODULE_SUPPORTED_DEVICE("{{ALSA,Loopback soundcard}}");
 
 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;     /* Index 0-MAX */
 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;      /* ID for this card */
-static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
+static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
 static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8};
 static int pcm_notify[SNDRV_CARDS];
 
@@ -77,6 +77,7 @@ struct loopback_cable {
        /* flags */
        unsigned int valid;
        unsigned int running;
+       unsigned int pause;
 };
 
 struct loopback_setup {
@@ -116,6 +117,7 @@ struct loopback_pcm {
        /* timer stuff */
        unsigned int irq_pos;           /* fractional IRQ position */
        unsigned int period_size_frac;
+       unsigned int last_drift;
        unsigned long last_jiffies;
        struct timer_list timer;
 };
@@ -254,7 +256,7 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
        struct snd_pcm_runtime *runtime = substream->runtime;
        struct loopback_pcm *dpcm = runtime->private_data;
        struct loopback_cable *cable = dpcm->cable;
-       int err;
+       int err, stream = 1 << substream->stream;
 
        switch (cmd) {
        case SNDRV_PCM_TRIGGER_START:
@@ -263,17 +265,37 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
                        return err;
                dpcm->last_jiffies = jiffies;
                dpcm->pcm_rate_shift = 0;
+               dpcm->last_drift = 0;
+               spin_lock(&cable->lock);        
+               cable->running |= stream;
+               cable->pause &= ~stream;
+               spin_unlock(&cable->lock);
                loopback_timer_start(dpcm);
-               cable->running |= (1 << substream->stream);
                if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
                        loopback_active_notify(dpcm);
                break;
        case SNDRV_PCM_TRIGGER_STOP:
-               cable->running &= ~(1 << substream->stream);
+               spin_lock(&cable->lock);        
+               cable->running &= ~stream;
+               cable->pause &= ~stream;
+               spin_unlock(&cable->lock);
                loopback_timer_stop(dpcm);
                if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
                        loopback_active_notify(dpcm);
                break;
+       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+               spin_lock(&cable->lock);        
+               cable->pause |= stream;
+               spin_unlock(&cable->lock);
+               loopback_timer_stop(dpcm);
+               break;
+       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+               spin_lock(&cable->lock);
+               dpcm->last_jiffies = jiffies;
+               cable->pause &= ~stream;
+               spin_unlock(&cable->lock);
+               loopback_timer_start(dpcm);
+               break;
        default:
                return -EINVAL;
        }
@@ -424,93 +446,100 @@ static void copy_play_buf(struct loopback_pcm *play,
        }
 }
 
-#define BYTEPOS_UPDATE_POSONLY 0
-#define BYTEPOS_UPDATE_CLEAR   1
-#define BYTEPOS_UPDATE_COPY    2
-
-static void loopback_bytepos_update(struct loopback_pcm *dpcm,
-                                   unsigned int delta,
-                                   unsigned int cmd)
+static inline unsigned int bytepos_delta(struct loopback_pcm *dpcm,
+                                        unsigned int jiffies_delta)
 {
-       unsigned int count;
        unsigned long last_pos;
+       unsigned int delta;
 
        last_pos = byte_pos(dpcm, dpcm->irq_pos);
-       dpcm->irq_pos += delta * dpcm->pcm_bps;
-       count = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
-       if (!count)
-               return;
-       if (cmd == BYTEPOS_UPDATE_CLEAR)
-               clear_capture_buf(dpcm, count);
-       else if (cmd == BYTEPOS_UPDATE_COPY)
-               copy_play_buf(dpcm->cable->streams[SNDRV_PCM_STREAM_PLAYBACK],
-                             dpcm->cable->streams[SNDRV_PCM_STREAM_CAPTURE],
-                             count);
-       dpcm->buf_pos += count;
-       dpcm->buf_pos %= dpcm->pcm_buffer_size;
+       dpcm->irq_pos += jiffies_delta * dpcm->pcm_bps;
+       delta = byte_pos(dpcm, dpcm->irq_pos) - last_pos;
+       if (delta >= dpcm->last_drift)
+               delta -= dpcm->last_drift;
+       dpcm->last_drift = 0;
        if (dpcm->irq_pos >= dpcm->period_size_frac) {
                dpcm->irq_pos %= dpcm->period_size_frac;
                dpcm->period_update_pending = 1;
        }
+       return delta;
+}
+
+static inline void bytepos_finish(struct loopback_pcm *dpcm,
+                                 unsigned int delta)
+{
+       dpcm->buf_pos += delta;
+       dpcm->buf_pos %= dpcm->pcm_buffer_size;
 }
 
-static void loopback_pos_update(struct loopback_cable *cable)
+static unsigned int loopback_pos_update(struct loopback_cable *cable)
 {
        struct loopback_pcm *dpcm_play =
                        cable->streams[SNDRV_PCM_STREAM_PLAYBACK];
        struct loopback_pcm *dpcm_capt =
                        cable->streams[SNDRV_PCM_STREAM_CAPTURE];
        unsigned long delta_play = 0, delta_capt = 0;
+       unsigned int running, count1, count2;
+       unsigned long flags;
 
-       spin_lock(&cable->lock);        
-       if (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
+       spin_lock_irqsave(&cable->lock, flags);
+       running = cable->running ^ cable->pause;
+       if (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) {
                delta_play = jiffies - dpcm_play->last_jiffies;
                dpcm_play->last_jiffies += delta_play;
        }
 
-       if (cable->running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
+       if (running & (1 << SNDRV_PCM_STREAM_CAPTURE)) {
                delta_capt = jiffies - dpcm_capt->last_jiffies;
                dpcm_capt->last_jiffies += delta_capt;
        }
 
-       if (delta_play == 0 && delta_capt == 0) {
-               spin_unlock(&cable->lock);
-               return;
-       }
+       if (delta_play == 0 && delta_capt == 0)
+               goto unlock;
                
        if (delta_play > delta_capt) {
-               loopback_bytepos_update(dpcm_play, delta_play - delta_capt,
-                                       BYTEPOS_UPDATE_POSONLY);
+               count1 = bytepos_delta(dpcm_play, delta_play - delta_capt);
+               bytepos_finish(dpcm_play, count1);
                delta_play = delta_capt;
        } else if (delta_play < delta_capt) {
-               loopback_bytepos_update(dpcm_capt, delta_capt - delta_play,
-                                       BYTEPOS_UPDATE_CLEAR);
+               count1 = bytepos_delta(dpcm_capt, delta_capt - delta_play);
+               clear_capture_buf(dpcm_capt, count1);
+               bytepos_finish(dpcm_capt, count1);
                delta_capt = delta_play;
        }
 
-       if (delta_play == 0 && delta_capt == 0) {
-               spin_unlock(&cable->lock);
-               return;
-       }
+       if (delta_play == 0 && delta_capt == 0)
+               goto unlock;
+
        /* note delta_capt == delta_play at this moment */
-       loopback_bytepos_update(dpcm_capt, delta_capt, BYTEPOS_UPDATE_COPY);
-       loopback_bytepos_update(dpcm_play, delta_play, BYTEPOS_UPDATE_POSONLY);
-       spin_unlock(&cable->lock);
+       count1 = bytepos_delta(dpcm_play, delta_play);
+       count2 = bytepos_delta(dpcm_capt, delta_capt);
+       if (count1 < count2) {
+               dpcm_capt->last_drift = count2 - count1;
+               count1 = count2;
+       } else if (count1 > count2) {
+               dpcm_play->last_drift = count1 - count2;
+       }
+       copy_play_buf(dpcm_play, dpcm_capt, count1);
+       bytepos_finish(dpcm_play, count1);
+       bytepos_finish(dpcm_capt, count1);
+ unlock:
+       spin_unlock_irqrestore(&cable->lock, flags);
+       return running;
 }
 
 static void loopback_timer_function(unsigned long data)
 {
        struct loopback_pcm *dpcm = (struct loopback_pcm *)data;
-       int stream;
+       unsigned int running;
 
-       loopback_pos_update(dpcm->cable);
-       stream = dpcm->substream->stream;
-       if (dpcm->cable->running & (1 << stream))
+       running = loopback_pos_update(dpcm->cable);
+       if (running & (1 << dpcm->substream->stream)) {
                loopback_timer_start(dpcm);
-       if (dpcm->period_update_pending) {
-               dpcm->period_update_pending = 0;
-               if (dpcm->cable->running & (1 << stream))
+               if (dpcm->period_update_pending) {
+                       dpcm->period_update_pending = 0;
                        snd_pcm_period_elapsed(dpcm->substream);
+               }
        }
 }
 
@@ -526,7 +555,7 @@ static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream)
 static struct snd_pcm_hardware loopback_pcm_hardware =
 {
        .info =         (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP |
-                        SNDRV_PCM_INFO_MMAP_VALID),
+                        SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
        .formats =      (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
                         SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |
                         SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE),
@@ -554,7 +583,8 @@ static void loopback_runtime_free(struct snd_pcm_runtime *runtime)
 static int loopback_hw_params(struct snd_pcm_substream *substream,
                              struct snd_pcm_hw_params *params)
 {
-       return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
+       return snd_pcm_lib_alloc_vmalloc_buffer(substream,
+                                               params_buffer_bytes(params));
 }
 
 static int loopback_hw_free(struct snd_pcm_substream *substream)
@@ -566,7 +596,7 @@ static int loopback_hw_free(struct snd_pcm_substream *substream)
        mutex_lock(&dpcm->loopback->cable_lock);
        cable->valid &= ~(1 << substream->stream);
        mutex_unlock(&dpcm->loopback->cable_lock);
-       return snd_pcm_lib_free_pages(substream);
+       return snd_pcm_lib_free_vmalloc_buffer(substream);
 }
 
 static unsigned int get_cable_index(struct snd_pcm_substream *substream)
@@ -719,6 +749,8 @@ static struct snd_pcm_ops loopback_playback_ops = {
        .prepare =      loopback_prepare,
        .trigger =      loopback_trigger,
        .pointer =      loopback_pointer,
+       .page =         snd_pcm_lib_get_vmalloc_page,
+       .mmap =         snd_pcm_lib_mmap_vmalloc,
 };
 
 static struct snd_pcm_ops loopback_capture_ops = {
@@ -730,6 +762,8 @@ static struct snd_pcm_ops loopback_capture_ops = {
        .prepare =      loopback_prepare,
        .trigger =      loopback_trigger,
        .pointer =      loopback_pointer,
+       .page =         snd_pcm_lib_get_vmalloc_page,
+       .mmap =         snd_pcm_lib_mmap_vmalloc,
 };
 
 static int __devinit loopback_pcm_new(struct loopback *loopback,
@@ -750,10 +784,6 @@ static int __devinit loopback_pcm_new(struct loopback *loopback,
        strcpy(pcm->name, "Loopback PCM");
 
        loopback->pcm[device] = pcm;
-
-       snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
-                       snd_dma_continuous_data(GFP_KERNEL),
-                       0, 2 * 1024 * 1024);
        return 0;
 }
 
@@ -1054,6 +1084,7 @@ static void print_substream_info(struct snd_info_buffer *buffer,
        }
        snd_iprintf(buffer, "  valid: %u\n", cable->valid);
        snd_iprintf(buffer, "  running: %u\n", cable->running);
+       snd_iprintf(buffer, "  pause: %u\n", cable->pause);
        print_dpcm_info(buffer, cable->streams[0], "Playback");
        print_dpcm_info(buffer, cable->streams[1], "Capture");
 }
@@ -1145,11 +1176,10 @@ static int __devexit loopback_remove(struct platform_device *devptr)
        return 0;
 }
 
-#ifdef CONFIG_PM
-static int loopback_suspend(struct platform_device *pdev,
-                               pm_message_t state)
+#ifdef CONFIG_PM_SLEEP
+static int loopback_suspend(struct device *pdev)
 {
-       struct snd_card *card = platform_get_drvdata(pdev);
+       struct snd_card *card = dev_get_drvdata(pdev);
        struct loopback *loopback = card->private_data;
 
        snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
@@ -1159,13 +1189,18 @@ static int loopback_suspend(struct platform_device *pdev,
        return 0;
 }
        
-static int loopback_resume(struct platform_device *pdev)
+static int loopback_resume(struct device *pdev)
 {
-       struct snd_card *card = platform_get_drvdata(pdev);
+       struct snd_card *card = dev_get_drvdata(pdev);
 
        snd_power_change_state(card, SNDRV_CTL_POWER_D0);
        return 0;
 }
+
+static SIMPLE_DEV_PM_OPS(loopback_pm, loopback_suspend, loopback_resume);
+#define LOOPBACK_PM_OPS        &loopback_pm
+#else
+#define LOOPBACK_PM_OPS        NULL
 #endif
 
 #define SND_LOOPBACK_DRIVER    "snd_aloop"
@@ -1173,12 +1208,10 @@ static int loopback_resume(struct platform_device *pdev)
 static struct platform_driver loopback_driver = {
        .probe          = loopback_probe,
        .remove         = __devexit_p(loopback_remove),
-#ifdef CONFIG_PM
-       .suspend        = loopback_suspend,
-       .resume         = loopback_resume,
-#endif
        .driver         = {
-               .name   = SND_LOOPBACK_DRIVER
+               .name   = SND_LOOPBACK_DRIVER,
+               .owner  = THIS_MODULE,
+               .pm     = LOOPBACK_PM_OPS,
        },
 };