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/mutex.h>
29 #include <linux/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 DEFINE_MUTEX(phantom_mutex
);
42 static struct class *phantom_class
;
43 static int phantom_major
;
45 struct phantom_device
{
53 wait_queue_head_t wait
;
56 struct mutex open_lock
;
59 /* used in NOT_OH mode */
60 struct phm_regs oregs
;
64 static unsigned char phantom_devices
[PHANTOM_MAX_MINORS
];
66 static int phantom_status(struct phantom_device
*dev
, unsigned long newstat
)
68 pr_debug("phantom_status %lx %lx\n", dev
->status
, newstat
);
70 if (!(dev
->status
& PHB_RUNNING
) && (newstat
& PHB_RUNNING
)) {
71 atomic_set(&dev
->counter
, 0);
72 iowrite32(PHN_CTL_IRQ
, dev
->iaddr
+ PHN_CONTROL
);
73 iowrite32(0x43, dev
->caddr
+ PHN_IRQCTL
);
74 ioread32(dev
->caddr
+ PHN_IRQCTL
); /* PCI posting */
75 } else if ((dev
->status
& PHB_RUNNING
) && !(newstat
& PHB_RUNNING
)) {
76 iowrite32(0, dev
->caddr
+ PHN_IRQCTL
);
77 ioread32(dev
->caddr
+ PHN_IRQCTL
); /* PCI posting */
80 dev
->status
= newstat
;
89 static long phantom_ioctl(struct file
*file
, unsigned int cmd
,
92 struct phantom_device
*dev
= file
->private_data
;
95 void __user
*argp
= (void __user
*)arg
;
102 if (copy_from_user(&r
, argp
, sizeof(r
)))
108 spin_lock_irqsave(&dev
->regs_lock
, flags
);
109 if (r
.reg
== PHN_CONTROL
&& (r
.value
& PHN_CTL_IRQ
) &&
110 phantom_status(dev
, dev
->status
| PHB_RUNNING
)){
111 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
115 pr_debug("phantom: writing %x to %u\n", r
.value
, r
.reg
);
117 /* preserve amp bit (don't allow to change it when in NOT_OH) */
118 if (r
.reg
== PHN_CONTROL
&& (dev
->status
& PHB_NOT_OH
)) {
119 r
.value
&= ~PHN_CTL_AMP
;
120 r
.value
|= dev
->ctl_reg
& PHN_CTL_AMP
;
121 dev
->ctl_reg
= r
.value
;
124 iowrite32(r
.value
, dev
->iaddr
+ r
.reg
);
125 ioread32(dev
->iaddr
); /* PCI posting */
127 if (r
.reg
== PHN_CONTROL
&& !(r
.value
& PHN_CTL_IRQ
))
128 phantom_status(dev
, dev
->status
& ~PHB_RUNNING
);
129 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
133 if (copy_from_user(&rs
, argp
, sizeof(rs
)))
136 pr_debug("phantom: SRS %u regs %x\n", rs
.count
, rs
.mask
);
137 spin_lock_irqsave(&dev
->regs_lock
, flags
);
138 if (dev
->status
& PHB_NOT_OH
)
139 memcpy(&dev
->oregs
, &rs
, sizeof(rs
));
141 u32 m
= min(rs
.count
, 8U);
142 for (i
= 0; i
< m
; i
++)
143 if (rs
.mask
& BIT(i
))
144 iowrite32(rs
.values
[i
], dev
->oaddr
+ i
);
145 ioread32(dev
->iaddr
); /* PCI posting */
147 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
151 if (copy_from_user(&r
, argp
, sizeof(r
)))
157 r
.value
= ioread32(dev
->iaddr
+ r
.reg
);
159 if (copy_to_user(argp
, &r
, sizeof(r
)))
166 if (copy_from_user(&rs
, argp
, sizeof(rs
)))
169 m
= min(rs
.count
, 8U);
171 pr_debug("phantom: GRS %u regs %x\n", rs
.count
, rs
.mask
);
172 spin_lock_irqsave(&dev
->regs_lock
, flags
);
173 for (i
= 0; i
< m
; i
++)
174 if (rs
.mask
& BIT(i
))
175 rs
.values
[i
] = ioread32(dev
->iaddr
+ i
);
176 atomic_set(&dev
->counter
, 0);
177 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
179 if (copy_to_user(argp
, &rs
, sizeof(rs
)))
183 spin_lock_irqsave(&dev
->regs_lock
, flags
);
184 if (dev
->status
& PHB_RUNNING
) {
185 printk(KERN_ERR
"phantom: you need to set NOT_OH "
186 "before you start the device!\n");
187 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
190 dev
->status
|= PHB_NOT_OH
;
191 spin_unlock_irqrestore(&dev
->regs_lock
, flags
);
201 static long phantom_compat_ioctl(struct file
*filp
, unsigned int cmd
,
204 if (_IOC_NR(cmd
) <= 3 && _IOC_SIZE(cmd
) == sizeof(compat_uptr_t
)) {
205 cmd
&= ~(_IOC_SIZEMASK
<< _IOC_SIZESHIFT
);
206 cmd
|= sizeof(void *) << _IOC_SIZESHIFT
;
208 return phantom_ioctl(filp
, cmd
, (unsigned long)compat_ptr(arg
));
211 #define phantom_compat_ioctl NULL
214 static int phantom_open(struct inode
*inode
, struct file
*file
)
216 struct phantom_device
*dev
= container_of(inode
->i_cdev
,
217 struct phantom_device
, cdev
);
219 mutex_lock(&phantom_mutex
);
220 nonseekable_open(inode
, file
);
222 if (mutex_lock_interruptible(&dev
->open_lock
)) {
223 mutex_unlock(&phantom_mutex
);
228 mutex_unlock(&dev
->open_lock
);
229 mutex_unlock(&phantom_mutex
);
233 WARN_ON(dev
->status
& PHB_NOT_OH
);
235 file
->private_data
= dev
;
237 atomic_set(&dev
->counter
, 0);
239 mutex_unlock(&dev
->open_lock
);
240 mutex_unlock(&phantom_mutex
);
244 static int phantom_release(struct inode
*inode
, struct file
*file
)
246 struct phantom_device
*dev
= file
->private_data
;
248 mutex_lock(&dev
->open_lock
);
251 phantom_status(dev
, dev
->status
& ~PHB_RUNNING
);
252 dev
->status
&= ~PHB_NOT_OH
;
254 mutex_unlock(&dev
->open_lock
);
259 static unsigned int phantom_poll(struct file
*file
, poll_table
*wait
)
261 struct phantom_device
*dev
= file
->private_data
;
262 unsigned int mask
= 0;
264 pr_debug("phantom_poll: %d\n", atomic_read(&dev
->counter
));
265 poll_wait(file
, &dev
->wait
, wait
);
267 if (!(dev
->status
& PHB_RUNNING
))
269 else if (atomic_read(&dev
->counter
))
270 mask
= POLLIN
| POLLRDNORM
;
272 pr_debug("phantom_poll end: %x/%d\n", mask
, atomic_read(&dev
->counter
));
277 static const struct file_operations phantom_file_ops
= {
278 .open
= phantom_open
,
279 .release
= phantom_release
,
280 .unlocked_ioctl
= phantom_ioctl
,
281 .compat_ioctl
= phantom_compat_ioctl
,
282 .poll
= phantom_poll
,
286 static irqreturn_t
phantom_isr(int irq
, void *data
)
288 struct phantom_device
*dev
= data
;
292 spin_lock(&dev
->regs_lock
);
293 ctl
= ioread32(dev
->iaddr
+ PHN_CONTROL
);
294 if (!(ctl
& PHN_CTL_IRQ
)) {
295 spin_unlock(&dev
->regs_lock
);
299 iowrite32(0, dev
->iaddr
);
300 iowrite32(0xc0, dev
->iaddr
);
302 if (dev
->status
& PHB_NOT_OH
) {
303 struct phm_regs
*r
= &dev
->oregs
;
304 u32 m
= min(r
->count
, 8U);
306 for (i
= 0; i
< m
; i
++)
307 if (r
->mask
& BIT(i
))
308 iowrite32(r
->values
[i
], dev
->oaddr
+ i
);
310 dev
->ctl_reg
^= PHN_CTL_AMP
;
311 iowrite32(dev
->ctl_reg
, dev
->iaddr
+ PHN_CONTROL
);
313 spin_unlock(&dev
->regs_lock
);
315 ioread32(dev
->iaddr
); /* PCI posting */
317 atomic_inc(&dev
->counter
);
318 wake_up_interruptible(&dev
->wait
);
324 * Init and deinit driver
327 static unsigned int phantom_get_free(void)
331 for (i
= 0; i
< PHANTOM_MAX_MINORS
; i
++)
332 if (phantom_devices
[i
] == 0)
338 static int phantom_probe(struct pci_dev
*pdev
,
339 const struct pci_device_id
*pci_id
)
341 struct phantom_device
*pht
;
345 retval
= pci_enable_device(pdev
);
347 dev_err(&pdev
->dev
, "pci_enable_device failed!\n");
351 minor
= phantom_get_free();
352 if (minor
== PHANTOM_MAX_MINORS
) {
353 dev_err(&pdev
->dev
, "too many devices found!\n");
358 phantom_devices
[minor
] = 1;
360 retval
= pci_request_regions(pdev
, "phantom");
362 dev_err(&pdev
->dev
, "pci_request_regions failed!\n");
367 pht
= kzalloc(sizeof(*pht
), GFP_KERNEL
);
369 dev_err(&pdev
->dev
, "unable to allocate device\n");
373 pht
->caddr
= pci_iomap(pdev
, 0, 0);
374 if (pht
->caddr
== NULL
) {
375 dev_err(&pdev
->dev
, "can't remap conf space\n");
378 pht
->iaddr
= pci_iomap(pdev
, 2, 0);
379 if (pht
->iaddr
== NULL
) {
380 dev_err(&pdev
->dev
, "can't remap input space\n");
383 pht
->oaddr
= pci_iomap(pdev
, 3, 0);
384 if (pht
->oaddr
== NULL
) {
385 dev_err(&pdev
->dev
, "can't remap output space\n");
389 mutex_init(&pht
->open_lock
);
390 spin_lock_init(&pht
->regs_lock
);
391 init_waitqueue_head(&pht
->wait
);
392 cdev_init(&pht
->cdev
, &phantom_file_ops
);
393 pht
->cdev
.owner
= THIS_MODULE
;
395 iowrite32(0, pht
->caddr
+ PHN_IRQCTL
);
396 ioread32(pht
->caddr
+ PHN_IRQCTL
); /* PCI posting */
397 retval
= request_irq(pdev
->irq
, phantom_isr
,
398 IRQF_SHARED
, "phantom", pht
);
400 dev_err(&pdev
->dev
, "can't establish ISR\n");
404 retval
= cdev_add(&pht
->cdev
, MKDEV(phantom_major
, minor
), 1);
406 dev_err(&pdev
->dev
, "chardev registration failed\n");
410 if (IS_ERR(device_create(phantom_class
, &pdev
->dev
,
411 MKDEV(phantom_major
, minor
), NULL
,
412 "phantom%u", minor
)))
413 dev_err(&pdev
->dev
, "can't create device\n");
415 pci_set_drvdata(pdev
, pht
);
419 free_irq(pdev
->irq
, pht
);
421 pci_iounmap(pdev
, pht
->oaddr
);
423 pci_iounmap(pdev
, pht
->iaddr
);
425 pci_iounmap(pdev
, pht
->caddr
);
429 pci_release_regions(pdev
);
431 phantom_devices
[minor
] = 0;
433 pci_disable_device(pdev
);
438 static void phantom_remove(struct pci_dev
*pdev
)
440 struct phantom_device
*pht
= pci_get_drvdata(pdev
);
441 unsigned int minor
= MINOR(pht
->cdev
.dev
);
443 device_destroy(phantom_class
, MKDEV(phantom_major
, minor
));
445 cdev_del(&pht
->cdev
);
447 iowrite32(0, pht
->caddr
+ PHN_IRQCTL
);
448 ioread32(pht
->caddr
+ PHN_IRQCTL
); /* PCI posting */
449 free_irq(pdev
->irq
, pht
);
451 pci_iounmap(pdev
, pht
->oaddr
);
452 pci_iounmap(pdev
, pht
->iaddr
);
453 pci_iounmap(pdev
, pht
->caddr
);
457 pci_release_regions(pdev
);
459 phantom_devices
[minor
] = 0;
461 pci_disable_device(pdev
);
465 static int phantom_suspend(struct pci_dev
*pdev
, pm_message_t state
)
467 struct phantom_device
*dev
= pci_get_drvdata(pdev
);
469 iowrite32(0, dev
->caddr
+ PHN_IRQCTL
);
470 ioread32(dev
->caddr
+ PHN_IRQCTL
); /* PCI posting */
472 synchronize_irq(pdev
->irq
);
477 static int phantom_resume(struct pci_dev
*pdev
)
479 struct phantom_device
*dev
= pci_get_drvdata(pdev
);
481 iowrite32(0, dev
->caddr
+ PHN_IRQCTL
);
486 #define phantom_suspend NULL
487 #define phantom_resume NULL
490 static struct pci_device_id phantom_pci_tbl
[] = {
491 { .vendor
= PCI_VENDOR_ID_PLX
, .device
= PCI_DEVICE_ID_PLX_9050
,
492 .subvendor
= PCI_VENDOR_ID_PLX
, .subdevice
= PCI_DEVICE_ID_PLX_9050
,
493 .class = PCI_CLASS_BRIDGE_OTHER
<< 8, .class_mask
= 0xffff00 },
496 MODULE_DEVICE_TABLE(pci
, phantom_pci_tbl
);
498 static struct pci_driver phantom_pci_driver
= {
500 .id_table
= phantom_pci_tbl
,
501 .probe
= phantom_probe
,
502 .remove
= phantom_remove
,
503 .suspend
= phantom_suspend
,
504 .resume
= phantom_resume
507 static CLASS_ATTR_STRING(version
, 0444, PHANTOM_VERSION
);
509 static int __init
phantom_init(void)
514 phantom_class
= class_create(THIS_MODULE
, "phantom");
515 if (IS_ERR(phantom_class
)) {
516 retval
= PTR_ERR(phantom_class
);
517 printk(KERN_ERR
"phantom: can't register phantom class\n");
520 retval
= class_create_file(phantom_class
, &class_attr_version
.attr
);
522 printk(KERN_ERR
"phantom: can't create sysfs version file\n");
526 retval
= alloc_chrdev_region(&dev
, 0, PHANTOM_MAX_MINORS
, "phantom");
528 printk(KERN_ERR
"phantom: can't register character device\n");
531 phantom_major
= MAJOR(dev
);
533 retval
= pci_register_driver(&phantom_pci_driver
);
535 printk(KERN_ERR
"phantom: can't register pci driver\n");
539 printk(KERN_INFO
"Phantom Linux Driver, version " PHANTOM_VERSION
", "
544 unregister_chrdev_region(dev
, PHANTOM_MAX_MINORS
);
546 class_remove_file(phantom_class
, &class_attr_version
.attr
);
548 class_destroy(phantom_class
);
553 static void __exit
phantom_exit(void)
555 pci_unregister_driver(&phantom_pci_driver
);
557 unregister_chrdev_region(MKDEV(phantom_major
, 0), PHANTOM_MAX_MINORS
);
559 class_remove_file(phantom_class
, &class_attr_version
.attr
);
560 class_destroy(phantom_class
);
562 pr_debug("phantom: module successfully removed\n");
565 module_init(phantom_init
);
566 module_exit(phantom_exit
);
568 MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
569 MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
570 MODULE_LICENSE("GPL");
571 MODULE_VERSION(PHANTOM_VERSION
);