blob: 2ab3633f4e56372c3da7b19cbafd9ded9022d73f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh6a2e2932008-01-08 13:02:56 -02006 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh1cee5cc2008-01-08 13:02:57 -020024#define TPACPI_VERSION "0.19"
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -020025#define TPACPI_SYSFS_VERSION 0x020200
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
57#include <linux/kthread.h>
58#include <linux/freezer.h>
59#include <linux/delay.h>
60
61#include <linux/nvram.h>
62#include <linux/proc_fs.h>
63#include <linux/sysfs.h>
64#include <linux/backlight.h>
65#include <linux/fb.h>
66#include <linux/platform_device.h>
67#include <linux/hwmon.h>
68#include <linux/hwmon-sysfs.h>
69#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030070#include <linux/leds.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020071#include <asm/uaccess.h>
72
73#include <linux/dmi.h>
74#include <linux/jiffies.h>
75#include <linux/workqueue.h>
76
77#include <acpi/acpi_drivers.h>
78#include <acpi/acnamesp.h>
79
80#include <linux/pci_ids.h>
81
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020082
83/* ThinkPad CMOS commands */
84#define TP_CMOS_VOLUME_DOWN 0
85#define TP_CMOS_VOLUME_UP 1
86#define TP_CMOS_VOLUME_MUTE 2
87#define TP_CMOS_BRIGHTNESS_UP 4
88#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030089#define TP_CMOS_THINKLIGHT_ON 12
90#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020091
92/* NVRAM Addresses */
93enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
99};
100
101/* NVRAM bit masks */
102enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
116};
117
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200118/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200119#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200120
121/* Input IDs */
122#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123#define TPACPI_HKEY_INPUT_VERSION 0x4101
124
125
126/****************************************************************************
127 * Main driver
128 */
129
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200130#define TPACPI_NAME "thinkpad"
131#define TPACPI_DESC "ThinkPad ACPI Extras"
132#define TPACPI_FILE TPACPI_NAME "_acpi"
133#define TPACPI_URL "http://ibm-acpi.sf.net/"
134#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200135
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200136#define TPACPI_PROC_DIR "ibm"
137#define TPACPI_ACPI_EVENT_PREFIX "ibm"
138#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300139#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200140#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200141
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300142#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
143
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200144#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200145
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200146/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200147#define TPACPI_LOG TPACPI_FILE ": "
148#define TPACPI_ERR KERN_ERR TPACPI_LOG
149#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
150#define TPACPI_INFO KERN_INFO TPACPI_LOG
151#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200152
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200153#define TPACPI_DBG_ALL 0xffff
154#define TPACPI_DBG_ALL 0xffff
155#define TPACPI_DBG_INIT 0x0001
156#define TPACPI_DBG_EXIT 0x0002
157#define dbg_printk(a_dbg_level, format, arg...) \
158 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200159 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
160 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200161#ifdef CONFIG_THINKPAD_ACPI_DEBUG
162#define vdbg_printk(a_dbg_level, format, arg...) \
163 dbg_printk(a_dbg_level, format, ## arg)
164static const char *str_supported(int is_supported);
165#else
166#define vdbg_printk(a_dbg_level, format, arg...)
167#endif
168
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200169#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
170#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
171#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200172
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200173
174/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200175 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200176 */
177
178struct ibm_struct;
179
180struct tp_acpi_drv_struct {
181 const struct acpi_device_id *hid;
182 struct acpi_driver *driver;
183
184 void (*notify) (struct ibm_struct *, u32);
185 acpi_handle *handle;
186 u32 type;
187 struct acpi_device *device;
188};
189
190struct ibm_struct {
191 char *name;
192
193 int (*read) (char *);
194 int (*write) (char *);
195 void (*exit) (void);
196 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200197 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200198
199 struct list_head all_drivers;
200
201 struct tp_acpi_drv_struct *acpi;
202
203 struct {
204 u8 acpi_driver_registered:1;
205 u8 acpi_notify_installed:1;
206 u8 proc_created:1;
207 u8 init_called:1;
208 u8 experimental:1;
209 } flags;
210};
211
212struct ibm_init_struct {
213 char param[32];
214
215 int (*init) (struct ibm_init_struct *);
216 struct ibm_struct *data;
217};
218
219static struct {
220#ifdef CONFIG_THINKPAD_ACPI_BAY
221 u32 bay_status:1;
222 u32 bay_eject:1;
223 u32 bay_status2:1;
224 u32 bay_eject2:1;
225#endif
226 u32 bluetooth:1;
227 u32 hotkey:1;
228 u32 hotkey_mask:1;
229 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200230 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200231 u32 light:1;
232 u32 light_status:1;
233 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300234 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200235 u32 wan:1;
236 u32 fan_ctrl_status_undef:1;
237 u32 input_device_registered:1;
238 u32 platform_drv_registered:1;
239 u32 platform_drv_attrs_registered:1;
240 u32 sensors_pdrv_registered:1;
241 u32 sensors_pdrv_attrs_registered:1;
242 u32 sensors_pdev_attrs_registered:1;
243 u32 hotkey_poll_active:1;
244} tp_features;
245
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300246static struct {
247 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300248 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300249} tp_warned;
250
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251struct thinkpad_id_data {
252 unsigned int vendor; /* ThinkPad vendor:
253 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
254
255 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
256 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
257
258 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
259 u16 ec_model;
260
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300261 char *model_str; /* ThinkPad T43 */
262 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200263};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264static struct thinkpad_id_data thinkpad_id;
265
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300266static enum {
267 TPACPI_LIFE_INIT = 0,
268 TPACPI_LIFE_RUNNING,
269 TPACPI_LIFE_EXITING,
270} tpacpi_lifecycle;
271
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200272static int experimental;
273static u32 dbg_level;
274
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300275/* Special LED class that can defer work */
276struct tpacpi_led_classdev {
277 struct led_classdev led_classdev;
278 struct work_struct work;
279 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300280 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300281};
282
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300283/****************************************************************************
284 ****************************************************************************
285 *
286 * ACPI Helpers and device model
287 *
288 ****************************************************************************
289 ****************************************************************************/
290
291/*************************************************************************
292 * ACPI basic handles
293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300295static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200297#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 static acpi_handle object##_handle; \
299 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400300 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 static char *object##_paths[] = { paths }
302
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200303TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400304 "\\_SB.PCI.ISA.EC", /* 570 */
305 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
306 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
307 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
308 "\\_SB.PCI0.ICH3.EC0", /* R31 */
309 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300310 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200312TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
313TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200315TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
316 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400317 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
318 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300319 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400320
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200321TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400322 "^HKEY", /* R30, R31 */
323 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300324 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400325
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200326TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
327 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
328 "\\_SB.PCI0.VID0", /* 770e */
329 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
330 "\\_SB.PCI0.AGP.VID", /* all others */
331 ); /* R30, R31 */
332
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400333
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300334/*************************************************************************
335 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200336 */
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static int acpi_evalf(acpi_handle handle,
339 void *res, char *method, char *fmt, ...)
340{
341 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400342 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200343 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400344 struct acpi_buffer result, *resultp;
345 union acpi_object out_obj;
346 acpi_status status;
347 va_list ap;
348 char res_type;
349 int success;
350 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200353 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355 }
356
357 if (*fmt == 'q') {
358 quiet = 1;
359 fmt++;
360 } else
361 quiet = 0;
362
363 res_type = *(fmt++);
364
365 params.count = 0;
366 params.pointer = &in_objs[0];
367
368 va_start(ap, fmt);
369 while (*fmt) {
370 char c = *(fmt++);
371 switch (c) {
372 case 'd': /* int */
373 in_objs[params.count].integer.value = va_arg(ap, int);
374 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
375 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400376 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200378 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 "with invalid format character '%c'\n", c);
380 return 0;
381 }
382 }
383 va_end(ap);
384
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400385 if (res_type != 'v') {
386 result.length = sizeof(out_obj);
387 result.pointer = &out_obj;
388 resultp = &result;
389 } else
390 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400392 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (res)
397 *(int *)res = out_obj.integer.value;
398 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
399 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400400 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 success = status == AE_OK;
402 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400403 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200405 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 "with invalid format character '%c'\n", res_type);
407 return 0;
408 }
409
410 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200411 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 method, fmt0, status);
413
414 return success;
415}
416
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200417static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300418{
419 int v;
420
421 if (ecrd_handle) {
422 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
423 return 0;
424 *p = v;
425 } else {
426 if (ec_read(i, p) < 0)
427 return 0;
428 }
429
430 return 1;
431}
432
433static int acpi_ec_write(int i, u8 v)
434{
435 if (ecwr_handle) {
436 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
437 return 0;
438 } else {
439 if (ec_write(i, v) < 0)
440 return 0;
441 }
442
443 return 1;
444}
445
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200446#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300447static int _sta(acpi_handle handle)
448{
449 int status;
450
451 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
452 status = 0;
453
454 return status;
455}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200456#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300457
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300458static int issue_thinkpad_cmos_command(int cmos_cmd)
459{
460 if (!cmos_handle)
461 return -ENXIO;
462
463 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
464 return -EIO;
465
466 return 0;
467}
468
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300469/*************************************************************************
470 * ACPI device model
471 */
472
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200473#define TPACPI_ACPIHANDLE_INIT(object) \
474 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200475 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
476
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300477static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300478 acpi_handle *handle, acpi_handle parent,
479 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300480{
481 int i;
482 acpi_status status;
483
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300484 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
485 name);
486
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300487 for (i = 0; i < num_paths; i++) {
488 status = acpi_get_handle(parent, paths[i], handle);
489 if (ACPI_SUCCESS(status)) {
490 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300491 dbg_printk(TPACPI_DBG_INIT,
492 "Found ACPI handle %s for %s\n",
493 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300494 return;
495 }
496 }
497
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300498 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
499 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300500 *handle = NULL;
501}
502
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300503static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504{
505 struct ibm_struct *ibm = data;
506
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300507 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
508 return;
509
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300510 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300511 return;
512
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300513 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514}
515
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300516static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300517{
518 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300519 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300520
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300521 BUG_ON(!ibm->acpi);
522
523 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300524 return 0;
525
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300526 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300527 "setting up ACPI notify for %s\n", ibm->name);
528
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300529 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
530 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200531 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300532 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533 return -ENODEV;
534 }
535
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300536 acpi_driver_data(ibm->acpi->device) = ibm;
537 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200538 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300539 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300540
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300541 status = acpi_install_notify_handler(*ibm->acpi->handle,
542 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543 if (ACPI_FAILURE(status)) {
544 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200545 printk(TPACPI_NOTICE
546 "another device driver is already "
547 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300548 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200549 printk(TPACPI_ERR
550 "acpi_install_notify_handler(%s) failed: %d\n",
551 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300552 }
553 return -ENODEV;
554 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300555 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300556 return 0;
557}
558
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300559static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300560{
561 return 0;
562}
563
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300564static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300565{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300566 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300567
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300568 dbg_printk(TPACPI_DBG_INIT,
569 "registering %s as an ACPI driver\n", ibm->name);
570
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300571 BUG_ON(!ibm->acpi);
572
573 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
574 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200575 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300576 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300577 }
578
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200579 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300580 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200581
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300582 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300583
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300584 rc = acpi_bus_register_driver(ibm->acpi->driver);
585 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200586 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200587 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300588 kfree(ibm->acpi->driver);
589 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300590 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300591 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300592
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300593 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300594}
595
596
597/****************************************************************************
598 ****************************************************************************
599 *
600 * Procfs Helpers
601 *
602 ****************************************************************************
603 ****************************************************************************/
604
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300605static int dispatch_procfs_read(char *page, char **start, off_t off,
606 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300607{
608 struct ibm_struct *ibm = data;
609 int len;
610
611 if (!ibm || !ibm->read)
612 return -EINVAL;
613
614 len = ibm->read(page);
615 if (len < 0)
616 return len;
617
618 if (len <= off + count)
619 *eof = 1;
620 *start = page + off;
621 len -= off;
622 if (len > count)
623 len = count;
624 if (len < 0)
625 len = 0;
626
627 return len;
628}
629
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300630static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200631 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300632 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300633{
634 struct ibm_struct *ibm = data;
635 char *kernbuf;
636 int ret;
637
638 if (!ibm || !ibm->write)
639 return -EINVAL;
640
641 kernbuf = kmalloc(count + 2, GFP_KERNEL);
642 if (!kernbuf)
643 return -ENOMEM;
644
645 if (copy_from_user(kernbuf, userbuf, count)) {
646 kfree(kernbuf);
647 return -EFAULT;
648 }
649
650 kernbuf[count] = 0;
651 strcat(kernbuf, ",");
652 ret = ibm->write(kernbuf);
653 if (ret == 0)
654 ret = count;
655
656 kfree(kernbuf);
657
658 return ret;
659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static char *next_cmd(char **cmds)
662{
663 char *start = *cmds;
664 char *end;
665
666 while ((end = strchr(start, ',')) && end == start)
667 start = end + 1;
668
669 if (!end)
670 return NULL;
671
672 *end = 0;
673 *cmds = end + 1;
674 return start;
675}
676
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300677
678/****************************************************************************
679 ****************************************************************************
680 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300681 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300682 *
683 ****************************************************************************
684 ****************************************************************************/
685
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300686static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300687static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700688static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300689static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300690static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200691static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300692
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200693static int tpacpi_suspend_handler(struct platform_device *pdev,
694 pm_message_t state)
695{
696 struct ibm_struct *ibm, *itmp;
697
698 list_for_each_entry_safe(ibm, itmp,
699 &tpacpi_all_drivers,
700 all_drivers) {
701 if (ibm->suspend)
702 (ibm->suspend)(state);
703 }
704
705 return 0;
706}
707
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300708static int tpacpi_resume_handler(struct platform_device *pdev)
709{
710 struct ibm_struct *ibm, *itmp;
711
712 list_for_each_entry_safe(ibm, itmp,
713 &tpacpi_all_drivers,
714 all_drivers) {
715 if (ibm->resume)
716 (ibm->resume)();
717 }
718
719 return 0;
720}
721
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300722static struct platform_driver tpacpi_pdriver = {
723 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200724 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300725 .owner = THIS_MODULE,
726 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200727 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300728 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300729};
730
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300731static struct platform_driver tpacpi_hwmon_pdriver = {
732 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200733 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300734 .owner = THIS_MODULE,
735 },
736};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300737
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300738/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300739 * sysfs support helpers
740 */
741
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200742struct attribute_set {
743 unsigned int members, max_members;
744 struct attribute_group group;
745};
746
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300747struct attribute_set_obj {
748 struct attribute_set s;
749 struct attribute *a;
750} __attribute__((packed));
751
752static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200753 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300754{
755 struct attribute_set_obj *sobj;
756
757 if (max_members == 0)
758 return NULL;
759
760 /* Allocates space for implicit NULL at the end too */
761 sobj = kzalloc(sizeof(struct attribute_set_obj) +
762 max_members * sizeof(struct attribute *),
763 GFP_KERNEL);
764 if (!sobj)
765 return NULL;
766 sobj->s.max_members = max_members;
767 sobj->s.group.attrs = &sobj->a;
768 sobj->s.group.name = name;
769
770 return &sobj->s;
771}
772
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200773#define destroy_attr_set(_set) \
774 kfree(_set);
775
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300776/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200777static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300778{
779 if (!s || !attr)
780 return -EINVAL;
781
782 if (s->members >= s->max_members)
783 return -ENOMEM;
784
785 s->group.attrs[s->members] = attr;
786 s->members++;
787
788 return 0;
789}
790
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200791static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300792 struct attribute **attr,
793 unsigned int count)
794{
795 int i, res;
796
797 for (i = 0; i < count; i++) {
798 res = add_to_attr_set(s, attr[i]);
799 if (res)
800 return res;
801 }
802
803 return 0;
804}
805
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200806static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300807{
808 sysfs_remove_group(kobj, &s->group);
809 destroy_attr_set(s);
810}
811
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200812#define register_attr_set_with_sysfs(_attr_set, _kobj) \
813 sysfs_create_group(_kobj, &_attr_set->group)
814
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300815static int parse_strtoul(const char *buf,
816 unsigned long max, unsigned long *value)
817{
818 char *endp;
819
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300820 while (*buf && isspace(*buf))
821 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300822 *value = simple_strtoul(buf, &endp, 0);
823 while (*endp && isspace(*endp))
824 endp++;
825 if (*endp || *value > max)
826 return -EINVAL;
827
828 return 0;
829}
830
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300831static int __init tpacpi_query_bcl_levels(acpi_handle handle)
832{
833 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
834 union acpi_object *obj;
835 int rc;
836
837 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
838 obj = (union acpi_object *)buffer.pointer;
839 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
840 printk(TPACPI_ERR "Unknown _BCL data, "
841 "please report this to %s\n", TPACPI_MAIL);
842 rc = 0;
843 } else {
844 rc = obj->package.count;
845 }
846 } else {
847 return 0;
848 }
849
850 kfree(buffer.pointer);
851 return rc;
852}
853
854static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
855 u32 lvl, void *context, void **rv)
856{
857 char name[ACPI_PATH_SEGMENT_LENGTH];
858 struct acpi_buffer buffer = { sizeof(name), &name };
859
860 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
861 !strncmp("_BCL", name, sizeof(name) - 1)) {
862 BUG_ON(!rv || !*rv);
863 **(int **)rv = tpacpi_query_bcl_levels(handle);
864 return AE_CTRL_TERMINATE;
865 } else {
866 return AE_OK;
867 }
868}
869
870/*
871 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
872 */
873static int __init tpacpi_check_std_acpi_brightness_support(void)
874{
875 int status;
876 int bcl_levels = 0;
877 void *bcl_ptr = &bcl_levels;
878
879 if (!vid_handle) {
880 TPACPI_ACPIHANDLE_INIT(vid);
881 }
882 if (!vid_handle)
883 return 0;
884
885 /*
886 * Search for a _BCL method, and execute it. This is safe on all
887 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
888 * BIOS in ACPI backlight control mode. We do NOT have to care
889 * about calling the _BCL method in an enabled video device, any
890 * will do for our purposes.
891 */
892
893 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
894 tpacpi_acpi_walk_find_bcl, NULL,
895 &bcl_ptr);
896
897 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
898 tp_features.bright_acpimode = 1;
899 return (bcl_levels - 2);
900 }
901
902 return 0;
903}
904
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200905/*************************************************************************
906 * thinkpad-acpi driver attributes
907 */
908
909/* interface_version --------------------------------------------------- */
910static ssize_t tpacpi_driver_interface_version_show(
911 struct device_driver *drv,
912 char *buf)
913{
914 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
915}
916
917static DRIVER_ATTR(interface_version, S_IRUGO,
918 tpacpi_driver_interface_version_show, NULL);
919
920/* debug_level --------------------------------------------------------- */
921static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
922 char *buf)
923{
924 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
925}
926
927static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
928 const char *buf, size_t count)
929{
930 unsigned long t;
931
932 if (parse_strtoul(buf, 0xffff, &t))
933 return -EINVAL;
934
935 dbg_level = t;
936
937 return count;
938}
939
940static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
941 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
942
943/* version ------------------------------------------------------------- */
944static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
945 char *buf)
946{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200947 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
948 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200949}
950
951static DRIVER_ATTR(version, S_IRUGO,
952 tpacpi_driver_version_show, NULL);
953
954/* --------------------------------------------------------------------- */
955
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200956static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200957 &driver_attr_debug_level, &driver_attr_version,
958 &driver_attr_interface_version,
959};
960
961static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
962{
963 int i, res;
964
965 i = 0;
966 res = 0;
967 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
968 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
969 i++;
970 }
971
972 return res;
973}
974
975static void tpacpi_remove_driver_attributes(struct device_driver *drv)
976{
977 int i;
978
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200979 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200980 driver_remove_file(drv, tpacpi_driver_attributes[i]);
981}
982
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300983/****************************************************************************
984 ****************************************************************************
985 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300986 * Subdrivers
987 *
988 ****************************************************************************
989 ****************************************************************************/
990
991/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300992 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300993 */
994
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300995static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200997 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
998 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001000 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001001 (thinkpad_id.bios_version_str) ?
1002 thinkpad_id.bios_version_str : "unknown",
1003 (thinkpad_id.ec_version_str) ?
1004 thinkpad_id.ec_version_str : "unknown");
1005
1006 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001007 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001008 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1009 "IBM" : ((thinkpad_id.vendor ==
1010 PCI_VENDOR_ID_LENOVO) ?
1011 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001012 thinkpad_id.model_str,
1013 (thinkpad_id.nummodel_str) ?
1014 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017}
1018
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001019static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 int len = 0;
1022
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001023 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1024 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 return len;
1027}
1028
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001029static struct ibm_struct thinkpad_acpi_driver_data = {
1030 .name = "driver",
1031 .read = thinkpad_acpi_driver_read,
1032};
1033
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001034/*************************************************************************
1035 * Hotkey subdriver
1036 */
1037
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001038enum { /* hot key scan codes (derived from ACPI DSDT) */
1039 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1040 TP_ACPI_HOTKEYSCAN_FNF2,
1041 TP_ACPI_HOTKEYSCAN_FNF3,
1042 TP_ACPI_HOTKEYSCAN_FNF4,
1043 TP_ACPI_HOTKEYSCAN_FNF5,
1044 TP_ACPI_HOTKEYSCAN_FNF6,
1045 TP_ACPI_HOTKEYSCAN_FNF7,
1046 TP_ACPI_HOTKEYSCAN_FNF8,
1047 TP_ACPI_HOTKEYSCAN_FNF9,
1048 TP_ACPI_HOTKEYSCAN_FNF10,
1049 TP_ACPI_HOTKEYSCAN_FNF11,
1050 TP_ACPI_HOTKEYSCAN_FNF12,
1051 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1052 TP_ACPI_HOTKEYSCAN_FNINSERT,
1053 TP_ACPI_HOTKEYSCAN_FNDELETE,
1054 TP_ACPI_HOTKEYSCAN_FNHOME,
1055 TP_ACPI_HOTKEYSCAN_FNEND,
1056 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1057 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1058 TP_ACPI_HOTKEYSCAN_FNSPACE,
1059 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1060 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1061 TP_ACPI_HOTKEYSCAN_MUTE,
1062 TP_ACPI_HOTKEYSCAN_THINKPAD,
1063};
1064
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001065enum { /* Keys available through NVRAM polling */
1066 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1067 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1068};
1069
1070enum { /* Positions of some of the keys in hotkey masks */
1071 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1072 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1073 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1074 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1075 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1076 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1077 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1078 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1079 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1080 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1081 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1082};
1083
1084enum { /* NVRAM to ACPI HKEY group map */
1085 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1086 TP_ACPI_HKEY_ZOOM_MASK |
1087 TP_ACPI_HKEY_DISPSWTCH_MASK |
1088 TP_ACPI_HKEY_HIBERNATE_MASK,
1089 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1090 TP_ACPI_HKEY_BRGHTDWN_MASK,
1091 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1092 TP_ACPI_HKEY_VOLDWN_MASK |
1093 TP_ACPI_HKEY_MUTE_MASK,
1094};
1095
1096#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1097struct tp_nvram_state {
1098 u16 thinkpad_toggle:1;
1099 u16 zoom_toggle:1;
1100 u16 display_toggle:1;
1101 u16 thinklight_toggle:1;
1102 u16 hibernate_toggle:1;
1103 u16 displayexp_toggle:1;
1104 u16 display_state:1;
1105 u16 brightness_toggle:1;
1106 u16 volume_toggle:1;
1107 u16 mute:1;
1108
1109 u8 brightness_level;
1110 u8 volume_level;
1111};
1112
1113static struct task_struct *tpacpi_hotkey_task;
1114static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1115static int hotkey_poll_freq = 10; /* Hz */
1116static struct mutex hotkey_thread_mutex;
1117static struct mutex hotkey_thread_data_mutex;
1118static unsigned int hotkey_config_change;
1119
1120#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1121
1122#define hotkey_source_mask 0U
1123
1124#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1125
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001126static struct mutex hotkey_mutex;
1127
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001128static enum { /* Reasons for waking up */
1129 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1130 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1131 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1132} hotkey_wakeup_reason;
1133
1134static int hotkey_autosleep_ack;
1135
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001136static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001137static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001138static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001139static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001140static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001141
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001142static unsigned int hotkey_report_mode;
1143
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001144static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001145
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001146static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001147
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001148#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1149#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001150 do { \
1151 mutex_lock(&hotkey_thread_data_mutex); \
1152 hotkey_config_change++; \
1153 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001154#define HOTKEY_CONFIG_CRITICAL_END \
1155 mutex_unlock(&hotkey_thread_data_mutex);
1156#else
1157#define HOTKEY_CONFIG_CRITICAL_START
1158#define HOTKEY_CONFIG_CRITICAL_END
1159#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1160
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001161/* HKEY.MHKG() return bits */
1162#define TP_HOTKEY_TABLET_MASK (1 << 3)
1163
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001164static int hotkey_get_wlsw(int *status)
1165{
1166 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1167 return -EIO;
1168 return 0;
1169}
1170
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001171static int hotkey_get_tablet_mode(int *status)
1172{
1173 int s;
1174
1175 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1176 return -EIO;
1177
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001178 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1179 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001180}
1181
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001182/*
1183 * Call with hotkey_mutex held
1184 */
1185static int hotkey_mask_get(void)
1186{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001187 u32 m = 0;
1188
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001189 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001190 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001191 return -EIO;
1192 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001193 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001194
1195 return 0;
1196}
1197
1198/*
1199 * Call with hotkey_mutex held
1200 */
1201static int hotkey_mask_set(u32 mask)
1202{
1203 int i;
1204 int rc = 0;
1205
1206 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001207 if (!tp_warned.hotkey_mask_ff &&
1208 (mask == 0xffff || mask == 0xffffff ||
1209 mask == 0xffffffff)) {
1210 tp_warned.hotkey_mask_ff = 1;
1211 printk(TPACPI_NOTICE
1212 "setting the hotkey mask to 0x%08x is likely "
1213 "not the best way to go about it\n", mask);
1214 printk(TPACPI_NOTICE
1215 "please consider using the driver defaults, "
1216 "and refer to up-to-date thinkpad-acpi "
1217 "documentation\n");
1218 }
1219
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001220 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001221 for (i = 0; i < 32; i++) {
1222 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001223 /* enable in firmware mask only keys not in NVRAM
1224 * mode, but enable the key in the cached hotkey_mask
1225 * regardless of mode, or the key will end up
1226 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001227 if (!acpi_evalf(hkey_handle,
1228 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001229 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001230 rc = -EIO;
1231 break;
1232 } else {
1233 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1234 }
1235 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001236 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001237
1238 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001239 if (!hotkey_mask_get() && !rc &&
1240 (hotkey_mask & ~hotkey_source_mask) !=
1241 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001242 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001243 "requested hot key mask 0x%08x, but "
1244 "firmware forced it to 0x%08x\n",
1245 mask, hotkey_mask);
1246 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001247 } else {
1248#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1249 HOTKEY_CONFIG_CRITICAL_START
1250 hotkey_mask = mask & hotkey_source_mask;
1251 HOTKEY_CONFIG_CRITICAL_END
1252 hotkey_mask_get();
1253 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001254 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001255 "requested hot key mask 0x%08x, "
1256 "forced to 0x%08x (NVRAM poll mask is "
1257 "0x%08x): no firmware mask support\n",
1258 mask, hotkey_mask, hotkey_source_mask);
1259 }
1260#else
1261 hotkey_mask_get();
1262 rc = -ENXIO;
1263#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001264 }
1265
1266 return rc;
1267}
1268
1269static int hotkey_status_get(int *status)
1270{
1271 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1272 return -EIO;
1273
1274 return 0;
1275}
1276
1277static int hotkey_status_set(int status)
1278{
1279 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1280 return -EIO;
1281
1282 return 0;
1283}
1284
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001285static void tpacpi_input_send_radiosw(void)
1286{
1287 int wlsw;
1288
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001289 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001290 mutex_lock(&tpacpi_inputdev_send_mutex);
1291
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001292 input_report_switch(tpacpi_inputdev,
1293 SW_RADIO, !!wlsw);
1294 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001295
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001296 mutex_unlock(&tpacpi_inputdev_send_mutex);
1297 }
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001298}
1299
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001300static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001301{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001302 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001303
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001304 if (tp_features.hotkey_tablet &&
1305 !hotkey_get_tablet_mode(&state)) {
1306 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001307
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001308 input_report_switch(tpacpi_inputdev,
1309 SW_TABLET_MODE, !!state);
1310 input_sync(tpacpi_inputdev);
1311
1312 mutex_unlock(&tpacpi_inputdev_send_mutex);
1313 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001314}
1315
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001316static void tpacpi_input_send_key(unsigned int scancode)
1317{
1318 unsigned int keycode;
1319
1320 keycode = hotkey_keycode_map[scancode];
1321
1322 if (keycode != KEY_RESERVED) {
1323 mutex_lock(&tpacpi_inputdev_send_mutex);
1324
1325 input_report_key(tpacpi_inputdev, keycode, 1);
1326 if (keycode == KEY_UNKNOWN)
1327 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1328 scancode);
1329 input_sync(tpacpi_inputdev);
1330
1331 input_report_key(tpacpi_inputdev, keycode, 0);
1332 if (keycode == KEY_UNKNOWN)
1333 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1334 scancode);
1335 input_sync(tpacpi_inputdev);
1336
1337 mutex_unlock(&tpacpi_inputdev_send_mutex);
1338 }
1339}
1340
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001341#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1342static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1343
1344static void tpacpi_hotkey_send_key(unsigned int scancode)
1345{
1346 tpacpi_input_send_key(scancode);
1347 if (hotkey_report_mode < 2) {
1348 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1349 0x80, 0x1001 + scancode);
1350 }
1351}
1352
1353static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1354{
1355 u8 d;
1356
1357 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1358 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1359 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1360 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1361 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1362 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1363 }
1364 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1365 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1366 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1367 }
1368 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1369 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1370 n->displayexp_toggle =
1371 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1372 }
1373 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1374 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1375 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1376 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1377 n->brightness_toggle =
1378 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1379 }
1380 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1381 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1382 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1383 >> TP_NVRAM_POS_LEVEL_VOLUME;
1384 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1385 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1386 }
1387}
1388
1389#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001390 do { \
1391 if ((mask & (1 << __scancode)) && \
1392 oldn->__member != newn->__member) \
1393 tpacpi_hotkey_send_key(__scancode); \
1394 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001395
1396#define TPACPI_MAY_SEND_KEY(__scancode) \
1397 do { if (mask & (1 << __scancode)) \
1398 tpacpi_hotkey_send_key(__scancode); } while (0)
1399
1400static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001401 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001402 u32 mask)
1403{
1404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1408
1409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1410
1411 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1412
1413 /* handle volume */
1414 if (oldn->volume_toggle != newn->volume_toggle) {
1415 if (oldn->mute != newn->mute) {
1416 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1417 }
1418 if (oldn->volume_level > newn->volume_level) {
1419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1420 } else if (oldn->volume_level < newn->volume_level) {
1421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1422 } else if (oldn->mute == newn->mute) {
1423 /* repeated key presses that didn't change state */
1424 if (newn->mute) {
1425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1426 } else if (newn->volume_level != 0) {
1427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1428 } else {
1429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1430 }
1431 }
1432 }
1433
1434 /* handle brightness */
1435 if (oldn->brightness_toggle != newn->brightness_toggle) {
1436 if (oldn->brightness_level < newn->brightness_level) {
1437 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1438 } else if (oldn->brightness_level > newn->brightness_level) {
1439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1440 } else {
1441 /* repeated key presses that didn't change state */
1442 if (newn->brightness_level != 0) {
1443 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1444 } else {
1445 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1446 }
1447 }
1448 }
1449}
1450
1451#undef TPACPI_COMPARE_KEY
1452#undef TPACPI_MAY_SEND_KEY
1453
1454static int hotkey_kthread(void *data)
1455{
1456 struct tp_nvram_state s[2];
1457 u32 mask;
1458 unsigned int si, so;
1459 unsigned long t;
1460 unsigned int change_detector, must_reset;
1461
1462 mutex_lock(&hotkey_thread_mutex);
1463
1464 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1465 goto exit;
1466
1467 set_freezable();
1468
1469 so = 0;
1470 si = 1;
1471 t = 0;
1472
1473 /* Initial state for compares */
1474 mutex_lock(&hotkey_thread_data_mutex);
1475 change_detector = hotkey_config_change;
1476 mask = hotkey_source_mask & hotkey_mask;
1477 mutex_unlock(&hotkey_thread_data_mutex);
1478 hotkey_read_nvram(&s[so], mask);
1479
1480 while (!kthread_should_stop() && hotkey_poll_freq) {
1481 if (t == 0)
1482 t = 1000/hotkey_poll_freq;
1483 t = msleep_interruptible(t);
1484 if (unlikely(kthread_should_stop()))
1485 break;
1486 must_reset = try_to_freeze();
1487 if (t > 0 && !must_reset)
1488 continue;
1489
1490 mutex_lock(&hotkey_thread_data_mutex);
1491 if (must_reset || hotkey_config_change != change_detector) {
1492 /* forget old state on thaw or config change */
1493 si = so;
1494 t = 0;
1495 change_detector = hotkey_config_change;
1496 }
1497 mask = hotkey_source_mask & hotkey_mask;
1498 mutex_unlock(&hotkey_thread_data_mutex);
1499
1500 if (likely(mask)) {
1501 hotkey_read_nvram(&s[si], mask);
1502 if (likely(si != so)) {
1503 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001504 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001505 }
1506 }
1507
1508 so = si;
1509 si ^= 1;
1510 }
1511
1512exit:
1513 mutex_unlock(&hotkey_thread_mutex);
1514 return 0;
1515}
1516
1517static void hotkey_poll_stop_sync(void)
1518{
1519 if (tpacpi_hotkey_task) {
1520 if (frozen(tpacpi_hotkey_task) ||
1521 freezing(tpacpi_hotkey_task))
1522 thaw_process(tpacpi_hotkey_task);
1523
1524 kthread_stop(tpacpi_hotkey_task);
1525 tpacpi_hotkey_task = NULL;
1526 mutex_lock(&hotkey_thread_mutex);
1527 /* at this point, the thread did exit */
1528 mutex_unlock(&hotkey_thread_mutex);
1529 }
1530}
1531
1532/* call with hotkey_mutex held */
1533static void hotkey_poll_setup(int may_warn)
1534{
1535 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1536 hotkey_poll_freq > 0 &&
1537 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1538 if (!tpacpi_hotkey_task) {
1539 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001540 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001541 if (IS_ERR(tpacpi_hotkey_task)) {
1542 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001543 printk(TPACPI_ERR
1544 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001545 "for hotkey polling\n");
1546 }
1547 }
1548 } else {
1549 hotkey_poll_stop_sync();
1550 if (may_warn &&
1551 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001552 printk(TPACPI_NOTICE
1553 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001554 "which is currently disabled\n",
1555 hotkey_source_mask);
1556 }
1557 }
1558}
1559
1560static void hotkey_poll_setup_safe(int may_warn)
1561{
1562 mutex_lock(&hotkey_mutex);
1563 hotkey_poll_setup(may_warn);
1564 mutex_unlock(&hotkey_mutex);
1565}
1566
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001567#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1568
1569static void hotkey_poll_setup_safe(int __unused)
1570{
1571}
1572
1573#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1574
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001575static int hotkey_inputdev_open(struct input_dev *dev)
1576{
1577 switch (tpacpi_lifecycle) {
1578 case TPACPI_LIFE_INIT:
1579 /*
1580 * hotkey_init will call hotkey_poll_setup_safe
1581 * at the appropriate moment
1582 */
1583 return 0;
1584 case TPACPI_LIFE_EXITING:
1585 return -EBUSY;
1586 case TPACPI_LIFE_RUNNING:
1587 hotkey_poll_setup_safe(0);
1588 return 0;
1589 }
1590
1591 /* Should only happen if tpacpi_lifecycle is corrupt */
1592 BUG();
1593 return -EBUSY;
1594}
1595
1596static void hotkey_inputdev_close(struct input_dev *dev)
1597{
1598 /* disable hotkey polling when possible */
1599 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1600 hotkey_poll_setup_safe(0);
1601}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001602
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001603/* sysfs hotkey enable ------------------------------------------------- */
1604static ssize_t hotkey_enable_show(struct device *dev,
1605 struct device_attribute *attr,
1606 char *buf)
1607{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001608 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001609
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001610 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001611 if (res)
1612 return res;
1613
1614 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1615}
1616
1617static ssize_t hotkey_enable_store(struct device *dev,
1618 struct device_attribute *attr,
1619 const char *buf, size_t count)
1620{
1621 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001622 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001623
1624 if (parse_strtoul(buf, 1, &t))
1625 return -EINVAL;
1626
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001627 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001628
1629 return (res) ? res : count;
1630}
1631
1632static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001633 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001634 hotkey_enable_show, hotkey_enable_store);
1635
1636/* sysfs hotkey mask --------------------------------------------------- */
1637static ssize_t hotkey_mask_show(struct device *dev,
1638 struct device_attribute *attr,
1639 char *buf)
1640{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001641 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001642
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001643 if (mutex_lock_interruptible(&hotkey_mutex))
1644 return -ERESTARTSYS;
1645 res = hotkey_mask_get();
1646 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001647
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001648 return (res)?
1649 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001650}
1651
1652static ssize_t hotkey_mask_store(struct device *dev,
1653 struct device_attribute *attr,
1654 const char *buf, size_t count)
1655{
1656 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001657 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001658
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001659 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001660 return -EINVAL;
1661
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001662 if (mutex_lock_interruptible(&hotkey_mutex))
1663 return -ERESTARTSYS;
1664
1665 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001666
1667#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1668 hotkey_poll_setup(1);
1669#endif
1670
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001671 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001672
1673 return (res) ? res : count;
1674}
1675
1676static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001677 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001678 hotkey_mask_show, hotkey_mask_store);
1679
1680/* sysfs hotkey bios_enabled ------------------------------------------- */
1681static ssize_t hotkey_bios_enabled_show(struct device *dev,
1682 struct device_attribute *attr,
1683 char *buf)
1684{
1685 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1686}
1687
1688static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001689 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001690
1691/* sysfs hotkey bios_mask ---------------------------------------------- */
1692static ssize_t hotkey_bios_mask_show(struct device *dev,
1693 struct device_attribute *attr,
1694 char *buf)
1695{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001696 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001697}
1698
1699static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001700 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001701
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001702/* sysfs hotkey all_mask ----------------------------------------------- */
1703static ssize_t hotkey_all_mask_show(struct device *dev,
1704 struct device_attribute *attr,
1705 char *buf)
1706{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001707 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1708 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001709}
1710
1711static struct device_attribute dev_attr_hotkey_all_mask =
1712 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1713
1714/* sysfs hotkey recommended_mask --------------------------------------- */
1715static ssize_t hotkey_recommended_mask_show(struct device *dev,
1716 struct device_attribute *attr,
1717 char *buf)
1718{
1719 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001720 (hotkey_all_mask | hotkey_source_mask)
1721 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001722}
1723
1724static struct device_attribute dev_attr_hotkey_recommended_mask =
1725 __ATTR(hotkey_recommended_mask, S_IRUGO,
1726 hotkey_recommended_mask_show, NULL);
1727
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001728#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1729
1730/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1731static ssize_t hotkey_source_mask_show(struct device *dev,
1732 struct device_attribute *attr,
1733 char *buf)
1734{
1735 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1736}
1737
1738static ssize_t hotkey_source_mask_store(struct device *dev,
1739 struct device_attribute *attr,
1740 const char *buf, size_t count)
1741{
1742 unsigned long t;
1743
1744 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1745 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1746 return -EINVAL;
1747
1748 if (mutex_lock_interruptible(&hotkey_mutex))
1749 return -ERESTARTSYS;
1750
1751 HOTKEY_CONFIG_CRITICAL_START
1752 hotkey_source_mask = t;
1753 HOTKEY_CONFIG_CRITICAL_END
1754
1755 hotkey_poll_setup(1);
1756
1757 mutex_unlock(&hotkey_mutex);
1758
1759 return count;
1760}
1761
1762static struct device_attribute dev_attr_hotkey_source_mask =
1763 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1764 hotkey_source_mask_show, hotkey_source_mask_store);
1765
1766/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1767static ssize_t hotkey_poll_freq_show(struct device *dev,
1768 struct device_attribute *attr,
1769 char *buf)
1770{
1771 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1772}
1773
1774static ssize_t hotkey_poll_freq_store(struct device *dev,
1775 struct device_attribute *attr,
1776 const char *buf, size_t count)
1777{
1778 unsigned long t;
1779
1780 if (parse_strtoul(buf, 25, &t))
1781 return -EINVAL;
1782
1783 if (mutex_lock_interruptible(&hotkey_mutex))
1784 return -ERESTARTSYS;
1785
1786 hotkey_poll_freq = t;
1787
1788 hotkey_poll_setup(1);
1789 mutex_unlock(&hotkey_mutex);
1790
1791 return count;
1792}
1793
1794static struct device_attribute dev_attr_hotkey_poll_freq =
1795 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1796 hotkey_poll_freq_show, hotkey_poll_freq_store);
1797
1798#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1799
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001800/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001801static ssize_t hotkey_radio_sw_show(struct device *dev,
1802 struct device_attribute *attr,
1803 char *buf)
1804{
1805 int res, s;
1806 res = hotkey_get_wlsw(&s);
1807 if (res < 0)
1808 return res;
1809
1810 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1811}
1812
1813static struct device_attribute dev_attr_hotkey_radio_sw =
1814 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1815
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001816static void hotkey_radio_sw_notify_change(void)
1817{
1818 if (tp_features.hotkey_wlsw)
1819 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1820 "hotkey_radio_sw");
1821}
1822
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001823/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1824static ssize_t hotkey_tablet_mode_show(struct device *dev,
1825 struct device_attribute *attr,
1826 char *buf)
1827{
1828 int res, s;
1829 res = hotkey_get_tablet_mode(&s);
1830 if (res < 0)
1831 return res;
1832
1833 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1834}
1835
1836static struct device_attribute dev_attr_hotkey_tablet_mode =
1837 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1838
1839static void hotkey_tablet_mode_notify_change(void)
1840{
1841 if (tp_features.hotkey_tablet)
1842 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1843 "hotkey_tablet_mode");
1844}
1845
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001846/* sysfs hotkey report_mode -------------------------------------------- */
1847static ssize_t hotkey_report_mode_show(struct device *dev,
1848 struct device_attribute *attr,
1849 char *buf)
1850{
1851 return snprintf(buf, PAGE_SIZE, "%d\n",
1852 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1853}
1854
1855static struct device_attribute dev_attr_hotkey_report_mode =
1856 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1857
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001858/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001859static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1860 struct device_attribute *attr,
1861 char *buf)
1862{
1863 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1864}
1865
1866static struct device_attribute dev_attr_hotkey_wakeup_reason =
1867 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1868
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001869static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001870{
1871 if (tp_features.hotkey_mask)
1872 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1873 "wakeup_reason");
1874}
1875
1876/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001877static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1878 struct device_attribute *attr,
1879 char *buf)
1880{
1881 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1882}
1883
1884static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1885 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1886 hotkey_wakeup_hotunplug_complete_show, NULL);
1887
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001888static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001889{
1890 if (tp_features.hotkey_mask)
1891 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1892 "wakeup_hotunplug_complete");
1893}
1894
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001895/* --------------------------------------------------------------------- */
1896
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001897static struct attribute *hotkey_attributes[] __initdata = {
1898 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001899 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001900 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001901#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1902 &dev_attr_hotkey_mask.attr,
1903 &dev_attr_hotkey_all_mask.attr,
1904 &dev_attr_hotkey_recommended_mask.attr,
1905 &dev_attr_hotkey_source_mask.attr,
1906 &dev_attr_hotkey_poll_freq.attr,
1907#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001908};
1909
1910static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001911 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001912#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1913 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001914 &dev_attr_hotkey_all_mask.attr,
1915 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001916#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001917 &dev_attr_hotkey_wakeup_reason.attr,
1918 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001919};
1920
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001921static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001922{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001923 /* Requirements for changing the default keymaps:
1924 *
1925 * 1. Many of the keys are mapped to KEY_RESERVED for very
1926 * good reasons. Do not change them unless you have deep
1927 * knowledge on the IBM and Lenovo ThinkPad firmware for
1928 * the various ThinkPad models. The driver behaves
1929 * differently for KEY_RESERVED: such keys have their
1930 * hot key mask *unset* in mask_recommended, and also
1931 * in the initial hot key mask programmed into the
1932 * firmware at driver load time, which means the firm-
1933 * ware may react very differently if you change them to
1934 * something else;
1935 *
1936 * 2. You must be subscribed to the linux-thinkpad and
1937 * ibm-acpi-devel mailing lists, and you should read the
1938 * list archives since 2007 if you want to change the
1939 * keymaps. This requirement exists so that you will
1940 * know the past history of problems with the thinkpad-
1941 * acpi driver keymaps, and also that you will be
1942 * listening to any bug reports;
1943 *
1944 * 3. Do not send thinkpad-acpi specific patches directly to
1945 * for merging, *ever*. Send them to the linux-acpi
1946 * mailinglist for comments. Merging is to be done only
1947 * through acpi-test and the ACPI maintainer.
1948 *
1949 * If the above is too much to ask, don't change the keymap.
1950 * Ask the thinkpad-acpi maintainer to do it, instead.
1951 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001952 static u16 ibm_keycode_map[] __initdata = {
1953 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1954 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1955 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1956 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001957
1958 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001959 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1960 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1961 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001962
1963 /* brightness: firmware always reacts to them, unless
1964 * X.org did some tricks in the radeon BIOS scratch
1965 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001966 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001967 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001968
1969 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001970 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001971
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001972 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1973 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001974
1975 /* Volume: firmware always react to it and reprograms
1976 * the built-in *extra* mixer. Never map it to control
1977 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001978 KEY_RESERVED, /* 0x14: VOLUME UP */
1979 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1980 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001981
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001982 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001983
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001984 /* (assignments unknown, please report if found) */
1985 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1986 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1987 };
1988 static u16 lenovo_keycode_map[] __initdata = {
1989 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1990 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1991 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1992 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001993
1994 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001995 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1996 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1997 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001998
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001999 /* These either have to go through ACPI video, or
2000 * act like in the IBM ThinkPads, so don't ever
2001 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002002 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002003 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002004
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002005 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002006
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002007 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2008 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002009
2010 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2011 * react to it and reprograms the built-in *extra* mixer.
2012 * Never map it to control another mixer by default.
2013 *
2014 * T60?, T61, R60?, R61: firmware and EC tries to send
2015 * these over the regular keyboard, so these are no-ops,
2016 * but there are still weird bugs re. MUTE, so do not
2017 * change unless you get test reports from all Lenovo
2018 * models. May cause the BIOS to interfere with the
2019 * HDA mixer.
2020 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002021 KEY_RESERVED, /* 0x14: VOLUME UP */
2022 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2023 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002024
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002025 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002026
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002027 /* (assignments unknown, please report if found) */
2028 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2029 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2030 };
2031
2032#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2033#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2034#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2035
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002036 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002037 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002038 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002039
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002040 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2041
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002042 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002043 BUG_ON(tpacpi_inputdev->open != NULL ||
2044 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002045
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002046 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002047 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002048
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002049#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2050 mutex_init(&hotkey_thread_mutex);
2051 mutex_init(&hotkey_thread_data_mutex);
2052#endif
2053
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002054 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002055 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002056
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002057 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002058 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002059
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002060 if (tp_features.hotkey) {
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002061 hotkey_dev_attributes = create_attr_set(13, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002062 if (!hotkey_dev_attributes)
2063 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002064 res = add_many_to_attr_set(hotkey_dev_attributes,
2065 hotkey_attributes,
2066 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002067 if (res)
2068 return res;
2069
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002070 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002071 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2072 for HKEY interface version 0x100 */
2073 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2074 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002075 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002076 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002077 printk(TPACPI_ERR "please report this to %s\n",
2078 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002079 } else {
2080 /*
2081 * MHKV 0x100 in A31, R40, R40e,
2082 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002083 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002084 tp_features.hotkey_mask = 1;
2085 }
2086 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002087
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002088 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002089 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002090
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002091 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002092 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002093 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002094 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002095 "missing MHKA handler, "
2096 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002097 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002098 /* FN+F12, FN+F4, FN+F3 */
2099 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002100 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002101 }
2102
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002103 /* hotkey_source_mask *must* be zero for
2104 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002105 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002106 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002107 res = hotkey_mask_get();
2108 hotkey_orig_mask = hotkey_mask;
2109 if (!res) {
2110 res = add_many_to_attr_set(
2111 hotkey_dev_attributes,
2112 hotkey_mask_attributes,
2113 ARRAY_SIZE(hotkey_mask_attributes));
2114 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002115 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002116
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002117#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2118 if (tp_features.hotkey_mask) {
2119 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2120 & ~hotkey_all_mask;
2121 } else {
2122 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2123 }
2124
2125 vdbg_printk(TPACPI_DBG_INIT,
2126 "hotkey source mask 0x%08x, polling freq %d\n",
2127 hotkey_source_mask, hotkey_poll_freq);
2128#endif
2129
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002130 /* Not all thinkpads have a hardware radio switch */
2131 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2132 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002133 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002134 "radio switch found; radios are %s\n",
2135 enabled(status, 0));
2136 res = add_to_attr_set(hotkey_dev_attributes,
2137 &dev_attr_hotkey_radio_sw.attr);
2138 }
2139
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002140 /* For X41t, X60t, X61t Tablets... */
2141 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2142 tp_features.hotkey_tablet = 1;
2143 printk(TPACPI_INFO
2144 "possible tablet mode switch found; "
2145 "ThinkPad in %s mode\n",
2146 (status & TP_HOTKEY_TABLET_MASK)?
2147 "tablet" : "laptop");
2148 res = add_to_attr_set(hotkey_dev_attributes,
2149 &dev_attr_hotkey_tablet_mode.attr);
2150 }
2151
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002152 if (!res)
2153 res = register_attr_set_with_sysfs(
2154 hotkey_dev_attributes,
2155 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002156 if (res)
2157 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002158
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002159 /* Set up key map */
2160
2161 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2162 GFP_KERNEL);
2163 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002164 printk(TPACPI_ERR
2165 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002166 return -ENOMEM;
2167 }
2168
2169 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2170 dbg_printk(TPACPI_DBG_INIT,
2171 "using Lenovo default hot key map\n");
2172 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2173 TPACPI_HOTKEY_MAP_SIZE);
2174 } else {
2175 dbg_printk(TPACPI_DBG_INIT,
2176 "using IBM default hot key map\n");
2177 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2178 TPACPI_HOTKEY_MAP_SIZE);
2179 }
2180
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002181 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2182 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2183 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002184 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2185 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2186 tpacpi_inputdev->keycode = hotkey_keycode_map;
2187 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002188 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2189 set_bit(hotkey_keycode_map[i],
2190 tpacpi_inputdev->keybit);
2191 } else {
2192 if (i < sizeof(hotkey_reserved_mask)*8)
2193 hotkey_reserved_mask |= 1 << i;
2194 }
2195 }
2196
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002197 if (tp_features.hotkey_wlsw) {
2198 set_bit(EV_SW, tpacpi_inputdev->evbit);
2199 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2200 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002201 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002202 set_bit(EV_SW, tpacpi_inputdev->evbit);
2203 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2204 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002205
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002206 /* Do not issue duplicate brightness change events to
2207 * userspace */
2208 if (!tp_features.bright_acpimode)
2209 /* update bright_acpimode... */
2210 tpacpi_check_std_acpi_brightness_support();
2211
2212 if (tp_features.bright_acpimode) {
2213 printk(TPACPI_INFO
2214 "This ThinkPad has standard ACPI backlight "
2215 "brightness control, supported by the ACPI "
2216 "video driver\n");
2217 printk(TPACPI_NOTICE
2218 "Disabling thinkpad-acpi brightness events "
2219 "by default...\n");
2220
2221 /* The hotkey_reserved_mask change below is not
2222 * necessary while the keys are at KEY_RESERVED in the
2223 * default map, but better safe than sorry, leave it
2224 * here as a marker of what we have to do, especially
2225 * when we finally become able to set this at runtime
2226 * on response to X.org requests */
2227 hotkey_reserved_mask |=
2228 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2229 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2230 }
2231
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002232 dbg_printk(TPACPI_DBG_INIT,
2233 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002234 res = hotkey_status_set(1);
2235 if (res)
2236 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002237 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2238 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002239 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002240 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002241 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002242
2243 dbg_printk(TPACPI_DBG_INIT,
2244 "legacy hot key reporting over procfs %s\n",
2245 (hotkey_report_mode < 2) ?
2246 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002247
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002248 tpacpi_inputdev->open = &hotkey_inputdev_open;
2249 tpacpi_inputdev->close = &hotkey_inputdev_close;
2250
2251 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002252 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002253 tpacpi_input_send_tabletsw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002254 }
2255
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002256 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002257}
2258
2259static void hotkey_exit(void)
2260{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002261#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2262 hotkey_poll_stop_sync();
2263#endif
2264
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002265 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002266 dbg_printk(TPACPI_DBG_EXIT,
2267 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002268 /* no short-circuit boolean operator below! */
2269 if ((hotkey_mask_set(hotkey_orig_mask) |
2270 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002271 printk(TPACPI_ERR
2272 "failed to restore hot key mask "
2273 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002274 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002275
2276 if (hotkey_dev_attributes) {
2277 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2278 hotkey_dev_attributes = NULL;
2279 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002280}
2281
2282static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2283{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002284 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002285 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002286 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002287 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002288 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002289
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002290 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002291 printk(TPACPI_ERR
2292 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002293 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002294 acpi_bus_generate_netlink_event(
2295 ibm->acpi->device->pnp.device_class,
2296 ibm->acpi->device->dev.bus_id,
2297 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002298 return;
2299 }
2300
2301 while (1) {
2302 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002303 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002304 return;
2305 }
2306
2307 if (hkey == 0) {
2308 /* queue empty */
2309 return;
2310 }
2311
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002312 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002313 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002314 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002315
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002316 switch (hkey >> 12) {
2317 case 1:
2318 /* 0x1000-0x1FFF: key presses */
2319 scancode = hkey & 0xfff;
2320 if (scancode > 0 && scancode < 0x21) {
2321 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002322 if (!(hotkey_source_mask & (1 << scancode))) {
2323 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002324 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002325 } else {
2326 ignore_acpi_ev = 1;
2327 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002328 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002329 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002330 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002331 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002332 case 2:
2333 /* Wakeup reason */
2334 switch (hkey) {
2335 case 0x2304: /* suspend, undock */
2336 case 0x2404: /* hibernation, undock */
2337 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2338 ignore_acpi_ev = 1;
2339 break;
2340 case 0x2305: /* suspend, bay eject */
2341 case 0x2405: /* hibernation, bay eject */
2342 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2343 ignore_acpi_ev = 1;
2344 break;
2345 default:
2346 unk_ev = 1;
2347 }
2348 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2349 printk(TPACPI_INFO
2350 "woke up due to a hot-unplug "
2351 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002352 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002353 }
2354 break;
2355 case 3:
2356 /* bay-related wakeups */
2357 if (hkey == 0x3003) {
2358 hotkey_autosleep_ack = 1;
2359 printk(TPACPI_INFO
2360 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002361 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002362 } else {
2363 unk_ev = 1;
2364 }
2365 break;
2366 case 4:
2367 /* dock-related wakeups */
2368 if (hkey == 0x4003) {
2369 hotkey_autosleep_ack = 1;
2370 printk(TPACPI_INFO
2371 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002372 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002373 } else {
2374 unk_ev = 1;
2375 }
2376 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002377 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002378 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002379 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002380 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002381 case 0x500b: /* X61t: tablet pen inserted into bay */
2382 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002383 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002384 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2385 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2386 tpacpi_input_send_tabletsw();
2387 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002388 send_acpi_ev = 0;
2389 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002390 case 0x5001:
2391 case 0x5002:
2392 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002393 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002394 break;
2395 default:
2396 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002397 }
2398 break;
2399 case 7:
2400 /* 0x7000-0x7FFF: misc */
2401 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2402 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002403 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002404 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002405 break;
2406 }
2407 /* fallthrough to default */
2408 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002409 unk_ev = 1;
2410 }
2411 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002412 printk(TPACPI_NOTICE
2413 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002414 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002415
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002416 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002417 if (!ignore_acpi_ev &&
2418 (send_acpi_ev || hotkey_report_mode < 2)) {
2419 acpi_bus_generate_proc_event(ibm->acpi->device,
2420 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002421 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002422
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002423 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002424 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002425 acpi_bus_generate_netlink_event(
2426 ibm->acpi->device->pnp.device_class,
2427 ibm->acpi->device->dev.bus_id,
2428 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002429 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002430 }
2431}
2432
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002433static void hotkey_suspend(pm_message_t state)
2434{
2435 /* Do these on suspend, we get the events on early resume! */
2436 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2437 hotkey_autosleep_ack = 0;
2438}
2439
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002440static void hotkey_resume(void)
2441{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002442 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002443 printk(TPACPI_ERR
2444 "error while trying to read hot key mask "
2445 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002446 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002447 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002448 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002449 hotkey_wakeup_reason_notify_change();
2450 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002451 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002452}
2453
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002454/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002455static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002457 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 int len = 0;
2459
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002460 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002461 len += sprintf(p + len, "status:\t\tnot supported\n");
2462 return len;
2463 }
2464
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002465 if (mutex_lock_interruptible(&hotkey_mutex))
2466 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002467 res = hotkey_status_get(&status);
2468 if (!res)
2469 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002470 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002471 if (res)
2472 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002475 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002476 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 len += sprintf(p + len,
2478 "commands:\tenable, disable, reset, <mask>\n");
2479 } else {
2480 len += sprintf(p + len, "mask:\t\tnot supported\n");
2481 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2482 }
2483
2484 return len;
2485}
2486
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002487static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002489 int res, status;
2490 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002493 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return -ENODEV;
2495
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002496 if (mutex_lock_interruptible(&hotkey_mutex))
2497 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002498
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002499 status = -1;
2500 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002501
2502 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 while ((cmd = next_cmd(&buf))) {
2504 if (strlencmp(cmd, "enable") == 0) {
2505 status = 1;
2506 } else if (strlencmp(cmd, "disable") == 0) {
2507 status = 0;
2508 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002509 status = hotkey_orig_status;
2510 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2512 /* mask set */
2513 } else if (sscanf(cmd, "%x", &mask) == 1) {
2514 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002515 } else {
2516 res = -EINVAL;
2517 goto errexit;
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002520 if (status != -1)
2521 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002523 if (!res && mask != hotkey_mask)
2524 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002526errexit:
2527 mutex_unlock(&hotkey_mutex);
2528 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002529}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002531static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002532 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002533 {"", 0},
2534};
2535
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002536static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002537 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002538 .notify = hotkey_notify,
2539 .handle = &hkey_handle,
2540 .type = ACPI_DEVICE_NOTIFY,
2541};
2542
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002543static struct ibm_struct hotkey_driver_data = {
2544 .name = "hotkey",
2545 .read = hotkey_read,
2546 .write = hotkey_write,
2547 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002548 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002549 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002550 .acpi = &ibm_hotkey_acpidriver,
2551};
2552
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002553/*************************************************************************
2554 * Bluetooth subdriver
2555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002557enum {
2558 /* ACPI GBDC/SBDC bits */
2559 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2560 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2561 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2562};
2563
2564static int bluetooth_get_radiosw(void);
2565static int bluetooth_set_radiosw(int radio_on);
2566
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002567/* sysfs bluetooth enable ---------------------------------------------- */
2568static ssize_t bluetooth_enable_show(struct device *dev,
2569 struct device_attribute *attr,
2570 char *buf)
2571{
2572 int status;
2573
2574 status = bluetooth_get_radiosw();
2575 if (status < 0)
2576 return status;
2577
2578 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2579}
2580
2581static ssize_t bluetooth_enable_store(struct device *dev,
2582 struct device_attribute *attr,
2583 const char *buf, size_t count)
2584{
2585 unsigned long t;
2586 int res;
2587
2588 if (parse_strtoul(buf, 1, &t))
2589 return -EINVAL;
2590
2591 res = bluetooth_set_radiosw(t);
2592
2593 return (res) ? res : count;
2594}
2595
2596static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002597 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002598 bluetooth_enable_show, bluetooth_enable_store);
2599
2600/* --------------------------------------------------------------------- */
2601
2602static struct attribute *bluetooth_attributes[] = {
2603 &dev_attr_bluetooth_enable.attr,
2604 NULL
2605};
2606
2607static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002608 .attrs = bluetooth_attributes,
2609};
2610
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002611static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002613 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002614 int status = 0;
2615
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002616 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2617
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002618 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002619
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002620 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2621 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002622 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002623 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002625 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2626 str_supported(tp_features.bluetooth),
2627 status);
2628
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002629 if (tp_features.bluetooth) {
2630 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2631 /* no bluetooth hardware present in system */
2632 tp_features.bluetooth = 0;
2633 dbg_printk(TPACPI_DBG_INIT,
2634 "bluetooth hardware not installed\n");
2635 } else {
2636 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2637 &bluetooth_attr_group);
2638 if (res)
2639 return res;
2640 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002641 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002642
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002643 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002646static void bluetooth_exit(void)
2647{
2648 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2649 &bluetooth_attr_group);
2650}
2651
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002652static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 int status;
2655
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002656 if (!tp_features.bluetooth)
2657 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002659 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2660 return -EIO;
2661
2662 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2663}
2664
2665static int bluetooth_set_radiosw(int radio_on)
2666{
2667 int status;
2668
2669 if (!tp_features.bluetooth)
2670 return -ENODEV;
2671
2672 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2673 return -EIO;
2674 if (radio_on)
2675 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2676 else
2677 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2678 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2679 return -EIO;
2680
2681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002684/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002685static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002688 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002690 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002693 len += sprintf(p + len, "status:\t\t%s\n",
2694 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 len += sprintf(p + len, "commands:\tenable, disable\n");
2696 }
2697
2698 return len;
2699}
2700
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002701static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002705 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002706 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 while ((cmd = next_cmd(&buf))) {
2709 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002710 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002712 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 } else
2714 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return 0;
2718}
2719
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002720static struct ibm_struct bluetooth_driver_data = {
2721 .name = "bluetooth",
2722 .read = bluetooth_read,
2723 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002724 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002725};
2726
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002727/*************************************************************************
2728 * Wan subdriver
2729 */
2730
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002731enum {
2732 /* ACPI GWAN/SWAN bits */
2733 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2734 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2735 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2736};
2737
2738static int wan_get_radiosw(void);
2739static int wan_set_radiosw(int radio_on);
2740
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002741/* sysfs wan enable ---------------------------------------------------- */
2742static ssize_t wan_enable_show(struct device *dev,
2743 struct device_attribute *attr,
2744 char *buf)
2745{
2746 int status;
2747
2748 status = wan_get_radiosw();
2749 if (status < 0)
2750 return status;
2751
2752 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2753}
2754
2755static ssize_t wan_enable_store(struct device *dev,
2756 struct device_attribute *attr,
2757 const char *buf, size_t count)
2758{
2759 unsigned long t;
2760 int res;
2761
2762 if (parse_strtoul(buf, 1, &t))
2763 return -EINVAL;
2764
2765 res = wan_set_radiosw(t);
2766
2767 return (res) ? res : count;
2768}
2769
2770static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002771 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002772 wan_enable_show, wan_enable_store);
2773
2774/* --------------------------------------------------------------------- */
2775
2776static struct attribute *wan_attributes[] = {
2777 &dev_attr_wan_enable.attr,
2778 NULL
2779};
2780
2781static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002782 .attrs = wan_attributes,
2783};
2784
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002785static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002786{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002787 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002788 int status = 0;
2789
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002790 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2791
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002792 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002793
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002794 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002795 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002796
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002797 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2798 str_supported(tp_features.wan),
2799 status);
2800
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002801 if (tp_features.wan) {
2802 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2803 /* no wan hardware present in system */
2804 tp_features.wan = 0;
2805 dbg_printk(TPACPI_DBG_INIT,
2806 "wan hardware not installed\n");
2807 } else {
2808 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2809 &wan_attr_group);
2810 if (res)
2811 return res;
2812 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002813 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002814
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002815 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002816}
2817
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002818static void wan_exit(void)
2819{
2820 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2821 &wan_attr_group);
2822}
2823
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002824static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002825{
2826 int status;
2827
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002828 if (!tp_features.wan)
2829 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002830
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002831 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2832 return -EIO;
2833
2834 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2835}
2836
2837static int wan_set_radiosw(int radio_on)
2838{
2839 int status;
2840
2841 if (!tp_features.wan)
2842 return -ENODEV;
2843
2844 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2845 return -EIO;
2846 if (radio_on)
2847 status |= TP_ACPI_WANCARD_RADIOSSW;
2848 else
2849 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2850 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2851 return -EIO;
2852
2853 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002854}
2855
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002856/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002857static int wan_read(char *p)
2858{
2859 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002860 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002861
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002862 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002863 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002864 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002865 len += sprintf(p + len, "status:\t\t%s\n",
2866 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002867 len += sprintf(p + len, "commands:\tenable, disable\n");
2868 }
2869
2870 return len;
2871}
2872
2873static int wan_write(char *buf)
2874{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002875 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002876
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002877 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002878 return -ENODEV;
2879
2880 while ((cmd = next_cmd(&buf))) {
2881 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002882 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002883 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002884 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002885 } else
2886 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002887 }
2888
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002889 return 0;
2890}
2891
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002892static struct ibm_struct wan_driver_data = {
2893 .name = "wan",
2894 .read = wan_read,
2895 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002896 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002897 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002898};
2899
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002900/*************************************************************************
2901 * Video subdriver
2902 */
2903
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002904#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2905
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002906enum video_access_mode {
2907 TPACPI_VIDEO_NONE = 0,
2908 TPACPI_VIDEO_570, /* 570 */
2909 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2910 TPACPI_VIDEO_NEW, /* all others */
2911};
2912
2913enum { /* video status flags, based on VIDEO_570 */
2914 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2915 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2916 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2917};
2918
2919enum { /* TPACPI_VIDEO_570 constants */
2920 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2921 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2922 * video_status_flags */
2923 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2924 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2925};
2926
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002927static enum video_access_mode video_supported;
2928static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002929
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002930static int video_autosw_get(void);
2931static int video_autosw_set(int enable);
2932
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002933TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002934
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002935static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002937 int ivga;
2938
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002939 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2940
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002941 TPACPI_ACPIHANDLE_INIT(vid);
2942 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002943
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002944 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2945 /* G41, assume IVGA doesn't change */
2946 vid_handle = vid2_handle;
2947
2948 if (!vid_handle)
2949 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002950 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002951 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2952 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002953 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002954 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2955 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002956 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002957 else
2958 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002959 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002961 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2962 str_supported(video_supported != TPACPI_VIDEO_NONE),
2963 video_supported);
2964
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002965 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966}
2967
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002968static void video_exit(void)
2969{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002970 dbg_printk(TPACPI_DBG_EXIT,
2971 "restoring original video autoswitch mode\n");
2972 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002973 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002974 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002975}
2976
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002977static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
2979 int status = 0;
2980 int i;
2981
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002982 switch (video_supported) {
2983 case TPACPI_VIDEO_570:
2984 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2985 TP_ACPI_VIDEO_570_PHSCMD))
2986 return -EIO;
2987 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2988 break;
2989 case TPACPI_VIDEO_770:
2990 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2991 return -EIO;
2992 if (i)
2993 status |= TP_ACPI_VIDEO_S_LCD;
2994 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2995 return -EIO;
2996 if (i)
2997 status |= TP_ACPI_VIDEO_S_CRT;
2998 break;
2999 case TPACPI_VIDEO_NEW:
3000 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3001 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3002 return -EIO;
3003 if (i)
3004 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003006 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3007 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3008 return -EIO;
3009 if (i)
3010 status |= TP_ACPI_VIDEO_S_LCD;
3011 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3012 return -EIO;
3013 if (i)
3014 status |= TP_ACPI_VIDEO_S_DVI;
3015 break;
3016 default:
3017 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
3020 return status;
3021}
3022
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003023static int video_outputsw_set(int status)
3024{
3025 int autosw;
3026 int res = 0;
3027
3028 switch (video_supported) {
3029 case TPACPI_VIDEO_570:
3030 res = acpi_evalf(NULL, NULL,
3031 "\\_SB.PHS2", "vdd",
3032 TP_ACPI_VIDEO_570_PHS2CMD,
3033 status | TP_ACPI_VIDEO_570_PHS2SET);
3034 break;
3035 case TPACPI_VIDEO_770:
3036 autosw = video_autosw_get();
3037 if (autosw < 0)
3038 return autosw;
3039
3040 res = video_autosw_set(1);
3041 if (res)
3042 return res;
3043 res = acpi_evalf(vid_handle, NULL,
3044 "ASWT", "vdd", status * 0x100, 0);
3045 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003046 printk(TPACPI_ERR
3047 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003048 return -EIO;
3049 }
3050 break;
3051 case TPACPI_VIDEO_NEW:
3052 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003053 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003054 break;
3055 default:
3056 return -ENOSYS;
3057 }
3058
3059 return (res)? 0 : -EIO;
3060}
3061
3062static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003064 int autosw = 0;
3065
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003066 switch (video_supported) {
3067 case TPACPI_VIDEO_570:
3068 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3069 return -EIO;
3070 break;
3071 case TPACPI_VIDEO_770:
3072 case TPACPI_VIDEO_NEW:
3073 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3074 return -EIO;
3075 break;
3076 default:
3077 return -ENOSYS;
3078 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003079
3080 return autosw & 1;
3081}
3082
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003083static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003084{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003085 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003086 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003087 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003088}
3089
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003090static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003091{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003092 int autosw = video_autosw_get();
3093 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003094
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003095 if (autosw < 0)
3096 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003097
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003098 switch (video_supported) {
3099 case TPACPI_VIDEO_570:
3100 res = video_autosw_set(1);
3101 if (res)
3102 return res;
3103 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3104 break;
3105 case TPACPI_VIDEO_770:
3106 case TPACPI_VIDEO_NEW:
3107 res = video_autosw_set(1);
3108 if (res)
3109 return res;
3110 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3111 break;
3112 default:
3113 return -ENOSYS;
3114 }
3115 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003116 printk(TPACPI_ERR
3117 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003118 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003119 }
3120
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003121 return (res)? 0 : -EIO;
3122}
3123
3124static int video_expand_toggle(void)
3125{
3126 switch (video_supported) {
3127 case TPACPI_VIDEO_570:
3128 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3129 0 : -EIO;
3130 case TPACPI_VIDEO_770:
3131 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3132 0 : -EIO;
3133 case TPACPI_VIDEO_NEW:
3134 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3135 0 : -EIO;
3136 default:
3137 return -ENOSYS;
3138 }
3139 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003140}
3141
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003142static int video_read(char *p)
3143{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003144 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003145 int len = 0;
3146
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003147 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003148 len += sprintf(p + len, "status:\t\tnot supported\n");
3149 return len;
3150 }
3151
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003152 status = video_outputsw_get();
3153 if (status < 0)
3154 return status;
3155
3156 autosw = video_autosw_get();
3157 if (autosw < 0)
3158 return autosw;
3159
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003160 len += sprintf(p + len, "status:\t\tsupported\n");
3161 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3162 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003163 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003164 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3165 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3166 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3167 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003168 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003169 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3170 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3171 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3172
3173 return len;
3174}
3175
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003176static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
3178 char *cmd;
3179 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003180 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003182 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003183 return -ENODEV;
3184
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003185 enable = 0;
3186 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
3188 while ((cmd = next_cmd(&buf))) {
3189 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003190 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003192 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003194 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003196 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003197 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003198 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003199 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003200 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003201 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003202 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003204 res = video_autosw_set(1);
3205 if (res)
3206 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003208 res = video_autosw_set(0);
3209 if (res)
3210 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003212 res = video_outputsw_cycle();
3213 if (res)
3214 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003216 res = video_expand_toggle();
3217 if (res)
3218 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 } else
3220 return -EINVAL;
3221 }
3222
3223 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003224 status = video_outputsw_get();
3225 if (status < 0)
3226 return status;
3227 res = video_outputsw_set((status & ~disable) | enable);
3228 if (res)
3229 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 }
3231
3232 return 0;
3233}
3234
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003235static struct ibm_struct video_driver_data = {
3236 .name = "video",
3237 .read = video_read,
3238 .write = video_write,
3239 .exit = video_exit,
3240};
3241
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003242#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3243
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003244/*************************************************************************
3245 * Light (thinklight) subdriver
3246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003248TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3249TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003250
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003251static int light_get_status(void)
3252{
3253 int status = 0;
3254
3255 if (tp_features.light_status) {
3256 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3257 return -EIO;
3258 return (!!status);
3259 }
3260
3261 return -ENXIO;
3262}
3263
3264static int light_set_status(int status)
3265{
3266 int rc;
3267
3268 if (tp_features.light) {
3269 if (cmos_handle) {
3270 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3271 (status)?
3272 TP_CMOS_THINKLIGHT_ON :
3273 TP_CMOS_THINKLIGHT_OFF);
3274 } else {
3275 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3276 (status)? 1 : 0);
3277 }
3278 return (rc)? 0 : -EIO;
3279 }
3280
3281 return -ENXIO;
3282}
3283
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003284static void light_set_status_worker(struct work_struct *work)
3285{
3286 struct tpacpi_led_classdev *data =
3287 container_of(work, struct tpacpi_led_classdev, work);
3288
3289 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3290 light_set_status((data->new_brightness != LED_OFF));
3291}
3292
3293static void light_sysfs_set(struct led_classdev *led_cdev,
3294 enum led_brightness brightness)
3295{
3296 struct tpacpi_led_classdev *data =
3297 container_of(led_cdev,
3298 struct tpacpi_led_classdev,
3299 led_classdev);
3300 data->new_brightness = brightness;
3301 schedule_work(&data->work);
3302}
3303
3304static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3305{
3306 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3307}
3308
3309static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3310 .led_classdev = {
3311 .name = "tpacpi::thinklight",
3312 .brightness_set = &light_sysfs_set,
3313 .brightness_get = &light_sysfs_get,
3314 }
3315};
3316
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003317static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003319 int rc = 0;
3320
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003321 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3322
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003323 TPACPI_ACPIHANDLE_INIT(ledb);
3324 TPACPI_ACPIHANDLE_INIT(lght);
3325 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003326 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003327
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003328 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003329 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003330
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003331 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003332 /* light status not supported on
3333 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003334 tp_features.light_status =
3335 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003337 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003338 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003339
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003340 if (tp_features.light) {
3341 rc = led_classdev_register(&tpacpi_pdev->dev,
3342 &tpacpi_led_thinklight.led_classdev);
3343 }
3344
3345 if (rc < 0) {
3346 tp_features.light = 0;
3347 tp_features.light_status = 0;
3348 } else {
3349 rc = (tp_features.light)? 0 : 1;
3350 }
3351 return rc;
3352}
3353
3354static void light_exit(void)
3355{
3356 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3357 if (work_pending(&tpacpi_led_thinklight.work))
3358 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003361static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
3363 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003364 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003366 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003367 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003368 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003369 len += sprintf(p + len, "status:\t\tunknown\n");
3370 len += sprintf(p + len, "commands:\ton, off\n");
3371 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003372 status = light_get_status();
3373 if (status < 0)
3374 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003376 len += sprintf(p + len, "commands:\ton, off\n");
3377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
3379 return len;
3380}
3381
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003382static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003385 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003386
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003387 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003388 return -ENODEV;
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 while ((cmd = next_cmd(&buf))) {
3391 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003392 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003394 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 } else
3396 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
3398
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003399 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003402static struct ibm_struct light_driver_data = {
3403 .name = "light",
3404 .read = light_read,
3405 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003406 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003407};
3408
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003409/*************************************************************************
3410 * Dock subdriver
3411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003413#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003414
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003415static void dock_notify(struct ibm_struct *ibm, u32 event);
3416static int dock_read(char *p);
3417static int dock_write(char *buf);
3418
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003419TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003420 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3421 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3422 "\\_SB.PCI.ISA.SLCE", /* 570 */
3423 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3424
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003425/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003426TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003427
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003428static const struct acpi_device_id ibm_pci_device_ids[] = {
3429 {PCI_ROOT_HID_STRING, 0},
3430 {"", 0},
3431};
3432
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003433static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3434 {
3435 .notify = dock_notify,
3436 .handle = &dock_handle,
3437 .type = ACPI_SYSTEM_NOTIFY,
3438 },
3439 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003440 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3441 * We just use it to get notifications of dock hotplug
3442 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003443 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003444 .notify = dock_notify,
3445 .handle = &pci_handle,
3446 .type = ACPI_SYSTEM_NOTIFY,
3447 },
3448};
3449
3450static struct ibm_struct dock_driver_data[2] = {
3451 {
3452 .name = "dock",
3453 .read = dock_read,
3454 .write = dock_write,
3455 .acpi = &ibm_dock_acpidriver[0],
3456 },
3457 {
3458 .name = "dock",
3459 .acpi = &ibm_dock_acpidriver[1],
3460 },
3461};
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463#define dock_docked() (_sta(dock_handle) & 1)
3464
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003465static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003466{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003467 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3468
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003469 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003470
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003471 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3472 str_supported(dock_handle != NULL));
3473
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003474 return (dock_handle)? 0 : 1;
3475}
3476
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003477static int __init dock_init2(struct ibm_init_struct *iibm)
3478{
3479 int dock2_needed;
3480
3481 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3482
3483 if (dock_driver_data[0].flags.acpi_driver_registered &&
3484 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003485 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003486 dock2_needed = (pci_handle != NULL);
3487 vdbg_printk(TPACPI_DBG_INIT,
3488 "dock PCI handler for the TP 570 is %s\n",
3489 str_supported(dock2_needed));
3490 } else {
3491 vdbg_printk(TPACPI_DBG_INIT,
3492 "dock subdriver part 2 not required\n");
3493 dock2_needed = 0;
3494 }
3495
3496 return (dock2_needed)? 0 : 1;
3497}
3498
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003499static void dock_notify(struct ibm_struct *ibm, u32 event)
3500{
3501 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003502 int pci = ibm->acpi->hid && ibm->acpi->device &&
3503 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003504 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003505
3506 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003507 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003508 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003509 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003510 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003511 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003512 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003513 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003514 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003515 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003516 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003517 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003518 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003519 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003520 }
Len Brown14e04fb2007-08-23 15:20:26 -04003521 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003522 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3523 ibm->acpi->device->dev.bus_id,
3524 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003525}
3526
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003527static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
3529 int len = 0;
3530 int docked = dock_docked();
3531
3532 if (!dock_handle)
3533 len += sprintf(p + len, "status:\t\tnot supported\n");
3534 else if (!docked)
3535 len += sprintf(p + len, "status:\t\tundocked\n");
3536 else {
3537 len += sprintf(p + len, "status:\t\tdocked\n");
3538 len += sprintf(p + len, "commands:\tdock, undock\n");
3539 }
3540
3541 return len;
3542}
3543
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003544static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
3546 char *cmd;
3547
3548 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003549 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 while ((cmd = next_cmd(&buf))) {
3552 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003553 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3554 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return -EIO;
3556 } else if (strlencmp(cmd, "dock") == 0) {
3557 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3558 return -EIO;
3559 } else
3560 return -EINVAL;
3561 }
3562
3563 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003564}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003566#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003568/*************************************************************************
3569 * Bay subdriver
3570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003572#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003573
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003574TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003575 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3576 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3577 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3578 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003579TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003580 "_EJ0", /* all others */
3581 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003582TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003583 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3584 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003585TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003586 "_EJ0", /* 770x */
3587 ); /* all others */
3588
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003589static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003591 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3592
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003593 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003594 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003595 TPACPI_ACPIHANDLE_INIT(bay_ej);
3596 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003597 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003598 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003599
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003600 tp_features.bay_status = bay_handle &&
3601 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3602 tp_features.bay_status2 = bay2_handle &&
3603 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003604
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003605 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3606 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3607 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3608 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003610 vdbg_printk(TPACPI_DBG_INIT,
3611 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003612 str_supported(tp_features.bay_status),
3613 str_supported(tp_features.bay_eject),
3614 str_supported(tp_features.bay_status2),
3615 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003616
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003617 return (tp_features.bay_status || tp_features.bay_eject ||
3618 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
3620
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003621static void bay_notify(struct ibm_struct *ibm, u32 event)
3622{
Len Brown14e04fb2007-08-23 15:20:26 -04003623 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003624 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3625 ibm->acpi->device->dev.bus_id,
3626 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003627}
3628
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003629#define bay_occupied(b) (_sta(b##_handle) & 1)
3630
3631static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
3633 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003634 int occupied = bay_occupied(bay);
3635 int occupied2 = bay_occupied(bay2);
3636 int eject, eject2;
3637
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003638 len += sprintf(p + len, "status:\t\t%s\n",
3639 tp_features.bay_status ?
3640 (occupied ? "occupied" : "unoccupied") :
3641 "not supported");
3642 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003643 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3644 "occupied" : "unoccupied");
3645
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003646 eject = tp_features.bay_eject && occupied;
3647 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003648
3649 if (eject && eject2)
3650 len += sprintf(p + len, "commands:\teject, eject2\n");
3651 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003653 else if (eject2)
3654 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
3656 return len;
3657}
3658
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003659static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
3661 char *cmd;
3662
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003663 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003664 return -ENODEV;
3665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003667 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003668 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3669 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003670 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003671 strlencmp(cmd, "eject2") == 0) {
3672 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return -EIO;
3674 } else
3675 return -EINVAL;
3676 }
3677
3678 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003679}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003680
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003681static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3682 .notify = bay_notify,
3683 .handle = &bay_handle,
3684 .type = ACPI_SYSTEM_NOTIFY,
3685};
3686
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003687static struct ibm_struct bay_driver_data = {
3688 .name = "bay",
3689 .read = bay_read,
3690 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003691 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003692};
3693
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003694#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003696/*************************************************************************
3697 * CMOS subdriver
3698 */
3699
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003700/* sysfs cmos_command -------------------------------------------------- */
3701static ssize_t cmos_command_store(struct device *dev,
3702 struct device_attribute *attr,
3703 const char *buf, size_t count)
3704{
3705 unsigned long cmos_cmd;
3706 int res;
3707
3708 if (parse_strtoul(buf, 21, &cmos_cmd))
3709 return -EINVAL;
3710
3711 res = issue_thinkpad_cmos_command(cmos_cmd);
3712 return (res)? res : count;
3713}
3714
3715static struct device_attribute dev_attr_cmos_command =
3716 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3717
3718/* --------------------------------------------------------------------- */
3719
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003720static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003721{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003722 int res;
3723
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003724 vdbg_printk(TPACPI_DBG_INIT,
3725 "initializing cmos commands subdriver\n");
3726
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003727 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003728
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003729 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3730 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003731
3732 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3733 if (res)
3734 return res;
3735
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003736 return (cmos_handle)? 0 : 1;
3737}
3738
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003739static void cmos_exit(void)
3740{
3741 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3742}
3743
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003744static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
3746 int len = 0;
3747
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003748 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3749 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 if (!cmos_handle)
3751 len += sprintf(p + len, "status:\t\tnot supported\n");
3752 else {
3753 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003754 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 }
3756
3757 return len;
3758}
3759
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003760static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
3762 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003763 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003766 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3767 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 /* cmos_cmd set */
3769 } else
3770 return -EINVAL;
3771
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003772 res = issue_thinkpad_cmos_command(cmos_cmd);
3773 if (res)
3774 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 }
3776
3777 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003778}
3779
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003780static struct ibm_struct cmos_driver_data = {
3781 .name = "cmos",
3782 .read = cmos_read,
3783 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003784 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003785};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003786
3787/*************************************************************************
3788 * LED subdriver
3789 */
3790
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003791enum led_access_mode {
3792 TPACPI_LED_NONE = 0,
3793 TPACPI_LED_570, /* 570 */
3794 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3795 TPACPI_LED_NEW, /* all others */
3796};
3797
3798enum { /* For TPACPI_LED_OLD */
3799 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3800 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3801 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3802};
3803
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003804enum led_status_t {
3805 TPACPI_LED_OFF = 0,
3806 TPACPI_LED_ON,
3807 TPACPI_LED_BLINK,
3808};
3809
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003810static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003811
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003812TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003813 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3814 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003815 "LED", /* all others */
3816 ); /* R30, R31 */
3817
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003818#define TPACPI_LED_NUMLEDS 8
3819static struct tpacpi_led_classdev *tpacpi_leds;
3820static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
3821static const char const *tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
3822 /* there's a limit of 19 chars + NULL before 2.6.26 */
3823 "tpacpi::power",
3824 "tpacpi:orange:batt",
3825 "tpacpi:green:batt",
3826 "tpacpi::dock_active",
3827 "tpacpi::bay_active",
3828 "tpacpi::dock_batt",
3829 "tpacpi::unknown_led",
3830 "tpacpi::standby",
3831};
3832
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003833static int led_get_status(unsigned int led)
3834{
3835 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003836 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003837
3838 switch (led_supported) {
3839 case TPACPI_LED_570:
3840 if (!acpi_evalf(ec_handle,
3841 &status, "GLED", "dd", 1 << led))
3842 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003843 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003844 TPACPI_LED_OFF :
3845 ((status == 1)?
3846 TPACPI_LED_ON :
3847 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003848 tpacpi_led_state_cache[led] = led_s;
3849 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003850 default:
3851 return -ENXIO;
3852 }
3853
3854 /* not reached */
3855}
3856
3857static int led_set_status(unsigned int led, enum led_status_t ledstatus)
3858{
3859 /* off, on, blink. Index is led_status_t */
3860 static const int const led_sled_arg1[] = { 0, 1, 3 };
3861 static const int const led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3862 static const int const led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3863 static const int const led_led_arg1[] = { 0, 0x80, 0xc0 };
3864
3865 int rc = 0;
3866
3867 switch (led_supported) {
3868 case TPACPI_LED_570:
3869 /* 570 */
3870 led = 1 << led;
3871 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3872 led, led_sled_arg1[ledstatus]))
3873 rc = -EIO;
3874 break;
3875 case TPACPI_LED_OLD:
3876 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3877 led = 1 << led;
3878 rc = ec_write(TPACPI_LED_EC_HLMS, led);
3879 if (rc >= 0)
3880 rc = ec_write(TPACPI_LED_EC_HLBL,
3881 led * led_exp_hlbl[ledstatus]);
3882 if (rc >= 0)
3883 rc = ec_write(TPACPI_LED_EC_HLCL,
3884 led * led_exp_hlcl[ledstatus]);
3885 break;
3886 case TPACPI_LED_NEW:
3887 /* all others */
3888 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3889 led, led_led_arg1[ledstatus]))
3890 rc = -EIO;
3891 break;
3892 default:
3893 rc = -ENXIO;
3894 }
3895
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003896 if (!rc)
3897 tpacpi_led_state_cache[led] = ledstatus;
3898
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003899 return rc;
3900}
3901
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003902static void led_sysfs_set_status(unsigned int led,
3903 enum led_brightness brightness)
3904{
3905 led_set_status(led,
3906 (brightness == LED_OFF) ?
3907 TPACPI_LED_OFF :
3908 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
3909 TPACPI_LED_BLINK : TPACPI_LED_ON);
3910}
3911
3912static void led_set_status_worker(struct work_struct *work)
3913{
3914 struct tpacpi_led_classdev *data =
3915 container_of(work, struct tpacpi_led_classdev, work);
3916
3917 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3918 led_sysfs_set_status(data->led, data->new_brightness);
3919}
3920
3921static void led_sysfs_set(struct led_classdev *led_cdev,
3922 enum led_brightness brightness)
3923{
3924 struct tpacpi_led_classdev *data = container_of(led_cdev,
3925 struct tpacpi_led_classdev, led_classdev);
3926
3927 data->new_brightness = brightness;
3928 schedule_work(&data->work);
3929}
3930
3931static int led_sysfs_blink_set(struct led_classdev *led_cdev,
3932 unsigned long *delay_on, unsigned long *delay_off)
3933{
3934 struct tpacpi_led_classdev *data = container_of(led_cdev,
3935 struct tpacpi_led_classdev, led_classdev);
3936
3937 /* Can we choose the flash rate? */
3938 if (*delay_on == 0 && *delay_off == 0) {
3939 /* yes. set them to the hardware blink rate (1 Hz) */
3940 *delay_on = 500; /* ms */
3941 *delay_off = 500; /* ms */
3942 } else if ((*delay_on != 500) || (*delay_off != 500))
3943 return -EINVAL;
3944
3945 data->new_brightness = TPACPI_LED_BLINK;
3946 schedule_work(&data->work);
3947
3948 return 0;
3949}
3950
3951static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
3952{
3953 int rc;
3954
3955 struct tpacpi_led_classdev *data = container_of(led_cdev,
3956 struct tpacpi_led_classdev, led_classdev);
3957
3958 rc = led_get_status(data->led);
3959
3960 if (rc == TPACPI_LED_OFF || rc < 0)
3961 rc = LED_OFF; /* no error handling in led class :( */
3962 else
3963 rc = LED_FULL;
3964
3965 return rc;
3966}
3967
3968static void led_exit(void)
3969{
3970 unsigned int i;
3971
3972 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
3973 if (tpacpi_leds[i].led_classdev.name)
3974 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
3975 }
3976
3977 kfree(tpacpi_leds);
3978 tpacpi_leds = NULL;
3979}
3980
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003981static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003982{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003983 unsigned int i;
3984 int rc;
3985
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003986 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3987
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003988 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003989
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003990 if (!led_handle)
3991 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003992 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003993 else if (strlencmp(led_path, "SLED") == 0)
3994 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003995 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003996 else if (strlencmp(led_path, "SYSL") == 0)
3997 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003998 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003999 else
4000 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004001 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004002
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004003 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4004 str_supported(led_supported), led_supported);
4005
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004006 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4007 GFP_KERNEL);
4008 if (!tpacpi_leds) {
4009 printk(TPACPI_ERR "Out of memory for LED data\n");
4010 return -ENOMEM;
4011 }
4012
4013 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4014 tpacpi_leds[i].led = i;
4015
4016 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4017 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4018 if (led_supported == TPACPI_LED_570)
4019 tpacpi_leds[i].led_classdev.brightness_get =
4020 &led_sysfs_get;
4021
4022 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4023
4024 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4025
4026 rc = led_classdev_register(&tpacpi_pdev->dev,
4027 &tpacpi_leds[i].led_classdev);
4028 if (rc < 0) {
4029 tpacpi_leds[i].led_classdev.name = NULL;
4030 led_exit();
4031 return rc;
4032 }
4033 }
4034
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004035 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004036}
4037
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004038#define str_led_status(s) \
4039 ((s) == TPACPI_LED_OFF ? "off" : \
4040 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004041
4042static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
4044 int len = 0;
4045
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004046 if (!led_supported) {
4047 len += sprintf(p + len, "status:\t\tnot supported\n");
4048 return len;
4049 }
4050 len += sprintf(p + len, "status:\t\tsupported\n");
4051
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004052 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004053 /* 570 */
4054 int i, status;
4055 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004056 status = led_get_status(i);
4057 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004058 return -EIO;
4059 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004060 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004061 }
4062 }
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004065 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
4067 return len;
4068}
4069
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004070static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
4072 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004073 int led, rc;
4074 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004075
4076 if (!led_supported)
4077 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004080 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 return -EINVAL;
4082
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004083 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004084 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004086 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004087 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004088 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004089 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004090 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004091 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004092
4093 rc = led_set_status(led, s);
4094 if (rc < 0)
4095 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 }
4097
4098 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004099}
4100
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004101static struct ibm_struct led_driver_data = {
4102 .name = "led",
4103 .read = led_read,
4104 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004105 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004106};
4107
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004108/*************************************************************************
4109 * Beep subdriver
4110 */
4111
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004112TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004113
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004114static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004115{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004116 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4117
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004118 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004119
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004120 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4121 str_supported(beep_handle != NULL));
4122
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004123 return (beep_handle)? 0 : 1;
4124}
4125
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004126static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
4128 int len = 0;
4129
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004130 if (!beep_handle)
4131 len += sprintf(p + len, "status:\t\tnot supported\n");
4132 else {
4133 len += sprintf(p + len, "status:\t\tsupported\n");
4134 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137 return len;
4138}
4139
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004140static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
4142 char *cmd;
4143 int beep_cmd;
4144
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004145 if (!beep_handle)
4146 return -ENODEV;
4147
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004149 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4150 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 /* beep_cmd set */
4152 } else
4153 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004154 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 return -EIO;
4156 }
4157
4158 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004159}
4160
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004161static struct ibm_struct beep_driver_data = {
4162 .name = "beep",
4163 .read = beep_read,
4164 .write = beep_write,
4165};
4166
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004167/*************************************************************************
4168 * Thermal subdriver
4169 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004170
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004171enum thermal_access_mode {
4172 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4173 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4174 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4175 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4176 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4177};
4178
4179enum { /* TPACPI_THERMAL_TPEC_* */
4180 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4181 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4182 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4183};
4184
4185#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4186struct ibm_thermal_sensors_struct {
4187 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4188};
4189
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004190static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004191
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004192/* idx is zero-based */
4193static int thermal_get_sensor(int idx, s32 *value)
4194{
4195 int t;
4196 s8 tmp;
4197 char tmpi[5];
4198
4199 t = TP_EC_THERMAL_TMP0;
4200
4201 switch (thermal_read_mode) {
4202#if TPACPI_MAX_THERMAL_SENSORS >= 16
4203 case TPACPI_THERMAL_TPEC_16:
4204 if (idx >= 8 && idx <= 15) {
4205 t = TP_EC_THERMAL_TMP8;
4206 idx -= 8;
4207 }
4208 /* fallthrough */
4209#endif
4210 case TPACPI_THERMAL_TPEC_8:
4211 if (idx <= 7) {
4212 if (!acpi_ec_read(t + idx, &tmp))
4213 return -EIO;
4214 *value = tmp * 1000;
4215 return 0;
4216 }
4217 break;
4218
4219 case TPACPI_THERMAL_ACPI_UPDT:
4220 if (idx <= 7) {
4221 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4222 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4223 return -EIO;
4224 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4225 return -EIO;
4226 *value = (t - 2732) * 100;
4227 return 0;
4228 }
4229 break;
4230
4231 case TPACPI_THERMAL_ACPI_TMP07:
4232 if (idx <= 7) {
4233 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4234 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4235 return -EIO;
4236 if (t > 127 || t < -127)
4237 t = TP_EC_THERMAL_TMP_NA;
4238 *value = t * 1000;
4239 return 0;
4240 }
4241 break;
4242
4243 case TPACPI_THERMAL_NONE:
4244 default:
4245 return -ENOSYS;
4246 }
4247
4248 return -EINVAL;
4249}
4250
4251static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4252{
4253 int res, i;
4254 int n;
4255
4256 n = 8;
4257 i = 0;
4258
4259 if (!s)
4260 return -EINVAL;
4261
4262 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4263 n = 16;
4264
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004265 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004266 res = thermal_get_sensor(i, &s->temp[i]);
4267 if (res)
4268 return res;
4269 }
4270
4271 return n;
4272}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004273
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004274/* sysfs temp##_input -------------------------------------------------- */
4275
4276static ssize_t thermal_temp_input_show(struct device *dev,
4277 struct device_attribute *attr,
4278 char *buf)
4279{
4280 struct sensor_device_attribute *sensor_attr =
4281 to_sensor_dev_attr(attr);
4282 int idx = sensor_attr->index;
4283 s32 value;
4284 int res;
4285
4286 res = thermal_get_sensor(idx, &value);
4287 if (res)
4288 return res;
4289 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4290 return -ENXIO;
4291
4292 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4293}
4294
4295#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004296 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4297 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004298
4299static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4300 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4301 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4302 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4303 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4304 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4305 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4306 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4307 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4308 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4309 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4310 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4311 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4312 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4313 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4314 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4315 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4316};
4317
4318#define THERMAL_ATTRS(X) \
4319 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4320
4321static struct attribute *thermal_temp_input_attr[] = {
4322 THERMAL_ATTRS(8),
4323 THERMAL_ATTRS(9),
4324 THERMAL_ATTRS(10),
4325 THERMAL_ATTRS(11),
4326 THERMAL_ATTRS(12),
4327 THERMAL_ATTRS(13),
4328 THERMAL_ATTRS(14),
4329 THERMAL_ATTRS(15),
4330 THERMAL_ATTRS(0),
4331 THERMAL_ATTRS(1),
4332 THERMAL_ATTRS(2),
4333 THERMAL_ATTRS(3),
4334 THERMAL_ATTRS(4),
4335 THERMAL_ATTRS(5),
4336 THERMAL_ATTRS(6),
4337 THERMAL_ATTRS(7),
4338 NULL
4339};
4340
4341static const struct attribute_group thermal_temp_input16_group = {
4342 .attrs = thermal_temp_input_attr
4343};
4344
4345static const struct attribute_group thermal_temp_input8_group = {
4346 .attrs = &thermal_temp_input_attr[8]
4347};
4348
4349#undef THERMAL_SENSOR_ATTR_TEMP
4350#undef THERMAL_ATTRS
4351
4352/* --------------------------------------------------------------------- */
4353
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004354static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004355{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004356 u8 t, ta1, ta2;
4357 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004358 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004359 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004360
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004361 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4362
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004363 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004364
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004365 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004366 /*
4367 * Direct EC access mode: sensors at registers
4368 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4369 * non-implemented, thermal sensors return 0x80 when
4370 * not available
4371 */
4372
4373 ta1 = ta2 = 0;
4374 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004375 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004376 ta1 |= t;
4377 } else {
4378 ta1 = 0;
4379 break;
4380 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004381 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004382 ta2 |= t;
4383 } else {
4384 ta1 = 0;
4385 break;
4386 }
4387 }
4388 if (ta1 == 0) {
4389 /* This is sheer paranoia, but we handle it anyway */
4390 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004391 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004392 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004393 "falling back to ACPI TMPx access "
4394 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004395 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004396 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004397 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004398 "ThinkPad ACPI EC access misbehaving, "
4399 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004400 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004401 }
4402 } else {
4403 thermal_read_mode =
4404 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004405 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004406 }
4407 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004408 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4409 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004410 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004411 } else {
4412 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004413 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004414 }
4415 } else {
4416 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004417 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004418 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004419
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004420 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4421 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4422 thermal_read_mode);
4423
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004424 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004425 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004426 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004427 &thermal_temp_input16_group);
4428 if (res)
4429 return res;
4430 break;
4431 case TPACPI_THERMAL_TPEC_8:
4432 case TPACPI_THERMAL_ACPI_TMP07:
4433 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004434 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004435 &thermal_temp_input8_group);
4436 if (res)
4437 return res;
4438 break;
4439 case TPACPI_THERMAL_NONE:
4440 default:
4441 return 1;
4442 }
4443
4444 return 0;
4445}
4446
4447static void thermal_exit(void)
4448{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004449 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004450 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004451 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004452 &thermal_temp_input16_group);
4453 break;
4454 case TPACPI_THERMAL_TPEC_8:
4455 case TPACPI_THERMAL_ACPI_TMP07:
4456 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004457 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004458 &thermal_temp_input16_group);
4459 break;
4460 case TPACPI_THERMAL_NONE:
4461 default:
4462 break;
4463 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004464}
4465
4466static int thermal_read(char *p)
4467{
4468 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004469 int n, i;
4470 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004471
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004472 n = thermal_get_sensors(&t);
4473 if (unlikely(n < 0))
4474 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004475
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004476 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004477
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004478 if (n > 0) {
4479 for (i = 0; i < (n - 1); i++)
4480 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4481 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4482 } else
4483 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004484
4485 return len;
4486}
4487
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004488static struct ibm_struct thermal_driver_data = {
4489 .name = "thermal",
4490 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004491 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004492};
4493
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004494/*************************************************************************
4495 * EC Dump subdriver
4496 */
4497
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004498static u8 ecdump_regs[256];
4499
4500static int ecdump_read(char *p)
4501{
4502 int len = 0;
4503 int i, j;
4504 u8 v;
4505
4506 len += sprintf(p + len, "EC "
4507 " +00 +01 +02 +03 +04 +05 +06 +07"
4508 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4509 for (i = 0; i < 256; i += 16) {
4510 len += sprintf(p + len, "EC 0x%02x:", i);
4511 for (j = 0; j < 16; j++) {
4512 if (!acpi_ec_read(i + j, &v))
4513 break;
4514 if (v != ecdump_regs[i + j])
4515 len += sprintf(p + len, " *%02x", v);
4516 else
4517 len += sprintf(p + len, " %02x", v);
4518 ecdump_regs[i + j] = v;
4519 }
4520 len += sprintf(p + len, "\n");
4521 if (j != 16)
4522 break;
4523 }
4524
4525 /* These are way too dangerous to advertise openly... */
4526#if 0
4527 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4528 " (<offset> is 00-ff, <value> is 00-ff)\n");
4529 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4530 " (<offset> is 00-ff, <value> is 0-255)\n");
4531#endif
4532 return len;
4533}
4534
4535static int ecdump_write(char *buf)
4536{
4537 char *cmd;
4538 int i, v;
4539
4540 while ((cmd = next_cmd(&buf))) {
4541 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4542 /* i and v set */
4543 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4544 /* i and v set */
4545 } else
4546 return -EINVAL;
4547 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4548 if (!acpi_ec_write(i, v))
4549 return -EIO;
4550 } else
4551 return -EINVAL;
4552 }
4553
4554 return 0;
4555}
4556
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004557static struct ibm_struct ecdump_driver_data = {
4558 .name = "ecdump",
4559 .read = ecdump_read,
4560 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004561 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004562};
4563
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004564/*************************************************************************
4565 * Backlight/brightness subdriver
4566 */
Holger Macht8acb0252006-10-20 14:30:28 -07004567
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004568#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4569
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004570enum {
4571 TP_EC_BACKLIGHT = 0x31,
4572
4573 /* TP_EC_BACKLIGHT bitmasks */
4574 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4575 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4576 TP_EC_BACKLIGHT_MAPSW = 0x20,
4577};
4578
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004579static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004580static int brightness_mode;
4581static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4582
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004583static struct mutex brightness_mutex;
4584
4585/*
4586 * ThinkPads can read brightness from two places: EC 0x31, or
4587 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004588 *
4589 * EC 0x31 has the following layout
4590 * Bit 7: unknown function
4591 * Bit 6: unknown function
4592 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4593 * Bit 4: must be set to zero to avoid problems
4594 * Bit 3-0: backlight brightness level
4595 *
4596 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004597 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004598static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004599{
4600 u8 lec = 0, lcmos = 0, level = 0;
4601
4602 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004603 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004604 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004605 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004606 };
4607 if (brightness_mode & 2) {
4608 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4609 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4610 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4611 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4612 level = lcmos;
4613 }
4614
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004615 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004616 *status = lec; /* Prefer EC, CMOS is just a backing store */
4617 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004618 if (lec == lcmos)
4619 tp_warned.bright_cmos_ec_unsync = 0;
4620 else {
4621 if (!tp_warned.bright_cmos_ec_unsync) {
4622 printk(TPACPI_ERR
4623 "CMOS NVRAM (%u) and EC (%u) do not "
4624 "agree on display brightness level\n",
4625 (unsigned int) lcmos,
4626 (unsigned int) lec);
4627 tp_warned.bright_cmos_ec_unsync = 1;
4628 }
4629 return -EIO;
4630 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004631 } else {
4632 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004633 }
4634
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004635 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004636}
4637
4638/* May return EINTR which can always be mapped to ERESTARTSYS */
4639static int brightness_set(int value)
4640{
4641 int cmos_cmd, inc, i, res;
4642 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004643 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004644
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004645 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4646 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004647 return -EINVAL;
4648
4649 res = mutex_lock_interruptible(&brightness_mutex);
4650 if (res < 0)
4651 return res;
4652
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004653 res = brightness_get_raw(&current_value);
4654 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004655 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004656
4657 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4658 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004659
4660 cmos_cmd = value > current_value ?
4661 TP_CMOS_BRIGHTNESS_UP :
4662 TP_CMOS_BRIGHTNESS_DOWN;
4663 inc = (value > current_value)? 1 : -1;
4664
4665 res = 0;
4666 for (i = current_value; i != value; i += inc) {
4667 if ((brightness_mode & 2) &&
4668 issue_thinkpad_cmos_command(cmos_cmd)) {
4669 res = -EIO;
4670 goto errout;
4671 }
4672 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004673 !acpi_ec_write(TP_EC_BACKLIGHT,
4674 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004675 res = -EIO;
4676 goto errout;;
4677 }
4678 }
4679
4680errout:
4681 mutex_unlock(&brightness_mutex);
4682 return res;
4683}
4684
4685/* sysfs backlight class ----------------------------------------------- */
4686
4687static int brightness_update_status(struct backlight_device *bd)
4688{
4689 /* it is the backlight class's job (caller) to handle
4690 * EINTR and other errors properly */
4691 return brightness_set(
4692 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4693 bd->props.power == FB_BLANK_UNBLANK) ?
4694 bd->props.brightness : 0);
4695}
Holger Macht8acb0252006-10-20 14:30:28 -07004696
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004697static int brightness_get(struct backlight_device *bd)
4698{
4699 int status, res;
4700
4701 res = brightness_get_raw(&status);
4702 if (res < 0)
4703 return 0; /* FIXME: teach backlight about error handling */
4704
4705 return status & TP_EC_BACKLIGHT_LVLMSK;
4706}
4707
Richard Purdie599a52d2007-02-10 23:07:48 +00004708static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004709 .get_brightness = brightness_get,
4710 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004711};
4712
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004713/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004714
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004715static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004716{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004717 int b;
4718
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004719 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4720
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004721 mutex_init(&brightness_mutex);
4722
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004723 /*
4724 * We always attempt to detect acpi support, so as to switch
4725 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4726 * going to publish a backlight interface
4727 */
4728 b = tpacpi_check_std_acpi_brightness_support();
4729 if (b > 0) {
4730 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4731 printk(TPACPI_NOTICE
4732 "Lenovo BIOS switched to ACPI backlight "
4733 "control mode\n");
4734 }
4735 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004736 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004737 "standard ACPI backlight interface "
4738 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004739 return 1;
4740 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004741 }
4742
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004743 if (!brightness_enable) {
4744 dbg_printk(TPACPI_DBG_INIT,
4745 "brightness support disabled by "
4746 "module parameter\n");
4747 return 1;
4748 }
4749
4750 if (b > 16) {
4751 printk(TPACPI_ERR
4752 "Unsupported brightness interface, "
4753 "please contact %s\n", TPACPI_MAIL);
4754 return 1;
4755 }
4756 if (b == 16)
4757 tp_features.bright_16levels = 1;
4758
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004759 if (!brightness_mode) {
4760 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4761 brightness_mode = 2;
4762 else
4763 brightness_mode = 3;
4764
4765 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4766 brightness_mode);
4767 }
4768
4769 if (brightness_mode > 3)
4770 return -EINVAL;
4771
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004772 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004773 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004774
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004775 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004776 printk(TPACPI_INFO
4777 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004778
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004779 ibm_backlight_device = backlight_device_register(
4780 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4781 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004782 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004783 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004784 return PTR_ERR(ibm_backlight_device);
4785 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004786 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004787
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004788 ibm_backlight_device->props.max_brightness =
4789 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004790 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004791 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004792
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004793 return 0;
4794}
4795
4796static void brightness_exit(void)
4797{
4798 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004799 vdbg_printk(TPACPI_DBG_EXIT,
4800 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004801 backlight_device_unregister(ibm_backlight_device);
4802 ibm_backlight_device = NULL;
4803 }
4804}
4805
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004806static int brightness_read(char *p)
4807{
4808 int len = 0;
4809 int level;
4810
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004811 level = brightness_get(NULL);
4812 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004813 len += sprintf(p + len, "level:\t\tunreadable\n");
4814 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004815 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004816 len += sprintf(p + len, "commands:\tup, down\n");
4817 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004818 " (<level> is 0-%d)\n",
4819 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004820 }
4821
4822 return len;
4823}
4824
4825static int brightness_write(char *buf)
4826{
4827 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004828 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004829 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004830 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004831
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004832 level = brightness_get(NULL);
4833 if (level < 0)
4834 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004835
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004836 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004837 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004838 if (level < max_level)
4839 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004840 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004841 if (level > 0)
4842 level--;
4843 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4844 level >= 0 && level <= max_level) {
4845 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004846 } else
4847 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004848 }
4849
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004850 /*
4851 * Now we know what the final level should be, so we try to set it.
4852 * Doing it this way makes the syscall restartable in case of EINTR
4853 */
4854 rc = brightness_set(level);
4855 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004856}
4857
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004858static struct ibm_struct brightness_driver_data = {
4859 .name = "brightness",
4860 .read = brightness_read,
4861 .write = brightness_write,
4862 .exit = brightness_exit,
4863};
4864
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004865/*************************************************************************
4866 * Volume subdriver
4867 */
4868
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004869static int volume_offset = 0x30;
4870
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004871static int volume_read(char *p)
4872{
4873 int len = 0;
4874 u8 level;
4875
4876 if (!acpi_ec_read(volume_offset, &level)) {
4877 len += sprintf(p + len, "level:\t\tunreadable\n");
4878 } else {
4879 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4880 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4881 len += sprintf(p + len, "commands:\tup, down, mute\n");
4882 len += sprintf(p + len, "commands:\tlevel <level>"
4883 " (<level> is 0-15)\n");
4884 }
4885
4886 return len;
4887}
4888
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004889static int volume_write(char *buf)
4890{
4891 int cmos_cmd, inc, i;
4892 u8 level, mute;
4893 int new_level, new_mute;
4894 char *cmd;
4895
4896 while ((cmd = next_cmd(&buf))) {
4897 if (!acpi_ec_read(volume_offset, &level))
4898 return -EIO;
4899 new_mute = mute = level & 0x40;
4900 new_level = level = level & 0xf;
4901
4902 if (strlencmp(cmd, "up") == 0) {
4903 if (mute)
4904 new_mute = 0;
4905 else
4906 new_level = level == 15 ? 15 : level + 1;
4907 } else if (strlencmp(cmd, "down") == 0) {
4908 if (mute)
4909 new_mute = 0;
4910 else
4911 new_level = level == 0 ? 0 : level - 1;
4912 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4913 new_level >= 0 && new_level <= 15) {
4914 /* new_level set */
4915 } else if (strlencmp(cmd, "mute") == 0) {
4916 new_mute = 0x40;
4917 } else
4918 return -EINVAL;
4919
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004920 if (new_level != level) {
4921 /* mute doesn't change */
4922
4923 cmos_cmd = (new_level > level) ?
4924 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004925 inc = new_level > level ? 1 : -1;
4926
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004927 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004928 !acpi_ec_write(volume_offset, level)))
4929 return -EIO;
4930
4931 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004932 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004933 !acpi_ec_write(volume_offset, i + inc))
4934 return -EIO;
4935
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004936 if (mute &&
4937 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4938 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004939 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004940 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004941 }
4942
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004943 if (new_mute != mute) {
4944 /* level doesn't change */
4945
4946 cmos_cmd = (new_mute) ?
4947 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004948
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004949 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004950 !acpi_ec_write(volume_offset, level + new_mute))
4951 return -EIO;
4952 }
4953 }
4954
4955 return 0;
4956}
4957
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004958static struct ibm_struct volume_driver_data = {
4959 .name = "volume",
4960 .read = volume_read,
4961 .write = volume_write,
4962};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004963
4964/*************************************************************************
4965 * Fan subdriver
4966 */
4967
4968/*
4969 * FAN ACCESS MODES
4970 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004971 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004972 * ACPI GFAN method: returns fan level
4973 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004974 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004975 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004976 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004977 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004978 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4979 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004980 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4981 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004982 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004983 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004984 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4985 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004986 *
4987 * Fan speed changes of any sort (including those caused by the
4988 * disengaged mode) are usually done slowly by the firmware as the
4989 * maximum ammount of fan duty cycle change per second seems to be
4990 * limited.
4991 *
4992 * Reading is not available if GFAN exists.
4993 * Writing is not available if SFAN exists.
4994 *
4995 * Bits
4996 * 7 automatic mode engaged;
4997 * (default operation mode of the ThinkPad)
4998 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004999 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005000 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005001 * the tachometer while the fan controller ramps up
5002 * the speed (which can take up to a few *minutes*).
5003 * Speeds up fan to 100% duty-cycle, which is far above
5004 * the standard RPM levels. It is not impossible that
5005 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005006 * 5-3 unused in some models. Extra bits for fan level
5007 * in others, but still useless as all values above
5008 * 7 map to the same speed as level 7 in these models.
5009 * 2-0 fan level (0..7 usually)
5010 * 0x00 = stop
5011 * 0x07 = max (set when temperatures critical)
5012 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005013 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005014 *
5015 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5016 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5017 * does so, its initial value is meaningless (0x07).
5018 *
5019 * For firmware bugs, refer to:
5020 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5021 *
5022 * ----
5023 *
5024 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5025 * Main fan tachometer reading (in RPM)
5026 *
5027 * This register is present on all ThinkPads with a new-style EC, and
5028 * it is known not to be present on the A21m/e, and T22, as there is
5029 * something else in offset 0x84 according to the ACPI DSDT. Other
5030 * ThinkPads from this same time period (and earlier) probably lack the
5031 * tachometer as well.
5032 *
5033 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5034 * was never fixed by IBM to report the EC firmware version string
5035 * probably support the tachometer (like the early X models), so
5036 * detecting it is quite hard. We need more data to know for sure.
5037 *
5038 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5039 * might result.
5040 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005041 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5042 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005043 *
5044 * For firmware bugs, refer to:
5045 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5046 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005047 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005048 * ThinkPad X31, X40, X41. Not available in the X60.
5049 *
5050 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5051 * high speed. ACPI DSDT seems to map these three speeds to levels
5052 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5053 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5054 *
5055 * The speeds are stored on handles
5056 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5057 *
5058 * There are three default speed sets, acessible as handles:
5059 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5060 *
5061 * ACPI DSDT switches which set is in use depending on various
5062 * factors.
5063 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005064 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005065 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5066 * but the ACPI tables just mention level 7.
5067 */
5068
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005069enum { /* Fan control constants */
5070 fan_status_offset = 0x2f, /* EC register 0x2f */
5071 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5072 * 0x84 must be read before 0x85 */
5073
5074 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5075 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5076
5077 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5078};
5079
5080enum fan_status_access_mode {
5081 TPACPI_FAN_NONE = 0, /* No fan status or control */
5082 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5083 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5084};
5085
5086enum fan_control_access_mode {
5087 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5088 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5089 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5090 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5091};
5092
5093enum fan_control_commands {
5094 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5095 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5096 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5097 * and also watchdog cmd */
5098};
5099
5100static int fan_control_allowed;
5101
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005102static enum fan_status_access_mode fan_status_access_mode;
5103static enum fan_control_access_mode fan_control_access_mode;
5104static enum fan_control_commands fan_control_commands;
5105
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005106static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005107static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005108static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005109
5110static struct mutex fan_mutex;
5111
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005112static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005113static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005114
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005115TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5116TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005117 "\\FSPD", /* 600e/x, 770e, 770x */
5118 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005119TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005120 "JFNS", /* 770x-JL */
5121 ); /* all others */
5122
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005123/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005124 * Call with fan_mutex held
5125 */
5126static void fan_update_desired_level(u8 status)
5127{
5128 if ((status &
5129 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5130 if (status > 7)
5131 fan_control_desired_level = 7;
5132 else
5133 fan_control_desired_level = status;
5134 }
5135}
5136
5137static int fan_get_status(u8 *status)
5138{
5139 u8 s;
5140
5141 /* TODO:
5142 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5143
5144 switch (fan_status_access_mode) {
5145 case TPACPI_FAN_RD_ACPI_GFAN:
5146 /* 570, 600e/x, 770e, 770x */
5147
5148 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5149 return -EIO;
5150
5151 if (likely(status))
5152 *status = s & 0x07;
5153
5154 break;
5155
5156 case TPACPI_FAN_RD_TPEC:
5157 /* all except 570, 600e/x, 770e, 770x */
5158 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5159 return -EIO;
5160
5161 if (likely(status))
5162 *status = s;
5163
5164 break;
5165
5166 default:
5167 return -ENXIO;
5168 }
5169
5170 return 0;
5171}
5172
5173static int fan_get_status_safe(u8 *status)
5174{
5175 int rc;
5176 u8 s;
5177
5178 if (mutex_lock_interruptible(&fan_mutex))
5179 return -ERESTARTSYS;
5180 rc = fan_get_status(&s);
5181 if (!rc)
5182 fan_update_desired_level(s);
5183 mutex_unlock(&fan_mutex);
5184
5185 if (status)
5186 *status = s;
5187
5188 return rc;
5189}
5190
5191static int fan_get_speed(unsigned int *speed)
5192{
5193 u8 hi, lo;
5194
5195 switch (fan_status_access_mode) {
5196 case TPACPI_FAN_RD_TPEC:
5197 /* all except 570, 600e/x, 770e, 770x */
5198 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5199 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5200 return -EIO;
5201
5202 if (likely(speed))
5203 *speed = (hi << 8) | lo;
5204
5205 break;
5206
5207 default:
5208 return -ENXIO;
5209 }
5210
5211 return 0;
5212}
5213
5214static int fan_set_level(int level)
5215{
5216 if (!fan_control_allowed)
5217 return -EPERM;
5218
5219 switch (fan_control_access_mode) {
5220 case TPACPI_FAN_WR_ACPI_SFAN:
5221 if (level >= 0 && level <= 7) {
5222 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5223 return -EIO;
5224 } else
5225 return -EINVAL;
5226 break;
5227
5228 case TPACPI_FAN_WR_ACPI_FANS:
5229 case TPACPI_FAN_WR_TPEC:
5230 if ((level != TP_EC_FAN_AUTO) &&
5231 (level != TP_EC_FAN_FULLSPEED) &&
5232 ((level < 0) || (level > 7)))
5233 return -EINVAL;
5234
5235 /* safety net should the EC not support AUTO
5236 * or FULLSPEED mode bits and just ignore them */
5237 if (level & TP_EC_FAN_FULLSPEED)
5238 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005239 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005240 level |= 4; /* safety min speed 4 */
5241
5242 if (!acpi_ec_write(fan_status_offset, level))
5243 return -EIO;
5244 else
5245 tp_features.fan_ctrl_status_undef = 0;
5246 break;
5247
5248 default:
5249 return -ENXIO;
5250 }
5251 return 0;
5252}
5253
5254static int fan_set_level_safe(int level)
5255{
5256 int rc;
5257
5258 if (!fan_control_allowed)
5259 return -EPERM;
5260
5261 if (mutex_lock_interruptible(&fan_mutex))
5262 return -ERESTARTSYS;
5263
5264 if (level == TPACPI_FAN_LAST_LEVEL)
5265 level = fan_control_desired_level;
5266
5267 rc = fan_set_level(level);
5268 if (!rc)
5269 fan_update_desired_level(level);
5270
5271 mutex_unlock(&fan_mutex);
5272 return rc;
5273}
5274
5275static int fan_set_enable(void)
5276{
5277 u8 s;
5278 int rc;
5279
5280 if (!fan_control_allowed)
5281 return -EPERM;
5282
5283 if (mutex_lock_interruptible(&fan_mutex))
5284 return -ERESTARTSYS;
5285
5286 switch (fan_control_access_mode) {
5287 case TPACPI_FAN_WR_ACPI_FANS:
5288 case TPACPI_FAN_WR_TPEC:
5289 rc = fan_get_status(&s);
5290 if (rc < 0)
5291 break;
5292
5293 /* Don't go out of emergency fan mode */
5294 if (s != 7) {
5295 s &= 0x07;
5296 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5297 }
5298
5299 if (!acpi_ec_write(fan_status_offset, s))
5300 rc = -EIO;
5301 else {
5302 tp_features.fan_ctrl_status_undef = 0;
5303 rc = 0;
5304 }
5305 break;
5306
5307 case TPACPI_FAN_WR_ACPI_SFAN:
5308 rc = fan_get_status(&s);
5309 if (rc < 0)
5310 break;
5311
5312 s &= 0x07;
5313
5314 /* Set fan to at least level 4 */
5315 s |= 4;
5316
5317 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005318 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005319 else
5320 rc = 0;
5321 break;
5322
5323 default:
5324 rc = -ENXIO;
5325 }
5326
5327 mutex_unlock(&fan_mutex);
5328 return rc;
5329}
5330
5331static int fan_set_disable(void)
5332{
5333 int rc;
5334
5335 if (!fan_control_allowed)
5336 return -EPERM;
5337
5338 if (mutex_lock_interruptible(&fan_mutex))
5339 return -ERESTARTSYS;
5340
5341 rc = 0;
5342 switch (fan_control_access_mode) {
5343 case TPACPI_FAN_WR_ACPI_FANS:
5344 case TPACPI_FAN_WR_TPEC:
5345 if (!acpi_ec_write(fan_status_offset, 0x00))
5346 rc = -EIO;
5347 else {
5348 fan_control_desired_level = 0;
5349 tp_features.fan_ctrl_status_undef = 0;
5350 }
5351 break;
5352
5353 case TPACPI_FAN_WR_ACPI_SFAN:
5354 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5355 rc = -EIO;
5356 else
5357 fan_control_desired_level = 0;
5358 break;
5359
5360 default:
5361 rc = -ENXIO;
5362 }
5363
5364
5365 mutex_unlock(&fan_mutex);
5366 return rc;
5367}
5368
5369static int fan_set_speed(int speed)
5370{
5371 int rc;
5372
5373 if (!fan_control_allowed)
5374 return -EPERM;
5375
5376 if (mutex_lock_interruptible(&fan_mutex))
5377 return -ERESTARTSYS;
5378
5379 rc = 0;
5380 switch (fan_control_access_mode) {
5381 case TPACPI_FAN_WR_ACPI_FANS:
5382 if (speed >= 0 && speed <= 65535) {
5383 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5384 speed, speed, speed))
5385 rc = -EIO;
5386 } else
5387 rc = -EINVAL;
5388 break;
5389
5390 default:
5391 rc = -ENXIO;
5392 }
5393
5394 mutex_unlock(&fan_mutex);
5395 return rc;
5396}
5397
5398static void fan_watchdog_reset(void)
5399{
5400 static int fan_watchdog_active;
5401
5402 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5403 return;
5404
5405 if (fan_watchdog_active)
5406 cancel_delayed_work(&fan_watchdog_task);
5407
5408 if (fan_watchdog_maxinterval > 0 &&
5409 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5410 fan_watchdog_active = 1;
5411 if (!schedule_delayed_work(&fan_watchdog_task,
5412 msecs_to_jiffies(fan_watchdog_maxinterval
5413 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005414 printk(TPACPI_ERR
5415 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005416 "watchdog will not trigger\n");
5417 }
5418 } else
5419 fan_watchdog_active = 0;
5420}
5421
5422static void fan_watchdog_fire(struct work_struct *ignored)
5423{
5424 int rc;
5425
5426 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5427 return;
5428
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005429 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005430 rc = fan_set_enable();
5431 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005432 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005433 "will try again later...\n", -rc);
5434 /* reschedule for later */
5435 fan_watchdog_reset();
5436 }
5437}
5438
5439/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005440 * SYSFS fan layout: hwmon compatible (device)
5441 *
5442 * pwm*_enable:
5443 * 0: "disengaged" mode
5444 * 1: manual mode
5445 * 2: native EC "auto" mode (recommended, hardware default)
5446 *
5447 * pwm*: set speed in manual mode, ignored otherwise.
5448 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5449 * interpolation.
5450 *
5451 * fan*_input: tachometer reading, RPM
5452 *
5453 *
5454 * SYSFS fan layout: extensions
5455 *
5456 * fan_watchdog (driver):
5457 * fan watchdog interval in seconds, 0 disables (default), max 120
5458 */
5459
5460/* sysfs fan pwm1_enable ----------------------------------------------- */
5461static ssize_t fan_pwm1_enable_show(struct device *dev,
5462 struct device_attribute *attr,
5463 char *buf)
5464{
5465 int res, mode;
5466 u8 status;
5467
5468 res = fan_get_status_safe(&status);
5469 if (res)
5470 return res;
5471
5472 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5473 if (status != fan_control_initial_status) {
5474 tp_features.fan_ctrl_status_undef = 0;
5475 } else {
5476 /* Return most likely status. In fact, it
5477 * might be the only possible status */
5478 status = TP_EC_FAN_AUTO;
5479 }
5480 }
5481
5482 if (status & TP_EC_FAN_FULLSPEED) {
5483 mode = 0;
5484 } else if (status & TP_EC_FAN_AUTO) {
5485 mode = 2;
5486 } else
5487 mode = 1;
5488
5489 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5490}
5491
5492static ssize_t fan_pwm1_enable_store(struct device *dev,
5493 struct device_attribute *attr,
5494 const char *buf, size_t count)
5495{
5496 unsigned long t;
5497 int res, level;
5498
5499 if (parse_strtoul(buf, 2, &t))
5500 return -EINVAL;
5501
5502 switch (t) {
5503 case 0:
5504 level = TP_EC_FAN_FULLSPEED;
5505 break;
5506 case 1:
5507 level = TPACPI_FAN_LAST_LEVEL;
5508 break;
5509 case 2:
5510 level = TP_EC_FAN_AUTO;
5511 break;
5512 case 3:
5513 /* reserved for software-controlled auto mode */
5514 return -ENOSYS;
5515 default:
5516 return -EINVAL;
5517 }
5518
5519 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005520 if (res == -ENXIO)
5521 return -EINVAL;
5522 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005523 return res;
5524
5525 fan_watchdog_reset();
5526
5527 return count;
5528}
5529
5530static struct device_attribute dev_attr_fan_pwm1_enable =
5531 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5532 fan_pwm1_enable_show, fan_pwm1_enable_store);
5533
5534/* sysfs fan pwm1 ------------------------------------------------------ */
5535static ssize_t fan_pwm1_show(struct device *dev,
5536 struct device_attribute *attr,
5537 char *buf)
5538{
5539 int res;
5540 u8 status;
5541
5542 res = fan_get_status_safe(&status);
5543 if (res)
5544 return res;
5545
5546 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5547 if (status != fan_control_initial_status) {
5548 tp_features.fan_ctrl_status_undef = 0;
5549 } else {
5550 status = TP_EC_FAN_AUTO;
5551 }
5552 }
5553
5554 if ((status &
5555 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5556 status = fan_control_desired_level;
5557
5558 if (status > 7)
5559 status = 7;
5560
5561 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5562}
5563
5564static ssize_t fan_pwm1_store(struct device *dev,
5565 struct device_attribute *attr,
5566 const char *buf, size_t count)
5567{
5568 unsigned long s;
5569 int rc;
5570 u8 status, newlevel;
5571
5572 if (parse_strtoul(buf, 255, &s))
5573 return -EINVAL;
5574
5575 /* scale down from 0-255 to 0-7 */
5576 newlevel = (s >> 5) & 0x07;
5577
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005578 if (mutex_lock_interruptible(&fan_mutex))
5579 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005580
5581 rc = fan_get_status(&status);
5582 if (!rc && (status &
5583 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5584 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005585 if (rc == -ENXIO)
5586 rc = -EINVAL;
5587 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005588 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005589 fan_watchdog_reset();
5590 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005591 }
5592
5593 mutex_unlock(&fan_mutex);
5594 return (rc)? rc : count;
5595}
5596
5597static struct device_attribute dev_attr_fan_pwm1 =
5598 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5599 fan_pwm1_show, fan_pwm1_store);
5600
5601/* sysfs fan fan1_input ------------------------------------------------ */
5602static ssize_t fan_fan1_input_show(struct device *dev,
5603 struct device_attribute *attr,
5604 char *buf)
5605{
5606 int res;
5607 unsigned int speed;
5608
5609 res = fan_get_speed(&speed);
5610 if (res < 0)
5611 return res;
5612
5613 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5614}
5615
5616static struct device_attribute dev_attr_fan_fan1_input =
5617 __ATTR(fan1_input, S_IRUGO,
5618 fan_fan1_input_show, NULL);
5619
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005620/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005621static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5622 char *buf)
5623{
5624 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5625}
5626
5627static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5628 const char *buf, size_t count)
5629{
5630 unsigned long t;
5631
5632 if (parse_strtoul(buf, 120, &t))
5633 return -EINVAL;
5634
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005635 if (!fan_control_allowed)
5636 return -EPERM;
5637
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005638 fan_watchdog_maxinterval = t;
5639 fan_watchdog_reset();
5640
5641 return count;
5642}
5643
5644static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5645 fan_fan_watchdog_show, fan_fan_watchdog_store);
5646
5647/* --------------------------------------------------------------------- */
5648static struct attribute *fan_attributes[] = {
5649 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5650 &dev_attr_fan_fan1_input.attr,
5651 NULL
5652};
5653
5654static const struct attribute_group fan_attr_group = {
5655 .attrs = fan_attributes,
5656};
5657
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005658static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005659{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005660 int rc;
5661
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005662 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5663
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005664 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005665 fan_status_access_mode = TPACPI_FAN_NONE;
5666 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005667 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005668 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005669 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005670 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005671
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005672 TPACPI_ACPIHANDLE_INIT(fans);
5673 TPACPI_ACPIHANDLE_INIT(gfan);
5674 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005675
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005676 if (gfan_handle) {
5677 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005678 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005679 } else {
5680 /* all other ThinkPads: note that even old-style
5681 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005682 if (likely(acpi_ec_read(fan_status_offset,
5683 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005684 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005685
5686 /* In some ThinkPads, neither the EC nor the ACPI
5687 * DSDT initialize the fan status, and it ends up
5688 * being set to 0x07 when it *could* be either
5689 * 0x07 or 0x80.
5690 *
5691 * Enable for TP-1Y (T43), TP-78 (R51e),
5692 * TP-76 (R52), TP-70 (T43, R52), which are known
5693 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005694 if (fan_control_initial_status == 0x07) {
5695 switch (thinkpad_id.ec_model) {
5696 case 0x5931: /* TP-1Y */
5697 case 0x3837: /* TP-78 */
5698 case 0x3637: /* TP-76 */
5699 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005700 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005701 "fan_init: initial fan status "
5702 "is unknown, assuming it is "
5703 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005704 tp_features.fan_ctrl_status_undef = 1;
5705 ;;
5706 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005707 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005708 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005709 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005710 "ThinkPad ACPI EC access misbehaving, "
5711 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005712 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005713 }
5714 }
5715
5716 if (sfan_handle) {
5717 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005718 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005719 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005720 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005721 } else {
5722 if (!gfan_handle) {
5723 /* gfan without sfan means no fan control */
5724 /* all other models implement TP EC 0x2f control */
5725
5726 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005727 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005728 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005729 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005730 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005731 TPACPI_FAN_CMD_SPEED |
5732 TPACPI_FAN_CMD_LEVEL |
5733 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005734 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005735 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005736 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005737 TPACPI_FAN_CMD_LEVEL |
5738 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005739 }
5740 }
5741 }
5742
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005743 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5744 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5745 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5746 fan_status_access_mode, fan_control_access_mode);
5747
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005748 /* fan control master switch */
5749 if (!fan_control_allowed) {
5750 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5751 fan_control_commands = 0;
5752 dbg_printk(TPACPI_DBG_INIT,
5753 "fan control features disabled by parameter\n");
5754 }
5755
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005756 /* update fan_control_desired_level */
5757 if (fan_status_access_mode != TPACPI_FAN_NONE)
5758 fan_get_status_safe(NULL);
5759
5760 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5761 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005762 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005763 &fan_attr_group);
5764 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005765 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005766 &driver_attr_fan_watchdog);
5767 if (rc < 0)
5768 return rc;
5769 return 0;
5770 } else
5771 return 1;
5772}
5773
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005774static void fan_exit(void)
5775{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005776 vdbg_printk(TPACPI_DBG_EXIT,
5777 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005778
5779 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005780 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005781 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5782 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005783
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005784 cancel_delayed_work(&fan_watchdog_task);
5785 flush_scheduled_work();
5786}
5787
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005788static int fan_read(char *p)
5789{
5790 int len = 0;
5791 int rc;
5792 u8 status;
5793 unsigned int speed = 0;
5794
5795 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005796 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005797 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005798 rc = fan_get_status_safe(&status);
5799 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005800 return rc;
5801
5802 len += sprintf(p + len, "status:\t\t%s\n"
5803 "level:\t\t%d\n",
5804 (status != 0) ? "enabled" : "disabled", status);
5805 break;
5806
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005807 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005808 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005809 rc = fan_get_status_safe(&status);
5810 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005811 return rc;
5812
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005813 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005814 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005815 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005816 else
5817 /* Return most likely status. In fact, it
5818 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005819 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005820 }
5821
5822 len += sprintf(p + len, "status:\t\t%s\n",
5823 (status != 0) ? "enabled" : "disabled");
5824
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005825 rc = fan_get_speed(&speed);
5826 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005827 return rc;
5828
5829 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5830
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005831 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005832 /* Disengaged mode takes precedence */
5833 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005834 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005835 len += sprintf(p + len, "level:\t\tauto\n");
5836 else
5837 len += sprintf(p + len, "level:\t\t%d\n", status);
5838 break;
5839
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005840 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005841 default:
5842 len += sprintf(p + len, "status:\t\tnot supported\n");
5843 }
5844
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005845 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005846 len += sprintf(p + len, "commands:\tlevel <level>");
5847
5848 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005849 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005850 len += sprintf(p + len, " (<level> is 0-7)\n");
5851 break;
5852
5853 default:
5854 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005855 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005856 break;
5857 }
5858 }
5859
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005860 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005861 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005862 "commands:\twatchdog <timeout> (<timeout> "
5863 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005864
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005865 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005866 len += sprintf(p + len, "commands:\tspeed <speed>"
5867 " (<speed> is 0-65535)\n");
5868
5869 return len;
5870}
5871
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005872static int fan_write_cmd_level(const char *cmd, int *rc)
5873{
5874 int level;
5875
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005876 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005877 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005878 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005879 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005880 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005881 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005882 return 0;
5883
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005884 *rc = fan_set_level_safe(level);
5885 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005886 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005887 "access mode %d", fan_control_access_mode);
5888
5889 return 1;
5890}
5891
5892static int fan_write_cmd_enable(const char *cmd, int *rc)
5893{
5894 if (strlencmp(cmd, "enable") != 0)
5895 return 0;
5896
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005897 *rc = fan_set_enable();
5898 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005899 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005900 "access mode %d", fan_control_access_mode);
5901
5902 return 1;
5903}
5904
5905static int fan_write_cmd_disable(const char *cmd, int *rc)
5906{
5907 if (strlencmp(cmd, "disable") != 0)
5908 return 0;
5909
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005910 *rc = fan_set_disable();
5911 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005912 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005913 "access mode %d", fan_control_access_mode);
5914
5915 return 1;
5916}
5917
5918static int fan_write_cmd_speed(const char *cmd, int *rc)
5919{
5920 int speed;
5921
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005922 /* TODO:
5923 * Support speed <low> <medium> <high> ? */
5924
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005925 if (sscanf(cmd, "speed %d", &speed) != 1)
5926 return 0;
5927
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005928 *rc = fan_set_speed(speed);
5929 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005930 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005931 "access mode %d", fan_control_access_mode);
5932
5933 return 1;
5934}
5935
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005936static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5937{
5938 int interval;
5939
5940 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5941 return 0;
5942
5943 if (interval < 0 || interval > 120)
5944 *rc = -EINVAL;
5945 else
5946 fan_watchdog_maxinterval = interval;
5947
5948 return 1;
5949}
5950
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005951static int fan_write(char *buf)
5952{
5953 char *cmd;
5954 int rc = 0;
5955
5956 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005957 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005958 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005959 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005960 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005961 fan_write_cmd_disable(cmd, &rc) ||
5962 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005963 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005964 fan_write_cmd_speed(cmd, &rc))
5965 )
5966 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005967 else if (!rc)
5968 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005969 }
5970
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005971 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005972}
5973
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005974static struct ibm_struct fan_driver_data = {
5975 .name = "fan",
5976 .read = fan_read,
5977 .write = fan_write,
5978 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005979};
5980
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005981/****************************************************************************
5982 ****************************************************************************
5983 *
5984 * Infrastructure
5985 *
5986 ****************************************************************************
5987 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005989/* sysfs name ---------------------------------------------------------- */
5990static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5991 struct device_attribute *attr,
5992 char *buf)
5993{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005994 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005995}
5996
5997static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5998 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5999
6000/* --------------------------------------------------------------------- */
6001
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006002/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03006003static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006004
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006005/*
6006 * Module and infrastructure proble, init and exit handling
6007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006009static int force_load;
6010
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006011#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006012static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006013{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006014 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006015
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006016 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006017}
6018#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6019
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006020static void ibm_exit(struct ibm_struct *ibm)
6021{
6022 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6023
6024 list_del_init(&ibm->all_drivers);
6025
6026 if (ibm->flags.acpi_notify_installed) {
6027 dbg_printk(TPACPI_DBG_EXIT,
6028 "%s: acpi_remove_notify_handler\n", ibm->name);
6029 BUG_ON(!ibm->acpi);
6030 acpi_remove_notify_handler(*ibm->acpi->handle,
6031 ibm->acpi->type,
6032 dispatch_acpi_notify);
6033 ibm->flags.acpi_notify_installed = 0;
6034 ibm->flags.acpi_notify_installed = 0;
6035 }
6036
6037 if (ibm->flags.proc_created) {
6038 dbg_printk(TPACPI_DBG_EXIT,
6039 "%s: remove_proc_entry\n", ibm->name);
6040 remove_proc_entry(ibm->name, proc_dir);
6041 ibm->flags.proc_created = 0;
6042 }
6043
6044 if (ibm->flags.acpi_driver_registered) {
6045 dbg_printk(TPACPI_DBG_EXIT,
6046 "%s: acpi_bus_unregister_driver\n", ibm->name);
6047 BUG_ON(!ibm->acpi);
6048 acpi_bus_unregister_driver(ibm->acpi->driver);
6049 kfree(ibm->acpi->driver);
6050 ibm->acpi->driver = NULL;
6051 ibm->flags.acpi_driver_registered = 0;
6052 }
6053
6054 if (ibm->flags.init_called && ibm->exit) {
6055 ibm->exit();
6056 ibm->flags.init_called = 0;
6057 }
6058
6059 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6060}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006061
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006062static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063{
6064 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006065 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 struct proc_dir_entry *entry;
6067
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006068 BUG_ON(ibm == NULL);
6069
6070 INIT_LIST_HEAD(&ibm->all_drivers);
6071
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006072 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 return 0;
6074
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006075 dbg_printk(TPACPI_DBG_INIT,
6076 "probing for %s\n", ibm->name);
6077
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006078 if (iibm->init) {
6079 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006080 if (ret > 0)
6081 return 0; /* probe failed */
6082 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006084
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006085 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 }
6087
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006088 if (ibm->acpi) {
6089 if (ibm->acpi->hid) {
6090 ret = register_tpacpi_subdriver(ibm);
6091 if (ret)
6092 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006093 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006094
6095 if (ibm->acpi->notify) {
6096 ret = setup_acpi_notify(ibm);
6097 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006098 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006099 ibm->name);
6100 ret = 0;
6101 goto err_out;
6102 }
6103 if (ret < 0)
6104 goto err_out;
6105 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006106 }
6107
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006108 dbg_printk(TPACPI_DBG_INIT,
6109 "%s installed\n", ibm->name);
6110
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006111 if (ibm->read) {
6112 entry = create_proc_entry(ibm->name,
6113 S_IFREG | S_IRUGO | S_IWUSR,
6114 proc_dir);
6115 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006116 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006117 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006118 ret = -ENODEV;
6119 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006120 }
6121 entry->owner = THIS_MODULE;
6122 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006123 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006124 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006125 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006126 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006129 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006132
6133err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006134 dbg_printk(TPACPI_DBG_INIT,
6135 "%s: at error exit path with result %d\n",
6136 ibm->name, ret);
6137
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006138 ibm_exit(ibm);
6139 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140}
6141
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006142/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006144static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006145{
Jeff Garzik18552562007-10-03 15:15:40 -04006146 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006147 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006148
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006149 if (!tp)
6150 return;
6151
6152 memset(tp, 0, sizeof(*tp));
6153
6154 if (dmi_name_in_vendors("IBM"))
6155 tp->vendor = PCI_VENDOR_ID_IBM;
6156 else if (dmi_name_in_vendors("LENOVO"))
6157 tp->vendor = PCI_VENDOR_ID_LENOVO;
6158 else
6159 return;
6160
6161 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
6162 GFP_KERNEL);
6163 if (!tp->bios_version_str)
6164 return;
6165 tp->bios_model = tp->bios_version_str[0]
6166 | (tp->bios_version_str[1] << 8);
6167
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006168 /*
6169 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6170 * X32 or newer, all Z series; Some models must have an
6171 * up-to-date BIOS or they will not be detected.
6172 *
6173 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6174 */
6175 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006176 if (sscanf(dev->name,
6177 "IBM ThinkPad Embedded Controller -[%17c",
6178 ec_fw_string) == 1) {
6179 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6180 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006181
6182 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6183 tp->ec_model = ec_fw_string[0]
6184 | (ec_fw_string[1] << 8);
6185 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006186 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006187 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006188
6189 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6190 GFP_KERNEL);
6191 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
6192 kfree(tp->model_str);
6193 tp->model_str = NULL;
6194 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006195
6196 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6197 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198}
6199
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006200static int __init probe_for_thinkpad(void)
6201{
6202 int is_thinkpad;
6203
6204 if (acpi_disabled)
6205 return -ENODEV;
6206
6207 /*
6208 * Non-ancient models have better DMI tagging, but very old models
6209 * don't.
6210 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006211 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006212
6213 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006214 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006215 if (!ec_handle) {
6216 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006217 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006218 "Not yet supported ThinkPad detected!\n");
6219 return -ENODEV;
6220 }
6221
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006222 /*
6223 * Risks a regression on very old machines, but reduces potential
6224 * false positives a damn great deal
6225 */
6226 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006227 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006228
6229 if (!is_thinkpad && !force_load)
6230 return -ENODEV;
6231
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006232 return 0;
6233}
6234
6235
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006236/* Module init, exit, parameters */
6237
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006238static struct ibm_init_struct ibms_init[] __initdata = {
6239 {
6240 .init = thinkpad_acpi_driver_init,
6241 .data = &thinkpad_acpi_driver_data,
6242 },
6243 {
6244 .init = hotkey_init,
6245 .data = &hotkey_driver_data,
6246 },
6247 {
6248 .init = bluetooth_init,
6249 .data = &bluetooth_driver_data,
6250 },
6251 {
6252 .init = wan_init,
6253 .data = &wan_driver_data,
6254 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006255#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006256 {
6257 .init = video_init,
6258 .data = &video_driver_data,
6259 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006260#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006261 {
6262 .init = light_init,
6263 .data = &light_driver_data,
6264 },
6265#ifdef CONFIG_THINKPAD_ACPI_DOCK
6266 {
6267 .init = dock_init,
6268 .data = &dock_driver_data[0],
6269 },
6270 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03006271 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006272 .data = &dock_driver_data[1],
6273 },
6274#endif
6275#ifdef CONFIG_THINKPAD_ACPI_BAY
6276 {
6277 .init = bay_init,
6278 .data = &bay_driver_data,
6279 },
6280#endif
6281 {
6282 .init = cmos_init,
6283 .data = &cmos_driver_data,
6284 },
6285 {
6286 .init = led_init,
6287 .data = &led_driver_data,
6288 },
6289 {
6290 .init = beep_init,
6291 .data = &beep_driver_data,
6292 },
6293 {
6294 .init = thermal_init,
6295 .data = &thermal_driver_data,
6296 },
6297 {
6298 .data = &ecdump_driver_data,
6299 },
6300 {
6301 .init = brightness_init,
6302 .data = &brightness_driver_data,
6303 },
6304 {
6305 .data = &volume_driver_data,
6306 },
6307 {
6308 .init = fan_init,
6309 .data = &fan_driver_data,
6310 },
6311};
6312
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006313static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6314{
6315 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006316 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006317
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006318 if (!kp || !kp->name || !val)
6319 return -EINVAL;
6320
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006321 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6322 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006323 WARN_ON(ibm == NULL);
6324
6325 if (!ibm || !ibm->name)
6326 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006327
6328 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6329 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006330 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006331 strcpy(ibms_init[i].param, val);
6332 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006333 return 0;
6334 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006335 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006336
6337 return -EINVAL;
6338}
6339
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006340module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006341MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006342 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006343
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006344module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006345MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006346
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006347module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006348MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006349 "Attempts to load the driver even on a "
6350 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006351
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006352module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006353MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006354 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006355
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006356module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006357MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006358 "Selects brightness control strategy: "
6359 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006360
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006361module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006362MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006363 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006364
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006365module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006366MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006367 "used for backwards compatibility with userspace, "
6368 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006369
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006370#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006371 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006372 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006373 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006374
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006375TPACPI_PARAM(hotkey);
6376TPACPI_PARAM(bluetooth);
6377TPACPI_PARAM(video);
6378TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006379#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006380TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006381#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006382#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006383TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006384#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006385TPACPI_PARAM(cmos);
6386TPACPI_PARAM(led);
6387TPACPI_PARAM(beep);
6388TPACPI_PARAM(ecdump);
6389TPACPI_PARAM(brightness);
6390TPACPI_PARAM(volume);
6391TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006392
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006393static void thinkpad_acpi_module_exit(void)
6394{
6395 struct ibm_struct *ibm, *itmp;
6396
6397 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6398
6399 list_for_each_entry_safe_reverse(ibm, itmp,
6400 &tpacpi_all_drivers,
6401 all_drivers) {
6402 ibm_exit(ibm);
6403 }
6404
6405 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6406
6407 if (tpacpi_inputdev) {
6408 if (tp_features.input_device_registered)
6409 input_unregister_device(tpacpi_inputdev);
6410 else
6411 input_free_device(tpacpi_inputdev);
6412 }
6413
6414 if (tpacpi_hwmon)
6415 hwmon_device_unregister(tpacpi_hwmon);
6416
6417 if (tp_features.sensors_pdev_attrs_registered)
6418 device_remove_file(&tpacpi_sensors_pdev->dev,
6419 &dev_attr_thinkpad_acpi_pdev_name);
6420 if (tpacpi_sensors_pdev)
6421 platform_device_unregister(tpacpi_sensors_pdev);
6422 if (tpacpi_pdev)
6423 platform_device_unregister(tpacpi_pdev);
6424
6425 if (tp_features.sensors_pdrv_attrs_registered)
6426 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6427 if (tp_features.platform_drv_attrs_registered)
6428 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6429
6430 if (tp_features.sensors_pdrv_registered)
6431 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6432
6433 if (tp_features.platform_drv_registered)
6434 platform_driver_unregister(&tpacpi_pdriver);
6435
6436 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006437 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006438
6439 kfree(thinkpad_id.bios_version_str);
6440 kfree(thinkpad_id.ec_version_str);
6441 kfree(thinkpad_id.model_str);
6442}
6443
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006444
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006445static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446{
6447 int ret, i;
6448
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006449 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6450
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006451 /* Parameter checking */
6452 if (hotkey_report_mode > 2)
6453 return -EINVAL;
6454
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006455 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006456
6457 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006458 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006459 if (ret) {
6460 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006461 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006464 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006465
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006466 TPACPI_ACPIHANDLE_INIT(ecrd);
6467 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006469 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006471 printk(TPACPI_ERR
6472 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006473 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 return -ENODEV;
6475 }
6476 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006477
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006478 ret = platform_driver_register(&tpacpi_pdriver);
6479 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006480 printk(TPACPI_ERR
6481 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006482 thinkpad_acpi_module_exit();
6483 return ret;
6484 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006485 tp_features.platform_drv_registered = 1;
6486
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006487 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6488 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006489 printk(TPACPI_ERR
6490 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006491 thinkpad_acpi_module_exit();
6492 return ret;
6493 }
6494 tp_features.sensors_pdrv_registered = 1;
6495
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006496 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006497 if (!ret) {
6498 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006499 ret = tpacpi_create_driver_attributes(
6500 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006501 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006502 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006503 printk(TPACPI_ERR
6504 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006505 thinkpad_acpi_module_exit();
6506 return ret;
6507 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006508 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006509
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006510
6511 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006512 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006513 NULL, 0);
6514 if (IS_ERR(tpacpi_pdev)) {
6515 ret = PTR_ERR(tpacpi_pdev);
6516 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006517 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006518 thinkpad_acpi_module_exit();
6519 return ret;
6520 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006521 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006522 TPACPI_HWMON_DRVR_NAME,
6523 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006524 if (IS_ERR(tpacpi_sensors_pdev)) {
6525 ret = PTR_ERR(tpacpi_sensors_pdev);
6526 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006527 printk(TPACPI_ERR
6528 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006529 thinkpad_acpi_module_exit();
6530 return ret;
6531 }
6532 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6533 &dev_attr_thinkpad_acpi_pdev_name);
6534 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006535 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006536 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006537 thinkpad_acpi_module_exit();
6538 return ret;
6539 }
6540 tp_features.sensors_pdev_attrs_registered = 1;
6541 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006542 if (IS_ERR(tpacpi_hwmon)) {
6543 ret = PTR_ERR(tpacpi_hwmon);
6544 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006545 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006546 thinkpad_acpi_module_exit();
6547 return ret;
6548 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006549 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006550 tpacpi_inputdev = input_allocate_device();
6551 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006552 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006553 thinkpad_acpi_module_exit();
6554 return -ENOMEM;
6555 } else {
6556 /* Prepare input device, but don't register */
6557 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006558 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006559 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006560 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6561 thinkpad_id.vendor :
6562 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006563 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6564 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6565 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006566 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6567 ret = ibm_init(&ibms_init[i]);
6568 if (ret >= 0 && *ibms_init[i].param)
6569 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006571 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 return ret;
6573 }
6574 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006575 ret = input_register_device(tpacpi_inputdev);
6576 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006577 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006578 thinkpad_acpi_module_exit();
6579 return ret;
6580 } else {
6581 tp_features.input_device_registered = 1;
6582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006584 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 return 0;
6586}
6587
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006588/* Please remove this in year 2009 */
6589MODULE_ALIAS("ibm_acpi");
6590
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03006591MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6592
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006593/*
6594 * DMI matching for module autoloading
6595 *
6596 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6597 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6598 *
6599 * Only models listed in thinkwiki will be supported, so add yours
6600 * if it is not there yet.
6601 */
6602#define IBM_BIOS_MODULE_ALIAS(__type) \
6603 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6604
6605/* Non-ancient thinkpads */
6606MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6607MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6608
6609/* Ancient thinkpad BIOSes have to be identified by
6610 * BIOS type or model number, and there are far less
6611 * BIOS types than model numbers... */
6612IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6613IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6614IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6615
6616MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006617MODULE_DESCRIPTION(TPACPI_DESC);
6618MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006619MODULE_LICENSE("GPL");
6620
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006621module_init(thinkpad_acpi_module_init);
6622module_exit(thinkpad_acpi_module_exit);