phantom.c 13 KB

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