Revert to Fedora kernel-2.6.17-1.2187_FC5 patched with vs2.0.2.1; there are too many...
[linux-2.6.git] / sound / oss / cmpci.c
index 42c9041..de60a05 100644 (file)
 #include <linux/smp_lock.h>
 #include <linux/bitops.h>
 #include <linux/wait.h>
+#include <linux/dma-mapping.h>
 
 #include <asm/io.h>
 #include <asm/page.h>
 #endif
 #ifdef CONFIG_SOUND_CMPCI_JOYSTICK
 #include <linux/gameport.h>
+#include <linux/mutex.h>
+
 #endif
 
 /* --------------------------------------------------------------------- */
@@ -391,7 +394,7 @@ struct cm_state {
        unsigned char fmt, enable;
 
        spinlock_t lock;
-       struct semaphore open_sem;
+       struct mutex open_mutex;
        mode_t open_mode;
        wait_queue_head_t open_wait;
 
@@ -426,7 +429,7 @@ struct cm_state {
        struct address_info mpu_data;
 #endif
 #ifdef CONFIG_SOUND_CMPCI_JOYSTICK
-       struct gameport gameport;
+       struct gameport *gameport;
 #endif
 
        int     chip_version;
@@ -468,28 +471,28 @@ struct cm_state {
 
 static LIST_HEAD(devs);
 
-static int     mpuio = 0;
-static int     fmio = 0;
-static int     joystick = 0;
-static int     spdif_inverse = 0;
-static int     spdif_loop = 0;
-static int     spdif_out = 0;
-static int     use_line_as_rear = 0;
-static int     use_line_as_bass = 0;
-static int     use_mic_as_bass = 0;
-static int     mic_boost = 0;
-static int     hw_copy = 0;
-MODULE_PARM(mpuio, "i");
-MODULE_PARM(fmio, "i");
-MODULE_PARM(joystick, "i");
-MODULE_PARM(spdif_inverse, "i");
-MODULE_PARM(spdif_loop, "i");
-MODULE_PARM(spdif_out, "i");
-MODULE_PARM(use_line_as_rear, "i");
-MODULE_PARM(use_line_as_bass, "i");
-MODULE_PARM(use_mic_as_bass, "i");
-MODULE_PARM(mic_boost, "i");
-MODULE_PARM(hw_copy, "i");
+static int     mpuio;
+static int     fmio;
+static int     joystick;
+static int     spdif_inverse;
+static int     spdif_loop;
+static int     spdif_out;
+static int     use_line_as_rear;
+static int     use_line_as_bass;
+static int     use_mic_as_bass;
+static int     mic_boost;
+static int     hw_copy;
+module_param(mpuio, int, 0);
+module_param(fmio, int, 0);
+module_param(joystick, bool, 0);
+module_param(spdif_inverse, bool, 0);
+module_param(spdif_loop, bool, 0);
+module_param(spdif_out, bool, 0);
+module_param(use_line_as_rear, bool, 0);
+module_param(use_line_as_bass, bool, 0);
+module_param(use_mic_as_bass, bool, 0);
+module_param(mic_boost, bool, 0);
+module_param(hw_copy, bool, 0);
 MODULE_PARM_DESC(mpuio, "(0x330, 0x320, 0x310, 0x300) Base of MPU-401, 0 to disable");
 MODULE_PARM_DESC(fmio, "(0x388, 0x3C8, 0x3E0) Base of OPL3, 0 to disable");
 MODULE_PARM_DESC(joystick, "(1/0) Enable joystick interface, still need joystick driver");
@@ -980,13 +983,13 @@ static void set_ac3(struct cm_state *s, unsigned rate)
        spin_unlock_irqrestore(&s->lock, flags);
 }
 
-static int trans_ac3(struct cm_state *s, void *dest, const char *source, int size)
+static int trans_ac3(struct cm_state *s, void *dest, const char __user *source, int size)
 {
        int   i = size / 2;
        unsigned long data;
        unsigned short data16;
        unsigned long *dst = (unsigned long *) dest;
-       unsigned short *src = (unsigned short *)source;
+       unsigned short __user *src = (unsigned short __user *)source;
        int err;
 
        do {
@@ -1162,15 +1165,6 @@ static inline void enable_dac_unlocked(struct cm_state *s)
                enable_adc(s);
 }
 
-static inline void enable_dac(struct cm_state *s)
-{
-       unsigned long flags;
-
-       spin_lock_irqsave(&s->lock, flags);
-       enable_dac_unlocked(s);
-       spin_unlock_irqrestore(&s->lock, flags);
-}
-
 static inline void stop_adc_unlocked(struct cm_state *s)
 {
        if (s->enable & ENADC) {
@@ -1393,7 +1387,7 @@ static int prog_dmabuf(struct cm_state *s, unsigned rec)
                if (!db->rawbuf || !db->dmaaddr)
                        return -ENOMEM;
                db->buforder = order;
-               /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
+               /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
                pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
                for (pstart = virt_to_page(db->rawbuf); pstart <= pend; pstart++)
                        SetPageReserved(pstart);
@@ -1639,6 +1633,8 @@ static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
        unsigned long flags;
        int i, val, j;
        unsigned char l, r, rl, rr;
+       void __user *argp = (void __user *)arg;
+       int __user *p = argp;
 
        VALIDATE_STATE(s);
         if (cmd == SOUND_MIXER_INFO) {
@@ -1647,7 +1643,7 @@ static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
                strlcpy(info.id, "cmpci", sizeof(info.id));
                strlcpy(info.name, "C-Media PCI", sizeof(info.name));
                info.modify_counter = s->mix.modcnt;
-               if (copy_to_user((void *)arg, &info, sizeof(info)))
+               if (copy_to_user(argp, &info, sizeof(info)))
                        return -EFAULT;
                return 0;
        }
@@ -1656,50 +1652,50 @@ static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
                memset(&info, 0, sizeof(info));
                strlcpy(info.id, "cmpci", sizeof(info.id));
                strlcpy(info.name, "C-Media cmpci", sizeof(info.name));
-               if (copy_to_user((void *)arg, &info, sizeof(info)))
+               if (copy_to_user(argp, &info, sizeof(info)))
                        return -EFAULT;
                return 0;
        }
        if (cmd == OSS_GETVERSION)
-               return put_user(SOUND_VERSION, (int *)arg);
+               return put_user(SOUND_VERSION, p);
        if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
                 return -EINVAL;
         if (_SIOC_DIR(cmd) == _SIOC_READ) {
                 switch (_IOC_NR(cmd)) {
                 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
                        val = mixer_recmask(s);
-                       return put_user(val, (int *)arg);
+                       return put_user(val, p);
 
                 case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
                        val = mixer_outmask(s);
-                       return put_user(val, (int *)arg);
+                       return put_user(val, p);
 
                 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
                        for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
                                if (mixtable[i].type)
                                        val |= 1 << i;
-                       return put_user(val, (int *)arg);
+                       return put_user(val, p);
 
                 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
                        for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
                                if (mixtable[i].rec)
                                        val |= 1 << i;
-                       return put_user(val, (int *)arg);
+                       return put_user(val, p);
 
                 case SOUND_MIXER_OUTMASK: /* Arg contains a bit for each supported recording source */
                        for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
                                if (mixtable[i].play)
                                        val |= 1 << i;
-                       return put_user(val, (int *)arg);
+                       return put_user(val, p);
 
                  case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
                        for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
                                if (mixtable[i].type && mixtable[i].type != MT_4MUTEMONO)
                                        val |= 1 << i;
-                       return put_user(val, (int *)arg);
+                       return put_user(val, p);
 
                 case SOUND_MIXER_CAPS:
-                       return put_user(0, (int *)arg);
+                       return put_user(0, p);
 
                default:
                        i = _IOC_NR(cmd);
@@ -1707,7 +1703,7 @@ static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
                                 return -EINVAL;
                        if (!volidx[i])
                                return -EINVAL;
-                       return put_user(s->mix.vol[volidx[i]-1], (int *)arg);
+                       return put_user(s->mix.vol[volidx[i]-1], p);
                }
        }
         if (_SIOC_DIR(cmd) != (_SIOC_READ|_SIOC_WRITE))
@@ -1715,9 +1711,9 @@ static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
        s->mix.modcnt++;
        switch (_IOC_NR(cmd)) {
        case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
-               i = generic_hweight32(val);
+               i = hweight32(val);
                for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
                        if (!(val & (1 << i)))
                                continue;
@@ -1734,7 +1730,7 @@ static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
                return 0;
 
        case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
                        if (!(val & (1 << i)))
@@ -1754,7 +1750,7 @@ static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
                i = _IOC_NR(cmd);
                if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
                        return -EINVAL;
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                l = val & 0xff;
                r = (val >> 8) & 0xff;
@@ -1829,7 +1825,7 @@ static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
                if (!volidx[i])
                        return -EINVAL;
                s->mix.vol[volidx[i]-1] = val;
-               return put_user(s->mix.vol[volidx[i]-1], (int *)arg);
+               return put_user(s->mix.vol[volidx[i]-1], p);
        }
 }
 
@@ -1850,7 +1846,7 @@ static int cm_open_mixdev(struct inode *inode, struct file *file)
        }
                VALIDATE_STATE(s);
        file->private_data = s;
-       return 0;
+       return nonseekable_open(inode, file);
 }
 
 static int cm_release_mixdev(struct inode *inode, struct file *file)
@@ -1914,7 +1910,7 @@ static int drain_dac(struct cm_state *s, int nonblock)
 
 /* --------------------------------------------------------------------- */
 
-static ssize_t cm_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+static ssize_t cm_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
 {
        struct cm_state *s = (struct cm_state *)file->private_data;
        DECLARE_WAITQUEUE(wait, current);
@@ -1924,8 +1920,6 @@ static ssize_t cm_read(struct file *file, char *buffer, size_t count, loff_t *pp
        int cnt;
 
        VALIDATE_STATE(s);
-       if (ppos != &file->f_pos)
-               return -ESPIPE;
        if (s->dma_adc.mapped)
                return -ENXIO;
        if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
@@ -1975,11 +1969,11 @@ static ssize_t cm_read(struct file *file, char *buffer, size_t count, loff_t *pp
                }
                if (s->status & DO_BIGENDIAN_R) {
                        int     i, err;
-                       unsigned char *src, *dst;
+                       unsigned char *src;
+                       char __user *dst = buffer;
                        unsigned char data[2];
 
                        src = (unsigned char *) (s->dma_adc.rawbuf + swptr);
-                       dst = (unsigned char *) buffer;
                        // copy left/right sample at one time
                        for (i = 0; i < cnt / 2; i++) {
                                data[0] = src[1];
@@ -2016,7 +2010,7 @@ out:
        return ret;
 }
 
-static ssize_t cm_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
+static ssize_t cm_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
 {
        struct cm_state *s = (struct cm_state *)file->private_data;
        DECLARE_WAITQUEUE(wait, current);
@@ -2026,8 +2020,6 @@ static ssize_t cm_write(struct file *file, const char *buffer, size_t count, lof
        int cnt;
 
        VALIDATE_STATE(s);
-       if (ppos != &file->f_pos)
-               return -ESPIPE;
        if (s->dma_dac.mapped)
                return -ENXIO;
        if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
@@ -2116,10 +2108,10 @@ static ssize_t cm_write(struct file *file, const char *buffer, size_t count, lof
                        swptr = (swptr + 2 * cnt) % s->dma_dac.dmasize;
                } else if ((s->status & DO_DUAL_DAC) && (s->status & DO_BIGENDIAN_W)) {
                        int     i, err;
-                       unsigned char *src, *dst0, *dst1;
+                       const char __user *src = buffer;
+                       unsigned char *dst0, *dst1;
                        unsigned char data[8];
 
-                       src = (unsigned char *) buffer;
                        dst0 = (unsigned char *) (s->dma_dac.rawbuf + swptr);
                        dst1 = (unsigned char *) (s->dma_adc.rawbuf + swptr);
                        // copy left/right sample at one time
@@ -2170,9 +2162,9 @@ static ssize_t cm_write(struct file *file, const char *buffer, size_t count, lof
                        swptr = (swptr + cnt) % s->dma_dac.dmasize;
                } else if (s->status & DO_DUAL_DAC) {
                        int     i, err;
-                       unsigned long *src, *dst0, *dst1;
+                       unsigned long __user *src = (unsigned long __user *) buffer;
+                       unsigned long *dst0, *dst1;
 
-                       src = (unsigned long *) buffer;
                        dst0 = (unsigned long *) (s->dma_dac.rawbuf + swptr);
                        dst1 = (unsigned long *) (s->dma_adc.rawbuf + swptr);
                        // copy left/right sample at one time
@@ -2189,10 +2181,10 @@ static ssize_t cm_write(struct file *file, const char *buffer, size_t count, lof
                        swptr = (swptr + cnt) % s->dma_dac.dmasize;
                } else if (s->status & DO_BIGENDIAN_W) {
                        int     i, err;
-                       unsigned char *src, *dst;
+                       const char __user *src = buffer;
+                       unsigned char *dst;
                        unsigned char data[2];
 
-                       src = (unsigned char *) buffer;
                        dst = (unsigned char *) (s->dma_dac.rawbuf + swptr);
                        // swap hi/lo bytes for each sample
                        for (i = 0; i < cnt / 2; i++) {
@@ -2303,7 +2295,9 @@ static int cm_mmap(struct file *file, struct vm_area_struct *vma)
        if (size > (PAGE_SIZE << db->buforder))
                goto out;
        ret = -EINVAL;
-       if (remap_page_range(vma, vma->vm_start, virt_to_phys(db->rawbuf), size, vma->vm_page_prot))
+       if (remap_pfn_range(vma, vma->vm_start,
+                               virt_to_phys(db->rawbuf) >> PAGE_SHIFT,
+                               size, vma->vm_page_prot))
                goto out;
        db->mapped = 1;
        ret = 0;
@@ -2330,13 +2324,15 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
         count_info cinfo;
        int val, mapped, ret;
        unsigned char fmtm, fmtd;
+       void __user *argp = (void __user *)arg;
+       int __user *p = argp;
 
        VALIDATE_STATE(s);
         mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
                ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
        switch (cmd) {
        case OSS_GETVERSION:
-               return put_user(SOUND_VERSION, (int *)arg);
+               return put_user(SOUND_VERSION, p);
 
        case SNDCTL_DSP_SYNC:
                if (file->f_mode & FMODE_WRITE)
@@ -2347,7 +2343,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                return 0;
 
        case SNDCTL_DSP_GETCAPS:
-               return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_BIND, (int *)arg);
+               return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_BIND, p);
 
         case SNDCTL_DSP_RESET:
                if (file->f_mode & FMODE_WRITE) {
@@ -2365,7 +2361,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                return 0;
 
         case SNDCTL_DSP_SPEED:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                if (val >= 0) {
                        if (file->f_mode & FMODE_READ) {
@@ -2383,10 +2379,10 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                                set_dac_rate(s, val);
                        }
                }
-               return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
+               return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
 
         case SNDCTL_DSP_STEREO:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                fmtd = 0;
                fmtm = ~0;
@@ -2417,7 +2413,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                return 0;
 
         case SNDCTL_DSP_CHANNELS:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                if (val != 0) {
                        fmtd = 0;
@@ -2449,18 +2445,18 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                        if ((s->capability & CAN_MULTI_CH)
                             && (file->f_mode & FMODE_WRITE)) {
                                val = set_dac_channels(s, val);
-                               return put_user(val, (int *)arg);
+                               return put_user(val, p);
                        }
                }
                return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT)
-                                          : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, (int *)arg);
+                                          : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, p);
 
        case SNDCTL_DSP_GETFMTS: /* Returns a mask */
                 return put_user(AFMT_S16_BE|AFMT_S16_LE|AFMT_U8|
-                       ((s->capability & CAN_AC3) ? AFMT_AC3 : 0), (int *)arg);
+                       ((s->capability & CAN_AC3) ? AFMT_AC3 : 0), p);
 
        case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                if (val != AFMT_QUERY) {
                        fmtd = 0;
@@ -2503,9 +2499,9 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                        }
                        set_fmt(s, fmtm, fmtd);
                }
-               if (s->status & DO_AC3) return put_user(AFMT_AC3, (int *)arg);
+               if (s->status & DO_AC3) return put_user(AFMT_AC3, p);
                return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT)
-                                          : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? val : AFMT_U8, (int *)arg);
+                                          : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? val : AFMT_U8, p);
 
        case SNDCTL_DSP_POST:
                 return 0;
@@ -2517,16 +2513,16 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                         (s->enable & ENDAC) &&
                         (s->enable & ENADC))
                                val |= PCM_ENABLE_OUTPUT;
-                       return put_user(val, (int *)arg);
+                       return put_user(val, p);
                }
                if (file->f_mode & FMODE_READ && s->enable & ENADC)
                        val |= PCM_ENABLE_INPUT;
                if (file->f_mode & FMODE_WRITE && s->enable & ENDAC)
                        val |= PCM_ENABLE_OUTPUT;
-               return put_user(val, (int *)arg);
+               return put_user(val, p);
 
        case SNDCTL_DSP_SETTRIGGER:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                if (file->f_mode & FMODE_READ) {
                        if (val & PCM_ENABLE_INPUT) {
@@ -2568,7 +2564,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                 abinfo.fragstotal = s->dma_dac.numfrag;
                 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
                spin_unlock_irqrestore(&s->lock, flags);
-               return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
+               return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
 
        case SNDCTL_DSP_GETISPACE:
                if (!(file->f_mode & FMODE_READ))
@@ -2582,7 +2578,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                 abinfo.fragstotal = s->dma_adc.numfrag;
                 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
                spin_unlock_irqrestore(&s->lock, flags);
-               return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
+               return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
 
         case SNDCTL_DSP_NONBLOCK:
                 file->f_flags |= O_NONBLOCK;
@@ -2595,7 +2591,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                cm_update_ptr(s);
                 val = s->dma_dac.count;
                spin_unlock_irqrestore(&s->lock, flags);
-               return put_user(val, (int *)arg);
+               return put_user(val, p);
 
         case SNDCTL_DSP_GETIPTR:
                if (!(file->f_mode & FMODE_READ))
@@ -2608,7 +2604,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                if (s->dma_adc.mapped)
                        s->dma_adc.count &= s->dma_adc.fragsize-1;
                spin_unlock_irqrestore(&s->lock, flags);
-                return copy_to_user((void *)arg, &cinfo, sizeof(cinfo))  ? -EFAULT : 0;
+                return copy_to_user(argp, &cinfo, sizeof(cinfo))  ? -EFAULT : 0;
 
         case SNDCTL_DSP_GETOPTR:
                if (!(file->f_mode & FMODE_WRITE))
@@ -2625,7 +2621,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                                s->dma_adc.count &= s->dma_adc.fragsize-1;
                }
                spin_unlock_irqrestore(&s->lock, flags);
-                return copy_to_user((void *)arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
+                return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
 
         case SNDCTL_DSP_GETBLKSIZE:
                if (file->f_mode & FMODE_WRITE) {
@@ -2634,16 +2630,16 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                        if (s->status & DO_DUAL_DAC) {
                                if ((val = prog_dmabuf(s, 1)))
                                        return val;
-                               return put_user(2 * s->dma_dac.fragsize, (int *)arg);
+                               return put_user(2 * s->dma_dac.fragsize, p);
                        }
-                       return put_user(s->dma_dac.fragsize, (int *)arg);
+                       return put_user(s->dma_dac.fragsize, p);
                }
                if ((val = prog_dmabuf(s, 1)))
                        return val;
-               return put_user(s->dma_adc.fragsize, (int *)arg);
+               return put_user(s->dma_adc.fragsize, p);
 
         case SNDCTL_DSP_SETFRAGMENT:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                if (file->f_mode & FMODE_READ) {
                        s->dma_adc.ossfragshift = val & 0xffff;
@@ -2675,7 +2671,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
                    (file->f_mode & FMODE_WRITE && s->dma_dac.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;
@@ -2689,22 +2685,22 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                return 0;
 
         case SOUND_PCM_READ_RATE:
-               return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
+               return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
 
         case SOUND_PCM_READ_CHANNELS:
-               return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT) : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, (int *)arg);
+               return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT) : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, p);
 
         case SOUND_PCM_READ_BITS:
-               return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT) : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? 16 : 8, (int *)arg);
+               return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT) : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? 16 : 8, p);
 
         case SOUND_PCM_READ_FILTER:
-               return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, (int *)arg);
+               return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
 
        case SNDCTL_DSP_GETCHANNELMASK:
-               return put_user(DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE|DSP_BIND_SPDIF, (int *)arg);
+               return put_user(DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE|DSP_BIND_SPDIF, p);
 
        case SNDCTL_DSP_BIND_CHANNEL:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                if (val == DSP_BIND_QUERY) {
                        val = DSP_BIND_FRONT;
@@ -2757,7 +2753,7 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
                                }
                        }
                }
-               return put_user(val, (int *)arg);
+               return put_user(val, p);
 
        case SOUND_PCM_WRITE_FILTER:
        case SNDCTL_DSP_MAPINBUF:
@@ -2765,47 +2761,47 @@ static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, un
         case SNDCTL_DSP_SETSYNCRO:
                 return -EINVAL;
        case SNDCTL_SPDIF_COPYRIGHT:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                set_spdif_copyright(s, val);
                 return 0;
        case SNDCTL_SPDIF_LOOP:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                set_spdif_loop(s, val);
                 return 0;
        case SNDCTL_SPDIF_MONITOR:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                set_spdif_monitor(s, val);
                 return 0;
        case SNDCTL_SPDIF_LEVEL:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                set_spdifout_level(s, val);
                 return 0;
        case SNDCTL_SPDIF_INV:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                set_spdifin_inverse(s, val);
                 return 0;
        case SNDCTL_SPDIF_SEL2:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                set_spdifin_channel2(s, val);
                 return 0;
        case SNDCTL_SPDIF_VALID:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                set_spdifin_valid(s, val);
                 return 0;
        case SNDCTL_SPDIFOUT:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                set_spdifout(s, val ? s->ratedac : 0);
                 return 0;
        case SNDCTL_SPDIFIN:
-               if (get_user(val, (int *)arg))
+               if (get_user(val, p))
                        return -EFAULT;
                set_spdifin(s, val ? s->rateadc : 0);
                 return 0;
@@ -2831,21 +2827,21 @@ static int cm_open(struct inode *inode, struct file *file)
                VALIDATE_STATE(s);
        file->private_data = s;
        /* wait for device to become free */
-       down(&s->open_sem);
+       mutex_lock(&s->open_mutex);
        while (s->open_mode & file->f_mode) {
                if (file->f_flags & O_NONBLOCK) {
-                       up(&s->open_sem);
+                       mutex_unlock(&s->open_mutex);
                        return -EBUSY;
                }
                add_wait_queue(&s->open_wait, &wait);
                __set_current_state(TASK_INTERRUPTIBLE);
-               up(&s->open_sem);
+               mutex_unlock(&s->open_mutex);
                schedule();
                remove_wait_queue(&s->open_wait, &wait);
                set_current_state(TASK_RUNNING);
                if (signal_pending(current))
                        return -ERESTARTSYS;
-               down(&s->open_sem);
+               mutex_lock(&s->open_mutex);
        }
        if (file->f_mode & FMODE_READ) {
                s->status &= ~DO_BIGENDIAN_R;
@@ -2873,8 +2869,8 @@ static int cm_open(struct inode *inode, struct file *file)
        }
        set_fmt(s, fmtm, fmts);
        s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
-       up(&s->open_sem);
-       return 0;
+       mutex_unlock(&s->open_mutex);
+       return nonseekable_open(inode, file);
 }
 
 static int cm_release(struct inode *inode, struct file *file)
@@ -2885,7 +2881,7 @@ static int cm_release(struct inode *inode, struct file *file)
        lock_kernel();
        if (file->f_mode & FMODE_WRITE)
                drain_dac(s, file->f_flags & O_NONBLOCK);
-       down(&s->open_sem);
+       mutex_lock(&s->open_mutex);
        if (file->f_mode & FMODE_WRITE) {
                stop_dac(s);
 
@@ -2909,7 +2905,7 @@ static int cm_release(struct inode *inode, struct file *file)
                s->status &= ~DO_BIGENDIAN_R;
        }
        s->open_mode &= ~(file->f_mode & (FMODE_READ|FMODE_WRITE));
-       up(&s->open_sem);
+       mutex_unlock(&s->open_mutex);
        wake_up(&s->open_wait);
        unlock_kernel();
        return 0;
@@ -2932,7 +2928,7 @@ static /*const*/ struct file_operations cm_audio_fops = {
 static struct initvol {
        int mixch;
        int vol;
-} initvol[] __initdata = {
+} initvol[] __devinitdata = {
        { SOUND_MIXER_WRITE_CD, 0x4f4f },
        { SOUND_MIXER_WRITE_LINE, 0x4f4f },
        { SOUND_MIXER_WRITE_MIC, 0x4f4f },
@@ -2991,6 +2987,51 @@ static int query_chip(struct cm_state *s)
        return ChipVersion;
 }
 
+#ifdef CONFIG_SOUND_CMPCI_JOYSTICK
+static int __devinit cm_create_gameport(struct cm_state *s, int io_port)
+{
+       struct gameport *gp;
+
+       if (!request_region(io_port, CM_EXTENT_GAME, "cmpci GAME")) {
+               printk(KERN_ERR "cmpci: gameport io ports 0x%#x in use\n", io_port);
+               return -EBUSY;
+       }
+
+       if (!(s->gameport = gp = gameport_allocate_port())) {
+               printk(KERN_ERR "cmpci: can not allocate memory for gameport\n");
+               release_region(io_port, CM_EXTENT_GAME);
+               return -ENOMEM;
+       }
+
+       gameport_set_name(gp, "C-Media GP");
+       gameport_set_phys(gp, "pci%s/gameport0", pci_name(s->dev));
+       gp->dev.parent = &s->dev->dev;
+       gp->io = io_port;
+
+       /* enable joystick */
+       maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x02);
+
+       gameport_register_port(gp);
+
+       return 0;
+}
+
+static void __devexit cm_free_gameport(struct cm_state *s)
+{
+       if (s->gameport) {
+               int gpio = s->gameport->io;
+
+               gameport_unregister_port(s->gameport);
+               s->gameport = NULL;
+               maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x02, 0);
+               release_region(gpio, CM_EXTENT_GAME);
+       }
+}
+#else
+static inline int cm_create_gameport(struct cm_state *s, int io_port) { return -ENOSYS; }
+static inline void cm_free_gameport(struct cm_state *s) { }
+#endif
+
 #define        echo_option(x)\
 if (x) strcat(options, "" #x " ")
 
@@ -3000,6 +3041,8 @@ static int __devinit cm_probe(struct pci_dev *pcidev, const struct pci_device_id
        mm_segment_t fs;
        int i, val, ret;
        unsigned char reg_mask;
+       int timeout;
+       struct resource *ports;
        struct {
                unsigned short  deviceid;
                char            *devicename;
@@ -3018,7 +3061,7 @@ static int __devinit cm_probe(struct pci_dev *pcidev, const struct pci_device_id
                return -ENODEV;
        if (pcidev->irq == 0)
                return -ENODEV;
-       i = pci_set_dma_mask(pcidev, 0xffffffff);
+       i = pci_set_dma_mask(pcidev, DMA_32BIT_MASK);
        if (i) {
                printk(KERN_WARNING "cmpci: architecture does not support 32bit PCI busmaster DMA\n");
                return i;
@@ -3039,7 +3082,7 @@ static int __devinit cm_probe(struct pci_dev *pcidev, const struct pci_device_id
        init_waitqueue_head(&s->dma_adc.wait);
        init_waitqueue_head(&s->dma_dac.wait);
        init_waitqueue_head(&s->open_wait);
-       init_MUTEX(&s->open_sem);
+       mutex_init(&s->open_mutex);
        spin_lock_init(&s->lock);
        s->magic = CM_MAGIC;
        s->dev = pcidev;
@@ -3184,71 +3227,61 @@ static int __devinit cm_probe(struct pci_dev *pcidev, const struct pci_device_id
        }
 #endif
 #ifdef CONFIG_SOUND_CMPCI_MIDI
+       switch (s->iomidi) {
+           case 0x330:
+               reg_mask = 0;
+               break;
+           case 0x320:
+               reg_mask = 0x20;
+               break;
+           case 0x310:
+               reg_mask = 0x40;
+               break;
+           case 0x300:
+               reg_mask = 0x60;
+               break;
+           default:
+               s->iomidi = 0;
+               goto skip_mpu;
+       }
+       ports = request_region(s->iomidi, 2, "mpu401");
+       if (!ports)
+               goto skip_mpu;
        /* disable MPU-401 */
        maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x04, 0);
        s->mpu_data.name = "cmpci mpu";
        s->mpu_data.io_base = s->iomidi;
        s->mpu_data.irq = -s->irq;      // tell mpu401 to share irq
-       if (probe_mpu401(&s->mpu_data))
+       if (probe_mpu401(&s->mpu_data, ports)) {
+               release_region(s->iomidi, 2);
                s->iomidi = 0;
-       if (s->iomidi) {
-               /* set IO based at 0x330 */
-               switch (s->iomidi) {
-                   case 0x330:
-                       reg_mask = 0;
-                       break;
-                   case 0x320:
-                       reg_mask = 0x20;
-                       break;
-                   case 0x310:
-                       reg_mask = 0x40;
-                       break;
-                   case 0x300:
-                       reg_mask = 0x60;
-                       break;
-                   default:
-                       s->iomidi = 0;
+               goto skip_mpu;
+       }
+       maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0x60, reg_mask);
+       /* enable MPU-401 */
+       maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x04);
+       /* clear all previously received interrupt */
+       for (timeout = 900000; timeout > 0; timeout--) {
+               if ((inb(s->iomidi + 1) && 0x80) == 0)
+                       inb(s->iomidi);
+               else
                        break;
-               }
-               maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0x60, reg_mask);
-               /* enable MPU-401 */
-               if (s->iomidi) {
-                       int timeout;
-
-                       maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x04);
-                       /* clear all previously received interrupt */
-                       for (timeout = 900000; timeout > 0; timeout--) {
-                               if ((inb(s->iomidi + 1) && 0x80) == 0)
-                                       inb(s->iomidi);
-                               else
-                                       break;
-                       }
-                       if (!probe_mpu401(&s->mpu_data)) {
-                               s->iomidi = 0;
-                               maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x04);
-                       } else {
-                               attach_mpu401(&s->mpu_data, THIS_MODULE);
-                               s->midi_devc = s->mpu_data.slots[1];
-                       }
-               }
        }
-#endif
-#ifdef CONFIG_SOUND_CMPCI_JOYSTICK
-       /* enable joystick */
-       if (joystick) {
-               s->gameport.io = 0x200;
-               if (!request_region(s->gameport.io, CM_EXTENT_GAME, "cmpci GAME")) {
-                       printk(KERN_ERR "cmpci: gameport io ports in use\n");
-                       s->gameport.io = 0;
-               } else {
-                       maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x02);
-                       gameport_register_port(&s->gameport);
-               }
+       if (!probe_mpu401(&s->mpu_data, ports)) {
+               release_region(s->iomidi, 2);
+               s->iomidi = 0;
+               maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x04);
        } else {
-               maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x02, 0);
-               s->gameport.io = 0;
+               attach_mpu401(&s->mpu_data, THIS_MODULE);
+               s->midi_devc = s->mpu_data.slots[1];
        }
+skip_mpu:
 #endif
+       /* disable joystick port */
+       maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x02, 0);
+       if (joystick)
+               cm_create_gameport(s, 0x200);
+
        /* store it in the driver field */
        pci_set_drvdata(pcidev, s);
        /* put it into driver list */
@@ -3276,19 +3309,15 @@ MODULE_AUTHOR("ChenLi Tien, cltien@cmedia.com.tw");
 MODULE_DESCRIPTION("CM8x38 Audio Driver");
 MODULE_LICENSE("GPL");
 
-static void __devinit cm_remove(struct pci_dev *dev)
+static void __devexit cm_remove(struct pci_dev *dev)
 {
        struct cm_state *s = pci_get_drvdata(dev);
 
        if (!s)
                return;
-#ifdef CONFIG_SOUND_CMPCI_JOYSTICK
-       if (s->gameport.io) {
-               gameport_unregister_port(&s->gameport);
-               release_region(s->gameport.io, CM_EXTENT_GAME);
-               maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x02, 0);
-       }
-#endif
+
+       cm_free_gameport(s);
+
 #ifdef CONFIG_SOUND_CMPCI_FM
        if (s->iosynth) {
                /* disable FM */
@@ -3333,13 +3362,13 @@ static struct pci_driver cm_driver = {
        .name    = "cmpci",
        .id_table = id_table,
        .probe   = cm_probe,
-       .remove  = cm_remove
+       .remove  = __devexit_p(cm_remove)
 };
 
 static int __init init_cmpci(void)
 {
        printk(KERN_INFO "cmpci: version $Revision: 6.82 $ time " __TIME__ " " __DATE__ "\n");
-       return pci_module_init(&cm_driver);
+       return pci_register_driver(&cm_driver);
 }
 
 static void __exit cleanup_cmpci(void)