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>
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * Support code for dealing with the file privates associated with each
37 * open of the DRM device.
40 #include <dev/drm2/drmP.h>
42 /* drm_open_helper is called whenever a process opens /dev/drm. */
43 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
44 struct drm_device *dev)
46 struct drm_file *priv;
50 return EBUSY; /* No exclusive opens */
53 DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
55 priv = malloc(sizeof(*priv), DRM_MEM_FILES, M_NOWAIT | M_ZERO);
62 priv->uid = p->td_ucred->cr_svuid;
63 priv->pid = p->td_proc->p_pid;
64 priv->ioctl_count = 0;
66 /* for compatibility root is always authenticated */
67 priv->authenticated = DRM_SUSER(p);
69 INIT_LIST_HEAD(&priv->fbs);
70 INIT_LIST_HEAD(&priv->event_list);
71 priv->event_space = 4096; /* set aside 4k for event buffer */
73 if (dev->driver->driver_features & DRIVER_GEM)
74 drm_gem_open(dev, priv);
76 if (dev->driver->open) {
77 /* shared code returns -errno */
78 retcode = -dev->driver->open(dev, priv);
80 free(priv, DRM_MEM_FILES);
86 /* first opener automatically becomes master */
87 priv->master = TAILQ_EMPTY(&dev->files);
89 TAILQ_INSERT_TAIL(&dev->files, priv, link);
93 retcode = devfs_set_cdevpriv(priv, drm_close);
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 mtx_lock(&dev->event_lock);
134 while (list_empty(&file_priv->event_list)) {
135 if ((ioflag & O_NONBLOCK) != 0) {
139 error = msleep(&file_priv->event_space, &dev->event_lock,
140 PCATCH, "drmrea", 0);
144 while (drm_dequeue_event(dev, file_priv, uio, &e)) {
145 mtx_unlock(&dev->event_lock);
146 error = uiomove(e->event, e->event->length, uio);
147 CTR3(KTR_DRM, "drm_event_dequeued %d %d %d", curproc->p_pid,
148 e->event->type, e->event->length);
152 mtx_lock(&dev->event_lock);
155 mtx_unlock(&dev->event_lock);
160 drm_event_wakeup(struct drm_pending_event *e)
162 struct drm_file *file_priv;
163 struct drm_device *dev;
165 file_priv = e->file_priv;
166 dev = file_priv->dev;
167 mtx_assert(&dev->event_lock, MA_OWNED);
169 wakeup(&file_priv->event_space);
170 selwakeup(&file_priv->event_poll);
174 drm_poll(struct cdev *kdev, int events, struct thread *td)
176 struct drm_file *file_priv;
177 struct drm_device *dev;
180 error = devfs_get_cdevpriv((void **)&file_priv);
182 DRM_ERROR("can't find authenticator\n");
185 dev = drm_get_device_from_kdev(kdev);
188 mtx_lock(&dev->event_lock);
189 if ((events & (POLLIN | POLLRDNORM)) != 0) {
190 if (list_empty(&file_priv->event_list)) {
191 CTR0(KTR_DRM, "drm_poll empty list");
192 selrecord(td, &file_priv->event_poll);
194 revents |= events & (POLLIN | POLLRDNORM);
195 CTR1(KTR_DRM, "drm_poll revents %x", revents);
198 mtx_unlock(&dev->event_lock);