phantom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2005-2007 Jiri Slaby <jirislaby@gmail.com>
  4. *
  5. * You need a userspace library to cooperate with this driver. It (and other
  6. * info) may be obtained here:
  7. * http://www.fi.muni.cz/~xslaby/phantom.html
  8. * or alternatively, you might use OpenHaptics provided by Sensable.
  9. */
  10. #include <linux/compat.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/pci.h>
  15. #include <linux/fs.h>
  16. #include <linux/poll.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/cdev.h>
  19. #include <linux/slab.h>
  20. #include <linux/phantom.h>
  21. #include <linux/sched.h>
  22. #include <linux/mutex.h>
  23. #include <linux/atomic.h>
  24. #include <asm/io.h>
  25. #define PHANTOM_VERSION "n0.9.8"
  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 DEFINE_MUTEX(phantom_mutex);
  31. static int phantom_major;
  32. static const struct class phantom_class = {
  33. .name = "phantom",
  34. };
  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. mutex_lock(&phantom_mutex);
  181. nonseekable_open(inode, file);
  182. if (mutex_lock_interruptible(&dev->open_lock)) {
  183. mutex_unlock(&phantom_mutex);
  184. return -ERESTARTSYS;
  185. }
  186. if (dev->opened) {
  187. mutex_unlock(&dev->open_lock);
  188. mutex_unlock(&phantom_mutex);
  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. mutex_unlock(&phantom_mutex);
  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 __poll_t phantom_poll(struct file *file, poll_table *wait)
  210. {
  211. struct phantom_device *dev = file->private_data;
  212. __poll_t 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 = EPOLLERR;
  217. else if (atomic_read(&dev->counter))
  218. mask = EPOLLIN | EPOLLRDNORM;
  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 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 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. dev_err(&pdev->dev, "pci_enable_device failed!\n");
  277. goto err;
  278. }
  279. minor = phantom_get_free();
  280. if (minor == PHANTOM_MAX_MINORS) {
  281. dev_err(&pdev->dev, "too many devices found!\n");
  282. retval = -EIO;
  283. goto err_dis;
  284. }
  285. phantom_devices[minor] = 1;
  286. retval = pci_request_regions(pdev, "phantom");
  287. if (retval) {
  288. dev_err(&pdev->dev, "pci_request_regions failed!\n");
  289. goto err_null;
  290. }
  291. retval = -ENOMEM;
  292. pht = kzalloc_obj(*pht);
  293. if (pht == NULL) {
  294. dev_err(&pdev->dev, "unable to allocate device\n");
  295. goto err_reg;
  296. }
  297. pht->caddr = pci_iomap(pdev, 0, 0);
  298. if (pht->caddr == NULL) {
  299. dev_err(&pdev->dev, "can't remap conf space\n");
  300. goto err_fr;
  301. }
  302. pht->iaddr = pci_iomap(pdev, 2, 0);
  303. if (pht->iaddr == NULL) {
  304. dev_err(&pdev->dev, "can't remap input space\n");
  305. goto err_unmc;
  306. }
  307. pht->oaddr = pci_iomap(pdev, 3, 0);
  308. if (pht->oaddr == NULL) {
  309. dev_err(&pdev->dev, "can't remap output space\n");
  310. goto err_unmi;
  311. }
  312. mutex_init(&pht->open_lock);
  313. spin_lock_init(&pht->regs_lock);
  314. init_waitqueue_head(&pht->wait);
  315. cdev_init(&pht->cdev, &phantom_file_ops);
  316. pht->cdev.owner = THIS_MODULE;
  317. iowrite32(0, pht->caddr + PHN_IRQCTL);
  318. ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
  319. retval = request_irq(pdev->irq, phantom_isr,
  320. IRQF_SHARED, "phantom", pht);
  321. if (retval) {
  322. dev_err(&pdev->dev, "can't establish ISR\n");
  323. goto err_unmo;
  324. }
  325. retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
  326. if (retval) {
  327. dev_err(&pdev->dev, "chardev registration failed\n");
  328. goto err_irq;
  329. }
  330. if (IS_ERR(device_create(&phantom_class, &pdev->dev,
  331. MKDEV(phantom_major, minor), NULL,
  332. "phantom%u", minor)))
  333. dev_err(&pdev->dev, "can't create device\n");
  334. pci_set_drvdata(pdev, pht);
  335. return 0;
  336. err_irq:
  337. free_irq(pdev->irq, pht);
  338. err_unmo:
  339. pci_iounmap(pdev, pht->oaddr);
  340. err_unmi:
  341. pci_iounmap(pdev, pht->iaddr);
  342. err_unmc:
  343. pci_iounmap(pdev, pht->caddr);
  344. err_fr:
  345. kfree(pht);
  346. err_reg:
  347. pci_release_regions(pdev);
  348. err_null:
  349. phantom_devices[minor] = 0;
  350. err_dis:
  351. pci_disable_device(pdev);
  352. err:
  353. return retval;
  354. }
  355. static void phantom_remove(struct pci_dev *pdev)
  356. {
  357. struct phantom_device *pht = pci_get_drvdata(pdev);
  358. unsigned int minor = MINOR(pht->cdev.dev);
  359. device_destroy(&phantom_class, MKDEV(phantom_major, minor));
  360. cdev_del(&pht->cdev);
  361. iowrite32(0, pht->caddr + PHN_IRQCTL);
  362. ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
  363. free_irq(pdev->irq, pht);
  364. pci_iounmap(pdev, pht->oaddr);
  365. pci_iounmap(pdev, pht->iaddr);
  366. pci_iounmap(pdev, pht->caddr);
  367. kfree(pht);
  368. pci_release_regions(pdev);
  369. phantom_devices[minor] = 0;
  370. pci_disable_device(pdev);
  371. }
  372. static int __maybe_unused phantom_suspend(struct device *dev_d)
  373. {
  374. struct phantom_device *dev = dev_get_drvdata(dev_d);
  375. iowrite32(0, dev->caddr + PHN_IRQCTL);
  376. ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
  377. synchronize_irq(to_pci_dev(dev_d)->irq);
  378. return 0;
  379. }
  380. static int __maybe_unused phantom_resume(struct device *dev_d)
  381. {
  382. struct phantom_device *dev = dev_get_drvdata(dev_d);
  383. iowrite32(0, dev->caddr + PHN_IRQCTL);
  384. return 0;
  385. }
  386. static struct pci_device_id phantom_pci_tbl[] = {
  387. { .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
  388. .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
  389. .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
  390. { 0, }
  391. };
  392. MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
  393. static SIMPLE_DEV_PM_OPS(phantom_pm_ops, phantom_suspend, phantom_resume);
  394. static struct pci_driver phantom_pci_driver = {
  395. .name = "phantom",
  396. .id_table = phantom_pci_tbl,
  397. .probe = phantom_probe,
  398. .remove = phantom_remove,
  399. .driver.pm = &phantom_pm_ops,
  400. };
  401. static CLASS_ATTR_STRING(version, 0444, PHANTOM_VERSION);
  402. static int __init phantom_init(void)
  403. {
  404. int retval;
  405. dev_t dev;
  406. retval = class_register(&phantom_class);
  407. if (retval) {
  408. printk(KERN_ERR "phantom: can't register phantom class\n");
  409. goto err;
  410. }
  411. retval = class_create_file(&phantom_class, &class_attr_version.attr);
  412. if (retval) {
  413. printk(KERN_ERR "phantom: can't create sysfs version file\n");
  414. goto err_class;
  415. }
  416. retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
  417. if (retval) {
  418. printk(KERN_ERR "phantom: can't register character device\n");
  419. goto err_attr;
  420. }
  421. phantom_major = MAJOR(dev);
  422. retval = pci_register_driver(&phantom_pci_driver);
  423. if (retval) {
  424. printk(KERN_ERR "phantom: can't register pci driver\n");
  425. goto err_unchr;
  426. }
  427. printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
  428. "init OK\n");
  429. return 0;
  430. err_unchr:
  431. unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
  432. err_attr:
  433. class_remove_file(&phantom_class, &class_attr_version.attr);
  434. err_class:
  435. class_unregister(&phantom_class);
  436. err:
  437. return retval;
  438. }
  439. static void __exit phantom_exit(void)
  440. {
  441. pci_unregister_driver(&phantom_pci_driver);
  442. unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
  443. class_remove_file(&phantom_class, &class_attr_version.attr);
  444. class_unregister(&phantom_class);
  445. pr_debug("phantom: module successfully removed\n");
  446. }
  447. module_init(phantom_init);
  448. module_exit(phantom_exit);
  449. MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
  450. MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
  451. MODULE_LICENSE("GPL");
  452. MODULE_VERSION(PHANTOM_VERSION);