Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[deliverable/linux.git] / drivers / misc / phantom.c
CommitLineData
cef2cf07
JS
1/*
2 * Copyright (C) 2005-2007 Jiri Slaby <jirislaby@gmail.com>
3 *
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.
8 *
0211a9c8 9 * You need a userspace library to cooperate with this driver. It (and other
cef2cf07
JS
10 * info) may be obtained here:
11 * http://www.fi.muni.cz/~xslaby/phantom.html
b2afe331 12 * or alternatively, you might use OpenHaptics provided by Sensable.
cef2cf07
JS
13 */
14
7e4e8e68 15#include <linux/compat.h>
cef2cf07
JS
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/device.h>
19#include <linux/pci.h>
20#include <linux/fs.h>
21#include <linux/poll.h>
22#include <linux/interrupt.h>
23#include <linux/cdev.h>
5a0e3ad6 24#include <linux/slab.h>
cef2cf07 25#include <linux/phantom.h>
d43c36dc 26#include <linux/sched.h>
613655fa 27#include <linux/mutex.h>
cef2cf07
JS
28
29#include <asm/atomic.h>
30#include <asm/io.h>
31
82f56087 32#define PHANTOM_VERSION "n0.9.8"
cef2cf07
JS
33
34#define PHANTOM_MAX_MINORS 8
35
36#define PHN_IRQCTL 0x4c /* irq control in caddr space */
37
38#define PHB_RUNNING 1
bc552f77 39#define PHB_NOT_OH 2
cef2cf07 40
613655fa 41static DEFINE_MUTEX(phantom_mutex);
cef2cf07
JS
42static struct class *phantom_class;
43static int phantom_major;
44
45struct phantom_device {
46 unsigned int opened;
47 void __iomem *caddr;
48 u32 __iomem *iaddr;
49 u32 __iomem *oaddr;
50 unsigned long status;
51 atomic_t counter;
52
53 wait_queue_head_t wait;
54 struct cdev cdev;
55
56 struct mutex open_lock;
bc552f77
JS
57 spinlock_t regs_lock;
58
59 /* used in NOT_OH mode */
60 struct phm_regs oregs;
61 u32 ctl_reg;
cef2cf07
JS
62};
63
64static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
65
66static int phantom_status(struct phantom_device *dev, unsigned long newstat)
67{
68 pr_debug("phantom_status %lx %lx\n", dev->status, newstat);
69
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);
c8511f94
JS
74 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
75 } else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) {
cef2cf07 76 iowrite32(0, dev->caddr + PHN_IRQCTL);
c8511f94
JS
77 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
78 }
cef2cf07
JS
79
80 dev->status = newstat;
81
82 return 0;
83}
84
85/*
86 * File ops
87 */
88
c15395c0
JS
89static long phantom_ioctl(struct file *file, unsigned int cmd,
90 unsigned long arg)
cef2cf07
JS
91{
92 struct phantom_device *dev = file->private_data;
93 struct phm_regs rs;
94 struct phm_reg r;
95 void __user *argp = (void __user *)arg;
bc552f77 96 unsigned long flags;
cef2cf07
JS
97 unsigned int i;
98
cef2cf07 99 switch (cmd) {
7e4e8e68 100 case PHN_SETREG:
cef2cf07
JS
101 case PHN_SET_REG:
102 if (copy_from_user(&r, argp, sizeof(r)))
103 return -EFAULT;
104
105 if (r.reg > 7)
106 return -EINVAL;
107
bc552f77 108 spin_lock_irqsave(&dev->regs_lock, flags);
cef2cf07 109 if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) &&
c15395c0 110 phantom_status(dev, dev->status | PHB_RUNNING)){
bc552f77 111 spin_unlock_irqrestore(&dev->regs_lock, flags);
cef2cf07 112 return -ENODEV;
c15395c0 113 }
cef2cf07
JS
114
115 pr_debug("phantom: writing %x to %u\n", r.value, r.reg);
bc552f77
JS
116
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;
122 }
123
cef2cf07 124 iowrite32(r.value, dev->iaddr + r.reg);
c8511f94 125 ioread32(dev->iaddr); /* PCI posting */
cef2cf07
JS
126
127 if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ))
128 phantom_status(dev, dev->status & ~PHB_RUNNING);
bc552f77 129 spin_unlock_irqrestore(&dev->regs_lock, flags);
cef2cf07 130 break;
7e4e8e68 131 case PHN_SETREGS:
cef2cf07
JS
132 case PHN_SET_REGS:
133 if (copy_from_user(&rs, argp, sizeof(rs)))
134 return -EFAULT;
135
136 pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
bc552f77
JS
137 spin_lock_irqsave(&dev->regs_lock, flags);
138 if (dev->status & PHB_NOT_OH)
139 memcpy(&dev->oregs, &rs, sizeof(rs));
140 else {
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 */
146 }
147 spin_unlock_irqrestore(&dev->regs_lock, flags);
cef2cf07 148 break;
7e4e8e68 149 case PHN_GETREG:
cef2cf07
JS
150 case PHN_GET_REG:
151 if (copy_from_user(&r, argp, sizeof(r)))
152 return -EFAULT;
153
154 if (r.reg > 7)
155 return -EINVAL;
156
157 r.value = ioread32(dev->iaddr + r.reg);
158
159 if (copy_to_user(argp, &r, sizeof(r)))
160 return -EFAULT;
161 break;
7e4e8e68 162 case PHN_GETREGS:
bc552f77
JS
163 case PHN_GET_REGS: {
164 u32 m;
165
cef2cf07
JS
166 if (copy_from_user(&rs, argp, sizeof(rs)))
167 return -EFAULT;
168
bc552f77
JS
169 m = min(rs.count, 8U);
170
cef2cf07 171 pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
bc552f77
JS
172 spin_lock_irqsave(&dev->regs_lock, flags);
173 for (i = 0; i < m; i++)
174 if (rs.mask & BIT(i))
cef2cf07 175 rs.values[i] = ioread32(dev->iaddr + i);
7d4f9f09 176 atomic_set(&dev->counter, 0);
bc552f77 177 spin_unlock_irqrestore(&dev->regs_lock, flags);
cef2cf07
JS
178
179 if (copy_to_user(argp, &rs, sizeof(rs)))
180 return -EFAULT;
181 break;
bc552f77
JS
182 } case PHN_NOT_OH:
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);
188 return -EINVAL;
189 }
190 dev->status |= PHB_NOT_OH;
191 spin_unlock_irqrestore(&dev->regs_lock, flags);
192 break;
cef2cf07
JS
193 default:
194 return -ENOTTY;
195 }
196
197 return 0;
198}
199
7e4e8e68
JS
200#ifdef CONFIG_COMPAT
201static long phantom_compat_ioctl(struct file *filp, unsigned int cmd,
202 unsigned long arg)
203{
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;
207 }
208 return phantom_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
209}
210#else
211#define phantom_compat_ioctl NULL
212#endif
213
cef2cf07
JS
214static int phantom_open(struct inode *inode, struct file *file)
215{
216 struct phantom_device *dev = container_of(inode->i_cdev,
217 struct phantom_device, cdev);
218
613655fa 219 mutex_lock(&phantom_mutex);
cef2cf07
JS
220 nonseekable_open(inode, file);
221
4541b5ec 222 if (mutex_lock_interruptible(&dev->open_lock)) {
613655fa 223 mutex_unlock(&phantom_mutex);
cef2cf07 224 return -ERESTARTSYS;
4541b5ec 225 }
cef2cf07
JS
226
227 if (dev->opened) {
228 mutex_unlock(&dev->open_lock);
613655fa 229 mutex_unlock(&phantom_mutex);
cef2cf07
JS
230 return -EINVAL;
231 }
232
bc552f77
JS
233 WARN_ON(dev->status & PHB_NOT_OH);
234
cef2cf07
JS
235 file->private_data = dev;
236
bc552f77 237 atomic_set(&dev->counter, 0);
cef2cf07
JS
238 dev->opened++;
239 mutex_unlock(&dev->open_lock);
613655fa 240 mutex_unlock(&phantom_mutex);
cef2cf07
JS
241 return 0;
242}
243
244static int phantom_release(struct inode *inode, struct file *file)
245{
246 struct phantom_device *dev = file->private_data;
247
248 mutex_lock(&dev->open_lock);
249
250 dev->opened = 0;
251 phantom_status(dev, dev->status & ~PHB_RUNNING);
bc552f77 252 dev->status &= ~PHB_NOT_OH;
cef2cf07
JS
253
254 mutex_unlock(&dev->open_lock);
255
256 return 0;
257}
258
259static unsigned int phantom_poll(struct file *file, poll_table *wait)
260{
261 struct phantom_device *dev = file->private_data;
262 unsigned int mask = 0;
263
264 pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
265 poll_wait(file, &dev->wait, wait);
7d4f9f09
JS
266
267 if (!(dev->status & PHB_RUNNING))
268 mask = POLLERR;
269 else if (atomic_read(&dev->counter))
cef2cf07 270 mask = POLLIN | POLLRDNORM;
7d4f9f09 271
cef2cf07
JS
272 pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
273
274 return mask;
275}
276
828c0950 277static const struct file_operations phantom_file_ops = {
cef2cf07
JS
278 .open = phantom_open,
279 .release = phantom_release,
c15395c0 280 .unlocked_ioctl = phantom_ioctl,
7e4e8e68 281 .compat_ioctl = phantom_compat_ioctl,
cef2cf07 282 .poll = phantom_poll,
6038f373 283 .llseek = no_llseek,
cef2cf07
JS
284};
285
286static irqreturn_t phantom_isr(int irq, void *data)
287{
288 struct phantom_device *dev = data;
bc552f77
JS
289 unsigned int i;
290 u32 ctl;
cef2cf07 291
bc552f77
JS
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);
cef2cf07 296 return IRQ_NONE;
bc552f77 297 }
cef2cf07
JS
298
299 iowrite32(0, dev->iaddr);
300 iowrite32(0xc0, dev->iaddr);
bc552f77
JS
301
302 if (dev->status & PHB_NOT_OH) {
303 struct phm_regs *r = &dev->oregs;
304 u32 m = min(r->count, 8U);
305
306 for (i = 0; i < m; i++)
307 if (r->mask & BIT(i))
308 iowrite32(r->values[i], dev->oaddr + i);
309
310 dev->ctl_reg ^= PHN_CTL_AMP;
311 iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
312 }
313 spin_unlock(&dev->regs_lock);
314
c8511f94 315 ioread32(dev->iaddr); /* PCI posting */
cef2cf07
JS
316
317 atomic_inc(&dev->counter);
318 wake_up_interruptible(&dev->wait);
319
320 return IRQ_HANDLED;
321}
322
323/*
324 * Init and deinit driver
325 */
326
327static unsigned int __devinit phantom_get_free(void)
328{
329 unsigned int i;
330
331 for (i = 0; i < PHANTOM_MAX_MINORS; i++)
332 if (phantom_devices[i] == 0)
333 break;
334
335 return i;
336}
337
338static int __devinit phantom_probe(struct pci_dev *pdev,
339 const struct pci_device_id *pci_id)
340{
341 struct phantom_device *pht;
342 unsigned int minor;
343 int retval;
344
345 retval = pci_enable_device(pdev);
346 if (retval)
347 goto err;
348
349 minor = phantom_get_free();
350 if (minor == PHANTOM_MAX_MINORS) {
351 dev_err(&pdev->dev, "too many devices found!\n");
352 retval = -EIO;
353 goto err_dis;
354 }
355
356 phantom_devices[minor] = 1;
357
358 retval = pci_request_regions(pdev, "phantom");
359 if (retval)
360 goto err_null;
361
362 retval = -ENOMEM;
363 pht = kzalloc(sizeof(*pht), GFP_KERNEL);
364 if (pht == NULL) {
365 dev_err(&pdev->dev, "unable to allocate device\n");
366 goto err_reg;
367 }
368
369 pht->caddr = pci_iomap(pdev, 0, 0);
370 if (pht->caddr == NULL) {
371 dev_err(&pdev->dev, "can't remap conf space\n");
372 goto err_fr;
373 }
374 pht->iaddr = pci_iomap(pdev, 2, 0);
375 if (pht->iaddr == NULL) {
376 dev_err(&pdev->dev, "can't remap input space\n");
377 goto err_unmc;
378 }
379 pht->oaddr = pci_iomap(pdev, 3, 0);
380 if (pht->oaddr == NULL) {
381 dev_err(&pdev->dev, "can't remap output space\n");
382 goto err_unmi;
383 }
384
385 mutex_init(&pht->open_lock);
bc552f77 386 spin_lock_init(&pht->regs_lock);
cef2cf07
JS
387 init_waitqueue_head(&pht->wait);
388 cdev_init(&pht->cdev, &phantom_file_ops);
389 pht->cdev.owner = THIS_MODULE;
390
391 iowrite32(0, pht->caddr + PHN_IRQCTL);
c8511f94 392 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
cef2cf07
JS
393 retval = request_irq(pdev->irq, phantom_isr,
394 IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
395 if (retval) {
396 dev_err(&pdev->dev, "can't establish ISR\n");
397 goto err_unmo;
398 }
399
400 retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
401 if (retval) {
402 dev_err(&pdev->dev, "chardev registration failed\n");
403 goto err_irq;
404 }
405
a9b12619
GKH
406 if (IS_ERR(device_create(phantom_class, &pdev->dev,
407 MKDEV(phantom_major, minor), NULL,
408 "phantom%u", minor)))
cef2cf07
JS
409 dev_err(&pdev->dev, "can't create device\n");
410
411 pci_set_drvdata(pdev, pht);
412
413 return 0;
414err_irq:
415 free_irq(pdev->irq, pht);
416err_unmo:
417 pci_iounmap(pdev, pht->oaddr);
418err_unmi:
419 pci_iounmap(pdev, pht->iaddr);
420err_unmc:
421 pci_iounmap(pdev, pht->caddr);
422err_fr:
423 kfree(pht);
424err_reg:
425 pci_release_regions(pdev);
426err_null:
427 phantom_devices[minor] = 0;
428err_dis:
429 pci_disable_device(pdev);
430err:
431 return retval;
432}
433
434static void __devexit phantom_remove(struct pci_dev *pdev)
435{
436 struct phantom_device *pht = pci_get_drvdata(pdev);
437 unsigned int minor = MINOR(pht->cdev.dev);
438
439 device_destroy(phantom_class, MKDEV(phantom_major, minor));
440
441 cdev_del(&pht->cdev);
442
443 iowrite32(0, pht->caddr + PHN_IRQCTL);
c8511f94 444 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
cef2cf07
JS
445 free_irq(pdev->irq, pht);
446
447 pci_iounmap(pdev, pht->oaddr);
448 pci_iounmap(pdev, pht->iaddr);
449 pci_iounmap(pdev, pht->caddr);
450
451 kfree(pht);
452
453 pci_release_regions(pdev);
454
455 phantom_devices[minor] = 0;
456
457 pci_disable_device(pdev);
458}
459
460#ifdef CONFIG_PM
461static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
462{
463 struct phantom_device *dev = pci_get_drvdata(pdev);
464
465 iowrite32(0, dev->caddr + PHN_IRQCTL);
c8511f94 466 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
cef2cf07 467
aee8447c
JS
468 synchronize_irq(pdev->irq);
469
cef2cf07
JS
470 return 0;
471}
472
473static int phantom_resume(struct pci_dev *pdev)
474{
475 struct phantom_device *dev = pci_get_drvdata(pdev);
476
477 iowrite32(0, dev->caddr + PHN_IRQCTL);
478
479 return 0;
480}
481#else
482#define phantom_suspend NULL
483#define phantom_resume NULL
484#endif
485
486static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
82f56087
JS
487 { .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
488 .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
489 .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
cef2cf07
JS
490 { 0, }
491};
492MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
493
494static struct pci_driver phantom_pci_driver = {
495 .name = "phantom",
496 .id_table = phantom_pci_tbl,
497 .probe = phantom_probe,
498 .remove = __devexit_p(phantom_remove),
499 .suspend = phantom_suspend,
500 .resume = phantom_resume
501};
502
0933e2d9 503static CLASS_ATTR_STRING(version, 0444, PHANTOM_VERSION);
cef2cf07
JS
504
505static int __init phantom_init(void)
506{
507 int retval;
508 dev_t dev;
509
510 phantom_class = class_create(THIS_MODULE, "phantom");
511 if (IS_ERR(phantom_class)) {
512 retval = PTR_ERR(phantom_class);
513 printk(KERN_ERR "phantom: can't register phantom class\n");
514 goto err;
515 }
0933e2d9 516 retval = class_create_file(phantom_class, &class_attr_version.attr);
cef2cf07
JS
517 if (retval) {
518 printk(KERN_ERR "phantom: can't create sysfs version file\n");
519 goto err_class;
520 }
521
522 retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
523 if (retval) {
524 printk(KERN_ERR "phantom: can't register character device\n");
525 goto err_attr;
526 }
527 phantom_major = MAJOR(dev);
528
529 retval = pci_register_driver(&phantom_pci_driver);
530 if (retval) {
531 printk(KERN_ERR "phantom: can't register pci driver\n");
532 goto err_unchr;
533 }
534
535 printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
536 "init OK\n");
537
538 return 0;
539err_unchr:
540 unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
541err_attr:
0933e2d9 542 class_remove_file(phantom_class, &class_attr_version.attr);
cef2cf07
JS
543err_class:
544 class_destroy(phantom_class);
545err:
546 return retval;
547}
548
549static void __exit phantom_exit(void)
550{
551 pci_unregister_driver(&phantom_pci_driver);
552
553 unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
554
0933e2d9 555 class_remove_file(phantom_class, &class_attr_version.attr);
cef2cf07
JS
556 class_destroy(phantom_class);
557
558 pr_debug("phantom: module successfully removed\n");
559}
560
561module_init(phantom_init);
562module_exit(phantom_exit);
563
564MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
ec905a18 565MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
cef2cf07
JS
566MODULE_LICENSE("GPL");
567MODULE_VERSION(PHANTOM_VERSION);
This page took 0.503513 seconds and 5 git commands to generate.