phantom.c 13 KB

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