blob: 6e61a79de24c3b4e2801a958e8fb3c0ac2c6110c [file] [log] [blame]
Jiri Slabycef2cf02007-05-08 00:31:45 -07001/*
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
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/pci.h>
18#include <linux/fs.h>
19#include <linux/poll.h>
20#include <linux/interrupt.h>
21#include <linux/cdev.h>
22#include <linux/phantom.h>
23
24#include <asm/atomic.h>
25#include <asm/io.h>
26
27#define PHANTOM_VERSION "n0.9.5"
28
29#define PHANTOM_MAX_MINORS 8
30
31#define PHN_IRQCTL 0x4c /* irq control in caddr space */
32
33#define PHB_RUNNING 1
34
35static struct class *phantom_class;
36static int phantom_major;
37
38struct phantom_device {
39 unsigned int opened;
40 void __iomem *caddr;
41 u32 __iomem *iaddr;
42 u32 __iomem *oaddr;
43 unsigned long status;
44 atomic_t counter;
45
46 wait_queue_head_t wait;
47 struct cdev cdev;
48
49 struct mutex open_lock;
Jiri Slabyc15395c2007-05-23 13:57:58 -070050 spinlock_t ioctl_lock;
Jiri Slabycef2cf02007-05-08 00:31:45 -070051};
52
53static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
54
55static int phantom_status(struct phantom_device *dev, unsigned long newstat)
56{
57 pr_debug("phantom_status %lx %lx\n", dev->status, newstat);
58
59 if (!(dev->status & PHB_RUNNING) && (newstat & PHB_RUNNING)) {
60 atomic_set(&dev->counter, 0);
61 iowrite32(PHN_CTL_IRQ, dev->iaddr + PHN_CONTROL);
62 iowrite32(0x43, dev->caddr + PHN_IRQCTL);
Jiri Slabyc8511f92007-05-23 13:57:59 -070063 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
64 } else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) {
Jiri Slabycef2cf02007-05-08 00:31:45 -070065 iowrite32(0, dev->caddr + PHN_IRQCTL);
Jiri Slabyc8511f92007-05-23 13:57:59 -070066 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
67 }
Jiri Slabycef2cf02007-05-08 00:31:45 -070068
69 dev->status = newstat;
70
71 return 0;
72}
73
74/*
75 * File ops
76 */
77
Jiri Slabyc15395c2007-05-23 13:57:58 -070078static long phantom_ioctl(struct file *file, unsigned int cmd,
79 unsigned long arg)
Jiri Slabycef2cf02007-05-08 00:31:45 -070080{
81 struct phantom_device *dev = file->private_data;
82 struct phm_regs rs;
83 struct phm_reg r;
84 void __user *argp = (void __user *)arg;
85 unsigned int i;
86
87 if (_IOC_TYPE(cmd) != PH_IOC_MAGIC ||
88 _IOC_NR(cmd) > PH_IOC_MAXNR)
89 return -ENOTTY;
90
91 switch (cmd) {
92 case PHN_SET_REG:
93 if (copy_from_user(&r, argp, sizeof(r)))
94 return -EFAULT;
95
96 if (r.reg > 7)
97 return -EINVAL;
98
Jiri Slabyc15395c2007-05-23 13:57:58 -070099 spin_lock(&dev->ioctl_lock);
Jiri Slabycef2cf02007-05-08 00:31:45 -0700100 if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) &&
Jiri Slabyc15395c2007-05-23 13:57:58 -0700101 phantom_status(dev, dev->status | PHB_RUNNING)){
102 spin_unlock(&dev->ioctl_lock);
Jiri Slabycef2cf02007-05-08 00:31:45 -0700103 return -ENODEV;
Jiri Slabyc15395c2007-05-23 13:57:58 -0700104 }
Jiri Slabycef2cf02007-05-08 00:31:45 -0700105
106 pr_debug("phantom: writing %x to %u\n", r.value, r.reg);
107 iowrite32(r.value, dev->iaddr + r.reg);
Jiri Slabyc8511f92007-05-23 13:57:59 -0700108 ioread32(dev->iaddr); /* PCI posting */
Jiri Slabycef2cf02007-05-08 00:31:45 -0700109
110 if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ))
111 phantom_status(dev, dev->status & ~PHB_RUNNING);
Jiri Slabyc15395c2007-05-23 13:57:58 -0700112 spin_unlock(&dev->ioctl_lock);
Jiri Slabycef2cf02007-05-08 00:31:45 -0700113 break;
114 case PHN_SET_REGS:
115 if (copy_from_user(&rs, argp, sizeof(rs)))
116 return -EFAULT;
117
118 pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
Jiri Slabyc15395c2007-05-23 13:57:58 -0700119 spin_lock(&dev->ioctl_lock);
Jiri Slabycef2cf02007-05-08 00:31:45 -0700120 for (i = 0; i < min(rs.count, 8U); i++)
121 if ((1 << i) & rs.mask)
122 iowrite32(rs.values[i], dev->oaddr + i);
Jiri Slabyc8511f92007-05-23 13:57:59 -0700123 ioread32(dev->iaddr); /* PCI posting */
Jiri Slabyc15395c2007-05-23 13:57:58 -0700124 spin_unlock(&dev->ioctl_lock);
Jiri Slabycef2cf02007-05-08 00:31:45 -0700125 break;
126 case PHN_GET_REG:
127 if (copy_from_user(&r, argp, sizeof(r)))
128 return -EFAULT;
129
130 if (r.reg > 7)
131 return -EINVAL;
132
133 r.value = ioread32(dev->iaddr + r.reg);
134
135 if (copy_to_user(argp, &r, sizeof(r)))
136 return -EFAULT;
137 break;
138 case PHN_GET_REGS:
139 if (copy_from_user(&rs, argp, sizeof(rs)))
140 return -EFAULT;
141
142 pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
Jiri Slabyc15395c2007-05-23 13:57:58 -0700143 spin_lock(&dev->ioctl_lock);
Jiri Slabycef2cf02007-05-08 00:31:45 -0700144 for (i = 0; i < min(rs.count, 8U); i++)
145 if ((1 << i) & rs.mask)
146 rs.values[i] = ioread32(dev->iaddr + i);
Jiri Slabyc15395c2007-05-23 13:57:58 -0700147 spin_unlock(&dev->ioctl_lock);
Jiri Slabycef2cf02007-05-08 00:31:45 -0700148
149 if (copy_to_user(argp, &rs, sizeof(rs)))
150 return -EFAULT;
151 break;
152 default:
153 return -ENOTTY;
154 }
155
156 return 0;
157}
158
159static int phantom_open(struct inode *inode, struct file *file)
160{
161 struct phantom_device *dev = container_of(inode->i_cdev,
162 struct phantom_device, cdev);
163
164 nonseekable_open(inode, file);
165
166 if (mutex_lock_interruptible(&dev->open_lock))
167 return -ERESTARTSYS;
168
169 if (dev->opened) {
170 mutex_unlock(&dev->open_lock);
171 return -EINVAL;
172 }
173
174 file->private_data = dev;
175
176 dev->opened++;
177 mutex_unlock(&dev->open_lock);
178
179 return 0;
180}
181
182static int phantom_release(struct inode *inode, struct file *file)
183{
184 struct phantom_device *dev = file->private_data;
185
186 mutex_lock(&dev->open_lock);
187
188 dev->opened = 0;
189 phantom_status(dev, dev->status & ~PHB_RUNNING);
190
191 mutex_unlock(&dev->open_lock);
192
193 return 0;
194}
195
196static unsigned int phantom_poll(struct file *file, poll_table *wait)
197{
198 struct phantom_device *dev = file->private_data;
199 unsigned int mask = 0;
200
201 pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
202 poll_wait(file, &dev->wait, wait);
203 if (atomic_read(&dev->counter)) {
204 mask = POLLIN | POLLRDNORM;
205 atomic_dec(&dev->counter);
206 } else if ((dev->status & PHB_RUNNING) == 0)
207 mask = POLLIN | POLLRDNORM | POLLERR;
208 pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
209
210 return mask;
211}
212
213static struct file_operations phantom_file_ops = {
214 .open = phantom_open,
215 .release = phantom_release,
Jiri Slabyc15395c2007-05-23 13:57:58 -0700216 .unlocked_ioctl = phantom_ioctl,
Jiri Slabycef2cf02007-05-08 00:31:45 -0700217 .poll = phantom_poll,
218};
219
220static irqreturn_t phantom_isr(int irq, void *data)
221{
222 struct phantom_device *dev = data;
223
224 if (!(ioread32(dev->iaddr + PHN_CONTROL) & PHN_CTL_IRQ))
225 return IRQ_NONE;
226
227 iowrite32(0, dev->iaddr);
228 iowrite32(0xc0, dev->iaddr);
Jiri Slabyc8511f92007-05-23 13:57:59 -0700229 ioread32(dev->iaddr); /* PCI posting */
Jiri Slabycef2cf02007-05-08 00:31:45 -0700230
231 atomic_inc(&dev->counter);
232 wake_up_interruptible(&dev->wait);
233
234 return IRQ_HANDLED;
235}
236
237/*
238 * Init and deinit driver
239 */
240
241static unsigned int __devinit phantom_get_free(void)
242{
243 unsigned int i;
244
245 for (i = 0; i < PHANTOM_MAX_MINORS; i++)
246 if (phantom_devices[i] == 0)
247 break;
248
249 return i;
250}
251
252static int __devinit phantom_probe(struct pci_dev *pdev,
253 const struct pci_device_id *pci_id)
254{
255 struct phantom_device *pht;
256 unsigned int minor;
257 int retval;
258
259 retval = pci_enable_device(pdev);
260 if (retval)
261 goto err;
262
263 minor = phantom_get_free();
264 if (minor == PHANTOM_MAX_MINORS) {
265 dev_err(&pdev->dev, "too many devices found!\n");
266 retval = -EIO;
267 goto err_dis;
268 }
269
270 phantom_devices[minor] = 1;
271
272 retval = pci_request_regions(pdev, "phantom");
273 if (retval)
274 goto err_null;
275
276 retval = -ENOMEM;
277 pht = kzalloc(sizeof(*pht), GFP_KERNEL);
278 if (pht == NULL) {
279 dev_err(&pdev->dev, "unable to allocate device\n");
280 goto err_reg;
281 }
282
283 pht->caddr = pci_iomap(pdev, 0, 0);
284 if (pht->caddr == NULL) {
285 dev_err(&pdev->dev, "can't remap conf space\n");
286 goto err_fr;
287 }
288 pht->iaddr = pci_iomap(pdev, 2, 0);
289 if (pht->iaddr == NULL) {
290 dev_err(&pdev->dev, "can't remap input space\n");
291 goto err_unmc;
292 }
293 pht->oaddr = pci_iomap(pdev, 3, 0);
294 if (pht->oaddr == NULL) {
295 dev_err(&pdev->dev, "can't remap output space\n");
296 goto err_unmi;
297 }
298
299 mutex_init(&pht->open_lock);
Jiri Slabyc15395c2007-05-23 13:57:58 -0700300 spin_lock_init(&pht->ioctl_lock);
Jiri Slabycef2cf02007-05-08 00:31:45 -0700301 init_waitqueue_head(&pht->wait);
302 cdev_init(&pht->cdev, &phantom_file_ops);
303 pht->cdev.owner = THIS_MODULE;
304
305 iowrite32(0, pht->caddr + PHN_IRQCTL);
Jiri Slabyc8511f92007-05-23 13:57:59 -0700306 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
Jiri Slabycef2cf02007-05-08 00:31:45 -0700307 retval = request_irq(pdev->irq, phantom_isr,
308 IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
309 if (retval) {
310 dev_err(&pdev->dev, "can't establish ISR\n");
311 goto err_unmo;
312 }
313
314 retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
315 if (retval) {
316 dev_err(&pdev->dev, "chardev registration failed\n");
317 goto err_irq;
318 }
319
320 if (IS_ERR(device_create(phantom_class, &pdev->dev, MKDEV(phantom_major,
321 minor), "phantom%u", minor)))
322 dev_err(&pdev->dev, "can't create device\n");
323
324 pci_set_drvdata(pdev, pht);
325
326 return 0;
327err_irq:
328 free_irq(pdev->irq, pht);
329err_unmo:
330 pci_iounmap(pdev, pht->oaddr);
331err_unmi:
332 pci_iounmap(pdev, pht->iaddr);
333err_unmc:
334 pci_iounmap(pdev, pht->caddr);
335err_fr:
336 kfree(pht);
337err_reg:
338 pci_release_regions(pdev);
339err_null:
340 phantom_devices[minor] = 0;
341err_dis:
342 pci_disable_device(pdev);
343err:
344 return retval;
345}
346
347static void __devexit phantom_remove(struct pci_dev *pdev)
348{
349 struct phantom_device *pht = pci_get_drvdata(pdev);
350 unsigned int minor = MINOR(pht->cdev.dev);
351
352 device_destroy(phantom_class, MKDEV(phantom_major, minor));
353
354 cdev_del(&pht->cdev);
355
356 iowrite32(0, pht->caddr + PHN_IRQCTL);
Jiri Slabyc8511f92007-05-23 13:57:59 -0700357 ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
Jiri Slabycef2cf02007-05-08 00:31:45 -0700358 free_irq(pdev->irq, pht);
359
360 pci_iounmap(pdev, pht->oaddr);
361 pci_iounmap(pdev, pht->iaddr);
362 pci_iounmap(pdev, pht->caddr);
363
364 kfree(pht);
365
366 pci_release_regions(pdev);
367
368 phantom_devices[minor] = 0;
369
370 pci_disable_device(pdev);
371}
372
373#ifdef CONFIG_PM
374static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
375{
376 struct phantom_device *dev = pci_get_drvdata(pdev);
377
378 iowrite32(0, dev->caddr + PHN_IRQCTL);
Jiri Slabyc8511f92007-05-23 13:57:59 -0700379 ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
Jiri Slabycef2cf02007-05-08 00:31:45 -0700380
Jiri Slabyaee84472007-10-18 23:40:23 -0700381 synchronize_irq(pdev->irq);
382
Jiri Slabycef2cf02007-05-08 00:31:45 -0700383 return 0;
384}
385
386static int phantom_resume(struct pci_dev *pdev)
387{
388 struct phantom_device *dev = pci_get_drvdata(pdev);
389
390 iowrite32(0, dev->caddr + PHN_IRQCTL);
391
392 return 0;
393}
394#else
395#define phantom_suspend NULL
396#define phantom_resume NULL
397#endif
398
399static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
400 { PCI_DEVICE(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050),
401 .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
402 { 0, }
403};
404MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
405
406static struct pci_driver phantom_pci_driver = {
407 .name = "phantom",
408 .id_table = phantom_pci_tbl,
409 .probe = phantom_probe,
410 .remove = __devexit_p(phantom_remove),
411 .suspend = phantom_suspend,
412 .resume = phantom_resume
413};
414
415static ssize_t phantom_show_version(struct class *cls, char *buf)
416{
417 return sprintf(buf, PHANTOM_VERSION "\n");
418}
419
420static CLASS_ATTR(version, 0444, phantom_show_version, NULL);
421
422static int __init phantom_init(void)
423{
424 int retval;
425 dev_t dev;
426
427 phantom_class = class_create(THIS_MODULE, "phantom");
428 if (IS_ERR(phantom_class)) {
429 retval = PTR_ERR(phantom_class);
430 printk(KERN_ERR "phantom: can't register phantom class\n");
431 goto err;
432 }
433 retval = class_create_file(phantom_class, &class_attr_version);
434 if (retval) {
435 printk(KERN_ERR "phantom: can't create sysfs version file\n");
436 goto err_class;
437 }
438
439 retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
440 if (retval) {
441 printk(KERN_ERR "phantom: can't register character device\n");
442 goto err_attr;
443 }
444 phantom_major = MAJOR(dev);
445
446 retval = pci_register_driver(&phantom_pci_driver);
447 if (retval) {
448 printk(KERN_ERR "phantom: can't register pci driver\n");
449 goto err_unchr;
450 }
451
452 printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
453 "init OK\n");
454
455 return 0;
456err_unchr:
457 unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
458err_attr:
459 class_remove_file(phantom_class, &class_attr_version);
460err_class:
461 class_destroy(phantom_class);
462err:
463 return retval;
464}
465
466static void __exit phantom_exit(void)
467{
468 pci_unregister_driver(&phantom_pci_driver);
469
470 unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
471
472 class_remove_file(phantom_class, &class_attr_version);
473 class_destroy(phantom_class);
474
475 pr_debug("phantom: module successfully removed\n");
476}
477
478module_init(phantom_init);
479module_exit(phantom_exit);
480
481MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
482MODULE_DESCRIPTION("Sensable Phantom driver");
483MODULE_LICENSE("GPL");
484MODULE_VERSION(PHANTOM_VERSION);