1/* $NetBSD */
2
3/*
4 * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/device.h>
34#include <sys/module.h>
35
36#include <dev/vmt/vmtreg.h>
37#include <dev/vmt/vmtvar.h>
38
39static int	vmt_match(device_t, cfdata_t, void *);
40static void	vmt_attach(device_t, device_t, void *);
41static int	vmt_detach(device_t, int);
42
43CFATTACH_DECL_NEW(vmt, sizeof(struct vmt_softc),
44	vmt_match, vmt_attach, vmt_detach, NULL);
45
46static bool vmt_attached = false;
47
48static int
49vmt_match(device_t parent, cfdata_t match, void *aux)
50{
51	/* vmt should not attach to more than a single CPU. */
52	if (vmt_attached)
53		return 0;
54
55	return vmt_probe();
56}
57
58static void
59vmt_attach(device_t parent, device_t self, void *aux)
60{
61	struct vmt_softc *sc = device_private(self);
62
63	aprint_naive("\n");
64	aprint_normal(": VMware Tools driver\n");
65
66	sc->sc_dev = self;
67	vmt_common_attach(sc);
68
69	vmt_attached = true;
70}
71
72static int
73vmt_detach(device_t self, int flags)
74{
75	struct vmt_softc *sc = device_private(self);
76	int rv;
77
78	rv = vmt_common_detach(sc);
79	if (rv != 0)
80		return rv;
81
82	vmt_attached = false;
83	return 0;
84}
85
86MODULE(MODULE_CLASS_DRIVER, vmt, "sysmon_power,sysmon_taskq");
87
88#ifdef _MODULE
89#include "ioconf.c"
90#endif
91
92static int
93vmt_modcmd(modcmd_t cmd, void *aux)
94{
95	int error = 0;
96
97	switch (cmd) {
98	case MODULE_CMD_INIT:
99#ifdef _MODULE
100		error = config_init_component(cfdriver_ioconf_vmt,
101		    cfattach_ioconf_vmt, cfdata_ioconf_vmt);
102#endif
103		break;
104	case MODULE_CMD_FINI:
105#ifdef _MODULE
106		error = config_fini_component(cfdriver_ioconf_vmt,
107		    cfattach_ioconf_vmt, cfdata_ioconf_vmt);
108#endif
109		break;
110	case MODULE_CMD_AUTOUNLOAD:
111		error = EBUSY;
112		break;
113	default:
114		error = ENOTTY;
115		break;
116	}
117
118	return error;
119}
120