Merge from vendor branch OPENSSL:
[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  * $DragonFly: src/sys/dev/drm/drm_fops.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
31  */
32
33 /** @file drm_fops.c
34  * Support code for dealing with the file privates associated with each
35  * open of the DRM device.
36  */
37
38 #include "drmP.h"
39
40 drm_file_t *drm_find_file_by_proc(drm_device_t *dev, DRM_STRUCTPROC *p)
41 {
42 #if __FreeBSD_version >= 500021
43         uid_t uid = p->td_ucred->cr_svuid;
44         pid_t pid = p->td_proc->p_pid;
45 #elif defined(__DragonFly__)
46         uid_t uid = p->td_proc->p_ucred->cr_svuid;
47         pid_t pid = p->td_proc->p_pid;
48 #else
49         uid_t uid = p->p_cred->p_svuid;
50         pid_t pid = p->p_pid;
51 #endif
52         drm_file_t *priv;
53
54         DRM_SPINLOCK_ASSERT(&dev->dev_lock);
55
56         TAILQ_FOREACH(priv, &dev->files, link)
57                 if (priv->pid == pid && priv->uid == uid)
58                         return priv;
59         return NULL;
60 }
61
62 /* drm_open_helper is called whenever a process opens /dev/drm. */
63 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
64                     drm_device_t *dev)
65 {
66         int          m = minor(kdev);
67         drm_file_t   *priv;
68         int retcode;
69
70         if (flags & O_EXCL)
71                 return EBUSY; /* No exclusive opens */
72         dev->flags = flags;
73
74         DRM_DEBUG("pid = %d, minor = %d\n", DRM_CURRENTPID, m);
75
76         DRM_LOCK();
77         priv = drm_find_file_by_proc(dev, p);
78         if (priv) {
79                 priv->refs++;
80         } else {
81                 priv = malloc(sizeof(*priv), M_DRM, M_NOWAIT | M_ZERO);
82                 if (priv == NULL) {
83                         DRM_UNLOCK();
84                         return ENOMEM;
85                 }
86 #if __FreeBSD_version >= 500000
87                 priv->uid               = p->td_ucred->cr_svuid;
88                 priv->pid               = p->td_proc->p_pid;
89 #elif defined(__DragonFly__)
90                 priv->uid               = p->td_proc->p_ucred->cr_svuid;
91                 priv->pid               = p->td_proc->p_pid;
92 #else
93                 priv->uid               = p->p_cred->p_svuid;
94                 priv->pid               = p->p_pid;
95 #endif
96
97                 priv->refs              = 1;
98                 priv->minor             = m;
99                 priv->ioctl_count       = 0;
100
101                 /* for compatibility root is always authenticated */
102                 priv->authenticated     = DRM_SUSER(p);
103
104                 if (dev->driver.open) {
105                         /* shared code returns -errno */
106                         retcode = -dev->driver.open(dev, priv);
107                         if (retcode != 0) {
108                                 free(priv, M_DRM);
109                                 DRM_UNLOCK();
110                                 return retcode;
111                         }
112                 }
113
114                 /* first opener automatically becomes master */
115                 priv->master = TAILQ_EMPTY(&dev->files);
116
117                 TAILQ_INSERT_TAIL(&dev->files, priv, link);
118         }
119         DRM_UNLOCK();
120 #if defined(__FreeBSD__) || defined(__DragonFly__)
121         kdev->si_drv1 = dev;
122 #endif
123         return 0;
124 }
125
126
127 /* The drm_read and drm_poll are stubs to prevent spurious errors
128  * on older X Servers (4.3.0 and earlier) */
129
130 #ifdef __DragonFly__
131 int drm_read(struct dev_read_args *ap)
132 {
133         return (0);
134 }
135
136 int drm_poll(struct dev_poll_args *ap)
137 {
138         return (0);
139 }
140 #else
141 int drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
142 {
143         return 0;
144 }
145
146 int drm_poll(struct cdev *kdev, int events, DRM_STRUCTPROC *p)
147 {
148         return 0;
149 }
150 #endif