ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /**
  2. *
  3. * @file ioctl.c
  4. * @author Guillermo Marcus
  5. * @date 2009-04-05
  6. * @brief Contains the functions handling the different ioctl calls.
  7. *
  8. */
  9. #include <linux/version.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/pci.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/fs.h>
  19. #include <linux/cdev.h>
  20. #include <linux/sysfs.h>
  21. #include <asm/atomic.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/list.h>
  25. #include <asm/scatterlist.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/stat.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/wait.h>
  30. #include <linux/sched.h>
  31. #include "config.h" /* Configuration for the driver */
  32. #include "compat.h" /* Compatibility functions/definitions */
  33. #include "pciDriver.h" /* External interface for the driver */
  34. #include "common.h" /* Internal definitions for all parts */
  35. #include "kmem.h" /* Internal definitions for kernel memory */
  36. #include "umem.h" /* Internal definitions for user space memory */
  37. #include "ioctl.h" /* Internal definitions for the ioctl part */
  38. /** Declares a variable of the given type with the given name and copies it from userspace */
  39. #define READ_FROM_USER(type, name) \
  40. type name; \
  41. if ((ret = copy_from_user(&name, (type*)arg, sizeof(name))) != 0) \
  42. return -EFAULT;
  43. /** Writes back the given variable with the given type to userspace */
  44. #define WRITE_TO_USER(type, name) \
  45. if ((ret = copy_to_user((type*)arg, &name, sizeof(name))) != 0) \
  46. return -EFAULT;
  47. /**
  48. *
  49. * Sets the mmap mode for following mmap() calls.
  50. *
  51. * @param arg Not a pointer, but either PCIDRIVER_MMAP_PCI or PCIDRIVER_MMAP_KMEM
  52. *
  53. */
  54. static int ioctl_mmap_mode(pcidriver_privdata_t *privdata, unsigned long arg)
  55. {
  56. if ((arg != PCIDRIVER_MMAP_PCI) && (arg != PCIDRIVER_MMAP_KMEM))
  57. return -EINVAL;
  58. /* change the mode */
  59. privdata->mmap_mode = arg;
  60. return 0;
  61. }
  62. /**
  63. *
  64. * Sets the mmap area (BAR) for following mmap() calls.
  65. *
  66. */
  67. static int ioctl_mmap_area(pcidriver_privdata_t *privdata, unsigned long arg)
  68. {
  69. /* validate input */
  70. if ((arg < PCIDRIVER_BAR0) || (arg > PCIDRIVER_BAR5))
  71. return -EINVAL;
  72. /* change the PCI area to mmap */
  73. privdata->mmap_area = arg;
  74. return 0;
  75. }
  76. /**
  77. *
  78. * Reads/writes a byte/word/dword of the device's PCI config.
  79. *
  80. * @see pcidriver_pci_read
  81. * @see pcidriver_pci_write
  82. *
  83. */
  84. static int ioctl_pci_config_read_write(pcidriver_privdata_t *privdata, unsigned int cmd, unsigned long arg)
  85. {
  86. int ret;
  87. READ_FROM_USER(pci_cfg_cmd, pci_cmd);
  88. if (cmd == PCIDRIVER_IOC_PCI_CFG_RD) {
  89. switch (pci_cmd.size) {
  90. case PCIDRIVER_PCI_CFG_SZ_BYTE:
  91. ret = pci_read_config_byte( privdata->pdev, pci_cmd.addr, &(pci_cmd.val.byte) );
  92. break;
  93. case PCIDRIVER_PCI_CFG_SZ_WORD:
  94. ret = pci_read_config_word( privdata->pdev, pci_cmd.addr, &(pci_cmd.val.word) );
  95. break;
  96. case PCIDRIVER_PCI_CFG_SZ_DWORD:
  97. ret = pci_read_config_dword( privdata->pdev, pci_cmd.addr, &(pci_cmd.val.dword) );
  98. break;
  99. default:
  100. return -EINVAL; /* Wrong size setting */
  101. }
  102. } else {
  103. switch (pci_cmd.size) {
  104. case PCIDRIVER_PCI_CFG_SZ_BYTE:
  105. ret = pci_write_config_byte( privdata->pdev, pci_cmd.addr, pci_cmd.val.byte );
  106. break;
  107. case PCIDRIVER_PCI_CFG_SZ_WORD:
  108. ret = pci_write_config_word( privdata->pdev, pci_cmd.addr, pci_cmd.val.word );
  109. break;
  110. case PCIDRIVER_PCI_CFG_SZ_DWORD:
  111. ret = pci_write_config_dword( privdata->pdev, pci_cmd.addr, pci_cmd.val.dword );
  112. break;
  113. default:
  114. return -EINVAL; /* Wrong size setting */
  115. break;
  116. }
  117. }
  118. WRITE_TO_USER(pci_cfg_cmd, pci_cmd);
  119. return 0;
  120. }
  121. /**
  122. *
  123. * Gets the PCI information for the device.
  124. *
  125. * @see pcidriver_pci_info
  126. *
  127. */
  128. static int ioctl_pci_info(pcidriver_privdata_t *privdata, unsigned long arg)
  129. {
  130. int ret;
  131. int bar;
  132. READ_FROM_USER(pcilib_board_info_t, pci_info);
  133. pci_info.vendor_id = privdata->pdev->vendor;
  134. pci_info.device_id = privdata->pdev->device;
  135. pci_info.bus = privdata->pdev->bus->number;
  136. pci_info.slot = PCI_SLOT(privdata->pdev->devfn);
  137. pci_info.devfn = privdata->pdev->devfn;
  138. pci_info.func = PCI_FUNC(privdata->pdev->devfn);
  139. if ((ret = pci_read_config_byte(privdata->pdev, PCI_INTERRUPT_PIN, &(pci_info.interrupt_pin))) != 0)
  140. return ret;
  141. if ((ret = pci_read_config_byte(privdata->pdev, PCI_INTERRUPT_LINE, &(pci_info.interrupt_line))) != 0)
  142. return ret;
  143. for (bar = 0; bar < 6; bar++) {
  144. pci_info.bar_start[bar] = pci_resource_start(privdata->pdev, bar);
  145. pci_info.bar_length[bar] = pci_resource_len(privdata->pdev, bar);
  146. pci_info.bar_flags[bar] = pci_resource_flags(privdata->pdev, bar);
  147. }
  148. WRITE_TO_USER(pcilib_board_info_t, pci_info);
  149. return 0;
  150. }
  151. /**
  152. *
  153. * Allocates kernel memory.
  154. *
  155. * @see pcidriver_kmem_alloc
  156. *
  157. */
  158. static int ioctl_kmem_alloc(pcidriver_privdata_t *privdata, unsigned long arg)
  159. {
  160. int ret;
  161. READ_FROM_USER(kmem_handle_t, khandle);
  162. if ((ret = pcidriver_kmem_alloc(privdata, &khandle)) != 0)
  163. return ret;
  164. WRITE_TO_USER(kmem_handle_t, khandle);
  165. return 0;
  166. }
  167. /**
  168. *
  169. * Frees kernel memory.
  170. *
  171. * @see pcidriver_kmem_free
  172. *
  173. */
  174. static int ioctl_kmem_free(pcidriver_privdata_t *privdata, unsigned long arg)
  175. {
  176. int ret;
  177. READ_FROM_USER(kmem_handle_t, khandle);
  178. if ((ret = pcidriver_kmem_free(privdata, &khandle)) != 0)
  179. return ret;
  180. return 0;
  181. }
  182. /**
  183. *
  184. * Syncs kernel memory.
  185. *
  186. * @see pcidriver_kmem_sync
  187. *
  188. */
  189. static int ioctl_kmem_sync(pcidriver_privdata_t *privdata, unsigned long arg)
  190. {
  191. int ret;
  192. READ_FROM_USER(kmem_sync_t, ksync);
  193. if ((ret = pcidriver_kmem_sync(privdata, &ksync)) != 0)
  194. return ret;
  195. WRITE_TO_USER(kmem_sync_t, ksync);
  196. return 0;
  197. }
  198. /*
  199. *
  200. * Maps the given scatter/gather list from memory to PCI bus addresses.
  201. *
  202. * @see pcidriver_umem_sgmap
  203. *
  204. */
  205. static int ioctl_umem_sgmap(pcidriver_privdata_t *privdata, unsigned long arg)
  206. {
  207. int ret;
  208. READ_FROM_USER(umem_handle_t, uhandle);
  209. if ((ret = pcidriver_umem_sgmap(privdata, &uhandle)) != 0)
  210. return ret;
  211. WRITE_TO_USER(umem_handle_t, uhandle);
  212. return 0;
  213. }
  214. /**
  215. *
  216. * Unmaps the given scatter/gather list.
  217. *
  218. * @see pcidriver_umem_sgunmap
  219. *
  220. */
  221. static int ioctl_umem_sgunmap(pcidriver_privdata_t *privdata, unsigned long arg)
  222. {
  223. int ret;
  224. pcidriver_umem_entry_t *umem_entry;
  225. READ_FROM_USER(umem_handle_t, uhandle);
  226. /* Find the associated umem_entry for this buffer,
  227. * return -EINVAL if the specified handle id is invalid */
  228. if ((umem_entry = pcidriver_umem_find_entry_id(privdata, uhandle.handle_id)) == NULL)
  229. return -EINVAL;
  230. if ((ret = pcidriver_umem_sgunmap(privdata, umem_entry)) != 0)
  231. return ret;
  232. return 0;
  233. }
  234. /**
  235. *
  236. * Copies the scatter/gather list from kernelspace to userspace.
  237. *
  238. * @see pcidriver_umem_sgget
  239. *
  240. */
  241. static int ioctl_umem_sgget(pcidriver_privdata_t *privdata, unsigned long arg)
  242. {
  243. int ret;
  244. READ_FROM_USER(umem_sglist_t, usglist);
  245. /* The umem_sglist_t has a pointer to the scatter/gather list itself which
  246. * needs to be copied separately. The number of elements is stored in ->nents.
  247. * As the list can get very big, we need to use vmalloc. */
  248. if ((usglist.sg = vmalloc(usglist.nents * sizeof(umem_sgentry_t))) == NULL)
  249. return -ENOMEM;
  250. /* copy array to kernel structure */
  251. ret = copy_from_user(usglist.sg, ((umem_sglist_t *)arg)->sg, (usglist.nents)*sizeof(umem_sgentry_t));
  252. if (ret) return -EFAULT;
  253. if ((ret = pcidriver_umem_sgget(privdata, &usglist)) != 0)
  254. return ret;
  255. /* write data to user space */
  256. ret = copy_to_user(((umem_sglist_t *)arg)->sg, usglist.sg, (usglist.nents)*sizeof(umem_sgentry_t));
  257. if (ret) return -EFAULT;
  258. /* free array memory */
  259. vfree(usglist.sg);
  260. /* restore sg pointer to vma address in user space before copying */
  261. usglist.sg = ((umem_sglist_t *)arg)->sg;
  262. WRITE_TO_USER(umem_sglist_t, usglist);
  263. return 0;
  264. }
  265. /**
  266. *
  267. * Syncs user memory.
  268. *
  269. * @see pcidriver_umem_sync
  270. *
  271. */
  272. static int ioctl_umem_sync(pcidriver_privdata_t *privdata, unsigned long arg)
  273. {
  274. int ret;
  275. READ_FROM_USER(umem_handle_t, uhandle);
  276. return pcidriver_umem_sync( privdata, &uhandle );
  277. }
  278. /**
  279. *
  280. * Waits for an interrupt
  281. *
  282. * @param arg Not a pointer, but the irq source to wait for (unsigned int)
  283. *
  284. */
  285. static int ioctl_wait_interrupt(pcidriver_privdata_t *privdata, unsigned long arg)
  286. {
  287. #ifdef ENABLE_IRQ
  288. int ret;
  289. unsigned long timeout;
  290. unsigned int irq_source;
  291. unsigned long temp = 0;
  292. READ_FROM_USER(interrupt_wait_t, irq_handle);
  293. irq_source = irq_handle.source;
  294. if (irq_source >= PCIDRIVER_INT_MAXSOURCES)
  295. return -EFAULT; /* User tried to overrun the IRQ_SOURCES array */
  296. timeout = jiffies + (irq_handle.timeout * HZ / 1000000);
  297. /* Thanks to Joern for the correction and tips! */
  298. /* done this way to avoid wrong behaviour (endless loop) of the compiler in AMD platforms */
  299. do {
  300. /* We wait here with an interruptible timeout. This will be interrupted
  301. * by int.c:check_acknowledge_channel() as soon as in interrupt for
  302. * the specified source arrives. */
  303. wait_event_interruptible_timeout( (privdata->irq_queues[irq_source]), (atomic_read(&(privdata->irq_outstanding[irq_source])) > 0), (10*HZ/1000) );
  304. if (atomic_add_negative( -1, &(privdata->irq_outstanding[irq_source])) )
  305. atomic_inc( &(privdata->irq_outstanding[irq_source]) );
  306. else
  307. temp = 1;
  308. } while ((!temp)&&(jiffies < timeout));
  309. if ((temp)&&(irq_handle.count)) {
  310. while (!atomic_add_negative( -1, &(privdata->irq_outstanding[irq_source]))) temp++;
  311. atomic_inc( &(privdata->irq_outstanding[irq_source]) );
  312. }
  313. irq_handle.count = temp;
  314. WRITE_TO_USER(interrupt_wait_t, irq_handle);
  315. return 0;
  316. #else
  317. mod_info("Asked to wait for interrupt but interrupts are not enabled in the driver\n");
  318. return -EFAULT;
  319. #endif
  320. }
  321. /**
  322. *
  323. * Clears the interrupt wait queue.
  324. *
  325. * @param arg Not a pointer, but the irq source (unsigned int)
  326. * @returns -EFAULT if the user specified an irq source out of range
  327. *
  328. */
  329. static int ioctl_clear_ioq(pcidriver_privdata_t *privdata, unsigned long arg)
  330. {
  331. #ifdef ENABLE_IRQ
  332. unsigned int irq_source;
  333. if (arg >= PCIDRIVER_INT_MAXSOURCES)
  334. return -EFAULT;
  335. irq_source = arg;
  336. atomic_set(&(privdata->irq_outstanding[irq_source]), 0);
  337. return 0;
  338. #else
  339. mod_info("Asked to wait for interrupt but interrupts are not enabled in the driver\n");
  340. return -EFAULT;
  341. #endif
  342. }
  343. /**
  344. *
  345. * This function handles all ioctl file operations.
  346. * Generally, the data of the ioctl is copied from userspace to kernelspace, a separate
  347. * function is called to handle the ioctl itself, then the data is copied back to userspace.
  348. *
  349. * @returns -EFAULT when an invalid memory pointer is passed
  350. *
  351. */
  352. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
  353. int pcidriver_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  354. #else
  355. long pcidriver_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  356. #endif
  357. {
  358. pcidriver_privdata_t *privdata = filp->private_data;
  359. /* Select the appropiate command */
  360. switch (cmd) {
  361. case PCIDRIVER_IOC_MMAP_MODE:
  362. return ioctl_mmap_mode(privdata, arg);
  363. case PCIDRIVER_IOC_MMAP_AREA:
  364. return ioctl_mmap_area(privdata, arg);
  365. case PCIDRIVER_IOC_PCI_CFG_RD:
  366. case PCIDRIVER_IOC_PCI_CFG_WR:
  367. return ioctl_pci_config_read_write(privdata, cmd, arg);
  368. case PCIDRIVER_IOC_PCI_INFO:
  369. return ioctl_pci_info(privdata, arg);
  370. case PCIDRIVER_IOC_KMEM_ALLOC:
  371. return ioctl_kmem_alloc(privdata, arg);
  372. case PCIDRIVER_IOC_KMEM_FREE:
  373. return ioctl_kmem_free(privdata, arg);
  374. case PCIDRIVER_IOC_KMEM_SYNC:
  375. return ioctl_kmem_sync(privdata, arg);
  376. case PCIDRIVER_IOC_UMEM_SGMAP:
  377. return ioctl_umem_sgmap(privdata, arg);
  378. case PCIDRIVER_IOC_UMEM_SGUNMAP:
  379. return ioctl_umem_sgunmap(privdata, arg);
  380. case PCIDRIVER_IOC_UMEM_SGGET:
  381. return ioctl_umem_sgget(privdata, arg);
  382. case PCIDRIVER_IOC_UMEM_SYNC:
  383. return ioctl_umem_sync(privdata, arg);
  384. case PCIDRIVER_IOC_WAITI:
  385. return ioctl_wait_interrupt(privdata, arg);
  386. case PCIDRIVER_IOC_CLEAR_IOQ:
  387. return ioctl_clear_ioq(privdata, arg);
  388. default:
  389. return -EINVAL;
  390. }
  391. }