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 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
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/phantom.h>
26 #include <asm/atomic.h>
29 #define PHANTOM_VERSION "n0.9.8"
31 #define PHANTOM_MAX_MINORS 8
33 #define PHN_IRQCTL 0x4c /* irq control in caddr space */
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 nonseekable_open(inode, file);
217 if (mutex_lock_interruptible(&dev->open_lock))
221 mutex_unlock(&dev->open_lock);
225 WARN_ON(dev->status & PHB_NOT_OH);
227 file->private_data = dev;
229 atomic_set(&dev->counter, 0);
231 mutex_unlock(&dev->open_lock);
236 static int phantom_release(struct inode *inode, struct file *file)
238 struct phantom_device *dev = file->private_data;
240 mutex_lock(&dev->open_lock);
243 phantom_status(dev, dev->status & ~PHB_RUNNING);
244 dev->status &= ~PHB_NOT_OH;
246 mutex_unlock(&dev->open_lock);
251 static unsigned int phantom_poll(struct file *file, poll_table *wait)
253 struct phantom_device *dev = file->private_data;
254 unsigned int mask = 0;
256 pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
257 poll_wait(file, &dev->wait, wait);
259 if (!(dev->status & PHB_RUNNING))
261 else if (atomic_read(&dev->counter))
262 mask = POLLIN | POLLRDNORM;
264 pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
269 static struct file_operations phantom_file_ops = {
270 .open = phantom_open,
271 .release = phantom_release,
272 .unlocked_ioctl = phantom_ioctl,
273 .compat_ioctl = phantom_compat_ioctl,
274 .poll = phantom_poll,
277 static irqreturn_t phantom_isr(int irq, void *data)
279 struct phantom_device *dev = data;
283 spin_lock(&dev->regs_lock);
284 ctl = ioread32(dev->iaddr + PHN_CONTROL);
285 if (!(ctl & PHN_CTL_IRQ)) {
286 spin_unlock(&dev->regs_lock);
290 iowrite32(0, dev->iaddr);
291 iowrite32(0xc0, dev->iaddr);
293 if (dev->status & PHB_NOT_OH) {
294 struct phm_regs *r = &dev->oregs;
295 u32 m = min(r->count, 8U);
297 for (i = 0; i < m; i++)
298 if (r->mask & BIT(i))
299 iowrite32(r->values[i], dev->oaddr + i);
301 dev->ctl_reg ^= PHN_CTL_AMP;
302 iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
304 spin_unlock(&dev->regs_lock);
306 ioread32(dev->iaddr); /* PCI posting */
308 atomic_inc(&dev->counter);
309 wake_up_interruptible(&dev->wait);
315 * Init and deinit driver
318 static unsigned int __devinit phantom_get_free(void)
322 for (i = 0; i < PHANTOM_MAX_MINORS; i++)
323 if (phantom_devices[i] == 0)
329 static int __devinit phantom_probe(struct pci_dev *pdev,
330 const struct pci_device_id *pci_id)
332 struct phantom_device *pht;
336 retval = pci_enable_device(pdev);
340 minor = phantom_get_free();
341 if (minor == PHANTOM_MAX_MINORS) {
342 dev_err(&pdev->dev, "too many devices found!\n");
347 phantom_devices[minor] = 1;
349 retval = pci_request_regions(pdev, "phantom");
354 pht = kzalloc(sizeof(*pht), GFP_KERNEL);
356 dev_err(&pdev->dev, "unable to allocate device\n");
360 pht->caddr = pci_iomap(pdev, 0, 0);
361 if (pht->caddr == NULL) {
362 dev_err(&pdev->dev, "can't remap conf space\n");
365 pht->iaddr = pci_iomap(pdev, 2, 0);
366 if (pht->iaddr == NULL) {
367 dev_err(&pdev->dev, "can't remap input space\n");
370 pht->oaddr = pci_iomap(pdev, 3, 0);
371 if (pht->oaddr == NULL) {
372 dev_err(&pdev->dev, "can't remap output space\n");
376 mutex_init(&pht->open_lock);
377 spin_lock_init(&pht->regs_lock);
378 init_waitqueue_head(&pht->wait);
379 cdev_init(&pht->cdev, &phantom_file_ops);
380 pht->cdev.owner = THIS_MODULE;
382 iowrite32(0, pht->caddr + PHN_IRQCTL);
383 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
384 retval = request_irq(pdev->irq, phantom_isr,
385 IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
387 dev_err(&pdev->dev, "can't establish ISR\n");
391 retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
393 dev_err(&pdev->dev, "chardev registration failed\n");
397 if (IS_ERR(device_create(phantom_class, &pdev->dev, MKDEV(phantom_major,
398 minor), "phantom%u", minor)))
399 dev_err(&pdev->dev, "can't create device\n");
401 pci_set_drvdata(pdev, pht);
405 free_irq(pdev->irq, pht);
407 pci_iounmap(pdev, pht->oaddr);
409 pci_iounmap(pdev, pht->iaddr);
411 pci_iounmap(pdev, pht->caddr);
415 pci_release_regions(pdev);
417 phantom_devices[minor] = 0;
419 pci_disable_device(pdev);
424 static void __devexit phantom_remove(struct pci_dev *pdev)
426 struct phantom_device *pht = pci_get_drvdata(pdev);
427 unsigned int minor = MINOR(pht->cdev.dev);
429 device_destroy(phantom_class, MKDEV(phantom_major, minor));
431 cdev_del(&pht->cdev);
433 iowrite32(0, pht->caddr + PHN_IRQCTL);
434 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
435 free_irq(pdev->irq, pht);
437 pci_iounmap(pdev, pht->oaddr);
438 pci_iounmap(pdev, pht->iaddr);
439 pci_iounmap(pdev, pht->caddr);
443 pci_release_regions(pdev);
445 phantom_devices[minor] = 0;
447 pci_disable_device(pdev);
451 static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
453 struct phantom_device *dev = pci_get_drvdata(pdev);
455 iowrite32(0, dev->caddr + PHN_IRQCTL);
456 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
458 synchronize_irq(pdev->irq);
463 static int phantom_resume(struct pci_dev *pdev)
465 struct phantom_device *dev = pci_get_drvdata(pdev);
467 iowrite32(0, dev->caddr + PHN_IRQCTL);
472 #define phantom_suspend NULL
473 #define phantom_resume NULL
476 static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
477 { .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
478 .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
479 .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
482 MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
484 static struct pci_driver phantom_pci_driver = {
486 .id_table = phantom_pci_tbl,
487 .probe = phantom_probe,
488 .remove = __devexit_p(phantom_remove),
489 .suspend = phantom_suspend,
490 .resume = phantom_resume
493 static ssize_t phantom_show_version(struct class *cls, char *buf)
495 return sprintf(buf, PHANTOM_VERSION "\n");
498 static CLASS_ATTR(version, 0444, phantom_show_version, NULL);
500 static int __init phantom_init(void)
505 phantom_class = class_create(THIS_MODULE, "phantom");
506 if (IS_ERR(phantom_class)) {
507 retval = PTR_ERR(phantom_class);
508 printk(KERN_ERR "phantom: can't register phantom class\n");
511 retval = class_create_file(phantom_class, &class_attr_version);
513 printk(KERN_ERR "phantom: can't create sysfs version file\n");
517 retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
519 printk(KERN_ERR "phantom: can't register character device\n");
522 phantom_major = MAJOR(dev);
524 retval = pci_register_driver(&phantom_pci_driver);
526 printk(KERN_ERR "phantom: can't register pci driver\n");
530 printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
535 unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
537 class_remove_file(phantom_class, &class_attr_version);
539 class_destroy(phantom_class);
544 static void __exit phantom_exit(void)
546 pci_unregister_driver(&phantom_pci_driver);
548 unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
550 class_remove_file(phantom_class, &class_attr_version);
551 class_destroy(phantom_class);
553 pr_debug("phantom: module successfully removed\n");
556 module_init(phantom_init);
557 module_exit(phantom_exit);
559 MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
560 MODULE_DESCRIPTION("Sensable Phantom driver");
561 MODULE_LICENSE("GPL");
562 MODULE_VERSION(PHANTOM_VERSION);