e8141923b3ef761511d5091d760936d0b02635cd
[dragonfly.git] / sys / dev / drm / drm_fops.c
1 /*-
2  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Rickard E. (Rik) Faith <faith@valinux.com>
27  *    Daryll Strauss <daryll@valinux.com>
28  *    Gareth Hughes <gareth@valinux.com>
29  *
30  */
31
32 /** @file drm_fops.c
33  * Support code for dealing with the file privates associated with each
34  * open of the DRM device.
35  */
36
37 #include "dev/drm/drmP.h"
38
39 struct drm_file *drm_find_file_by_proc(struct drm_device *dev, DRM_STRUCTPROC *p)
40 {
41         uid_t uid = p->td_proc->p_ucred->cr_svuid;
42         pid_t pid = p->td_proc->p_pid;
43         struct drm_file *priv;
44
45         TAILQ_FOREACH(priv, &dev->files, link)
46                 if (priv->pid == pid && priv->uid == uid)
47                         return priv;
48         return NULL;
49 }
50
51 /* drm_open_helper is called whenever a process opens /dev/drm. */
52 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
53                     struct drm_device *dev)
54 {
55         struct drm_file *priv;
56         int m = minor(kdev);
57         int retcode;
58
59         if (flags & O_EXCL)
60                 return EBUSY; /* No exclusive opens */
61         dev->flags = flags;
62
63         DRM_DEBUG("pid = %d, minor = %d\n", DRM_CURRENTPID, m);
64         DRM_LOCK();
65         priv = drm_find_file_by_proc(dev, p);
66         if (priv) {
67                 priv->refs++;
68         } else {
69                 priv = malloc(sizeof(*priv), DRM_MEM_FILES, M_NOWAIT | M_ZERO);
70                 if (priv == NULL) {
71                         DRM_UNLOCK();
72                         return ENOMEM;
73                 }
74                 priv->uid               = p->td_proc->p_ucred->cr_svuid;
75                 priv->pid               = p->td_proc->p_pid;
76                 priv->refs              = 1;
77                 priv->minor             = m;
78                 priv->ioctl_count       = 0;
79
80                 /* for compatibility root is always authenticated */
81                 priv->authenticated     = DRM_SUSER(p);
82
83                 if (dev->driver->open) {
84                         /* shared code returns -errno */
85                         retcode = -dev->driver->open(dev, priv);
86                         if (retcode != 0) {
87                                 free(priv, DRM_MEM_FILES);
88                                 DRM_UNLOCK();
89                                 return retcode;
90                         }
91                 }
92
93                 /* first opener automatically becomes master */
94                 priv->master = TAILQ_EMPTY(&dev->files);
95
96                 TAILQ_INSERT_TAIL(&dev->files, priv, link);
97         }
98
99         DRM_UNLOCK();
100         kdev->si_drv1 = dev;
101         return 0;
102 }
103
104
105 /* The drm_read and drm_poll are stubs to prevent spurious errors
106  * on older X Servers (4.3.0 and earlier) */
107 int drm_read(struct dev_read_args *ap)
108 {
109         return 0;
110 }
111
112 int drm_poll(struct dev_poll_args *ap)
113 {
114         return 0;
115 }
116
117 static int
118 drmfilt(struct knote *kn, long hint)
119 {
120         return (0);
121 }
122
123 static void
124 drmfilt_detach(struct knote *kn) {}
125
126 static struct filterops drmfiltops =
127         { 1, NULL, drmfilt_detach, drmfilt };
128
129 int
130 drm_kqfilter(struct dev_kqfilter_args *ap)
131 {
132         struct knote *kn = ap->a_kn;
133
134         ap->a_result = 0;
135
136         switch (kn->kn_filter) {
137         case EVFILT_READ:
138         case EVFILT_WRITE:
139                 kn->kn_fop = &drmfiltops;
140                 break;
141         default:
142                 ap->a_result = EOPNOTSUPP;
143                 return (0);
144         }
145
146         return (0);
147 }