blob: 6b9300779a431c1828acad495ddfbbec41332836 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh6a2e2932008-01-08 13:02:56 -02006 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh490673d2008-07-21 09:15:51 -030024#define TPACPI_VERSION "0.21"
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -020025#define TPACPI_SYSFS_VERSION 0x020200
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
57#include <linux/kthread.h>
58#include <linux/freezer.h>
59#include <linux/delay.h>
60
61#include <linux/nvram.h>
62#include <linux/proc_fs.h>
63#include <linux/sysfs.h>
64#include <linux/backlight.h>
65#include <linux/fb.h>
66#include <linux/platform_device.h>
67#include <linux/hwmon.h>
68#include <linux/hwmon-sysfs.h>
69#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030070#include <linux/leds.h>
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -030071#include <linux/rfkill.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020072#include <asm/uaccess.h>
73
74#include <linux/dmi.h>
75#include <linux/jiffies.h>
76#include <linux/workqueue.h>
77
78#include <acpi/acpi_drivers.h>
79#include <acpi/acnamesp.h>
80
81#include <linux/pci_ids.h>
82
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020083
84/* ThinkPad CMOS commands */
85#define TP_CMOS_VOLUME_DOWN 0
86#define TP_CMOS_VOLUME_UP 1
87#define TP_CMOS_VOLUME_MUTE 2
88#define TP_CMOS_BRIGHTNESS_UP 4
89#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030090#define TP_CMOS_THINKLIGHT_ON 12
91#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020092
93/* NVRAM Addresses */
94enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
100};
101
102/* NVRAM bit masks */
103enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
117};
118
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200119/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200120#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200121
122/* Input IDs */
123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124#define TPACPI_HKEY_INPUT_VERSION 0x4101
125
126
127/****************************************************************************
128 * Main driver
129 */
130
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200131#define TPACPI_NAME "thinkpad"
132#define TPACPI_DESC "ThinkPad ACPI Extras"
133#define TPACPI_FILE TPACPI_NAME "_acpi"
134#define TPACPI_URL "http://ibm-acpi.sf.net/"
135#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200136
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200137#define TPACPI_PROC_DIR "ibm"
138#define TPACPI_ACPI_EVENT_PREFIX "ibm"
139#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300140#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200141#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200142
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300143#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300144#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300145
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200146#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200147
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300148/* rfkill switches */
149enum {
150 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
151 TPACPI_RFK_WWAN_SW_ID,
152};
153
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200154/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200155#define TPACPI_LOG TPACPI_FILE ": "
156#define TPACPI_ERR KERN_ERR TPACPI_LOG
157#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
158#define TPACPI_INFO KERN_INFO TPACPI_LOG
159#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200160
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200161#define TPACPI_DBG_ALL 0xffff
162#define TPACPI_DBG_ALL 0xffff
163#define TPACPI_DBG_INIT 0x0001
164#define TPACPI_DBG_EXIT 0x0002
165#define dbg_printk(a_dbg_level, format, arg...) \
166 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200167 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
168 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200169#ifdef CONFIG_THINKPAD_ACPI_DEBUG
170#define vdbg_printk(a_dbg_level, format, arg...) \
171 dbg_printk(a_dbg_level, format, ## arg)
172static const char *str_supported(int is_supported);
173#else
174#define vdbg_printk(a_dbg_level, format, arg...)
175#endif
176
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200177#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
178#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
179#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200180
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200181
182/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200183 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200184 */
185
186struct ibm_struct;
187
188struct tp_acpi_drv_struct {
189 const struct acpi_device_id *hid;
190 struct acpi_driver *driver;
191
192 void (*notify) (struct ibm_struct *, u32);
193 acpi_handle *handle;
194 u32 type;
195 struct acpi_device *device;
196};
197
198struct ibm_struct {
199 char *name;
200
201 int (*read) (char *);
202 int (*write) (char *);
203 void (*exit) (void);
204 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200205 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200206
207 struct list_head all_drivers;
208
209 struct tp_acpi_drv_struct *acpi;
210
211 struct {
212 u8 acpi_driver_registered:1;
213 u8 acpi_notify_installed:1;
214 u8 proc_created:1;
215 u8 init_called:1;
216 u8 experimental:1;
217 } flags;
218};
219
220struct ibm_init_struct {
221 char param[32];
222
223 int (*init) (struct ibm_init_struct *);
224 struct ibm_struct *data;
225};
226
227static struct {
228#ifdef CONFIG_THINKPAD_ACPI_BAY
229 u32 bay_status:1;
230 u32 bay_eject:1;
231 u32 bay_status2:1;
232 u32 bay_eject2:1;
233#endif
234 u32 bluetooth:1;
235 u32 hotkey:1;
236 u32 hotkey_mask:1;
237 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200238 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200239 u32 light:1;
240 u32 light_status:1;
241 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300242 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200243 u32 wan:1;
244 u32 fan_ctrl_status_undef:1;
245 u32 input_device_registered:1;
246 u32 platform_drv_registered:1;
247 u32 platform_drv_attrs_registered:1;
248 u32 sensors_pdrv_registered:1;
249 u32 sensors_pdrv_attrs_registered:1;
250 u32 sensors_pdev_attrs_registered:1;
251 u32 hotkey_poll_active:1;
252} tp_features;
253
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300254static struct {
255 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300256 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300257} tp_warned;
258
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200259struct thinkpad_id_data {
260 unsigned int vendor; /* ThinkPad vendor:
261 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
262
263 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
264 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
265
266 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
267 u16 ec_model;
268
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300269 char *model_str; /* ThinkPad T43 */
270 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200271};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200272static struct thinkpad_id_data thinkpad_id;
273
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300274static enum {
275 TPACPI_LIFE_INIT = 0,
276 TPACPI_LIFE_RUNNING,
277 TPACPI_LIFE_EXITING,
278} tpacpi_lifecycle;
279
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200280static int experimental;
281static u32 dbg_level;
282
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300283static struct workqueue_struct *tpacpi_wq;
284
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300285/* Special LED class that can defer work */
286struct tpacpi_led_classdev {
287 struct led_classdev led_classdev;
288 struct work_struct work;
289 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300290 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300291};
292
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300293/****************************************************************************
294 ****************************************************************************
295 *
296 * ACPI Helpers and device model
297 *
298 ****************************************************************************
299 ****************************************************************************/
300
301/*************************************************************************
302 * ACPI basic handles
303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300305static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200307#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 static acpi_handle object##_handle; \
309 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400310 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 static char *object##_paths[] = { paths }
312
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200313TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400314 "\\_SB.PCI.ISA.EC", /* 570 */
315 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
316 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
317 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
318 "\\_SB.PCI0.ICH3.EC0", /* R31 */
319 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300320 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200322TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
323TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200325TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
326 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400327 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
328 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300329 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400330
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200331TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400332 "^HKEY", /* R30, R31 */
333 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300334 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400335
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200336TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
337 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
338 "\\_SB.PCI0.VID0", /* 770e */
339 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
340 "\\_SB.PCI0.AGP.VID", /* all others */
341 ); /* R30, R31 */
342
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400343
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300344/*************************************************************************
345 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200346 */
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static int acpi_evalf(acpi_handle handle,
349 void *res, char *method, char *fmt, ...)
350{
351 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400352 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200353 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400354 struct acpi_buffer result, *resultp;
355 union acpi_object out_obj;
356 acpi_status status;
357 va_list ap;
358 char res_type;
359 int success;
360 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200363 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365 }
366
367 if (*fmt == 'q') {
368 quiet = 1;
369 fmt++;
370 } else
371 quiet = 0;
372
373 res_type = *(fmt++);
374
375 params.count = 0;
376 params.pointer = &in_objs[0];
377
378 va_start(ap, fmt);
379 while (*fmt) {
380 char c = *(fmt++);
381 switch (c) {
382 case 'd': /* int */
383 in_objs[params.count].integer.value = va_arg(ap, int);
384 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
385 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400386 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200388 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 "with invalid format character '%c'\n", c);
390 return 0;
391 }
392 }
393 va_end(ap);
394
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395 if (res_type != 'v') {
396 result.length = sizeof(out_obj);
397 result.pointer = &out_obj;
398 resultp = &result;
399 } else
400 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400402 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400405 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (res)
407 *(int *)res = out_obj.integer.value;
408 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
409 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400410 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 success = status == AE_OK;
412 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400413 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200415 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 "with invalid format character '%c'\n", res_type);
417 return 0;
418 }
419
420 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200421 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 method, fmt0, status);
423
424 return success;
425}
426
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200427static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300428{
429 int v;
430
431 if (ecrd_handle) {
432 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
433 return 0;
434 *p = v;
435 } else {
436 if (ec_read(i, p) < 0)
437 return 0;
438 }
439
440 return 1;
441}
442
443static int acpi_ec_write(int i, u8 v)
444{
445 if (ecwr_handle) {
446 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
447 return 0;
448 } else {
449 if (ec_write(i, v) < 0)
450 return 0;
451 }
452
453 return 1;
454}
455
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200456#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300457static int _sta(acpi_handle handle)
458{
459 int status;
460
461 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
462 status = 0;
463
464 return status;
465}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200466#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300467
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300468static int issue_thinkpad_cmos_command(int cmos_cmd)
469{
470 if (!cmos_handle)
471 return -ENXIO;
472
473 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
474 return -EIO;
475
476 return 0;
477}
478
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300479/*************************************************************************
480 * ACPI device model
481 */
482
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200483#define TPACPI_ACPIHANDLE_INIT(object) \
484 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200485 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
486
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300487static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300488 acpi_handle *handle, acpi_handle parent,
489 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300490{
491 int i;
492 acpi_status status;
493
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300494 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
495 name);
496
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300497 for (i = 0; i < num_paths; i++) {
498 status = acpi_get_handle(parent, paths[i], handle);
499 if (ACPI_SUCCESS(status)) {
500 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300501 dbg_printk(TPACPI_DBG_INIT,
502 "Found ACPI handle %s for %s\n",
503 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504 return;
505 }
506 }
507
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300508 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
509 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300510 *handle = NULL;
511}
512
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300513static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514{
515 struct ibm_struct *ibm = data;
516
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300517 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
518 return;
519
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300520 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300521 return;
522
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300523 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300524}
525
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300526static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300527{
528 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300529 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300530
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300531 BUG_ON(!ibm->acpi);
532
533 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300534 return 0;
535
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300536 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300537 "setting up ACPI notify for %s\n", ibm->name);
538
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300539 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
540 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200541 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300542 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543 return -ENODEV;
544 }
545
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300546 acpi_driver_data(ibm->acpi->device) = ibm;
547 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200548 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300549 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300550
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300551 status = acpi_install_notify_handler(*ibm->acpi->handle,
552 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300553 if (ACPI_FAILURE(status)) {
554 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200555 printk(TPACPI_NOTICE
556 "another device driver is already "
557 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300558 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200559 printk(TPACPI_ERR
560 "acpi_install_notify_handler(%s) failed: %d\n",
561 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300562 }
563 return -ENODEV;
564 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300565 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300566 return 0;
567}
568
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300569static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300570{
571 return 0;
572}
573
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300574static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300575{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300576 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300577
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300578 dbg_printk(TPACPI_DBG_INIT,
579 "registering %s as an ACPI driver\n", ibm->name);
580
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300581 BUG_ON(!ibm->acpi);
582
583 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
584 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200585 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300586 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300587 }
588
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200589 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300590 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200591
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300592 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300593
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300594 rc = acpi_bus_register_driver(ibm->acpi->driver);
595 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200596 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200597 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300598 kfree(ibm->acpi->driver);
599 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300600 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300601 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300602
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300603 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300604}
605
606
607/****************************************************************************
608 ****************************************************************************
609 *
610 * Procfs Helpers
611 *
612 ****************************************************************************
613 ****************************************************************************/
614
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300615static int dispatch_procfs_read(char *page, char **start, off_t off,
616 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300617{
618 struct ibm_struct *ibm = data;
619 int len;
620
621 if (!ibm || !ibm->read)
622 return -EINVAL;
623
624 len = ibm->read(page);
625 if (len < 0)
626 return len;
627
628 if (len <= off + count)
629 *eof = 1;
630 *start = page + off;
631 len -= off;
632 if (len > count)
633 len = count;
634 if (len < 0)
635 len = 0;
636
637 return len;
638}
639
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300640static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200641 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300642 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300643{
644 struct ibm_struct *ibm = data;
645 char *kernbuf;
646 int ret;
647
648 if (!ibm || !ibm->write)
649 return -EINVAL;
650
651 kernbuf = kmalloc(count + 2, GFP_KERNEL);
652 if (!kernbuf)
653 return -ENOMEM;
654
655 if (copy_from_user(kernbuf, userbuf, count)) {
656 kfree(kernbuf);
657 return -EFAULT;
658 }
659
660 kernbuf[count] = 0;
661 strcat(kernbuf, ",");
662 ret = ibm->write(kernbuf);
663 if (ret == 0)
664 ret = count;
665
666 kfree(kernbuf);
667
668 return ret;
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static char *next_cmd(char **cmds)
672{
673 char *start = *cmds;
674 char *end;
675
676 while ((end = strchr(start, ',')) && end == start)
677 start = end + 1;
678
679 if (!end)
680 return NULL;
681
682 *end = 0;
683 *cmds = end + 1;
684 return start;
685}
686
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300687
688/****************************************************************************
689 ****************************************************************************
690 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300691 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300692 *
693 ****************************************************************************
694 ****************************************************************************/
695
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300696static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300697static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700698static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300699static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300700static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200701static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300702
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200703static int tpacpi_suspend_handler(struct platform_device *pdev,
704 pm_message_t state)
705{
706 struct ibm_struct *ibm, *itmp;
707
708 list_for_each_entry_safe(ibm, itmp,
709 &tpacpi_all_drivers,
710 all_drivers) {
711 if (ibm->suspend)
712 (ibm->suspend)(state);
713 }
714
715 return 0;
716}
717
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300718static int tpacpi_resume_handler(struct platform_device *pdev)
719{
720 struct ibm_struct *ibm, *itmp;
721
722 list_for_each_entry_safe(ibm, itmp,
723 &tpacpi_all_drivers,
724 all_drivers) {
725 if (ibm->resume)
726 (ibm->resume)();
727 }
728
729 return 0;
730}
731
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300732static struct platform_driver tpacpi_pdriver = {
733 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200734 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300735 .owner = THIS_MODULE,
736 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200737 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300738 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300739};
740
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300741static struct platform_driver tpacpi_hwmon_pdriver = {
742 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200743 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300744 .owner = THIS_MODULE,
745 },
746};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300747
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300748/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300749 * sysfs support helpers
750 */
751
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200752struct attribute_set {
753 unsigned int members, max_members;
754 struct attribute_group group;
755};
756
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300757struct attribute_set_obj {
758 struct attribute_set s;
759 struct attribute *a;
760} __attribute__((packed));
761
762static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200763 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300764{
765 struct attribute_set_obj *sobj;
766
767 if (max_members == 0)
768 return NULL;
769
770 /* Allocates space for implicit NULL at the end too */
771 sobj = kzalloc(sizeof(struct attribute_set_obj) +
772 max_members * sizeof(struct attribute *),
773 GFP_KERNEL);
774 if (!sobj)
775 return NULL;
776 sobj->s.max_members = max_members;
777 sobj->s.group.attrs = &sobj->a;
778 sobj->s.group.name = name;
779
780 return &sobj->s;
781}
782
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200783#define destroy_attr_set(_set) \
784 kfree(_set);
785
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300786/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200787static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300788{
789 if (!s || !attr)
790 return -EINVAL;
791
792 if (s->members >= s->max_members)
793 return -ENOMEM;
794
795 s->group.attrs[s->members] = attr;
796 s->members++;
797
798 return 0;
799}
800
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200801static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300802 struct attribute **attr,
803 unsigned int count)
804{
805 int i, res;
806
807 for (i = 0; i < count; i++) {
808 res = add_to_attr_set(s, attr[i]);
809 if (res)
810 return res;
811 }
812
813 return 0;
814}
815
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200816static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300817{
818 sysfs_remove_group(kobj, &s->group);
819 destroy_attr_set(s);
820}
821
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200822#define register_attr_set_with_sysfs(_attr_set, _kobj) \
823 sysfs_create_group(_kobj, &_attr_set->group)
824
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300825static int parse_strtoul(const char *buf,
826 unsigned long max, unsigned long *value)
827{
828 char *endp;
829
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300830 while (*buf && isspace(*buf))
831 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300832 *value = simple_strtoul(buf, &endp, 0);
833 while (*endp && isspace(*endp))
834 endp++;
835 if (*endp || *value > max)
836 return -EINVAL;
837
838 return 0;
839}
840
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300841static int __init tpacpi_query_bcl_levels(acpi_handle handle)
842{
843 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
844 union acpi_object *obj;
845 int rc;
846
847 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
848 obj = (union acpi_object *)buffer.pointer;
849 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
850 printk(TPACPI_ERR "Unknown _BCL data, "
851 "please report this to %s\n", TPACPI_MAIL);
852 rc = 0;
853 } else {
854 rc = obj->package.count;
855 }
856 } else {
857 return 0;
858 }
859
860 kfree(buffer.pointer);
861 return rc;
862}
863
864static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
865 u32 lvl, void *context, void **rv)
866{
867 char name[ACPI_PATH_SEGMENT_LENGTH];
868 struct acpi_buffer buffer = { sizeof(name), &name };
869
870 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
871 !strncmp("_BCL", name, sizeof(name) - 1)) {
872 BUG_ON(!rv || !*rv);
873 **(int **)rv = tpacpi_query_bcl_levels(handle);
874 return AE_CTRL_TERMINATE;
875 } else {
876 return AE_OK;
877 }
878}
879
880/*
881 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
882 */
883static int __init tpacpi_check_std_acpi_brightness_support(void)
884{
885 int status;
886 int bcl_levels = 0;
887 void *bcl_ptr = &bcl_levels;
888
889 if (!vid_handle) {
890 TPACPI_ACPIHANDLE_INIT(vid);
891 }
892 if (!vid_handle)
893 return 0;
894
895 /*
896 * Search for a _BCL method, and execute it. This is safe on all
897 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
898 * BIOS in ACPI backlight control mode. We do NOT have to care
899 * about calling the _BCL method in an enabled video device, any
900 * will do for our purposes.
901 */
902
903 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
904 tpacpi_acpi_walk_find_bcl, NULL,
905 &bcl_ptr);
906
907 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
908 tp_features.bright_acpimode = 1;
909 return (bcl_levels - 2);
910 }
911
912 return 0;
913}
914
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300915static int __init tpacpi_new_rfkill(const unsigned int id,
916 struct rfkill **rfk,
917 const enum rfkill_type rfktype,
918 const char *name,
919 int (*toggle_radio)(void *, enum rfkill_state),
920 int (*get_state)(void *, enum rfkill_state *))
921{
922 int res;
923 enum rfkill_state initial_state;
924
925 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
926 if (!*rfk) {
927 printk(TPACPI_ERR
928 "failed to allocate memory for rfkill class\n");
929 return -ENOMEM;
930 }
931
932 (*rfk)->name = name;
933 (*rfk)->get_state = get_state;
934 (*rfk)->toggle_radio = toggle_radio;
935
936 if (!get_state(NULL, &initial_state))
937 (*rfk)->state = initial_state;
938
939 res = rfkill_register(*rfk);
940 if (res < 0) {
941 printk(TPACPI_ERR
942 "failed to register %s rfkill switch: %d\n",
943 name, res);
944 rfkill_free(*rfk);
945 *rfk = NULL;
946 return res;
947 }
948
949 return 0;
950}
951
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200952/*************************************************************************
953 * thinkpad-acpi driver attributes
954 */
955
956/* interface_version --------------------------------------------------- */
957static ssize_t tpacpi_driver_interface_version_show(
958 struct device_driver *drv,
959 char *buf)
960{
961 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
962}
963
964static DRIVER_ATTR(interface_version, S_IRUGO,
965 tpacpi_driver_interface_version_show, NULL);
966
967/* debug_level --------------------------------------------------------- */
968static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
969 char *buf)
970{
971 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
972}
973
974static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
975 const char *buf, size_t count)
976{
977 unsigned long t;
978
979 if (parse_strtoul(buf, 0xffff, &t))
980 return -EINVAL;
981
982 dbg_level = t;
983
984 return count;
985}
986
987static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
988 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
989
990/* version ------------------------------------------------------------- */
991static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
992 char *buf)
993{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200994 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
995 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200996}
997
998static DRIVER_ATTR(version, S_IRUGO,
999 tpacpi_driver_version_show, NULL);
1000
1001/* --------------------------------------------------------------------- */
1002
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001003static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001004 &driver_attr_debug_level, &driver_attr_version,
1005 &driver_attr_interface_version,
1006};
1007
1008static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1009{
1010 int i, res;
1011
1012 i = 0;
1013 res = 0;
1014 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1015 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1016 i++;
1017 }
1018
1019 return res;
1020}
1021
1022static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1023{
1024 int i;
1025
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001026 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001027 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1028}
1029
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001030/****************************************************************************
1031 ****************************************************************************
1032 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001033 * Subdrivers
1034 *
1035 ****************************************************************************
1036 ****************************************************************************/
1037
1038/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001039 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001040 */
1041
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001042static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001044 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1045 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001047 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001048 (thinkpad_id.bios_version_str) ?
1049 thinkpad_id.bios_version_str : "unknown",
1050 (thinkpad_id.ec_version_str) ?
1051 thinkpad_id.ec_version_str : "unknown");
1052
1053 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001054 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001055 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1056 "IBM" : ((thinkpad_id.vendor ==
1057 PCI_VENDOR_ID_LENOVO) ?
1058 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001059 thinkpad_id.model_str,
1060 (thinkpad_id.nummodel_str) ?
1061 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064}
1065
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001066static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 int len = 0;
1069
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001070 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1071 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 return len;
1074}
1075
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001076static struct ibm_struct thinkpad_acpi_driver_data = {
1077 .name = "driver",
1078 .read = thinkpad_acpi_driver_read,
1079};
1080
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001081/*************************************************************************
1082 * Hotkey subdriver
1083 */
1084
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001085enum { /* hot key scan codes (derived from ACPI DSDT) */
1086 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1087 TP_ACPI_HOTKEYSCAN_FNF2,
1088 TP_ACPI_HOTKEYSCAN_FNF3,
1089 TP_ACPI_HOTKEYSCAN_FNF4,
1090 TP_ACPI_HOTKEYSCAN_FNF5,
1091 TP_ACPI_HOTKEYSCAN_FNF6,
1092 TP_ACPI_HOTKEYSCAN_FNF7,
1093 TP_ACPI_HOTKEYSCAN_FNF8,
1094 TP_ACPI_HOTKEYSCAN_FNF9,
1095 TP_ACPI_HOTKEYSCAN_FNF10,
1096 TP_ACPI_HOTKEYSCAN_FNF11,
1097 TP_ACPI_HOTKEYSCAN_FNF12,
1098 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1099 TP_ACPI_HOTKEYSCAN_FNINSERT,
1100 TP_ACPI_HOTKEYSCAN_FNDELETE,
1101 TP_ACPI_HOTKEYSCAN_FNHOME,
1102 TP_ACPI_HOTKEYSCAN_FNEND,
1103 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1104 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1105 TP_ACPI_HOTKEYSCAN_FNSPACE,
1106 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1107 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1108 TP_ACPI_HOTKEYSCAN_MUTE,
1109 TP_ACPI_HOTKEYSCAN_THINKPAD,
1110};
1111
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001112enum { /* Keys available through NVRAM polling */
1113 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1114 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1115};
1116
1117enum { /* Positions of some of the keys in hotkey masks */
1118 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1119 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1120 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1121 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1122 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1123 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1124 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1125 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1126 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1127 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1128 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1129};
1130
1131enum { /* NVRAM to ACPI HKEY group map */
1132 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1133 TP_ACPI_HKEY_ZOOM_MASK |
1134 TP_ACPI_HKEY_DISPSWTCH_MASK |
1135 TP_ACPI_HKEY_HIBERNATE_MASK,
1136 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1137 TP_ACPI_HKEY_BRGHTDWN_MASK,
1138 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1139 TP_ACPI_HKEY_VOLDWN_MASK |
1140 TP_ACPI_HKEY_MUTE_MASK,
1141};
1142
1143#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1144struct tp_nvram_state {
1145 u16 thinkpad_toggle:1;
1146 u16 zoom_toggle:1;
1147 u16 display_toggle:1;
1148 u16 thinklight_toggle:1;
1149 u16 hibernate_toggle:1;
1150 u16 displayexp_toggle:1;
1151 u16 display_state:1;
1152 u16 brightness_toggle:1;
1153 u16 volume_toggle:1;
1154 u16 mute:1;
1155
1156 u8 brightness_level;
1157 u8 volume_level;
1158};
1159
1160static struct task_struct *tpacpi_hotkey_task;
1161static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1162static int hotkey_poll_freq = 10; /* Hz */
1163static struct mutex hotkey_thread_mutex;
1164static struct mutex hotkey_thread_data_mutex;
1165static unsigned int hotkey_config_change;
1166
1167#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1168
1169#define hotkey_source_mask 0U
1170
1171#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1172
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001173static struct mutex hotkey_mutex;
1174
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001175static enum { /* Reasons for waking up */
1176 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1177 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1178 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1179} hotkey_wakeup_reason;
1180
1181static int hotkey_autosleep_ack;
1182
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001183static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001184static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001185static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001186static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001187static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001188
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001189static unsigned int hotkey_report_mode;
1190
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001191static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001192
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001193static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001194
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001195#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1196#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001197 do { \
1198 mutex_lock(&hotkey_thread_data_mutex); \
1199 hotkey_config_change++; \
1200 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001201#define HOTKEY_CONFIG_CRITICAL_END \
1202 mutex_unlock(&hotkey_thread_data_mutex);
1203#else
1204#define HOTKEY_CONFIG_CRITICAL_START
1205#define HOTKEY_CONFIG_CRITICAL_END
1206#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1207
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001208/* HKEY.MHKG() return bits */
1209#define TP_HOTKEY_TABLET_MASK (1 << 3)
1210
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001211static int hotkey_get_wlsw(int *status)
1212{
1213 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1214 return -EIO;
1215 return 0;
1216}
1217
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001218static int hotkey_get_tablet_mode(int *status)
1219{
1220 int s;
1221
1222 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1223 return -EIO;
1224
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001225 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1226 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001227}
1228
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001229/*
1230 * Call with hotkey_mutex held
1231 */
1232static int hotkey_mask_get(void)
1233{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001234 u32 m = 0;
1235
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001236 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001237 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001238 return -EIO;
1239 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001240 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001241
1242 return 0;
1243}
1244
1245/*
1246 * Call with hotkey_mutex held
1247 */
1248static int hotkey_mask_set(u32 mask)
1249{
1250 int i;
1251 int rc = 0;
1252
1253 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001254 if (!tp_warned.hotkey_mask_ff &&
1255 (mask == 0xffff || mask == 0xffffff ||
1256 mask == 0xffffffff)) {
1257 tp_warned.hotkey_mask_ff = 1;
1258 printk(TPACPI_NOTICE
1259 "setting the hotkey mask to 0x%08x is likely "
1260 "not the best way to go about it\n", mask);
1261 printk(TPACPI_NOTICE
1262 "please consider using the driver defaults, "
1263 "and refer to up-to-date thinkpad-acpi "
1264 "documentation\n");
1265 }
1266
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001267 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001268 for (i = 0; i < 32; i++) {
1269 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001270 /* enable in firmware mask only keys not in NVRAM
1271 * mode, but enable the key in the cached hotkey_mask
1272 * regardless of mode, or the key will end up
1273 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001274 if (!acpi_evalf(hkey_handle,
1275 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001276 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001277 rc = -EIO;
1278 break;
1279 } else {
1280 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1281 }
1282 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001283 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001284
1285 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001286 if (!hotkey_mask_get() && !rc &&
1287 (hotkey_mask & ~hotkey_source_mask) !=
1288 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001289 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001290 "requested hot key mask 0x%08x, but "
1291 "firmware forced it to 0x%08x\n",
1292 mask, hotkey_mask);
1293 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001294 } else {
1295#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1296 HOTKEY_CONFIG_CRITICAL_START
1297 hotkey_mask = mask & hotkey_source_mask;
1298 HOTKEY_CONFIG_CRITICAL_END
1299 hotkey_mask_get();
1300 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001301 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001302 "requested hot key mask 0x%08x, "
1303 "forced to 0x%08x (NVRAM poll mask is "
1304 "0x%08x): no firmware mask support\n",
1305 mask, hotkey_mask, hotkey_source_mask);
1306 }
1307#else
1308 hotkey_mask_get();
1309 rc = -ENXIO;
1310#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001311 }
1312
1313 return rc;
1314}
1315
1316static int hotkey_status_get(int *status)
1317{
1318 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1319 return -EIO;
1320
1321 return 0;
1322}
1323
1324static int hotkey_status_set(int status)
1325{
1326 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1327 return -EIO;
1328
1329 return 0;
1330}
1331
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001332static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001333{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001334 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001335
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001336 if (tp_features.hotkey_tablet &&
1337 !hotkey_get_tablet_mode(&state)) {
1338 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001339
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001340 input_report_switch(tpacpi_inputdev,
1341 SW_TABLET_MODE, !!state);
1342 input_sync(tpacpi_inputdev);
1343
1344 mutex_unlock(&tpacpi_inputdev_send_mutex);
1345 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001346}
1347
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001348static void tpacpi_input_send_key(unsigned int scancode)
1349{
1350 unsigned int keycode;
1351
1352 keycode = hotkey_keycode_map[scancode];
1353
1354 if (keycode != KEY_RESERVED) {
1355 mutex_lock(&tpacpi_inputdev_send_mutex);
1356
1357 input_report_key(tpacpi_inputdev, keycode, 1);
1358 if (keycode == KEY_UNKNOWN)
1359 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1360 scancode);
1361 input_sync(tpacpi_inputdev);
1362
1363 input_report_key(tpacpi_inputdev, keycode, 0);
1364 if (keycode == KEY_UNKNOWN)
1365 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1366 scancode);
1367 input_sync(tpacpi_inputdev);
1368
1369 mutex_unlock(&tpacpi_inputdev_send_mutex);
1370 }
1371}
1372
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001373#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1374static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1375
1376static void tpacpi_hotkey_send_key(unsigned int scancode)
1377{
1378 tpacpi_input_send_key(scancode);
1379 if (hotkey_report_mode < 2) {
1380 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1381 0x80, 0x1001 + scancode);
1382 }
1383}
1384
1385static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1386{
1387 u8 d;
1388
1389 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1390 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1391 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1392 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1393 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1394 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1395 }
1396 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1397 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1398 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1399 }
1400 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1401 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1402 n->displayexp_toggle =
1403 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1404 }
1405 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1406 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1407 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1408 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1409 n->brightness_toggle =
1410 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1411 }
1412 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1413 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1414 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1415 >> TP_NVRAM_POS_LEVEL_VOLUME;
1416 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1417 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1418 }
1419}
1420
1421#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001422 do { \
1423 if ((mask & (1 << __scancode)) && \
1424 oldn->__member != newn->__member) \
1425 tpacpi_hotkey_send_key(__scancode); \
1426 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001427
1428#define TPACPI_MAY_SEND_KEY(__scancode) \
1429 do { if (mask & (1 << __scancode)) \
1430 tpacpi_hotkey_send_key(__scancode); } while (0)
1431
1432static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001433 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001434 u32 mask)
1435{
1436 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1437 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1438 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1439 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1440
1441 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1442
1443 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1444
1445 /* handle volume */
1446 if (oldn->volume_toggle != newn->volume_toggle) {
1447 if (oldn->mute != newn->mute) {
1448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1449 }
1450 if (oldn->volume_level > newn->volume_level) {
1451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1452 } else if (oldn->volume_level < newn->volume_level) {
1453 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1454 } else if (oldn->mute == newn->mute) {
1455 /* repeated key presses that didn't change state */
1456 if (newn->mute) {
1457 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1458 } else if (newn->volume_level != 0) {
1459 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1460 } else {
1461 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1462 }
1463 }
1464 }
1465
1466 /* handle brightness */
1467 if (oldn->brightness_toggle != newn->brightness_toggle) {
1468 if (oldn->brightness_level < newn->brightness_level) {
1469 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1470 } else if (oldn->brightness_level > newn->brightness_level) {
1471 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1472 } else {
1473 /* repeated key presses that didn't change state */
1474 if (newn->brightness_level != 0) {
1475 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1476 } else {
1477 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1478 }
1479 }
1480 }
1481}
1482
1483#undef TPACPI_COMPARE_KEY
1484#undef TPACPI_MAY_SEND_KEY
1485
1486static int hotkey_kthread(void *data)
1487{
1488 struct tp_nvram_state s[2];
1489 u32 mask;
1490 unsigned int si, so;
1491 unsigned long t;
1492 unsigned int change_detector, must_reset;
1493
1494 mutex_lock(&hotkey_thread_mutex);
1495
1496 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1497 goto exit;
1498
1499 set_freezable();
1500
1501 so = 0;
1502 si = 1;
1503 t = 0;
1504
1505 /* Initial state for compares */
1506 mutex_lock(&hotkey_thread_data_mutex);
1507 change_detector = hotkey_config_change;
1508 mask = hotkey_source_mask & hotkey_mask;
1509 mutex_unlock(&hotkey_thread_data_mutex);
1510 hotkey_read_nvram(&s[so], mask);
1511
1512 while (!kthread_should_stop() && hotkey_poll_freq) {
1513 if (t == 0)
1514 t = 1000/hotkey_poll_freq;
1515 t = msleep_interruptible(t);
1516 if (unlikely(kthread_should_stop()))
1517 break;
1518 must_reset = try_to_freeze();
1519 if (t > 0 && !must_reset)
1520 continue;
1521
1522 mutex_lock(&hotkey_thread_data_mutex);
1523 if (must_reset || hotkey_config_change != change_detector) {
1524 /* forget old state on thaw or config change */
1525 si = so;
1526 t = 0;
1527 change_detector = hotkey_config_change;
1528 }
1529 mask = hotkey_source_mask & hotkey_mask;
1530 mutex_unlock(&hotkey_thread_data_mutex);
1531
1532 if (likely(mask)) {
1533 hotkey_read_nvram(&s[si], mask);
1534 if (likely(si != so)) {
1535 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001536 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001537 }
1538 }
1539
1540 so = si;
1541 si ^= 1;
1542 }
1543
1544exit:
1545 mutex_unlock(&hotkey_thread_mutex);
1546 return 0;
1547}
1548
1549static void hotkey_poll_stop_sync(void)
1550{
1551 if (tpacpi_hotkey_task) {
1552 if (frozen(tpacpi_hotkey_task) ||
1553 freezing(tpacpi_hotkey_task))
1554 thaw_process(tpacpi_hotkey_task);
1555
1556 kthread_stop(tpacpi_hotkey_task);
1557 tpacpi_hotkey_task = NULL;
1558 mutex_lock(&hotkey_thread_mutex);
1559 /* at this point, the thread did exit */
1560 mutex_unlock(&hotkey_thread_mutex);
1561 }
1562}
1563
1564/* call with hotkey_mutex held */
1565static void hotkey_poll_setup(int may_warn)
1566{
1567 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1568 hotkey_poll_freq > 0 &&
1569 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1570 if (!tpacpi_hotkey_task) {
1571 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001572 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001573 if (IS_ERR(tpacpi_hotkey_task)) {
1574 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001575 printk(TPACPI_ERR
1576 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001577 "for hotkey polling\n");
1578 }
1579 }
1580 } else {
1581 hotkey_poll_stop_sync();
1582 if (may_warn &&
1583 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001584 printk(TPACPI_NOTICE
1585 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001586 "which is currently disabled\n",
1587 hotkey_source_mask);
1588 }
1589 }
1590}
1591
1592static void hotkey_poll_setup_safe(int may_warn)
1593{
1594 mutex_lock(&hotkey_mutex);
1595 hotkey_poll_setup(may_warn);
1596 mutex_unlock(&hotkey_mutex);
1597}
1598
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001599#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1600
1601static void hotkey_poll_setup_safe(int __unused)
1602{
1603}
1604
1605#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1606
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001607static int hotkey_inputdev_open(struct input_dev *dev)
1608{
1609 switch (tpacpi_lifecycle) {
1610 case TPACPI_LIFE_INIT:
1611 /*
1612 * hotkey_init will call hotkey_poll_setup_safe
1613 * at the appropriate moment
1614 */
1615 return 0;
1616 case TPACPI_LIFE_EXITING:
1617 return -EBUSY;
1618 case TPACPI_LIFE_RUNNING:
1619 hotkey_poll_setup_safe(0);
1620 return 0;
1621 }
1622
1623 /* Should only happen if tpacpi_lifecycle is corrupt */
1624 BUG();
1625 return -EBUSY;
1626}
1627
1628static void hotkey_inputdev_close(struct input_dev *dev)
1629{
1630 /* disable hotkey polling when possible */
1631 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1632 hotkey_poll_setup_safe(0);
1633}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001634
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001635/* sysfs hotkey enable ------------------------------------------------- */
1636static ssize_t hotkey_enable_show(struct device *dev,
1637 struct device_attribute *attr,
1638 char *buf)
1639{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001640 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001641
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001642 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001643 if (res)
1644 return res;
1645
1646 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1647}
1648
1649static ssize_t hotkey_enable_store(struct device *dev,
1650 struct device_attribute *attr,
1651 const char *buf, size_t count)
1652{
1653 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001654 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001655
1656 if (parse_strtoul(buf, 1, &t))
1657 return -EINVAL;
1658
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001659 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001660
1661 return (res) ? res : count;
1662}
1663
1664static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001665 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001666 hotkey_enable_show, hotkey_enable_store);
1667
1668/* sysfs hotkey mask --------------------------------------------------- */
1669static ssize_t hotkey_mask_show(struct device *dev,
1670 struct device_attribute *attr,
1671 char *buf)
1672{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001673 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001674
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001675 if (mutex_lock_interruptible(&hotkey_mutex))
1676 return -ERESTARTSYS;
1677 res = hotkey_mask_get();
1678 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001679
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001680 return (res)?
1681 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001682}
1683
1684static ssize_t hotkey_mask_store(struct device *dev,
1685 struct device_attribute *attr,
1686 const char *buf, size_t count)
1687{
1688 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001689 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001690
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001691 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001692 return -EINVAL;
1693
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001694 if (mutex_lock_interruptible(&hotkey_mutex))
1695 return -ERESTARTSYS;
1696
1697 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001698
1699#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1700 hotkey_poll_setup(1);
1701#endif
1702
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001703 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001704
1705 return (res) ? res : count;
1706}
1707
1708static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001709 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001710 hotkey_mask_show, hotkey_mask_store);
1711
1712/* sysfs hotkey bios_enabled ------------------------------------------- */
1713static ssize_t hotkey_bios_enabled_show(struct device *dev,
1714 struct device_attribute *attr,
1715 char *buf)
1716{
1717 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1718}
1719
1720static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001721 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001722
1723/* sysfs hotkey bios_mask ---------------------------------------------- */
1724static ssize_t hotkey_bios_mask_show(struct device *dev,
1725 struct device_attribute *attr,
1726 char *buf)
1727{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001728 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001729}
1730
1731static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001732 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001733
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001734/* sysfs hotkey all_mask ----------------------------------------------- */
1735static ssize_t hotkey_all_mask_show(struct device *dev,
1736 struct device_attribute *attr,
1737 char *buf)
1738{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001739 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1740 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001741}
1742
1743static struct device_attribute dev_attr_hotkey_all_mask =
1744 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1745
1746/* sysfs hotkey recommended_mask --------------------------------------- */
1747static ssize_t hotkey_recommended_mask_show(struct device *dev,
1748 struct device_attribute *attr,
1749 char *buf)
1750{
1751 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001752 (hotkey_all_mask | hotkey_source_mask)
1753 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001754}
1755
1756static struct device_attribute dev_attr_hotkey_recommended_mask =
1757 __ATTR(hotkey_recommended_mask, S_IRUGO,
1758 hotkey_recommended_mask_show, NULL);
1759
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001760#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1761
1762/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1763static ssize_t hotkey_source_mask_show(struct device *dev,
1764 struct device_attribute *attr,
1765 char *buf)
1766{
1767 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1768}
1769
1770static ssize_t hotkey_source_mask_store(struct device *dev,
1771 struct device_attribute *attr,
1772 const char *buf, size_t count)
1773{
1774 unsigned long t;
1775
1776 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1777 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1778 return -EINVAL;
1779
1780 if (mutex_lock_interruptible(&hotkey_mutex))
1781 return -ERESTARTSYS;
1782
1783 HOTKEY_CONFIG_CRITICAL_START
1784 hotkey_source_mask = t;
1785 HOTKEY_CONFIG_CRITICAL_END
1786
1787 hotkey_poll_setup(1);
1788
1789 mutex_unlock(&hotkey_mutex);
1790
1791 return count;
1792}
1793
1794static struct device_attribute dev_attr_hotkey_source_mask =
1795 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1796 hotkey_source_mask_show, hotkey_source_mask_store);
1797
1798/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1799static ssize_t hotkey_poll_freq_show(struct device *dev,
1800 struct device_attribute *attr,
1801 char *buf)
1802{
1803 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1804}
1805
1806static ssize_t hotkey_poll_freq_store(struct device *dev,
1807 struct device_attribute *attr,
1808 const char *buf, size_t count)
1809{
1810 unsigned long t;
1811
1812 if (parse_strtoul(buf, 25, &t))
1813 return -EINVAL;
1814
1815 if (mutex_lock_interruptible(&hotkey_mutex))
1816 return -ERESTARTSYS;
1817
1818 hotkey_poll_freq = t;
1819
1820 hotkey_poll_setup(1);
1821 mutex_unlock(&hotkey_mutex);
1822
1823 return count;
1824}
1825
1826static struct device_attribute dev_attr_hotkey_poll_freq =
1827 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1828 hotkey_poll_freq_show, hotkey_poll_freq_store);
1829
1830#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1831
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001832/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001833static ssize_t hotkey_radio_sw_show(struct device *dev,
1834 struct device_attribute *attr,
1835 char *buf)
1836{
1837 int res, s;
1838 res = hotkey_get_wlsw(&s);
1839 if (res < 0)
1840 return res;
1841
1842 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1843}
1844
1845static struct device_attribute dev_attr_hotkey_radio_sw =
1846 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1847
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001848static void hotkey_radio_sw_notify_change(void)
1849{
1850 if (tp_features.hotkey_wlsw)
1851 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1852 "hotkey_radio_sw");
1853}
1854
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001855/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1856static ssize_t hotkey_tablet_mode_show(struct device *dev,
1857 struct device_attribute *attr,
1858 char *buf)
1859{
1860 int res, s;
1861 res = hotkey_get_tablet_mode(&s);
1862 if (res < 0)
1863 return res;
1864
1865 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1866}
1867
1868static struct device_attribute dev_attr_hotkey_tablet_mode =
1869 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1870
1871static void hotkey_tablet_mode_notify_change(void)
1872{
1873 if (tp_features.hotkey_tablet)
1874 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1875 "hotkey_tablet_mode");
1876}
1877
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001878/* sysfs hotkey report_mode -------------------------------------------- */
1879static ssize_t hotkey_report_mode_show(struct device *dev,
1880 struct device_attribute *attr,
1881 char *buf)
1882{
1883 return snprintf(buf, PAGE_SIZE, "%d\n",
1884 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1885}
1886
1887static struct device_attribute dev_attr_hotkey_report_mode =
1888 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1889
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001890/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001891static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1892 struct device_attribute *attr,
1893 char *buf)
1894{
1895 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1896}
1897
1898static struct device_attribute dev_attr_hotkey_wakeup_reason =
1899 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1900
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001901static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001902{
1903 if (tp_features.hotkey_mask)
1904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1905 "wakeup_reason");
1906}
1907
1908/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001909static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1910 struct device_attribute *attr,
1911 char *buf)
1912{
1913 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1914}
1915
1916static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1917 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1918 hotkey_wakeup_hotunplug_complete_show, NULL);
1919
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001920static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001921{
1922 if (tp_features.hotkey_mask)
1923 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1924 "wakeup_hotunplug_complete");
1925}
1926
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001927/* --------------------------------------------------------------------- */
1928
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001929static struct attribute *hotkey_attributes[] __initdata = {
1930 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001931 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001932 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001933#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1934 &dev_attr_hotkey_mask.attr,
1935 &dev_attr_hotkey_all_mask.attr,
1936 &dev_attr_hotkey_recommended_mask.attr,
1937 &dev_attr_hotkey_source_mask.attr,
1938 &dev_attr_hotkey_poll_freq.attr,
1939#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001940};
1941
1942static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001943 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001944#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1945 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001946 &dev_attr_hotkey_all_mask.attr,
1947 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001948#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001949 &dev_attr_hotkey_wakeup_reason.attr,
1950 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001951};
1952
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001953static void bluetooth_update_rfk(void);
1954static void wan_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03001955static void tpacpi_send_radiosw_update(void)
1956{
1957 int wlsw;
1958
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001959 /* Sync these BEFORE sending any rfkill events */
1960 if (tp_features.bluetooth)
1961 bluetooth_update_rfk();
1962 if (tp_features.wan)
1963 wan_update_rfk();
1964
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03001965 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1966 mutex_lock(&tpacpi_inputdev_send_mutex);
1967
1968 input_report_switch(tpacpi_inputdev,
1969 SW_RFKILL_ALL, !!wlsw);
1970 input_sync(tpacpi_inputdev);
1971
1972 mutex_unlock(&tpacpi_inputdev_send_mutex);
1973 }
1974 hotkey_radio_sw_notify_change();
1975}
1976
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03001977static void hotkey_exit(void)
1978{
1979#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1980 hotkey_poll_stop_sync();
1981#endif
1982
1983 if (hotkey_dev_attributes)
1984 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1985
1986 kfree(hotkey_keycode_map);
1987
1988 if (tp_features.hotkey) {
1989 dbg_printk(TPACPI_DBG_EXIT,
1990 "restoring original hot key mask\n");
1991 /* no short-circuit boolean operator below! */
1992 if ((hotkey_mask_set(hotkey_orig_mask) |
1993 hotkey_status_set(hotkey_orig_status)) != 0)
1994 printk(TPACPI_ERR
1995 "failed to restore hot key mask "
1996 "to BIOS defaults\n");
1997 }
1998}
1999
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002000static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002001{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002002 /* Requirements for changing the default keymaps:
2003 *
2004 * 1. Many of the keys are mapped to KEY_RESERVED for very
2005 * good reasons. Do not change them unless you have deep
2006 * knowledge on the IBM and Lenovo ThinkPad firmware for
2007 * the various ThinkPad models. The driver behaves
2008 * differently for KEY_RESERVED: such keys have their
2009 * hot key mask *unset* in mask_recommended, and also
2010 * in the initial hot key mask programmed into the
2011 * firmware at driver load time, which means the firm-
2012 * ware may react very differently if you change them to
2013 * something else;
2014 *
2015 * 2. You must be subscribed to the linux-thinkpad and
2016 * ibm-acpi-devel mailing lists, and you should read the
2017 * list archives since 2007 if you want to change the
2018 * keymaps. This requirement exists so that you will
2019 * know the past history of problems with the thinkpad-
2020 * acpi driver keymaps, and also that you will be
2021 * listening to any bug reports;
2022 *
2023 * 3. Do not send thinkpad-acpi specific patches directly to
2024 * for merging, *ever*. Send them to the linux-acpi
2025 * mailinglist for comments. Merging is to be done only
2026 * through acpi-test and the ACPI maintainer.
2027 *
2028 * If the above is too much to ask, don't change the keymap.
2029 * Ask the thinkpad-acpi maintainer to do it, instead.
2030 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002031 static u16 ibm_keycode_map[] __initdata = {
2032 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2033 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2034 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2035 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002036
2037 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002038 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2039 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2040 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002041
2042 /* brightness: firmware always reacts to them, unless
2043 * X.org did some tricks in the radeon BIOS scratch
2044 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002045 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002046 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002047
2048 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002049 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002050
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002051 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2052 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002053
2054 /* Volume: firmware always react to it and reprograms
2055 * the built-in *extra* mixer. Never map it to control
2056 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002057 KEY_RESERVED, /* 0x14: VOLUME UP */
2058 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2059 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002060
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002061 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002062
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002063 /* (assignments unknown, please report if found) */
2064 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2065 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2066 };
2067 static u16 lenovo_keycode_map[] __initdata = {
2068 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2069 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2070 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2071 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002072
2073 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002074 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2075 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2076 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002077
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002078 /* These either have to go through ACPI video, or
2079 * act like in the IBM ThinkPads, so don't ever
2080 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002081 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002082 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002083
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002084 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002085
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002086 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2087 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002088
2089 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2090 * react to it and reprograms the built-in *extra* mixer.
2091 * Never map it to control another mixer by default.
2092 *
2093 * T60?, T61, R60?, R61: firmware and EC tries to send
2094 * these over the regular keyboard, so these are no-ops,
2095 * but there are still weird bugs re. MUTE, so do not
2096 * change unless you get test reports from all Lenovo
2097 * models. May cause the BIOS to interfere with the
2098 * HDA mixer.
2099 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002100 KEY_RESERVED, /* 0x14: VOLUME UP */
2101 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2102 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002103
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002104 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002105
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002106 /* (assignments unknown, please report if found) */
2107 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2108 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2109 };
2110
2111#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2112#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2113#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2114
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002115 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002116 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002117 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002118
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002119 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2120
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002121 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002122 BUG_ON(tpacpi_inputdev->open != NULL ||
2123 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002124
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002125 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002126 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002127
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002128#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2129 mutex_init(&hotkey_thread_mutex);
2130 mutex_init(&hotkey_thread_data_mutex);
2131#endif
2132
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002133 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002134 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002135
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002136 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002137 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002138
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002139 if (!tp_features.hotkey)
2140 return 1;
2141
2142 hotkey_dev_attributes = create_attr_set(13, NULL);
2143 if (!hotkey_dev_attributes)
2144 return -ENOMEM;
2145 res = add_many_to_attr_set(hotkey_dev_attributes,
2146 hotkey_attributes,
2147 ARRAY_SIZE(hotkey_attributes));
2148 if (res)
2149 goto err_exit;
2150
2151 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2152 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2153 for HKEY interface version 0x100 */
2154 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2155 if ((hkeyv >> 8) != 1) {
2156 printk(TPACPI_ERR "unknown version of the "
2157 "HKEY interface: 0x%x\n", hkeyv);
2158 printk(TPACPI_ERR "please report this to %s\n",
2159 TPACPI_MAIL);
2160 } else {
2161 /*
2162 * MHKV 0x100 in A31, R40, R40e,
2163 * T4x, X31, and later
2164 */
2165 tp_features.hotkey_mask = 1;
2166 }
2167 }
2168
2169 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2170 str_supported(tp_features.hotkey_mask));
2171
2172 if (tp_features.hotkey_mask) {
2173 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2174 "MHKA", "qd")) {
2175 printk(TPACPI_ERR
2176 "missing MHKA handler, "
2177 "please report this to %s\n",
2178 TPACPI_MAIL);
2179 /* FN+F12, FN+F4, FN+F3 */
2180 hotkey_all_mask = 0x080cU;
2181 }
2182 }
2183
2184 /* hotkey_source_mask *must* be zero for
2185 * the first hotkey_mask_get */
2186 res = hotkey_status_get(&hotkey_orig_status);
2187 if (res)
2188 goto err_exit;
2189
2190 if (tp_features.hotkey_mask) {
2191 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002192 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002193 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002194
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002195 hotkey_orig_mask = hotkey_mask;
2196 res = add_many_to_attr_set(
2197 hotkey_dev_attributes,
2198 hotkey_mask_attributes,
2199 ARRAY_SIZE(hotkey_mask_attributes));
2200 if (res)
2201 goto err_exit;
2202 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002203
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002204#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002205 if (tp_features.hotkey_mask) {
2206 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2207 & ~hotkey_all_mask;
2208 } else {
2209 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2210 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002211
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002212 vdbg_printk(TPACPI_DBG_INIT,
2213 "hotkey source mask 0x%08x, polling freq %d\n",
2214 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002215#endif
2216
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002217 /* Not all thinkpads have a hardware radio switch */
2218 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2219 tp_features.hotkey_wlsw = 1;
2220 printk(TPACPI_INFO
2221 "radio switch found; radios are %s\n",
2222 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002223 }
2224 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002225 res = add_to_attr_set(hotkey_dev_attributes,
2226 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002227
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002228 /* For X41t, X60t, X61t Tablets... */
2229 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2230 tp_features.hotkey_tablet = 1;
2231 printk(TPACPI_INFO
2232 "possible tablet mode switch found; "
2233 "ThinkPad in %s mode\n",
2234 (status & TP_HOTKEY_TABLET_MASK)?
2235 "tablet" : "laptop");
2236 res = add_to_attr_set(hotkey_dev_attributes,
2237 &dev_attr_hotkey_tablet_mode.attr);
2238 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002239
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002240 if (!res)
2241 res = register_attr_set_with_sysfs(
2242 hotkey_dev_attributes,
2243 &tpacpi_pdev->dev.kobj);
2244 if (res)
2245 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002246
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002247 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002248
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002249 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2250 GFP_KERNEL);
2251 if (!hotkey_keycode_map) {
2252 printk(TPACPI_ERR
2253 "failed to allocate memory for key map\n");
2254 res = -ENOMEM;
2255 goto err_exit;
2256 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002257
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002258 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2259 dbg_printk(TPACPI_DBG_INIT,
2260 "using Lenovo default hot key map\n");
2261 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2262 TPACPI_HOTKEY_MAP_SIZE);
2263 } else {
2264 dbg_printk(TPACPI_DBG_INIT,
2265 "using IBM default hot key map\n");
2266 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2267 TPACPI_HOTKEY_MAP_SIZE);
2268 }
2269
2270 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2271 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2272 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2273 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2274 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2275 tpacpi_inputdev->keycode = hotkey_keycode_map;
2276 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2277 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2278 set_bit(hotkey_keycode_map[i],
2279 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002280 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002281 if (i < sizeof(hotkey_reserved_mask)*8)
2282 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002283 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002284 }
2285
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002286 if (tp_features.hotkey_wlsw) {
2287 set_bit(EV_SW, tpacpi_inputdev->evbit);
2288 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2289 }
2290 if (tp_features.hotkey_tablet) {
2291 set_bit(EV_SW, tpacpi_inputdev->evbit);
2292 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002293 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002294
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002295 /* Do not issue duplicate brightness change events to
2296 * userspace */
2297 if (!tp_features.bright_acpimode)
2298 /* update bright_acpimode... */
2299 tpacpi_check_std_acpi_brightness_support();
2300
2301 if (tp_features.bright_acpimode) {
2302 printk(TPACPI_INFO
2303 "This ThinkPad has standard ACPI backlight "
2304 "brightness control, supported by the ACPI "
2305 "video driver\n");
2306 printk(TPACPI_NOTICE
2307 "Disabling thinkpad-acpi brightness events "
2308 "by default...\n");
2309
2310 /* The hotkey_reserved_mask change below is not
2311 * necessary while the keys are at KEY_RESERVED in the
2312 * default map, but better safe than sorry, leave it
2313 * here as a marker of what we have to do, especially
2314 * when we finally become able to set this at runtime
2315 * on response to X.org requests */
2316 hotkey_reserved_mask |=
2317 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2318 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002319 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002320
2321 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2322 res = hotkey_status_set(1);
2323 if (res) {
2324 hotkey_exit();
2325 return res;
2326 }
2327 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2328 & ~hotkey_reserved_mask)
2329 | hotkey_orig_mask);
2330 if (res < 0 && res != -ENXIO) {
2331 hotkey_exit();
2332 return res;
2333 }
2334
2335 dbg_printk(TPACPI_DBG_INIT,
2336 "legacy hot key reporting over procfs %s\n",
2337 (hotkey_report_mode < 2) ?
2338 "enabled" : "disabled");
2339
2340 tpacpi_inputdev->open = &hotkey_inputdev_open;
2341 tpacpi_inputdev->close = &hotkey_inputdev_close;
2342
2343 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002344 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002345 tpacpi_input_send_tabletsw();
2346
2347 return 0;
2348
2349err_exit:
2350 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2351 hotkey_dev_attributes = NULL;
2352
2353 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002354}
2355
2356static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2357{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002358 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002359 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002360 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002361 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002362 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002363
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002364 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002365 printk(TPACPI_ERR
2366 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002367 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002368 acpi_bus_generate_netlink_event(
2369 ibm->acpi->device->pnp.device_class,
2370 ibm->acpi->device->dev.bus_id,
2371 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002372 return;
2373 }
2374
2375 while (1) {
2376 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002377 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002378 return;
2379 }
2380
2381 if (hkey == 0) {
2382 /* queue empty */
2383 return;
2384 }
2385
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002386 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002387 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002388 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002389
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002390 switch (hkey >> 12) {
2391 case 1:
2392 /* 0x1000-0x1FFF: key presses */
2393 scancode = hkey & 0xfff;
2394 if (scancode > 0 && scancode < 0x21) {
2395 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002396 if (!(hotkey_source_mask & (1 << scancode))) {
2397 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002398 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002399 } else {
2400 ignore_acpi_ev = 1;
2401 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002402 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002403 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002404 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002405 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002406 case 2:
2407 /* Wakeup reason */
2408 switch (hkey) {
2409 case 0x2304: /* suspend, undock */
2410 case 0x2404: /* hibernation, undock */
2411 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2412 ignore_acpi_ev = 1;
2413 break;
2414 case 0x2305: /* suspend, bay eject */
2415 case 0x2405: /* hibernation, bay eject */
2416 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2417 ignore_acpi_ev = 1;
2418 break;
2419 default:
2420 unk_ev = 1;
2421 }
2422 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2423 printk(TPACPI_INFO
2424 "woke up due to a hot-unplug "
2425 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002426 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002427 }
2428 break;
2429 case 3:
2430 /* bay-related wakeups */
2431 if (hkey == 0x3003) {
2432 hotkey_autosleep_ack = 1;
2433 printk(TPACPI_INFO
2434 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002435 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002436 } else {
2437 unk_ev = 1;
2438 }
2439 break;
2440 case 4:
2441 /* dock-related wakeups */
2442 if (hkey == 0x4003) {
2443 hotkey_autosleep_ack = 1;
2444 printk(TPACPI_INFO
2445 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002446 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002447 } else {
2448 unk_ev = 1;
2449 }
2450 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002451 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002452 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002453 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002454 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002455 case 0x500b: /* X61t: tablet pen inserted into bay */
2456 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002457 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002458 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2459 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2460 tpacpi_input_send_tabletsw();
2461 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002462 send_acpi_ev = 0;
2463 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002464 case 0x5001:
2465 case 0x5002:
2466 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002467 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002468 break;
2469 default:
2470 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002471 }
2472 break;
2473 case 7:
2474 /* 0x7000-0x7FFF: misc */
2475 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002476 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002477 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002478 break;
2479 }
2480 /* fallthrough to default */
2481 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002482 unk_ev = 1;
2483 }
2484 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002485 printk(TPACPI_NOTICE
2486 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002487 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002488
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002489 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002490 if (!ignore_acpi_ev &&
2491 (send_acpi_ev || hotkey_report_mode < 2)) {
2492 acpi_bus_generate_proc_event(ibm->acpi->device,
2493 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002494 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002495
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002496 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002497 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002498 acpi_bus_generate_netlink_event(
2499 ibm->acpi->device->pnp.device_class,
2500 ibm->acpi->device->dev.bus_id,
2501 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002502 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002503 }
2504}
2505
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002506static void hotkey_suspend(pm_message_t state)
2507{
2508 /* Do these on suspend, we get the events on early resume! */
2509 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2510 hotkey_autosleep_ack = 0;
2511}
2512
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002513static void hotkey_resume(void)
2514{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002515 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002516 printk(TPACPI_ERR
2517 "error while trying to read hot key mask "
2518 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002519 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002520 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002521 hotkey_wakeup_reason_notify_change();
2522 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002523 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002524}
2525
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002526/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002527static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002529 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 int len = 0;
2531
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002532 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002533 len += sprintf(p + len, "status:\t\tnot supported\n");
2534 return len;
2535 }
2536
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002537 if (mutex_lock_interruptible(&hotkey_mutex))
2538 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002539 res = hotkey_status_get(&status);
2540 if (!res)
2541 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002542 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002543 if (res)
2544 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
2546 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002547 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002548 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 len += sprintf(p + len,
2550 "commands:\tenable, disable, reset, <mask>\n");
2551 } else {
2552 len += sprintf(p + len, "mask:\t\tnot supported\n");
2553 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2554 }
2555
2556 return len;
2557}
2558
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002559static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002561 int res, status;
2562 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002565 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 return -ENODEV;
2567
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002568 if (mutex_lock_interruptible(&hotkey_mutex))
2569 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002570
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002571 status = -1;
2572 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002573
2574 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 while ((cmd = next_cmd(&buf))) {
2576 if (strlencmp(cmd, "enable") == 0) {
2577 status = 1;
2578 } else if (strlencmp(cmd, "disable") == 0) {
2579 status = 0;
2580 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002581 status = hotkey_orig_status;
2582 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2584 /* mask set */
2585 } else if (sscanf(cmd, "%x", &mask) == 1) {
2586 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002587 } else {
2588 res = -EINVAL;
2589 goto errexit;
2590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002592 if (status != -1)
2593 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002595 if (!res && mask != hotkey_mask)
2596 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002598errexit:
2599 mutex_unlock(&hotkey_mutex);
2600 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002603static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002604 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002605 {"", 0},
2606};
2607
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002608static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002609 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002610 .notify = hotkey_notify,
2611 .handle = &hkey_handle,
2612 .type = ACPI_DEVICE_NOTIFY,
2613};
2614
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002615static struct ibm_struct hotkey_driver_data = {
2616 .name = "hotkey",
2617 .read = hotkey_read,
2618 .write = hotkey_write,
2619 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002620 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002621 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002622 .acpi = &ibm_hotkey_acpidriver,
2623};
2624
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002625/*************************************************************************
2626 * Bluetooth subdriver
2627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002629enum {
2630 /* ACPI GBDC/SBDC bits */
2631 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2632 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2633 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2634};
2635
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002636static struct rfkill *tpacpi_bluetooth_rfkill;
2637
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002638static int bluetooth_get_radiosw(void)
2639{
2640 int status;
2641
2642 if (!tp_features.bluetooth)
2643 return -ENODEV;
2644
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002645 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2646 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002647 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002648
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002649 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2650 return -EIO;
2651
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002652 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2653 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002654}
2655
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002656static void bluetooth_update_rfk(void)
2657{
2658 int status;
2659
2660 if (!tpacpi_bluetooth_rfkill)
2661 return;
2662
2663 status = bluetooth_get_radiosw();
2664 if (status < 0)
2665 return;
2666 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2667}
2668
2669static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002670{
2671 int status;
2672
2673 if (!tp_features.bluetooth)
2674 return -ENODEV;
2675
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002676 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2677 * reason to risk weird behaviour. */
2678 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2679 && radio_on)
2680 return -EPERM;
2681
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002682 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2683 return -EIO;
2684 if (radio_on)
2685 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2686 else
2687 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2688 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2689 return -EIO;
2690
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002691 if (update_rfk)
2692 bluetooth_update_rfk();
2693
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002694 return 0;
2695}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002696
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002697/* sysfs bluetooth enable ---------------------------------------------- */
2698static ssize_t bluetooth_enable_show(struct device *dev,
2699 struct device_attribute *attr,
2700 char *buf)
2701{
2702 int status;
2703
2704 status = bluetooth_get_radiosw();
2705 if (status < 0)
2706 return status;
2707
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002708 return snprintf(buf, PAGE_SIZE, "%d\n",
2709 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002710}
2711
2712static ssize_t bluetooth_enable_store(struct device *dev,
2713 struct device_attribute *attr,
2714 const char *buf, size_t count)
2715{
2716 unsigned long t;
2717 int res;
2718
2719 if (parse_strtoul(buf, 1, &t))
2720 return -EINVAL;
2721
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002722 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002723
2724 return (res) ? res : count;
2725}
2726
2727static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002728 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002729 bluetooth_enable_show, bluetooth_enable_store);
2730
2731/* --------------------------------------------------------------------- */
2732
2733static struct attribute *bluetooth_attributes[] = {
2734 &dev_attr_bluetooth_enable.attr,
2735 NULL
2736};
2737
2738static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002739 .attrs = bluetooth_attributes,
2740};
2741
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002742static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2743{
2744 int bts = bluetooth_get_radiosw();
2745
2746 if (bts < 0)
2747 return bts;
2748
2749 *state = bts;
2750 return 0;
2751}
2752
2753static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2754{
2755 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2756}
2757
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002758static void bluetooth_exit(void)
2759{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002760 if (tpacpi_bluetooth_rfkill)
2761 rfkill_unregister(tpacpi_bluetooth_rfkill);
2762
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002763 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2764 &bluetooth_attr_group);
2765}
2766
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002767static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002769 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002770 int status = 0;
2771
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002772 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2773
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002774 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002775
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002776 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2777 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002778 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002779 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002781 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2782 str_supported(tp_features.bluetooth),
2783 status);
2784
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002785 if (tp_features.bluetooth &&
2786 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2787 /* no bluetooth hardware present in system */
2788 tp_features.bluetooth = 0;
2789 dbg_printk(TPACPI_DBG_INIT,
2790 "bluetooth hardware not installed\n");
2791 }
2792
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002793 if (!tp_features.bluetooth)
2794 return 1;
2795
2796 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002797 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002798 if (res)
2799 return res;
2800
2801 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2802 &tpacpi_bluetooth_rfkill,
2803 RFKILL_TYPE_BLUETOOTH,
2804 "tpacpi_bluetooth_sw",
2805 tpacpi_bluetooth_rfk_set,
2806 tpacpi_bluetooth_rfk_get);
2807 if (res) {
2808 bluetooth_exit();
2809 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002810 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002811
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813}
2814
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002815/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002816static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
2818 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002819 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002821 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002824 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002825 (status == RFKILL_STATE_UNBLOCKED) ?
2826 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 len += sprintf(p + len, "commands:\tenable, disable\n");
2828 }
2829
2830 return len;
2831}
2832
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002833static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002837 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002838 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 while ((cmd = next_cmd(&buf))) {
2841 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002842 bluetooth_set_radiosw(1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002844 bluetooth_set_radiosw(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 } else
2846 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 }
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return 0;
2850}
2851
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002852static struct ibm_struct bluetooth_driver_data = {
2853 .name = "bluetooth",
2854 .read = bluetooth_read,
2855 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002856 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002857};
2858
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002859/*************************************************************************
2860 * Wan subdriver
2861 */
2862
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002863enum {
2864 /* ACPI GWAN/SWAN bits */
2865 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2866 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2867 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2868};
2869
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002870static struct rfkill *tpacpi_wan_rfkill;
2871
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002872static int wan_get_radiosw(void)
2873{
2874 int status;
2875
2876 if (!tp_features.wan)
2877 return -ENODEV;
2878
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002879 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2880 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002881 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002882
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002883 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2884 return -EIO;
2885
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002886 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
2887 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002888}
2889
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002890static void wan_update_rfk(void)
2891{
2892 int status;
2893
2894 if (!tpacpi_wan_rfkill)
2895 return;
2896
2897 status = wan_get_radiosw();
2898 if (status < 0)
2899 return;
2900 rfkill_force_state(tpacpi_wan_rfkill, status);
2901}
2902
2903static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002904{
2905 int status;
2906
2907 if (!tp_features.wan)
2908 return -ENODEV;
2909
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002910 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2911 * reason to risk weird behaviour. */
2912 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2913 && radio_on)
2914 return -EPERM;
2915
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002916 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2917 return -EIO;
2918 if (radio_on)
2919 status |= TP_ACPI_WANCARD_RADIOSSW;
2920 else
2921 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2922 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2923 return -EIO;
2924
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002925 if (update_rfk)
2926 wan_update_rfk();
2927
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002928 return 0;
2929}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002930
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002931/* sysfs wan enable ---------------------------------------------------- */
2932static ssize_t wan_enable_show(struct device *dev,
2933 struct device_attribute *attr,
2934 char *buf)
2935{
2936 int status;
2937
2938 status = wan_get_radiosw();
2939 if (status < 0)
2940 return status;
2941
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002942 return snprintf(buf, PAGE_SIZE, "%d\n",
2943 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002944}
2945
2946static ssize_t wan_enable_store(struct device *dev,
2947 struct device_attribute *attr,
2948 const char *buf, size_t count)
2949{
2950 unsigned long t;
2951 int res;
2952
2953 if (parse_strtoul(buf, 1, &t))
2954 return -EINVAL;
2955
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002956 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002957
2958 return (res) ? res : count;
2959}
2960
2961static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002962 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002963 wan_enable_show, wan_enable_store);
2964
2965/* --------------------------------------------------------------------- */
2966
2967static struct attribute *wan_attributes[] = {
2968 &dev_attr_wan_enable.attr,
2969 NULL
2970};
2971
2972static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002973 .attrs = wan_attributes,
2974};
2975
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002976static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2977{
2978 int wans = wan_get_radiosw();
2979
2980 if (wans < 0)
2981 return wans;
2982
2983 *state = wans;
2984 return 0;
2985}
2986
2987static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
2988{
2989 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2990}
2991
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002992static void wan_exit(void)
2993{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002994 if (tpacpi_wan_rfkill)
2995 rfkill_unregister(tpacpi_wan_rfkill);
2996
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002997 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2998 &wan_attr_group);
2999}
3000
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003001static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003002{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003003 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003004 int status = 0;
3005
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003006 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3007
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003008 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003009
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003010 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003011 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003012
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003013 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3014 str_supported(tp_features.wan),
3015 status);
3016
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003017 if (tp_features.wan &&
3018 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3019 /* no wan hardware present in system */
3020 tp_features.wan = 0;
3021 dbg_printk(TPACPI_DBG_INIT,
3022 "wan hardware not installed\n");
3023 }
3024
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003025 if (!tp_features.wan)
3026 return 1;
3027
3028 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003029 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003030 if (res)
3031 return res;
3032
3033 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3034 &tpacpi_wan_rfkill,
3035 RFKILL_TYPE_WWAN,
3036 "tpacpi_wwan_sw",
3037 tpacpi_wan_rfk_set,
3038 tpacpi_wan_rfk_get);
3039 if (res) {
3040 wan_exit();
3041 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003042 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003043
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003044 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003045}
3046
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003047/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003048static int wan_read(char *p)
3049{
3050 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003051 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003052
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003053 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003054 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003055 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003056 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003057 (status == RFKILL_STATE_UNBLOCKED) ?
3058 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003059 len += sprintf(p + len, "commands:\tenable, disable\n");
3060 }
3061
3062 return len;
3063}
3064
3065static int wan_write(char *buf)
3066{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003067 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003068
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003069 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003070 return -ENODEV;
3071
3072 while ((cmd = next_cmd(&buf))) {
3073 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003074 wan_set_radiosw(1, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003075 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003076 wan_set_radiosw(0, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003077 } else
3078 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003079 }
3080
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003081 return 0;
3082}
3083
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003084static struct ibm_struct wan_driver_data = {
3085 .name = "wan",
3086 .read = wan_read,
3087 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003088 .exit = wan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003089};
3090
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003091/*************************************************************************
3092 * Video subdriver
3093 */
3094
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003095#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3096
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003097enum video_access_mode {
3098 TPACPI_VIDEO_NONE = 0,
3099 TPACPI_VIDEO_570, /* 570 */
3100 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3101 TPACPI_VIDEO_NEW, /* all others */
3102};
3103
3104enum { /* video status flags, based on VIDEO_570 */
3105 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3106 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3107 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3108};
3109
3110enum { /* TPACPI_VIDEO_570 constants */
3111 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3112 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3113 * video_status_flags */
3114 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3115 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3116};
3117
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003118static enum video_access_mode video_supported;
3119static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003120
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003121static int video_autosw_get(void);
3122static int video_autosw_set(int enable);
3123
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003124TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003125
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003126static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003128 int ivga;
3129
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003130 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3131
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003132 TPACPI_ACPIHANDLE_INIT(vid);
3133 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003134
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003135 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3136 /* G41, assume IVGA doesn't change */
3137 vid_handle = vid2_handle;
3138
3139 if (!vid_handle)
3140 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003141 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003142 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3143 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003144 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003145 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3146 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003147 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003148 else
3149 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003150 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003152 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3153 str_supported(video_supported != TPACPI_VIDEO_NONE),
3154 video_supported);
3155
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003156 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157}
3158
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003159static void video_exit(void)
3160{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003161 dbg_printk(TPACPI_DBG_EXIT,
3162 "restoring original video autoswitch mode\n");
3163 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003164 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003165 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003166}
3167
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003168static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
3170 int status = 0;
3171 int i;
3172
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003173 switch (video_supported) {
3174 case TPACPI_VIDEO_570:
3175 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3176 TP_ACPI_VIDEO_570_PHSCMD))
3177 return -EIO;
3178 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3179 break;
3180 case TPACPI_VIDEO_770:
3181 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3182 return -EIO;
3183 if (i)
3184 status |= TP_ACPI_VIDEO_S_LCD;
3185 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3186 return -EIO;
3187 if (i)
3188 status |= TP_ACPI_VIDEO_S_CRT;
3189 break;
3190 case TPACPI_VIDEO_NEW:
3191 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3192 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3193 return -EIO;
3194 if (i)
3195 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003197 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3198 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3199 return -EIO;
3200 if (i)
3201 status |= TP_ACPI_VIDEO_S_LCD;
3202 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3203 return -EIO;
3204 if (i)
3205 status |= TP_ACPI_VIDEO_S_DVI;
3206 break;
3207 default:
3208 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 return status;
3212}
3213
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003214static int video_outputsw_set(int status)
3215{
3216 int autosw;
3217 int res = 0;
3218
3219 switch (video_supported) {
3220 case TPACPI_VIDEO_570:
3221 res = acpi_evalf(NULL, NULL,
3222 "\\_SB.PHS2", "vdd",
3223 TP_ACPI_VIDEO_570_PHS2CMD,
3224 status | TP_ACPI_VIDEO_570_PHS2SET);
3225 break;
3226 case TPACPI_VIDEO_770:
3227 autosw = video_autosw_get();
3228 if (autosw < 0)
3229 return autosw;
3230
3231 res = video_autosw_set(1);
3232 if (res)
3233 return res;
3234 res = acpi_evalf(vid_handle, NULL,
3235 "ASWT", "vdd", status * 0x100, 0);
3236 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003237 printk(TPACPI_ERR
3238 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003239 return -EIO;
3240 }
3241 break;
3242 case TPACPI_VIDEO_NEW:
3243 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003244 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003245 break;
3246 default:
3247 return -ENOSYS;
3248 }
3249
3250 return (res)? 0 : -EIO;
3251}
3252
3253static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003255 int autosw = 0;
3256
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003257 switch (video_supported) {
3258 case TPACPI_VIDEO_570:
3259 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3260 return -EIO;
3261 break;
3262 case TPACPI_VIDEO_770:
3263 case TPACPI_VIDEO_NEW:
3264 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3265 return -EIO;
3266 break;
3267 default:
3268 return -ENOSYS;
3269 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003270
3271 return autosw & 1;
3272}
3273
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003274static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003275{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003276 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003277 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003278 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003279}
3280
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003281static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003282{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003283 int autosw = video_autosw_get();
3284 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003285
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003286 if (autosw < 0)
3287 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003288
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003289 switch (video_supported) {
3290 case TPACPI_VIDEO_570:
3291 res = video_autosw_set(1);
3292 if (res)
3293 return res;
3294 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3295 break;
3296 case TPACPI_VIDEO_770:
3297 case TPACPI_VIDEO_NEW:
3298 res = video_autosw_set(1);
3299 if (res)
3300 return res;
3301 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3302 break;
3303 default:
3304 return -ENOSYS;
3305 }
3306 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003307 printk(TPACPI_ERR
3308 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003309 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003310 }
3311
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003312 return (res)? 0 : -EIO;
3313}
3314
3315static int video_expand_toggle(void)
3316{
3317 switch (video_supported) {
3318 case TPACPI_VIDEO_570:
3319 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3320 0 : -EIO;
3321 case TPACPI_VIDEO_770:
3322 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3323 0 : -EIO;
3324 case TPACPI_VIDEO_NEW:
3325 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3326 0 : -EIO;
3327 default:
3328 return -ENOSYS;
3329 }
3330 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003331}
3332
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003333static int video_read(char *p)
3334{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003335 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003336 int len = 0;
3337
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003338 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003339 len += sprintf(p + len, "status:\t\tnot supported\n");
3340 return len;
3341 }
3342
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003343 status = video_outputsw_get();
3344 if (status < 0)
3345 return status;
3346
3347 autosw = video_autosw_get();
3348 if (autosw < 0)
3349 return autosw;
3350
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003351 len += sprintf(p + len, "status:\t\tsupported\n");
3352 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3353 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003354 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003355 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3356 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3357 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3358 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003359 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003360 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3361 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3362 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3363
3364 return len;
3365}
3366
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003367static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368{
3369 char *cmd;
3370 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003371 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003373 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003374 return -ENODEV;
3375
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003376 enable = 0;
3377 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
3379 while ((cmd = next_cmd(&buf))) {
3380 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003381 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003383 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003385 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003387 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003388 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003389 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003390 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003391 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003392 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003393 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003395 res = video_autosw_set(1);
3396 if (res)
3397 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003399 res = video_autosw_set(0);
3400 if (res)
3401 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003403 res = video_outputsw_cycle();
3404 if (res)
3405 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003407 res = video_expand_toggle();
3408 if (res)
3409 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 } else
3411 return -EINVAL;
3412 }
3413
3414 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003415 status = video_outputsw_get();
3416 if (status < 0)
3417 return status;
3418 res = video_outputsw_set((status & ~disable) | enable);
3419 if (res)
3420 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 }
3422
3423 return 0;
3424}
3425
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003426static struct ibm_struct video_driver_data = {
3427 .name = "video",
3428 .read = video_read,
3429 .write = video_write,
3430 .exit = video_exit,
3431};
3432
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003433#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3434
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003435/*************************************************************************
3436 * Light (thinklight) subdriver
3437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003439TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3440TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003441
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003442static int light_get_status(void)
3443{
3444 int status = 0;
3445
3446 if (tp_features.light_status) {
3447 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3448 return -EIO;
3449 return (!!status);
3450 }
3451
3452 return -ENXIO;
3453}
3454
3455static int light_set_status(int status)
3456{
3457 int rc;
3458
3459 if (tp_features.light) {
3460 if (cmos_handle) {
3461 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3462 (status)?
3463 TP_CMOS_THINKLIGHT_ON :
3464 TP_CMOS_THINKLIGHT_OFF);
3465 } else {
3466 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3467 (status)? 1 : 0);
3468 }
3469 return (rc)? 0 : -EIO;
3470 }
3471
3472 return -ENXIO;
3473}
3474
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003475static void light_set_status_worker(struct work_struct *work)
3476{
3477 struct tpacpi_led_classdev *data =
3478 container_of(work, struct tpacpi_led_classdev, work);
3479
3480 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3481 light_set_status((data->new_brightness != LED_OFF));
3482}
3483
3484static void light_sysfs_set(struct led_classdev *led_cdev,
3485 enum led_brightness brightness)
3486{
3487 struct tpacpi_led_classdev *data =
3488 container_of(led_cdev,
3489 struct tpacpi_led_classdev,
3490 led_classdev);
3491 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003492 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003493}
3494
3495static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3496{
3497 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3498}
3499
3500static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3501 .led_classdev = {
3502 .name = "tpacpi::thinklight",
3503 .brightness_set = &light_sysfs_set,
3504 .brightness_get = &light_sysfs_get,
3505 }
3506};
3507
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003508static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003510 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003511
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003512 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3513
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003514 TPACPI_ACPIHANDLE_INIT(ledb);
3515 TPACPI_ACPIHANDLE_INIT(lght);
3516 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003517 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003518
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003519 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003520 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003521
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003522 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003523 /* light status not supported on
3524 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003525 tp_features.light_status =
3526 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003528 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3529 str_supported(tp_features.light),
3530 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003531
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003532 if (!tp_features.light)
3533 return 1;
3534
3535 rc = led_classdev_register(&tpacpi_pdev->dev,
3536 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003537
3538 if (rc < 0) {
3539 tp_features.light = 0;
3540 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003541 } else {
3542 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003543 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003544
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003545 return rc;
3546}
3547
3548static void light_exit(void)
3549{
3550 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3551 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003552 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553}
3554
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003555static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003558 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003560 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003561 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003562 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003563 len += sprintf(p + len, "status:\t\tunknown\n");
3564 len += sprintf(p + len, "commands:\ton, off\n");
3565 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003566 status = light_get_status();
3567 if (status < 0)
3568 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003570 len += sprintf(p + len, "commands:\ton, off\n");
3571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
3573 return len;
3574}
3575
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003576static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003579 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003580
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003581 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003582 return -ENODEV;
3583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 while ((cmd = next_cmd(&buf))) {
3585 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003586 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003588 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 } else
3590 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 }
3592
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003593 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003596static struct ibm_struct light_driver_data = {
3597 .name = "light",
3598 .read = light_read,
3599 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003600 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003601};
3602
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003603/*************************************************************************
3604 * Dock subdriver
3605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003607#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003608
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003609static void dock_notify(struct ibm_struct *ibm, u32 event);
3610static int dock_read(char *p);
3611static int dock_write(char *buf);
3612
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003613TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003614 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3615 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3616 "\\_SB.PCI.ISA.SLCE", /* 570 */
3617 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3618
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003619/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003620TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003621
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003622static const struct acpi_device_id ibm_pci_device_ids[] = {
3623 {PCI_ROOT_HID_STRING, 0},
3624 {"", 0},
3625};
3626
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003627static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3628 {
3629 .notify = dock_notify,
3630 .handle = &dock_handle,
3631 .type = ACPI_SYSTEM_NOTIFY,
3632 },
3633 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003634 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3635 * We just use it to get notifications of dock hotplug
3636 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003637 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003638 .notify = dock_notify,
3639 .handle = &pci_handle,
3640 .type = ACPI_SYSTEM_NOTIFY,
3641 },
3642};
3643
3644static struct ibm_struct dock_driver_data[2] = {
3645 {
3646 .name = "dock",
3647 .read = dock_read,
3648 .write = dock_write,
3649 .acpi = &ibm_dock_acpidriver[0],
3650 },
3651 {
3652 .name = "dock",
3653 .acpi = &ibm_dock_acpidriver[1],
3654 },
3655};
3656
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657#define dock_docked() (_sta(dock_handle) & 1)
3658
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003659static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003660{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003661 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3662
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003663 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003664
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003665 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3666 str_supported(dock_handle != NULL));
3667
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003668 return (dock_handle)? 0 : 1;
3669}
3670
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003671static int __init dock_init2(struct ibm_init_struct *iibm)
3672{
3673 int dock2_needed;
3674
3675 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3676
3677 if (dock_driver_data[0].flags.acpi_driver_registered &&
3678 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003679 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003680 dock2_needed = (pci_handle != NULL);
3681 vdbg_printk(TPACPI_DBG_INIT,
3682 "dock PCI handler for the TP 570 is %s\n",
3683 str_supported(dock2_needed));
3684 } else {
3685 vdbg_printk(TPACPI_DBG_INIT,
3686 "dock subdriver part 2 not required\n");
3687 dock2_needed = 0;
3688 }
3689
3690 return (dock2_needed)? 0 : 1;
3691}
3692
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003693static void dock_notify(struct ibm_struct *ibm, u32 event)
3694{
3695 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003696 int pci = ibm->acpi->hid && ibm->acpi->device &&
3697 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003698 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003699
3700 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003701 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003702 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003703 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003704 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003705 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003706 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003707 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003708 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003709 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003710 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003711 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003712 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003713 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003714 }
Len Brown14e04fb2007-08-23 15:20:26 -04003715 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003716 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3717 ibm->acpi->device->dev.bus_id,
3718 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003719}
3720
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003721static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722{
3723 int len = 0;
3724 int docked = dock_docked();
3725
3726 if (!dock_handle)
3727 len += sprintf(p + len, "status:\t\tnot supported\n");
3728 else if (!docked)
3729 len += sprintf(p + len, "status:\t\tundocked\n");
3730 else {
3731 len += sprintf(p + len, "status:\t\tdocked\n");
3732 len += sprintf(p + len, "commands:\tdock, undock\n");
3733 }
3734
3735 return len;
3736}
3737
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003738static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
3740 char *cmd;
3741
3742 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003743 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745 while ((cmd = next_cmd(&buf))) {
3746 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003747 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3748 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return -EIO;
3750 } else if (strlencmp(cmd, "dock") == 0) {
3751 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3752 return -EIO;
3753 } else
3754 return -EINVAL;
3755 }
3756
3757 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003760#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003762/*************************************************************************
3763 * Bay subdriver
3764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003766#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003767
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003768TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003769 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3770 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3771 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3772 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003773TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003774 "_EJ0", /* all others */
3775 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003776TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003777 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3778 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003779TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003780 "_EJ0", /* 770x */
3781 ); /* all others */
3782
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003783static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003785 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3786
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003787 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003788 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003789 TPACPI_ACPIHANDLE_INIT(bay_ej);
3790 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003791 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003792 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003793
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003794 tp_features.bay_status = bay_handle &&
3795 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3796 tp_features.bay_status2 = bay2_handle &&
3797 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003798
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003799 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3800 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3801 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3802 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003804 vdbg_printk(TPACPI_DBG_INIT,
3805 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003806 str_supported(tp_features.bay_status),
3807 str_supported(tp_features.bay_eject),
3808 str_supported(tp_features.bay_status2),
3809 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003810
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003811 return (tp_features.bay_status || tp_features.bay_eject ||
3812 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813}
3814
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003815static void bay_notify(struct ibm_struct *ibm, u32 event)
3816{
Len Brown14e04fb2007-08-23 15:20:26 -04003817 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003818 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3819 ibm->acpi->device->dev.bus_id,
3820 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003821}
3822
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003823#define bay_occupied(b) (_sta(b##_handle) & 1)
3824
3825static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
3827 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003828 int occupied = bay_occupied(bay);
3829 int occupied2 = bay_occupied(bay2);
3830 int eject, eject2;
3831
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003832 len += sprintf(p + len, "status:\t\t%s\n",
3833 tp_features.bay_status ?
3834 (occupied ? "occupied" : "unoccupied") :
3835 "not supported");
3836 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003837 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3838 "occupied" : "unoccupied");
3839
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003840 eject = tp_features.bay_eject && occupied;
3841 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003842
3843 if (eject && eject2)
3844 len += sprintf(p + len, "commands:\teject, eject2\n");
3845 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003847 else if (eject2)
3848 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
3850 return len;
3851}
3852
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003853static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854{
3855 char *cmd;
3856
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003857 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003858 return -ENODEV;
3859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003861 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003862 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3863 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003864 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003865 strlencmp(cmd, "eject2") == 0) {
3866 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 return -EIO;
3868 } else
3869 return -EINVAL;
3870 }
3871
3872 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003873}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003874
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003875static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3876 .notify = bay_notify,
3877 .handle = &bay_handle,
3878 .type = ACPI_SYSTEM_NOTIFY,
3879};
3880
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003881static struct ibm_struct bay_driver_data = {
3882 .name = "bay",
3883 .read = bay_read,
3884 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003885 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003886};
3887
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003888#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003890/*************************************************************************
3891 * CMOS subdriver
3892 */
3893
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003894/* sysfs cmos_command -------------------------------------------------- */
3895static ssize_t cmos_command_store(struct device *dev,
3896 struct device_attribute *attr,
3897 const char *buf, size_t count)
3898{
3899 unsigned long cmos_cmd;
3900 int res;
3901
3902 if (parse_strtoul(buf, 21, &cmos_cmd))
3903 return -EINVAL;
3904
3905 res = issue_thinkpad_cmos_command(cmos_cmd);
3906 return (res)? res : count;
3907}
3908
3909static struct device_attribute dev_attr_cmos_command =
3910 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3911
3912/* --------------------------------------------------------------------- */
3913
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003914static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003915{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003916 int res;
3917
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003918 vdbg_printk(TPACPI_DBG_INIT,
3919 "initializing cmos commands subdriver\n");
3920
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003921 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003922
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003923 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3924 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003925
3926 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3927 if (res)
3928 return res;
3929
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003930 return (cmos_handle)? 0 : 1;
3931}
3932
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003933static void cmos_exit(void)
3934{
3935 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3936}
3937
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003938static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
3940 int len = 0;
3941
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003942 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3943 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 if (!cmos_handle)
3945 len += sprintf(p + len, "status:\t\tnot supported\n");
3946 else {
3947 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003948 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
3950
3951 return len;
3952}
3953
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003954static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
3956 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003957 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
3959 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003960 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3961 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 /* cmos_cmd set */
3963 } else
3964 return -EINVAL;
3965
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003966 res = issue_thinkpad_cmos_command(cmos_cmd);
3967 if (res)
3968 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 }
3970
3971 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003972}
3973
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003974static struct ibm_struct cmos_driver_data = {
3975 .name = "cmos",
3976 .read = cmos_read,
3977 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003978 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003979};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003980
3981/*************************************************************************
3982 * LED subdriver
3983 */
3984
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003985enum led_access_mode {
3986 TPACPI_LED_NONE = 0,
3987 TPACPI_LED_570, /* 570 */
3988 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3989 TPACPI_LED_NEW, /* all others */
3990};
3991
3992enum { /* For TPACPI_LED_OLD */
3993 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3994 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3995 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3996};
3997
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003998enum led_status_t {
3999 TPACPI_LED_OFF = 0,
4000 TPACPI_LED_ON,
4001 TPACPI_LED_BLINK,
4002};
4003
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004004static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004005
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004006TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004007 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4008 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004009 "LED", /* all others */
4010 ); /* R30, R31 */
4011
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004012#define TPACPI_LED_NUMLEDS 8
4013static struct tpacpi_led_classdev *tpacpi_leds;
4014static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004015static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004016 /* there's a limit of 19 chars + NULL before 2.6.26 */
4017 "tpacpi::power",
4018 "tpacpi:orange:batt",
4019 "tpacpi:green:batt",
4020 "tpacpi::dock_active",
4021 "tpacpi::bay_active",
4022 "tpacpi::dock_batt",
4023 "tpacpi::unknown_led",
4024 "tpacpi::standby",
4025};
4026
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004027static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004028{
4029 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004030 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004031
4032 switch (led_supported) {
4033 case TPACPI_LED_570:
4034 if (!acpi_evalf(ec_handle,
4035 &status, "GLED", "dd", 1 << led))
4036 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004037 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004038 TPACPI_LED_OFF :
4039 ((status == 1)?
4040 TPACPI_LED_ON :
4041 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004042 tpacpi_led_state_cache[led] = led_s;
4043 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004044 default:
4045 return -ENXIO;
4046 }
4047
4048 /* not reached */
4049}
4050
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004051static int led_set_status(const unsigned int led,
4052 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004053{
4054 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004055 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4056 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004057
4058 int rc = 0;
4059
4060 switch (led_supported) {
4061 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004062 /* 570 */
4063 if (led > 7)
4064 return -EINVAL;
4065 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4066 (1 << led), led_sled_arg1[ledstatus]))
4067 rc = -EIO;
4068 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004069 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004070 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4071 if (led > 7)
4072 return -EINVAL;
4073 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4074 if (rc >= 0)
4075 rc = ec_write(TPACPI_LED_EC_HLBL,
4076 (ledstatus == TPACPI_LED_BLINK) << led);
4077 if (rc >= 0)
4078 rc = ec_write(TPACPI_LED_EC_HLCL,
4079 (ledstatus != TPACPI_LED_OFF) << led);
4080 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004081 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004082 /* all others */
4083 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4084 led, led_led_arg1[ledstatus]))
4085 rc = -EIO;
4086 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004087 default:
4088 rc = -ENXIO;
4089 }
4090
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004091 if (!rc)
4092 tpacpi_led_state_cache[led] = ledstatus;
4093
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004094 return rc;
4095}
4096
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004097static void led_sysfs_set_status(unsigned int led,
4098 enum led_brightness brightness)
4099{
4100 led_set_status(led,
4101 (brightness == LED_OFF) ?
4102 TPACPI_LED_OFF :
4103 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4104 TPACPI_LED_BLINK : TPACPI_LED_ON);
4105}
4106
4107static void led_set_status_worker(struct work_struct *work)
4108{
4109 struct tpacpi_led_classdev *data =
4110 container_of(work, struct tpacpi_led_classdev, work);
4111
4112 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4113 led_sysfs_set_status(data->led, data->new_brightness);
4114}
4115
4116static void led_sysfs_set(struct led_classdev *led_cdev,
4117 enum led_brightness brightness)
4118{
4119 struct tpacpi_led_classdev *data = container_of(led_cdev,
4120 struct tpacpi_led_classdev, led_classdev);
4121
4122 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004123 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004124}
4125
4126static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4127 unsigned long *delay_on, unsigned long *delay_off)
4128{
4129 struct tpacpi_led_classdev *data = container_of(led_cdev,
4130 struct tpacpi_led_classdev, led_classdev);
4131
4132 /* Can we choose the flash rate? */
4133 if (*delay_on == 0 && *delay_off == 0) {
4134 /* yes. set them to the hardware blink rate (1 Hz) */
4135 *delay_on = 500; /* ms */
4136 *delay_off = 500; /* ms */
4137 } else if ((*delay_on != 500) || (*delay_off != 500))
4138 return -EINVAL;
4139
4140 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004141 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004142
4143 return 0;
4144}
4145
4146static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4147{
4148 int rc;
4149
4150 struct tpacpi_led_classdev *data = container_of(led_cdev,
4151 struct tpacpi_led_classdev, led_classdev);
4152
4153 rc = led_get_status(data->led);
4154
4155 if (rc == TPACPI_LED_OFF || rc < 0)
4156 rc = LED_OFF; /* no error handling in led class :( */
4157 else
4158 rc = LED_FULL;
4159
4160 return rc;
4161}
4162
4163static void led_exit(void)
4164{
4165 unsigned int i;
4166
4167 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4168 if (tpacpi_leds[i].led_classdev.name)
4169 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4170 }
4171
4172 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004173}
4174
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004175static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004176{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004177 unsigned int i;
4178 int rc;
4179
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004180 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4181
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004182 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004183
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004184 if (!led_handle)
4185 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004186 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004187 else if (strlencmp(led_path, "SLED") == 0)
4188 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004189 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004190 else if (strlencmp(led_path, "SYSL") == 0)
4191 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004192 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004193 else
4194 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004195 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004196
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004197 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4198 str_supported(led_supported), led_supported);
4199
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004200 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4201 GFP_KERNEL);
4202 if (!tpacpi_leds) {
4203 printk(TPACPI_ERR "Out of memory for LED data\n");
4204 return -ENOMEM;
4205 }
4206
4207 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4208 tpacpi_leds[i].led = i;
4209
4210 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4211 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4212 if (led_supported == TPACPI_LED_570)
4213 tpacpi_leds[i].led_classdev.brightness_get =
4214 &led_sysfs_get;
4215
4216 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4217
4218 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4219
4220 rc = led_classdev_register(&tpacpi_pdev->dev,
4221 &tpacpi_leds[i].led_classdev);
4222 if (rc < 0) {
4223 tpacpi_leds[i].led_classdev.name = NULL;
4224 led_exit();
4225 return rc;
4226 }
4227 }
4228
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004229 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004230}
4231
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004232#define str_led_status(s) \
4233 ((s) == TPACPI_LED_OFF ? "off" : \
4234 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004235
4236static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
4238 int len = 0;
4239
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004240 if (!led_supported) {
4241 len += sprintf(p + len, "status:\t\tnot supported\n");
4242 return len;
4243 }
4244 len += sprintf(p + len, "status:\t\tsupported\n");
4245
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004246 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004247 /* 570 */
4248 int i, status;
4249 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004250 status = led_get_status(i);
4251 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004252 return -EIO;
4253 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004254 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004255 }
4256 }
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004259 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
4261 return len;
4262}
4263
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004264static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
4266 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004267 int led, rc;
4268 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004269
4270 if (!led_supported)
4271 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004274 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return -EINVAL;
4276
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004277 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004278 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004280 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004281 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004282 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004283 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004284 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004285 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004286
4287 rc = led_set_status(led, s);
4288 if (rc < 0)
4289 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 }
4291
4292 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004293}
4294
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004295static struct ibm_struct led_driver_data = {
4296 .name = "led",
4297 .read = led_read,
4298 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004299 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004300};
4301
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004302/*************************************************************************
4303 * Beep subdriver
4304 */
4305
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004306TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004307
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004308static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004309{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004310 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4311
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004312 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004313
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004314 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4315 str_supported(beep_handle != NULL));
4316
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004317 return (beep_handle)? 0 : 1;
4318}
4319
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004320static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
4322 int len = 0;
4323
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004324 if (!beep_handle)
4325 len += sprintf(p + len, "status:\t\tnot supported\n");
4326 else {
4327 len += sprintf(p + len, "status:\t\tsupported\n");
4328 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331 return len;
4332}
4333
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004334static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335{
4336 char *cmd;
4337 int beep_cmd;
4338
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004339 if (!beep_handle)
4340 return -ENODEV;
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004343 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4344 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 /* beep_cmd set */
4346 } else
4347 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004348 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return -EIO;
4350 }
4351
4352 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004353}
4354
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004355static struct ibm_struct beep_driver_data = {
4356 .name = "beep",
4357 .read = beep_read,
4358 .write = beep_write,
4359};
4360
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004361/*************************************************************************
4362 * Thermal subdriver
4363 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004364
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004365enum thermal_access_mode {
4366 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4367 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4368 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4369 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4370 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4371};
4372
4373enum { /* TPACPI_THERMAL_TPEC_* */
4374 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4375 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4376 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4377};
4378
4379#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4380struct ibm_thermal_sensors_struct {
4381 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4382};
4383
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004384static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004385
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004386/* idx is zero-based */
4387static int thermal_get_sensor(int idx, s32 *value)
4388{
4389 int t;
4390 s8 tmp;
4391 char tmpi[5];
4392
4393 t = TP_EC_THERMAL_TMP0;
4394
4395 switch (thermal_read_mode) {
4396#if TPACPI_MAX_THERMAL_SENSORS >= 16
4397 case TPACPI_THERMAL_TPEC_16:
4398 if (idx >= 8 && idx <= 15) {
4399 t = TP_EC_THERMAL_TMP8;
4400 idx -= 8;
4401 }
4402 /* fallthrough */
4403#endif
4404 case TPACPI_THERMAL_TPEC_8:
4405 if (idx <= 7) {
4406 if (!acpi_ec_read(t + idx, &tmp))
4407 return -EIO;
4408 *value = tmp * 1000;
4409 return 0;
4410 }
4411 break;
4412
4413 case TPACPI_THERMAL_ACPI_UPDT:
4414 if (idx <= 7) {
4415 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4416 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4417 return -EIO;
4418 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4419 return -EIO;
4420 *value = (t - 2732) * 100;
4421 return 0;
4422 }
4423 break;
4424
4425 case TPACPI_THERMAL_ACPI_TMP07:
4426 if (idx <= 7) {
4427 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4428 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4429 return -EIO;
4430 if (t > 127 || t < -127)
4431 t = TP_EC_THERMAL_TMP_NA;
4432 *value = t * 1000;
4433 return 0;
4434 }
4435 break;
4436
4437 case TPACPI_THERMAL_NONE:
4438 default:
4439 return -ENOSYS;
4440 }
4441
4442 return -EINVAL;
4443}
4444
4445static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4446{
4447 int res, i;
4448 int n;
4449
4450 n = 8;
4451 i = 0;
4452
4453 if (!s)
4454 return -EINVAL;
4455
4456 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4457 n = 16;
4458
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004459 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004460 res = thermal_get_sensor(i, &s->temp[i]);
4461 if (res)
4462 return res;
4463 }
4464
4465 return n;
4466}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004467
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004468/* sysfs temp##_input -------------------------------------------------- */
4469
4470static ssize_t thermal_temp_input_show(struct device *dev,
4471 struct device_attribute *attr,
4472 char *buf)
4473{
4474 struct sensor_device_attribute *sensor_attr =
4475 to_sensor_dev_attr(attr);
4476 int idx = sensor_attr->index;
4477 s32 value;
4478 int res;
4479
4480 res = thermal_get_sensor(idx, &value);
4481 if (res)
4482 return res;
4483 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4484 return -ENXIO;
4485
4486 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4487}
4488
4489#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004490 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4491 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004492
4493static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4494 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4495 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4496 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4497 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4498 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4499 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4500 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4501 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4502 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4503 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4504 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4505 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4506 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4507 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4508 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4509 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4510};
4511
4512#define THERMAL_ATTRS(X) \
4513 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4514
4515static struct attribute *thermal_temp_input_attr[] = {
4516 THERMAL_ATTRS(8),
4517 THERMAL_ATTRS(9),
4518 THERMAL_ATTRS(10),
4519 THERMAL_ATTRS(11),
4520 THERMAL_ATTRS(12),
4521 THERMAL_ATTRS(13),
4522 THERMAL_ATTRS(14),
4523 THERMAL_ATTRS(15),
4524 THERMAL_ATTRS(0),
4525 THERMAL_ATTRS(1),
4526 THERMAL_ATTRS(2),
4527 THERMAL_ATTRS(3),
4528 THERMAL_ATTRS(4),
4529 THERMAL_ATTRS(5),
4530 THERMAL_ATTRS(6),
4531 THERMAL_ATTRS(7),
4532 NULL
4533};
4534
4535static const struct attribute_group thermal_temp_input16_group = {
4536 .attrs = thermal_temp_input_attr
4537};
4538
4539static const struct attribute_group thermal_temp_input8_group = {
4540 .attrs = &thermal_temp_input_attr[8]
4541};
4542
4543#undef THERMAL_SENSOR_ATTR_TEMP
4544#undef THERMAL_ATTRS
4545
4546/* --------------------------------------------------------------------- */
4547
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004548static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004549{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004550 u8 t, ta1, ta2;
4551 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004552 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004553 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004554
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004555 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4556
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004557 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004558
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004559 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004560 /*
4561 * Direct EC access mode: sensors at registers
4562 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4563 * non-implemented, thermal sensors return 0x80 when
4564 * not available
4565 */
4566
4567 ta1 = ta2 = 0;
4568 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004569 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004570 ta1 |= t;
4571 } else {
4572 ta1 = 0;
4573 break;
4574 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004575 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004576 ta2 |= t;
4577 } else {
4578 ta1 = 0;
4579 break;
4580 }
4581 }
4582 if (ta1 == 0) {
4583 /* This is sheer paranoia, but we handle it anyway */
4584 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004585 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004586 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004587 "falling back to ACPI TMPx access "
4588 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004589 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004590 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004591 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004592 "ThinkPad ACPI EC access misbehaving, "
4593 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004594 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004595 }
4596 } else {
4597 thermal_read_mode =
4598 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004599 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004600 }
4601 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004602 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4603 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004604 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004605 } else {
4606 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004607 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004608 }
4609 } else {
4610 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004611 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004612 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004613
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004614 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4615 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4616 thermal_read_mode);
4617
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004618 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004619 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004620 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004621 &thermal_temp_input16_group);
4622 if (res)
4623 return res;
4624 break;
4625 case TPACPI_THERMAL_TPEC_8:
4626 case TPACPI_THERMAL_ACPI_TMP07:
4627 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004628 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004629 &thermal_temp_input8_group);
4630 if (res)
4631 return res;
4632 break;
4633 case TPACPI_THERMAL_NONE:
4634 default:
4635 return 1;
4636 }
4637
4638 return 0;
4639}
4640
4641static void thermal_exit(void)
4642{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004643 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004644 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004645 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004646 &thermal_temp_input16_group);
4647 break;
4648 case TPACPI_THERMAL_TPEC_8:
4649 case TPACPI_THERMAL_ACPI_TMP07:
4650 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004651 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004652 &thermal_temp_input16_group);
4653 break;
4654 case TPACPI_THERMAL_NONE:
4655 default:
4656 break;
4657 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004658}
4659
4660static int thermal_read(char *p)
4661{
4662 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004663 int n, i;
4664 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004665
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004666 n = thermal_get_sensors(&t);
4667 if (unlikely(n < 0))
4668 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004669
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004670 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004671
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004672 if (n > 0) {
4673 for (i = 0; i < (n - 1); i++)
4674 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4675 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4676 } else
4677 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004678
4679 return len;
4680}
4681
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004682static struct ibm_struct thermal_driver_data = {
4683 .name = "thermal",
4684 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004685 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004686};
4687
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004688/*************************************************************************
4689 * EC Dump subdriver
4690 */
4691
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004692static u8 ecdump_regs[256];
4693
4694static int ecdump_read(char *p)
4695{
4696 int len = 0;
4697 int i, j;
4698 u8 v;
4699
4700 len += sprintf(p + len, "EC "
4701 " +00 +01 +02 +03 +04 +05 +06 +07"
4702 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4703 for (i = 0; i < 256; i += 16) {
4704 len += sprintf(p + len, "EC 0x%02x:", i);
4705 for (j = 0; j < 16; j++) {
4706 if (!acpi_ec_read(i + j, &v))
4707 break;
4708 if (v != ecdump_regs[i + j])
4709 len += sprintf(p + len, " *%02x", v);
4710 else
4711 len += sprintf(p + len, " %02x", v);
4712 ecdump_regs[i + j] = v;
4713 }
4714 len += sprintf(p + len, "\n");
4715 if (j != 16)
4716 break;
4717 }
4718
4719 /* These are way too dangerous to advertise openly... */
4720#if 0
4721 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4722 " (<offset> is 00-ff, <value> is 00-ff)\n");
4723 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4724 " (<offset> is 00-ff, <value> is 0-255)\n");
4725#endif
4726 return len;
4727}
4728
4729static int ecdump_write(char *buf)
4730{
4731 char *cmd;
4732 int i, v;
4733
4734 while ((cmd = next_cmd(&buf))) {
4735 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4736 /* i and v set */
4737 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4738 /* i and v set */
4739 } else
4740 return -EINVAL;
4741 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4742 if (!acpi_ec_write(i, v))
4743 return -EIO;
4744 } else
4745 return -EINVAL;
4746 }
4747
4748 return 0;
4749}
4750
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004751static struct ibm_struct ecdump_driver_data = {
4752 .name = "ecdump",
4753 .read = ecdump_read,
4754 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004755 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004756};
4757
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004758/*************************************************************************
4759 * Backlight/brightness subdriver
4760 */
Holger Macht8acb0252006-10-20 14:30:28 -07004761
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004762#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4763
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004764enum {
4765 TP_EC_BACKLIGHT = 0x31,
4766
4767 /* TP_EC_BACKLIGHT bitmasks */
4768 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4769 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4770 TP_EC_BACKLIGHT_MAPSW = 0x20,
4771};
4772
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004773static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004774static int brightness_mode;
4775static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4776
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004777static struct mutex brightness_mutex;
4778
4779/*
4780 * ThinkPads can read brightness from two places: EC 0x31, or
4781 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004782 *
4783 * EC 0x31 has the following layout
4784 * Bit 7: unknown function
4785 * Bit 6: unknown function
4786 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4787 * Bit 4: must be set to zero to avoid problems
4788 * Bit 3-0: backlight brightness level
4789 *
4790 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004791 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004792static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004793{
4794 u8 lec = 0, lcmos = 0, level = 0;
4795
4796 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004797 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004798 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004799 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004800 };
4801 if (brightness_mode & 2) {
4802 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4803 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4804 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4805 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4806 level = lcmos;
4807 }
4808
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004809 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004810 *status = lec; /* Prefer EC, CMOS is just a backing store */
4811 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004812 if (lec == lcmos)
4813 tp_warned.bright_cmos_ec_unsync = 0;
4814 else {
4815 if (!tp_warned.bright_cmos_ec_unsync) {
4816 printk(TPACPI_ERR
4817 "CMOS NVRAM (%u) and EC (%u) do not "
4818 "agree on display brightness level\n",
4819 (unsigned int) lcmos,
4820 (unsigned int) lec);
4821 tp_warned.bright_cmos_ec_unsync = 1;
4822 }
4823 return -EIO;
4824 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004825 } else {
4826 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004827 }
4828
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004829 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004830}
4831
4832/* May return EINTR which can always be mapped to ERESTARTSYS */
4833static int brightness_set(int value)
4834{
4835 int cmos_cmd, inc, i, res;
4836 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004837 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004838
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004839 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4840 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004841 return -EINVAL;
4842
4843 res = mutex_lock_interruptible(&brightness_mutex);
4844 if (res < 0)
4845 return res;
4846
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004847 res = brightness_get_raw(&current_value);
4848 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004849 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004850
4851 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4852 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004853
4854 cmos_cmd = value > current_value ?
4855 TP_CMOS_BRIGHTNESS_UP :
4856 TP_CMOS_BRIGHTNESS_DOWN;
4857 inc = (value > current_value)? 1 : -1;
4858
4859 res = 0;
4860 for (i = current_value; i != value; i += inc) {
4861 if ((brightness_mode & 2) &&
4862 issue_thinkpad_cmos_command(cmos_cmd)) {
4863 res = -EIO;
4864 goto errout;
4865 }
4866 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004867 !acpi_ec_write(TP_EC_BACKLIGHT,
4868 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004869 res = -EIO;
4870 goto errout;;
4871 }
4872 }
4873
4874errout:
4875 mutex_unlock(&brightness_mutex);
4876 return res;
4877}
4878
4879/* sysfs backlight class ----------------------------------------------- */
4880
4881static int brightness_update_status(struct backlight_device *bd)
4882{
4883 /* it is the backlight class's job (caller) to handle
4884 * EINTR and other errors properly */
4885 return brightness_set(
4886 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4887 bd->props.power == FB_BLANK_UNBLANK) ?
4888 bd->props.brightness : 0);
4889}
Holger Macht8acb0252006-10-20 14:30:28 -07004890
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004891static int brightness_get(struct backlight_device *bd)
4892{
4893 int status, res;
4894
4895 res = brightness_get_raw(&status);
4896 if (res < 0)
4897 return 0; /* FIXME: teach backlight about error handling */
4898
4899 return status & TP_EC_BACKLIGHT_LVLMSK;
4900}
4901
Richard Purdie599a52d2007-02-10 23:07:48 +00004902static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004903 .get_brightness = brightness_get,
4904 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004905};
4906
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004907/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004908
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004909static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004910{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004911 int b;
4912
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004913 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4914
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004915 mutex_init(&brightness_mutex);
4916
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004917 /*
4918 * We always attempt to detect acpi support, so as to switch
4919 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4920 * going to publish a backlight interface
4921 */
4922 b = tpacpi_check_std_acpi_brightness_support();
4923 if (b > 0) {
4924 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4925 printk(TPACPI_NOTICE
4926 "Lenovo BIOS switched to ACPI backlight "
4927 "control mode\n");
4928 }
4929 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004930 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004931 "standard ACPI backlight interface "
4932 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004933 return 1;
4934 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004935 }
4936
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004937 if (!brightness_enable) {
4938 dbg_printk(TPACPI_DBG_INIT,
4939 "brightness support disabled by "
4940 "module parameter\n");
4941 return 1;
4942 }
4943
4944 if (b > 16) {
4945 printk(TPACPI_ERR
4946 "Unsupported brightness interface, "
4947 "please contact %s\n", TPACPI_MAIL);
4948 return 1;
4949 }
4950 if (b == 16)
4951 tp_features.bright_16levels = 1;
4952
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004953 if (!brightness_mode) {
4954 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4955 brightness_mode = 2;
4956 else
4957 brightness_mode = 3;
4958
4959 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4960 brightness_mode);
4961 }
4962
4963 if (brightness_mode > 3)
4964 return -EINVAL;
4965
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004966 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004967 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004968
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004969 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004970 printk(TPACPI_INFO
4971 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004972
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004973 ibm_backlight_device = backlight_device_register(
4974 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4975 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004976 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004977 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004978 return PTR_ERR(ibm_backlight_device);
4979 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004980 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004981
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004982 ibm_backlight_device->props.max_brightness =
4983 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004984 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004985 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004986
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004987 return 0;
4988}
4989
4990static void brightness_exit(void)
4991{
4992 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004993 vdbg_printk(TPACPI_DBG_EXIT,
4994 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004995 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004996 }
4997}
4998
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004999static int brightness_read(char *p)
5000{
5001 int len = 0;
5002 int level;
5003
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005004 level = brightness_get(NULL);
5005 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005006 len += sprintf(p + len, "level:\t\tunreadable\n");
5007 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005008 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005009 len += sprintf(p + len, "commands:\tup, down\n");
5010 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005011 " (<level> is 0-%d)\n",
5012 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005013 }
5014
5015 return len;
5016}
5017
5018static int brightness_write(char *buf)
5019{
5020 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005021 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005022 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005023 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005024
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005025 level = brightness_get(NULL);
5026 if (level < 0)
5027 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005028
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005029 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005030 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005031 if (level < max_level)
5032 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005033 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005034 if (level > 0)
5035 level--;
5036 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5037 level >= 0 && level <= max_level) {
5038 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005039 } else
5040 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005041 }
5042
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005043 /*
5044 * Now we know what the final level should be, so we try to set it.
5045 * Doing it this way makes the syscall restartable in case of EINTR
5046 */
5047 rc = brightness_set(level);
5048 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005049}
5050
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005051static struct ibm_struct brightness_driver_data = {
5052 .name = "brightness",
5053 .read = brightness_read,
5054 .write = brightness_write,
5055 .exit = brightness_exit,
5056};
5057
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005058/*************************************************************************
5059 * Volume subdriver
5060 */
5061
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005062static int volume_offset = 0x30;
5063
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005064static int volume_read(char *p)
5065{
5066 int len = 0;
5067 u8 level;
5068
5069 if (!acpi_ec_read(volume_offset, &level)) {
5070 len += sprintf(p + len, "level:\t\tunreadable\n");
5071 } else {
5072 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5073 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5074 len += sprintf(p + len, "commands:\tup, down, mute\n");
5075 len += sprintf(p + len, "commands:\tlevel <level>"
5076 " (<level> is 0-15)\n");
5077 }
5078
5079 return len;
5080}
5081
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005082static int volume_write(char *buf)
5083{
5084 int cmos_cmd, inc, i;
5085 u8 level, mute;
5086 int new_level, new_mute;
5087 char *cmd;
5088
5089 while ((cmd = next_cmd(&buf))) {
5090 if (!acpi_ec_read(volume_offset, &level))
5091 return -EIO;
5092 new_mute = mute = level & 0x40;
5093 new_level = level = level & 0xf;
5094
5095 if (strlencmp(cmd, "up") == 0) {
5096 if (mute)
5097 new_mute = 0;
5098 else
5099 new_level = level == 15 ? 15 : level + 1;
5100 } else if (strlencmp(cmd, "down") == 0) {
5101 if (mute)
5102 new_mute = 0;
5103 else
5104 new_level = level == 0 ? 0 : level - 1;
5105 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5106 new_level >= 0 && new_level <= 15) {
5107 /* new_level set */
5108 } else if (strlencmp(cmd, "mute") == 0) {
5109 new_mute = 0x40;
5110 } else
5111 return -EINVAL;
5112
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005113 if (new_level != level) {
5114 /* mute doesn't change */
5115
5116 cmos_cmd = (new_level > level) ?
5117 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005118 inc = new_level > level ? 1 : -1;
5119
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005120 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005121 !acpi_ec_write(volume_offset, level)))
5122 return -EIO;
5123
5124 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005125 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005126 !acpi_ec_write(volume_offset, i + inc))
5127 return -EIO;
5128
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005129 if (mute &&
5130 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5131 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005132 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005133 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005134 }
5135
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005136 if (new_mute != mute) {
5137 /* level doesn't change */
5138
5139 cmos_cmd = (new_mute) ?
5140 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005141
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005142 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005143 !acpi_ec_write(volume_offset, level + new_mute))
5144 return -EIO;
5145 }
5146 }
5147
5148 return 0;
5149}
5150
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005151static struct ibm_struct volume_driver_data = {
5152 .name = "volume",
5153 .read = volume_read,
5154 .write = volume_write,
5155};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005156
5157/*************************************************************************
5158 * Fan subdriver
5159 */
5160
5161/*
5162 * FAN ACCESS MODES
5163 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005164 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005165 * ACPI GFAN method: returns fan level
5166 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005167 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005168 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005169 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005170 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005171 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5172 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005173 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5174 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005175 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005176 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005177 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5178 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005179 *
5180 * Fan speed changes of any sort (including those caused by the
5181 * disengaged mode) are usually done slowly by the firmware as the
5182 * maximum ammount of fan duty cycle change per second seems to be
5183 * limited.
5184 *
5185 * Reading is not available if GFAN exists.
5186 * Writing is not available if SFAN exists.
5187 *
5188 * Bits
5189 * 7 automatic mode engaged;
5190 * (default operation mode of the ThinkPad)
5191 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005192 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005193 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005194 * the tachometer while the fan controller ramps up
5195 * the speed (which can take up to a few *minutes*).
5196 * Speeds up fan to 100% duty-cycle, which is far above
5197 * the standard RPM levels. It is not impossible that
5198 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005199 * 5-3 unused in some models. Extra bits for fan level
5200 * in others, but still useless as all values above
5201 * 7 map to the same speed as level 7 in these models.
5202 * 2-0 fan level (0..7 usually)
5203 * 0x00 = stop
5204 * 0x07 = max (set when temperatures critical)
5205 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005206 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005207 *
5208 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5209 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5210 * does so, its initial value is meaningless (0x07).
5211 *
5212 * For firmware bugs, refer to:
5213 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5214 *
5215 * ----
5216 *
5217 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5218 * Main fan tachometer reading (in RPM)
5219 *
5220 * This register is present on all ThinkPads with a new-style EC, and
5221 * it is known not to be present on the A21m/e, and T22, as there is
5222 * something else in offset 0x84 according to the ACPI DSDT. Other
5223 * ThinkPads from this same time period (and earlier) probably lack the
5224 * tachometer as well.
5225 *
5226 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5227 * was never fixed by IBM to report the EC firmware version string
5228 * probably support the tachometer (like the early X models), so
5229 * detecting it is quite hard. We need more data to know for sure.
5230 *
5231 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5232 * might result.
5233 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005234 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5235 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005236 *
5237 * For firmware bugs, refer to:
5238 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5239 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005240 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005241 * ThinkPad X31, X40, X41. Not available in the X60.
5242 *
5243 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5244 * high speed. ACPI DSDT seems to map these three speeds to levels
5245 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5246 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5247 *
5248 * The speeds are stored on handles
5249 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5250 *
5251 * There are three default speed sets, acessible as handles:
5252 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5253 *
5254 * ACPI DSDT switches which set is in use depending on various
5255 * factors.
5256 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005257 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005258 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5259 * but the ACPI tables just mention level 7.
5260 */
5261
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005262enum { /* Fan control constants */
5263 fan_status_offset = 0x2f, /* EC register 0x2f */
5264 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5265 * 0x84 must be read before 0x85 */
5266
5267 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5268 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5269
5270 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5271};
5272
5273enum fan_status_access_mode {
5274 TPACPI_FAN_NONE = 0, /* No fan status or control */
5275 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5276 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5277};
5278
5279enum fan_control_access_mode {
5280 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5281 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5282 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5283 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5284};
5285
5286enum fan_control_commands {
5287 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5288 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5289 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5290 * and also watchdog cmd */
5291};
5292
5293static int fan_control_allowed;
5294
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005295static enum fan_status_access_mode fan_status_access_mode;
5296static enum fan_control_access_mode fan_control_access_mode;
5297static enum fan_control_commands fan_control_commands;
5298
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005299static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005300static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005301static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005302
5303static struct mutex fan_mutex;
5304
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005305static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005306static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005307
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005308TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5309TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005310 "\\FSPD", /* 600e/x, 770e, 770x */
5311 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005312TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005313 "JFNS", /* 770x-JL */
5314 ); /* all others */
5315
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005316/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005317 * Call with fan_mutex held
5318 */
5319static void fan_update_desired_level(u8 status)
5320{
5321 if ((status &
5322 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5323 if (status > 7)
5324 fan_control_desired_level = 7;
5325 else
5326 fan_control_desired_level = status;
5327 }
5328}
5329
5330static int fan_get_status(u8 *status)
5331{
5332 u8 s;
5333
5334 /* TODO:
5335 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5336
5337 switch (fan_status_access_mode) {
5338 case TPACPI_FAN_RD_ACPI_GFAN:
5339 /* 570, 600e/x, 770e, 770x */
5340
5341 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5342 return -EIO;
5343
5344 if (likely(status))
5345 *status = s & 0x07;
5346
5347 break;
5348
5349 case TPACPI_FAN_RD_TPEC:
5350 /* all except 570, 600e/x, 770e, 770x */
5351 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5352 return -EIO;
5353
5354 if (likely(status))
5355 *status = s;
5356
5357 break;
5358
5359 default:
5360 return -ENXIO;
5361 }
5362
5363 return 0;
5364}
5365
5366static int fan_get_status_safe(u8 *status)
5367{
5368 int rc;
5369 u8 s;
5370
5371 if (mutex_lock_interruptible(&fan_mutex))
5372 return -ERESTARTSYS;
5373 rc = fan_get_status(&s);
5374 if (!rc)
5375 fan_update_desired_level(s);
5376 mutex_unlock(&fan_mutex);
5377
5378 if (status)
5379 *status = s;
5380
5381 return rc;
5382}
5383
5384static int fan_get_speed(unsigned int *speed)
5385{
5386 u8 hi, lo;
5387
5388 switch (fan_status_access_mode) {
5389 case TPACPI_FAN_RD_TPEC:
5390 /* all except 570, 600e/x, 770e, 770x */
5391 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5392 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5393 return -EIO;
5394
5395 if (likely(speed))
5396 *speed = (hi << 8) | lo;
5397
5398 break;
5399
5400 default:
5401 return -ENXIO;
5402 }
5403
5404 return 0;
5405}
5406
5407static int fan_set_level(int level)
5408{
5409 if (!fan_control_allowed)
5410 return -EPERM;
5411
5412 switch (fan_control_access_mode) {
5413 case TPACPI_FAN_WR_ACPI_SFAN:
5414 if (level >= 0 && level <= 7) {
5415 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5416 return -EIO;
5417 } else
5418 return -EINVAL;
5419 break;
5420
5421 case TPACPI_FAN_WR_ACPI_FANS:
5422 case TPACPI_FAN_WR_TPEC:
5423 if ((level != TP_EC_FAN_AUTO) &&
5424 (level != TP_EC_FAN_FULLSPEED) &&
5425 ((level < 0) || (level > 7)))
5426 return -EINVAL;
5427
5428 /* safety net should the EC not support AUTO
5429 * or FULLSPEED mode bits and just ignore them */
5430 if (level & TP_EC_FAN_FULLSPEED)
5431 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005432 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005433 level |= 4; /* safety min speed 4 */
5434
5435 if (!acpi_ec_write(fan_status_offset, level))
5436 return -EIO;
5437 else
5438 tp_features.fan_ctrl_status_undef = 0;
5439 break;
5440
5441 default:
5442 return -ENXIO;
5443 }
5444 return 0;
5445}
5446
5447static int fan_set_level_safe(int level)
5448{
5449 int rc;
5450
5451 if (!fan_control_allowed)
5452 return -EPERM;
5453
5454 if (mutex_lock_interruptible(&fan_mutex))
5455 return -ERESTARTSYS;
5456
5457 if (level == TPACPI_FAN_LAST_LEVEL)
5458 level = fan_control_desired_level;
5459
5460 rc = fan_set_level(level);
5461 if (!rc)
5462 fan_update_desired_level(level);
5463
5464 mutex_unlock(&fan_mutex);
5465 return rc;
5466}
5467
5468static int fan_set_enable(void)
5469{
5470 u8 s;
5471 int rc;
5472
5473 if (!fan_control_allowed)
5474 return -EPERM;
5475
5476 if (mutex_lock_interruptible(&fan_mutex))
5477 return -ERESTARTSYS;
5478
5479 switch (fan_control_access_mode) {
5480 case TPACPI_FAN_WR_ACPI_FANS:
5481 case TPACPI_FAN_WR_TPEC:
5482 rc = fan_get_status(&s);
5483 if (rc < 0)
5484 break;
5485
5486 /* Don't go out of emergency fan mode */
5487 if (s != 7) {
5488 s &= 0x07;
5489 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5490 }
5491
5492 if (!acpi_ec_write(fan_status_offset, s))
5493 rc = -EIO;
5494 else {
5495 tp_features.fan_ctrl_status_undef = 0;
5496 rc = 0;
5497 }
5498 break;
5499
5500 case TPACPI_FAN_WR_ACPI_SFAN:
5501 rc = fan_get_status(&s);
5502 if (rc < 0)
5503 break;
5504
5505 s &= 0x07;
5506
5507 /* Set fan to at least level 4 */
5508 s |= 4;
5509
5510 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005511 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005512 else
5513 rc = 0;
5514 break;
5515
5516 default:
5517 rc = -ENXIO;
5518 }
5519
5520 mutex_unlock(&fan_mutex);
5521 return rc;
5522}
5523
5524static int fan_set_disable(void)
5525{
5526 int rc;
5527
5528 if (!fan_control_allowed)
5529 return -EPERM;
5530
5531 if (mutex_lock_interruptible(&fan_mutex))
5532 return -ERESTARTSYS;
5533
5534 rc = 0;
5535 switch (fan_control_access_mode) {
5536 case TPACPI_FAN_WR_ACPI_FANS:
5537 case TPACPI_FAN_WR_TPEC:
5538 if (!acpi_ec_write(fan_status_offset, 0x00))
5539 rc = -EIO;
5540 else {
5541 fan_control_desired_level = 0;
5542 tp_features.fan_ctrl_status_undef = 0;
5543 }
5544 break;
5545
5546 case TPACPI_FAN_WR_ACPI_SFAN:
5547 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5548 rc = -EIO;
5549 else
5550 fan_control_desired_level = 0;
5551 break;
5552
5553 default:
5554 rc = -ENXIO;
5555 }
5556
5557
5558 mutex_unlock(&fan_mutex);
5559 return rc;
5560}
5561
5562static int fan_set_speed(int speed)
5563{
5564 int rc;
5565
5566 if (!fan_control_allowed)
5567 return -EPERM;
5568
5569 if (mutex_lock_interruptible(&fan_mutex))
5570 return -ERESTARTSYS;
5571
5572 rc = 0;
5573 switch (fan_control_access_mode) {
5574 case TPACPI_FAN_WR_ACPI_FANS:
5575 if (speed >= 0 && speed <= 65535) {
5576 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5577 speed, speed, speed))
5578 rc = -EIO;
5579 } else
5580 rc = -EINVAL;
5581 break;
5582
5583 default:
5584 rc = -ENXIO;
5585 }
5586
5587 mutex_unlock(&fan_mutex);
5588 return rc;
5589}
5590
5591static void fan_watchdog_reset(void)
5592{
5593 static int fan_watchdog_active;
5594
5595 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5596 return;
5597
5598 if (fan_watchdog_active)
5599 cancel_delayed_work(&fan_watchdog_task);
5600
5601 if (fan_watchdog_maxinterval > 0 &&
5602 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5603 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005604 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005605 msecs_to_jiffies(fan_watchdog_maxinterval
5606 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005607 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005608 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005609 "watchdog will not trigger\n");
5610 }
5611 } else
5612 fan_watchdog_active = 0;
5613}
5614
5615static void fan_watchdog_fire(struct work_struct *ignored)
5616{
5617 int rc;
5618
5619 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5620 return;
5621
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005622 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005623 rc = fan_set_enable();
5624 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005625 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005626 "will try again later...\n", -rc);
5627 /* reschedule for later */
5628 fan_watchdog_reset();
5629 }
5630}
5631
5632/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005633 * SYSFS fan layout: hwmon compatible (device)
5634 *
5635 * pwm*_enable:
5636 * 0: "disengaged" mode
5637 * 1: manual mode
5638 * 2: native EC "auto" mode (recommended, hardware default)
5639 *
5640 * pwm*: set speed in manual mode, ignored otherwise.
5641 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5642 * interpolation.
5643 *
5644 * fan*_input: tachometer reading, RPM
5645 *
5646 *
5647 * SYSFS fan layout: extensions
5648 *
5649 * fan_watchdog (driver):
5650 * fan watchdog interval in seconds, 0 disables (default), max 120
5651 */
5652
5653/* sysfs fan pwm1_enable ----------------------------------------------- */
5654static ssize_t fan_pwm1_enable_show(struct device *dev,
5655 struct device_attribute *attr,
5656 char *buf)
5657{
5658 int res, mode;
5659 u8 status;
5660
5661 res = fan_get_status_safe(&status);
5662 if (res)
5663 return res;
5664
5665 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5666 if (status != fan_control_initial_status) {
5667 tp_features.fan_ctrl_status_undef = 0;
5668 } else {
5669 /* Return most likely status. In fact, it
5670 * might be the only possible status */
5671 status = TP_EC_FAN_AUTO;
5672 }
5673 }
5674
5675 if (status & TP_EC_FAN_FULLSPEED) {
5676 mode = 0;
5677 } else if (status & TP_EC_FAN_AUTO) {
5678 mode = 2;
5679 } else
5680 mode = 1;
5681
5682 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5683}
5684
5685static ssize_t fan_pwm1_enable_store(struct device *dev,
5686 struct device_attribute *attr,
5687 const char *buf, size_t count)
5688{
5689 unsigned long t;
5690 int res, level;
5691
5692 if (parse_strtoul(buf, 2, &t))
5693 return -EINVAL;
5694
5695 switch (t) {
5696 case 0:
5697 level = TP_EC_FAN_FULLSPEED;
5698 break;
5699 case 1:
5700 level = TPACPI_FAN_LAST_LEVEL;
5701 break;
5702 case 2:
5703 level = TP_EC_FAN_AUTO;
5704 break;
5705 case 3:
5706 /* reserved for software-controlled auto mode */
5707 return -ENOSYS;
5708 default:
5709 return -EINVAL;
5710 }
5711
5712 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005713 if (res == -ENXIO)
5714 return -EINVAL;
5715 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005716 return res;
5717
5718 fan_watchdog_reset();
5719
5720 return count;
5721}
5722
5723static struct device_attribute dev_attr_fan_pwm1_enable =
5724 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5725 fan_pwm1_enable_show, fan_pwm1_enable_store);
5726
5727/* sysfs fan pwm1 ------------------------------------------------------ */
5728static ssize_t fan_pwm1_show(struct device *dev,
5729 struct device_attribute *attr,
5730 char *buf)
5731{
5732 int res;
5733 u8 status;
5734
5735 res = fan_get_status_safe(&status);
5736 if (res)
5737 return res;
5738
5739 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5740 if (status != fan_control_initial_status) {
5741 tp_features.fan_ctrl_status_undef = 0;
5742 } else {
5743 status = TP_EC_FAN_AUTO;
5744 }
5745 }
5746
5747 if ((status &
5748 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5749 status = fan_control_desired_level;
5750
5751 if (status > 7)
5752 status = 7;
5753
5754 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5755}
5756
5757static ssize_t fan_pwm1_store(struct device *dev,
5758 struct device_attribute *attr,
5759 const char *buf, size_t count)
5760{
5761 unsigned long s;
5762 int rc;
5763 u8 status, newlevel;
5764
5765 if (parse_strtoul(buf, 255, &s))
5766 return -EINVAL;
5767
5768 /* scale down from 0-255 to 0-7 */
5769 newlevel = (s >> 5) & 0x07;
5770
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005771 if (mutex_lock_interruptible(&fan_mutex))
5772 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005773
5774 rc = fan_get_status(&status);
5775 if (!rc && (status &
5776 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5777 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005778 if (rc == -ENXIO)
5779 rc = -EINVAL;
5780 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005781 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005782 fan_watchdog_reset();
5783 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005784 }
5785
5786 mutex_unlock(&fan_mutex);
5787 return (rc)? rc : count;
5788}
5789
5790static struct device_attribute dev_attr_fan_pwm1 =
5791 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5792 fan_pwm1_show, fan_pwm1_store);
5793
5794/* sysfs fan fan1_input ------------------------------------------------ */
5795static ssize_t fan_fan1_input_show(struct device *dev,
5796 struct device_attribute *attr,
5797 char *buf)
5798{
5799 int res;
5800 unsigned int speed;
5801
5802 res = fan_get_speed(&speed);
5803 if (res < 0)
5804 return res;
5805
5806 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5807}
5808
5809static struct device_attribute dev_attr_fan_fan1_input =
5810 __ATTR(fan1_input, S_IRUGO,
5811 fan_fan1_input_show, NULL);
5812
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005813/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005814static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5815 char *buf)
5816{
5817 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5818}
5819
5820static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5821 const char *buf, size_t count)
5822{
5823 unsigned long t;
5824
5825 if (parse_strtoul(buf, 120, &t))
5826 return -EINVAL;
5827
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005828 if (!fan_control_allowed)
5829 return -EPERM;
5830
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005831 fan_watchdog_maxinterval = t;
5832 fan_watchdog_reset();
5833
5834 return count;
5835}
5836
5837static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5838 fan_fan_watchdog_show, fan_fan_watchdog_store);
5839
5840/* --------------------------------------------------------------------- */
5841static struct attribute *fan_attributes[] = {
5842 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5843 &dev_attr_fan_fan1_input.attr,
5844 NULL
5845};
5846
5847static const struct attribute_group fan_attr_group = {
5848 .attrs = fan_attributes,
5849};
5850
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005851static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005852{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005853 int rc;
5854
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005855 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5856
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005857 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005858 fan_status_access_mode = TPACPI_FAN_NONE;
5859 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005860 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005861 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005862 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005863 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005864
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005865 TPACPI_ACPIHANDLE_INIT(fans);
5866 TPACPI_ACPIHANDLE_INIT(gfan);
5867 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005868
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005869 if (gfan_handle) {
5870 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005871 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005872 } else {
5873 /* all other ThinkPads: note that even old-style
5874 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005875 if (likely(acpi_ec_read(fan_status_offset,
5876 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005877 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005878
5879 /* In some ThinkPads, neither the EC nor the ACPI
5880 * DSDT initialize the fan status, and it ends up
5881 * being set to 0x07 when it *could* be either
5882 * 0x07 or 0x80.
5883 *
5884 * Enable for TP-1Y (T43), TP-78 (R51e),
5885 * TP-76 (R52), TP-70 (T43, R52), which are known
5886 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005887 if (fan_control_initial_status == 0x07) {
5888 switch (thinkpad_id.ec_model) {
5889 case 0x5931: /* TP-1Y */
5890 case 0x3837: /* TP-78 */
5891 case 0x3637: /* TP-76 */
5892 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005893 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005894 "fan_init: initial fan status "
5895 "is unknown, assuming it is "
5896 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005897 tp_features.fan_ctrl_status_undef = 1;
5898 ;;
5899 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005900 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005901 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005902 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005903 "ThinkPad ACPI EC access misbehaving, "
5904 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005905 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005906 }
5907 }
5908
5909 if (sfan_handle) {
5910 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005911 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005912 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005913 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005914 } else {
5915 if (!gfan_handle) {
5916 /* gfan without sfan means no fan control */
5917 /* all other models implement TP EC 0x2f control */
5918
5919 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005920 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005921 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005922 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005923 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005924 TPACPI_FAN_CMD_SPEED |
5925 TPACPI_FAN_CMD_LEVEL |
5926 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005927 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005928 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005929 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005930 TPACPI_FAN_CMD_LEVEL |
5931 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005932 }
5933 }
5934 }
5935
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005936 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5937 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5938 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5939 fan_status_access_mode, fan_control_access_mode);
5940
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005941 /* fan control master switch */
5942 if (!fan_control_allowed) {
5943 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5944 fan_control_commands = 0;
5945 dbg_printk(TPACPI_DBG_INIT,
5946 "fan control features disabled by parameter\n");
5947 }
5948
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005949 /* update fan_control_desired_level */
5950 if (fan_status_access_mode != TPACPI_FAN_NONE)
5951 fan_get_status_safe(NULL);
5952
5953 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5954 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005955 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005956 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005957 if (rc < 0)
5958 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005959
5960 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5961 &driver_attr_fan_watchdog);
5962 if (rc < 0) {
5963 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5964 &fan_attr_group);
5965 return rc;
5966 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005967 return 0;
5968 } else
5969 return 1;
5970}
5971
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005972static void fan_exit(void)
5973{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005974 vdbg_printk(TPACPI_DBG_EXIT,
5975 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005976
5977 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005978 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005979 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5980 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005981
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005982 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005983 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005984}
5985
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005986static int fan_read(char *p)
5987{
5988 int len = 0;
5989 int rc;
5990 u8 status;
5991 unsigned int speed = 0;
5992
5993 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005994 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005995 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005996 rc = fan_get_status_safe(&status);
5997 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005998 return rc;
5999
6000 len += sprintf(p + len, "status:\t\t%s\n"
6001 "level:\t\t%d\n",
6002 (status != 0) ? "enabled" : "disabled", status);
6003 break;
6004
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006005 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006006 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006007 rc = fan_get_status_safe(&status);
6008 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006009 return rc;
6010
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006011 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006012 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006013 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006014 else
6015 /* Return most likely status. In fact, it
6016 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006017 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006018 }
6019
6020 len += sprintf(p + len, "status:\t\t%s\n",
6021 (status != 0) ? "enabled" : "disabled");
6022
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006023 rc = fan_get_speed(&speed);
6024 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006025 return rc;
6026
6027 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6028
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006029 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006030 /* Disengaged mode takes precedence */
6031 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006032 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006033 len += sprintf(p + len, "level:\t\tauto\n");
6034 else
6035 len += sprintf(p + len, "level:\t\t%d\n", status);
6036 break;
6037
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006038 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006039 default:
6040 len += sprintf(p + len, "status:\t\tnot supported\n");
6041 }
6042
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006043 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006044 len += sprintf(p + len, "commands:\tlevel <level>");
6045
6046 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006047 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006048 len += sprintf(p + len, " (<level> is 0-7)\n");
6049 break;
6050
6051 default:
6052 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006053 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006054 break;
6055 }
6056 }
6057
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006058 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006059 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006060 "commands:\twatchdog <timeout> (<timeout> "
6061 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006062
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006063 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006064 len += sprintf(p + len, "commands:\tspeed <speed>"
6065 " (<speed> is 0-65535)\n");
6066
6067 return len;
6068}
6069
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006070static int fan_write_cmd_level(const char *cmd, int *rc)
6071{
6072 int level;
6073
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006074 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006075 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006076 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006077 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006078 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006079 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006080 return 0;
6081
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006082 *rc = fan_set_level_safe(level);
6083 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006084 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006085 "access mode %d", fan_control_access_mode);
6086
6087 return 1;
6088}
6089
6090static int fan_write_cmd_enable(const char *cmd, int *rc)
6091{
6092 if (strlencmp(cmd, "enable") != 0)
6093 return 0;
6094
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006095 *rc = fan_set_enable();
6096 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006097 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006098 "access mode %d", fan_control_access_mode);
6099
6100 return 1;
6101}
6102
6103static int fan_write_cmd_disable(const char *cmd, int *rc)
6104{
6105 if (strlencmp(cmd, "disable") != 0)
6106 return 0;
6107
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006108 *rc = fan_set_disable();
6109 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006110 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006111 "access mode %d", fan_control_access_mode);
6112
6113 return 1;
6114}
6115
6116static int fan_write_cmd_speed(const char *cmd, int *rc)
6117{
6118 int speed;
6119
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006120 /* TODO:
6121 * Support speed <low> <medium> <high> ? */
6122
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006123 if (sscanf(cmd, "speed %d", &speed) != 1)
6124 return 0;
6125
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006126 *rc = fan_set_speed(speed);
6127 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006128 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006129 "access mode %d", fan_control_access_mode);
6130
6131 return 1;
6132}
6133
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006134static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6135{
6136 int interval;
6137
6138 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6139 return 0;
6140
6141 if (interval < 0 || interval > 120)
6142 *rc = -EINVAL;
6143 else
6144 fan_watchdog_maxinterval = interval;
6145
6146 return 1;
6147}
6148
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006149static int fan_write(char *buf)
6150{
6151 char *cmd;
6152 int rc = 0;
6153
6154 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006155 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006156 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006157 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006158 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006159 fan_write_cmd_disable(cmd, &rc) ||
6160 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006161 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006162 fan_write_cmd_speed(cmd, &rc))
6163 )
6164 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006165 else if (!rc)
6166 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006167 }
6168
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006169 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006170}
6171
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006172static struct ibm_struct fan_driver_data = {
6173 .name = "fan",
6174 .read = fan_read,
6175 .write = fan_write,
6176 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006177};
6178
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006179/****************************************************************************
6180 ****************************************************************************
6181 *
6182 * Infrastructure
6183 *
6184 ****************************************************************************
6185 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006187/* sysfs name ---------------------------------------------------------- */
6188static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6189 struct device_attribute *attr,
6190 char *buf)
6191{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006192 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006193}
6194
6195static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6196 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6197
6198/* --------------------------------------------------------------------- */
6199
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006200/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03006201static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006202
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006203/*
6204 * Module and infrastructure proble, init and exit handling
6205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006207static int force_load;
6208
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006209#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006210static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006211{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006212 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006213
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006214 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006215}
6216#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6217
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006218static void ibm_exit(struct ibm_struct *ibm)
6219{
6220 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6221
6222 list_del_init(&ibm->all_drivers);
6223
6224 if (ibm->flags.acpi_notify_installed) {
6225 dbg_printk(TPACPI_DBG_EXIT,
6226 "%s: acpi_remove_notify_handler\n", ibm->name);
6227 BUG_ON(!ibm->acpi);
6228 acpi_remove_notify_handler(*ibm->acpi->handle,
6229 ibm->acpi->type,
6230 dispatch_acpi_notify);
6231 ibm->flags.acpi_notify_installed = 0;
6232 ibm->flags.acpi_notify_installed = 0;
6233 }
6234
6235 if (ibm->flags.proc_created) {
6236 dbg_printk(TPACPI_DBG_EXIT,
6237 "%s: remove_proc_entry\n", ibm->name);
6238 remove_proc_entry(ibm->name, proc_dir);
6239 ibm->flags.proc_created = 0;
6240 }
6241
6242 if (ibm->flags.acpi_driver_registered) {
6243 dbg_printk(TPACPI_DBG_EXIT,
6244 "%s: acpi_bus_unregister_driver\n", ibm->name);
6245 BUG_ON(!ibm->acpi);
6246 acpi_bus_unregister_driver(ibm->acpi->driver);
6247 kfree(ibm->acpi->driver);
6248 ibm->acpi->driver = NULL;
6249 ibm->flags.acpi_driver_registered = 0;
6250 }
6251
6252 if (ibm->flags.init_called && ibm->exit) {
6253 ibm->exit();
6254 ibm->flags.init_called = 0;
6255 }
6256
6257 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6258}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006259
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006260static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261{
6262 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006263 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 struct proc_dir_entry *entry;
6265
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006266 BUG_ON(ibm == NULL);
6267
6268 INIT_LIST_HEAD(&ibm->all_drivers);
6269
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006270 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 return 0;
6272
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006273 dbg_printk(TPACPI_DBG_INIT,
6274 "probing for %s\n", ibm->name);
6275
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006276 if (iibm->init) {
6277 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006278 if (ret > 0)
6279 return 0; /* probe failed */
6280 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006282
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006283 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 }
6285
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006286 if (ibm->acpi) {
6287 if (ibm->acpi->hid) {
6288 ret = register_tpacpi_subdriver(ibm);
6289 if (ret)
6290 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006291 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006292
6293 if (ibm->acpi->notify) {
6294 ret = setup_acpi_notify(ibm);
6295 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006296 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006297 ibm->name);
6298 ret = 0;
6299 goto err_out;
6300 }
6301 if (ret < 0)
6302 goto err_out;
6303 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006304 }
6305
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006306 dbg_printk(TPACPI_DBG_INIT,
6307 "%s installed\n", ibm->name);
6308
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006309 if (ibm->read) {
6310 entry = create_proc_entry(ibm->name,
6311 S_IFREG | S_IRUGO | S_IWUSR,
6312 proc_dir);
6313 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006314 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006315 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006316 ret = -ENODEV;
6317 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006318 }
6319 entry->owner = THIS_MODULE;
6320 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006321 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006322 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006323 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006324 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006327 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6328
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006330
6331err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006332 dbg_printk(TPACPI_DBG_INIT,
6333 "%s: at error exit path with result %d\n",
6334 ibm->name, ret);
6335
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006336 ibm_exit(ibm);
6337 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338}
6339
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006340/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006342/* returns 0 - probe ok, or < 0 - probe error.
6343 * Probe ok doesn't mean thinkpad found.
6344 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6345static int __must_check __init get_thinkpad_model_data(
6346 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006347{
Jeff Garzik18552562007-10-03 15:15:40 -04006348 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006349 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006350 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006351
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006352 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006353 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006354
6355 memset(tp, 0, sizeof(*tp));
6356
6357 if (dmi_name_in_vendors("IBM"))
6358 tp->vendor = PCI_VENDOR_ID_IBM;
6359 else if (dmi_name_in_vendors("LENOVO"))
6360 tp->vendor = PCI_VENDOR_ID_LENOVO;
6361 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006362 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006363
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006364 s = dmi_get_system_info(DMI_BIOS_VERSION);
6365 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6366 if (s && !tp->bios_version_str)
6367 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006368 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006369 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006370 tp->bios_model = tp->bios_version_str[0]
6371 | (tp->bios_version_str[1] << 8);
6372
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006373 /*
6374 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6375 * X32 or newer, all Z series; Some models must have an
6376 * up-to-date BIOS or they will not be detected.
6377 *
6378 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6379 */
6380 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006381 if (sscanf(dev->name,
6382 "IBM ThinkPad Embedded Controller -[%17c",
6383 ec_fw_string) == 1) {
6384 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6385 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006386
6387 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006388 if (!tp->ec_version_str)
6389 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006390 tp->ec_model = ec_fw_string[0]
6391 | (ec_fw_string[1] << 8);
6392 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006393 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006394 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006395
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006396 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6397 if (s && !strnicmp(s, "ThinkPad", 8)) {
6398 tp->model_str = kstrdup(s, GFP_KERNEL);
6399 if (!tp->model_str)
6400 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006401 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006402
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006403 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6404 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6405 if (s && !tp->nummodel_str)
6406 return -ENOMEM;
6407
6408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409}
6410
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006411static int __init probe_for_thinkpad(void)
6412{
6413 int is_thinkpad;
6414
6415 if (acpi_disabled)
6416 return -ENODEV;
6417
6418 /*
6419 * Non-ancient models have better DMI tagging, but very old models
6420 * don't.
6421 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006422 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006423
6424 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006425 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006426 if (!ec_handle) {
6427 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006428 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006429 "Not yet supported ThinkPad detected!\n");
6430 return -ENODEV;
6431 }
6432
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006433 /*
6434 * Risks a regression on very old machines, but reduces potential
6435 * false positives a damn great deal
6436 */
6437 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006438 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006439
6440 if (!is_thinkpad && !force_load)
6441 return -ENODEV;
6442
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006443 return 0;
6444}
6445
6446
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006447/* Module init, exit, parameters */
6448
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006449static struct ibm_init_struct ibms_init[] __initdata = {
6450 {
6451 .init = thinkpad_acpi_driver_init,
6452 .data = &thinkpad_acpi_driver_data,
6453 },
6454 {
6455 .init = hotkey_init,
6456 .data = &hotkey_driver_data,
6457 },
6458 {
6459 .init = bluetooth_init,
6460 .data = &bluetooth_driver_data,
6461 },
6462 {
6463 .init = wan_init,
6464 .data = &wan_driver_data,
6465 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006466#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006467 {
6468 .init = video_init,
6469 .data = &video_driver_data,
6470 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006471#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006472 {
6473 .init = light_init,
6474 .data = &light_driver_data,
6475 },
6476#ifdef CONFIG_THINKPAD_ACPI_DOCK
6477 {
6478 .init = dock_init,
6479 .data = &dock_driver_data[0],
6480 },
6481 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03006482 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006483 .data = &dock_driver_data[1],
6484 },
6485#endif
6486#ifdef CONFIG_THINKPAD_ACPI_BAY
6487 {
6488 .init = bay_init,
6489 .data = &bay_driver_data,
6490 },
6491#endif
6492 {
6493 .init = cmos_init,
6494 .data = &cmos_driver_data,
6495 },
6496 {
6497 .init = led_init,
6498 .data = &led_driver_data,
6499 },
6500 {
6501 .init = beep_init,
6502 .data = &beep_driver_data,
6503 },
6504 {
6505 .init = thermal_init,
6506 .data = &thermal_driver_data,
6507 },
6508 {
6509 .data = &ecdump_driver_data,
6510 },
6511 {
6512 .init = brightness_init,
6513 .data = &brightness_driver_data,
6514 },
6515 {
6516 .data = &volume_driver_data,
6517 },
6518 {
6519 .init = fan_init,
6520 .data = &fan_driver_data,
6521 },
6522};
6523
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006524static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6525{
6526 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006527 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006528
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006529 if (!kp || !kp->name || !val)
6530 return -EINVAL;
6531
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006532 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6533 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006534 WARN_ON(ibm == NULL);
6535
6536 if (!ibm || !ibm->name)
6537 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006538
6539 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6540 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006541 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006542 strcpy(ibms_init[i].param, val);
6543 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006544 return 0;
6545 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006546 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006547
6548 return -EINVAL;
6549}
6550
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006551module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006552MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006553 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006554
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006555module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006556MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006557
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006558module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006559MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006560 "Attempts to load the driver even on a "
6561 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006562
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006563module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006564MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006565 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006566
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006567module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006568MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006569 "Selects brightness control strategy: "
6570 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006571
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006572module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006573MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006574 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006575
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006576module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006577MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006578 "used for backwards compatibility with userspace, "
6579 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006580
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006581#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006582 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006583 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006584 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006585
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006586TPACPI_PARAM(hotkey);
6587TPACPI_PARAM(bluetooth);
6588TPACPI_PARAM(video);
6589TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006590#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006591TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006592#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006593#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006594TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006595#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006596TPACPI_PARAM(cmos);
6597TPACPI_PARAM(led);
6598TPACPI_PARAM(beep);
6599TPACPI_PARAM(ecdump);
6600TPACPI_PARAM(brightness);
6601TPACPI_PARAM(volume);
6602TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006603
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006604static void thinkpad_acpi_module_exit(void)
6605{
6606 struct ibm_struct *ibm, *itmp;
6607
6608 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6609
6610 list_for_each_entry_safe_reverse(ibm, itmp,
6611 &tpacpi_all_drivers,
6612 all_drivers) {
6613 ibm_exit(ibm);
6614 }
6615
6616 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6617
6618 if (tpacpi_inputdev) {
6619 if (tp_features.input_device_registered)
6620 input_unregister_device(tpacpi_inputdev);
6621 else
6622 input_free_device(tpacpi_inputdev);
6623 }
6624
6625 if (tpacpi_hwmon)
6626 hwmon_device_unregister(tpacpi_hwmon);
6627
6628 if (tp_features.sensors_pdev_attrs_registered)
6629 device_remove_file(&tpacpi_sensors_pdev->dev,
6630 &dev_attr_thinkpad_acpi_pdev_name);
6631 if (tpacpi_sensors_pdev)
6632 platform_device_unregister(tpacpi_sensors_pdev);
6633 if (tpacpi_pdev)
6634 platform_device_unregister(tpacpi_pdev);
6635
6636 if (tp_features.sensors_pdrv_attrs_registered)
6637 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6638 if (tp_features.platform_drv_attrs_registered)
6639 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6640
6641 if (tp_features.sensors_pdrv_registered)
6642 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6643
6644 if (tp_features.platform_drv_registered)
6645 platform_driver_unregister(&tpacpi_pdriver);
6646
6647 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006648 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006649
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006650 if (tpacpi_wq)
6651 destroy_workqueue(tpacpi_wq);
6652
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006653 kfree(thinkpad_id.bios_version_str);
6654 kfree(thinkpad_id.ec_version_str);
6655 kfree(thinkpad_id.model_str);
6656}
6657
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006658
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006659static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660{
6661 int ret, i;
6662
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006663 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6664
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006665 /* Parameter checking */
6666 if (hotkey_report_mode > 2)
6667 return -EINVAL;
6668
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006669 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006670
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006671 ret = get_thinkpad_model_data(&thinkpad_id);
6672 if (ret) {
6673 printk(TPACPI_ERR
6674 "unable to get DMI data: %d\n", ret);
6675 thinkpad_acpi_module_exit();
6676 return ret;
6677 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006678 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006679 if (ret) {
6680 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006681 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006684 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006685
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006686 TPACPI_ACPIHANDLE_INIT(ecrd);
6687 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006689 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6690 if (!tpacpi_wq) {
6691 thinkpad_acpi_module_exit();
6692 return -ENOMEM;
6693 }
6694
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006695 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006697 printk(TPACPI_ERR
6698 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006699 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 return -ENODEV;
6701 }
6702 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006703
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006704 ret = platform_driver_register(&tpacpi_pdriver);
6705 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006706 printk(TPACPI_ERR
6707 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006708 thinkpad_acpi_module_exit();
6709 return ret;
6710 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006711 tp_features.platform_drv_registered = 1;
6712
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006713 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6714 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006715 printk(TPACPI_ERR
6716 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006717 thinkpad_acpi_module_exit();
6718 return ret;
6719 }
6720 tp_features.sensors_pdrv_registered = 1;
6721
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006722 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006723 if (!ret) {
6724 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006725 ret = tpacpi_create_driver_attributes(
6726 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006727 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006728 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006729 printk(TPACPI_ERR
6730 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006731 thinkpad_acpi_module_exit();
6732 return ret;
6733 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006734 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006735
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006736
6737 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006738 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006739 NULL, 0);
6740 if (IS_ERR(tpacpi_pdev)) {
6741 ret = PTR_ERR(tpacpi_pdev);
6742 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006743 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006744 thinkpad_acpi_module_exit();
6745 return ret;
6746 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006747 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006748 TPACPI_HWMON_DRVR_NAME,
6749 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006750 if (IS_ERR(tpacpi_sensors_pdev)) {
6751 ret = PTR_ERR(tpacpi_sensors_pdev);
6752 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006753 printk(TPACPI_ERR
6754 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006755 thinkpad_acpi_module_exit();
6756 return ret;
6757 }
6758 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6759 &dev_attr_thinkpad_acpi_pdev_name);
6760 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006761 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006762 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006763 thinkpad_acpi_module_exit();
6764 return ret;
6765 }
6766 tp_features.sensors_pdev_attrs_registered = 1;
6767 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006768 if (IS_ERR(tpacpi_hwmon)) {
6769 ret = PTR_ERR(tpacpi_hwmon);
6770 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006771 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006772 thinkpad_acpi_module_exit();
6773 return ret;
6774 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006775 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006776 tpacpi_inputdev = input_allocate_device();
6777 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006778 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006779 thinkpad_acpi_module_exit();
6780 return -ENOMEM;
6781 } else {
6782 /* Prepare input device, but don't register */
6783 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006784 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006785 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006786 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6787 thinkpad_id.vendor :
6788 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006789 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6790 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6791 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006792 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6793 ret = ibm_init(&ibms_init[i]);
6794 if (ret >= 0 && *ibms_init[i].param)
6795 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006797 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 return ret;
6799 }
6800 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006801 ret = input_register_device(tpacpi_inputdev);
6802 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006803 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006804 thinkpad_acpi_module_exit();
6805 return ret;
6806 } else {
6807 tp_features.input_device_registered = 1;
6808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006810 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 return 0;
6812}
6813
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006814/* Please remove this in year 2009 */
6815MODULE_ALIAS("ibm_acpi");
6816
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03006817MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6818
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006819/*
6820 * DMI matching for module autoloading
6821 *
6822 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6823 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6824 *
6825 * Only models listed in thinkwiki will be supported, so add yours
6826 * if it is not there yet.
6827 */
6828#define IBM_BIOS_MODULE_ALIAS(__type) \
6829 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6830
6831/* Non-ancient thinkpads */
6832MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6833MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6834
6835/* Ancient thinkpad BIOSes have to be identified by
6836 * BIOS type or model number, and there are far less
6837 * BIOS types than model numbers... */
6838IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6839IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6840IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6841
6842MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006843MODULE_DESCRIPTION(TPACPI_DESC);
6844MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006845MODULE_LICENSE("GPL");
6846
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006847module_init(thinkpad_acpi_module_init);
6848module_exit(thinkpad_acpi_module_exit);