Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / sound / oss / es1371.c
index d6b59fb..4400c85 100644 (file)
 #include <linux/ac97_codec.h>
 #include <linux/gameport.h>
 #include <linux/wait.h>
+#include <linux/dma-mapping.h>
+#include <linux/mutex.h>
 
 #include <asm/io.h>
 #include <asm/page.h>
 #include <asm/uaccess.h>
 
+#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
+#define SUPPORT_JOYSTICK
+#endif
+
 /* --------------------------------------------------------------------- */
 
 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
@@ -414,7 +420,7 @@ struct es1371_state {
        unsigned dac1rate, dac2rate, adcrate;
 
        spinlock_t lock;
-       struct semaphore open_sem;
+       struct mutex open_mutex;
        mode_t open_mode;
        wait_queue_head_t open_wait;
 
@@ -453,8 +459,11 @@ struct es1371_state {
                unsigned char obuf[MIDIOUTBUF];
        } midi;
 
-       struct gameport gameport;
-       struct semaphore sem;
+#ifdef SUPPORT_JOYSTICK
+       struct gameport *gameport;
+#endif
+
+       struct mutex sem;
 };
 
 /* --------------------------------------------------------------------- */
@@ -637,7 +646,7 @@ static void set_dac2_rate(struct es1371_state *s, unsigned rate)
 
 /* --------------------------------------------------------------------- */
 
-static void __init src_init(struct es1371_state *s)
+static void __devinit src_init(struct es1371_state *s)
 {
         unsigned int i;
 
@@ -910,7 +919,7 @@ static int prog_dmabuf(struct es1371_state *s, struct dmabuf *db, unsigned rate,
                if (!db->rawbuf)
                        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 (page = virt_to_page(db->rawbuf); page <= pend; page++)
                        SetPageReserved(page);
@@ -1338,7 +1347,7 @@ static ssize_t es1371_read(struct file *file, char __user *buffer, size_t count,
                return -ENXIO;
        if (!access_ok(VERIFY_WRITE, buffer, count))
                return -EFAULT;
-       down(&s->sem);
+       mutex_lock(&s->sem);
        if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
                goto out2;
        
@@ -1362,14 +1371,14 @@ static ssize_t es1371_read(struct file *file, char __user *buffer, size_t count,
                                        ret = -EAGAIN;
                                goto out;
                        }
-                       up(&s->sem);
+                       mutex_unlock(&s->sem);
                        schedule();
                        if (signal_pending(current)) {
                                if (!ret)
                                        ret = -ERESTARTSYS;
                                goto out2;
                        }
-                       down(&s->sem);
+                       mutex_lock(&s->sem);
                        if (s->dma_adc.mapped)
                        {
                                ret = -ENXIO;
@@ -1394,7 +1403,7 @@ static ssize_t es1371_read(struct file *file, char __user *buffer, size_t count,
                        start_adc(s);
        }
 out:
-       up(&s->sem);
+       mutex_unlock(&s->sem);
 out2:
        remove_wait_queue(&s->dma_adc.wait, &wait);
        set_current_state(TASK_RUNNING);
@@ -1415,7 +1424,7 @@ static ssize_t es1371_write(struct file *file, const char __user *buffer, size_t
                return -ENXIO;
        if (!access_ok(VERIFY_READ, buffer, count))
                return -EFAULT;
-       down(&s->sem);  
+       mutex_lock(&s->sem);
        if (!s->dma_dac2.ready && (ret = prog_dmabuf_dac2(s)))
                goto out3;
        ret = 0;
@@ -1443,14 +1452,14 @@ static ssize_t es1371_write(struct file *file, const char __user *buffer, size_t
                                        ret = -EAGAIN;
                                goto out;
                        }       
-                       up(&s->sem);
+                       mutex_unlock(&s->sem);
                        schedule();
                        if (signal_pending(current)) {
                                if (!ret)
                                        ret = -ERESTARTSYS;
                                goto out2;
                        }
-                       down(&s->sem);
+                       mutex_lock(&s->sem);
                        if (s->dma_dac2.mapped)
                        {
                                ret = -ENXIO;
@@ -1476,7 +1485,7 @@ static ssize_t es1371_write(struct file *file, const char __user *buffer, size_t
                        start_dac2(s);
        }
 out:
-       up(&s->sem);
+       mutex_unlock(&s->sem);
 out2:
        remove_wait_queue(&s->dma_dac2.wait, &wait);
 out3:  
@@ -1530,7 +1539,7 @@ static int es1371_mmap(struct file *file, struct vm_area_struct *vma)
 
        VALIDATE_STATE(s);
        lock_kernel();
-       down(&s->sem);
+       mutex_lock(&s->sem);
        
        if (vma->vm_flags & VM_WRITE) {
                if ((ret = prog_dmabuf_dac2(s)) != 0) {
@@ -1555,13 +1564,15 @@ static int es1371_mmap(struct file *file, struct vm_area_struct *vma)
                ret = -EINVAL;
                goto out;
        }
-       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)) {
                ret = -EAGAIN;
                goto out;
        }
        db->mapped = 1;
 out:
-       up(&s->sem);
+       mutex_unlock(&s->sem);
        unlock_kernel();
        return ret;
 }
@@ -1928,21 +1939,21 @@ static int es1371_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->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
@@ -1972,8 +1983,8 @@ static int es1371_open(struct inode *inode, struct file *file)
        outl(s->sctrl, s->io+ES1371_REG_SERIAL_CONTROL);
        spin_unlock_irqrestore(&s->lock, flags);
        s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
-       up(&s->open_sem);
-       init_MUTEX(&s->sem);
+       mutex_unlock(&s->open_mutex);
+       mutex_init(&s->sem);
        return nonseekable_open(inode, file);
 }
 
@@ -1985,7 +1996,7 @@ static int es1371_release(struct inode *inode, struct file *file)
        lock_kernel();
        if (file->f_mode & FMODE_WRITE)
                drain_dac2(s, file->f_flags & O_NONBLOCK);
-       down(&s->open_sem);
+       mutex_lock(&s->open_mutex);
        if (file->f_mode & FMODE_WRITE) {
                stop_dac2(s);
                dealloc_dmabuf(s, &s->dma_dac2);
@@ -1995,7 +2006,7 @@ static int es1371_release(struct inode *inode, struct file *file)
                dealloc_dmabuf(s, &s->dma_adc);
        }
        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;
@@ -2128,7 +2139,9 @@ static int es1371_mmap_dac(struct file *file, struct vm_area_struct *vma)
        if (size > (PAGE_SIZE << s->dma_dac1.buforder))
                goto out;
        ret = -EAGAIN;
-       if (remap_page_range(vma, vma->vm_start, virt_to_phys(s->dma_dac1.rawbuf), size, vma->vm_page_prot))
+       if (remap_pfn_range(vma, vma->vm_start,
+                       virt_to_phys(s->dma_dac1.rawbuf) >> PAGE_SHIFT,
+                       size, vma->vm_page_prot))
                goto out;
        s->dma_dac1.mapped = 1;
        ret = 0;
@@ -2365,21 +2378,21 @@ static int es1371_open_dac(struct inode *inode, struct file *file)
                return -EINVAL;
                file->private_data = s;
        /* wait for device to become free */
-       down(&s->open_sem);
+       mutex_lock(&s->open_mutex);
        while (s->open_mode & FMODE_DAC) {
                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);
        }
        s->dma_dac1.ossfragshift = s->dma_dac1.ossmaxfrags = s->dma_dac1.subdivision = 0;
        s->dma_dac1.enabled = 1;
@@ -2393,7 +2406,7 @@ static int es1371_open_dac(struct inode *inode, struct file *file)
        outl(s->sctrl, s->io+ES1371_REG_SERIAL_CONTROL);
        spin_unlock_irqrestore(&s->lock, flags);
        s->open_mode |= FMODE_DAC;
-       up(&s->open_sem);
+       mutex_unlock(&s->open_mutex);
        return nonseekable_open(inode, file);
 }
 
@@ -2404,11 +2417,11 @@ static int es1371_release_dac(struct inode *inode, struct file *file)
        VALIDATE_STATE(s);
        lock_kernel();
        drain_dac1(s, file->f_flags & O_NONBLOCK);
-       down(&s->open_sem);
+       mutex_lock(&s->open_mutex);
        stop_dac1(s);
        dealloc_dmabuf(s, &s->dma_dac1);
        s->open_mode &= ~FMODE_DAC;
-       up(&s->open_sem);
+       mutex_unlock(&s->open_mutex);
        wake_up(&s->open_wait);
        unlock_kernel();
        return 0;
@@ -2596,21 +2609,21 @@ static int es1371_midi_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 << FMODE_MIDI_SHIFT)) {
                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);
        }
        spin_lock_irqsave(&s->lock, flags);
        if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
@@ -2631,7 +2644,7 @@ static int es1371_midi_open(struct inode *inode, struct file *file)
        es1371_handle_midi(s);
        spin_unlock_irqrestore(&s->lock, flags);
        s->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
-       up(&s->open_sem);
+       mutex_unlock(&s->open_mutex);
        return nonseekable_open(inode, file);
 }
 
@@ -2664,7 +2677,7 @@ static int es1371_midi_release(struct inode *inode, struct file *file)
                remove_wait_queue(&s->midi.owait, &wait);
                set_current_state(TASK_RUNNING);
        }
-       down(&s->open_sem);
+       mutex_lock(&s->open_mutex);
        s->open_mode &= ~((file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ|FMODE_MIDI_WRITE));
        spin_lock_irqsave(&s->lock, flags);
        if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
@@ -2672,7 +2685,7 @@ static int es1371_midi_release(struct inode *inode, struct file *file)
                outl(s->ctrl, s->io+ES1371_REG_CONTROL);
        }
        spin_unlock_irqrestore(&s->lock, flags);
-       up(&s->open_sem);
+       mutex_unlock(&s->open_mutex);
        wake_up(&s->open_wait);
        unlock_kernel();
        return 0;
@@ -2737,11 +2750,11 @@ static int amplifier[NR_DEVICE];
 
 static unsigned int devindex;
 
-MODULE_PARM(spdif, "1-" __MODULE_STRING(NR_DEVICE) "i");
+module_param_array(spdif, bool, NULL, 0);
 MODULE_PARM_DESC(spdif, "if 1 the output is in S/PDIF digital mode");
-MODULE_PARM(nomix, "1-" __MODULE_STRING(NR_DEVICE) "i");
+module_param_array(nomix, bool, NULL, 0);
 MODULE_PARM_DESC(nomix, "if 1 no analog audio is mixed to the digital output");
-MODULE_PARM(amplifier, "1-" __MODULE_STRING(NR_DEVICE) "i");
+module_param_array(amplifier, bool, NULL, 0);
 MODULE_PARM_DESC(amplifier, "Set to 1 if the machine needs the amp control enabling (many laptops)");
 
 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
@@ -2754,7 +2767,7 @@ MODULE_LICENSE("GPL");
 static struct initvol {
        int mixch;
        int vol;
-} initvol[] __initdata = {
+} initvol[] __devinitdata = {
        { SOUND_MIXER_WRITE_LINE, 0x4040 },
        { SOUND_MIXER_WRITE_CD, 0x4040 },
        { MIXER_WRITE(SOUND_MIXER_VIDEO), 0x4040 },
@@ -2782,6 +2795,57 @@ static struct
        { PCI_ANY_ID, PCI_ANY_ID }
 };
 
+#ifdef SUPPORT_JOYSTICK
+
+static int __devinit es1371_register_gameport(struct es1371_state *s)
+{
+       struct gameport *gp;
+       int gpio;
+
+       for (gpio = 0x218; gpio >= 0x200; gpio -= 0x08)
+               if (request_region(gpio, JOY_EXTENT, "es1371"))
+                       break;
+
+       if (gpio < 0x200) {
+               printk(KERN_ERR PFX "no free joystick address found\n");
+               return -EBUSY;
+       }
+
+       s->gameport = gp = gameport_allocate_port();
+       if (!gp) {
+               printk(KERN_ERR PFX "can not allocate memory for gameport\n");
+               release_region(gpio, JOY_EXTENT);
+               return -ENOMEM;
+       }
+
+       gameport_set_name(gp, "ESS1371 Gameport");
+       gameport_set_phys(gp, "isa%04x/gameport0", gpio);
+       gp->dev.parent = &s->dev->dev;
+       gp->io = gpio;
+
+       s->ctrl |= CTRL_JYSTK_EN | (((gpio >> 3) & CTRL_JOY_MASK) << CTRL_JOY_SHIFT);
+       outl(s->ctrl, s->io + ES1371_REG_CONTROL);
+
+       gameport_register_port(gp);
+
+       return 0;
+}
+
+static inline void es1371_unregister_gameport(struct es1371_state *s)
+{
+       if (s->gameport) {
+               int gpio = s->gameport->io;
+               gameport_unregister_port(s->gameport);
+               release_region(gpio, JOY_EXTENT);
+
+       }
+}
+
+#else
+static inline int es1371_register_gameport(struct es1371_state *s) { return -ENOSYS; }
+static inline void es1371_unregister_gameport(struct es1371_state *s) { }
+#endif /* SUPPORT_JOYSTICK */
+
 
 static int __devinit es1371_probe(struct pci_dev *pcidev, const struct pci_device_id *pciid)
 {
@@ -2800,7 +2864,7 @@ static int __devinit es1371_probe(struct pci_dev *pcidev, const struct pci_devic
                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 "es1371: architecture does not support 32bit PCI busmaster DMA\n");
                return i;
@@ -2821,7 +2885,7 @@ static int __devinit es1371_probe(struct pci_dev *pcidev, const struct pci_devic
        init_waitqueue_head(&s->open_wait);
        init_waitqueue_head(&s->midi.iwait);
        init_waitqueue_head(&s->midi.owait);
-       init_MUTEX(&s->open_sem);
+       mutex_init(&s->open_mutex);
        spin_lock_init(&s->lock);
        s->magic = ES1371_MAGIC;
        s->dev = pcidev;
@@ -2845,8 +2909,8 @@ static int __devinit es1371_probe(struct pci_dev *pcidev, const struct pci_devic
                printk(KERN_ERR PFX "irq %u in use\n", s->irq);
                goto err_irq;
        }
-       printk(KERN_INFO PFX "found es1371 rev %d at io %#lx irq %u joystick %#x\n",
-              s->rev, s->io, s->irq, s->gameport.io);
+       printk(KERN_INFO PFX "found es1371 rev %d at io %#lx irq %u\n",
+              s->rev, s->io, s->irq);
        /* register devices */
        if ((res=(s->dev_audio = register_sound_dsp(&es1371_audio_fops,-1)))<0)
                goto err_dev1;
@@ -2877,16 +2941,6 @@ static int __devinit es1371_probe(struct pci_dev *pcidev, const struct pci_devic
                        printk(KERN_INFO PFX "Enabling internal amplifier.\n");
                }
        }
-       s->gameport.io = 0;
-       for (i = 0x218; i >= 0x200; i -= 0x08) {
-               if (request_region(i, JOY_EXTENT, "es1371")) {
-                       s->ctrl |= CTRL_JYSTK_EN | (((i >> 3) & CTRL_JOY_MASK) << CTRL_JOY_SHIFT);
-                       s->gameport.io = i;
-                       break;
-               }
-       }
-       if (!s->gameport.io)
-               printk(KERN_ERR PFX "no free joystick address found\n");
 
        s->sctrl = 0;
        cssr = 0;
@@ -2956,9 +3010,9 @@ static int __devinit es1371_probe(struct pci_dev *pcidev, const struct pci_devic
        set_fs(fs);
        /* turn on S/PDIF output driver if requested */
        outl(cssr, s->io+ES1371_REG_STATUS);
-       /* register gameport */
-       if (s->gameport.io)
-               gameport_register_port(&s->gameport);
+
+       es1371_register_gameport(s);
+
        /* store it in the driver field */
        pci_set_drvdata(pcidev, s);
        /* put it into driver list */
@@ -2966,11 +3020,9 @@ static int __devinit es1371_probe(struct pci_dev *pcidev, const struct pci_devic
        /* increment devindex */
        if (devindex < NR_DEVICE-1)
                devindex++;
-               return 0;
+       return 0;
 
  err_gp:
-       if (s->gameport.io)
-               release_region(s->gameport.io, JOY_EXTENT);
 #ifdef ES1371_DEBUG
        if (s->ps)
                remove_proc_entry("es1371", NULL);
@@ -3009,10 +3061,7 @@ static void __devexit es1371_remove(struct pci_dev *dev)
        outl(0, s->io+ES1371_REG_SERIAL_CONTROL); /* clear serial interrupts */
        synchronize_irq(s->irq);
        free_irq(s->irq, s);
-       if (s->gameport.io) {
-               gameport_unregister_port(&s->gameport);
-               release_region(s->gameport.io, JOY_EXTENT);
-       }
+       es1371_unregister_gameport(s);
        release_region(s->io, ES1371_EXTENT);
        unregister_sound_dsp(s->dev_audio);
        unregister_sound_mixer(s->codec->dev_mixer);
@@ -3042,7 +3091,7 @@ static struct pci_driver es1371_driver = {
 static int __init init_es1371(void)
 {
        printk(KERN_INFO PFX "version v0.32 time " __TIME__ " " __DATE__ "\n");
-       return pci_module_init(&es1371_driver);
+       return pci_register_driver(&es1371_driver);
 }
 
 static void __exit cleanup_es1371(void)