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