drm_auth.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * \file drm_auth.c
  3. * IOCTLs for authentication
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Tue Feb 2 08:37:54 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. /**
  36. * Find the file with the given magic number.
  37. *
  38. * \param dev DRM device.
  39. * \param magic magic number.
  40. *
  41. * Searches in drm_device::magiclist within all files with the same hash key
  42. * the one with matching magic number, while holding the drm_device::struct_mutex
  43. * lock.
  44. */
  45. static drm_file_t *drm_find_file(drm_device_t * dev, drm_magic_t magic)
  46. {
  47. drm_file_t *retval = NULL;
  48. drm_magic_entry_t *pt;
  49. drm_hash_item_t *hash;
  50. mutex_lock(&dev->struct_mutex);
  51. if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
  52. pt = drm_hash_entry(hash, drm_magic_entry_t, hash_item);
  53. retval = pt->priv;
  54. }
  55. mutex_unlock(&dev->struct_mutex);
  56. return retval;
  57. }
  58. /**
  59. * Adds a magic number.
  60. *
  61. * \param dev DRM device.
  62. * \param priv file private data.
  63. * \param magic magic number.
  64. *
  65. * Creates a drm_magic_entry structure and appends to the linked list
  66. * associated the magic number hash key in drm_device::magiclist, while holding
  67. * the drm_device::struct_mutex lock.
  68. */
  69. static int drm_add_magic(drm_device_t * dev, drm_file_t * priv,
  70. drm_magic_t magic)
  71. {
  72. drm_magic_entry_t *entry;
  73. DRM_DEBUG("%d\n", magic);
  74. entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
  75. if (!entry)
  76. return -ENOMEM;
  77. memset(entry, 0, sizeof(*entry));
  78. entry->priv = priv;
  79. entry->hash_item.key = (unsigned long)magic;
  80. mutex_lock(&dev->struct_mutex);
  81. drm_ht_insert_item(&dev->magiclist, &entry->hash_item);
  82. list_add_tail(&entry->head, &dev->magicfree);
  83. mutex_unlock(&dev->struct_mutex);
  84. return 0;
  85. }
  86. /**
  87. * Remove a magic number.
  88. *
  89. * \param dev DRM device.
  90. * \param magic magic number.
  91. *
  92. * Searches and unlinks the entry in drm_device::magiclist with the magic
  93. * number hash key, while holding the drm_device::struct_mutex lock.
  94. */
  95. static int drm_remove_magic(drm_device_t * dev, drm_magic_t magic)
  96. {
  97. drm_magic_entry_t *pt;
  98. drm_hash_item_t *hash;
  99. DRM_DEBUG("%d\n", magic);
  100. mutex_lock(&dev->struct_mutex);
  101. if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
  102. mutex_unlock(&dev->struct_mutex);
  103. return -EINVAL;
  104. }
  105. pt = drm_hash_entry(hash, drm_magic_entry_t, hash_item);
  106. drm_ht_remove_item(&dev->magiclist, hash);
  107. list_del(&pt->head);
  108. mutex_unlock(&dev->struct_mutex);
  109. drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
  110. return 0;
  111. }
  112. /**
  113. * Get a unique magic number (ioctl).
  114. *
  115. * \param inode device inode.
  116. * \param filp file pointer.
  117. * \param cmd command.
  118. * \param arg pointer to a resulting drm_auth structure.
  119. * \return zero on success, or a negative number on failure.
  120. *
  121. * If there is a magic number in drm_file::magic then use it, otherwise
  122. * searches an unique non-zero magic number and add it associating it with \p
  123. * filp.
  124. */
  125. int drm_getmagic(struct inode *inode, struct file *filp,
  126. unsigned int cmd, unsigned long arg)
  127. {
  128. static drm_magic_t sequence = 0;
  129. static DEFINE_SPINLOCK(lock);
  130. drm_file_t *priv = filp->private_data;
  131. drm_device_t *dev = priv->head->dev;
  132. drm_auth_t auth;
  133. /* Find unique magic */
  134. if (priv->magic) {
  135. auth.magic = priv->magic;
  136. } else {
  137. do {
  138. spin_lock(&lock);
  139. if (!sequence)
  140. ++sequence; /* reserve 0 */
  141. auth.magic = sequence++;
  142. spin_unlock(&lock);
  143. } while (drm_find_file(dev, auth.magic));
  144. priv->magic = auth.magic;
  145. drm_add_magic(dev, priv, auth.magic);
  146. }
  147. DRM_DEBUG("%u\n", auth.magic);
  148. if (copy_to_user((drm_auth_t __user *) arg, &auth, sizeof(auth)))
  149. return -EFAULT;
  150. return 0;
  151. }
  152. /**
  153. * Authenticate with a magic.
  154. *
  155. * \param inode device inode.
  156. * \param filp file pointer.
  157. * \param cmd command.
  158. * \param arg pointer to a drm_auth structure.
  159. * \return zero if authentication successed, or a negative number otherwise.
  160. *
  161. * Checks if \p filp is associated with the magic number passed in \arg.
  162. */
  163. int drm_authmagic(struct inode *inode, struct file *filp,
  164. unsigned int cmd, unsigned long arg)
  165. {
  166. drm_file_t *priv = filp->private_data;
  167. drm_device_t *dev = priv->head->dev;
  168. drm_auth_t auth;
  169. drm_file_t *file;
  170. if (copy_from_user(&auth, (drm_auth_t __user *) arg, sizeof(auth)))
  171. return -EFAULT;
  172. DRM_DEBUG("%u\n", auth.magic);
  173. if ((file = drm_find_file(dev, auth.magic))) {
  174. file->authenticated = 1;
  175. drm_remove_magic(dev, auth.magic);
  176. return 0;
  177. }
  178. return -EINVAL;
  179. }