1/* $NetBSD: i915_module.c,v 1.5 2014/11/12 03:14:00 christos 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: i915_module.c,v 1.5 2014/11/12 03:14:00 christos Exp $");
34
35#include <sys/types.h>
36#include <sys/module.h>
37#ifndef _MODULE
38#include <sys/once.h>
39#endif
40#include <sys/systm.h>
41
42#include <drm/drmP.h>
43#include <drm/drm_sysctl.h>
44
45#include "i915_drv.h"
46
47MODULE(MODULE_CLASS_DRIVER, i915drmkms, "drmkms,drmkms_pci"); /* XXX drmkms_i2c */
48
49#ifdef _MODULE
50#include "ioconf.c"
51#endif
52
53/* XXX Kludge to get these from i915_drv.c. */
54extern struct drm_driver *const i915_drm_driver;
55extern const struct pci_device_id *const i915_device_ids;
56extern const size_t i915_n_device_ids;
57
58struct drm_sysctl_def i915_def = DRM_SYSCTL_INIT();
59
60static int
61i915drmkms_init(void)
62{
63 extern int drm_guarantee_initialized(void);
64 int error;
65
66 error = drm_guarantee_initialized();
67 if (error)
68 return error;
69
70 i915_drm_driver->num_ioctls = i915_max_ioctl;
71 i915_drm_driver->driver_features |= DRIVER_MODESET;
72 i915_drm_driver->driver_features &= ~DRIVER_USE_AGP;
73
74 error = drm_pci_init(i915_drm_driver, NULL);
75 if (error) {
76 aprint_error("i915drmkms: failed to init pci: %d\n",
77 error);
78 return error;
79 }
80 drm_sysctl_init(&i915_def);
81
82 return 0;
83}
84
85int i915drmkms_guarantee_initialized(void); /* XXX */
86int
87i915drmkms_guarantee_initialized(void)
88{
89#ifdef _MODULE
90 return 0;
91#else
92 static ONCE_DECL(i915drmkms_init_once);
93
94 return RUN_ONCE(&i915drmkms_init_once, &i915drmkms_init);
95#endif
96}
97
98static void
99i915drmkms_fini(void)
100{
101
102 drm_pci_exit(i915_drm_driver, NULL);
103 drm_sysctl_fini(&i915_def);
104}
105
106static int
107i915drmkms_modcmd(modcmd_t cmd, void *arg __unused)
108{
109 int error;
110
111 switch (cmd) {
112 case MODULE_CMD_INIT:
113 /* XXX Kludge it up... Must happen before attachment. */
114#ifdef _MODULE
115 error = i915drmkms_init();
116#else
117 error = i915drmkms_guarantee_initialized();
118#endif
119 if (error) {
120 aprint_error("i915drmkms: failed to initialize: %d\n",
121 error);
122 return error;
123 }
124#ifdef _MODULE
125 error = config_init_component(cfdriver_ioconf_i915drmkms,
126 cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
127 if (error) {
128 aprint_error("i915drmkms: failed to init component"
129 ": %d\n", error);
130 i915drmkms_fini();
131 return error;
132 }
133#endif
134 return 0;
135
136 case MODULE_CMD_FINI:
137#ifdef _MODULE
138 error = config_fini_component(cfdriver_ioconf_i915drmkms,
139 cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
140 if (error) {
141 aprint_error("i915drmkms: failed to fini component"
142 ": %d\n", error);
143 return error;
144 }
145#endif
146 i915drmkms_fini();
147 return 0;
148
149 default:
150 return ENOTTY;
151 }
152}
153