blob: 202d63e1b391899f2228d4e9f2e0abb695862c34 [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 Holschuh68f12ae2008-04-26 01:02:30 -030024#define TPACPI_VERSION "0.20"
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>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030070#include <linux/leds.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020071#include <asm/uaccess.h>
72
73#include <linux/dmi.h>
74#include <linux/jiffies.h>
75#include <linux/workqueue.h>
76
77#include <acpi/acpi_drivers.h>
78#include <acpi/acnamesp.h>
79
80#include <linux/pci_ids.h>
81
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020082
83/* ThinkPad CMOS commands */
84#define TP_CMOS_VOLUME_DOWN 0
85#define TP_CMOS_VOLUME_UP 1
86#define TP_CMOS_VOLUME_MUTE 2
87#define TP_CMOS_BRIGHTNESS_UP 4
88#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030089#define TP_CMOS_THINKLIGHT_ON 12
90#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020091
92/* NVRAM Addresses */
93enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
99};
100
101/* NVRAM bit masks */
102enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
116};
117
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200118/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200119#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200120
121/* Input IDs */
122#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123#define TPACPI_HKEY_INPUT_VERSION 0x4101
124
125
126/****************************************************************************
127 * Main driver
128 */
129
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200130#define TPACPI_NAME "thinkpad"
131#define TPACPI_DESC "ThinkPad ACPI Extras"
132#define TPACPI_FILE TPACPI_NAME "_acpi"
133#define TPACPI_URL "http://ibm-acpi.sf.net/"
134#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200135
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200136#define TPACPI_PROC_DIR "ibm"
137#define TPACPI_ACPI_EVENT_PREFIX "ibm"
138#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300139#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200140#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200141
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300142#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300143#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300144
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200145#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200146
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200147/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200148#define TPACPI_LOG TPACPI_FILE ": "
149#define TPACPI_ERR KERN_ERR TPACPI_LOG
150#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
151#define TPACPI_INFO KERN_INFO TPACPI_LOG
152#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200153
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200154#define TPACPI_DBG_ALL 0xffff
155#define TPACPI_DBG_ALL 0xffff
156#define TPACPI_DBG_INIT 0x0001
157#define TPACPI_DBG_EXIT 0x0002
158#define dbg_printk(a_dbg_level, format, arg...) \
159 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200160 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
161 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200162#ifdef CONFIG_THINKPAD_ACPI_DEBUG
163#define vdbg_printk(a_dbg_level, format, arg...) \
164 dbg_printk(a_dbg_level, format, ## arg)
165static const char *str_supported(int is_supported);
166#else
167#define vdbg_printk(a_dbg_level, format, arg...)
168#endif
169
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200170#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
171#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
172#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200173
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200174
175/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200176 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200177 */
178
179struct ibm_struct;
180
181struct tp_acpi_drv_struct {
182 const struct acpi_device_id *hid;
183 struct acpi_driver *driver;
184
185 void (*notify) (struct ibm_struct *, u32);
186 acpi_handle *handle;
187 u32 type;
188 struct acpi_device *device;
189};
190
191struct ibm_struct {
192 char *name;
193
194 int (*read) (char *);
195 int (*write) (char *);
196 void (*exit) (void);
197 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200198 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200199
200 struct list_head all_drivers;
201
202 struct tp_acpi_drv_struct *acpi;
203
204 struct {
205 u8 acpi_driver_registered:1;
206 u8 acpi_notify_installed:1;
207 u8 proc_created:1;
208 u8 init_called:1;
209 u8 experimental:1;
210 } flags;
211};
212
213struct ibm_init_struct {
214 char param[32];
215
216 int (*init) (struct ibm_init_struct *);
217 struct ibm_struct *data;
218};
219
220static struct {
221#ifdef CONFIG_THINKPAD_ACPI_BAY
222 u32 bay_status:1;
223 u32 bay_eject:1;
224 u32 bay_status2:1;
225 u32 bay_eject2:1;
226#endif
227 u32 bluetooth:1;
228 u32 hotkey:1;
229 u32 hotkey_mask:1;
230 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200231 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200232 u32 light:1;
233 u32 light_status:1;
234 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300235 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200236 u32 wan:1;
237 u32 fan_ctrl_status_undef:1;
238 u32 input_device_registered:1;
239 u32 platform_drv_registered:1;
240 u32 platform_drv_attrs_registered:1;
241 u32 sensors_pdrv_registered:1;
242 u32 sensors_pdrv_attrs_registered:1;
243 u32 sensors_pdev_attrs_registered:1;
244 u32 hotkey_poll_active:1;
245} tp_features;
246
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300247static struct {
248 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300249 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300250} tp_warned;
251
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252struct thinkpad_id_data {
253 unsigned int vendor; /* ThinkPad vendor:
254 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
255
256 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
257 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
258
259 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
260 u16 ec_model;
261
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300262 char *model_str; /* ThinkPad T43 */
263 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200265static struct thinkpad_id_data thinkpad_id;
266
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300267static enum {
268 TPACPI_LIFE_INIT = 0,
269 TPACPI_LIFE_RUNNING,
270 TPACPI_LIFE_EXITING,
271} tpacpi_lifecycle;
272
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200273static int experimental;
274static u32 dbg_level;
275
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300276static struct workqueue_struct *tpacpi_wq;
277
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300278/* Special LED class that can defer work */
279struct tpacpi_led_classdev {
280 struct led_classdev led_classdev;
281 struct work_struct work;
282 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300283 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300284};
285
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300286/****************************************************************************
287 ****************************************************************************
288 *
289 * ACPI Helpers and device model
290 *
291 ****************************************************************************
292 ****************************************************************************/
293
294/*************************************************************************
295 * ACPI basic handles
296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300298static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200300#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 static acpi_handle object##_handle; \
302 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400303 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 static char *object##_paths[] = { paths }
305
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200306TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400307 "\\_SB.PCI.ISA.EC", /* 570 */
308 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
309 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
310 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
311 "\\_SB.PCI0.ICH3.EC0", /* R31 */
312 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300313 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200315TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
316TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200318TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
319 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400320 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
321 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300322 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400323
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200324TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400325 "^HKEY", /* R30, R31 */
326 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300327 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400328
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200329TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
330 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
331 "\\_SB.PCI0.VID0", /* 770e */
332 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
333 "\\_SB.PCI0.AGP.VID", /* all others */
334 ); /* R30, R31 */
335
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400336
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300337/*************************************************************************
338 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200339 */
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static int acpi_evalf(acpi_handle handle,
342 void *res, char *method, char *fmt, ...)
343{
344 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400345 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200346 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400347 struct acpi_buffer result, *resultp;
348 union acpi_object out_obj;
349 acpi_status status;
350 va_list ap;
351 char res_type;
352 int success;
353 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200356 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358 }
359
360 if (*fmt == 'q') {
361 quiet = 1;
362 fmt++;
363 } else
364 quiet = 0;
365
366 res_type = *(fmt++);
367
368 params.count = 0;
369 params.pointer = &in_objs[0];
370
371 va_start(ap, fmt);
372 while (*fmt) {
373 char c = *(fmt++);
374 switch (c) {
375 case 'd': /* int */
376 in_objs[params.count].integer.value = va_arg(ap, int);
377 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
378 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400379 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200381 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 "with invalid format character '%c'\n", c);
383 return 0;
384 }
385 }
386 va_end(ap);
387
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400388 if (res_type != 'v') {
389 result.length = sizeof(out_obj);
390 result.pointer = &out_obj;
391 resultp = &result;
392 } else
393 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400398 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (res)
400 *(int *)res = out_obj.integer.value;
401 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
402 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400403 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 success = status == AE_OK;
405 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400406 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200408 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 "with invalid format character '%c'\n", res_type);
410 return 0;
411 }
412
413 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200414 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 method, fmt0, status);
416
417 return success;
418}
419
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200420static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300421{
422 int v;
423
424 if (ecrd_handle) {
425 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
426 return 0;
427 *p = v;
428 } else {
429 if (ec_read(i, p) < 0)
430 return 0;
431 }
432
433 return 1;
434}
435
436static int acpi_ec_write(int i, u8 v)
437{
438 if (ecwr_handle) {
439 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
440 return 0;
441 } else {
442 if (ec_write(i, v) < 0)
443 return 0;
444 }
445
446 return 1;
447}
448
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200449#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300450static int _sta(acpi_handle handle)
451{
452 int status;
453
454 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
455 status = 0;
456
457 return status;
458}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200459#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300460
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300461static int issue_thinkpad_cmos_command(int cmos_cmd)
462{
463 if (!cmos_handle)
464 return -ENXIO;
465
466 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
467 return -EIO;
468
469 return 0;
470}
471
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300472/*************************************************************************
473 * ACPI device model
474 */
475
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200476#define TPACPI_ACPIHANDLE_INIT(object) \
477 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200478 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
479
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300480static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300481 acpi_handle *handle, acpi_handle parent,
482 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300483{
484 int i;
485 acpi_status status;
486
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300487 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
488 name);
489
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300490 for (i = 0; i < num_paths; i++) {
491 status = acpi_get_handle(parent, paths[i], handle);
492 if (ACPI_SUCCESS(status)) {
493 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300494 dbg_printk(TPACPI_DBG_INIT,
495 "Found ACPI handle %s for %s\n",
496 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300497 return;
498 }
499 }
500
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300501 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
502 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300503 *handle = NULL;
504}
505
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300506static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300507{
508 struct ibm_struct *ibm = data;
509
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300510 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
511 return;
512
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300513 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514 return;
515
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300516 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300517}
518
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300519static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300520{
521 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300522 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300524 BUG_ON(!ibm->acpi);
525
526 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300527 return 0;
528
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300529 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300530 "setting up ACPI notify for %s\n", ibm->name);
531
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300532 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
533 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200534 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300535 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536 return -ENODEV;
537 }
538
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300539 acpi_driver_data(ibm->acpi->device) = ibm;
540 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200541 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300542 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300544 status = acpi_install_notify_handler(*ibm->acpi->handle,
545 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300546 if (ACPI_FAILURE(status)) {
547 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200548 printk(TPACPI_NOTICE
549 "another device driver is already "
550 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300551 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200552 printk(TPACPI_ERR
553 "acpi_install_notify_handler(%s) failed: %d\n",
554 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300555 }
556 return -ENODEV;
557 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300558 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300559 return 0;
560}
561
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300562static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563{
564 return 0;
565}
566
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300567static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300568{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300569 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300570
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300571 dbg_printk(TPACPI_DBG_INIT,
572 "registering %s as an ACPI driver\n", ibm->name);
573
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300574 BUG_ON(!ibm->acpi);
575
576 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
577 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200578 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300579 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300580 }
581
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200582 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300583 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200584
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300585 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300586
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300587 rc = acpi_bus_register_driver(ibm->acpi->driver);
588 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200589 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200590 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300591 kfree(ibm->acpi->driver);
592 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300593 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300594 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300595
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300596 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300597}
598
599
600/****************************************************************************
601 ****************************************************************************
602 *
603 * Procfs Helpers
604 *
605 ****************************************************************************
606 ****************************************************************************/
607
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300608static int dispatch_procfs_read(char *page, char **start, off_t off,
609 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300610{
611 struct ibm_struct *ibm = data;
612 int len;
613
614 if (!ibm || !ibm->read)
615 return -EINVAL;
616
617 len = ibm->read(page);
618 if (len < 0)
619 return len;
620
621 if (len <= off + count)
622 *eof = 1;
623 *start = page + off;
624 len -= off;
625 if (len > count)
626 len = count;
627 if (len < 0)
628 len = 0;
629
630 return len;
631}
632
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300633static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200634 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300635 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300636{
637 struct ibm_struct *ibm = data;
638 char *kernbuf;
639 int ret;
640
641 if (!ibm || !ibm->write)
642 return -EINVAL;
643
644 kernbuf = kmalloc(count + 2, GFP_KERNEL);
645 if (!kernbuf)
646 return -ENOMEM;
647
648 if (copy_from_user(kernbuf, userbuf, count)) {
649 kfree(kernbuf);
650 return -EFAULT;
651 }
652
653 kernbuf[count] = 0;
654 strcat(kernbuf, ",");
655 ret = ibm->write(kernbuf);
656 if (ret == 0)
657 ret = count;
658
659 kfree(kernbuf);
660
661 return ret;
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static char *next_cmd(char **cmds)
665{
666 char *start = *cmds;
667 char *end;
668
669 while ((end = strchr(start, ',')) && end == start)
670 start = end + 1;
671
672 if (!end)
673 return NULL;
674
675 *end = 0;
676 *cmds = end + 1;
677 return start;
678}
679
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300680
681/****************************************************************************
682 ****************************************************************************
683 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300684 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300685 *
686 ****************************************************************************
687 ****************************************************************************/
688
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300689static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300690static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700691static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300692static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300693static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200694static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300695
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200696static int tpacpi_suspend_handler(struct platform_device *pdev,
697 pm_message_t state)
698{
699 struct ibm_struct *ibm, *itmp;
700
701 list_for_each_entry_safe(ibm, itmp,
702 &tpacpi_all_drivers,
703 all_drivers) {
704 if (ibm->suspend)
705 (ibm->suspend)(state);
706 }
707
708 return 0;
709}
710
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300711static int tpacpi_resume_handler(struct platform_device *pdev)
712{
713 struct ibm_struct *ibm, *itmp;
714
715 list_for_each_entry_safe(ibm, itmp,
716 &tpacpi_all_drivers,
717 all_drivers) {
718 if (ibm->resume)
719 (ibm->resume)();
720 }
721
722 return 0;
723}
724
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300725static struct platform_driver tpacpi_pdriver = {
726 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200727 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300728 .owner = THIS_MODULE,
729 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200730 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300731 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300732};
733
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300734static struct platform_driver tpacpi_hwmon_pdriver = {
735 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200736 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300737 .owner = THIS_MODULE,
738 },
739};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300740
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300741/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300742 * sysfs support helpers
743 */
744
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200745struct attribute_set {
746 unsigned int members, max_members;
747 struct attribute_group group;
748};
749
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300750struct attribute_set_obj {
751 struct attribute_set s;
752 struct attribute *a;
753} __attribute__((packed));
754
755static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200756 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300757{
758 struct attribute_set_obj *sobj;
759
760 if (max_members == 0)
761 return NULL;
762
763 /* Allocates space for implicit NULL at the end too */
764 sobj = kzalloc(sizeof(struct attribute_set_obj) +
765 max_members * sizeof(struct attribute *),
766 GFP_KERNEL);
767 if (!sobj)
768 return NULL;
769 sobj->s.max_members = max_members;
770 sobj->s.group.attrs = &sobj->a;
771 sobj->s.group.name = name;
772
773 return &sobj->s;
774}
775
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200776#define destroy_attr_set(_set) \
777 kfree(_set);
778
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300779/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200780static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300781{
782 if (!s || !attr)
783 return -EINVAL;
784
785 if (s->members >= s->max_members)
786 return -ENOMEM;
787
788 s->group.attrs[s->members] = attr;
789 s->members++;
790
791 return 0;
792}
793
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200794static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300795 struct attribute **attr,
796 unsigned int count)
797{
798 int i, res;
799
800 for (i = 0; i < count; i++) {
801 res = add_to_attr_set(s, attr[i]);
802 if (res)
803 return res;
804 }
805
806 return 0;
807}
808
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200809static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300810{
811 sysfs_remove_group(kobj, &s->group);
812 destroy_attr_set(s);
813}
814
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200815#define register_attr_set_with_sysfs(_attr_set, _kobj) \
816 sysfs_create_group(_kobj, &_attr_set->group)
817
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300818static int parse_strtoul(const char *buf,
819 unsigned long max, unsigned long *value)
820{
821 char *endp;
822
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300823 while (*buf && isspace(*buf))
824 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300825 *value = simple_strtoul(buf, &endp, 0);
826 while (*endp && isspace(*endp))
827 endp++;
828 if (*endp || *value > max)
829 return -EINVAL;
830
831 return 0;
832}
833
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300834static int __init tpacpi_query_bcl_levels(acpi_handle handle)
835{
836 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
837 union acpi_object *obj;
838 int rc;
839
840 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
841 obj = (union acpi_object *)buffer.pointer;
842 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
843 printk(TPACPI_ERR "Unknown _BCL data, "
844 "please report this to %s\n", TPACPI_MAIL);
845 rc = 0;
846 } else {
847 rc = obj->package.count;
848 }
849 } else {
850 return 0;
851 }
852
853 kfree(buffer.pointer);
854 return rc;
855}
856
857static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
858 u32 lvl, void *context, void **rv)
859{
860 char name[ACPI_PATH_SEGMENT_LENGTH];
861 struct acpi_buffer buffer = { sizeof(name), &name };
862
863 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
864 !strncmp("_BCL", name, sizeof(name) - 1)) {
865 BUG_ON(!rv || !*rv);
866 **(int **)rv = tpacpi_query_bcl_levels(handle);
867 return AE_CTRL_TERMINATE;
868 } else {
869 return AE_OK;
870 }
871}
872
873/*
874 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
875 */
876static int __init tpacpi_check_std_acpi_brightness_support(void)
877{
878 int status;
879 int bcl_levels = 0;
880 void *bcl_ptr = &bcl_levels;
881
882 if (!vid_handle) {
883 TPACPI_ACPIHANDLE_INIT(vid);
884 }
885 if (!vid_handle)
886 return 0;
887
888 /*
889 * Search for a _BCL method, and execute it. This is safe on all
890 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
891 * BIOS in ACPI backlight control mode. We do NOT have to care
892 * about calling the _BCL method in an enabled video device, any
893 * will do for our purposes.
894 */
895
896 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
897 tpacpi_acpi_walk_find_bcl, NULL,
898 &bcl_ptr);
899
900 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
901 tp_features.bright_acpimode = 1;
902 return (bcl_levels - 2);
903 }
904
905 return 0;
906}
907
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200908/*************************************************************************
909 * thinkpad-acpi driver attributes
910 */
911
912/* interface_version --------------------------------------------------- */
913static ssize_t tpacpi_driver_interface_version_show(
914 struct device_driver *drv,
915 char *buf)
916{
917 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
918}
919
920static DRIVER_ATTR(interface_version, S_IRUGO,
921 tpacpi_driver_interface_version_show, NULL);
922
923/* debug_level --------------------------------------------------------- */
924static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
925 char *buf)
926{
927 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
928}
929
930static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
931 const char *buf, size_t count)
932{
933 unsigned long t;
934
935 if (parse_strtoul(buf, 0xffff, &t))
936 return -EINVAL;
937
938 dbg_level = t;
939
940 return count;
941}
942
943static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
944 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
945
946/* version ------------------------------------------------------------- */
947static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
948 char *buf)
949{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200950 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
951 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200952}
953
954static DRIVER_ATTR(version, S_IRUGO,
955 tpacpi_driver_version_show, NULL);
956
957/* --------------------------------------------------------------------- */
958
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200959static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200960 &driver_attr_debug_level, &driver_attr_version,
961 &driver_attr_interface_version,
962};
963
964static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
965{
966 int i, res;
967
968 i = 0;
969 res = 0;
970 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
971 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
972 i++;
973 }
974
975 return res;
976}
977
978static void tpacpi_remove_driver_attributes(struct device_driver *drv)
979{
980 int i;
981
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200982 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200983 driver_remove_file(drv, tpacpi_driver_attributes[i]);
984}
985
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300986/****************************************************************************
987 ****************************************************************************
988 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300989 * Subdrivers
990 *
991 ****************************************************************************
992 ****************************************************************************/
993
994/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300995 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300996 */
997
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300998static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001000 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1001 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001003 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001004 (thinkpad_id.bios_version_str) ?
1005 thinkpad_id.bios_version_str : "unknown",
1006 (thinkpad_id.ec_version_str) ?
1007 thinkpad_id.ec_version_str : "unknown");
1008
1009 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001010 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001011 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1012 "IBM" : ((thinkpad_id.vendor ==
1013 PCI_VENDOR_ID_LENOVO) ?
1014 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001015 thinkpad_id.model_str,
1016 (thinkpad_id.nummodel_str) ?
1017 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020}
1021
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001022static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 int len = 0;
1025
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001026 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1027 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 return len;
1030}
1031
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001032static struct ibm_struct thinkpad_acpi_driver_data = {
1033 .name = "driver",
1034 .read = thinkpad_acpi_driver_read,
1035};
1036
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001037/*************************************************************************
1038 * Hotkey subdriver
1039 */
1040
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001041enum { /* hot key scan codes (derived from ACPI DSDT) */
1042 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1043 TP_ACPI_HOTKEYSCAN_FNF2,
1044 TP_ACPI_HOTKEYSCAN_FNF3,
1045 TP_ACPI_HOTKEYSCAN_FNF4,
1046 TP_ACPI_HOTKEYSCAN_FNF5,
1047 TP_ACPI_HOTKEYSCAN_FNF6,
1048 TP_ACPI_HOTKEYSCAN_FNF7,
1049 TP_ACPI_HOTKEYSCAN_FNF8,
1050 TP_ACPI_HOTKEYSCAN_FNF9,
1051 TP_ACPI_HOTKEYSCAN_FNF10,
1052 TP_ACPI_HOTKEYSCAN_FNF11,
1053 TP_ACPI_HOTKEYSCAN_FNF12,
1054 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1055 TP_ACPI_HOTKEYSCAN_FNINSERT,
1056 TP_ACPI_HOTKEYSCAN_FNDELETE,
1057 TP_ACPI_HOTKEYSCAN_FNHOME,
1058 TP_ACPI_HOTKEYSCAN_FNEND,
1059 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1060 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1061 TP_ACPI_HOTKEYSCAN_FNSPACE,
1062 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1063 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1064 TP_ACPI_HOTKEYSCAN_MUTE,
1065 TP_ACPI_HOTKEYSCAN_THINKPAD,
1066};
1067
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001068enum { /* Keys available through NVRAM polling */
1069 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1070 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1071};
1072
1073enum { /* Positions of some of the keys in hotkey masks */
1074 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1075 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1076 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1077 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1078 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1079 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1080 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1081 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1082 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1083 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1084 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1085};
1086
1087enum { /* NVRAM to ACPI HKEY group map */
1088 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1089 TP_ACPI_HKEY_ZOOM_MASK |
1090 TP_ACPI_HKEY_DISPSWTCH_MASK |
1091 TP_ACPI_HKEY_HIBERNATE_MASK,
1092 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1093 TP_ACPI_HKEY_BRGHTDWN_MASK,
1094 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1095 TP_ACPI_HKEY_VOLDWN_MASK |
1096 TP_ACPI_HKEY_MUTE_MASK,
1097};
1098
1099#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1100struct tp_nvram_state {
1101 u16 thinkpad_toggle:1;
1102 u16 zoom_toggle:1;
1103 u16 display_toggle:1;
1104 u16 thinklight_toggle:1;
1105 u16 hibernate_toggle:1;
1106 u16 displayexp_toggle:1;
1107 u16 display_state:1;
1108 u16 brightness_toggle:1;
1109 u16 volume_toggle:1;
1110 u16 mute:1;
1111
1112 u8 brightness_level;
1113 u8 volume_level;
1114};
1115
1116static struct task_struct *tpacpi_hotkey_task;
1117static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1118static int hotkey_poll_freq = 10; /* Hz */
1119static struct mutex hotkey_thread_mutex;
1120static struct mutex hotkey_thread_data_mutex;
1121static unsigned int hotkey_config_change;
1122
1123#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1124
1125#define hotkey_source_mask 0U
1126
1127#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1128
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001129static struct mutex hotkey_mutex;
1130
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001131static enum { /* Reasons for waking up */
1132 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1133 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1134 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1135} hotkey_wakeup_reason;
1136
1137static int hotkey_autosleep_ack;
1138
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001139static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001140static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001141static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001142static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001143static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001144
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001145static unsigned int hotkey_report_mode;
1146
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001147static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001148
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001149static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001150
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001151#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1152#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001153 do { \
1154 mutex_lock(&hotkey_thread_data_mutex); \
1155 hotkey_config_change++; \
1156 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001157#define HOTKEY_CONFIG_CRITICAL_END \
1158 mutex_unlock(&hotkey_thread_data_mutex);
1159#else
1160#define HOTKEY_CONFIG_CRITICAL_START
1161#define HOTKEY_CONFIG_CRITICAL_END
1162#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1163
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001164/* HKEY.MHKG() return bits */
1165#define TP_HOTKEY_TABLET_MASK (1 << 3)
1166
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001167static int hotkey_get_wlsw(int *status)
1168{
1169 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1170 return -EIO;
1171 return 0;
1172}
1173
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001174static int hotkey_get_tablet_mode(int *status)
1175{
1176 int s;
1177
1178 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1179 return -EIO;
1180
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001181 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1182 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001183}
1184
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001185/*
1186 * Call with hotkey_mutex held
1187 */
1188static int hotkey_mask_get(void)
1189{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001190 u32 m = 0;
1191
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001192 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001193 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001194 return -EIO;
1195 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001196 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001197
1198 return 0;
1199}
1200
1201/*
1202 * Call with hotkey_mutex held
1203 */
1204static int hotkey_mask_set(u32 mask)
1205{
1206 int i;
1207 int rc = 0;
1208
1209 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001210 if (!tp_warned.hotkey_mask_ff &&
1211 (mask == 0xffff || mask == 0xffffff ||
1212 mask == 0xffffffff)) {
1213 tp_warned.hotkey_mask_ff = 1;
1214 printk(TPACPI_NOTICE
1215 "setting the hotkey mask to 0x%08x is likely "
1216 "not the best way to go about it\n", mask);
1217 printk(TPACPI_NOTICE
1218 "please consider using the driver defaults, "
1219 "and refer to up-to-date thinkpad-acpi "
1220 "documentation\n");
1221 }
1222
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001223 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001224 for (i = 0; i < 32; i++) {
1225 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001226 /* enable in firmware mask only keys not in NVRAM
1227 * mode, but enable the key in the cached hotkey_mask
1228 * regardless of mode, or the key will end up
1229 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001230 if (!acpi_evalf(hkey_handle,
1231 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001232 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001233 rc = -EIO;
1234 break;
1235 } else {
1236 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1237 }
1238 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001239 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001240
1241 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001242 if (!hotkey_mask_get() && !rc &&
1243 (hotkey_mask & ~hotkey_source_mask) !=
1244 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001245 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001246 "requested hot key mask 0x%08x, but "
1247 "firmware forced it to 0x%08x\n",
1248 mask, hotkey_mask);
1249 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001250 } else {
1251#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1252 HOTKEY_CONFIG_CRITICAL_START
1253 hotkey_mask = mask & hotkey_source_mask;
1254 HOTKEY_CONFIG_CRITICAL_END
1255 hotkey_mask_get();
1256 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001257 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001258 "requested hot key mask 0x%08x, "
1259 "forced to 0x%08x (NVRAM poll mask is "
1260 "0x%08x): no firmware mask support\n",
1261 mask, hotkey_mask, hotkey_source_mask);
1262 }
1263#else
1264 hotkey_mask_get();
1265 rc = -ENXIO;
1266#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001267 }
1268
1269 return rc;
1270}
1271
1272static int hotkey_status_get(int *status)
1273{
1274 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1275 return -EIO;
1276
1277 return 0;
1278}
1279
1280static int hotkey_status_set(int status)
1281{
1282 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1283 return -EIO;
1284
1285 return 0;
1286}
1287
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001288static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001289{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001290 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001291
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001292 if (tp_features.hotkey_tablet &&
1293 !hotkey_get_tablet_mode(&state)) {
1294 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001295
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001296 input_report_switch(tpacpi_inputdev,
1297 SW_TABLET_MODE, !!state);
1298 input_sync(tpacpi_inputdev);
1299
1300 mutex_unlock(&tpacpi_inputdev_send_mutex);
1301 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001302}
1303
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001304static void tpacpi_input_send_key(unsigned int scancode)
1305{
1306 unsigned int keycode;
1307
1308 keycode = hotkey_keycode_map[scancode];
1309
1310 if (keycode != KEY_RESERVED) {
1311 mutex_lock(&tpacpi_inputdev_send_mutex);
1312
1313 input_report_key(tpacpi_inputdev, keycode, 1);
1314 if (keycode == KEY_UNKNOWN)
1315 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1316 scancode);
1317 input_sync(tpacpi_inputdev);
1318
1319 input_report_key(tpacpi_inputdev, keycode, 0);
1320 if (keycode == KEY_UNKNOWN)
1321 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1322 scancode);
1323 input_sync(tpacpi_inputdev);
1324
1325 mutex_unlock(&tpacpi_inputdev_send_mutex);
1326 }
1327}
1328
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001329#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1330static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1331
1332static void tpacpi_hotkey_send_key(unsigned int scancode)
1333{
1334 tpacpi_input_send_key(scancode);
1335 if (hotkey_report_mode < 2) {
1336 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1337 0x80, 0x1001 + scancode);
1338 }
1339}
1340
1341static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1342{
1343 u8 d;
1344
1345 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1346 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1347 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1348 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1349 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1350 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1351 }
1352 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1353 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1354 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1355 }
1356 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1357 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1358 n->displayexp_toggle =
1359 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1360 }
1361 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1362 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1363 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1364 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1365 n->brightness_toggle =
1366 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1367 }
1368 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1369 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1370 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1371 >> TP_NVRAM_POS_LEVEL_VOLUME;
1372 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1373 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1374 }
1375}
1376
1377#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001378 do { \
1379 if ((mask & (1 << __scancode)) && \
1380 oldn->__member != newn->__member) \
1381 tpacpi_hotkey_send_key(__scancode); \
1382 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001383
1384#define TPACPI_MAY_SEND_KEY(__scancode) \
1385 do { if (mask & (1 << __scancode)) \
1386 tpacpi_hotkey_send_key(__scancode); } while (0)
1387
1388static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001389 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001390 u32 mask)
1391{
1392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1394 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1396
1397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1398
1399 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1400
1401 /* handle volume */
1402 if (oldn->volume_toggle != newn->volume_toggle) {
1403 if (oldn->mute != newn->mute) {
1404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1405 }
1406 if (oldn->volume_level > newn->volume_level) {
1407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1408 } else if (oldn->volume_level < newn->volume_level) {
1409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1410 } else if (oldn->mute == newn->mute) {
1411 /* repeated key presses that didn't change state */
1412 if (newn->mute) {
1413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1414 } else if (newn->volume_level != 0) {
1415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1416 } else {
1417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1418 }
1419 }
1420 }
1421
1422 /* handle brightness */
1423 if (oldn->brightness_toggle != newn->brightness_toggle) {
1424 if (oldn->brightness_level < newn->brightness_level) {
1425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1426 } else if (oldn->brightness_level > newn->brightness_level) {
1427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1428 } else {
1429 /* repeated key presses that didn't change state */
1430 if (newn->brightness_level != 0) {
1431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1432 } else {
1433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1434 }
1435 }
1436 }
1437}
1438
1439#undef TPACPI_COMPARE_KEY
1440#undef TPACPI_MAY_SEND_KEY
1441
1442static int hotkey_kthread(void *data)
1443{
1444 struct tp_nvram_state s[2];
1445 u32 mask;
1446 unsigned int si, so;
1447 unsigned long t;
1448 unsigned int change_detector, must_reset;
1449
1450 mutex_lock(&hotkey_thread_mutex);
1451
1452 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1453 goto exit;
1454
1455 set_freezable();
1456
1457 so = 0;
1458 si = 1;
1459 t = 0;
1460
1461 /* Initial state for compares */
1462 mutex_lock(&hotkey_thread_data_mutex);
1463 change_detector = hotkey_config_change;
1464 mask = hotkey_source_mask & hotkey_mask;
1465 mutex_unlock(&hotkey_thread_data_mutex);
1466 hotkey_read_nvram(&s[so], mask);
1467
1468 while (!kthread_should_stop() && hotkey_poll_freq) {
1469 if (t == 0)
1470 t = 1000/hotkey_poll_freq;
1471 t = msleep_interruptible(t);
1472 if (unlikely(kthread_should_stop()))
1473 break;
1474 must_reset = try_to_freeze();
1475 if (t > 0 && !must_reset)
1476 continue;
1477
1478 mutex_lock(&hotkey_thread_data_mutex);
1479 if (must_reset || hotkey_config_change != change_detector) {
1480 /* forget old state on thaw or config change */
1481 si = so;
1482 t = 0;
1483 change_detector = hotkey_config_change;
1484 }
1485 mask = hotkey_source_mask & hotkey_mask;
1486 mutex_unlock(&hotkey_thread_data_mutex);
1487
1488 if (likely(mask)) {
1489 hotkey_read_nvram(&s[si], mask);
1490 if (likely(si != so)) {
1491 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001492 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001493 }
1494 }
1495
1496 so = si;
1497 si ^= 1;
1498 }
1499
1500exit:
1501 mutex_unlock(&hotkey_thread_mutex);
1502 return 0;
1503}
1504
1505static void hotkey_poll_stop_sync(void)
1506{
1507 if (tpacpi_hotkey_task) {
1508 if (frozen(tpacpi_hotkey_task) ||
1509 freezing(tpacpi_hotkey_task))
1510 thaw_process(tpacpi_hotkey_task);
1511
1512 kthread_stop(tpacpi_hotkey_task);
1513 tpacpi_hotkey_task = NULL;
1514 mutex_lock(&hotkey_thread_mutex);
1515 /* at this point, the thread did exit */
1516 mutex_unlock(&hotkey_thread_mutex);
1517 }
1518}
1519
1520/* call with hotkey_mutex held */
1521static void hotkey_poll_setup(int may_warn)
1522{
1523 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1524 hotkey_poll_freq > 0 &&
1525 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1526 if (!tpacpi_hotkey_task) {
1527 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001528 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001529 if (IS_ERR(tpacpi_hotkey_task)) {
1530 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001531 printk(TPACPI_ERR
1532 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001533 "for hotkey polling\n");
1534 }
1535 }
1536 } else {
1537 hotkey_poll_stop_sync();
1538 if (may_warn &&
1539 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001540 printk(TPACPI_NOTICE
1541 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001542 "which is currently disabled\n",
1543 hotkey_source_mask);
1544 }
1545 }
1546}
1547
1548static void hotkey_poll_setup_safe(int may_warn)
1549{
1550 mutex_lock(&hotkey_mutex);
1551 hotkey_poll_setup(may_warn);
1552 mutex_unlock(&hotkey_mutex);
1553}
1554
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001555#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1556
1557static void hotkey_poll_setup_safe(int __unused)
1558{
1559}
1560
1561#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1562
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001563static int hotkey_inputdev_open(struct input_dev *dev)
1564{
1565 switch (tpacpi_lifecycle) {
1566 case TPACPI_LIFE_INIT:
1567 /*
1568 * hotkey_init will call hotkey_poll_setup_safe
1569 * at the appropriate moment
1570 */
1571 return 0;
1572 case TPACPI_LIFE_EXITING:
1573 return -EBUSY;
1574 case TPACPI_LIFE_RUNNING:
1575 hotkey_poll_setup_safe(0);
1576 return 0;
1577 }
1578
1579 /* Should only happen if tpacpi_lifecycle is corrupt */
1580 BUG();
1581 return -EBUSY;
1582}
1583
1584static void hotkey_inputdev_close(struct input_dev *dev)
1585{
1586 /* disable hotkey polling when possible */
1587 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1588 hotkey_poll_setup_safe(0);
1589}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001590
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001591/* sysfs hotkey enable ------------------------------------------------- */
1592static ssize_t hotkey_enable_show(struct device *dev,
1593 struct device_attribute *attr,
1594 char *buf)
1595{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001596 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001597
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001598 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001599 if (res)
1600 return res;
1601
1602 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1603}
1604
1605static ssize_t hotkey_enable_store(struct device *dev,
1606 struct device_attribute *attr,
1607 const char *buf, size_t count)
1608{
1609 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001610 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001611
1612 if (parse_strtoul(buf, 1, &t))
1613 return -EINVAL;
1614
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001615 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001616
1617 return (res) ? res : count;
1618}
1619
1620static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001621 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001622 hotkey_enable_show, hotkey_enable_store);
1623
1624/* sysfs hotkey mask --------------------------------------------------- */
1625static ssize_t hotkey_mask_show(struct device *dev,
1626 struct device_attribute *attr,
1627 char *buf)
1628{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001629 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001630
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001631 if (mutex_lock_interruptible(&hotkey_mutex))
1632 return -ERESTARTSYS;
1633 res = hotkey_mask_get();
1634 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001635
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001636 return (res)?
1637 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001638}
1639
1640static ssize_t hotkey_mask_store(struct device *dev,
1641 struct device_attribute *attr,
1642 const char *buf, size_t count)
1643{
1644 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001645 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001646
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001647 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001648 return -EINVAL;
1649
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001650 if (mutex_lock_interruptible(&hotkey_mutex))
1651 return -ERESTARTSYS;
1652
1653 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001654
1655#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1656 hotkey_poll_setup(1);
1657#endif
1658
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001659 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001660
1661 return (res) ? res : count;
1662}
1663
1664static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001665 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001666 hotkey_mask_show, hotkey_mask_store);
1667
1668/* sysfs hotkey bios_enabled ------------------------------------------- */
1669static ssize_t hotkey_bios_enabled_show(struct device *dev,
1670 struct device_attribute *attr,
1671 char *buf)
1672{
1673 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1674}
1675
1676static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001677 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001678
1679/* sysfs hotkey bios_mask ---------------------------------------------- */
1680static ssize_t hotkey_bios_mask_show(struct device *dev,
1681 struct device_attribute *attr,
1682 char *buf)
1683{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001684 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001685}
1686
1687static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001688 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001689
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001690/* sysfs hotkey all_mask ----------------------------------------------- */
1691static ssize_t hotkey_all_mask_show(struct device *dev,
1692 struct device_attribute *attr,
1693 char *buf)
1694{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001695 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1696 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001697}
1698
1699static struct device_attribute dev_attr_hotkey_all_mask =
1700 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1701
1702/* sysfs hotkey recommended_mask --------------------------------------- */
1703static ssize_t hotkey_recommended_mask_show(struct device *dev,
1704 struct device_attribute *attr,
1705 char *buf)
1706{
1707 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001708 (hotkey_all_mask | hotkey_source_mask)
1709 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001710}
1711
1712static struct device_attribute dev_attr_hotkey_recommended_mask =
1713 __ATTR(hotkey_recommended_mask, S_IRUGO,
1714 hotkey_recommended_mask_show, NULL);
1715
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001716#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1717
1718/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1719static ssize_t hotkey_source_mask_show(struct device *dev,
1720 struct device_attribute *attr,
1721 char *buf)
1722{
1723 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1724}
1725
1726static ssize_t hotkey_source_mask_store(struct device *dev,
1727 struct device_attribute *attr,
1728 const char *buf, size_t count)
1729{
1730 unsigned long t;
1731
1732 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1733 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1734 return -EINVAL;
1735
1736 if (mutex_lock_interruptible(&hotkey_mutex))
1737 return -ERESTARTSYS;
1738
1739 HOTKEY_CONFIG_CRITICAL_START
1740 hotkey_source_mask = t;
1741 HOTKEY_CONFIG_CRITICAL_END
1742
1743 hotkey_poll_setup(1);
1744
1745 mutex_unlock(&hotkey_mutex);
1746
1747 return count;
1748}
1749
1750static struct device_attribute dev_attr_hotkey_source_mask =
1751 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1752 hotkey_source_mask_show, hotkey_source_mask_store);
1753
1754/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1755static ssize_t hotkey_poll_freq_show(struct device *dev,
1756 struct device_attribute *attr,
1757 char *buf)
1758{
1759 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1760}
1761
1762static ssize_t hotkey_poll_freq_store(struct device *dev,
1763 struct device_attribute *attr,
1764 const char *buf, size_t count)
1765{
1766 unsigned long t;
1767
1768 if (parse_strtoul(buf, 25, &t))
1769 return -EINVAL;
1770
1771 if (mutex_lock_interruptible(&hotkey_mutex))
1772 return -ERESTARTSYS;
1773
1774 hotkey_poll_freq = t;
1775
1776 hotkey_poll_setup(1);
1777 mutex_unlock(&hotkey_mutex);
1778
1779 return count;
1780}
1781
1782static struct device_attribute dev_attr_hotkey_poll_freq =
1783 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1784 hotkey_poll_freq_show, hotkey_poll_freq_store);
1785
1786#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1787
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001788/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001789static ssize_t hotkey_radio_sw_show(struct device *dev,
1790 struct device_attribute *attr,
1791 char *buf)
1792{
1793 int res, s;
1794 res = hotkey_get_wlsw(&s);
1795 if (res < 0)
1796 return res;
1797
1798 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1799}
1800
1801static struct device_attribute dev_attr_hotkey_radio_sw =
1802 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1803
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001804static void hotkey_radio_sw_notify_change(void)
1805{
1806 if (tp_features.hotkey_wlsw)
1807 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1808 "hotkey_radio_sw");
1809}
1810
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001811/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1812static ssize_t hotkey_tablet_mode_show(struct device *dev,
1813 struct device_attribute *attr,
1814 char *buf)
1815{
1816 int res, s;
1817 res = hotkey_get_tablet_mode(&s);
1818 if (res < 0)
1819 return res;
1820
1821 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1822}
1823
1824static struct device_attribute dev_attr_hotkey_tablet_mode =
1825 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1826
1827static void hotkey_tablet_mode_notify_change(void)
1828{
1829 if (tp_features.hotkey_tablet)
1830 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1831 "hotkey_tablet_mode");
1832}
1833
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001834/* sysfs hotkey report_mode -------------------------------------------- */
1835static ssize_t hotkey_report_mode_show(struct device *dev,
1836 struct device_attribute *attr,
1837 char *buf)
1838{
1839 return snprintf(buf, PAGE_SIZE, "%d\n",
1840 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1841}
1842
1843static struct device_attribute dev_attr_hotkey_report_mode =
1844 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1845
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001846/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001847static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1848 struct device_attribute *attr,
1849 char *buf)
1850{
1851 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1852}
1853
1854static struct device_attribute dev_attr_hotkey_wakeup_reason =
1855 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1856
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001857static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001858{
1859 if (tp_features.hotkey_mask)
1860 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1861 "wakeup_reason");
1862}
1863
1864/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001865static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1866 struct device_attribute *attr,
1867 char *buf)
1868{
1869 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1870}
1871
1872static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1873 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1874 hotkey_wakeup_hotunplug_complete_show, NULL);
1875
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001876static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001877{
1878 if (tp_features.hotkey_mask)
1879 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1880 "wakeup_hotunplug_complete");
1881}
1882
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001883/* --------------------------------------------------------------------- */
1884
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001885static struct attribute *hotkey_attributes[] __initdata = {
1886 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001887 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001888 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001889#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1890 &dev_attr_hotkey_mask.attr,
1891 &dev_attr_hotkey_all_mask.attr,
1892 &dev_attr_hotkey_recommended_mask.attr,
1893 &dev_attr_hotkey_source_mask.attr,
1894 &dev_attr_hotkey_poll_freq.attr,
1895#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001896};
1897
1898static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001899 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001900#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1901 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001902 &dev_attr_hotkey_all_mask.attr,
1903 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001904#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001905 &dev_attr_hotkey_wakeup_reason.attr,
1906 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001907};
1908
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03001909static void tpacpi_send_radiosw_update(void)
1910{
1911 int wlsw;
1912
1913 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1914 mutex_lock(&tpacpi_inputdev_send_mutex);
1915
1916 input_report_switch(tpacpi_inputdev,
1917 SW_RFKILL_ALL, !!wlsw);
1918 input_sync(tpacpi_inputdev);
1919
1920 mutex_unlock(&tpacpi_inputdev_send_mutex);
1921 }
1922 hotkey_radio_sw_notify_change();
1923}
1924
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03001925static void hotkey_exit(void)
1926{
1927#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1928 hotkey_poll_stop_sync();
1929#endif
1930
1931 if (hotkey_dev_attributes)
1932 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1933
1934 kfree(hotkey_keycode_map);
1935
1936 if (tp_features.hotkey) {
1937 dbg_printk(TPACPI_DBG_EXIT,
1938 "restoring original hot key mask\n");
1939 /* no short-circuit boolean operator below! */
1940 if ((hotkey_mask_set(hotkey_orig_mask) |
1941 hotkey_status_set(hotkey_orig_status)) != 0)
1942 printk(TPACPI_ERR
1943 "failed to restore hot key mask "
1944 "to BIOS defaults\n");
1945 }
1946}
1947
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001948static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001949{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001950 /* Requirements for changing the default keymaps:
1951 *
1952 * 1. Many of the keys are mapped to KEY_RESERVED for very
1953 * good reasons. Do not change them unless you have deep
1954 * knowledge on the IBM and Lenovo ThinkPad firmware for
1955 * the various ThinkPad models. The driver behaves
1956 * differently for KEY_RESERVED: such keys have their
1957 * hot key mask *unset* in mask_recommended, and also
1958 * in the initial hot key mask programmed into the
1959 * firmware at driver load time, which means the firm-
1960 * ware may react very differently if you change them to
1961 * something else;
1962 *
1963 * 2. You must be subscribed to the linux-thinkpad and
1964 * ibm-acpi-devel mailing lists, and you should read the
1965 * list archives since 2007 if you want to change the
1966 * keymaps. This requirement exists so that you will
1967 * know the past history of problems with the thinkpad-
1968 * acpi driver keymaps, and also that you will be
1969 * listening to any bug reports;
1970 *
1971 * 3. Do not send thinkpad-acpi specific patches directly to
1972 * for merging, *ever*. Send them to the linux-acpi
1973 * mailinglist for comments. Merging is to be done only
1974 * through acpi-test and the ACPI maintainer.
1975 *
1976 * If the above is too much to ask, don't change the keymap.
1977 * Ask the thinkpad-acpi maintainer to do it, instead.
1978 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001979 static u16 ibm_keycode_map[] __initdata = {
1980 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1981 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1982 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1983 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001984
1985 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001986 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1987 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1988 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001989
1990 /* brightness: firmware always reacts to them, unless
1991 * X.org did some tricks in the radeon BIOS scratch
1992 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001993 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001994 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001995
1996 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001997 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001998
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001999 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2000 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002001
2002 /* Volume: firmware always react to it and reprograms
2003 * the built-in *extra* mixer. Never map it to control
2004 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002005 KEY_RESERVED, /* 0x14: VOLUME UP */
2006 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2007 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002008
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002009 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002010
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002011 /* (assignments unknown, please report if found) */
2012 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2013 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2014 };
2015 static u16 lenovo_keycode_map[] __initdata = {
2016 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2017 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2018 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2019 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002020
2021 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002022 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2023 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2024 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002025
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002026 /* These either have to go through ACPI video, or
2027 * act like in the IBM ThinkPads, so don't ever
2028 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002029 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002030 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002031
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002032 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002033
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002034 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2035 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002036
2037 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2038 * react to it and reprograms the built-in *extra* mixer.
2039 * Never map it to control another mixer by default.
2040 *
2041 * T60?, T61, R60?, R61: firmware and EC tries to send
2042 * these over the regular keyboard, so these are no-ops,
2043 * but there are still weird bugs re. MUTE, so do not
2044 * change unless you get test reports from all Lenovo
2045 * models. May cause the BIOS to interfere with the
2046 * HDA mixer.
2047 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002048 KEY_RESERVED, /* 0x14: VOLUME UP */
2049 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2050 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002051
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002052 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002053
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002054 /* (assignments unknown, please report if found) */
2055 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2056 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2057 };
2058
2059#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2060#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2061#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2062
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002063 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002064 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002065 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002066
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002067 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2068
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002069 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002070 BUG_ON(tpacpi_inputdev->open != NULL ||
2071 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002072
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002073 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002074 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002075
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002076#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2077 mutex_init(&hotkey_thread_mutex);
2078 mutex_init(&hotkey_thread_data_mutex);
2079#endif
2080
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002081 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002082 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002083
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002084 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002085 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002086
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002087 if (!tp_features.hotkey)
2088 return 1;
2089
2090 hotkey_dev_attributes = create_attr_set(13, NULL);
2091 if (!hotkey_dev_attributes)
2092 return -ENOMEM;
2093 res = add_many_to_attr_set(hotkey_dev_attributes,
2094 hotkey_attributes,
2095 ARRAY_SIZE(hotkey_attributes));
2096 if (res)
2097 goto err_exit;
2098
2099 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2100 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2101 for HKEY interface version 0x100 */
2102 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2103 if ((hkeyv >> 8) != 1) {
2104 printk(TPACPI_ERR "unknown version of the "
2105 "HKEY interface: 0x%x\n", hkeyv);
2106 printk(TPACPI_ERR "please report this to %s\n",
2107 TPACPI_MAIL);
2108 } else {
2109 /*
2110 * MHKV 0x100 in A31, R40, R40e,
2111 * T4x, X31, and later
2112 */
2113 tp_features.hotkey_mask = 1;
2114 }
2115 }
2116
2117 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2118 str_supported(tp_features.hotkey_mask));
2119
2120 if (tp_features.hotkey_mask) {
2121 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2122 "MHKA", "qd")) {
2123 printk(TPACPI_ERR
2124 "missing MHKA handler, "
2125 "please report this to %s\n",
2126 TPACPI_MAIL);
2127 /* FN+F12, FN+F4, FN+F3 */
2128 hotkey_all_mask = 0x080cU;
2129 }
2130 }
2131
2132 /* hotkey_source_mask *must* be zero for
2133 * the first hotkey_mask_get */
2134 res = hotkey_status_get(&hotkey_orig_status);
2135 if (res)
2136 goto err_exit;
2137
2138 if (tp_features.hotkey_mask) {
2139 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002140 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002141 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002142
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002143 hotkey_orig_mask = hotkey_mask;
2144 res = add_many_to_attr_set(
2145 hotkey_dev_attributes,
2146 hotkey_mask_attributes,
2147 ARRAY_SIZE(hotkey_mask_attributes));
2148 if (res)
2149 goto err_exit;
2150 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002151
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002152#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002153 if (tp_features.hotkey_mask) {
2154 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2155 & ~hotkey_all_mask;
2156 } else {
2157 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2158 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002159
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002160 vdbg_printk(TPACPI_DBG_INIT,
2161 "hotkey source mask 0x%08x, polling freq %d\n",
2162 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002163#endif
2164
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002165 /* Not all thinkpads have a hardware radio switch */
2166 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2167 tp_features.hotkey_wlsw = 1;
2168 printk(TPACPI_INFO
2169 "radio switch found; radios are %s\n",
2170 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002171 }
2172 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002173 res = add_to_attr_set(hotkey_dev_attributes,
2174 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002175
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002176 /* For X41t, X60t, X61t Tablets... */
2177 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2178 tp_features.hotkey_tablet = 1;
2179 printk(TPACPI_INFO
2180 "possible tablet mode switch found; "
2181 "ThinkPad in %s mode\n",
2182 (status & TP_HOTKEY_TABLET_MASK)?
2183 "tablet" : "laptop");
2184 res = add_to_attr_set(hotkey_dev_attributes,
2185 &dev_attr_hotkey_tablet_mode.attr);
2186 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002187
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002188 if (!res)
2189 res = register_attr_set_with_sysfs(
2190 hotkey_dev_attributes,
2191 &tpacpi_pdev->dev.kobj);
2192 if (res)
2193 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002194
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002195 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002196
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002197 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2198 GFP_KERNEL);
2199 if (!hotkey_keycode_map) {
2200 printk(TPACPI_ERR
2201 "failed to allocate memory for key map\n");
2202 res = -ENOMEM;
2203 goto err_exit;
2204 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002205
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002206 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2207 dbg_printk(TPACPI_DBG_INIT,
2208 "using Lenovo default hot key map\n");
2209 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2210 TPACPI_HOTKEY_MAP_SIZE);
2211 } else {
2212 dbg_printk(TPACPI_DBG_INIT,
2213 "using IBM default hot key map\n");
2214 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2215 TPACPI_HOTKEY_MAP_SIZE);
2216 }
2217
2218 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2219 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2220 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2221 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2222 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2223 tpacpi_inputdev->keycode = hotkey_keycode_map;
2224 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2225 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2226 set_bit(hotkey_keycode_map[i],
2227 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002228 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002229 if (i < sizeof(hotkey_reserved_mask)*8)
2230 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002231 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002232 }
2233
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002234 if (tp_features.hotkey_wlsw) {
2235 set_bit(EV_SW, tpacpi_inputdev->evbit);
2236 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2237 }
2238 if (tp_features.hotkey_tablet) {
2239 set_bit(EV_SW, tpacpi_inputdev->evbit);
2240 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002241 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002242
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002243 /* Do not issue duplicate brightness change events to
2244 * userspace */
2245 if (!tp_features.bright_acpimode)
2246 /* update bright_acpimode... */
2247 tpacpi_check_std_acpi_brightness_support();
2248
2249 if (tp_features.bright_acpimode) {
2250 printk(TPACPI_INFO
2251 "This ThinkPad has standard ACPI backlight "
2252 "brightness control, supported by the ACPI "
2253 "video driver\n");
2254 printk(TPACPI_NOTICE
2255 "Disabling thinkpad-acpi brightness events "
2256 "by default...\n");
2257
2258 /* The hotkey_reserved_mask change below is not
2259 * necessary while the keys are at KEY_RESERVED in the
2260 * default map, but better safe than sorry, leave it
2261 * here as a marker of what we have to do, especially
2262 * when we finally become able to set this at runtime
2263 * on response to X.org requests */
2264 hotkey_reserved_mask |=
2265 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2266 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002267 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002268
2269 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2270 res = hotkey_status_set(1);
2271 if (res) {
2272 hotkey_exit();
2273 return res;
2274 }
2275 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2276 & ~hotkey_reserved_mask)
2277 | hotkey_orig_mask);
2278 if (res < 0 && res != -ENXIO) {
2279 hotkey_exit();
2280 return res;
2281 }
2282
2283 dbg_printk(TPACPI_DBG_INIT,
2284 "legacy hot key reporting over procfs %s\n",
2285 (hotkey_report_mode < 2) ?
2286 "enabled" : "disabled");
2287
2288 tpacpi_inputdev->open = &hotkey_inputdev_open;
2289 tpacpi_inputdev->close = &hotkey_inputdev_close;
2290
2291 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002292 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002293 tpacpi_input_send_tabletsw();
2294
2295 return 0;
2296
2297err_exit:
2298 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2299 hotkey_dev_attributes = NULL;
2300
2301 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002302}
2303
2304static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2305{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002306 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002307 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002308 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002309 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002310 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002311
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002312 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002313 printk(TPACPI_ERR
2314 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002315 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002316 acpi_bus_generate_netlink_event(
2317 ibm->acpi->device->pnp.device_class,
2318 ibm->acpi->device->dev.bus_id,
2319 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002320 return;
2321 }
2322
2323 while (1) {
2324 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002325 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002326 return;
2327 }
2328
2329 if (hkey == 0) {
2330 /* queue empty */
2331 return;
2332 }
2333
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002334 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002335 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002336 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002337
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002338 switch (hkey >> 12) {
2339 case 1:
2340 /* 0x1000-0x1FFF: key presses */
2341 scancode = hkey & 0xfff;
2342 if (scancode > 0 && scancode < 0x21) {
2343 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002344 if (!(hotkey_source_mask & (1 << scancode))) {
2345 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002346 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002347 } else {
2348 ignore_acpi_ev = 1;
2349 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002350 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002351 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002352 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002353 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002354 case 2:
2355 /* Wakeup reason */
2356 switch (hkey) {
2357 case 0x2304: /* suspend, undock */
2358 case 0x2404: /* hibernation, undock */
2359 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2360 ignore_acpi_ev = 1;
2361 break;
2362 case 0x2305: /* suspend, bay eject */
2363 case 0x2405: /* hibernation, bay eject */
2364 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2365 ignore_acpi_ev = 1;
2366 break;
2367 default:
2368 unk_ev = 1;
2369 }
2370 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2371 printk(TPACPI_INFO
2372 "woke up due to a hot-unplug "
2373 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002374 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002375 }
2376 break;
2377 case 3:
2378 /* bay-related wakeups */
2379 if (hkey == 0x3003) {
2380 hotkey_autosleep_ack = 1;
2381 printk(TPACPI_INFO
2382 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002383 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002384 } else {
2385 unk_ev = 1;
2386 }
2387 break;
2388 case 4:
2389 /* dock-related wakeups */
2390 if (hkey == 0x4003) {
2391 hotkey_autosleep_ack = 1;
2392 printk(TPACPI_INFO
2393 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002394 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002395 } else {
2396 unk_ev = 1;
2397 }
2398 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002399 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002400 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002401 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002402 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002403 case 0x500b: /* X61t: tablet pen inserted into bay */
2404 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002405 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002406 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2407 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2408 tpacpi_input_send_tabletsw();
2409 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002410 send_acpi_ev = 0;
2411 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002412 case 0x5001:
2413 case 0x5002:
2414 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002415 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002416 break;
2417 default:
2418 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002419 }
2420 break;
2421 case 7:
2422 /* 0x7000-0x7FFF: misc */
2423 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002424 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002425 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002426 break;
2427 }
2428 /* fallthrough to default */
2429 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002430 unk_ev = 1;
2431 }
2432 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002433 printk(TPACPI_NOTICE
2434 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002435 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002436
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002437 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002438 if (!ignore_acpi_ev &&
2439 (send_acpi_ev || hotkey_report_mode < 2)) {
2440 acpi_bus_generate_proc_event(ibm->acpi->device,
2441 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002442 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002443
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002444 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002445 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002446 acpi_bus_generate_netlink_event(
2447 ibm->acpi->device->pnp.device_class,
2448 ibm->acpi->device->dev.bus_id,
2449 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002450 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002451 }
2452}
2453
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002454static void hotkey_suspend(pm_message_t state)
2455{
2456 /* Do these on suspend, we get the events on early resume! */
2457 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2458 hotkey_autosleep_ack = 0;
2459}
2460
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002461static void hotkey_resume(void)
2462{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002463 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002464 printk(TPACPI_ERR
2465 "error while trying to read hot key mask "
2466 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002467 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002468 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002469 hotkey_wakeup_reason_notify_change();
2470 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002471 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002472}
2473
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002474/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002475static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002477 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 int len = 0;
2479
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002480 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481 len += sprintf(p + len, "status:\t\tnot supported\n");
2482 return len;
2483 }
2484
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002485 if (mutex_lock_interruptible(&hotkey_mutex))
2486 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002487 res = hotkey_status_get(&status);
2488 if (!res)
2489 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002490 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002491 if (res)
2492 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002495 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002496 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 len += sprintf(p + len,
2498 "commands:\tenable, disable, reset, <mask>\n");
2499 } else {
2500 len += sprintf(p + len, "mask:\t\tnot supported\n");
2501 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2502 }
2503
2504 return len;
2505}
2506
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002507static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002509 int res, status;
2510 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002513 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return -ENODEV;
2515
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002516 if (mutex_lock_interruptible(&hotkey_mutex))
2517 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002518
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002519 status = -1;
2520 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002521
2522 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 while ((cmd = next_cmd(&buf))) {
2524 if (strlencmp(cmd, "enable") == 0) {
2525 status = 1;
2526 } else if (strlencmp(cmd, "disable") == 0) {
2527 status = 0;
2528 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002529 status = hotkey_orig_status;
2530 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2532 /* mask set */
2533 } else if (sscanf(cmd, "%x", &mask) == 1) {
2534 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002535 } else {
2536 res = -EINVAL;
2537 goto errexit;
2538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002540 if (status != -1)
2541 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002543 if (!res && mask != hotkey_mask)
2544 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002546errexit:
2547 mutex_unlock(&hotkey_mutex);
2548 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002549}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002551static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002552 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002553 {"", 0},
2554};
2555
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002556static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002557 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002558 .notify = hotkey_notify,
2559 .handle = &hkey_handle,
2560 .type = ACPI_DEVICE_NOTIFY,
2561};
2562
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002563static struct ibm_struct hotkey_driver_data = {
2564 .name = "hotkey",
2565 .read = hotkey_read,
2566 .write = hotkey_write,
2567 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002568 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002569 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002570 .acpi = &ibm_hotkey_acpidriver,
2571};
2572
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002573/*************************************************************************
2574 * Bluetooth subdriver
2575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002577enum {
2578 /* ACPI GBDC/SBDC bits */
2579 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2580 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2581 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2582};
2583
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002584static int bluetooth_get_radiosw(void)
2585{
2586 int status;
2587
2588 if (!tp_features.bluetooth)
2589 return -ENODEV;
2590
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002591 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2592 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2593 return 0;
2594
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002595 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2596 return -EIO;
2597
2598 return (status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0;
2599}
2600
2601static int bluetooth_set_radiosw(int radio_on)
2602{
2603 int status;
2604
2605 if (!tp_features.bluetooth)
2606 return -ENODEV;
2607
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002608 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2609 * reason to risk weird behaviour. */
2610 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2611 && radio_on)
2612 return -EPERM;
2613
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002614 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2615 return -EIO;
2616 if (radio_on)
2617 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2618 else
2619 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2620 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2621 return -EIO;
2622
2623 return 0;
2624}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002625
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002626/* sysfs bluetooth enable ---------------------------------------------- */
2627static ssize_t bluetooth_enable_show(struct device *dev,
2628 struct device_attribute *attr,
2629 char *buf)
2630{
2631 int status;
2632
2633 status = bluetooth_get_radiosw();
2634 if (status < 0)
2635 return status;
2636
2637 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2638}
2639
2640static ssize_t bluetooth_enable_store(struct device *dev,
2641 struct device_attribute *attr,
2642 const char *buf, size_t count)
2643{
2644 unsigned long t;
2645 int res;
2646
2647 if (parse_strtoul(buf, 1, &t))
2648 return -EINVAL;
2649
2650 res = bluetooth_set_radiosw(t);
2651
2652 return (res) ? res : count;
2653}
2654
2655static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002656 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002657 bluetooth_enable_show, bluetooth_enable_store);
2658
2659/* --------------------------------------------------------------------- */
2660
2661static struct attribute *bluetooth_attributes[] = {
2662 &dev_attr_bluetooth_enable.attr,
2663 NULL
2664};
2665
2666static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002667 .attrs = bluetooth_attributes,
2668};
2669
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002670static void bluetooth_exit(void)
2671{
2672 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2673 &bluetooth_attr_group);
2674}
2675
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002676static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002678 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002679 int status = 0;
2680
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002681 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2682
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002683 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002684
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002685 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2686 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002687 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002688 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002690 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2691 str_supported(tp_features.bluetooth),
2692 status);
2693
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002694 if (tp_features.bluetooth &&
2695 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2696 /* no bluetooth hardware present in system */
2697 tp_features.bluetooth = 0;
2698 dbg_printk(TPACPI_DBG_INIT,
2699 "bluetooth hardware not installed\n");
2700 }
2701
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002702 if (tp_features.bluetooth) {
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002703 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2704 &bluetooth_attr_group);
2705 if (res)
2706 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002707 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002708
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002709 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002712/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002713static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
2715 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002716 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002718 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002721 len += sprintf(p + len, "status:\t\t%s\n",
2722 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 len += sprintf(p + len, "commands:\tenable, disable\n");
2724 }
2725
2726 return len;
2727}
2728
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002729static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002733 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002734 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736 while ((cmd = next_cmd(&buf))) {
2737 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002738 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002740 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 } else
2742 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return 0;
2746}
2747
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002748static struct ibm_struct bluetooth_driver_data = {
2749 .name = "bluetooth",
2750 .read = bluetooth_read,
2751 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002752 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002753};
2754
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002755/*************************************************************************
2756 * Wan subdriver
2757 */
2758
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002759enum {
2760 /* ACPI GWAN/SWAN bits */
2761 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2762 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2763 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2764};
2765
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002766static int wan_get_radiosw(void)
2767{
2768 int status;
2769
2770 if (!tp_features.wan)
2771 return -ENODEV;
2772
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002773 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2774 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2775 return 0;
2776
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002777 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2778 return -EIO;
2779
2780 return (status & TP_ACPI_WANCARD_RADIOSSW) != 0;
2781}
2782
2783static int wan_set_radiosw(int radio_on)
2784{
2785 int status;
2786
2787 if (!tp_features.wan)
2788 return -ENODEV;
2789
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002790 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2791 * reason to risk weird behaviour. */
2792 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2793 && radio_on)
2794 return -EPERM;
2795
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002796 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2797 return -EIO;
2798 if (radio_on)
2799 status |= TP_ACPI_WANCARD_RADIOSSW;
2800 else
2801 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2802 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2803 return -EIO;
2804
2805 return 0;
2806}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002807
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002808/* sysfs wan enable ---------------------------------------------------- */
2809static ssize_t wan_enable_show(struct device *dev,
2810 struct device_attribute *attr,
2811 char *buf)
2812{
2813 int status;
2814
2815 status = wan_get_radiosw();
2816 if (status < 0)
2817 return status;
2818
2819 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2820}
2821
2822static ssize_t wan_enable_store(struct device *dev,
2823 struct device_attribute *attr,
2824 const char *buf, size_t count)
2825{
2826 unsigned long t;
2827 int res;
2828
2829 if (parse_strtoul(buf, 1, &t))
2830 return -EINVAL;
2831
2832 res = wan_set_radiosw(t);
2833
2834 return (res) ? res : count;
2835}
2836
2837static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002838 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002839 wan_enable_show, wan_enable_store);
2840
2841/* --------------------------------------------------------------------- */
2842
2843static struct attribute *wan_attributes[] = {
2844 &dev_attr_wan_enable.attr,
2845 NULL
2846};
2847
2848static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002849 .attrs = wan_attributes,
2850};
2851
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002852static void wan_exit(void)
2853{
2854 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2855 &wan_attr_group);
2856}
2857
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002858static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002859{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002860 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002861 int status = 0;
2862
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002863 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2864
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002865 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002866
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002867 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002868 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002869
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002870 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2871 str_supported(tp_features.wan),
2872 status);
2873
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002874 if (tp_features.wan &&
2875 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
2876 /* no wan hardware present in system */
2877 tp_features.wan = 0;
2878 dbg_printk(TPACPI_DBG_INIT,
2879 "wan hardware not installed\n");
2880 }
2881
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002882 if (tp_features.wan) {
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002883 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2884 &wan_attr_group);
2885 if (res)
2886 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002887 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002888
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002889 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002890}
2891
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002892/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002893static int wan_read(char *p)
2894{
2895 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002896 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002897
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002898 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002899 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002900 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002901 len += sprintf(p + len, "status:\t\t%s\n",
2902 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002903 len += sprintf(p + len, "commands:\tenable, disable\n");
2904 }
2905
2906 return len;
2907}
2908
2909static int wan_write(char *buf)
2910{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002911 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002912
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002913 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002914 return -ENODEV;
2915
2916 while ((cmd = next_cmd(&buf))) {
2917 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002918 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002919 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002920 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002921 } else
2922 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002923 }
2924
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002925 return 0;
2926}
2927
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002928static struct ibm_struct wan_driver_data = {
2929 .name = "wan",
2930 .read = wan_read,
2931 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002932 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002933 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002934};
2935
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002936/*************************************************************************
2937 * Video subdriver
2938 */
2939
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002940#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2941
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002942enum video_access_mode {
2943 TPACPI_VIDEO_NONE = 0,
2944 TPACPI_VIDEO_570, /* 570 */
2945 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2946 TPACPI_VIDEO_NEW, /* all others */
2947};
2948
2949enum { /* video status flags, based on VIDEO_570 */
2950 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2951 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2952 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2953};
2954
2955enum { /* TPACPI_VIDEO_570 constants */
2956 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2957 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2958 * video_status_flags */
2959 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2960 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2961};
2962
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002963static enum video_access_mode video_supported;
2964static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002965
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002966static int video_autosw_get(void);
2967static int video_autosw_set(int enable);
2968
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002969TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002970
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002971static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002973 int ivga;
2974
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002975 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2976
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002977 TPACPI_ACPIHANDLE_INIT(vid);
2978 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002979
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002980 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2981 /* G41, assume IVGA doesn't change */
2982 vid_handle = vid2_handle;
2983
2984 if (!vid_handle)
2985 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002986 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002987 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2988 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002989 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002990 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2991 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002992 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002993 else
2994 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002995 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002997 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2998 str_supported(video_supported != TPACPI_VIDEO_NONE),
2999 video_supported);
3000
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003001 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003004static void video_exit(void)
3005{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003006 dbg_printk(TPACPI_DBG_EXIT,
3007 "restoring original video autoswitch mode\n");
3008 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003009 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003010 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003011}
3012
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003013static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
3015 int status = 0;
3016 int i;
3017
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003018 switch (video_supported) {
3019 case TPACPI_VIDEO_570:
3020 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3021 TP_ACPI_VIDEO_570_PHSCMD))
3022 return -EIO;
3023 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3024 break;
3025 case TPACPI_VIDEO_770:
3026 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3027 return -EIO;
3028 if (i)
3029 status |= TP_ACPI_VIDEO_S_LCD;
3030 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3031 return -EIO;
3032 if (i)
3033 status |= TP_ACPI_VIDEO_S_CRT;
3034 break;
3035 case TPACPI_VIDEO_NEW:
3036 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3037 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3038 return -EIO;
3039 if (i)
3040 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003042 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3043 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3044 return -EIO;
3045 if (i)
3046 status |= TP_ACPI_VIDEO_S_LCD;
3047 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3048 return -EIO;
3049 if (i)
3050 status |= TP_ACPI_VIDEO_S_DVI;
3051 break;
3052 default:
3053 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
3056 return status;
3057}
3058
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003059static int video_outputsw_set(int status)
3060{
3061 int autosw;
3062 int res = 0;
3063
3064 switch (video_supported) {
3065 case TPACPI_VIDEO_570:
3066 res = acpi_evalf(NULL, NULL,
3067 "\\_SB.PHS2", "vdd",
3068 TP_ACPI_VIDEO_570_PHS2CMD,
3069 status | TP_ACPI_VIDEO_570_PHS2SET);
3070 break;
3071 case TPACPI_VIDEO_770:
3072 autosw = video_autosw_get();
3073 if (autosw < 0)
3074 return autosw;
3075
3076 res = video_autosw_set(1);
3077 if (res)
3078 return res;
3079 res = acpi_evalf(vid_handle, NULL,
3080 "ASWT", "vdd", status * 0x100, 0);
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;
3085 }
3086 break;
3087 case TPACPI_VIDEO_NEW:
3088 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003089 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003090 break;
3091 default:
3092 return -ENOSYS;
3093 }
3094
3095 return (res)? 0 : -EIO;
3096}
3097
3098static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003100 int autosw = 0;
3101
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003102 switch (video_supported) {
3103 case TPACPI_VIDEO_570:
3104 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3105 return -EIO;
3106 break;
3107 case TPACPI_VIDEO_770:
3108 case TPACPI_VIDEO_NEW:
3109 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3110 return -EIO;
3111 break;
3112 default:
3113 return -ENOSYS;
3114 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003115
3116 return autosw & 1;
3117}
3118
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003119static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003120{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003121 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003122 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003123 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003124}
3125
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003126static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003127{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003128 int autosw = video_autosw_get();
3129 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003130
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003131 if (autosw < 0)
3132 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003133
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003134 switch (video_supported) {
3135 case TPACPI_VIDEO_570:
3136 res = video_autosw_set(1);
3137 if (res)
3138 return res;
3139 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3140 break;
3141 case TPACPI_VIDEO_770:
3142 case TPACPI_VIDEO_NEW:
3143 res = video_autosw_set(1);
3144 if (res)
3145 return res;
3146 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3147 break;
3148 default:
3149 return -ENOSYS;
3150 }
3151 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003152 printk(TPACPI_ERR
3153 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003154 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003155 }
3156
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003157 return (res)? 0 : -EIO;
3158}
3159
3160static int video_expand_toggle(void)
3161{
3162 switch (video_supported) {
3163 case TPACPI_VIDEO_570:
3164 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3165 0 : -EIO;
3166 case TPACPI_VIDEO_770:
3167 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3168 0 : -EIO;
3169 case TPACPI_VIDEO_NEW:
3170 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3171 0 : -EIO;
3172 default:
3173 return -ENOSYS;
3174 }
3175 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003176}
3177
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003178static int video_read(char *p)
3179{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003180 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003181 int len = 0;
3182
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003183 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003184 len += sprintf(p + len, "status:\t\tnot supported\n");
3185 return len;
3186 }
3187
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003188 status = video_outputsw_get();
3189 if (status < 0)
3190 return status;
3191
3192 autosw = video_autosw_get();
3193 if (autosw < 0)
3194 return autosw;
3195
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003196 len += sprintf(p + len, "status:\t\tsupported\n");
3197 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3198 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003199 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003200 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3201 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3202 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3203 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003204 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003205 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3206 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3207 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3208
3209 return len;
3210}
3211
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003212static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213{
3214 char *cmd;
3215 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003216 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003218 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003219 return -ENODEV;
3220
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003221 enable = 0;
3222 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 while ((cmd = next_cmd(&buf))) {
3225 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003226 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003228 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003230 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003232 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003233 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003234 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003235 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003236 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003237 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003238 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003240 res = video_autosw_set(1);
3241 if (res)
3242 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003244 res = video_autosw_set(0);
3245 if (res)
3246 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003248 res = video_outputsw_cycle();
3249 if (res)
3250 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003252 res = video_expand_toggle();
3253 if (res)
3254 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 } else
3256 return -EINVAL;
3257 }
3258
3259 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003260 status = video_outputsw_get();
3261 if (status < 0)
3262 return status;
3263 res = video_outputsw_set((status & ~disable) | enable);
3264 if (res)
3265 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
3267
3268 return 0;
3269}
3270
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003271static struct ibm_struct video_driver_data = {
3272 .name = "video",
3273 .read = video_read,
3274 .write = video_write,
3275 .exit = video_exit,
3276};
3277
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003278#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3279
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003280/*************************************************************************
3281 * Light (thinklight) subdriver
3282 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003284TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3285TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003286
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003287static int light_get_status(void)
3288{
3289 int status = 0;
3290
3291 if (tp_features.light_status) {
3292 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3293 return -EIO;
3294 return (!!status);
3295 }
3296
3297 return -ENXIO;
3298}
3299
3300static int light_set_status(int status)
3301{
3302 int rc;
3303
3304 if (tp_features.light) {
3305 if (cmos_handle) {
3306 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3307 (status)?
3308 TP_CMOS_THINKLIGHT_ON :
3309 TP_CMOS_THINKLIGHT_OFF);
3310 } else {
3311 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3312 (status)? 1 : 0);
3313 }
3314 return (rc)? 0 : -EIO;
3315 }
3316
3317 return -ENXIO;
3318}
3319
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003320static void light_set_status_worker(struct work_struct *work)
3321{
3322 struct tpacpi_led_classdev *data =
3323 container_of(work, struct tpacpi_led_classdev, work);
3324
3325 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3326 light_set_status((data->new_brightness != LED_OFF));
3327}
3328
3329static void light_sysfs_set(struct led_classdev *led_cdev,
3330 enum led_brightness brightness)
3331{
3332 struct tpacpi_led_classdev *data =
3333 container_of(led_cdev,
3334 struct tpacpi_led_classdev,
3335 led_classdev);
3336 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003337 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003338}
3339
3340static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3341{
3342 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3343}
3344
3345static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3346 .led_classdev = {
3347 .name = "tpacpi::thinklight",
3348 .brightness_set = &light_sysfs_set,
3349 .brightness_get = &light_sysfs_get,
3350 }
3351};
3352
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003353static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003355 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003356
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003357 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3358
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003359 TPACPI_ACPIHANDLE_INIT(ledb);
3360 TPACPI_ACPIHANDLE_INIT(lght);
3361 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003362 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003363
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003364 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003365 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003366
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003367 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003368 /* light status not supported on
3369 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003370 tp_features.light_status =
3371 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003373 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3374 str_supported(tp_features.light),
3375 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003376
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003377 if (!tp_features.light)
3378 return 1;
3379
3380 rc = led_classdev_register(&tpacpi_pdev->dev,
3381 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003382
3383 if (rc < 0) {
3384 tp_features.light = 0;
3385 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003386 } else {
3387 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003388 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003389
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003390 return rc;
3391}
3392
3393static void light_exit(void)
3394{
3395 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3396 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003397 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
3399
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003400static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
3402 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003403 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003405 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003406 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003407 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003408 len += sprintf(p + len, "status:\t\tunknown\n");
3409 len += sprintf(p + len, "commands:\ton, off\n");
3410 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003411 status = light_get_status();
3412 if (status < 0)
3413 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003415 len += sprintf(p + len, "commands:\ton, off\n");
3416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
3418 return len;
3419}
3420
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003421static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003424 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003425
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003426 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003427 return -ENODEV;
3428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 while ((cmd = next_cmd(&buf))) {
3430 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003431 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003433 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 } else
3435 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
3437
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003438 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439}
3440
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003441static struct ibm_struct light_driver_data = {
3442 .name = "light",
3443 .read = light_read,
3444 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003445 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003446};
3447
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003448/*************************************************************************
3449 * Dock subdriver
3450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003452#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003453
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003454static void dock_notify(struct ibm_struct *ibm, u32 event);
3455static int dock_read(char *p);
3456static int dock_write(char *buf);
3457
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003458TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003459 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3460 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3461 "\\_SB.PCI.ISA.SLCE", /* 570 */
3462 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3463
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003464/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003465TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003466
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003467static const struct acpi_device_id ibm_pci_device_ids[] = {
3468 {PCI_ROOT_HID_STRING, 0},
3469 {"", 0},
3470};
3471
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003472static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3473 {
3474 .notify = dock_notify,
3475 .handle = &dock_handle,
3476 .type = ACPI_SYSTEM_NOTIFY,
3477 },
3478 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003479 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3480 * We just use it to get notifications of dock hotplug
3481 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003482 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003483 .notify = dock_notify,
3484 .handle = &pci_handle,
3485 .type = ACPI_SYSTEM_NOTIFY,
3486 },
3487};
3488
3489static struct ibm_struct dock_driver_data[2] = {
3490 {
3491 .name = "dock",
3492 .read = dock_read,
3493 .write = dock_write,
3494 .acpi = &ibm_dock_acpidriver[0],
3495 },
3496 {
3497 .name = "dock",
3498 .acpi = &ibm_dock_acpidriver[1],
3499 },
3500};
3501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502#define dock_docked() (_sta(dock_handle) & 1)
3503
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003504static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003505{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003506 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3507
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003508 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003509
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003510 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3511 str_supported(dock_handle != NULL));
3512
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003513 return (dock_handle)? 0 : 1;
3514}
3515
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003516static int __init dock_init2(struct ibm_init_struct *iibm)
3517{
3518 int dock2_needed;
3519
3520 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3521
3522 if (dock_driver_data[0].flags.acpi_driver_registered &&
3523 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003524 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003525 dock2_needed = (pci_handle != NULL);
3526 vdbg_printk(TPACPI_DBG_INIT,
3527 "dock PCI handler for the TP 570 is %s\n",
3528 str_supported(dock2_needed));
3529 } else {
3530 vdbg_printk(TPACPI_DBG_INIT,
3531 "dock subdriver part 2 not required\n");
3532 dock2_needed = 0;
3533 }
3534
3535 return (dock2_needed)? 0 : 1;
3536}
3537
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003538static void dock_notify(struct ibm_struct *ibm, u32 event)
3539{
3540 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003541 int pci = ibm->acpi->hid && ibm->acpi->device &&
3542 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003543 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003544
3545 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003546 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003547 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003548 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003549 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003550 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003551 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003552 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003553 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003554 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003555 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003556 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003557 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003558 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003559 }
Len Brown14e04fb32007-08-23 15:20:26 -04003560 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003561 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3562 ibm->acpi->device->dev.bus_id,
3563 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003564}
3565
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003566static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 int len = 0;
3569 int docked = dock_docked();
3570
3571 if (!dock_handle)
3572 len += sprintf(p + len, "status:\t\tnot supported\n");
3573 else if (!docked)
3574 len += sprintf(p + len, "status:\t\tundocked\n");
3575 else {
3576 len += sprintf(p + len, "status:\t\tdocked\n");
3577 len += sprintf(p + len, "commands:\tdock, undock\n");
3578 }
3579
3580 return len;
3581}
3582
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003583static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
3585 char *cmd;
3586
3587 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003588 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590 while ((cmd = next_cmd(&buf))) {
3591 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003592 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3593 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 return -EIO;
3595 } else if (strlencmp(cmd, "dock") == 0) {
3596 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3597 return -EIO;
3598 } else
3599 return -EINVAL;
3600 }
3601
3602 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003605#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003607/*************************************************************************
3608 * Bay subdriver
3609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003611#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003612
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003613TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003614 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3615 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3616 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3617 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003618TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003619 "_EJ0", /* all others */
3620 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003621TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003622 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3623 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003624TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003625 "_EJ0", /* 770x */
3626 ); /* all others */
3627
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003628static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003630 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3631
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003632 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003633 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003634 TPACPI_ACPIHANDLE_INIT(bay_ej);
3635 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003636 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003637 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003638
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003639 tp_features.bay_status = bay_handle &&
3640 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3641 tp_features.bay_status2 = bay2_handle &&
3642 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003643
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003644 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3645 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3646 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3647 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003649 vdbg_printk(TPACPI_DBG_INIT,
3650 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003651 str_supported(tp_features.bay_status),
3652 str_supported(tp_features.bay_eject),
3653 str_supported(tp_features.bay_status2),
3654 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003655
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003656 return (tp_features.bay_status || tp_features.bay_eject ||
3657 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658}
3659
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003660static void bay_notify(struct ibm_struct *ibm, u32 event)
3661{
Len Brown14e04fb32007-08-23 15:20:26 -04003662 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003663 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3664 ibm->acpi->device->dev.bus_id,
3665 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003666}
3667
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003668#define bay_occupied(b) (_sta(b##_handle) & 1)
3669
3670static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
3672 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003673 int occupied = bay_occupied(bay);
3674 int occupied2 = bay_occupied(bay2);
3675 int eject, eject2;
3676
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003677 len += sprintf(p + len, "status:\t\t%s\n",
3678 tp_features.bay_status ?
3679 (occupied ? "occupied" : "unoccupied") :
3680 "not supported");
3681 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003682 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3683 "occupied" : "unoccupied");
3684
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003685 eject = tp_features.bay_eject && occupied;
3686 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003687
3688 if (eject && eject2)
3689 len += sprintf(p + len, "commands:\teject, eject2\n");
3690 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003692 else if (eject2)
3693 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
3695 return len;
3696}
3697
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003698static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
3700 char *cmd;
3701
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003702 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003703 return -ENODEV;
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003706 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003707 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3708 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003709 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003710 strlencmp(cmd, "eject2") == 0) {
3711 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 return -EIO;
3713 } else
3714 return -EINVAL;
3715 }
3716
3717 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003718}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003719
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003720static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3721 .notify = bay_notify,
3722 .handle = &bay_handle,
3723 .type = ACPI_SYSTEM_NOTIFY,
3724};
3725
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003726static struct ibm_struct bay_driver_data = {
3727 .name = "bay",
3728 .read = bay_read,
3729 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003730 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003731};
3732
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003733#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003735/*************************************************************************
3736 * CMOS subdriver
3737 */
3738
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003739/* sysfs cmos_command -------------------------------------------------- */
3740static ssize_t cmos_command_store(struct device *dev,
3741 struct device_attribute *attr,
3742 const char *buf, size_t count)
3743{
3744 unsigned long cmos_cmd;
3745 int res;
3746
3747 if (parse_strtoul(buf, 21, &cmos_cmd))
3748 return -EINVAL;
3749
3750 res = issue_thinkpad_cmos_command(cmos_cmd);
3751 return (res)? res : count;
3752}
3753
3754static struct device_attribute dev_attr_cmos_command =
3755 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3756
3757/* --------------------------------------------------------------------- */
3758
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003759static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003760{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003761 int res;
3762
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003763 vdbg_printk(TPACPI_DBG_INIT,
3764 "initializing cmos commands subdriver\n");
3765
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003766 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003767
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003768 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3769 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003770
3771 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3772 if (res)
3773 return res;
3774
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003775 return (cmos_handle)? 0 : 1;
3776}
3777
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003778static void cmos_exit(void)
3779{
3780 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3781}
3782
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003783static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
3785 int len = 0;
3786
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003787 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3788 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 if (!cmos_handle)
3790 len += sprintf(p + len, "status:\t\tnot supported\n");
3791 else {
3792 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003793 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 }
3795
3796 return len;
3797}
3798
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003799static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
3801 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003802 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
3804 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003805 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3806 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 /* cmos_cmd set */
3808 } else
3809 return -EINVAL;
3810
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003811 res = issue_thinkpad_cmos_command(cmos_cmd);
3812 if (res)
3813 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 }
3815
3816 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003817}
3818
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003819static struct ibm_struct cmos_driver_data = {
3820 .name = "cmos",
3821 .read = cmos_read,
3822 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003823 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003824};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003825
3826/*************************************************************************
3827 * LED subdriver
3828 */
3829
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003830enum led_access_mode {
3831 TPACPI_LED_NONE = 0,
3832 TPACPI_LED_570, /* 570 */
3833 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3834 TPACPI_LED_NEW, /* all others */
3835};
3836
3837enum { /* For TPACPI_LED_OLD */
3838 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3839 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3840 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3841};
3842
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003843enum led_status_t {
3844 TPACPI_LED_OFF = 0,
3845 TPACPI_LED_ON,
3846 TPACPI_LED_BLINK,
3847};
3848
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003849static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003850
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003851TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003852 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3853 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003854 "LED", /* all others */
3855 ); /* R30, R31 */
3856
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003857#define TPACPI_LED_NUMLEDS 8
3858static struct tpacpi_led_classdev *tpacpi_leds;
3859static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07003860static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003861 /* there's a limit of 19 chars + NULL before 2.6.26 */
3862 "tpacpi::power",
3863 "tpacpi:orange:batt",
3864 "tpacpi:green:batt",
3865 "tpacpi::dock_active",
3866 "tpacpi::bay_active",
3867 "tpacpi::dock_batt",
3868 "tpacpi::unknown_led",
3869 "tpacpi::standby",
3870};
3871
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03003872static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003873{
3874 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003875 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003876
3877 switch (led_supported) {
3878 case TPACPI_LED_570:
3879 if (!acpi_evalf(ec_handle,
3880 &status, "GLED", "dd", 1 << led))
3881 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003882 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003883 TPACPI_LED_OFF :
3884 ((status == 1)?
3885 TPACPI_LED_ON :
3886 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003887 tpacpi_led_state_cache[led] = led_s;
3888 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003889 default:
3890 return -ENXIO;
3891 }
3892
3893 /* not reached */
3894}
3895
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03003896static int led_set_status(const unsigned int led,
3897 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003898{
3899 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03003900 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
3901 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003902
3903 int rc = 0;
3904
3905 switch (led_supported) {
3906 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03003907 /* 570 */
3908 if (led > 7)
3909 return -EINVAL;
3910 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3911 (1 << led), led_sled_arg1[ledstatus]))
3912 rc = -EIO;
3913 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003914 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03003915 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3916 if (led > 7)
3917 return -EINVAL;
3918 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
3919 if (rc >= 0)
3920 rc = ec_write(TPACPI_LED_EC_HLBL,
3921 (ledstatus == TPACPI_LED_BLINK) << led);
3922 if (rc >= 0)
3923 rc = ec_write(TPACPI_LED_EC_HLCL,
3924 (ledstatus != TPACPI_LED_OFF) << led);
3925 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003926 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03003927 /* all others */
3928 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3929 led, led_led_arg1[ledstatus]))
3930 rc = -EIO;
3931 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003932 default:
3933 rc = -ENXIO;
3934 }
3935
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003936 if (!rc)
3937 tpacpi_led_state_cache[led] = ledstatus;
3938
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003939 return rc;
3940}
3941
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003942static void led_sysfs_set_status(unsigned int led,
3943 enum led_brightness brightness)
3944{
3945 led_set_status(led,
3946 (brightness == LED_OFF) ?
3947 TPACPI_LED_OFF :
3948 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
3949 TPACPI_LED_BLINK : TPACPI_LED_ON);
3950}
3951
3952static void led_set_status_worker(struct work_struct *work)
3953{
3954 struct tpacpi_led_classdev *data =
3955 container_of(work, struct tpacpi_led_classdev, work);
3956
3957 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3958 led_sysfs_set_status(data->led, data->new_brightness);
3959}
3960
3961static void led_sysfs_set(struct led_classdev *led_cdev,
3962 enum led_brightness brightness)
3963{
3964 struct tpacpi_led_classdev *data = container_of(led_cdev,
3965 struct tpacpi_led_classdev, led_classdev);
3966
3967 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003968 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003969}
3970
3971static int led_sysfs_blink_set(struct led_classdev *led_cdev,
3972 unsigned long *delay_on, unsigned long *delay_off)
3973{
3974 struct tpacpi_led_classdev *data = container_of(led_cdev,
3975 struct tpacpi_led_classdev, led_classdev);
3976
3977 /* Can we choose the flash rate? */
3978 if (*delay_on == 0 && *delay_off == 0) {
3979 /* yes. set them to the hardware blink rate (1 Hz) */
3980 *delay_on = 500; /* ms */
3981 *delay_off = 500; /* ms */
3982 } else if ((*delay_on != 500) || (*delay_off != 500))
3983 return -EINVAL;
3984
3985 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003986 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003987
3988 return 0;
3989}
3990
3991static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
3992{
3993 int rc;
3994
3995 struct tpacpi_led_classdev *data = container_of(led_cdev,
3996 struct tpacpi_led_classdev, led_classdev);
3997
3998 rc = led_get_status(data->led);
3999
4000 if (rc == TPACPI_LED_OFF || rc < 0)
4001 rc = LED_OFF; /* no error handling in led class :( */
4002 else
4003 rc = LED_FULL;
4004
4005 return rc;
4006}
4007
4008static void led_exit(void)
4009{
4010 unsigned int i;
4011
4012 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4013 if (tpacpi_leds[i].led_classdev.name)
4014 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4015 }
4016
4017 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004018}
4019
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004020static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004021{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004022 unsigned int i;
4023 int rc;
4024
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004025 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4026
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004027 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004028
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004029 if (!led_handle)
4030 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004031 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004032 else if (strlencmp(led_path, "SLED") == 0)
4033 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004034 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004035 else if (strlencmp(led_path, "SYSL") == 0)
4036 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004037 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004038 else
4039 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004040 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004041
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004042 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4043 str_supported(led_supported), led_supported);
4044
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004045 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4046 GFP_KERNEL);
4047 if (!tpacpi_leds) {
4048 printk(TPACPI_ERR "Out of memory for LED data\n");
4049 return -ENOMEM;
4050 }
4051
4052 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4053 tpacpi_leds[i].led = i;
4054
4055 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4056 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4057 if (led_supported == TPACPI_LED_570)
4058 tpacpi_leds[i].led_classdev.brightness_get =
4059 &led_sysfs_get;
4060
4061 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4062
4063 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4064
4065 rc = led_classdev_register(&tpacpi_pdev->dev,
4066 &tpacpi_leds[i].led_classdev);
4067 if (rc < 0) {
4068 tpacpi_leds[i].led_classdev.name = NULL;
4069 led_exit();
4070 return rc;
4071 }
4072 }
4073
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004074 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004075}
4076
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004077#define str_led_status(s) \
4078 ((s) == TPACPI_LED_OFF ? "off" : \
4079 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004080
4081static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082{
4083 int len = 0;
4084
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004085 if (!led_supported) {
4086 len += sprintf(p + len, "status:\t\tnot supported\n");
4087 return len;
4088 }
4089 len += sprintf(p + len, "status:\t\tsupported\n");
4090
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004091 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004092 /* 570 */
4093 int i, status;
4094 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004095 status = led_get_status(i);
4096 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004097 return -EIO;
4098 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004099 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004100 }
4101 }
4102
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004104 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
4106 return len;
4107}
4108
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004109static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004112 int led, rc;
4113 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004114
4115 if (!led_supported)
4116 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004119 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 return -EINVAL;
4121
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004122 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004123 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004125 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004126 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004127 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004128 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004129 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004130 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004131
4132 rc = led_set_status(led, s);
4133 if (rc < 0)
4134 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 }
4136
4137 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004138}
4139
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004140static struct ibm_struct led_driver_data = {
4141 .name = "led",
4142 .read = led_read,
4143 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004144 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004145};
4146
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004147/*************************************************************************
4148 * Beep subdriver
4149 */
4150
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004151TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004152
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004153static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004154{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004155 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4156
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004157 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004158
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004159 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4160 str_supported(beep_handle != NULL));
4161
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004162 return (beep_handle)? 0 : 1;
4163}
4164
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004165static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
4167 int len = 0;
4168
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004169 if (!beep_handle)
4170 len += sprintf(p + len, "status:\t\tnot supported\n");
4171 else {
4172 len += sprintf(p + len, "status:\t\tsupported\n");
4173 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 return len;
4177}
4178
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004179static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
4181 char *cmd;
4182 int beep_cmd;
4183
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004184 if (!beep_handle)
4185 return -ENODEV;
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004188 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4189 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 /* beep_cmd set */
4191 } else
4192 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004193 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 return -EIO;
4195 }
4196
4197 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004198}
4199
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004200static struct ibm_struct beep_driver_data = {
4201 .name = "beep",
4202 .read = beep_read,
4203 .write = beep_write,
4204};
4205
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004206/*************************************************************************
4207 * Thermal subdriver
4208 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004209
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004210enum thermal_access_mode {
4211 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4212 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4213 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4214 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4215 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4216};
4217
4218enum { /* TPACPI_THERMAL_TPEC_* */
4219 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4220 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4221 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4222};
4223
4224#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4225struct ibm_thermal_sensors_struct {
4226 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4227};
4228
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004229static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004230
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004231/* idx is zero-based */
4232static int thermal_get_sensor(int idx, s32 *value)
4233{
4234 int t;
4235 s8 tmp;
4236 char tmpi[5];
4237
4238 t = TP_EC_THERMAL_TMP0;
4239
4240 switch (thermal_read_mode) {
4241#if TPACPI_MAX_THERMAL_SENSORS >= 16
4242 case TPACPI_THERMAL_TPEC_16:
4243 if (idx >= 8 && idx <= 15) {
4244 t = TP_EC_THERMAL_TMP8;
4245 idx -= 8;
4246 }
4247 /* fallthrough */
4248#endif
4249 case TPACPI_THERMAL_TPEC_8:
4250 if (idx <= 7) {
4251 if (!acpi_ec_read(t + idx, &tmp))
4252 return -EIO;
4253 *value = tmp * 1000;
4254 return 0;
4255 }
4256 break;
4257
4258 case TPACPI_THERMAL_ACPI_UPDT:
4259 if (idx <= 7) {
4260 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4261 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4262 return -EIO;
4263 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4264 return -EIO;
4265 *value = (t - 2732) * 100;
4266 return 0;
4267 }
4268 break;
4269
4270 case TPACPI_THERMAL_ACPI_TMP07:
4271 if (idx <= 7) {
4272 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4273 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4274 return -EIO;
4275 if (t > 127 || t < -127)
4276 t = TP_EC_THERMAL_TMP_NA;
4277 *value = t * 1000;
4278 return 0;
4279 }
4280 break;
4281
4282 case TPACPI_THERMAL_NONE:
4283 default:
4284 return -ENOSYS;
4285 }
4286
4287 return -EINVAL;
4288}
4289
4290static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4291{
4292 int res, i;
4293 int n;
4294
4295 n = 8;
4296 i = 0;
4297
4298 if (!s)
4299 return -EINVAL;
4300
4301 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4302 n = 16;
4303
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004304 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004305 res = thermal_get_sensor(i, &s->temp[i]);
4306 if (res)
4307 return res;
4308 }
4309
4310 return n;
4311}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004312
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004313/* sysfs temp##_input -------------------------------------------------- */
4314
4315static ssize_t thermal_temp_input_show(struct device *dev,
4316 struct device_attribute *attr,
4317 char *buf)
4318{
4319 struct sensor_device_attribute *sensor_attr =
4320 to_sensor_dev_attr(attr);
4321 int idx = sensor_attr->index;
4322 s32 value;
4323 int res;
4324
4325 res = thermal_get_sensor(idx, &value);
4326 if (res)
4327 return res;
4328 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4329 return -ENXIO;
4330
4331 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4332}
4333
4334#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004335 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4336 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004337
4338static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4339 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4340 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4341 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4342 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4343 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4344 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4345 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4346 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4347 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4348 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4349 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4350 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4351 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4352 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4353 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4354 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4355};
4356
4357#define THERMAL_ATTRS(X) \
4358 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4359
4360static struct attribute *thermal_temp_input_attr[] = {
4361 THERMAL_ATTRS(8),
4362 THERMAL_ATTRS(9),
4363 THERMAL_ATTRS(10),
4364 THERMAL_ATTRS(11),
4365 THERMAL_ATTRS(12),
4366 THERMAL_ATTRS(13),
4367 THERMAL_ATTRS(14),
4368 THERMAL_ATTRS(15),
4369 THERMAL_ATTRS(0),
4370 THERMAL_ATTRS(1),
4371 THERMAL_ATTRS(2),
4372 THERMAL_ATTRS(3),
4373 THERMAL_ATTRS(4),
4374 THERMAL_ATTRS(5),
4375 THERMAL_ATTRS(6),
4376 THERMAL_ATTRS(7),
4377 NULL
4378};
4379
4380static const struct attribute_group thermal_temp_input16_group = {
4381 .attrs = thermal_temp_input_attr
4382};
4383
4384static const struct attribute_group thermal_temp_input8_group = {
4385 .attrs = &thermal_temp_input_attr[8]
4386};
4387
4388#undef THERMAL_SENSOR_ATTR_TEMP
4389#undef THERMAL_ATTRS
4390
4391/* --------------------------------------------------------------------- */
4392
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004393static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004394{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004395 u8 t, ta1, ta2;
4396 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004397 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004398 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004399
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004400 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4401
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004402 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004403
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004404 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004405 /*
4406 * Direct EC access mode: sensors at registers
4407 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4408 * non-implemented, thermal sensors return 0x80 when
4409 * not available
4410 */
4411
4412 ta1 = ta2 = 0;
4413 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004414 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004415 ta1 |= t;
4416 } else {
4417 ta1 = 0;
4418 break;
4419 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004420 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004421 ta2 |= t;
4422 } else {
4423 ta1 = 0;
4424 break;
4425 }
4426 }
4427 if (ta1 == 0) {
4428 /* This is sheer paranoia, but we handle it anyway */
4429 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004430 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004431 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004432 "falling back to ACPI TMPx access "
4433 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004434 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004435 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004436 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004437 "ThinkPad ACPI EC access misbehaving, "
4438 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004439 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004440 }
4441 } else {
4442 thermal_read_mode =
4443 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004444 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004445 }
4446 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004447 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4448 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004449 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004450 } else {
4451 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004452 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004453 }
4454 } else {
4455 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004456 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004457 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004458
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004459 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4460 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4461 thermal_read_mode);
4462
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004463 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004464 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004465 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004466 &thermal_temp_input16_group);
4467 if (res)
4468 return res;
4469 break;
4470 case TPACPI_THERMAL_TPEC_8:
4471 case TPACPI_THERMAL_ACPI_TMP07:
4472 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004473 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004474 &thermal_temp_input8_group);
4475 if (res)
4476 return res;
4477 break;
4478 case TPACPI_THERMAL_NONE:
4479 default:
4480 return 1;
4481 }
4482
4483 return 0;
4484}
4485
4486static void thermal_exit(void)
4487{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004488 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004489 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004490 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004491 &thermal_temp_input16_group);
4492 break;
4493 case TPACPI_THERMAL_TPEC_8:
4494 case TPACPI_THERMAL_ACPI_TMP07:
4495 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004496 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004497 &thermal_temp_input16_group);
4498 break;
4499 case TPACPI_THERMAL_NONE:
4500 default:
4501 break;
4502 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004503}
4504
4505static int thermal_read(char *p)
4506{
4507 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004508 int n, i;
4509 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004510
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004511 n = thermal_get_sensors(&t);
4512 if (unlikely(n < 0))
4513 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004514
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004515 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004516
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004517 if (n > 0) {
4518 for (i = 0; i < (n - 1); i++)
4519 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4520 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4521 } else
4522 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004523
4524 return len;
4525}
4526
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004527static struct ibm_struct thermal_driver_data = {
4528 .name = "thermal",
4529 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004530 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004531};
4532
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004533/*************************************************************************
4534 * EC Dump subdriver
4535 */
4536
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004537static u8 ecdump_regs[256];
4538
4539static int ecdump_read(char *p)
4540{
4541 int len = 0;
4542 int i, j;
4543 u8 v;
4544
4545 len += sprintf(p + len, "EC "
4546 " +00 +01 +02 +03 +04 +05 +06 +07"
4547 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4548 for (i = 0; i < 256; i += 16) {
4549 len += sprintf(p + len, "EC 0x%02x:", i);
4550 for (j = 0; j < 16; j++) {
4551 if (!acpi_ec_read(i + j, &v))
4552 break;
4553 if (v != ecdump_regs[i + j])
4554 len += sprintf(p + len, " *%02x", v);
4555 else
4556 len += sprintf(p + len, " %02x", v);
4557 ecdump_regs[i + j] = v;
4558 }
4559 len += sprintf(p + len, "\n");
4560 if (j != 16)
4561 break;
4562 }
4563
4564 /* These are way too dangerous to advertise openly... */
4565#if 0
4566 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4567 " (<offset> is 00-ff, <value> is 00-ff)\n");
4568 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4569 " (<offset> is 00-ff, <value> is 0-255)\n");
4570#endif
4571 return len;
4572}
4573
4574static int ecdump_write(char *buf)
4575{
4576 char *cmd;
4577 int i, v;
4578
4579 while ((cmd = next_cmd(&buf))) {
4580 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4581 /* i and v set */
4582 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4583 /* i and v set */
4584 } else
4585 return -EINVAL;
4586 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4587 if (!acpi_ec_write(i, v))
4588 return -EIO;
4589 } else
4590 return -EINVAL;
4591 }
4592
4593 return 0;
4594}
4595
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004596static struct ibm_struct ecdump_driver_data = {
4597 .name = "ecdump",
4598 .read = ecdump_read,
4599 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004600 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004601};
4602
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004603/*************************************************************************
4604 * Backlight/brightness subdriver
4605 */
Holger Macht8acb0252006-10-20 14:30:28 -07004606
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004607#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4608
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004609enum {
4610 TP_EC_BACKLIGHT = 0x31,
4611
4612 /* TP_EC_BACKLIGHT bitmasks */
4613 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4614 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4615 TP_EC_BACKLIGHT_MAPSW = 0x20,
4616};
4617
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004618static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004619static int brightness_mode;
4620static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4621
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004622static struct mutex brightness_mutex;
4623
4624/*
4625 * ThinkPads can read brightness from two places: EC 0x31, or
4626 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004627 *
4628 * EC 0x31 has the following layout
4629 * Bit 7: unknown function
4630 * Bit 6: unknown function
4631 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4632 * Bit 4: must be set to zero to avoid problems
4633 * Bit 3-0: backlight brightness level
4634 *
4635 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004636 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004637static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004638{
4639 u8 lec = 0, lcmos = 0, level = 0;
4640
4641 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004642 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004643 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004644 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004645 };
4646 if (brightness_mode & 2) {
4647 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4648 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4649 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4650 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4651 level = lcmos;
4652 }
4653
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004654 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004655 *status = lec; /* Prefer EC, CMOS is just a backing store */
4656 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004657 if (lec == lcmos)
4658 tp_warned.bright_cmos_ec_unsync = 0;
4659 else {
4660 if (!tp_warned.bright_cmos_ec_unsync) {
4661 printk(TPACPI_ERR
4662 "CMOS NVRAM (%u) and EC (%u) do not "
4663 "agree on display brightness level\n",
4664 (unsigned int) lcmos,
4665 (unsigned int) lec);
4666 tp_warned.bright_cmos_ec_unsync = 1;
4667 }
4668 return -EIO;
4669 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004670 } else {
4671 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004672 }
4673
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004674 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004675}
4676
4677/* May return EINTR which can always be mapped to ERESTARTSYS */
4678static int brightness_set(int value)
4679{
4680 int cmos_cmd, inc, i, res;
4681 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004682 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004683
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004684 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4685 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004686 return -EINVAL;
4687
4688 res = mutex_lock_interruptible(&brightness_mutex);
4689 if (res < 0)
4690 return res;
4691
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004692 res = brightness_get_raw(&current_value);
4693 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004694 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004695
4696 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4697 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004698
4699 cmos_cmd = value > current_value ?
4700 TP_CMOS_BRIGHTNESS_UP :
4701 TP_CMOS_BRIGHTNESS_DOWN;
4702 inc = (value > current_value)? 1 : -1;
4703
4704 res = 0;
4705 for (i = current_value; i != value; i += inc) {
4706 if ((brightness_mode & 2) &&
4707 issue_thinkpad_cmos_command(cmos_cmd)) {
4708 res = -EIO;
4709 goto errout;
4710 }
4711 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004712 !acpi_ec_write(TP_EC_BACKLIGHT,
4713 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004714 res = -EIO;
4715 goto errout;;
4716 }
4717 }
4718
4719errout:
4720 mutex_unlock(&brightness_mutex);
4721 return res;
4722}
4723
4724/* sysfs backlight class ----------------------------------------------- */
4725
4726static int brightness_update_status(struct backlight_device *bd)
4727{
4728 /* it is the backlight class's job (caller) to handle
4729 * EINTR and other errors properly */
4730 return brightness_set(
4731 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4732 bd->props.power == FB_BLANK_UNBLANK) ?
4733 bd->props.brightness : 0);
4734}
Holger Macht8acb0252006-10-20 14:30:28 -07004735
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004736static int brightness_get(struct backlight_device *bd)
4737{
4738 int status, res;
4739
4740 res = brightness_get_raw(&status);
4741 if (res < 0)
4742 return 0; /* FIXME: teach backlight about error handling */
4743
4744 return status & TP_EC_BACKLIGHT_LVLMSK;
4745}
4746
Richard Purdie599a52d2007-02-10 23:07:48 +00004747static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004748 .get_brightness = brightness_get,
4749 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004750};
4751
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004752/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004753
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004754static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004755{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004756 int b;
4757
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004758 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4759
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004760 mutex_init(&brightness_mutex);
4761
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004762 /*
4763 * We always attempt to detect acpi support, so as to switch
4764 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4765 * going to publish a backlight interface
4766 */
4767 b = tpacpi_check_std_acpi_brightness_support();
4768 if (b > 0) {
4769 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4770 printk(TPACPI_NOTICE
4771 "Lenovo BIOS switched to ACPI backlight "
4772 "control mode\n");
4773 }
4774 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004775 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004776 "standard ACPI backlight interface "
4777 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004778 return 1;
4779 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004780 }
4781
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004782 if (!brightness_enable) {
4783 dbg_printk(TPACPI_DBG_INIT,
4784 "brightness support disabled by "
4785 "module parameter\n");
4786 return 1;
4787 }
4788
4789 if (b > 16) {
4790 printk(TPACPI_ERR
4791 "Unsupported brightness interface, "
4792 "please contact %s\n", TPACPI_MAIL);
4793 return 1;
4794 }
4795 if (b == 16)
4796 tp_features.bright_16levels = 1;
4797
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004798 if (!brightness_mode) {
4799 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4800 brightness_mode = 2;
4801 else
4802 brightness_mode = 3;
4803
4804 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4805 brightness_mode);
4806 }
4807
4808 if (brightness_mode > 3)
4809 return -EINVAL;
4810
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004811 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004812 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004813
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004814 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004815 printk(TPACPI_INFO
4816 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004817
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004818 ibm_backlight_device = backlight_device_register(
4819 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4820 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004821 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004822 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004823 return PTR_ERR(ibm_backlight_device);
4824 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004825 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004826
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004827 ibm_backlight_device->props.max_brightness =
4828 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004829 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004830 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004831
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004832 return 0;
4833}
4834
4835static void brightness_exit(void)
4836{
4837 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004838 vdbg_printk(TPACPI_DBG_EXIT,
4839 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004840 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004841 }
4842}
4843
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004844static int brightness_read(char *p)
4845{
4846 int len = 0;
4847 int level;
4848
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004849 level = brightness_get(NULL);
4850 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004851 len += sprintf(p + len, "level:\t\tunreadable\n");
4852 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004853 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004854 len += sprintf(p + len, "commands:\tup, down\n");
4855 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004856 " (<level> is 0-%d)\n",
4857 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004858 }
4859
4860 return len;
4861}
4862
4863static int brightness_write(char *buf)
4864{
4865 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004866 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004867 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004868 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004869
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004870 level = brightness_get(NULL);
4871 if (level < 0)
4872 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004873
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004874 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004875 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004876 if (level < max_level)
4877 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004878 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004879 if (level > 0)
4880 level--;
4881 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4882 level >= 0 && level <= max_level) {
4883 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004884 } else
4885 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004886 }
4887
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004888 /*
4889 * Now we know what the final level should be, so we try to set it.
4890 * Doing it this way makes the syscall restartable in case of EINTR
4891 */
4892 rc = brightness_set(level);
4893 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004894}
4895
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004896static struct ibm_struct brightness_driver_data = {
4897 .name = "brightness",
4898 .read = brightness_read,
4899 .write = brightness_write,
4900 .exit = brightness_exit,
4901};
4902
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004903/*************************************************************************
4904 * Volume subdriver
4905 */
4906
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004907static int volume_offset = 0x30;
4908
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004909static int volume_read(char *p)
4910{
4911 int len = 0;
4912 u8 level;
4913
4914 if (!acpi_ec_read(volume_offset, &level)) {
4915 len += sprintf(p + len, "level:\t\tunreadable\n");
4916 } else {
4917 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4918 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4919 len += sprintf(p + len, "commands:\tup, down, mute\n");
4920 len += sprintf(p + len, "commands:\tlevel <level>"
4921 " (<level> is 0-15)\n");
4922 }
4923
4924 return len;
4925}
4926
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004927static int volume_write(char *buf)
4928{
4929 int cmos_cmd, inc, i;
4930 u8 level, mute;
4931 int new_level, new_mute;
4932 char *cmd;
4933
4934 while ((cmd = next_cmd(&buf))) {
4935 if (!acpi_ec_read(volume_offset, &level))
4936 return -EIO;
4937 new_mute = mute = level & 0x40;
4938 new_level = level = level & 0xf;
4939
4940 if (strlencmp(cmd, "up") == 0) {
4941 if (mute)
4942 new_mute = 0;
4943 else
4944 new_level = level == 15 ? 15 : level + 1;
4945 } else if (strlencmp(cmd, "down") == 0) {
4946 if (mute)
4947 new_mute = 0;
4948 else
4949 new_level = level == 0 ? 0 : level - 1;
4950 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4951 new_level >= 0 && new_level <= 15) {
4952 /* new_level set */
4953 } else if (strlencmp(cmd, "mute") == 0) {
4954 new_mute = 0x40;
4955 } else
4956 return -EINVAL;
4957
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004958 if (new_level != level) {
4959 /* mute doesn't change */
4960
4961 cmos_cmd = (new_level > level) ?
4962 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004963 inc = new_level > level ? 1 : -1;
4964
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004965 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004966 !acpi_ec_write(volume_offset, level)))
4967 return -EIO;
4968
4969 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004970 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004971 !acpi_ec_write(volume_offset, i + inc))
4972 return -EIO;
4973
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004974 if (mute &&
4975 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4976 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004977 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004978 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004979 }
4980
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004981 if (new_mute != mute) {
4982 /* level doesn't change */
4983
4984 cmos_cmd = (new_mute) ?
4985 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004986
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004987 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004988 !acpi_ec_write(volume_offset, level + new_mute))
4989 return -EIO;
4990 }
4991 }
4992
4993 return 0;
4994}
4995
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004996static struct ibm_struct volume_driver_data = {
4997 .name = "volume",
4998 .read = volume_read,
4999 .write = volume_write,
5000};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005001
5002/*************************************************************************
5003 * Fan subdriver
5004 */
5005
5006/*
5007 * FAN ACCESS MODES
5008 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005009 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005010 * ACPI GFAN method: returns fan level
5011 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005012 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005013 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005014 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005015 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005016 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5017 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005018 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5019 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005020 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005021 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005022 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5023 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005024 *
5025 * Fan speed changes of any sort (including those caused by the
5026 * disengaged mode) are usually done slowly by the firmware as the
5027 * maximum ammount of fan duty cycle change per second seems to be
5028 * limited.
5029 *
5030 * Reading is not available if GFAN exists.
5031 * Writing is not available if SFAN exists.
5032 *
5033 * Bits
5034 * 7 automatic mode engaged;
5035 * (default operation mode of the ThinkPad)
5036 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005037 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005038 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005039 * the tachometer while the fan controller ramps up
5040 * the speed (which can take up to a few *minutes*).
5041 * Speeds up fan to 100% duty-cycle, which is far above
5042 * the standard RPM levels. It is not impossible that
5043 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005044 * 5-3 unused in some models. Extra bits for fan level
5045 * in others, but still useless as all values above
5046 * 7 map to the same speed as level 7 in these models.
5047 * 2-0 fan level (0..7 usually)
5048 * 0x00 = stop
5049 * 0x07 = max (set when temperatures critical)
5050 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005051 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005052 *
5053 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5054 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5055 * does so, its initial value is meaningless (0x07).
5056 *
5057 * For firmware bugs, refer to:
5058 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5059 *
5060 * ----
5061 *
5062 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5063 * Main fan tachometer reading (in RPM)
5064 *
5065 * This register is present on all ThinkPads with a new-style EC, and
5066 * it is known not to be present on the A21m/e, and T22, as there is
5067 * something else in offset 0x84 according to the ACPI DSDT. Other
5068 * ThinkPads from this same time period (and earlier) probably lack the
5069 * tachometer as well.
5070 *
5071 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5072 * was never fixed by IBM to report the EC firmware version string
5073 * probably support the tachometer (like the early X models), so
5074 * detecting it is quite hard. We need more data to know for sure.
5075 *
5076 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5077 * might result.
5078 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005079 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5080 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005081 *
5082 * For firmware bugs, refer to:
5083 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5084 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005085 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005086 * ThinkPad X31, X40, X41. Not available in the X60.
5087 *
5088 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5089 * high speed. ACPI DSDT seems to map these three speeds to levels
5090 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5091 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5092 *
5093 * The speeds are stored on handles
5094 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5095 *
5096 * There are three default speed sets, acessible as handles:
5097 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5098 *
5099 * ACPI DSDT switches which set is in use depending on various
5100 * factors.
5101 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005102 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005103 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5104 * but the ACPI tables just mention level 7.
5105 */
5106
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005107enum { /* Fan control constants */
5108 fan_status_offset = 0x2f, /* EC register 0x2f */
5109 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5110 * 0x84 must be read before 0x85 */
5111
5112 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5113 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5114
5115 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5116};
5117
5118enum fan_status_access_mode {
5119 TPACPI_FAN_NONE = 0, /* No fan status or control */
5120 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5121 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5122};
5123
5124enum fan_control_access_mode {
5125 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5126 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5127 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5128 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5129};
5130
5131enum fan_control_commands {
5132 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5133 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5134 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5135 * and also watchdog cmd */
5136};
5137
5138static int fan_control_allowed;
5139
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005140static enum fan_status_access_mode fan_status_access_mode;
5141static enum fan_control_access_mode fan_control_access_mode;
5142static enum fan_control_commands fan_control_commands;
5143
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005144static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005145static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005146static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005147
5148static struct mutex fan_mutex;
5149
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005150static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005151static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005152
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005153TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5154TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005155 "\\FSPD", /* 600e/x, 770e, 770x */
5156 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005157TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005158 "JFNS", /* 770x-JL */
5159 ); /* all others */
5160
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005161/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005162 * Call with fan_mutex held
5163 */
5164static void fan_update_desired_level(u8 status)
5165{
5166 if ((status &
5167 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5168 if (status > 7)
5169 fan_control_desired_level = 7;
5170 else
5171 fan_control_desired_level = status;
5172 }
5173}
5174
5175static int fan_get_status(u8 *status)
5176{
5177 u8 s;
5178
5179 /* TODO:
5180 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5181
5182 switch (fan_status_access_mode) {
5183 case TPACPI_FAN_RD_ACPI_GFAN:
5184 /* 570, 600e/x, 770e, 770x */
5185
5186 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5187 return -EIO;
5188
5189 if (likely(status))
5190 *status = s & 0x07;
5191
5192 break;
5193
5194 case TPACPI_FAN_RD_TPEC:
5195 /* all except 570, 600e/x, 770e, 770x */
5196 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5197 return -EIO;
5198
5199 if (likely(status))
5200 *status = s;
5201
5202 break;
5203
5204 default:
5205 return -ENXIO;
5206 }
5207
5208 return 0;
5209}
5210
5211static int fan_get_status_safe(u8 *status)
5212{
5213 int rc;
5214 u8 s;
5215
5216 if (mutex_lock_interruptible(&fan_mutex))
5217 return -ERESTARTSYS;
5218 rc = fan_get_status(&s);
5219 if (!rc)
5220 fan_update_desired_level(s);
5221 mutex_unlock(&fan_mutex);
5222
5223 if (status)
5224 *status = s;
5225
5226 return rc;
5227}
5228
5229static int fan_get_speed(unsigned int *speed)
5230{
5231 u8 hi, lo;
5232
5233 switch (fan_status_access_mode) {
5234 case TPACPI_FAN_RD_TPEC:
5235 /* all except 570, 600e/x, 770e, 770x */
5236 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5237 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5238 return -EIO;
5239
5240 if (likely(speed))
5241 *speed = (hi << 8) | lo;
5242
5243 break;
5244
5245 default:
5246 return -ENXIO;
5247 }
5248
5249 return 0;
5250}
5251
5252static int fan_set_level(int level)
5253{
5254 if (!fan_control_allowed)
5255 return -EPERM;
5256
5257 switch (fan_control_access_mode) {
5258 case TPACPI_FAN_WR_ACPI_SFAN:
5259 if (level >= 0 && level <= 7) {
5260 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5261 return -EIO;
5262 } else
5263 return -EINVAL;
5264 break;
5265
5266 case TPACPI_FAN_WR_ACPI_FANS:
5267 case TPACPI_FAN_WR_TPEC:
5268 if ((level != TP_EC_FAN_AUTO) &&
5269 (level != TP_EC_FAN_FULLSPEED) &&
5270 ((level < 0) || (level > 7)))
5271 return -EINVAL;
5272
5273 /* safety net should the EC not support AUTO
5274 * or FULLSPEED mode bits and just ignore them */
5275 if (level & TP_EC_FAN_FULLSPEED)
5276 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005277 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005278 level |= 4; /* safety min speed 4 */
5279
5280 if (!acpi_ec_write(fan_status_offset, level))
5281 return -EIO;
5282 else
5283 tp_features.fan_ctrl_status_undef = 0;
5284 break;
5285
5286 default:
5287 return -ENXIO;
5288 }
5289 return 0;
5290}
5291
5292static int fan_set_level_safe(int level)
5293{
5294 int rc;
5295
5296 if (!fan_control_allowed)
5297 return -EPERM;
5298
5299 if (mutex_lock_interruptible(&fan_mutex))
5300 return -ERESTARTSYS;
5301
5302 if (level == TPACPI_FAN_LAST_LEVEL)
5303 level = fan_control_desired_level;
5304
5305 rc = fan_set_level(level);
5306 if (!rc)
5307 fan_update_desired_level(level);
5308
5309 mutex_unlock(&fan_mutex);
5310 return rc;
5311}
5312
5313static int fan_set_enable(void)
5314{
5315 u8 s;
5316 int rc;
5317
5318 if (!fan_control_allowed)
5319 return -EPERM;
5320
5321 if (mutex_lock_interruptible(&fan_mutex))
5322 return -ERESTARTSYS;
5323
5324 switch (fan_control_access_mode) {
5325 case TPACPI_FAN_WR_ACPI_FANS:
5326 case TPACPI_FAN_WR_TPEC:
5327 rc = fan_get_status(&s);
5328 if (rc < 0)
5329 break;
5330
5331 /* Don't go out of emergency fan mode */
5332 if (s != 7) {
5333 s &= 0x07;
5334 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5335 }
5336
5337 if (!acpi_ec_write(fan_status_offset, s))
5338 rc = -EIO;
5339 else {
5340 tp_features.fan_ctrl_status_undef = 0;
5341 rc = 0;
5342 }
5343 break;
5344
5345 case TPACPI_FAN_WR_ACPI_SFAN:
5346 rc = fan_get_status(&s);
5347 if (rc < 0)
5348 break;
5349
5350 s &= 0x07;
5351
5352 /* Set fan to at least level 4 */
5353 s |= 4;
5354
5355 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005356 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005357 else
5358 rc = 0;
5359 break;
5360
5361 default:
5362 rc = -ENXIO;
5363 }
5364
5365 mutex_unlock(&fan_mutex);
5366 return rc;
5367}
5368
5369static int fan_set_disable(void)
5370{
5371 int rc;
5372
5373 if (!fan_control_allowed)
5374 return -EPERM;
5375
5376 if (mutex_lock_interruptible(&fan_mutex))
5377 return -ERESTARTSYS;
5378
5379 rc = 0;
5380 switch (fan_control_access_mode) {
5381 case TPACPI_FAN_WR_ACPI_FANS:
5382 case TPACPI_FAN_WR_TPEC:
5383 if (!acpi_ec_write(fan_status_offset, 0x00))
5384 rc = -EIO;
5385 else {
5386 fan_control_desired_level = 0;
5387 tp_features.fan_ctrl_status_undef = 0;
5388 }
5389 break;
5390
5391 case TPACPI_FAN_WR_ACPI_SFAN:
5392 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5393 rc = -EIO;
5394 else
5395 fan_control_desired_level = 0;
5396 break;
5397
5398 default:
5399 rc = -ENXIO;
5400 }
5401
5402
5403 mutex_unlock(&fan_mutex);
5404 return rc;
5405}
5406
5407static int fan_set_speed(int speed)
5408{
5409 int rc;
5410
5411 if (!fan_control_allowed)
5412 return -EPERM;
5413
5414 if (mutex_lock_interruptible(&fan_mutex))
5415 return -ERESTARTSYS;
5416
5417 rc = 0;
5418 switch (fan_control_access_mode) {
5419 case TPACPI_FAN_WR_ACPI_FANS:
5420 if (speed >= 0 && speed <= 65535) {
5421 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5422 speed, speed, speed))
5423 rc = -EIO;
5424 } else
5425 rc = -EINVAL;
5426 break;
5427
5428 default:
5429 rc = -ENXIO;
5430 }
5431
5432 mutex_unlock(&fan_mutex);
5433 return rc;
5434}
5435
5436static void fan_watchdog_reset(void)
5437{
5438 static int fan_watchdog_active;
5439
5440 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5441 return;
5442
5443 if (fan_watchdog_active)
5444 cancel_delayed_work(&fan_watchdog_task);
5445
5446 if (fan_watchdog_maxinterval > 0 &&
5447 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5448 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005449 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005450 msecs_to_jiffies(fan_watchdog_maxinterval
5451 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005452 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005453 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005454 "watchdog will not trigger\n");
5455 }
5456 } else
5457 fan_watchdog_active = 0;
5458}
5459
5460static void fan_watchdog_fire(struct work_struct *ignored)
5461{
5462 int rc;
5463
5464 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5465 return;
5466
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005467 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005468 rc = fan_set_enable();
5469 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005470 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005471 "will try again later...\n", -rc);
5472 /* reschedule for later */
5473 fan_watchdog_reset();
5474 }
5475}
5476
5477/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005478 * SYSFS fan layout: hwmon compatible (device)
5479 *
5480 * pwm*_enable:
5481 * 0: "disengaged" mode
5482 * 1: manual mode
5483 * 2: native EC "auto" mode (recommended, hardware default)
5484 *
5485 * pwm*: set speed in manual mode, ignored otherwise.
5486 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5487 * interpolation.
5488 *
5489 * fan*_input: tachometer reading, RPM
5490 *
5491 *
5492 * SYSFS fan layout: extensions
5493 *
5494 * fan_watchdog (driver):
5495 * fan watchdog interval in seconds, 0 disables (default), max 120
5496 */
5497
5498/* sysfs fan pwm1_enable ----------------------------------------------- */
5499static ssize_t fan_pwm1_enable_show(struct device *dev,
5500 struct device_attribute *attr,
5501 char *buf)
5502{
5503 int res, mode;
5504 u8 status;
5505
5506 res = fan_get_status_safe(&status);
5507 if (res)
5508 return res;
5509
5510 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5511 if (status != fan_control_initial_status) {
5512 tp_features.fan_ctrl_status_undef = 0;
5513 } else {
5514 /* Return most likely status. In fact, it
5515 * might be the only possible status */
5516 status = TP_EC_FAN_AUTO;
5517 }
5518 }
5519
5520 if (status & TP_EC_FAN_FULLSPEED) {
5521 mode = 0;
5522 } else if (status & TP_EC_FAN_AUTO) {
5523 mode = 2;
5524 } else
5525 mode = 1;
5526
5527 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5528}
5529
5530static ssize_t fan_pwm1_enable_store(struct device *dev,
5531 struct device_attribute *attr,
5532 const char *buf, size_t count)
5533{
5534 unsigned long t;
5535 int res, level;
5536
5537 if (parse_strtoul(buf, 2, &t))
5538 return -EINVAL;
5539
5540 switch (t) {
5541 case 0:
5542 level = TP_EC_FAN_FULLSPEED;
5543 break;
5544 case 1:
5545 level = TPACPI_FAN_LAST_LEVEL;
5546 break;
5547 case 2:
5548 level = TP_EC_FAN_AUTO;
5549 break;
5550 case 3:
5551 /* reserved for software-controlled auto mode */
5552 return -ENOSYS;
5553 default:
5554 return -EINVAL;
5555 }
5556
5557 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005558 if (res == -ENXIO)
5559 return -EINVAL;
5560 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005561 return res;
5562
5563 fan_watchdog_reset();
5564
5565 return count;
5566}
5567
5568static struct device_attribute dev_attr_fan_pwm1_enable =
5569 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5570 fan_pwm1_enable_show, fan_pwm1_enable_store);
5571
5572/* sysfs fan pwm1 ------------------------------------------------------ */
5573static ssize_t fan_pwm1_show(struct device *dev,
5574 struct device_attribute *attr,
5575 char *buf)
5576{
5577 int res;
5578 u8 status;
5579
5580 res = fan_get_status_safe(&status);
5581 if (res)
5582 return res;
5583
5584 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5585 if (status != fan_control_initial_status) {
5586 tp_features.fan_ctrl_status_undef = 0;
5587 } else {
5588 status = TP_EC_FAN_AUTO;
5589 }
5590 }
5591
5592 if ((status &
5593 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5594 status = fan_control_desired_level;
5595
5596 if (status > 7)
5597 status = 7;
5598
5599 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5600}
5601
5602static ssize_t fan_pwm1_store(struct device *dev,
5603 struct device_attribute *attr,
5604 const char *buf, size_t count)
5605{
5606 unsigned long s;
5607 int rc;
5608 u8 status, newlevel;
5609
5610 if (parse_strtoul(buf, 255, &s))
5611 return -EINVAL;
5612
5613 /* scale down from 0-255 to 0-7 */
5614 newlevel = (s >> 5) & 0x07;
5615
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005616 if (mutex_lock_interruptible(&fan_mutex))
5617 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005618
5619 rc = fan_get_status(&status);
5620 if (!rc && (status &
5621 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5622 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005623 if (rc == -ENXIO)
5624 rc = -EINVAL;
5625 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005626 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005627 fan_watchdog_reset();
5628 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005629 }
5630
5631 mutex_unlock(&fan_mutex);
5632 return (rc)? rc : count;
5633}
5634
5635static struct device_attribute dev_attr_fan_pwm1 =
5636 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5637 fan_pwm1_show, fan_pwm1_store);
5638
5639/* sysfs fan fan1_input ------------------------------------------------ */
5640static ssize_t fan_fan1_input_show(struct device *dev,
5641 struct device_attribute *attr,
5642 char *buf)
5643{
5644 int res;
5645 unsigned int speed;
5646
5647 res = fan_get_speed(&speed);
5648 if (res < 0)
5649 return res;
5650
5651 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5652}
5653
5654static struct device_attribute dev_attr_fan_fan1_input =
5655 __ATTR(fan1_input, S_IRUGO,
5656 fan_fan1_input_show, NULL);
5657
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005658/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005659static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5660 char *buf)
5661{
5662 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5663}
5664
5665static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5666 const char *buf, size_t count)
5667{
5668 unsigned long t;
5669
5670 if (parse_strtoul(buf, 120, &t))
5671 return -EINVAL;
5672
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005673 if (!fan_control_allowed)
5674 return -EPERM;
5675
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005676 fan_watchdog_maxinterval = t;
5677 fan_watchdog_reset();
5678
5679 return count;
5680}
5681
5682static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5683 fan_fan_watchdog_show, fan_fan_watchdog_store);
5684
5685/* --------------------------------------------------------------------- */
5686static struct attribute *fan_attributes[] = {
5687 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5688 &dev_attr_fan_fan1_input.attr,
5689 NULL
5690};
5691
5692static const struct attribute_group fan_attr_group = {
5693 .attrs = fan_attributes,
5694};
5695
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005696static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005697{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005698 int rc;
5699
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005700 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5701
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005702 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005703 fan_status_access_mode = TPACPI_FAN_NONE;
5704 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005705 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005706 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005707 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005708 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005709
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005710 TPACPI_ACPIHANDLE_INIT(fans);
5711 TPACPI_ACPIHANDLE_INIT(gfan);
5712 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005713
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005714 if (gfan_handle) {
5715 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005716 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005717 } else {
5718 /* all other ThinkPads: note that even old-style
5719 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005720 if (likely(acpi_ec_read(fan_status_offset,
5721 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005722 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005723
5724 /* In some ThinkPads, neither the EC nor the ACPI
5725 * DSDT initialize the fan status, and it ends up
5726 * being set to 0x07 when it *could* be either
5727 * 0x07 or 0x80.
5728 *
5729 * Enable for TP-1Y (T43), TP-78 (R51e),
5730 * TP-76 (R52), TP-70 (T43, R52), which are known
5731 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005732 if (fan_control_initial_status == 0x07) {
5733 switch (thinkpad_id.ec_model) {
5734 case 0x5931: /* TP-1Y */
5735 case 0x3837: /* TP-78 */
5736 case 0x3637: /* TP-76 */
5737 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005738 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005739 "fan_init: initial fan status "
5740 "is unknown, assuming it is "
5741 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005742 tp_features.fan_ctrl_status_undef = 1;
5743 ;;
5744 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005745 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005746 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005747 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005748 "ThinkPad ACPI EC access misbehaving, "
5749 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005750 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005751 }
5752 }
5753
5754 if (sfan_handle) {
5755 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005756 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005757 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005758 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005759 } else {
5760 if (!gfan_handle) {
5761 /* gfan without sfan means no fan control */
5762 /* all other models implement TP EC 0x2f control */
5763
5764 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005765 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005766 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005767 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005768 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005769 TPACPI_FAN_CMD_SPEED |
5770 TPACPI_FAN_CMD_LEVEL |
5771 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005772 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005773 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005774 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005775 TPACPI_FAN_CMD_LEVEL |
5776 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005777 }
5778 }
5779 }
5780
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005781 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5782 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5783 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5784 fan_status_access_mode, fan_control_access_mode);
5785
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005786 /* fan control master switch */
5787 if (!fan_control_allowed) {
5788 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5789 fan_control_commands = 0;
5790 dbg_printk(TPACPI_DBG_INIT,
5791 "fan control features disabled by parameter\n");
5792 }
5793
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005794 /* update fan_control_desired_level */
5795 if (fan_status_access_mode != TPACPI_FAN_NONE)
5796 fan_get_status_safe(NULL);
5797
5798 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5799 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005800 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005801 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005802 if (rc < 0)
5803 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005804
5805 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5806 &driver_attr_fan_watchdog);
5807 if (rc < 0) {
5808 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5809 &fan_attr_group);
5810 return rc;
5811 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005812 return 0;
5813 } else
5814 return 1;
5815}
5816
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005817static void fan_exit(void)
5818{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005819 vdbg_printk(TPACPI_DBG_EXIT,
5820 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005821
5822 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005823 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005824 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5825 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005826
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005827 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005828 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005829}
5830
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005831static int fan_read(char *p)
5832{
5833 int len = 0;
5834 int rc;
5835 u8 status;
5836 unsigned int speed = 0;
5837
5838 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005839 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005840 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005841 rc = fan_get_status_safe(&status);
5842 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005843 return rc;
5844
5845 len += sprintf(p + len, "status:\t\t%s\n"
5846 "level:\t\t%d\n",
5847 (status != 0) ? "enabled" : "disabled", status);
5848 break;
5849
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005850 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005851 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005852 rc = fan_get_status_safe(&status);
5853 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005854 return rc;
5855
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005856 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005857 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005858 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005859 else
5860 /* Return most likely status. In fact, it
5861 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005862 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005863 }
5864
5865 len += sprintf(p + len, "status:\t\t%s\n",
5866 (status != 0) ? "enabled" : "disabled");
5867
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005868 rc = fan_get_speed(&speed);
5869 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005870 return rc;
5871
5872 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5873
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005874 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005875 /* Disengaged mode takes precedence */
5876 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005877 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005878 len += sprintf(p + len, "level:\t\tauto\n");
5879 else
5880 len += sprintf(p + len, "level:\t\t%d\n", status);
5881 break;
5882
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005883 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005884 default:
5885 len += sprintf(p + len, "status:\t\tnot supported\n");
5886 }
5887
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005888 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005889 len += sprintf(p + len, "commands:\tlevel <level>");
5890
5891 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005892 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005893 len += sprintf(p + len, " (<level> is 0-7)\n");
5894 break;
5895
5896 default:
5897 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005898 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005899 break;
5900 }
5901 }
5902
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005903 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005904 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005905 "commands:\twatchdog <timeout> (<timeout> "
5906 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005907
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005908 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005909 len += sprintf(p + len, "commands:\tspeed <speed>"
5910 " (<speed> is 0-65535)\n");
5911
5912 return len;
5913}
5914
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005915static int fan_write_cmd_level(const char *cmd, int *rc)
5916{
5917 int level;
5918
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005919 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005920 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005921 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005922 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005923 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005924 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005925 return 0;
5926
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005927 *rc = fan_set_level_safe(level);
5928 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005929 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005930 "access mode %d", fan_control_access_mode);
5931
5932 return 1;
5933}
5934
5935static int fan_write_cmd_enable(const char *cmd, int *rc)
5936{
5937 if (strlencmp(cmd, "enable") != 0)
5938 return 0;
5939
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005940 *rc = fan_set_enable();
5941 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005942 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005943 "access mode %d", fan_control_access_mode);
5944
5945 return 1;
5946}
5947
5948static int fan_write_cmd_disable(const char *cmd, int *rc)
5949{
5950 if (strlencmp(cmd, "disable") != 0)
5951 return 0;
5952
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005953 *rc = fan_set_disable();
5954 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005955 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005956 "access mode %d", fan_control_access_mode);
5957
5958 return 1;
5959}
5960
5961static int fan_write_cmd_speed(const char *cmd, int *rc)
5962{
5963 int speed;
5964
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005965 /* TODO:
5966 * Support speed <low> <medium> <high> ? */
5967
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005968 if (sscanf(cmd, "speed %d", &speed) != 1)
5969 return 0;
5970
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005971 *rc = fan_set_speed(speed);
5972 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005973 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005974 "access mode %d", fan_control_access_mode);
5975
5976 return 1;
5977}
5978
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005979static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5980{
5981 int interval;
5982
5983 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5984 return 0;
5985
5986 if (interval < 0 || interval > 120)
5987 *rc = -EINVAL;
5988 else
5989 fan_watchdog_maxinterval = interval;
5990
5991 return 1;
5992}
5993
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005994static int fan_write(char *buf)
5995{
5996 char *cmd;
5997 int rc = 0;
5998
5999 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006000 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006001 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006002 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006003 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006004 fan_write_cmd_disable(cmd, &rc) ||
6005 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006006 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006007 fan_write_cmd_speed(cmd, &rc))
6008 )
6009 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006010 else if (!rc)
6011 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006012 }
6013
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006014 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006015}
6016
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006017static struct ibm_struct fan_driver_data = {
6018 .name = "fan",
6019 .read = fan_read,
6020 .write = fan_write,
6021 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006022};
6023
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006024/****************************************************************************
6025 ****************************************************************************
6026 *
6027 * Infrastructure
6028 *
6029 ****************************************************************************
6030 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006032/* sysfs name ---------------------------------------------------------- */
6033static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6034 struct device_attribute *attr,
6035 char *buf)
6036{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006037 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006038}
6039
6040static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6041 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6042
6043/* --------------------------------------------------------------------- */
6044
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006045/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006046static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006047
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006048/*
6049 * Module and infrastructure proble, init and exit handling
6050 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006052static int force_load;
6053
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006054#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006055static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006056{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006057 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006058
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006059 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006060}
6061#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6062
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006063static void ibm_exit(struct ibm_struct *ibm)
6064{
6065 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6066
6067 list_del_init(&ibm->all_drivers);
6068
6069 if (ibm->flags.acpi_notify_installed) {
6070 dbg_printk(TPACPI_DBG_EXIT,
6071 "%s: acpi_remove_notify_handler\n", ibm->name);
6072 BUG_ON(!ibm->acpi);
6073 acpi_remove_notify_handler(*ibm->acpi->handle,
6074 ibm->acpi->type,
6075 dispatch_acpi_notify);
6076 ibm->flags.acpi_notify_installed = 0;
6077 ibm->flags.acpi_notify_installed = 0;
6078 }
6079
6080 if (ibm->flags.proc_created) {
6081 dbg_printk(TPACPI_DBG_EXIT,
6082 "%s: remove_proc_entry\n", ibm->name);
6083 remove_proc_entry(ibm->name, proc_dir);
6084 ibm->flags.proc_created = 0;
6085 }
6086
6087 if (ibm->flags.acpi_driver_registered) {
6088 dbg_printk(TPACPI_DBG_EXIT,
6089 "%s: acpi_bus_unregister_driver\n", ibm->name);
6090 BUG_ON(!ibm->acpi);
6091 acpi_bus_unregister_driver(ibm->acpi->driver);
6092 kfree(ibm->acpi->driver);
6093 ibm->acpi->driver = NULL;
6094 ibm->flags.acpi_driver_registered = 0;
6095 }
6096
6097 if (ibm->flags.init_called && ibm->exit) {
6098 ibm->exit();
6099 ibm->flags.init_called = 0;
6100 }
6101
6102 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6103}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006104
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006105static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106{
6107 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006108 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 struct proc_dir_entry *entry;
6110
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006111 BUG_ON(ibm == NULL);
6112
6113 INIT_LIST_HEAD(&ibm->all_drivers);
6114
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006115 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 return 0;
6117
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006118 dbg_printk(TPACPI_DBG_INIT,
6119 "probing for %s\n", ibm->name);
6120
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006121 if (iibm->init) {
6122 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006123 if (ret > 0)
6124 return 0; /* probe failed */
6125 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006127
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006128 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 }
6130
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006131 if (ibm->acpi) {
6132 if (ibm->acpi->hid) {
6133 ret = register_tpacpi_subdriver(ibm);
6134 if (ret)
6135 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006136 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006137
6138 if (ibm->acpi->notify) {
6139 ret = setup_acpi_notify(ibm);
6140 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006141 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006142 ibm->name);
6143 ret = 0;
6144 goto err_out;
6145 }
6146 if (ret < 0)
6147 goto err_out;
6148 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006149 }
6150
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006151 dbg_printk(TPACPI_DBG_INIT,
6152 "%s installed\n", ibm->name);
6153
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006154 if (ibm->read) {
6155 entry = create_proc_entry(ibm->name,
6156 S_IFREG | S_IRUGO | S_IWUSR,
6157 proc_dir);
6158 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006159 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006160 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006161 ret = -ENODEV;
6162 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006163 }
6164 entry->owner = THIS_MODULE;
6165 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006166 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006167 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006168 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006169 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006172 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6173
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006175
6176err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006177 dbg_printk(TPACPI_DBG_INIT,
6178 "%s: at error exit path with result %d\n",
6179 ibm->name, ret);
6180
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006181 ibm_exit(ibm);
6182 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183}
6184
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006185/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006187static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006188{
Jeff Garzik18552562007-10-03 15:15:40 -04006189 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006190 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006191
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006192 if (!tp)
6193 return;
6194
6195 memset(tp, 0, sizeof(*tp));
6196
6197 if (dmi_name_in_vendors("IBM"))
6198 tp->vendor = PCI_VENDOR_ID_IBM;
6199 else if (dmi_name_in_vendors("LENOVO"))
6200 tp->vendor = PCI_VENDOR_ID_LENOVO;
6201 else
6202 return;
6203
6204 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
6205 GFP_KERNEL);
6206 if (!tp->bios_version_str)
6207 return;
6208 tp->bios_model = tp->bios_version_str[0]
6209 | (tp->bios_version_str[1] << 8);
6210
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006211 /*
6212 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6213 * X32 or newer, all Z series; Some models must have an
6214 * up-to-date BIOS or they will not be detected.
6215 *
6216 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6217 */
6218 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006219 if (sscanf(dev->name,
6220 "IBM ThinkPad Embedded Controller -[%17c",
6221 ec_fw_string) == 1) {
6222 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6223 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006224
6225 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6226 tp->ec_model = ec_fw_string[0]
6227 | (ec_fw_string[1] << 8);
6228 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006229 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006230 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006231
6232 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6233 GFP_KERNEL);
Cyrill Gorcunov90fe17f2008-04-18 13:27:29 -07006234 if (tp->model_str && strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006235 kfree(tp->model_str);
6236 tp->model_str = NULL;
6237 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006238
6239 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6240 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241}
6242
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006243static int __init probe_for_thinkpad(void)
6244{
6245 int is_thinkpad;
6246
6247 if (acpi_disabled)
6248 return -ENODEV;
6249
6250 /*
6251 * Non-ancient models have better DMI tagging, but very old models
6252 * don't.
6253 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006254 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006255
6256 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006257 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006258 if (!ec_handle) {
6259 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006260 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006261 "Not yet supported ThinkPad detected!\n");
6262 return -ENODEV;
6263 }
6264
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006265 /*
6266 * Risks a regression on very old machines, but reduces potential
6267 * false positives a damn great deal
6268 */
6269 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006270 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006271
6272 if (!is_thinkpad && !force_load)
6273 return -ENODEV;
6274
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006275 return 0;
6276}
6277
6278
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006279/* Module init, exit, parameters */
6280
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006281static struct ibm_init_struct ibms_init[] __initdata = {
6282 {
6283 .init = thinkpad_acpi_driver_init,
6284 .data = &thinkpad_acpi_driver_data,
6285 },
6286 {
6287 .init = hotkey_init,
6288 .data = &hotkey_driver_data,
6289 },
6290 {
6291 .init = bluetooth_init,
6292 .data = &bluetooth_driver_data,
6293 },
6294 {
6295 .init = wan_init,
6296 .data = &wan_driver_data,
6297 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006298#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006299 {
6300 .init = video_init,
6301 .data = &video_driver_data,
6302 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006303#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006304 {
6305 .init = light_init,
6306 .data = &light_driver_data,
6307 },
6308#ifdef CONFIG_THINKPAD_ACPI_DOCK
6309 {
6310 .init = dock_init,
6311 .data = &dock_driver_data[0],
6312 },
6313 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03006314 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006315 .data = &dock_driver_data[1],
6316 },
6317#endif
6318#ifdef CONFIG_THINKPAD_ACPI_BAY
6319 {
6320 .init = bay_init,
6321 .data = &bay_driver_data,
6322 },
6323#endif
6324 {
6325 .init = cmos_init,
6326 .data = &cmos_driver_data,
6327 },
6328 {
6329 .init = led_init,
6330 .data = &led_driver_data,
6331 },
6332 {
6333 .init = beep_init,
6334 .data = &beep_driver_data,
6335 },
6336 {
6337 .init = thermal_init,
6338 .data = &thermal_driver_data,
6339 },
6340 {
6341 .data = &ecdump_driver_data,
6342 },
6343 {
6344 .init = brightness_init,
6345 .data = &brightness_driver_data,
6346 },
6347 {
6348 .data = &volume_driver_data,
6349 },
6350 {
6351 .init = fan_init,
6352 .data = &fan_driver_data,
6353 },
6354};
6355
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006356static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6357{
6358 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006359 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006360
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006361 if (!kp || !kp->name || !val)
6362 return -EINVAL;
6363
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006364 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6365 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006366 WARN_ON(ibm == NULL);
6367
6368 if (!ibm || !ibm->name)
6369 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006370
6371 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6372 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006373 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006374 strcpy(ibms_init[i].param, val);
6375 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006376 return 0;
6377 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006378 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006379
6380 return -EINVAL;
6381}
6382
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006383module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006384MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006385 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006386
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006387module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006388MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006389
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006390module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006391MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006392 "Attempts to load the driver even on a "
6393 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006394
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006395module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006396MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006397 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006398
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006399module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006400MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006401 "Selects brightness control strategy: "
6402 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006403
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006404module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006405MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006406 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006407
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006408module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006409MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006410 "used for backwards compatibility with userspace, "
6411 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006412
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006413#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006414 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006415 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006416 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006417
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006418TPACPI_PARAM(hotkey);
6419TPACPI_PARAM(bluetooth);
6420TPACPI_PARAM(video);
6421TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006422#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006423TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006424#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006425#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006426TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006427#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006428TPACPI_PARAM(cmos);
6429TPACPI_PARAM(led);
6430TPACPI_PARAM(beep);
6431TPACPI_PARAM(ecdump);
6432TPACPI_PARAM(brightness);
6433TPACPI_PARAM(volume);
6434TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006435
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006436static void thinkpad_acpi_module_exit(void)
6437{
6438 struct ibm_struct *ibm, *itmp;
6439
6440 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6441
6442 list_for_each_entry_safe_reverse(ibm, itmp,
6443 &tpacpi_all_drivers,
6444 all_drivers) {
6445 ibm_exit(ibm);
6446 }
6447
6448 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6449
6450 if (tpacpi_inputdev) {
6451 if (tp_features.input_device_registered)
6452 input_unregister_device(tpacpi_inputdev);
6453 else
6454 input_free_device(tpacpi_inputdev);
6455 }
6456
6457 if (tpacpi_hwmon)
6458 hwmon_device_unregister(tpacpi_hwmon);
6459
6460 if (tp_features.sensors_pdev_attrs_registered)
6461 device_remove_file(&tpacpi_sensors_pdev->dev,
6462 &dev_attr_thinkpad_acpi_pdev_name);
6463 if (tpacpi_sensors_pdev)
6464 platform_device_unregister(tpacpi_sensors_pdev);
6465 if (tpacpi_pdev)
6466 platform_device_unregister(tpacpi_pdev);
6467
6468 if (tp_features.sensors_pdrv_attrs_registered)
6469 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6470 if (tp_features.platform_drv_attrs_registered)
6471 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6472
6473 if (tp_features.sensors_pdrv_registered)
6474 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6475
6476 if (tp_features.platform_drv_registered)
6477 platform_driver_unregister(&tpacpi_pdriver);
6478
6479 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006480 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006481
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006482 if (tpacpi_wq)
6483 destroy_workqueue(tpacpi_wq);
6484
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006485 kfree(thinkpad_id.bios_version_str);
6486 kfree(thinkpad_id.ec_version_str);
6487 kfree(thinkpad_id.model_str);
6488}
6489
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006490
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006491static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492{
6493 int ret, i;
6494
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006495 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6496
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006497 /* Parameter checking */
6498 if (hotkey_report_mode > 2)
6499 return -EINVAL;
6500
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006501 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006502
6503 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006504 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006505 if (ret) {
6506 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006507 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006510 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006511
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006512 TPACPI_ACPIHANDLE_INIT(ecrd);
6513 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006515 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6516 if (!tpacpi_wq) {
6517 thinkpad_acpi_module_exit();
6518 return -ENOMEM;
6519 }
6520
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006521 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006523 printk(TPACPI_ERR
6524 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006525 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 return -ENODEV;
6527 }
6528 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006529
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006530 ret = platform_driver_register(&tpacpi_pdriver);
6531 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006532 printk(TPACPI_ERR
6533 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006534 thinkpad_acpi_module_exit();
6535 return ret;
6536 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006537 tp_features.platform_drv_registered = 1;
6538
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006539 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6540 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006541 printk(TPACPI_ERR
6542 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006543 thinkpad_acpi_module_exit();
6544 return ret;
6545 }
6546 tp_features.sensors_pdrv_registered = 1;
6547
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006548 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006549 if (!ret) {
6550 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006551 ret = tpacpi_create_driver_attributes(
6552 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006553 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006554 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006555 printk(TPACPI_ERR
6556 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006557 thinkpad_acpi_module_exit();
6558 return ret;
6559 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006560 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006561
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006562
6563 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006564 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006565 NULL, 0);
6566 if (IS_ERR(tpacpi_pdev)) {
6567 ret = PTR_ERR(tpacpi_pdev);
6568 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006569 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006570 thinkpad_acpi_module_exit();
6571 return ret;
6572 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006573 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006574 TPACPI_HWMON_DRVR_NAME,
6575 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006576 if (IS_ERR(tpacpi_sensors_pdev)) {
6577 ret = PTR_ERR(tpacpi_sensors_pdev);
6578 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006579 printk(TPACPI_ERR
6580 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006581 thinkpad_acpi_module_exit();
6582 return ret;
6583 }
6584 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6585 &dev_attr_thinkpad_acpi_pdev_name);
6586 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006587 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006588 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006589 thinkpad_acpi_module_exit();
6590 return ret;
6591 }
6592 tp_features.sensors_pdev_attrs_registered = 1;
6593 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006594 if (IS_ERR(tpacpi_hwmon)) {
6595 ret = PTR_ERR(tpacpi_hwmon);
6596 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006597 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006598 thinkpad_acpi_module_exit();
6599 return ret;
6600 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006601 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006602 tpacpi_inputdev = input_allocate_device();
6603 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006604 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006605 thinkpad_acpi_module_exit();
6606 return -ENOMEM;
6607 } else {
6608 /* Prepare input device, but don't register */
6609 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006610 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006611 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006612 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6613 thinkpad_id.vendor :
6614 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006615 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6616 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6617 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006618 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6619 ret = ibm_init(&ibms_init[i]);
6620 if (ret >= 0 && *ibms_init[i].param)
6621 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006623 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 return ret;
6625 }
6626 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006627 ret = input_register_device(tpacpi_inputdev);
6628 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006629 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006630 thinkpad_acpi_module_exit();
6631 return ret;
6632 } else {
6633 tp_features.input_device_registered = 1;
6634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006636 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 return 0;
6638}
6639
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006640/* Please remove this in year 2009 */
6641MODULE_ALIAS("ibm_acpi");
6642
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03006643MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6644
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006645/*
6646 * DMI matching for module autoloading
6647 *
6648 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6649 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6650 *
6651 * Only models listed in thinkwiki will be supported, so add yours
6652 * if it is not there yet.
6653 */
6654#define IBM_BIOS_MODULE_ALIAS(__type) \
6655 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6656
6657/* Non-ancient thinkpads */
6658MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6659MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6660
6661/* Ancient thinkpad BIOSes have to be identified by
6662 * BIOS type or model number, and there are far less
6663 * BIOS types than model numbers... */
6664IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6665IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6666IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6667
6668MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006669MODULE_DESCRIPTION(TPACPI_DESC);
6670MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006671MODULE_LICENSE("GPL");
6672
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006673module_init(thinkpad_acpi_module_init);
6674module_exit(thinkpad_acpi_module_exit);