PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/revue/src/linux/drivers/char/drm/drm_ioctl.c

https://github.com/gtvhacker/Logitech-Revue
C | 389 lines | 226 code | 51 blank | 112 comment | 56 complexity | ad49e2921d915829f693de1b65c130c1 MD5 | raw file
  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 filp file pointer.
  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 inode *inode, struct file *filp,
  49. unsigned int cmd, unsigned long arg)
  50. {
  51. struct drm_file *priv = filp->private_data;
  52. struct drm_device *dev = priv->head->dev;
  53. struct drm_unique __user *argp = (void __user *)arg;
  54. struct drm_unique u;
  55. if (copy_from_user(&u, argp, sizeof(u)))
  56. return -EFAULT;
  57. if (u.unique_len >= dev->unique_len) {
  58. if (copy_to_user(u.unique, dev->unique, dev->unique_len))
  59. return -EFAULT;
  60. }
  61. u.unique_len = dev->unique_len;
  62. if (copy_to_user(argp, &u, sizeof(u)))
  63. return -EFAULT;
  64. return 0;
  65. }
  66. /**
  67. * Set the bus id.
  68. *
  69. * \param inode device inode.
  70. * \param filp file pointer.
  71. * \param cmd command.
  72. * \param arg user argument, pointing to a drm_unique structure.
  73. * \return zero on success or a negative number on failure.
  74. *
  75. * Copies the bus id from userspace into drm_device::unique, and verifies that
  76. * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
  77. * in interface version 1.1 and will return EBUSY when setversion has requested
  78. * version 1.1 or greater.
  79. */
  80. int drm_setunique(struct inode *inode, struct file *filp,
  81. unsigned int cmd, unsigned long arg)
  82. {
  83. struct drm_file *priv = filp->private_data;
  84. struct drm_device *dev = priv->head->dev;
  85. struct drm_unique u;
  86. int domain, bus, slot, func, ret;
  87. if (dev->unique_len || dev->unique)
  88. return -EBUSY;
  89. if (copy_from_user(&u, (struct drm_unique __user *) arg, sizeof(u)))
  90. return -EFAULT;
  91. if (!u.unique_len || u.unique_len > 1024)
  92. return -EINVAL;
  93. dev->unique_len = u.unique_len;
  94. dev->unique = drm_alloc(u.unique_len + 1, DRM_MEM_DRIVER);
  95. if (!dev->unique)
  96. return -ENOMEM;
  97. if (copy_from_user(dev->unique, u.unique, dev->unique_len))
  98. return -EFAULT;
  99. dev->unique[dev->unique_len] = '\0';
  100. dev->devname =
  101. drm_alloc(strlen(dev->driver->pci_driver.name) +
  102. strlen(dev->unique) + 2, DRM_MEM_DRIVER);
  103. if (!dev->devname)
  104. return -ENOMEM;
  105. sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
  106. dev->unique);
  107. /* Return error if the busid submitted doesn't match the device's actual
  108. * busid.
  109. */
  110. ret = sscanf(dev->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
  111. if (ret != 3)
  112. return DRM_ERR(EINVAL);
  113. domain = bus >> 8;
  114. bus &= 0xff;
  115. if ((domain != drm_get_pci_domain(dev)) ||
  116. (bus != dev->pdev->bus->number) ||
  117. (slot != PCI_SLOT(dev->pdev->devfn)) ||
  118. (func != PCI_FUNC(dev->pdev->devfn)))
  119. return -EINVAL;
  120. return 0;
  121. }
  122. static int drm_set_busid(struct drm_device * dev)
  123. {
  124. int len;
  125. if (dev->unique != NULL)
  126. return 0;
  127. dev->unique_len = 40;
  128. dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER);
  129. if (dev->unique == NULL)
  130. return -ENOMEM;
  131. len = snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
  132. drm_get_pci_domain(dev), dev->pdev->bus->number,
  133. PCI_SLOT(dev->pdev->devfn),
  134. PCI_FUNC(dev->pdev->devfn));
  135. if (len > dev->unique_len)
  136. DRM_ERROR("Unique buffer overflowed\n");
  137. dev->devname =
  138. drm_alloc(strlen(dev->driver->pci_driver.name) + dev->unique_len +
  139. 2, DRM_MEM_DRIVER);
  140. if (dev->devname == NULL)
  141. return -ENOMEM;
  142. sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
  143. dev->unique);
  144. return 0;
  145. }
  146. /**
  147. * Get a mapping information.
  148. *
  149. * \param inode device inode.
  150. * \param filp file pointer.
  151. * \param cmd command.
  152. * \param arg user argument, pointing to a drm_map structure.
  153. *
  154. * \return zero on success or a negative number on failure.
  155. *
  156. * Searches for the mapping with the specified offset and copies its information
  157. * into userspace
  158. */
  159. int drm_getmap(struct inode *inode, struct file *filp,
  160. unsigned int cmd, unsigned long arg)
  161. {
  162. struct drm_file *priv = filp->private_data;
  163. struct drm_device *dev = priv->head->dev;
  164. struct drm_map __user *argp = (void __user *)arg;
  165. struct drm_map map;
  166. struct drm_map_list *r_list = NULL;
  167. struct list_head *list;
  168. int idx;
  169. int i;
  170. if (copy_from_user(&map, argp, sizeof(map)))
  171. return -EFAULT;
  172. idx = map.offset;
  173. mutex_lock(&dev->struct_mutex);
  174. if (idx < 0) {
  175. mutex_unlock(&dev->struct_mutex);
  176. return -EINVAL;
  177. }
  178. i = 0;
  179. list_for_each(list, &dev->maplist) {
  180. if (i == idx) {
  181. r_list = list_entry(list, struct drm_map_list, head);
  182. break;
  183. }
  184. i++;
  185. }
  186. if (!r_list || !r_list->map) {
  187. mutex_unlock(&dev->struct_mutex);
  188. return -EINVAL;
  189. }
  190. map.offset = r_list->map->offset;
  191. map.size = r_list->map->size;
  192. map.type = r_list->map->type;
  193. map.flags = r_list->map->flags;
  194. map.handle = (void *)(unsigned long)r_list->user_token;
  195. map.mtrr = r_list->map->mtrr;
  196. mutex_unlock(&dev->struct_mutex);
  197. if (copy_to_user(argp, &map, sizeof(map)))
  198. return -EFAULT;
  199. return 0;
  200. }
  201. /**
  202. * Get client information.
  203. *
  204. * \param inode device inode.
  205. * \param filp file pointer.
  206. * \param cmd command.
  207. * \param arg user argument, pointing to a drm_client structure.
  208. *
  209. * \return zero on success or a negative number on failure.
  210. *
  211. * Searches for the client with the specified index and copies its information
  212. * into userspace
  213. */
  214. int drm_getclient(struct inode *inode, struct file *filp,
  215. unsigned int cmd, unsigned long arg)
  216. {
  217. struct drm_file *priv = filp->private_data;
  218. struct drm_device *dev = priv->head->dev;
  219. struct drm_client __user *argp = (struct drm_client __user *)arg;
  220. struct drm_client client;
  221. struct drm_file *pt;
  222. int idx;
  223. int i;
  224. if (copy_from_user(&client, argp, sizeof(client)))
  225. return -EFAULT;
  226. idx = client.idx;
  227. mutex_lock(&dev->struct_mutex);
  228. if (list_empty(&dev->filelist)) {
  229. mutex_unlock(&dev->struct_mutex);
  230. return -EINVAL;
  231. }
  232. i = 0;
  233. list_for_each_entry(pt, &dev->filelist, lhead) {
  234. if (i++ >= idx)
  235. break;
  236. }
  237. client.auth = pt->authenticated;
  238. client.pid = pt->pid;
  239. client.uid = pt->uid;
  240. client.magic = pt->magic;
  241. client.iocs = pt->ioctl_count;
  242. mutex_unlock(&dev->struct_mutex);
  243. if (copy_to_user(argp, &client, sizeof(client)))
  244. return -EFAULT;
  245. return 0;
  246. }
  247. /**
  248. * Get statistics information.
  249. *
  250. * \param inode device inode.
  251. * \param filp file pointer.
  252. * \param cmd command.
  253. * \param arg user argument, pointing to a drm_stats structure.
  254. *
  255. * \return zero on success or a negative number on failure.
  256. */
  257. int drm_getstats(struct inode *inode, struct file *filp,
  258. unsigned int cmd, unsigned long arg)
  259. {
  260. struct drm_file *priv = filp->private_data;
  261. struct drm_device *dev = priv->head->dev;
  262. struct drm_stats stats;
  263. int i;
  264. memset(&stats, 0, sizeof(stats));
  265. mutex_lock(&dev->struct_mutex);
  266. for (i = 0; i < dev->counters; i++) {
  267. if (dev->types[i] == _DRM_STAT_LOCK)
  268. stats.data[i].value
  269. = (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
  270. else
  271. stats.data[i].value = atomic_read(&dev->counts[i]);
  272. stats.data[i].type = dev->types[i];
  273. }
  274. stats.count = dev->counters;
  275. mutex_unlock(&dev->struct_mutex);
  276. if (copy_to_user((struct drm_stats __user *) arg, &stats, sizeof(stats)))
  277. return -EFAULT;
  278. return 0;
  279. }
  280. /**
  281. * Setversion ioctl.
  282. *
  283. * \param inode device inode.
  284. * \param filp file pointer.
  285. * \param cmd command.
  286. * \param arg user argument, pointing to a drm_lock structure.
  287. * \return zero on success or negative number on failure.
  288. *
  289. * Sets the requested interface version
  290. */
  291. int drm_setversion(DRM_IOCTL_ARGS)
  292. {
  293. DRM_DEVICE;
  294. struct drm_set_version sv;
  295. struct drm_set_version retv;
  296. int if_version;
  297. struct drm_set_version __user *argp = (void __user *)data;
  298. int ret;
  299. if (copy_from_user(&sv, argp, sizeof(sv)))
  300. return -EFAULT;
  301. retv.drm_di_major = DRM_IF_MAJOR;
  302. retv.drm_di_minor = DRM_IF_MINOR;
  303. retv.drm_dd_major = dev->driver->major;
  304. retv.drm_dd_minor = dev->driver->minor;
  305. if (copy_to_user(argp, &retv, sizeof(retv)))
  306. return -EFAULT;
  307. if (sv.drm_di_major != -1) {
  308. if (sv.drm_di_major != DRM_IF_MAJOR ||
  309. sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
  310. return -EINVAL;
  311. if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_di_minor);
  312. dev->if_version = max(if_version, dev->if_version);
  313. if (sv.drm_di_minor >= 1) {
  314. /*
  315. * Version 1.1 includes tying of DRM to specific device
  316. */
  317. ret = drm_set_busid(dev);
  318. if (ret)
  319. return ret;
  320. }
  321. }
  322. if (sv.drm_dd_major != -1) {
  323. if (sv.drm_dd_major != dev->driver->major ||
  324. sv.drm_dd_minor < 0
  325. || sv.drm_dd_minor > dev->driver->minor)
  326. return -EINVAL;
  327. if (dev->driver->set_version)
  328. dev->driver->set_version(dev, &sv);
  329. }
  330. return 0;
  331. }
  332. /** No-op ioctl. */
  333. int drm_noop(struct inode *inode, struct file *filp, unsigned int cmd,
  334. unsigned long arg)
  335. {
  336. DRM_DEBUG("\n");
  337. return 0;
  338. }