drm_ioctl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * \file drm_ioctl.c
  3. * IOCTL processing for DRM
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Fri Jan 8 09:01:26 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include "drmP.h"
  35. #include "drm_core.h"
  36. #include "linux/pci.h"
  37. #include "linux/export.h"
  38. /**
  39. * Get the bus id.
  40. *
  41. * \param inode device inode.
  42. * \param file_priv DRM file private.
  43. * \param cmd command.
  44. * \param arg user argument, pointing to a drm_unique structure.
  45. * \return zero on success or a negative number on failure.
  46. *
  47. * Copies the bus id from drm_device::unique into user space.
  48. */
  49. int drm_getunique(struct drm_device *dev, void *data,
  50. struct drm_file *file_priv)
  51. {
  52. struct drm_unique *u = data;
  53. struct drm_master *master = file_priv->master;
  54. if (u->unique_len >= master->unique_len) {
  55. if (copy_to_user(u->unique, master->unique, master->unique_len))
  56. return -EFAULT;
  57. }
  58. u->unique_len = master->unique_len;
  59. return 0;
  60. }
  61. static void
  62. drm_unset_busid(struct drm_device *dev,
  63. struct drm_master *master)
  64. {
  65. kfree(dev->devname);
  66. dev->devname = NULL;
  67. kfree(master->unique);
  68. master->unique = NULL;
  69. master->unique_len = 0;
  70. master->unique_size = 0;
  71. }
  72. /**
  73. * Set the bus id.
  74. *
  75. * \param inode device inode.
  76. * \param file_priv DRM file private.
  77. * \param cmd command.
  78. * \param arg user argument, pointing to a drm_unique structure.
  79. * \return zero on success or a negative number on failure.
  80. *
  81. * Copies the bus id from userspace into drm_device::unique, and verifies that
  82. * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
  83. * in interface version 1.1 and will return EBUSY when setversion has requested
  84. * version 1.1 or greater.
  85. */
  86. int drm_setunique(struct drm_device *dev, void *data,
  87. struct drm_file *file_priv)
  88. {
  89. struct drm_unique *u = data;
  90. struct drm_master *master = file_priv->master;
  91. int ret;
  92. if (master->unique_len || master->unique)
  93. return -EBUSY;
  94. if (!u->unique_len || u->unique_len > 1024)
  95. return -EINVAL;
  96. if (!dev->driver->bus->set_unique)
  97. return -EINVAL;
  98. ret = dev->driver->bus->set_unique(dev, master, u);
  99. if (ret)
  100. goto err;
  101. return 0;
  102. err:
  103. drm_unset_busid(dev, master);
  104. return ret;
  105. }
  106. static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
  107. {
  108. struct drm_master *master = file_priv->master;
  109. int ret;
  110. if (master->unique != NULL)
  111. drm_unset_busid(dev, master);
  112. ret = dev->driver->bus->set_busid(dev, master);
  113. if (ret)
  114. goto err;
  115. return 0;
  116. err:
  117. drm_unset_busid(dev, master);
  118. return ret;
  119. }
  120. /**
  121. * Get a mapping information.
  122. *
  123. * \param inode device inode.
  124. * \param file_priv DRM file private.
  125. * \param cmd command.
  126. * \param arg user argument, pointing to a drm_map structure.
  127. *
  128. * \return zero on success or a negative number on failure.
  129. *
  130. * Searches for the mapping with the specified offset and copies its information
  131. * into userspace
  132. */
  133. int drm_getmap(struct drm_device *dev, void *data,
  134. struct drm_file *file_priv)
  135. {
  136. struct drm_map *map = data;
  137. struct drm_map_list *r_list = NULL;
  138. struct list_head *list;
  139. int idx;
  140. int i;
  141. idx = map->offset;
  142. if (idx < 0)
  143. return -EINVAL;
  144. i = 0;
  145. mutex_lock(&dev->struct_mutex);
  146. list_for_each(list, &dev->maplist) {
  147. if (i == idx) {
  148. r_list = list_entry(list, struct drm_map_list, head);
  149. break;
  150. }
  151. i++;
  152. }
  153. if (!r_list || !r_list->map) {
  154. mutex_unlock(&dev->struct_mutex);
  155. return -EINVAL;
  156. }
  157. map->offset = r_list->map->offset;
  158. map->size = r_list->map->size;
  159. map->type = r_list->map->type;
  160. map->flags = r_list->map->flags;
  161. map->handle = (void *)(unsigned long) r_list->user_token;
  162. map->mtrr = r_list->map->mtrr;
  163. mutex_unlock(&dev->struct_mutex);
  164. return 0;
  165. }
  166. /**
  167. * Get client information.
  168. *
  169. * \param inode device inode.
  170. * \param file_priv DRM file private.
  171. * \param cmd command.
  172. * \param arg user argument, pointing to a drm_client structure.
  173. *
  174. * \return zero on success or a negative number on failure.
  175. *
  176. * Searches for the client with the specified index and copies its information
  177. * into userspace
  178. */
  179. int drm_getclient(struct drm_device *dev, void *data,
  180. struct drm_file *file_priv)
  181. {
  182. struct drm_client *client = data;
  183. struct drm_file *pt;
  184. int idx;
  185. int i;
  186. idx = client->idx;
  187. i = 0;
  188. mutex_lock(&dev->struct_mutex);
  189. list_for_each_entry(pt, &dev->filelist, lhead) {
  190. if (i++ >= idx) {
  191. client->auth = pt->authenticated;
  192. client->pid = pt->pid;
  193. client->uid = pt->uid;
  194. client->magic = pt->magic;
  195. client->iocs = pt->ioctl_count;
  196. mutex_unlock(&dev->struct_mutex);
  197. return 0;
  198. }
  199. }
  200. mutex_unlock(&dev->struct_mutex);
  201. return -EINVAL;
  202. }
  203. /**
  204. * Get statistics information.
  205. *
  206. * \param inode device inode.
  207. * \param file_priv DRM file private.
  208. * \param cmd command.
  209. * \param arg user argument, pointing to a drm_stats structure.
  210. *
  211. * \return zero on success or a negative number on failure.
  212. */
  213. int drm_getstats(struct drm_device *dev, void *data,
  214. struct drm_file *file_priv)
  215. {
  216. struct drm_stats *stats = data;
  217. int i;
  218. memset(stats, 0, sizeof(*stats));
  219. for (i = 0; i < dev->counters; i++) {
  220. if (dev->types[i] == _DRM_STAT_LOCK)
  221. stats->data[i].value =
  222. (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
  223. else
  224. stats->data[i].value = atomic_read(&dev->counts[i]);
  225. stats->data[i].type = dev->types[i];
  226. }
  227. stats->count = dev->counters;
  228. return 0;
  229. }
  230. /**
  231. * Get device/driver capabilities
  232. */
  233. int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  234. {
  235. struct drm_get_cap *req = data;
  236. req->value = 0;
  237. switch (req->capability) {
  238. case DRM_CAP_DUMB_BUFFER:
  239. if (dev->driver->dumb_create)
  240. req->value = 1;
  241. break;
  242. case DRM_CAP_VBLANK_HIGH_CRTC:
  243. req->value = 1;
  244. break;
  245. case DRM_CAP_DUMB_PREFERRED_DEPTH:
  246. req->value = dev->mode_config.preferred_depth;
  247. break;
  248. case DRM_CAP_DUMB_PREFER_SHADOW:
  249. req->value = dev->mode_config.prefer_shadow;
  250. break;
  251. default:
  252. return -EINVAL;
  253. }
  254. return 0;
  255. }
  256. /**
  257. * Setversion ioctl.
  258. *
  259. * \param inode device inode.
  260. * \param file_priv DRM file private.
  261. * \param cmd command.
  262. * \param arg user argument, pointing to a drm_lock structure.
  263. * \return zero on success or negative number on failure.
  264. *
  265. * Sets the requested interface version
  266. */
  267. int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
  268. {
  269. struct drm_set_version *sv = data;
  270. int if_version, retcode = 0;
  271. if (sv->drm_di_major != -1) {
  272. if (sv->drm_di_major != DRM_IF_MAJOR ||
  273. sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
  274. retcode = -EINVAL;
  275. goto done;
  276. }
  277. if_version = DRM_IF_VERSION(sv->drm_di_major,
  278. sv->drm_di_minor);
  279. dev->if_version = max(if_version, dev->if_version);
  280. if (sv->drm_di_minor >= 1) {
  281. /*
  282. * Version 1.1 includes tying of DRM to specific device
  283. * Version 1.4 has proper PCI domain support
  284. */
  285. retcode = drm_set_busid(dev, file_priv);
  286. if (retcode)
  287. goto done;
  288. }
  289. }
  290. if (sv->drm_dd_major != -1) {
  291. if (sv->drm_dd_major != dev->driver->major ||
  292. sv->drm_dd_minor < 0 || sv->drm_dd_minor >
  293. dev->driver->minor) {
  294. retcode = -EINVAL;
  295. goto done;
  296. }
  297. if (dev->driver->set_version)
  298. dev->driver->set_version(dev, sv);
  299. }
  300. done:
  301. sv->drm_di_major = DRM_IF_MAJOR;
  302. sv->drm_di_minor = DRM_IF_MINOR;
  303. sv->drm_dd_major = dev->driver->major;
  304. sv->drm_dd_minor = dev->driver->minor;
  305. return retcode;
  306. }
  307. /** No-op ioctl. */
  308. int drm_noop(struct drm_device *dev, void *data,
  309. struct drm_file *file_priv)
  310. {
  311. DRM_DEBUG("\n");
  312. return 0;
  313. }
  314. EXPORT_SYMBOL(drm_noop);