1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/bio.h> 35 #include <sys/bus.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/proc.h> 39 40 #include <contrib/dev/acpica/include/acpi.h> 41 42 #include <dev/acpica/acpivar.h> 43 #include <dev/fdc/fdcvar.h> 44 45 static int fdc_acpi_probe(device_t dev); 46 static int fdc_acpi_attach(device_t dev); 47 static int fdc_acpi_probe_children(device_t bus, device_t dev, 48 void *fde); 49 static ACPI_STATUS fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, 50 int level, void *arg); 51 52 /* Maximum number of child devices of a controller (4 floppy + 1 tape.) */ 53 #define ACPI_FDC_MAXDEVS 5 54 55 /* Standard size of buffer returned by the _FDE method. */ 56 #define ACPI_FDC_FDE_LEN (ACPI_FDC_MAXDEVS * sizeof(uint32_t)) 57 58 /* 59 * Parameters for the tape drive (5th device). Some BIOS authors use this 60 * for all drives, not just the tape drive (e.g., ASUS K8V). This isn't 61 * grossly incompatible with the spec since it says the first four devices 62 * are simple booleans. 63 */ 64 #define ACPI_FD_UNKNOWN 0 65 #define ACPI_FD_PRESENT 1 66 #define ACPI_FD_NEVER_PRESENT 2 67 68 /* Temporary buf length for evaluating _FDE and _FDI. */ 69 #define ACPI_FDC_BUFLEN 1024 70 71 /* Context for walking FDC child devices. */ 72 struct fdc_walk_ctx { 73 uint32_t fd_present[ACPI_FDC_MAXDEVS]; 74 int index; 75 device_t acpi_dev; 76 device_t dev; 77 }; 78 79 static int 80 fdc_acpi_probe(device_t dev) 81 { 82 device_t bus; 83 static char *fdc_ids[] = { "PNP0700", "PNP0701", NULL }; 84 int rv; 85 86 bus = device_get_parent(dev); 87 rv = ACPI_ID_PROBE(bus, dev, fdc_ids, NULL); 88 if (rv > 0) 89 return (rv); 90 91 if (ACPI_SUCCESS(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, NULL))) 92 device_set_desc(dev, "floppy drive controller (FDE)"); 93 else 94 device_set_desc(dev, "floppy drive controller"); 95 return (rv); 96 } 97 98 static int 99 fdc_acpi_attach(device_t dev) 100 { 101 struct fdc_data *sc; 102 ACPI_BUFFER buf; 103 ACPI_OBJECT *obj; 104 device_t bus; 105 int error; 106 107 /* Get our softc and use the same accessor as ISA. */ 108 sc = device_get_softc(dev); 109 sc->fdc_dev = dev; 110 111 /* Initialize variables and get a temporary buffer for _FDE. */ 112 error = ENXIO; 113 buf.Length = ACPI_FDC_BUFLEN; 114 buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO); 115 if (buf.Pointer == NULL) 116 goto out; 117 118 /* Allocate resources the same as the ISA attachment. */ 119 error = fdc_isa_alloc_resources(dev, sc); 120 if (error != 0) 121 goto out; 122 123 /* Call common attach code in fdc(4) first. */ 124 error = fdc_attach(dev); 125 if (error != 0) 126 goto out; 127 128 /* 129 * Enumerate _FDE, which lists floppy drives that are present. If 130 * this fails, fall back to the ISA hints-based probe method. 131 */ 132 bus = device_get_parent(dev); 133 if (ACPI_FAILURE(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, &buf))) { 134 error = fdc_hints_probe(dev); 135 goto out; 136 } 137 138 /* Add fd child devices as specified. */ 139 obj = buf.Pointer; 140 error = fdc_acpi_probe_children(bus, dev, obj->Buffer.Pointer); 141 142 out: 143 if (buf.Pointer) 144 free(buf.Pointer, M_TEMP); 145 if (error != 0) 146 fdc_release_resources(sc); 147 else 148 fdc_start_worker(dev); 149 150 return (error); 151 } 152 153 static int 154 fdc_acpi_probe_children(device_t bus, device_t dev, void *fde) 155 { 156 struct fdc_walk_ctx *ctx; 157 devclass_t fd_dc; 158 int i; 159 160 /* Setup the context and walk all child devices. */ 161 ctx = malloc(sizeof(struct fdc_walk_ctx), M_TEMP, M_NOWAIT); 162 if (ctx == NULL) { 163 device_printf(dev, "no memory for walking children\n"); 164 return (ENOMEM); 165 } 166 bcopy(fde, ctx->fd_present, sizeof(ctx->fd_present)); 167 ctx->index = 0; 168 ctx->dev = dev; 169 ctx->acpi_dev = bus; 170 ACPI_SCAN_CHILDREN(ctx->acpi_dev, dev, 1, fdc_acpi_probe_child, 171 ctx); 172 173 /* Add any devices not represented by an AML Device handle/node. */ 174 fd_dc = devclass_find("fd"); 175 for (i = 0; i < ACPI_FDC_MAXDEVS; i++) 176 if (ctx->fd_present[i] == ACPI_FD_PRESENT && 177 devclass_get_device(fd_dc, i) == NULL) { 178 if (fdc_add_child(dev, "fd", i) == NULL) 179 device_printf(dev, "fd add failed\n"); 180 } 181 free(ctx, M_TEMP); 182 183 /* Attach any children found during the probe. */ 184 return (bus_generic_attach(dev)); 185 } 186 187 static ACPI_STATUS 188 fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, int level, void *arg) 189 { 190 struct fdc_walk_ctx *ctx; 191 device_t child, old_child; 192 ACPI_BUFFER buf; 193 ACPI_OBJECT *pkg, *obj; 194 ACPI_STATUS status; 195 196 ctx = (struct fdc_walk_ctx *)arg; 197 buf.Pointer = NULL; 198 199 /* 200 * The first four ints are booleans that indicate whether fd0-3 are 201 * present or not. The last is for a tape device, which we don't 202 * bother supporting for now. 203 */ 204 if (ctx->index > 3) 205 return (AE_OK); 206 207 /* This device is not present, move on to the next. */ 208 if (ctx->fd_present[ctx->index] != ACPI_FD_PRESENT) 209 goto out; 210 211 /* Create a device for the child with the given index. */ 212 child = fdc_add_child(ctx->dev, "fd", ctx->index); 213 if (child == NULL) 214 goto out; 215 old_child = *dev; 216 *dev = child; 217 218 /* Get temporary buffer for _FDI probe. */ 219 buf.Length = ACPI_FDC_BUFLEN; 220 buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO); 221 if (buf.Pointer == NULL) 222 goto out; 223 224 /* 225 * Evaluate _FDI to get drive type to pass to the child. We use the 226 * old child here since it has a valid ACPI_HANDLE since it is a 227 * child of acpi. A better way to implement this would be to make fdc 228 * support the ACPI handle ivar for its children. 229 */ 230 status = ACPI_EVALUATE_OBJECT(ctx->acpi_dev, old_child, "_FDI", NULL, 231 &buf); 232 if (ACPI_FAILURE(status)) { 233 if (status != AE_NOT_FOUND) 234 device_printf(ctx->dev, "_FDI failed - %#x\n", status); 235 goto out; 236 } 237 pkg = (ACPI_OBJECT *)buf.Pointer; 238 if (!ACPI_PKG_VALID(pkg, 16)) { 239 device_printf(ctx->dev, "invalid _FDI package\n"); 240 goto out; 241 } 242 obj = &pkg->Package.Elements[1]; 243 if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) { 244 device_printf(ctx->dev, "invalid type object in _FDI\n"); 245 goto out; 246 } 247 fdc_set_fdtype(child, obj->Integer.Value); 248 249 out: 250 ctx->index++; 251 if (buf.Pointer) 252 free(buf.Pointer, M_TEMP); 253 return (AE_OK); 254 } 255 256 static device_method_t fdc_acpi_methods[] = { 257 /* Device interface */ 258 DEVMETHOD(device_probe, fdc_acpi_probe), 259 DEVMETHOD(device_attach, fdc_acpi_attach), 260 DEVMETHOD(device_detach, fdc_detach), 261 262 /* Bus interface */ 263 DEVMETHOD(bus_print_child, fdc_print_child), 264 DEVMETHOD(bus_read_ivar, fdc_read_ivar), 265 DEVMETHOD(bus_write_ivar, fdc_write_ivar), 266 267 DEVMETHOD_END 268 }; 269 270 static driver_t fdc_acpi_driver = { 271 "fdc", 272 fdc_acpi_methods, 273 sizeof(struct fdc_data) 274 }; 275 276 DRIVER_MODULE(fdc, acpi, fdc_acpi_driver, 0, 0); 277