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