1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2005-2007 Jiri Slaby <jirislaby@gmail.com>
5 * You need a userspace library to cooperate with this driver. It (and other
6 * info) may be obtained here:
7 * http://www.fi.muni.cz/~xslaby/phantom.html
8 * or alternatively, you might use OpenHaptics provided by Sensable.
11 #include <linux/compat.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/pci.h>
17 #include <linux/poll.h>
18 #include <linux/interrupt.h>
19 #include <linux/cdev.h>
20 #include <linux/slab.h>
21 #include <linux/phantom.h>
22 #include <linux/sched.h>
23 #include <linux/mutex.h>
25 #include <linux/atomic.h>
28 #define PHANTOM_VERSION "n0.9.8"
30 #define PHANTOM_MAX_MINORS 8
32 #define PHN_IRQCTL 0x4c /* irq control in caddr space */
37 static DEFINE_MUTEX(phantom_mutex);
38 static struct class *phantom_class;
39 static int phantom_major;
41 struct phantom_device {
49 wait_queue_head_t wait;
52 struct mutex open_lock;
55 /* used in NOT_OH mode */
56 struct phm_regs oregs;
60 static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
62 static int phantom_status(struct phantom_device *dev, unsigned long newstat)
64 pr_debug("phantom_status %lx %lx\n", dev->status, newstat);
66 if (!(dev->status & PHB_RUNNING) && (newstat & PHB_RUNNING)) {
67 atomic_set(&dev->counter, 0);
68 iowrite32(PHN_CTL_IRQ, dev->iaddr + PHN_CONTROL);
69 iowrite32(0x43, dev->caddr + PHN_IRQCTL);
70 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
71 } else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) {
72 iowrite32(0, dev->caddr + PHN_IRQCTL);
73 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
76 dev->status = newstat;
85 static long phantom_ioctl(struct file *file, unsigned int cmd,
88 struct phantom_device *dev = file->private_data;
91 void __user *argp = (void __user *)arg;
98 if (copy_from_user(&r, argp, sizeof(r)))
104 spin_lock_irqsave(&dev->regs_lock, flags);
105 if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) &&
106 phantom_status(dev, dev->status | PHB_RUNNING)){
107 spin_unlock_irqrestore(&dev->regs_lock, flags);
111 pr_debug("phantom: writing %x to %u\n", r.value, r.reg);
113 /* preserve amp bit (don't allow to change it when in NOT_OH) */
114 if (r.reg == PHN_CONTROL && (dev->status & PHB_NOT_OH)) {
115 r.value &= ~PHN_CTL_AMP;
116 r.value |= dev->ctl_reg & PHN_CTL_AMP;
117 dev->ctl_reg = r.value;
120 iowrite32(r.value, dev->iaddr + r.reg);
121 ioread32(dev->iaddr); /* PCI posting */
123 if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ))
124 phantom_status(dev, dev->status & ~PHB_RUNNING);
125 spin_unlock_irqrestore(&dev->regs_lock, flags);
129 if (copy_from_user(&rs, argp, sizeof(rs)))
132 pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
133 spin_lock_irqsave(&dev->regs_lock, flags);
134 if (dev->status & PHB_NOT_OH)
135 memcpy(&dev->oregs, &rs, sizeof(rs));
137 u32 m = min(rs.count, 8U);
138 for (i = 0; i < m; i++)
139 if (rs.mask & BIT(i))
140 iowrite32(rs.values[i], dev->oaddr + i);
141 ioread32(dev->iaddr); /* PCI posting */
143 spin_unlock_irqrestore(&dev->regs_lock, flags);
147 if (copy_from_user(&r, argp, sizeof(r)))
153 r.value = ioread32(dev->iaddr + r.reg);
155 if (copy_to_user(argp, &r, sizeof(r)))
162 if (copy_from_user(&rs, argp, sizeof(rs)))
165 m = min(rs.count, 8U);
167 pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
168 spin_lock_irqsave(&dev->regs_lock, flags);
169 for (i = 0; i < m; i++)
170 if (rs.mask & BIT(i))
171 rs.values[i] = ioread32(dev->iaddr + i);
172 atomic_set(&dev->counter, 0);
173 spin_unlock_irqrestore(&dev->regs_lock, flags);
175 if (copy_to_user(argp, &rs, sizeof(rs)))
179 spin_lock_irqsave(&dev->regs_lock, flags);
180 if (dev->status & PHB_RUNNING) {
181 printk(KERN_ERR "phantom: you need to set NOT_OH "
182 "before you start the device!\n");
183 spin_unlock_irqrestore(&dev->regs_lock, flags);
186 dev->status |= PHB_NOT_OH;
187 spin_unlock_irqrestore(&dev->regs_lock, flags);
197 static long phantom_compat_ioctl(struct file *filp, unsigned int cmd,
200 if (_IOC_NR(cmd) <= 3 && _IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
201 cmd &= ~(_IOC_SIZEMASK << _IOC_SIZESHIFT);
202 cmd |= sizeof(void *) << _IOC_SIZESHIFT;
204 return phantom_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
207 #define phantom_compat_ioctl NULL
210 static int phantom_open(struct inode *inode, struct file *file)
212 struct phantom_device *dev = container_of(inode->i_cdev,
213 struct phantom_device, cdev);
215 mutex_lock(&phantom_mutex);
216 nonseekable_open(inode, file);
218 if (mutex_lock_interruptible(&dev->open_lock)) {
219 mutex_unlock(&phantom_mutex);
224 mutex_unlock(&dev->open_lock);
225 mutex_unlock(&phantom_mutex);
229 WARN_ON(dev->status & PHB_NOT_OH);
231 file->private_data = dev;
233 atomic_set(&dev->counter, 0);
235 mutex_unlock(&dev->open_lock);
236 mutex_unlock(&phantom_mutex);
240 static int phantom_release(struct inode *inode, struct file *file)
242 struct phantom_device *dev = file->private_data;
244 mutex_lock(&dev->open_lock);
247 phantom_status(dev, dev->status & ~PHB_RUNNING);
248 dev->status &= ~PHB_NOT_OH;
250 mutex_unlock(&dev->open_lock);
255 static __poll_t phantom_poll(struct file *file, poll_table *wait)
257 struct phantom_device *dev = file->private_data;
260 pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
261 poll_wait(file, &dev->wait, wait);
263 if (!(dev->status & PHB_RUNNING))
265 else if (atomic_read(&dev->counter))
266 mask = EPOLLIN | EPOLLRDNORM;
268 pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
273 static const struct file_operations phantom_file_ops = {
274 .open = phantom_open,
275 .release = phantom_release,
276 .unlocked_ioctl = phantom_ioctl,
277 .compat_ioctl = phantom_compat_ioctl,
278 .poll = phantom_poll,
282 static irqreturn_t phantom_isr(int irq, void *data)
284 struct phantom_device *dev = data;
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);
295 iowrite32(0, dev->iaddr);
296 iowrite32(0xc0, dev->iaddr);
298 if (dev->status & PHB_NOT_OH) {
299 struct phm_regs *r = &dev->oregs;
300 u32 m = min(r->count, 8U);
302 for (i = 0; i < m; i++)
303 if (r->mask & BIT(i))
304 iowrite32(r->values[i], dev->oaddr + i);
306 dev->ctl_reg ^= PHN_CTL_AMP;
307 iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
309 spin_unlock(&dev->regs_lock);
311 ioread32(dev->iaddr); /* PCI posting */
313 atomic_inc(&dev->counter);
314 wake_up_interruptible(&dev->wait);
320 * Init and deinit driver
323 static unsigned int phantom_get_free(void)
327 for (i = 0; i < PHANTOM_MAX_MINORS; i++)
328 if (phantom_devices[i] == 0)
334 static int phantom_probe(struct pci_dev *pdev,
335 const struct pci_device_id *pci_id)
337 struct phantom_device *pht;
341 retval = pci_enable_device(pdev);
343 dev_err(&pdev->dev, "pci_enable_device failed!\n");
347 minor = phantom_get_free();
348 if (minor == PHANTOM_MAX_MINORS) {
349 dev_err(&pdev->dev, "too many devices found!\n");
354 phantom_devices[minor] = 1;
356 retval = pci_request_regions(pdev, "phantom");
358 dev_err(&pdev->dev, "pci_request_regions failed!\n");
363 pht = kzalloc(sizeof(*pht), GFP_KERNEL);
365 dev_err(&pdev->dev, "unable to allocate device\n");
369 pht->caddr = pci_iomap(pdev, 0, 0);
370 if (pht->caddr == NULL) {
371 dev_err(&pdev->dev, "can't remap conf space\n");
374 pht->iaddr = pci_iomap(pdev, 2, 0);
375 if (pht->iaddr == NULL) {
376 dev_err(&pdev->dev, "can't remap input space\n");
379 pht->oaddr = pci_iomap(pdev, 3, 0);
380 if (pht->oaddr == NULL) {
381 dev_err(&pdev->dev, "can't remap output space\n");
385 mutex_init(&pht->open_lock);
386 spin_lock_init(&pht->regs_lock);
387 init_waitqueue_head(&pht->wait);
388 cdev_init(&pht->cdev, &phantom_file_ops);
389 pht->cdev.owner = THIS_MODULE;
391 iowrite32(0, pht->caddr + PHN_IRQCTL);
392 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
393 retval = request_irq(pdev->irq, phantom_isr,
394 IRQF_SHARED, "phantom", pht);
396 dev_err(&pdev->dev, "can't establish ISR\n");
400 retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
402 dev_err(&pdev->dev, "chardev registration failed\n");
406 if (IS_ERR(device_create(phantom_class, &pdev->dev,
407 MKDEV(phantom_major, minor), NULL,
408 "phantom%u", minor)))
409 dev_err(&pdev->dev, "can't create device\n");
411 pci_set_drvdata(pdev, pht);
415 free_irq(pdev->irq, pht);
417 pci_iounmap(pdev, pht->oaddr);
419 pci_iounmap(pdev, pht->iaddr);
421 pci_iounmap(pdev, pht->caddr);
425 pci_release_regions(pdev);
427 phantom_devices[minor] = 0;
429 pci_disable_device(pdev);
434 static void phantom_remove(struct pci_dev *pdev)
436 struct phantom_device *pht = pci_get_drvdata(pdev);
437 unsigned int minor = MINOR(pht->cdev.dev);
439 device_destroy(phantom_class, MKDEV(phantom_major, minor));
441 cdev_del(&pht->cdev);
443 iowrite32(0, pht->caddr + PHN_IRQCTL);
444 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
445 free_irq(pdev->irq, pht);
447 pci_iounmap(pdev, pht->oaddr);
448 pci_iounmap(pdev, pht->iaddr);
449 pci_iounmap(pdev, pht->caddr);
453 pci_release_regions(pdev);
455 phantom_devices[minor] = 0;
457 pci_disable_device(pdev);
460 static int __maybe_unused phantom_suspend(struct device *dev_d)
462 struct phantom_device *dev = dev_get_drvdata(dev_d);
464 iowrite32(0, dev->caddr + PHN_IRQCTL);
465 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
467 synchronize_irq(to_pci_dev(dev_d)->irq);
472 static int __maybe_unused phantom_resume(struct device *dev_d)
474 struct phantom_device *dev = dev_get_drvdata(dev_d);
476 iowrite32(0, dev->caddr + PHN_IRQCTL);
481 static struct pci_device_id phantom_pci_tbl[] = {
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 },
487 MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
489 static SIMPLE_DEV_PM_OPS(phantom_pm_ops, phantom_suspend, phantom_resume);
491 static struct pci_driver phantom_pci_driver = {
493 .id_table = phantom_pci_tbl,
494 .probe = phantom_probe,
495 .remove = phantom_remove,
496 .driver.pm = &phantom_pm_ops,
499 static CLASS_ATTR_STRING(version, 0444, PHANTOM_VERSION);
501 static int __init phantom_init(void)
506 phantom_class = class_create(THIS_MODULE, "phantom");
507 if (IS_ERR(phantom_class)) {
508 retval = PTR_ERR(phantom_class);
509 printk(KERN_ERR "phantom: can't register phantom class\n");
512 retval = class_create_file(phantom_class, &class_attr_version.attr);
514 printk(KERN_ERR "phantom: can't create sysfs version file\n");
518 retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
520 printk(KERN_ERR "phantom: can't register character device\n");
523 phantom_major = MAJOR(dev);
525 retval = pci_register_driver(&phantom_pci_driver);
527 printk(KERN_ERR "phantom: can't register pci driver\n");
531 printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
536 unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
538 class_remove_file(phantom_class, &class_attr_version.attr);
540 class_destroy(phantom_class);
545 static void __exit phantom_exit(void)
547 pci_unregister_driver(&phantom_pci_driver);
549 unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
551 class_remove_file(phantom_class, &class_attr_version.attr);
552 class_destroy(phantom_class);
554 pr_debug("phantom: module successfully removed\n");
557 module_init(phantom_init);
558 module_exit(phantom_exit);
560 MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
561 MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
562 MODULE_LICENSE("GPL");
563 MODULE_VERSION(PHANTOM_VERSION);