phantom.c 13 KB

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