blob: ee3fa007f312e52c656a2fab87f506f9243b5983 [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
125
126/****************************************************************************
127 * Main driver
128 */
129
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200130#define TPACPI_NAME "thinkpad"
131#define TPACPI_DESC "ThinkPad ACPI Extras"
132#define TPACPI_FILE TPACPI_NAME "_acpi"
133#define TPACPI_URL "http://ibm-acpi.sf.net/"
134#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200135
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200136#define TPACPI_PROC_DIR "ibm"
137#define TPACPI_ACPI_EVENT_PREFIX "ibm"
138#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300139#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200140#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200141
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300142#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300143#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300144
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200145#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200146
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300147/* rfkill switches */
148enum {
149 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
150 TPACPI_RFK_WWAN_SW_ID,
151};
152
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200153/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200154#define TPACPI_LOG TPACPI_FILE ": "
155#define TPACPI_ERR KERN_ERR TPACPI_LOG
156#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
157#define TPACPI_INFO KERN_INFO TPACPI_LOG
158#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200159
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200160#define TPACPI_DBG_ALL 0xffff
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200161#define TPACPI_DBG_INIT 0x0001
162#define TPACPI_DBG_EXIT 0x0002
163#define dbg_printk(a_dbg_level, format, arg...) \
164 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200165 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
166 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200167#ifdef CONFIG_THINKPAD_ACPI_DEBUG
168#define vdbg_printk(a_dbg_level, format, arg...) \
169 dbg_printk(a_dbg_level, format, ## arg)
170static const char *str_supported(int is_supported);
171#else
172#define vdbg_printk(a_dbg_level, format, arg...)
173#endif
174
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200175#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
176#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
177#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200178
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200179
180/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200181 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200182 */
183
184struct ibm_struct;
185
186struct tp_acpi_drv_struct {
187 const struct acpi_device_id *hid;
188 struct acpi_driver *driver;
189
190 void (*notify) (struct ibm_struct *, u32);
191 acpi_handle *handle;
192 u32 type;
193 struct acpi_device *device;
194};
195
196struct ibm_struct {
197 char *name;
198
199 int (*read) (char *);
200 int (*write) (char *);
201 void (*exit) (void);
202 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200203 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200204
205 struct list_head all_drivers;
206
207 struct tp_acpi_drv_struct *acpi;
208
209 struct {
210 u8 acpi_driver_registered:1;
211 u8 acpi_notify_installed:1;
212 u8 proc_created:1;
213 u8 init_called:1;
214 u8 experimental:1;
215 } flags;
216};
217
218struct ibm_init_struct {
219 char param[32];
220
221 int (*init) (struct ibm_init_struct *);
222 struct ibm_struct *data;
223};
224
225static struct {
226#ifdef CONFIG_THINKPAD_ACPI_BAY
227 u32 bay_status:1;
228 u32 bay_eject:1;
229 u32 bay_status2:1;
230 u32 bay_eject2:1;
231#endif
232 u32 bluetooth:1;
233 u32 hotkey:1;
234 u32 hotkey_mask:1;
235 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200236 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200237 u32 light:1;
238 u32 light_status:1;
239 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300240 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200241 u32 wan:1;
242 u32 fan_ctrl_status_undef:1;
243 u32 input_device_registered:1;
244 u32 platform_drv_registered:1;
245 u32 platform_drv_attrs_registered:1;
246 u32 sensors_pdrv_registered:1;
247 u32 sensors_pdrv_attrs_registered:1;
248 u32 sensors_pdev_attrs_registered:1;
249 u32 hotkey_poll_active:1;
250} tp_features;
251
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300252static struct {
253 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300254 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300255} tp_warned;
256
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200257struct thinkpad_id_data {
258 unsigned int vendor; /* ThinkPad vendor:
259 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
260
261 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
262 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
263
264 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
265 u16 ec_model;
266
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300267 char *model_str; /* ThinkPad T43 */
268 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200269};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200270static struct thinkpad_id_data thinkpad_id;
271
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300272static enum {
273 TPACPI_LIFE_INIT = 0,
274 TPACPI_LIFE_RUNNING,
275 TPACPI_LIFE_EXITING,
276} tpacpi_lifecycle;
277
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200278static int experimental;
279static u32 dbg_level;
280
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300281static struct workqueue_struct *tpacpi_wq;
282
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300283/* Special LED class that can defer work */
284struct tpacpi_led_classdev {
285 struct led_classdev led_classdev;
286 struct work_struct work;
287 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300288 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300289};
290
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300291/****************************************************************************
292 ****************************************************************************
293 *
294 * ACPI Helpers and device model
295 *
296 ****************************************************************************
297 ****************************************************************************/
298
299/*************************************************************************
300 * ACPI basic handles
301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300303static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200305#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 static acpi_handle object##_handle; \
307 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400308 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 static char *object##_paths[] = { paths }
310
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200311TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400312 "\\_SB.PCI.ISA.EC", /* 570 */
313 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
314 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
315 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
316 "\\_SB.PCI0.ICH3.EC0", /* R31 */
317 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300318 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200320TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
321TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200323TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
324 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400325 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
326 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300327 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400328
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200329TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400330 "^HKEY", /* R30, R31 */
331 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300332 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400333
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200334TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
335 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
336 "\\_SB.PCI0.VID0", /* 770e */
337 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
338 "\\_SB.PCI0.AGP.VID", /* all others */
339 ); /* R30, R31 */
340
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400341
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300342/*************************************************************************
343 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200344 */
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static int acpi_evalf(acpi_handle handle,
347 void *res, char *method, char *fmt, ...)
348{
349 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400350 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200351 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400352 struct acpi_buffer result, *resultp;
353 union acpi_object out_obj;
354 acpi_status status;
355 va_list ap;
356 char res_type;
357 int success;
358 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200361 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363 }
364
365 if (*fmt == 'q') {
366 quiet = 1;
367 fmt++;
368 } else
369 quiet = 0;
370
371 res_type = *(fmt++);
372
373 params.count = 0;
374 params.pointer = &in_objs[0];
375
376 va_start(ap, fmt);
377 while (*fmt) {
378 char c = *(fmt++);
379 switch (c) {
380 case 'd': /* int */
381 in_objs[params.count].integer.value = va_arg(ap, int);
382 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
383 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400384 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200386 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 "with invalid format character '%c'\n", c);
388 return 0;
389 }
390 }
391 va_end(ap);
392
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400393 if (res_type != 'v') {
394 result.length = sizeof(out_obj);
395 result.pointer = &out_obj;
396 resultp = &result;
397 } else
398 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400400 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400403 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (res)
405 *(int *)res = out_obj.integer.value;
406 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
407 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400408 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 success = status == AE_OK;
410 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400411 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200413 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 "with invalid format character '%c'\n", res_type);
415 return 0;
416 }
417
418 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200419 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 method, fmt0, status);
421
422 return success;
423}
424
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200425static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300426{
427 int v;
428
429 if (ecrd_handle) {
430 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
431 return 0;
432 *p = v;
433 } else {
434 if (ec_read(i, p) < 0)
435 return 0;
436 }
437
438 return 1;
439}
440
441static int acpi_ec_write(int i, u8 v)
442{
443 if (ecwr_handle) {
444 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
445 return 0;
446 } else {
447 if (ec_write(i, v) < 0)
448 return 0;
449 }
450
451 return 1;
452}
453
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200454#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300455static int _sta(acpi_handle handle)
456{
457 int status;
458
459 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
460 status = 0;
461
462 return status;
463}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200464#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300465
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300466static int issue_thinkpad_cmos_command(int cmos_cmd)
467{
468 if (!cmos_handle)
469 return -ENXIO;
470
471 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
472 return -EIO;
473
474 return 0;
475}
476
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300477/*************************************************************************
478 * ACPI device model
479 */
480
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200481#define TPACPI_ACPIHANDLE_INIT(object) \
482 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200483 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
484
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300485static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300486 acpi_handle *handle, acpi_handle parent,
487 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300488{
489 int i;
490 acpi_status status;
491
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300492 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
493 name);
494
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300495 for (i = 0; i < num_paths; i++) {
496 status = acpi_get_handle(parent, paths[i], handle);
497 if (ACPI_SUCCESS(status)) {
498 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300499 dbg_printk(TPACPI_DBG_INIT,
500 "Found ACPI handle %s for %s\n",
501 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300502 return;
503 }
504 }
505
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300506 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
507 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300508 *handle = NULL;
509}
510
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300511static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300512{
513 struct ibm_struct *ibm = data;
514
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300515 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
516 return;
517
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300518 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519 return;
520
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300521 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300522}
523
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300524static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300525{
526 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300527 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300528
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300529 BUG_ON(!ibm->acpi);
530
531 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300532 return 0;
533
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300534 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300535 "setting up ACPI notify for %s\n", ibm->name);
536
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300537 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
538 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200539 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300540 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541 return -ENODEV;
542 }
543
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700544 ibm->acpi->device->driver_data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300545 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200546 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300547 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300548
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300549 status = acpi_install_notify_handler(*ibm->acpi->handle,
550 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300551 if (ACPI_FAILURE(status)) {
552 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200553 printk(TPACPI_NOTICE
554 "another device driver is already "
555 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300556 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200557 printk(TPACPI_ERR
558 "acpi_install_notify_handler(%s) failed: %d\n",
559 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300560 }
561 return -ENODEV;
562 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300563 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300564 return 0;
565}
566
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300567static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300568{
569 return 0;
570}
571
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300572static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300573{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300574 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300575
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300576 dbg_printk(TPACPI_DBG_INIT,
577 "registering %s as an ACPI driver\n", ibm->name);
578
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300579 BUG_ON(!ibm->acpi);
580
581 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
582 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300583 printk(TPACPI_ERR
584 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300585 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300586 }
587
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200588 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300589 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200590
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300591 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300592
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300593 rc = acpi_bus_register_driver(ibm->acpi->driver);
594 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200595 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200596 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300597 kfree(ibm->acpi->driver);
598 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300599 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300600 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300601
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300602 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300603}
604
605
606/****************************************************************************
607 ****************************************************************************
608 *
609 * Procfs Helpers
610 *
611 ****************************************************************************
612 ****************************************************************************/
613
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300614static int dispatch_procfs_read(char *page, char **start, off_t off,
615 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300616{
617 struct ibm_struct *ibm = data;
618 int len;
619
620 if (!ibm || !ibm->read)
621 return -EINVAL;
622
623 len = ibm->read(page);
624 if (len < 0)
625 return len;
626
627 if (len <= off + count)
628 *eof = 1;
629 *start = page + off;
630 len -= off;
631 if (len > count)
632 len = count;
633 if (len < 0)
634 len = 0;
635
636 return len;
637}
638
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300639static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200640 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300641 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300642{
643 struct ibm_struct *ibm = data;
644 char *kernbuf;
645 int ret;
646
647 if (!ibm || !ibm->write)
648 return -EINVAL;
649
650 kernbuf = kmalloc(count + 2, GFP_KERNEL);
651 if (!kernbuf)
652 return -ENOMEM;
653
654 if (copy_from_user(kernbuf, userbuf, count)) {
655 kfree(kernbuf);
656 return -EFAULT;
657 }
658
659 kernbuf[count] = 0;
660 strcat(kernbuf, ",");
661 ret = ibm->write(kernbuf);
662 if (ret == 0)
663 ret = count;
664
665 kfree(kernbuf);
666
667 return ret;
668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670static char *next_cmd(char **cmds)
671{
672 char *start = *cmds;
673 char *end;
674
675 while ((end = strchr(start, ',')) && end == start)
676 start = end + 1;
677
678 if (!end)
679 return NULL;
680
681 *end = 0;
682 *cmds = end + 1;
683 return start;
684}
685
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300686
687/****************************************************************************
688 ****************************************************************************
689 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300690 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300691 *
692 ****************************************************************************
693 ****************************************************************************/
694
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300695static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300696static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700697static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300698static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300699static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200700static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300701
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200702static int tpacpi_suspend_handler(struct platform_device *pdev,
703 pm_message_t state)
704{
705 struct ibm_struct *ibm, *itmp;
706
707 list_for_each_entry_safe(ibm, itmp,
708 &tpacpi_all_drivers,
709 all_drivers) {
710 if (ibm->suspend)
711 (ibm->suspend)(state);
712 }
713
714 return 0;
715}
716
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300717static int tpacpi_resume_handler(struct platform_device *pdev)
718{
719 struct ibm_struct *ibm, *itmp;
720
721 list_for_each_entry_safe(ibm, itmp,
722 &tpacpi_all_drivers,
723 all_drivers) {
724 if (ibm->resume)
725 (ibm->resume)();
726 }
727
728 return 0;
729}
730
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300731static struct platform_driver tpacpi_pdriver = {
732 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200733 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300734 .owner = THIS_MODULE,
735 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200736 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300737 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300738};
739
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300740static struct platform_driver tpacpi_hwmon_pdriver = {
741 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200742 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300743 .owner = THIS_MODULE,
744 },
745};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300746
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300747/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300748 * sysfs support helpers
749 */
750
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200751struct attribute_set {
752 unsigned int members, max_members;
753 struct attribute_group group;
754};
755
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300756struct attribute_set_obj {
757 struct attribute_set s;
758 struct attribute *a;
759} __attribute__((packed));
760
761static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200762 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300763{
764 struct attribute_set_obj *sobj;
765
766 if (max_members == 0)
767 return NULL;
768
769 /* Allocates space for implicit NULL at the end too */
770 sobj = kzalloc(sizeof(struct attribute_set_obj) +
771 max_members * sizeof(struct attribute *),
772 GFP_KERNEL);
773 if (!sobj)
774 return NULL;
775 sobj->s.max_members = max_members;
776 sobj->s.group.attrs = &sobj->a;
777 sobj->s.group.name = name;
778
779 return &sobj->s;
780}
781
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200782#define destroy_attr_set(_set) \
783 kfree(_set);
784
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300785/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200786static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300787{
788 if (!s || !attr)
789 return -EINVAL;
790
791 if (s->members >= s->max_members)
792 return -ENOMEM;
793
794 s->group.attrs[s->members] = attr;
795 s->members++;
796
797 return 0;
798}
799
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200800static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300801 struct attribute **attr,
802 unsigned int count)
803{
804 int i, res;
805
806 for (i = 0; i < count; i++) {
807 res = add_to_attr_set(s, attr[i]);
808 if (res)
809 return res;
810 }
811
812 return 0;
813}
814
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200815static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300816{
817 sysfs_remove_group(kobj, &s->group);
818 destroy_attr_set(s);
819}
820
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200821#define register_attr_set_with_sysfs(_attr_set, _kobj) \
822 sysfs_create_group(_kobj, &_attr_set->group)
823
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300824static int parse_strtoul(const char *buf,
825 unsigned long max, unsigned long *value)
826{
827 char *endp;
828
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300829 while (*buf && isspace(*buf))
830 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300831 *value = simple_strtoul(buf, &endp, 0);
832 while (*endp && isspace(*endp))
833 endp++;
834 if (*endp || *value > max)
835 return -EINVAL;
836
837 return 0;
838}
839
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300840static void tpacpi_disable_brightness_delay(void)
841{
842 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
843 printk(TPACPI_NOTICE
844 "ACPI backlight control delay disabled\n");
845}
846
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300847static int __init tpacpi_query_bcl_levels(acpi_handle handle)
848{
849 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
850 union acpi_object *obj;
851 int rc;
852
853 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
854 obj = (union acpi_object *)buffer.pointer;
855 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
856 printk(TPACPI_ERR "Unknown _BCL data, "
857 "please report this to %s\n", TPACPI_MAIL);
858 rc = 0;
859 } else {
860 rc = obj->package.count;
861 }
862 } else {
863 return 0;
864 }
865
866 kfree(buffer.pointer);
867 return rc;
868}
869
870static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
871 u32 lvl, void *context, void **rv)
872{
873 char name[ACPI_PATH_SEGMENT_LENGTH];
874 struct acpi_buffer buffer = { sizeof(name), &name };
875
876 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
877 !strncmp("_BCL", name, sizeof(name) - 1)) {
878 BUG_ON(!rv || !*rv);
879 **(int **)rv = tpacpi_query_bcl_levels(handle);
880 return AE_CTRL_TERMINATE;
881 } else {
882 return AE_OK;
883 }
884}
885
886/*
887 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
888 */
889static int __init tpacpi_check_std_acpi_brightness_support(void)
890{
891 int status;
892 int bcl_levels = 0;
893 void *bcl_ptr = &bcl_levels;
894
895 if (!vid_handle) {
896 TPACPI_ACPIHANDLE_INIT(vid);
897 }
898 if (!vid_handle)
899 return 0;
900
901 /*
902 * Search for a _BCL method, and execute it. This is safe on all
903 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
904 * BIOS in ACPI backlight control mode. We do NOT have to care
905 * about calling the _BCL method in an enabled video device, any
906 * will do for our purposes.
907 */
908
909 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
910 tpacpi_acpi_walk_find_bcl, NULL,
911 &bcl_ptr);
912
913 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
914 tp_features.bright_acpimode = 1;
915 return (bcl_levels - 2);
916 }
917
918 return 0;
919}
920
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300921static int __init tpacpi_new_rfkill(const unsigned int id,
922 struct rfkill **rfk,
923 const enum rfkill_type rfktype,
924 const char *name,
925 int (*toggle_radio)(void *, enum rfkill_state),
926 int (*get_state)(void *, enum rfkill_state *))
927{
928 int res;
929 enum rfkill_state initial_state;
930
931 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
932 if (!*rfk) {
933 printk(TPACPI_ERR
934 "failed to allocate memory for rfkill class\n");
935 return -ENOMEM;
936 }
937
938 (*rfk)->name = name;
939 (*rfk)->get_state = get_state;
940 (*rfk)->toggle_radio = toggle_radio;
941
942 if (!get_state(NULL, &initial_state))
943 (*rfk)->state = initial_state;
944
945 res = rfkill_register(*rfk);
946 if (res < 0) {
947 printk(TPACPI_ERR
948 "failed to register %s rfkill switch: %d\n",
949 name, res);
950 rfkill_free(*rfk);
951 *rfk = NULL;
952 return res;
953 }
954
955 return 0;
956}
957
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200958/*************************************************************************
959 * thinkpad-acpi driver attributes
960 */
961
962/* interface_version --------------------------------------------------- */
963static ssize_t tpacpi_driver_interface_version_show(
964 struct device_driver *drv,
965 char *buf)
966{
967 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
968}
969
970static DRIVER_ATTR(interface_version, S_IRUGO,
971 tpacpi_driver_interface_version_show, NULL);
972
973/* debug_level --------------------------------------------------------- */
974static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
975 char *buf)
976{
977 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
978}
979
980static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
981 const char *buf, size_t count)
982{
983 unsigned long t;
984
985 if (parse_strtoul(buf, 0xffff, &t))
986 return -EINVAL;
987
988 dbg_level = t;
989
990 return count;
991}
992
993static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
994 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
995
996/* version ------------------------------------------------------------- */
997static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
998 char *buf)
999{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001000 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1001 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001002}
1003
1004static DRIVER_ATTR(version, S_IRUGO,
1005 tpacpi_driver_version_show, NULL);
1006
1007/* --------------------------------------------------------------------- */
1008
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001009static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001010 &driver_attr_debug_level, &driver_attr_version,
1011 &driver_attr_interface_version,
1012};
1013
1014static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1015{
1016 int i, res;
1017
1018 i = 0;
1019 res = 0;
1020 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1021 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1022 i++;
1023 }
1024
1025 return res;
1026}
1027
1028static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1029{
1030 int i;
1031
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001032 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001033 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1034}
1035
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001036/****************************************************************************
1037 ****************************************************************************
1038 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001039 * Subdrivers
1040 *
1041 ****************************************************************************
1042 ****************************************************************************/
1043
1044/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001045 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001046 */
1047
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001048static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001050 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1051 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001053 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001054 (thinkpad_id.bios_version_str) ?
1055 thinkpad_id.bios_version_str : "unknown",
1056 (thinkpad_id.ec_version_str) ?
1057 thinkpad_id.ec_version_str : "unknown");
1058
1059 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001060 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001061 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1062 "IBM" : ((thinkpad_id.vendor ==
1063 PCI_VENDOR_ID_LENOVO) ?
1064 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001065 thinkpad_id.model_str,
1066 (thinkpad_id.nummodel_str) ?
1067 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return 0;
1070}
1071
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001072static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 int len = 0;
1075
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001076 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1077 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 return len;
1080}
1081
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001082static struct ibm_struct thinkpad_acpi_driver_data = {
1083 .name = "driver",
1084 .read = thinkpad_acpi_driver_read,
1085};
1086
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001087/*************************************************************************
1088 * Hotkey subdriver
1089 */
1090
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001091enum { /* hot key scan codes (derived from ACPI DSDT) */
1092 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1093 TP_ACPI_HOTKEYSCAN_FNF2,
1094 TP_ACPI_HOTKEYSCAN_FNF3,
1095 TP_ACPI_HOTKEYSCAN_FNF4,
1096 TP_ACPI_HOTKEYSCAN_FNF5,
1097 TP_ACPI_HOTKEYSCAN_FNF6,
1098 TP_ACPI_HOTKEYSCAN_FNF7,
1099 TP_ACPI_HOTKEYSCAN_FNF8,
1100 TP_ACPI_HOTKEYSCAN_FNF9,
1101 TP_ACPI_HOTKEYSCAN_FNF10,
1102 TP_ACPI_HOTKEYSCAN_FNF11,
1103 TP_ACPI_HOTKEYSCAN_FNF12,
1104 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1105 TP_ACPI_HOTKEYSCAN_FNINSERT,
1106 TP_ACPI_HOTKEYSCAN_FNDELETE,
1107 TP_ACPI_HOTKEYSCAN_FNHOME,
1108 TP_ACPI_HOTKEYSCAN_FNEND,
1109 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1110 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1111 TP_ACPI_HOTKEYSCAN_FNSPACE,
1112 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1113 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1114 TP_ACPI_HOTKEYSCAN_MUTE,
1115 TP_ACPI_HOTKEYSCAN_THINKPAD,
1116};
1117
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001118enum { /* Keys available through NVRAM polling */
1119 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1120 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1121};
1122
1123enum { /* Positions of some of the keys in hotkey masks */
1124 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1125 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1126 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1127 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1128 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1129 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1130 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1131 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1132 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1133 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1134 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1135};
1136
1137enum { /* NVRAM to ACPI HKEY group map */
1138 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1139 TP_ACPI_HKEY_ZOOM_MASK |
1140 TP_ACPI_HKEY_DISPSWTCH_MASK |
1141 TP_ACPI_HKEY_HIBERNATE_MASK,
1142 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1143 TP_ACPI_HKEY_BRGHTDWN_MASK,
1144 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1145 TP_ACPI_HKEY_VOLDWN_MASK |
1146 TP_ACPI_HKEY_MUTE_MASK,
1147};
1148
1149#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1150struct tp_nvram_state {
1151 u16 thinkpad_toggle:1;
1152 u16 zoom_toggle:1;
1153 u16 display_toggle:1;
1154 u16 thinklight_toggle:1;
1155 u16 hibernate_toggle:1;
1156 u16 displayexp_toggle:1;
1157 u16 display_state:1;
1158 u16 brightness_toggle:1;
1159 u16 volume_toggle:1;
1160 u16 mute:1;
1161
1162 u8 brightness_level;
1163 u8 volume_level;
1164};
1165
1166static struct task_struct *tpacpi_hotkey_task;
1167static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1168static int hotkey_poll_freq = 10; /* Hz */
1169static struct mutex hotkey_thread_mutex;
1170static struct mutex hotkey_thread_data_mutex;
1171static unsigned int hotkey_config_change;
1172
1173#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1174
1175#define hotkey_source_mask 0U
1176
1177#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1178
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001179static struct mutex hotkey_mutex;
1180
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001181static enum { /* Reasons for waking up */
1182 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1183 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1184 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1185} hotkey_wakeup_reason;
1186
1187static int hotkey_autosleep_ack;
1188
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001189static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001190static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001191static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001192static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001193static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001194
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001195static unsigned int hotkey_report_mode;
1196
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001197static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001198
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001199static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001200
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001201#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1202#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001203 do { \
1204 mutex_lock(&hotkey_thread_data_mutex); \
1205 hotkey_config_change++; \
1206 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001207#define HOTKEY_CONFIG_CRITICAL_END \
1208 mutex_unlock(&hotkey_thread_data_mutex);
1209#else
1210#define HOTKEY_CONFIG_CRITICAL_START
1211#define HOTKEY_CONFIG_CRITICAL_END
1212#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1213
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001214/* HKEY.MHKG() return bits */
1215#define TP_HOTKEY_TABLET_MASK (1 << 3)
1216
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001217static int hotkey_get_wlsw(int *status)
1218{
1219 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1220 return -EIO;
1221 return 0;
1222}
1223
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001224static int hotkey_get_tablet_mode(int *status)
1225{
1226 int s;
1227
1228 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1229 return -EIO;
1230
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001231 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1232 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001233}
1234
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001235/*
1236 * Call with hotkey_mutex held
1237 */
1238static int hotkey_mask_get(void)
1239{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001240 u32 m = 0;
1241
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001242 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001243 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001244 return -EIO;
1245 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001246 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001247
1248 return 0;
1249}
1250
1251/*
1252 * Call with hotkey_mutex held
1253 */
1254static int hotkey_mask_set(u32 mask)
1255{
1256 int i;
1257 int rc = 0;
1258
1259 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001260 if (!tp_warned.hotkey_mask_ff &&
1261 (mask == 0xffff || mask == 0xffffff ||
1262 mask == 0xffffffff)) {
1263 tp_warned.hotkey_mask_ff = 1;
1264 printk(TPACPI_NOTICE
1265 "setting the hotkey mask to 0x%08x is likely "
1266 "not the best way to go about it\n", mask);
1267 printk(TPACPI_NOTICE
1268 "please consider using the driver defaults, "
1269 "and refer to up-to-date thinkpad-acpi "
1270 "documentation\n");
1271 }
1272
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001273 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001274 for (i = 0; i < 32; i++) {
1275 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001276 /* enable in firmware mask only keys not in NVRAM
1277 * mode, but enable the key in the cached hotkey_mask
1278 * regardless of mode, or the key will end up
1279 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001280 if (!acpi_evalf(hkey_handle,
1281 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001282 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001283 rc = -EIO;
1284 break;
1285 } else {
1286 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1287 }
1288 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001289 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001290
1291 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001292 if (!hotkey_mask_get() && !rc &&
1293 (hotkey_mask & ~hotkey_source_mask) !=
1294 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001295 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001296 "requested hot key mask 0x%08x, but "
1297 "firmware forced it to 0x%08x\n",
1298 mask, hotkey_mask);
1299 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001300 } else {
1301#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1302 HOTKEY_CONFIG_CRITICAL_START
1303 hotkey_mask = mask & hotkey_source_mask;
1304 HOTKEY_CONFIG_CRITICAL_END
1305 hotkey_mask_get();
1306 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001307 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001308 "requested hot key mask 0x%08x, "
1309 "forced to 0x%08x (NVRAM poll mask is "
1310 "0x%08x): no firmware mask support\n",
1311 mask, hotkey_mask, hotkey_source_mask);
1312 }
1313#else
1314 hotkey_mask_get();
1315 rc = -ENXIO;
1316#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001317 }
1318
1319 return rc;
1320}
1321
1322static int hotkey_status_get(int *status)
1323{
1324 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1325 return -EIO;
1326
1327 return 0;
1328}
1329
1330static int hotkey_status_set(int status)
1331{
1332 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1333 return -EIO;
1334
1335 return 0;
1336}
1337
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001338static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001339{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001340 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001341
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001342 if (tp_features.hotkey_tablet &&
1343 !hotkey_get_tablet_mode(&state)) {
1344 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001345
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001346 input_report_switch(tpacpi_inputdev,
1347 SW_TABLET_MODE, !!state);
1348 input_sync(tpacpi_inputdev);
1349
1350 mutex_unlock(&tpacpi_inputdev_send_mutex);
1351 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001352}
1353
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001354static void tpacpi_input_send_key(unsigned int scancode)
1355{
1356 unsigned int keycode;
1357
1358 keycode = hotkey_keycode_map[scancode];
1359
1360 if (keycode != KEY_RESERVED) {
1361 mutex_lock(&tpacpi_inputdev_send_mutex);
1362
1363 input_report_key(tpacpi_inputdev, keycode, 1);
1364 if (keycode == KEY_UNKNOWN)
1365 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1366 scancode);
1367 input_sync(tpacpi_inputdev);
1368
1369 input_report_key(tpacpi_inputdev, keycode, 0);
1370 if (keycode == KEY_UNKNOWN)
1371 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1372 scancode);
1373 input_sync(tpacpi_inputdev);
1374
1375 mutex_unlock(&tpacpi_inputdev_send_mutex);
1376 }
1377}
1378
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001379#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1380static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1381
1382static void tpacpi_hotkey_send_key(unsigned int scancode)
1383{
1384 tpacpi_input_send_key(scancode);
1385 if (hotkey_report_mode < 2) {
1386 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1387 0x80, 0x1001 + scancode);
1388 }
1389}
1390
1391static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1392{
1393 u8 d;
1394
1395 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1396 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1397 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1398 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1399 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1400 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1401 }
1402 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1403 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1404 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1405 }
1406 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1407 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1408 n->displayexp_toggle =
1409 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1410 }
1411 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1412 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1413 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1414 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1415 n->brightness_toggle =
1416 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1417 }
1418 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1419 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1420 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1421 >> TP_NVRAM_POS_LEVEL_VOLUME;
1422 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1423 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1424 }
1425}
1426
1427#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001428 do { \
1429 if ((mask & (1 << __scancode)) && \
1430 oldn->__member != newn->__member) \
1431 tpacpi_hotkey_send_key(__scancode); \
1432 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001433
1434#define TPACPI_MAY_SEND_KEY(__scancode) \
1435 do { if (mask & (1 << __scancode)) \
1436 tpacpi_hotkey_send_key(__scancode); } while (0)
1437
1438static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001439 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001440 u32 mask)
1441{
1442 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1443 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1444 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1445 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1446
1447 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1448
1449 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1450
1451 /* handle volume */
1452 if (oldn->volume_toggle != newn->volume_toggle) {
1453 if (oldn->mute != newn->mute) {
1454 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1455 }
1456 if (oldn->volume_level > newn->volume_level) {
1457 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1458 } else if (oldn->volume_level < newn->volume_level) {
1459 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1460 } else if (oldn->mute == newn->mute) {
1461 /* repeated key presses that didn't change state */
1462 if (newn->mute) {
1463 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1464 } else if (newn->volume_level != 0) {
1465 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1466 } else {
1467 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1468 }
1469 }
1470 }
1471
1472 /* handle brightness */
1473 if (oldn->brightness_toggle != newn->brightness_toggle) {
1474 if (oldn->brightness_level < newn->brightness_level) {
1475 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1476 } else if (oldn->brightness_level > newn->brightness_level) {
1477 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1478 } else {
1479 /* repeated key presses that didn't change state */
1480 if (newn->brightness_level != 0) {
1481 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1482 } else {
1483 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1484 }
1485 }
1486 }
1487}
1488
1489#undef TPACPI_COMPARE_KEY
1490#undef TPACPI_MAY_SEND_KEY
1491
1492static int hotkey_kthread(void *data)
1493{
1494 struct tp_nvram_state s[2];
1495 u32 mask;
1496 unsigned int si, so;
1497 unsigned long t;
1498 unsigned int change_detector, must_reset;
1499
1500 mutex_lock(&hotkey_thread_mutex);
1501
1502 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1503 goto exit;
1504
1505 set_freezable();
1506
1507 so = 0;
1508 si = 1;
1509 t = 0;
1510
1511 /* Initial state for compares */
1512 mutex_lock(&hotkey_thread_data_mutex);
1513 change_detector = hotkey_config_change;
1514 mask = hotkey_source_mask & hotkey_mask;
1515 mutex_unlock(&hotkey_thread_data_mutex);
1516 hotkey_read_nvram(&s[so], mask);
1517
1518 while (!kthread_should_stop() && hotkey_poll_freq) {
1519 if (t == 0)
1520 t = 1000/hotkey_poll_freq;
1521 t = msleep_interruptible(t);
1522 if (unlikely(kthread_should_stop()))
1523 break;
1524 must_reset = try_to_freeze();
1525 if (t > 0 && !must_reset)
1526 continue;
1527
1528 mutex_lock(&hotkey_thread_data_mutex);
1529 if (must_reset || hotkey_config_change != change_detector) {
1530 /* forget old state on thaw or config change */
1531 si = so;
1532 t = 0;
1533 change_detector = hotkey_config_change;
1534 }
1535 mask = hotkey_source_mask & hotkey_mask;
1536 mutex_unlock(&hotkey_thread_data_mutex);
1537
1538 if (likely(mask)) {
1539 hotkey_read_nvram(&s[si], mask);
1540 if (likely(si != so)) {
1541 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001542 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001543 }
1544 }
1545
1546 so = si;
1547 si ^= 1;
1548 }
1549
1550exit:
1551 mutex_unlock(&hotkey_thread_mutex);
1552 return 0;
1553}
1554
1555static void hotkey_poll_stop_sync(void)
1556{
1557 if (tpacpi_hotkey_task) {
1558 if (frozen(tpacpi_hotkey_task) ||
1559 freezing(tpacpi_hotkey_task))
1560 thaw_process(tpacpi_hotkey_task);
1561
1562 kthread_stop(tpacpi_hotkey_task);
1563 tpacpi_hotkey_task = NULL;
1564 mutex_lock(&hotkey_thread_mutex);
1565 /* at this point, the thread did exit */
1566 mutex_unlock(&hotkey_thread_mutex);
1567 }
1568}
1569
1570/* call with hotkey_mutex held */
1571static void hotkey_poll_setup(int may_warn)
1572{
1573 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1574 hotkey_poll_freq > 0 &&
1575 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1576 if (!tpacpi_hotkey_task) {
1577 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001578 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001579 if (IS_ERR(tpacpi_hotkey_task)) {
1580 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001581 printk(TPACPI_ERR
1582 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001583 "for hotkey polling\n");
1584 }
1585 }
1586 } else {
1587 hotkey_poll_stop_sync();
1588 if (may_warn &&
1589 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001590 printk(TPACPI_NOTICE
1591 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001592 "which is currently disabled\n",
1593 hotkey_source_mask);
1594 }
1595 }
1596}
1597
1598static void hotkey_poll_setup_safe(int may_warn)
1599{
1600 mutex_lock(&hotkey_mutex);
1601 hotkey_poll_setup(may_warn);
1602 mutex_unlock(&hotkey_mutex);
1603}
1604
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001605#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1606
1607static void hotkey_poll_setup_safe(int __unused)
1608{
1609}
1610
1611#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1612
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001613static int hotkey_inputdev_open(struct input_dev *dev)
1614{
1615 switch (tpacpi_lifecycle) {
1616 case TPACPI_LIFE_INIT:
1617 /*
1618 * hotkey_init will call hotkey_poll_setup_safe
1619 * at the appropriate moment
1620 */
1621 return 0;
1622 case TPACPI_LIFE_EXITING:
1623 return -EBUSY;
1624 case TPACPI_LIFE_RUNNING:
1625 hotkey_poll_setup_safe(0);
1626 return 0;
1627 }
1628
1629 /* Should only happen if tpacpi_lifecycle is corrupt */
1630 BUG();
1631 return -EBUSY;
1632}
1633
1634static void hotkey_inputdev_close(struct input_dev *dev)
1635{
1636 /* disable hotkey polling when possible */
1637 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1638 hotkey_poll_setup_safe(0);
1639}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001640
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001641/* sysfs hotkey enable ------------------------------------------------- */
1642static ssize_t hotkey_enable_show(struct device *dev,
1643 struct device_attribute *attr,
1644 char *buf)
1645{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001646 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001647
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001648 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001649 if (res)
1650 return res;
1651
1652 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1653}
1654
1655static ssize_t hotkey_enable_store(struct device *dev,
1656 struct device_attribute *attr,
1657 const char *buf, size_t count)
1658{
1659 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001660 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001661
1662 if (parse_strtoul(buf, 1, &t))
1663 return -EINVAL;
1664
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001665 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001666
1667 return (res) ? res : count;
1668}
1669
1670static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001671 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001672 hotkey_enable_show, hotkey_enable_store);
1673
1674/* sysfs hotkey mask --------------------------------------------------- */
1675static ssize_t hotkey_mask_show(struct device *dev,
1676 struct device_attribute *attr,
1677 char *buf)
1678{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001679 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001680
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001681 if (mutex_lock_interruptible(&hotkey_mutex))
1682 return -ERESTARTSYS;
1683 res = hotkey_mask_get();
1684 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001685
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001686 return (res)?
1687 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001688}
1689
1690static ssize_t hotkey_mask_store(struct device *dev,
1691 struct device_attribute *attr,
1692 const char *buf, size_t count)
1693{
1694 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001695 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001696
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001697 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001698 return -EINVAL;
1699
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001700 if (mutex_lock_interruptible(&hotkey_mutex))
1701 return -ERESTARTSYS;
1702
1703 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001704
1705#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1706 hotkey_poll_setup(1);
1707#endif
1708
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001709 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001710
1711 return (res) ? res : count;
1712}
1713
1714static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001715 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001716 hotkey_mask_show, hotkey_mask_store);
1717
1718/* sysfs hotkey bios_enabled ------------------------------------------- */
1719static ssize_t hotkey_bios_enabled_show(struct device *dev,
1720 struct device_attribute *attr,
1721 char *buf)
1722{
1723 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1724}
1725
1726static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001727 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001728
1729/* sysfs hotkey bios_mask ---------------------------------------------- */
1730static ssize_t hotkey_bios_mask_show(struct device *dev,
1731 struct device_attribute *attr,
1732 char *buf)
1733{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001734 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001735}
1736
1737static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001738 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001739
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001740/* sysfs hotkey all_mask ----------------------------------------------- */
1741static ssize_t hotkey_all_mask_show(struct device *dev,
1742 struct device_attribute *attr,
1743 char *buf)
1744{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001745 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1746 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001747}
1748
1749static struct device_attribute dev_attr_hotkey_all_mask =
1750 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1751
1752/* sysfs hotkey recommended_mask --------------------------------------- */
1753static ssize_t hotkey_recommended_mask_show(struct device *dev,
1754 struct device_attribute *attr,
1755 char *buf)
1756{
1757 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001758 (hotkey_all_mask | hotkey_source_mask)
1759 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001760}
1761
1762static struct device_attribute dev_attr_hotkey_recommended_mask =
1763 __ATTR(hotkey_recommended_mask, S_IRUGO,
1764 hotkey_recommended_mask_show, NULL);
1765
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001766#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1767
1768/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1769static ssize_t hotkey_source_mask_show(struct device *dev,
1770 struct device_attribute *attr,
1771 char *buf)
1772{
1773 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1774}
1775
1776static ssize_t hotkey_source_mask_store(struct device *dev,
1777 struct device_attribute *attr,
1778 const char *buf, size_t count)
1779{
1780 unsigned long t;
1781
1782 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1783 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1784 return -EINVAL;
1785
1786 if (mutex_lock_interruptible(&hotkey_mutex))
1787 return -ERESTARTSYS;
1788
1789 HOTKEY_CONFIG_CRITICAL_START
1790 hotkey_source_mask = t;
1791 HOTKEY_CONFIG_CRITICAL_END
1792
1793 hotkey_poll_setup(1);
1794
1795 mutex_unlock(&hotkey_mutex);
1796
1797 return count;
1798}
1799
1800static struct device_attribute dev_attr_hotkey_source_mask =
1801 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1802 hotkey_source_mask_show, hotkey_source_mask_store);
1803
1804/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1805static ssize_t hotkey_poll_freq_show(struct device *dev,
1806 struct device_attribute *attr,
1807 char *buf)
1808{
1809 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1810}
1811
1812static ssize_t hotkey_poll_freq_store(struct device *dev,
1813 struct device_attribute *attr,
1814 const char *buf, size_t count)
1815{
1816 unsigned long t;
1817
1818 if (parse_strtoul(buf, 25, &t))
1819 return -EINVAL;
1820
1821 if (mutex_lock_interruptible(&hotkey_mutex))
1822 return -ERESTARTSYS;
1823
1824 hotkey_poll_freq = t;
1825
1826 hotkey_poll_setup(1);
1827 mutex_unlock(&hotkey_mutex);
1828
1829 return count;
1830}
1831
1832static struct device_attribute dev_attr_hotkey_poll_freq =
1833 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1834 hotkey_poll_freq_show, hotkey_poll_freq_store);
1835
1836#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1837
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001838/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001839static ssize_t hotkey_radio_sw_show(struct device *dev,
1840 struct device_attribute *attr,
1841 char *buf)
1842{
1843 int res, s;
1844 res = hotkey_get_wlsw(&s);
1845 if (res < 0)
1846 return res;
1847
1848 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1849}
1850
1851static struct device_attribute dev_attr_hotkey_radio_sw =
1852 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1853
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001854static void hotkey_radio_sw_notify_change(void)
1855{
1856 if (tp_features.hotkey_wlsw)
1857 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1858 "hotkey_radio_sw");
1859}
1860
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001861/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1862static ssize_t hotkey_tablet_mode_show(struct device *dev,
1863 struct device_attribute *attr,
1864 char *buf)
1865{
1866 int res, s;
1867 res = hotkey_get_tablet_mode(&s);
1868 if (res < 0)
1869 return res;
1870
1871 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1872}
1873
1874static struct device_attribute dev_attr_hotkey_tablet_mode =
1875 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1876
1877static void hotkey_tablet_mode_notify_change(void)
1878{
1879 if (tp_features.hotkey_tablet)
1880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1881 "hotkey_tablet_mode");
1882}
1883
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001884/* sysfs hotkey report_mode -------------------------------------------- */
1885static ssize_t hotkey_report_mode_show(struct device *dev,
1886 struct device_attribute *attr,
1887 char *buf)
1888{
1889 return snprintf(buf, PAGE_SIZE, "%d\n",
1890 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1891}
1892
1893static struct device_attribute dev_attr_hotkey_report_mode =
1894 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1895
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001896/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001897static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1898 struct device_attribute *attr,
1899 char *buf)
1900{
1901 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1902}
1903
1904static struct device_attribute dev_attr_hotkey_wakeup_reason =
1905 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1906
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001907static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001908{
1909 if (tp_features.hotkey_mask)
1910 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1911 "wakeup_reason");
1912}
1913
1914/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001915static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1916 struct device_attribute *attr,
1917 char *buf)
1918{
1919 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1920}
1921
1922static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1923 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1924 hotkey_wakeup_hotunplug_complete_show, NULL);
1925
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001926static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001927{
1928 if (tp_features.hotkey_mask)
1929 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1930 "wakeup_hotunplug_complete");
1931}
1932
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001933/* --------------------------------------------------------------------- */
1934
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001935static struct attribute *hotkey_attributes[] __initdata = {
1936 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001937 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001938 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001939#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1940 &dev_attr_hotkey_mask.attr,
1941 &dev_attr_hotkey_all_mask.attr,
1942 &dev_attr_hotkey_recommended_mask.attr,
1943 &dev_attr_hotkey_source_mask.attr,
1944 &dev_attr_hotkey_poll_freq.attr,
1945#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001946};
1947
1948static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001949 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001950#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1951 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001952 &dev_attr_hotkey_all_mask.attr,
1953 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001954#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001955 &dev_attr_hotkey_wakeup_reason.attr,
1956 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001957};
1958
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001959static void bluetooth_update_rfk(void);
1960static void wan_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03001961static void tpacpi_send_radiosw_update(void)
1962{
1963 int wlsw;
1964
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001965 /* Sync these BEFORE sending any rfkill events */
1966 if (tp_features.bluetooth)
1967 bluetooth_update_rfk();
1968 if (tp_features.wan)
1969 wan_update_rfk();
1970
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03001971 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1972 mutex_lock(&tpacpi_inputdev_send_mutex);
1973
1974 input_report_switch(tpacpi_inputdev,
1975 SW_RFKILL_ALL, !!wlsw);
1976 input_sync(tpacpi_inputdev);
1977
1978 mutex_unlock(&tpacpi_inputdev_send_mutex);
1979 }
1980 hotkey_radio_sw_notify_change();
1981}
1982
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03001983static void hotkey_exit(void)
1984{
1985#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1986 hotkey_poll_stop_sync();
1987#endif
1988
1989 if (hotkey_dev_attributes)
1990 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1991
1992 kfree(hotkey_keycode_map);
1993
1994 if (tp_features.hotkey) {
1995 dbg_printk(TPACPI_DBG_EXIT,
1996 "restoring original hot key mask\n");
1997 /* no short-circuit boolean operator below! */
1998 if ((hotkey_mask_set(hotkey_orig_mask) |
1999 hotkey_status_set(hotkey_orig_status)) != 0)
2000 printk(TPACPI_ERR
2001 "failed to restore hot key mask "
2002 "to BIOS defaults\n");
2003 }
2004}
2005
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002006static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002007{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002008 /* Requirements for changing the default keymaps:
2009 *
2010 * 1. Many of the keys are mapped to KEY_RESERVED for very
2011 * good reasons. Do not change them unless you have deep
2012 * knowledge on the IBM and Lenovo ThinkPad firmware for
2013 * the various ThinkPad models. The driver behaves
2014 * differently for KEY_RESERVED: such keys have their
2015 * hot key mask *unset* in mask_recommended, and also
2016 * in the initial hot key mask programmed into the
2017 * firmware at driver load time, which means the firm-
2018 * ware may react very differently if you change them to
2019 * something else;
2020 *
2021 * 2. You must be subscribed to the linux-thinkpad and
2022 * ibm-acpi-devel mailing lists, and you should read the
2023 * list archives since 2007 if you want to change the
2024 * keymaps. This requirement exists so that you will
2025 * know the past history of problems with the thinkpad-
2026 * acpi driver keymaps, and also that you will be
2027 * listening to any bug reports;
2028 *
2029 * 3. Do not send thinkpad-acpi specific patches directly to
2030 * for merging, *ever*. Send them to the linux-acpi
2031 * mailinglist for comments. Merging is to be done only
2032 * through acpi-test and the ACPI maintainer.
2033 *
2034 * If the above is too much to ask, don't change the keymap.
2035 * Ask the thinkpad-acpi maintainer to do it, instead.
2036 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002037 static u16 ibm_keycode_map[] __initdata = {
2038 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2039 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2040 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2041 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002042
2043 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002044 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2045 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2046 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002047
2048 /* brightness: firmware always reacts to them, unless
2049 * X.org did some tricks in the radeon BIOS scratch
2050 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002051 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002052 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002053
2054 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002055 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002056
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002057 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2058 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002059
2060 /* Volume: firmware always react to it and reprograms
2061 * the built-in *extra* mixer. Never map it to control
2062 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002063 KEY_RESERVED, /* 0x14: VOLUME UP */
2064 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2065 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002066
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002067 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002068
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002069 /* (assignments unknown, please report if found) */
2070 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2071 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2072 };
2073 static u16 lenovo_keycode_map[] __initdata = {
2074 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2075 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2076 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2077 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002078
2079 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002080 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2081 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2082 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002083
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002084 /* These either have to go through ACPI video, or
2085 * act like in the IBM ThinkPads, so don't ever
2086 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002087 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002088 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002089
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002090 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002091
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002092 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2093 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002094
2095 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2096 * react to it and reprograms the built-in *extra* mixer.
2097 * Never map it to control another mixer by default.
2098 *
2099 * T60?, T61, R60?, R61: firmware and EC tries to send
2100 * these over the regular keyboard, so these are no-ops,
2101 * but there are still weird bugs re. MUTE, so do not
2102 * change unless you get test reports from all Lenovo
2103 * models. May cause the BIOS to interfere with the
2104 * HDA mixer.
2105 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002106 KEY_RESERVED, /* 0x14: VOLUME UP */
2107 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2108 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002109
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002110 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002111
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002112 /* (assignments unknown, please report if found) */
2113 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2114 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2115 };
2116
2117#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2118#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2119#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2120
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002121 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002122 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002123 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002124
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002125 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2126
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002127 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002128 BUG_ON(tpacpi_inputdev->open != NULL ||
2129 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002130
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002131 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002132 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002133
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002134#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2135 mutex_init(&hotkey_thread_mutex);
2136 mutex_init(&hotkey_thread_data_mutex);
2137#endif
2138
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002139 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002140 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002141
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002142 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002143 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002144
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002145 if (!tp_features.hotkey)
2146 return 1;
2147
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002148 tpacpi_disable_brightness_delay();
2149
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002150 hotkey_dev_attributes = create_attr_set(13, NULL);
2151 if (!hotkey_dev_attributes)
2152 return -ENOMEM;
2153 res = add_many_to_attr_set(hotkey_dev_attributes,
2154 hotkey_attributes,
2155 ARRAY_SIZE(hotkey_attributes));
2156 if (res)
2157 goto err_exit;
2158
2159 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2160 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2161 for HKEY interface version 0x100 */
2162 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2163 if ((hkeyv >> 8) != 1) {
2164 printk(TPACPI_ERR "unknown version of the "
2165 "HKEY interface: 0x%x\n", hkeyv);
2166 printk(TPACPI_ERR "please report this to %s\n",
2167 TPACPI_MAIL);
2168 } else {
2169 /*
2170 * MHKV 0x100 in A31, R40, R40e,
2171 * T4x, X31, and later
2172 */
2173 tp_features.hotkey_mask = 1;
2174 }
2175 }
2176
2177 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2178 str_supported(tp_features.hotkey_mask));
2179
2180 if (tp_features.hotkey_mask) {
2181 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2182 "MHKA", "qd")) {
2183 printk(TPACPI_ERR
2184 "missing MHKA handler, "
2185 "please report this to %s\n",
2186 TPACPI_MAIL);
2187 /* FN+F12, FN+F4, FN+F3 */
2188 hotkey_all_mask = 0x080cU;
2189 }
2190 }
2191
2192 /* hotkey_source_mask *must* be zero for
2193 * the first hotkey_mask_get */
2194 res = hotkey_status_get(&hotkey_orig_status);
2195 if (res)
2196 goto err_exit;
2197
2198 if (tp_features.hotkey_mask) {
2199 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002200 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002201 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002202
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002203 hotkey_orig_mask = hotkey_mask;
2204 res = add_many_to_attr_set(
2205 hotkey_dev_attributes,
2206 hotkey_mask_attributes,
2207 ARRAY_SIZE(hotkey_mask_attributes));
2208 if (res)
2209 goto err_exit;
2210 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002211
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002212#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002213 if (tp_features.hotkey_mask) {
2214 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2215 & ~hotkey_all_mask;
2216 } else {
2217 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2218 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002219
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002220 vdbg_printk(TPACPI_DBG_INIT,
2221 "hotkey source mask 0x%08x, polling freq %d\n",
2222 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002223#endif
2224
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002225 /* Not all thinkpads have a hardware radio switch */
2226 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2227 tp_features.hotkey_wlsw = 1;
2228 printk(TPACPI_INFO
2229 "radio switch found; radios are %s\n",
2230 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002231 }
2232 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002233 res = add_to_attr_set(hotkey_dev_attributes,
2234 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002235
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002236 /* For X41t, X60t, X61t Tablets... */
2237 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2238 tp_features.hotkey_tablet = 1;
2239 printk(TPACPI_INFO
2240 "possible tablet mode switch found; "
2241 "ThinkPad in %s mode\n",
2242 (status & TP_HOTKEY_TABLET_MASK)?
2243 "tablet" : "laptop");
2244 res = add_to_attr_set(hotkey_dev_attributes,
2245 &dev_attr_hotkey_tablet_mode.attr);
2246 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002247
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002248 if (!res)
2249 res = register_attr_set_with_sysfs(
2250 hotkey_dev_attributes,
2251 &tpacpi_pdev->dev.kobj);
2252 if (res)
2253 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002254
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002255 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002256
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002257 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2258 GFP_KERNEL);
2259 if (!hotkey_keycode_map) {
2260 printk(TPACPI_ERR
2261 "failed to allocate memory for key map\n");
2262 res = -ENOMEM;
2263 goto err_exit;
2264 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002265
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002266 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2267 dbg_printk(TPACPI_DBG_INIT,
2268 "using Lenovo default hot key map\n");
2269 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2270 TPACPI_HOTKEY_MAP_SIZE);
2271 } else {
2272 dbg_printk(TPACPI_DBG_INIT,
2273 "using IBM default hot key map\n");
2274 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2275 TPACPI_HOTKEY_MAP_SIZE);
2276 }
2277
2278 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2279 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2280 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2281 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2282 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2283 tpacpi_inputdev->keycode = hotkey_keycode_map;
2284 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2285 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2286 set_bit(hotkey_keycode_map[i],
2287 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002288 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002289 if (i < sizeof(hotkey_reserved_mask)*8)
2290 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002291 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002292 }
2293
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002294 if (tp_features.hotkey_wlsw) {
2295 set_bit(EV_SW, tpacpi_inputdev->evbit);
2296 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2297 }
2298 if (tp_features.hotkey_tablet) {
2299 set_bit(EV_SW, tpacpi_inputdev->evbit);
2300 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002301 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002302
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002303 /* Do not issue duplicate brightness change events to
2304 * userspace */
2305 if (!tp_features.bright_acpimode)
2306 /* update bright_acpimode... */
2307 tpacpi_check_std_acpi_brightness_support();
2308
2309 if (tp_features.bright_acpimode) {
2310 printk(TPACPI_INFO
2311 "This ThinkPad has standard ACPI backlight "
2312 "brightness control, supported by the ACPI "
2313 "video driver\n");
2314 printk(TPACPI_NOTICE
2315 "Disabling thinkpad-acpi brightness events "
2316 "by default...\n");
2317
2318 /* The hotkey_reserved_mask change below is not
2319 * necessary while the keys are at KEY_RESERVED in the
2320 * default map, but better safe than sorry, leave it
2321 * here as a marker of what we have to do, especially
2322 * when we finally become able to set this at runtime
2323 * on response to X.org requests */
2324 hotkey_reserved_mask |=
2325 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2326 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002327 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002328
2329 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2330 res = hotkey_status_set(1);
2331 if (res) {
2332 hotkey_exit();
2333 return res;
2334 }
2335 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2336 & ~hotkey_reserved_mask)
2337 | hotkey_orig_mask);
2338 if (res < 0 && res != -ENXIO) {
2339 hotkey_exit();
2340 return res;
2341 }
2342
2343 dbg_printk(TPACPI_DBG_INIT,
2344 "legacy hot key reporting over procfs %s\n",
2345 (hotkey_report_mode < 2) ?
2346 "enabled" : "disabled");
2347
2348 tpacpi_inputdev->open = &hotkey_inputdev_open;
2349 tpacpi_inputdev->close = &hotkey_inputdev_close;
2350
2351 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002352 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002353 tpacpi_input_send_tabletsw();
2354
2355 return 0;
2356
2357err_exit:
2358 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2359 hotkey_dev_attributes = NULL;
2360
2361 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002362}
2363
2364static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2365{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002366 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002367 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002368 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002369 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002370 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002371
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002372 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002373 printk(TPACPI_ERR
2374 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002375 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002376 acpi_bus_generate_netlink_event(
2377 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002378 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002379 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002380 return;
2381 }
2382
2383 while (1) {
2384 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002385 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002386 return;
2387 }
2388
2389 if (hkey == 0) {
2390 /* queue empty */
2391 return;
2392 }
2393
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002394 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002395 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002396 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002397
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002398 switch (hkey >> 12) {
2399 case 1:
2400 /* 0x1000-0x1FFF: key presses */
2401 scancode = hkey & 0xfff;
2402 if (scancode > 0 && scancode < 0x21) {
2403 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002404 if (!(hotkey_source_mask & (1 << scancode))) {
2405 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002406 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002407 } else {
2408 ignore_acpi_ev = 1;
2409 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002410 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002411 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002412 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002413 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002414 case 2:
2415 /* Wakeup reason */
2416 switch (hkey) {
2417 case 0x2304: /* suspend, undock */
2418 case 0x2404: /* hibernation, undock */
2419 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2420 ignore_acpi_ev = 1;
2421 break;
2422 case 0x2305: /* suspend, bay eject */
2423 case 0x2405: /* hibernation, bay eject */
2424 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2425 ignore_acpi_ev = 1;
2426 break;
2427 default:
2428 unk_ev = 1;
2429 }
2430 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2431 printk(TPACPI_INFO
2432 "woke up due to a hot-unplug "
2433 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002434 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002435 }
2436 break;
2437 case 3:
2438 /* bay-related wakeups */
2439 if (hkey == 0x3003) {
2440 hotkey_autosleep_ack = 1;
2441 printk(TPACPI_INFO
2442 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002443 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002444 } else {
2445 unk_ev = 1;
2446 }
2447 break;
2448 case 4:
2449 /* dock-related wakeups */
2450 if (hkey == 0x4003) {
2451 hotkey_autosleep_ack = 1;
2452 printk(TPACPI_INFO
2453 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002454 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002455 } else {
2456 unk_ev = 1;
2457 }
2458 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002459 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002460 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002461 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002462 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002463 case 0x500b: /* X61t: tablet pen inserted into bay */
2464 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002465 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002466 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2467 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2468 tpacpi_input_send_tabletsw();
2469 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002470 send_acpi_ev = 0;
2471 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002472 case 0x5001:
2473 case 0x5002:
2474 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002475 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002476 break;
2477 default:
2478 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002479 }
2480 break;
2481 case 7:
2482 /* 0x7000-0x7FFF: misc */
2483 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002484 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002485 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002486 break;
2487 }
2488 /* fallthrough to default */
2489 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002490 unk_ev = 1;
2491 }
2492 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002493 printk(TPACPI_NOTICE
2494 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002495 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002496
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002497 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002498 if (!ignore_acpi_ev &&
2499 (send_acpi_ev || hotkey_report_mode < 2)) {
2500 acpi_bus_generate_proc_event(ibm->acpi->device,
2501 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002502 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002503
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002504 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002505 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002506 acpi_bus_generate_netlink_event(
2507 ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02002508 dev_name(&ibm->acpi->device->dev),
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002509 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002510 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002511 }
2512}
2513
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002514static void hotkey_suspend(pm_message_t state)
2515{
2516 /* Do these on suspend, we get the events on early resume! */
2517 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2518 hotkey_autosleep_ack = 0;
2519}
2520
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002521static void hotkey_resume(void)
2522{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002523 tpacpi_disable_brightness_delay();
2524
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002525 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002526 printk(TPACPI_ERR
2527 "error while trying to read hot key mask "
2528 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002529 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002530 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002531 hotkey_wakeup_reason_notify_change();
2532 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002533 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002534}
2535
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002536/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002537static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002539 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 int len = 0;
2541
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002542 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002543 len += sprintf(p + len, "status:\t\tnot supported\n");
2544 return len;
2545 }
2546
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002547 if (mutex_lock_interruptible(&hotkey_mutex))
2548 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002549 res = hotkey_status_get(&status);
2550 if (!res)
2551 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002552 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002553 if (res)
2554 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002557 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002558 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 len += sprintf(p + len,
2560 "commands:\tenable, disable, reset, <mask>\n");
2561 } else {
2562 len += sprintf(p + len, "mask:\t\tnot supported\n");
2563 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2564 }
2565
2566 return len;
2567}
2568
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002569static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002571 int res, status;
2572 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002575 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return -ENODEV;
2577
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002578 if (mutex_lock_interruptible(&hotkey_mutex))
2579 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002580
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002581 status = -1;
2582 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002583
2584 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 while ((cmd = next_cmd(&buf))) {
2586 if (strlencmp(cmd, "enable") == 0) {
2587 status = 1;
2588 } else if (strlencmp(cmd, "disable") == 0) {
2589 status = 0;
2590 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002591 status = hotkey_orig_status;
2592 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2594 /* mask set */
2595 } else if (sscanf(cmd, "%x", &mask) == 1) {
2596 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002597 } else {
2598 res = -EINVAL;
2599 goto errexit;
2600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002602 if (status != -1)
2603 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002605 if (!res && mask != hotkey_mask)
2606 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002608errexit:
2609 mutex_unlock(&hotkey_mutex);
2610 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002611}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002613static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002614 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002615 {"", 0},
2616};
2617
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002618static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002619 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002620 .notify = hotkey_notify,
2621 .handle = &hkey_handle,
2622 .type = ACPI_DEVICE_NOTIFY,
2623};
2624
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002625static struct ibm_struct hotkey_driver_data = {
2626 .name = "hotkey",
2627 .read = hotkey_read,
2628 .write = hotkey_write,
2629 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002630 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002631 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002632 .acpi = &ibm_hotkey_acpidriver,
2633};
2634
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002635/*************************************************************************
2636 * Bluetooth subdriver
2637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002639enum {
2640 /* ACPI GBDC/SBDC bits */
2641 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2642 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2643 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2644};
2645
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002646static struct rfkill *tpacpi_bluetooth_rfkill;
2647
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002648static int bluetooth_get_radiosw(void)
2649{
2650 int status;
2651
2652 if (!tp_features.bluetooth)
2653 return -ENODEV;
2654
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002655 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2656 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002657 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002658
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002659 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2660 return -EIO;
2661
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002662 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2663 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002664}
2665
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002666static void bluetooth_update_rfk(void)
2667{
2668 int status;
2669
2670 if (!tpacpi_bluetooth_rfkill)
2671 return;
2672
2673 status = bluetooth_get_radiosw();
2674 if (status < 0)
2675 return;
2676 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2677}
2678
2679static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002680{
2681 int status;
2682
2683 if (!tp_features.bluetooth)
2684 return -ENODEV;
2685
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002686 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2687 * reason to risk weird behaviour. */
2688 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2689 && radio_on)
2690 return -EPERM;
2691
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002692 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2693 return -EIO;
2694 if (radio_on)
2695 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2696 else
2697 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2698 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2699 return -EIO;
2700
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002701 if (update_rfk)
2702 bluetooth_update_rfk();
2703
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002704 return 0;
2705}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002706
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002707/* sysfs bluetooth enable ---------------------------------------------- */
2708static ssize_t bluetooth_enable_show(struct device *dev,
2709 struct device_attribute *attr,
2710 char *buf)
2711{
2712 int status;
2713
2714 status = bluetooth_get_radiosw();
2715 if (status < 0)
2716 return status;
2717
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002718 return snprintf(buf, PAGE_SIZE, "%d\n",
2719 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002720}
2721
2722static ssize_t bluetooth_enable_store(struct device *dev,
2723 struct device_attribute *attr,
2724 const char *buf, size_t count)
2725{
2726 unsigned long t;
2727 int res;
2728
2729 if (parse_strtoul(buf, 1, &t))
2730 return -EINVAL;
2731
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002732 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002733
2734 return (res) ? res : count;
2735}
2736
2737static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002738 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002739 bluetooth_enable_show, bluetooth_enable_store);
2740
2741/* --------------------------------------------------------------------- */
2742
2743static struct attribute *bluetooth_attributes[] = {
2744 &dev_attr_bluetooth_enable.attr,
2745 NULL
2746};
2747
2748static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002749 .attrs = bluetooth_attributes,
2750};
2751
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002752static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2753{
2754 int bts = bluetooth_get_radiosw();
2755
2756 if (bts < 0)
2757 return bts;
2758
2759 *state = bts;
2760 return 0;
2761}
2762
2763static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2764{
2765 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2766}
2767
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002768static void bluetooth_exit(void)
2769{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002770 if (tpacpi_bluetooth_rfkill)
2771 rfkill_unregister(tpacpi_bluetooth_rfkill);
2772
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002773 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2774 &bluetooth_attr_group);
2775}
2776
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002777static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002779 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002780 int status = 0;
2781
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002782 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2783
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002784 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002785
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002786 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2787 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002788 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002789 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002791 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2792 str_supported(tp_features.bluetooth),
2793 status);
2794
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002795 if (tp_features.bluetooth &&
2796 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2797 /* no bluetooth hardware present in system */
2798 tp_features.bluetooth = 0;
2799 dbg_printk(TPACPI_DBG_INIT,
2800 "bluetooth hardware not installed\n");
2801 }
2802
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002803 if (!tp_features.bluetooth)
2804 return 1;
2805
2806 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002807 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002808 if (res)
2809 return res;
2810
2811 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2812 &tpacpi_bluetooth_rfkill,
2813 RFKILL_TYPE_BLUETOOTH,
2814 "tpacpi_bluetooth_sw",
2815 tpacpi_bluetooth_rfk_set,
2816 tpacpi_bluetooth_rfk_get);
2817 if (res) {
2818 bluetooth_exit();
2819 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002820 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002821
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
2824
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002825/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002826static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
2828 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002829 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002831 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002834 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002835 (status == RFKILL_STATE_UNBLOCKED) ?
2836 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 len += sprintf(p + len, "commands:\tenable, disable\n");
2838 }
2839
2840 return len;
2841}
2842
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002843static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002847 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002848 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 while ((cmd = next_cmd(&buf))) {
2851 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002852 bluetooth_set_radiosw(1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002854 bluetooth_set_radiosw(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 } else
2856 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return 0;
2860}
2861
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002862static struct ibm_struct bluetooth_driver_data = {
2863 .name = "bluetooth",
2864 .read = bluetooth_read,
2865 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002866 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002867};
2868
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002869/*************************************************************************
2870 * Wan subdriver
2871 */
2872
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002873enum {
2874 /* ACPI GWAN/SWAN bits */
2875 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2876 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2877 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2878};
2879
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002880static struct rfkill *tpacpi_wan_rfkill;
2881
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002882static int wan_get_radiosw(void)
2883{
2884 int status;
2885
2886 if (!tp_features.wan)
2887 return -ENODEV;
2888
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002889 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2890 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002891 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002892
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002893 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2894 return -EIO;
2895
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002896 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
2897 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002898}
2899
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002900static void wan_update_rfk(void)
2901{
2902 int status;
2903
2904 if (!tpacpi_wan_rfkill)
2905 return;
2906
2907 status = wan_get_radiosw();
2908 if (status < 0)
2909 return;
2910 rfkill_force_state(tpacpi_wan_rfkill, status);
2911}
2912
2913static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002914{
2915 int status;
2916
2917 if (!tp_features.wan)
2918 return -ENODEV;
2919
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002920 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2921 * reason to risk weird behaviour. */
2922 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2923 && radio_on)
2924 return -EPERM;
2925
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002926 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2927 return -EIO;
2928 if (radio_on)
2929 status |= TP_ACPI_WANCARD_RADIOSSW;
2930 else
2931 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2932 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2933 return -EIO;
2934
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002935 if (update_rfk)
2936 wan_update_rfk();
2937
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002938 return 0;
2939}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002940
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002941/* sysfs wan enable ---------------------------------------------------- */
2942static ssize_t wan_enable_show(struct device *dev,
2943 struct device_attribute *attr,
2944 char *buf)
2945{
2946 int status;
2947
2948 status = wan_get_radiosw();
2949 if (status < 0)
2950 return status;
2951
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002952 return snprintf(buf, PAGE_SIZE, "%d\n",
2953 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002954}
2955
2956static ssize_t wan_enable_store(struct device *dev,
2957 struct device_attribute *attr,
2958 const char *buf, size_t count)
2959{
2960 unsigned long t;
2961 int res;
2962
2963 if (parse_strtoul(buf, 1, &t))
2964 return -EINVAL;
2965
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002966 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002967
2968 return (res) ? res : count;
2969}
2970
2971static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002972 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002973 wan_enable_show, wan_enable_store);
2974
2975/* --------------------------------------------------------------------- */
2976
2977static struct attribute *wan_attributes[] = {
2978 &dev_attr_wan_enable.attr,
2979 NULL
2980};
2981
2982static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002983 .attrs = wan_attributes,
2984};
2985
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002986static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2987{
2988 int wans = wan_get_radiosw();
2989
2990 if (wans < 0)
2991 return wans;
2992
2993 *state = wans;
2994 return 0;
2995}
2996
2997static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
2998{
2999 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3000}
3001
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003002static void wan_exit(void)
3003{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003004 if (tpacpi_wan_rfkill)
3005 rfkill_unregister(tpacpi_wan_rfkill);
3006
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003007 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3008 &wan_attr_group);
3009}
3010
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003011static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003012{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003013 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003014 int status = 0;
3015
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003016 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3017
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003018 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003019
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003020 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003021 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003022
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003023 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3024 str_supported(tp_features.wan),
3025 status);
3026
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003027 if (tp_features.wan &&
3028 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3029 /* no wan hardware present in system */
3030 tp_features.wan = 0;
3031 dbg_printk(TPACPI_DBG_INIT,
3032 "wan hardware not installed\n");
3033 }
3034
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003035 if (!tp_features.wan)
3036 return 1;
3037
3038 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003039 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003040 if (res)
3041 return res;
3042
3043 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3044 &tpacpi_wan_rfkill,
3045 RFKILL_TYPE_WWAN,
3046 "tpacpi_wwan_sw",
3047 tpacpi_wan_rfk_set,
3048 tpacpi_wan_rfk_get);
3049 if (res) {
3050 wan_exit();
3051 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003052 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003053
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003054 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003055}
3056
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003057/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003058static int wan_read(char *p)
3059{
3060 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003061 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003062
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003063 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003064 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003065 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003066 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003067 (status == RFKILL_STATE_UNBLOCKED) ?
3068 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003069 len += sprintf(p + len, "commands:\tenable, disable\n");
3070 }
3071
3072 return len;
3073}
3074
3075static int wan_write(char *buf)
3076{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003077 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003078
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003079 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003080 return -ENODEV;
3081
3082 while ((cmd = next_cmd(&buf))) {
3083 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003084 wan_set_radiosw(1, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003085 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003086 wan_set_radiosw(0, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003087 } else
3088 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003089 }
3090
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003091 return 0;
3092}
3093
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003094static struct ibm_struct wan_driver_data = {
3095 .name = "wan",
3096 .read = wan_read,
3097 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003098 .exit = wan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003099};
3100
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003101/*************************************************************************
3102 * Video subdriver
3103 */
3104
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003105#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3106
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003107enum video_access_mode {
3108 TPACPI_VIDEO_NONE = 0,
3109 TPACPI_VIDEO_570, /* 570 */
3110 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3111 TPACPI_VIDEO_NEW, /* all others */
3112};
3113
3114enum { /* video status flags, based on VIDEO_570 */
3115 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3116 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3117 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3118};
3119
3120enum { /* TPACPI_VIDEO_570 constants */
3121 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3122 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3123 * video_status_flags */
3124 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3125 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3126};
3127
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003128static enum video_access_mode video_supported;
3129static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003130
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003131static int video_autosw_get(void);
3132static int video_autosw_set(int enable);
3133
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003134TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003135
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003136static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003138 int ivga;
3139
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003140 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3141
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003142 TPACPI_ACPIHANDLE_INIT(vid);
3143 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003144
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003145 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3146 /* G41, assume IVGA doesn't change */
3147 vid_handle = vid2_handle;
3148
3149 if (!vid_handle)
3150 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003151 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003152 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3153 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003154 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003155 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3156 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003157 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003158 else
3159 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003160 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003162 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3163 str_supported(video_supported != TPACPI_VIDEO_NONE),
3164 video_supported);
3165
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003166 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003169static void video_exit(void)
3170{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003171 dbg_printk(TPACPI_DBG_EXIT,
3172 "restoring original video autoswitch mode\n");
3173 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003174 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003175 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003176}
3177
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003178static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
3180 int status = 0;
3181 int i;
3182
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003183 switch (video_supported) {
3184 case TPACPI_VIDEO_570:
3185 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3186 TP_ACPI_VIDEO_570_PHSCMD))
3187 return -EIO;
3188 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3189 break;
3190 case TPACPI_VIDEO_770:
3191 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3192 return -EIO;
3193 if (i)
3194 status |= TP_ACPI_VIDEO_S_LCD;
3195 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3196 return -EIO;
3197 if (i)
3198 status |= TP_ACPI_VIDEO_S_CRT;
3199 break;
3200 case TPACPI_VIDEO_NEW:
3201 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3202 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3203 return -EIO;
3204 if (i)
3205 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003207 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3208 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3209 return -EIO;
3210 if (i)
3211 status |= TP_ACPI_VIDEO_S_LCD;
3212 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3213 return -EIO;
3214 if (i)
3215 status |= TP_ACPI_VIDEO_S_DVI;
3216 break;
3217 default:
3218 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221 return status;
3222}
3223
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003224static int video_outputsw_set(int status)
3225{
3226 int autosw;
3227 int res = 0;
3228
3229 switch (video_supported) {
3230 case TPACPI_VIDEO_570:
3231 res = acpi_evalf(NULL, NULL,
3232 "\\_SB.PHS2", "vdd",
3233 TP_ACPI_VIDEO_570_PHS2CMD,
3234 status | TP_ACPI_VIDEO_570_PHS2SET);
3235 break;
3236 case TPACPI_VIDEO_770:
3237 autosw = video_autosw_get();
3238 if (autosw < 0)
3239 return autosw;
3240
3241 res = video_autosw_set(1);
3242 if (res)
3243 return res;
3244 res = acpi_evalf(vid_handle, NULL,
3245 "ASWT", "vdd", status * 0x100, 0);
3246 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003247 printk(TPACPI_ERR
3248 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003249 return -EIO;
3250 }
3251 break;
3252 case TPACPI_VIDEO_NEW:
3253 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003254 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003255 break;
3256 default:
3257 return -ENOSYS;
3258 }
3259
3260 return (res)? 0 : -EIO;
3261}
3262
3263static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003265 int autosw = 0;
3266
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003267 switch (video_supported) {
3268 case TPACPI_VIDEO_570:
3269 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3270 return -EIO;
3271 break;
3272 case TPACPI_VIDEO_770:
3273 case TPACPI_VIDEO_NEW:
3274 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3275 return -EIO;
3276 break;
3277 default:
3278 return -ENOSYS;
3279 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003280
3281 return autosw & 1;
3282}
3283
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003284static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003285{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003286 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003287 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003288 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003289}
3290
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003291static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003292{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003293 int autosw = video_autosw_get();
3294 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003295
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003296 if (autosw < 0)
3297 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003298
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003299 switch (video_supported) {
3300 case TPACPI_VIDEO_570:
3301 res = video_autosw_set(1);
3302 if (res)
3303 return res;
3304 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3305 break;
3306 case TPACPI_VIDEO_770:
3307 case TPACPI_VIDEO_NEW:
3308 res = video_autosw_set(1);
3309 if (res)
3310 return res;
3311 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3312 break;
3313 default:
3314 return -ENOSYS;
3315 }
3316 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003317 printk(TPACPI_ERR
3318 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003319 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003320 }
3321
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003322 return (res)? 0 : -EIO;
3323}
3324
3325static int video_expand_toggle(void)
3326{
3327 switch (video_supported) {
3328 case TPACPI_VIDEO_570:
3329 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3330 0 : -EIO;
3331 case TPACPI_VIDEO_770:
3332 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3333 0 : -EIO;
3334 case TPACPI_VIDEO_NEW:
3335 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3336 0 : -EIO;
3337 default:
3338 return -ENOSYS;
3339 }
3340 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003341}
3342
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003343static int video_read(char *p)
3344{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003345 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003346 int len = 0;
3347
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003348 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003349 len += sprintf(p + len, "status:\t\tnot supported\n");
3350 return len;
3351 }
3352
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003353 status = video_outputsw_get();
3354 if (status < 0)
3355 return status;
3356
3357 autosw = video_autosw_get();
3358 if (autosw < 0)
3359 return autosw;
3360
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003361 len += sprintf(p + len, "status:\t\tsupported\n");
3362 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3363 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003364 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003365 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3366 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3367 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3368 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003369 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003370 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3371 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3372 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3373
3374 return len;
3375}
3376
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003377static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
3379 char *cmd;
3380 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003381 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003383 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003384 return -ENODEV;
3385
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003386 enable = 0;
3387 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389 while ((cmd = next_cmd(&buf))) {
3390 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003391 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003393 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003395 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003397 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003398 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003399 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003400 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003401 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003402 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003403 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003405 res = video_autosw_set(1);
3406 if (res)
3407 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003409 res = video_autosw_set(0);
3410 if (res)
3411 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003413 res = video_outputsw_cycle();
3414 if (res)
3415 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003417 res = video_expand_toggle();
3418 if (res)
3419 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 } else
3421 return -EINVAL;
3422 }
3423
3424 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003425 status = video_outputsw_get();
3426 if (status < 0)
3427 return status;
3428 res = video_outputsw_set((status & ~disable) | enable);
3429 if (res)
3430 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
3432
3433 return 0;
3434}
3435
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003436static struct ibm_struct video_driver_data = {
3437 .name = "video",
3438 .read = video_read,
3439 .write = video_write,
3440 .exit = video_exit,
3441};
3442
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003443#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3444
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003445/*************************************************************************
3446 * Light (thinklight) subdriver
3447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003449TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3450TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003451
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003452static int light_get_status(void)
3453{
3454 int status = 0;
3455
3456 if (tp_features.light_status) {
3457 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3458 return -EIO;
3459 return (!!status);
3460 }
3461
3462 return -ENXIO;
3463}
3464
3465static int light_set_status(int status)
3466{
3467 int rc;
3468
3469 if (tp_features.light) {
3470 if (cmos_handle) {
3471 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3472 (status)?
3473 TP_CMOS_THINKLIGHT_ON :
3474 TP_CMOS_THINKLIGHT_OFF);
3475 } else {
3476 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3477 (status)? 1 : 0);
3478 }
3479 return (rc)? 0 : -EIO;
3480 }
3481
3482 return -ENXIO;
3483}
3484
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003485static void light_set_status_worker(struct work_struct *work)
3486{
3487 struct tpacpi_led_classdev *data =
3488 container_of(work, struct tpacpi_led_classdev, work);
3489
3490 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3491 light_set_status((data->new_brightness != LED_OFF));
3492}
3493
3494static void light_sysfs_set(struct led_classdev *led_cdev,
3495 enum led_brightness brightness)
3496{
3497 struct tpacpi_led_classdev *data =
3498 container_of(led_cdev,
3499 struct tpacpi_led_classdev,
3500 led_classdev);
3501 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003502 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003503}
3504
3505static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3506{
3507 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3508}
3509
3510static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3511 .led_classdev = {
3512 .name = "tpacpi::thinklight",
3513 .brightness_set = &light_sysfs_set,
3514 .brightness_get = &light_sysfs_get,
3515 }
3516};
3517
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003518static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003520 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003521
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003522 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3523
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003524 TPACPI_ACPIHANDLE_INIT(ledb);
3525 TPACPI_ACPIHANDLE_INIT(lght);
3526 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003527 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003528
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003529 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003530 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003531
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003532 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003533 /* light status not supported on
3534 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003535 tp_features.light_status =
3536 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003538 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3539 str_supported(tp_features.light),
3540 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003541
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003542 if (!tp_features.light)
3543 return 1;
3544
3545 rc = led_classdev_register(&tpacpi_pdev->dev,
3546 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003547
3548 if (rc < 0) {
3549 tp_features.light = 0;
3550 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003551 } else {
3552 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003553 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003554
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003555 return rc;
3556}
3557
3558static void light_exit(void)
3559{
3560 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3561 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003562 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003565static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
3567 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003568 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003570 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003571 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003572 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003573 len += sprintf(p + len, "status:\t\tunknown\n");
3574 len += sprintf(p + len, "commands:\ton, off\n");
3575 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003576 status = light_get_status();
3577 if (status < 0)
3578 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003580 len += sprintf(p + len, "commands:\ton, off\n");
3581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 return len;
3584}
3585
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003586static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003589 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003590
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003591 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003592 return -ENODEV;
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 while ((cmd = next_cmd(&buf))) {
3595 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003596 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003598 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 } else
3600 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 }
3602
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003603 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003606static struct ibm_struct light_driver_data = {
3607 .name = "light",
3608 .read = light_read,
3609 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003610 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003611};
3612
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003613/*************************************************************************
3614 * Dock subdriver
3615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003617#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003618
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003619static void dock_notify(struct ibm_struct *ibm, u32 event);
3620static int dock_read(char *p);
3621static int dock_write(char *buf);
3622
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003623TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003624 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3625 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3626 "\\_SB.PCI.ISA.SLCE", /* 570 */
3627 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3628
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003629/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003630TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003631
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003632static const struct acpi_device_id ibm_pci_device_ids[] = {
3633 {PCI_ROOT_HID_STRING, 0},
3634 {"", 0},
3635};
3636
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003637static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3638 {
3639 .notify = dock_notify,
3640 .handle = &dock_handle,
3641 .type = ACPI_SYSTEM_NOTIFY,
3642 },
3643 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003644 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3645 * We just use it to get notifications of dock hotplug
3646 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003647 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003648 .notify = dock_notify,
3649 .handle = &pci_handle,
3650 .type = ACPI_SYSTEM_NOTIFY,
3651 },
3652};
3653
3654static struct ibm_struct dock_driver_data[2] = {
3655 {
3656 .name = "dock",
3657 .read = dock_read,
3658 .write = dock_write,
3659 .acpi = &ibm_dock_acpidriver[0],
3660 },
3661 {
3662 .name = "dock",
3663 .acpi = &ibm_dock_acpidriver[1],
3664 },
3665};
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667#define dock_docked() (_sta(dock_handle) & 1)
3668
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003669static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003670{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003671 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3672
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003673 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003674
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003675 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3676 str_supported(dock_handle != NULL));
3677
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003678 return (dock_handle)? 0 : 1;
3679}
3680
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003681static int __init dock_init2(struct ibm_init_struct *iibm)
3682{
3683 int dock2_needed;
3684
3685 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3686
3687 if (dock_driver_data[0].flags.acpi_driver_registered &&
3688 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003689 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003690 dock2_needed = (pci_handle != NULL);
3691 vdbg_printk(TPACPI_DBG_INIT,
3692 "dock PCI handler for the TP 570 is %s\n",
3693 str_supported(dock2_needed));
3694 } else {
3695 vdbg_printk(TPACPI_DBG_INIT,
3696 "dock subdriver part 2 not required\n");
3697 dock2_needed = 0;
3698 }
3699
3700 return (dock2_needed)? 0 : 1;
3701}
3702
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003703static void dock_notify(struct ibm_struct *ibm, u32 event)
3704{
3705 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003706 int pci = ibm->acpi->hid && ibm->acpi->device &&
3707 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003708 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003709
3710 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003711 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003712 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003713 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003714 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003715 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003716 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003717 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003718 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003719 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003720 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003721 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003722 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003723 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003724 }
Len Brown14e04fb2007-08-23 15:20:26 -04003725 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003726 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003727 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08003728 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003729}
3730
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003731static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
3733 int len = 0;
3734 int docked = dock_docked();
3735
3736 if (!dock_handle)
3737 len += sprintf(p + len, "status:\t\tnot supported\n");
3738 else if (!docked)
3739 len += sprintf(p + len, "status:\t\tundocked\n");
3740 else {
3741 len += sprintf(p + len, "status:\t\tdocked\n");
3742 len += sprintf(p + len, "commands:\tdock, undock\n");
3743 }
3744
3745 return len;
3746}
3747
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003748static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
3750 char *cmd;
3751
3752 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003753 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
3755 while ((cmd = next_cmd(&buf))) {
3756 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003757 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3758 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 return -EIO;
3760 } else if (strlencmp(cmd, "dock") == 0) {
3761 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3762 return -EIO;
3763 } else
3764 return -EINVAL;
3765 }
3766
3767 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003770#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003772/*************************************************************************
3773 * Bay subdriver
3774 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003776#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003777
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003778TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003779 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3780 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3781 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3782 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003783TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003784 "_EJ0", /* all others */
3785 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003786TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003787 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3788 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003789TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003790 "_EJ0", /* 770x */
3791 ); /* all others */
3792
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003793static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003795 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3796
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003797 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003798 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003799 TPACPI_ACPIHANDLE_INIT(bay_ej);
3800 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003801 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003802 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003803
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003804 tp_features.bay_status = bay_handle &&
3805 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3806 tp_features.bay_status2 = bay2_handle &&
3807 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003808
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003809 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3810 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3811 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3812 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003814 vdbg_printk(TPACPI_DBG_INIT,
3815 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003816 str_supported(tp_features.bay_status),
3817 str_supported(tp_features.bay_eject),
3818 str_supported(tp_features.bay_status2),
3819 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003820
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003821 return (tp_features.bay_status || tp_features.bay_eject ||
3822 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003825static void bay_notify(struct ibm_struct *ibm, u32 event)
3826{
Len Brown14e04fb2007-08-23 15:20:26 -04003827 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003828 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
Kay Sieverse0b36fc2009-01-11 03:00:59 -02003829 dev_name(&ibm->acpi->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +08003830 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003831}
3832
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003833#define bay_occupied(b) (_sta(b##_handle) & 1)
3834
3835static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836{
3837 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003838 int occupied = bay_occupied(bay);
3839 int occupied2 = bay_occupied(bay2);
3840 int eject, eject2;
3841
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003842 len += sprintf(p + len, "status:\t\t%s\n",
3843 tp_features.bay_status ?
3844 (occupied ? "occupied" : "unoccupied") :
3845 "not supported");
3846 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003847 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3848 "occupied" : "unoccupied");
3849
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003850 eject = tp_features.bay_eject && occupied;
3851 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003852
3853 if (eject && eject2)
3854 len += sprintf(p + len, "commands:\teject, eject2\n");
3855 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003857 else if (eject2)
3858 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
3860 return len;
3861}
3862
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003863static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864{
3865 char *cmd;
3866
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003867 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003868 return -ENODEV;
3869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003871 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003872 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3873 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003874 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003875 strlencmp(cmd, "eject2") == 0) {
3876 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 return -EIO;
3878 } else
3879 return -EINVAL;
3880 }
3881
3882 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003883}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003884
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003885static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3886 .notify = bay_notify,
3887 .handle = &bay_handle,
3888 .type = ACPI_SYSTEM_NOTIFY,
3889};
3890
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003891static struct ibm_struct bay_driver_data = {
3892 .name = "bay",
3893 .read = bay_read,
3894 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003895 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003896};
3897
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003898#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003900/*************************************************************************
3901 * CMOS subdriver
3902 */
3903
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003904/* sysfs cmos_command -------------------------------------------------- */
3905static ssize_t cmos_command_store(struct device *dev,
3906 struct device_attribute *attr,
3907 const char *buf, size_t count)
3908{
3909 unsigned long cmos_cmd;
3910 int res;
3911
3912 if (parse_strtoul(buf, 21, &cmos_cmd))
3913 return -EINVAL;
3914
3915 res = issue_thinkpad_cmos_command(cmos_cmd);
3916 return (res)? res : count;
3917}
3918
3919static struct device_attribute dev_attr_cmos_command =
3920 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3921
3922/* --------------------------------------------------------------------- */
3923
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003924static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003925{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003926 int res;
3927
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003928 vdbg_printk(TPACPI_DBG_INIT,
3929 "initializing cmos commands subdriver\n");
3930
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003931 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003932
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003933 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3934 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003935
3936 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3937 if (res)
3938 return res;
3939
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003940 return (cmos_handle)? 0 : 1;
3941}
3942
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003943static void cmos_exit(void)
3944{
3945 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3946}
3947
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003948static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
3950 int len = 0;
3951
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003952 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3953 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 if (!cmos_handle)
3955 len += sprintf(p + len, "status:\t\tnot supported\n");
3956 else {
3957 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003958 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 }
3960
3961 return len;
3962}
3963
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003964static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
3966 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003967 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
3969 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003970 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3971 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 /* cmos_cmd set */
3973 } else
3974 return -EINVAL;
3975
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003976 res = issue_thinkpad_cmos_command(cmos_cmd);
3977 if (res)
3978 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
3980
3981 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003982}
3983
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003984static struct ibm_struct cmos_driver_data = {
3985 .name = "cmos",
3986 .read = cmos_read,
3987 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003988 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003989};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003990
3991/*************************************************************************
3992 * LED subdriver
3993 */
3994
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003995enum led_access_mode {
3996 TPACPI_LED_NONE = 0,
3997 TPACPI_LED_570, /* 570 */
3998 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3999 TPACPI_LED_NEW, /* all others */
4000};
4001
4002enum { /* For TPACPI_LED_OLD */
4003 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4004 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4005 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4006};
4007
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004008enum led_status_t {
4009 TPACPI_LED_OFF = 0,
4010 TPACPI_LED_ON,
4011 TPACPI_LED_BLINK,
4012};
4013
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004014static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004015
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004016TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004017 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4018 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004019 "LED", /* all others */
4020 ); /* R30, R31 */
4021
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004022#define TPACPI_LED_NUMLEDS 8
4023static struct tpacpi_led_classdev *tpacpi_leds;
4024static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004025static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004026 /* there's a limit of 19 chars + NULL before 2.6.26 */
4027 "tpacpi::power",
4028 "tpacpi:orange:batt",
4029 "tpacpi:green:batt",
4030 "tpacpi::dock_active",
4031 "tpacpi::bay_active",
4032 "tpacpi::dock_batt",
4033 "tpacpi::unknown_led",
4034 "tpacpi::standby",
4035};
4036
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004037static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004038{
4039 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004040 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004041
4042 switch (led_supported) {
4043 case TPACPI_LED_570:
4044 if (!acpi_evalf(ec_handle,
4045 &status, "GLED", "dd", 1 << led))
4046 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004047 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004048 TPACPI_LED_OFF :
4049 ((status == 1)?
4050 TPACPI_LED_ON :
4051 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004052 tpacpi_led_state_cache[led] = led_s;
4053 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004054 default:
4055 return -ENXIO;
4056 }
4057
4058 /* not reached */
4059}
4060
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004061static int led_set_status(const unsigned int led,
4062 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004063{
4064 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004065 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4066 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004067
4068 int rc = 0;
4069
4070 switch (led_supported) {
4071 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004072 /* 570 */
4073 if (led > 7)
4074 return -EINVAL;
4075 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4076 (1 << led), led_sled_arg1[ledstatus]))
4077 rc = -EIO;
4078 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004079 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004080 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4081 if (led > 7)
4082 return -EINVAL;
4083 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4084 if (rc >= 0)
4085 rc = ec_write(TPACPI_LED_EC_HLBL,
4086 (ledstatus == TPACPI_LED_BLINK) << led);
4087 if (rc >= 0)
4088 rc = ec_write(TPACPI_LED_EC_HLCL,
4089 (ledstatus != TPACPI_LED_OFF) << led);
4090 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004091 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004092 /* all others */
4093 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4094 led, led_led_arg1[ledstatus]))
4095 rc = -EIO;
4096 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004097 default:
4098 rc = -ENXIO;
4099 }
4100
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004101 if (!rc)
4102 tpacpi_led_state_cache[led] = ledstatus;
4103
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004104 return rc;
4105}
4106
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004107static void led_sysfs_set_status(unsigned int led,
4108 enum led_brightness brightness)
4109{
4110 led_set_status(led,
4111 (brightness == LED_OFF) ?
4112 TPACPI_LED_OFF :
4113 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4114 TPACPI_LED_BLINK : TPACPI_LED_ON);
4115}
4116
4117static void led_set_status_worker(struct work_struct *work)
4118{
4119 struct tpacpi_led_classdev *data =
4120 container_of(work, struct tpacpi_led_classdev, work);
4121
4122 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4123 led_sysfs_set_status(data->led, data->new_brightness);
4124}
4125
4126static void led_sysfs_set(struct led_classdev *led_cdev,
4127 enum led_brightness brightness)
4128{
4129 struct tpacpi_led_classdev *data = container_of(led_cdev,
4130 struct tpacpi_led_classdev, led_classdev);
4131
4132 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004133 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004134}
4135
4136static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4137 unsigned long *delay_on, unsigned long *delay_off)
4138{
4139 struct tpacpi_led_classdev *data = container_of(led_cdev,
4140 struct tpacpi_led_classdev, led_classdev);
4141
4142 /* Can we choose the flash rate? */
4143 if (*delay_on == 0 && *delay_off == 0) {
4144 /* yes. set them to the hardware blink rate (1 Hz) */
4145 *delay_on = 500; /* ms */
4146 *delay_off = 500; /* ms */
4147 } else if ((*delay_on != 500) || (*delay_off != 500))
4148 return -EINVAL;
4149
4150 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004151 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004152
4153 return 0;
4154}
4155
4156static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4157{
4158 int rc;
4159
4160 struct tpacpi_led_classdev *data = container_of(led_cdev,
4161 struct tpacpi_led_classdev, led_classdev);
4162
4163 rc = led_get_status(data->led);
4164
4165 if (rc == TPACPI_LED_OFF || rc < 0)
4166 rc = LED_OFF; /* no error handling in led class :( */
4167 else
4168 rc = LED_FULL;
4169
4170 return rc;
4171}
4172
4173static void led_exit(void)
4174{
4175 unsigned int i;
4176
4177 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4178 if (tpacpi_leds[i].led_classdev.name)
4179 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4180 }
4181
4182 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004183}
4184
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004185static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004186{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004187 unsigned int i;
4188 int rc;
4189
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004190 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4191
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004192 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004193
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004194 if (!led_handle)
4195 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004196 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004197 else if (strlencmp(led_path, "SLED") == 0)
4198 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004199 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004200 else if (strlencmp(led_path, "SYSL") == 0)
4201 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004202 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004203 else
4204 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004205 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004206
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004207 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4208 str_supported(led_supported), led_supported);
4209
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004210 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4211 GFP_KERNEL);
4212 if (!tpacpi_leds) {
4213 printk(TPACPI_ERR "Out of memory for LED data\n");
4214 return -ENOMEM;
4215 }
4216
4217 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4218 tpacpi_leds[i].led = i;
4219
4220 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4221 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4222 if (led_supported == TPACPI_LED_570)
4223 tpacpi_leds[i].led_classdev.brightness_get =
4224 &led_sysfs_get;
4225
4226 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4227
4228 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4229
4230 rc = led_classdev_register(&tpacpi_pdev->dev,
4231 &tpacpi_leds[i].led_classdev);
4232 if (rc < 0) {
4233 tpacpi_leds[i].led_classdev.name = NULL;
4234 led_exit();
4235 return rc;
4236 }
4237 }
4238
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004239 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004240}
4241
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004242#define str_led_status(s) \
4243 ((s) == TPACPI_LED_OFF ? "off" : \
4244 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004245
4246static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
4248 int len = 0;
4249
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004250 if (!led_supported) {
4251 len += sprintf(p + len, "status:\t\tnot supported\n");
4252 return len;
4253 }
4254 len += sprintf(p + len, "status:\t\tsupported\n");
4255
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004256 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004257 /* 570 */
4258 int i, status;
4259 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004260 status = led_get_status(i);
4261 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004262 return -EIO;
4263 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004264 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004265 }
4266 }
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004269 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
4271 return len;
4272}
4273
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004274static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
4276 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004277 int led, rc;
4278 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004279
4280 if (!led_supported)
4281 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
4283 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004284 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 return -EINVAL;
4286
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004287 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004288 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004290 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004291 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004292 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004293 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004294 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004295 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004296
4297 rc = led_set_status(led, s);
4298 if (rc < 0)
4299 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 }
4301
4302 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004303}
4304
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004305static struct ibm_struct led_driver_data = {
4306 .name = "led",
4307 .read = led_read,
4308 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004309 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004310};
4311
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004312/*************************************************************************
4313 * Beep subdriver
4314 */
4315
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004316TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004317
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004318static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004319{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004320 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4321
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004322 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004323
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004324 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4325 str_supported(beep_handle != NULL));
4326
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004327 return (beep_handle)? 0 : 1;
4328}
4329
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004330static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331{
4332 int len = 0;
4333
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004334 if (!beep_handle)
4335 len += sprintf(p + len, "status:\t\tnot supported\n");
4336 else {
4337 len += sprintf(p + len, "status:\t\tsupported\n");
4338 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
4341 return len;
4342}
4343
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004344static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
4346 char *cmd;
4347 int beep_cmd;
4348
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004349 if (!beep_handle)
4350 return -ENODEV;
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004353 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4354 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 /* beep_cmd set */
4356 } else
4357 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004358 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 return -EIO;
4360 }
4361
4362 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004363}
4364
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004365static struct ibm_struct beep_driver_data = {
4366 .name = "beep",
4367 .read = beep_read,
4368 .write = beep_write,
4369};
4370
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004371/*************************************************************************
4372 * Thermal subdriver
4373 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004374
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004375enum thermal_access_mode {
4376 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4377 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4378 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4379 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4380 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4381};
4382
4383enum { /* TPACPI_THERMAL_TPEC_* */
4384 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4385 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4386 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4387};
4388
4389#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4390struct ibm_thermal_sensors_struct {
4391 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4392};
4393
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004394static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004395
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004396/* idx is zero-based */
4397static int thermal_get_sensor(int idx, s32 *value)
4398{
4399 int t;
4400 s8 tmp;
4401 char tmpi[5];
4402
4403 t = TP_EC_THERMAL_TMP0;
4404
4405 switch (thermal_read_mode) {
4406#if TPACPI_MAX_THERMAL_SENSORS >= 16
4407 case TPACPI_THERMAL_TPEC_16:
4408 if (idx >= 8 && idx <= 15) {
4409 t = TP_EC_THERMAL_TMP8;
4410 idx -= 8;
4411 }
4412 /* fallthrough */
4413#endif
4414 case TPACPI_THERMAL_TPEC_8:
4415 if (idx <= 7) {
4416 if (!acpi_ec_read(t + idx, &tmp))
4417 return -EIO;
4418 *value = tmp * 1000;
4419 return 0;
4420 }
4421 break;
4422
4423 case TPACPI_THERMAL_ACPI_UPDT:
4424 if (idx <= 7) {
4425 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4426 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4427 return -EIO;
4428 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4429 return -EIO;
4430 *value = (t - 2732) * 100;
4431 return 0;
4432 }
4433 break;
4434
4435 case TPACPI_THERMAL_ACPI_TMP07:
4436 if (idx <= 7) {
4437 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4438 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4439 return -EIO;
4440 if (t > 127 || t < -127)
4441 t = TP_EC_THERMAL_TMP_NA;
4442 *value = t * 1000;
4443 return 0;
4444 }
4445 break;
4446
4447 case TPACPI_THERMAL_NONE:
4448 default:
4449 return -ENOSYS;
4450 }
4451
4452 return -EINVAL;
4453}
4454
4455static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4456{
4457 int res, i;
4458 int n;
4459
4460 n = 8;
4461 i = 0;
4462
4463 if (!s)
4464 return -EINVAL;
4465
4466 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4467 n = 16;
4468
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004469 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004470 res = thermal_get_sensor(i, &s->temp[i]);
4471 if (res)
4472 return res;
4473 }
4474
4475 return n;
4476}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004477
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004478/* sysfs temp##_input -------------------------------------------------- */
4479
4480static ssize_t thermal_temp_input_show(struct device *dev,
4481 struct device_attribute *attr,
4482 char *buf)
4483{
4484 struct sensor_device_attribute *sensor_attr =
4485 to_sensor_dev_attr(attr);
4486 int idx = sensor_attr->index;
4487 s32 value;
4488 int res;
4489
4490 res = thermal_get_sensor(idx, &value);
4491 if (res)
4492 return res;
4493 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4494 return -ENXIO;
4495
4496 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4497}
4498
4499#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004500 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4501 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004502
4503static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4504 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4505 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4506 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4507 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4508 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4509 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4510 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4511 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4512 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4513 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4514 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4515 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4516 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4517 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4518 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4519 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4520};
4521
4522#define THERMAL_ATTRS(X) \
4523 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4524
4525static struct attribute *thermal_temp_input_attr[] = {
4526 THERMAL_ATTRS(8),
4527 THERMAL_ATTRS(9),
4528 THERMAL_ATTRS(10),
4529 THERMAL_ATTRS(11),
4530 THERMAL_ATTRS(12),
4531 THERMAL_ATTRS(13),
4532 THERMAL_ATTRS(14),
4533 THERMAL_ATTRS(15),
4534 THERMAL_ATTRS(0),
4535 THERMAL_ATTRS(1),
4536 THERMAL_ATTRS(2),
4537 THERMAL_ATTRS(3),
4538 THERMAL_ATTRS(4),
4539 THERMAL_ATTRS(5),
4540 THERMAL_ATTRS(6),
4541 THERMAL_ATTRS(7),
4542 NULL
4543};
4544
4545static const struct attribute_group thermal_temp_input16_group = {
4546 .attrs = thermal_temp_input_attr
4547};
4548
4549static const struct attribute_group thermal_temp_input8_group = {
4550 .attrs = &thermal_temp_input_attr[8]
4551};
4552
4553#undef THERMAL_SENSOR_ATTR_TEMP
4554#undef THERMAL_ATTRS
4555
4556/* --------------------------------------------------------------------- */
4557
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004558static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004559{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004560 u8 t, ta1, ta2;
4561 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004562 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004563 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004564
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004565 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4566
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004567 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004568
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004569 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004570 /*
4571 * Direct EC access mode: sensors at registers
4572 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4573 * non-implemented, thermal sensors return 0x80 when
4574 * not available
4575 */
4576
4577 ta1 = ta2 = 0;
4578 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004579 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004580 ta1 |= t;
4581 } else {
4582 ta1 = 0;
4583 break;
4584 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004585 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004586 ta2 |= t;
4587 } else {
4588 ta1 = 0;
4589 break;
4590 }
4591 }
4592 if (ta1 == 0) {
4593 /* This is sheer paranoia, but we handle it anyway */
4594 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004595 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004596 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004597 "falling back to ACPI TMPx access "
4598 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004599 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004600 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004601 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004602 "ThinkPad ACPI EC access misbehaving, "
4603 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004604 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004605 }
4606 } else {
4607 thermal_read_mode =
4608 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004609 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004610 }
4611 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004612 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4613 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004614 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004615 } else {
4616 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004617 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004618 }
4619 } else {
4620 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004621 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004622 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004623
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004624 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4625 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4626 thermal_read_mode);
4627
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004628 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004629 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004630 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004631 &thermal_temp_input16_group);
4632 if (res)
4633 return res;
4634 break;
4635 case TPACPI_THERMAL_TPEC_8:
4636 case TPACPI_THERMAL_ACPI_TMP07:
4637 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004638 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004639 &thermal_temp_input8_group);
4640 if (res)
4641 return res;
4642 break;
4643 case TPACPI_THERMAL_NONE:
4644 default:
4645 return 1;
4646 }
4647
4648 return 0;
4649}
4650
4651static void thermal_exit(void)
4652{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004653 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004654 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004655 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004656 &thermal_temp_input16_group);
4657 break;
4658 case TPACPI_THERMAL_TPEC_8:
4659 case TPACPI_THERMAL_ACPI_TMP07:
4660 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004661 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004662 &thermal_temp_input16_group);
4663 break;
4664 case TPACPI_THERMAL_NONE:
4665 default:
4666 break;
4667 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004668}
4669
4670static int thermal_read(char *p)
4671{
4672 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004673 int n, i;
4674 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004675
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004676 n = thermal_get_sensors(&t);
4677 if (unlikely(n < 0))
4678 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004679
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004680 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004681
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004682 if (n > 0) {
4683 for (i = 0; i < (n - 1); i++)
4684 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4685 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4686 } else
4687 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004688
4689 return len;
4690}
4691
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004692static struct ibm_struct thermal_driver_data = {
4693 .name = "thermal",
4694 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004695 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004696};
4697
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004698/*************************************************************************
4699 * EC Dump subdriver
4700 */
4701
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004702static u8 ecdump_regs[256];
4703
4704static int ecdump_read(char *p)
4705{
4706 int len = 0;
4707 int i, j;
4708 u8 v;
4709
4710 len += sprintf(p + len, "EC "
4711 " +00 +01 +02 +03 +04 +05 +06 +07"
4712 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4713 for (i = 0; i < 256; i += 16) {
4714 len += sprintf(p + len, "EC 0x%02x:", i);
4715 for (j = 0; j < 16; j++) {
4716 if (!acpi_ec_read(i + j, &v))
4717 break;
4718 if (v != ecdump_regs[i + j])
4719 len += sprintf(p + len, " *%02x", v);
4720 else
4721 len += sprintf(p + len, " %02x", v);
4722 ecdump_regs[i + j] = v;
4723 }
4724 len += sprintf(p + len, "\n");
4725 if (j != 16)
4726 break;
4727 }
4728
4729 /* These are way too dangerous to advertise openly... */
4730#if 0
4731 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4732 " (<offset> is 00-ff, <value> is 00-ff)\n");
4733 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4734 " (<offset> is 00-ff, <value> is 0-255)\n");
4735#endif
4736 return len;
4737}
4738
4739static int ecdump_write(char *buf)
4740{
4741 char *cmd;
4742 int i, v;
4743
4744 while ((cmd = next_cmd(&buf))) {
4745 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4746 /* i and v set */
4747 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4748 /* i and v set */
4749 } else
4750 return -EINVAL;
4751 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4752 if (!acpi_ec_write(i, v))
4753 return -EIO;
4754 } else
4755 return -EINVAL;
4756 }
4757
4758 return 0;
4759}
4760
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004761static struct ibm_struct ecdump_driver_data = {
4762 .name = "ecdump",
4763 .read = ecdump_read,
4764 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004765 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004766};
4767
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004768/*************************************************************************
4769 * Backlight/brightness subdriver
4770 */
Holger Macht8acb0252006-10-20 14:30:28 -07004771
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004772#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4773
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004774enum {
4775 TP_EC_BACKLIGHT = 0x31,
4776
4777 /* TP_EC_BACKLIGHT bitmasks */
4778 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4779 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4780 TP_EC_BACKLIGHT_MAPSW = 0x20,
4781};
4782
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004783static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004784static int brightness_mode;
4785static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4786
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004787static struct mutex brightness_mutex;
4788
4789/*
4790 * ThinkPads can read brightness from two places: EC 0x31, or
4791 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004792 *
4793 * EC 0x31 has the following layout
4794 * Bit 7: unknown function
4795 * Bit 6: unknown function
4796 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4797 * Bit 4: must be set to zero to avoid problems
4798 * Bit 3-0: backlight brightness level
4799 *
4800 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004801 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004802static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004803{
4804 u8 lec = 0, lcmos = 0, level = 0;
4805
4806 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004807 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004808 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004809 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004810 };
4811 if (brightness_mode & 2) {
4812 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4813 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4814 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4815 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4816 level = lcmos;
4817 }
4818
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004819 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004820 *status = lec; /* Prefer EC, CMOS is just a backing store */
4821 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004822 if (lec == lcmos)
4823 tp_warned.bright_cmos_ec_unsync = 0;
4824 else {
4825 if (!tp_warned.bright_cmos_ec_unsync) {
4826 printk(TPACPI_ERR
4827 "CMOS NVRAM (%u) and EC (%u) do not "
4828 "agree on display brightness level\n",
4829 (unsigned int) lcmos,
4830 (unsigned int) lec);
4831 tp_warned.bright_cmos_ec_unsync = 1;
4832 }
4833 return -EIO;
4834 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004835 } else {
4836 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004837 }
4838
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004839 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004840}
4841
4842/* May return EINTR which can always be mapped to ERESTARTSYS */
4843static int brightness_set(int value)
4844{
4845 int cmos_cmd, inc, i, res;
4846 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004847 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004848
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004849 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4850 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004851 return -EINVAL;
4852
4853 res = mutex_lock_interruptible(&brightness_mutex);
4854 if (res < 0)
4855 return res;
4856
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004857 res = brightness_get_raw(&current_value);
4858 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004859 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004860
4861 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4862 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004863
4864 cmos_cmd = value > current_value ?
4865 TP_CMOS_BRIGHTNESS_UP :
4866 TP_CMOS_BRIGHTNESS_DOWN;
4867 inc = (value > current_value)? 1 : -1;
4868
4869 res = 0;
4870 for (i = current_value; i != value; i += inc) {
4871 if ((brightness_mode & 2) &&
4872 issue_thinkpad_cmos_command(cmos_cmd)) {
4873 res = -EIO;
4874 goto errout;
4875 }
4876 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004877 !acpi_ec_write(TP_EC_BACKLIGHT,
4878 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004879 res = -EIO;
4880 goto errout;;
4881 }
4882 }
4883
4884errout:
4885 mutex_unlock(&brightness_mutex);
4886 return res;
4887}
4888
4889/* sysfs backlight class ----------------------------------------------- */
4890
4891static int brightness_update_status(struct backlight_device *bd)
4892{
4893 /* it is the backlight class's job (caller) to handle
4894 * EINTR and other errors properly */
4895 return brightness_set(
4896 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4897 bd->props.power == FB_BLANK_UNBLANK) ?
4898 bd->props.brightness : 0);
4899}
Holger Macht8acb0252006-10-20 14:30:28 -07004900
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004901static int brightness_get(struct backlight_device *bd)
4902{
4903 int status, res;
4904
4905 res = brightness_get_raw(&status);
4906 if (res < 0)
4907 return 0; /* FIXME: teach backlight about error handling */
4908
4909 return status & TP_EC_BACKLIGHT_LVLMSK;
4910}
4911
Richard Purdie599a52d2007-02-10 23:07:48 +00004912static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004913 .get_brightness = brightness_get,
4914 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004915};
4916
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004917/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004918
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004919static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004920{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004921 int b;
4922
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004923 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4924
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004925 mutex_init(&brightness_mutex);
4926
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004927 /*
4928 * We always attempt to detect acpi support, so as to switch
4929 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4930 * going to publish a backlight interface
4931 */
4932 b = tpacpi_check_std_acpi_brightness_support();
4933 if (b > 0) {
Thomas Renninger2dba1b52008-08-01 17:38:03 +02004934
4935 if (acpi_video_backlight_support()) {
4936 if (brightness_enable > 1) {
4937 printk(TPACPI_NOTICE
4938 "Standard ACPI backlight interface "
4939 "available, not loading native one.\n");
4940 return 1;
4941 } else if (brightness_enable == 1) {
4942 printk(TPACPI_NOTICE
4943 "Backlight control force enabled, even if standard "
4944 "ACPI backlight interface is available\n");
4945 }
4946 } else {
4947 if (brightness_enable > 1) {
4948 printk(TPACPI_NOTICE
4949 "Standard ACPI backlight interface not "
4950 "available, thinkpad_acpi native "
4951 "brightness control enabled\n");
4952 }
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004953 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004954 }
4955
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004956 if (!brightness_enable) {
4957 dbg_printk(TPACPI_DBG_INIT,
4958 "brightness support disabled by "
4959 "module parameter\n");
4960 return 1;
4961 }
4962
4963 if (b > 16) {
4964 printk(TPACPI_ERR
4965 "Unsupported brightness interface, "
4966 "please contact %s\n", TPACPI_MAIL);
4967 return 1;
4968 }
4969 if (b == 16)
4970 tp_features.bright_16levels = 1;
4971
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004972 if (!brightness_mode) {
4973 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4974 brightness_mode = 2;
4975 else
4976 brightness_mode = 3;
4977
4978 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4979 brightness_mode);
4980 }
4981
4982 if (brightness_mode > 3)
4983 return -EINVAL;
4984
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004985 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004986 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004987
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004988 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004989 printk(TPACPI_INFO
4990 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004991
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004992 ibm_backlight_device = backlight_device_register(
4993 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4994 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004995 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004996 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004997 return PTR_ERR(ibm_backlight_device);
4998 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004999 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005000
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005001 ibm_backlight_device->props.max_brightness =
5002 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03005003 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02005004 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00005005
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005006 return 0;
5007}
5008
5009static void brightness_exit(void)
5010{
5011 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005012 vdbg_printk(TPACPI_DBG_EXIT,
5013 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005014 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005015 }
5016}
5017
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005018static int brightness_read(char *p)
5019{
5020 int len = 0;
5021 int level;
5022
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005023 level = brightness_get(NULL);
5024 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005025 len += sprintf(p + len, "level:\t\tunreadable\n");
5026 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005027 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005028 len += sprintf(p + len, "commands:\tup, down\n");
5029 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005030 " (<level> is 0-%d)\n",
5031 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005032 }
5033
5034 return len;
5035}
5036
5037static int brightness_write(char *buf)
5038{
5039 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005040 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005041 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005042 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005043
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005044 level = brightness_get(NULL);
5045 if (level < 0)
5046 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005047
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005048 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005049 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005050 if (level < max_level)
5051 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005052 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005053 if (level > 0)
5054 level--;
5055 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5056 level >= 0 && level <= max_level) {
5057 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005058 } else
5059 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005060 }
5061
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005062 /*
5063 * Now we know what the final level should be, so we try to set it.
5064 * Doing it this way makes the syscall restartable in case of EINTR
5065 */
5066 rc = brightness_set(level);
5067 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005068}
5069
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005070static struct ibm_struct brightness_driver_data = {
5071 .name = "brightness",
5072 .read = brightness_read,
5073 .write = brightness_write,
5074 .exit = brightness_exit,
5075};
5076
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005077/*************************************************************************
5078 * Volume subdriver
5079 */
5080
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005081static int volume_offset = 0x30;
5082
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005083static int volume_read(char *p)
5084{
5085 int len = 0;
5086 u8 level;
5087
5088 if (!acpi_ec_read(volume_offset, &level)) {
5089 len += sprintf(p + len, "level:\t\tunreadable\n");
5090 } else {
5091 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5092 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5093 len += sprintf(p + len, "commands:\tup, down, mute\n");
5094 len += sprintf(p + len, "commands:\tlevel <level>"
5095 " (<level> is 0-15)\n");
5096 }
5097
5098 return len;
5099}
5100
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005101static int volume_write(char *buf)
5102{
5103 int cmos_cmd, inc, i;
5104 u8 level, mute;
5105 int new_level, new_mute;
5106 char *cmd;
5107
5108 while ((cmd = next_cmd(&buf))) {
5109 if (!acpi_ec_read(volume_offset, &level))
5110 return -EIO;
5111 new_mute = mute = level & 0x40;
5112 new_level = level = level & 0xf;
5113
5114 if (strlencmp(cmd, "up") == 0) {
5115 if (mute)
5116 new_mute = 0;
5117 else
5118 new_level = level == 15 ? 15 : level + 1;
5119 } else if (strlencmp(cmd, "down") == 0) {
5120 if (mute)
5121 new_mute = 0;
5122 else
5123 new_level = level == 0 ? 0 : level - 1;
5124 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5125 new_level >= 0 && new_level <= 15) {
5126 /* new_level set */
5127 } else if (strlencmp(cmd, "mute") == 0) {
5128 new_mute = 0x40;
5129 } else
5130 return -EINVAL;
5131
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005132 if (new_level != level) {
5133 /* mute doesn't change */
5134
5135 cmos_cmd = (new_level > level) ?
5136 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005137 inc = new_level > level ? 1 : -1;
5138
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005139 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005140 !acpi_ec_write(volume_offset, level)))
5141 return -EIO;
5142
5143 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005144 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005145 !acpi_ec_write(volume_offset, i + inc))
5146 return -EIO;
5147
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005148 if (mute &&
5149 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5150 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005151 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005152 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005153 }
5154
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005155 if (new_mute != mute) {
5156 /* level doesn't change */
5157
5158 cmos_cmd = (new_mute) ?
5159 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005160
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005161 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005162 !acpi_ec_write(volume_offset, level + new_mute))
5163 return -EIO;
5164 }
5165 }
5166
5167 return 0;
5168}
5169
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005170static struct ibm_struct volume_driver_data = {
5171 .name = "volume",
5172 .read = volume_read,
5173 .write = volume_write,
5174};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005175
5176/*************************************************************************
5177 * Fan subdriver
5178 */
5179
5180/*
5181 * FAN ACCESS MODES
5182 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005183 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005184 * ACPI GFAN method: returns fan level
5185 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005186 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005187 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005188 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005189 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005190 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5191 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005192 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5193 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005194 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005195 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005196 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5197 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005198 *
5199 * Fan speed changes of any sort (including those caused by the
5200 * disengaged mode) are usually done slowly by the firmware as the
5201 * maximum ammount of fan duty cycle change per second seems to be
5202 * limited.
5203 *
5204 * Reading is not available if GFAN exists.
5205 * Writing is not available if SFAN exists.
5206 *
5207 * Bits
5208 * 7 automatic mode engaged;
5209 * (default operation mode of the ThinkPad)
5210 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005211 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005212 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005213 * the tachometer while the fan controller ramps up
5214 * the speed (which can take up to a few *minutes*).
5215 * Speeds up fan to 100% duty-cycle, which is far above
5216 * the standard RPM levels. It is not impossible that
5217 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005218 * 5-3 unused in some models. Extra bits for fan level
5219 * in others, but still useless as all values above
5220 * 7 map to the same speed as level 7 in these models.
5221 * 2-0 fan level (0..7 usually)
5222 * 0x00 = stop
5223 * 0x07 = max (set when temperatures critical)
5224 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005225 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005226 *
5227 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5228 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5229 * does so, its initial value is meaningless (0x07).
5230 *
5231 * For firmware bugs, refer to:
5232 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5233 *
5234 * ----
5235 *
5236 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5237 * Main fan tachometer reading (in RPM)
5238 *
5239 * This register is present on all ThinkPads with a new-style EC, and
5240 * it is known not to be present on the A21m/e, and T22, as there is
5241 * something else in offset 0x84 according to the ACPI DSDT. Other
5242 * ThinkPads from this same time period (and earlier) probably lack the
5243 * tachometer as well.
5244 *
5245 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5246 * was never fixed by IBM to report the EC firmware version string
5247 * probably support the tachometer (like the early X models), so
5248 * detecting it is quite hard. We need more data to know for sure.
5249 *
5250 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5251 * might result.
5252 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005253 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5254 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005255 *
5256 * For firmware bugs, refer to:
5257 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5258 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005259 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005260 * ThinkPad X31, X40, X41. Not available in the X60.
5261 *
5262 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5263 * high speed. ACPI DSDT seems to map these three speeds to levels
5264 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5265 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5266 *
5267 * The speeds are stored on handles
5268 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5269 *
5270 * There are three default speed sets, acessible as handles:
5271 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5272 *
5273 * ACPI DSDT switches which set is in use depending on various
5274 * factors.
5275 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005276 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005277 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5278 * but the ACPI tables just mention level 7.
5279 */
5280
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005281enum { /* Fan control constants */
5282 fan_status_offset = 0x2f, /* EC register 0x2f */
5283 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5284 * 0x84 must be read before 0x85 */
5285
5286 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5287 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5288
5289 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5290};
5291
5292enum fan_status_access_mode {
5293 TPACPI_FAN_NONE = 0, /* No fan status or control */
5294 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5295 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5296};
5297
5298enum fan_control_access_mode {
5299 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5300 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5301 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5302 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5303};
5304
5305enum fan_control_commands {
5306 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5307 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5308 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5309 * and also watchdog cmd */
5310};
5311
5312static int fan_control_allowed;
5313
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005314static enum fan_status_access_mode fan_status_access_mode;
5315static enum fan_control_access_mode fan_control_access_mode;
5316static enum fan_control_commands fan_control_commands;
5317
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005318static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005319static u8 fan_control_desired_level;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02005320static u8 fan_control_resume_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005321static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005322
5323static struct mutex fan_mutex;
5324
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005325static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005326static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005327
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005328TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5329TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005330 "\\FSPD", /* 600e/x, 770e, 770x */
5331 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005332TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005333 "JFNS", /* 770x-JL */
5334 ); /* all others */
5335
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005336/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005337 * Call with fan_mutex held
5338 */
5339static void fan_update_desired_level(u8 status)
5340{
5341 if ((status &
5342 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5343 if (status > 7)
5344 fan_control_desired_level = 7;
5345 else
5346 fan_control_desired_level = status;
5347 }
5348}
5349
5350static int fan_get_status(u8 *status)
5351{
5352 u8 s;
5353
5354 /* TODO:
5355 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5356
5357 switch (fan_status_access_mode) {
5358 case TPACPI_FAN_RD_ACPI_GFAN:
5359 /* 570, 600e/x, 770e, 770x */
5360
5361 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5362 return -EIO;
5363
5364 if (likely(status))
5365 *status = s & 0x07;
5366
5367 break;
5368
5369 case TPACPI_FAN_RD_TPEC:
5370 /* all except 570, 600e/x, 770e, 770x */
5371 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5372 return -EIO;
5373
5374 if (likely(status))
5375 *status = s;
5376
5377 break;
5378
5379 default:
5380 return -ENXIO;
5381 }
5382
5383 return 0;
5384}
5385
5386static int fan_get_status_safe(u8 *status)
5387{
5388 int rc;
5389 u8 s;
5390
5391 if (mutex_lock_interruptible(&fan_mutex))
5392 return -ERESTARTSYS;
5393 rc = fan_get_status(&s);
5394 if (!rc)
5395 fan_update_desired_level(s);
5396 mutex_unlock(&fan_mutex);
5397
5398 if (status)
5399 *status = s;
5400
5401 return rc;
5402}
5403
5404static int fan_get_speed(unsigned int *speed)
5405{
5406 u8 hi, lo;
5407
5408 switch (fan_status_access_mode) {
5409 case TPACPI_FAN_RD_TPEC:
5410 /* all except 570, 600e/x, 770e, 770x */
5411 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5412 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5413 return -EIO;
5414
5415 if (likely(speed))
5416 *speed = (hi << 8) | lo;
5417
5418 break;
5419
5420 default:
5421 return -ENXIO;
5422 }
5423
5424 return 0;
5425}
5426
5427static int fan_set_level(int level)
5428{
5429 if (!fan_control_allowed)
5430 return -EPERM;
5431
5432 switch (fan_control_access_mode) {
5433 case TPACPI_FAN_WR_ACPI_SFAN:
5434 if (level >= 0 && level <= 7) {
5435 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5436 return -EIO;
5437 } else
5438 return -EINVAL;
5439 break;
5440
5441 case TPACPI_FAN_WR_ACPI_FANS:
5442 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02005443 if (!(level & TP_EC_FAN_AUTO) &&
5444 !(level & TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005445 ((level < 0) || (level > 7)))
5446 return -EINVAL;
5447
5448 /* safety net should the EC not support AUTO
5449 * or FULLSPEED mode bits and just ignore them */
5450 if (level & TP_EC_FAN_FULLSPEED)
5451 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005452 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005453 level |= 4; /* safety min speed 4 */
5454
5455 if (!acpi_ec_write(fan_status_offset, level))
5456 return -EIO;
5457 else
5458 tp_features.fan_ctrl_status_undef = 0;
5459 break;
5460
5461 default:
5462 return -ENXIO;
5463 }
5464 return 0;
5465}
5466
5467static int fan_set_level_safe(int level)
5468{
5469 int rc;
5470
5471 if (!fan_control_allowed)
5472 return -EPERM;
5473
5474 if (mutex_lock_interruptible(&fan_mutex))
5475 return -ERESTARTSYS;
5476
5477 if (level == TPACPI_FAN_LAST_LEVEL)
5478 level = fan_control_desired_level;
5479
5480 rc = fan_set_level(level);
5481 if (!rc)
5482 fan_update_desired_level(level);
5483
5484 mutex_unlock(&fan_mutex);
5485 return rc;
5486}
5487
5488static int fan_set_enable(void)
5489{
5490 u8 s;
5491 int rc;
5492
5493 if (!fan_control_allowed)
5494 return -EPERM;
5495
5496 if (mutex_lock_interruptible(&fan_mutex))
5497 return -ERESTARTSYS;
5498
5499 switch (fan_control_access_mode) {
5500 case TPACPI_FAN_WR_ACPI_FANS:
5501 case TPACPI_FAN_WR_TPEC:
5502 rc = fan_get_status(&s);
5503 if (rc < 0)
5504 break;
5505
5506 /* Don't go out of emergency fan mode */
5507 if (s != 7) {
5508 s &= 0x07;
5509 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5510 }
5511
5512 if (!acpi_ec_write(fan_status_offset, s))
5513 rc = -EIO;
5514 else {
5515 tp_features.fan_ctrl_status_undef = 0;
5516 rc = 0;
5517 }
5518 break;
5519
5520 case TPACPI_FAN_WR_ACPI_SFAN:
5521 rc = fan_get_status(&s);
5522 if (rc < 0)
5523 break;
5524
5525 s &= 0x07;
5526
5527 /* Set fan to at least level 4 */
5528 s |= 4;
5529
5530 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005531 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005532 else
5533 rc = 0;
5534 break;
5535
5536 default:
5537 rc = -ENXIO;
5538 }
5539
5540 mutex_unlock(&fan_mutex);
5541 return rc;
5542}
5543
5544static int fan_set_disable(void)
5545{
5546 int rc;
5547
5548 if (!fan_control_allowed)
5549 return -EPERM;
5550
5551 if (mutex_lock_interruptible(&fan_mutex))
5552 return -ERESTARTSYS;
5553
5554 rc = 0;
5555 switch (fan_control_access_mode) {
5556 case TPACPI_FAN_WR_ACPI_FANS:
5557 case TPACPI_FAN_WR_TPEC:
5558 if (!acpi_ec_write(fan_status_offset, 0x00))
5559 rc = -EIO;
5560 else {
5561 fan_control_desired_level = 0;
5562 tp_features.fan_ctrl_status_undef = 0;
5563 }
5564 break;
5565
5566 case TPACPI_FAN_WR_ACPI_SFAN:
5567 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5568 rc = -EIO;
5569 else
5570 fan_control_desired_level = 0;
5571 break;
5572
5573 default:
5574 rc = -ENXIO;
5575 }
5576
5577
5578 mutex_unlock(&fan_mutex);
5579 return rc;
5580}
5581
5582static int fan_set_speed(int speed)
5583{
5584 int rc;
5585
5586 if (!fan_control_allowed)
5587 return -EPERM;
5588
5589 if (mutex_lock_interruptible(&fan_mutex))
5590 return -ERESTARTSYS;
5591
5592 rc = 0;
5593 switch (fan_control_access_mode) {
5594 case TPACPI_FAN_WR_ACPI_FANS:
5595 if (speed >= 0 && speed <= 65535) {
5596 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5597 speed, speed, speed))
5598 rc = -EIO;
5599 } else
5600 rc = -EINVAL;
5601 break;
5602
5603 default:
5604 rc = -ENXIO;
5605 }
5606
5607 mutex_unlock(&fan_mutex);
5608 return rc;
5609}
5610
5611static void fan_watchdog_reset(void)
5612{
5613 static int fan_watchdog_active;
5614
5615 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5616 return;
5617
5618 if (fan_watchdog_active)
5619 cancel_delayed_work(&fan_watchdog_task);
5620
5621 if (fan_watchdog_maxinterval > 0 &&
5622 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5623 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005624 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005625 msecs_to_jiffies(fan_watchdog_maxinterval
5626 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005627 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005628 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005629 "watchdog will not trigger\n");
5630 }
5631 } else
5632 fan_watchdog_active = 0;
5633}
5634
5635static void fan_watchdog_fire(struct work_struct *ignored)
5636{
5637 int rc;
5638
5639 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5640 return;
5641
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005642 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005643 rc = fan_set_enable();
5644 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005645 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005646 "will try again later...\n", -rc);
5647 /* reschedule for later */
5648 fan_watchdog_reset();
5649 }
5650}
5651
5652/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005653 * SYSFS fan layout: hwmon compatible (device)
5654 *
5655 * pwm*_enable:
5656 * 0: "disengaged" mode
5657 * 1: manual mode
5658 * 2: native EC "auto" mode (recommended, hardware default)
5659 *
5660 * pwm*: set speed in manual mode, ignored otherwise.
5661 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5662 * interpolation.
5663 *
5664 * fan*_input: tachometer reading, RPM
5665 *
5666 *
5667 * SYSFS fan layout: extensions
5668 *
5669 * fan_watchdog (driver):
5670 * fan watchdog interval in seconds, 0 disables (default), max 120
5671 */
5672
5673/* sysfs fan pwm1_enable ----------------------------------------------- */
5674static ssize_t fan_pwm1_enable_show(struct device *dev,
5675 struct device_attribute *attr,
5676 char *buf)
5677{
5678 int res, mode;
5679 u8 status;
5680
5681 res = fan_get_status_safe(&status);
5682 if (res)
5683 return res;
5684
5685 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5686 if (status != fan_control_initial_status) {
5687 tp_features.fan_ctrl_status_undef = 0;
5688 } else {
5689 /* Return most likely status. In fact, it
5690 * might be the only possible status */
5691 status = TP_EC_FAN_AUTO;
5692 }
5693 }
5694
5695 if (status & TP_EC_FAN_FULLSPEED) {
5696 mode = 0;
5697 } else if (status & TP_EC_FAN_AUTO) {
5698 mode = 2;
5699 } else
5700 mode = 1;
5701
5702 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5703}
5704
5705static ssize_t fan_pwm1_enable_store(struct device *dev,
5706 struct device_attribute *attr,
5707 const char *buf, size_t count)
5708{
5709 unsigned long t;
5710 int res, level;
5711
5712 if (parse_strtoul(buf, 2, &t))
5713 return -EINVAL;
5714
5715 switch (t) {
5716 case 0:
5717 level = TP_EC_FAN_FULLSPEED;
5718 break;
5719 case 1:
5720 level = TPACPI_FAN_LAST_LEVEL;
5721 break;
5722 case 2:
5723 level = TP_EC_FAN_AUTO;
5724 break;
5725 case 3:
5726 /* reserved for software-controlled auto mode */
5727 return -ENOSYS;
5728 default:
5729 return -EINVAL;
5730 }
5731
5732 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005733 if (res == -ENXIO)
5734 return -EINVAL;
5735 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005736 return res;
5737
5738 fan_watchdog_reset();
5739
5740 return count;
5741}
5742
5743static struct device_attribute dev_attr_fan_pwm1_enable =
5744 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5745 fan_pwm1_enable_show, fan_pwm1_enable_store);
5746
5747/* sysfs fan pwm1 ------------------------------------------------------ */
5748static ssize_t fan_pwm1_show(struct device *dev,
5749 struct device_attribute *attr,
5750 char *buf)
5751{
5752 int res;
5753 u8 status;
5754
5755 res = fan_get_status_safe(&status);
5756 if (res)
5757 return res;
5758
5759 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5760 if (status != fan_control_initial_status) {
5761 tp_features.fan_ctrl_status_undef = 0;
5762 } else {
5763 status = TP_EC_FAN_AUTO;
5764 }
5765 }
5766
5767 if ((status &
5768 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5769 status = fan_control_desired_level;
5770
5771 if (status > 7)
5772 status = 7;
5773
5774 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5775}
5776
5777static ssize_t fan_pwm1_store(struct device *dev,
5778 struct device_attribute *attr,
5779 const char *buf, size_t count)
5780{
5781 unsigned long s;
5782 int rc;
5783 u8 status, newlevel;
5784
5785 if (parse_strtoul(buf, 255, &s))
5786 return -EINVAL;
5787
5788 /* scale down from 0-255 to 0-7 */
5789 newlevel = (s >> 5) & 0x07;
5790
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005791 if (mutex_lock_interruptible(&fan_mutex))
5792 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005793
5794 rc = fan_get_status(&status);
5795 if (!rc && (status &
5796 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5797 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005798 if (rc == -ENXIO)
5799 rc = -EINVAL;
5800 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005801 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005802 fan_watchdog_reset();
5803 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005804 }
5805
5806 mutex_unlock(&fan_mutex);
5807 return (rc)? rc : count;
5808}
5809
5810static struct device_attribute dev_attr_fan_pwm1 =
5811 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5812 fan_pwm1_show, fan_pwm1_store);
5813
5814/* sysfs fan fan1_input ------------------------------------------------ */
5815static ssize_t fan_fan1_input_show(struct device *dev,
5816 struct device_attribute *attr,
5817 char *buf)
5818{
5819 int res;
5820 unsigned int speed;
5821
5822 res = fan_get_speed(&speed);
5823 if (res < 0)
5824 return res;
5825
5826 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5827}
5828
5829static struct device_attribute dev_attr_fan_fan1_input =
5830 __ATTR(fan1_input, S_IRUGO,
5831 fan_fan1_input_show, NULL);
5832
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005833/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005834static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5835 char *buf)
5836{
5837 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5838}
5839
5840static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5841 const char *buf, size_t count)
5842{
5843 unsigned long t;
5844
5845 if (parse_strtoul(buf, 120, &t))
5846 return -EINVAL;
5847
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005848 if (!fan_control_allowed)
5849 return -EPERM;
5850
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005851 fan_watchdog_maxinterval = t;
5852 fan_watchdog_reset();
5853
5854 return count;
5855}
5856
5857static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5858 fan_fan_watchdog_show, fan_fan_watchdog_store);
5859
5860/* --------------------------------------------------------------------- */
5861static struct attribute *fan_attributes[] = {
5862 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5863 &dev_attr_fan_fan1_input.attr,
5864 NULL
5865};
5866
5867static const struct attribute_group fan_attr_group = {
5868 .attrs = fan_attributes,
5869};
5870
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005871static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005872{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005873 int rc;
5874
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005875 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5876
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005877 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005878 fan_status_access_mode = TPACPI_FAN_NONE;
5879 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005880 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005881 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005882 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005883 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005884
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005885 TPACPI_ACPIHANDLE_INIT(fans);
5886 TPACPI_ACPIHANDLE_INIT(gfan);
5887 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005888
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005889 if (gfan_handle) {
5890 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005891 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005892 } else {
5893 /* all other ThinkPads: note that even old-style
5894 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005895 if (likely(acpi_ec_read(fan_status_offset,
5896 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005897 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005898
5899 /* In some ThinkPads, neither the EC nor the ACPI
5900 * DSDT initialize the fan status, and it ends up
5901 * being set to 0x07 when it *could* be either
5902 * 0x07 or 0x80.
5903 *
5904 * Enable for TP-1Y (T43), TP-78 (R51e),
5905 * TP-76 (R52), TP-70 (T43, R52), which are known
5906 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005907 if (fan_control_initial_status == 0x07) {
5908 switch (thinkpad_id.ec_model) {
5909 case 0x5931: /* TP-1Y */
5910 case 0x3837: /* TP-78 */
5911 case 0x3637: /* TP-76 */
5912 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005913 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005914 "fan_init: initial fan status "
5915 "is unknown, assuming it is "
5916 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005917 tp_features.fan_ctrl_status_undef = 1;
5918 ;;
5919 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005920 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005921 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005922 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005923 "ThinkPad ACPI EC access misbehaving, "
5924 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005925 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005926 }
5927 }
5928
5929 if (sfan_handle) {
5930 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005931 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005932 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005933 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005934 } else {
5935 if (!gfan_handle) {
5936 /* gfan without sfan means no fan control */
5937 /* all other models implement TP EC 0x2f control */
5938
5939 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005940 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005941 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005942 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005943 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005944 TPACPI_FAN_CMD_SPEED |
5945 TPACPI_FAN_CMD_LEVEL |
5946 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005947 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005948 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005949 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005950 TPACPI_FAN_CMD_LEVEL |
5951 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005952 }
5953 }
5954 }
5955
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005956 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5957 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5958 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5959 fan_status_access_mode, fan_control_access_mode);
5960
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005961 /* fan control master switch */
5962 if (!fan_control_allowed) {
5963 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5964 fan_control_commands = 0;
5965 dbg_printk(TPACPI_DBG_INIT,
5966 "fan control features disabled by parameter\n");
5967 }
5968
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005969 /* update fan_control_desired_level */
5970 if (fan_status_access_mode != TPACPI_FAN_NONE)
5971 fan_get_status_safe(NULL);
5972
5973 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5974 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005975 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005976 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005977 if (rc < 0)
5978 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005979
5980 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5981 &driver_attr_fan_watchdog);
5982 if (rc < 0) {
5983 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5984 &fan_attr_group);
5985 return rc;
5986 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005987 return 0;
5988 } else
5989 return 1;
5990}
5991
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005992static void fan_exit(void)
5993{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005994 vdbg_printk(TPACPI_DBG_EXIT,
5995 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005996
5997 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005998 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005999 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6000 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006001
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006002 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006003 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006004}
6005
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006006static void fan_suspend(pm_message_t state)
6007{
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006008 int rc;
6009
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006010 if (!fan_control_allowed)
6011 return;
6012
6013 /* Store fan status in cache */
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006014 fan_control_resume_level = 0;
6015 rc = fan_get_status_safe(&fan_control_resume_level);
6016 if (rc < 0)
6017 printk(TPACPI_NOTICE
6018 "failed to read fan level for later "
6019 "restore during resume: %d\n", rc);
6020
6021 /* if it is undefined, don't attempt to restore it.
6022 * KEEP THIS LAST */
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006023 if (tp_features.fan_ctrl_status_undef)
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006024 fan_control_resume_level = 0;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006025}
6026
6027static void fan_resume(void)
6028{
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006029 u8 current_level = 7;
6030 bool do_set = false;
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006031 int rc;
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006032
6033 /* DSDT *always* updates status on resume */
6034 tp_features.fan_ctrl_status_undef = 0;
6035
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006036 if (!fan_control_allowed ||
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006037 !fan_control_resume_level ||
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006038 (fan_get_status_safe(&current_level) < 0))
6039 return;
6040
6041 switch (fan_control_access_mode) {
6042 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006043 /* never decrease fan level */
6044 do_set = (fan_control_resume_level > current_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006045 break;
6046 case TPACPI_FAN_WR_ACPI_FANS:
6047 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006048 /* never decrease fan level, scale is:
6049 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6050 *
6051 * We expect the firmware to set either 7 or AUTO, but we
6052 * handle FULLSPEED out of paranoia.
6053 *
6054 * So, we can safely only restore FULLSPEED or 7, anything
6055 * else could slow the fan. Restoring AUTO is useless, at
6056 * best that's exactly what the DSDT already set (it is the
6057 * slower it uses).
6058 *
6059 * Always keep in mind that the DSDT *will* have set the
6060 * fans to what the vendor supposes is the best level. We
6061 * muck with it only to speed the fan up.
6062 */
6063 if (fan_control_resume_level != 7 &&
6064 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6065 return;
6066 else
6067 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6068 (current_level != fan_control_resume_level);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006069 break;
6070 default:
6071 return;
6072 }
6073 if (do_set) {
6074 printk(TPACPI_NOTICE
6075 "restoring fan level to 0x%02x\n",
Henrique de Moraes Holschuh0081b162008-11-09 10:54:02 -02006076 fan_control_resume_level);
6077 rc = fan_set_level_safe(fan_control_resume_level);
6078 if (rc < 0)
6079 printk(TPACPI_NOTICE
6080 "failed to restore fan level: %d\n", rc);
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006081 }
6082}
6083
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006084static int fan_read(char *p)
6085{
6086 int len = 0;
6087 int rc;
6088 u8 status;
6089 unsigned int speed = 0;
6090
6091 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006092 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006093 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006094 rc = fan_get_status_safe(&status);
6095 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006096 return rc;
6097
6098 len += sprintf(p + len, "status:\t\t%s\n"
6099 "level:\t\t%d\n",
6100 (status != 0) ? "enabled" : "disabled", status);
6101 break;
6102
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006103 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006104 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006105 rc = fan_get_status_safe(&status);
6106 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006107 return rc;
6108
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006109 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006110 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006111 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006112 else
6113 /* Return most likely status. In fact, it
6114 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006115 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006116 }
6117
6118 len += sprintf(p + len, "status:\t\t%s\n",
6119 (status != 0) ? "enabled" : "disabled");
6120
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006121 rc = fan_get_speed(&speed);
6122 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006123 return rc;
6124
6125 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6126
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006127 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006128 /* Disengaged mode takes precedence */
6129 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006130 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006131 len += sprintf(p + len, "level:\t\tauto\n");
6132 else
6133 len += sprintf(p + len, "level:\t\t%d\n", status);
6134 break;
6135
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006136 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006137 default:
6138 len += sprintf(p + len, "status:\t\tnot supported\n");
6139 }
6140
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006141 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006142 len += sprintf(p + len, "commands:\tlevel <level>");
6143
6144 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006145 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006146 len += sprintf(p + len, " (<level> is 0-7)\n");
6147 break;
6148
6149 default:
6150 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006151 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006152 break;
6153 }
6154 }
6155
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006156 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006157 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006158 "commands:\twatchdog <timeout> (<timeout> "
6159 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006160
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006161 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006162 len += sprintf(p + len, "commands:\tspeed <speed>"
6163 " (<speed> is 0-65535)\n");
6164
6165 return len;
6166}
6167
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006168static int fan_write_cmd_level(const char *cmd, int *rc)
6169{
6170 int level;
6171
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006172 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006173 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006174 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006175 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006176 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006177 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006178 return 0;
6179
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006180 *rc = fan_set_level_safe(level);
6181 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006182 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006183 "access mode %d", fan_control_access_mode);
6184
6185 return 1;
6186}
6187
6188static int fan_write_cmd_enable(const char *cmd, int *rc)
6189{
6190 if (strlencmp(cmd, "enable") != 0)
6191 return 0;
6192
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006193 *rc = fan_set_enable();
6194 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006195 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006196 "access mode %d", fan_control_access_mode);
6197
6198 return 1;
6199}
6200
6201static int fan_write_cmd_disable(const char *cmd, int *rc)
6202{
6203 if (strlencmp(cmd, "disable") != 0)
6204 return 0;
6205
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006206 *rc = fan_set_disable();
6207 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006208 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006209 "access mode %d", fan_control_access_mode);
6210
6211 return 1;
6212}
6213
6214static int fan_write_cmd_speed(const char *cmd, int *rc)
6215{
6216 int speed;
6217
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006218 /* TODO:
6219 * Support speed <low> <medium> <high> ? */
6220
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006221 if (sscanf(cmd, "speed %d", &speed) != 1)
6222 return 0;
6223
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006224 *rc = fan_set_speed(speed);
6225 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006226 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006227 "access mode %d", fan_control_access_mode);
6228
6229 return 1;
6230}
6231
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006232static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6233{
6234 int interval;
6235
6236 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6237 return 0;
6238
6239 if (interval < 0 || interval > 120)
6240 *rc = -EINVAL;
6241 else
6242 fan_watchdog_maxinterval = interval;
6243
6244 return 1;
6245}
6246
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006247static int fan_write(char *buf)
6248{
6249 char *cmd;
6250 int rc = 0;
6251
6252 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006253 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006254 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006255 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006256 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006257 fan_write_cmd_disable(cmd, &rc) ||
6258 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006259 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006260 fan_write_cmd_speed(cmd, &rc))
6261 )
6262 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006263 else if (!rc)
6264 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006265 }
6266
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006267 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006268}
6269
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006270static struct ibm_struct fan_driver_data = {
6271 .name = "fan",
6272 .read = fan_read,
6273 .write = fan_write,
6274 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006275 .suspend = fan_suspend,
6276 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006277};
6278
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006279/****************************************************************************
6280 ****************************************************************************
6281 *
6282 * Infrastructure
6283 *
6284 ****************************************************************************
6285 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006287/* sysfs name ---------------------------------------------------------- */
6288static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6289 struct device_attribute *attr,
6290 char *buf)
6291{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006292 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006293}
6294
6295static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6296 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6297
6298/* --------------------------------------------------------------------- */
6299
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006300/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03006301static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006302
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006303/*
6304 * Module and infrastructure proble, init and exit handling
6305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006307static int force_load;
6308
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006309#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006310static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006311{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006312 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006313
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006314 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006315}
6316#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6317
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006318static void ibm_exit(struct ibm_struct *ibm)
6319{
6320 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6321
6322 list_del_init(&ibm->all_drivers);
6323
6324 if (ibm->flags.acpi_notify_installed) {
6325 dbg_printk(TPACPI_DBG_EXIT,
6326 "%s: acpi_remove_notify_handler\n", ibm->name);
6327 BUG_ON(!ibm->acpi);
6328 acpi_remove_notify_handler(*ibm->acpi->handle,
6329 ibm->acpi->type,
6330 dispatch_acpi_notify);
6331 ibm->flags.acpi_notify_installed = 0;
6332 ibm->flags.acpi_notify_installed = 0;
6333 }
6334
6335 if (ibm->flags.proc_created) {
6336 dbg_printk(TPACPI_DBG_EXIT,
6337 "%s: remove_proc_entry\n", ibm->name);
6338 remove_proc_entry(ibm->name, proc_dir);
6339 ibm->flags.proc_created = 0;
6340 }
6341
6342 if (ibm->flags.acpi_driver_registered) {
6343 dbg_printk(TPACPI_DBG_EXIT,
6344 "%s: acpi_bus_unregister_driver\n", ibm->name);
6345 BUG_ON(!ibm->acpi);
6346 acpi_bus_unregister_driver(ibm->acpi->driver);
6347 kfree(ibm->acpi->driver);
6348 ibm->acpi->driver = NULL;
6349 ibm->flags.acpi_driver_registered = 0;
6350 }
6351
6352 if (ibm->flags.init_called && ibm->exit) {
6353 ibm->exit();
6354 ibm->flags.init_called = 0;
6355 }
6356
6357 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6358}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006359
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006360static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361{
6362 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006363 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 struct proc_dir_entry *entry;
6365
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006366 BUG_ON(ibm == NULL);
6367
6368 INIT_LIST_HEAD(&ibm->all_drivers);
6369
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006370 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 return 0;
6372
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006373 dbg_printk(TPACPI_DBG_INIT,
6374 "probing for %s\n", ibm->name);
6375
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006376 if (iibm->init) {
6377 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006378 if (ret > 0)
6379 return 0; /* probe failed */
6380 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006382
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006383 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 }
6385
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006386 if (ibm->acpi) {
6387 if (ibm->acpi->hid) {
6388 ret = register_tpacpi_subdriver(ibm);
6389 if (ret)
6390 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006391 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006392
6393 if (ibm->acpi->notify) {
6394 ret = setup_acpi_notify(ibm);
6395 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006396 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006397 ibm->name);
6398 ret = 0;
6399 goto err_out;
6400 }
6401 if (ret < 0)
6402 goto err_out;
6403 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006404 }
6405
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006406 dbg_printk(TPACPI_DBG_INIT,
6407 "%s installed\n", ibm->name);
6408
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006409 if (ibm->read) {
6410 entry = create_proc_entry(ibm->name,
6411 S_IFREG | S_IRUGO | S_IWUSR,
6412 proc_dir);
6413 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006414 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006415 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006416 ret = -ENODEV;
6417 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006418 }
6419 entry->owner = THIS_MODULE;
6420 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006421 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006422 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006423 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006424 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006427 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6428
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006430
6431err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006432 dbg_printk(TPACPI_DBG_INIT,
6433 "%s: at error exit path with result %d\n",
6434 ibm->name, ret);
6435
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006436 ibm_exit(ibm);
6437 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438}
6439
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006440/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006442/* returns 0 - probe ok, or < 0 - probe error.
6443 * Probe ok doesn't mean thinkpad found.
6444 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6445static int __must_check __init get_thinkpad_model_data(
6446 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006447{
Jeff Garzik18552562007-10-03 15:15:40 -04006448 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006449 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006450 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006451
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006452 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006453 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006454
6455 memset(tp, 0, sizeof(*tp));
6456
6457 if (dmi_name_in_vendors("IBM"))
6458 tp->vendor = PCI_VENDOR_ID_IBM;
6459 else if (dmi_name_in_vendors("LENOVO"))
6460 tp->vendor = PCI_VENDOR_ID_LENOVO;
6461 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006462 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006463
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006464 s = dmi_get_system_info(DMI_BIOS_VERSION);
6465 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6466 if (s && !tp->bios_version_str)
6467 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006468 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006469 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006470 tp->bios_model = tp->bios_version_str[0]
6471 | (tp->bios_version_str[1] << 8);
6472
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006473 /*
6474 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6475 * X32 or newer, all Z series; Some models must have an
6476 * up-to-date BIOS or they will not be detected.
6477 *
6478 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6479 */
6480 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006481 if (sscanf(dev->name,
6482 "IBM ThinkPad Embedded Controller -[%17c",
6483 ec_fw_string) == 1) {
6484 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6485 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006486
6487 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006488 if (!tp->ec_version_str)
6489 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006490 tp->ec_model = ec_fw_string[0]
6491 | (ec_fw_string[1] << 8);
6492 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006493 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006494 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006495
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006496 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6497 if (s && !strnicmp(s, "ThinkPad", 8)) {
6498 tp->model_str = kstrdup(s, GFP_KERNEL);
6499 if (!tp->model_str)
6500 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006501 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006502
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006503 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6504 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6505 if (s && !tp->nummodel_str)
6506 return -ENOMEM;
6507
6508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509}
6510
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006511static int __init probe_for_thinkpad(void)
6512{
6513 int is_thinkpad;
6514
6515 if (acpi_disabled)
6516 return -ENODEV;
6517
6518 /*
6519 * Non-ancient models have better DMI tagging, but very old models
6520 * don't.
6521 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006522 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006523
6524 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006525 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006526 if (!ec_handle) {
6527 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006528 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006529 "Not yet supported ThinkPad detected!\n");
6530 return -ENODEV;
6531 }
6532
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006533 /*
6534 * Risks a regression on very old machines, but reduces potential
6535 * false positives a damn great deal
6536 */
6537 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006538 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006539
6540 if (!is_thinkpad && !force_load)
6541 return -ENODEV;
6542
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006543 return 0;
6544}
6545
6546
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006547/* Module init, exit, parameters */
6548
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006549static struct ibm_init_struct ibms_init[] __initdata = {
6550 {
6551 .init = thinkpad_acpi_driver_init,
6552 .data = &thinkpad_acpi_driver_data,
6553 },
6554 {
6555 .init = hotkey_init,
6556 .data = &hotkey_driver_data,
6557 },
6558 {
6559 .init = bluetooth_init,
6560 .data = &bluetooth_driver_data,
6561 },
6562 {
6563 .init = wan_init,
6564 .data = &wan_driver_data,
6565 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006566#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006567 {
6568 .init = video_init,
6569 .data = &video_driver_data,
6570 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006571#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006572 {
6573 .init = light_init,
6574 .data = &light_driver_data,
6575 },
6576#ifdef CONFIG_THINKPAD_ACPI_DOCK
6577 {
6578 .init = dock_init,
6579 .data = &dock_driver_data[0],
6580 },
6581 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03006582 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006583 .data = &dock_driver_data[1],
6584 },
6585#endif
6586#ifdef CONFIG_THINKPAD_ACPI_BAY
6587 {
6588 .init = bay_init,
6589 .data = &bay_driver_data,
6590 },
6591#endif
6592 {
6593 .init = cmos_init,
6594 .data = &cmos_driver_data,
6595 },
6596 {
6597 .init = led_init,
6598 .data = &led_driver_data,
6599 },
6600 {
6601 .init = beep_init,
6602 .data = &beep_driver_data,
6603 },
6604 {
6605 .init = thermal_init,
6606 .data = &thermal_driver_data,
6607 },
6608 {
6609 .data = &ecdump_driver_data,
6610 },
6611 {
6612 .init = brightness_init,
6613 .data = &brightness_driver_data,
6614 },
6615 {
6616 .data = &volume_driver_data,
6617 },
6618 {
6619 .init = fan_init,
6620 .data = &fan_driver_data,
6621 },
6622};
6623
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006624static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6625{
6626 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006627 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006628
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006629 if (!kp || !kp->name || !val)
6630 return -EINVAL;
6631
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006632 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6633 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006634 WARN_ON(ibm == NULL);
6635
6636 if (!ibm || !ibm->name)
6637 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006638
6639 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6640 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006641 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006642 strcpy(ibms_init[i].param, val);
6643 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006644 return 0;
6645 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006646 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006647
6648 return -EINVAL;
6649}
6650
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006651module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006652MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006653 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006654
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006655module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006656MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006657
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006658module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006659MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006660 "Attempts to load the driver even on a "
6661 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006662
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006663module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006664MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006665 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006666
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006667module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006668MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006669 "Selects brightness control strategy: "
6670 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006671
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006672module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006673MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006674 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006675
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006676module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006677MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006678 "used for backwards compatibility with userspace, "
6679 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006680
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006681#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006682 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006683 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006684 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006685
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006686TPACPI_PARAM(hotkey);
6687TPACPI_PARAM(bluetooth);
6688TPACPI_PARAM(video);
6689TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006690#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006691TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006692#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006693#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006694TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006695#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006696TPACPI_PARAM(cmos);
6697TPACPI_PARAM(led);
6698TPACPI_PARAM(beep);
6699TPACPI_PARAM(ecdump);
6700TPACPI_PARAM(brightness);
6701TPACPI_PARAM(volume);
6702TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006703
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006704static void thinkpad_acpi_module_exit(void)
6705{
6706 struct ibm_struct *ibm, *itmp;
6707
6708 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6709
6710 list_for_each_entry_safe_reverse(ibm, itmp,
6711 &tpacpi_all_drivers,
6712 all_drivers) {
6713 ibm_exit(ibm);
6714 }
6715
6716 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6717
6718 if (tpacpi_inputdev) {
6719 if (tp_features.input_device_registered)
6720 input_unregister_device(tpacpi_inputdev);
6721 else
6722 input_free_device(tpacpi_inputdev);
6723 }
6724
6725 if (tpacpi_hwmon)
6726 hwmon_device_unregister(tpacpi_hwmon);
6727
6728 if (tp_features.sensors_pdev_attrs_registered)
6729 device_remove_file(&tpacpi_sensors_pdev->dev,
6730 &dev_attr_thinkpad_acpi_pdev_name);
6731 if (tpacpi_sensors_pdev)
6732 platform_device_unregister(tpacpi_sensors_pdev);
6733 if (tpacpi_pdev)
6734 platform_device_unregister(tpacpi_pdev);
6735
6736 if (tp_features.sensors_pdrv_attrs_registered)
6737 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6738 if (tp_features.platform_drv_attrs_registered)
6739 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6740
6741 if (tp_features.sensors_pdrv_registered)
6742 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6743
6744 if (tp_features.platform_drv_registered)
6745 platform_driver_unregister(&tpacpi_pdriver);
6746
6747 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006748 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006749
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006750 if (tpacpi_wq)
6751 destroy_workqueue(tpacpi_wq);
6752
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006753 kfree(thinkpad_id.bios_version_str);
6754 kfree(thinkpad_id.ec_version_str);
6755 kfree(thinkpad_id.model_str);
6756}
6757
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006758
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006759static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760{
6761 int ret, i;
6762
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006763 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6764
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006765 /* Parameter checking */
6766 if (hotkey_report_mode > 2)
6767 return -EINVAL;
6768
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006769 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006770
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006771 ret = get_thinkpad_model_data(&thinkpad_id);
6772 if (ret) {
6773 printk(TPACPI_ERR
6774 "unable to get DMI data: %d\n", ret);
6775 thinkpad_acpi_module_exit();
6776 return ret;
6777 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006778 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006779 if (ret) {
6780 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006781 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006784 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006785
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006786 TPACPI_ACPIHANDLE_INIT(ecrd);
6787 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006789 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6790 if (!tpacpi_wq) {
6791 thinkpad_acpi_module_exit();
6792 return -ENOMEM;
6793 }
6794
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006795 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006797 printk(TPACPI_ERR
6798 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006799 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 return -ENODEV;
6801 }
6802 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006803
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006804 ret = platform_driver_register(&tpacpi_pdriver);
6805 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006806 printk(TPACPI_ERR
6807 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006808 thinkpad_acpi_module_exit();
6809 return ret;
6810 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006811 tp_features.platform_drv_registered = 1;
6812
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006813 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6814 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006815 printk(TPACPI_ERR
6816 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006817 thinkpad_acpi_module_exit();
6818 return ret;
6819 }
6820 tp_features.sensors_pdrv_registered = 1;
6821
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006822 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006823 if (!ret) {
6824 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006825 ret = tpacpi_create_driver_attributes(
6826 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006827 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006828 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006829 printk(TPACPI_ERR
6830 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006831 thinkpad_acpi_module_exit();
6832 return ret;
6833 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006834 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006835
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006836
6837 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006838 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006839 NULL, 0);
6840 if (IS_ERR(tpacpi_pdev)) {
6841 ret = PTR_ERR(tpacpi_pdev);
6842 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006843 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006844 thinkpad_acpi_module_exit();
6845 return ret;
6846 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006847 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006848 TPACPI_HWMON_DRVR_NAME,
6849 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006850 if (IS_ERR(tpacpi_sensors_pdev)) {
6851 ret = PTR_ERR(tpacpi_sensors_pdev);
6852 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006853 printk(TPACPI_ERR
6854 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006855 thinkpad_acpi_module_exit();
6856 return ret;
6857 }
6858 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6859 &dev_attr_thinkpad_acpi_pdev_name);
6860 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006861 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006862 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006863 thinkpad_acpi_module_exit();
6864 return ret;
6865 }
6866 tp_features.sensors_pdev_attrs_registered = 1;
6867 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006868 if (IS_ERR(tpacpi_hwmon)) {
6869 ret = PTR_ERR(tpacpi_hwmon);
6870 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006871 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006872 thinkpad_acpi_module_exit();
6873 return ret;
6874 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006875 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006876 tpacpi_inputdev = input_allocate_device();
6877 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006878 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006879 thinkpad_acpi_module_exit();
6880 return -ENOMEM;
6881 } else {
6882 /* Prepare input device, but don't register */
6883 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006884 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006885 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006886 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6887 thinkpad_id.vendor :
6888 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006889 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6890 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6891 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006892 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6893 ret = ibm_init(&ibms_init[i]);
6894 if (ret >= 0 && *ibms_init[i].param)
6895 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006897 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 return ret;
6899 }
6900 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006901 ret = input_register_device(tpacpi_inputdev);
6902 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006903 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006904 thinkpad_acpi_module_exit();
6905 return ret;
6906 } else {
6907 tp_features.input_device_registered = 1;
6908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006910 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 return 0;
6912}
6913
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006914/* Please remove this in year 2009 */
6915MODULE_ALIAS("ibm_acpi");
6916
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03006917MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6918
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006919/*
6920 * DMI matching for module autoloading
6921 *
6922 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6923 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6924 *
6925 * Only models listed in thinkwiki will be supported, so add yours
6926 * if it is not there yet.
6927 */
6928#define IBM_BIOS_MODULE_ALIAS(__type) \
6929 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6930
6931/* Non-ancient thinkpads */
6932MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6933MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6934
6935/* Ancient thinkpad BIOSes have to be identified by
6936 * BIOS type or model number, and there are far less
6937 * BIOS types than model numbers... */
6938IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6939IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6940IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6941
6942MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006943MODULE_DESCRIPTION(TPACPI_DESC);
6944MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006945MODULE_LICENSE("GPL");
6946
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006947module_init(thinkpad_acpi_module_init);
6948module_exit(thinkpad_acpi_module_exit);