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