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/smp_lock.h>
27 #include <asm/atomic.h>
30 #define PHANTOM_VERSION "n0.9.8"
32 #define PHANTOM_MAX_MINORS 8
34 #define PHN_IRQCTL 0x4c /* irq control in caddr space */
39 static struct class *phantom_class
;
40 static int phantom_major
;
42 struct phantom_device
{
50 wait_queue_head_t wait
;
53 struct mutex open_lock
;
56 /* used in NOT_OH mode */
57 struct phm_regs oregs
;
61 static unsigned char phantom_devices
[PHANTOM_MAX_MINORS
];
63 static int phantom_status(struct phantom_device
*dev
, unsigned long newstat
)
65 pr_debug("phantom_status %lx %lx\n", dev
->status
, newstat
);
67 if (!(dev
->status
& PHB_RUNNING
) && (newstat
& PHB_RUNNING
)) {
68 atomic_set(&dev
->counter
, 0);
69 iowrite32(PHN_CTL_IRQ
, dev
->iaddr
+ PHN_CONTROL
);
70 iowrite32(0x43, dev
->caddr
+ PHN_IRQCTL
);
71 ioread32(dev
->caddr
+ PHN_IRQCTL
); /* PCI posting */
72 } else if ((dev
->status
& PHB_RUNNING
) && !(newstat
& PHB_RUNNING
)) {
73 iowrite32(0, dev
->caddr
+ PHN_IRQCTL
);
74 ioread32(dev
->caddr
+ PHN_IRQCTL
); /* PCI posting */
77 dev
->status
= newstat
;
86 static long phantom_ioctl(struct file
*file
, unsigned int cmd
,
89 struct phantom_device
*dev
= file
->private_data
;
92 void __user
*argp
= (void __user
*)arg
;
99 if (copy_from_user(&r
, argp
, sizeof(r
)))
105 spin_lock_irqsave(&dev
->regs_lock
, flags
);
106 if (r
.reg
== PHN_CONTROL
&& (r
.value
& PHN_CTL_IRQ
) &&
107 phantom_status(dev
, dev
->status
| PHB_RUNNING
)){
108 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
112 pr_debug("phantom: writing %x to %u\n", r
.value
, r
.reg
);
114 /* preserve amp bit (don't allow to change it when in NOT_OH) */
115 if (r
.reg
== PHN_CONTROL
&& (dev
->status
& PHB_NOT_OH
)) {
116 r
.value
&= ~PHN_CTL_AMP
;
117 r
.value
|= dev
->ctl_reg
& PHN_CTL_AMP
;
118 dev
->ctl_reg
= r
.value
;
121 iowrite32(r
.value
, dev
->iaddr
+ r
.reg
);
122 ioread32(dev
->iaddr
); /* PCI posting */
124 if (r
.reg
== PHN_CONTROL
&& !(r
.value
& PHN_CTL_IRQ
))
125 phantom_status(dev
, dev
->status
& ~PHB_RUNNING
);
126 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
);
148 if (copy_from_user(&r
, argp
, sizeof(r
)))
154 r
.value
= ioread32(dev
->iaddr
+ r
.reg
);
156 if (copy_to_user(argp
, &r
, sizeof(r
)))
163 if (copy_from_user(&rs
, argp
, sizeof(rs
)))
166 m
= min(rs
.count
, 8U);
168 pr_debug("phantom: GRS %u regs %x\n", rs
.count
, rs
.mask
);
169 spin_lock_irqsave(&dev
->regs_lock
, flags
);
170 for (i
= 0; i
< m
; i
++)
171 if (rs
.mask
& BIT(i
))
172 rs
.values
[i
] = ioread32(dev
->iaddr
+ i
);
173 atomic_set(&dev
->counter
, 0);
174 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
176 if (copy_to_user(argp
, &rs
, sizeof(rs
)))
180 spin_lock_irqsave(&dev
->regs_lock
, flags
);
181 if (dev
->status
& PHB_RUNNING
) {
182 printk(KERN_ERR
"phantom: you need to set NOT_OH "
183 "before you start the device!\n");
184 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
187 dev
->status
|= PHB_NOT_OH
;
188 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
198 static long phantom_compat_ioctl(struct file
*filp
, unsigned int cmd
,
201 if (_IOC_NR(cmd
) <= 3 && _IOC_SIZE(cmd
) == sizeof(compat_uptr_t
)) {
202 cmd
&= ~(_IOC_SIZEMASK
<< _IOC_SIZESHIFT
);
203 cmd
|= sizeof(void *) << _IOC_SIZESHIFT
;
205 return phantom_ioctl(filp
, cmd
, (unsigned long)compat_ptr(arg
));
208 #define phantom_compat_ioctl NULL
211 static int phantom_open(struct inode
*inode
, struct file
*file
)
213 struct phantom_device
*dev
= container_of(inode
->i_cdev
,
214 struct phantom_device
, cdev
);
217 nonseekable_open(inode
, file
);
219 if (mutex_lock_interruptible(&dev
->open_lock
)) {
225 mutex_unlock(&dev
->open_lock
);
230 WARN_ON(dev
->status
& PHB_NOT_OH
);
232 file
->private_data
= dev
;
234 atomic_set(&dev
->counter
, 0);
236 mutex_unlock(&dev
->open_lock
);
241 static int phantom_release(struct inode
*inode
, struct file
*file
)
243 struct phantom_device
*dev
= file
->private_data
;
245 mutex_lock(&dev
->open_lock
);
248 phantom_status(dev
, dev
->status
& ~PHB_RUNNING
);
249 dev
->status
&= ~PHB_NOT_OH
;
251 mutex_unlock(&dev
->open_lock
);
256 static unsigned int phantom_poll(struct file
*file
, poll_table
*wait
)
258 struct phantom_device
*dev
= file
->private_data
;
259 unsigned int mask
= 0;
261 pr_debug("phantom_poll: %d\n", atomic_read(&dev
->counter
));
262 poll_wait(file
, &dev
->wait
, wait
);
264 if (!(dev
->status
& PHB_RUNNING
))
266 else if (atomic_read(&dev
->counter
))
267 mask
= POLLIN
| POLLRDNORM
;
269 pr_debug("phantom_poll end: %x/%d\n", mask
, atomic_read(&dev
->counter
));
274 static struct file_operations phantom_file_ops
= {
275 .open
= phantom_open
,
276 .release
= phantom_release
,
277 .unlocked_ioctl
= phantom_ioctl
,
278 .compat_ioctl
= phantom_compat_ioctl
,
279 .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 __devinit
phantom_get_free(void)
327 for (i
= 0; i
< PHANTOM_MAX_MINORS
; i
++)
328 if (phantom_devices
[i
] == 0)
334 static int __devinit
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
);
345 minor
= phantom_get_free();
346 if (minor
== PHANTOM_MAX_MINORS
) {
347 dev_err(&pdev
->dev
, "too many devices found!\n");
352 phantom_devices
[minor
] = 1;
354 retval
= pci_request_regions(pdev
, "phantom");
359 pht
= kzalloc(sizeof(*pht
), GFP_KERNEL
);
361 dev_err(&pdev
->dev
, "unable to allocate device\n");
365 pht
->caddr
= pci_iomap(pdev
, 0, 0);
366 if (pht
->caddr
== NULL
) {
367 dev_err(&pdev
->dev
, "can't remap conf space\n");
370 pht
->iaddr
= pci_iomap(pdev
, 2, 0);
371 if (pht
->iaddr
== NULL
) {
372 dev_err(&pdev
->dev
, "can't remap input space\n");
375 pht
->oaddr
= pci_iomap(pdev
, 3, 0);
376 if (pht
->oaddr
== NULL
) {
377 dev_err(&pdev
->dev
, "can't remap output space\n");
381 mutex_init(&pht
->open_lock
);
382 spin_lock_init(&pht
->regs_lock
);
383 init_waitqueue_head(&pht
->wait
);
384 cdev_init(&pht
->cdev
, &phantom_file_ops
);
385 pht
->cdev
.owner
= THIS_MODULE
;
387 iowrite32(0, pht
->caddr
+ PHN_IRQCTL
);
388 ioread32(pht
->caddr
+ PHN_IRQCTL
); /* PCI posting */
389 retval
= request_irq(pdev
->irq
, phantom_isr
,
390 IRQF_SHARED
| IRQF_DISABLED
, "phantom", pht
);
392 dev_err(&pdev
->dev
, "can't establish ISR\n");
396 retval
= cdev_add(&pht
->cdev
, MKDEV(phantom_major
, minor
), 1);
398 dev_err(&pdev
->dev
, "chardev registration failed\n");
402 if (IS_ERR(device_create(phantom_class
, &pdev
->dev
,
403 MKDEV(phantom_major
, minor
), NULL
,
404 "phantom%u", minor
)))
405 dev_err(&pdev
->dev
, "can't create device\n");
407 pci_set_drvdata(pdev
, pht
);
411 free_irq(pdev
->irq
, pht
);
413 pci_iounmap(pdev
, pht
->oaddr
);
415 pci_iounmap(pdev
, pht
->iaddr
);
417 pci_iounmap(pdev
, pht
->caddr
);
421 pci_release_regions(pdev
);
423 phantom_devices
[minor
] = 0;
425 pci_disable_device(pdev
);
430 static void __devexit
phantom_remove(struct pci_dev
*pdev
)
432 struct phantom_device
*pht
= pci_get_drvdata(pdev
);
433 unsigned int minor
= MINOR(pht
->cdev
.dev
);
435 device_destroy(phantom_class
, MKDEV(phantom_major
, minor
));
437 cdev_del(&pht
->cdev
);
439 iowrite32(0, pht
->caddr
+ PHN_IRQCTL
);
440 ioread32(pht
->caddr
+ PHN_IRQCTL
); /* PCI posting */
441 free_irq(pdev
->irq
, pht
);
443 pci_iounmap(pdev
, pht
->oaddr
);
444 pci_iounmap(pdev
, pht
->iaddr
);
445 pci_iounmap(pdev
, pht
->caddr
);
449 pci_release_regions(pdev
);
451 phantom_devices
[minor
] = 0;
453 pci_disable_device(pdev
);
457 static int phantom_suspend(struct pci_dev
*pdev
, pm_message_t state
)
459 struct phantom_device
*dev
= pci_get_drvdata(pdev
);
461 iowrite32(0, dev
->caddr
+ PHN_IRQCTL
);
462 ioread32(dev
->caddr
+ PHN_IRQCTL
); /* PCI posting */
464 synchronize_irq(pdev
->irq
);
469 static int phantom_resume(struct pci_dev
*pdev
)
471 struct phantom_device
*dev
= pci_get_drvdata(pdev
);
473 iowrite32(0, dev
->caddr
+ PHN_IRQCTL
);
478 #define phantom_suspend NULL
479 #define phantom_resume NULL
482 static struct pci_device_id phantom_pci_tbl
[] __devinitdata
= {
483 { .vendor
= PCI_VENDOR_ID_PLX
, .device
= PCI_DEVICE_ID_PLX_9050
,
484 .subvendor
= PCI_VENDOR_ID_PLX
, .subdevice
= PCI_DEVICE_ID_PLX_9050
,
485 .class = PCI_CLASS_BRIDGE_OTHER
<< 8, .class_mask
= 0xffff00 },
488 MODULE_DEVICE_TABLE(pci
, phantom_pci_tbl
);
490 static struct pci_driver phantom_pci_driver
= {
492 .id_table
= phantom_pci_tbl
,
493 .probe
= phantom_probe
,
494 .remove
= __devexit_p(phantom_remove
),
495 .suspend
= phantom_suspend
,
496 .resume
= phantom_resume
499 static ssize_t
phantom_show_version(struct class *cls
, char *buf
)
501 return sprintf(buf
, PHANTOM_VERSION
"\n");
504 static CLASS_ATTR(version
, 0444, phantom_show_version
, NULL
);
506 static int __init
phantom_init(void)
511 phantom_class
= class_create(THIS_MODULE
, "phantom");
512 if (IS_ERR(phantom_class
)) {
513 retval
= PTR_ERR(phantom_class
);
514 printk(KERN_ERR
"phantom: can't register phantom class\n");
517 retval
= class_create_file(phantom_class
, &class_attr_version
);
519 printk(KERN_ERR
"phantom: can't create sysfs version file\n");
523 retval
= alloc_chrdev_region(&dev
, 0, PHANTOM_MAX_MINORS
, "phantom");
525 printk(KERN_ERR
"phantom: can't register character device\n");
528 phantom_major
= MAJOR(dev
);
530 retval
= pci_register_driver(&phantom_pci_driver
);
532 printk(KERN_ERR
"phantom: can't register pci driver\n");
536 printk(KERN_INFO
"Phantom Linux Driver, version " PHANTOM_VERSION
", "
541 unregister_chrdev_region(dev
, PHANTOM_MAX_MINORS
);
543 class_remove_file(phantom_class
, &class_attr_version
);
545 class_destroy(phantom_class
);
550 static void __exit
phantom_exit(void)
552 pci_unregister_driver(&phantom_pci_driver
);
554 unregister_chrdev_region(MKDEV(phantom_major
, 0), PHANTOM_MAX_MINORS
);
556 class_remove_file(phantom_class
, &class_attr_version
);
557 class_destroy(phantom_class
);
559 pr_debug("phantom: module successfully removed\n");
562 module_init(phantom_init
);
563 module_exit(phantom_exit
);
565 MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
566 MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
567 MODULE_LICENSE("GPL");
568 MODULE_VERSION(PHANTOM_VERSION
);