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