phantom.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 an 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. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/pci.h>
  17. #include <linux/fs.h>
  18. #include <linux/poll.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/cdev.h>
  21. #include <linux/phantom.h>
  22. #include <asm/atomic.h>
  23. #include <asm/io.h>
  24. #define PHANTOM_VERSION "n0.9.5"
  25. #define PHANTOM_MAX_MINORS 8
  26. #define PHN_IRQCTL 0x4c /* irq control in caddr space */
  27. #define PHB_RUNNING 1
  28. static struct class *phantom_class;
  29. static int phantom_major;
  30. struct phantom_device {
  31. unsigned int opened;
  32. void __iomem *caddr;
  33. u32 __iomem *iaddr;
  34. u32 __iomem *oaddr;
  35. unsigned long status;
  36. atomic_t counter;
  37. wait_queue_head_t wait;
  38. struct cdev cdev;
  39. struct mutex open_lock;
  40. };
  41. static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
  42. static int phantom_status(struct phantom_device *dev, unsigned long newstat)
  43. {
  44. pr_debug("phantom_status %lx %lx\n", dev->status, newstat);
  45. if (!(dev->status & PHB_RUNNING) && (newstat & PHB_RUNNING)) {
  46. atomic_set(&dev->counter, 0);
  47. iowrite32(PHN_CTL_IRQ, dev->iaddr + PHN_CONTROL);
  48. iowrite32(0x43, dev->caddr + PHN_IRQCTL);
  49. } else if ((dev->status & PHB_RUNNING) && !(newstat & PHB_RUNNING))
  50. iowrite32(0, dev->caddr + PHN_IRQCTL);
  51. dev->status = newstat;
  52. return 0;
  53. }
  54. /*
  55. * File ops
  56. */
  57. static int phantom_ioctl(struct inode *inode, struct file *file, u_int cmd,
  58. u_long arg)
  59. {
  60. struct phantom_device *dev = file->private_data;
  61. struct phm_regs rs;
  62. struct phm_reg r;
  63. void __user *argp = (void __user *)arg;
  64. unsigned int i;
  65. if (_IOC_TYPE(cmd) != PH_IOC_MAGIC ||
  66. _IOC_NR(cmd) > PH_IOC_MAXNR)
  67. return -ENOTTY;
  68. switch (cmd) {
  69. case PHN_SET_REG:
  70. if (copy_from_user(&r, argp, sizeof(r)))
  71. return -EFAULT;
  72. if (r.reg > 7)
  73. return -EINVAL;
  74. if (r.reg == PHN_CONTROL && (r.value & PHN_CTL_IRQ) &&
  75. phantom_status(dev, dev->status | PHB_RUNNING))
  76. return -ENODEV;
  77. pr_debug("phantom: writing %x to %u\n", r.value, r.reg);
  78. iowrite32(r.value, dev->iaddr + r.reg);
  79. if (r.reg == PHN_CONTROL && !(r.value & PHN_CTL_IRQ))
  80. phantom_status(dev, dev->status & ~PHB_RUNNING);
  81. break;
  82. case PHN_SET_REGS:
  83. if (copy_from_user(&rs, argp, sizeof(rs)))
  84. return -EFAULT;
  85. pr_debug("phantom: SRS %u regs %x\n", rs.count, rs.mask);
  86. for (i = 0; i < min(rs.count, 8U); i++)
  87. if ((1 << i) & rs.mask)
  88. iowrite32(rs.values[i], dev->oaddr + i);
  89. break;
  90. case PHN_GET_REG:
  91. if (copy_from_user(&r, argp, sizeof(r)))
  92. return -EFAULT;
  93. if (r.reg > 7)
  94. return -EINVAL;
  95. r.value = ioread32(dev->iaddr + r.reg);
  96. if (copy_to_user(argp, &r, sizeof(r)))
  97. return -EFAULT;
  98. break;
  99. case PHN_GET_REGS:
  100. if (copy_from_user(&rs, argp, sizeof(rs)))
  101. return -EFAULT;
  102. pr_debug("phantom: GRS %u regs %x\n", rs.count, rs.mask);
  103. for (i = 0; i < min(rs.count, 8U); i++)
  104. if ((1 << i) & rs.mask)
  105. rs.values[i] = ioread32(dev->iaddr + i);
  106. if (copy_to_user(argp, &rs, sizeof(rs)))
  107. return -EFAULT;
  108. break;
  109. default:
  110. return -ENOTTY;
  111. }
  112. return 0;
  113. }
  114. static int phantom_open(struct inode *inode, struct file *file)
  115. {
  116. struct phantom_device *dev = container_of(inode->i_cdev,
  117. struct phantom_device, cdev);
  118. nonseekable_open(inode, file);
  119. if (mutex_lock_interruptible(&dev->open_lock))
  120. return -ERESTARTSYS;
  121. if (dev->opened) {
  122. mutex_unlock(&dev->open_lock);
  123. return -EINVAL;
  124. }
  125. file->private_data = dev;
  126. dev->opened++;
  127. mutex_unlock(&dev->open_lock);
  128. return 0;
  129. }
  130. static int phantom_release(struct inode *inode, struct file *file)
  131. {
  132. struct phantom_device *dev = file->private_data;
  133. mutex_lock(&dev->open_lock);
  134. dev->opened = 0;
  135. phantom_status(dev, dev->status & ~PHB_RUNNING);
  136. mutex_unlock(&dev->open_lock);
  137. return 0;
  138. }
  139. static unsigned int phantom_poll(struct file *file, poll_table *wait)
  140. {
  141. struct phantom_device *dev = file->private_data;
  142. unsigned int mask = 0;
  143. pr_debug("phantom_poll: %d\n", atomic_read(&dev->counter));
  144. poll_wait(file, &dev->wait, wait);
  145. if (atomic_read(&dev->counter)) {
  146. mask = POLLIN | POLLRDNORM;
  147. atomic_dec(&dev->counter);
  148. } else if ((dev->status & PHB_RUNNING) == 0)
  149. mask = POLLIN | POLLRDNORM | POLLERR;
  150. pr_debug("phantom_poll end: %x/%d\n", mask, atomic_read(&dev->counter));
  151. return mask;
  152. }
  153. static struct file_operations phantom_file_ops = {
  154. .open = phantom_open,
  155. .release = phantom_release,
  156. .ioctl = phantom_ioctl,
  157. .poll = phantom_poll,
  158. };
  159. static irqreturn_t phantom_isr(int irq, void *data)
  160. {
  161. struct phantom_device *dev = data;
  162. if (!(ioread32(dev->iaddr + PHN_CONTROL) & PHN_CTL_IRQ))
  163. return IRQ_NONE;
  164. iowrite32(0, dev->iaddr);
  165. iowrite32(0xc0, dev->iaddr);
  166. atomic_inc(&dev->counter);
  167. wake_up_interruptible(&dev->wait);
  168. return IRQ_HANDLED;
  169. }
  170. /*
  171. * Init and deinit driver
  172. */
  173. static unsigned int __devinit phantom_get_free(void)
  174. {
  175. unsigned int i;
  176. for (i = 0; i < PHANTOM_MAX_MINORS; i++)
  177. if (phantom_devices[i] == 0)
  178. break;
  179. return i;
  180. }
  181. static int __devinit phantom_probe(struct pci_dev *pdev,
  182. const struct pci_device_id *pci_id)
  183. {
  184. struct phantom_device *pht;
  185. unsigned int minor;
  186. int retval;
  187. retval = pci_enable_device(pdev);
  188. if (retval)
  189. goto err;
  190. minor = phantom_get_free();
  191. if (minor == PHANTOM_MAX_MINORS) {
  192. dev_err(&pdev->dev, "too many devices found!\n");
  193. retval = -EIO;
  194. goto err_dis;
  195. }
  196. phantom_devices[minor] = 1;
  197. retval = pci_request_regions(pdev, "phantom");
  198. if (retval)
  199. goto err_null;
  200. retval = -ENOMEM;
  201. pht = kzalloc(sizeof(*pht), GFP_KERNEL);
  202. if (pht == NULL) {
  203. dev_err(&pdev->dev, "unable to allocate device\n");
  204. goto err_reg;
  205. }
  206. pht->caddr = pci_iomap(pdev, 0, 0);
  207. if (pht->caddr == NULL) {
  208. dev_err(&pdev->dev, "can't remap conf space\n");
  209. goto err_fr;
  210. }
  211. pht->iaddr = pci_iomap(pdev, 2, 0);
  212. if (pht->iaddr == NULL) {
  213. dev_err(&pdev->dev, "can't remap input space\n");
  214. goto err_unmc;
  215. }
  216. pht->oaddr = pci_iomap(pdev, 3, 0);
  217. if (pht->oaddr == NULL) {
  218. dev_err(&pdev->dev, "can't remap output space\n");
  219. goto err_unmi;
  220. }
  221. mutex_init(&pht->open_lock);
  222. init_waitqueue_head(&pht->wait);
  223. cdev_init(&pht->cdev, &phantom_file_ops);
  224. pht->cdev.owner = THIS_MODULE;
  225. iowrite32(0, pht->caddr + PHN_IRQCTL);
  226. retval = request_irq(pdev->irq, phantom_isr,
  227. IRQF_SHARED | IRQF_DISABLED, "phantom", pht);
  228. if (retval) {
  229. dev_err(&pdev->dev, "can't establish ISR\n");
  230. goto err_unmo;
  231. }
  232. retval = cdev_add(&pht->cdev, MKDEV(phantom_major, minor), 1);
  233. if (retval) {
  234. dev_err(&pdev->dev, "chardev registration failed\n");
  235. goto err_irq;
  236. }
  237. if (IS_ERR(device_create(phantom_class, &pdev->dev, MKDEV(phantom_major,
  238. minor), "phantom%u", minor)))
  239. dev_err(&pdev->dev, "can't create device\n");
  240. pci_set_drvdata(pdev, pht);
  241. return 0;
  242. err_irq:
  243. free_irq(pdev->irq, pht);
  244. err_unmo:
  245. pci_iounmap(pdev, pht->oaddr);
  246. err_unmi:
  247. pci_iounmap(pdev, pht->iaddr);
  248. err_unmc:
  249. pci_iounmap(pdev, pht->caddr);
  250. err_fr:
  251. kfree(pht);
  252. err_reg:
  253. pci_release_regions(pdev);
  254. err_null:
  255. phantom_devices[minor] = 0;
  256. err_dis:
  257. pci_disable_device(pdev);
  258. err:
  259. return retval;
  260. }
  261. static void __devexit phantom_remove(struct pci_dev *pdev)
  262. {
  263. struct phantom_device *pht = pci_get_drvdata(pdev);
  264. unsigned int minor = MINOR(pht->cdev.dev);
  265. device_destroy(phantom_class, MKDEV(phantom_major, minor));
  266. cdev_del(&pht->cdev);
  267. iowrite32(0, pht->caddr + PHN_IRQCTL);
  268. free_irq(pdev->irq, pht);
  269. pci_iounmap(pdev, pht->oaddr);
  270. pci_iounmap(pdev, pht->iaddr);
  271. pci_iounmap(pdev, pht->caddr);
  272. kfree(pht);
  273. pci_release_regions(pdev);
  274. phantom_devices[minor] = 0;
  275. pci_disable_device(pdev);
  276. }
  277. #ifdef CONFIG_PM
  278. static int phantom_suspend(struct pci_dev *pdev, pm_message_t state)
  279. {
  280. struct phantom_device *dev = pci_get_drvdata(pdev);
  281. iowrite32(0, dev->caddr + PHN_IRQCTL);
  282. return 0;
  283. }
  284. static int phantom_resume(struct pci_dev *pdev)
  285. {
  286. struct phantom_device *dev = pci_get_drvdata(pdev);
  287. iowrite32(0, dev->caddr + PHN_IRQCTL);
  288. return 0;
  289. }
  290. #else
  291. #define phantom_suspend NULL
  292. #define phantom_resume NULL
  293. #endif
  294. static struct pci_device_id phantom_pci_tbl[] __devinitdata = {
  295. { PCI_DEVICE(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050),
  296. .class = PCI_CLASS_BRIDGE_OTHER << 8, .class_mask = 0xffff00 },
  297. { 0, }
  298. };
  299. MODULE_DEVICE_TABLE(pci, phantom_pci_tbl);
  300. static struct pci_driver phantom_pci_driver = {
  301. .name = "phantom",
  302. .id_table = phantom_pci_tbl,
  303. .probe = phantom_probe,
  304. .remove = __devexit_p(phantom_remove),
  305. .suspend = phantom_suspend,
  306. .resume = phantom_resume
  307. };
  308. static ssize_t phantom_show_version(struct class *cls, char *buf)
  309. {
  310. return sprintf(buf, PHANTOM_VERSION "\n");
  311. }
  312. static CLASS_ATTR(version, 0444, phantom_show_version, NULL);
  313. static int __init phantom_init(void)
  314. {
  315. int retval;
  316. dev_t dev;
  317. phantom_class = class_create(THIS_MODULE, "phantom");
  318. if (IS_ERR(phantom_class)) {
  319. retval = PTR_ERR(phantom_class);
  320. printk(KERN_ERR "phantom: can't register phantom class\n");
  321. goto err;
  322. }
  323. retval = class_create_file(phantom_class, &class_attr_version);
  324. if (retval) {
  325. printk(KERN_ERR "phantom: can't create sysfs version file\n");
  326. goto err_class;
  327. }
  328. retval = alloc_chrdev_region(&dev, 0, PHANTOM_MAX_MINORS, "phantom");
  329. if (retval) {
  330. printk(KERN_ERR "phantom: can't register character device\n");
  331. goto err_attr;
  332. }
  333. phantom_major = MAJOR(dev);
  334. retval = pci_register_driver(&phantom_pci_driver);
  335. if (retval) {
  336. printk(KERN_ERR "phantom: can't register pci driver\n");
  337. goto err_unchr;
  338. }
  339. printk(KERN_INFO "Phantom Linux Driver, version " PHANTOM_VERSION ", "
  340. "init OK\n");
  341. return 0;
  342. err_unchr:
  343. unregister_chrdev_region(dev, PHANTOM_MAX_MINORS);
  344. err_attr:
  345. class_remove_file(phantom_class, &class_attr_version);
  346. err_class:
  347. class_destroy(phantom_class);
  348. err:
  349. return retval;
  350. }
  351. static void __exit phantom_exit(void)
  352. {
  353. pci_unregister_driver(&phantom_pci_driver);
  354. unregister_chrdev_region(MKDEV(phantom_major, 0), PHANTOM_MAX_MINORS);
  355. class_remove_file(phantom_class, &class_attr_version);
  356. class_destroy(phantom_class);
  357. pr_debug("phantom: module successfully removed\n");
  358. }
  359. module_init(phantom_init);
  360. module_exit(phantom_exit);
  361. MODULE_AUTHOR("Jiri Slaby <jirislaby@gmail.com>");
  362. MODULE_DESCRIPTION("Sensable Phantom driver");
  363. MODULE_LICENSE("GPL");
  364. MODULE_VERSION(PHANTOM_VERSION);