]> bbs.cooldavid.org Git - net-next-2.6.git/commitdiff
Merge branch 'topic/misc' into topic/pcsp-fix
authorTakashi Iwai <tiwai@suse.de>
Wed, 26 Nov 2008 13:12:42 +0000 (14:12 +0100)
committerTakashi Iwai <tiwai@suse.de>
Wed, 26 Nov 2008 13:12:42 +0000 (14:12 +0100)
Conflicts:
sound/drivers/pcsp/pcsp_lib.c

1  2 
include/sound/core.h
sound/drivers/pcsp/pcsp_lib.c

diff --combined include/sound/core.h
index 1508c4ec1ba929162fa14651426d27dc9ab81437,b2f3bbe9e56d7bc9d4a79243a0edd040e603117e..f632484bc7439171d1b682788305981b66e074f4
@@@ -43,6 -43,9 +43,6 @@@
  #ifdef CONFIG_PCI
  struct pci_dev;
  #endif
 -#ifdef CONFIG_SBUS
 -struct sbus_dev;
 -#endif
  
  /* device allocation stuff */
  
@@@ -353,7 -356,7 +353,7 @@@ void snd_verbose_printd(const char *fil
   * snd_printk - printk wrapper
   * @fmt: format string
   *
-  * Works like print() but prints the file and the line of the caller
+  * Works like printk() but prints the file and the line of the caller
   * when configured with CONFIG_SND_VERBOSE_PRINTK.
   */
  #define snd_printk(fmt, args...) \
        printk(fmt ,##args)
  #endif
  
+ /**
+  * snd_BUG - give a BUG warning message and stack trace
+  *
+  * Calls WARN() if CONFIG_SND_DEBUG is set.
+  * Ignored when CONFIG_SND_DEBUG is not set.
+  */
  #define snd_BUG()             WARN(1, "BUG?\n")
+ /**
+  * snd_BUG_ON - debugging check macro
+  * @cond: condition to evaluate
+  *
+  * When CONFIG_SND_DEBUG is set, this macro evaluates the given condition,
+  * and call WARN() and returns the value if it's non-zero.
+  * 
+  * When CONFIG_SND_DEBUG is not set, this just returns zero, and the given
+  * condition is ignored.
+  *
+  * NOTE: the argument won't be evaluated at all when CONFIG_SND_DEBUG=n.
+  * Thus, don't put any statement that influences on the code behavior,
+  * such as pre/post increment, to the argument of this macro.
+  * If you want to evaluate and give a warning, use standard WARN_ON().
+  */
  #define snd_BUG_ON(cond)      WARN((cond), "BUG? (%s)\n", __stringify(cond))
  
  #else /* !CONFIG_SND_DEBUG */
  
  #define snd_printd(fmt, args...)      do { } while (0)
  #define snd_BUG()                     do { } while (0)
- static inline int __snd_bug_on(void)
+ static inline int __snd_bug_on(int cond)
  {
        return 0;
  }
- #define snd_BUG_ON(cond)              __snd_bug_on()  /* always false */
+ #define snd_BUG_ON(cond)      __snd_bug_on(0 && (cond))  /* always false */
  
  #endif /* CONFIG_SND_DEBUG */
  
index 1f42e406311835a5c98aae272d9969ec6d5aa61b,40f95f549d2bb5739a7e777e1b27c3acdda3a3f0..f8d8470861dac9fa99dc4f7a4db179dd20e9a962
@@@ -8,6 -8,7 +8,7 @@@
  
  #include <linux/module.h>
  #include <linux/moduleparam.h>
+ #include <linux/interrupt.h>
  #include <sound/pcm.h>
  #include <asm/io.h>
  #include "pcsp.h"
@@@ -19,6 -20,22 +20,22 @@@ MODULE_PARM_DESC(nforce_wa, "Apply NFor
  
  #define DMIX_WANTS_S16        1
  
+ /*
+  * Call snd_pcm_period_elapsed in a tasklet
+  * This avoids spinlock messes and long-running irq contexts
+  */
+ static void pcsp_call_pcm_elapsed(unsigned long priv)
+ {
+       if (atomic_read(&pcsp_chip.timer_active)) {
+               struct snd_pcm_substream *substream;
+               substream = pcsp_chip.playback_substream;
+               if (substream)
+                       snd_pcm_period_elapsed(substream);
+       }
+ }
+ static DECLARE_TASKLET(pcsp_pcm_tasklet, pcsp_call_pcm_elapsed, 0);
  enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
  {
        unsigned char timer_cnt, val;
        struct snd_pcm_substream *substream;
        struct snd_pcm_runtime *runtime;
        struct snd_pcsp *chip = container_of(handle, struct snd_pcsp, timer);
+       unsigned long flags;
  
        if (chip->thalf) {
                outb(chip->val61, 0x61);
                chip->thalf = 0;
                if (!atomic_read(&chip->timer_active))
-                       return HRTIMER_NORESTART;
+                       goto stop;
 -              hrtimer_forward(&chip->timer, chip->timer.expires,
 +              hrtimer_forward(&chip->timer, hrtimer_get_expires(&chip->timer),
                                ktime_set(0, chip->ns_rem));
                return HRTIMER_RESTART;
        }
  
-       spin_lock_irq(&chip->substream_lock);
-       /* Takashi Iwai says regarding this extra lock:
-       If the irq handler handles some data on the DMA buffer, it should
-       do snd_pcm_stream_lock().
-       That protects basically against all races among PCM callbacks, yes.
-       However, there are two remaining issues:
-       1. The substream pointer you try to lock isn't protected _before_
-         this lock yet.
-       2. snd_pcm_period_elapsed() itself acquires the lock.
-       The requirement of another lock is because of 1.  When you get
-       chip->playback_substream, it's not protected.
-       Keeping this lock while snd_pcm_period_elapsed() assures the substream
-       is still protected (at least, not released).  And the other status is
-       handled properly inside snd_pcm_stream_lock() in
-       snd_pcm_period_elapsed().
-       */
-       if (!chip->playback_substream)
-               goto exit_nr_unlock1;
-       substream = chip->playback_substream;
-       snd_pcm_stream_lock(substream);
        if (!atomic_read(&chip->timer_active))
-               goto exit_nr_unlock2;
+               goto stop;
+       substream = chip->playback_substream;
+       if (!substream)
+               goto stop;
  
        runtime = substream->runtime;
        fmt_size = snd_pcm_format_physical_width(runtime->format) >> 3;
@@@ -87,6 -86,8 +86,8 @@@
  
        period_bytes = snd_pcm_lib_period_bytes(substream);
        buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
+       spin_lock_irqsave(&chip->substream_lock, flags);
        chip->playback_ptr += PCSP_INDEX_INC() * fmt_size;
        periods_elapsed = chip->playback_ptr - chip->period_ptr;
        if (periods_elapsed < 0) {
         * or ALSA will BUG on us. */
        chip->playback_ptr %= buffer_bytes;
  
-       snd_pcm_stream_unlock(substream);
        if (periods_elapsed) {
-               snd_pcm_period_elapsed(substream);
                chip->period_ptr += periods_elapsed * period_bytes;
                chip->period_ptr %= buffer_bytes;
+               tasklet_schedule(&pcsp_pcm_tasklet);
        }
-       spin_unlock_irq(&chip->substream_lock);
+       spin_unlock_irqrestore(&chip->substream_lock, flags);
  
        if (!atomic_read(&chip->timer_active))
-               return HRTIMER_NORESTART;
+               goto stop;
  
        chip->ns_rem = PCSP_PERIOD_NS();
        ns = (chip->thalf ? PCSP_CALC_NS(timer_cnt) : chip->ns_rem);
        chip->ns_rem -= ns;
 -      hrtimer_forward(&chip->timer, chip->timer.expires, ktime_set(0, ns));
 +      hrtimer_forward(&chip->timer, hrtimer_get_expires(&chip->timer),
 +                                                      ktime_set(0, ns));
        return HRTIMER_RESTART;
  
- exit_nr_unlock2:
-       snd_pcm_stream_unlock(substream);
- exit_nr_unlock1:
-       spin_unlock_irq(&chip->substream_lock);
+  stop:
        return HRTIMER_NORESTART;
  }
  
@@@ -165,26 -159,35 +160,35 @@@ static void pcsp_stop_playing(struct sn
        spin_unlock(&i8253_lock);
  }
  
+ /*
+  * Force to stop and sync the stream
+  */
+ void pcsp_sync_stop(struct snd_pcsp *chip)
+ {
+       local_irq_disable();
+       pcsp_stop_playing(chip);
+       local_irq_enable();
+       hrtimer_cancel(&chip->timer);
+       tasklet_kill(&pcsp_pcm_tasklet);
+ }
  static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
  {
        struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  #if PCSP_DEBUG
        printk(KERN_INFO "PCSP: close called\n");
  #endif
-       if (atomic_read(&chip->timer_active)) {
-               printk(KERN_ERR "PCSP: timer still active\n");
-               pcsp_stop_playing(chip);
-       }
-       spin_lock_irq(&chip->substream_lock);
+       pcsp_sync_stop(chip);
        chip->playback_substream = NULL;
-       spin_unlock_irq(&chip->substream_lock);
        return 0;
  }
  
  static int snd_pcsp_playback_hw_params(struct snd_pcm_substream *substream,
                                       struct snd_pcm_hw_params *hw_params)
  {
+       struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
        int err;
+       pcsp_sync_stop(chip);
        err = snd_pcm_lib_malloc_pages(substream,
                                      params_buffer_bytes(hw_params));
        if (err < 0)
  
  static int snd_pcsp_playback_hw_free(struct snd_pcm_substream *substream)
  {
+       struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
  #if PCSP_DEBUG
        printk(KERN_INFO "PCSP: hw_free called\n");
  #endif
+       pcsp_sync_stop(chip);
        return snd_pcm_lib_free_pages(substream);
  }
  
@@@ -212,6 -217,7 +218,7 @@@ static int snd_pcsp_playback_prepare(st
                        snd_pcm_lib_period_bytes(substream),
                        substream->runtime->periods);
  #endif
+       pcsp_sync_stop(chip);
        chip->playback_ptr = 0;
        chip->period_ptr = 0;
        return 0;
@@@ -242,7 -248,11 +249,11 @@@ static snd_pcm_uframes_t snd_pcsp_playb
                                                   *substream)
  {
        struct snd_pcsp *chip = snd_pcm_substream_chip(substream);
-       return bytes_to_frames(substream->runtime, chip->playback_ptr);
+       unsigned int pos;
+       spin_lock(&chip->substream_lock);
+       pos = chip->playback_ptr;
+       spin_unlock(&chip->substream_lock);
+       return bytes_to_frames(substream->runtime, pos);
  }
  
  static struct snd_pcm_hardware snd_pcsp_playback = {
@@@ -279,9 -289,7 +290,7 @@@ static int snd_pcsp_playback_open(struc
                return -EBUSY;
        }
        runtime->hw = snd_pcsp_playback;
-       spin_lock_irq(&chip->substream_lock);
        chip->playback_substream = substream;
-       spin_unlock_irq(&chip->substream_lock);
        return 0;
  }