phantom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 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. atomic_set(&dev->counter, 0);
  141. spin_unlock_irqrestore(&dev->regs_lock, flags);
  142. if (copy_to_user(argp, &rs, sizeof(rs)))
  143. return -EFAULT;
  144. break;
  145. } case PHN_NOT_OH:
  146. spin_lock_irqsave(&dev->regs_lock, flags);
  147. if (dev->status & PHB_RUNNING) {
  148. printk(KERN_ERR "phantom: you need to set NOT_OH "
  149. "before you start the device!\n");
  150. spin_unlock_irqrestore(&dev->regs_lock, flags);
  151. return -EINVAL;
  152. }
  153. dev->status |= PHB_NOT_OH;
  154. spin_unlock_irqrestore(&dev->regs_lock, flags);
  155. break;
  156. default:
  157. return -ENOTTY;
  158. }
  159. return 0;
  160. }
  161. #ifdef CONFIG_COMPAT
  162. static long phantom_compat_ioctl(struct file *filp, unsigned int cmd,
  163. unsigned long arg)
  164. {
  165. if (_IOC_NR(cmd) <= 3 && _IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
  166. cmd &= ~(_IOC_SIZEMASK << _IOC_SIZESHIFT);
  167. cmd |= sizeof(void *) << _IOC_SIZESHIFT;
  168. }
  169. return phantom_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  170. }
  171. #else
  172. #define phantom_compat_ioctl NULL
  173. #endif
  174. static int phantom_open(struct inode *inode, struct file *file)
  175. {
  176. struct phantom_device *dev = container_of(inode->i_cdev,
  177. struct phantom_device, cdev);
  178. mutex_lock(&phantom_mutex);
  179. nonseekable_open(inode, file);
  180. if (mutex_lock_interruptible(&dev->open_lock)) {
  181. mutex_unlock(&phantom_mutex);
  182. return -ERESTARTSYS;
  183. }
  184. if (dev->opened) {
  185. mutex_unlock(&dev->open_lock);
  186. mutex_unlock(&phantom_mutex);
  187. return -EINVAL;
  188. }
  189. WARN_ON(dev->status & PHB_NOT_OH);
  190. file->private_data = dev;
  191. atomic_set(&dev->counter, 0);
  192. dev->opened++;
  193. mutex_unlock(&dev->open_lock);
  194. mutex_unlock(&phantom_mutex);
  195. return 0;
  196. }
  197. static int phantom_release(struct inode *inode, struct file *file)
  198. {
  199. struct phantom_device *dev = file->private_data;
  200. mutex_lock(&dev->open_lock);
  201. dev->opened = 0;
  202. phantom_status(dev, dev->status & ~PHB_RUNNING);
  203. dev->status &= ~PHB_NOT_OH;
  204. mutex_unlock(&dev->open_lock);
  205. return 0;
  206. }
  207. static __poll_t phantom_poll(struct file *file, poll_table *wait)
  208. {
  209. struct phantom_device *dev = file->private_data;
  210. __poll_t mask = 0;
  211. pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
  212. poll_wait(file, &dev->wait, wait);
  213. if (!(dev->status & PHB_RUNNING))
  214. mask = EPOLLERR;
  215. else if (atomic_read(&dev->counter))
  216. mask = EPOLLIN | EPOLLRDNORM;
  217. pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
  218. return mask;
  219. }
  220. static const struct file_operations phantom_file_ops = {
  221. .open = phantom_open,
  222. .release = phantom_release,
  223. .unlocked_ioctl = phantom_ioctl,
  224. .compat_ioctl = phantom_compat_ioctl,
  225. .poll = phantom_poll,
  226. .llseek = no_llseek,
  227. };
  228. static irqreturn_t phantom_isr(int irq, void *data)
  229. {
  230. struct phantom_device *dev = data;
  231. unsigned int i;
  232. u32 ctl;
  233. spin_lock(&dev->regs_lock);
  234. ctl = ioread32(dev->iaddr + PHN_CONTROL);
  235. if (!(ctl & PHN_CTL_IRQ)) {
  236. spin_unlock(&dev->regs_lock);
  237. return IRQ_NONE;
  238. }
  239. iowrite32(0, dev->iaddr);
  240. iowrite32(0xc0, dev->iaddr);
  241. if (dev->status & PHB_NOT_OH) {
  242. struct phm_regs *r = &dev->oregs;
  243. u32 m = min(r->count, 8U);
  244. for (i = 0; i < m; i++)
  245. if (r->mask & BIT(i))
  246. iowrite32(r->values[i], dev->oaddr + i);
  247. dev->ctl_reg ^= PHN_CTL_AMP;
  248. iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
  249. }
  250. spin_unlock(&dev->regs_lock);
  251. ioread32(dev->iaddr); /* PCI posting */
  252. atomic_inc(&dev->counter);
  253. wake_up_interruptible(&dev->wait);
  254. return IRQ_HANDLED;
  255. }
  256. /*
  257. * Init and deinit driver
  258. */
  259. static unsigned int phantom_get_free(void)
  260. {
  261. unsigned int i;
  262. for (i = 0; i < PHANTOM_MAX_MINORS; i++)
  263. if (phantom_devices[i] == 0)
  264. break;
  265. return i;
  266. }
  267. static int phantom_probe(struct pci_dev *pdev,
  268. const struct pci_device_id *pci_id)
  269. {
  270. struct phantom_device *pht;
  271. unsigned int minor;
  272. int retval;
  273. retval = pci_enable_device(pdev);
  274. if (retval) {
  275. dev_err(&pdev->dev, "pci_enable_device failed!\n");
  276. goto err;
  277. }
  278. minor = phantom_get_free();
  279. if (minor == PHANTOM_MAX_MINORS) {
  280. dev_err(&pdev->dev, "too many devices found!\n");
  281. retval = -EIO;
  282. goto err_dis;
  283. }
  284. phantom_devices[minor] = 1;
  285. retval = pci_request_regions(pdev, "phantom");
  286. if (retval) {
  287. dev_err(&pdev->dev, "pci_request_regions failed!\n");
  288. goto err_null;
  289. }
  290. retval = -ENOMEM;
  291. pht = kzalloc(sizeof(*pht), GFP_KERNEL);
  292. if (pht == NULL) {
  293. dev_err(&pdev->dev, "unable to allocate device\n");
  294. goto err_reg;
  295. }
  296. pht->caddr = pci_iomap(pdev, 0, 0);
  297. if (pht->caddr == NULL) {
  298. dev_err(&pdev->dev, "can't remap conf space\n");
  299. goto err_fr;
  300. }
  301. pht->iaddr = pci_iomap(pdev, 2, 0);
  302. if (pht->iaddr == NULL) {
  303. dev_err(&pdev->dev, "can't remap input space\n");
  304. goto err_unmc;
  305. }
  306. pht->oaddr = pci_iomap(pdev, 3, 0);
  307. if (pht->oaddr == NULL) {
  308. dev_err(&pdev->dev, "can't remap output space\n");
  309. goto err_unmi;
  310. }
  311. mutex_init(&pht->open_lock);
  312. spin_lock_init(&pht->regs_lock);
  313. init_waitqueue_head(&pht->wait);
  314. cdev_init(&pht->cdev, &phantom_file_ops);
  315. pht->cdev.owner = THIS_MODULE;
  316. iowrite32(0, pht->caddr + PHN_IRQCTL);
  317. ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
  318. retval = request_irq(pdev->irq, phantom_isr,
  319. IRQF_SHARED, "phantom", pht);
  320. if (retval) {
  321. dev_err(&pdev->dev, "can't establish ISR\n");
  322. goto err_unmo;
  323. }
  324. retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
  325. if (retval) {
  326. dev_err(&pdev->dev, "chardev registration failed\n");
  327. goto err_irq;
  328. }
  329. if (IS_ERR(device_create(phantom_class, &pdev->dev,
  330. MKDEV(phantom_major, minor), NULL,
  331. "phantom%u", minor)))
  332. dev_err(&pdev->dev, "can't create device\n");
  333. pci_set_drvdata(pdev, pht);
  334. return 0;
  335. err_irq:
  336. free_irq(pdev->irq, pht);
  337. err_unmo:
  338. pci_iounmap(pdev, pht->oaddr);
  339. err_unmi:
  340. pci_iounmap(pdev, pht->iaddr);
  341. err_unmc:
  342. pci_iounmap(pdev, pht->caddr);
  343. err_fr:
  344. kfree(pht);
  345. err_reg:
  346. pci_release_regions(pdev);
  347. err_null:
  348. phantom_devices[minor] = 0;
  349. err_dis:
  350. pci_disable_device(pdev);
  351. err:
  352. return retval;
  353. }
  354. static void phantom_remove(struct pci_dev *pdev)
  355. {
  356. struct phantom_device *pht = pci_get_drvdata(pdev);
  357. unsigned int minor = MINOR(pht->cdev.dev);
  358. device_destroy(phantom_class, MKDEV(phantom_major, minor));
  359. cdev_del(&pht->cdev);
  360. iowrite32(0, pht->caddr + PHN_IRQCTL);
  361. ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
  362. free_irq(pdev->irq, pht);
  363. pci_iounmap(pdev, pht->oaddr);
  364. pci_iounmap(pdev, pht->iaddr);
  365. pci_iounmap(pdev, pht->caddr);
  366. kfree(pht);
  367. pci_release_regions(pdev);
  368. phantom_devices[minor] = 0;
  369. pci_disable_device(pdev);
  370. }
  371. static int __maybe_unused phantom_suspend(struct device *dev_d)
  372. {
  373. struct phantom_device *dev = dev_get_drvdata(dev_d);
  374. iowrite32(0, dev->caddr + PHN_IRQCTL);
  375. ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
  376. synchronize_irq(to_pci_dev(dev_d)->irq);
  377. return 0;
  378. }
  379. static int __maybe_unused phantom_resume(struct device *dev_d)
  380. {
  381. struct phantom_device *dev = dev_get_drvdata(dev_d);
  382. iowrite32(0, dev->caddr + PHN_IRQCTL);
  383. return 0;
  384. }
  385. static struct pci_device_id phantom_pci_tbl[] = {
  386. { .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
  387. .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
  388. .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
  389. { 0, }
  390. };
  391. MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
  392. static SIMPLE_DEV_PM_OPS(phantom_pm_ops, phantom_suspend, phantom_resume);
  393. static struct pci_driver phantom_pci_driver = {
  394. .name = "phantom",
  395. .id_table = phantom_pci_tbl,
  396. .probe = phantom_probe,
  397. .remove = phantom_remove,
  398. .driver.pm = &phantom_pm_ops,
  399. };
  400. static CLASS_ATTR_STRING(version, 0444, PHANTOM_VERSION);
  401. static int __init phantom_init(void)
  402. {
  403. int retval;
  404. dev_t dev;
  405. phantom_class = class_create(THIS_MODULE, "phantom");
  406. if (IS_ERR(phantom_class)) {
  407. retval = PTR_ERR(phantom_class);
  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_destroy(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_destroy(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);