0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/compat.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/device.h>
0015 #include <linux/pci.h>
0016 #include <linux/fs.h>
0017 #include <linux/poll.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/cdev.h>
0020 #include <linux/slab.h>
0021 #include <linux/phantom.h>
0022 #include <linux/sched.h>
0023 #include <linux/mutex.h>
0024
0025 #include <linux/atomic.h>
0026 #include <asm/io.h>
0027
0028 #define PHANTOM_VERSION "n0.9.8"
0029
0030 #define PHANTOM_MAX_MINORS 8
0031
0032 #define PHN_IRQCTL 0x4c
0033
0034 #define PHB_RUNNING 1
0035 #define PHB_NOT_OH 2
0036
0037 static DEFINE_MUTEX(phantom_mutex);
0038 static struct class *phantom_class;
0039 static int phantom_major;
0040
0041 struct phantom_device {
0042 unsigned int opened;
0043 void __iomem *caddr;
0044 u32 __iomem *iaddr;
0045 u32 __iomem *oaddr;
0046 unsigned long status;
0047 atomic_t counter;
0048
0049 wait_queue_head_t wait;
0050 struct cdev cdev;
0051
0052 struct mutex open_lock;
0053 spinlock_t regs_lock;
0054
0055
0056 struct phm_regs oregs;
0057 u32 ctl_reg;
0058 };
0059
0060 static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
0061
0062 static int phantom_status(struct phantom_device *dev, unsigned long newstat)
0063 {
0064 pr_debug("phantom_status %lx %lx\n", dev->status, newstat);
0065
0066 if (!(dev->status & PHB_RUNNING) && (newstat & PHB_RUNNING)) {
0067 atomic_set(&dev->counter, 0);
0068 iowrite32(PHN_CTL_IRQ, dev->iaddr + PHN_CONTROL);
0069 iowrite32(0x43, dev->caddr + PHN_IRQCTL);
0070 ioread32(dev->caddr + PHN_IRQCTL);
0071 } else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) {
0072 iowrite32(0, dev->caddr + PHN_IRQCTL);
0073 ioread32(dev->caddr + PHN_IRQCTL);
0074 }
0075
0076 dev->status = newstat;
0077
0078 return 0;
0079 }
0080
0081
0082
0083
0084
0085 static long phantom_ioctl(struct file *file, unsigned int cmd,
0086 unsigned long arg)
0087 {
0088 struct phantom_device *dev = file->private_data;
0089 struct phm_regs rs;
0090 struct phm_reg r;
0091 void __user *argp = (void __user *)arg;
0092 unsigned long flags;
0093 unsigned int i;
0094
0095 switch (cmd) {
0096 case PHN_SETREG:
0097 case PHN_SET_REG:
0098 if (copy_from_user(&r, argp, sizeof(r)))
0099 return -EFAULT;
0100
0101 if (r.reg > 7)
0102 return -EINVAL;
0103
0104 spin_lock_irqsave(&dev->regs_lock, flags);
0105 if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) &&
0106 phantom_status(dev, dev->status | PHB_RUNNING)){
0107 spin_unlock_irqrestore(&dev->regs_lock, flags);
0108 return -ENODEV;
0109 }
0110
0111 pr_debug("phantom: writing %x to %u\n", r.value, r.reg);
0112
0113
0114 if (r.reg == PHN_CONTROL && (dev->status & PHB_NOT_OH)) {
0115 r.value &= ~PHN_CTL_AMP;
0116 r.value |= dev->ctl_reg & PHN_CTL_AMP;
0117 dev->ctl_reg = r.value;
0118 }
0119
0120 iowrite32(r.value, dev->iaddr + r.reg);
0121 ioread32(dev->iaddr);
0122
0123 if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ))
0124 phantom_status(dev, dev->status & ~PHB_RUNNING);
0125 spin_unlock_irqrestore(&dev->regs_lock, flags);
0126 break;
0127 case PHN_SETREGS:
0128 case PHN_SET_REGS:
0129 if (copy_from_user(&rs, argp, sizeof(rs)))
0130 return -EFAULT;
0131
0132 pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
0133 spin_lock_irqsave(&dev->regs_lock, flags);
0134 if (dev->status & PHB_NOT_OH)
0135 memcpy(&dev->oregs, &rs, sizeof(rs));
0136 else {
0137 u32 m = min(rs.count, 8U);
0138 for (i = 0; i < m; i++)
0139 if (rs.mask & BIT(i))
0140 iowrite32(rs.values[i], dev->oaddr + i);
0141 ioread32(dev->iaddr);
0142 }
0143 spin_unlock_irqrestore(&dev->regs_lock, flags);
0144 break;
0145 case PHN_GETREG:
0146 case PHN_GET_REG:
0147 if (copy_from_user(&r, argp, sizeof(r)))
0148 return -EFAULT;
0149
0150 if (r.reg > 7)
0151 return -EINVAL;
0152
0153 r.value = ioread32(dev->iaddr + r.reg);
0154
0155 if (copy_to_user(argp, &r, sizeof(r)))
0156 return -EFAULT;
0157 break;
0158 case PHN_GETREGS:
0159 case PHN_GET_REGS: {
0160 u32 m;
0161
0162 if (copy_from_user(&rs, argp, sizeof(rs)))
0163 return -EFAULT;
0164
0165 m = min(rs.count, 8U);
0166
0167 pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
0168 spin_lock_irqsave(&dev->regs_lock, flags);
0169 for (i = 0; i < m; i++)
0170 if (rs.mask & BIT(i))
0171 rs.values[i] = ioread32(dev->iaddr + i);
0172 atomic_set(&dev->counter, 0);
0173 spin_unlock_irqrestore(&dev->regs_lock, flags);
0174
0175 if (copy_to_user(argp, &rs, sizeof(rs)))
0176 return -EFAULT;
0177 break;
0178 } case PHN_NOT_OH:
0179 spin_lock_irqsave(&dev->regs_lock, flags);
0180 if (dev->status & PHB_RUNNING) {
0181 printk(KERN_ERR "phantom: you need to set NOT_OH "
0182 "before you start the device!\n");
0183 spin_unlock_irqrestore(&dev->regs_lock, flags);
0184 return -EINVAL;
0185 }
0186 dev->status |= PHB_NOT_OH;
0187 spin_unlock_irqrestore(&dev->regs_lock, flags);
0188 break;
0189 default:
0190 return -ENOTTY;
0191 }
0192
0193 return 0;
0194 }
0195
0196 #ifdef CONFIG_COMPAT
0197 static long phantom_compat_ioctl(struct file *filp, unsigned int cmd,
0198 unsigned long arg)
0199 {
0200 if (_IOC_NR(cmd) <= 3 && _IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
0201 cmd &= ~(_IOC_SIZEMASK << _IOC_SIZESHIFT);
0202 cmd |= sizeof(void *) << _IOC_SIZESHIFT;
0203 }
0204 return phantom_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
0205 }
0206 #else
0207 #define phantom_compat_ioctl NULL
0208 #endif
0209
0210 static int phantom_open(struct inode *inode, struct file *file)
0211 {
0212 struct phantom_device *dev = container_of(inode->i_cdev,
0213 struct phantom_device, cdev);
0214
0215 mutex_lock(&phantom_mutex);
0216 nonseekable_open(inode, file);
0217
0218 if (mutex_lock_interruptible(&dev->open_lock)) {
0219 mutex_unlock(&phantom_mutex);
0220 return -ERESTARTSYS;
0221 }
0222
0223 if (dev->opened) {
0224 mutex_unlock(&dev->open_lock);
0225 mutex_unlock(&phantom_mutex);
0226 return -EINVAL;
0227 }
0228
0229 WARN_ON(dev->status & PHB_NOT_OH);
0230
0231 file->private_data = dev;
0232
0233 atomic_set(&dev->counter, 0);
0234 dev->opened++;
0235 mutex_unlock(&dev->open_lock);
0236 mutex_unlock(&phantom_mutex);
0237 return 0;
0238 }
0239
0240 static int phantom_release(struct inode *inode, struct file *file)
0241 {
0242 struct phantom_device *dev = file->private_data;
0243
0244 mutex_lock(&dev->open_lock);
0245
0246 dev->opened = 0;
0247 phantom_status(dev, dev->status & ~PHB_RUNNING);
0248 dev->status &= ~PHB_NOT_OH;
0249
0250 mutex_unlock(&dev->open_lock);
0251
0252 return 0;
0253 }
0254
0255 static __poll_t phantom_poll(struct file *file, poll_table *wait)
0256 {
0257 struct phantom_device *dev = file->private_data;
0258 __poll_t mask = 0;
0259
0260 pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
0261 poll_wait(file, &dev->wait, wait);
0262
0263 if (!(dev->status & PHB_RUNNING))
0264 mask = EPOLLERR;
0265 else if (atomic_read(&dev->counter))
0266 mask = EPOLLIN | EPOLLRDNORM;
0267
0268 pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
0269
0270 return mask;
0271 }
0272
0273 static const struct file_operations phantom_file_ops = {
0274 .open = phantom_open,
0275 .release = phantom_release,
0276 .unlocked_ioctl = phantom_ioctl,
0277 .compat_ioctl = phantom_compat_ioctl,
0278 .poll = phantom_poll,
0279 .llseek = no_llseek,
0280 };
0281
0282 static irqreturn_t phantom_isr(int irq, void *data)
0283 {
0284 struct phantom_device *dev = data;
0285 unsigned int i;
0286 u32 ctl;
0287
0288 spin_lock(&dev->regs_lock);
0289 ctl = ioread32(dev->iaddr + PHN_CONTROL);
0290 if (!(ctl & PHN_CTL_IRQ)) {
0291 spin_unlock(&dev->regs_lock);
0292 return IRQ_NONE;
0293 }
0294
0295 iowrite32(0, dev->iaddr);
0296 iowrite32(0xc0, dev->iaddr);
0297
0298 if (dev->status & PHB_NOT_OH) {
0299 struct phm_regs *r = &dev->oregs;
0300 u32 m = min(r->count, 8U);
0301
0302 for (i = 0; i < m; i++)
0303 if (r->mask & BIT(i))
0304 iowrite32(r->values[i], dev->oaddr + i);
0305
0306 dev->ctl_reg ^= PHN_CTL_AMP;
0307 iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
0308 }
0309 spin_unlock(&dev->regs_lock);
0310
0311 ioread32(dev->iaddr);
0312
0313 atomic_inc(&dev->counter);
0314 wake_up_interruptible(&dev->wait);
0315
0316 return IRQ_HANDLED;
0317 }
0318
0319
0320
0321
0322
0323 static unsigned int phantom_get_free(void)
0324 {
0325 unsigned int i;
0326
0327 for (i = 0; i < PHANTOM_MAX_MINORS; i++)
0328 if (phantom_devices[i] == 0)
0329 break;
0330
0331 return i;
0332 }
0333
0334 static int phantom_probe(struct pci_dev *pdev,
0335 const struct pci_device_id *pci_id)
0336 {
0337 struct phantom_device *pht;
0338 unsigned int minor;
0339 int retval;
0340
0341 retval = pci_enable_device(pdev);
0342 if (retval) {
0343 dev_err(&pdev->dev, "pci_enable_device failed!\n");
0344 goto err;
0345 }
0346
0347 minor = phantom_get_free();
0348 if (minor == PHANTOM_MAX_MINORS) {
0349 dev_err(&pdev->dev, "too many devices found!\n");
0350 retval = -EIO;
0351 goto err_dis;
0352 }
0353
0354 phantom_devices[minor] = 1;
0355
0356 retval = pci_request_regions(pdev, "phantom");
0357 if (retval) {
0358 dev_err(&pdev->dev, "pci_request_regions failed!\n");
0359 goto err_null;
0360 }
0361
0362 retval = -ENOMEM;
0363 pht = kzalloc(sizeof(*pht), GFP_KERNEL);
0364 if (pht == NULL) {
0365 dev_err(&pdev->dev, "unable to allocate device\n");
0366 goto err_reg;
0367 }
0368
0369 pht->caddr = pci_iomap(pdev, 0, 0);
0370 if (pht->caddr == NULL) {
0371 dev_err(&pdev->dev, "can't remap conf space\n");
0372 goto err_fr;
0373 }
0374 pht->iaddr = pci_iomap(pdev, 2, 0);
0375 if (pht->iaddr == NULL) {
0376 dev_err(&pdev->dev, "can't remap input space\n");
0377 goto err_unmc;
0378 }
0379 pht->oaddr = pci_iomap(pdev, 3, 0);
0380 if (pht->oaddr == NULL) {
0381 dev_err(&pdev->dev, "can't remap output space\n");
0382 goto err_unmi;
0383 }
0384
0385 mutex_init(&pht->open_lock);
0386 spin_lock_init(&pht->regs_lock);
0387 init_waitqueue_head(&pht->wait);
0388 cdev_init(&pht->cdev, &phantom_file_ops);
0389 pht->cdev.owner = THIS_MODULE;
0390
0391 iowrite32(0, pht->caddr + PHN_IRQCTL);
0392 ioread32(pht->caddr + PHN_IRQCTL);
0393 retval = request_irq(pdev->irq, phantom_isr,
0394 IRQF_SHARED, "phantom", pht);
0395 if (retval) {
0396 dev_err(&pdev->dev, "can't establish ISR\n");
0397 goto err_unmo;
0398 }
0399
0400 retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
0401 if (retval) {
0402 dev_err(&pdev->dev, "chardev registration failed\n");
0403 goto err_irq;
0404 }
0405
0406 if (IS_ERR(device_create(phantom_class, &pdev->dev,
0407 MKDEV(phantom_major, minor), NULL,
0408 "phantom%u", minor)))
0409 dev_err(&pdev->dev, "can't create device\n");
0410
0411 pci_set_drvdata(pdev, pht);
0412
0413 return 0;
0414 err_irq:
0415 free_irq(pdev->irq, pht);
0416 err_unmo:
0417 pci_iounmap(pdev, pht->oaddr);
0418 err_unmi:
0419 pci_iounmap(pdev, pht->iaddr);
0420 err_unmc:
0421 pci_iounmap(pdev, pht->caddr);
0422 err_fr:
0423 kfree(pht);
0424 err_reg:
0425 pci_release_regions(pdev);
0426 err_null:
0427 phantom_devices[minor] = 0;
0428 err_dis:
0429 pci_disable_device(pdev);
0430 err:
0431 return retval;
0432 }
0433
0434 static void phantom_remove(struct pci_dev *pdev)
0435 {
0436 struct phantom_device *pht = pci_get_drvdata(pdev);
0437 unsigned int minor = MINOR(pht->cdev.dev);
0438
0439 device_destroy(phantom_class, MKDEV(phantom_major, minor));
0440
0441 cdev_del(&pht->cdev);
0442
0443 iowrite32(0, pht->caddr + PHN_IRQCTL);
0444 ioread32(pht->caddr + PHN_IRQCTL);
0445 free_irq(pdev->irq, pht);
0446
0447 pci_iounmap(pdev, pht->oaddr);
0448 pci_iounmap(pdev, pht->iaddr);
0449 pci_iounmap(pdev, pht->caddr);
0450
0451 kfree(pht);
0452
0453 pci_release_regions(pdev);
0454
0455 phantom_devices[minor] = 0;
0456
0457 pci_disable_device(pdev);
0458 }
0459
0460 static int __maybe_unused phantom_suspend(struct device *dev_d)
0461 {
0462 struct phantom_device *dev = dev_get_drvdata(dev_d);
0463
0464 iowrite32(0, dev->caddr + PHN_IRQCTL);
0465 ioread32(dev->caddr + PHN_IRQCTL);
0466
0467 synchronize_irq(to_pci_dev(dev_d)->irq);
0468
0469 return 0;
0470 }
0471
0472 static int __maybe_unused phantom_resume(struct device *dev_d)
0473 {
0474 struct phantom_device *dev = dev_get_drvdata(dev_d);
0475
0476 iowrite32(0, dev->caddr + PHN_IRQCTL);
0477
0478 return 0;
0479 }
0480
0481 static struct pci_device_id phantom_pci_tbl[] = {
0482 { .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
0483 .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
0484 .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
0485 { 0, }
0486 };
0487 MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
0488
0489 static SIMPLE_DEV_PM_OPS(phantom_pm_ops, phantom_suspend, phantom_resume);
0490
0491 static struct pci_driver phantom_pci_driver = {
0492 .name = "phantom",
0493 .id_table = phantom_pci_tbl,
0494 .probe = phantom_probe,
0495 .remove = phantom_remove,
0496 .driver.pm = &phantom_pm_ops,
0497 };
0498
0499 static CLASS_ATTR_STRING(version, 0444, PHANTOM_VERSION);
0500
0501 static int __init phantom_init(void)
0502 {
0503 int retval;
0504 dev_t dev;
0505
0506 phantom_class = class_create(THIS_MODULE, "phantom");
0507 if (IS_ERR(phantom_class)) {
0508 retval = PTR_ERR(phantom_class);
0509 printk(KERN_ERR "phantom: can't register phantom class\n");
0510 goto err;
0511 }
0512 retval = class_create_file(phantom_class, &class_attr_version.attr);
0513 if (retval) {
0514 printk(KERN_ERR "phantom: can't create sysfs version file\n");
0515 goto err_class;
0516 }
0517
0518 retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
0519 if (retval) {
0520 printk(KERN_ERR "phantom: can't register character device\n");
0521 goto err_attr;
0522 }
0523 phantom_major = MAJOR(dev);
0524
0525 retval = pci_register_driver(&phantom_pci_driver);
0526 if (retval) {
0527 printk(KERN_ERR "phantom: can't register pci driver\n");
0528 goto err_unchr;
0529 }
0530
0531 printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
0532 "init OK\n");
0533
0534 return 0;
0535 err_unchr:
0536 unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
0537 err_attr:
0538 class_remove_file(phantom_class, &class_attr_version.attr);
0539 err_class:
0540 class_destroy(phantom_class);
0541 err:
0542 return retval;
0543 }
0544
0545 static void __exit phantom_exit(void)
0546 {
0547 pci_unregister_driver(&phantom_pci_driver);
0548
0549 unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
0550
0551 class_remove_file(phantom_class, &class_attr_version.attr);
0552 class_destroy(phantom_class);
0553
0554 pr_debug("phantom: module successfully removed\n");
0555 }
0556
0557 module_init(phantom_init);
0558 module_exit(phantom_exit);
0559
0560 MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
0561 MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
0562 MODULE_LICENSE("GPL");
0563 MODULE_VERSION(PHANTOM_VERSION);