xref: /netbsd-src/sys/external/bsd/drm2/drm/drm_module.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: drm_module.c,v 1.5 2014/04/04 15:16:59 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_module.c,v 1.5 2014/04/04 15:16:59 riastradh Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/device.h>
37 #include <sys/module.h>
38 #include <sys/reboot.h>
39 #include <sys/systm.h>
40 
41 #include <drm/drmP.h>
42 
43 /*
44  * XXX I2C stuff should be moved to a separate drmkms_i2c module.
45  */
46 MODULE(MODULE_CLASS_DRIVER, drmkms, "iic,drmkms_linux");
47 
48 #ifdef _MODULE
49 #include "ioconf.c"
50 #endif
51 
52 /*
53  * XXX Mega-kludge.  See drm_init in drm_drv.c for details.
54  */
55 #ifdef _MODULE
56 static const int linux_suppress_init = 1;
57 #else
58 extern int linux_suppress_init;
59 #endif
60 
61 static int
62 drmkms_modcmd(modcmd_t cmd, void *arg __unused)
63 {
64 #ifdef _MODULE
65 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
66 	int error;
67 #endif
68 
69 	switch (cmd) {
70 	case MODULE_CMD_INIT:
71 		if (!linux_suppress_init) {
72 			linux_mutex_init(&drm_global_mutex);
73 			if (ISSET(boothowto, AB_DEBUG))
74 				drm_debug = ~(unsigned int)0;
75 		}
76 #ifdef _MODULE
77 		error = config_init_component(cfdriver_ioconf_drmkms,
78 		    cfattach_ioconf_drmkms, cfdata_ioconf_drmkms);
79 		if (error) {
80 			aprint_error("drmkms: unable to init component: %d\n",
81 			    error);
82 			goto init_fail0;
83 		}
84 		error = devsw_attach("drm", NULL, &bmajor,
85 		    &drm_cdevsw, &cmajor);
86 		if (error) {
87 			aprint_error("drmkms: unable to attach devsw: %d\n",
88 			    error);
89 			goto init_fail1;
90 		}
91 #endif
92 		return 0;
93 
94 #ifdef _MODULE
95 init_fail2: __unused
96 		(void)devsw_detach(NULL, &drm_cdevsw);
97 init_fail1:	(void)config_fini_component(cfdriver_ioconf_drmkms,
98 		    cfattach_ioconf_drmkms, cfdata_ioconf_drmkms);
99 init_fail0:	linux_mutex_destroy(&drm_global_mutex);
100 		return error;
101 #endif	/* _MODULE */
102 
103 	case MODULE_CMD_FINI:
104 #ifdef _MODULE
105 		error = devsw_detach(NULL, &drm_cdevsw);
106 		if (error)
107 			return error;
108 		error = config_fini_component(cfdriver_ioconf_drmkms,
109 		    cfattach_ioconf_drmkms, cfdata_ioconf_drmkms);
110 		if (error)
111 			/* XXX Now what?  Reattach the devsw?  */
112 			return error;
113 #endif
114 		linux_mutex_destroy(&drm_global_mutex);
115 		return 0;
116 
117 	default:
118 		return ENOTTY;
119 	}
120 }
121