blob: cd263c518e06451b1eaa738a020cfa3484640476 [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
254 char *model_str;
255};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200256static struct thinkpad_id_data thinkpad_id;
257
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300258static enum {
259 TPACPI_LIFE_INIT = 0,
260 TPACPI_LIFE_RUNNING,
261 TPACPI_LIFE_EXITING,
262} tpacpi_lifecycle;
263
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200264static int experimental;
265static u32 dbg_level;
266
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300267/****************************************************************************
268 ****************************************************************************
269 *
270 * ACPI Helpers and device model
271 *
272 ****************************************************************************
273 ****************************************************************************/
274
275/*************************************************************************
276 * ACPI basic handles
277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300279static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200281#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 static acpi_handle object##_handle; \
283 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400284 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 static char *object##_paths[] = { paths }
286
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200287TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400288 "\\_SB.PCI.ISA.EC", /* 570 */
289 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
290 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
291 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
292 "\\_SB.PCI0.ICH3.EC0", /* R31 */
293 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300294 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200296TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
297TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200299TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
300 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400301 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
302 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300303 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400304
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200305TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400306 "^HKEY", /* R30, R31 */
307 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300308 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400309
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200310TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
311 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
312 "\\_SB.PCI0.VID0", /* 770e */
313 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
314 "\\_SB.PCI0.AGP.VID", /* all others */
315 ); /* R30, R31 */
316
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400317
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300318/*************************************************************************
319 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200320 */
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static int acpi_evalf(acpi_handle handle,
323 void *res, char *method, char *fmt, ...)
324{
325 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400326 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200327 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400328 struct acpi_buffer result, *resultp;
329 union acpi_object out_obj;
330 acpi_status status;
331 va_list ap;
332 char res_type;
333 int success;
334 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200337 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339 }
340
341 if (*fmt == 'q') {
342 quiet = 1;
343 fmt++;
344 } else
345 quiet = 0;
346
347 res_type = *(fmt++);
348
349 params.count = 0;
350 params.pointer = &in_objs[0];
351
352 va_start(ap, fmt);
353 while (*fmt) {
354 char c = *(fmt++);
355 switch (c) {
356 case 'd': /* int */
357 in_objs[params.count].integer.value = va_arg(ap, int);
358 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
359 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400360 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200362 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 "with invalid format character '%c'\n", c);
364 return 0;
365 }
366 }
367 va_end(ap);
368
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400369 if (res_type != 'v') {
370 result.length = sizeof(out_obj);
371 result.pointer = &out_obj;
372 resultp = &result;
373 } else
374 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400376 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400379 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (res)
381 *(int *)res = out_obj.integer.value;
382 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
383 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400384 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 success = status == AE_OK;
386 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400387 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200389 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 "with invalid format character '%c'\n", res_type);
391 return 0;
392 }
393
394 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200395 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 method, fmt0, status);
397
398 return success;
399}
400
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200401static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300402{
403 int v;
404
405 if (ecrd_handle) {
406 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
407 return 0;
408 *p = v;
409 } else {
410 if (ec_read(i, p) < 0)
411 return 0;
412 }
413
414 return 1;
415}
416
417static int acpi_ec_write(int i, u8 v)
418{
419 if (ecwr_handle) {
420 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
421 return 0;
422 } else {
423 if (ec_write(i, v) < 0)
424 return 0;
425 }
426
427 return 1;
428}
429
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200430#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300431static int _sta(acpi_handle handle)
432{
433 int status;
434
435 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
436 status = 0;
437
438 return status;
439}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200440#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300441
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300442static int issue_thinkpad_cmos_command(int cmos_cmd)
443{
444 if (!cmos_handle)
445 return -ENXIO;
446
447 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
448 return -EIO;
449
450 return 0;
451}
452
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300453/*************************************************************************
454 * ACPI device model
455 */
456
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200457#define TPACPI_ACPIHANDLE_INIT(object) \
458 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200459 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
460
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300461static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300462 acpi_handle *handle, acpi_handle parent,
463 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300464{
465 int i;
466 acpi_status status;
467
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300468 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
469 name);
470
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300471 for (i = 0; i < num_paths; i++) {
472 status = acpi_get_handle(parent, paths[i], handle);
473 if (ACPI_SUCCESS(status)) {
474 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300475 dbg_printk(TPACPI_DBG_INIT,
476 "Found ACPI handle %s for %s\n",
477 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300478 return;
479 }
480 }
481
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300482 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
483 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300484 *handle = NULL;
485}
486
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300487static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300488{
489 struct ibm_struct *ibm = data;
490
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300491 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
492 return;
493
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300494 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300495 return;
496
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300497 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300498}
499
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300500static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300501{
502 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300503 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300505 BUG_ON(!ibm->acpi);
506
507 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300508 return 0;
509
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300510 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300511 "setting up ACPI notify for %s\n", ibm->name);
512
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300513 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
514 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200515 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300516 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300517 return -ENODEV;
518 }
519
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300520 acpi_driver_data(ibm->acpi->device) = ibm;
521 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200522 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300523 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300524
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300525 status = acpi_install_notify_handler(*ibm->acpi->handle,
526 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300527 if (ACPI_FAILURE(status)) {
528 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200529 printk(TPACPI_NOTICE
530 "another device driver is already "
531 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300532 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200533 printk(TPACPI_ERR
534 "acpi_install_notify_handler(%s) failed: %d\n",
535 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536 }
537 return -ENODEV;
538 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300539 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300540 return 0;
541}
542
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300543static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300544{
545 return 0;
546}
547
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300548static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300549{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300550 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300551
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300552 dbg_printk(TPACPI_DBG_INIT,
553 "registering %s as an ACPI driver\n", ibm->name);
554
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300555 BUG_ON(!ibm->acpi);
556
557 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
558 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200559 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300560 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300561 }
562
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200563 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300564 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200565
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300566 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300567
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300568 rc = acpi_bus_register_driver(ibm->acpi->driver);
569 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200570 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200571 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300572 kfree(ibm->acpi->driver);
573 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300574 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300575 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300576
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300577 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300578}
579
580
581/****************************************************************************
582 ****************************************************************************
583 *
584 * Procfs Helpers
585 *
586 ****************************************************************************
587 ****************************************************************************/
588
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300589static int dispatch_procfs_read(char *page, char **start, off_t off,
590 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300591{
592 struct ibm_struct *ibm = data;
593 int len;
594
595 if (!ibm || !ibm->read)
596 return -EINVAL;
597
598 len = ibm->read(page);
599 if (len < 0)
600 return len;
601
602 if (len <= off + count)
603 *eof = 1;
604 *start = page + off;
605 len -= off;
606 if (len > count)
607 len = count;
608 if (len < 0)
609 len = 0;
610
611 return len;
612}
613
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300614static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200615 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300616 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300617{
618 struct ibm_struct *ibm = data;
619 char *kernbuf;
620 int ret;
621
622 if (!ibm || !ibm->write)
623 return -EINVAL;
624
625 kernbuf = kmalloc(count + 2, GFP_KERNEL);
626 if (!kernbuf)
627 return -ENOMEM;
628
629 if (copy_from_user(kernbuf, userbuf, count)) {
630 kfree(kernbuf);
631 return -EFAULT;
632 }
633
634 kernbuf[count] = 0;
635 strcat(kernbuf, ",");
636 ret = ibm->write(kernbuf);
637 if (ret == 0)
638 ret = count;
639
640 kfree(kernbuf);
641
642 return ret;
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645static char *next_cmd(char **cmds)
646{
647 char *start = *cmds;
648 char *end;
649
650 while ((end = strchr(start, ',')) && end == start)
651 start = end + 1;
652
653 if (!end)
654 return NULL;
655
656 *end = 0;
657 *cmds = end + 1;
658 return start;
659}
660
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300661
662/****************************************************************************
663 ****************************************************************************
664 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300665 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300666 *
667 ****************************************************************************
668 ****************************************************************************/
669
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300670static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300671static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700672static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300673static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300674static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200675static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300676
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200677static int tpacpi_suspend_handler(struct platform_device *pdev,
678 pm_message_t state)
679{
680 struct ibm_struct *ibm, *itmp;
681
682 list_for_each_entry_safe(ibm, itmp,
683 &tpacpi_all_drivers,
684 all_drivers) {
685 if (ibm->suspend)
686 (ibm->suspend)(state);
687 }
688
689 return 0;
690}
691
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300692static int tpacpi_resume_handler(struct platform_device *pdev)
693{
694 struct ibm_struct *ibm, *itmp;
695
696 list_for_each_entry_safe(ibm, itmp,
697 &tpacpi_all_drivers,
698 all_drivers) {
699 if (ibm->resume)
700 (ibm->resume)();
701 }
702
703 return 0;
704}
705
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300706static struct platform_driver tpacpi_pdriver = {
707 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200708 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300709 .owner = THIS_MODULE,
710 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200711 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300712 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300713};
714
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300715static struct platform_driver tpacpi_hwmon_pdriver = {
716 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200717 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300718 .owner = THIS_MODULE,
719 },
720};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300721
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300722/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300723 * sysfs support helpers
724 */
725
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200726struct attribute_set {
727 unsigned int members, max_members;
728 struct attribute_group group;
729};
730
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300731struct attribute_set_obj {
732 struct attribute_set s;
733 struct attribute *a;
734} __attribute__((packed));
735
736static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200737 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300738{
739 struct attribute_set_obj *sobj;
740
741 if (max_members == 0)
742 return NULL;
743
744 /* Allocates space for implicit NULL at the end too */
745 sobj = kzalloc(sizeof(struct attribute_set_obj) +
746 max_members * sizeof(struct attribute *),
747 GFP_KERNEL);
748 if (!sobj)
749 return NULL;
750 sobj->s.max_members = max_members;
751 sobj->s.group.attrs = &sobj->a;
752 sobj->s.group.name = name;
753
754 return &sobj->s;
755}
756
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200757#define destroy_attr_set(_set) \
758 kfree(_set);
759
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300760/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200761static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300762{
763 if (!s || !attr)
764 return -EINVAL;
765
766 if (s->members >= s->max_members)
767 return -ENOMEM;
768
769 s->group.attrs[s->members] = attr;
770 s->members++;
771
772 return 0;
773}
774
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200775static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300776 struct attribute **attr,
777 unsigned int count)
778{
779 int i, res;
780
781 for (i = 0; i < count; i++) {
782 res = add_to_attr_set(s, attr[i]);
783 if (res)
784 return res;
785 }
786
787 return 0;
788}
789
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200790static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300791{
792 sysfs_remove_group(kobj, &s->group);
793 destroy_attr_set(s);
794}
795
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200796#define register_attr_set_with_sysfs(_attr_set, _kobj) \
797 sysfs_create_group(_kobj, &_attr_set->group)
798
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300799static int parse_strtoul(const char *buf,
800 unsigned long max, unsigned long *value)
801{
802 char *endp;
803
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300804 while (*buf && isspace(*buf))
805 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300806 *value = simple_strtoul(buf, &endp, 0);
807 while (*endp && isspace(*endp))
808 endp++;
809 if (*endp || *value > max)
810 return -EINVAL;
811
812 return 0;
813}
814
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300815static int __init tpacpi_query_bcl_levels(acpi_handle handle)
816{
817 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
818 union acpi_object *obj;
819 int rc;
820
821 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
822 obj = (union acpi_object *)buffer.pointer;
823 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
824 printk(TPACPI_ERR "Unknown _BCL data, "
825 "please report this to %s\n", TPACPI_MAIL);
826 rc = 0;
827 } else {
828 rc = obj->package.count;
829 }
830 } else {
831 return 0;
832 }
833
834 kfree(buffer.pointer);
835 return rc;
836}
837
838static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
839 u32 lvl, void *context, void **rv)
840{
841 char name[ACPI_PATH_SEGMENT_LENGTH];
842 struct acpi_buffer buffer = { sizeof(name), &name };
843
844 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
845 !strncmp("_BCL", name, sizeof(name) - 1)) {
846 BUG_ON(!rv || !*rv);
847 **(int **)rv = tpacpi_query_bcl_levels(handle);
848 return AE_CTRL_TERMINATE;
849 } else {
850 return AE_OK;
851 }
852}
853
854/*
855 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
856 */
857static int __init tpacpi_check_std_acpi_brightness_support(void)
858{
859 int status;
860 int bcl_levels = 0;
861 void *bcl_ptr = &bcl_levels;
862
863 if (!vid_handle) {
864 TPACPI_ACPIHANDLE_INIT(vid);
865 }
866 if (!vid_handle)
867 return 0;
868
869 /*
870 * Search for a _BCL method, and execute it. This is safe on all
871 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
872 * BIOS in ACPI backlight control mode. We do NOT have to care
873 * about calling the _BCL method in an enabled video device, any
874 * will do for our purposes.
875 */
876
877 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
878 tpacpi_acpi_walk_find_bcl, NULL,
879 &bcl_ptr);
880
881 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
882 tp_features.bright_acpimode = 1;
883 return (bcl_levels - 2);
884 }
885
886 return 0;
887}
888
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200889/*************************************************************************
890 * thinkpad-acpi driver attributes
891 */
892
893/* interface_version --------------------------------------------------- */
894static ssize_t tpacpi_driver_interface_version_show(
895 struct device_driver *drv,
896 char *buf)
897{
898 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
899}
900
901static DRIVER_ATTR(interface_version, S_IRUGO,
902 tpacpi_driver_interface_version_show, NULL);
903
904/* debug_level --------------------------------------------------------- */
905static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
906 char *buf)
907{
908 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
909}
910
911static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
912 const char *buf, size_t count)
913{
914 unsigned long t;
915
916 if (parse_strtoul(buf, 0xffff, &t))
917 return -EINVAL;
918
919 dbg_level = t;
920
921 return count;
922}
923
924static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
925 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
926
927/* version ------------------------------------------------------------- */
928static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
929 char *buf)
930{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200931 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
932 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200933}
934
935static DRIVER_ATTR(version, S_IRUGO,
936 tpacpi_driver_version_show, NULL);
937
938/* --------------------------------------------------------------------- */
939
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200940static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200941 &driver_attr_debug_level, &driver_attr_version,
942 &driver_attr_interface_version,
943};
944
945static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
946{
947 int i, res;
948
949 i = 0;
950 res = 0;
951 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
952 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
953 i++;
954 }
955
956 return res;
957}
958
959static void tpacpi_remove_driver_attributes(struct device_driver *drv)
960{
961 int i;
962
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200963 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200964 driver_remove_file(drv, tpacpi_driver_attributes[i]);
965}
966
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300967/****************************************************************************
968 ****************************************************************************
969 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300970 * Subdrivers
971 *
972 ****************************************************************************
973 ****************************************************************************/
974
975/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300976 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300977 */
978
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300979static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200981 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
982 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200984 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300985 (thinkpad_id.bios_version_str) ?
986 thinkpad_id.bios_version_str : "unknown",
987 (thinkpad_id.ec_version_str) ?
988 thinkpad_id.ec_version_str : "unknown");
989
990 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200991 printk(TPACPI_INFO "%s %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300992 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
993 "IBM" : ((thinkpad_id.vendor ==
994 PCI_VENDOR_ID_LENOVO) ?
995 "Lenovo" : "Unknown vendor"),
996 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 0;
999}
1000
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001001static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 int len = 0;
1004
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001005 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1006 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 return len;
1009}
1010
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001011static struct ibm_struct thinkpad_acpi_driver_data = {
1012 .name = "driver",
1013 .read = thinkpad_acpi_driver_read,
1014};
1015
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001016/*************************************************************************
1017 * Hotkey subdriver
1018 */
1019
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001020enum { /* hot key scan codes (derived from ACPI DSDT) */
1021 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1022 TP_ACPI_HOTKEYSCAN_FNF2,
1023 TP_ACPI_HOTKEYSCAN_FNF3,
1024 TP_ACPI_HOTKEYSCAN_FNF4,
1025 TP_ACPI_HOTKEYSCAN_FNF5,
1026 TP_ACPI_HOTKEYSCAN_FNF6,
1027 TP_ACPI_HOTKEYSCAN_FNF7,
1028 TP_ACPI_HOTKEYSCAN_FNF8,
1029 TP_ACPI_HOTKEYSCAN_FNF9,
1030 TP_ACPI_HOTKEYSCAN_FNF10,
1031 TP_ACPI_HOTKEYSCAN_FNF11,
1032 TP_ACPI_HOTKEYSCAN_FNF12,
1033 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1034 TP_ACPI_HOTKEYSCAN_FNINSERT,
1035 TP_ACPI_HOTKEYSCAN_FNDELETE,
1036 TP_ACPI_HOTKEYSCAN_FNHOME,
1037 TP_ACPI_HOTKEYSCAN_FNEND,
1038 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1039 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1040 TP_ACPI_HOTKEYSCAN_FNSPACE,
1041 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1042 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1043 TP_ACPI_HOTKEYSCAN_MUTE,
1044 TP_ACPI_HOTKEYSCAN_THINKPAD,
1045};
1046
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001047enum { /* Keys available through NVRAM polling */
1048 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1049 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1050};
1051
1052enum { /* Positions of some of the keys in hotkey masks */
1053 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1054 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1055 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1056 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1057 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1058 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1059 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1060 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1061 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1062 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1063 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1064};
1065
1066enum { /* NVRAM to ACPI HKEY group map */
1067 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1068 TP_ACPI_HKEY_ZOOM_MASK |
1069 TP_ACPI_HKEY_DISPSWTCH_MASK |
1070 TP_ACPI_HKEY_HIBERNATE_MASK,
1071 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1072 TP_ACPI_HKEY_BRGHTDWN_MASK,
1073 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1074 TP_ACPI_HKEY_VOLDWN_MASK |
1075 TP_ACPI_HKEY_MUTE_MASK,
1076};
1077
1078#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1079struct tp_nvram_state {
1080 u16 thinkpad_toggle:1;
1081 u16 zoom_toggle:1;
1082 u16 display_toggle:1;
1083 u16 thinklight_toggle:1;
1084 u16 hibernate_toggle:1;
1085 u16 displayexp_toggle:1;
1086 u16 display_state:1;
1087 u16 brightness_toggle:1;
1088 u16 volume_toggle:1;
1089 u16 mute:1;
1090
1091 u8 brightness_level;
1092 u8 volume_level;
1093};
1094
1095static struct task_struct *tpacpi_hotkey_task;
1096static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1097static int hotkey_poll_freq = 10; /* Hz */
1098static struct mutex hotkey_thread_mutex;
1099static struct mutex hotkey_thread_data_mutex;
1100static unsigned int hotkey_config_change;
1101
1102#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1103
1104#define hotkey_source_mask 0U
1105
1106#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1107
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001108static struct mutex hotkey_mutex;
1109
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001110static enum { /* Reasons for waking up */
1111 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1112 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1113 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1114} hotkey_wakeup_reason;
1115
1116static int hotkey_autosleep_ack;
1117
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001118static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001119static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001120static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001121static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001122static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001123
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001124static unsigned int hotkey_report_mode;
1125
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001126static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001127
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001128static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001129
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001130#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1131#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001132 do { \
1133 mutex_lock(&hotkey_thread_data_mutex); \
1134 hotkey_config_change++; \
1135 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001136#define HOTKEY_CONFIG_CRITICAL_END \
1137 mutex_unlock(&hotkey_thread_data_mutex);
1138#else
1139#define HOTKEY_CONFIG_CRITICAL_START
1140#define HOTKEY_CONFIG_CRITICAL_END
1141#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1142
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001143/* HKEY.MHKG() return bits */
1144#define TP_HOTKEY_TABLET_MASK (1 << 3)
1145
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001146static int hotkey_get_wlsw(int *status)
1147{
1148 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1149 return -EIO;
1150 return 0;
1151}
1152
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001153static int hotkey_get_tablet_mode(int *status)
1154{
1155 int s;
1156
1157 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1158 return -EIO;
1159
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001160 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1161 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001162}
1163
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001164/*
1165 * Call with hotkey_mutex held
1166 */
1167static int hotkey_mask_get(void)
1168{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001169 u32 m = 0;
1170
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001171 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001172 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001173 return -EIO;
1174 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001175 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001176
1177 return 0;
1178}
1179
1180/*
1181 * Call with hotkey_mutex held
1182 */
1183static int hotkey_mask_set(u32 mask)
1184{
1185 int i;
1186 int rc = 0;
1187
1188 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001189 if (!tp_warned.hotkey_mask_ff &&
1190 (mask == 0xffff || mask == 0xffffff ||
1191 mask == 0xffffffff)) {
1192 tp_warned.hotkey_mask_ff = 1;
1193 printk(TPACPI_NOTICE
1194 "setting the hotkey mask to 0x%08x is likely "
1195 "not the best way to go about it\n", mask);
1196 printk(TPACPI_NOTICE
1197 "please consider using the driver defaults, "
1198 "and refer to up-to-date thinkpad-acpi "
1199 "documentation\n");
1200 }
1201
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001202 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001203 for (i = 0; i < 32; i++) {
1204 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001205 /* enable in firmware mask only keys not in NVRAM
1206 * mode, but enable the key in the cached hotkey_mask
1207 * regardless of mode, or the key will end up
1208 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001209 if (!acpi_evalf(hkey_handle,
1210 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001211 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001212 rc = -EIO;
1213 break;
1214 } else {
1215 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1216 }
1217 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001218 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001219
1220 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001221 if (!hotkey_mask_get() && !rc &&
1222 (hotkey_mask & ~hotkey_source_mask) !=
1223 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001224 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001225 "requested hot key mask 0x%08x, but "
1226 "firmware forced it to 0x%08x\n",
1227 mask, hotkey_mask);
1228 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001229 } else {
1230#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1231 HOTKEY_CONFIG_CRITICAL_START
1232 hotkey_mask = mask & hotkey_source_mask;
1233 HOTKEY_CONFIG_CRITICAL_END
1234 hotkey_mask_get();
1235 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001236 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001237 "requested hot key mask 0x%08x, "
1238 "forced to 0x%08x (NVRAM poll mask is "
1239 "0x%08x): no firmware mask support\n",
1240 mask, hotkey_mask, hotkey_source_mask);
1241 }
1242#else
1243 hotkey_mask_get();
1244 rc = -ENXIO;
1245#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001246 }
1247
1248 return rc;
1249}
1250
1251static int hotkey_status_get(int *status)
1252{
1253 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1254 return -EIO;
1255
1256 return 0;
1257}
1258
1259static int hotkey_status_set(int status)
1260{
1261 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1262 return -EIO;
1263
1264 return 0;
1265}
1266
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001267static void tpacpi_input_send_radiosw(void)
1268{
1269 int wlsw;
1270
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001271 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001272 mutex_lock(&tpacpi_inputdev_send_mutex);
1273
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001274 input_report_switch(tpacpi_inputdev,
1275 SW_RADIO, !!wlsw);
1276 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001277
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001278 mutex_unlock(&tpacpi_inputdev_send_mutex);
1279 }
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001280}
1281
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001282static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001283{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001284 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001285
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001286 if (tp_features.hotkey_tablet &&
1287 !hotkey_get_tablet_mode(&state)) {
1288 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001289
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001290 input_report_switch(tpacpi_inputdev,
1291 SW_TABLET_MODE, !!state);
1292 input_sync(tpacpi_inputdev);
1293
1294 mutex_unlock(&tpacpi_inputdev_send_mutex);
1295 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001296}
1297
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001298static void tpacpi_input_send_key(unsigned int scancode)
1299{
1300 unsigned int keycode;
1301
1302 keycode = hotkey_keycode_map[scancode];
1303
1304 if (keycode != KEY_RESERVED) {
1305 mutex_lock(&tpacpi_inputdev_send_mutex);
1306
1307 input_report_key(tpacpi_inputdev, keycode, 1);
1308 if (keycode == KEY_UNKNOWN)
1309 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1310 scancode);
1311 input_sync(tpacpi_inputdev);
1312
1313 input_report_key(tpacpi_inputdev, keycode, 0);
1314 if (keycode == KEY_UNKNOWN)
1315 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1316 scancode);
1317 input_sync(tpacpi_inputdev);
1318
1319 mutex_unlock(&tpacpi_inputdev_send_mutex);
1320 }
1321}
1322
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001323#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1324static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1325
1326static void tpacpi_hotkey_send_key(unsigned int scancode)
1327{
1328 tpacpi_input_send_key(scancode);
1329 if (hotkey_report_mode < 2) {
1330 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1331 0x80, 0x1001 + scancode);
1332 }
1333}
1334
1335static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1336{
1337 u8 d;
1338
1339 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1340 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1341 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1342 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1343 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1344 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1345 }
1346 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1347 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1348 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1349 }
1350 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1351 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1352 n->displayexp_toggle =
1353 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1354 }
1355 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1356 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1357 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1358 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1359 n->brightness_toggle =
1360 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1361 }
1362 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1363 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1364 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1365 >> TP_NVRAM_POS_LEVEL_VOLUME;
1366 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1367 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1368 }
1369}
1370
1371#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001372 do { \
1373 if ((mask & (1 << __scancode)) && \
1374 oldn->__member != newn->__member) \
1375 tpacpi_hotkey_send_key(__scancode); \
1376 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001377
1378#define TPACPI_MAY_SEND_KEY(__scancode) \
1379 do { if (mask & (1 << __scancode)) \
1380 tpacpi_hotkey_send_key(__scancode); } while (0)
1381
1382static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001383 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001384 u32 mask)
1385{
1386 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1387 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1388 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1390
1391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1392
1393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1394
1395 /* handle volume */
1396 if (oldn->volume_toggle != newn->volume_toggle) {
1397 if (oldn->mute != newn->mute) {
1398 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1399 }
1400 if (oldn->volume_level > newn->volume_level) {
1401 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1402 } else if (oldn->volume_level < newn->volume_level) {
1403 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1404 } else if (oldn->mute == newn->mute) {
1405 /* repeated key presses that didn't change state */
1406 if (newn->mute) {
1407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1408 } else if (newn->volume_level != 0) {
1409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1410 } else {
1411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1412 }
1413 }
1414 }
1415
1416 /* handle brightness */
1417 if (oldn->brightness_toggle != newn->brightness_toggle) {
1418 if (oldn->brightness_level < newn->brightness_level) {
1419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1420 } else if (oldn->brightness_level > newn->brightness_level) {
1421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1422 } else {
1423 /* repeated key presses that didn't change state */
1424 if (newn->brightness_level != 0) {
1425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1426 } else {
1427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1428 }
1429 }
1430 }
1431}
1432
1433#undef TPACPI_COMPARE_KEY
1434#undef TPACPI_MAY_SEND_KEY
1435
1436static int hotkey_kthread(void *data)
1437{
1438 struct tp_nvram_state s[2];
1439 u32 mask;
1440 unsigned int si, so;
1441 unsigned long t;
1442 unsigned int change_detector, must_reset;
1443
1444 mutex_lock(&hotkey_thread_mutex);
1445
1446 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1447 goto exit;
1448
1449 set_freezable();
1450
1451 so = 0;
1452 si = 1;
1453 t = 0;
1454
1455 /* Initial state for compares */
1456 mutex_lock(&hotkey_thread_data_mutex);
1457 change_detector = hotkey_config_change;
1458 mask = hotkey_source_mask & hotkey_mask;
1459 mutex_unlock(&hotkey_thread_data_mutex);
1460 hotkey_read_nvram(&s[so], mask);
1461
1462 while (!kthread_should_stop() && hotkey_poll_freq) {
1463 if (t == 0)
1464 t = 1000/hotkey_poll_freq;
1465 t = msleep_interruptible(t);
1466 if (unlikely(kthread_should_stop()))
1467 break;
1468 must_reset = try_to_freeze();
1469 if (t > 0 && !must_reset)
1470 continue;
1471
1472 mutex_lock(&hotkey_thread_data_mutex);
1473 if (must_reset || hotkey_config_change != change_detector) {
1474 /* forget old state on thaw or config change */
1475 si = so;
1476 t = 0;
1477 change_detector = hotkey_config_change;
1478 }
1479 mask = hotkey_source_mask & hotkey_mask;
1480 mutex_unlock(&hotkey_thread_data_mutex);
1481
1482 if (likely(mask)) {
1483 hotkey_read_nvram(&s[si], mask);
1484 if (likely(si != so)) {
1485 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001486 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001487 }
1488 }
1489
1490 so = si;
1491 si ^= 1;
1492 }
1493
1494exit:
1495 mutex_unlock(&hotkey_thread_mutex);
1496 return 0;
1497}
1498
1499static void hotkey_poll_stop_sync(void)
1500{
1501 if (tpacpi_hotkey_task) {
1502 if (frozen(tpacpi_hotkey_task) ||
1503 freezing(tpacpi_hotkey_task))
1504 thaw_process(tpacpi_hotkey_task);
1505
1506 kthread_stop(tpacpi_hotkey_task);
1507 tpacpi_hotkey_task = NULL;
1508 mutex_lock(&hotkey_thread_mutex);
1509 /* at this point, the thread did exit */
1510 mutex_unlock(&hotkey_thread_mutex);
1511 }
1512}
1513
1514/* call with hotkey_mutex held */
1515static void hotkey_poll_setup(int may_warn)
1516{
1517 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1518 hotkey_poll_freq > 0 &&
1519 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1520 if (!tpacpi_hotkey_task) {
1521 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001522 NULL,
1523 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001524 if (IS_ERR(tpacpi_hotkey_task)) {
1525 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001526 printk(TPACPI_ERR
1527 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001528 "for hotkey polling\n");
1529 }
1530 }
1531 } else {
1532 hotkey_poll_stop_sync();
1533 if (may_warn &&
1534 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001535 printk(TPACPI_NOTICE
1536 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001537 "which is currently disabled\n",
1538 hotkey_source_mask);
1539 }
1540 }
1541}
1542
1543static void hotkey_poll_setup_safe(int may_warn)
1544{
1545 mutex_lock(&hotkey_mutex);
1546 hotkey_poll_setup(may_warn);
1547 mutex_unlock(&hotkey_mutex);
1548}
1549
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001550#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1551
1552static void hotkey_poll_setup_safe(int __unused)
1553{
1554}
1555
1556#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1557
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001558static int hotkey_inputdev_open(struct input_dev *dev)
1559{
1560 switch (tpacpi_lifecycle) {
1561 case TPACPI_LIFE_INIT:
1562 /*
1563 * hotkey_init will call hotkey_poll_setup_safe
1564 * at the appropriate moment
1565 */
1566 return 0;
1567 case TPACPI_LIFE_EXITING:
1568 return -EBUSY;
1569 case TPACPI_LIFE_RUNNING:
1570 hotkey_poll_setup_safe(0);
1571 return 0;
1572 }
1573
1574 /* Should only happen if tpacpi_lifecycle is corrupt */
1575 BUG();
1576 return -EBUSY;
1577}
1578
1579static void hotkey_inputdev_close(struct input_dev *dev)
1580{
1581 /* disable hotkey polling when possible */
1582 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1583 hotkey_poll_setup_safe(0);
1584}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001585
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001586/* sysfs hotkey enable ------------------------------------------------- */
1587static ssize_t hotkey_enable_show(struct device *dev,
1588 struct device_attribute *attr,
1589 char *buf)
1590{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001591 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001592
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001593 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001594 if (res)
1595 return res;
1596
1597 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1598}
1599
1600static ssize_t hotkey_enable_store(struct device *dev,
1601 struct device_attribute *attr,
1602 const char *buf, size_t count)
1603{
1604 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001605 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001606
1607 if (parse_strtoul(buf, 1, &t))
1608 return -EINVAL;
1609
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001610 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001611
1612 return (res) ? res : count;
1613}
1614
1615static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001616 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001617 hotkey_enable_show, hotkey_enable_store);
1618
1619/* sysfs hotkey mask --------------------------------------------------- */
1620static ssize_t hotkey_mask_show(struct device *dev,
1621 struct device_attribute *attr,
1622 char *buf)
1623{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001624 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001625
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001626 if (mutex_lock_interruptible(&hotkey_mutex))
1627 return -ERESTARTSYS;
1628 res = hotkey_mask_get();
1629 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001630
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001631 return (res)?
1632 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001633}
1634
1635static ssize_t hotkey_mask_store(struct device *dev,
1636 struct device_attribute *attr,
1637 const char *buf, size_t count)
1638{
1639 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001640 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001641
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001642 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001643 return -EINVAL;
1644
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001645 if (mutex_lock_interruptible(&hotkey_mutex))
1646 return -ERESTARTSYS;
1647
1648 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001649
1650#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1651 hotkey_poll_setup(1);
1652#endif
1653
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001654 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001655
1656 return (res) ? res : count;
1657}
1658
1659static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001660 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001661 hotkey_mask_show, hotkey_mask_store);
1662
1663/* sysfs hotkey bios_enabled ------------------------------------------- */
1664static ssize_t hotkey_bios_enabled_show(struct device *dev,
1665 struct device_attribute *attr,
1666 char *buf)
1667{
1668 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1669}
1670
1671static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001672 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001673
1674/* sysfs hotkey bios_mask ---------------------------------------------- */
1675static ssize_t hotkey_bios_mask_show(struct device *dev,
1676 struct device_attribute *attr,
1677 char *buf)
1678{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001679 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001680}
1681
1682static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001683 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001684
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001685/* sysfs hotkey all_mask ----------------------------------------------- */
1686static ssize_t hotkey_all_mask_show(struct device *dev,
1687 struct device_attribute *attr,
1688 char *buf)
1689{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001690 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1691 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001692}
1693
1694static struct device_attribute dev_attr_hotkey_all_mask =
1695 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1696
1697/* sysfs hotkey recommended_mask --------------------------------------- */
1698static ssize_t hotkey_recommended_mask_show(struct device *dev,
1699 struct device_attribute *attr,
1700 char *buf)
1701{
1702 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001703 (hotkey_all_mask | hotkey_source_mask)
1704 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001705}
1706
1707static struct device_attribute dev_attr_hotkey_recommended_mask =
1708 __ATTR(hotkey_recommended_mask, S_IRUGO,
1709 hotkey_recommended_mask_show, NULL);
1710
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001711#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1712
1713/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1714static ssize_t hotkey_source_mask_show(struct device *dev,
1715 struct device_attribute *attr,
1716 char *buf)
1717{
1718 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1719}
1720
1721static ssize_t hotkey_source_mask_store(struct device *dev,
1722 struct device_attribute *attr,
1723 const char *buf, size_t count)
1724{
1725 unsigned long t;
1726
1727 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1728 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1729 return -EINVAL;
1730
1731 if (mutex_lock_interruptible(&hotkey_mutex))
1732 return -ERESTARTSYS;
1733
1734 HOTKEY_CONFIG_CRITICAL_START
1735 hotkey_source_mask = t;
1736 HOTKEY_CONFIG_CRITICAL_END
1737
1738 hotkey_poll_setup(1);
1739
1740 mutex_unlock(&hotkey_mutex);
1741
1742 return count;
1743}
1744
1745static struct device_attribute dev_attr_hotkey_source_mask =
1746 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1747 hotkey_source_mask_show, hotkey_source_mask_store);
1748
1749/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1750static ssize_t hotkey_poll_freq_show(struct device *dev,
1751 struct device_attribute *attr,
1752 char *buf)
1753{
1754 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1755}
1756
1757static ssize_t hotkey_poll_freq_store(struct device *dev,
1758 struct device_attribute *attr,
1759 const char *buf, size_t count)
1760{
1761 unsigned long t;
1762
1763 if (parse_strtoul(buf, 25, &t))
1764 return -EINVAL;
1765
1766 if (mutex_lock_interruptible(&hotkey_mutex))
1767 return -ERESTARTSYS;
1768
1769 hotkey_poll_freq = t;
1770
1771 hotkey_poll_setup(1);
1772 mutex_unlock(&hotkey_mutex);
1773
1774 return count;
1775}
1776
1777static struct device_attribute dev_attr_hotkey_poll_freq =
1778 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1779 hotkey_poll_freq_show, hotkey_poll_freq_store);
1780
1781#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1782
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001783/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001784static ssize_t hotkey_radio_sw_show(struct device *dev,
1785 struct device_attribute *attr,
1786 char *buf)
1787{
1788 int res, s;
1789 res = hotkey_get_wlsw(&s);
1790 if (res < 0)
1791 return res;
1792
1793 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1794}
1795
1796static struct device_attribute dev_attr_hotkey_radio_sw =
1797 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1798
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001799static void hotkey_radio_sw_notify_change(void)
1800{
1801 if (tp_features.hotkey_wlsw)
1802 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1803 "hotkey_radio_sw");
1804}
1805
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001806/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1807static ssize_t hotkey_tablet_mode_show(struct device *dev,
1808 struct device_attribute *attr,
1809 char *buf)
1810{
1811 int res, s;
1812 res = hotkey_get_tablet_mode(&s);
1813 if (res < 0)
1814 return res;
1815
1816 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1817}
1818
1819static struct device_attribute dev_attr_hotkey_tablet_mode =
1820 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1821
1822static void hotkey_tablet_mode_notify_change(void)
1823{
1824 if (tp_features.hotkey_tablet)
1825 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1826 "hotkey_tablet_mode");
1827}
1828
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001829/* sysfs hotkey report_mode -------------------------------------------- */
1830static ssize_t hotkey_report_mode_show(struct device *dev,
1831 struct device_attribute *attr,
1832 char *buf)
1833{
1834 return snprintf(buf, PAGE_SIZE, "%d\n",
1835 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1836}
1837
1838static struct device_attribute dev_attr_hotkey_report_mode =
1839 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1840
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001841/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001842static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1843 struct device_attribute *attr,
1844 char *buf)
1845{
1846 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1847}
1848
1849static struct device_attribute dev_attr_hotkey_wakeup_reason =
1850 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1851
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001852static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001853{
1854 if (tp_features.hotkey_mask)
1855 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1856 "wakeup_reason");
1857}
1858
1859/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001860static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1861 struct device_attribute *attr,
1862 char *buf)
1863{
1864 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1865}
1866
1867static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1868 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1869 hotkey_wakeup_hotunplug_complete_show, NULL);
1870
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001871static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001872{
1873 if (tp_features.hotkey_mask)
1874 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1875 "wakeup_hotunplug_complete");
1876}
1877
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001878/* --------------------------------------------------------------------- */
1879
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001880static struct attribute *hotkey_attributes[] __initdata = {
1881 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001882 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001883 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001884#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1885 &dev_attr_hotkey_mask.attr,
1886 &dev_attr_hotkey_all_mask.attr,
1887 &dev_attr_hotkey_recommended_mask.attr,
1888 &dev_attr_hotkey_source_mask.attr,
1889 &dev_attr_hotkey_poll_freq.attr,
1890#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001891};
1892
1893static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001894 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001895#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1896 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001897 &dev_attr_hotkey_all_mask.attr,
1898 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001899#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001900 &dev_attr_hotkey_wakeup_reason.attr,
1901 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001902};
1903
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001904static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001905{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001906 /* Requirements for changing the default keymaps:
1907 *
1908 * 1. Many of the keys are mapped to KEY_RESERVED for very
1909 * good reasons. Do not change them unless you have deep
1910 * knowledge on the IBM and Lenovo ThinkPad firmware for
1911 * the various ThinkPad models. The driver behaves
1912 * differently for KEY_RESERVED: such keys have their
1913 * hot key mask *unset* in mask_recommended, and also
1914 * in the initial hot key mask programmed into the
1915 * firmware at driver load time, which means the firm-
1916 * ware may react very differently if you change them to
1917 * something else;
1918 *
1919 * 2. You must be subscribed to the linux-thinkpad and
1920 * ibm-acpi-devel mailing lists, and you should read the
1921 * list archives since 2007 if you want to change the
1922 * keymaps. This requirement exists so that you will
1923 * know the past history of problems with the thinkpad-
1924 * acpi driver keymaps, and also that you will be
1925 * listening to any bug reports;
1926 *
1927 * 3. Do not send thinkpad-acpi specific patches directly to
1928 * for merging, *ever*. Send them to the linux-acpi
1929 * mailinglist for comments. Merging is to be done only
1930 * through acpi-test and the ACPI maintainer.
1931 *
1932 * If the above is too much to ask, don't change the keymap.
1933 * Ask the thinkpad-acpi maintainer to do it, instead.
1934 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001935 static u16 ibm_keycode_map[] __initdata = {
1936 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1937 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1938 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1939 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001940
1941 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001942 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1943 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1944 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001945
1946 /* brightness: firmware always reacts to them, unless
1947 * X.org did some tricks in the radeon BIOS scratch
1948 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001949 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001950 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001951
1952 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001953 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001954
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001955 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1956 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001957
1958 /* Volume: firmware always react to it and reprograms
1959 * the built-in *extra* mixer. Never map it to control
1960 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001961 KEY_RESERVED, /* 0x14: VOLUME UP */
1962 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1963 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001964
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001965 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001966
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001967 /* (assignments unknown, please report if found) */
1968 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1969 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1970 };
1971 static u16 lenovo_keycode_map[] __initdata = {
1972 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1973 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1974 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1975 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001976
1977 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001978 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1979 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1980 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001981
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001982 /* These either have to go through ACPI video, or
1983 * act like in the IBM ThinkPads, so don't ever
1984 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001985 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001986 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001987
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001988 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001989
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001990 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1991 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001992
1993 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1994 * react to it and reprograms the built-in *extra* mixer.
1995 * Never map it to control another mixer by default.
1996 *
1997 * T60?, T61, R60?, R61: firmware and EC tries to send
1998 * these over the regular keyboard, so these are no-ops,
1999 * but there are still weird bugs re. MUTE, so do not
2000 * change unless you get test reports from all Lenovo
2001 * models. May cause the BIOS to interfere with the
2002 * HDA mixer.
2003 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002004 KEY_RESERVED, /* 0x14: VOLUME UP */
2005 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2006 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002007
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002008 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002009
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002010 /* (assignments unknown, please report if found) */
2011 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2012 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2013 };
2014
2015#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2016#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2017#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2018
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002019 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002020 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002021 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002022
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002023 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2024
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002025 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002026 BUG_ON(tpacpi_inputdev->open != NULL ||
2027 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002028
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002029 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002030 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002031
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002032#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2033 mutex_init(&hotkey_thread_mutex);
2034 mutex_init(&hotkey_thread_data_mutex);
2035#endif
2036
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002037 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002038 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002039
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002040 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002041 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002042
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002043 if (tp_features.hotkey) {
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002044 hotkey_dev_attributes = create_attr_set(13, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002045 if (!hotkey_dev_attributes)
2046 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002047 res = add_many_to_attr_set(hotkey_dev_attributes,
2048 hotkey_attributes,
2049 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002050 if (res)
2051 return res;
2052
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002053 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002054 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2055 for HKEY interface version 0x100 */
2056 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2057 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002058 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002059 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002060 printk(TPACPI_ERR "please report this to %s\n",
2061 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002062 } else {
2063 /*
2064 * MHKV 0x100 in A31, R40, R40e,
2065 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002066 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002067 tp_features.hotkey_mask = 1;
2068 }
2069 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002070
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002071 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002072 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002073
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002074 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002075 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002076 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002077 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002078 "missing MHKA handler, "
2079 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002080 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002081 /* FN+F12, FN+F4, FN+F3 */
2082 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002083 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002084 }
2085
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002086 /* hotkey_source_mask *must* be zero for
2087 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002088 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002089 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002090 res = hotkey_mask_get();
2091 hotkey_orig_mask = hotkey_mask;
2092 if (!res) {
2093 res = add_many_to_attr_set(
2094 hotkey_dev_attributes,
2095 hotkey_mask_attributes,
2096 ARRAY_SIZE(hotkey_mask_attributes));
2097 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002098 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002099
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002100#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2101 if (tp_features.hotkey_mask) {
2102 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2103 & ~hotkey_all_mask;
2104 } else {
2105 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2106 }
2107
2108 vdbg_printk(TPACPI_DBG_INIT,
2109 "hotkey source mask 0x%08x, polling freq %d\n",
2110 hotkey_source_mask, hotkey_poll_freq);
2111#endif
2112
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002113 /* Not all thinkpads have a hardware radio switch */
2114 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2115 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002116 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002117 "radio switch found; radios are %s\n",
2118 enabled(status, 0));
2119 res = add_to_attr_set(hotkey_dev_attributes,
2120 &dev_attr_hotkey_radio_sw.attr);
2121 }
2122
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002123 /* For X41t, X60t, X61t Tablets... */
2124 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2125 tp_features.hotkey_tablet = 1;
2126 printk(TPACPI_INFO
2127 "possible tablet mode switch found; "
2128 "ThinkPad in %s mode\n",
2129 (status & TP_HOTKEY_TABLET_MASK)?
2130 "tablet" : "laptop");
2131 res = add_to_attr_set(hotkey_dev_attributes,
2132 &dev_attr_hotkey_tablet_mode.attr);
2133 }
2134
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002135 if (!res)
2136 res = register_attr_set_with_sysfs(
2137 hotkey_dev_attributes,
2138 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002139 if (res)
2140 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002141
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002142 /* Set up key map */
2143
2144 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2145 GFP_KERNEL);
2146 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002147 printk(TPACPI_ERR
2148 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002149 return -ENOMEM;
2150 }
2151
2152 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2153 dbg_printk(TPACPI_DBG_INIT,
2154 "using Lenovo default hot key map\n");
2155 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2156 TPACPI_HOTKEY_MAP_SIZE);
2157 } else {
2158 dbg_printk(TPACPI_DBG_INIT,
2159 "using IBM default hot key map\n");
2160 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2161 TPACPI_HOTKEY_MAP_SIZE);
2162 }
2163
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002164 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2165 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2166 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002167 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2168 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2169 tpacpi_inputdev->keycode = hotkey_keycode_map;
2170 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002171 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2172 set_bit(hotkey_keycode_map[i],
2173 tpacpi_inputdev->keybit);
2174 } else {
2175 if (i < sizeof(hotkey_reserved_mask)*8)
2176 hotkey_reserved_mask |= 1 << i;
2177 }
2178 }
2179
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002180 if (tp_features.hotkey_wlsw) {
2181 set_bit(EV_SW, tpacpi_inputdev->evbit);
2182 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2183 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002184 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002185 set_bit(EV_SW, tpacpi_inputdev->evbit);
2186 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2187 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002188
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002189 /* Do not issue duplicate brightness change events to
2190 * userspace */
2191 if (!tp_features.bright_acpimode)
2192 /* update bright_acpimode... */
2193 tpacpi_check_std_acpi_brightness_support();
2194
2195 if (tp_features.bright_acpimode) {
2196 printk(TPACPI_INFO
2197 "This ThinkPad has standard ACPI backlight "
2198 "brightness control, supported by the ACPI "
2199 "video driver\n");
2200 printk(TPACPI_NOTICE
2201 "Disabling thinkpad-acpi brightness events "
2202 "by default...\n");
2203
2204 /* The hotkey_reserved_mask change below is not
2205 * necessary while the keys are at KEY_RESERVED in the
2206 * default map, but better safe than sorry, leave it
2207 * here as a marker of what we have to do, especially
2208 * when we finally become able to set this at runtime
2209 * on response to X.org requests */
2210 hotkey_reserved_mask |=
2211 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2212 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2213 }
2214
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002215 dbg_printk(TPACPI_DBG_INIT,
2216 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002217 res = hotkey_status_set(1);
2218 if (res)
2219 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002220 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2221 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002222 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002223 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002224 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002225
2226 dbg_printk(TPACPI_DBG_INIT,
2227 "legacy hot key reporting over procfs %s\n",
2228 (hotkey_report_mode < 2) ?
2229 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002230
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002231 tpacpi_inputdev->open = &hotkey_inputdev_open;
2232 tpacpi_inputdev->close = &hotkey_inputdev_close;
2233
2234 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002235 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002236 tpacpi_input_send_tabletsw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002237 }
2238
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002239 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002240}
2241
2242static void hotkey_exit(void)
2243{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002244#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2245 hotkey_poll_stop_sync();
2246#endif
2247
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002248 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002249 dbg_printk(TPACPI_DBG_EXIT,
2250 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002251 /* no short-circuit boolean operator below! */
2252 if ((hotkey_mask_set(hotkey_orig_mask) |
2253 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002254 printk(TPACPI_ERR
2255 "failed to restore hot key mask "
2256 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002257 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002258
2259 if (hotkey_dev_attributes) {
2260 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2261 hotkey_dev_attributes = NULL;
2262 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002263}
2264
2265static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2266{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002267 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002268 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002269 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002270 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002271 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002272
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002273 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002274 printk(TPACPI_ERR
2275 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002276 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002277 acpi_bus_generate_netlink_event(
2278 ibm->acpi->device->pnp.device_class,
2279 ibm->acpi->device->dev.bus_id,
2280 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002281 return;
2282 }
2283
2284 while (1) {
2285 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002286 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002287 return;
2288 }
2289
2290 if (hkey == 0) {
2291 /* queue empty */
2292 return;
2293 }
2294
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002295 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002296 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002297 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002298
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002299 switch (hkey >> 12) {
2300 case 1:
2301 /* 0x1000-0x1FFF: key presses */
2302 scancode = hkey & 0xfff;
2303 if (scancode > 0 && scancode < 0x21) {
2304 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002305 if (!(hotkey_source_mask & (1 << scancode))) {
2306 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002307 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002308 } else {
2309 ignore_acpi_ev = 1;
2310 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002311 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002312 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002313 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002314 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002315 case 2:
2316 /* Wakeup reason */
2317 switch (hkey) {
2318 case 0x2304: /* suspend, undock */
2319 case 0x2404: /* hibernation, undock */
2320 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2321 ignore_acpi_ev = 1;
2322 break;
2323 case 0x2305: /* suspend, bay eject */
2324 case 0x2405: /* hibernation, bay eject */
2325 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2326 ignore_acpi_ev = 1;
2327 break;
2328 default:
2329 unk_ev = 1;
2330 }
2331 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2332 printk(TPACPI_INFO
2333 "woke up due to a hot-unplug "
2334 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002335 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002336 }
2337 break;
2338 case 3:
2339 /* bay-related wakeups */
2340 if (hkey == 0x3003) {
2341 hotkey_autosleep_ack = 1;
2342 printk(TPACPI_INFO
2343 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002344 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002345 } else {
2346 unk_ev = 1;
2347 }
2348 break;
2349 case 4:
2350 /* dock-related wakeups */
2351 if (hkey == 0x4003) {
2352 hotkey_autosleep_ack = 1;
2353 printk(TPACPI_INFO
2354 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002355 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002356 } else {
2357 unk_ev = 1;
2358 }
2359 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002360 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002361 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002362 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002363 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002364 case 0x500b: /* X61t: tablet pen inserted into bay */
2365 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002366 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002367 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2368 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2369 tpacpi_input_send_tabletsw();
2370 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002371 send_acpi_ev = 0;
2372 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002373 case 0x5001:
2374 case 0x5002:
2375 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002376 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002377 break;
2378 default:
2379 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002380 }
2381 break;
2382 case 7:
2383 /* 0x7000-0x7FFF: misc */
2384 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2385 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002386 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002387 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002388 break;
2389 }
2390 /* fallthrough to default */
2391 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002392 unk_ev = 1;
2393 }
2394 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002395 printk(TPACPI_NOTICE
2396 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002397 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002398
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002399 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002400 if (!ignore_acpi_ev &&
2401 (send_acpi_ev || hotkey_report_mode < 2)) {
2402 acpi_bus_generate_proc_event(ibm->acpi->device,
2403 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002404 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002405
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002406 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002407 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002408 acpi_bus_generate_netlink_event(
2409 ibm->acpi->device->pnp.device_class,
2410 ibm->acpi->device->dev.bus_id,
2411 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002412 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002413 }
2414}
2415
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002416static void hotkey_suspend(pm_message_t state)
2417{
2418 /* Do these on suspend, we get the events on early resume! */
2419 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2420 hotkey_autosleep_ack = 0;
2421}
2422
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002423static void hotkey_resume(void)
2424{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002425 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002426 printk(TPACPI_ERR
2427 "error while trying to read hot key mask "
2428 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002429 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002430 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002431 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002432 hotkey_wakeup_reason_notify_change();
2433 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002434 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002435}
2436
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002437/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002438static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002440 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 int len = 0;
2442
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002443 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002444 len += sprintf(p + len, "status:\t\tnot supported\n");
2445 return len;
2446 }
2447
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002448 if (mutex_lock_interruptible(&hotkey_mutex))
2449 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002450 res = hotkey_status_get(&status);
2451 if (!res)
2452 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002453 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002454 if (res)
2455 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002458 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002459 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 len += sprintf(p + len,
2461 "commands:\tenable, disable, reset, <mask>\n");
2462 } else {
2463 len += sprintf(p + len, "mask:\t\tnot supported\n");
2464 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2465 }
2466
2467 return len;
2468}
2469
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002470static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002472 int res, status;
2473 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002476 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return -ENODEV;
2478
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002479 if (mutex_lock_interruptible(&hotkey_mutex))
2480 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002482 status = -1;
2483 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002484
2485 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 while ((cmd = next_cmd(&buf))) {
2487 if (strlencmp(cmd, "enable") == 0) {
2488 status = 1;
2489 } else if (strlencmp(cmd, "disable") == 0) {
2490 status = 0;
2491 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002492 status = hotkey_orig_status;
2493 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2495 /* mask set */
2496 } else if (sscanf(cmd, "%x", &mask) == 1) {
2497 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002498 } else {
2499 res = -EINVAL;
2500 goto errexit;
2501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002503 if (status != -1)
2504 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002506 if (!res && mask != hotkey_mask)
2507 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002509errexit:
2510 mutex_unlock(&hotkey_mutex);
2511 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002512}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002514static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002515 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002516 {"", 0},
2517};
2518
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002519static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002520 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002521 .notify = hotkey_notify,
2522 .handle = &hkey_handle,
2523 .type = ACPI_DEVICE_NOTIFY,
2524};
2525
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002526static struct ibm_struct hotkey_driver_data = {
2527 .name = "hotkey",
2528 .read = hotkey_read,
2529 .write = hotkey_write,
2530 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002531 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002532 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002533 .acpi = &ibm_hotkey_acpidriver,
2534};
2535
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002536/*************************************************************************
2537 * Bluetooth subdriver
2538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002540enum {
2541 /* ACPI GBDC/SBDC bits */
2542 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2543 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2544 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2545};
2546
2547static int bluetooth_get_radiosw(void);
2548static int bluetooth_set_radiosw(int radio_on);
2549
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002550/* sysfs bluetooth enable ---------------------------------------------- */
2551static ssize_t bluetooth_enable_show(struct device *dev,
2552 struct device_attribute *attr,
2553 char *buf)
2554{
2555 int status;
2556
2557 status = bluetooth_get_radiosw();
2558 if (status < 0)
2559 return status;
2560
2561 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2562}
2563
2564static ssize_t bluetooth_enable_store(struct device *dev,
2565 struct device_attribute *attr,
2566 const char *buf, size_t count)
2567{
2568 unsigned long t;
2569 int res;
2570
2571 if (parse_strtoul(buf, 1, &t))
2572 return -EINVAL;
2573
2574 res = bluetooth_set_radiosw(t);
2575
2576 return (res) ? res : count;
2577}
2578
2579static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002580 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002581 bluetooth_enable_show, bluetooth_enable_store);
2582
2583/* --------------------------------------------------------------------- */
2584
2585static struct attribute *bluetooth_attributes[] = {
2586 &dev_attr_bluetooth_enable.attr,
2587 NULL
2588};
2589
2590static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002591 .attrs = bluetooth_attributes,
2592};
2593
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002594static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002596 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002597 int status = 0;
2598
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002599 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2600
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002601 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002602
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002603 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2604 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002605 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002606 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002608 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2609 str_supported(tp_features.bluetooth),
2610 status);
2611
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002612 if (tp_features.bluetooth) {
2613 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2614 /* no bluetooth hardware present in system */
2615 tp_features.bluetooth = 0;
2616 dbg_printk(TPACPI_DBG_INIT,
2617 "bluetooth hardware not installed\n");
2618 } else {
2619 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2620 &bluetooth_attr_group);
2621 if (res)
2622 return res;
2623 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002624 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002625
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002626 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002629static void bluetooth_exit(void)
2630{
2631 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2632 &bluetooth_attr_group);
2633}
2634
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002635static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 int status;
2638
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002639 if (!tp_features.bluetooth)
2640 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002642 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2643 return -EIO;
2644
2645 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2646}
2647
2648static int bluetooth_set_radiosw(int radio_on)
2649{
2650 int status;
2651
2652 if (!tp_features.bluetooth)
2653 return -ENODEV;
2654
2655 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2656 return -EIO;
2657 if (radio_on)
2658 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2659 else
2660 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2661 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2662 return -EIO;
2663
2664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002667/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002668static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002671 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002673 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002676 len += sprintf(p + len, "status:\t\t%s\n",
2677 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 len += sprintf(p + len, "commands:\tenable, disable\n");
2679 }
2680
2681 return len;
2682}
2683
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002684static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002688 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002689 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 while ((cmd = next_cmd(&buf))) {
2692 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002693 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002695 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 } else
2697 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 return 0;
2701}
2702
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002703static struct ibm_struct bluetooth_driver_data = {
2704 .name = "bluetooth",
2705 .read = bluetooth_read,
2706 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002707 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002708};
2709
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002710/*************************************************************************
2711 * Wan subdriver
2712 */
2713
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002714enum {
2715 /* ACPI GWAN/SWAN bits */
2716 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2717 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2718 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2719};
2720
2721static int wan_get_radiosw(void);
2722static int wan_set_radiosw(int radio_on);
2723
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002724/* sysfs wan enable ---------------------------------------------------- */
2725static ssize_t wan_enable_show(struct device *dev,
2726 struct device_attribute *attr,
2727 char *buf)
2728{
2729 int status;
2730
2731 status = wan_get_radiosw();
2732 if (status < 0)
2733 return status;
2734
2735 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2736}
2737
2738static ssize_t wan_enable_store(struct device *dev,
2739 struct device_attribute *attr,
2740 const char *buf, size_t count)
2741{
2742 unsigned long t;
2743 int res;
2744
2745 if (parse_strtoul(buf, 1, &t))
2746 return -EINVAL;
2747
2748 res = wan_set_radiosw(t);
2749
2750 return (res) ? res : count;
2751}
2752
2753static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002754 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002755 wan_enable_show, wan_enable_store);
2756
2757/* --------------------------------------------------------------------- */
2758
2759static struct attribute *wan_attributes[] = {
2760 &dev_attr_wan_enable.attr,
2761 NULL
2762};
2763
2764static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002765 .attrs = wan_attributes,
2766};
2767
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002768static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002769{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002770 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002771 int status = 0;
2772
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002773 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2774
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002775 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002776
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002777 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002778 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002779
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002780 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2781 str_supported(tp_features.wan),
2782 status);
2783
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002784 if (tp_features.wan) {
2785 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2786 /* no wan hardware present in system */
2787 tp_features.wan = 0;
2788 dbg_printk(TPACPI_DBG_INIT,
2789 "wan hardware not installed\n");
2790 } else {
2791 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2792 &wan_attr_group);
2793 if (res)
2794 return res;
2795 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002796 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002797
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002798 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002799}
2800
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002801static void wan_exit(void)
2802{
2803 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2804 &wan_attr_group);
2805}
2806
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002807static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002808{
2809 int status;
2810
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002811 if (!tp_features.wan)
2812 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002813
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002814 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2815 return -EIO;
2816
2817 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2818}
2819
2820static int wan_set_radiosw(int radio_on)
2821{
2822 int status;
2823
2824 if (!tp_features.wan)
2825 return -ENODEV;
2826
2827 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2828 return -EIO;
2829 if (radio_on)
2830 status |= TP_ACPI_WANCARD_RADIOSSW;
2831 else
2832 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2833 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2834 return -EIO;
2835
2836 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002837}
2838
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002839/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002840static int wan_read(char *p)
2841{
2842 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002843 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002844
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002845 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002846 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002847 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002848 len += sprintf(p + len, "status:\t\t%s\n",
2849 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002850 len += sprintf(p + len, "commands:\tenable, disable\n");
2851 }
2852
2853 return len;
2854}
2855
2856static int wan_write(char *buf)
2857{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002858 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002859
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002860 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002861 return -ENODEV;
2862
2863 while ((cmd = next_cmd(&buf))) {
2864 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002865 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002866 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002867 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002868 } else
2869 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002870 }
2871
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002872 return 0;
2873}
2874
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002875static struct ibm_struct wan_driver_data = {
2876 .name = "wan",
2877 .read = wan_read,
2878 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002879 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002880 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002881};
2882
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002883/*************************************************************************
2884 * Video subdriver
2885 */
2886
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002887#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2888
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002889enum video_access_mode {
2890 TPACPI_VIDEO_NONE = 0,
2891 TPACPI_VIDEO_570, /* 570 */
2892 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2893 TPACPI_VIDEO_NEW, /* all others */
2894};
2895
2896enum { /* video status flags, based on VIDEO_570 */
2897 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2898 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2899 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2900};
2901
2902enum { /* TPACPI_VIDEO_570 constants */
2903 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2904 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2905 * video_status_flags */
2906 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2907 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2908};
2909
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002910static enum video_access_mode video_supported;
2911static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002912
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002913static int video_autosw_get(void);
2914static int video_autosw_set(int enable);
2915
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002916TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002917
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002918static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002920 int ivga;
2921
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002922 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2923
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002924 TPACPI_ACPIHANDLE_INIT(vid);
2925 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002926
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002927 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2928 /* G41, assume IVGA doesn't change */
2929 vid_handle = vid2_handle;
2930
2931 if (!vid_handle)
2932 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002933 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002934 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2935 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002936 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002937 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2938 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002939 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002940 else
2941 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002942 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002944 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2945 str_supported(video_supported != TPACPI_VIDEO_NONE),
2946 video_supported);
2947
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002948 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002951static void video_exit(void)
2952{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002953 dbg_printk(TPACPI_DBG_EXIT,
2954 "restoring original video autoswitch mode\n");
2955 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002956 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002957 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002958}
2959
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002960static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 int status = 0;
2963 int i;
2964
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002965 switch (video_supported) {
2966 case TPACPI_VIDEO_570:
2967 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2968 TP_ACPI_VIDEO_570_PHSCMD))
2969 return -EIO;
2970 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2971 break;
2972 case TPACPI_VIDEO_770:
2973 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2974 return -EIO;
2975 if (i)
2976 status |= TP_ACPI_VIDEO_S_LCD;
2977 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2978 return -EIO;
2979 if (i)
2980 status |= TP_ACPI_VIDEO_S_CRT;
2981 break;
2982 case TPACPI_VIDEO_NEW:
2983 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2984 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2985 return -EIO;
2986 if (i)
2987 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002989 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2990 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2991 return -EIO;
2992 if (i)
2993 status |= TP_ACPI_VIDEO_S_LCD;
2994 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2995 return -EIO;
2996 if (i)
2997 status |= TP_ACPI_VIDEO_S_DVI;
2998 break;
2999 default:
3000 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 return status;
3004}
3005
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003006static int video_outputsw_set(int status)
3007{
3008 int autosw;
3009 int res = 0;
3010
3011 switch (video_supported) {
3012 case TPACPI_VIDEO_570:
3013 res = acpi_evalf(NULL, NULL,
3014 "\\_SB.PHS2", "vdd",
3015 TP_ACPI_VIDEO_570_PHS2CMD,
3016 status | TP_ACPI_VIDEO_570_PHS2SET);
3017 break;
3018 case TPACPI_VIDEO_770:
3019 autosw = video_autosw_get();
3020 if (autosw < 0)
3021 return autosw;
3022
3023 res = video_autosw_set(1);
3024 if (res)
3025 return res;
3026 res = acpi_evalf(vid_handle, NULL,
3027 "ASWT", "vdd", status * 0x100, 0);
3028 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003029 printk(TPACPI_ERR
3030 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003031 return -EIO;
3032 }
3033 break;
3034 case TPACPI_VIDEO_NEW:
3035 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003036 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003037 break;
3038 default:
3039 return -ENOSYS;
3040 }
3041
3042 return (res)? 0 : -EIO;
3043}
3044
3045static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003047 int autosw = 0;
3048
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003049 switch (video_supported) {
3050 case TPACPI_VIDEO_570:
3051 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3052 return -EIO;
3053 break;
3054 case TPACPI_VIDEO_770:
3055 case TPACPI_VIDEO_NEW:
3056 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3057 return -EIO;
3058 break;
3059 default:
3060 return -ENOSYS;
3061 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003062
3063 return autosw & 1;
3064}
3065
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003066static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003067{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003068 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003069 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003070 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003071}
3072
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003073static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003074{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003075 int autosw = video_autosw_get();
3076 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003077
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003078 if (autosw < 0)
3079 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003080
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003081 switch (video_supported) {
3082 case TPACPI_VIDEO_570:
3083 res = video_autosw_set(1);
3084 if (res)
3085 return res;
3086 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3087 break;
3088 case TPACPI_VIDEO_770:
3089 case TPACPI_VIDEO_NEW:
3090 res = video_autosw_set(1);
3091 if (res)
3092 return res;
3093 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3094 break;
3095 default:
3096 return -ENOSYS;
3097 }
3098 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003099 printk(TPACPI_ERR
3100 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003101 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003102 }
3103
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003104 return (res)? 0 : -EIO;
3105}
3106
3107static int video_expand_toggle(void)
3108{
3109 switch (video_supported) {
3110 case TPACPI_VIDEO_570:
3111 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3112 0 : -EIO;
3113 case TPACPI_VIDEO_770:
3114 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3115 0 : -EIO;
3116 case TPACPI_VIDEO_NEW:
3117 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3118 0 : -EIO;
3119 default:
3120 return -ENOSYS;
3121 }
3122 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003123}
3124
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003125static int video_read(char *p)
3126{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003127 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003128 int len = 0;
3129
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003130 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003131 len += sprintf(p + len, "status:\t\tnot supported\n");
3132 return len;
3133 }
3134
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003135 status = video_outputsw_get();
3136 if (status < 0)
3137 return status;
3138
3139 autosw = video_autosw_get();
3140 if (autosw < 0)
3141 return autosw;
3142
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003143 len += sprintf(p + len, "status:\t\tsupported\n");
3144 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3145 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003146 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003147 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3148 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3149 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3150 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003151 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003152 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3153 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3154 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3155
3156 return len;
3157}
3158
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003159static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
3161 char *cmd;
3162 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003163 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003165 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003166 return -ENODEV;
3167
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003168 enable = 0;
3169 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
3171 while ((cmd = next_cmd(&buf))) {
3172 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003173 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003175 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003177 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003179 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003180 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003181 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003182 enable |= TP_ACPI_VIDEO_S_DVI;
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_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003185 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003187 res = video_autosw_set(1);
3188 if (res)
3189 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003191 res = video_autosw_set(0);
3192 if (res)
3193 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003195 res = video_outputsw_cycle();
3196 if (res)
3197 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003199 res = video_expand_toggle();
3200 if (res)
3201 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 } else
3203 return -EINVAL;
3204 }
3205
3206 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003207 status = video_outputsw_get();
3208 if (status < 0)
3209 return status;
3210 res = video_outputsw_set((status & ~disable) | enable);
3211 if (res)
3212 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 }
3214
3215 return 0;
3216}
3217
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003218static struct ibm_struct video_driver_data = {
3219 .name = "video",
3220 .read = video_read,
3221 .write = video_write,
3222 .exit = video_exit,
3223};
3224
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003225#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3226
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003227/*************************************************************************
3228 * Light (thinklight) subdriver
3229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003231TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3232TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003233
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003234static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003236 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3237
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003238 TPACPI_ACPIHANDLE_INIT(ledb);
3239 TPACPI_ACPIHANDLE_INIT(lght);
3240 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003241
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003242 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003243 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003244
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003245 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003246 /* light status not supported on
3247 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003248 tp_features.light_status =
3249 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003251 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003252 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003253
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003254 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003257static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
3259 int len = 0;
3260 int status = 0;
3261
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003262 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003263 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003264 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003265 len += sprintf(p + len, "status:\t\tunknown\n");
3266 len += sprintf(p + len, "commands:\ton, off\n");
3267 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3269 return -EIO;
3270 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003271 len += sprintf(p + len, "commands:\ton, off\n");
3272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 return len;
3275}
3276
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003277static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
3279 int cmos_cmd, lght_cmd;
3280 char *cmd;
3281 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003282
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003283 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003284 return -ENODEV;
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 while ((cmd = next_cmd(&buf))) {
3287 if (strlencmp(cmd, "on") == 0) {
3288 cmos_cmd = 0x0c;
3289 lght_cmd = 1;
3290 } else if (strlencmp(cmd, "off") == 0) {
3291 cmos_cmd = 0x0d;
3292 lght_cmd = 0;
3293 } else
3294 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003297 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3298 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (!success)
3300 return -EIO;
3301 }
3302
3303 return 0;
3304}
3305
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003306static struct ibm_struct light_driver_data = {
3307 .name = "light",
3308 .read = light_read,
3309 .write = light_write,
3310};
3311
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003312/*************************************************************************
3313 * Dock subdriver
3314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003316#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003317
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003318static void dock_notify(struct ibm_struct *ibm, u32 event);
3319static int dock_read(char *p);
3320static int dock_write(char *buf);
3321
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003322TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003323 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3324 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3325 "\\_SB.PCI.ISA.SLCE", /* 570 */
3326 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3327
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003328/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003329TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003330
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003331static const struct acpi_device_id ibm_pci_device_ids[] = {
3332 {PCI_ROOT_HID_STRING, 0},
3333 {"", 0},
3334};
3335
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003336static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3337 {
3338 .notify = dock_notify,
3339 .handle = &dock_handle,
3340 .type = ACPI_SYSTEM_NOTIFY,
3341 },
3342 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003343 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3344 * We just use it to get notifications of dock hotplug
3345 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003346 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003347 .notify = dock_notify,
3348 .handle = &pci_handle,
3349 .type = ACPI_SYSTEM_NOTIFY,
3350 },
3351};
3352
3353static struct ibm_struct dock_driver_data[2] = {
3354 {
3355 .name = "dock",
3356 .read = dock_read,
3357 .write = dock_write,
3358 .acpi = &ibm_dock_acpidriver[0],
3359 },
3360 {
3361 .name = "dock",
3362 .acpi = &ibm_dock_acpidriver[1],
3363 },
3364};
3365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366#define dock_docked() (_sta(dock_handle) & 1)
3367
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003368static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003369{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003370 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3371
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003372 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003373
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003374 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3375 str_supported(dock_handle != NULL));
3376
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003377 return (dock_handle)? 0 : 1;
3378}
3379
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003380static int __init dock_init2(struct ibm_init_struct *iibm)
3381{
3382 int dock2_needed;
3383
3384 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3385
3386 if (dock_driver_data[0].flags.acpi_driver_registered &&
3387 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003388 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003389 dock2_needed = (pci_handle != NULL);
3390 vdbg_printk(TPACPI_DBG_INIT,
3391 "dock PCI handler for the TP 570 is %s\n",
3392 str_supported(dock2_needed));
3393 } else {
3394 vdbg_printk(TPACPI_DBG_INIT,
3395 "dock subdriver part 2 not required\n");
3396 dock2_needed = 0;
3397 }
3398
3399 return (dock2_needed)? 0 : 1;
3400}
3401
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003402static void dock_notify(struct ibm_struct *ibm, u32 event)
3403{
3404 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003405 int pci = ibm->acpi->hid && ibm->acpi->device &&
3406 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003407 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003408
3409 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003410 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003411 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003412 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003413 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003414 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003415 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003416 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003417 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003418 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003419 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003420 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003421 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003422 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003423 }
Len Brown14e04fb32007-08-23 15:20:26 -04003424 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003425 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3426 ibm->acpi->device->dev.bus_id,
3427 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003428}
3429
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003430static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
3432 int len = 0;
3433 int docked = dock_docked();
3434
3435 if (!dock_handle)
3436 len += sprintf(p + len, "status:\t\tnot supported\n");
3437 else if (!docked)
3438 len += sprintf(p + len, "status:\t\tundocked\n");
3439 else {
3440 len += sprintf(p + len, "status:\t\tdocked\n");
3441 len += sprintf(p + len, "commands:\tdock, undock\n");
3442 }
3443
3444 return len;
3445}
3446
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003447static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
3449 char *cmd;
3450
3451 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003452 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
3454 while ((cmd = next_cmd(&buf))) {
3455 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003456 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3457 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return -EIO;
3459 } else if (strlencmp(cmd, "dock") == 0) {
3460 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3461 return -EIO;
3462 } else
3463 return -EINVAL;
3464 }
3465
3466 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003469#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003471/*************************************************************************
3472 * Bay subdriver
3473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003475#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003476
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003477TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003478 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3479 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3480 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3481 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003482TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003483 "_EJ0", /* all others */
3484 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003485TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003486 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3487 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003488TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003489 "_EJ0", /* 770x */
3490 ); /* all others */
3491
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003492static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003494 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3495
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003496 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003497 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003498 TPACPI_ACPIHANDLE_INIT(bay_ej);
3499 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003500 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003501 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003502
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003503 tp_features.bay_status = bay_handle &&
3504 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3505 tp_features.bay_status2 = bay2_handle &&
3506 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003507
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003508 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3509 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3510 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3511 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003513 vdbg_printk(TPACPI_DBG_INIT,
3514 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003515 str_supported(tp_features.bay_status),
3516 str_supported(tp_features.bay_eject),
3517 str_supported(tp_features.bay_status2),
3518 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003519
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003520 return (tp_features.bay_status || tp_features.bay_eject ||
3521 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522}
3523
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003524static void bay_notify(struct ibm_struct *ibm, u32 event)
3525{
Len Brown14e04fb32007-08-23 15:20:26 -04003526 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003527 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3528 ibm->acpi->device->dev.bus_id,
3529 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003530}
3531
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003532#define bay_occupied(b) (_sta(b##_handle) & 1)
3533
3534static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
3536 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003537 int occupied = bay_occupied(bay);
3538 int occupied2 = bay_occupied(bay2);
3539 int eject, eject2;
3540
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003541 len += sprintf(p + len, "status:\t\t%s\n",
3542 tp_features.bay_status ?
3543 (occupied ? "occupied" : "unoccupied") :
3544 "not supported");
3545 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003546 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3547 "occupied" : "unoccupied");
3548
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003549 eject = tp_features.bay_eject && occupied;
3550 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003551
3552 if (eject && eject2)
3553 len += sprintf(p + len, "commands:\teject, eject2\n");
3554 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003556 else if (eject2)
3557 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 return len;
3560}
3561
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003562static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
3564 char *cmd;
3565
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003566 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003567 return -ENODEV;
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003570 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003571 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3572 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003573 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003574 strlencmp(cmd, "eject2") == 0) {
3575 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 return -EIO;
3577 } else
3578 return -EINVAL;
3579 }
3580
3581 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003582}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003583
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003584static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3585 .notify = bay_notify,
3586 .handle = &bay_handle,
3587 .type = ACPI_SYSTEM_NOTIFY,
3588};
3589
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003590static struct ibm_struct bay_driver_data = {
3591 .name = "bay",
3592 .read = bay_read,
3593 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003594 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003595};
3596
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003597#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003599/*************************************************************************
3600 * CMOS subdriver
3601 */
3602
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003603/* sysfs cmos_command -------------------------------------------------- */
3604static ssize_t cmos_command_store(struct device *dev,
3605 struct device_attribute *attr,
3606 const char *buf, size_t count)
3607{
3608 unsigned long cmos_cmd;
3609 int res;
3610
3611 if (parse_strtoul(buf, 21, &cmos_cmd))
3612 return -EINVAL;
3613
3614 res = issue_thinkpad_cmos_command(cmos_cmd);
3615 return (res)? res : count;
3616}
3617
3618static struct device_attribute dev_attr_cmos_command =
3619 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3620
3621/* --------------------------------------------------------------------- */
3622
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003623static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003624{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003625 int res;
3626
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003627 vdbg_printk(TPACPI_DBG_INIT,
3628 "initializing cmos commands subdriver\n");
3629
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003630 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003631
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003632 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3633 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003634
3635 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3636 if (res)
3637 return res;
3638
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003639 return (cmos_handle)? 0 : 1;
3640}
3641
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003642static void cmos_exit(void)
3643{
3644 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3645}
3646
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003647static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
3649 int len = 0;
3650
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003651 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3652 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 if (!cmos_handle)
3654 len += sprintf(p + len, "status:\t\tnot supported\n");
3655 else {
3656 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003657 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
3659
3660 return len;
3661}
3662
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003663static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
3665 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003666 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
3668 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003669 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3670 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 /* cmos_cmd set */
3672 } else
3673 return -EINVAL;
3674
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003675 res = issue_thinkpad_cmos_command(cmos_cmd);
3676 if (res)
3677 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
3679
3680 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003681}
3682
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003683static struct ibm_struct cmos_driver_data = {
3684 .name = "cmos",
3685 .read = cmos_read,
3686 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003687 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003688};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003689
3690/*************************************************************************
3691 * LED subdriver
3692 */
3693
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003694enum led_access_mode {
3695 TPACPI_LED_NONE = 0,
3696 TPACPI_LED_570, /* 570 */
3697 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3698 TPACPI_LED_NEW, /* all others */
3699};
3700
3701enum { /* For TPACPI_LED_OLD */
3702 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3703 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3704 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3705};
3706
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003707static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003708
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003709TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003710 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3711 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003712 "LED", /* all others */
3713 ); /* R30, R31 */
3714
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003715static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003716{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003717 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3718
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003719 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003720
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003721 if (!led_handle)
3722 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003723 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003724 else if (strlencmp(led_path, "SLED") == 0)
3725 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003726 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003727 else if (strlencmp(led_path, "SYSL") == 0)
3728 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003729 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003730 else
3731 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003732 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003733
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003734 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3735 str_supported(led_supported), led_supported);
3736
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003737 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003738}
3739
3740#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3741
3742static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743{
3744 int len = 0;
3745
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003746 if (!led_supported) {
3747 len += sprintf(p + len, "status:\t\tnot supported\n");
3748 return len;
3749 }
3750 len += sprintf(p + len, "status:\t\tsupported\n");
3751
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003752 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003753 /* 570 */
3754 int i, status;
3755 for (i = 0; i < 8; i++) {
3756 if (!acpi_evalf(ec_handle,
3757 &status, "GLED", "dd", 1 << i))
3758 return -EIO;
3759 len += sprintf(p + len, "%d:\t\t%s\n",
3760 i, led_status(status));
3761 }
3762 }
3763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003765 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 return len;
3768}
3769
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003770/* off, on, blink */
3771static const int led_sled_arg1[] = { 0, 1, 3 };
3772static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3773static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3774static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3775
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003776static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
3778 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003779 int led, ind, ret;
3780
3781 if (!led_supported)
3782 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
3784 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003785 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return -EINVAL;
3787
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003788 if (strstr(cmd, "off")) {
3789 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003791 ind = 1;
3792 } else if (strstr(cmd, "blink")) {
3793 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 } else
3795 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003796
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003797 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003798 /* 570 */
3799 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003801 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003803 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003804 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3805 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003806 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003807 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003808 ret = ec_write(TPACPI_LED_EC_HLBL,
3809 led * led_exp_hlbl[ind]);
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_HLCL,
3812 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003813 if (ret < 0)
3814 return ret;
3815 } else {
3816 /* all others */
3817 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3818 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 }
3822
3823 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003824}
3825
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003826static struct ibm_struct led_driver_data = {
3827 .name = "led",
3828 .read = led_read,
3829 .write = led_write,
3830};
3831
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003832/*************************************************************************
3833 * Beep subdriver
3834 */
3835
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003836TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003837
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003838static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003839{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003840 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3841
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003842 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003843
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003844 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3845 str_supported(beep_handle != NULL));
3846
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003847 return (beep_handle)? 0 : 1;
3848}
3849
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003850static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
3852 int len = 0;
3853
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003854 if (!beep_handle)
3855 len += sprintf(p + len, "status:\t\tnot supported\n");
3856 else {
3857 len += sprintf(p + len, "status:\t\tsupported\n");
3858 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
3861 return len;
3862}
3863
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003864static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
3866 char *cmd;
3867 int beep_cmd;
3868
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003869 if (!beep_handle)
3870 return -ENODEV;
3871
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003873 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3874 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 /* beep_cmd set */
3876 } else
3877 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003878 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 return -EIO;
3880 }
3881
3882 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003883}
3884
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003885static struct ibm_struct beep_driver_data = {
3886 .name = "beep",
3887 .read = beep_read,
3888 .write = beep_write,
3889};
3890
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003891/*************************************************************************
3892 * Thermal subdriver
3893 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003894
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003895enum thermal_access_mode {
3896 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3897 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3898 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3899 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3900 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3901};
3902
3903enum { /* TPACPI_THERMAL_TPEC_* */
3904 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3905 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3906 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3907};
3908
3909#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3910struct ibm_thermal_sensors_struct {
3911 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3912};
3913
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003914static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003915
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003916/* idx is zero-based */
3917static int thermal_get_sensor(int idx, s32 *value)
3918{
3919 int t;
3920 s8 tmp;
3921 char tmpi[5];
3922
3923 t = TP_EC_THERMAL_TMP0;
3924
3925 switch (thermal_read_mode) {
3926#if TPACPI_MAX_THERMAL_SENSORS >= 16
3927 case TPACPI_THERMAL_TPEC_16:
3928 if (idx >= 8 && idx <= 15) {
3929 t = TP_EC_THERMAL_TMP8;
3930 idx -= 8;
3931 }
3932 /* fallthrough */
3933#endif
3934 case TPACPI_THERMAL_TPEC_8:
3935 if (idx <= 7) {
3936 if (!acpi_ec_read(t + idx, &tmp))
3937 return -EIO;
3938 *value = tmp * 1000;
3939 return 0;
3940 }
3941 break;
3942
3943 case TPACPI_THERMAL_ACPI_UPDT:
3944 if (idx <= 7) {
3945 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3946 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3947 return -EIO;
3948 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3949 return -EIO;
3950 *value = (t - 2732) * 100;
3951 return 0;
3952 }
3953 break;
3954
3955 case TPACPI_THERMAL_ACPI_TMP07:
3956 if (idx <= 7) {
3957 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3958 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3959 return -EIO;
3960 if (t > 127 || t < -127)
3961 t = TP_EC_THERMAL_TMP_NA;
3962 *value = t * 1000;
3963 return 0;
3964 }
3965 break;
3966
3967 case TPACPI_THERMAL_NONE:
3968 default:
3969 return -ENOSYS;
3970 }
3971
3972 return -EINVAL;
3973}
3974
3975static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3976{
3977 int res, i;
3978 int n;
3979
3980 n = 8;
3981 i = 0;
3982
3983 if (!s)
3984 return -EINVAL;
3985
3986 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3987 n = 16;
3988
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003989 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003990 res = thermal_get_sensor(i, &s->temp[i]);
3991 if (res)
3992 return res;
3993 }
3994
3995 return n;
3996}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003997
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003998/* sysfs temp##_input -------------------------------------------------- */
3999
4000static ssize_t thermal_temp_input_show(struct device *dev,
4001 struct device_attribute *attr,
4002 char *buf)
4003{
4004 struct sensor_device_attribute *sensor_attr =
4005 to_sensor_dev_attr(attr);
4006 int idx = sensor_attr->index;
4007 s32 value;
4008 int res;
4009
4010 res = thermal_get_sensor(idx, &value);
4011 if (res)
4012 return res;
4013 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4014 return -ENXIO;
4015
4016 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4017}
4018
4019#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004020 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4021 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004022
4023static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4024 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4025 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4026 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4027 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4028 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4029 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4030 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4031 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4032 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4033 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4034 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4035 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4036 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4037 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4038 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4039 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4040};
4041
4042#define THERMAL_ATTRS(X) \
4043 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4044
4045static struct attribute *thermal_temp_input_attr[] = {
4046 THERMAL_ATTRS(8),
4047 THERMAL_ATTRS(9),
4048 THERMAL_ATTRS(10),
4049 THERMAL_ATTRS(11),
4050 THERMAL_ATTRS(12),
4051 THERMAL_ATTRS(13),
4052 THERMAL_ATTRS(14),
4053 THERMAL_ATTRS(15),
4054 THERMAL_ATTRS(0),
4055 THERMAL_ATTRS(1),
4056 THERMAL_ATTRS(2),
4057 THERMAL_ATTRS(3),
4058 THERMAL_ATTRS(4),
4059 THERMAL_ATTRS(5),
4060 THERMAL_ATTRS(6),
4061 THERMAL_ATTRS(7),
4062 NULL
4063};
4064
4065static const struct attribute_group thermal_temp_input16_group = {
4066 .attrs = thermal_temp_input_attr
4067};
4068
4069static const struct attribute_group thermal_temp_input8_group = {
4070 .attrs = &thermal_temp_input_attr[8]
4071};
4072
4073#undef THERMAL_SENSOR_ATTR_TEMP
4074#undef THERMAL_ATTRS
4075
4076/* --------------------------------------------------------------------- */
4077
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004078static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004079{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004080 u8 t, ta1, ta2;
4081 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004082 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004083 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004084
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004085 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4086
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004087 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004088
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004089 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004090 /*
4091 * Direct EC access mode: sensors at registers
4092 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4093 * non-implemented, thermal sensors return 0x80 when
4094 * not available
4095 */
4096
4097 ta1 = ta2 = 0;
4098 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004099 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004100 ta1 |= t;
4101 } else {
4102 ta1 = 0;
4103 break;
4104 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004105 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004106 ta2 |= t;
4107 } else {
4108 ta1 = 0;
4109 break;
4110 }
4111 }
4112 if (ta1 == 0) {
4113 /* This is sheer paranoia, but we handle it anyway */
4114 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004115 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004116 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004117 "falling back to ACPI TMPx access "
4118 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004119 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004120 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004121 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004122 "ThinkPad ACPI EC access misbehaving, "
4123 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004124 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004125 }
4126 } else {
4127 thermal_read_mode =
4128 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004129 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004130 }
4131 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004132 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4133 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004134 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004135 } else {
4136 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004137 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004138 }
4139 } else {
4140 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004141 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004142 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004143
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004144 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4145 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4146 thermal_read_mode);
4147
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004148 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004149 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004150 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004151 &thermal_temp_input16_group);
4152 if (res)
4153 return res;
4154 break;
4155 case TPACPI_THERMAL_TPEC_8:
4156 case TPACPI_THERMAL_ACPI_TMP07:
4157 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004158 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004159 &thermal_temp_input8_group);
4160 if (res)
4161 return res;
4162 break;
4163 case TPACPI_THERMAL_NONE:
4164 default:
4165 return 1;
4166 }
4167
4168 return 0;
4169}
4170
4171static void thermal_exit(void)
4172{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004173 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004174 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004175 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004176 &thermal_temp_input16_group);
4177 break;
4178 case TPACPI_THERMAL_TPEC_8:
4179 case TPACPI_THERMAL_ACPI_TMP07:
4180 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004181 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004182 &thermal_temp_input16_group);
4183 break;
4184 case TPACPI_THERMAL_NONE:
4185 default:
4186 break;
4187 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004188}
4189
4190static int thermal_read(char *p)
4191{
4192 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004193 int n, i;
4194 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004195
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004196 n = thermal_get_sensors(&t);
4197 if (unlikely(n < 0))
4198 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004199
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004200 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004201
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004202 if (n > 0) {
4203 for (i = 0; i < (n - 1); i++)
4204 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4205 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4206 } else
4207 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004208
4209 return len;
4210}
4211
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004212static struct ibm_struct thermal_driver_data = {
4213 .name = "thermal",
4214 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004215 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004216};
4217
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004218/*************************************************************************
4219 * EC Dump subdriver
4220 */
4221
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004222static u8 ecdump_regs[256];
4223
4224static int ecdump_read(char *p)
4225{
4226 int len = 0;
4227 int i, j;
4228 u8 v;
4229
4230 len += sprintf(p + len, "EC "
4231 " +00 +01 +02 +03 +04 +05 +06 +07"
4232 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4233 for (i = 0; i < 256; i += 16) {
4234 len += sprintf(p + len, "EC 0x%02x:", i);
4235 for (j = 0; j < 16; j++) {
4236 if (!acpi_ec_read(i + j, &v))
4237 break;
4238 if (v != ecdump_regs[i + j])
4239 len += sprintf(p + len, " *%02x", v);
4240 else
4241 len += sprintf(p + len, " %02x", v);
4242 ecdump_regs[i + j] = v;
4243 }
4244 len += sprintf(p + len, "\n");
4245 if (j != 16)
4246 break;
4247 }
4248
4249 /* These are way too dangerous to advertise openly... */
4250#if 0
4251 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4252 " (<offset> is 00-ff, <value> is 00-ff)\n");
4253 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4254 " (<offset> is 00-ff, <value> is 0-255)\n");
4255#endif
4256 return len;
4257}
4258
4259static int ecdump_write(char *buf)
4260{
4261 char *cmd;
4262 int i, v;
4263
4264 while ((cmd = next_cmd(&buf))) {
4265 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4266 /* i and v set */
4267 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4268 /* i and v set */
4269 } else
4270 return -EINVAL;
4271 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4272 if (!acpi_ec_write(i, v))
4273 return -EIO;
4274 } else
4275 return -EINVAL;
4276 }
4277
4278 return 0;
4279}
4280
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004281static struct ibm_struct ecdump_driver_data = {
4282 .name = "ecdump",
4283 .read = ecdump_read,
4284 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004285 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004286};
4287
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004288/*************************************************************************
4289 * Backlight/brightness subdriver
4290 */
Holger Macht8acb0252006-10-20 14:30:28 -07004291
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004292#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4293
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004294static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004295static int brightness_offset = 0x31;
4296static int brightness_mode;
4297static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4298
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004299static struct mutex brightness_mutex;
4300
4301/*
4302 * ThinkPads can read brightness from two places: EC 0x31, or
4303 * CMOS NVRAM byte 0x5E, bits 0-3.
4304 */
4305static int brightness_get(struct backlight_device *bd)
4306{
4307 u8 lec = 0, lcmos = 0, level = 0;
4308
4309 if (brightness_mode & 1) {
4310 if (!acpi_ec_read(brightness_offset, &lec))
4311 return -EIO;
4312 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4313 level = lec;
4314 };
4315 if (brightness_mode & 2) {
4316 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4317 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4318 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4319 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4320 level = lcmos;
4321 }
4322
4323 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004324 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004325 "CMOS NVRAM (%u) and EC (%u) do not agree "
4326 "on display brightness level\n",
4327 (unsigned int) lcmos,
4328 (unsigned int) lec);
4329 return -EIO;
4330 }
4331
4332 return level;
4333}
4334
4335/* May return EINTR which can always be mapped to ERESTARTSYS */
4336static int brightness_set(int value)
4337{
4338 int cmos_cmd, inc, i, res;
4339 int current_value;
4340
4341 if (value > ((tp_features.bright_16levels)? 15 : 7))
4342 return -EINVAL;
4343
4344 res = mutex_lock_interruptible(&brightness_mutex);
4345 if (res < 0)
4346 return res;
4347
4348 current_value = brightness_get(NULL);
4349 if (current_value < 0) {
4350 res = current_value;
4351 goto errout;
4352 }
4353
4354 cmos_cmd = value > current_value ?
4355 TP_CMOS_BRIGHTNESS_UP :
4356 TP_CMOS_BRIGHTNESS_DOWN;
4357 inc = (value > current_value)? 1 : -1;
4358
4359 res = 0;
4360 for (i = current_value; i != value; i += inc) {
4361 if ((brightness_mode & 2) &&
4362 issue_thinkpad_cmos_command(cmos_cmd)) {
4363 res = -EIO;
4364 goto errout;
4365 }
4366 if ((brightness_mode & 1) &&
4367 !acpi_ec_write(brightness_offset, i + inc)) {
4368 res = -EIO;
4369 goto errout;;
4370 }
4371 }
4372
4373errout:
4374 mutex_unlock(&brightness_mutex);
4375 return res;
4376}
4377
4378/* sysfs backlight class ----------------------------------------------- */
4379
4380static int brightness_update_status(struct backlight_device *bd)
4381{
4382 /* it is the backlight class's job (caller) to handle
4383 * EINTR and other errors properly */
4384 return brightness_set(
4385 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4386 bd->props.power == FB_BLANK_UNBLANK) ?
4387 bd->props.brightness : 0);
4388}
Holger Macht8acb0252006-10-20 14:30:28 -07004389
Richard Purdie599a52d2007-02-10 23:07:48 +00004390static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004391 .get_brightness = brightness_get,
4392 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004393};
4394
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004395/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004396
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004397static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004398{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004399 int b;
4400
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004401 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4402
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004403 mutex_init(&brightness_mutex);
4404
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004405 /*
4406 * We always attempt to detect acpi support, so as to switch
4407 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4408 * going to publish a backlight interface
4409 */
4410 b = tpacpi_check_std_acpi_brightness_support();
4411 if (b > 0) {
4412 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4413 printk(TPACPI_NOTICE
4414 "Lenovo BIOS switched to ACPI backlight "
4415 "control mode\n");
4416 }
4417 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004418 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004419 "standard ACPI backlight interface "
4420 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004421 return 1;
4422 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004423 }
4424
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004425 if (!brightness_enable) {
4426 dbg_printk(TPACPI_DBG_INIT,
4427 "brightness support disabled by "
4428 "module parameter\n");
4429 return 1;
4430 }
4431
4432 if (b > 16) {
4433 printk(TPACPI_ERR
4434 "Unsupported brightness interface, "
4435 "please contact %s\n", TPACPI_MAIL);
4436 return 1;
4437 }
4438 if (b == 16)
4439 tp_features.bright_16levels = 1;
4440
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004441 if (!brightness_mode) {
4442 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4443 brightness_mode = 2;
4444 else
4445 brightness_mode = 3;
4446
4447 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4448 brightness_mode);
4449 }
4450
4451 if (brightness_mode > 3)
4452 return -EINVAL;
4453
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004454 b = brightness_get(NULL);
4455 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004456 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004457
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004458 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004459 printk(TPACPI_INFO
4460 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004461
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004462 ibm_backlight_device = backlight_device_register(
4463 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4464 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004465 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004466 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004467 return PTR_ERR(ibm_backlight_device);
4468 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004469 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004470
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004471 ibm_backlight_device->props.max_brightness =
4472 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004473 ibm_backlight_device->props.brightness = b;
4474 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004475
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004476 return 0;
4477}
4478
4479static void brightness_exit(void)
4480{
4481 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004482 vdbg_printk(TPACPI_DBG_EXIT,
4483 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004484 backlight_device_unregister(ibm_backlight_device);
4485 ibm_backlight_device = NULL;
4486 }
4487}
4488
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004489static int brightness_read(char *p)
4490{
4491 int len = 0;
4492 int level;
4493
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004494 level = brightness_get(NULL);
4495 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004496 len += sprintf(p + len, "level:\t\tunreadable\n");
4497 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004498 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004499 len += sprintf(p + len, "commands:\tup, down\n");
4500 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004501 " (<level> is 0-%d)\n",
4502 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004503 }
4504
4505 return len;
4506}
4507
4508static int brightness_write(char *buf)
4509{
4510 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004511 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004512 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004513 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004514
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004515 level = brightness_get(NULL);
4516 if (level < 0)
4517 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004518
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004519 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004520 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004521 if (level < max_level)
4522 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004523 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004524 if (level > 0)
4525 level--;
4526 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4527 level >= 0 && level <= max_level) {
4528 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004529 } else
4530 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004531 }
4532
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004533 /*
4534 * Now we know what the final level should be, so we try to set it.
4535 * Doing it this way makes the syscall restartable in case of EINTR
4536 */
4537 rc = brightness_set(level);
4538 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004539}
4540
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004541static struct ibm_struct brightness_driver_data = {
4542 .name = "brightness",
4543 .read = brightness_read,
4544 .write = brightness_write,
4545 .exit = brightness_exit,
4546};
4547
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004548/*************************************************************************
4549 * Volume subdriver
4550 */
4551
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004552static int volume_offset = 0x30;
4553
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004554static int volume_read(char *p)
4555{
4556 int len = 0;
4557 u8 level;
4558
4559 if (!acpi_ec_read(volume_offset, &level)) {
4560 len += sprintf(p + len, "level:\t\tunreadable\n");
4561 } else {
4562 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4563 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4564 len += sprintf(p + len, "commands:\tup, down, mute\n");
4565 len += sprintf(p + len, "commands:\tlevel <level>"
4566 " (<level> is 0-15)\n");
4567 }
4568
4569 return len;
4570}
4571
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004572static int volume_write(char *buf)
4573{
4574 int cmos_cmd, inc, i;
4575 u8 level, mute;
4576 int new_level, new_mute;
4577 char *cmd;
4578
4579 while ((cmd = next_cmd(&buf))) {
4580 if (!acpi_ec_read(volume_offset, &level))
4581 return -EIO;
4582 new_mute = mute = level & 0x40;
4583 new_level = level = level & 0xf;
4584
4585 if (strlencmp(cmd, "up") == 0) {
4586 if (mute)
4587 new_mute = 0;
4588 else
4589 new_level = level == 15 ? 15 : level + 1;
4590 } else if (strlencmp(cmd, "down") == 0) {
4591 if (mute)
4592 new_mute = 0;
4593 else
4594 new_level = level == 0 ? 0 : level - 1;
4595 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4596 new_level >= 0 && new_level <= 15) {
4597 /* new_level set */
4598 } else if (strlencmp(cmd, "mute") == 0) {
4599 new_mute = 0x40;
4600 } else
4601 return -EINVAL;
4602
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004603 if (new_level != level) {
4604 /* mute doesn't change */
4605
4606 cmos_cmd = (new_level > level) ?
4607 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004608 inc = new_level > level ? 1 : -1;
4609
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004610 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004611 !acpi_ec_write(volume_offset, level)))
4612 return -EIO;
4613
4614 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004615 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004616 !acpi_ec_write(volume_offset, i + inc))
4617 return -EIO;
4618
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004619 if (mute &&
4620 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4621 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004622 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004623 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004624 }
4625
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004626 if (new_mute != mute) {
4627 /* level doesn't change */
4628
4629 cmos_cmd = (new_mute) ?
4630 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004631
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004632 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004633 !acpi_ec_write(volume_offset, level + new_mute))
4634 return -EIO;
4635 }
4636 }
4637
4638 return 0;
4639}
4640
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004641static struct ibm_struct volume_driver_data = {
4642 .name = "volume",
4643 .read = volume_read,
4644 .write = volume_write,
4645};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004646
4647/*************************************************************************
4648 * Fan subdriver
4649 */
4650
4651/*
4652 * FAN ACCESS MODES
4653 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004654 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004655 * ACPI GFAN method: returns fan level
4656 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004657 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004658 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004659 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004660 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004661 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4662 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004663 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4664 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004665 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004666 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004667 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4668 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004669 *
4670 * Fan speed changes of any sort (including those caused by the
4671 * disengaged mode) are usually done slowly by the firmware as the
4672 * maximum ammount of fan duty cycle change per second seems to be
4673 * limited.
4674 *
4675 * Reading is not available if GFAN exists.
4676 * Writing is not available if SFAN exists.
4677 *
4678 * Bits
4679 * 7 automatic mode engaged;
4680 * (default operation mode of the ThinkPad)
4681 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004682 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004683 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004684 * the tachometer while the fan controller ramps up
4685 * the speed (which can take up to a few *minutes*).
4686 * Speeds up fan to 100% duty-cycle, which is far above
4687 * the standard RPM levels. It is not impossible that
4688 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004689 * 5-3 unused in some models. Extra bits for fan level
4690 * in others, but still useless as all values above
4691 * 7 map to the same speed as level 7 in these models.
4692 * 2-0 fan level (0..7 usually)
4693 * 0x00 = stop
4694 * 0x07 = max (set when temperatures critical)
4695 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004696 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004697 *
4698 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4699 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4700 * does so, its initial value is meaningless (0x07).
4701 *
4702 * For firmware bugs, refer to:
4703 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4704 *
4705 * ----
4706 *
4707 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4708 * Main fan tachometer reading (in RPM)
4709 *
4710 * This register is present on all ThinkPads with a new-style EC, and
4711 * it is known not to be present on the A21m/e, and T22, as there is
4712 * something else in offset 0x84 according to the ACPI DSDT. Other
4713 * ThinkPads from this same time period (and earlier) probably lack the
4714 * tachometer as well.
4715 *
4716 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4717 * was never fixed by IBM to report the EC firmware version string
4718 * probably support the tachometer (like the early X models), so
4719 * detecting it is quite hard. We need more data to know for sure.
4720 *
4721 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4722 * might result.
4723 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004724 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4725 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004726 *
4727 * For firmware bugs, refer to:
4728 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4729 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004730 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004731 * ThinkPad X31, X40, X41. Not available in the X60.
4732 *
4733 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4734 * high speed. ACPI DSDT seems to map these three speeds to levels
4735 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4736 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4737 *
4738 * The speeds are stored on handles
4739 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4740 *
4741 * There are three default speed sets, acessible as handles:
4742 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4743 *
4744 * ACPI DSDT switches which set is in use depending on various
4745 * factors.
4746 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004747 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004748 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4749 * but the ACPI tables just mention level 7.
4750 */
4751
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004752enum { /* Fan control constants */
4753 fan_status_offset = 0x2f, /* EC register 0x2f */
4754 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4755 * 0x84 must be read before 0x85 */
4756
4757 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4758 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4759
4760 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4761};
4762
4763enum fan_status_access_mode {
4764 TPACPI_FAN_NONE = 0, /* No fan status or control */
4765 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4766 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4767};
4768
4769enum fan_control_access_mode {
4770 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4771 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4772 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4773 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4774};
4775
4776enum fan_control_commands {
4777 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4778 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4779 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4780 * and also watchdog cmd */
4781};
4782
4783static int fan_control_allowed;
4784
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004785static enum fan_status_access_mode fan_status_access_mode;
4786static enum fan_control_access_mode fan_control_access_mode;
4787static enum fan_control_commands fan_control_commands;
4788
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004789static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004790static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004791static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004792
4793static struct mutex fan_mutex;
4794
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004795static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004796static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004797
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004798TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4799TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004800 "\\FSPD", /* 600e/x, 770e, 770x */
4801 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004802TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004803 "JFNS", /* 770x-JL */
4804 ); /* all others */
4805
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004806/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004807 * Call with fan_mutex held
4808 */
4809static void fan_update_desired_level(u8 status)
4810{
4811 if ((status &
4812 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4813 if (status > 7)
4814 fan_control_desired_level = 7;
4815 else
4816 fan_control_desired_level = status;
4817 }
4818}
4819
4820static int fan_get_status(u8 *status)
4821{
4822 u8 s;
4823
4824 /* TODO:
4825 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4826
4827 switch (fan_status_access_mode) {
4828 case TPACPI_FAN_RD_ACPI_GFAN:
4829 /* 570, 600e/x, 770e, 770x */
4830
4831 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4832 return -EIO;
4833
4834 if (likely(status))
4835 *status = s & 0x07;
4836
4837 break;
4838
4839 case TPACPI_FAN_RD_TPEC:
4840 /* all except 570, 600e/x, 770e, 770x */
4841 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4842 return -EIO;
4843
4844 if (likely(status))
4845 *status = s;
4846
4847 break;
4848
4849 default:
4850 return -ENXIO;
4851 }
4852
4853 return 0;
4854}
4855
4856static int fan_get_status_safe(u8 *status)
4857{
4858 int rc;
4859 u8 s;
4860
4861 if (mutex_lock_interruptible(&fan_mutex))
4862 return -ERESTARTSYS;
4863 rc = fan_get_status(&s);
4864 if (!rc)
4865 fan_update_desired_level(s);
4866 mutex_unlock(&fan_mutex);
4867
4868 if (status)
4869 *status = s;
4870
4871 return rc;
4872}
4873
4874static int fan_get_speed(unsigned int *speed)
4875{
4876 u8 hi, lo;
4877
4878 switch (fan_status_access_mode) {
4879 case TPACPI_FAN_RD_TPEC:
4880 /* all except 570, 600e/x, 770e, 770x */
4881 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4882 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4883 return -EIO;
4884
4885 if (likely(speed))
4886 *speed = (hi << 8) | lo;
4887
4888 break;
4889
4890 default:
4891 return -ENXIO;
4892 }
4893
4894 return 0;
4895}
4896
4897static int fan_set_level(int level)
4898{
4899 if (!fan_control_allowed)
4900 return -EPERM;
4901
4902 switch (fan_control_access_mode) {
4903 case TPACPI_FAN_WR_ACPI_SFAN:
4904 if (level >= 0 && level <= 7) {
4905 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4906 return -EIO;
4907 } else
4908 return -EINVAL;
4909 break;
4910
4911 case TPACPI_FAN_WR_ACPI_FANS:
4912 case TPACPI_FAN_WR_TPEC:
4913 if ((level != TP_EC_FAN_AUTO) &&
4914 (level != TP_EC_FAN_FULLSPEED) &&
4915 ((level < 0) || (level > 7)))
4916 return -EINVAL;
4917
4918 /* safety net should the EC not support AUTO
4919 * or FULLSPEED mode bits and just ignore them */
4920 if (level & TP_EC_FAN_FULLSPEED)
4921 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01004922 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004923 level |= 4; /* safety min speed 4 */
4924
4925 if (!acpi_ec_write(fan_status_offset, level))
4926 return -EIO;
4927 else
4928 tp_features.fan_ctrl_status_undef = 0;
4929 break;
4930
4931 default:
4932 return -ENXIO;
4933 }
4934 return 0;
4935}
4936
4937static int fan_set_level_safe(int level)
4938{
4939 int rc;
4940
4941 if (!fan_control_allowed)
4942 return -EPERM;
4943
4944 if (mutex_lock_interruptible(&fan_mutex))
4945 return -ERESTARTSYS;
4946
4947 if (level == TPACPI_FAN_LAST_LEVEL)
4948 level = fan_control_desired_level;
4949
4950 rc = fan_set_level(level);
4951 if (!rc)
4952 fan_update_desired_level(level);
4953
4954 mutex_unlock(&fan_mutex);
4955 return rc;
4956}
4957
4958static int fan_set_enable(void)
4959{
4960 u8 s;
4961 int rc;
4962
4963 if (!fan_control_allowed)
4964 return -EPERM;
4965
4966 if (mutex_lock_interruptible(&fan_mutex))
4967 return -ERESTARTSYS;
4968
4969 switch (fan_control_access_mode) {
4970 case TPACPI_FAN_WR_ACPI_FANS:
4971 case TPACPI_FAN_WR_TPEC:
4972 rc = fan_get_status(&s);
4973 if (rc < 0)
4974 break;
4975
4976 /* Don't go out of emergency fan mode */
4977 if (s != 7) {
4978 s &= 0x07;
4979 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4980 }
4981
4982 if (!acpi_ec_write(fan_status_offset, s))
4983 rc = -EIO;
4984 else {
4985 tp_features.fan_ctrl_status_undef = 0;
4986 rc = 0;
4987 }
4988 break;
4989
4990 case TPACPI_FAN_WR_ACPI_SFAN:
4991 rc = fan_get_status(&s);
4992 if (rc < 0)
4993 break;
4994
4995 s &= 0x07;
4996
4997 /* Set fan to at least level 4 */
4998 s |= 4;
4999
5000 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005001 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005002 else
5003 rc = 0;
5004 break;
5005
5006 default:
5007 rc = -ENXIO;
5008 }
5009
5010 mutex_unlock(&fan_mutex);
5011 return rc;
5012}
5013
5014static int fan_set_disable(void)
5015{
5016 int rc;
5017
5018 if (!fan_control_allowed)
5019 return -EPERM;
5020
5021 if (mutex_lock_interruptible(&fan_mutex))
5022 return -ERESTARTSYS;
5023
5024 rc = 0;
5025 switch (fan_control_access_mode) {
5026 case TPACPI_FAN_WR_ACPI_FANS:
5027 case TPACPI_FAN_WR_TPEC:
5028 if (!acpi_ec_write(fan_status_offset, 0x00))
5029 rc = -EIO;
5030 else {
5031 fan_control_desired_level = 0;
5032 tp_features.fan_ctrl_status_undef = 0;
5033 }
5034 break;
5035
5036 case TPACPI_FAN_WR_ACPI_SFAN:
5037 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5038 rc = -EIO;
5039 else
5040 fan_control_desired_level = 0;
5041 break;
5042
5043 default:
5044 rc = -ENXIO;
5045 }
5046
5047
5048 mutex_unlock(&fan_mutex);
5049 return rc;
5050}
5051
5052static int fan_set_speed(int speed)
5053{
5054 int rc;
5055
5056 if (!fan_control_allowed)
5057 return -EPERM;
5058
5059 if (mutex_lock_interruptible(&fan_mutex))
5060 return -ERESTARTSYS;
5061
5062 rc = 0;
5063 switch (fan_control_access_mode) {
5064 case TPACPI_FAN_WR_ACPI_FANS:
5065 if (speed >= 0 && speed <= 65535) {
5066 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5067 speed, speed, speed))
5068 rc = -EIO;
5069 } else
5070 rc = -EINVAL;
5071 break;
5072
5073 default:
5074 rc = -ENXIO;
5075 }
5076
5077 mutex_unlock(&fan_mutex);
5078 return rc;
5079}
5080
5081static void fan_watchdog_reset(void)
5082{
5083 static int fan_watchdog_active;
5084
5085 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5086 return;
5087
5088 if (fan_watchdog_active)
5089 cancel_delayed_work(&fan_watchdog_task);
5090
5091 if (fan_watchdog_maxinterval > 0 &&
5092 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5093 fan_watchdog_active = 1;
5094 if (!schedule_delayed_work(&fan_watchdog_task,
5095 msecs_to_jiffies(fan_watchdog_maxinterval
5096 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005097 printk(TPACPI_ERR
5098 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005099 "watchdog will not trigger\n");
5100 }
5101 } else
5102 fan_watchdog_active = 0;
5103}
5104
5105static void fan_watchdog_fire(struct work_struct *ignored)
5106{
5107 int rc;
5108
5109 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5110 return;
5111
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005112 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005113 rc = fan_set_enable();
5114 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005115 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005116 "will try again later...\n", -rc);
5117 /* reschedule for later */
5118 fan_watchdog_reset();
5119 }
5120}
5121
5122/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005123 * SYSFS fan layout: hwmon compatible (device)
5124 *
5125 * pwm*_enable:
5126 * 0: "disengaged" mode
5127 * 1: manual mode
5128 * 2: native EC "auto" mode (recommended, hardware default)
5129 *
5130 * pwm*: set speed in manual mode, ignored otherwise.
5131 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5132 * interpolation.
5133 *
5134 * fan*_input: tachometer reading, RPM
5135 *
5136 *
5137 * SYSFS fan layout: extensions
5138 *
5139 * fan_watchdog (driver):
5140 * fan watchdog interval in seconds, 0 disables (default), max 120
5141 */
5142
5143/* sysfs fan pwm1_enable ----------------------------------------------- */
5144static ssize_t fan_pwm1_enable_show(struct device *dev,
5145 struct device_attribute *attr,
5146 char *buf)
5147{
5148 int res, mode;
5149 u8 status;
5150
5151 res = fan_get_status_safe(&status);
5152 if (res)
5153 return res;
5154
5155 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5156 if (status != fan_control_initial_status) {
5157 tp_features.fan_ctrl_status_undef = 0;
5158 } else {
5159 /* Return most likely status. In fact, it
5160 * might be the only possible status */
5161 status = TP_EC_FAN_AUTO;
5162 }
5163 }
5164
5165 if (status & TP_EC_FAN_FULLSPEED) {
5166 mode = 0;
5167 } else if (status & TP_EC_FAN_AUTO) {
5168 mode = 2;
5169 } else
5170 mode = 1;
5171
5172 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5173}
5174
5175static ssize_t fan_pwm1_enable_store(struct device *dev,
5176 struct device_attribute *attr,
5177 const char *buf, size_t count)
5178{
5179 unsigned long t;
5180 int res, level;
5181
5182 if (parse_strtoul(buf, 2, &t))
5183 return -EINVAL;
5184
5185 switch (t) {
5186 case 0:
5187 level = TP_EC_FAN_FULLSPEED;
5188 break;
5189 case 1:
5190 level = TPACPI_FAN_LAST_LEVEL;
5191 break;
5192 case 2:
5193 level = TP_EC_FAN_AUTO;
5194 break;
5195 case 3:
5196 /* reserved for software-controlled auto mode */
5197 return -ENOSYS;
5198 default:
5199 return -EINVAL;
5200 }
5201
5202 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005203 if (res == -ENXIO)
5204 return -EINVAL;
5205 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005206 return res;
5207
5208 fan_watchdog_reset();
5209
5210 return count;
5211}
5212
5213static struct device_attribute dev_attr_fan_pwm1_enable =
5214 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5215 fan_pwm1_enable_show, fan_pwm1_enable_store);
5216
5217/* sysfs fan pwm1 ------------------------------------------------------ */
5218static ssize_t fan_pwm1_show(struct device *dev,
5219 struct device_attribute *attr,
5220 char *buf)
5221{
5222 int res;
5223 u8 status;
5224
5225 res = fan_get_status_safe(&status);
5226 if (res)
5227 return res;
5228
5229 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5230 if (status != fan_control_initial_status) {
5231 tp_features.fan_ctrl_status_undef = 0;
5232 } else {
5233 status = TP_EC_FAN_AUTO;
5234 }
5235 }
5236
5237 if ((status &
5238 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5239 status = fan_control_desired_level;
5240
5241 if (status > 7)
5242 status = 7;
5243
5244 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5245}
5246
5247static ssize_t fan_pwm1_store(struct device *dev,
5248 struct device_attribute *attr,
5249 const char *buf, size_t count)
5250{
5251 unsigned long s;
5252 int rc;
5253 u8 status, newlevel;
5254
5255 if (parse_strtoul(buf, 255, &s))
5256 return -EINVAL;
5257
5258 /* scale down from 0-255 to 0-7 */
5259 newlevel = (s >> 5) & 0x07;
5260
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005261 if (mutex_lock_interruptible(&fan_mutex))
5262 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005263
5264 rc = fan_get_status(&status);
5265 if (!rc && (status &
5266 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5267 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005268 if (rc == -ENXIO)
5269 rc = -EINVAL;
5270 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005271 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005272 fan_watchdog_reset();
5273 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005274 }
5275
5276 mutex_unlock(&fan_mutex);
5277 return (rc)? rc : count;
5278}
5279
5280static struct device_attribute dev_attr_fan_pwm1 =
5281 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5282 fan_pwm1_show, fan_pwm1_store);
5283
5284/* sysfs fan fan1_input ------------------------------------------------ */
5285static ssize_t fan_fan1_input_show(struct device *dev,
5286 struct device_attribute *attr,
5287 char *buf)
5288{
5289 int res;
5290 unsigned int speed;
5291
5292 res = fan_get_speed(&speed);
5293 if (res < 0)
5294 return res;
5295
5296 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5297}
5298
5299static struct device_attribute dev_attr_fan_fan1_input =
5300 __ATTR(fan1_input, S_IRUGO,
5301 fan_fan1_input_show, NULL);
5302
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005303/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005304static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5305 char *buf)
5306{
5307 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5308}
5309
5310static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5311 const char *buf, size_t count)
5312{
5313 unsigned long t;
5314
5315 if (parse_strtoul(buf, 120, &t))
5316 return -EINVAL;
5317
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005318 if (!fan_control_allowed)
5319 return -EPERM;
5320
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005321 fan_watchdog_maxinterval = t;
5322 fan_watchdog_reset();
5323
5324 return count;
5325}
5326
5327static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5328 fan_fan_watchdog_show, fan_fan_watchdog_store);
5329
5330/* --------------------------------------------------------------------- */
5331static struct attribute *fan_attributes[] = {
5332 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5333 &dev_attr_fan_fan1_input.attr,
5334 NULL
5335};
5336
5337static const struct attribute_group fan_attr_group = {
5338 .attrs = fan_attributes,
5339};
5340
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005341static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005342{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005343 int rc;
5344
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005345 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5346
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005347 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005348 fan_status_access_mode = TPACPI_FAN_NONE;
5349 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005350 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005351 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005352 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005353 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005354
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005355 TPACPI_ACPIHANDLE_INIT(fans);
5356 TPACPI_ACPIHANDLE_INIT(gfan);
5357 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005358
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005359 if (gfan_handle) {
5360 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005361 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005362 } else {
5363 /* all other ThinkPads: note that even old-style
5364 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005365 if (likely(acpi_ec_read(fan_status_offset,
5366 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005367 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005368
5369 /* In some ThinkPads, neither the EC nor the ACPI
5370 * DSDT initialize the fan status, and it ends up
5371 * being set to 0x07 when it *could* be either
5372 * 0x07 or 0x80.
5373 *
5374 * Enable for TP-1Y (T43), TP-78 (R51e),
5375 * TP-76 (R52), TP-70 (T43, R52), which are known
5376 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005377 if (fan_control_initial_status == 0x07) {
5378 switch (thinkpad_id.ec_model) {
5379 case 0x5931: /* TP-1Y */
5380 case 0x3837: /* TP-78 */
5381 case 0x3637: /* TP-76 */
5382 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005383 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005384 "fan_init: initial fan status "
5385 "is unknown, assuming it is "
5386 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005387 tp_features.fan_ctrl_status_undef = 1;
5388 ;;
5389 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005390 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005391 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005392 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005393 "ThinkPad ACPI EC access misbehaving, "
5394 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005395 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005396 }
5397 }
5398
5399 if (sfan_handle) {
5400 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005401 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005402 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005403 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005404 } else {
5405 if (!gfan_handle) {
5406 /* gfan without sfan means no fan control */
5407 /* all other models implement TP EC 0x2f control */
5408
5409 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005410 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005411 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005412 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005413 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005414 TPACPI_FAN_CMD_SPEED |
5415 TPACPI_FAN_CMD_LEVEL |
5416 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005417 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005418 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005419 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005420 TPACPI_FAN_CMD_LEVEL |
5421 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005422 }
5423 }
5424 }
5425
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005426 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5427 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5428 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5429 fan_status_access_mode, fan_control_access_mode);
5430
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005431 /* fan control master switch */
5432 if (!fan_control_allowed) {
5433 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5434 fan_control_commands = 0;
5435 dbg_printk(TPACPI_DBG_INIT,
5436 "fan control features disabled by parameter\n");
5437 }
5438
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005439 /* update fan_control_desired_level */
5440 if (fan_status_access_mode != TPACPI_FAN_NONE)
5441 fan_get_status_safe(NULL);
5442
5443 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5444 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005445 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005446 &fan_attr_group);
5447 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005448 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005449 &driver_attr_fan_watchdog);
5450 if (rc < 0)
5451 return rc;
5452 return 0;
5453 } else
5454 return 1;
5455}
5456
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005457static void fan_exit(void)
5458{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005459 vdbg_printk(TPACPI_DBG_EXIT,
5460 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005461
5462 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005463 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005464 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5465 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005466
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005467 cancel_delayed_work(&fan_watchdog_task);
5468 flush_scheduled_work();
5469}
5470
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005471static int fan_read(char *p)
5472{
5473 int len = 0;
5474 int rc;
5475 u8 status;
5476 unsigned int speed = 0;
5477
5478 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005479 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005480 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005481 rc = fan_get_status_safe(&status);
5482 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005483 return rc;
5484
5485 len += sprintf(p + len, "status:\t\t%s\n"
5486 "level:\t\t%d\n",
5487 (status != 0) ? "enabled" : "disabled", status);
5488 break;
5489
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005490 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005491 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005492 rc = fan_get_status_safe(&status);
5493 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005494 return rc;
5495
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005496 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005497 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005498 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005499 else
5500 /* Return most likely status. In fact, it
5501 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005502 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005503 }
5504
5505 len += sprintf(p + len, "status:\t\t%s\n",
5506 (status != 0) ? "enabled" : "disabled");
5507
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005508 rc = fan_get_speed(&speed);
5509 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005510 return rc;
5511
5512 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5513
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005514 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005515 /* Disengaged mode takes precedence */
5516 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005517 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005518 len += sprintf(p + len, "level:\t\tauto\n");
5519 else
5520 len += sprintf(p + len, "level:\t\t%d\n", status);
5521 break;
5522
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005523 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005524 default:
5525 len += sprintf(p + len, "status:\t\tnot supported\n");
5526 }
5527
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005528 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005529 len += sprintf(p + len, "commands:\tlevel <level>");
5530
5531 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005532 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005533 len += sprintf(p + len, " (<level> is 0-7)\n");
5534 break;
5535
5536 default:
5537 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005538 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005539 break;
5540 }
5541 }
5542
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005543 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005544 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005545 "commands:\twatchdog <timeout> (<timeout> "
5546 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005547
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005548 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005549 len += sprintf(p + len, "commands:\tspeed <speed>"
5550 " (<speed> is 0-65535)\n");
5551
5552 return len;
5553}
5554
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005555static int fan_write_cmd_level(const char *cmd, int *rc)
5556{
5557 int level;
5558
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005559 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005560 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005561 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005562 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005563 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005564 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005565 return 0;
5566
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005567 *rc = fan_set_level_safe(level);
5568 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005569 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005570 "access mode %d", fan_control_access_mode);
5571
5572 return 1;
5573}
5574
5575static int fan_write_cmd_enable(const char *cmd, int *rc)
5576{
5577 if (strlencmp(cmd, "enable") != 0)
5578 return 0;
5579
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005580 *rc = fan_set_enable();
5581 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005582 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005583 "access mode %d", fan_control_access_mode);
5584
5585 return 1;
5586}
5587
5588static int fan_write_cmd_disable(const char *cmd, int *rc)
5589{
5590 if (strlencmp(cmd, "disable") != 0)
5591 return 0;
5592
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005593 *rc = fan_set_disable();
5594 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005595 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005596 "access mode %d", fan_control_access_mode);
5597
5598 return 1;
5599}
5600
5601static int fan_write_cmd_speed(const char *cmd, int *rc)
5602{
5603 int speed;
5604
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005605 /* TODO:
5606 * Support speed <low> <medium> <high> ? */
5607
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005608 if (sscanf(cmd, "speed %d", &speed) != 1)
5609 return 0;
5610
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005611 *rc = fan_set_speed(speed);
5612 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005613 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005614 "access mode %d", fan_control_access_mode);
5615
5616 return 1;
5617}
5618
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005619static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5620{
5621 int interval;
5622
5623 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5624 return 0;
5625
5626 if (interval < 0 || interval > 120)
5627 *rc = -EINVAL;
5628 else
5629 fan_watchdog_maxinterval = interval;
5630
5631 return 1;
5632}
5633
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005634static int fan_write(char *buf)
5635{
5636 char *cmd;
5637 int rc = 0;
5638
5639 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005640 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005641 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005642 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005643 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005644 fan_write_cmd_disable(cmd, &rc) ||
5645 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005646 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005647 fan_write_cmd_speed(cmd, &rc))
5648 )
5649 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005650 else if (!rc)
5651 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005652 }
5653
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005654 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005655}
5656
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005657static struct ibm_struct fan_driver_data = {
5658 .name = "fan",
5659 .read = fan_read,
5660 .write = fan_write,
5661 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005662};
5663
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005664/****************************************************************************
5665 ****************************************************************************
5666 *
5667 * Infrastructure
5668 *
5669 ****************************************************************************
5670 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005672/* sysfs name ---------------------------------------------------------- */
5673static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5674 struct device_attribute *attr,
5675 char *buf)
5676{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005677 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005678}
5679
5680static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5681 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5682
5683/* --------------------------------------------------------------------- */
5684
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005685/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005686static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005687
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005688/*
5689 * Module and infrastructure proble, init and exit handling
5690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005692static int force_load;
5693
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005694#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005695static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005696{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005697 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005698
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005699 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005700}
5701#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5702
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005703static void ibm_exit(struct ibm_struct *ibm)
5704{
5705 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5706
5707 list_del_init(&ibm->all_drivers);
5708
5709 if (ibm->flags.acpi_notify_installed) {
5710 dbg_printk(TPACPI_DBG_EXIT,
5711 "%s: acpi_remove_notify_handler\n", ibm->name);
5712 BUG_ON(!ibm->acpi);
5713 acpi_remove_notify_handler(*ibm->acpi->handle,
5714 ibm->acpi->type,
5715 dispatch_acpi_notify);
5716 ibm->flags.acpi_notify_installed = 0;
5717 ibm->flags.acpi_notify_installed = 0;
5718 }
5719
5720 if (ibm->flags.proc_created) {
5721 dbg_printk(TPACPI_DBG_EXIT,
5722 "%s: remove_proc_entry\n", ibm->name);
5723 remove_proc_entry(ibm->name, proc_dir);
5724 ibm->flags.proc_created = 0;
5725 }
5726
5727 if (ibm->flags.acpi_driver_registered) {
5728 dbg_printk(TPACPI_DBG_EXIT,
5729 "%s: acpi_bus_unregister_driver\n", ibm->name);
5730 BUG_ON(!ibm->acpi);
5731 acpi_bus_unregister_driver(ibm->acpi->driver);
5732 kfree(ibm->acpi->driver);
5733 ibm->acpi->driver = NULL;
5734 ibm->flags.acpi_driver_registered = 0;
5735 }
5736
5737 if (ibm->flags.init_called && ibm->exit) {
5738 ibm->exit();
5739 ibm->flags.init_called = 0;
5740 }
5741
5742 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5743}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005744
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005745static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746{
5747 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005748 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 struct proc_dir_entry *entry;
5750
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005751 BUG_ON(ibm == NULL);
5752
5753 INIT_LIST_HEAD(&ibm->all_drivers);
5754
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005755 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 return 0;
5757
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005758 dbg_printk(TPACPI_DBG_INIT,
5759 "probing for %s\n", ibm->name);
5760
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005761 if (iibm->init) {
5762 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005763 if (ret > 0)
5764 return 0; /* probe failed */
5765 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005767
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005768 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 }
5770
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005771 if (ibm->acpi) {
5772 if (ibm->acpi->hid) {
5773 ret = register_tpacpi_subdriver(ibm);
5774 if (ret)
5775 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005776 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005777
5778 if (ibm->acpi->notify) {
5779 ret = setup_acpi_notify(ibm);
5780 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005781 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005782 ibm->name);
5783 ret = 0;
5784 goto err_out;
5785 }
5786 if (ret < 0)
5787 goto err_out;
5788 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005789 }
5790
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005791 dbg_printk(TPACPI_DBG_INIT,
5792 "%s installed\n", ibm->name);
5793
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005794 if (ibm->read) {
5795 entry = create_proc_entry(ibm->name,
5796 S_IFREG | S_IRUGO | S_IWUSR,
5797 proc_dir);
5798 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005799 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005800 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005801 ret = -ENODEV;
5802 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005803 }
5804 entry->owner = THIS_MODULE;
5805 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005806 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005807 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005808 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005809 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005812 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005815
5816err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005817 dbg_printk(TPACPI_DBG_INIT,
5818 "%s: at error exit path with result %d\n",
5819 ibm->name, ret);
5820
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005821 ibm_exit(ibm);
5822 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823}
5824
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005825/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005827static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005828{
Jeff Garzik18552562007-10-03 15:15:40 -04005829 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005830 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005831
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005832 if (!tp)
5833 return;
5834
5835 memset(tp, 0, sizeof(*tp));
5836
5837 if (dmi_name_in_vendors("IBM"))
5838 tp->vendor = PCI_VENDOR_ID_IBM;
5839 else if (dmi_name_in_vendors("LENOVO"))
5840 tp->vendor = PCI_VENDOR_ID_LENOVO;
5841 else
5842 return;
5843
5844 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5845 GFP_KERNEL);
5846 if (!tp->bios_version_str)
5847 return;
5848 tp->bios_model = tp->bios_version_str[0]
5849 | (tp->bios_version_str[1] << 8);
5850
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005851 /*
5852 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5853 * X32 or newer, all Z series; Some models must have an
5854 * up-to-date BIOS or they will not be detected.
5855 *
5856 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5857 */
5858 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005859 if (sscanf(dev->name,
5860 "IBM ThinkPad Embedded Controller -[%17c",
5861 ec_fw_string) == 1) {
5862 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5863 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005864
5865 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5866 tp->ec_model = ec_fw_string[0]
5867 | (ec_fw_string[1] << 8);
5868 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005869 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005870 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005871
5872 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5873 GFP_KERNEL);
5874 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5875 kfree(tp->model_str);
5876 tp->model_str = NULL;
5877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878}
5879
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005880static int __init probe_for_thinkpad(void)
5881{
5882 int is_thinkpad;
5883
5884 if (acpi_disabled)
5885 return -ENODEV;
5886
5887 /*
5888 * Non-ancient models have better DMI tagging, but very old models
5889 * don't.
5890 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005891 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005892
5893 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005894 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005895 if (!ec_handle) {
5896 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005897 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005898 "Not yet supported ThinkPad detected!\n");
5899 return -ENODEV;
5900 }
5901
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005902 /*
5903 * Risks a regression on very old machines, but reduces potential
5904 * false positives a damn great deal
5905 */
5906 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005907 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005908
5909 if (!is_thinkpad && !force_load)
5910 return -ENODEV;
5911
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005912 return 0;
5913}
5914
5915
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005916/* Module init, exit, parameters */
5917
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005918static struct ibm_init_struct ibms_init[] __initdata = {
5919 {
5920 .init = thinkpad_acpi_driver_init,
5921 .data = &thinkpad_acpi_driver_data,
5922 },
5923 {
5924 .init = hotkey_init,
5925 .data = &hotkey_driver_data,
5926 },
5927 {
5928 .init = bluetooth_init,
5929 .data = &bluetooth_driver_data,
5930 },
5931 {
5932 .init = wan_init,
5933 .data = &wan_driver_data,
5934 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005935#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005936 {
5937 .init = video_init,
5938 .data = &video_driver_data,
5939 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005940#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005941 {
5942 .init = light_init,
5943 .data = &light_driver_data,
5944 },
5945#ifdef CONFIG_THINKPAD_ACPI_DOCK
5946 {
5947 .init = dock_init,
5948 .data = &dock_driver_data[0],
5949 },
5950 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005951 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005952 .data = &dock_driver_data[1],
5953 },
5954#endif
5955#ifdef CONFIG_THINKPAD_ACPI_BAY
5956 {
5957 .init = bay_init,
5958 .data = &bay_driver_data,
5959 },
5960#endif
5961 {
5962 .init = cmos_init,
5963 .data = &cmos_driver_data,
5964 },
5965 {
5966 .init = led_init,
5967 .data = &led_driver_data,
5968 },
5969 {
5970 .init = beep_init,
5971 .data = &beep_driver_data,
5972 },
5973 {
5974 .init = thermal_init,
5975 .data = &thermal_driver_data,
5976 },
5977 {
5978 .data = &ecdump_driver_data,
5979 },
5980 {
5981 .init = brightness_init,
5982 .data = &brightness_driver_data,
5983 },
5984 {
5985 .data = &volume_driver_data,
5986 },
5987 {
5988 .init = fan_init,
5989 .data = &fan_driver_data,
5990 },
5991};
5992
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005993static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5994{
5995 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005996 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005997
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005998 if (!kp || !kp->name || !val)
5999 return -EINVAL;
6000
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006001 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6002 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006003 WARN_ON(ibm == NULL);
6004
6005 if (!ibm || !ibm->name)
6006 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006007
6008 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6009 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006010 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006011 strcpy(ibms_init[i].param, val);
6012 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006013 return 0;
6014 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006015 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006016
6017 return -EINVAL;
6018}
6019
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006020module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006021MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006022 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006023
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006024module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006025MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006026
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006027module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006028MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006029 "Attempts to load the driver even on a "
6030 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006031
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006032module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006033MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006034 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006035
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006036module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006037MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006038 "Selects brightness control strategy: "
6039 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006040
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006041module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006042MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006043 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006044
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006045module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006046MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006047 "used for backwards compatibility with userspace, "
6048 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006049
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006050#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006051 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006052 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006053 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006054
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006055TPACPI_PARAM(hotkey);
6056TPACPI_PARAM(bluetooth);
6057TPACPI_PARAM(video);
6058TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006059#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006060TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006061#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006062#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006063TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006064#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006065TPACPI_PARAM(cmos);
6066TPACPI_PARAM(led);
6067TPACPI_PARAM(beep);
6068TPACPI_PARAM(ecdump);
6069TPACPI_PARAM(brightness);
6070TPACPI_PARAM(volume);
6071TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006072
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006073static void thinkpad_acpi_module_exit(void)
6074{
6075 struct ibm_struct *ibm, *itmp;
6076
6077 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6078
6079 list_for_each_entry_safe_reverse(ibm, itmp,
6080 &tpacpi_all_drivers,
6081 all_drivers) {
6082 ibm_exit(ibm);
6083 }
6084
6085 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6086
6087 if (tpacpi_inputdev) {
6088 if (tp_features.input_device_registered)
6089 input_unregister_device(tpacpi_inputdev);
6090 else
6091 input_free_device(tpacpi_inputdev);
6092 }
6093
6094 if (tpacpi_hwmon)
6095 hwmon_device_unregister(tpacpi_hwmon);
6096
6097 if (tp_features.sensors_pdev_attrs_registered)
6098 device_remove_file(&tpacpi_sensors_pdev->dev,
6099 &dev_attr_thinkpad_acpi_pdev_name);
6100 if (tpacpi_sensors_pdev)
6101 platform_device_unregister(tpacpi_sensors_pdev);
6102 if (tpacpi_pdev)
6103 platform_device_unregister(tpacpi_pdev);
6104
6105 if (tp_features.sensors_pdrv_attrs_registered)
6106 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6107 if (tp_features.platform_drv_attrs_registered)
6108 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6109
6110 if (tp_features.sensors_pdrv_registered)
6111 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6112
6113 if (tp_features.platform_drv_registered)
6114 platform_driver_unregister(&tpacpi_pdriver);
6115
6116 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006117 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006118
6119 kfree(thinkpad_id.bios_version_str);
6120 kfree(thinkpad_id.ec_version_str);
6121 kfree(thinkpad_id.model_str);
6122}
6123
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006124
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006125static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126{
6127 int ret, i;
6128
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006129 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6130
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006131 /* Parameter checking */
6132 if (hotkey_report_mode > 2)
6133 return -EINVAL;
6134
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006135 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006136
6137 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006138 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006139 if (ret) {
6140 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006141 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006144 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006145
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006146 TPACPI_ACPIHANDLE_INIT(ecrd);
6147 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006149 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006151 printk(TPACPI_ERR
6152 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006153 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 return -ENODEV;
6155 }
6156 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006157
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006158 ret = platform_driver_register(&tpacpi_pdriver);
6159 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006160 printk(TPACPI_ERR
6161 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006162 thinkpad_acpi_module_exit();
6163 return ret;
6164 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006165 tp_features.platform_drv_registered = 1;
6166
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006167 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6168 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006169 printk(TPACPI_ERR
6170 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006171 thinkpad_acpi_module_exit();
6172 return ret;
6173 }
6174 tp_features.sensors_pdrv_registered = 1;
6175
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006176 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006177 if (!ret) {
6178 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006179 ret = tpacpi_create_driver_attributes(
6180 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006181 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006182 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006183 printk(TPACPI_ERR
6184 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006185 thinkpad_acpi_module_exit();
6186 return ret;
6187 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006188 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006189
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006190
6191 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006192 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006193 NULL, 0);
6194 if (IS_ERR(tpacpi_pdev)) {
6195 ret = PTR_ERR(tpacpi_pdev);
6196 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006197 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006198 thinkpad_acpi_module_exit();
6199 return ret;
6200 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006201 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006202 TPACPI_HWMON_DRVR_NAME,
6203 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006204 if (IS_ERR(tpacpi_sensors_pdev)) {
6205 ret = PTR_ERR(tpacpi_sensors_pdev);
6206 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006207 printk(TPACPI_ERR
6208 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006209 thinkpad_acpi_module_exit();
6210 return ret;
6211 }
6212 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6213 &dev_attr_thinkpad_acpi_pdev_name);
6214 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006215 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006216 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006217 thinkpad_acpi_module_exit();
6218 return ret;
6219 }
6220 tp_features.sensors_pdev_attrs_registered = 1;
6221 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006222 if (IS_ERR(tpacpi_hwmon)) {
6223 ret = PTR_ERR(tpacpi_hwmon);
6224 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006225 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006226 thinkpad_acpi_module_exit();
6227 return ret;
6228 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006229 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006230 tpacpi_inputdev = input_allocate_device();
6231 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006232 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006233 thinkpad_acpi_module_exit();
6234 return -ENOMEM;
6235 } else {
6236 /* Prepare input device, but don't register */
6237 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006238 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006239 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006240 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6241 thinkpad_id.vendor :
6242 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006243 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6244 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6245 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006246 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6247 ret = ibm_init(&ibms_init[i]);
6248 if (ret >= 0 && *ibms_init[i].param)
6249 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006251 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 return ret;
6253 }
6254 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006255 ret = input_register_device(tpacpi_inputdev);
6256 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006257 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006258 thinkpad_acpi_module_exit();
6259 return ret;
6260 } else {
6261 tp_features.input_device_registered = 1;
6262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006264 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 return 0;
6266}
6267
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006268/* Please remove this in year 2009 */
6269MODULE_ALIAS("ibm_acpi");
6270
6271/*
6272 * DMI matching for module autoloading
6273 *
6274 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6275 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6276 *
6277 * Only models listed in thinkwiki will be supported, so add yours
6278 * if it is not there yet.
6279 */
6280#define IBM_BIOS_MODULE_ALIAS(__type) \
6281 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6282
6283/* Non-ancient thinkpads */
6284MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6285MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6286
6287/* Ancient thinkpad BIOSes have to be identified by
6288 * BIOS type or model number, and there are far less
6289 * BIOS types than model numbers... */
6290IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6291IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6292IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6293
6294MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006295MODULE_DESCRIPTION(TPACPI_DESC);
6296MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006297MODULE_LICENSE("GPL");
6298
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006299module_init(thinkpad_acpi_module_init);
6300module_exit(thinkpad_acpi_module_exit);