2 * Copyright (C) 2005-2007 Jiri Slaby <jirislaby@gmail.com>
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.
9 * You need a 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
12 * or alternatively, you might use OpenHaptics provided by Sensable.
15 #include <linux/compat.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/pci.h>
21 #include <linux/poll.h>
22 #include <linux/interrupt.h>
23 #include <linux/cdev.h>
24 #include <linux/slab.h>
25 #include <linux/phantom.h>
26 #include <linux/sched.h>
27 #include <linux/smp_lock.h>
29 #include <asm/atomic.h>
32 #define PHANTOM_VERSION "n0.9.8"
34 #define PHANTOM_MAX_MINORS 8
36 #define PHN_IRQCTL 0x4c /* irq control in caddr space */
41 static struct class *phantom_class;
42 static int phantom_major;
44 struct phantom_device {
52 wait_queue_head_t wait;
55 struct mutex open_lock;
58 /* used in NOT_OH mode */
59 struct phm_regs oregs;
63 static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
65 static int phantom_status(struct phantom_device *dev, unsigned long newstat)
67 pr_debug("phantom_status %lx %lx\n", dev->status, newstat);
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);
73 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
74 } else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) {
75 iowrite32(0, dev->caddr + PHN_IRQCTL);
76 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
79 dev->status = newstat;
88 static long phantom_ioctl(struct file *file, unsigned int cmd,
91 struct phantom_device *dev = file->private_data;
94 void __user *argp = (void __user *)arg;
101 if (copy_from_user(&r, argp, sizeof(r)))
107 spin_lock_irqsave(&dev->regs_lock, flags);
108 if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) &&
109 phantom_status(dev, dev->status | PHB_RUNNING)){
110 spin_unlock_irqrestore(&dev->regs_lock, flags);
114 pr_debug("phantom: writing %x to %u\n", r.value, r.reg);
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;
123 iowrite32(r.value, dev->iaddr + r.reg);
124 ioread32(dev->iaddr); /* PCI posting */
126 if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ))
127 phantom_status(dev, dev->status & ~PHB_RUNNING);
128 spin_unlock_irqrestore(&dev->regs_lock, flags);
132 if (copy_from_user(&rs, argp, sizeof(rs)))
135 pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
136 spin_lock_irqsave(&dev->regs_lock, flags);
137 if (dev->status & PHB_NOT_OH)
138 memcpy(&dev->oregs, &rs, sizeof(rs));
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 */
146 spin_unlock_irqrestore(&dev->regs_lock, flags);
150 if (copy_from_user(&r, argp, sizeof(r)))
156 r.value = ioread32(dev->iaddr + r.reg);
158 if (copy_to_user(argp, &r, sizeof(r)))
165 if (copy_from_user(&rs, argp, sizeof(rs)))
168 m = min(rs.count, 8U);
170 pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
171 spin_lock_irqsave(&dev->regs_lock, flags);
172 for (i = 0; i < m; i++)
173 if (rs.mask & BIT(i))
174 rs.values[i] = ioread32(dev->iaddr + i);
175 atomic_set(&dev->counter, 0);
176 spin_unlock_irqrestore(&dev->regs_lock, flags);
178 if (copy_to_user(argp, &rs, sizeof(rs)))
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);
189 dev->status |= PHB_NOT_OH;
190 spin_unlock_irqrestore(&dev->regs_lock, flags);
200 static long phantom_compat_ioctl(struct file *filp, unsigned int cmd,
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;
207 return phantom_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
210 #define phantom_compat_ioctl NULL
213 static int phantom_open(struct inode *inode, struct file *file)
215 struct phantom_device *dev = container_of(inode->i_cdev,
216 struct phantom_device, cdev);
219 nonseekable_open(inode, file);
221 if (mutex_lock_interruptible(&dev->open_lock)) {
227 mutex_unlock(&dev->open_lock);
232 WARN_ON(dev->status & PHB_NOT_OH);
234 file->private_data = dev;
236 atomic_set(&dev->counter, 0);
238 mutex_unlock(&dev->open_lock);
243 static int phantom_release(struct inode *inode, struct file *file)
245 struct phantom_device *dev = file->private_data;
247 mutex_lock(&dev->open_lock);
250 phantom_status(dev, dev->status & ~PHB_RUNNING);
251 dev->status &= ~PHB_NOT_OH;
253 mutex_unlock(&dev->open_lock);
258 static unsigned int phantom_poll(struct file *file, poll_table *wait)
260 struct phantom_device *dev = file->private_data;
261 unsigned int mask = 0;
263 pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
264 poll_wait(file, &dev->wait, wait);
266 if (!(dev->status & PHB_RUNNING))
268 else if (atomic_read(&dev->counter))
269 mask = POLLIN | POLLRDNORM;
271 pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
276 static const struct file_operations phantom_file_ops = {
277 .open = phantom_open,
278 .release = phantom_release,
279 .unlocked_ioctl = phantom_ioctl,
280 .compat_ioctl = phantom_compat_ioctl,
281 .poll = phantom_poll,
285 static irqreturn_t phantom_isr(int irq, void *data)
287 struct phantom_device *dev = data;
291 spin_lock(&dev->regs_lock);
292 ctl = ioread32(dev->iaddr + PHN_CONTROL);
293 if (!(ctl & PHN_CTL_IRQ)) {
294 spin_unlock(&dev->regs_lock);
298 iowrite32(0, dev->iaddr);
299 iowrite32(0xc0, dev->iaddr);
301 if (dev->status & PHB_NOT_OH) {
302 struct phm_regs *r = &dev->oregs;
303 u32 m = min(r->count, 8U);
305 for (i = 0; i < m; i++)
306 if (r->mask & BIT(i))
307 iowrite32(r->values[i], dev->oaddr + i);
309 dev->ctl_reg ^= PHN_CTL_AMP;
310 iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
312 spin_unlock(&dev->regs_lock);
314 ioread32(dev->iaddr); /* PCI posting */
316 atomic_inc(&dev->counter);
317 wake_up_interruptible(&dev->wait);
323 * Init and deinit driver
326 static unsigned int __devinit phantom_get_free(void)
330 for (i = 0; i < PHANTOM_MAX_MINORS; i++)
331 if (phantom_devices[i] == 0)
337 static int __devinit phantom_probe(struct pci_dev *pdev,
338 const struct pci_device_id *pci_id)
340 struct phantom_device *pht;
344 retval = pci_enable_device(pdev);
348 minor = phantom_get_free();
349 if (minor == PHANTOM_MAX_MINORS) {
350 dev_err(&pdev->dev, "too many devices found!\n");
355 phantom_devices[minor] = 1;
357 retval = pci_request_regions(pdev, "phantom");
362 pht = kzalloc(sizeof(*pht), GFP_KERNEL);
364 dev_err(&pdev->dev, "unable to allocate device\n");
368 pht->caddr = pci_iomap(pdev, 0, 0);
369 if (pht->caddr == NULL) {
370 dev_err(&pdev->dev, "can't remap conf space\n");
373 pht->iaddr = pci_iomap(pdev, 2, 0);
374 if (pht->iaddr == NULL) {
375 dev_err(&pdev->dev, "can't remap input space\n");
378 pht->oaddr = pci_iomap(pdev, 3, 0);
379 if (pht->oaddr == NULL) {
380 dev_err(&pdev->dev, "can't remap output space\n");
384 mutex_init(&pht->open_lock);
385 spin_lock_init(&pht->regs_lock);
386 init_waitqueue_head(&pht->wait);
387 cdev_init(&pht->cdev, &phantom_file_ops);
388 pht->cdev.owner = THIS_MODULE;
390 iowrite32(0, pht->caddr + PHN_IRQCTL);
391 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
392 retval = request_irq(pdev->irq, phantom_isr,
393 IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
395 dev_err(&pdev->dev, "can't establish ISR\n");
399 retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
401 dev_err(&pdev->dev, "chardev registration failed\n");
405 if (IS_ERR(device_create(phantom_class, &pdev->dev,
406 MKDEV(phantom_major, minor), NULL,
407 "phantom%u", minor)))
408 dev_err(&pdev->dev, "can't create device\n");
410 pci_set_drvdata(pdev, pht);
414 free_irq(pdev->irq, pht);
416 pci_iounmap(pdev, pht->oaddr);
418 pci_iounmap(pdev, pht->iaddr);
420 pci_iounmap(pdev, pht->caddr);
424 pci_release_regions(pdev);
426 phantom_devices[minor] = 0;
428 pci_disable_device(pdev);
433 static void __devexit phantom_remove(struct pci_dev *pdev)
435 struct phantom_device *pht = pci_get_drvdata(pdev);
436 unsigned int minor = MINOR(pht->cdev.dev);
438 device_destroy(phantom_class, MKDEV(phantom_major, minor));
440 cdev_del(&pht->cdev);
442 iowrite32(0, pht->caddr + PHN_IRQCTL);
443 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
444 free_irq(pdev->irq, pht);
446 pci_iounmap(pdev, pht->oaddr);
447 pci_iounmap(pdev, pht->iaddr);
448 pci_iounmap(pdev, pht->caddr);
452 pci_release_regions(pdev);
454 phantom_devices[minor] = 0;
456 pci_disable_device(pdev);
460 static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
462 struct phantom_device *dev = pci_get_drvdata(pdev);
464 iowrite32(0, dev->caddr + PHN_IRQCTL);
465 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
467 synchronize_irq(pdev->irq);
472 static int phantom_resume(struct pci_dev *pdev)
474 struct phantom_device *dev = pci_get_drvdata(pdev);
476 iowrite32(0, dev->caddr + PHN_IRQCTL);
481 #define phantom_suspend NULL
482 #define phantom_resume NULL
485 static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
486 { .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
487 .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
488 .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
491 MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
493 static struct pci_driver phantom_pci_driver = {
495 .id_table = phantom_pci_tbl,
496 .probe = phantom_probe,
497 .remove = __devexit_p(phantom_remove),
498 .suspend = phantom_suspend,
499 .resume = phantom_resume
502 static CLASS_ATTR_STRING(version, 0444, PHANTOM_VERSION);
504 static int __init phantom_init(void)
509 phantom_class = class_create(THIS_MODULE, "phantom");
510 if (IS_ERR(phantom_class)) {
511 retval = PTR_ERR(phantom_class);
512 printk(KERN_ERR "phantom: can't register phantom class\n");
515 retval = class_create_file(phantom_class, &class_attr_version.attr);
517 printk(KERN_ERR "phantom: can't create sysfs version file\n");
521 retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
523 printk(KERN_ERR "phantom: can't register character device\n");
526 phantom_major = MAJOR(dev);
528 retval = pci_register_driver(&phantom_pci_driver);
530 printk(KERN_ERR "phantom: can't register pci driver\n");
534 printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
539 unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
541 class_remove_file(phantom_class, &class_attr_version.attr);
543 class_destroy(phantom_class);
548 static void __exit phantom_exit(void)
550 pci_unregister_driver(&phantom_pci_driver);
552 unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
554 class_remove_file(phantom_class, &class_attr_version.attr);
555 class_destroy(phantom_class);
557 pr_debug("phantom: module successfully removed\n");
560 module_init(phantom_init);
561 module_exit(phantom_exit);
563 MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
564 MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
565 MODULE_LICENSE("GPL");
566 MODULE_VERSION(PHANTOM_VERSION);