drm_ioctl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /**
  38. * Get the bus id.
  39. *
  40. * \param inode device inode.
  41. * \param file_priv DRM file private.
  42. * \param cmd command.
  43. * \param arg user argument, pointing to a drm_unique structure.
  44. * \return zero on success or a negative number on failure.
  45. *
  46. * Copies the bus id from drm_device::unique into user space.
  47. */
  48. int drm_getunique(struct drm_device *dev, void *data,
  49. struct drm_file *file_priv)
  50. {
  51. struct drm_unique *u = data;
  52. struct drm_master *master = file_priv->master;
  53. if (u->unique_len >= master->unique_len) {
  54. if (copy_to_user(u->unique, master->unique, master->unique_len))
  55. return -EFAULT;
  56. }
  57. u->unique_len = master->unique_len;
  58. return 0;
  59. }
  60. static void
  61. drm_unset_busid(struct drm_device *dev,
  62. struct drm_master *master)
  63. {
  64. kfree(dev->devname);
  65. dev->devname = NULL;
  66. kfree(master->unique);
  67. master->unique = NULL;
  68. master->unique_len = 0;
  69. master->unique_size = 0;
  70. }
  71. /**
  72. * Set the bus id.
  73. *
  74. * \param inode device inode.
  75. * \param file_priv DRM file private.
  76. * \param cmd command.
  77. * \param arg user argument, pointing to a drm_unique structure.
  78. * \return zero on success or a negative number on failure.
  79. *
  80. * Copies the bus id from userspace into drm_device::unique, and verifies that
  81. * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
  82. * in interface version 1.1 and will return EBUSY when setversion has requested
  83. * version 1.1 or greater.
  84. */
  85. int drm_setunique(struct drm_device *dev, void *data,
  86. struct drm_file *file_priv)
  87. {
  88. struct drm_unique *u = data;
  89. struct drm_master *master = file_priv->master;
  90. int ret;
  91. if (master->unique_len || master->unique)
  92. return -EBUSY;
  93. if (!u->unique_len || u->unique_len > 1024)
  94. return -EINVAL;
  95. if (!dev->driver->bus->set_unique)
  96. return -EINVAL;
  97. ret = dev->driver->bus->set_unique(dev, master, u);
  98. if (ret)
  99. goto err;
  100. return 0;
  101. err:
  102. drm_unset_busid(dev, master);
  103. return ret;
  104. }
  105. static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
  106. {
  107. struct drm_master *master = file_priv->master;
  108. int ret;
  109. if (master->unique != NULL)
  110. drm_unset_busid(dev, master);
  111. ret = dev->driver->bus->set_busid(dev, master);
  112. if (ret)
  113. goto err;
  114. return 0;
  115. err:
  116. drm_unset_busid(dev, master);
  117. return ret;
  118. }
  119. /**
  120. * Get a mapping information.
  121. *
  122. * \param inode device inode.
  123. * \param file_priv DRM file private.
  124. * \param cmd command.
  125. * \param arg user argument, pointing to a drm_map structure.
  126. *
  127. * \return zero on success or a negative number on failure.
  128. *
  129. * Searches for the mapping with the specified offset and copies its information
  130. * into userspace
  131. */
  132. int drm_getmap(struct drm_device *dev, void *data,
  133. struct drm_file *file_priv)
  134. {
  135. struct drm_map *map = data;
  136. struct drm_map_list *r_list = NULL;
  137. struct list_head *list;
  138. int idx;
  139. int i;
  140. idx = map->offset;
  141. if (idx < 0)
  142. return -EINVAL;
  143. i = 0;
  144. mutex_lock(&dev->struct_mutex);
  145. list_for_each(list, &dev->maplist) {
  146. if (i == idx) {
  147. r_list = list_entry(list, struct drm_map_list, head);
  148. break;
  149. }
  150. i++;
  151. }
  152. if (!r_list || !r_list->map) {
  153. mutex_unlock(&dev->struct_mutex);
  154. return -EINVAL;
  155. }
  156. map->offset = r_list->map->offset;
  157. map->size = r_list->map->size;
  158. map->type = r_list->map->type;
  159. map->flags = r_list->map->flags;
  160. map->handle = (void *)(unsigned long) r_list->user_token;
  161. map->mtrr = r_list->map->mtrr;
  162. mutex_unlock(&dev->struct_mutex);
  163. return 0;
  164. }
  165. /**
  166. * Get client information.
  167. *
  168. * \param inode device inode.
  169. * \param file_priv DRM file private.
  170. * \param cmd command.
  171. * \param arg user argument, pointing to a drm_client structure.
  172. *
  173. * \return zero on success or a negative number on failure.
  174. *
  175. * Searches for the client with the specified index and copies its information
  176. * into userspace
  177. */
  178. int drm_getclient(struct drm_device *dev, void *data,
  179. struct drm_file *file_priv)
  180. {
  181. struct drm_client *client = data;
  182. struct drm_file *pt;
  183. int idx;
  184. int i;
  185. idx = client->idx;
  186. i = 0;
  187. mutex_lock(&dev->struct_mutex);
  188. list_for_each_entry(pt, &dev->filelist, lhead) {
  189. if (i++ >= idx) {
  190. client->auth = pt->authenticated;
  191. client->pid = pt->pid;
  192. client->uid = pt->uid;
  193. client->magic = pt->magic;
  194. client->iocs = pt->ioctl_count;
  195. mutex_unlock(&dev->struct_mutex);
  196. return 0;
  197. }
  198. }
  199. mutex_unlock(&dev->struct_mutex);
  200. return -EINVAL;
  201. }
  202. /**
  203. * Get statistics information.
  204. *
  205. * \param inode device inode.
  206. * \param file_priv DRM file private.
  207. * \param cmd command.
  208. * \param arg user argument, pointing to a drm_stats structure.
  209. *
  210. * \return zero on success or a negative number on failure.
  211. */
  212. int drm_getstats(struct drm_device *dev, void *data,
  213. struct drm_file *file_priv)
  214. {
  215. struct drm_stats *stats = data;
  216. int i;
  217. memset(stats, 0, sizeof(*stats));
  218. for (i = 0; i < dev->counters; i++) {
  219. if (dev->types[i] == _DRM_STAT_LOCK)
  220. stats->data[i].value =
  221. (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
  222. else
  223. stats->data[i].value = atomic_read(&dev->counts[i]);
  224. stats->data[i].type = dev->types[i];
  225. }
  226. stats->count = dev->counters;
  227. return 0;
  228. }
  229. /**
  230. * Get device/driver capabilities
  231. */
  232. int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  233. {
  234. struct drm_get_cap *req = data;
  235. req->value = 0;
  236. switch (req->capability) {
  237. case DRM_CAP_DUMB_BUFFER:
  238. if (dev->driver->dumb_create)
  239. req->value = 1;
  240. break;
  241. case DRM_CAP_VBLANK_HIGH_CRTC:
  242. req->value = 1;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. return 0;
  248. }
  249. /**
  250. * Setversion ioctl.
  251. *
  252. * \param inode device inode.
  253. * \param file_priv DRM file private.
  254. * \param cmd command.
  255. * \param arg user argument, pointing to a drm_lock structure.
  256. * \return zero on success or negative number on failure.
  257. *
  258. * Sets the requested interface version
  259. */
  260. int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
  261. {
  262. struct drm_set_version *sv = data;
  263. int if_version, retcode = 0;
  264. if (sv->drm_di_major != -1) {
  265. if (sv->drm_di_major != DRM_IF_MAJOR ||
  266. sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
  267. retcode = -EINVAL;
  268. goto done;
  269. }
  270. if_version = DRM_IF_VERSION(sv->drm_di_major,
  271. sv->drm_di_minor);
  272. dev->if_version = max(if_version, dev->if_version);
  273. if (sv->drm_di_minor >= 1) {
  274. /*
  275. * Version 1.1 includes tying of DRM to specific device
  276. * Version 1.4 has proper PCI domain support
  277. */
  278. retcode = drm_set_busid(dev, file_priv);
  279. if (retcode)
  280. goto done;
  281. }
  282. }
  283. if (sv->drm_dd_major != -1) {
  284. if (sv->drm_dd_major != dev->driver->major ||
  285. sv->drm_dd_minor < 0 || sv->drm_dd_minor >
  286. dev->driver->minor) {
  287. retcode = -EINVAL;
  288. goto done;
  289. }
  290. if (dev->driver->set_version)
  291. dev->driver->set_version(dev, sv);
  292. }
  293. done:
  294. sv->drm_di_major = DRM_IF_MAJOR;
  295. sv->drm_di_minor = DRM_IF_MINOR;
  296. sv->drm_dd_major = dev->driver->major;
  297. sv->drm_dd_minor = dev->driver->minor;
  298. return retcode;
  299. }
  300. /** No-op ioctl. */
  301. int drm_noop(struct drm_device *dev, void *data,
  302. struct drm_file *file_priv)
  303. {
  304. DRM_DEBUG("\n");
  305. return 0;
  306. }