44c2b8de60b72c4baf4616a1ae3232c1e51886cc
[profile/ivi/libdrm.git] / bsd-core / drm_fops.c
1 /* drm_fops.h -- File operations for DRM -*- linux-c -*-
2  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Authors:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *    Daryll Strauss <daryll@valinux.com>
30  *    Gareth Hughes <gareth@valinux.com>
31  *
32  */
33
34 #include "drmP.h"
35
36 drm_file_t *drm_find_file_by_proc(drm_device_t *dev, DRM_STRUCTPROC *p)
37 {
38 #if __FreeBSD_version >= 500021
39         uid_t uid = p->td_ucred->cr_svuid;
40         pid_t pid = p->td_proc->p_pid;
41 #else
42         uid_t uid = p->p_cred->p_svuid;
43         pid_t pid = p->p_pid;
44 #endif
45         drm_file_t *priv;
46
47         DRM_SPINLOCK_ASSERT(&dev->dev_lock);
48
49         TAILQ_FOREACH(priv, &dev->files, link)
50                 if (priv->pid == pid && priv->uid == uid)
51                         return priv;
52         return NULL;
53 }
54
55 /* drm_open_helper is called whenever a process opens /dev/drm. */
56 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
57                     drm_device_t *dev)
58 {
59         int          m = minor(kdev);
60         drm_file_t   *priv;
61         int retcode;
62
63         if (flags & O_EXCL)
64                 return EBUSY; /* No exclusive opens */
65         dev->flags = flags;
66
67         DRM_DEBUG("pid = %d, minor = %d\n", DRM_CURRENTPID, m);
68
69         DRM_LOCK();
70         priv = drm_find_file_by_proc(dev, p);
71         if (priv) {
72                 priv->refs++;
73         } else {
74                 priv = malloc(sizeof(*priv), M_DRM, M_NOWAIT | M_ZERO);
75                 if (priv == NULL) {
76                         DRM_UNLOCK();
77                         return DRM_ERR(ENOMEM);
78                 }
79 #if __FreeBSD_version >= 500000
80                 priv->uid               = p->td_ucred->cr_svuid;
81                 priv->pid               = p->td_proc->p_pid;
82 #else
83                 priv->uid               = p->p_cred->p_svuid;
84                 priv->pid               = p->p_pid;
85 #endif
86
87                 priv->refs              = 1;
88                 priv->minor             = m;
89                 priv->ioctl_count       = 0;
90                 priv->authenticated     = !DRM_SUSER(p);
91
92                 if (dev->open_helper) {
93                         retcode = dev->open_helper(dev, priv);
94                         if (retcode != 0) {
95                                 free(priv, M_DRM);
96                                 DRM_UNLOCK();
97                                 return retcode;
98                         }
99                 }
100
101                 TAILQ_INSERT_TAIL(&dev->files, priv, link);
102         }
103         DRM_UNLOCK();
104 #ifdef __FreeBSD__
105         kdev->si_drv1 = dev;
106 #endif
107         return 0;
108 }
109
110
111 /* The drm_read and drm_poll are stubs to prevent spurious errors
112  * on older X Servers (4.3.0 and earlier) */
113
114 int drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
115 {
116         return 0;
117 }
118
119 int drm_poll(struct cdev *kdev, int events, DRM_STRUCTPROC *p)
120 {
121         return 0;
122 }