blob: 601dbe8b407a5e13701f3585fd044f37e3edae3c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh6a2e2932008-01-08 13:02:56 -02006 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh1cee5cc2008-01-08 13:02:57 -020024#define TPACPI_VERSION "0.19"
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -020025#define TPACPI_SYSFS_VERSION 0x020200
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
57#include <linux/kthread.h>
58#include <linux/freezer.h>
59#include <linux/delay.h>
60
61#include <linux/nvram.h>
62#include <linux/proc_fs.h>
63#include <linux/sysfs.h>
64#include <linux/backlight.h>
65#include <linux/fb.h>
66#include <linux/platform_device.h>
67#include <linux/hwmon.h>
68#include <linux/hwmon-sysfs.h>
69#include <linux/input.h>
70#include <asm/uaccess.h>
71
72#include <linux/dmi.h>
73#include <linux/jiffies.h>
74#include <linux/workqueue.h>
75
76#include <acpi/acpi_drivers.h>
77#include <acpi/acnamesp.h>
78
79#include <linux/pci_ids.h>
80
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020081
82/* ThinkPad CMOS commands */
83#define TP_CMOS_VOLUME_DOWN 0
84#define TP_CMOS_VOLUME_UP 1
85#define TP_CMOS_VOLUME_MUTE 2
86#define TP_CMOS_BRIGHTNESS_UP 4
87#define TP_CMOS_BRIGHTNESS_DOWN 5
88
89/* NVRAM Addresses */
90enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
96};
97
98/* NVRAM bit masks */
99enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
113};
114
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200115/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200116#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200117
118/* Input IDs */
119#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120#define TPACPI_HKEY_INPUT_VERSION 0x4101
121
122
123/****************************************************************************
124 * Main driver
125 */
126
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200127#define TPACPI_NAME "thinkpad"
128#define TPACPI_DESC "ThinkPad ACPI Extras"
129#define TPACPI_FILE TPACPI_NAME "_acpi"
130#define TPACPI_URL "http://ibm-acpi.sf.net/"
131#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200133#define TPACPI_PROC_DIR "ibm"
134#define TPACPI_ACPI_EVENT_PREFIX "ibm"
135#define TPACPI_DRVR_NAME TPACPI_FILE
136#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200137
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200138#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200139
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200140/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200141#define TPACPI_LOG TPACPI_FILE ": "
142#define TPACPI_ERR KERN_ERR TPACPI_LOG
143#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144#define TPACPI_INFO KERN_INFO TPACPI_LOG
145#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200146
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200147#define TPACPI_DBG_ALL 0xffff
148#define TPACPI_DBG_ALL 0xffff
149#define TPACPI_DBG_INIT 0x0001
150#define TPACPI_DBG_EXIT 0x0002
151#define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200155#ifdef CONFIG_THINKPAD_ACPI_DEBUG
156#define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158static const char *str_supported(int is_supported);
159#else
160#define vdbg_printk(a_dbg_level, format, arg...)
161#endif
162
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200163#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200166
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200167
168/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200169 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200170 */
171
172struct ibm_struct;
173
174struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
177
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
182};
183
184struct ibm_struct {
185 char *name;
186
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200191 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200192
193 struct list_head all_drivers;
194
195 struct tp_acpi_drv_struct *acpi;
196
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
204};
205
206struct ibm_init_struct {
207 char param[32];
208
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
211};
212
213static struct {
214#ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219#endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200224 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200225 u32 light:1;
226 u32 light_status:1;
227 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300228 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200229 u32 wan:1;
230 u32 fan_ctrl_status_undef:1;
231 u32 input_device_registered:1;
232 u32 platform_drv_registered:1;
233 u32 platform_drv_attrs_registered:1;
234 u32 sensors_pdrv_registered:1;
235 u32 sensors_pdrv_attrs_registered:1;
236 u32 sensors_pdev_attrs_registered:1;
237 u32 hotkey_poll_active:1;
238} tp_features;
239
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300240static struct {
241 u16 hotkey_mask_ff:1;
242} tp_warned;
243
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200244struct thinkpad_id_data {
245 unsigned int vendor; /* ThinkPad vendor:
246 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
247
248 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
249 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
250
251 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
252 u16 ec_model;
253
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300254 char *model_str; /* ThinkPad T43 */
255 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257static struct thinkpad_id_data thinkpad_id;
258
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300259static enum {
260 TPACPI_LIFE_INIT = 0,
261 TPACPI_LIFE_RUNNING,
262 TPACPI_LIFE_EXITING,
263} tpacpi_lifecycle;
264
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200265static int experimental;
266static u32 dbg_level;
267
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300268/****************************************************************************
269 ****************************************************************************
270 *
271 * ACPI Helpers and device model
272 *
273 ****************************************************************************
274 ****************************************************************************/
275
276/*************************************************************************
277 * ACPI basic handles
278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300280static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200282#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 static acpi_handle object##_handle; \
284 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400285 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 static char *object##_paths[] = { paths }
287
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200288TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400289 "\\_SB.PCI.ISA.EC", /* 570 */
290 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
291 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
292 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
293 "\\_SB.PCI0.ICH3.EC0", /* R31 */
294 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300295 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200297TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
298TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200300TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
301 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400302 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
303 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300304 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400305
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200306TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400307 "^HKEY", /* R30, R31 */
308 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300309 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400310
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200311TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
312 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
313 "\\_SB.PCI0.VID0", /* 770e */
314 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
315 "\\_SB.PCI0.AGP.VID", /* all others */
316 ); /* R30, R31 */
317
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400318
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300319/*************************************************************************
320 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200321 */
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static int acpi_evalf(acpi_handle handle,
324 void *res, char *method, char *fmt, ...)
325{
326 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400327 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200328 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400329 struct acpi_buffer result, *resultp;
330 union acpi_object out_obj;
331 acpi_status status;
332 va_list ap;
333 char res_type;
334 int success;
335 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200338 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
340 }
341
342 if (*fmt == 'q') {
343 quiet = 1;
344 fmt++;
345 } else
346 quiet = 0;
347
348 res_type = *(fmt++);
349
350 params.count = 0;
351 params.pointer = &in_objs[0];
352
353 va_start(ap, fmt);
354 while (*fmt) {
355 char c = *(fmt++);
356 switch (c) {
357 case 'd': /* int */
358 in_objs[params.count].integer.value = va_arg(ap, int);
359 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
360 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400361 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200363 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 "with invalid format character '%c'\n", c);
365 return 0;
366 }
367 }
368 va_end(ap);
369
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400370 if (res_type != 'v') {
371 result.length = sizeof(out_obj);
372 result.pointer = &out_obj;
373 resultp = &result;
374 } else
375 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400377 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400380 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (res)
382 *(int *)res = out_obj.integer.value;
383 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
384 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400385 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 success = status == AE_OK;
387 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400388 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200390 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 "with invalid format character '%c'\n", res_type);
392 return 0;
393 }
394
395 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200396 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 method, fmt0, status);
398
399 return success;
400}
401
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200402static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300403{
404 int v;
405
406 if (ecrd_handle) {
407 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
408 return 0;
409 *p = v;
410 } else {
411 if (ec_read(i, p) < 0)
412 return 0;
413 }
414
415 return 1;
416}
417
418static int acpi_ec_write(int i, u8 v)
419{
420 if (ecwr_handle) {
421 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
422 return 0;
423 } else {
424 if (ec_write(i, v) < 0)
425 return 0;
426 }
427
428 return 1;
429}
430
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200431#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300432static int _sta(acpi_handle handle)
433{
434 int status;
435
436 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
437 status = 0;
438
439 return status;
440}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200441#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300442
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300443static int issue_thinkpad_cmos_command(int cmos_cmd)
444{
445 if (!cmos_handle)
446 return -ENXIO;
447
448 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
449 return -EIO;
450
451 return 0;
452}
453
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300454/*************************************************************************
455 * ACPI device model
456 */
457
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200458#define TPACPI_ACPIHANDLE_INIT(object) \
459 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200460 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
461
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300462static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300463 acpi_handle *handle, acpi_handle parent,
464 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300465{
466 int i;
467 acpi_status status;
468
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300469 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
470 name);
471
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300472 for (i = 0; i < num_paths; i++) {
473 status = acpi_get_handle(parent, paths[i], handle);
474 if (ACPI_SUCCESS(status)) {
475 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300476 dbg_printk(TPACPI_DBG_INIT,
477 "Found ACPI handle %s for %s\n",
478 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300479 return;
480 }
481 }
482
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300483 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
484 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300485 *handle = NULL;
486}
487
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300488static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300489{
490 struct ibm_struct *ibm = data;
491
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300492 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
493 return;
494
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300495 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300496 return;
497
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300498 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300499}
500
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300501static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300502{
503 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300504 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300505
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300506 BUG_ON(!ibm->acpi);
507
508 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300509 return 0;
510
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300511 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300512 "setting up ACPI notify for %s\n", ibm->name);
513
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300514 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
515 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200516 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300517 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300518 return -ENODEV;
519 }
520
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300521 acpi_driver_data(ibm->acpi->device) = ibm;
522 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200523 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300524 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300525
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300526 status = acpi_install_notify_handler(*ibm->acpi->handle,
527 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300528 if (ACPI_FAILURE(status)) {
529 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200530 printk(TPACPI_NOTICE
531 "another device driver is already "
532 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200534 printk(TPACPI_ERR
535 "acpi_install_notify_handler(%s) failed: %d\n",
536 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300537 }
538 return -ENODEV;
539 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300540 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541 return 0;
542}
543
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300544static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300545{
546 return 0;
547}
548
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300549static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300550{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300551 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300552
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300553 dbg_printk(TPACPI_DBG_INIT,
554 "registering %s as an ACPI driver\n", ibm->name);
555
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300556 BUG_ON(!ibm->acpi);
557
558 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
559 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200560 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300561 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300562 }
563
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200564 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300565 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200566
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300567 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300568
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300569 rc = acpi_bus_register_driver(ibm->acpi->driver);
570 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200571 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200572 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300573 kfree(ibm->acpi->driver);
574 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300575 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300576 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300577
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300578 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300579}
580
581
582/****************************************************************************
583 ****************************************************************************
584 *
585 * Procfs Helpers
586 *
587 ****************************************************************************
588 ****************************************************************************/
589
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300590static int dispatch_procfs_read(char *page, char **start, off_t off,
591 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300592{
593 struct ibm_struct *ibm = data;
594 int len;
595
596 if (!ibm || !ibm->read)
597 return -EINVAL;
598
599 len = ibm->read(page);
600 if (len < 0)
601 return len;
602
603 if (len <= off + count)
604 *eof = 1;
605 *start = page + off;
606 len -= off;
607 if (len > count)
608 len = count;
609 if (len < 0)
610 len = 0;
611
612 return len;
613}
614
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300615static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200616 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300617 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300618{
619 struct ibm_struct *ibm = data;
620 char *kernbuf;
621 int ret;
622
623 if (!ibm || !ibm->write)
624 return -EINVAL;
625
626 kernbuf = kmalloc(count + 2, GFP_KERNEL);
627 if (!kernbuf)
628 return -ENOMEM;
629
630 if (copy_from_user(kernbuf, userbuf, count)) {
631 kfree(kernbuf);
632 return -EFAULT;
633 }
634
635 kernbuf[count] = 0;
636 strcat(kernbuf, ",");
637 ret = ibm->write(kernbuf);
638 if (ret == 0)
639 ret = count;
640
641 kfree(kernbuf);
642
643 return ret;
644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static char *next_cmd(char **cmds)
647{
648 char *start = *cmds;
649 char *end;
650
651 while ((end = strchr(start, ',')) && end == start)
652 start = end + 1;
653
654 if (!end)
655 return NULL;
656
657 *end = 0;
658 *cmds = end + 1;
659 return start;
660}
661
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300662
663/****************************************************************************
664 ****************************************************************************
665 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300666 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300667 *
668 ****************************************************************************
669 ****************************************************************************/
670
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300671static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300672static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700673static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300674static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300675static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200676static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300677
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200678static int tpacpi_suspend_handler(struct platform_device *pdev,
679 pm_message_t state)
680{
681 struct ibm_struct *ibm, *itmp;
682
683 list_for_each_entry_safe(ibm, itmp,
684 &tpacpi_all_drivers,
685 all_drivers) {
686 if (ibm->suspend)
687 (ibm->suspend)(state);
688 }
689
690 return 0;
691}
692
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300693static int tpacpi_resume_handler(struct platform_device *pdev)
694{
695 struct ibm_struct *ibm, *itmp;
696
697 list_for_each_entry_safe(ibm, itmp,
698 &tpacpi_all_drivers,
699 all_drivers) {
700 if (ibm->resume)
701 (ibm->resume)();
702 }
703
704 return 0;
705}
706
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300707static struct platform_driver tpacpi_pdriver = {
708 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200709 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300710 .owner = THIS_MODULE,
711 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200712 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300713 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300714};
715
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300716static struct platform_driver tpacpi_hwmon_pdriver = {
717 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200718 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300719 .owner = THIS_MODULE,
720 },
721};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300722
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300723/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300724 * sysfs support helpers
725 */
726
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200727struct attribute_set {
728 unsigned int members, max_members;
729 struct attribute_group group;
730};
731
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300732struct attribute_set_obj {
733 struct attribute_set s;
734 struct attribute *a;
735} __attribute__((packed));
736
737static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200738 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300739{
740 struct attribute_set_obj *sobj;
741
742 if (max_members == 0)
743 return NULL;
744
745 /* Allocates space for implicit NULL at the end too */
746 sobj = kzalloc(sizeof(struct attribute_set_obj) +
747 max_members * sizeof(struct attribute *),
748 GFP_KERNEL);
749 if (!sobj)
750 return NULL;
751 sobj->s.max_members = max_members;
752 sobj->s.group.attrs = &sobj->a;
753 sobj->s.group.name = name;
754
755 return &sobj->s;
756}
757
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200758#define destroy_attr_set(_set) \
759 kfree(_set);
760
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300761/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200762static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300763{
764 if (!s || !attr)
765 return -EINVAL;
766
767 if (s->members >= s->max_members)
768 return -ENOMEM;
769
770 s->group.attrs[s->members] = attr;
771 s->members++;
772
773 return 0;
774}
775
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200776static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300777 struct attribute **attr,
778 unsigned int count)
779{
780 int i, res;
781
782 for (i = 0; i < count; i++) {
783 res = add_to_attr_set(s, attr[i]);
784 if (res)
785 return res;
786 }
787
788 return 0;
789}
790
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200791static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300792{
793 sysfs_remove_group(kobj, &s->group);
794 destroy_attr_set(s);
795}
796
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200797#define register_attr_set_with_sysfs(_attr_set, _kobj) \
798 sysfs_create_group(_kobj, &_attr_set->group)
799
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300800static int parse_strtoul(const char *buf,
801 unsigned long max, unsigned long *value)
802{
803 char *endp;
804
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300805 while (*buf && isspace(*buf))
806 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300807 *value = simple_strtoul(buf, &endp, 0);
808 while (*endp && isspace(*endp))
809 endp++;
810 if (*endp || *value > max)
811 return -EINVAL;
812
813 return 0;
814}
815
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300816static int __init tpacpi_query_bcl_levels(acpi_handle handle)
817{
818 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
819 union acpi_object *obj;
820 int rc;
821
822 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
823 obj = (union acpi_object *)buffer.pointer;
824 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
825 printk(TPACPI_ERR "Unknown _BCL data, "
826 "please report this to %s\n", TPACPI_MAIL);
827 rc = 0;
828 } else {
829 rc = obj->package.count;
830 }
831 } else {
832 return 0;
833 }
834
835 kfree(buffer.pointer);
836 return rc;
837}
838
839static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
840 u32 lvl, void *context, void **rv)
841{
842 char name[ACPI_PATH_SEGMENT_LENGTH];
843 struct acpi_buffer buffer = { sizeof(name), &name };
844
845 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
846 !strncmp("_BCL", name, sizeof(name) - 1)) {
847 BUG_ON(!rv || !*rv);
848 **(int **)rv = tpacpi_query_bcl_levels(handle);
849 return AE_CTRL_TERMINATE;
850 } else {
851 return AE_OK;
852 }
853}
854
855/*
856 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
857 */
858static int __init tpacpi_check_std_acpi_brightness_support(void)
859{
860 int status;
861 int bcl_levels = 0;
862 void *bcl_ptr = &bcl_levels;
863
864 if (!vid_handle) {
865 TPACPI_ACPIHANDLE_INIT(vid);
866 }
867 if (!vid_handle)
868 return 0;
869
870 /*
871 * Search for a _BCL method, and execute it. This is safe on all
872 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
873 * BIOS in ACPI backlight control mode. We do NOT have to care
874 * about calling the _BCL method in an enabled video device, any
875 * will do for our purposes.
876 */
877
878 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
879 tpacpi_acpi_walk_find_bcl, NULL,
880 &bcl_ptr);
881
882 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
883 tp_features.bright_acpimode = 1;
884 return (bcl_levels - 2);
885 }
886
887 return 0;
888}
889
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200890/*************************************************************************
891 * thinkpad-acpi driver attributes
892 */
893
894/* interface_version --------------------------------------------------- */
895static ssize_t tpacpi_driver_interface_version_show(
896 struct device_driver *drv,
897 char *buf)
898{
899 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
900}
901
902static DRIVER_ATTR(interface_version, S_IRUGO,
903 tpacpi_driver_interface_version_show, NULL);
904
905/* debug_level --------------------------------------------------------- */
906static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
907 char *buf)
908{
909 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
910}
911
912static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
913 const char *buf, size_t count)
914{
915 unsigned long t;
916
917 if (parse_strtoul(buf, 0xffff, &t))
918 return -EINVAL;
919
920 dbg_level = t;
921
922 return count;
923}
924
925static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
926 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
927
928/* version ------------------------------------------------------------- */
929static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
930 char *buf)
931{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200932 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
933 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200934}
935
936static DRIVER_ATTR(version, S_IRUGO,
937 tpacpi_driver_version_show, NULL);
938
939/* --------------------------------------------------------------------- */
940
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200941static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200942 &driver_attr_debug_level, &driver_attr_version,
943 &driver_attr_interface_version,
944};
945
946static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
947{
948 int i, res;
949
950 i = 0;
951 res = 0;
952 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
953 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
954 i++;
955 }
956
957 return res;
958}
959
960static void tpacpi_remove_driver_attributes(struct device_driver *drv)
961{
962 int i;
963
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200964 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200965 driver_remove_file(drv, tpacpi_driver_attributes[i]);
966}
967
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300968/****************************************************************************
969 ****************************************************************************
970 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300971 * Subdrivers
972 *
973 ****************************************************************************
974 ****************************************************************************/
975
976/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300977 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300978 */
979
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300980static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200982 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
983 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200985 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300986 (thinkpad_id.bios_version_str) ?
987 thinkpad_id.bios_version_str : "unknown",
988 (thinkpad_id.ec_version_str) ?
989 thinkpad_id.ec_version_str : "unknown");
990
991 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300992 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300993 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
994 "IBM" : ((thinkpad_id.vendor ==
995 PCI_VENDOR_ID_LENOVO) ?
996 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300997 thinkpad_id.model_str,
998 (thinkpad_id.nummodel_str) ?
999 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return 0;
1002}
1003
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001004static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 int len = 0;
1007
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001008 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1009 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 return len;
1012}
1013
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001014static struct ibm_struct thinkpad_acpi_driver_data = {
1015 .name = "driver",
1016 .read = thinkpad_acpi_driver_read,
1017};
1018
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001019/*************************************************************************
1020 * Hotkey subdriver
1021 */
1022
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001023enum { /* hot key scan codes (derived from ACPI DSDT) */
1024 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1025 TP_ACPI_HOTKEYSCAN_FNF2,
1026 TP_ACPI_HOTKEYSCAN_FNF3,
1027 TP_ACPI_HOTKEYSCAN_FNF4,
1028 TP_ACPI_HOTKEYSCAN_FNF5,
1029 TP_ACPI_HOTKEYSCAN_FNF6,
1030 TP_ACPI_HOTKEYSCAN_FNF7,
1031 TP_ACPI_HOTKEYSCAN_FNF8,
1032 TP_ACPI_HOTKEYSCAN_FNF9,
1033 TP_ACPI_HOTKEYSCAN_FNF10,
1034 TP_ACPI_HOTKEYSCAN_FNF11,
1035 TP_ACPI_HOTKEYSCAN_FNF12,
1036 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1037 TP_ACPI_HOTKEYSCAN_FNINSERT,
1038 TP_ACPI_HOTKEYSCAN_FNDELETE,
1039 TP_ACPI_HOTKEYSCAN_FNHOME,
1040 TP_ACPI_HOTKEYSCAN_FNEND,
1041 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1042 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1043 TP_ACPI_HOTKEYSCAN_FNSPACE,
1044 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1045 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1046 TP_ACPI_HOTKEYSCAN_MUTE,
1047 TP_ACPI_HOTKEYSCAN_THINKPAD,
1048};
1049
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001050enum { /* Keys available through NVRAM polling */
1051 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1052 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1053};
1054
1055enum { /* Positions of some of the keys in hotkey masks */
1056 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1057 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1058 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1059 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1060 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1061 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1062 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1063 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1064 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1065 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1066 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1067};
1068
1069enum { /* NVRAM to ACPI HKEY group map */
1070 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1071 TP_ACPI_HKEY_ZOOM_MASK |
1072 TP_ACPI_HKEY_DISPSWTCH_MASK |
1073 TP_ACPI_HKEY_HIBERNATE_MASK,
1074 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1075 TP_ACPI_HKEY_BRGHTDWN_MASK,
1076 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1077 TP_ACPI_HKEY_VOLDWN_MASK |
1078 TP_ACPI_HKEY_MUTE_MASK,
1079};
1080
1081#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1082struct tp_nvram_state {
1083 u16 thinkpad_toggle:1;
1084 u16 zoom_toggle:1;
1085 u16 display_toggle:1;
1086 u16 thinklight_toggle:1;
1087 u16 hibernate_toggle:1;
1088 u16 displayexp_toggle:1;
1089 u16 display_state:1;
1090 u16 brightness_toggle:1;
1091 u16 volume_toggle:1;
1092 u16 mute:1;
1093
1094 u8 brightness_level;
1095 u8 volume_level;
1096};
1097
1098static struct task_struct *tpacpi_hotkey_task;
1099static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1100static int hotkey_poll_freq = 10; /* Hz */
1101static struct mutex hotkey_thread_mutex;
1102static struct mutex hotkey_thread_data_mutex;
1103static unsigned int hotkey_config_change;
1104
1105#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1106
1107#define hotkey_source_mask 0U
1108
1109#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1110
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001111static struct mutex hotkey_mutex;
1112
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001113static enum { /* Reasons for waking up */
1114 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1115 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1116 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1117} hotkey_wakeup_reason;
1118
1119static int hotkey_autosleep_ack;
1120
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001121static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001122static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001123static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001124static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001125static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001126
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001127static unsigned int hotkey_report_mode;
1128
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001129static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001130
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001131static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001132
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001133#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1134#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001135 do { \
1136 mutex_lock(&hotkey_thread_data_mutex); \
1137 hotkey_config_change++; \
1138 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001139#define HOTKEY_CONFIG_CRITICAL_END \
1140 mutex_unlock(&hotkey_thread_data_mutex);
1141#else
1142#define HOTKEY_CONFIG_CRITICAL_START
1143#define HOTKEY_CONFIG_CRITICAL_END
1144#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1145
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001146/* HKEY.MHKG() return bits */
1147#define TP_HOTKEY_TABLET_MASK (1 << 3)
1148
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001149static int hotkey_get_wlsw(int *status)
1150{
1151 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1152 return -EIO;
1153 return 0;
1154}
1155
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001156static int hotkey_get_tablet_mode(int *status)
1157{
1158 int s;
1159
1160 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1161 return -EIO;
1162
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001163 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1164 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001165}
1166
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001167/*
1168 * Call with hotkey_mutex held
1169 */
1170static int hotkey_mask_get(void)
1171{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001172 u32 m = 0;
1173
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001174 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001175 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001176 return -EIO;
1177 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001178 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001179
1180 return 0;
1181}
1182
1183/*
1184 * Call with hotkey_mutex held
1185 */
1186static int hotkey_mask_set(u32 mask)
1187{
1188 int i;
1189 int rc = 0;
1190
1191 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001192 if (!tp_warned.hotkey_mask_ff &&
1193 (mask == 0xffff || mask == 0xffffff ||
1194 mask == 0xffffffff)) {
1195 tp_warned.hotkey_mask_ff = 1;
1196 printk(TPACPI_NOTICE
1197 "setting the hotkey mask to 0x%08x is likely "
1198 "not the best way to go about it\n", mask);
1199 printk(TPACPI_NOTICE
1200 "please consider using the driver defaults, "
1201 "and refer to up-to-date thinkpad-acpi "
1202 "documentation\n");
1203 }
1204
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001205 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001206 for (i = 0; i < 32; i++) {
1207 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001208 /* enable in firmware mask only keys not in NVRAM
1209 * mode, but enable the key in the cached hotkey_mask
1210 * regardless of mode, or the key will end up
1211 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001212 if (!acpi_evalf(hkey_handle,
1213 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001214 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001215 rc = -EIO;
1216 break;
1217 } else {
1218 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1219 }
1220 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001221 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001222
1223 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001224 if (!hotkey_mask_get() && !rc &&
1225 (hotkey_mask & ~hotkey_source_mask) !=
1226 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001227 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001228 "requested hot key mask 0x%08x, but "
1229 "firmware forced it to 0x%08x\n",
1230 mask, hotkey_mask);
1231 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001232 } else {
1233#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1234 HOTKEY_CONFIG_CRITICAL_START
1235 hotkey_mask = mask & hotkey_source_mask;
1236 HOTKEY_CONFIG_CRITICAL_END
1237 hotkey_mask_get();
1238 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001239 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001240 "requested hot key mask 0x%08x, "
1241 "forced to 0x%08x (NVRAM poll mask is "
1242 "0x%08x): no firmware mask support\n",
1243 mask, hotkey_mask, hotkey_source_mask);
1244 }
1245#else
1246 hotkey_mask_get();
1247 rc = -ENXIO;
1248#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001249 }
1250
1251 return rc;
1252}
1253
1254static int hotkey_status_get(int *status)
1255{
1256 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1257 return -EIO;
1258
1259 return 0;
1260}
1261
1262static int hotkey_status_set(int status)
1263{
1264 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1265 return -EIO;
1266
1267 return 0;
1268}
1269
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001270static void tpacpi_input_send_radiosw(void)
1271{
1272 int wlsw;
1273
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001274 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001275 mutex_lock(&tpacpi_inputdev_send_mutex);
1276
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001277 input_report_switch(tpacpi_inputdev,
1278 SW_RADIO, !!wlsw);
1279 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001280
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001281 mutex_unlock(&tpacpi_inputdev_send_mutex);
1282 }
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001283}
1284
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001285static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001286{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001287 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001288
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001289 if (tp_features.hotkey_tablet &&
1290 !hotkey_get_tablet_mode(&state)) {
1291 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001292
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001293 input_report_switch(tpacpi_inputdev,
1294 SW_TABLET_MODE, !!state);
1295 input_sync(tpacpi_inputdev);
1296
1297 mutex_unlock(&tpacpi_inputdev_send_mutex);
1298 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001299}
1300
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001301static void tpacpi_input_send_key(unsigned int scancode)
1302{
1303 unsigned int keycode;
1304
1305 keycode = hotkey_keycode_map[scancode];
1306
1307 if (keycode != KEY_RESERVED) {
1308 mutex_lock(&tpacpi_inputdev_send_mutex);
1309
1310 input_report_key(tpacpi_inputdev, keycode, 1);
1311 if (keycode == KEY_UNKNOWN)
1312 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1313 scancode);
1314 input_sync(tpacpi_inputdev);
1315
1316 input_report_key(tpacpi_inputdev, keycode, 0);
1317 if (keycode == KEY_UNKNOWN)
1318 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1319 scancode);
1320 input_sync(tpacpi_inputdev);
1321
1322 mutex_unlock(&tpacpi_inputdev_send_mutex);
1323 }
1324}
1325
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001326#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1327static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1328
1329static void tpacpi_hotkey_send_key(unsigned int scancode)
1330{
1331 tpacpi_input_send_key(scancode);
1332 if (hotkey_report_mode < 2) {
1333 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1334 0x80, 0x1001 + scancode);
1335 }
1336}
1337
1338static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1339{
1340 u8 d;
1341
1342 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1343 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1344 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1345 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1346 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1347 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1348 }
1349 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1350 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1351 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1352 }
1353 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1354 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1355 n->displayexp_toggle =
1356 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1357 }
1358 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1359 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1360 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1361 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1362 n->brightness_toggle =
1363 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1364 }
1365 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1366 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1367 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1368 >> TP_NVRAM_POS_LEVEL_VOLUME;
1369 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1370 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1371 }
1372}
1373
1374#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001375 do { \
1376 if ((mask & (1 << __scancode)) && \
1377 oldn->__member != newn->__member) \
1378 tpacpi_hotkey_send_key(__scancode); \
1379 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001380
1381#define TPACPI_MAY_SEND_KEY(__scancode) \
1382 do { if (mask & (1 << __scancode)) \
1383 tpacpi_hotkey_send_key(__scancode); } while (0)
1384
1385static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001386 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001387 u32 mask)
1388{
1389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1393
1394 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1395
1396 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1397
1398 /* handle volume */
1399 if (oldn->volume_toggle != newn->volume_toggle) {
1400 if (oldn->mute != newn->mute) {
1401 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1402 }
1403 if (oldn->volume_level > newn->volume_level) {
1404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1405 } else if (oldn->volume_level < newn->volume_level) {
1406 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1407 } else if (oldn->mute == newn->mute) {
1408 /* repeated key presses that didn't change state */
1409 if (newn->mute) {
1410 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1411 } else if (newn->volume_level != 0) {
1412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1413 } else {
1414 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1415 }
1416 }
1417 }
1418
1419 /* handle brightness */
1420 if (oldn->brightness_toggle != newn->brightness_toggle) {
1421 if (oldn->brightness_level < newn->brightness_level) {
1422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1423 } else if (oldn->brightness_level > newn->brightness_level) {
1424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1425 } else {
1426 /* repeated key presses that didn't change state */
1427 if (newn->brightness_level != 0) {
1428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1429 } else {
1430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1431 }
1432 }
1433 }
1434}
1435
1436#undef TPACPI_COMPARE_KEY
1437#undef TPACPI_MAY_SEND_KEY
1438
1439static int hotkey_kthread(void *data)
1440{
1441 struct tp_nvram_state s[2];
1442 u32 mask;
1443 unsigned int si, so;
1444 unsigned long t;
1445 unsigned int change_detector, must_reset;
1446
1447 mutex_lock(&hotkey_thread_mutex);
1448
1449 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1450 goto exit;
1451
1452 set_freezable();
1453
1454 so = 0;
1455 si = 1;
1456 t = 0;
1457
1458 /* Initial state for compares */
1459 mutex_lock(&hotkey_thread_data_mutex);
1460 change_detector = hotkey_config_change;
1461 mask = hotkey_source_mask & hotkey_mask;
1462 mutex_unlock(&hotkey_thread_data_mutex);
1463 hotkey_read_nvram(&s[so], mask);
1464
1465 while (!kthread_should_stop() && hotkey_poll_freq) {
1466 if (t == 0)
1467 t = 1000/hotkey_poll_freq;
1468 t = msleep_interruptible(t);
1469 if (unlikely(kthread_should_stop()))
1470 break;
1471 must_reset = try_to_freeze();
1472 if (t > 0 && !must_reset)
1473 continue;
1474
1475 mutex_lock(&hotkey_thread_data_mutex);
1476 if (must_reset || hotkey_config_change != change_detector) {
1477 /* forget old state on thaw or config change */
1478 si = so;
1479 t = 0;
1480 change_detector = hotkey_config_change;
1481 }
1482 mask = hotkey_source_mask & hotkey_mask;
1483 mutex_unlock(&hotkey_thread_data_mutex);
1484
1485 if (likely(mask)) {
1486 hotkey_read_nvram(&s[si], mask);
1487 if (likely(si != so)) {
1488 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001489 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001490 }
1491 }
1492
1493 so = si;
1494 si ^= 1;
1495 }
1496
1497exit:
1498 mutex_unlock(&hotkey_thread_mutex);
1499 return 0;
1500}
1501
1502static void hotkey_poll_stop_sync(void)
1503{
1504 if (tpacpi_hotkey_task) {
1505 if (frozen(tpacpi_hotkey_task) ||
1506 freezing(tpacpi_hotkey_task))
1507 thaw_process(tpacpi_hotkey_task);
1508
1509 kthread_stop(tpacpi_hotkey_task);
1510 tpacpi_hotkey_task = NULL;
1511 mutex_lock(&hotkey_thread_mutex);
1512 /* at this point, the thread did exit */
1513 mutex_unlock(&hotkey_thread_mutex);
1514 }
1515}
1516
1517/* call with hotkey_mutex held */
1518static void hotkey_poll_setup(int may_warn)
1519{
1520 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1521 hotkey_poll_freq > 0 &&
1522 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1523 if (!tpacpi_hotkey_task) {
1524 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001525 NULL,
1526 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001527 if (IS_ERR(tpacpi_hotkey_task)) {
1528 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001529 printk(TPACPI_ERR
1530 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001531 "for hotkey polling\n");
1532 }
1533 }
1534 } else {
1535 hotkey_poll_stop_sync();
1536 if (may_warn &&
1537 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001538 printk(TPACPI_NOTICE
1539 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001540 "which is currently disabled\n",
1541 hotkey_source_mask);
1542 }
1543 }
1544}
1545
1546static void hotkey_poll_setup_safe(int may_warn)
1547{
1548 mutex_lock(&hotkey_mutex);
1549 hotkey_poll_setup(may_warn);
1550 mutex_unlock(&hotkey_mutex);
1551}
1552
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001553#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1554
1555static void hotkey_poll_setup_safe(int __unused)
1556{
1557}
1558
1559#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1560
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001561static int hotkey_inputdev_open(struct input_dev *dev)
1562{
1563 switch (tpacpi_lifecycle) {
1564 case TPACPI_LIFE_INIT:
1565 /*
1566 * hotkey_init will call hotkey_poll_setup_safe
1567 * at the appropriate moment
1568 */
1569 return 0;
1570 case TPACPI_LIFE_EXITING:
1571 return -EBUSY;
1572 case TPACPI_LIFE_RUNNING:
1573 hotkey_poll_setup_safe(0);
1574 return 0;
1575 }
1576
1577 /* Should only happen if tpacpi_lifecycle is corrupt */
1578 BUG();
1579 return -EBUSY;
1580}
1581
1582static void hotkey_inputdev_close(struct input_dev *dev)
1583{
1584 /* disable hotkey polling when possible */
1585 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1586 hotkey_poll_setup_safe(0);
1587}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001588
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001589/* sysfs hotkey enable ------------------------------------------------- */
1590static ssize_t hotkey_enable_show(struct device *dev,
1591 struct device_attribute *attr,
1592 char *buf)
1593{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001594 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001595
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001596 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001597 if (res)
1598 return res;
1599
1600 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1601}
1602
1603static ssize_t hotkey_enable_store(struct device *dev,
1604 struct device_attribute *attr,
1605 const char *buf, size_t count)
1606{
1607 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001608 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001609
1610 if (parse_strtoul(buf, 1, &t))
1611 return -EINVAL;
1612
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001613 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001614
1615 return (res) ? res : count;
1616}
1617
1618static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001619 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001620 hotkey_enable_show, hotkey_enable_store);
1621
1622/* sysfs hotkey mask --------------------------------------------------- */
1623static ssize_t hotkey_mask_show(struct device *dev,
1624 struct device_attribute *attr,
1625 char *buf)
1626{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001627 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001628
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001629 if (mutex_lock_interruptible(&hotkey_mutex))
1630 return -ERESTARTSYS;
1631 res = hotkey_mask_get();
1632 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001633
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001634 return (res)?
1635 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001636}
1637
1638static ssize_t hotkey_mask_store(struct device *dev,
1639 struct device_attribute *attr,
1640 const char *buf, size_t count)
1641{
1642 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001643 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001644
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001645 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001646 return -EINVAL;
1647
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001648 if (mutex_lock_interruptible(&hotkey_mutex))
1649 return -ERESTARTSYS;
1650
1651 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001652
1653#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1654 hotkey_poll_setup(1);
1655#endif
1656
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001657 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001658
1659 return (res) ? res : count;
1660}
1661
1662static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001663 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001664 hotkey_mask_show, hotkey_mask_store);
1665
1666/* sysfs hotkey bios_enabled ------------------------------------------- */
1667static ssize_t hotkey_bios_enabled_show(struct device *dev,
1668 struct device_attribute *attr,
1669 char *buf)
1670{
1671 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1672}
1673
1674static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001675 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001676
1677/* sysfs hotkey bios_mask ---------------------------------------------- */
1678static ssize_t hotkey_bios_mask_show(struct device *dev,
1679 struct device_attribute *attr,
1680 char *buf)
1681{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001682 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001683}
1684
1685static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001686 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001687
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001688/* sysfs hotkey all_mask ----------------------------------------------- */
1689static ssize_t hotkey_all_mask_show(struct device *dev,
1690 struct device_attribute *attr,
1691 char *buf)
1692{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001693 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1694 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001695}
1696
1697static struct device_attribute dev_attr_hotkey_all_mask =
1698 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1699
1700/* sysfs hotkey recommended_mask --------------------------------------- */
1701static ssize_t hotkey_recommended_mask_show(struct device *dev,
1702 struct device_attribute *attr,
1703 char *buf)
1704{
1705 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001706 (hotkey_all_mask | hotkey_source_mask)
1707 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001708}
1709
1710static struct device_attribute dev_attr_hotkey_recommended_mask =
1711 __ATTR(hotkey_recommended_mask, S_IRUGO,
1712 hotkey_recommended_mask_show, NULL);
1713
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001714#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1715
1716/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1717static ssize_t hotkey_source_mask_show(struct device *dev,
1718 struct device_attribute *attr,
1719 char *buf)
1720{
1721 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1722}
1723
1724static ssize_t hotkey_source_mask_store(struct device *dev,
1725 struct device_attribute *attr,
1726 const char *buf, size_t count)
1727{
1728 unsigned long t;
1729
1730 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1731 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1732 return -EINVAL;
1733
1734 if (mutex_lock_interruptible(&hotkey_mutex))
1735 return -ERESTARTSYS;
1736
1737 HOTKEY_CONFIG_CRITICAL_START
1738 hotkey_source_mask = t;
1739 HOTKEY_CONFIG_CRITICAL_END
1740
1741 hotkey_poll_setup(1);
1742
1743 mutex_unlock(&hotkey_mutex);
1744
1745 return count;
1746}
1747
1748static struct device_attribute dev_attr_hotkey_source_mask =
1749 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1750 hotkey_source_mask_show, hotkey_source_mask_store);
1751
1752/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1753static ssize_t hotkey_poll_freq_show(struct device *dev,
1754 struct device_attribute *attr,
1755 char *buf)
1756{
1757 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1758}
1759
1760static ssize_t hotkey_poll_freq_store(struct device *dev,
1761 struct device_attribute *attr,
1762 const char *buf, size_t count)
1763{
1764 unsigned long t;
1765
1766 if (parse_strtoul(buf, 25, &t))
1767 return -EINVAL;
1768
1769 if (mutex_lock_interruptible(&hotkey_mutex))
1770 return -ERESTARTSYS;
1771
1772 hotkey_poll_freq = t;
1773
1774 hotkey_poll_setup(1);
1775 mutex_unlock(&hotkey_mutex);
1776
1777 return count;
1778}
1779
1780static struct device_attribute dev_attr_hotkey_poll_freq =
1781 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1782 hotkey_poll_freq_show, hotkey_poll_freq_store);
1783
1784#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1785
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001786/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001787static ssize_t hotkey_radio_sw_show(struct device *dev,
1788 struct device_attribute *attr,
1789 char *buf)
1790{
1791 int res, s;
1792 res = hotkey_get_wlsw(&s);
1793 if (res < 0)
1794 return res;
1795
1796 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1797}
1798
1799static struct device_attribute dev_attr_hotkey_radio_sw =
1800 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1801
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001802static void hotkey_radio_sw_notify_change(void)
1803{
1804 if (tp_features.hotkey_wlsw)
1805 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1806 "hotkey_radio_sw");
1807}
1808
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001809/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1810static ssize_t hotkey_tablet_mode_show(struct device *dev,
1811 struct device_attribute *attr,
1812 char *buf)
1813{
1814 int res, s;
1815 res = hotkey_get_tablet_mode(&s);
1816 if (res < 0)
1817 return res;
1818
1819 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1820}
1821
1822static struct device_attribute dev_attr_hotkey_tablet_mode =
1823 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1824
1825static void hotkey_tablet_mode_notify_change(void)
1826{
1827 if (tp_features.hotkey_tablet)
1828 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1829 "hotkey_tablet_mode");
1830}
1831
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001832/* sysfs hotkey report_mode -------------------------------------------- */
1833static ssize_t hotkey_report_mode_show(struct device *dev,
1834 struct device_attribute *attr,
1835 char *buf)
1836{
1837 return snprintf(buf, PAGE_SIZE, "%d\n",
1838 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1839}
1840
1841static struct device_attribute dev_attr_hotkey_report_mode =
1842 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1843
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001844/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001845static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1846 struct device_attribute *attr,
1847 char *buf)
1848{
1849 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1850}
1851
1852static struct device_attribute dev_attr_hotkey_wakeup_reason =
1853 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1854
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001855static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001856{
1857 if (tp_features.hotkey_mask)
1858 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1859 "wakeup_reason");
1860}
1861
1862/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001863static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1864 struct device_attribute *attr,
1865 char *buf)
1866{
1867 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1868}
1869
1870static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1871 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1872 hotkey_wakeup_hotunplug_complete_show, NULL);
1873
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001874static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001875{
1876 if (tp_features.hotkey_mask)
1877 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1878 "wakeup_hotunplug_complete");
1879}
1880
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001881/* --------------------------------------------------------------------- */
1882
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001883static struct attribute *hotkey_attributes[] __initdata = {
1884 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001885 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001886 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001887#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1888 &dev_attr_hotkey_mask.attr,
1889 &dev_attr_hotkey_all_mask.attr,
1890 &dev_attr_hotkey_recommended_mask.attr,
1891 &dev_attr_hotkey_source_mask.attr,
1892 &dev_attr_hotkey_poll_freq.attr,
1893#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001894};
1895
1896static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001897 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001898#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1899 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001900 &dev_attr_hotkey_all_mask.attr,
1901 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001902#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001903 &dev_attr_hotkey_wakeup_reason.attr,
1904 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001905};
1906
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001907static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001908{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001909 /* Requirements for changing the default keymaps:
1910 *
1911 * 1. Many of the keys are mapped to KEY_RESERVED for very
1912 * good reasons. Do not change them unless you have deep
1913 * knowledge on the IBM and Lenovo ThinkPad firmware for
1914 * the various ThinkPad models. The driver behaves
1915 * differently for KEY_RESERVED: such keys have their
1916 * hot key mask *unset* in mask_recommended, and also
1917 * in the initial hot key mask programmed into the
1918 * firmware at driver load time, which means the firm-
1919 * ware may react very differently if you change them to
1920 * something else;
1921 *
1922 * 2. You must be subscribed to the linux-thinkpad and
1923 * ibm-acpi-devel mailing lists, and you should read the
1924 * list archives since 2007 if you want to change the
1925 * keymaps. This requirement exists so that you will
1926 * know the past history of problems with the thinkpad-
1927 * acpi driver keymaps, and also that you will be
1928 * listening to any bug reports;
1929 *
1930 * 3. Do not send thinkpad-acpi specific patches directly to
1931 * for merging, *ever*. Send them to the linux-acpi
1932 * mailinglist for comments. Merging is to be done only
1933 * through acpi-test and the ACPI maintainer.
1934 *
1935 * If the above is too much to ask, don't change the keymap.
1936 * Ask the thinkpad-acpi maintainer to do it, instead.
1937 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001938 static u16 ibm_keycode_map[] __initdata = {
1939 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1940 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1941 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1942 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001943
1944 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001945 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1946 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1947 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001948
1949 /* brightness: firmware always reacts to them, unless
1950 * X.org did some tricks in the radeon BIOS scratch
1951 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001952 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001953 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001954
1955 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001956 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001957
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001958 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1959 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001960
1961 /* Volume: firmware always react to it and reprograms
1962 * the built-in *extra* mixer. Never map it to control
1963 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001964 KEY_RESERVED, /* 0x14: VOLUME UP */
1965 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1966 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001967
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001968 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001969
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001970 /* (assignments unknown, please report if found) */
1971 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1972 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1973 };
1974 static u16 lenovo_keycode_map[] __initdata = {
1975 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1976 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1977 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1978 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001979
1980 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001981 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1982 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1983 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001984
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001985 /* These either have to go through ACPI video, or
1986 * act like in the IBM ThinkPads, so don't ever
1987 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001988 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001989 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001990
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001991 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001992
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001993 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1994 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001995
1996 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1997 * react to it and reprograms the built-in *extra* mixer.
1998 * Never map it to control another mixer by default.
1999 *
2000 * T60?, T61, R60?, R61: firmware and EC tries to send
2001 * these over the regular keyboard, so these are no-ops,
2002 * but there are still weird bugs re. MUTE, so do not
2003 * change unless you get test reports from all Lenovo
2004 * models. May cause the BIOS to interfere with the
2005 * HDA mixer.
2006 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002007 KEY_RESERVED, /* 0x14: VOLUME UP */
2008 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2009 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002010
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002011 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002012
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002013 /* (assignments unknown, please report if found) */
2014 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2015 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2016 };
2017
2018#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2019#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2020#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2021
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002022 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002023 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002024 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002025
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002026 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2027
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002028 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002029 BUG_ON(tpacpi_inputdev->open != NULL ||
2030 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002031
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002032 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002033 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002034
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002035#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2036 mutex_init(&hotkey_thread_mutex);
2037 mutex_init(&hotkey_thread_data_mutex);
2038#endif
2039
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002040 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002041 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002042
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002043 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002044 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002045
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002046 if (tp_features.hotkey) {
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002047 hotkey_dev_attributes = create_attr_set(13, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002048 if (!hotkey_dev_attributes)
2049 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002050 res = add_many_to_attr_set(hotkey_dev_attributes,
2051 hotkey_attributes,
2052 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002053 if (res)
2054 return res;
2055
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002056 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002057 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2058 for HKEY interface version 0x100 */
2059 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2060 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002061 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002062 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002063 printk(TPACPI_ERR "please report this to %s\n",
2064 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002065 } else {
2066 /*
2067 * MHKV 0x100 in A31, R40, R40e,
2068 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002069 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002070 tp_features.hotkey_mask = 1;
2071 }
2072 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002073
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002074 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002075 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002076
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002077 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002078 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002079 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002080 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002081 "missing MHKA handler, "
2082 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002083 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002084 /* FN+F12, FN+F4, FN+F3 */
2085 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002086 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002087 }
2088
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002089 /* hotkey_source_mask *must* be zero for
2090 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002091 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002092 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002093 res = hotkey_mask_get();
2094 hotkey_orig_mask = hotkey_mask;
2095 if (!res) {
2096 res = add_many_to_attr_set(
2097 hotkey_dev_attributes,
2098 hotkey_mask_attributes,
2099 ARRAY_SIZE(hotkey_mask_attributes));
2100 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002101 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002102
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002103#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2104 if (tp_features.hotkey_mask) {
2105 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2106 & ~hotkey_all_mask;
2107 } else {
2108 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2109 }
2110
2111 vdbg_printk(TPACPI_DBG_INIT,
2112 "hotkey source mask 0x%08x, polling freq %d\n",
2113 hotkey_source_mask, hotkey_poll_freq);
2114#endif
2115
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002116 /* Not all thinkpads have a hardware radio switch */
2117 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2118 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002119 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002120 "radio switch found; radios are %s\n",
2121 enabled(status, 0));
2122 res = add_to_attr_set(hotkey_dev_attributes,
2123 &dev_attr_hotkey_radio_sw.attr);
2124 }
2125
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002126 /* For X41t, X60t, X61t Tablets... */
2127 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2128 tp_features.hotkey_tablet = 1;
2129 printk(TPACPI_INFO
2130 "possible tablet mode switch found; "
2131 "ThinkPad in %s mode\n",
2132 (status & TP_HOTKEY_TABLET_MASK)?
2133 "tablet" : "laptop");
2134 res = add_to_attr_set(hotkey_dev_attributes,
2135 &dev_attr_hotkey_tablet_mode.attr);
2136 }
2137
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002138 if (!res)
2139 res = register_attr_set_with_sysfs(
2140 hotkey_dev_attributes,
2141 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002142 if (res)
2143 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002144
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002145 /* Set up key map */
2146
2147 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2148 GFP_KERNEL);
2149 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002150 printk(TPACPI_ERR
2151 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002152 return -ENOMEM;
2153 }
2154
2155 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2156 dbg_printk(TPACPI_DBG_INIT,
2157 "using Lenovo default hot key map\n");
2158 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2159 TPACPI_HOTKEY_MAP_SIZE);
2160 } else {
2161 dbg_printk(TPACPI_DBG_INIT,
2162 "using IBM default hot key map\n");
2163 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2164 TPACPI_HOTKEY_MAP_SIZE);
2165 }
2166
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002167 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2168 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2169 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002170 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2171 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2172 tpacpi_inputdev->keycode = hotkey_keycode_map;
2173 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002174 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2175 set_bit(hotkey_keycode_map[i],
2176 tpacpi_inputdev->keybit);
2177 } else {
2178 if (i < sizeof(hotkey_reserved_mask)*8)
2179 hotkey_reserved_mask |= 1 << i;
2180 }
2181 }
2182
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002183 if (tp_features.hotkey_wlsw) {
2184 set_bit(EV_SW, tpacpi_inputdev->evbit);
2185 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2186 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002187 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002188 set_bit(EV_SW, tpacpi_inputdev->evbit);
2189 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2190 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002191
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002192 /* Do not issue duplicate brightness change events to
2193 * userspace */
2194 if (!tp_features.bright_acpimode)
2195 /* update bright_acpimode... */
2196 tpacpi_check_std_acpi_brightness_support();
2197
2198 if (tp_features.bright_acpimode) {
2199 printk(TPACPI_INFO
2200 "This ThinkPad has standard ACPI backlight "
2201 "brightness control, supported by the ACPI "
2202 "video driver\n");
2203 printk(TPACPI_NOTICE
2204 "Disabling thinkpad-acpi brightness events "
2205 "by default...\n");
2206
2207 /* The hotkey_reserved_mask change below is not
2208 * necessary while the keys are at KEY_RESERVED in the
2209 * default map, but better safe than sorry, leave it
2210 * here as a marker of what we have to do, especially
2211 * when we finally become able to set this at runtime
2212 * on response to X.org requests */
2213 hotkey_reserved_mask |=
2214 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2215 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2216 }
2217
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002218 dbg_printk(TPACPI_DBG_INIT,
2219 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002220 res = hotkey_status_set(1);
2221 if (res)
2222 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002223 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2224 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002225 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002226 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002227 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002228
2229 dbg_printk(TPACPI_DBG_INIT,
2230 "legacy hot key reporting over procfs %s\n",
2231 (hotkey_report_mode < 2) ?
2232 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002233
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002234 tpacpi_inputdev->open = &hotkey_inputdev_open;
2235 tpacpi_inputdev->close = &hotkey_inputdev_close;
2236
2237 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002238 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002239 tpacpi_input_send_tabletsw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002240 }
2241
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002242 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002243}
2244
2245static void hotkey_exit(void)
2246{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002247#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2248 hotkey_poll_stop_sync();
2249#endif
2250
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002251 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002252 dbg_printk(TPACPI_DBG_EXIT,
2253 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002254 /* no short-circuit boolean operator below! */
2255 if ((hotkey_mask_set(hotkey_orig_mask) |
2256 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002257 printk(TPACPI_ERR
2258 "failed to restore hot key mask "
2259 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002260 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002261
2262 if (hotkey_dev_attributes) {
2263 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2264 hotkey_dev_attributes = NULL;
2265 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002266}
2267
2268static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2269{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002270 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002271 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002272 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002273 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002274 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002275
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002276 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002277 printk(TPACPI_ERR
2278 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002279 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002280 acpi_bus_generate_netlink_event(
2281 ibm->acpi->device->pnp.device_class,
2282 ibm->acpi->device->dev.bus_id,
2283 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002284 return;
2285 }
2286
2287 while (1) {
2288 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002289 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002290 return;
2291 }
2292
2293 if (hkey == 0) {
2294 /* queue empty */
2295 return;
2296 }
2297
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002298 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002299 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002300 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002301
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002302 switch (hkey >> 12) {
2303 case 1:
2304 /* 0x1000-0x1FFF: key presses */
2305 scancode = hkey & 0xfff;
2306 if (scancode > 0 && scancode < 0x21) {
2307 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002308 if (!(hotkey_source_mask & (1 << scancode))) {
2309 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002310 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002311 } else {
2312 ignore_acpi_ev = 1;
2313 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002314 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002315 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002316 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002317 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002318 case 2:
2319 /* Wakeup reason */
2320 switch (hkey) {
2321 case 0x2304: /* suspend, undock */
2322 case 0x2404: /* hibernation, undock */
2323 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2324 ignore_acpi_ev = 1;
2325 break;
2326 case 0x2305: /* suspend, bay eject */
2327 case 0x2405: /* hibernation, bay eject */
2328 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2329 ignore_acpi_ev = 1;
2330 break;
2331 default:
2332 unk_ev = 1;
2333 }
2334 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2335 printk(TPACPI_INFO
2336 "woke up due to a hot-unplug "
2337 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002338 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002339 }
2340 break;
2341 case 3:
2342 /* bay-related wakeups */
2343 if (hkey == 0x3003) {
2344 hotkey_autosleep_ack = 1;
2345 printk(TPACPI_INFO
2346 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002347 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002348 } else {
2349 unk_ev = 1;
2350 }
2351 break;
2352 case 4:
2353 /* dock-related wakeups */
2354 if (hkey == 0x4003) {
2355 hotkey_autosleep_ack = 1;
2356 printk(TPACPI_INFO
2357 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002358 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002359 } else {
2360 unk_ev = 1;
2361 }
2362 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002363 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002364 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002365 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002366 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002367 case 0x500b: /* X61t: tablet pen inserted into bay */
2368 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002369 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002370 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2371 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2372 tpacpi_input_send_tabletsw();
2373 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002374 send_acpi_ev = 0;
2375 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002376 case 0x5001:
2377 case 0x5002:
2378 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002379 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002380 break;
2381 default:
2382 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002383 }
2384 break;
2385 case 7:
2386 /* 0x7000-0x7FFF: misc */
2387 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2388 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002389 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002390 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002391 break;
2392 }
2393 /* fallthrough to default */
2394 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002395 unk_ev = 1;
2396 }
2397 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002398 printk(TPACPI_NOTICE
2399 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002400 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002401
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002402 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002403 if (!ignore_acpi_ev &&
2404 (send_acpi_ev || hotkey_report_mode < 2)) {
2405 acpi_bus_generate_proc_event(ibm->acpi->device,
2406 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002407 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002408
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002409 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002410 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002411 acpi_bus_generate_netlink_event(
2412 ibm->acpi->device->pnp.device_class,
2413 ibm->acpi->device->dev.bus_id,
2414 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002415 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002416 }
2417}
2418
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002419static void hotkey_suspend(pm_message_t state)
2420{
2421 /* Do these on suspend, we get the events on early resume! */
2422 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2423 hotkey_autosleep_ack = 0;
2424}
2425
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002426static void hotkey_resume(void)
2427{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002428 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002429 printk(TPACPI_ERR
2430 "error while trying to read hot key mask "
2431 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002432 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002433 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002434 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002435 hotkey_wakeup_reason_notify_change();
2436 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002437 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002438}
2439
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002440/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002441static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002443 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 int len = 0;
2445
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002446 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002447 len += sprintf(p + len, "status:\t\tnot supported\n");
2448 return len;
2449 }
2450
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002451 if (mutex_lock_interruptible(&hotkey_mutex))
2452 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002453 res = hotkey_status_get(&status);
2454 if (!res)
2455 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002456 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002457 if (res)
2458 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002461 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002462 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 len += sprintf(p + len,
2464 "commands:\tenable, disable, reset, <mask>\n");
2465 } else {
2466 len += sprintf(p + len, "mask:\t\tnot supported\n");
2467 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2468 }
2469
2470 return len;
2471}
2472
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002473static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002475 int res, status;
2476 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002479 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return -ENODEV;
2481
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002482 if (mutex_lock_interruptible(&hotkey_mutex))
2483 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002484
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002485 status = -1;
2486 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002487
2488 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 while ((cmd = next_cmd(&buf))) {
2490 if (strlencmp(cmd, "enable") == 0) {
2491 status = 1;
2492 } else if (strlencmp(cmd, "disable") == 0) {
2493 status = 0;
2494 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002495 status = hotkey_orig_status;
2496 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2498 /* mask set */
2499 } else if (sscanf(cmd, "%x", &mask) == 1) {
2500 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002501 } else {
2502 res = -EINVAL;
2503 goto errexit;
2504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002506 if (status != -1)
2507 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002509 if (!res && mask != hotkey_mask)
2510 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002512errexit:
2513 mutex_unlock(&hotkey_mutex);
2514 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002515}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002517static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002518 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002519 {"", 0},
2520};
2521
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002522static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002523 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002524 .notify = hotkey_notify,
2525 .handle = &hkey_handle,
2526 .type = ACPI_DEVICE_NOTIFY,
2527};
2528
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002529static struct ibm_struct hotkey_driver_data = {
2530 .name = "hotkey",
2531 .read = hotkey_read,
2532 .write = hotkey_write,
2533 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002534 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002535 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002536 .acpi = &ibm_hotkey_acpidriver,
2537};
2538
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002539/*************************************************************************
2540 * Bluetooth subdriver
2541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002543enum {
2544 /* ACPI GBDC/SBDC bits */
2545 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2546 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2547 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2548};
2549
2550static int bluetooth_get_radiosw(void);
2551static int bluetooth_set_radiosw(int radio_on);
2552
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002553/* sysfs bluetooth enable ---------------------------------------------- */
2554static ssize_t bluetooth_enable_show(struct device *dev,
2555 struct device_attribute *attr,
2556 char *buf)
2557{
2558 int status;
2559
2560 status = bluetooth_get_radiosw();
2561 if (status < 0)
2562 return status;
2563
2564 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2565}
2566
2567static ssize_t bluetooth_enable_store(struct device *dev,
2568 struct device_attribute *attr,
2569 const char *buf, size_t count)
2570{
2571 unsigned long t;
2572 int res;
2573
2574 if (parse_strtoul(buf, 1, &t))
2575 return -EINVAL;
2576
2577 res = bluetooth_set_radiosw(t);
2578
2579 return (res) ? res : count;
2580}
2581
2582static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002583 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002584 bluetooth_enable_show, bluetooth_enable_store);
2585
2586/* --------------------------------------------------------------------- */
2587
2588static struct attribute *bluetooth_attributes[] = {
2589 &dev_attr_bluetooth_enable.attr,
2590 NULL
2591};
2592
2593static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002594 .attrs = bluetooth_attributes,
2595};
2596
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002597static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002599 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002600 int status = 0;
2601
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002602 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2603
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002604 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002605
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002606 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2607 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002608 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002609 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002611 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2612 str_supported(tp_features.bluetooth),
2613 status);
2614
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002615 if (tp_features.bluetooth) {
2616 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2617 /* no bluetooth hardware present in system */
2618 tp_features.bluetooth = 0;
2619 dbg_printk(TPACPI_DBG_INIT,
2620 "bluetooth hardware not installed\n");
2621 } else {
2622 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2623 &bluetooth_attr_group);
2624 if (res)
2625 return res;
2626 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002627 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002628
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002629 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002632static void bluetooth_exit(void)
2633{
2634 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2635 &bluetooth_attr_group);
2636}
2637
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002638static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 int status;
2641
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002642 if (!tp_features.bluetooth)
2643 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002645 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2646 return -EIO;
2647
2648 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2649}
2650
2651static int bluetooth_set_radiosw(int radio_on)
2652{
2653 int status;
2654
2655 if (!tp_features.bluetooth)
2656 return -ENODEV;
2657
2658 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2659 return -EIO;
2660 if (radio_on)
2661 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2662 else
2663 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2664 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2665 return -EIO;
2666
2667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002670/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002671static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002674 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002676 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002679 len += sprintf(p + len, "status:\t\t%s\n",
2680 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 len += sprintf(p + len, "commands:\tenable, disable\n");
2682 }
2683
2684 return len;
2685}
2686
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002687static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002691 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002692 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694 while ((cmd = next_cmd(&buf))) {
2695 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002696 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002698 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 } else
2700 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 return 0;
2704}
2705
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002706static struct ibm_struct bluetooth_driver_data = {
2707 .name = "bluetooth",
2708 .read = bluetooth_read,
2709 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002710 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002711};
2712
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002713/*************************************************************************
2714 * Wan subdriver
2715 */
2716
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002717enum {
2718 /* ACPI GWAN/SWAN bits */
2719 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2720 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2721 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2722};
2723
2724static int wan_get_radiosw(void);
2725static int wan_set_radiosw(int radio_on);
2726
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002727/* sysfs wan enable ---------------------------------------------------- */
2728static ssize_t wan_enable_show(struct device *dev,
2729 struct device_attribute *attr,
2730 char *buf)
2731{
2732 int status;
2733
2734 status = wan_get_radiosw();
2735 if (status < 0)
2736 return status;
2737
2738 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2739}
2740
2741static ssize_t wan_enable_store(struct device *dev,
2742 struct device_attribute *attr,
2743 const char *buf, size_t count)
2744{
2745 unsigned long t;
2746 int res;
2747
2748 if (parse_strtoul(buf, 1, &t))
2749 return -EINVAL;
2750
2751 res = wan_set_radiosw(t);
2752
2753 return (res) ? res : count;
2754}
2755
2756static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002757 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002758 wan_enable_show, wan_enable_store);
2759
2760/* --------------------------------------------------------------------- */
2761
2762static struct attribute *wan_attributes[] = {
2763 &dev_attr_wan_enable.attr,
2764 NULL
2765};
2766
2767static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002768 .attrs = wan_attributes,
2769};
2770
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002771static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002772{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002773 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002774 int status = 0;
2775
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002776 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2777
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002778 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002779
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002780 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002781 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002782
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002783 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2784 str_supported(tp_features.wan),
2785 status);
2786
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002787 if (tp_features.wan) {
2788 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2789 /* no wan hardware present in system */
2790 tp_features.wan = 0;
2791 dbg_printk(TPACPI_DBG_INIT,
2792 "wan hardware not installed\n");
2793 } else {
2794 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2795 &wan_attr_group);
2796 if (res)
2797 return res;
2798 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002799 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002800
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002801 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002802}
2803
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002804static void wan_exit(void)
2805{
2806 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2807 &wan_attr_group);
2808}
2809
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002810static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002811{
2812 int status;
2813
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002814 if (!tp_features.wan)
2815 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002816
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002817 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2818 return -EIO;
2819
2820 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2821}
2822
2823static int wan_set_radiosw(int radio_on)
2824{
2825 int status;
2826
2827 if (!tp_features.wan)
2828 return -ENODEV;
2829
2830 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2831 return -EIO;
2832 if (radio_on)
2833 status |= TP_ACPI_WANCARD_RADIOSSW;
2834 else
2835 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2836 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2837 return -EIO;
2838
2839 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002840}
2841
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002842/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002843static int wan_read(char *p)
2844{
2845 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002846 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002847
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002848 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002849 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002850 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002851 len += sprintf(p + len, "status:\t\t%s\n",
2852 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002853 len += sprintf(p + len, "commands:\tenable, disable\n");
2854 }
2855
2856 return len;
2857}
2858
2859static int wan_write(char *buf)
2860{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002861 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002862
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002863 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002864 return -ENODEV;
2865
2866 while ((cmd = next_cmd(&buf))) {
2867 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002868 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002869 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002870 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002871 } else
2872 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002873 }
2874
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002875 return 0;
2876}
2877
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002878static struct ibm_struct wan_driver_data = {
2879 .name = "wan",
2880 .read = wan_read,
2881 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002882 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002883 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002884};
2885
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002886/*************************************************************************
2887 * Video subdriver
2888 */
2889
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002890#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2891
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002892enum video_access_mode {
2893 TPACPI_VIDEO_NONE = 0,
2894 TPACPI_VIDEO_570, /* 570 */
2895 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2896 TPACPI_VIDEO_NEW, /* all others */
2897};
2898
2899enum { /* video status flags, based on VIDEO_570 */
2900 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2901 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2902 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2903};
2904
2905enum { /* TPACPI_VIDEO_570 constants */
2906 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2907 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2908 * video_status_flags */
2909 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2910 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2911};
2912
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002913static enum video_access_mode video_supported;
2914static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002915
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002916static int video_autosw_get(void);
2917static int video_autosw_set(int enable);
2918
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002919TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002920
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002921static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002923 int ivga;
2924
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002925 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2926
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002927 TPACPI_ACPIHANDLE_INIT(vid);
2928 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002929
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002930 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2931 /* G41, assume IVGA doesn't change */
2932 vid_handle = vid2_handle;
2933
2934 if (!vid_handle)
2935 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002936 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002937 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2938 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002939 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002940 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2941 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002942 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002943 else
2944 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002945 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002947 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2948 str_supported(video_supported != TPACPI_VIDEO_NONE),
2949 video_supported);
2950
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002951 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002954static void video_exit(void)
2955{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002956 dbg_printk(TPACPI_DBG_EXIT,
2957 "restoring original video autoswitch mode\n");
2958 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002959 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002960 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002961}
2962
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002963static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
2965 int status = 0;
2966 int i;
2967
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002968 switch (video_supported) {
2969 case TPACPI_VIDEO_570:
2970 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2971 TP_ACPI_VIDEO_570_PHSCMD))
2972 return -EIO;
2973 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2974 break;
2975 case TPACPI_VIDEO_770:
2976 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2977 return -EIO;
2978 if (i)
2979 status |= TP_ACPI_VIDEO_S_LCD;
2980 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2981 return -EIO;
2982 if (i)
2983 status |= TP_ACPI_VIDEO_S_CRT;
2984 break;
2985 case TPACPI_VIDEO_NEW:
2986 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2987 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2988 return -EIO;
2989 if (i)
2990 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002992 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2993 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2994 return -EIO;
2995 if (i)
2996 status |= TP_ACPI_VIDEO_S_LCD;
2997 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2998 return -EIO;
2999 if (i)
3000 status |= TP_ACPI_VIDEO_S_DVI;
3001 break;
3002 default:
3003 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
3006 return status;
3007}
3008
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003009static int video_outputsw_set(int status)
3010{
3011 int autosw;
3012 int res = 0;
3013
3014 switch (video_supported) {
3015 case TPACPI_VIDEO_570:
3016 res = acpi_evalf(NULL, NULL,
3017 "\\_SB.PHS2", "vdd",
3018 TP_ACPI_VIDEO_570_PHS2CMD,
3019 status | TP_ACPI_VIDEO_570_PHS2SET);
3020 break;
3021 case TPACPI_VIDEO_770:
3022 autosw = video_autosw_get();
3023 if (autosw < 0)
3024 return autosw;
3025
3026 res = video_autosw_set(1);
3027 if (res)
3028 return res;
3029 res = acpi_evalf(vid_handle, NULL,
3030 "ASWT", "vdd", status * 0x100, 0);
3031 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003032 printk(TPACPI_ERR
3033 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003034 return -EIO;
3035 }
3036 break;
3037 case TPACPI_VIDEO_NEW:
3038 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003039 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003040 break;
3041 default:
3042 return -ENOSYS;
3043 }
3044
3045 return (res)? 0 : -EIO;
3046}
3047
3048static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003050 int autosw = 0;
3051
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003052 switch (video_supported) {
3053 case TPACPI_VIDEO_570:
3054 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3055 return -EIO;
3056 break;
3057 case TPACPI_VIDEO_770:
3058 case TPACPI_VIDEO_NEW:
3059 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3060 return -EIO;
3061 break;
3062 default:
3063 return -ENOSYS;
3064 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003065
3066 return autosw & 1;
3067}
3068
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003069static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003070{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003071 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003072 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003073 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003074}
3075
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003076static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003077{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003078 int autosw = video_autosw_get();
3079 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003080
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003081 if (autosw < 0)
3082 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003083
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003084 switch (video_supported) {
3085 case TPACPI_VIDEO_570:
3086 res = video_autosw_set(1);
3087 if (res)
3088 return res;
3089 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3090 break;
3091 case TPACPI_VIDEO_770:
3092 case TPACPI_VIDEO_NEW:
3093 res = video_autosw_set(1);
3094 if (res)
3095 return res;
3096 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3097 break;
3098 default:
3099 return -ENOSYS;
3100 }
3101 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003102 printk(TPACPI_ERR
3103 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003104 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003105 }
3106
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003107 return (res)? 0 : -EIO;
3108}
3109
3110static int video_expand_toggle(void)
3111{
3112 switch (video_supported) {
3113 case TPACPI_VIDEO_570:
3114 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3115 0 : -EIO;
3116 case TPACPI_VIDEO_770:
3117 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3118 0 : -EIO;
3119 case TPACPI_VIDEO_NEW:
3120 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3121 0 : -EIO;
3122 default:
3123 return -ENOSYS;
3124 }
3125 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003126}
3127
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003128static int video_read(char *p)
3129{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003130 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003131 int len = 0;
3132
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003133 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003134 len += sprintf(p + len, "status:\t\tnot supported\n");
3135 return len;
3136 }
3137
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003138 status = video_outputsw_get();
3139 if (status < 0)
3140 return status;
3141
3142 autosw = video_autosw_get();
3143 if (autosw < 0)
3144 return autosw;
3145
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003146 len += sprintf(p + len, "status:\t\tsupported\n");
3147 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3148 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003149 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003150 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3151 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3152 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3153 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003154 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003155 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3156 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3157 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3158
3159 return len;
3160}
3161
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003162static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
3164 char *cmd;
3165 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003166 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003168 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003169 return -ENODEV;
3170
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003171 enable = 0;
3172 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
3174 while ((cmd = next_cmd(&buf))) {
3175 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003176 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003178 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003180 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003182 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003183 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003184 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003185 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003186 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003187 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003188 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003190 res = video_autosw_set(1);
3191 if (res)
3192 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003194 res = video_autosw_set(0);
3195 if (res)
3196 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003198 res = video_outputsw_cycle();
3199 if (res)
3200 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003202 res = video_expand_toggle();
3203 if (res)
3204 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 } else
3206 return -EINVAL;
3207 }
3208
3209 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003210 status = video_outputsw_get();
3211 if (status < 0)
3212 return status;
3213 res = video_outputsw_set((status & ~disable) | enable);
3214 if (res)
3215 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
3217
3218 return 0;
3219}
3220
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003221static struct ibm_struct video_driver_data = {
3222 .name = "video",
3223 .read = video_read,
3224 .write = video_write,
3225 .exit = video_exit,
3226};
3227
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003228#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3229
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003230/*************************************************************************
3231 * Light (thinklight) subdriver
3232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003234TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3235TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003236
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003237static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003239 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3240
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003241 TPACPI_ACPIHANDLE_INIT(ledb);
3242 TPACPI_ACPIHANDLE_INIT(lght);
3243 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003244
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003245 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003246 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003247
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003248 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003249 /* light status not supported on
3250 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003251 tp_features.light_status =
3252 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003254 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003255 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003256
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003257 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003260static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
3262 int len = 0;
3263 int status = 0;
3264
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003265 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003266 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003267 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003268 len += sprintf(p + len, "status:\t\tunknown\n");
3269 len += sprintf(p + len, "commands:\ton, off\n");
3270 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3272 return -EIO;
3273 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003274 len += sprintf(p + len, "commands:\ton, off\n");
3275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 return len;
3278}
3279
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003280static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
3282 int cmos_cmd, lght_cmd;
3283 char *cmd;
3284 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003285
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003286 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003287 return -ENODEV;
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 while ((cmd = next_cmd(&buf))) {
3290 if (strlencmp(cmd, "on") == 0) {
3291 cmos_cmd = 0x0c;
3292 lght_cmd = 1;
3293 } else if (strlencmp(cmd, "off") == 0) {
3294 cmos_cmd = 0x0d;
3295 lght_cmd = 0;
3296 } else
3297 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003300 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3301 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (!success)
3303 return -EIO;
3304 }
3305
3306 return 0;
3307}
3308
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003309static struct ibm_struct light_driver_data = {
3310 .name = "light",
3311 .read = light_read,
3312 .write = light_write,
3313};
3314
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003315/*************************************************************************
3316 * Dock subdriver
3317 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003319#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003320
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003321static void dock_notify(struct ibm_struct *ibm, u32 event);
3322static int dock_read(char *p);
3323static int dock_write(char *buf);
3324
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003325TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003326 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3327 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3328 "\\_SB.PCI.ISA.SLCE", /* 570 */
3329 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3330
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003331/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003332TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003333
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003334static const struct acpi_device_id ibm_pci_device_ids[] = {
3335 {PCI_ROOT_HID_STRING, 0},
3336 {"", 0},
3337};
3338
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003339static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3340 {
3341 .notify = dock_notify,
3342 .handle = &dock_handle,
3343 .type = ACPI_SYSTEM_NOTIFY,
3344 },
3345 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003346 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3347 * We just use it to get notifications of dock hotplug
3348 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003349 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003350 .notify = dock_notify,
3351 .handle = &pci_handle,
3352 .type = ACPI_SYSTEM_NOTIFY,
3353 },
3354};
3355
3356static struct ibm_struct dock_driver_data[2] = {
3357 {
3358 .name = "dock",
3359 .read = dock_read,
3360 .write = dock_write,
3361 .acpi = &ibm_dock_acpidriver[0],
3362 },
3363 {
3364 .name = "dock",
3365 .acpi = &ibm_dock_acpidriver[1],
3366 },
3367};
3368
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369#define dock_docked() (_sta(dock_handle) & 1)
3370
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003371static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003372{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003373 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3374
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003375 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003376
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003377 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3378 str_supported(dock_handle != NULL));
3379
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003380 return (dock_handle)? 0 : 1;
3381}
3382
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003383static int __init dock_init2(struct ibm_init_struct *iibm)
3384{
3385 int dock2_needed;
3386
3387 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3388
3389 if (dock_driver_data[0].flags.acpi_driver_registered &&
3390 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003391 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003392 dock2_needed = (pci_handle != NULL);
3393 vdbg_printk(TPACPI_DBG_INIT,
3394 "dock PCI handler for the TP 570 is %s\n",
3395 str_supported(dock2_needed));
3396 } else {
3397 vdbg_printk(TPACPI_DBG_INIT,
3398 "dock subdriver part 2 not required\n");
3399 dock2_needed = 0;
3400 }
3401
3402 return (dock2_needed)? 0 : 1;
3403}
3404
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003405static void dock_notify(struct ibm_struct *ibm, u32 event)
3406{
3407 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003408 int pci = ibm->acpi->hid && ibm->acpi->device &&
3409 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003410 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003411
3412 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003413 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003414 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003415 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003416 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003417 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003418 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003419 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003420 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003421 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003422 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003423 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003424 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003425 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003426 }
Len Brown14e04fb2007-08-23 15:20:26 -04003427 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003428 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3429 ibm->acpi->device->dev.bus_id,
3430 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003431}
3432
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003433static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
3435 int len = 0;
3436 int docked = dock_docked();
3437
3438 if (!dock_handle)
3439 len += sprintf(p + len, "status:\t\tnot supported\n");
3440 else if (!docked)
3441 len += sprintf(p + len, "status:\t\tundocked\n");
3442 else {
3443 len += sprintf(p + len, "status:\t\tdocked\n");
3444 len += sprintf(p + len, "commands:\tdock, undock\n");
3445 }
3446
3447 return len;
3448}
3449
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003450static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
3452 char *cmd;
3453
3454 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003455 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 while ((cmd = next_cmd(&buf))) {
3458 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003459 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3460 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return -EIO;
3462 } else if (strlencmp(cmd, "dock") == 0) {
3463 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3464 return -EIO;
3465 } else
3466 return -EINVAL;
3467 }
3468
3469 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003470}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003472#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003474/*************************************************************************
3475 * Bay subdriver
3476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003478#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003479
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003480TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003481 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3482 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3483 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3484 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003485TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003486 "_EJ0", /* all others */
3487 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003488TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003489 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3490 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003491TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003492 "_EJ0", /* 770x */
3493 ); /* all others */
3494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003495static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003497 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3498
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003499 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003500 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003501 TPACPI_ACPIHANDLE_INIT(bay_ej);
3502 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003503 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003504 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003505
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003506 tp_features.bay_status = bay_handle &&
3507 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3508 tp_features.bay_status2 = bay2_handle &&
3509 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003510
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003511 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3512 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3513 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3514 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003516 vdbg_printk(TPACPI_DBG_INIT,
3517 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003518 str_supported(tp_features.bay_status),
3519 str_supported(tp_features.bay_eject),
3520 str_supported(tp_features.bay_status2),
3521 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003522
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003523 return (tp_features.bay_status || tp_features.bay_eject ||
3524 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003527static void bay_notify(struct ibm_struct *ibm, u32 event)
3528{
Len Brown14e04fb2007-08-23 15:20:26 -04003529 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003530 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3531 ibm->acpi->device->dev.bus_id,
3532 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003533}
3534
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003535#define bay_occupied(b) (_sta(b##_handle) & 1)
3536
3537static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
3539 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003540 int occupied = bay_occupied(bay);
3541 int occupied2 = bay_occupied(bay2);
3542 int eject, eject2;
3543
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003544 len += sprintf(p + len, "status:\t\t%s\n",
3545 tp_features.bay_status ?
3546 (occupied ? "occupied" : "unoccupied") :
3547 "not supported");
3548 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003549 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3550 "occupied" : "unoccupied");
3551
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003552 eject = tp_features.bay_eject && occupied;
3553 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003554
3555 if (eject && eject2)
3556 len += sprintf(p + len, "commands:\teject, eject2\n");
3557 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003559 else if (eject2)
3560 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
3562 return len;
3563}
3564
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003565static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
3567 char *cmd;
3568
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003569 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003570 return -ENODEV;
3571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003573 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003574 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3575 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003576 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003577 strlencmp(cmd, "eject2") == 0) {
3578 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return -EIO;
3580 } else
3581 return -EINVAL;
3582 }
3583
3584 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003585}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003586
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003587static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3588 .notify = bay_notify,
3589 .handle = &bay_handle,
3590 .type = ACPI_SYSTEM_NOTIFY,
3591};
3592
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003593static struct ibm_struct bay_driver_data = {
3594 .name = "bay",
3595 .read = bay_read,
3596 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003597 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003598};
3599
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003600#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003602/*************************************************************************
3603 * CMOS subdriver
3604 */
3605
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003606/* sysfs cmos_command -------------------------------------------------- */
3607static ssize_t cmos_command_store(struct device *dev,
3608 struct device_attribute *attr,
3609 const char *buf, size_t count)
3610{
3611 unsigned long cmos_cmd;
3612 int res;
3613
3614 if (parse_strtoul(buf, 21, &cmos_cmd))
3615 return -EINVAL;
3616
3617 res = issue_thinkpad_cmos_command(cmos_cmd);
3618 return (res)? res : count;
3619}
3620
3621static struct device_attribute dev_attr_cmos_command =
3622 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3623
3624/* --------------------------------------------------------------------- */
3625
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003626static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003627{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003628 int res;
3629
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003630 vdbg_printk(TPACPI_DBG_INIT,
3631 "initializing cmos commands subdriver\n");
3632
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003633 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003634
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003635 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3636 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003637
3638 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3639 if (res)
3640 return res;
3641
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003642 return (cmos_handle)? 0 : 1;
3643}
3644
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003645static void cmos_exit(void)
3646{
3647 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3648}
3649
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003650static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 int len = 0;
3653
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003654 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3655 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 if (!cmos_handle)
3657 len += sprintf(p + len, "status:\t\tnot supported\n");
3658 else {
3659 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003660 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
3662
3663 return len;
3664}
3665
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003666static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
3668 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003669 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
3671 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003672 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3673 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 /* cmos_cmd set */
3675 } else
3676 return -EINVAL;
3677
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003678 res = issue_thinkpad_cmos_command(cmos_cmd);
3679 if (res)
3680 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 }
3682
3683 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003684}
3685
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003686static struct ibm_struct cmos_driver_data = {
3687 .name = "cmos",
3688 .read = cmos_read,
3689 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003690 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003691};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003692
3693/*************************************************************************
3694 * LED subdriver
3695 */
3696
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003697enum led_access_mode {
3698 TPACPI_LED_NONE = 0,
3699 TPACPI_LED_570, /* 570 */
3700 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3701 TPACPI_LED_NEW, /* all others */
3702};
3703
3704enum { /* For TPACPI_LED_OLD */
3705 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3706 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3707 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3708};
3709
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003710static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003711
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003712TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003713 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3714 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003715 "LED", /* all others */
3716 ); /* R30, R31 */
3717
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003718static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003719{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003720 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3721
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003722 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003723
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003724 if (!led_handle)
3725 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003726 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003727 else if (strlencmp(led_path, "SLED") == 0)
3728 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003729 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003730 else if (strlencmp(led_path, "SYSL") == 0)
3731 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003732 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003733 else
3734 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003735 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003736
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003737 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3738 str_supported(led_supported), led_supported);
3739
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003740 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003741}
3742
3743#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3744
3745static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
3747 int len = 0;
3748
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003749 if (!led_supported) {
3750 len += sprintf(p + len, "status:\t\tnot supported\n");
3751 return len;
3752 }
3753 len += sprintf(p + len, "status:\t\tsupported\n");
3754
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003755 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003756 /* 570 */
3757 int i, status;
3758 for (i = 0; i < 8; i++) {
3759 if (!acpi_evalf(ec_handle,
3760 &status, "GLED", "dd", 1 << i))
3761 return -EIO;
3762 len += sprintf(p + len, "%d:\t\t%s\n",
3763 i, led_status(status));
3764 }
3765 }
3766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003768 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
3770 return len;
3771}
3772
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003773/* off, on, blink */
3774static const int led_sled_arg1[] = { 0, 1, 3 };
3775static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3776static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3777static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3778
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003779static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
3781 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003782 int led, ind, ret;
3783
3784 if (!led_supported)
3785 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
3787 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003788 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 return -EINVAL;
3790
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003791 if (strstr(cmd, "off")) {
3792 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003794 ind = 1;
3795 } else if (strstr(cmd, "blink")) {
3796 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 } else
3798 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003799
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003800 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003801 /* 570 */
3802 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003804 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003806 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003807 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3808 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003809 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003810 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003811 ret = ec_write(TPACPI_LED_EC_HLBL,
3812 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003813 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003814 ret = ec_write(TPACPI_LED_EC_HLCL,
3815 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003816 if (ret < 0)
3817 return ret;
3818 } else {
3819 /* all others */
3820 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3821 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 }
3825
3826 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003827}
3828
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003829static struct ibm_struct led_driver_data = {
3830 .name = "led",
3831 .read = led_read,
3832 .write = led_write,
3833};
3834
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003835/*************************************************************************
3836 * Beep subdriver
3837 */
3838
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003839TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003840
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003841static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003842{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003843 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3844
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003845 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003846
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003847 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3848 str_supported(beep_handle != NULL));
3849
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003850 return (beep_handle)? 0 : 1;
3851}
3852
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003853static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854{
3855 int len = 0;
3856
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003857 if (!beep_handle)
3858 len += sprintf(p + len, "status:\t\tnot supported\n");
3859 else {
3860 len += sprintf(p + len, "status:\t\tsupported\n");
3861 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
3864 return len;
3865}
3866
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003867static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868{
3869 char *cmd;
3870 int beep_cmd;
3871
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003872 if (!beep_handle)
3873 return -ENODEV;
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003876 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3877 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 /* beep_cmd set */
3879 } else
3880 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003881 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 return -EIO;
3883 }
3884
3885 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003886}
3887
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003888static struct ibm_struct beep_driver_data = {
3889 .name = "beep",
3890 .read = beep_read,
3891 .write = beep_write,
3892};
3893
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003894/*************************************************************************
3895 * Thermal subdriver
3896 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003897
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003898enum thermal_access_mode {
3899 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3900 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3901 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3902 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3903 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3904};
3905
3906enum { /* TPACPI_THERMAL_TPEC_* */
3907 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3908 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3909 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3910};
3911
3912#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3913struct ibm_thermal_sensors_struct {
3914 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3915};
3916
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003917static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003918
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003919/* idx is zero-based */
3920static int thermal_get_sensor(int idx, s32 *value)
3921{
3922 int t;
3923 s8 tmp;
3924 char tmpi[5];
3925
3926 t = TP_EC_THERMAL_TMP0;
3927
3928 switch (thermal_read_mode) {
3929#if TPACPI_MAX_THERMAL_SENSORS >= 16
3930 case TPACPI_THERMAL_TPEC_16:
3931 if (idx >= 8 && idx <= 15) {
3932 t = TP_EC_THERMAL_TMP8;
3933 idx -= 8;
3934 }
3935 /* fallthrough */
3936#endif
3937 case TPACPI_THERMAL_TPEC_8:
3938 if (idx <= 7) {
3939 if (!acpi_ec_read(t + idx, &tmp))
3940 return -EIO;
3941 *value = tmp * 1000;
3942 return 0;
3943 }
3944 break;
3945
3946 case TPACPI_THERMAL_ACPI_UPDT:
3947 if (idx <= 7) {
3948 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3949 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3950 return -EIO;
3951 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3952 return -EIO;
3953 *value = (t - 2732) * 100;
3954 return 0;
3955 }
3956 break;
3957
3958 case TPACPI_THERMAL_ACPI_TMP07:
3959 if (idx <= 7) {
3960 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3961 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3962 return -EIO;
3963 if (t > 127 || t < -127)
3964 t = TP_EC_THERMAL_TMP_NA;
3965 *value = t * 1000;
3966 return 0;
3967 }
3968 break;
3969
3970 case TPACPI_THERMAL_NONE:
3971 default:
3972 return -ENOSYS;
3973 }
3974
3975 return -EINVAL;
3976}
3977
3978static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3979{
3980 int res, i;
3981 int n;
3982
3983 n = 8;
3984 i = 0;
3985
3986 if (!s)
3987 return -EINVAL;
3988
3989 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3990 n = 16;
3991
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003992 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003993 res = thermal_get_sensor(i, &s->temp[i]);
3994 if (res)
3995 return res;
3996 }
3997
3998 return n;
3999}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004000
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004001/* sysfs temp##_input -------------------------------------------------- */
4002
4003static ssize_t thermal_temp_input_show(struct device *dev,
4004 struct device_attribute *attr,
4005 char *buf)
4006{
4007 struct sensor_device_attribute *sensor_attr =
4008 to_sensor_dev_attr(attr);
4009 int idx = sensor_attr->index;
4010 s32 value;
4011 int res;
4012
4013 res = thermal_get_sensor(idx, &value);
4014 if (res)
4015 return res;
4016 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4017 return -ENXIO;
4018
4019 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4020}
4021
4022#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004023 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4024 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004025
4026static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4027 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4028 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4029 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4030 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4031 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4032 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4033 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4034 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4035 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4036 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4037 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4038 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4039 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4040 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4041 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4042 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4043};
4044
4045#define THERMAL_ATTRS(X) \
4046 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4047
4048static struct attribute *thermal_temp_input_attr[] = {
4049 THERMAL_ATTRS(8),
4050 THERMAL_ATTRS(9),
4051 THERMAL_ATTRS(10),
4052 THERMAL_ATTRS(11),
4053 THERMAL_ATTRS(12),
4054 THERMAL_ATTRS(13),
4055 THERMAL_ATTRS(14),
4056 THERMAL_ATTRS(15),
4057 THERMAL_ATTRS(0),
4058 THERMAL_ATTRS(1),
4059 THERMAL_ATTRS(2),
4060 THERMAL_ATTRS(3),
4061 THERMAL_ATTRS(4),
4062 THERMAL_ATTRS(5),
4063 THERMAL_ATTRS(6),
4064 THERMAL_ATTRS(7),
4065 NULL
4066};
4067
4068static const struct attribute_group thermal_temp_input16_group = {
4069 .attrs = thermal_temp_input_attr
4070};
4071
4072static const struct attribute_group thermal_temp_input8_group = {
4073 .attrs = &thermal_temp_input_attr[8]
4074};
4075
4076#undef THERMAL_SENSOR_ATTR_TEMP
4077#undef THERMAL_ATTRS
4078
4079/* --------------------------------------------------------------------- */
4080
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004081static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004082{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004083 u8 t, ta1, ta2;
4084 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004085 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004086 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004087
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004088 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4089
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004090 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004091
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004092 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004093 /*
4094 * Direct EC access mode: sensors at registers
4095 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4096 * non-implemented, thermal sensors return 0x80 when
4097 * not available
4098 */
4099
4100 ta1 = ta2 = 0;
4101 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004102 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004103 ta1 |= t;
4104 } else {
4105 ta1 = 0;
4106 break;
4107 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004108 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004109 ta2 |= t;
4110 } else {
4111 ta1 = 0;
4112 break;
4113 }
4114 }
4115 if (ta1 == 0) {
4116 /* This is sheer paranoia, but we handle it anyway */
4117 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004118 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004119 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004120 "falling back to ACPI TMPx access "
4121 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004122 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004123 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004124 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004125 "ThinkPad ACPI EC access misbehaving, "
4126 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004127 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004128 }
4129 } else {
4130 thermal_read_mode =
4131 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004132 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004133 }
4134 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004135 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4136 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004137 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004138 } else {
4139 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004140 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004141 }
4142 } else {
4143 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004144 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004145 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004146
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004147 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4148 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4149 thermal_read_mode);
4150
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004151 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004152 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004153 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004154 &thermal_temp_input16_group);
4155 if (res)
4156 return res;
4157 break;
4158 case TPACPI_THERMAL_TPEC_8:
4159 case TPACPI_THERMAL_ACPI_TMP07:
4160 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004161 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004162 &thermal_temp_input8_group);
4163 if (res)
4164 return res;
4165 break;
4166 case TPACPI_THERMAL_NONE:
4167 default:
4168 return 1;
4169 }
4170
4171 return 0;
4172}
4173
4174static void thermal_exit(void)
4175{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004176 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004177 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004178 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004179 &thermal_temp_input16_group);
4180 break;
4181 case TPACPI_THERMAL_TPEC_8:
4182 case TPACPI_THERMAL_ACPI_TMP07:
4183 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004184 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004185 &thermal_temp_input16_group);
4186 break;
4187 case TPACPI_THERMAL_NONE:
4188 default:
4189 break;
4190 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004191}
4192
4193static int thermal_read(char *p)
4194{
4195 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004196 int n, i;
4197 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004198
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004199 n = thermal_get_sensors(&t);
4200 if (unlikely(n < 0))
4201 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004202
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004203 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004204
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004205 if (n > 0) {
4206 for (i = 0; i < (n - 1); i++)
4207 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4208 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4209 } else
4210 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004211
4212 return len;
4213}
4214
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004215static struct ibm_struct thermal_driver_data = {
4216 .name = "thermal",
4217 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004218 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004219};
4220
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004221/*************************************************************************
4222 * EC Dump subdriver
4223 */
4224
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004225static u8 ecdump_regs[256];
4226
4227static int ecdump_read(char *p)
4228{
4229 int len = 0;
4230 int i, j;
4231 u8 v;
4232
4233 len += sprintf(p + len, "EC "
4234 " +00 +01 +02 +03 +04 +05 +06 +07"
4235 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4236 for (i = 0; i < 256; i += 16) {
4237 len += sprintf(p + len, "EC 0x%02x:", i);
4238 for (j = 0; j < 16; j++) {
4239 if (!acpi_ec_read(i + j, &v))
4240 break;
4241 if (v != ecdump_regs[i + j])
4242 len += sprintf(p + len, " *%02x", v);
4243 else
4244 len += sprintf(p + len, " %02x", v);
4245 ecdump_regs[i + j] = v;
4246 }
4247 len += sprintf(p + len, "\n");
4248 if (j != 16)
4249 break;
4250 }
4251
4252 /* These are way too dangerous to advertise openly... */
4253#if 0
4254 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4255 " (<offset> is 00-ff, <value> is 00-ff)\n");
4256 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4257 " (<offset> is 00-ff, <value> is 0-255)\n");
4258#endif
4259 return len;
4260}
4261
4262static int ecdump_write(char *buf)
4263{
4264 char *cmd;
4265 int i, v;
4266
4267 while ((cmd = next_cmd(&buf))) {
4268 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4269 /* i and v set */
4270 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4271 /* i and v set */
4272 } else
4273 return -EINVAL;
4274 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4275 if (!acpi_ec_write(i, v))
4276 return -EIO;
4277 } else
4278 return -EINVAL;
4279 }
4280
4281 return 0;
4282}
4283
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004284static struct ibm_struct ecdump_driver_data = {
4285 .name = "ecdump",
4286 .read = ecdump_read,
4287 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004288 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004289};
4290
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004291/*************************************************************************
4292 * Backlight/brightness subdriver
4293 */
Holger Macht8acb0252006-10-20 14:30:28 -07004294
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004295#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4296
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004297static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004298static int brightness_offset = 0x31;
4299static int brightness_mode;
4300static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4301
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004302static struct mutex brightness_mutex;
4303
4304/*
4305 * ThinkPads can read brightness from two places: EC 0x31, or
4306 * CMOS NVRAM byte 0x5E, bits 0-3.
4307 */
4308static int brightness_get(struct backlight_device *bd)
4309{
4310 u8 lec = 0, lcmos = 0, level = 0;
4311
4312 if (brightness_mode & 1) {
4313 if (!acpi_ec_read(brightness_offset, &lec))
4314 return -EIO;
4315 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4316 level = lec;
4317 };
4318 if (brightness_mode & 2) {
4319 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4320 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4321 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4322 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4323 level = lcmos;
4324 }
4325
4326 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004327 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004328 "CMOS NVRAM (%u) and EC (%u) do not agree "
4329 "on display brightness level\n",
4330 (unsigned int) lcmos,
4331 (unsigned int) lec);
4332 return -EIO;
4333 }
4334
4335 return level;
4336}
4337
4338/* May return EINTR which can always be mapped to ERESTARTSYS */
4339static int brightness_set(int value)
4340{
4341 int cmos_cmd, inc, i, res;
4342 int current_value;
4343
4344 if (value > ((tp_features.bright_16levels)? 15 : 7))
4345 return -EINVAL;
4346
4347 res = mutex_lock_interruptible(&brightness_mutex);
4348 if (res < 0)
4349 return res;
4350
4351 current_value = brightness_get(NULL);
4352 if (current_value < 0) {
4353 res = current_value;
4354 goto errout;
4355 }
4356
4357 cmos_cmd = value > current_value ?
4358 TP_CMOS_BRIGHTNESS_UP :
4359 TP_CMOS_BRIGHTNESS_DOWN;
4360 inc = (value > current_value)? 1 : -1;
4361
4362 res = 0;
4363 for (i = current_value; i != value; i += inc) {
4364 if ((brightness_mode & 2) &&
4365 issue_thinkpad_cmos_command(cmos_cmd)) {
4366 res = -EIO;
4367 goto errout;
4368 }
4369 if ((brightness_mode & 1) &&
4370 !acpi_ec_write(brightness_offset, i + inc)) {
4371 res = -EIO;
4372 goto errout;;
4373 }
4374 }
4375
4376errout:
4377 mutex_unlock(&brightness_mutex);
4378 return res;
4379}
4380
4381/* sysfs backlight class ----------------------------------------------- */
4382
4383static int brightness_update_status(struct backlight_device *bd)
4384{
4385 /* it is the backlight class's job (caller) to handle
4386 * EINTR and other errors properly */
4387 return brightness_set(
4388 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4389 bd->props.power == FB_BLANK_UNBLANK) ?
4390 bd->props.brightness : 0);
4391}
Holger Macht8acb0252006-10-20 14:30:28 -07004392
Richard Purdie599a52d2007-02-10 23:07:48 +00004393static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004394 .get_brightness = brightness_get,
4395 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004396};
4397
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004398/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004399
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004400static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004401{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004402 int b;
4403
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004404 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4405
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004406 mutex_init(&brightness_mutex);
4407
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004408 /*
4409 * We always attempt to detect acpi support, so as to switch
4410 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4411 * going to publish a backlight interface
4412 */
4413 b = tpacpi_check_std_acpi_brightness_support();
4414 if (b > 0) {
4415 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4416 printk(TPACPI_NOTICE
4417 "Lenovo BIOS switched to ACPI backlight "
4418 "control mode\n");
4419 }
4420 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004421 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004422 "standard ACPI backlight interface "
4423 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004424 return 1;
4425 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004426 }
4427
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004428 if (!brightness_enable) {
4429 dbg_printk(TPACPI_DBG_INIT,
4430 "brightness support disabled by "
4431 "module parameter\n");
4432 return 1;
4433 }
4434
4435 if (b > 16) {
4436 printk(TPACPI_ERR
4437 "Unsupported brightness interface, "
4438 "please contact %s\n", TPACPI_MAIL);
4439 return 1;
4440 }
4441 if (b == 16)
4442 tp_features.bright_16levels = 1;
4443
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004444 if (!brightness_mode) {
4445 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4446 brightness_mode = 2;
4447 else
4448 brightness_mode = 3;
4449
4450 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4451 brightness_mode);
4452 }
4453
4454 if (brightness_mode > 3)
4455 return -EINVAL;
4456
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004457 b = brightness_get(NULL);
4458 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004459 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004460
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004461 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004462 printk(TPACPI_INFO
4463 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004464
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004465 ibm_backlight_device = backlight_device_register(
4466 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4467 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004468 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004469 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004470 return PTR_ERR(ibm_backlight_device);
4471 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004472 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004473
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004474 ibm_backlight_device->props.max_brightness =
4475 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004476 ibm_backlight_device->props.brightness = b;
4477 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004478
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004479 return 0;
4480}
4481
4482static void brightness_exit(void)
4483{
4484 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004485 vdbg_printk(TPACPI_DBG_EXIT,
4486 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004487 backlight_device_unregister(ibm_backlight_device);
4488 ibm_backlight_device = NULL;
4489 }
4490}
4491
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004492static int brightness_read(char *p)
4493{
4494 int len = 0;
4495 int level;
4496
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004497 level = brightness_get(NULL);
4498 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004499 len += sprintf(p + len, "level:\t\tunreadable\n");
4500 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004501 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004502 len += sprintf(p + len, "commands:\tup, down\n");
4503 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004504 " (<level> is 0-%d)\n",
4505 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004506 }
4507
4508 return len;
4509}
4510
4511static int brightness_write(char *buf)
4512{
4513 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004514 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004515 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004516 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004517
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004518 level = brightness_get(NULL);
4519 if (level < 0)
4520 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004521
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004522 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004523 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004524 if (level < max_level)
4525 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004526 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004527 if (level > 0)
4528 level--;
4529 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4530 level >= 0 && level <= max_level) {
4531 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004532 } else
4533 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004534 }
4535
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004536 /*
4537 * Now we know what the final level should be, so we try to set it.
4538 * Doing it this way makes the syscall restartable in case of EINTR
4539 */
4540 rc = brightness_set(level);
4541 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004542}
4543
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004544static struct ibm_struct brightness_driver_data = {
4545 .name = "brightness",
4546 .read = brightness_read,
4547 .write = brightness_write,
4548 .exit = brightness_exit,
4549};
4550
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004551/*************************************************************************
4552 * Volume subdriver
4553 */
4554
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004555static int volume_offset = 0x30;
4556
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004557static int volume_read(char *p)
4558{
4559 int len = 0;
4560 u8 level;
4561
4562 if (!acpi_ec_read(volume_offset, &level)) {
4563 len += sprintf(p + len, "level:\t\tunreadable\n");
4564 } else {
4565 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4566 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4567 len += sprintf(p + len, "commands:\tup, down, mute\n");
4568 len += sprintf(p + len, "commands:\tlevel <level>"
4569 " (<level> is 0-15)\n");
4570 }
4571
4572 return len;
4573}
4574
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004575static int volume_write(char *buf)
4576{
4577 int cmos_cmd, inc, i;
4578 u8 level, mute;
4579 int new_level, new_mute;
4580 char *cmd;
4581
4582 while ((cmd = next_cmd(&buf))) {
4583 if (!acpi_ec_read(volume_offset, &level))
4584 return -EIO;
4585 new_mute = mute = level & 0x40;
4586 new_level = level = level & 0xf;
4587
4588 if (strlencmp(cmd, "up") == 0) {
4589 if (mute)
4590 new_mute = 0;
4591 else
4592 new_level = level == 15 ? 15 : level + 1;
4593 } else if (strlencmp(cmd, "down") == 0) {
4594 if (mute)
4595 new_mute = 0;
4596 else
4597 new_level = level == 0 ? 0 : level - 1;
4598 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4599 new_level >= 0 && new_level <= 15) {
4600 /* new_level set */
4601 } else if (strlencmp(cmd, "mute") == 0) {
4602 new_mute = 0x40;
4603 } else
4604 return -EINVAL;
4605
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004606 if (new_level != level) {
4607 /* mute doesn't change */
4608
4609 cmos_cmd = (new_level > level) ?
4610 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004611 inc = new_level > level ? 1 : -1;
4612
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004613 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004614 !acpi_ec_write(volume_offset, level)))
4615 return -EIO;
4616
4617 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004618 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004619 !acpi_ec_write(volume_offset, i + inc))
4620 return -EIO;
4621
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004622 if (mute &&
4623 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4624 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004625 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004626 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004627 }
4628
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004629 if (new_mute != mute) {
4630 /* level doesn't change */
4631
4632 cmos_cmd = (new_mute) ?
4633 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004634
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004635 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004636 !acpi_ec_write(volume_offset, level + new_mute))
4637 return -EIO;
4638 }
4639 }
4640
4641 return 0;
4642}
4643
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004644static struct ibm_struct volume_driver_data = {
4645 .name = "volume",
4646 .read = volume_read,
4647 .write = volume_write,
4648};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004649
4650/*************************************************************************
4651 * Fan subdriver
4652 */
4653
4654/*
4655 * FAN ACCESS MODES
4656 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004657 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004658 * ACPI GFAN method: returns fan level
4659 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004660 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004661 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004662 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004663 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004664 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4665 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004666 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4667 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004668 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004669 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004670 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4671 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004672 *
4673 * Fan speed changes of any sort (including those caused by the
4674 * disengaged mode) are usually done slowly by the firmware as the
4675 * maximum ammount of fan duty cycle change per second seems to be
4676 * limited.
4677 *
4678 * Reading is not available if GFAN exists.
4679 * Writing is not available if SFAN exists.
4680 *
4681 * Bits
4682 * 7 automatic mode engaged;
4683 * (default operation mode of the ThinkPad)
4684 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004685 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004686 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004687 * the tachometer while the fan controller ramps up
4688 * the speed (which can take up to a few *minutes*).
4689 * Speeds up fan to 100% duty-cycle, which is far above
4690 * the standard RPM levels. It is not impossible that
4691 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004692 * 5-3 unused in some models. Extra bits for fan level
4693 * in others, but still useless as all values above
4694 * 7 map to the same speed as level 7 in these models.
4695 * 2-0 fan level (0..7 usually)
4696 * 0x00 = stop
4697 * 0x07 = max (set when temperatures critical)
4698 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004699 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004700 *
4701 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4702 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4703 * does so, its initial value is meaningless (0x07).
4704 *
4705 * For firmware bugs, refer to:
4706 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4707 *
4708 * ----
4709 *
4710 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4711 * Main fan tachometer reading (in RPM)
4712 *
4713 * This register is present on all ThinkPads with a new-style EC, and
4714 * it is known not to be present on the A21m/e, and T22, as there is
4715 * something else in offset 0x84 according to the ACPI DSDT. Other
4716 * ThinkPads from this same time period (and earlier) probably lack the
4717 * tachometer as well.
4718 *
4719 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4720 * was never fixed by IBM to report the EC firmware version string
4721 * probably support the tachometer (like the early X models), so
4722 * detecting it is quite hard. We need more data to know for sure.
4723 *
4724 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4725 * might result.
4726 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004727 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4728 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004729 *
4730 * For firmware bugs, refer to:
4731 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4732 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004733 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004734 * ThinkPad X31, X40, X41. Not available in the X60.
4735 *
4736 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4737 * high speed. ACPI DSDT seems to map these three speeds to levels
4738 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4739 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4740 *
4741 * The speeds are stored on handles
4742 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4743 *
4744 * There are three default speed sets, acessible as handles:
4745 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4746 *
4747 * ACPI DSDT switches which set is in use depending on various
4748 * factors.
4749 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004750 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004751 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4752 * but the ACPI tables just mention level 7.
4753 */
4754
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004755enum { /* Fan control constants */
4756 fan_status_offset = 0x2f, /* EC register 0x2f */
4757 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4758 * 0x84 must be read before 0x85 */
4759
4760 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4761 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4762
4763 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4764};
4765
4766enum fan_status_access_mode {
4767 TPACPI_FAN_NONE = 0, /* No fan status or control */
4768 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4769 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4770};
4771
4772enum fan_control_access_mode {
4773 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4774 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4775 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4776 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4777};
4778
4779enum fan_control_commands {
4780 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4781 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4782 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4783 * and also watchdog cmd */
4784};
4785
4786static int fan_control_allowed;
4787
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004788static enum fan_status_access_mode fan_status_access_mode;
4789static enum fan_control_access_mode fan_control_access_mode;
4790static enum fan_control_commands fan_control_commands;
4791
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004792static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004793static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004794static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004795
4796static struct mutex fan_mutex;
4797
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004798static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004799static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004800
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004801TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4802TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004803 "\\FSPD", /* 600e/x, 770e, 770x */
4804 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004805TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004806 "JFNS", /* 770x-JL */
4807 ); /* all others */
4808
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004809/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004810 * Call with fan_mutex held
4811 */
4812static void fan_update_desired_level(u8 status)
4813{
4814 if ((status &
4815 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4816 if (status > 7)
4817 fan_control_desired_level = 7;
4818 else
4819 fan_control_desired_level = status;
4820 }
4821}
4822
4823static int fan_get_status(u8 *status)
4824{
4825 u8 s;
4826
4827 /* TODO:
4828 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4829
4830 switch (fan_status_access_mode) {
4831 case TPACPI_FAN_RD_ACPI_GFAN:
4832 /* 570, 600e/x, 770e, 770x */
4833
4834 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4835 return -EIO;
4836
4837 if (likely(status))
4838 *status = s & 0x07;
4839
4840 break;
4841
4842 case TPACPI_FAN_RD_TPEC:
4843 /* all except 570, 600e/x, 770e, 770x */
4844 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4845 return -EIO;
4846
4847 if (likely(status))
4848 *status = s;
4849
4850 break;
4851
4852 default:
4853 return -ENXIO;
4854 }
4855
4856 return 0;
4857}
4858
4859static int fan_get_status_safe(u8 *status)
4860{
4861 int rc;
4862 u8 s;
4863
4864 if (mutex_lock_interruptible(&fan_mutex))
4865 return -ERESTARTSYS;
4866 rc = fan_get_status(&s);
4867 if (!rc)
4868 fan_update_desired_level(s);
4869 mutex_unlock(&fan_mutex);
4870
4871 if (status)
4872 *status = s;
4873
4874 return rc;
4875}
4876
4877static int fan_get_speed(unsigned int *speed)
4878{
4879 u8 hi, lo;
4880
4881 switch (fan_status_access_mode) {
4882 case TPACPI_FAN_RD_TPEC:
4883 /* all except 570, 600e/x, 770e, 770x */
4884 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4885 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4886 return -EIO;
4887
4888 if (likely(speed))
4889 *speed = (hi << 8) | lo;
4890
4891 break;
4892
4893 default:
4894 return -ENXIO;
4895 }
4896
4897 return 0;
4898}
4899
4900static int fan_set_level(int level)
4901{
4902 if (!fan_control_allowed)
4903 return -EPERM;
4904
4905 switch (fan_control_access_mode) {
4906 case TPACPI_FAN_WR_ACPI_SFAN:
4907 if (level >= 0 && level <= 7) {
4908 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4909 return -EIO;
4910 } else
4911 return -EINVAL;
4912 break;
4913
4914 case TPACPI_FAN_WR_ACPI_FANS:
4915 case TPACPI_FAN_WR_TPEC:
4916 if ((level != TP_EC_FAN_AUTO) &&
4917 (level != TP_EC_FAN_FULLSPEED) &&
4918 ((level < 0) || (level > 7)))
4919 return -EINVAL;
4920
4921 /* safety net should the EC not support AUTO
4922 * or FULLSPEED mode bits and just ignore them */
4923 if (level & TP_EC_FAN_FULLSPEED)
4924 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01004925 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004926 level |= 4; /* safety min speed 4 */
4927
4928 if (!acpi_ec_write(fan_status_offset, level))
4929 return -EIO;
4930 else
4931 tp_features.fan_ctrl_status_undef = 0;
4932 break;
4933
4934 default:
4935 return -ENXIO;
4936 }
4937 return 0;
4938}
4939
4940static int fan_set_level_safe(int level)
4941{
4942 int rc;
4943
4944 if (!fan_control_allowed)
4945 return -EPERM;
4946
4947 if (mutex_lock_interruptible(&fan_mutex))
4948 return -ERESTARTSYS;
4949
4950 if (level == TPACPI_FAN_LAST_LEVEL)
4951 level = fan_control_desired_level;
4952
4953 rc = fan_set_level(level);
4954 if (!rc)
4955 fan_update_desired_level(level);
4956
4957 mutex_unlock(&fan_mutex);
4958 return rc;
4959}
4960
4961static int fan_set_enable(void)
4962{
4963 u8 s;
4964 int rc;
4965
4966 if (!fan_control_allowed)
4967 return -EPERM;
4968
4969 if (mutex_lock_interruptible(&fan_mutex))
4970 return -ERESTARTSYS;
4971
4972 switch (fan_control_access_mode) {
4973 case TPACPI_FAN_WR_ACPI_FANS:
4974 case TPACPI_FAN_WR_TPEC:
4975 rc = fan_get_status(&s);
4976 if (rc < 0)
4977 break;
4978
4979 /* Don't go out of emergency fan mode */
4980 if (s != 7) {
4981 s &= 0x07;
4982 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4983 }
4984
4985 if (!acpi_ec_write(fan_status_offset, s))
4986 rc = -EIO;
4987 else {
4988 tp_features.fan_ctrl_status_undef = 0;
4989 rc = 0;
4990 }
4991 break;
4992
4993 case TPACPI_FAN_WR_ACPI_SFAN:
4994 rc = fan_get_status(&s);
4995 if (rc < 0)
4996 break;
4997
4998 s &= 0x07;
4999
5000 /* Set fan to at least level 4 */
5001 s |= 4;
5002
5003 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005004 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005005 else
5006 rc = 0;
5007 break;
5008
5009 default:
5010 rc = -ENXIO;
5011 }
5012
5013 mutex_unlock(&fan_mutex);
5014 return rc;
5015}
5016
5017static int fan_set_disable(void)
5018{
5019 int rc;
5020
5021 if (!fan_control_allowed)
5022 return -EPERM;
5023
5024 if (mutex_lock_interruptible(&fan_mutex))
5025 return -ERESTARTSYS;
5026
5027 rc = 0;
5028 switch (fan_control_access_mode) {
5029 case TPACPI_FAN_WR_ACPI_FANS:
5030 case TPACPI_FAN_WR_TPEC:
5031 if (!acpi_ec_write(fan_status_offset, 0x00))
5032 rc = -EIO;
5033 else {
5034 fan_control_desired_level = 0;
5035 tp_features.fan_ctrl_status_undef = 0;
5036 }
5037 break;
5038
5039 case TPACPI_FAN_WR_ACPI_SFAN:
5040 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5041 rc = -EIO;
5042 else
5043 fan_control_desired_level = 0;
5044 break;
5045
5046 default:
5047 rc = -ENXIO;
5048 }
5049
5050
5051 mutex_unlock(&fan_mutex);
5052 return rc;
5053}
5054
5055static int fan_set_speed(int speed)
5056{
5057 int rc;
5058
5059 if (!fan_control_allowed)
5060 return -EPERM;
5061
5062 if (mutex_lock_interruptible(&fan_mutex))
5063 return -ERESTARTSYS;
5064
5065 rc = 0;
5066 switch (fan_control_access_mode) {
5067 case TPACPI_FAN_WR_ACPI_FANS:
5068 if (speed >= 0 && speed <= 65535) {
5069 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5070 speed, speed, speed))
5071 rc = -EIO;
5072 } else
5073 rc = -EINVAL;
5074 break;
5075
5076 default:
5077 rc = -ENXIO;
5078 }
5079
5080 mutex_unlock(&fan_mutex);
5081 return rc;
5082}
5083
5084static void fan_watchdog_reset(void)
5085{
5086 static int fan_watchdog_active;
5087
5088 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5089 return;
5090
5091 if (fan_watchdog_active)
5092 cancel_delayed_work(&fan_watchdog_task);
5093
5094 if (fan_watchdog_maxinterval > 0 &&
5095 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5096 fan_watchdog_active = 1;
5097 if (!schedule_delayed_work(&fan_watchdog_task,
5098 msecs_to_jiffies(fan_watchdog_maxinterval
5099 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005100 printk(TPACPI_ERR
5101 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005102 "watchdog will not trigger\n");
5103 }
5104 } else
5105 fan_watchdog_active = 0;
5106}
5107
5108static void fan_watchdog_fire(struct work_struct *ignored)
5109{
5110 int rc;
5111
5112 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5113 return;
5114
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005115 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005116 rc = fan_set_enable();
5117 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005118 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005119 "will try again later...\n", -rc);
5120 /* reschedule for later */
5121 fan_watchdog_reset();
5122 }
5123}
5124
5125/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005126 * SYSFS fan layout: hwmon compatible (device)
5127 *
5128 * pwm*_enable:
5129 * 0: "disengaged" mode
5130 * 1: manual mode
5131 * 2: native EC "auto" mode (recommended, hardware default)
5132 *
5133 * pwm*: set speed in manual mode, ignored otherwise.
5134 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5135 * interpolation.
5136 *
5137 * fan*_input: tachometer reading, RPM
5138 *
5139 *
5140 * SYSFS fan layout: extensions
5141 *
5142 * fan_watchdog (driver):
5143 * fan watchdog interval in seconds, 0 disables (default), max 120
5144 */
5145
5146/* sysfs fan pwm1_enable ----------------------------------------------- */
5147static ssize_t fan_pwm1_enable_show(struct device *dev,
5148 struct device_attribute *attr,
5149 char *buf)
5150{
5151 int res, mode;
5152 u8 status;
5153
5154 res = fan_get_status_safe(&status);
5155 if (res)
5156 return res;
5157
5158 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5159 if (status != fan_control_initial_status) {
5160 tp_features.fan_ctrl_status_undef = 0;
5161 } else {
5162 /* Return most likely status. In fact, it
5163 * might be the only possible status */
5164 status = TP_EC_FAN_AUTO;
5165 }
5166 }
5167
5168 if (status & TP_EC_FAN_FULLSPEED) {
5169 mode = 0;
5170 } else if (status & TP_EC_FAN_AUTO) {
5171 mode = 2;
5172 } else
5173 mode = 1;
5174
5175 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5176}
5177
5178static ssize_t fan_pwm1_enable_store(struct device *dev,
5179 struct device_attribute *attr,
5180 const char *buf, size_t count)
5181{
5182 unsigned long t;
5183 int res, level;
5184
5185 if (parse_strtoul(buf, 2, &t))
5186 return -EINVAL;
5187
5188 switch (t) {
5189 case 0:
5190 level = TP_EC_FAN_FULLSPEED;
5191 break;
5192 case 1:
5193 level = TPACPI_FAN_LAST_LEVEL;
5194 break;
5195 case 2:
5196 level = TP_EC_FAN_AUTO;
5197 break;
5198 case 3:
5199 /* reserved for software-controlled auto mode */
5200 return -ENOSYS;
5201 default:
5202 return -EINVAL;
5203 }
5204
5205 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005206 if (res == -ENXIO)
5207 return -EINVAL;
5208 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005209 return res;
5210
5211 fan_watchdog_reset();
5212
5213 return count;
5214}
5215
5216static struct device_attribute dev_attr_fan_pwm1_enable =
5217 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5218 fan_pwm1_enable_show, fan_pwm1_enable_store);
5219
5220/* sysfs fan pwm1 ------------------------------------------------------ */
5221static ssize_t fan_pwm1_show(struct device *dev,
5222 struct device_attribute *attr,
5223 char *buf)
5224{
5225 int res;
5226 u8 status;
5227
5228 res = fan_get_status_safe(&status);
5229 if (res)
5230 return res;
5231
5232 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5233 if (status != fan_control_initial_status) {
5234 tp_features.fan_ctrl_status_undef = 0;
5235 } else {
5236 status = TP_EC_FAN_AUTO;
5237 }
5238 }
5239
5240 if ((status &
5241 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5242 status = fan_control_desired_level;
5243
5244 if (status > 7)
5245 status = 7;
5246
5247 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5248}
5249
5250static ssize_t fan_pwm1_store(struct device *dev,
5251 struct device_attribute *attr,
5252 const char *buf, size_t count)
5253{
5254 unsigned long s;
5255 int rc;
5256 u8 status, newlevel;
5257
5258 if (parse_strtoul(buf, 255, &s))
5259 return -EINVAL;
5260
5261 /* scale down from 0-255 to 0-7 */
5262 newlevel = (s >> 5) & 0x07;
5263
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005264 if (mutex_lock_interruptible(&fan_mutex))
5265 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005266
5267 rc = fan_get_status(&status);
5268 if (!rc && (status &
5269 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5270 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005271 if (rc == -ENXIO)
5272 rc = -EINVAL;
5273 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005274 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005275 fan_watchdog_reset();
5276 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005277 }
5278
5279 mutex_unlock(&fan_mutex);
5280 return (rc)? rc : count;
5281}
5282
5283static struct device_attribute dev_attr_fan_pwm1 =
5284 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5285 fan_pwm1_show, fan_pwm1_store);
5286
5287/* sysfs fan fan1_input ------------------------------------------------ */
5288static ssize_t fan_fan1_input_show(struct device *dev,
5289 struct device_attribute *attr,
5290 char *buf)
5291{
5292 int res;
5293 unsigned int speed;
5294
5295 res = fan_get_speed(&speed);
5296 if (res < 0)
5297 return res;
5298
5299 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5300}
5301
5302static struct device_attribute dev_attr_fan_fan1_input =
5303 __ATTR(fan1_input, S_IRUGO,
5304 fan_fan1_input_show, NULL);
5305
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005306/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005307static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5308 char *buf)
5309{
5310 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5311}
5312
5313static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5314 const char *buf, size_t count)
5315{
5316 unsigned long t;
5317
5318 if (parse_strtoul(buf, 120, &t))
5319 return -EINVAL;
5320
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005321 if (!fan_control_allowed)
5322 return -EPERM;
5323
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005324 fan_watchdog_maxinterval = t;
5325 fan_watchdog_reset();
5326
5327 return count;
5328}
5329
5330static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5331 fan_fan_watchdog_show, fan_fan_watchdog_store);
5332
5333/* --------------------------------------------------------------------- */
5334static struct attribute *fan_attributes[] = {
5335 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5336 &dev_attr_fan_fan1_input.attr,
5337 NULL
5338};
5339
5340static const struct attribute_group fan_attr_group = {
5341 .attrs = fan_attributes,
5342};
5343
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005344static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005345{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005346 int rc;
5347
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005348 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5349
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005350 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005351 fan_status_access_mode = TPACPI_FAN_NONE;
5352 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005353 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005354 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005355 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005356 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005357
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005358 TPACPI_ACPIHANDLE_INIT(fans);
5359 TPACPI_ACPIHANDLE_INIT(gfan);
5360 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005361
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005362 if (gfan_handle) {
5363 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005364 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005365 } else {
5366 /* all other ThinkPads: note that even old-style
5367 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005368 if (likely(acpi_ec_read(fan_status_offset,
5369 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005370 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005371
5372 /* In some ThinkPads, neither the EC nor the ACPI
5373 * DSDT initialize the fan status, and it ends up
5374 * being set to 0x07 when it *could* be either
5375 * 0x07 or 0x80.
5376 *
5377 * Enable for TP-1Y (T43), TP-78 (R51e),
5378 * TP-76 (R52), TP-70 (T43, R52), which are known
5379 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005380 if (fan_control_initial_status == 0x07) {
5381 switch (thinkpad_id.ec_model) {
5382 case 0x5931: /* TP-1Y */
5383 case 0x3837: /* TP-78 */
5384 case 0x3637: /* TP-76 */
5385 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005386 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005387 "fan_init: initial fan status "
5388 "is unknown, assuming it is "
5389 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005390 tp_features.fan_ctrl_status_undef = 1;
5391 ;;
5392 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005393 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005394 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005395 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005396 "ThinkPad ACPI EC access misbehaving, "
5397 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005398 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005399 }
5400 }
5401
5402 if (sfan_handle) {
5403 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005404 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005405 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005406 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005407 } else {
5408 if (!gfan_handle) {
5409 /* gfan without sfan means no fan control */
5410 /* all other models implement TP EC 0x2f control */
5411
5412 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005413 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005414 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005415 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005416 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005417 TPACPI_FAN_CMD_SPEED |
5418 TPACPI_FAN_CMD_LEVEL |
5419 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005420 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005421 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005422 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005423 TPACPI_FAN_CMD_LEVEL |
5424 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005425 }
5426 }
5427 }
5428
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005429 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5430 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5431 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5432 fan_status_access_mode, fan_control_access_mode);
5433
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005434 /* fan control master switch */
5435 if (!fan_control_allowed) {
5436 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5437 fan_control_commands = 0;
5438 dbg_printk(TPACPI_DBG_INIT,
5439 "fan control features disabled by parameter\n");
5440 }
5441
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005442 /* update fan_control_desired_level */
5443 if (fan_status_access_mode != TPACPI_FAN_NONE)
5444 fan_get_status_safe(NULL);
5445
5446 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5447 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005448 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005449 &fan_attr_group);
5450 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005451 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005452 &driver_attr_fan_watchdog);
5453 if (rc < 0)
5454 return rc;
5455 return 0;
5456 } else
5457 return 1;
5458}
5459
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005460static void fan_exit(void)
5461{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005462 vdbg_printk(TPACPI_DBG_EXIT,
5463 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005464
5465 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005466 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005467 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5468 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005469
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005470 cancel_delayed_work(&fan_watchdog_task);
5471 flush_scheduled_work();
5472}
5473
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005474static int fan_read(char *p)
5475{
5476 int len = 0;
5477 int rc;
5478 u8 status;
5479 unsigned int speed = 0;
5480
5481 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005482 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005483 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005484 rc = fan_get_status_safe(&status);
5485 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005486 return rc;
5487
5488 len += sprintf(p + len, "status:\t\t%s\n"
5489 "level:\t\t%d\n",
5490 (status != 0) ? "enabled" : "disabled", status);
5491 break;
5492
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005493 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005494 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005495 rc = fan_get_status_safe(&status);
5496 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005497 return rc;
5498
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005499 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005500 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005501 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005502 else
5503 /* Return most likely status. In fact, it
5504 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005505 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005506 }
5507
5508 len += sprintf(p + len, "status:\t\t%s\n",
5509 (status != 0) ? "enabled" : "disabled");
5510
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005511 rc = fan_get_speed(&speed);
5512 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005513 return rc;
5514
5515 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5516
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005517 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005518 /* Disengaged mode takes precedence */
5519 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005520 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005521 len += sprintf(p + len, "level:\t\tauto\n");
5522 else
5523 len += sprintf(p + len, "level:\t\t%d\n", status);
5524 break;
5525
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005526 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005527 default:
5528 len += sprintf(p + len, "status:\t\tnot supported\n");
5529 }
5530
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005531 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005532 len += sprintf(p + len, "commands:\tlevel <level>");
5533
5534 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005535 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005536 len += sprintf(p + len, " (<level> is 0-7)\n");
5537 break;
5538
5539 default:
5540 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005541 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005542 break;
5543 }
5544 }
5545
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005546 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005547 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005548 "commands:\twatchdog <timeout> (<timeout> "
5549 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005550
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005551 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005552 len += sprintf(p + len, "commands:\tspeed <speed>"
5553 " (<speed> is 0-65535)\n");
5554
5555 return len;
5556}
5557
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005558static int fan_write_cmd_level(const char *cmd, int *rc)
5559{
5560 int level;
5561
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005562 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005563 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005564 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005565 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005566 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005567 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005568 return 0;
5569
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005570 *rc = fan_set_level_safe(level);
5571 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005572 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005573 "access mode %d", fan_control_access_mode);
5574
5575 return 1;
5576}
5577
5578static int fan_write_cmd_enable(const char *cmd, int *rc)
5579{
5580 if (strlencmp(cmd, "enable") != 0)
5581 return 0;
5582
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005583 *rc = fan_set_enable();
5584 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005585 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005586 "access mode %d", fan_control_access_mode);
5587
5588 return 1;
5589}
5590
5591static int fan_write_cmd_disable(const char *cmd, int *rc)
5592{
5593 if (strlencmp(cmd, "disable") != 0)
5594 return 0;
5595
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005596 *rc = fan_set_disable();
5597 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005598 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005599 "access mode %d", fan_control_access_mode);
5600
5601 return 1;
5602}
5603
5604static int fan_write_cmd_speed(const char *cmd, int *rc)
5605{
5606 int speed;
5607
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005608 /* TODO:
5609 * Support speed <low> <medium> <high> ? */
5610
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005611 if (sscanf(cmd, "speed %d", &speed) != 1)
5612 return 0;
5613
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005614 *rc = fan_set_speed(speed);
5615 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005616 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005617 "access mode %d", fan_control_access_mode);
5618
5619 return 1;
5620}
5621
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005622static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5623{
5624 int interval;
5625
5626 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5627 return 0;
5628
5629 if (interval < 0 || interval > 120)
5630 *rc = -EINVAL;
5631 else
5632 fan_watchdog_maxinterval = interval;
5633
5634 return 1;
5635}
5636
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005637static int fan_write(char *buf)
5638{
5639 char *cmd;
5640 int rc = 0;
5641
5642 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005643 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005644 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005645 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005646 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005647 fan_write_cmd_disable(cmd, &rc) ||
5648 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005649 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005650 fan_write_cmd_speed(cmd, &rc))
5651 )
5652 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005653 else if (!rc)
5654 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005655 }
5656
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005657 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005658}
5659
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005660static struct ibm_struct fan_driver_data = {
5661 .name = "fan",
5662 .read = fan_read,
5663 .write = fan_write,
5664 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005665};
5666
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005667/****************************************************************************
5668 ****************************************************************************
5669 *
5670 * Infrastructure
5671 *
5672 ****************************************************************************
5673 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005675/* sysfs name ---------------------------------------------------------- */
5676static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5677 struct device_attribute *attr,
5678 char *buf)
5679{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005680 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005681}
5682
5683static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5684 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5685
5686/* --------------------------------------------------------------------- */
5687
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005688/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005689static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005690
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005691/*
5692 * Module and infrastructure proble, init and exit handling
5693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005695static int force_load;
5696
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005697#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005698static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005699{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005700 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005701
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005702 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005703}
5704#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5705
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005706static void ibm_exit(struct ibm_struct *ibm)
5707{
5708 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5709
5710 list_del_init(&ibm->all_drivers);
5711
5712 if (ibm->flags.acpi_notify_installed) {
5713 dbg_printk(TPACPI_DBG_EXIT,
5714 "%s: acpi_remove_notify_handler\n", ibm->name);
5715 BUG_ON(!ibm->acpi);
5716 acpi_remove_notify_handler(*ibm->acpi->handle,
5717 ibm->acpi->type,
5718 dispatch_acpi_notify);
5719 ibm->flags.acpi_notify_installed = 0;
5720 ibm->flags.acpi_notify_installed = 0;
5721 }
5722
5723 if (ibm->flags.proc_created) {
5724 dbg_printk(TPACPI_DBG_EXIT,
5725 "%s: remove_proc_entry\n", ibm->name);
5726 remove_proc_entry(ibm->name, proc_dir);
5727 ibm->flags.proc_created = 0;
5728 }
5729
5730 if (ibm->flags.acpi_driver_registered) {
5731 dbg_printk(TPACPI_DBG_EXIT,
5732 "%s: acpi_bus_unregister_driver\n", ibm->name);
5733 BUG_ON(!ibm->acpi);
5734 acpi_bus_unregister_driver(ibm->acpi->driver);
5735 kfree(ibm->acpi->driver);
5736 ibm->acpi->driver = NULL;
5737 ibm->flags.acpi_driver_registered = 0;
5738 }
5739
5740 if (ibm->flags.init_called && ibm->exit) {
5741 ibm->exit();
5742 ibm->flags.init_called = 0;
5743 }
5744
5745 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5746}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005747
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005748static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749{
5750 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005751 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 struct proc_dir_entry *entry;
5753
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005754 BUG_ON(ibm == NULL);
5755
5756 INIT_LIST_HEAD(&ibm->all_drivers);
5757
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005758 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 return 0;
5760
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005761 dbg_printk(TPACPI_DBG_INIT,
5762 "probing for %s\n", ibm->name);
5763
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005764 if (iibm->init) {
5765 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005766 if (ret > 0)
5767 return 0; /* probe failed */
5768 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005770
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005771 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 }
5773
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005774 if (ibm->acpi) {
5775 if (ibm->acpi->hid) {
5776 ret = register_tpacpi_subdriver(ibm);
5777 if (ret)
5778 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005779 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005780
5781 if (ibm->acpi->notify) {
5782 ret = setup_acpi_notify(ibm);
5783 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005784 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005785 ibm->name);
5786 ret = 0;
5787 goto err_out;
5788 }
5789 if (ret < 0)
5790 goto err_out;
5791 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005792 }
5793
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005794 dbg_printk(TPACPI_DBG_INIT,
5795 "%s installed\n", ibm->name);
5796
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005797 if (ibm->read) {
5798 entry = create_proc_entry(ibm->name,
5799 S_IFREG | S_IRUGO | S_IWUSR,
5800 proc_dir);
5801 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005802 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005803 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005804 ret = -ENODEV;
5805 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005806 }
5807 entry->owner = THIS_MODULE;
5808 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005809 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005810 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005811 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005812 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005815 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5816
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005818
5819err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005820 dbg_printk(TPACPI_DBG_INIT,
5821 "%s: at error exit path with result %d\n",
5822 ibm->name, ret);
5823
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005824 ibm_exit(ibm);
5825 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
5827
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005828/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005830static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005831{
Jeff Garzik18552562007-10-03 15:15:40 -04005832 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005833 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005834
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005835 if (!tp)
5836 return;
5837
5838 memset(tp, 0, sizeof(*tp));
5839
5840 if (dmi_name_in_vendors("IBM"))
5841 tp->vendor = PCI_VENDOR_ID_IBM;
5842 else if (dmi_name_in_vendors("LENOVO"))
5843 tp->vendor = PCI_VENDOR_ID_LENOVO;
5844 else
5845 return;
5846
5847 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5848 GFP_KERNEL);
5849 if (!tp->bios_version_str)
5850 return;
5851 tp->bios_model = tp->bios_version_str[0]
5852 | (tp->bios_version_str[1] << 8);
5853
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005854 /*
5855 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5856 * X32 or newer, all Z series; Some models must have an
5857 * up-to-date BIOS or they will not be detected.
5858 *
5859 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5860 */
5861 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005862 if (sscanf(dev->name,
5863 "IBM ThinkPad Embedded Controller -[%17c",
5864 ec_fw_string) == 1) {
5865 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5866 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005867
5868 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5869 tp->ec_model = ec_fw_string[0]
5870 | (ec_fw_string[1] << 8);
5871 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005872 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005873 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005874
5875 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5876 GFP_KERNEL);
5877 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5878 kfree(tp->model_str);
5879 tp->model_str = NULL;
5880 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03005881
5882 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
5883 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884}
5885
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005886static int __init probe_for_thinkpad(void)
5887{
5888 int is_thinkpad;
5889
5890 if (acpi_disabled)
5891 return -ENODEV;
5892
5893 /*
5894 * Non-ancient models have better DMI tagging, but very old models
5895 * don't.
5896 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005897 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005898
5899 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005900 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005901 if (!ec_handle) {
5902 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005903 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005904 "Not yet supported ThinkPad detected!\n");
5905 return -ENODEV;
5906 }
5907
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005908 /*
5909 * Risks a regression on very old machines, but reduces potential
5910 * false positives a damn great deal
5911 */
5912 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005913 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005914
5915 if (!is_thinkpad && !force_load)
5916 return -ENODEV;
5917
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005918 return 0;
5919}
5920
5921
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005922/* Module init, exit, parameters */
5923
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005924static struct ibm_init_struct ibms_init[] __initdata = {
5925 {
5926 .init = thinkpad_acpi_driver_init,
5927 .data = &thinkpad_acpi_driver_data,
5928 },
5929 {
5930 .init = hotkey_init,
5931 .data = &hotkey_driver_data,
5932 },
5933 {
5934 .init = bluetooth_init,
5935 .data = &bluetooth_driver_data,
5936 },
5937 {
5938 .init = wan_init,
5939 .data = &wan_driver_data,
5940 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005941#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005942 {
5943 .init = video_init,
5944 .data = &video_driver_data,
5945 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005946#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005947 {
5948 .init = light_init,
5949 .data = &light_driver_data,
5950 },
5951#ifdef CONFIG_THINKPAD_ACPI_DOCK
5952 {
5953 .init = dock_init,
5954 .data = &dock_driver_data[0],
5955 },
5956 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005957 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005958 .data = &dock_driver_data[1],
5959 },
5960#endif
5961#ifdef CONFIG_THINKPAD_ACPI_BAY
5962 {
5963 .init = bay_init,
5964 .data = &bay_driver_data,
5965 },
5966#endif
5967 {
5968 .init = cmos_init,
5969 .data = &cmos_driver_data,
5970 },
5971 {
5972 .init = led_init,
5973 .data = &led_driver_data,
5974 },
5975 {
5976 .init = beep_init,
5977 .data = &beep_driver_data,
5978 },
5979 {
5980 .init = thermal_init,
5981 .data = &thermal_driver_data,
5982 },
5983 {
5984 .data = &ecdump_driver_data,
5985 },
5986 {
5987 .init = brightness_init,
5988 .data = &brightness_driver_data,
5989 },
5990 {
5991 .data = &volume_driver_data,
5992 },
5993 {
5994 .init = fan_init,
5995 .data = &fan_driver_data,
5996 },
5997};
5998
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005999static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6000{
6001 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006002 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006003
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006004 if (!kp || !kp->name || !val)
6005 return -EINVAL;
6006
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006007 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6008 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006009 WARN_ON(ibm == NULL);
6010
6011 if (!ibm || !ibm->name)
6012 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006013
6014 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6015 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006016 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006017 strcpy(ibms_init[i].param, val);
6018 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006019 return 0;
6020 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006021 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006022
6023 return -EINVAL;
6024}
6025
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006026module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006027MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006028 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006029
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006030module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006031MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006032
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006033module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006034MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006035 "Attempts to load the driver even on a "
6036 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006037
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006038module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006039MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006040 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006041
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006042module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006043MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006044 "Selects brightness control strategy: "
6045 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006046
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006047module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006048MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006049 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006050
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006051module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006052MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006053 "used for backwards compatibility with userspace, "
6054 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006055
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006056#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006057 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006058 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006059 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006060
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006061TPACPI_PARAM(hotkey);
6062TPACPI_PARAM(bluetooth);
6063TPACPI_PARAM(video);
6064TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006065#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006066TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006067#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006068#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006069TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006070#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006071TPACPI_PARAM(cmos);
6072TPACPI_PARAM(led);
6073TPACPI_PARAM(beep);
6074TPACPI_PARAM(ecdump);
6075TPACPI_PARAM(brightness);
6076TPACPI_PARAM(volume);
6077TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006078
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006079static void thinkpad_acpi_module_exit(void)
6080{
6081 struct ibm_struct *ibm, *itmp;
6082
6083 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6084
6085 list_for_each_entry_safe_reverse(ibm, itmp,
6086 &tpacpi_all_drivers,
6087 all_drivers) {
6088 ibm_exit(ibm);
6089 }
6090
6091 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6092
6093 if (tpacpi_inputdev) {
6094 if (tp_features.input_device_registered)
6095 input_unregister_device(tpacpi_inputdev);
6096 else
6097 input_free_device(tpacpi_inputdev);
6098 }
6099
6100 if (tpacpi_hwmon)
6101 hwmon_device_unregister(tpacpi_hwmon);
6102
6103 if (tp_features.sensors_pdev_attrs_registered)
6104 device_remove_file(&tpacpi_sensors_pdev->dev,
6105 &dev_attr_thinkpad_acpi_pdev_name);
6106 if (tpacpi_sensors_pdev)
6107 platform_device_unregister(tpacpi_sensors_pdev);
6108 if (tpacpi_pdev)
6109 platform_device_unregister(tpacpi_pdev);
6110
6111 if (tp_features.sensors_pdrv_attrs_registered)
6112 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6113 if (tp_features.platform_drv_attrs_registered)
6114 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6115
6116 if (tp_features.sensors_pdrv_registered)
6117 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6118
6119 if (tp_features.platform_drv_registered)
6120 platform_driver_unregister(&tpacpi_pdriver);
6121
6122 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006123 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006124
6125 kfree(thinkpad_id.bios_version_str);
6126 kfree(thinkpad_id.ec_version_str);
6127 kfree(thinkpad_id.model_str);
6128}
6129
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006130
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006131static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132{
6133 int ret, i;
6134
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006135 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6136
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006137 /* Parameter checking */
6138 if (hotkey_report_mode > 2)
6139 return -EINVAL;
6140
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006141 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006142
6143 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006144 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006145 if (ret) {
6146 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006147 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006150 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006151
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006152 TPACPI_ACPIHANDLE_INIT(ecrd);
6153 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006155 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006157 printk(TPACPI_ERR
6158 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006159 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 return -ENODEV;
6161 }
6162 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006163
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006164 ret = platform_driver_register(&tpacpi_pdriver);
6165 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006166 printk(TPACPI_ERR
6167 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006168 thinkpad_acpi_module_exit();
6169 return ret;
6170 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006171 tp_features.platform_drv_registered = 1;
6172
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006173 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6174 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006175 printk(TPACPI_ERR
6176 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006177 thinkpad_acpi_module_exit();
6178 return ret;
6179 }
6180 tp_features.sensors_pdrv_registered = 1;
6181
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006182 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006183 if (!ret) {
6184 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006185 ret = tpacpi_create_driver_attributes(
6186 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006187 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006188 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006189 printk(TPACPI_ERR
6190 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006191 thinkpad_acpi_module_exit();
6192 return ret;
6193 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006194 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006195
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006196
6197 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006198 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006199 NULL, 0);
6200 if (IS_ERR(tpacpi_pdev)) {
6201 ret = PTR_ERR(tpacpi_pdev);
6202 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006203 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006204 thinkpad_acpi_module_exit();
6205 return ret;
6206 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006207 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006208 TPACPI_HWMON_DRVR_NAME,
6209 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006210 if (IS_ERR(tpacpi_sensors_pdev)) {
6211 ret = PTR_ERR(tpacpi_sensors_pdev);
6212 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006213 printk(TPACPI_ERR
6214 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006215 thinkpad_acpi_module_exit();
6216 return ret;
6217 }
6218 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6219 &dev_attr_thinkpad_acpi_pdev_name);
6220 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006221 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006222 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006223 thinkpad_acpi_module_exit();
6224 return ret;
6225 }
6226 tp_features.sensors_pdev_attrs_registered = 1;
6227 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006228 if (IS_ERR(tpacpi_hwmon)) {
6229 ret = PTR_ERR(tpacpi_hwmon);
6230 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006231 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006232 thinkpad_acpi_module_exit();
6233 return ret;
6234 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006235 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006236 tpacpi_inputdev = input_allocate_device();
6237 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006238 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006239 thinkpad_acpi_module_exit();
6240 return -ENOMEM;
6241 } else {
6242 /* Prepare input device, but don't register */
6243 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006244 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006245 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006246 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6247 thinkpad_id.vendor :
6248 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006249 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6250 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6251 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006252 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6253 ret = ibm_init(&ibms_init[i]);
6254 if (ret >= 0 && *ibms_init[i].param)
6255 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006257 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 return ret;
6259 }
6260 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006261 ret = input_register_device(tpacpi_inputdev);
6262 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006263 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006264 thinkpad_acpi_module_exit();
6265 return ret;
6266 } else {
6267 tp_features.input_device_registered = 1;
6268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006270 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 return 0;
6272}
6273
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006274/* Please remove this in year 2009 */
6275MODULE_ALIAS("ibm_acpi");
6276
6277/*
6278 * DMI matching for module autoloading
6279 *
6280 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6281 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6282 *
6283 * Only models listed in thinkwiki will be supported, so add yours
6284 * if it is not there yet.
6285 */
6286#define IBM_BIOS_MODULE_ALIAS(__type) \
6287 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6288
6289/* Non-ancient thinkpads */
6290MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6291MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6292
6293/* Ancient thinkpad BIOSes have to be identified by
6294 * BIOS type or model number, and there are far less
6295 * BIOS types than model numbers... */
6296IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6297IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6298IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6299
6300MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006301MODULE_DESCRIPTION(TPACPI_DESC);
6302MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006303MODULE_LICENSE("GPL");
6304
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006305module_init(thinkpad_acpi_module_init);
6306module_exit(thinkpad_acpi_module_exit);