compat_ioctl.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * AGPGART driver frontend compatibility ioctls
  3. * Copyright (C) 2004 Silicon Graphics, Inc.
  4. * Copyright (C) 2002-2003 Dave Jones
  5. * Copyright (C) 1999 Jeff Hartmann
  6. * Copyright (C) 1999 Precision Insight, Inc.
  7. * Copyright (C) 1999 Xi Graphics, Inc.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included
  17. * in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  25. * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/pci.h>
  30. #include <linux/fs.h>
  31. #include <linux/agpgart.h>
  32. #include <linux/slab.h>
  33. #include <linux/uaccess.h>
  34. #include "agp.h"
  35. #include "compat_ioctl.h"
  36. static int compat_agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
  37. {
  38. struct agp_info32 userinfo;
  39. struct agp_kern_info kerninfo;
  40. agp_copy_info(agp_bridge, &kerninfo);
  41. userinfo.version.major = kerninfo.version.major;
  42. userinfo.version.minor = kerninfo.version.minor;
  43. userinfo.bridge_id = kerninfo.device->vendor |
  44. (kerninfo.device->device << 16);
  45. userinfo.agp_mode = kerninfo.mode;
  46. userinfo.aper_base = (compat_long_t)kerninfo.aper_base;
  47. userinfo.aper_size = kerninfo.aper_size;
  48. userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
  49. userinfo.pg_used = kerninfo.current_memory;
  50. if (copy_to_user(arg, &userinfo, sizeof(userinfo)))
  51. return -EFAULT;
  52. return 0;
  53. }
  54. static int compat_agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
  55. {
  56. struct agp_region32 ureserve;
  57. struct agp_region kreserve;
  58. struct agp_client *client;
  59. struct agp_file_private *client_priv;
  60. DBG("");
  61. if (copy_from_user(&ureserve, arg, sizeof(ureserve)))
  62. return -EFAULT;
  63. if ((unsigned) ureserve.seg_count >= ~0U/sizeof(struct agp_segment32))
  64. return -EFAULT;
  65. kreserve.pid = ureserve.pid;
  66. kreserve.seg_count = ureserve.seg_count;
  67. client = agp_find_client_by_pid(kreserve.pid);
  68. if (kreserve.seg_count == 0) {
  69. /* remove a client */
  70. client_priv = agp_find_private(kreserve.pid);
  71. if (client_priv != NULL) {
  72. set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
  73. set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
  74. }
  75. if (client == NULL) {
  76. /* client is already removed */
  77. return 0;
  78. }
  79. return agp_remove_client(kreserve.pid);
  80. } else {
  81. struct agp_segment32 *usegment;
  82. struct agp_segment *ksegment;
  83. int seg;
  84. if (ureserve.seg_count >= 16384)
  85. return -EINVAL;
  86. usegment = kmalloc_array(ureserve.seg_count,
  87. sizeof(*usegment),
  88. GFP_KERNEL);
  89. if (!usegment)
  90. return -ENOMEM;
  91. ksegment = kmalloc_array(kreserve.seg_count,
  92. sizeof(*ksegment),
  93. GFP_KERNEL);
  94. if (!ksegment) {
  95. kfree(usegment);
  96. return -ENOMEM;
  97. }
  98. if (copy_from_user(usegment, (void __user *) ureserve.seg_list,
  99. sizeof(*usegment) * ureserve.seg_count)) {
  100. kfree(usegment);
  101. kfree(ksegment);
  102. return -EFAULT;
  103. }
  104. for (seg = 0; seg < ureserve.seg_count; seg++) {
  105. ksegment[seg].pg_start = usegment[seg].pg_start;
  106. ksegment[seg].pg_count = usegment[seg].pg_count;
  107. ksegment[seg].prot = usegment[seg].prot;
  108. }
  109. kfree(usegment);
  110. kreserve.seg_list = ksegment;
  111. if (client == NULL) {
  112. /* Create the client and add the segment */
  113. client = agp_create_client(kreserve.pid);
  114. if (client == NULL) {
  115. kfree(ksegment);
  116. return -ENOMEM;
  117. }
  118. client_priv = agp_find_private(kreserve.pid);
  119. if (client_priv != NULL) {
  120. set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
  121. set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
  122. }
  123. }
  124. return agp_create_segment(client, &kreserve);
  125. }
  126. /* Will never really happen */
  127. return -EINVAL;
  128. }
  129. static int compat_agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
  130. {
  131. struct agp_memory *memory;
  132. struct agp_allocate32 alloc;
  133. DBG("");
  134. if (copy_from_user(&alloc, arg, sizeof(alloc)))
  135. return -EFAULT;
  136. memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
  137. if (memory == NULL)
  138. return -ENOMEM;
  139. alloc.key = memory->key;
  140. alloc.physical = memory->physical;
  141. if (copy_to_user(arg, &alloc, sizeof(alloc))) {
  142. agp_free_memory_wrap(memory);
  143. return -EFAULT;
  144. }
  145. return 0;
  146. }
  147. static int compat_agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
  148. {
  149. struct agp_bind32 bind_info;
  150. struct agp_memory *memory;
  151. DBG("");
  152. if (copy_from_user(&bind_info, arg, sizeof(bind_info)))
  153. return -EFAULT;
  154. memory = agp_find_mem_by_key(bind_info.key);
  155. if (memory == NULL)
  156. return -EINVAL;
  157. return agp_bind_memory(memory, bind_info.pg_start);
  158. }
  159. static int compat_agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
  160. {
  161. struct agp_memory *memory;
  162. struct agp_unbind32 unbind;
  163. DBG("");
  164. if (copy_from_user(&unbind, arg, sizeof(unbind)))
  165. return -EFAULT;
  166. memory = agp_find_mem_by_key(unbind.key);
  167. if (memory == NULL)
  168. return -EINVAL;
  169. return agp_unbind_memory(memory);
  170. }
  171. long compat_agp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  172. {
  173. struct agp_file_private *curr_priv = file->private_data;
  174. int ret_val = -ENOTTY;
  175. mutex_lock(&(agp_fe.agp_mutex));
  176. if ((agp_fe.current_controller == NULL) &&
  177. (cmd != AGPIOC_ACQUIRE32)) {
  178. ret_val = -EINVAL;
  179. goto ioctl_out;
  180. }
  181. if ((agp_fe.backend_acquired != true) &&
  182. (cmd != AGPIOC_ACQUIRE32)) {
  183. ret_val = -EBUSY;
  184. goto ioctl_out;
  185. }
  186. if (cmd != AGPIOC_ACQUIRE32) {
  187. if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
  188. ret_val = -EPERM;
  189. goto ioctl_out;
  190. }
  191. /* Use the original pid of the controller,
  192. * in case it's threaded */
  193. if (agp_fe.current_controller->pid != curr_priv->my_pid) {
  194. ret_val = -EBUSY;
  195. goto ioctl_out;
  196. }
  197. }
  198. switch (cmd) {
  199. case AGPIOC_INFO32:
  200. ret_val = compat_agpioc_info_wrap(curr_priv, (void __user *) arg);
  201. break;
  202. case AGPIOC_ACQUIRE32:
  203. ret_val = agpioc_acquire_wrap(curr_priv);
  204. break;
  205. case AGPIOC_RELEASE32:
  206. ret_val = agpioc_release_wrap(curr_priv);
  207. break;
  208. case AGPIOC_SETUP32:
  209. ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
  210. break;
  211. case AGPIOC_RESERVE32:
  212. ret_val = compat_agpioc_reserve_wrap(curr_priv, (void __user *) arg);
  213. break;
  214. case AGPIOC_PROTECT32:
  215. ret_val = agpioc_protect_wrap(curr_priv);
  216. break;
  217. case AGPIOC_ALLOCATE32:
  218. ret_val = compat_agpioc_allocate_wrap(curr_priv, (void __user *) arg);
  219. break;
  220. case AGPIOC_DEALLOCATE32:
  221. ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
  222. break;
  223. case AGPIOC_BIND32:
  224. ret_val = compat_agpioc_bind_wrap(curr_priv, (void __user *) arg);
  225. break;
  226. case AGPIOC_UNBIND32:
  227. ret_val = compat_agpioc_unbind_wrap(curr_priv, (void __user *) arg);
  228. break;
  229. case AGPIOC_CHIPSET_FLUSH32:
  230. break;
  231. }
  232. ioctl_out:
  233. DBG("ioctl returns %d\n", ret_val);
  234. mutex_unlock(&(agp_fe.agp_mutex));
  235. return ret_val;
  236. }