blob: 6c9b6c892288144f67671aeb0d9cee43df000739 [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) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300585 printk(TPACPI_ERR
586 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300587 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300588 }
589
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200590 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300591 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200592
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300593 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300594
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300595 rc = acpi_bus_register_driver(ibm->acpi->driver);
596 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200597 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200598 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300599 kfree(ibm->acpi->driver);
600 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300601 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300602 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300603
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300604 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300605}
606
607
608/****************************************************************************
609 ****************************************************************************
610 *
611 * Procfs Helpers
612 *
613 ****************************************************************************
614 ****************************************************************************/
615
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300616static int dispatch_procfs_read(char *page, char **start, off_t off,
617 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300618{
619 struct ibm_struct *ibm = data;
620 int len;
621
622 if (!ibm || !ibm->read)
623 return -EINVAL;
624
625 len = ibm->read(page);
626 if (len < 0)
627 return len;
628
629 if (len <= off + count)
630 *eof = 1;
631 *start = page + off;
632 len -= off;
633 if (len > count)
634 len = count;
635 if (len < 0)
636 len = 0;
637
638 return len;
639}
640
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300641static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200642 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300643 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300644{
645 struct ibm_struct *ibm = data;
646 char *kernbuf;
647 int ret;
648
649 if (!ibm || !ibm->write)
650 return -EINVAL;
651
652 kernbuf = kmalloc(count + 2, GFP_KERNEL);
653 if (!kernbuf)
654 return -ENOMEM;
655
656 if (copy_from_user(kernbuf, userbuf, count)) {
657 kfree(kernbuf);
658 return -EFAULT;
659 }
660
661 kernbuf[count] = 0;
662 strcat(kernbuf, ",");
663 ret = ibm->write(kernbuf);
664 if (ret == 0)
665 ret = count;
666
667 kfree(kernbuf);
668
669 return ret;
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static char *next_cmd(char **cmds)
673{
674 char *start = *cmds;
675 char *end;
676
677 while ((end = strchr(start, ',')) && end == start)
678 start = end + 1;
679
680 if (!end)
681 return NULL;
682
683 *end = 0;
684 *cmds = end + 1;
685 return start;
686}
687
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300688
689/****************************************************************************
690 ****************************************************************************
691 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300692 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300693 *
694 ****************************************************************************
695 ****************************************************************************/
696
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300697static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300698static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700699static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300700static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300701static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200702static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300703
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200704static int tpacpi_suspend_handler(struct platform_device *pdev,
705 pm_message_t state)
706{
707 struct ibm_struct *ibm, *itmp;
708
709 list_for_each_entry_safe(ibm, itmp,
710 &tpacpi_all_drivers,
711 all_drivers) {
712 if (ibm->suspend)
713 (ibm->suspend)(state);
714 }
715
716 return 0;
717}
718
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300719static int tpacpi_resume_handler(struct platform_device *pdev)
720{
721 struct ibm_struct *ibm, *itmp;
722
723 list_for_each_entry_safe(ibm, itmp,
724 &tpacpi_all_drivers,
725 all_drivers) {
726 if (ibm->resume)
727 (ibm->resume)();
728 }
729
730 return 0;
731}
732
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300733static struct platform_driver tpacpi_pdriver = {
734 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200735 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300736 .owner = THIS_MODULE,
737 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200738 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300739 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300740};
741
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300742static struct platform_driver tpacpi_hwmon_pdriver = {
743 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200744 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300745 .owner = THIS_MODULE,
746 },
747};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300748
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300749/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300750 * sysfs support helpers
751 */
752
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200753struct attribute_set {
754 unsigned int members, max_members;
755 struct attribute_group group;
756};
757
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300758struct attribute_set_obj {
759 struct attribute_set s;
760 struct attribute *a;
761} __attribute__((packed));
762
763static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200764 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300765{
766 struct attribute_set_obj *sobj;
767
768 if (max_members == 0)
769 return NULL;
770
771 /* Allocates space for implicit NULL at the end too */
772 sobj = kzalloc(sizeof(struct attribute_set_obj) +
773 max_members * sizeof(struct attribute *),
774 GFP_KERNEL);
775 if (!sobj)
776 return NULL;
777 sobj->s.max_members = max_members;
778 sobj->s.group.attrs = &sobj->a;
779 sobj->s.group.name = name;
780
781 return &sobj->s;
782}
783
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200784#define destroy_attr_set(_set) \
785 kfree(_set);
786
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300787/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200788static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300789{
790 if (!s || !attr)
791 return -EINVAL;
792
793 if (s->members >= s->max_members)
794 return -ENOMEM;
795
796 s->group.attrs[s->members] = attr;
797 s->members++;
798
799 return 0;
800}
801
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200802static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300803 struct attribute **attr,
804 unsigned int count)
805{
806 int i, res;
807
808 for (i = 0; i < count; i++) {
809 res = add_to_attr_set(s, attr[i]);
810 if (res)
811 return res;
812 }
813
814 return 0;
815}
816
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200817static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300818{
819 sysfs_remove_group(kobj, &s->group);
820 destroy_attr_set(s);
821}
822
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200823#define register_attr_set_with_sysfs(_attr_set, _kobj) \
824 sysfs_create_group(_kobj, &_attr_set->group)
825
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300826static int parse_strtoul(const char *buf,
827 unsigned long max, unsigned long *value)
828{
829 char *endp;
830
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300831 while (*buf && isspace(*buf))
832 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300833 *value = simple_strtoul(buf, &endp, 0);
834 while (*endp && isspace(*endp))
835 endp++;
836 if (*endp || *value > max)
837 return -EINVAL;
838
839 return 0;
840}
841
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300842static int __init tpacpi_query_bcl_levels(acpi_handle handle)
843{
844 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
845 union acpi_object *obj;
846 int rc;
847
848 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
849 obj = (union acpi_object *)buffer.pointer;
850 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
851 printk(TPACPI_ERR "Unknown _BCL data, "
852 "please report this to %s\n", TPACPI_MAIL);
853 rc = 0;
854 } else {
855 rc = obj->package.count;
856 }
857 } else {
858 return 0;
859 }
860
861 kfree(buffer.pointer);
862 return rc;
863}
864
865static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
866 u32 lvl, void *context, void **rv)
867{
868 char name[ACPI_PATH_SEGMENT_LENGTH];
869 struct acpi_buffer buffer = { sizeof(name), &name };
870
871 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
872 !strncmp("_BCL", name, sizeof(name) - 1)) {
873 BUG_ON(!rv || !*rv);
874 **(int **)rv = tpacpi_query_bcl_levels(handle);
875 return AE_CTRL_TERMINATE;
876 } else {
877 return AE_OK;
878 }
879}
880
881/*
882 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
883 */
884static int __init tpacpi_check_std_acpi_brightness_support(void)
885{
886 int status;
887 int bcl_levels = 0;
888 void *bcl_ptr = &bcl_levels;
889
890 if (!vid_handle) {
891 TPACPI_ACPIHANDLE_INIT(vid);
892 }
893 if (!vid_handle)
894 return 0;
895
896 /*
897 * Search for a _BCL method, and execute it. This is safe on all
898 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
899 * BIOS in ACPI backlight control mode. We do NOT have to care
900 * about calling the _BCL method in an enabled video device, any
901 * will do for our purposes.
902 */
903
904 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
905 tpacpi_acpi_walk_find_bcl, NULL,
906 &bcl_ptr);
907
908 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
909 tp_features.bright_acpimode = 1;
910 return (bcl_levels - 2);
911 }
912
913 return 0;
914}
915
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300916static int __init tpacpi_new_rfkill(const unsigned int id,
917 struct rfkill **rfk,
918 const enum rfkill_type rfktype,
919 const char *name,
920 int (*toggle_radio)(void *, enum rfkill_state),
921 int (*get_state)(void *, enum rfkill_state *))
922{
923 int res;
924 enum rfkill_state initial_state;
925
926 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
927 if (!*rfk) {
928 printk(TPACPI_ERR
929 "failed to allocate memory for rfkill class\n");
930 return -ENOMEM;
931 }
932
933 (*rfk)->name = name;
934 (*rfk)->get_state = get_state;
935 (*rfk)->toggle_radio = toggle_radio;
936
937 if (!get_state(NULL, &initial_state))
938 (*rfk)->state = initial_state;
939
940 res = rfkill_register(*rfk);
941 if (res < 0) {
942 printk(TPACPI_ERR
943 "failed to register %s rfkill switch: %d\n",
944 name, res);
945 rfkill_free(*rfk);
946 *rfk = NULL;
947 return res;
948 }
949
950 return 0;
951}
952
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200953/*************************************************************************
954 * thinkpad-acpi driver attributes
955 */
956
957/* interface_version --------------------------------------------------- */
958static ssize_t tpacpi_driver_interface_version_show(
959 struct device_driver *drv,
960 char *buf)
961{
962 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
963}
964
965static DRIVER_ATTR(interface_version, S_IRUGO,
966 tpacpi_driver_interface_version_show, NULL);
967
968/* debug_level --------------------------------------------------------- */
969static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
970 char *buf)
971{
972 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
973}
974
975static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
976 const char *buf, size_t count)
977{
978 unsigned long t;
979
980 if (parse_strtoul(buf, 0xffff, &t))
981 return -EINVAL;
982
983 dbg_level = t;
984
985 return count;
986}
987
988static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
989 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
990
991/* version ------------------------------------------------------------- */
992static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
993 char *buf)
994{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200995 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
996 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200997}
998
999static DRIVER_ATTR(version, S_IRUGO,
1000 tpacpi_driver_version_show, NULL);
1001
1002/* --------------------------------------------------------------------- */
1003
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001004static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001005 &driver_attr_debug_level, &driver_attr_version,
1006 &driver_attr_interface_version,
1007};
1008
1009static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1010{
1011 int i, res;
1012
1013 i = 0;
1014 res = 0;
1015 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1016 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1017 i++;
1018 }
1019
1020 return res;
1021}
1022
1023static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1024{
1025 int i;
1026
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001027 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001028 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1029}
1030
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001031/****************************************************************************
1032 ****************************************************************************
1033 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001034 * Subdrivers
1035 *
1036 ****************************************************************************
1037 ****************************************************************************/
1038
1039/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001040 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001041 */
1042
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001043static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001045 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1046 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001048 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001049 (thinkpad_id.bios_version_str) ?
1050 thinkpad_id.bios_version_str : "unknown",
1051 (thinkpad_id.ec_version_str) ?
1052 thinkpad_id.ec_version_str : "unknown");
1053
1054 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001055 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001056 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1057 "IBM" : ((thinkpad_id.vendor ==
1058 PCI_VENDOR_ID_LENOVO) ?
1059 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001060 thinkpad_id.model_str,
1061 (thinkpad_id.nummodel_str) ?
1062 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return 0;
1065}
1066
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001067static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 int len = 0;
1070
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001071 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1072 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 return len;
1075}
1076
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001077static struct ibm_struct thinkpad_acpi_driver_data = {
1078 .name = "driver",
1079 .read = thinkpad_acpi_driver_read,
1080};
1081
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001082/*************************************************************************
1083 * Hotkey subdriver
1084 */
1085
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001086enum { /* hot key scan codes (derived from ACPI DSDT) */
1087 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1088 TP_ACPI_HOTKEYSCAN_FNF2,
1089 TP_ACPI_HOTKEYSCAN_FNF3,
1090 TP_ACPI_HOTKEYSCAN_FNF4,
1091 TP_ACPI_HOTKEYSCAN_FNF5,
1092 TP_ACPI_HOTKEYSCAN_FNF6,
1093 TP_ACPI_HOTKEYSCAN_FNF7,
1094 TP_ACPI_HOTKEYSCAN_FNF8,
1095 TP_ACPI_HOTKEYSCAN_FNF9,
1096 TP_ACPI_HOTKEYSCAN_FNF10,
1097 TP_ACPI_HOTKEYSCAN_FNF11,
1098 TP_ACPI_HOTKEYSCAN_FNF12,
1099 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1100 TP_ACPI_HOTKEYSCAN_FNINSERT,
1101 TP_ACPI_HOTKEYSCAN_FNDELETE,
1102 TP_ACPI_HOTKEYSCAN_FNHOME,
1103 TP_ACPI_HOTKEYSCAN_FNEND,
1104 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1105 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1106 TP_ACPI_HOTKEYSCAN_FNSPACE,
1107 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1108 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1109 TP_ACPI_HOTKEYSCAN_MUTE,
1110 TP_ACPI_HOTKEYSCAN_THINKPAD,
1111};
1112
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001113enum { /* Keys available through NVRAM polling */
1114 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1115 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1116};
1117
1118enum { /* Positions of some of the keys in hotkey masks */
1119 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1120 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1121 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1122 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1123 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1124 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1125 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1126 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1127 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1128 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1129 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1130};
1131
1132enum { /* NVRAM to ACPI HKEY group map */
1133 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1134 TP_ACPI_HKEY_ZOOM_MASK |
1135 TP_ACPI_HKEY_DISPSWTCH_MASK |
1136 TP_ACPI_HKEY_HIBERNATE_MASK,
1137 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1138 TP_ACPI_HKEY_BRGHTDWN_MASK,
1139 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1140 TP_ACPI_HKEY_VOLDWN_MASK |
1141 TP_ACPI_HKEY_MUTE_MASK,
1142};
1143
1144#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1145struct tp_nvram_state {
1146 u16 thinkpad_toggle:1;
1147 u16 zoom_toggle:1;
1148 u16 display_toggle:1;
1149 u16 thinklight_toggle:1;
1150 u16 hibernate_toggle:1;
1151 u16 displayexp_toggle:1;
1152 u16 display_state:1;
1153 u16 brightness_toggle:1;
1154 u16 volume_toggle:1;
1155 u16 mute:1;
1156
1157 u8 brightness_level;
1158 u8 volume_level;
1159};
1160
1161static struct task_struct *tpacpi_hotkey_task;
1162static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1163static int hotkey_poll_freq = 10; /* Hz */
1164static struct mutex hotkey_thread_mutex;
1165static struct mutex hotkey_thread_data_mutex;
1166static unsigned int hotkey_config_change;
1167
1168#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1169
1170#define hotkey_source_mask 0U
1171
1172#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1173
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001174static struct mutex hotkey_mutex;
1175
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001176static enum { /* Reasons for waking up */
1177 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1178 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1179 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1180} hotkey_wakeup_reason;
1181
1182static int hotkey_autosleep_ack;
1183
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001184static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001185static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001186static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001187static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001188static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001189
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001190static unsigned int hotkey_report_mode;
1191
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001192static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001193
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001194static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001195
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001196#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1197#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001198 do { \
1199 mutex_lock(&hotkey_thread_data_mutex); \
1200 hotkey_config_change++; \
1201 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001202#define HOTKEY_CONFIG_CRITICAL_END \
1203 mutex_unlock(&hotkey_thread_data_mutex);
1204#else
1205#define HOTKEY_CONFIG_CRITICAL_START
1206#define HOTKEY_CONFIG_CRITICAL_END
1207#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1208
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001209/* HKEY.MHKG() return bits */
1210#define TP_HOTKEY_TABLET_MASK (1 << 3)
1211
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001212static int hotkey_get_wlsw(int *status)
1213{
1214 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1215 return -EIO;
1216 return 0;
1217}
1218
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001219static int hotkey_get_tablet_mode(int *status)
1220{
1221 int s;
1222
1223 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1224 return -EIO;
1225
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001226 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1227 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001228}
1229
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001230/*
1231 * Call with hotkey_mutex held
1232 */
1233static int hotkey_mask_get(void)
1234{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001235 u32 m = 0;
1236
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001237 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001238 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001239 return -EIO;
1240 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001241 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001242
1243 return 0;
1244}
1245
1246/*
1247 * Call with hotkey_mutex held
1248 */
1249static int hotkey_mask_set(u32 mask)
1250{
1251 int i;
1252 int rc = 0;
1253
1254 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001255 if (!tp_warned.hotkey_mask_ff &&
1256 (mask == 0xffff || mask == 0xffffff ||
1257 mask == 0xffffffff)) {
1258 tp_warned.hotkey_mask_ff = 1;
1259 printk(TPACPI_NOTICE
1260 "setting the hotkey mask to 0x%08x is likely "
1261 "not the best way to go about it\n", mask);
1262 printk(TPACPI_NOTICE
1263 "please consider using the driver defaults, "
1264 "and refer to up-to-date thinkpad-acpi "
1265 "documentation\n");
1266 }
1267
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001268 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001269 for (i = 0; i < 32; i++) {
1270 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001271 /* enable in firmware mask only keys not in NVRAM
1272 * mode, but enable the key in the cached hotkey_mask
1273 * regardless of mode, or the key will end up
1274 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001275 if (!acpi_evalf(hkey_handle,
1276 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001277 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001278 rc = -EIO;
1279 break;
1280 } else {
1281 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1282 }
1283 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001284 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001285
1286 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001287 if (!hotkey_mask_get() && !rc &&
1288 (hotkey_mask & ~hotkey_source_mask) !=
1289 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001290 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001291 "requested hot key mask 0x%08x, but "
1292 "firmware forced it to 0x%08x\n",
1293 mask, hotkey_mask);
1294 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001295 } else {
1296#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1297 HOTKEY_CONFIG_CRITICAL_START
1298 hotkey_mask = mask & hotkey_source_mask;
1299 HOTKEY_CONFIG_CRITICAL_END
1300 hotkey_mask_get();
1301 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001302 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001303 "requested hot key mask 0x%08x, "
1304 "forced to 0x%08x (NVRAM poll mask is "
1305 "0x%08x): no firmware mask support\n",
1306 mask, hotkey_mask, hotkey_source_mask);
1307 }
1308#else
1309 hotkey_mask_get();
1310 rc = -ENXIO;
1311#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001312 }
1313
1314 return rc;
1315}
1316
1317static int hotkey_status_get(int *status)
1318{
1319 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1320 return -EIO;
1321
1322 return 0;
1323}
1324
1325static int hotkey_status_set(int status)
1326{
1327 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1328 return -EIO;
1329
1330 return 0;
1331}
1332
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001333static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001334{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001335 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001336
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001337 if (tp_features.hotkey_tablet &&
1338 !hotkey_get_tablet_mode(&state)) {
1339 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001340
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001341 input_report_switch(tpacpi_inputdev,
1342 SW_TABLET_MODE, !!state);
1343 input_sync(tpacpi_inputdev);
1344
1345 mutex_unlock(&tpacpi_inputdev_send_mutex);
1346 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001347}
1348
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001349static void tpacpi_input_send_key(unsigned int scancode)
1350{
1351 unsigned int keycode;
1352
1353 keycode = hotkey_keycode_map[scancode];
1354
1355 if (keycode != KEY_RESERVED) {
1356 mutex_lock(&tpacpi_inputdev_send_mutex);
1357
1358 input_report_key(tpacpi_inputdev, keycode, 1);
1359 if (keycode == KEY_UNKNOWN)
1360 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1361 scancode);
1362 input_sync(tpacpi_inputdev);
1363
1364 input_report_key(tpacpi_inputdev, keycode, 0);
1365 if (keycode == KEY_UNKNOWN)
1366 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1367 scancode);
1368 input_sync(tpacpi_inputdev);
1369
1370 mutex_unlock(&tpacpi_inputdev_send_mutex);
1371 }
1372}
1373
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001374#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1375static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1376
1377static void tpacpi_hotkey_send_key(unsigned int scancode)
1378{
1379 tpacpi_input_send_key(scancode);
1380 if (hotkey_report_mode < 2) {
1381 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1382 0x80, 0x1001 + scancode);
1383 }
1384}
1385
1386static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1387{
1388 u8 d;
1389
1390 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1391 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1392 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1393 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1394 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1395 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1396 }
1397 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1398 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1399 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1400 }
1401 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1402 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1403 n->displayexp_toggle =
1404 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1405 }
1406 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1407 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1408 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1409 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1410 n->brightness_toggle =
1411 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1412 }
1413 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1414 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1415 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1416 >> TP_NVRAM_POS_LEVEL_VOLUME;
1417 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1418 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1419 }
1420}
1421
1422#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001423 do { \
1424 if ((mask & (1 << __scancode)) && \
1425 oldn->__member != newn->__member) \
1426 tpacpi_hotkey_send_key(__scancode); \
1427 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001428
1429#define TPACPI_MAY_SEND_KEY(__scancode) \
1430 do { if (mask & (1 << __scancode)) \
1431 tpacpi_hotkey_send_key(__scancode); } while (0)
1432
1433static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001434 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001435 u32 mask)
1436{
1437 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1438 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1439 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1440 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1441
1442 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1443
1444 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1445
1446 /* handle volume */
1447 if (oldn->volume_toggle != newn->volume_toggle) {
1448 if (oldn->mute != newn->mute) {
1449 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1450 }
1451 if (oldn->volume_level > newn->volume_level) {
1452 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1453 } else if (oldn->volume_level < newn->volume_level) {
1454 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1455 } else if (oldn->mute == newn->mute) {
1456 /* repeated key presses that didn't change state */
1457 if (newn->mute) {
1458 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1459 } else if (newn->volume_level != 0) {
1460 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1461 } else {
1462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1463 }
1464 }
1465 }
1466
1467 /* handle brightness */
1468 if (oldn->brightness_toggle != newn->brightness_toggle) {
1469 if (oldn->brightness_level < newn->brightness_level) {
1470 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1471 } else if (oldn->brightness_level > newn->brightness_level) {
1472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1473 } else {
1474 /* repeated key presses that didn't change state */
1475 if (newn->brightness_level != 0) {
1476 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1477 } else {
1478 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1479 }
1480 }
1481 }
1482}
1483
1484#undef TPACPI_COMPARE_KEY
1485#undef TPACPI_MAY_SEND_KEY
1486
1487static int hotkey_kthread(void *data)
1488{
1489 struct tp_nvram_state s[2];
1490 u32 mask;
1491 unsigned int si, so;
1492 unsigned long t;
1493 unsigned int change_detector, must_reset;
1494
1495 mutex_lock(&hotkey_thread_mutex);
1496
1497 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1498 goto exit;
1499
1500 set_freezable();
1501
1502 so = 0;
1503 si = 1;
1504 t = 0;
1505
1506 /* Initial state for compares */
1507 mutex_lock(&hotkey_thread_data_mutex);
1508 change_detector = hotkey_config_change;
1509 mask = hotkey_source_mask & hotkey_mask;
1510 mutex_unlock(&hotkey_thread_data_mutex);
1511 hotkey_read_nvram(&s[so], mask);
1512
1513 while (!kthread_should_stop() && hotkey_poll_freq) {
1514 if (t == 0)
1515 t = 1000/hotkey_poll_freq;
1516 t = msleep_interruptible(t);
1517 if (unlikely(kthread_should_stop()))
1518 break;
1519 must_reset = try_to_freeze();
1520 if (t > 0 && !must_reset)
1521 continue;
1522
1523 mutex_lock(&hotkey_thread_data_mutex);
1524 if (must_reset || hotkey_config_change != change_detector) {
1525 /* forget old state on thaw or config change */
1526 si = so;
1527 t = 0;
1528 change_detector = hotkey_config_change;
1529 }
1530 mask = hotkey_source_mask & hotkey_mask;
1531 mutex_unlock(&hotkey_thread_data_mutex);
1532
1533 if (likely(mask)) {
1534 hotkey_read_nvram(&s[si], mask);
1535 if (likely(si != so)) {
1536 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001537 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001538 }
1539 }
1540
1541 so = si;
1542 si ^= 1;
1543 }
1544
1545exit:
1546 mutex_unlock(&hotkey_thread_mutex);
1547 return 0;
1548}
1549
1550static void hotkey_poll_stop_sync(void)
1551{
1552 if (tpacpi_hotkey_task) {
1553 if (frozen(tpacpi_hotkey_task) ||
1554 freezing(tpacpi_hotkey_task))
1555 thaw_process(tpacpi_hotkey_task);
1556
1557 kthread_stop(tpacpi_hotkey_task);
1558 tpacpi_hotkey_task = NULL;
1559 mutex_lock(&hotkey_thread_mutex);
1560 /* at this point, the thread did exit */
1561 mutex_unlock(&hotkey_thread_mutex);
1562 }
1563}
1564
1565/* call with hotkey_mutex held */
1566static void hotkey_poll_setup(int may_warn)
1567{
1568 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1569 hotkey_poll_freq > 0 &&
1570 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1571 if (!tpacpi_hotkey_task) {
1572 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001573 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001574 if (IS_ERR(tpacpi_hotkey_task)) {
1575 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001576 printk(TPACPI_ERR
1577 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001578 "for hotkey polling\n");
1579 }
1580 }
1581 } else {
1582 hotkey_poll_stop_sync();
1583 if (may_warn &&
1584 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001585 printk(TPACPI_NOTICE
1586 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001587 "which is currently disabled\n",
1588 hotkey_source_mask);
1589 }
1590 }
1591}
1592
1593static void hotkey_poll_setup_safe(int may_warn)
1594{
1595 mutex_lock(&hotkey_mutex);
1596 hotkey_poll_setup(may_warn);
1597 mutex_unlock(&hotkey_mutex);
1598}
1599
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001600#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1601
1602static void hotkey_poll_setup_safe(int __unused)
1603{
1604}
1605
1606#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1607
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001608static int hotkey_inputdev_open(struct input_dev *dev)
1609{
1610 switch (tpacpi_lifecycle) {
1611 case TPACPI_LIFE_INIT:
1612 /*
1613 * hotkey_init will call hotkey_poll_setup_safe
1614 * at the appropriate moment
1615 */
1616 return 0;
1617 case TPACPI_LIFE_EXITING:
1618 return -EBUSY;
1619 case TPACPI_LIFE_RUNNING:
1620 hotkey_poll_setup_safe(0);
1621 return 0;
1622 }
1623
1624 /* Should only happen if tpacpi_lifecycle is corrupt */
1625 BUG();
1626 return -EBUSY;
1627}
1628
1629static void hotkey_inputdev_close(struct input_dev *dev)
1630{
1631 /* disable hotkey polling when possible */
1632 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1633 hotkey_poll_setup_safe(0);
1634}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001635
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001636/* sysfs hotkey enable ------------------------------------------------- */
1637static ssize_t hotkey_enable_show(struct device *dev,
1638 struct device_attribute *attr,
1639 char *buf)
1640{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001641 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001642
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001643 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001644 if (res)
1645 return res;
1646
1647 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1648}
1649
1650static ssize_t hotkey_enable_store(struct device *dev,
1651 struct device_attribute *attr,
1652 const char *buf, size_t count)
1653{
1654 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001655 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001656
1657 if (parse_strtoul(buf, 1, &t))
1658 return -EINVAL;
1659
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001660 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001661
1662 return (res) ? res : count;
1663}
1664
1665static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001666 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001667 hotkey_enable_show, hotkey_enable_store);
1668
1669/* sysfs hotkey mask --------------------------------------------------- */
1670static ssize_t hotkey_mask_show(struct device *dev,
1671 struct device_attribute *attr,
1672 char *buf)
1673{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001674 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001675
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001676 if (mutex_lock_interruptible(&hotkey_mutex))
1677 return -ERESTARTSYS;
1678 res = hotkey_mask_get();
1679 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001680
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001681 return (res)?
1682 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001683}
1684
1685static ssize_t hotkey_mask_store(struct device *dev,
1686 struct device_attribute *attr,
1687 const char *buf, size_t count)
1688{
1689 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001690 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001691
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001692 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001693 return -EINVAL;
1694
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001695 if (mutex_lock_interruptible(&hotkey_mutex))
1696 return -ERESTARTSYS;
1697
1698 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001699
1700#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1701 hotkey_poll_setup(1);
1702#endif
1703
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001704 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001705
1706 return (res) ? res : count;
1707}
1708
1709static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001710 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001711 hotkey_mask_show, hotkey_mask_store);
1712
1713/* sysfs hotkey bios_enabled ------------------------------------------- */
1714static ssize_t hotkey_bios_enabled_show(struct device *dev,
1715 struct device_attribute *attr,
1716 char *buf)
1717{
1718 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1719}
1720
1721static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001722 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001723
1724/* sysfs hotkey bios_mask ---------------------------------------------- */
1725static ssize_t hotkey_bios_mask_show(struct device *dev,
1726 struct device_attribute *attr,
1727 char *buf)
1728{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001729 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001730}
1731
1732static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001733 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001734
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001735/* sysfs hotkey all_mask ----------------------------------------------- */
1736static ssize_t hotkey_all_mask_show(struct device *dev,
1737 struct device_attribute *attr,
1738 char *buf)
1739{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001740 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1741 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001742}
1743
1744static struct device_attribute dev_attr_hotkey_all_mask =
1745 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1746
1747/* sysfs hotkey recommended_mask --------------------------------------- */
1748static ssize_t hotkey_recommended_mask_show(struct device *dev,
1749 struct device_attribute *attr,
1750 char *buf)
1751{
1752 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001753 (hotkey_all_mask | hotkey_source_mask)
1754 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001755}
1756
1757static struct device_attribute dev_attr_hotkey_recommended_mask =
1758 __ATTR(hotkey_recommended_mask, S_IRUGO,
1759 hotkey_recommended_mask_show, NULL);
1760
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001761#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1762
1763/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1764static ssize_t hotkey_source_mask_show(struct device *dev,
1765 struct device_attribute *attr,
1766 char *buf)
1767{
1768 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1769}
1770
1771static ssize_t hotkey_source_mask_store(struct device *dev,
1772 struct device_attribute *attr,
1773 const char *buf, size_t count)
1774{
1775 unsigned long t;
1776
1777 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1778 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1779 return -EINVAL;
1780
1781 if (mutex_lock_interruptible(&hotkey_mutex))
1782 return -ERESTARTSYS;
1783
1784 HOTKEY_CONFIG_CRITICAL_START
1785 hotkey_source_mask = t;
1786 HOTKEY_CONFIG_CRITICAL_END
1787
1788 hotkey_poll_setup(1);
1789
1790 mutex_unlock(&hotkey_mutex);
1791
1792 return count;
1793}
1794
1795static struct device_attribute dev_attr_hotkey_source_mask =
1796 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1797 hotkey_source_mask_show, hotkey_source_mask_store);
1798
1799/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1800static ssize_t hotkey_poll_freq_show(struct device *dev,
1801 struct device_attribute *attr,
1802 char *buf)
1803{
1804 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1805}
1806
1807static ssize_t hotkey_poll_freq_store(struct device *dev,
1808 struct device_attribute *attr,
1809 const char *buf, size_t count)
1810{
1811 unsigned long t;
1812
1813 if (parse_strtoul(buf, 25, &t))
1814 return -EINVAL;
1815
1816 if (mutex_lock_interruptible(&hotkey_mutex))
1817 return -ERESTARTSYS;
1818
1819 hotkey_poll_freq = t;
1820
1821 hotkey_poll_setup(1);
1822 mutex_unlock(&hotkey_mutex);
1823
1824 return count;
1825}
1826
1827static struct device_attribute dev_attr_hotkey_poll_freq =
1828 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1829 hotkey_poll_freq_show, hotkey_poll_freq_store);
1830
1831#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1832
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001833/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001834static ssize_t hotkey_radio_sw_show(struct device *dev,
1835 struct device_attribute *attr,
1836 char *buf)
1837{
1838 int res, s;
1839 res = hotkey_get_wlsw(&s);
1840 if (res < 0)
1841 return res;
1842
1843 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1844}
1845
1846static struct device_attribute dev_attr_hotkey_radio_sw =
1847 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1848
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001849static void hotkey_radio_sw_notify_change(void)
1850{
1851 if (tp_features.hotkey_wlsw)
1852 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1853 "hotkey_radio_sw");
1854}
1855
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001856/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1857static ssize_t hotkey_tablet_mode_show(struct device *dev,
1858 struct device_attribute *attr,
1859 char *buf)
1860{
1861 int res, s;
1862 res = hotkey_get_tablet_mode(&s);
1863 if (res < 0)
1864 return res;
1865
1866 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1867}
1868
1869static struct device_attribute dev_attr_hotkey_tablet_mode =
1870 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1871
1872static void hotkey_tablet_mode_notify_change(void)
1873{
1874 if (tp_features.hotkey_tablet)
1875 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1876 "hotkey_tablet_mode");
1877}
1878
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001879/* sysfs hotkey report_mode -------------------------------------------- */
1880static ssize_t hotkey_report_mode_show(struct device *dev,
1881 struct device_attribute *attr,
1882 char *buf)
1883{
1884 return snprintf(buf, PAGE_SIZE, "%d\n",
1885 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1886}
1887
1888static struct device_attribute dev_attr_hotkey_report_mode =
1889 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1890
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001891/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001892static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1893 struct device_attribute *attr,
1894 char *buf)
1895{
1896 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1897}
1898
1899static struct device_attribute dev_attr_hotkey_wakeup_reason =
1900 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1901
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001902static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001903{
1904 if (tp_features.hotkey_mask)
1905 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1906 "wakeup_reason");
1907}
1908
1909/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001910static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1911 struct device_attribute *attr,
1912 char *buf)
1913{
1914 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1915}
1916
1917static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1918 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1919 hotkey_wakeup_hotunplug_complete_show, NULL);
1920
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001921static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001922{
1923 if (tp_features.hotkey_mask)
1924 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1925 "wakeup_hotunplug_complete");
1926}
1927
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001928/* --------------------------------------------------------------------- */
1929
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001930static struct attribute *hotkey_attributes[] __initdata = {
1931 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001932 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001933 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001934#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1935 &dev_attr_hotkey_mask.attr,
1936 &dev_attr_hotkey_all_mask.attr,
1937 &dev_attr_hotkey_recommended_mask.attr,
1938 &dev_attr_hotkey_source_mask.attr,
1939 &dev_attr_hotkey_poll_freq.attr,
1940#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001941};
1942
1943static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001944 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001945#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1946 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001947 &dev_attr_hotkey_all_mask.attr,
1948 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001949#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001950 &dev_attr_hotkey_wakeup_reason.attr,
1951 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001952};
1953
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001954static void bluetooth_update_rfk(void);
1955static void wan_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03001956static void tpacpi_send_radiosw_update(void)
1957{
1958 int wlsw;
1959
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001960 /* Sync these BEFORE sending any rfkill events */
1961 if (tp_features.bluetooth)
1962 bluetooth_update_rfk();
1963 if (tp_features.wan)
1964 wan_update_rfk();
1965
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03001966 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1967 mutex_lock(&tpacpi_inputdev_send_mutex);
1968
1969 input_report_switch(tpacpi_inputdev,
1970 SW_RFKILL_ALL, !!wlsw);
1971 input_sync(tpacpi_inputdev);
1972
1973 mutex_unlock(&tpacpi_inputdev_send_mutex);
1974 }
1975 hotkey_radio_sw_notify_change();
1976}
1977
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03001978static void hotkey_exit(void)
1979{
1980#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1981 hotkey_poll_stop_sync();
1982#endif
1983
1984 if (hotkey_dev_attributes)
1985 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1986
1987 kfree(hotkey_keycode_map);
1988
1989 if (tp_features.hotkey) {
1990 dbg_printk(TPACPI_DBG_EXIT,
1991 "restoring original hot key mask\n");
1992 /* no short-circuit boolean operator below! */
1993 if ((hotkey_mask_set(hotkey_orig_mask) |
1994 hotkey_status_set(hotkey_orig_status)) != 0)
1995 printk(TPACPI_ERR
1996 "failed to restore hot key mask "
1997 "to BIOS defaults\n");
1998 }
1999}
2000
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002001static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002002{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002003 /* Requirements for changing the default keymaps:
2004 *
2005 * 1. Many of the keys are mapped to KEY_RESERVED for very
2006 * good reasons. Do not change them unless you have deep
2007 * knowledge on the IBM and Lenovo ThinkPad firmware for
2008 * the various ThinkPad models. The driver behaves
2009 * differently for KEY_RESERVED: such keys have their
2010 * hot key mask *unset* in mask_recommended, and also
2011 * in the initial hot key mask programmed into the
2012 * firmware at driver load time, which means the firm-
2013 * ware may react very differently if you change them to
2014 * something else;
2015 *
2016 * 2. You must be subscribed to the linux-thinkpad and
2017 * ibm-acpi-devel mailing lists, and you should read the
2018 * list archives since 2007 if you want to change the
2019 * keymaps. This requirement exists so that you will
2020 * know the past history of problems with the thinkpad-
2021 * acpi driver keymaps, and also that you will be
2022 * listening to any bug reports;
2023 *
2024 * 3. Do not send thinkpad-acpi specific patches directly to
2025 * for merging, *ever*. Send them to the linux-acpi
2026 * mailinglist for comments. Merging is to be done only
2027 * through acpi-test and the ACPI maintainer.
2028 *
2029 * If the above is too much to ask, don't change the keymap.
2030 * Ask the thinkpad-acpi maintainer to do it, instead.
2031 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002032 static u16 ibm_keycode_map[] __initdata = {
2033 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2034 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2035 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2036 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002037
2038 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002039 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2040 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2041 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002042
2043 /* brightness: firmware always reacts to them, unless
2044 * X.org did some tricks in the radeon BIOS scratch
2045 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002046 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002047 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002048
2049 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002050 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002051
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002052 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2053 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002054
2055 /* Volume: firmware always react to it and reprograms
2056 * the built-in *extra* mixer. Never map it to control
2057 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002058 KEY_RESERVED, /* 0x14: VOLUME UP */
2059 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2060 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002061
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002062 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002063
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002064 /* (assignments unknown, please report if found) */
2065 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2066 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2067 };
2068 static u16 lenovo_keycode_map[] __initdata = {
2069 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2070 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2071 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2072 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002073
2074 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002075 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2076 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2077 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002078
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002079 /* These either have to go through ACPI video, or
2080 * act like in the IBM ThinkPads, so don't ever
2081 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002082 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002083 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002084
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002085 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002086
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002087 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2088 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002089
2090 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2091 * react to it and reprograms the built-in *extra* mixer.
2092 * Never map it to control another mixer by default.
2093 *
2094 * T60?, T61, R60?, R61: firmware and EC tries to send
2095 * these over the regular keyboard, so these are no-ops,
2096 * but there are still weird bugs re. MUTE, so do not
2097 * change unless you get test reports from all Lenovo
2098 * models. May cause the BIOS to interfere with the
2099 * HDA mixer.
2100 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002101 KEY_RESERVED, /* 0x14: VOLUME UP */
2102 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2103 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002104
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002105 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002106
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002107 /* (assignments unknown, please report if found) */
2108 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2109 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2110 };
2111
2112#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2113#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2114#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2115
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002116 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002117 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002118 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002119
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002120 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2121
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002122 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002123 BUG_ON(tpacpi_inputdev->open != NULL ||
2124 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002125
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002126 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002127 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002128
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002129#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2130 mutex_init(&hotkey_thread_mutex);
2131 mutex_init(&hotkey_thread_data_mutex);
2132#endif
2133
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002134 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002135 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002136
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002137 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002138 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002139
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002140 if (!tp_features.hotkey)
2141 return 1;
2142
2143 hotkey_dev_attributes = create_attr_set(13, NULL);
2144 if (!hotkey_dev_attributes)
2145 return -ENOMEM;
2146 res = add_many_to_attr_set(hotkey_dev_attributes,
2147 hotkey_attributes,
2148 ARRAY_SIZE(hotkey_attributes));
2149 if (res)
2150 goto err_exit;
2151
2152 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2153 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2154 for HKEY interface version 0x100 */
2155 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2156 if ((hkeyv >> 8) != 1) {
2157 printk(TPACPI_ERR "unknown version of the "
2158 "HKEY interface: 0x%x\n", hkeyv);
2159 printk(TPACPI_ERR "please report this to %s\n",
2160 TPACPI_MAIL);
2161 } else {
2162 /*
2163 * MHKV 0x100 in A31, R40, R40e,
2164 * T4x, X31, and later
2165 */
2166 tp_features.hotkey_mask = 1;
2167 }
2168 }
2169
2170 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2171 str_supported(tp_features.hotkey_mask));
2172
2173 if (tp_features.hotkey_mask) {
2174 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2175 "MHKA", "qd")) {
2176 printk(TPACPI_ERR
2177 "missing MHKA handler, "
2178 "please report this to %s\n",
2179 TPACPI_MAIL);
2180 /* FN+F12, FN+F4, FN+F3 */
2181 hotkey_all_mask = 0x080cU;
2182 }
2183 }
2184
2185 /* hotkey_source_mask *must* be zero for
2186 * the first hotkey_mask_get */
2187 res = hotkey_status_get(&hotkey_orig_status);
2188 if (res)
2189 goto err_exit;
2190
2191 if (tp_features.hotkey_mask) {
2192 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002193 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002194 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002195
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002196 hotkey_orig_mask = hotkey_mask;
2197 res = add_many_to_attr_set(
2198 hotkey_dev_attributes,
2199 hotkey_mask_attributes,
2200 ARRAY_SIZE(hotkey_mask_attributes));
2201 if (res)
2202 goto err_exit;
2203 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002204
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002205#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002206 if (tp_features.hotkey_mask) {
2207 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2208 & ~hotkey_all_mask;
2209 } else {
2210 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2211 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002212
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002213 vdbg_printk(TPACPI_DBG_INIT,
2214 "hotkey source mask 0x%08x, polling freq %d\n",
2215 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002216#endif
2217
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002218 /* Not all thinkpads have a hardware radio switch */
2219 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2220 tp_features.hotkey_wlsw = 1;
2221 printk(TPACPI_INFO
2222 "radio switch found; radios are %s\n",
2223 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002224 }
2225 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002226 res = add_to_attr_set(hotkey_dev_attributes,
2227 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002228
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002229 /* For X41t, X60t, X61t Tablets... */
2230 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2231 tp_features.hotkey_tablet = 1;
2232 printk(TPACPI_INFO
2233 "possible tablet mode switch found; "
2234 "ThinkPad in %s mode\n",
2235 (status & TP_HOTKEY_TABLET_MASK)?
2236 "tablet" : "laptop");
2237 res = add_to_attr_set(hotkey_dev_attributes,
2238 &dev_attr_hotkey_tablet_mode.attr);
2239 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002240
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002241 if (!res)
2242 res = register_attr_set_with_sysfs(
2243 hotkey_dev_attributes,
2244 &tpacpi_pdev->dev.kobj);
2245 if (res)
2246 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002247
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002248 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002249
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002250 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2251 GFP_KERNEL);
2252 if (!hotkey_keycode_map) {
2253 printk(TPACPI_ERR
2254 "failed to allocate memory for key map\n");
2255 res = -ENOMEM;
2256 goto err_exit;
2257 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002258
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002259 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2260 dbg_printk(TPACPI_DBG_INIT,
2261 "using Lenovo default hot key map\n");
2262 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2263 TPACPI_HOTKEY_MAP_SIZE);
2264 } else {
2265 dbg_printk(TPACPI_DBG_INIT,
2266 "using IBM default hot key map\n");
2267 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2268 TPACPI_HOTKEY_MAP_SIZE);
2269 }
2270
2271 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2272 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2273 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2274 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2275 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2276 tpacpi_inputdev->keycode = hotkey_keycode_map;
2277 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2278 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2279 set_bit(hotkey_keycode_map[i],
2280 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002281 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002282 if (i < sizeof(hotkey_reserved_mask)*8)
2283 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002284 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002285 }
2286
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002287 if (tp_features.hotkey_wlsw) {
2288 set_bit(EV_SW, tpacpi_inputdev->evbit);
2289 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2290 }
2291 if (tp_features.hotkey_tablet) {
2292 set_bit(EV_SW, tpacpi_inputdev->evbit);
2293 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002294 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002295
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002296 /* Do not issue duplicate brightness change events to
2297 * userspace */
2298 if (!tp_features.bright_acpimode)
2299 /* update bright_acpimode... */
2300 tpacpi_check_std_acpi_brightness_support();
2301
2302 if (tp_features.bright_acpimode) {
2303 printk(TPACPI_INFO
2304 "This ThinkPad has standard ACPI backlight "
2305 "brightness control, supported by the ACPI "
2306 "video driver\n");
2307 printk(TPACPI_NOTICE
2308 "Disabling thinkpad-acpi brightness events "
2309 "by default...\n");
2310
2311 /* The hotkey_reserved_mask change below is not
2312 * necessary while the keys are at KEY_RESERVED in the
2313 * default map, but better safe than sorry, leave it
2314 * here as a marker of what we have to do, especially
2315 * when we finally become able to set this at runtime
2316 * on response to X.org requests */
2317 hotkey_reserved_mask |=
2318 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2319 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002320 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002321
2322 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2323 res = hotkey_status_set(1);
2324 if (res) {
2325 hotkey_exit();
2326 return res;
2327 }
2328 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2329 & ~hotkey_reserved_mask)
2330 | hotkey_orig_mask);
2331 if (res < 0 && res != -ENXIO) {
2332 hotkey_exit();
2333 return res;
2334 }
2335
2336 dbg_printk(TPACPI_DBG_INIT,
2337 "legacy hot key reporting over procfs %s\n",
2338 (hotkey_report_mode < 2) ?
2339 "enabled" : "disabled");
2340
2341 tpacpi_inputdev->open = &hotkey_inputdev_open;
2342 tpacpi_inputdev->close = &hotkey_inputdev_close;
2343
2344 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002345 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002346 tpacpi_input_send_tabletsw();
2347
2348 return 0;
2349
2350err_exit:
2351 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2352 hotkey_dev_attributes = NULL;
2353
2354 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002355}
2356
2357static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2358{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002359 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002360 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002361 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002362 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002363 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002364
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002365 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002366 printk(TPACPI_ERR
2367 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002368 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002369 acpi_bus_generate_netlink_event(
2370 ibm->acpi->device->pnp.device_class,
2371 ibm->acpi->device->dev.bus_id,
2372 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002373 return;
2374 }
2375
2376 while (1) {
2377 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002378 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002379 return;
2380 }
2381
2382 if (hkey == 0) {
2383 /* queue empty */
2384 return;
2385 }
2386
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002387 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002388 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002389 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002390
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002391 switch (hkey >> 12) {
2392 case 1:
2393 /* 0x1000-0x1FFF: key presses */
2394 scancode = hkey & 0xfff;
2395 if (scancode > 0 && scancode < 0x21) {
2396 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002397 if (!(hotkey_source_mask & (1 << scancode))) {
2398 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002399 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002400 } else {
2401 ignore_acpi_ev = 1;
2402 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002403 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002404 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002405 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002406 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002407 case 2:
2408 /* Wakeup reason */
2409 switch (hkey) {
2410 case 0x2304: /* suspend, undock */
2411 case 0x2404: /* hibernation, undock */
2412 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2413 ignore_acpi_ev = 1;
2414 break;
2415 case 0x2305: /* suspend, bay eject */
2416 case 0x2405: /* hibernation, bay eject */
2417 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2418 ignore_acpi_ev = 1;
2419 break;
2420 default:
2421 unk_ev = 1;
2422 }
2423 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2424 printk(TPACPI_INFO
2425 "woke up due to a hot-unplug "
2426 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002427 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002428 }
2429 break;
2430 case 3:
2431 /* bay-related wakeups */
2432 if (hkey == 0x3003) {
2433 hotkey_autosleep_ack = 1;
2434 printk(TPACPI_INFO
2435 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002436 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002437 } else {
2438 unk_ev = 1;
2439 }
2440 break;
2441 case 4:
2442 /* dock-related wakeups */
2443 if (hkey == 0x4003) {
2444 hotkey_autosleep_ack = 1;
2445 printk(TPACPI_INFO
2446 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002447 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002448 } else {
2449 unk_ev = 1;
2450 }
2451 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002452 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002453 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002454 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002455 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002456 case 0x500b: /* X61t: tablet pen inserted into bay */
2457 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002458 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002459 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2460 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2461 tpacpi_input_send_tabletsw();
2462 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002463 send_acpi_ev = 0;
2464 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002465 case 0x5001:
2466 case 0x5002:
2467 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002468 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002469 break;
2470 default:
2471 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002472 }
2473 break;
2474 case 7:
2475 /* 0x7000-0x7FFF: misc */
2476 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002477 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002478 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002479 break;
2480 }
2481 /* fallthrough to default */
2482 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002483 unk_ev = 1;
2484 }
2485 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002486 printk(TPACPI_NOTICE
2487 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002488 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002489
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002490 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002491 if (!ignore_acpi_ev &&
2492 (send_acpi_ev || hotkey_report_mode < 2)) {
2493 acpi_bus_generate_proc_event(ibm->acpi->device,
2494 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002495 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002496
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002497 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002498 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002499 acpi_bus_generate_netlink_event(
2500 ibm->acpi->device->pnp.device_class,
2501 ibm->acpi->device->dev.bus_id,
2502 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002503 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002504 }
2505}
2506
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002507static void hotkey_suspend(pm_message_t state)
2508{
2509 /* Do these on suspend, we get the events on early resume! */
2510 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2511 hotkey_autosleep_ack = 0;
2512}
2513
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002514static void hotkey_resume(void)
2515{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002516 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002517 printk(TPACPI_ERR
2518 "error while trying to read hot key mask "
2519 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002520 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002521 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002522 hotkey_wakeup_reason_notify_change();
2523 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002524 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002525}
2526
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002527/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002528static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002530 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 int len = 0;
2532
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002533 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002534 len += sprintf(p + len, "status:\t\tnot supported\n");
2535 return len;
2536 }
2537
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002538 if (mutex_lock_interruptible(&hotkey_mutex))
2539 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002540 res = hotkey_status_get(&status);
2541 if (!res)
2542 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002543 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002544 if (res)
2545 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002548 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002549 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 len += sprintf(p + len,
2551 "commands:\tenable, disable, reset, <mask>\n");
2552 } else {
2553 len += sprintf(p + len, "mask:\t\tnot supported\n");
2554 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2555 }
2556
2557 return len;
2558}
2559
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002560static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002562 int res, status;
2563 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002566 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return -ENODEV;
2568
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002569 if (mutex_lock_interruptible(&hotkey_mutex))
2570 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002571
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002572 status = -1;
2573 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002574
2575 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 while ((cmd = next_cmd(&buf))) {
2577 if (strlencmp(cmd, "enable") == 0) {
2578 status = 1;
2579 } else if (strlencmp(cmd, "disable") == 0) {
2580 status = 0;
2581 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002582 status = hotkey_orig_status;
2583 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2585 /* mask set */
2586 } else if (sscanf(cmd, "%x", &mask) == 1) {
2587 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002588 } else {
2589 res = -EINVAL;
2590 goto errexit;
2591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002593 if (status != -1)
2594 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002596 if (!res && mask != hotkey_mask)
2597 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002599errexit:
2600 mutex_unlock(&hotkey_mutex);
2601 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002604static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002605 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002606 {"", 0},
2607};
2608
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002609static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002610 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002611 .notify = hotkey_notify,
2612 .handle = &hkey_handle,
2613 .type = ACPI_DEVICE_NOTIFY,
2614};
2615
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002616static struct ibm_struct hotkey_driver_data = {
2617 .name = "hotkey",
2618 .read = hotkey_read,
2619 .write = hotkey_write,
2620 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002621 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002622 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002623 .acpi = &ibm_hotkey_acpidriver,
2624};
2625
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002626/*************************************************************************
2627 * Bluetooth subdriver
2628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002630enum {
2631 /* ACPI GBDC/SBDC bits */
2632 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2633 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2634 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2635};
2636
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002637static struct rfkill *tpacpi_bluetooth_rfkill;
2638
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002639static int bluetooth_get_radiosw(void)
2640{
2641 int status;
2642
2643 if (!tp_features.bluetooth)
2644 return -ENODEV;
2645
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002646 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2647 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002648 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002649
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002650 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2651 return -EIO;
2652
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002653 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2654 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002655}
2656
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002657static void bluetooth_update_rfk(void)
2658{
2659 int status;
2660
2661 if (!tpacpi_bluetooth_rfkill)
2662 return;
2663
2664 status = bluetooth_get_radiosw();
2665 if (status < 0)
2666 return;
2667 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2668}
2669
2670static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002671{
2672 int status;
2673
2674 if (!tp_features.bluetooth)
2675 return -ENODEV;
2676
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002677 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2678 * reason to risk weird behaviour. */
2679 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2680 && radio_on)
2681 return -EPERM;
2682
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002683 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2684 return -EIO;
2685 if (radio_on)
2686 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2687 else
2688 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2689 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2690 return -EIO;
2691
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002692 if (update_rfk)
2693 bluetooth_update_rfk();
2694
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002695 return 0;
2696}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002697
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002698/* sysfs bluetooth enable ---------------------------------------------- */
2699static ssize_t bluetooth_enable_show(struct device *dev,
2700 struct device_attribute *attr,
2701 char *buf)
2702{
2703 int status;
2704
2705 status = bluetooth_get_radiosw();
2706 if (status < 0)
2707 return status;
2708
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002709 return snprintf(buf, PAGE_SIZE, "%d\n",
2710 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002711}
2712
2713static ssize_t bluetooth_enable_store(struct device *dev,
2714 struct device_attribute *attr,
2715 const char *buf, size_t count)
2716{
2717 unsigned long t;
2718 int res;
2719
2720 if (parse_strtoul(buf, 1, &t))
2721 return -EINVAL;
2722
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002723 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002724
2725 return (res) ? res : count;
2726}
2727
2728static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002729 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002730 bluetooth_enable_show, bluetooth_enable_store);
2731
2732/* --------------------------------------------------------------------- */
2733
2734static struct attribute *bluetooth_attributes[] = {
2735 &dev_attr_bluetooth_enable.attr,
2736 NULL
2737};
2738
2739static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002740 .attrs = bluetooth_attributes,
2741};
2742
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002743static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2744{
2745 int bts = bluetooth_get_radiosw();
2746
2747 if (bts < 0)
2748 return bts;
2749
2750 *state = bts;
2751 return 0;
2752}
2753
2754static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2755{
2756 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2757}
2758
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002759static void bluetooth_exit(void)
2760{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002761 if (tpacpi_bluetooth_rfkill)
2762 rfkill_unregister(tpacpi_bluetooth_rfkill);
2763
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002764 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2765 &bluetooth_attr_group);
2766}
2767
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002768static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002770 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002771 int status = 0;
2772
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002773 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2774
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002775 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002776
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002777 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2778 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002779 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002780 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002782 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2783 str_supported(tp_features.bluetooth),
2784 status);
2785
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002786 if (tp_features.bluetooth &&
2787 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2788 /* no bluetooth hardware present in system */
2789 tp_features.bluetooth = 0;
2790 dbg_printk(TPACPI_DBG_INIT,
2791 "bluetooth hardware not installed\n");
2792 }
2793
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002794 if (!tp_features.bluetooth)
2795 return 1;
2796
2797 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002798 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002799 if (res)
2800 return res;
2801
2802 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2803 &tpacpi_bluetooth_rfkill,
2804 RFKILL_TYPE_BLUETOOTH,
2805 "tpacpi_bluetooth_sw",
2806 tpacpi_bluetooth_rfk_set,
2807 tpacpi_bluetooth_rfk_get);
2808 if (res) {
2809 bluetooth_exit();
2810 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002811 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002812
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002816/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002817static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002820 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002822 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002825 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002826 (status == RFKILL_STATE_UNBLOCKED) ?
2827 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 len += sprintf(p + len, "commands:\tenable, disable\n");
2829 }
2830
2831 return len;
2832}
2833
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002834static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002838 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002839 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 while ((cmd = next_cmd(&buf))) {
2842 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002843 bluetooth_set_radiosw(1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002845 bluetooth_set_radiosw(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 } else
2847 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return 0;
2851}
2852
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002853static struct ibm_struct bluetooth_driver_data = {
2854 .name = "bluetooth",
2855 .read = bluetooth_read,
2856 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002857 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002858};
2859
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002860/*************************************************************************
2861 * Wan subdriver
2862 */
2863
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002864enum {
2865 /* ACPI GWAN/SWAN bits */
2866 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2867 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2868 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2869};
2870
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002871static struct rfkill *tpacpi_wan_rfkill;
2872
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002873static int wan_get_radiosw(void)
2874{
2875 int status;
2876
2877 if (!tp_features.wan)
2878 return -ENODEV;
2879
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002880 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2881 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002882 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002883
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002884 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2885 return -EIO;
2886
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002887 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
2888 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002889}
2890
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002891static void wan_update_rfk(void)
2892{
2893 int status;
2894
2895 if (!tpacpi_wan_rfkill)
2896 return;
2897
2898 status = wan_get_radiosw();
2899 if (status < 0)
2900 return;
2901 rfkill_force_state(tpacpi_wan_rfkill, status);
2902}
2903
2904static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002905{
2906 int status;
2907
2908 if (!tp_features.wan)
2909 return -ENODEV;
2910
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002911 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2912 * reason to risk weird behaviour. */
2913 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2914 && radio_on)
2915 return -EPERM;
2916
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002917 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2918 return -EIO;
2919 if (radio_on)
2920 status |= TP_ACPI_WANCARD_RADIOSSW;
2921 else
2922 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2923 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2924 return -EIO;
2925
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002926 if (update_rfk)
2927 wan_update_rfk();
2928
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002929 return 0;
2930}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002931
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002932/* sysfs wan enable ---------------------------------------------------- */
2933static ssize_t wan_enable_show(struct device *dev,
2934 struct device_attribute *attr,
2935 char *buf)
2936{
2937 int status;
2938
2939 status = wan_get_radiosw();
2940 if (status < 0)
2941 return status;
2942
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002943 return snprintf(buf, PAGE_SIZE, "%d\n",
2944 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002945}
2946
2947static ssize_t wan_enable_store(struct device *dev,
2948 struct device_attribute *attr,
2949 const char *buf, size_t count)
2950{
2951 unsigned long t;
2952 int res;
2953
2954 if (parse_strtoul(buf, 1, &t))
2955 return -EINVAL;
2956
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002957 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002958
2959 return (res) ? res : count;
2960}
2961
2962static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002963 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002964 wan_enable_show, wan_enable_store);
2965
2966/* --------------------------------------------------------------------- */
2967
2968static struct attribute *wan_attributes[] = {
2969 &dev_attr_wan_enable.attr,
2970 NULL
2971};
2972
2973static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002974 .attrs = wan_attributes,
2975};
2976
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002977static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2978{
2979 int wans = wan_get_radiosw();
2980
2981 if (wans < 0)
2982 return wans;
2983
2984 *state = wans;
2985 return 0;
2986}
2987
2988static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
2989{
2990 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2991}
2992
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002993static void wan_exit(void)
2994{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002995 if (tpacpi_wan_rfkill)
2996 rfkill_unregister(tpacpi_wan_rfkill);
2997
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002998 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2999 &wan_attr_group);
3000}
3001
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003002static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003003{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003004 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003005 int status = 0;
3006
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003007 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3008
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003009 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003010
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003011 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003012 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003013
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003014 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3015 str_supported(tp_features.wan),
3016 status);
3017
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003018 if (tp_features.wan &&
3019 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3020 /* no wan hardware present in system */
3021 tp_features.wan = 0;
3022 dbg_printk(TPACPI_DBG_INIT,
3023 "wan hardware not installed\n");
3024 }
3025
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003026 if (!tp_features.wan)
3027 return 1;
3028
3029 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003030 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003031 if (res)
3032 return res;
3033
3034 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3035 &tpacpi_wan_rfkill,
3036 RFKILL_TYPE_WWAN,
3037 "tpacpi_wwan_sw",
3038 tpacpi_wan_rfk_set,
3039 tpacpi_wan_rfk_get);
3040 if (res) {
3041 wan_exit();
3042 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003043 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003044
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003045 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003046}
3047
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003048/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003049static int wan_read(char *p)
3050{
3051 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003052 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003053
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003054 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003055 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003056 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003057 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003058 (status == RFKILL_STATE_UNBLOCKED) ?
3059 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003060 len += sprintf(p + len, "commands:\tenable, disable\n");
3061 }
3062
3063 return len;
3064}
3065
3066static int wan_write(char *buf)
3067{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003068 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003069
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003070 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003071 return -ENODEV;
3072
3073 while ((cmd = next_cmd(&buf))) {
3074 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003075 wan_set_radiosw(1, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003076 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003077 wan_set_radiosw(0, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003078 } else
3079 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003080 }
3081
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003082 return 0;
3083}
3084
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003085static struct ibm_struct wan_driver_data = {
3086 .name = "wan",
3087 .read = wan_read,
3088 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003089 .exit = wan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003090};
3091
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003092/*************************************************************************
3093 * Video subdriver
3094 */
3095
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003096#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3097
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003098enum video_access_mode {
3099 TPACPI_VIDEO_NONE = 0,
3100 TPACPI_VIDEO_570, /* 570 */
3101 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3102 TPACPI_VIDEO_NEW, /* all others */
3103};
3104
3105enum { /* video status flags, based on VIDEO_570 */
3106 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3107 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3108 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3109};
3110
3111enum { /* TPACPI_VIDEO_570 constants */
3112 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3113 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3114 * video_status_flags */
3115 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3116 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3117};
3118
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003119static enum video_access_mode video_supported;
3120static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003121
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003122static int video_autosw_get(void);
3123static int video_autosw_set(int enable);
3124
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003125TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003126
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003127static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003129 int ivga;
3130
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003131 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3132
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003133 TPACPI_ACPIHANDLE_INIT(vid);
3134 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003135
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003136 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3137 /* G41, assume IVGA doesn't change */
3138 vid_handle = vid2_handle;
3139
3140 if (!vid_handle)
3141 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003142 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003143 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3144 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003145 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003146 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3147 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003148 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003149 else
3150 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003151 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003153 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3154 str_supported(video_supported != TPACPI_VIDEO_NONE),
3155 video_supported);
3156
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003157 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003160static void video_exit(void)
3161{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003162 dbg_printk(TPACPI_DBG_EXIT,
3163 "restoring original video autoswitch mode\n");
3164 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003165 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003166 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003167}
3168
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003169static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
3171 int status = 0;
3172 int i;
3173
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003174 switch (video_supported) {
3175 case TPACPI_VIDEO_570:
3176 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3177 TP_ACPI_VIDEO_570_PHSCMD))
3178 return -EIO;
3179 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3180 break;
3181 case TPACPI_VIDEO_770:
3182 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3183 return -EIO;
3184 if (i)
3185 status |= TP_ACPI_VIDEO_S_LCD;
3186 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3187 return -EIO;
3188 if (i)
3189 status |= TP_ACPI_VIDEO_S_CRT;
3190 break;
3191 case TPACPI_VIDEO_NEW:
3192 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3193 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3194 return -EIO;
3195 if (i)
3196 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003198 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3199 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3200 return -EIO;
3201 if (i)
3202 status |= TP_ACPI_VIDEO_S_LCD;
3203 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3204 return -EIO;
3205 if (i)
3206 status |= TP_ACPI_VIDEO_S_DVI;
3207 break;
3208 default:
3209 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212 return status;
3213}
3214
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003215static int video_outputsw_set(int status)
3216{
3217 int autosw;
3218 int res = 0;
3219
3220 switch (video_supported) {
3221 case TPACPI_VIDEO_570:
3222 res = acpi_evalf(NULL, NULL,
3223 "\\_SB.PHS2", "vdd",
3224 TP_ACPI_VIDEO_570_PHS2CMD,
3225 status | TP_ACPI_VIDEO_570_PHS2SET);
3226 break;
3227 case TPACPI_VIDEO_770:
3228 autosw = video_autosw_get();
3229 if (autosw < 0)
3230 return autosw;
3231
3232 res = video_autosw_set(1);
3233 if (res)
3234 return res;
3235 res = acpi_evalf(vid_handle, NULL,
3236 "ASWT", "vdd", status * 0x100, 0);
3237 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003238 printk(TPACPI_ERR
3239 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003240 return -EIO;
3241 }
3242 break;
3243 case TPACPI_VIDEO_NEW:
3244 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003245 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003246 break;
3247 default:
3248 return -ENOSYS;
3249 }
3250
3251 return (res)? 0 : -EIO;
3252}
3253
3254static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003256 int autosw = 0;
3257
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003258 switch (video_supported) {
3259 case TPACPI_VIDEO_570:
3260 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3261 return -EIO;
3262 break;
3263 case TPACPI_VIDEO_770:
3264 case TPACPI_VIDEO_NEW:
3265 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3266 return -EIO;
3267 break;
3268 default:
3269 return -ENOSYS;
3270 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003271
3272 return autosw & 1;
3273}
3274
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003275static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003276{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003277 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003278 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003279 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003280}
3281
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003282static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003283{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003284 int autosw = video_autosw_get();
3285 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003286
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003287 if (autosw < 0)
3288 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003289
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003290 switch (video_supported) {
3291 case TPACPI_VIDEO_570:
3292 res = video_autosw_set(1);
3293 if (res)
3294 return res;
3295 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3296 break;
3297 case TPACPI_VIDEO_770:
3298 case TPACPI_VIDEO_NEW:
3299 res = video_autosw_set(1);
3300 if (res)
3301 return res;
3302 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3303 break;
3304 default:
3305 return -ENOSYS;
3306 }
3307 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003308 printk(TPACPI_ERR
3309 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003310 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003311 }
3312
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003313 return (res)? 0 : -EIO;
3314}
3315
3316static int video_expand_toggle(void)
3317{
3318 switch (video_supported) {
3319 case TPACPI_VIDEO_570:
3320 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3321 0 : -EIO;
3322 case TPACPI_VIDEO_770:
3323 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3324 0 : -EIO;
3325 case TPACPI_VIDEO_NEW:
3326 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3327 0 : -EIO;
3328 default:
3329 return -ENOSYS;
3330 }
3331 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003332}
3333
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003334static int video_read(char *p)
3335{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003336 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003337 int len = 0;
3338
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003339 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003340 len += sprintf(p + len, "status:\t\tnot supported\n");
3341 return len;
3342 }
3343
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003344 status = video_outputsw_get();
3345 if (status < 0)
3346 return status;
3347
3348 autosw = video_autosw_get();
3349 if (autosw < 0)
3350 return autosw;
3351
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003352 len += sprintf(p + len, "status:\t\tsupported\n");
3353 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3354 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003355 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003356 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3357 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3358 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3359 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003360 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003361 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3362 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3363 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3364
3365 return len;
3366}
3367
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003368static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
3370 char *cmd;
3371 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003372 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003374 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003375 return -ENODEV;
3376
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003377 enable = 0;
3378 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
3380 while ((cmd = next_cmd(&buf))) {
3381 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003382 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003384 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003386 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003388 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003389 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003390 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003391 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003392 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003393 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003394 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003396 res = video_autosw_set(1);
3397 if (res)
3398 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003400 res = video_autosw_set(0);
3401 if (res)
3402 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003404 res = video_outputsw_cycle();
3405 if (res)
3406 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003408 res = video_expand_toggle();
3409 if (res)
3410 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 } else
3412 return -EINVAL;
3413 }
3414
3415 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003416 status = video_outputsw_get();
3417 if (status < 0)
3418 return status;
3419 res = video_outputsw_set((status & ~disable) | enable);
3420 if (res)
3421 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 }
3423
3424 return 0;
3425}
3426
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003427static struct ibm_struct video_driver_data = {
3428 .name = "video",
3429 .read = video_read,
3430 .write = video_write,
3431 .exit = video_exit,
3432};
3433
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003434#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3435
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003436/*************************************************************************
3437 * Light (thinklight) subdriver
3438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003440TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3441TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003442
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003443static int light_get_status(void)
3444{
3445 int status = 0;
3446
3447 if (tp_features.light_status) {
3448 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3449 return -EIO;
3450 return (!!status);
3451 }
3452
3453 return -ENXIO;
3454}
3455
3456static int light_set_status(int status)
3457{
3458 int rc;
3459
3460 if (tp_features.light) {
3461 if (cmos_handle) {
3462 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3463 (status)?
3464 TP_CMOS_THINKLIGHT_ON :
3465 TP_CMOS_THINKLIGHT_OFF);
3466 } else {
3467 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3468 (status)? 1 : 0);
3469 }
3470 return (rc)? 0 : -EIO;
3471 }
3472
3473 return -ENXIO;
3474}
3475
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003476static void light_set_status_worker(struct work_struct *work)
3477{
3478 struct tpacpi_led_classdev *data =
3479 container_of(work, struct tpacpi_led_classdev, work);
3480
3481 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3482 light_set_status((data->new_brightness != LED_OFF));
3483}
3484
3485static void light_sysfs_set(struct led_classdev *led_cdev,
3486 enum led_brightness brightness)
3487{
3488 struct tpacpi_led_classdev *data =
3489 container_of(led_cdev,
3490 struct tpacpi_led_classdev,
3491 led_classdev);
3492 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003493 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003494}
3495
3496static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3497{
3498 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3499}
3500
3501static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3502 .led_classdev = {
3503 .name = "tpacpi::thinklight",
3504 .brightness_set = &light_sysfs_set,
3505 .brightness_get = &light_sysfs_get,
3506 }
3507};
3508
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003509static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003511 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003512
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003513 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3514
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003515 TPACPI_ACPIHANDLE_INIT(ledb);
3516 TPACPI_ACPIHANDLE_INIT(lght);
3517 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003518 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003519
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003520 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003521 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003522
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003523 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003524 /* light status not supported on
3525 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003526 tp_features.light_status =
3527 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003529 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3530 str_supported(tp_features.light),
3531 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003532
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003533 if (!tp_features.light)
3534 return 1;
3535
3536 rc = led_classdev_register(&tpacpi_pdev->dev,
3537 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003538
3539 if (rc < 0) {
3540 tp_features.light = 0;
3541 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003542 } else {
3543 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003544 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003545
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003546 return rc;
3547}
3548
3549static void light_exit(void)
3550{
3551 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3552 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003553 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554}
3555
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003556static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
3558 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003559 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003561 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003562 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003563 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003564 len += sprintf(p + len, "status:\t\tunknown\n");
3565 len += sprintf(p + len, "commands:\ton, off\n");
3566 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003567 status = light_get_status();
3568 if (status < 0)
3569 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003571 len += sprintf(p + len, "commands:\ton, off\n");
3572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
3574 return len;
3575}
3576
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003577static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003580 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003581
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003582 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003583 return -ENODEV;
3584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 while ((cmd = next_cmd(&buf))) {
3586 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003587 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003589 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 } else
3591 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
3593
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003594 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003597static struct ibm_struct light_driver_data = {
3598 .name = "light",
3599 .read = light_read,
3600 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003601 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003602};
3603
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003604/*************************************************************************
3605 * Dock subdriver
3606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003608#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003609
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003610static void dock_notify(struct ibm_struct *ibm, u32 event);
3611static int dock_read(char *p);
3612static int dock_write(char *buf);
3613
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003614TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003615 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3616 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3617 "\\_SB.PCI.ISA.SLCE", /* 570 */
3618 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3619
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003620/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003621TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003622
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003623static const struct acpi_device_id ibm_pci_device_ids[] = {
3624 {PCI_ROOT_HID_STRING, 0},
3625 {"", 0},
3626};
3627
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003628static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3629 {
3630 .notify = dock_notify,
3631 .handle = &dock_handle,
3632 .type = ACPI_SYSTEM_NOTIFY,
3633 },
3634 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003635 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3636 * We just use it to get notifications of dock hotplug
3637 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003638 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003639 .notify = dock_notify,
3640 .handle = &pci_handle,
3641 .type = ACPI_SYSTEM_NOTIFY,
3642 },
3643};
3644
3645static struct ibm_struct dock_driver_data[2] = {
3646 {
3647 .name = "dock",
3648 .read = dock_read,
3649 .write = dock_write,
3650 .acpi = &ibm_dock_acpidriver[0],
3651 },
3652 {
3653 .name = "dock",
3654 .acpi = &ibm_dock_acpidriver[1],
3655 },
3656};
3657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658#define dock_docked() (_sta(dock_handle) & 1)
3659
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003660static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003661{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003662 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3663
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003664 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003665
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003666 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3667 str_supported(dock_handle != NULL));
3668
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003669 return (dock_handle)? 0 : 1;
3670}
3671
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003672static int __init dock_init2(struct ibm_init_struct *iibm)
3673{
3674 int dock2_needed;
3675
3676 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3677
3678 if (dock_driver_data[0].flags.acpi_driver_registered &&
3679 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003680 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003681 dock2_needed = (pci_handle != NULL);
3682 vdbg_printk(TPACPI_DBG_INIT,
3683 "dock PCI handler for the TP 570 is %s\n",
3684 str_supported(dock2_needed));
3685 } else {
3686 vdbg_printk(TPACPI_DBG_INIT,
3687 "dock subdriver part 2 not required\n");
3688 dock2_needed = 0;
3689 }
3690
3691 return (dock2_needed)? 0 : 1;
3692}
3693
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003694static void dock_notify(struct ibm_struct *ibm, u32 event)
3695{
3696 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003697 int pci = ibm->acpi->hid && ibm->acpi->device &&
3698 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003699 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003700
3701 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003702 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003703 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003704 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003705 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003706 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003707 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003708 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003709 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003710 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003711 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003712 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003713 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003714 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003715 }
Len Brown14e04fb2007-08-23 15:20:26 -04003716 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003717 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3718 ibm->acpi->device->dev.bus_id,
3719 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003720}
3721
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003722static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
3724 int len = 0;
3725 int docked = dock_docked();
3726
3727 if (!dock_handle)
3728 len += sprintf(p + len, "status:\t\tnot supported\n");
3729 else if (!docked)
3730 len += sprintf(p + len, "status:\t\tundocked\n");
3731 else {
3732 len += sprintf(p + len, "status:\t\tdocked\n");
3733 len += sprintf(p + len, "commands:\tdock, undock\n");
3734 }
3735
3736 return len;
3737}
3738
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003739static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
3741 char *cmd;
3742
3743 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003744 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
3746 while ((cmd = next_cmd(&buf))) {
3747 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003748 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3749 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 return -EIO;
3751 } else if (strlencmp(cmd, "dock") == 0) {
3752 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3753 return -EIO;
3754 } else
3755 return -EINVAL;
3756 }
3757
3758 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003759}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003761#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003763/*************************************************************************
3764 * Bay subdriver
3765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003767#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003768
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003769TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003770 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3771 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3772 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3773 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003774TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003775 "_EJ0", /* all others */
3776 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003777TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003778 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3779 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003780TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003781 "_EJ0", /* 770x */
3782 ); /* all others */
3783
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003784static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003786 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3787
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003788 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003789 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003790 TPACPI_ACPIHANDLE_INIT(bay_ej);
3791 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003792 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003793 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003794
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003795 tp_features.bay_status = bay_handle &&
3796 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3797 tp_features.bay_status2 = bay2_handle &&
3798 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003799
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003800 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3801 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3802 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3803 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003805 vdbg_printk(TPACPI_DBG_INIT,
3806 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003807 str_supported(tp_features.bay_status),
3808 str_supported(tp_features.bay_eject),
3809 str_supported(tp_features.bay_status2),
3810 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003811
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003812 return (tp_features.bay_status || tp_features.bay_eject ||
3813 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814}
3815
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003816static void bay_notify(struct ibm_struct *ibm, u32 event)
3817{
Len Brown14e04fb2007-08-23 15:20:26 -04003818 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003819 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3820 ibm->acpi->device->dev.bus_id,
3821 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003822}
3823
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003824#define bay_occupied(b) (_sta(b##_handle) & 1)
3825
3826static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
3828 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003829 int occupied = bay_occupied(bay);
3830 int occupied2 = bay_occupied(bay2);
3831 int eject, eject2;
3832
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003833 len += sprintf(p + len, "status:\t\t%s\n",
3834 tp_features.bay_status ?
3835 (occupied ? "occupied" : "unoccupied") :
3836 "not supported");
3837 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003838 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3839 "occupied" : "unoccupied");
3840
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003841 eject = tp_features.bay_eject && occupied;
3842 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003843
3844 if (eject && eject2)
3845 len += sprintf(p + len, "commands:\teject, eject2\n");
3846 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003848 else if (eject2)
3849 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
3851 return len;
3852}
3853
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003854static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
3856 char *cmd;
3857
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003858 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003859 return -ENODEV;
3860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003862 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003863 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3864 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003865 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003866 strlencmp(cmd, "eject2") == 0) {
3867 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 return -EIO;
3869 } else
3870 return -EINVAL;
3871 }
3872
3873 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003874}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003875
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003876static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3877 .notify = bay_notify,
3878 .handle = &bay_handle,
3879 .type = ACPI_SYSTEM_NOTIFY,
3880};
3881
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003882static struct ibm_struct bay_driver_data = {
3883 .name = "bay",
3884 .read = bay_read,
3885 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003886 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003887};
3888
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003889#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003891/*************************************************************************
3892 * CMOS subdriver
3893 */
3894
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003895/* sysfs cmos_command -------------------------------------------------- */
3896static ssize_t cmos_command_store(struct device *dev,
3897 struct device_attribute *attr,
3898 const char *buf, size_t count)
3899{
3900 unsigned long cmos_cmd;
3901 int res;
3902
3903 if (parse_strtoul(buf, 21, &cmos_cmd))
3904 return -EINVAL;
3905
3906 res = issue_thinkpad_cmos_command(cmos_cmd);
3907 return (res)? res : count;
3908}
3909
3910static struct device_attribute dev_attr_cmos_command =
3911 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3912
3913/* --------------------------------------------------------------------- */
3914
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003915static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003916{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003917 int res;
3918
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003919 vdbg_printk(TPACPI_DBG_INIT,
3920 "initializing cmos commands subdriver\n");
3921
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003922 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003923
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003924 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3925 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003926
3927 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3928 if (res)
3929 return res;
3930
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003931 return (cmos_handle)? 0 : 1;
3932}
3933
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003934static void cmos_exit(void)
3935{
3936 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3937}
3938
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003939static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
3941 int len = 0;
3942
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003943 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3944 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 if (!cmos_handle)
3946 len += sprintf(p + len, "status:\t\tnot supported\n");
3947 else {
3948 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003949 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 }
3951
3952 return len;
3953}
3954
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003955static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
3957 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003958 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003961 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3962 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 /* cmos_cmd set */
3964 } else
3965 return -EINVAL;
3966
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003967 res = issue_thinkpad_cmos_command(cmos_cmd);
3968 if (res)
3969 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 }
3971
3972 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003973}
3974
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003975static struct ibm_struct cmos_driver_data = {
3976 .name = "cmos",
3977 .read = cmos_read,
3978 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003979 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003980};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003981
3982/*************************************************************************
3983 * LED subdriver
3984 */
3985
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003986enum led_access_mode {
3987 TPACPI_LED_NONE = 0,
3988 TPACPI_LED_570, /* 570 */
3989 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3990 TPACPI_LED_NEW, /* all others */
3991};
3992
3993enum { /* For TPACPI_LED_OLD */
3994 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3995 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3996 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3997};
3998
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003999enum led_status_t {
4000 TPACPI_LED_OFF = 0,
4001 TPACPI_LED_ON,
4002 TPACPI_LED_BLINK,
4003};
4004
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004005static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004006
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004007TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004008 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4009 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004010 "LED", /* all others */
4011 ); /* R30, R31 */
4012
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004013#define TPACPI_LED_NUMLEDS 8
4014static struct tpacpi_led_classdev *tpacpi_leds;
4015static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004016static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004017 /* there's a limit of 19 chars + NULL before 2.6.26 */
4018 "tpacpi::power",
4019 "tpacpi:orange:batt",
4020 "tpacpi:green:batt",
4021 "tpacpi::dock_active",
4022 "tpacpi::bay_active",
4023 "tpacpi::dock_batt",
4024 "tpacpi::unknown_led",
4025 "tpacpi::standby",
4026};
4027
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004028static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004029{
4030 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004031 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004032
4033 switch (led_supported) {
4034 case TPACPI_LED_570:
4035 if (!acpi_evalf(ec_handle,
4036 &status, "GLED", "dd", 1 << led))
4037 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004038 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004039 TPACPI_LED_OFF :
4040 ((status == 1)?
4041 TPACPI_LED_ON :
4042 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004043 tpacpi_led_state_cache[led] = led_s;
4044 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004045 default:
4046 return -ENXIO;
4047 }
4048
4049 /* not reached */
4050}
4051
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004052static int led_set_status(const unsigned int led,
4053 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004054{
4055 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004056 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4057 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004058
4059 int rc = 0;
4060
4061 switch (led_supported) {
4062 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004063 /* 570 */
4064 if (led > 7)
4065 return -EINVAL;
4066 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4067 (1 << led), led_sled_arg1[ledstatus]))
4068 rc = -EIO;
4069 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004070 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004071 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4072 if (led > 7)
4073 return -EINVAL;
4074 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4075 if (rc >= 0)
4076 rc = ec_write(TPACPI_LED_EC_HLBL,
4077 (ledstatus == TPACPI_LED_BLINK) << led);
4078 if (rc >= 0)
4079 rc = ec_write(TPACPI_LED_EC_HLCL,
4080 (ledstatus != TPACPI_LED_OFF) << led);
4081 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004082 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004083 /* all others */
4084 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4085 led, led_led_arg1[ledstatus]))
4086 rc = -EIO;
4087 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004088 default:
4089 rc = -ENXIO;
4090 }
4091
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004092 if (!rc)
4093 tpacpi_led_state_cache[led] = ledstatus;
4094
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004095 return rc;
4096}
4097
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004098static void led_sysfs_set_status(unsigned int led,
4099 enum led_brightness brightness)
4100{
4101 led_set_status(led,
4102 (brightness == LED_OFF) ?
4103 TPACPI_LED_OFF :
4104 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4105 TPACPI_LED_BLINK : TPACPI_LED_ON);
4106}
4107
4108static void led_set_status_worker(struct work_struct *work)
4109{
4110 struct tpacpi_led_classdev *data =
4111 container_of(work, struct tpacpi_led_classdev, work);
4112
4113 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4114 led_sysfs_set_status(data->led, data->new_brightness);
4115}
4116
4117static void led_sysfs_set(struct led_classdev *led_cdev,
4118 enum led_brightness brightness)
4119{
4120 struct tpacpi_led_classdev *data = container_of(led_cdev,
4121 struct tpacpi_led_classdev, led_classdev);
4122
4123 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004124 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004125}
4126
4127static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4128 unsigned long *delay_on, unsigned long *delay_off)
4129{
4130 struct tpacpi_led_classdev *data = container_of(led_cdev,
4131 struct tpacpi_led_classdev, led_classdev);
4132
4133 /* Can we choose the flash rate? */
4134 if (*delay_on == 0 && *delay_off == 0) {
4135 /* yes. set them to the hardware blink rate (1 Hz) */
4136 *delay_on = 500; /* ms */
4137 *delay_off = 500; /* ms */
4138 } else if ((*delay_on != 500) || (*delay_off != 500))
4139 return -EINVAL;
4140
4141 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004142 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004143
4144 return 0;
4145}
4146
4147static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4148{
4149 int rc;
4150
4151 struct tpacpi_led_classdev *data = container_of(led_cdev,
4152 struct tpacpi_led_classdev, led_classdev);
4153
4154 rc = led_get_status(data->led);
4155
4156 if (rc == TPACPI_LED_OFF || rc < 0)
4157 rc = LED_OFF; /* no error handling in led class :( */
4158 else
4159 rc = LED_FULL;
4160
4161 return rc;
4162}
4163
4164static void led_exit(void)
4165{
4166 unsigned int i;
4167
4168 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4169 if (tpacpi_leds[i].led_classdev.name)
4170 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4171 }
4172
4173 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004174}
4175
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004176static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004177{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004178 unsigned int i;
4179 int rc;
4180
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004181 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4182
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004183 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004184
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004185 if (!led_handle)
4186 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004187 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004188 else if (strlencmp(led_path, "SLED") == 0)
4189 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004190 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004191 else if (strlencmp(led_path, "SYSL") == 0)
4192 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004193 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004194 else
4195 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004196 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004197
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004198 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4199 str_supported(led_supported), led_supported);
4200
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004201 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4202 GFP_KERNEL);
4203 if (!tpacpi_leds) {
4204 printk(TPACPI_ERR "Out of memory for LED data\n");
4205 return -ENOMEM;
4206 }
4207
4208 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4209 tpacpi_leds[i].led = i;
4210
4211 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4212 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4213 if (led_supported == TPACPI_LED_570)
4214 tpacpi_leds[i].led_classdev.brightness_get =
4215 &led_sysfs_get;
4216
4217 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4218
4219 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4220
4221 rc = led_classdev_register(&tpacpi_pdev->dev,
4222 &tpacpi_leds[i].led_classdev);
4223 if (rc < 0) {
4224 tpacpi_leds[i].led_classdev.name = NULL;
4225 led_exit();
4226 return rc;
4227 }
4228 }
4229
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004230 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004231}
4232
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004233#define str_led_status(s) \
4234 ((s) == TPACPI_LED_OFF ? "off" : \
4235 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004236
4237static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
4239 int len = 0;
4240
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004241 if (!led_supported) {
4242 len += sprintf(p + len, "status:\t\tnot supported\n");
4243 return len;
4244 }
4245 len += sprintf(p + len, "status:\t\tsupported\n");
4246
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004247 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004248 /* 570 */
4249 int i, status;
4250 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004251 status = led_get_status(i);
4252 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004253 return -EIO;
4254 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004255 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004256 }
4257 }
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004260 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 return len;
4263}
4264
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004265static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
4267 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004268 int led, rc;
4269 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004270
4271 if (!led_supported)
4272 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004275 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return -EINVAL;
4277
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004278 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004279 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004281 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004282 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004283 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004284 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004285 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004286 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004287
4288 rc = led_set_status(led, s);
4289 if (rc < 0)
4290 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
4292
4293 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004294}
4295
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004296static struct ibm_struct led_driver_data = {
4297 .name = "led",
4298 .read = led_read,
4299 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004300 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004301};
4302
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004303/*************************************************************************
4304 * Beep subdriver
4305 */
4306
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004307TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004308
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004309static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004310{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004311 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4312
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004313 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004314
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004315 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4316 str_supported(beep_handle != NULL));
4317
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004318 return (beep_handle)? 0 : 1;
4319}
4320
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004321static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
4323 int len = 0;
4324
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004325 if (!beep_handle)
4326 len += sprintf(p + len, "status:\t\tnot supported\n");
4327 else {
4328 len += sprintf(p + len, "status:\t\tsupported\n");
4329 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 return len;
4333}
4334
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004335static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
4337 char *cmd;
4338 int beep_cmd;
4339
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004340 if (!beep_handle)
4341 return -ENODEV;
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004344 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4345 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 /* beep_cmd set */
4347 } else
4348 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004349 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return -EIO;
4351 }
4352
4353 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004354}
4355
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004356static struct ibm_struct beep_driver_data = {
4357 .name = "beep",
4358 .read = beep_read,
4359 .write = beep_write,
4360};
4361
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004362/*************************************************************************
4363 * Thermal subdriver
4364 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004365
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004366enum thermal_access_mode {
4367 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4368 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4369 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4370 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4371 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4372};
4373
4374enum { /* TPACPI_THERMAL_TPEC_* */
4375 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4376 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4377 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4378};
4379
4380#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4381struct ibm_thermal_sensors_struct {
4382 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4383};
4384
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004385static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004386
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004387/* idx is zero-based */
4388static int thermal_get_sensor(int idx, s32 *value)
4389{
4390 int t;
4391 s8 tmp;
4392 char tmpi[5];
4393
4394 t = TP_EC_THERMAL_TMP0;
4395
4396 switch (thermal_read_mode) {
4397#if TPACPI_MAX_THERMAL_SENSORS >= 16
4398 case TPACPI_THERMAL_TPEC_16:
4399 if (idx >= 8 && idx <= 15) {
4400 t = TP_EC_THERMAL_TMP8;
4401 idx -= 8;
4402 }
4403 /* fallthrough */
4404#endif
4405 case TPACPI_THERMAL_TPEC_8:
4406 if (idx <= 7) {
4407 if (!acpi_ec_read(t + idx, &tmp))
4408 return -EIO;
4409 *value = tmp * 1000;
4410 return 0;
4411 }
4412 break;
4413
4414 case TPACPI_THERMAL_ACPI_UPDT:
4415 if (idx <= 7) {
4416 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4417 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4418 return -EIO;
4419 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4420 return -EIO;
4421 *value = (t - 2732) * 100;
4422 return 0;
4423 }
4424 break;
4425
4426 case TPACPI_THERMAL_ACPI_TMP07:
4427 if (idx <= 7) {
4428 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4429 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4430 return -EIO;
4431 if (t > 127 || t < -127)
4432 t = TP_EC_THERMAL_TMP_NA;
4433 *value = t * 1000;
4434 return 0;
4435 }
4436 break;
4437
4438 case TPACPI_THERMAL_NONE:
4439 default:
4440 return -ENOSYS;
4441 }
4442
4443 return -EINVAL;
4444}
4445
4446static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4447{
4448 int res, i;
4449 int n;
4450
4451 n = 8;
4452 i = 0;
4453
4454 if (!s)
4455 return -EINVAL;
4456
4457 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4458 n = 16;
4459
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004460 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004461 res = thermal_get_sensor(i, &s->temp[i]);
4462 if (res)
4463 return res;
4464 }
4465
4466 return n;
4467}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004468
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004469/* sysfs temp##_input -------------------------------------------------- */
4470
4471static ssize_t thermal_temp_input_show(struct device *dev,
4472 struct device_attribute *attr,
4473 char *buf)
4474{
4475 struct sensor_device_attribute *sensor_attr =
4476 to_sensor_dev_attr(attr);
4477 int idx = sensor_attr->index;
4478 s32 value;
4479 int res;
4480
4481 res = thermal_get_sensor(idx, &value);
4482 if (res)
4483 return res;
4484 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4485 return -ENXIO;
4486
4487 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4488}
4489
4490#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004491 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4492 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004493
4494static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4495 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4496 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4497 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4498 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4499 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4500 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4501 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4502 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4503 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4504 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4505 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4506 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4507 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4508 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4509 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4510 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4511};
4512
4513#define THERMAL_ATTRS(X) \
4514 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4515
4516static struct attribute *thermal_temp_input_attr[] = {
4517 THERMAL_ATTRS(8),
4518 THERMAL_ATTRS(9),
4519 THERMAL_ATTRS(10),
4520 THERMAL_ATTRS(11),
4521 THERMAL_ATTRS(12),
4522 THERMAL_ATTRS(13),
4523 THERMAL_ATTRS(14),
4524 THERMAL_ATTRS(15),
4525 THERMAL_ATTRS(0),
4526 THERMAL_ATTRS(1),
4527 THERMAL_ATTRS(2),
4528 THERMAL_ATTRS(3),
4529 THERMAL_ATTRS(4),
4530 THERMAL_ATTRS(5),
4531 THERMAL_ATTRS(6),
4532 THERMAL_ATTRS(7),
4533 NULL
4534};
4535
4536static const struct attribute_group thermal_temp_input16_group = {
4537 .attrs = thermal_temp_input_attr
4538};
4539
4540static const struct attribute_group thermal_temp_input8_group = {
4541 .attrs = &thermal_temp_input_attr[8]
4542};
4543
4544#undef THERMAL_SENSOR_ATTR_TEMP
4545#undef THERMAL_ATTRS
4546
4547/* --------------------------------------------------------------------- */
4548
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004549static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004550{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004551 u8 t, ta1, ta2;
4552 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004553 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004554 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004555
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004556 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4557
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004558 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004559
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004560 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004561 /*
4562 * Direct EC access mode: sensors at registers
4563 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4564 * non-implemented, thermal sensors return 0x80 when
4565 * not available
4566 */
4567
4568 ta1 = ta2 = 0;
4569 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004570 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004571 ta1 |= t;
4572 } else {
4573 ta1 = 0;
4574 break;
4575 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004576 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004577 ta2 |= t;
4578 } else {
4579 ta1 = 0;
4580 break;
4581 }
4582 }
4583 if (ta1 == 0) {
4584 /* This is sheer paranoia, but we handle it anyway */
4585 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004586 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004587 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004588 "falling back to ACPI TMPx access "
4589 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004590 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004591 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004592 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004593 "ThinkPad ACPI EC access misbehaving, "
4594 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004595 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004596 }
4597 } else {
4598 thermal_read_mode =
4599 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004600 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004601 }
4602 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004603 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4604 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004605 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004606 } else {
4607 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004608 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004609 }
4610 } else {
4611 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004612 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004613 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004614
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004615 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4616 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4617 thermal_read_mode);
4618
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004619 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004620 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004621 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004622 &thermal_temp_input16_group);
4623 if (res)
4624 return res;
4625 break;
4626 case TPACPI_THERMAL_TPEC_8:
4627 case TPACPI_THERMAL_ACPI_TMP07:
4628 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004629 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004630 &thermal_temp_input8_group);
4631 if (res)
4632 return res;
4633 break;
4634 case TPACPI_THERMAL_NONE:
4635 default:
4636 return 1;
4637 }
4638
4639 return 0;
4640}
4641
4642static void thermal_exit(void)
4643{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004644 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004645 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004646 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004647 &thermal_temp_input16_group);
4648 break;
4649 case TPACPI_THERMAL_TPEC_8:
4650 case TPACPI_THERMAL_ACPI_TMP07:
4651 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004652 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004653 &thermal_temp_input16_group);
4654 break;
4655 case TPACPI_THERMAL_NONE:
4656 default:
4657 break;
4658 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004659}
4660
4661static int thermal_read(char *p)
4662{
4663 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004664 int n, i;
4665 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004666
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004667 n = thermal_get_sensors(&t);
4668 if (unlikely(n < 0))
4669 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004670
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004671 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004672
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004673 if (n > 0) {
4674 for (i = 0; i < (n - 1); i++)
4675 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4676 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4677 } else
4678 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004679
4680 return len;
4681}
4682
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004683static struct ibm_struct thermal_driver_data = {
4684 .name = "thermal",
4685 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004686 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004687};
4688
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004689/*************************************************************************
4690 * EC Dump subdriver
4691 */
4692
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004693static u8 ecdump_regs[256];
4694
4695static int ecdump_read(char *p)
4696{
4697 int len = 0;
4698 int i, j;
4699 u8 v;
4700
4701 len += sprintf(p + len, "EC "
4702 " +00 +01 +02 +03 +04 +05 +06 +07"
4703 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4704 for (i = 0; i < 256; i += 16) {
4705 len += sprintf(p + len, "EC 0x%02x:", i);
4706 for (j = 0; j < 16; j++) {
4707 if (!acpi_ec_read(i + j, &v))
4708 break;
4709 if (v != ecdump_regs[i + j])
4710 len += sprintf(p + len, " *%02x", v);
4711 else
4712 len += sprintf(p + len, " %02x", v);
4713 ecdump_regs[i + j] = v;
4714 }
4715 len += sprintf(p + len, "\n");
4716 if (j != 16)
4717 break;
4718 }
4719
4720 /* These are way too dangerous to advertise openly... */
4721#if 0
4722 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4723 " (<offset> is 00-ff, <value> is 00-ff)\n");
4724 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4725 " (<offset> is 00-ff, <value> is 0-255)\n");
4726#endif
4727 return len;
4728}
4729
4730static int ecdump_write(char *buf)
4731{
4732 char *cmd;
4733 int i, v;
4734
4735 while ((cmd = next_cmd(&buf))) {
4736 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4737 /* i and v set */
4738 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4739 /* i and v set */
4740 } else
4741 return -EINVAL;
4742 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4743 if (!acpi_ec_write(i, v))
4744 return -EIO;
4745 } else
4746 return -EINVAL;
4747 }
4748
4749 return 0;
4750}
4751
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004752static struct ibm_struct ecdump_driver_data = {
4753 .name = "ecdump",
4754 .read = ecdump_read,
4755 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004756 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004757};
4758
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004759/*************************************************************************
4760 * Backlight/brightness subdriver
4761 */
Holger Macht8acb0252006-10-20 14:30:28 -07004762
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004763#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4764
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004765enum {
4766 TP_EC_BACKLIGHT = 0x31,
4767
4768 /* TP_EC_BACKLIGHT bitmasks */
4769 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4770 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4771 TP_EC_BACKLIGHT_MAPSW = 0x20,
4772};
4773
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004774static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004775static int brightness_mode;
4776static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4777
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004778static struct mutex brightness_mutex;
4779
4780/*
4781 * ThinkPads can read brightness from two places: EC 0x31, or
4782 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004783 *
4784 * EC 0x31 has the following layout
4785 * Bit 7: unknown function
4786 * Bit 6: unknown function
4787 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4788 * Bit 4: must be set to zero to avoid problems
4789 * Bit 3-0: backlight brightness level
4790 *
4791 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004792 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004793static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004794{
4795 u8 lec = 0, lcmos = 0, level = 0;
4796
4797 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004798 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004799 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004800 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004801 };
4802 if (brightness_mode & 2) {
4803 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4804 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4805 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4806 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4807 level = lcmos;
4808 }
4809
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004810 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004811 *status = lec; /* Prefer EC, CMOS is just a backing store */
4812 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004813 if (lec == lcmos)
4814 tp_warned.bright_cmos_ec_unsync = 0;
4815 else {
4816 if (!tp_warned.bright_cmos_ec_unsync) {
4817 printk(TPACPI_ERR
4818 "CMOS NVRAM (%u) and EC (%u) do not "
4819 "agree on display brightness level\n",
4820 (unsigned int) lcmos,
4821 (unsigned int) lec);
4822 tp_warned.bright_cmos_ec_unsync = 1;
4823 }
4824 return -EIO;
4825 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004826 } else {
4827 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004828 }
4829
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004830 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004831}
4832
4833/* May return EINTR which can always be mapped to ERESTARTSYS */
4834static int brightness_set(int value)
4835{
4836 int cmos_cmd, inc, i, res;
4837 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004838 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004839
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004840 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4841 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004842 return -EINVAL;
4843
4844 res = mutex_lock_interruptible(&brightness_mutex);
4845 if (res < 0)
4846 return res;
4847
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004848 res = brightness_get_raw(&current_value);
4849 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004850 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004851
4852 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4853 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004854
4855 cmos_cmd = value > current_value ?
4856 TP_CMOS_BRIGHTNESS_UP :
4857 TP_CMOS_BRIGHTNESS_DOWN;
4858 inc = (value > current_value)? 1 : -1;
4859
4860 res = 0;
4861 for (i = current_value; i != value; i += inc) {
4862 if ((brightness_mode & 2) &&
4863 issue_thinkpad_cmos_command(cmos_cmd)) {
4864 res = -EIO;
4865 goto errout;
4866 }
4867 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004868 !acpi_ec_write(TP_EC_BACKLIGHT,
4869 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004870 res = -EIO;
4871 goto errout;;
4872 }
4873 }
4874
4875errout:
4876 mutex_unlock(&brightness_mutex);
4877 return res;
4878}
4879
4880/* sysfs backlight class ----------------------------------------------- */
4881
4882static int brightness_update_status(struct backlight_device *bd)
4883{
4884 /* it is the backlight class's job (caller) to handle
4885 * EINTR and other errors properly */
4886 return brightness_set(
4887 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4888 bd->props.power == FB_BLANK_UNBLANK) ?
4889 bd->props.brightness : 0);
4890}
Holger Macht8acb0252006-10-20 14:30:28 -07004891
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004892static int brightness_get(struct backlight_device *bd)
4893{
4894 int status, res;
4895
4896 res = brightness_get_raw(&status);
4897 if (res < 0)
4898 return 0; /* FIXME: teach backlight about error handling */
4899
4900 return status & TP_EC_BACKLIGHT_LVLMSK;
4901}
4902
Richard Purdie599a52d2007-02-10 23:07:48 +00004903static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004904 .get_brightness = brightness_get,
4905 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004906};
4907
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004908/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004909
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004910static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004911{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004912 int b;
4913
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004914 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4915
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004916 mutex_init(&brightness_mutex);
4917
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004918 /*
4919 * We always attempt to detect acpi support, so as to switch
4920 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4921 * going to publish a backlight interface
4922 */
4923 b = tpacpi_check_std_acpi_brightness_support();
4924 if (b > 0) {
4925 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4926 printk(TPACPI_NOTICE
4927 "Lenovo BIOS switched to ACPI backlight "
4928 "control mode\n");
4929 }
4930 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004931 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004932 "standard ACPI backlight interface "
4933 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004934 return 1;
4935 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004936 }
4937
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004938 if (!brightness_enable) {
4939 dbg_printk(TPACPI_DBG_INIT,
4940 "brightness support disabled by "
4941 "module parameter\n");
4942 return 1;
4943 }
4944
4945 if (b > 16) {
4946 printk(TPACPI_ERR
4947 "Unsupported brightness interface, "
4948 "please contact %s\n", TPACPI_MAIL);
4949 return 1;
4950 }
4951 if (b == 16)
4952 tp_features.bright_16levels = 1;
4953
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004954 if (!brightness_mode) {
4955 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4956 brightness_mode = 2;
4957 else
4958 brightness_mode = 3;
4959
4960 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4961 brightness_mode);
4962 }
4963
4964 if (brightness_mode > 3)
4965 return -EINVAL;
4966
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004967 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004968 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004969
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004970 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004971 printk(TPACPI_INFO
4972 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004973
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004974 ibm_backlight_device = backlight_device_register(
4975 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4976 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004977 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004978 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004979 return PTR_ERR(ibm_backlight_device);
4980 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004981 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004982
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004983 ibm_backlight_device->props.max_brightness =
4984 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004985 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004986 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004987
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004988 return 0;
4989}
4990
4991static void brightness_exit(void)
4992{
4993 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004994 vdbg_printk(TPACPI_DBG_EXIT,
4995 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004996 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004997 }
4998}
4999
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005000static int brightness_read(char *p)
5001{
5002 int len = 0;
5003 int level;
5004
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005005 level = brightness_get(NULL);
5006 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005007 len += sprintf(p + len, "level:\t\tunreadable\n");
5008 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005009 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005010 len += sprintf(p + len, "commands:\tup, down\n");
5011 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005012 " (<level> is 0-%d)\n",
5013 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005014 }
5015
5016 return len;
5017}
5018
5019static int brightness_write(char *buf)
5020{
5021 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005022 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005023 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005024 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005025
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005026 level = brightness_get(NULL);
5027 if (level < 0)
5028 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005029
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005030 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005031 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005032 if (level < max_level)
5033 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005034 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005035 if (level > 0)
5036 level--;
5037 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5038 level >= 0 && level <= max_level) {
5039 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005040 } else
5041 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005042 }
5043
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005044 /*
5045 * Now we know what the final level should be, so we try to set it.
5046 * Doing it this way makes the syscall restartable in case of EINTR
5047 */
5048 rc = brightness_set(level);
5049 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005050}
5051
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005052static struct ibm_struct brightness_driver_data = {
5053 .name = "brightness",
5054 .read = brightness_read,
5055 .write = brightness_write,
5056 .exit = brightness_exit,
5057};
5058
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005059/*************************************************************************
5060 * Volume subdriver
5061 */
5062
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005063static int volume_offset = 0x30;
5064
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005065static int volume_read(char *p)
5066{
5067 int len = 0;
5068 u8 level;
5069
5070 if (!acpi_ec_read(volume_offset, &level)) {
5071 len += sprintf(p + len, "level:\t\tunreadable\n");
5072 } else {
5073 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5074 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5075 len += sprintf(p + len, "commands:\tup, down, mute\n");
5076 len += sprintf(p + len, "commands:\tlevel <level>"
5077 " (<level> is 0-15)\n");
5078 }
5079
5080 return len;
5081}
5082
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005083static int volume_write(char *buf)
5084{
5085 int cmos_cmd, inc, i;
5086 u8 level, mute;
5087 int new_level, new_mute;
5088 char *cmd;
5089
5090 while ((cmd = next_cmd(&buf))) {
5091 if (!acpi_ec_read(volume_offset, &level))
5092 return -EIO;
5093 new_mute = mute = level & 0x40;
5094 new_level = level = level & 0xf;
5095
5096 if (strlencmp(cmd, "up") == 0) {
5097 if (mute)
5098 new_mute = 0;
5099 else
5100 new_level = level == 15 ? 15 : level + 1;
5101 } else if (strlencmp(cmd, "down") == 0) {
5102 if (mute)
5103 new_mute = 0;
5104 else
5105 new_level = level == 0 ? 0 : level - 1;
5106 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5107 new_level >= 0 && new_level <= 15) {
5108 /* new_level set */
5109 } else if (strlencmp(cmd, "mute") == 0) {
5110 new_mute = 0x40;
5111 } else
5112 return -EINVAL;
5113
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005114 if (new_level != level) {
5115 /* mute doesn't change */
5116
5117 cmos_cmd = (new_level > level) ?
5118 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005119 inc = new_level > level ? 1 : -1;
5120
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005121 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005122 !acpi_ec_write(volume_offset, level)))
5123 return -EIO;
5124
5125 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005126 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005127 !acpi_ec_write(volume_offset, i + inc))
5128 return -EIO;
5129
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005130 if (mute &&
5131 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5132 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005133 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005134 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005135 }
5136
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005137 if (new_mute != mute) {
5138 /* level doesn't change */
5139
5140 cmos_cmd = (new_mute) ?
5141 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005142
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005143 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005144 !acpi_ec_write(volume_offset, level + new_mute))
5145 return -EIO;
5146 }
5147 }
5148
5149 return 0;
5150}
5151
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005152static struct ibm_struct volume_driver_data = {
5153 .name = "volume",
5154 .read = volume_read,
5155 .write = volume_write,
5156};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005157
5158/*************************************************************************
5159 * Fan subdriver
5160 */
5161
5162/*
5163 * FAN ACCESS MODES
5164 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005165 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005166 * ACPI GFAN method: returns fan level
5167 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005168 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005169 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005170 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005171 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005172 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5173 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005174 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5175 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005176 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005177 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005178 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5179 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005180 *
5181 * Fan speed changes of any sort (including those caused by the
5182 * disengaged mode) are usually done slowly by the firmware as the
5183 * maximum ammount of fan duty cycle change per second seems to be
5184 * limited.
5185 *
5186 * Reading is not available if GFAN exists.
5187 * Writing is not available if SFAN exists.
5188 *
5189 * Bits
5190 * 7 automatic mode engaged;
5191 * (default operation mode of the ThinkPad)
5192 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005193 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005194 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005195 * the tachometer while the fan controller ramps up
5196 * the speed (which can take up to a few *minutes*).
5197 * Speeds up fan to 100% duty-cycle, which is far above
5198 * the standard RPM levels. It is not impossible that
5199 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005200 * 5-3 unused in some models. Extra bits for fan level
5201 * in others, but still useless as all values above
5202 * 7 map to the same speed as level 7 in these models.
5203 * 2-0 fan level (0..7 usually)
5204 * 0x00 = stop
5205 * 0x07 = max (set when temperatures critical)
5206 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005207 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005208 *
5209 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5210 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5211 * does so, its initial value is meaningless (0x07).
5212 *
5213 * For firmware bugs, refer to:
5214 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5215 *
5216 * ----
5217 *
5218 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5219 * Main fan tachometer reading (in RPM)
5220 *
5221 * This register is present on all ThinkPads with a new-style EC, and
5222 * it is known not to be present on the A21m/e, and T22, as there is
5223 * something else in offset 0x84 according to the ACPI DSDT. Other
5224 * ThinkPads from this same time period (and earlier) probably lack the
5225 * tachometer as well.
5226 *
5227 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5228 * was never fixed by IBM to report the EC firmware version string
5229 * probably support the tachometer (like the early X models), so
5230 * detecting it is quite hard. We need more data to know for sure.
5231 *
5232 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5233 * might result.
5234 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005235 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5236 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005237 *
5238 * For firmware bugs, refer to:
5239 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5240 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005241 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005242 * ThinkPad X31, X40, X41. Not available in the X60.
5243 *
5244 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5245 * high speed. ACPI DSDT seems to map these three speeds to levels
5246 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5247 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5248 *
5249 * The speeds are stored on handles
5250 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5251 *
5252 * There are three default speed sets, acessible as handles:
5253 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5254 *
5255 * ACPI DSDT switches which set is in use depending on various
5256 * factors.
5257 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005258 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005259 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5260 * but the ACPI tables just mention level 7.
5261 */
5262
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005263enum { /* Fan control constants */
5264 fan_status_offset = 0x2f, /* EC register 0x2f */
5265 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5266 * 0x84 must be read before 0x85 */
5267
5268 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5269 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5270
5271 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5272};
5273
5274enum fan_status_access_mode {
5275 TPACPI_FAN_NONE = 0, /* No fan status or control */
5276 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5277 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5278};
5279
5280enum fan_control_access_mode {
5281 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5282 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5283 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5284 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5285};
5286
5287enum fan_control_commands {
5288 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5289 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5290 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5291 * and also watchdog cmd */
5292};
5293
5294static int fan_control_allowed;
5295
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005296static enum fan_status_access_mode fan_status_access_mode;
5297static enum fan_control_access_mode fan_control_access_mode;
5298static enum fan_control_commands fan_control_commands;
5299
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005300static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005301static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005302static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005303
5304static struct mutex fan_mutex;
5305
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005306static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005307static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005308
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005309TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5310TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005311 "\\FSPD", /* 600e/x, 770e, 770x */
5312 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005313TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005314 "JFNS", /* 770x-JL */
5315 ); /* all others */
5316
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005317/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005318 * Call with fan_mutex held
5319 */
5320static void fan_update_desired_level(u8 status)
5321{
5322 if ((status &
5323 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5324 if (status > 7)
5325 fan_control_desired_level = 7;
5326 else
5327 fan_control_desired_level = status;
5328 }
5329}
5330
5331static int fan_get_status(u8 *status)
5332{
5333 u8 s;
5334
5335 /* TODO:
5336 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5337
5338 switch (fan_status_access_mode) {
5339 case TPACPI_FAN_RD_ACPI_GFAN:
5340 /* 570, 600e/x, 770e, 770x */
5341
5342 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5343 return -EIO;
5344
5345 if (likely(status))
5346 *status = s & 0x07;
5347
5348 break;
5349
5350 case TPACPI_FAN_RD_TPEC:
5351 /* all except 570, 600e/x, 770e, 770x */
5352 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5353 return -EIO;
5354
5355 if (likely(status))
5356 *status = s;
5357
5358 break;
5359
5360 default:
5361 return -ENXIO;
5362 }
5363
5364 return 0;
5365}
5366
5367static int fan_get_status_safe(u8 *status)
5368{
5369 int rc;
5370 u8 s;
5371
5372 if (mutex_lock_interruptible(&fan_mutex))
5373 return -ERESTARTSYS;
5374 rc = fan_get_status(&s);
5375 if (!rc)
5376 fan_update_desired_level(s);
5377 mutex_unlock(&fan_mutex);
5378
5379 if (status)
5380 *status = s;
5381
5382 return rc;
5383}
5384
5385static int fan_get_speed(unsigned int *speed)
5386{
5387 u8 hi, lo;
5388
5389 switch (fan_status_access_mode) {
5390 case TPACPI_FAN_RD_TPEC:
5391 /* all except 570, 600e/x, 770e, 770x */
5392 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5393 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5394 return -EIO;
5395
5396 if (likely(speed))
5397 *speed = (hi << 8) | lo;
5398
5399 break;
5400
5401 default:
5402 return -ENXIO;
5403 }
5404
5405 return 0;
5406}
5407
5408static int fan_set_level(int level)
5409{
5410 if (!fan_control_allowed)
5411 return -EPERM;
5412
5413 switch (fan_control_access_mode) {
5414 case TPACPI_FAN_WR_ACPI_SFAN:
5415 if (level >= 0 && level <= 7) {
5416 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5417 return -EIO;
5418 } else
5419 return -EINVAL;
5420 break;
5421
5422 case TPACPI_FAN_WR_ACPI_FANS:
5423 case TPACPI_FAN_WR_TPEC:
5424 if ((level != TP_EC_FAN_AUTO) &&
5425 (level != TP_EC_FAN_FULLSPEED) &&
5426 ((level < 0) || (level > 7)))
5427 return -EINVAL;
5428
5429 /* safety net should the EC not support AUTO
5430 * or FULLSPEED mode bits and just ignore them */
5431 if (level & TP_EC_FAN_FULLSPEED)
5432 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005433 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005434 level |= 4; /* safety min speed 4 */
5435
5436 if (!acpi_ec_write(fan_status_offset, level))
5437 return -EIO;
5438 else
5439 tp_features.fan_ctrl_status_undef = 0;
5440 break;
5441
5442 default:
5443 return -ENXIO;
5444 }
5445 return 0;
5446}
5447
5448static int fan_set_level_safe(int level)
5449{
5450 int rc;
5451
5452 if (!fan_control_allowed)
5453 return -EPERM;
5454
5455 if (mutex_lock_interruptible(&fan_mutex))
5456 return -ERESTARTSYS;
5457
5458 if (level == TPACPI_FAN_LAST_LEVEL)
5459 level = fan_control_desired_level;
5460
5461 rc = fan_set_level(level);
5462 if (!rc)
5463 fan_update_desired_level(level);
5464
5465 mutex_unlock(&fan_mutex);
5466 return rc;
5467}
5468
5469static int fan_set_enable(void)
5470{
5471 u8 s;
5472 int rc;
5473
5474 if (!fan_control_allowed)
5475 return -EPERM;
5476
5477 if (mutex_lock_interruptible(&fan_mutex))
5478 return -ERESTARTSYS;
5479
5480 switch (fan_control_access_mode) {
5481 case TPACPI_FAN_WR_ACPI_FANS:
5482 case TPACPI_FAN_WR_TPEC:
5483 rc = fan_get_status(&s);
5484 if (rc < 0)
5485 break;
5486
5487 /* Don't go out of emergency fan mode */
5488 if (s != 7) {
5489 s &= 0x07;
5490 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5491 }
5492
5493 if (!acpi_ec_write(fan_status_offset, s))
5494 rc = -EIO;
5495 else {
5496 tp_features.fan_ctrl_status_undef = 0;
5497 rc = 0;
5498 }
5499 break;
5500
5501 case TPACPI_FAN_WR_ACPI_SFAN:
5502 rc = fan_get_status(&s);
5503 if (rc < 0)
5504 break;
5505
5506 s &= 0x07;
5507
5508 /* Set fan to at least level 4 */
5509 s |= 4;
5510
5511 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005512 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005513 else
5514 rc = 0;
5515 break;
5516
5517 default:
5518 rc = -ENXIO;
5519 }
5520
5521 mutex_unlock(&fan_mutex);
5522 return rc;
5523}
5524
5525static int fan_set_disable(void)
5526{
5527 int rc;
5528
5529 if (!fan_control_allowed)
5530 return -EPERM;
5531
5532 if (mutex_lock_interruptible(&fan_mutex))
5533 return -ERESTARTSYS;
5534
5535 rc = 0;
5536 switch (fan_control_access_mode) {
5537 case TPACPI_FAN_WR_ACPI_FANS:
5538 case TPACPI_FAN_WR_TPEC:
5539 if (!acpi_ec_write(fan_status_offset, 0x00))
5540 rc = -EIO;
5541 else {
5542 fan_control_desired_level = 0;
5543 tp_features.fan_ctrl_status_undef = 0;
5544 }
5545 break;
5546
5547 case TPACPI_FAN_WR_ACPI_SFAN:
5548 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5549 rc = -EIO;
5550 else
5551 fan_control_desired_level = 0;
5552 break;
5553
5554 default:
5555 rc = -ENXIO;
5556 }
5557
5558
5559 mutex_unlock(&fan_mutex);
5560 return rc;
5561}
5562
5563static int fan_set_speed(int speed)
5564{
5565 int rc;
5566
5567 if (!fan_control_allowed)
5568 return -EPERM;
5569
5570 if (mutex_lock_interruptible(&fan_mutex))
5571 return -ERESTARTSYS;
5572
5573 rc = 0;
5574 switch (fan_control_access_mode) {
5575 case TPACPI_FAN_WR_ACPI_FANS:
5576 if (speed >= 0 && speed <= 65535) {
5577 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5578 speed, speed, speed))
5579 rc = -EIO;
5580 } else
5581 rc = -EINVAL;
5582 break;
5583
5584 default:
5585 rc = -ENXIO;
5586 }
5587
5588 mutex_unlock(&fan_mutex);
5589 return rc;
5590}
5591
5592static void fan_watchdog_reset(void)
5593{
5594 static int fan_watchdog_active;
5595
5596 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5597 return;
5598
5599 if (fan_watchdog_active)
5600 cancel_delayed_work(&fan_watchdog_task);
5601
5602 if (fan_watchdog_maxinterval > 0 &&
5603 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5604 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005605 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005606 msecs_to_jiffies(fan_watchdog_maxinterval
5607 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005608 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005609 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005610 "watchdog will not trigger\n");
5611 }
5612 } else
5613 fan_watchdog_active = 0;
5614}
5615
5616static void fan_watchdog_fire(struct work_struct *ignored)
5617{
5618 int rc;
5619
5620 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5621 return;
5622
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005623 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005624 rc = fan_set_enable();
5625 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005626 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005627 "will try again later...\n", -rc);
5628 /* reschedule for later */
5629 fan_watchdog_reset();
5630 }
5631}
5632
5633/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005634 * SYSFS fan layout: hwmon compatible (device)
5635 *
5636 * pwm*_enable:
5637 * 0: "disengaged" mode
5638 * 1: manual mode
5639 * 2: native EC "auto" mode (recommended, hardware default)
5640 *
5641 * pwm*: set speed in manual mode, ignored otherwise.
5642 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5643 * interpolation.
5644 *
5645 * fan*_input: tachometer reading, RPM
5646 *
5647 *
5648 * SYSFS fan layout: extensions
5649 *
5650 * fan_watchdog (driver):
5651 * fan watchdog interval in seconds, 0 disables (default), max 120
5652 */
5653
5654/* sysfs fan pwm1_enable ----------------------------------------------- */
5655static ssize_t fan_pwm1_enable_show(struct device *dev,
5656 struct device_attribute *attr,
5657 char *buf)
5658{
5659 int res, mode;
5660 u8 status;
5661
5662 res = fan_get_status_safe(&status);
5663 if (res)
5664 return res;
5665
5666 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5667 if (status != fan_control_initial_status) {
5668 tp_features.fan_ctrl_status_undef = 0;
5669 } else {
5670 /* Return most likely status. In fact, it
5671 * might be the only possible status */
5672 status = TP_EC_FAN_AUTO;
5673 }
5674 }
5675
5676 if (status & TP_EC_FAN_FULLSPEED) {
5677 mode = 0;
5678 } else if (status & TP_EC_FAN_AUTO) {
5679 mode = 2;
5680 } else
5681 mode = 1;
5682
5683 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5684}
5685
5686static ssize_t fan_pwm1_enable_store(struct device *dev,
5687 struct device_attribute *attr,
5688 const char *buf, size_t count)
5689{
5690 unsigned long t;
5691 int res, level;
5692
5693 if (parse_strtoul(buf, 2, &t))
5694 return -EINVAL;
5695
5696 switch (t) {
5697 case 0:
5698 level = TP_EC_FAN_FULLSPEED;
5699 break;
5700 case 1:
5701 level = TPACPI_FAN_LAST_LEVEL;
5702 break;
5703 case 2:
5704 level = TP_EC_FAN_AUTO;
5705 break;
5706 case 3:
5707 /* reserved for software-controlled auto mode */
5708 return -ENOSYS;
5709 default:
5710 return -EINVAL;
5711 }
5712
5713 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005714 if (res == -ENXIO)
5715 return -EINVAL;
5716 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005717 return res;
5718
5719 fan_watchdog_reset();
5720
5721 return count;
5722}
5723
5724static struct device_attribute dev_attr_fan_pwm1_enable =
5725 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5726 fan_pwm1_enable_show, fan_pwm1_enable_store);
5727
5728/* sysfs fan pwm1 ------------------------------------------------------ */
5729static ssize_t fan_pwm1_show(struct device *dev,
5730 struct device_attribute *attr,
5731 char *buf)
5732{
5733 int res;
5734 u8 status;
5735
5736 res = fan_get_status_safe(&status);
5737 if (res)
5738 return res;
5739
5740 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5741 if (status != fan_control_initial_status) {
5742 tp_features.fan_ctrl_status_undef = 0;
5743 } else {
5744 status = TP_EC_FAN_AUTO;
5745 }
5746 }
5747
5748 if ((status &
5749 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5750 status = fan_control_desired_level;
5751
5752 if (status > 7)
5753 status = 7;
5754
5755 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5756}
5757
5758static ssize_t fan_pwm1_store(struct device *dev,
5759 struct device_attribute *attr,
5760 const char *buf, size_t count)
5761{
5762 unsigned long s;
5763 int rc;
5764 u8 status, newlevel;
5765
5766 if (parse_strtoul(buf, 255, &s))
5767 return -EINVAL;
5768
5769 /* scale down from 0-255 to 0-7 */
5770 newlevel = (s >> 5) & 0x07;
5771
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005772 if (mutex_lock_interruptible(&fan_mutex))
5773 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005774
5775 rc = fan_get_status(&status);
5776 if (!rc && (status &
5777 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5778 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005779 if (rc == -ENXIO)
5780 rc = -EINVAL;
5781 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005782 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005783 fan_watchdog_reset();
5784 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005785 }
5786
5787 mutex_unlock(&fan_mutex);
5788 return (rc)? rc : count;
5789}
5790
5791static struct device_attribute dev_attr_fan_pwm1 =
5792 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5793 fan_pwm1_show, fan_pwm1_store);
5794
5795/* sysfs fan fan1_input ------------------------------------------------ */
5796static ssize_t fan_fan1_input_show(struct device *dev,
5797 struct device_attribute *attr,
5798 char *buf)
5799{
5800 int res;
5801 unsigned int speed;
5802
5803 res = fan_get_speed(&speed);
5804 if (res < 0)
5805 return res;
5806
5807 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5808}
5809
5810static struct device_attribute dev_attr_fan_fan1_input =
5811 __ATTR(fan1_input, S_IRUGO,
5812 fan_fan1_input_show, NULL);
5813
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005814/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005815static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5816 char *buf)
5817{
5818 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5819}
5820
5821static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5822 const char *buf, size_t count)
5823{
5824 unsigned long t;
5825
5826 if (parse_strtoul(buf, 120, &t))
5827 return -EINVAL;
5828
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005829 if (!fan_control_allowed)
5830 return -EPERM;
5831
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005832 fan_watchdog_maxinterval = t;
5833 fan_watchdog_reset();
5834
5835 return count;
5836}
5837
5838static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5839 fan_fan_watchdog_show, fan_fan_watchdog_store);
5840
5841/* --------------------------------------------------------------------- */
5842static struct attribute *fan_attributes[] = {
5843 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5844 &dev_attr_fan_fan1_input.attr,
5845 NULL
5846};
5847
5848static const struct attribute_group fan_attr_group = {
5849 .attrs = fan_attributes,
5850};
5851
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005852static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005853{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005854 int rc;
5855
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005856 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5857
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005858 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005859 fan_status_access_mode = TPACPI_FAN_NONE;
5860 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005861 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005862 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005863 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005864 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005865
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005866 TPACPI_ACPIHANDLE_INIT(fans);
5867 TPACPI_ACPIHANDLE_INIT(gfan);
5868 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005869
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005870 if (gfan_handle) {
5871 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005872 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005873 } else {
5874 /* all other ThinkPads: note that even old-style
5875 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005876 if (likely(acpi_ec_read(fan_status_offset,
5877 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005878 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005879
5880 /* In some ThinkPads, neither the EC nor the ACPI
5881 * DSDT initialize the fan status, and it ends up
5882 * being set to 0x07 when it *could* be either
5883 * 0x07 or 0x80.
5884 *
5885 * Enable for TP-1Y (T43), TP-78 (R51e),
5886 * TP-76 (R52), TP-70 (T43, R52), which are known
5887 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005888 if (fan_control_initial_status == 0x07) {
5889 switch (thinkpad_id.ec_model) {
5890 case 0x5931: /* TP-1Y */
5891 case 0x3837: /* TP-78 */
5892 case 0x3637: /* TP-76 */
5893 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005894 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005895 "fan_init: initial fan status "
5896 "is unknown, assuming it is "
5897 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005898 tp_features.fan_ctrl_status_undef = 1;
5899 ;;
5900 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005901 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005902 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005903 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005904 "ThinkPad ACPI EC access misbehaving, "
5905 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005906 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005907 }
5908 }
5909
5910 if (sfan_handle) {
5911 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005912 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005913 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005914 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005915 } else {
5916 if (!gfan_handle) {
5917 /* gfan without sfan means no fan control */
5918 /* all other models implement TP EC 0x2f control */
5919
5920 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005921 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005922 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005923 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005924 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005925 TPACPI_FAN_CMD_SPEED |
5926 TPACPI_FAN_CMD_LEVEL |
5927 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005928 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005929 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005930 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005931 TPACPI_FAN_CMD_LEVEL |
5932 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005933 }
5934 }
5935 }
5936
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005937 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5938 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5939 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5940 fan_status_access_mode, fan_control_access_mode);
5941
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005942 /* fan control master switch */
5943 if (!fan_control_allowed) {
5944 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5945 fan_control_commands = 0;
5946 dbg_printk(TPACPI_DBG_INIT,
5947 "fan control features disabled by parameter\n");
5948 }
5949
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005950 /* update fan_control_desired_level */
5951 if (fan_status_access_mode != TPACPI_FAN_NONE)
5952 fan_get_status_safe(NULL);
5953
5954 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5955 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005956 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005957 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005958 if (rc < 0)
5959 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005960
5961 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5962 &driver_attr_fan_watchdog);
5963 if (rc < 0) {
5964 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5965 &fan_attr_group);
5966 return rc;
5967 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005968 return 0;
5969 } else
5970 return 1;
5971}
5972
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005973static void fan_exit(void)
5974{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005975 vdbg_printk(TPACPI_DBG_EXIT,
5976 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005977
5978 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005979 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005980 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5981 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005982
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005983 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005984 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005985}
5986
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03005987static void fan_suspend(pm_message_t state)
5988{
5989 if (!fan_control_allowed)
5990 return;
5991
5992 /* Store fan status in cache */
5993 fan_get_status_safe(NULL);
5994 if (tp_features.fan_ctrl_status_undef)
5995 fan_control_desired_level = TP_EC_FAN_AUTO;
5996}
5997
5998static void fan_resume(void)
5999{
6000 u8 saved_fan_level;
6001 u8 current_level = 7;
6002 bool do_set = false;
6003
6004 /* DSDT *always* updates status on resume */
6005 tp_features.fan_ctrl_status_undef = 0;
6006
6007 saved_fan_level = fan_control_desired_level;
6008 if (!fan_control_allowed ||
6009 (fan_get_status_safe(&current_level) < 0))
6010 return;
6011
6012 switch (fan_control_access_mode) {
6013 case TPACPI_FAN_WR_ACPI_SFAN:
6014 do_set = (saved_fan_level > current_level);
6015 break;
6016 case TPACPI_FAN_WR_ACPI_FANS:
6017 case TPACPI_FAN_WR_TPEC:
6018 do_set = ((saved_fan_level & TP_EC_FAN_FULLSPEED) ||
6019 (saved_fan_level == 7 &&
6020 !(current_level & TP_EC_FAN_FULLSPEED)));
6021 break;
6022 default:
6023 return;
6024 }
6025 if (do_set) {
6026 printk(TPACPI_NOTICE
6027 "restoring fan level to 0x%02x\n",
6028 saved_fan_level);
6029 fan_set_level_safe(saved_fan_level);
6030 }
6031}
6032
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006033static int fan_read(char *p)
6034{
6035 int len = 0;
6036 int rc;
6037 u8 status;
6038 unsigned int speed = 0;
6039
6040 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006041 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006042 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006043 rc = fan_get_status_safe(&status);
6044 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006045 return rc;
6046
6047 len += sprintf(p + len, "status:\t\t%s\n"
6048 "level:\t\t%d\n",
6049 (status != 0) ? "enabled" : "disabled", status);
6050 break;
6051
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006052 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006053 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006054 rc = fan_get_status_safe(&status);
6055 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006056 return rc;
6057
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006058 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006059 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006060 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006061 else
6062 /* Return most likely status. In fact, it
6063 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006064 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006065 }
6066
6067 len += sprintf(p + len, "status:\t\t%s\n",
6068 (status != 0) ? "enabled" : "disabled");
6069
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006070 rc = fan_get_speed(&speed);
6071 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006072 return rc;
6073
6074 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6075
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006076 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006077 /* Disengaged mode takes precedence */
6078 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006079 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006080 len += sprintf(p + len, "level:\t\tauto\n");
6081 else
6082 len += sprintf(p + len, "level:\t\t%d\n", status);
6083 break;
6084
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006085 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006086 default:
6087 len += sprintf(p + len, "status:\t\tnot supported\n");
6088 }
6089
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006090 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006091 len += sprintf(p + len, "commands:\tlevel <level>");
6092
6093 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006094 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006095 len += sprintf(p + len, " (<level> is 0-7)\n");
6096 break;
6097
6098 default:
6099 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006100 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006101 break;
6102 }
6103 }
6104
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006105 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006106 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006107 "commands:\twatchdog <timeout> (<timeout> "
6108 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006109
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006110 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006111 len += sprintf(p + len, "commands:\tspeed <speed>"
6112 " (<speed> is 0-65535)\n");
6113
6114 return len;
6115}
6116
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006117static int fan_write_cmd_level(const char *cmd, int *rc)
6118{
6119 int level;
6120
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006121 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006122 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006123 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006124 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006125 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006126 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006127 return 0;
6128
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006129 *rc = fan_set_level_safe(level);
6130 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006131 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006132 "access mode %d", fan_control_access_mode);
6133
6134 return 1;
6135}
6136
6137static int fan_write_cmd_enable(const char *cmd, int *rc)
6138{
6139 if (strlencmp(cmd, "enable") != 0)
6140 return 0;
6141
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006142 *rc = fan_set_enable();
6143 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006144 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006145 "access mode %d", fan_control_access_mode);
6146
6147 return 1;
6148}
6149
6150static int fan_write_cmd_disable(const char *cmd, int *rc)
6151{
6152 if (strlencmp(cmd, "disable") != 0)
6153 return 0;
6154
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006155 *rc = fan_set_disable();
6156 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006157 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006158 "access mode %d", fan_control_access_mode);
6159
6160 return 1;
6161}
6162
6163static int fan_write_cmd_speed(const char *cmd, int *rc)
6164{
6165 int speed;
6166
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006167 /* TODO:
6168 * Support speed <low> <medium> <high> ? */
6169
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006170 if (sscanf(cmd, "speed %d", &speed) != 1)
6171 return 0;
6172
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006173 *rc = fan_set_speed(speed);
6174 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006175 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006176 "access mode %d", fan_control_access_mode);
6177
6178 return 1;
6179}
6180
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006181static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6182{
6183 int interval;
6184
6185 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6186 return 0;
6187
6188 if (interval < 0 || interval > 120)
6189 *rc = -EINVAL;
6190 else
6191 fan_watchdog_maxinterval = interval;
6192
6193 return 1;
6194}
6195
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006196static int fan_write(char *buf)
6197{
6198 char *cmd;
6199 int rc = 0;
6200
6201 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006202 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006203 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006204 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006205 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006206 fan_write_cmd_disable(cmd, &rc) ||
6207 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006208 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006209 fan_write_cmd_speed(cmd, &rc))
6210 )
6211 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006212 else if (!rc)
6213 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006214 }
6215
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006216 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006217}
6218
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006219static struct ibm_struct fan_driver_data = {
6220 .name = "fan",
6221 .read = fan_read,
6222 .write = fan_write,
6223 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006224 .suspend = fan_suspend,
6225 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006226};
6227
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006228/****************************************************************************
6229 ****************************************************************************
6230 *
6231 * Infrastructure
6232 *
6233 ****************************************************************************
6234 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006236/* sysfs name ---------------------------------------------------------- */
6237static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6238 struct device_attribute *attr,
6239 char *buf)
6240{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006241 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006242}
6243
6244static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6245 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6246
6247/* --------------------------------------------------------------------- */
6248
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006249/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03006250static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006251
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006252/*
6253 * Module and infrastructure proble, init and exit handling
6254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006256static int force_load;
6257
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006258#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006259static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006260{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006261 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006262
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006263 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006264}
6265#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6266
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006267static void ibm_exit(struct ibm_struct *ibm)
6268{
6269 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6270
6271 list_del_init(&ibm->all_drivers);
6272
6273 if (ibm->flags.acpi_notify_installed) {
6274 dbg_printk(TPACPI_DBG_EXIT,
6275 "%s: acpi_remove_notify_handler\n", ibm->name);
6276 BUG_ON(!ibm->acpi);
6277 acpi_remove_notify_handler(*ibm->acpi->handle,
6278 ibm->acpi->type,
6279 dispatch_acpi_notify);
6280 ibm->flags.acpi_notify_installed = 0;
6281 ibm->flags.acpi_notify_installed = 0;
6282 }
6283
6284 if (ibm->flags.proc_created) {
6285 dbg_printk(TPACPI_DBG_EXIT,
6286 "%s: remove_proc_entry\n", ibm->name);
6287 remove_proc_entry(ibm->name, proc_dir);
6288 ibm->flags.proc_created = 0;
6289 }
6290
6291 if (ibm->flags.acpi_driver_registered) {
6292 dbg_printk(TPACPI_DBG_EXIT,
6293 "%s: acpi_bus_unregister_driver\n", ibm->name);
6294 BUG_ON(!ibm->acpi);
6295 acpi_bus_unregister_driver(ibm->acpi->driver);
6296 kfree(ibm->acpi->driver);
6297 ibm->acpi->driver = NULL;
6298 ibm->flags.acpi_driver_registered = 0;
6299 }
6300
6301 if (ibm->flags.init_called && ibm->exit) {
6302 ibm->exit();
6303 ibm->flags.init_called = 0;
6304 }
6305
6306 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6307}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006308
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006309static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310{
6311 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006312 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 struct proc_dir_entry *entry;
6314
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006315 BUG_ON(ibm == NULL);
6316
6317 INIT_LIST_HEAD(&ibm->all_drivers);
6318
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006319 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 return 0;
6321
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006322 dbg_printk(TPACPI_DBG_INIT,
6323 "probing for %s\n", ibm->name);
6324
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006325 if (iibm->init) {
6326 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006327 if (ret > 0)
6328 return 0; /* probe failed */
6329 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006331
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006332 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 }
6334
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006335 if (ibm->acpi) {
6336 if (ibm->acpi->hid) {
6337 ret = register_tpacpi_subdriver(ibm);
6338 if (ret)
6339 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006340 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006341
6342 if (ibm->acpi->notify) {
6343 ret = setup_acpi_notify(ibm);
6344 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006345 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006346 ibm->name);
6347 ret = 0;
6348 goto err_out;
6349 }
6350 if (ret < 0)
6351 goto err_out;
6352 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006353 }
6354
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006355 dbg_printk(TPACPI_DBG_INIT,
6356 "%s installed\n", ibm->name);
6357
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006358 if (ibm->read) {
6359 entry = create_proc_entry(ibm->name,
6360 S_IFREG | S_IRUGO | S_IWUSR,
6361 proc_dir);
6362 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006363 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006364 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006365 ret = -ENODEV;
6366 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006367 }
6368 entry->owner = THIS_MODULE;
6369 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006370 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006371 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006372 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006373 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006376 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6377
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006379
6380err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006381 dbg_printk(TPACPI_DBG_INIT,
6382 "%s: at error exit path with result %d\n",
6383 ibm->name, ret);
6384
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006385 ibm_exit(ibm);
6386 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387}
6388
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006389/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006391/* returns 0 - probe ok, or < 0 - probe error.
6392 * Probe ok doesn't mean thinkpad found.
6393 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6394static int __must_check __init get_thinkpad_model_data(
6395 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006396{
Jeff Garzik18552562007-10-03 15:15:40 -04006397 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006398 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006399 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006400
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006401 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006402 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006403
6404 memset(tp, 0, sizeof(*tp));
6405
6406 if (dmi_name_in_vendors("IBM"))
6407 tp->vendor = PCI_VENDOR_ID_IBM;
6408 else if (dmi_name_in_vendors("LENOVO"))
6409 tp->vendor = PCI_VENDOR_ID_LENOVO;
6410 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006411 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006412
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006413 s = dmi_get_system_info(DMI_BIOS_VERSION);
6414 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6415 if (s && !tp->bios_version_str)
6416 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006417 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006418 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006419 tp->bios_model = tp->bios_version_str[0]
6420 | (tp->bios_version_str[1] << 8);
6421
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006422 /*
6423 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6424 * X32 or newer, all Z series; Some models must have an
6425 * up-to-date BIOS or they will not be detected.
6426 *
6427 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6428 */
6429 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006430 if (sscanf(dev->name,
6431 "IBM ThinkPad Embedded Controller -[%17c",
6432 ec_fw_string) == 1) {
6433 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6434 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006435
6436 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006437 if (!tp->ec_version_str)
6438 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006439 tp->ec_model = ec_fw_string[0]
6440 | (ec_fw_string[1] << 8);
6441 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006442 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006443 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006444
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006445 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6446 if (s && !strnicmp(s, "ThinkPad", 8)) {
6447 tp->model_str = kstrdup(s, GFP_KERNEL);
6448 if (!tp->model_str)
6449 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006450 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006451
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006452 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6453 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6454 if (s && !tp->nummodel_str)
6455 return -ENOMEM;
6456
6457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458}
6459
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006460static int __init probe_for_thinkpad(void)
6461{
6462 int is_thinkpad;
6463
6464 if (acpi_disabled)
6465 return -ENODEV;
6466
6467 /*
6468 * Non-ancient models have better DMI tagging, but very old models
6469 * don't.
6470 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006471 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006472
6473 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006474 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006475 if (!ec_handle) {
6476 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006477 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006478 "Not yet supported ThinkPad detected!\n");
6479 return -ENODEV;
6480 }
6481
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006482 /*
6483 * Risks a regression on very old machines, but reduces potential
6484 * false positives a damn great deal
6485 */
6486 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006487 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006488
6489 if (!is_thinkpad && !force_load)
6490 return -ENODEV;
6491
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006492 return 0;
6493}
6494
6495
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006496/* Module init, exit, parameters */
6497
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006498static struct ibm_init_struct ibms_init[] __initdata = {
6499 {
6500 .init = thinkpad_acpi_driver_init,
6501 .data = &thinkpad_acpi_driver_data,
6502 },
6503 {
6504 .init = hotkey_init,
6505 .data = &hotkey_driver_data,
6506 },
6507 {
6508 .init = bluetooth_init,
6509 .data = &bluetooth_driver_data,
6510 },
6511 {
6512 .init = wan_init,
6513 .data = &wan_driver_data,
6514 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006515#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006516 {
6517 .init = video_init,
6518 .data = &video_driver_data,
6519 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006520#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006521 {
6522 .init = light_init,
6523 .data = &light_driver_data,
6524 },
6525#ifdef CONFIG_THINKPAD_ACPI_DOCK
6526 {
6527 .init = dock_init,
6528 .data = &dock_driver_data[0],
6529 },
6530 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03006531 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006532 .data = &dock_driver_data[1],
6533 },
6534#endif
6535#ifdef CONFIG_THINKPAD_ACPI_BAY
6536 {
6537 .init = bay_init,
6538 .data = &bay_driver_data,
6539 },
6540#endif
6541 {
6542 .init = cmos_init,
6543 .data = &cmos_driver_data,
6544 },
6545 {
6546 .init = led_init,
6547 .data = &led_driver_data,
6548 },
6549 {
6550 .init = beep_init,
6551 .data = &beep_driver_data,
6552 },
6553 {
6554 .init = thermal_init,
6555 .data = &thermal_driver_data,
6556 },
6557 {
6558 .data = &ecdump_driver_data,
6559 },
6560 {
6561 .init = brightness_init,
6562 .data = &brightness_driver_data,
6563 },
6564 {
6565 .data = &volume_driver_data,
6566 },
6567 {
6568 .init = fan_init,
6569 .data = &fan_driver_data,
6570 },
6571};
6572
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006573static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6574{
6575 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006576 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006577
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006578 if (!kp || !kp->name || !val)
6579 return -EINVAL;
6580
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006581 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6582 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006583 WARN_ON(ibm == NULL);
6584
6585 if (!ibm || !ibm->name)
6586 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006587
6588 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6589 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006590 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006591 strcpy(ibms_init[i].param, val);
6592 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006593 return 0;
6594 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006595 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006596
6597 return -EINVAL;
6598}
6599
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006600module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006601MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006602 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006603
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006604module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006605MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006606
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006607module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006608MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006609 "Attempts to load the driver even on a "
6610 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006611
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006612module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006613MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006614 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006615
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006616module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006617MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006618 "Selects brightness control strategy: "
6619 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006620
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006621module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006622MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006623 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006624
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006625module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006626MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006627 "used for backwards compatibility with userspace, "
6628 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006629
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006630#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006631 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006632 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006633 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006634
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006635TPACPI_PARAM(hotkey);
6636TPACPI_PARAM(bluetooth);
6637TPACPI_PARAM(video);
6638TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006639#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006640TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006641#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006642#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006643TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006644#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006645TPACPI_PARAM(cmos);
6646TPACPI_PARAM(led);
6647TPACPI_PARAM(beep);
6648TPACPI_PARAM(ecdump);
6649TPACPI_PARAM(brightness);
6650TPACPI_PARAM(volume);
6651TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006652
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006653static void thinkpad_acpi_module_exit(void)
6654{
6655 struct ibm_struct *ibm, *itmp;
6656
6657 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6658
6659 list_for_each_entry_safe_reverse(ibm, itmp,
6660 &tpacpi_all_drivers,
6661 all_drivers) {
6662 ibm_exit(ibm);
6663 }
6664
6665 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6666
6667 if (tpacpi_inputdev) {
6668 if (tp_features.input_device_registered)
6669 input_unregister_device(tpacpi_inputdev);
6670 else
6671 input_free_device(tpacpi_inputdev);
6672 }
6673
6674 if (tpacpi_hwmon)
6675 hwmon_device_unregister(tpacpi_hwmon);
6676
6677 if (tp_features.sensors_pdev_attrs_registered)
6678 device_remove_file(&tpacpi_sensors_pdev->dev,
6679 &dev_attr_thinkpad_acpi_pdev_name);
6680 if (tpacpi_sensors_pdev)
6681 platform_device_unregister(tpacpi_sensors_pdev);
6682 if (tpacpi_pdev)
6683 platform_device_unregister(tpacpi_pdev);
6684
6685 if (tp_features.sensors_pdrv_attrs_registered)
6686 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6687 if (tp_features.platform_drv_attrs_registered)
6688 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6689
6690 if (tp_features.sensors_pdrv_registered)
6691 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6692
6693 if (tp_features.platform_drv_registered)
6694 platform_driver_unregister(&tpacpi_pdriver);
6695
6696 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006697 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006698
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006699 if (tpacpi_wq)
6700 destroy_workqueue(tpacpi_wq);
6701
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006702 kfree(thinkpad_id.bios_version_str);
6703 kfree(thinkpad_id.ec_version_str);
6704 kfree(thinkpad_id.model_str);
6705}
6706
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006707
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006708static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709{
6710 int ret, i;
6711
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006712 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6713
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006714 /* Parameter checking */
6715 if (hotkey_report_mode > 2)
6716 return -EINVAL;
6717
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006718 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006719
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006720 ret = get_thinkpad_model_data(&thinkpad_id);
6721 if (ret) {
6722 printk(TPACPI_ERR
6723 "unable to get DMI data: %d\n", ret);
6724 thinkpad_acpi_module_exit();
6725 return ret;
6726 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006727 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006728 if (ret) {
6729 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006730 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006733 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006734
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006735 TPACPI_ACPIHANDLE_INIT(ecrd);
6736 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006738 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6739 if (!tpacpi_wq) {
6740 thinkpad_acpi_module_exit();
6741 return -ENOMEM;
6742 }
6743
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006744 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006746 printk(TPACPI_ERR
6747 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006748 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 return -ENODEV;
6750 }
6751 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006752
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006753 ret = platform_driver_register(&tpacpi_pdriver);
6754 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006755 printk(TPACPI_ERR
6756 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006757 thinkpad_acpi_module_exit();
6758 return ret;
6759 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006760 tp_features.platform_drv_registered = 1;
6761
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006762 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6763 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006764 printk(TPACPI_ERR
6765 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006766 thinkpad_acpi_module_exit();
6767 return ret;
6768 }
6769 tp_features.sensors_pdrv_registered = 1;
6770
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006771 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006772 if (!ret) {
6773 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006774 ret = tpacpi_create_driver_attributes(
6775 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006776 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006777 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006778 printk(TPACPI_ERR
6779 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006780 thinkpad_acpi_module_exit();
6781 return ret;
6782 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006783 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006784
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006785
6786 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006787 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006788 NULL, 0);
6789 if (IS_ERR(tpacpi_pdev)) {
6790 ret = PTR_ERR(tpacpi_pdev);
6791 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006792 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006793 thinkpad_acpi_module_exit();
6794 return ret;
6795 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006796 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006797 TPACPI_HWMON_DRVR_NAME,
6798 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006799 if (IS_ERR(tpacpi_sensors_pdev)) {
6800 ret = PTR_ERR(tpacpi_sensors_pdev);
6801 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006802 printk(TPACPI_ERR
6803 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006804 thinkpad_acpi_module_exit();
6805 return ret;
6806 }
6807 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6808 &dev_attr_thinkpad_acpi_pdev_name);
6809 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006810 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006811 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006812 thinkpad_acpi_module_exit();
6813 return ret;
6814 }
6815 tp_features.sensors_pdev_attrs_registered = 1;
6816 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006817 if (IS_ERR(tpacpi_hwmon)) {
6818 ret = PTR_ERR(tpacpi_hwmon);
6819 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006820 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006821 thinkpad_acpi_module_exit();
6822 return ret;
6823 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006824 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006825 tpacpi_inputdev = input_allocate_device();
6826 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006827 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006828 thinkpad_acpi_module_exit();
6829 return -ENOMEM;
6830 } else {
6831 /* Prepare input device, but don't register */
6832 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006833 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006834 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006835 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6836 thinkpad_id.vendor :
6837 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006838 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6839 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6840 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006841 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6842 ret = ibm_init(&ibms_init[i]);
6843 if (ret >= 0 && *ibms_init[i].param)
6844 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006846 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 return ret;
6848 }
6849 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006850 ret = input_register_device(tpacpi_inputdev);
6851 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006852 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006853 thinkpad_acpi_module_exit();
6854 return ret;
6855 } else {
6856 tp_features.input_device_registered = 1;
6857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006859 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 return 0;
6861}
6862
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006863/* Please remove this in year 2009 */
6864MODULE_ALIAS("ibm_acpi");
6865
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03006866MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6867
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006868/*
6869 * DMI matching for module autoloading
6870 *
6871 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6872 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6873 *
6874 * Only models listed in thinkwiki will be supported, so add yours
6875 * if it is not there yet.
6876 */
6877#define IBM_BIOS_MODULE_ALIAS(__type) \
6878 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6879
6880/* Non-ancient thinkpads */
6881MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6882MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6883
6884/* Ancient thinkpad BIOSes have to be identified by
6885 * BIOS type or model number, and there are far less
6886 * BIOS types than model numbers... */
6887IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6888IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6889IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6890
6891MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006892MODULE_DESCRIPTION(TPACPI_DESC);
6893MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006894MODULE_LICENSE("GPL");
6895
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006896module_init(thinkpad_acpi_module_init);
6897module_exit(thinkpad_acpi_module_exit);