1/* $NetBSD: drm_module.c,v 1.11 2015/04/13 22:24:34 pgoyette 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.11 2015/04/13 22:24:34 pgoyette Exp $");
34
35#include <sys/types.h>
36#include <sys/conf.h>
37#include <sys/device.h>
38#include <sys/module.h>
39#ifndef _MODULE
40#include <sys/once.h>
41#endif
42#include <sys/reboot.h>
43#include <sys/systm.h>
44
45#include <linux/mutex.h>
46
47#include <drm/drmP.h>
48#include <drm/drm_encoder_slave.h>
49#include <drm/drm_sysctl.h>
50
51/*
52 * XXX I2C stuff should be moved to a separate drmkms_i2c module.
53 */
54MODULE(MODULE_CLASS_DRIVER, drmkms, "drmkms_linux");
55
56struct mutex drm_global_mutex;
57
58struct drm_sysctl_def drm_def = DRM_SYSCTL_INIT();
59
60static int
61drm_init(void)
62{
63 extern int linux_guarantee_initialized(void);
64 int error;
65
66 error = linux_guarantee_initialized();
67 if (error)
68 return error;
69
70 if (ISSET(boothowto, AB_DEBUG))
71 drm_debug = ~(unsigned int)0;
72
73 spin_lock_init(&drm_minor_lock);
74 idr_init(&drm_minors_idr);
75 linux_mutex_init(&drm_global_mutex);
76 drm_connector_ida_init();
77 drm_global_init();
78 drm_sysctl_init(&drm_def);
79 drm_i2c_encoders_init();
80
81 return 0;
82}
83
84int drm_guarantee_initialized(void); /* XXX */
85int
86drm_guarantee_initialized(void)
87{
88#ifdef _MODULE
89 return 0;
90#else
91 static ONCE_DECL(drm_init_once);
92
93 return RUN_ONCE(&drm_init_once, &drm_init);
94#endif
95}
96
97static void
98drm_fini(void)
99{
100
101 drm_i2c_encoders_fini();
102 drm_sysctl_fini(&drm_def);
103 drm_global_release();
104 drm_connector_ida_destroy();
105 linux_mutex_destroy(&drm_global_mutex);
106 idr_destroy(&drm_minors_idr);
107 spin_lock_destroy(&drm_minor_lock);
108}
109
110static int
111drmkms_modcmd(modcmd_t cmd, void *arg __unused)
112{
113#ifdef _MODULE
114 devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
115#endif
116 int error;
117
118 switch (cmd) {
119 case MODULE_CMD_INIT:
120#ifdef _MODULE
121 error = drm_init();
122#else
123 error = drm_guarantee_initialized();
124#endif
125 if (error)
126 return error;
127#ifdef _MODULE
128 error = devsw_attach("drm", NULL, &bmajor,
129 &drm_cdevsw, &cmajor);
130 if (error) {
131 aprint_error("drmkms: unable to attach devsw: %d\n",
132 error);
133 return error;
134 }
135#endif
136 return 0;
137
138 case MODULE_CMD_FINI:
139#ifdef _MODULE
140 error = devsw_detach(NULL, &drm_cdevsw);
141 if (error)
142 return error;
143#endif
144 drm_fini();
145 return 0;
146
147 default:
148 return ENOTTY;
149 }
150}
151