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/phantom.h>
25 #include <linux/sched.h>
26 #include <linux/smp_lock.h>
28 #include <asm/atomic.h>
31 #define PHANTOM_VERSION "n0.9.8"
33 #define PHANTOM_MAX_MINORS 8
35 #define PHN_IRQCTL 0x4c /* irq control in caddr space */
40 static struct class *phantom_class;
41 static int phantom_major;
43 struct phantom_device {
51 wait_queue_head_t wait;
54 struct mutex open_lock;
57 /* used in NOT_OH mode */
58 struct phm_regs oregs;
62 static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
64 static int phantom_status(struct phantom_device *dev, unsigned long newstat)
66 pr_debug("phantom_status %lx %lx\n", dev->status, newstat);
68 if (!(dev->status & PHB_RUNNING) && (newstat & PHB_RUNNING)) {
69 atomic_set(&dev->counter, 0);
70 iowrite32(PHN_CTL_IRQ, dev->iaddr + PHN_CONTROL);
71 iowrite32(0x43, dev->caddr + PHN_IRQCTL);
72 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
73 } else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) {
74 iowrite32(0, dev->caddr + PHN_IRQCTL);
75 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
78 dev->status = newstat;
87 static long phantom_ioctl(struct file *file, unsigned int cmd,
90 struct phantom_device *dev = file->private_data;
93 void __user *argp = (void __user *)arg;
100 if (copy_from_user(&r, argp, sizeof(r)))
106 spin_lock_irqsave(&dev->regs_lock, flags);
107 if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) &&
108 phantom_status(dev, dev->status | PHB_RUNNING)){
109 spin_unlock_irqrestore(&dev->regs_lock, flags);
113 pr_debug("phantom: writing %x to %u\n", r.value, r.reg);
115 /* preserve amp bit (don't allow to change it when in NOT_OH) */
116 if (r.reg == PHN_CONTROL && (dev->status & PHB_NOT_OH)) {
117 r.value &= ~PHN_CTL_AMP;
118 r.value |= dev->ctl_reg & PHN_CTL_AMP;
119 dev->ctl_reg = r.value;
122 iowrite32(r.value, dev->iaddr + r.reg);
123 ioread32(dev->iaddr); /* PCI posting */
125 if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ))
126 phantom_status(dev, dev->status & ~PHB_RUNNING);
127 spin_unlock_irqrestore(&dev->regs_lock, flags);
131 if (copy_from_user(&rs, argp, sizeof(rs)))
134 pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
135 spin_lock_irqsave(&dev->regs_lock, flags);
136 if (dev->status & PHB_NOT_OH)
137 memcpy(&dev->oregs, &rs, sizeof(rs));
139 u32 m = min(rs.count, 8U);
140 for (i = 0; i < m; i++)
141 if (rs.mask & BIT(i))
142 iowrite32(rs.values[i], dev->oaddr + i);
143 ioread32(dev->iaddr); /* PCI posting */
145 spin_unlock_irqrestore(&dev->regs_lock, flags);
149 if (copy_from_user(&r, argp, sizeof(r)))
155 r.value = ioread32(dev->iaddr + r.reg);
157 if (copy_to_user(argp, &r, sizeof(r)))
164 if (copy_from_user(&rs, argp, sizeof(rs)))
167 m = min(rs.count, 8U);
169 pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
170 spin_lock_irqsave(&dev->regs_lock, flags);
171 for (i = 0; i < m; i++)
172 if (rs.mask & BIT(i))
173 rs.values[i] = ioread32(dev->iaddr + i);
174 atomic_set(&dev->counter, 0);
175 spin_unlock_irqrestore(&dev->regs_lock, flags);
177 if (copy_to_user(argp, &rs, sizeof(rs)))
181 spin_lock_irqsave(&dev->regs_lock, flags);
182 if (dev->status & PHB_RUNNING) {
183 printk(KERN_ERR "phantom: you need to set NOT_OH "
184 "before you start the device!\n");
185 spin_unlock_irqrestore(&dev->regs_lock, flags);
188 dev->status |= PHB_NOT_OH;
189 spin_unlock_irqrestore(&dev->regs_lock, flags);
199 static long phantom_compat_ioctl(struct file *filp, unsigned int cmd,
202 if (_IOC_NR(cmd) <= 3 && _IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
203 cmd &= ~(_IOC_SIZEMASK << _IOC_SIZESHIFT);
204 cmd |= sizeof(void *) << _IOC_SIZESHIFT;
206 return phantom_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
209 #define phantom_compat_ioctl NULL
212 static int phantom_open(struct inode *inode, struct file *file)
214 struct phantom_device *dev = container_of(inode->i_cdev,
215 struct phantom_device, cdev);
218 nonseekable_open(inode, file);
220 if (mutex_lock_interruptible(&dev->open_lock)) {
226 mutex_unlock(&dev->open_lock);
231 WARN_ON(dev->status & PHB_NOT_OH);
233 file->private_data = dev;
235 atomic_set(&dev->counter, 0);
237 mutex_unlock(&dev->open_lock);
242 static int phantom_release(struct inode *inode, struct file *file)
244 struct phantom_device *dev = file->private_data;
246 mutex_lock(&dev->open_lock);
249 phantom_status(dev, dev->status & ~PHB_RUNNING);
250 dev->status &= ~PHB_NOT_OH;
252 mutex_unlock(&dev->open_lock);
257 static unsigned int phantom_poll(struct file *file, poll_table *wait)
259 struct phantom_device *dev = file->private_data;
260 unsigned int mask = 0;
262 pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
263 poll_wait(file, &dev->wait, wait);
265 if (!(dev->status & PHB_RUNNING))
267 else if (atomic_read(&dev->counter))
268 mask = POLLIN | POLLRDNORM;
270 pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
275 static const struct file_operations phantom_file_ops = {
276 .open = phantom_open,
277 .release = phantom_release,
278 .unlocked_ioctl = phantom_ioctl,
279 .compat_ioctl = phantom_compat_ioctl,
280 .poll = phantom_poll,
283 static irqreturn_t phantom_isr(int irq, void *data)
285 struct phantom_device *dev = data;
289 spin_lock(&dev->regs_lock);
290 ctl = ioread32(dev->iaddr + PHN_CONTROL);
291 if (!(ctl & PHN_CTL_IRQ)) {
292 spin_unlock(&dev->regs_lock);
296 iowrite32(0, dev->iaddr);
297 iowrite32(0xc0, dev->iaddr);
299 if (dev->status & PHB_NOT_OH) {
300 struct phm_regs *r = &dev->oregs;
301 u32 m = min(r->count, 8U);
303 for (i = 0; i < m; i++)
304 if (r->mask & BIT(i))
305 iowrite32(r->values[i], dev->oaddr + i);
307 dev->ctl_reg ^= PHN_CTL_AMP;
308 iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
310 spin_unlock(&dev->regs_lock);
312 ioread32(dev->iaddr); /* PCI posting */
314 atomic_inc(&dev->counter);
315 wake_up_interruptible(&dev->wait);
321 * Init and deinit driver
324 static unsigned int __devinit phantom_get_free(void)
328 for (i = 0; i < PHANTOM_MAX_MINORS; i++)
329 if (phantom_devices[i] == 0)
335 static int __devinit phantom_probe(struct pci_dev *pdev,
336 const struct pci_device_id *pci_id)
338 struct phantom_device *pht;
342 retval = pci_enable_device(pdev);
346 minor = phantom_get_free();
347 if (minor == PHANTOM_MAX_MINORS) {
348 dev_err(&pdev->dev, "too many devices found!\n");
353 phantom_devices[minor] = 1;
355 retval = pci_request_regions(pdev, "phantom");
360 pht = kzalloc(sizeof(*pht), GFP_KERNEL);
362 dev_err(&pdev->dev, "unable to allocate device\n");
366 pht->caddr = pci_iomap(pdev, 0, 0);
367 if (pht->caddr == NULL) {
368 dev_err(&pdev->dev, "can't remap conf space\n");
371 pht->iaddr = pci_iomap(pdev, 2, 0);
372 if (pht->iaddr == NULL) {
373 dev_err(&pdev->dev, "can't remap input space\n");
376 pht->oaddr = pci_iomap(pdev, 3, 0);
377 if (pht->oaddr == NULL) {
378 dev_err(&pdev->dev, "can't remap output space\n");
382 mutex_init(&pht->open_lock);
383 spin_lock_init(&pht->regs_lock);
384 init_waitqueue_head(&pht->wait);
385 cdev_init(&pht->cdev, &phantom_file_ops);
386 pht->cdev.owner = THIS_MODULE;
388 iowrite32(0, pht->caddr + PHN_IRQCTL);
389 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
390 retval = request_irq(pdev->irq, phantom_isr,
391 IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
393 dev_err(&pdev->dev, "can't establish ISR\n");
397 retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
399 dev_err(&pdev->dev, "chardev registration failed\n");
403 if (IS_ERR(device_create(phantom_class, &pdev->dev,
404 MKDEV(phantom_major, minor), NULL,
405 "phantom%u", minor)))
406 dev_err(&pdev->dev, "can't create device\n");
408 pci_set_drvdata(pdev, pht);
412 free_irq(pdev->irq, pht);
414 pci_iounmap(pdev, pht->oaddr);
416 pci_iounmap(pdev, pht->iaddr);
418 pci_iounmap(pdev, pht->caddr);
422 pci_release_regions(pdev);
424 phantom_devices[minor] = 0;
426 pci_disable_device(pdev);
431 static void __devexit phantom_remove(struct pci_dev *pdev)
433 struct phantom_device *pht = pci_get_drvdata(pdev);
434 unsigned int minor = MINOR(pht->cdev.dev);
436 device_destroy(phantom_class, MKDEV(phantom_major, minor));
438 cdev_del(&pht->cdev);
440 iowrite32(0, pht->caddr + PHN_IRQCTL);
441 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
442 free_irq(pdev->irq, pht);
444 pci_iounmap(pdev, pht->oaddr);
445 pci_iounmap(pdev, pht->iaddr);
446 pci_iounmap(pdev, pht->caddr);
450 pci_release_regions(pdev);
452 phantom_devices[minor] = 0;
454 pci_disable_device(pdev);
458 static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
460 struct phantom_device *dev = pci_get_drvdata(pdev);
462 iowrite32(0, dev->caddr + PHN_IRQCTL);
463 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
465 synchronize_irq(pdev->irq);
470 static int phantom_resume(struct pci_dev *pdev)
472 struct phantom_device *dev = pci_get_drvdata(pdev);
474 iowrite32(0, dev->caddr + PHN_IRQCTL);
479 #define phantom_suspend NULL
480 #define phantom_resume NULL
483 static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
484 { .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
485 .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
486 .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
489 MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
491 static struct pci_driver phantom_pci_driver = {
493 .id_table = phantom_pci_tbl,
494 .probe = phantom_probe,
495 .remove = __devexit_p(phantom_remove),
496 .suspend = phantom_suspend,
497 .resume = phantom_resume
500 static ssize_t phantom_show_version(struct class *cls, char *buf)
502 return sprintf(buf, PHANTOM_VERSION "\n");
505 static CLASS_ATTR(version, 0444, phantom_show_version, NULL);
507 static int __init phantom_init(void)
512 phantom_class = class_create(THIS_MODULE, "phantom");
513 if (IS_ERR(phantom_class)) {
514 retval = PTR_ERR(phantom_class);
515 printk(KERN_ERR "phantom: can't register phantom class\n");
518 retval = class_create_file(phantom_class, &class_attr_version);
520 printk(KERN_ERR "phantom: can't create sysfs version file\n");
524 retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
526 printk(KERN_ERR "phantom: can't register character device\n");
529 phantom_major = MAJOR(dev);
531 retval = pci_register_driver(&phantom_pci_driver);
533 printk(KERN_ERR "phantom: can't register pci driver\n");
537 printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
542 unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
544 class_remove_file(phantom_class, &class_attr_version);
546 class_destroy(phantom_class);
551 static void __exit phantom_exit(void)
553 pci_unregister_driver(&phantom_pci_driver);
555 unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
557 class_remove_file(phantom_class, &class_attr_version);
558 class_destroy(phantom_class);
560 pr_debug("phantom: module successfully removed\n");
563 module_init(phantom_init);
564 module_exit(phantom_exit);
566 MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
567 MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
568 MODULE_LICENSE("GPL");
569 MODULE_VERSION(PHANTOM_VERSION);