kernel: Cleanup <sys/uio.h> issues.
[dragonfly.git] / sys / dev / drm / drm_fops.c
1 /*
2  * \file drm_fops.c
3  * File operations for DRM
4  *
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 <sys/types.h>
38 #include <sys/uio.h>    /* must come first to avoid kfree() macros issues */
39 #include <drm/drmP.h>
40 #include <linux/poll.h>
41 #include <linux/slab.h>
42 #include <linux/module.h>
43 #include "drm_legacy.h"
44 #include "drm_internal.h"
45
46 #include <sys/devfs.h>
47
48 /* from BKL pushdown */
49 DEFINE_MUTEX(drm_global_mutex);
50
51 /**
52  * DOC: file operations
53  *
54  * Drivers must define the file operations structure that forms the DRM
55  * userspace API entry point, even though most of those operations are
56  * implemented in the DRM core. The mandatory functions are drm_open(),
57  * drm_read(), drm_ioctl() and drm_compat_ioctl if CONFIG_COMPAT is enabled.
58  * Drivers which implement private ioctls that require 32/64 bit compatibility
59  * support must provided their onw .compat_ioctl() handler that processes
60  * private ioctls and calls drm_compat_ioctl() for core ioctls.
61  *
62  * In addition drm_read() and drm_poll() provide support for DRM events. DRM
63  * events are a generic and extensible means to send asynchronous events to
64  * userspace through the file descriptor. They are used to send vblank event and
65  * page flip completions by the KMS API. But drivers can also use it for their
66  * own needs, e.g. to signal completion of rendering.
67  *
68  * The memory mapping implementation will vary depending on how the driver
69  * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
70  * function, modern drivers should use one of the provided memory-manager
71  * specific implementations. For GEM-based drivers this is drm_gem_mmap().
72  *
73  * No other file operations are supported by the DRM userspace API. Overall the
74  * following is an example #file_operations structure:
75  *
76  *     static const example_drm_fops = {
77  *             .owner = THIS_MODULE,
78  *             .open = drm_open,
79  *             .release = drm_release,
80  *             .unlocked_ioctl = drm_ioctl,
81  *     #ifdef CONFIG_COMPAT
82  *             .compat_ioctl = drm_compat_ioctl,
83  *     #endif
84  *             .poll = drm_poll,
85  *             .read = drm_read,
86  *             .llseek = no_llseek,
87  *             .mmap = drm_gem_mmap,
88  *     };
89  */
90
91 extern drm_pci_id_list_t *drm_find_description(int vendor, int device,
92     drm_pci_id_list_t *idlist);
93 extern devclass_t drm_devclass;
94
95 static int drm_setup(struct drm_device * dev)
96 {
97         int ret;
98
99         if (dev->driver->firstopen &&
100             !drm_core_check_feature(dev, DRIVER_MODESET)) {
101                 ret = dev->driver->firstopen(dev);
102                 if (ret != 0)
103                         return ret;
104         }
105
106         dev->buf_use = 0;
107
108         ret = drm_legacy_dma_setup(dev);
109         if (ret < 0)
110                 return ret;
111
112         init_waitqueue_head(&dev->lock.lock_queue);
113         if (!drm_core_check_feature(dev, DRIVER_MODESET))
114                 dev->irq_enabled = 0;
115         dev->context_flag = 0;
116         dev->last_context = 0;
117         dev->if_version = 0;
118
119         dev->buf_sigio = NULL;
120
121
122         DRM_DEBUG("\n");
123         return 0;
124 }
125
126 #define DRIVER_SOFTC(unit) \
127         ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
128
129 /**
130  * drm_open - open method for DRM file
131  * @inode: device inode
132  * @filp: file pointer.
133  *
134  * This function must be used by drivers as their .open() #file_operations
135  * method. It looks up the correct DRM device and instantiates all the per-file
136  * resources for it.
137  *
138  * RETURNS:
139  *
140  * 0 on success or negative errno value on falure.
141  */
142 int drm_open(struct dev_open_args *ap)
143 {
144         struct cdev *kdev = ap->a_head.a_dev;
145         int flags = ap->a_oflags;
146         int fmt = 0;
147         struct thread *p = curthread;
148         struct drm_device *dev;
149         int retcode;
150
151         dev = DRIVER_SOFTC(minor(kdev));
152         if (dev == NULL)
153                 return (ENXIO);
154
155         DRM_DEBUG("open_count = %d\n", dev->open_count);
156
157         retcode = drm_open_helper(kdev, flags, fmt, p, dev, ap->a_fp);
158
159         if (retcode == 0) {
160                 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
161                 DRM_LOCK(dev);
162                 device_busy(dev->dev->bsddev);
163                 if (!dev->open_count++)
164                         retcode = drm_setup(dev);
165                 DRM_UNLOCK(dev);
166         }
167
168         DRM_DEBUG("return %d\n", retcode);
169
170         return (retcode);
171 }
172 EXPORT_SYMBOL(drm_open);
173
174 /*
175  * Check whether DRI will run on this CPU.
176  *
177  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
178  */
179
180 /*
181  * drm_new_set_master - Allocate a new master object and become master for the
182  * associated master realm.
183  *
184  * @dev: The associated device.
185  * @fpriv: File private identifying the client.
186  *
187  * This function must be called with dev::struct_mutex held.
188  * Returns negative error code on failure. Zero on success.
189  */
190
191 /*
192  * Called whenever a process opens /dev/drm.
193  *
194  * \param filp file pointer.
195  * \param minor acquired minor-object.
196  * \return zero on success or a negative number on failure.
197  *
198  * Creates and initializes a drm_file structure for the file private data in \p
199  * filp and add it into the double linked list in \p dev.
200  */
201 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
202                     struct drm_device *dev, struct file *filp)
203 {
204         struct drm_file *priv;
205         int retcode;
206
207         if (flags & O_EXCL)
208                 return EBUSY; /* No exclusive opens */
209         dev->flags = flags;
210
211         DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
212
213         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
214         if (!priv)
215                 return -ENOMEM;
216
217         filp->private_data = priv;
218         priv->filp = filp;
219         priv->uid               = p->td_proc->p_ucred->cr_svuid;
220         priv->pid               = p->td_proc->p_pid;
221         priv->dev               = dev;
222
223         /* for compatibility root is always authenticated */
224         priv->authenticated = capable(CAP_SYS_ADMIN);
225         priv->lock_count = 0;
226
227         INIT_LIST_HEAD(&priv->lhead);
228         INIT_LIST_HEAD(&priv->fbs);
229         lockinit(&priv->fbs_lock, "dpfl", 0, LK_CANRECURSE);
230         INIT_LIST_HEAD(&priv->blobs);
231         INIT_LIST_HEAD(&priv->pending_event_list);
232         INIT_LIST_HEAD(&priv->event_list);
233         init_waitqueue_head(&priv->event_wait);
234         priv->event_space = 4096; /* set aside 4k for event buffer */
235
236         lockinit(&priv->event_read_lock, "dperl", 0, LK_CANRECURSE);
237
238         if (drm_core_check_feature(dev, DRIVER_GEM))
239                 drm_gem_open(dev, priv);
240
241         if (dev->driver->open) {
242                 /* shared code returns -errno */
243                 retcode = -dev->driver->open(dev, priv);
244                 if (retcode != 0) {
245                         kfree(priv);
246                         return retcode;
247                 }
248         }
249
250         /* first opener automatically becomes master */
251         mutex_lock(&dev->master_mutex);
252         priv->is_master = list_empty(&dev->filelist);
253         mutex_unlock(&dev->master_mutex);
254
255         mutex_lock(&dev->filelist_mutex);
256         list_add(&priv->lhead, &dev->filelist);
257         mutex_unlock(&dev->filelist_mutex);
258
259         kdev->si_drv1 = dev;
260         retcode = devfs_set_cdevpriv(filp, priv, &drm_cdevpriv_dtor);
261         if (retcode != 0)
262                 drm_cdevpriv_dtor(priv);
263
264         return retcode;
265 }
266
267 /*
268  * drm_legacy_dev_reinit
269  *
270  * Reinitializes a legacy/ums drm device in it's lastclose function.
271  */
272 static void drm_legacy_dev_reinit(struct drm_device *dev)
273 {
274         if (dev->irq_enabled)
275                 drm_irq_uninstall(dev);
276
277         mutex_lock(&dev->struct_mutex);
278
279         drm_legacy_agp_clear(dev);
280
281         drm_legacy_sg_cleanup(dev);
282 #if 0
283         drm_legacy_vma_flush(dev);
284 #endif
285         drm_legacy_dma_takedown(dev);
286
287         mutex_unlock(&dev->struct_mutex);
288
289         dev->sigdata.lock = NULL;
290
291         dev->context_flag = 0;
292         dev->last_context = 0;
293         dev->if_version = 0;
294
295         DRM_DEBUG("lastclose completed\n");
296 }
297
298 /*
299  * Take down the DRM device.
300  *
301  * \param dev DRM device structure.
302  *
303  * Frees every resource in \p dev.
304  *
305  * \sa drm_device
306  */
307 void drm_lastclose(struct drm_device * dev)
308 {
309         DRM_DEBUG("\n");
310
311         if (dev->driver->lastclose)
312                 dev->driver->lastclose(dev);
313         DRM_DEBUG("driver lastclose completed\n");
314
315         if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
316                 drm_irq_uninstall(dev);
317
318         mutex_lock(&dev->struct_mutex);
319
320         if (dev->unique) {
321                 kfree(dev->unique);
322                 dev->unique = NULL;
323                 dev->unique_len = 0;
324         }
325
326         drm_legacy_agp_clear(dev);
327
328         drm_legacy_sg_cleanup(dev);
329         drm_legacy_dma_takedown(dev);
330
331         if (dev->lock.hw_lock) {
332                 dev->lock.hw_lock = NULL; /* SHM removed */
333                 dev->lock.file_priv = NULL;
334                 wakeup(&dev->lock.lock_queue);
335         }
336
337         mutex_unlock(&dev->struct_mutex);
338
339         DRM_DEBUG("lastclose completed\n");
340
341         if (!drm_core_check_feature(dev, DRIVER_MODESET))
342                 drm_legacy_dev_reinit(dev);
343 }
344
345 /**
346  * drm_release - release method for DRM file
347  * @inode: device inode
348  * @filp: file pointer.
349  *
350  * This function must be used by drivers as their .release() #file_operations
351  * method. It frees any resources associated with the open file, and if this is
352  * the last open file for the DRM device also proceeds to call drm_lastclose().
353  *
354  * RETURNS:
355  *
356  * Always succeeds and returns 0.
357  */
358 //int drm_release(struct inode *inode, struct file *filp)
359 int drm_release(device_t kdev)
360 {
361 //      XXX: filp is needed in this function
362 #if 0
363         struct drm_file *file_priv = filp->private_data;
364 #endif
365         struct drm_device *dev = device_get_softc(kdev);
366         int i;
367
368         mutex_lock(&drm_global_mutex);
369
370 #if 0
371         if (dev->magicfree.next) {
372                 list_for_each_entry_safe(pt, next, &dev->magicfree, head) {
373                         list_del(&pt->head);
374                         drm_ht_remove_item(&dev->magiclist, &pt->hash_item);
375                         kfree(pt);
376                 }
377                 drm_ht_remove(&dev->magiclist);
378         }
379 #endif
380
381         /* ========================================================
382          * Begin inline drm_release
383          */
384
385         DRM_DEBUG("\n");
386
387         drm_sysctl_cleanup(dev);
388         if (dev->devnode != NULL)
389                 destroy_dev(dev->devnode);
390
391         if (drm_core_check_feature(dev, DRIVER_GEM))
392                 drm_gem_destroy(dev);
393
394         drm_vblank_cleanup(dev);
395
396         DRM_LOCK(dev);
397         drm_lastclose(dev);
398         DRM_UNLOCK(dev);
399
400         /* Clean up PCI resources allocated by drm_bufs.c.  We're not really
401          * worried about resource consumption while the DRM is inactive (between
402          * lastclose and firstopen or unload) because these aren't actually
403          * taking up KVA, just keeping the PCI resource allocated.
404          */
405         for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
406                 if (dev->pcir[i] == NULL)
407                         continue;
408                 bus_release_resource(dev->dev->bsddev, SYS_RES_MEMORY,
409                     dev->pcirid[i], dev->pcir[i]);
410                 dev->pcir[i] = NULL;
411         }
412
413         if (dev->agp) {
414                 kfree(dev->agp);
415                 dev->agp = NULL;
416         }
417
418         if (dev->driver->unload != NULL) {
419                 DRM_LOCK(dev);
420                 dev->driver->unload(dev);
421                 DRM_UNLOCK(dev);
422         }
423
424         if (pci_disable_busmaster(dev->dev->bsddev))
425                 DRM_ERROR("Request to disable bus-master failed.\n");
426
427         lockuninit(&dev->vbl_lock);
428         lockuninit(&dev->dev_lock);
429         lockuninit(&dev->event_lock);
430         lockuninit(&dev->struct_mutex);
431
432         /* ========================================================
433          * End inline drm_release
434          */
435
436         mutex_unlock(&drm_global_mutex);
437
438         return (0);
439 }
440 EXPORT_SYMBOL(drm_release);
441
442 /**
443  * drm_read - read method for DRM file
444  * @filp: file pointer
445  * @buffer: userspace destination pointer for the read
446  * @count: count in bytes to read
447  * @offset: offset to read
448  *
449  * This function must be used by drivers as their .read() #file_operations
450  * method iff they use DRM events for asynchronous signalling to userspace.
451  * Since events are used by the KMS API for vblank and page flip completion this
452  * means all modern display drivers must use it.
453  *
454  * @offset is ignore, DRM events are read like a pipe. Therefore drivers also
455  * must set the .llseek() #file_operation to no_llseek(). Polling support is
456  * provided by drm_poll().
457  *
458  * This function will only ever read a full event. Therefore userspace must
459  * supply a big enough buffer to fit any event to ensure forward progress. Since
460  * the maximum event space is currently 4K it's recommended to just use that for
461  * safety.
462  *
463  * RETURNS:
464  *
465  * Number of bytes read (always aligned to full events, and can be 0) or a
466  * negative error code on failure.
467  */
468 /*
469 ssize_t drm_read(struct file *filp, char __user *buffer,
470                  size_t count, loff_t *offset)
471 */
472 int drm_read(struct dev_read_args *ap)
473 {
474         struct file *filp = ap->a_fp;
475         struct cdev *kdev = ap->a_head.a_dev;
476         struct uio *uio = ap->a_uio;
477         size_t count = uio->uio_resid;
478         struct drm_file *file_priv = filp->private_data;
479         struct drm_device *dev = drm_get_device_from_kdev(kdev);
480         int ret = 0;    /* drm_read() returns int in DragonFly */
481
482         ret = mutex_lock_interruptible(&file_priv->event_read_lock);
483         if (ret)
484                 return ret;
485
486         for (;;) {
487                 struct drm_pending_event *e = NULL;
488
489                 spin_lock_irq(&dev->event_lock);
490                 if (!list_empty(&file_priv->event_list)) {
491                         e = list_first_entry(&file_priv->event_list,
492                                         struct drm_pending_event, link);
493                         file_priv->event_space += e->event->length;
494                         list_del(&e->link);
495                 }
496                 spin_unlock_irq(&dev->event_lock);
497
498                 if (e == NULL) {
499                         if (ret) {
500                                 ret = 0;        /* DragonFly expects a zero return value on success */
501                                 break;
502                         }
503
504                         if (filp->f_flag & O_NONBLOCK) {
505                                 ret = -EAGAIN;
506                                 break;
507                         }
508
509                         mutex_unlock(&file_priv->event_read_lock);
510                         ret = wait_event_interruptible(file_priv->event_wait,
511                                                        !list_empty(&file_priv->event_list));
512                         if (ret >= 0)
513                                 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
514                         if (ret)
515                                 return ret;
516                 } else {
517                         unsigned length = e->event->length;
518
519                         if (length > count - ret) {
520 put_back_event:
521                                 spin_lock_irq(&dev->event_lock);
522                                 file_priv->event_space -= length;
523                                 list_add(&e->link, &file_priv->event_list);
524                                 spin_unlock_irq(&dev->event_lock);
525                                 break;
526                         }
527
528                         if (uiomove((caddr_t)e->event, length, uio)) {
529                                 if (ret == 0)
530                                         ret = -EFAULT;
531                                 goto put_back_event;
532                         }
533
534                         ret += length;
535                         e->destroy(e);
536                 }
537         }
538         mutex_unlock(&file_priv->event_read_lock);
539
540         return ret;
541 }
542 EXPORT_SYMBOL(drm_read);
543
544 /**
545  * drm_poll - poll method for DRM file
546  * @filp: file pointer
547  * @wait: poll waiter table
548  *
549  * This function must be used by drivers as their .read() #file_operations
550  * method iff they use DRM events for asynchronous signalling to userspace.
551  * Since events are used by the KMS API for vblank and page flip completion this
552  * means all modern display drivers must use it.
553  *
554  * See also drm_read().
555  *
556  * RETURNS:
557  *
558  * Mask of POLL flags indicating the current status of the file.
559  */
560
561 static int
562 drmfilt(struct knote *kn, long hint)
563 {
564         struct drm_file *file_priv = (struct drm_file *)kn->kn_hook;
565         int ready = 0;
566
567 //      poll_wait(filp, &file_priv->event_wait, wait);
568
569         if (!list_empty(&file_priv->event_list))
570                 ready = 1;
571
572         return (ready);
573 }
574
575 static void
576 drmfilt_detach(struct knote *kn)
577 {
578         struct drm_file *file_priv;
579         struct drm_device *dev;
580         struct klist *klist;
581
582         file_priv = (struct drm_file *)kn->kn_hook;
583         dev = file_priv->dev;
584
585         klist = &file_priv->dkq.ki_note;
586         knote_remove(klist, kn);
587 }
588
589 static struct filterops drmfiltops =
590         { FILTEROP_MPSAFE | FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
591
592 int
593 drm_kqfilter(struct dev_kqfilter_args *ap)
594 {
595         struct file *filp = ap->a_fp;
596         struct drm_file *file_priv = filp->private_data;
597         struct knote *kn = ap->a_kn;
598         struct klist *klist;
599
600         ap->a_result = 0;
601
602         switch (kn->kn_filter) {
603         case EVFILT_READ:
604         case EVFILT_WRITE:
605                 kn->kn_fop = &drmfiltops;
606                 kn->kn_hook = (caddr_t)file_priv;
607                 break;
608         default:
609                 ap->a_result = EOPNOTSUPP;
610                 return (0);
611         }
612
613         klist = &file_priv->dkq.ki_note;
614         knote_insert(klist, kn);
615
616         return (0);
617 }
618
619 #ifdef __DragonFly__
620 /*
621  * The Linux layer version of kfree() is a macro and can't be called
622  * directly via a function pointer
623  */
624 static void
625 drm_event_destroy(struct drm_pending_event *e)
626 {
627         kfree(e);
628 }
629 #endif
630
631 /**
632  * drm_event_reserve_init_locked - init a DRM event and reserve space for it
633  * @dev: DRM device
634  * @file_priv: DRM file private data
635  * @p: tracking structure for the pending event
636  * @e: actual event data to deliver to userspace
637  *
638  * This function prepares the passed in event for eventual delivery. If the event
639  * doesn't get delivered (because the IOCTL fails later on, before queuing up
640  * anything) then the even must be cancelled and freed using
641  * drm_event_cancel_free(). Successfully initialized events should be sent out
642  * using drm_send_event() or drm_send_event_locked() to signal completion of the
643  * asynchronous event to userspace.
644  *
645  * If callers embedded @p into a larger structure it must be allocated with
646  * kmalloc and @p must be the first member element.
647  *
648  * This is the locked version of drm_event_reserve_init() for callers which
649  * already hold dev->event_lock.
650  *
651  * RETURNS:
652  *
653  * 0 on success or a negative error code on failure.
654  */
655 int drm_event_reserve_init_locked(struct drm_device *dev,
656                                   struct drm_file *file_priv,
657                                   struct drm_pending_event *p,
658                                   struct drm_event *e)
659 {
660         if (file_priv->event_space < e->length)
661                 return -ENOMEM;
662
663         file_priv->event_space -= e->length;
664
665         p->event = e;
666         list_add(&p->pending_link, &file_priv->pending_event_list);
667         p->file_priv = file_priv;
668
669         /* we *could* pass this in as arg, but everyone uses kfree: */
670 #ifdef __DragonFly__
671         p->destroy = drm_event_destroy;
672 #else
673         p->destroy = (void (*) (struct drm_pending_event *)) kfree;
674 #endif
675
676         return 0;
677 }
678 EXPORT_SYMBOL(drm_event_reserve_init_locked);
679
680 /**
681  * drm_event_reserve_init - init a DRM event and reserve space for it
682  * @dev: DRM device
683  * @file_priv: DRM file private data
684  * @p: tracking structure for the pending event
685  * @e: actual event data to deliver to userspace
686  *
687  * This function prepares the passed in event for eventual delivery. If the event
688  * doesn't get delivered (because the IOCTL fails later on, before queuing up
689  * anything) then the even must be cancelled and freed using
690  * drm_event_cancel_free(). Successfully initialized events should be sent out
691  * using drm_send_event() or drm_send_event_locked() to signal completion of the
692  * asynchronous event to userspace.
693  *
694  * If callers embedded @p into a larger structure it must be allocated with
695  * kmalloc and @p must be the first member element.
696  *
697  * Callers which already hold dev->event_lock should use
698  * drm_event_reserve_init() instead.
699  *
700  * RETURNS:
701  *
702  * 0 on success or a negative error code on failure.
703  */
704 int drm_event_reserve_init(struct drm_device *dev,
705                            struct drm_file *file_priv,
706                            struct drm_pending_event *p,
707                            struct drm_event *e)
708 {
709         unsigned long flags;
710         int ret;
711
712         spin_lock_irqsave(&dev->event_lock, flags);
713         ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
714         spin_unlock_irqrestore(&dev->event_lock, flags);
715
716         return ret;
717 }
718 EXPORT_SYMBOL(drm_event_reserve_init);
719
720 /**
721  * drm_event_cancel_free - free a DRM event and release it's space
722  * @dev: DRM device
723  * @p: tracking structure for the pending event
724  *
725  * This function frees the event @p initialized with drm_event_reserve_init()
726  * and releases any allocated space.
727  */
728 void drm_event_cancel_free(struct drm_device *dev,
729                            struct drm_pending_event *p)
730 {
731         unsigned long flags;
732         spin_lock_irqsave(&dev->event_lock, flags);
733         if (p->file_priv) {
734                 p->file_priv->event_space += p->event->length;
735                 list_del(&p->pending_link);
736         }
737         spin_unlock_irqrestore(&dev->event_lock, flags);
738         p->destroy(p);
739 }
740 EXPORT_SYMBOL(drm_event_cancel_free);
741
742 /**
743  * drm_send_event_locked - send DRM event to file descriptor
744  * @dev: DRM device
745  * @e: DRM event to deliver
746  *
747  * This function sends the event @e, initialized with drm_event_reserve_init(),
748  * to its associated userspace DRM file. Callers must already hold
749  * dev->event_lock, see drm_send_event() for the unlocked version.
750  */
751 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
752 {
753         assert_spin_locked(&dev->event_lock);
754
755         if (!e->file_priv) {
756                 e->destroy(e);
757                 return;
758         }
759
760         list_del(&e->pending_link);
761         list_add_tail(&e->link,
762                       &e->file_priv->event_list);
763         wake_up_interruptible(&e->file_priv->event_wait);
764 #ifdef __DragonFly__
765         KNOTE(&e->file_priv->dkq.ki_note, 0);
766 #endif
767
768 }
769 EXPORT_SYMBOL(drm_send_event_locked);
770
771 /**
772  * drm_send_event - send DRM event to file descriptor
773  * @dev: DRM device
774  * @e: DRM event to deliver
775  *
776  * This function sends the event @e, initialized with drm_event_reserve_init(),
777  * to its associated userspace DRM file. This function acquires dev->event_lock,
778  * see drm_send_event_locked() for callers which already hold this lock.
779  *
780  * Note that the core will take care of unlinking and disarming events when the
781  * corresponding DRM file is closed. Drivers need not worry about whether the
782  * DRM file for this event still exists and can call this function upon
783  * completion of the asynchronous work unconditionally.
784  */
785 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
786 {
787         unsigned long irqflags;
788
789         spin_lock_irqsave(&dev->event_lock, irqflags);
790         drm_send_event_locked(dev, e);
791         spin_unlock_irqrestore(&dev->event_lock, irqflags);
792 }
793 EXPORT_SYMBOL(drm_send_event);