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