blob: 02f9465165847f9a90784928689cf4257e45497d [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 Holschuh94954cc62007-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
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400304
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300305/*************************************************************************
306 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200307 */
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static int acpi_evalf(acpi_handle handle,
310 void *res, char *method, char *fmt, ...)
311{
312 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400313 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200314 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400315 struct acpi_buffer result, *resultp;
316 union acpi_object out_obj;
317 acpi_status status;
318 va_list ap;
319 char res_type;
320 int success;
321 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200324 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326 }
327
328 if (*fmt == 'q') {
329 quiet = 1;
330 fmt++;
331 } else
332 quiet = 0;
333
334 res_type = *(fmt++);
335
336 params.count = 0;
337 params.pointer = &in_objs[0];
338
339 va_start(ap, fmt);
340 while (*fmt) {
341 char c = *(fmt++);
342 switch (c) {
343 case 'd': /* int */
344 in_objs[params.count].integer.value = va_arg(ap, int);
345 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
346 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400347 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200349 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 "with invalid format character '%c'\n", c);
351 return 0;
352 }
353 }
354 va_end(ap);
355
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400356 if (res_type != 'v') {
357 result.length = sizeof(out_obj);
358 result.pointer = &out_obj;
359 resultp = &result;
360 } else
361 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400363 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400366 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (res)
368 *(int *)res = out_obj.integer.value;
369 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
370 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400371 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 success = status == AE_OK;
373 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400374 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200376 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 "with invalid format character '%c'\n", res_type);
378 return 0;
379 }
380
381 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200382 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 method, fmt0, status);
384
385 return success;
386}
387
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200388static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300389{
390 int v;
391
392 if (ecrd_handle) {
393 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
394 return 0;
395 *p = v;
396 } else {
397 if (ec_read(i, p) < 0)
398 return 0;
399 }
400
401 return 1;
402}
403
404static int acpi_ec_write(int i, u8 v)
405{
406 if (ecwr_handle) {
407 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
408 return 0;
409 } else {
410 if (ec_write(i, v) < 0)
411 return 0;
412 }
413
414 return 1;
415}
416
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200417#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300418static int _sta(acpi_handle handle)
419{
420 int status;
421
422 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
423 status = 0;
424
425 return status;
426}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200427#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300428
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300429static int issue_thinkpad_cmos_command(int cmos_cmd)
430{
431 if (!cmos_handle)
432 return -ENXIO;
433
434 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
435 return -EIO;
436
437 return 0;
438}
439
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300440/*************************************************************************
441 * ACPI device model
442 */
443
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200444#define TPACPI_ACPIHANDLE_INIT(object) \
445 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200446 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
447
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300448static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300449 acpi_handle *handle, acpi_handle parent,
450 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300451{
452 int i;
453 acpi_status status;
454
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300455 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
456 name);
457
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300458 for (i = 0; i < num_paths; i++) {
459 status = acpi_get_handle(parent, paths[i], handle);
460 if (ACPI_SUCCESS(status)) {
461 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300462 dbg_printk(TPACPI_DBG_INIT,
463 "Found ACPI handle %s for %s\n",
464 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300465 return;
466 }
467 }
468
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300469 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
470 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300471 *handle = NULL;
472}
473
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300474static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300475{
476 struct ibm_struct *ibm = data;
477
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300478 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
479 return;
480
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300481 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300482 return;
483
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300484 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300485}
486
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300487static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300488{
489 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300490 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300491
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300492 BUG_ON(!ibm->acpi);
493
494 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300495 return 0;
496
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300497 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300498 "setting up ACPI notify for %s\n", ibm->name);
499
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300500 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
501 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200502 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300503 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504 return -ENODEV;
505 }
506
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300507 acpi_driver_data(ibm->acpi->device) = ibm;
508 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200509 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300510 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300511
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300512 status = acpi_install_notify_handler(*ibm->acpi->handle,
513 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514 if (ACPI_FAILURE(status)) {
515 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200516 printk(TPACPI_NOTICE
517 "another device driver is already "
518 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200520 printk(TPACPI_ERR
521 "acpi_install_notify_handler(%s) failed: %d\n",
522 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523 }
524 return -ENODEV;
525 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300526 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300527 return 0;
528}
529
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300530static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300531{
532 return 0;
533}
534
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300535static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300537 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300538
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300539 dbg_printk(TPACPI_DBG_INIT,
540 "registering %s as an ACPI driver\n", ibm->name);
541
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300542 BUG_ON(!ibm->acpi);
543
544 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
545 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200546 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300547 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300548 }
549
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200550 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300551 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200552
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300553 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300554
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300555 rc = acpi_bus_register_driver(ibm->acpi->driver);
556 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200557 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200558 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300559 kfree(ibm->acpi->driver);
560 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300561 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300562 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300564 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300565}
566
567
568/****************************************************************************
569 ****************************************************************************
570 *
571 * Procfs Helpers
572 *
573 ****************************************************************************
574 ****************************************************************************/
575
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300576static int dispatch_procfs_read(char *page, char **start, off_t off,
577 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300578{
579 struct ibm_struct *ibm = data;
580 int len;
581
582 if (!ibm || !ibm->read)
583 return -EINVAL;
584
585 len = ibm->read(page);
586 if (len < 0)
587 return len;
588
589 if (len <= off + count)
590 *eof = 1;
591 *start = page + off;
592 len -= off;
593 if (len > count)
594 len = count;
595 if (len < 0)
596 len = 0;
597
598 return len;
599}
600
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300601static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200602 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300603 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300604{
605 struct ibm_struct *ibm = data;
606 char *kernbuf;
607 int ret;
608
609 if (!ibm || !ibm->write)
610 return -EINVAL;
611
612 kernbuf = kmalloc(count + 2, GFP_KERNEL);
613 if (!kernbuf)
614 return -ENOMEM;
615
616 if (copy_from_user(kernbuf, userbuf, count)) {
617 kfree(kernbuf);
618 return -EFAULT;
619 }
620
621 kernbuf[count] = 0;
622 strcat(kernbuf, ",");
623 ret = ibm->write(kernbuf);
624 if (ret == 0)
625 ret = count;
626
627 kfree(kernbuf);
628
629 return ret;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static char *next_cmd(char **cmds)
633{
634 char *start = *cmds;
635 char *end;
636
637 while ((end = strchr(start, ',')) && end == start)
638 start = end + 1;
639
640 if (!end)
641 return NULL;
642
643 *end = 0;
644 *cmds = end + 1;
645 return start;
646}
647
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300648
649/****************************************************************************
650 ****************************************************************************
651 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300652 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300653 *
654 ****************************************************************************
655 ****************************************************************************/
656
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300657static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300658static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700659static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300660static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300661static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200662static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300663
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200664static int tpacpi_suspend_handler(struct platform_device *pdev,
665 pm_message_t state)
666{
667 struct ibm_struct *ibm, *itmp;
668
669 list_for_each_entry_safe(ibm, itmp,
670 &tpacpi_all_drivers,
671 all_drivers) {
672 if (ibm->suspend)
673 (ibm->suspend)(state);
674 }
675
676 return 0;
677}
678
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300679static int tpacpi_resume_handler(struct platform_device *pdev)
680{
681 struct ibm_struct *ibm, *itmp;
682
683 list_for_each_entry_safe(ibm, itmp,
684 &tpacpi_all_drivers,
685 all_drivers) {
686 if (ibm->resume)
687 (ibm->resume)();
688 }
689
690 return 0;
691}
692
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300693static struct platform_driver tpacpi_pdriver = {
694 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200695 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300696 .owner = THIS_MODULE,
697 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200698 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300699 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300700};
701
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300702static struct platform_driver tpacpi_hwmon_pdriver = {
703 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200704 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300705 .owner = THIS_MODULE,
706 },
707};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300708
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300709/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300710 * sysfs support helpers
711 */
712
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200713struct attribute_set {
714 unsigned int members, max_members;
715 struct attribute_group group;
716};
717
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300718struct attribute_set_obj {
719 struct attribute_set s;
720 struct attribute *a;
721} __attribute__((packed));
722
723static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200724 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300725{
726 struct attribute_set_obj *sobj;
727
728 if (max_members == 0)
729 return NULL;
730
731 /* Allocates space for implicit NULL at the end too */
732 sobj = kzalloc(sizeof(struct attribute_set_obj) +
733 max_members * sizeof(struct attribute *),
734 GFP_KERNEL);
735 if (!sobj)
736 return NULL;
737 sobj->s.max_members = max_members;
738 sobj->s.group.attrs = &sobj->a;
739 sobj->s.group.name = name;
740
741 return &sobj->s;
742}
743
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200744#define destroy_attr_set(_set) \
745 kfree(_set);
746
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300747/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200748static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300749{
750 if (!s || !attr)
751 return -EINVAL;
752
753 if (s->members >= s->max_members)
754 return -ENOMEM;
755
756 s->group.attrs[s->members] = attr;
757 s->members++;
758
759 return 0;
760}
761
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200762static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300763 struct attribute **attr,
764 unsigned int count)
765{
766 int i, res;
767
768 for (i = 0; i < count; i++) {
769 res = add_to_attr_set(s, attr[i]);
770 if (res)
771 return res;
772 }
773
774 return 0;
775}
776
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200777static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300778{
779 sysfs_remove_group(kobj, &s->group);
780 destroy_attr_set(s);
781}
782
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200783#define register_attr_set_with_sysfs(_attr_set, _kobj) \
784 sysfs_create_group(_kobj, &_attr_set->group)
785
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300786static int parse_strtoul(const char *buf,
787 unsigned long max, unsigned long *value)
788{
789 char *endp;
790
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300791 while (*buf && isspace(*buf))
792 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300793 *value = simple_strtoul(buf, &endp, 0);
794 while (*endp && isspace(*endp))
795 endp++;
796 if (*endp || *value > max)
797 return -EINVAL;
798
799 return 0;
800}
801
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200802/*************************************************************************
803 * thinkpad-acpi driver attributes
804 */
805
806/* interface_version --------------------------------------------------- */
807static ssize_t tpacpi_driver_interface_version_show(
808 struct device_driver *drv,
809 char *buf)
810{
811 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
812}
813
814static DRIVER_ATTR(interface_version, S_IRUGO,
815 tpacpi_driver_interface_version_show, NULL);
816
817/* debug_level --------------------------------------------------------- */
818static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
819 char *buf)
820{
821 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
822}
823
824static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
825 const char *buf, size_t count)
826{
827 unsigned long t;
828
829 if (parse_strtoul(buf, 0xffff, &t))
830 return -EINVAL;
831
832 dbg_level = t;
833
834 return count;
835}
836
837static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
838 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
839
840/* version ------------------------------------------------------------- */
841static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
842 char *buf)
843{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200844 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
845 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200846}
847
848static DRIVER_ATTR(version, S_IRUGO,
849 tpacpi_driver_version_show, NULL);
850
851/* --------------------------------------------------------------------- */
852
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200853static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200854 &driver_attr_debug_level, &driver_attr_version,
855 &driver_attr_interface_version,
856};
857
858static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
859{
860 int i, res;
861
862 i = 0;
863 res = 0;
864 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
865 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
866 i++;
867 }
868
869 return res;
870}
871
872static void tpacpi_remove_driver_attributes(struct device_driver *drv)
873{
874 int i;
875
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200876 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200877 driver_remove_file(drv, tpacpi_driver_attributes[i]);
878}
879
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300880/****************************************************************************
881 ****************************************************************************
882 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300883 * Subdrivers
884 *
885 ****************************************************************************
886 ****************************************************************************/
887
888/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300889 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300890 */
891
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300892static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200894 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
895 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200897 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300898 (thinkpad_id.bios_version_str) ?
899 thinkpad_id.bios_version_str : "unknown",
900 (thinkpad_id.ec_version_str) ?
901 thinkpad_id.ec_version_str : "unknown");
902
903 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200904 printk(TPACPI_INFO "%s %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300905 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
906 "IBM" : ((thinkpad_id.vendor ==
907 PCI_VENDOR_ID_LENOVO) ?
908 "Lenovo" : "Unknown vendor"),
909 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return 0;
912}
913
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300914static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 int len = 0;
917
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200918 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
919 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return len;
922}
923
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300924static struct ibm_struct thinkpad_acpi_driver_data = {
925 .name = "driver",
926 .read = thinkpad_acpi_driver_read,
927};
928
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300929/*************************************************************************
930 * Hotkey subdriver
931 */
932
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200933enum { /* hot key scan codes (derived from ACPI DSDT) */
934 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
935 TP_ACPI_HOTKEYSCAN_FNF2,
936 TP_ACPI_HOTKEYSCAN_FNF3,
937 TP_ACPI_HOTKEYSCAN_FNF4,
938 TP_ACPI_HOTKEYSCAN_FNF5,
939 TP_ACPI_HOTKEYSCAN_FNF6,
940 TP_ACPI_HOTKEYSCAN_FNF7,
941 TP_ACPI_HOTKEYSCAN_FNF8,
942 TP_ACPI_HOTKEYSCAN_FNF9,
943 TP_ACPI_HOTKEYSCAN_FNF10,
944 TP_ACPI_HOTKEYSCAN_FNF11,
945 TP_ACPI_HOTKEYSCAN_FNF12,
946 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
947 TP_ACPI_HOTKEYSCAN_FNINSERT,
948 TP_ACPI_HOTKEYSCAN_FNDELETE,
949 TP_ACPI_HOTKEYSCAN_FNHOME,
950 TP_ACPI_HOTKEYSCAN_FNEND,
951 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
952 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
953 TP_ACPI_HOTKEYSCAN_FNSPACE,
954 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
955 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
956 TP_ACPI_HOTKEYSCAN_MUTE,
957 TP_ACPI_HOTKEYSCAN_THINKPAD,
958};
959
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200960enum { /* Keys available through NVRAM polling */
961 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
962 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
963};
964
965enum { /* Positions of some of the keys in hotkey masks */
966 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
967 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
968 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
969 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
970 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
971 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
972 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
973 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
974 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
975 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
976 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
977};
978
979enum { /* NVRAM to ACPI HKEY group map */
980 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
981 TP_ACPI_HKEY_ZOOM_MASK |
982 TP_ACPI_HKEY_DISPSWTCH_MASK |
983 TP_ACPI_HKEY_HIBERNATE_MASK,
984 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
985 TP_ACPI_HKEY_BRGHTDWN_MASK,
986 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
987 TP_ACPI_HKEY_VOLDWN_MASK |
988 TP_ACPI_HKEY_MUTE_MASK,
989};
990
991#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
992struct tp_nvram_state {
993 u16 thinkpad_toggle:1;
994 u16 zoom_toggle:1;
995 u16 display_toggle:1;
996 u16 thinklight_toggle:1;
997 u16 hibernate_toggle:1;
998 u16 displayexp_toggle:1;
999 u16 display_state:1;
1000 u16 brightness_toggle:1;
1001 u16 volume_toggle:1;
1002 u16 mute:1;
1003
1004 u8 brightness_level;
1005 u8 volume_level;
1006};
1007
1008static struct task_struct *tpacpi_hotkey_task;
1009static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1010static int hotkey_poll_freq = 10; /* Hz */
1011static struct mutex hotkey_thread_mutex;
1012static struct mutex hotkey_thread_data_mutex;
1013static unsigned int hotkey_config_change;
1014
1015#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1016
1017#define hotkey_source_mask 0U
1018
1019#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1020
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001021static struct mutex hotkey_mutex;
1022
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001023static enum { /* Reasons for waking up */
1024 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1025 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1026 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1027} hotkey_wakeup_reason;
1028
1029static int hotkey_autosleep_ack;
1030
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001031static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001032static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001033static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001034static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001035static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001036
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001037static unsigned int hotkey_report_mode;
1038
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001039static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001040
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001041static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001042
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001043#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1044#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001045 do { \
1046 mutex_lock(&hotkey_thread_data_mutex); \
1047 hotkey_config_change++; \
1048 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001049#define HOTKEY_CONFIG_CRITICAL_END \
1050 mutex_unlock(&hotkey_thread_data_mutex);
1051#else
1052#define HOTKEY_CONFIG_CRITICAL_START
1053#define HOTKEY_CONFIG_CRITICAL_END
1054#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1055
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001056static int hotkey_get_wlsw(int *status)
1057{
1058 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1059 return -EIO;
1060 return 0;
1061}
1062
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001063/*
1064 * Call with hotkey_mutex held
1065 */
1066static int hotkey_mask_get(void)
1067{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001068 u32 m = 0;
1069
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001070 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001071 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001072 return -EIO;
1073 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001074 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001075
1076 return 0;
1077}
1078
1079/*
1080 * Call with hotkey_mutex held
1081 */
1082static int hotkey_mask_set(u32 mask)
1083{
1084 int i;
1085 int rc = 0;
1086
1087 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001088 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001089 for (i = 0; i < 32; i++) {
1090 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001091 /* enable in firmware mask only keys not in NVRAM
1092 * mode, but enable the key in the cached hotkey_mask
1093 * regardless of mode, or the key will end up
1094 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001095 if (!acpi_evalf(hkey_handle,
1096 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001097 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001098 rc = -EIO;
1099 break;
1100 } else {
1101 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1102 }
1103 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001104 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001105
1106 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001107 if (!hotkey_mask_get() && !rc &&
1108 (hotkey_mask & ~hotkey_source_mask) !=
1109 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001110 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001111 "requested hot key mask 0x%08x, but "
1112 "firmware forced it to 0x%08x\n",
1113 mask, hotkey_mask);
1114 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001115 } else {
1116#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1117 HOTKEY_CONFIG_CRITICAL_START
1118 hotkey_mask = mask & hotkey_source_mask;
1119 HOTKEY_CONFIG_CRITICAL_END
1120 hotkey_mask_get();
1121 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001122 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001123 "requested hot key mask 0x%08x, "
1124 "forced to 0x%08x (NVRAM poll mask is "
1125 "0x%08x): no firmware mask support\n",
1126 mask, hotkey_mask, hotkey_source_mask);
1127 }
1128#else
1129 hotkey_mask_get();
1130 rc = -ENXIO;
1131#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001132 }
1133
1134 return rc;
1135}
1136
1137static int hotkey_status_get(int *status)
1138{
1139 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1140 return -EIO;
1141
1142 return 0;
1143}
1144
1145static int hotkey_status_set(int status)
1146{
1147 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1148 return -EIO;
1149
1150 return 0;
1151}
1152
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001153static void tpacpi_input_send_radiosw(void)
1154{
1155 int wlsw;
1156
1157 mutex_lock(&tpacpi_inputdev_send_mutex);
1158
1159 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1160 input_report_switch(tpacpi_inputdev,
1161 SW_RADIO, !!wlsw);
1162 input_sync(tpacpi_inputdev);
1163 }
1164
1165 mutex_unlock(&tpacpi_inputdev_send_mutex);
1166}
1167
1168static void tpacpi_input_send_key(unsigned int scancode)
1169{
1170 unsigned int keycode;
1171
1172 keycode = hotkey_keycode_map[scancode];
1173
1174 if (keycode != KEY_RESERVED) {
1175 mutex_lock(&tpacpi_inputdev_send_mutex);
1176
1177 input_report_key(tpacpi_inputdev, keycode, 1);
1178 if (keycode == KEY_UNKNOWN)
1179 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1180 scancode);
1181 input_sync(tpacpi_inputdev);
1182
1183 input_report_key(tpacpi_inputdev, keycode, 0);
1184 if (keycode == KEY_UNKNOWN)
1185 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1186 scancode);
1187 input_sync(tpacpi_inputdev);
1188
1189 mutex_unlock(&tpacpi_inputdev_send_mutex);
1190 }
1191}
1192
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001193#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1194static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1195
1196static void tpacpi_hotkey_send_key(unsigned int scancode)
1197{
1198 tpacpi_input_send_key(scancode);
1199 if (hotkey_report_mode < 2) {
1200 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1201 0x80, 0x1001 + scancode);
1202 }
1203}
1204
1205static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1206{
1207 u8 d;
1208
1209 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1210 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1211 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1212 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1213 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1214 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1215 }
1216 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1217 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1218 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1219 }
1220 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1221 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1222 n->displayexp_toggle =
1223 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1224 }
1225 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1226 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1227 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1228 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1229 n->brightness_toggle =
1230 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1231 }
1232 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1233 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1234 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1235 >> TP_NVRAM_POS_LEVEL_VOLUME;
1236 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1237 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1238 }
1239}
1240
1241#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001242 do { \
1243 if ((mask & (1 << __scancode)) && \
1244 oldn->__member != newn->__member) \
1245 tpacpi_hotkey_send_key(__scancode); \
1246 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001247
1248#define TPACPI_MAY_SEND_KEY(__scancode) \
1249 do { if (mask & (1 << __scancode)) \
1250 tpacpi_hotkey_send_key(__scancode); } while (0)
1251
1252static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001253 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001254 u32 mask)
1255{
1256 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1257 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1258 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1259 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1260
1261 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1262
1263 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1264
1265 /* handle volume */
1266 if (oldn->volume_toggle != newn->volume_toggle) {
1267 if (oldn->mute != newn->mute) {
1268 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1269 }
1270 if (oldn->volume_level > newn->volume_level) {
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1272 } else if (oldn->volume_level < newn->volume_level) {
1273 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1274 } else if (oldn->mute == newn->mute) {
1275 /* repeated key presses that didn't change state */
1276 if (newn->mute) {
1277 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1278 } else if (newn->volume_level != 0) {
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1280 } else {
1281 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1282 }
1283 }
1284 }
1285
1286 /* handle brightness */
1287 if (oldn->brightness_toggle != newn->brightness_toggle) {
1288 if (oldn->brightness_level < newn->brightness_level) {
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1290 } else if (oldn->brightness_level > newn->brightness_level) {
1291 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1292 } else {
1293 /* repeated key presses that didn't change state */
1294 if (newn->brightness_level != 0) {
1295 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1296 } else {
1297 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1298 }
1299 }
1300 }
1301}
1302
1303#undef TPACPI_COMPARE_KEY
1304#undef TPACPI_MAY_SEND_KEY
1305
1306static int hotkey_kthread(void *data)
1307{
1308 struct tp_nvram_state s[2];
1309 u32 mask;
1310 unsigned int si, so;
1311 unsigned long t;
1312 unsigned int change_detector, must_reset;
1313
1314 mutex_lock(&hotkey_thread_mutex);
1315
1316 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1317 goto exit;
1318
1319 set_freezable();
1320
1321 so = 0;
1322 si = 1;
1323 t = 0;
1324
1325 /* Initial state for compares */
1326 mutex_lock(&hotkey_thread_data_mutex);
1327 change_detector = hotkey_config_change;
1328 mask = hotkey_source_mask & hotkey_mask;
1329 mutex_unlock(&hotkey_thread_data_mutex);
1330 hotkey_read_nvram(&s[so], mask);
1331
1332 while (!kthread_should_stop() && hotkey_poll_freq) {
1333 if (t == 0)
1334 t = 1000/hotkey_poll_freq;
1335 t = msleep_interruptible(t);
1336 if (unlikely(kthread_should_stop()))
1337 break;
1338 must_reset = try_to_freeze();
1339 if (t > 0 && !must_reset)
1340 continue;
1341
1342 mutex_lock(&hotkey_thread_data_mutex);
1343 if (must_reset || hotkey_config_change != change_detector) {
1344 /* forget old state on thaw or config change */
1345 si = so;
1346 t = 0;
1347 change_detector = hotkey_config_change;
1348 }
1349 mask = hotkey_source_mask & hotkey_mask;
1350 mutex_unlock(&hotkey_thread_data_mutex);
1351
1352 if (likely(mask)) {
1353 hotkey_read_nvram(&s[si], mask);
1354 if (likely(si != so)) {
1355 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001356 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001357 }
1358 }
1359
1360 so = si;
1361 si ^= 1;
1362 }
1363
1364exit:
1365 mutex_unlock(&hotkey_thread_mutex);
1366 return 0;
1367}
1368
1369static void hotkey_poll_stop_sync(void)
1370{
1371 if (tpacpi_hotkey_task) {
1372 if (frozen(tpacpi_hotkey_task) ||
1373 freezing(tpacpi_hotkey_task))
1374 thaw_process(tpacpi_hotkey_task);
1375
1376 kthread_stop(tpacpi_hotkey_task);
1377 tpacpi_hotkey_task = NULL;
1378 mutex_lock(&hotkey_thread_mutex);
1379 /* at this point, the thread did exit */
1380 mutex_unlock(&hotkey_thread_mutex);
1381 }
1382}
1383
1384/* call with hotkey_mutex held */
1385static void hotkey_poll_setup(int may_warn)
1386{
1387 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1388 hotkey_poll_freq > 0 &&
1389 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1390 if (!tpacpi_hotkey_task) {
1391 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001392 NULL,
1393 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001394 if (IS_ERR(tpacpi_hotkey_task)) {
1395 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001396 printk(TPACPI_ERR
1397 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001398 "for hotkey polling\n");
1399 }
1400 }
1401 } else {
1402 hotkey_poll_stop_sync();
1403 if (may_warn &&
1404 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001405 printk(TPACPI_NOTICE
1406 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001407 "which is currently disabled\n",
1408 hotkey_source_mask);
1409 }
1410 }
1411}
1412
1413static void hotkey_poll_setup_safe(int may_warn)
1414{
1415 mutex_lock(&hotkey_mutex);
1416 hotkey_poll_setup(may_warn);
1417 mutex_unlock(&hotkey_mutex);
1418}
1419
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001420#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1421
1422static void hotkey_poll_setup_safe(int __unused)
1423{
1424}
1425
1426#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1427
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001428static int hotkey_inputdev_open(struct input_dev *dev)
1429{
1430 switch (tpacpi_lifecycle) {
1431 case TPACPI_LIFE_INIT:
1432 /*
1433 * hotkey_init will call hotkey_poll_setup_safe
1434 * at the appropriate moment
1435 */
1436 return 0;
1437 case TPACPI_LIFE_EXITING:
1438 return -EBUSY;
1439 case TPACPI_LIFE_RUNNING:
1440 hotkey_poll_setup_safe(0);
1441 return 0;
1442 }
1443
1444 /* Should only happen if tpacpi_lifecycle is corrupt */
1445 BUG();
1446 return -EBUSY;
1447}
1448
1449static void hotkey_inputdev_close(struct input_dev *dev)
1450{
1451 /* disable hotkey polling when possible */
1452 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1453 hotkey_poll_setup_safe(0);
1454}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001455
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001456/* sysfs hotkey enable ------------------------------------------------- */
1457static ssize_t hotkey_enable_show(struct device *dev,
1458 struct device_attribute *attr,
1459 char *buf)
1460{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001461 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001462
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001463 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001464 if (res)
1465 return res;
1466
1467 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1468}
1469
1470static ssize_t hotkey_enable_store(struct device *dev,
1471 struct device_attribute *attr,
1472 const char *buf, size_t count)
1473{
1474 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001475 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001476
1477 if (parse_strtoul(buf, 1, &t))
1478 return -EINVAL;
1479
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001480 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001481
1482 return (res) ? res : count;
1483}
1484
1485static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001486 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001487 hotkey_enable_show, hotkey_enable_store);
1488
1489/* sysfs hotkey mask --------------------------------------------------- */
1490static ssize_t hotkey_mask_show(struct device *dev,
1491 struct device_attribute *attr,
1492 char *buf)
1493{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001494 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001495
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001496 if (mutex_lock_interruptible(&hotkey_mutex))
1497 return -ERESTARTSYS;
1498 res = hotkey_mask_get();
1499 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001500
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001501 return (res)?
1502 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001503}
1504
1505static ssize_t hotkey_mask_store(struct device *dev,
1506 struct device_attribute *attr,
1507 const char *buf, size_t count)
1508{
1509 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001510 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001511
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001512 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001513 return -EINVAL;
1514
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001515 if (mutex_lock_interruptible(&hotkey_mutex))
1516 return -ERESTARTSYS;
1517
1518 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001519
1520#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1521 hotkey_poll_setup(1);
1522#endif
1523
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001524 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001525
1526 return (res) ? res : count;
1527}
1528
1529static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001530 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001531 hotkey_mask_show, hotkey_mask_store);
1532
1533/* sysfs hotkey bios_enabled ------------------------------------------- */
1534static ssize_t hotkey_bios_enabled_show(struct device *dev,
1535 struct device_attribute *attr,
1536 char *buf)
1537{
1538 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1539}
1540
1541static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001542 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001543
1544/* sysfs hotkey bios_mask ---------------------------------------------- */
1545static ssize_t hotkey_bios_mask_show(struct device *dev,
1546 struct device_attribute *attr,
1547 char *buf)
1548{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001549 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001550}
1551
1552static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001553 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001554
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001555/* sysfs hotkey all_mask ----------------------------------------------- */
1556static ssize_t hotkey_all_mask_show(struct device *dev,
1557 struct device_attribute *attr,
1558 char *buf)
1559{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001560 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1561 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001562}
1563
1564static struct device_attribute dev_attr_hotkey_all_mask =
1565 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1566
1567/* sysfs hotkey recommended_mask --------------------------------------- */
1568static ssize_t hotkey_recommended_mask_show(struct device *dev,
1569 struct device_attribute *attr,
1570 char *buf)
1571{
1572 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001573 (hotkey_all_mask | hotkey_source_mask)
1574 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001575}
1576
1577static struct device_attribute dev_attr_hotkey_recommended_mask =
1578 __ATTR(hotkey_recommended_mask, S_IRUGO,
1579 hotkey_recommended_mask_show, NULL);
1580
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001581#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1582
1583/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1584static ssize_t hotkey_source_mask_show(struct device *dev,
1585 struct device_attribute *attr,
1586 char *buf)
1587{
1588 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1589}
1590
1591static ssize_t hotkey_source_mask_store(struct device *dev,
1592 struct device_attribute *attr,
1593 const char *buf, size_t count)
1594{
1595 unsigned long t;
1596
1597 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1598 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1599 return -EINVAL;
1600
1601 if (mutex_lock_interruptible(&hotkey_mutex))
1602 return -ERESTARTSYS;
1603
1604 HOTKEY_CONFIG_CRITICAL_START
1605 hotkey_source_mask = t;
1606 HOTKEY_CONFIG_CRITICAL_END
1607
1608 hotkey_poll_setup(1);
1609
1610 mutex_unlock(&hotkey_mutex);
1611
1612 return count;
1613}
1614
1615static struct device_attribute dev_attr_hotkey_source_mask =
1616 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1617 hotkey_source_mask_show, hotkey_source_mask_store);
1618
1619/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1620static ssize_t hotkey_poll_freq_show(struct device *dev,
1621 struct device_attribute *attr,
1622 char *buf)
1623{
1624 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1625}
1626
1627static ssize_t hotkey_poll_freq_store(struct device *dev,
1628 struct device_attribute *attr,
1629 const char *buf, size_t count)
1630{
1631 unsigned long t;
1632
1633 if (parse_strtoul(buf, 25, &t))
1634 return -EINVAL;
1635
1636 if (mutex_lock_interruptible(&hotkey_mutex))
1637 return -ERESTARTSYS;
1638
1639 hotkey_poll_freq = t;
1640
1641 hotkey_poll_setup(1);
1642 mutex_unlock(&hotkey_mutex);
1643
1644 return count;
1645}
1646
1647static struct device_attribute dev_attr_hotkey_poll_freq =
1648 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1649 hotkey_poll_freq_show, hotkey_poll_freq_store);
1650
1651#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1652
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001653/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001654static ssize_t hotkey_radio_sw_show(struct device *dev,
1655 struct device_attribute *attr,
1656 char *buf)
1657{
1658 int res, s;
1659 res = hotkey_get_wlsw(&s);
1660 if (res < 0)
1661 return res;
1662
1663 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1664}
1665
1666static struct device_attribute dev_attr_hotkey_radio_sw =
1667 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1668
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001669static void hotkey_radio_sw_notify_change(void)
1670{
1671 if (tp_features.hotkey_wlsw)
1672 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1673 "hotkey_radio_sw");
1674}
1675
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001676/* sysfs hotkey report_mode -------------------------------------------- */
1677static ssize_t hotkey_report_mode_show(struct device *dev,
1678 struct device_attribute *attr,
1679 char *buf)
1680{
1681 return snprintf(buf, PAGE_SIZE, "%d\n",
1682 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1683}
1684
1685static struct device_attribute dev_attr_hotkey_report_mode =
1686 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1687
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001688/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001689static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1690 struct device_attribute *attr,
1691 char *buf)
1692{
1693 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1694}
1695
1696static struct device_attribute dev_attr_hotkey_wakeup_reason =
1697 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1698
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001699static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001700{
1701 if (tp_features.hotkey_mask)
1702 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1703 "wakeup_reason");
1704}
1705
1706/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001707static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1708 struct device_attribute *attr,
1709 char *buf)
1710{
1711 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1712}
1713
1714static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1715 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1716 hotkey_wakeup_hotunplug_complete_show, NULL);
1717
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001718static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001719{
1720 if (tp_features.hotkey_mask)
1721 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1722 "wakeup_hotunplug_complete");
1723}
1724
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001725/* --------------------------------------------------------------------- */
1726
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001727static struct attribute *hotkey_attributes[] __initdata = {
1728 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001729 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001730 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001731#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1732 &dev_attr_hotkey_mask.attr,
1733 &dev_attr_hotkey_all_mask.attr,
1734 &dev_attr_hotkey_recommended_mask.attr,
1735 &dev_attr_hotkey_source_mask.attr,
1736 &dev_attr_hotkey_poll_freq.attr,
1737#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001738};
1739
1740static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001741 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001742#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1743 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001744 &dev_attr_hotkey_all_mask.attr,
1745 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001746#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001747 &dev_attr_hotkey_wakeup_reason.attr,
1748 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001749};
1750
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001751static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001752{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001753 /* Requirements for changing the default keymaps:
1754 *
1755 * 1. Many of the keys are mapped to KEY_RESERVED for very
1756 * good reasons. Do not change them unless you have deep
1757 * knowledge on the IBM and Lenovo ThinkPad firmware for
1758 * the various ThinkPad models. The driver behaves
1759 * differently for KEY_RESERVED: such keys have their
1760 * hot key mask *unset* in mask_recommended, and also
1761 * in the initial hot key mask programmed into the
1762 * firmware at driver load time, which means the firm-
1763 * ware may react very differently if you change them to
1764 * something else;
1765 *
1766 * 2. You must be subscribed to the linux-thinkpad and
1767 * ibm-acpi-devel mailing lists, and you should read the
1768 * list archives since 2007 if you want to change the
1769 * keymaps. This requirement exists so that you will
1770 * know the past history of problems with the thinkpad-
1771 * acpi driver keymaps, and also that you will be
1772 * listening to any bug reports;
1773 *
1774 * 3. Do not send thinkpad-acpi specific patches directly to
1775 * for merging, *ever*. Send them to the linux-acpi
1776 * mailinglist for comments. Merging is to be done only
1777 * through acpi-test and the ACPI maintainer.
1778 *
1779 * If the above is too much to ask, don't change the keymap.
1780 * Ask the thinkpad-acpi maintainer to do it, instead.
1781 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001782 static u16 ibm_keycode_map[] __initdata = {
1783 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1784 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1785 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1786 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001787
1788 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001789 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1790 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1791 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001792
1793 /* brightness: firmware always reacts to them, unless
1794 * X.org did some tricks in the radeon BIOS scratch
1795 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001796 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001797 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001798
1799 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001800 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001801
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001802 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1803 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001804
1805 /* Volume: firmware always react to it and reprograms
1806 * the built-in *extra* mixer. Never map it to control
1807 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001808 KEY_RESERVED, /* 0x14: VOLUME UP */
1809 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1810 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001811
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001812 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001813
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001814 /* (assignments unknown, please report if found) */
1815 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1816 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1817 };
1818 static u16 lenovo_keycode_map[] __initdata = {
1819 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1820 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1821 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1822 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001823
1824 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001825 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1826 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1827 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001828
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001829 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001830 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001831
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001832 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001833
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001834 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1835 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001836
1837 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1838 * react to it and reprograms the built-in *extra* mixer.
1839 * Never map it to control another mixer by default.
1840 *
1841 * T60?, T61, R60?, R61: firmware and EC tries to send
1842 * these over the regular keyboard, so these are no-ops,
1843 * but there are still weird bugs re. MUTE, so do not
1844 * change unless you get test reports from all Lenovo
1845 * models. May cause the BIOS to interfere with the
1846 * HDA mixer.
1847 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001848 KEY_RESERVED, /* 0x14: VOLUME UP */
1849 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1850 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001851
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001852 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001853
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001854 /* (assignments unknown, please report if found) */
1855 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1856 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1857 };
1858
1859#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1860#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1861#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1862
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001863 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001864 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001865 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001866
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001867 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1868
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001869 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001870 BUG_ON(tpacpi_inputdev->open != NULL ||
1871 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001872
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001873 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001874 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001875
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001876#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1877 mutex_init(&hotkey_thread_mutex);
1878 mutex_init(&hotkey_thread_data_mutex);
1879#endif
1880
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001881 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001882 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001883
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001884 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001885 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001886
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001887 if (tp_features.hotkey) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001888 hotkey_dev_attributes = create_attr_set(12, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001889 if (!hotkey_dev_attributes)
1890 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001891 res = add_many_to_attr_set(hotkey_dev_attributes,
1892 hotkey_attributes,
1893 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001894 if (res)
1895 return res;
1896
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001897 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001898 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1899 for HKEY interface version 0x100 */
1900 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1901 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001902 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001903 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001904 printk(TPACPI_ERR "please report this to %s\n",
1905 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001906 } else {
1907 /*
1908 * MHKV 0x100 in A31, R40, R40e,
1909 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001910 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001911 tp_features.hotkey_mask = 1;
1912 }
1913 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001914
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001915 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001916 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001917
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001918 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001919 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001920 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001921 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001922 "missing MHKA handler, "
1923 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001924 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001925 /* FN+F12, FN+F4, FN+F3 */
1926 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001927 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001928 }
1929
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001930 /* hotkey_source_mask *must* be zero for
1931 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001932 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001933 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001934 res = hotkey_mask_get();
1935 hotkey_orig_mask = hotkey_mask;
1936 if (!res) {
1937 res = add_many_to_attr_set(
1938 hotkey_dev_attributes,
1939 hotkey_mask_attributes,
1940 ARRAY_SIZE(hotkey_mask_attributes));
1941 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001942 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001943
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001944#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1945 if (tp_features.hotkey_mask) {
1946 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1947 & ~hotkey_all_mask;
1948 } else {
1949 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1950 }
1951
1952 vdbg_printk(TPACPI_DBG_INIT,
1953 "hotkey source mask 0x%08x, polling freq %d\n",
1954 hotkey_source_mask, hotkey_poll_freq);
1955#endif
1956
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001957 /* Not all thinkpads have a hardware radio switch */
1958 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1959 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001960 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001961 "radio switch found; radios are %s\n",
1962 enabled(status, 0));
1963 res = add_to_attr_set(hotkey_dev_attributes,
1964 &dev_attr_hotkey_radio_sw.attr);
1965 }
1966
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001967 if (!res)
1968 res = register_attr_set_with_sysfs(
1969 hotkey_dev_attributes,
1970 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001971 if (res)
1972 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001973
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001974 /* Set up key map */
1975
1976 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1977 GFP_KERNEL);
1978 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001979 printk(TPACPI_ERR
1980 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001981 return -ENOMEM;
1982 }
1983
1984 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1985 dbg_printk(TPACPI_DBG_INIT,
1986 "using Lenovo default hot key map\n");
1987 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1988 TPACPI_HOTKEY_MAP_SIZE);
1989 } else {
1990 dbg_printk(TPACPI_DBG_INIT,
1991 "using IBM default hot key map\n");
1992 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1993 TPACPI_HOTKEY_MAP_SIZE);
1994 }
1995
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001996 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1997 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1998 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001999 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2000 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2001 tpacpi_inputdev->keycode = hotkey_keycode_map;
2002 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002003 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2004 set_bit(hotkey_keycode_map[i],
2005 tpacpi_inputdev->keybit);
2006 } else {
2007 if (i < sizeof(hotkey_reserved_mask)*8)
2008 hotkey_reserved_mask |= 1 << i;
2009 }
2010 }
2011
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002012 if (tp_features.hotkey_wlsw) {
2013 set_bit(EV_SW, tpacpi_inputdev->evbit);
2014 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2015 }
2016
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002017 dbg_printk(TPACPI_DBG_INIT,
2018 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002019 res = hotkey_status_set(1);
2020 if (res)
2021 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002022 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2023 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002024 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002025 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002026 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002027
2028 dbg_printk(TPACPI_DBG_INIT,
2029 "legacy hot key reporting over procfs %s\n",
2030 (hotkey_report_mode < 2) ?
2031 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002032
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002033 tpacpi_inputdev->open = &hotkey_inputdev_open;
2034 tpacpi_inputdev->close = &hotkey_inputdev_close;
2035
2036 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002037 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002038 }
2039
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002040 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002041}
2042
2043static void hotkey_exit(void)
2044{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002045#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2046 hotkey_poll_stop_sync();
2047#endif
2048
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002049 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002050 dbg_printk(TPACPI_DBG_EXIT,
2051 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002052 /* no short-circuit boolean operator below! */
2053 if ((hotkey_mask_set(hotkey_orig_mask) |
2054 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002055 printk(TPACPI_ERR
2056 "failed to restore hot key mask "
2057 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002058 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002059
2060 if (hotkey_dev_attributes) {
2061 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2062 hotkey_dev_attributes = NULL;
2063 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002064}
2065
2066static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2067{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002068 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002069 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002070 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002071 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002072 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002073
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002074 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002075 printk(TPACPI_ERR
2076 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002077 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002078 acpi_bus_generate_netlink_event(
2079 ibm->acpi->device->pnp.device_class,
2080 ibm->acpi->device->dev.bus_id,
2081 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002082 return;
2083 }
2084
2085 while (1) {
2086 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002087 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002088 return;
2089 }
2090
2091 if (hkey == 0) {
2092 /* queue empty */
2093 return;
2094 }
2095
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002096 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002097 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002098 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002099
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002100 switch (hkey >> 12) {
2101 case 1:
2102 /* 0x1000-0x1FFF: key presses */
2103 scancode = hkey & 0xfff;
2104 if (scancode > 0 && scancode < 0x21) {
2105 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002106 if (!(hotkey_source_mask & (1 << scancode))) {
2107 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002108 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002109 } else {
2110 ignore_acpi_ev = 1;
2111 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002112 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002113 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002114 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002115 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002116 case 2:
2117 /* Wakeup reason */
2118 switch (hkey) {
2119 case 0x2304: /* suspend, undock */
2120 case 0x2404: /* hibernation, undock */
2121 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2122 ignore_acpi_ev = 1;
2123 break;
2124 case 0x2305: /* suspend, bay eject */
2125 case 0x2405: /* hibernation, bay eject */
2126 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2127 ignore_acpi_ev = 1;
2128 break;
2129 default:
2130 unk_ev = 1;
2131 }
2132 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2133 printk(TPACPI_INFO
2134 "woke up due to a hot-unplug "
2135 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002136 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002137 }
2138 break;
2139 case 3:
2140 /* bay-related wakeups */
2141 if (hkey == 0x3003) {
2142 hotkey_autosleep_ack = 1;
2143 printk(TPACPI_INFO
2144 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002145 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002146 } else {
2147 unk_ev = 1;
2148 }
2149 break;
2150 case 4:
2151 /* dock-related wakeups */
2152 if (hkey == 0x4003) {
2153 hotkey_autosleep_ack = 1;
2154 printk(TPACPI_INFO
2155 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002156 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002157 } else {
2158 unk_ev = 1;
2159 }
2160 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002161 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002162 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002163 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002164 case 0x5010: /* Lenovo new BIOS: brightness changed */
2165 case 0x5009: /* X61t: swivel up (tablet mode) */
2166 case 0x500a: /* X61t: swivel down (normal mode) */
2167 case 0x500b: /* X61t: tablet pen inserted into bay */
2168 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002169 break;
2170 case 0x5001:
2171 case 0x5002:
2172 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002173 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002174 break;
2175 default:
2176 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002177 }
2178 break;
2179 case 7:
2180 /* 0x7000-0x7FFF: misc */
2181 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2182 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002183 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002184 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002185 break;
2186 }
2187 /* fallthrough to default */
2188 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002189 unk_ev = 1;
2190 }
2191 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002192 printk(TPACPI_NOTICE
2193 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002194 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002195
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002196 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002197 if (!ignore_acpi_ev &&
2198 (send_acpi_ev || hotkey_report_mode < 2)) {
2199 acpi_bus_generate_proc_event(ibm->acpi->device,
2200 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002201 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002202
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002203 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002204 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002205 acpi_bus_generate_netlink_event(
2206 ibm->acpi->device->pnp.device_class,
2207 ibm->acpi->device->dev.bus_id,
2208 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002209 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002210 }
2211}
2212
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002213static void hotkey_suspend(pm_message_t state)
2214{
2215 /* Do these on suspend, we get the events on early resume! */
2216 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2217 hotkey_autosleep_ack = 0;
2218}
2219
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002220static void hotkey_resume(void)
2221{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002222 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002223 printk(TPACPI_ERR
2224 "error while trying to read hot key mask "
2225 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002226 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002227 hotkey_radio_sw_notify_change();
2228 hotkey_wakeup_reason_notify_change();
2229 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002230 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002231}
2232
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002233/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002234static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002236 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 int len = 0;
2238
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002239 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002240 len += sprintf(p + len, "status:\t\tnot supported\n");
2241 return len;
2242 }
2243
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002244 if (mutex_lock_interruptible(&hotkey_mutex))
2245 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002246 res = hotkey_status_get(&status);
2247 if (!res)
2248 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002249 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002250 if (res)
2251 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002254 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002255 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 len += sprintf(p + len,
2257 "commands:\tenable, disable, reset, <mask>\n");
2258 } else {
2259 len += sprintf(p + len, "mask:\t\tnot supported\n");
2260 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2261 }
2262
2263 return len;
2264}
2265
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002266static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002268 int res, status;
2269 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002272 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return -ENODEV;
2274
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002275 if (mutex_lock_interruptible(&hotkey_mutex))
2276 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002277
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002278 status = -1;
2279 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002280
2281 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 while ((cmd = next_cmd(&buf))) {
2283 if (strlencmp(cmd, "enable") == 0) {
2284 status = 1;
2285 } else if (strlencmp(cmd, "disable") == 0) {
2286 status = 0;
2287 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002288 status = hotkey_orig_status;
2289 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2291 /* mask set */
2292 } else if (sscanf(cmd, "%x", &mask) == 1) {
2293 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002294 } else {
2295 res = -EINVAL;
2296 goto errexit;
2297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002299 if (status != -1)
2300 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002302 if (!res && mask != hotkey_mask)
2303 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002305errexit:
2306 mutex_unlock(&hotkey_mutex);
2307 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002310static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002311 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002312 {"", 0},
2313};
2314
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002315static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002316 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002317 .notify = hotkey_notify,
2318 .handle = &hkey_handle,
2319 .type = ACPI_DEVICE_NOTIFY,
2320};
2321
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002322static struct ibm_struct hotkey_driver_data = {
2323 .name = "hotkey",
2324 .read = hotkey_read,
2325 .write = hotkey_write,
2326 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002327 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002328 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002329 .acpi = &ibm_hotkey_acpidriver,
2330};
2331
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002332/*************************************************************************
2333 * Bluetooth subdriver
2334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002336enum {
2337 /* ACPI GBDC/SBDC bits */
2338 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2339 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2340 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2341};
2342
2343static int bluetooth_get_radiosw(void);
2344static int bluetooth_set_radiosw(int radio_on);
2345
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002346/* sysfs bluetooth enable ---------------------------------------------- */
2347static ssize_t bluetooth_enable_show(struct device *dev,
2348 struct device_attribute *attr,
2349 char *buf)
2350{
2351 int status;
2352
2353 status = bluetooth_get_radiosw();
2354 if (status < 0)
2355 return status;
2356
2357 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2358}
2359
2360static ssize_t bluetooth_enable_store(struct device *dev,
2361 struct device_attribute *attr,
2362 const char *buf, size_t count)
2363{
2364 unsigned long t;
2365 int res;
2366
2367 if (parse_strtoul(buf, 1, &t))
2368 return -EINVAL;
2369
2370 res = bluetooth_set_radiosw(t);
2371
2372 return (res) ? res : count;
2373}
2374
2375static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002376 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002377 bluetooth_enable_show, bluetooth_enable_store);
2378
2379/* --------------------------------------------------------------------- */
2380
2381static struct attribute *bluetooth_attributes[] = {
2382 &dev_attr_bluetooth_enable.attr,
2383 NULL
2384};
2385
2386static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002387 .attrs = bluetooth_attributes,
2388};
2389
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002390static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002392 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002393 int status = 0;
2394
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002395 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2396
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002397 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002398
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002399 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2400 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002401 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002402 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002404 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2405 str_supported(tp_features.bluetooth),
2406 status);
2407
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002408 if (tp_features.bluetooth) {
2409 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2410 /* no bluetooth hardware present in system */
2411 tp_features.bluetooth = 0;
2412 dbg_printk(TPACPI_DBG_INIT,
2413 "bluetooth hardware not installed\n");
2414 } else {
2415 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2416 &bluetooth_attr_group);
2417 if (res)
2418 return res;
2419 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002420 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002421
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002422 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002425static void bluetooth_exit(void)
2426{
2427 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2428 &bluetooth_attr_group);
2429}
2430
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002431static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 int status;
2434
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002435 if (!tp_features.bluetooth)
2436 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002438 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2439 return -EIO;
2440
2441 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2442}
2443
2444static int bluetooth_set_radiosw(int radio_on)
2445{
2446 int status;
2447
2448 if (!tp_features.bluetooth)
2449 return -ENODEV;
2450
2451 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2452 return -EIO;
2453 if (radio_on)
2454 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2455 else
2456 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2457 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2458 return -EIO;
2459
2460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002463/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002464static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002467 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002469 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002472 len += sprintf(p + len, "status:\t\t%s\n",
2473 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 len += sprintf(p + len, "commands:\tenable, disable\n");
2475 }
2476
2477 return len;
2478}
2479
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002480static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002484 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002485 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 while ((cmd = next_cmd(&buf))) {
2488 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002489 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002491 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 } else
2493 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return 0;
2497}
2498
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002499static struct ibm_struct bluetooth_driver_data = {
2500 .name = "bluetooth",
2501 .read = bluetooth_read,
2502 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002503 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002504};
2505
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002506/*************************************************************************
2507 * Wan subdriver
2508 */
2509
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002510enum {
2511 /* ACPI GWAN/SWAN bits */
2512 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2513 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2514 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2515};
2516
2517static int wan_get_radiosw(void);
2518static int wan_set_radiosw(int radio_on);
2519
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002520/* sysfs wan enable ---------------------------------------------------- */
2521static ssize_t wan_enable_show(struct device *dev,
2522 struct device_attribute *attr,
2523 char *buf)
2524{
2525 int status;
2526
2527 status = wan_get_radiosw();
2528 if (status < 0)
2529 return status;
2530
2531 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2532}
2533
2534static ssize_t wan_enable_store(struct device *dev,
2535 struct device_attribute *attr,
2536 const char *buf, size_t count)
2537{
2538 unsigned long t;
2539 int res;
2540
2541 if (parse_strtoul(buf, 1, &t))
2542 return -EINVAL;
2543
2544 res = wan_set_radiosw(t);
2545
2546 return (res) ? res : count;
2547}
2548
2549static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002550 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002551 wan_enable_show, wan_enable_store);
2552
2553/* --------------------------------------------------------------------- */
2554
2555static struct attribute *wan_attributes[] = {
2556 &dev_attr_wan_enable.attr,
2557 NULL
2558};
2559
2560static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002561 .attrs = wan_attributes,
2562};
2563
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002564static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002565{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002566 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002567 int status = 0;
2568
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002569 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2570
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002571 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002572
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002573 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002574 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002575
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002576 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2577 str_supported(tp_features.wan),
2578 status);
2579
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002580 if (tp_features.wan) {
2581 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2582 /* no wan hardware present in system */
2583 tp_features.wan = 0;
2584 dbg_printk(TPACPI_DBG_INIT,
2585 "wan hardware not installed\n");
2586 } else {
2587 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2588 &wan_attr_group);
2589 if (res)
2590 return res;
2591 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002592 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002593
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002594 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002595}
2596
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002597static void wan_exit(void)
2598{
2599 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2600 &wan_attr_group);
2601}
2602
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002603static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002604{
2605 int status;
2606
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002607 if (!tp_features.wan)
2608 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002609
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002610 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2611 return -EIO;
2612
2613 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2614}
2615
2616static int wan_set_radiosw(int radio_on)
2617{
2618 int status;
2619
2620 if (!tp_features.wan)
2621 return -ENODEV;
2622
2623 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2624 return -EIO;
2625 if (radio_on)
2626 status |= TP_ACPI_WANCARD_RADIOSSW;
2627 else
2628 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2629 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2630 return -EIO;
2631
2632 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002633}
2634
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002635/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002636static int wan_read(char *p)
2637{
2638 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002639 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002640
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002641 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002642 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002643 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002644 len += sprintf(p + len, "status:\t\t%s\n",
2645 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002646 len += sprintf(p + len, "commands:\tenable, disable\n");
2647 }
2648
2649 return len;
2650}
2651
2652static int wan_write(char *buf)
2653{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002654 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002655
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002656 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002657 return -ENODEV;
2658
2659 while ((cmd = next_cmd(&buf))) {
2660 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002661 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002662 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002663 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002664 } else
2665 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002666 }
2667
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002668 return 0;
2669}
2670
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002671static struct ibm_struct wan_driver_data = {
2672 .name = "wan",
2673 .read = wan_read,
2674 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002675 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002676 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002677};
2678
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002679/*************************************************************************
2680 * Video subdriver
2681 */
2682
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002683enum video_access_mode {
2684 TPACPI_VIDEO_NONE = 0,
2685 TPACPI_VIDEO_570, /* 570 */
2686 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2687 TPACPI_VIDEO_NEW, /* all others */
2688};
2689
2690enum { /* video status flags, based on VIDEO_570 */
2691 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2692 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2693 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2694};
2695
2696enum { /* TPACPI_VIDEO_570 constants */
2697 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2698 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2699 * video_status_flags */
2700 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2701 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2702};
2703
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002704static enum video_access_mode video_supported;
2705static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002706
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002707static int video_autosw_get(void);
2708static int video_autosw_set(int enable);
2709
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002710TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002711 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2712 "\\_SB.PCI0.VID0", /* 770e */
2713 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2714 "\\_SB.PCI0.AGP.VID", /* all others */
2715 ); /* R30, R31 */
2716
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002717TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002718
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002719static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002721 int ivga;
2722
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002723 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2724
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002725 TPACPI_ACPIHANDLE_INIT(vid);
2726 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002727
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002728 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2729 /* G41, assume IVGA doesn't change */
2730 vid_handle = vid2_handle;
2731
2732 if (!vid_handle)
2733 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002734 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002735 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2736 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002737 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002738 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2739 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002740 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002741 else
2742 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002743 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002745 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2746 str_supported(video_supported != TPACPI_VIDEO_NONE),
2747 video_supported);
2748
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002749 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002752static void video_exit(void)
2753{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002754 dbg_printk(TPACPI_DBG_EXIT,
2755 "restoring original video autoswitch mode\n");
2756 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002757 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002758 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002759}
2760
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002761static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
2763 int status = 0;
2764 int i;
2765
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002766 switch (video_supported) {
2767 case TPACPI_VIDEO_570:
2768 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2769 TP_ACPI_VIDEO_570_PHSCMD))
2770 return -EIO;
2771 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2772 break;
2773 case TPACPI_VIDEO_770:
2774 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2775 return -EIO;
2776 if (i)
2777 status |= TP_ACPI_VIDEO_S_LCD;
2778 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2779 return -EIO;
2780 if (i)
2781 status |= TP_ACPI_VIDEO_S_CRT;
2782 break;
2783 case TPACPI_VIDEO_NEW:
2784 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2785 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2786 return -EIO;
2787 if (i)
2788 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002790 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2791 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2792 return -EIO;
2793 if (i)
2794 status |= TP_ACPI_VIDEO_S_LCD;
2795 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2796 return -EIO;
2797 if (i)
2798 status |= TP_ACPI_VIDEO_S_DVI;
2799 break;
2800 default:
2801 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 return status;
2805}
2806
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002807static int video_outputsw_set(int status)
2808{
2809 int autosw;
2810 int res = 0;
2811
2812 switch (video_supported) {
2813 case TPACPI_VIDEO_570:
2814 res = acpi_evalf(NULL, NULL,
2815 "\\_SB.PHS2", "vdd",
2816 TP_ACPI_VIDEO_570_PHS2CMD,
2817 status | TP_ACPI_VIDEO_570_PHS2SET);
2818 break;
2819 case TPACPI_VIDEO_770:
2820 autosw = video_autosw_get();
2821 if (autosw < 0)
2822 return autosw;
2823
2824 res = video_autosw_set(1);
2825 if (res)
2826 return res;
2827 res = acpi_evalf(vid_handle, NULL,
2828 "ASWT", "vdd", status * 0x100, 0);
2829 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002830 printk(TPACPI_ERR
2831 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002832 return -EIO;
2833 }
2834 break;
2835 case TPACPI_VIDEO_NEW:
2836 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002837 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002838 break;
2839 default:
2840 return -ENOSYS;
2841 }
2842
2843 return (res)? 0 : -EIO;
2844}
2845
2846static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002848 int autosw = 0;
2849
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002850 switch (video_supported) {
2851 case TPACPI_VIDEO_570:
2852 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2853 return -EIO;
2854 break;
2855 case TPACPI_VIDEO_770:
2856 case TPACPI_VIDEO_NEW:
2857 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2858 return -EIO;
2859 break;
2860 default:
2861 return -ENOSYS;
2862 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002863
2864 return autosw & 1;
2865}
2866
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002867static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002868{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002869 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002870 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002871 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002872}
2873
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002874static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002875{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002876 int autosw = video_autosw_get();
2877 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002878
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002879 if (autosw < 0)
2880 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002881
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002882 switch (video_supported) {
2883 case TPACPI_VIDEO_570:
2884 res = video_autosw_set(1);
2885 if (res)
2886 return res;
2887 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2888 break;
2889 case TPACPI_VIDEO_770:
2890 case TPACPI_VIDEO_NEW:
2891 res = video_autosw_set(1);
2892 if (res)
2893 return res;
2894 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2895 break;
2896 default:
2897 return -ENOSYS;
2898 }
2899 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002900 printk(TPACPI_ERR
2901 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002902 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002903 }
2904
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002905 return (res)? 0 : -EIO;
2906}
2907
2908static int video_expand_toggle(void)
2909{
2910 switch (video_supported) {
2911 case TPACPI_VIDEO_570:
2912 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2913 0 : -EIO;
2914 case TPACPI_VIDEO_770:
2915 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2916 0 : -EIO;
2917 case TPACPI_VIDEO_NEW:
2918 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2919 0 : -EIO;
2920 default:
2921 return -ENOSYS;
2922 }
2923 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002924}
2925
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002926static int video_read(char *p)
2927{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002928 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002929 int len = 0;
2930
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002931 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002932 len += sprintf(p + len, "status:\t\tnot supported\n");
2933 return len;
2934 }
2935
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002936 status = video_outputsw_get();
2937 if (status < 0)
2938 return status;
2939
2940 autosw = video_autosw_get();
2941 if (autosw < 0)
2942 return autosw;
2943
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002944 len += sprintf(p + len, "status:\t\tsupported\n");
2945 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2946 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002947 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002948 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2949 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2950 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2951 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002952 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002953 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2954 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2955 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2956
2957 return len;
2958}
2959
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002960static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 char *cmd;
2963 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002964 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002966 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002967 return -ENODEV;
2968
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002969 enable = 0;
2970 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 while ((cmd = next_cmd(&buf))) {
2973 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002974 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002976 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002978 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002980 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002981 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002982 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002983 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002984 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002985 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002986 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002988 res = video_autosw_set(1);
2989 if (res)
2990 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002992 res = video_autosw_set(0);
2993 if (res)
2994 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002996 res = video_outputsw_cycle();
2997 if (res)
2998 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003000 res = video_expand_toggle();
3001 if (res)
3002 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 } else
3004 return -EINVAL;
3005 }
3006
3007 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003008 status = video_outputsw_get();
3009 if (status < 0)
3010 return status;
3011 res = video_outputsw_set((status & ~disable) | enable);
3012 if (res)
3013 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
3015
3016 return 0;
3017}
3018
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003019static struct ibm_struct video_driver_data = {
3020 .name = "video",
3021 .read = video_read,
3022 .write = video_write,
3023 .exit = video_exit,
3024};
3025
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003026/*************************************************************************
3027 * Light (thinklight) subdriver
3028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003030TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3031TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003032
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003033static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003035 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3036
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003037 TPACPI_ACPIHANDLE_INIT(ledb);
3038 TPACPI_ACPIHANDLE_INIT(lght);
3039 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003040
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003041 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003042 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003043
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003044 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003045 /* light status not supported on
3046 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003047 tp_features.light_status =
3048 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003050 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003051 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003052
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003053 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003056static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 int len = 0;
3059 int status = 0;
3060
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003061 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003062 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003063 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003064 len += sprintf(p + len, "status:\t\tunknown\n");
3065 len += sprintf(p + len, "commands:\ton, off\n");
3066 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3068 return -EIO;
3069 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003070 len += sprintf(p + len, "commands:\ton, off\n");
3071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 return len;
3074}
3075
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003076static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
3078 int cmos_cmd, lght_cmd;
3079 char *cmd;
3080 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003081
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003082 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003083 return -ENODEV;
3084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 while ((cmd = next_cmd(&buf))) {
3086 if (strlencmp(cmd, "on") == 0) {
3087 cmos_cmd = 0x0c;
3088 lght_cmd = 1;
3089 } else if (strlencmp(cmd, "off") == 0) {
3090 cmos_cmd = 0x0d;
3091 lght_cmd = 0;
3092 } else
3093 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003096 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3097 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 if (!success)
3099 return -EIO;
3100 }
3101
3102 return 0;
3103}
3104
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003105static struct ibm_struct light_driver_data = {
3106 .name = "light",
3107 .read = light_read,
3108 .write = light_write,
3109};
3110
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003111/*************************************************************************
3112 * Dock subdriver
3113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003115#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003116
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003117static void dock_notify(struct ibm_struct *ibm, u32 event);
3118static int dock_read(char *p);
3119static int dock_write(char *buf);
3120
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003121TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003122 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3123 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3124 "\\_SB.PCI.ISA.SLCE", /* 570 */
3125 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3126
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003127/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003128TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003129
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003130static const struct acpi_device_id ibm_pci_device_ids[] = {
3131 {PCI_ROOT_HID_STRING, 0},
3132 {"", 0},
3133};
3134
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003135static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3136 {
3137 .notify = dock_notify,
3138 .handle = &dock_handle,
3139 .type = ACPI_SYSTEM_NOTIFY,
3140 },
3141 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003142 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3143 * We just use it to get notifications of dock hotplug
3144 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003145 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003146 .notify = dock_notify,
3147 .handle = &pci_handle,
3148 .type = ACPI_SYSTEM_NOTIFY,
3149 },
3150};
3151
3152static struct ibm_struct dock_driver_data[2] = {
3153 {
3154 .name = "dock",
3155 .read = dock_read,
3156 .write = dock_write,
3157 .acpi = &ibm_dock_acpidriver[0],
3158 },
3159 {
3160 .name = "dock",
3161 .acpi = &ibm_dock_acpidriver[1],
3162 },
3163};
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165#define dock_docked() (_sta(dock_handle) & 1)
3166
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003167static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003168{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003169 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3170
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003171 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003172
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003173 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3174 str_supported(dock_handle != NULL));
3175
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003176 return (dock_handle)? 0 : 1;
3177}
3178
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003179static int __init dock_init2(struct ibm_init_struct *iibm)
3180{
3181 int dock2_needed;
3182
3183 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3184
3185 if (dock_driver_data[0].flags.acpi_driver_registered &&
3186 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003187 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003188 dock2_needed = (pci_handle != NULL);
3189 vdbg_printk(TPACPI_DBG_INIT,
3190 "dock PCI handler for the TP 570 is %s\n",
3191 str_supported(dock2_needed));
3192 } else {
3193 vdbg_printk(TPACPI_DBG_INIT,
3194 "dock subdriver part 2 not required\n");
3195 dock2_needed = 0;
3196 }
3197
3198 return (dock2_needed)? 0 : 1;
3199}
3200
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003201static void dock_notify(struct ibm_struct *ibm, u32 event)
3202{
3203 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003204 int pci = ibm->acpi->hid && ibm->acpi->device &&
3205 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003206 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003207
3208 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003209 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003210 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003211 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003212 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003213 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003214 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003215 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003216 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003217 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003218 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003219 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003220 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003221 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003222 }
Len Brown14e04fb32007-08-23 15:20:26 -04003223 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003224 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3225 ibm->acpi->device->dev.bus_id,
3226 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003227}
3228
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003229static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231 int len = 0;
3232 int docked = dock_docked();
3233
3234 if (!dock_handle)
3235 len += sprintf(p + len, "status:\t\tnot supported\n");
3236 else if (!docked)
3237 len += sprintf(p + len, "status:\t\tundocked\n");
3238 else {
3239 len += sprintf(p + len, "status:\t\tdocked\n");
3240 len += sprintf(p + len, "commands:\tdock, undock\n");
3241 }
3242
3243 return len;
3244}
3245
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003246static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
3248 char *cmd;
3249
3250 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003251 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 while ((cmd = next_cmd(&buf))) {
3254 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003255 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3256 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return -EIO;
3258 } else if (strlencmp(cmd, "dock") == 0) {
3259 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3260 return -EIO;
3261 } else
3262 return -EINVAL;
3263 }
3264
3265 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003266}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003268#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003270/*************************************************************************
3271 * Bay subdriver
3272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003274#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003275
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003276TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003277 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3278 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3279 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3280 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003281TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003282 "_EJ0", /* all others */
3283 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003284TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003285 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3286 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003287TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003288 "_EJ0", /* 770x */
3289 ); /* all others */
3290
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003291static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003293 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3294
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003295 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003296 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003297 TPACPI_ACPIHANDLE_INIT(bay_ej);
3298 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003299 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003300 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003301
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003302 tp_features.bay_status = bay_handle &&
3303 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3304 tp_features.bay_status2 = bay2_handle &&
3305 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003306
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003307 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3308 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3309 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3310 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003312 vdbg_printk(TPACPI_DBG_INIT,
3313 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003314 str_supported(tp_features.bay_status),
3315 str_supported(tp_features.bay_eject),
3316 str_supported(tp_features.bay_status2),
3317 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003318
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003319 return (tp_features.bay_status || tp_features.bay_eject ||
3320 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003323static void bay_notify(struct ibm_struct *ibm, u32 event)
3324{
Len Brown14e04fb32007-08-23 15:20:26 -04003325 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003326 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3327 ibm->acpi->device->dev.bus_id,
3328 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003329}
3330
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003331#define bay_occupied(b) (_sta(b##_handle) & 1)
3332
3333static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
3335 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003336 int occupied = bay_occupied(bay);
3337 int occupied2 = bay_occupied(bay2);
3338 int eject, eject2;
3339
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003340 len += sprintf(p + len, "status:\t\t%s\n",
3341 tp_features.bay_status ?
3342 (occupied ? "occupied" : "unoccupied") :
3343 "not supported");
3344 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003345 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3346 "occupied" : "unoccupied");
3347
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003348 eject = tp_features.bay_eject && occupied;
3349 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003350
3351 if (eject && eject2)
3352 len += sprintf(p + len, "commands:\teject, eject2\n");
3353 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003355 else if (eject2)
3356 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358 return len;
3359}
3360
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003361static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
3363 char *cmd;
3364
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003365 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003366 return -ENODEV;
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003369 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003370 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3371 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003372 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003373 strlencmp(cmd, "eject2") == 0) {
3374 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return -EIO;
3376 } else
3377 return -EINVAL;
3378 }
3379
3380 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003381}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003382
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003383static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3384 .notify = bay_notify,
3385 .handle = &bay_handle,
3386 .type = ACPI_SYSTEM_NOTIFY,
3387};
3388
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003389static struct ibm_struct bay_driver_data = {
3390 .name = "bay",
3391 .read = bay_read,
3392 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003393 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003394};
3395
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003396#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003398/*************************************************************************
3399 * CMOS subdriver
3400 */
3401
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003402/* sysfs cmos_command -------------------------------------------------- */
3403static ssize_t cmos_command_store(struct device *dev,
3404 struct device_attribute *attr,
3405 const char *buf, size_t count)
3406{
3407 unsigned long cmos_cmd;
3408 int res;
3409
3410 if (parse_strtoul(buf, 21, &cmos_cmd))
3411 return -EINVAL;
3412
3413 res = issue_thinkpad_cmos_command(cmos_cmd);
3414 return (res)? res : count;
3415}
3416
3417static struct device_attribute dev_attr_cmos_command =
3418 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3419
3420/* --------------------------------------------------------------------- */
3421
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003422static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003423{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003424 int res;
3425
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003426 vdbg_printk(TPACPI_DBG_INIT,
3427 "initializing cmos commands subdriver\n");
3428
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003429 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003430
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003431 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3432 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003433
3434 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3435 if (res)
3436 return res;
3437
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003438 return (cmos_handle)? 0 : 1;
3439}
3440
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003441static void cmos_exit(void)
3442{
3443 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3444}
3445
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003446static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
3448 int len = 0;
3449
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003450 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3451 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 if (!cmos_handle)
3453 len += sprintf(p + len, "status:\t\tnot supported\n");
3454 else {
3455 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003456 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
3458
3459 return len;
3460}
3461
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003462static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
3464 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003465 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003468 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3469 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 /* cmos_cmd set */
3471 } else
3472 return -EINVAL;
3473
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003474 res = issue_thinkpad_cmos_command(cmos_cmd);
3475 if (res)
3476 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
3478
3479 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003480}
3481
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003482static struct ibm_struct cmos_driver_data = {
3483 .name = "cmos",
3484 .read = cmos_read,
3485 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003486 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003487};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003488
3489/*************************************************************************
3490 * LED subdriver
3491 */
3492
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003493enum led_access_mode {
3494 TPACPI_LED_NONE = 0,
3495 TPACPI_LED_570, /* 570 */
3496 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3497 TPACPI_LED_NEW, /* all others */
3498};
3499
3500enum { /* For TPACPI_LED_OLD */
3501 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3502 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3503 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3504};
3505
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003506static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003507
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003508TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003509 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3510 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003511 "LED", /* all others */
3512 ); /* R30, R31 */
3513
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003514static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003515{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003516 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3517
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003518 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003519
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003520 if (!led_handle)
3521 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003522 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003523 else if (strlencmp(led_path, "SLED") == 0)
3524 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003525 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003526 else if (strlencmp(led_path, "SYSL") == 0)
3527 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003528 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003529 else
3530 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003531 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003532
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003533 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3534 str_supported(led_supported), led_supported);
3535
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003536 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003537}
3538
3539#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3540
3541static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 int len = 0;
3544
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003545 if (!led_supported) {
3546 len += sprintf(p + len, "status:\t\tnot supported\n");
3547 return len;
3548 }
3549 len += sprintf(p + len, "status:\t\tsupported\n");
3550
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003551 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003552 /* 570 */
3553 int i, status;
3554 for (i = 0; i < 8; i++) {
3555 if (!acpi_evalf(ec_handle,
3556 &status, "GLED", "dd", 1 << i))
3557 return -EIO;
3558 len += sprintf(p + len, "%d:\t\t%s\n",
3559 i, led_status(status));
3560 }
3561 }
3562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003564 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566 return len;
3567}
3568
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003569/* off, on, blink */
3570static const int led_sled_arg1[] = { 0, 1, 3 };
3571static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3572static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3573static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3574
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003575static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576{
3577 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003578 int led, ind, ret;
3579
3580 if (!led_supported)
3581 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003584 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return -EINVAL;
3586
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003587 if (strstr(cmd, "off")) {
3588 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003590 ind = 1;
3591 } else if (strstr(cmd, "blink")) {
3592 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 } else
3594 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003595
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003596 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003597 /* 570 */
3598 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003600 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003602 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003603 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3604 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003605 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003606 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003607 ret = ec_write(TPACPI_LED_EC_HLBL,
3608 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003609 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003610 ret = ec_write(TPACPI_LED_EC_HLCL,
3611 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003612 if (ret < 0)
3613 return ret;
3614 } else {
3615 /* all others */
3616 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3617 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 }
3621
3622 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003623}
3624
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003625static struct ibm_struct led_driver_data = {
3626 .name = "led",
3627 .read = led_read,
3628 .write = led_write,
3629};
3630
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003631/*************************************************************************
3632 * Beep subdriver
3633 */
3634
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003635TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003636
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003637static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003638{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003639 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3640
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003641 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003642
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003643 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3644 str_supported(beep_handle != NULL));
3645
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003646 return (beep_handle)? 0 : 1;
3647}
3648
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003649static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
3651 int len = 0;
3652
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003653 if (!beep_handle)
3654 len += sprintf(p + len, "status:\t\tnot supported\n");
3655 else {
3656 len += sprintf(p + len, "status:\t\tsupported\n");
3657 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
3660 return len;
3661}
3662
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003663static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
3665 char *cmd;
3666 int beep_cmd;
3667
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003668 if (!beep_handle)
3669 return -ENODEV;
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003672 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3673 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 /* beep_cmd set */
3675 } else
3676 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003677 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return -EIO;
3679 }
3680
3681 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003682}
3683
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003684static struct ibm_struct beep_driver_data = {
3685 .name = "beep",
3686 .read = beep_read,
3687 .write = beep_write,
3688};
3689
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003690/*************************************************************************
3691 * Thermal subdriver
3692 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003693
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003694enum thermal_access_mode {
3695 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3696 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3697 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3698 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3699 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3700};
3701
3702enum { /* TPACPI_THERMAL_TPEC_* */
3703 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3704 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3705 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3706};
3707
3708#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3709struct ibm_thermal_sensors_struct {
3710 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3711};
3712
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003713static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003714
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003715/* idx is zero-based */
3716static int thermal_get_sensor(int idx, s32 *value)
3717{
3718 int t;
3719 s8 tmp;
3720 char tmpi[5];
3721
3722 t = TP_EC_THERMAL_TMP0;
3723
3724 switch (thermal_read_mode) {
3725#if TPACPI_MAX_THERMAL_SENSORS >= 16
3726 case TPACPI_THERMAL_TPEC_16:
3727 if (idx >= 8 && idx <= 15) {
3728 t = TP_EC_THERMAL_TMP8;
3729 idx -= 8;
3730 }
3731 /* fallthrough */
3732#endif
3733 case TPACPI_THERMAL_TPEC_8:
3734 if (idx <= 7) {
3735 if (!acpi_ec_read(t + idx, &tmp))
3736 return -EIO;
3737 *value = tmp * 1000;
3738 return 0;
3739 }
3740 break;
3741
3742 case TPACPI_THERMAL_ACPI_UPDT:
3743 if (idx <= 7) {
3744 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3745 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3746 return -EIO;
3747 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3748 return -EIO;
3749 *value = (t - 2732) * 100;
3750 return 0;
3751 }
3752 break;
3753
3754 case TPACPI_THERMAL_ACPI_TMP07:
3755 if (idx <= 7) {
3756 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3757 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3758 return -EIO;
3759 if (t > 127 || t < -127)
3760 t = TP_EC_THERMAL_TMP_NA;
3761 *value = t * 1000;
3762 return 0;
3763 }
3764 break;
3765
3766 case TPACPI_THERMAL_NONE:
3767 default:
3768 return -ENOSYS;
3769 }
3770
3771 return -EINVAL;
3772}
3773
3774static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3775{
3776 int res, i;
3777 int n;
3778
3779 n = 8;
3780 i = 0;
3781
3782 if (!s)
3783 return -EINVAL;
3784
3785 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3786 n = 16;
3787
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003788 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003789 res = thermal_get_sensor(i, &s->temp[i]);
3790 if (res)
3791 return res;
3792 }
3793
3794 return n;
3795}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003796
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003797/* sysfs temp##_input -------------------------------------------------- */
3798
3799static ssize_t thermal_temp_input_show(struct device *dev,
3800 struct device_attribute *attr,
3801 char *buf)
3802{
3803 struct sensor_device_attribute *sensor_attr =
3804 to_sensor_dev_attr(attr);
3805 int idx = sensor_attr->index;
3806 s32 value;
3807 int res;
3808
3809 res = thermal_get_sensor(idx, &value);
3810 if (res)
3811 return res;
3812 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3813 return -ENXIO;
3814
3815 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3816}
3817
3818#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003819 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3820 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003821
3822static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3823 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3824 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3825 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3826 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3827 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3828 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3829 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3830 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3831 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3832 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3833 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3834 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3835 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3836 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3837 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3838 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3839};
3840
3841#define THERMAL_ATTRS(X) \
3842 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3843
3844static struct attribute *thermal_temp_input_attr[] = {
3845 THERMAL_ATTRS(8),
3846 THERMAL_ATTRS(9),
3847 THERMAL_ATTRS(10),
3848 THERMAL_ATTRS(11),
3849 THERMAL_ATTRS(12),
3850 THERMAL_ATTRS(13),
3851 THERMAL_ATTRS(14),
3852 THERMAL_ATTRS(15),
3853 THERMAL_ATTRS(0),
3854 THERMAL_ATTRS(1),
3855 THERMAL_ATTRS(2),
3856 THERMAL_ATTRS(3),
3857 THERMAL_ATTRS(4),
3858 THERMAL_ATTRS(5),
3859 THERMAL_ATTRS(6),
3860 THERMAL_ATTRS(7),
3861 NULL
3862};
3863
3864static const struct attribute_group thermal_temp_input16_group = {
3865 .attrs = thermal_temp_input_attr
3866};
3867
3868static const struct attribute_group thermal_temp_input8_group = {
3869 .attrs = &thermal_temp_input_attr[8]
3870};
3871
3872#undef THERMAL_SENSOR_ATTR_TEMP
3873#undef THERMAL_ATTRS
3874
3875/* --------------------------------------------------------------------- */
3876
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003877static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003878{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003879 u8 t, ta1, ta2;
3880 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003881 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003882 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003883
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003884 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3885
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003886 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003887
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003888 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003889 /*
3890 * Direct EC access mode: sensors at registers
3891 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3892 * non-implemented, thermal sensors return 0x80 when
3893 * not available
3894 */
3895
3896 ta1 = ta2 = 0;
3897 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003898 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003899 ta1 |= t;
3900 } else {
3901 ta1 = 0;
3902 break;
3903 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003904 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003905 ta2 |= t;
3906 } else {
3907 ta1 = 0;
3908 break;
3909 }
3910 }
3911 if (ta1 == 0) {
3912 /* This is sheer paranoia, but we handle it anyway */
3913 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003914 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003915 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003916 "falling back to ACPI TMPx access "
3917 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003918 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003919 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003920 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003921 "ThinkPad ACPI EC access misbehaving, "
3922 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003923 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003924 }
3925 } else {
3926 thermal_read_mode =
3927 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003928 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003929 }
3930 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003931 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3932 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003933 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003934 } else {
3935 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003936 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003937 }
3938 } else {
3939 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003940 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003941 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003942
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003943 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3944 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3945 thermal_read_mode);
3946
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003947 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003948 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003949 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003950 &thermal_temp_input16_group);
3951 if (res)
3952 return res;
3953 break;
3954 case TPACPI_THERMAL_TPEC_8:
3955 case TPACPI_THERMAL_ACPI_TMP07:
3956 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003957 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003958 &thermal_temp_input8_group);
3959 if (res)
3960 return res;
3961 break;
3962 case TPACPI_THERMAL_NONE:
3963 default:
3964 return 1;
3965 }
3966
3967 return 0;
3968}
3969
3970static void thermal_exit(void)
3971{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003972 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003973 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003974 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003975 &thermal_temp_input16_group);
3976 break;
3977 case TPACPI_THERMAL_TPEC_8:
3978 case TPACPI_THERMAL_ACPI_TMP07:
3979 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003980 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003981 &thermal_temp_input16_group);
3982 break;
3983 case TPACPI_THERMAL_NONE:
3984 default:
3985 break;
3986 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003987}
3988
3989static int thermal_read(char *p)
3990{
3991 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003992 int n, i;
3993 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003994
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003995 n = thermal_get_sensors(&t);
3996 if (unlikely(n < 0))
3997 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003998
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003999 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004000
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004001 if (n > 0) {
4002 for (i = 0; i < (n - 1); i++)
4003 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4004 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4005 } else
4006 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004007
4008 return len;
4009}
4010
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004011static struct ibm_struct thermal_driver_data = {
4012 .name = "thermal",
4013 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004014 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004015};
4016
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004017/*************************************************************************
4018 * EC Dump subdriver
4019 */
4020
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004021static u8 ecdump_regs[256];
4022
4023static int ecdump_read(char *p)
4024{
4025 int len = 0;
4026 int i, j;
4027 u8 v;
4028
4029 len += sprintf(p + len, "EC "
4030 " +00 +01 +02 +03 +04 +05 +06 +07"
4031 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4032 for (i = 0; i < 256; i += 16) {
4033 len += sprintf(p + len, "EC 0x%02x:", i);
4034 for (j = 0; j < 16; j++) {
4035 if (!acpi_ec_read(i + j, &v))
4036 break;
4037 if (v != ecdump_regs[i + j])
4038 len += sprintf(p + len, " *%02x", v);
4039 else
4040 len += sprintf(p + len, " %02x", v);
4041 ecdump_regs[i + j] = v;
4042 }
4043 len += sprintf(p + len, "\n");
4044 if (j != 16)
4045 break;
4046 }
4047
4048 /* These are way too dangerous to advertise openly... */
4049#if 0
4050 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4051 " (<offset> is 00-ff, <value> is 00-ff)\n");
4052 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4053 " (<offset> is 00-ff, <value> is 0-255)\n");
4054#endif
4055 return len;
4056}
4057
4058static int ecdump_write(char *buf)
4059{
4060 char *cmd;
4061 int i, v;
4062
4063 while ((cmd = next_cmd(&buf))) {
4064 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4065 /* i and v set */
4066 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4067 /* i and v set */
4068 } else
4069 return -EINVAL;
4070 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4071 if (!acpi_ec_write(i, v))
4072 return -EIO;
4073 } else
4074 return -EINVAL;
4075 }
4076
4077 return 0;
4078}
4079
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004080static struct ibm_struct ecdump_driver_data = {
4081 .name = "ecdump",
4082 .read = ecdump_read,
4083 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004084 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004085};
4086
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004087/*************************************************************************
4088 * Backlight/brightness subdriver
4089 */
Holger Macht8acb0252006-10-20 14:30:28 -07004090
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004091#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4092
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004093static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004094static int brightness_offset = 0x31;
4095static int brightness_mode;
4096static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4097
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004098static struct mutex brightness_mutex;
4099
4100/*
4101 * ThinkPads can read brightness from two places: EC 0x31, or
4102 * CMOS NVRAM byte 0x5E, bits 0-3.
4103 */
4104static int brightness_get(struct backlight_device *bd)
4105{
4106 u8 lec = 0, lcmos = 0, level = 0;
4107
4108 if (brightness_mode & 1) {
4109 if (!acpi_ec_read(brightness_offset, &lec))
4110 return -EIO;
4111 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4112 level = lec;
4113 };
4114 if (brightness_mode & 2) {
4115 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4116 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4117 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4118 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4119 level = lcmos;
4120 }
4121
4122 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004123 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004124 "CMOS NVRAM (%u) and EC (%u) do not agree "
4125 "on display brightness level\n",
4126 (unsigned int) lcmos,
4127 (unsigned int) lec);
4128 return -EIO;
4129 }
4130
4131 return level;
4132}
4133
4134/* May return EINTR which can always be mapped to ERESTARTSYS */
4135static int brightness_set(int value)
4136{
4137 int cmos_cmd, inc, i, res;
4138 int current_value;
4139
4140 if (value > ((tp_features.bright_16levels)? 15 : 7))
4141 return -EINVAL;
4142
4143 res = mutex_lock_interruptible(&brightness_mutex);
4144 if (res < 0)
4145 return res;
4146
4147 current_value = brightness_get(NULL);
4148 if (current_value < 0) {
4149 res = current_value;
4150 goto errout;
4151 }
4152
4153 cmos_cmd = value > current_value ?
4154 TP_CMOS_BRIGHTNESS_UP :
4155 TP_CMOS_BRIGHTNESS_DOWN;
4156 inc = (value > current_value)? 1 : -1;
4157
4158 res = 0;
4159 for (i = current_value; i != value; i += inc) {
4160 if ((brightness_mode & 2) &&
4161 issue_thinkpad_cmos_command(cmos_cmd)) {
4162 res = -EIO;
4163 goto errout;
4164 }
4165 if ((brightness_mode & 1) &&
4166 !acpi_ec_write(brightness_offset, i + inc)) {
4167 res = -EIO;
4168 goto errout;;
4169 }
4170 }
4171
4172errout:
4173 mutex_unlock(&brightness_mutex);
4174 return res;
4175}
4176
4177/* sysfs backlight class ----------------------------------------------- */
4178
4179static int brightness_update_status(struct backlight_device *bd)
4180{
4181 /* it is the backlight class's job (caller) to handle
4182 * EINTR and other errors properly */
4183 return brightness_set(
4184 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4185 bd->props.power == FB_BLANK_UNBLANK) ?
4186 bd->props.brightness : 0);
4187}
Holger Macht8acb0252006-10-20 14:30:28 -07004188
Richard Purdie599a52d2007-02-10 23:07:48 +00004189static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004190 .get_brightness = brightness_get,
4191 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004192};
4193
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004194/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004195
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004196static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4197{
4198 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4199 union acpi_object *obj;
4200 int rc;
4201
4202 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4203 obj = (union acpi_object *)buffer.pointer;
4204 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004205 printk(TPACPI_ERR "Unknown BCLL data, "
4206 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004207 rc = 0;
4208 } else {
4209 rc = obj->package.count;
4210 }
4211 } else {
4212 return 0;
4213 }
4214
4215 kfree(buffer.pointer);
4216 return rc;
4217}
4218
4219static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4220 void *context, void **rv)
4221{
4222 char name[ACPI_PATH_SEGMENT_LENGTH];
4223 struct acpi_buffer buffer = { sizeof(name), &name };
4224
4225 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4226 !strncmp("BCLL", name, sizeof(name) - 1)) {
4227 if (tpacpi_query_bcll_levels(handle) == 16) {
4228 *rv = handle;
4229 return AE_CTRL_TERMINATE;
4230 } else {
4231 return AE_OK;
4232 }
4233 } else {
4234 return AE_OK;
4235 }
4236}
4237
4238static int __init brightness_check_levels(void)
4239{
4240 int status;
4241 void *found_node = NULL;
4242
4243 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004244 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004245 }
4246 if (!vid_handle)
4247 return 0;
4248
4249 /* Search for a BCLL package with 16 levels */
4250 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004251 brightness_find_bcll, NULL,
4252 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004253
4254 return (ACPI_SUCCESS(status) && found_node != NULL);
4255}
4256
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004257static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4258 void *context, void **rv)
4259{
4260 char name[ACPI_PATH_SEGMENT_LENGTH];
4261 struct acpi_buffer buffer = { sizeof(name), &name };
4262
4263 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4264 !strncmp("_BCL", name, sizeof(name) - 1)) {
4265 *rv = handle;
4266 return AE_CTRL_TERMINATE;
4267 } else {
4268 return AE_OK;
4269 }
4270}
4271
4272static int __init brightness_check_std_acpi_support(void)
4273{
4274 int status;
4275 void *found_node = NULL;
4276
4277 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004278 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004279 }
4280 if (!vid_handle)
4281 return 0;
4282
4283 /* Search for a _BCL method, but don't execute it */
4284 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004285 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004286
4287 return (ACPI_SUCCESS(status) && found_node != NULL);
4288}
4289
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004290static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004291{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004292 int b;
4293
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004294 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4295
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004296 mutex_init(&brightness_mutex);
4297
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004298 if (!brightness_enable) {
4299 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004300 "brightness support disabled by "
4301 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004302 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004303 } else if (brightness_enable > 1) {
4304 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004305 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004306 "standard ACPI backlight interface "
4307 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004308 return 1;
4309 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004310 }
4311
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004312 if (!brightness_mode) {
4313 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4314 brightness_mode = 2;
4315 else
4316 brightness_mode = 3;
4317
4318 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4319 brightness_mode);
4320 }
4321
4322 if (brightness_mode > 3)
4323 return -EINVAL;
4324
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004325 tp_features.bright_16levels =
4326 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4327 brightness_check_levels();
4328
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004329 b = brightness_get(NULL);
4330 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004331 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004332
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004333 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004334 printk(TPACPI_INFO
4335 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004336
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004337 ibm_backlight_device = backlight_device_register(
4338 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4339 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004340 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004341 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004342 return PTR_ERR(ibm_backlight_device);
4343 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004344 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004345
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004346 ibm_backlight_device->props.max_brightness =
4347 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004348 ibm_backlight_device->props.brightness = b;
4349 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004350
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004351 return 0;
4352}
4353
4354static void brightness_exit(void)
4355{
4356 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004357 vdbg_printk(TPACPI_DBG_EXIT,
4358 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004359 backlight_device_unregister(ibm_backlight_device);
4360 ibm_backlight_device = NULL;
4361 }
4362}
4363
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004364static int brightness_read(char *p)
4365{
4366 int len = 0;
4367 int level;
4368
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004369 level = brightness_get(NULL);
4370 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004371 len += sprintf(p + len, "level:\t\tunreadable\n");
4372 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004373 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004374 len += sprintf(p + len, "commands:\tup, down\n");
4375 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004376 " (<level> is 0-%d)\n",
4377 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004378 }
4379
4380 return len;
4381}
4382
4383static int brightness_write(char *buf)
4384{
4385 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004386 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004387 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004388 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004389
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004390 level = brightness_get(NULL);
4391 if (level < 0)
4392 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004393
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004394 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004395 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004396 if (level < max_level)
4397 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004398 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004399 if (level > 0)
4400 level--;
4401 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4402 level >= 0 && level <= max_level) {
4403 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004404 } else
4405 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004406 }
4407
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004408 /*
4409 * Now we know what the final level should be, so we try to set it.
4410 * Doing it this way makes the syscall restartable in case of EINTR
4411 */
4412 rc = brightness_set(level);
4413 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004414}
4415
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004416static struct ibm_struct brightness_driver_data = {
4417 .name = "brightness",
4418 .read = brightness_read,
4419 .write = brightness_write,
4420 .exit = brightness_exit,
4421};
4422
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004423/*************************************************************************
4424 * Volume subdriver
4425 */
4426
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004427static int volume_offset = 0x30;
4428
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004429static int volume_read(char *p)
4430{
4431 int len = 0;
4432 u8 level;
4433
4434 if (!acpi_ec_read(volume_offset, &level)) {
4435 len += sprintf(p + len, "level:\t\tunreadable\n");
4436 } else {
4437 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4438 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4439 len += sprintf(p + len, "commands:\tup, down, mute\n");
4440 len += sprintf(p + len, "commands:\tlevel <level>"
4441 " (<level> is 0-15)\n");
4442 }
4443
4444 return len;
4445}
4446
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004447static int volume_write(char *buf)
4448{
4449 int cmos_cmd, inc, i;
4450 u8 level, mute;
4451 int new_level, new_mute;
4452 char *cmd;
4453
4454 while ((cmd = next_cmd(&buf))) {
4455 if (!acpi_ec_read(volume_offset, &level))
4456 return -EIO;
4457 new_mute = mute = level & 0x40;
4458 new_level = level = level & 0xf;
4459
4460 if (strlencmp(cmd, "up") == 0) {
4461 if (mute)
4462 new_mute = 0;
4463 else
4464 new_level = level == 15 ? 15 : level + 1;
4465 } else if (strlencmp(cmd, "down") == 0) {
4466 if (mute)
4467 new_mute = 0;
4468 else
4469 new_level = level == 0 ? 0 : level - 1;
4470 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4471 new_level >= 0 && new_level <= 15) {
4472 /* new_level set */
4473 } else if (strlencmp(cmd, "mute") == 0) {
4474 new_mute = 0x40;
4475 } else
4476 return -EINVAL;
4477
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004478 if (new_level != level) {
4479 /* mute doesn't change */
4480
4481 cmos_cmd = (new_level > level) ?
4482 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004483 inc = new_level > level ? 1 : -1;
4484
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004485 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004486 !acpi_ec_write(volume_offset, level)))
4487 return -EIO;
4488
4489 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004490 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004491 !acpi_ec_write(volume_offset, i + inc))
4492 return -EIO;
4493
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004494 if (mute &&
4495 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4496 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004497 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004498 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004499 }
4500
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004501 if (new_mute != mute) {
4502 /* level doesn't change */
4503
4504 cmos_cmd = (new_mute) ?
4505 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004506
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004507 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004508 !acpi_ec_write(volume_offset, level + new_mute))
4509 return -EIO;
4510 }
4511 }
4512
4513 return 0;
4514}
4515
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004516static struct ibm_struct volume_driver_data = {
4517 .name = "volume",
4518 .read = volume_read,
4519 .write = volume_write,
4520};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004521
4522/*************************************************************************
4523 * Fan subdriver
4524 */
4525
4526/*
4527 * FAN ACCESS MODES
4528 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004529 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004530 * ACPI GFAN method: returns fan level
4531 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004532 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004533 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004534 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004535 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004536 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4537 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004538 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4539 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004540 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004541 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004542 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4543 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004544 *
4545 * Fan speed changes of any sort (including those caused by the
4546 * disengaged mode) are usually done slowly by the firmware as the
4547 * maximum ammount of fan duty cycle change per second seems to be
4548 * limited.
4549 *
4550 * Reading is not available if GFAN exists.
4551 * Writing is not available if SFAN exists.
4552 *
4553 * Bits
4554 * 7 automatic mode engaged;
4555 * (default operation mode of the ThinkPad)
4556 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004557 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004558 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004559 * the tachometer while the fan controller ramps up
4560 * the speed (which can take up to a few *minutes*).
4561 * Speeds up fan to 100% duty-cycle, which is far above
4562 * the standard RPM levels. It is not impossible that
4563 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004564 * 5-3 unused in some models. Extra bits for fan level
4565 * in others, but still useless as all values above
4566 * 7 map to the same speed as level 7 in these models.
4567 * 2-0 fan level (0..7 usually)
4568 * 0x00 = stop
4569 * 0x07 = max (set when temperatures critical)
4570 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004571 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004572 *
4573 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4574 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4575 * does so, its initial value is meaningless (0x07).
4576 *
4577 * For firmware bugs, refer to:
4578 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4579 *
4580 * ----
4581 *
4582 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4583 * Main fan tachometer reading (in RPM)
4584 *
4585 * This register is present on all ThinkPads with a new-style EC, and
4586 * it is known not to be present on the A21m/e, and T22, as there is
4587 * something else in offset 0x84 according to the ACPI DSDT. Other
4588 * ThinkPads from this same time period (and earlier) probably lack the
4589 * tachometer as well.
4590 *
4591 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4592 * was never fixed by IBM to report the EC firmware version string
4593 * probably support the tachometer (like the early X models), so
4594 * detecting it is quite hard. We need more data to know for sure.
4595 *
4596 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4597 * might result.
4598 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004599 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4600 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004601 *
4602 * For firmware bugs, refer to:
4603 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4604 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004605 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004606 * ThinkPad X31, X40, X41. Not available in the X60.
4607 *
4608 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4609 * high speed. ACPI DSDT seems to map these three speeds to levels
4610 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4611 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4612 *
4613 * The speeds are stored on handles
4614 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4615 *
4616 * There are three default speed sets, acessible as handles:
4617 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4618 *
4619 * ACPI DSDT switches which set is in use depending on various
4620 * factors.
4621 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004622 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004623 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4624 * but the ACPI tables just mention level 7.
4625 */
4626
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004627enum { /* Fan control constants */
4628 fan_status_offset = 0x2f, /* EC register 0x2f */
4629 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4630 * 0x84 must be read before 0x85 */
4631
4632 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4633 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4634
4635 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4636};
4637
4638enum fan_status_access_mode {
4639 TPACPI_FAN_NONE = 0, /* No fan status or control */
4640 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4641 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4642};
4643
4644enum fan_control_access_mode {
4645 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4646 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4647 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4648 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4649};
4650
4651enum fan_control_commands {
4652 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4653 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4654 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4655 * and also watchdog cmd */
4656};
4657
4658static int fan_control_allowed;
4659
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004660static enum fan_status_access_mode fan_status_access_mode;
4661static enum fan_control_access_mode fan_control_access_mode;
4662static enum fan_control_commands fan_control_commands;
4663
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004664static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004665static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004666static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004667
4668static struct mutex fan_mutex;
4669
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004670static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004671static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004672
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004673TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4674TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004675 "\\FSPD", /* 600e/x, 770e, 770x */
4676 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004677TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004678 "JFNS", /* 770x-JL */
4679 ); /* all others */
4680
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004681/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004682 * Call with fan_mutex held
4683 */
4684static void fan_update_desired_level(u8 status)
4685{
4686 if ((status &
4687 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4688 if (status > 7)
4689 fan_control_desired_level = 7;
4690 else
4691 fan_control_desired_level = status;
4692 }
4693}
4694
4695static int fan_get_status(u8 *status)
4696{
4697 u8 s;
4698
4699 /* TODO:
4700 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4701
4702 switch (fan_status_access_mode) {
4703 case TPACPI_FAN_RD_ACPI_GFAN:
4704 /* 570, 600e/x, 770e, 770x */
4705
4706 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4707 return -EIO;
4708
4709 if (likely(status))
4710 *status = s & 0x07;
4711
4712 break;
4713
4714 case TPACPI_FAN_RD_TPEC:
4715 /* all except 570, 600e/x, 770e, 770x */
4716 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4717 return -EIO;
4718
4719 if (likely(status))
4720 *status = s;
4721
4722 break;
4723
4724 default:
4725 return -ENXIO;
4726 }
4727
4728 return 0;
4729}
4730
4731static int fan_get_status_safe(u8 *status)
4732{
4733 int rc;
4734 u8 s;
4735
4736 if (mutex_lock_interruptible(&fan_mutex))
4737 return -ERESTARTSYS;
4738 rc = fan_get_status(&s);
4739 if (!rc)
4740 fan_update_desired_level(s);
4741 mutex_unlock(&fan_mutex);
4742
4743 if (status)
4744 *status = s;
4745
4746 return rc;
4747}
4748
4749static int fan_get_speed(unsigned int *speed)
4750{
4751 u8 hi, lo;
4752
4753 switch (fan_status_access_mode) {
4754 case TPACPI_FAN_RD_TPEC:
4755 /* all except 570, 600e/x, 770e, 770x */
4756 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4757 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4758 return -EIO;
4759
4760 if (likely(speed))
4761 *speed = (hi << 8) | lo;
4762
4763 break;
4764
4765 default:
4766 return -ENXIO;
4767 }
4768
4769 return 0;
4770}
4771
4772static int fan_set_level(int level)
4773{
4774 if (!fan_control_allowed)
4775 return -EPERM;
4776
4777 switch (fan_control_access_mode) {
4778 case TPACPI_FAN_WR_ACPI_SFAN:
4779 if (level >= 0 && level <= 7) {
4780 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4781 return -EIO;
4782 } else
4783 return -EINVAL;
4784 break;
4785
4786 case TPACPI_FAN_WR_ACPI_FANS:
4787 case TPACPI_FAN_WR_TPEC:
4788 if ((level != TP_EC_FAN_AUTO) &&
4789 (level != TP_EC_FAN_FULLSPEED) &&
4790 ((level < 0) || (level > 7)))
4791 return -EINVAL;
4792
4793 /* safety net should the EC not support AUTO
4794 * or FULLSPEED mode bits and just ignore them */
4795 if (level & TP_EC_FAN_FULLSPEED)
4796 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01004797 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004798 level |= 4; /* safety min speed 4 */
4799
4800 if (!acpi_ec_write(fan_status_offset, level))
4801 return -EIO;
4802 else
4803 tp_features.fan_ctrl_status_undef = 0;
4804 break;
4805
4806 default:
4807 return -ENXIO;
4808 }
4809 return 0;
4810}
4811
4812static int fan_set_level_safe(int level)
4813{
4814 int rc;
4815
4816 if (!fan_control_allowed)
4817 return -EPERM;
4818
4819 if (mutex_lock_interruptible(&fan_mutex))
4820 return -ERESTARTSYS;
4821
4822 if (level == TPACPI_FAN_LAST_LEVEL)
4823 level = fan_control_desired_level;
4824
4825 rc = fan_set_level(level);
4826 if (!rc)
4827 fan_update_desired_level(level);
4828
4829 mutex_unlock(&fan_mutex);
4830 return rc;
4831}
4832
4833static int fan_set_enable(void)
4834{
4835 u8 s;
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 switch (fan_control_access_mode) {
4845 case TPACPI_FAN_WR_ACPI_FANS:
4846 case TPACPI_FAN_WR_TPEC:
4847 rc = fan_get_status(&s);
4848 if (rc < 0)
4849 break;
4850
4851 /* Don't go out of emergency fan mode */
4852 if (s != 7) {
4853 s &= 0x07;
4854 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4855 }
4856
4857 if (!acpi_ec_write(fan_status_offset, s))
4858 rc = -EIO;
4859 else {
4860 tp_features.fan_ctrl_status_undef = 0;
4861 rc = 0;
4862 }
4863 break;
4864
4865 case TPACPI_FAN_WR_ACPI_SFAN:
4866 rc = fan_get_status(&s);
4867 if (rc < 0)
4868 break;
4869
4870 s &= 0x07;
4871
4872 /* Set fan to at least level 4 */
4873 s |= 4;
4874
4875 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004876 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004877 else
4878 rc = 0;
4879 break;
4880
4881 default:
4882 rc = -ENXIO;
4883 }
4884
4885 mutex_unlock(&fan_mutex);
4886 return rc;
4887}
4888
4889static int fan_set_disable(void)
4890{
4891 int rc;
4892
4893 if (!fan_control_allowed)
4894 return -EPERM;
4895
4896 if (mutex_lock_interruptible(&fan_mutex))
4897 return -ERESTARTSYS;
4898
4899 rc = 0;
4900 switch (fan_control_access_mode) {
4901 case TPACPI_FAN_WR_ACPI_FANS:
4902 case TPACPI_FAN_WR_TPEC:
4903 if (!acpi_ec_write(fan_status_offset, 0x00))
4904 rc = -EIO;
4905 else {
4906 fan_control_desired_level = 0;
4907 tp_features.fan_ctrl_status_undef = 0;
4908 }
4909 break;
4910
4911 case TPACPI_FAN_WR_ACPI_SFAN:
4912 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4913 rc = -EIO;
4914 else
4915 fan_control_desired_level = 0;
4916 break;
4917
4918 default:
4919 rc = -ENXIO;
4920 }
4921
4922
4923 mutex_unlock(&fan_mutex);
4924 return rc;
4925}
4926
4927static int fan_set_speed(int speed)
4928{
4929 int rc;
4930
4931 if (!fan_control_allowed)
4932 return -EPERM;
4933
4934 if (mutex_lock_interruptible(&fan_mutex))
4935 return -ERESTARTSYS;
4936
4937 rc = 0;
4938 switch (fan_control_access_mode) {
4939 case TPACPI_FAN_WR_ACPI_FANS:
4940 if (speed >= 0 && speed <= 65535) {
4941 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4942 speed, speed, speed))
4943 rc = -EIO;
4944 } else
4945 rc = -EINVAL;
4946 break;
4947
4948 default:
4949 rc = -ENXIO;
4950 }
4951
4952 mutex_unlock(&fan_mutex);
4953 return rc;
4954}
4955
4956static void fan_watchdog_reset(void)
4957{
4958 static int fan_watchdog_active;
4959
4960 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4961 return;
4962
4963 if (fan_watchdog_active)
4964 cancel_delayed_work(&fan_watchdog_task);
4965
4966 if (fan_watchdog_maxinterval > 0 &&
4967 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4968 fan_watchdog_active = 1;
4969 if (!schedule_delayed_work(&fan_watchdog_task,
4970 msecs_to_jiffies(fan_watchdog_maxinterval
4971 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004972 printk(TPACPI_ERR
4973 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004974 "watchdog will not trigger\n");
4975 }
4976 } else
4977 fan_watchdog_active = 0;
4978}
4979
4980static void fan_watchdog_fire(struct work_struct *ignored)
4981{
4982 int rc;
4983
4984 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4985 return;
4986
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004987 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004988 rc = fan_set_enable();
4989 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004990 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004991 "will try again later...\n", -rc);
4992 /* reschedule for later */
4993 fan_watchdog_reset();
4994 }
4995}
4996
4997/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004998 * SYSFS fan layout: hwmon compatible (device)
4999 *
5000 * pwm*_enable:
5001 * 0: "disengaged" mode
5002 * 1: manual mode
5003 * 2: native EC "auto" mode (recommended, hardware default)
5004 *
5005 * pwm*: set speed in manual mode, ignored otherwise.
5006 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5007 * interpolation.
5008 *
5009 * fan*_input: tachometer reading, RPM
5010 *
5011 *
5012 * SYSFS fan layout: extensions
5013 *
5014 * fan_watchdog (driver):
5015 * fan watchdog interval in seconds, 0 disables (default), max 120
5016 */
5017
5018/* sysfs fan pwm1_enable ----------------------------------------------- */
5019static ssize_t fan_pwm1_enable_show(struct device *dev,
5020 struct device_attribute *attr,
5021 char *buf)
5022{
5023 int res, mode;
5024 u8 status;
5025
5026 res = fan_get_status_safe(&status);
5027 if (res)
5028 return res;
5029
5030 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5031 if (status != fan_control_initial_status) {
5032 tp_features.fan_ctrl_status_undef = 0;
5033 } else {
5034 /* Return most likely status. In fact, it
5035 * might be the only possible status */
5036 status = TP_EC_FAN_AUTO;
5037 }
5038 }
5039
5040 if (status & TP_EC_FAN_FULLSPEED) {
5041 mode = 0;
5042 } else if (status & TP_EC_FAN_AUTO) {
5043 mode = 2;
5044 } else
5045 mode = 1;
5046
5047 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5048}
5049
5050static ssize_t fan_pwm1_enable_store(struct device *dev,
5051 struct device_attribute *attr,
5052 const char *buf, size_t count)
5053{
5054 unsigned long t;
5055 int res, level;
5056
5057 if (parse_strtoul(buf, 2, &t))
5058 return -EINVAL;
5059
5060 switch (t) {
5061 case 0:
5062 level = TP_EC_FAN_FULLSPEED;
5063 break;
5064 case 1:
5065 level = TPACPI_FAN_LAST_LEVEL;
5066 break;
5067 case 2:
5068 level = TP_EC_FAN_AUTO;
5069 break;
5070 case 3:
5071 /* reserved for software-controlled auto mode */
5072 return -ENOSYS;
5073 default:
5074 return -EINVAL;
5075 }
5076
5077 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005078 if (res == -ENXIO)
5079 return -EINVAL;
5080 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005081 return res;
5082
5083 fan_watchdog_reset();
5084
5085 return count;
5086}
5087
5088static struct device_attribute dev_attr_fan_pwm1_enable =
5089 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5090 fan_pwm1_enable_show, fan_pwm1_enable_store);
5091
5092/* sysfs fan pwm1 ------------------------------------------------------ */
5093static ssize_t fan_pwm1_show(struct device *dev,
5094 struct device_attribute *attr,
5095 char *buf)
5096{
5097 int res;
5098 u8 status;
5099
5100 res = fan_get_status_safe(&status);
5101 if (res)
5102 return res;
5103
5104 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5105 if (status != fan_control_initial_status) {
5106 tp_features.fan_ctrl_status_undef = 0;
5107 } else {
5108 status = TP_EC_FAN_AUTO;
5109 }
5110 }
5111
5112 if ((status &
5113 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5114 status = fan_control_desired_level;
5115
5116 if (status > 7)
5117 status = 7;
5118
5119 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5120}
5121
5122static ssize_t fan_pwm1_store(struct device *dev,
5123 struct device_attribute *attr,
5124 const char *buf, size_t count)
5125{
5126 unsigned long s;
5127 int rc;
5128 u8 status, newlevel;
5129
5130 if (parse_strtoul(buf, 255, &s))
5131 return -EINVAL;
5132
5133 /* scale down from 0-255 to 0-7 */
5134 newlevel = (s >> 5) & 0x07;
5135
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005136 if (mutex_lock_interruptible(&fan_mutex))
5137 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005138
5139 rc = fan_get_status(&status);
5140 if (!rc && (status &
5141 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5142 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005143 if (rc == -ENXIO)
5144 rc = -EINVAL;
5145 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005146 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005147 fan_watchdog_reset();
5148 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005149 }
5150
5151 mutex_unlock(&fan_mutex);
5152 return (rc)? rc : count;
5153}
5154
5155static struct device_attribute dev_attr_fan_pwm1 =
5156 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5157 fan_pwm1_show, fan_pwm1_store);
5158
5159/* sysfs fan fan1_input ------------------------------------------------ */
5160static ssize_t fan_fan1_input_show(struct device *dev,
5161 struct device_attribute *attr,
5162 char *buf)
5163{
5164 int res;
5165 unsigned int speed;
5166
5167 res = fan_get_speed(&speed);
5168 if (res < 0)
5169 return res;
5170
5171 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5172}
5173
5174static struct device_attribute dev_attr_fan_fan1_input =
5175 __ATTR(fan1_input, S_IRUGO,
5176 fan_fan1_input_show, NULL);
5177
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005178/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005179static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5180 char *buf)
5181{
5182 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5183}
5184
5185static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5186 const char *buf, size_t count)
5187{
5188 unsigned long t;
5189
5190 if (parse_strtoul(buf, 120, &t))
5191 return -EINVAL;
5192
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005193 if (!fan_control_allowed)
5194 return -EPERM;
5195
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005196 fan_watchdog_maxinterval = t;
5197 fan_watchdog_reset();
5198
5199 return count;
5200}
5201
5202static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5203 fan_fan_watchdog_show, fan_fan_watchdog_store);
5204
5205/* --------------------------------------------------------------------- */
5206static struct attribute *fan_attributes[] = {
5207 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5208 &dev_attr_fan_fan1_input.attr,
5209 NULL
5210};
5211
5212static const struct attribute_group fan_attr_group = {
5213 .attrs = fan_attributes,
5214};
5215
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005216static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005217{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005218 int rc;
5219
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005220 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5221
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005222 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005223 fan_status_access_mode = TPACPI_FAN_NONE;
5224 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005225 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005226 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005227 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005228 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005229
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005230 TPACPI_ACPIHANDLE_INIT(fans);
5231 TPACPI_ACPIHANDLE_INIT(gfan);
5232 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005233
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005234 if (gfan_handle) {
5235 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005236 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005237 } else {
5238 /* all other ThinkPads: note that even old-style
5239 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005240 if (likely(acpi_ec_read(fan_status_offset,
5241 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005242 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005243
5244 /* In some ThinkPads, neither the EC nor the ACPI
5245 * DSDT initialize the fan status, and it ends up
5246 * being set to 0x07 when it *could* be either
5247 * 0x07 or 0x80.
5248 *
5249 * Enable for TP-1Y (T43), TP-78 (R51e),
5250 * TP-76 (R52), TP-70 (T43, R52), which are known
5251 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005252 if (fan_control_initial_status == 0x07) {
5253 switch (thinkpad_id.ec_model) {
5254 case 0x5931: /* TP-1Y */
5255 case 0x3837: /* TP-78 */
5256 case 0x3637: /* TP-76 */
5257 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005258 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005259 "fan_init: initial fan status "
5260 "is unknown, assuming it is "
5261 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005262 tp_features.fan_ctrl_status_undef = 1;
5263 ;;
5264 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005265 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005266 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005267 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005268 "ThinkPad ACPI EC access misbehaving, "
5269 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005270 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005271 }
5272 }
5273
5274 if (sfan_handle) {
5275 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005276 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005277 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005278 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005279 } else {
5280 if (!gfan_handle) {
5281 /* gfan without sfan means no fan control */
5282 /* all other models implement TP EC 0x2f control */
5283
5284 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005285 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005286 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005287 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005288 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005289 TPACPI_FAN_CMD_SPEED |
5290 TPACPI_FAN_CMD_LEVEL |
5291 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005292 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005293 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005294 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005295 TPACPI_FAN_CMD_LEVEL |
5296 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005297 }
5298 }
5299 }
5300
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005301 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5302 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5303 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5304 fan_status_access_mode, fan_control_access_mode);
5305
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005306 /* fan control master switch */
5307 if (!fan_control_allowed) {
5308 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5309 fan_control_commands = 0;
5310 dbg_printk(TPACPI_DBG_INIT,
5311 "fan control features disabled by parameter\n");
5312 }
5313
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005314 /* update fan_control_desired_level */
5315 if (fan_status_access_mode != TPACPI_FAN_NONE)
5316 fan_get_status_safe(NULL);
5317
5318 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5319 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005320 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005321 &fan_attr_group);
5322 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005323 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005324 &driver_attr_fan_watchdog);
5325 if (rc < 0)
5326 return rc;
5327 return 0;
5328 } else
5329 return 1;
5330}
5331
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005332static void fan_exit(void)
5333{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005334 vdbg_printk(TPACPI_DBG_EXIT,
5335 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005336
5337 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005338 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005339 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5340 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005341
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005342 cancel_delayed_work(&fan_watchdog_task);
5343 flush_scheduled_work();
5344}
5345
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005346static int fan_read(char *p)
5347{
5348 int len = 0;
5349 int rc;
5350 u8 status;
5351 unsigned int speed = 0;
5352
5353 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005354 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005355 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005356 rc = fan_get_status_safe(&status);
5357 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005358 return rc;
5359
5360 len += sprintf(p + len, "status:\t\t%s\n"
5361 "level:\t\t%d\n",
5362 (status != 0) ? "enabled" : "disabled", status);
5363 break;
5364
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005365 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005366 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005367 rc = fan_get_status_safe(&status);
5368 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005369 return rc;
5370
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005371 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005372 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005373 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005374 else
5375 /* Return most likely status. In fact, it
5376 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005377 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005378 }
5379
5380 len += sprintf(p + len, "status:\t\t%s\n",
5381 (status != 0) ? "enabled" : "disabled");
5382
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005383 rc = fan_get_speed(&speed);
5384 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005385 return rc;
5386
5387 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5388
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005389 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005390 /* Disengaged mode takes precedence */
5391 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005392 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005393 len += sprintf(p + len, "level:\t\tauto\n");
5394 else
5395 len += sprintf(p + len, "level:\t\t%d\n", status);
5396 break;
5397
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005398 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005399 default:
5400 len += sprintf(p + len, "status:\t\tnot supported\n");
5401 }
5402
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005403 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005404 len += sprintf(p + len, "commands:\tlevel <level>");
5405
5406 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005407 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005408 len += sprintf(p + len, " (<level> is 0-7)\n");
5409 break;
5410
5411 default:
5412 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005413 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005414 break;
5415 }
5416 }
5417
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005418 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005419 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005420 "commands:\twatchdog <timeout> (<timeout> "
5421 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005422
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005423 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005424 len += sprintf(p + len, "commands:\tspeed <speed>"
5425 " (<speed> is 0-65535)\n");
5426
5427 return len;
5428}
5429
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005430static int fan_write_cmd_level(const char *cmd, int *rc)
5431{
5432 int level;
5433
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005434 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005435 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005436 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005437 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005438 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005439 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005440 return 0;
5441
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005442 *rc = fan_set_level_safe(level);
5443 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005444 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005445 "access mode %d", fan_control_access_mode);
5446
5447 return 1;
5448}
5449
5450static int fan_write_cmd_enable(const char *cmd, int *rc)
5451{
5452 if (strlencmp(cmd, "enable") != 0)
5453 return 0;
5454
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005455 *rc = fan_set_enable();
5456 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005457 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005458 "access mode %d", fan_control_access_mode);
5459
5460 return 1;
5461}
5462
5463static int fan_write_cmd_disable(const char *cmd, int *rc)
5464{
5465 if (strlencmp(cmd, "disable") != 0)
5466 return 0;
5467
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005468 *rc = fan_set_disable();
5469 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005470 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005471 "access mode %d", fan_control_access_mode);
5472
5473 return 1;
5474}
5475
5476static int fan_write_cmd_speed(const char *cmd, int *rc)
5477{
5478 int speed;
5479
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005480 /* TODO:
5481 * Support speed <low> <medium> <high> ? */
5482
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005483 if (sscanf(cmd, "speed %d", &speed) != 1)
5484 return 0;
5485
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005486 *rc = fan_set_speed(speed);
5487 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005488 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005489 "access mode %d", fan_control_access_mode);
5490
5491 return 1;
5492}
5493
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005494static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5495{
5496 int interval;
5497
5498 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5499 return 0;
5500
5501 if (interval < 0 || interval > 120)
5502 *rc = -EINVAL;
5503 else
5504 fan_watchdog_maxinterval = interval;
5505
5506 return 1;
5507}
5508
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005509static int fan_write(char *buf)
5510{
5511 char *cmd;
5512 int rc = 0;
5513
5514 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005515 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005516 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005517 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005518 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005519 fan_write_cmd_disable(cmd, &rc) ||
5520 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005521 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005522 fan_write_cmd_speed(cmd, &rc))
5523 )
5524 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005525 else if (!rc)
5526 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005527 }
5528
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005529 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005530}
5531
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005532static struct ibm_struct fan_driver_data = {
5533 .name = "fan",
5534 .read = fan_read,
5535 .write = fan_write,
5536 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005537};
5538
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005539/****************************************************************************
5540 ****************************************************************************
5541 *
5542 * Infrastructure
5543 *
5544 ****************************************************************************
5545 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005547/* sysfs name ---------------------------------------------------------- */
5548static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5549 struct device_attribute *attr,
5550 char *buf)
5551{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005552 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005553}
5554
5555static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5556 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5557
5558/* --------------------------------------------------------------------- */
5559
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005560/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005561static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005562
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005563/*
5564 * Module and infrastructure proble, init and exit handling
5565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005567static int force_load;
5568
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005569#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005570static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005571{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005572 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005573
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005574 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005575}
5576#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5577
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005578static void ibm_exit(struct ibm_struct *ibm)
5579{
5580 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5581
5582 list_del_init(&ibm->all_drivers);
5583
5584 if (ibm->flags.acpi_notify_installed) {
5585 dbg_printk(TPACPI_DBG_EXIT,
5586 "%s: acpi_remove_notify_handler\n", ibm->name);
5587 BUG_ON(!ibm->acpi);
5588 acpi_remove_notify_handler(*ibm->acpi->handle,
5589 ibm->acpi->type,
5590 dispatch_acpi_notify);
5591 ibm->flags.acpi_notify_installed = 0;
5592 ibm->flags.acpi_notify_installed = 0;
5593 }
5594
5595 if (ibm->flags.proc_created) {
5596 dbg_printk(TPACPI_DBG_EXIT,
5597 "%s: remove_proc_entry\n", ibm->name);
5598 remove_proc_entry(ibm->name, proc_dir);
5599 ibm->flags.proc_created = 0;
5600 }
5601
5602 if (ibm->flags.acpi_driver_registered) {
5603 dbg_printk(TPACPI_DBG_EXIT,
5604 "%s: acpi_bus_unregister_driver\n", ibm->name);
5605 BUG_ON(!ibm->acpi);
5606 acpi_bus_unregister_driver(ibm->acpi->driver);
5607 kfree(ibm->acpi->driver);
5608 ibm->acpi->driver = NULL;
5609 ibm->flags.acpi_driver_registered = 0;
5610 }
5611
5612 if (ibm->flags.init_called && ibm->exit) {
5613 ibm->exit();
5614 ibm->flags.init_called = 0;
5615 }
5616
5617 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5618}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005619
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005620static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621{
5622 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005623 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 struct proc_dir_entry *entry;
5625
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005626 BUG_ON(ibm == NULL);
5627
5628 INIT_LIST_HEAD(&ibm->all_drivers);
5629
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005630 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 return 0;
5632
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005633 dbg_printk(TPACPI_DBG_INIT,
5634 "probing for %s\n", ibm->name);
5635
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005636 if (iibm->init) {
5637 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005638 if (ret > 0)
5639 return 0; /* probe failed */
5640 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005642
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005643 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 }
5645
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005646 if (ibm->acpi) {
5647 if (ibm->acpi->hid) {
5648 ret = register_tpacpi_subdriver(ibm);
5649 if (ret)
5650 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005651 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005652
5653 if (ibm->acpi->notify) {
5654 ret = setup_acpi_notify(ibm);
5655 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005656 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005657 ibm->name);
5658 ret = 0;
5659 goto err_out;
5660 }
5661 if (ret < 0)
5662 goto err_out;
5663 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005664 }
5665
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005666 dbg_printk(TPACPI_DBG_INIT,
5667 "%s installed\n", ibm->name);
5668
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005669 if (ibm->read) {
5670 entry = create_proc_entry(ibm->name,
5671 S_IFREG | S_IRUGO | S_IWUSR,
5672 proc_dir);
5673 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005674 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005675 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005676 ret = -ENODEV;
5677 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005678 }
5679 entry->owner = THIS_MODULE;
5680 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005681 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005682 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005683 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005684 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005687 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5688
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005690
5691err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005692 dbg_printk(TPACPI_DBG_INIT,
5693 "%s: at error exit path with result %d\n",
5694 ibm->name, ret);
5695
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005696 ibm_exit(ibm);
5697 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698}
5699
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005700/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005702static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005703{
Jeff Garzik18552562007-10-03 15:15:40 -04005704 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005705 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005706
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005707 if (!tp)
5708 return;
5709
5710 memset(tp, 0, sizeof(*tp));
5711
5712 if (dmi_name_in_vendors("IBM"))
5713 tp->vendor = PCI_VENDOR_ID_IBM;
5714 else if (dmi_name_in_vendors("LENOVO"))
5715 tp->vendor = PCI_VENDOR_ID_LENOVO;
5716 else
5717 return;
5718
5719 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5720 GFP_KERNEL);
5721 if (!tp->bios_version_str)
5722 return;
5723 tp->bios_model = tp->bios_version_str[0]
5724 | (tp->bios_version_str[1] << 8);
5725
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005726 /*
5727 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5728 * X32 or newer, all Z series; Some models must have an
5729 * up-to-date BIOS or they will not be detected.
5730 *
5731 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5732 */
5733 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005734 if (sscanf(dev->name,
5735 "IBM ThinkPad Embedded Controller -[%17c",
5736 ec_fw_string) == 1) {
5737 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5738 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005739
5740 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5741 tp->ec_model = ec_fw_string[0]
5742 | (ec_fw_string[1] << 8);
5743 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005744 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005745 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005746
5747 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5748 GFP_KERNEL);
5749 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5750 kfree(tp->model_str);
5751 tp->model_str = NULL;
5752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753}
5754
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005755static int __init probe_for_thinkpad(void)
5756{
5757 int is_thinkpad;
5758
5759 if (acpi_disabled)
5760 return -ENODEV;
5761
5762 /*
5763 * Non-ancient models have better DMI tagging, but very old models
5764 * don't.
5765 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005766 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005767
5768 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005769 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005770 if (!ec_handle) {
5771 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005772 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005773 "Not yet supported ThinkPad detected!\n");
5774 return -ENODEV;
5775 }
5776
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005777 /*
5778 * Risks a regression on very old machines, but reduces potential
5779 * false positives a damn great deal
5780 */
5781 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005782 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005783
5784 if (!is_thinkpad && !force_load)
5785 return -ENODEV;
5786
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005787 return 0;
5788}
5789
5790
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005791/* Module init, exit, parameters */
5792
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005793static struct ibm_init_struct ibms_init[] __initdata = {
5794 {
5795 .init = thinkpad_acpi_driver_init,
5796 .data = &thinkpad_acpi_driver_data,
5797 },
5798 {
5799 .init = hotkey_init,
5800 .data = &hotkey_driver_data,
5801 },
5802 {
5803 .init = bluetooth_init,
5804 .data = &bluetooth_driver_data,
5805 },
5806 {
5807 .init = wan_init,
5808 .data = &wan_driver_data,
5809 },
5810 {
5811 .init = video_init,
5812 .data = &video_driver_data,
5813 },
5814 {
5815 .init = light_init,
5816 .data = &light_driver_data,
5817 },
5818#ifdef CONFIG_THINKPAD_ACPI_DOCK
5819 {
5820 .init = dock_init,
5821 .data = &dock_driver_data[0],
5822 },
5823 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005824 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005825 .data = &dock_driver_data[1],
5826 },
5827#endif
5828#ifdef CONFIG_THINKPAD_ACPI_BAY
5829 {
5830 .init = bay_init,
5831 .data = &bay_driver_data,
5832 },
5833#endif
5834 {
5835 .init = cmos_init,
5836 .data = &cmos_driver_data,
5837 },
5838 {
5839 .init = led_init,
5840 .data = &led_driver_data,
5841 },
5842 {
5843 .init = beep_init,
5844 .data = &beep_driver_data,
5845 },
5846 {
5847 .init = thermal_init,
5848 .data = &thermal_driver_data,
5849 },
5850 {
5851 .data = &ecdump_driver_data,
5852 },
5853 {
5854 .init = brightness_init,
5855 .data = &brightness_driver_data,
5856 },
5857 {
5858 .data = &volume_driver_data,
5859 },
5860 {
5861 .init = fan_init,
5862 .data = &fan_driver_data,
5863 },
5864};
5865
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005866static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5867{
5868 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005869 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005870
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005871 if (!kp || !kp->name || !val)
5872 return -EINVAL;
5873
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005874 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5875 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005876 WARN_ON(ibm == NULL);
5877
5878 if (!ibm || !ibm->name)
5879 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005880
5881 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5882 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005883 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005884 strcpy(ibms_init[i].param, val);
5885 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005886 return 0;
5887 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005888 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005889
5890 return -EINVAL;
5891}
5892
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005893module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005894MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005895 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005896
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005897module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005898MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005899
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005900module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005901MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005902 "Attempts to load the driver even on a "
5903 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005904
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005905module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005906MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005907 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005908
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005909module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005910MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005911 "Selects brightness control strategy: "
5912 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005913
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005914module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005915MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005916 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005917
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005918module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005919MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005920 "used for backwards compatibility with userspace, "
5921 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005922
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005923#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005924 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02005925 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005926 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005927
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005928TPACPI_PARAM(hotkey);
5929TPACPI_PARAM(bluetooth);
5930TPACPI_PARAM(video);
5931TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005932#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005933TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005934#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005935#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005936TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005937#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005938TPACPI_PARAM(cmos);
5939TPACPI_PARAM(led);
5940TPACPI_PARAM(beep);
5941TPACPI_PARAM(ecdump);
5942TPACPI_PARAM(brightness);
5943TPACPI_PARAM(volume);
5944TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005945
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005946static void thinkpad_acpi_module_exit(void)
5947{
5948 struct ibm_struct *ibm, *itmp;
5949
5950 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5951
5952 list_for_each_entry_safe_reverse(ibm, itmp,
5953 &tpacpi_all_drivers,
5954 all_drivers) {
5955 ibm_exit(ibm);
5956 }
5957
5958 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5959
5960 if (tpacpi_inputdev) {
5961 if (tp_features.input_device_registered)
5962 input_unregister_device(tpacpi_inputdev);
5963 else
5964 input_free_device(tpacpi_inputdev);
5965 }
5966
5967 if (tpacpi_hwmon)
5968 hwmon_device_unregister(tpacpi_hwmon);
5969
5970 if (tp_features.sensors_pdev_attrs_registered)
5971 device_remove_file(&tpacpi_sensors_pdev->dev,
5972 &dev_attr_thinkpad_acpi_pdev_name);
5973 if (tpacpi_sensors_pdev)
5974 platform_device_unregister(tpacpi_sensors_pdev);
5975 if (tpacpi_pdev)
5976 platform_device_unregister(tpacpi_pdev);
5977
5978 if (tp_features.sensors_pdrv_attrs_registered)
5979 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5980 if (tp_features.platform_drv_attrs_registered)
5981 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5982
5983 if (tp_features.sensors_pdrv_registered)
5984 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5985
5986 if (tp_features.platform_drv_registered)
5987 platform_driver_unregister(&tpacpi_pdriver);
5988
5989 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005990 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005991
5992 kfree(thinkpad_id.bios_version_str);
5993 kfree(thinkpad_id.ec_version_str);
5994 kfree(thinkpad_id.model_str);
5995}
5996
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005997
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005998static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999{
6000 int ret, i;
6001
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006002 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6003
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006004 /* Parameter checking */
6005 if (hotkey_report_mode > 2)
6006 return -EINVAL;
6007
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006008 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006009
6010 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006011 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006012 if (ret) {
6013 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006014 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006017 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006018
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006019 TPACPI_ACPIHANDLE_INIT(ecrd);
6020 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006022 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006024 printk(TPACPI_ERR
6025 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006026 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 return -ENODEV;
6028 }
6029 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006030
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006031 ret = platform_driver_register(&tpacpi_pdriver);
6032 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006033 printk(TPACPI_ERR
6034 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006035 thinkpad_acpi_module_exit();
6036 return ret;
6037 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006038 tp_features.platform_drv_registered = 1;
6039
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006040 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6041 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006042 printk(TPACPI_ERR
6043 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006044 thinkpad_acpi_module_exit();
6045 return ret;
6046 }
6047 tp_features.sensors_pdrv_registered = 1;
6048
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006049 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006050 if (!ret) {
6051 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006052 ret = tpacpi_create_driver_attributes(
6053 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006054 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006055 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006056 printk(TPACPI_ERR
6057 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006058 thinkpad_acpi_module_exit();
6059 return ret;
6060 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006061 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006062
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006063
6064 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006065 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006066 NULL, 0);
6067 if (IS_ERR(tpacpi_pdev)) {
6068 ret = PTR_ERR(tpacpi_pdev);
6069 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006070 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006071 thinkpad_acpi_module_exit();
6072 return ret;
6073 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006074 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006075 TPACPI_HWMON_DRVR_NAME,
6076 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006077 if (IS_ERR(tpacpi_sensors_pdev)) {
6078 ret = PTR_ERR(tpacpi_sensors_pdev);
6079 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006080 printk(TPACPI_ERR
6081 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006082 thinkpad_acpi_module_exit();
6083 return ret;
6084 }
6085 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6086 &dev_attr_thinkpad_acpi_pdev_name);
6087 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006088 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006089 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006090 thinkpad_acpi_module_exit();
6091 return ret;
6092 }
6093 tp_features.sensors_pdev_attrs_registered = 1;
6094 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006095 if (IS_ERR(tpacpi_hwmon)) {
6096 ret = PTR_ERR(tpacpi_hwmon);
6097 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006098 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006099 thinkpad_acpi_module_exit();
6100 return ret;
6101 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006102 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006103 tpacpi_inputdev = input_allocate_device();
6104 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006105 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006106 thinkpad_acpi_module_exit();
6107 return -ENOMEM;
6108 } else {
6109 /* Prepare input device, but don't register */
6110 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006111 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006112 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006113 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6114 thinkpad_id.vendor :
6115 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006116 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6117 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6118 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006119 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6120 ret = ibm_init(&ibms_init[i]);
6121 if (ret >= 0 && *ibms_init[i].param)
6122 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006124 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 return ret;
6126 }
6127 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006128 ret = input_register_device(tpacpi_inputdev);
6129 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006130 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006131 thinkpad_acpi_module_exit();
6132 return ret;
6133 } else {
6134 tp_features.input_device_registered = 1;
6135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006137 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 return 0;
6139}
6140
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006141/* Please remove this in year 2009 */
6142MODULE_ALIAS("ibm_acpi");
6143
6144/*
6145 * DMI matching for module autoloading
6146 *
6147 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6148 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6149 *
6150 * Only models listed in thinkwiki will be supported, so add yours
6151 * if it is not there yet.
6152 */
6153#define IBM_BIOS_MODULE_ALIAS(__type) \
6154 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6155
6156/* Non-ancient thinkpads */
6157MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6158MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6159
6160/* Ancient thinkpad BIOSes have to be identified by
6161 * BIOS type or model number, and there are far less
6162 * BIOS types than model numbers... */
6163IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6164IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6165IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6166
6167MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006168MODULE_DESCRIPTION(TPACPI_DESC);
6169MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006170MODULE_LICENSE("GPL");
6171
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006172module_init(thinkpad_acpi_module_init);
6173module_exit(thinkpad_acpi_module_exit);