phantom.c 13 KB

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