xref: /titanic_41/usr/src/uts/common/io/drm/drm_fops.c (revision c0c79a3f09914f35651895ffc111883455b7f62d)
1 /*
2  * Copyright 2006 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 *p)
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 /* drm_open_helper is called whenever a process opens /dev/drm. */
62 /*ARGSUSED*/
63 int
64 drm_open_helper(drm_softstate_t *dev, int flags, int otyp, cred_t *credp)
65 {
66 	drm_file_t   *priv;
67 	pid_t pid;
68 	int retcode;
69 
70 	if (flags & FEXCL)
71 		return (DRM_ERR(EBUSY)); /* No exclusive opens */
72 	dev->flags = flags;
73 
74 	pid = ddi_get_pid();
75 	DRM_DEBUG("drm_open_helper :pid = %d", pid);
76 
77 	DRM_LOCK();
78 	priv = drm_find_file_by_proc(dev, credp);
79 	if (priv) {
80 		priv->refs++;
81 	} else {
82 		priv = drm_alloc(sizeof (*priv), DRM_MEM_FILES);
83 		if (priv == NULL) {
84 			DRM_UNLOCK();
85 			return (DRM_ERR(ENOMEM));
86 		}
87 		bzero(priv, sizeof (*priv));
88 
89 		priv->uid		= crgetuid(credp);
90 		priv->pid		= pid;
91 
92 		priv->refs		= 1;
93 		priv->minor		= 5;	/* just for hack */
94 		priv->ioctl_count 	= 0;
95 
96 		/* for compatibility root is always authenticated */
97 		priv->authenticated	= DRM_SUSER(credp);
98 
99 		if (dev->open) {
100 			retcode = dev->open(dev, priv);
101 			if (retcode != 0) {
102 				drm_free(priv, sizeof (*priv), DRM_MEM_FILES);
103 				DRM_UNLOCK();
104 				return (retcode);
105 			}
106 		}
107 
108 		/* first opener automatically becomes master */
109 		priv->master = TAILQ_EMPTY(&dev->files);
110 
111 		TAILQ_INSERT_TAIL(&dev->files, priv, link);
112 	}
113 	DRM_UNLOCK();
114 	return (0);
115 }
116