blob: 35483502a11788f44f3155604e0967f6e1878d3c [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
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001175static void tpacpi_input_send_tabletsw(unsigned int state)
1176{
1177 mutex_lock(&tpacpi_inputdev_send_mutex);
1178
1179 input_report_switch(tpacpi_inputdev,
1180 SW_TABLET_MODE, !!state);
1181 input_sync(tpacpi_inputdev);
1182
1183 mutex_unlock(&tpacpi_inputdev_send_mutex);
1184}
1185
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001186static void tpacpi_input_send_key(unsigned int scancode)
1187{
1188 unsigned int keycode;
1189
1190 keycode = hotkey_keycode_map[scancode];
1191
1192 if (keycode != KEY_RESERVED) {
1193 mutex_lock(&tpacpi_inputdev_send_mutex);
1194
1195 input_report_key(tpacpi_inputdev, keycode, 1);
1196 if (keycode == KEY_UNKNOWN)
1197 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1198 scancode);
1199 input_sync(tpacpi_inputdev);
1200
1201 input_report_key(tpacpi_inputdev, keycode, 0);
1202 if (keycode == KEY_UNKNOWN)
1203 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1204 scancode);
1205 input_sync(tpacpi_inputdev);
1206
1207 mutex_unlock(&tpacpi_inputdev_send_mutex);
1208 }
1209}
1210
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001211#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1212static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1213
1214static void tpacpi_hotkey_send_key(unsigned int scancode)
1215{
1216 tpacpi_input_send_key(scancode);
1217 if (hotkey_report_mode < 2) {
1218 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1219 0x80, 0x1001 + scancode);
1220 }
1221}
1222
1223static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1224{
1225 u8 d;
1226
1227 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1228 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1229 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1230 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1231 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1232 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1233 }
1234 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1235 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1236 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1237 }
1238 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1239 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1240 n->displayexp_toggle =
1241 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1242 }
1243 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1244 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1245 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1246 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1247 n->brightness_toggle =
1248 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1249 }
1250 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1251 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1252 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1253 >> TP_NVRAM_POS_LEVEL_VOLUME;
1254 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1255 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1256 }
1257}
1258
1259#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001260 do { \
1261 if ((mask & (1 << __scancode)) && \
1262 oldn->__member != newn->__member) \
1263 tpacpi_hotkey_send_key(__scancode); \
1264 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001265
1266#define TPACPI_MAY_SEND_KEY(__scancode) \
1267 do { if (mask & (1 << __scancode)) \
1268 tpacpi_hotkey_send_key(__scancode); } while (0)
1269
1270static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001271 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001272 u32 mask)
1273{
1274 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1275 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1276 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1277 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1278
1279 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1280
1281 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1282
1283 /* handle volume */
1284 if (oldn->volume_toggle != newn->volume_toggle) {
1285 if (oldn->mute != newn->mute) {
1286 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1287 }
1288 if (oldn->volume_level > newn->volume_level) {
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1290 } else if (oldn->volume_level < newn->volume_level) {
1291 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1292 } else if (oldn->mute == newn->mute) {
1293 /* repeated key presses that didn't change state */
1294 if (newn->mute) {
1295 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1296 } else if (newn->volume_level != 0) {
1297 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1298 } else {
1299 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1300 }
1301 }
1302 }
1303
1304 /* handle brightness */
1305 if (oldn->brightness_toggle != newn->brightness_toggle) {
1306 if (oldn->brightness_level < newn->brightness_level) {
1307 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1308 } else if (oldn->brightness_level > newn->brightness_level) {
1309 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1310 } else {
1311 /* repeated key presses that didn't change state */
1312 if (newn->brightness_level != 0) {
1313 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1314 } else {
1315 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1316 }
1317 }
1318 }
1319}
1320
1321#undef TPACPI_COMPARE_KEY
1322#undef TPACPI_MAY_SEND_KEY
1323
1324static int hotkey_kthread(void *data)
1325{
1326 struct tp_nvram_state s[2];
1327 u32 mask;
1328 unsigned int si, so;
1329 unsigned long t;
1330 unsigned int change_detector, must_reset;
1331
1332 mutex_lock(&hotkey_thread_mutex);
1333
1334 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1335 goto exit;
1336
1337 set_freezable();
1338
1339 so = 0;
1340 si = 1;
1341 t = 0;
1342
1343 /* Initial state for compares */
1344 mutex_lock(&hotkey_thread_data_mutex);
1345 change_detector = hotkey_config_change;
1346 mask = hotkey_source_mask & hotkey_mask;
1347 mutex_unlock(&hotkey_thread_data_mutex);
1348 hotkey_read_nvram(&s[so], mask);
1349
1350 while (!kthread_should_stop() && hotkey_poll_freq) {
1351 if (t == 0)
1352 t = 1000/hotkey_poll_freq;
1353 t = msleep_interruptible(t);
1354 if (unlikely(kthread_should_stop()))
1355 break;
1356 must_reset = try_to_freeze();
1357 if (t > 0 && !must_reset)
1358 continue;
1359
1360 mutex_lock(&hotkey_thread_data_mutex);
1361 if (must_reset || hotkey_config_change != change_detector) {
1362 /* forget old state on thaw or config change */
1363 si = so;
1364 t = 0;
1365 change_detector = hotkey_config_change;
1366 }
1367 mask = hotkey_source_mask & hotkey_mask;
1368 mutex_unlock(&hotkey_thread_data_mutex);
1369
1370 if (likely(mask)) {
1371 hotkey_read_nvram(&s[si], mask);
1372 if (likely(si != so)) {
1373 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001374 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001375 }
1376 }
1377
1378 so = si;
1379 si ^= 1;
1380 }
1381
1382exit:
1383 mutex_unlock(&hotkey_thread_mutex);
1384 return 0;
1385}
1386
1387static void hotkey_poll_stop_sync(void)
1388{
1389 if (tpacpi_hotkey_task) {
1390 if (frozen(tpacpi_hotkey_task) ||
1391 freezing(tpacpi_hotkey_task))
1392 thaw_process(tpacpi_hotkey_task);
1393
1394 kthread_stop(tpacpi_hotkey_task);
1395 tpacpi_hotkey_task = NULL;
1396 mutex_lock(&hotkey_thread_mutex);
1397 /* at this point, the thread did exit */
1398 mutex_unlock(&hotkey_thread_mutex);
1399 }
1400}
1401
1402/* call with hotkey_mutex held */
1403static void hotkey_poll_setup(int may_warn)
1404{
1405 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1406 hotkey_poll_freq > 0 &&
1407 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1408 if (!tpacpi_hotkey_task) {
1409 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001410 NULL,
1411 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001412 if (IS_ERR(tpacpi_hotkey_task)) {
1413 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001414 printk(TPACPI_ERR
1415 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001416 "for hotkey polling\n");
1417 }
1418 }
1419 } else {
1420 hotkey_poll_stop_sync();
1421 if (may_warn &&
1422 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001423 printk(TPACPI_NOTICE
1424 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001425 "which is currently disabled\n",
1426 hotkey_source_mask);
1427 }
1428 }
1429}
1430
1431static void hotkey_poll_setup_safe(int may_warn)
1432{
1433 mutex_lock(&hotkey_mutex);
1434 hotkey_poll_setup(may_warn);
1435 mutex_unlock(&hotkey_mutex);
1436}
1437
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001438#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1439
1440static void hotkey_poll_setup_safe(int __unused)
1441{
1442}
1443
1444#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1445
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001446static int hotkey_inputdev_open(struct input_dev *dev)
1447{
1448 switch (tpacpi_lifecycle) {
1449 case TPACPI_LIFE_INIT:
1450 /*
1451 * hotkey_init will call hotkey_poll_setup_safe
1452 * at the appropriate moment
1453 */
1454 return 0;
1455 case TPACPI_LIFE_EXITING:
1456 return -EBUSY;
1457 case TPACPI_LIFE_RUNNING:
1458 hotkey_poll_setup_safe(0);
1459 return 0;
1460 }
1461
1462 /* Should only happen if tpacpi_lifecycle is corrupt */
1463 BUG();
1464 return -EBUSY;
1465}
1466
1467static void hotkey_inputdev_close(struct input_dev *dev)
1468{
1469 /* disable hotkey polling when possible */
1470 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1471 hotkey_poll_setup_safe(0);
1472}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001473
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001474/* sysfs hotkey enable ------------------------------------------------- */
1475static ssize_t hotkey_enable_show(struct device *dev,
1476 struct device_attribute *attr,
1477 char *buf)
1478{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001479 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001480
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001481 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001482 if (res)
1483 return res;
1484
1485 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1486}
1487
1488static ssize_t hotkey_enable_store(struct device *dev,
1489 struct device_attribute *attr,
1490 const char *buf, size_t count)
1491{
1492 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001493 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001494
1495 if (parse_strtoul(buf, 1, &t))
1496 return -EINVAL;
1497
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001498 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001499
1500 return (res) ? res : count;
1501}
1502
1503static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001504 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001505 hotkey_enable_show, hotkey_enable_store);
1506
1507/* sysfs hotkey mask --------------------------------------------------- */
1508static ssize_t hotkey_mask_show(struct device *dev,
1509 struct device_attribute *attr,
1510 char *buf)
1511{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001512 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001513
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001514 if (mutex_lock_interruptible(&hotkey_mutex))
1515 return -ERESTARTSYS;
1516 res = hotkey_mask_get();
1517 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001518
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001519 return (res)?
1520 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001521}
1522
1523static ssize_t hotkey_mask_store(struct device *dev,
1524 struct device_attribute *attr,
1525 const char *buf, size_t count)
1526{
1527 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001528 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001529
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001530 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001531 return -EINVAL;
1532
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001533 if (mutex_lock_interruptible(&hotkey_mutex))
1534 return -ERESTARTSYS;
1535
1536 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001537
1538#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1539 hotkey_poll_setup(1);
1540#endif
1541
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001542 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001543
1544 return (res) ? res : count;
1545}
1546
1547static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001548 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001549 hotkey_mask_show, hotkey_mask_store);
1550
1551/* sysfs hotkey bios_enabled ------------------------------------------- */
1552static ssize_t hotkey_bios_enabled_show(struct device *dev,
1553 struct device_attribute *attr,
1554 char *buf)
1555{
1556 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1557}
1558
1559static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001560 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001561
1562/* sysfs hotkey bios_mask ---------------------------------------------- */
1563static ssize_t hotkey_bios_mask_show(struct device *dev,
1564 struct device_attribute *attr,
1565 char *buf)
1566{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001567 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001568}
1569
1570static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001571 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001572
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001573/* sysfs hotkey all_mask ----------------------------------------------- */
1574static ssize_t hotkey_all_mask_show(struct device *dev,
1575 struct device_attribute *attr,
1576 char *buf)
1577{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001578 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1579 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001580}
1581
1582static struct device_attribute dev_attr_hotkey_all_mask =
1583 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1584
1585/* sysfs hotkey recommended_mask --------------------------------------- */
1586static ssize_t hotkey_recommended_mask_show(struct device *dev,
1587 struct device_attribute *attr,
1588 char *buf)
1589{
1590 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001591 (hotkey_all_mask | hotkey_source_mask)
1592 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001593}
1594
1595static struct device_attribute dev_attr_hotkey_recommended_mask =
1596 __ATTR(hotkey_recommended_mask, S_IRUGO,
1597 hotkey_recommended_mask_show, NULL);
1598
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001599#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1600
1601/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1602static ssize_t hotkey_source_mask_show(struct device *dev,
1603 struct device_attribute *attr,
1604 char *buf)
1605{
1606 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1607}
1608
1609static ssize_t hotkey_source_mask_store(struct device *dev,
1610 struct device_attribute *attr,
1611 const char *buf, size_t count)
1612{
1613 unsigned long t;
1614
1615 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1616 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1617 return -EINVAL;
1618
1619 if (mutex_lock_interruptible(&hotkey_mutex))
1620 return -ERESTARTSYS;
1621
1622 HOTKEY_CONFIG_CRITICAL_START
1623 hotkey_source_mask = t;
1624 HOTKEY_CONFIG_CRITICAL_END
1625
1626 hotkey_poll_setup(1);
1627
1628 mutex_unlock(&hotkey_mutex);
1629
1630 return count;
1631}
1632
1633static struct device_attribute dev_attr_hotkey_source_mask =
1634 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1635 hotkey_source_mask_show, hotkey_source_mask_store);
1636
1637/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1638static ssize_t hotkey_poll_freq_show(struct device *dev,
1639 struct device_attribute *attr,
1640 char *buf)
1641{
1642 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1643}
1644
1645static ssize_t hotkey_poll_freq_store(struct device *dev,
1646 struct device_attribute *attr,
1647 const char *buf, size_t count)
1648{
1649 unsigned long t;
1650
1651 if (parse_strtoul(buf, 25, &t))
1652 return -EINVAL;
1653
1654 if (mutex_lock_interruptible(&hotkey_mutex))
1655 return -ERESTARTSYS;
1656
1657 hotkey_poll_freq = t;
1658
1659 hotkey_poll_setup(1);
1660 mutex_unlock(&hotkey_mutex);
1661
1662 return count;
1663}
1664
1665static struct device_attribute dev_attr_hotkey_poll_freq =
1666 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1667 hotkey_poll_freq_show, hotkey_poll_freq_store);
1668
1669#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1670
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001671/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001672static ssize_t hotkey_radio_sw_show(struct device *dev,
1673 struct device_attribute *attr,
1674 char *buf)
1675{
1676 int res, s;
1677 res = hotkey_get_wlsw(&s);
1678 if (res < 0)
1679 return res;
1680
1681 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1682}
1683
1684static struct device_attribute dev_attr_hotkey_radio_sw =
1685 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1686
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001687static void hotkey_radio_sw_notify_change(void)
1688{
1689 if (tp_features.hotkey_wlsw)
1690 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1691 "hotkey_radio_sw");
1692}
1693
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001694/* sysfs hotkey report_mode -------------------------------------------- */
1695static ssize_t hotkey_report_mode_show(struct device *dev,
1696 struct device_attribute *attr,
1697 char *buf)
1698{
1699 return snprintf(buf, PAGE_SIZE, "%d\n",
1700 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1701}
1702
1703static struct device_attribute dev_attr_hotkey_report_mode =
1704 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1705
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001706/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001707static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1708 struct device_attribute *attr,
1709 char *buf)
1710{
1711 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1712}
1713
1714static struct device_attribute dev_attr_hotkey_wakeup_reason =
1715 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1716
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001717static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001718{
1719 if (tp_features.hotkey_mask)
1720 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1721 "wakeup_reason");
1722}
1723
1724/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001725static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1726 struct device_attribute *attr,
1727 char *buf)
1728{
1729 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1730}
1731
1732static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1733 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1734 hotkey_wakeup_hotunplug_complete_show, NULL);
1735
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001736static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001737{
1738 if (tp_features.hotkey_mask)
1739 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1740 "wakeup_hotunplug_complete");
1741}
1742
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001743/* --------------------------------------------------------------------- */
1744
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001745static struct attribute *hotkey_attributes[] __initdata = {
1746 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001747 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001748 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001749#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1750 &dev_attr_hotkey_mask.attr,
1751 &dev_attr_hotkey_all_mask.attr,
1752 &dev_attr_hotkey_recommended_mask.attr,
1753 &dev_attr_hotkey_source_mask.attr,
1754 &dev_attr_hotkey_poll_freq.attr,
1755#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001756};
1757
1758static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001759 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001760#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1761 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001762 &dev_attr_hotkey_all_mask.attr,
1763 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001764#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001765 &dev_attr_hotkey_wakeup_reason.attr,
1766 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001767};
1768
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001769static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001770{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001771 /* Requirements for changing the default keymaps:
1772 *
1773 * 1. Many of the keys are mapped to KEY_RESERVED for very
1774 * good reasons. Do not change them unless you have deep
1775 * knowledge on the IBM and Lenovo ThinkPad firmware for
1776 * the various ThinkPad models. The driver behaves
1777 * differently for KEY_RESERVED: such keys have their
1778 * hot key mask *unset* in mask_recommended, and also
1779 * in the initial hot key mask programmed into the
1780 * firmware at driver load time, which means the firm-
1781 * ware may react very differently if you change them to
1782 * something else;
1783 *
1784 * 2. You must be subscribed to the linux-thinkpad and
1785 * ibm-acpi-devel mailing lists, and you should read the
1786 * list archives since 2007 if you want to change the
1787 * keymaps. This requirement exists so that you will
1788 * know the past history of problems with the thinkpad-
1789 * acpi driver keymaps, and also that you will be
1790 * listening to any bug reports;
1791 *
1792 * 3. Do not send thinkpad-acpi specific patches directly to
1793 * for merging, *ever*. Send them to the linux-acpi
1794 * mailinglist for comments. Merging is to be done only
1795 * through acpi-test and the ACPI maintainer.
1796 *
1797 * If the above is too much to ask, don't change the keymap.
1798 * Ask the thinkpad-acpi maintainer to do it, instead.
1799 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001800 static u16 ibm_keycode_map[] __initdata = {
1801 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1802 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1803 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1804 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001805
1806 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001807 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1808 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1809 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001810
1811 /* brightness: firmware always reacts to them, unless
1812 * X.org did some tricks in the radeon BIOS scratch
1813 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001814 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001815 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001816
1817 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001818 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001819
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001820 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1821 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001822
1823 /* Volume: firmware always react to it and reprograms
1824 * the built-in *extra* mixer. Never map it to control
1825 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001826 KEY_RESERVED, /* 0x14: VOLUME UP */
1827 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1828 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001829
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001830 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001831
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001832 /* (assignments unknown, please report if found) */
1833 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1834 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1835 };
1836 static u16 lenovo_keycode_map[] __initdata = {
1837 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1838 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1839 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1840 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001841
1842 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001843 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1844 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1845 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001846
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001847 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001848 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001849
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001850 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001851
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001852 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1853 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001854
1855 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1856 * react to it and reprograms the built-in *extra* mixer.
1857 * Never map it to control another mixer by default.
1858 *
1859 * T60?, T61, R60?, R61: firmware and EC tries to send
1860 * these over the regular keyboard, so these are no-ops,
1861 * but there are still weird bugs re. MUTE, so do not
1862 * change unless you get test reports from all Lenovo
1863 * models. May cause the BIOS to interfere with the
1864 * HDA mixer.
1865 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001866 KEY_RESERVED, /* 0x14: VOLUME UP */
1867 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1868 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001869
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001870 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001871
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001872 /* (assignments unknown, please report if found) */
1873 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1874 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1875 };
1876
1877#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1878#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1879#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1880
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001881 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001882 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001883 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001884
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001885 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1886
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001887 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001888 BUG_ON(tpacpi_inputdev->open != NULL ||
1889 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001890
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001891 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001892 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001893
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001894#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1895 mutex_init(&hotkey_thread_mutex);
1896 mutex_init(&hotkey_thread_data_mutex);
1897#endif
1898
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001899 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001900 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001901
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001902 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001903 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001904
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001905 if (tp_features.hotkey) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001906 hotkey_dev_attributes = create_attr_set(12, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001907 if (!hotkey_dev_attributes)
1908 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001909 res = add_many_to_attr_set(hotkey_dev_attributes,
1910 hotkey_attributes,
1911 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001912 if (res)
1913 return res;
1914
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001915 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001916 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1917 for HKEY interface version 0x100 */
1918 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1919 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001920 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001921 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001922 printk(TPACPI_ERR "please report this to %s\n",
1923 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001924 } else {
1925 /*
1926 * MHKV 0x100 in A31, R40, R40e,
1927 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001928 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001929 tp_features.hotkey_mask = 1;
1930 }
1931 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001932
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001933 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001934 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001935
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001936 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001937 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001938 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001939 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001940 "missing MHKA handler, "
1941 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001942 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001943 /* FN+F12, FN+F4, FN+F3 */
1944 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001945 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001946 }
1947
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001948 /* hotkey_source_mask *must* be zero for
1949 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001950 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001951 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001952 res = hotkey_mask_get();
1953 hotkey_orig_mask = hotkey_mask;
1954 if (!res) {
1955 res = add_many_to_attr_set(
1956 hotkey_dev_attributes,
1957 hotkey_mask_attributes,
1958 ARRAY_SIZE(hotkey_mask_attributes));
1959 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001960 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001961
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001962#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1963 if (tp_features.hotkey_mask) {
1964 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1965 & ~hotkey_all_mask;
1966 } else {
1967 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1968 }
1969
1970 vdbg_printk(TPACPI_DBG_INIT,
1971 "hotkey source mask 0x%08x, polling freq %d\n",
1972 hotkey_source_mask, hotkey_poll_freq);
1973#endif
1974
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001975 /* Not all thinkpads have a hardware radio switch */
1976 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1977 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001978 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001979 "radio switch found; radios are %s\n",
1980 enabled(status, 0));
1981 res = add_to_attr_set(hotkey_dev_attributes,
1982 &dev_attr_hotkey_radio_sw.attr);
1983 }
1984
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001985 if (!res)
1986 res = register_attr_set_with_sysfs(
1987 hotkey_dev_attributes,
1988 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001989 if (res)
1990 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001991
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001992 /* Set up key map */
1993
1994 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1995 GFP_KERNEL);
1996 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001997 printk(TPACPI_ERR
1998 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001999 return -ENOMEM;
2000 }
2001
2002 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2003 dbg_printk(TPACPI_DBG_INIT,
2004 "using Lenovo default hot key map\n");
2005 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2006 TPACPI_HOTKEY_MAP_SIZE);
2007 } else {
2008 dbg_printk(TPACPI_DBG_INIT,
2009 "using IBM default hot key map\n");
2010 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2011 TPACPI_HOTKEY_MAP_SIZE);
2012 }
2013
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002014 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2015 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2016 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002017 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2018 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2019 tpacpi_inputdev->keycode = hotkey_keycode_map;
2020 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002021 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2022 set_bit(hotkey_keycode_map[i],
2023 tpacpi_inputdev->keybit);
2024 } else {
2025 if (i < sizeof(hotkey_reserved_mask)*8)
2026 hotkey_reserved_mask |= 1 << i;
2027 }
2028 }
2029
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002030 if (tp_features.hotkey_wlsw) {
2031 set_bit(EV_SW, tpacpi_inputdev->evbit);
2032 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2033 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002034 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2035 set_bit(EV_SW, tpacpi_inputdev->evbit);
2036 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2037 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002038
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002039 dbg_printk(TPACPI_DBG_INIT,
2040 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002041 res = hotkey_status_set(1);
2042 if (res)
2043 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002044 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2045 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002046 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002047 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002048 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002049
2050 dbg_printk(TPACPI_DBG_INIT,
2051 "legacy hot key reporting over procfs %s\n",
2052 (hotkey_report_mode < 2) ?
2053 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002054
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002055 tpacpi_inputdev->open = &hotkey_inputdev_open;
2056 tpacpi_inputdev->close = &hotkey_inputdev_close;
2057
2058 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002059 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002060 }
2061
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002062 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002063}
2064
2065static void hotkey_exit(void)
2066{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002067#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2068 hotkey_poll_stop_sync();
2069#endif
2070
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002071 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002072 dbg_printk(TPACPI_DBG_EXIT,
2073 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002074 /* no short-circuit boolean operator below! */
2075 if ((hotkey_mask_set(hotkey_orig_mask) |
2076 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002077 printk(TPACPI_ERR
2078 "failed to restore hot key mask "
2079 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002080 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002081
2082 if (hotkey_dev_attributes) {
2083 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2084 hotkey_dev_attributes = NULL;
2085 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002086}
2087
2088static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2089{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002090 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002091 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002092 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002093 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002094 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002095
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002096 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002097 printk(TPACPI_ERR
2098 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002099 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002100 acpi_bus_generate_netlink_event(
2101 ibm->acpi->device->pnp.device_class,
2102 ibm->acpi->device->dev.bus_id,
2103 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002104 return;
2105 }
2106
2107 while (1) {
2108 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002109 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002110 return;
2111 }
2112
2113 if (hkey == 0) {
2114 /* queue empty */
2115 return;
2116 }
2117
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002118 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002119 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002120 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002121
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002122 switch (hkey >> 12) {
2123 case 1:
2124 /* 0x1000-0x1FFF: key presses */
2125 scancode = hkey & 0xfff;
2126 if (scancode > 0 && scancode < 0x21) {
2127 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002128 if (!(hotkey_source_mask & (1 << scancode))) {
2129 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002130 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002131 } else {
2132 ignore_acpi_ev = 1;
2133 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002134 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002135 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002136 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002137 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002138 case 2:
2139 /* Wakeup reason */
2140 switch (hkey) {
2141 case 0x2304: /* suspend, undock */
2142 case 0x2404: /* hibernation, undock */
2143 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2144 ignore_acpi_ev = 1;
2145 break;
2146 case 0x2305: /* suspend, bay eject */
2147 case 0x2405: /* hibernation, bay eject */
2148 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2149 ignore_acpi_ev = 1;
2150 break;
2151 default:
2152 unk_ev = 1;
2153 }
2154 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2155 printk(TPACPI_INFO
2156 "woke up due to a hot-unplug "
2157 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002158 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002159 }
2160 break;
2161 case 3:
2162 /* bay-related wakeups */
2163 if (hkey == 0x3003) {
2164 hotkey_autosleep_ack = 1;
2165 printk(TPACPI_INFO
2166 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002167 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002168 } else {
2169 unk_ev = 1;
2170 }
2171 break;
2172 case 4:
2173 /* dock-related wakeups */
2174 if (hkey == 0x4003) {
2175 hotkey_autosleep_ack = 1;
2176 printk(TPACPI_INFO
2177 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002178 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002179 } else {
2180 unk_ev = 1;
2181 }
2182 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002183 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002184 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002185 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002186 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002187 case 0x500b: /* X61t: tablet pen inserted into bay */
2188 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002189 break;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002190 case 0x5009: /* X61t: swivel up (tablet mode) */
2191 case 0x500a: /* X61t: swivel down (normal mode) */
2192 tpacpi_input_send_tabletsw((hkey == 0x5009));
2193 send_acpi_ev = 0;
2194 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002195 case 0x5001:
2196 case 0x5002:
2197 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002198 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002199 break;
2200 default:
2201 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002202 }
2203 break;
2204 case 7:
2205 /* 0x7000-0x7FFF: misc */
2206 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2207 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002208 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002209 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002210 break;
2211 }
2212 /* fallthrough to default */
2213 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002214 unk_ev = 1;
2215 }
2216 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002217 printk(TPACPI_NOTICE
2218 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002219 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002220
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002221 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002222 if (!ignore_acpi_ev &&
2223 (send_acpi_ev || hotkey_report_mode < 2)) {
2224 acpi_bus_generate_proc_event(ibm->acpi->device,
2225 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002226 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002227
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002228 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002229 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002230 acpi_bus_generate_netlink_event(
2231 ibm->acpi->device->pnp.device_class,
2232 ibm->acpi->device->dev.bus_id,
2233 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002234 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002235 }
2236}
2237
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002238static void hotkey_suspend(pm_message_t state)
2239{
2240 /* Do these on suspend, we get the events on early resume! */
2241 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2242 hotkey_autosleep_ack = 0;
2243}
2244
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002245static void hotkey_resume(void)
2246{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002247 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002248 printk(TPACPI_ERR
2249 "error while trying to read hot key mask "
2250 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002251 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002252 hotkey_radio_sw_notify_change();
2253 hotkey_wakeup_reason_notify_change();
2254 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002255 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002256}
2257
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002258/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002259static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002261 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 int len = 0;
2263
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002264 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002265 len += sprintf(p + len, "status:\t\tnot supported\n");
2266 return len;
2267 }
2268
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002269 if (mutex_lock_interruptible(&hotkey_mutex))
2270 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002271 res = hotkey_status_get(&status);
2272 if (!res)
2273 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002274 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002275 if (res)
2276 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002279 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002280 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 len += sprintf(p + len,
2282 "commands:\tenable, disable, reset, <mask>\n");
2283 } else {
2284 len += sprintf(p + len, "mask:\t\tnot supported\n");
2285 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2286 }
2287
2288 return len;
2289}
2290
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002291static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002293 int res, status;
2294 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002297 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return -ENODEV;
2299
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002300 if (mutex_lock_interruptible(&hotkey_mutex))
2301 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002302
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002303 status = -1;
2304 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002305
2306 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 while ((cmd = next_cmd(&buf))) {
2308 if (strlencmp(cmd, "enable") == 0) {
2309 status = 1;
2310 } else if (strlencmp(cmd, "disable") == 0) {
2311 status = 0;
2312 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002313 status = hotkey_orig_status;
2314 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2316 /* mask set */
2317 } else if (sscanf(cmd, "%x", &mask) == 1) {
2318 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002319 } else {
2320 res = -EINVAL;
2321 goto errexit;
2322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002324 if (status != -1)
2325 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002327 if (!res && mask != hotkey_mask)
2328 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002330errexit:
2331 mutex_unlock(&hotkey_mutex);
2332 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002333}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002335static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002336 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002337 {"", 0},
2338};
2339
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002340static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002341 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002342 .notify = hotkey_notify,
2343 .handle = &hkey_handle,
2344 .type = ACPI_DEVICE_NOTIFY,
2345};
2346
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002347static struct ibm_struct hotkey_driver_data = {
2348 .name = "hotkey",
2349 .read = hotkey_read,
2350 .write = hotkey_write,
2351 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002352 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002353 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002354 .acpi = &ibm_hotkey_acpidriver,
2355};
2356
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002357/*************************************************************************
2358 * Bluetooth subdriver
2359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002361enum {
2362 /* ACPI GBDC/SBDC bits */
2363 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2364 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2365 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2366};
2367
2368static int bluetooth_get_radiosw(void);
2369static int bluetooth_set_radiosw(int radio_on);
2370
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002371/* sysfs bluetooth enable ---------------------------------------------- */
2372static ssize_t bluetooth_enable_show(struct device *dev,
2373 struct device_attribute *attr,
2374 char *buf)
2375{
2376 int status;
2377
2378 status = bluetooth_get_radiosw();
2379 if (status < 0)
2380 return status;
2381
2382 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2383}
2384
2385static ssize_t bluetooth_enable_store(struct device *dev,
2386 struct device_attribute *attr,
2387 const char *buf, size_t count)
2388{
2389 unsigned long t;
2390 int res;
2391
2392 if (parse_strtoul(buf, 1, &t))
2393 return -EINVAL;
2394
2395 res = bluetooth_set_radiosw(t);
2396
2397 return (res) ? res : count;
2398}
2399
2400static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002401 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002402 bluetooth_enable_show, bluetooth_enable_store);
2403
2404/* --------------------------------------------------------------------- */
2405
2406static struct attribute *bluetooth_attributes[] = {
2407 &dev_attr_bluetooth_enable.attr,
2408 NULL
2409};
2410
2411static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002412 .attrs = bluetooth_attributes,
2413};
2414
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002415static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002417 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002418 int status = 0;
2419
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002420 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2421
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002422 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002423
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002424 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2425 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002426 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002427 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002429 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2430 str_supported(tp_features.bluetooth),
2431 status);
2432
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002433 if (tp_features.bluetooth) {
2434 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2435 /* no bluetooth hardware present in system */
2436 tp_features.bluetooth = 0;
2437 dbg_printk(TPACPI_DBG_INIT,
2438 "bluetooth hardware not installed\n");
2439 } else {
2440 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2441 &bluetooth_attr_group);
2442 if (res)
2443 return res;
2444 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002445 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002446
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002447 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002450static void bluetooth_exit(void)
2451{
2452 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2453 &bluetooth_attr_group);
2454}
2455
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002456static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
2458 int status;
2459
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002460 if (!tp_features.bluetooth)
2461 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002463 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2464 return -EIO;
2465
2466 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2467}
2468
2469static int bluetooth_set_radiosw(int radio_on)
2470{
2471 int status;
2472
2473 if (!tp_features.bluetooth)
2474 return -ENODEV;
2475
2476 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2477 return -EIO;
2478 if (radio_on)
2479 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2480 else
2481 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2482 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2483 return -EIO;
2484
2485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002488/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002489static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002492 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002494 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002497 len += sprintf(p + len, "status:\t\t%s\n",
2498 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 len += sprintf(p + len, "commands:\tenable, disable\n");
2500 }
2501
2502 return len;
2503}
2504
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002505static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002509 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002510 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 while ((cmd = next_cmd(&buf))) {
2513 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002514 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002516 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 } else
2518 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return 0;
2522}
2523
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002524static struct ibm_struct bluetooth_driver_data = {
2525 .name = "bluetooth",
2526 .read = bluetooth_read,
2527 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002528 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002529};
2530
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002531/*************************************************************************
2532 * Wan subdriver
2533 */
2534
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002535enum {
2536 /* ACPI GWAN/SWAN bits */
2537 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2538 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2539 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2540};
2541
2542static int wan_get_radiosw(void);
2543static int wan_set_radiosw(int radio_on);
2544
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002545/* sysfs wan enable ---------------------------------------------------- */
2546static ssize_t wan_enable_show(struct device *dev,
2547 struct device_attribute *attr,
2548 char *buf)
2549{
2550 int status;
2551
2552 status = wan_get_radiosw();
2553 if (status < 0)
2554 return status;
2555
2556 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2557}
2558
2559static ssize_t wan_enable_store(struct device *dev,
2560 struct device_attribute *attr,
2561 const char *buf, size_t count)
2562{
2563 unsigned long t;
2564 int res;
2565
2566 if (parse_strtoul(buf, 1, &t))
2567 return -EINVAL;
2568
2569 res = wan_set_radiosw(t);
2570
2571 return (res) ? res : count;
2572}
2573
2574static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002575 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002576 wan_enable_show, wan_enable_store);
2577
2578/* --------------------------------------------------------------------- */
2579
2580static struct attribute *wan_attributes[] = {
2581 &dev_attr_wan_enable.attr,
2582 NULL
2583};
2584
2585static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002586 .attrs = wan_attributes,
2587};
2588
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002589static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002590{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002591 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002592 int status = 0;
2593
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002594 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2595
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002596 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002597
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002598 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002599 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002600
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002601 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2602 str_supported(tp_features.wan),
2603 status);
2604
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002605 if (tp_features.wan) {
2606 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2607 /* no wan hardware present in system */
2608 tp_features.wan = 0;
2609 dbg_printk(TPACPI_DBG_INIT,
2610 "wan hardware not installed\n");
2611 } else {
2612 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2613 &wan_attr_group);
2614 if (res)
2615 return res;
2616 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002617 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002618
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002619 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002620}
2621
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002622static void wan_exit(void)
2623{
2624 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2625 &wan_attr_group);
2626}
2627
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002628static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002629{
2630 int status;
2631
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002632 if (!tp_features.wan)
2633 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002634
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002635 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2636 return -EIO;
2637
2638 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2639}
2640
2641static int wan_set_radiosw(int radio_on)
2642{
2643 int status;
2644
2645 if (!tp_features.wan)
2646 return -ENODEV;
2647
2648 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2649 return -EIO;
2650 if (radio_on)
2651 status |= TP_ACPI_WANCARD_RADIOSSW;
2652 else
2653 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2654 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2655 return -EIO;
2656
2657 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002658}
2659
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002660/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002661static int wan_read(char *p)
2662{
2663 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002664 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002665
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002666 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002667 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002668 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002669 len += sprintf(p + len, "status:\t\t%s\n",
2670 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002671 len += sprintf(p + len, "commands:\tenable, disable\n");
2672 }
2673
2674 return len;
2675}
2676
2677static int wan_write(char *buf)
2678{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002679 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002680
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002681 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002682 return -ENODEV;
2683
2684 while ((cmd = next_cmd(&buf))) {
2685 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002686 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002687 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002688 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002689 } else
2690 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002691 }
2692
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002693 return 0;
2694}
2695
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002696static struct ibm_struct wan_driver_data = {
2697 .name = "wan",
2698 .read = wan_read,
2699 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002700 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002701 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002702};
2703
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002704/*************************************************************************
2705 * Video subdriver
2706 */
2707
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002708#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2709
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002710enum video_access_mode {
2711 TPACPI_VIDEO_NONE = 0,
2712 TPACPI_VIDEO_570, /* 570 */
2713 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2714 TPACPI_VIDEO_NEW, /* all others */
2715};
2716
2717enum { /* video status flags, based on VIDEO_570 */
2718 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2719 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2720 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2721};
2722
2723enum { /* TPACPI_VIDEO_570 constants */
2724 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2725 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2726 * video_status_flags */
2727 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2728 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2729};
2730
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002731static enum video_access_mode video_supported;
2732static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002733
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002734static int video_autosw_get(void);
2735static int video_autosw_set(int enable);
2736
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002737TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002738
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002739static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002741 int ivga;
2742
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002743 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2744
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002745 TPACPI_ACPIHANDLE_INIT(vid);
2746 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002747
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002748 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2749 /* G41, assume IVGA doesn't change */
2750 vid_handle = vid2_handle;
2751
2752 if (!vid_handle)
2753 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002754 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002755 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2756 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002757 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002758 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2759 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002760 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002761 else
2762 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002763 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002765 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2766 str_supported(video_supported != TPACPI_VIDEO_NONE),
2767 video_supported);
2768
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002769 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002772static void video_exit(void)
2773{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002774 dbg_printk(TPACPI_DBG_EXIT,
2775 "restoring original video autoswitch mode\n");
2776 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002777 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002778 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002779}
2780
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002781static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
2783 int status = 0;
2784 int i;
2785
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002786 switch (video_supported) {
2787 case TPACPI_VIDEO_570:
2788 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2789 TP_ACPI_VIDEO_570_PHSCMD))
2790 return -EIO;
2791 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2792 break;
2793 case TPACPI_VIDEO_770:
2794 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2795 return -EIO;
2796 if (i)
2797 status |= TP_ACPI_VIDEO_S_LCD;
2798 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2799 return -EIO;
2800 if (i)
2801 status |= TP_ACPI_VIDEO_S_CRT;
2802 break;
2803 case TPACPI_VIDEO_NEW:
2804 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2805 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2806 return -EIO;
2807 if (i)
2808 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002810 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2811 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2812 return -EIO;
2813 if (i)
2814 status |= TP_ACPI_VIDEO_S_LCD;
2815 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2816 return -EIO;
2817 if (i)
2818 status |= TP_ACPI_VIDEO_S_DVI;
2819 break;
2820 default:
2821 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 return status;
2825}
2826
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002827static int video_outputsw_set(int status)
2828{
2829 int autosw;
2830 int res = 0;
2831
2832 switch (video_supported) {
2833 case TPACPI_VIDEO_570:
2834 res = acpi_evalf(NULL, NULL,
2835 "\\_SB.PHS2", "vdd",
2836 TP_ACPI_VIDEO_570_PHS2CMD,
2837 status | TP_ACPI_VIDEO_570_PHS2SET);
2838 break;
2839 case TPACPI_VIDEO_770:
2840 autosw = video_autosw_get();
2841 if (autosw < 0)
2842 return autosw;
2843
2844 res = video_autosw_set(1);
2845 if (res)
2846 return res;
2847 res = acpi_evalf(vid_handle, NULL,
2848 "ASWT", "vdd", status * 0x100, 0);
2849 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002850 printk(TPACPI_ERR
2851 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002852 return -EIO;
2853 }
2854 break;
2855 case TPACPI_VIDEO_NEW:
2856 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002857 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002858 break;
2859 default:
2860 return -ENOSYS;
2861 }
2862
2863 return (res)? 0 : -EIO;
2864}
2865
2866static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002868 int autosw = 0;
2869
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002870 switch (video_supported) {
2871 case TPACPI_VIDEO_570:
2872 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2873 return -EIO;
2874 break;
2875 case TPACPI_VIDEO_770:
2876 case TPACPI_VIDEO_NEW:
2877 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2878 return -EIO;
2879 break;
2880 default:
2881 return -ENOSYS;
2882 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002883
2884 return autosw & 1;
2885}
2886
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002887static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002888{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002889 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002890 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002891 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002892}
2893
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002894static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002895{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002896 int autosw = video_autosw_get();
2897 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002898
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002899 if (autosw < 0)
2900 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002901
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002902 switch (video_supported) {
2903 case TPACPI_VIDEO_570:
2904 res = video_autosw_set(1);
2905 if (res)
2906 return res;
2907 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2908 break;
2909 case TPACPI_VIDEO_770:
2910 case TPACPI_VIDEO_NEW:
2911 res = video_autosw_set(1);
2912 if (res)
2913 return res;
2914 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2915 break;
2916 default:
2917 return -ENOSYS;
2918 }
2919 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002920 printk(TPACPI_ERR
2921 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002922 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002923 }
2924
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002925 return (res)? 0 : -EIO;
2926}
2927
2928static int video_expand_toggle(void)
2929{
2930 switch (video_supported) {
2931 case TPACPI_VIDEO_570:
2932 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2933 0 : -EIO;
2934 case TPACPI_VIDEO_770:
2935 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2936 0 : -EIO;
2937 case TPACPI_VIDEO_NEW:
2938 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2939 0 : -EIO;
2940 default:
2941 return -ENOSYS;
2942 }
2943 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002944}
2945
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002946static int video_read(char *p)
2947{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002948 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002949 int len = 0;
2950
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002951 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002952 len += sprintf(p + len, "status:\t\tnot supported\n");
2953 return len;
2954 }
2955
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002956 status = video_outputsw_get();
2957 if (status < 0)
2958 return status;
2959
2960 autosw = video_autosw_get();
2961 if (autosw < 0)
2962 return autosw;
2963
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002964 len += sprintf(p + len, "status:\t\tsupported\n");
2965 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2966 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002967 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002968 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2969 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2970 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2971 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002972 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002973 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2974 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2975 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2976
2977 return len;
2978}
2979
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002980static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 char *cmd;
2983 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002984 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002986 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002987 return -ENODEV;
2988
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002989 enable = 0;
2990 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 while ((cmd = next_cmd(&buf))) {
2993 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002994 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002996 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002998 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003000 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003001 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003002 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003003 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003004 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003005 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003006 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003008 res = video_autosw_set(1);
3009 if (res)
3010 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003012 res = video_autosw_set(0);
3013 if (res)
3014 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003016 res = video_outputsw_cycle();
3017 if (res)
3018 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003020 res = video_expand_toggle();
3021 if (res)
3022 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 } else
3024 return -EINVAL;
3025 }
3026
3027 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003028 status = video_outputsw_get();
3029 if (status < 0)
3030 return status;
3031 res = video_outputsw_set((status & ~disable) | enable);
3032 if (res)
3033 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
3035
3036 return 0;
3037}
3038
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003039static struct ibm_struct video_driver_data = {
3040 .name = "video",
3041 .read = video_read,
3042 .write = video_write,
3043 .exit = video_exit,
3044};
3045
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003046#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3047
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003048/*************************************************************************
3049 * Light (thinklight) subdriver
3050 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003052TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3053TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003054
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003055static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003057 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3058
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003059 TPACPI_ACPIHANDLE_INIT(ledb);
3060 TPACPI_ACPIHANDLE_INIT(lght);
3061 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003062
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003063 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003064 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003065
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003066 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003067 /* light status not supported on
3068 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003069 tp_features.light_status =
3070 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003072 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003073 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003074
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003075 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003078static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
3080 int len = 0;
3081 int status = 0;
3082
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003083 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003084 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003085 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003086 len += sprintf(p + len, "status:\t\tunknown\n");
3087 len += sprintf(p + len, "commands:\ton, off\n");
3088 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3090 return -EIO;
3091 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003092 len += sprintf(p + len, "commands:\ton, off\n");
3093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 return len;
3096}
3097
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003098static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
3100 int cmos_cmd, lght_cmd;
3101 char *cmd;
3102 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003103
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003104 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003105 return -ENODEV;
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 while ((cmd = next_cmd(&buf))) {
3108 if (strlencmp(cmd, "on") == 0) {
3109 cmos_cmd = 0x0c;
3110 lght_cmd = 1;
3111 } else if (strlencmp(cmd, "off") == 0) {
3112 cmos_cmd = 0x0d;
3113 lght_cmd = 0;
3114 } else
3115 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003118 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3119 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (!success)
3121 return -EIO;
3122 }
3123
3124 return 0;
3125}
3126
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003127static struct ibm_struct light_driver_data = {
3128 .name = "light",
3129 .read = light_read,
3130 .write = light_write,
3131};
3132
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003133/*************************************************************************
3134 * Dock subdriver
3135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003137#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003138
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003139static void dock_notify(struct ibm_struct *ibm, u32 event);
3140static int dock_read(char *p);
3141static int dock_write(char *buf);
3142
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003143TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003144 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3145 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3146 "\\_SB.PCI.ISA.SLCE", /* 570 */
3147 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3148
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003149/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003150TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003151
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003152static const struct acpi_device_id ibm_pci_device_ids[] = {
3153 {PCI_ROOT_HID_STRING, 0},
3154 {"", 0},
3155};
3156
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003157static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3158 {
3159 .notify = dock_notify,
3160 .handle = &dock_handle,
3161 .type = ACPI_SYSTEM_NOTIFY,
3162 },
3163 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003164 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3165 * We just use it to get notifications of dock hotplug
3166 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003167 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003168 .notify = dock_notify,
3169 .handle = &pci_handle,
3170 .type = ACPI_SYSTEM_NOTIFY,
3171 },
3172};
3173
3174static struct ibm_struct dock_driver_data[2] = {
3175 {
3176 .name = "dock",
3177 .read = dock_read,
3178 .write = dock_write,
3179 .acpi = &ibm_dock_acpidriver[0],
3180 },
3181 {
3182 .name = "dock",
3183 .acpi = &ibm_dock_acpidriver[1],
3184 },
3185};
3186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187#define dock_docked() (_sta(dock_handle) & 1)
3188
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003189static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003190{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003191 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3192
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003193 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003194
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003195 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3196 str_supported(dock_handle != NULL));
3197
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003198 return (dock_handle)? 0 : 1;
3199}
3200
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003201static int __init dock_init2(struct ibm_init_struct *iibm)
3202{
3203 int dock2_needed;
3204
3205 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3206
3207 if (dock_driver_data[0].flags.acpi_driver_registered &&
3208 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003209 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003210 dock2_needed = (pci_handle != NULL);
3211 vdbg_printk(TPACPI_DBG_INIT,
3212 "dock PCI handler for the TP 570 is %s\n",
3213 str_supported(dock2_needed));
3214 } else {
3215 vdbg_printk(TPACPI_DBG_INIT,
3216 "dock subdriver part 2 not required\n");
3217 dock2_needed = 0;
3218 }
3219
3220 return (dock2_needed)? 0 : 1;
3221}
3222
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003223static void dock_notify(struct ibm_struct *ibm, u32 event)
3224{
3225 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003226 int pci = ibm->acpi->hid && ibm->acpi->device &&
3227 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003228 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003229
3230 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003231 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003232 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003233 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003234 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003235 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003236 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003237 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003238 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003239 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003240 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003241 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003242 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003243 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003244 }
Len Brown14e04fb2007-08-23 15:20:26 -04003245 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003246 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3247 ibm->acpi->device->dev.bus_id,
3248 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003249}
3250
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003251static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
3253 int len = 0;
3254 int docked = dock_docked();
3255
3256 if (!dock_handle)
3257 len += sprintf(p + len, "status:\t\tnot supported\n");
3258 else if (!docked)
3259 len += sprintf(p + len, "status:\t\tundocked\n");
3260 else {
3261 len += sprintf(p + len, "status:\t\tdocked\n");
3262 len += sprintf(p + len, "commands:\tdock, undock\n");
3263 }
3264
3265 return len;
3266}
3267
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003268static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 char *cmd;
3271
3272 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003273 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 while ((cmd = next_cmd(&buf))) {
3276 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003277 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3278 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return -EIO;
3280 } else if (strlencmp(cmd, "dock") == 0) {
3281 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3282 return -EIO;
3283 } else
3284 return -EINVAL;
3285 }
3286
3287 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003290#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003292/*************************************************************************
3293 * Bay subdriver
3294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003296#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003297
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003298TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003299 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3300 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3301 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3302 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003303TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003304 "_EJ0", /* all others */
3305 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003306TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003307 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3308 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003309TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003310 "_EJ0", /* 770x */
3311 ); /* all others */
3312
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003313static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003315 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3316
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003317 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003318 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003319 TPACPI_ACPIHANDLE_INIT(bay_ej);
3320 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003321 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003322 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003323
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003324 tp_features.bay_status = bay_handle &&
3325 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3326 tp_features.bay_status2 = bay2_handle &&
3327 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003328
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003329 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3330 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3331 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3332 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003334 vdbg_printk(TPACPI_DBG_INIT,
3335 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003336 str_supported(tp_features.bay_status),
3337 str_supported(tp_features.bay_eject),
3338 str_supported(tp_features.bay_status2),
3339 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003340
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003341 return (tp_features.bay_status || tp_features.bay_eject ||
3342 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003345static void bay_notify(struct ibm_struct *ibm, u32 event)
3346{
Len Brown14e04fb2007-08-23 15:20:26 -04003347 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003348 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3349 ibm->acpi->device->dev.bus_id,
3350 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003351}
3352
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003353#define bay_occupied(b) (_sta(b##_handle) & 1)
3354
3355static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
3357 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003358 int occupied = bay_occupied(bay);
3359 int occupied2 = bay_occupied(bay2);
3360 int eject, eject2;
3361
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003362 len += sprintf(p + len, "status:\t\t%s\n",
3363 tp_features.bay_status ?
3364 (occupied ? "occupied" : "unoccupied") :
3365 "not supported");
3366 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003367 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3368 "occupied" : "unoccupied");
3369
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003370 eject = tp_features.bay_eject && occupied;
3371 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003372
3373 if (eject && eject2)
3374 len += sprintf(p + len, "commands:\teject, eject2\n");
3375 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003377 else if (eject2)
3378 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
3380 return len;
3381}
3382
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003383static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
3385 char *cmd;
3386
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003387 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003388 return -ENODEV;
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003391 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003392 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3393 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003394 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003395 strlencmp(cmd, "eject2") == 0) {
3396 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return -EIO;
3398 } else
3399 return -EINVAL;
3400 }
3401
3402 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003403}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003404
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003405static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3406 .notify = bay_notify,
3407 .handle = &bay_handle,
3408 .type = ACPI_SYSTEM_NOTIFY,
3409};
3410
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003411static struct ibm_struct bay_driver_data = {
3412 .name = "bay",
3413 .read = bay_read,
3414 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003415 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003416};
3417
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003418#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003420/*************************************************************************
3421 * CMOS subdriver
3422 */
3423
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003424/* sysfs cmos_command -------------------------------------------------- */
3425static ssize_t cmos_command_store(struct device *dev,
3426 struct device_attribute *attr,
3427 const char *buf, size_t count)
3428{
3429 unsigned long cmos_cmd;
3430 int res;
3431
3432 if (parse_strtoul(buf, 21, &cmos_cmd))
3433 return -EINVAL;
3434
3435 res = issue_thinkpad_cmos_command(cmos_cmd);
3436 return (res)? res : count;
3437}
3438
3439static struct device_attribute dev_attr_cmos_command =
3440 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3441
3442/* --------------------------------------------------------------------- */
3443
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003444static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003445{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003446 int res;
3447
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003448 vdbg_printk(TPACPI_DBG_INIT,
3449 "initializing cmos commands subdriver\n");
3450
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003451 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003452
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003453 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3454 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003455
3456 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3457 if (res)
3458 return res;
3459
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003460 return (cmos_handle)? 0 : 1;
3461}
3462
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003463static void cmos_exit(void)
3464{
3465 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3466}
3467
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003468static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
3470 int len = 0;
3471
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003472 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3473 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 if (!cmos_handle)
3475 len += sprintf(p + len, "status:\t\tnot supported\n");
3476 else {
3477 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003478 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 }
3480
3481 return len;
3482}
3483
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003484static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
3486 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003487 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
3489 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003490 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3491 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 /* cmos_cmd set */
3493 } else
3494 return -EINVAL;
3495
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003496 res = issue_thinkpad_cmos_command(cmos_cmd);
3497 if (res)
3498 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 }
3500
3501 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003502}
3503
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003504static struct ibm_struct cmos_driver_data = {
3505 .name = "cmos",
3506 .read = cmos_read,
3507 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003508 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003509};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003510
3511/*************************************************************************
3512 * LED subdriver
3513 */
3514
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003515enum led_access_mode {
3516 TPACPI_LED_NONE = 0,
3517 TPACPI_LED_570, /* 570 */
3518 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3519 TPACPI_LED_NEW, /* all others */
3520};
3521
3522enum { /* For TPACPI_LED_OLD */
3523 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3524 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3525 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3526};
3527
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003528static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003529
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003530TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003531 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3532 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003533 "LED", /* all others */
3534 ); /* R30, R31 */
3535
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003536static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003537{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003538 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3539
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003540 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003541
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003542 if (!led_handle)
3543 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003544 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003545 else if (strlencmp(led_path, "SLED") == 0)
3546 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003547 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003548 else if (strlencmp(led_path, "SYSL") == 0)
3549 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003550 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003551 else
3552 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003553 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003554
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003555 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3556 str_supported(led_supported), led_supported);
3557
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003558 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003559}
3560
3561#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3562
3563static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
3565 int len = 0;
3566
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003567 if (!led_supported) {
3568 len += sprintf(p + len, "status:\t\tnot supported\n");
3569 return len;
3570 }
3571 len += sprintf(p + len, "status:\t\tsupported\n");
3572
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003573 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003574 /* 570 */
3575 int i, status;
3576 for (i = 0; i < 8; i++) {
3577 if (!acpi_evalf(ec_handle,
3578 &status, "GLED", "dd", 1 << i))
3579 return -EIO;
3580 len += sprintf(p + len, "%d:\t\t%s\n",
3581 i, led_status(status));
3582 }
3583 }
3584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003586 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 return len;
3589}
3590
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003591/* off, on, blink */
3592static const int led_sled_arg1[] = { 0, 1, 3 };
3593static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3594static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3595static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3596
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003597static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
3599 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003600 int led, ind, ret;
3601
3602 if (!led_supported)
3603 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003606 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 return -EINVAL;
3608
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003609 if (strstr(cmd, "off")) {
3610 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003612 ind = 1;
3613 } else if (strstr(cmd, "blink")) {
3614 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 } else
3616 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003617
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003618 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003619 /* 570 */
3620 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003622 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003624 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003625 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3626 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003627 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003628 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003629 ret = ec_write(TPACPI_LED_EC_HLBL,
3630 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003631 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003632 ret = ec_write(TPACPI_LED_EC_HLCL,
3633 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003634 if (ret < 0)
3635 return ret;
3636 } else {
3637 /* all others */
3638 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3639 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 }
3643
3644 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003645}
3646
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003647static struct ibm_struct led_driver_data = {
3648 .name = "led",
3649 .read = led_read,
3650 .write = led_write,
3651};
3652
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003653/*************************************************************************
3654 * Beep subdriver
3655 */
3656
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003657TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003658
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003659static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003660{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003661 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3662
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003663 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003664
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003665 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3666 str_supported(beep_handle != NULL));
3667
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003668 return (beep_handle)? 0 : 1;
3669}
3670
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003671static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
3673 int len = 0;
3674
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003675 if (!beep_handle)
3676 len += sprintf(p + len, "status:\t\tnot supported\n");
3677 else {
3678 len += sprintf(p + len, "status:\t\tsupported\n");
3679 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
3682 return len;
3683}
3684
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003685static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
3687 char *cmd;
3688 int beep_cmd;
3689
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003690 if (!beep_handle)
3691 return -ENODEV;
3692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003694 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3695 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 /* beep_cmd set */
3697 } else
3698 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003699 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 return -EIO;
3701 }
3702
3703 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003704}
3705
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003706static struct ibm_struct beep_driver_data = {
3707 .name = "beep",
3708 .read = beep_read,
3709 .write = beep_write,
3710};
3711
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003712/*************************************************************************
3713 * Thermal subdriver
3714 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003715
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003716enum thermal_access_mode {
3717 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3718 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3719 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3720 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3721 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3722};
3723
3724enum { /* TPACPI_THERMAL_TPEC_* */
3725 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3726 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3727 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3728};
3729
3730#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3731struct ibm_thermal_sensors_struct {
3732 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3733};
3734
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003735static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003736
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003737/* idx is zero-based */
3738static int thermal_get_sensor(int idx, s32 *value)
3739{
3740 int t;
3741 s8 tmp;
3742 char tmpi[5];
3743
3744 t = TP_EC_THERMAL_TMP0;
3745
3746 switch (thermal_read_mode) {
3747#if TPACPI_MAX_THERMAL_SENSORS >= 16
3748 case TPACPI_THERMAL_TPEC_16:
3749 if (idx >= 8 && idx <= 15) {
3750 t = TP_EC_THERMAL_TMP8;
3751 idx -= 8;
3752 }
3753 /* fallthrough */
3754#endif
3755 case TPACPI_THERMAL_TPEC_8:
3756 if (idx <= 7) {
3757 if (!acpi_ec_read(t + idx, &tmp))
3758 return -EIO;
3759 *value = tmp * 1000;
3760 return 0;
3761 }
3762 break;
3763
3764 case TPACPI_THERMAL_ACPI_UPDT:
3765 if (idx <= 7) {
3766 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3767 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3768 return -EIO;
3769 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3770 return -EIO;
3771 *value = (t - 2732) * 100;
3772 return 0;
3773 }
3774 break;
3775
3776 case TPACPI_THERMAL_ACPI_TMP07:
3777 if (idx <= 7) {
3778 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3779 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3780 return -EIO;
3781 if (t > 127 || t < -127)
3782 t = TP_EC_THERMAL_TMP_NA;
3783 *value = t * 1000;
3784 return 0;
3785 }
3786 break;
3787
3788 case TPACPI_THERMAL_NONE:
3789 default:
3790 return -ENOSYS;
3791 }
3792
3793 return -EINVAL;
3794}
3795
3796static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3797{
3798 int res, i;
3799 int n;
3800
3801 n = 8;
3802 i = 0;
3803
3804 if (!s)
3805 return -EINVAL;
3806
3807 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3808 n = 16;
3809
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003810 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003811 res = thermal_get_sensor(i, &s->temp[i]);
3812 if (res)
3813 return res;
3814 }
3815
3816 return n;
3817}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003818
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003819/* sysfs temp##_input -------------------------------------------------- */
3820
3821static ssize_t thermal_temp_input_show(struct device *dev,
3822 struct device_attribute *attr,
3823 char *buf)
3824{
3825 struct sensor_device_attribute *sensor_attr =
3826 to_sensor_dev_attr(attr);
3827 int idx = sensor_attr->index;
3828 s32 value;
3829 int res;
3830
3831 res = thermal_get_sensor(idx, &value);
3832 if (res)
3833 return res;
3834 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3835 return -ENXIO;
3836
3837 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3838}
3839
3840#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003841 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3842 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003843
3844static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3845 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3846 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3847 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3848 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3849 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3850 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3851 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3852 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3853 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3854 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3855 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3856 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3857 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3858 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3859 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3860 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3861};
3862
3863#define THERMAL_ATTRS(X) \
3864 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3865
3866static struct attribute *thermal_temp_input_attr[] = {
3867 THERMAL_ATTRS(8),
3868 THERMAL_ATTRS(9),
3869 THERMAL_ATTRS(10),
3870 THERMAL_ATTRS(11),
3871 THERMAL_ATTRS(12),
3872 THERMAL_ATTRS(13),
3873 THERMAL_ATTRS(14),
3874 THERMAL_ATTRS(15),
3875 THERMAL_ATTRS(0),
3876 THERMAL_ATTRS(1),
3877 THERMAL_ATTRS(2),
3878 THERMAL_ATTRS(3),
3879 THERMAL_ATTRS(4),
3880 THERMAL_ATTRS(5),
3881 THERMAL_ATTRS(6),
3882 THERMAL_ATTRS(7),
3883 NULL
3884};
3885
3886static const struct attribute_group thermal_temp_input16_group = {
3887 .attrs = thermal_temp_input_attr
3888};
3889
3890static const struct attribute_group thermal_temp_input8_group = {
3891 .attrs = &thermal_temp_input_attr[8]
3892};
3893
3894#undef THERMAL_SENSOR_ATTR_TEMP
3895#undef THERMAL_ATTRS
3896
3897/* --------------------------------------------------------------------- */
3898
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003899static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003900{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003901 u8 t, ta1, ta2;
3902 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003903 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003904 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003905
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003906 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3907
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003908 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003909
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003910 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003911 /*
3912 * Direct EC access mode: sensors at registers
3913 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3914 * non-implemented, thermal sensors return 0x80 when
3915 * not available
3916 */
3917
3918 ta1 = ta2 = 0;
3919 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003920 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003921 ta1 |= t;
3922 } else {
3923 ta1 = 0;
3924 break;
3925 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003926 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003927 ta2 |= t;
3928 } else {
3929 ta1 = 0;
3930 break;
3931 }
3932 }
3933 if (ta1 == 0) {
3934 /* This is sheer paranoia, but we handle it anyway */
3935 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003936 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003937 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003938 "falling back to ACPI TMPx access "
3939 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003940 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003941 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003942 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003943 "ThinkPad ACPI EC access misbehaving, "
3944 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003945 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003946 }
3947 } else {
3948 thermal_read_mode =
3949 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003950 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003951 }
3952 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003953 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3954 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003955 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003956 } else {
3957 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003958 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003959 }
3960 } else {
3961 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003962 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003963 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003964
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003965 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3966 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3967 thermal_read_mode);
3968
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003969 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003970 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003971 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003972 &thermal_temp_input16_group);
3973 if (res)
3974 return res;
3975 break;
3976 case TPACPI_THERMAL_TPEC_8:
3977 case TPACPI_THERMAL_ACPI_TMP07:
3978 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003979 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003980 &thermal_temp_input8_group);
3981 if (res)
3982 return res;
3983 break;
3984 case TPACPI_THERMAL_NONE:
3985 default:
3986 return 1;
3987 }
3988
3989 return 0;
3990}
3991
3992static void thermal_exit(void)
3993{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003994 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003995 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003996 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003997 &thermal_temp_input16_group);
3998 break;
3999 case TPACPI_THERMAL_TPEC_8:
4000 case TPACPI_THERMAL_ACPI_TMP07:
4001 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004002 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004003 &thermal_temp_input16_group);
4004 break;
4005 case TPACPI_THERMAL_NONE:
4006 default:
4007 break;
4008 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004009}
4010
4011static int thermal_read(char *p)
4012{
4013 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004014 int n, i;
4015 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004016
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004017 n = thermal_get_sensors(&t);
4018 if (unlikely(n < 0))
4019 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004020
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004021 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004022
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004023 if (n > 0) {
4024 for (i = 0; i < (n - 1); i++)
4025 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4026 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4027 } else
4028 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004029
4030 return len;
4031}
4032
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004033static struct ibm_struct thermal_driver_data = {
4034 .name = "thermal",
4035 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004036 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004037};
4038
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004039/*************************************************************************
4040 * EC Dump subdriver
4041 */
4042
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004043static u8 ecdump_regs[256];
4044
4045static int ecdump_read(char *p)
4046{
4047 int len = 0;
4048 int i, j;
4049 u8 v;
4050
4051 len += sprintf(p + len, "EC "
4052 " +00 +01 +02 +03 +04 +05 +06 +07"
4053 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4054 for (i = 0; i < 256; i += 16) {
4055 len += sprintf(p + len, "EC 0x%02x:", i);
4056 for (j = 0; j < 16; j++) {
4057 if (!acpi_ec_read(i + j, &v))
4058 break;
4059 if (v != ecdump_regs[i + j])
4060 len += sprintf(p + len, " *%02x", v);
4061 else
4062 len += sprintf(p + len, " %02x", v);
4063 ecdump_regs[i + j] = v;
4064 }
4065 len += sprintf(p + len, "\n");
4066 if (j != 16)
4067 break;
4068 }
4069
4070 /* These are way too dangerous to advertise openly... */
4071#if 0
4072 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4073 " (<offset> is 00-ff, <value> is 00-ff)\n");
4074 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4075 " (<offset> is 00-ff, <value> is 0-255)\n");
4076#endif
4077 return len;
4078}
4079
4080static int ecdump_write(char *buf)
4081{
4082 char *cmd;
4083 int i, v;
4084
4085 while ((cmd = next_cmd(&buf))) {
4086 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4087 /* i and v set */
4088 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4089 /* i and v set */
4090 } else
4091 return -EINVAL;
4092 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4093 if (!acpi_ec_write(i, v))
4094 return -EIO;
4095 } else
4096 return -EINVAL;
4097 }
4098
4099 return 0;
4100}
4101
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004102static struct ibm_struct ecdump_driver_data = {
4103 .name = "ecdump",
4104 .read = ecdump_read,
4105 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004106 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004107};
4108
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004109/*************************************************************************
4110 * Backlight/brightness subdriver
4111 */
Holger Macht8acb0252006-10-20 14:30:28 -07004112
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004113#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4114
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004115static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004116static int brightness_offset = 0x31;
4117static int brightness_mode;
4118static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4119
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004120static struct mutex brightness_mutex;
4121
4122/*
4123 * ThinkPads can read brightness from two places: EC 0x31, or
4124 * CMOS NVRAM byte 0x5E, bits 0-3.
4125 */
4126static int brightness_get(struct backlight_device *bd)
4127{
4128 u8 lec = 0, lcmos = 0, level = 0;
4129
4130 if (brightness_mode & 1) {
4131 if (!acpi_ec_read(brightness_offset, &lec))
4132 return -EIO;
4133 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4134 level = lec;
4135 };
4136 if (brightness_mode & 2) {
4137 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4138 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4139 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4140 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4141 level = lcmos;
4142 }
4143
4144 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004145 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004146 "CMOS NVRAM (%u) and EC (%u) do not agree "
4147 "on display brightness level\n",
4148 (unsigned int) lcmos,
4149 (unsigned int) lec);
4150 return -EIO;
4151 }
4152
4153 return level;
4154}
4155
4156/* May return EINTR which can always be mapped to ERESTARTSYS */
4157static int brightness_set(int value)
4158{
4159 int cmos_cmd, inc, i, res;
4160 int current_value;
4161
4162 if (value > ((tp_features.bright_16levels)? 15 : 7))
4163 return -EINVAL;
4164
4165 res = mutex_lock_interruptible(&brightness_mutex);
4166 if (res < 0)
4167 return res;
4168
4169 current_value = brightness_get(NULL);
4170 if (current_value < 0) {
4171 res = current_value;
4172 goto errout;
4173 }
4174
4175 cmos_cmd = value > current_value ?
4176 TP_CMOS_BRIGHTNESS_UP :
4177 TP_CMOS_BRIGHTNESS_DOWN;
4178 inc = (value > current_value)? 1 : -1;
4179
4180 res = 0;
4181 for (i = current_value; i != value; i += inc) {
4182 if ((brightness_mode & 2) &&
4183 issue_thinkpad_cmos_command(cmos_cmd)) {
4184 res = -EIO;
4185 goto errout;
4186 }
4187 if ((brightness_mode & 1) &&
4188 !acpi_ec_write(brightness_offset, i + inc)) {
4189 res = -EIO;
4190 goto errout;;
4191 }
4192 }
4193
4194errout:
4195 mutex_unlock(&brightness_mutex);
4196 return res;
4197}
4198
4199/* sysfs backlight class ----------------------------------------------- */
4200
4201static int brightness_update_status(struct backlight_device *bd)
4202{
4203 /* it is the backlight class's job (caller) to handle
4204 * EINTR and other errors properly */
4205 return brightness_set(
4206 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4207 bd->props.power == FB_BLANK_UNBLANK) ?
4208 bd->props.brightness : 0);
4209}
Holger Macht8acb0252006-10-20 14:30:28 -07004210
Richard Purdie599a52d2007-02-10 23:07:48 +00004211static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004212 .get_brightness = brightness_get,
4213 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004214};
4215
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004216/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004217
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004218static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4219{
4220 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4221 union acpi_object *obj;
4222 int rc;
4223
4224 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4225 obj = (union acpi_object *)buffer.pointer;
4226 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004227 printk(TPACPI_ERR "Unknown BCLL data, "
4228 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004229 rc = 0;
4230 } else {
4231 rc = obj->package.count;
4232 }
4233 } else {
4234 return 0;
4235 }
4236
4237 kfree(buffer.pointer);
4238 return rc;
4239}
4240
4241static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4242 void *context, void **rv)
4243{
4244 char name[ACPI_PATH_SEGMENT_LENGTH];
4245 struct acpi_buffer buffer = { sizeof(name), &name };
4246
4247 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4248 !strncmp("BCLL", name, sizeof(name) - 1)) {
4249 if (tpacpi_query_bcll_levels(handle) == 16) {
4250 *rv = handle;
4251 return AE_CTRL_TERMINATE;
4252 } else {
4253 return AE_OK;
4254 }
4255 } else {
4256 return AE_OK;
4257 }
4258}
4259
4260static int __init brightness_check_levels(void)
4261{
4262 int status;
4263 void *found_node = NULL;
4264
4265 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004266 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004267 }
4268 if (!vid_handle)
4269 return 0;
4270
4271 /* Search for a BCLL package with 16 levels */
4272 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004273 brightness_find_bcll, NULL,
4274 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004275
4276 return (ACPI_SUCCESS(status) && found_node != NULL);
4277}
4278
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004279static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4280 void *context, void **rv)
4281{
4282 char name[ACPI_PATH_SEGMENT_LENGTH];
4283 struct acpi_buffer buffer = { sizeof(name), &name };
4284
4285 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4286 !strncmp("_BCL", name, sizeof(name) - 1)) {
4287 *rv = handle;
4288 return AE_CTRL_TERMINATE;
4289 } else {
4290 return AE_OK;
4291 }
4292}
4293
4294static int __init brightness_check_std_acpi_support(void)
4295{
4296 int status;
4297 void *found_node = NULL;
4298
4299 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004300 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004301 }
4302 if (!vid_handle)
4303 return 0;
4304
4305 /* Search for a _BCL method, but don't execute it */
4306 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004307 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004308
4309 return (ACPI_SUCCESS(status) && found_node != NULL);
4310}
4311
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004312static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004313{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004314 int b;
4315
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004316 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4317
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004318 mutex_init(&brightness_mutex);
4319
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004320 if (!brightness_enable) {
4321 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004322 "brightness support disabled by "
4323 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004324 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004325 } else if (brightness_enable > 1) {
4326 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004327 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004328 "standard ACPI backlight interface "
4329 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004330 return 1;
4331 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004332 }
4333
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004334 if (!brightness_mode) {
4335 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4336 brightness_mode = 2;
4337 else
4338 brightness_mode = 3;
4339
4340 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4341 brightness_mode);
4342 }
4343
4344 if (brightness_mode > 3)
4345 return -EINVAL;
4346
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004347 tp_features.bright_16levels =
4348 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4349 brightness_check_levels();
4350
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004351 b = brightness_get(NULL);
4352 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004353 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004354
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004355 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004356 printk(TPACPI_INFO
4357 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004358
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004359 ibm_backlight_device = backlight_device_register(
4360 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4361 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004362 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004363 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004364 return PTR_ERR(ibm_backlight_device);
4365 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004366 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004367
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004368 ibm_backlight_device->props.max_brightness =
4369 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004370 ibm_backlight_device->props.brightness = b;
4371 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004372
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004373 return 0;
4374}
4375
4376static void brightness_exit(void)
4377{
4378 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004379 vdbg_printk(TPACPI_DBG_EXIT,
4380 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004381 backlight_device_unregister(ibm_backlight_device);
4382 ibm_backlight_device = NULL;
4383 }
4384}
4385
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004386static int brightness_read(char *p)
4387{
4388 int len = 0;
4389 int level;
4390
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004391 level = brightness_get(NULL);
4392 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004393 len += sprintf(p + len, "level:\t\tunreadable\n");
4394 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004395 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004396 len += sprintf(p + len, "commands:\tup, down\n");
4397 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004398 " (<level> is 0-%d)\n",
4399 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004400 }
4401
4402 return len;
4403}
4404
4405static int brightness_write(char *buf)
4406{
4407 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004408 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004409 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004410 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004411
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004412 level = brightness_get(NULL);
4413 if (level < 0)
4414 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004415
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004416 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004417 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004418 if (level < max_level)
4419 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004420 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004421 if (level > 0)
4422 level--;
4423 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4424 level >= 0 && level <= max_level) {
4425 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004426 } else
4427 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004428 }
4429
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004430 /*
4431 * Now we know what the final level should be, so we try to set it.
4432 * Doing it this way makes the syscall restartable in case of EINTR
4433 */
4434 rc = brightness_set(level);
4435 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004436}
4437
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004438static struct ibm_struct brightness_driver_data = {
4439 .name = "brightness",
4440 .read = brightness_read,
4441 .write = brightness_write,
4442 .exit = brightness_exit,
4443};
4444
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004445/*************************************************************************
4446 * Volume subdriver
4447 */
4448
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004449static int volume_offset = 0x30;
4450
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004451static int volume_read(char *p)
4452{
4453 int len = 0;
4454 u8 level;
4455
4456 if (!acpi_ec_read(volume_offset, &level)) {
4457 len += sprintf(p + len, "level:\t\tunreadable\n");
4458 } else {
4459 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4460 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4461 len += sprintf(p + len, "commands:\tup, down, mute\n");
4462 len += sprintf(p + len, "commands:\tlevel <level>"
4463 " (<level> is 0-15)\n");
4464 }
4465
4466 return len;
4467}
4468
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004469static int volume_write(char *buf)
4470{
4471 int cmos_cmd, inc, i;
4472 u8 level, mute;
4473 int new_level, new_mute;
4474 char *cmd;
4475
4476 while ((cmd = next_cmd(&buf))) {
4477 if (!acpi_ec_read(volume_offset, &level))
4478 return -EIO;
4479 new_mute = mute = level & 0x40;
4480 new_level = level = level & 0xf;
4481
4482 if (strlencmp(cmd, "up") == 0) {
4483 if (mute)
4484 new_mute = 0;
4485 else
4486 new_level = level == 15 ? 15 : level + 1;
4487 } else if (strlencmp(cmd, "down") == 0) {
4488 if (mute)
4489 new_mute = 0;
4490 else
4491 new_level = level == 0 ? 0 : level - 1;
4492 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4493 new_level >= 0 && new_level <= 15) {
4494 /* new_level set */
4495 } else if (strlencmp(cmd, "mute") == 0) {
4496 new_mute = 0x40;
4497 } else
4498 return -EINVAL;
4499
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004500 if (new_level != level) {
4501 /* mute doesn't change */
4502
4503 cmos_cmd = (new_level > level) ?
4504 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004505 inc = new_level > level ? 1 : -1;
4506
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004507 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004508 !acpi_ec_write(volume_offset, level)))
4509 return -EIO;
4510
4511 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004512 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004513 !acpi_ec_write(volume_offset, i + inc))
4514 return -EIO;
4515
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004516 if (mute &&
4517 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4518 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004519 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004520 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004521 }
4522
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004523 if (new_mute != mute) {
4524 /* level doesn't change */
4525
4526 cmos_cmd = (new_mute) ?
4527 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004528
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004529 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004530 !acpi_ec_write(volume_offset, level + new_mute))
4531 return -EIO;
4532 }
4533 }
4534
4535 return 0;
4536}
4537
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004538static struct ibm_struct volume_driver_data = {
4539 .name = "volume",
4540 .read = volume_read,
4541 .write = volume_write,
4542};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004543
4544/*************************************************************************
4545 * Fan subdriver
4546 */
4547
4548/*
4549 * FAN ACCESS MODES
4550 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004551 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004552 * ACPI GFAN method: returns fan level
4553 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004554 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004555 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004556 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004557 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004558 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4559 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004560 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4561 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004562 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004563 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004564 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4565 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004566 *
4567 * Fan speed changes of any sort (including those caused by the
4568 * disengaged mode) are usually done slowly by the firmware as the
4569 * maximum ammount of fan duty cycle change per second seems to be
4570 * limited.
4571 *
4572 * Reading is not available if GFAN exists.
4573 * Writing is not available if SFAN exists.
4574 *
4575 * Bits
4576 * 7 automatic mode engaged;
4577 * (default operation mode of the ThinkPad)
4578 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004579 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004580 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004581 * the tachometer while the fan controller ramps up
4582 * the speed (which can take up to a few *minutes*).
4583 * Speeds up fan to 100% duty-cycle, which is far above
4584 * the standard RPM levels. It is not impossible that
4585 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004586 * 5-3 unused in some models. Extra bits for fan level
4587 * in others, but still useless as all values above
4588 * 7 map to the same speed as level 7 in these models.
4589 * 2-0 fan level (0..7 usually)
4590 * 0x00 = stop
4591 * 0x07 = max (set when temperatures critical)
4592 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004593 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004594 *
4595 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4596 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4597 * does so, its initial value is meaningless (0x07).
4598 *
4599 * For firmware bugs, refer to:
4600 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4601 *
4602 * ----
4603 *
4604 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4605 * Main fan tachometer reading (in RPM)
4606 *
4607 * This register is present on all ThinkPads with a new-style EC, and
4608 * it is known not to be present on the A21m/e, and T22, as there is
4609 * something else in offset 0x84 according to the ACPI DSDT. Other
4610 * ThinkPads from this same time period (and earlier) probably lack the
4611 * tachometer as well.
4612 *
4613 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4614 * was never fixed by IBM to report the EC firmware version string
4615 * probably support the tachometer (like the early X models), so
4616 * detecting it is quite hard. We need more data to know for sure.
4617 *
4618 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4619 * might result.
4620 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004621 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4622 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004623 *
4624 * For firmware bugs, refer to:
4625 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4626 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004627 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004628 * ThinkPad X31, X40, X41. Not available in the X60.
4629 *
4630 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4631 * high speed. ACPI DSDT seems to map these three speeds to levels
4632 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4633 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4634 *
4635 * The speeds are stored on handles
4636 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4637 *
4638 * There are three default speed sets, acessible as handles:
4639 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4640 *
4641 * ACPI DSDT switches which set is in use depending on various
4642 * factors.
4643 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004644 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004645 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4646 * but the ACPI tables just mention level 7.
4647 */
4648
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004649enum { /* Fan control constants */
4650 fan_status_offset = 0x2f, /* EC register 0x2f */
4651 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4652 * 0x84 must be read before 0x85 */
4653
4654 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4655 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4656
4657 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4658};
4659
4660enum fan_status_access_mode {
4661 TPACPI_FAN_NONE = 0, /* No fan status or control */
4662 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4663 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4664};
4665
4666enum fan_control_access_mode {
4667 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4668 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4669 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4670 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4671};
4672
4673enum fan_control_commands {
4674 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4675 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4676 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4677 * and also watchdog cmd */
4678};
4679
4680static int fan_control_allowed;
4681
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004682static enum fan_status_access_mode fan_status_access_mode;
4683static enum fan_control_access_mode fan_control_access_mode;
4684static enum fan_control_commands fan_control_commands;
4685
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004686static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004687static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004688static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004689
4690static struct mutex fan_mutex;
4691
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004692static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004693static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004694
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004695TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4696TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004697 "\\FSPD", /* 600e/x, 770e, 770x */
4698 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004699TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004700 "JFNS", /* 770x-JL */
4701 ); /* all others */
4702
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004703/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004704 * Call with fan_mutex held
4705 */
4706static void fan_update_desired_level(u8 status)
4707{
4708 if ((status &
4709 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4710 if (status > 7)
4711 fan_control_desired_level = 7;
4712 else
4713 fan_control_desired_level = status;
4714 }
4715}
4716
4717static int fan_get_status(u8 *status)
4718{
4719 u8 s;
4720
4721 /* TODO:
4722 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4723
4724 switch (fan_status_access_mode) {
4725 case TPACPI_FAN_RD_ACPI_GFAN:
4726 /* 570, 600e/x, 770e, 770x */
4727
4728 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4729 return -EIO;
4730
4731 if (likely(status))
4732 *status = s & 0x07;
4733
4734 break;
4735
4736 case TPACPI_FAN_RD_TPEC:
4737 /* all except 570, 600e/x, 770e, 770x */
4738 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4739 return -EIO;
4740
4741 if (likely(status))
4742 *status = s;
4743
4744 break;
4745
4746 default:
4747 return -ENXIO;
4748 }
4749
4750 return 0;
4751}
4752
4753static int fan_get_status_safe(u8 *status)
4754{
4755 int rc;
4756 u8 s;
4757
4758 if (mutex_lock_interruptible(&fan_mutex))
4759 return -ERESTARTSYS;
4760 rc = fan_get_status(&s);
4761 if (!rc)
4762 fan_update_desired_level(s);
4763 mutex_unlock(&fan_mutex);
4764
4765 if (status)
4766 *status = s;
4767
4768 return rc;
4769}
4770
4771static int fan_get_speed(unsigned int *speed)
4772{
4773 u8 hi, lo;
4774
4775 switch (fan_status_access_mode) {
4776 case TPACPI_FAN_RD_TPEC:
4777 /* all except 570, 600e/x, 770e, 770x */
4778 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4779 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4780 return -EIO;
4781
4782 if (likely(speed))
4783 *speed = (hi << 8) | lo;
4784
4785 break;
4786
4787 default:
4788 return -ENXIO;
4789 }
4790
4791 return 0;
4792}
4793
4794static int fan_set_level(int level)
4795{
4796 if (!fan_control_allowed)
4797 return -EPERM;
4798
4799 switch (fan_control_access_mode) {
4800 case TPACPI_FAN_WR_ACPI_SFAN:
4801 if (level >= 0 && level <= 7) {
4802 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4803 return -EIO;
4804 } else
4805 return -EINVAL;
4806 break;
4807
4808 case TPACPI_FAN_WR_ACPI_FANS:
4809 case TPACPI_FAN_WR_TPEC:
4810 if ((level != TP_EC_FAN_AUTO) &&
4811 (level != TP_EC_FAN_FULLSPEED) &&
4812 ((level < 0) || (level > 7)))
4813 return -EINVAL;
4814
4815 /* safety net should the EC not support AUTO
4816 * or FULLSPEED mode bits and just ignore them */
4817 if (level & TP_EC_FAN_FULLSPEED)
4818 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01004819 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004820 level |= 4; /* safety min speed 4 */
4821
4822 if (!acpi_ec_write(fan_status_offset, level))
4823 return -EIO;
4824 else
4825 tp_features.fan_ctrl_status_undef = 0;
4826 break;
4827
4828 default:
4829 return -ENXIO;
4830 }
4831 return 0;
4832}
4833
4834static int fan_set_level_safe(int level)
4835{
4836 int rc;
4837
4838 if (!fan_control_allowed)
4839 return -EPERM;
4840
4841 if (mutex_lock_interruptible(&fan_mutex))
4842 return -ERESTARTSYS;
4843
4844 if (level == TPACPI_FAN_LAST_LEVEL)
4845 level = fan_control_desired_level;
4846
4847 rc = fan_set_level(level);
4848 if (!rc)
4849 fan_update_desired_level(level);
4850
4851 mutex_unlock(&fan_mutex);
4852 return rc;
4853}
4854
4855static int fan_set_enable(void)
4856{
4857 u8 s;
4858 int rc;
4859
4860 if (!fan_control_allowed)
4861 return -EPERM;
4862
4863 if (mutex_lock_interruptible(&fan_mutex))
4864 return -ERESTARTSYS;
4865
4866 switch (fan_control_access_mode) {
4867 case TPACPI_FAN_WR_ACPI_FANS:
4868 case TPACPI_FAN_WR_TPEC:
4869 rc = fan_get_status(&s);
4870 if (rc < 0)
4871 break;
4872
4873 /* Don't go out of emergency fan mode */
4874 if (s != 7) {
4875 s &= 0x07;
4876 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4877 }
4878
4879 if (!acpi_ec_write(fan_status_offset, s))
4880 rc = -EIO;
4881 else {
4882 tp_features.fan_ctrl_status_undef = 0;
4883 rc = 0;
4884 }
4885 break;
4886
4887 case TPACPI_FAN_WR_ACPI_SFAN:
4888 rc = fan_get_status(&s);
4889 if (rc < 0)
4890 break;
4891
4892 s &= 0x07;
4893
4894 /* Set fan to at least level 4 */
4895 s |= 4;
4896
4897 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004898 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004899 else
4900 rc = 0;
4901 break;
4902
4903 default:
4904 rc = -ENXIO;
4905 }
4906
4907 mutex_unlock(&fan_mutex);
4908 return rc;
4909}
4910
4911static int fan_set_disable(void)
4912{
4913 int rc;
4914
4915 if (!fan_control_allowed)
4916 return -EPERM;
4917
4918 if (mutex_lock_interruptible(&fan_mutex))
4919 return -ERESTARTSYS;
4920
4921 rc = 0;
4922 switch (fan_control_access_mode) {
4923 case TPACPI_FAN_WR_ACPI_FANS:
4924 case TPACPI_FAN_WR_TPEC:
4925 if (!acpi_ec_write(fan_status_offset, 0x00))
4926 rc = -EIO;
4927 else {
4928 fan_control_desired_level = 0;
4929 tp_features.fan_ctrl_status_undef = 0;
4930 }
4931 break;
4932
4933 case TPACPI_FAN_WR_ACPI_SFAN:
4934 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4935 rc = -EIO;
4936 else
4937 fan_control_desired_level = 0;
4938 break;
4939
4940 default:
4941 rc = -ENXIO;
4942 }
4943
4944
4945 mutex_unlock(&fan_mutex);
4946 return rc;
4947}
4948
4949static int fan_set_speed(int speed)
4950{
4951 int rc;
4952
4953 if (!fan_control_allowed)
4954 return -EPERM;
4955
4956 if (mutex_lock_interruptible(&fan_mutex))
4957 return -ERESTARTSYS;
4958
4959 rc = 0;
4960 switch (fan_control_access_mode) {
4961 case TPACPI_FAN_WR_ACPI_FANS:
4962 if (speed >= 0 && speed <= 65535) {
4963 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4964 speed, speed, speed))
4965 rc = -EIO;
4966 } else
4967 rc = -EINVAL;
4968 break;
4969
4970 default:
4971 rc = -ENXIO;
4972 }
4973
4974 mutex_unlock(&fan_mutex);
4975 return rc;
4976}
4977
4978static void fan_watchdog_reset(void)
4979{
4980 static int fan_watchdog_active;
4981
4982 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4983 return;
4984
4985 if (fan_watchdog_active)
4986 cancel_delayed_work(&fan_watchdog_task);
4987
4988 if (fan_watchdog_maxinterval > 0 &&
4989 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4990 fan_watchdog_active = 1;
4991 if (!schedule_delayed_work(&fan_watchdog_task,
4992 msecs_to_jiffies(fan_watchdog_maxinterval
4993 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004994 printk(TPACPI_ERR
4995 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004996 "watchdog will not trigger\n");
4997 }
4998 } else
4999 fan_watchdog_active = 0;
5000}
5001
5002static void fan_watchdog_fire(struct work_struct *ignored)
5003{
5004 int rc;
5005
5006 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5007 return;
5008
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005009 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005010 rc = fan_set_enable();
5011 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005012 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005013 "will try again later...\n", -rc);
5014 /* reschedule for later */
5015 fan_watchdog_reset();
5016 }
5017}
5018
5019/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005020 * SYSFS fan layout: hwmon compatible (device)
5021 *
5022 * pwm*_enable:
5023 * 0: "disengaged" mode
5024 * 1: manual mode
5025 * 2: native EC "auto" mode (recommended, hardware default)
5026 *
5027 * pwm*: set speed in manual mode, ignored otherwise.
5028 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5029 * interpolation.
5030 *
5031 * fan*_input: tachometer reading, RPM
5032 *
5033 *
5034 * SYSFS fan layout: extensions
5035 *
5036 * fan_watchdog (driver):
5037 * fan watchdog interval in seconds, 0 disables (default), max 120
5038 */
5039
5040/* sysfs fan pwm1_enable ----------------------------------------------- */
5041static ssize_t fan_pwm1_enable_show(struct device *dev,
5042 struct device_attribute *attr,
5043 char *buf)
5044{
5045 int res, mode;
5046 u8 status;
5047
5048 res = fan_get_status_safe(&status);
5049 if (res)
5050 return res;
5051
5052 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5053 if (status != fan_control_initial_status) {
5054 tp_features.fan_ctrl_status_undef = 0;
5055 } else {
5056 /* Return most likely status. In fact, it
5057 * might be the only possible status */
5058 status = TP_EC_FAN_AUTO;
5059 }
5060 }
5061
5062 if (status & TP_EC_FAN_FULLSPEED) {
5063 mode = 0;
5064 } else if (status & TP_EC_FAN_AUTO) {
5065 mode = 2;
5066 } else
5067 mode = 1;
5068
5069 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5070}
5071
5072static ssize_t fan_pwm1_enable_store(struct device *dev,
5073 struct device_attribute *attr,
5074 const char *buf, size_t count)
5075{
5076 unsigned long t;
5077 int res, level;
5078
5079 if (parse_strtoul(buf, 2, &t))
5080 return -EINVAL;
5081
5082 switch (t) {
5083 case 0:
5084 level = TP_EC_FAN_FULLSPEED;
5085 break;
5086 case 1:
5087 level = TPACPI_FAN_LAST_LEVEL;
5088 break;
5089 case 2:
5090 level = TP_EC_FAN_AUTO;
5091 break;
5092 case 3:
5093 /* reserved for software-controlled auto mode */
5094 return -ENOSYS;
5095 default:
5096 return -EINVAL;
5097 }
5098
5099 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005100 if (res == -ENXIO)
5101 return -EINVAL;
5102 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005103 return res;
5104
5105 fan_watchdog_reset();
5106
5107 return count;
5108}
5109
5110static struct device_attribute dev_attr_fan_pwm1_enable =
5111 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5112 fan_pwm1_enable_show, fan_pwm1_enable_store);
5113
5114/* sysfs fan pwm1 ------------------------------------------------------ */
5115static ssize_t fan_pwm1_show(struct device *dev,
5116 struct device_attribute *attr,
5117 char *buf)
5118{
5119 int res;
5120 u8 status;
5121
5122 res = fan_get_status_safe(&status);
5123 if (res)
5124 return res;
5125
5126 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5127 if (status != fan_control_initial_status) {
5128 tp_features.fan_ctrl_status_undef = 0;
5129 } else {
5130 status = TP_EC_FAN_AUTO;
5131 }
5132 }
5133
5134 if ((status &
5135 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5136 status = fan_control_desired_level;
5137
5138 if (status > 7)
5139 status = 7;
5140
5141 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5142}
5143
5144static ssize_t fan_pwm1_store(struct device *dev,
5145 struct device_attribute *attr,
5146 const char *buf, size_t count)
5147{
5148 unsigned long s;
5149 int rc;
5150 u8 status, newlevel;
5151
5152 if (parse_strtoul(buf, 255, &s))
5153 return -EINVAL;
5154
5155 /* scale down from 0-255 to 0-7 */
5156 newlevel = (s >> 5) & 0x07;
5157
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005158 if (mutex_lock_interruptible(&fan_mutex))
5159 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005160
5161 rc = fan_get_status(&status);
5162 if (!rc && (status &
5163 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5164 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005165 if (rc == -ENXIO)
5166 rc = -EINVAL;
5167 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005168 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005169 fan_watchdog_reset();
5170 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005171 }
5172
5173 mutex_unlock(&fan_mutex);
5174 return (rc)? rc : count;
5175}
5176
5177static struct device_attribute dev_attr_fan_pwm1 =
5178 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5179 fan_pwm1_show, fan_pwm1_store);
5180
5181/* sysfs fan fan1_input ------------------------------------------------ */
5182static ssize_t fan_fan1_input_show(struct device *dev,
5183 struct device_attribute *attr,
5184 char *buf)
5185{
5186 int res;
5187 unsigned int speed;
5188
5189 res = fan_get_speed(&speed);
5190 if (res < 0)
5191 return res;
5192
5193 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5194}
5195
5196static struct device_attribute dev_attr_fan_fan1_input =
5197 __ATTR(fan1_input, S_IRUGO,
5198 fan_fan1_input_show, NULL);
5199
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005200/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005201static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5202 char *buf)
5203{
5204 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5205}
5206
5207static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5208 const char *buf, size_t count)
5209{
5210 unsigned long t;
5211
5212 if (parse_strtoul(buf, 120, &t))
5213 return -EINVAL;
5214
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005215 if (!fan_control_allowed)
5216 return -EPERM;
5217
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005218 fan_watchdog_maxinterval = t;
5219 fan_watchdog_reset();
5220
5221 return count;
5222}
5223
5224static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5225 fan_fan_watchdog_show, fan_fan_watchdog_store);
5226
5227/* --------------------------------------------------------------------- */
5228static struct attribute *fan_attributes[] = {
5229 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5230 &dev_attr_fan_fan1_input.attr,
5231 NULL
5232};
5233
5234static const struct attribute_group fan_attr_group = {
5235 .attrs = fan_attributes,
5236};
5237
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005238static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005239{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005240 int rc;
5241
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005242 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5243
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005244 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005245 fan_status_access_mode = TPACPI_FAN_NONE;
5246 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005247 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005248 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005249 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005250 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005251
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005252 TPACPI_ACPIHANDLE_INIT(fans);
5253 TPACPI_ACPIHANDLE_INIT(gfan);
5254 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005255
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005256 if (gfan_handle) {
5257 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005258 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005259 } else {
5260 /* all other ThinkPads: note that even old-style
5261 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005262 if (likely(acpi_ec_read(fan_status_offset,
5263 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005264 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005265
5266 /* In some ThinkPads, neither the EC nor the ACPI
5267 * DSDT initialize the fan status, and it ends up
5268 * being set to 0x07 when it *could* be either
5269 * 0x07 or 0x80.
5270 *
5271 * Enable for TP-1Y (T43), TP-78 (R51e),
5272 * TP-76 (R52), TP-70 (T43, R52), which are known
5273 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005274 if (fan_control_initial_status == 0x07) {
5275 switch (thinkpad_id.ec_model) {
5276 case 0x5931: /* TP-1Y */
5277 case 0x3837: /* TP-78 */
5278 case 0x3637: /* TP-76 */
5279 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005280 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005281 "fan_init: initial fan status "
5282 "is unknown, assuming it is "
5283 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005284 tp_features.fan_ctrl_status_undef = 1;
5285 ;;
5286 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005287 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005288 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005289 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005290 "ThinkPad ACPI EC access misbehaving, "
5291 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005292 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005293 }
5294 }
5295
5296 if (sfan_handle) {
5297 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005298 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005299 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005300 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005301 } else {
5302 if (!gfan_handle) {
5303 /* gfan without sfan means no fan control */
5304 /* all other models implement TP EC 0x2f control */
5305
5306 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005307 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005308 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005309 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005310 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005311 TPACPI_FAN_CMD_SPEED |
5312 TPACPI_FAN_CMD_LEVEL |
5313 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005314 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005315 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005316 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005317 TPACPI_FAN_CMD_LEVEL |
5318 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005319 }
5320 }
5321 }
5322
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005323 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5324 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5325 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5326 fan_status_access_mode, fan_control_access_mode);
5327
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005328 /* fan control master switch */
5329 if (!fan_control_allowed) {
5330 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5331 fan_control_commands = 0;
5332 dbg_printk(TPACPI_DBG_INIT,
5333 "fan control features disabled by parameter\n");
5334 }
5335
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005336 /* update fan_control_desired_level */
5337 if (fan_status_access_mode != TPACPI_FAN_NONE)
5338 fan_get_status_safe(NULL);
5339
5340 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5341 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005342 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005343 &fan_attr_group);
5344 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005345 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005346 &driver_attr_fan_watchdog);
5347 if (rc < 0)
5348 return rc;
5349 return 0;
5350 } else
5351 return 1;
5352}
5353
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005354static void fan_exit(void)
5355{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005356 vdbg_printk(TPACPI_DBG_EXIT,
5357 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005358
5359 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005360 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005361 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5362 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005363
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005364 cancel_delayed_work(&fan_watchdog_task);
5365 flush_scheduled_work();
5366}
5367
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005368static int fan_read(char *p)
5369{
5370 int len = 0;
5371 int rc;
5372 u8 status;
5373 unsigned int speed = 0;
5374
5375 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005376 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005377 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005378 rc = fan_get_status_safe(&status);
5379 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005380 return rc;
5381
5382 len += sprintf(p + len, "status:\t\t%s\n"
5383 "level:\t\t%d\n",
5384 (status != 0) ? "enabled" : "disabled", status);
5385 break;
5386
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005387 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005388 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005389 rc = fan_get_status_safe(&status);
5390 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005391 return rc;
5392
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005393 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005394 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005395 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005396 else
5397 /* Return most likely status. In fact, it
5398 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005399 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005400 }
5401
5402 len += sprintf(p + len, "status:\t\t%s\n",
5403 (status != 0) ? "enabled" : "disabled");
5404
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005405 rc = fan_get_speed(&speed);
5406 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005407 return rc;
5408
5409 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5410
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005411 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005412 /* Disengaged mode takes precedence */
5413 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005414 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005415 len += sprintf(p + len, "level:\t\tauto\n");
5416 else
5417 len += sprintf(p + len, "level:\t\t%d\n", status);
5418 break;
5419
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005420 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005421 default:
5422 len += sprintf(p + len, "status:\t\tnot supported\n");
5423 }
5424
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005425 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005426 len += sprintf(p + len, "commands:\tlevel <level>");
5427
5428 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005429 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005430 len += sprintf(p + len, " (<level> is 0-7)\n");
5431 break;
5432
5433 default:
5434 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005435 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005436 break;
5437 }
5438 }
5439
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005440 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005441 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005442 "commands:\twatchdog <timeout> (<timeout> "
5443 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005444
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005445 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005446 len += sprintf(p + len, "commands:\tspeed <speed>"
5447 " (<speed> is 0-65535)\n");
5448
5449 return len;
5450}
5451
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005452static int fan_write_cmd_level(const char *cmd, int *rc)
5453{
5454 int level;
5455
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005456 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005457 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005458 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005459 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005460 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005461 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005462 return 0;
5463
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005464 *rc = fan_set_level_safe(level);
5465 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005466 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005467 "access mode %d", fan_control_access_mode);
5468
5469 return 1;
5470}
5471
5472static int fan_write_cmd_enable(const char *cmd, int *rc)
5473{
5474 if (strlencmp(cmd, "enable") != 0)
5475 return 0;
5476
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005477 *rc = fan_set_enable();
5478 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005479 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005480 "access mode %d", fan_control_access_mode);
5481
5482 return 1;
5483}
5484
5485static int fan_write_cmd_disable(const char *cmd, int *rc)
5486{
5487 if (strlencmp(cmd, "disable") != 0)
5488 return 0;
5489
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005490 *rc = fan_set_disable();
5491 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005492 printk(TPACPI_ERR "disable 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
5498static int fan_write_cmd_speed(const char *cmd, int *rc)
5499{
5500 int speed;
5501
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005502 /* TODO:
5503 * Support speed <low> <medium> <high> ? */
5504
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005505 if (sscanf(cmd, "speed %d", &speed) != 1)
5506 return 0;
5507
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005508 *rc = fan_set_speed(speed);
5509 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005510 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005511 "access mode %d", fan_control_access_mode);
5512
5513 return 1;
5514}
5515
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005516static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5517{
5518 int interval;
5519
5520 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5521 return 0;
5522
5523 if (interval < 0 || interval > 120)
5524 *rc = -EINVAL;
5525 else
5526 fan_watchdog_maxinterval = interval;
5527
5528 return 1;
5529}
5530
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005531static int fan_write(char *buf)
5532{
5533 char *cmd;
5534 int rc = 0;
5535
5536 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005537 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005538 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005539 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005540 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005541 fan_write_cmd_disable(cmd, &rc) ||
5542 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005543 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005544 fan_write_cmd_speed(cmd, &rc))
5545 )
5546 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005547 else if (!rc)
5548 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005549 }
5550
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005551 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005552}
5553
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005554static struct ibm_struct fan_driver_data = {
5555 .name = "fan",
5556 .read = fan_read,
5557 .write = fan_write,
5558 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005559};
5560
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005561/****************************************************************************
5562 ****************************************************************************
5563 *
5564 * Infrastructure
5565 *
5566 ****************************************************************************
5567 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005569/* sysfs name ---------------------------------------------------------- */
5570static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5571 struct device_attribute *attr,
5572 char *buf)
5573{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005574 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005575}
5576
5577static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5578 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5579
5580/* --------------------------------------------------------------------- */
5581
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005582/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005583static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005584
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005585/*
5586 * Module and infrastructure proble, init and exit handling
5587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005589static int force_load;
5590
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005591#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005592static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005593{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005594 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005595
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005596 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005597}
5598#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5599
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005600static void ibm_exit(struct ibm_struct *ibm)
5601{
5602 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5603
5604 list_del_init(&ibm->all_drivers);
5605
5606 if (ibm->flags.acpi_notify_installed) {
5607 dbg_printk(TPACPI_DBG_EXIT,
5608 "%s: acpi_remove_notify_handler\n", ibm->name);
5609 BUG_ON(!ibm->acpi);
5610 acpi_remove_notify_handler(*ibm->acpi->handle,
5611 ibm->acpi->type,
5612 dispatch_acpi_notify);
5613 ibm->flags.acpi_notify_installed = 0;
5614 ibm->flags.acpi_notify_installed = 0;
5615 }
5616
5617 if (ibm->flags.proc_created) {
5618 dbg_printk(TPACPI_DBG_EXIT,
5619 "%s: remove_proc_entry\n", ibm->name);
5620 remove_proc_entry(ibm->name, proc_dir);
5621 ibm->flags.proc_created = 0;
5622 }
5623
5624 if (ibm->flags.acpi_driver_registered) {
5625 dbg_printk(TPACPI_DBG_EXIT,
5626 "%s: acpi_bus_unregister_driver\n", ibm->name);
5627 BUG_ON(!ibm->acpi);
5628 acpi_bus_unregister_driver(ibm->acpi->driver);
5629 kfree(ibm->acpi->driver);
5630 ibm->acpi->driver = NULL;
5631 ibm->flags.acpi_driver_registered = 0;
5632 }
5633
5634 if (ibm->flags.init_called && ibm->exit) {
5635 ibm->exit();
5636 ibm->flags.init_called = 0;
5637 }
5638
5639 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5640}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005641
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005642static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643{
5644 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005645 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 struct proc_dir_entry *entry;
5647
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005648 BUG_ON(ibm == NULL);
5649
5650 INIT_LIST_HEAD(&ibm->all_drivers);
5651
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005652 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 return 0;
5654
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005655 dbg_printk(TPACPI_DBG_INIT,
5656 "probing for %s\n", ibm->name);
5657
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005658 if (iibm->init) {
5659 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005660 if (ret > 0)
5661 return 0; /* probe failed */
5662 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005664
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005665 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 }
5667
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005668 if (ibm->acpi) {
5669 if (ibm->acpi->hid) {
5670 ret = register_tpacpi_subdriver(ibm);
5671 if (ret)
5672 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005673 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005674
5675 if (ibm->acpi->notify) {
5676 ret = setup_acpi_notify(ibm);
5677 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005678 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005679 ibm->name);
5680 ret = 0;
5681 goto err_out;
5682 }
5683 if (ret < 0)
5684 goto err_out;
5685 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005686 }
5687
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005688 dbg_printk(TPACPI_DBG_INIT,
5689 "%s installed\n", ibm->name);
5690
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005691 if (ibm->read) {
5692 entry = create_proc_entry(ibm->name,
5693 S_IFREG | S_IRUGO | S_IWUSR,
5694 proc_dir);
5695 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005696 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005697 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005698 ret = -ENODEV;
5699 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005700 }
5701 entry->owner = THIS_MODULE;
5702 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005703 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005704 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005705 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005706 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005709 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005712
5713err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005714 dbg_printk(TPACPI_DBG_INIT,
5715 "%s: at error exit path with result %d\n",
5716 ibm->name, ret);
5717
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005718 ibm_exit(ibm);
5719 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720}
5721
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005722/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005724static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005725{
Jeff Garzik18552562007-10-03 15:15:40 -04005726 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005727 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005728
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005729 if (!tp)
5730 return;
5731
5732 memset(tp, 0, sizeof(*tp));
5733
5734 if (dmi_name_in_vendors("IBM"))
5735 tp->vendor = PCI_VENDOR_ID_IBM;
5736 else if (dmi_name_in_vendors("LENOVO"))
5737 tp->vendor = PCI_VENDOR_ID_LENOVO;
5738 else
5739 return;
5740
5741 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5742 GFP_KERNEL);
5743 if (!tp->bios_version_str)
5744 return;
5745 tp->bios_model = tp->bios_version_str[0]
5746 | (tp->bios_version_str[1] << 8);
5747
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005748 /*
5749 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5750 * X32 or newer, all Z series; Some models must have an
5751 * up-to-date BIOS or they will not be detected.
5752 *
5753 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5754 */
5755 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005756 if (sscanf(dev->name,
5757 "IBM ThinkPad Embedded Controller -[%17c",
5758 ec_fw_string) == 1) {
5759 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5760 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005761
5762 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5763 tp->ec_model = ec_fw_string[0]
5764 | (ec_fw_string[1] << 8);
5765 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005766 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005767 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005768
5769 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5770 GFP_KERNEL);
5771 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5772 kfree(tp->model_str);
5773 tp->model_str = NULL;
5774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775}
5776
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005777static int __init probe_for_thinkpad(void)
5778{
5779 int is_thinkpad;
5780
5781 if (acpi_disabled)
5782 return -ENODEV;
5783
5784 /*
5785 * Non-ancient models have better DMI tagging, but very old models
5786 * don't.
5787 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005788 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005789
5790 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005791 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005792 if (!ec_handle) {
5793 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005794 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005795 "Not yet supported ThinkPad detected!\n");
5796 return -ENODEV;
5797 }
5798
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005799 /*
5800 * Risks a regression on very old machines, but reduces potential
5801 * false positives a damn great deal
5802 */
5803 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005804 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005805
5806 if (!is_thinkpad && !force_load)
5807 return -ENODEV;
5808
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005809 return 0;
5810}
5811
5812
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005813/* Module init, exit, parameters */
5814
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005815static struct ibm_init_struct ibms_init[] __initdata = {
5816 {
5817 .init = thinkpad_acpi_driver_init,
5818 .data = &thinkpad_acpi_driver_data,
5819 },
5820 {
5821 .init = hotkey_init,
5822 .data = &hotkey_driver_data,
5823 },
5824 {
5825 .init = bluetooth_init,
5826 .data = &bluetooth_driver_data,
5827 },
5828 {
5829 .init = wan_init,
5830 .data = &wan_driver_data,
5831 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005832#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005833 {
5834 .init = video_init,
5835 .data = &video_driver_data,
5836 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005837#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005838 {
5839 .init = light_init,
5840 .data = &light_driver_data,
5841 },
5842#ifdef CONFIG_THINKPAD_ACPI_DOCK
5843 {
5844 .init = dock_init,
5845 .data = &dock_driver_data[0],
5846 },
5847 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005848 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005849 .data = &dock_driver_data[1],
5850 },
5851#endif
5852#ifdef CONFIG_THINKPAD_ACPI_BAY
5853 {
5854 .init = bay_init,
5855 .data = &bay_driver_data,
5856 },
5857#endif
5858 {
5859 .init = cmos_init,
5860 .data = &cmos_driver_data,
5861 },
5862 {
5863 .init = led_init,
5864 .data = &led_driver_data,
5865 },
5866 {
5867 .init = beep_init,
5868 .data = &beep_driver_data,
5869 },
5870 {
5871 .init = thermal_init,
5872 .data = &thermal_driver_data,
5873 },
5874 {
5875 .data = &ecdump_driver_data,
5876 },
5877 {
5878 .init = brightness_init,
5879 .data = &brightness_driver_data,
5880 },
5881 {
5882 .data = &volume_driver_data,
5883 },
5884 {
5885 .init = fan_init,
5886 .data = &fan_driver_data,
5887 },
5888};
5889
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005890static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5891{
5892 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005893 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005894
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005895 if (!kp || !kp->name || !val)
5896 return -EINVAL;
5897
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005898 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5899 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005900 WARN_ON(ibm == NULL);
5901
5902 if (!ibm || !ibm->name)
5903 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005904
5905 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5906 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005907 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005908 strcpy(ibms_init[i].param, val);
5909 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005910 return 0;
5911 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005912 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005913
5914 return -EINVAL;
5915}
5916
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005917module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005918MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005919 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005920
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005921module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005922MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005923
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005924module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005925MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005926 "Attempts to load the driver even on a "
5927 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005928
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005929module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005930MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005931 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005932
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005933module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005934MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005935 "Selects brightness control strategy: "
5936 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005937
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005938module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005939MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005940 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005941
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005942module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005943MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005944 "used for backwards compatibility with userspace, "
5945 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005946
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005947#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005948 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02005949 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005950 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005951
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005952TPACPI_PARAM(hotkey);
5953TPACPI_PARAM(bluetooth);
5954TPACPI_PARAM(video);
5955TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005956#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005957TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005958#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005959#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005960TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005961#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005962TPACPI_PARAM(cmos);
5963TPACPI_PARAM(led);
5964TPACPI_PARAM(beep);
5965TPACPI_PARAM(ecdump);
5966TPACPI_PARAM(brightness);
5967TPACPI_PARAM(volume);
5968TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005969
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005970static void thinkpad_acpi_module_exit(void)
5971{
5972 struct ibm_struct *ibm, *itmp;
5973
5974 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5975
5976 list_for_each_entry_safe_reverse(ibm, itmp,
5977 &tpacpi_all_drivers,
5978 all_drivers) {
5979 ibm_exit(ibm);
5980 }
5981
5982 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5983
5984 if (tpacpi_inputdev) {
5985 if (tp_features.input_device_registered)
5986 input_unregister_device(tpacpi_inputdev);
5987 else
5988 input_free_device(tpacpi_inputdev);
5989 }
5990
5991 if (tpacpi_hwmon)
5992 hwmon_device_unregister(tpacpi_hwmon);
5993
5994 if (tp_features.sensors_pdev_attrs_registered)
5995 device_remove_file(&tpacpi_sensors_pdev->dev,
5996 &dev_attr_thinkpad_acpi_pdev_name);
5997 if (tpacpi_sensors_pdev)
5998 platform_device_unregister(tpacpi_sensors_pdev);
5999 if (tpacpi_pdev)
6000 platform_device_unregister(tpacpi_pdev);
6001
6002 if (tp_features.sensors_pdrv_attrs_registered)
6003 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6004 if (tp_features.platform_drv_attrs_registered)
6005 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6006
6007 if (tp_features.sensors_pdrv_registered)
6008 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6009
6010 if (tp_features.platform_drv_registered)
6011 platform_driver_unregister(&tpacpi_pdriver);
6012
6013 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006014 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006015
6016 kfree(thinkpad_id.bios_version_str);
6017 kfree(thinkpad_id.ec_version_str);
6018 kfree(thinkpad_id.model_str);
6019}
6020
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006021
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006022static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023{
6024 int ret, i;
6025
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006026 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6027
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006028 /* Parameter checking */
6029 if (hotkey_report_mode > 2)
6030 return -EINVAL;
6031
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006032 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006033
6034 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006035 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006036 if (ret) {
6037 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006038 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006041 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006042
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006043 TPACPI_ACPIHANDLE_INIT(ecrd);
6044 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006046 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006048 printk(TPACPI_ERR
6049 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006050 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 return -ENODEV;
6052 }
6053 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006054
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006055 ret = platform_driver_register(&tpacpi_pdriver);
6056 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006057 printk(TPACPI_ERR
6058 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006059 thinkpad_acpi_module_exit();
6060 return ret;
6061 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006062 tp_features.platform_drv_registered = 1;
6063
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006064 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6065 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006066 printk(TPACPI_ERR
6067 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006068 thinkpad_acpi_module_exit();
6069 return ret;
6070 }
6071 tp_features.sensors_pdrv_registered = 1;
6072
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006073 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006074 if (!ret) {
6075 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006076 ret = tpacpi_create_driver_attributes(
6077 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006078 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006079 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006080 printk(TPACPI_ERR
6081 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006082 thinkpad_acpi_module_exit();
6083 return ret;
6084 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006085 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006086
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006087
6088 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006089 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006090 NULL, 0);
6091 if (IS_ERR(tpacpi_pdev)) {
6092 ret = PTR_ERR(tpacpi_pdev);
6093 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006094 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006095 thinkpad_acpi_module_exit();
6096 return ret;
6097 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006098 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006099 TPACPI_HWMON_DRVR_NAME,
6100 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006101 if (IS_ERR(tpacpi_sensors_pdev)) {
6102 ret = PTR_ERR(tpacpi_sensors_pdev);
6103 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006104 printk(TPACPI_ERR
6105 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006106 thinkpad_acpi_module_exit();
6107 return ret;
6108 }
6109 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6110 &dev_attr_thinkpad_acpi_pdev_name);
6111 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006112 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006113 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006114 thinkpad_acpi_module_exit();
6115 return ret;
6116 }
6117 tp_features.sensors_pdev_attrs_registered = 1;
6118 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006119 if (IS_ERR(tpacpi_hwmon)) {
6120 ret = PTR_ERR(tpacpi_hwmon);
6121 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006122 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006123 thinkpad_acpi_module_exit();
6124 return ret;
6125 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006126 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006127 tpacpi_inputdev = input_allocate_device();
6128 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006129 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006130 thinkpad_acpi_module_exit();
6131 return -ENOMEM;
6132 } else {
6133 /* Prepare input device, but don't register */
6134 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006135 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006136 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006137 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6138 thinkpad_id.vendor :
6139 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006140 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6141 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6142 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006143 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6144 ret = ibm_init(&ibms_init[i]);
6145 if (ret >= 0 && *ibms_init[i].param)
6146 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006148 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 return ret;
6150 }
6151 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006152 ret = input_register_device(tpacpi_inputdev);
6153 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006154 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006155 thinkpad_acpi_module_exit();
6156 return ret;
6157 } else {
6158 tp_features.input_device_registered = 1;
6159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006161 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 return 0;
6163}
6164
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006165/* Please remove this in year 2009 */
6166MODULE_ALIAS("ibm_acpi");
6167
6168/*
6169 * DMI matching for module autoloading
6170 *
6171 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6172 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6173 *
6174 * Only models listed in thinkwiki will be supported, so add yours
6175 * if it is not there yet.
6176 */
6177#define IBM_BIOS_MODULE_ALIAS(__type) \
6178 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6179
6180/* Non-ancient thinkpads */
6181MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6182MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6183
6184/* Ancient thinkpad BIOSes have to be identified by
6185 * BIOS type or model number, and there are far less
6186 * BIOS types than model numbers... */
6187IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6188IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6189IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6190
6191MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006192MODULE_DESCRIPTION(TPACPI_DESC);
6193MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006194MODULE_LICENSE("GPL");
6195
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006196module_init(thinkpad_acpi_module_init);
6197module_exit(thinkpad_acpi_module_exit);