2 * Copyright (c) 2004 Nate Lawson (SDG)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
38 #include <contrib/dev/acpica/include/acpi.h>
40 #include <dev/acpica/acpivar.h>
41 #include <dev/fdc/fdcvar.h>
43 static int fdc_acpi_probe(device_t dev);
44 static int fdc_acpi_attach(device_t dev);
45 static int fdc_acpi_probe_children(device_t bus, device_t dev,
47 static ACPI_STATUS fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev,
48 int level, void *arg);
50 /* Maximum number of child devices of a controller (4 floppy + 1 tape.) */
51 #define ACPI_FDC_MAXDEVS 5
53 /* Standard size of buffer returned by the _FDE method. */
54 #define ACPI_FDC_FDE_LEN (ACPI_FDC_MAXDEVS * sizeof(uint32_t))
57 * Parameters for the tape drive (5th device). Some BIOS authors use this
58 * for all drives, not just the tape drive (e.g., ASUS K8V). This isn't
59 * grossly incompatible with the spec since it says the first four devices
60 * are simple booleans.
62 #define ACPI_FD_UNKNOWN 0
63 #define ACPI_FD_PRESENT 1
64 #define ACPI_FD_NEVER_PRESENT 2
66 /* Temporary buf length for evaluating _FDE and _FDI. */
67 #define ACPI_FDC_BUFLEN 1024
69 /* Context for walking FDC child devices. */
71 uint32_t fd_present[ACPI_FDC_MAXDEVS];
78 fdc_acpi_probe(device_t dev)
81 static char *fdc_ids[] = { "PNP0700", "PNP0701", NULL };
83 bus = device_get_parent(dev);
84 if (ACPI_ID_PROBE(bus, dev, fdc_ids) == NULL)
87 if (ACPI_SUCCESS(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, NULL)))
88 device_set_desc(dev, "floppy drive controller (FDE)");
90 device_set_desc(dev, "floppy drive controller");
95 fdc_acpi_attach(device_t dev)
100 int error, fde_count, i;
101 ACPI_OBJECT *obj, *pkg;
102 uint32_t fde[ACPI_FDC_MAXDEVS];
104 /* Get our softc and use the same accessor as ISA. */
105 sc = device_get_softc(dev);
108 /* Initialize variables and get a temporary buffer for _FDE. */
110 buf.Length = ACPI_FDC_BUFLEN;
111 buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO);
112 if (buf.Pointer == NULL)
115 /* Allocate resources the same as the ISA attachment. */
116 error = fdc_isa_alloc_resources(dev, sc);
120 /* Call common attach code in fdc(4) first. */
121 error = fdc_attach(dev);
126 * Enumerate _FDE, which lists floppy drives that are present. If
127 * this fails, fall back to the ISA hints-based probe method.
129 bus = device_get_parent(dev);
130 if (ACPI_FAILURE(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, &buf))) {
135 /* Parse the output of _FDE in various ways. */
136 obj = pkg = (ACPI_OBJECT *)buf.Pointer;
138 case ACPI_TYPE_BUFFER:
140 * The spec says _FDE should be a buffer of five 32-bit
141 * integers. In violation of the spec, some systems use
142 * five bytes instead.
144 switch (obj->Buffer.Length) {
145 case ACPI_FDC_FDE_LEN:
146 bcopy(obj->Buffer.Pointer, fde, ACPI_FDC_FDE_LEN);
148 case ACPI_FDC_MAXDEVS:
149 for (i = 0; i < ACPI_FDC_MAXDEVS; i++)
150 fde[i] = ((uint8_t *)obj->Buffer.Pointer)[i];
153 device_printf(dev, "_FDE wrong length: %d\n",
159 case ACPI_TYPE_PACKAGE:
161 * In violation of the spec, systems including the ASUS
162 * K8V return a package of five integers instead of a
163 * buffer of five 32-bit integers.
165 fde_count = min(ACPI_FDC_MAXDEVS, pkg->Package.Count);
166 for (i = 0; i < fde_count; i++) {
167 obj = &pkg->Package.Elements[i];
168 if (obj->Type == ACPI_TYPE_INTEGER)
169 fde[i] = (uint32_t)obj->Integer.Value;
173 device_printf(dev, "invalid _FDE type %d\n", obj->Type);
178 /* Add fd child devices as specified. */
179 error = fdc_acpi_probe_children(bus, dev, fde);
183 * If there was a problem with the _FDE drive enumeration, fall
184 * back to the hints-based probe.
187 error = fdc_hints_probe(dev);
191 free(buf.Pointer, M_TEMP);
193 fdc_release_resources(sc);
199 fdc_acpi_probe_children(device_t bus, device_t dev, void *fde)
201 struct fdc_walk_ctx *ctx;
205 /* Setup the context and walk all child devices. */
206 ctx = malloc(sizeof(struct fdc_walk_ctx), M_TEMP, M_NOWAIT);
208 device_printf(dev, "no memory for walking children\n");
211 bcopy(fde, ctx->fd_present, sizeof(ctx->fd_present));
215 ACPI_SCAN_CHILDREN(ctx->acpi_dev, dev, 1, fdc_acpi_probe_child,
218 /* Add any devices not represented by an AML Device handle/node. */
219 fd_dc = devclass_find("fd");
220 for (i = 0; i < ACPI_FDC_MAXDEVS; i++)
221 if (ctx->fd_present[i] == ACPI_FD_PRESENT &&
222 devclass_get_device(fd_dc, i) == NULL) {
223 if (fdc_add_child(dev, "fd", i) == NULL)
224 device_printf(dev, "fd add failed\n");
228 /* Attach any children found during the probe. */
229 return (bus_generic_attach(dev));
233 fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, int level, void *arg)
235 struct fdc_walk_ctx *ctx;
236 device_t child, old_child;
238 ACPI_OBJECT *pkg, *obj;
241 ctx = (struct fdc_walk_ctx *)arg;
245 * The first four ints are booleans that indicate whether fd0-3 are
246 * present or not. The last is for a tape device, which we don't
247 * bother supporting for now.
252 /* This device is not present, move on to the next. */
253 if (ctx->fd_present[ctx->index] != ACPI_FD_PRESENT)
256 /* Create a device for the child with the given index. */
257 child = fdc_add_child(ctx->dev, "fd", ctx->index);
263 /* Get temporary buffer for _FDI probe. */
264 buf.Length = ACPI_FDC_BUFLEN;
265 buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO);
266 if (buf.Pointer == NULL)
270 * Evaluate _FDI to get drive type to pass to the child. We use the
271 * old child here since it has a valid ACPI_HANDLE since it is a
272 * child of acpi. A better way to implement this would be to make fdc
273 * support the ACPI handle ivar for its children.
275 status = ACPI_EVALUATE_OBJECT(ctx->acpi_dev, old_child, "_FDI", NULL,
277 if (ACPI_FAILURE(status)) {
278 if (status != AE_NOT_FOUND)
279 device_printf(ctx->dev, "_FDI failed - %#x\n", status);
282 pkg = (ACPI_OBJECT *)buf.Pointer;
283 if (!ACPI_PKG_VALID(pkg, 16)) {
284 device_printf(ctx->dev, "invalid _FDI package\n");
287 obj = &pkg->Package.Elements[1];
288 if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) {
289 device_printf(ctx->dev, "invalid type object in _FDI\n");
292 fdc_set_fdtype(child, obj->Integer.Value);
297 free(buf.Pointer, M_TEMP);
301 static device_method_t fdc_acpi_methods[] = {
302 /* Device interface */
303 DEVMETHOD(device_probe, fdc_acpi_probe),
304 DEVMETHOD(device_attach, fdc_acpi_attach),
305 DEVMETHOD(device_detach, fdc_detach),
308 DEVMETHOD(bus_print_child, fdc_print_child),
309 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
310 DEVMETHOD(bus_write_ivar, fdc_write_ivar),
315 static driver_t fdc_acpi_driver = {
318 sizeof(struct fdc_data)
321 DRIVER_MODULE(fdc, acpi, fdc_acpi_driver, fdc_devclass, 0, 0);