blob: afe5f07f2e171e6001af950414d31df703080b07 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_fops.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * File operations for DRM
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \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"
Dave Airlied985c102006-01-02 21:32:48 +110038#include "drm_sarea.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/poll.h>
40
Dave Airlieb5e89ed2005-09-25 14:28:13 +100041static int drm_open_helper(struct inode *inode, struct file *filp,
42 drm_device_t * dev);
Dave Airliec94f7022005-07-07 21:03:38 +100043
Dave Airlieb5e89ed2005-09-25 14:28:13 +100044static int drm_setup(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Dave Airlied985c102006-01-02 21:32:48 +110046 drm_local_map_t *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int i;
48 int ret;
Dave Airlie11d9c2f2007-02-18 17:13:39 +110049 u32 sareapage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Dave Airlie22eae942005-11-10 22:16:34 +110051 if (dev->driver->firstopen) {
52 ret = dev->driver->firstopen(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100053 if (ret != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return ret;
55 }
56
Thomas Hellstrom1f4eccf2006-08-18 16:37:10 +100057 dev->magicfree.next = NULL;
58
Dave Airlied985c102006-01-02 21:32:48 +110059 /* prebuild the SAREA */
Dave Airlie11d9c2f2007-02-18 17:13:39 +110060 sareapage = max(SAREA_MAX, PAGE_SIZE);
61 i = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
Dave Airlied985c102006-01-02 21:32:48 +110062 if (i != 0)
63 return i;
64
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 atomic_set(&dev->ioctl_count, 0);
66 atomic_set(&dev->vma_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 dev->buf_use = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 atomic_set(&dev->buf_alloc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dave Airlieb5e89ed2005-09-25 14:28:13 +100070 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
71 i = drm_dma_setup(dev);
72 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return i;
74 }
75
Dave Airlie3d774612006-08-07 20:07:43 +100076 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077 atomic_set(&dev->counts[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Thomas Hellstrom8669cbc2006-08-07 22:22:10 +100079 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
80 INIT_LIST_HEAD(&dev->magicfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082 dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist), DRM_MEM_CTXLIST);
83 if (dev->ctxlist == NULL)
84 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
86 INIT_LIST_HEAD(&dev->ctxlist->head);
87
88 dev->vmalist = NULL;
Dave Airlie11d9c2f2007-02-18 17:13:39 +110089 dev->sigdata.lock = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090 init_waitqueue_head(&dev->lock.lock_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 dev->queue_count = 0;
92 dev->queue_reserved = 0;
93 dev->queue_slots = 0;
94 dev->queuelist = NULL;
95 dev->irq_enabled = 0;
96 dev->context_flag = 0;
97 dev->interrupt_flag = 0;
98 dev->dma_flag = 0;
99 dev->last_context = 0;
100 dev->last_switch = 0;
101 dev->last_checked = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000102 init_waitqueue_head(&dev->context_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 dev->if_version = 0;
104
105 dev->ctx_start = 0;
106 dev->lck_start = 0;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 dev->buf_async = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 init_waitqueue_head(&dev->buf_readers);
110 init_waitqueue_head(&dev->buf_writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 /*
115 * The kernel's context could be created here, but is now created
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 * in drm_dma_enqueue. This is more resource-efficient for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * hardware that does not do DMA, but may mean that
118 * drm_select_queue fails between the time the interrupt is
119 * initialized and the time the queues are initialized.
120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 return 0;
123}
124
125/**
126 * Open file.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * \param inode device inode
129 * \param filp file pointer.
130 * \return zero on success or a negative number on failure.
131 *
132 * Searches the DRM device with the same minor number, calls open_helper(), and
133 * increments the device open count. If the open count was previous at zero,
134 * i.e., it's the first that the device is open, then calls setup().
135 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000136int drm_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 drm_device_t *dev = NULL;
139 int minor = iminor(inode);
140 int retcode = 0;
141
142 if (!((minor >= 0) && (minor < drm_cards_limit)))
143 return -ENODEV;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (!drm_heads[minor])
146 return -ENODEV;
147
148 if (!(dev = drm_heads[minor]->dev))
149 return -ENODEV;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150
151 retcode = drm_open_helper(inode, filp, dev);
152 if (!retcode) {
153 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
154 spin_lock(&dev->count_lock);
155 if (!dev->open_count++) {
156 spin_unlock(&dev->count_lock);
157 return drm_setup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 return retcode;
163}
164EXPORT_SYMBOL(drm_open);
165
166/**
Dave Airlied985c102006-01-02 21:32:48 +1100167 * File \c open operation.
168 *
169 * \param inode device inode.
170 * \param filp file pointer.
171 *
172 * Puts the dev->fops corresponding to the device minor number into
173 * \p filp, call the \c open method, and restore the file operations.
174 */
175int drm_stub_open(struct inode *inode, struct file *filp)
176{
177 drm_device_t *dev = NULL;
178 int minor = iminor(inode);
179 int err = -ENODEV;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800180 const struct file_operations *old_fops;
Dave Airlied985c102006-01-02 21:32:48 +1100181
182 DRM_DEBUG("\n");
183
184 if (!((minor >= 0) && (minor < drm_cards_limit)))
185 return -ENODEV;
186
187 if (!drm_heads[minor])
188 return -ENODEV;
189
190 if (!(dev = drm_heads[minor]->dev))
191 return -ENODEV;
192
193 old_fops = filp->f_op;
194 filp->f_op = fops_get(&dev->driver->fops);
195 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
196 fops_put(filp->f_op);
197 filp->f_op = fops_get(old_fops);
198 }
199 fops_put(old_fops);
200
201 return err;
202}
203
204/**
205 * Check whether DRI will run on this CPU.
206 *
207 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
208 */
209static int drm_cpu_valid(void)
210{
211#if defined(__i386__)
212 if (boot_cpu_data.x86 == 3)
213 return 0; /* No cmpxchg on a 386 */
214#endif
215#if defined(__sparc__) && !defined(__sparc_v9__)
216 return 0; /* No cmpxchg before v9 sparc. */
217#endif
218 return 1;
219}
220
221/**
222 * Called whenever a process opens /dev/drm.
223 *
224 * \param inode device inode.
225 * \param filp file pointer.
226 * \param dev device.
227 * \return zero on success or a negative number on failure.
228 *
229 * Creates and initializes a drm_file structure for the file private data in \p
230 * filp and add it into the double linked list in \p dev.
231 */
232static int drm_open_helper(struct inode *inode, struct file *filp,
233 drm_device_t * dev)
234{
235 int minor = iminor(inode);
236 drm_file_t *priv;
237 int ret;
238
239 if (filp->f_flags & O_EXCL)
240 return -EBUSY; /* No exclusive opens */
241 if (!drm_cpu_valid())
242 return -EINVAL;
243
244 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
245
246 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
247 if (!priv)
248 return -ENOMEM;
249
250 memset(priv, 0, sizeof(*priv));
251 filp->private_data = priv;
252 priv->uid = current->euid;
253 priv->pid = current->pid;
254 priv->minor = minor;
255 priv->head = drm_heads[minor];
256 priv->ioctl_count = 0;
257 /* for compatibility root is always authenticated */
258 priv->authenticated = capable(CAP_SYS_ADMIN);
259 priv->lock_count = 0;
260
261 if (dev->driver->open) {
262 ret = dev->driver->open(dev, priv);
263 if (ret < 0)
264 goto out_free;
265 }
266
Dave Airlie30e2fb12006-02-02 19:37:46 +1100267 mutex_lock(&dev->struct_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100268 if (!dev->file_last) {
269 priv->next = NULL;
270 priv->prev = NULL;
271 dev->file_first = priv;
272 dev->file_last = priv;
273 /* first opener automatically becomes master */
274 priv->master = 1;
275 } else {
276 priv->next = NULL;
277 priv->prev = dev->file_last;
278 dev->file_last->next = priv;
279 dev->file_last = priv;
280 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100281 mutex_unlock(&dev->struct_mutex);
Dave Airlied985c102006-01-02 21:32:48 +1100282
283#ifdef __alpha__
284 /*
285 * Default the hose
286 */
287 if (!dev->hose) {
288 struct pci_dev *pci_dev;
289 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
290 if (pci_dev) {
291 dev->hose = pci_dev->sysdata;
292 pci_dev_put(pci_dev);
293 }
294 if (!dev->hose) {
295 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
296 if (b)
297 dev->hose = b->sysdata;
298 }
299 }
300#endif
301
302 return 0;
303 out_free:
304 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
305 filp->private_data = NULL;
306 return ret;
307}
308
309/** No-op. */
310int drm_fasync(int fd, struct file *filp, int on)
311{
312 drm_file_t *priv = filp->private_data;
313 drm_device_t *dev = priv->head->dev;
314 int retcode;
315
316 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
317 (long)old_encode_dev(priv->head->device));
318 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
319 if (retcode < 0)
320 return retcode;
321 return 0;
322}
323EXPORT_SYMBOL(drm_fasync);
324
325/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * Release file.
327 *
328 * \param inode device inode
329 * \param filp file pointer.
330 * \return zero on success or a negative number on failure.
331 *
332 * If the hardware lock is held then free it, and take it again for the kernel
333 * context since it's necessary to reclaim buffers. Unlink the file private
334 * data from its list and free it. Decreases the open count and if it reaches
Dave Airlie22eae942005-11-10 22:16:34 +1100335 * zero calls drm_lastclose().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337int drm_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 drm_file_t *priv = filp->private_data;
340 drm_device_t *dev;
341 int retcode = 0;
342
343 lock_kernel();
344 dev = priv->head->dev;
345
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 DRM_DEBUG("open_count = %d\n", dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Dave Airlie22eae942005-11-10 22:16:34 +1100348 if (dev->driver->preclose)
349 dev->driver->preclose(dev, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* ========================================================
352 * Begin inline drm_release
353 */
354
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
356 current->pid, (long)old_encode_dev(priv->head->device),
357 dev->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000359 if (priv->lock_count && dev->lock.hw_lock &&
360 _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
361 dev->lock.filp == filp) {
362 DRM_DEBUG("File %p released, freeing lock for context %d\n",
363 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
364
Dave Airlie22eae942005-11-10 22:16:34 +1100365 if (dev->driver->reclaim_buffers_locked)
366 dev->driver->reclaim_buffers_locked(dev, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000368 drm_lock_free(dev, &dev->lock.hw_lock->lock,
369 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 /* FIXME: may require heavy-handed reset of
372 hardware at this point, possibly
373 processed via a callback to the X
374 server. */
Dave Airlie22eae942005-11-10 22:16:34 +1100375 } else if (dev->driver->reclaim_buffers_locked && priv->lock_count
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 && dev->lock.hw_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* The lock is required to reclaim buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000378 DECLARE_WAITQUEUE(entry, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380 add_wait_queue(&dev->lock.lock_queue, &entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 for (;;) {
382 __set_current_state(TASK_INTERRUPTIBLE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 if (!dev->lock.hw_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Device has been unregistered */
385 retcode = -EINTR;
386 break;
387 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000388 if (drm_lock_take(&dev->lock.hw_lock->lock,
389 DRM_KERNEL_CONTEXT)) {
390 dev->lock.filp = filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 dev->lock.lock_time = jiffies;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break; /* Got lock */
394 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 /* Contention */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 schedule();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000397 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 retcode = -ERESTARTSYS;
399 break;
400 }
401 }
402 __set_current_state(TASK_RUNNING);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403 remove_wait_queue(&dev->lock.lock_queue, &entry);
404 if (!retcode) {
Dave Airlie22eae942005-11-10 22:16:34 +1100405 dev->driver->reclaim_buffers_locked(dev, filp);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000406 drm_lock_free(dev, &dev->lock.hw_lock->lock,
407 DRM_KERNEL_CONTEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410
Dave Airlie22eae942005-11-10 22:16:34 +1100411 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
412 !dev->driver->reclaim_buffers_locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 dev->driver->reclaim_buffers(dev, filp);
414 }
415
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 drm_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Dave Airlie30e2fb12006-02-02 19:37:46 +1100418 mutex_lock(&dev->ctxlist_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 if (dev->ctxlist && (!list_empty(&dev->ctxlist->head))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 drm_ctx_list_t *pos, *n;
421
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) {
423 if (pos->tag == priv &&
424 pos->handle != DRM_KERNEL_CONTEXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (dev->driver->context_dtor)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426 dev->driver->context_dtor(dev,
427 pos->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 drm_ctxbitmap_free(dev, pos->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 list_del(&pos->head);
432 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 --dev->ctx_count;
434 }
435 }
436 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100437 mutex_unlock(&dev->ctxlist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Dave Airlie30e2fb12006-02-02 19:37:46 +1100439 mutex_lock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000440 if (priv->remove_auth_on_close == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 drm_file_t *temp = dev->file_first;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000442 while (temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 temp->authenticated = 0;
444 temp = temp->next;
445 }
446 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 if (priv->prev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 priv->prev->next = priv->next;
449 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000450 dev->file_first = priv->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000452 if (priv->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 priv->next->prev = priv->prev;
454 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000455 dev->file_last = priv->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100457 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458
Dave Airlie22eae942005-11-10 22:16:34 +1100459 if (dev->driver->postclose)
460 dev->driver->postclose(dev, priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* ========================================================
464 * End inline drm_release
465 */
466
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
468 spin_lock(&dev->count_lock);
469 if (!--dev->open_count) {
470 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
471 DRM_ERROR("Device busy: %d %d\n",
472 atomic_read(&dev->ioctl_count), dev->blocked);
473 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 unlock_kernel();
475 return -EBUSY;
476 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unlock_kernel();
Dave Airlie22eae942005-11-10 22:16:34 +1100479 return drm_lastclose(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 unlock_kernel();
484
485 return retcode;
486}
487EXPORT_SYMBOL(drm_release);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/** No-op. */
490unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
491{
492 return 0;
493}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494EXPORT_SYMBOL(drm_poll);