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