blob: 2c85a2e10a2513e36453fefb6372d6889f9de65b [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
240struct thinkpad_id_data {
241 unsigned int vendor; /* ThinkPad vendor:
242 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
243
244 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
245 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
246
247 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
248 u16 ec_model;
249
250 char *model_str;
251};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252static struct thinkpad_id_data thinkpad_id;
253
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300254static enum {
255 TPACPI_LIFE_INIT = 0,
256 TPACPI_LIFE_RUNNING,
257 TPACPI_LIFE_EXITING,
258} tpacpi_lifecycle;
259
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200260static int experimental;
261static u32 dbg_level;
262
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300263/****************************************************************************
264 ****************************************************************************
265 *
266 * ACPI Helpers and device model
267 *
268 ****************************************************************************
269 ****************************************************************************/
270
271/*************************************************************************
272 * ACPI basic handles
273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300275static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200277#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 static acpi_handle object##_handle; \
279 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400280 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 static char *object##_paths[] = { paths }
282
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200283TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400284 "\\_SB.PCI.ISA.EC", /* 570 */
285 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
286 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
287 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
288 "\\_SB.PCI0.ICH3.EC0", /* R31 */
289 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300290 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200292TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
293TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200295TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
296 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400297 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
298 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300299 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400300
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200301TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400302 "^HKEY", /* R30, R31 */
303 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300304 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400305
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200306TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
307 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
308 "\\_SB.PCI0.VID0", /* 770e */
309 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
310 "\\_SB.PCI0.AGP.VID", /* all others */
311 ); /* R30, R31 */
312
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400313
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300314/*************************************************************************
315 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200316 */
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static int acpi_evalf(acpi_handle handle,
319 void *res, char *method, char *fmt, ...)
320{
321 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400322 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200323 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400324 struct acpi_buffer result, *resultp;
325 union acpi_object out_obj;
326 acpi_status status;
327 va_list ap;
328 char res_type;
329 int success;
330 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200333 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335 }
336
337 if (*fmt == 'q') {
338 quiet = 1;
339 fmt++;
340 } else
341 quiet = 0;
342
343 res_type = *(fmt++);
344
345 params.count = 0;
346 params.pointer = &in_objs[0];
347
348 va_start(ap, fmt);
349 while (*fmt) {
350 char c = *(fmt++);
351 switch (c) {
352 case 'd': /* int */
353 in_objs[params.count].integer.value = va_arg(ap, int);
354 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
355 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400356 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200358 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 "with invalid format character '%c'\n", c);
360 return 0;
361 }
362 }
363 va_end(ap);
364
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400365 if (res_type != 'v') {
366 result.length = sizeof(out_obj);
367 result.pointer = &out_obj;
368 resultp = &result;
369 } else
370 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400372 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400375 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (res)
377 *(int *)res = out_obj.integer.value;
378 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
379 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400380 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 success = status == AE_OK;
382 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400383 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200385 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 "with invalid format character '%c'\n", res_type);
387 return 0;
388 }
389
390 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200391 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 method, fmt0, status);
393
394 return success;
395}
396
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200397static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300398{
399 int v;
400
401 if (ecrd_handle) {
402 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
403 return 0;
404 *p = v;
405 } else {
406 if (ec_read(i, p) < 0)
407 return 0;
408 }
409
410 return 1;
411}
412
413static int acpi_ec_write(int i, u8 v)
414{
415 if (ecwr_handle) {
416 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
417 return 0;
418 } else {
419 if (ec_write(i, v) < 0)
420 return 0;
421 }
422
423 return 1;
424}
425
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200426#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300427static int _sta(acpi_handle handle)
428{
429 int status;
430
431 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
432 status = 0;
433
434 return status;
435}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200436#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300437
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300438static int issue_thinkpad_cmos_command(int cmos_cmd)
439{
440 if (!cmos_handle)
441 return -ENXIO;
442
443 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
444 return -EIO;
445
446 return 0;
447}
448
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300449/*************************************************************************
450 * ACPI device model
451 */
452
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200453#define TPACPI_ACPIHANDLE_INIT(object) \
454 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200455 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
456
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300457static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300458 acpi_handle *handle, acpi_handle parent,
459 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300460{
461 int i;
462 acpi_status status;
463
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300464 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
465 name);
466
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300467 for (i = 0; i < num_paths; i++) {
468 status = acpi_get_handle(parent, paths[i], handle);
469 if (ACPI_SUCCESS(status)) {
470 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300471 dbg_printk(TPACPI_DBG_INIT,
472 "Found ACPI handle %s for %s\n",
473 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300474 return;
475 }
476 }
477
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300478 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
479 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300480 *handle = NULL;
481}
482
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300483static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300484{
485 struct ibm_struct *ibm = data;
486
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300487 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
488 return;
489
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300490 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300491 return;
492
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300493 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300494}
495
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300496static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300497{
498 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300499 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300500
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300501 BUG_ON(!ibm->acpi);
502
503 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504 return 0;
505
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300506 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300507 "setting up ACPI notify for %s\n", ibm->name);
508
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300509 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
510 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200511 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300512 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300513 return -ENODEV;
514 }
515
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300516 acpi_driver_data(ibm->acpi->device) = ibm;
517 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200518 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300519 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300520
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300521 status = acpi_install_notify_handler(*ibm->acpi->handle,
522 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523 if (ACPI_FAILURE(status)) {
524 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200525 printk(TPACPI_NOTICE
526 "another device driver is already "
527 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300528 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200529 printk(TPACPI_ERR
530 "acpi_install_notify_handler(%s) failed: %d\n",
531 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300532 }
533 return -ENODEV;
534 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300535 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536 return 0;
537}
538
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300539static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300540{
541 return 0;
542}
543
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300544static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300545{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300546 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300547
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300548 dbg_printk(TPACPI_DBG_INIT,
549 "registering %s as an ACPI driver\n", ibm->name);
550
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300551 BUG_ON(!ibm->acpi);
552
553 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
554 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200555 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300556 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300557 }
558
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200559 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300560 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200561
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300562 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300564 rc = acpi_bus_register_driver(ibm->acpi->driver);
565 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200566 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200567 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300568 kfree(ibm->acpi->driver);
569 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300570 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300571 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300572
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300573 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300574}
575
576
577/****************************************************************************
578 ****************************************************************************
579 *
580 * Procfs Helpers
581 *
582 ****************************************************************************
583 ****************************************************************************/
584
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300585static int dispatch_procfs_read(char *page, char **start, off_t off,
586 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300587{
588 struct ibm_struct *ibm = data;
589 int len;
590
591 if (!ibm || !ibm->read)
592 return -EINVAL;
593
594 len = ibm->read(page);
595 if (len < 0)
596 return len;
597
598 if (len <= off + count)
599 *eof = 1;
600 *start = page + off;
601 len -= off;
602 if (len > count)
603 len = count;
604 if (len < 0)
605 len = 0;
606
607 return len;
608}
609
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300610static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200611 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300612 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300613{
614 struct ibm_struct *ibm = data;
615 char *kernbuf;
616 int ret;
617
618 if (!ibm || !ibm->write)
619 return -EINVAL;
620
621 kernbuf = kmalloc(count + 2, GFP_KERNEL);
622 if (!kernbuf)
623 return -ENOMEM;
624
625 if (copy_from_user(kernbuf, userbuf, count)) {
626 kfree(kernbuf);
627 return -EFAULT;
628 }
629
630 kernbuf[count] = 0;
631 strcat(kernbuf, ",");
632 ret = ibm->write(kernbuf);
633 if (ret == 0)
634 ret = count;
635
636 kfree(kernbuf);
637
638 return ret;
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641static char *next_cmd(char **cmds)
642{
643 char *start = *cmds;
644 char *end;
645
646 while ((end = strchr(start, ',')) && end == start)
647 start = end + 1;
648
649 if (!end)
650 return NULL;
651
652 *end = 0;
653 *cmds = end + 1;
654 return start;
655}
656
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300657
658/****************************************************************************
659 ****************************************************************************
660 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300661 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300662 *
663 ****************************************************************************
664 ****************************************************************************/
665
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300666static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300667static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700668static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300669static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300670static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200671static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300672
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200673static int tpacpi_suspend_handler(struct platform_device *pdev,
674 pm_message_t state)
675{
676 struct ibm_struct *ibm, *itmp;
677
678 list_for_each_entry_safe(ibm, itmp,
679 &tpacpi_all_drivers,
680 all_drivers) {
681 if (ibm->suspend)
682 (ibm->suspend)(state);
683 }
684
685 return 0;
686}
687
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300688static int tpacpi_resume_handler(struct platform_device *pdev)
689{
690 struct ibm_struct *ibm, *itmp;
691
692 list_for_each_entry_safe(ibm, itmp,
693 &tpacpi_all_drivers,
694 all_drivers) {
695 if (ibm->resume)
696 (ibm->resume)();
697 }
698
699 return 0;
700}
701
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300702static struct platform_driver tpacpi_pdriver = {
703 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200704 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300705 .owner = THIS_MODULE,
706 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200707 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300708 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300709};
710
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300711static struct platform_driver tpacpi_hwmon_pdriver = {
712 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200713 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300714 .owner = THIS_MODULE,
715 },
716};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300717
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300718/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300719 * sysfs support helpers
720 */
721
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200722struct attribute_set {
723 unsigned int members, max_members;
724 struct attribute_group group;
725};
726
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300727struct attribute_set_obj {
728 struct attribute_set s;
729 struct attribute *a;
730} __attribute__((packed));
731
732static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200733 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300734{
735 struct attribute_set_obj *sobj;
736
737 if (max_members == 0)
738 return NULL;
739
740 /* Allocates space for implicit NULL at the end too */
741 sobj = kzalloc(sizeof(struct attribute_set_obj) +
742 max_members * sizeof(struct attribute *),
743 GFP_KERNEL);
744 if (!sobj)
745 return NULL;
746 sobj->s.max_members = max_members;
747 sobj->s.group.attrs = &sobj->a;
748 sobj->s.group.name = name;
749
750 return &sobj->s;
751}
752
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200753#define destroy_attr_set(_set) \
754 kfree(_set);
755
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300756/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200757static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300758{
759 if (!s || !attr)
760 return -EINVAL;
761
762 if (s->members >= s->max_members)
763 return -ENOMEM;
764
765 s->group.attrs[s->members] = attr;
766 s->members++;
767
768 return 0;
769}
770
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200771static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300772 struct attribute **attr,
773 unsigned int count)
774{
775 int i, res;
776
777 for (i = 0; i < count; i++) {
778 res = add_to_attr_set(s, attr[i]);
779 if (res)
780 return res;
781 }
782
783 return 0;
784}
785
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200786static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300787{
788 sysfs_remove_group(kobj, &s->group);
789 destroy_attr_set(s);
790}
791
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200792#define register_attr_set_with_sysfs(_attr_set, _kobj) \
793 sysfs_create_group(_kobj, &_attr_set->group)
794
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300795static int parse_strtoul(const char *buf,
796 unsigned long max, unsigned long *value)
797{
798 char *endp;
799
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300800 while (*buf && isspace(*buf))
801 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300802 *value = simple_strtoul(buf, &endp, 0);
803 while (*endp && isspace(*endp))
804 endp++;
805 if (*endp || *value > max)
806 return -EINVAL;
807
808 return 0;
809}
810
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300811static int __init tpacpi_query_bcl_levels(acpi_handle handle)
812{
813 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
814 union acpi_object *obj;
815 int rc;
816
817 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
818 obj = (union acpi_object *)buffer.pointer;
819 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
820 printk(TPACPI_ERR "Unknown _BCL data, "
821 "please report this to %s\n", TPACPI_MAIL);
822 rc = 0;
823 } else {
824 rc = obj->package.count;
825 }
826 } else {
827 return 0;
828 }
829
830 kfree(buffer.pointer);
831 return rc;
832}
833
834static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
835 u32 lvl, void *context, void **rv)
836{
837 char name[ACPI_PATH_SEGMENT_LENGTH];
838 struct acpi_buffer buffer = { sizeof(name), &name };
839
840 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
841 !strncmp("_BCL", name, sizeof(name) - 1)) {
842 BUG_ON(!rv || !*rv);
843 **(int **)rv = tpacpi_query_bcl_levels(handle);
844 return AE_CTRL_TERMINATE;
845 } else {
846 return AE_OK;
847 }
848}
849
850/*
851 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
852 */
853static int __init tpacpi_check_std_acpi_brightness_support(void)
854{
855 int status;
856 int bcl_levels = 0;
857 void *bcl_ptr = &bcl_levels;
858
859 if (!vid_handle) {
860 TPACPI_ACPIHANDLE_INIT(vid);
861 }
862 if (!vid_handle)
863 return 0;
864
865 /*
866 * Search for a _BCL method, and execute it. This is safe on all
867 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
868 * BIOS in ACPI backlight control mode. We do NOT have to care
869 * about calling the _BCL method in an enabled video device, any
870 * will do for our purposes.
871 */
872
873 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
874 tpacpi_acpi_walk_find_bcl, NULL,
875 &bcl_ptr);
876
877 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
878 tp_features.bright_acpimode = 1;
879 return (bcl_levels - 2);
880 }
881
882 return 0;
883}
884
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200885/*************************************************************************
886 * thinkpad-acpi driver attributes
887 */
888
889/* interface_version --------------------------------------------------- */
890static ssize_t tpacpi_driver_interface_version_show(
891 struct device_driver *drv,
892 char *buf)
893{
894 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
895}
896
897static DRIVER_ATTR(interface_version, S_IRUGO,
898 tpacpi_driver_interface_version_show, NULL);
899
900/* debug_level --------------------------------------------------------- */
901static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
902 char *buf)
903{
904 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
905}
906
907static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
908 const char *buf, size_t count)
909{
910 unsigned long t;
911
912 if (parse_strtoul(buf, 0xffff, &t))
913 return -EINVAL;
914
915 dbg_level = t;
916
917 return count;
918}
919
920static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
921 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
922
923/* version ------------------------------------------------------------- */
924static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
925 char *buf)
926{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200927 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
928 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200929}
930
931static DRIVER_ATTR(version, S_IRUGO,
932 tpacpi_driver_version_show, NULL);
933
934/* --------------------------------------------------------------------- */
935
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200936static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200937 &driver_attr_debug_level, &driver_attr_version,
938 &driver_attr_interface_version,
939};
940
941static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
942{
943 int i, res;
944
945 i = 0;
946 res = 0;
947 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
948 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
949 i++;
950 }
951
952 return res;
953}
954
955static void tpacpi_remove_driver_attributes(struct device_driver *drv)
956{
957 int i;
958
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200959 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200960 driver_remove_file(drv, tpacpi_driver_attributes[i]);
961}
962
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300963/****************************************************************************
964 ****************************************************************************
965 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300966 * Subdrivers
967 *
968 ****************************************************************************
969 ****************************************************************************/
970
971/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300972 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300973 */
974
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300975static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200977 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
978 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200980 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300981 (thinkpad_id.bios_version_str) ?
982 thinkpad_id.bios_version_str : "unknown",
983 (thinkpad_id.ec_version_str) ?
984 thinkpad_id.ec_version_str : "unknown");
985
986 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200987 printk(TPACPI_INFO "%s %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300988 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
989 "IBM" : ((thinkpad_id.vendor ==
990 PCI_VENDOR_ID_LENOVO) ?
991 "Lenovo" : "Unknown vendor"),
992 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 0;
995}
996
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300997static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 int len = 0;
1000
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001001 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1002 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 return len;
1005}
1006
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001007static struct ibm_struct thinkpad_acpi_driver_data = {
1008 .name = "driver",
1009 .read = thinkpad_acpi_driver_read,
1010};
1011
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001012/*************************************************************************
1013 * Hotkey subdriver
1014 */
1015
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001016enum { /* hot key scan codes (derived from ACPI DSDT) */
1017 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1018 TP_ACPI_HOTKEYSCAN_FNF2,
1019 TP_ACPI_HOTKEYSCAN_FNF3,
1020 TP_ACPI_HOTKEYSCAN_FNF4,
1021 TP_ACPI_HOTKEYSCAN_FNF5,
1022 TP_ACPI_HOTKEYSCAN_FNF6,
1023 TP_ACPI_HOTKEYSCAN_FNF7,
1024 TP_ACPI_HOTKEYSCAN_FNF8,
1025 TP_ACPI_HOTKEYSCAN_FNF9,
1026 TP_ACPI_HOTKEYSCAN_FNF10,
1027 TP_ACPI_HOTKEYSCAN_FNF11,
1028 TP_ACPI_HOTKEYSCAN_FNF12,
1029 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1030 TP_ACPI_HOTKEYSCAN_FNINSERT,
1031 TP_ACPI_HOTKEYSCAN_FNDELETE,
1032 TP_ACPI_HOTKEYSCAN_FNHOME,
1033 TP_ACPI_HOTKEYSCAN_FNEND,
1034 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1035 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1036 TP_ACPI_HOTKEYSCAN_FNSPACE,
1037 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1038 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1039 TP_ACPI_HOTKEYSCAN_MUTE,
1040 TP_ACPI_HOTKEYSCAN_THINKPAD,
1041};
1042
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001043enum { /* Keys available through NVRAM polling */
1044 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1045 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1046};
1047
1048enum { /* Positions of some of the keys in hotkey masks */
1049 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1050 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1051 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1052 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1053 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1054 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1055 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1056 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1057 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1058 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1059 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1060};
1061
1062enum { /* NVRAM to ACPI HKEY group map */
1063 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1064 TP_ACPI_HKEY_ZOOM_MASK |
1065 TP_ACPI_HKEY_DISPSWTCH_MASK |
1066 TP_ACPI_HKEY_HIBERNATE_MASK,
1067 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1068 TP_ACPI_HKEY_BRGHTDWN_MASK,
1069 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1070 TP_ACPI_HKEY_VOLDWN_MASK |
1071 TP_ACPI_HKEY_MUTE_MASK,
1072};
1073
1074#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1075struct tp_nvram_state {
1076 u16 thinkpad_toggle:1;
1077 u16 zoom_toggle:1;
1078 u16 display_toggle:1;
1079 u16 thinklight_toggle:1;
1080 u16 hibernate_toggle:1;
1081 u16 displayexp_toggle:1;
1082 u16 display_state:1;
1083 u16 brightness_toggle:1;
1084 u16 volume_toggle:1;
1085 u16 mute:1;
1086
1087 u8 brightness_level;
1088 u8 volume_level;
1089};
1090
1091static struct task_struct *tpacpi_hotkey_task;
1092static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1093static int hotkey_poll_freq = 10; /* Hz */
1094static struct mutex hotkey_thread_mutex;
1095static struct mutex hotkey_thread_data_mutex;
1096static unsigned int hotkey_config_change;
1097
1098#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1099
1100#define hotkey_source_mask 0U
1101
1102#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1103
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001104static struct mutex hotkey_mutex;
1105
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001106static enum { /* Reasons for waking up */
1107 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1108 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1109 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1110} hotkey_wakeup_reason;
1111
1112static int hotkey_autosleep_ack;
1113
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001114static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001115static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001116static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001117static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001118static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001119
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001120static unsigned int hotkey_report_mode;
1121
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001122static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001123
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001124static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001125
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001126#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1127#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001128 do { \
1129 mutex_lock(&hotkey_thread_data_mutex); \
1130 hotkey_config_change++; \
1131 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001132#define HOTKEY_CONFIG_CRITICAL_END \
1133 mutex_unlock(&hotkey_thread_data_mutex);
1134#else
1135#define HOTKEY_CONFIG_CRITICAL_START
1136#define HOTKEY_CONFIG_CRITICAL_END
1137#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1138
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001139/* HKEY.MHKG() return bits */
1140#define TP_HOTKEY_TABLET_MASK (1 << 3)
1141
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001142static int hotkey_get_wlsw(int *status)
1143{
1144 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1145 return -EIO;
1146 return 0;
1147}
1148
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001149static int hotkey_get_tablet_mode(int *status)
1150{
1151 int s;
1152
1153 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1154 return -EIO;
1155
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001156 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1157 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001158}
1159
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001160/*
1161 * Call with hotkey_mutex held
1162 */
1163static int hotkey_mask_get(void)
1164{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001165 u32 m = 0;
1166
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001167 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001168 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001169 return -EIO;
1170 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001171 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001172
1173 return 0;
1174}
1175
1176/*
1177 * Call with hotkey_mutex held
1178 */
1179static int hotkey_mask_set(u32 mask)
1180{
1181 int i;
1182 int rc = 0;
1183
1184 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001185 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001186 for (i = 0; i < 32; i++) {
1187 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001188 /* enable in firmware mask only keys not in NVRAM
1189 * mode, but enable the key in the cached hotkey_mask
1190 * regardless of mode, or the key will end up
1191 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001192 if (!acpi_evalf(hkey_handle,
1193 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001194 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001195 rc = -EIO;
1196 break;
1197 } else {
1198 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1199 }
1200 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001201 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001202
1203 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001204 if (!hotkey_mask_get() && !rc &&
1205 (hotkey_mask & ~hotkey_source_mask) !=
1206 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001207 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001208 "requested hot key mask 0x%08x, but "
1209 "firmware forced it to 0x%08x\n",
1210 mask, hotkey_mask);
1211 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001212 } else {
1213#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1214 HOTKEY_CONFIG_CRITICAL_START
1215 hotkey_mask = mask & hotkey_source_mask;
1216 HOTKEY_CONFIG_CRITICAL_END
1217 hotkey_mask_get();
1218 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001219 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001220 "requested hot key mask 0x%08x, "
1221 "forced to 0x%08x (NVRAM poll mask is "
1222 "0x%08x): no firmware mask support\n",
1223 mask, hotkey_mask, hotkey_source_mask);
1224 }
1225#else
1226 hotkey_mask_get();
1227 rc = -ENXIO;
1228#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001229 }
1230
1231 return rc;
1232}
1233
1234static int hotkey_status_get(int *status)
1235{
1236 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1237 return -EIO;
1238
1239 return 0;
1240}
1241
1242static int hotkey_status_set(int status)
1243{
1244 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1245 return -EIO;
1246
1247 return 0;
1248}
1249
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001250static void tpacpi_input_send_radiosw(void)
1251{
1252 int wlsw;
1253
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001254 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001255 mutex_lock(&tpacpi_inputdev_send_mutex);
1256
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001257 input_report_switch(tpacpi_inputdev,
1258 SW_RADIO, !!wlsw);
1259 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001260
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001261 mutex_unlock(&tpacpi_inputdev_send_mutex);
1262 }
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001263}
1264
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001265static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001266{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001267 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001268
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001269 if (tp_features.hotkey_tablet &&
1270 !hotkey_get_tablet_mode(&state)) {
1271 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001272
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001273 input_report_switch(tpacpi_inputdev,
1274 SW_TABLET_MODE, !!state);
1275 input_sync(tpacpi_inputdev);
1276
1277 mutex_unlock(&tpacpi_inputdev_send_mutex);
1278 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001279}
1280
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001281static void tpacpi_input_send_key(unsigned int scancode)
1282{
1283 unsigned int keycode;
1284
1285 keycode = hotkey_keycode_map[scancode];
1286
1287 if (keycode != KEY_RESERVED) {
1288 mutex_lock(&tpacpi_inputdev_send_mutex);
1289
1290 input_report_key(tpacpi_inputdev, keycode, 1);
1291 if (keycode == KEY_UNKNOWN)
1292 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1293 scancode);
1294 input_sync(tpacpi_inputdev);
1295
1296 input_report_key(tpacpi_inputdev, keycode, 0);
1297 if (keycode == KEY_UNKNOWN)
1298 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1299 scancode);
1300 input_sync(tpacpi_inputdev);
1301
1302 mutex_unlock(&tpacpi_inputdev_send_mutex);
1303 }
1304}
1305
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001306#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1307static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1308
1309static void tpacpi_hotkey_send_key(unsigned int scancode)
1310{
1311 tpacpi_input_send_key(scancode);
1312 if (hotkey_report_mode < 2) {
1313 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1314 0x80, 0x1001 + scancode);
1315 }
1316}
1317
1318static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1319{
1320 u8 d;
1321
1322 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1323 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1324 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1325 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1326 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1327 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1328 }
1329 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1330 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1331 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1332 }
1333 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1334 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1335 n->displayexp_toggle =
1336 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1337 }
1338 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1339 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1340 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1341 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1342 n->brightness_toggle =
1343 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1344 }
1345 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1346 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1347 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1348 >> TP_NVRAM_POS_LEVEL_VOLUME;
1349 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1350 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1351 }
1352}
1353
1354#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001355 do { \
1356 if ((mask & (1 << __scancode)) && \
1357 oldn->__member != newn->__member) \
1358 tpacpi_hotkey_send_key(__scancode); \
1359 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001360
1361#define TPACPI_MAY_SEND_KEY(__scancode) \
1362 do { if (mask & (1 << __scancode)) \
1363 tpacpi_hotkey_send_key(__scancode); } while (0)
1364
1365static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001366 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001367 u32 mask)
1368{
1369 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1370 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1371 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1372 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1373
1374 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1375
1376 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1377
1378 /* handle volume */
1379 if (oldn->volume_toggle != newn->volume_toggle) {
1380 if (oldn->mute != newn->mute) {
1381 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1382 }
1383 if (oldn->volume_level > newn->volume_level) {
1384 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1385 } else if (oldn->volume_level < newn->volume_level) {
1386 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1387 } else if (oldn->mute == newn->mute) {
1388 /* repeated key presses that didn't change state */
1389 if (newn->mute) {
1390 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1391 } else if (newn->volume_level != 0) {
1392 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1393 } else {
1394 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1395 }
1396 }
1397 }
1398
1399 /* handle brightness */
1400 if (oldn->brightness_toggle != newn->brightness_toggle) {
1401 if (oldn->brightness_level < newn->brightness_level) {
1402 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1403 } else if (oldn->brightness_level > newn->brightness_level) {
1404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1405 } else {
1406 /* repeated key presses that didn't change state */
1407 if (newn->brightness_level != 0) {
1408 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1409 } else {
1410 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1411 }
1412 }
1413 }
1414}
1415
1416#undef TPACPI_COMPARE_KEY
1417#undef TPACPI_MAY_SEND_KEY
1418
1419static int hotkey_kthread(void *data)
1420{
1421 struct tp_nvram_state s[2];
1422 u32 mask;
1423 unsigned int si, so;
1424 unsigned long t;
1425 unsigned int change_detector, must_reset;
1426
1427 mutex_lock(&hotkey_thread_mutex);
1428
1429 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1430 goto exit;
1431
1432 set_freezable();
1433
1434 so = 0;
1435 si = 1;
1436 t = 0;
1437
1438 /* Initial state for compares */
1439 mutex_lock(&hotkey_thread_data_mutex);
1440 change_detector = hotkey_config_change;
1441 mask = hotkey_source_mask & hotkey_mask;
1442 mutex_unlock(&hotkey_thread_data_mutex);
1443 hotkey_read_nvram(&s[so], mask);
1444
1445 while (!kthread_should_stop() && hotkey_poll_freq) {
1446 if (t == 0)
1447 t = 1000/hotkey_poll_freq;
1448 t = msleep_interruptible(t);
1449 if (unlikely(kthread_should_stop()))
1450 break;
1451 must_reset = try_to_freeze();
1452 if (t > 0 && !must_reset)
1453 continue;
1454
1455 mutex_lock(&hotkey_thread_data_mutex);
1456 if (must_reset || hotkey_config_change != change_detector) {
1457 /* forget old state on thaw or config change */
1458 si = so;
1459 t = 0;
1460 change_detector = hotkey_config_change;
1461 }
1462 mask = hotkey_source_mask & hotkey_mask;
1463 mutex_unlock(&hotkey_thread_data_mutex);
1464
1465 if (likely(mask)) {
1466 hotkey_read_nvram(&s[si], mask);
1467 if (likely(si != so)) {
1468 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001469 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001470 }
1471 }
1472
1473 so = si;
1474 si ^= 1;
1475 }
1476
1477exit:
1478 mutex_unlock(&hotkey_thread_mutex);
1479 return 0;
1480}
1481
1482static void hotkey_poll_stop_sync(void)
1483{
1484 if (tpacpi_hotkey_task) {
1485 if (frozen(tpacpi_hotkey_task) ||
1486 freezing(tpacpi_hotkey_task))
1487 thaw_process(tpacpi_hotkey_task);
1488
1489 kthread_stop(tpacpi_hotkey_task);
1490 tpacpi_hotkey_task = NULL;
1491 mutex_lock(&hotkey_thread_mutex);
1492 /* at this point, the thread did exit */
1493 mutex_unlock(&hotkey_thread_mutex);
1494 }
1495}
1496
1497/* call with hotkey_mutex held */
1498static void hotkey_poll_setup(int may_warn)
1499{
1500 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1501 hotkey_poll_freq > 0 &&
1502 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1503 if (!tpacpi_hotkey_task) {
1504 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001505 NULL,
1506 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001507 if (IS_ERR(tpacpi_hotkey_task)) {
1508 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001509 printk(TPACPI_ERR
1510 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001511 "for hotkey polling\n");
1512 }
1513 }
1514 } else {
1515 hotkey_poll_stop_sync();
1516 if (may_warn &&
1517 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001518 printk(TPACPI_NOTICE
1519 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001520 "which is currently disabled\n",
1521 hotkey_source_mask);
1522 }
1523 }
1524}
1525
1526static void hotkey_poll_setup_safe(int may_warn)
1527{
1528 mutex_lock(&hotkey_mutex);
1529 hotkey_poll_setup(may_warn);
1530 mutex_unlock(&hotkey_mutex);
1531}
1532
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001533#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1534
1535static void hotkey_poll_setup_safe(int __unused)
1536{
1537}
1538
1539#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1540
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001541static int hotkey_inputdev_open(struct input_dev *dev)
1542{
1543 switch (tpacpi_lifecycle) {
1544 case TPACPI_LIFE_INIT:
1545 /*
1546 * hotkey_init will call hotkey_poll_setup_safe
1547 * at the appropriate moment
1548 */
1549 return 0;
1550 case TPACPI_LIFE_EXITING:
1551 return -EBUSY;
1552 case TPACPI_LIFE_RUNNING:
1553 hotkey_poll_setup_safe(0);
1554 return 0;
1555 }
1556
1557 /* Should only happen if tpacpi_lifecycle is corrupt */
1558 BUG();
1559 return -EBUSY;
1560}
1561
1562static void hotkey_inputdev_close(struct input_dev *dev)
1563{
1564 /* disable hotkey polling when possible */
1565 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1566 hotkey_poll_setup_safe(0);
1567}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001568
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001569/* sysfs hotkey enable ------------------------------------------------- */
1570static ssize_t hotkey_enable_show(struct device *dev,
1571 struct device_attribute *attr,
1572 char *buf)
1573{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001574 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001575
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001576 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001577 if (res)
1578 return res;
1579
1580 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1581}
1582
1583static ssize_t hotkey_enable_store(struct device *dev,
1584 struct device_attribute *attr,
1585 const char *buf, size_t count)
1586{
1587 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001588 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001589
1590 if (parse_strtoul(buf, 1, &t))
1591 return -EINVAL;
1592
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001593 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001594
1595 return (res) ? res : count;
1596}
1597
1598static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001599 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001600 hotkey_enable_show, hotkey_enable_store);
1601
1602/* sysfs hotkey mask --------------------------------------------------- */
1603static ssize_t hotkey_mask_show(struct device *dev,
1604 struct device_attribute *attr,
1605 char *buf)
1606{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001607 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001608
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001609 if (mutex_lock_interruptible(&hotkey_mutex))
1610 return -ERESTARTSYS;
1611 res = hotkey_mask_get();
1612 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001613
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001614 return (res)?
1615 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001616}
1617
1618static ssize_t hotkey_mask_store(struct device *dev,
1619 struct device_attribute *attr,
1620 const char *buf, size_t count)
1621{
1622 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001623 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001624
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001625 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001626 return -EINVAL;
1627
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001628 if (mutex_lock_interruptible(&hotkey_mutex))
1629 return -ERESTARTSYS;
1630
1631 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001632
1633#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1634 hotkey_poll_setup(1);
1635#endif
1636
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001637 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001638
1639 return (res) ? res : count;
1640}
1641
1642static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001643 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001644 hotkey_mask_show, hotkey_mask_store);
1645
1646/* sysfs hotkey bios_enabled ------------------------------------------- */
1647static ssize_t hotkey_bios_enabled_show(struct device *dev,
1648 struct device_attribute *attr,
1649 char *buf)
1650{
1651 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1652}
1653
1654static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001655 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001656
1657/* sysfs hotkey bios_mask ---------------------------------------------- */
1658static ssize_t hotkey_bios_mask_show(struct device *dev,
1659 struct device_attribute *attr,
1660 char *buf)
1661{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001662 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001663}
1664
1665static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001666 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001667
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001668/* sysfs hotkey all_mask ----------------------------------------------- */
1669static ssize_t hotkey_all_mask_show(struct device *dev,
1670 struct device_attribute *attr,
1671 char *buf)
1672{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001673 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1674 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001675}
1676
1677static struct device_attribute dev_attr_hotkey_all_mask =
1678 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1679
1680/* sysfs hotkey recommended_mask --------------------------------------- */
1681static ssize_t hotkey_recommended_mask_show(struct device *dev,
1682 struct device_attribute *attr,
1683 char *buf)
1684{
1685 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001686 (hotkey_all_mask | hotkey_source_mask)
1687 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001688}
1689
1690static struct device_attribute dev_attr_hotkey_recommended_mask =
1691 __ATTR(hotkey_recommended_mask, S_IRUGO,
1692 hotkey_recommended_mask_show, NULL);
1693
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001694#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1695
1696/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1697static ssize_t hotkey_source_mask_show(struct device *dev,
1698 struct device_attribute *attr,
1699 char *buf)
1700{
1701 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1702}
1703
1704static ssize_t hotkey_source_mask_store(struct device *dev,
1705 struct device_attribute *attr,
1706 const char *buf, size_t count)
1707{
1708 unsigned long t;
1709
1710 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1711 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1712 return -EINVAL;
1713
1714 if (mutex_lock_interruptible(&hotkey_mutex))
1715 return -ERESTARTSYS;
1716
1717 HOTKEY_CONFIG_CRITICAL_START
1718 hotkey_source_mask = t;
1719 HOTKEY_CONFIG_CRITICAL_END
1720
1721 hotkey_poll_setup(1);
1722
1723 mutex_unlock(&hotkey_mutex);
1724
1725 return count;
1726}
1727
1728static struct device_attribute dev_attr_hotkey_source_mask =
1729 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1730 hotkey_source_mask_show, hotkey_source_mask_store);
1731
1732/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1733static ssize_t hotkey_poll_freq_show(struct device *dev,
1734 struct device_attribute *attr,
1735 char *buf)
1736{
1737 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1738}
1739
1740static ssize_t hotkey_poll_freq_store(struct device *dev,
1741 struct device_attribute *attr,
1742 const char *buf, size_t count)
1743{
1744 unsigned long t;
1745
1746 if (parse_strtoul(buf, 25, &t))
1747 return -EINVAL;
1748
1749 if (mutex_lock_interruptible(&hotkey_mutex))
1750 return -ERESTARTSYS;
1751
1752 hotkey_poll_freq = t;
1753
1754 hotkey_poll_setup(1);
1755 mutex_unlock(&hotkey_mutex);
1756
1757 return count;
1758}
1759
1760static struct device_attribute dev_attr_hotkey_poll_freq =
1761 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1762 hotkey_poll_freq_show, hotkey_poll_freq_store);
1763
1764#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1765
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001766/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001767static ssize_t hotkey_radio_sw_show(struct device *dev,
1768 struct device_attribute *attr,
1769 char *buf)
1770{
1771 int res, s;
1772 res = hotkey_get_wlsw(&s);
1773 if (res < 0)
1774 return res;
1775
1776 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1777}
1778
1779static struct device_attribute dev_attr_hotkey_radio_sw =
1780 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1781
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001782static void hotkey_radio_sw_notify_change(void)
1783{
1784 if (tp_features.hotkey_wlsw)
1785 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1786 "hotkey_radio_sw");
1787}
1788
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001789/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1790static ssize_t hotkey_tablet_mode_show(struct device *dev,
1791 struct device_attribute *attr,
1792 char *buf)
1793{
1794 int res, s;
1795 res = hotkey_get_tablet_mode(&s);
1796 if (res < 0)
1797 return res;
1798
1799 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1800}
1801
1802static struct device_attribute dev_attr_hotkey_tablet_mode =
1803 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1804
1805static void hotkey_tablet_mode_notify_change(void)
1806{
1807 if (tp_features.hotkey_tablet)
1808 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1809 "hotkey_tablet_mode");
1810}
1811
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001812/* sysfs hotkey report_mode -------------------------------------------- */
1813static ssize_t hotkey_report_mode_show(struct device *dev,
1814 struct device_attribute *attr,
1815 char *buf)
1816{
1817 return snprintf(buf, PAGE_SIZE, "%d\n",
1818 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1819}
1820
1821static struct device_attribute dev_attr_hotkey_report_mode =
1822 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1823
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001824/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001825static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1826 struct device_attribute *attr,
1827 char *buf)
1828{
1829 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1830}
1831
1832static struct device_attribute dev_attr_hotkey_wakeup_reason =
1833 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1834
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001835static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001836{
1837 if (tp_features.hotkey_mask)
1838 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1839 "wakeup_reason");
1840}
1841
1842/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001843static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1844 struct device_attribute *attr,
1845 char *buf)
1846{
1847 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1848}
1849
1850static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1851 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1852 hotkey_wakeup_hotunplug_complete_show, NULL);
1853
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001854static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001855{
1856 if (tp_features.hotkey_mask)
1857 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1858 "wakeup_hotunplug_complete");
1859}
1860
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001861/* --------------------------------------------------------------------- */
1862
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001863static struct attribute *hotkey_attributes[] __initdata = {
1864 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001865 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001866 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001867#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1868 &dev_attr_hotkey_mask.attr,
1869 &dev_attr_hotkey_all_mask.attr,
1870 &dev_attr_hotkey_recommended_mask.attr,
1871 &dev_attr_hotkey_source_mask.attr,
1872 &dev_attr_hotkey_poll_freq.attr,
1873#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001874};
1875
1876static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001877 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001878#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1879 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001880 &dev_attr_hotkey_all_mask.attr,
1881 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001882#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001883 &dev_attr_hotkey_wakeup_reason.attr,
1884 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001885};
1886
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001887static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001888{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001889 /* Requirements for changing the default keymaps:
1890 *
1891 * 1. Many of the keys are mapped to KEY_RESERVED for very
1892 * good reasons. Do not change them unless you have deep
1893 * knowledge on the IBM and Lenovo ThinkPad firmware for
1894 * the various ThinkPad models. The driver behaves
1895 * differently for KEY_RESERVED: such keys have their
1896 * hot key mask *unset* in mask_recommended, and also
1897 * in the initial hot key mask programmed into the
1898 * firmware at driver load time, which means the firm-
1899 * ware may react very differently if you change them to
1900 * something else;
1901 *
1902 * 2. You must be subscribed to the linux-thinkpad and
1903 * ibm-acpi-devel mailing lists, and you should read the
1904 * list archives since 2007 if you want to change the
1905 * keymaps. This requirement exists so that you will
1906 * know the past history of problems with the thinkpad-
1907 * acpi driver keymaps, and also that you will be
1908 * listening to any bug reports;
1909 *
1910 * 3. Do not send thinkpad-acpi specific patches directly to
1911 * for merging, *ever*. Send them to the linux-acpi
1912 * mailinglist for comments. Merging is to be done only
1913 * through acpi-test and the ACPI maintainer.
1914 *
1915 * If the above is too much to ask, don't change the keymap.
1916 * Ask the thinkpad-acpi maintainer to do it, instead.
1917 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001918 static u16 ibm_keycode_map[] __initdata = {
1919 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1920 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1921 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1922 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001923
1924 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001925 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1926 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1927 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001928
1929 /* brightness: firmware always reacts to them, unless
1930 * X.org did some tricks in the radeon BIOS scratch
1931 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001932 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001933 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001934
1935 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001936 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001937
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001938 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1939 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001940
1941 /* Volume: firmware always react to it and reprograms
1942 * the built-in *extra* mixer. Never map it to control
1943 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001944 KEY_RESERVED, /* 0x14: VOLUME UP */
1945 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1946 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001947
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001948 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001949
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001950 /* (assignments unknown, please report if found) */
1951 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1952 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1953 };
1954 static u16 lenovo_keycode_map[] __initdata = {
1955 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1956 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1957 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1958 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001959
1960 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001961 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1962 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1963 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001964
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001965 /* These either have to go through ACPI video, or
1966 * act like in the IBM ThinkPads, so don't ever
1967 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001968 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001969 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001970
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001971 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001972
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001973 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1974 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001975
1976 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1977 * react to it and reprograms the built-in *extra* mixer.
1978 * Never map it to control another mixer by default.
1979 *
1980 * T60?, T61, R60?, R61: firmware and EC tries to send
1981 * these over the regular keyboard, so these are no-ops,
1982 * but there are still weird bugs re. MUTE, so do not
1983 * change unless you get test reports from all Lenovo
1984 * models. May cause the BIOS to interfere with the
1985 * HDA mixer.
1986 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001987 KEY_RESERVED, /* 0x14: VOLUME UP */
1988 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1989 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001990
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001991 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001992
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001993 /* (assignments unknown, please report if found) */
1994 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1995 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1996 };
1997
1998#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1999#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2000#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2001
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002002 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002003 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002004 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002005
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002006 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2007
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002008 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002009 BUG_ON(tpacpi_inputdev->open != NULL ||
2010 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002011
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002012 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002013 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002014
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002015#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2016 mutex_init(&hotkey_thread_mutex);
2017 mutex_init(&hotkey_thread_data_mutex);
2018#endif
2019
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002020 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002021 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002022
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002023 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002024 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002025
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002026 if (tp_features.hotkey) {
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002027 hotkey_dev_attributes = create_attr_set(13, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002028 if (!hotkey_dev_attributes)
2029 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002030 res = add_many_to_attr_set(hotkey_dev_attributes,
2031 hotkey_attributes,
2032 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002033 if (res)
2034 return res;
2035
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002036 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002037 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2038 for HKEY interface version 0x100 */
2039 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2040 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002041 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002042 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002043 printk(TPACPI_ERR "please report this to %s\n",
2044 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002045 } else {
2046 /*
2047 * MHKV 0x100 in A31, R40, R40e,
2048 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002049 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002050 tp_features.hotkey_mask = 1;
2051 }
2052 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002053
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002054 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002055 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002056
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002057 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002058 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002059 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002060 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002061 "missing MHKA handler, "
2062 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002063 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002064 /* FN+F12, FN+F4, FN+F3 */
2065 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002066 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002067 }
2068
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002069 /* hotkey_source_mask *must* be zero for
2070 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002071 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002072 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002073 res = hotkey_mask_get();
2074 hotkey_orig_mask = hotkey_mask;
2075 if (!res) {
2076 res = add_many_to_attr_set(
2077 hotkey_dev_attributes,
2078 hotkey_mask_attributes,
2079 ARRAY_SIZE(hotkey_mask_attributes));
2080 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002081 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002082
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002083#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2084 if (tp_features.hotkey_mask) {
2085 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2086 & ~hotkey_all_mask;
2087 } else {
2088 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2089 }
2090
2091 vdbg_printk(TPACPI_DBG_INIT,
2092 "hotkey source mask 0x%08x, polling freq %d\n",
2093 hotkey_source_mask, hotkey_poll_freq);
2094#endif
2095
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002096 /* Not all thinkpads have a hardware radio switch */
2097 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2098 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002099 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002100 "radio switch found; radios are %s\n",
2101 enabled(status, 0));
2102 res = add_to_attr_set(hotkey_dev_attributes,
2103 &dev_attr_hotkey_radio_sw.attr);
2104 }
2105
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002106 /* For X41t, X60t, X61t Tablets... */
2107 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2108 tp_features.hotkey_tablet = 1;
2109 printk(TPACPI_INFO
2110 "possible tablet mode switch found; "
2111 "ThinkPad in %s mode\n",
2112 (status & TP_HOTKEY_TABLET_MASK)?
2113 "tablet" : "laptop");
2114 res = add_to_attr_set(hotkey_dev_attributes,
2115 &dev_attr_hotkey_tablet_mode.attr);
2116 }
2117
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002118 if (!res)
2119 res = register_attr_set_with_sysfs(
2120 hotkey_dev_attributes,
2121 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002122 if (res)
2123 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002124
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002125 /* Set up key map */
2126
2127 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2128 GFP_KERNEL);
2129 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002130 printk(TPACPI_ERR
2131 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002132 return -ENOMEM;
2133 }
2134
2135 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2136 dbg_printk(TPACPI_DBG_INIT,
2137 "using Lenovo default hot key map\n");
2138 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2139 TPACPI_HOTKEY_MAP_SIZE);
2140 } else {
2141 dbg_printk(TPACPI_DBG_INIT,
2142 "using IBM default hot key map\n");
2143 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2144 TPACPI_HOTKEY_MAP_SIZE);
2145 }
2146
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002147 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2148 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2149 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002150 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2151 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2152 tpacpi_inputdev->keycode = hotkey_keycode_map;
2153 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002154 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2155 set_bit(hotkey_keycode_map[i],
2156 tpacpi_inputdev->keybit);
2157 } else {
2158 if (i < sizeof(hotkey_reserved_mask)*8)
2159 hotkey_reserved_mask |= 1 << i;
2160 }
2161 }
2162
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002163 if (tp_features.hotkey_wlsw) {
2164 set_bit(EV_SW, tpacpi_inputdev->evbit);
2165 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2166 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002167 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002168 set_bit(EV_SW, tpacpi_inputdev->evbit);
2169 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2170 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002171
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002172 /* Do not issue duplicate brightness change events to
2173 * userspace */
2174 if (!tp_features.bright_acpimode)
2175 /* update bright_acpimode... */
2176 tpacpi_check_std_acpi_brightness_support();
2177
2178 if (tp_features.bright_acpimode) {
2179 printk(TPACPI_INFO
2180 "This ThinkPad has standard ACPI backlight "
2181 "brightness control, supported by the ACPI "
2182 "video driver\n");
2183 printk(TPACPI_NOTICE
2184 "Disabling thinkpad-acpi brightness events "
2185 "by default...\n");
2186
2187 /* The hotkey_reserved_mask change below is not
2188 * necessary while the keys are at KEY_RESERVED in the
2189 * default map, but better safe than sorry, leave it
2190 * here as a marker of what we have to do, especially
2191 * when we finally become able to set this at runtime
2192 * on response to X.org requests */
2193 hotkey_reserved_mask |=
2194 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2195 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2196 }
2197
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002198 dbg_printk(TPACPI_DBG_INIT,
2199 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002200 res = hotkey_status_set(1);
2201 if (res)
2202 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002203 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2204 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002205 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002206 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002207 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002208
2209 dbg_printk(TPACPI_DBG_INIT,
2210 "legacy hot key reporting over procfs %s\n",
2211 (hotkey_report_mode < 2) ?
2212 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002213
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002214 tpacpi_inputdev->open = &hotkey_inputdev_open;
2215 tpacpi_inputdev->close = &hotkey_inputdev_close;
2216
2217 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002218 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002219 tpacpi_input_send_tabletsw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002220 }
2221
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002222 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002223}
2224
2225static void hotkey_exit(void)
2226{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002227#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2228 hotkey_poll_stop_sync();
2229#endif
2230
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002231 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002232 dbg_printk(TPACPI_DBG_EXIT,
2233 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002234 /* no short-circuit boolean operator below! */
2235 if ((hotkey_mask_set(hotkey_orig_mask) |
2236 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002237 printk(TPACPI_ERR
2238 "failed to restore hot key mask "
2239 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002240 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002241
2242 if (hotkey_dev_attributes) {
2243 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2244 hotkey_dev_attributes = NULL;
2245 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002246}
2247
2248static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2249{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002250 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002251 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002252 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002253 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002254 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002255
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002256 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002257 printk(TPACPI_ERR
2258 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002259 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002260 acpi_bus_generate_netlink_event(
2261 ibm->acpi->device->pnp.device_class,
2262 ibm->acpi->device->dev.bus_id,
2263 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002264 return;
2265 }
2266
2267 while (1) {
2268 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002269 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002270 return;
2271 }
2272
2273 if (hkey == 0) {
2274 /* queue empty */
2275 return;
2276 }
2277
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002278 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002279 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002280 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002281
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002282 switch (hkey >> 12) {
2283 case 1:
2284 /* 0x1000-0x1FFF: key presses */
2285 scancode = hkey & 0xfff;
2286 if (scancode > 0 && scancode < 0x21) {
2287 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002288 if (!(hotkey_source_mask & (1 << scancode))) {
2289 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002290 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002291 } else {
2292 ignore_acpi_ev = 1;
2293 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002294 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002295 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002296 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002297 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002298 case 2:
2299 /* Wakeup reason */
2300 switch (hkey) {
2301 case 0x2304: /* suspend, undock */
2302 case 0x2404: /* hibernation, undock */
2303 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2304 ignore_acpi_ev = 1;
2305 break;
2306 case 0x2305: /* suspend, bay eject */
2307 case 0x2405: /* hibernation, bay eject */
2308 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2309 ignore_acpi_ev = 1;
2310 break;
2311 default:
2312 unk_ev = 1;
2313 }
2314 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2315 printk(TPACPI_INFO
2316 "woke up due to a hot-unplug "
2317 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002318 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002319 }
2320 break;
2321 case 3:
2322 /* bay-related wakeups */
2323 if (hkey == 0x3003) {
2324 hotkey_autosleep_ack = 1;
2325 printk(TPACPI_INFO
2326 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002327 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002328 } else {
2329 unk_ev = 1;
2330 }
2331 break;
2332 case 4:
2333 /* dock-related wakeups */
2334 if (hkey == 0x4003) {
2335 hotkey_autosleep_ack = 1;
2336 printk(TPACPI_INFO
2337 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002338 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002339 } else {
2340 unk_ev = 1;
2341 }
2342 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002343 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002344 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002345 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002346 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002347 case 0x500b: /* X61t: tablet pen inserted into bay */
2348 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002349 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002350 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2351 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2352 tpacpi_input_send_tabletsw();
2353 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002354 send_acpi_ev = 0;
2355 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002356 case 0x5001:
2357 case 0x5002:
2358 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002359 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002360 break;
2361 default:
2362 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002363 }
2364 break;
2365 case 7:
2366 /* 0x7000-0x7FFF: misc */
2367 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2368 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002369 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002370 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002371 break;
2372 }
2373 /* fallthrough to default */
2374 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002375 unk_ev = 1;
2376 }
2377 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002378 printk(TPACPI_NOTICE
2379 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002380 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002381
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002382 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002383 if (!ignore_acpi_ev &&
2384 (send_acpi_ev || hotkey_report_mode < 2)) {
2385 acpi_bus_generate_proc_event(ibm->acpi->device,
2386 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002387 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002388
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002389 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002390 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002391 acpi_bus_generate_netlink_event(
2392 ibm->acpi->device->pnp.device_class,
2393 ibm->acpi->device->dev.bus_id,
2394 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002395 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002396 }
2397}
2398
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002399static void hotkey_suspend(pm_message_t state)
2400{
2401 /* Do these on suspend, we get the events on early resume! */
2402 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2403 hotkey_autosleep_ack = 0;
2404}
2405
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002406static void hotkey_resume(void)
2407{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002408 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002409 printk(TPACPI_ERR
2410 "error while trying to read hot key mask "
2411 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002412 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002413 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002414 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002415 hotkey_wakeup_reason_notify_change();
2416 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002417 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002418}
2419
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002420/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002421static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002423 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int len = 0;
2425
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002426 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002427 len += sprintf(p + len, "status:\t\tnot supported\n");
2428 return len;
2429 }
2430
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002431 if (mutex_lock_interruptible(&hotkey_mutex))
2432 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002433 res = hotkey_status_get(&status);
2434 if (!res)
2435 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002436 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002437 if (res)
2438 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002441 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002442 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 len += sprintf(p + len,
2444 "commands:\tenable, disable, reset, <mask>\n");
2445 } else {
2446 len += sprintf(p + len, "mask:\t\tnot supported\n");
2447 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2448 }
2449
2450 return len;
2451}
2452
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002453static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002455 int res, status;
2456 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002459 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return -ENODEV;
2461
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002462 if (mutex_lock_interruptible(&hotkey_mutex))
2463 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002464
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002465 status = -1;
2466 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002467
2468 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 while ((cmd = next_cmd(&buf))) {
2470 if (strlencmp(cmd, "enable") == 0) {
2471 status = 1;
2472 } else if (strlencmp(cmd, "disable") == 0) {
2473 status = 0;
2474 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002475 status = hotkey_orig_status;
2476 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2478 /* mask set */
2479 } else if (sscanf(cmd, "%x", &mask) == 1) {
2480 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002481 } else {
2482 res = -EINVAL;
2483 goto errexit;
2484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002486 if (status != -1)
2487 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002489 if (!res && mask != hotkey_mask)
2490 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002492errexit:
2493 mutex_unlock(&hotkey_mutex);
2494 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002497static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002498 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002499 {"", 0},
2500};
2501
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002502static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002503 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002504 .notify = hotkey_notify,
2505 .handle = &hkey_handle,
2506 .type = ACPI_DEVICE_NOTIFY,
2507};
2508
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002509static struct ibm_struct hotkey_driver_data = {
2510 .name = "hotkey",
2511 .read = hotkey_read,
2512 .write = hotkey_write,
2513 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002514 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002515 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002516 .acpi = &ibm_hotkey_acpidriver,
2517};
2518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002519/*************************************************************************
2520 * Bluetooth subdriver
2521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002523enum {
2524 /* ACPI GBDC/SBDC bits */
2525 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2526 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2527 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2528};
2529
2530static int bluetooth_get_radiosw(void);
2531static int bluetooth_set_radiosw(int radio_on);
2532
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002533/* sysfs bluetooth enable ---------------------------------------------- */
2534static ssize_t bluetooth_enable_show(struct device *dev,
2535 struct device_attribute *attr,
2536 char *buf)
2537{
2538 int status;
2539
2540 status = bluetooth_get_radiosw();
2541 if (status < 0)
2542 return status;
2543
2544 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2545}
2546
2547static ssize_t bluetooth_enable_store(struct device *dev,
2548 struct device_attribute *attr,
2549 const char *buf, size_t count)
2550{
2551 unsigned long t;
2552 int res;
2553
2554 if (parse_strtoul(buf, 1, &t))
2555 return -EINVAL;
2556
2557 res = bluetooth_set_radiosw(t);
2558
2559 return (res) ? res : count;
2560}
2561
2562static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002563 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002564 bluetooth_enable_show, bluetooth_enable_store);
2565
2566/* --------------------------------------------------------------------- */
2567
2568static struct attribute *bluetooth_attributes[] = {
2569 &dev_attr_bluetooth_enable.attr,
2570 NULL
2571};
2572
2573static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002574 .attrs = bluetooth_attributes,
2575};
2576
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002577static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002579 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002580 int status = 0;
2581
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002582 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2583
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002584 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002585
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002586 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2587 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002588 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002589 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002591 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2592 str_supported(tp_features.bluetooth),
2593 status);
2594
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002595 if (tp_features.bluetooth) {
2596 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2597 /* no bluetooth hardware present in system */
2598 tp_features.bluetooth = 0;
2599 dbg_printk(TPACPI_DBG_INIT,
2600 "bluetooth hardware not installed\n");
2601 } else {
2602 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2603 &bluetooth_attr_group);
2604 if (res)
2605 return res;
2606 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002607 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002608
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002609 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002612static void bluetooth_exit(void)
2613{
2614 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2615 &bluetooth_attr_group);
2616}
2617
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002618static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
2620 int status;
2621
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002622 if (!tp_features.bluetooth)
2623 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002625 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2626 return -EIO;
2627
2628 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2629}
2630
2631static int bluetooth_set_radiosw(int radio_on)
2632{
2633 int status;
2634
2635 if (!tp_features.bluetooth)
2636 return -ENODEV;
2637
2638 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2639 return -EIO;
2640 if (radio_on)
2641 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2642 else
2643 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2644 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2645 return -EIO;
2646
2647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002650/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002651static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
2653 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002654 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002656 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002659 len += sprintf(p + len, "status:\t\t%s\n",
2660 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 len += sprintf(p + len, "commands:\tenable, disable\n");
2662 }
2663
2664 return len;
2665}
2666
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002667static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002671 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002672 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 while ((cmd = next_cmd(&buf))) {
2675 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002676 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002678 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 } else
2680 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return 0;
2684}
2685
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002686static struct ibm_struct bluetooth_driver_data = {
2687 .name = "bluetooth",
2688 .read = bluetooth_read,
2689 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002690 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002691};
2692
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002693/*************************************************************************
2694 * Wan subdriver
2695 */
2696
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002697enum {
2698 /* ACPI GWAN/SWAN bits */
2699 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2700 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2701 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2702};
2703
2704static int wan_get_radiosw(void);
2705static int wan_set_radiosw(int radio_on);
2706
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002707/* sysfs wan enable ---------------------------------------------------- */
2708static ssize_t wan_enable_show(struct device *dev,
2709 struct device_attribute *attr,
2710 char *buf)
2711{
2712 int status;
2713
2714 status = wan_get_radiosw();
2715 if (status < 0)
2716 return status;
2717
2718 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2719}
2720
2721static ssize_t wan_enable_store(struct device *dev,
2722 struct device_attribute *attr,
2723 const char *buf, size_t count)
2724{
2725 unsigned long t;
2726 int res;
2727
2728 if (parse_strtoul(buf, 1, &t))
2729 return -EINVAL;
2730
2731 res = wan_set_radiosw(t);
2732
2733 return (res) ? res : count;
2734}
2735
2736static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002737 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002738 wan_enable_show, wan_enable_store);
2739
2740/* --------------------------------------------------------------------- */
2741
2742static struct attribute *wan_attributes[] = {
2743 &dev_attr_wan_enable.attr,
2744 NULL
2745};
2746
2747static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002748 .attrs = wan_attributes,
2749};
2750
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002751static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002752{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002753 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002754 int status = 0;
2755
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002756 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2757
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002758 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002759
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002760 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002761 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002762
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002763 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2764 str_supported(tp_features.wan),
2765 status);
2766
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002767 if (tp_features.wan) {
2768 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2769 /* no wan hardware present in system */
2770 tp_features.wan = 0;
2771 dbg_printk(TPACPI_DBG_INIT,
2772 "wan hardware not installed\n");
2773 } else {
2774 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2775 &wan_attr_group);
2776 if (res)
2777 return res;
2778 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002779 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002780
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002781 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002782}
2783
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002784static void wan_exit(void)
2785{
2786 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2787 &wan_attr_group);
2788}
2789
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002790static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002791{
2792 int status;
2793
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002794 if (!tp_features.wan)
2795 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002796
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002797 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2798 return -EIO;
2799
2800 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2801}
2802
2803static int wan_set_radiosw(int radio_on)
2804{
2805 int status;
2806
2807 if (!tp_features.wan)
2808 return -ENODEV;
2809
2810 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2811 return -EIO;
2812 if (radio_on)
2813 status |= TP_ACPI_WANCARD_RADIOSSW;
2814 else
2815 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2816 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2817 return -EIO;
2818
2819 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002820}
2821
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002822/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002823static int wan_read(char *p)
2824{
2825 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002826 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002827
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002828 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002829 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002830 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002831 len += sprintf(p + len, "status:\t\t%s\n",
2832 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002833 len += sprintf(p + len, "commands:\tenable, disable\n");
2834 }
2835
2836 return len;
2837}
2838
2839static int wan_write(char *buf)
2840{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002841 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002842
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002843 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002844 return -ENODEV;
2845
2846 while ((cmd = next_cmd(&buf))) {
2847 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002848 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002849 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002850 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002851 } else
2852 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002853 }
2854
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002855 return 0;
2856}
2857
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002858static struct ibm_struct wan_driver_data = {
2859 .name = "wan",
2860 .read = wan_read,
2861 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002862 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002863 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002864};
2865
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002866/*************************************************************************
2867 * Video subdriver
2868 */
2869
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002870#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2871
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002872enum video_access_mode {
2873 TPACPI_VIDEO_NONE = 0,
2874 TPACPI_VIDEO_570, /* 570 */
2875 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2876 TPACPI_VIDEO_NEW, /* all others */
2877};
2878
2879enum { /* video status flags, based on VIDEO_570 */
2880 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2881 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2882 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2883};
2884
2885enum { /* TPACPI_VIDEO_570 constants */
2886 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2887 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2888 * video_status_flags */
2889 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2890 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2891};
2892
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002893static enum video_access_mode video_supported;
2894static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002895
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002896static int video_autosw_get(void);
2897static int video_autosw_set(int enable);
2898
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002899TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002900
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002901static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002903 int ivga;
2904
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002905 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2906
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002907 TPACPI_ACPIHANDLE_INIT(vid);
2908 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002909
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002910 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2911 /* G41, assume IVGA doesn't change */
2912 vid_handle = vid2_handle;
2913
2914 if (!vid_handle)
2915 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002916 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002917 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2918 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002919 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002920 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2921 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002922 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002923 else
2924 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002925 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002927 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2928 str_supported(video_supported != TPACPI_VIDEO_NONE),
2929 video_supported);
2930
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002931 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002934static void video_exit(void)
2935{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002936 dbg_printk(TPACPI_DBG_EXIT,
2937 "restoring original video autoswitch mode\n");
2938 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002939 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002940 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002941}
2942
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002943static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
2945 int status = 0;
2946 int i;
2947
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002948 switch (video_supported) {
2949 case TPACPI_VIDEO_570:
2950 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2951 TP_ACPI_VIDEO_570_PHSCMD))
2952 return -EIO;
2953 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2954 break;
2955 case TPACPI_VIDEO_770:
2956 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2957 return -EIO;
2958 if (i)
2959 status |= TP_ACPI_VIDEO_S_LCD;
2960 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2961 return -EIO;
2962 if (i)
2963 status |= TP_ACPI_VIDEO_S_CRT;
2964 break;
2965 case TPACPI_VIDEO_NEW:
2966 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2967 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2968 return -EIO;
2969 if (i)
2970 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002972 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2973 !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, "\\VCDD", "d"))
2978 return -EIO;
2979 if (i)
2980 status |= TP_ACPI_VIDEO_S_DVI;
2981 break;
2982 default:
2983 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 return status;
2987}
2988
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002989static int video_outputsw_set(int status)
2990{
2991 int autosw;
2992 int res = 0;
2993
2994 switch (video_supported) {
2995 case TPACPI_VIDEO_570:
2996 res = acpi_evalf(NULL, NULL,
2997 "\\_SB.PHS2", "vdd",
2998 TP_ACPI_VIDEO_570_PHS2CMD,
2999 status | TP_ACPI_VIDEO_570_PHS2SET);
3000 break;
3001 case TPACPI_VIDEO_770:
3002 autosw = video_autosw_get();
3003 if (autosw < 0)
3004 return autosw;
3005
3006 res = video_autosw_set(1);
3007 if (res)
3008 return res;
3009 res = acpi_evalf(vid_handle, NULL,
3010 "ASWT", "vdd", status * 0x100, 0);
3011 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003012 printk(TPACPI_ERR
3013 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003014 return -EIO;
3015 }
3016 break;
3017 case TPACPI_VIDEO_NEW:
3018 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003019 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003020 break;
3021 default:
3022 return -ENOSYS;
3023 }
3024
3025 return (res)? 0 : -EIO;
3026}
3027
3028static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003030 int autosw = 0;
3031
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003032 switch (video_supported) {
3033 case TPACPI_VIDEO_570:
3034 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3035 return -EIO;
3036 break;
3037 case TPACPI_VIDEO_770:
3038 case TPACPI_VIDEO_NEW:
3039 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3040 return -EIO;
3041 break;
3042 default:
3043 return -ENOSYS;
3044 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003045
3046 return autosw & 1;
3047}
3048
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003049static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003050{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003051 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003052 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003053 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003054}
3055
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003056static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003057{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003058 int autosw = video_autosw_get();
3059 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003060
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003061 if (autosw < 0)
3062 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003063
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003064 switch (video_supported) {
3065 case TPACPI_VIDEO_570:
3066 res = video_autosw_set(1);
3067 if (res)
3068 return res;
3069 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3070 break;
3071 case TPACPI_VIDEO_770:
3072 case TPACPI_VIDEO_NEW:
3073 res = video_autosw_set(1);
3074 if (res)
3075 return res;
3076 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3077 break;
3078 default:
3079 return -ENOSYS;
3080 }
3081 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003082 printk(TPACPI_ERR
3083 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003084 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003085 }
3086
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003087 return (res)? 0 : -EIO;
3088}
3089
3090static int video_expand_toggle(void)
3091{
3092 switch (video_supported) {
3093 case TPACPI_VIDEO_570:
3094 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3095 0 : -EIO;
3096 case TPACPI_VIDEO_770:
3097 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3098 0 : -EIO;
3099 case TPACPI_VIDEO_NEW:
3100 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3101 0 : -EIO;
3102 default:
3103 return -ENOSYS;
3104 }
3105 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003106}
3107
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003108static int video_read(char *p)
3109{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003110 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003111 int len = 0;
3112
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003113 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003114 len += sprintf(p + len, "status:\t\tnot supported\n");
3115 return len;
3116 }
3117
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003118 status = video_outputsw_get();
3119 if (status < 0)
3120 return status;
3121
3122 autosw = video_autosw_get();
3123 if (autosw < 0)
3124 return autosw;
3125
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003126 len += sprintf(p + len, "status:\t\tsupported\n");
3127 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3128 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003129 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003130 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3131 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3132 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3133 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003134 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003135 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3136 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3137 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3138
3139 return len;
3140}
3141
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003142static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 char *cmd;
3145 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003146 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003148 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003149 return -ENODEV;
3150
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003151 enable = 0;
3152 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 while ((cmd = next_cmd(&buf))) {
3155 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003156 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003158 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003160 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003162 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003163 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003164 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003165 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003166 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003167 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003168 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003170 res = video_autosw_set(1);
3171 if (res)
3172 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003174 res = video_autosw_set(0);
3175 if (res)
3176 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003178 res = video_outputsw_cycle();
3179 if (res)
3180 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003182 res = video_expand_toggle();
3183 if (res)
3184 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 } else
3186 return -EINVAL;
3187 }
3188
3189 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003190 status = video_outputsw_get();
3191 if (status < 0)
3192 return status;
3193 res = video_outputsw_set((status & ~disable) | enable);
3194 if (res)
3195 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
3197
3198 return 0;
3199}
3200
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003201static struct ibm_struct video_driver_data = {
3202 .name = "video",
3203 .read = video_read,
3204 .write = video_write,
3205 .exit = video_exit,
3206};
3207
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003208#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3209
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003210/*************************************************************************
3211 * Light (thinklight) subdriver
3212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003214TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3215TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003216
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003217static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003219 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3220
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003221 TPACPI_ACPIHANDLE_INIT(ledb);
3222 TPACPI_ACPIHANDLE_INIT(lght);
3223 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003224
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003225 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003226 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003227
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003228 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003229 /* light status not supported on
3230 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003231 tp_features.light_status =
3232 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003234 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003235 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003236
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003237 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003240static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
3242 int len = 0;
3243 int status = 0;
3244
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003245 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003246 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003247 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003248 len += sprintf(p + len, "status:\t\tunknown\n");
3249 len += sprintf(p + len, "commands:\ton, off\n");
3250 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3252 return -EIO;
3253 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003254 len += sprintf(p + len, "commands:\ton, off\n");
3255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257 return len;
3258}
3259
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003260static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
3262 int cmos_cmd, lght_cmd;
3263 char *cmd;
3264 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003265
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003266 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003267 return -ENODEV;
3268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 while ((cmd = next_cmd(&buf))) {
3270 if (strlencmp(cmd, "on") == 0) {
3271 cmos_cmd = 0x0c;
3272 lght_cmd = 1;
3273 } else if (strlencmp(cmd, "off") == 0) {
3274 cmos_cmd = 0x0d;
3275 lght_cmd = 0;
3276 } else
3277 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003280 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3281 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if (!success)
3283 return -EIO;
3284 }
3285
3286 return 0;
3287}
3288
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003289static struct ibm_struct light_driver_data = {
3290 .name = "light",
3291 .read = light_read,
3292 .write = light_write,
3293};
3294
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003295/*************************************************************************
3296 * Dock subdriver
3297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003299#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003300
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003301static void dock_notify(struct ibm_struct *ibm, u32 event);
3302static int dock_read(char *p);
3303static int dock_write(char *buf);
3304
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003305TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003306 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3307 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3308 "\\_SB.PCI.ISA.SLCE", /* 570 */
3309 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3310
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003311/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003312TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003313
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003314static const struct acpi_device_id ibm_pci_device_ids[] = {
3315 {PCI_ROOT_HID_STRING, 0},
3316 {"", 0},
3317};
3318
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003319static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3320 {
3321 .notify = dock_notify,
3322 .handle = &dock_handle,
3323 .type = ACPI_SYSTEM_NOTIFY,
3324 },
3325 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003326 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3327 * We just use it to get notifications of dock hotplug
3328 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003329 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003330 .notify = dock_notify,
3331 .handle = &pci_handle,
3332 .type = ACPI_SYSTEM_NOTIFY,
3333 },
3334};
3335
3336static struct ibm_struct dock_driver_data[2] = {
3337 {
3338 .name = "dock",
3339 .read = dock_read,
3340 .write = dock_write,
3341 .acpi = &ibm_dock_acpidriver[0],
3342 },
3343 {
3344 .name = "dock",
3345 .acpi = &ibm_dock_acpidriver[1],
3346 },
3347};
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349#define dock_docked() (_sta(dock_handle) & 1)
3350
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003351static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003352{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003353 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3354
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003355 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003356
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003357 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3358 str_supported(dock_handle != NULL));
3359
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003360 return (dock_handle)? 0 : 1;
3361}
3362
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003363static int __init dock_init2(struct ibm_init_struct *iibm)
3364{
3365 int dock2_needed;
3366
3367 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3368
3369 if (dock_driver_data[0].flags.acpi_driver_registered &&
3370 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003371 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003372 dock2_needed = (pci_handle != NULL);
3373 vdbg_printk(TPACPI_DBG_INIT,
3374 "dock PCI handler for the TP 570 is %s\n",
3375 str_supported(dock2_needed));
3376 } else {
3377 vdbg_printk(TPACPI_DBG_INIT,
3378 "dock subdriver part 2 not required\n");
3379 dock2_needed = 0;
3380 }
3381
3382 return (dock2_needed)? 0 : 1;
3383}
3384
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003385static void dock_notify(struct ibm_struct *ibm, u32 event)
3386{
3387 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003388 int pci = ibm->acpi->hid && ibm->acpi->device &&
3389 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003390 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003391
3392 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003393 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003394 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003395 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003396 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003397 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003398 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003399 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003400 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003401 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003402 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003403 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003404 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003405 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003406 }
Len Brown14e04fb32007-08-23 15:20:26 -04003407 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003408 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3409 ibm->acpi->device->dev.bus_id,
3410 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003411}
3412
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003413static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
3415 int len = 0;
3416 int docked = dock_docked();
3417
3418 if (!dock_handle)
3419 len += sprintf(p + len, "status:\t\tnot supported\n");
3420 else if (!docked)
3421 len += sprintf(p + len, "status:\t\tundocked\n");
3422 else {
3423 len += sprintf(p + len, "status:\t\tdocked\n");
3424 len += sprintf(p + len, "commands:\tdock, undock\n");
3425 }
3426
3427 return len;
3428}
3429
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003430static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
3432 char *cmd;
3433
3434 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003435 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 while ((cmd = next_cmd(&buf))) {
3438 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003439 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3440 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return -EIO;
3442 } else if (strlencmp(cmd, "dock") == 0) {
3443 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3444 return -EIO;
3445 } else
3446 return -EINVAL;
3447 }
3448
3449 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003452#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003454/*************************************************************************
3455 * Bay subdriver
3456 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003458#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003459
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003460TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003461 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3462 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3463 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3464 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003465TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003466 "_EJ0", /* all others */
3467 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003468TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003469 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3470 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003471TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003472 "_EJ0", /* 770x */
3473 ); /* all others */
3474
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003475static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003477 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3478
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003479 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003480 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003481 TPACPI_ACPIHANDLE_INIT(bay_ej);
3482 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003483 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003484 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003485
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003486 tp_features.bay_status = bay_handle &&
3487 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3488 tp_features.bay_status2 = bay2_handle &&
3489 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003490
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003491 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3492 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3493 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3494 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003496 vdbg_printk(TPACPI_DBG_INIT,
3497 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003498 str_supported(tp_features.bay_status),
3499 str_supported(tp_features.bay_eject),
3500 str_supported(tp_features.bay_status2),
3501 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003502
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003503 return (tp_features.bay_status || tp_features.bay_eject ||
3504 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505}
3506
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003507static void bay_notify(struct ibm_struct *ibm, u32 event)
3508{
Len Brown14e04fb32007-08-23 15:20:26 -04003509 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003510 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3511 ibm->acpi->device->dev.bus_id,
3512 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003513}
3514
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003515#define bay_occupied(b) (_sta(b##_handle) & 1)
3516
3517static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
3519 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003520 int occupied = bay_occupied(bay);
3521 int occupied2 = bay_occupied(bay2);
3522 int eject, eject2;
3523
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003524 len += sprintf(p + len, "status:\t\t%s\n",
3525 tp_features.bay_status ?
3526 (occupied ? "occupied" : "unoccupied") :
3527 "not supported");
3528 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003529 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3530 "occupied" : "unoccupied");
3531
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003532 eject = tp_features.bay_eject && occupied;
3533 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003534
3535 if (eject && eject2)
3536 len += sprintf(p + len, "commands:\teject, eject2\n");
3537 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003539 else if (eject2)
3540 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 return len;
3543}
3544
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003545static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 char *cmd;
3548
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003549 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003550 return -ENODEV;
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003553 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003554 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3555 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003556 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003557 strlencmp(cmd, "eject2") == 0) {
3558 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return -EIO;
3560 } else
3561 return -EINVAL;
3562 }
3563
3564 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003565}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003566
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003567static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3568 .notify = bay_notify,
3569 .handle = &bay_handle,
3570 .type = ACPI_SYSTEM_NOTIFY,
3571};
3572
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003573static struct ibm_struct bay_driver_data = {
3574 .name = "bay",
3575 .read = bay_read,
3576 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003577 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003578};
3579
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003580#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003582/*************************************************************************
3583 * CMOS subdriver
3584 */
3585
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003586/* sysfs cmos_command -------------------------------------------------- */
3587static ssize_t cmos_command_store(struct device *dev,
3588 struct device_attribute *attr,
3589 const char *buf, size_t count)
3590{
3591 unsigned long cmos_cmd;
3592 int res;
3593
3594 if (parse_strtoul(buf, 21, &cmos_cmd))
3595 return -EINVAL;
3596
3597 res = issue_thinkpad_cmos_command(cmos_cmd);
3598 return (res)? res : count;
3599}
3600
3601static struct device_attribute dev_attr_cmos_command =
3602 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3603
3604/* --------------------------------------------------------------------- */
3605
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003606static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003607{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003608 int res;
3609
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003610 vdbg_printk(TPACPI_DBG_INIT,
3611 "initializing cmos commands subdriver\n");
3612
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003613 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003614
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003615 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3616 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003617
3618 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3619 if (res)
3620 return res;
3621
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003622 return (cmos_handle)? 0 : 1;
3623}
3624
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003625static void cmos_exit(void)
3626{
3627 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3628}
3629
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003630static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
3632 int len = 0;
3633
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003634 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3635 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 if (!cmos_handle)
3637 len += sprintf(p + len, "status:\t\tnot supported\n");
3638 else {
3639 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003640 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 }
3642
3643 return len;
3644}
3645
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003646static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
3648 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003649 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
3651 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003652 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3653 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 /* cmos_cmd set */
3655 } else
3656 return -EINVAL;
3657
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003658 res = issue_thinkpad_cmos_command(cmos_cmd);
3659 if (res)
3660 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
3662
3663 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003664}
3665
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003666static struct ibm_struct cmos_driver_data = {
3667 .name = "cmos",
3668 .read = cmos_read,
3669 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003670 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003671};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003672
3673/*************************************************************************
3674 * LED subdriver
3675 */
3676
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003677enum led_access_mode {
3678 TPACPI_LED_NONE = 0,
3679 TPACPI_LED_570, /* 570 */
3680 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3681 TPACPI_LED_NEW, /* all others */
3682};
3683
3684enum { /* For TPACPI_LED_OLD */
3685 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3686 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3687 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3688};
3689
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003690static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003691
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003692TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003693 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3694 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003695 "LED", /* all others */
3696 ); /* R30, R31 */
3697
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003698static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003699{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003700 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3701
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003702 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003703
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003704 if (!led_handle)
3705 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003706 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003707 else if (strlencmp(led_path, "SLED") == 0)
3708 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003709 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003710 else if (strlencmp(led_path, "SYSL") == 0)
3711 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003712 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003713 else
3714 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003715 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003716
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003717 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3718 str_supported(led_supported), led_supported);
3719
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003720 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003721}
3722
3723#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3724
3725static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
3727 int len = 0;
3728
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003729 if (!led_supported) {
3730 len += sprintf(p + len, "status:\t\tnot supported\n");
3731 return len;
3732 }
3733 len += sprintf(p + len, "status:\t\tsupported\n");
3734
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003735 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003736 /* 570 */
3737 int i, status;
3738 for (i = 0; i < 8; i++) {
3739 if (!acpi_evalf(ec_handle,
3740 &status, "GLED", "dd", 1 << i))
3741 return -EIO;
3742 len += sprintf(p + len, "%d:\t\t%s\n",
3743 i, led_status(status));
3744 }
3745 }
3746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003748 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 return len;
3751}
3752
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003753/* off, on, blink */
3754static const int led_sled_arg1[] = { 0, 1, 3 };
3755static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3756static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3757static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3758
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003759static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
3761 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003762 int led, ind, ret;
3763
3764 if (!led_supported)
3765 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003768 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return -EINVAL;
3770
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003771 if (strstr(cmd, "off")) {
3772 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003774 ind = 1;
3775 } else if (strstr(cmd, "blink")) {
3776 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 } else
3778 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003779
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003780 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003781 /* 570 */
3782 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003784 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003786 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003787 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3788 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003789 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003790 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003791 ret = ec_write(TPACPI_LED_EC_HLBL,
3792 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003793 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003794 ret = ec_write(TPACPI_LED_EC_HLCL,
3795 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003796 if (ret < 0)
3797 return ret;
3798 } else {
3799 /* all others */
3800 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3801 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 }
3805
3806 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003807}
3808
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003809static struct ibm_struct led_driver_data = {
3810 .name = "led",
3811 .read = led_read,
3812 .write = led_write,
3813};
3814
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003815/*************************************************************************
3816 * Beep subdriver
3817 */
3818
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003819TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003820
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003821static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003822{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003823 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3824
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003825 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003826
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003827 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3828 str_supported(beep_handle != NULL));
3829
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003830 return (beep_handle)? 0 : 1;
3831}
3832
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003833static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
3835 int len = 0;
3836
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003837 if (!beep_handle)
3838 len += sprintf(p + len, "status:\t\tnot supported\n");
3839 else {
3840 len += sprintf(p + len, "status:\t\tsupported\n");
3841 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
3844 return len;
3845}
3846
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003847static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
3849 char *cmd;
3850 int beep_cmd;
3851
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003852 if (!beep_handle)
3853 return -ENODEV;
3854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003856 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3857 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 /* beep_cmd set */
3859 } else
3860 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003861 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return -EIO;
3863 }
3864
3865 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003866}
3867
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003868static struct ibm_struct beep_driver_data = {
3869 .name = "beep",
3870 .read = beep_read,
3871 .write = beep_write,
3872};
3873
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003874/*************************************************************************
3875 * Thermal subdriver
3876 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003877
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003878enum thermal_access_mode {
3879 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3880 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3881 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3882 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3883 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3884};
3885
3886enum { /* TPACPI_THERMAL_TPEC_* */
3887 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3888 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3889 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3890};
3891
3892#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3893struct ibm_thermal_sensors_struct {
3894 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3895};
3896
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003897static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003898
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003899/* idx is zero-based */
3900static int thermal_get_sensor(int idx, s32 *value)
3901{
3902 int t;
3903 s8 tmp;
3904 char tmpi[5];
3905
3906 t = TP_EC_THERMAL_TMP0;
3907
3908 switch (thermal_read_mode) {
3909#if TPACPI_MAX_THERMAL_SENSORS >= 16
3910 case TPACPI_THERMAL_TPEC_16:
3911 if (idx >= 8 && idx <= 15) {
3912 t = TP_EC_THERMAL_TMP8;
3913 idx -= 8;
3914 }
3915 /* fallthrough */
3916#endif
3917 case TPACPI_THERMAL_TPEC_8:
3918 if (idx <= 7) {
3919 if (!acpi_ec_read(t + idx, &tmp))
3920 return -EIO;
3921 *value = tmp * 1000;
3922 return 0;
3923 }
3924 break;
3925
3926 case TPACPI_THERMAL_ACPI_UPDT:
3927 if (idx <= 7) {
3928 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3929 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3930 return -EIO;
3931 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3932 return -EIO;
3933 *value = (t - 2732) * 100;
3934 return 0;
3935 }
3936 break;
3937
3938 case TPACPI_THERMAL_ACPI_TMP07:
3939 if (idx <= 7) {
3940 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3941 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3942 return -EIO;
3943 if (t > 127 || t < -127)
3944 t = TP_EC_THERMAL_TMP_NA;
3945 *value = t * 1000;
3946 return 0;
3947 }
3948 break;
3949
3950 case TPACPI_THERMAL_NONE:
3951 default:
3952 return -ENOSYS;
3953 }
3954
3955 return -EINVAL;
3956}
3957
3958static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3959{
3960 int res, i;
3961 int n;
3962
3963 n = 8;
3964 i = 0;
3965
3966 if (!s)
3967 return -EINVAL;
3968
3969 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3970 n = 16;
3971
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003972 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003973 res = thermal_get_sensor(i, &s->temp[i]);
3974 if (res)
3975 return res;
3976 }
3977
3978 return n;
3979}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003980
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003981/* sysfs temp##_input -------------------------------------------------- */
3982
3983static ssize_t thermal_temp_input_show(struct device *dev,
3984 struct device_attribute *attr,
3985 char *buf)
3986{
3987 struct sensor_device_attribute *sensor_attr =
3988 to_sensor_dev_attr(attr);
3989 int idx = sensor_attr->index;
3990 s32 value;
3991 int res;
3992
3993 res = thermal_get_sensor(idx, &value);
3994 if (res)
3995 return res;
3996 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3997 return -ENXIO;
3998
3999 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4000}
4001
4002#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004003 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4004 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004005
4006static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4007 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4008 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4009 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4010 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4011 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4012 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4013 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4014 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4015 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4016 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4017 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4018 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4019 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4020 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4021 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4022 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4023};
4024
4025#define THERMAL_ATTRS(X) \
4026 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4027
4028static struct attribute *thermal_temp_input_attr[] = {
4029 THERMAL_ATTRS(8),
4030 THERMAL_ATTRS(9),
4031 THERMAL_ATTRS(10),
4032 THERMAL_ATTRS(11),
4033 THERMAL_ATTRS(12),
4034 THERMAL_ATTRS(13),
4035 THERMAL_ATTRS(14),
4036 THERMAL_ATTRS(15),
4037 THERMAL_ATTRS(0),
4038 THERMAL_ATTRS(1),
4039 THERMAL_ATTRS(2),
4040 THERMAL_ATTRS(3),
4041 THERMAL_ATTRS(4),
4042 THERMAL_ATTRS(5),
4043 THERMAL_ATTRS(6),
4044 THERMAL_ATTRS(7),
4045 NULL
4046};
4047
4048static const struct attribute_group thermal_temp_input16_group = {
4049 .attrs = thermal_temp_input_attr
4050};
4051
4052static const struct attribute_group thermal_temp_input8_group = {
4053 .attrs = &thermal_temp_input_attr[8]
4054};
4055
4056#undef THERMAL_SENSOR_ATTR_TEMP
4057#undef THERMAL_ATTRS
4058
4059/* --------------------------------------------------------------------- */
4060
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004061static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004062{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004063 u8 t, ta1, ta2;
4064 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004065 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004066 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004067
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004068 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4069
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004070 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004071
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004072 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004073 /*
4074 * Direct EC access mode: sensors at registers
4075 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4076 * non-implemented, thermal sensors return 0x80 when
4077 * not available
4078 */
4079
4080 ta1 = ta2 = 0;
4081 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004082 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004083 ta1 |= t;
4084 } else {
4085 ta1 = 0;
4086 break;
4087 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004088 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004089 ta2 |= t;
4090 } else {
4091 ta1 = 0;
4092 break;
4093 }
4094 }
4095 if (ta1 == 0) {
4096 /* This is sheer paranoia, but we handle it anyway */
4097 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004098 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004099 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004100 "falling back to ACPI TMPx access "
4101 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004102 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004103 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004104 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004105 "ThinkPad ACPI EC access misbehaving, "
4106 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004107 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004108 }
4109 } else {
4110 thermal_read_mode =
4111 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004112 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004113 }
4114 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004115 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4116 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004117 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004118 } else {
4119 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004120 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004121 }
4122 } else {
4123 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004124 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004125 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004126
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004127 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4128 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4129 thermal_read_mode);
4130
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004131 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004132 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004133 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004134 &thermal_temp_input16_group);
4135 if (res)
4136 return res;
4137 break;
4138 case TPACPI_THERMAL_TPEC_8:
4139 case TPACPI_THERMAL_ACPI_TMP07:
4140 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004141 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004142 &thermal_temp_input8_group);
4143 if (res)
4144 return res;
4145 break;
4146 case TPACPI_THERMAL_NONE:
4147 default:
4148 return 1;
4149 }
4150
4151 return 0;
4152}
4153
4154static void thermal_exit(void)
4155{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004156 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004157 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004158 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004159 &thermal_temp_input16_group);
4160 break;
4161 case TPACPI_THERMAL_TPEC_8:
4162 case TPACPI_THERMAL_ACPI_TMP07:
4163 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004164 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004165 &thermal_temp_input16_group);
4166 break;
4167 case TPACPI_THERMAL_NONE:
4168 default:
4169 break;
4170 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004171}
4172
4173static int thermal_read(char *p)
4174{
4175 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004176 int n, i;
4177 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004178
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004179 n = thermal_get_sensors(&t);
4180 if (unlikely(n < 0))
4181 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004182
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004183 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004184
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004185 if (n > 0) {
4186 for (i = 0; i < (n - 1); i++)
4187 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4188 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4189 } else
4190 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004191
4192 return len;
4193}
4194
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004195static struct ibm_struct thermal_driver_data = {
4196 .name = "thermal",
4197 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004198 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004199};
4200
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004201/*************************************************************************
4202 * EC Dump subdriver
4203 */
4204
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004205static u8 ecdump_regs[256];
4206
4207static int ecdump_read(char *p)
4208{
4209 int len = 0;
4210 int i, j;
4211 u8 v;
4212
4213 len += sprintf(p + len, "EC "
4214 " +00 +01 +02 +03 +04 +05 +06 +07"
4215 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4216 for (i = 0; i < 256; i += 16) {
4217 len += sprintf(p + len, "EC 0x%02x:", i);
4218 for (j = 0; j < 16; j++) {
4219 if (!acpi_ec_read(i + j, &v))
4220 break;
4221 if (v != ecdump_regs[i + j])
4222 len += sprintf(p + len, " *%02x", v);
4223 else
4224 len += sprintf(p + len, " %02x", v);
4225 ecdump_regs[i + j] = v;
4226 }
4227 len += sprintf(p + len, "\n");
4228 if (j != 16)
4229 break;
4230 }
4231
4232 /* These are way too dangerous to advertise openly... */
4233#if 0
4234 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4235 " (<offset> is 00-ff, <value> is 00-ff)\n");
4236 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4237 " (<offset> is 00-ff, <value> is 0-255)\n");
4238#endif
4239 return len;
4240}
4241
4242static int ecdump_write(char *buf)
4243{
4244 char *cmd;
4245 int i, v;
4246
4247 while ((cmd = next_cmd(&buf))) {
4248 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4249 /* i and v set */
4250 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4251 /* i and v set */
4252 } else
4253 return -EINVAL;
4254 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4255 if (!acpi_ec_write(i, v))
4256 return -EIO;
4257 } else
4258 return -EINVAL;
4259 }
4260
4261 return 0;
4262}
4263
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004264static struct ibm_struct ecdump_driver_data = {
4265 .name = "ecdump",
4266 .read = ecdump_read,
4267 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004268 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004269};
4270
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004271/*************************************************************************
4272 * Backlight/brightness subdriver
4273 */
Holger Macht8acb0252006-10-20 14:30:28 -07004274
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004275#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4276
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004277static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004278static int brightness_offset = 0x31;
4279static int brightness_mode;
4280static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4281
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004282static struct mutex brightness_mutex;
4283
4284/*
4285 * ThinkPads can read brightness from two places: EC 0x31, or
4286 * CMOS NVRAM byte 0x5E, bits 0-3.
4287 */
4288static int brightness_get(struct backlight_device *bd)
4289{
4290 u8 lec = 0, lcmos = 0, level = 0;
4291
4292 if (brightness_mode & 1) {
4293 if (!acpi_ec_read(brightness_offset, &lec))
4294 return -EIO;
4295 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4296 level = lec;
4297 };
4298 if (brightness_mode & 2) {
4299 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4300 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4301 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4302 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4303 level = lcmos;
4304 }
4305
4306 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004307 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004308 "CMOS NVRAM (%u) and EC (%u) do not agree "
4309 "on display brightness level\n",
4310 (unsigned int) lcmos,
4311 (unsigned int) lec);
4312 return -EIO;
4313 }
4314
4315 return level;
4316}
4317
4318/* May return EINTR which can always be mapped to ERESTARTSYS */
4319static int brightness_set(int value)
4320{
4321 int cmos_cmd, inc, i, res;
4322 int current_value;
4323
4324 if (value > ((tp_features.bright_16levels)? 15 : 7))
4325 return -EINVAL;
4326
4327 res = mutex_lock_interruptible(&brightness_mutex);
4328 if (res < 0)
4329 return res;
4330
4331 current_value = brightness_get(NULL);
4332 if (current_value < 0) {
4333 res = current_value;
4334 goto errout;
4335 }
4336
4337 cmos_cmd = value > current_value ?
4338 TP_CMOS_BRIGHTNESS_UP :
4339 TP_CMOS_BRIGHTNESS_DOWN;
4340 inc = (value > current_value)? 1 : -1;
4341
4342 res = 0;
4343 for (i = current_value; i != value; i += inc) {
4344 if ((brightness_mode & 2) &&
4345 issue_thinkpad_cmos_command(cmos_cmd)) {
4346 res = -EIO;
4347 goto errout;
4348 }
4349 if ((brightness_mode & 1) &&
4350 !acpi_ec_write(brightness_offset, i + inc)) {
4351 res = -EIO;
4352 goto errout;;
4353 }
4354 }
4355
4356errout:
4357 mutex_unlock(&brightness_mutex);
4358 return res;
4359}
4360
4361/* sysfs backlight class ----------------------------------------------- */
4362
4363static int brightness_update_status(struct backlight_device *bd)
4364{
4365 /* it is the backlight class's job (caller) to handle
4366 * EINTR and other errors properly */
4367 return brightness_set(
4368 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4369 bd->props.power == FB_BLANK_UNBLANK) ?
4370 bd->props.brightness : 0);
4371}
Holger Macht8acb0252006-10-20 14:30:28 -07004372
Richard Purdie599a52d2007-02-10 23:07:48 +00004373static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004374 .get_brightness = brightness_get,
4375 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004376};
4377
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004378/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004379
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004380static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004381{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004382 int b;
4383
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004384 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4385
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004386 mutex_init(&brightness_mutex);
4387
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004388 /*
4389 * We always attempt to detect acpi support, so as to switch
4390 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4391 * going to publish a backlight interface
4392 */
4393 b = tpacpi_check_std_acpi_brightness_support();
4394 if (b > 0) {
4395 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4396 printk(TPACPI_NOTICE
4397 "Lenovo BIOS switched to ACPI backlight "
4398 "control mode\n");
4399 }
4400 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004401 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004402 "standard ACPI backlight interface "
4403 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004404 return 1;
4405 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004406 }
4407
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004408 if (!brightness_enable) {
4409 dbg_printk(TPACPI_DBG_INIT,
4410 "brightness support disabled by "
4411 "module parameter\n");
4412 return 1;
4413 }
4414
4415 if (b > 16) {
4416 printk(TPACPI_ERR
4417 "Unsupported brightness interface, "
4418 "please contact %s\n", TPACPI_MAIL);
4419 return 1;
4420 }
4421 if (b == 16)
4422 tp_features.bright_16levels = 1;
4423
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004424 if (!brightness_mode) {
4425 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4426 brightness_mode = 2;
4427 else
4428 brightness_mode = 3;
4429
4430 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4431 brightness_mode);
4432 }
4433
4434 if (brightness_mode > 3)
4435 return -EINVAL;
4436
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004437 b = brightness_get(NULL);
4438 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004439 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004440
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004441 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004442 printk(TPACPI_INFO
4443 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004444
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004445 ibm_backlight_device = backlight_device_register(
4446 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4447 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004448 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004449 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004450 return PTR_ERR(ibm_backlight_device);
4451 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004452 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004453
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004454 ibm_backlight_device->props.max_brightness =
4455 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004456 ibm_backlight_device->props.brightness = b;
4457 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004458
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004459 return 0;
4460}
4461
4462static void brightness_exit(void)
4463{
4464 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004465 vdbg_printk(TPACPI_DBG_EXIT,
4466 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004467 backlight_device_unregister(ibm_backlight_device);
4468 ibm_backlight_device = NULL;
4469 }
4470}
4471
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004472static int brightness_read(char *p)
4473{
4474 int len = 0;
4475 int level;
4476
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004477 level = brightness_get(NULL);
4478 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004479 len += sprintf(p + len, "level:\t\tunreadable\n");
4480 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004481 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004482 len += sprintf(p + len, "commands:\tup, down\n");
4483 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004484 " (<level> is 0-%d)\n",
4485 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004486 }
4487
4488 return len;
4489}
4490
4491static int brightness_write(char *buf)
4492{
4493 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004494 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004495 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004496 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004497
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004498 level = brightness_get(NULL);
4499 if (level < 0)
4500 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004501
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004502 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004503 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004504 if (level < max_level)
4505 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004506 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004507 if (level > 0)
4508 level--;
4509 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4510 level >= 0 && level <= max_level) {
4511 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004512 } else
4513 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004514 }
4515
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004516 /*
4517 * Now we know what the final level should be, so we try to set it.
4518 * Doing it this way makes the syscall restartable in case of EINTR
4519 */
4520 rc = brightness_set(level);
4521 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004522}
4523
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004524static struct ibm_struct brightness_driver_data = {
4525 .name = "brightness",
4526 .read = brightness_read,
4527 .write = brightness_write,
4528 .exit = brightness_exit,
4529};
4530
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004531/*************************************************************************
4532 * Volume subdriver
4533 */
4534
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004535static int volume_offset = 0x30;
4536
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004537static int volume_read(char *p)
4538{
4539 int len = 0;
4540 u8 level;
4541
4542 if (!acpi_ec_read(volume_offset, &level)) {
4543 len += sprintf(p + len, "level:\t\tunreadable\n");
4544 } else {
4545 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4546 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4547 len += sprintf(p + len, "commands:\tup, down, mute\n");
4548 len += sprintf(p + len, "commands:\tlevel <level>"
4549 " (<level> is 0-15)\n");
4550 }
4551
4552 return len;
4553}
4554
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004555static int volume_write(char *buf)
4556{
4557 int cmos_cmd, inc, i;
4558 u8 level, mute;
4559 int new_level, new_mute;
4560 char *cmd;
4561
4562 while ((cmd = next_cmd(&buf))) {
4563 if (!acpi_ec_read(volume_offset, &level))
4564 return -EIO;
4565 new_mute = mute = level & 0x40;
4566 new_level = level = level & 0xf;
4567
4568 if (strlencmp(cmd, "up") == 0) {
4569 if (mute)
4570 new_mute = 0;
4571 else
4572 new_level = level == 15 ? 15 : level + 1;
4573 } else if (strlencmp(cmd, "down") == 0) {
4574 if (mute)
4575 new_mute = 0;
4576 else
4577 new_level = level == 0 ? 0 : level - 1;
4578 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4579 new_level >= 0 && new_level <= 15) {
4580 /* new_level set */
4581 } else if (strlencmp(cmd, "mute") == 0) {
4582 new_mute = 0x40;
4583 } else
4584 return -EINVAL;
4585
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004586 if (new_level != level) {
4587 /* mute doesn't change */
4588
4589 cmos_cmd = (new_level > level) ?
4590 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004591 inc = new_level > level ? 1 : -1;
4592
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004593 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004594 !acpi_ec_write(volume_offset, level)))
4595 return -EIO;
4596
4597 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004598 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004599 !acpi_ec_write(volume_offset, i + inc))
4600 return -EIO;
4601
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004602 if (mute &&
4603 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4604 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004605 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004606 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004607 }
4608
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004609 if (new_mute != mute) {
4610 /* level doesn't change */
4611
4612 cmos_cmd = (new_mute) ?
4613 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004614
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, level + new_mute))
4617 return -EIO;
4618 }
4619 }
4620
4621 return 0;
4622}
4623
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004624static struct ibm_struct volume_driver_data = {
4625 .name = "volume",
4626 .read = volume_read,
4627 .write = volume_write,
4628};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004629
4630/*************************************************************************
4631 * Fan subdriver
4632 */
4633
4634/*
4635 * FAN ACCESS MODES
4636 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004637 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004638 * ACPI GFAN method: returns fan level
4639 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004640 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004641 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004642 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004643 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004644 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4645 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004646 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4647 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004648 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004649 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004650 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4651 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004652 *
4653 * Fan speed changes of any sort (including those caused by the
4654 * disengaged mode) are usually done slowly by the firmware as the
4655 * maximum ammount of fan duty cycle change per second seems to be
4656 * limited.
4657 *
4658 * Reading is not available if GFAN exists.
4659 * Writing is not available if SFAN exists.
4660 *
4661 * Bits
4662 * 7 automatic mode engaged;
4663 * (default operation mode of the ThinkPad)
4664 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004665 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004666 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004667 * the tachometer while the fan controller ramps up
4668 * the speed (which can take up to a few *minutes*).
4669 * Speeds up fan to 100% duty-cycle, which is far above
4670 * the standard RPM levels. It is not impossible that
4671 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004672 * 5-3 unused in some models. Extra bits for fan level
4673 * in others, but still useless as all values above
4674 * 7 map to the same speed as level 7 in these models.
4675 * 2-0 fan level (0..7 usually)
4676 * 0x00 = stop
4677 * 0x07 = max (set when temperatures critical)
4678 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004679 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004680 *
4681 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4682 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4683 * does so, its initial value is meaningless (0x07).
4684 *
4685 * For firmware bugs, refer to:
4686 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4687 *
4688 * ----
4689 *
4690 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4691 * Main fan tachometer reading (in RPM)
4692 *
4693 * This register is present on all ThinkPads with a new-style EC, and
4694 * it is known not to be present on the A21m/e, and T22, as there is
4695 * something else in offset 0x84 according to the ACPI DSDT. Other
4696 * ThinkPads from this same time period (and earlier) probably lack the
4697 * tachometer as well.
4698 *
4699 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4700 * was never fixed by IBM to report the EC firmware version string
4701 * probably support the tachometer (like the early X models), so
4702 * detecting it is quite hard. We need more data to know for sure.
4703 *
4704 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4705 * might result.
4706 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004707 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4708 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004709 *
4710 * For firmware bugs, refer to:
4711 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4712 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004713 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004714 * ThinkPad X31, X40, X41. Not available in the X60.
4715 *
4716 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4717 * high speed. ACPI DSDT seems to map these three speeds to levels
4718 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4719 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4720 *
4721 * The speeds are stored on handles
4722 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4723 *
4724 * There are three default speed sets, acessible as handles:
4725 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4726 *
4727 * ACPI DSDT switches which set is in use depending on various
4728 * factors.
4729 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004730 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004731 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4732 * but the ACPI tables just mention level 7.
4733 */
4734
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004735enum { /* Fan control constants */
4736 fan_status_offset = 0x2f, /* EC register 0x2f */
4737 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4738 * 0x84 must be read before 0x85 */
4739
4740 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4741 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4742
4743 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4744};
4745
4746enum fan_status_access_mode {
4747 TPACPI_FAN_NONE = 0, /* No fan status or control */
4748 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4749 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4750};
4751
4752enum fan_control_access_mode {
4753 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4754 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4755 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4756 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4757};
4758
4759enum fan_control_commands {
4760 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4761 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4762 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4763 * and also watchdog cmd */
4764};
4765
4766static int fan_control_allowed;
4767
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004768static enum fan_status_access_mode fan_status_access_mode;
4769static enum fan_control_access_mode fan_control_access_mode;
4770static enum fan_control_commands fan_control_commands;
4771
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004772static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004773static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004774static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004775
4776static struct mutex fan_mutex;
4777
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004778static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004779static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004780
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004781TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4782TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004783 "\\FSPD", /* 600e/x, 770e, 770x */
4784 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004785TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004786 "JFNS", /* 770x-JL */
4787 ); /* all others */
4788
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004789/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004790 * Call with fan_mutex held
4791 */
4792static void fan_update_desired_level(u8 status)
4793{
4794 if ((status &
4795 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4796 if (status > 7)
4797 fan_control_desired_level = 7;
4798 else
4799 fan_control_desired_level = status;
4800 }
4801}
4802
4803static int fan_get_status(u8 *status)
4804{
4805 u8 s;
4806
4807 /* TODO:
4808 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4809
4810 switch (fan_status_access_mode) {
4811 case TPACPI_FAN_RD_ACPI_GFAN:
4812 /* 570, 600e/x, 770e, 770x */
4813
4814 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4815 return -EIO;
4816
4817 if (likely(status))
4818 *status = s & 0x07;
4819
4820 break;
4821
4822 case TPACPI_FAN_RD_TPEC:
4823 /* all except 570, 600e/x, 770e, 770x */
4824 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4825 return -EIO;
4826
4827 if (likely(status))
4828 *status = s;
4829
4830 break;
4831
4832 default:
4833 return -ENXIO;
4834 }
4835
4836 return 0;
4837}
4838
4839static int fan_get_status_safe(u8 *status)
4840{
4841 int rc;
4842 u8 s;
4843
4844 if (mutex_lock_interruptible(&fan_mutex))
4845 return -ERESTARTSYS;
4846 rc = fan_get_status(&s);
4847 if (!rc)
4848 fan_update_desired_level(s);
4849 mutex_unlock(&fan_mutex);
4850
4851 if (status)
4852 *status = s;
4853
4854 return rc;
4855}
4856
4857static int fan_get_speed(unsigned int *speed)
4858{
4859 u8 hi, lo;
4860
4861 switch (fan_status_access_mode) {
4862 case TPACPI_FAN_RD_TPEC:
4863 /* all except 570, 600e/x, 770e, 770x */
4864 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4865 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4866 return -EIO;
4867
4868 if (likely(speed))
4869 *speed = (hi << 8) | lo;
4870
4871 break;
4872
4873 default:
4874 return -ENXIO;
4875 }
4876
4877 return 0;
4878}
4879
4880static int fan_set_level(int level)
4881{
4882 if (!fan_control_allowed)
4883 return -EPERM;
4884
4885 switch (fan_control_access_mode) {
4886 case TPACPI_FAN_WR_ACPI_SFAN:
4887 if (level >= 0 && level <= 7) {
4888 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4889 return -EIO;
4890 } else
4891 return -EINVAL;
4892 break;
4893
4894 case TPACPI_FAN_WR_ACPI_FANS:
4895 case TPACPI_FAN_WR_TPEC:
4896 if ((level != TP_EC_FAN_AUTO) &&
4897 (level != TP_EC_FAN_FULLSPEED) &&
4898 ((level < 0) || (level > 7)))
4899 return -EINVAL;
4900
4901 /* safety net should the EC not support AUTO
4902 * or FULLSPEED mode bits and just ignore them */
4903 if (level & TP_EC_FAN_FULLSPEED)
4904 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01004905 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004906 level |= 4; /* safety min speed 4 */
4907
4908 if (!acpi_ec_write(fan_status_offset, level))
4909 return -EIO;
4910 else
4911 tp_features.fan_ctrl_status_undef = 0;
4912 break;
4913
4914 default:
4915 return -ENXIO;
4916 }
4917 return 0;
4918}
4919
4920static int fan_set_level_safe(int level)
4921{
4922 int rc;
4923
4924 if (!fan_control_allowed)
4925 return -EPERM;
4926
4927 if (mutex_lock_interruptible(&fan_mutex))
4928 return -ERESTARTSYS;
4929
4930 if (level == TPACPI_FAN_LAST_LEVEL)
4931 level = fan_control_desired_level;
4932
4933 rc = fan_set_level(level);
4934 if (!rc)
4935 fan_update_desired_level(level);
4936
4937 mutex_unlock(&fan_mutex);
4938 return rc;
4939}
4940
4941static int fan_set_enable(void)
4942{
4943 u8 s;
4944 int rc;
4945
4946 if (!fan_control_allowed)
4947 return -EPERM;
4948
4949 if (mutex_lock_interruptible(&fan_mutex))
4950 return -ERESTARTSYS;
4951
4952 switch (fan_control_access_mode) {
4953 case TPACPI_FAN_WR_ACPI_FANS:
4954 case TPACPI_FAN_WR_TPEC:
4955 rc = fan_get_status(&s);
4956 if (rc < 0)
4957 break;
4958
4959 /* Don't go out of emergency fan mode */
4960 if (s != 7) {
4961 s &= 0x07;
4962 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4963 }
4964
4965 if (!acpi_ec_write(fan_status_offset, s))
4966 rc = -EIO;
4967 else {
4968 tp_features.fan_ctrl_status_undef = 0;
4969 rc = 0;
4970 }
4971 break;
4972
4973 case TPACPI_FAN_WR_ACPI_SFAN:
4974 rc = fan_get_status(&s);
4975 if (rc < 0)
4976 break;
4977
4978 s &= 0x07;
4979
4980 /* Set fan to at least level 4 */
4981 s |= 4;
4982
4983 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004984 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004985 else
4986 rc = 0;
4987 break;
4988
4989 default:
4990 rc = -ENXIO;
4991 }
4992
4993 mutex_unlock(&fan_mutex);
4994 return rc;
4995}
4996
4997static int fan_set_disable(void)
4998{
4999 int rc;
5000
5001 if (!fan_control_allowed)
5002 return -EPERM;
5003
5004 if (mutex_lock_interruptible(&fan_mutex))
5005 return -ERESTARTSYS;
5006
5007 rc = 0;
5008 switch (fan_control_access_mode) {
5009 case TPACPI_FAN_WR_ACPI_FANS:
5010 case TPACPI_FAN_WR_TPEC:
5011 if (!acpi_ec_write(fan_status_offset, 0x00))
5012 rc = -EIO;
5013 else {
5014 fan_control_desired_level = 0;
5015 tp_features.fan_ctrl_status_undef = 0;
5016 }
5017 break;
5018
5019 case TPACPI_FAN_WR_ACPI_SFAN:
5020 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5021 rc = -EIO;
5022 else
5023 fan_control_desired_level = 0;
5024 break;
5025
5026 default:
5027 rc = -ENXIO;
5028 }
5029
5030
5031 mutex_unlock(&fan_mutex);
5032 return rc;
5033}
5034
5035static int fan_set_speed(int speed)
5036{
5037 int rc;
5038
5039 if (!fan_control_allowed)
5040 return -EPERM;
5041
5042 if (mutex_lock_interruptible(&fan_mutex))
5043 return -ERESTARTSYS;
5044
5045 rc = 0;
5046 switch (fan_control_access_mode) {
5047 case TPACPI_FAN_WR_ACPI_FANS:
5048 if (speed >= 0 && speed <= 65535) {
5049 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5050 speed, speed, speed))
5051 rc = -EIO;
5052 } else
5053 rc = -EINVAL;
5054 break;
5055
5056 default:
5057 rc = -ENXIO;
5058 }
5059
5060 mutex_unlock(&fan_mutex);
5061 return rc;
5062}
5063
5064static void fan_watchdog_reset(void)
5065{
5066 static int fan_watchdog_active;
5067
5068 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5069 return;
5070
5071 if (fan_watchdog_active)
5072 cancel_delayed_work(&fan_watchdog_task);
5073
5074 if (fan_watchdog_maxinterval > 0 &&
5075 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5076 fan_watchdog_active = 1;
5077 if (!schedule_delayed_work(&fan_watchdog_task,
5078 msecs_to_jiffies(fan_watchdog_maxinterval
5079 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005080 printk(TPACPI_ERR
5081 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005082 "watchdog will not trigger\n");
5083 }
5084 } else
5085 fan_watchdog_active = 0;
5086}
5087
5088static void fan_watchdog_fire(struct work_struct *ignored)
5089{
5090 int rc;
5091
5092 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5093 return;
5094
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005095 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005096 rc = fan_set_enable();
5097 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005098 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005099 "will try again later...\n", -rc);
5100 /* reschedule for later */
5101 fan_watchdog_reset();
5102 }
5103}
5104
5105/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005106 * SYSFS fan layout: hwmon compatible (device)
5107 *
5108 * pwm*_enable:
5109 * 0: "disengaged" mode
5110 * 1: manual mode
5111 * 2: native EC "auto" mode (recommended, hardware default)
5112 *
5113 * pwm*: set speed in manual mode, ignored otherwise.
5114 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5115 * interpolation.
5116 *
5117 * fan*_input: tachometer reading, RPM
5118 *
5119 *
5120 * SYSFS fan layout: extensions
5121 *
5122 * fan_watchdog (driver):
5123 * fan watchdog interval in seconds, 0 disables (default), max 120
5124 */
5125
5126/* sysfs fan pwm1_enable ----------------------------------------------- */
5127static ssize_t fan_pwm1_enable_show(struct device *dev,
5128 struct device_attribute *attr,
5129 char *buf)
5130{
5131 int res, mode;
5132 u8 status;
5133
5134 res = fan_get_status_safe(&status);
5135 if (res)
5136 return res;
5137
5138 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5139 if (status != fan_control_initial_status) {
5140 tp_features.fan_ctrl_status_undef = 0;
5141 } else {
5142 /* Return most likely status. In fact, it
5143 * might be the only possible status */
5144 status = TP_EC_FAN_AUTO;
5145 }
5146 }
5147
5148 if (status & TP_EC_FAN_FULLSPEED) {
5149 mode = 0;
5150 } else if (status & TP_EC_FAN_AUTO) {
5151 mode = 2;
5152 } else
5153 mode = 1;
5154
5155 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5156}
5157
5158static ssize_t fan_pwm1_enable_store(struct device *dev,
5159 struct device_attribute *attr,
5160 const char *buf, size_t count)
5161{
5162 unsigned long t;
5163 int res, level;
5164
5165 if (parse_strtoul(buf, 2, &t))
5166 return -EINVAL;
5167
5168 switch (t) {
5169 case 0:
5170 level = TP_EC_FAN_FULLSPEED;
5171 break;
5172 case 1:
5173 level = TPACPI_FAN_LAST_LEVEL;
5174 break;
5175 case 2:
5176 level = TP_EC_FAN_AUTO;
5177 break;
5178 case 3:
5179 /* reserved for software-controlled auto mode */
5180 return -ENOSYS;
5181 default:
5182 return -EINVAL;
5183 }
5184
5185 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005186 if (res == -ENXIO)
5187 return -EINVAL;
5188 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005189 return res;
5190
5191 fan_watchdog_reset();
5192
5193 return count;
5194}
5195
5196static struct device_attribute dev_attr_fan_pwm1_enable =
5197 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5198 fan_pwm1_enable_show, fan_pwm1_enable_store);
5199
5200/* sysfs fan pwm1 ------------------------------------------------------ */
5201static ssize_t fan_pwm1_show(struct device *dev,
5202 struct device_attribute *attr,
5203 char *buf)
5204{
5205 int res;
5206 u8 status;
5207
5208 res = fan_get_status_safe(&status);
5209 if (res)
5210 return res;
5211
5212 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5213 if (status != fan_control_initial_status) {
5214 tp_features.fan_ctrl_status_undef = 0;
5215 } else {
5216 status = TP_EC_FAN_AUTO;
5217 }
5218 }
5219
5220 if ((status &
5221 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5222 status = fan_control_desired_level;
5223
5224 if (status > 7)
5225 status = 7;
5226
5227 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5228}
5229
5230static ssize_t fan_pwm1_store(struct device *dev,
5231 struct device_attribute *attr,
5232 const char *buf, size_t count)
5233{
5234 unsigned long s;
5235 int rc;
5236 u8 status, newlevel;
5237
5238 if (parse_strtoul(buf, 255, &s))
5239 return -EINVAL;
5240
5241 /* scale down from 0-255 to 0-7 */
5242 newlevel = (s >> 5) & 0x07;
5243
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005244 if (mutex_lock_interruptible(&fan_mutex))
5245 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005246
5247 rc = fan_get_status(&status);
5248 if (!rc && (status &
5249 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5250 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005251 if (rc == -ENXIO)
5252 rc = -EINVAL;
5253 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005254 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005255 fan_watchdog_reset();
5256 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005257 }
5258
5259 mutex_unlock(&fan_mutex);
5260 return (rc)? rc : count;
5261}
5262
5263static struct device_attribute dev_attr_fan_pwm1 =
5264 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5265 fan_pwm1_show, fan_pwm1_store);
5266
5267/* sysfs fan fan1_input ------------------------------------------------ */
5268static ssize_t fan_fan1_input_show(struct device *dev,
5269 struct device_attribute *attr,
5270 char *buf)
5271{
5272 int res;
5273 unsigned int speed;
5274
5275 res = fan_get_speed(&speed);
5276 if (res < 0)
5277 return res;
5278
5279 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5280}
5281
5282static struct device_attribute dev_attr_fan_fan1_input =
5283 __ATTR(fan1_input, S_IRUGO,
5284 fan_fan1_input_show, NULL);
5285
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005286/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005287static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5288 char *buf)
5289{
5290 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5291}
5292
5293static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5294 const char *buf, size_t count)
5295{
5296 unsigned long t;
5297
5298 if (parse_strtoul(buf, 120, &t))
5299 return -EINVAL;
5300
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005301 if (!fan_control_allowed)
5302 return -EPERM;
5303
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005304 fan_watchdog_maxinterval = t;
5305 fan_watchdog_reset();
5306
5307 return count;
5308}
5309
5310static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5311 fan_fan_watchdog_show, fan_fan_watchdog_store);
5312
5313/* --------------------------------------------------------------------- */
5314static struct attribute *fan_attributes[] = {
5315 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5316 &dev_attr_fan_fan1_input.attr,
5317 NULL
5318};
5319
5320static const struct attribute_group fan_attr_group = {
5321 .attrs = fan_attributes,
5322};
5323
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005324static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005325{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005326 int rc;
5327
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005328 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5329
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005330 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005331 fan_status_access_mode = TPACPI_FAN_NONE;
5332 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005333 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005334 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005335 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005336 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005337
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005338 TPACPI_ACPIHANDLE_INIT(fans);
5339 TPACPI_ACPIHANDLE_INIT(gfan);
5340 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005341
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005342 if (gfan_handle) {
5343 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005344 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005345 } else {
5346 /* all other ThinkPads: note that even old-style
5347 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005348 if (likely(acpi_ec_read(fan_status_offset,
5349 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005350 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005351
5352 /* In some ThinkPads, neither the EC nor the ACPI
5353 * DSDT initialize the fan status, and it ends up
5354 * being set to 0x07 when it *could* be either
5355 * 0x07 or 0x80.
5356 *
5357 * Enable for TP-1Y (T43), TP-78 (R51e),
5358 * TP-76 (R52), TP-70 (T43, R52), which are known
5359 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005360 if (fan_control_initial_status == 0x07) {
5361 switch (thinkpad_id.ec_model) {
5362 case 0x5931: /* TP-1Y */
5363 case 0x3837: /* TP-78 */
5364 case 0x3637: /* TP-76 */
5365 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005366 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005367 "fan_init: initial fan status "
5368 "is unknown, assuming it is "
5369 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005370 tp_features.fan_ctrl_status_undef = 1;
5371 ;;
5372 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005373 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005374 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005375 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005376 "ThinkPad ACPI EC access misbehaving, "
5377 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005378 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005379 }
5380 }
5381
5382 if (sfan_handle) {
5383 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005384 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005385 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005386 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005387 } else {
5388 if (!gfan_handle) {
5389 /* gfan without sfan means no fan control */
5390 /* all other models implement TP EC 0x2f control */
5391
5392 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005393 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005394 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005395 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005396 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005397 TPACPI_FAN_CMD_SPEED |
5398 TPACPI_FAN_CMD_LEVEL |
5399 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005400 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005401 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005402 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005403 TPACPI_FAN_CMD_LEVEL |
5404 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005405 }
5406 }
5407 }
5408
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005409 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5410 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5411 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5412 fan_status_access_mode, fan_control_access_mode);
5413
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005414 /* fan control master switch */
5415 if (!fan_control_allowed) {
5416 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5417 fan_control_commands = 0;
5418 dbg_printk(TPACPI_DBG_INIT,
5419 "fan control features disabled by parameter\n");
5420 }
5421
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005422 /* update fan_control_desired_level */
5423 if (fan_status_access_mode != TPACPI_FAN_NONE)
5424 fan_get_status_safe(NULL);
5425
5426 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5427 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005428 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005429 &fan_attr_group);
5430 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005431 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005432 &driver_attr_fan_watchdog);
5433 if (rc < 0)
5434 return rc;
5435 return 0;
5436 } else
5437 return 1;
5438}
5439
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005440static void fan_exit(void)
5441{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005442 vdbg_printk(TPACPI_DBG_EXIT,
5443 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005444
5445 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005446 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005447 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5448 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005449
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005450 cancel_delayed_work(&fan_watchdog_task);
5451 flush_scheduled_work();
5452}
5453
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005454static int fan_read(char *p)
5455{
5456 int len = 0;
5457 int rc;
5458 u8 status;
5459 unsigned int speed = 0;
5460
5461 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005462 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005463 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005464 rc = fan_get_status_safe(&status);
5465 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005466 return rc;
5467
5468 len += sprintf(p + len, "status:\t\t%s\n"
5469 "level:\t\t%d\n",
5470 (status != 0) ? "enabled" : "disabled", status);
5471 break;
5472
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005473 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005474 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005475 rc = fan_get_status_safe(&status);
5476 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005477 return rc;
5478
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005479 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005480 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005481 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005482 else
5483 /* Return most likely status. In fact, it
5484 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005485 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005486 }
5487
5488 len += sprintf(p + len, "status:\t\t%s\n",
5489 (status != 0) ? "enabled" : "disabled");
5490
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005491 rc = fan_get_speed(&speed);
5492 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005493 return rc;
5494
5495 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5496
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005497 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005498 /* Disengaged mode takes precedence */
5499 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005500 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005501 len += sprintf(p + len, "level:\t\tauto\n");
5502 else
5503 len += sprintf(p + len, "level:\t\t%d\n", status);
5504 break;
5505
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005506 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005507 default:
5508 len += sprintf(p + len, "status:\t\tnot supported\n");
5509 }
5510
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005511 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005512 len += sprintf(p + len, "commands:\tlevel <level>");
5513
5514 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005515 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005516 len += sprintf(p + len, " (<level> is 0-7)\n");
5517 break;
5518
5519 default:
5520 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005521 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005522 break;
5523 }
5524 }
5525
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005526 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005527 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005528 "commands:\twatchdog <timeout> (<timeout> "
5529 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005530
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005531 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005532 len += sprintf(p + len, "commands:\tspeed <speed>"
5533 " (<speed> is 0-65535)\n");
5534
5535 return len;
5536}
5537
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005538static int fan_write_cmd_level(const char *cmd, int *rc)
5539{
5540 int level;
5541
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005542 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005543 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005544 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005545 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005546 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005547 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005548 return 0;
5549
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005550 *rc = fan_set_level_safe(level);
5551 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005552 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005553 "access mode %d", fan_control_access_mode);
5554
5555 return 1;
5556}
5557
5558static int fan_write_cmd_enable(const char *cmd, int *rc)
5559{
5560 if (strlencmp(cmd, "enable") != 0)
5561 return 0;
5562
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005563 *rc = fan_set_enable();
5564 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005565 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005566 "access mode %d", fan_control_access_mode);
5567
5568 return 1;
5569}
5570
5571static int fan_write_cmd_disable(const char *cmd, int *rc)
5572{
5573 if (strlencmp(cmd, "disable") != 0)
5574 return 0;
5575
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005576 *rc = fan_set_disable();
5577 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005578 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005579 "access mode %d", fan_control_access_mode);
5580
5581 return 1;
5582}
5583
5584static int fan_write_cmd_speed(const char *cmd, int *rc)
5585{
5586 int speed;
5587
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005588 /* TODO:
5589 * Support speed <low> <medium> <high> ? */
5590
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005591 if (sscanf(cmd, "speed %d", &speed) != 1)
5592 return 0;
5593
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005594 *rc = fan_set_speed(speed);
5595 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005596 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005597 "access mode %d", fan_control_access_mode);
5598
5599 return 1;
5600}
5601
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005602static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5603{
5604 int interval;
5605
5606 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5607 return 0;
5608
5609 if (interval < 0 || interval > 120)
5610 *rc = -EINVAL;
5611 else
5612 fan_watchdog_maxinterval = interval;
5613
5614 return 1;
5615}
5616
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005617static int fan_write(char *buf)
5618{
5619 char *cmd;
5620 int rc = 0;
5621
5622 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005623 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005624 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005625 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005626 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005627 fan_write_cmd_disable(cmd, &rc) ||
5628 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005629 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005630 fan_write_cmd_speed(cmd, &rc))
5631 )
5632 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005633 else if (!rc)
5634 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005635 }
5636
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005637 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005638}
5639
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005640static struct ibm_struct fan_driver_data = {
5641 .name = "fan",
5642 .read = fan_read,
5643 .write = fan_write,
5644 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005645};
5646
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005647/****************************************************************************
5648 ****************************************************************************
5649 *
5650 * Infrastructure
5651 *
5652 ****************************************************************************
5653 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005655/* sysfs name ---------------------------------------------------------- */
5656static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5657 struct device_attribute *attr,
5658 char *buf)
5659{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005660 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005661}
5662
5663static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5664 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5665
5666/* --------------------------------------------------------------------- */
5667
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005668/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005669static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005670
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005671/*
5672 * Module and infrastructure proble, init and exit handling
5673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005675static int force_load;
5676
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005677#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005678static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005679{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005680 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005681
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005682 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005683}
5684#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5685
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005686static void ibm_exit(struct ibm_struct *ibm)
5687{
5688 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5689
5690 list_del_init(&ibm->all_drivers);
5691
5692 if (ibm->flags.acpi_notify_installed) {
5693 dbg_printk(TPACPI_DBG_EXIT,
5694 "%s: acpi_remove_notify_handler\n", ibm->name);
5695 BUG_ON(!ibm->acpi);
5696 acpi_remove_notify_handler(*ibm->acpi->handle,
5697 ibm->acpi->type,
5698 dispatch_acpi_notify);
5699 ibm->flags.acpi_notify_installed = 0;
5700 ibm->flags.acpi_notify_installed = 0;
5701 }
5702
5703 if (ibm->flags.proc_created) {
5704 dbg_printk(TPACPI_DBG_EXIT,
5705 "%s: remove_proc_entry\n", ibm->name);
5706 remove_proc_entry(ibm->name, proc_dir);
5707 ibm->flags.proc_created = 0;
5708 }
5709
5710 if (ibm->flags.acpi_driver_registered) {
5711 dbg_printk(TPACPI_DBG_EXIT,
5712 "%s: acpi_bus_unregister_driver\n", ibm->name);
5713 BUG_ON(!ibm->acpi);
5714 acpi_bus_unregister_driver(ibm->acpi->driver);
5715 kfree(ibm->acpi->driver);
5716 ibm->acpi->driver = NULL;
5717 ibm->flags.acpi_driver_registered = 0;
5718 }
5719
5720 if (ibm->flags.init_called && ibm->exit) {
5721 ibm->exit();
5722 ibm->flags.init_called = 0;
5723 }
5724
5725 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5726}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005727
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005728static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729{
5730 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005731 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 struct proc_dir_entry *entry;
5733
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005734 BUG_ON(ibm == NULL);
5735
5736 INIT_LIST_HEAD(&ibm->all_drivers);
5737
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005738 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 return 0;
5740
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005741 dbg_printk(TPACPI_DBG_INIT,
5742 "probing for %s\n", ibm->name);
5743
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005744 if (iibm->init) {
5745 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005746 if (ret > 0)
5747 return 0; /* probe failed */
5748 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005750
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005751 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 }
5753
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005754 if (ibm->acpi) {
5755 if (ibm->acpi->hid) {
5756 ret = register_tpacpi_subdriver(ibm);
5757 if (ret)
5758 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005759 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005760
5761 if (ibm->acpi->notify) {
5762 ret = setup_acpi_notify(ibm);
5763 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005764 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005765 ibm->name);
5766 ret = 0;
5767 goto err_out;
5768 }
5769 if (ret < 0)
5770 goto err_out;
5771 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005772 }
5773
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005774 dbg_printk(TPACPI_DBG_INIT,
5775 "%s installed\n", ibm->name);
5776
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005777 if (ibm->read) {
5778 entry = create_proc_entry(ibm->name,
5779 S_IFREG | S_IRUGO | S_IWUSR,
5780 proc_dir);
5781 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005782 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005783 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005784 ret = -ENODEV;
5785 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005786 }
5787 entry->owner = THIS_MODULE;
5788 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005789 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005790 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005791 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005792 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005795 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5796
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005798
5799err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005800 dbg_printk(TPACPI_DBG_INIT,
5801 "%s: at error exit path with result %d\n",
5802 ibm->name, ret);
5803
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005804 ibm_exit(ibm);
5805 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806}
5807
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005808/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005810static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005811{
Jeff Garzik18552562007-10-03 15:15:40 -04005812 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005813 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005814
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005815 if (!tp)
5816 return;
5817
5818 memset(tp, 0, sizeof(*tp));
5819
5820 if (dmi_name_in_vendors("IBM"))
5821 tp->vendor = PCI_VENDOR_ID_IBM;
5822 else if (dmi_name_in_vendors("LENOVO"))
5823 tp->vendor = PCI_VENDOR_ID_LENOVO;
5824 else
5825 return;
5826
5827 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5828 GFP_KERNEL);
5829 if (!tp->bios_version_str)
5830 return;
5831 tp->bios_model = tp->bios_version_str[0]
5832 | (tp->bios_version_str[1] << 8);
5833
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005834 /*
5835 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5836 * X32 or newer, all Z series; Some models must have an
5837 * up-to-date BIOS or they will not be detected.
5838 *
5839 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5840 */
5841 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005842 if (sscanf(dev->name,
5843 "IBM ThinkPad Embedded Controller -[%17c",
5844 ec_fw_string) == 1) {
5845 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5846 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005847
5848 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5849 tp->ec_model = ec_fw_string[0]
5850 | (ec_fw_string[1] << 8);
5851 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005852 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005853 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005854
5855 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5856 GFP_KERNEL);
5857 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5858 kfree(tp->model_str);
5859 tp->model_str = NULL;
5860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861}
5862
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005863static int __init probe_for_thinkpad(void)
5864{
5865 int is_thinkpad;
5866
5867 if (acpi_disabled)
5868 return -ENODEV;
5869
5870 /*
5871 * Non-ancient models have better DMI tagging, but very old models
5872 * don't.
5873 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005874 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005875
5876 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005877 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005878 if (!ec_handle) {
5879 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005880 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005881 "Not yet supported ThinkPad detected!\n");
5882 return -ENODEV;
5883 }
5884
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005885 /*
5886 * Risks a regression on very old machines, but reduces potential
5887 * false positives a damn great deal
5888 */
5889 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005890 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005891
5892 if (!is_thinkpad && !force_load)
5893 return -ENODEV;
5894
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005895 return 0;
5896}
5897
5898
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005899/* Module init, exit, parameters */
5900
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005901static struct ibm_init_struct ibms_init[] __initdata = {
5902 {
5903 .init = thinkpad_acpi_driver_init,
5904 .data = &thinkpad_acpi_driver_data,
5905 },
5906 {
5907 .init = hotkey_init,
5908 .data = &hotkey_driver_data,
5909 },
5910 {
5911 .init = bluetooth_init,
5912 .data = &bluetooth_driver_data,
5913 },
5914 {
5915 .init = wan_init,
5916 .data = &wan_driver_data,
5917 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005918#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005919 {
5920 .init = video_init,
5921 .data = &video_driver_data,
5922 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005923#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005924 {
5925 .init = light_init,
5926 .data = &light_driver_data,
5927 },
5928#ifdef CONFIG_THINKPAD_ACPI_DOCK
5929 {
5930 .init = dock_init,
5931 .data = &dock_driver_data[0],
5932 },
5933 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005934 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005935 .data = &dock_driver_data[1],
5936 },
5937#endif
5938#ifdef CONFIG_THINKPAD_ACPI_BAY
5939 {
5940 .init = bay_init,
5941 .data = &bay_driver_data,
5942 },
5943#endif
5944 {
5945 .init = cmos_init,
5946 .data = &cmos_driver_data,
5947 },
5948 {
5949 .init = led_init,
5950 .data = &led_driver_data,
5951 },
5952 {
5953 .init = beep_init,
5954 .data = &beep_driver_data,
5955 },
5956 {
5957 .init = thermal_init,
5958 .data = &thermal_driver_data,
5959 },
5960 {
5961 .data = &ecdump_driver_data,
5962 },
5963 {
5964 .init = brightness_init,
5965 .data = &brightness_driver_data,
5966 },
5967 {
5968 .data = &volume_driver_data,
5969 },
5970 {
5971 .init = fan_init,
5972 .data = &fan_driver_data,
5973 },
5974};
5975
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005976static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5977{
5978 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005979 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005980
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005981 if (!kp || !kp->name || !val)
5982 return -EINVAL;
5983
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005984 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5985 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005986 WARN_ON(ibm == NULL);
5987
5988 if (!ibm || !ibm->name)
5989 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005990
5991 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5992 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005993 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005994 strcpy(ibms_init[i].param, val);
5995 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005996 return 0;
5997 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005998 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005999
6000 return -EINVAL;
6001}
6002
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006003module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006004MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006005 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006006
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006007module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006008MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006009
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006010module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006011MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006012 "Attempts to load the driver even on a "
6013 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006014
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006015module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006016MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006017 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006018
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006019module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006020MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006021 "Selects brightness control strategy: "
6022 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006023
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006024module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006025MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006026 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006027
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006028module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006029MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006030 "used for backwards compatibility with userspace, "
6031 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006032
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006033#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006034 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006035 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006036 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006037
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006038TPACPI_PARAM(hotkey);
6039TPACPI_PARAM(bluetooth);
6040TPACPI_PARAM(video);
6041TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006042#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006043TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006044#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006045#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006046TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006047#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006048TPACPI_PARAM(cmos);
6049TPACPI_PARAM(led);
6050TPACPI_PARAM(beep);
6051TPACPI_PARAM(ecdump);
6052TPACPI_PARAM(brightness);
6053TPACPI_PARAM(volume);
6054TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006055
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006056static void thinkpad_acpi_module_exit(void)
6057{
6058 struct ibm_struct *ibm, *itmp;
6059
6060 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6061
6062 list_for_each_entry_safe_reverse(ibm, itmp,
6063 &tpacpi_all_drivers,
6064 all_drivers) {
6065 ibm_exit(ibm);
6066 }
6067
6068 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6069
6070 if (tpacpi_inputdev) {
6071 if (tp_features.input_device_registered)
6072 input_unregister_device(tpacpi_inputdev);
6073 else
6074 input_free_device(tpacpi_inputdev);
6075 }
6076
6077 if (tpacpi_hwmon)
6078 hwmon_device_unregister(tpacpi_hwmon);
6079
6080 if (tp_features.sensors_pdev_attrs_registered)
6081 device_remove_file(&tpacpi_sensors_pdev->dev,
6082 &dev_attr_thinkpad_acpi_pdev_name);
6083 if (tpacpi_sensors_pdev)
6084 platform_device_unregister(tpacpi_sensors_pdev);
6085 if (tpacpi_pdev)
6086 platform_device_unregister(tpacpi_pdev);
6087
6088 if (tp_features.sensors_pdrv_attrs_registered)
6089 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6090 if (tp_features.platform_drv_attrs_registered)
6091 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6092
6093 if (tp_features.sensors_pdrv_registered)
6094 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6095
6096 if (tp_features.platform_drv_registered)
6097 platform_driver_unregister(&tpacpi_pdriver);
6098
6099 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006100 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006101
6102 kfree(thinkpad_id.bios_version_str);
6103 kfree(thinkpad_id.ec_version_str);
6104 kfree(thinkpad_id.model_str);
6105}
6106
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006107
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006108static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109{
6110 int ret, i;
6111
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006112 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6113
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006114 /* Parameter checking */
6115 if (hotkey_report_mode > 2)
6116 return -EINVAL;
6117
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006118 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006119
6120 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006121 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006122 if (ret) {
6123 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006124 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006127 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006128
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006129 TPACPI_ACPIHANDLE_INIT(ecrd);
6130 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006132 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006134 printk(TPACPI_ERR
6135 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006136 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 return -ENODEV;
6138 }
6139 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006140
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006141 ret = platform_driver_register(&tpacpi_pdriver);
6142 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006143 printk(TPACPI_ERR
6144 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006145 thinkpad_acpi_module_exit();
6146 return ret;
6147 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006148 tp_features.platform_drv_registered = 1;
6149
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006150 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6151 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006152 printk(TPACPI_ERR
6153 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006154 thinkpad_acpi_module_exit();
6155 return ret;
6156 }
6157 tp_features.sensors_pdrv_registered = 1;
6158
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006159 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006160 if (!ret) {
6161 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006162 ret = tpacpi_create_driver_attributes(
6163 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006164 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006165 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006166 printk(TPACPI_ERR
6167 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006168 thinkpad_acpi_module_exit();
6169 return ret;
6170 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006171 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006172
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006173
6174 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006175 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006176 NULL, 0);
6177 if (IS_ERR(tpacpi_pdev)) {
6178 ret = PTR_ERR(tpacpi_pdev);
6179 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006180 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006181 thinkpad_acpi_module_exit();
6182 return ret;
6183 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006184 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006185 TPACPI_HWMON_DRVR_NAME,
6186 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006187 if (IS_ERR(tpacpi_sensors_pdev)) {
6188 ret = PTR_ERR(tpacpi_sensors_pdev);
6189 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006190 printk(TPACPI_ERR
6191 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006192 thinkpad_acpi_module_exit();
6193 return ret;
6194 }
6195 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6196 &dev_attr_thinkpad_acpi_pdev_name);
6197 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006198 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006199 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006200 thinkpad_acpi_module_exit();
6201 return ret;
6202 }
6203 tp_features.sensors_pdev_attrs_registered = 1;
6204 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006205 if (IS_ERR(tpacpi_hwmon)) {
6206 ret = PTR_ERR(tpacpi_hwmon);
6207 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006208 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006209 thinkpad_acpi_module_exit();
6210 return ret;
6211 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006212 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006213 tpacpi_inputdev = input_allocate_device();
6214 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006215 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006216 thinkpad_acpi_module_exit();
6217 return -ENOMEM;
6218 } else {
6219 /* Prepare input device, but don't register */
6220 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006221 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006222 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006223 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6224 thinkpad_id.vendor :
6225 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006226 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6227 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6228 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006229 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6230 ret = ibm_init(&ibms_init[i]);
6231 if (ret >= 0 && *ibms_init[i].param)
6232 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006234 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 return ret;
6236 }
6237 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006238 ret = input_register_device(tpacpi_inputdev);
6239 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006240 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006241 thinkpad_acpi_module_exit();
6242 return ret;
6243 } else {
6244 tp_features.input_device_registered = 1;
6245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006247 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 return 0;
6249}
6250
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006251/* Please remove this in year 2009 */
6252MODULE_ALIAS("ibm_acpi");
6253
6254/*
6255 * DMI matching for module autoloading
6256 *
6257 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6258 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6259 *
6260 * Only models listed in thinkwiki will be supported, so add yours
6261 * if it is not there yet.
6262 */
6263#define IBM_BIOS_MODULE_ALIAS(__type) \
6264 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6265
6266/* Non-ancient thinkpads */
6267MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6268MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6269
6270/* Ancient thinkpad BIOSes have to be identified by
6271 * BIOS type or model number, and there are far less
6272 * BIOS types than model numbers... */
6273IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6274IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6275IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6276
6277MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006278MODULE_DESCRIPTION(TPACPI_DESC);
6279MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006280MODULE_LICENSE("GPL");
6281
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006282module_init(thinkpad_acpi_module_init);
6283module_exit(thinkpad_acpi_module_exit);