blob: 5a3fb09f10d3f4ae5697b8b4124401a85fb15900 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh6a2e2932008-01-08 13:02:56 -02006 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh1cee5cc2008-01-08 13:02:57 -020024#define TPACPI_VERSION "0.19"
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -020025#define TPACPI_SYSFS_VERSION 0x020200
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
57#include <linux/kthread.h>
58#include <linux/freezer.h>
59#include <linux/delay.h>
60
61#include <linux/nvram.h>
62#include <linux/proc_fs.h>
63#include <linux/sysfs.h>
64#include <linux/backlight.h>
65#include <linux/fb.h>
66#include <linux/platform_device.h>
67#include <linux/hwmon.h>
68#include <linux/hwmon-sysfs.h>
69#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030070#include <linux/leds.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020071#include <asm/uaccess.h>
72
73#include <linux/dmi.h>
74#include <linux/jiffies.h>
75#include <linux/workqueue.h>
76
77#include <acpi/acpi_drivers.h>
78#include <acpi/acnamesp.h>
79
80#include <linux/pci_ids.h>
81
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020082
83/* ThinkPad CMOS commands */
84#define TP_CMOS_VOLUME_DOWN 0
85#define TP_CMOS_VOLUME_UP 1
86#define TP_CMOS_VOLUME_MUTE 2
87#define TP_CMOS_BRIGHTNESS_UP 4
88#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030089#define TP_CMOS_THINKLIGHT_ON 12
90#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020091
92/* NVRAM Addresses */
93enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
99};
100
101/* NVRAM bit masks */
102enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
116};
117
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200118/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200119#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200120
121/* Input IDs */
122#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123#define TPACPI_HKEY_INPUT_VERSION 0x4101
124
125
126/****************************************************************************
127 * Main driver
128 */
129
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200130#define TPACPI_NAME "thinkpad"
131#define TPACPI_DESC "ThinkPad ACPI Extras"
132#define TPACPI_FILE TPACPI_NAME "_acpi"
133#define TPACPI_URL "http://ibm-acpi.sf.net/"
134#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200135
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200136#define TPACPI_PROC_DIR "ibm"
137#define TPACPI_ACPI_EVENT_PREFIX "ibm"
138#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300139#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200140#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200141
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300142#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
143
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200144#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200145
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200146/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200147#define TPACPI_LOG TPACPI_FILE ": "
148#define TPACPI_ERR KERN_ERR TPACPI_LOG
149#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
150#define TPACPI_INFO KERN_INFO TPACPI_LOG
151#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200152
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200153#define TPACPI_DBG_ALL 0xffff
154#define TPACPI_DBG_ALL 0xffff
155#define TPACPI_DBG_INIT 0x0001
156#define TPACPI_DBG_EXIT 0x0002
157#define dbg_printk(a_dbg_level, format, arg...) \
158 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200159 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
160 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200161#ifdef CONFIG_THINKPAD_ACPI_DEBUG
162#define vdbg_printk(a_dbg_level, format, arg...) \
163 dbg_printk(a_dbg_level, format, ## arg)
164static const char *str_supported(int is_supported);
165#else
166#define vdbg_printk(a_dbg_level, format, arg...)
167#endif
168
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200169#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
170#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
171#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200172
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200173
174/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200175 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200176 */
177
178struct ibm_struct;
179
180struct tp_acpi_drv_struct {
181 const struct acpi_device_id *hid;
182 struct acpi_driver *driver;
183
184 void (*notify) (struct ibm_struct *, u32);
185 acpi_handle *handle;
186 u32 type;
187 struct acpi_device *device;
188};
189
190struct ibm_struct {
191 char *name;
192
193 int (*read) (char *);
194 int (*write) (char *);
195 void (*exit) (void);
196 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200197 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200198
199 struct list_head all_drivers;
200
201 struct tp_acpi_drv_struct *acpi;
202
203 struct {
204 u8 acpi_driver_registered:1;
205 u8 acpi_notify_installed:1;
206 u8 proc_created:1;
207 u8 init_called:1;
208 u8 experimental:1;
209 } flags;
210};
211
212struct ibm_init_struct {
213 char param[32];
214
215 int (*init) (struct ibm_init_struct *);
216 struct ibm_struct *data;
217};
218
219static struct {
220#ifdef CONFIG_THINKPAD_ACPI_BAY
221 u32 bay_status:1;
222 u32 bay_eject:1;
223 u32 bay_status2:1;
224 u32 bay_eject2:1;
225#endif
226 u32 bluetooth:1;
227 u32 hotkey:1;
228 u32 hotkey_mask:1;
229 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200230 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200231 u32 light:1;
232 u32 light_status:1;
233 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300234 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200235 u32 wan:1;
236 u32 fan_ctrl_status_undef:1;
237 u32 input_device_registered:1;
238 u32 platform_drv_registered:1;
239 u32 platform_drv_attrs_registered:1;
240 u32 sensors_pdrv_registered:1;
241 u32 sensors_pdrv_attrs_registered:1;
242 u32 sensors_pdev_attrs_registered:1;
243 u32 hotkey_poll_active:1;
244} tp_features;
245
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300246static struct {
247 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300248 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300249} tp_warned;
250
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251struct thinkpad_id_data {
252 unsigned int vendor; /* ThinkPad vendor:
253 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
254
255 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
256 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
257
258 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
259 u16 ec_model;
260
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300261 char *model_str; /* ThinkPad T43 */
262 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200263};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264static struct thinkpad_id_data thinkpad_id;
265
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300266static enum {
267 TPACPI_LIFE_INIT = 0,
268 TPACPI_LIFE_RUNNING,
269 TPACPI_LIFE_EXITING,
270} tpacpi_lifecycle;
271
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200272static int experimental;
273static u32 dbg_level;
274
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300275/* Special LED class that can defer work */
276struct tpacpi_led_classdev {
277 struct led_classdev led_classdev;
278 struct work_struct work;
279 enum led_brightness new_brightness;
280};
281
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300282/****************************************************************************
283 ****************************************************************************
284 *
285 * ACPI Helpers and device model
286 *
287 ****************************************************************************
288 ****************************************************************************/
289
290/*************************************************************************
291 * ACPI basic handles
292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300294static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200296#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 static acpi_handle object##_handle; \
298 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400299 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 static char *object##_paths[] = { paths }
301
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200302TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400303 "\\_SB.PCI.ISA.EC", /* 570 */
304 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
305 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
306 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
307 "\\_SB.PCI0.ICH3.EC0", /* R31 */
308 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300309 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200311TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
312TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200314TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
315 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400316 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
317 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300318 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400319
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200320TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400321 "^HKEY", /* R30, R31 */
322 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300323 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400324
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200325TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
326 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
327 "\\_SB.PCI0.VID0", /* 770e */
328 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
329 "\\_SB.PCI0.AGP.VID", /* all others */
330 ); /* R30, R31 */
331
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400332
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300333/*************************************************************************
334 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200335 */
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static int acpi_evalf(acpi_handle handle,
338 void *res, char *method, char *fmt, ...)
339{
340 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400341 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200342 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400343 struct acpi_buffer result, *resultp;
344 union acpi_object out_obj;
345 acpi_status status;
346 va_list ap;
347 char res_type;
348 int success;
349 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200352 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354 }
355
356 if (*fmt == 'q') {
357 quiet = 1;
358 fmt++;
359 } else
360 quiet = 0;
361
362 res_type = *(fmt++);
363
364 params.count = 0;
365 params.pointer = &in_objs[0];
366
367 va_start(ap, fmt);
368 while (*fmt) {
369 char c = *(fmt++);
370 switch (c) {
371 case 'd': /* int */
372 in_objs[params.count].integer.value = va_arg(ap, int);
373 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
374 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400375 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200377 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 "with invalid format character '%c'\n", c);
379 return 0;
380 }
381 }
382 va_end(ap);
383
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400384 if (res_type != 'v') {
385 result.length = sizeof(out_obj);
386 result.pointer = &out_obj;
387 resultp = &result;
388 } else
389 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400391 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400394 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (res)
396 *(int *)res = out_obj.integer.value;
397 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
398 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400399 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 success = status == AE_OK;
401 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400402 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200404 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 "with invalid format character '%c'\n", res_type);
406 return 0;
407 }
408
409 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200410 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 method, fmt0, status);
412
413 return success;
414}
415
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200416static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300417{
418 int v;
419
420 if (ecrd_handle) {
421 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
422 return 0;
423 *p = v;
424 } else {
425 if (ec_read(i, p) < 0)
426 return 0;
427 }
428
429 return 1;
430}
431
432static int acpi_ec_write(int i, u8 v)
433{
434 if (ecwr_handle) {
435 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
436 return 0;
437 } else {
438 if (ec_write(i, v) < 0)
439 return 0;
440 }
441
442 return 1;
443}
444
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200445#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300446static int _sta(acpi_handle handle)
447{
448 int status;
449
450 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
451 status = 0;
452
453 return status;
454}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200455#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300456
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300457static int issue_thinkpad_cmos_command(int cmos_cmd)
458{
459 if (!cmos_handle)
460 return -ENXIO;
461
462 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
463 return -EIO;
464
465 return 0;
466}
467
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300468/*************************************************************************
469 * ACPI device model
470 */
471
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200472#define TPACPI_ACPIHANDLE_INIT(object) \
473 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200474 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
475
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300476static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300477 acpi_handle *handle, acpi_handle parent,
478 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300479{
480 int i;
481 acpi_status status;
482
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300483 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
484 name);
485
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300486 for (i = 0; i < num_paths; i++) {
487 status = acpi_get_handle(parent, paths[i], handle);
488 if (ACPI_SUCCESS(status)) {
489 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300490 dbg_printk(TPACPI_DBG_INIT,
491 "Found ACPI handle %s for %s\n",
492 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300493 return;
494 }
495 }
496
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300497 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
498 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300499 *handle = NULL;
500}
501
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300502static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300503{
504 struct ibm_struct *ibm = data;
505
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300506 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
507 return;
508
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300509 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300510 return;
511
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300512 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300513}
514
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300515static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300516{
517 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300518 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300520 BUG_ON(!ibm->acpi);
521
522 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523 return 0;
524
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300525 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300526 "setting up ACPI notify for %s\n", ibm->name);
527
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300528 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
529 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200530 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300531 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300532 return -ENODEV;
533 }
534
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300535 acpi_driver_data(ibm->acpi->device) = ibm;
536 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200537 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300538 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300539
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300540 status = acpi_install_notify_handler(*ibm->acpi->handle,
541 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300542 if (ACPI_FAILURE(status)) {
543 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200544 printk(TPACPI_NOTICE
545 "another device driver is already "
546 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300547 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200548 printk(TPACPI_ERR
549 "acpi_install_notify_handler(%s) failed: %d\n",
550 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300551 }
552 return -ENODEV;
553 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300554 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300555 return 0;
556}
557
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300558static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300559{
560 return 0;
561}
562
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300563static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300564{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300565 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300566
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300567 dbg_printk(TPACPI_DBG_INIT,
568 "registering %s as an ACPI driver\n", ibm->name);
569
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300570 BUG_ON(!ibm->acpi);
571
572 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
573 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200574 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300575 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300576 }
577
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200578 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300579 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200580
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300581 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300582
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300583 rc = acpi_bus_register_driver(ibm->acpi->driver);
584 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200585 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200586 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300587 kfree(ibm->acpi->driver);
588 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300589 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300590 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300591
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300592 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300593}
594
595
596/****************************************************************************
597 ****************************************************************************
598 *
599 * Procfs Helpers
600 *
601 ****************************************************************************
602 ****************************************************************************/
603
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300604static int dispatch_procfs_read(char *page, char **start, off_t off,
605 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300606{
607 struct ibm_struct *ibm = data;
608 int len;
609
610 if (!ibm || !ibm->read)
611 return -EINVAL;
612
613 len = ibm->read(page);
614 if (len < 0)
615 return len;
616
617 if (len <= off + count)
618 *eof = 1;
619 *start = page + off;
620 len -= off;
621 if (len > count)
622 len = count;
623 if (len < 0)
624 len = 0;
625
626 return len;
627}
628
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300629static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200630 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300631 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300632{
633 struct ibm_struct *ibm = data;
634 char *kernbuf;
635 int ret;
636
637 if (!ibm || !ibm->write)
638 return -EINVAL;
639
640 kernbuf = kmalloc(count + 2, GFP_KERNEL);
641 if (!kernbuf)
642 return -ENOMEM;
643
644 if (copy_from_user(kernbuf, userbuf, count)) {
645 kfree(kernbuf);
646 return -EFAULT;
647 }
648
649 kernbuf[count] = 0;
650 strcat(kernbuf, ",");
651 ret = ibm->write(kernbuf);
652 if (ret == 0)
653 ret = count;
654
655 kfree(kernbuf);
656
657 return ret;
658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660static char *next_cmd(char **cmds)
661{
662 char *start = *cmds;
663 char *end;
664
665 while ((end = strchr(start, ',')) && end == start)
666 start = end + 1;
667
668 if (!end)
669 return NULL;
670
671 *end = 0;
672 *cmds = end + 1;
673 return start;
674}
675
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300676
677/****************************************************************************
678 ****************************************************************************
679 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300680 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300681 *
682 ****************************************************************************
683 ****************************************************************************/
684
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300685static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300686static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700687static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300688static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300689static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200690static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300691
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200692static int tpacpi_suspend_handler(struct platform_device *pdev,
693 pm_message_t state)
694{
695 struct ibm_struct *ibm, *itmp;
696
697 list_for_each_entry_safe(ibm, itmp,
698 &tpacpi_all_drivers,
699 all_drivers) {
700 if (ibm->suspend)
701 (ibm->suspend)(state);
702 }
703
704 return 0;
705}
706
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300707static int tpacpi_resume_handler(struct platform_device *pdev)
708{
709 struct ibm_struct *ibm, *itmp;
710
711 list_for_each_entry_safe(ibm, itmp,
712 &tpacpi_all_drivers,
713 all_drivers) {
714 if (ibm->resume)
715 (ibm->resume)();
716 }
717
718 return 0;
719}
720
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300721static struct platform_driver tpacpi_pdriver = {
722 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200723 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300724 .owner = THIS_MODULE,
725 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200726 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300727 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300728};
729
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300730static struct platform_driver tpacpi_hwmon_pdriver = {
731 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200732 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300733 .owner = THIS_MODULE,
734 },
735};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300736
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300737/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300738 * sysfs support helpers
739 */
740
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200741struct attribute_set {
742 unsigned int members, max_members;
743 struct attribute_group group;
744};
745
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300746struct attribute_set_obj {
747 struct attribute_set s;
748 struct attribute *a;
749} __attribute__((packed));
750
751static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200752 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300753{
754 struct attribute_set_obj *sobj;
755
756 if (max_members == 0)
757 return NULL;
758
759 /* Allocates space for implicit NULL at the end too */
760 sobj = kzalloc(sizeof(struct attribute_set_obj) +
761 max_members * sizeof(struct attribute *),
762 GFP_KERNEL);
763 if (!sobj)
764 return NULL;
765 sobj->s.max_members = max_members;
766 sobj->s.group.attrs = &sobj->a;
767 sobj->s.group.name = name;
768
769 return &sobj->s;
770}
771
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200772#define destroy_attr_set(_set) \
773 kfree(_set);
774
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300775/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200776static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300777{
778 if (!s || !attr)
779 return -EINVAL;
780
781 if (s->members >= s->max_members)
782 return -ENOMEM;
783
784 s->group.attrs[s->members] = attr;
785 s->members++;
786
787 return 0;
788}
789
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200790static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300791 struct attribute **attr,
792 unsigned int count)
793{
794 int i, res;
795
796 for (i = 0; i < count; i++) {
797 res = add_to_attr_set(s, attr[i]);
798 if (res)
799 return res;
800 }
801
802 return 0;
803}
804
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200805static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300806{
807 sysfs_remove_group(kobj, &s->group);
808 destroy_attr_set(s);
809}
810
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200811#define register_attr_set_with_sysfs(_attr_set, _kobj) \
812 sysfs_create_group(_kobj, &_attr_set->group)
813
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300814static int parse_strtoul(const char *buf,
815 unsigned long max, unsigned long *value)
816{
817 char *endp;
818
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300819 while (*buf && isspace(*buf))
820 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300821 *value = simple_strtoul(buf, &endp, 0);
822 while (*endp && isspace(*endp))
823 endp++;
824 if (*endp || *value > max)
825 return -EINVAL;
826
827 return 0;
828}
829
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300830static int __init tpacpi_query_bcl_levels(acpi_handle handle)
831{
832 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
833 union acpi_object *obj;
834 int rc;
835
836 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
837 obj = (union acpi_object *)buffer.pointer;
838 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
839 printk(TPACPI_ERR "Unknown _BCL data, "
840 "please report this to %s\n", TPACPI_MAIL);
841 rc = 0;
842 } else {
843 rc = obj->package.count;
844 }
845 } else {
846 return 0;
847 }
848
849 kfree(buffer.pointer);
850 return rc;
851}
852
853static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
854 u32 lvl, void *context, void **rv)
855{
856 char name[ACPI_PATH_SEGMENT_LENGTH];
857 struct acpi_buffer buffer = { sizeof(name), &name };
858
859 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
860 !strncmp("_BCL", name, sizeof(name) - 1)) {
861 BUG_ON(!rv || !*rv);
862 **(int **)rv = tpacpi_query_bcl_levels(handle);
863 return AE_CTRL_TERMINATE;
864 } else {
865 return AE_OK;
866 }
867}
868
869/*
870 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
871 */
872static int __init tpacpi_check_std_acpi_brightness_support(void)
873{
874 int status;
875 int bcl_levels = 0;
876 void *bcl_ptr = &bcl_levels;
877
878 if (!vid_handle) {
879 TPACPI_ACPIHANDLE_INIT(vid);
880 }
881 if (!vid_handle)
882 return 0;
883
884 /*
885 * Search for a _BCL method, and execute it. This is safe on all
886 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
887 * BIOS in ACPI backlight control mode. We do NOT have to care
888 * about calling the _BCL method in an enabled video device, any
889 * will do for our purposes.
890 */
891
892 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
893 tpacpi_acpi_walk_find_bcl, NULL,
894 &bcl_ptr);
895
896 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
897 tp_features.bright_acpimode = 1;
898 return (bcl_levels - 2);
899 }
900
901 return 0;
902}
903
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200904/*************************************************************************
905 * thinkpad-acpi driver attributes
906 */
907
908/* interface_version --------------------------------------------------- */
909static ssize_t tpacpi_driver_interface_version_show(
910 struct device_driver *drv,
911 char *buf)
912{
913 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
914}
915
916static DRIVER_ATTR(interface_version, S_IRUGO,
917 tpacpi_driver_interface_version_show, NULL);
918
919/* debug_level --------------------------------------------------------- */
920static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
921 char *buf)
922{
923 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
924}
925
926static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
927 const char *buf, size_t count)
928{
929 unsigned long t;
930
931 if (parse_strtoul(buf, 0xffff, &t))
932 return -EINVAL;
933
934 dbg_level = t;
935
936 return count;
937}
938
939static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
940 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
941
942/* version ------------------------------------------------------------- */
943static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
944 char *buf)
945{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200946 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
947 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200948}
949
950static DRIVER_ATTR(version, S_IRUGO,
951 tpacpi_driver_version_show, NULL);
952
953/* --------------------------------------------------------------------- */
954
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200955static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200956 &driver_attr_debug_level, &driver_attr_version,
957 &driver_attr_interface_version,
958};
959
960static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
961{
962 int i, res;
963
964 i = 0;
965 res = 0;
966 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
967 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
968 i++;
969 }
970
971 return res;
972}
973
974static void tpacpi_remove_driver_attributes(struct device_driver *drv)
975{
976 int i;
977
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200978 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200979 driver_remove_file(drv, tpacpi_driver_attributes[i]);
980}
981
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300982/****************************************************************************
983 ****************************************************************************
984 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300985 * Subdrivers
986 *
987 ****************************************************************************
988 ****************************************************************************/
989
990/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300991 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300992 */
993
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300994static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200996 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
997 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200999 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001000 (thinkpad_id.bios_version_str) ?
1001 thinkpad_id.bios_version_str : "unknown",
1002 (thinkpad_id.ec_version_str) ?
1003 thinkpad_id.ec_version_str : "unknown");
1004
1005 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001006 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001007 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1008 "IBM" : ((thinkpad_id.vendor ==
1009 PCI_VENDOR_ID_LENOVO) ?
1010 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001011 thinkpad_id.model_str,
1012 (thinkpad_id.nummodel_str) ?
1013 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016}
1017
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001018static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 int len = 0;
1021
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001022 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1023 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 return len;
1026}
1027
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001028static struct ibm_struct thinkpad_acpi_driver_data = {
1029 .name = "driver",
1030 .read = thinkpad_acpi_driver_read,
1031};
1032
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001033/*************************************************************************
1034 * Hotkey subdriver
1035 */
1036
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001037enum { /* hot key scan codes (derived from ACPI DSDT) */
1038 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1039 TP_ACPI_HOTKEYSCAN_FNF2,
1040 TP_ACPI_HOTKEYSCAN_FNF3,
1041 TP_ACPI_HOTKEYSCAN_FNF4,
1042 TP_ACPI_HOTKEYSCAN_FNF5,
1043 TP_ACPI_HOTKEYSCAN_FNF6,
1044 TP_ACPI_HOTKEYSCAN_FNF7,
1045 TP_ACPI_HOTKEYSCAN_FNF8,
1046 TP_ACPI_HOTKEYSCAN_FNF9,
1047 TP_ACPI_HOTKEYSCAN_FNF10,
1048 TP_ACPI_HOTKEYSCAN_FNF11,
1049 TP_ACPI_HOTKEYSCAN_FNF12,
1050 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1051 TP_ACPI_HOTKEYSCAN_FNINSERT,
1052 TP_ACPI_HOTKEYSCAN_FNDELETE,
1053 TP_ACPI_HOTKEYSCAN_FNHOME,
1054 TP_ACPI_HOTKEYSCAN_FNEND,
1055 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1056 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1057 TP_ACPI_HOTKEYSCAN_FNSPACE,
1058 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1059 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1060 TP_ACPI_HOTKEYSCAN_MUTE,
1061 TP_ACPI_HOTKEYSCAN_THINKPAD,
1062};
1063
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001064enum { /* Keys available through NVRAM polling */
1065 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1066 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1067};
1068
1069enum { /* Positions of some of the keys in hotkey masks */
1070 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1071 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1072 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1073 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1074 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1075 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1076 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1077 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1078 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1079 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1080 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1081};
1082
1083enum { /* NVRAM to ACPI HKEY group map */
1084 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1085 TP_ACPI_HKEY_ZOOM_MASK |
1086 TP_ACPI_HKEY_DISPSWTCH_MASK |
1087 TP_ACPI_HKEY_HIBERNATE_MASK,
1088 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1089 TP_ACPI_HKEY_BRGHTDWN_MASK,
1090 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1091 TP_ACPI_HKEY_VOLDWN_MASK |
1092 TP_ACPI_HKEY_MUTE_MASK,
1093};
1094
1095#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1096struct tp_nvram_state {
1097 u16 thinkpad_toggle:1;
1098 u16 zoom_toggle:1;
1099 u16 display_toggle:1;
1100 u16 thinklight_toggle:1;
1101 u16 hibernate_toggle:1;
1102 u16 displayexp_toggle:1;
1103 u16 display_state:1;
1104 u16 brightness_toggle:1;
1105 u16 volume_toggle:1;
1106 u16 mute:1;
1107
1108 u8 brightness_level;
1109 u8 volume_level;
1110};
1111
1112static struct task_struct *tpacpi_hotkey_task;
1113static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1114static int hotkey_poll_freq = 10; /* Hz */
1115static struct mutex hotkey_thread_mutex;
1116static struct mutex hotkey_thread_data_mutex;
1117static unsigned int hotkey_config_change;
1118
1119#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1120
1121#define hotkey_source_mask 0U
1122
1123#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1124
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001125static struct mutex hotkey_mutex;
1126
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001127static enum { /* Reasons for waking up */
1128 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1129 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1130 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1131} hotkey_wakeup_reason;
1132
1133static int hotkey_autosleep_ack;
1134
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001135static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001136static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001137static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001138static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001139static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001140
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001141static unsigned int hotkey_report_mode;
1142
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001143static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001144
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001145static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001146
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001147#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1148#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001149 do { \
1150 mutex_lock(&hotkey_thread_data_mutex); \
1151 hotkey_config_change++; \
1152 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001153#define HOTKEY_CONFIG_CRITICAL_END \
1154 mutex_unlock(&hotkey_thread_data_mutex);
1155#else
1156#define HOTKEY_CONFIG_CRITICAL_START
1157#define HOTKEY_CONFIG_CRITICAL_END
1158#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1159
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001160/* HKEY.MHKG() return bits */
1161#define TP_HOTKEY_TABLET_MASK (1 << 3)
1162
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001163static int hotkey_get_wlsw(int *status)
1164{
1165 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1166 return -EIO;
1167 return 0;
1168}
1169
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001170static int hotkey_get_tablet_mode(int *status)
1171{
1172 int s;
1173
1174 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1175 return -EIO;
1176
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001177 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1178 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001179}
1180
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001181/*
1182 * Call with hotkey_mutex held
1183 */
1184static int hotkey_mask_get(void)
1185{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001186 u32 m = 0;
1187
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001188 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001189 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001190 return -EIO;
1191 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001192 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001193
1194 return 0;
1195}
1196
1197/*
1198 * Call with hotkey_mutex held
1199 */
1200static int hotkey_mask_set(u32 mask)
1201{
1202 int i;
1203 int rc = 0;
1204
1205 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001206 if (!tp_warned.hotkey_mask_ff &&
1207 (mask == 0xffff || mask == 0xffffff ||
1208 mask == 0xffffffff)) {
1209 tp_warned.hotkey_mask_ff = 1;
1210 printk(TPACPI_NOTICE
1211 "setting the hotkey mask to 0x%08x is likely "
1212 "not the best way to go about it\n", mask);
1213 printk(TPACPI_NOTICE
1214 "please consider using the driver defaults, "
1215 "and refer to up-to-date thinkpad-acpi "
1216 "documentation\n");
1217 }
1218
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001219 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001220 for (i = 0; i < 32; i++) {
1221 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001222 /* enable in firmware mask only keys not in NVRAM
1223 * mode, but enable the key in the cached hotkey_mask
1224 * regardless of mode, or the key will end up
1225 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001226 if (!acpi_evalf(hkey_handle,
1227 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001228 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001229 rc = -EIO;
1230 break;
1231 } else {
1232 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1233 }
1234 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001235 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001236
1237 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001238 if (!hotkey_mask_get() && !rc &&
1239 (hotkey_mask & ~hotkey_source_mask) !=
1240 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001241 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001242 "requested hot key mask 0x%08x, but "
1243 "firmware forced it to 0x%08x\n",
1244 mask, hotkey_mask);
1245 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001246 } else {
1247#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1248 HOTKEY_CONFIG_CRITICAL_START
1249 hotkey_mask = mask & hotkey_source_mask;
1250 HOTKEY_CONFIG_CRITICAL_END
1251 hotkey_mask_get();
1252 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001253 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001254 "requested hot key mask 0x%08x, "
1255 "forced to 0x%08x (NVRAM poll mask is "
1256 "0x%08x): no firmware mask support\n",
1257 mask, hotkey_mask, hotkey_source_mask);
1258 }
1259#else
1260 hotkey_mask_get();
1261 rc = -ENXIO;
1262#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001263 }
1264
1265 return rc;
1266}
1267
1268static int hotkey_status_get(int *status)
1269{
1270 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1271 return -EIO;
1272
1273 return 0;
1274}
1275
1276static int hotkey_status_set(int status)
1277{
1278 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1279 return -EIO;
1280
1281 return 0;
1282}
1283
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001284static void tpacpi_input_send_radiosw(void)
1285{
1286 int wlsw;
1287
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001288 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001289 mutex_lock(&tpacpi_inputdev_send_mutex);
1290
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001291 input_report_switch(tpacpi_inputdev,
1292 SW_RADIO, !!wlsw);
1293 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001294
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001295 mutex_unlock(&tpacpi_inputdev_send_mutex);
1296 }
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001297}
1298
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001299static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001300{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001301 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001302
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001303 if (tp_features.hotkey_tablet &&
1304 !hotkey_get_tablet_mode(&state)) {
1305 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001306
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001307 input_report_switch(tpacpi_inputdev,
1308 SW_TABLET_MODE, !!state);
1309 input_sync(tpacpi_inputdev);
1310
1311 mutex_unlock(&tpacpi_inputdev_send_mutex);
1312 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001313}
1314
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001315static void tpacpi_input_send_key(unsigned int scancode)
1316{
1317 unsigned int keycode;
1318
1319 keycode = hotkey_keycode_map[scancode];
1320
1321 if (keycode != KEY_RESERVED) {
1322 mutex_lock(&tpacpi_inputdev_send_mutex);
1323
1324 input_report_key(tpacpi_inputdev, keycode, 1);
1325 if (keycode == KEY_UNKNOWN)
1326 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1327 scancode);
1328 input_sync(tpacpi_inputdev);
1329
1330 input_report_key(tpacpi_inputdev, keycode, 0);
1331 if (keycode == KEY_UNKNOWN)
1332 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1333 scancode);
1334 input_sync(tpacpi_inputdev);
1335
1336 mutex_unlock(&tpacpi_inputdev_send_mutex);
1337 }
1338}
1339
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001340#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1341static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1342
1343static void tpacpi_hotkey_send_key(unsigned int scancode)
1344{
1345 tpacpi_input_send_key(scancode);
1346 if (hotkey_report_mode < 2) {
1347 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1348 0x80, 0x1001 + scancode);
1349 }
1350}
1351
1352static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1353{
1354 u8 d;
1355
1356 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1357 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1358 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1359 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1360 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1361 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1362 }
1363 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1364 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1365 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1366 }
1367 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1368 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1369 n->displayexp_toggle =
1370 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1371 }
1372 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1373 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1374 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1375 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1376 n->brightness_toggle =
1377 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1378 }
1379 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1380 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1381 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1382 >> TP_NVRAM_POS_LEVEL_VOLUME;
1383 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1384 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1385 }
1386}
1387
1388#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001389 do { \
1390 if ((mask & (1 << __scancode)) && \
1391 oldn->__member != newn->__member) \
1392 tpacpi_hotkey_send_key(__scancode); \
1393 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001394
1395#define TPACPI_MAY_SEND_KEY(__scancode) \
1396 do { if (mask & (1 << __scancode)) \
1397 tpacpi_hotkey_send_key(__scancode); } while (0)
1398
1399static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001400 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001401 u32 mask)
1402{
1403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1407
1408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1409
1410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1411
1412 /* handle volume */
1413 if (oldn->volume_toggle != newn->volume_toggle) {
1414 if (oldn->mute != newn->mute) {
1415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1416 }
1417 if (oldn->volume_level > newn->volume_level) {
1418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1419 } else if (oldn->volume_level < newn->volume_level) {
1420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1421 } else if (oldn->mute == newn->mute) {
1422 /* repeated key presses that didn't change state */
1423 if (newn->mute) {
1424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1425 } else if (newn->volume_level != 0) {
1426 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1427 } else {
1428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1429 }
1430 }
1431 }
1432
1433 /* handle brightness */
1434 if (oldn->brightness_toggle != newn->brightness_toggle) {
1435 if (oldn->brightness_level < newn->brightness_level) {
1436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1437 } else if (oldn->brightness_level > newn->brightness_level) {
1438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1439 } else {
1440 /* repeated key presses that didn't change state */
1441 if (newn->brightness_level != 0) {
1442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1443 } else {
1444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1445 }
1446 }
1447 }
1448}
1449
1450#undef TPACPI_COMPARE_KEY
1451#undef TPACPI_MAY_SEND_KEY
1452
1453static int hotkey_kthread(void *data)
1454{
1455 struct tp_nvram_state s[2];
1456 u32 mask;
1457 unsigned int si, so;
1458 unsigned long t;
1459 unsigned int change_detector, must_reset;
1460
1461 mutex_lock(&hotkey_thread_mutex);
1462
1463 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1464 goto exit;
1465
1466 set_freezable();
1467
1468 so = 0;
1469 si = 1;
1470 t = 0;
1471
1472 /* Initial state for compares */
1473 mutex_lock(&hotkey_thread_data_mutex);
1474 change_detector = hotkey_config_change;
1475 mask = hotkey_source_mask & hotkey_mask;
1476 mutex_unlock(&hotkey_thread_data_mutex);
1477 hotkey_read_nvram(&s[so], mask);
1478
1479 while (!kthread_should_stop() && hotkey_poll_freq) {
1480 if (t == 0)
1481 t = 1000/hotkey_poll_freq;
1482 t = msleep_interruptible(t);
1483 if (unlikely(kthread_should_stop()))
1484 break;
1485 must_reset = try_to_freeze();
1486 if (t > 0 && !must_reset)
1487 continue;
1488
1489 mutex_lock(&hotkey_thread_data_mutex);
1490 if (must_reset || hotkey_config_change != change_detector) {
1491 /* forget old state on thaw or config change */
1492 si = so;
1493 t = 0;
1494 change_detector = hotkey_config_change;
1495 }
1496 mask = hotkey_source_mask & hotkey_mask;
1497 mutex_unlock(&hotkey_thread_data_mutex);
1498
1499 if (likely(mask)) {
1500 hotkey_read_nvram(&s[si], mask);
1501 if (likely(si != so)) {
1502 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001503 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001504 }
1505 }
1506
1507 so = si;
1508 si ^= 1;
1509 }
1510
1511exit:
1512 mutex_unlock(&hotkey_thread_mutex);
1513 return 0;
1514}
1515
1516static void hotkey_poll_stop_sync(void)
1517{
1518 if (tpacpi_hotkey_task) {
1519 if (frozen(tpacpi_hotkey_task) ||
1520 freezing(tpacpi_hotkey_task))
1521 thaw_process(tpacpi_hotkey_task);
1522
1523 kthread_stop(tpacpi_hotkey_task);
1524 tpacpi_hotkey_task = NULL;
1525 mutex_lock(&hotkey_thread_mutex);
1526 /* at this point, the thread did exit */
1527 mutex_unlock(&hotkey_thread_mutex);
1528 }
1529}
1530
1531/* call with hotkey_mutex held */
1532static void hotkey_poll_setup(int may_warn)
1533{
1534 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1535 hotkey_poll_freq > 0 &&
1536 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1537 if (!tpacpi_hotkey_task) {
1538 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001539 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001540 if (IS_ERR(tpacpi_hotkey_task)) {
1541 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001542 printk(TPACPI_ERR
1543 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001544 "for hotkey polling\n");
1545 }
1546 }
1547 } else {
1548 hotkey_poll_stop_sync();
1549 if (may_warn &&
1550 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001551 printk(TPACPI_NOTICE
1552 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001553 "which is currently disabled\n",
1554 hotkey_source_mask);
1555 }
1556 }
1557}
1558
1559static void hotkey_poll_setup_safe(int may_warn)
1560{
1561 mutex_lock(&hotkey_mutex);
1562 hotkey_poll_setup(may_warn);
1563 mutex_unlock(&hotkey_mutex);
1564}
1565
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001566#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1567
1568static void hotkey_poll_setup_safe(int __unused)
1569{
1570}
1571
1572#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1573
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001574static int hotkey_inputdev_open(struct input_dev *dev)
1575{
1576 switch (tpacpi_lifecycle) {
1577 case TPACPI_LIFE_INIT:
1578 /*
1579 * hotkey_init will call hotkey_poll_setup_safe
1580 * at the appropriate moment
1581 */
1582 return 0;
1583 case TPACPI_LIFE_EXITING:
1584 return -EBUSY;
1585 case TPACPI_LIFE_RUNNING:
1586 hotkey_poll_setup_safe(0);
1587 return 0;
1588 }
1589
1590 /* Should only happen if tpacpi_lifecycle is corrupt */
1591 BUG();
1592 return -EBUSY;
1593}
1594
1595static void hotkey_inputdev_close(struct input_dev *dev)
1596{
1597 /* disable hotkey polling when possible */
1598 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1599 hotkey_poll_setup_safe(0);
1600}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001601
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001602/* sysfs hotkey enable ------------------------------------------------- */
1603static ssize_t hotkey_enable_show(struct device *dev,
1604 struct device_attribute *attr,
1605 char *buf)
1606{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001607 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001608
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001609 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001610 if (res)
1611 return res;
1612
1613 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1614}
1615
1616static ssize_t hotkey_enable_store(struct device *dev,
1617 struct device_attribute *attr,
1618 const char *buf, size_t count)
1619{
1620 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001621 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001622
1623 if (parse_strtoul(buf, 1, &t))
1624 return -EINVAL;
1625
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001626 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001627
1628 return (res) ? res : count;
1629}
1630
1631static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001632 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001633 hotkey_enable_show, hotkey_enable_store);
1634
1635/* sysfs hotkey mask --------------------------------------------------- */
1636static ssize_t hotkey_mask_show(struct device *dev,
1637 struct device_attribute *attr,
1638 char *buf)
1639{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001640 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001641
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001642 if (mutex_lock_interruptible(&hotkey_mutex))
1643 return -ERESTARTSYS;
1644 res = hotkey_mask_get();
1645 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001646
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001647 return (res)?
1648 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001649}
1650
1651static ssize_t hotkey_mask_store(struct device *dev,
1652 struct device_attribute *attr,
1653 const char *buf, size_t count)
1654{
1655 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001656 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001657
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001658 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001659 return -EINVAL;
1660
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001661 if (mutex_lock_interruptible(&hotkey_mutex))
1662 return -ERESTARTSYS;
1663
1664 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001665
1666#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1667 hotkey_poll_setup(1);
1668#endif
1669
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001670 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001671
1672 return (res) ? res : count;
1673}
1674
1675static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001676 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001677 hotkey_mask_show, hotkey_mask_store);
1678
1679/* sysfs hotkey bios_enabled ------------------------------------------- */
1680static ssize_t hotkey_bios_enabled_show(struct device *dev,
1681 struct device_attribute *attr,
1682 char *buf)
1683{
1684 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1685}
1686
1687static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001688 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001689
1690/* sysfs hotkey bios_mask ---------------------------------------------- */
1691static ssize_t hotkey_bios_mask_show(struct device *dev,
1692 struct device_attribute *attr,
1693 char *buf)
1694{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001695 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001696}
1697
1698static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001699 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001700
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001701/* sysfs hotkey all_mask ----------------------------------------------- */
1702static ssize_t hotkey_all_mask_show(struct device *dev,
1703 struct device_attribute *attr,
1704 char *buf)
1705{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001706 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1707 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001708}
1709
1710static struct device_attribute dev_attr_hotkey_all_mask =
1711 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1712
1713/* sysfs hotkey recommended_mask --------------------------------------- */
1714static ssize_t hotkey_recommended_mask_show(struct device *dev,
1715 struct device_attribute *attr,
1716 char *buf)
1717{
1718 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001719 (hotkey_all_mask | hotkey_source_mask)
1720 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001721}
1722
1723static struct device_attribute dev_attr_hotkey_recommended_mask =
1724 __ATTR(hotkey_recommended_mask, S_IRUGO,
1725 hotkey_recommended_mask_show, NULL);
1726
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001727#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1728
1729/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1730static ssize_t hotkey_source_mask_show(struct device *dev,
1731 struct device_attribute *attr,
1732 char *buf)
1733{
1734 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1735}
1736
1737static ssize_t hotkey_source_mask_store(struct device *dev,
1738 struct device_attribute *attr,
1739 const char *buf, size_t count)
1740{
1741 unsigned long t;
1742
1743 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1744 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1745 return -EINVAL;
1746
1747 if (mutex_lock_interruptible(&hotkey_mutex))
1748 return -ERESTARTSYS;
1749
1750 HOTKEY_CONFIG_CRITICAL_START
1751 hotkey_source_mask = t;
1752 HOTKEY_CONFIG_CRITICAL_END
1753
1754 hotkey_poll_setup(1);
1755
1756 mutex_unlock(&hotkey_mutex);
1757
1758 return count;
1759}
1760
1761static struct device_attribute dev_attr_hotkey_source_mask =
1762 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1763 hotkey_source_mask_show, hotkey_source_mask_store);
1764
1765/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1766static ssize_t hotkey_poll_freq_show(struct device *dev,
1767 struct device_attribute *attr,
1768 char *buf)
1769{
1770 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1771}
1772
1773static ssize_t hotkey_poll_freq_store(struct device *dev,
1774 struct device_attribute *attr,
1775 const char *buf, size_t count)
1776{
1777 unsigned long t;
1778
1779 if (parse_strtoul(buf, 25, &t))
1780 return -EINVAL;
1781
1782 if (mutex_lock_interruptible(&hotkey_mutex))
1783 return -ERESTARTSYS;
1784
1785 hotkey_poll_freq = t;
1786
1787 hotkey_poll_setup(1);
1788 mutex_unlock(&hotkey_mutex);
1789
1790 return count;
1791}
1792
1793static struct device_attribute dev_attr_hotkey_poll_freq =
1794 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1795 hotkey_poll_freq_show, hotkey_poll_freq_store);
1796
1797#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1798
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001799/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001800static ssize_t hotkey_radio_sw_show(struct device *dev,
1801 struct device_attribute *attr,
1802 char *buf)
1803{
1804 int res, s;
1805 res = hotkey_get_wlsw(&s);
1806 if (res < 0)
1807 return res;
1808
1809 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1810}
1811
1812static struct device_attribute dev_attr_hotkey_radio_sw =
1813 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1814
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001815static void hotkey_radio_sw_notify_change(void)
1816{
1817 if (tp_features.hotkey_wlsw)
1818 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1819 "hotkey_radio_sw");
1820}
1821
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001822/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1823static ssize_t hotkey_tablet_mode_show(struct device *dev,
1824 struct device_attribute *attr,
1825 char *buf)
1826{
1827 int res, s;
1828 res = hotkey_get_tablet_mode(&s);
1829 if (res < 0)
1830 return res;
1831
1832 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1833}
1834
1835static struct device_attribute dev_attr_hotkey_tablet_mode =
1836 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1837
1838static void hotkey_tablet_mode_notify_change(void)
1839{
1840 if (tp_features.hotkey_tablet)
1841 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1842 "hotkey_tablet_mode");
1843}
1844
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001845/* sysfs hotkey report_mode -------------------------------------------- */
1846static ssize_t hotkey_report_mode_show(struct device *dev,
1847 struct device_attribute *attr,
1848 char *buf)
1849{
1850 return snprintf(buf, PAGE_SIZE, "%d\n",
1851 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1852}
1853
1854static struct device_attribute dev_attr_hotkey_report_mode =
1855 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1856
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001857/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001858static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1859 struct device_attribute *attr,
1860 char *buf)
1861{
1862 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1863}
1864
1865static struct device_attribute dev_attr_hotkey_wakeup_reason =
1866 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1867
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001868static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001869{
1870 if (tp_features.hotkey_mask)
1871 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1872 "wakeup_reason");
1873}
1874
1875/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001876static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1877 struct device_attribute *attr,
1878 char *buf)
1879{
1880 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1881}
1882
1883static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1884 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1885 hotkey_wakeup_hotunplug_complete_show, NULL);
1886
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001887static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001888{
1889 if (tp_features.hotkey_mask)
1890 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1891 "wakeup_hotunplug_complete");
1892}
1893
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001894/* --------------------------------------------------------------------- */
1895
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001896static struct attribute *hotkey_attributes[] __initdata = {
1897 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001898 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001899 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001900#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1901 &dev_attr_hotkey_mask.attr,
1902 &dev_attr_hotkey_all_mask.attr,
1903 &dev_attr_hotkey_recommended_mask.attr,
1904 &dev_attr_hotkey_source_mask.attr,
1905 &dev_attr_hotkey_poll_freq.attr,
1906#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001907};
1908
1909static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001910 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001911#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1912 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001913 &dev_attr_hotkey_all_mask.attr,
1914 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001915#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001916 &dev_attr_hotkey_wakeup_reason.attr,
1917 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001918};
1919
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001920static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001921{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001922 /* Requirements for changing the default keymaps:
1923 *
1924 * 1. Many of the keys are mapped to KEY_RESERVED for very
1925 * good reasons. Do not change them unless you have deep
1926 * knowledge on the IBM and Lenovo ThinkPad firmware for
1927 * the various ThinkPad models. The driver behaves
1928 * differently for KEY_RESERVED: such keys have their
1929 * hot key mask *unset* in mask_recommended, and also
1930 * in the initial hot key mask programmed into the
1931 * firmware at driver load time, which means the firm-
1932 * ware may react very differently if you change them to
1933 * something else;
1934 *
1935 * 2. You must be subscribed to the linux-thinkpad and
1936 * ibm-acpi-devel mailing lists, and you should read the
1937 * list archives since 2007 if you want to change the
1938 * keymaps. This requirement exists so that you will
1939 * know the past history of problems with the thinkpad-
1940 * acpi driver keymaps, and also that you will be
1941 * listening to any bug reports;
1942 *
1943 * 3. Do not send thinkpad-acpi specific patches directly to
1944 * for merging, *ever*. Send them to the linux-acpi
1945 * mailinglist for comments. Merging is to be done only
1946 * through acpi-test and the ACPI maintainer.
1947 *
1948 * If the above is too much to ask, don't change the keymap.
1949 * Ask the thinkpad-acpi maintainer to do it, instead.
1950 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001951 static u16 ibm_keycode_map[] __initdata = {
1952 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1953 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1954 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1955 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001956
1957 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001958 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1959 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1960 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001961
1962 /* brightness: firmware always reacts to them, unless
1963 * X.org did some tricks in the radeon BIOS scratch
1964 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001965 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001966 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001967
1968 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001969 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001970
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001971 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1972 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001973
1974 /* Volume: firmware always react to it and reprograms
1975 * the built-in *extra* mixer. Never map it to control
1976 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001977 KEY_RESERVED, /* 0x14: VOLUME UP */
1978 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1979 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001980
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001981 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001982
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001983 /* (assignments unknown, please report if found) */
1984 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1985 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1986 };
1987 static u16 lenovo_keycode_map[] __initdata = {
1988 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1989 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1990 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1991 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001992
1993 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001994 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1995 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1996 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001997
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03001998 /* These either have to go through ACPI video, or
1999 * act like in the IBM ThinkPads, so don't ever
2000 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002001 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002002 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002003
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002004 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002005
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002006 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2007 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002008
2009 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2010 * react to it and reprograms the built-in *extra* mixer.
2011 * Never map it to control another mixer by default.
2012 *
2013 * T60?, T61, R60?, R61: firmware and EC tries to send
2014 * these over the regular keyboard, so these are no-ops,
2015 * but there are still weird bugs re. MUTE, so do not
2016 * change unless you get test reports from all Lenovo
2017 * models. May cause the BIOS to interfere with the
2018 * HDA mixer.
2019 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002020 KEY_RESERVED, /* 0x14: VOLUME UP */
2021 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2022 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002023
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002024 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002025
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002026 /* (assignments unknown, please report if found) */
2027 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2028 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2029 };
2030
2031#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2032#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2033#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2034
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002035 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002036 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002037 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002038
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002039 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2040
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002041 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002042 BUG_ON(tpacpi_inputdev->open != NULL ||
2043 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002044
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002045 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002046 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002047
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002048#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2049 mutex_init(&hotkey_thread_mutex);
2050 mutex_init(&hotkey_thread_data_mutex);
2051#endif
2052
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002053 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002054 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002055
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002056 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002057 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002058
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002059 if (tp_features.hotkey) {
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002060 hotkey_dev_attributes = create_attr_set(13, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002061 if (!hotkey_dev_attributes)
2062 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002063 res = add_many_to_attr_set(hotkey_dev_attributes,
2064 hotkey_attributes,
2065 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002066 if (res)
2067 return res;
2068
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002069 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002070 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2071 for HKEY interface version 0x100 */
2072 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2073 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002074 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002075 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002076 printk(TPACPI_ERR "please report this to %s\n",
2077 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002078 } else {
2079 /*
2080 * MHKV 0x100 in A31, R40, R40e,
2081 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002082 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002083 tp_features.hotkey_mask = 1;
2084 }
2085 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002086
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002087 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002088 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002089
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002090 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002091 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002092 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002093 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002094 "missing MHKA handler, "
2095 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002096 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002097 /* FN+F12, FN+F4, FN+F3 */
2098 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002099 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002100 }
2101
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002102 /* hotkey_source_mask *must* be zero for
2103 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002104 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002105 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002106 res = hotkey_mask_get();
2107 hotkey_orig_mask = hotkey_mask;
2108 if (!res) {
2109 res = add_many_to_attr_set(
2110 hotkey_dev_attributes,
2111 hotkey_mask_attributes,
2112 ARRAY_SIZE(hotkey_mask_attributes));
2113 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002114 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002115
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002116#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2117 if (tp_features.hotkey_mask) {
2118 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2119 & ~hotkey_all_mask;
2120 } else {
2121 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2122 }
2123
2124 vdbg_printk(TPACPI_DBG_INIT,
2125 "hotkey source mask 0x%08x, polling freq %d\n",
2126 hotkey_source_mask, hotkey_poll_freq);
2127#endif
2128
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002129 /* Not all thinkpads have a hardware radio switch */
2130 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2131 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002132 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002133 "radio switch found; radios are %s\n",
2134 enabled(status, 0));
2135 res = add_to_attr_set(hotkey_dev_attributes,
2136 &dev_attr_hotkey_radio_sw.attr);
2137 }
2138
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002139 /* For X41t, X60t, X61t Tablets... */
2140 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2141 tp_features.hotkey_tablet = 1;
2142 printk(TPACPI_INFO
2143 "possible tablet mode switch found; "
2144 "ThinkPad in %s mode\n",
2145 (status & TP_HOTKEY_TABLET_MASK)?
2146 "tablet" : "laptop");
2147 res = add_to_attr_set(hotkey_dev_attributes,
2148 &dev_attr_hotkey_tablet_mode.attr);
2149 }
2150
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002151 if (!res)
2152 res = register_attr_set_with_sysfs(
2153 hotkey_dev_attributes,
2154 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002155 if (res)
2156 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002157
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002158 /* Set up key map */
2159
2160 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2161 GFP_KERNEL);
2162 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002163 printk(TPACPI_ERR
2164 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002165 return -ENOMEM;
2166 }
2167
2168 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2169 dbg_printk(TPACPI_DBG_INIT,
2170 "using Lenovo default hot key map\n");
2171 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2172 TPACPI_HOTKEY_MAP_SIZE);
2173 } else {
2174 dbg_printk(TPACPI_DBG_INIT,
2175 "using IBM default hot key map\n");
2176 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2177 TPACPI_HOTKEY_MAP_SIZE);
2178 }
2179
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002180 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2181 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2182 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002183 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2184 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2185 tpacpi_inputdev->keycode = hotkey_keycode_map;
2186 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002187 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2188 set_bit(hotkey_keycode_map[i],
2189 tpacpi_inputdev->keybit);
2190 } else {
2191 if (i < sizeof(hotkey_reserved_mask)*8)
2192 hotkey_reserved_mask |= 1 << i;
2193 }
2194 }
2195
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002196 if (tp_features.hotkey_wlsw) {
2197 set_bit(EV_SW, tpacpi_inputdev->evbit);
2198 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2199 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002200 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002201 set_bit(EV_SW, tpacpi_inputdev->evbit);
2202 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2203 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002204
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002205 /* Do not issue duplicate brightness change events to
2206 * userspace */
2207 if (!tp_features.bright_acpimode)
2208 /* update bright_acpimode... */
2209 tpacpi_check_std_acpi_brightness_support();
2210
2211 if (tp_features.bright_acpimode) {
2212 printk(TPACPI_INFO
2213 "This ThinkPad has standard ACPI backlight "
2214 "brightness control, supported by the ACPI "
2215 "video driver\n");
2216 printk(TPACPI_NOTICE
2217 "Disabling thinkpad-acpi brightness events "
2218 "by default...\n");
2219
2220 /* The hotkey_reserved_mask change below is not
2221 * necessary while the keys are at KEY_RESERVED in the
2222 * default map, but better safe than sorry, leave it
2223 * here as a marker of what we have to do, especially
2224 * when we finally become able to set this at runtime
2225 * on response to X.org requests */
2226 hotkey_reserved_mask |=
2227 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2228 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2229 }
2230
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002231 dbg_printk(TPACPI_DBG_INIT,
2232 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002233 res = hotkey_status_set(1);
2234 if (res)
2235 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002236 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2237 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002238 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002239 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002240 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002241
2242 dbg_printk(TPACPI_DBG_INIT,
2243 "legacy hot key reporting over procfs %s\n",
2244 (hotkey_report_mode < 2) ?
2245 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002246
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002247 tpacpi_inputdev->open = &hotkey_inputdev_open;
2248 tpacpi_inputdev->close = &hotkey_inputdev_close;
2249
2250 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002251 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002252 tpacpi_input_send_tabletsw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002253 }
2254
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002255 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002256}
2257
2258static void hotkey_exit(void)
2259{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002260#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2261 hotkey_poll_stop_sync();
2262#endif
2263
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002264 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002265 dbg_printk(TPACPI_DBG_EXIT,
2266 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002267 /* no short-circuit boolean operator below! */
2268 if ((hotkey_mask_set(hotkey_orig_mask) |
2269 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002270 printk(TPACPI_ERR
2271 "failed to restore hot key mask "
2272 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002273 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002274
2275 if (hotkey_dev_attributes) {
2276 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2277 hotkey_dev_attributes = NULL;
2278 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002279}
2280
2281static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2282{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002283 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002284 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002285 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002286 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002287 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002288
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002289 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002290 printk(TPACPI_ERR
2291 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002292 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002293 acpi_bus_generate_netlink_event(
2294 ibm->acpi->device->pnp.device_class,
2295 ibm->acpi->device->dev.bus_id,
2296 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002297 return;
2298 }
2299
2300 while (1) {
2301 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002302 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002303 return;
2304 }
2305
2306 if (hkey == 0) {
2307 /* queue empty */
2308 return;
2309 }
2310
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002311 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002312 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002313 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002314
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002315 switch (hkey >> 12) {
2316 case 1:
2317 /* 0x1000-0x1FFF: key presses */
2318 scancode = hkey & 0xfff;
2319 if (scancode > 0 && scancode < 0x21) {
2320 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002321 if (!(hotkey_source_mask & (1 << scancode))) {
2322 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002323 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002324 } else {
2325 ignore_acpi_ev = 1;
2326 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002327 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002328 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002329 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002330 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002331 case 2:
2332 /* Wakeup reason */
2333 switch (hkey) {
2334 case 0x2304: /* suspend, undock */
2335 case 0x2404: /* hibernation, undock */
2336 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2337 ignore_acpi_ev = 1;
2338 break;
2339 case 0x2305: /* suspend, bay eject */
2340 case 0x2405: /* hibernation, bay eject */
2341 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2342 ignore_acpi_ev = 1;
2343 break;
2344 default:
2345 unk_ev = 1;
2346 }
2347 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2348 printk(TPACPI_INFO
2349 "woke up due to a hot-unplug "
2350 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002351 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002352 }
2353 break;
2354 case 3:
2355 /* bay-related wakeups */
2356 if (hkey == 0x3003) {
2357 hotkey_autosleep_ack = 1;
2358 printk(TPACPI_INFO
2359 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002360 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002361 } else {
2362 unk_ev = 1;
2363 }
2364 break;
2365 case 4:
2366 /* dock-related wakeups */
2367 if (hkey == 0x4003) {
2368 hotkey_autosleep_ack = 1;
2369 printk(TPACPI_INFO
2370 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002371 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002372 } else {
2373 unk_ev = 1;
2374 }
2375 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002376 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002377 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002378 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002379 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002380 case 0x500b: /* X61t: tablet pen inserted into bay */
2381 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002382 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002383 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2384 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2385 tpacpi_input_send_tabletsw();
2386 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002387 send_acpi_ev = 0;
2388 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002389 case 0x5001:
2390 case 0x5002:
2391 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002392 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002393 break;
2394 default:
2395 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002396 }
2397 break;
2398 case 7:
2399 /* 0x7000-0x7FFF: misc */
2400 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2401 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002402 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002403 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002404 break;
2405 }
2406 /* fallthrough to default */
2407 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002408 unk_ev = 1;
2409 }
2410 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002411 printk(TPACPI_NOTICE
2412 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002413 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002414
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002415 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002416 if (!ignore_acpi_ev &&
2417 (send_acpi_ev || hotkey_report_mode < 2)) {
2418 acpi_bus_generate_proc_event(ibm->acpi->device,
2419 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002420 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002421
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002422 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002423 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002424 acpi_bus_generate_netlink_event(
2425 ibm->acpi->device->pnp.device_class,
2426 ibm->acpi->device->dev.bus_id,
2427 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002428 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002429 }
2430}
2431
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002432static void hotkey_suspend(pm_message_t state)
2433{
2434 /* Do these on suspend, we get the events on early resume! */
2435 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2436 hotkey_autosleep_ack = 0;
2437}
2438
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002439static void hotkey_resume(void)
2440{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002441 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002442 printk(TPACPI_ERR
2443 "error while trying to read hot key mask "
2444 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002445 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002446 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002447 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002448 hotkey_wakeup_reason_notify_change();
2449 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002450 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002451}
2452
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002453/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002454static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002456 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 int len = 0;
2458
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002459 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002460 len += sprintf(p + len, "status:\t\tnot supported\n");
2461 return len;
2462 }
2463
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002464 if (mutex_lock_interruptible(&hotkey_mutex))
2465 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002466 res = hotkey_status_get(&status);
2467 if (!res)
2468 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002469 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002470 if (res)
2471 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002474 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002475 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 len += sprintf(p + len,
2477 "commands:\tenable, disable, reset, <mask>\n");
2478 } else {
2479 len += sprintf(p + len, "mask:\t\tnot supported\n");
2480 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2481 }
2482
2483 return len;
2484}
2485
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002486static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002488 int res, status;
2489 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002492 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return -ENODEV;
2494
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002495 if (mutex_lock_interruptible(&hotkey_mutex))
2496 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002497
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002498 status = -1;
2499 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002500
2501 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 while ((cmd = next_cmd(&buf))) {
2503 if (strlencmp(cmd, "enable") == 0) {
2504 status = 1;
2505 } else if (strlencmp(cmd, "disable") == 0) {
2506 status = 0;
2507 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002508 status = hotkey_orig_status;
2509 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2511 /* mask set */
2512 } else if (sscanf(cmd, "%x", &mask) == 1) {
2513 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002514 } else {
2515 res = -EINVAL;
2516 goto errexit;
2517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002519 if (status != -1)
2520 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002522 if (!res && mask != hotkey_mask)
2523 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002525errexit:
2526 mutex_unlock(&hotkey_mutex);
2527 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002528}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002530static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002531 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002532 {"", 0},
2533};
2534
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002535static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002536 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002537 .notify = hotkey_notify,
2538 .handle = &hkey_handle,
2539 .type = ACPI_DEVICE_NOTIFY,
2540};
2541
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002542static struct ibm_struct hotkey_driver_data = {
2543 .name = "hotkey",
2544 .read = hotkey_read,
2545 .write = hotkey_write,
2546 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002547 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002548 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002549 .acpi = &ibm_hotkey_acpidriver,
2550};
2551
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002552/*************************************************************************
2553 * Bluetooth subdriver
2554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002556enum {
2557 /* ACPI GBDC/SBDC bits */
2558 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2559 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2560 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2561};
2562
2563static int bluetooth_get_radiosw(void);
2564static int bluetooth_set_radiosw(int radio_on);
2565
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002566/* sysfs bluetooth enable ---------------------------------------------- */
2567static ssize_t bluetooth_enable_show(struct device *dev,
2568 struct device_attribute *attr,
2569 char *buf)
2570{
2571 int status;
2572
2573 status = bluetooth_get_radiosw();
2574 if (status < 0)
2575 return status;
2576
2577 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2578}
2579
2580static ssize_t bluetooth_enable_store(struct device *dev,
2581 struct device_attribute *attr,
2582 const char *buf, size_t count)
2583{
2584 unsigned long t;
2585 int res;
2586
2587 if (parse_strtoul(buf, 1, &t))
2588 return -EINVAL;
2589
2590 res = bluetooth_set_radiosw(t);
2591
2592 return (res) ? res : count;
2593}
2594
2595static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002596 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002597 bluetooth_enable_show, bluetooth_enable_store);
2598
2599/* --------------------------------------------------------------------- */
2600
2601static struct attribute *bluetooth_attributes[] = {
2602 &dev_attr_bluetooth_enable.attr,
2603 NULL
2604};
2605
2606static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002607 .attrs = bluetooth_attributes,
2608};
2609
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002610static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002612 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002613 int status = 0;
2614
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002615 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2616
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002617 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002618
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002619 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2620 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002621 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002622 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002624 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2625 str_supported(tp_features.bluetooth),
2626 status);
2627
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002628 if (tp_features.bluetooth) {
2629 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2630 /* no bluetooth hardware present in system */
2631 tp_features.bluetooth = 0;
2632 dbg_printk(TPACPI_DBG_INIT,
2633 "bluetooth hardware not installed\n");
2634 } else {
2635 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2636 &bluetooth_attr_group);
2637 if (res)
2638 return res;
2639 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002640 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002641
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002642 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
2644
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002645static void bluetooth_exit(void)
2646{
2647 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2648 &bluetooth_attr_group);
2649}
2650
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002651static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
2653 int status;
2654
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002655 if (!tp_features.bluetooth)
2656 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002658 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2659 return -EIO;
2660
2661 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2662}
2663
2664static int bluetooth_set_radiosw(int radio_on)
2665{
2666 int status;
2667
2668 if (!tp_features.bluetooth)
2669 return -ENODEV;
2670
2671 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2672 return -EIO;
2673 if (radio_on)
2674 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2675 else
2676 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2677 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2678 return -EIO;
2679
2680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002683/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002684static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
2686 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002687 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002689 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002692 len += sprintf(p + len, "status:\t\t%s\n",
2693 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 len += sprintf(p + len, "commands:\tenable, disable\n");
2695 }
2696
2697 return len;
2698}
2699
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002700static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002704 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002705 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 while ((cmd = next_cmd(&buf))) {
2708 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002709 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002711 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 } else
2713 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return 0;
2717}
2718
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002719static struct ibm_struct bluetooth_driver_data = {
2720 .name = "bluetooth",
2721 .read = bluetooth_read,
2722 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002723 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002724};
2725
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002726/*************************************************************************
2727 * Wan subdriver
2728 */
2729
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002730enum {
2731 /* ACPI GWAN/SWAN bits */
2732 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2733 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2734 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2735};
2736
2737static int wan_get_radiosw(void);
2738static int wan_set_radiosw(int radio_on);
2739
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002740/* sysfs wan enable ---------------------------------------------------- */
2741static ssize_t wan_enable_show(struct device *dev,
2742 struct device_attribute *attr,
2743 char *buf)
2744{
2745 int status;
2746
2747 status = wan_get_radiosw();
2748 if (status < 0)
2749 return status;
2750
2751 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2752}
2753
2754static ssize_t wan_enable_store(struct device *dev,
2755 struct device_attribute *attr,
2756 const char *buf, size_t count)
2757{
2758 unsigned long t;
2759 int res;
2760
2761 if (parse_strtoul(buf, 1, &t))
2762 return -EINVAL;
2763
2764 res = wan_set_radiosw(t);
2765
2766 return (res) ? res : count;
2767}
2768
2769static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002770 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002771 wan_enable_show, wan_enable_store);
2772
2773/* --------------------------------------------------------------------- */
2774
2775static struct attribute *wan_attributes[] = {
2776 &dev_attr_wan_enable.attr,
2777 NULL
2778};
2779
2780static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002781 .attrs = wan_attributes,
2782};
2783
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002784static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002785{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002786 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002787 int status = 0;
2788
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002789 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2790
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002791 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002792
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002793 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002794 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002795
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002796 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2797 str_supported(tp_features.wan),
2798 status);
2799
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002800 if (tp_features.wan) {
2801 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2802 /* no wan hardware present in system */
2803 tp_features.wan = 0;
2804 dbg_printk(TPACPI_DBG_INIT,
2805 "wan hardware not installed\n");
2806 } else {
2807 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2808 &wan_attr_group);
2809 if (res)
2810 return res;
2811 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002812 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002813
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002814 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002815}
2816
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002817static void wan_exit(void)
2818{
2819 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2820 &wan_attr_group);
2821}
2822
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002823static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002824{
2825 int status;
2826
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002827 if (!tp_features.wan)
2828 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002829
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002830 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2831 return -EIO;
2832
2833 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2834}
2835
2836static int wan_set_radiosw(int radio_on)
2837{
2838 int status;
2839
2840 if (!tp_features.wan)
2841 return -ENODEV;
2842
2843 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2844 return -EIO;
2845 if (radio_on)
2846 status |= TP_ACPI_WANCARD_RADIOSSW;
2847 else
2848 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2849 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2850 return -EIO;
2851
2852 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002853}
2854
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002855/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002856static int wan_read(char *p)
2857{
2858 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002859 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002860
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002861 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002862 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002863 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002864 len += sprintf(p + len, "status:\t\t%s\n",
2865 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002866 len += sprintf(p + len, "commands:\tenable, disable\n");
2867 }
2868
2869 return len;
2870}
2871
2872static int wan_write(char *buf)
2873{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002874 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002875
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002876 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002877 return -ENODEV;
2878
2879 while ((cmd = next_cmd(&buf))) {
2880 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002881 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002882 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002883 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002884 } else
2885 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002886 }
2887
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002888 return 0;
2889}
2890
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002891static struct ibm_struct wan_driver_data = {
2892 .name = "wan",
2893 .read = wan_read,
2894 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002895 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002896 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002897};
2898
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002899/*************************************************************************
2900 * Video subdriver
2901 */
2902
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002903#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2904
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002905enum video_access_mode {
2906 TPACPI_VIDEO_NONE = 0,
2907 TPACPI_VIDEO_570, /* 570 */
2908 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2909 TPACPI_VIDEO_NEW, /* all others */
2910};
2911
2912enum { /* video status flags, based on VIDEO_570 */
2913 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2914 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2915 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2916};
2917
2918enum { /* TPACPI_VIDEO_570 constants */
2919 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2920 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2921 * video_status_flags */
2922 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2923 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2924};
2925
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002926static enum video_access_mode video_supported;
2927static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002928
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002929static int video_autosw_get(void);
2930static int video_autosw_set(int enable);
2931
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002932TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002933
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002934static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002936 int ivga;
2937
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002938 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2939
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002940 TPACPI_ACPIHANDLE_INIT(vid);
2941 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002942
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002943 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2944 /* G41, assume IVGA doesn't change */
2945 vid_handle = vid2_handle;
2946
2947 if (!vid_handle)
2948 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002949 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002950 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2951 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002952 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002953 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2954 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002955 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002956 else
2957 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002958 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002960 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2961 str_supported(video_supported != TPACPI_VIDEO_NONE),
2962 video_supported);
2963
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002964 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002967static void video_exit(void)
2968{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002969 dbg_printk(TPACPI_DBG_EXIT,
2970 "restoring original video autoswitch mode\n");
2971 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002972 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002973 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002974}
2975
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002976static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
2978 int status = 0;
2979 int i;
2980
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002981 switch (video_supported) {
2982 case TPACPI_VIDEO_570:
2983 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2984 TP_ACPI_VIDEO_570_PHSCMD))
2985 return -EIO;
2986 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2987 break;
2988 case TPACPI_VIDEO_770:
2989 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2990 return -EIO;
2991 if (i)
2992 status |= TP_ACPI_VIDEO_S_LCD;
2993 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2994 return -EIO;
2995 if (i)
2996 status |= TP_ACPI_VIDEO_S_CRT;
2997 break;
2998 case TPACPI_VIDEO_NEW:
2999 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3000 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3001 return -EIO;
3002 if (i)
3003 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003005 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3006 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3007 return -EIO;
3008 if (i)
3009 status |= TP_ACPI_VIDEO_S_LCD;
3010 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3011 return -EIO;
3012 if (i)
3013 status |= TP_ACPI_VIDEO_S_DVI;
3014 break;
3015 default:
3016 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019 return status;
3020}
3021
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003022static int video_outputsw_set(int status)
3023{
3024 int autosw;
3025 int res = 0;
3026
3027 switch (video_supported) {
3028 case TPACPI_VIDEO_570:
3029 res = acpi_evalf(NULL, NULL,
3030 "\\_SB.PHS2", "vdd",
3031 TP_ACPI_VIDEO_570_PHS2CMD,
3032 status | TP_ACPI_VIDEO_570_PHS2SET);
3033 break;
3034 case TPACPI_VIDEO_770:
3035 autosw = video_autosw_get();
3036 if (autosw < 0)
3037 return autosw;
3038
3039 res = video_autosw_set(1);
3040 if (res)
3041 return res;
3042 res = acpi_evalf(vid_handle, NULL,
3043 "ASWT", "vdd", status * 0x100, 0);
3044 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003045 printk(TPACPI_ERR
3046 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003047 return -EIO;
3048 }
3049 break;
3050 case TPACPI_VIDEO_NEW:
3051 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003052 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003053 break;
3054 default:
3055 return -ENOSYS;
3056 }
3057
3058 return (res)? 0 : -EIO;
3059}
3060
3061static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003063 int autosw = 0;
3064
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003065 switch (video_supported) {
3066 case TPACPI_VIDEO_570:
3067 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3068 return -EIO;
3069 break;
3070 case TPACPI_VIDEO_770:
3071 case TPACPI_VIDEO_NEW:
3072 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3073 return -EIO;
3074 break;
3075 default:
3076 return -ENOSYS;
3077 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003078
3079 return autosw & 1;
3080}
3081
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003082static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003083{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003084 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003085 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003086 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003087}
3088
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003089static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003090{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003091 int autosw = video_autosw_get();
3092 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003093
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003094 if (autosw < 0)
3095 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003096
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003097 switch (video_supported) {
3098 case TPACPI_VIDEO_570:
3099 res = video_autosw_set(1);
3100 if (res)
3101 return res;
3102 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3103 break;
3104 case TPACPI_VIDEO_770:
3105 case TPACPI_VIDEO_NEW:
3106 res = video_autosw_set(1);
3107 if (res)
3108 return res;
3109 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3110 break;
3111 default:
3112 return -ENOSYS;
3113 }
3114 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003115 printk(TPACPI_ERR
3116 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003117 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003118 }
3119
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003120 return (res)? 0 : -EIO;
3121}
3122
3123static int video_expand_toggle(void)
3124{
3125 switch (video_supported) {
3126 case TPACPI_VIDEO_570:
3127 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3128 0 : -EIO;
3129 case TPACPI_VIDEO_770:
3130 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3131 0 : -EIO;
3132 case TPACPI_VIDEO_NEW:
3133 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3134 0 : -EIO;
3135 default:
3136 return -ENOSYS;
3137 }
3138 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003139}
3140
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003141static int video_read(char *p)
3142{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003143 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003144 int len = 0;
3145
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003146 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003147 len += sprintf(p + len, "status:\t\tnot supported\n");
3148 return len;
3149 }
3150
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003151 status = video_outputsw_get();
3152 if (status < 0)
3153 return status;
3154
3155 autosw = video_autosw_get();
3156 if (autosw < 0)
3157 return autosw;
3158
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003159 len += sprintf(p + len, "status:\t\tsupported\n");
3160 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3161 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003162 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003163 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3164 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3165 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3166 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003167 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003168 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3169 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3170 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3171
3172 return len;
3173}
3174
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003175static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
3177 char *cmd;
3178 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003179 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003181 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003182 return -ENODEV;
3183
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003184 enable = 0;
3185 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 while ((cmd = next_cmd(&buf))) {
3188 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003189 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003191 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003193 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003195 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003196 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003197 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003198 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003199 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003200 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003201 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003203 res = video_autosw_set(1);
3204 if (res)
3205 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003207 res = video_autosw_set(0);
3208 if (res)
3209 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003211 res = video_outputsw_cycle();
3212 if (res)
3213 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003215 res = video_expand_toggle();
3216 if (res)
3217 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 } else
3219 return -EINVAL;
3220 }
3221
3222 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003223 status = video_outputsw_get();
3224 if (status < 0)
3225 return status;
3226 res = video_outputsw_set((status & ~disable) | enable);
3227 if (res)
3228 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 }
3230
3231 return 0;
3232}
3233
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003234static struct ibm_struct video_driver_data = {
3235 .name = "video",
3236 .read = video_read,
3237 .write = video_write,
3238 .exit = video_exit,
3239};
3240
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003241#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3242
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003243/*************************************************************************
3244 * Light (thinklight) subdriver
3245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003247TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3248TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003249
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003250static int light_get_status(void)
3251{
3252 int status = 0;
3253
3254 if (tp_features.light_status) {
3255 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3256 return -EIO;
3257 return (!!status);
3258 }
3259
3260 return -ENXIO;
3261}
3262
3263static int light_set_status(int status)
3264{
3265 int rc;
3266
3267 if (tp_features.light) {
3268 if (cmos_handle) {
3269 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3270 (status)?
3271 TP_CMOS_THINKLIGHT_ON :
3272 TP_CMOS_THINKLIGHT_OFF);
3273 } else {
3274 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3275 (status)? 1 : 0);
3276 }
3277 return (rc)? 0 : -EIO;
3278 }
3279
3280 return -ENXIO;
3281}
3282
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003283static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003285 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3286
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003287 TPACPI_ACPIHANDLE_INIT(ledb);
3288 TPACPI_ACPIHANDLE_INIT(lght);
3289 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003290
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003291 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003292 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003293
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003294 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003295 /* light status not supported on
3296 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003297 tp_features.light_status =
3298 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003300 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003301 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003302
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003303 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003306static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
3308 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003309 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003311 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003312 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003313 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003314 len += sprintf(p + len, "status:\t\tunknown\n");
3315 len += sprintf(p + len, "commands:\ton, off\n");
3316 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003317 status = light_get_status();
3318 if (status < 0)
3319 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003321 len += sprintf(p + len, "commands:\ton, off\n");
3322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 return len;
3325}
3326
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003327static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003330 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003331
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003332 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003333 return -ENODEV;
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 while ((cmd = next_cmd(&buf))) {
3336 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003337 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003339 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 } else
3341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003344 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003347static struct ibm_struct light_driver_data = {
3348 .name = "light",
3349 .read = light_read,
3350 .write = light_write,
3351};
3352
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003353/*************************************************************************
3354 * Dock subdriver
3355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003357#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003358
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003359static void dock_notify(struct ibm_struct *ibm, u32 event);
3360static int dock_read(char *p);
3361static int dock_write(char *buf);
3362
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003363TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003364 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3365 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3366 "\\_SB.PCI.ISA.SLCE", /* 570 */
3367 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3368
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003369/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003370TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003371
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003372static const struct acpi_device_id ibm_pci_device_ids[] = {
3373 {PCI_ROOT_HID_STRING, 0},
3374 {"", 0},
3375};
3376
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003377static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3378 {
3379 .notify = dock_notify,
3380 .handle = &dock_handle,
3381 .type = ACPI_SYSTEM_NOTIFY,
3382 },
3383 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003384 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3385 * We just use it to get notifications of dock hotplug
3386 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003387 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003388 .notify = dock_notify,
3389 .handle = &pci_handle,
3390 .type = ACPI_SYSTEM_NOTIFY,
3391 },
3392};
3393
3394static struct ibm_struct dock_driver_data[2] = {
3395 {
3396 .name = "dock",
3397 .read = dock_read,
3398 .write = dock_write,
3399 .acpi = &ibm_dock_acpidriver[0],
3400 },
3401 {
3402 .name = "dock",
3403 .acpi = &ibm_dock_acpidriver[1],
3404 },
3405};
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407#define dock_docked() (_sta(dock_handle) & 1)
3408
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003409static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003410{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003411 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3412
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003413 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003414
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003415 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3416 str_supported(dock_handle != NULL));
3417
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003418 return (dock_handle)? 0 : 1;
3419}
3420
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003421static int __init dock_init2(struct ibm_init_struct *iibm)
3422{
3423 int dock2_needed;
3424
3425 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3426
3427 if (dock_driver_data[0].flags.acpi_driver_registered &&
3428 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003429 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003430 dock2_needed = (pci_handle != NULL);
3431 vdbg_printk(TPACPI_DBG_INIT,
3432 "dock PCI handler for the TP 570 is %s\n",
3433 str_supported(dock2_needed));
3434 } else {
3435 vdbg_printk(TPACPI_DBG_INIT,
3436 "dock subdriver part 2 not required\n");
3437 dock2_needed = 0;
3438 }
3439
3440 return (dock2_needed)? 0 : 1;
3441}
3442
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003443static void dock_notify(struct ibm_struct *ibm, u32 event)
3444{
3445 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003446 int pci = ibm->acpi->hid && ibm->acpi->device &&
3447 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003448 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003449
3450 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003451 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003452 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003453 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003454 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003455 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003456 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003457 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003458 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003459 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003460 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003461 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003462 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003463 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003464 }
Len Brown14e04fb32007-08-23 15:20:26 -04003465 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003466 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3467 ibm->acpi->device->dev.bus_id,
3468 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003469}
3470
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003471static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
3473 int len = 0;
3474 int docked = dock_docked();
3475
3476 if (!dock_handle)
3477 len += sprintf(p + len, "status:\t\tnot supported\n");
3478 else if (!docked)
3479 len += sprintf(p + len, "status:\t\tundocked\n");
3480 else {
3481 len += sprintf(p + len, "status:\t\tdocked\n");
3482 len += sprintf(p + len, "commands:\tdock, undock\n");
3483 }
3484
3485 return len;
3486}
3487
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003488static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
3490 char *cmd;
3491
3492 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003493 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
3495 while ((cmd = next_cmd(&buf))) {
3496 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003497 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3498 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return -EIO;
3500 } else if (strlencmp(cmd, "dock") == 0) {
3501 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3502 return -EIO;
3503 } else
3504 return -EINVAL;
3505 }
3506
3507 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003508}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003510#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003512/*************************************************************************
3513 * Bay subdriver
3514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003516#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003517
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003518TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003519 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3520 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3521 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3522 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003523TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003524 "_EJ0", /* all others */
3525 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003526TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003527 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3528 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003529TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003530 "_EJ0", /* 770x */
3531 ); /* all others */
3532
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003533static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003535 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3536
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003537 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003538 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003539 TPACPI_ACPIHANDLE_INIT(bay_ej);
3540 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003541 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003542 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003543
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003544 tp_features.bay_status = bay_handle &&
3545 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3546 tp_features.bay_status2 = bay2_handle &&
3547 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003548
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003549 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3550 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3551 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3552 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003554 vdbg_printk(TPACPI_DBG_INIT,
3555 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003556 str_supported(tp_features.bay_status),
3557 str_supported(tp_features.bay_eject),
3558 str_supported(tp_features.bay_status2),
3559 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003560
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003561 return (tp_features.bay_status || tp_features.bay_eject ||
3562 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003565static void bay_notify(struct ibm_struct *ibm, u32 event)
3566{
Len Brown14e04fb32007-08-23 15:20:26 -04003567 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003568 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3569 ibm->acpi->device->dev.bus_id,
3570 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003571}
3572
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003573#define bay_occupied(b) (_sta(b##_handle) & 1)
3574
3575static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576{
3577 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003578 int occupied = bay_occupied(bay);
3579 int occupied2 = bay_occupied(bay2);
3580 int eject, eject2;
3581
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003582 len += sprintf(p + len, "status:\t\t%s\n",
3583 tp_features.bay_status ?
3584 (occupied ? "occupied" : "unoccupied") :
3585 "not supported");
3586 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003587 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3588 "occupied" : "unoccupied");
3589
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003590 eject = tp_features.bay_eject && occupied;
3591 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003592
3593 if (eject && eject2)
3594 len += sprintf(p + len, "commands:\teject, eject2\n");
3595 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003597 else if (eject2)
3598 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
3600 return len;
3601}
3602
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003603static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
3605 char *cmd;
3606
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003607 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003608 return -ENODEV;
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003611 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003612 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3613 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003614 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003615 strlencmp(cmd, "eject2") == 0) {
3616 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return -EIO;
3618 } else
3619 return -EINVAL;
3620 }
3621
3622 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003623}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003624
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003625static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3626 .notify = bay_notify,
3627 .handle = &bay_handle,
3628 .type = ACPI_SYSTEM_NOTIFY,
3629};
3630
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003631static struct ibm_struct bay_driver_data = {
3632 .name = "bay",
3633 .read = bay_read,
3634 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003635 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003636};
3637
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003638#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003640/*************************************************************************
3641 * CMOS subdriver
3642 */
3643
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003644/* sysfs cmos_command -------------------------------------------------- */
3645static ssize_t cmos_command_store(struct device *dev,
3646 struct device_attribute *attr,
3647 const char *buf, size_t count)
3648{
3649 unsigned long cmos_cmd;
3650 int res;
3651
3652 if (parse_strtoul(buf, 21, &cmos_cmd))
3653 return -EINVAL;
3654
3655 res = issue_thinkpad_cmos_command(cmos_cmd);
3656 return (res)? res : count;
3657}
3658
3659static struct device_attribute dev_attr_cmos_command =
3660 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3661
3662/* --------------------------------------------------------------------- */
3663
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003664static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003665{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003666 int res;
3667
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003668 vdbg_printk(TPACPI_DBG_INIT,
3669 "initializing cmos commands subdriver\n");
3670
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003671 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003672
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003673 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3674 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003675
3676 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3677 if (res)
3678 return res;
3679
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003680 return (cmos_handle)? 0 : 1;
3681}
3682
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003683static void cmos_exit(void)
3684{
3685 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3686}
3687
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003688static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
3690 int len = 0;
3691
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003692 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3693 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 if (!cmos_handle)
3695 len += sprintf(p + len, "status:\t\tnot supported\n");
3696 else {
3697 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003698 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 }
3700
3701 return len;
3702}
3703
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003704static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
3706 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003707 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003710 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3711 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 /* cmos_cmd set */
3713 } else
3714 return -EINVAL;
3715
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003716 res = issue_thinkpad_cmos_command(cmos_cmd);
3717 if (res)
3718 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 }
3720
3721 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003722}
3723
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003724static struct ibm_struct cmos_driver_data = {
3725 .name = "cmos",
3726 .read = cmos_read,
3727 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003728 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003729};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003730
3731/*************************************************************************
3732 * LED subdriver
3733 */
3734
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003735enum led_access_mode {
3736 TPACPI_LED_NONE = 0,
3737 TPACPI_LED_570, /* 570 */
3738 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3739 TPACPI_LED_NEW, /* all others */
3740};
3741
3742enum { /* For TPACPI_LED_OLD */
3743 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3744 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3745 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3746};
3747
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003748enum led_status_t {
3749 TPACPI_LED_OFF = 0,
3750 TPACPI_LED_ON,
3751 TPACPI_LED_BLINK,
3752};
3753
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003754static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003755
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003756TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003757 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3758 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003759 "LED", /* all others */
3760 ); /* R30, R31 */
3761
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003762static int led_get_status(unsigned int led)
3763{
3764 int status;
3765
3766 switch (led_supported) {
3767 case TPACPI_LED_570:
3768 if (!acpi_evalf(ec_handle,
3769 &status, "GLED", "dd", 1 << led))
3770 return -EIO;
3771 return (status == 0)?
3772 TPACPI_LED_OFF :
3773 ((status == 1)?
3774 TPACPI_LED_ON :
3775 TPACPI_LED_BLINK);
3776 default:
3777 return -ENXIO;
3778 }
3779
3780 /* not reached */
3781}
3782
3783static int led_set_status(unsigned int led, enum led_status_t ledstatus)
3784{
3785 /* off, on, blink. Index is led_status_t */
3786 static const int const led_sled_arg1[] = { 0, 1, 3 };
3787 static const int const led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3788 static const int const led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3789 static const int const led_led_arg1[] = { 0, 0x80, 0xc0 };
3790
3791 int rc = 0;
3792
3793 switch (led_supported) {
3794 case TPACPI_LED_570:
3795 /* 570 */
3796 led = 1 << led;
3797 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3798 led, led_sled_arg1[ledstatus]))
3799 rc = -EIO;
3800 break;
3801 case TPACPI_LED_OLD:
3802 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3803 led = 1 << led;
3804 rc = ec_write(TPACPI_LED_EC_HLMS, led);
3805 if (rc >= 0)
3806 rc = ec_write(TPACPI_LED_EC_HLBL,
3807 led * led_exp_hlbl[ledstatus]);
3808 if (rc >= 0)
3809 rc = ec_write(TPACPI_LED_EC_HLCL,
3810 led * led_exp_hlcl[ledstatus]);
3811 break;
3812 case TPACPI_LED_NEW:
3813 /* all others */
3814 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3815 led, led_led_arg1[ledstatus]))
3816 rc = -EIO;
3817 break;
3818 default:
3819 rc = -ENXIO;
3820 }
3821
3822 return rc;
3823}
3824
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003825static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003826{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003827 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3828
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003829 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003830
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003831 if (!led_handle)
3832 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003833 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003834 else if (strlencmp(led_path, "SLED") == 0)
3835 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003836 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003837 else if (strlencmp(led_path, "SYSL") == 0)
3838 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003839 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003840 else
3841 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003842 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003843
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003844 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3845 str_supported(led_supported), led_supported);
3846
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003847 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003848}
3849
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003850#define str_led_status(s) \
3851 ((s) == TPACPI_LED_OFF ? "off" : \
3852 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003853
3854static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
3856 int len = 0;
3857
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003858 if (!led_supported) {
3859 len += sprintf(p + len, "status:\t\tnot supported\n");
3860 return len;
3861 }
3862 len += sprintf(p + len, "status:\t\tsupported\n");
3863
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003864 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003865 /* 570 */
3866 int i, status;
3867 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003868 status = led_get_status(i);
3869 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003870 return -EIO;
3871 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003872 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003873 }
3874 }
3875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003877 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 return len;
3880}
3881
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003882static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
3884 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003885 int led, rc;
3886 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003887
3888 if (!led_supported)
3889 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
3891 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003892 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 return -EINVAL;
3894
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003895 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003896 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003898 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003899 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003900 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003901 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003902 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003903 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003904
3905 rc = led_set_status(led, s);
3906 if (rc < 0)
3907 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
3909
3910 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003911}
3912
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003913static struct ibm_struct led_driver_data = {
3914 .name = "led",
3915 .read = led_read,
3916 .write = led_write,
3917};
3918
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003919/*************************************************************************
3920 * Beep subdriver
3921 */
3922
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003923TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003924
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003925static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003926{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003927 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3928
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003929 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003930
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003931 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3932 str_supported(beep_handle != NULL));
3933
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003934 return (beep_handle)? 0 : 1;
3935}
3936
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003937static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
3939 int len = 0;
3940
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003941 if (!beep_handle)
3942 len += sprintf(p + len, "status:\t\tnot supported\n");
3943 else {
3944 len += sprintf(p + len, "status:\t\tsupported\n");
3945 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 return len;
3949}
3950
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003951static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
3953 char *cmd;
3954 int beep_cmd;
3955
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003956 if (!beep_handle)
3957 return -ENODEV;
3958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003960 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3961 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 /* beep_cmd set */
3963 } else
3964 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003965 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return -EIO;
3967 }
3968
3969 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003970}
3971
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003972static struct ibm_struct beep_driver_data = {
3973 .name = "beep",
3974 .read = beep_read,
3975 .write = beep_write,
3976};
3977
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003978/*************************************************************************
3979 * Thermal subdriver
3980 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003981
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003982enum thermal_access_mode {
3983 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3984 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3985 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3986 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3987 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3988};
3989
3990enum { /* TPACPI_THERMAL_TPEC_* */
3991 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3992 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3993 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3994};
3995
3996#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3997struct ibm_thermal_sensors_struct {
3998 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3999};
4000
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004001static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004002
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004003/* idx is zero-based */
4004static int thermal_get_sensor(int idx, s32 *value)
4005{
4006 int t;
4007 s8 tmp;
4008 char tmpi[5];
4009
4010 t = TP_EC_THERMAL_TMP0;
4011
4012 switch (thermal_read_mode) {
4013#if TPACPI_MAX_THERMAL_SENSORS >= 16
4014 case TPACPI_THERMAL_TPEC_16:
4015 if (idx >= 8 && idx <= 15) {
4016 t = TP_EC_THERMAL_TMP8;
4017 idx -= 8;
4018 }
4019 /* fallthrough */
4020#endif
4021 case TPACPI_THERMAL_TPEC_8:
4022 if (idx <= 7) {
4023 if (!acpi_ec_read(t + idx, &tmp))
4024 return -EIO;
4025 *value = tmp * 1000;
4026 return 0;
4027 }
4028 break;
4029
4030 case TPACPI_THERMAL_ACPI_UPDT:
4031 if (idx <= 7) {
4032 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4033 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4034 return -EIO;
4035 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4036 return -EIO;
4037 *value = (t - 2732) * 100;
4038 return 0;
4039 }
4040 break;
4041
4042 case TPACPI_THERMAL_ACPI_TMP07:
4043 if (idx <= 7) {
4044 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4045 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4046 return -EIO;
4047 if (t > 127 || t < -127)
4048 t = TP_EC_THERMAL_TMP_NA;
4049 *value = t * 1000;
4050 return 0;
4051 }
4052 break;
4053
4054 case TPACPI_THERMAL_NONE:
4055 default:
4056 return -ENOSYS;
4057 }
4058
4059 return -EINVAL;
4060}
4061
4062static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4063{
4064 int res, i;
4065 int n;
4066
4067 n = 8;
4068 i = 0;
4069
4070 if (!s)
4071 return -EINVAL;
4072
4073 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4074 n = 16;
4075
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004076 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004077 res = thermal_get_sensor(i, &s->temp[i]);
4078 if (res)
4079 return res;
4080 }
4081
4082 return n;
4083}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004084
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004085/* sysfs temp##_input -------------------------------------------------- */
4086
4087static ssize_t thermal_temp_input_show(struct device *dev,
4088 struct device_attribute *attr,
4089 char *buf)
4090{
4091 struct sensor_device_attribute *sensor_attr =
4092 to_sensor_dev_attr(attr);
4093 int idx = sensor_attr->index;
4094 s32 value;
4095 int res;
4096
4097 res = thermal_get_sensor(idx, &value);
4098 if (res)
4099 return res;
4100 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4101 return -ENXIO;
4102
4103 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4104}
4105
4106#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004107 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4108 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004109
4110static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4111 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4112 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4113 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4114 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4115 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4116 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4117 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4118 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4119 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4120 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4121 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4122 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4123 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4124 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4125 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4126 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4127};
4128
4129#define THERMAL_ATTRS(X) \
4130 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4131
4132static struct attribute *thermal_temp_input_attr[] = {
4133 THERMAL_ATTRS(8),
4134 THERMAL_ATTRS(9),
4135 THERMAL_ATTRS(10),
4136 THERMAL_ATTRS(11),
4137 THERMAL_ATTRS(12),
4138 THERMAL_ATTRS(13),
4139 THERMAL_ATTRS(14),
4140 THERMAL_ATTRS(15),
4141 THERMAL_ATTRS(0),
4142 THERMAL_ATTRS(1),
4143 THERMAL_ATTRS(2),
4144 THERMAL_ATTRS(3),
4145 THERMAL_ATTRS(4),
4146 THERMAL_ATTRS(5),
4147 THERMAL_ATTRS(6),
4148 THERMAL_ATTRS(7),
4149 NULL
4150};
4151
4152static const struct attribute_group thermal_temp_input16_group = {
4153 .attrs = thermal_temp_input_attr
4154};
4155
4156static const struct attribute_group thermal_temp_input8_group = {
4157 .attrs = &thermal_temp_input_attr[8]
4158};
4159
4160#undef THERMAL_SENSOR_ATTR_TEMP
4161#undef THERMAL_ATTRS
4162
4163/* --------------------------------------------------------------------- */
4164
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004165static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004166{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004167 u8 t, ta1, ta2;
4168 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004169 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004170 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004171
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004172 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4173
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004174 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004175
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004176 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004177 /*
4178 * Direct EC access mode: sensors at registers
4179 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4180 * non-implemented, thermal sensors return 0x80 when
4181 * not available
4182 */
4183
4184 ta1 = ta2 = 0;
4185 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004186 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004187 ta1 |= t;
4188 } else {
4189 ta1 = 0;
4190 break;
4191 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004192 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004193 ta2 |= t;
4194 } else {
4195 ta1 = 0;
4196 break;
4197 }
4198 }
4199 if (ta1 == 0) {
4200 /* This is sheer paranoia, but we handle it anyway */
4201 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004202 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004203 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004204 "falling back to ACPI TMPx access "
4205 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004206 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004207 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004208 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004209 "ThinkPad ACPI EC access misbehaving, "
4210 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004211 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004212 }
4213 } else {
4214 thermal_read_mode =
4215 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004216 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004217 }
4218 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004219 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4220 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004221 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004222 } else {
4223 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004224 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004225 }
4226 } else {
4227 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004228 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004229 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004230
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004231 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4232 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4233 thermal_read_mode);
4234
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004235 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004236 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004237 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004238 &thermal_temp_input16_group);
4239 if (res)
4240 return res;
4241 break;
4242 case TPACPI_THERMAL_TPEC_8:
4243 case TPACPI_THERMAL_ACPI_TMP07:
4244 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004245 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004246 &thermal_temp_input8_group);
4247 if (res)
4248 return res;
4249 break;
4250 case TPACPI_THERMAL_NONE:
4251 default:
4252 return 1;
4253 }
4254
4255 return 0;
4256}
4257
4258static void thermal_exit(void)
4259{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004260 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004261 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004262 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004263 &thermal_temp_input16_group);
4264 break;
4265 case TPACPI_THERMAL_TPEC_8:
4266 case TPACPI_THERMAL_ACPI_TMP07:
4267 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004268 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004269 &thermal_temp_input16_group);
4270 break;
4271 case TPACPI_THERMAL_NONE:
4272 default:
4273 break;
4274 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004275}
4276
4277static int thermal_read(char *p)
4278{
4279 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004280 int n, i;
4281 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004282
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004283 n = thermal_get_sensors(&t);
4284 if (unlikely(n < 0))
4285 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004286
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004287 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004288
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004289 if (n > 0) {
4290 for (i = 0; i < (n - 1); i++)
4291 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4292 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4293 } else
4294 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004295
4296 return len;
4297}
4298
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004299static struct ibm_struct thermal_driver_data = {
4300 .name = "thermal",
4301 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004302 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004303};
4304
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004305/*************************************************************************
4306 * EC Dump subdriver
4307 */
4308
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004309static u8 ecdump_regs[256];
4310
4311static int ecdump_read(char *p)
4312{
4313 int len = 0;
4314 int i, j;
4315 u8 v;
4316
4317 len += sprintf(p + len, "EC "
4318 " +00 +01 +02 +03 +04 +05 +06 +07"
4319 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4320 for (i = 0; i < 256; i += 16) {
4321 len += sprintf(p + len, "EC 0x%02x:", i);
4322 for (j = 0; j < 16; j++) {
4323 if (!acpi_ec_read(i + j, &v))
4324 break;
4325 if (v != ecdump_regs[i + j])
4326 len += sprintf(p + len, " *%02x", v);
4327 else
4328 len += sprintf(p + len, " %02x", v);
4329 ecdump_regs[i + j] = v;
4330 }
4331 len += sprintf(p + len, "\n");
4332 if (j != 16)
4333 break;
4334 }
4335
4336 /* These are way too dangerous to advertise openly... */
4337#if 0
4338 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4339 " (<offset> is 00-ff, <value> is 00-ff)\n");
4340 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4341 " (<offset> is 00-ff, <value> is 0-255)\n");
4342#endif
4343 return len;
4344}
4345
4346static int ecdump_write(char *buf)
4347{
4348 char *cmd;
4349 int i, v;
4350
4351 while ((cmd = next_cmd(&buf))) {
4352 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4353 /* i and v set */
4354 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4355 /* i and v set */
4356 } else
4357 return -EINVAL;
4358 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4359 if (!acpi_ec_write(i, v))
4360 return -EIO;
4361 } else
4362 return -EINVAL;
4363 }
4364
4365 return 0;
4366}
4367
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004368static struct ibm_struct ecdump_driver_data = {
4369 .name = "ecdump",
4370 .read = ecdump_read,
4371 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004372 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004373};
4374
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004375/*************************************************************************
4376 * Backlight/brightness subdriver
4377 */
Holger Macht8acb0252006-10-20 14:30:28 -07004378
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004379#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4380
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004381enum {
4382 TP_EC_BACKLIGHT = 0x31,
4383
4384 /* TP_EC_BACKLIGHT bitmasks */
4385 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4386 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4387 TP_EC_BACKLIGHT_MAPSW = 0x20,
4388};
4389
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004390static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004391static int brightness_mode;
4392static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4393
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004394static struct mutex brightness_mutex;
4395
4396/*
4397 * ThinkPads can read brightness from two places: EC 0x31, or
4398 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004399 *
4400 * EC 0x31 has the following layout
4401 * Bit 7: unknown function
4402 * Bit 6: unknown function
4403 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4404 * Bit 4: must be set to zero to avoid problems
4405 * Bit 3-0: backlight brightness level
4406 *
4407 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004408 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004409static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004410{
4411 u8 lec = 0, lcmos = 0, level = 0;
4412
4413 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004414 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004415 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004416 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004417 };
4418 if (brightness_mode & 2) {
4419 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4420 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4421 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4422 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4423 level = lcmos;
4424 }
4425
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004426 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004427 *status = lec; /* Prefer EC, CMOS is just a backing store */
4428 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004429 if (lec == lcmos)
4430 tp_warned.bright_cmos_ec_unsync = 0;
4431 else {
4432 if (!tp_warned.bright_cmos_ec_unsync) {
4433 printk(TPACPI_ERR
4434 "CMOS NVRAM (%u) and EC (%u) do not "
4435 "agree on display brightness level\n",
4436 (unsigned int) lcmos,
4437 (unsigned int) lec);
4438 tp_warned.bright_cmos_ec_unsync = 1;
4439 }
4440 return -EIO;
4441 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004442 } else {
4443 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004444 }
4445
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004446 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004447}
4448
4449/* May return EINTR which can always be mapped to ERESTARTSYS */
4450static int brightness_set(int value)
4451{
4452 int cmos_cmd, inc, i, res;
4453 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004454 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004455
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004456 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4457 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004458 return -EINVAL;
4459
4460 res = mutex_lock_interruptible(&brightness_mutex);
4461 if (res < 0)
4462 return res;
4463
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004464 res = brightness_get_raw(&current_value);
4465 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004466 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004467
4468 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4469 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004470
4471 cmos_cmd = value > current_value ?
4472 TP_CMOS_BRIGHTNESS_UP :
4473 TP_CMOS_BRIGHTNESS_DOWN;
4474 inc = (value > current_value)? 1 : -1;
4475
4476 res = 0;
4477 for (i = current_value; i != value; i += inc) {
4478 if ((brightness_mode & 2) &&
4479 issue_thinkpad_cmos_command(cmos_cmd)) {
4480 res = -EIO;
4481 goto errout;
4482 }
4483 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004484 !acpi_ec_write(TP_EC_BACKLIGHT,
4485 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004486 res = -EIO;
4487 goto errout;;
4488 }
4489 }
4490
4491errout:
4492 mutex_unlock(&brightness_mutex);
4493 return res;
4494}
4495
4496/* sysfs backlight class ----------------------------------------------- */
4497
4498static int brightness_update_status(struct backlight_device *bd)
4499{
4500 /* it is the backlight class's job (caller) to handle
4501 * EINTR and other errors properly */
4502 return brightness_set(
4503 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4504 bd->props.power == FB_BLANK_UNBLANK) ?
4505 bd->props.brightness : 0);
4506}
Holger Macht8acb0252006-10-20 14:30:28 -07004507
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004508static int brightness_get(struct backlight_device *bd)
4509{
4510 int status, res;
4511
4512 res = brightness_get_raw(&status);
4513 if (res < 0)
4514 return 0; /* FIXME: teach backlight about error handling */
4515
4516 return status & TP_EC_BACKLIGHT_LVLMSK;
4517}
4518
Richard Purdie599a52d2007-02-10 23:07:48 +00004519static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004520 .get_brightness = brightness_get,
4521 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004522};
4523
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004524/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004525
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004526static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004527{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004528 int b;
4529
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004530 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4531
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004532 mutex_init(&brightness_mutex);
4533
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004534 /*
4535 * We always attempt to detect acpi support, so as to switch
4536 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4537 * going to publish a backlight interface
4538 */
4539 b = tpacpi_check_std_acpi_brightness_support();
4540 if (b > 0) {
4541 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4542 printk(TPACPI_NOTICE
4543 "Lenovo BIOS switched to ACPI backlight "
4544 "control mode\n");
4545 }
4546 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004547 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004548 "standard ACPI backlight interface "
4549 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004550 return 1;
4551 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004552 }
4553
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004554 if (!brightness_enable) {
4555 dbg_printk(TPACPI_DBG_INIT,
4556 "brightness support disabled by "
4557 "module parameter\n");
4558 return 1;
4559 }
4560
4561 if (b > 16) {
4562 printk(TPACPI_ERR
4563 "Unsupported brightness interface, "
4564 "please contact %s\n", TPACPI_MAIL);
4565 return 1;
4566 }
4567 if (b == 16)
4568 tp_features.bright_16levels = 1;
4569
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004570 if (!brightness_mode) {
4571 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4572 brightness_mode = 2;
4573 else
4574 brightness_mode = 3;
4575
4576 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4577 brightness_mode);
4578 }
4579
4580 if (brightness_mode > 3)
4581 return -EINVAL;
4582
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004583 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004584 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004585
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004586 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004587 printk(TPACPI_INFO
4588 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004589
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004590 ibm_backlight_device = backlight_device_register(
4591 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4592 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004593 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004594 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004595 return PTR_ERR(ibm_backlight_device);
4596 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004597 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004598
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004599 ibm_backlight_device->props.max_brightness =
4600 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004601 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004602 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004603
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004604 return 0;
4605}
4606
4607static void brightness_exit(void)
4608{
4609 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004610 vdbg_printk(TPACPI_DBG_EXIT,
4611 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004612 backlight_device_unregister(ibm_backlight_device);
4613 ibm_backlight_device = NULL;
4614 }
4615}
4616
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004617static int brightness_read(char *p)
4618{
4619 int len = 0;
4620 int level;
4621
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004622 level = brightness_get(NULL);
4623 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004624 len += sprintf(p + len, "level:\t\tunreadable\n");
4625 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004626 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004627 len += sprintf(p + len, "commands:\tup, down\n");
4628 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004629 " (<level> is 0-%d)\n",
4630 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004631 }
4632
4633 return len;
4634}
4635
4636static int brightness_write(char *buf)
4637{
4638 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004639 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004640 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004641 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004642
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004643 level = brightness_get(NULL);
4644 if (level < 0)
4645 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004646
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004647 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004648 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004649 if (level < max_level)
4650 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004651 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004652 if (level > 0)
4653 level--;
4654 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4655 level >= 0 && level <= max_level) {
4656 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004657 } else
4658 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004659 }
4660
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004661 /*
4662 * Now we know what the final level should be, so we try to set it.
4663 * Doing it this way makes the syscall restartable in case of EINTR
4664 */
4665 rc = brightness_set(level);
4666 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004667}
4668
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004669static struct ibm_struct brightness_driver_data = {
4670 .name = "brightness",
4671 .read = brightness_read,
4672 .write = brightness_write,
4673 .exit = brightness_exit,
4674};
4675
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004676/*************************************************************************
4677 * Volume subdriver
4678 */
4679
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004680static int volume_offset = 0x30;
4681
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004682static int volume_read(char *p)
4683{
4684 int len = 0;
4685 u8 level;
4686
4687 if (!acpi_ec_read(volume_offset, &level)) {
4688 len += sprintf(p + len, "level:\t\tunreadable\n");
4689 } else {
4690 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4691 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4692 len += sprintf(p + len, "commands:\tup, down, mute\n");
4693 len += sprintf(p + len, "commands:\tlevel <level>"
4694 " (<level> is 0-15)\n");
4695 }
4696
4697 return len;
4698}
4699
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004700static int volume_write(char *buf)
4701{
4702 int cmos_cmd, inc, i;
4703 u8 level, mute;
4704 int new_level, new_mute;
4705 char *cmd;
4706
4707 while ((cmd = next_cmd(&buf))) {
4708 if (!acpi_ec_read(volume_offset, &level))
4709 return -EIO;
4710 new_mute = mute = level & 0x40;
4711 new_level = level = level & 0xf;
4712
4713 if (strlencmp(cmd, "up") == 0) {
4714 if (mute)
4715 new_mute = 0;
4716 else
4717 new_level = level == 15 ? 15 : level + 1;
4718 } else if (strlencmp(cmd, "down") == 0) {
4719 if (mute)
4720 new_mute = 0;
4721 else
4722 new_level = level == 0 ? 0 : level - 1;
4723 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4724 new_level >= 0 && new_level <= 15) {
4725 /* new_level set */
4726 } else if (strlencmp(cmd, "mute") == 0) {
4727 new_mute = 0x40;
4728 } else
4729 return -EINVAL;
4730
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004731 if (new_level != level) {
4732 /* mute doesn't change */
4733
4734 cmos_cmd = (new_level > level) ?
4735 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004736 inc = new_level > level ? 1 : -1;
4737
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004738 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004739 !acpi_ec_write(volume_offset, level)))
4740 return -EIO;
4741
4742 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004743 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004744 !acpi_ec_write(volume_offset, i + inc))
4745 return -EIO;
4746
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004747 if (mute &&
4748 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4749 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004750 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004751 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004752 }
4753
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004754 if (new_mute != mute) {
4755 /* level doesn't change */
4756
4757 cmos_cmd = (new_mute) ?
4758 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004759
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004760 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004761 !acpi_ec_write(volume_offset, level + new_mute))
4762 return -EIO;
4763 }
4764 }
4765
4766 return 0;
4767}
4768
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004769static struct ibm_struct volume_driver_data = {
4770 .name = "volume",
4771 .read = volume_read,
4772 .write = volume_write,
4773};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004774
4775/*************************************************************************
4776 * Fan subdriver
4777 */
4778
4779/*
4780 * FAN ACCESS MODES
4781 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004782 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004783 * ACPI GFAN method: returns fan level
4784 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004785 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004786 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004787 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004788 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004789 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4790 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004791 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4792 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004793 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004794 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004795 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4796 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004797 *
4798 * Fan speed changes of any sort (including those caused by the
4799 * disengaged mode) are usually done slowly by the firmware as the
4800 * maximum ammount of fan duty cycle change per second seems to be
4801 * limited.
4802 *
4803 * Reading is not available if GFAN exists.
4804 * Writing is not available if SFAN exists.
4805 *
4806 * Bits
4807 * 7 automatic mode engaged;
4808 * (default operation mode of the ThinkPad)
4809 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004810 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004811 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004812 * the tachometer while the fan controller ramps up
4813 * the speed (which can take up to a few *minutes*).
4814 * Speeds up fan to 100% duty-cycle, which is far above
4815 * the standard RPM levels. It is not impossible that
4816 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004817 * 5-3 unused in some models. Extra bits for fan level
4818 * in others, but still useless as all values above
4819 * 7 map to the same speed as level 7 in these models.
4820 * 2-0 fan level (0..7 usually)
4821 * 0x00 = stop
4822 * 0x07 = max (set when temperatures critical)
4823 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004824 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004825 *
4826 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4827 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4828 * does so, its initial value is meaningless (0x07).
4829 *
4830 * For firmware bugs, refer to:
4831 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4832 *
4833 * ----
4834 *
4835 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4836 * Main fan tachometer reading (in RPM)
4837 *
4838 * This register is present on all ThinkPads with a new-style EC, and
4839 * it is known not to be present on the A21m/e, and T22, as there is
4840 * something else in offset 0x84 according to the ACPI DSDT. Other
4841 * ThinkPads from this same time period (and earlier) probably lack the
4842 * tachometer as well.
4843 *
4844 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4845 * was never fixed by IBM to report the EC firmware version string
4846 * probably support the tachometer (like the early X models), so
4847 * detecting it is quite hard. We need more data to know for sure.
4848 *
4849 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4850 * might result.
4851 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004852 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4853 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004854 *
4855 * For firmware bugs, refer to:
4856 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4857 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004858 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004859 * ThinkPad X31, X40, X41. Not available in the X60.
4860 *
4861 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4862 * high speed. ACPI DSDT seems to map these three speeds to levels
4863 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4864 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4865 *
4866 * The speeds are stored on handles
4867 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4868 *
4869 * There are three default speed sets, acessible as handles:
4870 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4871 *
4872 * ACPI DSDT switches which set is in use depending on various
4873 * factors.
4874 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004875 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004876 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4877 * but the ACPI tables just mention level 7.
4878 */
4879
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004880enum { /* Fan control constants */
4881 fan_status_offset = 0x2f, /* EC register 0x2f */
4882 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4883 * 0x84 must be read before 0x85 */
4884
4885 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4886 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4887
4888 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4889};
4890
4891enum fan_status_access_mode {
4892 TPACPI_FAN_NONE = 0, /* No fan status or control */
4893 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4894 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4895};
4896
4897enum fan_control_access_mode {
4898 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4899 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4900 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4901 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4902};
4903
4904enum fan_control_commands {
4905 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4906 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4907 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4908 * and also watchdog cmd */
4909};
4910
4911static int fan_control_allowed;
4912
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004913static enum fan_status_access_mode fan_status_access_mode;
4914static enum fan_control_access_mode fan_control_access_mode;
4915static enum fan_control_commands fan_control_commands;
4916
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004917static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004918static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004919static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004920
4921static struct mutex fan_mutex;
4922
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004923static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004924static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004925
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004926TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4927TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004928 "\\FSPD", /* 600e/x, 770e, 770x */
4929 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004930TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004931 "JFNS", /* 770x-JL */
4932 ); /* all others */
4933
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004934/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004935 * Call with fan_mutex held
4936 */
4937static void fan_update_desired_level(u8 status)
4938{
4939 if ((status &
4940 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4941 if (status > 7)
4942 fan_control_desired_level = 7;
4943 else
4944 fan_control_desired_level = status;
4945 }
4946}
4947
4948static int fan_get_status(u8 *status)
4949{
4950 u8 s;
4951
4952 /* TODO:
4953 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4954
4955 switch (fan_status_access_mode) {
4956 case TPACPI_FAN_RD_ACPI_GFAN:
4957 /* 570, 600e/x, 770e, 770x */
4958
4959 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4960 return -EIO;
4961
4962 if (likely(status))
4963 *status = s & 0x07;
4964
4965 break;
4966
4967 case TPACPI_FAN_RD_TPEC:
4968 /* all except 570, 600e/x, 770e, 770x */
4969 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4970 return -EIO;
4971
4972 if (likely(status))
4973 *status = s;
4974
4975 break;
4976
4977 default:
4978 return -ENXIO;
4979 }
4980
4981 return 0;
4982}
4983
4984static int fan_get_status_safe(u8 *status)
4985{
4986 int rc;
4987 u8 s;
4988
4989 if (mutex_lock_interruptible(&fan_mutex))
4990 return -ERESTARTSYS;
4991 rc = fan_get_status(&s);
4992 if (!rc)
4993 fan_update_desired_level(s);
4994 mutex_unlock(&fan_mutex);
4995
4996 if (status)
4997 *status = s;
4998
4999 return rc;
5000}
5001
5002static int fan_get_speed(unsigned int *speed)
5003{
5004 u8 hi, lo;
5005
5006 switch (fan_status_access_mode) {
5007 case TPACPI_FAN_RD_TPEC:
5008 /* all except 570, 600e/x, 770e, 770x */
5009 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5010 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5011 return -EIO;
5012
5013 if (likely(speed))
5014 *speed = (hi << 8) | lo;
5015
5016 break;
5017
5018 default:
5019 return -ENXIO;
5020 }
5021
5022 return 0;
5023}
5024
5025static int fan_set_level(int level)
5026{
5027 if (!fan_control_allowed)
5028 return -EPERM;
5029
5030 switch (fan_control_access_mode) {
5031 case TPACPI_FAN_WR_ACPI_SFAN:
5032 if (level >= 0 && level <= 7) {
5033 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5034 return -EIO;
5035 } else
5036 return -EINVAL;
5037 break;
5038
5039 case TPACPI_FAN_WR_ACPI_FANS:
5040 case TPACPI_FAN_WR_TPEC:
5041 if ((level != TP_EC_FAN_AUTO) &&
5042 (level != TP_EC_FAN_FULLSPEED) &&
5043 ((level < 0) || (level > 7)))
5044 return -EINVAL;
5045
5046 /* safety net should the EC not support AUTO
5047 * or FULLSPEED mode bits and just ignore them */
5048 if (level & TP_EC_FAN_FULLSPEED)
5049 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005050 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005051 level |= 4; /* safety min speed 4 */
5052
5053 if (!acpi_ec_write(fan_status_offset, level))
5054 return -EIO;
5055 else
5056 tp_features.fan_ctrl_status_undef = 0;
5057 break;
5058
5059 default:
5060 return -ENXIO;
5061 }
5062 return 0;
5063}
5064
5065static int fan_set_level_safe(int level)
5066{
5067 int rc;
5068
5069 if (!fan_control_allowed)
5070 return -EPERM;
5071
5072 if (mutex_lock_interruptible(&fan_mutex))
5073 return -ERESTARTSYS;
5074
5075 if (level == TPACPI_FAN_LAST_LEVEL)
5076 level = fan_control_desired_level;
5077
5078 rc = fan_set_level(level);
5079 if (!rc)
5080 fan_update_desired_level(level);
5081
5082 mutex_unlock(&fan_mutex);
5083 return rc;
5084}
5085
5086static int fan_set_enable(void)
5087{
5088 u8 s;
5089 int rc;
5090
5091 if (!fan_control_allowed)
5092 return -EPERM;
5093
5094 if (mutex_lock_interruptible(&fan_mutex))
5095 return -ERESTARTSYS;
5096
5097 switch (fan_control_access_mode) {
5098 case TPACPI_FAN_WR_ACPI_FANS:
5099 case TPACPI_FAN_WR_TPEC:
5100 rc = fan_get_status(&s);
5101 if (rc < 0)
5102 break;
5103
5104 /* Don't go out of emergency fan mode */
5105 if (s != 7) {
5106 s &= 0x07;
5107 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5108 }
5109
5110 if (!acpi_ec_write(fan_status_offset, s))
5111 rc = -EIO;
5112 else {
5113 tp_features.fan_ctrl_status_undef = 0;
5114 rc = 0;
5115 }
5116 break;
5117
5118 case TPACPI_FAN_WR_ACPI_SFAN:
5119 rc = fan_get_status(&s);
5120 if (rc < 0)
5121 break;
5122
5123 s &= 0x07;
5124
5125 /* Set fan to at least level 4 */
5126 s |= 4;
5127
5128 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005129 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005130 else
5131 rc = 0;
5132 break;
5133
5134 default:
5135 rc = -ENXIO;
5136 }
5137
5138 mutex_unlock(&fan_mutex);
5139 return rc;
5140}
5141
5142static int fan_set_disable(void)
5143{
5144 int rc;
5145
5146 if (!fan_control_allowed)
5147 return -EPERM;
5148
5149 if (mutex_lock_interruptible(&fan_mutex))
5150 return -ERESTARTSYS;
5151
5152 rc = 0;
5153 switch (fan_control_access_mode) {
5154 case TPACPI_FAN_WR_ACPI_FANS:
5155 case TPACPI_FAN_WR_TPEC:
5156 if (!acpi_ec_write(fan_status_offset, 0x00))
5157 rc = -EIO;
5158 else {
5159 fan_control_desired_level = 0;
5160 tp_features.fan_ctrl_status_undef = 0;
5161 }
5162 break;
5163
5164 case TPACPI_FAN_WR_ACPI_SFAN:
5165 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5166 rc = -EIO;
5167 else
5168 fan_control_desired_level = 0;
5169 break;
5170
5171 default:
5172 rc = -ENXIO;
5173 }
5174
5175
5176 mutex_unlock(&fan_mutex);
5177 return rc;
5178}
5179
5180static int fan_set_speed(int speed)
5181{
5182 int rc;
5183
5184 if (!fan_control_allowed)
5185 return -EPERM;
5186
5187 if (mutex_lock_interruptible(&fan_mutex))
5188 return -ERESTARTSYS;
5189
5190 rc = 0;
5191 switch (fan_control_access_mode) {
5192 case TPACPI_FAN_WR_ACPI_FANS:
5193 if (speed >= 0 && speed <= 65535) {
5194 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5195 speed, speed, speed))
5196 rc = -EIO;
5197 } else
5198 rc = -EINVAL;
5199 break;
5200
5201 default:
5202 rc = -ENXIO;
5203 }
5204
5205 mutex_unlock(&fan_mutex);
5206 return rc;
5207}
5208
5209static void fan_watchdog_reset(void)
5210{
5211 static int fan_watchdog_active;
5212
5213 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5214 return;
5215
5216 if (fan_watchdog_active)
5217 cancel_delayed_work(&fan_watchdog_task);
5218
5219 if (fan_watchdog_maxinterval > 0 &&
5220 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5221 fan_watchdog_active = 1;
5222 if (!schedule_delayed_work(&fan_watchdog_task,
5223 msecs_to_jiffies(fan_watchdog_maxinterval
5224 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005225 printk(TPACPI_ERR
5226 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005227 "watchdog will not trigger\n");
5228 }
5229 } else
5230 fan_watchdog_active = 0;
5231}
5232
5233static void fan_watchdog_fire(struct work_struct *ignored)
5234{
5235 int rc;
5236
5237 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5238 return;
5239
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005240 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005241 rc = fan_set_enable();
5242 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005243 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005244 "will try again later...\n", -rc);
5245 /* reschedule for later */
5246 fan_watchdog_reset();
5247 }
5248}
5249
5250/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005251 * SYSFS fan layout: hwmon compatible (device)
5252 *
5253 * pwm*_enable:
5254 * 0: "disengaged" mode
5255 * 1: manual mode
5256 * 2: native EC "auto" mode (recommended, hardware default)
5257 *
5258 * pwm*: set speed in manual mode, ignored otherwise.
5259 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5260 * interpolation.
5261 *
5262 * fan*_input: tachometer reading, RPM
5263 *
5264 *
5265 * SYSFS fan layout: extensions
5266 *
5267 * fan_watchdog (driver):
5268 * fan watchdog interval in seconds, 0 disables (default), max 120
5269 */
5270
5271/* sysfs fan pwm1_enable ----------------------------------------------- */
5272static ssize_t fan_pwm1_enable_show(struct device *dev,
5273 struct device_attribute *attr,
5274 char *buf)
5275{
5276 int res, mode;
5277 u8 status;
5278
5279 res = fan_get_status_safe(&status);
5280 if (res)
5281 return res;
5282
5283 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5284 if (status != fan_control_initial_status) {
5285 tp_features.fan_ctrl_status_undef = 0;
5286 } else {
5287 /* Return most likely status. In fact, it
5288 * might be the only possible status */
5289 status = TP_EC_FAN_AUTO;
5290 }
5291 }
5292
5293 if (status & TP_EC_FAN_FULLSPEED) {
5294 mode = 0;
5295 } else if (status & TP_EC_FAN_AUTO) {
5296 mode = 2;
5297 } else
5298 mode = 1;
5299
5300 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5301}
5302
5303static ssize_t fan_pwm1_enable_store(struct device *dev,
5304 struct device_attribute *attr,
5305 const char *buf, size_t count)
5306{
5307 unsigned long t;
5308 int res, level;
5309
5310 if (parse_strtoul(buf, 2, &t))
5311 return -EINVAL;
5312
5313 switch (t) {
5314 case 0:
5315 level = TP_EC_FAN_FULLSPEED;
5316 break;
5317 case 1:
5318 level = TPACPI_FAN_LAST_LEVEL;
5319 break;
5320 case 2:
5321 level = TP_EC_FAN_AUTO;
5322 break;
5323 case 3:
5324 /* reserved for software-controlled auto mode */
5325 return -ENOSYS;
5326 default:
5327 return -EINVAL;
5328 }
5329
5330 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005331 if (res == -ENXIO)
5332 return -EINVAL;
5333 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005334 return res;
5335
5336 fan_watchdog_reset();
5337
5338 return count;
5339}
5340
5341static struct device_attribute dev_attr_fan_pwm1_enable =
5342 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5343 fan_pwm1_enable_show, fan_pwm1_enable_store);
5344
5345/* sysfs fan pwm1 ------------------------------------------------------ */
5346static ssize_t fan_pwm1_show(struct device *dev,
5347 struct device_attribute *attr,
5348 char *buf)
5349{
5350 int res;
5351 u8 status;
5352
5353 res = fan_get_status_safe(&status);
5354 if (res)
5355 return res;
5356
5357 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5358 if (status != fan_control_initial_status) {
5359 tp_features.fan_ctrl_status_undef = 0;
5360 } else {
5361 status = TP_EC_FAN_AUTO;
5362 }
5363 }
5364
5365 if ((status &
5366 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5367 status = fan_control_desired_level;
5368
5369 if (status > 7)
5370 status = 7;
5371
5372 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5373}
5374
5375static ssize_t fan_pwm1_store(struct device *dev,
5376 struct device_attribute *attr,
5377 const char *buf, size_t count)
5378{
5379 unsigned long s;
5380 int rc;
5381 u8 status, newlevel;
5382
5383 if (parse_strtoul(buf, 255, &s))
5384 return -EINVAL;
5385
5386 /* scale down from 0-255 to 0-7 */
5387 newlevel = (s >> 5) & 0x07;
5388
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005389 if (mutex_lock_interruptible(&fan_mutex))
5390 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005391
5392 rc = fan_get_status(&status);
5393 if (!rc && (status &
5394 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5395 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005396 if (rc == -ENXIO)
5397 rc = -EINVAL;
5398 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005399 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005400 fan_watchdog_reset();
5401 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005402 }
5403
5404 mutex_unlock(&fan_mutex);
5405 return (rc)? rc : count;
5406}
5407
5408static struct device_attribute dev_attr_fan_pwm1 =
5409 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5410 fan_pwm1_show, fan_pwm1_store);
5411
5412/* sysfs fan fan1_input ------------------------------------------------ */
5413static ssize_t fan_fan1_input_show(struct device *dev,
5414 struct device_attribute *attr,
5415 char *buf)
5416{
5417 int res;
5418 unsigned int speed;
5419
5420 res = fan_get_speed(&speed);
5421 if (res < 0)
5422 return res;
5423
5424 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5425}
5426
5427static struct device_attribute dev_attr_fan_fan1_input =
5428 __ATTR(fan1_input, S_IRUGO,
5429 fan_fan1_input_show, NULL);
5430
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005431/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005432static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5433 char *buf)
5434{
5435 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5436}
5437
5438static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5439 const char *buf, size_t count)
5440{
5441 unsigned long t;
5442
5443 if (parse_strtoul(buf, 120, &t))
5444 return -EINVAL;
5445
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005446 if (!fan_control_allowed)
5447 return -EPERM;
5448
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005449 fan_watchdog_maxinterval = t;
5450 fan_watchdog_reset();
5451
5452 return count;
5453}
5454
5455static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5456 fan_fan_watchdog_show, fan_fan_watchdog_store);
5457
5458/* --------------------------------------------------------------------- */
5459static struct attribute *fan_attributes[] = {
5460 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5461 &dev_attr_fan_fan1_input.attr,
5462 NULL
5463};
5464
5465static const struct attribute_group fan_attr_group = {
5466 .attrs = fan_attributes,
5467};
5468
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005469static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005470{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005471 int rc;
5472
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005473 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5474
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005475 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005476 fan_status_access_mode = TPACPI_FAN_NONE;
5477 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005478 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005479 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005480 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005481 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005482
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005483 TPACPI_ACPIHANDLE_INIT(fans);
5484 TPACPI_ACPIHANDLE_INIT(gfan);
5485 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005486
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005487 if (gfan_handle) {
5488 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005489 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005490 } else {
5491 /* all other ThinkPads: note that even old-style
5492 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005493 if (likely(acpi_ec_read(fan_status_offset,
5494 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005495 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005496
5497 /* In some ThinkPads, neither the EC nor the ACPI
5498 * DSDT initialize the fan status, and it ends up
5499 * being set to 0x07 when it *could* be either
5500 * 0x07 or 0x80.
5501 *
5502 * Enable for TP-1Y (T43), TP-78 (R51e),
5503 * TP-76 (R52), TP-70 (T43, R52), which are known
5504 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005505 if (fan_control_initial_status == 0x07) {
5506 switch (thinkpad_id.ec_model) {
5507 case 0x5931: /* TP-1Y */
5508 case 0x3837: /* TP-78 */
5509 case 0x3637: /* TP-76 */
5510 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005511 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005512 "fan_init: initial fan status "
5513 "is unknown, assuming it is "
5514 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005515 tp_features.fan_ctrl_status_undef = 1;
5516 ;;
5517 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005518 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005519 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005520 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005521 "ThinkPad ACPI EC access misbehaving, "
5522 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005523 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005524 }
5525 }
5526
5527 if (sfan_handle) {
5528 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005529 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005530 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005531 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005532 } else {
5533 if (!gfan_handle) {
5534 /* gfan without sfan means no fan control */
5535 /* all other models implement TP EC 0x2f control */
5536
5537 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005538 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005539 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005540 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005541 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005542 TPACPI_FAN_CMD_SPEED |
5543 TPACPI_FAN_CMD_LEVEL |
5544 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005545 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005546 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005547 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005548 TPACPI_FAN_CMD_LEVEL |
5549 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005550 }
5551 }
5552 }
5553
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005554 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5555 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5556 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5557 fan_status_access_mode, fan_control_access_mode);
5558
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005559 /* fan control master switch */
5560 if (!fan_control_allowed) {
5561 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5562 fan_control_commands = 0;
5563 dbg_printk(TPACPI_DBG_INIT,
5564 "fan control features disabled by parameter\n");
5565 }
5566
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005567 /* update fan_control_desired_level */
5568 if (fan_status_access_mode != TPACPI_FAN_NONE)
5569 fan_get_status_safe(NULL);
5570
5571 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5572 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005573 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005574 &fan_attr_group);
5575 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005576 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005577 &driver_attr_fan_watchdog);
5578 if (rc < 0)
5579 return rc;
5580 return 0;
5581 } else
5582 return 1;
5583}
5584
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005585static void fan_exit(void)
5586{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005587 vdbg_printk(TPACPI_DBG_EXIT,
5588 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005589
5590 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005591 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005592 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5593 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005594
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005595 cancel_delayed_work(&fan_watchdog_task);
5596 flush_scheduled_work();
5597}
5598
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005599static int fan_read(char *p)
5600{
5601 int len = 0;
5602 int rc;
5603 u8 status;
5604 unsigned int speed = 0;
5605
5606 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005607 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005608 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005609 rc = fan_get_status_safe(&status);
5610 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005611 return rc;
5612
5613 len += sprintf(p + len, "status:\t\t%s\n"
5614 "level:\t\t%d\n",
5615 (status != 0) ? "enabled" : "disabled", status);
5616 break;
5617
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005618 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005619 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005620 rc = fan_get_status_safe(&status);
5621 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005622 return rc;
5623
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005624 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005625 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005626 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005627 else
5628 /* Return most likely status. In fact, it
5629 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005630 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005631 }
5632
5633 len += sprintf(p + len, "status:\t\t%s\n",
5634 (status != 0) ? "enabled" : "disabled");
5635
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005636 rc = fan_get_speed(&speed);
5637 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005638 return rc;
5639
5640 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5641
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005642 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005643 /* Disengaged mode takes precedence */
5644 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005645 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005646 len += sprintf(p + len, "level:\t\tauto\n");
5647 else
5648 len += sprintf(p + len, "level:\t\t%d\n", status);
5649 break;
5650
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005651 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005652 default:
5653 len += sprintf(p + len, "status:\t\tnot supported\n");
5654 }
5655
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005656 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005657 len += sprintf(p + len, "commands:\tlevel <level>");
5658
5659 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005660 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005661 len += sprintf(p + len, " (<level> is 0-7)\n");
5662 break;
5663
5664 default:
5665 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005666 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005667 break;
5668 }
5669 }
5670
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005671 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005672 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005673 "commands:\twatchdog <timeout> (<timeout> "
5674 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005675
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005676 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005677 len += sprintf(p + len, "commands:\tspeed <speed>"
5678 " (<speed> is 0-65535)\n");
5679
5680 return len;
5681}
5682
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005683static int fan_write_cmd_level(const char *cmd, int *rc)
5684{
5685 int level;
5686
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005687 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005688 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005689 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005690 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005691 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005692 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005693 return 0;
5694
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005695 *rc = fan_set_level_safe(level);
5696 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005697 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005698 "access mode %d", fan_control_access_mode);
5699
5700 return 1;
5701}
5702
5703static int fan_write_cmd_enable(const char *cmd, int *rc)
5704{
5705 if (strlencmp(cmd, "enable") != 0)
5706 return 0;
5707
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005708 *rc = fan_set_enable();
5709 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005710 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005711 "access mode %d", fan_control_access_mode);
5712
5713 return 1;
5714}
5715
5716static int fan_write_cmd_disable(const char *cmd, int *rc)
5717{
5718 if (strlencmp(cmd, "disable") != 0)
5719 return 0;
5720
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005721 *rc = fan_set_disable();
5722 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005723 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005724 "access mode %d", fan_control_access_mode);
5725
5726 return 1;
5727}
5728
5729static int fan_write_cmd_speed(const char *cmd, int *rc)
5730{
5731 int speed;
5732
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005733 /* TODO:
5734 * Support speed <low> <medium> <high> ? */
5735
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005736 if (sscanf(cmd, "speed %d", &speed) != 1)
5737 return 0;
5738
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005739 *rc = fan_set_speed(speed);
5740 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005741 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005742 "access mode %d", fan_control_access_mode);
5743
5744 return 1;
5745}
5746
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005747static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5748{
5749 int interval;
5750
5751 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5752 return 0;
5753
5754 if (interval < 0 || interval > 120)
5755 *rc = -EINVAL;
5756 else
5757 fan_watchdog_maxinterval = interval;
5758
5759 return 1;
5760}
5761
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005762static int fan_write(char *buf)
5763{
5764 char *cmd;
5765 int rc = 0;
5766
5767 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005768 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005769 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005770 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005771 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005772 fan_write_cmd_disable(cmd, &rc) ||
5773 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005774 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005775 fan_write_cmd_speed(cmd, &rc))
5776 )
5777 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005778 else if (!rc)
5779 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005780 }
5781
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005782 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005783}
5784
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005785static struct ibm_struct fan_driver_data = {
5786 .name = "fan",
5787 .read = fan_read,
5788 .write = fan_write,
5789 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005790};
5791
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005792/****************************************************************************
5793 ****************************************************************************
5794 *
5795 * Infrastructure
5796 *
5797 ****************************************************************************
5798 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005800/* sysfs name ---------------------------------------------------------- */
5801static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5802 struct device_attribute *attr,
5803 char *buf)
5804{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005805 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005806}
5807
5808static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5809 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5810
5811/* --------------------------------------------------------------------- */
5812
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005813/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005814static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005815
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005816/*
5817 * Module and infrastructure proble, init and exit handling
5818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005820static int force_load;
5821
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005822#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005823static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005824{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005825 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005826
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005827 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005828}
5829#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5830
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005831static void ibm_exit(struct ibm_struct *ibm)
5832{
5833 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5834
5835 list_del_init(&ibm->all_drivers);
5836
5837 if (ibm->flags.acpi_notify_installed) {
5838 dbg_printk(TPACPI_DBG_EXIT,
5839 "%s: acpi_remove_notify_handler\n", ibm->name);
5840 BUG_ON(!ibm->acpi);
5841 acpi_remove_notify_handler(*ibm->acpi->handle,
5842 ibm->acpi->type,
5843 dispatch_acpi_notify);
5844 ibm->flags.acpi_notify_installed = 0;
5845 ibm->flags.acpi_notify_installed = 0;
5846 }
5847
5848 if (ibm->flags.proc_created) {
5849 dbg_printk(TPACPI_DBG_EXIT,
5850 "%s: remove_proc_entry\n", ibm->name);
5851 remove_proc_entry(ibm->name, proc_dir);
5852 ibm->flags.proc_created = 0;
5853 }
5854
5855 if (ibm->flags.acpi_driver_registered) {
5856 dbg_printk(TPACPI_DBG_EXIT,
5857 "%s: acpi_bus_unregister_driver\n", ibm->name);
5858 BUG_ON(!ibm->acpi);
5859 acpi_bus_unregister_driver(ibm->acpi->driver);
5860 kfree(ibm->acpi->driver);
5861 ibm->acpi->driver = NULL;
5862 ibm->flags.acpi_driver_registered = 0;
5863 }
5864
5865 if (ibm->flags.init_called && ibm->exit) {
5866 ibm->exit();
5867 ibm->flags.init_called = 0;
5868 }
5869
5870 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5871}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005872
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005873static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874{
5875 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005876 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 struct proc_dir_entry *entry;
5878
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005879 BUG_ON(ibm == NULL);
5880
5881 INIT_LIST_HEAD(&ibm->all_drivers);
5882
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005883 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 return 0;
5885
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005886 dbg_printk(TPACPI_DBG_INIT,
5887 "probing for %s\n", ibm->name);
5888
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005889 if (iibm->init) {
5890 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005891 if (ret > 0)
5892 return 0; /* probe failed */
5893 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005895
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005896 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 }
5898
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005899 if (ibm->acpi) {
5900 if (ibm->acpi->hid) {
5901 ret = register_tpacpi_subdriver(ibm);
5902 if (ret)
5903 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005904 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005905
5906 if (ibm->acpi->notify) {
5907 ret = setup_acpi_notify(ibm);
5908 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005909 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005910 ibm->name);
5911 ret = 0;
5912 goto err_out;
5913 }
5914 if (ret < 0)
5915 goto err_out;
5916 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005917 }
5918
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005919 dbg_printk(TPACPI_DBG_INIT,
5920 "%s installed\n", ibm->name);
5921
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005922 if (ibm->read) {
5923 entry = create_proc_entry(ibm->name,
5924 S_IFREG | S_IRUGO | S_IWUSR,
5925 proc_dir);
5926 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005927 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005928 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005929 ret = -ENODEV;
5930 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005931 }
5932 entry->owner = THIS_MODULE;
5933 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005934 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005935 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005936 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005937 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005940 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5941
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005943
5944err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005945 dbg_printk(TPACPI_DBG_INIT,
5946 "%s: at error exit path with result %d\n",
5947 ibm->name, ret);
5948
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005949 ibm_exit(ibm);
5950 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951}
5952
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005953/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005955static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005956{
Jeff Garzik18552562007-10-03 15:15:40 -04005957 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005958 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005959
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005960 if (!tp)
5961 return;
5962
5963 memset(tp, 0, sizeof(*tp));
5964
5965 if (dmi_name_in_vendors("IBM"))
5966 tp->vendor = PCI_VENDOR_ID_IBM;
5967 else if (dmi_name_in_vendors("LENOVO"))
5968 tp->vendor = PCI_VENDOR_ID_LENOVO;
5969 else
5970 return;
5971
5972 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5973 GFP_KERNEL);
5974 if (!tp->bios_version_str)
5975 return;
5976 tp->bios_model = tp->bios_version_str[0]
5977 | (tp->bios_version_str[1] << 8);
5978
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005979 /*
5980 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5981 * X32 or newer, all Z series; Some models must have an
5982 * up-to-date BIOS or they will not be detected.
5983 *
5984 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5985 */
5986 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005987 if (sscanf(dev->name,
5988 "IBM ThinkPad Embedded Controller -[%17c",
5989 ec_fw_string) == 1) {
5990 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5991 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005992
5993 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5994 tp->ec_model = ec_fw_string[0]
5995 | (ec_fw_string[1] << 8);
5996 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005997 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005998 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005999
6000 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6001 GFP_KERNEL);
6002 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
6003 kfree(tp->model_str);
6004 tp->model_str = NULL;
6005 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006006
6007 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6008 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009}
6010
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006011static int __init probe_for_thinkpad(void)
6012{
6013 int is_thinkpad;
6014
6015 if (acpi_disabled)
6016 return -ENODEV;
6017
6018 /*
6019 * Non-ancient models have better DMI tagging, but very old models
6020 * don't.
6021 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006022 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006023
6024 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006025 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006026 if (!ec_handle) {
6027 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006028 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006029 "Not yet supported ThinkPad detected!\n");
6030 return -ENODEV;
6031 }
6032
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006033 /*
6034 * Risks a regression on very old machines, but reduces potential
6035 * false positives a damn great deal
6036 */
6037 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006038 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006039
6040 if (!is_thinkpad && !force_load)
6041 return -ENODEV;
6042
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006043 return 0;
6044}
6045
6046
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006047/* Module init, exit, parameters */
6048
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006049static struct ibm_init_struct ibms_init[] __initdata = {
6050 {
6051 .init = thinkpad_acpi_driver_init,
6052 .data = &thinkpad_acpi_driver_data,
6053 },
6054 {
6055 .init = hotkey_init,
6056 .data = &hotkey_driver_data,
6057 },
6058 {
6059 .init = bluetooth_init,
6060 .data = &bluetooth_driver_data,
6061 },
6062 {
6063 .init = wan_init,
6064 .data = &wan_driver_data,
6065 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006066#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006067 {
6068 .init = video_init,
6069 .data = &video_driver_data,
6070 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006071#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006072 {
6073 .init = light_init,
6074 .data = &light_driver_data,
6075 },
6076#ifdef CONFIG_THINKPAD_ACPI_DOCK
6077 {
6078 .init = dock_init,
6079 .data = &dock_driver_data[0],
6080 },
6081 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03006082 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006083 .data = &dock_driver_data[1],
6084 },
6085#endif
6086#ifdef CONFIG_THINKPAD_ACPI_BAY
6087 {
6088 .init = bay_init,
6089 .data = &bay_driver_data,
6090 },
6091#endif
6092 {
6093 .init = cmos_init,
6094 .data = &cmos_driver_data,
6095 },
6096 {
6097 .init = led_init,
6098 .data = &led_driver_data,
6099 },
6100 {
6101 .init = beep_init,
6102 .data = &beep_driver_data,
6103 },
6104 {
6105 .init = thermal_init,
6106 .data = &thermal_driver_data,
6107 },
6108 {
6109 .data = &ecdump_driver_data,
6110 },
6111 {
6112 .init = brightness_init,
6113 .data = &brightness_driver_data,
6114 },
6115 {
6116 .data = &volume_driver_data,
6117 },
6118 {
6119 .init = fan_init,
6120 .data = &fan_driver_data,
6121 },
6122};
6123
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006124static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6125{
6126 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006127 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006128
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006129 if (!kp || !kp->name || !val)
6130 return -EINVAL;
6131
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006132 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6133 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006134 WARN_ON(ibm == NULL);
6135
6136 if (!ibm || !ibm->name)
6137 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006138
6139 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6140 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006141 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006142 strcpy(ibms_init[i].param, val);
6143 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006144 return 0;
6145 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006146 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006147
6148 return -EINVAL;
6149}
6150
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006151module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006152MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006153 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006154
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006155module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006156MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006157
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006158module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006159MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006160 "Attempts to load the driver even on a "
6161 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006162
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006163module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006164MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006165 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006166
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006167module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006168MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006169 "Selects brightness control strategy: "
6170 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006171
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006172module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006173MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006174 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006175
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006176module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006177MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006178 "used for backwards compatibility with userspace, "
6179 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006180
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006181#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006182 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006183 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006184 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006185
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006186TPACPI_PARAM(hotkey);
6187TPACPI_PARAM(bluetooth);
6188TPACPI_PARAM(video);
6189TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006190#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006191TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006192#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006193#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006194TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006195#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006196TPACPI_PARAM(cmos);
6197TPACPI_PARAM(led);
6198TPACPI_PARAM(beep);
6199TPACPI_PARAM(ecdump);
6200TPACPI_PARAM(brightness);
6201TPACPI_PARAM(volume);
6202TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006203
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006204static void thinkpad_acpi_module_exit(void)
6205{
6206 struct ibm_struct *ibm, *itmp;
6207
6208 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6209
6210 list_for_each_entry_safe_reverse(ibm, itmp,
6211 &tpacpi_all_drivers,
6212 all_drivers) {
6213 ibm_exit(ibm);
6214 }
6215
6216 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6217
6218 if (tpacpi_inputdev) {
6219 if (tp_features.input_device_registered)
6220 input_unregister_device(tpacpi_inputdev);
6221 else
6222 input_free_device(tpacpi_inputdev);
6223 }
6224
6225 if (tpacpi_hwmon)
6226 hwmon_device_unregister(tpacpi_hwmon);
6227
6228 if (tp_features.sensors_pdev_attrs_registered)
6229 device_remove_file(&tpacpi_sensors_pdev->dev,
6230 &dev_attr_thinkpad_acpi_pdev_name);
6231 if (tpacpi_sensors_pdev)
6232 platform_device_unregister(tpacpi_sensors_pdev);
6233 if (tpacpi_pdev)
6234 platform_device_unregister(tpacpi_pdev);
6235
6236 if (tp_features.sensors_pdrv_attrs_registered)
6237 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6238 if (tp_features.platform_drv_attrs_registered)
6239 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6240
6241 if (tp_features.sensors_pdrv_registered)
6242 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6243
6244 if (tp_features.platform_drv_registered)
6245 platform_driver_unregister(&tpacpi_pdriver);
6246
6247 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006248 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006249
6250 kfree(thinkpad_id.bios_version_str);
6251 kfree(thinkpad_id.ec_version_str);
6252 kfree(thinkpad_id.model_str);
6253}
6254
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006255
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006256static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257{
6258 int ret, i;
6259
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006260 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6261
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006262 /* Parameter checking */
6263 if (hotkey_report_mode > 2)
6264 return -EINVAL;
6265
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006266 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006267
6268 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006269 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006270 if (ret) {
6271 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006272 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006275 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006276
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006277 TPACPI_ACPIHANDLE_INIT(ecrd);
6278 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006280 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006282 printk(TPACPI_ERR
6283 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006284 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 return -ENODEV;
6286 }
6287 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006288
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006289 ret = platform_driver_register(&tpacpi_pdriver);
6290 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006291 printk(TPACPI_ERR
6292 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006293 thinkpad_acpi_module_exit();
6294 return ret;
6295 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006296 tp_features.platform_drv_registered = 1;
6297
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006298 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6299 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006300 printk(TPACPI_ERR
6301 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006302 thinkpad_acpi_module_exit();
6303 return ret;
6304 }
6305 tp_features.sensors_pdrv_registered = 1;
6306
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006307 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006308 if (!ret) {
6309 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006310 ret = tpacpi_create_driver_attributes(
6311 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006312 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006313 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006314 printk(TPACPI_ERR
6315 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006316 thinkpad_acpi_module_exit();
6317 return ret;
6318 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006319 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006320
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006321
6322 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006323 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006324 NULL, 0);
6325 if (IS_ERR(tpacpi_pdev)) {
6326 ret = PTR_ERR(tpacpi_pdev);
6327 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006328 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006329 thinkpad_acpi_module_exit();
6330 return ret;
6331 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006332 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006333 TPACPI_HWMON_DRVR_NAME,
6334 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006335 if (IS_ERR(tpacpi_sensors_pdev)) {
6336 ret = PTR_ERR(tpacpi_sensors_pdev);
6337 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006338 printk(TPACPI_ERR
6339 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006340 thinkpad_acpi_module_exit();
6341 return ret;
6342 }
6343 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6344 &dev_attr_thinkpad_acpi_pdev_name);
6345 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006346 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006347 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006348 thinkpad_acpi_module_exit();
6349 return ret;
6350 }
6351 tp_features.sensors_pdev_attrs_registered = 1;
6352 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006353 if (IS_ERR(tpacpi_hwmon)) {
6354 ret = PTR_ERR(tpacpi_hwmon);
6355 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006356 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006357 thinkpad_acpi_module_exit();
6358 return ret;
6359 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006360 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006361 tpacpi_inputdev = input_allocate_device();
6362 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006363 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006364 thinkpad_acpi_module_exit();
6365 return -ENOMEM;
6366 } else {
6367 /* Prepare input device, but don't register */
6368 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006369 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006370 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006371 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6372 thinkpad_id.vendor :
6373 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006374 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6375 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6376 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006377 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6378 ret = ibm_init(&ibms_init[i]);
6379 if (ret >= 0 && *ibms_init[i].param)
6380 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006382 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 return ret;
6384 }
6385 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006386 ret = input_register_device(tpacpi_inputdev);
6387 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006388 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006389 thinkpad_acpi_module_exit();
6390 return ret;
6391 } else {
6392 tp_features.input_device_registered = 1;
6393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006395 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 return 0;
6397}
6398
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006399/* Please remove this in year 2009 */
6400MODULE_ALIAS("ibm_acpi");
6401
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03006402MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6403
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006404/*
6405 * DMI matching for module autoloading
6406 *
6407 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6408 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6409 *
6410 * Only models listed in thinkwiki will be supported, so add yours
6411 * if it is not there yet.
6412 */
6413#define IBM_BIOS_MODULE_ALIAS(__type) \
6414 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6415
6416/* Non-ancient thinkpads */
6417MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6418MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6419
6420/* Ancient thinkpad BIOSes have to be identified by
6421 * BIOS type or model number, and there are far less
6422 * BIOS types than model numbers... */
6423IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6424IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6425IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6426
6427MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006428MODULE_DESCRIPTION(TPACPI_DESC);
6429MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006430MODULE_LICENSE("GPL");
6431
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006432module_init(thinkpad_acpi_module_init);
6433module_exit(thinkpad_acpi_module_exit);