]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/fdc/fdc_acpi.c
Use pci_find_extcap() to search for AGP capabilities (PCIY_AGP).
[FreeBSD/FreeBSD.git] / sys / dev / fdc / fdc_acpi.c
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/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 /* Standard size of buffer returned by the _FDE method. */
53 #define ACPI_FDC_FDE_LEN        (ACPI_FDC_MAXDEVS * sizeof(uint32_t))
54
55 /*
56  * Parameters for the tape drive (5th device).  Some BIOS authors use this
57  * for all drives, not just the tape drive (e.g., ASUS K8V).  This isn't
58  * grossly incompatible with the spec since it says the first four devices
59  * are simple booleans.
60  */
61 #define ACPI_FD_UNKNOWN         0
62 #define ACPI_FD_PRESENT         1
63 #define ACPI_FD_NEVER_PRESENT   2
64
65 /* Temporary buf length for evaluating _FDE and _FDI. */
66 #define ACPI_FDC_BUFLEN         1024
67
68 /* Context for walking FDC child devices. */
69 struct fdc_walk_ctx {
70         uint32_t        fd_present[ACPI_FDC_MAXDEVS];
71         int             index;
72         device_t        acpi_dev;
73         device_t        dev;
74 };
75
76 static int
77 fdc_acpi_probe(device_t dev)
78 {
79         device_t bus;
80         static char *fdc_ids[] = { "PNP0700", "PNP0701", NULL };
81
82         bus = device_get_parent(dev);
83         if (ACPI_ID_PROBE(bus, dev, fdc_ids) == NULL)
84                 return (ENXIO);
85
86         if (ACPI_SUCCESS(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, NULL)))
87                 device_set_desc(dev, "floppy drive controller (FDE)");
88         else
89                 device_set_desc(dev, "floppy drive controller");
90         return (0);
91 }
92
93 static int
94 fdc_acpi_attach(device_t dev)
95 {
96         struct fdc_data *sc;
97         ACPI_BUFFER buf;
98         device_t bus;
99         int error, fde_count, i;
100         ACPI_OBJECT *obj, *pkg;
101         ACPI_HANDLE h;
102         uint32_t fde[ACPI_FDC_MAXDEVS];
103
104         /* Get our softc and use the same accessor as ISA. */
105         sc = device_get_softc(dev);
106         sc->fdc_dev = dev;
107
108         /* Initialize variables and get a temporary buffer for _FDE. */
109         error = ENXIO;
110         h = acpi_get_handle(dev);
111         buf.Length = ACPI_FDC_BUFLEN;
112         buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO);
113         if (buf.Pointer == NULL)
114                 goto out;
115
116         /* Allocate resources the same as the ISA attachment. */
117         error = fdc_isa_alloc_resources(dev, sc);
118         if (error != 0)
119                 goto out;
120
121         /* Call common attach code in fdc(4) first. */
122         error = fdc_attach(dev);
123         if (error != 0)
124                 goto out;
125
126         /*
127          * Enumerate _FDE, which lists floppy drives that are present.  If
128          * this fails, fall back to the ISA hints-based probe method.
129          */
130         bus = device_get_parent(dev);
131         if (ACPI_FAILURE(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, &buf))) {
132                 error = ENXIO;
133                 goto out_hintsprobe;
134         }
135
136         /* Parse the output of _FDE in various ways. */
137         obj = pkg = (ACPI_OBJECT *)buf.Pointer;
138         switch (obj->Type) {
139         case ACPI_TYPE_BUFFER:
140                 /*
141                  * The spec says _FDE should be a buffer of five 32-bit
142                  * integers.  In violation of the spec, some systems use
143                  * five bytes instead.
144                  */
145                 switch (obj->Buffer.Length) {
146                 case ACPI_FDC_FDE_LEN:
147                         bcopy(obj->Buffer.Pointer, fde, ACPI_FDC_FDE_LEN);
148                         break;
149                 case ACPI_FDC_MAXDEVS:
150                         for (i = 0; i < ACPI_FDC_MAXDEVS; i++)
151                                 fde[i] = ((uint8_t *)obj->Buffer.Pointer)[i];
152                         break;
153                 default:
154                         device_printf(dev, "_FDE wrong length: %d\n",
155                             obj->Buffer.Length);
156                         error = ENXIO;
157                         goto out_hintsprobe;
158                 }
159                 break;
160         case ACPI_TYPE_PACKAGE:
161                 /*
162                  * In violation of the spec, systems including the ASUS
163                  * K8V return a package of five integers instead of a
164                  * buffer of five 32-bit integers.
165                  */
166                 fde_count = min(ACPI_FDC_MAXDEVS, pkg->Package.Count);
167                 for (i = 0; i < fde_count; i++) {
168                         obj = &pkg->Package.Elements[i];
169                         if (obj->Type == ACPI_TYPE_INTEGER)
170                                 fde[i] = (uint32_t)obj->Integer.Value;
171                 }
172                 break;
173         default:
174                 device_printf(dev, "invalid _FDE type %d\n", obj->Type);
175                 error = ENXIO;
176                 goto out_hintsprobe;
177         }
178
179         /* Add fd child devices as specified. */
180         error = fdc_acpi_probe_children(bus, dev, fde);
181
182 out_hintsprobe:
183         /*
184          * If there was a problem with the _FDE drive enumeration, fall
185          * back to the hints-based probe.
186          */
187         if (error)
188                 error = fdc_hints_probe(dev);
189
190 out:
191         if (buf.Pointer)
192                 free(buf.Pointer, M_TEMP);
193         if (error != 0)
194                 fdc_release_resources(sc);
195
196         return (error);
197 }
198
199 static int
200 fdc_acpi_probe_children(device_t bus, device_t dev, void *fde)
201 {
202         struct fdc_walk_ctx *ctx;
203         devclass_t fd_dc;
204         int i;
205
206         /* Setup the context and walk all child devices. */
207         ctx = malloc(sizeof(struct fdc_walk_ctx), M_TEMP, M_NOWAIT);
208         if (ctx == NULL) {
209                 device_printf(dev, "no memory for walking children\n");
210                 return (ENOMEM);
211         }
212         bcopy(fde, ctx->fd_present, sizeof(ctx->fd_present));
213         ctx->index = 0;
214         ctx->dev = dev;
215         ctx->acpi_dev = bus;
216         ACPI_SCAN_CHILDREN(ctx->acpi_dev, dev, 1, fdc_acpi_probe_child,
217             ctx);
218
219         /* Add any devices not represented by an AML Device handle/node. */
220         fd_dc = devclass_find("fd");
221         for (i = 0; i < ACPI_FDC_MAXDEVS; i++)
222                 if (ctx->fd_present[i] == ACPI_FD_PRESENT &&
223                     devclass_get_device(fd_dc, i) == NULL) {
224                         if (fdc_add_child(dev, "fd", i) == NULL)
225                                 device_printf(dev, "fd add failed\n");
226                 }
227         free(ctx, M_TEMP);
228
229         /* Attach any children found during the probe. */
230         return (bus_generic_attach(dev));
231 }
232
233 static ACPI_STATUS
234 fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, int level, void *arg)
235 {
236         struct fdc_walk_ctx *ctx;
237         device_t child, old_child;
238         ACPI_BUFFER buf;
239         ACPI_OBJECT *pkg, *obj;
240         ACPI_STATUS status;
241
242         ctx = (struct fdc_walk_ctx *)arg;
243         buf.Pointer = NULL;
244
245         /*
246          * The first four ints are booleans that indicate whether fd0-3 are
247          * present or not.  The last is for a tape device, which we don't
248          * bother supporting for now.
249          */
250         if (ctx->index > 3)
251                 return (AE_OK);
252
253         /* This device is not present, move on to the next. */
254         if (ctx->fd_present[ctx->index] != ACPI_FD_PRESENT)
255                 goto out;
256
257         /* Create a device for the child with the given index. */
258         child = fdc_add_child(ctx->dev, "fd", ctx->index);
259         if (child == NULL)
260                 goto out;
261         old_child = *dev;
262         *dev = child;
263
264         /* Get temporary buffer for _FDI probe. */
265         buf.Length = ACPI_FDC_BUFLEN;
266         buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO);
267         if (buf.Pointer == NULL)
268                 goto out;
269
270         /*
271          * Evaluate _FDI to get drive type to pass to the child.  We use the
272          * old child here since it has a valid ACPI_HANDLE since it is a
273          * child of acpi.  A better way to implement this would be to make fdc
274          * support the ACPI handle ivar for its children.
275          */
276         status = ACPI_EVALUATE_OBJECT(ctx->acpi_dev, old_child, "_FDI", NULL,
277             &buf);
278         if (ACPI_FAILURE(status)) {
279                 if (status != AE_NOT_FOUND)
280                         device_printf(ctx->dev, "_FDI failed - %#x\n", status);
281                 goto out;
282         }
283         pkg = (ACPI_OBJECT *)buf.Pointer;
284         if (!ACPI_PKG_VALID(pkg, 16)) {
285                 device_printf(ctx->dev, "invalid _FDI package\n");
286                 goto out;
287         }
288         obj = &pkg->Package.Elements[1];
289         if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) {
290                 device_printf(ctx->dev, "invalid type object in _FDI\n");
291                 goto out;
292         }
293         fdc_set_fdtype(child, obj->Integer.Value);
294
295 out:
296         ctx->index++;
297         if (buf.Pointer)
298                 free(buf.Pointer, M_TEMP);
299         return (AE_OK);
300 }
301
302 static device_method_t fdc_acpi_methods[] = {
303         /* Device interface */
304         DEVMETHOD(device_probe,         fdc_acpi_probe),
305         DEVMETHOD(device_attach,        fdc_acpi_attach),
306         DEVMETHOD(device_detach,        fdc_detach),
307
308         /* Bus interface */
309         DEVMETHOD(bus_print_child,      fdc_print_child),
310         DEVMETHOD(bus_read_ivar,        fdc_read_ivar),
311         DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
312
313         {0, 0}
314 };
315
316 static driver_t fdc_acpi_driver = {
317         "fdc",
318         fdc_acpi_methods,
319         sizeof(struct fdc_data)
320 };
321
322 DRIVER_MODULE(fdc, acpi, fdc_acpi_driver, fdc_devclass, 0, 0);