fdc_acpi.c revision 134533
190075Sobrien/*- 290075Sobrien * Copyright (c) 2004 Nate Lawson (SDG) 390075Sobrien * All rights reserved. 490075Sobrien * 590075Sobrien * Redistribution and use in source and binary forms, with or without 6132718Skan * modification, are permitted provided that the following conditions 790075Sobrien * are met: 8132718Skan * 1. Redistributions of source code must retain the above copyright 9132718Skan * notice, this list of conditions and the following disclaimer. 10132718Skan * 2. Redistributions in binary form must reproduce the above copyright 11132718Skan * notice, this list of conditions and the following disclaimer in the 1290075Sobrien * documentation and/or other materials provided with the distribution. 13132718Skan * 14132718Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15132718Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16132718Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1790075Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18132718Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19132718Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20132718Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21132718Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2290075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2390075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2490075Sobrien * SUCH DAMAGE. 2590075Sobrien */ 2690075Sobrien 2790075Sobrien#include <sys/cdefs.h> 2890075Sobrien__FBSDID("$FreeBSD: head/sys/dev/fdc/fdc_acpi.c 134533 2004-08-30 21:13:03Z njl $"); 2990075Sobrien 30132718Skan#include <sys/param.h> 3190075Sobrien#include <sys/kernel.h> 3290075Sobrien#include <sys/bio.h> 3390075Sobrien#include <sys/bus.h> 3490075Sobrien#include <sys/malloc.h> 3590075Sobrien#include <sys/module.h> 3690075Sobrien#include <sys/proc.h> 3790075Sobrien 3890075Sobrien#include "acpi.h" 3990075Sobrien#include <dev/acpica/acpivar.h> 4090075Sobrien#include <dev/fdc/fdcvar.h> 4190075Sobrien 4290075Sobrienstatic int fdc_acpi_probe(device_t dev); 4390075Sobrienstatic int fdc_acpi_attach(device_t dev); 44117395Skanstatic int fdc_acpi_probe_children(device_t bus, device_t dev, 4590075Sobrien void *fde); 4690075Sobrienstatic ACPI_STATUS fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, 4790075Sobrien int level, void *arg); 4890075Sobrien 4990075Sobrien/* Maximum number of child devices of a controller (4 floppy + 1 tape.) */ 5090075Sobrien#define ACPI_FDC_MAXDEVS 5 5190075Sobrien 5290075Sobrien/* 5390075Sobrien * Parameters for the tape drive (5th device). Some BIOS authors use this 5490075Sobrien * for all drives, not just the tape drive (e.g., ASUS K8V). This isn't 5590075Sobrien * grossly incompatible with the spec since it says the first four devices 5690075Sobrien * are simple booleans. 5790075Sobrien */ 5890075Sobrien#define ACPI_FD_UNKNOWN 0 5990075Sobrien#define ACPI_FD_PRESENT 1 6090075Sobrien#define ACPI_FD_NEVER_PRESENT 2 6190075Sobrien 6290075Sobrien/* Temporary buf length for evaluating _FDE and _FDI. */ 6390075Sobrien#define ACPI_FDC_BUFLEN 1024 6490075Sobrien 6590075Sobrien/* Context for walking FDC child devices. */ 6690075Sobrienstruct fdc_walk_ctx { 6790075Sobrien uint32_t fd_present[ACPI_FDC_MAXDEVS]; 68 int index; 69 device_t acpi_dev; 70 device_t dev; 71}; 72 73static int 74fdc_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 90static int 91fdc_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 error = ENXIO; 141 goto out; 142 } 143 break; 144 case ACPI_TYPE_PACKAGE: 145 /* 146 * In violation of the spec, systems including the ASUS 147 * K8V return a package of five integers instead of a 148 * buffer of five 32-bit integers. 149 */ 150 fde = malloc(pkg->Package.Count * sizeof(uint32_t), 151 M_TEMP, M_NOWAIT | M_ZERO); 152 if (fde == NULL) { 153 error = ENOMEM; 154 goto out; 155 } 156 for (i = 0; i < pkg->Package.Count; i++) { 157 obj = &pkg->Package.Elements[i]; 158 if (obj->Type == ACPI_TYPE_INTEGER) 159 fde[i] = (uint32_t)obj->Integer.Value; 160 } 161 break; 162 default: 163 device_printf(dev, "invalid _FDE type %d\n", obj->Type); 164 error = ENXIO; 165 goto out; 166 } 167 error = fdc_acpi_probe_children(bus, dev, fde); 168 if (pkg->Type == ACPI_TYPE_PACKAGE) 169 free(fde, M_TEMP); 170 } else 171 error = fdc_hints_probe(dev); 172 173out: 174 if (buf.Pointer) 175 free(buf.Pointer, M_TEMP); 176 if (error != 0) 177 fdc_release_resources(sc); 178 179 return (error); 180} 181 182static int 183fdc_acpi_probe_children(device_t bus, device_t dev, void *fde) 184{ 185 struct fdc_walk_ctx *ctx; 186 devclass_t fd_dc; 187 int i; 188 189 /* Setup the context and walk all child devices. */ 190 ctx = malloc(sizeof(struct fdc_walk_ctx), M_TEMP, M_NOWAIT); 191 if (ctx == NULL) { 192 device_printf(dev, "no memory for walking children\n"); 193 return (ENOMEM); 194 } 195 bcopy(fde, ctx->fd_present, sizeof(ctx->fd_present)); 196 ctx->index = 0; 197 ctx->dev = dev; 198 ctx->acpi_dev = bus; 199 ACPI_SCAN_CHILDREN(ctx->acpi_dev, dev, 1, fdc_acpi_probe_child, 200 ctx); 201 202 /* Add any devices not represented by an AML Device handle/node. */ 203 fd_dc = devclass_find("fd"); 204 for (i = 0; i < ACPI_FDC_MAXDEVS; i++) 205 if (ctx->fd_present[i] == ACPI_FD_PRESENT && 206 devclass_get_device(fd_dc, i) == NULL) { 207 if (fdc_add_child(dev, "fd", i) == NULL) 208 device_printf(dev, "fd add failed\n"); 209 } 210 free(ctx, M_TEMP); 211 212 /* Attach any children found during the probe. */ 213 return (bus_generic_attach(dev)); 214} 215 216static ACPI_STATUS 217fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, int level, void *arg) 218{ 219 struct fdc_walk_ctx *ctx; 220 device_t child; 221 ACPI_BUFFER buf; 222 ACPI_OBJECT *pkg, *obj; 223 ACPI_STATUS status; 224 225 ctx = (struct fdc_walk_ctx *)arg; 226 buf.Pointer = NULL; 227 228 /* 229 * The first four ints are booleans that indicate whether fd0-3 are 230 * present or not. The last is for a tape device, which we don't 231 * bother supporting for now. 232 */ 233 if (ctx->index > 3) 234 return (AE_OK); 235 236 /* This device is not present, move on to the next. */ 237 if (ctx->fd_present[ctx->index] != ACPI_FD_PRESENT) 238 goto out; 239 240 /* Create a device for the child with the given index. */ 241 child = fdc_add_child(ctx->dev, "fd", ctx->index); 242 if (child == NULL) 243 goto out; 244 *dev = child; 245 246 /* Get temporary buffer for _FDI probe. */ 247 buf.Length = ACPI_FDC_BUFLEN; 248 buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO); 249 if (buf.Pointer == NULL) 250 goto out; 251 252 /* Evaluate _FDI to get drive type to pass to the child. */ 253 status = ACPI_EVALUATE_OBJECT(ctx->acpi_dev, *dev, "_FDI", NULL, &buf); 254 if (ACPI_FAILURE(status)) { 255 if (status != AE_NOT_FOUND) 256 device_printf(ctx->dev, "_FDI failed - %#x\n", status); 257 goto out; 258 } 259 pkg = (ACPI_OBJECT *)buf.Pointer; 260 if (!ACPI_PKG_VALID(pkg, 16)) { 261 device_printf(ctx->dev, "invalid _FDI package\n"); 262 goto out; 263 } 264 obj = &pkg->Package.Elements[1]; 265 if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) { 266 device_printf(ctx->dev, "invalid type object in _FDI\n"); 267 goto out; 268 } 269 fdc_set_fdtype(child, obj->Integer.Value); 270 271out: 272 ctx->index++; 273 if (buf.Pointer) 274 free(buf.Pointer, M_TEMP); 275 return (AE_OK); 276} 277 278static device_method_t fdc_acpi_methods[] = { 279 /* Device interface */ 280 DEVMETHOD(device_probe, fdc_acpi_probe), 281 DEVMETHOD(device_attach, fdc_acpi_attach), 282 DEVMETHOD(device_detach, fdc_detach), 283 284 /* Bus interface */ 285 DEVMETHOD(bus_print_child, fdc_print_child), 286 DEVMETHOD(bus_read_ivar, fdc_read_ivar), 287 DEVMETHOD(bus_write_ivar, fdc_write_ivar), 288 289 {0, 0} 290}; 291 292static driver_t fdc_acpi_driver = { 293 "fdc", 294 fdc_acpi_methods, 295 sizeof(struct fdc_data) 296}; 297 298DRIVER_MODULE(fdc, acpi, fdc_acpi_driver, fdc_devclass, 0, 0); 299