xref: /freebsd/sys/dev/drm2/drm_fops.c (revision b652778e426d00b6a1df29bbd86869db86f36e25)
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 	retcode = devfs_set_cdevpriv(priv, drm_close);
61 	if (retcode != 0) {
62 		free(priv, DRM_MEM_FILES);
63 		return retcode;
64 	}
65 
66 	DRM_LOCK(dev);
67 	priv->dev		= dev;
68 	priv->uid		= p->td_ucred->cr_svuid;
69 	priv->pid		= p->td_proc->p_pid;
70 	priv->ioctl_count 	= 0;
71 
72 	/* for compatibility root is always authenticated */
73 	priv->authenticated	= DRM_SUSER(p);
74 
75 	INIT_LIST_HEAD(&priv->fbs);
76 	INIT_LIST_HEAD(&priv->event_list);
77 	priv->event_space = 4096; /* set aside 4k for event buffer */
78 
79 	if (dev->driver->driver_features & DRIVER_GEM)
80 		drm_gem_open(dev, priv);
81 
82 	if (dev->driver->open) {
83 		/* shared code returns -errno */
84 		retcode = -dev->driver->open(dev, priv);
85 		if (retcode != 0) {
86 			devfs_clear_cdevpriv();
87 			free(priv, DRM_MEM_FILES);
88 			DRM_UNLOCK(dev);
89 			return retcode;
90 		}
91 	}
92 
93 	/* first opener automatically becomes master */
94 	priv->master = TAILQ_EMPTY(&dev->files);
95 
96 	TAILQ_INSERT_TAIL(&dev->files, priv, link);
97 	DRM_UNLOCK(dev);
98 	kdev->si_drv1 = dev;
99 	return 0;
100 }
101 
102 static bool
103 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
104     struct uio *uio, struct drm_pending_event **out)
105 {
106 	struct drm_pending_event *e;
107 
108 	if (list_empty(&file_priv->event_list))
109 		return (false);
110 	e = list_first_entry(&file_priv->event_list,
111 	    struct drm_pending_event, link);
112 	if (e->event->length > uio->uio_resid)
113 		return (false);
114 
115 	file_priv->event_space += e->event->length;
116 	list_del(&e->link);
117 	*out = e;
118 	return (true);
119 }
120 
121 int
122 drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
123 {
124 	struct drm_file *file_priv;
125 	struct drm_device *dev;
126 	struct drm_pending_event *e;
127 	int error;
128 
129 	error = devfs_get_cdevpriv((void **)&file_priv);
130 	if (error != 0) {
131 		DRM_ERROR("can't find authenticator\n");
132 		return (EINVAL);
133 	}
134 	dev = drm_get_device_from_kdev(kdev);
135 	mtx_lock(&dev->event_lock);
136 	while (list_empty(&file_priv->event_list)) {
137 		if ((ioflag & O_NONBLOCK) != 0) {
138 			error = EAGAIN;
139 			goto out;
140 		}
141 		error = msleep(&file_priv->event_space, &dev->event_lock,
142 	           PCATCH, "drmrea", 0);
143 	       if (error != 0)
144 		       goto out;
145 	}
146 	while (drm_dequeue_event(dev, file_priv, uio, &e)) {
147 		mtx_unlock(&dev->event_lock);
148 		error = uiomove(e->event, e->event->length, uio);
149 		CTR3(KTR_DRM, "drm_event_dequeued %d %d %d", curproc->p_pid,
150 		    e->event->type, e->event->length);
151 		e->destroy(e);
152 		if (error != 0)
153 			return (error);
154 		mtx_lock(&dev->event_lock);
155 	}
156 out:
157 	mtx_unlock(&dev->event_lock);
158 	return (error);
159 }
160 
161 void
162 drm_event_wakeup(struct drm_pending_event *e)
163 {
164 	struct drm_file *file_priv;
165 	struct drm_device *dev;
166 
167 	file_priv = e->file_priv;
168 	dev = file_priv->dev;
169 	mtx_assert(&dev->event_lock, MA_OWNED);
170 
171 	wakeup(&file_priv->event_space);
172 	selwakeup(&file_priv->event_poll);
173 }
174 
175 int
176 drm_poll(struct cdev *kdev, int events, struct thread *td)
177 {
178 	struct drm_file *file_priv;
179 	struct drm_device *dev;
180 	int error, revents;
181 
182 	error = devfs_get_cdevpriv((void **)&file_priv);
183 	if (error != 0) {
184 		DRM_ERROR("can't find authenticator\n");
185 		return (EINVAL);
186 	}
187 	dev = drm_get_device_from_kdev(kdev);
188 
189 	revents = 0;
190 	mtx_lock(&dev->event_lock);
191 	if ((events & (POLLIN | POLLRDNORM)) != 0) {
192 		if (list_empty(&file_priv->event_list)) {
193 			CTR0(KTR_DRM, "drm_poll empty list");
194 			selrecord(td, &file_priv->event_poll);
195 		} else {
196 			revents |= events & (POLLIN | POLLRDNORM);
197 			CTR1(KTR_DRM, "drm_poll revents %x", revents);
198 		}
199 	}
200 	mtx_unlock(&dev->event_lock);
201 	return (revents);
202 }
203