xref: /freebsd/sys/dev/drm2/drm_fops.c (revision 24bf3585ae49955b5d065afd9ae3b8aabe1f04a1)
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 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 /** @file drm_fops.c
36  * Support code for dealing with the file privates associated with each
37  * open of the DRM device.
38  */
39 
40 #include <dev/drm2/drmP.h>
41 
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)
45 {
46 	struct drm_file *priv;
47 	int retcode;
48 
49 	if (flags & O_EXCL)
50 		return EBUSY; /* No exclusive opens */
51 	dev->flags = flags;
52 
53 	DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
54 
55 	priv = malloc(sizeof(*priv), DRM_MEM_FILES, M_NOWAIT | M_ZERO);
56 	if (priv == NULL) {
57 		return ENOMEM;
58 	}
59 
60 	DRM_LOCK(dev);
61 	priv->dev		= dev;
62 	priv->uid		= p->td_ucred->cr_svuid;
63 	priv->pid		= p->td_proc->p_pid;
64 	priv->ioctl_count 	= 0;
65 
66 	/* for compatibility root is always authenticated */
67 	priv->authenticated	= DRM_SUSER(p);
68 
69 	INIT_LIST_HEAD(&priv->fbs);
70 	INIT_LIST_HEAD(&priv->event_list);
71 	priv->event_space = 4096; /* set aside 4k for event buffer */
72 
73 	if (dev->driver->driver_features & DRIVER_GEM)
74 		drm_gem_open(dev, priv);
75 
76 	if (dev->driver->open) {
77 		/* shared code returns -errno */
78 		retcode = -dev->driver->open(dev, priv);
79 		if (retcode != 0) {
80 			free(priv, DRM_MEM_FILES);
81 			DRM_UNLOCK(dev);
82 			return retcode;
83 		}
84 	}
85 
86 	/* first opener automatically becomes master */
87 	priv->master = TAILQ_EMPTY(&dev->files);
88 
89 	TAILQ_INSERT_TAIL(&dev->files, priv, link);
90 	DRM_UNLOCK(dev);
91 	kdev->si_drv1 = dev;
92 
93 	retcode = devfs_set_cdevpriv(priv, drm_close);
94 	if (retcode != 0)
95 		drm_close(priv);
96 
97 	return (retcode);
98 }
99 
100 static bool
101 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
102     struct uio *uio, struct drm_pending_event **out)
103 {
104 	struct drm_pending_event *e;
105 
106 	if (list_empty(&file_priv->event_list))
107 		return (false);
108 	e = list_first_entry(&file_priv->event_list,
109 	    struct drm_pending_event, link);
110 	if (e->event->length > uio->uio_resid)
111 		return (false);
112 
113 	file_priv->event_space += e->event->length;
114 	list_del(&e->link);
115 	*out = e;
116 	return (true);
117 }
118 
119 int
120 drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
121 {
122 	struct drm_file *file_priv;
123 	struct drm_device *dev;
124 	struct drm_pending_event *e;
125 	int error;
126 
127 	error = devfs_get_cdevpriv((void **)&file_priv);
128 	if (error != 0) {
129 		DRM_ERROR("can't find authenticator\n");
130 		return (EINVAL);
131 	}
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) {
136 			error = EAGAIN;
137 			goto out;
138 		}
139 		error = msleep(&file_priv->event_space, &dev->event_lock,
140 	           PCATCH, "drmrea", 0);
141 	       if (error != 0)
142 		       goto out;
143 	}
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);
149 		e->destroy(e);
150 		if (error != 0)
151 			return (error);
152 		mtx_lock(&dev->event_lock);
153 	}
154 out:
155 	mtx_unlock(&dev->event_lock);
156 	return (error);
157 }
158 
159 void
160 drm_event_wakeup(struct drm_pending_event *e)
161 {
162 	struct drm_file *file_priv;
163 	struct drm_device *dev;
164 
165 	file_priv = e->file_priv;
166 	dev = file_priv->dev;
167 	mtx_assert(&dev->event_lock, MA_OWNED);
168 
169 	wakeup(&file_priv->event_space);
170 	selwakeup(&file_priv->event_poll);
171 }
172 
173 int
174 drm_poll(struct cdev *kdev, int events, struct thread *td)
175 {
176 	struct drm_file *file_priv;
177 	struct drm_device *dev;
178 	int error, revents;
179 
180 	error = devfs_get_cdevpriv((void **)&file_priv);
181 	if (error != 0) {
182 		DRM_ERROR("can't find authenticator\n");
183 		return (EINVAL);
184 	}
185 	dev = drm_get_device_from_kdev(kdev);
186 
187 	revents = 0;
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);
193 		} else {
194 			revents |= events & (POLLIN | POLLRDNORM);
195 			CTR1(KTR_DRM, "drm_poll revents %x", revents);
196 		}
197 	}
198 	mtx_unlock(&dev->event_lock);
199 	return (revents);
200 }
201