| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Copyright (C) 2005-2007 Jiri Slaby <jirislaby@gmail.com> | 
|  | 3 | * | 
|  | 4 | *  This program is free software; you can redistribute it and/or modify | 
|  | 5 | *  it under the terms of the GNU General Public License as published by | 
|  | 6 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 7 | *  (at your option) any later version. | 
|  | 8 | * | 
| Frederik Schwarzer | 0211a9c | 2008-12-29 22:14:56 +0100 | [diff] [blame] | 9 | *  You need a userspace library to cooperate with this driver. It (and other | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 10 | *  info) may be obtained here: | 
|  | 11 | *  http://www.fi.muni.cz/~xslaby/phantom.html | 
| Jiri Slaby | b2afe33 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 12 | *  or alternatively, you might use OpenHaptics provided by Sensable. | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 13 | */ | 
|  | 14 |  | 
| Jiri Slaby | 7e4e8e6 | 2008-04-29 00:59:13 -0700 | [diff] [blame] | 15 | #include <linux/compat.h> | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> | 
|  | 17 | #include <linux/module.h> | 
|  | 18 | #include <linux/device.h> | 
|  | 19 | #include <linux/pci.h> | 
|  | 20 | #include <linux/fs.h> | 
|  | 21 | #include <linux/poll.h> | 
|  | 22 | #include <linux/interrupt.h> | 
|  | 23 | #include <linux/cdev.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 25 | #include <linux/phantom.h> | 
| Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 26 | #include <linux/sched.h> | 
| Jonathan Corbet | 4541b5e | 2008-05-16 14:03:05 -0600 | [diff] [blame] | 27 | #include <linux/smp_lock.h> | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | #include <asm/atomic.h> | 
|  | 30 | #include <asm/io.h> | 
|  | 31 |  | 
| Jiri Slaby | 82f5608 | 2008-02-06 01:38:03 -0800 | [diff] [blame] | 32 | #define PHANTOM_VERSION		"n0.9.8" | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | #define PHANTOM_MAX_MINORS	8 | 
|  | 35 |  | 
|  | 36 | #define PHN_IRQCTL		0x4c    /* irq control in caddr space */ | 
|  | 37 |  | 
|  | 38 | #define PHB_RUNNING		1 | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 39 | #define PHB_NOT_OH		2 | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | static struct class *phantom_class; | 
|  | 42 | static int phantom_major; | 
|  | 43 |  | 
|  | 44 | struct phantom_device { | 
|  | 45 | unsigned int opened; | 
|  | 46 | void __iomem *caddr; | 
|  | 47 | u32 __iomem *iaddr; | 
|  | 48 | u32 __iomem *oaddr; | 
|  | 49 | unsigned long status; | 
|  | 50 | atomic_t counter; | 
|  | 51 |  | 
|  | 52 | wait_queue_head_t wait; | 
|  | 53 | struct cdev cdev; | 
|  | 54 |  | 
|  | 55 | struct mutex open_lock; | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 56 | spinlock_t regs_lock; | 
|  | 57 |  | 
|  | 58 | /* used in NOT_OH mode */ | 
|  | 59 | struct phm_regs oregs; | 
|  | 60 | u32 ctl_reg; | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 61 | }; | 
|  | 62 |  | 
|  | 63 | static unsigned char phantom_devices[PHANTOM_MAX_MINORS]; | 
|  | 64 |  | 
|  | 65 | static int phantom_status(struct phantom_device *dev, unsigned long newstat) | 
|  | 66 | { | 
|  | 67 | pr_debug("phantom_status %lx %lx\n", dev->status, newstat); | 
|  | 68 |  | 
|  | 69 | if (!(dev->status & PHB_RUNNING) && (newstat & PHB_RUNNING)) { | 
|  | 70 | atomic_set(&dev->counter, 0); | 
|  | 71 | iowrite32(PHN_CTL_IRQ, dev->iaddr + PHN_CONTROL); | 
|  | 72 | iowrite32(0x43, dev->caddr + PHN_IRQCTL); | 
| Jiri Slaby | c8511f9 | 2007-05-23 13:57:59 -0700 | [diff] [blame] | 73 | ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */ | 
|  | 74 | } else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) { | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 75 | iowrite32(0, dev->caddr + PHN_IRQCTL); | 
| Jiri Slaby | c8511f9 | 2007-05-23 13:57:59 -0700 | [diff] [blame] | 76 | ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */ | 
|  | 77 | } | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | dev->status = newstat; | 
|  | 80 |  | 
|  | 81 | return 0; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | /* | 
|  | 85 | * File ops | 
|  | 86 | */ | 
|  | 87 |  | 
| Jiri Slaby | c15395c | 2007-05-23 13:57:58 -0700 | [diff] [blame] | 88 | static long phantom_ioctl(struct file *file, unsigned int cmd, | 
|  | 89 | unsigned long arg) | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 90 | { | 
|  | 91 | struct phantom_device *dev = file->private_data; | 
|  | 92 | struct phm_regs rs; | 
|  | 93 | struct phm_reg r; | 
|  | 94 | void __user *argp = (void __user *)arg; | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 95 | unsigned long flags; | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 96 | unsigned int i; | 
|  | 97 |  | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 98 | switch (cmd) { | 
| Jiri Slaby | 7e4e8e6 | 2008-04-29 00:59:13 -0700 | [diff] [blame] | 99 | case PHN_SETREG: | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 100 | case PHN_SET_REG: | 
|  | 101 | if (copy_from_user(&r, argp, sizeof(r))) | 
|  | 102 | return -EFAULT; | 
|  | 103 |  | 
|  | 104 | if (r.reg > 7) | 
|  | 105 | return -EINVAL; | 
|  | 106 |  | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 107 | spin_lock_irqsave(&dev->regs_lock, flags); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 108 | if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) && | 
| Jiri Slaby | c15395c | 2007-05-23 13:57:58 -0700 | [diff] [blame] | 109 | phantom_status(dev, dev->status | PHB_RUNNING)){ | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 110 | spin_unlock_irqrestore(&dev->regs_lock, flags); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 111 | return -ENODEV; | 
| Jiri Slaby | c15395c | 2007-05-23 13:57:58 -0700 | [diff] [blame] | 112 | } | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | pr_debug("phantom: writing %x to %u\n", r.value, r.reg); | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | /* preserve amp bit (don't allow to change it when in NOT_OH) */ | 
|  | 117 | if (r.reg == PHN_CONTROL && (dev->status & PHB_NOT_OH)) { | 
|  | 118 | r.value &= ~PHN_CTL_AMP; | 
|  | 119 | r.value |= dev->ctl_reg & PHN_CTL_AMP; | 
|  | 120 | dev->ctl_reg = r.value; | 
|  | 121 | } | 
|  | 122 |  | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 123 | iowrite32(r.value, dev->iaddr + r.reg); | 
| Jiri Slaby | c8511f9 | 2007-05-23 13:57:59 -0700 | [diff] [blame] | 124 | ioread32(dev->iaddr); /* PCI posting */ | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 125 |  | 
|  | 126 | if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ)) | 
|  | 127 | phantom_status(dev, dev->status & ~PHB_RUNNING); | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 128 | spin_unlock_irqrestore(&dev->regs_lock, flags); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 129 | break; | 
| Jiri Slaby | 7e4e8e6 | 2008-04-29 00:59:13 -0700 | [diff] [blame] | 130 | case PHN_SETREGS: | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 131 | case PHN_SET_REGS: | 
|  | 132 | if (copy_from_user(&rs, argp, sizeof(rs))) | 
|  | 133 | return -EFAULT; | 
|  | 134 |  | 
|  | 135 | pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask); | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 136 | spin_lock_irqsave(&dev->regs_lock, flags); | 
|  | 137 | if (dev->status & PHB_NOT_OH) | 
|  | 138 | memcpy(&dev->oregs, &rs, sizeof(rs)); | 
|  | 139 | else { | 
|  | 140 | u32 m = min(rs.count, 8U); | 
|  | 141 | for (i = 0; i < m; i++) | 
|  | 142 | if (rs.mask & BIT(i)) | 
|  | 143 | iowrite32(rs.values[i], dev->oaddr + i); | 
|  | 144 | ioread32(dev->iaddr); /* PCI posting */ | 
|  | 145 | } | 
|  | 146 | spin_unlock_irqrestore(&dev->regs_lock, flags); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 147 | break; | 
| Jiri Slaby | 7e4e8e6 | 2008-04-29 00:59:13 -0700 | [diff] [blame] | 148 | case PHN_GETREG: | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 149 | case PHN_GET_REG: | 
|  | 150 | if (copy_from_user(&r, argp, sizeof(r))) | 
|  | 151 | return -EFAULT; | 
|  | 152 |  | 
|  | 153 | if (r.reg > 7) | 
|  | 154 | return -EINVAL; | 
|  | 155 |  | 
|  | 156 | r.value = ioread32(dev->iaddr + r.reg); | 
|  | 157 |  | 
|  | 158 | if (copy_to_user(argp, &r, sizeof(r))) | 
|  | 159 | return -EFAULT; | 
|  | 160 | break; | 
| Jiri Slaby | 7e4e8e6 | 2008-04-29 00:59:13 -0700 | [diff] [blame] | 161 | case PHN_GETREGS: | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 162 | case PHN_GET_REGS: { | 
|  | 163 | u32 m; | 
|  | 164 |  | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 165 | if (copy_from_user(&rs, argp, sizeof(rs))) | 
|  | 166 | return -EFAULT; | 
|  | 167 |  | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 168 | m = min(rs.count, 8U); | 
|  | 169 |  | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 170 | pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask); | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 171 | spin_lock_irqsave(&dev->regs_lock, flags); | 
|  | 172 | for (i = 0; i < m; i++) | 
|  | 173 | if (rs.mask & BIT(i)) | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 174 | rs.values[i] = ioread32(dev->iaddr + i); | 
| Jiri Slaby | 7d4f9f0 | 2008-04-29 00:59:14 -0700 | [diff] [blame] | 175 | atomic_set(&dev->counter, 0); | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 176 | spin_unlock_irqrestore(&dev->regs_lock, flags); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | if (copy_to_user(argp, &rs, sizeof(rs))) | 
|  | 179 | return -EFAULT; | 
|  | 180 | break; | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 181 | } case PHN_NOT_OH: | 
|  | 182 | spin_lock_irqsave(&dev->regs_lock, flags); | 
|  | 183 | if (dev->status & PHB_RUNNING) { | 
|  | 184 | printk(KERN_ERR "phantom: you need to set NOT_OH " | 
|  | 185 | "before you start the device!\n"); | 
|  | 186 | spin_unlock_irqrestore(&dev->regs_lock, flags); | 
|  | 187 | return -EINVAL; | 
|  | 188 | } | 
|  | 189 | dev->status |= PHB_NOT_OH; | 
|  | 190 | spin_unlock_irqrestore(&dev->regs_lock, flags); | 
|  | 191 | break; | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 192 | default: | 
|  | 193 | return -ENOTTY; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | return 0; | 
|  | 197 | } | 
|  | 198 |  | 
| Jiri Slaby | 7e4e8e6 | 2008-04-29 00:59:13 -0700 | [diff] [blame] | 199 | #ifdef CONFIG_COMPAT | 
|  | 200 | static long phantom_compat_ioctl(struct file *filp, unsigned int cmd, | 
|  | 201 | unsigned long arg) | 
|  | 202 | { | 
|  | 203 | if (_IOC_NR(cmd) <= 3 && _IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { | 
|  | 204 | cmd &= ~(_IOC_SIZEMASK << _IOC_SIZESHIFT); | 
|  | 205 | cmd |= sizeof(void *) << _IOC_SIZESHIFT; | 
|  | 206 | } | 
|  | 207 | return phantom_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); | 
|  | 208 | } | 
|  | 209 | #else | 
|  | 210 | #define phantom_compat_ioctl NULL | 
|  | 211 | #endif | 
|  | 212 |  | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 213 | static int phantom_open(struct inode *inode, struct file *file) | 
|  | 214 | { | 
|  | 215 | struct phantom_device *dev = container_of(inode->i_cdev, | 
|  | 216 | struct phantom_device, cdev); | 
|  | 217 |  | 
| Jonathan Corbet | 4541b5e | 2008-05-16 14:03:05 -0600 | [diff] [blame] | 218 | lock_kernel(); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 219 | nonseekable_open(inode, file); | 
|  | 220 |  | 
| Jonathan Corbet | 4541b5e | 2008-05-16 14:03:05 -0600 | [diff] [blame] | 221 | if (mutex_lock_interruptible(&dev->open_lock)) { | 
|  | 222 | unlock_kernel(); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 223 | return -ERESTARTSYS; | 
| Jonathan Corbet | 4541b5e | 2008-05-16 14:03:05 -0600 | [diff] [blame] | 224 | } | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 225 |  | 
|  | 226 | if (dev->opened) { | 
|  | 227 | mutex_unlock(&dev->open_lock); | 
| Jonathan Corbet | 4541b5e | 2008-05-16 14:03:05 -0600 | [diff] [blame] | 228 | unlock_kernel(); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 229 | return -EINVAL; | 
|  | 230 | } | 
|  | 231 |  | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 232 | WARN_ON(dev->status & PHB_NOT_OH); | 
|  | 233 |  | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 234 | file->private_data = dev; | 
|  | 235 |  | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 236 | atomic_set(&dev->counter, 0); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 237 | dev->opened++; | 
|  | 238 | mutex_unlock(&dev->open_lock); | 
| Jonathan Corbet | 4541b5e | 2008-05-16 14:03:05 -0600 | [diff] [blame] | 239 | unlock_kernel(); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 240 | return 0; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | static int phantom_release(struct inode *inode, struct file *file) | 
|  | 244 | { | 
|  | 245 | struct phantom_device *dev = file->private_data; | 
|  | 246 |  | 
|  | 247 | mutex_lock(&dev->open_lock); | 
|  | 248 |  | 
|  | 249 | dev->opened = 0; | 
|  | 250 | phantom_status(dev, dev->status & ~PHB_RUNNING); | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 251 | dev->status &= ~PHB_NOT_OH; | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 252 |  | 
|  | 253 | mutex_unlock(&dev->open_lock); | 
|  | 254 |  | 
|  | 255 | return 0; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | static unsigned int phantom_poll(struct file *file, poll_table *wait) | 
|  | 259 | { | 
|  | 260 | struct phantom_device *dev = file->private_data; | 
|  | 261 | unsigned int mask = 0; | 
|  | 262 |  | 
|  | 263 | pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter)); | 
|  | 264 | poll_wait(file, &dev->wait, wait); | 
| Jiri Slaby | 7d4f9f0 | 2008-04-29 00:59:14 -0700 | [diff] [blame] | 265 |  | 
|  | 266 | if (!(dev->status & PHB_RUNNING)) | 
|  | 267 | mask = POLLERR; | 
|  | 268 | else if (atomic_read(&dev->counter)) | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 269 | mask = POLLIN | POLLRDNORM; | 
| Jiri Slaby | 7d4f9f0 | 2008-04-29 00:59:14 -0700 | [diff] [blame] | 270 |  | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 271 | pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter)); | 
|  | 272 |  | 
|  | 273 | return mask; | 
|  | 274 | } | 
|  | 275 |  | 
| Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 276 | static const struct file_operations phantom_file_ops = { | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 277 | .open = phantom_open, | 
|  | 278 | .release = phantom_release, | 
| Jiri Slaby | c15395c | 2007-05-23 13:57:58 -0700 | [diff] [blame] | 279 | .unlocked_ioctl = phantom_ioctl, | 
| Jiri Slaby | 7e4e8e6 | 2008-04-29 00:59:13 -0700 | [diff] [blame] | 280 | .compat_ioctl = phantom_compat_ioctl, | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 281 | .poll = phantom_poll, | 
|  | 282 | }; | 
|  | 283 |  | 
|  | 284 | static irqreturn_t phantom_isr(int irq, void *data) | 
|  | 285 | { | 
|  | 286 | struct phantom_device *dev = data; | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 287 | unsigned int i; | 
|  | 288 | u32 ctl; | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 289 |  | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 290 | spin_lock(&dev->regs_lock); | 
|  | 291 | ctl = ioread32(dev->iaddr + PHN_CONTROL); | 
|  | 292 | if (!(ctl & PHN_CTL_IRQ)) { | 
|  | 293 | spin_unlock(&dev->regs_lock); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 294 | return IRQ_NONE; | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 295 | } | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 296 |  | 
|  | 297 | iowrite32(0, dev->iaddr); | 
|  | 298 | iowrite32(0xc0, dev->iaddr); | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 299 |  | 
|  | 300 | if (dev->status & PHB_NOT_OH) { | 
|  | 301 | struct phm_regs *r = &dev->oregs; | 
|  | 302 | u32 m = min(r->count, 8U); | 
|  | 303 |  | 
|  | 304 | for (i = 0; i < m; i++) | 
|  | 305 | if (r->mask & BIT(i)) | 
|  | 306 | iowrite32(r->values[i], dev->oaddr + i); | 
|  | 307 |  | 
|  | 308 | dev->ctl_reg ^= PHN_CTL_AMP; | 
|  | 309 | iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL); | 
|  | 310 | } | 
|  | 311 | spin_unlock(&dev->regs_lock); | 
|  | 312 |  | 
| Jiri Slaby | c8511f9 | 2007-05-23 13:57:59 -0700 | [diff] [blame] | 313 | ioread32(dev->iaddr); /* PCI posting */ | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 314 |  | 
|  | 315 | atomic_inc(&dev->counter); | 
|  | 316 | wake_up_interruptible(&dev->wait); | 
|  | 317 |  | 
|  | 318 | return IRQ_HANDLED; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | /* | 
|  | 322 | * Init and deinit driver | 
|  | 323 | */ | 
|  | 324 |  | 
|  | 325 | static unsigned int __devinit phantom_get_free(void) | 
|  | 326 | { | 
|  | 327 | unsigned int i; | 
|  | 328 |  | 
|  | 329 | for (i = 0; i < PHANTOM_MAX_MINORS; i++) | 
|  | 330 | if (phantom_devices[i] == 0) | 
|  | 331 | break; | 
|  | 332 |  | 
|  | 333 | return i; | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | static int __devinit phantom_probe(struct pci_dev *pdev, | 
|  | 337 | const struct pci_device_id *pci_id) | 
|  | 338 | { | 
|  | 339 | struct phantom_device *pht; | 
|  | 340 | unsigned int minor; | 
|  | 341 | int retval; | 
|  | 342 |  | 
|  | 343 | retval = pci_enable_device(pdev); | 
|  | 344 | if (retval) | 
|  | 345 | goto err; | 
|  | 346 |  | 
|  | 347 | minor = phantom_get_free(); | 
|  | 348 | if (minor == PHANTOM_MAX_MINORS) { | 
|  | 349 | dev_err(&pdev->dev, "too many devices found!\n"); | 
|  | 350 | retval = -EIO; | 
|  | 351 | goto err_dis; | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | phantom_devices[minor] = 1; | 
|  | 355 |  | 
|  | 356 | retval = pci_request_regions(pdev, "phantom"); | 
|  | 357 | if (retval) | 
|  | 358 | goto err_null; | 
|  | 359 |  | 
|  | 360 | retval = -ENOMEM; | 
|  | 361 | pht = kzalloc(sizeof(*pht), GFP_KERNEL); | 
|  | 362 | if (pht == NULL) { | 
|  | 363 | dev_err(&pdev->dev, "unable to allocate device\n"); | 
|  | 364 | goto err_reg; | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | pht->caddr = pci_iomap(pdev, 0, 0); | 
|  | 368 | if (pht->caddr == NULL) { | 
|  | 369 | dev_err(&pdev->dev, "can't remap conf space\n"); | 
|  | 370 | goto err_fr; | 
|  | 371 | } | 
|  | 372 | pht->iaddr = pci_iomap(pdev, 2, 0); | 
|  | 373 | if (pht->iaddr == NULL) { | 
|  | 374 | dev_err(&pdev->dev, "can't remap input space\n"); | 
|  | 375 | goto err_unmc; | 
|  | 376 | } | 
|  | 377 | pht->oaddr = pci_iomap(pdev, 3, 0); | 
|  | 378 | if (pht->oaddr == NULL) { | 
|  | 379 | dev_err(&pdev->dev, "can't remap output space\n"); | 
|  | 380 | goto err_unmi; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | mutex_init(&pht->open_lock); | 
| Jiri Slaby | bc552f7 | 2007-10-18 23:40:24 -0700 | [diff] [blame] | 384 | spin_lock_init(&pht->regs_lock); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 385 | init_waitqueue_head(&pht->wait); | 
|  | 386 | cdev_init(&pht->cdev, &phantom_file_ops); | 
|  | 387 | pht->cdev.owner = THIS_MODULE; | 
|  | 388 |  | 
|  | 389 | iowrite32(0, pht->caddr + PHN_IRQCTL); | 
| Jiri Slaby | c8511f9 | 2007-05-23 13:57:59 -0700 | [diff] [blame] | 390 | ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */ | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 391 | retval = request_irq(pdev->irq, phantom_isr, | 
|  | 392 | IRQF_SHARED | IRQF_DISABLED, "phantom", pht); | 
|  | 393 | if (retval) { | 
|  | 394 | dev_err(&pdev->dev, "can't establish ISR\n"); | 
|  | 395 | goto err_unmo; | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1); | 
|  | 399 | if (retval) { | 
|  | 400 | dev_err(&pdev->dev, "chardev registration failed\n"); | 
|  | 401 | goto err_irq; | 
|  | 402 | } | 
|  | 403 |  | 
| Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 404 | if (IS_ERR(device_create(phantom_class, &pdev->dev, | 
|  | 405 | MKDEV(phantom_major, minor), NULL, | 
|  | 406 | "phantom%u", minor))) | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 407 | dev_err(&pdev->dev, "can't create device\n"); | 
|  | 408 |  | 
|  | 409 | pci_set_drvdata(pdev, pht); | 
|  | 410 |  | 
|  | 411 | return 0; | 
|  | 412 | err_irq: | 
|  | 413 | free_irq(pdev->irq, pht); | 
|  | 414 | err_unmo: | 
|  | 415 | pci_iounmap(pdev, pht->oaddr); | 
|  | 416 | err_unmi: | 
|  | 417 | pci_iounmap(pdev, pht->iaddr); | 
|  | 418 | err_unmc: | 
|  | 419 | pci_iounmap(pdev, pht->caddr); | 
|  | 420 | err_fr: | 
|  | 421 | kfree(pht); | 
|  | 422 | err_reg: | 
|  | 423 | pci_release_regions(pdev); | 
|  | 424 | err_null: | 
|  | 425 | phantom_devices[minor] = 0; | 
|  | 426 | err_dis: | 
|  | 427 | pci_disable_device(pdev); | 
|  | 428 | err: | 
|  | 429 | return retval; | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | static void __devexit phantom_remove(struct pci_dev *pdev) | 
|  | 433 | { | 
|  | 434 | struct phantom_device *pht = pci_get_drvdata(pdev); | 
|  | 435 | unsigned int minor = MINOR(pht->cdev.dev); | 
|  | 436 |  | 
|  | 437 | device_destroy(phantom_class, MKDEV(phantom_major, minor)); | 
|  | 438 |  | 
|  | 439 | cdev_del(&pht->cdev); | 
|  | 440 |  | 
|  | 441 | iowrite32(0, pht->caddr + PHN_IRQCTL); | 
| Jiri Slaby | c8511f9 | 2007-05-23 13:57:59 -0700 | [diff] [blame] | 442 | ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */ | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 443 | free_irq(pdev->irq, pht); | 
|  | 444 |  | 
|  | 445 | pci_iounmap(pdev, pht->oaddr); | 
|  | 446 | pci_iounmap(pdev, pht->iaddr); | 
|  | 447 | pci_iounmap(pdev, pht->caddr); | 
|  | 448 |  | 
|  | 449 | kfree(pht); | 
|  | 450 |  | 
|  | 451 | pci_release_regions(pdev); | 
|  | 452 |  | 
|  | 453 | phantom_devices[minor] = 0; | 
|  | 454 |  | 
|  | 455 | pci_disable_device(pdev); | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | #ifdef CONFIG_PM | 
|  | 459 | static int phantom_suspend(struct pci_dev *pdev, pm_message_t state) | 
|  | 460 | { | 
|  | 461 | struct phantom_device *dev = pci_get_drvdata(pdev); | 
|  | 462 |  | 
|  | 463 | iowrite32(0, dev->caddr + PHN_IRQCTL); | 
| Jiri Slaby | c8511f9 | 2007-05-23 13:57:59 -0700 | [diff] [blame] | 464 | ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */ | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 465 |  | 
| Jiri Slaby | aee8447 | 2007-10-18 23:40:23 -0700 | [diff] [blame] | 466 | synchronize_irq(pdev->irq); | 
|  | 467 |  | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 468 | return 0; | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | static int phantom_resume(struct pci_dev *pdev) | 
|  | 472 | { | 
|  | 473 | struct phantom_device *dev = pci_get_drvdata(pdev); | 
|  | 474 |  | 
|  | 475 | iowrite32(0, dev->caddr + PHN_IRQCTL); | 
|  | 476 |  | 
|  | 477 | return 0; | 
|  | 478 | } | 
|  | 479 | #else | 
|  | 480 | #define phantom_suspend	NULL | 
|  | 481 | #define phantom_resume	NULL | 
|  | 482 | #endif | 
|  | 483 |  | 
|  | 484 | static struct pci_device_id phantom_pci_tbl[] __devinitdata = { | 
| Jiri Slaby | 82f5608 | 2008-02-06 01:38:03 -0800 | [diff] [blame] | 485 | { .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050, | 
|  | 486 | .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050, | 
|  | 487 | .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 }, | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 488 | { 0, } | 
|  | 489 | }; | 
|  | 490 | MODULE_DEVICE_TABLE(pci, phantom_pci_tbl); | 
|  | 491 |  | 
|  | 492 | static struct pci_driver phantom_pci_driver = { | 
|  | 493 | .name = "phantom", | 
|  | 494 | .id_table = phantom_pci_tbl, | 
|  | 495 | .probe = phantom_probe, | 
|  | 496 | .remove = __devexit_p(phantom_remove), | 
|  | 497 | .suspend = phantom_suspend, | 
|  | 498 | .resume = phantom_resume | 
|  | 499 | }; | 
|  | 500 |  | 
| Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 501 | static CLASS_ATTR_STRING(version, 0444, PHANTOM_VERSION); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 502 |  | 
|  | 503 | static int __init phantom_init(void) | 
|  | 504 | { | 
|  | 505 | int retval; | 
|  | 506 | dev_t dev; | 
|  | 507 |  | 
|  | 508 | phantom_class = class_create(THIS_MODULE, "phantom"); | 
|  | 509 | if (IS_ERR(phantom_class)) { | 
|  | 510 | retval = PTR_ERR(phantom_class); | 
|  | 511 | printk(KERN_ERR "phantom: can't register phantom class\n"); | 
|  | 512 | goto err; | 
|  | 513 | } | 
| Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 514 | retval = class_create_file(phantom_class, &class_attr_version.attr); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 515 | if (retval) { | 
|  | 516 | printk(KERN_ERR "phantom: can't create sysfs version file\n"); | 
|  | 517 | goto err_class; | 
|  | 518 | } | 
|  | 519 |  | 
|  | 520 | retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom"); | 
|  | 521 | if (retval) { | 
|  | 522 | printk(KERN_ERR "phantom: can't register character device\n"); | 
|  | 523 | goto err_attr; | 
|  | 524 | } | 
|  | 525 | phantom_major = MAJOR(dev); | 
|  | 526 |  | 
|  | 527 | retval = pci_register_driver(&phantom_pci_driver); | 
|  | 528 | if (retval) { | 
|  | 529 | printk(KERN_ERR "phantom: can't register pci driver\n"); | 
|  | 530 | goto err_unchr; | 
|  | 531 | } | 
|  | 532 |  | 
|  | 533 | printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", " | 
|  | 534 | "init OK\n"); | 
|  | 535 |  | 
|  | 536 | return 0; | 
|  | 537 | err_unchr: | 
|  | 538 | unregister_chrdev_region(dev, PHANTOM_MAX_MINORS); | 
|  | 539 | err_attr: | 
| Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 540 | class_remove_file(phantom_class, &class_attr_version.attr); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 541 | err_class: | 
|  | 542 | class_destroy(phantom_class); | 
|  | 543 | err: | 
|  | 544 | return retval; | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | static void __exit phantom_exit(void) | 
|  | 548 | { | 
|  | 549 | pci_unregister_driver(&phantom_pci_driver); | 
|  | 550 |  | 
|  | 551 | unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS); | 
|  | 552 |  | 
| Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 553 | class_remove_file(phantom_class, &class_attr_version.attr); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 554 | class_destroy(phantom_class); | 
|  | 555 |  | 
|  | 556 | pr_debug("phantom: module successfully removed\n"); | 
|  | 557 | } | 
|  | 558 |  | 
|  | 559 | module_init(phantom_init); | 
|  | 560 | module_exit(phantom_exit); | 
|  | 561 |  | 
|  | 562 | MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>"); | 
| Jiri Slaby | ec905a1 | 2008-07-25 01:48:23 -0700 | [diff] [blame] | 563 | MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)"); | 
| Jiri Slaby | cef2cf0 | 2007-05-08 00:31:45 -0700 | [diff] [blame] | 564 | MODULE_LICENSE("GPL"); | 
|  | 565 | MODULE_VERSION(PHANTOM_VERSION); |