blob: 17b8281deee5731827c8e0733824dcbf8cf7109b [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
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200162#define TPACPI_DBG_INIT 0x0001
163#define TPACPI_DBG_EXIT 0x0002
164#define dbg_printk(a_dbg_level, format, arg...) \
165 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200166 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
167 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200168#ifdef CONFIG_THINKPAD_ACPI_DEBUG
169#define vdbg_printk(a_dbg_level, format, arg...) \
170 dbg_printk(a_dbg_level, format, ## arg)
171static const char *str_supported(int is_supported);
172#else
173#define vdbg_printk(a_dbg_level, format, arg...)
174#endif
175
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200176#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
177#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
178#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200179
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200180
181/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200182 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200183 */
184
185struct ibm_struct;
186
187struct tp_acpi_drv_struct {
188 const struct acpi_device_id *hid;
189 struct acpi_driver *driver;
190
191 void (*notify) (struct ibm_struct *, u32);
192 acpi_handle *handle;
193 u32 type;
194 struct acpi_device *device;
195};
196
197struct ibm_struct {
198 char *name;
199
200 int (*read) (char *);
201 int (*write) (char *);
202 void (*exit) (void);
203 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200204 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200205
206 struct list_head all_drivers;
207
208 struct tp_acpi_drv_struct *acpi;
209
210 struct {
211 u8 acpi_driver_registered:1;
212 u8 acpi_notify_installed:1;
213 u8 proc_created:1;
214 u8 init_called:1;
215 u8 experimental:1;
216 } flags;
217};
218
219struct ibm_init_struct {
220 char param[32];
221
222 int (*init) (struct ibm_init_struct *);
223 struct ibm_struct *data;
224};
225
226static struct {
227#ifdef CONFIG_THINKPAD_ACPI_BAY
228 u32 bay_status:1;
229 u32 bay_eject:1;
230 u32 bay_status2:1;
231 u32 bay_eject2:1;
232#endif
233 u32 bluetooth:1;
234 u32 hotkey:1;
235 u32 hotkey_mask:1;
236 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200237 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200238 u32 light:1;
239 u32 light_status:1;
240 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300241 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200242 u32 wan:1;
243 u32 fan_ctrl_status_undef:1;
244 u32 input_device_registered:1;
245 u32 platform_drv_registered:1;
246 u32 platform_drv_attrs_registered:1;
247 u32 sensors_pdrv_registered:1;
248 u32 sensors_pdrv_attrs_registered:1;
249 u32 sensors_pdev_attrs_registered:1;
250 u32 hotkey_poll_active:1;
251} tp_features;
252
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300253static struct {
254 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300255 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300256} tp_warned;
257
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200258struct thinkpad_id_data {
259 unsigned int vendor; /* ThinkPad vendor:
260 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
261
262 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
263 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
264
265 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
266 u16 ec_model;
267
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300268 char *model_str; /* ThinkPad T43 */
269 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200270};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200271static struct thinkpad_id_data thinkpad_id;
272
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300273static enum {
274 TPACPI_LIFE_INIT = 0,
275 TPACPI_LIFE_RUNNING,
276 TPACPI_LIFE_EXITING,
277} tpacpi_lifecycle;
278
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200279static int experimental;
280static u32 dbg_level;
281
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300282static struct workqueue_struct *tpacpi_wq;
283
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300284/* Special LED class that can defer work */
285struct tpacpi_led_classdev {
286 struct led_classdev led_classdev;
287 struct work_struct work;
288 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300289 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300290};
291
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300292/****************************************************************************
293 ****************************************************************************
294 *
295 * ACPI Helpers and device model
296 *
297 ****************************************************************************
298 ****************************************************************************/
299
300/*************************************************************************
301 * ACPI basic handles
302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300304static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200306#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 static acpi_handle object##_handle; \
308 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400309 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 static char *object##_paths[] = { paths }
311
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200312TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400313 "\\_SB.PCI.ISA.EC", /* 570 */
314 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
315 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
316 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
317 "\\_SB.PCI0.ICH3.EC0", /* R31 */
318 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300319 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200321TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
322TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200324TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
325 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400326 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
327 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300328 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400329
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200330TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400331 "^HKEY", /* R30, R31 */
332 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300333 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400334
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200335TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
336 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
337 "\\_SB.PCI0.VID0", /* 770e */
338 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
339 "\\_SB.PCI0.AGP.VID", /* all others */
340 ); /* R30, R31 */
341
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400342
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300343/*************************************************************************
344 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200345 */
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static int acpi_evalf(acpi_handle handle,
348 void *res, char *method, char *fmt, ...)
349{
350 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400351 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200352 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400353 struct acpi_buffer result, *resultp;
354 union acpi_object out_obj;
355 acpi_status status;
356 va_list ap;
357 char res_type;
358 int success;
359 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200362 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
364 }
365
366 if (*fmt == 'q') {
367 quiet = 1;
368 fmt++;
369 } else
370 quiet = 0;
371
372 res_type = *(fmt++);
373
374 params.count = 0;
375 params.pointer = &in_objs[0];
376
377 va_start(ap, fmt);
378 while (*fmt) {
379 char c = *(fmt++);
380 switch (c) {
381 case 'd': /* int */
382 in_objs[params.count].integer.value = va_arg(ap, int);
383 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
384 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400385 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200387 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 "with invalid format character '%c'\n", c);
389 return 0;
390 }
391 }
392 va_end(ap);
393
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400394 if (res_type != 'v') {
395 result.length = sizeof(out_obj);
396 result.pointer = &out_obj;
397 resultp = &result;
398 } else
399 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400401 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400404 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (res)
406 *(int *)res = out_obj.integer.value;
407 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
408 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400409 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 success = status == AE_OK;
411 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400412 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200414 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 "with invalid format character '%c'\n", res_type);
416 return 0;
417 }
418
419 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200420 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 method, fmt0, status);
422
423 return success;
424}
425
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200426static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300427{
428 int v;
429
430 if (ecrd_handle) {
431 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
432 return 0;
433 *p = v;
434 } else {
435 if (ec_read(i, p) < 0)
436 return 0;
437 }
438
439 return 1;
440}
441
442static int acpi_ec_write(int i, u8 v)
443{
444 if (ecwr_handle) {
445 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
446 return 0;
447 } else {
448 if (ec_write(i, v) < 0)
449 return 0;
450 }
451
452 return 1;
453}
454
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200455#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300456static int _sta(acpi_handle handle)
457{
458 int status;
459
460 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
461 status = 0;
462
463 return status;
464}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200465#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300466
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300467static int issue_thinkpad_cmos_command(int cmos_cmd)
468{
469 if (!cmos_handle)
470 return -ENXIO;
471
472 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
473 return -EIO;
474
475 return 0;
476}
477
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300478/*************************************************************************
479 * ACPI device model
480 */
481
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200482#define TPACPI_ACPIHANDLE_INIT(object) \
483 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200484 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
485
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300486static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300487 acpi_handle *handle, acpi_handle parent,
488 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300489{
490 int i;
491 acpi_status status;
492
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300493 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
494 name);
495
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300496 for (i = 0; i < num_paths; i++) {
497 status = acpi_get_handle(parent, paths[i], handle);
498 if (ACPI_SUCCESS(status)) {
499 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300500 dbg_printk(TPACPI_DBG_INIT,
501 "Found ACPI handle %s for %s\n",
502 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300503 return;
504 }
505 }
506
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300507 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
508 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300509 *handle = NULL;
510}
511
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300512static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300513{
514 struct ibm_struct *ibm = data;
515
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300516 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
517 return;
518
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300519 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300520 return;
521
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300522 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523}
524
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300525static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300526{
527 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300528 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300529
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300530 BUG_ON(!ibm->acpi);
531
532 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533 return 0;
534
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300535 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300536 "setting up ACPI notify for %s\n", ibm->name);
537
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300538 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
539 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200540 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300541 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300542 return -ENODEV;
543 }
544
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300545 acpi_driver_data(ibm->acpi->device) = ibm;
546 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200547 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300548 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300549
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300550 status = acpi_install_notify_handler(*ibm->acpi->handle,
551 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300552 if (ACPI_FAILURE(status)) {
553 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200554 printk(TPACPI_NOTICE
555 "another device driver is already "
556 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300557 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200558 printk(TPACPI_ERR
559 "acpi_install_notify_handler(%s) failed: %d\n",
560 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300561 }
562 return -ENODEV;
563 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300564 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300565 return 0;
566}
567
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300568static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300569{
570 return 0;
571}
572
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300573static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300574{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300575 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300576
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300577 dbg_printk(TPACPI_DBG_INIT,
578 "registering %s as an ACPI driver\n", ibm->name);
579
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300580 BUG_ON(!ibm->acpi);
581
582 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
583 if (!ibm->acpi->driver) {
Mariusz Kozlowski4f778b92008-10-18 14:23:53 -0300584 printk(TPACPI_ERR
585 "failed to allocate memory for ibm->acpi->driver\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300586 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300587 }
588
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200589 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300590 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200591
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300592 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300593
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300594 rc = acpi_bus_register_driver(ibm->acpi->driver);
595 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200596 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200597 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300598 kfree(ibm->acpi->driver);
599 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300600 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300601 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300602
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300603 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300604}
605
606
607/****************************************************************************
608 ****************************************************************************
609 *
610 * Procfs Helpers
611 *
612 ****************************************************************************
613 ****************************************************************************/
614
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300615static int dispatch_procfs_read(char *page, char **start, off_t off,
616 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300617{
618 struct ibm_struct *ibm = data;
619 int len;
620
621 if (!ibm || !ibm->read)
622 return -EINVAL;
623
624 len = ibm->read(page);
625 if (len < 0)
626 return len;
627
628 if (len <= off + count)
629 *eof = 1;
630 *start = page + off;
631 len -= off;
632 if (len > count)
633 len = count;
634 if (len < 0)
635 len = 0;
636
637 return len;
638}
639
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300640static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200641 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300642 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300643{
644 struct ibm_struct *ibm = data;
645 char *kernbuf;
646 int ret;
647
648 if (!ibm || !ibm->write)
649 return -EINVAL;
650
651 kernbuf = kmalloc(count + 2, GFP_KERNEL);
652 if (!kernbuf)
653 return -ENOMEM;
654
655 if (copy_from_user(kernbuf, userbuf, count)) {
656 kfree(kernbuf);
657 return -EFAULT;
658 }
659
660 kernbuf[count] = 0;
661 strcat(kernbuf, ",");
662 ret = ibm->write(kernbuf);
663 if (ret == 0)
664 ret = count;
665
666 kfree(kernbuf);
667
668 return ret;
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static char *next_cmd(char **cmds)
672{
673 char *start = *cmds;
674 char *end;
675
676 while ((end = strchr(start, ',')) && end == start)
677 start = end + 1;
678
679 if (!end)
680 return NULL;
681
682 *end = 0;
683 *cmds = end + 1;
684 return start;
685}
686
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300687
688/****************************************************************************
689 ****************************************************************************
690 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300691 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300692 *
693 ****************************************************************************
694 ****************************************************************************/
695
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300696static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300697static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700698static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300699static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300700static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200701static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300702
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200703static int tpacpi_suspend_handler(struct platform_device *pdev,
704 pm_message_t state)
705{
706 struct ibm_struct *ibm, *itmp;
707
708 list_for_each_entry_safe(ibm, itmp,
709 &tpacpi_all_drivers,
710 all_drivers) {
711 if (ibm->suspend)
712 (ibm->suspend)(state);
713 }
714
715 return 0;
716}
717
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300718static int tpacpi_resume_handler(struct platform_device *pdev)
719{
720 struct ibm_struct *ibm, *itmp;
721
722 list_for_each_entry_safe(ibm, itmp,
723 &tpacpi_all_drivers,
724 all_drivers) {
725 if (ibm->resume)
726 (ibm->resume)();
727 }
728
729 return 0;
730}
731
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300732static struct platform_driver tpacpi_pdriver = {
733 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200734 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300735 .owner = THIS_MODULE,
736 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200737 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300738 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300739};
740
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300741static struct platform_driver tpacpi_hwmon_pdriver = {
742 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200743 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300744 .owner = THIS_MODULE,
745 },
746};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300747
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300748/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300749 * sysfs support helpers
750 */
751
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200752struct attribute_set {
753 unsigned int members, max_members;
754 struct attribute_group group;
755};
756
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300757struct attribute_set_obj {
758 struct attribute_set s;
759 struct attribute *a;
760} __attribute__((packed));
761
762static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200763 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300764{
765 struct attribute_set_obj *sobj;
766
767 if (max_members == 0)
768 return NULL;
769
770 /* Allocates space for implicit NULL at the end too */
771 sobj = kzalloc(sizeof(struct attribute_set_obj) +
772 max_members * sizeof(struct attribute *),
773 GFP_KERNEL);
774 if (!sobj)
775 return NULL;
776 sobj->s.max_members = max_members;
777 sobj->s.group.attrs = &sobj->a;
778 sobj->s.group.name = name;
779
780 return &sobj->s;
781}
782
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200783#define destroy_attr_set(_set) \
784 kfree(_set);
785
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300786/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200787static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300788{
789 if (!s || !attr)
790 return -EINVAL;
791
792 if (s->members >= s->max_members)
793 return -ENOMEM;
794
795 s->group.attrs[s->members] = attr;
796 s->members++;
797
798 return 0;
799}
800
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200801static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300802 struct attribute **attr,
803 unsigned int count)
804{
805 int i, res;
806
807 for (i = 0; i < count; i++) {
808 res = add_to_attr_set(s, attr[i]);
809 if (res)
810 return res;
811 }
812
813 return 0;
814}
815
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200816static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300817{
818 sysfs_remove_group(kobj, &s->group);
819 destroy_attr_set(s);
820}
821
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200822#define register_attr_set_with_sysfs(_attr_set, _kobj) \
823 sysfs_create_group(_kobj, &_attr_set->group)
824
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300825static int parse_strtoul(const char *buf,
826 unsigned long max, unsigned long *value)
827{
828 char *endp;
829
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300830 while (*buf && isspace(*buf))
831 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300832 *value = simple_strtoul(buf, &endp, 0);
833 while (*endp && isspace(*endp))
834 endp++;
835 if (*endp || *value > max)
836 return -EINVAL;
837
838 return 0;
839}
840
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -0300841static void tpacpi_disable_brightness_delay(void)
842{
843 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
844 printk(TPACPI_NOTICE
845 "ACPI backlight control delay disabled\n");
846}
847
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300848static int __init tpacpi_query_bcl_levels(acpi_handle handle)
849{
850 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
851 union acpi_object *obj;
852 int rc;
853
854 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
855 obj = (union acpi_object *)buffer.pointer;
856 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
857 printk(TPACPI_ERR "Unknown _BCL data, "
858 "please report this to %s\n", TPACPI_MAIL);
859 rc = 0;
860 } else {
861 rc = obj->package.count;
862 }
863 } else {
864 return 0;
865 }
866
867 kfree(buffer.pointer);
868 return rc;
869}
870
871static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
872 u32 lvl, void *context, void **rv)
873{
874 char name[ACPI_PATH_SEGMENT_LENGTH];
875 struct acpi_buffer buffer = { sizeof(name), &name };
876
877 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
878 !strncmp("_BCL", name, sizeof(name) - 1)) {
879 BUG_ON(!rv || !*rv);
880 **(int **)rv = tpacpi_query_bcl_levels(handle);
881 return AE_CTRL_TERMINATE;
882 } else {
883 return AE_OK;
884 }
885}
886
887/*
888 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
889 */
890static int __init tpacpi_check_std_acpi_brightness_support(void)
891{
892 int status;
893 int bcl_levels = 0;
894 void *bcl_ptr = &bcl_levels;
895
896 if (!vid_handle) {
897 TPACPI_ACPIHANDLE_INIT(vid);
898 }
899 if (!vid_handle)
900 return 0;
901
902 /*
903 * Search for a _BCL method, and execute it. This is safe on all
904 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
905 * BIOS in ACPI backlight control mode. We do NOT have to care
906 * about calling the _BCL method in an enabled video device, any
907 * will do for our purposes.
908 */
909
910 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
911 tpacpi_acpi_walk_find_bcl, NULL,
912 &bcl_ptr);
913
914 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
915 tp_features.bright_acpimode = 1;
916 return (bcl_levels - 2);
917 }
918
919 return 0;
920}
921
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -0300922static int __init tpacpi_new_rfkill(const unsigned int id,
923 struct rfkill **rfk,
924 const enum rfkill_type rfktype,
925 const char *name,
926 int (*toggle_radio)(void *, enum rfkill_state),
927 int (*get_state)(void *, enum rfkill_state *))
928{
929 int res;
930 enum rfkill_state initial_state;
931
932 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
933 if (!*rfk) {
934 printk(TPACPI_ERR
935 "failed to allocate memory for rfkill class\n");
936 return -ENOMEM;
937 }
938
939 (*rfk)->name = name;
940 (*rfk)->get_state = get_state;
941 (*rfk)->toggle_radio = toggle_radio;
942
943 if (!get_state(NULL, &initial_state))
944 (*rfk)->state = initial_state;
945
946 res = rfkill_register(*rfk);
947 if (res < 0) {
948 printk(TPACPI_ERR
949 "failed to register %s rfkill switch: %d\n",
950 name, res);
951 rfkill_free(*rfk);
952 *rfk = NULL;
953 return res;
954 }
955
956 return 0;
957}
958
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200959/*************************************************************************
960 * thinkpad-acpi driver attributes
961 */
962
963/* interface_version --------------------------------------------------- */
964static ssize_t tpacpi_driver_interface_version_show(
965 struct device_driver *drv,
966 char *buf)
967{
968 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
969}
970
971static DRIVER_ATTR(interface_version, S_IRUGO,
972 tpacpi_driver_interface_version_show, NULL);
973
974/* debug_level --------------------------------------------------------- */
975static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
976 char *buf)
977{
978 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
979}
980
981static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
982 const char *buf, size_t count)
983{
984 unsigned long t;
985
986 if (parse_strtoul(buf, 0xffff, &t))
987 return -EINVAL;
988
989 dbg_level = t;
990
991 return count;
992}
993
994static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
995 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
996
997/* version ------------------------------------------------------------- */
998static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
999 char *buf)
1000{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001001 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1002 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001003}
1004
1005static DRIVER_ATTR(version, S_IRUGO,
1006 tpacpi_driver_version_show, NULL);
1007
1008/* --------------------------------------------------------------------- */
1009
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001010static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001011 &driver_attr_debug_level, &driver_attr_version,
1012 &driver_attr_interface_version,
1013};
1014
1015static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1016{
1017 int i, res;
1018
1019 i = 0;
1020 res = 0;
1021 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1022 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1023 i++;
1024 }
1025
1026 return res;
1027}
1028
1029static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1030{
1031 int i;
1032
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001033 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001034 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1035}
1036
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03001037/****************************************************************************
1038 ****************************************************************************
1039 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001040 * Subdrivers
1041 *
1042 ****************************************************************************
1043 ****************************************************************************/
1044
1045/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -03001046 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001047 */
1048
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001049static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001051 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1052 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001054 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001055 (thinkpad_id.bios_version_str) ?
1056 thinkpad_id.bios_version_str : "unknown",
1057 (thinkpad_id.ec_version_str) ?
1058 thinkpad_id.ec_version_str : "unknown");
1059
1060 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001061 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001062 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1063 "IBM" : ((thinkpad_id.vendor ==
1064 PCI_VENDOR_ID_LENOVO) ?
1065 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001066 thinkpad_id.model_str,
1067 (thinkpad_id.nummodel_str) ?
1068 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return 0;
1071}
1072
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001073static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 int len = 0;
1076
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001077 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1078 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 return len;
1081}
1082
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001083static struct ibm_struct thinkpad_acpi_driver_data = {
1084 .name = "driver",
1085 .read = thinkpad_acpi_driver_read,
1086};
1087
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001088/*************************************************************************
1089 * Hotkey subdriver
1090 */
1091
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001092enum { /* hot key scan codes (derived from ACPI DSDT) */
1093 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1094 TP_ACPI_HOTKEYSCAN_FNF2,
1095 TP_ACPI_HOTKEYSCAN_FNF3,
1096 TP_ACPI_HOTKEYSCAN_FNF4,
1097 TP_ACPI_HOTKEYSCAN_FNF5,
1098 TP_ACPI_HOTKEYSCAN_FNF6,
1099 TP_ACPI_HOTKEYSCAN_FNF7,
1100 TP_ACPI_HOTKEYSCAN_FNF8,
1101 TP_ACPI_HOTKEYSCAN_FNF9,
1102 TP_ACPI_HOTKEYSCAN_FNF10,
1103 TP_ACPI_HOTKEYSCAN_FNF11,
1104 TP_ACPI_HOTKEYSCAN_FNF12,
1105 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1106 TP_ACPI_HOTKEYSCAN_FNINSERT,
1107 TP_ACPI_HOTKEYSCAN_FNDELETE,
1108 TP_ACPI_HOTKEYSCAN_FNHOME,
1109 TP_ACPI_HOTKEYSCAN_FNEND,
1110 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1111 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1112 TP_ACPI_HOTKEYSCAN_FNSPACE,
1113 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1114 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1115 TP_ACPI_HOTKEYSCAN_MUTE,
1116 TP_ACPI_HOTKEYSCAN_THINKPAD,
1117};
1118
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001119enum { /* Keys available through NVRAM polling */
1120 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1121 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1122};
1123
1124enum { /* Positions of some of the keys in hotkey masks */
1125 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1126 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1127 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1128 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1129 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1130 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1131 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1132 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1133 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1134 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1135 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1136};
1137
1138enum { /* NVRAM to ACPI HKEY group map */
1139 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1140 TP_ACPI_HKEY_ZOOM_MASK |
1141 TP_ACPI_HKEY_DISPSWTCH_MASK |
1142 TP_ACPI_HKEY_HIBERNATE_MASK,
1143 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1144 TP_ACPI_HKEY_BRGHTDWN_MASK,
1145 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1146 TP_ACPI_HKEY_VOLDWN_MASK |
1147 TP_ACPI_HKEY_MUTE_MASK,
1148};
1149
1150#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1151struct tp_nvram_state {
1152 u16 thinkpad_toggle:1;
1153 u16 zoom_toggle:1;
1154 u16 display_toggle:1;
1155 u16 thinklight_toggle:1;
1156 u16 hibernate_toggle:1;
1157 u16 displayexp_toggle:1;
1158 u16 display_state:1;
1159 u16 brightness_toggle:1;
1160 u16 volume_toggle:1;
1161 u16 mute:1;
1162
1163 u8 brightness_level;
1164 u8 volume_level;
1165};
1166
1167static struct task_struct *tpacpi_hotkey_task;
1168static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1169static int hotkey_poll_freq = 10; /* Hz */
1170static struct mutex hotkey_thread_mutex;
1171static struct mutex hotkey_thread_data_mutex;
1172static unsigned int hotkey_config_change;
1173
1174#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1175
1176#define hotkey_source_mask 0U
1177
1178#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1179
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001180static struct mutex hotkey_mutex;
1181
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001182static enum { /* Reasons for waking up */
1183 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1184 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1185 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1186} hotkey_wakeup_reason;
1187
1188static int hotkey_autosleep_ack;
1189
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001190static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001191static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001192static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001193static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001194static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001195
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001196static unsigned int hotkey_report_mode;
1197
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001198static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001199
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001200static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001201
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001202#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1203#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001204 do { \
1205 mutex_lock(&hotkey_thread_data_mutex); \
1206 hotkey_config_change++; \
1207 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001208#define HOTKEY_CONFIG_CRITICAL_END \
1209 mutex_unlock(&hotkey_thread_data_mutex);
1210#else
1211#define HOTKEY_CONFIG_CRITICAL_START
1212#define HOTKEY_CONFIG_CRITICAL_END
1213#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1214
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001215/* HKEY.MHKG() return bits */
1216#define TP_HOTKEY_TABLET_MASK (1 << 3)
1217
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001218static int hotkey_get_wlsw(int *status)
1219{
1220 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1221 return -EIO;
1222 return 0;
1223}
1224
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001225static int hotkey_get_tablet_mode(int *status)
1226{
1227 int s;
1228
1229 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1230 return -EIO;
1231
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001232 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1233 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001234}
1235
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001236/*
1237 * Call with hotkey_mutex held
1238 */
1239static int hotkey_mask_get(void)
1240{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001241 u32 m = 0;
1242
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001243 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001244 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001245 return -EIO;
1246 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001247 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001248
1249 return 0;
1250}
1251
1252/*
1253 * Call with hotkey_mutex held
1254 */
1255static int hotkey_mask_set(u32 mask)
1256{
1257 int i;
1258 int rc = 0;
1259
1260 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001261 if (!tp_warned.hotkey_mask_ff &&
1262 (mask == 0xffff || mask == 0xffffff ||
1263 mask == 0xffffffff)) {
1264 tp_warned.hotkey_mask_ff = 1;
1265 printk(TPACPI_NOTICE
1266 "setting the hotkey mask to 0x%08x is likely "
1267 "not the best way to go about it\n", mask);
1268 printk(TPACPI_NOTICE
1269 "please consider using the driver defaults, "
1270 "and refer to up-to-date thinkpad-acpi "
1271 "documentation\n");
1272 }
1273
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001274 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001275 for (i = 0; i < 32; i++) {
1276 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001277 /* enable in firmware mask only keys not in NVRAM
1278 * mode, but enable the key in the cached hotkey_mask
1279 * regardless of mode, or the key will end up
1280 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001281 if (!acpi_evalf(hkey_handle,
1282 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001283 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001284 rc = -EIO;
1285 break;
1286 } else {
1287 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1288 }
1289 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001290 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001291
1292 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001293 if (!hotkey_mask_get() && !rc &&
1294 (hotkey_mask & ~hotkey_source_mask) !=
1295 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001296 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001297 "requested hot key mask 0x%08x, but "
1298 "firmware forced it to 0x%08x\n",
1299 mask, hotkey_mask);
1300 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001301 } else {
1302#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1303 HOTKEY_CONFIG_CRITICAL_START
1304 hotkey_mask = mask & hotkey_source_mask;
1305 HOTKEY_CONFIG_CRITICAL_END
1306 hotkey_mask_get();
1307 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001308 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001309 "requested hot key mask 0x%08x, "
1310 "forced to 0x%08x (NVRAM poll mask is "
1311 "0x%08x): no firmware mask support\n",
1312 mask, hotkey_mask, hotkey_source_mask);
1313 }
1314#else
1315 hotkey_mask_get();
1316 rc = -ENXIO;
1317#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001318 }
1319
1320 return rc;
1321}
1322
1323static int hotkey_status_get(int *status)
1324{
1325 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1326 return -EIO;
1327
1328 return 0;
1329}
1330
1331static int hotkey_status_set(int status)
1332{
1333 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1334 return -EIO;
1335
1336 return 0;
1337}
1338
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001339static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001340{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001341 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001342
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001343 if (tp_features.hotkey_tablet &&
1344 !hotkey_get_tablet_mode(&state)) {
1345 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001346
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001347 input_report_switch(tpacpi_inputdev,
1348 SW_TABLET_MODE, !!state);
1349 input_sync(tpacpi_inputdev);
1350
1351 mutex_unlock(&tpacpi_inputdev_send_mutex);
1352 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001353}
1354
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001355static void tpacpi_input_send_key(unsigned int scancode)
1356{
1357 unsigned int keycode;
1358
1359 keycode = hotkey_keycode_map[scancode];
1360
1361 if (keycode != KEY_RESERVED) {
1362 mutex_lock(&tpacpi_inputdev_send_mutex);
1363
1364 input_report_key(tpacpi_inputdev, keycode, 1);
1365 if (keycode == KEY_UNKNOWN)
1366 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1367 scancode);
1368 input_sync(tpacpi_inputdev);
1369
1370 input_report_key(tpacpi_inputdev, keycode, 0);
1371 if (keycode == KEY_UNKNOWN)
1372 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1373 scancode);
1374 input_sync(tpacpi_inputdev);
1375
1376 mutex_unlock(&tpacpi_inputdev_send_mutex);
1377 }
1378}
1379
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001380#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1381static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1382
1383static void tpacpi_hotkey_send_key(unsigned int scancode)
1384{
1385 tpacpi_input_send_key(scancode);
1386 if (hotkey_report_mode < 2) {
1387 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1388 0x80, 0x1001 + scancode);
1389 }
1390}
1391
1392static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1393{
1394 u8 d;
1395
1396 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1397 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1398 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1399 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1400 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1401 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1402 }
1403 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1404 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1405 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1406 }
1407 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1408 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1409 n->displayexp_toggle =
1410 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1411 }
1412 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1413 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1414 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1415 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1416 n->brightness_toggle =
1417 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1418 }
1419 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1420 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1421 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1422 >> TP_NVRAM_POS_LEVEL_VOLUME;
1423 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1424 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1425 }
1426}
1427
1428#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001429 do { \
1430 if ((mask & (1 << __scancode)) && \
1431 oldn->__member != newn->__member) \
1432 tpacpi_hotkey_send_key(__scancode); \
1433 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001434
1435#define TPACPI_MAY_SEND_KEY(__scancode) \
1436 do { if (mask & (1 << __scancode)) \
1437 tpacpi_hotkey_send_key(__scancode); } while (0)
1438
1439static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001440 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001441 u32 mask)
1442{
1443 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1444 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1445 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1446 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1447
1448 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1449
1450 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1451
1452 /* handle volume */
1453 if (oldn->volume_toggle != newn->volume_toggle) {
1454 if (oldn->mute != newn->mute) {
1455 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1456 }
1457 if (oldn->volume_level > newn->volume_level) {
1458 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1459 } else if (oldn->volume_level < newn->volume_level) {
1460 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1461 } else if (oldn->mute == newn->mute) {
1462 /* repeated key presses that didn't change state */
1463 if (newn->mute) {
1464 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1465 } else if (newn->volume_level != 0) {
1466 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1467 } else {
1468 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1469 }
1470 }
1471 }
1472
1473 /* handle brightness */
1474 if (oldn->brightness_toggle != newn->brightness_toggle) {
1475 if (oldn->brightness_level < newn->brightness_level) {
1476 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1477 } else if (oldn->brightness_level > newn->brightness_level) {
1478 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1479 } else {
1480 /* repeated key presses that didn't change state */
1481 if (newn->brightness_level != 0) {
1482 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1483 } else {
1484 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1485 }
1486 }
1487 }
1488}
1489
1490#undef TPACPI_COMPARE_KEY
1491#undef TPACPI_MAY_SEND_KEY
1492
1493static int hotkey_kthread(void *data)
1494{
1495 struct tp_nvram_state s[2];
1496 u32 mask;
1497 unsigned int si, so;
1498 unsigned long t;
1499 unsigned int change_detector, must_reset;
1500
1501 mutex_lock(&hotkey_thread_mutex);
1502
1503 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1504 goto exit;
1505
1506 set_freezable();
1507
1508 so = 0;
1509 si = 1;
1510 t = 0;
1511
1512 /* Initial state for compares */
1513 mutex_lock(&hotkey_thread_data_mutex);
1514 change_detector = hotkey_config_change;
1515 mask = hotkey_source_mask & hotkey_mask;
1516 mutex_unlock(&hotkey_thread_data_mutex);
1517 hotkey_read_nvram(&s[so], mask);
1518
1519 while (!kthread_should_stop() && hotkey_poll_freq) {
1520 if (t == 0)
1521 t = 1000/hotkey_poll_freq;
1522 t = msleep_interruptible(t);
1523 if (unlikely(kthread_should_stop()))
1524 break;
1525 must_reset = try_to_freeze();
1526 if (t > 0 && !must_reset)
1527 continue;
1528
1529 mutex_lock(&hotkey_thread_data_mutex);
1530 if (must_reset || hotkey_config_change != change_detector) {
1531 /* forget old state on thaw or config change */
1532 si = so;
1533 t = 0;
1534 change_detector = hotkey_config_change;
1535 }
1536 mask = hotkey_source_mask & hotkey_mask;
1537 mutex_unlock(&hotkey_thread_data_mutex);
1538
1539 if (likely(mask)) {
1540 hotkey_read_nvram(&s[si], mask);
1541 if (likely(si != so)) {
1542 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001543 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001544 }
1545 }
1546
1547 so = si;
1548 si ^= 1;
1549 }
1550
1551exit:
1552 mutex_unlock(&hotkey_thread_mutex);
1553 return 0;
1554}
1555
1556static void hotkey_poll_stop_sync(void)
1557{
1558 if (tpacpi_hotkey_task) {
1559 if (frozen(tpacpi_hotkey_task) ||
1560 freezing(tpacpi_hotkey_task))
1561 thaw_process(tpacpi_hotkey_task);
1562
1563 kthread_stop(tpacpi_hotkey_task);
1564 tpacpi_hotkey_task = NULL;
1565 mutex_lock(&hotkey_thread_mutex);
1566 /* at this point, the thread did exit */
1567 mutex_unlock(&hotkey_thread_mutex);
1568 }
1569}
1570
1571/* call with hotkey_mutex held */
1572static void hotkey_poll_setup(int may_warn)
1573{
1574 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1575 hotkey_poll_freq > 0 &&
1576 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1577 if (!tpacpi_hotkey_task) {
1578 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001579 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001580 if (IS_ERR(tpacpi_hotkey_task)) {
1581 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001582 printk(TPACPI_ERR
1583 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001584 "for hotkey polling\n");
1585 }
1586 }
1587 } else {
1588 hotkey_poll_stop_sync();
1589 if (may_warn &&
1590 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001591 printk(TPACPI_NOTICE
1592 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001593 "which is currently disabled\n",
1594 hotkey_source_mask);
1595 }
1596 }
1597}
1598
1599static void hotkey_poll_setup_safe(int may_warn)
1600{
1601 mutex_lock(&hotkey_mutex);
1602 hotkey_poll_setup(may_warn);
1603 mutex_unlock(&hotkey_mutex);
1604}
1605
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001606#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1607
1608static void hotkey_poll_setup_safe(int __unused)
1609{
1610}
1611
1612#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1613
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001614static int hotkey_inputdev_open(struct input_dev *dev)
1615{
1616 switch (tpacpi_lifecycle) {
1617 case TPACPI_LIFE_INIT:
1618 /*
1619 * hotkey_init will call hotkey_poll_setup_safe
1620 * at the appropriate moment
1621 */
1622 return 0;
1623 case TPACPI_LIFE_EXITING:
1624 return -EBUSY;
1625 case TPACPI_LIFE_RUNNING:
1626 hotkey_poll_setup_safe(0);
1627 return 0;
1628 }
1629
1630 /* Should only happen if tpacpi_lifecycle is corrupt */
1631 BUG();
1632 return -EBUSY;
1633}
1634
1635static void hotkey_inputdev_close(struct input_dev *dev)
1636{
1637 /* disable hotkey polling when possible */
1638 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1639 hotkey_poll_setup_safe(0);
1640}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001641
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001642/* sysfs hotkey enable ------------------------------------------------- */
1643static ssize_t hotkey_enable_show(struct device *dev,
1644 struct device_attribute *attr,
1645 char *buf)
1646{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001647 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001648
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001649 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001650 if (res)
1651 return res;
1652
1653 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1654}
1655
1656static ssize_t hotkey_enable_store(struct device *dev,
1657 struct device_attribute *attr,
1658 const char *buf, size_t count)
1659{
1660 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001661 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001662
1663 if (parse_strtoul(buf, 1, &t))
1664 return -EINVAL;
1665
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001666 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001667
1668 return (res) ? res : count;
1669}
1670
1671static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001672 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001673 hotkey_enable_show, hotkey_enable_store);
1674
1675/* sysfs hotkey mask --------------------------------------------------- */
1676static ssize_t hotkey_mask_show(struct device *dev,
1677 struct device_attribute *attr,
1678 char *buf)
1679{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001680 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001681
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001682 if (mutex_lock_interruptible(&hotkey_mutex))
1683 return -ERESTARTSYS;
1684 res = hotkey_mask_get();
1685 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001686
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001687 return (res)?
1688 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001689}
1690
1691static ssize_t hotkey_mask_store(struct device *dev,
1692 struct device_attribute *attr,
1693 const char *buf, size_t count)
1694{
1695 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001696 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001697
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001698 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001699 return -EINVAL;
1700
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001701 if (mutex_lock_interruptible(&hotkey_mutex))
1702 return -ERESTARTSYS;
1703
1704 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001705
1706#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1707 hotkey_poll_setup(1);
1708#endif
1709
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001710 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001711
1712 return (res) ? res : count;
1713}
1714
1715static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001716 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001717 hotkey_mask_show, hotkey_mask_store);
1718
1719/* sysfs hotkey bios_enabled ------------------------------------------- */
1720static ssize_t hotkey_bios_enabled_show(struct device *dev,
1721 struct device_attribute *attr,
1722 char *buf)
1723{
1724 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1725}
1726
1727static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001728 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001729
1730/* sysfs hotkey bios_mask ---------------------------------------------- */
1731static ssize_t hotkey_bios_mask_show(struct device *dev,
1732 struct device_attribute *attr,
1733 char *buf)
1734{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001735 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001736}
1737
1738static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001739 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001740
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001741/* sysfs hotkey all_mask ----------------------------------------------- */
1742static ssize_t hotkey_all_mask_show(struct device *dev,
1743 struct device_attribute *attr,
1744 char *buf)
1745{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001746 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1747 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001748}
1749
1750static struct device_attribute dev_attr_hotkey_all_mask =
1751 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1752
1753/* sysfs hotkey recommended_mask --------------------------------------- */
1754static ssize_t hotkey_recommended_mask_show(struct device *dev,
1755 struct device_attribute *attr,
1756 char *buf)
1757{
1758 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001759 (hotkey_all_mask | hotkey_source_mask)
1760 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001761}
1762
1763static struct device_attribute dev_attr_hotkey_recommended_mask =
1764 __ATTR(hotkey_recommended_mask, S_IRUGO,
1765 hotkey_recommended_mask_show, NULL);
1766
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001767#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1768
1769/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1770static ssize_t hotkey_source_mask_show(struct device *dev,
1771 struct device_attribute *attr,
1772 char *buf)
1773{
1774 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1775}
1776
1777static ssize_t hotkey_source_mask_store(struct device *dev,
1778 struct device_attribute *attr,
1779 const char *buf, size_t count)
1780{
1781 unsigned long t;
1782
1783 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1784 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1785 return -EINVAL;
1786
1787 if (mutex_lock_interruptible(&hotkey_mutex))
1788 return -ERESTARTSYS;
1789
1790 HOTKEY_CONFIG_CRITICAL_START
1791 hotkey_source_mask = t;
1792 HOTKEY_CONFIG_CRITICAL_END
1793
1794 hotkey_poll_setup(1);
1795
1796 mutex_unlock(&hotkey_mutex);
1797
1798 return count;
1799}
1800
1801static struct device_attribute dev_attr_hotkey_source_mask =
1802 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1803 hotkey_source_mask_show, hotkey_source_mask_store);
1804
1805/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1806static ssize_t hotkey_poll_freq_show(struct device *dev,
1807 struct device_attribute *attr,
1808 char *buf)
1809{
1810 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1811}
1812
1813static ssize_t hotkey_poll_freq_store(struct device *dev,
1814 struct device_attribute *attr,
1815 const char *buf, size_t count)
1816{
1817 unsigned long t;
1818
1819 if (parse_strtoul(buf, 25, &t))
1820 return -EINVAL;
1821
1822 if (mutex_lock_interruptible(&hotkey_mutex))
1823 return -ERESTARTSYS;
1824
1825 hotkey_poll_freq = t;
1826
1827 hotkey_poll_setup(1);
1828 mutex_unlock(&hotkey_mutex);
1829
1830 return count;
1831}
1832
1833static struct device_attribute dev_attr_hotkey_poll_freq =
1834 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1835 hotkey_poll_freq_show, hotkey_poll_freq_store);
1836
1837#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1838
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001839/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001840static ssize_t hotkey_radio_sw_show(struct device *dev,
1841 struct device_attribute *attr,
1842 char *buf)
1843{
1844 int res, s;
1845 res = hotkey_get_wlsw(&s);
1846 if (res < 0)
1847 return res;
1848
1849 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1850}
1851
1852static struct device_attribute dev_attr_hotkey_radio_sw =
1853 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1854
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001855static void hotkey_radio_sw_notify_change(void)
1856{
1857 if (tp_features.hotkey_wlsw)
1858 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1859 "hotkey_radio_sw");
1860}
1861
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001862/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1863static ssize_t hotkey_tablet_mode_show(struct device *dev,
1864 struct device_attribute *attr,
1865 char *buf)
1866{
1867 int res, s;
1868 res = hotkey_get_tablet_mode(&s);
1869 if (res < 0)
1870 return res;
1871
1872 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1873}
1874
1875static struct device_attribute dev_attr_hotkey_tablet_mode =
1876 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1877
1878static void hotkey_tablet_mode_notify_change(void)
1879{
1880 if (tp_features.hotkey_tablet)
1881 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1882 "hotkey_tablet_mode");
1883}
1884
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001885/* sysfs hotkey report_mode -------------------------------------------- */
1886static ssize_t hotkey_report_mode_show(struct device *dev,
1887 struct device_attribute *attr,
1888 char *buf)
1889{
1890 return snprintf(buf, PAGE_SIZE, "%d\n",
1891 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1892}
1893
1894static struct device_attribute dev_attr_hotkey_report_mode =
1895 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1896
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001897/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001898static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1899 struct device_attribute *attr,
1900 char *buf)
1901{
1902 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1903}
1904
1905static struct device_attribute dev_attr_hotkey_wakeup_reason =
1906 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1907
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001908static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001909{
1910 if (tp_features.hotkey_mask)
1911 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1912 "wakeup_reason");
1913}
1914
1915/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001916static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1917 struct device_attribute *attr,
1918 char *buf)
1919{
1920 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1921}
1922
1923static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1924 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1925 hotkey_wakeup_hotunplug_complete_show, NULL);
1926
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001927static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001928{
1929 if (tp_features.hotkey_mask)
1930 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1931 "wakeup_hotunplug_complete");
1932}
1933
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001934/* --------------------------------------------------------------------- */
1935
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001936static struct attribute *hotkey_attributes[] __initdata = {
1937 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001938 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001939 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001940#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1941 &dev_attr_hotkey_mask.attr,
1942 &dev_attr_hotkey_all_mask.attr,
1943 &dev_attr_hotkey_recommended_mask.attr,
1944 &dev_attr_hotkey_source_mask.attr,
1945 &dev_attr_hotkey_poll_freq.attr,
1946#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001947};
1948
1949static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001950 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001951#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1952 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001953 &dev_attr_hotkey_all_mask.attr,
1954 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001955#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001956 &dev_attr_hotkey_wakeup_reason.attr,
1957 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001958};
1959
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001960static void bluetooth_update_rfk(void);
1961static void wan_update_rfk(void);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03001962static void tpacpi_send_radiosw_update(void)
1963{
1964 int wlsw;
1965
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03001966 /* Sync these BEFORE sending any rfkill events */
1967 if (tp_features.bluetooth)
1968 bluetooth_update_rfk();
1969 if (tp_features.wan)
1970 wan_update_rfk();
1971
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03001972 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1973 mutex_lock(&tpacpi_inputdev_send_mutex);
1974
1975 input_report_switch(tpacpi_inputdev,
1976 SW_RFKILL_ALL, !!wlsw);
1977 input_sync(tpacpi_inputdev);
1978
1979 mutex_unlock(&tpacpi_inputdev_send_mutex);
1980 }
1981 hotkey_radio_sw_notify_change();
1982}
1983
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03001984static void hotkey_exit(void)
1985{
1986#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1987 hotkey_poll_stop_sync();
1988#endif
1989
1990 if (hotkey_dev_attributes)
1991 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1992
1993 kfree(hotkey_keycode_map);
1994
1995 if (tp_features.hotkey) {
1996 dbg_printk(TPACPI_DBG_EXIT,
1997 "restoring original hot key mask\n");
1998 /* no short-circuit boolean operator below! */
1999 if ((hotkey_mask_set(hotkey_orig_mask) |
2000 hotkey_status_set(hotkey_orig_status)) != 0)
2001 printk(TPACPI_ERR
2002 "failed to restore hot key mask "
2003 "to BIOS defaults\n");
2004 }
2005}
2006
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002007static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002008{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002009 /* Requirements for changing the default keymaps:
2010 *
2011 * 1. Many of the keys are mapped to KEY_RESERVED for very
2012 * good reasons. Do not change them unless you have deep
2013 * knowledge on the IBM and Lenovo ThinkPad firmware for
2014 * the various ThinkPad models. The driver behaves
2015 * differently for KEY_RESERVED: such keys have their
2016 * hot key mask *unset* in mask_recommended, and also
2017 * in the initial hot key mask programmed into the
2018 * firmware at driver load time, which means the firm-
2019 * ware may react very differently if you change them to
2020 * something else;
2021 *
2022 * 2. You must be subscribed to the linux-thinkpad and
2023 * ibm-acpi-devel mailing lists, and you should read the
2024 * list archives since 2007 if you want to change the
2025 * keymaps. This requirement exists so that you will
2026 * know the past history of problems with the thinkpad-
2027 * acpi driver keymaps, and also that you will be
2028 * listening to any bug reports;
2029 *
2030 * 3. Do not send thinkpad-acpi specific patches directly to
2031 * for merging, *ever*. Send them to the linux-acpi
2032 * mailinglist for comments. Merging is to be done only
2033 * through acpi-test and the ACPI maintainer.
2034 *
2035 * If the above is too much to ask, don't change the keymap.
2036 * Ask the thinkpad-acpi maintainer to do it, instead.
2037 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002038 static u16 ibm_keycode_map[] __initdata = {
2039 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2040 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2041 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2042 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002043
2044 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002045 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2046 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2047 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002048
2049 /* brightness: firmware always reacts to them, unless
2050 * X.org did some tricks in the radeon BIOS scratch
2051 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002052 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002053 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002054
2055 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002056 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002057
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002058 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2059 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002060
2061 /* Volume: firmware always react to it and reprograms
2062 * the built-in *extra* mixer. Never map it to control
2063 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002064 KEY_RESERVED, /* 0x14: VOLUME UP */
2065 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2066 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002067
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002068 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002069
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002070 /* (assignments unknown, please report if found) */
2071 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2072 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2073 };
2074 static u16 lenovo_keycode_map[] __initdata = {
2075 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2076 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2077 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2078 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002079
2080 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002081 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2082 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2083 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002084
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002085 /* These either have to go through ACPI video, or
2086 * act like in the IBM ThinkPads, so don't ever
2087 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002088 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002089 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002090
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002091 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002092
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002093 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2094 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002095
2096 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2097 * react to it and reprograms the built-in *extra* mixer.
2098 * Never map it to control another mixer by default.
2099 *
2100 * T60?, T61, R60?, R61: firmware and EC tries to send
2101 * these over the regular keyboard, so these are no-ops,
2102 * but there are still weird bugs re. MUTE, so do not
2103 * change unless you get test reports from all Lenovo
2104 * models. May cause the BIOS to interfere with the
2105 * HDA mixer.
2106 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002107 KEY_RESERVED, /* 0x14: VOLUME UP */
2108 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2109 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002110
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002111 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002112
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002113 /* (assignments unknown, please report if found) */
2114 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2115 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2116 };
2117
2118#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2119#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2120#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2121
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002122 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002123 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002124 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002125
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002126 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2127
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002128 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002129 BUG_ON(tpacpi_inputdev->open != NULL ||
2130 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002131
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002132 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002133 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002134
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002135#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2136 mutex_init(&hotkey_thread_mutex);
2137 mutex_init(&hotkey_thread_data_mutex);
2138#endif
2139
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002140 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002141 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002142
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002143 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002144 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002145
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002146 if (!tp_features.hotkey)
2147 return 1;
2148
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002149 tpacpi_disable_brightness_delay();
2150
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002151 hotkey_dev_attributes = create_attr_set(13, NULL);
2152 if (!hotkey_dev_attributes)
2153 return -ENOMEM;
2154 res = add_many_to_attr_set(hotkey_dev_attributes,
2155 hotkey_attributes,
2156 ARRAY_SIZE(hotkey_attributes));
2157 if (res)
2158 goto err_exit;
2159
2160 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2161 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2162 for HKEY interface version 0x100 */
2163 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2164 if ((hkeyv >> 8) != 1) {
2165 printk(TPACPI_ERR "unknown version of the "
2166 "HKEY interface: 0x%x\n", hkeyv);
2167 printk(TPACPI_ERR "please report this to %s\n",
2168 TPACPI_MAIL);
2169 } else {
2170 /*
2171 * MHKV 0x100 in A31, R40, R40e,
2172 * T4x, X31, and later
2173 */
2174 tp_features.hotkey_mask = 1;
2175 }
2176 }
2177
2178 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2179 str_supported(tp_features.hotkey_mask));
2180
2181 if (tp_features.hotkey_mask) {
2182 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2183 "MHKA", "qd")) {
2184 printk(TPACPI_ERR
2185 "missing MHKA handler, "
2186 "please report this to %s\n",
2187 TPACPI_MAIL);
2188 /* FN+F12, FN+F4, FN+F3 */
2189 hotkey_all_mask = 0x080cU;
2190 }
2191 }
2192
2193 /* hotkey_source_mask *must* be zero for
2194 * the first hotkey_mask_get */
2195 res = hotkey_status_get(&hotkey_orig_status);
2196 if (res)
2197 goto err_exit;
2198
2199 if (tp_features.hotkey_mask) {
2200 res = hotkey_mask_get();
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002201 if (res)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002202 goto err_exit;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002203
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002204 hotkey_orig_mask = hotkey_mask;
2205 res = add_many_to_attr_set(
2206 hotkey_dev_attributes,
2207 hotkey_mask_attributes,
2208 ARRAY_SIZE(hotkey_mask_attributes));
2209 if (res)
2210 goto err_exit;
2211 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002212
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002213#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002214 if (tp_features.hotkey_mask) {
2215 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2216 & ~hotkey_all_mask;
2217 } else {
2218 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2219 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002220
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002221 vdbg_printk(TPACPI_DBG_INIT,
2222 "hotkey source mask 0x%08x, polling freq %d\n",
2223 hotkey_source_mask, hotkey_poll_freq);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002224#endif
2225
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002226 /* Not all thinkpads have a hardware radio switch */
2227 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2228 tp_features.hotkey_wlsw = 1;
2229 printk(TPACPI_INFO
2230 "radio switch found; radios are %s\n",
2231 enabled(status, 0));
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002232 }
2233 if (tp_features.hotkey_wlsw)
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002234 res = add_to_attr_set(hotkey_dev_attributes,
2235 &dev_attr_hotkey_radio_sw.attr);
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002236
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002237 /* For X41t, X60t, X61t Tablets... */
2238 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2239 tp_features.hotkey_tablet = 1;
2240 printk(TPACPI_INFO
2241 "possible tablet mode switch found; "
2242 "ThinkPad in %s mode\n",
2243 (status & TP_HOTKEY_TABLET_MASK)?
2244 "tablet" : "laptop");
2245 res = add_to_attr_set(hotkey_dev_attributes,
2246 &dev_attr_hotkey_tablet_mode.attr);
2247 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002248
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002249 if (!res)
2250 res = register_attr_set_with_sysfs(
2251 hotkey_dev_attributes,
2252 &tpacpi_pdev->dev.kobj);
2253 if (res)
2254 goto err_exit;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002255
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002256 /* Set up key map */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002257
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002258 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2259 GFP_KERNEL);
2260 if (!hotkey_keycode_map) {
2261 printk(TPACPI_ERR
2262 "failed to allocate memory for key map\n");
2263 res = -ENOMEM;
2264 goto err_exit;
2265 }
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002266
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002267 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2268 dbg_printk(TPACPI_DBG_INIT,
2269 "using Lenovo default hot key map\n");
2270 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2271 TPACPI_HOTKEY_MAP_SIZE);
2272 } else {
2273 dbg_printk(TPACPI_DBG_INIT,
2274 "using IBM default hot key map\n");
2275 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2276 TPACPI_HOTKEY_MAP_SIZE);
2277 }
2278
2279 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2280 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2281 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2282 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2283 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2284 tpacpi_inputdev->keycode = hotkey_keycode_map;
2285 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2286 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2287 set_bit(hotkey_keycode_map[i],
2288 tpacpi_inputdev->keybit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002289 } else {
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002290 if (i < sizeof(hotkey_reserved_mask)*8)
2291 hotkey_reserved_mask |= 1 << i;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002292 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002293 }
2294
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002295 if (tp_features.hotkey_wlsw) {
2296 set_bit(EV_SW, tpacpi_inputdev->evbit);
2297 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2298 }
2299 if (tp_features.hotkey_tablet) {
2300 set_bit(EV_SW, tpacpi_inputdev->evbit);
2301 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002302 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002303
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002304 /* Do not issue duplicate brightness change events to
2305 * userspace */
2306 if (!tp_features.bright_acpimode)
2307 /* update bright_acpimode... */
2308 tpacpi_check_std_acpi_brightness_support();
2309
2310 if (tp_features.bright_acpimode) {
2311 printk(TPACPI_INFO
2312 "This ThinkPad has standard ACPI backlight "
2313 "brightness control, supported by the ACPI "
2314 "video driver\n");
2315 printk(TPACPI_NOTICE
2316 "Disabling thinkpad-acpi brightness events "
2317 "by default...\n");
2318
2319 /* The hotkey_reserved_mask change below is not
2320 * necessary while the keys are at KEY_RESERVED in the
2321 * default map, but better safe than sorry, leave it
2322 * here as a marker of what we have to do, especially
2323 * when we finally become able to set this at runtime
2324 * on response to X.org requests */
2325 hotkey_reserved_mask |=
2326 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2327 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002328 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002329
2330 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2331 res = hotkey_status_set(1);
2332 if (res) {
2333 hotkey_exit();
2334 return res;
2335 }
2336 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2337 & ~hotkey_reserved_mask)
2338 | hotkey_orig_mask);
2339 if (res < 0 && res != -ENXIO) {
2340 hotkey_exit();
2341 return res;
2342 }
2343
2344 dbg_printk(TPACPI_DBG_INIT,
2345 "legacy hot key reporting over procfs %s\n",
2346 (hotkey_report_mode < 2) ?
2347 "enabled" : "disabled");
2348
2349 tpacpi_inputdev->open = &hotkey_inputdev_open;
2350 tpacpi_inputdev->close = &hotkey_inputdev_close;
2351
2352 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002353 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03002354 tpacpi_input_send_tabletsw();
2355
2356 return 0;
2357
2358err_exit:
2359 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2360 hotkey_dev_attributes = NULL;
2361
2362 return (res < 0)? res : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002363}
2364
2365static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2366{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002367 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002368 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002369 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002370 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002371 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002372
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002373 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002374 printk(TPACPI_ERR
2375 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002376 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002377 acpi_bus_generate_netlink_event(
2378 ibm->acpi->device->pnp.device_class,
2379 ibm->acpi->device->dev.bus_id,
2380 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002381 return;
2382 }
2383
2384 while (1) {
2385 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002386 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002387 return;
2388 }
2389
2390 if (hkey == 0) {
2391 /* queue empty */
2392 return;
2393 }
2394
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002395 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002396 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002397 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002398
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002399 switch (hkey >> 12) {
2400 case 1:
2401 /* 0x1000-0x1FFF: key presses */
2402 scancode = hkey & 0xfff;
2403 if (scancode > 0 && scancode < 0x21) {
2404 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002405 if (!(hotkey_source_mask & (1 << scancode))) {
2406 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002407 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002408 } else {
2409 ignore_acpi_ev = 1;
2410 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002411 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002412 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002413 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002414 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002415 case 2:
2416 /* Wakeup reason */
2417 switch (hkey) {
2418 case 0x2304: /* suspend, undock */
2419 case 0x2404: /* hibernation, undock */
2420 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2421 ignore_acpi_ev = 1;
2422 break;
2423 case 0x2305: /* suspend, bay eject */
2424 case 0x2405: /* hibernation, bay eject */
2425 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2426 ignore_acpi_ev = 1;
2427 break;
2428 default:
2429 unk_ev = 1;
2430 }
2431 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2432 printk(TPACPI_INFO
2433 "woke up due to a hot-unplug "
2434 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002435 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002436 }
2437 break;
2438 case 3:
2439 /* bay-related wakeups */
2440 if (hkey == 0x3003) {
2441 hotkey_autosleep_ack = 1;
2442 printk(TPACPI_INFO
2443 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002444 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002445 } else {
2446 unk_ev = 1;
2447 }
2448 break;
2449 case 4:
2450 /* dock-related wakeups */
2451 if (hkey == 0x4003) {
2452 hotkey_autosleep_ack = 1;
2453 printk(TPACPI_INFO
2454 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002455 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002456 } else {
2457 unk_ev = 1;
2458 }
2459 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002460 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002461 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002462 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002463 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002464 case 0x500b: /* X61t: tablet pen inserted into bay */
2465 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002466 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002467 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2468 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2469 tpacpi_input_send_tabletsw();
2470 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002471 send_acpi_ev = 0;
2472 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002473 case 0x5001:
2474 case 0x5002:
2475 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002476 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002477 break;
2478 default:
2479 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002480 }
2481 break;
2482 case 7:
2483 /* 0x7000-0x7FFF: misc */
2484 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002485 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002486 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002487 break;
2488 }
2489 /* fallthrough to default */
2490 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002491 unk_ev = 1;
2492 }
2493 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002494 printk(TPACPI_NOTICE
2495 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002496 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002497
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002498 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002499 if (!ignore_acpi_ev &&
2500 (send_acpi_ev || hotkey_report_mode < 2)) {
2501 acpi_bus_generate_proc_event(ibm->acpi->device,
2502 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002503 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002504
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002505 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002506 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002507 acpi_bus_generate_netlink_event(
2508 ibm->acpi->device->pnp.device_class,
2509 ibm->acpi->device->dev.bus_id,
2510 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002511 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002512 }
2513}
2514
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002515static void hotkey_suspend(pm_message_t state)
2516{
2517 /* Do these on suspend, we get the events on early resume! */
2518 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2519 hotkey_autosleep_ack = 0;
2520}
2521
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002522static void hotkey_resume(void)
2523{
Henrique de Moraes Holschuhd64c81c2008-10-18 14:23:55 -03002524 tpacpi_disable_brightness_delay();
2525
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002526 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002527 printk(TPACPI_ERR
2528 "error while trying to read hot key mask "
2529 "from firmware\n");
Henrique de Moraes Holschuh733e27c2008-07-21 09:15:49 -03002530 tpacpi_send_radiosw_update();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002531 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002532 hotkey_wakeup_reason_notify_change();
2533 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002534 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002535}
2536
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002537/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002538static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002540 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 int len = 0;
2542
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002543 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002544 len += sprintf(p + len, "status:\t\tnot supported\n");
2545 return len;
2546 }
2547
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002548 if (mutex_lock_interruptible(&hotkey_mutex))
2549 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002550 res = hotkey_status_get(&status);
2551 if (!res)
2552 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002553 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002554 if (res)
2555 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002558 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002559 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 len += sprintf(p + len,
2561 "commands:\tenable, disable, reset, <mask>\n");
2562 } else {
2563 len += sprintf(p + len, "mask:\t\tnot supported\n");
2564 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2565 }
2566
2567 return len;
2568}
2569
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002570static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002572 int res, status;
2573 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002576 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 return -ENODEV;
2578
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002579 if (mutex_lock_interruptible(&hotkey_mutex))
2580 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002581
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002582 status = -1;
2583 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002584
2585 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 while ((cmd = next_cmd(&buf))) {
2587 if (strlencmp(cmd, "enable") == 0) {
2588 status = 1;
2589 } else if (strlencmp(cmd, "disable") == 0) {
2590 status = 0;
2591 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002592 status = hotkey_orig_status;
2593 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2595 /* mask set */
2596 } else if (sscanf(cmd, "%x", &mask) == 1) {
2597 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002598 } else {
2599 res = -EINVAL;
2600 goto errexit;
2601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002603 if (status != -1)
2604 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002606 if (!res && mask != hotkey_mask)
2607 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002609errexit:
2610 mutex_unlock(&hotkey_mutex);
2611 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002614static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002615 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002616 {"", 0},
2617};
2618
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002619static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002620 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002621 .notify = hotkey_notify,
2622 .handle = &hkey_handle,
2623 .type = ACPI_DEVICE_NOTIFY,
2624};
2625
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002626static struct ibm_struct hotkey_driver_data = {
2627 .name = "hotkey",
2628 .read = hotkey_read,
2629 .write = hotkey_write,
2630 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002631 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002632 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002633 .acpi = &ibm_hotkey_acpidriver,
2634};
2635
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002636/*************************************************************************
2637 * Bluetooth subdriver
2638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002640enum {
2641 /* ACPI GBDC/SBDC bits */
2642 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2643 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2644 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2645};
2646
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002647static struct rfkill *tpacpi_bluetooth_rfkill;
2648
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002649static int bluetooth_get_radiosw(void)
2650{
2651 int status;
2652
2653 if (!tp_features.bluetooth)
2654 return -ENODEV;
2655
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002656 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2657 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002658 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002659
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002660 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2661 return -EIO;
2662
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002663 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2664 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002665}
2666
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002667static void bluetooth_update_rfk(void)
2668{
2669 int status;
2670
2671 if (!tpacpi_bluetooth_rfkill)
2672 return;
2673
2674 status = bluetooth_get_radiosw();
2675 if (status < 0)
2676 return;
2677 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2678}
2679
2680static int bluetooth_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002681{
2682 int status;
2683
2684 if (!tp_features.bluetooth)
2685 return -ENODEV;
2686
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002687 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2688 * reason to risk weird behaviour. */
2689 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2690 && radio_on)
2691 return -EPERM;
2692
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002693 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2694 return -EIO;
2695 if (radio_on)
2696 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2697 else
2698 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2699 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2700 return -EIO;
2701
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002702 if (update_rfk)
2703 bluetooth_update_rfk();
2704
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002705 return 0;
2706}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002707
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002708/* sysfs bluetooth enable ---------------------------------------------- */
2709static ssize_t bluetooth_enable_show(struct device *dev,
2710 struct device_attribute *attr,
2711 char *buf)
2712{
2713 int status;
2714
2715 status = bluetooth_get_radiosw();
2716 if (status < 0)
2717 return status;
2718
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002719 return snprintf(buf, PAGE_SIZE, "%d\n",
2720 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002721}
2722
2723static ssize_t bluetooth_enable_store(struct device *dev,
2724 struct device_attribute *attr,
2725 const char *buf, size_t count)
2726{
2727 unsigned long t;
2728 int res;
2729
2730 if (parse_strtoul(buf, 1, &t))
2731 return -EINVAL;
2732
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002733 res = bluetooth_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002734
2735 return (res) ? res : count;
2736}
2737
2738static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002739 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002740 bluetooth_enable_show, bluetooth_enable_store);
2741
2742/* --------------------------------------------------------------------- */
2743
2744static struct attribute *bluetooth_attributes[] = {
2745 &dev_attr_bluetooth_enable.attr,
2746 NULL
2747};
2748
2749static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002750 .attrs = bluetooth_attributes,
2751};
2752
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002753static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2754{
2755 int bts = bluetooth_get_radiosw();
2756
2757 if (bts < 0)
2758 return bts;
2759
2760 *state = bts;
2761 return 0;
2762}
2763
2764static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2765{
2766 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2767}
2768
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002769static void bluetooth_exit(void)
2770{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002771 if (tpacpi_bluetooth_rfkill)
2772 rfkill_unregister(tpacpi_bluetooth_rfkill);
2773
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002774 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2775 &bluetooth_attr_group);
2776}
2777
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002778static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002780 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002781 int status = 0;
2782
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002783 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2784
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002785 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002786
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002787 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2788 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002789 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002790 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002792 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2793 str_supported(tp_features.bluetooth),
2794 status);
2795
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002796 if (tp_features.bluetooth &&
2797 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2798 /* no bluetooth hardware present in system */
2799 tp_features.bluetooth = 0;
2800 dbg_printk(TPACPI_DBG_INIT,
2801 "bluetooth hardware not installed\n");
2802 }
2803
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002804 if (!tp_features.bluetooth)
2805 return 1;
2806
2807 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03002808 &bluetooth_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002809 if (res)
2810 return res;
2811
2812 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2813 &tpacpi_bluetooth_rfkill,
2814 RFKILL_TYPE_BLUETOOTH,
2815 "tpacpi_bluetooth_sw",
2816 tpacpi_bluetooth_rfk_set,
2817 tpacpi_bluetooth_rfk_get);
2818 if (res) {
2819 bluetooth_exit();
2820 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002821 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002822
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824}
2825
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002826/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002827static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002830 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002832 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002835 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002836 (status == RFKILL_STATE_UNBLOCKED) ?
2837 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 len += sprintf(p + len, "commands:\tenable, disable\n");
2839 }
2840
2841 return len;
2842}
2843
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002844static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002848 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002849 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 while ((cmd = next_cmd(&buf))) {
2852 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002853 bluetooth_set_radiosw(1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002855 bluetooth_set_radiosw(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 } else
2857 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
2859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return 0;
2861}
2862
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002863static struct ibm_struct bluetooth_driver_data = {
2864 .name = "bluetooth",
2865 .read = bluetooth_read,
2866 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002867 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002868};
2869
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002870/*************************************************************************
2871 * Wan subdriver
2872 */
2873
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002874enum {
2875 /* ACPI GWAN/SWAN bits */
2876 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2877 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2878 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2879};
2880
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002881static struct rfkill *tpacpi_wan_rfkill;
2882
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002883static int wan_get_radiosw(void)
2884{
2885 int status;
2886
2887 if (!tp_features.wan)
2888 return -ENODEV;
2889
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002890 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2891 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002892 return RFKILL_STATE_HARD_BLOCKED;
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002893
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002894 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2895 return -EIO;
2896
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002897 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
2898 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002899}
2900
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002901static void wan_update_rfk(void)
2902{
2903 int status;
2904
2905 if (!tpacpi_wan_rfkill)
2906 return;
2907
2908 status = wan_get_radiosw();
2909 if (status < 0)
2910 return;
2911 rfkill_force_state(tpacpi_wan_rfkill, status);
2912}
2913
2914static int wan_set_radiosw(int radio_on, int update_rfk)
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002915{
2916 int status;
2917
2918 if (!tp_features.wan)
2919 return -ENODEV;
2920
Henrique de Moraes Holschuh133ec3b2008-07-21 09:15:50 -03002921 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2922 * reason to risk weird behaviour. */
2923 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2924 && radio_on)
2925 return -EPERM;
2926
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002927 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2928 return -EIO;
2929 if (radio_on)
2930 status |= TP_ACPI_WANCARD_RADIOSSW;
2931 else
2932 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2933 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2934 return -EIO;
2935
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002936 if (update_rfk)
2937 wan_update_rfk();
2938
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03002939 return 0;
2940}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002941
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002942/* sysfs wan enable ---------------------------------------------------- */
2943static ssize_t wan_enable_show(struct device *dev,
2944 struct device_attribute *attr,
2945 char *buf)
2946{
2947 int status;
2948
2949 status = wan_get_radiosw();
2950 if (status < 0)
2951 return status;
2952
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002953 return snprintf(buf, PAGE_SIZE, "%d\n",
2954 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002955}
2956
2957static ssize_t wan_enable_store(struct device *dev,
2958 struct device_attribute *attr,
2959 const char *buf, size_t count)
2960{
2961 unsigned long t;
2962 int res;
2963
2964 if (parse_strtoul(buf, 1, &t))
2965 return -EINVAL;
2966
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002967 res = wan_set_radiosw(t, 1);
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002968
2969 return (res) ? res : count;
2970}
2971
2972static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002973 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002974 wan_enable_show, wan_enable_store);
2975
2976/* --------------------------------------------------------------------- */
2977
2978static struct attribute *wan_attributes[] = {
2979 &dev_attr_wan_enable.attr,
2980 NULL
2981};
2982
2983static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002984 .attrs = wan_attributes,
2985};
2986
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03002987static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2988{
2989 int wans = wan_get_radiosw();
2990
2991 if (wans < 0)
2992 return wans;
2993
2994 *state = wans;
2995 return 0;
2996}
2997
2998static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
2999{
3000 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3001}
3002
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003003static void wan_exit(void)
3004{
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003005 if (tpacpi_wan_rfkill)
3006 rfkill_unregister(tpacpi_wan_rfkill);
3007
Henrique de Moraes Holschuh07431ec2008-07-21 09:15:50 -03003008 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3009 &wan_attr_group);
3010}
3011
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003012static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003013{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003014 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003015 int status = 0;
3016
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003017 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3018
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003019 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003020
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003021 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003022 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003023
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003024 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3025 str_supported(tp_features.wan),
3026 status);
3027
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003028 if (tp_features.wan &&
3029 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3030 /* no wan hardware present in system */
3031 tp_features.wan = 0;
3032 dbg_printk(TPACPI_DBG_INIT,
3033 "wan hardware not installed\n");
3034 }
3035
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003036 if (!tp_features.wan)
3037 return 1;
3038
3039 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
Henrique de Moraes Holschuh3a872082008-07-21 09:15:49 -03003040 &wan_attr_group);
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003041 if (res)
3042 return res;
3043
3044 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3045 &tpacpi_wan_rfkill,
3046 RFKILL_TYPE_WWAN,
3047 "tpacpi_wwan_sw",
3048 tpacpi_wan_rfk_set,
3049 tpacpi_wan_rfk_get);
3050 if (res) {
3051 wan_exit();
3052 return res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003053 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003054
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003055 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003056}
3057
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003058/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003059static int wan_read(char *p)
3060{
3061 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003062 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003063
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003064 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003065 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003066 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03003067 len += sprintf(p + len, "status:\t\t%s\n",
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003068 (status == RFKILL_STATE_UNBLOCKED) ?
3069 "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003070 len += sprintf(p + len, "commands:\tenable, disable\n");
3071 }
3072
3073 return len;
3074}
3075
3076static int wan_write(char *buf)
3077{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003078 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003079
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003080 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003081 return -ENODEV;
3082
3083 while ((cmd = next_cmd(&buf))) {
3084 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003085 wan_set_radiosw(1, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003086 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuh0e74dc22008-07-21 09:15:51 -03003087 wan_set_radiosw(0, 1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003088 } else
3089 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003090 }
3091
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04003092 return 0;
3093}
3094
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003095static struct ibm_struct wan_driver_data = {
3096 .name = "wan",
3097 .read = wan_read,
3098 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03003099 .exit = wan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003100};
3101
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003102/*************************************************************************
3103 * Video subdriver
3104 */
3105
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003106#ifdef CONFIG_THINKPAD_ACPI_VIDEO
3107
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003108enum video_access_mode {
3109 TPACPI_VIDEO_NONE = 0,
3110 TPACPI_VIDEO_570, /* 570 */
3111 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3112 TPACPI_VIDEO_NEW, /* all others */
3113};
3114
3115enum { /* video status flags, based on VIDEO_570 */
3116 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3117 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3118 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3119};
3120
3121enum { /* TPACPI_VIDEO_570 constants */
3122 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3123 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3124 * video_status_flags */
3125 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3126 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3127};
3128
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003129static enum video_access_mode video_supported;
3130static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003131
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003132static int video_autosw_get(void);
3133static int video_autosw_set(int enable);
3134
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003135TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003136
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003137static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003139 int ivga;
3140
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003141 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3142
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003143 TPACPI_ACPIHANDLE_INIT(vid);
3144 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003145
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003146 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3147 /* G41, assume IVGA doesn't change */
3148 vid_handle = vid2_handle;
3149
3150 if (!vid_handle)
3151 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003152 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003153 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3154 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003155 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003156 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3157 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003158 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003159 else
3160 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003161 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003163 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3164 str_supported(video_supported != TPACPI_VIDEO_NONE),
3165 video_supported);
3166
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003167 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003170static void video_exit(void)
3171{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003172 dbg_printk(TPACPI_DBG_EXIT,
3173 "restoring original video autoswitch mode\n");
3174 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003175 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003176 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003177}
3178
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003179static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 int status = 0;
3182 int i;
3183
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003184 switch (video_supported) {
3185 case TPACPI_VIDEO_570:
3186 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3187 TP_ACPI_VIDEO_570_PHSCMD))
3188 return -EIO;
3189 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3190 break;
3191 case TPACPI_VIDEO_770:
3192 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3193 return -EIO;
3194 if (i)
3195 status |= TP_ACPI_VIDEO_S_LCD;
3196 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3197 return -EIO;
3198 if (i)
3199 status |= TP_ACPI_VIDEO_S_CRT;
3200 break;
3201 case TPACPI_VIDEO_NEW:
3202 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3203 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3204 return -EIO;
3205 if (i)
3206 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003208 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3209 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3210 return -EIO;
3211 if (i)
3212 status |= TP_ACPI_VIDEO_S_LCD;
3213 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3214 return -EIO;
3215 if (i)
3216 status |= TP_ACPI_VIDEO_S_DVI;
3217 break;
3218 default:
3219 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 return status;
3223}
3224
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003225static int video_outputsw_set(int status)
3226{
3227 int autosw;
3228 int res = 0;
3229
3230 switch (video_supported) {
3231 case TPACPI_VIDEO_570:
3232 res = acpi_evalf(NULL, NULL,
3233 "\\_SB.PHS2", "vdd",
3234 TP_ACPI_VIDEO_570_PHS2CMD,
3235 status | TP_ACPI_VIDEO_570_PHS2SET);
3236 break;
3237 case TPACPI_VIDEO_770:
3238 autosw = video_autosw_get();
3239 if (autosw < 0)
3240 return autosw;
3241
3242 res = video_autosw_set(1);
3243 if (res)
3244 return res;
3245 res = acpi_evalf(vid_handle, NULL,
3246 "ASWT", "vdd", status * 0x100, 0);
3247 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003248 printk(TPACPI_ERR
3249 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003250 return -EIO;
3251 }
3252 break;
3253 case TPACPI_VIDEO_NEW:
3254 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003255 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003256 break;
3257 default:
3258 return -ENOSYS;
3259 }
3260
3261 return (res)? 0 : -EIO;
3262}
3263
3264static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003266 int autosw = 0;
3267
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003268 switch (video_supported) {
3269 case TPACPI_VIDEO_570:
3270 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3271 return -EIO;
3272 break;
3273 case TPACPI_VIDEO_770:
3274 case TPACPI_VIDEO_NEW:
3275 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3276 return -EIO;
3277 break;
3278 default:
3279 return -ENOSYS;
3280 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003281
3282 return autosw & 1;
3283}
3284
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003285static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003286{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003287 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003288 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003289 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003290}
3291
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003292static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003293{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003294 int autosw = video_autosw_get();
3295 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003296
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003297 if (autosw < 0)
3298 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003299
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003300 switch (video_supported) {
3301 case TPACPI_VIDEO_570:
3302 res = video_autosw_set(1);
3303 if (res)
3304 return res;
3305 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3306 break;
3307 case TPACPI_VIDEO_770:
3308 case TPACPI_VIDEO_NEW:
3309 res = video_autosw_set(1);
3310 if (res)
3311 return res;
3312 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3313 break;
3314 default:
3315 return -ENOSYS;
3316 }
3317 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003318 printk(TPACPI_ERR
3319 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003320 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003321 }
3322
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003323 return (res)? 0 : -EIO;
3324}
3325
3326static int video_expand_toggle(void)
3327{
3328 switch (video_supported) {
3329 case TPACPI_VIDEO_570:
3330 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3331 0 : -EIO;
3332 case TPACPI_VIDEO_770:
3333 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3334 0 : -EIO;
3335 case TPACPI_VIDEO_NEW:
3336 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3337 0 : -EIO;
3338 default:
3339 return -ENOSYS;
3340 }
3341 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003342}
3343
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003344static int video_read(char *p)
3345{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003346 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003347 int len = 0;
3348
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003349 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003350 len += sprintf(p + len, "status:\t\tnot supported\n");
3351 return len;
3352 }
3353
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003354 status = video_outputsw_get();
3355 if (status < 0)
3356 return status;
3357
3358 autosw = video_autosw_get();
3359 if (autosw < 0)
3360 return autosw;
3361
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003362 len += sprintf(p + len, "status:\t\tsupported\n");
3363 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3364 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003365 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003366 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3367 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3368 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3369 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003370 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003371 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3372 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3373 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3374
3375 return len;
3376}
3377
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003378static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
3380 char *cmd;
3381 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003382 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003384 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003385 return -ENODEV;
3386
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003387 enable = 0;
3388 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 while ((cmd = next_cmd(&buf))) {
3391 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003392 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003394 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003396 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003398 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003399 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003400 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003401 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003402 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003403 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003404 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003406 res = video_autosw_set(1);
3407 if (res)
3408 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003410 res = video_autosw_set(0);
3411 if (res)
3412 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003414 res = video_outputsw_cycle();
3415 if (res)
3416 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003418 res = video_expand_toggle();
3419 if (res)
3420 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 } else
3422 return -EINVAL;
3423 }
3424
3425 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003426 status = video_outputsw_get();
3427 if (status < 0)
3428 return status;
3429 res = video_outputsw_set((status & ~disable) | enable);
3430 if (res)
3431 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
3433
3434 return 0;
3435}
3436
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003437static struct ibm_struct video_driver_data = {
3438 .name = "video",
3439 .read = video_read,
3440 .write = video_write,
3441 .exit = video_exit,
3442};
3443
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003444#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3445
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003446/*************************************************************************
3447 * Light (thinklight) subdriver
3448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003450TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3451TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003452
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003453static int light_get_status(void)
3454{
3455 int status = 0;
3456
3457 if (tp_features.light_status) {
3458 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3459 return -EIO;
3460 return (!!status);
3461 }
3462
3463 return -ENXIO;
3464}
3465
3466static int light_set_status(int status)
3467{
3468 int rc;
3469
3470 if (tp_features.light) {
3471 if (cmos_handle) {
3472 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3473 (status)?
3474 TP_CMOS_THINKLIGHT_ON :
3475 TP_CMOS_THINKLIGHT_OFF);
3476 } else {
3477 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3478 (status)? 1 : 0);
3479 }
3480 return (rc)? 0 : -EIO;
3481 }
3482
3483 return -ENXIO;
3484}
3485
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003486static void light_set_status_worker(struct work_struct *work)
3487{
3488 struct tpacpi_led_classdev *data =
3489 container_of(work, struct tpacpi_led_classdev, work);
3490
3491 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3492 light_set_status((data->new_brightness != LED_OFF));
3493}
3494
3495static void light_sysfs_set(struct led_classdev *led_cdev,
3496 enum led_brightness brightness)
3497{
3498 struct tpacpi_led_classdev *data =
3499 container_of(led_cdev,
3500 struct tpacpi_led_classdev,
3501 led_classdev);
3502 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003503 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003504}
3505
3506static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3507{
3508 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3509}
3510
3511static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3512 .led_classdev = {
3513 .name = "tpacpi::thinklight",
3514 .brightness_set = &light_sysfs_set,
3515 .brightness_get = &light_sysfs_get,
3516 }
3517};
3518
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003519static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003521 int rc;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003522
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003523 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3524
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003525 TPACPI_ACPIHANDLE_INIT(ledb);
3526 TPACPI_ACPIHANDLE_INIT(lght);
3527 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003528 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003529
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003530 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003531 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003532
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003533 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003534 /* light status not supported on
3535 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003536 tp_features.light_status =
3537 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003539 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3540 str_supported(tp_features.light),
3541 str_supported(tp_features.light_status));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003542
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003543 if (!tp_features.light)
3544 return 1;
3545
3546 rc = led_classdev_register(&tpacpi_pdev->dev,
3547 &tpacpi_led_thinklight.led_classdev);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003548
3549 if (rc < 0) {
3550 tp_features.light = 0;
3551 tp_features.light_status = 0;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003552 } else {
3553 rc = 0;
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003554 }
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03003555
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003556 return rc;
3557}
3558
3559static void light_exit(void)
3560{
3561 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3562 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003563 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003566static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003569 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003571 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003572 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003573 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003574 len += sprintf(p + len, "status:\t\tunknown\n");
3575 len += sprintf(p + len, "commands:\ton, off\n");
3576 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003577 status = light_get_status();
3578 if (status < 0)
3579 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003581 len += sprintf(p + len, "commands:\ton, off\n");
3582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
3584 return len;
3585}
3586
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003587static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003590 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003591
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003592 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003593 return -ENODEV;
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 while ((cmd = next_cmd(&buf))) {
3596 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003597 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003599 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 } else
3601 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 }
3603
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003604 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003607static struct ibm_struct light_driver_data = {
3608 .name = "light",
3609 .read = light_read,
3610 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003611 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003612};
3613
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003614/*************************************************************************
3615 * Dock subdriver
3616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003618#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003619
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003620static void dock_notify(struct ibm_struct *ibm, u32 event);
3621static int dock_read(char *p);
3622static int dock_write(char *buf);
3623
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003624TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003625 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3626 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3627 "\\_SB.PCI.ISA.SLCE", /* 570 */
3628 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3629
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003630/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003631TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003632
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003633static const struct acpi_device_id ibm_pci_device_ids[] = {
3634 {PCI_ROOT_HID_STRING, 0},
3635 {"", 0},
3636};
3637
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003638static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3639 {
3640 .notify = dock_notify,
3641 .handle = &dock_handle,
3642 .type = ACPI_SYSTEM_NOTIFY,
3643 },
3644 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003645 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3646 * We just use it to get notifications of dock hotplug
3647 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003648 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003649 .notify = dock_notify,
3650 .handle = &pci_handle,
3651 .type = ACPI_SYSTEM_NOTIFY,
3652 },
3653};
3654
3655static struct ibm_struct dock_driver_data[2] = {
3656 {
3657 .name = "dock",
3658 .read = dock_read,
3659 .write = dock_write,
3660 .acpi = &ibm_dock_acpidriver[0],
3661 },
3662 {
3663 .name = "dock",
3664 .acpi = &ibm_dock_acpidriver[1],
3665 },
3666};
3667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668#define dock_docked() (_sta(dock_handle) & 1)
3669
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003670static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003671{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003672 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3673
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003674 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003675
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003676 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3677 str_supported(dock_handle != NULL));
3678
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003679 return (dock_handle)? 0 : 1;
3680}
3681
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003682static int __init dock_init2(struct ibm_init_struct *iibm)
3683{
3684 int dock2_needed;
3685
3686 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3687
3688 if (dock_driver_data[0].flags.acpi_driver_registered &&
3689 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003690 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003691 dock2_needed = (pci_handle != NULL);
3692 vdbg_printk(TPACPI_DBG_INIT,
3693 "dock PCI handler for the TP 570 is %s\n",
3694 str_supported(dock2_needed));
3695 } else {
3696 vdbg_printk(TPACPI_DBG_INIT,
3697 "dock subdriver part 2 not required\n");
3698 dock2_needed = 0;
3699 }
3700
3701 return (dock2_needed)? 0 : 1;
3702}
3703
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003704static void dock_notify(struct ibm_struct *ibm, u32 event)
3705{
3706 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003707 int pci = ibm->acpi->hid && ibm->acpi->device &&
3708 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003709 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003710
3711 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003712 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003713 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003714 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003715 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003716 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003717 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003718 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003719 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003720 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003721 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003722 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003723 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003724 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003725 }
Len Brown14e04fb2007-08-23 15:20:26 -04003726 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003727 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3728 ibm->acpi->device->dev.bus_id,
3729 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003730}
3731
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003732static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
3734 int len = 0;
3735 int docked = dock_docked();
3736
3737 if (!dock_handle)
3738 len += sprintf(p + len, "status:\t\tnot supported\n");
3739 else if (!docked)
3740 len += sprintf(p + len, "status:\t\tundocked\n");
3741 else {
3742 len += sprintf(p + len, "status:\t\tdocked\n");
3743 len += sprintf(p + len, "commands:\tdock, undock\n");
3744 }
3745
3746 return len;
3747}
3748
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003749static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 char *cmd;
3752
3753 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003754 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
3756 while ((cmd = next_cmd(&buf))) {
3757 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003758 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3759 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return -EIO;
3761 } else if (strlencmp(cmd, "dock") == 0) {
3762 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3763 return -EIO;
3764 } else
3765 return -EINVAL;
3766 }
3767
3768 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003771#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003773/*************************************************************************
3774 * Bay subdriver
3775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003777#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003778
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003779TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003780 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3781 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3782 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3783 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003784TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003785 "_EJ0", /* all others */
3786 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003787TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003788 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3789 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003790TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003791 "_EJ0", /* 770x */
3792 ); /* all others */
3793
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003794static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003796 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3797
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003798 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003799 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003800 TPACPI_ACPIHANDLE_INIT(bay_ej);
3801 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003802 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003803 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003804
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003805 tp_features.bay_status = bay_handle &&
3806 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3807 tp_features.bay_status2 = bay2_handle &&
3808 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003809
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003810 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3811 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3812 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3813 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003815 vdbg_printk(TPACPI_DBG_INIT,
3816 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003817 str_supported(tp_features.bay_status),
3818 str_supported(tp_features.bay_eject),
3819 str_supported(tp_features.bay_status2),
3820 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003821
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003822 return (tp_features.bay_status || tp_features.bay_eject ||
3823 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003826static void bay_notify(struct ibm_struct *ibm, u32 event)
3827{
Len Brown14e04fb2007-08-23 15:20:26 -04003828 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003829 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3830 ibm->acpi->device->dev.bus_id,
3831 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003832}
3833
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003834#define bay_occupied(b) (_sta(b##_handle) & 1)
3835
3836static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
3838 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003839 int occupied = bay_occupied(bay);
3840 int occupied2 = bay_occupied(bay2);
3841 int eject, eject2;
3842
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003843 len += sprintf(p + len, "status:\t\t%s\n",
3844 tp_features.bay_status ?
3845 (occupied ? "occupied" : "unoccupied") :
3846 "not supported");
3847 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003848 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3849 "occupied" : "unoccupied");
3850
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003851 eject = tp_features.bay_eject && occupied;
3852 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003853
3854 if (eject && eject2)
3855 len += sprintf(p + len, "commands:\teject, eject2\n");
3856 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003858 else if (eject2)
3859 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
3861 return len;
3862}
3863
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003864static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
3866 char *cmd;
3867
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003868 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003869 return -ENODEV;
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003872 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003873 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3874 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003875 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003876 strlencmp(cmd, "eject2") == 0) {
3877 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 return -EIO;
3879 } else
3880 return -EINVAL;
3881 }
3882
3883 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003884}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003885
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003886static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3887 .notify = bay_notify,
3888 .handle = &bay_handle,
3889 .type = ACPI_SYSTEM_NOTIFY,
3890};
3891
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003892static struct ibm_struct bay_driver_data = {
3893 .name = "bay",
3894 .read = bay_read,
3895 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003896 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003897};
3898
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003899#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003901/*************************************************************************
3902 * CMOS subdriver
3903 */
3904
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003905/* sysfs cmos_command -------------------------------------------------- */
3906static ssize_t cmos_command_store(struct device *dev,
3907 struct device_attribute *attr,
3908 const char *buf, size_t count)
3909{
3910 unsigned long cmos_cmd;
3911 int res;
3912
3913 if (parse_strtoul(buf, 21, &cmos_cmd))
3914 return -EINVAL;
3915
3916 res = issue_thinkpad_cmos_command(cmos_cmd);
3917 return (res)? res : count;
3918}
3919
3920static struct device_attribute dev_attr_cmos_command =
3921 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3922
3923/* --------------------------------------------------------------------- */
3924
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003925static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003926{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003927 int res;
3928
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003929 vdbg_printk(TPACPI_DBG_INIT,
3930 "initializing cmos commands subdriver\n");
3931
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003932 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003933
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003934 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3935 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003936
3937 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3938 if (res)
3939 return res;
3940
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003941 return (cmos_handle)? 0 : 1;
3942}
3943
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003944static void cmos_exit(void)
3945{
3946 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3947}
3948
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003949static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
3951 int len = 0;
3952
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003953 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3954 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (!cmos_handle)
3956 len += sprintf(p + len, "status:\t\tnot supported\n");
3957 else {
3958 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003959 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 }
3961
3962 return len;
3963}
3964
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003965static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
3967 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003968 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
3970 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003971 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3972 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 /* cmos_cmd set */
3974 } else
3975 return -EINVAL;
3976
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003977 res = issue_thinkpad_cmos_command(cmos_cmd);
3978 if (res)
3979 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 }
3981
3982 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003983}
3984
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003985static struct ibm_struct cmos_driver_data = {
3986 .name = "cmos",
3987 .read = cmos_read,
3988 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003989 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003990};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003991
3992/*************************************************************************
3993 * LED subdriver
3994 */
3995
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003996enum led_access_mode {
3997 TPACPI_LED_NONE = 0,
3998 TPACPI_LED_570, /* 570 */
3999 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4000 TPACPI_LED_NEW, /* all others */
4001};
4002
4003enum { /* For TPACPI_LED_OLD */
4004 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4005 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4006 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4007};
4008
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004009enum led_status_t {
4010 TPACPI_LED_OFF = 0,
4011 TPACPI_LED_ON,
4012 TPACPI_LED_BLINK,
4013};
4014
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02004015static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004016
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004017TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004018 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4019 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004020 "LED", /* all others */
4021 ); /* R30, R31 */
4022
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004023#define TPACPI_LED_NUMLEDS 8
4024static struct tpacpi_led_classdev *tpacpi_leds;
4025static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
Harvey Harrisone3aa51f2008-05-29 17:51:57 -07004026static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004027 /* there's a limit of 19 chars + NULL before 2.6.26 */
4028 "tpacpi::power",
4029 "tpacpi:orange:batt",
4030 "tpacpi:green:batt",
4031 "tpacpi::dock_active",
4032 "tpacpi::bay_active",
4033 "tpacpi::dock_batt",
4034 "tpacpi::unknown_led",
4035 "tpacpi::standby",
4036};
4037
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004038static int led_get_status(const unsigned int led)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004039{
4040 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004041 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004042
4043 switch (led_supported) {
4044 case TPACPI_LED_570:
4045 if (!acpi_evalf(ec_handle,
4046 &status, "GLED", "dd", 1 << led))
4047 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004048 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004049 TPACPI_LED_OFF :
4050 ((status == 1)?
4051 TPACPI_LED_ON :
4052 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004053 tpacpi_led_state_cache[led] = led_s;
4054 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004055 default:
4056 return -ENXIO;
4057 }
4058
4059 /* not reached */
4060}
4061
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004062static int led_set_status(const unsigned int led,
4063 const enum led_status_t ledstatus)
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004064{
4065 /* off, on, blink. Index is led_status_t */
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004066 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4067 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004068
4069 int rc = 0;
4070
4071 switch (led_supported) {
4072 case TPACPI_LED_570:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004073 /* 570 */
4074 if (led > 7)
4075 return -EINVAL;
4076 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4077 (1 << led), led_sled_arg1[ledstatus]))
4078 rc = -EIO;
4079 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004080 case TPACPI_LED_OLD:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004081 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4082 if (led > 7)
4083 return -EINVAL;
4084 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4085 if (rc >= 0)
4086 rc = ec_write(TPACPI_LED_EC_HLBL,
4087 (ledstatus == TPACPI_LED_BLINK) << led);
4088 if (rc >= 0)
4089 rc = ec_write(TPACPI_LED_EC_HLCL,
4090 (ledstatus != TPACPI_LED_OFF) << led);
4091 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004092 case TPACPI_LED_NEW:
Henrique de Moraes Holschuh24e45bb2008-06-03 23:36:11 -03004093 /* all others */
4094 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4095 led, led_led_arg1[ledstatus]))
4096 rc = -EIO;
4097 break;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004098 default:
4099 rc = -ENXIO;
4100 }
4101
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004102 if (!rc)
4103 tpacpi_led_state_cache[led] = ledstatus;
4104
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004105 return rc;
4106}
4107
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004108static void led_sysfs_set_status(unsigned int led,
4109 enum led_brightness brightness)
4110{
4111 led_set_status(led,
4112 (brightness == LED_OFF) ?
4113 TPACPI_LED_OFF :
4114 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4115 TPACPI_LED_BLINK : TPACPI_LED_ON);
4116}
4117
4118static void led_set_status_worker(struct work_struct *work)
4119{
4120 struct tpacpi_led_classdev *data =
4121 container_of(work, struct tpacpi_led_classdev, work);
4122
4123 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4124 led_sysfs_set_status(data->led, data->new_brightness);
4125}
4126
4127static void led_sysfs_set(struct led_classdev *led_cdev,
4128 enum led_brightness brightness)
4129{
4130 struct tpacpi_led_classdev *data = container_of(led_cdev,
4131 struct tpacpi_led_classdev, led_classdev);
4132
4133 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004134 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004135}
4136
4137static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4138 unsigned long *delay_on, unsigned long *delay_off)
4139{
4140 struct tpacpi_led_classdev *data = container_of(led_cdev,
4141 struct tpacpi_led_classdev, led_classdev);
4142
4143 /* Can we choose the flash rate? */
4144 if (*delay_on == 0 && *delay_off == 0) {
4145 /* yes. set them to the hardware blink rate (1 Hz) */
4146 *delay_on = 500; /* ms */
4147 *delay_off = 500; /* ms */
4148 } else if ((*delay_on != 500) || (*delay_off != 500))
4149 return -EINVAL;
4150
4151 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03004152 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004153
4154 return 0;
4155}
4156
4157static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4158{
4159 int rc;
4160
4161 struct tpacpi_led_classdev *data = container_of(led_cdev,
4162 struct tpacpi_led_classdev, led_classdev);
4163
4164 rc = led_get_status(data->led);
4165
4166 if (rc == TPACPI_LED_OFF || rc < 0)
4167 rc = LED_OFF; /* no error handling in led class :( */
4168 else
4169 rc = LED_FULL;
4170
4171 return rc;
4172}
4173
4174static void led_exit(void)
4175{
4176 unsigned int i;
4177
4178 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4179 if (tpacpi_leds[i].led_classdev.name)
4180 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4181 }
4182
4183 kfree(tpacpi_leds);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004184}
4185
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004186static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004187{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004188 unsigned int i;
4189 int rc;
4190
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004191 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4192
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004193 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004194
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004195 if (!led_handle)
4196 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004197 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004198 else if (strlencmp(led_path, "SLED") == 0)
4199 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004200 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004201 else if (strlencmp(led_path, "SYSL") == 0)
4202 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004203 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004204 else
4205 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004206 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004207
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004208 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4209 str_supported(led_supported), led_supported);
4210
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004211 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4212 GFP_KERNEL);
4213 if (!tpacpi_leds) {
4214 printk(TPACPI_ERR "Out of memory for LED data\n");
4215 return -ENOMEM;
4216 }
4217
4218 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4219 tpacpi_leds[i].led = i;
4220
4221 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4222 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4223 if (led_supported == TPACPI_LED_570)
4224 tpacpi_leds[i].led_classdev.brightness_get =
4225 &led_sysfs_get;
4226
4227 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4228
4229 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4230
4231 rc = led_classdev_register(&tpacpi_pdev->dev,
4232 &tpacpi_leds[i].led_classdev);
4233 if (rc < 0) {
4234 tpacpi_leds[i].led_classdev.name = NULL;
4235 led_exit();
4236 return rc;
4237 }
4238 }
4239
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004240 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004241}
4242
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004243#define str_led_status(s) \
4244 ((s) == TPACPI_LED_OFF ? "off" : \
4245 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004246
4247static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
4249 int len = 0;
4250
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004251 if (!led_supported) {
4252 len += sprintf(p + len, "status:\t\tnot supported\n");
4253 return len;
4254 }
4255 len += sprintf(p + len, "status:\t\tsupported\n");
4256
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004257 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004258 /* 570 */
4259 int i, status;
4260 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004261 status = led_get_status(i);
4262 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004263 return -EIO;
4264 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004265 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004266 }
4267 }
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004270 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 return len;
4273}
4274
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004275static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
4277 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004278 int led, rc;
4279 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004280
4281 if (!led_supported)
4282 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
4284 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004285 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 return -EINVAL;
4287
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004288 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004289 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004291 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004292 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004293 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004294 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004295 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004296 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004297
4298 rc = led_set_status(led, s);
4299 if (rc < 0)
4300 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 }
4302
4303 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004304}
4305
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004306static struct ibm_struct led_driver_data = {
4307 .name = "led",
4308 .read = led_read,
4309 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004310 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004311};
4312
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004313/*************************************************************************
4314 * Beep subdriver
4315 */
4316
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004317TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004318
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004319static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004320{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004321 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4322
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004323 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004324
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004325 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4326 str_supported(beep_handle != NULL));
4327
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004328 return (beep_handle)? 0 : 1;
4329}
4330
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004331static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
4333 int len = 0;
4334
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004335 if (!beep_handle)
4336 len += sprintf(p + len, "status:\t\tnot supported\n");
4337 else {
4338 len += sprintf(p + len, "status:\t\tsupported\n");
4339 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
4342 return len;
4343}
4344
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004345static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
4347 char *cmd;
4348 int beep_cmd;
4349
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004350 if (!beep_handle)
4351 return -ENODEV;
4352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004354 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4355 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 /* beep_cmd set */
4357 } else
4358 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004359 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return -EIO;
4361 }
4362
4363 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004364}
4365
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004366static struct ibm_struct beep_driver_data = {
4367 .name = "beep",
4368 .read = beep_read,
4369 .write = beep_write,
4370};
4371
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004372/*************************************************************************
4373 * Thermal subdriver
4374 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004375
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004376enum thermal_access_mode {
4377 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4378 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4379 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4380 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4381 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4382};
4383
4384enum { /* TPACPI_THERMAL_TPEC_* */
4385 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4386 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4387 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4388};
4389
4390#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4391struct ibm_thermal_sensors_struct {
4392 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4393};
4394
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004395static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004396
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004397/* idx is zero-based */
4398static int thermal_get_sensor(int idx, s32 *value)
4399{
4400 int t;
4401 s8 tmp;
4402 char tmpi[5];
4403
4404 t = TP_EC_THERMAL_TMP0;
4405
4406 switch (thermal_read_mode) {
4407#if TPACPI_MAX_THERMAL_SENSORS >= 16
4408 case TPACPI_THERMAL_TPEC_16:
4409 if (idx >= 8 && idx <= 15) {
4410 t = TP_EC_THERMAL_TMP8;
4411 idx -= 8;
4412 }
4413 /* fallthrough */
4414#endif
4415 case TPACPI_THERMAL_TPEC_8:
4416 if (idx <= 7) {
4417 if (!acpi_ec_read(t + idx, &tmp))
4418 return -EIO;
4419 *value = tmp * 1000;
4420 return 0;
4421 }
4422 break;
4423
4424 case TPACPI_THERMAL_ACPI_UPDT:
4425 if (idx <= 7) {
4426 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4427 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4428 return -EIO;
4429 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4430 return -EIO;
4431 *value = (t - 2732) * 100;
4432 return 0;
4433 }
4434 break;
4435
4436 case TPACPI_THERMAL_ACPI_TMP07:
4437 if (idx <= 7) {
4438 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4439 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4440 return -EIO;
4441 if (t > 127 || t < -127)
4442 t = TP_EC_THERMAL_TMP_NA;
4443 *value = t * 1000;
4444 return 0;
4445 }
4446 break;
4447
4448 case TPACPI_THERMAL_NONE:
4449 default:
4450 return -ENOSYS;
4451 }
4452
4453 return -EINVAL;
4454}
4455
4456static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4457{
4458 int res, i;
4459 int n;
4460
4461 n = 8;
4462 i = 0;
4463
4464 if (!s)
4465 return -EINVAL;
4466
4467 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4468 n = 16;
4469
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004470 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004471 res = thermal_get_sensor(i, &s->temp[i]);
4472 if (res)
4473 return res;
4474 }
4475
4476 return n;
4477}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004478
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004479/* sysfs temp##_input -------------------------------------------------- */
4480
4481static ssize_t thermal_temp_input_show(struct device *dev,
4482 struct device_attribute *attr,
4483 char *buf)
4484{
4485 struct sensor_device_attribute *sensor_attr =
4486 to_sensor_dev_attr(attr);
4487 int idx = sensor_attr->index;
4488 s32 value;
4489 int res;
4490
4491 res = thermal_get_sensor(idx, &value);
4492 if (res)
4493 return res;
4494 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4495 return -ENXIO;
4496
4497 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4498}
4499
4500#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004501 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4502 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004503
4504static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4505 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4506 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4507 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4508 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4509 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4510 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4511 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4512 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4513 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4514 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4515 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4516 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4517 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4518 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4519 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4520 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4521};
4522
4523#define THERMAL_ATTRS(X) \
4524 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4525
4526static struct attribute *thermal_temp_input_attr[] = {
4527 THERMAL_ATTRS(8),
4528 THERMAL_ATTRS(9),
4529 THERMAL_ATTRS(10),
4530 THERMAL_ATTRS(11),
4531 THERMAL_ATTRS(12),
4532 THERMAL_ATTRS(13),
4533 THERMAL_ATTRS(14),
4534 THERMAL_ATTRS(15),
4535 THERMAL_ATTRS(0),
4536 THERMAL_ATTRS(1),
4537 THERMAL_ATTRS(2),
4538 THERMAL_ATTRS(3),
4539 THERMAL_ATTRS(4),
4540 THERMAL_ATTRS(5),
4541 THERMAL_ATTRS(6),
4542 THERMAL_ATTRS(7),
4543 NULL
4544};
4545
4546static const struct attribute_group thermal_temp_input16_group = {
4547 .attrs = thermal_temp_input_attr
4548};
4549
4550static const struct attribute_group thermal_temp_input8_group = {
4551 .attrs = &thermal_temp_input_attr[8]
4552};
4553
4554#undef THERMAL_SENSOR_ATTR_TEMP
4555#undef THERMAL_ATTRS
4556
4557/* --------------------------------------------------------------------- */
4558
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004559static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004560{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004561 u8 t, ta1, ta2;
4562 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004563 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004564 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004565
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004566 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4567
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004568 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004570 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004571 /*
4572 * Direct EC access mode: sensors at registers
4573 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4574 * non-implemented, thermal sensors return 0x80 when
4575 * not available
4576 */
4577
4578 ta1 = ta2 = 0;
4579 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004580 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004581 ta1 |= t;
4582 } else {
4583 ta1 = 0;
4584 break;
4585 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004586 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004587 ta2 |= t;
4588 } else {
4589 ta1 = 0;
4590 break;
4591 }
4592 }
4593 if (ta1 == 0) {
4594 /* This is sheer paranoia, but we handle it anyway */
4595 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004596 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004597 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004598 "falling back to ACPI TMPx access "
4599 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004600 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004601 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004602 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004603 "ThinkPad ACPI EC access misbehaving, "
4604 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004605 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004606 }
4607 } else {
4608 thermal_read_mode =
4609 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004610 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004611 }
4612 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004613 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4614 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004615 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004616 } else {
4617 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004618 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004619 }
4620 } else {
4621 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004622 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004623 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004624
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004625 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4626 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4627 thermal_read_mode);
4628
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004629 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004630 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004631 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004632 &thermal_temp_input16_group);
4633 if (res)
4634 return res;
4635 break;
4636 case TPACPI_THERMAL_TPEC_8:
4637 case TPACPI_THERMAL_ACPI_TMP07:
4638 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004639 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004640 &thermal_temp_input8_group);
4641 if (res)
4642 return res;
4643 break;
4644 case TPACPI_THERMAL_NONE:
4645 default:
4646 return 1;
4647 }
4648
4649 return 0;
4650}
4651
4652static void thermal_exit(void)
4653{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004654 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004655 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004656 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004657 &thermal_temp_input16_group);
4658 break;
4659 case TPACPI_THERMAL_TPEC_8:
4660 case TPACPI_THERMAL_ACPI_TMP07:
4661 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004662 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004663 &thermal_temp_input16_group);
4664 break;
4665 case TPACPI_THERMAL_NONE:
4666 default:
4667 break;
4668 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004669}
4670
4671static int thermal_read(char *p)
4672{
4673 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004674 int n, i;
4675 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004676
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004677 n = thermal_get_sensors(&t);
4678 if (unlikely(n < 0))
4679 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004680
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004681 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004682
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004683 if (n > 0) {
4684 for (i = 0; i < (n - 1); i++)
4685 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4686 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4687 } else
4688 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004689
4690 return len;
4691}
4692
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004693static struct ibm_struct thermal_driver_data = {
4694 .name = "thermal",
4695 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004696 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004697};
4698
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004699/*************************************************************************
4700 * EC Dump subdriver
4701 */
4702
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004703static u8 ecdump_regs[256];
4704
4705static int ecdump_read(char *p)
4706{
4707 int len = 0;
4708 int i, j;
4709 u8 v;
4710
4711 len += sprintf(p + len, "EC "
4712 " +00 +01 +02 +03 +04 +05 +06 +07"
4713 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4714 for (i = 0; i < 256; i += 16) {
4715 len += sprintf(p + len, "EC 0x%02x:", i);
4716 for (j = 0; j < 16; j++) {
4717 if (!acpi_ec_read(i + j, &v))
4718 break;
4719 if (v != ecdump_regs[i + j])
4720 len += sprintf(p + len, " *%02x", v);
4721 else
4722 len += sprintf(p + len, " %02x", v);
4723 ecdump_regs[i + j] = v;
4724 }
4725 len += sprintf(p + len, "\n");
4726 if (j != 16)
4727 break;
4728 }
4729
4730 /* These are way too dangerous to advertise openly... */
4731#if 0
4732 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4733 " (<offset> is 00-ff, <value> is 00-ff)\n");
4734 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4735 " (<offset> is 00-ff, <value> is 0-255)\n");
4736#endif
4737 return len;
4738}
4739
4740static int ecdump_write(char *buf)
4741{
4742 char *cmd;
4743 int i, v;
4744
4745 while ((cmd = next_cmd(&buf))) {
4746 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4747 /* i and v set */
4748 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4749 /* i and v set */
4750 } else
4751 return -EINVAL;
4752 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4753 if (!acpi_ec_write(i, v))
4754 return -EIO;
4755 } else
4756 return -EINVAL;
4757 }
4758
4759 return 0;
4760}
4761
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004762static struct ibm_struct ecdump_driver_data = {
4763 .name = "ecdump",
4764 .read = ecdump_read,
4765 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004766 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004767};
4768
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004769/*************************************************************************
4770 * Backlight/brightness subdriver
4771 */
Holger Macht8acb0252006-10-20 14:30:28 -07004772
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004773#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4774
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004775enum {
4776 TP_EC_BACKLIGHT = 0x31,
4777
4778 /* TP_EC_BACKLIGHT bitmasks */
4779 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4780 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4781 TP_EC_BACKLIGHT_MAPSW = 0x20,
4782};
4783
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004784static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004785static int brightness_mode;
4786static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4787
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004788static struct mutex brightness_mutex;
4789
4790/*
4791 * ThinkPads can read brightness from two places: EC 0x31, or
4792 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004793 *
4794 * EC 0x31 has the following layout
4795 * Bit 7: unknown function
4796 * Bit 6: unknown function
4797 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4798 * Bit 4: must be set to zero to avoid problems
4799 * Bit 3-0: backlight brightness level
4800 *
4801 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004802 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004803static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004804{
4805 u8 lec = 0, lcmos = 0, level = 0;
4806
4807 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004808 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004809 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004810 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004811 };
4812 if (brightness_mode & 2) {
4813 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4814 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4815 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4816 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4817 level = lcmos;
4818 }
4819
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004820 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004821 *status = lec; /* Prefer EC, CMOS is just a backing store */
4822 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004823 if (lec == lcmos)
4824 tp_warned.bright_cmos_ec_unsync = 0;
4825 else {
4826 if (!tp_warned.bright_cmos_ec_unsync) {
4827 printk(TPACPI_ERR
4828 "CMOS NVRAM (%u) and EC (%u) do not "
4829 "agree on display brightness level\n",
4830 (unsigned int) lcmos,
4831 (unsigned int) lec);
4832 tp_warned.bright_cmos_ec_unsync = 1;
4833 }
4834 return -EIO;
4835 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004836 } else {
4837 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004838 }
4839
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004840 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004841}
4842
4843/* May return EINTR which can always be mapped to ERESTARTSYS */
4844static int brightness_set(int value)
4845{
4846 int cmos_cmd, inc, i, res;
4847 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004848 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004849
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004850 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4851 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004852 return -EINVAL;
4853
4854 res = mutex_lock_interruptible(&brightness_mutex);
4855 if (res < 0)
4856 return res;
4857
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004858 res = brightness_get_raw(&current_value);
4859 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004860 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004861
4862 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4863 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004864
4865 cmos_cmd = value > current_value ?
4866 TP_CMOS_BRIGHTNESS_UP :
4867 TP_CMOS_BRIGHTNESS_DOWN;
4868 inc = (value > current_value)? 1 : -1;
4869
4870 res = 0;
4871 for (i = current_value; i != value; i += inc) {
4872 if ((brightness_mode & 2) &&
4873 issue_thinkpad_cmos_command(cmos_cmd)) {
4874 res = -EIO;
4875 goto errout;
4876 }
4877 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004878 !acpi_ec_write(TP_EC_BACKLIGHT,
4879 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004880 res = -EIO;
4881 goto errout;;
4882 }
4883 }
4884
4885errout:
4886 mutex_unlock(&brightness_mutex);
4887 return res;
4888}
4889
4890/* sysfs backlight class ----------------------------------------------- */
4891
4892static int brightness_update_status(struct backlight_device *bd)
4893{
4894 /* it is the backlight class's job (caller) to handle
4895 * EINTR and other errors properly */
4896 return brightness_set(
4897 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4898 bd->props.power == FB_BLANK_UNBLANK) ?
4899 bd->props.brightness : 0);
4900}
Holger Macht8acb0252006-10-20 14:30:28 -07004901
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004902static int brightness_get(struct backlight_device *bd)
4903{
4904 int status, res;
4905
4906 res = brightness_get_raw(&status);
4907 if (res < 0)
4908 return 0; /* FIXME: teach backlight about error handling */
4909
4910 return status & TP_EC_BACKLIGHT_LVLMSK;
4911}
4912
Richard Purdie599a52d2007-02-10 23:07:48 +00004913static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004914 .get_brightness = brightness_get,
4915 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004916};
4917
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004918/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004919
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004920static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004921{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004922 int b;
4923
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004924 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4925
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004926 mutex_init(&brightness_mutex);
4927
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004928 /*
4929 * We always attempt to detect acpi support, so as to switch
4930 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4931 * going to publish a backlight interface
4932 */
4933 b = tpacpi_check_std_acpi_brightness_support();
4934 if (b > 0) {
4935 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4936 printk(TPACPI_NOTICE
4937 "Lenovo BIOS switched to ACPI backlight "
4938 "control mode\n");
4939 }
4940 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004941 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004942 "standard ACPI backlight interface "
4943 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004944 return 1;
4945 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004946 }
4947
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004948 if (!brightness_enable) {
4949 dbg_printk(TPACPI_DBG_INIT,
4950 "brightness support disabled by "
4951 "module parameter\n");
4952 return 1;
4953 }
4954
4955 if (b > 16) {
4956 printk(TPACPI_ERR
4957 "Unsupported brightness interface, "
4958 "please contact %s\n", TPACPI_MAIL);
4959 return 1;
4960 }
4961 if (b == 16)
4962 tp_features.bright_16levels = 1;
4963
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004964 if (!brightness_mode) {
4965 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4966 brightness_mode = 2;
4967 else
4968 brightness_mode = 3;
4969
4970 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4971 brightness_mode);
4972 }
4973
4974 if (brightness_mode > 3)
4975 return -EINVAL;
4976
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004977 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004978 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004979
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004980 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004981 printk(TPACPI_INFO
4982 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004983
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004984 ibm_backlight_device = backlight_device_register(
4985 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4986 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004987 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004988 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004989 return PTR_ERR(ibm_backlight_device);
4990 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004991 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004992
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004993 ibm_backlight_device->props.max_brightness =
4994 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004995 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004996 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004997
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004998 return 0;
4999}
5000
5001static void brightness_exit(void)
5002{
5003 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005004 vdbg_printk(TPACPI_DBG_EXIT,
5005 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005006 backlight_device_unregister(ibm_backlight_device);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02005007 }
5008}
5009
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005010static int brightness_read(char *p)
5011{
5012 int len = 0;
5013 int level;
5014
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005015 level = brightness_get(NULL);
5016 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005017 len += sprintf(p + len, "level:\t\tunreadable\n");
5018 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005019 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005020 len += sprintf(p + len, "commands:\tup, down\n");
5021 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005022 " (<level> is 0-%d)\n",
5023 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005024 }
5025
5026 return len;
5027}
5028
5029static int brightness_write(char *buf)
5030{
5031 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005032 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005033 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02005034 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005035
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005036 level = brightness_get(NULL);
5037 if (level < 0)
5038 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005039
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005040 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005041 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005042 if (level < max_level)
5043 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005044 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005045 if (level > 0)
5046 level--;
5047 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5048 level >= 0 && level <= max_level) {
5049 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005050 } else
5051 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005052 }
5053
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02005054 /*
5055 * Now we know what the final level should be, so we try to set it.
5056 * Doing it this way makes the syscall restartable in case of EINTR
5057 */
5058 rc = brightness_set(level);
5059 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005060}
5061
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005062static struct ibm_struct brightness_driver_data = {
5063 .name = "brightness",
5064 .read = brightness_read,
5065 .write = brightness_write,
5066 .exit = brightness_exit,
5067};
5068
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005069/*************************************************************************
5070 * Volume subdriver
5071 */
5072
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005073static int volume_offset = 0x30;
5074
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005075static int volume_read(char *p)
5076{
5077 int len = 0;
5078 u8 level;
5079
5080 if (!acpi_ec_read(volume_offset, &level)) {
5081 len += sprintf(p + len, "level:\t\tunreadable\n");
5082 } else {
5083 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5084 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5085 len += sprintf(p + len, "commands:\tup, down, mute\n");
5086 len += sprintf(p + len, "commands:\tlevel <level>"
5087 " (<level> is 0-15)\n");
5088 }
5089
5090 return len;
5091}
5092
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005093static int volume_write(char *buf)
5094{
5095 int cmos_cmd, inc, i;
5096 u8 level, mute;
5097 int new_level, new_mute;
5098 char *cmd;
5099
5100 while ((cmd = next_cmd(&buf))) {
5101 if (!acpi_ec_read(volume_offset, &level))
5102 return -EIO;
5103 new_mute = mute = level & 0x40;
5104 new_level = level = level & 0xf;
5105
5106 if (strlencmp(cmd, "up") == 0) {
5107 if (mute)
5108 new_mute = 0;
5109 else
5110 new_level = level == 15 ? 15 : level + 1;
5111 } else if (strlencmp(cmd, "down") == 0) {
5112 if (mute)
5113 new_mute = 0;
5114 else
5115 new_level = level == 0 ? 0 : level - 1;
5116 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5117 new_level >= 0 && new_level <= 15) {
5118 /* new_level set */
5119 } else if (strlencmp(cmd, "mute") == 0) {
5120 new_mute = 0x40;
5121 } else
5122 return -EINVAL;
5123
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005124 if (new_level != level) {
5125 /* mute doesn't change */
5126
5127 cmos_cmd = (new_level > level) ?
5128 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005129 inc = new_level > level ? 1 : -1;
5130
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005131 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005132 !acpi_ec_write(volume_offset, level)))
5133 return -EIO;
5134
5135 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005136 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005137 !acpi_ec_write(volume_offset, i + inc))
5138 return -EIO;
5139
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005140 if (mute &&
5141 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5142 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005143 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005144 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005145 }
5146
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005147 if (new_mute != mute) {
5148 /* level doesn't change */
5149
5150 cmos_cmd = (new_mute) ?
5151 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005152
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03005153 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005154 !acpi_ec_write(volume_offset, level + new_mute))
5155 return -EIO;
5156 }
5157 }
5158
5159 return 0;
5160}
5161
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005162static struct ibm_struct volume_driver_data = {
5163 .name = "volume",
5164 .read = volume_read,
5165 .write = volume_write,
5166};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005167
5168/*************************************************************************
5169 * Fan subdriver
5170 */
5171
5172/*
5173 * FAN ACCESS MODES
5174 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005175 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005176 * ACPI GFAN method: returns fan level
5177 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005178 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005179 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005180 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005181 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005182 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5183 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005184 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5185 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005186 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005187 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005188 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5189 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005190 *
5191 * Fan speed changes of any sort (including those caused by the
5192 * disengaged mode) are usually done slowly by the firmware as the
5193 * maximum ammount of fan duty cycle change per second seems to be
5194 * limited.
5195 *
5196 * Reading is not available if GFAN exists.
5197 * Writing is not available if SFAN exists.
5198 *
5199 * Bits
5200 * 7 automatic mode engaged;
5201 * (default operation mode of the ThinkPad)
5202 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005203 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005204 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005205 * the tachometer while the fan controller ramps up
5206 * the speed (which can take up to a few *minutes*).
5207 * Speeds up fan to 100% duty-cycle, which is far above
5208 * the standard RPM levels. It is not impossible that
5209 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005210 * 5-3 unused in some models. Extra bits for fan level
5211 * in others, but still useless as all values above
5212 * 7 map to the same speed as level 7 in these models.
5213 * 2-0 fan level (0..7 usually)
5214 * 0x00 = stop
5215 * 0x07 = max (set when temperatures critical)
5216 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005217 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005218 *
5219 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5220 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5221 * does so, its initial value is meaningless (0x07).
5222 *
5223 * For firmware bugs, refer to:
5224 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5225 *
5226 * ----
5227 *
5228 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5229 * Main fan tachometer reading (in RPM)
5230 *
5231 * This register is present on all ThinkPads with a new-style EC, and
5232 * it is known not to be present on the A21m/e, and T22, as there is
5233 * something else in offset 0x84 according to the ACPI DSDT. Other
5234 * ThinkPads from this same time period (and earlier) probably lack the
5235 * tachometer as well.
5236 *
5237 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5238 * was never fixed by IBM to report the EC firmware version string
5239 * probably support the tachometer (like the early X models), so
5240 * detecting it is quite hard. We need more data to know for sure.
5241 *
5242 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5243 * might result.
5244 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005245 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5246 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005247 *
5248 * For firmware bugs, refer to:
5249 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5250 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005251 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005252 * ThinkPad X31, X40, X41. Not available in the X60.
5253 *
5254 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5255 * high speed. ACPI DSDT seems to map these three speeds to levels
5256 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5257 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5258 *
5259 * The speeds are stored on handles
5260 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5261 *
5262 * There are three default speed sets, acessible as handles:
5263 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5264 *
5265 * ACPI DSDT switches which set is in use depending on various
5266 * factors.
5267 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005268 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005269 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5270 * but the ACPI tables just mention level 7.
5271 */
5272
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005273enum { /* Fan control constants */
5274 fan_status_offset = 0x2f, /* EC register 0x2f */
5275 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5276 * 0x84 must be read before 0x85 */
5277
5278 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5279 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5280
5281 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5282};
5283
5284enum fan_status_access_mode {
5285 TPACPI_FAN_NONE = 0, /* No fan status or control */
5286 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5287 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5288};
5289
5290enum fan_control_access_mode {
5291 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5292 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5293 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5294 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5295};
5296
5297enum fan_control_commands {
5298 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5299 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5300 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5301 * and also watchdog cmd */
5302};
5303
5304static int fan_control_allowed;
5305
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005306static enum fan_status_access_mode fan_status_access_mode;
5307static enum fan_control_access_mode fan_control_access_mode;
5308static enum fan_control_commands fan_control_commands;
5309
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005310static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005311static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005312static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005313
5314static struct mutex fan_mutex;
5315
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005316static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005317static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005318
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005319TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5320TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005321 "\\FSPD", /* 600e/x, 770e, 770x */
5322 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005323TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005324 "JFNS", /* 770x-JL */
5325 ); /* all others */
5326
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005327/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005328 * Call with fan_mutex held
5329 */
5330static void fan_update_desired_level(u8 status)
5331{
5332 if ((status &
5333 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5334 if (status > 7)
5335 fan_control_desired_level = 7;
5336 else
5337 fan_control_desired_level = status;
5338 }
5339}
5340
5341static int fan_get_status(u8 *status)
5342{
5343 u8 s;
5344
5345 /* TODO:
5346 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5347
5348 switch (fan_status_access_mode) {
5349 case TPACPI_FAN_RD_ACPI_GFAN:
5350 /* 570, 600e/x, 770e, 770x */
5351
5352 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5353 return -EIO;
5354
5355 if (likely(status))
5356 *status = s & 0x07;
5357
5358 break;
5359
5360 case TPACPI_FAN_RD_TPEC:
5361 /* all except 570, 600e/x, 770e, 770x */
5362 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5363 return -EIO;
5364
5365 if (likely(status))
5366 *status = s;
5367
5368 break;
5369
5370 default:
5371 return -ENXIO;
5372 }
5373
5374 return 0;
5375}
5376
5377static int fan_get_status_safe(u8 *status)
5378{
5379 int rc;
5380 u8 s;
5381
5382 if (mutex_lock_interruptible(&fan_mutex))
5383 return -ERESTARTSYS;
5384 rc = fan_get_status(&s);
5385 if (!rc)
5386 fan_update_desired_level(s);
5387 mutex_unlock(&fan_mutex);
5388
5389 if (status)
5390 *status = s;
5391
5392 return rc;
5393}
5394
5395static int fan_get_speed(unsigned int *speed)
5396{
5397 u8 hi, lo;
5398
5399 switch (fan_status_access_mode) {
5400 case TPACPI_FAN_RD_TPEC:
5401 /* all except 570, 600e/x, 770e, 770x */
5402 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5403 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5404 return -EIO;
5405
5406 if (likely(speed))
5407 *speed = (hi << 8) | lo;
5408
5409 break;
5410
5411 default:
5412 return -ENXIO;
5413 }
5414
5415 return 0;
5416}
5417
5418static int fan_set_level(int level)
5419{
5420 if (!fan_control_allowed)
5421 return -EPERM;
5422
5423 switch (fan_control_access_mode) {
5424 case TPACPI_FAN_WR_ACPI_SFAN:
5425 if (level >= 0 && level <= 7) {
5426 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5427 return -EIO;
5428 } else
5429 return -EINVAL;
5430 break;
5431
5432 case TPACPI_FAN_WR_ACPI_FANS:
5433 case TPACPI_FAN_WR_TPEC:
5434 if ((level != TP_EC_FAN_AUTO) &&
5435 (level != TP_EC_FAN_FULLSPEED) &&
5436 ((level < 0) || (level > 7)))
5437 return -EINVAL;
5438
5439 /* safety net should the EC not support AUTO
5440 * or FULLSPEED mode bits and just ignore them */
5441 if (level & TP_EC_FAN_FULLSPEED)
5442 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005443 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005444 level |= 4; /* safety min speed 4 */
5445
5446 if (!acpi_ec_write(fan_status_offset, level))
5447 return -EIO;
5448 else
5449 tp_features.fan_ctrl_status_undef = 0;
5450 break;
5451
5452 default:
5453 return -ENXIO;
5454 }
5455 return 0;
5456}
5457
5458static int fan_set_level_safe(int level)
5459{
5460 int rc;
5461
5462 if (!fan_control_allowed)
5463 return -EPERM;
5464
5465 if (mutex_lock_interruptible(&fan_mutex))
5466 return -ERESTARTSYS;
5467
5468 if (level == TPACPI_FAN_LAST_LEVEL)
5469 level = fan_control_desired_level;
5470
5471 rc = fan_set_level(level);
5472 if (!rc)
5473 fan_update_desired_level(level);
5474
5475 mutex_unlock(&fan_mutex);
5476 return rc;
5477}
5478
5479static int fan_set_enable(void)
5480{
5481 u8 s;
5482 int rc;
5483
5484 if (!fan_control_allowed)
5485 return -EPERM;
5486
5487 if (mutex_lock_interruptible(&fan_mutex))
5488 return -ERESTARTSYS;
5489
5490 switch (fan_control_access_mode) {
5491 case TPACPI_FAN_WR_ACPI_FANS:
5492 case TPACPI_FAN_WR_TPEC:
5493 rc = fan_get_status(&s);
5494 if (rc < 0)
5495 break;
5496
5497 /* Don't go out of emergency fan mode */
5498 if (s != 7) {
5499 s &= 0x07;
5500 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5501 }
5502
5503 if (!acpi_ec_write(fan_status_offset, s))
5504 rc = -EIO;
5505 else {
5506 tp_features.fan_ctrl_status_undef = 0;
5507 rc = 0;
5508 }
5509 break;
5510
5511 case TPACPI_FAN_WR_ACPI_SFAN:
5512 rc = fan_get_status(&s);
5513 if (rc < 0)
5514 break;
5515
5516 s &= 0x07;
5517
5518 /* Set fan to at least level 4 */
5519 s |= 4;
5520
5521 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005522 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005523 else
5524 rc = 0;
5525 break;
5526
5527 default:
5528 rc = -ENXIO;
5529 }
5530
5531 mutex_unlock(&fan_mutex);
5532 return rc;
5533}
5534
5535static int fan_set_disable(void)
5536{
5537 int rc;
5538
5539 if (!fan_control_allowed)
5540 return -EPERM;
5541
5542 if (mutex_lock_interruptible(&fan_mutex))
5543 return -ERESTARTSYS;
5544
5545 rc = 0;
5546 switch (fan_control_access_mode) {
5547 case TPACPI_FAN_WR_ACPI_FANS:
5548 case TPACPI_FAN_WR_TPEC:
5549 if (!acpi_ec_write(fan_status_offset, 0x00))
5550 rc = -EIO;
5551 else {
5552 fan_control_desired_level = 0;
5553 tp_features.fan_ctrl_status_undef = 0;
5554 }
5555 break;
5556
5557 case TPACPI_FAN_WR_ACPI_SFAN:
5558 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5559 rc = -EIO;
5560 else
5561 fan_control_desired_level = 0;
5562 break;
5563
5564 default:
5565 rc = -ENXIO;
5566 }
5567
5568
5569 mutex_unlock(&fan_mutex);
5570 return rc;
5571}
5572
5573static int fan_set_speed(int speed)
5574{
5575 int rc;
5576
5577 if (!fan_control_allowed)
5578 return -EPERM;
5579
5580 if (mutex_lock_interruptible(&fan_mutex))
5581 return -ERESTARTSYS;
5582
5583 rc = 0;
5584 switch (fan_control_access_mode) {
5585 case TPACPI_FAN_WR_ACPI_FANS:
5586 if (speed >= 0 && speed <= 65535) {
5587 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5588 speed, speed, speed))
5589 rc = -EIO;
5590 } else
5591 rc = -EINVAL;
5592 break;
5593
5594 default:
5595 rc = -ENXIO;
5596 }
5597
5598 mutex_unlock(&fan_mutex);
5599 return rc;
5600}
5601
5602static void fan_watchdog_reset(void)
5603{
5604 static int fan_watchdog_active;
5605
5606 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5607 return;
5608
5609 if (fan_watchdog_active)
5610 cancel_delayed_work(&fan_watchdog_task);
5611
5612 if (fan_watchdog_maxinterval > 0 &&
5613 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5614 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005615 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005616 msecs_to_jiffies(fan_watchdog_maxinterval
5617 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005618 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005619 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005620 "watchdog will not trigger\n");
5621 }
5622 } else
5623 fan_watchdog_active = 0;
5624}
5625
5626static void fan_watchdog_fire(struct work_struct *ignored)
5627{
5628 int rc;
5629
5630 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5631 return;
5632
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005633 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005634 rc = fan_set_enable();
5635 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005636 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005637 "will try again later...\n", -rc);
5638 /* reschedule for later */
5639 fan_watchdog_reset();
5640 }
5641}
5642
5643/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005644 * SYSFS fan layout: hwmon compatible (device)
5645 *
5646 * pwm*_enable:
5647 * 0: "disengaged" mode
5648 * 1: manual mode
5649 * 2: native EC "auto" mode (recommended, hardware default)
5650 *
5651 * pwm*: set speed in manual mode, ignored otherwise.
5652 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5653 * interpolation.
5654 *
5655 * fan*_input: tachometer reading, RPM
5656 *
5657 *
5658 * SYSFS fan layout: extensions
5659 *
5660 * fan_watchdog (driver):
5661 * fan watchdog interval in seconds, 0 disables (default), max 120
5662 */
5663
5664/* sysfs fan pwm1_enable ----------------------------------------------- */
5665static ssize_t fan_pwm1_enable_show(struct device *dev,
5666 struct device_attribute *attr,
5667 char *buf)
5668{
5669 int res, mode;
5670 u8 status;
5671
5672 res = fan_get_status_safe(&status);
5673 if (res)
5674 return res;
5675
5676 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5677 if (status != fan_control_initial_status) {
5678 tp_features.fan_ctrl_status_undef = 0;
5679 } else {
5680 /* Return most likely status. In fact, it
5681 * might be the only possible status */
5682 status = TP_EC_FAN_AUTO;
5683 }
5684 }
5685
5686 if (status & TP_EC_FAN_FULLSPEED) {
5687 mode = 0;
5688 } else if (status & TP_EC_FAN_AUTO) {
5689 mode = 2;
5690 } else
5691 mode = 1;
5692
5693 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5694}
5695
5696static ssize_t fan_pwm1_enable_store(struct device *dev,
5697 struct device_attribute *attr,
5698 const char *buf, size_t count)
5699{
5700 unsigned long t;
5701 int res, level;
5702
5703 if (parse_strtoul(buf, 2, &t))
5704 return -EINVAL;
5705
5706 switch (t) {
5707 case 0:
5708 level = TP_EC_FAN_FULLSPEED;
5709 break;
5710 case 1:
5711 level = TPACPI_FAN_LAST_LEVEL;
5712 break;
5713 case 2:
5714 level = TP_EC_FAN_AUTO;
5715 break;
5716 case 3:
5717 /* reserved for software-controlled auto mode */
5718 return -ENOSYS;
5719 default:
5720 return -EINVAL;
5721 }
5722
5723 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005724 if (res == -ENXIO)
5725 return -EINVAL;
5726 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005727 return res;
5728
5729 fan_watchdog_reset();
5730
5731 return count;
5732}
5733
5734static struct device_attribute dev_attr_fan_pwm1_enable =
5735 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5736 fan_pwm1_enable_show, fan_pwm1_enable_store);
5737
5738/* sysfs fan pwm1 ------------------------------------------------------ */
5739static ssize_t fan_pwm1_show(struct device *dev,
5740 struct device_attribute *attr,
5741 char *buf)
5742{
5743 int res;
5744 u8 status;
5745
5746 res = fan_get_status_safe(&status);
5747 if (res)
5748 return res;
5749
5750 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5751 if (status != fan_control_initial_status) {
5752 tp_features.fan_ctrl_status_undef = 0;
5753 } else {
5754 status = TP_EC_FAN_AUTO;
5755 }
5756 }
5757
5758 if ((status &
5759 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5760 status = fan_control_desired_level;
5761
5762 if (status > 7)
5763 status = 7;
5764
5765 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5766}
5767
5768static ssize_t fan_pwm1_store(struct device *dev,
5769 struct device_attribute *attr,
5770 const char *buf, size_t count)
5771{
5772 unsigned long s;
5773 int rc;
5774 u8 status, newlevel;
5775
5776 if (parse_strtoul(buf, 255, &s))
5777 return -EINVAL;
5778
5779 /* scale down from 0-255 to 0-7 */
5780 newlevel = (s >> 5) & 0x07;
5781
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005782 if (mutex_lock_interruptible(&fan_mutex))
5783 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005784
5785 rc = fan_get_status(&status);
5786 if (!rc && (status &
5787 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5788 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005789 if (rc == -ENXIO)
5790 rc = -EINVAL;
5791 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005792 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005793 fan_watchdog_reset();
5794 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005795 }
5796
5797 mutex_unlock(&fan_mutex);
5798 return (rc)? rc : count;
5799}
5800
5801static struct device_attribute dev_attr_fan_pwm1 =
5802 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5803 fan_pwm1_show, fan_pwm1_store);
5804
5805/* sysfs fan fan1_input ------------------------------------------------ */
5806static ssize_t fan_fan1_input_show(struct device *dev,
5807 struct device_attribute *attr,
5808 char *buf)
5809{
5810 int res;
5811 unsigned int speed;
5812
5813 res = fan_get_speed(&speed);
5814 if (res < 0)
5815 return res;
5816
5817 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5818}
5819
5820static struct device_attribute dev_attr_fan_fan1_input =
5821 __ATTR(fan1_input, S_IRUGO,
5822 fan_fan1_input_show, NULL);
5823
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005824/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005825static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5826 char *buf)
5827{
5828 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5829}
5830
5831static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5832 const char *buf, size_t count)
5833{
5834 unsigned long t;
5835
5836 if (parse_strtoul(buf, 120, &t))
5837 return -EINVAL;
5838
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005839 if (!fan_control_allowed)
5840 return -EPERM;
5841
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005842 fan_watchdog_maxinterval = t;
5843 fan_watchdog_reset();
5844
5845 return count;
5846}
5847
5848static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5849 fan_fan_watchdog_show, fan_fan_watchdog_store);
5850
5851/* --------------------------------------------------------------------- */
5852static struct attribute *fan_attributes[] = {
5853 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5854 &dev_attr_fan_fan1_input.attr,
5855 NULL
5856};
5857
5858static const struct attribute_group fan_attr_group = {
5859 .attrs = fan_attributes,
5860};
5861
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005862static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005863{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005864 int rc;
5865
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005866 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5867
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005868 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005869 fan_status_access_mode = TPACPI_FAN_NONE;
5870 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005871 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005872 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005873 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005874 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005875
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005876 TPACPI_ACPIHANDLE_INIT(fans);
5877 TPACPI_ACPIHANDLE_INIT(gfan);
5878 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005879
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005880 if (gfan_handle) {
5881 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005882 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005883 } else {
5884 /* all other ThinkPads: note that even old-style
5885 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005886 if (likely(acpi_ec_read(fan_status_offset,
5887 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005888 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005889
5890 /* In some ThinkPads, neither the EC nor the ACPI
5891 * DSDT initialize the fan status, and it ends up
5892 * being set to 0x07 when it *could* be either
5893 * 0x07 or 0x80.
5894 *
5895 * Enable for TP-1Y (T43), TP-78 (R51e),
5896 * TP-76 (R52), TP-70 (T43, R52), which are known
5897 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005898 if (fan_control_initial_status == 0x07) {
5899 switch (thinkpad_id.ec_model) {
5900 case 0x5931: /* TP-1Y */
5901 case 0x3837: /* TP-78 */
5902 case 0x3637: /* TP-76 */
5903 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005904 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005905 "fan_init: initial fan status "
5906 "is unknown, assuming it is "
5907 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005908 tp_features.fan_ctrl_status_undef = 1;
5909 ;;
5910 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005911 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005912 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005913 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005914 "ThinkPad ACPI EC access misbehaving, "
5915 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005916 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005917 }
5918 }
5919
5920 if (sfan_handle) {
5921 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005922 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005923 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005924 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005925 } else {
5926 if (!gfan_handle) {
5927 /* gfan without sfan means no fan control */
5928 /* all other models implement TP EC 0x2f control */
5929
5930 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005931 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005932 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005933 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005934 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005935 TPACPI_FAN_CMD_SPEED |
5936 TPACPI_FAN_CMD_LEVEL |
5937 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005938 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005939 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005940 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005941 TPACPI_FAN_CMD_LEVEL |
5942 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005943 }
5944 }
5945 }
5946
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005947 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5948 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5949 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5950 fan_status_access_mode, fan_control_access_mode);
5951
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005952 /* fan control master switch */
5953 if (!fan_control_allowed) {
5954 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5955 fan_control_commands = 0;
5956 dbg_printk(TPACPI_DBG_INIT,
5957 "fan control features disabled by parameter\n");
5958 }
5959
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005960 /* update fan_control_desired_level */
5961 if (fan_status_access_mode != TPACPI_FAN_NONE)
5962 fan_get_status_safe(NULL);
5963
5964 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5965 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005966 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005967 &fan_attr_group);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005968 if (rc < 0)
5969 return rc;
Henrique de Moraes Holschuh9c0a76e2008-06-03 23:36:10 -03005970
5971 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5972 &driver_attr_fan_watchdog);
5973 if (rc < 0) {
5974 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5975 &fan_attr_group);
5976 return rc;
5977 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005978 return 0;
5979 } else
5980 return 1;
5981}
5982
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005983static void fan_exit(void)
5984{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005985 vdbg_printk(TPACPI_DBG_EXIT,
5986 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005987
5988 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005989 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005990 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5991 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005992
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005993 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005994 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005995}
5996
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03005997static void fan_suspend(pm_message_t state)
5998{
5999 if (!fan_control_allowed)
6000 return;
6001
6002 /* Store fan status in cache */
6003 fan_get_status_safe(NULL);
6004 if (tp_features.fan_ctrl_status_undef)
6005 fan_control_desired_level = TP_EC_FAN_AUTO;
6006}
6007
6008static void fan_resume(void)
6009{
6010 u8 saved_fan_level;
6011 u8 current_level = 7;
6012 bool do_set = false;
6013
6014 /* DSDT *always* updates status on resume */
6015 tp_features.fan_ctrl_status_undef = 0;
6016
6017 saved_fan_level = fan_control_desired_level;
6018 if (!fan_control_allowed ||
6019 (fan_get_status_safe(&current_level) < 0))
6020 return;
6021
6022 switch (fan_control_access_mode) {
6023 case TPACPI_FAN_WR_ACPI_SFAN:
6024 do_set = (saved_fan_level > current_level);
6025 break;
6026 case TPACPI_FAN_WR_ACPI_FANS:
6027 case TPACPI_FAN_WR_TPEC:
6028 do_set = ((saved_fan_level & TP_EC_FAN_FULLSPEED) ||
6029 (saved_fan_level == 7 &&
6030 !(current_level & TP_EC_FAN_FULLSPEED)));
6031 break;
6032 default:
6033 return;
6034 }
6035 if (do_set) {
6036 printk(TPACPI_NOTICE
6037 "restoring fan level to 0x%02x\n",
6038 saved_fan_level);
6039 fan_set_level_safe(saved_fan_level);
6040 }
6041}
6042
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006043static int fan_read(char *p)
6044{
6045 int len = 0;
6046 int rc;
6047 u8 status;
6048 unsigned int speed = 0;
6049
6050 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006051 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006052 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006053 rc = fan_get_status_safe(&status);
6054 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006055 return rc;
6056
6057 len += sprintf(p + len, "status:\t\t%s\n"
6058 "level:\t\t%d\n",
6059 (status != 0) ? "enabled" : "disabled", status);
6060 break;
6061
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006062 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006063 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006064 rc = fan_get_status_safe(&status);
6065 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006066 return rc;
6067
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006068 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006069 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03006070 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006071 else
6072 /* Return most likely status. In fact, it
6073 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006074 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006075 }
6076
6077 len += sprintf(p + len, "status:\t\t%s\n",
6078 (status != 0) ? "enabled" : "disabled");
6079
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006080 rc = fan_get_speed(&speed);
6081 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006082 return rc;
6083
6084 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6085
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006086 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006087 /* Disengaged mode takes precedence */
6088 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006089 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006090 len += sprintf(p + len, "level:\t\tauto\n");
6091 else
6092 len += sprintf(p + len, "level:\t\t%d\n", status);
6093 break;
6094
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006095 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006096 default:
6097 len += sprintf(p + len, "status:\t\tnot supported\n");
6098 }
6099
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006100 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006101 len += sprintf(p + len, "commands:\tlevel <level>");
6102
6103 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006104 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006105 len += sprintf(p + len, " (<level> is 0-7)\n");
6106 break;
6107
6108 default:
6109 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006110 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006111 break;
6112 }
6113 }
6114
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006115 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006116 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006117 "commands:\twatchdog <timeout> (<timeout> "
6118 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006119
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006120 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006121 len += sprintf(p + len, "commands:\tspeed <speed>"
6122 " (<speed> is 0-65535)\n");
6123
6124 return len;
6125}
6126
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006127static int fan_write_cmd_level(const char *cmd, int *rc)
6128{
6129 int level;
6130
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006131 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006132 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03006133 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006134 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006135 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02006136 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006137 return 0;
6138
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006139 *rc = fan_set_level_safe(level);
6140 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006141 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006142 "access mode %d", fan_control_access_mode);
6143
6144 return 1;
6145}
6146
6147static int fan_write_cmd_enable(const char *cmd, int *rc)
6148{
6149 if (strlencmp(cmd, "enable") != 0)
6150 return 0;
6151
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006152 *rc = fan_set_enable();
6153 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006154 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006155 "access mode %d", fan_control_access_mode);
6156
6157 return 1;
6158}
6159
6160static int fan_write_cmd_disable(const char *cmd, int *rc)
6161{
6162 if (strlencmp(cmd, "disable") != 0)
6163 return 0;
6164
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006165 *rc = fan_set_disable();
6166 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006167 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006168 "access mode %d", fan_control_access_mode);
6169
6170 return 1;
6171}
6172
6173static int fan_write_cmd_speed(const char *cmd, int *rc)
6174{
6175 int speed;
6176
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02006177 /* TODO:
6178 * Support speed <low> <medium> <high> ? */
6179
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006180 if (sscanf(cmd, "speed %d", &speed) != 1)
6181 return 0;
6182
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006183 *rc = fan_set_speed(speed);
6184 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006185 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006186 "access mode %d", fan_control_access_mode);
6187
6188 return 1;
6189}
6190
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006191static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6192{
6193 int interval;
6194
6195 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6196 return 0;
6197
6198 if (interval < 0 || interval > 120)
6199 *rc = -EINVAL;
6200 else
6201 fan_watchdog_maxinterval = interval;
6202
6203 return 1;
6204}
6205
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006206static int fan_write(char *buf)
6207{
6208 char *cmd;
6209 int rc = 0;
6210
6211 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006212 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006213 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006214 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006215 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006216 fan_write_cmd_disable(cmd, &rc) ||
6217 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03006218 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006219 fan_write_cmd_speed(cmd, &rc))
6220 )
6221 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02006222 else if (!rc)
6223 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006224 }
6225
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02006226 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006227}
6228
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006229static struct ibm_struct fan_driver_data = {
6230 .name = "fan",
6231 .read = fan_read,
6232 .write = fan_write,
6233 .exit = fan_exit,
Henrique de Moraes Holschuh75700e52008-10-18 14:23:52 -03006234 .suspend = fan_suspend,
6235 .resume = fan_resume,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006236};
6237
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006238/****************************************************************************
6239 ****************************************************************************
6240 *
6241 * Infrastructure
6242 *
6243 ****************************************************************************
6244 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006246/* sysfs name ---------------------------------------------------------- */
6247static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6248 struct device_attribute *attr,
6249 char *buf)
6250{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006251 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006252}
6253
6254static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6255 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6256
6257/* --------------------------------------------------------------------- */
6258
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006259/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03006260static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006261
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006262/*
6263 * Module and infrastructure proble, init and exit handling
6264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006266static int force_load;
6267
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006268#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006269static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006270{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006271 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006272
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006273 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006274}
6275#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6276
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006277static void ibm_exit(struct ibm_struct *ibm)
6278{
6279 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6280
6281 list_del_init(&ibm->all_drivers);
6282
6283 if (ibm->flags.acpi_notify_installed) {
6284 dbg_printk(TPACPI_DBG_EXIT,
6285 "%s: acpi_remove_notify_handler\n", ibm->name);
6286 BUG_ON(!ibm->acpi);
6287 acpi_remove_notify_handler(*ibm->acpi->handle,
6288 ibm->acpi->type,
6289 dispatch_acpi_notify);
6290 ibm->flags.acpi_notify_installed = 0;
6291 ibm->flags.acpi_notify_installed = 0;
6292 }
6293
6294 if (ibm->flags.proc_created) {
6295 dbg_printk(TPACPI_DBG_EXIT,
6296 "%s: remove_proc_entry\n", ibm->name);
6297 remove_proc_entry(ibm->name, proc_dir);
6298 ibm->flags.proc_created = 0;
6299 }
6300
6301 if (ibm->flags.acpi_driver_registered) {
6302 dbg_printk(TPACPI_DBG_EXIT,
6303 "%s: acpi_bus_unregister_driver\n", ibm->name);
6304 BUG_ON(!ibm->acpi);
6305 acpi_bus_unregister_driver(ibm->acpi->driver);
6306 kfree(ibm->acpi->driver);
6307 ibm->acpi->driver = NULL;
6308 ibm->flags.acpi_driver_registered = 0;
6309 }
6310
6311 if (ibm->flags.init_called && ibm->exit) {
6312 ibm->exit();
6313 ibm->flags.init_called = 0;
6314 }
6315
6316 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6317}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006318
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006319static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320{
6321 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006322 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 struct proc_dir_entry *entry;
6324
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006325 BUG_ON(ibm == NULL);
6326
6327 INIT_LIST_HEAD(&ibm->all_drivers);
6328
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006329 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 return 0;
6331
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006332 dbg_printk(TPACPI_DBG_INIT,
6333 "probing for %s\n", ibm->name);
6334
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006335 if (iibm->init) {
6336 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006337 if (ret > 0)
6338 return 0; /* probe failed */
6339 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006341
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006342 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 }
6344
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006345 if (ibm->acpi) {
6346 if (ibm->acpi->hid) {
6347 ret = register_tpacpi_subdriver(ibm);
6348 if (ret)
6349 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006350 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006351
6352 if (ibm->acpi->notify) {
6353 ret = setup_acpi_notify(ibm);
6354 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006355 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006356 ibm->name);
6357 ret = 0;
6358 goto err_out;
6359 }
6360 if (ret < 0)
6361 goto err_out;
6362 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006363 }
6364
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006365 dbg_printk(TPACPI_DBG_INIT,
6366 "%s installed\n", ibm->name);
6367
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006368 if (ibm->read) {
6369 entry = create_proc_entry(ibm->name,
6370 S_IFREG | S_IRUGO | S_IWUSR,
6371 proc_dir);
6372 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006373 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006374 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006375 ret = -ENODEV;
6376 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006377 }
6378 entry->owner = THIS_MODULE;
6379 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006380 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006381 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006382 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006383 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006386 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6387
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006389
6390err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006391 dbg_printk(TPACPI_DBG_INIT,
6392 "%s: at error exit path with result %d\n",
6393 ibm->name, ret);
6394
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006395 ibm_exit(ibm);
6396 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397}
6398
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006399/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006401/* returns 0 - probe ok, or < 0 - probe error.
6402 * Probe ok doesn't mean thinkpad found.
6403 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6404static int __must_check __init get_thinkpad_model_data(
6405 struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006406{
Jeff Garzik18552562007-10-03 15:15:40 -04006407 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006408 char ec_fw_string[18];
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006409 char const *s;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006410
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006411 if (!tp)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006412 return -EINVAL;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006413
6414 memset(tp, 0, sizeof(*tp));
6415
6416 if (dmi_name_in_vendors("IBM"))
6417 tp->vendor = PCI_VENDOR_ID_IBM;
6418 else if (dmi_name_in_vendors("LENOVO"))
6419 tp->vendor = PCI_VENDOR_ID_LENOVO;
6420 else
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006421 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006422
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006423 s = dmi_get_system_info(DMI_BIOS_VERSION);
6424 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6425 if (s && !tp->bios_version_str)
6426 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006427 if (!tp->bios_version_str)
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006428 return 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006429 tp->bios_model = tp->bios_version_str[0]
6430 | (tp->bios_version_str[1] << 8);
6431
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006432 /*
6433 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6434 * X32 or newer, all Z series; Some models must have an
6435 * up-to-date BIOS or they will not be detected.
6436 *
6437 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6438 */
6439 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006440 if (sscanf(dev->name,
6441 "IBM ThinkPad Embedded Controller -[%17c",
6442 ec_fw_string) == 1) {
6443 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6444 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006445
6446 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006447 if (!tp->ec_version_str)
6448 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006449 tp->ec_model = ec_fw_string[0]
6450 | (ec_fw_string[1] << 8);
6451 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006452 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006453 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006454
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006455 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6456 if (s && !strnicmp(s, "ThinkPad", 8)) {
6457 tp->model_str = kstrdup(s, GFP_KERNEL);
6458 if (!tp->model_str)
6459 return -ENOMEM;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006460 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006461
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006462 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6463 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6464 if (s && !tp->nummodel_str)
6465 return -ENOMEM;
6466
6467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468}
6469
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006470static int __init probe_for_thinkpad(void)
6471{
6472 int is_thinkpad;
6473
6474 if (acpi_disabled)
6475 return -ENODEV;
6476
6477 /*
6478 * Non-ancient models have better DMI tagging, but very old models
6479 * don't.
6480 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006481 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006482
6483 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006484 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006485 if (!ec_handle) {
6486 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006487 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006488 "Not yet supported ThinkPad detected!\n");
6489 return -ENODEV;
6490 }
6491
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006492 /*
6493 * Risks a regression on very old machines, but reduces potential
6494 * false positives a damn great deal
6495 */
6496 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006497 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006498
6499 if (!is_thinkpad && !force_load)
6500 return -ENODEV;
6501
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006502 return 0;
6503}
6504
6505
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006506/* Module init, exit, parameters */
6507
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006508static struct ibm_init_struct ibms_init[] __initdata = {
6509 {
6510 .init = thinkpad_acpi_driver_init,
6511 .data = &thinkpad_acpi_driver_data,
6512 },
6513 {
6514 .init = hotkey_init,
6515 .data = &hotkey_driver_data,
6516 },
6517 {
6518 .init = bluetooth_init,
6519 .data = &bluetooth_driver_data,
6520 },
6521 {
6522 .init = wan_init,
6523 .data = &wan_driver_data,
6524 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006525#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006526 {
6527 .init = video_init,
6528 .data = &video_driver_data,
6529 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006530#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006531 {
6532 .init = light_init,
6533 .data = &light_driver_data,
6534 },
6535#ifdef CONFIG_THINKPAD_ACPI_DOCK
6536 {
6537 .init = dock_init,
6538 .data = &dock_driver_data[0],
6539 },
6540 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03006541 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006542 .data = &dock_driver_data[1],
6543 },
6544#endif
6545#ifdef CONFIG_THINKPAD_ACPI_BAY
6546 {
6547 .init = bay_init,
6548 .data = &bay_driver_data,
6549 },
6550#endif
6551 {
6552 .init = cmos_init,
6553 .data = &cmos_driver_data,
6554 },
6555 {
6556 .init = led_init,
6557 .data = &led_driver_data,
6558 },
6559 {
6560 .init = beep_init,
6561 .data = &beep_driver_data,
6562 },
6563 {
6564 .init = thermal_init,
6565 .data = &thermal_driver_data,
6566 },
6567 {
6568 .data = &ecdump_driver_data,
6569 },
6570 {
6571 .init = brightness_init,
6572 .data = &brightness_driver_data,
6573 },
6574 {
6575 .data = &volume_driver_data,
6576 },
6577 {
6578 .init = fan_init,
6579 .data = &fan_driver_data,
6580 },
6581};
6582
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006583static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6584{
6585 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006586 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006587
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006588 if (!kp || !kp->name || !val)
6589 return -EINVAL;
6590
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006591 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6592 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006593 WARN_ON(ibm == NULL);
6594
6595 if (!ibm || !ibm->name)
6596 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006597
6598 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6599 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006600 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006601 strcpy(ibms_init[i].param, val);
6602 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006603 return 0;
6604 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006605 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006606
6607 return -EINVAL;
6608}
6609
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006610module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006611MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006612 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006613
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006614module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006615MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006616
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006617module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006618MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006619 "Attempts to load the driver even on a "
6620 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006621
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006622module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006623MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006624 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006625
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006626module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006627MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006628 "Selects brightness control strategy: "
6629 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006630
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006631module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006632MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006633 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006634
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006635module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006636MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006637 "used for backwards compatibility with userspace, "
6638 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006639
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006640#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006641 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006642 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006643 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006644
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006645TPACPI_PARAM(hotkey);
6646TPACPI_PARAM(bluetooth);
6647TPACPI_PARAM(video);
6648TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006649#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006650TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006651#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006652#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006653TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006654#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006655TPACPI_PARAM(cmos);
6656TPACPI_PARAM(led);
6657TPACPI_PARAM(beep);
6658TPACPI_PARAM(ecdump);
6659TPACPI_PARAM(brightness);
6660TPACPI_PARAM(volume);
6661TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006662
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006663static void thinkpad_acpi_module_exit(void)
6664{
6665 struct ibm_struct *ibm, *itmp;
6666
6667 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6668
6669 list_for_each_entry_safe_reverse(ibm, itmp,
6670 &tpacpi_all_drivers,
6671 all_drivers) {
6672 ibm_exit(ibm);
6673 }
6674
6675 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6676
6677 if (tpacpi_inputdev) {
6678 if (tp_features.input_device_registered)
6679 input_unregister_device(tpacpi_inputdev);
6680 else
6681 input_free_device(tpacpi_inputdev);
6682 }
6683
6684 if (tpacpi_hwmon)
6685 hwmon_device_unregister(tpacpi_hwmon);
6686
6687 if (tp_features.sensors_pdev_attrs_registered)
6688 device_remove_file(&tpacpi_sensors_pdev->dev,
6689 &dev_attr_thinkpad_acpi_pdev_name);
6690 if (tpacpi_sensors_pdev)
6691 platform_device_unregister(tpacpi_sensors_pdev);
6692 if (tpacpi_pdev)
6693 platform_device_unregister(tpacpi_pdev);
6694
6695 if (tp_features.sensors_pdrv_attrs_registered)
6696 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6697 if (tp_features.platform_drv_attrs_registered)
6698 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6699
6700 if (tp_features.sensors_pdrv_registered)
6701 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6702
6703 if (tp_features.platform_drv_registered)
6704 platform_driver_unregister(&tpacpi_pdriver);
6705
6706 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006707 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006708
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006709 if (tpacpi_wq)
6710 destroy_workqueue(tpacpi_wq);
6711
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006712 kfree(thinkpad_id.bios_version_str);
6713 kfree(thinkpad_id.ec_version_str);
6714 kfree(thinkpad_id.model_str);
6715}
6716
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006717
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006718static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719{
6720 int ret, i;
6721
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006722 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6723
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006724 /* Parameter checking */
6725 if (hotkey_report_mode > 2)
6726 return -EINVAL;
6727
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006728 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006729
Henrique de Moraes Holschuhbf20e742008-07-21 09:15:51 -03006730 ret = get_thinkpad_model_data(&thinkpad_id);
6731 if (ret) {
6732 printk(TPACPI_ERR
6733 "unable to get DMI data: %d\n", ret);
6734 thinkpad_acpi_module_exit();
6735 return ret;
6736 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006737 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006738 if (ret) {
6739 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006740 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006743 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006744
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006745 TPACPI_ACPIHANDLE_INIT(ecrd);
6746 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006748 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6749 if (!tpacpi_wq) {
6750 thinkpad_acpi_module_exit();
6751 return -ENOMEM;
6752 }
6753
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006754 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006756 printk(TPACPI_ERR
6757 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006758 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 return -ENODEV;
6760 }
6761 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006762
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006763 ret = platform_driver_register(&tpacpi_pdriver);
6764 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006765 printk(TPACPI_ERR
6766 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006767 thinkpad_acpi_module_exit();
6768 return ret;
6769 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006770 tp_features.platform_drv_registered = 1;
6771
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006772 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6773 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006774 printk(TPACPI_ERR
6775 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006776 thinkpad_acpi_module_exit();
6777 return ret;
6778 }
6779 tp_features.sensors_pdrv_registered = 1;
6780
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006781 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006782 if (!ret) {
6783 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006784 ret = tpacpi_create_driver_attributes(
6785 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006786 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006787 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006788 printk(TPACPI_ERR
6789 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006790 thinkpad_acpi_module_exit();
6791 return ret;
6792 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006793 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006794
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006795
6796 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006797 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006798 NULL, 0);
6799 if (IS_ERR(tpacpi_pdev)) {
6800 ret = PTR_ERR(tpacpi_pdev);
6801 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006802 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006803 thinkpad_acpi_module_exit();
6804 return ret;
6805 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006806 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006807 TPACPI_HWMON_DRVR_NAME,
6808 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006809 if (IS_ERR(tpacpi_sensors_pdev)) {
6810 ret = PTR_ERR(tpacpi_sensors_pdev);
6811 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006812 printk(TPACPI_ERR
6813 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006814 thinkpad_acpi_module_exit();
6815 return ret;
6816 }
6817 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6818 &dev_attr_thinkpad_acpi_pdev_name);
6819 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006820 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006821 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006822 thinkpad_acpi_module_exit();
6823 return ret;
6824 }
6825 tp_features.sensors_pdev_attrs_registered = 1;
6826 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006827 if (IS_ERR(tpacpi_hwmon)) {
6828 ret = PTR_ERR(tpacpi_hwmon);
6829 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006830 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006831 thinkpad_acpi_module_exit();
6832 return ret;
6833 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006834 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006835 tpacpi_inputdev = input_allocate_device();
6836 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006837 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006838 thinkpad_acpi_module_exit();
6839 return -ENOMEM;
6840 } else {
6841 /* Prepare input device, but don't register */
6842 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006843 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006844 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006845 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6846 thinkpad_id.vendor :
6847 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006848 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6849 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6850 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006851 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6852 ret = ibm_init(&ibms_init[i]);
6853 if (ret >= 0 && *ibms_init[i].param)
6854 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006856 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 return ret;
6858 }
6859 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006860 ret = input_register_device(tpacpi_inputdev);
6861 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006862 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006863 thinkpad_acpi_module_exit();
6864 return ret;
6865 } else {
6866 tp_features.input_device_registered = 1;
6867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006869 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 return 0;
6871}
6872
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006873/* Please remove this in year 2009 */
6874MODULE_ALIAS("ibm_acpi");
6875
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03006876MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6877
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006878/*
6879 * DMI matching for module autoloading
6880 *
6881 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6882 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6883 *
6884 * Only models listed in thinkwiki will be supported, so add yours
6885 * if it is not there yet.
6886 */
6887#define IBM_BIOS_MODULE_ALIAS(__type) \
6888 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6889
6890/* Non-ancient thinkpads */
6891MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6892MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6893
6894/* Ancient thinkpad BIOSes have to be identified by
6895 * BIOS type or model number, and there are far less
6896 * BIOS types than model numbers... */
6897IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6898IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6899IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6900
6901MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006902MODULE_DESCRIPTION(TPACPI_DESC);
6903MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006904MODULE_LICENSE("GPL");
6905
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006906module_init(thinkpad_acpi_module_init);
6907module_exit(thinkpad_acpi_module_exit);