1*f21b21b0Sriastradh /* $NetBSD: via_module.c,v 1.5 2021/12/19 10:33:00 riastradh Exp $ */
2a72b8c43Sriastradh
3a72b8c43Sriastradh /*-
4a72b8c43Sriastradh * Copyright (c) 2015 The NetBSD Foundation, Inc.
5a72b8c43Sriastradh * All rights reserved.
6a72b8c43Sriastradh *
7a72b8c43Sriastradh * This code is derived from software contributed to The NetBSD Foundation
8a72b8c43Sriastradh * by Taylor R. Campbell.
9a72b8c43Sriastradh *
10a72b8c43Sriastradh * Redistribution and use in source and binary forms, with or without
11a72b8c43Sriastradh * modification, are permitted provided that the following conditions
12a72b8c43Sriastradh * are met:
13a72b8c43Sriastradh * 1. Redistributions of source code must retain the above copyright
14a72b8c43Sriastradh * notice, this list of conditions and the following disclaimer.
15a72b8c43Sriastradh * 2. Redistributions in binary form must reproduce the above copyright
16a72b8c43Sriastradh * notice, this list of conditions and the following disclaimer in the
17a72b8c43Sriastradh * documentation and/or other materials provided with the distribution.
18a72b8c43Sriastradh *
19a72b8c43Sriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a72b8c43Sriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a72b8c43Sriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a72b8c43Sriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a72b8c43Sriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a72b8c43Sriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a72b8c43Sriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a72b8c43Sriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a72b8c43Sriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a72b8c43Sriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a72b8c43Sriastradh * POSSIBILITY OF SUCH DAMAGE.
30a72b8c43Sriastradh */
31a72b8c43Sriastradh
32a72b8c43Sriastradh #include <sys/cdefs.h>
33*f21b21b0Sriastradh __KERNEL_RCSID(0, "$NetBSD: via_module.c,v 1.5 2021/12/19 10:33:00 riastradh Exp $");
34a72b8c43Sriastradh
35a72b8c43Sriastradh #include <sys/types.h>
36a72b8c43Sriastradh #include <sys/module.h>
37a72b8c43Sriastradh #include <sys/once.h>
38a72b8c43Sriastradh
39*f21b21b0Sriastradh #include <drm/drm_device.h>
40a72b8c43Sriastradh #include <drm/via_drm.h>
41a72b8c43Sriastradh
42a72b8c43Sriastradh #include "via_drv.h"
43a72b8c43Sriastradh
44a72b8c43Sriastradh MODULE(MODULE_CLASS_DRIVER, viadrmums, "drmkms,drmkms_pci");
45a72b8c43Sriastradh
46a72b8c43Sriastradh #ifdef _MODULE
47a72b8c43Sriastradh #include "ioconf.c"
48a72b8c43Sriastradh #endif
49a72b8c43Sriastradh
50a72b8c43Sriastradh extern struct drm_driver *const via_drm_driver; /* XXX */
51a72b8c43Sriastradh
52a72b8c43Sriastradh static int
viadrm_init(void)53a72b8c43Sriastradh viadrm_init(void)
54a72b8c43Sriastradh {
55a72b8c43Sriastradh int error;
56a72b8c43Sriastradh
57a72b8c43Sriastradh via_init_command_verifier(); /* idempotent, no unwind needed */
58a72b8c43Sriastradh
59a72b8c43Sriastradh error = drm_guarantee_initialized();
60a72b8c43Sriastradh if (error)
61a72b8c43Sriastradh return error;
62a72b8c43Sriastradh
63a72b8c43Sriastradh return 0;
64a72b8c43Sriastradh }
65a72b8c43Sriastradh
66a72b8c43Sriastradh int viadrm_guarantee_initialized(void); /* XXX */
67a72b8c43Sriastradh int
viadrm_guarantee_initialized(void)68a72b8c43Sriastradh viadrm_guarantee_initialized(void)
69a72b8c43Sriastradh {
70a72b8c43Sriastradh #ifdef _MODULE
71a72b8c43Sriastradh return 0;
72a72b8c43Sriastradh #else
73a72b8c43Sriastradh static ONCE_DECL(viadrm_init_once);
74a72b8c43Sriastradh
75a72b8c43Sriastradh return RUN_ONCE(&viadrm_init_once, &viadrm_init);
76a72b8c43Sriastradh #endif
77a72b8c43Sriastradh }
78a72b8c43Sriastradh
79a72b8c43Sriastradh static void
viadrm_fini(void)80a72b8c43Sriastradh viadrm_fini(void)
81a72b8c43Sriastradh {
82a72b8c43Sriastradh }
83a72b8c43Sriastradh
84a72b8c43Sriastradh static int
viadrmums_modcmd(modcmd_t cmd,void * arg __unused)85a72b8c43Sriastradh viadrmums_modcmd(modcmd_t cmd, void *arg __unused)
86a72b8c43Sriastradh {
87a72b8c43Sriastradh int error;
88a72b8c43Sriastradh
89a72b8c43Sriastradh switch (cmd) {
90a72b8c43Sriastradh case MODULE_CMD_INIT:
91a72b8c43Sriastradh #ifdef _MODULE
92a72b8c43Sriastradh error = viadrm_init();
93a72b8c43Sriastradh #else
94a72b8c43Sriastradh error = viadrm_guarantee_initialized();
95a72b8c43Sriastradh #endif
96a72b8c43Sriastradh if (error) {
97a72b8c43Sriastradh aprint_error("viadrmums: failed to initialize: %d\n",
98a72b8c43Sriastradh error);
99a72b8c43Sriastradh return error;
100a72b8c43Sriastradh }
101a72b8c43Sriastradh #ifdef _MODULE
102a72b8c43Sriastradh error = config_init_component(cfdriver_ioconf_viadrmums,
103a72b8c43Sriastradh cfattach_ioconf_viadrmums, cfdata_ioconf_viadrmums);
104a72b8c43Sriastradh if (error) {
105a72b8c43Sriastradh aprint_error("viadrmums: failed to init component"
106a72b8c43Sriastradh ": %d\n", error);
107a72b8c43Sriastradh viadrm_fini();
108a72b8c43Sriastradh return error;
109a72b8c43Sriastradh }
110a72b8c43Sriastradh #endif
111a72b8c43Sriastradh return 0;
112a72b8c43Sriastradh
113a72b8c43Sriastradh case MODULE_CMD_FINI:
114a72b8c43Sriastradh #ifdef _MODULE
115a72b8c43Sriastradh error = config_fini_component(cfdriver_ioconf_viadrmums,
116a72b8c43Sriastradh cfattach_ioconf_viadrmums, cfdata_ioconf_viadrmums);
117a72b8c43Sriastradh if (error) {
118a72b8c43Sriastradh aprint_error("viadrmums: failed to fini component"
119a72b8c43Sriastradh ": %d\n", error);
120a72b8c43Sriastradh return error;
121a72b8c43Sriastradh }
122a72b8c43Sriastradh #endif
123a72b8c43Sriastradh viadrm_fini();
124a72b8c43Sriastradh return 0;
125a72b8c43Sriastradh
126a72b8c43Sriastradh default:
127a72b8c43Sriastradh return ENOTTY;
128a72b8c43Sriastradh }
129a72b8c43Sriastradh }
130