Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 2 | * \file drm_fops.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * File operations for DRM |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
| 6 | * \author Daryll Strauss <daryll@valinux.com> |
| 7 | * \author Gareth Hughes <gareth@valinux.com> |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com |
| 12 | * |
| 13 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. |
| 14 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 15 | * All Rights Reserved. |
| 16 | * |
| 17 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 18 | * copy of this software and associated documentation files (the "Software"), |
| 19 | * to deal in the Software without restriction, including without limitation |
| 20 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 21 | * and/or sell copies of the Software, and to permit persons to whom the |
| 22 | * Software is furnished to do so, subject to the following conditions: |
| 23 | * |
| 24 | * The above copyright notice and this permission notice (including the next |
| 25 | * paragraph) shall be included in all copies or substantial portions of the |
| 26 | * Software. |
| 27 | * |
| 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 29 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 30 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 31 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 32 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 33 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 34 | * OTHER DEALINGS IN THE SOFTWARE. |
| 35 | */ |
| 36 | |
| 37 | #include "drmP.h" |
| 38 | #include <linux/poll.h> |
| 39 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 40 | static int drm_open_helper(struct inode *inode, struct file *filp, |
| 41 | drm_device_t * dev); |
Dave Airlie | c94f702 | 2005-07-07 21:03:38 +1000 | [diff] [blame] | 42 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 43 | static int drm_setup(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | int i; |
| 46 | int ret; |
| 47 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 48 | if (dev->driver->firstopen) { |
| 49 | ret = dev->driver->firstopen(dev); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 50 | if (ret != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | return ret; |
| 52 | } |
| 53 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 54 | atomic_set(&dev->ioctl_count, 0); |
| 55 | atomic_set(&dev->vma_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | dev->buf_use = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 57 | atomic_set(&dev->buf_alloc, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 59 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) { |
| 60 | i = drm_dma_setup(dev); |
| 61 | if (i < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return i; |
| 63 | } |
| 64 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 65 | for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++) |
| 66 | atomic_set(&dev->counts[i], 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 68 | for (i = 0; i < DRM_HASH_SIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | dev->magiclist[i].head = NULL; |
| 70 | dev->magiclist[i].tail = NULL; |
| 71 | } |
| 72 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 73 | dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist), DRM_MEM_CTXLIST); |
| 74 | if (dev->ctxlist == NULL) |
| 75 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | memset(dev->ctxlist, 0, sizeof(*dev->ctxlist)); |
| 77 | INIT_LIST_HEAD(&dev->ctxlist->head); |
| 78 | |
| 79 | dev->vmalist = NULL; |
| 80 | dev->sigdata.lock = dev->lock.hw_lock = NULL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 81 | init_waitqueue_head(&dev->lock.lock_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | dev->queue_count = 0; |
| 83 | dev->queue_reserved = 0; |
| 84 | dev->queue_slots = 0; |
| 85 | dev->queuelist = NULL; |
| 86 | dev->irq_enabled = 0; |
| 87 | dev->context_flag = 0; |
| 88 | dev->interrupt_flag = 0; |
| 89 | dev->dma_flag = 0; |
| 90 | dev->last_context = 0; |
| 91 | dev->last_switch = 0; |
| 92 | dev->last_checked = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 93 | init_waitqueue_head(&dev->context_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | dev->if_version = 0; |
| 95 | |
| 96 | dev->ctx_start = 0; |
| 97 | dev->lck_start = 0; |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | dev->buf_async = NULL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 100 | init_waitqueue_head(&dev->buf_readers); |
| 101 | init_waitqueue_head(&dev->buf_writers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 103 | DRM_DEBUG("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * The kernel's context could be created here, but is now created |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 107 | * in drm_dma_enqueue. This is more resource-efficient for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | * hardware that does not do DMA, but may mean that |
| 109 | * drm_select_queue fails between the time the interrupt is |
| 110 | * initialized and the time the queues are initialized. |
| 111 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Open file. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 118 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | * \param inode device inode |
| 120 | * \param filp file pointer. |
| 121 | * \return zero on success or a negative number on failure. |
| 122 | * |
| 123 | * Searches the DRM device with the same minor number, calls open_helper(), and |
| 124 | * increments the device open count. If the open count was previous at zero, |
| 125 | * i.e., it's the first that the device is open, then calls setup(). |
| 126 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 127 | int drm_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | drm_device_t *dev = NULL; |
| 130 | int minor = iminor(inode); |
| 131 | int retcode = 0; |
| 132 | |
| 133 | if (!((minor >= 0) && (minor < drm_cards_limit))) |
| 134 | return -ENODEV; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (!drm_heads[minor]) |
| 137 | return -ENODEV; |
| 138 | |
| 139 | if (!(dev = drm_heads[minor]->dev)) |
| 140 | return -ENODEV; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 141 | |
| 142 | retcode = drm_open_helper(inode, filp, dev); |
| 143 | if (!retcode) { |
| 144 | atomic_inc(&dev->counts[_DRM_STAT_OPENS]); |
| 145 | spin_lock(&dev->count_lock); |
| 146 | if (!dev->open_count++) { |
| 147 | spin_unlock(&dev->count_lock); |
| 148 | return drm_setup(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 150 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return retcode; |
| 154 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | EXPORT_SYMBOL(drm_open); |
| 157 | |
| 158 | /** |
| 159 | * Release file. |
| 160 | * |
| 161 | * \param inode device inode |
| 162 | * \param filp file pointer. |
| 163 | * \return zero on success or a negative number on failure. |
| 164 | * |
| 165 | * If the hardware lock is held then free it, and take it again for the kernel |
| 166 | * context since it's necessary to reclaim buffers. Unlink the file private |
| 167 | * data from its list and free it. Decreases the open count and if it reaches |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 168 | * zero calls drm_lastclose(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 170 | int drm_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | drm_file_t *priv = filp->private_data; |
| 173 | drm_device_t *dev; |
| 174 | int retcode = 0; |
| 175 | |
| 176 | lock_kernel(); |
| 177 | dev = priv->head->dev; |
| 178 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 179 | DRM_DEBUG("open_count = %d\n", dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 181 | if (dev->driver->preclose) |
| 182 | dev->driver->preclose(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | /* ======================================================== |
| 185 | * Begin inline drm_release |
| 186 | */ |
| 187 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 188 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", |
| 189 | current->pid, (long)old_encode_dev(priv->head->device), |
| 190 | dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 192 | if (priv->lock_count && dev->lock.hw_lock && |
| 193 | _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) && |
| 194 | dev->lock.filp == filp) { |
| 195 | DRM_DEBUG("File %p released, freeing lock for context %d\n", |
| 196 | filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); |
| 197 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 198 | if (dev->driver->reclaim_buffers_locked) |
| 199 | dev->driver->reclaim_buffers_locked(dev, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 201 | drm_lock_free(dev, &dev->lock.hw_lock->lock, |
| 202 | _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 204 | /* FIXME: may require heavy-handed reset of |
| 205 | hardware at this point, possibly |
| 206 | processed via a callback to the X |
| 207 | server. */ |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 208 | } else if (dev->driver->reclaim_buffers_locked && priv->lock_count |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 209 | && dev->lock.hw_lock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* The lock is required to reclaim buffers */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 211 | DECLARE_WAITQUEUE(entry, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 213 | add_wait_queue(&dev->lock.lock_queue, &entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | for (;;) { |
| 215 | __set_current_state(TASK_INTERRUPTIBLE); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 216 | if (!dev->lock.hw_lock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* Device has been unregistered */ |
| 218 | retcode = -EINTR; |
| 219 | break; |
| 220 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 221 | if (drm_lock_take(&dev->lock.hw_lock->lock, |
| 222 | DRM_KERNEL_CONTEXT)) { |
| 223 | dev->lock.filp = filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | dev->lock.lock_time = jiffies; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 225 | atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | break; /* Got lock */ |
| 227 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 228 | /* Contention */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | schedule(); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 230 | if (signal_pending(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | retcode = -ERESTARTSYS; |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | __set_current_state(TASK_RUNNING); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 236 | remove_wait_queue(&dev->lock.lock_queue, &entry); |
| 237 | if (!retcode) { |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 238 | dev->driver->reclaim_buffers_locked(dev, filp); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 239 | drm_lock_free(dev, &dev->lock.hw_lock->lock, |
| 240 | DRM_KERNEL_CONTEXT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 243 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 244 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && |
| 245 | !dev->driver->reclaim_buffers_locked) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | dev->driver->reclaim_buffers(dev, filp); |
| 247 | } |
| 248 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 249 | drm_fasync(-1, filp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 251 | down(&dev->ctxlist_sem); |
| 252 | if (dev->ctxlist && (!list_empty(&dev->ctxlist->head))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | drm_ctx_list_t *pos, *n; |
| 254 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 255 | list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) { |
| 256 | if (pos->tag == priv && |
| 257 | pos->handle != DRM_KERNEL_CONTEXT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (dev->driver->context_dtor) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 259 | dev->driver->context_dtor(dev, |
| 260 | pos->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 262 | drm_ctxbitmap_free(dev, pos->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 264 | list_del(&pos->head); |
| 265 | drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | --dev->ctx_count; |
| 267 | } |
| 268 | } |
| 269 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 270 | up(&dev->ctxlist_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 272 | down(&dev->struct_sem); |
| 273 | if (priv->remove_auth_on_close == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | drm_file_t *temp = dev->file_first; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 275 | while (temp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | temp->authenticated = 0; |
| 277 | temp = temp->next; |
| 278 | } |
| 279 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 280 | if (priv->prev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | priv->prev->next = priv->next; |
| 282 | } else { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 283 | dev->file_first = priv->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 285 | if (priv->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | priv->next->prev = priv->prev; |
| 287 | } else { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 288 | dev->file_last = priv->prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 290 | up(&dev->struct_sem); |
| 291 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 292 | if (dev->driver->postclose) |
| 293 | dev->driver->postclose(dev, priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 295 | drm_free(priv, sizeof(*priv), DRM_MEM_FILES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | /* ======================================================== |
| 298 | * End inline drm_release |
| 299 | */ |
| 300 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 301 | atomic_inc(&dev->counts[_DRM_STAT_CLOSES]); |
| 302 | spin_lock(&dev->count_lock); |
| 303 | if (!--dev->open_count) { |
| 304 | if (atomic_read(&dev->ioctl_count) || dev->blocked) { |
| 305 | DRM_ERROR("Device busy: %d %d\n", |
| 306 | atomic_read(&dev->ioctl_count), dev->blocked); |
| 307 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | unlock_kernel(); |
| 309 | return -EBUSY; |
| 310 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 311 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | unlock_kernel(); |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 313 | return drm_lastclose(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 315 | spin_unlock(&dev->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | unlock_kernel(); |
| 318 | |
| 319 | return retcode; |
| 320 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | EXPORT_SYMBOL(drm_release); |
| 323 | |
| 324 | /** |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 325 | * Called whenever a process opens /dev/drm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | * |
| 327 | * \param inode device inode. |
| 328 | * \param filp file pointer. |
| 329 | * \param dev device. |
| 330 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 331 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | * Creates and initializes a drm_file structure for the file private data in \p |
| 333 | * filp and add it into the double linked list in \p dev. |
| 334 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 335 | static int drm_open_helper(struct inode *inode, struct file *filp, |
| 336 | drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 338 | int minor = iminor(inode); |
| 339 | drm_file_t *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | int ret; |
| 341 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 342 | if (filp->f_flags & O_EXCL) |
| 343 | return -EBUSY; /* No exclusive opens */ |
| 344 | if (!drm_cpu_valid()) |
| 345 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor); |
| 348 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 349 | priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES); |
| 350 | if (!priv) |
| 351 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 353 | memset(priv, 0, sizeof(*priv)); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 354 | filp->private_data = priv; |
| 355 | priv->uid = current->euid; |
| 356 | priv->pid = current->pid; |
| 357 | priv->minor = minor; |
| 358 | priv->head = drm_heads[minor]; |
| 359 | priv->ioctl_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | priv->authenticated = capable(CAP_SYS_ADMIN); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 361 | priv->lock_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Dave Airlie | 22eae94 | 2005-11-10 22:16:34 +1100 | [diff] [blame^] | 363 | if (dev->driver->open) { |
| 364 | ret = dev->driver->open(dev, priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | if (ret < 0) |
| 366 | goto out_free; |
| 367 | } |
| 368 | |
| 369 | down(&dev->struct_sem); |
| 370 | if (!dev->file_last) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 371 | priv->next = NULL; |
| 372 | priv->prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | dev->file_first = priv; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 374 | dev->file_last = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } else { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 376 | priv->next = NULL; |
| 377 | priv->prev = dev->file_last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | dev->file_last->next = priv; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 379 | dev->file_last = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | up(&dev->struct_sem); |
| 382 | |
| 383 | #ifdef __alpha__ |
| 384 | /* |
| 385 | * Default the hose |
| 386 | */ |
| 387 | if (!dev->hose) { |
| 388 | struct pci_dev *pci_dev; |
| 389 | pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); |
| 390 | if (pci_dev) { |
| 391 | dev->hose = pci_dev->sysdata; |
| 392 | pci_dev_put(pci_dev); |
| 393 | } |
| 394 | if (!dev->hose) { |
| 395 | struct pci_bus *b = pci_bus_b(pci_root_buses.next); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 396 | if (b) |
| 397 | dev->hose = b->sysdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | return 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 403 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | drm_free(priv, sizeof(*priv), DRM_MEM_FILES); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 405 | filp->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | /** No-op. */ |
| 410 | int drm_flush(struct file *filp) |
| 411 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 412 | drm_file_t *priv = filp->private_data; |
| 413 | drm_device_t *dev = priv->head->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 416 | current->pid, (long)old_encode_dev(priv->head->device), |
| 417 | dev->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | EXPORT_SYMBOL(drm_flush); |
| 422 | |
| 423 | /** No-op. */ |
| 424 | int drm_fasync(int fd, struct file *filp, int on) |
| 425 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 426 | drm_file_t *priv = filp->private_data; |
| 427 | drm_device_t *dev = priv->head->dev; |
| 428 | int retcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 430 | DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, |
| 431 | (long)old_encode_dev(priv->head->device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | retcode = fasync_helper(fd, filp, on, &dev->buf_async); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 433 | if (retcode < 0) |
| 434 | return retcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | return 0; |
| 436 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | EXPORT_SYMBOL(drm_fasync); |
| 439 | |
| 440 | /** No-op. */ |
| 441 | unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait) |
| 442 | { |
| 443 | return 0; |
| 444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 446 | EXPORT_SYMBOL(drm_poll); |