]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /** |
b5e89ed5 | 2 | * \file drm_fops.c |
1da177e4 | 3 | * File operations for DRM |
b5e89ed5 | 4 | * |
1da177e4 LT |
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> | |
5a0e3ad6 | 39 | #include <linux/slab.h> |
e0cd3608 | 40 | #include <linux/module.h> |
1da177e4 | 41 | |
58374713 AB |
42 | /* from BKL pushdown: note that nothing else serializes idr_find() */ |
43 | DEFINE_MUTEX(drm_global_mutex); | |
e3461a2b | 44 | EXPORT_SYMBOL(drm_global_mutex); |
58374713 | 45 | |
b5e89ed5 | 46 | static int drm_open_helper(struct inode *inode, struct file *filp, |
84b1fd10 | 47 | struct drm_device * dev); |
c94f7029 | 48 | |
84b1fd10 | 49 | static int drm_setup(struct drm_device * dev) |
1da177e4 LT |
50 | { |
51 | int i; | |
52 | int ret; | |
53 | ||
22eae947 DA |
54 | if (dev->driver->firstopen) { |
55 | ret = dev->driver->firstopen(dev); | |
b5e89ed5 | 56 | if (ret != 0) |
1da177e4 LT |
57 | return ret; |
58 | } | |
59 | ||
b5e89ed5 DA |
60 | atomic_set(&dev->ioctl_count, 0); |
61 | atomic_set(&dev->vma_count, 0); | |
1da177e4 | 62 | |
f453ba04 DA |
63 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) && |
64 | !drm_core_check_feature(dev, DRIVER_MODESET)) { | |
65 | dev->buf_use = 0; | |
66 | atomic_set(&dev->buf_alloc, 0); | |
67 | ||
b5e89ed5 DA |
68 | i = drm_dma_setup(dev); |
69 | if (i < 0) | |
1da177e4 LT |
70 | return i; |
71 | } | |
72 | ||
3d77461e | 73 | for (i = 0; i < ARRAY_SIZE(dev->counts); i++) |
b5e89ed5 | 74 | atomic_set(&dev->counts[i], 0); |
1da177e4 | 75 | |
11d9c2fd | 76 | dev->sigdata.lock = NULL; |
7c1c2871 | 77 | |
1da177e4 LT |
78 | dev->context_flag = 0; |
79 | dev->interrupt_flag = 0; | |
80 | dev->dma_flag = 0; | |
81 | dev->last_context = 0; | |
82 | dev->last_switch = 0; | |
83 | dev->last_checked = 0; | |
b5e89ed5 | 84 | init_waitqueue_head(&dev->context_wait); |
1da177e4 LT |
85 | dev->if_version = 0; |
86 | ||
87 | dev->ctx_start = 0; | |
88 | dev->lck_start = 0; | |
89 | ||
1da177e4 | 90 | dev->buf_async = NULL; |
b5e89ed5 DA |
91 | init_waitqueue_head(&dev->buf_readers); |
92 | init_waitqueue_head(&dev->buf_writers); | |
1da177e4 | 93 | |
b5e89ed5 | 94 | DRM_DEBUG("\n"); |
1da177e4 LT |
95 | |
96 | /* | |
97 | * The kernel's context could be created here, but is now created | |
b5e89ed5 | 98 | * in drm_dma_enqueue. This is more resource-efficient for |
1da177e4 LT |
99 | * hardware that does not do DMA, but may mean that |
100 | * drm_select_queue fails between the time the interrupt is | |
101 | * initialized and the time the queues are initialized. | |
102 | */ | |
1da177e4 LT |
103 | |
104 | return 0; | |
105 | } | |
106 | ||
107 | /** | |
108 | * Open file. | |
b5e89ed5 | 109 | * |
1da177e4 LT |
110 | * \param inode device inode |
111 | * \param filp file pointer. | |
112 | * \return zero on success or a negative number on failure. | |
113 | * | |
114 | * Searches the DRM device with the same minor number, calls open_helper(), and | |
115 | * increments the device open count. If the open count was previous at zero, | |
116 | * i.e., it's the first that the device is open, then calls setup(). | |
117 | */ | |
b5e89ed5 | 118 | int drm_open(struct inode *inode, struct file *filp) |
1da177e4 | 119 | { |
84b1fd10 | 120 | struct drm_device *dev = NULL; |
2c14f28b DA |
121 | int minor_id = iminor(inode); |
122 | struct drm_minor *minor; | |
1da177e4 LT |
123 | int retcode = 0; |
124 | ||
2c14f28b DA |
125 | minor = idr_find(&drm_minors_idr, minor_id); |
126 | if (!minor) | |
1da177e4 | 127 | return -ENODEV; |
b5e89ed5 | 128 | |
2c14f28b | 129 | if (!(dev = minor->dev)) |
1da177e4 | 130 | return -ENODEV; |
b5e89ed5 | 131 | |
2c07a21d DA |
132 | if (drm_device_is_unplugged(dev)) |
133 | return -ENODEV; | |
134 | ||
b5e89ed5 DA |
135 | retcode = drm_open_helper(inode, filp, dev); |
136 | if (!retcode) { | |
137 | atomic_inc(&dev->counts[_DRM_STAT_OPENS]); | |
1a72d65d | 138 | if (!dev->open_count++) |
a2c0a97b | 139 | retcode = drm_setup(dev); |
1da177e4 | 140 | } |
da584058 CW |
141 | if (!retcode) { |
142 | mutex_lock(&dev->struct_mutex); | |
143 | if (minor->type == DRM_MINOR_LEGACY) { | |
144 | if (dev->dev_mapping == NULL) | |
145 | dev->dev_mapping = inode->i_mapping; | |
146 | else if (dev->dev_mapping != inode->i_mapping) | |
147 | retcode = -ENODEV; | |
148 | } | |
149 | mutex_unlock(&dev->struct_mutex); | |
f453ba04 | 150 | } |
a2c0a97b | 151 | |
1da177e4 LT |
152 | return retcode; |
153 | } | |
154 | EXPORT_SYMBOL(drm_open); | |
155 | ||
d985c108 DA |
156 | /** |
157 | * File \c open operation. | |
158 | * | |
159 | * \param inode device inode. | |
160 | * \param filp file pointer. | |
161 | * | |
162 | * Puts the dev->fops corresponding to the device minor number into | |
163 | * \p filp, call the \c open method, and restore the file operations. | |
164 | */ | |
165 | int drm_stub_open(struct inode *inode, struct file *filp) | |
166 | { | |
84b1fd10 | 167 | struct drm_device *dev = NULL; |
2c14f28b DA |
168 | struct drm_minor *minor; |
169 | int minor_id = iminor(inode); | |
d985c108 | 170 | int err = -ENODEV; |
99ac48f5 | 171 | const struct file_operations *old_fops; |
d985c108 DA |
172 | |
173 | DRM_DEBUG("\n"); | |
174 | ||
58374713 | 175 | mutex_lock(&drm_global_mutex); |
2c14f28b DA |
176 | minor = idr_find(&drm_minors_idr, minor_id); |
177 | if (!minor) | |
70ffa16e | 178 | goto out; |
d985c108 | 179 | |
2c14f28b | 180 | if (!(dev = minor->dev)) |
70ffa16e | 181 | goto out; |
d985c108 | 182 | |
2c07a21d DA |
183 | if (drm_device_is_unplugged(dev)) |
184 | goto out; | |
185 | ||
d985c108 | 186 | old_fops = filp->f_op; |
e08e96de | 187 | filp->f_op = fops_get(dev->driver->fops); |
f41ced8f LP |
188 | if (filp->f_op == NULL) { |
189 | filp->f_op = old_fops; | |
190 | goto out; | |
191 | } | |
d985c108 DA |
192 | if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) { |
193 | fops_put(filp->f_op); | |
194 | filp->f_op = fops_get(old_fops); | |
195 | } | |
196 | fops_put(old_fops); | |
197 | ||
70ffa16e | 198 | out: |
58374713 | 199 | mutex_unlock(&drm_global_mutex); |
d985c108 DA |
200 | return err; |
201 | } | |
202 | ||
203 | /** | |
204 | * Check whether DRI will run on this CPU. | |
205 | * | |
206 | * \return non-zero if the DRI will run on this CPU, or zero otherwise. | |
207 | */ | |
208 | static int drm_cpu_valid(void) | |
209 | { | |
210 | #if defined(__i386__) | |
211 | if (boot_cpu_data.x86 == 3) | |
212 | return 0; /* No cmpxchg on a 386 */ | |
213 | #endif | |
214 | #if defined(__sparc__) && !defined(__sparc_v9__) | |
215 | return 0; /* No cmpxchg before v9 sparc. */ | |
216 | #endif | |
217 | return 1; | |
218 | } | |
219 | ||
220 | /** | |
221 | * Called whenever a process opens /dev/drm. | |
222 | * | |
223 | * \param inode device inode. | |
224 | * \param filp file pointer. | |
225 | * \param dev device. | |
226 | * \return zero on success or a negative number on failure. | |
227 | * | |
228 | * Creates and initializes a drm_file structure for the file private data in \p | |
229 | * filp and add it into the double linked list in \p dev. | |
230 | */ | |
231 | static int drm_open_helper(struct inode *inode, struct file *filp, | |
84b1fd10 | 232 | struct drm_device * dev) |
d985c108 | 233 | { |
2c14f28b | 234 | int minor_id = iminor(inode); |
84b1fd10 | 235 | struct drm_file *priv; |
d985c108 DA |
236 | int ret; |
237 | ||
238 | if (filp->f_flags & O_EXCL) | |
239 | return -EBUSY; /* No exclusive opens */ | |
240 | if (!drm_cpu_valid()) | |
241 | return -EINVAL; | |
5bcf719b DA |
242 | if (dev->switch_power_state != DRM_SWITCH_POWER_ON) |
243 | return -EINVAL; | |
d985c108 | 244 | |
2c14f28b | 245 | DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id); |
d985c108 | 246 | |
6ebc22e6 | 247 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
d985c108 DA |
248 | if (!priv) |
249 | return -ENOMEM; | |
250 | ||
d985c108 | 251 | filp->private_data = priv; |
6c340eac | 252 | priv->filp = filp; |
2df68b43 | 253 | priv->uid = current_euid(); |
ba25f9dc | 254 | priv->pid = task_pid_nr(current); |
2c14f28b | 255 | priv->minor = idr_find(&drm_minors_idr, minor_id); |
d985c108 DA |
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 | ||
bd1b331f | 261 | INIT_LIST_HEAD(&priv->lhead); |
f453ba04 | 262 | INIT_LIST_HEAD(&priv->fbs); |
c9a9c5e0 KH |
263 | INIT_LIST_HEAD(&priv->event_list); |
264 | init_waitqueue_head(&priv->event_wait); | |
265 | priv->event_space = 4096; /* set aside 4k for event buffer */ | |
bd1b331f | 266 | |
673a394b EA |
267 | if (dev->driver->driver_features & DRIVER_GEM) |
268 | drm_gem_open(dev, priv); | |
269 | ||
3248877e DA |
270 | if (drm_core_check_feature(dev, DRIVER_PRIME)) |
271 | drm_prime_init_file_private(&priv->prime); | |
272 | ||
d985c108 DA |
273 | if (dev->driver->open) { |
274 | ret = dev->driver->open(dev, priv); | |
275 | if (ret < 0) | |
276 | goto out_free; | |
277 | } | |
278 | ||
7c1c2871 DA |
279 | |
280 | /* if there is no current master make this fd it */ | |
30e2fb18 | 281 | mutex_lock(&dev->struct_mutex); |
7c1c2871 DA |
282 | if (!priv->minor->master) { |
283 | /* create a new master */ | |
284 | priv->minor->master = drm_master_create(priv->minor); | |
285 | if (!priv->minor->master) { | |
dba5ed0c | 286 | mutex_unlock(&dev->struct_mutex); |
7c1c2871 DA |
287 | ret = -ENOMEM; |
288 | goto out_free; | |
289 | } | |
290 | ||
291 | priv->is_master = 1; | |
292 | /* take another reference for the copy in the local file priv */ | |
293 | priv->master = drm_master_get(priv->minor->master); | |
294 | ||
295 | priv->authenticated = 1; | |
296 | ||
297 | mutex_unlock(&dev->struct_mutex); | |
298 | if (dev->driver->master_create) { | |
299 | ret = dev->driver->master_create(dev, priv->master); | |
300 | if (ret) { | |
301 | mutex_lock(&dev->struct_mutex); | |
302 | /* drop both references if this fails */ | |
303 | drm_master_put(&priv->minor->master); | |
304 | drm_master_put(&priv->master); | |
305 | mutex_unlock(&dev->struct_mutex); | |
306 | goto out_free; | |
307 | } | |
308 | } | |
862302ff TH |
309 | mutex_lock(&dev->struct_mutex); |
310 | if (dev->driver->master_set) { | |
311 | ret = dev->driver->master_set(dev, priv, true); | |
312 | if (ret) { | |
313 | /* drop both references if this fails */ | |
314 | drm_master_put(&priv->minor->master); | |
315 | drm_master_put(&priv->master); | |
316 | mutex_unlock(&dev->struct_mutex); | |
317 | goto out_free; | |
318 | } | |
319 | } | |
320 | mutex_unlock(&dev->struct_mutex); | |
7c1c2871 DA |
321 | } else { |
322 | /* get a reference to the master */ | |
323 | priv->master = drm_master_get(priv->minor->master); | |
324 | mutex_unlock(&dev->struct_mutex); | |
325 | } | |
bd1b331f | 326 | |
7c1c2871 | 327 | mutex_lock(&dev->struct_mutex); |
bd1b331f | 328 | list_add(&priv->lhead, &dev->filelist); |
30e2fb18 | 329 | mutex_unlock(&dev->struct_mutex); |
d985c108 DA |
330 | |
331 | #ifdef __alpha__ | |
332 | /* | |
333 | * Default the hose | |
334 | */ | |
335 | if (!dev->hose) { | |
336 | struct pci_dev *pci_dev; | |
337 | pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL); | |
338 | if (pci_dev) { | |
339 | dev->hose = pci_dev->sysdata; | |
340 | pci_dev_put(pci_dev); | |
341 | } | |
342 | if (!dev->hose) { | |
343 | struct pci_bus *b = pci_bus_b(pci_root_buses.next); | |
344 | if (b) | |
345 | dev->hose = b->sysdata; | |
346 | } | |
347 | } | |
348 | #endif | |
349 | ||
350 | return 0; | |
351 | out_free: | |
9a298b2a | 352 | kfree(priv); |
d985c108 DA |
353 | filp->private_data = NULL; |
354 | return ret; | |
355 | } | |
356 | ||
357 | /** No-op. */ | |
358 | int drm_fasync(int fd, struct file *filp, int on) | |
359 | { | |
84b1fd10 | 360 | struct drm_file *priv = filp->private_data; |
2c14f28b | 361 | struct drm_device *dev = priv->minor->dev; |
d985c108 DA |
362 | |
363 | DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, | |
2c14f28b | 364 | (long)old_encode_dev(priv->minor->device)); |
60aa4924 | 365 | return fasync_helper(fd, filp, on, &dev->buf_async); |
d985c108 DA |
366 | } |
367 | EXPORT_SYMBOL(drm_fasync); | |
368 | ||
7c1c2871 DA |
369 | static void drm_master_release(struct drm_device *dev, struct file *filp) |
370 | { | |
371 | struct drm_file *file_priv = filp->private_data; | |
372 | ||
7c1c2871 DA |
373 | if (drm_i_have_hw_lock(dev, file_priv)) { |
374 | DRM_DEBUG("File %p released, freeing lock for context %d\n", | |
375 | filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock)); | |
376 | drm_lock_free(&file_priv->master->lock, | |
377 | _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock)); | |
378 | } | |
379 | ||
b0071efe DV |
380 | if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) |
381 | drm_core_reclaim_buffers(dev, file_priv); | |
7c1c2871 DA |
382 | } |
383 | ||
c9a9c5e0 KH |
384 | static void drm_events_release(struct drm_file *file_priv) |
385 | { | |
386 | struct drm_device *dev = file_priv->minor->dev; | |
387 | struct drm_pending_event *e, *et; | |
388 | struct drm_pending_vblank_event *v, *vt; | |
389 | unsigned long flags; | |
390 | ||
391 | spin_lock_irqsave(&dev->event_lock, flags); | |
392 | ||
393 | /* Remove pending flips */ | |
394 | list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link) | |
395 | if (v->base.file_priv == file_priv) { | |
396 | list_del(&v->base.link); | |
397 | drm_vblank_put(dev, v->pipe); | |
398 | v->base.destroy(&v->base); | |
399 | } | |
400 | ||
401 | /* Remove unconsumed events */ | |
402 | list_for_each_entry_safe(e, et, &file_priv->event_list, link) | |
403 | e->destroy(e); | |
404 | ||
405 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
406 | } | |
407 | ||
1da177e4 LT |
408 | /** |
409 | * Release file. | |
410 | * | |
411 | * \param inode device inode | |
6c340eac | 412 | * \param file_priv DRM file private. |
1da177e4 LT |
413 | * \return zero on success or a negative number on failure. |
414 | * | |
415 | * If the hardware lock is held then free it, and take it again for the kernel | |
416 | * context since it's necessary to reclaim buffers. Unlink the file private | |
417 | * data from its list and free it. Decreases the open count and if it reaches | |
22eae947 | 418 | * zero calls drm_lastclose(). |
1da177e4 | 419 | */ |
b5e89ed5 | 420 | int drm_release(struct inode *inode, struct file *filp) |
1da177e4 | 421 | { |
6c340eac | 422 | struct drm_file *file_priv = filp->private_data; |
2c14f28b | 423 | struct drm_device *dev = file_priv->minor->dev; |
1da177e4 LT |
424 | int retcode = 0; |
425 | ||
58374713 | 426 | mutex_lock(&drm_global_mutex); |
1da177e4 | 427 | |
b5e89ed5 | 428 | DRM_DEBUG("open_count = %d\n", dev->open_count); |
1da177e4 | 429 | |
22eae947 | 430 | if (dev->driver->preclose) |
6c340eac | 431 | dev->driver->preclose(dev, file_priv); |
1da177e4 LT |
432 | |
433 | /* ======================================================== | |
434 | * Begin inline drm_release | |
435 | */ | |
436 | ||
b5e89ed5 | 437 | DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n", |
ba25f9dc | 438 | task_pid_nr(current), |
2c14f28b | 439 | (long)old_encode_dev(file_priv->minor->device), |
b5e89ed5 DA |
440 | dev->open_count); |
441 | ||
598781d7 TH |
442 | /* Release any auth tokens that might point to this file_priv, |
443 | (do that under the drm_global_mutex) */ | |
444 | if (file_priv->magic) | |
445 | (void) drm_remove_magic(file_priv->master, file_priv->magic); | |
446 | ||
7c1c2871 DA |
447 | /* if the master has gone away we can't do anything with the lock */ |
448 | if (file_priv->minor->master) | |
449 | drm_master_release(dev, filp); | |
1da177e4 | 450 | |
c9a9c5e0 KH |
451 | drm_events_release(file_priv); |
452 | ||
ea39f835 KH |
453 | if (dev->driver->driver_features & DRIVER_MODESET) |
454 | drm_fb_release(file_priv); | |
455 | ||
4e47e02d P |
456 | if (dev->driver->driver_features & DRIVER_GEM) |
457 | drm_gem_release(dev, file_priv); | |
458 | ||
30e2fb18 | 459 | mutex_lock(&dev->ctxlist_mutex); |
bd1b331f | 460 | if (!list_empty(&dev->ctxlist)) { |
55910517 | 461 | struct drm_ctx_list *pos, *n; |
1da177e4 | 462 | |
bd1b331f | 463 | list_for_each_entry_safe(pos, n, &dev->ctxlist, head) { |
6c340eac | 464 | if (pos->tag == file_priv && |
b5e89ed5 | 465 | pos->handle != DRM_KERNEL_CONTEXT) { |
1da177e4 | 466 | if (dev->driver->context_dtor) |
b5e89ed5 DA |
467 | dev->driver->context_dtor(dev, |
468 | pos->handle); | |
1da177e4 | 469 | |
b5e89ed5 | 470 | drm_ctxbitmap_free(dev, pos->handle); |
1da177e4 | 471 | |
b5e89ed5 | 472 | list_del(&pos->head); |
9a298b2a | 473 | kfree(pos); |
1da177e4 LT |
474 | --dev->ctx_count; |
475 | } | |
476 | } | |
477 | } | |
30e2fb18 | 478 | mutex_unlock(&dev->ctxlist_mutex); |
1da177e4 | 479 | |
30e2fb18 | 480 | mutex_lock(&dev->struct_mutex); |
7c1c2871 DA |
481 | |
482 | if (file_priv->is_master) { | |
fda714c2 | 483 | struct drm_master *master = file_priv->master; |
84b1fd10 | 484 | struct drm_file *temp; |
7c1c2871 DA |
485 | list_for_each_entry(temp, &dev->filelist, lhead) { |
486 | if ((temp->master == file_priv->master) && | |
487 | (temp != file_priv)) | |
488 | temp->authenticated = 0; | |
489 | } | |
bd1b331f | 490 | |
fda714c2 TH |
491 | /** |
492 | * Since the master is disappearing, so is the | |
493 | * possibility to lock. | |
494 | */ | |
495 | ||
496 | if (master->lock.hw_lock) { | |
497 | if (dev->sigdata.lock == master->lock.hw_lock) | |
498 | dev->sigdata.lock = NULL; | |
499 | master->lock.hw_lock = NULL; | |
500 | master->lock.file_priv = NULL; | |
501 | wake_up_interruptible_all(&master->lock.lock_queue); | |
502 | } | |
503 | ||
7c1c2871 DA |
504 | if (file_priv->minor->master == file_priv->master) { |
505 | /* drop the reference held my the minor */ | |
862302ff TH |
506 | if (dev->driver->master_drop) |
507 | dev->driver->master_drop(dev, file_priv, true); | |
7c1c2871 DA |
508 | drm_master_put(&file_priv->minor->master); |
509 | } | |
1da177e4 | 510 | } |
7c1c2871 DA |
511 | |
512 | /* drop the reference held my the file priv */ | |
513 | drm_master_put(&file_priv->master); | |
514 | file_priv->is_master = 0; | |
6c340eac | 515 | list_del(&file_priv->lhead); |
30e2fb18 | 516 | mutex_unlock(&dev->struct_mutex); |
b5e89ed5 | 517 | |
22eae947 | 518 | if (dev->driver->postclose) |
6c340eac | 519 | dev->driver->postclose(dev, file_priv); |
3248877e DA |
520 | |
521 | if (drm_core_check_feature(dev, DRIVER_PRIME)) | |
522 | drm_prime_destroy_file_private(&file_priv->prime); | |
523 | ||
9a298b2a | 524 | kfree(file_priv); |
1da177e4 LT |
525 | |
526 | /* ======================================================== | |
527 | * End inline drm_release | |
528 | */ | |
529 | ||
b5e89ed5 | 530 | atomic_inc(&dev->counts[_DRM_STAT_CLOSES]); |
b5e89ed5 | 531 | if (!--dev->open_count) { |
7c1c2871 DA |
532 | if (atomic_read(&dev->ioctl_count)) { |
533 | DRM_ERROR("Device busy: %d\n", | |
534 | atomic_read(&dev->ioctl_count)); | |
58374713 | 535 | retcode = -EBUSY; |
1a72d65d CW |
536 | } else |
537 | retcode = drm_lastclose(dev); | |
2c07a21d DA |
538 | if (drm_device_is_unplugged(dev)) |
539 | drm_put_dev(dev); | |
1da177e4 | 540 | } |
58374713 | 541 | mutex_unlock(&drm_global_mutex); |
1da177e4 LT |
542 | |
543 | return retcode; | |
544 | } | |
545 | EXPORT_SYMBOL(drm_release); | |
546 | ||
c9a9c5e0 KH |
547 | static bool |
548 | drm_dequeue_event(struct drm_file *file_priv, | |
549 | size_t total, size_t max, struct drm_pending_event **out) | |
550 | { | |
551 | struct drm_device *dev = file_priv->minor->dev; | |
552 | struct drm_pending_event *e; | |
553 | unsigned long flags; | |
554 | bool ret = false; | |
555 | ||
556 | spin_lock_irqsave(&dev->event_lock, flags); | |
557 | ||
558 | *out = NULL; | |
559 | if (list_empty(&file_priv->event_list)) | |
560 | goto out; | |
561 | e = list_first_entry(&file_priv->event_list, | |
562 | struct drm_pending_event, link); | |
563 | if (e->event->length + total > max) | |
564 | goto out; | |
565 | ||
566 | file_priv->event_space += e->event->length; | |
567 | list_del(&e->link); | |
568 | *out = e; | |
569 | ret = true; | |
570 | ||
571 | out: | |
572 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
573 | return ret; | |
574 | } | |
575 | ||
576 | ssize_t drm_read(struct file *filp, char __user *buffer, | |
577 | size_t count, loff_t *offset) | |
578 | { | |
579 | struct drm_file *file_priv = filp->private_data; | |
580 | struct drm_pending_event *e; | |
581 | size_t total; | |
582 | ssize_t ret; | |
583 | ||
584 | ret = wait_event_interruptible(file_priv->event_wait, | |
585 | !list_empty(&file_priv->event_list)); | |
586 | if (ret < 0) | |
587 | return ret; | |
588 | ||
589 | total = 0; | |
590 | while (drm_dequeue_event(file_priv, total, count, &e)) { | |
591 | if (copy_to_user(buffer + total, | |
592 | e->event, e->event->length)) { | |
593 | total = -EFAULT; | |
594 | break; | |
595 | } | |
596 | ||
597 | total += e->event->length; | |
598 | e->destroy(e); | |
599 | } | |
600 | ||
601 | return total; | |
602 | } | |
603 | EXPORT_SYMBOL(drm_read); | |
604 | ||
1da177e4 LT |
605 | unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait) |
606 | { | |
c9a9c5e0 KH |
607 | struct drm_file *file_priv = filp->private_data; |
608 | unsigned int mask = 0; | |
609 | ||
610 | poll_wait(filp, &file_priv->event_wait, wait); | |
611 | ||
612 | if (!list_empty(&file_priv->event_list)) | |
613 | mask |= POLLIN | POLLRDNORM; | |
614 | ||
615 | return mask; | |
1da177e4 | 616 | } |
b5e89ed5 | 617 | EXPORT_SYMBOL(drm_poll); |