xref: /freebsd/sys/dev/fdc/fdc_acpi.c (revision 2cf0c51793da5a2fc03db8990fc2feb3f9aa119f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004 Nate Lawson (SDG)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *	notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *	notice, this list of conditions and the following disclaimer in the
14  *	documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/bio.h>
32 #include <sys/bus.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/proc.h>
36 
37 #include <contrib/dev/acpica/include/acpi.h>
38 
39 #include <dev/acpica/acpivar.h>
40 #include <dev/fdc/fdcvar.h>
41 
42 static int		fdc_acpi_probe(device_t dev);
43 static int		fdc_acpi_attach(device_t dev);
44 static int		fdc_acpi_probe_children(device_t bus, device_t dev,
45 			    void *fde);
46 static ACPI_STATUS	fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev,
47 			    int level, void *arg);
48 
49 /* Maximum number of child devices of a controller (4 floppy + 1 tape.) */
50 #define ACPI_FDC_MAXDEVS	5
51 
52 /* Standard size of buffer returned by the _FDE method. */
53 #define ACPI_FDC_FDE_LEN	(ACPI_FDC_MAXDEVS * sizeof(uint32_t))
54 
55 /*
56  * Parameters for the tape drive (5th device).  Some BIOS authors use this
57  * for all drives, not just the tape drive (e.g., ASUS K8V).  This isn't
58  * grossly incompatible with the spec since it says the first four devices
59  * are simple booleans.
60  */
61 #define ACPI_FD_UNKNOWN		0
62 #define ACPI_FD_PRESENT		1
63 #define ACPI_FD_NEVER_PRESENT	2
64 
65 /* Temporary buf length for evaluating _FDE and _FDI. */
66 #define ACPI_FDC_BUFLEN		1024
67 
68 /* Context for walking FDC child devices. */
69 struct fdc_walk_ctx {
70 	uint32_t	fd_present[ACPI_FDC_MAXDEVS];
71 	int		index;
72 	device_t	acpi_dev;
73 	device_t	dev;
74 };
75 
76 static int
77 fdc_acpi_probe(device_t dev)
78 {
79 	device_t bus;
80 	static char *fdc_ids[] = { "PNP0700", "PNP0701", NULL };
81 	int rv;
82 
83 	bus = device_get_parent(dev);
84 	rv = ACPI_ID_PROBE(bus, dev, fdc_ids, NULL);
85 	if (rv > 0)
86 		return (rv);
87 
88 	if (ACPI_SUCCESS(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, NULL)))
89 		device_set_desc(dev, "floppy drive controller (FDE)");
90 	else
91 		device_set_desc(dev, "floppy drive controller");
92 	return (rv);
93 }
94 
95 static int
96 fdc_acpi_attach(device_t dev)
97 {
98 	struct fdc_data *sc;
99 	ACPI_BUFFER buf;
100 	ACPI_OBJECT *obj;
101 	device_t bus;
102 	int error;
103 
104 	/* Get our softc and use the same accessor as ISA. */
105 	sc = device_get_softc(dev);
106 	sc->fdc_dev = dev;
107 
108 	/* Initialize variables and get a temporary buffer for _FDE. */
109 	error = ENXIO;
110 	buf.Length = ACPI_FDC_BUFLEN;
111 	buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO);
112 	if (buf.Pointer == NULL)
113 		goto out;
114 
115 	/* Allocate resources the same as the ISA attachment. */
116 	error = fdc_isa_alloc_resources(dev, sc);
117 	if (error != 0)
118 		goto out;
119 
120 	/* Call common attach code in fdc(4) first. */
121 	error = fdc_attach(dev);
122 	if (error != 0)
123 		goto out;
124 
125 	/*
126 	 * Enumerate _FDE, which lists floppy drives that are present.  If
127 	 * this fails, fall back to the ISA hints-based probe method.
128 	 */
129 	bus = device_get_parent(dev);
130 	if (ACPI_FAILURE(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, &buf))) {
131 		error = fdc_hints_probe(dev);
132 		goto out;
133 	}
134 
135 	/* Add fd child devices as specified. */
136 	obj = buf.Pointer;
137 	error = fdc_acpi_probe_children(bus, dev, obj->Buffer.Pointer);
138 
139 out:
140 	if (buf.Pointer)
141 		free(buf.Pointer, M_TEMP);
142 	if (error != 0)
143 		fdc_release_resources(sc);
144 	else
145 		fdc_start_worker(dev);
146 
147 	return (error);
148 }
149 
150 static int
151 fdc_acpi_probe_children(device_t bus, device_t dev, void *fde)
152 {
153 	struct fdc_walk_ctx *ctx;
154 	devclass_t fd_dc;
155 	int i;
156 
157 	/* Setup the context and walk all child devices. */
158 	ctx = malloc(sizeof(struct fdc_walk_ctx), M_TEMP, M_NOWAIT);
159 	if (ctx == NULL) {
160 		device_printf(dev, "no memory for walking children\n");
161 		return (ENOMEM);
162 	}
163 	bcopy(fde, ctx->fd_present, sizeof(ctx->fd_present));
164 	ctx->index = 0;
165 	ctx->dev = dev;
166 	ctx->acpi_dev = bus;
167 	ACPI_SCAN_CHILDREN(ctx->acpi_dev, dev, 1, fdc_acpi_probe_child,
168 	    ctx);
169 
170 	/* Add any devices not represented by an AML Device handle/node. */
171 	fd_dc = devclass_find("fd");
172 	for (i = 0; i < ACPI_FDC_MAXDEVS; i++)
173 		if (ctx->fd_present[i] == ACPI_FD_PRESENT &&
174 		    devclass_get_device(fd_dc, i) == NULL) {
175 			if (fdc_add_child(dev, "fd", i) == NULL)
176 				device_printf(dev, "fd add failed\n");
177 		}
178 	free(ctx, M_TEMP);
179 
180 	/* Attach any children found during the probe. */
181 	return (bus_generic_attach(dev));
182 }
183 
184 static ACPI_STATUS
185 fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, int level, void *arg)
186 {
187 	struct fdc_walk_ctx *ctx;
188 	device_t child, old_child;
189 	ACPI_BUFFER buf;
190 	ACPI_OBJECT *pkg, *obj;
191 	ACPI_STATUS status;
192 
193 	ctx = (struct fdc_walk_ctx *)arg;
194 	buf.Pointer = NULL;
195 
196 	/*
197 	 * The first four ints are booleans that indicate whether fd0-3 are
198 	 * present or not.  The last is for a tape device, which we don't
199 	 * bother supporting for now.
200 	 */
201 	if (ctx->index > 3)
202 		return (AE_OK);
203 
204 	/* This device is not present, move on to the next. */
205 	if (ctx->fd_present[ctx->index] != ACPI_FD_PRESENT)
206 		goto out;
207 
208 	/* Create a device for the child with the given index. */
209 	child = fdc_add_child(ctx->dev, "fd", ctx->index);
210 	if (child == NULL)
211 		goto out;
212 	old_child = *dev;
213 	*dev = child;
214 
215 	/* Get temporary buffer for _FDI probe. */
216 	buf.Length = ACPI_FDC_BUFLEN;
217 	buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO);
218 	if (buf.Pointer == NULL)
219 		goto out;
220 
221 	/*
222 	 * Evaluate _FDI to get drive type to pass to the child.  We use the
223 	 * old child here since it has a valid ACPI_HANDLE since it is a
224 	 * child of acpi.  A better way to implement this would be to make fdc
225 	 * support the ACPI handle ivar for its children.
226 	 */
227 	status = ACPI_EVALUATE_OBJECT(ctx->acpi_dev, old_child, "_FDI", NULL,
228 	    &buf);
229 	if (ACPI_FAILURE(status)) {
230 		if (status != AE_NOT_FOUND)
231 			device_printf(ctx->dev, "_FDI failed - %#x\n", status);
232 		goto out;
233 	}
234 	pkg = (ACPI_OBJECT *)buf.Pointer;
235 	if (!ACPI_PKG_VALID(pkg, 16)) {
236 		device_printf(ctx->dev, "invalid _FDI package\n");
237 		goto out;
238 	}
239 	obj = &pkg->Package.Elements[1];
240 	if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) {
241 		device_printf(ctx->dev, "invalid type object in _FDI\n");
242 		goto out;
243 	}
244 	fdc_set_fdtype(child, obj->Integer.Value);
245 
246 out:
247 	ctx->index++;
248 	if (buf.Pointer)
249 		free(buf.Pointer, M_TEMP);
250 	return (AE_OK);
251 }
252 
253 static device_method_t fdc_acpi_methods[] = {
254 	/* Device interface */
255 	DEVMETHOD(device_probe,		fdc_acpi_probe),
256 	DEVMETHOD(device_attach,	fdc_acpi_attach),
257 	DEVMETHOD(device_detach,	fdc_detach),
258 
259 	/* Bus interface */
260 	DEVMETHOD(bus_print_child,	fdc_print_child),
261 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
262 	DEVMETHOD(bus_write_ivar,	fdc_write_ivar),
263 
264 	DEVMETHOD_END
265 };
266 
267 static driver_t fdc_acpi_driver = {
268 	"fdc",
269 	fdc_acpi_methods,
270 	sizeof(struct fdc_data)
271 };
272 
273 DRIVER_MODULE(fdc, acpi, fdc_acpi_driver, 0, 0);
274