xref: /titanic_44/usr/src/uts/common/io/drm/drm_fops.c (revision 49b225e1cfa7bbf7738d4df0a03f18e3283426eb)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /* BEGIN CSTYLED */
7 
8 /* drm_fops.h -- File operations for DRM -*- linux-c -*-
9  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
10  */
11 /*-
12  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14  * All Rights Reserved.
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files (the "Software"),
18  * to deal in the Software without restriction, including without limitation
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20  * and/or sell copies of the Software, and to permit persons to whom the
21  * Software is furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice (including the next
24  * paragraph) shall be included in all copies or substantial portions of the
25  * Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  *
35  * Authors:
36  *    Rickard E. (Rik) Faith <faith@valinux.com>
37  *    Daryll Strauss <daryll@valinux.com>
38  *    Gareth Hughes <gareth@valinux.com>
39  *
40  */
41 
42 /* END CSTYLED */
43 
44 #pragma ident	"%Z%%M%	%I%	%E% SMI"
45 
46 #include "drmP.h"
47 
48 /*ARGSUSED*/
49 drm_file_t *
50 drm_find_file_by_proc(drm_device_t *dev, cred_t *credp)
51 {
52 	pid_t pid = ddi_get_pid();
53 	drm_file_t *priv;
54 
55 	TAILQ_FOREACH(priv, &dev->files, link)
56 	if (priv->pid == pid)
57 		return (priv);
58 	return (NULL);
59 }
60 
61 
62 drm_cminor_t *
63 drm_find_file_by_minor(drm_device_t *dev, int minor)
64 {
65 	drm_cminor_t	*mp;
66 
67 	TAILQ_FOREACH(mp, &dev->minordevs, link) {
68 		if (mp->minor == minor)
69 			return (mp);
70 	}
71 	return (NULL);
72 }
73 
74 /* drm_open_helper is called whenever a process opens /dev/drm. */
75 /*ARGSUSED*/
76 int
77 drm_open_helper(drm_device_t *dev, drm_cminor_t *mp, int flags,
78     int otyp, cred_t *credp)
79 {
80 	drm_file_t   *priv;
81 	pid_t pid;
82 	int retcode;
83 
84 	if (flags & FEXCL)
85 		return (EBUSY); /* No exclusive opens */
86 	dev->flags = flags;
87 
88 	pid = ddi_get_pid();
89 	DRM_DEBUG("drm_open_helper :pid = %d", pid);
90 
91 	DRM_LOCK();
92 	priv = drm_find_file_by_proc(dev, credp);
93 	if (priv) {
94 		priv->refs++;
95 	} else {
96 		priv = drm_alloc(sizeof (*priv), DRM_MEM_FILES);
97 		if (priv == NULL) {
98 			DRM_UNLOCK();
99 			return (ENOMEM);
100 		}
101 		bzero(priv, sizeof (*priv));
102 
103 		priv->uid		= crgetsuid(credp);
104 		priv->pid		= pid;
105 
106 		priv->refs		= 1;
107 		priv->minor		= 5;	/* just for hack */
108 		priv->ioctl_count 	= 0;
109 
110 		/* for compatibility root is always authenticated */
111 		priv->authenticated	= DRM_SUSER(credp);
112 
113 		if (dev->driver->open) {
114 			retcode = dev->driver->open(dev, priv);
115 			if (retcode != 0) {
116 				drm_free(priv, sizeof (*priv), DRM_MEM_FILES);
117 				DRM_UNLOCK();
118 				return (retcode);
119 			}
120 		}
121 
122 		/* first opener automatically becomes master */
123 		priv->master = TAILQ_EMPTY(&dev->files);
124 		TAILQ_INSERT_TAIL(&dev->files, priv, link);
125 	}
126 	mp->fpriv = priv;
127 	DRM_UNLOCK();
128 	return (0);
129 }
130