phantom.c 13 KB

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