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/kernel.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/pci.h>
20 #include <linux/poll.h>
21 #include <linux/interrupt.h>
22 #include <linux/cdev.h>
23 #include <linux/phantom.h>
25 #include <asm/atomic.h>
28 #define PHANTOM_VERSION "n0.9.7"
30 #define PHANTOM_MAX_MINORS 8
32 #define PHN_IRQCTL 0x4c /* irq control in caddr space */
37 static struct class *phantom_class;
38 static int phantom_major;
40 struct phantom_device {
48 wait_queue_head_t wait;
51 struct mutex open_lock;
54 /* used in NOT_OH mode */
55 struct phm_regs oregs;
59 static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
61 static int phantom_status(struct phantom_device *dev, unsigned long newstat)
63 pr_debug("phantom_status %lx %lx\n", dev->status, newstat);
65 if (!(dev->status & PHB_RUNNING) && (newstat & PHB_RUNNING)) {
66 atomic_set(&dev->counter, 0);
67 iowrite32(PHN_CTL_IRQ, dev->iaddr + PHN_CONTROL);
68 iowrite32(0x43, dev->caddr + PHN_IRQCTL);
69 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
70 } else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) {
71 iowrite32(0, dev->caddr + PHN_IRQCTL);
72 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
75 dev->status = newstat;
84 static long phantom_ioctl(struct file *file, unsigned int cmd,
87 struct phantom_device *dev = file->private_data;
90 void __user *argp = (void __user *)arg;
94 if (_IOC_TYPE(cmd) != PH_IOC_MAGIC ||
95 _IOC_NR(cmd) > PH_IOC_MAXNR)
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);
130 if (copy_from_user(&rs, argp, sizeof(rs)))
133 pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
134 spin_lock_irqsave(&dev->regs_lock, flags);
135 if (dev->status & PHB_NOT_OH)
136 memcpy(&dev->oregs, &rs, sizeof(rs));
138 u32 m = min(rs.count, 8U);
139 for (i = 0; i < m; i++)
140 if (rs.mask & BIT(i))
141 iowrite32(rs.values[i], dev->oaddr + i);
142 ioread32(dev->iaddr); /* PCI posting */
144 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)))
161 if (copy_from_user(&rs, argp, sizeof(rs)))
164 m = min(rs.count, 8U);
166 pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
167 spin_lock_irqsave(&dev->regs_lock, flags);
168 for (i = 0; i < m; i++)
169 if (rs.mask & BIT(i))
170 rs.values[i] = ioread32(dev->iaddr + i);
171 spin_unlock_irqrestore(&dev->regs_lock, flags);
173 if (copy_to_user(argp, &rs, sizeof(rs)))
177 spin_lock_irqsave(&dev->regs_lock, flags);
178 if (dev->status & PHB_RUNNING) {
179 printk(KERN_ERR "phantom: you need to set NOT_OH "
180 "before you start the device!\n");
181 spin_unlock_irqrestore(&dev->regs_lock, flags);
184 dev->status |= PHB_NOT_OH;
185 spin_unlock_irqrestore(&dev->regs_lock, flags);
194 static int phantom_open(struct inode *inode, struct file *file)
196 struct phantom_device *dev = container_of(inode->i_cdev,
197 struct phantom_device, cdev);
199 nonseekable_open(inode, file);
201 if (mutex_lock_interruptible(&dev->open_lock))
205 mutex_unlock(&dev->open_lock);
209 WARN_ON(dev->status & PHB_NOT_OH);
211 file->private_data = dev;
213 atomic_set(&dev->counter, 0);
215 mutex_unlock(&dev->open_lock);
220 static int phantom_release(struct inode *inode, struct file *file)
222 struct phantom_device *dev = file->private_data;
224 mutex_lock(&dev->open_lock);
227 phantom_status(dev, dev->status & ~PHB_RUNNING);
228 dev->status &= ~PHB_NOT_OH;
230 mutex_unlock(&dev->open_lock);
235 static unsigned int phantom_poll(struct file *file, poll_table *wait)
237 struct phantom_device *dev = file->private_data;
238 unsigned int mask = 0;
240 pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
241 poll_wait(file, &dev->wait, wait);
242 if (atomic_read(&dev->counter)) {
243 mask = POLLIN | POLLRDNORM;
244 atomic_dec(&dev->counter);
245 } else if ((dev->status & PHB_RUNNING) == 0)
246 mask = POLLIN | POLLRDNORM | POLLERR;
247 pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
252 static struct file_operations phantom_file_ops = {
253 .open = phantom_open,
254 .release = phantom_release,
255 .unlocked_ioctl = phantom_ioctl,
256 .poll = phantom_poll,
259 static irqreturn_t phantom_isr(int irq, void *data)
261 struct phantom_device *dev = data;
265 spin_lock(&dev->regs_lock);
266 ctl = ioread32(dev->iaddr + PHN_CONTROL);
267 if (!(ctl & PHN_CTL_IRQ)) {
268 spin_unlock(&dev->regs_lock);
272 iowrite32(0, dev->iaddr);
273 iowrite32(0xc0, dev->iaddr);
275 if (dev->status & PHB_NOT_OH) {
276 struct phm_regs *r = &dev->oregs;
277 u32 m = min(r->count, 8U);
279 for (i = 0; i < m; i++)
280 if (r->mask & BIT(i))
281 iowrite32(r->values[i], dev->oaddr + i);
283 dev->ctl_reg ^= PHN_CTL_AMP;
284 iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
286 spin_unlock(&dev->regs_lock);
288 ioread32(dev->iaddr); /* PCI posting */
290 atomic_inc(&dev->counter);
291 wake_up_interruptible(&dev->wait);
297 * Init and deinit driver
300 static unsigned int __devinit phantom_get_free(void)
304 for (i = 0; i < PHANTOM_MAX_MINORS; i++)
305 if (phantom_devices[i] == 0)
311 static int __devinit phantom_probe(struct pci_dev *pdev,
312 const struct pci_device_id *pci_id)
314 struct phantom_device *pht;
318 retval = pci_enable_device(pdev);
322 minor = phantom_get_free();
323 if (minor == PHANTOM_MAX_MINORS) {
324 dev_err(&pdev->dev, "too many devices found!\n");
329 phantom_devices[minor] = 1;
331 retval = pci_request_regions(pdev, "phantom");
336 pht = kzalloc(sizeof(*pht), GFP_KERNEL);
338 dev_err(&pdev->dev, "unable to allocate device\n");
342 pht->caddr = pci_iomap(pdev, 0, 0);
343 if (pht->caddr == NULL) {
344 dev_err(&pdev->dev, "can't remap conf space\n");
347 pht->iaddr = pci_iomap(pdev, 2, 0);
348 if (pht->iaddr == NULL) {
349 dev_err(&pdev->dev, "can't remap input space\n");
352 pht->oaddr = pci_iomap(pdev, 3, 0);
353 if (pht->oaddr == NULL) {
354 dev_err(&pdev->dev, "can't remap output space\n");
358 mutex_init(&pht->open_lock);
359 spin_lock_init(&pht->regs_lock);
360 init_waitqueue_head(&pht->wait);
361 cdev_init(&pht->cdev, &phantom_file_ops);
362 pht->cdev.owner = THIS_MODULE;
364 iowrite32(0, pht->caddr + PHN_IRQCTL);
365 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
366 retval = request_irq(pdev->irq, phantom_isr,
367 IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
369 dev_err(&pdev->dev, "can't establish ISR\n");
373 retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
375 dev_err(&pdev->dev, "chardev registration failed\n");
379 if (IS_ERR(device_create(phantom_class, &pdev->dev, MKDEV(phantom_major,
380 minor), "phantom%u", minor)))
381 dev_err(&pdev->dev, "can't create device\n");
383 pci_set_drvdata(pdev, pht);
387 free_irq(pdev->irq, pht);
389 pci_iounmap(pdev, pht->oaddr);
391 pci_iounmap(pdev, pht->iaddr);
393 pci_iounmap(pdev, pht->caddr);
397 pci_release_regions(pdev);
399 phantom_devices[minor] = 0;
401 pci_disable_device(pdev);
406 static void __devexit phantom_remove(struct pci_dev *pdev)
408 struct phantom_device *pht = pci_get_drvdata(pdev);
409 unsigned int minor = MINOR(pht->cdev.dev);
411 device_destroy(phantom_class, MKDEV(phantom_major, minor));
413 cdev_del(&pht->cdev);
415 iowrite32(0, pht->caddr + PHN_IRQCTL);
416 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
417 free_irq(pdev->irq, pht);
419 pci_iounmap(pdev, pht->oaddr);
420 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);
433 static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
435 struct phantom_device *dev = pci_get_drvdata(pdev);
437 iowrite32(0, dev->caddr + PHN_IRQCTL);
438 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
440 synchronize_irq(pdev->irq);
445 static int phantom_resume(struct pci_dev *pdev)
447 struct phantom_device *dev = pci_get_drvdata(pdev);
449 iowrite32(0, dev->caddr + PHN_IRQCTL);
454 #define phantom_suspend NULL
455 #define phantom_resume NULL
458 static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
459 { PCI_DEVICE(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050),
460 .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
463 MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
465 static struct pci_driver phantom_pci_driver = {
467 .id_table = phantom_pci_tbl,
468 .probe = phantom_probe,
469 .remove = __devexit_p(phantom_remove),
470 .suspend = phantom_suspend,
471 .resume = phantom_resume
474 static ssize_t phantom_show_version(struct class *cls, char *buf)
476 return sprintf(buf, PHANTOM_VERSION "\n");
479 static CLASS_ATTR(version, 0444, phantom_show_version, NULL);
481 static int __init phantom_init(void)
486 phantom_class = class_create(THIS_MODULE, "phantom");
487 if (IS_ERR(phantom_class)) {
488 retval = PTR_ERR(phantom_class);
489 printk(KERN_ERR "phantom: can't register phantom class\n");
492 retval = class_create_file(phantom_class, &class_attr_version);
494 printk(KERN_ERR "phantom: can't create sysfs version file\n");
498 retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
500 printk(KERN_ERR "phantom: can't register character device\n");
503 phantom_major = MAJOR(dev);
505 retval = pci_register_driver(&phantom_pci_driver);
507 printk(KERN_ERR "phantom: can't register pci driver\n");
511 printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
516 unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
518 class_remove_file(phantom_class, &class_attr_version);
520 class_destroy(phantom_class);
525 static void __exit phantom_exit(void)
527 pci_unregister_driver(&phantom_pci_driver);
529 unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
531 class_remove_file(phantom_class, &class_attr_version);
532 class_destroy(phantom_class);
534 pr_debug("phantom: module successfully removed\n");
537 module_init(phantom_init);
538 module_exit(phantom_exit);
540 MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
541 MODULE_DESCRIPTION("Sensable Phantom driver");
542 MODULE_LICENSE("GPL");
543 MODULE_VERSION(PHANTOM_VERSION);