]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/acpica/acpi_ec.c
This commit was generated by cvs2svn to compensate for changes in r167805,
[FreeBSD/FreeBSD.git] / sys / dev / acpica / acpi_ec.c
1 /*-
2  * Copyright (c) 2003 Nate Lawson
3  * Copyright (c) 2000 Michael Smith
4  * Copyright (c) 2000 BSDi
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  ******************************************************************************
30  *
31  * 1. Copyright Notice
32  *
33  * Some or all of this work - Copyright (c) 1999, Intel Corp.  All rights
34  * reserved.
35  *
36  * 2. License
37  *
38  * 2.1. This is your license from Intel Corp. under its intellectual property
39  * rights.  You may have additional license terms from the party that provided
40  * you this software, covering your right to use that party's intellectual
41  * property rights.
42  *
43  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
44  * copy of the source code appearing in this file ("Covered Code") an
45  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
46  * base code distributed originally by Intel ("Original Intel Code") to copy,
47  * make derivatives, distribute, use and display any portion of the Covered
48  * Code in any form, with the right to sublicense such rights; and
49  *
50  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
51  * license (with the right to sublicense), under only those claims of Intel
52  * patents that are infringed by the Original Intel Code, to make, use, sell,
53  * offer to sell, and import the Covered Code and derivative works thereof
54  * solely to the minimum extent necessary to exercise the above copyright
55  * license, and in no event shall the patent license extend to any additions
56  * to or modifications of the Original Intel Code.  No other license or right
57  * is granted directly or by implication, estoppel or otherwise;
58  *
59  * The above copyright and patent license is granted only if the following
60  * conditions are met:
61  *
62  * 3. Conditions 
63  *
64  * 3.1. Redistribution of Source with Rights to Further Distribute Source.  
65  * Redistribution of source code of any substantial portion of the Covered
66  * Code or modification with rights to further distribute source must include
67  * the above Copyright Notice, the above License, this list of Conditions,
68  * and the following Disclaimer and Export Compliance provision.  In addition,
69  * Licensee must cause all Covered Code to which Licensee contributes to
70  * contain a file documenting the changes Licensee made to create that Covered
71  * Code and the date of any change.  Licensee must include in that file the
72  * documentation of any changes made by any predecessor Licensee.  Licensee 
73  * must include a prominent statement that the modification is derived,
74  * directly or indirectly, from Original Intel Code.
75  *
76  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.  
77  * Redistribution of source code of any substantial portion of the Covered
78  * Code or modification without rights to further distribute source must
79  * include the following Disclaimer and Export Compliance provision in the
80  * documentation and/or other materials provided with distribution.  In
81  * addition, Licensee may not authorize further sublicense of source of any
82  * portion of the Covered Code, and must include terms to the effect that the
83  * license from Licensee to its licensee is limited to the intellectual
84  * property embodied in the software Licensee provides to its licensee, and
85  * not to intellectual property embodied in modifications its licensee may
86  * make.
87  *
88  * 3.3. Redistribution of Executable. Redistribution in executable form of any
89  * substantial portion of the Covered Code or modification must reproduce the
90  * above Copyright Notice, and the following Disclaimer and Export Compliance
91  * provision in the documentation and/or other materials provided with the
92  * distribution.
93  *
94  * 3.4. Intel retains all right, title, and interest in and to the Original
95  * Intel Code.
96  *
97  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
98  * Intel shall be used in advertising or otherwise to promote the sale, use or
99  * other dealings in products derived from or relating to the Covered Code
100  * without prior written authorization from Intel.
101  *
102  * 4. Disclaimer and Export Compliance
103  *
104  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
105  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
106  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
107  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
108  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
109  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
110  * PARTICULAR PURPOSE. 
111  *
112  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
113  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
114  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
115  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
116  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
117  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
118  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
119  * LIMITED REMEDY.
120  *
121  * 4.3. Licensee shall not export, either directly or indirectly, any of this
122  * software or system incorporating such software without first obtaining any
123  * required license or other approval from the U. S. Department of Commerce or
124  * any other agency or department of the United States Government.  In the
125  * event Licensee exports any such software from the United States or
126  * re-exports any such software from a foreign destination, Licensee shall
127  * ensure that the distribution and export/re-export of the software is in
128  * compliance with all laws, regulations, orders, or other restrictions of the
129  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
130  * any of its subsidiaries will export/re-export any technical data, process,
131  * software, or service, directly or indirectly, to any country for which the
132  * United States government or any agency thereof requires an export license,
133  * other governmental approval, or letter of assurance, without first obtaining
134  * such license, approval or letter.
135  *
136  *****************************************************************************/
137
138 #include <sys/cdefs.h>
139 __FBSDID("$FreeBSD$");
140
141 #include "opt_acpi.h"
142 #include <sys/param.h>
143 #include <sys/kernel.h>
144 #include <sys/bus.h>
145 #include <sys/malloc.h>
146 #include <sys/module.h>
147 #include <sys/sx.h>
148
149 #include <machine/bus.h>
150 #include <machine/resource.h>
151 #include <sys/rman.h>
152
153 #include <contrib/dev/acpica/acpi.h>
154 #include <dev/acpica/acpivar.h>
155
156 /* Hooks for the ACPI CA debugging infrastructure */
157 #define _COMPONENT      ACPI_EC
158 ACPI_MODULE_NAME("EC")
159
160 /*
161  * EC_COMMAND:
162  * -----------
163  */
164 typedef UINT8                           EC_COMMAND;
165
166 #define EC_COMMAND_UNKNOWN              ((EC_COMMAND) 0x00)
167 #define EC_COMMAND_READ                 ((EC_COMMAND) 0x80)
168 #define EC_COMMAND_WRITE                ((EC_COMMAND) 0x81)
169 #define EC_COMMAND_BURST_ENABLE         ((EC_COMMAND) 0x82)
170 #define EC_COMMAND_BURST_DISABLE        ((EC_COMMAND) 0x83)
171 #define EC_COMMAND_QUERY                ((EC_COMMAND) 0x84)
172
173 /* 
174  * EC_STATUS:
175  * ----------
176  * The encoding of the EC status register is illustrated below.
177  * Note that a set bit (1) indicates the property is TRUE
178  * (e.g. if bit 0 is set then the output buffer is full).
179  * +-+-+-+-+-+-+-+-+
180  * |7|6|5|4|3|2|1|0|
181  * +-+-+-+-+-+-+-+-+
182  *  | | | | | | | |
183  *  | | | | | | | +- Output Buffer Full?
184  *  | | | | | | +--- Input Buffer Full?
185  *  | | | | | +----- <reserved>
186  *  | | | | +------- Data Register is Command Byte?
187  *  | | | +--------- Burst Mode Enabled?
188  *  | | +----------- SCI Event?
189  *  | +------------- SMI Event?
190  *  +--------------- <reserved>
191  *
192  */
193 typedef UINT8                           EC_STATUS;
194
195 #define EC_FLAG_OUTPUT_BUFFER           ((EC_STATUS) 0x01)
196 #define EC_FLAG_INPUT_BUFFER            ((EC_STATUS) 0x02)
197 #define EC_FLAG_DATA_IS_CMD             ((EC_STATUS) 0x08)
198 #define EC_FLAG_BURST_MODE              ((EC_STATUS) 0x10)
199
200 /*
201  * EC_EVENT:
202  * ---------
203  */
204 typedef UINT8                           EC_EVENT;
205
206 #define EC_EVENT_UNKNOWN                ((EC_EVENT) 0x00)
207 #define EC_EVENT_OUTPUT_BUFFER_FULL     ((EC_EVENT) 0x01)
208 #define EC_EVENT_INPUT_BUFFER_EMPTY     ((EC_EVENT) 0x02)
209 #define EC_EVENT_SCI                    ((EC_EVENT) 0x20)
210 #define EC_EVENT_SMI                    ((EC_EVENT) 0x40)
211
212 /* Data byte returned after burst enable indicating it was successful. */
213 #define EC_BURST_ACK                    0x90
214
215 /*
216  * Register access primitives
217  */
218 #define EC_GET_DATA(sc)                                                 \
219         bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
220
221 #define EC_SET_DATA(sc, v)                                              \
222         bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
223
224 #define EC_GET_CSR(sc)                                                  \
225         bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
226
227 #define EC_SET_CSR(sc, v)                                               \
228         bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
229
230 /* Embedded Controller Boot Resources Table (ECDT) */
231 typedef struct {
232     ACPI_TABLE_HEADER           header;
233     ACPI_GENERIC_ADDRESS        control;
234     ACPI_GENERIC_ADDRESS        data;
235     UINT32                      uid;
236     UINT8                       gpe_bit;
237     char                        ec_id[0];
238 } ACPI_TABLE_ECDT;
239
240 /* Additional params to pass from the probe routine */
241 struct acpi_ec_params {
242     int         glk;
243     int         gpe_bit;
244     ACPI_HANDLE gpe_handle;
245     int         uid;
246 };
247
248 /* Indicate that this device has already been probed via ECDT. */
249 #define DEV_ECDT(x)             (acpi_get_magic(x) == (int)&acpi_ec_devclass)
250
251 /*
252  * Driver softc.
253  */
254 struct acpi_ec_softc {
255     device_t            ec_dev;
256     ACPI_HANDLE         ec_handle;
257     int                 ec_uid;
258     ACPI_HANDLE         ec_gpehandle;
259     UINT8               ec_gpebit;
260     UINT8               ec_csrvalue;
261     
262     int                 ec_data_rid;
263     struct resource     *ec_data_res;
264     bus_space_tag_t     ec_data_tag;
265     bus_space_handle_t  ec_data_handle;
266
267     int                 ec_csr_rid;
268     struct resource     *ec_csr_res;
269     bus_space_tag_t     ec_csr_tag;
270     bus_space_handle_t  ec_csr_handle;
271
272     struct mtx          ec_mtx;
273     int                 ec_glk;
274     int                 ec_glkhandle;
275     int                 ec_burstactive;
276     int                 ec_sci_pend;
277 };
278
279 /*
280  * XXX njl
281  * I couldn't find it in the spec but other implementations also use a
282  * value of 1 ms for the time to acquire global lock.
283  */
284 #define EC_LOCK_TIMEOUT 1000
285
286 /* Default delay in microseconds between each run of the status polling loop. */
287 #define EC_POLL_DELAY   10
288
289 /* Default time in microseconds spent polling before sleep waiting. */
290 #define EC_POLL_TIME    500
291
292 /* Total time in ms spent waiting for a response from EC. */
293 #define EC_TIMEOUT      500
294
295 #define EVENT_READY(event, status)                      \
296         (((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&     \
297          ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||    \
298          ((event) == EC_EVENT_INPUT_BUFFER_EMPTY &&     \
299          ((status) & EC_FLAG_INPUT_BUFFER) == 0))
300
301 ACPI_SERIAL_DECL(ec, "ACPI embedded controller");
302
303 SYSCTL_DECL(_debug_acpi);
304 SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging");
305
306 static int      ec_burst_mode;
307 TUNABLE_INT("debug.acpi.ec.burst", &ec_burst_mode);
308 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RW, &ec_burst_mode, 0,
309     "Enable use of burst mode (faster for nearly all systems)");
310 static int      ec_poll_time = EC_POLL_TIME;
311 TUNABLE_INT("debug.acpi.ec.poll_time", &ec_poll_time);
312 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, poll_time, CTLFLAG_RW, &ec_poll_time,
313     EC_POLL_TIME, "Time spent polling vs. sleeping (CPU intensive)");
314 static int      ec_timeout = EC_TIMEOUT;
315 TUNABLE_INT("debug.acpi.ec.timeout", &ec_timeout);
316 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RW, &ec_timeout,
317     EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
318
319 static __inline ACPI_STATUS
320 EcLock(struct acpi_ec_softc *sc, int serialize)
321 {
322     ACPI_STATUS status;
323
324     /*
325      * If caller is executing a series of commands, acquire the exclusive lock
326      * to serialize with other users.
327      * To sync with bottom-half interrupt handler, always acquire the mutex.
328      */
329     status = AE_OK;
330     if (serialize)
331         ACPI_SERIAL_BEGIN(ec);
332     mtx_lock(&sc->ec_mtx);
333
334     /* If _GLK is non-zero, also acquire the global lock. */
335     if (sc->ec_glk) {
336         status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
337         if (ACPI_FAILURE(status)) {
338             mtx_unlock(&sc->ec_mtx);
339             if (serialize)
340                 ACPI_SERIAL_END(ec);
341         }
342     }
343
344     return (status);
345 }
346
347 static __inline void
348 EcUnlock(struct acpi_ec_softc *sc)
349 {
350     if (sc->ec_glk)
351         AcpiReleaseGlobalLock(sc->ec_glkhandle);
352     mtx_unlock(&sc->ec_mtx);
353     if (sx_xlocked(&ec_sxlock))
354         ACPI_SERIAL_END(ec);
355 }
356
357 static uint32_t         EcGpeHandler(void *Context);
358 static ACPI_STATUS      EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, 
359                                 void *Context, void **return_Context);
360 static ACPI_STATUS      EcSpaceHandler(UINT32 Function,
361                                 ACPI_PHYSICAL_ADDRESS Address,
362                                 UINT32 width, ACPI_INTEGER *Value,
363                                 void *Context, void *RegionContext);
364 static ACPI_STATUS      EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
365 static ACPI_STATUS      EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
366 static ACPI_STATUS      EcRead(struct acpi_ec_softc *sc, UINT8 Address,
367                                 UINT8 *Data);
368 static ACPI_STATUS      EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
369                                 UINT8 *Data);
370 static int              acpi_ec_probe(device_t dev);
371 static int              acpi_ec_attach(device_t dev);
372 static int              acpi_ec_shutdown(device_t dev);
373 static int              acpi_ec_read_method(device_t dev, u_int addr,
374                                 ACPI_INTEGER *val, int width);
375 static int              acpi_ec_write_method(device_t dev, u_int addr,
376                                 ACPI_INTEGER val, int width);
377
378 static device_method_t acpi_ec_methods[] = {
379     /* Device interface */
380     DEVMETHOD(device_probe,     acpi_ec_probe),
381     DEVMETHOD(device_attach,    acpi_ec_attach),
382     DEVMETHOD(device_shutdown,  acpi_ec_shutdown),
383
384     /* Embedded controller interface */
385     DEVMETHOD(acpi_ec_read,     acpi_ec_read_method),
386     DEVMETHOD(acpi_ec_write,    acpi_ec_write_method),
387
388     {0, 0}
389 };
390
391 static driver_t acpi_ec_driver = {
392     "acpi_ec",
393     acpi_ec_methods,
394     sizeof(struct acpi_ec_softc),
395 };
396
397 static devclass_t acpi_ec_devclass;
398 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
399 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
400
401 /*
402  * Look for an ECDT and if we find one, set up default GPE and 
403  * space handlers to catch attempts to access EC space before
404  * we have a real driver instance in place.
405  * TODO: if people report invalid ECDTs, add a tunable to disable them.
406  */
407 void
408 acpi_ec_ecdt_probe(device_t parent)
409 {
410     ACPI_TABLE_ECDT *ecdt;
411     ACPI_TABLE_HEADER *hdr;
412     ACPI_STATUS      status;
413     device_t         child;
414     ACPI_HANDLE      h;
415     struct acpi_ec_params *params;
416
417     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
418
419     /* Find and validate the ECDT. */
420     status = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING, &hdr);
421     ecdt = (ACPI_TABLE_ECDT *)hdr;
422     if (ACPI_FAILURE(status) ||
423         ecdt->control.RegisterBitWidth != 8 ||
424         ecdt->data.RegisterBitWidth != 8) {
425         return;
426     }
427
428     /* Create the child device with the given unit number. */
429     child = BUS_ADD_CHILD(parent, 0, "acpi_ec", ecdt->uid);
430     if (child == NULL) {
431         printf("%s: can't add child\n", __func__);
432         return;
433     }
434
435     /* Find and save the ACPI handle for this device. */
436     status = AcpiGetHandle(NULL, ecdt->ec_id, &h);
437     if (ACPI_FAILURE(status)) {
438         device_delete_child(parent, child);
439         printf("%s: can't get handle\n", __func__);
440         return;
441     }
442     acpi_set_handle(child, h);
443
444     /* Set the data and CSR register addresses. */
445     bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->data.Address,
446         /*count*/1);
447     bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->control.Address,
448         /*count*/1);
449
450     /*
451      * Store values for the probe/attach routines to use.  Store the
452      * ECDT GPE bit and set the global lock flag according to _GLK.
453      * Note that it is not perfectly correct to be evaluating a method
454      * before initializing devices, but in practice this function
455      * should be safe to call at this point.
456      */
457     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
458     params->gpe_handle = NULL;
459     params->gpe_bit = ecdt->gpe_bit;
460     params->uid = ecdt->uid;
461     acpi_GetInteger(h, "_GLK", &params->glk);
462     acpi_set_private(child, params);
463     acpi_set_magic(child, (int)&acpi_ec_devclass);
464
465     /* Finish the attach process. */
466     if (device_probe_and_attach(child) != 0)
467         device_delete_child(parent, child);
468 }
469
470 static int
471 acpi_ec_probe(device_t dev)
472 {
473     ACPI_BUFFER buf;
474     ACPI_HANDLE h;
475     ACPI_OBJECT *obj;
476     ACPI_STATUS status;
477     device_t    peer;
478     char        desc[64];
479     int         ret;
480     struct acpi_ec_params *params;
481     static char *ec_ids[] = { "PNP0C09", NULL };
482
483     /* Check that this is a device and that EC is not disabled. */
484     if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
485         return (ENXIO);
486
487     /*
488      * If probed via ECDT, set description and continue.  Otherwise,
489      * we can access the namespace and make sure this is not a
490      * duplicate probe.
491      */
492     ret = ENXIO;
493     params = NULL;
494     buf.Pointer = NULL;
495     buf.Length = ACPI_ALLOCATE_BUFFER;
496     if (DEV_ECDT(dev)) {
497         params = acpi_get_private(dev);
498         ret = 0;
499     } else if (!acpi_disabled("ec") &&
500         ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) {
501         params = malloc(sizeof(struct acpi_ec_params), M_TEMP,
502                         M_WAITOK | M_ZERO);
503         h = acpi_get_handle(dev);
504
505         /*
506          * Read the unit ID to check for duplicate attach and the
507          * global lock value to see if we should acquire it when
508          * accessing the EC.
509          */
510         status = acpi_GetInteger(h, "_UID", &params->uid);
511         if (ACPI_FAILURE(status))
512             params->uid = 0;
513         status = acpi_GetInteger(h, "_GLK", &params->glk);
514         if (ACPI_FAILURE(status))
515             params->glk = 0;
516
517         /*
518          * Evaluate the _GPE method to find the GPE bit used by the EC to
519          * signal status (SCI).  If it's a package, it contains a reference
520          * and GPE bit, similar to _PRW.
521          */
522         status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
523         if (ACPI_FAILURE(status)) {
524             device_printf(dev, "can't evaluate _GPE - %s\n",
525                           AcpiFormatException(status));
526             goto out;
527         }
528         obj = (ACPI_OBJECT *)buf.Pointer;
529         if (obj == NULL)
530             goto out;
531
532         switch (obj->Type) {
533         case ACPI_TYPE_INTEGER:
534             params->gpe_handle = NULL;
535             params->gpe_bit = obj->Integer.Value;
536             break;
537         case ACPI_TYPE_PACKAGE:
538             if (!ACPI_PKG_VALID(obj, 2))
539                 goto out;
540             params->gpe_handle =
541                 acpi_GetReference(NULL, &obj->Package.Elements[0]);
542             if (params->gpe_handle == NULL ||
543                 acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
544                 goto out;
545             break;
546         default:
547             device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
548             goto out;
549         }
550
551         /* Store the values we got from the namespace for attach. */
552         acpi_set_private(dev, params);
553
554         /*
555          * Check for a duplicate probe.  This can happen when a probe
556          * via ECDT succeeded already.  If this is a duplicate, disable
557          * this device.
558          */
559         peer = devclass_get_device(acpi_ec_devclass, params->uid);
560         if (peer == NULL || !device_is_alive(peer))
561             ret = 0;
562         else
563             device_disable(dev);
564     }
565
566 out:
567     if (ret == 0) {
568         snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
569                  params->gpe_bit, (params->glk) ? ", GLK" : "",
570                  DEV_ECDT(dev) ? ", ECDT" : "");
571         device_set_desc_copy(dev, desc);
572     }
573
574     if (ret > 0 && params)
575         free(params, M_TEMP);
576     if (buf.Pointer)
577         AcpiOsFree(buf.Pointer);
578     return (ret);
579 }
580
581 static int
582 acpi_ec_attach(device_t dev)
583 {
584     struct acpi_ec_softc        *sc;
585     struct acpi_ec_params       *params;
586     ACPI_STATUS                 Status;
587
588     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
589
590     /* Fetch/initialize softc (assumes softc is pre-zeroed). */
591     sc = device_get_softc(dev);
592     params = acpi_get_private(dev);
593     sc->ec_dev = dev;
594     sc->ec_handle = acpi_get_handle(dev);
595     mtx_init(&sc->ec_mtx, "ACPI EC lock", NULL, MTX_DEF);
596
597     /* Retrieve previously probed values via device ivars. */
598     sc->ec_glk = params->glk;
599     sc->ec_gpebit = params->gpe_bit;
600     sc->ec_gpehandle = params->gpe_handle;
601     sc->ec_uid = params->uid;
602     free(params, M_TEMP);
603
604     /* Attach bus resources for data and command/status ports. */
605     sc->ec_data_rid = 0;
606     sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
607                         &sc->ec_data_rid, RF_ACTIVE);
608     if (sc->ec_data_res == NULL) {
609         device_printf(dev, "can't allocate data port\n");
610         goto error;
611     }
612     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
613     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
614
615     sc->ec_csr_rid = 1;
616     sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
617                         &sc->ec_csr_rid, RF_ACTIVE);
618     if (sc->ec_csr_res == NULL) {
619         device_printf(dev, "can't allocate command/status port\n");
620         goto error;
621     }
622     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
623     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
624
625     /*
626      * Install a handler for this EC's GPE bit.  We want edge-triggered
627      * behavior.
628      */
629     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
630     Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
631                 ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc);
632     if (ACPI_FAILURE(Status)) {
633         device_printf(dev, "can't install GPE handler for %s - %s\n",
634                       acpi_name(sc->ec_handle), AcpiFormatException(Status));
635         goto error;
636     }
637
638     /* 
639      * Install address space handler
640      */
641     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
642     Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
643                 &EcSpaceHandler, &EcSpaceSetup, sc);
644     if (ACPI_FAILURE(Status)) {
645         device_printf(dev, "can't install address space handler for %s - %s\n",
646                       acpi_name(sc->ec_handle), AcpiFormatException(Status));
647         goto error;
648     }
649
650     /* Enable runtime GPEs for the handler. */
651     Status = AcpiSetGpeType(sc->ec_gpehandle, sc->ec_gpebit,
652                             ACPI_GPE_TYPE_RUNTIME);
653     if (ACPI_FAILURE(Status)) {
654         device_printf(dev, "AcpiSetGpeType failed: %s\n",
655                       AcpiFormatException(Status));
656         goto error;
657     }
658     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
659     if (ACPI_FAILURE(Status)) {
660         device_printf(dev, "AcpiEnableGpe failed: %s\n",
661                       AcpiFormatException(Status));
662         goto error;
663     }
664
665     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
666     return (0);
667
668 error:
669     AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, &EcGpeHandler);
670     AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
671         EcSpaceHandler);
672     if (sc->ec_csr_res)
673         bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid, 
674                              sc->ec_csr_res);
675     if (sc->ec_data_res)
676         bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
677                              sc->ec_data_res);
678     mtx_destroy(&sc->ec_mtx);
679     return (ENXIO);
680 }
681
682 static int
683 acpi_ec_shutdown(device_t dev)
684 {
685     struct acpi_ec_softc        *sc;
686
687     /* Disable the GPE so we don't get EC events during shutdown. */
688     sc = device_get_softc(dev);
689     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
690     return (0);
691 }
692
693 /* Methods to allow other devices (e.g., smbat) to read/write EC space. */
694 static int
695 acpi_ec_read_method(device_t dev, u_int addr, ACPI_INTEGER *val, int width)
696 {
697     struct acpi_ec_softc *sc;
698     ACPI_STATUS status;
699
700     sc = device_get_softc(dev);
701     status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
702     if (ACPI_FAILURE(status))
703         return (ENXIO);
704     return (0);
705 }
706
707 static int
708 acpi_ec_write_method(device_t dev, u_int addr, ACPI_INTEGER val, int width)
709 {
710     struct acpi_ec_softc *sc;
711     ACPI_STATUS status;
712
713     sc = device_get_softc(dev);
714     status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
715     if (ACPI_FAILURE(status))
716         return (ENXIO);
717     return (0);
718 }
719
720 static void
721 EcGpeQueryHandler(void *Context)
722 {
723     struct acpi_ec_softc        *sc = (struct acpi_ec_softc *)Context;
724     UINT8                       Data;
725     ACPI_STATUS                 Status;
726     char                        qxx[5];
727
728     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
729     KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
730
731     /* Serialize user access with EcSpaceHandler(). */
732     Status = EcLock(sc, TRUE);
733     if (ACPI_FAILURE(Status)) {
734         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
735                     "GpeQuery lock error: %s\n", AcpiFormatException(Status));
736         return;
737     }
738
739     /*
740      * Send a query command to the EC to find out which _Qxx call it
741      * wants to make.  This command clears the SCI bit and also the
742      * interrupt source since we are edge-triggered.
743      */
744     Status = EcCommand(sc, EC_COMMAND_QUERY);
745     if (ACPI_FAILURE(Status)) {
746         EcUnlock(sc);
747         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
748                     "GPE query failed - %s\n", AcpiFormatException(Status));
749         goto re_enable;
750     }
751     Data = EC_GET_DATA(sc);
752     sc->ec_sci_pend = FALSE;
753
754     /* Drop locks before evaluating _Qxx method since it may trigger GPEs. */
755     EcUnlock(sc);
756
757     /* Ignore the value for "no outstanding event". (13.3.5) */
758     CTR2(KTR_ACPI, "ec query ok,%s running _Q%02x", Data ? "" : " not", Data);
759     if (Data == 0)
760         goto re_enable;
761
762     /* Evaluate _Qxx to respond to the controller. */
763     snprintf(qxx, sizeof(qxx), "_Q%02x", Data);
764     AcpiUtStrupr(qxx);
765     Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
766     if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
767         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
768                     "evaluation of GPE query method %s failed - %s\n", 
769                     qxx, AcpiFormatException(Status));
770     }
771
772 re_enable:
773     /* Re-enable the GPE event so we'll get future requests. */
774     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR);
775     if (ACPI_FAILURE(Status))
776         printf("EcGpeQueryHandler: AcpiEnableEvent failed\n");
777 }
778
779 /*
780  * Handle a GPE.  Currently we only handle SCI events as others must
781  * be handled by polling in EcWaitEvent().  This is because some ECs
782  * treat events as level when they should be edge-triggered.
783  */
784 static uint32_t
785 EcGpeHandler(void *Context)
786 {
787     struct acpi_ec_softc *sc = Context;
788     ACPI_STATUS                Status;
789     EC_STATUS                  EcStatus;
790     int                        query_pend;
791
792     KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
793
794     /*
795      * Disable further GPEs while we handle this one.  Since we are directly
796      * called by ACPI-CA and it may have unknown locks held, we specify the
797      * ACPI_ISR flag to keep it from acquiring any more mutexes (although
798      * sleeping would be ok since we're in an ithread.)
799      */
800     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR);
801
802     /* For interrupt (GPE) handler, don't acquire serialization lock. */
803     Status = EcLock(sc, FALSE);
804     if (ACPI_FAILURE(Status)) {
805         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
806                     "GpeQuery lock error: %s\n", AcpiFormatException(Status));
807         return (-1);
808     }
809
810     /*
811      * If burst was active, but the status bit was cleared, the EC had to
812      * exit burst mode for some reason.  Record this for later.
813      */
814     EcStatus = EC_GET_CSR(sc);
815     if (sc->ec_burstactive && (EcStatus & EC_FLAG_BURST_MODE) == 0) {
816         CTR0(KTR_ACPI, "ec burst disabled in query handler");
817         sc->ec_burstactive = FALSE;
818     }
819
820     /*
821      * If the EC_SCI bit of the status register is not set, then pass
822      * it along to any potential waiters as it may be an IBE/OBF event.
823      * If it is set, queue a query handler.
824      */
825     query_pend = FALSE;
826     if ((EcStatus & EC_EVENT_SCI) == 0) {
827         CTR1(KTR_ACPI, "ec event was IBE/OBF, status %#x", EcStatus);
828         sc->ec_csrvalue = EcStatus;
829         wakeup(&sc->ec_csrvalue);
830     } else if (!sc->ec_sci_pend) {
831         /* SCI bit set and no pending query handler, so schedule one. */
832         CTR0(KTR_ACPI, "ec queueing gpe handler");
833         Status = AcpiOsQueueForExecution(OSD_PRIORITY_GPE, EcGpeQueryHandler,
834             Context);
835         if (ACPI_SUCCESS(Status)) {
836             sc->ec_sci_pend = TRUE;
837             query_pend = TRUE;
838         } else
839             printf("Queuing GPE query handler failed.\n");
840     }
841
842     /*
843      * If we didn't queue a query handler, which will eventually re-enable
844      * the GPE, re-enable it right now so we can get more events.
845      */
846     if (!query_pend) {
847         Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR);
848         if (ACPI_FAILURE(Status))
849             printf("EcGpeHandler: AcpiEnableGpe failed\n");
850     }
851
852     EcUnlock(sc);
853     return (0);
854 }
855
856 static ACPI_STATUS
857 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
858              void **RegionContext)
859 {
860
861     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
862
863     /*
864      * If deactivating a region, always set the output to NULL.  Otherwise,
865      * just pass the context through.
866      */
867     if (Function == ACPI_REGION_DEACTIVATE)
868         *RegionContext = NULL;
869     else
870         *RegionContext = Context;
871
872     return_ACPI_STATUS (AE_OK);
873 }
874
875 static ACPI_STATUS
876 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
877                ACPI_INTEGER *Value, void *Context, void *RegionContext)
878 {
879     struct acpi_ec_softc        *sc = (struct acpi_ec_softc *)Context;
880     ACPI_STATUS                 Status;
881     UINT8                       EcAddr, EcData;
882     int                         i;
883
884     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
885
886     if (width % 8 != 0 || Value == NULL || Context == NULL)
887         return_ACPI_STATUS (AE_BAD_PARAMETER);
888     if (Address + (width / 8) - 1 > 0xFF)
889         return_ACPI_STATUS (AE_BAD_ADDRESS);
890
891     if (Function == ACPI_READ)
892         *Value = 0;
893     EcAddr = Address;
894     Status = AE_ERROR;
895
896     /* Grab serialization lock to hold across command sequence. */
897     Status = EcLock(sc, TRUE);
898     if (ACPI_FAILURE(Status))
899         return_ACPI_STATUS (Status);
900
901     /* Perform the transaction(s), based on width. */
902     for (i = 0; i < width; i += 8, EcAddr++) {
903         switch (Function) {
904         case ACPI_READ:
905             Status = EcRead(sc, EcAddr, &EcData);
906             if (ACPI_SUCCESS(Status))
907                 *Value |= ((ACPI_INTEGER)EcData) << i;
908             break;
909         case ACPI_WRITE:
910             EcData = (UINT8)((*Value) >> i);
911             Status = EcWrite(sc, EcAddr, &EcData);
912             break;
913         default:
914             device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n",
915                           Function);
916             Status = AE_BAD_PARAMETER;
917             break;
918         }
919         if (ACPI_FAILURE(Status))
920             break;
921     }
922
923     EcUnlock(sc);
924     return_ACPI_STATUS (Status);
925 }
926
927 static ACPI_STATUS
928 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
929 {
930     EC_STATUS   EcStatus;
931     ACPI_STATUS Status;
932     int         count, i, retval, slp_ival;
933
934     ACPI_SERIAL_ASSERT(ec);
935     Status = AE_NO_HARDWARE_RESPONSE;
936     EcStatus = 0;
937
938     /*
939      * Poll for up to ec_poll_time microseconds since many ECs complete
940      * the command quickly, especially if in burst mode.
941      */
942 #if 0 /* Enable this as a possible workaround if EC times out. */
943     AcpiOsStall(EC_POLL_DELAY);
944 #endif
945     count = ec_poll_time / EC_POLL_DELAY;
946     if (count <= 0)
947         count = 1;
948     for (i = 0; i < count; i++) {
949         EcStatus = EC_GET_CSR(sc);
950         if (sc->ec_burstactive && (EcStatus & EC_FLAG_BURST_MODE) == 0) {
951             CTR0(KTR_ACPI, "ec burst disabled in waitevent (poll)");
952             sc->ec_burstactive = FALSE;
953         }
954         if (EVENT_READY(Event, EcStatus)) {
955             CTR1(KTR_ACPI, "ec poll wait ready, status %#x", EcStatus);
956             Status = AE_OK;
957             break;
958         }
959         AcpiOsStall(EC_POLL_DELAY);
960     }
961
962     /*
963      * If we still don't have a response and we're up and running, wait up
964      * to ec_timeout ms for completion, sleeping for chunks of 1 ms or the
965      * smallest resolution hz supports.
966      */
967     slp_ival = 0;
968     if (Status != AE_OK) {
969         retval = ENXIO;
970         if (!cold) {
971             slp_ival = hz / 1000;
972             if (slp_ival != 0) {
973                 count = ec_timeout / slp_ival;
974             } else {
975                 /* hz has less than 1000 Hz resolution so scale timeout. */
976                 slp_ival = 1;
977                 count = ec_timeout / (1000 / hz);
978             }
979         } else
980             count = ec_timeout;
981         for (i = 0; i < count; i++) {
982             if (retval != 0)
983                 EcStatus = EC_GET_CSR(sc);
984             else
985                 EcStatus = sc->ec_csrvalue;
986             if (sc->ec_burstactive && (EcStatus & EC_FLAG_BURST_MODE) == 0) {
987                 CTR0(KTR_ACPI, "ec burst disabled in waitevent (slp)");
988                 sc->ec_burstactive = FALSE;
989             }
990             if (EVENT_READY(Event, EcStatus)) {
991                 CTR1(KTR_ACPI, "ec sleep wait ready, status %#x", EcStatus);
992                 Status = AE_OK;
993                 break;
994             }
995             if (!cold) {
996                 retval = msleep(&sc->ec_csrvalue, &sc->ec_mtx, PZERO, "ecpoll",
997                     slp_ival);
998             } else
999                 AcpiOsStall(1000);
1000         }
1001     }
1002
1003     return (Status);
1004 }    
1005
1006 static ACPI_STATUS
1007 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
1008 {
1009     ACPI_STATUS status;
1010     EC_EVENT    event;
1011     EC_STATUS   ec_status;
1012
1013     ACPI_SERIAL_ASSERT(ec);
1014
1015     /* Don't use burst mode if user disabled it. */
1016     if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
1017         return (AE_ERROR);
1018
1019     /* Decide what to wait for based on command type. */
1020     switch (cmd) {
1021     case EC_COMMAND_READ:
1022     case EC_COMMAND_WRITE:
1023     case EC_COMMAND_BURST_DISABLE:
1024         event = EC_EVENT_INPUT_BUFFER_EMPTY;
1025         break;
1026     case EC_COMMAND_QUERY:
1027     case EC_COMMAND_BURST_ENABLE:
1028         event = EC_EVENT_OUTPUT_BUFFER_FULL;
1029         break;
1030     default:
1031         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
1032                     "EcCommand: Invalid command %#x\n", cmd);
1033         return (AE_BAD_PARAMETER);
1034     }
1035
1036     /* Run the command and wait for the chosen event. */
1037     CTR1(KTR_ACPI, "ec running command %#x", cmd);
1038     EC_SET_CSR(sc, cmd);
1039     status = EcWaitEvent(sc, event);
1040     if (ACPI_SUCCESS(status)) {
1041         /* If we succeeded, burst flag should now be present. */
1042         if (cmd == EC_COMMAND_BURST_ENABLE) {
1043             ec_status = EC_GET_CSR(sc);
1044             if ((ec_status & EC_FLAG_BURST_MODE) == 0)
1045                 status = AE_ERROR;
1046         }
1047     } else {
1048         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
1049                     "EcCommand: no response to %#x\n", cmd);
1050     }
1051
1052     return (status);
1053 }
1054
1055 static ACPI_STATUS
1056 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
1057 {
1058     ACPI_STATUS status;
1059     UINT8 data;
1060
1061     ACPI_SERIAL_ASSERT(ec);
1062     CTR1(KTR_ACPI, "ec read from %#x", Address);
1063
1064     /* If we can't start burst mode, continue anyway. */
1065     status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
1066     if (status == AE_OK) {
1067         data = EC_GET_DATA(sc);
1068         if (data == EC_BURST_ACK) {
1069             CTR0(KTR_ACPI, "ec burst enabled");
1070             sc->ec_burstactive = TRUE;
1071         }
1072     }
1073
1074     status = EcCommand(sc, EC_COMMAND_READ);
1075     if (ACPI_FAILURE(status))
1076         return (status);
1077
1078     EC_SET_DATA(sc, Address);
1079     status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
1080     if (ACPI_FAILURE(status)) {
1081         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
1082                     "EcRead: Failed waiting for EC to send data.\n");
1083         return (status);
1084     }
1085
1086     *Data = EC_GET_DATA(sc);
1087
1088     if (sc->ec_burstactive) {
1089         status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
1090         if (ACPI_FAILURE(status))
1091             return (status);
1092         sc->ec_burstactive = FALSE;
1093         CTR0(KTR_ACPI, "ec disabled burst ok");
1094     }
1095
1096     return (AE_OK);
1097 }    
1098
1099 static ACPI_STATUS
1100 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
1101 {
1102     ACPI_STATUS status;
1103     UINT8 data;
1104
1105     ACPI_SERIAL_ASSERT(ec);
1106     CTR2(KTR_ACPI, "ec write to %#x, data %#x", Address, *Data);
1107
1108     /* If we can't start burst mode, continue anyway. */
1109     status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
1110     if (status == AE_OK) {
1111         data = EC_GET_DATA(sc);
1112         if (data == EC_BURST_ACK) {
1113             CTR0(KTR_ACPI, "ec burst enabled");
1114             sc->ec_burstactive = TRUE;
1115         }
1116     }
1117
1118     status = EcCommand(sc, EC_COMMAND_WRITE);
1119     if (ACPI_FAILURE(status))
1120         return (status);
1121
1122     EC_SET_DATA(sc, Address);
1123     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
1124     if (ACPI_FAILURE(status)) {
1125         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
1126                     "EcRead: Failed waiting for EC to process address\n");
1127         return (status);
1128     }
1129
1130     EC_SET_DATA(sc, *Data);
1131     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
1132     if (ACPI_FAILURE(status)) {
1133         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
1134                     "EcWrite: Failed waiting for EC to process data\n");
1135         return (status);
1136     }
1137
1138     if (sc->ec_burstactive) {
1139         status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
1140         if (ACPI_FAILURE(status))
1141             return (status);
1142         sc->ec_burstactive = FALSE;
1143         CTR0(KTR_ACPI, "ec disabled burst ok");
1144     }
1145
1146     return (AE_OK);
1147 }