a521b6b3797e6a2f4d12606dd23a0d4ecd022b5f
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / ec.c
1 /*
2  *  ec.c - ACPI Embedded Controller Driver (v3)
3  *
4  *  Copyright (C) 2001-2015 Intel Corporation
5  *    Author: 2014, 2015 Lv Zheng <lv.zheng@intel.com>
6  *            2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
7  *            2006       Denis Sadykov <denis.m.sadykov@intel.com>
8  *            2004       Luming Yu <luming.yu@intel.com>
9  *            2001, 2002 Andy Grover <andrew.grover@intel.com>
10  *            2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
11  *  Copyright (C) 2008      Alexey Starikovskiy <astarikovskiy@suse.de>
12  *
13  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14  *
15  *  This program is free software; you can redistribute it and/or modify
16  *  it under the terms of the GNU General Public License as published by
17  *  the Free Software Foundation; either version 2 of the License, or (at
18  *  your option) any later version.
19  *
20  *  This program is distributed in the hope that it will be useful, but
21  *  WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  *  General Public License for more details.
24  *
25  *  You should have received a copy of the GNU General Public License along
26  *  with this program; if not, write to the Free Software Foundation, Inc.,
27  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28  *
29  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30  */
31
32 /* Uncomment next line to get verbose printout */
33 /* #define DEBUG */
34 #define pr_fmt(fmt) "ACPI : EC: " fmt
35
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/types.h>
40 #include <linux/delay.h>
41 #include <linux/interrupt.h>
42 #include <linux/list.h>
43 #include <linux/spinlock.h>
44 #include <linux/slab.h>
45 #include <linux/acpi.h>
46 #include <linux/dmi.h>
47 #include <asm/io.h>
48
49 #include "internal.h"
50
51 #define ACPI_EC_CLASS                   "embedded_controller"
52 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
53 #define ACPI_EC_FILE_INFO               "info"
54
55 /* EC status register */
56 #define ACPI_EC_FLAG_OBF        0x01    /* Output buffer full */
57 #define ACPI_EC_FLAG_IBF        0x02    /* Input buffer full */
58 #define ACPI_EC_FLAG_CMD        0x08    /* Input buffer contains a command */
59 #define ACPI_EC_FLAG_BURST      0x10    /* burst mode */
60 #define ACPI_EC_FLAG_SCI        0x20    /* EC-SCI occurred */
61
62 /* EC commands */
63 enum ec_command {
64         ACPI_EC_COMMAND_READ = 0x80,
65         ACPI_EC_COMMAND_WRITE = 0x81,
66         ACPI_EC_BURST_ENABLE = 0x82,
67         ACPI_EC_BURST_DISABLE = 0x83,
68         ACPI_EC_COMMAND_QUERY = 0x84,
69 };
70
71 #define ACPI_EC_DELAY           500     /* Wait 500ms max. during EC ops */
72 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
73 #define ACPI_EC_UDELAY_POLL     550     /* Wait 1ms for EC transaction polling */
74 #define ACPI_EC_CLEAR_MAX       100     /* Maximum number of events to query
75                                          * when trying to clear the EC */
76
77 enum {
78         EC_FLAGS_QUERY_PENDING,         /* Query is pending */
79         EC_FLAGS_HANDLERS_INSTALLED,    /* Handlers for GPE and
80                                          * OpReg are installed */
81         EC_FLAGS_STARTED,               /* Driver is started */
82         EC_FLAGS_STOPPED,               /* Driver is stopped */
83         EC_FLAGS_COMMAND_STORM,         /* GPE storms occurred to the
84                                          * current command processing */
85 };
86
87 #define ACPI_EC_COMMAND_POLL            0x01 /* Available for command byte */
88 #define ACPI_EC_COMMAND_COMPLETE        0x02 /* Completed last byte */
89
90 /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
91 static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
92 module_param(ec_delay, uint, 0644);
93 MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
94
95 /*
96  * If the number of false interrupts per one transaction exceeds
97  * this threshold, will think there is a GPE storm happened and
98  * will disable the GPE for normal transaction.
99  */
100 static unsigned int ec_storm_threshold  __read_mostly = 8;
101 module_param(ec_storm_threshold, uint, 0644);
102 MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
103
104 struct acpi_ec_query_handler {
105         struct list_head node;
106         acpi_ec_query_func func;
107         acpi_handle handle;
108         void *data;
109         u8 query_bit;
110         struct kref kref;
111 };
112
113 struct transaction {
114         const u8 *wdata;
115         u8 *rdata;
116         unsigned short irq_count;
117         u8 command;
118         u8 wi;
119         u8 ri;
120         u8 wlen;
121         u8 rlen;
122         u8 flags;
123 };
124
125 static int acpi_ec_query(struct acpi_ec *ec, u8 *data);
126 static void advance_transaction(struct acpi_ec *ec);
127
128 struct acpi_ec *boot_ec, *first_ec;
129 EXPORT_SYMBOL(first_ec);
130
131 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
132 static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
133 static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
134 static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
135 static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
136
137 /* --------------------------------------------------------------------------
138  *                           Logging/Debugging
139  * -------------------------------------------------------------------------- */
140
141 /*
142  * Splitters used by the developers to track the boundary of the EC
143  * handling processes.
144  */
145 #ifdef DEBUG
146 #define EC_DBG_SEP      " "
147 #define EC_DBG_DRV      "+++++"
148 #define EC_DBG_STM      "====="
149 #define EC_DBG_REQ      "*****"
150 #define EC_DBG_EVT      "#####"
151 #else
152 #define EC_DBG_SEP      ""
153 #define EC_DBG_DRV
154 #define EC_DBG_STM
155 #define EC_DBG_REQ
156 #define EC_DBG_EVT
157 #endif
158
159 #define ec_log_raw(fmt, ...) \
160         pr_info(fmt "\n", ##__VA_ARGS__)
161 #define ec_dbg_raw(fmt, ...) \
162         pr_debug(fmt "\n", ##__VA_ARGS__)
163 #define ec_log(filter, fmt, ...) \
164         ec_log_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
165 #define ec_dbg(filter, fmt, ...) \
166         ec_dbg_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
167
168 #define ec_log_drv(fmt, ...) \
169         ec_log(EC_DBG_DRV, fmt, ##__VA_ARGS__)
170 #define ec_dbg_drv(fmt, ...) \
171         ec_dbg(EC_DBG_DRV, fmt, ##__VA_ARGS__)
172 #define ec_dbg_stm(fmt, ...) \
173         ec_dbg(EC_DBG_STM, fmt, ##__VA_ARGS__)
174 #define ec_dbg_req(fmt, ...) \
175         ec_dbg(EC_DBG_REQ, fmt, ##__VA_ARGS__)
176 #define ec_dbg_evt(fmt, ...) \
177         ec_dbg(EC_DBG_EVT, fmt, ##__VA_ARGS__)
178 #define ec_dbg_ref(ec, fmt, ...) \
179         ec_dbg_raw("%lu: " fmt, ec->reference_count, ## __VA_ARGS__)
180
181 /* --------------------------------------------------------------------------
182  *                           Device Flags
183  * -------------------------------------------------------------------------- */
184
185 static bool acpi_ec_started(struct acpi_ec *ec)
186 {
187         return test_bit(EC_FLAGS_STARTED, &ec->flags) &&
188                !test_bit(EC_FLAGS_STOPPED, &ec->flags);
189 }
190
191 static bool acpi_ec_flushed(struct acpi_ec *ec)
192 {
193         return ec->reference_count == 1;
194 }
195
196 /* --------------------------------------------------------------------------
197  *                           EC Registers
198  * -------------------------------------------------------------------------- */
199
200 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
201 {
202         u8 x = inb(ec->command_addr);
203
204         ec_dbg_raw("EC_SC(R) = 0x%2.2x "
205                    "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d",
206                    x,
207                    !!(x & ACPI_EC_FLAG_SCI),
208                    !!(x & ACPI_EC_FLAG_BURST),
209                    !!(x & ACPI_EC_FLAG_CMD),
210                    !!(x & ACPI_EC_FLAG_IBF),
211                    !!(x & ACPI_EC_FLAG_OBF));
212         return x;
213 }
214
215 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
216 {
217         u8 x = inb(ec->data_addr);
218
219         ec->timestamp = jiffies;
220         ec_dbg_raw("EC_DATA(R) = 0x%2.2x", x);
221         return x;
222 }
223
224 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
225 {
226         ec_dbg_raw("EC_SC(W) = 0x%2.2x", command);
227         outb(command, ec->command_addr);
228         ec->timestamp = jiffies;
229 }
230
231 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
232 {
233         ec_dbg_raw("EC_DATA(W) = 0x%2.2x", data);
234         outb(data, ec->data_addr);
235         ec->timestamp = jiffies;
236 }
237
238 #ifdef DEBUG
239 static const char *acpi_ec_cmd_string(u8 cmd)
240 {
241         switch (cmd) {
242         case 0x80:
243                 return "RD_EC";
244         case 0x81:
245                 return "WR_EC";
246         case 0x82:
247                 return "BE_EC";
248         case 0x83:
249                 return "BD_EC";
250         case 0x84:
251                 return "QR_EC";
252         }
253         return "UNKNOWN";
254 }
255 #else
256 #define acpi_ec_cmd_string(cmd)         "UNDEF"
257 #endif
258
259 /* --------------------------------------------------------------------------
260  *                           GPE Registers
261  * -------------------------------------------------------------------------- */
262
263 static inline bool acpi_ec_is_gpe_raised(struct acpi_ec *ec)
264 {
265         acpi_event_status gpe_status = 0;
266
267         (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status);
268         return (gpe_status & ACPI_EVENT_FLAG_STATUS_SET) ? true : false;
269 }
270
271 static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open)
272 {
273         if (open)
274                 acpi_enable_gpe(NULL, ec->gpe);
275         else {
276                 BUG_ON(ec->reference_count < 1);
277                 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
278         }
279         if (acpi_ec_is_gpe_raised(ec)) {
280                 /*
281                  * On some platforms, EN=1 writes cannot trigger GPE. So
282                  * software need to manually trigger a pseudo GPE event on
283                  * EN=1 writes.
284                  */
285                 ec_dbg_raw("Polling quirk");
286                 advance_transaction(ec);
287         }
288 }
289
290 static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close)
291 {
292         if (close)
293                 acpi_disable_gpe(NULL, ec->gpe);
294         else {
295                 BUG_ON(ec->reference_count < 1);
296                 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
297         }
298 }
299
300 static inline void acpi_ec_clear_gpe(struct acpi_ec *ec)
301 {
302         /*
303          * GPE STS is a W1C register, which means:
304          * 1. Software can clear it without worrying about clearing other
305          *    GPEs' STS bits when the hardware sets them in parallel.
306          * 2. As long as software can ensure only clearing it when it is
307          *    set, hardware won't set it in parallel.
308          * So software can clear GPE in any contexts.
309          * Warning: do not move the check into advance_transaction() as the
310          * EC commands will be sent without GPE raised.
311          */
312         if (!acpi_ec_is_gpe_raised(ec))
313                 return;
314         acpi_clear_gpe(NULL, ec->gpe);
315 }
316
317 /* --------------------------------------------------------------------------
318  *                           Transaction Management
319  * -------------------------------------------------------------------------- */
320
321 static void acpi_ec_submit_request(struct acpi_ec *ec)
322 {
323         ec->reference_count++;
324         if (ec->reference_count == 1)
325                 acpi_ec_enable_gpe(ec, true);
326 }
327
328 static void acpi_ec_complete_request(struct acpi_ec *ec)
329 {
330         bool flushed = false;
331
332         ec->reference_count--;
333         if (ec->reference_count == 0)
334                 acpi_ec_disable_gpe(ec, true);
335         flushed = acpi_ec_flushed(ec);
336         if (flushed)
337                 wake_up(&ec->wait);
338 }
339
340 static void acpi_ec_set_storm(struct acpi_ec *ec, u8 flag)
341 {
342         if (!test_bit(flag, &ec->flags)) {
343                 acpi_ec_disable_gpe(ec, false);
344                 ec_dbg_drv("Polling enabled");
345                 set_bit(flag, &ec->flags);
346         }
347 }
348
349 static void acpi_ec_clear_storm(struct acpi_ec *ec, u8 flag)
350 {
351         if (test_bit(flag, &ec->flags)) {
352                 clear_bit(flag, &ec->flags);
353                 acpi_ec_enable_gpe(ec, false);
354                 ec_dbg_drv("Polling disabled");
355         }
356 }
357
358 /*
359  * acpi_ec_submit_flushable_request() - Increase the reference count unless
360  *                                      the flush operation is not in
361  *                                      progress
362  * @ec: the EC device
363  *
364  * This function must be used before taking a new action that should hold
365  * the reference count.  If this function returns false, then the action
366  * must be discarded or it will prevent the flush operation from being
367  * completed.
368  */
369 static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec)
370 {
371         if (!acpi_ec_started(ec))
372                 return false;
373         acpi_ec_submit_request(ec);
374         return true;
375 }
376
377 static void acpi_ec_submit_query(struct acpi_ec *ec)
378 {
379         if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
380                 ec_dbg_req("Event started");
381                 schedule_work(&ec->work);
382         }
383 }
384
385 static void acpi_ec_complete_query(struct acpi_ec *ec)
386 {
387         if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
388                 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
389                 ec_dbg_req("Event stopped");
390         }
391 }
392
393 static int ec_transaction_polled(struct acpi_ec *ec)
394 {
395         unsigned long flags;
396         int ret = 0;
397
398         spin_lock_irqsave(&ec->lock, flags);
399         if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL))
400                 ret = 1;
401         spin_unlock_irqrestore(&ec->lock, flags);
402         return ret;
403 }
404
405 static int ec_transaction_completed(struct acpi_ec *ec)
406 {
407         unsigned long flags;
408         int ret = 0;
409
410         spin_lock_irqsave(&ec->lock, flags);
411         if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
412                 ret = 1;
413         spin_unlock_irqrestore(&ec->lock, flags);
414         return ret;
415 }
416
417 static void advance_transaction(struct acpi_ec *ec)
418 {
419         struct transaction *t;
420         u8 status;
421         bool wakeup = false;
422
423         ec_dbg_stm("%s (%d)", in_interrupt() ? "IRQ" : "TASK",
424                    smp_processor_id());
425         /*
426          * By always clearing STS before handling all indications, we can
427          * ensure a hardware STS 0->1 change after this clearing can always
428          * trigger a GPE interrupt.
429          */
430         acpi_ec_clear_gpe(ec);
431         status = acpi_ec_read_status(ec);
432         t = ec->curr;
433         if (!t)
434                 goto err;
435         if (t->flags & ACPI_EC_COMMAND_POLL) {
436                 if (t->wlen > t->wi) {
437                         if ((status & ACPI_EC_FLAG_IBF) == 0)
438                                 acpi_ec_write_data(ec, t->wdata[t->wi++]);
439                         else
440                                 goto err;
441                 } else if (t->rlen > t->ri) {
442                         if ((status & ACPI_EC_FLAG_OBF) == 1) {
443                                 t->rdata[t->ri++] = acpi_ec_read_data(ec);
444                                 if (t->rlen == t->ri) {
445                                         t->flags |= ACPI_EC_COMMAND_COMPLETE;
446                                         if (t->command == ACPI_EC_COMMAND_QUERY)
447                                                 ec_dbg_req("Command(%s) hardware completion",
448                                                            acpi_ec_cmd_string(t->command));
449                                         wakeup = true;
450                                 }
451                         } else
452                                 goto err;
453                 } else if (t->wlen == t->wi &&
454                            (status & ACPI_EC_FLAG_IBF) == 0) {
455                         t->flags |= ACPI_EC_COMMAND_COMPLETE;
456                         wakeup = true;
457                 }
458                 goto out;
459         } else {
460                 if (EC_FLAGS_QUERY_HANDSHAKE &&
461                     !(status & ACPI_EC_FLAG_SCI) &&
462                     (t->command == ACPI_EC_COMMAND_QUERY)) {
463                         t->flags |= ACPI_EC_COMMAND_POLL;
464                         acpi_ec_complete_query(ec);
465                         t->rdata[t->ri++] = 0x00;
466                         t->flags |= ACPI_EC_COMMAND_COMPLETE;
467                         ec_dbg_req("Command(%s) software completion",
468                                    acpi_ec_cmd_string(t->command));
469                         wakeup = true;
470                 } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
471                         acpi_ec_write_cmd(ec, t->command);
472                         t->flags |= ACPI_EC_COMMAND_POLL;
473                         acpi_ec_complete_query(ec);
474                 } else
475                         goto err;
476                 goto out;
477         }
478 err:
479         /*
480          * If SCI bit is set, then don't think it's a false IRQ
481          * otherwise will take a not handled IRQ as a false one.
482          */
483         if (!(status & ACPI_EC_FLAG_SCI)) {
484                 if (in_interrupt() && t) {
485                         if (t->irq_count < ec_storm_threshold)
486                                 ++t->irq_count;
487                         /* Allow triggering on 0 threshold */
488                         if (t->irq_count == ec_storm_threshold)
489                                 acpi_ec_set_storm(ec, EC_FLAGS_COMMAND_STORM);
490                 }
491         }
492 out:
493         if (status & ACPI_EC_FLAG_SCI)
494                 acpi_ec_submit_query(ec);
495         if (wakeup && in_interrupt())
496                 wake_up(&ec->wait);
497 }
498
499 static void start_transaction(struct acpi_ec *ec)
500 {
501         ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
502         ec->curr->flags = 0;
503 }
504
505 static int ec_guard(struct acpi_ec *ec)
506 {
507         unsigned long guard = usecs_to_jiffies(ACPI_EC_UDELAY_POLL);
508         unsigned long timeout = ec->timestamp + guard;
509
510         do {
511                 if (EC_FLAGS_MSI) {
512                         /* Perform busy polling */
513                         if (ec_transaction_completed(ec))
514                                 return 0;
515                         udelay(jiffies_to_usecs(guard));
516                 } else {
517                         /*
518                          * Perform wait polling
519                          *
520                          * The following check is there to keep the old
521                          * logic - no inter-transaction guarding for the
522                          * wait polling mode.
523                          */
524                         if (!ec_transaction_polled(ec))
525                                 break;
526                         if (wait_event_timeout(ec->wait,
527                                                ec_transaction_completed(ec),
528                                                guard))
529                                 return 0;
530                 }
531                 /* Guard the register accesses for the polling modes */
532         } while (time_before(jiffies, timeout));
533         return -ETIME;
534 }
535
536 static int ec_poll(struct acpi_ec *ec)
537 {
538         unsigned long flags;
539         int repeat = 5; /* number of command restarts */
540
541         while (repeat--) {
542                 unsigned long delay = jiffies +
543                         msecs_to_jiffies(ec_delay);
544                 do {
545                         if (!ec_guard(ec))
546                                 return 0;
547                         spin_lock_irqsave(&ec->lock, flags);
548                         advance_transaction(ec);
549                         spin_unlock_irqrestore(&ec->lock, flags);
550                 } while (time_before(jiffies, delay));
551                 pr_debug("controller reset, restart transaction\n");
552                 spin_lock_irqsave(&ec->lock, flags);
553                 start_transaction(ec);
554                 spin_unlock_irqrestore(&ec->lock, flags);
555         }
556         return -ETIME;
557 }
558
559 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
560                                         struct transaction *t)
561 {
562         unsigned long tmp;
563         int ret = 0;
564
565         /* start transaction */
566         spin_lock_irqsave(&ec->lock, tmp);
567         /* Enable GPE for command processing (IBF=0/OBF=1) */
568         if (!acpi_ec_submit_flushable_request(ec)) {
569                 ret = -EINVAL;
570                 goto unlock;
571         }
572         ec_dbg_ref(ec, "Increase command");
573         /* following two actions should be kept atomic */
574         ec->curr = t;
575         ec_dbg_req("Command(%s) started", acpi_ec_cmd_string(t->command));
576         start_transaction(ec);
577         spin_unlock_irqrestore(&ec->lock, tmp);
578
579         ret = ec_poll(ec);
580
581         spin_lock_irqsave(&ec->lock, tmp);
582         if (t->irq_count == ec_storm_threshold)
583                 acpi_ec_clear_storm(ec, EC_FLAGS_COMMAND_STORM);
584         ec_dbg_req("Command(%s) stopped", acpi_ec_cmd_string(t->command));
585         ec->curr = NULL;
586         /* Disable GPE for command processing (IBF=0/OBF=1) */
587         acpi_ec_complete_request(ec);
588         ec_dbg_ref(ec, "Decrease command");
589 unlock:
590         spin_unlock_irqrestore(&ec->lock, tmp);
591         return ret;
592 }
593
594 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
595 {
596         int status;
597         u32 glk;
598
599         if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
600                 return -EINVAL;
601         if (t->rdata)
602                 memset(t->rdata, 0, t->rlen);
603
604         mutex_lock(&ec->mutex);
605         if (ec->global_lock) {
606                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
607                 if (ACPI_FAILURE(status)) {
608                         status = -ENODEV;
609                         goto unlock;
610                 }
611         }
612
613         status = acpi_ec_transaction_unlocked(ec, t);
614
615         if (ec->global_lock)
616                 acpi_release_global_lock(glk);
617 unlock:
618         mutex_unlock(&ec->mutex);
619         return status;
620 }
621
622 static int acpi_ec_burst_enable(struct acpi_ec *ec)
623 {
624         u8 d;
625         struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
626                                 .wdata = NULL, .rdata = &d,
627                                 .wlen = 0, .rlen = 1};
628
629         return acpi_ec_transaction(ec, &t);
630 }
631
632 static int acpi_ec_burst_disable(struct acpi_ec *ec)
633 {
634         struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
635                                 .wdata = NULL, .rdata = NULL,
636                                 .wlen = 0, .rlen = 0};
637
638         return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
639                                 acpi_ec_transaction(ec, &t) : 0;
640 }
641
642 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
643 {
644         int result;
645         u8 d;
646         struct transaction t = {.command = ACPI_EC_COMMAND_READ,
647                                 .wdata = &address, .rdata = &d,
648                                 .wlen = 1, .rlen = 1};
649
650         result = acpi_ec_transaction(ec, &t);
651         *data = d;
652         return result;
653 }
654
655 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
656 {
657         u8 wdata[2] = { address, data };
658         struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
659                                 .wdata = wdata, .rdata = NULL,
660                                 .wlen = 2, .rlen = 0};
661
662         return acpi_ec_transaction(ec, &t);
663 }
664
665 int ec_read(u8 addr, u8 *val)
666 {
667         int err;
668         u8 temp_data;
669
670         if (!first_ec)
671                 return -ENODEV;
672
673         err = acpi_ec_read(first_ec, addr, &temp_data);
674
675         if (!err) {
676                 *val = temp_data;
677                 return 0;
678         }
679         return err;
680 }
681 EXPORT_SYMBOL(ec_read);
682
683 int ec_write(u8 addr, u8 val)
684 {
685         int err;
686
687         if (!first_ec)
688                 return -ENODEV;
689
690         err = acpi_ec_write(first_ec, addr, val);
691
692         return err;
693 }
694 EXPORT_SYMBOL(ec_write);
695
696 int ec_transaction(u8 command,
697                    const u8 *wdata, unsigned wdata_len,
698                    u8 *rdata, unsigned rdata_len)
699 {
700         struct transaction t = {.command = command,
701                                 .wdata = wdata, .rdata = rdata,
702                                 .wlen = wdata_len, .rlen = rdata_len};
703
704         if (!first_ec)
705                 return -ENODEV;
706
707         return acpi_ec_transaction(first_ec, &t);
708 }
709 EXPORT_SYMBOL(ec_transaction);
710
711 /* Get the handle to the EC device */
712 acpi_handle ec_get_handle(void)
713 {
714         if (!first_ec)
715                 return NULL;
716         return first_ec->handle;
717 }
718 EXPORT_SYMBOL(ec_get_handle);
719
720 /*
721  * Process _Q events that might have accumulated in the EC.
722  * Run with locked ec mutex.
723  */
724 static void acpi_ec_clear(struct acpi_ec *ec)
725 {
726         int i, status;
727         u8 value = 0;
728
729         for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
730                 status = acpi_ec_query(ec, &value);
731                 if (status || !value)
732                         break;
733         }
734
735         if (unlikely(i == ACPI_EC_CLEAR_MAX))
736                 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
737         else
738                 pr_info("%d stale EC events cleared\n", i);
739 }
740
741 static void acpi_ec_start(struct acpi_ec *ec, bool resuming)
742 {
743         unsigned long flags;
744
745         spin_lock_irqsave(&ec->lock, flags);
746         if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) {
747                 ec_dbg_drv("Starting EC");
748                 /* Enable GPE for event processing (SCI_EVT=1) */
749                 if (!resuming) {
750                         acpi_ec_submit_request(ec);
751                         ec_dbg_ref(ec, "Increase driver");
752                 }
753                 ec_log_drv("EC started");
754         }
755         spin_unlock_irqrestore(&ec->lock, flags);
756 }
757
758 static bool acpi_ec_stopped(struct acpi_ec *ec)
759 {
760         unsigned long flags;
761         bool flushed;
762
763         spin_lock_irqsave(&ec->lock, flags);
764         flushed = acpi_ec_flushed(ec);
765         spin_unlock_irqrestore(&ec->lock, flags);
766         return flushed;
767 }
768
769 static void acpi_ec_stop(struct acpi_ec *ec, bool suspending)
770 {
771         unsigned long flags;
772
773         spin_lock_irqsave(&ec->lock, flags);
774         if (acpi_ec_started(ec)) {
775                 ec_dbg_drv("Stopping EC");
776                 set_bit(EC_FLAGS_STOPPED, &ec->flags);
777                 spin_unlock_irqrestore(&ec->lock, flags);
778                 wait_event(ec->wait, acpi_ec_stopped(ec));
779                 spin_lock_irqsave(&ec->lock, flags);
780                 /* Disable GPE for event processing (SCI_EVT=1) */
781                 if (!suspending) {
782                         acpi_ec_complete_request(ec);
783                         ec_dbg_ref(ec, "Decrease driver");
784                 }
785                 clear_bit(EC_FLAGS_STARTED, &ec->flags);
786                 clear_bit(EC_FLAGS_STOPPED, &ec->flags);
787                 ec_log_drv("EC stopped");
788         }
789         spin_unlock_irqrestore(&ec->lock, flags);
790 }
791
792 void acpi_ec_block_transactions(void)
793 {
794         struct acpi_ec *ec = first_ec;
795
796         if (!ec)
797                 return;
798
799         mutex_lock(&ec->mutex);
800         /* Prevent transactions from being carried out */
801         acpi_ec_stop(ec, true);
802         mutex_unlock(&ec->mutex);
803 }
804
805 void acpi_ec_unblock_transactions(void)
806 {
807         struct acpi_ec *ec = first_ec;
808
809         if (!ec)
810                 return;
811
812         /* Allow transactions to be carried out again */
813         acpi_ec_start(ec, true);
814
815         if (EC_FLAGS_CLEAR_ON_RESUME)
816                 acpi_ec_clear(ec);
817 }
818
819 void acpi_ec_unblock_transactions_early(void)
820 {
821         /*
822          * Allow transactions to happen again (this function is called from
823          * atomic context during wakeup, so we don't need to acquire the mutex).
824          */
825         if (first_ec)
826                 acpi_ec_start(first_ec, true);
827 }
828
829 /* --------------------------------------------------------------------------
830                                 Event Management
831    -------------------------------------------------------------------------- */
832 static struct acpi_ec_query_handler *
833 acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler)
834 {
835         if (handler)
836                 kref_get(&handler->kref);
837         return handler;
838 }
839
840 static void acpi_ec_query_handler_release(struct kref *kref)
841 {
842         struct acpi_ec_query_handler *handler =
843                 container_of(kref, struct acpi_ec_query_handler, kref);
844
845         kfree(handler);
846 }
847
848 static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
849 {
850         kref_put(&handler->kref, acpi_ec_query_handler_release);
851 }
852
853 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
854                               acpi_handle handle, acpi_ec_query_func func,
855                               void *data)
856 {
857         struct acpi_ec_query_handler *handler =
858             kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
859
860         if (!handler)
861                 return -ENOMEM;
862
863         handler->query_bit = query_bit;
864         handler->handle = handle;
865         handler->func = func;
866         handler->data = data;
867         mutex_lock(&ec->mutex);
868         kref_init(&handler->kref);
869         list_add(&handler->node, &ec->list);
870         mutex_unlock(&ec->mutex);
871         return 0;
872 }
873 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
874
875 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
876 {
877         struct acpi_ec_query_handler *handler, *tmp;
878         LIST_HEAD(free_list);
879
880         mutex_lock(&ec->mutex);
881         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
882                 if (query_bit == handler->query_bit) {
883                         list_del_init(&handler->node);
884                         list_add(&handler->node, &free_list);
885                 }
886         }
887         mutex_unlock(&ec->mutex);
888         list_for_each_entry_safe(handler, tmp, &free_list, node)
889                 acpi_ec_put_query_handler(handler);
890 }
891 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
892
893 static void acpi_ec_run(void *cxt)
894 {
895         struct acpi_ec_query_handler *handler = cxt;
896
897         if (!handler)
898                 return;
899         ec_dbg_evt("Query(0x%02x) started", handler->query_bit);
900         if (handler->func)
901                 handler->func(handler->data);
902         else if (handler->handle)
903                 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
904         ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit);
905         acpi_ec_put_query_handler(handler);
906 }
907
908 static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
909 {
910         u8 value = 0;
911         int result;
912         acpi_status status;
913         struct acpi_ec_query_handler *handler;
914         struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
915                                 .wdata = NULL, .rdata = &value,
916                                 .wlen = 0, .rlen = 1};
917
918         /*
919          * Query the EC to find out which _Qxx method we need to evaluate.
920          * Note that successful completion of the query causes the ACPI_EC_SCI
921          * bit to be cleared (and thus clearing the interrupt source).
922          */
923         result = acpi_ec_transaction(ec, &t);
924         if (result)
925                 return result;
926         if (data)
927                 *data = value;
928         if (!value)
929                 return -ENODATA;
930
931         mutex_lock(&ec->mutex);
932         list_for_each_entry(handler, &ec->list, node) {
933                 if (value == handler->query_bit) {
934                         /* have custom handler for this bit */
935                         handler = acpi_ec_get_query_handler(handler);
936                         ec_dbg_evt("Query(0x%02x) scheduled",
937                                    handler->query_bit);
938                         status = acpi_os_execute((handler->func) ?
939                                 OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
940                                 acpi_ec_run, handler);
941                         if (ACPI_FAILURE(status))
942                                 result = -EBUSY;
943                         break;
944                 }
945         }
946         mutex_unlock(&ec->mutex);
947         return result;
948 }
949
950 static void acpi_ec_gpe_poller(struct work_struct *work)
951 {
952         struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
953
954         acpi_ec_query(ec, NULL);
955 }
956
957 static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
958         u32 gpe_number, void *data)
959 {
960         unsigned long flags;
961         struct acpi_ec *ec = data;
962
963         spin_lock_irqsave(&ec->lock, flags);
964         advance_transaction(ec);
965         spin_unlock_irqrestore(&ec->lock, flags);
966         return ACPI_INTERRUPT_HANDLED;
967 }
968
969 /* --------------------------------------------------------------------------
970  *                           Address Space Management
971  * -------------------------------------------------------------------------- */
972
973 static acpi_status
974 acpi_ec_space_handler(u32 function, acpi_physical_address address,
975                       u32 bits, u64 *value64,
976                       void *handler_context, void *region_context)
977 {
978         struct acpi_ec *ec = handler_context;
979         int result = 0, i, bytes = bits / 8;
980         u8 *value = (u8 *)value64;
981
982         if ((address > 0xFF) || !value || !handler_context)
983                 return AE_BAD_PARAMETER;
984
985         if (function != ACPI_READ && function != ACPI_WRITE)
986                 return AE_BAD_PARAMETER;
987
988         if (EC_FLAGS_MSI || bits > 8)
989                 acpi_ec_burst_enable(ec);
990
991         for (i = 0; i < bytes; ++i, ++address, ++value)
992                 result = (function == ACPI_READ) ?
993                         acpi_ec_read(ec, address, value) :
994                         acpi_ec_write(ec, address, *value);
995
996         if (EC_FLAGS_MSI || bits > 8)
997                 acpi_ec_burst_disable(ec);
998
999         switch (result) {
1000         case -EINVAL:
1001                 return AE_BAD_PARAMETER;
1002         case -ENODEV:
1003                 return AE_NOT_FOUND;
1004         case -ETIME:
1005                 return AE_TIME;
1006         default:
1007                 return AE_OK;
1008         }
1009 }
1010
1011 /* --------------------------------------------------------------------------
1012  *                             Driver Interface
1013  * -------------------------------------------------------------------------- */
1014
1015 static acpi_status
1016 ec_parse_io_ports(struct acpi_resource *resource, void *context);
1017
1018 static struct acpi_ec *make_acpi_ec(void)
1019 {
1020         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1021
1022         if (!ec)
1023                 return NULL;
1024         ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
1025         mutex_init(&ec->mutex);
1026         init_waitqueue_head(&ec->wait);
1027         INIT_LIST_HEAD(&ec->list);
1028         spin_lock_init(&ec->lock);
1029         INIT_WORK(&ec->work, acpi_ec_gpe_poller);
1030         ec->timestamp = jiffies;
1031         return ec;
1032 }
1033
1034 static acpi_status
1035 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
1036                                void *context, void **return_value)
1037 {
1038         char node_name[5];
1039         struct acpi_buffer buffer = { sizeof(node_name), node_name };
1040         struct acpi_ec *ec = context;
1041         int value = 0;
1042         acpi_status status;
1043
1044         status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
1045
1046         if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
1047                 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
1048         return AE_OK;
1049 }
1050
1051 static acpi_status
1052 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
1053 {
1054         acpi_status status;
1055         unsigned long long tmp = 0;
1056         struct acpi_ec *ec = context;
1057
1058         /* clear addr values, ec_parse_io_ports depend on it */
1059         ec->command_addr = ec->data_addr = 0;
1060
1061         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1062                                      ec_parse_io_ports, ec);
1063         if (ACPI_FAILURE(status))
1064                 return status;
1065
1066         /* Get GPE bit assignment (EC events). */
1067         /* TODO: Add support for _GPE returning a package */
1068         status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
1069         if (ACPI_FAILURE(status))
1070                 return status;
1071         ec->gpe = tmp;
1072         /* Use the global lock for all EC transactions? */
1073         tmp = 0;
1074         acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
1075         ec->global_lock = tmp;
1076         ec->handle = handle;
1077         return AE_CTRL_TERMINATE;
1078 }
1079
1080 static int ec_install_handlers(struct acpi_ec *ec)
1081 {
1082         acpi_status status;
1083
1084         if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1085                 return 0;
1086         status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
1087                                   ACPI_GPE_EDGE_TRIGGERED,
1088                                   &acpi_ec_gpe_handler, ec);
1089         if (ACPI_FAILURE(status))
1090                 return -ENODEV;
1091
1092         acpi_ec_start(ec, false);
1093         status = acpi_install_address_space_handler(ec->handle,
1094                                                     ACPI_ADR_SPACE_EC,
1095                                                     &acpi_ec_space_handler,
1096                                                     NULL, ec);
1097         if (ACPI_FAILURE(status)) {
1098                 if (status == AE_NOT_FOUND) {
1099                         /*
1100                          * Maybe OS fails in evaluating the _REG object.
1101                          * The AE_NOT_FOUND error will be ignored and OS
1102                          * continue to initialize EC.
1103                          */
1104                         pr_err("Fail in evaluating the _REG object"
1105                                 " of EC device. Broken bios is suspected.\n");
1106                 } else {
1107                         acpi_ec_stop(ec, false);
1108                         acpi_remove_gpe_handler(NULL, ec->gpe,
1109                                 &acpi_ec_gpe_handler);
1110                         return -ENODEV;
1111                 }
1112         }
1113
1114         set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
1115         return 0;
1116 }
1117
1118 static void ec_remove_handlers(struct acpi_ec *ec)
1119 {
1120         if (!test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1121                 return;
1122         acpi_ec_stop(ec, false);
1123         if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
1124                                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
1125                 pr_err("failed to remove space handler\n");
1126         if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
1127                                 &acpi_ec_gpe_handler)))
1128                 pr_err("failed to remove gpe handler\n");
1129         clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
1130 }
1131
1132 static int acpi_ec_add(struct acpi_device *device)
1133 {
1134         struct acpi_ec *ec = NULL;
1135         int ret;
1136
1137         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1138         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1139
1140         /* Check for boot EC */
1141         if (boot_ec &&
1142             (boot_ec->handle == device->handle ||
1143              boot_ec->handle == ACPI_ROOT_OBJECT)) {
1144                 ec = boot_ec;
1145                 boot_ec = NULL;
1146         } else {
1147                 ec = make_acpi_ec();
1148                 if (!ec)
1149                         return -ENOMEM;
1150         }
1151         if (ec_parse_device(device->handle, 0, ec, NULL) !=
1152                 AE_CTRL_TERMINATE) {
1153                         kfree(ec);
1154                         return -EINVAL;
1155         }
1156
1157         /* Find and register all query methods */
1158         acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
1159                             acpi_ec_register_query_methods, NULL, ec, NULL);
1160
1161         if (!first_ec)
1162                 first_ec = ec;
1163         device->driver_data = ec;
1164
1165         ret = !!request_region(ec->data_addr, 1, "EC data");
1166         WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
1167         ret = !!request_region(ec->command_addr, 1, "EC cmd");
1168         WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
1169
1170         pr_info("GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
1171                           ec->gpe, ec->command_addr, ec->data_addr);
1172
1173         ret = ec_install_handlers(ec);
1174
1175         /* Reprobe devices depending on the EC */
1176         acpi_walk_dep_device_list(ec->handle);
1177
1178         /* EC is fully operational, allow queries */
1179         clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
1180
1181         /* Clear stale _Q events if hardware might require that */
1182         if (EC_FLAGS_CLEAR_ON_RESUME)
1183                 acpi_ec_clear(ec);
1184         return ret;
1185 }
1186
1187 static int acpi_ec_remove(struct acpi_device *device)
1188 {
1189         struct acpi_ec *ec;
1190         struct acpi_ec_query_handler *handler, *tmp;
1191
1192         if (!device)
1193                 return -EINVAL;
1194
1195         ec = acpi_driver_data(device);
1196         ec_remove_handlers(ec);
1197         mutex_lock(&ec->mutex);
1198         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
1199                 list_del(&handler->node);
1200                 kfree(handler);
1201         }
1202         mutex_unlock(&ec->mutex);
1203         release_region(ec->data_addr, 1);
1204         release_region(ec->command_addr, 1);
1205         device->driver_data = NULL;
1206         if (ec == first_ec)
1207                 first_ec = NULL;
1208         kfree(ec);
1209         return 0;
1210 }
1211
1212 static acpi_status
1213 ec_parse_io_ports(struct acpi_resource *resource, void *context)
1214 {
1215         struct acpi_ec *ec = context;
1216
1217         if (resource->type != ACPI_RESOURCE_TYPE_IO)
1218                 return AE_OK;
1219
1220         /*
1221          * The first address region returned is the data port, and
1222          * the second address region returned is the status/command
1223          * port.
1224          */
1225         if (ec->data_addr == 0)
1226                 ec->data_addr = resource->data.io.minimum;
1227         else if (ec->command_addr == 0)
1228                 ec->command_addr = resource->data.io.minimum;
1229         else
1230                 return AE_CTRL_TERMINATE;
1231
1232         return AE_OK;
1233 }
1234
1235 int __init acpi_boot_ec_enable(void)
1236 {
1237         if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
1238                 return 0;
1239         if (!ec_install_handlers(boot_ec)) {
1240                 first_ec = boot_ec;
1241                 return 0;
1242         }
1243         return -EFAULT;
1244 }
1245
1246 static const struct acpi_device_id ec_device_ids[] = {
1247         {"PNP0C09", 0},
1248         {"", 0},
1249 };
1250
1251 /* Some BIOS do not survive early DSDT scan, skip it */
1252 static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
1253 {
1254         EC_FLAGS_SKIP_DSDT_SCAN = 1;
1255         return 0;
1256 }
1257
1258 /* ASUStek often supplies us with broken ECDT, validate it */
1259 static int ec_validate_ecdt(const struct dmi_system_id *id)
1260 {
1261         EC_FLAGS_VALIDATE_ECDT = 1;
1262         return 0;
1263 }
1264
1265 /* MSI EC needs special treatment, enable it */
1266 static int ec_flag_msi(const struct dmi_system_id *id)
1267 {
1268         pr_debug("Detected MSI hardware, enabling workarounds.\n");
1269         EC_FLAGS_MSI = 1;
1270         EC_FLAGS_VALIDATE_ECDT = 1;
1271         return 0;
1272 }
1273
1274 /*
1275  * Acer EC firmware refuses to respond QR_EC when SCI_EVT is not set, for
1276  * which case, we complete the QR_EC without issuing it to the firmware.
1277  * https://bugzilla.kernel.org/show_bug.cgi?id=86211
1278  */
1279 static int ec_flag_query_handshake(const struct dmi_system_id *id)
1280 {
1281         pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
1282         EC_FLAGS_QUERY_HANDSHAKE = 1;
1283         return 0;
1284 }
1285
1286 /*
1287  * On some hardware it is necessary to clear events accumulated by the EC during
1288  * sleep. These ECs stop reporting GPEs until they are manually polled, if too
1289  * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
1290  *
1291  * https://bugzilla.kernel.org/show_bug.cgi?id=44161
1292  *
1293  * Ideally, the EC should also be instructed NOT to accumulate events during
1294  * sleep (which Windows seems to do somehow), but the interface to control this
1295  * behaviour is not known at this time.
1296  *
1297  * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
1298  * however it is very likely that other Samsung models are affected.
1299  *
1300  * On systems which don't accumulate _Q events during sleep, this extra check
1301  * should be harmless.
1302  */
1303 static int ec_clear_on_resume(const struct dmi_system_id *id)
1304 {
1305         pr_debug("Detected system needing EC poll on resume.\n");
1306         EC_FLAGS_CLEAR_ON_RESUME = 1;
1307         return 0;
1308 }
1309
1310 static struct dmi_system_id ec_dmi_table[] __initdata = {
1311         {
1312         ec_skip_dsdt_scan, "Compal JFL92", {
1313         DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
1314         DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
1315         {
1316         ec_flag_msi, "MSI hardware", {
1317         DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
1318         {
1319         ec_flag_msi, "MSI hardware", {
1320         DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
1321         {
1322         ec_flag_msi, "MSI hardware", {
1323         DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
1324         {
1325         ec_flag_msi, "MSI hardware", {
1326         DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
1327         {
1328         ec_flag_msi, "Quanta hardware", {
1329         DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1330         DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
1331         {
1332         ec_flag_msi, "Quanta hardware", {
1333         DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1334         DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
1335         {
1336         ec_flag_msi, "Clevo W350etq", {
1337         DMI_MATCH(DMI_SYS_VENDOR, "CLEVO CO."),
1338         DMI_MATCH(DMI_PRODUCT_NAME, "W35_37ET"),}, NULL},
1339         {
1340         ec_validate_ecdt, "ASUS hardware", {
1341         DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
1342         {
1343         ec_validate_ecdt, "ASUS hardware", {
1344         DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
1345         {
1346         ec_skip_dsdt_scan, "HP Folio 13", {
1347         DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1348         DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL},
1349         {
1350         ec_validate_ecdt, "ASUS hardware", {
1351         DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
1352         DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
1353         {
1354         ec_clear_on_resume, "Samsung hardware", {
1355         DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
1356         {
1357         ec_flag_query_handshake, "Acer hardware", {
1358         DMI_MATCH(DMI_SYS_VENDOR, "Acer"), }, NULL},
1359         {},
1360 };
1361
1362 int __init acpi_ec_ecdt_probe(void)
1363 {
1364         acpi_status status;
1365         struct acpi_ec *saved_ec = NULL;
1366         struct acpi_table_ecdt *ecdt_ptr;
1367
1368         boot_ec = make_acpi_ec();
1369         if (!boot_ec)
1370                 return -ENOMEM;
1371         /*
1372          * Generate a boot ec context
1373          */
1374         dmi_check_system(ec_dmi_table);
1375         status = acpi_get_table(ACPI_SIG_ECDT, 1,
1376                                 (struct acpi_table_header **)&ecdt_ptr);
1377         if (ACPI_SUCCESS(status)) {
1378                 pr_info("EC description table is found, configuring boot EC\n");
1379                 boot_ec->command_addr = ecdt_ptr->control.address;
1380                 boot_ec->data_addr = ecdt_ptr->data.address;
1381                 boot_ec->gpe = ecdt_ptr->gpe;
1382                 boot_ec->handle = ACPI_ROOT_OBJECT;
1383                 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id,
1384                                 &boot_ec->handle);
1385                 /* Don't trust ECDT, which comes from ASUSTek */
1386                 if (!EC_FLAGS_VALIDATE_ECDT)
1387                         goto install;
1388                 saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
1389                 if (!saved_ec)
1390                         return -ENOMEM;
1391         /* fall through */
1392         }
1393
1394         if (EC_FLAGS_SKIP_DSDT_SCAN) {
1395                 kfree(saved_ec);
1396                 return -ENODEV;
1397         }
1398
1399         /* This workaround is needed only on some broken machines,
1400          * which require early EC, but fail to provide ECDT */
1401         pr_debug("Look up EC in DSDT\n");
1402         status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1403                                         boot_ec, NULL);
1404         /* Check that acpi_get_devices actually find something */
1405         if (ACPI_FAILURE(status) || !boot_ec->handle)
1406                 goto error;
1407         if (saved_ec) {
1408                 /* try to find good ECDT from ASUSTek */
1409                 if (saved_ec->command_addr != boot_ec->command_addr ||
1410                     saved_ec->data_addr != boot_ec->data_addr ||
1411                     saved_ec->gpe != boot_ec->gpe ||
1412                     saved_ec->handle != boot_ec->handle)
1413                         pr_info("ASUSTek keeps feeding us with broken "
1414                         "ECDT tables, which are very hard to workaround. "
1415                         "Trying to use DSDT EC info instead. Please send "
1416                         "output of acpidump to linux-acpi@vger.kernel.org\n");
1417                 kfree(saved_ec);
1418                 saved_ec = NULL;
1419         } else {
1420                 /* We really need to limit this workaround, the only ASUS,
1421                 * which needs it, has fake EC._INI method, so use it as flag.
1422                 * Keep boot_ec struct as it will be needed soon.
1423                 */
1424                 if (!dmi_name_in_vendors("ASUS") ||
1425                     !acpi_has_method(boot_ec->handle, "_INI"))
1426                         return -ENODEV;
1427         }
1428 install:
1429         if (!ec_install_handlers(boot_ec)) {
1430                 first_ec = boot_ec;
1431                 return 0;
1432         }
1433 error:
1434         kfree(boot_ec);
1435         kfree(saved_ec);
1436         boot_ec = NULL;
1437         return -ENODEV;
1438 }
1439
1440 static struct acpi_driver acpi_ec_driver = {
1441         .name = "ec",
1442         .class = ACPI_EC_CLASS,
1443         .ids = ec_device_ids,
1444         .ops = {
1445                 .add = acpi_ec_add,
1446                 .remove = acpi_ec_remove,
1447                 },
1448 };
1449
1450 int __init acpi_ec_init(void)
1451 {
1452         int result = 0;
1453
1454         /* Now register the driver for the EC */
1455         result = acpi_bus_register_driver(&acpi_ec_driver);
1456         if (result < 0)
1457                 return -ENODEV;
1458
1459         return result;
1460 }
1461
1462 /* EC driver currently not unloadable */
1463 #if 0
1464 static void __exit acpi_ec_exit(void)
1465 {
1466
1467         acpi_bus_unregister_driver(&acpi_ec_driver);
1468 }
1469 #endif  /* 0 */