blob: 6da3f40ac9f62a247f9f2126eb9f663ccbd338b5 [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 Holschuh56b6aeb2007-03-23 17:33:57 -03002037 }
2038
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002039 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002040}
2041
2042static void hotkey_exit(void)
2043{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002044#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2045 hotkey_poll_stop_sync();
2046#endif
2047
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002048 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002049 dbg_printk(TPACPI_DBG_EXIT,
2050 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002051 /* no short-circuit boolean operator below! */
2052 if ((hotkey_mask_set(hotkey_orig_mask) |
2053 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002054 printk(TPACPI_ERR
2055 "failed to restore hot key mask "
2056 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002057 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002058
2059 if (hotkey_dev_attributes) {
2060 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2061 hotkey_dev_attributes = NULL;
2062 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002063}
2064
2065static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2066{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002067 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002068 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002069 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002070 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002071 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002072
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002073 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002074 printk(TPACPI_ERR
2075 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002076 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002077 acpi_bus_generate_netlink_event(
2078 ibm->acpi->device->pnp.device_class,
2079 ibm->acpi->device->dev.bus_id,
2080 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002081 return;
2082 }
2083
2084 while (1) {
2085 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002086 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002087 return;
2088 }
2089
2090 if (hkey == 0) {
2091 /* queue empty */
2092 return;
2093 }
2094
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002095 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002096 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002097 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002098
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002099 switch (hkey >> 12) {
2100 case 1:
2101 /* 0x1000-0x1FFF: key presses */
2102 scancode = hkey & 0xfff;
2103 if (scancode > 0 && scancode < 0x21) {
2104 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002105 if (!(hotkey_source_mask & (1 << scancode))) {
2106 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002107 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002108 } else {
2109 ignore_acpi_ev = 1;
2110 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002111 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002112 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002113 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002114 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002115 case 2:
2116 /* Wakeup reason */
2117 switch (hkey) {
2118 case 0x2304: /* suspend, undock */
2119 case 0x2404: /* hibernation, undock */
2120 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2121 ignore_acpi_ev = 1;
2122 break;
2123 case 0x2305: /* suspend, bay eject */
2124 case 0x2405: /* hibernation, bay eject */
2125 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2126 ignore_acpi_ev = 1;
2127 break;
2128 default:
2129 unk_ev = 1;
2130 }
2131 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2132 printk(TPACPI_INFO
2133 "woke up due to a hot-unplug "
2134 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002135 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002136 }
2137 break;
2138 case 3:
2139 /* bay-related wakeups */
2140 if (hkey == 0x3003) {
2141 hotkey_autosleep_ack = 1;
2142 printk(TPACPI_INFO
2143 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002144 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002145 } else {
2146 unk_ev = 1;
2147 }
2148 break;
2149 case 4:
2150 /* dock-related wakeups */
2151 if (hkey == 0x4003) {
2152 hotkey_autosleep_ack = 1;
2153 printk(TPACPI_INFO
2154 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002155 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002156 } else {
2157 unk_ev = 1;
2158 }
2159 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002160 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002161 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002162 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002163 case 0x5010: /* Lenovo new BIOS: brightness changed */
2164 case 0x5009: /* X61t: swivel up (tablet mode) */
2165 case 0x500a: /* X61t: swivel down (normal mode) */
2166 case 0x500b: /* X61t: tablet pen inserted into bay */
2167 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002168 break;
2169 case 0x5001:
2170 case 0x5002:
2171 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002172 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002173 break;
2174 default:
2175 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002176 }
2177 break;
2178 case 7:
2179 /* 0x7000-0x7FFF: misc */
2180 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2181 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002182 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002183 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002184 break;
2185 }
2186 /* fallthrough to default */
2187 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002188 unk_ev = 1;
2189 }
2190 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002191 printk(TPACPI_NOTICE
2192 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002193 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002194
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002195 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002196 if (!ignore_acpi_ev &&
2197 (send_acpi_ev || hotkey_report_mode < 2)) {
2198 acpi_bus_generate_proc_event(ibm->acpi->device,
2199 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002200 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002201
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002202 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002203 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002204 acpi_bus_generate_netlink_event(
2205 ibm->acpi->device->pnp.device_class,
2206 ibm->acpi->device->dev.bus_id,
2207 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002208 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002209 }
2210}
2211
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002212static void hotkey_suspend(pm_message_t state)
2213{
2214 /* Do these on suspend, we get the events on early resume! */
2215 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2216 hotkey_autosleep_ack = 0;
2217}
2218
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002219static void hotkey_resume(void)
2220{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002221 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002222 printk(TPACPI_ERR
2223 "error while trying to read hot key mask "
2224 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002225 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002226 hotkey_radio_sw_notify_change();
2227 hotkey_wakeup_reason_notify_change();
2228 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002229 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002230}
2231
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002232/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002233static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002235 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 int len = 0;
2237
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002238 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002239 len += sprintf(p + len, "status:\t\tnot supported\n");
2240 return len;
2241 }
2242
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002243 if (mutex_lock_interruptible(&hotkey_mutex))
2244 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002245 res = hotkey_status_get(&status);
2246 if (!res)
2247 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002248 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002249 if (res)
2250 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002253 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002254 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 len += sprintf(p + len,
2256 "commands:\tenable, disable, reset, <mask>\n");
2257 } else {
2258 len += sprintf(p + len, "mask:\t\tnot supported\n");
2259 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2260 }
2261
2262 return len;
2263}
2264
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002265static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002267 int res, status;
2268 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002271 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return -ENODEV;
2273
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002274 if (mutex_lock_interruptible(&hotkey_mutex))
2275 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002276
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002277 status = -1;
2278 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002279
2280 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 while ((cmd = next_cmd(&buf))) {
2282 if (strlencmp(cmd, "enable") == 0) {
2283 status = 1;
2284 } else if (strlencmp(cmd, "disable") == 0) {
2285 status = 0;
2286 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002287 status = hotkey_orig_status;
2288 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2290 /* mask set */
2291 } else if (sscanf(cmd, "%x", &mask) == 1) {
2292 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002293 } else {
2294 res = -EINVAL;
2295 goto errexit;
2296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002298 if (status != -1)
2299 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002301 if (!res && mask != hotkey_mask)
2302 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002304errexit:
2305 mutex_unlock(&hotkey_mutex);
2306 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002307}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002309static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002310 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002311 {"", 0},
2312};
2313
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002314static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002315 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002316 .notify = hotkey_notify,
2317 .handle = &hkey_handle,
2318 .type = ACPI_DEVICE_NOTIFY,
2319};
2320
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002321static struct ibm_struct hotkey_driver_data = {
2322 .name = "hotkey",
2323 .read = hotkey_read,
2324 .write = hotkey_write,
2325 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002326 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002327 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002328 .acpi = &ibm_hotkey_acpidriver,
2329};
2330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002331/*************************************************************************
2332 * Bluetooth subdriver
2333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002335enum {
2336 /* ACPI GBDC/SBDC bits */
2337 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2338 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2339 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2340};
2341
2342static int bluetooth_get_radiosw(void);
2343static int bluetooth_set_radiosw(int radio_on);
2344
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002345/* sysfs bluetooth enable ---------------------------------------------- */
2346static ssize_t bluetooth_enable_show(struct device *dev,
2347 struct device_attribute *attr,
2348 char *buf)
2349{
2350 int status;
2351
2352 status = bluetooth_get_radiosw();
2353 if (status < 0)
2354 return status;
2355
2356 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2357}
2358
2359static ssize_t bluetooth_enable_store(struct device *dev,
2360 struct device_attribute *attr,
2361 const char *buf, size_t count)
2362{
2363 unsigned long t;
2364 int res;
2365
2366 if (parse_strtoul(buf, 1, &t))
2367 return -EINVAL;
2368
2369 res = bluetooth_set_radiosw(t);
2370
2371 return (res) ? res : count;
2372}
2373
2374static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002375 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002376 bluetooth_enable_show, bluetooth_enable_store);
2377
2378/* --------------------------------------------------------------------- */
2379
2380static struct attribute *bluetooth_attributes[] = {
2381 &dev_attr_bluetooth_enable.attr,
2382 NULL
2383};
2384
2385static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002386 .attrs = bluetooth_attributes,
2387};
2388
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002389static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002391 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002392 int status = 0;
2393
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002394 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2395
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002396 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002397
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002398 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2399 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002400 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002401 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002403 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2404 str_supported(tp_features.bluetooth),
2405 status);
2406
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002407 if (tp_features.bluetooth) {
2408 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2409 /* no bluetooth hardware present in system */
2410 tp_features.bluetooth = 0;
2411 dbg_printk(TPACPI_DBG_INIT,
2412 "bluetooth hardware not installed\n");
2413 } else {
2414 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2415 &bluetooth_attr_group);
2416 if (res)
2417 return res;
2418 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002419 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002420
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002421 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002424static void bluetooth_exit(void)
2425{
2426 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2427 &bluetooth_attr_group);
2428}
2429
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002430static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
2432 int status;
2433
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002434 if (!tp_features.bluetooth)
2435 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002437 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2438 return -EIO;
2439
2440 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2441}
2442
2443static int bluetooth_set_radiosw(int radio_on)
2444{
2445 int status;
2446
2447 if (!tp_features.bluetooth)
2448 return -ENODEV;
2449
2450 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2451 return -EIO;
2452 if (radio_on)
2453 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2454 else
2455 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2456 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2457 return -EIO;
2458
2459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002462/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002463static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002466 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002468 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002471 len += sprintf(p + len, "status:\t\t%s\n",
2472 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 len += sprintf(p + len, "commands:\tenable, disable\n");
2474 }
2475
2476 return len;
2477}
2478
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002479static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002483 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002484 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 while ((cmd = next_cmd(&buf))) {
2487 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002488 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002490 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 } else
2492 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return 0;
2496}
2497
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002498static struct ibm_struct bluetooth_driver_data = {
2499 .name = "bluetooth",
2500 .read = bluetooth_read,
2501 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002502 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002503};
2504
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002505/*************************************************************************
2506 * Wan subdriver
2507 */
2508
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002509enum {
2510 /* ACPI GWAN/SWAN bits */
2511 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2512 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2513 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2514};
2515
2516static int wan_get_radiosw(void);
2517static int wan_set_radiosw(int radio_on);
2518
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002519/* sysfs wan enable ---------------------------------------------------- */
2520static ssize_t wan_enable_show(struct device *dev,
2521 struct device_attribute *attr,
2522 char *buf)
2523{
2524 int status;
2525
2526 status = wan_get_radiosw();
2527 if (status < 0)
2528 return status;
2529
2530 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2531}
2532
2533static ssize_t wan_enable_store(struct device *dev,
2534 struct device_attribute *attr,
2535 const char *buf, size_t count)
2536{
2537 unsigned long t;
2538 int res;
2539
2540 if (parse_strtoul(buf, 1, &t))
2541 return -EINVAL;
2542
2543 res = wan_set_radiosw(t);
2544
2545 return (res) ? res : count;
2546}
2547
2548static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002549 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002550 wan_enable_show, wan_enable_store);
2551
2552/* --------------------------------------------------------------------- */
2553
2554static struct attribute *wan_attributes[] = {
2555 &dev_attr_wan_enable.attr,
2556 NULL
2557};
2558
2559static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002560 .attrs = wan_attributes,
2561};
2562
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002563static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002564{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002565 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002566 int status = 0;
2567
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002568 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2569
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002570 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002571
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002572 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002573 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002574
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002575 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2576 str_supported(tp_features.wan),
2577 status);
2578
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002579 if (tp_features.wan) {
2580 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2581 /* no wan hardware present in system */
2582 tp_features.wan = 0;
2583 dbg_printk(TPACPI_DBG_INIT,
2584 "wan hardware not installed\n");
2585 } else {
2586 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2587 &wan_attr_group);
2588 if (res)
2589 return res;
2590 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002591 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002592
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002593 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002594}
2595
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002596static void wan_exit(void)
2597{
2598 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2599 &wan_attr_group);
2600}
2601
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002602static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002603{
2604 int status;
2605
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002606 if (!tp_features.wan)
2607 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002608
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002609 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2610 return -EIO;
2611
2612 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2613}
2614
2615static int wan_set_radiosw(int radio_on)
2616{
2617 int status;
2618
2619 if (!tp_features.wan)
2620 return -ENODEV;
2621
2622 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2623 return -EIO;
2624 if (radio_on)
2625 status |= TP_ACPI_WANCARD_RADIOSSW;
2626 else
2627 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2628 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2629 return -EIO;
2630
2631 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002632}
2633
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002634/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002635static int wan_read(char *p)
2636{
2637 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002638 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002639
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002640 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002641 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002642 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002643 len += sprintf(p + len, "status:\t\t%s\n",
2644 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002645 len += sprintf(p + len, "commands:\tenable, disable\n");
2646 }
2647
2648 return len;
2649}
2650
2651static int wan_write(char *buf)
2652{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002653 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002654
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002655 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002656 return -ENODEV;
2657
2658 while ((cmd = next_cmd(&buf))) {
2659 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002660 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002661 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002662 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002663 } else
2664 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002665 }
2666
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002667 return 0;
2668}
2669
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002670static struct ibm_struct wan_driver_data = {
2671 .name = "wan",
2672 .read = wan_read,
2673 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002674 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002675 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002676};
2677
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002678/*************************************************************************
2679 * Video subdriver
2680 */
2681
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002682enum video_access_mode {
2683 TPACPI_VIDEO_NONE = 0,
2684 TPACPI_VIDEO_570, /* 570 */
2685 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2686 TPACPI_VIDEO_NEW, /* all others */
2687};
2688
2689enum { /* video status flags, based on VIDEO_570 */
2690 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2691 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2692 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2693};
2694
2695enum { /* TPACPI_VIDEO_570 constants */
2696 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2697 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2698 * video_status_flags */
2699 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2700 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2701};
2702
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002703static enum video_access_mode video_supported;
2704static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002705
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002706static int video_autosw_get(void);
2707static int video_autosw_set(int enable);
2708
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002709TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002710 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2711 "\\_SB.PCI0.VID0", /* 770e */
2712 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2713 "\\_SB.PCI0.AGP.VID", /* all others */
2714 ); /* R30, R31 */
2715
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002716TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002717
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002718static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002720 int ivga;
2721
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002722 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2723
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002724 TPACPI_ACPIHANDLE_INIT(vid);
2725 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002726
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002727 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2728 /* G41, assume IVGA doesn't change */
2729 vid_handle = vid2_handle;
2730
2731 if (!vid_handle)
2732 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002733 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002734 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2735 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002736 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002737 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2738 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002739 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002740 else
2741 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002742 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002744 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2745 str_supported(video_supported != TPACPI_VIDEO_NONE),
2746 video_supported);
2747
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002748 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002751static void video_exit(void)
2752{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002753 dbg_printk(TPACPI_DBG_EXIT,
2754 "restoring original video autoswitch mode\n");
2755 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002756 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002757 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002758}
2759
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002760static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 int status = 0;
2763 int i;
2764
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002765 switch (video_supported) {
2766 case TPACPI_VIDEO_570:
2767 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2768 TP_ACPI_VIDEO_570_PHSCMD))
2769 return -EIO;
2770 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2771 break;
2772 case TPACPI_VIDEO_770:
2773 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2774 return -EIO;
2775 if (i)
2776 status |= TP_ACPI_VIDEO_S_LCD;
2777 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2778 return -EIO;
2779 if (i)
2780 status |= TP_ACPI_VIDEO_S_CRT;
2781 break;
2782 case TPACPI_VIDEO_NEW:
2783 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2784 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2785 return -EIO;
2786 if (i)
2787 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002789 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2790 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2791 return -EIO;
2792 if (i)
2793 status |= TP_ACPI_VIDEO_S_LCD;
2794 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2795 return -EIO;
2796 if (i)
2797 status |= TP_ACPI_VIDEO_S_DVI;
2798 break;
2799 default:
2800 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 return status;
2804}
2805
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002806static int video_outputsw_set(int status)
2807{
2808 int autosw;
2809 int res = 0;
2810
2811 switch (video_supported) {
2812 case TPACPI_VIDEO_570:
2813 res = acpi_evalf(NULL, NULL,
2814 "\\_SB.PHS2", "vdd",
2815 TP_ACPI_VIDEO_570_PHS2CMD,
2816 status | TP_ACPI_VIDEO_570_PHS2SET);
2817 break;
2818 case TPACPI_VIDEO_770:
2819 autosw = video_autosw_get();
2820 if (autosw < 0)
2821 return autosw;
2822
2823 res = video_autosw_set(1);
2824 if (res)
2825 return res;
2826 res = acpi_evalf(vid_handle, NULL,
2827 "ASWT", "vdd", status * 0x100, 0);
2828 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002829 printk(TPACPI_ERR
2830 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002831 return -EIO;
2832 }
2833 break;
2834 case TPACPI_VIDEO_NEW:
2835 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002836 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002837 break;
2838 default:
2839 return -ENOSYS;
2840 }
2841
2842 return (res)? 0 : -EIO;
2843}
2844
2845static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002847 int autosw = 0;
2848
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002849 switch (video_supported) {
2850 case TPACPI_VIDEO_570:
2851 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2852 return -EIO;
2853 break;
2854 case TPACPI_VIDEO_770:
2855 case TPACPI_VIDEO_NEW:
2856 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2857 return -EIO;
2858 break;
2859 default:
2860 return -ENOSYS;
2861 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002862
2863 return autosw & 1;
2864}
2865
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002866static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002867{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002868 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002869 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002870 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002871}
2872
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002873static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002874{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002875 int autosw = video_autosw_get();
2876 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002877
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002878 if (autosw < 0)
2879 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002880
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002881 switch (video_supported) {
2882 case TPACPI_VIDEO_570:
2883 res = video_autosw_set(1);
2884 if (res)
2885 return res;
2886 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2887 break;
2888 case TPACPI_VIDEO_770:
2889 case TPACPI_VIDEO_NEW:
2890 res = video_autosw_set(1);
2891 if (res)
2892 return res;
2893 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2894 break;
2895 default:
2896 return -ENOSYS;
2897 }
2898 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002899 printk(TPACPI_ERR
2900 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002901 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002902 }
2903
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002904 return (res)? 0 : -EIO;
2905}
2906
2907static int video_expand_toggle(void)
2908{
2909 switch (video_supported) {
2910 case TPACPI_VIDEO_570:
2911 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2912 0 : -EIO;
2913 case TPACPI_VIDEO_770:
2914 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2915 0 : -EIO;
2916 case TPACPI_VIDEO_NEW:
2917 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2918 0 : -EIO;
2919 default:
2920 return -ENOSYS;
2921 }
2922 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002923}
2924
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002925static int video_read(char *p)
2926{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002927 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002928 int len = 0;
2929
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002930 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002931 len += sprintf(p + len, "status:\t\tnot supported\n");
2932 return len;
2933 }
2934
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002935 status = video_outputsw_get();
2936 if (status < 0)
2937 return status;
2938
2939 autosw = video_autosw_get();
2940 if (autosw < 0)
2941 return autosw;
2942
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002943 len += sprintf(p + len, "status:\t\tsupported\n");
2944 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2945 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002946 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002947 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2948 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2949 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2950 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002951 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002952 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2953 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2954 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2955
2956 return len;
2957}
2958
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002959static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
2961 char *cmd;
2962 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002963 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002965 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002966 return -ENODEV;
2967
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002968 enable = 0;
2969 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 while ((cmd = next_cmd(&buf))) {
2972 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002973 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002975 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002977 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002979 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002980 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002981 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002982 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002983 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002984 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002985 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002987 res = video_autosw_set(1);
2988 if (res)
2989 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002991 res = video_autosw_set(0);
2992 if (res)
2993 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002995 res = video_outputsw_cycle();
2996 if (res)
2997 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002999 res = video_expand_toggle();
3000 if (res)
3001 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 } else
3003 return -EINVAL;
3004 }
3005
3006 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003007 status = video_outputsw_get();
3008 if (status < 0)
3009 return status;
3010 res = video_outputsw_set((status & ~disable) | enable);
3011 if (res)
3012 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
3014
3015 return 0;
3016}
3017
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003018static struct ibm_struct video_driver_data = {
3019 .name = "video",
3020 .read = video_read,
3021 .write = video_write,
3022 .exit = video_exit,
3023};
3024
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003025/*************************************************************************
3026 * Light (thinklight) subdriver
3027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003029TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3030TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003031
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003032static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003034 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3035
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003036 TPACPI_ACPIHANDLE_INIT(ledb);
3037 TPACPI_ACPIHANDLE_INIT(lght);
3038 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003039
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003040 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003041 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003042
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003043 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003044 /* light status not supported on
3045 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003046 tp_features.light_status =
3047 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003049 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003050 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003051
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003052 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053}
3054
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003055static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 int len = 0;
3058 int status = 0;
3059
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003060 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003061 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003062 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003063 len += sprintf(p + len, "status:\t\tunknown\n");
3064 len += sprintf(p + len, "commands:\ton, off\n");
3065 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3067 return -EIO;
3068 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003069 len += sprintf(p + len, "commands:\ton, off\n");
3070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 return len;
3073}
3074
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003075static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
3077 int cmos_cmd, lght_cmd;
3078 char *cmd;
3079 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003080
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003081 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003082 return -ENODEV;
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 while ((cmd = next_cmd(&buf))) {
3085 if (strlencmp(cmd, "on") == 0) {
3086 cmos_cmd = 0x0c;
3087 lght_cmd = 1;
3088 } else if (strlencmp(cmd, "off") == 0) {
3089 cmos_cmd = 0x0d;
3090 lght_cmd = 0;
3091 } else
3092 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003095 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3096 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (!success)
3098 return -EIO;
3099 }
3100
3101 return 0;
3102}
3103
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003104static struct ibm_struct light_driver_data = {
3105 .name = "light",
3106 .read = light_read,
3107 .write = light_write,
3108};
3109
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003110/*************************************************************************
3111 * Dock subdriver
3112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003114#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003115
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003116static void dock_notify(struct ibm_struct *ibm, u32 event);
3117static int dock_read(char *p);
3118static int dock_write(char *buf);
3119
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003120TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003121 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3122 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3123 "\\_SB.PCI.ISA.SLCE", /* 570 */
3124 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3125
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003126/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003127TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003128
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003129static const struct acpi_device_id ibm_pci_device_ids[] = {
3130 {PCI_ROOT_HID_STRING, 0},
3131 {"", 0},
3132};
3133
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003134static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3135 {
3136 .notify = dock_notify,
3137 .handle = &dock_handle,
3138 .type = ACPI_SYSTEM_NOTIFY,
3139 },
3140 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003141 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3142 * We just use it to get notifications of dock hotplug
3143 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003144 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003145 .notify = dock_notify,
3146 .handle = &pci_handle,
3147 .type = ACPI_SYSTEM_NOTIFY,
3148 },
3149};
3150
3151static struct ibm_struct dock_driver_data[2] = {
3152 {
3153 .name = "dock",
3154 .read = dock_read,
3155 .write = dock_write,
3156 .acpi = &ibm_dock_acpidriver[0],
3157 },
3158 {
3159 .name = "dock",
3160 .acpi = &ibm_dock_acpidriver[1],
3161 },
3162};
3163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164#define dock_docked() (_sta(dock_handle) & 1)
3165
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003166static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003167{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003168 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3169
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003170 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003171
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003172 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3173 str_supported(dock_handle != NULL));
3174
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003175 return (dock_handle)? 0 : 1;
3176}
3177
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003178static int __init dock_init2(struct ibm_init_struct *iibm)
3179{
3180 int dock2_needed;
3181
3182 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3183
3184 if (dock_driver_data[0].flags.acpi_driver_registered &&
3185 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003186 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003187 dock2_needed = (pci_handle != NULL);
3188 vdbg_printk(TPACPI_DBG_INIT,
3189 "dock PCI handler for the TP 570 is %s\n",
3190 str_supported(dock2_needed));
3191 } else {
3192 vdbg_printk(TPACPI_DBG_INIT,
3193 "dock subdriver part 2 not required\n");
3194 dock2_needed = 0;
3195 }
3196
3197 return (dock2_needed)? 0 : 1;
3198}
3199
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003200static void dock_notify(struct ibm_struct *ibm, u32 event)
3201{
3202 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003203 int pci = ibm->acpi->hid && ibm->acpi->device &&
3204 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003205 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003206
3207 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003208 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003209 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003210 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003211 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003212 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003213 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003214 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003215 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003216 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003217 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003218 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003219 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003220 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003221 }
Len Brown14e04fb32007-08-23 15:20:26 -04003222 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003223 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3224 ibm->acpi->device->dev.bus_id,
3225 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003226}
3227
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003228static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 int len = 0;
3231 int docked = dock_docked();
3232
3233 if (!dock_handle)
3234 len += sprintf(p + len, "status:\t\tnot supported\n");
3235 else if (!docked)
3236 len += sprintf(p + len, "status:\t\tundocked\n");
3237 else {
3238 len += sprintf(p + len, "status:\t\tdocked\n");
3239 len += sprintf(p + len, "commands:\tdock, undock\n");
3240 }
3241
3242 return len;
3243}
3244
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003245static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
3247 char *cmd;
3248
3249 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003250 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252 while ((cmd = next_cmd(&buf))) {
3253 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003254 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3255 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return -EIO;
3257 } else if (strlencmp(cmd, "dock") == 0) {
3258 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3259 return -EIO;
3260 } else
3261 return -EINVAL;
3262 }
3263
3264 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003267#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003269/*************************************************************************
3270 * Bay subdriver
3271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003273#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003274
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003275TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003276 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3277 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3278 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3279 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003280TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003281 "_EJ0", /* all others */
3282 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003283TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003284 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3285 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003286TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003287 "_EJ0", /* 770x */
3288 ); /* all others */
3289
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003290static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003292 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3293
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003294 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003295 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003296 TPACPI_ACPIHANDLE_INIT(bay_ej);
3297 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003298 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003299 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003300
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003301 tp_features.bay_status = bay_handle &&
3302 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3303 tp_features.bay_status2 = bay2_handle &&
3304 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003305
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003306 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3307 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3308 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3309 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003311 vdbg_printk(TPACPI_DBG_INIT,
3312 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003313 str_supported(tp_features.bay_status),
3314 str_supported(tp_features.bay_eject),
3315 str_supported(tp_features.bay_status2),
3316 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003317
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003318 return (tp_features.bay_status || tp_features.bay_eject ||
3319 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003322static void bay_notify(struct ibm_struct *ibm, u32 event)
3323{
Len Brown14e04fb32007-08-23 15:20:26 -04003324 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003325 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3326 ibm->acpi->device->dev.bus_id,
3327 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003328}
3329
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003330#define bay_occupied(b) (_sta(b##_handle) & 1)
3331
3332static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003335 int occupied = bay_occupied(bay);
3336 int occupied2 = bay_occupied(bay2);
3337 int eject, eject2;
3338
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003339 len += sprintf(p + len, "status:\t\t%s\n",
3340 tp_features.bay_status ?
3341 (occupied ? "occupied" : "unoccupied") :
3342 "not supported");
3343 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003344 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3345 "occupied" : "unoccupied");
3346
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003347 eject = tp_features.bay_eject && occupied;
3348 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003349
3350 if (eject && eject2)
3351 len += sprintf(p + len, "commands:\teject, eject2\n");
3352 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003354 else if (eject2)
3355 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357 return len;
3358}
3359
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003360static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
3362 char *cmd;
3363
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003364 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003365 return -ENODEV;
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003368 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003369 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3370 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003371 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003372 strlencmp(cmd, "eject2") == 0) {
3373 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return -EIO;
3375 } else
3376 return -EINVAL;
3377 }
3378
3379 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003380}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003381
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003382static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3383 .notify = bay_notify,
3384 .handle = &bay_handle,
3385 .type = ACPI_SYSTEM_NOTIFY,
3386};
3387
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003388static struct ibm_struct bay_driver_data = {
3389 .name = "bay",
3390 .read = bay_read,
3391 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003392 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003393};
3394
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003395#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003397/*************************************************************************
3398 * CMOS subdriver
3399 */
3400
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003401/* sysfs cmos_command -------------------------------------------------- */
3402static ssize_t cmos_command_store(struct device *dev,
3403 struct device_attribute *attr,
3404 const char *buf, size_t count)
3405{
3406 unsigned long cmos_cmd;
3407 int res;
3408
3409 if (parse_strtoul(buf, 21, &cmos_cmd))
3410 return -EINVAL;
3411
3412 res = issue_thinkpad_cmos_command(cmos_cmd);
3413 return (res)? res : count;
3414}
3415
3416static struct device_attribute dev_attr_cmos_command =
3417 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3418
3419/* --------------------------------------------------------------------- */
3420
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003421static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003422{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003423 int res;
3424
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003425 vdbg_printk(TPACPI_DBG_INIT,
3426 "initializing cmos commands subdriver\n");
3427
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003428 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003429
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003430 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3431 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003432
3433 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3434 if (res)
3435 return res;
3436
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003437 return (cmos_handle)? 0 : 1;
3438}
3439
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003440static void cmos_exit(void)
3441{
3442 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3443}
3444
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003445static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
3447 int len = 0;
3448
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003449 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3450 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 if (!cmos_handle)
3452 len += sprintf(p + len, "status:\t\tnot supported\n");
3453 else {
3454 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003455 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
3457
3458 return len;
3459}
3460
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003461static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
3463 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003464 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003467 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3468 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 /* cmos_cmd set */
3470 } else
3471 return -EINVAL;
3472
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003473 res = issue_thinkpad_cmos_command(cmos_cmd);
3474 if (res)
3475 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 }
3477
3478 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003479}
3480
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003481static struct ibm_struct cmos_driver_data = {
3482 .name = "cmos",
3483 .read = cmos_read,
3484 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003485 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003486};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003487
3488/*************************************************************************
3489 * LED subdriver
3490 */
3491
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003492enum led_access_mode {
3493 TPACPI_LED_NONE = 0,
3494 TPACPI_LED_570, /* 570 */
3495 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3496 TPACPI_LED_NEW, /* all others */
3497};
3498
3499enum { /* For TPACPI_LED_OLD */
3500 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3501 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3502 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3503};
3504
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003505static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003506
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003507TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003508 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3509 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003510 "LED", /* all others */
3511 ); /* R30, R31 */
3512
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003513static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003514{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003515 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3516
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003517 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003518
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003519 if (!led_handle)
3520 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003521 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003522 else if (strlencmp(led_path, "SLED") == 0)
3523 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003524 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003525 else if (strlencmp(led_path, "SYSL") == 0)
3526 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003527 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003528 else
3529 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003530 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003531
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003532 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3533 str_supported(led_supported), led_supported);
3534
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003535 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003536}
3537
3538#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3539
3540static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
3542 int len = 0;
3543
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003544 if (!led_supported) {
3545 len += sprintf(p + len, "status:\t\tnot supported\n");
3546 return len;
3547 }
3548 len += sprintf(p + len, "status:\t\tsupported\n");
3549
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003550 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003551 /* 570 */
3552 int i, status;
3553 for (i = 0; i < 8; i++) {
3554 if (!acpi_evalf(ec_handle,
3555 &status, "GLED", "dd", 1 << i))
3556 return -EIO;
3557 len += sprintf(p + len, "%d:\t\t%s\n",
3558 i, led_status(status));
3559 }
3560 }
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003563 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
3565 return len;
3566}
3567
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003568/* off, on, blink */
3569static const int led_sled_arg1[] = { 0, 1, 3 };
3570static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3571static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3572static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3573
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003574static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
3576 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003577 int led, ind, ret;
3578
3579 if (!led_supported)
3580 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
3582 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003583 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return -EINVAL;
3585
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003586 if (strstr(cmd, "off")) {
3587 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003589 ind = 1;
3590 } else if (strstr(cmd, "blink")) {
3591 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 } else
3593 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003594
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003595 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003596 /* 570 */
3597 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003599 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003601 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003602 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3603 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003604 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003605 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003606 ret = ec_write(TPACPI_LED_EC_HLBL,
3607 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003608 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003609 ret = ec_write(TPACPI_LED_EC_HLCL,
3610 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003611 if (ret < 0)
3612 return ret;
3613 } else {
3614 /* all others */
3615 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3616 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
3620
3621 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003622}
3623
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003624static struct ibm_struct led_driver_data = {
3625 .name = "led",
3626 .read = led_read,
3627 .write = led_write,
3628};
3629
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003630/*************************************************************************
3631 * Beep subdriver
3632 */
3633
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003634TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003635
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003636static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003637{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003638 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3639
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003640 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003641
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003642 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3643 str_supported(beep_handle != NULL));
3644
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003645 return (beep_handle)? 0 : 1;
3646}
3647
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003648static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
3650 int len = 0;
3651
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003652 if (!beep_handle)
3653 len += sprintf(p + len, "status:\t\tnot supported\n");
3654 else {
3655 len += sprintf(p + len, "status:\t\tsupported\n");
3656 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
3659 return len;
3660}
3661
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003662static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663{
3664 char *cmd;
3665 int beep_cmd;
3666
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003667 if (!beep_handle)
3668 return -ENODEV;
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003671 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3672 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 /* beep_cmd set */
3674 } else
3675 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003676 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return -EIO;
3678 }
3679
3680 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003681}
3682
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003683static struct ibm_struct beep_driver_data = {
3684 .name = "beep",
3685 .read = beep_read,
3686 .write = beep_write,
3687};
3688
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003689/*************************************************************************
3690 * Thermal subdriver
3691 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003692
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003693enum thermal_access_mode {
3694 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3695 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3696 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3697 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3698 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3699};
3700
3701enum { /* TPACPI_THERMAL_TPEC_* */
3702 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3703 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3704 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3705};
3706
3707#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3708struct ibm_thermal_sensors_struct {
3709 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3710};
3711
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003712static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003713
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003714/* idx is zero-based */
3715static int thermal_get_sensor(int idx, s32 *value)
3716{
3717 int t;
3718 s8 tmp;
3719 char tmpi[5];
3720
3721 t = TP_EC_THERMAL_TMP0;
3722
3723 switch (thermal_read_mode) {
3724#if TPACPI_MAX_THERMAL_SENSORS >= 16
3725 case TPACPI_THERMAL_TPEC_16:
3726 if (idx >= 8 && idx <= 15) {
3727 t = TP_EC_THERMAL_TMP8;
3728 idx -= 8;
3729 }
3730 /* fallthrough */
3731#endif
3732 case TPACPI_THERMAL_TPEC_8:
3733 if (idx <= 7) {
3734 if (!acpi_ec_read(t + idx, &tmp))
3735 return -EIO;
3736 *value = tmp * 1000;
3737 return 0;
3738 }
3739 break;
3740
3741 case TPACPI_THERMAL_ACPI_UPDT:
3742 if (idx <= 7) {
3743 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3744 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3745 return -EIO;
3746 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3747 return -EIO;
3748 *value = (t - 2732) * 100;
3749 return 0;
3750 }
3751 break;
3752
3753 case TPACPI_THERMAL_ACPI_TMP07:
3754 if (idx <= 7) {
3755 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3756 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3757 return -EIO;
3758 if (t > 127 || t < -127)
3759 t = TP_EC_THERMAL_TMP_NA;
3760 *value = t * 1000;
3761 return 0;
3762 }
3763 break;
3764
3765 case TPACPI_THERMAL_NONE:
3766 default:
3767 return -ENOSYS;
3768 }
3769
3770 return -EINVAL;
3771}
3772
3773static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3774{
3775 int res, i;
3776 int n;
3777
3778 n = 8;
3779 i = 0;
3780
3781 if (!s)
3782 return -EINVAL;
3783
3784 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3785 n = 16;
3786
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003787 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003788 res = thermal_get_sensor(i, &s->temp[i]);
3789 if (res)
3790 return res;
3791 }
3792
3793 return n;
3794}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003795
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003796/* sysfs temp##_input -------------------------------------------------- */
3797
3798static ssize_t thermal_temp_input_show(struct device *dev,
3799 struct device_attribute *attr,
3800 char *buf)
3801{
3802 struct sensor_device_attribute *sensor_attr =
3803 to_sensor_dev_attr(attr);
3804 int idx = sensor_attr->index;
3805 s32 value;
3806 int res;
3807
3808 res = thermal_get_sensor(idx, &value);
3809 if (res)
3810 return res;
3811 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3812 return -ENXIO;
3813
3814 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3815}
3816
3817#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003818 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3819 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003820
3821static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3822 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3823 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3824 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3825 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3826 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3827 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3828 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3829 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3830 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3831 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3832 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3833 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3834 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3835 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3836 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3837 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3838};
3839
3840#define THERMAL_ATTRS(X) \
3841 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3842
3843static struct attribute *thermal_temp_input_attr[] = {
3844 THERMAL_ATTRS(8),
3845 THERMAL_ATTRS(9),
3846 THERMAL_ATTRS(10),
3847 THERMAL_ATTRS(11),
3848 THERMAL_ATTRS(12),
3849 THERMAL_ATTRS(13),
3850 THERMAL_ATTRS(14),
3851 THERMAL_ATTRS(15),
3852 THERMAL_ATTRS(0),
3853 THERMAL_ATTRS(1),
3854 THERMAL_ATTRS(2),
3855 THERMAL_ATTRS(3),
3856 THERMAL_ATTRS(4),
3857 THERMAL_ATTRS(5),
3858 THERMAL_ATTRS(6),
3859 THERMAL_ATTRS(7),
3860 NULL
3861};
3862
3863static const struct attribute_group thermal_temp_input16_group = {
3864 .attrs = thermal_temp_input_attr
3865};
3866
3867static const struct attribute_group thermal_temp_input8_group = {
3868 .attrs = &thermal_temp_input_attr[8]
3869};
3870
3871#undef THERMAL_SENSOR_ATTR_TEMP
3872#undef THERMAL_ATTRS
3873
3874/* --------------------------------------------------------------------- */
3875
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003876static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003877{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003878 u8 t, ta1, ta2;
3879 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003880 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003881 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003882
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003883 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3884
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003885 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003886
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003887 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003888 /*
3889 * Direct EC access mode: sensors at registers
3890 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3891 * non-implemented, thermal sensors return 0x80 when
3892 * not available
3893 */
3894
3895 ta1 = ta2 = 0;
3896 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003897 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003898 ta1 |= t;
3899 } else {
3900 ta1 = 0;
3901 break;
3902 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003903 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003904 ta2 |= t;
3905 } else {
3906 ta1 = 0;
3907 break;
3908 }
3909 }
3910 if (ta1 == 0) {
3911 /* This is sheer paranoia, but we handle it anyway */
3912 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003913 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003914 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003915 "falling back to ACPI TMPx access "
3916 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003917 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003918 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003919 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003920 "ThinkPad ACPI EC access misbehaving, "
3921 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003922 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003923 }
3924 } else {
3925 thermal_read_mode =
3926 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003927 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003928 }
3929 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003930 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3931 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003932 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003933 } else {
3934 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003935 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003936 }
3937 } else {
3938 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003939 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003940 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003941
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003942 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3943 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3944 thermal_read_mode);
3945
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003946 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003947 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003948 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003949 &thermal_temp_input16_group);
3950 if (res)
3951 return res;
3952 break;
3953 case TPACPI_THERMAL_TPEC_8:
3954 case TPACPI_THERMAL_ACPI_TMP07:
3955 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003956 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003957 &thermal_temp_input8_group);
3958 if (res)
3959 return res;
3960 break;
3961 case TPACPI_THERMAL_NONE:
3962 default:
3963 return 1;
3964 }
3965
3966 return 0;
3967}
3968
3969static void thermal_exit(void)
3970{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003971 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003972 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003973 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003974 &thermal_temp_input16_group);
3975 break;
3976 case TPACPI_THERMAL_TPEC_8:
3977 case TPACPI_THERMAL_ACPI_TMP07:
3978 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003979 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003980 &thermal_temp_input16_group);
3981 break;
3982 case TPACPI_THERMAL_NONE:
3983 default:
3984 break;
3985 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003986}
3987
3988static int thermal_read(char *p)
3989{
3990 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003991 int n, i;
3992 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003993
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003994 n = thermal_get_sensors(&t);
3995 if (unlikely(n < 0))
3996 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003997
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003998 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003999
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004000 if (n > 0) {
4001 for (i = 0; i < (n - 1); i++)
4002 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4003 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4004 } else
4005 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004006
4007 return len;
4008}
4009
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004010static struct ibm_struct thermal_driver_data = {
4011 .name = "thermal",
4012 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004013 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004014};
4015
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004016/*************************************************************************
4017 * EC Dump subdriver
4018 */
4019
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004020static u8 ecdump_regs[256];
4021
4022static int ecdump_read(char *p)
4023{
4024 int len = 0;
4025 int i, j;
4026 u8 v;
4027
4028 len += sprintf(p + len, "EC "
4029 " +00 +01 +02 +03 +04 +05 +06 +07"
4030 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4031 for (i = 0; i < 256; i += 16) {
4032 len += sprintf(p + len, "EC 0x%02x:", i);
4033 for (j = 0; j < 16; j++) {
4034 if (!acpi_ec_read(i + j, &v))
4035 break;
4036 if (v != ecdump_regs[i + j])
4037 len += sprintf(p + len, " *%02x", v);
4038 else
4039 len += sprintf(p + len, " %02x", v);
4040 ecdump_regs[i + j] = v;
4041 }
4042 len += sprintf(p + len, "\n");
4043 if (j != 16)
4044 break;
4045 }
4046
4047 /* These are way too dangerous to advertise openly... */
4048#if 0
4049 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4050 " (<offset> is 00-ff, <value> is 00-ff)\n");
4051 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4052 " (<offset> is 00-ff, <value> is 0-255)\n");
4053#endif
4054 return len;
4055}
4056
4057static int ecdump_write(char *buf)
4058{
4059 char *cmd;
4060 int i, v;
4061
4062 while ((cmd = next_cmd(&buf))) {
4063 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4064 /* i and v set */
4065 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4066 /* i and v set */
4067 } else
4068 return -EINVAL;
4069 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4070 if (!acpi_ec_write(i, v))
4071 return -EIO;
4072 } else
4073 return -EINVAL;
4074 }
4075
4076 return 0;
4077}
4078
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004079static struct ibm_struct ecdump_driver_data = {
4080 .name = "ecdump",
4081 .read = ecdump_read,
4082 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004083 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004084};
4085
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004086/*************************************************************************
4087 * Backlight/brightness subdriver
4088 */
Holger Macht8acb0252006-10-20 14:30:28 -07004089
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004090#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4091
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004092static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004093static int brightness_offset = 0x31;
4094static int brightness_mode;
4095static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4096
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004097static struct mutex brightness_mutex;
4098
4099/*
4100 * ThinkPads can read brightness from two places: EC 0x31, or
4101 * CMOS NVRAM byte 0x5E, bits 0-3.
4102 */
4103static int brightness_get(struct backlight_device *bd)
4104{
4105 u8 lec = 0, lcmos = 0, level = 0;
4106
4107 if (brightness_mode & 1) {
4108 if (!acpi_ec_read(brightness_offset, &lec))
4109 return -EIO;
4110 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4111 level = lec;
4112 };
4113 if (brightness_mode & 2) {
4114 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4115 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4116 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4117 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4118 level = lcmos;
4119 }
4120
4121 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004122 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004123 "CMOS NVRAM (%u) and EC (%u) do not agree "
4124 "on display brightness level\n",
4125 (unsigned int) lcmos,
4126 (unsigned int) lec);
4127 return -EIO;
4128 }
4129
4130 return level;
4131}
4132
4133/* May return EINTR which can always be mapped to ERESTARTSYS */
4134static int brightness_set(int value)
4135{
4136 int cmos_cmd, inc, i, res;
4137 int current_value;
4138
4139 if (value > ((tp_features.bright_16levels)? 15 : 7))
4140 return -EINVAL;
4141
4142 res = mutex_lock_interruptible(&brightness_mutex);
4143 if (res < 0)
4144 return res;
4145
4146 current_value = brightness_get(NULL);
4147 if (current_value < 0) {
4148 res = current_value;
4149 goto errout;
4150 }
4151
4152 cmos_cmd = value > current_value ?
4153 TP_CMOS_BRIGHTNESS_UP :
4154 TP_CMOS_BRIGHTNESS_DOWN;
4155 inc = (value > current_value)? 1 : -1;
4156
4157 res = 0;
4158 for (i = current_value; i != value; i += inc) {
4159 if ((brightness_mode & 2) &&
4160 issue_thinkpad_cmos_command(cmos_cmd)) {
4161 res = -EIO;
4162 goto errout;
4163 }
4164 if ((brightness_mode & 1) &&
4165 !acpi_ec_write(brightness_offset, i + inc)) {
4166 res = -EIO;
4167 goto errout;;
4168 }
4169 }
4170
4171errout:
4172 mutex_unlock(&brightness_mutex);
4173 return res;
4174}
4175
4176/* sysfs backlight class ----------------------------------------------- */
4177
4178static int brightness_update_status(struct backlight_device *bd)
4179{
4180 /* it is the backlight class's job (caller) to handle
4181 * EINTR and other errors properly */
4182 return brightness_set(
4183 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4184 bd->props.power == FB_BLANK_UNBLANK) ?
4185 bd->props.brightness : 0);
4186}
Holger Macht8acb0252006-10-20 14:30:28 -07004187
Richard Purdie599a52d2007-02-10 23:07:48 +00004188static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004189 .get_brightness = brightness_get,
4190 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004191};
4192
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004193/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004194
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004195static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4196{
4197 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4198 union acpi_object *obj;
4199 int rc;
4200
4201 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4202 obj = (union acpi_object *)buffer.pointer;
4203 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004204 printk(TPACPI_ERR "Unknown BCLL data, "
4205 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004206 rc = 0;
4207 } else {
4208 rc = obj->package.count;
4209 }
4210 } else {
4211 return 0;
4212 }
4213
4214 kfree(buffer.pointer);
4215 return rc;
4216}
4217
4218static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4219 void *context, void **rv)
4220{
4221 char name[ACPI_PATH_SEGMENT_LENGTH];
4222 struct acpi_buffer buffer = { sizeof(name), &name };
4223
4224 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4225 !strncmp("BCLL", name, sizeof(name) - 1)) {
4226 if (tpacpi_query_bcll_levels(handle) == 16) {
4227 *rv = handle;
4228 return AE_CTRL_TERMINATE;
4229 } else {
4230 return AE_OK;
4231 }
4232 } else {
4233 return AE_OK;
4234 }
4235}
4236
4237static int __init brightness_check_levels(void)
4238{
4239 int status;
4240 void *found_node = NULL;
4241
4242 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004243 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004244 }
4245 if (!vid_handle)
4246 return 0;
4247
4248 /* Search for a BCLL package with 16 levels */
4249 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004250 brightness_find_bcll, NULL,
4251 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004252
4253 return (ACPI_SUCCESS(status) && found_node != NULL);
4254}
4255
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004256static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4257 void *context, void **rv)
4258{
4259 char name[ACPI_PATH_SEGMENT_LENGTH];
4260 struct acpi_buffer buffer = { sizeof(name), &name };
4261
4262 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4263 !strncmp("_BCL", name, sizeof(name) - 1)) {
4264 *rv = handle;
4265 return AE_CTRL_TERMINATE;
4266 } else {
4267 return AE_OK;
4268 }
4269}
4270
4271static int __init brightness_check_std_acpi_support(void)
4272{
4273 int status;
4274 void *found_node = NULL;
4275
4276 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004277 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004278 }
4279 if (!vid_handle)
4280 return 0;
4281
4282 /* Search for a _BCL method, but don't execute it */
4283 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004284 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004285
4286 return (ACPI_SUCCESS(status) && found_node != NULL);
4287}
4288
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004289static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004290{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004291 int b;
4292
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004293 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4294
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004295 mutex_init(&brightness_mutex);
4296
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004297 if (!brightness_enable) {
4298 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004299 "brightness support disabled by "
4300 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004301 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004302 } else if (brightness_enable > 1) {
4303 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004304 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004305 "standard ACPI backlight interface "
4306 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004307 return 1;
4308 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004309 }
4310
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004311 if (!brightness_mode) {
4312 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4313 brightness_mode = 2;
4314 else
4315 brightness_mode = 3;
4316
4317 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4318 brightness_mode);
4319 }
4320
4321 if (brightness_mode > 3)
4322 return -EINVAL;
4323
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004324 tp_features.bright_16levels =
4325 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4326 brightness_check_levels();
4327
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004328 b = brightness_get(NULL);
4329 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004330 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004331
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004332 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004333 printk(TPACPI_INFO
4334 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004335
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004336 ibm_backlight_device = backlight_device_register(
4337 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4338 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004339 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004340 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004341 return PTR_ERR(ibm_backlight_device);
4342 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004343 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004344
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004345 ibm_backlight_device->props.max_brightness =
4346 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004347 ibm_backlight_device->props.brightness = b;
4348 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004349
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004350 return 0;
4351}
4352
4353static void brightness_exit(void)
4354{
4355 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004356 vdbg_printk(TPACPI_DBG_EXIT,
4357 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004358 backlight_device_unregister(ibm_backlight_device);
4359 ibm_backlight_device = NULL;
4360 }
4361}
4362
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004363static int brightness_read(char *p)
4364{
4365 int len = 0;
4366 int level;
4367
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004368 level = brightness_get(NULL);
4369 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004370 len += sprintf(p + len, "level:\t\tunreadable\n");
4371 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004372 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004373 len += sprintf(p + len, "commands:\tup, down\n");
4374 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004375 " (<level> is 0-%d)\n",
4376 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004377 }
4378
4379 return len;
4380}
4381
4382static int brightness_write(char *buf)
4383{
4384 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004385 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004386 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004387 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004388
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004389 level = brightness_get(NULL);
4390 if (level < 0)
4391 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004392
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004393 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004394 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004395 if (level < max_level)
4396 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004397 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004398 if (level > 0)
4399 level--;
4400 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4401 level >= 0 && level <= max_level) {
4402 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004403 } else
4404 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004405 }
4406
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004407 /*
4408 * Now we know what the final level should be, so we try to set it.
4409 * Doing it this way makes the syscall restartable in case of EINTR
4410 */
4411 rc = brightness_set(level);
4412 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004413}
4414
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004415static struct ibm_struct brightness_driver_data = {
4416 .name = "brightness",
4417 .read = brightness_read,
4418 .write = brightness_write,
4419 .exit = brightness_exit,
4420};
4421
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004422/*************************************************************************
4423 * Volume subdriver
4424 */
4425
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004426static int volume_offset = 0x30;
4427
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004428static int volume_read(char *p)
4429{
4430 int len = 0;
4431 u8 level;
4432
4433 if (!acpi_ec_read(volume_offset, &level)) {
4434 len += sprintf(p + len, "level:\t\tunreadable\n");
4435 } else {
4436 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4437 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4438 len += sprintf(p + len, "commands:\tup, down, mute\n");
4439 len += sprintf(p + len, "commands:\tlevel <level>"
4440 " (<level> is 0-15)\n");
4441 }
4442
4443 return len;
4444}
4445
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004446static int volume_write(char *buf)
4447{
4448 int cmos_cmd, inc, i;
4449 u8 level, mute;
4450 int new_level, new_mute;
4451 char *cmd;
4452
4453 while ((cmd = next_cmd(&buf))) {
4454 if (!acpi_ec_read(volume_offset, &level))
4455 return -EIO;
4456 new_mute = mute = level & 0x40;
4457 new_level = level = level & 0xf;
4458
4459 if (strlencmp(cmd, "up") == 0) {
4460 if (mute)
4461 new_mute = 0;
4462 else
4463 new_level = level == 15 ? 15 : level + 1;
4464 } else if (strlencmp(cmd, "down") == 0) {
4465 if (mute)
4466 new_mute = 0;
4467 else
4468 new_level = level == 0 ? 0 : level - 1;
4469 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4470 new_level >= 0 && new_level <= 15) {
4471 /* new_level set */
4472 } else if (strlencmp(cmd, "mute") == 0) {
4473 new_mute = 0x40;
4474 } else
4475 return -EINVAL;
4476
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004477 if (new_level != level) {
4478 /* mute doesn't change */
4479
4480 cmos_cmd = (new_level > level) ?
4481 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004482 inc = new_level > level ? 1 : -1;
4483
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004484 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004485 !acpi_ec_write(volume_offset, level)))
4486 return -EIO;
4487
4488 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004489 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004490 !acpi_ec_write(volume_offset, i + inc))
4491 return -EIO;
4492
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004493 if (mute &&
4494 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4495 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004496 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004497 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004498 }
4499
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004500 if (new_mute != mute) {
4501 /* level doesn't change */
4502
4503 cmos_cmd = (new_mute) ?
4504 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004505
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004506 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004507 !acpi_ec_write(volume_offset, level + new_mute))
4508 return -EIO;
4509 }
4510 }
4511
4512 return 0;
4513}
4514
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004515static struct ibm_struct volume_driver_data = {
4516 .name = "volume",
4517 .read = volume_read,
4518 .write = volume_write,
4519};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004520
4521/*************************************************************************
4522 * Fan subdriver
4523 */
4524
4525/*
4526 * FAN ACCESS MODES
4527 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004528 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004529 * ACPI GFAN method: returns fan level
4530 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004531 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004532 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004533 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004534 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004535 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4536 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004537 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4538 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004539 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004540 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004541 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4542 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004543 *
4544 * Fan speed changes of any sort (including those caused by the
4545 * disengaged mode) are usually done slowly by the firmware as the
4546 * maximum ammount of fan duty cycle change per second seems to be
4547 * limited.
4548 *
4549 * Reading is not available if GFAN exists.
4550 * Writing is not available if SFAN exists.
4551 *
4552 * Bits
4553 * 7 automatic mode engaged;
4554 * (default operation mode of the ThinkPad)
4555 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004556 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004557 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004558 * the tachometer while the fan controller ramps up
4559 * the speed (which can take up to a few *minutes*).
4560 * Speeds up fan to 100% duty-cycle, which is far above
4561 * the standard RPM levels. It is not impossible that
4562 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004563 * 5-3 unused in some models. Extra bits for fan level
4564 * in others, but still useless as all values above
4565 * 7 map to the same speed as level 7 in these models.
4566 * 2-0 fan level (0..7 usually)
4567 * 0x00 = stop
4568 * 0x07 = max (set when temperatures critical)
4569 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004570 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004571 *
4572 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4573 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4574 * does so, its initial value is meaningless (0x07).
4575 *
4576 * For firmware bugs, refer to:
4577 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4578 *
4579 * ----
4580 *
4581 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4582 * Main fan tachometer reading (in RPM)
4583 *
4584 * This register is present on all ThinkPads with a new-style EC, and
4585 * it is known not to be present on the A21m/e, and T22, as there is
4586 * something else in offset 0x84 according to the ACPI DSDT. Other
4587 * ThinkPads from this same time period (and earlier) probably lack the
4588 * tachometer as well.
4589 *
4590 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4591 * was never fixed by IBM to report the EC firmware version string
4592 * probably support the tachometer (like the early X models), so
4593 * detecting it is quite hard. We need more data to know for sure.
4594 *
4595 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4596 * might result.
4597 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004598 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4599 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004600 *
4601 * For firmware bugs, refer to:
4602 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4603 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004604 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004605 * ThinkPad X31, X40, X41. Not available in the X60.
4606 *
4607 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4608 * high speed. ACPI DSDT seems to map these three speeds to levels
4609 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4610 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4611 *
4612 * The speeds are stored on handles
4613 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4614 *
4615 * There are three default speed sets, acessible as handles:
4616 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4617 *
4618 * ACPI DSDT switches which set is in use depending on various
4619 * factors.
4620 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004621 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004622 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4623 * but the ACPI tables just mention level 7.
4624 */
4625
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004626enum { /* Fan control constants */
4627 fan_status_offset = 0x2f, /* EC register 0x2f */
4628 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4629 * 0x84 must be read before 0x85 */
4630
4631 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4632 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4633
4634 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4635};
4636
4637enum fan_status_access_mode {
4638 TPACPI_FAN_NONE = 0, /* No fan status or control */
4639 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4640 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4641};
4642
4643enum fan_control_access_mode {
4644 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4645 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4646 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4647 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4648};
4649
4650enum fan_control_commands {
4651 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4652 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4653 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4654 * and also watchdog cmd */
4655};
4656
4657static int fan_control_allowed;
4658
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004659static enum fan_status_access_mode fan_status_access_mode;
4660static enum fan_control_access_mode fan_control_access_mode;
4661static enum fan_control_commands fan_control_commands;
4662
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004663static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004664static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004665static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004666
4667static struct mutex fan_mutex;
4668
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004669static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004670static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004671
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004672TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4673TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004674 "\\FSPD", /* 600e/x, 770e, 770x */
4675 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004676TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004677 "JFNS", /* 770x-JL */
4678 ); /* all others */
4679
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004680/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004681 * Call with fan_mutex held
4682 */
4683static void fan_update_desired_level(u8 status)
4684{
4685 if ((status &
4686 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4687 if (status > 7)
4688 fan_control_desired_level = 7;
4689 else
4690 fan_control_desired_level = status;
4691 }
4692}
4693
4694static int fan_get_status(u8 *status)
4695{
4696 u8 s;
4697
4698 /* TODO:
4699 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4700
4701 switch (fan_status_access_mode) {
4702 case TPACPI_FAN_RD_ACPI_GFAN:
4703 /* 570, 600e/x, 770e, 770x */
4704
4705 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4706 return -EIO;
4707
4708 if (likely(status))
4709 *status = s & 0x07;
4710
4711 break;
4712
4713 case TPACPI_FAN_RD_TPEC:
4714 /* all except 570, 600e/x, 770e, 770x */
4715 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4716 return -EIO;
4717
4718 if (likely(status))
4719 *status = s;
4720
4721 break;
4722
4723 default:
4724 return -ENXIO;
4725 }
4726
4727 return 0;
4728}
4729
4730static int fan_get_status_safe(u8 *status)
4731{
4732 int rc;
4733 u8 s;
4734
4735 if (mutex_lock_interruptible(&fan_mutex))
4736 return -ERESTARTSYS;
4737 rc = fan_get_status(&s);
4738 if (!rc)
4739 fan_update_desired_level(s);
4740 mutex_unlock(&fan_mutex);
4741
4742 if (status)
4743 *status = s;
4744
4745 return rc;
4746}
4747
4748static int fan_get_speed(unsigned int *speed)
4749{
4750 u8 hi, lo;
4751
4752 switch (fan_status_access_mode) {
4753 case TPACPI_FAN_RD_TPEC:
4754 /* all except 570, 600e/x, 770e, 770x */
4755 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4756 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4757 return -EIO;
4758
4759 if (likely(speed))
4760 *speed = (hi << 8) | lo;
4761
4762 break;
4763
4764 default:
4765 return -ENXIO;
4766 }
4767
4768 return 0;
4769}
4770
4771static int fan_set_level(int level)
4772{
4773 if (!fan_control_allowed)
4774 return -EPERM;
4775
4776 switch (fan_control_access_mode) {
4777 case TPACPI_FAN_WR_ACPI_SFAN:
4778 if (level >= 0 && level <= 7) {
4779 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4780 return -EIO;
4781 } else
4782 return -EINVAL;
4783 break;
4784
4785 case TPACPI_FAN_WR_ACPI_FANS:
4786 case TPACPI_FAN_WR_TPEC:
4787 if ((level != TP_EC_FAN_AUTO) &&
4788 (level != TP_EC_FAN_FULLSPEED) &&
4789 ((level < 0) || (level > 7)))
4790 return -EINVAL;
4791
4792 /* safety net should the EC not support AUTO
4793 * or FULLSPEED mode bits and just ignore them */
4794 if (level & TP_EC_FAN_FULLSPEED)
4795 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01004796 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004797 level |= 4; /* safety min speed 4 */
4798
4799 if (!acpi_ec_write(fan_status_offset, level))
4800 return -EIO;
4801 else
4802 tp_features.fan_ctrl_status_undef = 0;
4803 break;
4804
4805 default:
4806 return -ENXIO;
4807 }
4808 return 0;
4809}
4810
4811static int fan_set_level_safe(int level)
4812{
4813 int rc;
4814
4815 if (!fan_control_allowed)
4816 return -EPERM;
4817
4818 if (mutex_lock_interruptible(&fan_mutex))
4819 return -ERESTARTSYS;
4820
4821 if (level == TPACPI_FAN_LAST_LEVEL)
4822 level = fan_control_desired_level;
4823
4824 rc = fan_set_level(level);
4825 if (!rc)
4826 fan_update_desired_level(level);
4827
4828 mutex_unlock(&fan_mutex);
4829 return rc;
4830}
4831
4832static int fan_set_enable(void)
4833{
4834 u8 s;
4835 int rc;
4836
4837 if (!fan_control_allowed)
4838 return -EPERM;
4839
4840 if (mutex_lock_interruptible(&fan_mutex))
4841 return -ERESTARTSYS;
4842
4843 switch (fan_control_access_mode) {
4844 case TPACPI_FAN_WR_ACPI_FANS:
4845 case TPACPI_FAN_WR_TPEC:
4846 rc = fan_get_status(&s);
4847 if (rc < 0)
4848 break;
4849
4850 /* Don't go out of emergency fan mode */
4851 if (s != 7) {
4852 s &= 0x07;
4853 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4854 }
4855
4856 if (!acpi_ec_write(fan_status_offset, s))
4857 rc = -EIO;
4858 else {
4859 tp_features.fan_ctrl_status_undef = 0;
4860 rc = 0;
4861 }
4862 break;
4863
4864 case TPACPI_FAN_WR_ACPI_SFAN:
4865 rc = fan_get_status(&s);
4866 if (rc < 0)
4867 break;
4868
4869 s &= 0x07;
4870
4871 /* Set fan to at least level 4 */
4872 s |= 4;
4873
4874 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004875 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004876 else
4877 rc = 0;
4878 break;
4879
4880 default:
4881 rc = -ENXIO;
4882 }
4883
4884 mutex_unlock(&fan_mutex);
4885 return rc;
4886}
4887
4888static int fan_set_disable(void)
4889{
4890 int rc;
4891
4892 if (!fan_control_allowed)
4893 return -EPERM;
4894
4895 if (mutex_lock_interruptible(&fan_mutex))
4896 return -ERESTARTSYS;
4897
4898 rc = 0;
4899 switch (fan_control_access_mode) {
4900 case TPACPI_FAN_WR_ACPI_FANS:
4901 case TPACPI_FAN_WR_TPEC:
4902 if (!acpi_ec_write(fan_status_offset, 0x00))
4903 rc = -EIO;
4904 else {
4905 fan_control_desired_level = 0;
4906 tp_features.fan_ctrl_status_undef = 0;
4907 }
4908 break;
4909
4910 case TPACPI_FAN_WR_ACPI_SFAN:
4911 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4912 rc = -EIO;
4913 else
4914 fan_control_desired_level = 0;
4915 break;
4916
4917 default:
4918 rc = -ENXIO;
4919 }
4920
4921
4922 mutex_unlock(&fan_mutex);
4923 return rc;
4924}
4925
4926static int fan_set_speed(int speed)
4927{
4928 int rc;
4929
4930 if (!fan_control_allowed)
4931 return -EPERM;
4932
4933 if (mutex_lock_interruptible(&fan_mutex))
4934 return -ERESTARTSYS;
4935
4936 rc = 0;
4937 switch (fan_control_access_mode) {
4938 case TPACPI_FAN_WR_ACPI_FANS:
4939 if (speed >= 0 && speed <= 65535) {
4940 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4941 speed, speed, speed))
4942 rc = -EIO;
4943 } else
4944 rc = -EINVAL;
4945 break;
4946
4947 default:
4948 rc = -ENXIO;
4949 }
4950
4951 mutex_unlock(&fan_mutex);
4952 return rc;
4953}
4954
4955static void fan_watchdog_reset(void)
4956{
4957 static int fan_watchdog_active;
4958
4959 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4960 return;
4961
4962 if (fan_watchdog_active)
4963 cancel_delayed_work(&fan_watchdog_task);
4964
4965 if (fan_watchdog_maxinterval > 0 &&
4966 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4967 fan_watchdog_active = 1;
4968 if (!schedule_delayed_work(&fan_watchdog_task,
4969 msecs_to_jiffies(fan_watchdog_maxinterval
4970 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004971 printk(TPACPI_ERR
4972 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004973 "watchdog will not trigger\n");
4974 }
4975 } else
4976 fan_watchdog_active = 0;
4977}
4978
4979static void fan_watchdog_fire(struct work_struct *ignored)
4980{
4981 int rc;
4982
4983 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4984 return;
4985
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004986 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004987 rc = fan_set_enable();
4988 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004989 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004990 "will try again later...\n", -rc);
4991 /* reschedule for later */
4992 fan_watchdog_reset();
4993 }
4994}
4995
4996/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004997 * SYSFS fan layout: hwmon compatible (device)
4998 *
4999 * pwm*_enable:
5000 * 0: "disengaged" mode
5001 * 1: manual mode
5002 * 2: native EC "auto" mode (recommended, hardware default)
5003 *
5004 * pwm*: set speed in manual mode, ignored otherwise.
5005 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5006 * interpolation.
5007 *
5008 * fan*_input: tachometer reading, RPM
5009 *
5010 *
5011 * SYSFS fan layout: extensions
5012 *
5013 * fan_watchdog (driver):
5014 * fan watchdog interval in seconds, 0 disables (default), max 120
5015 */
5016
5017/* sysfs fan pwm1_enable ----------------------------------------------- */
5018static ssize_t fan_pwm1_enable_show(struct device *dev,
5019 struct device_attribute *attr,
5020 char *buf)
5021{
5022 int res, mode;
5023 u8 status;
5024
5025 res = fan_get_status_safe(&status);
5026 if (res)
5027 return res;
5028
5029 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5030 if (status != fan_control_initial_status) {
5031 tp_features.fan_ctrl_status_undef = 0;
5032 } else {
5033 /* Return most likely status. In fact, it
5034 * might be the only possible status */
5035 status = TP_EC_FAN_AUTO;
5036 }
5037 }
5038
5039 if (status & TP_EC_FAN_FULLSPEED) {
5040 mode = 0;
5041 } else if (status & TP_EC_FAN_AUTO) {
5042 mode = 2;
5043 } else
5044 mode = 1;
5045
5046 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5047}
5048
5049static ssize_t fan_pwm1_enable_store(struct device *dev,
5050 struct device_attribute *attr,
5051 const char *buf, size_t count)
5052{
5053 unsigned long t;
5054 int res, level;
5055
5056 if (parse_strtoul(buf, 2, &t))
5057 return -EINVAL;
5058
5059 switch (t) {
5060 case 0:
5061 level = TP_EC_FAN_FULLSPEED;
5062 break;
5063 case 1:
5064 level = TPACPI_FAN_LAST_LEVEL;
5065 break;
5066 case 2:
5067 level = TP_EC_FAN_AUTO;
5068 break;
5069 case 3:
5070 /* reserved for software-controlled auto mode */
5071 return -ENOSYS;
5072 default:
5073 return -EINVAL;
5074 }
5075
5076 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005077 if (res == -ENXIO)
5078 return -EINVAL;
5079 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005080 return res;
5081
5082 fan_watchdog_reset();
5083
5084 return count;
5085}
5086
5087static struct device_attribute dev_attr_fan_pwm1_enable =
5088 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5089 fan_pwm1_enable_show, fan_pwm1_enable_store);
5090
5091/* sysfs fan pwm1 ------------------------------------------------------ */
5092static ssize_t fan_pwm1_show(struct device *dev,
5093 struct device_attribute *attr,
5094 char *buf)
5095{
5096 int res;
5097 u8 status;
5098
5099 res = fan_get_status_safe(&status);
5100 if (res)
5101 return res;
5102
5103 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5104 if (status != fan_control_initial_status) {
5105 tp_features.fan_ctrl_status_undef = 0;
5106 } else {
5107 status = TP_EC_FAN_AUTO;
5108 }
5109 }
5110
5111 if ((status &
5112 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5113 status = fan_control_desired_level;
5114
5115 if (status > 7)
5116 status = 7;
5117
5118 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5119}
5120
5121static ssize_t fan_pwm1_store(struct device *dev,
5122 struct device_attribute *attr,
5123 const char *buf, size_t count)
5124{
5125 unsigned long s;
5126 int rc;
5127 u8 status, newlevel;
5128
5129 if (parse_strtoul(buf, 255, &s))
5130 return -EINVAL;
5131
5132 /* scale down from 0-255 to 0-7 */
5133 newlevel = (s >> 5) & 0x07;
5134
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005135 if (mutex_lock_interruptible(&fan_mutex))
5136 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005137
5138 rc = fan_get_status(&status);
5139 if (!rc && (status &
5140 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5141 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005142 if (rc == -ENXIO)
5143 rc = -EINVAL;
5144 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005145 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005146 fan_watchdog_reset();
5147 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005148 }
5149
5150 mutex_unlock(&fan_mutex);
5151 return (rc)? rc : count;
5152}
5153
5154static struct device_attribute dev_attr_fan_pwm1 =
5155 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5156 fan_pwm1_show, fan_pwm1_store);
5157
5158/* sysfs fan fan1_input ------------------------------------------------ */
5159static ssize_t fan_fan1_input_show(struct device *dev,
5160 struct device_attribute *attr,
5161 char *buf)
5162{
5163 int res;
5164 unsigned int speed;
5165
5166 res = fan_get_speed(&speed);
5167 if (res < 0)
5168 return res;
5169
5170 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5171}
5172
5173static struct device_attribute dev_attr_fan_fan1_input =
5174 __ATTR(fan1_input, S_IRUGO,
5175 fan_fan1_input_show, NULL);
5176
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005177/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005178static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5179 char *buf)
5180{
5181 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5182}
5183
5184static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5185 const char *buf, size_t count)
5186{
5187 unsigned long t;
5188
5189 if (parse_strtoul(buf, 120, &t))
5190 return -EINVAL;
5191
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005192 if (!fan_control_allowed)
5193 return -EPERM;
5194
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005195 fan_watchdog_maxinterval = t;
5196 fan_watchdog_reset();
5197
5198 return count;
5199}
5200
5201static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5202 fan_fan_watchdog_show, fan_fan_watchdog_store);
5203
5204/* --------------------------------------------------------------------- */
5205static struct attribute *fan_attributes[] = {
5206 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5207 &dev_attr_fan_fan1_input.attr,
5208 NULL
5209};
5210
5211static const struct attribute_group fan_attr_group = {
5212 .attrs = fan_attributes,
5213};
5214
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005215static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005216{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005217 int rc;
5218
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005219 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5220
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005221 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005222 fan_status_access_mode = TPACPI_FAN_NONE;
5223 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005224 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005225 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005226 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005227 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005228
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005229 TPACPI_ACPIHANDLE_INIT(fans);
5230 TPACPI_ACPIHANDLE_INIT(gfan);
5231 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005232
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005233 if (gfan_handle) {
5234 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005235 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005236 } else {
5237 /* all other ThinkPads: note that even old-style
5238 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005239 if (likely(acpi_ec_read(fan_status_offset,
5240 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005241 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005242
5243 /* In some ThinkPads, neither the EC nor the ACPI
5244 * DSDT initialize the fan status, and it ends up
5245 * being set to 0x07 when it *could* be either
5246 * 0x07 or 0x80.
5247 *
5248 * Enable for TP-1Y (T43), TP-78 (R51e),
5249 * TP-76 (R52), TP-70 (T43, R52), which are known
5250 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005251 if (fan_control_initial_status == 0x07) {
5252 switch (thinkpad_id.ec_model) {
5253 case 0x5931: /* TP-1Y */
5254 case 0x3837: /* TP-78 */
5255 case 0x3637: /* TP-76 */
5256 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005257 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005258 "fan_init: initial fan status "
5259 "is unknown, assuming it is "
5260 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005261 tp_features.fan_ctrl_status_undef = 1;
5262 ;;
5263 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005264 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005265 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005266 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005267 "ThinkPad ACPI EC access misbehaving, "
5268 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005269 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005270 }
5271 }
5272
5273 if (sfan_handle) {
5274 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005275 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005276 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005277 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005278 } else {
5279 if (!gfan_handle) {
5280 /* gfan without sfan means no fan control */
5281 /* all other models implement TP EC 0x2f control */
5282
5283 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005284 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005285 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005286 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005287 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005288 TPACPI_FAN_CMD_SPEED |
5289 TPACPI_FAN_CMD_LEVEL |
5290 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005291 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005292 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005293 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005294 TPACPI_FAN_CMD_LEVEL |
5295 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005296 }
5297 }
5298 }
5299
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005300 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5301 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5302 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5303 fan_status_access_mode, fan_control_access_mode);
5304
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005305 /* fan control master switch */
5306 if (!fan_control_allowed) {
5307 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5308 fan_control_commands = 0;
5309 dbg_printk(TPACPI_DBG_INIT,
5310 "fan control features disabled by parameter\n");
5311 }
5312
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005313 /* update fan_control_desired_level */
5314 if (fan_status_access_mode != TPACPI_FAN_NONE)
5315 fan_get_status_safe(NULL);
5316
5317 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5318 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005319 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005320 &fan_attr_group);
5321 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005322 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005323 &driver_attr_fan_watchdog);
5324 if (rc < 0)
5325 return rc;
5326 return 0;
5327 } else
5328 return 1;
5329}
5330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005331static void fan_exit(void)
5332{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005333 vdbg_printk(TPACPI_DBG_EXIT,
5334 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005335
5336 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005337 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005338 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5339 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005340
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005341 cancel_delayed_work(&fan_watchdog_task);
5342 flush_scheduled_work();
5343}
5344
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005345static int fan_read(char *p)
5346{
5347 int len = 0;
5348 int rc;
5349 u8 status;
5350 unsigned int speed = 0;
5351
5352 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005353 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005354 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005355 rc = fan_get_status_safe(&status);
5356 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005357 return rc;
5358
5359 len += sprintf(p + len, "status:\t\t%s\n"
5360 "level:\t\t%d\n",
5361 (status != 0) ? "enabled" : "disabled", status);
5362 break;
5363
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005364 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005365 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005366 rc = fan_get_status_safe(&status);
5367 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005368 return rc;
5369
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005370 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005371 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005372 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005373 else
5374 /* Return most likely status. In fact, it
5375 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005376 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005377 }
5378
5379 len += sprintf(p + len, "status:\t\t%s\n",
5380 (status != 0) ? "enabled" : "disabled");
5381
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005382 rc = fan_get_speed(&speed);
5383 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005384 return rc;
5385
5386 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5387
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005388 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005389 /* Disengaged mode takes precedence */
5390 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005391 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005392 len += sprintf(p + len, "level:\t\tauto\n");
5393 else
5394 len += sprintf(p + len, "level:\t\t%d\n", status);
5395 break;
5396
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005397 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005398 default:
5399 len += sprintf(p + len, "status:\t\tnot supported\n");
5400 }
5401
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005402 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005403 len += sprintf(p + len, "commands:\tlevel <level>");
5404
5405 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005406 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005407 len += sprintf(p + len, " (<level> is 0-7)\n");
5408 break;
5409
5410 default:
5411 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005412 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005413 break;
5414 }
5415 }
5416
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005417 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005418 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005419 "commands:\twatchdog <timeout> (<timeout> "
5420 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005421
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005422 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005423 len += sprintf(p + len, "commands:\tspeed <speed>"
5424 " (<speed> is 0-65535)\n");
5425
5426 return len;
5427}
5428
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005429static int fan_write_cmd_level(const char *cmd, int *rc)
5430{
5431 int level;
5432
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005433 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005434 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005435 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005436 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005437 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005438 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005439 return 0;
5440
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005441 *rc = fan_set_level_safe(level);
5442 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005443 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005444 "access mode %d", fan_control_access_mode);
5445
5446 return 1;
5447}
5448
5449static int fan_write_cmd_enable(const char *cmd, int *rc)
5450{
5451 if (strlencmp(cmd, "enable") != 0)
5452 return 0;
5453
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005454 *rc = fan_set_enable();
5455 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005456 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005457 "access mode %d", fan_control_access_mode);
5458
5459 return 1;
5460}
5461
5462static int fan_write_cmd_disable(const char *cmd, int *rc)
5463{
5464 if (strlencmp(cmd, "disable") != 0)
5465 return 0;
5466
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005467 *rc = fan_set_disable();
5468 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005469 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005470 "access mode %d", fan_control_access_mode);
5471
5472 return 1;
5473}
5474
5475static int fan_write_cmd_speed(const char *cmd, int *rc)
5476{
5477 int speed;
5478
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005479 /* TODO:
5480 * Support speed <low> <medium> <high> ? */
5481
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005482 if (sscanf(cmd, "speed %d", &speed) != 1)
5483 return 0;
5484
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005485 *rc = fan_set_speed(speed);
5486 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005487 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005488 "access mode %d", fan_control_access_mode);
5489
5490 return 1;
5491}
5492
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005493static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5494{
5495 int interval;
5496
5497 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5498 return 0;
5499
5500 if (interval < 0 || interval > 120)
5501 *rc = -EINVAL;
5502 else
5503 fan_watchdog_maxinterval = interval;
5504
5505 return 1;
5506}
5507
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005508static int fan_write(char *buf)
5509{
5510 char *cmd;
5511 int rc = 0;
5512
5513 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005514 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005515 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005516 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005517 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005518 fan_write_cmd_disable(cmd, &rc) ||
5519 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005520 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005521 fan_write_cmd_speed(cmd, &rc))
5522 )
5523 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005524 else if (!rc)
5525 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005526 }
5527
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005528 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005529}
5530
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005531static struct ibm_struct fan_driver_data = {
5532 .name = "fan",
5533 .read = fan_read,
5534 .write = fan_write,
5535 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005536};
5537
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005538/****************************************************************************
5539 ****************************************************************************
5540 *
5541 * Infrastructure
5542 *
5543 ****************************************************************************
5544 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005546/* sysfs name ---------------------------------------------------------- */
5547static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5548 struct device_attribute *attr,
5549 char *buf)
5550{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005551 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005552}
5553
5554static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5555 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5556
5557/* --------------------------------------------------------------------- */
5558
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005559/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005560static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005561
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005562/*
5563 * Module and infrastructure proble, init and exit handling
5564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005566static int force_load;
5567
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005568#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005569static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005570{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005571 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005572
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005573 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005574}
5575#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5576
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005577static void ibm_exit(struct ibm_struct *ibm)
5578{
5579 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5580
5581 list_del_init(&ibm->all_drivers);
5582
5583 if (ibm->flags.acpi_notify_installed) {
5584 dbg_printk(TPACPI_DBG_EXIT,
5585 "%s: acpi_remove_notify_handler\n", ibm->name);
5586 BUG_ON(!ibm->acpi);
5587 acpi_remove_notify_handler(*ibm->acpi->handle,
5588 ibm->acpi->type,
5589 dispatch_acpi_notify);
5590 ibm->flags.acpi_notify_installed = 0;
5591 ibm->flags.acpi_notify_installed = 0;
5592 }
5593
5594 if (ibm->flags.proc_created) {
5595 dbg_printk(TPACPI_DBG_EXIT,
5596 "%s: remove_proc_entry\n", ibm->name);
5597 remove_proc_entry(ibm->name, proc_dir);
5598 ibm->flags.proc_created = 0;
5599 }
5600
5601 if (ibm->flags.acpi_driver_registered) {
5602 dbg_printk(TPACPI_DBG_EXIT,
5603 "%s: acpi_bus_unregister_driver\n", ibm->name);
5604 BUG_ON(!ibm->acpi);
5605 acpi_bus_unregister_driver(ibm->acpi->driver);
5606 kfree(ibm->acpi->driver);
5607 ibm->acpi->driver = NULL;
5608 ibm->flags.acpi_driver_registered = 0;
5609 }
5610
5611 if (ibm->flags.init_called && ibm->exit) {
5612 ibm->exit();
5613 ibm->flags.init_called = 0;
5614 }
5615
5616 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5617}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005618
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005619static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620{
5621 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005622 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 struct proc_dir_entry *entry;
5624
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005625 BUG_ON(ibm == NULL);
5626
5627 INIT_LIST_HEAD(&ibm->all_drivers);
5628
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005629 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 return 0;
5631
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005632 dbg_printk(TPACPI_DBG_INIT,
5633 "probing for %s\n", ibm->name);
5634
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005635 if (iibm->init) {
5636 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005637 if (ret > 0)
5638 return 0; /* probe failed */
5639 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005641
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005642 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 }
5644
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005645 if (ibm->acpi) {
5646 if (ibm->acpi->hid) {
5647 ret = register_tpacpi_subdriver(ibm);
5648 if (ret)
5649 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005650 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005651
5652 if (ibm->acpi->notify) {
5653 ret = setup_acpi_notify(ibm);
5654 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005655 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005656 ibm->name);
5657 ret = 0;
5658 goto err_out;
5659 }
5660 if (ret < 0)
5661 goto err_out;
5662 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005663 }
5664
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005665 dbg_printk(TPACPI_DBG_INIT,
5666 "%s installed\n", ibm->name);
5667
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005668 if (ibm->read) {
5669 entry = create_proc_entry(ibm->name,
5670 S_IFREG | S_IRUGO | S_IWUSR,
5671 proc_dir);
5672 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005673 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005674 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005675 ret = -ENODEV;
5676 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005677 }
5678 entry->owner = THIS_MODULE;
5679 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005680 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005681 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005682 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005683 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005686 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005689
5690err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005691 dbg_printk(TPACPI_DBG_INIT,
5692 "%s: at error exit path with result %d\n",
5693 ibm->name, ret);
5694
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005695 ibm_exit(ibm);
5696 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697}
5698
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005699/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005701static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005702{
Jeff Garzik18552562007-10-03 15:15:40 -04005703 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005704 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005705
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005706 if (!tp)
5707 return;
5708
5709 memset(tp, 0, sizeof(*tp));
5710
5711 if (dmi_name_in_vendors("IBM"))
5712 tp->vendor = PCI_VENDOR_ID_IBM;
5713 else if (dmi_name_in_vendors("LENOVO"))
5714 tp->vendor = PCI_VENDOR_ID_LENOVO;
5715 else
5716 return;
5717
5718 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5719 GFP_KERNEL);
5720 if (!tp->bios_version_str)
5721 return;
5722 tp->bios_model = tp->bios_version_str[0]
5723 | (tp->bios_version_str[1] << 8);
5724
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005725 /*
5726 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5727 * X32 or newer, all Z series; Some models must have an
5728 * up-to-date BIOS or they will not be detected.
5729 *
5730 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5731 */
5732 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005733 if (sscanf(dev->name,
5734 "IBM ThinkPad Embedded Controller -[%17c",
5735 ec_fw_string) == 1) {
5736 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5737 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005738
5739 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5740 tp->ec_model = ec_fw_string[0]
5741 | (ec_fw_string[1] << 8);
5742 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005743 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005744 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005745
5746 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5747 GFP_KERNEL);
5748 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5749 kfree(tp->model_str);
5750 tp->model_str = NULL;
5751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752}
5753
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005754static int __init probe_for_thinkpad(void)
5755{
5756 int is_thinkpad;
5757
5758 if (acpi_disabled)
5759 return -ENODEV;
5760
5761 /*
5762 * Non-ancient models have better DMI tagging, but very old models
5763 * don't.
5764 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005765 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005766
5767 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005768 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005769 if (!ec_handle) {
5770 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005771 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005772 "Not yet supported ThinkPad detected!\n");
5773 return -ENODEV;
5774 }
5775
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005776 /*
5777 * Risks a regression on very old machines, but reduces potential
5778 * false positives a damn great deal
5779 */
5780 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005781 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005782
5783 if (!is_thinkpad && !force_load)
5784 return -ENODEV;
5785
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005786 return 0;
5787}
5788
5789
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005790/* Module init, exit, parameters */
5791
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005792static struct ibm_init_struct ibms_init[] __initdata = {
5793 {
5794 .init = thinkpad_acpi_driver_init,
5795 .data = &thinkpad_acpi_driver_data,
5796 },
5797 {
5798 .init = hotkey_init,
5799 .data = &hotkey_driver_data,
5800 },
5801 {
5802 .init = bluetooth_init,
5803 .data = &bluetooth_driver_data,
5804 },
5805 {
5806 .init = wan_init,
5807 .data = &wan_driver_data,
5808 },
5809 {
5810 .init = video_init,
5811 .data = &video_driver_data,
5812 },
5813 {
5814 .init = light_init,
5815 .data = &light_driver_data,
5816 },
5817#ifdef CONFIG_THINKPAD_ACPI_DOCK
5818 {
5819 .init = dock_init,
5820 .data = &dock_driver_data[0],
5821 },
5822 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005823 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005824 .data = &dock_driver_data[1],
5825 },
5826#endif
5827#ifdef CONFIG_THINKPAD_ACPI_BAY
5828 {
5829 .init = bay_init,
5830 .data = &bay_driver_data,
5831 },
5832#endif
5833 {
5834 .init = cmos_init,
5835 .data = &cmos_driver_data,
5836 },
5837 {
5838 .init = led_init,
5839 .data = &led_driver_data,
5840 },
5841 {
5842 .init = beep_init,
5843 .data = &beep_driver_data,
5844 },
5845 {
5846 .init = thermal_init,
5847 .data = &thermal_driver_data,
5848 },
5849 {
5850 .data = &ecdump_driver_data,
5851 },
5852 {
5853 .init = brightness_init,
5854 .data = &brightness_driver_data,
5855 },
5856 {
5857 .data = &volume_driver_data,
5858 },
5859 {
5860 .init = fan_init,
5861 .data = &fan_driver_data,
5862 },
5863};
5864
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005865static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5866{
5867 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005868 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005869
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005870 if (!kp || !kp->name || !val)
5871 return -EINVAL;
5872
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005873 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5874 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005875 WARN_ON(ibm == NULL);
5876
5877 if (!ibm || !ibm->name)
5878 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005879
5880 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5881 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005882 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005883 strcpy(ibms_init[i].param, val);
5884 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005885 return 0;
5886 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005887 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005888
5889 return -EINVAL;
5890}
5891
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005892module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005893MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005894 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005895
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005896module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005897MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005898
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005899module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005900MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005901 "Attempts to load the driver even on a "
5902 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005903
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005904module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005905MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005906 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005907
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005908module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005909MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005910 "Selects brightness control strategy: "
5911 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005912
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005913module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005914MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005915 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005916
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005917module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005918MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005919 "used for backwards compatibility with userspace, "
5920 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005921
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005922#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005923 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02005924 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005925 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005926
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005927TPACPI_PARAM(hotkey);
5928TPACPI_PARAM(bluetooth);
5929TPACPI_PARAM(video);
5930TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005931#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005932TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005933#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005934#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005935TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005936#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005937TPACPI_PARAM(cmos);
5938TPACPI_PARAM(led);
5939TPACPI_PARAM(beep);
5940TPACPI_PARAM(ecdump);
5941TPACPI_PARAM(brightness);
5942TPACPI_PARAM(volume);
5943TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005944
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005945static void thinkpad_acpi_module_exit(void)
5946{
5947 struct ibm_struct *ibm, *itmp;
5948
5949 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5950
5951 list_for_each_entry_safe_reverse(ibm, itmp,
5952 &tpacpi_all_drivers,
5953 all_drivers) {
5954 ibm_exit(ibm);
5955 }
5956
5957 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5958
5959 if (tpacpi_inputdev) {
5960 if (tp_features.input_device_registered)
5961 input_unregister_device(tpacpi_inputdev);
5962 else
5963 input_free_device(tpacpi_inputdev);
5964 }
5965
5966 if (tpacpi_hwmon)
5967 hwmon_device_unregister(tpacpi_hwmon);
5968
5969 if (tp_features.sensors_pdev_attrs_registered)
5970 device_remove_file(&tpacpi_sensors_pdev->dev,
5971 &dev_attr_thinkpad_acpi_pdev_name);
5972 if (tpacpi_sensors_pdev)
5973 platform_device_unregister(tpacpi_sensors_pdev);
5974 if (tpacpi_pdev)
5975 platform_device_unregister(tpacpi_pdev);
5976
5977 if (tp_features.sensors_pdrv_attrs_registered)
5978 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5979 if (tp_features.platform_drv_attrs_registered)
5980 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5981
5982 if (tp_features.sensors_pdrv_registered)
5983 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5984
5985 if (tp_features.platform_drv_registered)
5986 platform_driver_unregister(&tpacpi_pdriver);
5987
5988 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005989 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005990
5991 kfree(thinkpad_id.bios_version_str);
5992 kfree(thinkpad_id.ec_version_str);
5993 kfree(thinkpad_id.model_str);
5994}
5995
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005996
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005997static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
5999 int ret, i;
6000
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006001 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6002
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006003 /* Parameter checking */
6004 if (hotkey_report_mode > 2)
6005 return -EINVAL;
6006
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006007 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006008
6009 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006010 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006011 if (ret) {
6012 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006013 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006016 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006017
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006018 TPACPI_ACPIHANDLE_INIT(ecrd);
6019 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006021 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006023 printk(TPACPI_ERR
6024 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006025 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 return -ENODEV;
6027 }
6028 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006029
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006030 ret = platform_driver_register(&tpacpi_pdriver);
6031 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006032 printk(TPACPI_ERR
6033 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006034 thinkpad_acpi_module_exit();
6035 return ret;
6036 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006037 tp_features.platform_drv_registered = 1;
6038
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006039 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6040 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006041 printk(TPACPI_ERR
6042 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006043 thinkpad_acpi_module_exit();
6044 return ret;
6045 }
6046 tp_features.sensors_pdrv_registered = 1;
6047
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006048 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006049 if (!ret) {
6050 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006051 ret = tpacpi_create_driver_attributes(
6052 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006053 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006054 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006055 printk(TPACPI_ERR
6056 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006057 thinkpad_acpi_module_exit();
6058 return ret;
6059 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006060 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006061
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006062
6063 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006064 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006065 NULL, 0);
6066 if (IS_ERR(tpacpi_pdev)) {
6067 ret = PTR_ERR(tpacpi_pdev);
6068 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006069 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006070 thinkpad_acpi_module_exit();
6071 return ret;
6072 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006073 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006074 TPACPI_HWMON_DRVR_NAME,
6075 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006076 if (IS_ERR(tpacpi_sensors_pdev)) {
6077 ret = PTR_ERR(tpacpi_sensors_pdev);
6078 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006079 printk(TPACPI_ERR
6080 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006081 thinkpad_acpi_module_exit();
6082 return ret;
6083 }
6084 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6085 &dev_attr_thinkpad_acpi_pdev_name);
6086 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006087 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006088 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006089 thinkpad_acpi_module_exit();
6090 return ret;
6091 }
6092 tp_features.sensors_pdev_attrs_registered = 1;
6093 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006094 if (IS_ERR(tpacpi_hwmon)) {
6095 ret = PTR_ERR(tpacpi_hwmon);
6096 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006097 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006098 thinkpad_acpi_module_exit();
6099 return ret;
6100 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006101 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006102 tpacpi_inputdev = input_allocate_device();
6103 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006104 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006105 thinkpad_acpi_module_exit();
6106 return -ENOMEM;
6107 } else {
6108 /* Prepare input device, but don't register */
6109 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006110 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006111 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006112 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6113 thinkpad_id.vendor :
6114 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006115 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6116 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6117 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006118 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6119 ret = ibm_init(&ibms_init[i]);
6120 if (ret >= 0 && *ibms_init[i].param)
6121 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006123 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 return ret;
6125 }
6126 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006127 ret = input_register_device(tpacpi_inputdev);
6128 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006129 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006130 thinkpad_acpi_module_exit();
6131 return ret;
6132 } else {
6133 tp_features.input_device_registered = 1;
6134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006136 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 return 0;
6138}
6139
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006140/* Please remove this in year 2009 */
6141MODULE_ALIAS("ibm_acpi");
6142
6143/*
6144 * DMI matching for module autoloading
6145 *
6146 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6147 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6148 *
6149 * Only models listed in thinkwiki will be supported, so add yours
6150 * if it is not there yet.
6151 */
6152#define IBM_BIOS_MODULE_ALIAS(__type) \
6153 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6154
6155/* Non-ancient thinkpads */
6156MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6157MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6158
6159/* Ancient thinkpad BIOSes have to be identified by
6160 * BIOS type or model number, and there are far less
6161 * BIOS types than model numbers... */
6162IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6163IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6164IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6165
6166MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006167MODULE_DESCRIPTION(TPACPI_DESC);
6168MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006169MODULE_LICENSE("GPL");
6170
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006171module_init(thinkpad_acpi_module_init);
6172module_exit(thinkpad_acpi_module_exit);