phantom.c 13.1 KB
Newer Older
J
Jiri Slaby 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 *  Copyright (C) 2005-2007 Jiri Slaby <jirislaby@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  You need an userspace library to cooperate with this driver. It (and other
 *  info) may be obtained here:
 *  http://www.fi.muni.cz/~xslaby/phantom.html
12
 *  or alternatively, you might use OpenHaptics provided by Sensable.
J
Jiri Slaby 已提交
13 14
 */

J
Jiri Slaby 已提交
15
#include <linux/compat.h>
J
Jiri Slaby 已提交
16 17 18 19 20 21 22 23 24
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/pci.h>
#include <linux/fs.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>
#include <linux/phantom.h>
J
Jonathan Corbet 已提交
25
#include <linux/smp_lock.h>
J
Jiri Slaby 已提交
26 27 28 29

#include <asm/atomic.h>
#include <asm/io.h>

J
Jiri Slaby 已提交
30
#define PHANTOM_VERSION		"n0.9.8"
J
Jiri Slaby 已提交
31 32 33 34 35 36

#define PHANTOM_MAX_MINORS	8

#define PHN_IRQCTL		0x4c    /* irq control in caddr space */

#define PHB_RUNNING		1
J
Jiri Slaby 已提交
37
#define PHB_NOT_OH		2
J
Jiri Slaby 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

static struct class *phantom_class;
static int phantom_major;

struct phantom_device {
	unsigned int opened;
	void __iomem *caddr;
	u32 __iomem *iaddr;
	u32 __iomem *oaddr;
	unsigned long status;
	atomic_t counter;

	wait_queue_head_t wait;
	struct cdev cdev;

	struct mutex open_lock;
J
Jiri Slaby 已提交
54 55 56 57 58
	spinlock_t regs_lock;

	/* used in NOT_OH mode */
	struct phm_regs oregs;
	u32 ctl_reg;
J
Jiri Slaby 已提交
59 60 61 62 63 64 65 66 67 68 69 70
};

static unsigned char phantom_devices[PHANTOM_MAX_MINORS];

static int phantom_status(struct phantom_device *dev, unsigned long newstat)
{
	pr_debug("phantom_status %lx %lx\n", dev->status, newstat);

	if (!(dev->status & PHB_RUNNING) && (newstat & PHB_RUNNING)) {
		atomic_set(&dev->counter, 0);
		iowrite32(PHN_CTL_IRQ, dev->iaddr + PHN_CONTROL);
		iowrite32(0x43, dev->caddr + PHN_IRQCTL);
71 72
		ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
	} else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING)) {
J
Jiri Slaby 已提交
73
		iowrite32(0, dev->caddr + PHN_IRQCTL);
74 75
		ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
	}
J
Jiri Slaby 已提交
76 77 78 79 80 81 82 83 84 85

	dev->status = newstat;

	return 0;
}

/*
 * File ops
 */

J
Jiri Slaby 已提交
86 87
static long phantom_ioctl(struct file *file, unsigned int cmd,
		unsigned long arg)
J
Jiri Slaby 已提交
88 89 90 91 92
{
	struct phantom_device *dev = file->private_data;
	struct phm_regs rs;
	struct phm_reg r;
	void __user *argp = (void __user *)arg;
J
Jiri Slaby 已提交
93
	unsigned long flags;
J
Jiri Slaby 已提交
94 95 96
	unsigned int i;

	switch (cmd) {
J
Jiri Slaby 已提交
97
	case PHN_SETREG:
J
Jiri Slaby 已提交
98 99 100 101 102 103 104
	case PHN_SET_REG:
		if (copy_from_user(&r, argp, sizeof(r)))
			return -EFAULT;

		if (r.reg > 7)
			return -EINVAL;

J
Jiri Slaby 已提交
105
		spin_lock_irqsave(&dev->regs_lock, flags);
J
Jiri Slaby 已提交
106
		if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) &&
J
Jiri Slaby 已提交
107
				phantom_status(dev, dev->status | PHB_RUNNING)){
J
Jiri Slaby 已提交
108
			spin_unlock_irqrestore(&dev->regs_lock, flags);
J
Jiri Slaby 已提交
109
			return -ENODEV;
J
Jiri Slaby 已提交
110
		}
J
Jiri Slaby 已提交
111 112

		pr_debug("phantom: writing %x to %u\n", r.value, r.reg);
J
Jiri Slaby 已提交
113 114 115 116 117 118 119 120

		/* preserve amp bit (don't allow to change it when in NOT_OH) */
		if (r.reg == PHN_CONTROL && (dev->status & PHB_NOT_OH)) {
			r.value &= ~PHN_CTL_AMP;
			r.value |= dev->ctl_reg & PHN_CTL_AMP;
			dev->ctl_reg = r.value;
		}

J
Jiri Slaby 已提交
121
		iowrite32(r.value, dev->iaddr + r.reg);
122
		ioread32(dev->iaddr); /* PCI posting */
J
Jiri Slaby 已提交
123 124 125

		if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ))
			phantom_status(dev, dev->status & ~PHB_RUNNING);
J
Jiri Slaby 已提交
126
		spin_unlock_irqrestore(&dev->regs_lock, flags);
J
Jiri Slaby 已提交
127
		break;
J
Jiri Slaby 已提交
128
	case PHN_SETREGS:
J
Jiri Slaby 已提交
129 130 131 132 133
	case PHN_SET_REGS:
		if (copy_from_user(&rs, argp, sizeof(rs)))
			return -EFAULT;

		pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
J
Jiri Slaby 已提交
134 135 136 137 138 139 140 141 142 143 144
		spin_lock_irqsave(&dev->regs_lock, flags);
		if (dev->status & PHB_NOT_OH)
			memcpy(&dev->oregs, &rs, sizeof(rs));
		else {
			u32 m = min(rs.count, 8U);
			for (i = 0; i < m; i++)
				if (rs.mask & BIT(i))
					iowrite32(rs.values[i], dev->oaddr + i);
			ioread32(dev->iaddr); /* PCI posting */
		}
		spin_unlock_irqrestore(&dev->regs_lock, flags);
J
Jiri Slaby 已提交
145
		break;
J
Jiri Slaby 已提交
146
	case PHN_GETREG:
J
Jiri Slaby 已提交
147 148 149 150 151 152 153 154 155 156 157 158
	case PHN_GET_REG:
		if (copy_from_user(&r, argp, sizeof(r)))
			return -EFAULT;

		if (r.reg > 7)
			return -EINVAL;

		r.value = ioread32(dev->iaddr + r.reg);

		if (copy_to_user(argp, &r, sizeof(r)))
			return -EFAULT;
		break;
J
Jiri Slaby 已提交
159
	case PHN_GETREGS:
J
Jiri Slaby 已提交
160 161 162
	case PHN_GET_REGS: {
		u32 m;

J
Jiri Slaby 已提交
163 164 165
		if (copy_from_user(&rs, argp, sizeof(rs)))
			return -EFAULT;

J
Jiri Slaby 已提交
166 167
		m = min(rs.count, 8U);

J
Jiri Slaby 已提交
168
		pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
J
Jiri Slaby 已提交
169 170 171
		spin_lock_irqsave(&dev->regs_lock, flags);
		for (i = 0; i < m; i++)
			if (rs.mask & BIT(i))
J
Jiri Slaby 已提交
172
				rs.values[i] = ioread32(dev->iaddr + i);
J
Jiri Slaby 已提交
173
		atomic_set(&dev->counter, 0);
J
Jiri Slaby 已提交
174
		spin_unlock_irqrestore(&dev->regs_lock, flags);
J
Jiri Slaby 已提交
175 176 177 178

		if (copy_to_user(argp, &rs, sizeof(rs)))
			return -EFAULT;
		break;
J
Jiri Slaby 已提交
179 180 181 182 183 184 185 186 187 188 189
	} case PHN_NOT_OH:
		spin_lock_irqsave(&dev->regs_lock, flags);
		if (dev->status & PHB_RUNNING) {
			printk(KERN_ERR "phantom: you need to set NOT_OH "
					"before you start the device!\n");
			spin_unlock_irqrestore(&dev->regs_lock, flags);
			return -EINVAL;
		}
		dev->status |= PHB_NOT_OH;
		spin_unlock_irqrestore(&dev->regs_lock, flags);
		break;
J
Jiri Slaby 已提交
190 191 192 193 194 195 196
	default:
		return -ENOTTY;
	}

	return 0;
}

J
Jiri Slaby 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210
#ifdef CONFIG_COMPAT
static long phantom_compat_ioctl(struct file *filp, unsigned int cmd,
		unsigned long arg)
{
	if (_IOC_NR(cmd) <= 3 && _IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
		cmd &= ~(_IOC_SIZEMASK << _IOC_SIZESHIFT);
		cmd |= sizeof(void *) << _IOC_SIZESHIFT;
	}
	return phantom_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
}
#else
#define phantom_compat_ioctl NULL
#endif

J
Jiri Slaby 已提交
211 212 213 214 215
static int phantom_open(struct inode *inode, struct file *file)
{
	struct phantom_device *dev = container_of(inode->i_cdev,
			struct phantom_device, cdev);

J
Jonathan Corbet 已提交
216
	lock_kernel();
J
Jiri Slaby 已提交
217 218
	nonseekable_open(inode, file);

J
Jonathan Corbet 已提交
219 220
	if (mutex_lock_interruptible(&dev->open_lock)) {
		unlock_kernel();
J
Jiri Slaby 已提交
221
		return -ERESTARTSYS;
J
Jonathan Corbet 已提交
222
	}
J
Jiri Slaby 已提交
223 224 225

	if (dev->opened) {
		mutex_unlock(&dev->open_lock);
J
Jonathan Corbet 已提交
226
		unlock_kernel();
J
Jiri Slaby 已提交
227 228 229
		return -EINVAL;
	}

J
Jiri Slaby 已提交
230 231
	WARN_ON(dev->status & PHB_NOT_OH);

J
Jiri Slaby 已提交
232 233
	file->private_data = dev;

J
Jiri Slaby 已提交
234
	atomic_set(&dev->counter, 0);
J
Jiri Slaby 已提交
235 236
	dev->opened++;
	mutex_unlock(&dev->open_lock);
J
Jonathan Corbet 已提交
237
	unlock_kernel();
J
Jiri Slaby 已提交
238 239 240 241 242 243 244 245 246 247 248
	return 0;
}

static int phantom_release(struct inode *inode, struct file *file)
{
	struct phantom_device *dev = file->private_data;

	mutex_lock(&dev->open_lock);

	dev->opened = 0;
	phantom_status(dev, dev->status & ~PHB_RUNNING);
J
Jiri Slaby 已提交
249
	dev->status &= ~PHB_NOT_OH;
J
Jiri Slaby 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262

	mutex_unlock(&dev->open_lock);

	return 0;
}

static unsigned int phantom_poll(struct file *file, poll_table *wait)
{
	struct phantom_device *dev = file->private_data;
	unsigned int mask = 0;

	pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
	poll_wait(file, &dev->wait, wait);
J
Jiri Slaby 已提交
263 264 265 266

	if (!(dev->status & PHB_RUNNING))
		mask = POLLERR;
	else if (atomic_read(&dev->counter))
J
Jiri Slaby 已提交
267
		mask = POLLIN | POLLRDNORM;
J
Jiri Slaby 已提交
268

J
Jiri Slaby 已提交
269 270 271 272 273 274 275 276
	pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));

	return mask;
}

static struct file_operations phantom_file_ops = {
	.open = phantom_open,
	.release = phantom_release,
J
Jiri Slaby 已提交
277
	.unlocked_ioctl = phantom_ioctl,
J
Jiri Slaby 已提交
278
	.compat_ioctl = phantom_compat_ioctl,
J
Jiri Slaby 已提交
279 280 281 282 283 284
	.poll = phantom_poll,
};

static irqreturn_t phantom_isr(int irq, void *data)
{
	struct phantom_device *dev = data;
J
Jiri Slaby 已提交
285 286
	unsigned int i;
	u32 ctl;
J
Jiri Slaby 已提交
287

J
Jiri Slaby 已提交
288 289 290 291
	spin_lock(&dev->regs_lock);
	ctl = ioread32(dev->iaddr + PHN_CONTROL);
	if (!(ctl & PHN_CTL_IRQ)) {
		spin_unlock(&dev->regs_lock);
J
Jiri Slaby 已提交
292
		return IRQ_NONE;
J
Jiri Slaby 已提交
293
	}
J
Jiri Slaby 已提交
294 295 296

	iowrite32(0, dev->iaddr);
	iowrite32(0xc0, dev->iaddr);
J
Jiri Slaby 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310

	if (dev->status & PHB_NOT_OH) {
		struct phm_regs *r = &dev->oregs;
		u32 m = min(r->count, 8U);

		for (i = 0; i < m; i++)
			if (r->mask & BIT(i))
				iowrite32(r->values[i], dev->oaddr + i);

		dev->ctl_reg ^= PHN_CTL_AMP;
		iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
	}
	spin_unlock(&dev->regs_lock);

311
	ioread32(dev->iaddr); /* PCI posting */
J
Jiri Slaby 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

	atomic_inc(&dev->counter);
	wake_up_interruptible(&dev->wait);

	return IRQ_HANDLED;
}

/*
 * Init and deinit driver
 */

static unsigned int __devinit phantom_get_free(void)
{
	unsigned int i;

	for (i = 0; i < PHANTOM_MAX_MINORS; i++)
		if (phantom_devices[i] == 0)
			break;

	return i;
}

static int __devinit phantom_probe(struct pci_dev *pdev,
	const struct pci_device_id *pci_id)
{
	struct phantom_device *pht;
	unsigned int minor;
	int retval;

	retval = pci_enable_device(pdev);
	if (retval)
		goto err;

	minor = phantom_get_free();
	if (minor == PHANTOM_MAX_MINORS) {
		dev_err(&pdev->dev, "too many devices found!\n");
		retval = -EIO;
		goto err_dis;
	}

	phantom_devices[minor] = 1;

	retval = pci_request_regions(pdev, "phantom");
	if (retval)
		goto err_null;

	retval = -ENOMEM;
	pht = kzalloc(sizeof(*pht), GFP_KERNEL);
	if (pht == NULL) {
		dev_err(&pdev->dev, "unable to allocate device\n");
		goto err_reg;
	}

	pht->caddr = pci_iomap(pdev, 0, 0);
	if (pht->caddr == NULL) {
		dev_err(&pdev->dev, "can't remap conf space\n");
		goto err_fr;
	}
	pht->iaddr = pci_iomap(pdev, 2, 0);
	if (pht->iaddr == NULL) {
		dev_err(&pdev->dev, "can't remap input space\n");
		goto err_unmc;
	}
	pht->oaddr = pci_iomap(pdev, 3, 0);
	if (pht->oaddr == NULL) {
		dev_err(&pdev->dev, "can't remap output space\n");
		goto err_unmi;
	}

	mutex_init(&pht->open_lock);
J
Jiri Slaby 已提交
382
	spin_lock_init(&pht->regs_lock);
J
Jiri Slaby 已提交
383 384 385 386 387
	init_waitqueue_head(&pht->wait);
	cdev_init(&pht->cdev, &phantom_file_ops);
	pht->cdev.owner = THIS_MODULE;

	iowrite32(0, pht->caddr + PHN_IRQCTL);
388
	ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
J
Jiri Slaby 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401
	retval = request_irq(pdev->irq, phantom_isr,
			IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
	if (retval) {
		dev_err(&pdev->dev, "can't establish ISR\n");
		goto err_unmo;
	}

	retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
	if (retval) {
		dev_err(&pdev->dev, "chardev registration failed\n");
		goto err_irq;
	}

402 403 404
	if (IS_ERR(device_create(phantom_class, &pdev->dev,
				 MKDEV(phantom_major, minor), NULL,
				 "phantom%u", minor)))
J
Jiri Slaby 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
		dev_err(&pdev->dev, "can't create device\n");

	pci_set_drvdata(pdev, pht);

	return 0;
err_irq:
	free_irq(pdev->irq, pht);
err_unmo:
	pci_iounmap(pdev, pht->oaddr);
err_unmi:
	pci_iounmap(pdev, pht->iaddr);
err_unmc:
	pci_iounmap(pdev, pht->caddr);
err_fr:
	kfree(pht);
err_reg:
	pci_release_regions(pdev);
err_null:
	phantom_devices[minor] = 0;
err_dis:
	pci_disable_device(pdev);
err:
	return retval;
}

static void __devexit phantom_remove(struct pci_dev *pdev)
{
	struct phantom_device *pht = pci_get_drvdata(pdev);
	unsigned int minor = MINOR(pht->cdev.dev);

	device_destroy(phantom_class, MKDEV(phantom_major, minor));

	cdev_del(&pht->cdev);

	iowrite32(0, pht->caddr + PHN_IRQCTL);
440
	ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
J
Jiri Slaby 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	free_irq(pdev->irq, pht);

	pci_iounmap(pdev, pht->oaddr);
	pci_iounmap(pdev, pht->iaddr);
	pci_iounmap(pdev, pht->caddr);

	kfree(pht);

	pci_release_regions(pdev);

	phantom_devices[minor] = 0;

	pci_disable_device(pdev);
}

#ifdef CONFIG_PM
static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
{
	struct phantom_device *dev = pci_get_drvdata(pdev);

	iowrite32(0, dev->caddr + PHN_IRQCTL);
462
	ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
J
Jiri Slaby 已提交
463

464 465
	synchronize_irq(pdev->irq);

J
Jiri Slaby 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
	return 0;
}

static int phantom_resume(struct pci_dev *pdev)
{
	struct phantom_device *dev = pci_get_drvdata(pdev);

	iowrite32(0, dev->caddr + PHN_IRQCTL);

	return 0;
}
#else
#define phantom_suspend	NULL
#define phantom_resume	NULL
#endif

static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
J
Jiri Slaby 已提交
483 484 485
	{ .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
	  .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
	  .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
J
Jiri Slaby 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
	{ 0, }
};
MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);

static struct pci_driver phantom_pci_driver = {
	.name = "phantom",
	.id_table = phantom_pci_tbl,
	.probe = phantom_probe,
	.remove = __devexit_p(phantom_remove),
	.suspend = phantom_suspend,
	.resume = phantom_resume
};

static ssize_t phantom_show_version(struct class *cls, char *buf)
{
	return sprintf(buf, PHANTOM_VERSION "\n");
}

static CLASS_ATTR(version, 0444, phantom_show_version, NULL);

static int __init phantom_init(void)
{
	int retval;
	dev_t dev;

	phantom_class = class_create(THIS_MODULE, "phantom");
	if (IS_ERR(phantom_class)) {
		retval = PTR_ERR(phantom_class);
		printk(KERN_ERR "phantom: can't register phantom class\n");
		goto err;
	}
	retval = class_create_file(phantom_class, &class_attr_version);
	if (retval) {
		printk(KERN_ERR "phantom: can't create sysfs version file\n");
		goto err_class;
	}

	retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
	if (retval) {
		printk(KERN_ERR "phantom: can't register character device\n");
		goto err_attr;
	}
	phantom_major = MAJOR(dev);

	retval = pci_register_driver(&phantom_pci_driver);
	if (retval) {
		printk(KERN_ERR "phantom: can't register pci driver\n");
		goto err_unchr;
	}

	printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
			"init OK\n");

	return 0;
err_unchr:
	unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
err_attr:
	class_remove_file(phantom_class, &class_attr_version);
err_class:
	class_destroy(phantom_class);
err:
	return retval;
}

static void __exit phantom_exit(void)
{
	pci_unregister_driver(&phantom_pci_driver);

	unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);

	class_remove_file(phantom_class, &class_attr_version);
	class_destroy(phantom_class);

	pr_debug("phantom: module successfully removed\n");
}

module_init(phantom_init);
module_exit(phantom_exit);

MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
J
Jiri Slaby 已提交
566
MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
J
Jiri Slaby 已提交
567 568
MODULE_LICENSE("GPL");
MODULE_VERSION(PHANTOM_VERSION);