blob: 4ea3866ddf9056fe5eb89de4100385a4a6442ab3 [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>
70#include <asm/uaccess.h>
71
72#include <linux/dmi.h>
73#include <linux/jiffies.h>
74#include <linux/workqueue.h>
75
76#include <acpi/acpi_drivers.h>
77#include <acpi/acnamesp.h>
78
79#include <linux/pci_ids.h>
80
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020081
82/* ThinkPad CMOS commands */
83#define TP_CMOS_VOLUME_DOWN 0
84#define TP_CMOS_VOLUME_UP 1
85#define TP_CMOS_VOLUME_MUTE 2
86#define TP_CMOS_BRIGHTNESS_UP 4
87#define TP_CMOS_BRIGHTNESS_DOWN 5
88
89/* NVRAM Addresses */
90enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
96};
97
98/* NVRAM bit masks */
99enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
113};
114
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200115/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200116#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200117
118/* Input IDs */
119#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120#define TPACPI_HKEY_INPUT_VERSION 0x4101
121
122
123/****************************************************************************
124 * Main driver
125 */
126
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200127#define TPACPI_NAME "thinkpad"
128#define TPACPI_DESC "ThinkPad ACPI Extras"
129#define TPACPI_FILE TPACPI_NAME "_acpi"
130#define TPACPI_URL "http://ibm-acpi.sf.net/"
131#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200133#define TPACPI_PROC_DIR "ibm"
134#define TPACPI_ACPI_EVENT_PREFIX "ibm"
135#define TPACPI_DRVR_NAME TPACPI_FILE
136#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200137
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200138#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200139
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200140/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200141#define TPACPI_LOG TPACPI_FILE ": "
142#define TPACPI_ERR KERN_ERR TPACPI_LOG
143#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144#define TPACPI_INFO KERN_INFO TPACPI_LOG
145#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200146
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200147#define TPACPI_DBG_ALL 0xffff
148#define TPACPI_DBG_ALL 0xffff
149#define TPACPI_DBG_INIT 0x0001
150#define TPACPI_DBG_EXIT 0x0002
151#define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200155#ifdef CONFIG_THINKPAD_ACPI_DEBUG
156#define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158static const char *str_supported(int is_supported);
159#else
160#define vdbg_printk(a_dbg_level, format, arg...)
161#endif
162
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200163#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200166
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200167
168/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200169 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200170 */
171
172struct ibm_struct;
173
174struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
177
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
182};
183
184struct ibm_struct {
185 char *name;
186
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200191 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200192
193 struct list_head all_drivers;
194
195 struct tp_acpi_drv_struct *acpi;
196
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
204};
205
206struct ibm_init_struct {
207 char param[32];
208
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
211};
212
213static struct {
214#ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219#endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
224 u32 light:1;
225 u32 light_status:1;
226 u32 bright_16levels:1;
227 u32 wan:1;
228 u32 fan_ctrl_status_undef:1;
229 u32 input_device_registered:1;
230 u32 platform_drv_registered:1;
231 u32 platform_drv_attrs_registered:1;
232 u32 sensors_pdrv_registered:1;
233 u32 sensors_pdrv_attrs_registered:1;
234 u32 sensors_pdev_attrs_registered:1;
235 u32 hotkey_poll_active:1;
236} tp_features;
237
238struct thinkpad_id_data {
239 unsigned int vendor; /* ThinkPad vendor:
240 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
241
242 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
243 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
244
245 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
246 u16 ec_model;
247
248 char *model_str;
249};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200250static struct thinkpad_id_data thinkpad_id;
251
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300252static enum {
253 TPACPI_LIFE_INIT = 0,
254 TPACPI_LIFE_RUNNING,
255 TPACPI_LIFE_EXITING,
256} tpacpi_lifecycle;
257
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200258static int experimental;
259static u32 dbg_level;
260
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300261/****************************************************************************
262 ****************************************************************************
263 *
264 * ACPI Helpers and device model
265 *
266 ****************************************************************************
267 ****************************************************************************/
268
269/*************************************************************************
270 * ACPI basic handles
271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300273static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200275#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 static acpi_handle object##_handle; \
277 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400278 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 static char *object##_paths[] = { paths }
280
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200281TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400282 "\\_SB.PCI.ISA.EC", /* 570 */
283 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
284 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
285 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
286 "\\_SB.PCI0.ICH3.EC0", /* R31 */
287 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300288 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200290TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
291TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200293TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
294 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400295 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
296 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300297 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400298
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200299TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400300 "^HKEY", /* R30, R31 */
301 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300302 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400303
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200304TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
305 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
306 "\\_SB.PCI0.VID0", /* 770e */
307 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
308 "\\_SB.PCI0.AGP.VID", /* all others */
309 ); /* R30, R31 */
310
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400311
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300312/*************************************************************************
313 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200314 */
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static int acpi_evalf(acpi_handle handle,
317 void *res, char *method, char *fmt, ...)
318{
319 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400320 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200321 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400322 struct acpi_buffer result, *resultp;
323 union acpi_object out_obj;
324 acpi_status status;
325 va_list ap;
326 char res_type;
327 int success;
328 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200331 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
333 }
334
335 if (*fmt == 'q') {
336 quiet = 1;
337 fmt++;
338 } else
339 quiet = 0;
340
341 res_type = *(fmt++);
342
343 params.count = 0;
344 params.pointer = &in_objs[0];
345
346 va_start(ap, fmt);
347 while (*fmt) {
348 char c = *(fmt++);
349 switch (c) {
350 case 'd': /* int */
351 in_objs[params.count].integer.value = va_arg(ap, int);
352 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
353 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400354 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200356 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 "with invalid format character '%c'\n", c);
358 return 0;
359 }
360 }
361 va_end(ap);
362
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400363 if (res_type != 'v') {
364 result.length = sizeof(out_obj);
365 result.pointer = &out_obj;
366 resultp = &result;
367 } else
368 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400370 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400373 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (res)
375 *(int *)res = out_obj.integer.value;
376 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
377 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400378 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 success = status == AE_OK;
380 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400381 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200383 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 "with invalid format character '%c'\n", res_type);
385 return 0;
386 }
387
388 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200389 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 method, fmt0, status);
391
392 return success;
393}
394
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200395static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300396{
397 int v;
398
399 if (ecrd_handle) {
400 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
401 return 0;
402 *p = v;
403 } else {
404 if (ec_read(i, p) < 0)
405 return 0;
406 }
407
408 return 1;
409}
410
411static int acpi_ec_write(int i, u8 v)
412{
413 if (ecwr_handle) {
414 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
415 return 0;
416 } else {
417 if (ec_write(i, v) < 0)
418 return 0;
419 }
420
421 return 1;
422}
423
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200424#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300425static int _sta(acpi_handle handle)
426{
427 int status;
428
429 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
430 status = 0;
431
432 return status;
433}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200434#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300435
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300436static int issue_thinkpad_cmos_command(int cmos_cmd)
437{
438 if (!cmos_handle)
439 return -ENXIO;
440
441 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
442 return -EIO;
443
444 return 0;
445}
446
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300447/*************************************************************************
448 * ACPI device model
449 */
450
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200451#define TPACPI_ACPIHANDLE_INIT(object) \
452 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200453 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
454
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300455static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300456 acpi_handle *handle, acpi_handle parent,
457 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300458{
459 int i;
460 acpi_status status;
461
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300462 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
463 name);
464
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300465 for (i = 0; i < num_paths; i++) {
466 status = acpi_get_handle(parent, paths[i], handle);
467 if (ACPI_SUCCESS(status)) {
468 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300469 dbg_printk(TPACPI_DBG_INIT,
470 "Found ACPI handle %s for %s\n",
471 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300472 return;
473 }
474 }
475
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300476 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
477 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300478 *handle = NULL;
479}
480
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300481static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300482{
483 struct ibm_struct *ibm = data;
484
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300485 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
486 return;
487
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300488 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300489 return;
490
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300491 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300492}
493
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300494static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300495{
496 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300497 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300498
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300499 BUG_ON(!ibm->acpi);
500
501 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300502 return 0;
503
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300504 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300505 "setting up ACPI notify for %s\n", ibm->name);
506
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300507 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
508 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200509 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300510 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300511 return -ENODEV;
512 }
513
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300514 acpi_driver_data(ibm->acpi->device) = ibm;
515 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200516 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300517 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300518
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300519 status = acpi_install_notify_handler(*ibm->acpi->handle,
520 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300521 if (ACPI_FAILURE(status)) {
522 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200523 printk(TPACPI_NOTICE
524 "another device driver is already "
525 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300526 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200527 printk(TPACPI_ERR
528 "acpi_install_notify_handler(%s) failed: %d\n",
529 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300530 }
531 return -ENODEV;
532 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300533 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300534 return 0;
535}
536
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300537static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300538{
539 return 0;
540}
541
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300542static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300544 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300545
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300546 dbg_printk(TPACPI_DBG_INIT,
547 "registering %s as an ACPI driver\n", ibm->name);
548
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300549 BUG_ON(!ibm->acpi);
550
551 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
552 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200553 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300554 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300555 }
556
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200557 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300558 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200559
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300560 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300561
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300562 rc = acpi_bus_register_driver(ibm->acpi->driver);
563 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200564 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200565 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300566 kfree(ibm->acpi->driver);
567 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300568 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300569 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300570
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300571 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300572}
573
574
575/****************************************************************************
576 ****************************************************************************
577 *
578 * Procfs Helpers
579 *
580 ****************************************************************************
581 ****************************************************************************/
582
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300583static int dispatch_procfs_read(char *page, char **start, off_t off,
584 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300585{
586 struct ibm_struct *ibm = data;
587 int len;
588
589 if (!ibm || !ibm->read)
590 return -EINVAL;
591
592 len = ibm->read(page);
593 if (len < 0)
594 return len;
595
596 if (len <= off + count)
597 *eof = 1;
598 *start = page + off;
599 len -= off;
600 if (len > count)
601 len = count;
602 if (len < 0)
603 len = 0;
604
605 return len;
606}
607
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300608static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200609 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300610 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300611{
612 struct ibm_struct *ibm = data;
613 char *kernbuf;
614 int ret;
615
616 if (!ibm || !ibm->write)
617 return -EINVAL;
618
619 kernbuf = kmalloc(count + 2, GFP_KERNEL);
620 if (!kernbuf)
621 return -ENOMEM;
622
623 if (copy_from_user(kernbuf, userbuf, count)) {
624 kfree(kernbuf);
625 return -EFAULT;
626 }
627
628 kernbuf[count] = 0;
629 strcat(kernbuf, ",");
630 ret = ibm->write(kernbuf);
631 if (ret == 0)
632 ret = count;
633
634 kfree(kernbuf);
635
636 return ret;
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639static char *next_cmd(char **cmds)
640{
641 char *start = *cmds;
642 char *end;
643
644 while ((end = strchr(start, ',')) && end == start)
645 start = end + 1;
646
647 if (!end)
648 return NULL;
649
650 *end = 0;
651 *cmds = end + 1;
652 return start;
653}
654
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300655
656/****************************************************************************
657 ****************************************************************************
658 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300659 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300660 *
661 ****************************************************************************
662 ****************************************************************************/
663
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300664static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300665static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700666static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300667static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300668static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200669static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300670
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200671static int tpacpi_suspend_handler(struct platform_device *pdev,
672 pm_message_t state)
673{
674 struct ibm_struct *ibm, *itmp;
675
676 list_for_each_entry_safe(ibm, itmp,
677 &tpacpi_all_drivers,
678 all_drivers) {
679 if (ibm->suspend)
680 (ibm->suspend)(state);
681 }
682
683 return 0;
684}
685
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300686static int tpacpi_resume_handler(struct platform_device *pdev)
687{
688 struct ibm_struct *ibm, *itmp;
689
690 list_for_each_entry_safe(ibm, itmp,
691 &tpacpi_all_drivers,
692 all_drivers) {
693 if (ibm->resume)
694 (ibm->resume)();
695 }
696
697 return 0;
698}
699
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300700static struct platform_driver tpacpi_pdriver = {
701 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200702 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300703 .owner = THIS_MODULE,
704 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200705 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300706 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300707};
708
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300709static struct platform_driver tpacpi_hwmon_pdriver = {
710 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200711 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300712 .owner = THIS_MODULE,
713 },
714};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300715
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300716/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300717 * sysfs support helpers
718 */
719
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200720struct attribute_set {
721 unsigned int members, max_members;
722 struct attribute_group group;
723};
724
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300725struct attribute_set_obj {
726 struct attribute_set s;
727 struct attribute *a;
728} __attribute__((packed));
729
730static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200731 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300732{
733 struct attribute_set_obj *sobj;
734
735 if (max_members == 0)
736 return NULL;
737
738 /* Allocates space for implicit NULL at the end too */
739 sobj = kzalloc(sizeof(struct attribute_set_obj) +
740 max_members * sizeof(struct attribute *),
741 GFP_KERNEL);
742 if (!sobj)
743 return NULL;
744 sobj->s.max_members = max_members;
745 sobj->s.group.attrs = &sobj->a;
746 sobj->s.group.name = name;
747
748 return &sobj->s;
749}
750
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200751#define destroy_attr_set(_set) \
752 kfree(_set);
753
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300754/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200755static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300756{
757 if (!s || !attr)
758 return -EINVAL;
759
760 if (s->members >= s->max_members)
761 return -ENOMEM;
762
763 s->group.attrs[s->members] = attr;
764 s->members++;
765
766 return 0;
767}
768
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200769static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300770 struct attribute **attr,
771 unsigned int count)
772{
773 int i, res;
774
775 for (i = 0; i < count; i++) {
776 res = add_to_attr_set(s, attr[i]);
777 if (res)
778 return res;
779 }
780
781 return 0;
782}
783
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200784static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300785{
786 sysfs_remove_group(kobj, &s->group);
787 destroy_attr_set(s);
788}
789
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200790#define register_attr_set_with_sysfs(_attr_set, _kobj) \
791 sysfs_create_group(_kobj, &_attr_set->group)
792
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300793static int parse_strtoul(const char *buf,
794 unsigned long max, unsigned long *value)
795{
796 char *endp;
797
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300798 while (*buf && isspace(*buf))
799 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300800 *value = simple_strtoul(buf, &endp, 0);
801 while (*endp && isspace(*endp))
802 endp++;
803 if (*endp || *value > max)
804 return -EINVAL;
805
806 return 0;
807}
808
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200809/*************************************************************************
810 * thinkpad-acpi driver attributes
811 */
812
813/* interface_version --------------------------------------------------- */
814static ssize_t tpacpi_driver_interface_version_show(
815 struct device_driver *drv,
816 char *buf)
817{
818 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
819}
820
821static DRIVER_ATTR(interface_version, S_IRUGO,
822 tpacpi_driver_interface_version_show, NULL);
823
824/* debug_level --------------------------------------------------------- */
825static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
826 char *buf)
827{
828 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
829}
830
831static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
832 const char *buf, size_t count)
833{
834 unsigned long t;
835
836 if (parse_strtoul(buf, 0xffff, &t))
837 return -EINVAL;
838
839 dbg_level = t;
840
841 return count;
842}
843
844static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
845 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
846
847/* version ------------------------------------------------------------- */
848static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
849 char *buf)
850{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200851 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
852 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200853}
854
855static DRIVER_ATTR(version, S_IRUGO,
856 tpacpi_driver_version_show, NULL);
857
858/* --------------------------------------------------------------------- */
859
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200860static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200861 &driver_attr_debug_level, &driver_attr_version,
862 &driver_attr_interface_version,
863};
864
865static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
866{
867 int i, res;
868
869 i = 0;
870 res = 0;
871 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
872 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
873 i++;
874 }
875
876 return res;
877}
878
879static void tpacpi_remove_driver_attributes(struct device_driver *drv)
880{
881 int i;
882
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200883 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200884 driver_remove_file(drv, tpacpi_driver_attributes[i]);
885}
886
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300887/****************************************************************************
888 ****************************************************************************
889 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300890 * Subdrivers
891 *
892 ****************************************************************************
893 ****************************************************************************/
894
895/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300896 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300897 */
898
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300899static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200901 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
902 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200904 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300905 (thinkpad_id.bios_version_str) ?
906 thinkpad_id.bios_version_str : "unknown",
907 (thinkpad_id.ec_version_str) ?
908 thinkpad_id.ec_version_str : "unknown");
909
910 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200911 printk(TPACPI_INFO "%s %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300912 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
913 "IBM" : ((thinkpad_id.vendor ==
914 PCI_VENDOR_ID_LENOVO) ?
915 "Lenovo" : "Unknown vendor"),
916 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 0;
919}
920
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300921static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 int len = 0;
924
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200925 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
926 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 return len;
929}
930
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300931static struct ibm_struct thinkpad_acpi_driver_data = {
932 .name = "driver",
933 .read = thinkpad_acpi_driver_read,
934};
935
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300936/*************************************************************************
937 * Hotkey subdriver
938 */
939
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200940enum { /* hot key scan codes (derived from ACPI DSDT) */
941 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
942 TP_ACPI_HOTKEYSCAN_FNF2,
943 TP_ACPI_HOTKEYSCAN_FNF3,
944 TP_ACPI_HOTKEYSCAN_FNF4,
945 TP_ACPI_HOTKEYSCAN_FNF5,
946 TP_ACPI_HOTKEYSCAN_FNF6,
947 TP_ACPI_HOTKEYSCAN_FNF7,
948 TP_ACPI_HOTKEYSCAN_FNF8,
949 TP_ACPI_HOTKEYSCAN_FNF9,
950 TP_ACPI_HOTKEYSCAN_FNF10,
951 TP_ACPI_HOTKEYSCAN_FNF11,
952 TP_ACPI_HOTKEYSCAN_FNF12,
953 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
954 TP_ACPI_HOTKEYSCAN_FNINSERT,
955 TP_ACPI_HOTKEYSCAN_FNDELETE,
956 TP_ACPI_HOTKEYSCAN_FNHOME,
957 TP_ACPI_HOTKEYSCAN_FNEND,
958 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
959 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
960 TP_ACPI_HOTKEYSCAN_FNSPACE,
961 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
962 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
963 TP_ACPI_HOTKEYSCAN_MUTE,
964 TP_ACPI_HOTKEYSCAN_THINKPAD,
965};
966
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200967enum { /* Keys available through NVRAM polling */
968 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
969 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
970};
971
972enum { /* Positions of some of the keys in hotkey masks */
973 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
974 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
975 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
976 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
977 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
978 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
979 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
980 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
981 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
982 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
983 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
984};
985
986enum { /* NVRAM to ACPI HKEY group map */
987 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
988 TP_ACPI_HKEY_ZOOM_MASK |
989 TP_ACPI_HKEY_DISPSWTCH_MASK |
990 TP_ACPI_HKEY_HIBERNATE_MASK,
991 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
992 TP_ACPI_HKEY_BRGHTDWN_MASK,
993 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
994 TP_ACPI_HKEY_VOLDWN_MASK |
995 TP_ACPI_HKEY_MUTE_MASK,
996};
997
998#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
999struct tp_nvram_state {
1000 u16 thinkpad_toggle:1;
1001 u16 zoom_toggle:1;
1002 u16 display_toggle:1;
1003 u16 thinklight_toggle:1;
1004 u16 hibernate_toggle:1;
1005 u16 displayexp_toggle:1;
1006 u16 display_state:1;
1007 u16 brightness_toggle:1;
1008 u16 volume_toggle:1;
1009 u16 mute:1;
1010
1011 u8 brightness_level;
1012 u8 volume_level;
1013};
1014
1015static struct task_struct *tpacpi_hotkey_task;
1016static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1017static int hotkey_poll_freq = 10; /* Hz */
1018static struct mutex hotkey_thread_mutex;
1019static struct mutex hotkey_thread_data_mutex;
1020static unsigned int hotkey_config_change;
1021
1022#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1023
1024#define hotkey_source_mask 0U
1025
1026#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1027
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001028static struct mutex hotkey_mutex;
1029
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001030static enum { /* Reasons for waking up */
1031 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1032 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1033 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1034} hotkey_wakeup_reason;
1035
1036static int hotkey_autosleep_ack;
1037
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001038static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001039static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001040static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001041static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001042static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001043
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001044static unsigned int hotkey_report_mode;
1045
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001046static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001047
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001048static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001049
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001050#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1051#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001052 do { \
1053 mutex_lock(&hotkey_thread_data_mutex); \
1054 hotkey_config_change++; \
1055 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001056#define HOTKEY_CONFIG_CRITICAL_END \
1057 mutex_unlock(&hotkey_thread_data_mutex);
1058#else
1059#define HOTKEY_CONFIG_CRITICAL_START
1060#define HOTKEY_CONFIG_CRITICAL_END
1061#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1062
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001063static int hotkey_get_wlsw(int *status)
1064{
1065 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1066 return -EIO;
1067 return 0;
1068}
1069
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001070/*
1071 * Call with hotkey_mutex held
1072 */
1073static int hotkey_mask_get(void)
1074{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001075 u32 m = 0;
1076
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001077 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001078 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001079 return -EIO;
1080 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001081 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001082
1083 return 0;
1084}
1085
1086/*
1087 * Call with hotkey_mutex held
1088 */
1089static int hotkey_mask_set(u32 mask)
1090{
1091 int i;
1092 int rc = 0;
1093
1094 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001095 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001096 for (i = 0; i < 32; i++) {
1097 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001098 /* enable in firmware mask only keys not in NVRAM
1099 * mode, but enable the key in the cached hotkey_mask
1100 * regardless of mode, or the key will end up
1101 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001102 if (!acpi_evalf(hkey_handle,
1103 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001104 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001105 rc = -EIO;
1106 break;
1107 } else {
1108 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1109 }
1110 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001111 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001112
1113 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001114 if (!hotkey_mask_get() && !rc &&
1115 (hotkey_mask & ~hotkey_source_mask) !=
1116 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001117 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001118 "requested hot key mask 0x%08x, but "
1119 "firmware forced it to 0x%08x\n",
1120 mask, hotkey_mask);
1121 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001122 } else {
1123#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1124 HOTKEY_CONFIG_CRITICAL_START
1125 hotkey_mask = mask & hotkey_source_mask;
1126 HOTKEY_CONFIG_CRITICAL_END
1127 hotkey_mask_get();
1128 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001129 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001130 "requested hot key mask 0x%08x, "
1131 "forced to 0x%08x (NVRAM poll mask is "
1132 "0x%08x): no firmware mask support\n",
1133 mask, hotkey_mask, hotkey_source_mask);
1134 }
1135#else
1136 hotkey_mask_get();
1137 rc = -ENXIO;
1138#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001139 }
1140
1141 return rc;
1142}
1143
1144static int hotkey_status_get(int *status)
1145{
1146 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1147 return -EIO;
1148
1149 return 0;
1150}
1151
1152static int hotkey_status_set(int status)
1153{
1154 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1155 return -EIO;
1156
1157 return 0;
1158}
1159
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001160static void tpacpi_input_send_radiosw(void)
1161{
1162 int wlsw;
1163
1164 mutex_lock(&tpacpi_inputdev_send_mutex);
1165
1166 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1167 input_report_switch(tpacpi_inputdev,
1168 SW_RADIO, !!wlsw);
1169 input_sync(tpacpi_inputdev);
1170 }
1171
1172 mutex_unlock(&tpacpi_inputdev_send_mutex);
1173}
1174
1175static void tpacpi_input_send_key(unsigned int scancode)
1176{
1177 unsigned int keycode;
1178
1179 keycode = hotkey_keycode_map[scancode];
1180
1181 if (keycode != KEY_RESERVED) {
1182 mutex_lock(&tpacpi_inputdev_send_mutex);
1183
1184 input_report_key(tpacpi_inputdev, keycode, 1);
1185 if (keycode == KEY_UNKNOWN)
1186 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1187 scancode);
1188 input_sync(tpacpi_inputdev);
1189
1190 input_report_key(tpacpi_inputdev, keycode, 0);
1191 if (keycode == KEY_UNKNOWN)
1192 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1193 scancode);
1194 input_sync(tpacpi_inputdev);
1195
1196 mutex_unlock(&tpacpi_inputdev_send_mutex);
1197 }
1198}
1199
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001200#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1201static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1202
1203static void tpacpi_hotkey_send_key(unsigned int scancode)
1204{
1205 tpacpi_input_send_key(scancode);
1206 if (hotkey_report_mode < 2) {
1207 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1208 0x80, 0x1001 + scancode);
1209 }
1210}
1211
1212static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1213{
1214 u8 d;
1215
1216 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1217 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1218 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1219 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1220 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1221 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1222 }
1223 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1224 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1225 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1226 }
1227 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1228 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1229 n->displayexp_toggle =
1230 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1231 }
1232 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1233 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1234 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1235 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1236 n->brightness_toggle =
1237 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1238 }
1239 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1240 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1241 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1242 >> TP_NVRAM_POS_LEVEL_VOLUME;
1243 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1244 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1245 }
1246}
1247
1248#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001249 do { \
1250 if ((mask & (1 << __scancode)) && \
1251 oldn->__member != newn->__member) \
1252 tpacpi_hotkey_send_key(__scancode); \
1253 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001254
1255#define TPACPI_MAY_SEND_KEY(__scancode) \
1256 do { if (mask & (1 << __scancode)) \
1257 tpacpi_hotkey_send_key(__scancode); } while (0)
1258
1259static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001260 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001261 u32 mask)
1262{
1263 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1264 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1265 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1266 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1267
1268 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1269
1270 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1271
1272 /* handle volume */
1273 if (oldn->volume_toggle != newn->volume_toggle) {
1274 if (oldn->mute != newn->mute) {
1275 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1276 }
1277 if (oldn->volume_level > newn->volume_level) {
1278 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1279 } else if (oldn->volume_level < newn->volume_level) {
1280 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1281 } else if (oldn->mute == newn->mute) {
1282 /* repeated key presses that didn't change state */
1283 if (newn->mute) {
1284 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1285 } else if (newn->volume_level != 0) {
1286 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1287 } else {
1288 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1289 }
1290 }
1291 }
1292
1293 /* handle brightness */
1294 if (oldn->brightness_toggle != newn->brightness_toggle) {
1295 if (oldn->brightness_level < newn->brightness_level) {
1296 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1297 } else if (oldn->brightness_level > newn->brightness_level) {
1298 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1299 } else {
1300 /* repeated key presses that didn't change state */
1301 if (newn->brightness_level != 0) {
1302 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1303 } else {
1304 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1305 }
1306 }
1307 }
1308}
1309
1310#undef TPACPI_COMPARE_KEY
1311#undef TPACPI_MAY_SEND_KEY
1312
1313static int hotkey_kthread(void *data)
1314{
1315 struct tp_nvram_state s[2];
1316 u32 mask;
1317 unsigned int si, so;
1318 unsigned long t;
1319 unsigned int change_detector, must_reset;
1320
1321 mutex_lock(&hotkey_thread_mutex);
1322
1323 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1324 goto exit;
1325
1326 set_freezable();
1327
1328 so = 0;
1329 si = 1;
1330 t = 0;
1331
1332 /* Initial state for compares */
1333 mutex_lock(&hotkey_thread_data_mutex);
1334 change_detector = hotkey_config_change;
1335 mask = hotkey_source_mask & hotkey_mask;
1336 mutex_unlock(&hotkey_thread_data_mutex);
1337 hotkey_read_nvram(&s[so], mask);
1338
1339 while (!kthread_should_stop() && hotkey_poll_freq) {
1340 if (t == 0)
1341 t = 1000/hotkey_poll_freq;
1342 t = msleep_interruptible(t);
1343 if (unlikely(kthread_should_stop()))
1344 break;
1345 must_reset = try_to_freeze();
1346 if (t > 0 && !must_reset)
1347 continue;
1348
1349 mutex_lock(&hotkey_thread_data_mutex);
1350 if (must_reset || hotkey_config_change != change_detector) {
1351 /* forget old state on thaw or config change */
1352 si = so;
1353 t = 0;
1354 change_detector = hotkey_config_change;
1355 }
1356 mask = hotkey_source_mask & hotkey_mask;
1357 mutex_unlock(&hotkey_thread_data_mutex);
1358
1359 if (likely(mask)) {
1360 hotkey_read_nvram(&s[si], mask);
1361 if (likely(si != so)) {
1362 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001363 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001364 }
1365 }
1366
1367 so = si;
1368 si ^= 1;
1369 }
1370
1371exit:
1372 mutex_unlock(&hotkey_thread_mutex);
1373 return 0;
1374}
1375
1376static void hotkey_poll_stop_sync(void)
1377{
1378 if (tpacpi_hotkey_task) {
1379 if (frozen(tpacpi_hotkey_task) ||
1380 freezing(tpacpi_hotkey_task))
1381 thaw_process(tpacpi_hotkey_task);
1382
1383 kthread_stop(tpacpi_hotkey_task);
1384 tpacpi_hotkey_task = NULL;
1385 mutex_lock(&hotkey_thread_mutex);
1386 /* at this point, the thread did exit */
1387 mutex_unlock(&hotkey_thread_mutex);
1388 }
1389}
1390
1391/* call with hotkey_mutex held */
1392static void hotkey_poll_setup(int may_warn)
1393{
1394 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1395 hotkey_poll_freq > 0 &&
1396 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1397 if (!tpacpi_hotkey_task) {
1398 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001399 NULL,
1400 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001401 if (IS_ERR(tpacpi_hotkey_task)) {
1402 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001403 printk(TPACPI_ERR
1404 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001405 "for hotkey polling\n");
1406 }
1407 }
1408 } else {
1409 hotkey_poll_stop_sync();
1410 if (may_warn &&
1411 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001412 printk(TPACPI_NOTICE
1413 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001414 "which is currently disabled\n",
1415 hotkey_source_mask);
1416 }
1417 }
1418}
1419
1420static void hotkey_poll_setup_safe(int may_warn)
1421{
1422 mutex_lock(&hotkey_mutex);
1423 hotkey_poll_setup(may_warn);
1424 mutex_unlock(&hotkey_mutex);
1425}
1426
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001427#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1428
1429static void hotkey_poll_setup_safe(int __unused)
1430{
1431}
1432
1433#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1434
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001435static int hotkey_inputdev_open(struct input_dev *dev)
1436{
1437 switch (tpacpi_lifecycle) {
1438 case TPACPI_LIFE_INIT:
1439 /*
1440 * hotkey_init will call hotkey_poll_setup_safe
1441 * at the appropriate moment
1442 */
1443 return 0;
1444 case TPACPI_LIFE_EXITING:
1445 return -EBUSY;
1446 case TPACPI_LIFE_RUNNING:
1447 hotkey_poll_setup_safe(0);
1448 return 0;
1449 }
1450
1451 /* Should only happen if tpacpi_lifecycle is corrupt */
1452 BUG();
1453 return -EBUSY;
1454}
1455
1456static void hotkey_inputdev_close(struct input_dev *dev)
1457{
1458 /* disable hotkey polling when possible */
1459 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1460 hotkey_poll_setup_safe(0);
1461}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001462
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001463/* sysfs hotkey enable ------------------------------------------------- */
1464static ssize_t hotkey_enable_show(struct device *dev,
1465 struct device_attribute *attr,
1466 char *buf)
1467{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001468 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001469
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001470 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001471 if (res)
1472 return res;
1473
1474 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1475}
1476
1477static ssize_t hotkey_enable_store(struct device *dev,
1478 struct device_attribute *attr,
1479 const char *buf, size_t count)
1480{
1481 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001482 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001483
1484 if (parse_strtoul(buf, 1, &t))
1485 return -EINVAL;
1486
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001487 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001488
1489 return (res) ? res : count;
1490}
1491
1492static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001493 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001494 hotkey_enable_show, hotkey_enable_store);
1495
1496/* sysfs hotkey mask --------------------------------------------------- */
1497static ssize_t hotkey_mask_show(struct device *dev,
1498 struct device_attribute *attr,
1499 char *buf)
1500{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001501 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001502
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001503 if (mutex_lock_interruptible(&hotkey_mutex))
1504 return -ERESTARTSYS;
1505 res = hotkey_mask_get();
1506 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001507
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001508 return (res)?
1509 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001510}
1511
1512static ssize_t hotkey_mask_store(struct device *dev,
1513 struct device_attribute *attr,
1514 const char *buf, size_t count)
1515{
1516 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001517 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001518
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001519 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001520 return -EINVAL;
1521
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001522 if (mutex_lock_interruptible(&hotkey_mutex))
1523 return -ERESTARTSYS;
1524
1525 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001526
1527#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1528 hotkey_poll_setup(1);
1529#endif
1530
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001531 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001532
1533 return (res) ? res : count;
1534}
1535
1536static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001537 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001538 hotkey_mask_show, hotkey_mask_store);
1539
1540/* sysfs hotkey bios_enabled ------------------------------------------- */
1541static ssize_t hotkey_bios_enabled_show(struct device *dev,
1542 struct device_attribute *attr,
1543 char *buf)
1544{
1545 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1546}
1547
1548static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001549 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001550
1551/* sysfs hotkey bios_mask ---------------------------------------------- */
1552static ssize_t hotkey_bios_mask_show(struct device *dev,
1553 struct device_attribute *attr,
1554 char *buf)
1555{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001556 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001557}
1558
1559static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001560 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001561
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001562/* sysfs hotkey all_mask ----------------------------------------------- */
1563static ssize_t hotkey_all_mask_show(struct device *dev,
1564 struct device_attribute *attr,
1565 char *buf)
1566{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001567 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1568 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001569}
1570
1571static struct device_attribute dev_attr_hotkey_all_mask =
1572 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1573
1574/* sysfs hotkey recommended_mask --------------------------------------- */
1575static ssize_t hotkey_recommended_mask_show(struct device *dev,
1576 struct device_attribute *attr,
1577 char *buf)
1578{
1579 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001580 (hotkey_all_mask | hotkey_source_mask)
1581 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001582}
1583
1584static struct device_attribute dev_attr_hotkey_recommended_mask =
1585 __ATTR(hotkey_recommended_mask, S_IRUGO,
1586 hotkey_recommended_mask_show, NULL);
1587
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001588#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1589
1590/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1591static ssize_t hotkey_source_mask_show(struct device *dev,
1592 struct device_attribute *attr,
1593 char *buf)
1594{
1595 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1596}
1597
1598static ssize_t hotkey_source_mask_store(struct device *dev,
1599 struct device_attribute *attr,
1600 const char *buf, size_t count)
1601{
1602 unsigned long t;
1603
1604 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1605 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1606 return -EINVAL;
1607
1608 if (mutex_lock_interruptible(&hotkey_mutex))
1609 return -ERESTARTSYS;
1610
1611 HOTKEY_CONFIG_CRITICAL_START
1612 hotkey_source_mask = t;
1613 HOTKEY_CONFIG_CRITICAL_END
1614
1615 hotkey_poll_setup(1);
1616
1617 mutex_unlock(&hotkey_mutex);
1618
1619 return count;
1620}
1621
1622static struct device_attribute dev_attr_hotkey_source_mask =
1623 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1624 hotkey_source_mask_show, hotkey_source_mask_store);
1625
1626/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1627static ssize_t hotkey_poll_freq_show(struct device *dev,
1628 struct device_attribute *attr,
1629 char *buf)
1630{
1631 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1632}
1633
1634static ssize_t hotkey_poll_freq_store(struct device *dev,
1635 struct device_attribute *attr,
1636 const char *buf, size_t count)
1637{
1638 unsigned long t;
1639
1640 if (parse_strtoul(buf, 25, &t))
1641 return -EINVAL;
1642
1643 if (mutex_lock_interruptible(&hotkey_mutex))
1644 return -ERESTARTSYS;
1645
1646 hotkey_poll_freq = t;
1647
1648 hotkey_poll_setup(1);
1649 mutex_unlock(&hotkey_mutex);
1650
1651 return count;
1652}
1653
1654static struct device_attribute dev_attr_hotkey_poll_freq =
1655 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1656 hotkey_poll_freq_show, hotkey_poll_freq_store);
1657
1658#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1659
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001660/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001661static ssize_t hotkey_radio_sw_show(struct device *dev,
1662 struct device_attribute *attr,
1663 char *buf)
1664{
1665 int res, s;
1666 res = hotkey_get_wlsw(&s);
1667 if (res < 0)
1668 return res;
1669
1670 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1671}
1672
1673static struct device_attribute dev_attr_hotkey_radio_sw =
1674 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1675
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001676static void hotkey_radio_sw_notify_change(void)
1677{
1678 if (tp_features.hotkey_wlsw)
1679 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1680 "hotkey_radio_sw");
1681}
1682
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001683/* sysfs hotkey report_mode -------------------------------------------- */
1684static ssize_t hotkey_report_mode_show(struct device *dev,
1685 struct device_attribute *attr,
1686 char *buf)
1687{
1688 return snprintf(buf, PAGE_SIZE, "%d\n",
1689 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1690}
1691
1692static struct device_attribute dev_attr_hotkey_report_mode =
1693 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1694
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001695/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001696static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1697 struct device_attribute *attr,
1698 char *buf)
1699{
1700 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1701}
1702
1703static struct device_attribute dev_attr_hotkey_wakeup_reason =
1704 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1705
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001706static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001707{
1708 if (tp_features.hotkey_mask)
1709 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1710 "wakeup_reason");
1711}
1712
1713/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001714static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1715 struct device_attribute *attr,
1716 char *buf)
1717{
1718 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1719}
1720
1721static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1722 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1723 hotkey_wakeup_hotunplug_complete_show, NULL);
1724
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001725static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001726{
1727 if (tp_features.hotkey_mask)
1728 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1729 "wakeup_hotunplug_complete");
1730}
1731
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001732/* --------------------------------------------------------------------- */
1733
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001734static struct attribute *hotkey_attributes[] __initdata = {
1735 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001736 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001737 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001738#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1739 &dev_attr_hotkey_mask.attr,
1740 &dev_attr_hotkey_all_mask.attr,
1741 &dev_attr_hotkey_recommended_mask.attr,
1742 &dev_attr_hotkey_source_mask.attr,
1743 &dev_attr_hotkey_poll_freq.attr,
1744#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001745};
1746
1747static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001748 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001749#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1750 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001751 &dev_attr_hotkey_all_mask.attr,
1752 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001753#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001754 &dev_attr_hotkey_wakeup_reason.attr,
1755 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001756};
1757
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001758static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001759{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001760 /* Requirements for changing the default keymaps:
1761 *
1762 * 1. Many of the keys are mapped to KEY_RESERVED for very
1763 * good reasons. Do not change them unless you have deep
1764 * knowledge on the IBM and Lenovo ThinkPad firmware for
1765 * the various ThinkPad models. The driver behaves
1766 * differently for KEY_RESERVED: such keys have their
1767 * hot key mask *unset* in mask_recommended, and also
1768 * in the initial hot key mask programmed into the
1769 * firmware at driver load time, which means the firm-
1770 * ware may react very differently if you change them to
1771 * something else;
1772 *
1773 * 2. You must be subscribed to the linux-thinkpad and
1774 * ibm-acpi-devel mailing lists, and you should read the
1775 * list archives since 2007 if you want to change the
1776 * keymaps. This requirement exists so that you will
1777 * know the past history of problems with the thinkpad-
1778 * acpi driver keymaps, and also that you will be
1779 * listening to any bug reports;
1780 *
1781 * 3. Do not send thinkpad-acpi specific patches directly to
1782 * for merging, *ever*. Send them to the linux-acpi
1783 * mailinglist for comments. Merging is to be done only
1784 * through acpi-test and the ACPI maintainer.
1785 *
1786 * If the above is too much to ask, don't change the keymap.
1787 * Ask the thinkpad-acpi maintainer to do it, instead.
1788 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001789 static u16 ibm_keycode_map[] __initdata = {
1790 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1791 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1792 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1793 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001794
1795 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001796 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1797 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1798 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001799
1800 /* brightness: firmware always reacts to them, unless
1801 * X.org did some tricks in the radeon BIOS scratch
1802 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001803 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001804 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001805
1806 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001807 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001808
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001809 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1810 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001811
1812 /* Volume: firmware always react to it and reprograms
1813 * the built-in *extra* mixer. Never map it to control
1814 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001815 KEY_RESERVED, /* 0x14: VOLUME UP */
1816 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1817 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001818
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001819 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001820
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001821 /* (assignments unknown, please report if found) */
1822 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1823 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1824 };
1825 static u16 lenovo_keycode_map[] __initdata = {
1826 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1827 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1828 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1829 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001830
1831 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001832 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1833 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1834 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001835
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001836 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001837 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001838
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001839 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001840
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001841 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1842 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001843
1844 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1845 * react to it and reprograms the built-in *extra* mixer.
1846 * Never map it to control another mixer by default.
1847 *
1848 * T60?, T61, R60?, R61: firmware and EC tries to send
1849 * these over the regular keyboard, so these are no-ops,
1850 * but there are still weird bugs re. MUTE, so do not
1851 * change unless you get test reports from all Lenovo
1852 * models. May cause the BIOS to interfere with the
1853 * HDA mixer.
1854 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001855 KEY_RESERVED, /* 0x14: VOLUME UP */
1856 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1857 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001858
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001859 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001860
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001861 /* (assignments unknown, please report if found) */
1862 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1863 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1864 };
1865
1866#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1867#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1868#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1869
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001870 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001871 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001872 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001873
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001874 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1875
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001876 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001877 BUG_ON(tpacpi_inputdev->open != NULL ||
1878 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001879
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001880 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001881 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001882
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001883#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1884 mutex_init(&hotkey_thread_mutex);
1885 mutex_init(&hotkey_thread_data_mutex);
1886#endif
1887
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001888 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001889 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001890
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001891 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001892 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001893
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001894 if (tp_features.hotkey) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001895 hotkey_dev_attributes = create_attr_set(12, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001896 if (!hotkey_dev_attributes)
1897 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001898 res = add_many_to_attr_set(hotkey_dev_attributes,
1899 hotkey_attributes,
1900 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001901 if (res)
1902 return res;
1903
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001904 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001905 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1906 for HKEY interface version 0x100 */
1907 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1908 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001909 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001910 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001911 printk(TPACPI_ERR "please report this to %s\n",
1912 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001913 } else {
1914 /*
1915 * MHKV 0x100 in A31, R40, R40e,
1916 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001917 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001918 tp_features.hotkey_mask = 1;
1919 }
1920 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001921
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001922 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001923 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001924
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001925 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001926 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001927 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001928 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001929 "missing MHKA handler, "
1930 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001931 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001932 /* FN+F12, FN+F4, FN+F3 */
1933 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001934 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001935 }
1936
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001937 /* hotkey_source_mask *must* be zero for
1938 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001939 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001940 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001941 res = hotkey_mask_get();
1942 hotkey_orig_mask = hotkey_mask;
1943 if (!res) {
1944 res = add_many_to_attr_set(
1945 hotkey_dev_attributes,
1946 hotkey_mask_attributes,
1947 ARRAY_SIZE(hotkey_mask_attributes));
1948 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001949 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001950
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001951#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1952 if (tp_features.hotkey_mask) {
1953 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1954 & ~hotkey_all_mask;
1955 } else {
1956 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1957 }
1958
1959 vdbg_printk(TPACPI_DBG_INIT,
1960 "hotkey source mask 0x%08x, polling freq %d\n",
1961 hotkey_source_mask, hotkey_poll_freq);
1962#endif
1963
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001964 /* Not all thinkpads have a hardware radio switch */
1965 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1966 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001967 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001968 "radio switch found; radios are %s\n",
1969 enabled(status, 0));
1970 res = add_to_attr_set(hotkey_dev_attributes,
1971 &dev_attr_hotkey_radio_sw.attr);
1972 }
1973
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001974 if (!res)
1975 res = register_attr_set_with_sysfs(
1976 hotkey_dev_attributes,
1977 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001978 if (res)
1979 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001980
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001981 /* Set up key map */
1982
1983 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1984 GFP_KERNEL);
1985 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001986 printk(TPACPI_ERR
1987 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001988 return -ENOMEM;
1989 }
1990
1991 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1992 dbg_printk(TPACPI_DBG_INIT,
1993 "using Lenovo default hot key map\n");
1994 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1995 TPACPI_HOTKEY_MAP_SIZE);
1996 } else {
1997 dbg_printk(TPACPI_DBG_INIT,
1998 "using IBM default hot key map\n");
1999 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2000 TPACPI_HOTKEY_MAP_SIZE);
2001 }
2002
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002003 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2004 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2005 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002006 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2007 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2008 tpacpi_inputdev->keycode = hotkey_keycode_map;
2009 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002010 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2011 set_bit(hotkey_keycode_map[i],
2012 tpacpi_inputdev->keybit);
2013 } else {
2014 if (i < sizeof(hotkey_reserved_mask)*8)
2015 hotkey_reserved_mask |= 1 << i;
2016 }
2017 }
2018
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002019 if (tp_features.hotkey_wlsw) {
2020 set_bit(EV_SW, tpacpi_inputdev->evbit);
2021 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2022 }
2023
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002024 dbg_printk(TPACPI_DBG_INIT,
2025 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002026 res = hotkey_status_set(1);
2027 if (res)
2028 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002029 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2030 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002031 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002032 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002033 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002034
2035 dbg_printk(TPACPI_DBG_INIT,
2036 "legacy hot key reporting over procfs %s\n",
2037 (hotkey_report_mode < 2) ?
2038 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002039
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002040 tpacpi_inputdev->open = &hotkey_inputdev_open;
2041 tpacpi_inputdev->close = &hotkey_inputdev_close;
2042
2043 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002044 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002045 }
2046
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002047 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002048}
2049
2050static void hotkey_exit(void)
2051{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002052#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2053 hotkey_poll_stop_sync();
2054#endif
2055
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002056 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002057 dbg_printk(TPACPI_DBG_EXIT,
2058 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002059 /* no short-circuit boolean operator below! */
2060 if ((hotkey_mask_set(hotkey_orig_mask) |
2061 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002062 printk(TPACPI_ERR
2063 "failed to restore hot key mask "
2064 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002065 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002066
2067 if (hotkey_dev_attributes) {
2068 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2069 hotkey_dev_attributes = NULL;
2070 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002071}
2072
2073static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2074{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002075 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002076 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002077 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002078 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002079 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002080
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002081 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002082 printk(TPACPI_ERR
2083 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002084 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002085 acpi_bus_generate_netlink_event(
2086 ibm->acpi->device->pnp.device_class,
2087 ibm->acpi->device->dev.bus_id,
2088 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002089 return;
2090 }
2091
2092 while (1) {
2093 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002094 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002095 return;
2096 }
2097
2098 if (hkey == 0) {
2099 /* queue empty */
2100 return;
2101 }
2102
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002103 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002104 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002105 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002106
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002107 switch (hkey >> 12) {
2108 case 1:
2109 /* 0x1000-0x1FFF: key presses */
2110 scancode = hkey & 0xfff;
2111 if (scancode > 0 && scancode < 0x21) {
2112 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002113 if (!(hotkey_source_mask & (1 << scancode))) {
2114 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002115 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002116 } else {
2117 ignore_acpi_ev = 1;
2118 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002119 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002120 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002121 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002122 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002123 case 2:
2124 /* Wakeup reason */
2125 switch (hkey) {
2126 case 0x2304: /* suspend, undock */
2127 case 0x2404: /* hibernation, undock */
2128 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2129 ignore_acpi_ev = 1;
2130 break;
2131 case 0x2305: /* suspend, bay eject */
2132 case 0x2405: /* hibernation, bay eject */
2133 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2134 ignore_acpi_ev = 1;
2135 break;
2136 default:
2137 unk_ev = 1;
2138 }
2139 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2140 printk(TPACPI_INFO
2141 "woke up due to a hot-unplug "
2142 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002143 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002144 }
2145 break;
2146 case 3:
2147 /* bay-related wakeups */
2148 if (hkey == 0x3003) {
2149 hotkey_autosleep_ack = 1;
2150 printk(TPACPI_INFO
2151 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002152 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002153 } else {
2154 unk_ev = 1;
2155 }
2156 break;
2157 case 4:
2158 /* dock-related wakeups */
2159 if (hkey == 0x4003) {
2160 hotkey_autosleep_ack = 1;
2161 printk(TPACPI_INFO
2162 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002163 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002164 } else {
2165 unk_ev = 1;
2166 }
2167 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002168 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002169 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002170 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002171 case 0x5010: /* Lenovo new BIOS: brightness changed */
2172 case 0x5009: /* X61t: swivel up (tablet mode) */
2173 case 0x500a: /* X61t: swivel down (normal mode) */
2174 case 0x500b: /* X61t: tablet pen inserted into bay */
2175 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002176 break;
2177 case 0x5001:
2178 case 0x5002:
2179 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002180 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002181 break;
2182 default:
2183 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002184 }
2185 break;
2186 case 7:
2187 /* 0x7000-0x7FFF: misc */
2188 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2189 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002190 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002191 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002192 break;
2193 }
2194 /* fallthrough to default */
2195 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002196 unk_ev = 1;
2197 }
2198 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002199 printk(TPACPI_NOTICE
2200 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002201 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002202
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002203 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002204 if (!ignore_acpi_ev &&
2205 (send_acpi_ev || hotkey_report_mode < 2)) {
2206 acpi_bus_generate_proc_event(ibm->acpi->device,
2207 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002208 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002209
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002210 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002211 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002212 acpi_bus_generate_netlink_event(
2213 ibm->acpi->device->pnp.device_class,
2214 ibm->acpi->device->dev.bus_id,
2215 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002216 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002217 }
2218}
2219
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002220static void hotkey_suspend(pm_message_t state)
2221{
2222 /* Do these on suspend, we get the events on early resume! */
2223 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2224 hotkey_autosleep_ack = 0;
2225}
2226
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002227static void hotkey_resume(void)
2228{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002229 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002230 printk(TPACPI_ERR
2231 "error while trying to read hot key mask "
2232 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002233 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002234 hotkey_radio_sw_notify_change();
2235 hotkey_wakeup_reason_notify_change();
2236 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002237 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002238}
2239
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002240/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002241static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002243 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 int len = 0;
2245
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002246 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002247 len += sprintf(p + len, "status:\t\tnot supported\n");
2248 return len;
2249 }
2250
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002251 if (mutex_lock_interruptible(&hotkey_mutex))
2252 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002253 res = hotkey_status_get(&status);
2254 if (!res)
2255 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002256 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002257 if (res)
2258 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002261 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002262 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 len += sprintf(p + len,
2264 "commands:\tenable, disable, reset, <mask>\n");
2265 } else {
2266 len += sprintf(p + len, "mask:\t\tnot supported\n");
2267 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2268 }
2269
2270 return len;
2271}
2272
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002273static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002275 int res, status;
2276 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002279 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return -ENODEV;
2281
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002282 if (mutex_lock_interruptible(&hotkey_mutex))
2283 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002284
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002285 status = -1;
2286 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002287
2288 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 while ((cmd = next_cmd(&buf))) {
2290 if (strlencmp(cmd, "enable") == 0) {
2291 status = 1;
2292 } else if (strlencmp(cmd, "disable") == 0) {
2293 status = 0;
2294 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002295 status = hotkey_orig_status;
2296 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2298 /* mask set */
2299 } else if (sscanf(cmd, "%x", &mask) == 1) {
2300 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002301 } else {
2302 res = -EINVAL;
2303 goto errexit;
2304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002306 if (status != -1)
2307 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002309 if (!res && mask != hotkey_mask)
2310 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002312errexit:
2313 mutex_unlock(&hotkey_mutex);
2314 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002317static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002318 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002319 {"", 0},
2320};
2321
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002322static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002323 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002324 .notify = hotkey_notify,
2325 .handle = &hkey_handle,
2326 .type = ACPI_DEVICE_NOTIFY,
2327};
2328
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002329static struct ibm_struct hotkey_driver_data = {
2330 .name = "hotkey",
2331 .read = hotkey_read,
2332 .write = hotkey_write,
2333 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002334 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002335 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002336 .acpi = &ibm_hotkey_acpidriver,
2337};
2338
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002339/*************************************************************************
2340 * Bluetooth subdriver
2341 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002343enum {
2344 /* ACPI GBDC/SBDC bits */
2345 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2346 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2347 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2348};
2349
2350static int bluetooth_get_radiosw(void);
2351static int bluetooth_set_radiosw(int radio_on);
2352
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002353/* sysfs bluetooth enable ---------------------------------------------- */
2354static ssize_t bluetooth_enable_show(struct device *dev,
2355 struct device_attribute *attr,
2356 char *buf)
2357{
2358 int status;
2359
2360 status = bluetooth_get_radiosw();
2361 if (status < 0)
2362 return status;
2363
2364 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2365}
2366
2367static ssize_t bluetooth_enable_store(struct device *dev,
2368 struct device_attribute *attr,
2369 const char *buf, size_t count)
2370{
2371 unsigned long t;
2372 int res;
2373
2374 if (parse_strtoul(buf, 1, &t))
2375 return -EINVAL;
2376
2377 res = bluetooth_set_radiosw(t);
2378
2379 return (res) ? res : count;
2380}
2381
2382static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002383 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002384 bluetooth_enable_show, bluetooth_enable_store);
2385
2386/* --------------------------------------------------------------------- */
2387
2388static struct attribute *bluetooth_attributes[] = {
2389 &dev_attr_bluetooth_enable.attr,
2390 NULL
2391};
2392
2393static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002394 .attrs = bluetooth_attributes,
2395};
2396
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002397static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002399 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002400 int status = 0;
2401
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002402 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2403
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002404 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002405
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002406 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2407 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002408 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002409 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002411 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2412 str_supported(tp_features.bluetooth),
2413 status);
2414
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002415 if (tp_features.bluetooth) {
2416 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2417 /* no bluetooth hardware present in system */
2418 tp_features.bluetooth = 0;
2419 dbg_printk(TPACPI_DBG_INIT,
2420 "bluetooth hardware not installed\n");
2421 } else {
2422 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2423 &bluetooth_attr_group);
2424 if (res)
2425 return res;
2426 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002427 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002428
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002429 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002432static void bluetooth_exit(void)
2433{
2434 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2435 &bluetooth_attr_group);
2436}
2437
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002438static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 int status;
2441
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002442 if (!tp_features.bluetooth)
2443 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002445 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2446 return -EIO;
2447
2448 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2449}
2450
2451static int bluetooth_set_radiosw(int radio_on)
2452{
2453 int status;
2454
2455 if (!tp_features.bluetooth)
2456 return -ENODEV;
2457
2458 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2459 return -EIO;
2460 if (radio_on)
2461 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2462 else
2463 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2464 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2465 return -EIO;
2466
2467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002470/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002471static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
2473 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002474 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002476 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002479 len += sprintf(p + len, "status:\t\t%s\n",
2480 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 len += sprintf(p + len, "commands:\tenable, disable\n");
2482 }
2483
2484 return len;
2485}
2486
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002487static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002491 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002492 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 while ((cmd = next_cmd(&buf))) {
2495 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002496 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002498 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 } else
2500 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 return 0;
2504}
2505
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002506static struct ibm_struct bluetooth_driver_data = {
2507 .name = "bluetooth",
2508 .read = bluetooth_read,
2509 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002510 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002511};
2512
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002513/*************************************************************************
2514 * Wan subdriver
2515 */
2516
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002517enum {
2518 /* ACPI GWAN/SWAN bits */
2519 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2520 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2521 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2522};
2523
2524static int wan_get_radiosw(void);
2525static int wan_set_radiosw(int radio_on);
2526
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002527/* sysfs wan enable ---------------------------------------------------- */
2528static ssize_t wan_enable_show(struct device *dev,
2529 struct device_attribute *attr,
2530 char *buf)
2531{
2532 int status;
2533
2534 status = wan_get_radiosw();
2535 if (status < 0)
2536 return status;
2537
2538 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2539}
2540
2541static ssize_t wan_enable_store(struct device *dev,
2542 struct device_attribute *attr,
2543 const char *buf, size_t count)
2544{
2545 unsigned long t;
2546 int res;
2547
2548 if (parse_strtoul(buf, 1, &t))
2549 return -EINVAL;
2550
2551 res = wan_set_radiosw(t);
2552
2553 return (res) ? res : count;
2554}
2555
2556static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002557 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002558 wan_enable_show, wan_enable_store);
2559
2560/* --------------------------------------------------------------------- */
2561
2562static struct attribute *wan_attributes[] = {
2563 &dev_attr_wan_enable.attr,
2564 NULL
2565};
2566
2567static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002568 .attrs = wan_attributes,
2569};
2570
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002571static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002572{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002573 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002574 int status = 0;
2575
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002576 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2577
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002578 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002579
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002580 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002581 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002582
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002583 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2584 str_supported(tp_features.wan),
2585 status);
2586
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002587 if (tp_features.wan) {
2588 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2589 /* no wan hardware present in system */
2590 tp_features.wan = 0;
2591 dbg_printk(TPACPI_DBG_INIT,
2592 "wan hardware not installed\n");
2593 } else {
2594 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2595 &wan_attr_group);
2596 if (res)
2597 return res;
2598 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002599 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002600
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002601 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002602}
2603
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002604static void wan_exit(void)
2605{
2606 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2607 &wan_attr_group);
2608}
2609
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002610static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002611{
2612 int status;
2613
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002614 if (!tp_features.wan)
2615 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002616
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002617 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2618 return -EIO;
2619
2620 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2621}
2622
2623static int wan_set_radiosw(int radio_on)
2624{
2625 int status;
2626
2627 if (!tp_features.wan)
2628 return -ENODEV;
2629
2630 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2631 return -EIO;
2632 if (radio_on)
2633 status |= TP_ACPI_WANCARD_RADIOSSW;
2634 else
2635 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2636 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2637 return -EIO;
2638
2639 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002640}
2641
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002642/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002643static int wan_read(char *p)
2644{
2645 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002646 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002647
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002648 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002649 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002650 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002651 len += sprintf(p + len, "status:\t\t%s\n",
2652 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002653 len += sprintf(p + len, "commands:\tenable, disable\n");
2654 }
2655
2656 return len;
2657}
2658
2659static int wan_write(char *buf)
2660{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002661 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002662
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002663 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002664 return -ENODEV;
2665
2666 while ((cmd = next_cmd(&buf))) {
2667 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002668 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002669 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002670 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002671 } else
2672 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002673 }
2674
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002675 return 0;
2676}
2677
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002678static struct ibm_struct wan_driver_data = {
2679 .name = "wan",
2680 .read = wan_read,
2681 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002682 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002683 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002684};
2685
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002686/*************************************************************************
2687 * Video subdriver
2688 */
2689
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002690#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2691
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002692enum video_access_mode {
2693 TPACPI_VIDEO_NONE = 0,
2694 TPACPI_VIDEO_570, /* 570 */
2695 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2696 TPACPI_VIDEO_NEW, /* all others */
2697};
2698
2699enum { /* video status flags, based on VIDEO_570 */
2700 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2701 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2702 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2703};
2704
2705enum { /* TPACPI_VIDEO_570 constants */
2706 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2707 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2708 * video_status_flags */
2709 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2710 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2711};
2712
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002713static enum video_access_mode video_supported;
2714static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002715
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002716static int video_autosw_get(void);
2717static int video_autosw_set(int enable);
2718
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002719TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002720
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002721static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002723 int ivga;
2724
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002725 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2726
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002727 TPACPI_ACPIHANDLE_INIT(vid);
2728 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002729
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002730 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2731 /* G41, assume IVGA doesn't change */
2732 vid_handle = vid2_handle;
2733
2734 if (!vid_handle)
2735 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002736 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002737 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2738 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002739 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002740 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2741 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002742 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002743 else
2744 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002745 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002747 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2748 str_supported(video_supported != TPACPI_VIDEO_NONE),
2749 video_supported);
2750
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002751 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
2753
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002754static void video_exit(void)
2755{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002756 dbg_printk(TPACPI_DBG_EXIT,
2757 "restoring original video autoswitch mode\n");
2758 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002759 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002760 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002761}
2762
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002763static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 int status = 0;
2766 int i;
2767
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002768 switch (video_supported) {
2769 case TPACPI_VIDEO_570:
2770 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2771 TP_ACPI_VIDEO_570_PHSCMD))
2772 return -EIO;
2773 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2774 break;
2775 case TPACPI_VIDEO_770:
2776 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2777 return -EIO;
2778 if (i)
2779 status |= TP_ACPI_VIDEO_S_LCD;
2780 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2781 return -EIO;
2782 if (i)
2783 status |= TP_ACPI_VIDEO_S_CRT;
2784 break;
2785 case TPACPI_VIDEO_NEW:
2786 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2787 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2788 return -EIO;
2789 if (i)
2790 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002792 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2793 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2794 return -EIO;
2795 if (i)
2796 status |= TP_ACPI_VIDEO_S_LCD;
2797 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2798 return -EIO;
2799 if (i)
2800 status |= TP_ACPI_VIDEO_S_DVI;
2801 break;
2802 default:
2803 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 return status;
2807}
2808
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002809static int video_outputsw_set(int status)
2810{
2811 int autosw;
2812 int res = 0;
2813
2814 switch (video_supported) {
2815 case TPACPI_VIDEO_570:
2816 res = acpi_evalf(NULL, NULL,
2817 "\\_SB.PHS2", "vdd",
2818 TP_ACPI_VIDEO_570_PHS2CMD,
2819 status | TP_ACPI_VIDEO_570_PHS2SET);
2820 break;
2821 case TPACPI_VIDEO_770:
2822 autosw = video_autosw_get();
2823 if (autosw < 0)
2824 return autosw;
2825
2826 res = video_autosw_set(1);
2827 if (res)
2828 return res;
2829 res = acpi_evalf(vid_handle, NULL,
2830 "ASWT", "vdd", status * 0x100, 0);
2831 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002832 printk(TPACPI_ERR
2833 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002834 return -EIO;
2835 }
2836 break;
2837 case TPACPI_VIDEO_NEW:
2838 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002839 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002840 break;
2841 default:
2842 return -ENOSYS;
2843 }
2844
2845 return (res)? 0 : -EIO;
2846}
2847
2848static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002850 int autosw = 0;
2851
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002852 switch (video_supported) {
2853 case TPACPI_VIDEO_570:
2854 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2855 return -EIO;
2856 break;
2857 case TPACPI_VIDEO_770:
2858 case TPACPI_VIDEO_NEW:
2859 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2860 return -EIO;
2861 break;
2862 default:
2863 return -ENOSYS;
2864 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002865
2866 return autosw & 1;
2867}
2868
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002869static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002870{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002871 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002872 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002873 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002874}
2875
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002876static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002877{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002878 int autosw = video_autosw_get();
2879 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002880
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002881 if (autosw < 0)
2882 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002883
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002884 switch (video_supported) {
2885 case TPACPI_VIDEO_570:
2886 res = video_autosw_set(1);
2887 if (res)
2888 return res;
2889 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2890 break;
2891 case TPACPI_VIDEO_770:
2892 case TPACPI_VIDEO_NEW:
2893 res = video_autosw_set(1);
2894 if (res)
2895 return res;
2896 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2897 break;
2898 default:
2899 return -ENOSYS;
2900 }
2901 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002902 printk(TPACPI_ERR
2903 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002904 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002905 }
2906
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002907 return (res)? 0 : -EIO;
2908}
2909
2910static int video_expand_toggle(void)
2911{
2912 switch (video_supported) {
2913 case TPACPI_VIDEO_570:
2914 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2915 0 : -EIO;
2916 case TPACPI_VIDEO_770:
2917 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2918 0 : -EIO;
2919 case TPACPI_VIDEO_NEW:
2920 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2921 0 : -EIO;
2922 default:
2923 return -ENOSYS;
2924 }
2925 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002926}
2927
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002928static int video_read(char *p)
2929{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002930 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002931 int len = 0;
2932
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002933 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002934 len += sprintf(p + len, "status:\t\tnot supported\n");
2935 return len;
2936 }
2937
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002938 status = video_outputsw_get();
2939 if (status < 0)
2940 return status;
2941
2942 autosw = video_autosw_get();
2943 if (autosw < 0)
2944 return autosw;
2945
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002946 len += sprintf(p + len, "status:\t\tsupported\n");
2947 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2948 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002949 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002950 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2951 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2952 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2953 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002954 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002955 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2956 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2957 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2958
2959 return len;
2960}
2961
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002962static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
2964 char *cmd;
2965 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002966 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002968 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002969 return -ENODEV;
2970
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002971 enable = 0;
2972 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 while ((cmd = next_cmd(&buf))) {
2975 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002976 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002978 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002980 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002982 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002983 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002984 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002985 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002986 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002987 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002988 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002990 res = video_autosw_set(1);
2991 if (res)
2992 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002994 res = video_autosw_set(0);
2995 if (res)
2996 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002998 res = video_outputsw_cycle();
2999 if (res)
3000 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003002 res = video_expand_toggle();
3003 if (res)
3004 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 } else
3006 return -EINVAL;
3007 }
3008
3009 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003010 status = video_outputsw_get();
3011 if (status < 0)
3012 return status;
3013 res = video_outputsw_set((status & ~disable) | enable);
3014 if (res)
3015 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
3017
3018 return 0;
3019}
3020
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003021static struct ibm_struct video_driver_data = {
3022 .name = "video",
3023 .read = video_read,
3024 .write = video_write,
3025 .exit = video_exit,
3026};
3027
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003028#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3029
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003030/*************************************************************************
3031 * Light (thinklight) subdriver
3032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003034TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3035TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003036
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003037static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003039 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3040
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003041 TPACPI_ACPIHANDLE_INIT(ledb);
3042 TPACPI_ACPIHANDLE_INIT(lght);
3043 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003044
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003045 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003046 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003047
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003048 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003049 /* light status not supported on
3050 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003051 tp_features.light_status =
3052 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003054 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003055 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003056
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003057 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003060static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 int len = 0;
3063 int status = 0;
3064
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003065 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003066 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003067 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003068 len += sprintf(p + len, "status:\t\tunknown\n");
3069 len += sprintf(p + len, "commands:\ton, off\n");
3070 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3072 return -EIO;
3073 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003074 len += sprintf(p + len, "commands:\ton, off\n");
3075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 return len;
3078}
3079
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003080static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 int cmos_cmd, lght_cmd;
3083 char *cmd;
3084 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003085
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003086 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003087 return -ENODEV;
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 while ((cmd = next_cmd(&buf))) {
3090 if (strlencmp(cmd, "on") == 0) {
3091 cmos_cmd = 0x0c;
3092 lght_cmd = 1;
3093 } else if (strlencmp(cmd, "off") == 0) {
3094 cmos_cmd = 0x0d;
3095 lght_cmd = 0;
3096 } else
3097 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003100 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3101 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (!success)
3103 return -EIO;
3104 }
3105
3106 return 0;
3107}
3108
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003109static struct ibm_struct light_driver_data = {
3110 .name = "light",
3111 .read = light_read,
3112 .write = light_write,
3113};
3114
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003115/*************************************************************************
3116 * Dock subdriver
3117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003119#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003120
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003121static void dock_notify(struct ibm_struct *ibm, u32 event);
3122static int dock_read(char *p);
3123static int dock_write(char *buf);
3124
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003125TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003126 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3127 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3128 "\\_SB.PCI.ISA.SLCE", /* 570 */
3129 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3130
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003131/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003132TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003133
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003134static const struct acpi_device_id ibm_pci_device_ids[] = {
3135 {PCI_ROOT_HID_STRING, 0},
3136 {"", 0},
3137};
3138
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003139static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3140 {
3141 .notify = dock_notify,
3142 .handle = &dock_handle,
3143 .type = ACPI_SYSTEM_NOTIFY,
3144 },
3145 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003146 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3147 * We just use it to get notifications of dock hotplug
3148 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003149 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003150 .notify = dock_notify,
3151 .handle = &pci_handle,
3152 .type = ACPI_SYSTEM_NOTIFY,
3153 },
3154};
3155
3156static struct ibm_struct dock_driver_data[2] = {
3157 {
3158 .name = "dock",
3159 .read = dock_read,
3160 .write = dock_write,
3161 .acpi = &ibm_dock_acpidriver[0],
3162 },
3163 {
3164 .name = "dock",
3165 .acpi = &ibm_dock_acpidriver[1],
3166 },
3167};
3168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169#define dock_docked() (_sta(dock_handle) & 1)
3170
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003171static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003172{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003173 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3174
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003175 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003176
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003177 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3178 str_supported(dock_handle != NULL));
3179
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003180 return (dock_handle)? 0 : 1;
3181}
3182
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003183static int __init dock_init2(struct ibm_init_struct *iibm)
3184{
3185 int dock2_needed;
3186
3187 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3188
3189 if (dock_driver_data[0].flags.acpi_driver_registered &&
3190 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003191 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003192 dock2_needed = (pci_handle != NULL);
3193 vdbg_printk(TPACPI_DBG_INIT,
3194 "dock PCI handler for the TP 570 is %s\n",
3195 str_supported(dock2_needed));
3196 } else {
3197 vdbg_printk(TPACPI_DBG_INIT,
3198 "dock subdriver part 2 not required\n");
3199 dock2_needed = 0;
3200 }
3201
3202 return (dock2_needed)? 0 : 1;
3203}
3204
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003205static void dock_notify(struct ibm_struct *ibm, u32 event)
3206{
3207 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003208 int pci = ibm->acpi->hid && ibm->acpi->device &&
3209 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003210 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003211
3212 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003213 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003214 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003215 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003216 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003217 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003218 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003219 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003220 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003221 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003222 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003223 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003224 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003225 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003226 }
Len Brown14e04fb2007-08-23 15:20:26 -04003227 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003228 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3229 ibm->acpi->device->dev.bus_id,
3230 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003231}
3232
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003233static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
3235 int len = 0;
3236 int docked = dock_docked();
3237
3238 if (!dock_handle)
3239 len += sprintf(p + len, "status:\t\tnot supported\n");
3240 else if (!docked)
3241 len += sprintf(p + len, "status:\t\tundocked\n");
3242 else {
3243 len += sprintf(p + len, "status:\t\tdocked\n");
3244 len += sprintf(p + len, "commands:\tdock, undock\n");
3245 }
3246
3247 return len;
3248}
3249
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003250static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
3252 char *cmd;
3253
3254 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003255 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257 while ((cmd = next_cmd(&buf))) {
3258 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003259 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3260 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return -EIO;
3262 } else if (strlencmp(cmd, "dock") == 0) {
3263 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3264 return -EIO;
3265 } else
3266 return -EINVAL;
3267 }
3268
3269 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003272#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003274/*************************************************************************
3275 * Bay subdriver
3276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003278#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003279
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003280TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003281 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3282 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3283 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3284 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003285TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003286 "_EJ0", /* all others */
3287 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003288TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003289 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3290 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003291TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003292 "_EJ0", /* 770x */
3293 ); /* all others */
3294
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003295static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003297 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3298
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003299 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003300 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003301 TPACPI_ACPIHANDLE_INIT(bay_ej);
3302 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003303 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003304 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003305
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003306 tp_features.bay_status = bay_handle &&
3307 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3308 tp_features.bay_status2 = bay2_handle &&
3309 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003310
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003311 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3312 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3313 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3314 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003316 vdbg_printk(TPACPI_DBG_INIT,
3317 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003318 str_supported(tp_features.bay_status),
3319 str_supported(tp_features.bay_eject),
3320 str_supported(tp_features.bay_status2),
3321 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003322
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003323 return (tp_features.bay_status || tp_features.bay_eject ||
3324 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325}
3326
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003327static void bay_notify(struct ibm_struct *ibm, u32 event)
3328{
Len Brown14e04fb2007-08-23 15:20:26 -04003329 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003330 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3331 ibm->acpi->device->dev.bus_id,
3332 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003333}
3334
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003335#define bay_occupied(b) (_sta(b##_handle) & 1)
3336
3337static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003340 int occupied = bay_occupied(bay);
3341 int occupied2 = bay_occupied(bay2);
3342 int eject, eject2;
3343
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003344 len += sprintf(p + len, "status:\t\t%s\n",
3345 tp_features.bay_status ?
3346 (occupied ? "occupied" : "unoccupied") :
3347 "not supported");
3348 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003349 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3350 "occupied" : "unoccupied");
3351
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003352 eject = tp_features.bay_eject && occupied;
3353 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003354
3355 if (eject && eject2)
3356 len += sprintf(p + len, "commands:\teject, eject2\n");
3357 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003359 else if (eject2)
3360 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 return len;
3363}
3364
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003365static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
3367 char *cmd;
3368
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003369 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003370 return -ENODEV;
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003373 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003374 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3375 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003376 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003377 strlencmp(cmd, "eject2") == 0) {
3378 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return -EIO;
3380 } else
3381 return -EINVAL;
3382 }
3383
3384 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003385}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003386
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003387static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3388 .notify = bay_notify,
3389 .handle = &bay_handle,
3390 .type = ACPI_SYSTEM_NOTIFY,
3391};
3392
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003393static struct ibm_struct bay_driver_data = {
3394 .name = "bay",
3395 .read = bay_read,
3396 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003397 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003398};
3399
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003400#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003402/*************************************************************************
3403 * CMOS subdriver
3404 */
3405
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003406/* sysfs cmos_command -------------------------------------------------- */
3407static ssize_t cmos_command_store(struct device *dev,
3408 struct device_attribute *attr,
3409 const char *buf, size_t count)
3410{
3411 unsigned long cmos_cmd;
3412 int res;
3413
3414 if (parse_strtoul(buf, 21, &cmos_cmd))
3415 return -EINVAL;
3416
3417 res = issue_thinkpad_cmos_command(cmos_cmd);
3418 return (res)? res : count;
3419}
3420
3421static struct device_attribute dev_attr_cmos_command =
3422 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3423
3424/* --------------------------------------------------------------------- */
3425
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003426static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003427{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003428 int res;
3429
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003430 vdbg_printk(TPACPI_DBG_INIT,
3431 "initializing cmos commands subdriver\n");
3432
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003433 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003434
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003435 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3436 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003437
3438 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3439 if (res)
3440 return res;
3441
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003442 return (cmos_handle)? 0 : 1;
3443}
3444
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003445static void cmos_exit(void)
3446{
3447 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3448}
3449
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003450static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
3452 int len = 0;
3453
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003454 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3455 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (!cmos_handle)
3457 len += sprintf(p + len, "status:\t\tnot supported\n");
3458 else {
3459 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003460 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
3462
3463 return len;
3464}
3465
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003466static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
3468 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003469 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003472 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3473 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 /* cmos_cmd set */
3475 } else
3476 return -EINVAL;
3477
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003478 res = issue_thinkpad_cmos_command(cmos_cmd);
3479 if (res)
3480 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
3482
3483 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003484}
3485
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003486static struct ibm_struct cmos_driver_data = {
3487 .name = "cmos",
3488 .read = cmos_read,
3489 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003490 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003491};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003492
3493/*************************************************************************
3494 * LED subdriver
3495 */
3496
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003497enum led_access_mode {
3498 TPACPI_LED_NONE = 0,
3499 TPACPI_LED_570, /* 570 */
3500 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3501 TPACPI_LED_NEW, /* all others */
3502};
3503
3504enum { /* For TPACPI_LED_OLD */
3505 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3506 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3507 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3508};
3509
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003510static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003511
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003512TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003513 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3514 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003515 "LED", /* all others */
3516 ); /* R30, R31 */
3517
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003518static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003519{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003520 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3521
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003522 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003523
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003524 if (!led_handle)
3525 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003526 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003527 else if (strlencmp(led_path, "SLED") == 0)
3528 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003529 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003530 else if (strlencmp(led_path, "SYSL") == 0)
3531 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003532 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003533 else
3534 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003535 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003536
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003537 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3538 str_supported(led_supported), led_supported);
3539
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003540 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003541}
3542
3543#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3544
3545static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 int len = 0;
3548
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003549 if (!led_supported) {
3550 len += sprintf(p + len, "status:\t\tnot supported\n");
3551 return len;
3552 }
3553 len += sprintf(p + len, "status:\t\tsupported\n");
3554
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003555 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003556 /* 570 */
3557 int i, status;
3558 for (i = 0; i < 8; i++) {
3559 if (!acpi_evalf(ec_handle,
3560 &status, "GLED", "dd", 1 << i))
3561 return -EIO;
3562 len += sprintf(p + len, "%d:\t\t%s\n",
3563 i, led_status(status));
3564 }
3565 }
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003568 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
3570 return len;
3571}
3572
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003573/* off, on, blink */
3574static const int led_sled_arg1[] = { 0, 1, 3 };
3575static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3576static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3577static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3578
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003579static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
3581 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003582 int led, ind, ret;
3583
3584 if (!led_supported)
3585 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003588 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return -EINVAL;
3590
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003591 if (strstr(cmd, "off")) {
3592 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003594 ind = 1;
3595 } else if (strstr(cmd, "blink")) {
3596 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 } else
3598 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003599
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003600 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003601 /* 570 */
3602 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003604 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003606 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003607 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3608 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003609 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003610 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003611 ret = ec_write(TPACPI_LED_EC_HLBL,
3612 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003613 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003614 ret = ec_write(TPACPI_LED_EC_HLCL,
3615 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003616 if (ret < 0)
3617 return ret;
3618 } else {
3619 /* all others */
3620 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3621 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
3625
3626 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003627}
3628
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003629static struct ibm_struct led_driver_data = {
3630 .name = "led",
3631 .read = led_read,
3632 .write = led_write,
3633};
3634
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003635/*************************************************************************
3636 * Beep subdriver
3637 */
3638
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003639TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003640
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003641static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003642{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003643 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3644
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003645 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003646
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003647 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3648 str_supported(beep_handle != NULL));
3649
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003650 return (beep_handle)? 0 : 1;
3651}
3652
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003653static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
3655 int len = 0;
3656
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003657 if (!beep_handle)
3658 len += sprintf(p + len, "status:\t\tnot supported\n");
3659 else {
3660 len += sprintf(p + len, "status:\t\tsupported\n");
3661 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664 return len;
3665}
3666
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003667static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
3669 char *cmd;
3670 int beep_cmd;
3671
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003672 if (!beep_handle)
3673 return -ENODEV;
3674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003676 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3677 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 /* beep_cmd set */
3679 } else
3680 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003681 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 return -EIO;
3683 }
3684
3685 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003686}
3687
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003688static struct ibm_struct beep_driver_data = {
3689 .name = "beep",
3690 .read = beep_read,
3691 .write = beep_write,
3692};
3693
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003694/*************************************************************************
3695 * Thermal subdriver
3696 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003697
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003698enum thermal_access_mode {
3699 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3700 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3701 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3702 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3703 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3704};
3705
3706enum { /* TPACPI_THERMAL_TPEC_* */
3707 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3708 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3709 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3710};
3711
3712#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3713struct ibm_thermal_sensors_struct {
3714 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3715};
3716
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003717static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003718
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003719/* idx is zero-based */
3720static int thermal_get_sensor(int idx, s32 *value)
3721{
3722 int t;
3723 s8 tmp;
3724 char tmpi[5];
3725
3726 t = TP_EC_THERMAL_TMP0;
3727
3728 switch (thermal_read_mode) {
3729#if TPACPI_MAX_THERMAL_SENSORS >= 16
3730 case TPACPI_THERMAL_TPEC_16:
3731 if (idx >= 8 && idx <= 15) {
3732 t = TP_EC_THERMAL_TMP8;
3733 idx -= 8;
3734 }
3735 /* fallthrough */
3736#endif
3737 case TPACPI_THERMAL_TPEC_8:
3738 if (idx <= 7) {
3739 if (!acpi_ec_read(t + idx, &tmp))
3740 return -EIO;
3741 *value = tmp * 1000;
3742 return 0;
3743 }
3744 break;
3745
3746 case TPACPI_THERMAL_ACPI_UPDT:
3747 if (idx <= 7) {
3748 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3749 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3750 return -EIO;
3751 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3752 return -EIO;
3753 *value = (t - 2732) * 100;
3754 return 0;
3755 }
3756 break;
3757
3758 case TPACPI_THERMAL_ACPI_TMP07:
3759 if (idx <= 7) {
3760 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3761 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3762 return -EIO;
3763 if (t > 127 || t < -127)
3764 t = TP_EC_THERMAL_TMP_NA;
3765 *value = t * 1000;
3766 return 0;
3767 }
3768 break;
3769
3770 case TPACPI_THERMAL_NONE:
3771 default:
3772 return -ENOSYS;
3773 }
3774
3775 return -EINVAL;
3776}
3777
3778static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3779{
3780 int res, i;
3781 int n;
3782
3783 n = 8;
3784 i = 0;
3785
3786 if (!s)
3787 return -EINVAL;
3788
3789 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3790 n = 16;
3791
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003792 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003793 res = thermal_get_sensor(i, &s->temp[i]);
3794 if (res)
3795 return res;
3796 }
3797
3798 return n;
3799}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003800
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003801/* sysfs temp##_input -------------------------------------------------- */
3802
3803static ssize_t thermal_temp_input_show(struct device *dev,
3804 struct device_attribute *attr,
3805 char *buf)
3806{
3807 struct sensor_device_attribute *sensor_attr =
3808 to_sensor_dev_attr(attr);
3809 int idx = sensor_attr->index;
3810 s32 value;
3811 int res;
3812
3813 res = thermal_get_sensor(idx, &value);
3814 if (res)
3815 return res;
3816 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3817 return -ENXIO;
3818
3819 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3820}
3821
3822#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003823 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3824 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003825
3826static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3827 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3828 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3829 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3830 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3831 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3832 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3833 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3834 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3835 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3836 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3837 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3838 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3839 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3840 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3841 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3842 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3843};
3844
3845#define THERMAL_ATTRS(X) \
3846 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3847
3848static struct attribute *thermal_temp_input_attr[] = {
3849 THERMAL_ATTRS(8),
3850 THERMAL_ATTRS(9),
3851 THERMAL_ATTRS(10),
3852 THERMAL_ATTRS(11),
3853 THERMAL_ATTRS(12),
3854 THERMAL_ATTRS(13),
3855 THERMAL_ATTRS(14),
3856 THERMAL_ATTRS(15),
3857 THERMAL_ATTRS(0),
3858 THERMAL_ATTRS(1),
3859 THERMAL_ATTRS(2),
3860 THERMAL_ATTRS(3),
3861 THERMAL_ATTRS(4),
3862 THERMAL_ATTRS(5),
3863 THERMAL_ATTRS(6),
3864 THERMAL_ATTRS(7),
3865 NULL
3866};
3867
3868static const struct attribute_group thermal_temp_input16_group = {
3869 .attrs = thermal_temp_input_attr
3870};
3871
3872static const struct attribute_group thermal_temp_input8_group = {
3873 .attrs = &thermal_temp_input_attr[8]
3874};
3875
3876#undef THERMAL_SENSOR_ATTR_TEMP
3877#undef THERMAL_ATTRS
3878
3879/* --------------------------------------------------------------------- */
3880
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003881static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003882{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003883 u8 t, ta1, ta2;
3884 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003885 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003886 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003887
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003888 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3889
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003890 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003891
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003892 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003893 /*
3894 * Direct EC access mode: sensors at registers
3895 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3896 * non-implemented, thermal sensors return 0x80 when
3897 * not available
3898 */
3899
3900 ta1 = ta2 = 0;
3901 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003902 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003903 ta1 |= t;
3904 } else {
3905 ta1 = 0;
3906 break;
3907 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003908 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003909 ta2 |= t;
3910 } else {
3911 ta1 = 0;
3912 break;
3913 }
3914 }
3915 if (ta1 == 0) {
3916 /* This is sheer paranoia, but we handle it anyway */
3917 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003918 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003919 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003920 "falling back to ACPI TMPx access "
3921 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003922 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003923 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003924 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003925 "ThinkPad ACPI EC access misbehaving, "
3926 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003927 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003928 }
3929 } else {
3930 thermal_read_mode =
3931 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003932 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003933 }
3934 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003935 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3936 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003937 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003938 } else {
3939 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003940 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003941 }
3942 } else {
3943 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003944 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003945 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003946
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003947 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3948 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3949 thermal_read_mode);
3950
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003951 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003952 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003953 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003954 &thermal_temp_input16_group);
3955 if (res)
3956 return res;
3957 break;
3958 case TPACPI_THERMAL_TPEC_8:
3959 case TPACPI_THERMAL_ACPI_TMP07:
3960 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003961 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003962 &thermal_temp_input8_group);
3963 if (res)
3964 return res;
3965 break;
3966 case TPACPI_THERMAL_NONE:
3967 default:
3968 return 1;
3969 }
3970
3971 return 0;
3972}
3973
3974static void thermal_exit(void)
3975{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003976 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003977 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003978 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003979 &thermal_temp_input16_group);
3980 break;
3981 case TPACPI_THERMAL_TPEC_8:
3982 case TPACPI_THERMAL_ACPI_TMP07:
3983 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003984 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003985 &thermal_temp_input16_group);
3986 break;
3987 case TPACPI_THERMAL_NONE:
3988 default:
3989 break;
3990 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003991}
3992
3993static int thermal_read(char *p)
3994{
3995 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003996 int n, i;
3997 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003998
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003999 n = thermal_get_sensors(&t);
4000 if (unlikely(n < 0))
4001 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004002
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004003 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004004
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004005 if (n > 0) {
4006 for (i = 0; i < (n - 1); i++)
4007 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4008 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4009 } else
4010 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004011
4012 return len;
4013}
4014
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004015static struct ibm_struct thermal_driver_data = {
4016 .name = "thermal",
4017 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004018 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004019};
4020
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004021/*************************************************************************
4022 * EC Dump subdriver
4023 */
4024
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004025static u8 ecdump_regs[256];
4026
4027static int ecdump_read(char *p)
4028{
4029 int len = 0;
4030 int i, j;
4031 u8 v;
4032
4033 len += sprintf(p + len, "EC "
4034 " +00 +01 +02 +03 +04 +05 +06 +07"
4035 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4036 for (i = 0; i < 256; i += 16) {
4037 len += sprintf(p + len, "EC 0x%02x:", i);
4038 for (j = 0; j < 16; j++) {
4039 if (!acpi_ec_read(i + j, &v))
4040 break;
4041 if (v != ecdump_regs[i + j])
4042 len += sprintf(p + len, " *%02x", v);
4043 else
4044 len += sprintf(p + len, " %02x", v);
4045 ecdump_regs[i + j] = v;
4046 }
4047 len += sprintf(p + len, "\n");
4048 if (j != 16)
4049 break;
4050 }
4051
4052 /* These are way too dangerous to advertise openly... */
4053#if 0
4054 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4055 " (<offset> is 00-ff, <value> is 00-ff)\n");
4056 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4057 " (<offset> is 00-ff, <value> is 0-255)\n");
4058#endif
4059 return len;
4060}
4061
4062static int ecdump_write(char *buf)
4063{
4064 char *cmd;
4065 int i, v;
4066
4067 while ((cmd = next_cmd(&buf))) {
4068 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4069 /* i and v set */
4070 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4071 /* i and v set */
4072 } else
4073 return -EINVAL;
4074 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4075 if (!acpi_ec_write(i, v))
4076 return -EIO;
4077 } else
4078 return -EINVAL;
4079 }
4080
4081 return 0;
4082}
4083
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004084static struct ibm_struct ecdump_driver_data = {
4085 .name = "ecdump",
4086 .read = ecdump_read,
4087 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004088 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004089};
4090
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004091/*************************************************************************
4092 * Backlight/brightness subdriver
4093 */
Holger Macht8acb0252006-10-20 14:30:28 -07004094
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004095#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4096
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004097static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004098static int brightness_offset = 0x31;
4099static int brightness_mode;
4100static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4101
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004102static struct mutex brightness_mutex;
4103
4104/*
4105 * ThinkPads can read brightness from two places: EC 0x31, or
4106 * CMOS NVRAM byte 0x5E, bits 0-3.
4107 */
4108static int brightness_get(struct backlight_device *bd)
4109{
4110 u8 lec = 0, lcmos = 0, level = 0;
4111
4112 if (brightness_mode & 1) {
4113 if (!acpi_ec_read(brightness_offset, &lec))
4114 return -EIO;
4115 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4116 level = lec;
4117 };
4118 if (brightness_mode & 2) {
4119 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4120 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4121 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4122 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4123 level = lcmos;
4124 }
4125
4126 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004127 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004128 "CMOS NVRAM (%u) and EC (%u) do not agree "
4129 "on display brightness level\n",
4130 (unsigned int) lcmos,
4131 (unsigned int) lec);
4132 return -EIO;
4133 }
4134
4135 return level;
4136}
4137
4138/* May return EINTR which can always be mapped to ERESTARTSYS */
4139static int brightness_set(int value)
4140{
4141 int cmos_cmd, inc, i, res;
4142 int current_value;
4143
4144 if (value > ((tp_features.bright_16levels)? 15 : 7))
4145 return -EINVAL;
4146
4147 res = mutex_lock_interruptible(&brightness_mutex);
4148 if (res < 0)
4149 return res;
4150
4151 current_value = brightness_get(NULL);
4152 if (current_value < 0) {
4153 res = current_value;
4154 goto errout;
4155 }
4156
4157 cmos_cmd = value > current_value ?
4158 TP_CMOS_BRIGHTNESS_UP :
4159 TP_CMOS_BRIGHTNESS_DOWN;
4160 inc = (value > current_value)? 1 : -1;
4161
4162 res = 0;
4163 for (i = current_value; i != value; i += inc) {
4164 if ((brightness_mode & 2) &&
4165 issue_thinkpad_cmos_command(cmos_cmd)) {
4166 res = -EIO;
4167 goto errout;
4168 }
4169 if ((brightness_mode & 1) &&
4170 !acpi_ec_write(brightness_offset, i + inc)) {
4171 res = -EIO;
4172 goto errout;;
4173 }
4174 }
4175
4176errout:
4177 mutex_unlock(&brightness_mutex);
4178 return res;
4179}
4180
4181/* sysfs backlight class ----------------------------------------------- */
4182
4183static int brightness_update_status(struct backlight_device *bd)
4184{
4185 /* it is the backlight class's job (caller) to handle
4186 * EINTR and other errors properly */
4187 return brightness_set(
4188 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4189 bd->props.power == FB_BLANK_UNBLANK) ?
4190 bd->props.brightness : 0);
4191}
Holger Macht8acb0252006-10-20 14:30:28 -07004192
Richard Purdie599a52d2007-02-10 23:07:48 +00004193static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004194 .get_brightness = brightness_get,
4195 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004196};
4197
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004198/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004199
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004200static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4201{
4202 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4203 union acpi_object *obj;
4204 int rc;
4205
4206 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4207 obj = (union acpi_object *)buffer.pointer;
4208 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004209 printk(TPACPI_ERR "Unknown BCLL data, "
4210 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004211 rc = 0;
4212 } else {
4213 rc = obj->package.count;
4214 }
4215 } else {
4216 return 0;
4217 }
4218
4219 kfree(buffer.pointer);
4220 return rc;
4221}
4222
4223static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4224 void *context, void **rv)
4225{
4226 char name[ACPI_PATH_SEGMENT_LENGTH];
4227 struct acpi_buffer buffer = { sizeof(name), &name };
4228
4229 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4230 !strncmp("BCLL", name, sizeof(name) - 1)) {
4231 if (tpacpi_query_bcll_levels(handle) == 16) {
4232 *rv = handle;
4233 return AE_CTRL_TERMINATE;
4234 } else {
4235 return AE_OK;
4236 }
4237 } else {
4238 return AE_OK;
4239 }
4240}
4241
4242static int __init brightness_check_levels(void)
4243{
4244 int status;
4245 void *found_node = NULL;
4246
4247 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004248 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004249 }
4250 if (!vid_handle)
4251 return 0;
4252
4253 /* Search for a BCLL package with 16 levels */
4254 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004255 brightness_find_bcll, NULL,
4256 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004257
4258 return (ACPI_SUCCESS(status) && found_node != NULL);
4259}
4260
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004261static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4262 void *context, void **rv)
4263{
4264 char name[ACPI_PATH_SEGMENT_LENGTH];
4265 struct acpi_buffer buffer = { sizeof(name), &name };
4266
4267 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4268 !strncmp("_BCL", name, sizeof(name) - 1)) {
4269 *rv = handle;
4270 return AE_CTRL_TERMINATE;
4271 } else {
4272 return AE_OK;
4273 }
4274}
4275
4276static int __init brightness_check_std_acpi_support(void)
4277{
4278 int status;
4279 void *found_node = NULL;
4280
4281 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004282 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004283 }
4284 if (!vid_handle)
4285 return 0;
4286
4287 /* Search for a _BCL method, but don't execute it */
4288 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004289 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004290
4291 return (ACPI_SUCCESS(status) && found_node != NULL);
4292}
4293
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004294static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004295{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004296 int b;
4297
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004298 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4299
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004300 mutex_init(&brightness_mutex);
4301
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004302 if (!brightness_enable) {
4303 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004304 "brightness support disabled by "
4305 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004306 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004307 } else if (brightness_enable > 1) {
4308 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004309 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004310 "standard ACPI backlight interface "
4311 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004312 return 1;
4313 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004314 }
4315
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004316 if (!brightness_mode) {
4317 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4318 brightness_mode = 2;
4319 else
4320 brightness_mode = 3;
4321
4322 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4323 brightness_mode);
4324 }
4325
4326 if (brightness_mode > 3)
4327 return -EINVAL;
4328
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004329 tp_features.bright_16levels =
4330 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4331 brightness_check_levels();
4332
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004333 b = brightness_get(NULL);
4334 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004335 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004336
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004337 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004338 printk(TPACPI_INFO
4339 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004340
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004341 ibm_backlight_device = backlight_device_register(
4342 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4343 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004344 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004345 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004346 return PTR_ERR(ibm_backlight_device);
4347 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004348 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004349
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004350 ibm_backlight_device->props.max_brightness =
4351 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004352 ibm_backlight_device->props.brightness = b;
4353 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004354
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004355 return 0;
4356}
4357
4358static void brightness_exit(void)
4359{
4360 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004361 vdbg_printk(TPACPI_DBG_EXIT,
4362 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004363 backlight_device_unregister(ibm_backlight_device);
4364 ibm_backlight_device = NULL;
4365 }
4366}
4367
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004368static int brightness_read(char *p)
4369{
4370 int len = 0;
4371 int level;
4372
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004373 level = brightness_get(NULL);
4374 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004375 len += sprintf(p + len, "level:\t\tunreadable\n");
4376 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004377 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004378 len += sprintf(p + len, "commands:\tup, down\n");
4379 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004380 " (<level> is 0-%d)\n",
4381 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004382 }
4383
4384 return len;
4385}
4386
4387static int brightness_write(char *buf)
4388{
4389 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004390 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004391 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004392 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004393
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004394 level = brightness_get(NULL);
4395 if (level < 0)
4396 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004397
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004398 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004399 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004400 if (level < max_level)
4401 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004402 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004403 if (level > 0)
4404 level--;
4405 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4406 level >= 0 && level <= max_level) {
4407 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004408 } else
4409 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004410 }
4411
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004412 /*
4413 * Now we know what the final level should be, so we try to set it.
4414 * Doing it this way makes the syscall restartable in case of EINTR
4415 */
4416 rc = brightness_set(level);
4417 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004418}
4419
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004420static struct ibm_struct brightness_driver_data = {
4421 .name = "brightness",
4422 .read = brightness_read,
4423 .write = brightness_write,
4424 .exit = brightness_exit,
4425};
4426
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004427/*************************************************************************
4428 * Volume subdriver
4429 */
4430
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004431static int volume_offset = 0x30;
4432
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004433static int volume_read(char *p)
4434{
4435 int len = 0;
4436 u8 level;
4437
4438 if (!acpi_ec_read(volume_offset, &level)) {
4439 len += sprintf(p + len, "level:\t\tunreadable\n");
4440 } else {
4441 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4442 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4443 len += sprintf(p + len, "commands:\tup, down, mute\n");
4444 len += sprintf(p + len, "commands:\tlevel <level>"
4445 " (<level> is 0-15)\n");
4446 }
4447
4448 return len;
4449}
4450
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004451static int volume_write(char *buf)
4452{
4453 int cmos_cmd, inc, i;
4454 u8 level, mute;
4455 int new_level, new_mute;
4456 char *cmd;
4457
4458 while ((cmd = next_cmd(&buf))) {
4459 if (!acpi_ec_read(volume_offset, &level))
4460 return -EIO;
4461 new_mute = mute = level & 0x40;
4462 new_level = level = level & 0xf;
4463
4464 if (strlencmp(cmd, "up") == 0) {
4465 if (mute)
4466 new_mute = 0;
4467 else
4468 new_level = level == 15 ? 15 : level + 1;
4469 } else if (strlencmp(cmd, "down") == 0) {
4470 if (mute)
4471 new_mute = 0;
4472 else
4473 new_level = level == 0 ? 0 : level - 1;
4474 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4475 new_level >= 0 && new_level <= 15) {
4476 /* new_level set */
4477 } else if (strlencmp(cmd, "mute") == 0) {
4478 new_mute = 0x40;
4479 } else
4480 return -EINVAL;
4481
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004482 if (new_level != level) {
4483 /* mute doesn't change */
4484
4485 cmos_cmd = (new_level > level) ?
4486 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004487 inc = new_level > level ? 1 : -1;
4488
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004489 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004490 !acpi_ec_write(volume_offset, level)))
4491 return -EIO;
4492
4493 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004494 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004495 !acpi_ec_write(volume_offset, i + inc))
4496 return -EIO;
4497
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004498 if (mute &&
4499 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4500 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004501 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004502 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004503 }
4504
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004505 if (new_mute != mute) {
4506 /* level doesn't change */
4507
4508 cmos_cmd = (new_mute) ?
4509 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004510
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004511 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004512 !acpi_ec_write(volume_offset, level + new_mute))
4513 return -EIO;
4514 }
4515 }
4516
4517 return 0;
4518}
4519
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004520static struct ibm_struct volume_driver_data = {
4521 .name = "volume",
4522 .read = volume_read,
4523 .write = volume_write,
4524};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004525
4526/*************************************************************************
4527 * Fan subdriver
4528 */
4529
4530/*
4531 * FAN ACCESS MODES
4532 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004533 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004534 * ACPI GFAN method: returns fan level
4535 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004536 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004537 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004538 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004539 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004540 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4541 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004542 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4543 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004544 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004545 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004546 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4547 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004548 *
4549 * Fan speed changes of any sort (including those caused by the
4550 * disengaged mode) are usually done slowly by the firmware as the
4551 * maximum ammount of fan duty cycle change per second seems to be
4552 * limited.
4553 *
4554 * Reading is not available if GFAN exists.
4555 * Writing is not available if SFAN exists.
4556 *
4557 * Bits
4558 * 7 automatic mode engaged;
4559 * (default operation mode of the ThinkPad)
4560 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004561 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004562 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004563 * the tachometer while the fan controller ramps up
4564 * the speed (which can take up to a few *minutes*).
4565 * Speeds up fan to 100% duty-cycle, which is far above
4566 * the standard RPM levels. It is not impossible that
4567 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004568 * 5-3 unused in some models. Extra bits for fan level
4569 * in others, but still useless as all values above
4570 * 7 map to the same speed as level 7 in these models.
4571 * 2-0 fan level (0..7 usually)
4572 * 0x00 = stop
4573 * 0x07 = max (set when temperatures critical)
4574 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004575 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004576 *
4577 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4578 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4579 * does so, its initial value is meaningless (0x07).
4580 *
4581 * For firmware bugs, refer to:
4582 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4583 *
4584 * ----
4585 *
4586 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4587 * Main fan tachometer reading (in RPM)
4588 *
4589 * This register is present on all ThinkPads with a new-style EC, and
4590 * it is known not to be present on the A21m/e, and T22, as there is
4591 * something else in offset 0x84 according to the ACPI DSDT. Other
4592 * ThinkPads from this same time period (and earlier) probably lack the
4593 * tachometer as well.
4594 *
4595 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4596 * was never fixed by IBM to report the EC firmware version string
4597 * probably support the tachometer (like the early X models), so
4598 * detecting it is quite hard. We need more data to know for sure.
4599 *
4600 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4601 * might result.
4602 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004603 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4604 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004605 *
4606 * For firmware bugs, refer to:
4607 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4608 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004609 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004610 * ThinkPad X31, X40, X41. Not available in the X60.
4611 *
4612 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4613 * high speed. ACPI DSDT seems to map these three speeds to levels
4614 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4615 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4616 *
4617 * The speeds are stored on handles
4618 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4619 *
4620 * There are three default speed sets, acessible as handles:
4621 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4622 *
4623 * ACPI DSDT switches which set is in use depending on various
4624 * factors.
4625 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004626 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004627 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4628 * but the ACPI tables just mention level 7.
4629 */
4630
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004631enum { /* Fan control constants */
4632 fan_status_offset = 0x2f, /* EC register 0x2f */
4633 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4634 * 0x84 must be read before 0x85 */
4635
4636 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4637 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4638
4639 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4640};
4641
4642enum fan_status_access_mode {
4643 TPACPI_FAN_NONE = 0, /* No fan status or control */
4644 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4645 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4646};
4647
4648enum fan_control_access_mode {
4649 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4650 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4651 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4652 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4653};
4654
4655enum fan_control_commands {
4656 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4657 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4658 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4659 * and also watchdog cmd */
4660};
4661
4662static int fan_control_allowed;
4663
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004664static enum fan_status_access_mode fan_status_access_mode;
4665static enum fan_control_access_mode fan_control_access_mode;
4666static enum fan_control_commands fan_control_commands;
4667
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004668static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004669static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004670static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004671
4672static struct mutex fan_mutex;
4673
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004674static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004675static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004676
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004677TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4678TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004679 "\\FSPD", /* 600e/x, 770e, 770x */
4680 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004681TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004682 "JFNS", /* 770x-JL */
4683 ); /* all others */
4684
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004685/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004686 * Call with fan_mutex held
4687 */
4688static void fan_update_desired_level(u8 status)
4689{
4690 if ((status &
4691 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4692 if (status > 7)
4693 fan_control_desired_level = 7;
4694 else
4695 fan_control_desired_level = status;
4696 }
4697}
4698
4699static int fan_get_status(u8 *status)
4700{
4701 u8 s;
4702
4703 /* TODO:
4704 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4705
4706 switch (fan_status_access_mode) {
4707 case TPACPI_FAN_RD_ACPI_GFAN:
4708 /* 570, 600e/x, 770e, 770x */
4709
4710 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4711 return -EIO;
4712
4713 if (likely(status))
4714 *status = s & 0x07;
4715
4716 break;
4717
4718 case TPACPI_FAN_RD_TPEC:
4719 /* all except 570, 600e/x, 770e, 770x */
4720 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4721 return -EIO;
4722
4723 if (likely(status))
4724 *status = s;
4725
4726 break;
4727
4728 default:
4729 return -ENXIO;
4730 }
4731
4732 return 0;
4733}
4734
4735static int fan_get_status_safe(u8 *status)
4736{
4737 int rc;
4738 u8 s;
4739
4740 if (mutex_lock_interruptible(&fan_mutex))
4741 return -ERESTARTSYS;
4742 rc = fan_get_status(&s);
4743 if (!rc)
4744 fan_update_desired_level(s);
4745 mutex_unlock(&fan_mutex);
4746
4747 if (status)
4748 *status = s;
4749
4750 return rc;
4751}
4752
4753static int fan_get_speed(unsigned int *speed)
4754{
4755 u8 hi, lo;
4756
4757 switch (fan_status_access_mode) {
4758 case TPACPI_FAN_RD_TPEC:
4759 /* all except 570, 600e/x, 770e, 770x */
4760 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4761 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4762 return -EIO;
4763
4764 if (likely(speed))
4765 *speed = (hi << 8) | lo;
4766
4767 break;
4768
4769 default:
4770 return -ENXIO;
4771 }
4772
4773 return 0;
4774}
4775
4776static int fan_set_level(int level)
4777{
4778 if (!fan_control_allowed)
4779 return -EPERM;
4780
4781 switch (fan_control_access_mode) {
4782 case TPACPI_FAN_WR_ACPI_SFAN:
4783 if (level >= 0 && level <= 7) {
4784 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4785 return -EIO;
4786 } else
4787 return -EINVAL;
4788 break;
4789
4790 case TPACPI_FAN_WR_ACPI_FANS:
4791 case TPACPI_FAN_WR_TPEC:
4792 if ((level != TP_EC_FAN_AUTO) &&
4793 (level != TP_EC_FAN_FULLSPEED) &&
4794 ((level < 0) || (level > 7)))
4795 return -EINVAL;
4796
4797 /* safety net should the EC not support AUTO
4798 * or FULLSPEED mode bits and just ignore them */
4799 if (level & TP_EC_FAN_FULLSPEED)
4800 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01004801 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004802 level |= 4; /* safety min speed 4 */
4803
4804 if (!acpi_ec_write(fan_status_offset, level))
4805 return -EIO;
4806 else
4807 tp_features.fan_ctrl_status_undef = 0;
4808 break;
4809
4810 default:
4811 return -ENXIO;
4812 }
4813 return 0;
4814}
4815
4816static int fan_set_level_safe(int level)
4817{
4818 int rc;
4819
4820 if (!fan_control_allowed)
4821 return -EPERM;
4822
4823 if (mutex_lock_interruptible(&fan_mutex))
4824 return -ERESTARTSYS;
4825
4826 if (level == TPACPI_FAN_LAST_LEVEL)
4827 level = fan_control_desired_level;
4828
4829 rc = fan_set_level(level);
4830 if (!rc)
4831 fan_update_desired_level(level);
4832
4833 mutex_unlock(&fan_mutex);
4834 return rc;
4835}
4836
4837static int fan_set_enable(void)
4838{
4839 u8 s;
4840 int rc;
4841
4842 if (!fan_control_allowed)
4843 return -EPERM;
4844
4845 if (mutex_lock_interruptible(&fan_mutex))
4846 return -ERESTARTSYS;
4847
4848 switch (fan_control_access_mode) {
4849 case TPACPI_FAN_WR_ACPI_FANS:
4850 case TPACPI_FAN_WR_TPEC:
4851 rc = fan_get_status(&s);
4852 if (rc < 0)
4853 break;
4854
4855 /* Don't go out of emergency fan mode */
4856 if (s != 7) {
4857 s &= 0x07;
4858 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4859 }
4860
4861 if (!acpi_ec_write(fan_status_offset, s))
4862 rc = -EIO;
4863 else {
4864 tp_features.fan_ctrl_status_undef = 0;
4865 rc = 0;
4866 }
4867 break;
4868
4869 case TPACPI_FAN_WR_ACPI_SFAN:
4870 rc = fan_get_status(&s);
4871 if (rc < 0)
4872 break;
4873
4874 s &= 0x07;
4875
4876 /* Set fan to at least level 4 */
4877 s |= 4;
4878
4879 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004880 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004881 else
4882 rc = 0;
4883 break;
4884
4885 default:
4886 rc = -ENXIO;
4887 }
4888
4889 mutex_unlock(&fan_mutex);
4890 return rc;
4891}
4892
4893static int fan_set_disable(void)
4894{
4895 int rc;
4896
4897 if (!fan_control_allowed)
4898 return -EPERM;
4899
4900 if (mutex_lock_interruptible(&fan_mutex))
4901 return -ERESTARTSYS;
4902
4903 rc = 0;
4904 switch (fan_control_access_mode) {
4905 case TPACPI_FAN_WR_ACPI_FANS:
4906 case TPACPI_FAN_WR_TPEC:
4907 if (!acpi_ec_write(fan_status_offset, 0x00))
4908 rc = -EIO;
4909 else {
4910 fan_control_desired_level = 0;
4911 tp_features.fan_ctrl_status_undef = 0;
4912 }
4913 break;
4914
4915 case TPACPI_FAN_WR_ACPI_SFAN:
4916 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4917 rc = -EIO;
4918 else
4919 fan_control_desired_level = 0;
4920 break;
4921
4922 default:
4923 rc = -ENXIO;
4924 }
4925
4926
4927 mutex_unlock(&fan_mutex);
4928 return rc;
4929}
4930
4931static int fan_set_speed(int speed)
4932{
4933 int rc;
4934
4935 if (!fan_control_allowed)
4936 return -EPERM;
4937
4938 if (mutex_lock_interruptible(&fan_mutex))
4939 return -ERESTARTSYS;
4940
4941 rc = 0;
4942 switch (fan_control_access_mode) {
4943 case TPACPI_FAN_WR_ACPI_FANS:
4944 if (speed >= 0 && speed <= 65535) {
4945 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4946 speed, speed, speed))
4947 rc = -EIO;
4948 } else
4949 rc = -EINVAL;
4950 break;
4951
4952 default:
4953 rc = -ENXIO;
4954 }
4955
4956 mutex_unlock(&fan_mutex);
4957 return rc;
4958}
4959
4960static void fan_watchdog_reset(void)
4961{
4962 static int fan_watchdog_active;
4963
4964 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4965 return;
4966
4967 if (fan_watchdog_active)
4968 cancel_delayed_work(&fan_watchdog_task);
4969
4970 if (fan_watchdog_maxinterval > 0 &&
4971 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4972 fan_watchdog_active = 1;
4973 if (!schedule_delayed_work(&fan_watchdog_task,
4974 msecs_to_jiffies(fan_watchdog_maxinterval
4975 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004976 printk(TPACPI_ERR
4977 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004978 "watchdog will not trigger\n");
4979 }
4980 } else
4981 fan_watchdog_active = 0;
4982}
4983
4984static void fan_watchdog_fire(struct work_struct *ignored)
4985{
4986 int rc;
4987
4988 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4989 return;
4990
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004991 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004992 rc = fan_set_enable();
4993 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004994 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004995 "will try again later...\n", -rc);
4996 /* reschedule for later */
4997 fan_watchdog_reset();
4998 }
4999}
5000
5001/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005002 * SYSFS fan layout: hwmon compatible (device)
5003 *
5004 * pwm*_enable:
5005 * 0: "disengaged" mode
5006 * 1: manual mode
5007 * 2: native EC "auto" mode (recommended, hardware default)
5008 *
5009 * pwm*: set speed in manual mode, ignored otherwise.
5010 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5011 * interpolation.
5012 *
5013 * fan*_input: tachometer reading, RPM
5014 *
5015 *
5016 * SYSFS fan layout: extensions
5017 *
5018 * fan_watchdog (driver):
5019 * fan watchdog interval in seconds, 0 disables (default), max 120
5020 */
5021
5022/* sysfs fan pwm1_enable ----------------------------------------------- */
5023static ssize_t fan_pwm1_enable_show(struct device *dev,
5024 struct device_attribute *attr,
5025 char *buf)
5026{
5027 int res, mode;
5028 u8 status;
5029
5030 res = fan_get_status_safe(&status);
5031 if (res)
5032 return res;
5033
5034 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5035 if (status != fan_control_initial_status) {
5036 tp_features.fan_ctrl_status_undef = 0;
5037 } else {
5038 /* Return most likely status. In fact, it
5039 * might be the only possible status */
5040 status = TP_EC_FAN_AUTO;
5041 }
5042 }
5043
5044 if (status & TP_EC_FAN_FULLSPEED) {
5045 mode = 0;
5046 } else if (status & TP_EC_FAN_AUTO) {
5047 mode = 2;
5048 } else
5049 mode = 1;
5050
5051 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5052}
5053
5054static ssize_t fan_pwm1_enable_store(struct device *dev,
5055 struct device_attribute *attr,
5056 const char *buf, size_t count)
5057{
5058 unsigned long t;
5059 int res, level;
5060
5061 if (parse_strtoul(buf, 2, &t))
5062 return -EINVAL;
5063
5064 switch (t) {
5065 case 0:
5066 level = TP_EC_FAN_FULLSPEED;
5067 break;
5068 case 1:
5069 level = TPACPI_FAN_LAST_LEVEL;
5070 break;
5071 case 2:
5072 level = TP_EC_FAN_AUTO;
5073 break;
5074 case 3:
5075 /* reserved for software-controlled auto mode */
5076 return -ENOSYS;
5077 default:
5078 return -EINVAL;
5079 }
5080
5081 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005082 if (res == -ENXIO)
5083 return -EINVAL;
5084 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005085 return res;
5086
5087 fan_watchdog_reset();
5088
5089 return count;
5090}
5091
5092static struct device_attribute dev_attr_fan_pwm1_enable =
5093 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5094 fan_pwm1_enable_show, fan_pwm1_enable_store);
5095
5096/* sysfs fan pwm1 ------------------------------------------------------ */
5097static ssize_t fan_pwm1_show(struct device *dev,
5098 struct device_attribute *attr,
5099 char *buf)
5100{
5101 int res;
5102 u8 status;
5103
5104 res = fan_get_status_safe(&status);
5105 if (res)
5106 return res;
5107
5108 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5109 if (status != fan_control_initial_status) {
5110 tp_features.fan_ctrl_status_undef = 0;
5111 } else {
5112 status = TP_EC_FAN_AUTO;
5113 }
5114 }
5115
5116 if ((status &
5117 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5118 status = fan_control_desired_level;
5119
5120 if (status > 7)
5121 status = 7;
5122
5123 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5124}
5125
5126static ssize_t fan_pwm1_store(struct device *dev,
5127 struct device_attribute *attr,
5128 const char *buf, size_t count)
5129{
5130 unsigned long s;
5131 int rc;
5132 u8 status, newlevel;
5133
5134 if (parse_strtoul(buf, 255, &s))
5135 return -EINVAL;
5136
5137 /* scale down from 0-255 to 0-7 */
5138 newlevel = (s >> 5) & 0x07;
5139
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005140 if (mutex_lock_interruptible(&fan_mutex))
5141 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005142
5143 rc = fan_get_status(&status);
5144 if (!rc && (status &
5145 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5146 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005147 if (rc == -ENXIO)
5148 rc = -EINVAL;
5149 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005150 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005151 fan_watchdog_reset();
5152 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005153 }
5154
5155 mutex_unlock(&fan_mutex);
5156 return (rc)? rc : count;
5157}
5158
5159static struct device_attribute dev_attr_fan_pwm1 =
5160 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5161 fan_pwm1_show, fan_pwm1_store);
5162
5163/* sysfs fan fan1_input ------------------------------------------------ */
5164static ssize_t fan_fan1_input_show(struct device *dev,
5165 struct device_attribute *attr,
5166 char *buf)
5167{
5168 int res;
5169 unsigned int speed;
5170
5171 res = fan_get_speed(&speed);
5172 if (res < 0)
5173 return res;
5174
5175 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5176}
5177
5178static struct device_attribute dev_attr_fan_fan1_input =
5179 __ATTR(fan1_input, S_IRUGO,
5180 fan_fan1_input_show, NULL);
5181
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005182/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005183static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5184 char *buf)
5185{
5186 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5187}
5188
5189static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5190 const char *buf, size_t count)
5191{
5192 unsigned long t;
5193
5194 if (parse_strtoul(buf, 120, &t))
5195 return -EINVAL;
5196
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005197 if (!fan_control_allowed)
5198 return -EPERM;
5199
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005200 fan_watchdog_maxinterval = t;
5201 fan_watchdog_reset();
5202
5203 return count;
5204}
5205
5206static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5207 fan_fan_watchdog_show, fan_fan_watchdog_store);
5208
5209/* --------------------------------------------------------------------- */
5210static struct attribute *fan_attributes[] = {
5211 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5212 &dev_attr_fan_fan1_input.attr,
5213 NULL
5214};
5215
5216static const struct attribute_group fan_attr_group = {
5217 .attrs = fan_attributes,
5218};
5219
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005220static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005221{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005222 int rc;
5223
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005224 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5225
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005226 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005227 fan_status_access_mode = TPACPI_FAN_NONE;
5228 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005229 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005230 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005231 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005232 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005233
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005234 TPACPI_ACPIHANDLE_INIT(fans);
5235 TPACPI_ACPIHANDLE_INIT(gfan);
5236 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005237
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005238 if (gfan_handle) {
5239 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005240 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005241 } else {
5242 /* all other ThinkPads: note that even old-style
5243 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005244 if (likely(acpi_ec_read(fan_status_offset,
5245 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005246 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005247
5248 /* In some ThinkPads, neither the EC nor the ACPI
5249 * DSDT initialize the fan status, and it ends up
5250 * being set to 0x07 when it *could* be either
5251 * 0x07 or 0x80.
5252 *
5253 * Enable for TP-1Y (T43), TP-78 (R51e),
5254 * TP-76 (R52), TP-70 (T43, R52), which are known
5255 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005256 if (fan_control_initial_status == 0x07) {
5257 switch (thinkpad_id.ec_model) {
5258 case 0x5931: /* TP-1Y */
5259 case 0x3837: /* TP-78 */
5260 case 0x3637: /* TP-76 */
5261 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005262 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005263 "fan_init: initial fan status "
5264 "is unknown, assuming it is "
5265 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005266 tp_features.fan_ctrl_status_undef = 1;
5267 ;;
5268 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005269 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005270 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005271 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005272 "ThinkPad ACPI EC access misbehaving, "
5273 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005274 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005275 }
5276 }
5277
5278 if (sfan_handle) {
5279 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005280 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005281 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005282 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005283 } else {
5284 if (!gfan_handle) {
5285 /* gfan without sfan means no fan control */
5286 /* all other models implement TP EC 0x2f control */
5287
5288 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005289 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005290 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005291 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005292 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005293 TPACPI_FAN_CMD_SPEED |
5294 TPACPI_FAN_CMD_LEVEL |
5295 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005296 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005297 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005298 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005299 TPACPI_FAN_CMD_LEVEL |
5300 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005301 }
5302 }
5303 }
5304
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005305 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5306 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5307 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5308 fan_status_access_mode, fan_control_access_mode);
5309
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005310 /* fan control master switch */
5311 if (!fan_control_allowed) {
5312 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5313 fan_control_commands = 0;
5314 dbg_printk(TPACPI_DBG_INIT,
5315 "fan control features disabled by parameter\n");
5316 }
5317
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005318 /* update fan_control_desired_level */
5319 if (fan_status_access_mode != TPACPI_FAN_NONE)
5320 fan_get_status_safe(NULL);
5321
5322 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5323 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005324 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005325 &fan_attr_group);
5326 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005327 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005328 &driver_attr_fan_watchdog);
5329 if (rc < 0)
5330 return rc;
5331 return 0;
5332 } else
5333 return 1;
5334}
5335
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005336static void fan_exit(void)
5337{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005338 vdbg_printk(TPACPI_DBG_EXIT,
5339 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005340
5341 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005342 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005343 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5344 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005345
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005346 cancel_delayed_work(&fan_watchdog_task);
5347 flush_scheduled_work();
5348}
5349
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005350static int fan_read(char *p)
5351{
5352 int len = 0;
5353 int rc;
5354 u8 status;
5355 unsigned int speed = 0;
5356
5357 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005358 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005359 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005360 rc = fan_get_status_safe(&status);
5361 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005362 return rc;
5363
5364 len += sprintf(p + len, "status:\t\t%s\n"
5365 "level:\t\t%d\n",
5366 (status != 0) ? "enabled" : "disabled", status);
5367 break;
5368
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005369 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005370 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005371 rc = fan_get_status_safe(&status);
5372 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005373 return rc;
5374
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005375 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005376 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005377 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005378 else
5379 /* Return most likely status. In fact, it
5380 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005381 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005382 }
5383
5384 len += sprintf(p + len, "status:\t\t%s\n",
5385 (status != 0) ? "enabled" : "disabled");
5386
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005387 rc = fan_get_speed(&speed);
5388 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005389 return rc;
5390
5391 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5392
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005393 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005394 /* Disengaged mode takes precedence */
5395 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005396 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005397 len += sprintf(p + len, "level:\t\tauto\n");
5398 else
5399 len += sprintf(p + len, "level:\t\t%d\n", status);
5400 break;
5401
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005402 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005403 default:
5404 len += sprintf(p + len, "status:\t\tnot supported\n");
5405 }
5406
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005407 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005408 len += sprintf(p + len, "commands:\tlevel <level>");
5409
5410 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005411 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005412 len += sprintf(p + len, " (<level> is 0-7)\n");
5413 break;
5414
5415 default:
5416 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005417 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005418 break;
5419 }
5420 }
5421
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005422 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005423 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005424 "commands:\twatchdog <timeout> (<timeout> "
5425 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005426
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005427 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005428 len += sprintf(p + len, "commands:\tspeed <speed>"
5429 " (<speed> is 0-65535)\n");
5430
5431 return len;
5432}
5433
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005434static int fan_write_cmd_level(const char *cmd, int *rc)
5435{
5436 int level;
5437
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005438 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005439 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005440 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005441 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005442 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005443 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005444 return 0;
5445
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005446 *rc = fan_set_level_safe(level);
5447 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005448 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005449 "access mode %d", fan_control_access_mode);
5450
5451 return 1;
5452}
5453
5454static int fan_write_cmd_enable(const char *cmd, int *rc)
5455{
5456 if (strlencmp(cmd, "enable") != 0)
5457 return 0;
5458
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005459 *rc = fan_set_enable();
5460 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005461 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005462 "access mode %d", fan_control_access_mode);
5463
5464 return 1;
5465}
5466
5467static int fan_write_cmd_disable(const char *cmd, int *rc)
5468{
5469 if (strlencmp(cmd, "disable") != 0)
5470 return 0;
5471
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005472 *rc = fan_set_disable();
5473 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005474 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005475 "access mode %d", fan_control_access_mode);
5476
5477 return 1;
5478}
5479
5480static int fan_write_cmd_speed(const char *cmd, int *rc)
5481{
5482 int speed;
5483
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005484 /* TODO:
5485 * Support speed <low> <medium> <high> ? */
5486
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005487 if (sscanf(cmd, "speed %d", &speed) != 1)
5488 return 0;
5489
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005490 *rc = fan_set_speed(speed);
5491 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005492 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005493 "access mode %d", fan_control_access_mode);
5494
5495 return 1;
5496}
5497
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005498static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5499{
5500 int interval;
5501
5502 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5503 return 0;
5504
5505 if (interval < 0 || interval > 120)
5506 *rc = -EINVAL;
5507 else
5508 fan_watchdog_maxinterval = interval;
5509
5510 return 1;
5511}
5512
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005513static int fan_write(char *buf)
5514{
5515 char *cmd;
5516 int rc = 0;
5517
5518 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005519 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005520 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005521 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005522 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005523 fan_write_cmd_disable(cmd, &rc) ||
5524 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005525 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005526 fan_write_cmd_speed(cmd, &rc))
5527 )
5528 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005529 else if (!rc)
5530 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005531 }
5532
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005533 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005534}
5535
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005536static struct ibm_struct fan_driver_data = {
5537 .name = "fan",
5538 .read = fan_read,
5539 .write = fan_write,
5540 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005541};
5542
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005543/****************************************************************************
5544 ****************************************************************************
5545 *
5546 * Infrastructure
5547 *
5548 ****************************************************************************
5549 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005551/* sysfs name ---------------------------------------------------------- */
5552static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5553 struct device_attribute *attr,
5554 char *buf)
5555{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005556 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005557}
5558
5559static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5560 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5561
5562/* --------------------------------------------------------------------- */
5563
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005564/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005565static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005566
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005567/*
5568 * Module and infrastructure proble, init and exit handling
5569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005571static int force_load;
5572
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005573#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005574static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005575{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005576 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005577
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005578 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005579}
5580#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5581
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005582static void ibm_exit(struct ibm_struct *ibm)
5583{
5584 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5585
5586 list_del_init(&ibm->all_drivers);
5587
5588 if (ibm->flags.acpi_notify_installed) {
5589 dbg_printk(TPACPI_DBG_EXIT,
5590 "%s: acpi_remove_notify_handler\n", ibm->name);
5591 BUG_ON(!ibm->acpi);
5592 acpi_remove_notify_handler(*ibm->acpi->handle,
5593 ibm->acpi->type,
5594 dispatch_acpi_notify);
5595 ibm->flags.acpi_notify_installed = 0;
5596 ibm->flags.acpi_notify_installed = 0;
5597 }
5598
5599 if (ibm->flags.proc_created) {
5600 dbg_printk(TPACPI_DBG_EXIT,
5601 "%s: remove_proc_entry\n", ibm->name);
5602 remove_proc_entry(ibm->name, proc_dir);
5603 ibm->flags.proc_created = 0;
5604 }
5605
5606 if (ibm->flags.acpi_driver_registered) {
5607 dbg_printk(TPACPI_DBG_EXIT,
5608 "%s: acpi_bus_unregister_driver\n", ibm->name);
5609 BUG_ON(!ibm->acpi);
5610 acpi_bus_unregister_driver(ibm->acpi->driver);
5611 kfree(ibm->acpi->driver);
5612 ibm->acpi->driver = NULL;
5613 ibm->flags.acpi_driver_registered = 0;
5614 }
5615
5616 if (ibm->flags.init_called && ibm->exit) {
5617 ibm->exit();
5618 ibm->flags.init_called = 0;
5619 }
5620
5621 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5622}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005623
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005624static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625{
5626 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005627 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 struct proc_dir_entry *entry;
5629
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005630 BUG_ON(ibm == NULL);
5631
5632 INIT_LIST_HEAD(&ibm->all_drivers);
5633
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005634 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 return 0;
5636
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005637 dbg_printk(TPACPI_DBG_INIT,
5638 "probing for %s\n", ibm->name);
5639
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005640 if (iibm->init) {
5641 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005642 if (ret > 0)
5643 return 0; /* probe failed */
5644 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005646
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005647 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 }
5649
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005650 if (ibm->acpi) {
5651 if (ibm->acpi->hid) {
5652 ret = register_tpacpi_subdriver(ibm);
5653 if (ret)
5654 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005655 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005656
5657 if (ibm->acpi->notify) {
5658 ret = setup_acpi_notify(ibm);
5659 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005660 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005661 ibm->name);
5662 ret = 0;
5663 goto err_out;
5664 }
5665 if (ret < 0)
5666 goto err_out;
5667 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005668 }
5669
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005670 dbg_printk(TPACPI_DBG_INIT,
5671 "%s installed\n", ibm->name);
5672
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005673 if (ibm->read) {
5674 entry = create_proc_entry(ibm->name,
5675 S_IFREG | S_IRUGO | S_IWUSR,
5676 proc_dir);
5677 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005678 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005679 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005680 ret = -ENODEV;
5681 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005682 }
5683 entry->owner = THIS_MODULE;
5684 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005685 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005686 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005687 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005688 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005691 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005694
5695err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005696 dbg_printk(TPACPI_DBG_INIT,
5697 "%s: at error exit path with result %d\n",
5698 ibm->name, ret);
5699
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005700 ibm_exit(ibm);
5701 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702}
5703
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005704/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005706static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005707{
Jeff Garzik18552562007-10-03 15:15:40 -04005708 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005709 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005710
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005711 if (!tp)
5712 return;
5713
5714 memset(tp, 0, sizeof(*tp));
5715
5716 if (dmi_name_in_vendors("IBM"))
5717 tp->vendor = PCI_VENDOR_ID_IBM;
5718 else if (dmi_name_in_vendors("LENOVO"))
5719 tp->vendor = PCI_VENDOR_ID_LENOVO;
5720 else
5721 return;
5722
5723 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5724 GFP_KERNEL);
5725 if (!tp->bios_version_str)
5726 return;
5727 tp->bios_model = tp->bios_version_str[0]
5728 | (tp->bios_version_str[1] << 8);
5729
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005730 /*
5731 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5732 * X32 or newer, all Z series; Some models must have an
5733 * up-to-date BIOS or they will not be detected.
5734 *
5735 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5736 */
5737 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005738 if (sscanf(dev->name,
5739 "IBM ThinkPad Embedded Controller -[%17c",
5740 ec_fw_string) == 1) {
5741 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5742 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005743
5744 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5745 tp->ec_model = ec_fw_string[0]
5746 | (ec_fw_string[1] << 8);
5747 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005748 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005749 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005750
5751 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5752 GFP_KERNEL);
5753 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5754 kfree(tp->model_str);
5755 tp->model_str = NULL;
5756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757}
5758
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005759static int __init probe_for_thinkpad(void)
5760{
5761 int is_thinkpad;
5762
5763 if (acpi_disabled)
5764 return -ENODEV;
5765
5766 /*
5767 * Non-ancient models have better DMI tagging, but very old models
5768 * don't.
5769 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005770 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005771
5772 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005773 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005774 if (!ec_handle) {
5775 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005776 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005777 "Not yet supported ThinkPad detected!\n");
5778 return -ENODEV;
5779 }
5780
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005781 /*
5782 * Risks a regression on very old machines, but reduces potential
5783 * false positives a damn great deal
5784 */
5785 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005786 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005787
5788 if (!is_thinkpad && !force_load)
5789 return -ENODEV;
5790
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005791 return 0;
5792}
5793
5794
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005795/* Module init, exit, parameters */
5796
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005797static struct ibm_init_struct ibms_init[] __initdata = {
5798 {
5799 .init = thinkpad_acpi_driver_init,
5800 .data = &thinkpad_acpi_driver_data,
5801 },
5802 {
5803 .init = hotkey_init,
5804 .data = &hotkey_driver_data,
5805 },
5806 {
5807 .init = bluetooth_init,
5808 .data = &bluetooth_driver_data,
5809 },
5810 {
5811 .init = wan_init,
5812 .data = &wan_driver_data,
5813 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005814#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005815 {
5816 .init = video_init,
5817 .data = &video_driver_data,
5818 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005819#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005820 {
5821 .init = light_init,
5822 .data = &light_driver_data,
5823 },
5824#ifdef CONFIG_THINKPAD_ACPI_DOCK
5825 {
5826 .init = dock_init,
5827 .data = &dock_driver_data[0],
5828 },
5829 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005830 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005831 .data = &dock_driver_data[1],
5832 },
5833#endif
5834#ifdef CONFIG_THINKPAD_ACPI_BAY
5835 {
5836 .init = bay_init,
5837 .data = &bay_driver_data,
5838 },
5839#endif
5840 {
5841 .init = cmos_init,
5842 .data = &cmos_driver_data,
5843 },
5844 {
5845 .init = led_init,
5846 .data = &led_driver_data,
5847 },
5848 {
5849 .init = beep_init,
5850 .data = &beep_driver_data,
5851 },
5852 {
5853 .init = thermal_init,
5854 .data = &thermal_driver_data,
5855 },
5856 {
5857 .data = &ecdump_driver_data,
5858 },
5859 {
5860 .init = brightness_init,
5861 .data = &brightness_driver_data,
5862 },
5863 {
5864 .data = &volume_driver_data,
5865 },
5866 {
5867 .init = fan_init,
5868 .data = &fan_driver_data,
5869 },
5870};
5871
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005872static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5873{
5874 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005875 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005876
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005877 if (!kp || !kp->name || !val)
5878 return -EINVAL;
5879
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005880 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5881 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005882 WARN_ON(ibm == NULL);
5883
5884 if (!ibm || !ibm->name)
5885 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005886
5887 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5888 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005889 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005890 strcpy(ibms_init[i].param, val);
5891 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005892 return 0;
5893 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005894 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005895
5896 return -EINVAL;
5897}
5898
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005899module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005900MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005901 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005902
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005903module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005904MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005905
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005906module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005907MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005908 "Attempts to load the driver even on a "
5909 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005910
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005911module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005912MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005913 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005914
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005915module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005916MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005917 "Selects brightness control strategy: "
5918 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005919
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005920module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005921MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005922 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005923
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005924module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005925MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005926 "used for backwards compatibility with userspace, "
5927 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005928
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005929#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005930 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02005931 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005932 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005933
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005934TPACPI_PARAM(hotkey);
5935TPACPI_PARAM(bluetooth);
5936TPACPI_PARAM(video);
5937TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005938#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005939TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005940#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005941#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005942TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005943#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005944TPACPI_PARAM(cmos);
5945TPACPI_PARAM(led);
5946TPACPI_PARAM(beep);
5947TPACPI_PARAM(ecdump);
5948TPACPI_PARAM(brightness);
5949TPACPI_PARAM(volume);
5950TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005951
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005952static void thinkpad_acpi_module_exit(void)
5953{
5954 struct ibm_struct *ibm, *itmp;
5955
5956 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5957
5958 list_for_each_entry_safe_reverse(ibm, itmp,
5959 &tpacpi_all_drivers,
5960 all_drivers) {
5961 ibm_exit(ibm);
5962 }
5963
5964 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5965
5966 if (tpacpi_inputdev) {
5967 if (tp_features.input_device_registered)
5968 input_unregister_device(tpacpi_inputdev);
5969 else
5970 input_free_device(tpacpi_inputdev);
5971 }
5972
5973 if (tpacpi_hwmon)
5974 hwmon_device_unregister(tpacpi_hwmon);
5975
5976 if (tp_features.sensors_pdev_attrs_registered)
5977 device_remove_file(&tpacpi_sensors_pdev->dev,
5978 &dev_attr_thinkpad_acpi_pdev_name);
5979 if (tpacpi_sensors_pdev)
5980 platform_device_unregister(tpacpi_sensors_pdev);
5981 if (tpacpi_pdev)
5982 platform_device_unregister(tpacpi_pdev);
5983
5984 if (tp_features.sensors_pdrv_attrs_registered)
5985 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5986 if (tp_features.platform_drv_attrs_registered)
5987 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5988
5989 if (tp_features.sensors_pdrv_registered)
5990 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5991
5992 if (tp_features.platform_drv_registered)
5993 platform_driver_unregister(&tpacpi_pdriver);
5994
5995 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005996 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005997
5998 kfree(thinkpad_id.bios_version_str);
5999 kfree(thinkpad_id.ec_version_str);
6000 kfree(thinkpad_id.model_str);
6001}
6002
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006003
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006004static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005{
6006 int ret, i;
6007
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006008 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6009
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006010 /* Parameter checking */
6011 if (hotkey_report_mode > 2)
6012 return -EINVAL;
6013
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006014 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006015
6016 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006017 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006018 if (ret) {
6019 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006020 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006023 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006024
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006025 TPACPI_ACPIHANDLE_INIT(ecrd);
6026 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006028 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006030 printk(TPACPI_ERR
6031 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006032 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 return -ENODEV;
6034 }
6035 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006036
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006037 ret = platform_driver_register(&tpacpi_pdriver);
6038 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006039 printk(TPACPI_ERR
6040 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006041 thinkpad_acpi_module_exit();
6042 return ret;
6043 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006044 tp_features.platform_drv_registered = 1;
6045
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006046 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6047 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006048 printk(TPACPI_ERR
6049 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006050 thinkpad_acpi_module_exit();
6051 return ret;
6052 }
6053 tp_features.sensors_pdrv_registered = 1;
6054
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006055 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006056 if (!ret) {
6057 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006058 ret = tpacpi_create_driver_attributes(
6059 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006060 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006061 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006062 printk(TPACPI_ERR
6063 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006064 thinkpad_acpi_module_exit();
6065 return ret;
6066 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006067 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006068
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006069
6070 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006071 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006072 NULL, 0);
6073 if (IS_ERR(tpacpi_pdev)) {
6074 ret = PTR_ERR(tpacpi_pdev);
6075 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006076 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006077 thinkpad_acpi_module_exit();
6078 return ret;
6079 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006080 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006081 TPACPI_HWMON_DRVR_NAME,
6082 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006083 if (IS_ERR(tpacpi_sensors_pdev)) {
6084 ret = PTR_ERR(tpacpi_sensors_pdev);
6085 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006086 printk(TPACPI_ERR
6087 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006088 thinkpad_acpi_module_exit();
6089 return ret;
6090 }
6091 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6092 &dev_attr_thinkpad_acpi_pdev_name);
6093 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006094 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006095 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006096 thinkpad_acpi_module_exit();
6097 return ret;
6098 }
6099 tp_features.sensors_pdev_attrs_registered = 1;
6100 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006101 if (IS_ERR(tpacpi_hwmon)) {
6102 ret = PTR_ERR(tpacpi_hwmon);
6103 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006104 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006105 thinkpad_acpi_module_exit();
6106 return ret;
6107 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006108 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006109 tpacpi_inputdev = input_allocate_device();
6110 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006111 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006112 thinkpad_acpi_module_exit();
6113 return -ENOMEM;
6114 } else {
6115 /* Prepare input device, but don't register */
6116 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006117 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006118 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006119 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6120 thinkpad_id.vendor :
6121 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006122 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6123 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6124 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006125 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6126 ret = ibm_init(&ibms_init[i]);
6127 if (ret >= 0 && *ibms_init[i].param)
6128 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006130 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 return ret;
6132 }
6133 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006134 ret = input_register_device(tpacpi_inputdev);
6135 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006136 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006137 thinkpad_acpi_module_exit();
6138 return ret;
6139 } else {
6140 tp_features.input_device_registered = 1;
6141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006143 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 return 0;
6145}
6146
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006147/* Please remove this in year 2009 */
6148MODULE_ALIAS("ibm_acpi");
6149
6150/*
6151 * DMI matching for module autoloading
6152 *
6153 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6154 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6155 *
6156 * Only models listed in thinkwiki will be supported, so add yours
6157 * if it is not there yet.
6158 */
6159#define IBM_BIOS_MODULE_ALIAS(__type) \
6160 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6161
6162/* Non-ancient thinkpads */
6163MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6164MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6165
6166/* Ancient thinkpad BIOSes have to be identified by
6167 * BIOS type or model number, and there are far less
6168 * BIOS types than model numbers... */
6169IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6170IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6171IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6172
6173MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006174MODULE_DESCRIPTION(TPACPI_DESC);
6175MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006176MODULE_LICENSE("GPL");
6177
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006178module_init(thinkpad_acpi_module_init);
6179module_exit(thinkpad_acpi_module_exit);