patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / oss / i810_audio.c
index cb693b0..e4f8542 100644 (file)
 #include <linux/spinlock.h>
 #include <linux/smp_lock.h>
 #include <linux/ac97_codec.h>
+#include <linux/bitops.h>
 #include <asm/uaccess.h>
 #include <asm/hardirq.h>
 
-#ifndef PCI_DEVICE_ID_INTEL_82801
-#define PCI_DEVICE_ID_INTEL_82801      0x2415
-#endif
-#ifndef PCI_DEVICE_ID_INTEL_82901
-#define PCI_DEVICE_ID_INTEL_82901      0x2425
-#endif
-#ifndef PCI_DEVICE_ID_INTEL_ICH2
-#define PCI_DEVICE_ID_INTEL_ICH2       0x2445
-#endif
-#ifndef PCI_DEVICE_ID_INTEL_ICH3
-#define PCI_DEVICE_ID_INTEL_ICH3       0x2485
-#endif
-#ifndef PCI_DEVICE_ID_INTEL_ICH4
-#define PCI_DEVICE_ID_INTEL_ICH4       0x24c5
-#endif
-#ifndef PCI_DEVICE_ID_INTEL_ICH5
-#define PCI_DEVICE_ID_INTEL_ICH5       0x24d5
-#endif
-#ifndef PCI_DEVICE_ID_INTEL_ICH6_3
-#define PCI_DEVICE_ID_INTEL_ICH6_3     0x266e
-#endif
-#ifndef PCI_DEVICE_ID_INTEL_440MX
-#define PCI_DEVICE_ID_INTEL_440MX      0x7195
-#endif
-#ifndef PCI_DEVICE_ID_INTEL_ESB_5
-#define PCI_DEVICE_ID_INTEL_ESB_5      0x25a6
-#endif
-#ifndef PCI_DEVICE_ID_SI_7012
-#define PCI_DEVICE_ID_SI_7012          0x7012
-#endif
-#ifndef PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO
-#define PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO        0x01b1
-#endif
-#ifndef PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO
-#define PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO        0x006a
-#endif
-#ifndef PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO
-#define PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO        0x00da
-#endif
-#ifndef PCI_DEVICE_ID_AMD_768_AUDIO
-#define PCI_DEVICE_ID_AMD_768_AUDIO    0x7445
-#endif
-#ifndef PCI_DEVICE_ID_AMD_8111_AC97
-#define PCI_DEVICE_ID_AMD_8111_AC97    0x746d
-#endif
+#define DRIVER_VERSION "1.01"
+
+#define MODULOP2(a, b) ((a) & ((b) - 1))
+#define MASKP2(a, b) ((a) & ~((b) - 1))
 
 static int ftsodell;
 static int strict_clocking;
@@ -209,6 +169,7 @@ struct i810_channel
 
 #define ENUM_ENGINE(PRE,DIG)                                                                   \
 enum {                                                                                         \
+       PRE##_BASE =    0x##DIG##0,             /* Base Address */                              \
        PRE##_BDBAR =   0x##DIG##0,             /* Buffer Descriptor list Base Address */       \
        PRE##_CIV =     0x##DIG##4,             /* Current Index Value */                       \
        PRE##_LVI =     0x##DIG##5,             /* Last Valid Index */                          \
@@ -256,8 +217,6 @@ enum {
 #define INT_GPI                (1<<0)
 #define INT_MASK (INT_SEC|INT_PRI|INT_MC|INT_PO|INT_PI|INT_MO|INT_NI|INT_GPI)
 
-#define DRIVER_VERSION "0.24"
-
 /* magic numbers to protect our data structures */
 #define I810_CARD_MAGIC                0x5072696E /* "Prin" */
 #define I810_STATE_MAGIC       0x63657373 /* "cess" */
@@ -323,19 +282,19 @@ static struct {
 };
 
 static struct pci_device_id i810_pci_tbl [] = {
-       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801,
+       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_5,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82801AA},
-       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82901,
+       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_5,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82901AB},
        {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_440MX,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTEL440MX},
-       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH2,
+       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_4,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH2},
-       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH3,
+       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_5,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH3},
-       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH4,
+       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_5,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
-       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH5,
+       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_5,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH5},
        {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7012,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, SI7012},
@@ -345,13 +304,13 @@ static struct pci_device_id i810_pci_tbl [] = {
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
        {PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, NVIDIA_NFORCE},
-       {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_768_AUDIO,
+       {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_OPUS_7445,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, AMD768},
-       {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_AC97,
+       {PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_AUDIO,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, AMD8111},
        {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_5,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
-       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_3,
+       {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_18,
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH4},
 
        {0,}
@@ -491,8 +450,12 @@ struct i810_card {
 /* extract register offset from codec struct */
 #define IO_REG_OFF(codec) (((struct i810_card *) codec->private_data)->ac97_id_map[codec->id])
 
+#define GET_CIV(port) MODULOP2(inb((port) + OFF_CIV), SG_LEN)
+#define GET_LVI(port) MODULOP2(inb((port) + OFF_LVI), SG_LEN)
+
 /* set LVI from CIV */
-#define CIV_TO_LVI(port, off) outb((inb(port+OFF_CIV)+off) & 31, port+OFF_LVI)
+#define CIV_TO_LVI(port, off) \
+       outb(MODULOP2(GET_CIV((port)) + (off), SG_LEN), (port) + OFF_LVI)
 
 static struct i810_card *devs = NULL;
 
@@ -762,7 +725,7 @@ static inline unsigned i810_get_dma_addr(struct i810_state *state, int rec)
                port_picb = port + OFF_PICB;
 
        do {
-               civ = inb(port+OFF_CIV) & 31;
+               civ = GET_CIV(port);
                offset = inw(port_picb);
                /* Must have a delay here! */ 
                if(offset == 0)
@@ -782,7 +745,7 @@ static inline unsigned i810_get_dma_addr(struct i810_state *state, int rec)
                 * that we won't have to worry about the chip still being
                 * out of sync with reality ;-)
                 */
-       } while (civ != (inb(port+OFF_CIV) & 31) || offset != inw(port_picb));
+       } while (civ != GET_CIV(port) || offset != inw(port_picb));
                 
        return (((civ + 1) * dmabuf->fragsize - (bytes * offset))
                % dmabuf->dmasize);
@@ -992,6 +955,7 @@ static int prog_dmabuf(struct i810_state *state, unsigned rec)
        dmabuf->numfrag = SG_LEN;
        dmabuf->fragsize = dmabuf->dmasize/dmabuf->numfrag;
        dmabuf->fragsamples = dmabuf->fragsize >> 1;
+       dmabuf->fragshift = ffs(dmabuf->fragsize) - 1;
        dmabuf->userfragsize = dmabuf->ossfragsize;
        dmabuf->userfrags = dmabuf->dmasize/dmabuf->ossfragsize;
 
@@ -999,16 +963,12 @@ static int prog_dmabuf(struct i810_state *state, unsigned rec)
 
        if(dmabuf->ossmaxfrags == 4) {
                fragint = 8;
-               dmabuf->fragshift = 2;
        } else if (dmabuf->ossmaxfrags == 8) {
                fragint = 4;
-               dmabuf->fragshift = 3;
        } else if (dmabuf->ossmaxfrags == 16) {
                fragint = 2;
-               dmabuf->fragshift = 4;
        } else {
                fragint = 1;
-               dmabuf->fragshift = 5;
        }
        /*
         *      Now set up the ring 
@@ -1072,41 +1032,41 @@ static void __i810_update_lvi(struct i810_state *state, int rec)
 {
        struct dmabuf *dmabuf = &state->dmabuf;
        int x, port;
-       
+       int trigger;
+       int count, fragsize;
+       void (*start)(struct i810_state *);
+
+       count = dmabuf->count;
        port = state->card->iobase;
-       if(rec)
+       if (rec) {
                port += dmabuf->read_channel->port;
-       else
+               trigger = PCM_ENABLE_INPUT;
+               start = __start_adc;
+               count = dmabuf->dmasize - count;
+       } else {
                port += dmabuf->write_channel->port;
+               trigger = PCM_ENABLE_OUTPUT;
+               start = __start_dac;
+       }
+
+       /* Do not process partial fragments. */
+       fragsize = dmabuf->fragsize;
+       if (count < fragsize)
+               return;
 
-       /* if we are currently stopped, then our CIV is actually set to our
-        * *last* sg segment and we are ready to wrap to the next.  However,
-        * if we set our LVI to the last sg segment, then it won't wrap to
-        * the next sg segment, it won't even get a start.  So, instead, when
-        * we are stopped, we set both the LVI value and also we increment
-        * the CIV value to the next sg segment to be played so that when
-        * we call start_{dac,adc}, things will operate properly
-        */
        if (!dmabuf->enable && dmabuf->ready) {
-               if(rec && dmabuf->count < dmabuf->dmasize &&
-                  (dmabuf->trigger & PCM_ENABLE_INPUT))
-               {
-                       CIV_TO_LVI(port, 1);
-                       __start_adc(state);
-                       while( !(inb(port + OFF_CR) & ((1<<4) | (1<<2))) ) ;
-               } else if (!rec && dmabuf->count &&
-                          (dmabuf->trigger & PCM_ENABLE_OUTPUT))
-               {
-                       CIV_TO_LVI(port, 1);
-                       __start_dac(state);
-                       while( !(inb(port + OFF_CR) & ((1<<4) | (1<<2))) ) ;
-               }
+               if (!(dmabuf->trigger & trigger))
+                       return;
+
+               start(state);
+               while (!(inb(port + OFF_CR) & ((1<<4) | (1<<2))))
+                       ;
        }
 
-       /* swptr - 1 is the tail of our transfer */
-       x = (dmabuf->dmasize + dmabuf->swptr - 1) % dmabuf->dmasize;
-       x /= dmabuf->fragsize;
-       outb(x, port+OFF_LVI);
+       /* MASKP2(swptr, fragsize) - 1 is the tail of our transfer */
+       x = MODULOP2(MASKP2(dmabuf->swptr, fragsize) - 1, dmabuf->dmasize);
+       x >>= dmabuf->fragshift;
+       outb(x, port + OFF_LVI);
 }
 
 static void i810_update_lvi(struct i810_state *state, int rec)
@@ -1126,13 +1086,17 @@ static void i810_update_ptr(struct i810_state *state)
 {
        struct dmabuf *dmabuf = &state->dmabuf;
        unsigned hwptr;
+       unsigned fragmask, dmamask;
        int diff;
 
-       /* error handling and process wake up for DAC */
+       fragmask = MASKP2(~0, dmabuf->fragsize);
+       dmamask = MODULOP2(~0, dmabuf->dmasize);
+
+       /* error handling and process wake up for ADC */
        if (dmabuf->enable == ADC_RUNNING) {
                /* update hardware pointer */
-               hwptr = i810_get_dma_addr(state, 1);
-               diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
+               hwptr = i810_get_dma_addr(state, 1) & fragmask;
+               diff = (hwptr - dmabuf->hwptr) & dmamask;
 #if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
                printk("ADC HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
 #endif
@@ -1144,20 +1108,20 @@ static void i810_update_ptr(struct i810_state *state)
                        /* this is normal for the end of a read */
                        /* only give an error if we went past the */
                        /* last valid sg entry */
-                       if((inb(state->card->iobase + PI_CIV) & 31) !=
-                          (inb(state->card->iobase + PI_LVI) & 31)) {
+                       if (GET_CIV(state->card->iobase + PI_BASE) !=
+                           GET_LVI(state->card->iobase + PI_BASE)) {
                                printk(KERN_WARNING "i810_audio: DMA overrun on read\n");
                                dmabuf->error++;
                        }
                }
-               if (dmabuf->count > dmabuf->userfragsize)
+               if (diff)
                        wake_up(&dmabuf->wait);
        }
        /* error handling and process wake up for DAC */
        if (dmabuf->enable == DAC_RUNNING) {
                /* update hardware pointer */
-               hwptr = i810_get_dma_addr(state, 0);
-               diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
+               hwptr = i810_get_dma_addr(state, 0) & fragmask;
+               diff = (hwptr - dmabuf->hwptr) & dmamask;
 #if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
                printk("DAC HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
 #endif
@@ -1169,18 +1133,18 @@ static void i810_update_ptr(struct i810_state *state)
                        /* this is normal for the end of a write */
                        /* only give an error if we went past the */
                        /* last valid sg entry */
-                       if((inb(state->card->iobase + PO_CIV) & 31) !=
-                          (inb(state->card->iobase + PO_LVI) & 31)) {
+                       if (GET_CIV(state->card->iobase + PO_BASE) !=
+                           GET_LVI(state->card->iobase + PO_BASE)) {
                                printk(KERN_WARNING "i810_audio: DMA overrun on write\n");
                                printk("i810_audio: CIV %d, LVI %d, hwptr %x, "
                                        "count %d\n",
-                                       inb(state->card->iobase + PO_CIV) & 31,
-                                       inb(state->card->iobase + PO_LVI) & 31,
+                                       GET_CIV(state->card->iobase + PO_BASE),
+                                       GET_LVI(state->card->iobase + PO_BASE),
                                        dmabuf->hwptr, dmabuf->count);
                                dmabuf->error++;
                        }
                }
-               if (dmabuf->count < (dmabuf->dmasize-dmabuf->userfragsize))
+               if (diff)
                        wake_up(&dmabuf->wait);
        }
 }
@@ -1197,7 +1161,6 @@ static inline int i810_get_free_write_space(struct i810_state *state)
                dmabuf->swptr = dmabuf->hwptr;
        }
        free = dmabuf->dmasize - dmabuf->count;
-       free -= (dmabuf->hwptr % dmabuf->fragsize);
        if(free < 0)
                return(0);
        return(free);
@@ -1215,12 +1178,27 @@ static inline int i810_get_available_read_data(struct i810_state *state)
                dmabuf->swptr = dmabuf->hwptr;
        }
        avail = dmabuf->count;
-       avail -= (dmabuf->hwptr % dmabuf->fragsize);
        if(avail < 0)
                return(0);
        return(avail);
 }
 
+static inline void fill_partial_frag(struct dmabuf *dmabuf)
+{
+       unsigned fragsize;
+       unsigned swptr, len;
+
+       fragsize = dmabuf->fragsize;
+       swptr = dmabuf->swptr;
+       len = fragsize - MODULOP2(dmabuf->swptr, fragsize);
+       if (len == fragsize)
+               return;
+
+       memset(dmabuf->rawbuf + swptr, '\0', len);
+       dmabuf->swptr = MODULOP2(swptr + len, dmabuf->dmasize);
+       dmabuf->count += len;
+}
+
 static int drain_dac(struct i810_state *state, int signals_allowed)
 {
        DECLARE_WAITQUEUE(wait, current);
@@ -1235,30 +1213,28 @@ static int drain_dac(struct i810_state *state, int signals_allowed)
                stop_dac(state);
                return 0;
        }
+
+       spin_lock_irqsave(&state->card->lock, flags);
+
+       fill_partial_frag(dmabuf);
+
+       /* 
+        * This will make sure that our LVI is correct, that our
+        * pointer is updated, and that the DAC is running.  We
+        * have to force the setting of dmabuf->trigger to avoid
+        * any possible deadlocks.
+        */
+       dmabuf->trigger = PCM_ENABLE_OUTPUT;
+       __i810_update_lvi(state, 0);
+
+       spin_unlock_irqrestore(&state->card->lock, flags);
+
        add_wait_queue(&dmabuf->wait, &wait);
        for (;;) {
 
                spin_lock_irqsave(&state->card->lock, flags);
                i810_update_ptr(state);
                count = dmabuf->count;
-               spin_unlock_irqrestore(&state->card->lock, flags);
-
-               if (count <= 0)
-                       break;
-
-               /* 
-                * This will make sure that our LVI is correct, that our
-                * pointer is updated, and that the DAC is running.  We
-                * have to force the setting of dmabuf->trigger to avoid
-                * any possible deadlocks.
-                */
-               if(!dmabuf->enable) {
-                       dmabuf->trigger = PCM_ENABLE_OUTPUT;
-                       i810_update_lvi(state,0);
-               }
-                if (signal_pending(current) && signals_allowed) {
-                        break;
-                }
 
                /* It seems that we have to set the current state to
                 * TASK_INTERRUPTIBLE every time to make the process
@@ -1269,7 +1245,17 @@ static int drain_dac(struct i810_state *state, int signals_allowed)
                 * instead of actually sleeping and waiting for an
                 * interrupt to wake us up!
                 */
-               set_current_state(TASK_INTERRUPTIBLE);
+               __set_current_state(signals_allowed ?
+                                   TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
+               spin_unlock_irqrestore(&state->card->lock, flags);
+
+               if (count <= 0)
+                       break;
+
+                if (signal_pending(current) && signals_allowed) {
+                        break;
+                }
+
                /*
                 * set the timeout to significantly longer than it *should*
                 * take for the DAC to drain the DMA buffer
@@ -1350,11 +1336,10 @@ static void i810_channel_interrupt(struct i810_card *card)
                        if(status & DMA_INT_DCH)
                                printk("DCH -");
 #endif
-                       if(dmabuf->enable & DAC_RUNNING)
-                               count = dmabuf->count;
-                       else
-                               count = dmabuf->dmasize - dmabuf->count;
-                       if(count > 0) {
+                       count = dmabuf->count;
+                       if(dmabuf->enable & ADC_RUNNING)
+                               count = dmabuf->dmasize - count;
+                       if (count >= (int)dmabuf->fragsize) {
                                outb(inb(port+OFF_CR) | 1, port+OFF_CR);
 #ifdef DEBUG_INTERRUPTS
                                printk(" CONTINUE ");
@@ -1408,7 +1393,7 @@ static irqreturn_t i810_interrupt(int irq, void *dev_id, struct pt_regs *regs)
    waiting to be copied to the user's buffer.  It is filled by the dma
    machine and drained by this loop. */
 
-static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+static ssize_t i810_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
 {
        struct i810_state *state = (struct i810_state *)file->private_data;
        struct i810_card *card=state ? state->card : 0;
@@ -1417,6 +1402,7 @@ static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *
        unsigned long flags;
        unsigned int swptr;
        int cnt;
+       int pending;
         DECLARE_WAITQUEUE(waita, current);
 
 #ifdef DEBUG2
@@ -1442,6 +1428,8 @@ static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *
                return -EFAULT;
        ret = 0;
 
+       pending = 0;
+
         add_wait_queue(&dmabuf->wait, &waita);
        while (count > 0) {
                set_current_state(TASK_INTERRUPTIBLE);
@@ -1455,8 +1443,8 @@ static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *
                         }
                         continue;
                 }
-               swptr = dmabuf->swptr;
                cnt = i810_get_available_read_data(state);
+               swptr = dmabuf->swptr;
                // this is to make the copy_to_user simpler below
                if(cnt > (dmabuf->dmasize - swptr))
                        cnt = dmabuf->dmasize - swptr;
@@ -1464,15 +1452,6 @@ static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *
 
                if (cnt > count)
                        cnt = count;
-               /* Lop off the last two bits to force the code to always
-                * write in full samples.  This keeps software that sets
-                * O_NONBLOCK but doesn't check the return value of the
-                * write call from getting things out of state where they
-                * think a full 4 byte sample was written when really only
-                * a portion was, resulting in odd sound and stereo
-                * hysteresis.
-                */
-               cnt &= ~0x3;
                if (cnt <= 0) {
                        unsigned long tmo;
                        /*
@@ -1526,7 +1505,7 @@ static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *
                        goto done;
                }
 
-               swptr = (swptr + cnt) % dmabuf->dmasize;
+               swptr = MODULOP2(swptr + cnt, dmabuf->dmasize);
 
                spin_lock_irqsave(&card->lock, flags);
 
@@ -1535,7 +1514,7 @@ static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *
                         continue;
                 }
                dmabuf->swptr = swptr;
-               dmabuf->count -= cnt;
+               pending = dmabuf->count -= cnt;
                spin_unlock_irqrestore(&card->lock, flags);
 
                count -= cnt;
@@ -1543,7 +1522,9 @@ static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *
                ret += cnt;
        }
  done:
-       i810_update_lvi(state,1);
+       pending = dmabuf->dmasize - pending;
+       if (dmabuf->enable || pending >= dmabuf->userfragsize)
+               i810_update_lvi(state, 1);
         set_current_state(TASK_RUNNING);
         remove_wait_queue(&dmabuf->wait, &waita);
 
@@ -1552,7 +1533,7 @@ static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *
 
 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
    the soundcard.  it is drained by the dma machine and filled by this loop. */
-static ssize_t i810_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
+static ssize_t i810_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
 {
        struct i810_state *state = (struct i810_state *)file->private_data;
        struct i810_card *card=state ? state->card : 0;
@@ -1560,7 +1541,8 @@ static ssize_t i810_write(struct file *file, const char *buffer, size_t count, l
        ssize_t ret;
        unsigned long flags;
        unsigned int swptr = 0;
-       int cnt, x;
+       int pending;
+       int cnt;
         DECLARE_WAITQUEUE(waita, current);
 
 #ifdef DEBUG2
@@ -1585,6 +1567,8 @@ static ssize_t i810_write(struct file *file, const char *buffer, size_t count, l
                return -EFAULT;
        ret = 0;
 
+       pending = 0;
+
         add_wait_queue(&dmabuf->wait, &waita);
        while (count > 0) {
                set_current_state(TASK_INTERRUPTIBLE);
@@ -1599,8 +1583,8 @@ static ssize_t i810_write(struct file *file, const char *buffer, size_t count, l
                         continue;
                 }
 
-               swptr = dmabuf->swptr;
                cnt = i810_get_free_write_space(state);
+               swptr = dmabuf->swptr;
                /* Bound the maximum size to how much we can copy to the
                 * dma buffer before we hit the end.  If we have more to
                 * copy then it will get done in a second pass of this
@@ -1615,15 +1599,6 @@ static ssize_t i810_write(struct file *file, const char *buffer, size_t count, l
 #endif
                if (cnt > count)
                        cnt = count;
-               /* Lop off the last two bits to force the code to always
-                * write in full samples.  This keeps software that sets
-                * O_NONBLOCK but doesn't check the return value of the
-                * write call from getting things out of state where they
-                * think a full 4 byte sample was written when really only
-                * a portion was, resulting in odd sound and stereo
-                * hysteresis.
-                */
-               cnt &= ~0x3;
                if (cnt <= 0) {
                        unsigned long tmo;
                        // There is data waiting to be played
@@ -1668,7 +1643,7 @@ static ssize_t i810_write(struct file *file, const char *buffer, size_t count, l
                        goto ret;
                }
 
-               swptr = (swptr + cnt) % dmabuf->dmasize;
+               swptr = MODULOP2(swptr + cnt, dmabuf->dmasize);
 
                spin_lock_irqsave(&state->card->lock, flags);
                 if (PM_SUSPENDED(card)) {
@@ -1677,19 +1652,16 @@ static ssize_t i810_write(struct file *file, const char *buffer, size_t count, l
                 }
 
                dmabuf->swptr = swptr;
-               dmabuf->count += cnt;
+               pending = dmabuf->count += cnt;
 
                count -= cnt;
                buffer += cnt;
                ret += cnt;
                spin_unlock_irqrestore(&state->card->lock, flags);
        }
-       if (swptr % dmabuf->fragsize) {
-               x = dmabuf->fragsize - (swptr % dmabuf->fragsize);
-               memset(dmabuf->rawbuf + swptr, '\0', x);
-       }
 ret:
-       i810_update_lvi(state,0);
+       if (dmabuf->enable || pending >= dmabuf->userfragsize)
+               i810_update_lvi(state, 0);
         set_current_state(TASK_RUNNING);
         remove_wait_queue(&dmabuf->wait, &waita);
 
@@ -1783,9 +1755,11 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
        unsigned int i_glob_cnt;
        int val = 0, ret;
        struct ac97_codec *codec = state->card->ac97_codec[0];
+       void __user *argp = (void __user *)arg;
+       int __user *p = argp;
 
 #ifdef DEBUG
-       printk("i810_audio: i810_ioctl, arg=0x%x, cmd=", arg ? *(int *)arg : 0);
+       printk("i810_audio: i810_ioctl, arg=0x%x, cmd=", arg ? *p : 0);
 #endif
 
        switch (cmd) 
@@ -1794,7 +1768,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 #ifdef DEBUG
                printk("OSS_GETVERSION\n");
 #endif
-               return put_user(SOUND_VERSION, (int *)arg);
+               return put_user(SOUND_VERSION, p);
 
        case SNDCTL_DSP_RESET:
 #ifdef DEBUG
@@ -1841,7 +1815,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 #ifdef DEBUG
                printk("SNDCTL_DSP_SPEED\n");
 #endif
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                if (val >= 0) {
                        if (file->f_mode & FMODE_WRITE) {
@@ -1879,7 +1853,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                                spin_unlock_irqrestore(&state->card->lock, flags);
                        }
                }
-               return put_user(dmabuf->rate, (int *)arg);
+               return put_user(dmabuf->rate, p);
 
        case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
 #ifdef DEBUG
@@ -1891,7 +1865,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                if (dmabuf->enable & ADC_RUNNING) {
                        stop_adc(state);
                }
-               return put_user(1, (int *)arg);
+               return put_user(1, p);
 
        case SNDCTL_DSP_GETBLKSIZE:
                if (file->f_mode & FMODE_WRITE) {
@@ -1905,25 +1879,25 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 #ifdef DEBUG
                printk("SNDCTL_DSP_GETBLKSIZE %d\n", dmabuf->userfragsize);
 #endif
-               return put_user(dmabuf->userfragsize, (int *)arg);
+               return put_user(dmabuf->userfragsize, p);
 
        case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
 #ifdef DEBUG
                printk("SNDCTL_DSP_GETFMTS\n");
 #endif
-               return put_user(AFMT_S16_LE, (int *)arg);
+               return put_user(AFMT_S16_LE, p);
 
        case SNDCTL_DSP_SETFMT: /* Select sample format */
 #ifdef DEBUG
                printk("SNDCTL_DSP_SETFMT\n");
 #endif
-               return put_user(AFMT_S16_LE, (int *)arg);
+               return put_user(AFMT_S16_LE, p);
 
        case SNDCTL_DSP_CHANNELS:
 #ifdef DEBUG
                printk("SNDCTL_DSP_CHANNELS\n");
 #endif
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
 
                if (val > 0) {
@@ -1934,13 +1908,13 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                                stop_adc(state);
                        }
                } else {
-                       return put_user(state->card->channels, (int *)arg);
+                       return put_user(state->card->channels, p);
                }
 
                /* ICH and ICH0 only support 2 channels */
-               if ( state->card->pci_id == PCI_DEVICE_ID_INTEL_82801 
-                    || state->card->pci_id == PCI_DEVICE_ID_INTEL_82901
-                       return put_user(2, (int *)arg);
+               if ( state->card->pci_id == PCI_DEVICE_ID_INTEL_82801AA_5
+                    || state->card->pci_id == PCI_DEVICE_ID_INTEL_82801AB_5
+                       return put_user(2, p);
        
                /* Multi-channel support was added with ICH2. Bits in */
                /* Global Status and Global Control register are now  */
@@ -1985,7 +1959,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                                break;
                }
 
-               return put_user(val, (int *)arg);
+               return put_user(val, p);
 
        case SNDCTL_DSP_POST: /* the user has sent all data and is notifying us */
                /* we update the swptr to the end of the last sg segment then return */
@@ -2004,7 +1978,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
        case SNDCTL_DSP_SUBDIVIDE:
                if (dmabuf->subdivision)
                        return -EINVAL;
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                if (val != 1 && val != 2 && val != 4)
                        return -EINVAL;
@@ -2016,7 +1990,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                return 0;
 
        case SNDCTL_DSP_SETFRAGMENT:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
 
                dmabuf->ossfragsize = 1<<(val & 0xffff);
@@ -2089,7 +2063,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                printk("SNDCTL_DSP_GETOSPACE %d, %d, %d, %d\n", abinfo.bytes,
                        abinfo.fragsize, abinfo.fragments, abinfo.fragstotal);
 #endif
-               return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
+               return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
 
        case SNDCTL_DSP_GETOPTR:
                if (!(file->f_mode & FMODE_WRITE))
@@ -2111,7 +2085,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                printk("SNDCTL_DSP_GETOPTR %d, %d, %d, %d\n", cinfo.bytes,
                        cinfo.blocks, cinfo.ptr, dmabuf->count);
 #endif
-               return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
+               return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
 
        case SNDCTL_DSP_GETISPACE:
                if (!(file->f_mode & FMODE_READ))
@@ -2128,7 +2102,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                printk("SNDCTL_DSP_GETISPACE %d, %d, %d, %d\n", abinfo.bytes,
                        abinfo.fragsize, abinfo.fragments, abinfo.fragstotal);
 #endif
-               return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
+               return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
 
        case SNDCTL_DSP_GETIPTR:
                if (!(file->f_mode & FMODE_READ))
@@ -2150,7 +2124,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                printk("SNDCTL_DSP_GETIPTR %d, %d, %d, %d\n", cinfo.bytes,
                        cinfo.blocks, cinfo.ptr, dmabuf->count);
 #endif
-               return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
+               return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
 
        case SNDCTL_DSP_NONBLOCK:
 #ifdef DEBUG
@@ -2164,21 +2138,28 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                printk("SNDCTL_DSP_GETCAPS\n");
 #endif
            return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP|DSP_CAP_BIND,
-                           (int *)arg);
+                           p);
 
        case SNDCTL_DSP_GETTRIGGER:
                val = 0;
 #ifdef DEBUG
                printk("SNDCTL_DSP_GETTRIGGER 0x%x\n", dmabuf->trigger);
 #endif
-               return put_user(dmabuf->trigger, (int *)arg);
+               return put_user(dmabuf->trigger, p);
 
        case SNDCTL_DSP_SETTRIGGER:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
 #if defined(DEBUG) || defined(DEBUG_MMAP)
                printk("SNDCTL_DSP_SETTRIGGER 0x%x\n", val);
 #endif
+               /* silently ignore invalid PCM_ENABLE_xxx bits,
+                * like the other drivers do
+                */
+               if (!(file->f_mode & FMODE_READ ))
+                       val &= ~PCM_ENABLE_INPUT;
+               if (!(file->f_mode & FMODE_WRITE ))
+                       val &= ~PCM_ENABLE_OUTPUT;
                if((file->f_mode & FMODE_READ) && !(val & PCM_ENABLE_INPUT) && dmabuf->enable == ADC_RUNNING) {
                        stop_adc(state);
                }
@@ -2186,7 +2167,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                        stop_dac(state);
                }
                dmabuf->trigger = val;
-               if((file->f_mode & FMODE_WRITE) && (val & PCM_ENABLE_OUTPUT) && !(dmabuf->enable & DAC_RUNNING)) {
+               if((val & PCM_ENABLE_OUTPUT) && !(dmabuf->enable & DAC_RUNNING)) {
                        if (!dmabuf->write_channel) {
                                dmabuf->ready = 0;
                                dmabuf->write_channel = state->card->alloc_pcm_channel(state->card);
@@ -2202,12 +2183,12 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                                dmabuf->swptr = dmabuf->hwptr;
                                dmabuf->count = i810_get_free_write_space(state);
                                dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
-                               __i810_update_lvi(state, 0);
                                spin_unlock_irqrestore(&state->card->lock, flags);
-                       } else
-                               start_dac(state);
+                       }
+                       i810_update_lvi(state, 0);
+                       start_dac(state);
                }
-               if((file->f_mode & FMODE_READ) && (val & PCM_ENABLE_INPUT) && !(dmabuf->enable & ADC_RUNNING)) {
+               if((val & PCM_ENABLE_INPUT) && !(dmabuf->enable & ADC_RUNNING)) {
                        if (!dmabuf->read_channel) {
                                dmabuf->ready = 0;
                                dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card);
@@ -2244,31 +2225,31 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
 #ifdef DEBUG
                printk("SNDCTL_DSP_GETODELAY %d\n", dmabuf->count);
 #endif
-               return put_user(val, (int *)arg);
+               return put_user(val, p);
 
        case SOUND_PCM_READ_RATE:
 #ifdef DEBUG
                printk("SOUND_PCM_READ_RATE %d\n", dmabuf->rate);
 #endif
-               return put_user(dmabuf->rate, (int *)arg);
+               return put_user(dmabuf->rate, p);
 
        case SOUND_PCM_READ_CHANNELS:
 #ifdef DEBUG
                printk("SOUND_PCM_READ_CHANNELS\n");
 #endif
-               return put_user(2, (int *)arg);
+               return put_user(2, p);
 
        case SOUND_PCM_READ_BITS:
 #ifdef DEBUG
                printk("SOUND_PCM_READ_BITS\n");
 #endif
-               return put_user(AFMT_S16_LE, (int *)arg);
+               return put_user(AFMT_S16_LE, p);
 
        case SNDCTL_DSP_SETSPDIF: /* Set S/PDIF Control register */
 #ifdef DEBUG
                printk("SNDCTL_DSP_SETSPDIF\n");
 #endif
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
 
                /* Check to make sure the codec supports S/PDIF transmitter */
@@ -2291,13 +2272,13 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                else 
                        printk(KERN_WARNING "i810_audio: S/PDIF transmitter not avalible.\n");
 #endif
-               return put_user(val, (int *)arg);
+               return put_user(val, p);
 
        case SNDCTL_DSP_GETSPDIF: /* Get S/PDIF Control register */
 #ifdef DEBUG
                printk("SNDCTL_DSP_GETSPDIF\n");
 #endif
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
 
                /* Check to make sure the codec supports S/PDIF transmitter */
@@ -2310,14 +2291,14 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                } else {
                        val = i810_ac97_get(codec, AC97_SPDIF_CONTROL);
                }
-               //return put_user((val & 0xcfff), (int *)arg);
-               return put_user(val, (int *)arg);
+               //return put_user((val & 0xcfff), p);
+               return put_user(val, p);
                        
        case SNDCTL_DSP_GETCHANNELMASK:
 #ifdef DEBUG
                printk("SNDCTL_DSP_GETCHANNELMASK\n");
 #endif
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                
                /* Based on AC'97 DAC support, not ICH hardware */
@@ -2330,13 +2311,13 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                if ( state->card->ac97_features & 0x0140 )
                        val |= DSP_BIND_CENTER_LFE;
 
-               return put_user(val, (int *)arg);
+               return put_user(val, p);
 
        case SNDCTL_DSP_BIND_CHANNEL:
 #ifdef DEBUG
                printk("SNDCTL_DSP_BIND_CHANNEL\n");
 #endif
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                if ( val == DSP_BIND_QUERY ) {
                        val = DSP_BIND_FRONT; /* Always report this as being enabled */
@@ -2404,7 +2385,7 @@ static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
                                        val &= ~DSP_BIND_CENTER_LFE;
                        }
                }
-               return put_user(val, (int *)arg);
+               return put_user(val, p);
                
        case SNDCTL_DSP_MAPINBUF:
        case SNDCTL_DSP_MAPOUTBUF:
@@ -2747,6 +2728,26 @@ static int i810_ac97_probe_and_powerup(struct i810_card *card,struct ac97_codec
        return i;
 }
 
+static int is_new_ich(u16 pci_id)
+{
+       switch (pci_id) {
+       case PCI_DEVICE_ID_INTEL_82801DB_5:
+       case PCI_DEVICE_ID_INTEL_82801EB_5:
+       case PCI_DEVICE_ID_INTEL_ESB_5:
+       case PCI_DEVICE_ID_INTEL_ICH6_18:
+               return 1;
+       default:
+               break;
+       }
+
+       return 0;
+}
+
+static inline int ich_use_mmio(struct i810_card *card)
+{
+       return is_new_ich(card->pci_id) && card->use_mmio;
+}
+
 /**
  *     i810_ac97_power_up_bus  -       bring up AC97 link
  *     @card : ICH audio device to power up
@@ -2796,9 +2797,7 @@ static int i810_ac97_power_up_bus(struct i810_card *card)
         */     
        /* see i810_ac97_init for the next 7 lines (jsaw) */
        inw(card->ac97base);
-       if ((card->pci_id == PCI_DEVICE_ID_INTEL_ICH4 || card->pci_id == PCI_DEVICE_ID_INTEL_ICH5 ||
-            card->pci_id == PCI_DEVICE_ID_INTEL_ESB_5 || card->pci_id == PCI_DEVICE_ID_INTEL_ICH6_3)
-           && (card->use_mmio)) {
+       if (ich_use_mmio(card)) {
                primary_codec_id = (int) readl(card->iobase_mmio + SDM) & 0x3;
                printk(KERN_INFO "i810_audio: Primary codec has ID %d\n",
                       primary_codec_id);
@@ -2867,9 +2866,7 @@ static int __devinit i810_ac97_init(struct i810_card *card)
                   possible IO channels. Bit 0:1 of SDM then holds the 
                   last codec ID spoken to. 
                */
-               if ((card->pci_id == PCI_DEVICE_ID_INTEL_ICH4 || card->pci_id == PCI_DEVICE_ID_INTEL_ICH5 ||
-                    card->pci_id == PCI_DEVICE_ID_INTEL_ESB_5 || card->pci_id == PCI_DEVICE_ID_INTEL_ICH6_3)
-                   && (card->use_mmio)) {
+               if (ich_use_mmio(card)) {
                        ac97_id = (int) readl(card->iobase_mmio + SDM) & 0x3;
                        printk(KERN_INFO "i810_audio: Connection %d with codec id %d\n",
                               num_ac97, ac97_id);
@@ -3065,7 +3062,7 @@ static void __devinit i810_configure_clocking (void)
                        goto config_out;
                }
                dmabuf->count = dmabuf->dmasize;
-               CIV_TO_LVI(card->iobase+dmabuf->write_channel->port, 31);
+               CIV_TO_LVI(card->iobase+dmabuf->write_channel->port, -1);
                local_irq_save(flags);
                start_dac(state);
                offset = i810_get_dma_addr(state, 0);