1 /* drm_stub.h -- -*- linux-c -*-
2  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
3  *
4  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  *
26  * Authors:
27  *    Rickard E. (Rik) Faith <faith@valinux.com>
28  *
29  */
30 
31 #include "drmP.h"
32 
33 #define DRM_STUB_MAXCARDS 16	/* Enough for one machine */
34 
35 static struct drm_stub_list {
36 	const char             *name;
37 	struct file_operations *fops;
38 	struct proc_dir_entry  *dev_root;
39 } *DRM(stub_list);
40 
41 static struct proc_dir_entry *DRM(stub_root);
42 
43 static struct drm_stub_info {
44 	int (*info_register)(const char *name, struct file_operations *fops,
45 			     drm_device_t *dev);
46 	int (*info_unregister)(int minor);
47 } DRM(stub_info);
48 
DRM(stub_open)49 static int DRM(stub_open)(struct inode *inode, struct file *filp)
50 {
51 	int                    minor = minor(inode->i_rdev);
52 	int                    err   = -ENODEV;
53 	struct file_operations *old_fops;
54 
55 	if (minor < 0 || minor >=DRM_STUB_MAXCARDS) return -ENODEV;
56 	if (!DRM(stub_list) || !DRM(stub_list)[minor].fops) return -ENODEV;
57 	old_fops   = filp->f_op;
58 	filp->f_op = fops_get(DRM(stub_list)[minor].fops);
59 	if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
60 		fops_put(filp->f_op);
61 		filp->f_op = fops_get(old_fops);
62 	}
63 	fops_put(old_fops);
64 
65 	return err;
66 }
67 
68 static struct file_operations DRM(stub_fops) = {
69 	.owner = THIS_MODULE,
70 	.open  = DRM(stub_open)
71 };
72 
DRM(stub_getminor)73 static int DRM(stub_getminor)(const char *name, struct file_operations *fops,
74 			      drm_device_t *dev)
75 {
76 	int i;
77 
78 	if (!DRM(stub_list)) {
79 		DRM(stub_list) = DRM(alloc)(sizeof(*DRM(stub_list))
80 					    * DRM_STUB_MAXCARDS, DRM_MEM_STUB);
81 		if(!DRM(stub_list)) return -1;
82 		for (i = 0; i < DRM_STUB_MAXCARDS; i++) {
83 			DRM(stub_list)[i].name = NULL;
84 			DRM(stub_list)[i].fops = NULL;
85 		}
86 	}
87 	for (i = 0; i < DRM_STUB_MAXCARDS; i++) {
88 		if (!DRM(stub_list)[i].fops) {
89 			DRM(stub_list)[i].name = name;
90 			DRM(stub_list)[i].fops = fops;
91 			DRM(stub_root) = DRM(proc_init)(dev, i, DRM(stub_root),
92 							&DRM(stub_list)[i]
93 							.dev_root);
94 			return i;
95 		}
96 	}
97 	return -1;
98 }
99 
DRM(stub_putminor)100 static int DRM(stub_putminor)(int minor)
101 {
102 	if (minor < 0 || minor >= DRM_STUB_MAXCARDS) return -1;
103 	DRM(stub_list)[minor].name = NULL;
104 	DRM(stub_list)[minor].fops = NULL;
105 	DRM(proc_cleanup)(minor, DRM(stub_root),
106 			  DRM(stub_list)[minor].dev_root);
107 	if (minor) {
108 		inter_module_put("drm");
109 	} else {
110 		inter_module_unregister("drm");
111 		DRM(free)(DRM(stub_list),
112 			  sizeof(*DRM(stub_list)) * DRM_STUB_MAXCARDS,
113 			  DRM_MEM_STUB);
114 		unregister_chrdev(DRM_MAJOR, "drm");
115 	}
116 	return 0;
117 }
118 
119 
DRM(stub_register)120 int DRM(stub_register)(const char *name, struct file_operations *fops,
121 		       drm_device_t *dev)
122 {
123 	struct drm_stub_info *i = NULL;
124 
125 	DRM_DEBUG("\n");
126 	if (register_chrdev(DRM_MAJOR, "drm", &DRM(stub_fops)))
127 		i = (struct drm_stub_info *)inter_module_get("drm");
128 
129 	if (i) {
130 				/* Already registered */
131 		DRM(stub_info).info_register   = i->info_register;
132 		DRM(stub_info).info_unregister = i->info_unregister;
133 		DRM_DEBUG("already registered\n");
134 	} else if (DRM(stub_info).info_register != DRM(stub_getminor)) {
135 		DRM(stub_info).info_register   = DRM(stub_getminor);
136 		DRM(stub_info).info_unregister = DRM(stub_putminor);
137 		DRM_DEBUG("calling inter_module_register\n");
138 		inter_module_register("drm", THIS_MODULE, &DRM(stub_info));
139 	}
140 	if (DRM(stub_info).info_register)
141 		return DRM(stub_info).info_register(name, fops, dev);
142 	return -1;
143 }
144 
DRM(stub_unregister)145 int DRM(stub_unregister)(int minor)
146 {
147 	DRM_DEBUG("%d\n", minor);
148 	if (DRM(stub_info).info_unregister)
149 		return DRM(stub_info).info_unregister(minor);
150 	return -1;
151 }
152