PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/misc/phantom.c

https://github.com/ssvb/linux-n810
C | 571 lines | 440 code | 110 blank | 21 comment | 64 complexity | 1fa05055c752c8df1e016feaec1352b6 MD5 | raw file
  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. .llseek = no_llseek,
  231. };
  232. static irqreturn_t phantom_isr(int irq, void *data)
  233. {
  234. struct phantom_device *dev = data;
  235. unsigned int i;
  236. u32 ctl;
  237. spin_lock(&dev->regs_lock);
  238. ctl = ioread32(dev->iaddr + PHN_CONTROL);
  239. if (!(ctl & PHN_CTL_IRQ)) {
  240. spin_unlock(&dev->regs_lock);
  241. return IRQ_NONE;
  242. }
  243. iowrite32(0, dev->iaddr);
  244. iowrite32(0xc0, dev->iaddr);
  245. if (dev->status & PHB_NOT_OH) {
  246. struct phm_regs *r = &dev->oregs;
  247. u32 m = min(r->count, 8U);
  248. for (i = 0; i < m; i++)
  249. if (r->mask & BIT(i))
  250. iowrite32(r->values[i], dev->oaddr + i);
  251. dev->ctl_reg ^= PHN_CTL_AMP;
  252. iowrite32(dev->ctl_reg, dev->iaddr + PHN_CONTROL);
  253. }
  254. spin_unlock(&dev->regs_lock);
  255. ioread32(dev->iaddr); /* PCI posting */
  256. atomic_inc(&dev->counter);
  257. wake_up_interruptible(&dev->wait);
  258. return IRQ_HANDLED;
  259. }
  260. /*
  261. * Init and deinit driver
  262. */
  263. static unsigned int __devinit phantom_get_free(void)
  264. {
  265. unsigned int i;
  266. for (i = 0; i < PHANTOM_MAX_MINORS; i++)
  267. if (phantom_devices[i] == 0)
  268. break;
  269. return i;
  270. }
  271. static int __devinit phantom_probe(struct pci_dev *pdev,
  272. const struct pci_device_id *pci_id)
  273. {
  274. struct phantom_device *pht;
  275. unsigned int minor;
  276. int retval;
  277. retval = pci_enable_device(pdev);
  278. if (retval) {
  279. dev_err(&pdev->dev, "pci_enable_device failed!\n");
  280. goto err;
  281. }
  282. minor = phantom_get_free();
  283. if (minor == PHANTOM_MAX_MINORS) {
  284. dev_err(&pdev->dev, "too many devices found!\n");
  285. retval = -EIO;
  286. goto err_dis;
  287. }
  288. phantom_devices[minor] = 1;
  289. retval = pci_request_regions(pdev, "phantom");
  290. if (retval) {
  291. dev_err(&pdev->dev, "pci_request_regions failed!\n");
  292. goto err_null;
  293. }
  294. retval = -ENOMEM;
  295. pht = kzalloc(sizeof(*pht), GFP_KERNEL);
  296. if (pht == NULL) {
  297. dev_err(&pdev->dev, "unable to allocate device\n");
  298. goto err_reg;
  299. }
  300. pht->caddr = pci_iomap(pdev, 0, 0);
  301. if (pht->caddr == NULL) {
  302. dev_err(&pdev->dev, "can't remap conf space\n");
  303. goto err_fr;
  304. }
  305. pht->iaddr = pci_iomap(pdev, 2, 0);
  306. if (pht->iaddr == NULL) {
  307. dev_err(&pdev->dev, "can't remap input space\n");
  308. goto err_unmc;
  309. }
  310. pht->oaddr = pci_iomap(pdev, 3, 0);
  311. if (pht->oaddr == NULL) {
  312. dev_err(&pdev->dev, "can't remap output space\n");
  313. goto err_unmi;
  314. }
  315. mutex_init(&pht->open_lock);
  316. spin_lock_init(&pht->regs_lock);
  317. init_waitqueue_head(&pht->wait);
  318. cdev_init(&pht->cdev, &phantom_file_ops);
  319. pht->cdev.owner = THIS_MODULE;
  320. iowrite32(0, pht->caddr + PHN_IRQCTL);
  321. ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
  322. retval = request_irq(pdev->irq, phantom_isr,
  323. IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
  324. if (retval) {
  325. dev_err(&pdev->dev, "can't establish ISR\n");
  326. goto err_unmo;
  327. }
  328. retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
  329. if (retval) {
  330. dev_err(&pdev->dev, "chardev registration failed\n");
  331. goto err_irq;
  332. }
  333. if (IS_ERR(device_create(phantom_class, &pdev->dev,
  334. MKDEV(phantom_major, minor), NULL,
  335. "phantom%u", minor)))
  336. dev_err(&pdev->dev, "can't create device\n");
  337. pci_set_drvdata(pdev, pht);
  338. return 0;
  339. err_irq:
  340. free_irq(pdev->irq, pht);
  341. err_unmo:
  342. pci_iounmap(pdev, pht->oaddr);
  343. err_unmi:
  344. pci_iounmap(pdev, pht->iaddr);
  345. err_unmc:
  346. pci_iounmap(pdev, pht->caddr);
  347. err_fr:
  348. kfree(pht);
  349. err_reg:
  350. pci_release_regions(pdev);
  351. err_null:
  352. phantom_devices[minor] = 0;
  353. err_dis:
  354. pci_disable_device(pdev);
  355. err:
  356. return retval;
  357. }
  358. static void __devexit phantom_remove(struct pci_dev *pdev)
  359. {
  360. struct phantom_device *pht = pci_get_drvdata(pdev);
  361. unsigned int minor = MINOR(pht->cdev.dev);
  362. device_destroy(phantom_class, MKDEV(phantom_major, minor));
  363. cdev_del(&pht->cdev);
  364. iowrite32(0, pht->caddr + PHN_IRQCTL);
  365. ioread32(pht->caddr + PHN_IRQCTL); /* PCI posting */
  366. free_irq(pdev->irq, pht);
  367. pci_iounmap(pdev, pht->oaddr);
  368. pci_iounmap(pdev, pht->iaddr);
  369. pci_iounmap(pdev, pht->caddr);
  370. kfree(pht);
  371. pci_release_regions(pdev);
  372. phantom_devices[minor] = 0;
  373. pci_disable_device(pdev);
  374. }
  375. #ifdef CONFIG_PM
  376. static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
  377. {
  378. struct phantom_device *dev = pci_get_drvdata(pdev);
  379. iowrite32(0, dev->caddr + PHN_IRQCTL);
  380. ioread32(dev->caddr + PHN_IRQCTL); /* PCI posting */
  381. synchronize_irq(pdev->irq);
  382. return 0;
  383. }
  384. static int phantom_resume(struct pci_dev *pdev)
  385. {
  386. struct phantom_device *dev = pci_get_drvdata(pdev);
  387. iowrite32(0, dev->caddr + PHN_IRQCTL);
  388. return 0;
  389. }
  390. #else
  391. #define phantom_suspend NULL
  392. #define phantom_resume NULL
  393. #endif
  394. static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
  395. { .vendor = PCI_VENDOR_ID_PLX, .device = PCI_DEVICE_ID_PLX_9050,
  396. .subvendor = PCI_VENDOR_ID_PLX, .subdevice = PCI_DEVICE_ID_PLX_9050,
  397. .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
  398. { 0, }
  399. };
  400. MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
  401. static struct pci_driver phantom_pci_driver = {
  402. .name = "phantom",
  403. .id_table = phantom_pci_tbl,
  404. .probe = phantom_probe,
  405. .remove = __devexit_p(phantom_remove),
  406. .suspend = phantom_suspend,
  407. .resume = phantom_resume
  408. };
  409. static CLASS_ATTR_STRING(version, 0444, PHANTOM_VERSION);
  410. static int __init phantom_init(void)
  411. {
  412. int retval;
  413. dev_t dev;
  414. phantom_class = class_create(THIS_MODULE, "phantom");
  415. if (IS_ERR(phantom_class)) {
  416. retval = PTR_ERR(phantom_class);
  417. printk(KERN_ERR "phantom: can't register phantom class\n");
  418. goto err;
  419. }
  420. retval = class_create_file(phantom_class, &class_attr_version.attr);
  421. if (retval) {
  422. printk(KERN_ERR "phantom: can't create sysfs version file\n");
  423. goto err_class;
  424. }
  425. retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
  426. if (retval) {
  427. printk(KERN_ERR "phantom: can't register character device\n");
  428. goto err_attr;
  429. }
  430. phantom_major = MAJOR(dev);
  431. retval = pci_register_driver(&phantom_pci_driver);
  432. if (retval) {
  433. printk(KERN_ERR "phantom: can't register pci driver\n");
  434. goto err_unchr;
  435. }
  436. printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
  437. "init OK\n");
  438. return 0;
  439. err_unchr:
  440. unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
  441. err_attr:
  442. class_remove_file(phantom_class, &class_attr_version.attr);
  443. err_class:
  444. class_destroy(phantom_class);
  445. err:
  446. return retval;
  447. }
  448. static void __exit phantom_exit(void)
  449. {
  450. pci_unregister_driver(&phantom_pci_driver);
  451. unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
  452. class_remove_file(phantom_class, &class_attr_version.attr);
  453. class_destroy(phantom_class);
  454. pr_debug("phantom: module successfully removed\n");
  455. }
  456. module_init(phantom_init);
  457. module_exit(phantom_exit);
  458. MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
  459. MODULE_DESCRIPTION("Sensable Phantom driver (PCI devices)");
  460. MODULE_LICENSE("GPL");
  461. MODULE_VERSION(PHANTOM_VERSION);