2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Daryll Strauss <daryll@valinux.com>
28 * Gareth Hughes <gareth@valinux.com>
30 * $FreeBSD: src/sys/dev/drm2/drm_fops.c,v 1.1 2012/05/22 11:07:44 kib Exp $
34 * Support code for dealing with the file privates associated with each
35 * open of the DRM device.
38 #include <dev/drm2/drmP.h>
40 /* drm_open_helper is called whenever a process opens /dev/drm. */
41 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
42 struct drm_device *dev)
44 struct drm_file *priv;
48 return EBUSY; /* No exclusive opens */
51 DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
53 priv = kmalloc(sizeof(*priv), DRM_MEM_FILES, M_NOWAIT | M_ZERO);
58 retcode = devfs_set_cdevpriv(priv, drm_close);
60 kfree(priv, DRM_MEM_FILES);
66 priv->uid = p->td_ucred->cr_svuid;
67 priv->pid = p->td_proc->p_pid;
68 priv->ioctl_count = 0;
70 /* for compatibility root is always authenticated */
71 priv->authenticated = DRM_SUSER(p);
73 INIT_LIST_HEAD(&priv->fbs);
74 INIT_LIST_HEAD(&priv->event_list);
75 priv->event_space = 4096; /* set aside 4k for event buffer */
77 if (dev->driver->driver_features & DRIVER_GEM)
78 drm_gem_open(dev, priv);
80 if (dev->driver->open) {
81 /* shared code returns -errno */
82 retcode = -dev->driver->open(dev, priv);
84 devfs_clear_cdevpriv();
85 kfree(priv, DRM_MEM_FILES);
91 /* first opener automatically becomes master */
92 priv->master = TAILQ_EMPTY(&dev->files);
94 TAILQ_INSERT_TAIL(&dev->files, priv, link);
101 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
102 struct uio *uio, struct drm_pending_event **out)
104 struct drm_pending_event *e;
106 if (list_empty(&file_priv->event_list))
108 e = list_first_entry(&file_priv->event_list,
109 struct drm_pending_event, link);
110 if (e->event->length > uio->uio_resid)
113 file_priv->event_space += e->event->length;
120 drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
122 struct drm_file *file_priv;
123 struct drm_device *dev;
124 struct drm_pending_event *e;
127 error = devfs_get_cdevpriv((void **)&file_priv);
129 DRM_ERROR("can't find authenticator\n");
132 dev = drm_get_device_from_kdev(kdev);
133 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
134 while (list_empty(&file_priv->event_list)) {
135 if ((ioflag & O_NONBLOCK) != 0) {
139 error = lksleep(&file_priv->event_space, &dev->event_lock,
140 PCATCH, "drmrea", 0);
144 while (drm_dequeue_event(dev, file_priv, uio, &e)) {
145 lockmgr(&dev->event_lock, LK_RELEASE);
146 error = uiomove(e->event, e->event->length, uio);
150 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
153 lockmgr(&dev->event_lock, LK_RELEASE);
158 drm_event_wakeup(struct drm_pending_event *e)
160 struct drm_file *file_priv;
161 struct drm_device *dev;
163 file_priv = e->file_priv;
164 dev = file_priv->dev;
165 KKASSERT(lockstatus(&dev->event_lock, curthread) != 0);
167 wakeup(&file_priv->event_space);
168 selwakeup(&file_priv->event_poll);
172 drm_poll(struct cdev *kdev, int events, struct thread *td)
174 struct drm_file *file_priv;
175 struct drm_device *dev;
178 error = devfs_get_cdevpriv((void **)&file_priv);
180 DRM_ERROR("can't find authenticator\n");
183 dev = drm_get_device_from_kdev(kdev);
186 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
187 if ((events & (POLLIN | POLLRDNORM)) != 0) {
188 if (list_empty(&file_priv->event_list)) {
189 selrecord(td, &file_priv->event_poll);
191 revents |= events & (POLLIN | POLLRDNORM);
194 lockmgr(&dev->event_lock, LK_RELEASE);