drm_drv.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /**
  2. * \file drm_drv.c
  3. * Generic driver template
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. *
  8. * To use this template, you must at least define the following (samples
  9. * given for the MGA driver):
  10. *
  11. * \code
  12. * #define DRIVER_AUTHOR "VA Linux Systems, Inc."
  13. *
  14. * #define DRIVER_NAME "mga"
  15. * #define DRIVER_DESC "Matrox G200/G400"
  16. * #define DRIVER_DATE "20001127"
  17. *
  18. * #define drm_x mga_##x
  19. * \endcode
  20. */
  21. /*
  22. * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
  23. *
  24. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  25. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  26. * All Rights Reserved.
  27. *
  28. * Permission is hereby granted, free of charge, to any person obtaining a
  29. * copy of this software and associated documentation files (the "Software"),
  30. * to deal in the Software without restriction, including without limitation
  31. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  32. * and/or sell copies of the Software, and to permit persons to whom the
  33. * Software is furnished to do so, subject to the following conditions:
  34. *
  35. * The above copyright notice and this permission notice (including the next
  36. * paragraph) shall be included in all copies or substantial portions of the
  37. * Software.
  38. *
  39. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  40. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  41. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  42. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  43. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  44. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  45. * OTHER DEALINGS IN THE SOFTWARE.
  46. */
  47. #include <linux/debugfs.h>
  48. #include <linux/slab.h>
  49. #include <linux/export.h>
  50. #include "drmP.h"
  51. #include "drm_core.h"
  52. static int drm_version(struct drm_device *dev, void *data,
  53. struct drm_file *file_priv);
  54. #define DRM_IOCTL_DEF(ioctl, _func, _flags) \
  55. [DRM_IOCTL_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, .cmd_drv = 0}
  56. /** Ioctl table */
  57. static struct drm_ioctl_desc drm_ioctls[] = {
  58. DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, DRM_UNLOCKED),
  59. DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
  60. DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
  61. DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
  62. DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, DRM_UNLOCKED),
  63. DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, DRM_UNLOCKED),
  64. DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, DRM_UNLOCKED),
  65. DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED),
  66. DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
  67. DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  68. DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  69. DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  70. DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER),
  71. DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  72. DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_rmmap_ioctl, DRM_AUTH),
  73. DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  74. DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_getsareactx, DRM_AUTH),
  75. DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY),
  76. DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY),
  77. DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_addctx, DRM_AUTH|DRM_ROOT_ONLY),
  78. DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  79. DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_modctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  80. DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_getctx, DRM_AUTH),
  81. DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  82. DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  83. DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_resctx, DRM_AUTH),
  84. DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  85. DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  86. DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_lock, DRM_AUTH),
  87. DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_unlock, DRM_AUTH),
  88. DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
  89. DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  90. DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  91. DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_infobufs, DRM_AUTH),
  92. DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_mapbufs, DRM_AUTH),
  93. DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_freebufs, DRM_AUTH),
  94. /* The DRM_IOCTL_DMA ioctl should be defined by the driver. */
  95. DRM_IOCTL_DEF(DRM_IOCTL_DMA, NULL, DRM_AUTH),
  96. DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  97. #if __OS_HAS_AGP
  98. DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  99. DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  100. DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  101. DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
  102. DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  103. DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  104. DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  105. DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  106. #endif
  107. DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_sg_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  108. DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  109. DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, DRM_UNLOCKED),
  110. DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0),
  111. DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  112. DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED),
  113. DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED),
  114. DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED),
  115. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  116. DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_AUTH|DRM_UNLOCKED),
  117. DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_AUTH|DRM_UNLOCKED),
  118. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  119. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  120. DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  121. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  122. DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  123. DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  124. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_UNLOCKED),
  125. DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
  126. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  127. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  128. DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_mode_attachmode_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  129. DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_mode_detachmode_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  130. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_MASTER | DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  131. DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  132. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  133. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  134. DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  135. DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  136. DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  137. DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  138. DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  139. DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  140. DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
  141. DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED)
  142. };
  143. #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )
  144. /**
  145. * Take down the DRM device.
  146. *
  147. * \param dev DRM device structure.
  148. *
  149. * Frees every resource in \p dev.
  150. *
  151. * \sa drm_device
  152. */
  153. int drm_lastclose(struct drm_device * dev)
  154. {
  155. struct drm_vma_entry *vma, *vma_temp;
  156. int i;
  157. DRM_DEBUG("\n");
  158. if (dev->driver->lastclose)
  159. dev->driver->lastclose(dev);
  160. DRM_DEBUG("driver lastclose completed\n");
  161. if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
  162. drm_irq_uninstall(dev);
  163. mutex_lock(&dev->struct_mutex);
  164. /* Clear AGP information */
  165. if (drm_core_has_AGP(dev) && dev->agp &&
  166. !drm_core_check_feature(dev, DRIVER_MODESET)) {
  167. struct drm_agp_mem *entry, *tempe;
  168. /* Remove AGP resources, but leave dev->agp
  169. intact until drv_cleanup is called. */
  170. list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) {
  171. if (entry->bound)
  172. drm_unbind_agp(entry->memory);
  173. drm_free_agp(entry->memory, entry->pages);
  174. kfree(entry);
  175. }
  176. INIT_LIST_HEAD(&dev->agp->memory);
  177. if (dev->agp->acquired)
  178. drm_agp_release(dev);
  179. dev->agp->acquired = 0;
  180. dev->agp->enabled = 0;
  181. }
  182. if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
  183. !drm_core_check_feature(dev, DRIVER_MODESET)) {
  184. drm_sg_cleanup(dev->sg);
  185. dev->sg = NULL;
  186. }
  187. /* Clear vma list (only built for debugging) */
  188. list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
  189. list_del(&vma->head);
  190. kfree(vma);
  191. }
  192. if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE) && dev->queuelist) {
  193. for (i = 0; i < dev->queue_count; i++) {
  194. kfree(dev->queuelist[i]);
  195. dev->queuelist[i] = NULL;
  196. }
  197. kfree(dev->queuelist);
  198. dev->queuelist = NULL;
  199. }
  200. dev->queue_count = 0;
  201. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
  202. !drm_core_check_feature(dev, DRIVER_MODESET))
  203. drm_dma_takedown(dev);
  204. dev->dev_mapping = NULL;
  205. mutex_unlock(&dev->struct_mutex);
  206. DRM_DEBUG("lastclose completed\n");
  207. return 0;
  208. }
  209. /** File operations structure */
  210. static const struct file_operations drm_stub_fops = {
  211. .owner = THIS_MODULE,
  212. .open = drm_stub_open,
  213. .llseek = noop_llseek,
  214. };
  215. static int __init drm_core_init(void)
  216. {
  217. int ret = -ENOMEM;
  218. drm_global_init();
  219. idr_init(&drm_minors_idr);
  220. if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
  221. goto err_p1;
  222. drm_class = drm_sysfs_create(THIS_MODULE, "drm");
  223. if (IS_ERR(drm_class)) {
  224. printk(KERN_ERR "DRM: Error creating drm class.\n");
  225. ret = PTR_ERR(drm_class);
  226. goto err_p2;
  227. }
  228. drm_proc_root = proc_mkdir("dri", NULL);
  229. if (!drm_proc_root) {
  230. DRM_ERROR("Cannot create /proc/dri\n");
  231. ret = -1;
  232. goto err_p3;
  233. }
  234. drm_debugfs_root = debugfs_create_dir("dri", NULL);
  235. if (!drm_debugfs_root) {
  236. DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
  237. ret = -1;
  238. goto err_p3;
  239. }
  240. DRM_INFO("Initialized %s %d.%d.%d %s\n",
  241. CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
  242. return 0;
  243. err_p3:
  244. drm_sysfs_destroy();
  245. err_p2:
  246. unregister_chrdev(DRM_MAJOR, "drm");
  247. idr_destroy(&drm_minors_idr);
  248. err_p1:
  249. return ret;
  250. }
  251. static void __exit drm_core_exit(void)
  252. {
  253. remove_proc_entry("dri", NULL);
  254. debugfs_remove(drm_debugfs_root);
  255. drm_sysfs_destroy();
  256. unregister_chrdev(DRM_MAJOR, "drm");
  257. idr_remove_all(&drm_minors_idr);
  258. idr_destroy(&drm_minors_idr);
  259. }
  260. module_init(drm_core_init);
  261. module_exit(drm_core_exit);
  262. /**
  263. * Copy and IOCTL return string to user space
  264. */
  265. static int drm_copy_field(char *buf, size_t *buf_len, const char *value)
  266. {
  267. int len;
  268. /* don't overflow userbuf */
  269. len = strlen(value);
  270. if (len > *buf_len)
  271. len = *buf_len;
  272. /* let userspace know exact length of driver value (which could be
  273. * larger than the userspace-supplied buffer) */
  274. *buf_len = strlen(value);
  275. /* finally, try filling in the userbuf */
  276. if (len && buf)
  277. if (copy_to_user(buf, value, len))
  278. return -EFAULT;
  279. return 0;
  280. }
  281. /**
  282. * Get version information
  283. *
  284. * \param inode device inode.
  285. * \param filp file pointer.
  286. * \param cmd command.
  287. * \param arg user argument, pointing to a drm_version structure.
  288. * \return zero on success or negative number on failure.
  289. *
  290. * Fills in the version information in \p arg.
  291. */
  292. static int drm_version(struct drm_device *dev, void *data,
  293. struct drm_file *file_priv)
  294. {
  295. struct drm_version *version = data;
  296. int err;
  297. version->version_major = dev->driver->major;
  298. version->version_minor = dev->driver->minor;
  299. version->version_patchlevel = dev->driver->patchlevel;
  300. err = drm_copy_field(version->name, &version->name_len,
  301. dev->driver->name);
  302. if (!err)
  303. err = drm_copy_field(version->date, &version->date_len,
  304. dev->driver->date);
  305. if (!err)
  306. err = drm_copy_field(version->desc, &version->desc_len,
  307. dev->driver->desc);
  308. return err;
  309. }
  310. /**
  311. * Called whenever a process performs an ioctl on /dev/drm.
  312. *
  313. * \param inode device inode.
  314. * \param file_priv DRM file private.
  315. * \param cmd command.
  316. * \param arg user argument.
  317. * \return zero on success or negative number on failure.
  318. *
  319. * Looks up the ioctl function in the ::ioctls table, checking for root
  320. * previleges if so required, and dispatches to the respective function.
  321. */
  322. long drm_ioctl(struct file *filp,
  323. unsigned int cmd, unsigned long arg)
  324. {
  325. struct drm_file *file_priv = filp->private_data;
  326. struct drm_device *dev;
  327. struct drm_ioctl_desc *ioctl;
  328. drm_ioctl_t *func;
  329. unsigned int nr = DRM_IOCTL_NR(cmd);
  330. int retcode = -EINVAL;
  331. char stack_kdata[128];
  332. char *kdata = NULL;
  333. unsigned int usize, asize;
  334. dev = file_priv->minor->dev;
  335. if (drm_device_is_unplugged(dev))
  336. return -ENODEV;
  337. atomic_inc(&dev->ioctl_count);
  338. atomic_inc(&dev->counts[_DRM_STAT_IOCTLS]);
  339. ++file_priv->ioctl_count;
  340. DRM_DEBUG("pid=%d, cmd=0x%02x, nr=0x%02x, dev 0x%lx, auth=%d\n",
  341. task_pid_nr(current), cmd, nr,
  342. (long)old_encode_dev(file_priv->minor->device),
  343. file_priv->authenticated);
  344. if ((nr >= DRM_CORE_IOCTL_COUNT) &&
  345. ((nr < DRM_COMMAND_BASE) || (nr >= DRM_COMMAND_END)))
  346. goto err_i1;
  347. if ((nr >= DRM_COMMAND_BASE) && (nr < DRM_COMMAND_END) &&
  348. (nr < DRM_COMMAND_BASE + dev->driver->num_ioctls)) {
  349. u32 drv_size;
  350. ioctl = &dev->driver->ioctls[nr - DRM_COMMAND_BASE];
  351. drv_size = _IOC_SIZE(ioctl->cmd_drv);
  352. usize = asize = _IOC_SIZE(cmd);
  353. if (drv_size > asize)
  354. asize = drv_size;
  355. }
  356. else if ((nr >= DRM_COMMAND_END) || (nr < DRM_COMMAND_BASE)) {
  357. u32 drv_size;
  358. ioctl = &drm_ioctls[nr];
  359. drv_size = _IOC_SIZE(ioctl->cmd);
  360. usize = asize = _IOC_SIZE(cmd);
  361. if (drv_size > asize)
  362. asize = drv_size;
  363. cmd = ioctl->cmd;
  364. } else
  365. goto err_i1;
  366. /* Do not trust userspace, use our own definition */
  367. func = ioctl->func;
  368. /* is there a local override? */
  369. if ((nr == DRM_IOCTL_NR(DRM_IOCTL_DMA)) && dev->driver->dma_ioctl)
  370. func = dev->driver->dma_ioctl;
  371. if (!func) {
  372. DRM_DEBUG("no function\n");
  373. retcode = -EINVAL;
  374. } else if (((ioctl->flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)) ||
  375. ((ioctl->flags & DRM_AUTH) && !file_priv->authenticated) ||
  376. ((ioctl->flags & DRM_MASTER) && !file_priv->is_master) ||
  377. (!(ioctl->flags & DRM_CONTROL_ALLOW) && (file_priv->minor->type == DRM_MINOR_CONTROL))) {
  378. retcode = -EACCES;
  379. } else {
  380. if (cmd & (IOC_IN | IOC_OUT)) {
  381. if (asize <= sizeof(stack_kdata)) {
  382. kdata = stack_kdata;
  383. } else {
  384. kdata = kmalloc(asize, GFP_KERNEL);
  385. if (!kdata) {
  386. retcode = -ENOMEM;
  387. goto err_i1;
  388. }
  389. }
  390. if (asize > usize)
  391. memset(kdata + usize, 0, asize - usize);
  392. }
  393. if (cmd & IOC_IN) {
  394. if (copy_from_user(kdata, (void __user *)arg,
  395. usize) != 0) {
  396. retcode = -EFAULT;
  397. goto err_i1;
  398. }
  399. } else
  400. memset(kdata, 0, usize);
  401. if (ioctl->flags & DRM_UNLOCKED)
  402. retcode = func(dev, kdata, file_priv);
  403. else {
  404. mutex_lock(&drm_global_mutex);
  405. retcode = func(dev, kdata, file_priv);
  406. mutex_unlock(&drm_global_mutex);
  407. }
  408. if (cmd & IOC_OUT) {
  409. if (copy_to_user((void __user *)arg, kdata,
  410. usize) != 0)
  411. retcode = -EFAULT;
  412. }
  413. }
  414. err_i1:
  415. if (kdata != stack_kdata)
  416. kfree(kdata);
  417. atomic_dec(&dev->ioctl_count);
  418. if (retcode)
  419. DRM_DEBUG("ret = %x\n", retcode);
  420. return retcode;
  421. }
  422. EXPORT_SYMBOL(drm_ioctl);
  423. struct drm_local_map *drm_getsarea(struct drm_device *dev)
  424. {
  425. struct drm_map_list *entry;
  426. list_for_each_entry(entry, &dev->maplist, head) {
  427. if (entry->map && entry->map->type == _DRM_SHM &&
  428. (entry->map->flags & _DRM_CONTAINS_LOCK)) {
  429. return entry->map;
  430. }
  431. }
  432. return NULL;
  433. }
  434. EXPORT_SYMBOL(drm_getsarea);