blob: c1d40410ad794ec353eca3f1d819e5a187337604 [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 Holschuh490673d2008-07-21 09:15:51 -030024#define TPACPI_VERSION "0.21"
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 Holschuh0e74dc22008-07-21 09:15:51 -030071#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020072#include <asm/uaccess.h>
73
74#include <linux/dmi.h>
75#include <linux/jiffies.h>
76#include <linux/workqueue.h>
77
78#include <acpi/acpi_drivers.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020079
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
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -0200125/* ACPI \WGSV commands */
126enum {
127 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
128 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
129 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
130 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
131};
132
133/* TP_ACPI_WGSV_GET_STATE bits */
134enum {
135 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
136 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
137 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
138 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
139 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
140 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
141 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
142 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
143 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
144 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
145};
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200146
147/****************************************************************************
148 * Main driver
149 */
150
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200151#define TPACPI_NAME "thinkpad"
152#define TPACPI_DESC "ThinkPad ACPI Extras"
153#define TPACPI_FILE TPACPI_NAME "_acpi"
154#define TPACPI_URL "http://ibm-acpi.sf.net/"
155#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200156
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200157#define TPACPI_PROC_DIR "ibm"
158#define TPACPI_ACPI_EVENT_PREFIX "ibm"
159#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300160#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200161#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200162
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300163#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300164#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300165
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200166#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200167
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300168/* rfkill switches */
169enum {
170 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
171 TPACPI_RFK_WWAN_SW_ID,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200172 TPACPI_RFK_UWB_SW_ID,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300173};
174
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200175/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200176#define TPACPI_LOG TPACPI_FILE ": "
177#define TPACPI_ERR KERN_ERR TPACPI_LOG
178#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
179#define TPACPI_INFO KERN_INFO TPACPI_LOG
180#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200181
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200182#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200183#define TPACPI_DBG_INIT 0x0001
184#define TPACPI_DBG_EXIT 0x0002
185#define dbg_printk(a_dbg_level, format, arg...) \
186 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200187 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
188 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200189#ifdef CONFIG_THINKPAD_ACPI_DEBUG
190#define vdbg_printk(a_dbg_level, format, arg...) \
191 dbg_printk(a_dbg_level, format, ## arg)
192static const char *str_supported(int is_supported);
193#else
194#define vdbg_printk(a_dbg_level, format, arg...)
195#endif
196
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200197#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200200
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200201
202/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200203 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200204 */
205
206struct ibm_struct;
207
208struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
211
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
216};
217
218struct ibm_struct {
219 char *name;
220
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200225 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200226 void (*shutdown) (void);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200227
228 struct list_head all_drivers;
229
230 struct tp_acpi_drv_struct *acpi;
231
232 struct {
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
235 u8 proc_created:1;
236 u8 init_called:1;
237 u8 experimental:1;
238 } flags;
239};
240
241struct ibm_init_struct {
242 char param[32];
243
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
246};
247
248static struct {
249#ifdef CONFIG_THINKPAD_ACPI_BAY
250 u32 bay_status:1;
251 u32 bay_eject:1;
252 u32 bay_status2:1;
253 u32 bay_eject2:1;
254#endif
255 u32 bluetooth:1;
256 u32 hotkey:1;
257 u32 hotkey_mask:1;
258 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200259 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200260 u32 light:1;
261 u32 light_status:1;
262 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300263 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264 u32 wan:1;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200265 u32 uwb:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200266 u32 fan_ctrl_status_undef:1;
267 u32 input_device_registered:1;
268 u32 platform_drv_registered:1;
269 u32 platform_drv_attrs_registered:1;
270 u32 sensors_pdrv_registered:1;
271 u32 sensors_pdrv_attrs_registered:1;
272 u32 sensors_pdev_attrs_registered:1;
273 u32 hotkey_poll_active:1;
274} tp_features;
275
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300276static struct {
277 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300278 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300279} tp_warned;
280
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200281struct thinkpad_id_data {
282 unsigned int vendor; /* ThinkPad vendor:
283 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
284
285 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
286 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
287
288 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
289 u16 ec_model;
290
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300291 char *model_str; /* ThinkPad T43 */
292 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200293};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200294static struct thinkpad_id_data thinkpad_id;
295
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300296static enum {
297 TPACPI_LIFE_INIT = 0,
298 TPACPI_LIFE_RUNNING,
299 TPACPI_LIFE_EXITING,
300} tpacpi_lifecycle;
301
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200302static int experimental;
303static u32 dbg_level;
304
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300305static struct workqueue_struct *tpacpi_wq;
306
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300307/* Special LED class that can defer work */
308struct tpacpi_led_classdev {
309 struct led_classdev led_classdev;
310 struct work_struct work;
311 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300312 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300313};
314
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200315#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
316static int dbg_wlswemul;
317static int tpacpi_wlsw_emulstate;
318static int dbg_bluetoothemul;
319static int tpacpi_bluetooth_emulstate;
320static int dbg_wwanemul;
321static int tpacpi_wwan_emulstate;
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200322static int dbg_uwbemul;
323static int tpacpi_uwb_emulstate;
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -0200324#endif
325
326
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300327/****************************************************************************
328 ****************************************************************************
329 *
330 * ACPI Helpers and device model
331 *
332 ****************************************************************************
333 ****************************************************************************/
334
335/*************************************************************************
336 * ACPI basic handles
337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300339static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200341#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 static acpi_handle object##_handle; \
343 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400344 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 static char *object##_paths[] = { paths }
346
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200347TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400348 "\\_SB.PCI.ISA.EC", /* 570 */
349 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
350 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
351 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
352 "\\_SB.PCI0.ICH3.EC0", /* R31 */
353 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300354 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200356TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
357TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200359TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
360 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400361 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
362 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300363 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400364
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200365TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400366 "^HKEY", /* R30, R31 */
367 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300368 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400369
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200370TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
371 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
372 "\\_SB.PCI0.VID0", /* 770e */
373 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
374 "\\_SB.PCI0.AGP.VID", /* all others */
375 ); /* R30, R31 */
376
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400377
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300378/*************************************************************************
379 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200380 */
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static int acpi_evalf(acpi_handle handle,
383 void *res, char *method, char *fmt, ...)
384{
385 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400386 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200387 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400388 struct acpi_buffer result, *resultp;
389 union acpi_object out_obj;
390 acpi_status status;
391 va_list ap;
392 char res_type;
393 int success;
394 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200397 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399 }
400
401 if (*fmt == 'q') {
402 quiet = 1;
403 fmt++;
404 } else
405 quiet = 0;
406
407 res_type = *(fmt++);
408
409 params.count = 0;
410 params.pointer = &in_objs[0];
411
412 va_start(ap, fmt);
413 while (*fmt) {
414 char c = *(fmt++);
415 switch (c) {
416 case 'd': /* int */
417 in_objs[params.count].integer.value = va_arg(ap, int);
418 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
419 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400420 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200422 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 "with invalid format character '%c'\n", c);
424 return 0;
425 }
426 }
427 va_end(ap);
428
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400429 if (res_type != 'v') {
430 result.length = sizeof(out_obj);
431 result.pointer = &out_obj;
432 resultp = &result;
433 } else
434 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400436 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400439 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (res)
441 *(int *)res = out_obj.integer.value;
442 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
443 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400444 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 success = status == AE_OK;
446 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400447 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200449 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 "with invalid format character '%c'\n", res_type);
451 return 0;
452 }
453
454 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200455 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 method, fmt0, status);
457
458 return success;
459}
460
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200461static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300462{
463 int v;
464
465 if (ecrd_handle) {
466 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
467 return 0;
468 *p = v;
469 } else {
470 if (ec_read(i, p) < 0)
471 return 0;
472 }
473
474 return 1;
475}
476
477static int acpi_ec_write(int i, u8 v)
478{
479 if (ecwr_handle) {
480 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
481 return 0;
482 } else {
483 if (ec_write(i, v) < 0)
484 return 0;
485 }
486
487 return 1;
488}
489
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200490#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300491static int _sta(acpi_handle handle)
492{
493 int status;
494
495 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
496 status = 0;
497
498 return status;
499}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200500#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300501
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300502static int issue_thinkpad_cmos_command(int cmos_cmd)
503{
504 if (!cmos_handle)
505 return -ENXIO;
506
507 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
508 return -EIO;
509
510 return 0;
511}
512
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300513/*************************************************************************
514 * ACPI device model
515 */
516
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200517#define TPACPI_ACPIHANDLE_INIT(object) \
518 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200519 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
520
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300521static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300522 acpi_handle *handle, acpi_handle parent,
523 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300524{
525 int i;
526 acpi_status status;
527
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300528 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
529 name);
530
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300531 for (i = 0; i < num_paths; i++) {
532 status = acpi_get_handle(parent, paths[i], handle);
533 if (ACPI_SUCCESS(status)) {
534 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300535 dbg_printk(TPACPI_DBG_INIT,
536 "Found ACPI handle %s for %s\n",
537 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300538 return;
539 }
540 }
541
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300542 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
543 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300544 *handle = NULL;
545}
546
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300547static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300548{
549 struct ibm_struct *ibm = data;
550
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300551 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
552 return;
553
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300554 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300555 return;
556
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300557 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300558}
559
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300560static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300561{
562 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300563 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300564
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300565 BUG_ON(!ibm->acpi);
566
567 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300568 return 0;
569
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300570 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300571 "setting up ACPI notify for %s\n", ibm->name);
572
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300573 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
574 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200575 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300576 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300577 return -ENODEV;
578 }
579
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700580 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300581 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200582 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300583 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300584
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300585 status = acpi_install_notify_handler(*ibm->acpi->handle,
586 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300587 if (ACPI_FAILURE(status)) {
588 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200589 printk(TPACPI_NOTICE
590 "another device driver is already "
591 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300592 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200593 printk(TPACPI_ERR
594 "acpi_install_notify_handler(%s) failed: %d\n",
595 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300596 }
597 return -ENODEV;
598 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300599 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300600 return 0;
601}
602
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300603static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300604{
605 return 0;
606}
607
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300608static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300609{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300610 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300611
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300612 dbg_printk(TPACPI_DBG_INIT,
613 "registering %s as an ACPI driver\n", ibm->name);
614
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300615 BUG_ON(!ibm->acpi);
616
617 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
618 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300619 printk(TPACPI_ERR
620 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300621 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300622 }
623
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200624 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300625 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200626
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300627 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300628
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300629 rc = acpi_bus_register_driver(ibm->acpi->driver);
630 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200631 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200632 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300633 kfree(ibm->acpi->driver);
634 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300635 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300636 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300637
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300638 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300639}
640
641
642/****************************************************************************
643 ****************************************************************************
644 *
645 * Procfs Helpers
646 *
647 ****************************************************************************
648 ****************************************************************************/
649
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300650static int dispatch_procfs_read(char *page, char **start, off_t off,
651 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300652{
653 struct ibm_struct *ibm = data;
654 int len;
655
656 if (!ibm || !ibm->read)
657 return -EINVAL;
658
659 len = ibm->read(page);
660 if (len < 0)
661 return len;
662
663 if (len <= off + count)
664 *eof = 1;
665 *start = page + off;
666 len -= off;
667 if (len > count)
668 len = count;
669 if (len < 0)
670 len = 0;
671
672 return len;
673}
674
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300675static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200676 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300677 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300678{
679 struct ibm_struct *ibm = data;
680 char *kernbuf;
681 int ret;
682
683 if (!ibm || !ibm->write)
684 return -EINVAL;
685
686 kernbuf = kmalloc(count + 2, GFP_KERNEL);
687 if (!kernbuf)
688 return -ENOMEM;
689
690 if (copy_from_user(kernbuf, userbuf, count)) {
691 kfree(kernbuf);
692 return -EFAULT;
693 }
694
695 kernbuf[count] = 0;
696 strcat(kernbuf, ",");
697 ret = ibm->write(kernbuf);
698 if (ret == 0)
699 ret = count;
700
701 kfree(kernbuf);
702
703 return ret;
704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706static char *next_cmd(char **cmds)
707{
708 char *start = *cmds;
709 char *end;
710
711 while ((end = strchr(start, ',')) && end == start)
712 start = end + 1;
713
714 if (!end)
715 return NULL;
716
717 *end = 0;
718 *cmds = end + 1;
719 return start;
720}
721
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300722
723/****************************************************************************
724 ****************************************************************************
725 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300726 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300727 *
728 ****************************************************************************
729 ****************************************************************************/
730
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300731static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300732static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700733static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300734static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300735static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200736static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300737
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200738static int tpacpi_suspend_handler(struct platform_device *pdev,
739 pm_message_t state)
740{
741 struct ibm_struct *ibm, *itmp;
742
743 list_for_each_entry_safe(ibm, itmp,
744 &tpacpi_all_drivers,
745 all_drivers) {
746 if (ibm->suspend)
747 (ibm->suspend)(state);
748 }
749
750 return 0;
751}
752
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300753static int tpacpi_resume_handler(struct platform_device *pdev)
754{
755 struct ibm_struct *ibm, *itmp;
756
757 list_for_each_entry_safe(ibm, itmp,
758 &tpacpi_all_drivers,
759 all_drivers) {
760 if (ibm->resume)
761 (ibm->resume)();
762 }
763
764 return 0;
765}
766
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200767static void tpacpi_shutdown_handler(struct platform_device *pdev)
768{
769 struct ibm_struct *ibm, *itmp;
770
771 list_for_each_entry_safe(ibm, itmp,
772 &tpacpi_all_drivers,
773 all_drivers) {
774 if (ibm->shutdown)
775 (ibm->shutdown)();
776 }
777}
778
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300779static struct platform_driver tpacpi_pdriver = {
780 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200781 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300782 .owner = THIS_MODULE,
783 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200784 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300785 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200786 .shutdown = tpacpi_shutdown_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300787};
788
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300789static struct platform_driver tpacpi_hwmon_pdriver = {
790 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200791 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300792 .owner = THIS_MODULE,
793 },
794};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300795
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300796/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300797 * sysfs support helpers
798 */
799
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200800struct attribute_set {
801 unsigned int members, max_members;
802 struct attribute_group group;
803};
804
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300805struct attribute_set_obj {
806 struct attribute_set s;
807 struct attribute *a;
808} __attribute__((packed));
809
810static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200811 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300812{
813 struct attribute_set_obj *sobj;
814
815 if (max_members == 0)
816 return NULL;
817
818 /* Allocates space for implicit NULL at the end too */
819 sobj = kzalloc(sizeof(struct attribute_set_obj) +
820 max_members * sizeof(struct attribute *),
821 GFP_KERNEL);
822 if (!sobj)
823 return NULL;
824 sobj->s.max_members = max_members;
825 sobj->s.group.attrs = &sobj->a;
826 sobj->s.group.name = name;
827
828 return &sobj->s;
829}
830
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200831#define destroy_attr_set(_set) \
832 kfree(_set);
833
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300834/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200835static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300836{
837 if (!s || !attr)
838 return -EINVAL;
839
840 if (s->members >= s->max_members)
841 return -ENOMEM;
842
843 s->group.attrs[s->members] = attr;
844 s->members++;
845
846 return 0;
847}
848
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200849static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300850 struct attribute **attr,
851 unsigned int count)
852{
853 int i, res;
854
855 for (i = 0; i < count; i++) {
856 res = add_to_attr_set(s, attr[i]);
857 if (res)
858 return res;
859 }
860
861 return 0;
862}
863
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200864static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300865{
866 sysfs_remove_group(kobj, &s->group);
867 destroy_attr_set(s);
868}
869
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200870#define register_attr_set_with_sysfs(_attr_set, _kobj) \
871 sysfs_create_group(_kobj, &_attr_set->group)
872
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300873static int parse_strtoul(const char *buf,
874 unsigned long max, unsigned long *value)
875{
876 char *endp;
877
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300878 while (*buf && isspace(*buf))
879 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300880 *value = simple_strtoul(buf, &endp, 0);
881 while (*endp && isspace(*endp))
882 endp++;
883 if (*endp || *value > max)
884 return -EINVAL;
885
886 return 0;
887}
888
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300889static void tpacpi_disable_brightness_delay(void)
890{
891 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
892 printk(TPACPI_NOTICE
893 "ACPI backlight control delay disabled\n");
894}
895
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300896static int __init tpacpi_query_bcl_levels(acpi_handle handle)
897{
898 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
899 union acpi_object *obj;
900 int rc;
901
902 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
903 obj = (union acpi_object *)buffer.pointer;
904 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
905 printk(TPACPI_ERR "Unknown _BCL data, "
906 "please report this to %s\n", TPACPI_MAIL);
907 rc = 0;
908 } else {
909 rc = obj->package.count;
910 }
911 } else {
912 return 0;
913 }
914
915 kfree(buffer.pointer);
916 return rc;
917}
918
919static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
920 u32 lvl, void *context, void **rv)
921{
922 char name[ACPI_PATH_SEGMENT_LENGTH];
923 struct acpi_buffer buffer = { sizeof(name), &name };
924
925 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
926 !strncmp("_BCL", name, sizeof(name) - 1)) {
927 BUG_ON(!rv || !*rv);
928 **(int **)rv = tpacpi_query_bcl_levels(handle);
929 return AE_CTRL_TERMINATE;
930 } else {
931 return AE_OK;
932 }
933}
934
935/*
936 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
937 */
938static int __init tpacpi_check_std_acpi_brightness_support(void)
939{
940 int status;
941 int bcl_levels = 0;
942 void *bcl_ptr = &bcl_levels;
943
944 if (!vid_handle) {
945 TPACPI_ACPIHANDLE_INIT(vid);
946 }
947 if (!vid_handle)
948 return 0;
949
950 /*
951 * Search for a _BCL method, and execute it. This is safe on all
952 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
953 * BIOS in ACPI backlight control mode. We do NOT have to care
954 * about calling the _BCL method in an enabled video device, any
955 * will do for our purposes.
956 */
957
958 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
959 tpacpi_acpi_walk_find_bcl, NULL,
960 &bcl_ptr);
961
962 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
963 tp_features.bright_acpimode = 1;
964 return (bcl_levels - 2);
965 }
966
967 return 0;
968}
969
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300970static int __init tpacpi_new_rfkill(const unsigned int id,
971 struct rfkill **rfk,
972 const enum rfkill_type rfktype,
973 const char *name,
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200974 const bool set_default,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300975 int (*toggle_radio)(void *, enum rfkill_state),
976 int (*get_state)(void *, enum rfkill_state *))
977{
978 int res;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200979 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
980
981 res = get_state(NULL, &initial_state);
982 if (res < 0) {
983 printk(TPACPI_ERR
984 "failed to read initial state for %s, error %d; "
985 "will turn radio off\n", name, res);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -0200986 } else if (set_default) {
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -0200987 /* try to set the initial state as the default for the rfkill
988 * type, since we ask the firmware to preserve it across S5 in
989 * NVRAM */
990 rfkill_set_default(rfktype,
991 (initial_state == RFKILL_STATE_UNBLOCKED) ?
992 RFKILL_STATE_UNBLOCKED :
993 RFKILL_STATE_SOFT_BLOCKED);
994 }
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300995
996 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
997 if (!*rfk) {
998 printk(TPACPI_ERR
999 "failed to allocate memory for rfkill class\n");
1000 return -ENOMEM;
1001 }
1002
1003 (*rfk)->name = name;
1004 (*rfk)->get_state = get_state;
1005 (*rfk)->toggle_radio = toggle_radio;
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02001006 (*rfk)->state = initial_state;
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001007
1008 res = rfkill_register(*rfk);
1009 if (res < 0) {
1010 printk(TPACPI_ERR
1011 "failed to register %s rfkill switch: %d\n",
1012 name, res);
1013 rfkill_free(*rfk);
1014 *rfk = NULL;
1015 return res;
1016 }
1017
1018 return 0;
1019}
1020
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001021/*************************************************************************
1022 * thinkpad-acpi driver attributes
1023 */
1024
1025/* interface_version --------------------------------------------------- */
1026static ssize_t tpacpi_driver_interface_version_show(
1027 struct device_driver *drv,
1028 char *buf)
1029{
1030 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1031}
1032
1033static DRIVER_ATTR(interface_version, S_IRUGO,
1034 tpacpi_driver_interface_version_show, NULL);
1035
1036/* debug_level --------------------------------------------------------- */
1037static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1038 char *buf)
1039{
1040 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1041}
1042
1043static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1044 const char *buf, size_t count)
1045{
1046 unsigned long t;
1047
1048 if (parse_strtoul(buf, 0xffff, &t))
1049 return -EINVAL;
1050
1051 dbg_level = t;
1052
1053 return count;
1054}
1055
1056static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1057 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1058
1059/* version ------------------------------------------------------------- */
1060static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1061 char *buf)
1062{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001063 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1064 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001065}
1066
1067static DRIVER_ATTR(version, S_IRUGO,
1068 tpacpi_driver_version_show, NULL);
1069
1070/* --------------------------------------------------------------------- */
1071
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001072#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1073
1074static void tpacpi_send_radiosw_update(void);
1075
1076/* wlsw_emulstate ------------------------------------------------------ */
1077static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1078 char *buf)
1079{
1080 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1081}
1082
1083static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1084 const char *buf, size_t count)
1085{
1086 unsigned long t;
1087
1088 if (parse_strtoul(buf, 1, &t))
1089 return -EINVAL;
1090
1091 if (tpacpi_wlsw_emulstate != t) {
1092 tpacpi_wlsw_emulstate = !!t;
1093 tpacpi_send_radiosw_update();
1094 } else
1095 tpacpi_wlsw_emulstate = !!t;
1096
1097 return count;
1098}
1099
1100static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1101 tpacpi_driver_wlsw_emulstate_show,
1102 tpacpi_driver_wlsw_emulstate_store);
1103
1104/* bluetooth_emulstate ------------------------------------------------- */
1105static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1106 struct device_driver *drv,
1107 char *buf)
1108{
1109 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1110}
1111
1112static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1113 struct device_driver *drv,
1114 const char *buf, size_t count)
1115{
1116 unsigned long t;
1117
1118 if (parse_strtoul(buf, 1, &t))
1119 return -EINVAL;
1120
1121 tpacpi_bluetooth_emulstate = !!t;
1122
1123 return count;
1124}
1125
1126static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1127 tpacpi_driver_bluetooth_emulstate_show,
1128 tpacpi_driver_bluetooth_emulstate_store);
1129
1130/* wwan_emulstate ------------------------------------------------- */
1131static ssize_t tpacpi_driver_wwan_emulstate_show(
1132 struct device_driver *drv,
1133 char *buf)
1134{
1135 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1136}
1137
1138static ssize_t tpacpi_driver_wwan_emulstate_store(
1139 struct device_driver *drv,
1140 const char *buf, size_t count)
1141{
1142 unsigned long t;
1143
1144 if (parse_strtoul(buf, 1, &t))
1145 return -EINVAL;
1146
1147 tpacpi_wwan_emulstate = !!t;
1148
1149 return count;
1150}
1151
1152static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1153 tpacpi_driver_wwan_emulstate_show,
1154 tpacpi_driver_wwan_emulstate_store);
1155
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001156/* uwb_emulstate ------------------------------------------------- */
1157static ssize_t tpacpi_driver_uwb_emulstate_show(
1158 struct device_driver *drv,
1159 char *buf)
1160{
1161 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1162}
1163
1164static ssize_t tpacpi_driver_uwb_emulstate_store(
1165 struct device_driver *drv,
1166 const char *buf, size_t count)
1167{
1168 unsigned long t;
1169
1170 if (parse_strtoul(buf, 1, &t))
1171 return -EINVAL;
1172
1173 tpacpi_uwb_emulstate = !!t;
1174
1175 return count;
1176}
1177
1178static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1179 tpacpi_driver_uwb_emulstate_show,
1180 tpacpi_driver_uwb_emulstate_store);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001181#endif
1182
1183/* --------------------------------------------------------------------- */
1184
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001185static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001186 &driver_attr_debug_level, &driver_attr_version,
1187 &driver_attr_interface_version,
1188};
1189
1190static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1191{
1192 int i, res;
1193
1194 i = 0;
1195 res = 0;
1196 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1197 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1198 i++;
1199 }
1200
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001201#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1202 if (!res && dbg_wlswemul)
1203 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1204 if (!res && dbg_bluetoothemul)
1205 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1206 if (!res && dbg_wwanemul)
1207 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001208 if (!res && dbg_uwbemul)
1209 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001210#endif
1211
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001212 return res;
1213}
1214
1215static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1216{
1217 int i;
1218
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001219 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001220 driver_remove_file(drv, tpacpi_driver_attributes[i]);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001221
1222#ifdef THINKPAD_ACPI_DEBUGFACILITIES
1223 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1224 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1225 driver_remove_file(drv, &driver_attr_wwan_emulstate);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02001226 driver_remove_file(drv, &driver_attr_uwb_emulstate);
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001227#endif
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001228}
1229
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001230/****************************************************************************
1231 ****************************************************************************
1232 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001233 * Subdrivers
1234 *
1235 ****************************************************************************
1236 ****************************************************************************/
1237
1238/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001239 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001240 */
1241
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001242static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001244 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1245 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001247 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001248 (thinkpad_id.bios_version_str) ?
1249 thinkpad_id.bios_version_str : "unknown",
1250 (thinkpad_id.ec_version_str) ?
1251 thinkpad_id.ec_version_str : "unknown");
1252
1253 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001254 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001255 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1256 "IBM" : ((thinkpad_id.vendor ==
1257 PCI_VENDOR_ID_LENOVO) ?
1258 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001259 thinkpad_id.model_str,
1260 (thinkpad_id.nummodel_str) ?
1261 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return 0;
1264}
1265
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001266static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 int len = 0;
1269
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001270 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1271 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 return len;
1274}
1275
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001276static struct ibm_struct thinkpad_acpi_driver_data = {
1277 .name = "driver",
1278 .read = thinkpad_acpi_driver_read,
1279};
1280
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001281/*************************************************************************
1282 * Hotkey subdriver
1283 */
1284
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001285enum { /* hot key scan codes (derived from ACPI DSDT) */
1286 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1287 TP_ACPI_HOTKEYSCAN_FNF2,
1288 TP_ACPI_HOTKEYSCAN_FNF3,
1289 TP_ACPI_HOTKEYSCAN_FNF4,
1290 TP_ACPI_HOTKEYSCAN_FNF5,
1291 TP_ACPI_HOTKEYSCAN_FNF6,
1292 TP_ACPI_HOTKEYSCAN_FNF7,
1293 TP_ACPI_HOTKEYSCAN_FNF8,
1294 TP_ACPI_HOTKEYSCAN_FNF9,
1295 TP_ACPI_HOTKEYSCAN_FNF10,
1296 TP_ACPI_HOTKEYSCAN_FNF11,
1297 TP_ACPI_HOTKEYSCAN_FNF12,
1298 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1299 TP_ACPI_HOTKEYSCAN_FNINSERT,
1300 TP_ACPI_HOTKEYSCAN_FNDELETE,
1301 TP_ACPI_HOTKEYSCAN_FNHOME,
1302 TP_ACPI_HOTKEYSCAN_FNEND,
1303 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1304 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1305 TP_ACPI_HOTKEYSCAN_FNSPACE,
1306 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1307 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1308 TP_ACPI_HOTKEYSCAN_MUTE,
1309 TP_ACPI_HOTKEYSCAN_THINKPAD,
1310};
1311
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001312enum { /* Keys available through NVRAM polling */
1313 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1314 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1315};
1316
1317enum { /* Positions of some of the keys in hotkey masks */
1318 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1319 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1320 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1321 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1322 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1323 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1324 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1325 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1326 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1327 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1328 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1329};
1330
1331enum { /* NVRAM to ACPI HKEY group map */
1332 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1333 TP_ACPI_HKEY_ZOOM_MASK |
1334 TP_ACPI_HKEY_DISPSWTCH_MASK |
1335 TP_ACPI_HKEY_HIBERNATE_MASK,
1336 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1337 TP_ACPI_HKEY_BRGHTDWN_MASK,
1338 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1339 TP_ACPI_HKEY_VOLDWN_MASK |
1340 TP_ACPI_HKEY_MUTE_MASK,
1341};
1342
1343#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1344struct tp_nvram_state {
1345 u16 thinkpad_toggle:1;
1346 u16 zoom_toggle:1;
1347 u16 display_toggle:1;
1348 u16 thinklight_toggle:1;
1349 u16 hibernate_toggle:1;
1350 u16 displayexp_toggle:1;
1351 u16 display_state:1;
1352 u16 brightness_toggle:1;
1353 u16 volume_toggle:1;
1354 u16 mute:1;
1355
1356 u8 brightness_level;
1357 u8 volume_level;
1358};
1359
1360static struct task_struct *tpacpi_hotkey_task;
1361static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1362static int hotkey_poll_freq = 10; /* Hz */
1363static struct mutex hotkey_thread_mutex;
1364static struct mutex hotkey_thread_data_mutex;
1365static unsigned int hotkey_config_change;
1366
1367#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1368
1369#define hotkey_source_mask 0U
1370
1371#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1372
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001373static struct mutex hotkey_mutex;
1374
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001375static enum { /* Reasons for waking up */
1376 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1377 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1378 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1379} hotkey_wakeup_reason;
1380
1381static int hotkey_autosleep_ack;
1382
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001383static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001384static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001385static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001386static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001387static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001388
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001389static unsigned int hotkey_report_mode;
1390
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001391static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001392
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001393static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001394
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001395#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1396#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001397 do { \
1398 mutex_lock(&hotkey_thread_data_mutex); \
1399 hotkey_config_change++; \
1400 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001401#define HOTKEY_CONFIG_CRITICAL_END \
1402 mutex_unlock(&hotkey_thread_data_mutex);
1403#else
1404#define HOTKEY_CONFIG_CRITICAL_START
1405#define HOTKEY_CONFIG_CRITICAL_END
1406#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1407
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001408/* HKEY.MHKG() return bits */
1409#define TP_HOTKEY_TABLET_MASK (1 << 3)
1410
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001411static int hotkey_get_wlsw(int *status)
1412{
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02001413#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1414 if (dbg_wlswemul) {
1415 *status = !!tpacpi_wlsw_emulstate;
1416 return 0;
1417 }
1418#endif
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001419 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1420 return -EIO;
1421 return 0;
1422}
1423
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001424static int hotkey_get_tablet_mode(int *status)
1425{
1426 int s;
1427
1428 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1429 return -EIO;
1430
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001431 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1432 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001433}
1434
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001435/*
1436 * Call with hotkey_mutex held
1437 */
1438static int hotkey_mask_get(void)
1439{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001440 u32 m = 0;
1441
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001442 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001443 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001444 return -EIO;
1445 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001446 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001447
1448 return 0;
1449}
1450
1451/*
1452 * Call with hotkey_mutex held
1453 */
1454static int hotkey_mask_set(u32 mask)
1455{
1456 int i;
1457 int rc = 0;
1458
1459 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001460 if (!tp_warned.hotkey_mask_ff &&
1461 (mask == 0xffff || mask == 0xffffff ||
1462 mask == 0xffffffff)) {
1463 tp_warned.hotkey_mask_ff = 1;
1464 printk(TPACPI_NOTICE
1465 "setting the hotkey mask to 0x%08x is likely "
1466 "not the best way to go about it\n", mask);
1467 printk(TPACPI_NOTICE
1468 "please consider using the driver defaults, "
1469 "and refer to up-to-date thinkpad-acpi "
1470 "documentation\n");
1471 }
1472
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001473 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001474 for (i = 0; i < 32; i++) {
1475 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001476 /* enable in firmware mask only keys not in NVRAM
1477 * mode, but enable the key in the cached hotkey_mask
1478 * regardless of mode, or the key will end up
1479 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001480 if (!acpi_evalf(hkey_handle,
1481 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001482 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001483 rc = -EIO;
1484 break;
1485 } else {
1486 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1487 }
1488 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001489 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001490
1491 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001492 if (!hotkey_mask_get() && !rc &&
1493 (hotkey_mask & ~hotkey_source_mask) !=
1494 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001495 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001496 "requested hot key mask 0x%08x, but "
1497 "firmware forced it to 0x%08x\n",
1498 mask, hotkey_mask);
1499 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001500 } else {
1501#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1502 HOTKEY_CONFIG_CRITICAL_START
1503 hotkey_mask = mask & hotkey_source_mask;
1504 HOTKEY_CONFIG_CRITICAL_END
1505 hotkey_mask_get();
1506 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001507 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001508 "requested hot key mask 0x%08x, "
1509 "forced to 0x%08x (NVRAM poll mask is "
1510 "0x%08x): no firmware mask support\n",
1511 mask, hotkey_mask, hotkey_source_mask);
1512 }
1513#else
1514 hotkey_mask_get();
1515 rc = -ENXIO;
1516#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001517 }
1518
1519 return rc;
1520}
1521
1522static int hotkey_status_get(int *status)
1523{
1524 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1525 return -EIO;
1526
1527 return 0;
1528}
1529
1530static int hotkey_status_set(int status)
1531{
1532 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1533 return -EIO;
1534
1535 return 0;
1536}
1537
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001538static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001539{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001540 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001541
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001542 if (tp_features.hotkey_tablet &&
1543 !hotkey_get_tablet_mode(&state)) {
1544 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001545
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001546 input_report_switch(tpacpi_inputdev,
1547 SW_TABLET_MODE, !!state);
1548 input_sync(tpacpi_inputdev);
1549
1550 mutex_unlock(&tpacpi_inputdev_send_mutex);
1551 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001552}
1553
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001554static void tpacpi_input_send_key(unsigned int scancode)
1555{
1556 unsigned int keycode;
1557
1558 keycode = hotkey_keycode_map[scancode];
1559
1560 if (keycode != KEY_RESERVED) {
1561 mutex_lock(&tpacpi_inputdev_send_mutex);
1562
1563 input_report_key(tpacpi_inputdev, keycode, 1);
1564 if (keycode == KEY_UNKNOWN)
1565 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1566 scancode);
1567 input_sync(tpacpi_inputdev);
1568
1569 input_report_key(tpacpi_inputdev, keycode, 0);
1570 if (keycode == KEY_UNKNOWN)
1571 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1572 scancode);
1573 input_sync(tpacpi_inputdev);
1574
1575 mutex_unlock(&tpacpi_inputdev_send_mutex);
1576 }
1577}
1578
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001579#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1580static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1581
1582static void tpacpi_hotkey_send_key(unsigned int scancode)
1583{
1584 tpacpi_input_send_key(scancode);
1585 if (hotkey_report_mode < 2) {
1586 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1587 0x80, 0x1001 + scancode);
1588 }
1589}
1590
1591static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1592{
1593 u8 d;
1594
1595 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1596 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1597 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1598 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1599 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1600 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1601 }
1602 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1603 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1604 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1605 }
1606 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1607 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1608 n->displayexp_toggle =
1609 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1610 }
1611 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1612 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1613 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1614 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1615 n->brightness_toggle =
1616 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1617 }
1618 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1619 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1620 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1621 >> TP_NVRAM_POS_LEVEL_VOLUME;
1622 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1623 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1624 }
1625}
1626
1627#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001628 do { \
1629 if ((mask & (1 << __scancode)) && \
1630 oldn->__member != newn->__member) \
1631 tpacpi_hotkey_send_key(__scancode); \
1632 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001633
1634#define TPACPI_MAY_SEND_KEY(__scancode) \
1635 do { if (mask & (1 << __scancode)) \
1636 tpacpi_hotkey_send_key(__scancode); } while (0)
1637
1638static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001639 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001640 u32 mask)
1641{
1642 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1643 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1644 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1645 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1646
1647 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1648
1649 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1650
1651 /* handle volume */
1652 if (oldn->volume_toggle != newn->volume_toggle) {
1653 if (oldn->mute != newn->mute) {
1654 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1655 }
1656 if (oldn->volume_level > newn->volume_level) {
1657 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1658 } else if (oldn->volume_level < newn->volume_level) {
1659 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1660 } else if (oldn->mute == newn->mute) {
1661 /* repeated key presses that didn't change state */
1662 if (newn->mute) {
1663 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1664 } else if (newn->volume_level != 0) {
1665 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1666 } else {
1667 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1668 }
1669 }
1670 }
1671
1672 /* handle brightness */
1673 if (oldn->brightness_toggle != newn->brightness_toggle) {
1674 if (oldn->brightness_level < newn->brightness_level) {
1675 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1676 } else if (oldn->brightness_level > newn->brightness_level) {
1677 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1678 } else {
1679 /* repeated key presses that didn't change state */
1680 if (newn->brightness_level != 0) {
1681 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1682 } else {
1683 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1684 }
1685 }
1686 }
1687}
1688
1689#undef TPACPI_COMPARE_KEY
1690#undef TPACPI_MAY_SEND_KEY
1691
1692static int hotkey_kthread(void *data)
1693{
1694 struct tp_nvram_state s[2];
1695 u32 mask;
1696 unsigned int si, so;
1697 unsigned long t;
1698 unsigned int change_detector, must_reset;
1699
1700 mutex_lock(&hotkey_thread_mutex);
1701
1702 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1703 goto exit;
1704
1705 set_freezable();
1706
1707 so = 0;
1708 si = 1;
1709 t = 0;
1710
1711 /* Initial state for compares */
1712 mutex_lock(&hotkey_thread_data_mutex);
1713 change_detector = hotkey_config_change;
1714 mask = hotkey_source_mask & hotkey_mask;
1715 mutex_unlock(&hotkey_thread_data_mutex);
1716 hotkey_read_nvram(&s[so], mask);
1717
1718 while (!kthread_should_stop() && hotkey_poll_freq) {
1719 if (t == 0)
1720 t = 1000/hotkey_poll_freq;
1721 t = msleep_interruptible(t);
1722 if (unlikely(kthread_should_stop()))
1723 break;
1724 must_reset = try_to_freeze();
1725 if (t > 0 && !must_reset)
1726 continue;
1727
1728 mutex_lock(&hotkey_thread_data_mutex);
1729 if (must_reset || hotkey_config_change != change_detector) {
1730 /* forget old state on thaw or config change */
1731 si = so;
1732 t = 0;
1733 change_detector = hotkey_config_change;
1734 }
1735 mask = hotkey_source_mask & hotkey_mask;
1736 mutex_unlock(&hotkey_thread_data_mutex);
1737
1738 if (likely(mask)) {
1739 hotkey_read_nvram(&s[si], mask);
1740 if (likely(si != so)) {
1741 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001742 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001743 }
1744 }
1745
1746 so = si;
1747 si ^= 1;
1748 }
1749
1750exit:
1751 mutex_unlock(&hotkey_thread_mutex);
1752 return 0;
1753}
1754
1755static void hotkey_poll_stop_sync(void)
1756{
1757 if (tpacpi_hotkey_task) {
1758 if (frozen(tpacpi_hotkey_task) ||
1759 freezing(tpacpi_hotkey_task))
1760 thaw_process(tpacpi_hotkey_task);
1761
1762 kthread_stop(tpacpi_hotkey_task);
1763 tpacpi_hotkey_task = NULL;
1764 mutex_lock(&hotkey_thread_mutex);
1765 /* at this point, the thread did exit */
1766 mutex_unlock(&hotkey_thread_mutex);
1767 }
1768}
1769
1770/* call with hotkey_mutex held */
1771static void hotkey_poll_setup(int may_warn)
1772{
1773 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1774 hotkey_poll_freq > 0 &&
1775 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1776 if (!tpacpi_hotkey_task) {
1777 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001778 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001779 if (IS_ERR(tpacpi_hotkey_task)) {
1780 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001781 printk(TPACPI_ERR
1782 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001783 "for hotkey polling\n");
1784 }
1785 }
1786 } else {
1787 hotkey_poll_stop_sync();
1788 if (may_warn &&
1789 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001790 printk(TPACPI_NOTICE
1791 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001792 "which is currently disabled\n",
1793 hotkey_source_mask);
1794 }
1795 }
1796}
1797
1798static void hotkey_poll_setup_safe(int may_warn)
1799{
1800 mutex_lock(&hotkey_mutex);
1801 hotkey_poll_setup(may_warn);
1802 mutex_unlock(&hotkey_mutex);
1803}
1804
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001805#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1806
1807static void hotkey_poll_setup_safe(int __unused)
1808{
1809}
1810
1811#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1812
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001813static int hotkey_inputdev_open(struct input_dev *dev)
1814{
1815 switch (tpacpi_lifecycle) {
1816 case TPACPI_LIFE_INIT:
1817 /*
1818 * hotkey_init will call hotkey_poll_setup_safe
1819 * at the appropriate moment
1820 */
1821 return 0;
1822 case TPACPI_LIFE_EXITING:
1823 return -EBUSY;
1824 case TPACPI_LIFE_RUNNING:
1825 hotkey_poll_setup_safe(0);
1826 return 0;
1827 }
1828
1829 /* Should only happen if tpacpi_lifecycle is corrupt */
1830 BUG();
1831 return -EBUSY;
1832}
1833
1834static void hotkey_inputdev_close(struct input_dev *dev)
1835{
1836 /* disable hotkey polling when possible */
1837 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1838 hotkey_poll_setup_safe(0);
1839}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001840
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001841/* sysfs hotkey enable ------------------------------------------------- */
1842static ssize_t hotkey_enable_show(struct device *dev,
1843 struct device_attribute *attr,
1844 char *buf)
1845{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001846 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001847
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001848 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001849 if (res)
1850 return res;
1851
1852 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1853}
1854
1855static ssize_t hotkey_enable_store(struct device *dev,
1856 struct device_attribute *attr,
1857 const char *buf, size_t count)
1858{
1859 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001860 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001861
1862 if (parse_strtoul(buf, 1, &t))
1863 return -EINVAL;
1864
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001865 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001866
1867 return (res) ? res : count;
1868}
1869
1870static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001871 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001872 hotkey_enable_show, hotkey_enable_store);
1873
1874/* sysfs hotkey mask --------------------------------------------------- */
1875static ssize_t hotkey_mask_show(struct device *dev,
1876 struct device_attribute *attr,
1877 char *buf)
1878{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001879 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001880
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001881 if (mutex_lock_interruptible(&hotkey_mutex))
1882 return -ERESTARTSYS;
1883 res = hotkey_mask_get();
1884 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001885
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001886 return (res)?
1887 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001888}
1889
1890static ssize_t hotkey_mask_store(struct device *dev,
1891 struct device_attribute *attr,
1892 const char *buf, size_t count)
1893{
1894 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001895 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001896
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001897 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001898 return -EINVAL;
1899
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001900 if (mutex_lock_interruptible(&hotkey_mutex))
1901 return -ERESTARTSYS;
1902
1903 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001904
1905#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1906 hotkey_poll_setup(1);
1907#endif
1908
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001909 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001910
1911 return (res) ? res : count;
1912}
1913
1914static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001915 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001916 hotkey_mask_show, hotkey_mask_store);
1917
1918/* sysfs hotkey bios_enabled ------------------------------------------- */
1919static ssize_t hotkey_bios_enabled_show(struct device *dev,
1920 struct device_attribute *attr,
1921 char *buf)
1922{
1923 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1924}
1925
1926static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001927 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001928
1929/* sysfs hotkey bios_mask ---------------------------------------------- */
1930static ssize_t hotkey_bios_mask_show(struct device *dev,
1931 struct device_attribute *attr,
1932 char *buf)
1933{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001934 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001935}
1936
1937static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001938 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001939
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001940/* sysfs hotkey all_mask ----------------------------------------------- */
1941static ssize_t hotkey_all_mask_show(struct device *dev,
1942 struct device_attribute *attr,
1943 char *buf)
1944{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001945 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1946 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001947}
1948
1949static struct device_attribute dev_attr_hotkey_all_mask =
1950 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1951
1952/* sysfs hotkey recommended_mask --------------------------------------- */
1953static ssize_t hotkey_recommended_mask_show(struct device *dev,
1954 struct device_attribute *attr,
1955 char *buf)
1956{
1957 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001958 (hotkey_all_mask | hotkey_source_mask)
1959 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001960}
1961
1962static struct device_attribute dev_attr_hotkey_recommended_mask =
1963 __ATTR(hotkey_recommended_mask, S_IRUGO,
1964 hotkey_recommended_mask_show, NULL);
1965
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001966#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1967
1968/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1969static ssize_t hotkey_source_mask_show(struct device *dev,
1970 struct device_attribute *attr,
1971 char *buf)
1972{
1973 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1974}
1975
1976static ssize_t hotkey_source_mask_store(struct device *dev,
1977 struct device_attribute *attr,
1978 const char *buf, size_t count)
1979{
1980 unsigned long t;
1981
1982 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1983 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1984 return -EINVAL;
1985
1986 if (mutex_lock_interruptible(&hotkey_mutex))
1987 return -ERESTARTSYS;
1988
1989 HOTKEY_CONFIG_CRITICAL_START
1990 hotkey_source_mask = t;
1991 HOTKEY_CONFIG_CRITICAL_END
1992
1993 hotkey_poll_setup(1);
1994
1995 mutex_unlock(&hotkey_mutex);
1996
1997 return count;
1998}
1999
2000static struct device_attribute dev_attr_hotkey_source_mask =
2001 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2002 hotkey_source_mask_show, hotkey_source_mask_store);
2003
2004/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2005static ssize_t hotkey_poll_freq_show(struct device *dev,
2006 struct device_attribute *attr,
2007 char *buf)
2008{
2009 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2010}
2011
2012static ssize_t hotkey_poll_freq_store(struct device *dev,
2013 struct device_attribute *attr,
2014 const char *buf, size_t count)
2015{
2016 unsigned long t;
2017
2018 if (parse_strtoul(buf, 25, &t))
2019 return -EINVAL;
2020
2021 if (mutex_lock_interruptible(&hotkey_mutex))
2022 return -ERESTARTSYS;
2023
2024 hotkey_poll_freq = t;
2025
2026 hotkey_poll_setup(1);
2027 mutex_unlock(&hotkey_mutex);
2028
2029 return count;
2030}
2031
2032static struct device_attribute dev_attr_hotkey_poll_freq =
2033 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2034 hotkey_poll_freq_show, hotkey_poll_freq_store);
2035
2036#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2037
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002038/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002039static ssize_t hotkey_radio_sw_show(struct device *dev,
2040 struct device_attribute *attr,
2041 char *buf)
2042{
2043 int res, s;
2044 res = hotkey_get_wlsw(&s);
2045 if (res < 0)
2046 return res;
2047
2048 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2049}
2050
2051static struct device_attribute dev_attr_hotkey_radio_sw =
2052 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2053
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002054static void hotkey_radio_sw_notify_change(void)
2055{
2056 if (tp_features.hotkey_wlsw)
2057 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2058 "hotkey_radio_sw");
2059}
2060
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002061/* sysfs hotkey tablet mode (pollable) --------------------------------- */
2062static ssize_t hotkey_tablet_mode_show(struct device *dev,
2063 struct device_attribute *attr,
2064 char *buf)
2065{
2066 int res, s;
2067 res = hotkey_get_tablet_mode(&s);
2068 if (res < 0)
2069 return res;
2070
2071 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2072}
2073
2074static struct device_attribute dev_attr_hotkey_tablet_mode =
2075 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2076
2077static void hotkey_tablet_mode_notify_change(void)
2078{
2079 if (tp_features.hotkey_tablet)
2080 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2081 "hotkey_tablet_mode");
2082}
2083
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002084/* sysfs hotkey report_mode -------------------------------------------- */
2085static ssize_t hotkey_report_mode_show(struct device *dev,
2086 struct device_attribute *attr,
2087 char *buf)
2088{
2089 return snprintf(buf, PAGE_SIZE, "%d\n",
2090 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2091}
2092
2093static struct device_attribute dev_attr_hotkey_report_mode =
2094 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2095
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002096/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002097static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2098 struct device_attribute *attr,
2099 char *buf)
2100{
2101 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2102}
2103
2104static struct device_attribute dev_attr_hotkey_wakeup_reason =
2105 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2106
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002107static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002108{
2109 if (tp_features.hotkey_mask)
2110 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2111 "wakeup_reason");
2112}
2113
2114/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002115static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2116 struct device_attribute *attr,
2117 char *buf)
2118{
2119 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2120}
2121
2122static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2123 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2124 hotkey_wakeup_hotunplug_complete_show, NULL);
2125
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02002126static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002127{
2128 if (tp_features.hotkey_mask)
2129 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2130 "wakeup_hotunplug_complete");
2131}
2132
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002133/* --------------------------------------------------------------------- */
2134
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002135static struct attribute *hotkey_attributes[] __initdata = {
2136 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002137 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002138 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002139#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2140 &dev_attr_hotkey_mask.attr,
2141 &dev_attr_hotkey_all_mask.attr,
2142 &dev_attr_hotkey_recommended_mask.attr,
2143 &dev_attr_hotkey_source_mask.attr,
2144 &dev_attr_hotkey_poll_freq.attr,
2145#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002146};
2147
2148static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002149 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002150#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2151 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002152 &dev_attr_hotkey_all_mask.attr,
2153 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002154#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002155 &dev_attr_hotkey_wakeup_reason.attr,
2156 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002157};
2158
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002159static void bluetooth_update_rfk(void);
2160static void wan_update_rfk(void);
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002161static void uwb_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002162static void tpacpi_send_radiosw_update(void)
2163{
2164 int wlsw;
2165
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002166 /* Sync these BEFORE sending any rfkill events */
2167 if (tp_features.bluetooth)
2168 bluetooth_update_rfk();
2169 if (tp_features.wan)
2170 wan_update_rfk();
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02002171 if (tp_features.uwb)
2172 uwb_update_rfk();
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002173
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002174 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2175 mutex_lock(&tpacpi_inputdev_send_mutex);
2176
2177 input_report_switch(tpacpi_inputdev,
2178 SW_RFKILL_ALL, !!wlsw);
2179 input_sync(tpacpi_inputdev);
2180
2181 mutex_unlock(&tpacpi_inputdev_send_mutex);
2182 }
2183 hotkey_radio_sw_notify_change();
2184}
2185
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002186static void hotkey_exit(void)
2187{
2188#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2189 hotkey_poll_stop_sync();
2190#endif
2191
2192 if (hotkey_dev_attributes)
2193 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2194
2195 kfree(hotkey_keycode_map);
2196
2197 if (tp_features.hotkey) {
2198 dbg_printk(TPACPI_DBG_EXIT,
2199 "restoring original hot key mask\n");
2200 /* no short-circuit boolean operator below! */
2201 if ((hotkey_mask_set(hotkey_orig_mask) |
2202 hotkey_status_set(hotkey_orig_status)) != 0)
2203 printk(TPACPI_ERR
2204 "failed to restore hot key mask "
2205 "to BIOS defaults\n");
2206 }
2207}
2208
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002209static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002210{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002211 /* Requirements for changing the default keymaps:
2212 *
2213 * 1. Many of the keys are mapped to KEY_RESERVED for very
2214 * good reasons. Do not change them unless you have deep
2215 * knowledge on the IBM and Lenovo ThinkPad firmware for
2216 * the various ThinkPad models. The driver behaves
2217 * differently for KEY_RESERVED: such keys have their
2218 * hot key mask *unset* in mask_recommended, and also
2219 * in the initial hot key mask programmed into the
2220 * firmware at driver load time, which means the firm-
2221 * ware may react very differently if you change them to
2222 * something else;
2223 *
2224 * 2. You must be subscribed to the linux-thinkpad and
2225 * ibm-acpi-devel mailing lists, and you should read the
2226 * list archives since 2007 if you want to change the
2227 * keymaps. This requirement exists so that you will
2228 * know the past history of problems with the thinkpad-
2229 * acpi driver keymaps, and also that you will be
2230 * listening to any bug reports;
2231 *
2232 * 3. Do not send thinkpad-acpi specific patches directly to
2233 * for merging, *ever*. Send them to the linux-acpi
2234 * mailinglist for comments. Merging is to be done only
2235 * through acpi-test and the ACPI maintainer.
2236 *
2237 * If the above is too much to ask, don't change the keymap.
2238 * Ask the thinkpad-acpi maintainer to do it, instead.
2239 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002240 static u16 ibm_keycode_map[] __initdata = {
2241 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2242 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2243 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2244 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002245
2246 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002247 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2248 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2249 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002250
2251 /* brightness: firmware always reacts to them, unless
2252 * X.org did some tricks in the radeon BIOS scratch
2253 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002254 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002255 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002256
2257 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002258 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002259
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002260 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2261 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002262
2263 /* Volume: firmware always react to it and reprograms
2264 * the built-in *extra* mixer. Never map it to control
2265 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002266 KEY_RESERVED, /* 0x14: VOLUME UP */
2267 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2268 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002269
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002270 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002271
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002272 /* (assignments unknown, please report if found) */
2273 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2274 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2275 };
2276 static u16 lenovo_keycode_map[] __initdata = {
2277 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2278 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2279 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2280 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002281
2282 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002283 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2284 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2285 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002286
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002287 /* These either have to go through ACPI video, or
2288 * act like in the IBM ThinkPads, so don't ever
2289 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002290 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002291 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002292
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002293 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002294
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002295 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2296 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002297
2298 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2299 * react to it and reprograms the built-in *extra* mixer.
2300 * Never map it to control another mixer by default.
2301 *
2302 * T60?, T61, R60?, R61: firmware and EC tries to send
2303 * these over the regular keyboard, so these are no-ops,
2304 * but there are still weird bugs re. MUTE, so do not
2305 * change unless you get test reports from all Lenovo
2306 * models. May cause the BIOS to interfere with the
2307 * HDA mixer.
2308 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002309 KEY_RESERVED, /* 0x14: VOLUME UP */
2310 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2311 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002312
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002313 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002314
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002315 /* (assignments unknown, please report if found) */
2316 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2317 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2318 };
2319
2320#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2321#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2322#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2323
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002324 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002325 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002326 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002327
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002328 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2329
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002330 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002331 BUG_ON(tpacpi_inputdev->open != NULL ||
2332 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002333
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002334 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002335 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002336
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002337#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2338 mutex_init(&hotkey_thread_mutex);
2339 mutex_init(&hotkey_thread_data_mutex);
2340#endif
2341
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002342 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002343 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002344
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002345 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002346 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002347
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002348 if (!tp_features.hotkey)
2349 return 1;
2350
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002351 tpacpi_disable_brightness_delay();
2352
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002353 hotkey_dev_attributes = create_attr_set(13, NULL);
2354 if (!hotkey_dev_attributes)
2355 return -ENOMEM;
2356 res = add_many_to_attr_set(hotkey_dev_attributes,
2357 hotkey_attributes,
2358 ARRAY_SIZE(hotkey_attributes));
2359 if (res)
2360 goto err_exit;
2361
2362 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2363 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2364 for HKEY interface version 0x100 */
2365 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2366 if ((hkeyv >> 8) != 1) {
2367 printk(TPACPI_ERR "unknown version of the "
2368 "HKEY interface: 0x%x\n", hkeyv);
2369 printk(TPACPI_ERR "please report this to %s\n",
2370 TPACPI_MAIL);
2371 } else {
2372 /*
2373 * MHKV 0x100 in A31, R40, R40e,
2374 * T4x, X31, and later
2375 */
2376 tp_features.hotkey_mask = 1;
2377 }
2378 }
2379
2380 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2381 str_supported(tp_features.hotkey_mask));
2382
2383 if (tp_features.hotkey_mask) {
2384 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2385 "MHKA", "qd")) {
2386 printk(TPACPI_ERR
2387 "missing MHKA handler, "
2388 "please report this to %s\n",
2389 TPACPI_MAIL);
2390 /* FN+F12, FN+F4, FN+F3 */
2391 hotkey_all_mask = 0x080cU;
2392 }
2393 }
2394
2395 /* hotkey_source_mask *must* be zero for
2396 * the first hotkey_mask_get */
2397 res = hotkey_status_get(&hotkey_orig_status);
2398 if (res)
2399 goto err_exit;
2400
2401 if (tp_features.hotkey_mask) {
2402 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002403 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002404 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002405
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002406 hotkey_orig_mask = hotkey_mask;
2407 res = add_many_to_attr_set(
2408 hotkey_dev_attributes,
2409 hotkey_mask_attributes,
2410 ARRAY_SIZE(hotkey_mask_attributes));
2411 if (res)
2412 goto err_exit;
2413 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002414
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002415#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002416 if (tp_features.hotkey_mask) {
2417 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2418 & ~hotkey_all_mask;
2419 } else {
2420 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2421 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002422
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002423 vdbg_printk(TPACPI_DBG_INIT,
2424 "hotkey source mask 0x%08x, polling freq %d\n",
2425 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002426#endif
2427
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002428#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2429 if (dbg_wlswemul) {
2430 tp_features.hotkey_wlsw = 1;
2431 printk(TPACPI_INFO
2432 "radio switch emulation enabled\n");
2433 } else
2434#endif
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002435 /* Not all thinkpads have a hardware radio switch */
2436 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2437 tp_features.hotkey_wlsw = 1;
2438 printk(TPACPI_INFO
2439 "radio switch found; radios are %s\n",
2440 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002441 }
2442 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002443 res = add_to_attr_set(hotkey_dev_attributes,
2444 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002445
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002446 /* For X41t, X60t, X61t Tablets... */
2447 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2448 tp_features.hotkey_tablet = 1;
2449 printk(TPACPI_INFO
2450 "possible tablet mode switch found; "
2451 "ThinkPad in %s mode\n",
2452 (status & TP_HOTKEY_TABLET_MASK)?
2453 "tablet" : "laptop");
2454 res = add_to_attr_set(hotkey_dev_attributes,
2455 &dev_attr_hotkey_tablet_mode.attr);
2456 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002457
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002458 if (!res)
2459 res = register_attr_set_with_sysfs(
2460 hotkey_dev_attributes,
2461 &tpacpi_pdev->dev.kobj);
2462 if (res)
2463 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002464
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002465 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002466
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002467 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2468 GFP_KERNEL);
2469 if (!hotkey_keycode_map) {
2470 printk(TPACPI_ERR
2471 "failed to allocate memory for key map\n");
2472 res = -ENOMEM;
2473 goto err_exit;
2474 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002475
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002476 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2477 dbg_printk(TPACPI_DBG_INIT,
2478 "using Lenovo default hot key map\n");
2479 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2480 TPACPI_HOTKEY_MAP_SIZE);
2481 } else {
2482 dbg_printk(TPACPI_DBG_INIT,
2483 "using IBM default hot key map\n");
2484 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2485 TPACPI_HOTKEY_MAP_SIZE);
2486 }
2487
2488 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2489 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2490 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2491 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2492 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2493 tpacpi_inputdev->keycode = hotkey_keycode_map;
2494 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2495 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2496 set_bit(hotkey_keycode_map[i],
2497 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002498 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002499 if (i < sizeof(hotkey_reserved_mask)*8)
2500 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002501 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002502 }
2503
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002504 if (tp_features.hotkey_wlsw) {
2505 set_bit(EV_SW, tpacpi_inputdev->evbit);
2506 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2507 }
2508 if (tp_features.hotkey_tablet) {
2509 set_bit(EV_SW, tpacpi_inputdev->evbit);
2510 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002511 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002512
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002513 /* Do not issue duplicate brightness change events to
2514 * userspace */
2515 if (!tp_features.bright_acpimode)
2516 /* update bright_acpimode... */
2517 tpacpi_check_std_acpi_brightness_support();
2518
2519 if (tp_features.bright_acpimode) {
2520 printk(TPACPI_INFO
2521 "This ThinkPad has standard ACPI backlight "
2522 "brightness control, supported by the ACPI "
2523 "video driver\n");
2524 printk(TPACPI_NOTICE
2525 "Disabling thinkpad-acpi brightness events "
2526 "by default...\n");
2527
2528 /* The hotkey_reserved_mask change below is not
2529 * necessary while the keys are at KEY_RESERVED in the
2530 * default map, but better safe than sorry, leave it
2531 * here as a marker of what we have to do, especially
2532 * when we finally become able to set this at runtime
2533 * on response to X.org requests */
2534 hotkey_reserved_mask |=
2535 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2536 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002537 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002538
2539 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2540 res = hotkey_status_set(1);
2541 if (res) {
2542 hotkey_exit();
2543 return res;
2544 }
2545 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2546 & ~hotkey_reserved_mask)
2547 | hotkey_orig_mask);
2548 if (res < 0 && res != -ENXIO) {
2549 hotkey_exit();
2550 return res;
2551 }
2552
2553 dbg_printk(TPACPI_DBG_INIT,
2554 "legacy hot key reporting over procfs %s\n",
2555 (hotkey_report_mode < 2) ?
2556 "enabled" : "disabled");
2557
2558 tpacpi_inputdev->open = &hotkey_inputdev_open;
2559 tpacpi_inputdev->close = &hotkey_inputdev_close;
2560
2561 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002562 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002563 tpacpi_input_send_tabletsw();
2564
2565 return 0;
2566
2567err_exit:
2568 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2569 hotkey_dev_attributes = NULL;
2570
2571 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002572}
2573
2574static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2575{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002576 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002577 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002578 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002579 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002580 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002581
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002582 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002583 printk(TPACPI_ERR
2584 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002585 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002586 acpi_bus_generate_netlink_event(
2587 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002588 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002589 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002590 return;
2591 }
2592
2593 while (1) {
2594 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002595 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002596 return;
2597 }
2598
2599 if (hkey == 0) {
2600 /* queue empty */
2601 return;
2602 }
2603
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002604 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002605 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002606 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002607
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002608 switch (hkey >> 12) {
2609 case 1:
2610 /* 0x1000-0x1FFF: key presses */
2611 scancode = hkey & 0xfff;
2612 if (scancode > 0 && scancode < 0x21) {
2613 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002614 if (!(hotkey_source_mask & (1 << scancode))) {
2615 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002616 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002617 } else {
2618 ignore_acpi_ev = 1;
2619 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002620 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002621 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002622 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002623 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002624 case 2:
2625 /* Wakeup reason */
2626 switch (hkey) {
2627 case 0x2304: /* suspend, undock */
2628 case 0x2404: /* hibernation, undock */
2629 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2630 ignore_acpi_ev = 1;
2631 break;
2632 case 0x2305: /* suspend, bay eject */
2633 case 0x2405: /* hibernation, bay eject */
2634 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2635 ignore_acpi_ev = 1;
2636 break;
2637 default:
2638 unk_ev = 1;
2639 }
2640 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2641 printk(TPACPI_INFO
2642 "woke up due to a hot-unplug "
2643 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002644 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002645 }
2646 break;
2647 case 3:
2648 /* bay-related wakeups */
2649 if (hkey == 0x3003) {
2650 hotkey_autosleep_ack = 1;
2651 printk(TPACPI_INFO
2652 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002653 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002654 } else {
2655 unk_ev = 1;
2656 }
2657 break;
2658 case 4:
2659 /* dock-related wakeups */
2660 if (hkey == 0x4003) {
2661 hotkey_autosleep_ack = 1;
2662 printk(TPACPI_INFO
2663 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002664 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002665 } else {
2666 unk_ev = 1;
2667 }
2668 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002669 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002670 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002671 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002672 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002673 case 0x500b: /* X61t: tablet pen inserted into bay */
2674 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002675 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002676 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2677 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2678 tpacpi_input_send_tabletsw();
2679 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002680 send_acpi_ev = 0;
2681 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002682 case 0x5001:
2683 case 0x5002:
2684 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002685 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002686 break;
2687 default:
2688 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002689 }
2690 break;
2691 case 7:
2692 /* 0x7000-0x7FFF: misc */
2693 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002694 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002695 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002696 break;
2697 }
2698 /* fallthrough to default */
2699 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002700 unk_ev = 1;
2701 }
2702 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002703 printk(TPACPI_NOTICE
2704 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002705 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002706
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002707 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002708 if (!ignore_acpi_ev &&
2709 (send_acpi_ev || hotkey_report_mode < 2)) {
2710 acpi_bus_generate_proc_event(ibm->acpi->device,
2711 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002712 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002713
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002714 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002715 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002716 acpi_bus_generate_netlink_event(
2717 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002718 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002719 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002720 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002721 }
2722}
2723
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002724static void hotkey_suspend(pm_message_t state)
2725{
2726 /* Do these on suspend, we get the events on early resume! */
2727 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2728 hotkey_autosleep_ack = 0;
2729}
2730
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002731static void hotkey_resume(void)
2732{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002733 tpacpi_disable_brightness_delay();
2734
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002735 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002736 printk(TPACPI_ERR
2737 "error while trying to read hot key mask "
2738 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002739 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002740 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002741 hotkey_wakeup_reason_notify_change();
2742 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002743 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002744}
2745
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002746/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002747static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002749 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 int len = 0;
2751
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002752 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002753 len += sprintf(p + len, "status:\t\tnot supported\n");
2754 return len;
2755 }
2756
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002757 if (mutex_lock_interruptible(&hotkey_mutex))
2758 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002759 res = hotkey_status_get(&status);
2760 if (!res)
2761 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002762 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002763 if (res)
2764 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002767 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002768 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 len += sprintf(p + len,
2770 "commands:\tenable, disable, reset, <mask>\n");
2771 } else {
2772 len += sprintf(p + len, "mask:\t\tnot supported\n");
2773 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2774 }
2775
2776 return len;
2777}
2778
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002779static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002781 int res, status;
2782 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002785 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return -ENODEV;
2787
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002788 if (mutex_lock_interruptible(&hotkey_mutex))
2789 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002790
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002791 status = -1;
2792 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002793
2794 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 while ((cmd = next_cmd(&buf))) {
2796 if (strlencmp(cmd, "enable") == 0) {
2797 status = 1;
2798 } else if (strlencmp(cmd, "disable") == 0) {
2799 status = 0;
2800 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002801 status = hotkey_orig_status;
2802 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2804 /* mask set */
2805 } else if (sscanf(cmd, "%x", &mask) == 1) {
2806 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002807 } else {
2808 res = -EINVAL;
2809 goto errexit;
2810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002812 if (status != -1)
2813 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002815 if (!res && mask != hotkey_mask)
2816 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002818errexit:
2819 mutex_unlock(&hotkey_mutex);
2820 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002821}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002823static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002824 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002825 {"", 0},
2826};
2827
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002828static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002829 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002830 .notify = hotkey_notify,
2831 .handle = &hkey_handle,
2832 .type = ACPI_DEVICE_NOTIFY,
2833};
2834
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002835static struct ibm_struct hotkey_driver_data = {
2836 .name = "hotkey",
2837 .read = hotkey_read,
2838 .write = hotkey_write,
2839 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002840 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002841 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002842 .acpi = &ibm_hotkey_acpidriver,
2843};
2844
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002845/*************************************************************************
2846 * Bluetooth subdriver
2847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002849enum {
2850 /* ACPI GBDC/SBDC bits */
2851 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2852 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002853 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2854 off / last state */
2855};
2856
2857enum {
2858 /* ACPI \BLTH commands */
2859 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2860 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2861 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2862 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2863 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002864};
2865
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002866static struct rfkill *tpacpi_bluetooth_rfkill;
2867
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002868static void bluetooth_suspend(pm_message_t state)
2869{
2870 /* Try to make sure radio will resume powered off */
2871 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2872 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2873}
2874
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002875static int bluetooth_get_radiosw(void)
2876{
2877 int status;
2878
2879 if (!tp_features.bluetooth)
2880 return -ENODEV;
2881
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002882 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2883 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002884 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002885
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002886#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2887 if (dbg_bluetoothemul)
2888 return (tpacpi_bluetooth_emulstate) ?
2889 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2890#endif
2891
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002892 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2893 return -EIO;
2894
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002895 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2896 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002897}
2898
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002899static void bluetooth_update_rfk(void)
2900{
2901 int status;
2902
2903 if (!tpacpi_bluetooth_rfkill)
2904 return;
2905
2906 status = bluetooth_get_radiosw();
2907 if (status < 0)
2908 return;
2909 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2910}
2911
2912static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002913{
2914 int status;
2915
2916 if (!tp_features.bluetooth)
2917 return -ENODEV;
2918
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002919 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2920 * reason to risk weird behaviour. */
2921 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2922 && radio_on)
2923 return -EPERM;
2924
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02002925#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2926 if (dbg_bluetoothemul) {
2927 tpacpi_bluetooth_emulstate = !!radio_on;
2928 if (update_rfk)
2929 bluetooth_update_rfk();
2930 return 0;
2931 }
2932#endif
2933
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002934 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002935 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002936 status = TP_ACPI_BLUETOOTH_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002937 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02002938 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002939 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2940 return -EIO;
2941
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002942 if (update_rfk)
2943 bluetooth_update_rfk();
2944
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002945 return 0;
2946}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002947
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002948/* sysfs bluetooth enable ---------------------------------------------- */
2949static ssize_t bluetooth_enable_show(struct device *dev,
2950 struct device_attribute *attr,
2951 char *buf)
2952{
2953 int status;
2954
2955 status = bluetooth_get_radiosw();
2956 if (status < 0)
2957 return status;
2958
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002959 return snprintf(buf, PAGE_SIZE, "%d\n",
2960 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002961}
2962
2963static ssize_t bluetooth_enable_store(struct device *dev,
2964 struct device_attribute *attr,
2965 const char *buf, size_t count)
2966{
2967 unsigned long t;
2968 int res;
2969
2970 if (parse_strtoul(buf, 1, &t))
2971 return -EINVAL;
2972
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002973 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002974
2975 return (res) ? res : count;
2976}
2977
2978static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002979 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002980 bluetooth_enable_show, bluetooth_enable_store);
2981
2982/* --------------------------------------------------------------------- */
2983
2984static struct attribute *bluetooth_attributes[] = {
2985 &dev_attr_bluetooth_enable.attr,
2986 NULL
2987};
2988
2989static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002990 .attrs = bluetooth_attributes,
2991};
2992
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002993static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2994{
2995 int bts = bluetooth_get_radiosw();
2996
2997 if (bts < 0)
2998 return bts;
2999
3000 *state = bts;
3001 return 0;
3002}
3003
3004static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3005{
3006 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3007}
3008
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003009static void bluetooth_shutdown(void)
3010{
3011 /* Order firmware to save current state to NVRAM */
3012 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3013 TP_ACPI_BLTH_SAVE_STATE))
3014 printk(TPACPI_NOTICE
3015 "failed to save bluetooth state to NVRAM\n");
3016}
3017
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003018static void bluetooth_exit(void)
3019{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003020 bluetooth_shutdown();
3021
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003022 if (tpacpi_bluetooth_rfkill)
3023 rfkill_unregister(tpacpi_bluetooth_rfkill);
3024
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003025 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3026 &bluetooth_attr_group);
3027}
3028
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003029static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003031 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003032 int status = 0;
3033
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003034 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3035
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003036 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003037
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003038 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3039 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003040 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003041 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003043 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3044 str_supported(tp_features.bluetooth),
3045 status);
3046
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003047#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3048 if (dbg_bluetoothemul) {
3049 tp_features.bluetooth = 1;
3050 printk(TPACPI_INFO
3051 "bluetooth switch emulation enabled\n");
3052 } else
3053#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003054 if (tp_features.bluetooth &&
3055 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3056 /* no bluetooth hardware present in system */
3057 tp_features.bluetooth = 0;
3058 dbg_printk(TPACPI_DBG_INIT,
3059 "bluetooth hardware not installed\n");
3060 }
3061
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003062 if (!tp_features.bluetooth)
3063 return 1;
3064
3065 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003066 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003067 if (res)
3068 return res;
3069
3070 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3071 &tpacpi_bluetooth_rfkill,
3072 RFKILL_TYPE_BLUETOOTH,
3073 "tpacpi_bluetooth_sw",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003074 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003075 tpacpi_bluetooth_rfk_set,
3076 tpacpi_bluetooth_rfk_get);
3077 if (res) {
3078 bluetooth_exit();
3079 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003080 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003081
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
3084
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003085/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003086static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
3088 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003089 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003091 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003094 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003095 (status == RFKILL_STATE_UNBLOCKED) ?
3096 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 len += sprintf(p + len, "commands:\tenable, disable\n");
3098 }
3099
3100 return len;
3101}
3102
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003103static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003107 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003108 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 while ((cmd = next_cmd(&buf))) {
3111 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003112 bluetooth_set_radiosw(1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003114 bluetooth_set_radiosw(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 } else
3116 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 }
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return 0;
3120}
3121
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003122static struct ibm_struct bluetooth_driver_data = {
3123 .name = "bluetooth",
3124 .read = bluetooth_read,
3125 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003126 .exit = bluetooth_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003127 .suspend = bluetooth_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003128 .shutdown = bluetooth_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003129};
3130
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003131/*************************************************************************
3132 * Wan subdriver
3133 */
3134
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003135enum {
3136 /* ACPI GWAN/SWAN bits */
3137 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3138 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003139 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3140 off / last state */
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003141};
3142
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003143static struct rfkill *tpacpi_wan_rfkill;
3144
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003145static void wan_suspend(pm_message_t state)
3146{
3147 /* Try to make sure radio will resume powered off */
3148 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3149 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3150}
3151
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003152static int wan_get_radiosw(void)
3153{
3154 int status;
3155
3156 if (!tp_features.wan)
3157 return -ENODEV;
3158
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003159 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3160 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003161 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003162
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003163#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3164 if (dbg_wwanemul)
3165 return (tpacpi_wwan_emulstate) ?
3166 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3167#endif
3168
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003169 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3170 return -EIO;
3171
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003172 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3173 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003174}
3175
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003176static void wan_update_rfk(void)
3177{
3178 int status;
3179
3180 if (!tpacpi_wan_rfkill)
3181 return;
3182
3183 status = wan_get_radiosw();
3184 if (status < 0)
3185 return;
3186 rfkill_force_state(tpacpi_wan_rfkill, status);
3187}
3188
3189static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003190{
3191 int status;
3192
3193 if (!tp_features.wan)
3194 return -ENODEV;
3195
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03003196 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3197 * reason to risk weird behaviour. */
3198 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3199 && radio_on)
3200 return -EPERM;
3201
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003202#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3203 if (dbg_wwanemul) {
3204 tpacpi_wwan_emulstate = !!radio_on;
3205 if (update_rfk)
3206 wan_update_rfk();
3207 return 0;
3208 }
3209#endif
3210
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003211 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003212 if (radio_on)
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003213 status = TP_ACPI_WANCARD_RADIOSSW;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003214 else
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003215 status = 0;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003216 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3217 return -EIO;
3218
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003219 if (update_rfk)
3220 wan_update_rfk();
3221
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003222 return 0;
3223}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003224
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003225/* sysfs wan enable ---------------------------------------------------- */
3226static ssize_t wan_enable_show(struct device *dev,
3227 struct device_attribute *attr,
3228 char *buf)
3229{
3230 int status;
3231
3232 status = wan_get_radiosw();
3233 if (status < 0)
3234 return status;
3235
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003236 return snprintf(buf, PAGE_SIZE, "%d\n",
3237 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003238}
3239
3240static ssize_t wan_enable_store(struct device *dev,
3241 struct device_attribute *attr,
3242 const char *buf, size_t count)
3243{
3244 unsigned long t;
3245 int res;
3246
3247 if (parse_strtoul(buf, 1, &t))
3248 return -EINVAL;
3249
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003250 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003251
3252 return (res) ? res : count;
3253}
3254
3255static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03003256 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003257 wan_enable_show, wan_enable_store);
3258
3259/* --------------------------------------------------------------------- */
3260
3261static struct attribute *wan_attributes[] = {
3262 &dev_attr_wan_enable.attr,
3263 NULL
3264};
3265
3266static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003267 .attrs = wan_attributes,
3268};
3269
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003270static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3271{
3272 int wans = wan_get_radiosw();
3273
3274 if (wans < 0)
3275 return wans;
3276
3277 *state = wans;
3278 return 0;
3279}
3280
3281static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3282{
3283 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3284}
3285
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003286static void wan_shutdown(void)
3287{
3288 /* Order firmware to save current state to NVRAM */
3289 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3290 TP_ACPI_WGSV_SAVE_STATE))
3291 printk(TPACPI_NOTICE
3292 "failed to save WWAN state to NVRAM\n");
3293}
3294
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003295static void wan_exit(void)
3296{
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003297 wan_shutdown();
3298
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003299 if (tpacpi_wan_rfkill)
3300 rfkill_unregister(tpacpi_wan_rfkill);
3301
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003302 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3303 &wan_attr_group);
3304}
3305
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003306static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003307{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003308 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003309 int status = 0;
3310
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003311 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3312
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003313 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003314
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003315 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003316 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003317
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003318 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3319 str_supported(tp_features.wan),
3320 status);
3321
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02003322#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3323 if (dbg_wwanemul) {
3324 tp_features.wan = 1;
3325 printk(TPACPI_INFO
3326 "wwan switch emulation enabled\n");
3327 } else
3328#endif
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003329 if (tp_features.wan &&
3330 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3331 /* no wan hardware present in system */
3332 tp_features.wan = 0;
3333 dbg_printk(TPACPI_DBG_INIT,
3334 "wan hardware not installed\n");
3335 }
3336
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003337 if (!tp_features.wan)
3338 return 1;
3339
3340 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003341 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003342 if (res)
3343 return res;
3344
3345 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3346 &tpacpi_wan_rfkill,
3347 RFKILL_TYPE_WWAN,
3348 "tpacpi_wwan_sw",
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003349 true,
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003350 tpacpi_wan_rfk_set,
3351 tpacpi_wan_rfk_get);
3352 if (res) {
3353 wan_exit();
3354 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003355 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003356
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003357 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003358}
3359
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003360/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003361static int wan_read(char *p)
3362{
3363 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003364 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003365
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003366 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003367 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003368 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003369 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003370 (status == RFKILL_STATE_UNBLOCKED) ?
3371 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003372 len += sprintf(p + len, "commands:\tenable, disable\n");
3373 }
3374
3375 return len;
3376}
3377
3378static int wan_write(char *buf)
3379{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003380 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003381
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003382 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003383 return -ENODEV;
3384
3385 while ((cmd = next_cmd(&buf))) {
3386 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003387 wan_set_radiosw(1, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003388 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003389 wan_set_radiosw(0, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003390 } else
3391 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003392 }
3393
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003394 return 0;
3395}
3396
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003397static struct ibm_struct wan_driver_data = {
3398 .name = "wan",
3399 .read = wan_read,
3400 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003401 .exit = wan_exit,
Henrique de Moraes Holschuh153f8222009-01-11 03:01:01 -02003402 .suspend = wan_suspend,
Henrique de Moraes Holschuh90d9d3c2009-01-11 03:01:02 -02003403 .shutdown = wan_shutdown,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003404};
3405
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003406/*************************************************************************
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02003407 * UWB subdriver
3408 */
3409
3410enum {
3411 /* ACPI GUWB/SUWB bits */
3412 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3413 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3414};
3415
3416static struct rfkill *tpacpi_uwb_rfkill;
3417
3418static int uwb_get_radiosw(void)
3419{
3420 int status;
3421
3422 if (!tp_features.uwb)
3423 return -ENODEV;
3424
3425 /* WLSW overrides UWB in firmware/hardware, reflect that */
3426 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3427 return RFKILL_STATE_HARD_BLOCKED;
3428
3429#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3430 if (dbg_uwbemul)
3431 return (tpacpi_uwb_emulstate) ?
3432 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3433#endif
3434
3435 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3436 return -EIO;
3437
3438 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3439 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3440}
3441
3442static void uwb_update_rfk(void)
3443{
3444 int status;
3445
3446 if (!tpacpi_uwb_rfkill)
3447 return;
3448
3449 status = uwb_get_radiosw();
3450 if (status < 0)
3451 return;
3452 rfkill_force_state(tpacpi_uwb_rfkill, status);
3453}
3454
3455static int uwb_set_radiosw(int radio_on, int update_rfk)
3456{
3457 int status;
3458
3459 if (!tp_features.uwb)
3460 return -ENODEV;
3461
3462 /* WLSW overrides UWB in firmware/hardware, but there is no
3463 * reason to risk weird behaviour. */
3464 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3465 && radio_on)
3466 return -EPERM;
3467
3468#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3469 if (dbg_uwbemul) {
3470 tpacpi_uwb_emulstate = !!radio_on;
3471 if (update_rfk)
3472 uwb_update_rfk();
3473 return 0;
3474 }
3475#endif
3476
3477 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3478 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3479 return -EIO;
3480
3481 if (update_rfk)
3482 uwb_update_rfk();
3483
3484 return 0;
3485}
3486
3487/* --------------------------------------------------------------------- */
3488
3489static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3490{
3491 int uwbs = uwb_get_radiosw();
3492
3493 if (uwbs < 0)
3494 return uwbs;
3495
3496 *state = uwbs;
3497 return 0;
3498}
3499
3500static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3501{
3502 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3503}
3504
3505static void uwb_exit(void)
3506{
3507 if (tpacpi_uwb_rfkill)
3508 rfkill_unregister(tpacpi_uwb_rfkill);
3509}
3510
3511static int __init uwb_init(struct ibm_init_struct *iibm)
3512{
3513 int res;
3514 int status = 0;
3515
3516 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3517
3518 TPACPI_ACPIHANDLE_INIT(hkey);
3519
3520 tp_features.uwb = hkey_handle &&
3521 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3522
3523 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3524 str_supported(tp_features.uwb),
3525 status);
3526
3527#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3528 if (dbg_uwbemul) {
3529 tp_features.uwb = 1;
3530 printk(TPACPI_INFO
3531 "uwb switch emulation enabled\n");
3532 } else
3533#endif
3534 if (tp_features.uwb &&
3535 !(status & TP_ACPI_UWB_HWPRESENT)) {
3536 /* no uwb hardware present in system */
3537 tp_features.uwb = 0;
3538 dbg_printk(TPACPI_DBG_INIT,
3539 "uwb hardware not installed\n");
3540 }
3541
3542 if (!tp_features.uwb)
3543 return 1;
3544
3545 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3546 &tpacpi_uwb_rfkill,
3547 RFKILL_TYPE_UWB,
3548 "tpacpi_uwb_sw",
3549 false,
3550 tpacpi_uwb_rfk_set,
3551 tpacpi_uwb_rfk_get);
3552
3553 return res;
3554}
3555
3556static struct ibm_struct uwb_driver_data = {
3557 .name = "uwb",
3558 .exit = uwb_exit,
3559 .flags.experimental = 1,
3560};
3561
3562/*************************************************************************
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003563 * Video subdriver
3564 */
3565
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003566#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3567
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003568enum video_access_mode {
3569 TPACPI_VIDEO_NONE = 0,
3570 TPACPI_VIDEO_570, /* 570 */
3571 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3572 TPACPI_VIDEO_NEW, /* all others */
3573};
3574
3575enum { /* video status flags, based on VIDEO_570 */
3576 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3577 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3578 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3579};
3580
3581enum { /* TPACPI_VIDEO_570 constants */
3582 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3583 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3584 * video_status_flags */
3585 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3586 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3587};
3588
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003589static enum video_access_mode video_supported;
3590static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003591
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003592static int video_autosw_get(void);
3593static int video_autosw_set(int enable);
3594
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003595TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003596
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003597static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003599 int ivga;
3600
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003601 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3602
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003603 TPACPI_ACPIHANDLE_INIT(vid);
3604 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003605
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003606 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3607 /* G41, assume IVGA doesn't change */
3608 vid_handle = vid2_handle;
3609
3610 if (!vid_handle)
3611 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003612 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003613 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3614 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003615 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003616 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3617 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003618 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003619 else
3620 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003621 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003623 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3624 str_supported(video_supported != TPACPI_VIDEO_NONE),
3625 video_supported);
3626
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003627 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003630static void video_exit(void)
3631{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003632 dbg_printk(TPACPI_DBG_EXIT,
3633 "restoring original video autoswitch mode\n");
3634 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003635 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003636 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003637}
3638
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003639static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
3641 int status = 0;
3642 int i;
3643
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003644 switch (video_supported) {
3645 case TPACPI_VIDEO_570:
3646 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3647 TP_ACPI_VIDEO_570_PHSCMD))
3648 return -EIO;
3649 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3650 break;
3651 case TPACPI_VIDEO_770:
3652 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3653 return -EIO;
3654 if (i)
3655 status |= TP_ACPI_VIDEO_S_LCD;
3656 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3657 return -EIO;
3658 if (i)
3659 status |= TP_ACPI_VIDEO_S_CRT;
3660 break;
3661 case TPACPI_VIDEO_NEW:
3662 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3663 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3664 return -EIO;
3665 if (i)
3666 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003668 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3669 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3670 return -EIO;
3671 if (i)
3672 status |= TP_ACPI_VIDEO_S_LCD;
3673 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3674 return -EIO;
3675 if (i)
3676 status |= TP_ACPI_VIDEO_S_DVI;
3677 break;
3678 default:
3679 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
3682 return status;
3683}
3684
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003685static int video_outputsw_set(int status)
3686{
3687 int autosw;
3688 int res = 0;
3689
3690 switch (video_supported) {
3691 case TPACPI_VIDEO_570:
3692 res = acpi_evalf(NULL, NULL,
3693 "\\_SB.PHS2", "vdd",
3694 TP_ACPI_VIDEO_570_PHS2CMD,
3695 status | TP_ACPI_VIDEO_570_PHS2SET);
3696 break;
3697 case TPACPI_VIDEO_770:
3698 autosw = video_autosw_get();
3699 if (autosw < 0)
3700 return autosw;
3701
3702 res = video_autosw_set(1);
3703 if (res)
3704 return res;
3705 res = acpi_evalf(vid_handle, NULL,
3706 "ASWT", "vdd", status * 0x100, 0);
3707 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003708 printk(TPACPI_ERR
3709 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003710 return -EIO;
3711 }
3712 break;
3713 case TPACPI_VIDEO_NEW:
3714 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003715 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003716 break;
3717 default:
3718 return -ENOSYS;
3719 }
3720
3721 return (res)? 0 : -EIO;
3722}
3723
3724static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003726 int autosw = 0;
3727
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003728 switch (video_supported) {
3729 case TPACPI_VIDEO_570:
3730 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3731 return -EIO;
3732 break;
3733 case TPACPI_VIDEO_770:
3734 case TPACPI_VIDEO_NEW:
3735 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3736 return -EIO;
3737 break;
3738 default:
3739 return -ENOSYS;
3740 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003741
3742 return autosw & 1;
3743}
3744
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003745static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003746{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003747 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003748 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003749 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003750}
3751
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003752static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003753{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003754 int autosw = video_autosw_get();
3755 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003756
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003757 if (autosw < 0)
3758 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003759
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003760 switch (video_supported) {
3761 case TPACPI_VIDEO_570:
3762 res = video_autosw_set(1);
3763 if (res)
3764 return res;
3765 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3766 break;
3767 case TPACPI_VIDEO_770:
3768 case TPACPI_VIDEO_NEW:
3769 res = video_autosw_set(1);
3770 if (res)
3771 return res;
3772 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3773 break;
3774 default:
3775 return -ENOSYS;
3776 }
3777 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003778 printk(TPACPI_ERR
3779 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003780 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003781 }
3782
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003783 return (res)? 0 : -EIO;
3784}
3785
3786static int video_expand_toggle(void)
3787{
3788 switch (video_supported) {
3789 case TPACPI_VIDEO_570:
3790 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3791 0 : -EIO;
3792 case TPACPI_VIDEO_770:
3793 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3794 0 : -EIO;
3795 case TPACPI_VIDEO_NEW:
3796 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3797 0 : -EIO;
3798 default:
3799 return -ENOSYS;
3800 }
3801 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003802}
3803
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003804static int video_read(char *p)
3805{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003806 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003807 int len = 0;
3808
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003809 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003810 len += sprintf(p + len, "status:\t\tnot supported\n");
3811 return len;
3812 }
3813
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003814 status = video_outputsw_get();
3815 if (status < 0)
3816 return status;
3817
3818 autosw = video_autosw_get();
3819 if (autosw < 0)
3820 return autosw;
3821
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003822 len += sprintf(p + len, "status:\t\tsupported\n");
3823 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3824 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003825 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003826 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3827 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3828 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3829 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003830 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003831 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3832 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3833 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3834
3835 return len;
3836}
3837
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003838static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
3840 char *cmd;
3841 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003842 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003844 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003845 return -ENODEV;
3846
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003847 enable = 0;
3848 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
3850 while ((cmd = next_cmd(&buf))) {
3851 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003852 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003854 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003856 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003858 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003859 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003860 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003861 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003862 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003863 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003864 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003866 res = video_autosw_set(1);
3867 if (res)
3868 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003870 res = video_autosw_set(0);
3871 if (res)
3872 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003874 res = video_outputsw_cycle();
3875 if (res)
3876 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003878 res = video_expand_toggle();
3879 if (res)
3880 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 } else
3882 return -EINVAL;
3883 }
3884
3885 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003886 status = video_outputsw_get();
3887 if (status < 0)
3888 return status;
3889 res = video_outputsw_set((status & ~disable) | enable);
3890 if (res)
3891 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
3893
3894 return 0;
3895}
3896
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003897static struct ibm_struct video_driver_data = {
3898 .name = "video",
3899 .read = video_read,
3900 .write = video_write,
3901 .exit = video_exit,
3902};
3903
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003904#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3905
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003906/*************************************************************************
3907 * Light (thinklight) subdriver
3908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003910TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3911TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003912
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003913static int light_get_status(void)
3914{
3915 int status = 0;
3916
3917 if (tp_features.light_status) {
3918 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3919 return -EIO;
3920 return (!!status);
3921 }
3922
3923 return -ENXIO;
3924}
3925
3926static int light_set_status(int status)
3927{
3928 int rc;
3929
3930 if (tp_features.light) {
3931 if (cmos_handle) {
3932 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3933 (status)?
3934 TP_CMOS_THINKLIGHT_ON :
3935 TP_CMOS_THINKLIGHT_OFF);
3936 } else {
3937 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3938 (status)? 1 : 0);
3939 }
3940 return (rc)? 0 : -EIO;
3941 }
3942
3943 return -ENXIO;
3944}
3945
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003946static void light_set_status_worker(struct work_struct *work)
3947{
3948 struct tpacpi_led_classdev *data =
3949 container_of(work, struct tpacpi_led_classdev, work);
3950
3951 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3952 light_set_status((data->new_brightness != LED_OFF));
3953}
3954
3955static void light_sysfs_set(struct led_classdev *led_cdev,
3956 enum led_brightness brightness)
3957{
3958 struct tpacpi_led_classdev *data =
3959 container_of(led_cdev,
3960 struct tpacpi_led_classdev,
3961 led_classdev);
3962 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003963 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003964}
3965
3966static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3967{
3968 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3969}
3970
3971static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3972 .led_classdev = {
3973 .name = "tpacpi::thinklight",
3974 .brightness_set = &light_sysfs_set,
3975 .brightness_get = &light_sysfs_get,
3976 }
3977};
3978
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003979static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003981 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003982
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003983 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3984
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003985 TPACPI_ACPIHANDLE_INIT(ledb);
3986 TPACPI_ACPIHANDLE_INIT(lght);
3987 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003988 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003989
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003990 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003991 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003992
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003993 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003994 /* light status not supported on
3995 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003996 tp_features.light_status =
3997 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003999 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4000 str_supported(tp_features.light),
4001 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004002
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004003 if (!tp_features.light)
4004 return 1;
4005
4006 rc = led_classdev_register(&tpacpi_pdev->dev,
4007 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004008
4009 if (rc < 0) {
4010 tp_features.light = 0;
4011 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004012 } else {
4013 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004014 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03004015
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004016 return rc;
4017}
4018
4019static void light_exit(void)
4020{
4021 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4022 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004023 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004026static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
4028 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004029 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004031 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004032 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004033 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004034 len += sprintf(p + len, "status:\t\tunknown\n");
4035 len += sprintf(p + len, "commands:\ton, off\n");
4036 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004037 status = light_get_status();
4038 if (status < 0)
4039 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004041 len += sprintf(p + len, "commands:\ton, off\n");
4042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
4044 return len;
4045}
4046
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004047static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004050 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004051
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004052 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004053 return -ENODEV;
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 while ((cmd = next_cmd(&buf))) {
4056 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004057 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004059 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 } else
4061 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 }
4063
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004064 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065}
4066
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004067static struct ibm_struct light_driver_data = {
4068 .name = "light",
4069 .read = light_read,
4070 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03004071 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004072};
4073
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004074/*************************************************************************
4075 * Dock subdriver
4076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004078#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004079
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004080static void dock_notify(struct ibm_struct *ibm, u32 event);
4081static int dock_read(char *p);
4082static int dock_write(char *buf);
4083
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004084TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004085 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4086 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4087 "\\_SB.PCI.ISA.SLCE", /* 570 */
4088 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4089
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004090/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004091TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004092
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004093static const struct acpi_device_id ibm_pci_device_ids[] = {
4094 {PCI_ROOT_HID_STRING, 0},
4095 {"", 0},
4096};
4097
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004098static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4099 {
4100 .notify = dock_notify,
4101 .handle = &dock_handle,
4102 .type = ACPI_SYSTEM_NOTIFY,
4103 },
4104 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03004105 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4106 * We just use it to get notifications of dock hotplug
4107 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004108 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004109 .notify = dock_notify,
4110 .handle = &pci_handle,
4111 .type = ACPI_SYSTEM_NOTIFY,
4112 },
4113};
4114
4115static struct ibm_struct dock_driver_data[2] = {
4116 {
4117 .name = "dock",
4118 .read = dock_read,
4119 .write = dock_write,
4120 .acpi = &ibm_dock_acpidriver[0],
4121 },
4122 {
4123 .name = "dock",
4124 .acpi = &ibm_dock_acpidriver[1],
4125 },
4126};
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128#define dock_docked() (_sta(dock_handle) & 1)
4129
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004130static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004131{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004132 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4133
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004134 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004135
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004136 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4137 str_supported(dock_handle != NULL));
4138
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004139 return (dock_handle)? 0 : 1;
4140}
4141
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004142static int __init dock_init2(struct ibm_init_struct *iibm)
4143{
4144 int dock2_needed;
4145
4146 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4147
4148 if (dock_driver_data[0].flags.acpi_driver_registered &&
4149 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004150 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004151 dock2_needed = (pci_handle != NULL);
4152 vdbg_printk(TPACPI_DBG_INIT,
4153 "dock PCI handler for the TP 570 is %s\n",
4154 str_supported(dock2_needed));
4155 } else {
4156 vdbg_printk(TPACPI_DBG_INIT,
4157 "dock subdriver part 2 not required\n");
4158 dock2_needed = 0;
4159 }
4160
4161 return (dock2_needed)? 0 : 1;
4162}
4163
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004164static void dock_notify(struct ibm_struct *ibm, u32 event)
4165{
4166 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02004167 int pci = ibm->acpi->hid && ibm->acpi->device &&
4168 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004169 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004170
4171 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004172 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004173 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08004174 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004175 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004176 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004177 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004178 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004179 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08004180 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004181 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004182 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004183 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08004184 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004185 }
Len Brown14e04fb2007-08-23 15:20:26 -04004186 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004187 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004188 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004189 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004190}
4191
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004192static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
4194 int len = 0;
4195 int docked = dock_docked();
4196
4197 if (!dock_handle)
4198 len += sprintf(p + len, "status:\t\tnot supported\n");
4199 else if (!docked)
4200 len += sprintf(p + len, "status:\t\tundocked\n");
4201 else {
4202 len += sprintf(p + len, "status:\t\tdocked\n");
4203 len += sprintf(p + len, "commands:\tdock, undock\n");
4204 }
4205
4206 return len;
4207}
4208
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004209static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
4211 char *cmd;
4212
4213 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004214 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
4216 while ((cmd = next_cmd(&buf))) {
4217 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004218 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4219 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return -EIO;
4221 } else if (strlencmp(cmd, "dock") == 0) {
4222 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4223 return -EIO;
4224 } else
4225 return -EINVAL;
4226 }
4227
4228 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004231#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004233/*************************************************************************
4234 * Bay subdriver
4235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004237#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004238
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004239TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004240 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4241 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4242 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4243 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004244TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004245 "_EJ0", /* all others */
4246 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004247TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004248 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4249 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004250TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004251 "_EJ0", /* 770x */
4252 ); /* all others */
4253
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004254static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004256 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4257
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004258 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004259 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004260 TPACPI_ACPIHANDLE_INIT(bay_ej);
4261 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004262 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004263 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004264
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004265 tp_features.bay_status = bay_handle &&
4266 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4267 tp_features.bay_status2 = bay2_handle &&
4268 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004269
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004270 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4271 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4272 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4273 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004275 vdbg_printk(TPACPI_DBG_INIT,
4276 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004277 str_supported(tp_features.bay_status),
4278 str_supported(tp_features.bay_eject),
4279 str_supported(tp_features.bay_status2),
4280 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004281
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004282 return (tp_features.bay_status || tp_features.bay_eject ||
4283 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284}
4285
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004286static void bay_notify(struct ibm_struct *ibm, u32 event)
4287{
Len Brown14e04fb2007-08-23 15:20:26 -04004288 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08004289 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02004290 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08004291 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004292}
4293
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004294#define bay_occupied(b) (_sta(b##_handle) & 1)
4295
4296static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
4298 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004299 int occupied = bay_occupied(bay);
4300 int occupied2 = bay_occupied(bay2);
4301 int eject, eject2;
4302
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004303 len += sprintf(p + len, "status:\t\t%s\n",
4304 tp_features.bay_status ?
4305 (occupied ? "occupied" : "unoccupied") :
4306 "not supported");
4307 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004308 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4309 "occupied" : "unoccupied");
4310
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004311 eject = tp_features.bay_eject && occupied;
4312 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004313
4314 if (eject && eject2)
4315 len += sprintf(p + len, "commands:\teject, eject2\n");
4316 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004318 else if (eject2)
4319 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
4321 return len;
4322}
4323
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004324static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
4326 char *cmd;
4327
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004328 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004329 return -ENODEV;
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004332 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004333 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4334 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004335 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004336 strlencmp(cmd, "eject2") == 0) {
4337 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return -EIO;
4339 } else
4340 return -EINVAL;
4341 }
4342
4343 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004344}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004345
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004346static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4347 .notify = bay_notify,
4348 .handle = &bay_handle,
4349 .type = ACPI_SYSTEM_NOTIFY,
4350};
4351
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004352static struct ibm_struct bay_driver_data = {
4353 .name = "bay",
4354 .read = bay_read,
4355 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004356 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004357};
4358
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004359#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004361/*************************************************************************
4362 * CMOS subdriver
4363 */
4364
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004365/* sysfs cmos_command -------------------------------------------------- */
4366static ssize_t cmos_command_store(struct device *dev,
4367 struct device_attribute *attr,
4368 const char *buf, size_t count)
4369{
4370 unsigned long cmos_cmd;
4371 int res;
4372
4373 if (parse_strtoul(buf, 21, &cmos_cmd))
4374 return -EINVAL;
4375
4376 res = issue_thinkpad_cmos_command(cmos_cmd);
4377 return (res)? res : count;
4378}
4379
4380static struct device_attribute dev_attr_cmos_command =
4381 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4382
4383/* --------------------------------------------------------------------- */
4384
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004385static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004386{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004387 int res;
4388
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004389 vdbg_printk(TPACPI_DBG_INIT,
4390 "initializing cmos commands subdriver\n");
4391
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004392 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004393
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004394 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4395 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004396
4397 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4398 if (res)
4399 return res;
4400
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004401 return (cmos_handle)? 0 : 1;
4402}
4403
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004404static void cmos_exit(void)
4405{
4406 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4407}
4408
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004409static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
4411 int len = 0;
4412
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004413 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4414 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 if (!cmos_handle)
4416 len += sprintf(p + len, "status:\t\tnot supported\n");
4417 else {
4418 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004419 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 }
4421
4422 return len;
4423}
4424
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004425static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
4427 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004428 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004431 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4432 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 /* cmos_cmd set */
4434 } else
4435 return -EINVAL;
4436
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004437 res = issue_thinkpad_cmos_command(cmos_cmd);
4438 if (res)
4439 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 }
4441
4442 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004443}
4444
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004445static struct ibm_struct cmos_driver_data = {
4446 .name = "cmos",
4447 .read = cmos_read,
4448 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03004449 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004450};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004451
4452/*************************************************************************
4453 * LED subdriver
4454 */
4455
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004456enum led_access_mode {
4457 TPACPI_LED_NONE = 0,
4458 TPACPI_LED_570, /* 570 */
4459 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4460 TPACPI_LED_NEW, /* all others */
4461};
4462
4463enum { /* For TPACPI_LED_OLD */
4464 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4465 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4466 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4467};
4468
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004469enum led_status_t {
4470 TPACPI_LED_OFF = 0,
4471 TPACPI_LED_ON,
4472 TPACPI_LED_BLINK,
4473};
4474
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004475static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004476
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004477TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004478 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4479 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004480 "LED", /* all others */
4481 ); /* R30, R31 */
4482
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004483#define TPACPI_LED_NUMLEDS 8
4484static struct tpacpi_led_classdev *tpacpi_leds;
4485static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004486static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004487 /* there's a limit of 19 chars + NULL before 2.6.26 */
4488 "tpacpi::power",
4489 "tpacpi:orange:batt",
4490 "tpacpi:green:batt",
4491 "tpacpi::dock_active",
4492 "tpacpi::bay_active",
4493 "tpacpi::dock_batt",
4494 "tpacpi::unknown_led",
4495 "tpacpi::standby",
4496};
4497
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004498static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004499{
4500 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004501 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004502
4503 switch (led_supported) {
4504 case TPACPI_LED_570:
4505 if (!acpi_evalf(ec_handle,
4506 &status, "GLED", "dd", 1 << led))
4507 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004508 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004509 TPACPI_LED_OFF :
4510 ((status == 1)?
4511 TPACPI_LED_ON :
4512 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004513 tpacpi_led_state_cache[led] = led_s;
4514 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004515 default:
4516 return -ENXIO;
4517 }
4518
4519 /* not reached */
4520}
4521
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004522static int led_set_status(const unsigned int led,
4523 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004524{
4525 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004526 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4527 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004528
4529 int rc = 0;
4530
4531 switch (led_supported) {
4532 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004533 /* 570 */
4534 if (led > 7)
4535 return -EINVAL;
4536 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4537 (1 << led), led_sled_arg1[ledstatus]))
4538 rc = -EIO;
4539 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004540 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004541 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4542 if (led > 7)
4543 return -EINVAL;
4544 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4545 if (rc >= 0)
4546 rc = ec_write(TPACPI_LED_EC_HLBL,
4547 (ledstatus == TPACPI_LED_BLINK) << led);
4548 if (rc >= 0)
4549 rc = ec_write(TPACPI_LED_EC_HLCL,
4550 (ledstatus != TPACPI_LED_OFF) << led);
4551 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004552 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004553 /* all others */
4554 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4555 led, led_led_arg1[ledstatus]))
4556 rc = -EIO;
4557 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004558 default:
4559 rc = -ENXIO;
4560 }
4561
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004562 if (!rc)
4563 tpacpi_led_state_cache[led] = ledstatus;
4564
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004565 return rc;
4566}
4567
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004568static void led_sysfs_set_status(unsigned int led,
4569 enum led_brightness brightness)
4570{
4571 led_set_status(led,
4572 (brightness == LED_OFF) ?
4573 TPACPI_LED_OFF :
4574 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4575 TPACPI_LED_BLINK : TPACPI_LED_ON);
4576}
4577
4578static void led_set_status_worker(struct work_struct *work)
4579{
4580 struct tpacpi_led_classdev *data =
4581 container_of(work, struct tpacpi_led_classdev, work);
4582
4583 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4584 led_sysfs_set_status(data->led, data->new_brightness);
4585}
4586
4587static void led_sysfs_set(struct led_classdev *led_cdev,
4588 enum led_brightness brightness)
4589{
4590 struct tpacpi_led_classdev *data = container_of(led_cdev,
4591 struct tpacpi_led_classdev, led_classdev);
4592
4593 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004594 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004595}
4596
4597static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4598 unsigned long *delay_on, unsigned long *delay_off)
4599{
4600 struct tpacpi_led_classdev *data = container_of(led_cdev,
4601 struct tpacpi_led_classdev, led_classdev);
4602
4603 /* Can we choose the flash rate? */
4604 if (*delay_on == 0 && *delay_off == 0) {
4605 /* yes. set them to the hardware blink rate (1 Hz) */
4606 *delay_on = 500; /* ms */
4607 *delay_off = 500; /* ms */
4608 } else if ((*delay_on != 500) || (*delay_off != 500))
4609 return -EINVAL;
4610
4611 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004612 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004613
4614 return 0;
4615}
4616
4617static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4618{
4619 int rc;
4620
4621 struct tpacpi_led_classdev *data = container_of(led_cdev,
4622 struct tpacpi_led_classdev, led_classdev);
4623
4624 rc = led_get_status(data->led);
4625
4626 if (rc == TPACPI_LED_OFF || rc < 0)
4627 rc = LED_OFF; /* no error handling in led class :( */
4628 else
4629 rc = LED_FULL;
4630
4631 return rc;
4632}
4633
4634static void led_exit(void)
4635{
4636 unsigned int i;
4637
4638 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4639 if (tpacpi_leds[i].led_classdev.name)
4640 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4641 }
4642
4643 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004644}
4645
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004646static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004647{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004648 unsigned int i;
4649 int rc;
4650
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004651 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4652
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004653 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004654
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004655 if (!led_handle)
4656 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004657 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004658 else if (strlencmp(led_path, "SLED") == 0)
4659 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004660 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004661 else if (strlencmp(led_path, "SYSL") == 0)
4662 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004663 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004664 else
4665 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004666 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004667
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004668 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4669 str_supported(led_supported), led_supported);
4670
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004671 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4672 GFP_KERNEL);
4673 if (!tpacpi_leds) {
4674 printk(TPACPI_ERR "Out of memory for LED data\n");
4675 return -ENOMEM;
4676 }
4677
4678 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4679 tpacpi_leds[i].led = i;
4680
4681 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4682 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4683 if (led_supported == TPACPI_LED_570)
4684 tpacpi_leds[i].led_classdev.brightness_get =
4685 &led_sysfs_get;
4686
4687 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4688
4689 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4690
4691 rc = led_classdev_register(&tpacpi_pdev->dev,
4692 &tpacpi_leds[i].led_classdev);
4693 if (rc < 0) {
4694 tpacpi_leds[i].led_classdev.name = NULL;
4695 led_exit();
4696 return rc;
4697 }
4698 }
4699
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004700 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004701}
4702
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004703#define str_led_status(s) \
4704 ((s) == TPACPI_LED_OFF ? "off" : \
4705 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004706
4707static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
4709 int len = 0;
4710
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004711 if (!led_supported) {
4712 len += sprintf(p + len, "status:\t\tnot supported\n");
4713 return len;
4714 }
4715 len += sprintf(p + len, "status:\t\tsupported\n");
4716
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004717 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004718 /* 570 */
4719 int i, status;
4720 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004721 status = led_get_status(i);
4722 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004723 return -EIO;
4724 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004725 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004726 }
4727 }
4728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004730 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
4732 return len;
4733}
4734
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004735static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
4737 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004738 int led, rc;
4739 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004740
4741 if (!led_supported)
4742 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
4744 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004745 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 return -EINVAL;
4747
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004748 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004749 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004751 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004752 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004753 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004754 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004755 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004756 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004757
4758 rc = led_set_status(led, s);
4759 if (rc < 0)
4760 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 }
4762
4763 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004764}
4765
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004766static struct ibm_struct led_driver_data = {
4767 .name = "led",
4768 .read = led_read,
4769 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004770 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004771};
4772
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004773/*************************************************************************
4774 * Beep subdriver
4775 */
4776
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004777TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004778
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004779static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004780{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004781 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4782
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004783 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004784
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004785 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4786 str_supported(beep_handle != NULL));
4787
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004788 return (beep_handle)? 0 : 1;
4789}
4790
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004791static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
4793 int len = 0;
4794
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004795 if (!beep_handle)
4796 len += sprintf(p + len, "status:\t\tnot supported\n");
4797 else {
4798 len += sprintf(p + len, "status:\t\tsupported\n");
4799 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
4802 return len;
4803}
4804
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004805static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
4807 char *cmd;
4808 int beep_cmd;
4809
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004810 if (!beep_handle)
4811 return -ENODEV;
4812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004814 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4815 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 /* beep_cmd set */
4817 } else
4818 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004819 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 return -EIO;
4821 }
4822
4823 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004824}
4825
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004826static struct ibm_struct beep_driver_data = {
4827 .name = "beep",
4828 .read = beep_read,
4829 .write = beep_write,
4830};
4831
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004832/*************************************************************************
4833 * Thermal subdriver
4834 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004835
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004836enum thermal_access_mode {
4837 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4838 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4839 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4840 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4841 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4842};
4843
4844enum { /* TPACPI_THERMAL_TPEC_* */
4845 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4846 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4847 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4848};
4849
4850#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4851struct ibm_thermal_sensors_struct {
4852 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4853};
4854
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004855static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004856
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004857/* idx is zero-based */
4858static int thermal_get_sensor(int idx, s32 *value)
4859{
4860 int t;
4861 s8 tmp;
4862 char tmpi[5];
4863
4864 t = TP_EC_THERMAL_TMP0;
4865
4866 switch (thermal_read_mode) {
4867#if TPACPI_MAX_THERMAL_SENSORS >= 16
4868 case TPACPI_THERMAL_TPEC_16:
4869 if (idx >= 8 && idx <= 15) {
4870 t = TP_EC_THERMAL_TMP8;
4871 idx -= 8;
4872 }
4873 /* fallthrough */
4874#endif
4875 case TPACPI_THERMAL_TPEC_8:
4876 if (idx <= 7) {
4877 if (!acpi_ec_read(t + idx, &tmp))
4878 return -EIO;
4879 *value = tmp * 1000;
4880 return 0;
4881 }
4882 break;
4883
4884 case TPACPI_THERMAL_ACPI_UPDT:
4885 if (idx <= 7) {
4886 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4887 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4888 return -EIO;
4889 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4890 return -EIO;
4891 *value = (t - 2732) * 100;
4892 return 0;
4893 }
4894 break;
4895
4896 case TPACPI_THERMAL_ACPI_TMP07:
4897 if (idx <= 7) {
4898 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4899 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4900 return -EIO;
4901 if (t > 127 || t < -127)
4902 t = TP_EC_THERMAL_TMP_NA;
4903 *value = t * 1000;
4904 return 0;
4905 }
4906 break;
4907
4908 case TPACPI_THERMAL_NONE:
4909 default:
4910 return -ENOSYS;
4911 }
4912
4913 return -EINVAL;
4914}
4915
4916static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4917{
4918 int res, i;
4919 int n;
4920
4921 n = 8;
4922 i = 0;
4923
4924 if (!s)
4925 return -EINVAL;
4926
4927 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4928 n = 16;
4929
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004930 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004931 res = thermal_get_sensor(i, &s->temp[i]);
4932 if (res)
4933 return res;
4934 }
4935
4936 return n;
4937}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004938
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004939/* sysfs temp##_input -------------------------------------------------- */
4940
4941static ssize_t thermal_temp_input_show(struct device *dev,
4942 struct device_attribute *attr,
4943 char *buf)
4944{
4945 struct sensor_device_attribute *sensor_attr =
4946 to_sensor_dev_attr(attr);
4947 int idx = sensor_attr->index;
4948 s32 value;
4949 int res;
4950
4951 res = thermal_get_sensor(idx, &value);
4952 if (res)
4953 return res;
4954 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4955 return -ENXIO;
4956
4957 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4958}
4959
4960#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004961 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4962 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004963
4964static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4965 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4966 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4967 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4968 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4969 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4970 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4971 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4972 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4973 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4974 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4975 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4976 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4977 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4978 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4979 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4980 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4981};
4982
4983#define THERMAL_ATTRS(X) \
4984 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4985
4986static struct attribute *thermal_temp_input_attr[] = {
4987 THERMAL_ATTRS(8),
4988 THERMAL_ATTRS(9),
4989 THERMAL_ATTRS(10),
4990 THERMAL_ATTRS(11),
4991 THERMAL_ATTRS(12),
4992 THERMAL_ATTRS(13),
4993 THERMAL_ATTRS(14),
4994 THERMAL_ATTRS(15),
4995 THERMAL_ATTRS(0),
4996 THERMAL_ATTRS(1),
4997 THERMAL_ATTRS(2),
4998 THERMAL_ATTRS(3),
4999 THERMAL_ATTRS(4),
5000 THERMAL_ATTRS(5),
5001 THERMAL_ATTRS(6),
5002 THERMAL_ATTRS(7),
5003 NULL
5004};
5005
5006static const struct attribute_group thermal_temp_input16_group = {
5007 .attrs = thermal_temp_input_attr
5008};
5009
5010static const struct attribute_group thermal_temp_input8_group = {
5011 .attrs = &thermal_temp_input_attr[8]
5012};
5013
5014#undef THERMAL_SENSOR_ATTR_TEMP
5015#undef THERMAL_ATTRS
5016
5017/* --------------------------------------------------------------------- */
5018
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005019static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005020{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005021 u8 t, ta1, ta2;
5022 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005023 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005024 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005025
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005026 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5027
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005028 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005029
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03005030 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005031 /*
5032 * Direct EC access mode: sensors at registers
5033 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5034 * non-implemented, thermal sensors return 0x80 when
5035 * not available
5036 */
5037
5038 ta1 = ta2 = 0;
5039 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005040 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005041 ta1 |= t;
5042 } else {
5043 ta1 = 0;
5044 break;
5045 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03005046 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005047 ta2 |= t;
5048 } else {
5049 ta1 = 0;
5050 break;
5051 }
5052 }
5053 if (ta1 == 0) {
5054 /* This is sheer paranoia, but we handle it anyway */
5055 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005056 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005057 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005058 "falling back to ACPI TMPx access "
5059 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005060 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005061 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005062 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005063 "ThinkPad ACPI EC access misbehaving, "
5064 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005065 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005066 }
5067 } else {
5068 thermal_read_mode =
5069 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005070 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005071 }
5072 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005073 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5074 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005075 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005076 } else {
5077 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005078 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005079 }
5080 } else {
5081 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005082 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005083 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005084
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005085 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5086 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5087 thermal_read_mode);
5088
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005089 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005090 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005091 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005092 &thermal_temp_input16_group);
5093 if (res)
5094 return res;
5095 break;
5096 case TPACPI_THERMAL_TPEC_8:
5097 case TPACPI_THERMAL_ACPI_TMP07:
5098 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005099 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005100 &thermal_temp_input8_group);
5101 if (res)
5102 return res;
5103 break;
5104 case TPACPI_THERMAL_NONE:
5105 default:
5106 return 1;
5107 }
5108
5109 return 0;
5110}
5111
5112static void thermal_exit(void)
5113{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005114 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005115 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005116 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005117 &thermal_temp_input16_group);
5118 break;
5119 case TPACPI_THERMAL_TPEC_8:
5120 case TPACPI_THERMAL_ACPI_TMP07:
5121 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005122 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005123 &thermal_temp_input16_group);
5124 break;
5125 case TPACPI_THERMAL_NONE:
5126 default:
5127 break;
5128 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005129}
5130
5131static int thermal_read(char *p)
5132{
5133 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005134 int n, i;
5135 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005136
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005137 n = thermal_get_sensors(&t);
5138 if (unlikely(n < 0))
5139 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005140
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005141 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005142
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02005143 if (n > 0) {
5144 for (i = 0; i < (n - 1); i++)
5145 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5146 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5147 } else
5148 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005149
5150 return len;
5151}
5152
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005153static struct ibm_struct thermal_driver_data = {
5154 .name = "thermal",
5155 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03005156 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005157};
5158
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005159/*************************************************************************
5160 * EC Dump subdriver
5161 */
5162
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005163static u8 ecdump_regs[256];
5164
5165static int ecdump_read(char *p)
5166{
5167 int len = 0;
5168 int i, j;
5169 u8 v;
5170
5171 len += sprintf(p + len, "EC "
5172 " +00 +01 +02 +03 +04 +05 +06 +07"
5173 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5174 for (i = 0; i < 256; i += 16) {
5175 len += sprintf(p + len, "EC 0x%02x:", i);
5176 for (j = 0; j < 16; j++) {
5177 if (!acpi_ec_read(i + j, &v))
5178 break;
5179 if (v != ecdump_regs[i + j])
5180 len += sprintf(p + len, " *%02x", v);
5181 else
5182 len += sprintf(p + len, " %02x", v);
5183 ecdump_regs[i + j] = v;
5184 }
5185 len += sprintf(p + len, "\n");
5186 if (j != 16)
5187 break;
5188 }
5189
5190 /* These are way too dangerous to advertise openly... */
5191#if 0
5192 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5193 " (<offset> is 00-ff, <value> is 00-ff)\n");
5194 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5195 " (<offset> is 00-ff, <value> is 0-255)\n");
5196#endif
5197 return len;
5198}
5199
5200static int ecdump_write(char *buf)
5201{
5202 char *cmd;
5203 int i, v;
5204
5205 while ((cmd = next_cmd(&buf))) {
5206 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5207 /* i and v set */
5208 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5209 /* i and v set */
5210 } else
5211 return -EINVAL;
5212 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5213 if (!acpi_ec_write(i, v))
5214 return -EIO;
5215 } else
5216 return -EINVAL;
5217 }
5218
5219 return 0;
5220}
5221
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005222static struct ibm_struct ecdump_driver_data = {
5223 .name = "ecdump",
5224 .read = ecdump_read,
5225 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005226 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005227};
5228
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005229/*************************************************************************
5230 * Backlight/brightness subdriver
5231 */
Holger Macht8acb0252006-10-20 14:30:28 -07005232
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005233#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5234
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005235enum {
5236 TP_EC_BACKLIGHT = 0x31,
5237
5238 /* TP_EC_BACKLIGHT bitmasks */
5239 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5240 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5241 TP_EC_BACKLIGHT_MAPSW = 0x20,
5242};
5243
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005244static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005245static int brightness_mode;
5246static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5247
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005248static struct mutex brightness_mutex;
5249
5250/*
5251 * ThinkPads can read brightness from two places: EC 0x31, or
5252 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005253 *
5254 * EC 0x31 has the following layout
5255 * Bit 7: unknown function
5256 * Bit 6: unknown function
5257 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5258 * Bit 4: must be set to zero to avoid problems
5259 * Bit 3-0: backlight brightness level
5260 *
5261 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005262 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005263static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005264{
5265 u8 lec = 0, lcmos = 0, level = 0;
5266
5267 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005268 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005269 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005270 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005271 };
5272 if (brightness_mode & 2) {
5273 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5274 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5275 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5276 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5277 level = lcmos;
5278 }
5279
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005280 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005281 *status = lec; /* Prefer EC, CMOS is just a backing store */
5282 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03005283 if (lec == lcmos)
5284 tp_warned.bright_cmos_ec_unsync = 0;
5285 else {
5286 if (!tp_warned.bright_cmos_ec_unsync) {
5287 printk(TPACPI_ERR
5288 "CMOS NVRAM (%u) and EC (%u) do not "
5289 "agree on display brightness level\n",
5290 (unsigned int) lcmos,
5291 (unsigned int) lec);
5292 tp_warned.bright_cmos_ec_unsync = 1;
5293 }
5294 return -EIO;
5295 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005296 } else {
5297 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005298 }
5299
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005300 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005301}
5302
5303/* May return EINTR which can always be mapped to ERESTARTSYS */
5304static int brightness_set(int value)
5305{
5306 int cmos_cmd, inc, i, res;
5307 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005308 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005309
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005310 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5311 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005312 return -EINVAL;
5313
5314 res = mutex_lock_interruptible(&brightness_mutex);
5315 if (res < 0)
5316 return res;
5317
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005318 res = brightness_get_raw(&current_value);
5319 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005320 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005321
5322 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5323 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005324
5325 cmos_cmd = value > current_value ?
5326 TP_CMOS_BRIGHTNESS_UP :
5327 TP_CMOS_BRIGHTNESS_DOWN;
5328 inc = (value > current_value)? 1 : -1;
5329
5330 res = 0;
5331 for (i = current_value; i != value; i += inc) {
5332 if ((brightness_mode & 2) &&
5333 issue_thinkpad_cmos_command(cmos_cmd)) {
5334 res = -EIO;
5335 goto errout;
5336 }
5337 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005338 !acpi_ec_write(TP_EC_BACKLIGHT,
5339 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005340 res = -EIO;
5341 goto errout;;
5342 }
5343 }
5344
5345errout:
5346 mutex_unlock(&brightness_mutex);
5347 return res;
5348}
5349
5350/* sysfs backlight class ----------------------------------------------- */
5351
5352static int brightness_update_status(struct backlight_device *bd)
5353{
5354 /* it is the backlight class's job (caller) to handle
5355 * EINTR and other errors properly */
5356 return brightness_set(
5357 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5358 bd->props.power == FB_BLANK_UNBLANK) ?
5359 bd->props.brightness : 0);
5360}
Holger Macht8acb0252006-10-20 14:30:28 -07005361
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005362static int brightness_get(struct backlight_device *bd)
5363{
5364 int status, res;
5365
5366 res = brightness_get_raw(&status);
5367 if (res < 0)
5368 return 0; /* FIXME: teach backlight about error handling */
5369
5370 return status & TP_EC_BACKLIGHT_LVLMSK;
5371}
5372
Richard Purdie599a52d2007-02-10 23:07:48 +00005373static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005374 .get_brightness = brightness_get,
5375 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005376};
5377
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005378/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005379
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005380static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005381{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005382 int b;
5383
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005384 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5385
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03005386 mutex_init(&brightness_mutex);
5387
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005388 /*
5389 * We always attempt to detect acpi support, so as to switch
5390 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5391 * going to publish a backlight interface
5392 */
5393 b = tpacpi_check_std_acpi_brightness_support();
5394 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02005395
5396 if (acpi_video_backlight_support()) {
5397 if (brightness_enable > 1) {
5398 printk(TPACPI_NOTICE
5399 "Standard ACPI backlight interface "
5400 "available, not loading native one.\n");
5401 return 1;
5402 } else if (brightness_enable == 1) {
5403 printk(TPACPI_NOTICE
5404 "Backlight control force enabled, even if standard "
5405 "ACPI backlight interface is available\n");
5406 }
5407 } else {
5408 if (brightness_enable > 1) {
5409 printk(TPACPI_NOTICE
5410 "Standard ACPI backlight interface not "
5411 "available, thinkpad_acpi native "
5412 "brightness control enabled\n");
5413 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02005414 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005415 }
5416
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03005417 if (!brightness_enable) {
5418 dbg_printk(TPACPI_DBG_INIT,
5419 "brightness support disabled by "
5420 "module parameter\n");
5421 return 1;
5422 }
5423
5424 if (b > 16) {
5425 printk(TPACPI_ERR
5426 "Unsupported brightness interface, "
5427 "please contact %s\n", TPACPI_MAIL);
5428 return 1;
5429 }
5430 if (b == 16)
5431 tp_features.bright_16levels = 1;
5432
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005433 if (!brightness_mode) {
5434 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5435 brightness_mode = 2;
5436 else
5437 brightness_mode = 3;
5438
5439 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5440 brightness_mode);
5441 }
5442
5443 if (brightness_mode > 3)
5444 return -EINVAL;
5445
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005446 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005447 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005448
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005449 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005450 printk(TPACPI_INFO
5451 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005452
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03005453 ibm_backlight_device = backlight_device_register(
5454 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5455 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005456 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005457 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005458 return PTR_ERR(ibm_backlight_device);
5459 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005460 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005461
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005462 ibm_backlight_device->props.max_brightness =
5463 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005464 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005465 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005466
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005467 return 0;
5468}
5469
5470static void brightness_exit(void)
5471{
5472 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005473 vdbg_printk(TPACPI_DBG_EXIT,
5474 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005475 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005476 }
5477}
5478
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005479static int brightness_read(char *p)
5480{
5481 int len = 0;
5482 int level;
5483
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005484 level = brightness_get(NULL);
5485 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005486 len += sprintf(p + len, "level:\t\tunreadable\n");
5487 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005488 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005489 len += sprintf(p + len, "commands:\tup, down\n");
5490 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005491 " (<level> is 0-%d)\n",
5492 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005493 }
5494
5495 return len;
5496}
5497
5498static int brightness_write(char *buf)
5499{
5500 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005501 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005502 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005503 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005504
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005505 level = brightness_get(NULL);
5506 if (level < 0)
5507 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005508
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005509 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005510 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005511 if (level < max_level)
5512 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005513 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005514 if (level > 0)
5515 level--;
5516 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5517 level >= 0 && level <= max_level) {
5518 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005519 } else
5520 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005521 }
5522
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005523 /*
5524 * Now we know what the final level should be, so we try to set it.
5525 * Doing it this way makes the syscall restartable in case of EINTR
5526 */
5527 rc = brightness_set(level);
5528 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005529}
5530
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005531static struct ibm_struct brightness_driver_data = {
5532 .name = "brightness",
5533 .read = brightness_read,
5534 .write = brightness_write,
5535 .exit = brightness_exit,
5536};
5537
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005538/*************************************************************************
5539 * Volume subdriver
5540 */
5541
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005542static int volume_offset = 0x30;
5543
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005544static int volume_read(char *p)
5545{
5546 int len = 0;
5547 u8 level;
5548
5549 if (!acpi_ec_read(volume_offset, &level)) {
5550 len += sprintf(p + len, "level:\t\tunreadable\n");
5551 } else {
5552 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5553 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5554 len += sprintf(p + len, "commands:\tup, down, mute\n");
5555 len += sprintf(p + len, "commands:\tlevel <level>"
5556 " (<level> is 0-15)\n");
5557 }
5558
5559 return len;
5560}
5561
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005562static int volume_write(char *buf)
5563{
5564 int cmos_cmd, inc, i;
5565 u8 level, mute;
5566 int new_level, new_mute;
5567 char *cmd;
5568
5569 while ((cmd = next_cmd(&buf))) {
5570 if (!acpi_ec_read(volume_offset, &level))
5571 return -EIO;
5572 new_mute = mute = level & 0x40;
5573 new_level = level = level & 0xf;
5574
5575 if (strlencmp(cmd, "up") == 0) {
5576 if (mute)
5577 new_mute = 0;
5578 else
5579 new_level = level == 15 ? 15 : level + 1;
5580 } else if (strlencmp(cmd, "down") == 0) {
5581 if (mute)
5582 new_mute = 0;
5583 else
5584 new_level = level == 0 ? 0 : level - 1;
5585 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5586 new_level >= 0 && new_level <= 15) {
5587 /* new_level set */
5588 } else if (strlencmp(cmd, "mute") == 0) {
5589 new_mute = 0x40;
5590 } else
5591 return -EINVAL;
5592
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005593 if (new_level != level) {
5594 /* mute doesn't change */
5595
5596 cmos_cmd = (new_level > level) ?
5597 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005598 inc = new_level > level ? 1 : -1;
5599
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005600 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005601 !acpi_ec_write(volume_offset, level)))
5602 return -EIO;
5603
5604 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005605 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005606 !acpi_ec_write(volume_offset, i + inc))
5607 return -EIO;
5608
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005609 if (mute &&
5610 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5611 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005612 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005613 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005614 }
5615
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005616 if (new_mute != mute) {
5617 /* level doesn't change */
5618
5619 cmos_cmd = (new_mute) ?
5620 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005621
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005622 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005623 !acpi_ec_write(volume_offset, level + new_mute))
5624 return -EIO;
5625 }
5626 }
5627
5628 return 0;
5629}
5630
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005631static struct ibm_struct volume_driver_data = {
5632 .name = "volume",
5633 .read = volume_read,
5634 .write = volume_write,
5635};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005636
5637/*************************************************************************
5638 * Fan subdriver
5639 */
5640
5641/*
5642 * FAN ACCESS MODES
5643 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005644 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005645 * ACPI GFAN method: returns fan level
5646 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005647 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005648 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005649 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005650 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005651 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5652 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005653 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5654 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005655 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005656 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005657 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5658 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005659 *
5660 * Fan speed changes of any sort (including those caused by the
5661 * disengaged mode) are usually done slowly by the firmware as the
5662 * maximum ammount of fan duty cycle change per second seems to be
5663 * limited.
5664 *
5665 * Reading is not available if GFAN exists.
5666 * Writing is not available if SFAN exists.
5667 *
5668 * Bits
5669 * 7 automatic mode engaged;
5670 * (default operation mode of the ThinkPad)
5671 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005672 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005673 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005674 * the tachometer while the fan controller ramps up
5675 * the speed (which can take up to a few *minutes*).
5676 * Speeds up fan to 100% duty-cycle, which is far above
5677 * the standard RPM levels. It is not impossible that
5678 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005679 * 5-3 unused in some models. Extra bits for fan level
5680 * in others, but still useless as all values above
5681 * 7 map to the same speed as level 7 in these models.
5682 * 2-0 fan level (0..7 usually)
5683 * 0x00 = stop
5684 * 0x07 = max (set when temperatures critical)
5685 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005686 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005687 *
5688 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5689 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5690 * does so, its initial value is meaningless (0x07).
5691 *
5692 * For firmware bugs, refer to:
5693 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5694 *
5695 * ----
5696 *
5697 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5698 * Main fan tachometer reading (in RPM)
5699 *
5700 * This register is present on all ThinkPads with a new-style EC, and
5701 * it is known not to be present on the A21m/e, and T22, as there is
5702 * something else in offset 0x84 according to the ACPI DSDT. Other
5703 * ThinkPads from this same time period (and earlier) probably lack the
5704 * tachometer as well.
5705 *
5706 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5707 * was never fixed by IBM to report the EC firmware version string
5708 * probably support the tachometer (like the early X models), so
5709 * detecting it is quite hard. We need more data to know for sure.
5710 *
5711 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5712 * might result.
5713 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005714 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5715 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005716 *
5717 * For firmware bugs, refer to:
5718 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5719 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005720 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005721 * ThinkPad X31, X40, X41. Not available in the X60.
5722 *
5723 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5724 * high speed. ACPI DSDT seems to map these three speeds to levels
5725 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5726 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5727 *
5728 * The speeds are stored on handles
5729 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5730 *
5731 * There are three default speed sets, acessible as handles:
5732 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5733 *
5734 * ACPI DSDT switches which set is in use depending on various
5735 * factors.
5736 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005737 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005738 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5739 * but the ACPI tables just mention level 7.
5740 */
5741
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005742enum { /* Fan control constants */
5743 fan_status_offset = 0x2f, /* EC register 0x2f */
5744 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5745 * 0x84 must be read before 0x85 */
5746
5747 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5748 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5749
5750 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5751};
5752
5753enum fan_status_access_mode {
5754 TPACPI_FAN_NONE = 0, /* No fan status or control */
5755 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5756 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5757};
5758
5759enum fan_control_access_mode {
5760 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5761 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5762 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5763 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5764};
5765
5766enum fan_control_commands {
5767 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5768 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5769 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5770 * and also watchdog cmd */
5771};
5772
5773static int fan_control_allowed;
5774
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005775static enum fan_status_access_mode fan_status_access_mode;
5776static enum fan_control_access_mode fan_control_access_mode;
5777static enum fan_control_commands fan_control_commands;
5778
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005779static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005780static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02005781static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005782static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005783
5784static struct mutex fan_mutex;
5785
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005786static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005787static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005788
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005789TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5790TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005791 "\\FSPD", /* 600e/x, 770e, 770x */
5792 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005793TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005794 "JFNS", /* 770x-JL */
5795 ); /* all others */
5796
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005797/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005798 * Call with fan_mutex held
5799 */
5800static void fan_update_desired_level(u8 status)
5801{
5802 if ((status &
5803 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5804 if (status > 7)
5805 fan_control_desired_level = 7;
5806 else
5807 fan_control_desired_level = status;
5808 }
5809}
5810
5811static int fan_get_status(u8 *status)
5812{
5813 u8 s;
5814
5815 /* TODO:
5816 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5817
5818 switch (fan_status_access_mode) {
5819 case TPACPI_FAN_RD_ACPI_GFAN:
5820 /* 570, 600e/x, 770e, 770x */
5821
5822 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5823 return -EIO;
5824
5825 if (likely(status))
5826 *status = s & 0x07;
5827
5828 break;
5829
5830 case TPACPI_FAN_RD_TPEC:
5831 /* all except 570, 600e/x, 770e, 770x */
5832 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5833 return -EIO;
5834
5835 if (likely(status))
5836 *status = s;
5837
5838 break;
5839
5840 default:
5841 return -ENXIO;
5842 }
5843
5844 return 0;
5845}
5846
5847static int fan_get_status_safe(u8 *status)
5848{
5849 int rc;
5850 u8 s;
5851
5852 if (mutex_lock_interruptible(&fan_mutex))
5853 return -ERESTARTSYS;
5854 rc = fan_get_status(&s);
5855 if (!rc)
5856 fan_update_desired_level(s);
5857 mutex_unlock(&fan_mutex);
5858
5859 if (status)
5860 *status = s;
5861
5862 return rc;
5863}
5864
5865static int fan_get_speed(unsigned int *speed)
5866{
5867 u8 hi, lo;
5868
5869 switch (fan_status_access_mode) {
5870 case TPACPI_FAN_RD_TPEC:
5871 /* all except 570, 600e/x, 770e, 770x */
5872 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5873 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5874 return -EIO;
5875
5876 if (likely(speed))
5877 *speed = (hi << 8) | lo;
5878
5879 break;
5880
5881 default:
5882 return -ENXIO;
5883 }
5884
5885 return 0;
5886}
5887
5888static int fan_set_level(int level)
5889{
5890 if (!fan_control_allowed)
5891 return -EPERM;
5892
5893 switch (fan_control_access_mode) {
5894 case TPACPI_FAN_WR_ACPI_SFAN:
5895 if (level >= 0 && level <= 7) {
5896 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5897 return -EIO;
5898 } else
5899 return -EINVAL;
5900 break;
5901
5902 case TPACPI_FAN_WR_ACPI_FANS:
5903 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02005904 if (!(level & TP_EC_FAN_AUTO) &&
5905 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005906 ((level < 0) || (level > 7)))
5907 return -EINVAL;
5908
5909 /* safety net should the EC not support AUTO
5910 * or FULLSPEED mode bits and just ignore them */
5911 if (level & TP_EC_FAN_FULLSPEED)
5912 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005913 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005914 level |= 4; /* safety min speed 4 */
5915
5916 if (!acpi_ec_write(fan_status_offset, level))
5917 return -EIO;
5918 else
5919 tp_features.fan_ctrl_status_undef = 0;
5920 break;
5921
5922 default:
5923 return -ENXIO;
5924 }
5925 return 0;
5926}
5927
5928static int fan_set_level_safe(int level)
5929{
5930 int rc;
5931
5932 if (!fan_control_allowed)
5933 return -EPERM;
5934
5935 if (mutex_lock_interruptible(&fan_mutex))
5936 return -ERESTARTSYS;
5937
5938 if (level == TPACPI_FAN_LAST_LEVEL)
5939 level = fan_control_desired_level;
5940
5941 rc = fan_set_level(level);
5942 if (!rc)
5943 fan_update_desired_level(level);
5944
5945 mutex_unlock(&fan_mutex);
5946 return rc;
5947}
5948
5949static int fan_set_enable(void)
5950{
5951 u8 s;
5952 int rc;
5953
5954 if (!fan_control_allowed)
5955 return -EPERM;
5956
5957 if (mutex_lock_interruptible(&fan_mutex))
5958 return -ERESTARTSYS;
5959
5960 switch (fan_control_access_mode) {
5961 case TPACPI_FAN_WR_ACPI_FANS:
5962 case TPACPI_FAN_WR_TPEC:
5963 rc = fan_get_status(&s);
5964 if (rc < 0)
5965 break;
5966
5967 /* Don't go out of emergency fan mode */
5968 if (s != 7) {
5969 s &= 0x07;
5970 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5971 }
5972
5973 if (!acpi_ec_write(fan_status_offset, s))
5974 rc = -EIO;
5975 else {
5976 tp_features.fan_ctrl_status_undef = 0;
5977 rc = 0;
5978 }
5979 break;
5980
5981 case TPACPI_FAN_WR_ACPI_SFAN:
5982 rc = fan_get_status(&s);
5983 if (rc < 0)
5984 break;
5985
5986 s &= 0x07;
5987
5988 /* Set fan to at least level 4 */
5989 s |= 4;
5990
5991 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005992 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005993 else
5994 rc = 0;
5995 break;
5996
5997 default:
5998 rc = -ENXIO;
5999 }
6000
6001 mutex_unlock(&fan_mutex);
6002 return rc;
6003}
6004
6005static int fan_set_disable(void)
6006{
6007 int rc;
6008
6009 if (!fan_control_allowed)
6010 return -EPERM;
6011
6012 if (mutex_lock_interruptible(&fan_mutex))
6013 return -ERESTARTSYS;
6014
6015 rc = 0;
6016 switch (fan_control_access_mode) {
6017 case TPACPI_FAN_WR_ACPI_FANS:
6018 case TPACPI_FAN_WR_TPEC:
6019 if (!acpi_ec_write(fan_status_offset, 0x00))
6020 rc = -EIO;
6021 else {
6022 fan_control_desired_level = 0;
6023 tp_features.fan_ctrl_status_undef = 0;
6024 }
6025 break;
6026
6027 case TPACPI_FAN_WR_ACPI_SFAN:
6028 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6029 rc = -EIO;
6030 else
6031 fan_control_desired_level = 0;
6032 break;
6033
6034 default:
6035 rc = -ENXIO;
6036 }
6037
6038
6039 mutex_unlock(&fan_mutex);
6040 return rc;
6041}
6042
6043static int fan_set_speed(int speed)
6044{
6045 int rc;
6046
6047 if (!fan_control_allowed)
6048 return -EPERM;
6049
6050 if (mutex_lock_interruptible(&fan_mutex))
6051 return -ERESTARTSYS;
6052
6053 rc = 0;
6054 switch (fan_control_access_mode) {
6055 case TPACPI_FAN_WR_ACPI_FANS:
6056 if (speed >= 0 && speed <= 65535) {
6057 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6058 speed, speed, speed))
6059 rc = -EIO;
6060 } else
6061 rc = -EINVAL;
6062 break;
6063
6064 default:
6065 rc = -ENXIO;
6066 }
6067
6068 mutex_unlock(&fan_mutex);
6069 return rc;
6070}
6071
6072static void fan_watchdog_reset(void)
6073{
6074 static int fan_watchdog_active;
6075
6076 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6077 return;
6078
6079 if (fan_watchdog_active)
6080 cancel_delayed_work(&fan_watchdog_task);
6081
6082 if (fan_watchdog_maxinterval > 0 &&
6083 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6084 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006085 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006086 msecs_to_jiffies(fan_watchdog_maxinterval
6087 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006088 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006089 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006090 "watchdog will not trigger\n");
6091 }
6092 } else
6093 fan_watchdog_active = 0;
6094}
6095
6096static void fan_watchdog_fire(struct work_struct *ignored)
6097{
6098 int rc;
6099
6100 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6101 return;
6102
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006103 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006104 rc = fan_set_enable();
6105 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006106 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006107 "will try again later...\n", -rc);
6108 /* reschedule for later */
6109 fan_watchdog_reset();
6110 }
6111}
6112
6113/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006114 * SYSFS fan layout: hwmon compatible (device)
6115 *
6116 * pwm*_enable:
6117 * 0: "disengaged" mode
6118 * 1: manual mode
6119 * 2: native EC "auto" mode (recommended, hardware default)
6120 *
6121 * pwm*: set speed in manual mode, ignored otherwise.
6122 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6123 * interpolation.
6124 *
6125 * fan*_input: tachometer reading, RPM
6126 *
6127 *
6128 * SYSFS fan layout: extensions
6129 *
6130 * fan_watchdog (driver):
6131 * fan watchdog interval in seconds, 0 disables (default), max 120
6132 */
6133
6134/* sysfs fan pwm1_enable ----------------------------------------------- */
6135static ssize_t fan_pwm1_enable_show(struct device *dev,
6136 struct device_attribute *attr,
6137 char *buf)
6138{
6139 int res, mode;
6140 u8 status;
6141
6142 res = fan_get_status_safe(&status);
6143 if (res)
6144 return res;
6145
6146 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6147 if (status != fan_control_initial_status) {
6148 tp_features.fan_ctrl_status_undef = 0;
6149 } else {
6150 /* Return most likely status. In fact, it
6151 * might be the only possible status */
6152 status = TP_EC_FAN_AUTO;
6153 }
6154 }
6155
6156 if (status & TP_EC_FAN_FULLSPEED) {
6157 mode = 0;
6158 } else if (status & TP_EC_FAN_AUTO) {
6159 mode = 2;
6160 } else
6161 mode = 1;
6162
6163 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6164}
6165
6166static ssize_t fan_pwm1_enable_store(struct device *dev,
6167 struct device_attribute *attr,
6168 const char *buf, size_t count)
6169{
6170 unsigned long t;
6171 int res, level;
6172
6173 if (parse_strtoul(buf, 2, &t))
6174 return -EINVAL;
6175
6176 switch (t) {
6177 case 0:
6178 level = TP_EC_FAN_FULLSPEED;
6179 break;
6180 case 1:
6181 level = TPACPI_FAN_LAST_LEVEL;
6182 break;
6183 case 2:
6184 level = TP_EC_FAN_AUTO;
6185 break;
6186 case 3:
6187 /* reserved for software-controlled auto mode */
6188 return -ENOSYS;
6189 default:
6190 return -EINVAL;
6191 }
6192
6193 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006194 if (res == -ENXIO)
6195 return -EINVAL;
6196 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006197 return res;
6198
6199 fan_watchdog_reset();
6200
6201 return count;
6202}
6203
6204static struct device_attribute dev_attr_fan_pwm1_enable =
6205 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6206 fan_pwm1_enable_show, fan_pwm1_enable_store);
6207
6208/* sysfs fan pwm1 ------------------------------------------------------ */
6209static ssize_t fan_pwm1_show(struct device *dev,
6210 struct device_attribute *attr,
6211 char *buf)
6212{
6213 int res;
6214 u8 status;
6215
6216 res = fan_get_status_safe(&status);
6217 if (res)
6218 return res;
6219
6220 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6221 if (status != fan_control_initial_status) {
6222 tp_features.fan_ctrl_status_undef = 0;
6223 } else {
6224 status = TP_EC_FAN_AUTO;
6225 }
6226 }
6227
6228 if ((status &
6229 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6230 status = fan_control_desired_level;
6231
6232 if (status > 7)
6233 status = 7;
6234
6235 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6236}
6237
6238static ssize_t fan_pwm1_store(struct device *dev,
6239 struct device_attribute *attr,
6240 const char *buf, size_t count)
6241{
6242 unsigned long s;
6243 int rc;
6244 u8 status, newlevel;
6245
6246 if (parse_strtoul(buf, 255, &s))
6247 return -EINVAL;
6248
6249 /* scale down from 0-255 to 0-7 */
6250 newlevel = (s >> 5) & 0x07;
6251
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02006252 if (mutex_lock_interruptible(&fan_mutex))
6253 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006254
6255 rc = fan_get_status(&status);
6256 if (!rc && (status &
6257 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6258 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03006259 if (rc == -ENXIO)
6260 rc = -EINVAL;
6261 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006262 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03006263 fan_watchdog_reset();
6264 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006265 }
6266
6267 mutex_unlock(&fan_mutex);
6268 return (rc)? rc : count;
6269}
6270
6271static struct device_attribute dev_attr_fan_pwm1 =
6272 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6273 fan_pwm1_show, fan_pwm1_store);
6274
6275/* sysfs fan fan1_input ------------------------------------------------ */
6276static ssize_t fan_fan1_input_show(struct device *dev,
6277 struct device_attribute *attr,
6278 char *buf)
6279{
6280 int res;
6281 unsigned int speed;
6282
6283 res = fan_get_speed(&speed);
6284 if (res < 0)
6285 return res;
6286
6287 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6288}
6289
6290static struct device_attribute dev_attr_fan_fan1_input =
6291 __ATTR(fan1_input, S_IRUGO,
6292 fan_fan1_input_show, NULL);
6293
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006294/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006295static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6296 char *buf)
6297{
6298 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6299}
6300
6301static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6302 const char *buf, size_t count)
6303{
6304 unsigned long t;
6305
6306 if (parse_strtoul(buf, 120, &t))
6307 return -EINVAL;
6308
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006309 if (!fan_control_allowed)
6310 return -EPERM;
6311
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006312 fan_watchdog_maxinterval = t;
6313 fan_watchdog_reset();
6314
6315 return count;
6316}
6317
6318static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6319 fan_fan_watchdog_show, fan_fan_watchdog_store);
6320
6321/* --------------------------------------------------------------------- */
6322static struct attribute *fan_attributes[] = {
6323 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6324 &dev_attr_fan_fan1_input.attr,
6325 NULL
6326};
6327
6328static const struct attribute_group fan_attr_group = {
6329 .attrs = fan_attributes,
6330};
6331
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006332static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006333{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006334 int rc;
6335
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006336 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6337
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03006338 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006339 fan_status_access_mode = TPACPI_FAN_NONE;
6340 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006341 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006342 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006343 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006344 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006345
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006346 TPACPI_ACPIHANDLE_INIT(fans);
6347 TPACPI_ACPIHANDLE_INIT(gfan);
6348 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006349
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006350 if (gfan_handle) {
6351 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006352 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006353 } else {
6354 /* all other ThinkPads: note that even old-style
6355 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006356 if (likely(acpi_ec_read(fan_status_offset,
6357 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006358 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006359
6360 /* In some ThinkPads, neither the EC nor the ACPI
6361 * DSDT initialize the fan status, and it ends up
6362 * being set to 0x07 when it *could* be either
6363 * 0x07 or 0x80.
6364 *
6365 * Enable for TP-1Y (T43), TP-78 (R51e),
6366 * TP-76 (R52), TP-70 (T43, R52), which are known
6367 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006368 if (fan_control_initial_status == 0x07) {
6369 switch (thinkpad_id.ec_model) {
6370 case 0x5931: /* TP-1Y */
6371 case 0x3837: /* TP-78 */
6372 case 0x3637: /* TP-76 */
6373 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006374 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006375 "fan_init: initial fan status "
6376 "is unknown, assuming it is "
6377 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006378 tp_features.fan_ctrl_status_undef = 1;
6379 ;;
6380 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02006381 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006382 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006383 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006384 "ThinkPad ACPI EC access misbehaving, "
6385 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006386 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006387 }
6388 }
6389
6390 if (sfan_handle) {
6391 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006392 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02006393 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006394 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006395 } else {
6396 if (!gfan_handle) {
6397 /* gfan without sfan means no fan control */
6398 /* all other models implement TP EC 0x2f control */
6399
6400 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006401 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006402 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006403 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006404 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006405 TPACPI_FAN_CMD_SPEED |
6406 TPACPI_FAN_CMD_LEVEL |
6407 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006408 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006409 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006410 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006411 TPACPI_FAN_CMD_LEVEL |
6412 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02006413 }
6414 }
6415 }
6416
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006417 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6418 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6419 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6420 fan_status_access_mode, fan_control_access_mode);
6421
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006422 /* fan control master switch */
6423 if (!fan_control_allowed) {
6424 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6425 fan_control_commands = 0;
6426 dbg_printk(TPACPI_DBG_INIT,
6427 "fan control features disabled by parameter\n");
6428 }
6429
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006430 /* update fan_control_desired_level */
6431 if (fan_status_access_mode != TPACPI_FAN_NONE)
6432 fan_get_status_safe(NULL);
6433
6434 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6435 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006436 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006437 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006438 if (rc < 0)
6439 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03006440
6441 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6442 &driver_attr_fan_watchdog);
6443 if (rc < 0) {
6444 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6445 &fan_attr_group);
6446 return rc;
6447 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006448 return 0;
6449 } else
6450 return 1;
6451}
6452
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006453static void fan_exit(void)
6454{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006455 vdbg_printk(TPACPI_DBG_EXIT,
6456 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006457
6458 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006459 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006460 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6461 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006462
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006463 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006464 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006465}
6466
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006467static void fan_suspend(pm_message_t state)
6468{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006469 int rc;
6470
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006471 if (!fan_control_allowed)
6472 return;
6473
6474 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006475 fan_control_resume_level = 0;
6476 rc = fan_get_status_safe(&fan_control_resume_level);
6477 if (rc < 0)
6478 printk(TPACPI_NOTICE
6479 "failed to read fan level for later "
6480 "restore during resume: %d\n", rc);
6481
6482 /* if it is undefined, don't attempt to restore it.
6483 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006484 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006485 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006486}
6487
6488static void fan_resume(void)
6489{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006490 u8 current_level = 7;
6491 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006492 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006493
6494 /* DSDT *always* updates status on resume */
6495 tp_features.fan_ctrl_status_undef = 0;
6496
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006497 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006498 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006499 (fan_get_status_safe(&current_level) < 0))
6500 return;
6501
6502 switch (fan_control_access_mode) {
6503 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006504 /* never decrease fan level */
6505 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006506 break;
6507 case TPACPI_FAN_WR_ACPI_FANS:
6508 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006509 /* never decrease fan level, scale is:
6510 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6511 *
6512 * We expect the firmware to set either 7 or AUTO, but we
6513 * handle FULLSPEED out of paranoia.
6514 *
6515 * So, we can safely only restore FULLSPEED or 7, anything
6516 * else could slow the fan. Restoring AUTO is useless, at
6517 * best that's exactly what the DSDT already set (it is the
6518 * slower it uses).
6519 *
6520 * Always keep in mind that the DSDT *will* have set the
6521 * fans to what the vendor supposes is the best level. We
6522 * muck with it only to speed the fan up.
6523 */
6524 if (fan_control_resume_level != 7 &&
6525 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6526 return;
6527 else
6528 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6529 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006530 break;
6531 default:
6532 return;
6533 }
6534 if (do_set) {
6535 printk(TPACPI_NOTICE
6536 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006537 fan_control_resume_level);
6538 rc = fan_set_level_safe(fan_control_resume_level);
6539 if (rc < 0)
6540 printk(TPACPI_NOTICE
6541 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006542 }
6543}
6544
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006545static int fan_read(char *p)
6546{
6547 int len = 0;
6548 int rc;
6549 u8 status;
6550 unsigned int speed = 0;
6551
6552 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006553 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006554 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006555 rc = fan_get_status_safe(&status);
6556 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006557 return rc;
6558
6559 len += sprintf(p + len, "status:\t\t%s\n"
6560 "level:\t\t%d\n",
6561 (status != 0) ? "enabled" : "disabled", status);
6562 break;
6563
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006564 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006565 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006566 rc = fan_get_status_safe(&status);
6567 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006568 return rc;
6569
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006570 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006571 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006572 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006573 else
6574 /* Return most likely status. In fact, it
6575 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006576 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006577 }
6578
6579 len += sprintf(p + len, "status:\t\t%s\n",
6580 (status != 0) ? "enabled" : "disabled");
6581
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006582 rc = fan_get_speed(&speed);
6583 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006584 return rc;
6585
6586 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6587
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006588 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006589 /* Disengaged mode takes precedence */
6590 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006591 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006592 len += sprintf(p + len, "level:\t\tauto\n");
6593 else
6594 len += sprintf(p + len, "level:\t\t%d\n", status);
6595 break;
6596
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006597 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006598 default:
6599 len += sprintf(p + len, "status:\t\tnot supported\n");
6600 }
6601
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006602 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006603 len += sprintf(p + len, "commands:\tlevel <level>");
6604
6605 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006606 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006607 len += sprintf(p + len, " (<level> is 0-7)\n");
6608 break;
6609
6610 default:
6611 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006612 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006613 break;
6614 }
6615 }
6616
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006617 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006618 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006619 "commands:\twatchdog <timeout> (<timeout> "
6620 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006621
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006622 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006623 len += sprintf(p + len, "commands:\tspeed <speed>"
6624 " (<speed> is 0-65535)\n");
6625
6626 return len;
6627}
6628
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006629static int fan_write_cmd_level(const char *cmd, int *rc)
6630{
6631 int level;
6632
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006633 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006634 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006635 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006636 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006637 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006638 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006639 return 0;
6640
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006641 *rc = fan_set_level_safe(level);
6642 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006643 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006644 "access mode %d", fan_control_access_mode);
6645
6646 return 1;
6647}
6648
6649static int fan_write_cmd_enable(const char *cmd, int *rc)
6650{
6651 if (strlencmp(cmd, "enable") != 0)
6652 return 0;
6653
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006654 *rc = fan_set_enable();
6655 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006656 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006657 "access mode %d", fan_control_access_mode);
6658
6659 return 1;
6660}
6661
6662static int fan_write_cmd_disable(const char *cmd, int *rc)
6663{
6664 if (strlencmp(cmd, "disable") != 0)
6665 return 0;
6666
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006667 *rc = fan_set_disable();
6668 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006669 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006670 "access mode %d", fan_control_access_mode);
6671
6672 return 1;
6673}
6674
6675static int fan_write_cmd_speed(const char *cmd, int *rc)
6676{
6677 int speed;
6678
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006679 /* TODO:
6680 * Support speed <low> <medium> <high> ? */
6681
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006682 if (sscanf(cmd, "speed %d", &speed) != 1)
6683 return 0;
6684
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006685 *rc = fan_set_speed(speed);
6686 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006687 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006688 "access mode %d", fan_control_access_mode);
6689
6690 return 1;
6691}
6692
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006693static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6694{
6695 int interval;
6696
6697 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6698 return 0;
6699
6700 if (interval < 0 || interval > 120)
6701 *rc = -EINVAL;
6702 else
6703 fan_watchdog_maxinterval = interval;
6704
6705 return 1;
6706}
6707
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006708static int fan_write(char *buf)
6709{
6710 char *cmd;
6711 int rc = 0;
6712
6713 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006714 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006715 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006716 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006717 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006718 fan_write_cmd_disable(cmd, &rc) ||
6719 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006720 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006721 fan_write_cmd_speed(cmd, &rc))
6722 )
6723 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006724 else if (!rc)
6725 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006726 }
6727
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006728 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006729}
6730
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006731static struct ibm_struct fan_driver_data = {
6732 .name = "fan",
6733 .read = fan_read,
6734 .write = fan_write,
6735 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006736 .suspend = fan_suspend,
6737 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006738};
6739
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006740/****************************************************************************
6741 ****************************************************************************
6742 *
6743 * Infrastructure
6744 *
6745 ****************************************************************************
6746 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006748/* sysfs name ---------------------------------------------------------- */
6749static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6750 struct device_attribute *attr,
6751 char *buf)
6752{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006753 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006754}
6755
6756static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6757 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6758
6759/* --------------------------------------------------------------------- */
6760
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006761/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03006762static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006763
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006764/*
6765 * Module and infrastructure proble, init and exit handling
6766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006768static int force_load;
6769
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006770#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006771static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006772{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006773 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006774
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006775 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006776}
6777#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6778
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006779static void ibm_exit(struct ibm_struct *ibm)
6780{
6781 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6782
6783 list_del_init(&ibm->all_drivers);
6784
6785 if (ibm->flags.acpi_notify_installed) {
6786 dbg_printk(TPACPI_DBG_EXIT,
6787 "%s: acpi_remove_notify_handler\n", ibm->name);
6788 BUG_ON(!ibm->acpi);
6789 acpi_remove_notify_handler(*ibm->acpi->handle,
6790 ibm->acpi->type,
6791 dispatch_acpi_notify);
6792 ibm->flags.acpi_notify_installed = 0;
6793 ibm->flags.acpi_notify_installed = 0;
6794 }
6795
6796 if (ibm->flags.proc_created) {
6797 dbg_printk(TPACPI_DBG_EXIT,
6798 "%s: remove_proc_entry\n", ibm->name);
6799 remove_proc_entry(ibm->name, proc_dir);
6800 ibm->flags.proc_created = 0;
6801 }
6802
6803 if (ibm->flags.acpi_driver_registered) {
6804 dbg_printk(TPACPI_DBG_EXIT,
6805 "%s: acpi_bus_unregister_driver\n", ibm->name);
6806 BUG_ON(!ibm->acpi);
6807 acpi_bus_unregister_driver(ibm->acpi->driver);
6808 kfree(ibm->acpi->driver);
6809 ibm->acpi->driver = NULL;
6810 ibm->flags.acpi_driver_registered = 0;
6811 }
6812
6813 if (ibm->flags.init_called && ibm->exit) {
6814 ibm->exit();
6815 ibm->flags.init_called = 0;
6816 }
6817
6818 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6819}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006820
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006821static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822{
6823 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006824 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 struct proc_dir_entry *entry;
6826
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006827 BUG_ON(ibm == NULL);
6828
6829 INIT_LIST_HEAD(&ibm->all_drivers);
6830
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006831 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 return 0;
6833
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006834 dbg_printk(TPACPI_DBG_INIT,
6835 "probing for %s\n", ibm->name);
6836
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006837 if (iibm->init) {
6838 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006839 if (ret > 0)
6840 return 0; /* probe failed */
6841 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006843
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006844 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 }
6846
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006847 if (ibm->acpi) {
6848 if (ibm->acpi->hid) {
6849 ret = register_tpacpi_subdriver(ibm);
6850 if (ret)
6851 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006852 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006853
6854 if (ibm->acpi->notify) {
6855 ret = setup_acpi_notify(ibm);
6856 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006857 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006858 ibm->name);
6859 ret = 0;
6860 goto err_out;
6861 }
6862 if (ret < 0)
6863 goto err_out;
6864 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006865 }
6866
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006867 dbg_printk(TPACPI_DBG_INIT,
6868 "%s installed\n", ibm->name);
6869
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006870 if (ibm->read) {
6871 entry = create_proc_entry(ibm->name,
6872 S_IFREG | S_IRUGO | S_IWUSR,
6873 proc_dir);
6874 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006875 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006876 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006877 ret = -ENODEV;
6878 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006879 }
6880 entry->owner = THIS_MODULE;
6881 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006882 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006883 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006884 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006885 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006888 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6889
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006891
6892err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006893 dbg_printk(TPACPI_DBG_INIT,
6894 "%s: at error exit path with result %d\n",
6895 ibm->name, ret);
6896
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006897 ibm_exit(ibm);
6898 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899}
6900
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006901/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006903/* returns 0 - probe ok, or < 0 - probe error.
6904 * Probe ok doesn't mean thinkpad found.
6905 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6906static int __must_check __init get_thinkpad_model_data(
6907 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006908{
Jeff Garzik18552562007-10-03 15:15:40 -04006909 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006910 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006911 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006912
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006913 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006914 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006915
6916 memset(tp, 0, sizeof(*tp));
6917
6918 if (dmi_name_in_vendors("IBM"))
6919 tp->vendor = PCI_VENDOR_ID_IBM;
6920 else if (dmi_name_in_vendors("LENOVO"))
6921 tp->vendor = PCI_VENDOR_ID_LENOVO;
6922 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006923 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006924
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006925 s = dmi_get_system_info(DMI_BIOS_VERSION);
6926 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6927 if (s && !tp->bios_version_str)
6928 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006929 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006930 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006931 tp->bios_model = tp->bios_version_str[0]
6932 | (tp->bios_version_str[1] << 8);
6933
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006934 /*
6935 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6936 * X32 or newer, all Z series; Some models must have an
6937 * up-to-date BIOS or they will not be detected.
6938 *
6939 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6940 */
6941 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006942 if (sscanf(dev->name,
6943 "IBM ThinkPad Embedded Controller -[%17c",
6944 ec_fw_string) == 1) {
6945 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6946 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006947
6948 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006949 if (!tp->ec_version_str)
6950 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006951 tp->ec_model = ec_fw_string[0]
6952 | (ec_fw_string[1] << 8);
6953 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006954 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006955 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006956
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006957 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6958 if (s && !strnicmp(s, "ThinkPad", 8)) {
6959 tp->model_str = kstrdup(s, GFP_KERNEL);
6960 if (!tp->model_str)
6961 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006962 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006963
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006964 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6965 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6966 if (s && !tp->nummodel_str)
6967 return -ENOMEM;
6968
6969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970}
6971
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006972static int __init probe_for_thinkpad(void)
6973{
6974 int is_thinkpad;
6975
6976 if (acpi_disabled)
6977 return -ENODEV;
6978
6979 /*
6980 * Non-ancient models have better DMI tagging, but very old models
6981 * don't.
6982 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006983 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006984
6985 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006986 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006987 if (!ec_handle) {
6988 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006989 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006990 "Not yet supported ThinkPad detected!\n");
6991 return -ENODEV;
6992 }
6993
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006994 /*
6995 * Risks a regression on very old machines, but reduces potential
6996 * false positives a damn great deal
6997 */
6998 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006999 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007000
7001 if (!is_thinkpad && !force_load)
7002 return -ENODEV;
7003
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007004 return 0;
7005}
7006
7007
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007008/* Module init, exit, parameters */
7009
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007010static struct ibm_init_struct ibms_init[] __initdata = {
7011 {
7012 .init = thinkpad_acpi_driver_init,
7013 .data = &thinkpad_acpi_driver_data,
7014 },
7015 {
7016 .init = hotkey_init,
7017 .data = &hotkey_driver_data,
7018 },
7019 {
7020 .init = bluetooth_init,
7021 .data = &bluetooth_driver_data,
7022 },
7023 {
7024 .init = wan_init,
7025 .data = &wan_driver_data,
7026 },
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007027 {
7028 .init = uwb_init,
7029 .data = &uwb_driver_data,
7030 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007031#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007032 {
7033 .init = video_init,
7034 .data = &video_driver_data,
7035 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02007036#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007037 {
7038 .init = light_init,
7039 .data = &light_driver_data,
7040 },
7041#ifdef CONFIG_THINKPAD_ACPI_DOCK
7042 {
7043 .init = dock_init,
7044 .data = &dock_driver_data[0],
7045 },
7046 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03007047 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007048 .data = &dock_driver_data[1],
7049 },
7050#endif
7051#ifdef CONFIG_THINKPAD_ACPI_BAY
7052 {
7053 .init = bay_init,
7054 .data = &bay_driver_data,
7055 },
7056#endif
7057 {
7058 .init = cmos_init,
7059 .data = &cmos_driver_data,
7060 },
7061 {
7062 .init = led_init,
7063 .data = &led_driver_data,
7064 },
7065 {
7066 .init = beep_init,
7067 .data = &beep_driver_data,
7068 },
7069 {
7070 .init = thermal_init,
7071 .data = &thermal_driver_data,
7072 },
7073 {
7074 .data = &ecdump_driver_data,
7075 },
7076 {
7077 .init = brightness_init,
7078 .data = &brightness_driver_data,
7079 },
7080 {
7081 .data = &volume_driver_data,
7082 },
7083 {
7084 .init = fan_init,
7085 .data = &fan_driver_data,
7086 },
7087};
7088
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007089static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7090{
7091 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007092 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007093
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007094 if (!kp || !kp->name || !val)
7095 return -EINVAL;
7096
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007097 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7098 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02007099 WARN_ON(ibm == NULL);
7100
7101 if (!ibm || !ibm->name)
7102 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007103
7104 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7105 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007106 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007107 strcpy(ibms_init[i].param, val);
7108 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007109 return 0;
7110 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007111 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007112
7113 return -EINVAL;
7114}
7115
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007116module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007117MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007118 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007119
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007120module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007121MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03007122
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007123module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007124MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007125 "Attempts to load the driver even on a "
7126 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03007127
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03007128module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007129MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007130 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03007131
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007132module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007133MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007134 "Selects brightness control strategy: "
7135 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03007136
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007137module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007138MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007139 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02007140
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007141module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007142MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007143 "used for backwards compatibility with userspace, "
7144 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007145
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007146#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007147 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02007148 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007149 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007150
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007151TPACPI_PARAM(hotkey);
7152TPACPI_PARAM(bluetooth);
7153TPACPI_PARAM(video);
7154TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007155#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007156TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007157#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007158#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007159TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03007160#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007161TPACPI_PARAM(cmos);
7162TPACPI_PARAM(led);
7163TPACPI_PARAM(beep);
7164TPACPI_PARAM(ecdump);
7165TPACPI_PARAM(brightness);
7166TPACPI_PARAM(volume);
7167TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03007168
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007169#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7170module_param(dbg_wlswemul, uint, 0);
7171MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7172module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7173MODULE_PARM_DESC(wlsw_state,
7174 "Initial state of the emulated WLSW switch");
7175
7176module_param(dbg_bluetoothemul, uint, 0);
7177MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7178module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7179MODULE_PARM_DESC(bluetooth_state,
7180 "Initial state of the emulated bluetooth switch");
7181
7182module_param(dbg_wwanemul, uint, 0);
7183MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7184module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7185MODULE_PARM_DESC(wwan_state,
7186 "Initial state of the emulated WWAN switch");
Henrique de Moraes Holschuh0045c0a2009-01-11 03:01:03 -02007187
7188module_param(dbg_uwbemul, uint, 0);
7189MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7190module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7191MODULE_PARM_DESC(uwb_state,
7192 "Initial state of the emulated UWB switch");
Henrique de Moraes Holschuha73f3092009-01-11 03:01:00 -02007193#endif
7194
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007195static void thinkpad_acpi_module_exit(void)
7196{
7197 struct ibm_struct *ibm, *itmp;
7198
7199 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7200
7201 list_for_each_entry_safe_reverse(ibm, itmp,
7202 &tpacpi_all_drivers,
7203 all_drivers) {
7204 ibm_exit(ibm);
7205 }
7206
7207 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7208
7209 if (tpacpi_inputdev) {
7210 if (tp_features.input_device_registered)
7211 input_unregister_device(tpacpi_inputdev);
7212 else
7213 input_free_device(tpacpi_inputdev);
7214 }
7215
7216 if (tpacpi_hwmon)
7217 hwmon_device_unregister(tpacpi_hwmon);
7218
7219 if (tp_features.sensors_pdev_attrs_registered)
7220 device_remove_file(&tpacpi_sensors_pdev->dev,
7221 &dev_attr_thinkpad_acpi_pdev_name);
7222 if (tpacpi_sensors_pdev)
7223 platform_device_unregister(tpacpi_sensors_pdev);
7224 if (tpacpi_pdev)
7225 platform_device_unregister(tpacpi_pdev);
7226
7227 if (tp_features.sensors_pdrv_attrs_registered)
7228 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7229 if (tp_features.platform_drv_attrs_registered)
7230 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7231
7232 if (tp_features.sensors_pdrv_registered)
7233 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7234
7235 if (tp_features.platform_drv_registered)
7236 platform_driver_unregister(&tpacpi_pdriver);
7237
7238 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007239 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007240
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007241 if (tpacpi_wq)
7242 destroy_workqueue(tpacpi_wq);
7243
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02007244 kfree(thinkpad_id.bios_version_str);
7245 kfree(thinkpad_id.ec_version_str);
7246 kfree(thinkpad_id.model_str);
7247}
7248
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02007249
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007250static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251{
7252 int ret, i;
7253
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007254 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7255
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03007256 /* Parameter checking */
7257 if (hotkey_report_mode > 2)
7258 return -EINVAL;
7259
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007260 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007261
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03007262 ret = get_thinkpad_model_data(&thinkpad_id);
7263 if (ret) {
7264 printk(TPACPI_ERR
7265 "unable to get DMI data: %d\n", ret);
7266 thinkpad_acpi_module_exit();
7267 return ret;
7268 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007269 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007270 if (ret) {
7271 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03007272 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007275 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03007276
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007277 TPACPI_ACPIHANDLE_INIT(ecrd);
7278 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03007280 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7281 if (!tpacpi_wq) {
7282 thinkpad_acpi_module_exit();
7283 return -ENOMEM;
7284 }
7285
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007286 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007288 printk(TPACPI_ERR
7289 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007290 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 return -ENODEV;
7292 }
7293 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04007294
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007295 ret = platform_driver_register(&tpacpi_pdriver);
7296 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007297 printk(TPACPI_ERR
7298 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007299 thinkpad_acpi_module_exit();
7300 return ret;
7301 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03007302 tp_features.platform_drv_registered = 1;
7303
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007304 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7305 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007306 printk(TPACPI_ERR
7307 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007308 thinkpad_acpi_module_exit();
7309 return ret;
7310 }
7311 tp_features.sensors_pdrv_registered = 1;
7312
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007313 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007314 if (!ret) {
7315 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007316 ret = tpacpi_create_driver_attributes(
7317 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007318 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007319 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007320 printk(TPACPI_ERR
7321 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007322 thinkpad_acpi_module_exit();
7323 return ret;
7324 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03007325 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03007326
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007327
7328 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007329 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007330 NULL, 0);
7331 if (IS_ERR(tpacpi_pdev)) {
7332 ret = PTR_ERR(tpacpi_pdev);
7333 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007334 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007335 thinkpad_acpi_module_exit();
7336 return ret;
7337 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007338 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007339 TPACPI_HWMON_DRVR_NAME,
7340 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007341 if (IS_ERR(tpacpi_sensors_pdev)) {
7342 ret = PTR_ERR(tpacpi_sensors_pdev);
7343 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007344 printk(TPACPI_ERR
7345 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007346 thinkpad_acpi_module_exit();
7347 return ret;
7348 }
7349 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7350 &dev_attr_thinkpad_acpi_pdev_name);
7351 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007352 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02007353 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03007354 thinkpad_acpi_module_exit();
7355 return ret;
7356 }
7357 tp_features.sensors_pdev_attrs_registered = 1;
7358 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007359 if (IS_ERR(tpacpi_hwmon)) {
7360 ret = PTR_ERR(tpacpi_hwmon);
7361 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007362 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03007363 thinkpad_acpi_module_exit();
7364 return ret;
7365 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03007366 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007367 tpacpi_inputdev = input_allocate_device();
7368 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007369 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007370 thinkpad_acpi_module_exit();
7371 return -ENOMEM;
7372 } else {
7373 /* Prepare input device, but don't register */
7374 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007375 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007376 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03007377 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7378 thinkpad_id.vendor :
7379 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007380 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7381 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7382 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03007383 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7384 ret = ibm_init(&ibms_init[i]);
7385 if (ret >= 0 && *ibms_init[i].param)
7386 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007388 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 return ret;
7390 }
7391 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007392 ret = input_register_device(tpacpi_inputdev);
7393 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007394 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03007395 thinkpad_acpi_module_exit();
7396 return ret;
7397 } else {
7398 tp_features.input_device_registered = 1;
7399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03007401 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 return 0;
7403}
7404
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007405/* Please remove this in year 2009 */
7406MODULE_ALIAS("ibm_acpi");
7407
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03007408MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7409
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007410/*
7411 * DMI matching for module autoloading
7412 *
7413 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7414 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7415 *
7416 * Only models listed in thinkwiki will be supported, so add yours
7417 * if it is not there yet.
7418 */
7419#define IBM_BIOS_MODULE_ALIAS(__type) \
7420 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
7421
7422/* Non-ancient thinkpads */
7423MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7424MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7425
7426/* Ancient thinkpad BIOSes have to be identified by
7427 * BIOS type or model number, and there are far less
7428 * BIOS types than model numbers... */
7429IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
7430IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
7431IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
7432
7433MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02007434MODULE_DESCRIPTION(TPACPI_DESC);
7435MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02007436MODULE_LICENSE("GPL");
7437
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03007438module_init(thinkpad_acpi_module_init);
7439module_exit(thinkpad_acpi_module_exit);