Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Alexey Starikovskiy | 01f22462 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 2 | * ec.c - ACPI Embedded Controller Driver (v2.0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Alexey Starikovskiy | 01f22462 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 4 | * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com> |
| 5 | * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright (C) 2004 Luming Yu <luming.yu@intel.com> |
| 7 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 8 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
| 9 | * |
| 10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or (at |
| 15 | * your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, but |
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along |
| 23 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 25 | * |
| 26 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 27 | */ |
| 28 | |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/proc_fs.h> |
| 35 | #include <linux/seq_file.h> |
Dmitry Torokhov | 451566f | 2005-03-19 01:10:05 -0500 | [diff] [blame] | 36 | #include <linux/interrupt.h> |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 37 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/io.h> |
| 39 | #include <acpi/acpi_bus.h> |
| 40 | #include <acpi/acpi_drivers.h> |
| 41 | #include <acpi/actypes.h> |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #define ACPI_EC_CLASS "embedded_controller" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #define ACPI_EC_DEVICE_NAME "Embedded Controller" |
| 45 | #define ACPI_EC_FILE_INFO "info" |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 46 | |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 47 | #undef PREFIX |
| 48 | #define PREFIX "ACPI: EC: " |
Alexey Starikovskiy | 4350933 | 2007-05-29 16:42:57 +0400 | [diff] [blame] | 49 | |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 50 | /* Uncomment next line to get verbose print outs*/ |
| 51 | /* #define DEBUG */ |
| 52 | |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 53 | /* EC status register */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */ |
| 55 | #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */ |
Dmitry Torokhov | 451566f | 2005-03-19 01:10:05 -0500 | [diff] [blame] | 56 | #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */ |
Alexey Starikovskiy | 4350933 | 2007-05-29 16:42:57 +0400 | [diff] [blame] | 58 | |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 59 | /* EC commands */ |
Alexey Starikovskiy | 3261ff4 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 60 | enum ec_command { |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 61 | ACPI_EC_COMMAND_READ = 0x80, |
| 62 | ACPI_EC_COMMAND_WRITE = 0x81, |
| 63 | ACPI_EC_BURST_ENABLE = 0x82, |
| 64 | ACPI_EC_BURST_DISABLE = 0x83, |
| 65 | ACPI_EC_COMMAND_QUERY = 0x84, |
Alexey Starikovskiy | 3261ff4 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 66 | }; |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 67 | |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 68 | /* EC events */ |
Alexey Starikovskiy | 3261ff4 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 69 | enum ec_event { |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 70 | ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */ |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 71 | ACPI_EC_EVENT_IBF_0, /* Input buffer empty */ |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 72 | }; |
| 73 | |
Alexey Starikovskiy | 5c40641 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 74 | #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */ |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 75 | #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */ |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 76 | |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 77 | enum { |
| 78 | EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */ |
| 79 | EC_FLAGS_QUERY_PENDING, /* Query is pending */ |
Alexey Starikovskiy | 7843932 | 2007-10-22 14:18:43 +0400 | [diff] [blame] | 80 | EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */ |
Len Brown | 0af2f65 | 2007-11-20 19:59:08 -0500 | [diff] [blame] | 81 | EC_FLAGS_ONLY_IBF_GPE, /* Expect GPE only for IBF = 0 event */ |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 82 | }; |
| 83 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 84 | static int acpi_ec_remove(struct acpi_device *device, int type); |
| 85 | static int acpi_ec_start(struct acpi_device *device); |
| 86 | static int acpi_ec_stop(struct acpi_device *device, int type); |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 87 | static int acpi_ec_add(struct acpi_device *device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 89 | static const struct acpi_device_id ec_device_ids[] = { |
| 90 | {"PNP0C09", 0}, |
| 91 | {"", 0}, |
| 92 | }; |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | static struct acpi_driver acpi_ec_driver = { |
Len Brown | c2b6705 | 2007-02-12 23:33:40 -0500 | [diff] [blame] | 95 | .name = "ec", |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 96 | .class = ACPI_EC_CLASS, |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 97 | .ids = ec_device_ids, |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 98 | .ops = { |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 99 | .add = acpi_ec_add, |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 100 | .remove = acpi_ec_remove, |
| 101 | .start = acpi_ec_start, |
| 102 | .stop = acpi_ec_stop, |
| 103 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | }; |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 105 | |
| 106 | /* If we find an EC via the ECDT, we need to keep a ptr to its context */ |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 107 | /* External interfaces use first EC only, so remember */ |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 108 | typedef int (*acpi_ec_query_func) (void *data); |
| 109 | |
| 110 | struct acpi_ec_query_handler { |
| 111 | struct list_head node; |
| 112 | acpi_ec_query_func func; |
| 113 | acpi_handle handle; |
| 114 | void *data; |
| 115 | u8 query_bit; |
| 116 | }; |
| 117 | |
Adrian Bunk | a854e08 | 2006-12-19 12:56:12 -0800 | [diff] [blame] | 118 | static struct acpi_ec { |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 119 | acpi_handle handle; |
Alexey Starikovskiy | a86e277 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 120 | unsigned long gpe; |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 121 | unsigned long command_addr; |
| 122 | unsigned long data_addr; |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 123 | unsigned long global_lock; |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 124 | unsigned long flags; |
Alexey Starikovskiy | c787a85 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 125 | struct mutex lock; |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 126 | wait_queue_head_t wait; |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 127 | struct list_head list; |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 128 | u8 handlers_installed; |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 129 | } *boot_ec, *first_ec; |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | /* -------------------------------------------------------------------------- |
| 132 | Transaction Management |
| 133 | -------------------------------------------------------------------------- */ |
| 134 | |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 135 | static inline u8 acpi_ec_read_status(struct acpi_ec *ec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 137 | u8 x = inb(ec->command_addr); |
| 138 | pr_debug(PREFIX "---> status = 0x%2x\n", x); |
| 139 | return x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 142 | static inline u8 acpi_ec_read_data(struct acpi_ec *ec) |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 143 | { |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 144 | u8 x = inb(ec->data_addr); |
| 145 | pr_debug(PREFIX "---> data = 0x%2x\n", x); |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 146 | return inb(ec->data_addr); |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 147 | } |
| 148 | |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 149 | static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 150 | { |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 151 | pr_debug(PREFIX "<--- command = 0x%2x\n", command); |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 152 | outb(command, ec->command_addr); |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 153 | } |
| 154 | |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 155 | static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 156 | { |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 157 | pr_debug(PREFIX "<--- data = 0x%2x\n", data); |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 158 | outb(data, ec->data_addr); |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 159 | } |
| 160 | |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 161 | static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event) |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 162 | { |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 163 | if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags)) |
Alexey Starikovskiy | 9e19721 | 2007-03-07 18:29:35 -0500 | [diff] [blame] | 164 | return 0; |
Alexey Starikovskiy | 78d0af3 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 165 | if (event == ACPI_EC_EVENT_OBF_1) { |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 166 | if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF) |
Denis M. Sadykov | 7c6db5e | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 167 | return 1; |
Alexey Starikovskiy | 78d0af3 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 168 | } else if (event == ACPI_EC_EVENT_IBF_0) { |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 169 | if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)) |
Denis M. Sadykov | 7c6db5e | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 170 | return 1; |
Denis M. Sadykov | 7c6db5e | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 176 | static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll) |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 177 | { |
Alexey Starikovskiy | 7843932 | 2007-10-22 14:18:43 +0400 | [diff] [blame] | 178 | if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) && |
| 179 | likely(!force_poll)) { |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 180 | if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event), |
| 181 | msecs_to_jiffies(ACPI_EC_DELAY))) |
Len Brown | 0af2f65 | 2007-11-20 19:59:08 -0500 | [diff] [blame] | 182 | return 0; |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 183 | clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
| 184 | if (acpi_ec_check_status(ec, event)) { |
Len Brown | 0af2f65 | 2007-11-20 19:59:08 -0500 | [diff] [blame] | 185 | if (event == ACPI_EC_EVENT_OBF_1) { |
| 186 | /* miss OBF = 1 GPE, don't expect it anymore */ |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 187 | pr_info(PREFIX "missing OBF_1 confirmation," |
Len Brown | 0af2f65 | 2007-11-20 19:59:08 -0500 | [diff] [blame] | 188 | "switching to degraded mode.\n"); |
| 189 | set_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags); |
Alexey Starikovskiy | 95b937e | 2007-10-22 14:19:03 +0400 | [diff] [blame] | 190 | } else { |
Alexey Starikovskiy | 66c5f4e | 2007-10-22 14:18:56 +0400 | [diff] [blame] | 191 | /* missing GPEs, switch back to poll mode */ |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 192 | if (printk_ratelimit()) |
| 193 | pr_info(PREFIX "missing IBF_1 confirmations," |
| 194 | "switch off interrupt mode.\n"); |
Alexey Starikovskiy | 66c5f4e | 2007-10-22 14:18:56 +0400 | [diff] [blame] | 195 | clear_bit(EC_FLAGS_GPE_MODE, &ec->flags); |
Alexey Starikovskiy | 95b937e | 2007-10-22 14:19:03 +0400 | [diff] [blame] | 196 | } |
Len Brown | 0af2f65 | 2007-11-20 19:59:08 -0500 | [diff] [blame] | 197 | return 0; |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 198 | } |
Alexey Starikovskiy | 7843932 | 2007-10-22 14:18:43 +0400 | [diff] [blame] | 199 | } else { |
| 200 | unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY); |
| 201 | clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
| 202 | while (time_before(jiffies, delay)) { |
| 203 | if (acpi_ec_check_status(ec, event)) |
Len Brown | 0af2f65 | 2007-11-20 19:59:08 -0500 | [diff] [blame] | 204 | return 0; |
Alexey Starikovskiy | 7843932 | 2007-10-22 14:18:43 +0400 | [diff] [blame] | 205 | } |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 206 | } |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 207 | pr_err(PREFIX "acpi_ec_wait timeout," |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 208 | " status = %d, expect_event = %d\n", |
| 209 | acpi_ec_read_status(ec), event); |
Len Brown | 0af2f65 | 2007-11-20 19:59:08 -0500 | [diff] [blame] | 210 | return -ETIME; |
Dmitry Torokhov | 451566f | 2005-03-19 01:10:05 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 213 | static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command, |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 214 | const u8 * wdata, unsigned wdata_len, |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 215 | u8 * rdata, unsigned rdata_len, |
| 216 | int force_poll) |
Lennart Poettering | d7a76e4 | 2006-09-05 12:12:24 -0400 | [diff] [blame] | 217 | { |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 218 | int result = 0; |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 219 | set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 220 | acpi_ec_write_cmd(ec, command); |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 221 | pr_debug(PREFIX "transaction start\n"); |
Alexey Starikovskiy | 78d0af3 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 222 | for (; wdata_len > 0; --wdata_len) { |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 223 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll); |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 224 | if (result) { |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 225 | pr_err(PREFIX |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 226 | "write_cmd timeout, command = %d\n", command); |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 227 | goto end; |
| 228 | } |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 229 | set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 230 | acpi_ec_write_data(ec, *(wdata++)); |
Denis M. Sadykov | 3576cf6 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 231 | } |
Lennart Poettering | d7a76e4 | 2006-09-05 12:12:24 -0400 | [diff] [blame] | 232 | |
Alexey Starikovskiy | d91df1a | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 233 | if (!rdata_len) { |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 234 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll); |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 235 | if (result) { |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 236 | pr_err(PREFIX |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 237 | "finish-write timeout, command = %d\n", command); |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 238 | goto end; |
| 239 | } |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 240 | } else if (command == ACPI_EC_COMMAND_QUERY) |
| 241 | clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); |
Lennart Poettering | d7a76e4 | 2006-09-05 12:12:24 -0400 | [diff] [blame] | 242 | |
Alexey Starikovskiy | 78d0af3 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 243 | for (; rdata_len > 0; --rdata_len) { |
Len Brown | 0af2f65 | 2007-11-20 19:59:08 -0500 | [diff] [blame] | 244 | if (test_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags)) |
| 245 | force_poll = 1; |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 246 | result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll); |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 247 | if (result) { |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 248 | pr_err(PREFIX "read timeout, command = %d\n", command); |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 249 | goto end; |
| 250 | } |
Alexey Starikovskiy | 0c5d31f | 2007-10-22 14:18:36 +0400 | [diff] [blame] | 251 | /* Don't expect GPE after last read */ |
| 252 | if (rdata_len > 1) |
| 253 | set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 254 | *(rdata++) = acpi_ec_read_data(ec); |
Denis M. Sadykov | 7c6db5e | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 255 | } |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 256 | end: |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 257 | pr_debug(PREFIX "transaction end\n"); |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 258 | return result; |
Lennart Poettering | d7a76e4 | 2006-09-05 12:12:24 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Denis M. Sadykov | 3576cf6 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 261 | static int acpi_ec_transaction(struct acpi_ec *ec, u8 command, |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 262 | const u8 * wdata, unsigned wdata_len, |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 263 | u8 * rdata, unsigned rdata_len, |
| 264 | int force_poll) |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 265 | { |
Lennart Poettering | d7a76e4 | 2006-09-05 12:12:24 -0400 | [diff] [blame] | 266 | int status; |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 267 | u32 glk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Lennart Poettering | d7a76e4 | 2006-09-05 12:12:24 -0400 | [diff] [blame] | 269 | if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 270 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 272 | if (rdata) |
| 273 | memset(rdata, 0, rdata_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Alexey Starikovskiy | 523953b | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 275 | mutex_lock(&ec->lock); |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 276 | if (ec->global_lock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk); |
Alexey Starikovskiy | c24e912 | 2007-02-15 23:16:18 +0300 | [diff] [blame] | 278 | if (ACPI_FAILURE(status)) { |
| 279 | mutex_unlock(&ec->lock); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 280 | return -ENODEV; |
Alexey Starikovskiy | c24e912 | 2007-02-15 23:16:18 +0300 | [diff] [blame] | 281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
Dmitry Torokhov | 451566f | 2005-03-19 01:10:05 -0500 | [diff] [blame] | 283 | |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 284 | status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0); |
Luming Yu | 716e084 | 2005-08-10 01:40:00 -0400 | [diff] [blame] | 285 | if (status) { |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 286 | pr_err(PREFIX "input buffer is not empty, " |
| 287 | "aborting transaction\n"); |
Dmitry Torokhov | 451566f | 2005-03-19 01:10:05 -0500 | [diff] [blame] | 288 | goto end; |
Luming Yu | 716e084 | 2005-08-10 01:40:00 -0400 | [diff] [blame] | 289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 291 | status = acpi_ec_transaction_unlocked(ec, command, |
| 292 | wdata, wdata_len, |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 293 | rdata, rdata_len, |
| 294 | force_poll); |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 295 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 296 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 298 | if (ec->global_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | acpi_release_global_lock(glk); |
Alexey Starikovskiy | 523953b | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 300 | mutex_unlock(&ec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 302 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Alexey Starikovskiy | c45aac4 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 305 | /* |
| 306 | * Note: samsung nv5000 doesn't work with ec burst mode. |
| 307 | * http://bugzilla.kernel.org/show_bug.cgi?id=4980 |
| 308 | */ |
| 309 | int acpi_ec_burst_enable(struct acpi_ec *ec) |
| 310 | { |
| 311 | u8 d; |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 312 | return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0); |
Alexey Starikovskiy | c45aac4 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | int acpi_ec_burst_disable(struct acpi_ec *ec) |
| 316 | { |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 317 | return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0); |
Alexey Starikovskiy | c45aac4 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 318 | } |
| 319 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 320 | static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data) |
Denis M. Sadykov | 3576cf6 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 321 | { |
| 322 | int result; |
| 323 | u8 d; |
| 324 | |
| 325 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 326 | &address, 1, &d, 1, 0); |
Denis M. Sadykov | 3576cf6 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 327 | *data = d; |
| 328 | return result; |
| 329 | } |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 330 | |
Denis M. Sadykov | 3576cf6 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 331 | static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) |
| 332 | { |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 333 | u8 wdata[2] = { address, data }; |
| 334 | return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 335 | wdata, 2, NULL, 0, 0); |
Denis M. Sadykov | 3576cf6 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 336 | } |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | /* |
| 339 | * Externally callable EC access functions. For now, assume 1 EC only |
| 340 | */ |
Alexey Starikovskiy | c45aac4 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 341 | int ec_burst_enable(void) |
| 342 | { |
Alexey Starikovskiy | c45aac4 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 343 | if (!first_ec) |
| 344 | return -ENODEV; |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 345 | return acpi_ec_burst_enable(first_ec); |
Alexey Starikovskiy | c45aac4 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | EXPORT_SYMBOL(ec_burst_enable); |
| 349 | |
| 350 | int ec_burst_disable(void) |
| 351 | { |
Alexey Starikovskiy | c45aac4 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 352 | if (!first_ec) |
| 353 | return -ENODEV; |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 354 | return acpi_ec_burst_disable(first_ec); |
Alexey Starikovskiy | c45aac4 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | EXPORT_SYMBOL(ec_burst_disable); |
| 358 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 359 | int ec_read(u8 addr, u8 * val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | int err; |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 362 | u8 temp_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | if (!first_ec) |
| 365 | return -ENODEV; |
| 366 | |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 367 | err = acpi_ec_read(first_ec, addr, &temp_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | if (!err) { |
| 370 | *val = temp_data; |
| 371 | return 0; |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 372 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return err; |
| 374 | } |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | EXPORT_SYMBOL(ec_read); |
| 377 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 378 | int ec_write(u8 addr, u8 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | int err; |
| 381 | |
| 382 | if (!first_ec) |
| 383 | return -ENODEV; |
| 384 | |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 385 | err = acpi_ec_write(first_ec, addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | return err; |
| 388 | } |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | EXPORT_SYMBOL(ec_write); |
| 391 | |
Randy Dunlap | 616362d | 2006-10-27 01:47:34 -0400 | [diff] [blame] | 392 | int ec_transaction(u8 command, |
Alexey Starikovskiy | 9e19721 | 2007-03-07 18:29:35 -0500 | [diff] [blame] | 393 | const u8 * wdata, unsigned wdata_len, |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 394 | u8 * rdata, unsigned rdata_len, |
| 395 | int force_poll) |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 396 | { |
Lennart Poettering | d7a76e4 | 2006-09-05 12:12:24 -0400 | [diff] [blame] | 397 | if (!first_ec) |
| 398 | return -ENODEV; |
| 399 | |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 400 | return acpi_ec_transaction(first_ec, command, wdata, |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 401 | wdata_len, rdata, rdata_len, |
| 402 | force_poll); |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 403 | } |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 404 | |
Lennart Poettering | ab9e43c | 2006-10-03 22:49:00 -0400 | [diff] [blame] | 405 | EXPORT_SYMBOL(ec_transaction); |
| 406 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 407 | static int acpi_ec_query(struct acpi_ec *ec, u8 * data) |
Denis M. Sadykov | 3576cf6 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 408 | { |
| 409 | int result; |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 410 | u8 d; |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 411 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 412 | if (!ec || !data) |
| 413 | return -EINVAL; |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 414 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 415 | /* |
| 416 | * Query the EC to find out which _Qxx method we need to evaluate. |
| 417 | * Note that successful completion of the query causes the ACPI_EC_SCI |
| 418 | * bit to be cleared (and thus clearing the interrupt source). |
| 419 | */ |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 420 | |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 421 | result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0); |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 422 | if (result) |
| 423 | return result; |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 424 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 425 | if (!d) |
| 426 | return -ENODATA; |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 427 | |
Alexey Starikovskiy | 6ccedb1 | 2006-12-07 18:42:17 +0300 | [diff] [blame] | 428 | *data = d; |
| 429 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | /* -------------------------------------------------------------------------- |
| 433 | Event Management |
| 434 | -------------------------------------------------------------------------- */ |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 435 | int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit, |
| 436 | acpi_handle handle, acpi_ec_query_func func, |
| 437 | void *data) |
| 438 | { |
| 439 | struct acpi_ec_query_handler *handler = |
| 440 | kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL); |
| 441 | if (!handler) |
| 442 | return -ENOMEM; |
| 443 | |
| 444 | handler->query_bit = query_bit; |
| 445 | handler->handle = handle; |
| 446 | handler->func = func; |
| 447 | handler->data = data; |
| 448 | mutex_lock(&ec->lock); |
Alexey Starikovskiy | 30c0857 | 2007-09-26 19:43:22 +0400 | [diff] [blame] | 449 | list_add(&handler->node, &ec->list); |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 450 | mutex_unlock(&ec->lock); |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler); |
| 455 | |
| 456 | void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit) |
| 457 | { |
Adrian Bunk | 1544fdb | 2007-10-24 18:26:00 +0200 | [diff] [blame] | 458 | struct acpi_ec_query_handler *handler, *tmp; |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 459 | mutex_lock(&ec->lock); |
Adrian Bunk | 1544fdb | 2007-10-24 18:26:00 +0200 | [diff] [blame] | 460 | list_for_each_entry_safe(handler, tmp, &ec->list, node) { |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 461 | if (query_bit == handler->query_bit) { |
| 462 | list_del(&handler->node); |
| 463 | kfree(handler); |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | mutex_unlock(&ec->lock); |
| 467 | } |
| 468 | |
| 469 | EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 471 | static void acpi_ec_gpe_query(void *ec_cxt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
Alexey Starikovskiy | 3d02b90 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 473 | struct acpi_ec *ec = ec_cxt; |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 474 | u8 value = 0; |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 475 | struct acpi_ec_query_handler *handler, copy; |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 476 | |
Alexey Starikovskiy | 5d0c288 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 477 | if (!ec || acpi_ec_query(ec, &value)) |
Alexey Starikovskiy | e41334c | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 478 | return; |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 479 | mutex_lock(&ec->lock); |
| 480 | list_for_each_entry(handler, &ec->list, node) { |
| 481 | if (value == handler->query_bit) { |
| 482 | /* have custom handler for this bit */ |
| 483 | memcpy(©, handler, sizeof(copy)); |
| 484 | mutex_unlock(&ec->lock); |
| 485 | if (copy.func) { |
| 486 | copy.func(copy.data); |
| 487 | } else if (copy.handle) { |
| 488 | acpi_evaluate_object(copy.handle, NULL, NULL, NULL); |
| 489 | } |
| 490 | return; |
| 491 | } |
| 492 | } |
| 493 | mutex_unlock(&ec->lock); |
Luming Yu | 45bea15 | 2005-07-23 04:08:00 -0400 | [diff] [blame] | 494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 496 | static u32 acpi_ec_gpe_handler(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 498 | acpi_status status = AE_OK; |
Alexey Starikovskiy | 3d02b90 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 499 | struct acpi_ec *ec = data; |
Lennart Poettering | 00eb43a | 2007-05-04 14:16:19 +0200 | [diff] [blame] | 500 | |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 501 | pr_debug(PREFIX "~~~> interrupt\n"); |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 502 | clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); |
Alexey Starikovskiy | 7843932 | 2007-10-22 14:18:43 +0400 | [diff] [blame] | 503 | if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) |
Alexey Starikovskiy | af3fd14 | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 504 | wake_up(&ec->wait); |
Dmitry Torokhov | 451566f | 2005-03-19 01:10:05 -0500 | [diff] [blame] | 505 | |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 506 | if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) { |
| 507 | if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) |
| 508 | status = acpi_os_execute(OSL_EC_BURST_HANDLER, |
| 509 | acpi_ec_gpe_query, ec); |
Alexey Starikovskiy | 95b937e | 2007-10-22 14:19:03 +0400 | [diff] [blame] | 510 | } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) { |
| 511 | /* this is non-query, must be confirmation */ |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 512 | if (printk_ratelimit()) |
| 513 | pr_info(PREFIX "non-query interrupt received," |
| 514 | " switching to interrupt mode\n"); |
Alexey Starikovskiy | 7843932 | 2007-10-22 14:18:43 +0400 | [diff] [blame] | 515 | set_bit(EC_FLAGS_GPE_MODE, &ec->flags); |
Alexey Starikovskiy | 95b937e | 2007-10-22 14:19:03 +0400 | [diff] [blame] | 516 | } |
Alexey Starikovskiy | e41334c | 2006-12-07 18:42:16 +0300 | [diff] [blame] | 517 | |
Alexey Starikovskiy | 7843932 | 2007-10-22 14:18:43 +0400 | [diff] [blame] | 518 | return ACPI_SUCCESS(status) ? |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 519 | ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* -------------------------------------------------------------------------- |
| 523 | Address Space Management |
| 524 | -------------------------------------------------------------------------- */ |
| 525 | |
| 526 | static acpi_status |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 527 | acpi_ec_space_setup(acpi_handle region_handle, |
| 528 | u32 function, void *handler_context, void **return_context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
| 530 | /* |
| 531 | * The EC object is in the handler context and is needed |
| 532 | * when calling the acpi_ec_space_handler. |
| 533 | */ |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 534 | *return_context = (function != ACPI_REGION_DEACTIVATE) ? |
| 535 | handler_context : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | return AE_OK; |
| 538 | } |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | static acpi_status |
Alexey Starikovskiy | 5b7734b | 2007-05-29 16:42:52 +0400 | [diff] [blame] | 541 | acpi_ec_space_handler(u32 function, acpi_physical_address address, |
| 542 | u32 bits, acpi_integer *value, |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 543 | void *handler_context, void *region_context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
Alexey Starikovskiy | 3d02b90 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 545 | struct acpi_ec *ec = handler_context; |
Alexey Starikovskiy | 5b7734b | 2007-05-29 16:42:52 +0400 | [diff] [blame] | 546 | int result = 0, i = 0; |
| 547 | u8 temp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | if ((address > 0xFF) || !value || !handler_context) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 550 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Alexey Starikovskiy | 5b7734b | 2007-05-29 16:42:52 +0400 | [diff] [blame] | 552 | if (function != ACPI_READ && function != ACPI_WRITE) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 553 | return AE_BAD_PARAMETER; |
Alexey Starikovskiy | 5b7734b | 2007-05-29 16:42:52 +0400 | [diff] [blame] | 554 | |
| 555 | if (bits != 8 && acpi_strict) |
| 556 | return AE_BAD_PARAMETER; |
| 557 | |
| 558 | while (bits - i > 0) { |
| 559 | if (function == ACPI_READ) { |
| 560 | result = acpi_ec_read(ec, address, &temp); |
| 561 | (*value) |= ((acpi_integer)temp) << i; |
| 562 | } else { |
| 563 | temp = 0xff & ((*value) >> i); |
| 564 | result = acpi_ec_write(ec, address, temp); |
| 565 | } |
| 566 | i += 8; |
| 567 | ++address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | switch (result) { |
| 571 | case -EINVAL: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 572 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | break; |
| 574 | case -ENODEV: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 575 | return AE_NOT_FOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | break; |
| 577 | case -ETIME: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 578 | return AE_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | break; |
| 580 | default: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 581 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | /* -------------------------------------------------------------------------- |
| 586 | FS Interface (/proc) |
| 587 | -------------------------------------------------------------------------- */ |
| 588 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 589 | static struct proc_dir_entry *acpi_ec_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 591 | static int acpi_ec_read_info(struct seq_file *seq, void *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
Alexey Starikovskiy | 3d02b90 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 593 | struct acpi_ec *ec = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | if (!ec) |
| 596 | goto end; |
| 597 | |
Alexey Starikovskiy | 01f22462 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 598 | seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe); |
| 599 | seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n", |
| 600 | (unsigned)ec->command_addr, (unsigned)ec->data_addr); |
| 601 | seq_printf(seq, "use global lock:\t%s\n", |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 602 | ec->global_lock ? "yes" : "no"); |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 603 | end: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 604 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static int acpi_ec_info_open_fs(struct inode *inode, struct file *file) |
| 608 | { |
| 609 | return single_open(file, acpi_ec_read_info, PDE(inode)->data); |
| 610 | } |
| 611 | |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 612 | static struct file_operations acpi_ec_info_ops = { |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 613 | .open = acpi_ec_info_open_fs, |
| 614 | .read = seq_read, |
| 615 | .llseek = seq_lseek, |
| 616 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | .owner = THIS_MODULE, |
| 618 | }; |
| 619 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 620 | static int acpi_ec_add_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 622 | struct proc_dir_entry *entry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | if (!acpi_device_dir(device)) { |
| 625 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 626 | acpi_ec_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | if (!acpi_device_dir(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 628 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO, |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 632 | acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 634 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | else { |
| 636 | entry->proc_fops = &acpi_ec_info_ops; |
| 637 | entry->data = acpi_driver_data(device); |
| 638 | entry->owner = THIS_MODULE; |
| 639 | } |
| 640 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 641 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 644 | static int acpi_ec_remove_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
| 647 | if (acpi_device_dir(device)) { |
| 648 | remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device)); |
| 649 | remove_proc_entry(acpi_device_bid(device), acpi_ec_dir); |
| 650 | acpi_device_dir(device) = NULL; |
| 651 | } |
| 652 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 653 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | /* -------------------------------------------------------------------------- |
| 657 | Driver Interface |
| 658 | -------------------------------------------------------------------------- */ |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 659 | static acpi_status |
| 660 | ec_parse_io_ports(struct acpi_resource *resource, void *context); |
| 661 | |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 662 | static struct acpi_ec *make_acpi_ec(void) |
| 663 | { |
| 664 | struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL); |
| 665 | if (!ec) |
| 666 | return NULL; |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 667 | ec->flags = 1 << EC_FLAGS_QUERY_PENDING; |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 668 | mutex_init(&ec->lock); |
| 669 | init_waitqueue_head(&ec->wait); |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 670 | INIT_LIST_HEAD(&ec->list); |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 671 | return ec; |
| 672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 674 | static acpi_status |
Alexey Starikovskiy | c019b19 | 2007-08-14 01:03:42 -0400 | [diff] [blame] | 675 | acpi_ec_register_query_methods(acpi_handle handle, u32 level, |
| 676 | void *context, void **return_value) |
| 677 | { |
| 678 | struct acpi_namespace_node *node = handle; |
| 679 | struct acpi_ec *ec = context; |
| 680 | int value = 0; |
| 681 | if (sscanf(node->name.ascii, "_Q%x", &value) == 1) { |
| 682 | acpi_ec_add_query_handler(ec, value, handle, NULL, NULL); |
| 683 | } |
| 684 | return AE_OK; |
| 685 | } |
| 686 | |
| 687 | static acpi_status |
Alexey Starikovskiy | cd8c93a | 2007-08-03 17:52:48 -0400 | [diff] [blame] | 688 | ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval) |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 689 | { |
Alexey Starikovskiy | cd8c93a | 2007-08-03 17:52:48 -0400 | [diff] [blame] | 690 | acpi_status status; |
| 691 | |
| 692 | struct acpi_ec *ec = context; |
| 693 | status = acpi_walk_resources(handle, METHOD_NAME__CRS, |
| 694 | ec_parse_io_ports, ec); |
| 695 | if (ACPI_FAILURE(status)) |
| 696 | return status; |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 697 | |
| 698 | /* Get GPE bit assignment (EC events). */ |
| 699 | /* TODO: Add support for _GPE returning a package */ |
Alexey Starikovskiy | cd8c93a | 2007-08-03 17:52:48 -0400 | [diff] [blame] | 700 | status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe); |
| 701 | if (ACPI_FAILURE(status)) |
| 702 | return status; |
Alexey Starikovskiy | c019b19 | 2007-08-14 01:03:42 -0400 | [diff] [blame] | 703 | /* Find and register all query methods */ |
| 704 | acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1, |
| 705 | acpi_ec_register_query_methods, ec, NULL); |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 706 | /* Use the global lock for all EC transactions? */ |
| 707 | acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock); |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 708 | ec->handle = handle; |
Alexey Starikovskiy | cd8c93a | 2007-08-03 17:52:48 -0400 | [diff] [blame] | 709 | return AE_CTRL_TERMINATE; |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 710 | } |
| 711 | |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 712 | static void ec_remove_handlers(struct acpi_ec *ec) |
| 713 | { |
| 714 | if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle, |
| 715 | ACPI_ADR_SPACE_EC, &acpi_ec_space_handler))) |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 716 | pr_err(PREFIX "failed to remove space handler\n"); |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 717 | if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe, |
| 718 | &acpi_ec_gpe_handler))) |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 719 | pr_err(PREFIX "failed to remove gpe handler\n"); |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 720 | ec->handlers_installed = 0; |
| 721 | } |
| 722 | |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 723 | static int acpi_ec_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | { |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 725 | struct acpi_ec *ec = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 728 | return -EINVAL; |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 729 | strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME); |
| 730 | strcpy(acpi_device_class(device), ACPI_EC_CLASS); |
| 731 | |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 732 | /* Check for boot EC */ |
| 733 | if (boot_ec) { |
| 734 | if (boot_ec->handle == device->handle) { |
| 735 | /* Pre-loaded EC from DSDT, just move pointer */ |
| 736 | ec = boot_ec; |
| 737 | boot_ec = NULL; |
| 738 | goto end; |
| 739 | } else if (boot_ec->handle == ACPI_ROOT_OBJECT) { |
| 740 | /* ECDT-based EC, time to shut it down */ |
| 741 | ec_remove_handlers(boot_ec); |
| 742 | kfree(boot_ec); |
| 743 | first_ec = boot_ec = NULL; |
| 744 | } |
| 745 | } |
| 746 | |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 747 | ec = make_acpi_ec(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | if (!ec) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 749 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Alexey Starikovskiy | cd8c93a | 2007-08-03 17:52:48 -0400 | [diff] [blame] | 751 | if (ec_parse_device(device->handle, 0, ec, NULL) != |
| 752 | AE_CTRL_TERMINATE) { |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 753 | kfree(ec); |
| 754 | return -EINVAL; |
Denis M. Sadykov | 703959d | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 755 | } |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 756 | ec->handle = device->handle; |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 757 | end: |
| 758 | if (!first_ec) |
| 759 | first_ec = ec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | acpi_driver_data(device) = ec; |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 761 | acpi_ec_add_fs(device); |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 762 | pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n", |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 763 | ec->gpe, ec->command_addr, ec->data_addr); |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 764 | pr_info(PREFIX "driver started in %s mode\n", |
Alexey Starikovskiy | 95b937e | 2007-10-22 14:19:03 +0400 | [diff] [blame] | 765 | (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll"); |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 766 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 769 | static int acpi_ec_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | { |
Alexey Starikovskiy | 01f22462 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 771 | struct acpi_ec *ec; |
Adrian Bunk | 07ddf76 | 2007-07-29 17:00:37 +0200 | [diff] [blame] | 772 | struct acpi_ec_query_handler *handler, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 775 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | ec = acpi_driver_data(device); |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 778 | mutex_lock(&ec->lock); |
Adrian Bunk | 07ddf76 | 2007-07-29 17:00:37 +0200 | [diff] [blame] | 779 | list_for_each_entry_safe(handler, tmp, &ec->list, node) { |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 780 | list_del(&handler->node); |
| 781 | kfree(handler); |
| 782 | } |
| 783 | mutex_unlock(&ec->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | acpi_ec_remove_fs(device); |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 785 | acpi_driver_data(device) = NULL; |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 786 | if (ec == first_ec) |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 787 | first_ec = NULL; |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 788 | kfree(ec); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 789 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | static acpi_status |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 793 | ec_parse_io_ports(struct acpi_resource *resource, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
Alexey Starikovskiy | 3d02b90 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 795 | struct acpi_ec *ec = context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Alexey Starikovskiy | 01f22462 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 797 | if (resource->type != ACPI_RESOURCE_TYPE_IO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
| 800 | /* |
| 801 | * The first address region returned is the data port, and |
| 802 | * the second address region returned is the status/command |
| 803 | * port. |
| 804 | */ |
Alexey Starikovskiy | 01f22462 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 805 | if (ec->data_addr == 0) |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 806 | ec->data_addr = resource->data.io.minimum; |
Alexey Starikovskiy | 01f22462 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 807 | else if (ec->command_addr == 0) |
Denis M. Sadykov | 6ffb221 | 2006-09-26 19:50:33 +0400 | [diff] [blame] | 808 | ec->command_addr = resource->data.io.minimum; |
Alexey Starikovskiy | 01f22462 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 809 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | return AE_CTRL_TERMINATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | return AE_OK; |
| 813 | } |
| 814 | |
Alexey Starikovskiy | e828432 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 815 | static int ec_install_handlers(struct acpi_ec *ec) |
| 816 | { |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 817 | acpi_status status; |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 818 | if (ec->handlers_installed) |
| 819 | return 0; |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 820 | status = acpi_install_gpe_handler(NULL, ec->gpe, |
| 821 | ACPI_GPE_EDGE_TRIGGERED, |
| 822 | &acpi_ec_gpe_handler, ec); |
Alexey Starikovskiy | e828432 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 823 | if (ACPI_FAILURE(status)) |
| 824 | return -ENODEV; |
Alexey Starikovskiy | 01f22462 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 825 | |
Alexey Starikovskiy | e828432 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 826 | acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME); |
| 827 | acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); |
| 828 | |
| 829 | status = acpi_install_address_space_handler(ec->handle, |
| 830 | ACPI_ADR_SPACE_EC, |
| 831 | &acpi_ec_space_handler, |
| 832 | &acpi_ec_space_setup, ec); |
| 833 | if (ACPI_FAILURE(status)) { |
| 834 | acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler); |
| 835 | return -ENODEV; |
| 836 | } |
| 837 | |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 838 | ec->handlers_installed = 1; |
Alexey Starikovskiy | e828432 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 839 | return 0; |
| 840 | } |
| 841 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 842 | static int acpi_ec_start(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | { |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 844 | struct acpi_ec *ec; |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 845 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 848 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
| 850 | ec = acpi_driver_data(device); |
| 851 | |
| 852 | if (!ec) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 853 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 855 | ret = ec_install_handlers(ec); |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 856 | |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 857 | /* EC is fully operational, allow queries */ |
Alexey Starikovskiy | 080e412 | 2007-10-22 14:18:30 +0400 | [diff] [blame] | 858 | clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); |
Alexey Starikovskiy | 837012e | 2007-05-29 16:43:02 +0400 | [diff] [blame] | 859 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 862 | static int acpi_ec_stop(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | { |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 864 | struct acpi_ec *ec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 866 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | ec = acpi_driver_data(device); |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 868 | if (!ec) |
| 869 | return -EINVAL; |
Alexey Starikovskiy | 4c61106 | 2007-09-05 19:56:38 -0400 | [diff] [blame] | 870 | ec_remove_handlers(ec); |
Alexey Starikovskiy | f9319f9 | 2007-08-24 08:10:11 +0400 | [diff] [blame] | 871 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 872 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 875 | int __init acpi_ec_ecdt_probe(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | { |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 877 | int ret; |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 878 | acpi_status status; |
| 879 | struct acpi_table_ecdt *ecdt_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
Alexey Starikovskiy | d66d969 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 881 | boot_ec = make_acpi_ec(); |
| 882 | if (!boot_ec) |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 883 | return -ENOMEM; |
| 884 | /* |
| 885 | * Generate a boot ec context |
| 886 | */ |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 887 | status = acpi_get_table(ACPI_SIG_ECDT, 1, |
| 888 | (struct acpi_table_header **)&ecdt_ptr); |
Alexey Starikovskiy | cd8c93a | 2007-08-03 17:52:48 -0400 | [diff] [blame] | 889 | if (ACPI_SUCCESS(status)) { |
Márton Németh | 3ebe08a | 2007-11-21 03:23:26 +0300 | [diff] [blame^] | 890 | pr_info(PREFIX "EC description table is found, configuring boot EC\n"); |
Alexey Starikovskiy | cd8c93a | 2007-08-03 17:52:48 -0400 | [diff] [blame] | 891 | boot_ec->command_addr = ecdt_ptr->control.address; |
| 892 | boot_ec->data_addr = ecdt_ptr->data.address; |
| 893 | boot_ec->gpe = ecdt_ptr->gpe; |
| 894 | boot_ec->handle = ACPI_ROOT_OBJECT; |
| 895 | } else { |
| 896 | printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n"); |
| 897 | status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, |
| 898 | boot_ec, NULL); |
Alexey Starikovskiy | 2d8348b | 2007-08-31 09:05:26 +0400 | [diff] [blame] | 899 | /* Check that acpi_get_devices actually find something */ |
| 900 | if (ACPI_FAILURE(status) || !boot_ec->handle) |
Alexey Starikovskiy | cd8c93a | 2007-08-03 17:52:48 -0400 | [diff] [blame] | 901 | goto error; |
| 902 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Alexey Starikovskiy | d66d969 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 904 | ret = ec_install_handlers(boot_ec); |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 905 | if (!ret) { |
| 906 | first_ec = boot_ec; |
Alexey Starikovskiy | e828432 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 907 | return 0; |
Alexey Starikovskiy | d033879 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 908 | } |
Alexey Starikovskiy | c0900c3 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 909 | error: |
Alexey Starikovskiy | d66d969 | 2007-03-07 22:28:00 +0300 | [diff] [blame] | 910 | kfree(boot_ec); |
| 911 | boot_ec = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | return -ENODEV; |
| 913 | } |
| 914 | |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 915 | static int __init acpi_ec_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | { |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 917 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (acpi_disabled) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 920 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
| 922 | acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir); |
| 923 | if (!acpi_ec_dir) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 924 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
| 926 | /* Now register the driver for the EC */ |
| 927 | result = acpi_bus_register_driver(&acpi_ec_driver); |
| 928 | if (result < 0) { |
| 929 | remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 930 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 933 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | subsys_initcall(acpi_ec_init); |
| 937 | |
| 938 | /* EC driver currently not unloadable */ |
| 939 | #if 0 |
Len Brown | 50526df | 2005-08-11 17:32:05 -0400 | [diff] [blame] | 940 | static void __exit acpi_ec_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
| 943 | acpi_bus_unregister_driver(&acpi_ec_driver); |
| 944 | |
| 945 | remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir); |
| 946 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 947 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } |
Alexey Starikovskiy | 7843932 | 2007-10-22 14:18:43 +0400 | [diff] [blame] | 949 | #endif /* 0 */ |