blob: e18f1e18781fe1fdff06b822993dc59c6bd8a202 [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 Holschuha62bc912007-03-23 17:33:58 -03006 * Copyright (C) 2006-2007 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 Holschuhe0c7dfe2008-01-08 13:02:48 -020024#define TPACPI_VERSION "0.18"
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -020025#define TPACPI_SYSFS_VERSION 0x020101
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
1420static int hotkey_inputdev_open(struct input_dev *dev)
1421{
1422 switch (tpacpi_lifecycle) {
1423 case TPACPI_LIFE_INIT:
1424 /*
1425 * hotkey_init will call hotkey_poll_setup_safe
1426 * at the appropriate moment
1427 */
1428 return 0;
1429 case TPACPI_LIFE_EXITING:
1430 return -EBUSY;
1431 case TPACPI_LIFE_RUNNING:
1432 hotkey_poll_setup_safe(0);
1433 return 0;
1434 }
1435
1436 /* Should only happen if tpacpi_lifecycle is corrupt */
1437 BUG();
1438 return -EBUSY;
1439}
1440
1441static void hotkey_inputdev_close(struct input_dev *dev)
1442{
1443 /* disable hotkey polling when possible */
1444 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1445 hotkey_poll_setup_safe(0);
1446}
1447#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1448
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001449/* sysfs hotkey enable ------------------------------------------------- */
1450static ssize_t hotkey_enable_show(struct device *dev,
1451 struct device_attribute *attr,
1452 char *buf)
1453{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001454 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001455
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001456 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001457 if (res)
1458 return res;
1459
1460 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1461}
1462
1463static ssize_t hotkey_enable_store(struct device *dev,
1464 struct device_attribute *attr,
1465 const char *buf, size_t count)
1466{
1467 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001468 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001469
1470 if (parse_strtoul(buf, 1, &t))
1471 return -EINVAL;
1472
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001473 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001474
1475 return (res) ? res : count;
1476}
1477
1478static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001479 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001480 hotkey_enable_show, hotkey_enable_store);
1481
1482/* sysfs hotkey mask --------------------------------------------------- */
1483static ssize_t hotkey_mask_show(struct device *dev,
1484 struct device_attribute *attr,
1485 char *buf)
1486{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001487 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001488
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001489 if (mutex_lock_interruptible(&hotkey_mutex))
1490 return -ERESTARTSYS;
1491 res = hotkey_mask_get();
1492 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001493
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001494 return (res)?
1495 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001496}
1497
1498static ssize_t hotkey_mask_store(struct device *dev,
1499 struct device_attribute *attr,
1500 const char *buf, size_t count)
1501{
1502 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001503 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001504
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001505 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001506 return -EINVAL;
1507
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001508 if (mutex_lock_interruptible(&hotkey_mutex))
1509 return -ERESTARTSYS;
1510
1511 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001512
1513#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1514 hotkey_poll_setup(1);
1515#endif
1516
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001517 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001518
1519 return (res) ? res : count;
1520}
1521
1522static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001523 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001524 hotkey_mask_show, hotkey_mask_store);
1525
1526/* sysfs hotkey bios_enabled ------------------------------------------- */
1527static ssize_t hotkey_bios_enabled_show(struct device *dev,
1528 struct device_attribute *attr,
1529 char *buf)
1530{
1531 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1532}
1533
1534static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001535 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001536
1537/* sysfs hotkey bios_mask ---------------------------------------------- */
1538static ssize_t hotkey_bios_mask_show(struct device *dev,
1539 struct device_attribute *attr,
1540 char *buf)
1541{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001542 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001543}
1544
1545static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001546 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001547
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001548/* sysfs hotkey all_mask ----------------------------------------------- */
1549static ssize_t hotkey_all_mask_show(struct device *dev,
1550 struct device_attribute *attr,
1551 char *buf)
1552{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001553 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1554 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001555}
1556
1557static struct device_attribute dev_attr_hotkey_all_mask =
1558 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1559
1560/* sysfs hotkey recommended_mask --------------------------------------- */
1561static ssize_t hotkey_recommended_mask_show(struct device *dev,
1562 struct device_attribute *attr,
1563 char *buf)
1564{
1565 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001566 (hotkey_all_mask | hotkey_source_mask)
1567 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001568}
1569
1570static struct device_attribute dev_attr_hotkey_recommended_mask =
1571 __ATTR(hotkey_recommended_mask, S_IRUGO,
1572 hotkey_recommended_mask_show, NULL);
1573
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001574#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1575
1576/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1577static ssize_t hotkey_source_mask_show(struct device *dev,
1578 struct device_attribute *attr,
1579 char *buf)
1580{
1581 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1582}
1583
1584static ssize_t hotkey_source_mask_store(struct device *dev,
1585 struct device_attribute *attr,
1586 const char *buf, size_t count)
1587{
1588 unsigned long t;
1589
1590 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1591 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1592 return -EINVAL;
1593
1594 if (mutex_lock_interruptible(&hotkey_mutex))
1595 return -ERESTARTSYS;
1596
1597 HOTKEY_CONFIG_CRITICAL_START
1598 hotkey_source_mask = t;
1599 HOTKEY_CONFIG_CRITICAL_END
1600
1601 hotkey_poll_setup(1);
1602
1603 mutex_unlock(&hotkey_mutex);
1604
1605 return count;
1606}
1607
1608static struct device_attribute dev_attr_hotkey_source_mask =
1609 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1610 hotkey_source_mask_show, hotkey_source_mask_store);
1611
1612/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1613static ssize_t hotkey_poll_freq_show(struct device *dev,
1614 struct device_attribute *attr,
1615 char *buf)
1616{
1617 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1618}
1619
1620static ssize_t hotkey_poll_freq_store(struct device *dev,
1621 struct device_attribute *attr,
1622 const char *buf, size_t count)
1623{
1624 unsigned long t;
1625
1626 if (parse_strtoul(buf, 25, &t))
1627 return -EINVAL;
1628
1629 if (mutex_lock_interruptible(&hotkey_mutex))
1630 return -ERESTARTSYS;
1631
1632 hotkey_poll_freq = t;
1633
1634 hotkey_poll_setup(1);
1635 mutex_unlock(&hotkey_mutex);
1636
1637 return count;
1638}
1639
1640static struct device_attribute dev_attr_hotkey_poll_freq =
1641 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1642 hotkey_poll_freq_show, hotkey_poll_freq_store);
1643
1644#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1645
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001646/* sysfs hotkey radio_sw ----------------------------------------------- */
1647static ssize_t hotkey_radio_sw_show(struct device *dev,
1648 struct device_attribute *attr,
1649 char *buf)
1650{
1651 int res, s;
1652 res = hotkey_get_wlsw(&s);
1653 if (res < 0)
1654 return res;
1655
1656 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1657}
1658
1659static struct device_attribute dev_attr_hotkey_radio_sw =
1660 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1661
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001662/* sysfs hotkey report_mode -------------------------------------------- */
1663static ssize_t hotkey_report_mode_show(struct device *dev,
1664 struct device_attribute *attr,
1665 char *buf)
1666{
1667 return snprintf(buf, PAGE_SIZE, "%d\n",
1668 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1669}
1670
1671static struct device_attribute dev_attr_hotkey_report_mode =
1672 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1673
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001674/* sysfs wakeup reason ------------------------------------------------- */
1675static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1676 struct device_attribute *attr,
1677 char *buf)
1678{
1679 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1680}
1681
1682static struct device_attribute dev_attr_hotkey_wakeup_reason =
1683 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1684
1685/* sysfs wakeup hotunplug_complete ------------------------------------- */
1686static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1687 struct device_attribute *attr,
1688 char *buf)
1689{
1690 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1691}
1692
1693static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1694 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1695 hotkey_wakeup_hotunplug_complete_show, NULL);
1696
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001697/* --------------------------------------------------------------------- */
1698
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001699static struct attribute *hotkey_attributes[] __initdata = {
1700 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001701 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001702 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001703#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1704 &dev_attr_hotkey_mask.attr,
1705 &dev_attr_hotkey_all_mask.attr,
1706 &dev_attr_hotkey_recommended_mask.attr,
1707 &dev_attr_hotkey_source_mask.attr,
1708 &dev_attr_hotkey_poll_freq.attr,
1709#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001710};
1711
1712static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001713 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001714#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1715 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001716 &dev_attr_hotkey_all_mask.attr,
1717 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001718#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001719 &dev_attr_hotkey_wakeup_reason.attr,
1720 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001721};
1722
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001723static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001724{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001725 /* Requirements for changing the default keymaps:
1726 *
1727 * 1. Many of the keys are mapped to KEY_RESERVED for very
1728 * good reasons. Do not change them unless you have deep
1729 * knowledge on the IBM and Lenovo ThinkPad firmware for
1730 * the various ThinkPad models. The driver behaves
1731 * differently for KEY_RESERVED: such keys have their
1732 * hot key mask *unset* in mask_recommended, and also
1733 * in the initial hot key mask programmed into the
1734 * firmware at driver load time, which means the firm-
1735 * ware may react very differently if you change them to
1736 * something else;
1737 *
1738 * 2. You must be subscribed to the linux-thinkpad and
1739 * ibm-acpi-devel mailing lists, and you should read the
1740 * list archives since 2007 if you want to change the
1741 * keymaps. This requirement exists so that you will
1742 * know the past history of problems with the thinkpad-
1743 * acpi driver keymaps, and also that you will be
1744 * listening to any bug reports;
1745 *
1746 * 3. Do not send thinkpad-acpi specific patches directly to
1747 * for merging, *ever*. Send them to the linux-acpi
1748 * mailinglist for comments. Merging is to be done only
1749 * through acpi-test and the ACPI maintainer.
1750 *
1751 * If the above is too much to ask, don't change the keymap.
1752 * Ask the thinkpad-acpi maintainer to do it, instead.
1753 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001754 static u16 ibm_keycode_map[] __initdata = {
1755 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1756 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1757 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1758 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001759
1760 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001761 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1762 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1763 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001764
1765 /* brightness: firmware always reacts to them, unless
1766 * X.org did some tricks in the radeon BIOS scratch
1767 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001768 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001769 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001770
1771 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001772 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001773
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001774 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1775 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001776
1777 /* Volume: firmware always react to it and reprograms
1778 * the built-in *extra* mixer. Never map it to control
1779 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001780 KEY_RESERVED, /* 0x14: VOLUME UP */
1781 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1782 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001783
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001784 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001785
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001786 /* (assignments unknown, please report if found) */
1787 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1788 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1789 };
1790 static u16 lenovo_keycode_map[] __initdata = {
1791 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1792 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1793 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1794 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001795
1796 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001797 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1798 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1799 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001800
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001801 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001802 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001803
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001804 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001805
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001806 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1807 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001808
1809 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1810 * react to it and reprograms the built-in *extra* mixer.
1811 * Never map it to control another mixer by default.
1812 *
1813 * T60?, T61, R60?, R61: firmware and EC tries to send
1814 * these over the regular keyboard, so these are no-ops,
1815 * but there are still weird bugs re. MUTE, so do not
1816 * change unless you get test reports from all Lenovo
1817 * models. May cause the BIOS to interfere with the
1818 * HDA mixer.
1819 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001820 KEY_RESERVED, /* 0x14: VOLUME UP */
1821 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1822 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001823
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001824 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001825
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001826 /* (assignments unknown, please report if found) */
1827 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1828 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1829 };
1830
1831#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1832#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1833#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1834
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001835 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001836 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001837 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001838
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001839 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1840
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001841 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001842 BUG_ON(tpacpi_inputdev->open != NULL ||
1843 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001844
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001845 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001846 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001847
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001848#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1849 mutex_init(&hotkey_thread_mutex);
1850 mutex_init(&hotkey_thread_data_mutex);
1851#endif
1852
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001853 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001854 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001855
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001856 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001857 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001858
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001859 if (tp_features.hotkey) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001860 hotkey_dev_attributes = create_attr_set(12, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001861 if (!hotkey_dev_attributes)
1862 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001863 res = add_many_to_attr_set(hotkey_dev_attributes,
1864 hotkey_attributes,
1865 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001866 if (res)
1867 return res;
1868
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001869 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001870 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1871 for HKEY interface version 0x100 */
1872 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1873 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001874 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001875 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001876 printk(TPACPI_ERR "please report this to %s\n",
1877 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001878 } else {
1879 /*
1880 * MHKV 0x100 in A31, R40, R40e,
1881 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001882 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001883 tp_features.hotkey_mask = 1;
1884 }
1885 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001886
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001887 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001888 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001889
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001890 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001891 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001892 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001893 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001894 "missing MHKA handler, "
1895 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001896 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001897 /* FN+F12, FN+F4, FN+F3 */
1898 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001899 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001900 }
1901
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001902 /* hotkey_source_mask *must* be zero for
1903 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001904 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001905 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001906 res = hotkey_mask_get();
1907 hotkey_orig_mask = hotkey_mask;
1908 if (!res) {
1909 res = add_many_to_attr_set(
1910 hotkey_dev_attributes,
1911 hotkey_mask_attributes,
1912 ARRAY_SIZE(hotkey_mask_attributes));
1913 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001914 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001915
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001916#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1917 if (tp_features.hotkey_mask) {
1918 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1919 & ~hotkey_all_mask;
1920 } else {
1921 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1922 }
1923
1924 vdbg_printk(TPACPI_DBG_INIT,
1925 "hotkey source mask 0x%08x, polling freq %d\n",
1926 hotkey_source_mask, hotkey_poll_freq);
1927#endif
1928
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001929 /* Not all thinkpads have a hardware radio switch */
1930 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1931 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001932 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001933 "radio switch found; radios are %s\n",
1934 enabled(status, 0));
1935 res = add_to_attr_set(hotkey_dev_attributes,
1936 &dev_attr_hotkey_radio_sw.attr);
1937 }
1938
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001939 if (!res)
1940 res = register_attr_set_with_sysfs(
1941 hotkey_dev_attributes,
1942 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001943 if (res)
1944 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001945
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001946 /* Set up key map */
1947
1948 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1949 GFP_KERNEL);
1950 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001951 printk(TPACPI_ERR
1952 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001953 return -ENOMEM;
1954 }
1955
1956 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1957 dbg_printk(TPACPI_DBG_INIT,
1958 "using Lenovo default hot key map\n");
1959 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1960 TPACPI_HOTKEY_MAP_SIZE);
1961 } else {
1962 dbg_printk(TPACPI_DBG_INIT,
1963 "using IBM default hot key map\n");
1964 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1965 TPACPI_HOTKEY_MAP_SIZE);
1966 }
1967
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001968 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1969 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1970 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001971 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1972 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1973 tpacpi_inputdev->keycode = hotkey_keycode_map;
1974 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001975 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1976 set_bit(hotkey_keycode_map[i],
1977 tpacpi_inputdev->keybit);
1978 } else {
1979 if (i < sizeof(hotkey_reserved_mask)*8)
1980 hotkey_reserved_mask |= 1 << i;
1981 }
1982 }
1983
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001984 if (tp_features.hotkey_wlsw) {
1985 set_bit(EV_SW, tpacpi_inputdev->evbit);
1986 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1987 }
1988
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001989 dbg_printk(TPACPI_DBG_INIT,
1990 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001991 res = hotkey_status_set(1);
1992 if (res)
1993 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001994 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1995 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001996 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001997 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001998 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001999
2000 dbg_printk(TPACPI_DBG_INIT,
2001 "legacy hot key reporting over procfs %s\n",
2002 (hotkey_report_mode < 2) ?
2003 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002004
2005#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2006 tpacpi_inputdev->open = &hotkey_inputdev_open;
2007 tpacpi_inputdev->close = &hotkey_inputdev_close;
2008
2009 hotkey_poll_setup_safe(1);
2010#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002011 }
2012
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002013 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002014}
2015
2016static void hotkey_exit(void)
2017{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002018#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2019 hotkey_poll_stop_sync();
2020#endif
2021
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002022 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002023 dbg_printk(TPACPI_DBG_EXIT,
2024 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002025 /* no short-circuit boolean operator below! */
2026 if ((hotkey_mask_set(hotkey_orig_mask) |
2027 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002028 printk(TPACPI_ERR
2029 "failed to restore hot key mask "
2030 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002031 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002032
2033 if (hotkey_dev_attributes) {
2034 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2035 hotkey_dev_attributes = NULL;
2036 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002037}
2038
2039static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2040{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002041 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002042 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002043 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002044 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002045 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002046
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002047 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002048 printk(TPACPI_ERR
2049 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002050 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002051 acpi_bus_generate_netlink_event(
2052 ibm->acpi->device->pnp.device_class,
2053 ibm->acpi->device->dev.bus_id,
2054 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002055 return;
2056 }
2057
2058 while (1) {
2059 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002060 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002061 return;
2062 }
2063
2064 if (hkey == 0) {
2065 /* queue empty */
2066 return;
2067 }
2068
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002069 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002070 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002071 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002072
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002073 switch (hkey >> 12) {
2074 case 1:
2075 /* 0x1000-0x1FFF: key presses */
2076 scancode = hkey & 0xfff;
2077 if (scancode > 0 && scancode < 0x21) {
2078 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002079 if (!(hotkey_source_mask & (1 << scancode))) {
2080 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002081 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002082 } else {
2083 ignore_acpi_ev = 1;
2084 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002085 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002086 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002087 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002088 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002089 case 2:
2090 /* Wakeup reason */
2091 switch (hkey) {
2092 case 0x2304: /* suspend, undock */
2093 case 0x2404: /* hibernation, undock */
2094 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2095 ignore_acpi_ev = 1;
2096 break;
2097 case 0x2305: /* suspend, bay eject */
2098 case 0x2405: /* hibernation, bay eject */
2099 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2100 ignore_acpi_ev = 1;
2101 break;
2102 default:
2103 unk_ev = 1;
2104 }
2105 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2106 printk(TPACPI_INFO
2107 "woke up due to a hot-unplug "
2108 "request...\n");
2109 }
2110 break;
2111 case 3:
2112 /* bay-related wakeups */
2113 if (hkey == 0x3003) {
2114 hotkey_autosleep_ack = 1;
2115 printk(TPACPI_INFO
2116 "bay ejected\n");
2117 } else {
2118 unk_ev = 1;
2119 }
2120 break;
2121 case 4:
2122 /* dock-related wakeups */
2123 if (hkey == 0x4003) {
2124 hotkey_autosleep_ack = 1;
2125 printk(TPACPI_INFO
2126 "undocked\n");
2127 } else {
2128 unk_ev = 1;
2129 }
2130 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002131 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002132 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002133 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002134 case 0x5010: /* Lenovo new BIOS: brightness changed */
2135 case 0x5009: /* X61t: swivel up (tablet mode) */
2136 case 0x500a: /* X61t: swivel down (normal mode) */
2137 case 0x500b: /* X61t: tablet pen inserted into bay */
2138 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002139 break;
2140 case 0x5001:
2141 case 0x5002:
2142 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002143 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002144 break;
2145 default:
2146 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002147 }
2148 break;
2149 case 7:
2150 /* 0x7000-0x7FFF: misc */
2151 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2152 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002153 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002154 break;
2155 }
2156 /* fallthrough to default */
2157 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002158 unk_ev = 1;
2159 }
2160 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002161 printk(TPACPI_NOTICE
2162 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002163 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002164
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002165 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002166 if (!ignore_acpi_ev &&
2167 (send_acpi_ev || hotkey_report_mode < 2)) {
2168 acpi_bus_generate_proc_event(ibm->acpi->device,
2169 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002170 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002171
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002172 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002173 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002174 acpi_bus_generate_netlink_event(
2175 ibm->acpi->device->pnp.device_class,
2176 ibm->acpi->device->dev.bus_id,
2177 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002178 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002179 }
2180}
2181
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002182static void hotkey_suspend(pm_message_t state)
2183{
2184 /* Do these on suspend, we get the events on early resume! */
2185 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2186 hotkey_autosleep_ack = 0;
2187}
2188
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002189static void hotkey_resume(void)
2190{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002191 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002192 printk(TPACPI_ERR
2193 "error while trying to read hot key mask "
2194 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002195 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002196#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2197 hotkey_poll_setup_safe(0);
2198#endif
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002199}
2200
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002201/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002202static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002204 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 int len = 0;
2206
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002207 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002208 len += sprintf(p + len, "status:\t\tnot supported\n");
2209 return len;
2210 }
2211
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002212 if (mutex_lock_interruptible(&hotkey_mutex))
2213 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002214 res = hotkey_status_get(&status);
2215 if (!res)
2216 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002217 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002218 if (res)
2219 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002222 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002223 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 len += sprintf(p + len,
2225 "commands:\tenable, disable, reset, <mask>\n");
2226 } else {
2227 len += sprintf(p + len, "mask:\t\tnot supported\n");
2228 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2229 }
2230
2231 return len;
2232}
2233
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002234static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002236 int res, status;
2237 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002240 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return -ENODEV;
2242
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002243 if (mutex_lock_interruptible(&hotkey_mutex))
2244 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002245
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002246 status = -1;
2247 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002248
2249 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 while ((cmd = next_cmd(&buf))) {
2251 if (strlencmp(cmd, "enable") == 0) {
2252 status = 1;
2253 } else if (strlencmp(cmd, "disable") == 0) {
2254 status = 0;
2255 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002256 status = hotkey_orig_status;
2257 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2259 /* mask set */
2260 } else if (sscanf(cmd, "%x", &mask) == 1) {
2261 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002262 } else {
2263 res = -EINVAL;
2264 goto errexit;
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002267 if (status != -1)
2268 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002270 if (!res && mask != hotkey_mask)
2271 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002273errexit:
2274 mutex_unlock(&hotkey_mutex);
2275 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002278static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002279 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002280 {"", 0},
2281};
2282
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002283static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002284 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002285 .notify = hotkey_notify,
2286 .handle = &hkey_handle,
2287 .type = ACPI_DEVICE_NOTIFY,
2288};
2289
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002290static struct ibm_struct hotkey_driver_data = {
2291 .name = "hotkey",
2292 .read = hotkey_read,
2293 .write = hotkey_write,
2294 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002295 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002296 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002297 .acpi = &ibm_hotkey_acpidriver,
2298};
2299
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002300/*************************************************************************
2301 * Bluetooth subdriver
2302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002304enum {
2305 /* ACPI GBDC/SBDC bits */
2306 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2307 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2308 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2309};
2310
2311static int bluetooth_get_radiosw(void);
2312static int bluetooth_set_radiosw(int radio_on);
2313
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002314/* sysfs bluetooth enable ---------------------------------------------- */
2315static ssize_t bluetooth_enable_show(struct device *dev,
2316 struct device_attribute *attr,
2317 char *buf)
2318{
2319 int status;
2320
2321 status = bluetooth_get_radiosw();
2322 if (status < 0)
2323 return status;
2324
2325 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2326}
2327
2328static ssize_t bluetooth_enable_store(struct device *dev,
2329 struct device_attribute *attr,
2330 const char *buf, size_t count)
2331{
2332 unsigned long t;
2333 int res;
2334
2335 if (parse_strtoul(buf, 1, &t))
2336 return -EINVAL;
2337
2338 res = bluetooth_set_radiosw(t);
2339
2340 return (res) ? res : count;
2341}
2342
2343static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002344 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002345 bluetooth_enable_show, bluetooth_enable_store);
2346
2347/* --------------------------------------------------------------------- */
2348
2349static struct attribute *bluetooth_attributes[] = {
2350 &dev_attr_bluetooth_enable.attr,
2351 NULL
2352};
2353
2354static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002355 .attrs = bluetooth_attributes,
2356};
2357
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002358static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002360 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002361 int status = 0;
2362
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002363 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2364
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002365 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002366
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002367 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2368 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002369 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002370 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002372 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2373 str_supported(tp_features.bluetooth),
2374 status);
2375
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002376 if (tp_features.bluetooth) {
2377 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2378 /* no bluetooth hardware present in system */
2379 tp_features.bluetooth = 0;
2380 dbg_printk(TPACPI_DBG_INIT,
2381 "bluetooth hardware not installed\n");
2382 } else {
2383 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2384 &bluetooth_attr_group);
2385 if (res)
2386 return res;
2387 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002388 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002389
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002390 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002393static void bluetooth_exit(void)
2394{
2395 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2396 &bluetooth_attr_group);
2397}
2398
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002399static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
2401 int status;
2402
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002403 if (!tp_features.bluetooth)
2404 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002406 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2407 return -EIO;
2408
2409 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2410}
2411
2412static int bluetooth_set_radiosw(int radio_on)
2413{
2414 int status;
2415
2416 if (!tp_features.bluetooth)
2417 return -ENODEV;
2418
2419 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2420 return -EIO;
2421 if (radio_on)
2422 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2423 else
2424 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2425 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2426 return -EIO;
2427
2428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002431/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002432static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
2434 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002435 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002437 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002440 len += sprintf(p + len, "status:\t\t%s\n",
2441 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 len += sprintf(p + len, "commands:\tenable, disable\n");
2443 }
2444
2445 return len;
2446}
2447
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002448static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002452 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002453 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 while ((cmd = next_cmd(&buf))) {
2456 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002457 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002459 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 } else
2461 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return 0;
2465}
2466
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002467static struct ibm_struct bluetooth_driver_data = {
2468 .name = "bluetooth",
2469 .read = bluetooth_read,
2470 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002471 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002472};
2473
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002474/*************************************************************************
2475 * Wan subdriver
2476 */
2477
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002478enum {
2479 /* ACPI GWAN/SWAN bits */
2480 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2481 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2482 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2483};
2484
2485static int wan_get_radiosw(void);
2486static int wan_set_radiosw(int radio_on);
2487
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002488/* sysfs wan enable ---------------------------------------------------- */
2489static ssize_t wan_enable_show(struct device *dev,
2490 struct device_attribute *attr,
2491 char *buf)
2492{
2493 int status;
2494
2495 status = wan_get_radiosw();
2496 if (status < 0)
2497 return status;
2498
2499 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2500}
2501
2502static ssize_t wan_enable_store(struct device *dev,
2503 struct device_attribute *attr,
2504 const char *buf, size_t count)
2505{
2506 unsigned long t;
2507 int res;
2508
2509 if (parse_strtoul(buf, 1, &t))
2510 return -EINVAL;
2511
2512 res = wan_set_radiosw(t);
2513
2514 return (res) ? res : count;
2515}
2516
2517static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002518 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002519 wan_enable_show, wan_enable_store);
2520
2521/* --------------------------------------------------------------------- */
2522
2523static struct attribute *wan_attributes[] = {
2524 &dev_attr_wan_enable.attr,
2525 NULL
2526};
2527
2528static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002529 .attrs = wan_attributes,
2530};
2531
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002532static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002533{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002534 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002535 int status = 0;
2536
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002537 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2538
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002539 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002540
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002541 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002542 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002543
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002544 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2545 str_supported(tp_features.wan),
2546 status);
2547
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002548 if (tp_features.wan) {
2549 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2550 /* no wan hardware present in system */
2551 tp_features.wan = 0;
2552 dbg_printk(TPACPI_DBG_INIT,
2553 "wan hardware not installed\n");
2554 } else {
2555 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2556 &wan_attr_group);
2557 if (res)
2558 return res;
2559 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002560 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002561
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002562 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002563}
2564
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002565static void wan_exit(void)
2566{
2567 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2568 &wan_attr_group);
2569}
2570
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002571static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002572{
2573 int status;
2574
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002575 if (!tp_features.wan)
2576 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002577
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002578 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2579 return -EIO;
2580
2581 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2582}
2583
2584static int wan_set_radiosw(int radio_on)
2585{
2586 int status;
2587
2588 if (!tp_features.wan)
2589 return -ENODEV;
2590
2591 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2592 return -EIO;
2593 if (radio_on)
2594 status |= TP_ACPI_WANCARD_RADIOSSW;
2595 else
2596 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2597 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2598 return -EIO;
2599
2600 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002601}
2602
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002603/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002604static int wan_read(char *p)
2605{
2606 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002607 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002608
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002609 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002610 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002611 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002612 len += sprintf(p + len, "status:\t\t%s\n",
2613 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002614 len += sprintf(p + len, "commands:\tenable, disable\n");
2615 }
2616
2617 return len;
2618}
2619
2620static int wan_write(char *buf)
2621{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002622 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002623
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002624 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002625 return -ENODEV;
2626
2627 while ((cmd = next_cmd(&buf))) {
2628 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002629 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002630 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002631 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002632 } else
2633 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002634 }
2635
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002636 return 0;
2637}
2638
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002639static struct ibm_struct wan_driver_data = {
2640 .name = "wan",
2641 .read = wan_read,
2642 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002643 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002644 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002645};
2646
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002647/*************************************************************************
2648 * Video subdriver
2649 */
2650
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002651enum video_access_mode {
2652 TPACPI_VIDEO_NONE = 0,
2653 TPACPI_VIDEO_570, /* 570 */
2654 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2655 TPACPI_VIDEO_NEW, /* all others */
2656};
2657
2658enum { /* video status flags, based on VIDEO_570 */
2659 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2660 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2661 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2662};
2663
2664enum { /* TPACPI_VIDEO_570 constants */
2665 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2666 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2667 * video_status_flags */
2668 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2669 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2670};
2671
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002672static enum video_access_mode video_supported;
2673static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002674
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002675static int video_autosw_get(void);
2676static int video_autosw_set(int enable);
2677
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002678TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002679 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2680 "\\_SB.PCI0.VID0", /* 770e */
2681 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2682 "\\_SB.PCI0.AGP.VID", /* all others */
2683 ); /* R30, R31 */
2684
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002685TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002686
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002687static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002689 int ivga;
2690
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002691 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2692
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002693 TPACPI_ACPIHANDLE_INIT(vid);
2694 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002695
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002696 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2697 /* G41, assume IVGA doesn't change */
2698 vid_handle = vid2_handle;
2699
2700 if (!vid_handle)
2701 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002702 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002703 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2704 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002705 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002706 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2707 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002708 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002709 else
2710 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002711 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002713 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2714 str_supported(video_supported != TPACPI_VIDEO_NONE),
2715 video_supported);
2716
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002717 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002720static void video_exit(void)
2721{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002722 dbg_printk(TPACPI_DBG_EXIT,
2723 "restoring original video autoswitch mode\n");
2724 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002725 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002726 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002727}
2728
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002729static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 int status = 0;
2732 int i;
2733
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002734 switch (video_supported) {
2735 case TPACPI_VIDEO_570:
2736 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2737 TP_ACPI_VIDEO_570_PHSCMD))
2738 return -EIO;
2739 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2740 break;
2741 case TPACPI_VIDEO_770:
2742 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2743 return -EIO;
2744 if (i)
2745 status |= TP_ACPI_VIDEO_S_LCD;
2746 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2747 return -EIO;
2748 if (i)
2749 status |= TP_ACPI_VIDEO_S_CRT;
2750 break;
2751 case TPACPI_VIDEO_NEW:
2752 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2753 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2754 return -EIO;
2755 if (i)
2756 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002758 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2759 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2760 return -EIO;
2761 if (i)
2762 status |= TP_ACPI_VIDEO_S_LCD;
2763 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2764 return -EIO;
2765 if (i)
2766 status |= TP_ACPI_VIDEO_S_DVI;
2767 break;
2768 default:
2769 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 return status;
2773}
2774
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002775static int video_outputsw_set(int status)
2776{
2777 int autosw;
2778 int res = 0;
2779
2780 switch (video_supported) {
2781 case TPACPI_VIDEO_570:
2782 res = acpi_evalf(NULL, NULL,
2783 "\\_SB.PHS2", "vdd",
2784 TP_ACPI_VIDEO_570_PHS2CMD,
2785 status | TP_ACPI_VIDEO_570_PHS2SET);
2786 break;
2787 case TPACPI_VIDEO_770:
2788 autosw = video_autosw_get();
2789 if (autosw < 0)
2790 return autosw;
2791
2792 res = video_autosw_set(1);
2793 if (res)
2794 return res;
2795 res = acpi_evalf(vid_handle, NULL,
2796 "ASWT", "vdd", status * 0x100, 0);
2797 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002798 printk(TPACPI_ERR
2799 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002800 return -EIO;
2801 }
2802 break;
2803 case TPACPI_VIDEO_NEW:
2804 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002805 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002806 break;
2807 default:
2808 return -ENOSYS;
2809 }
2810
2811 return (res)? 0 : -EIO;
2812}
2813
2814static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002816 int autosw = 0;
2817
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002818 switch (video_supported) {
2819 case TPACPI_VIDEO_570:
2820 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2821 return -EIO;
2822 break;
2823 case TPACPI_VIDEO_770:
2824 case TPACPI_VIDEO_NEW:
2825 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2826 return -EIO;
2827 break;
2828 default:
2829 return -ENOSYS;
2830 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002831
2832 return autosw & 1;
2833}
2834
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002835static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002836{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002837 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002838 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002839 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002840}
2841
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002842static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002843{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002844 int autosw = video_autosw_get();
2845 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002846
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002847 if (autosw < 0)
2848 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002849
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002850 switch (video_supported) {
2851 case TPACPI_VIDEO_570:
2852 res = video_autosw_set(1);
2853 if (res)
2854 return res;
2855 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2856 break;
2857 case TPACPI_VIDEO_770:
2858 case TPACPI_VIDEO_NEW:
2859 res = video_autosw_set(1);
2860 if (res)
2861 return res;
2862 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2863 break;
2864 default:
2865 return -ENOSYS;
2866 }
2867 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002868 printk(TPACPI_ERR
2869 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002870 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002871 }
2872
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002873 return (res)? 0 : -EIO;
2874}
2875
2876static int video_expand_toggle(void)
2877{
2878 switch (video_supported) {
2879 case TPACPI_VIDEO_570:
2880 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2881 0 : -EIO;
2882 case TPACPI_VIDEO_770:
2883 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2884 0 : -EIO;
2885 case TPACPI_VIDEO_NEW:
2886 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2887 0 : -EIO;
2888 default:
2889 return -ENOSYS;
2890 }
2891 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002892}
2893
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002894static int video_read(char *p)
2895{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002896 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002897 int len = 0;
2898
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002899 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002900 len += sprintf(p + len, "status:\t\tnot supported\n");
2901 return len;
2902 }
2903
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002904 status = video_outputsw_get();
2905 if (status < 0)
2906 return status;
2907
2908 autosw = video_autosw_get();
2909 if (autosw < 0)
2910 return autosw;
2911
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002912 len += sprintf(p + len, "status:\t\tsupported\n");
2913 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2914 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002915 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002916 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2917 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2918 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2919 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002920 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002921 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2922 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2923 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2924
2925 return len;
2926}
2927
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002928static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 char *cmd;
2931 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002932 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002934 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002935 return -ENODEV;
2936
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002937 enable = 0;
2938 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 while ((cmd = next_cmd(&buf))) {
2941 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002942 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002944 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002946 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002948 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002949 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002950 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002951 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002952 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002953 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002954 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002956 res = video_autosw_set(1);
2957 if (res)
2958 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002960 res = video_autosw_set(0);
2961 if (res)
2962 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002964 res = video_outputsw_cycle();
2965 if (res)
2966 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002968 res = video_expand_toggle();
2969 if (res)
2970 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 } else
2972 return -EINVAL;
2973 }
2974
2975 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002976 status = video_outputsw_get();
2977 if (status < 0)
2978 return status;
2979 res = video_outputsw_set((status & ~disable) | enable);
2980 if (res)
2981 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
2983
2984 return 0;
2985}
2986
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002987static struct ibm_struct video_driver_data = {
2988 .name = "video",
2989 .read = video_read,
2990 .write = video_write,
2991 .exit = video_exit,
2992};
2993
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002994/*************************************************************************
2995 * Light (thinklight) subdriver
2996 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002998TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2999TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003000
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003001static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003003 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3004
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003005 TPACPI_ACPIHANDLE_INIT(ledb);
3006 TPACPI_ACPIHANDLE_INIT(lght);
3007 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003008
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003009 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003010 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003011
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003012 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003013 /* light status not supported on
3014 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003015 tp_features.light_status =
3016 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003018 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003019 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003020
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003021 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003024static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
3026 int len = 0;
3027 int status = 0;
3028
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003029 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003030 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003031 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003032 len += sprintf(p + len, "status:\t\tunknown\n");
3033 len += sprintf(p + len, "commands:\ton, off\n");
3034 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3036 return -EIO;
3037 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003038 len += sprintf(p + len, "commands:\ton, off\n");
3039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 return len;
3042}
3043
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003044static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
3046 int cmos_cmd, lght_cmd;
3047 char *cmd;
3048 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003049
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003050 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003051 return -ENODEV;
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 while ((cmd = next_cmd(&buf))) {
3054 if (strlencmp(cmd, "on") == 0) {
3055 cmos_cmd = 0x0c;
3056 lght_cmd = 1;
3057 } else if (strlencmp(cmd, "off") == 0) {
3058 cmos_cmd = 0x0d;
3059 lght_cmd = 0;
3060 } else
3061 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003064 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3065 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (!success)
3067 return -EIO;
3068 }
3069
3070 return 0;
3071}
3072
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003073static struct ibm_struct light_driver_data = {
3074 .name = "light",
3075 .read = light_read,
3076 .write = light_write,
3077};
3078
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003079/*************************************************************************
3080 * Dock subdriver
3081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003083#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003084
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003085static void dock_notify(struct ibm_struct *ibm, u32 event);
3086static int dock_read(char *p);
3087static int dock_write(char *buf);
3088
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003089TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003090 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3091 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3092 "\\_SB.PCI.ISA.SLCE", /* 570 */
3093 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3094
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003095/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003096TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003097
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003098static const struct acpi_device_id ibm_pci_device_ids[] = {
3099 {PCI_ROOT_HID_STRING, 0},
3100 {"", 0},
3101};
3102
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003103static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3104 {
3105 .notify = dock_notify,
3106 .handle = &dock_handle,
3107 .type = ACPI_SYSTEM_NOTIFY,
3108 },
3109 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003110 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3111 * We just use it to get notifications of dock hotplug
3112 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003113 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003114 .notify = dock_notify,
3115 .handle = &pci_handle,
3116 .type = ACPI_SYSTEM_NOTIFY,
3117 },
3118};
3119
3120static struct ibm_struct dock_driver_data[2] = {
3121 {
3122 .name = "dock",
3123 .read = dock_read,
3124 .write = dock_write,
3125 .acpi = &ibm_dock_acpidriver[0],
3126 },
3127 {
3128 .name = "dock",
3129 .acpi = &ibm_dock_acpidriver[1],
3130 },
3131};
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133#define dock_docked() (_sta(dock_handle) & 1)
3134
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003135static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003136{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003137 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3138
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003139 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003140
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003141 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3142 str_supported(dock_handle != NULL));
3143
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003144 return (dock_handle)? 0 : 1;
3145}
3146
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003147static int __init dock_init2(struct ibm_init_struct *iibm)
3148{
3149 int dock2_needed;
3150
3151 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3152
3153 if (dock_driver_data[0].flags.acpi_driver_registered &&
3154 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003155 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003156 dock2_needed = (pci_handle != NULL);
3157 vdbg_printk(TPACPI_DBG_INIT,
3158 "dock PCI handler for the TP 570 is %s\n",
3159 str_supported(dock2_needed));
3160 } else {
3161 vdbg_printk(TPACPI_DBG_INIT,
3162 "dock subdriver part 2 not required\n");
3163 dock2_needed = 0;
3164 }
3165
3166 return (dock2_needed)? 0 : 1;
3167}
3168
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003169static void dock_notify(struct ibm_struct *ibm, u32 event)
3170{
3171 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003172 int pci = ibm->acpi->hid && ibm->acpi->device &&
3173 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003174 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003175
3176 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003177 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003178 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003179 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003180 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003181 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003182 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003183 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003184 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003185 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003186 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003187 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003188 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003189 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003190 }
Len Brown14e04fb32007-08-23 15:20:26 -04003191 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003192 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3193 ibm->acpi->device->dev.bus_id,
3194 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003195}
3196
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003197static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
3199 int len = 0;
3200 int docked = dock_docked();
3201
3202 if (!dock_handle)
3203 len += sprintf(p + len, "status:\t\tnot supported\n");
3204 else if (!docked)
3205 len += sprintf(p + len, "status:\t\tundocked\n");
3206 else {
3207 len += sprintf(p + len, "status:\t\tdocked\n");
3208 len += sprintf(p + len, "commands:\tdock, undock\n");
3209 }
3210
3211 return len;
3212}
3213
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003214static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 char *cmd;
3217
3218 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003219 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221 while ((cmd = next_cmd(&buf))) {
3222 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003223 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3224 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return -EIO;
3226 } else if (strlencmp(cmd, "dock") == 0) {
3227 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3228 return -EIO;
3229 } else
3230 return -EINVAL;
3231 }
3232
3233 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003234}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003236#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003238/*************************************************************************
3239 * Bay subdriver
3240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003242#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003243
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003244TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003245 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3246 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3247 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3248 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003249TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003250 "_EJ0", /* all others */
3251 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003252TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003253 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3254 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003255TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003256 "_EJ0", /* 770x */
3257 ); /* all others */
3258
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003259static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003261 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3262
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003263 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003264 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003265 TPACPI_ACPIHANDLE_INIT(bay_ej);
3266 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003267 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003268 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003269
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003270 tp_features.bay_status = bay_handle &&
3271 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3272 tp_features.bay_status2 = bay2_handle &&
3273 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003274
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003275 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3276 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3277 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3278 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003280 vdbg_printk(TPACPI_DBG_INIT,
3281 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003282 str_supported(tp_features.bay_status),
3283 str_supported(tp_features.bay_eject),
3284 str_supported(tp_features.bay_status2),
3285 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003286
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003287 return (tp_features.bay_status || tp_features.bay_eject ||
3288 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003291static void bay_notify(struct ibm_struct *ibm, u32 event)
3292{
Len Brown14e04fb32007-08-23 15:20:26 -04003293 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003294 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3295 ibm->acpi->device->dev.bus_id,
3296 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003297}
3298
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003299#define bay_occupied(b) (_sta(b##_handle) & 1)
3300
3301static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
3303 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003304 int occupied = bay_occupied(bay);
3305 int occupied2 = bay_occupied(bay2);
3306 int eject, eject2;
3307
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003308 len += sprintf(p + len, "status:\t\t%s\n",
3309 tp_features.bay_status ?
3310 (occupied ? "occupied" : "unoccupied") :
3311 "not supported");
3312 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003313 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3314 "occupied" : "unoccupied");
3315
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003316 eject = tp_features.bay_eject && occupied;
3317 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003318
3319 if (eject && eject2)
3320 len += sprintf(p + len, "commands:\teject, eject2\n");
3321 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003323 else if (eject2)
3324 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 return len;
3327}
3328
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003329static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
3331 char *cmd;
3332
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003333 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003334 return -ENODEV;
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003337 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003338 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3339 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003340 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003341 strlencmp(cmd, "eject2") == 0) {
3342 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return -EIO;
3344 } else
3345 return -EINVAL;
3346 }
3347
3348 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003349}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003350
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003351static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3352 .notify = bay_notify,
3353 .handle = &bay_handle,
3354 .type = ACPI_SYSTEM_NOTIFY,
3355};
3356
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003357static struct ibm_struct bay_driver_data = {
3358 .name = "bay",
3359 .read = bay_read,
3360 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003361 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003362};
3363
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003364#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003366/*************************************************************************
3367 * CMOS subdriver
3368 */
3369
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003370/* sysfs cmos_command -------------------------------------------------- */
3371static ssize_t cmos_command_store(struct device *dev,
3372 struct device_attribute *attr,
3373 const char *buf, size_t count)
3374{
3375 unsigned long cmos_cmd;
3376 int res;
3377
3378 if (parse_strtoul(buf, 21, &cmos_cmd))
3379 return -EINVAL;
3380
3381 res = issue_thinkpad_cmos_command(cmos_cmd);
3382 return (res)? res : count;
3383}
3384
3385static struct device_attribute dev_attr_cmos_command =
3386 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3387
3388/* --------------------------------------------------------------------- */
3389
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003390static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003391{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003392 int res;
3393
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003394 vdbg_printk(TPACPI_DBG_INIT,
3395 "initializing cmos commands subdriver\n");
3396
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003397 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003398
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003399 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3400 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003401
3402 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3403 if (res)
3404 return res;
3405
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003406 return (cmos_handle)? 0 : 1;
3407}
3408
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003409static void cmos_exit(void)
3410{
3411 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3412}
3413
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003414static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415{
3416 int len = 0;
3417
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003418 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3419 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 if (!cmos_handle)
3421 len += sprintf(p + len, "status:\t\tnot supported\n");
3422 else {
3423 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003424 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
3426
3427 return len;
3428}
3429
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003430static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
3432 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003433 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
3435 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003436 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3437 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 /* cmos_cmd set */
3439 } else
3440 return -EINVAL;
3441
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003442 res = issue_thinkpad_cmos_command(cmos_cmd);
3443 if (res)
3444 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
3446
3447 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003448}
3449
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003450static struct ibm_struct cmos_driver_data = {
3451 .name = "cmos",
3452 .read = cmos_read,
3453 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003454 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003455};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003456
3457/*************************************************************************
3458 * LED subdriver
3459 */
3460
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003461enum led_access_mode {
3462 TPACPI_LED_NONE = 0,
3463 TPACPI_LED_570, /* 570 */
3464 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3465 TPACPI_LED_NEW, /* all others */
3466};
3467
3468enum { /* For TPACPI_LED_OLD */
3469 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3470 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3471 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3472};
3473
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003474static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003475
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003476TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003477 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3478 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003479 "LED", /* all others */
3480 ); /* R30, R31 */
3481
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003482static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003483{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003484 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3485
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003486 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003487
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003488 if (!led_handle)
3489 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003490 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003491 else if (strlencmp(led_path, "SLED") == 0)
3492 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003493 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003494 else if (strlencmp(led_path, "SYSL") == 0)
3495 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003496 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003497 else
3498 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003499 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003500
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003501 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3502 str_supported(led_supported), led_supported);
3503
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003504 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003505}
3506
3507#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3508
3509static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510{
3511 int len = 0;
3512
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003513 if (!led_supported) {
3514 len += sprintf(p + len, "status:\t\tnot supported\n");
3515 return len;
3516 }
3517 len += sprintf(p + len, "status:\t\tsupported\n");
3518
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003519 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003520 /* 570 */
3521 int i, status;
3522 for (i = 0; i < 8; i++) {
3523 if (!acpi_evalf(ec_handle,
3524 &status, "GLED", "dd", 1 << i))
3525 return -EIO;
3526 len += sprintf(p + len, "%d:\t\t%s\n",
3527 i, led_status(status));
3528 }
3529 }
3530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003532 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534 return len;
3535}
3536
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003537/* off, on, blink */
3538static const int led_sled_arg1[] = { 0, 1, 3 };
3539static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3540static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3541static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3542
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003543static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
3545 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003546 int led, ind, ret;
3547
3548 if (!led_supported)
3549 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003552 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return -EINVAL;
3554
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003555 if (strstr(cmd, "off")) {
3556 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003558 ind = 1;
3559 } else if (strstr(cmd, "blink")) {
3560 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 } else
3562 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003563
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003564 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003565 /* 570 */
3566 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003568 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003570 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003571 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3572 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003573 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003574 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003575 ret = ec_write(TPACPI_LED_EC_HLBL,
3576 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003577 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003578 ret = ec_write(TPACPI_LED_EC_HLCL,
3579 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003580 if (ret < 0)
3581 return ret;
3582 } else {
3583 /* all others */
3584 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3585 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 }
3589
3590 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003591}
3592
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003593static struct ibm_struct led_driver_data = {
3594 .name = "led",
3595 .read = led_read,
3596 .write = led_write,
3597};
3598
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003599/*************************************************************************
3600 * Beep subdriver
3601 */
3602
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003603TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003604
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003605static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003606{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003607 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3608
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003609 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003610
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003611 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3612 str_supported(beep_handle != NULL));
3613
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003614 return (beep_handle)? 0 : 1;
3615}
3616
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003617static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
3619 int len = 0;
3620
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003621 if (!beep_handle)
3622 len += sprintf(p + len, "status:\t\tnot supported\n");
3623 else {
3624 len += sprintf(p + len, "status:\t\tsupported\n");
3625 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
3628 return len;
3629}
3630
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003631static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
3633 char *cmd;
3634 int beep_cmd;
3635
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003636 if (!beep_handle)
3637 return -ENODEV;
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003640 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3641 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 /* beep_cmd set */
3643 } else
3644 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003645 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return -EIO;
3647 }
3648
3649 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003650}
3651
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003652static struct ibm_struct beep_driver_data = {
3653 .name = "beep",
3654 .read = beep_read,
3655 .write = beep_write,
3656};
3657
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003658/*************************************************************************
3659 * Thermal subdriver
3660 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003661
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003662enum thermal_access_mode {
3663 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3664 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3665 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3666 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3667 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3668};
3669
3670enum { /* TPACPI_THERMAL_TPEC_* */
3671 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3672 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3673 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3674};
3675
3676#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3677struct ibm_thermal_sensors_struct {
3678 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3679};
3680
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003681static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003682
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003683/* idx is zero-based */
3684static int thermal_get_sensor(int idx, s32 *value)
3685{
3686 int t;
3687 s8 tmp;
3688 char tmpi[5];
3689
3690 t = TP_EC_THERMAL_TMP0;
3691
3692 switch (thermal_read_mode) {
3693#if TPACPI_MAX_THERMAL_SENSORS >= 16
3694 case TPACPI_THERMAL_TPEC_16:
3695 if (idx >= 8 && idx <= 15) {
3696 t = TP_EC_THERMAL_TMP8;
3697 idx -= 8;
3698 }
3699 /* fallthrough */
3700#endif
3701 case TPACPI_THERMAL_TPEC_8:
3702 if (idx <= 7) {
3703 if (!acpi_ec_read(t + idx, &tmp))
3704 return -EIO;
3705 *value = tmp * 1000;
3706 return 0;
3707 }
3708 break;
3709
3710 case TPACPI_THERMAL_ACPI_UPDT:
3711 if (idx <= 7) {
3712 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3713 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3714 return -EIO;
3715 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3716 return -EIO;
3717 *value = (t - 2732) * 100;
3718 return 0;
3719 }
3720 break;
3721
3722 case TPACPI_THERMAL_ACPI_TMP07:
3723 if (idx <= 7) {
3724 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3725 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3726 return -EIO;
3727 if (t > 127 || t < -127)
3728 t = TP_EC_THERMAL_TMP_NA;
3729 *value = t * 1000;
3730 return 0;
3731 }
3732 break;
3733
3734 case TPACPI_THERMAL_NONE:
3735 default:
3736 return -ENOSYS;
3737 }
3738
3739 return -EINVAL;
3740}
3741
3742static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3743{
3744 int res, i;
3745 int n;
3746
3747 n = 8;
3748 i = 0;
3749
3750 if (!s)
3751 return -EINVAL;
3752
3753 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3754 n = 16;
3755
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003756 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003757 res = thermal_get_sensor(i, &s->temp[i]);
3758 if (res)
3759 return res;
3760 }
3761
3762 return n;
3763}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003764
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003765/* sysfs temp##_input -------------------------------------------------- */
3766
3767static ssize_t thermal_temp_input_show(struct device *dev,
3768 struct device_attribute *attr,
3769 char *buf)
3770{
3771 struct sensor_device_attribute *sensor_attr =
3772 to_sensor_dev_attr(attr);
3773 int idx = sensor_attr->index;
3774 s32 value;
3775 int res;
3776
3777 res = thermal_get_sensor(idx, &value);
3778 if (res)
3779 return res;
3780 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3781 return -ENXIO;
3782
3783 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3784}
3785
3786#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003787 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3788 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003789
3790static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3791 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3792 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3793 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3794 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3795 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3796 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3797 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3798 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3799 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3800 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3801 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3802 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3803 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3804 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3805 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3806 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3807};
3808
3809#define THERMAL_ATTRS(X) \
3810 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3811
3812static struct attribute *thermal_temp_input_attr[] = {
3813 THERMAL_ATTRS(8),
3814 THERMAL_ATTRS(9),
3815 THERMAL_ATTRS(10),
3816 THERMAL_ATTRS(11),
3817 THERMAL_ATTRS(12),
3818 THERMAL_ATTRS(13),
3819 THERMAL_ATTRS(14),
3820 THERMAL_ATTRS(15),
3821 THERMAL_ATTRS(0),
3822 THERMAL_ATTRS(1),
3823 THERMAL_ATTRS(2),
3824 THERMAL_ATTRS(3),
3825 THERMAL_ATTRS(4),
3826 THERMAL_ATTRS(5),
3827 THERMAL_ATTRS(6),
3828 THERMAL_ATTRS(7),
3829 NULL
3830};
3831
3832static const struct attribute_group thermal_temp_input16_group = {
3833 .attrs = thermal_temp_input_attr
3834};
3835
3836static const struct attribute_group thermal_temp_input8_group = {
3837 .attrs = &thermal_temp_input_attr[8]
3838};
3839
3840#undef THERMAL_SENSOR_ATTR_TEMP
3841#undef THERMAL_ATTRS
3842
3843/* --------------------------------------------------------------------- */
3844
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003845static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003846{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003847 u8 t, ta1, ta2;
3848 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003849 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003850 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003851
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003852 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3853
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003854 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003855
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003856 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003857 /*
3858 * Direct EC access mode: sensors at registers
3859 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3860 * non-implemented, thermal sensors return 0x80 when
3861 * not available
3862 */
3863
3864 ta1 = ta2 = 0;
3865 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003866 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003867 ta1 |= t;
3868 } else {
3869 ta1 = 0;
3870 break;
3871 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003872 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003873 ta2 |= t;
3874 } else {
3875 ta1 = 0;
3876 break;
3877 }
3878 }
3879 if (ta1 == 0) {
3880 /* This is sheer paranoia, but we handle it anyway */
3881 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003882 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003883 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003884 "falling back to ACPI TMPx access "
3885 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003886 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003887 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003888 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003889 "ThinkPad ACPI EC access misbehaving, "
3890 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003891 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003892 }
3893 } else {
3894 thermal_read_mode =
3895 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003896 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003897 }
3898 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003899 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3900 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003901 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003902 } else {
3903 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003904 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003905 }
3906 } else {
3907 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003908 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003909 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003910
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003911 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3912 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3913 thermal_read_mode);
3914
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003915 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003916 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003917 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003918 &thermal_temp_input16_group);
3919 if (res)
3920 return res;
3921 break;
3922 case TPACPI_THERMAL_TPEC_8:
3923 case TPACPI_THERMAL_ACPI_TMP07:
3924 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003925 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003926 &thermal_temp_input8_group);
3927 if (res)
3928 return res;
3929 break;
3930 case TPACPI_THERMAL_NONE:
3931 default:
3932 return 1;
3933 }
3934
3935 return 0;
3936}
3937
3938static void thermal_exit(void)
3939{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003940 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003941 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003942 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003943 &thermal_temp_input16_group);
3944 break;
3945 case TPACPI_THERMAL_TPEC_8:
3946 case TPACPI_THERMAL_ACPI_TMP07:
3947 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003948 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003949 &thermal_temp_input16_group);
3950 break;
3951 case TPACPI_THERMAL_NONE:
3952 default:
3953 break;
3954 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003955}
3956
3957static int thermal_read(char *p)
3958{
3959 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003960 int n, i;
3961 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003962
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003963 n = thermal_get_sensors(&t);
3964 if (unlikely(n < 0))
3965 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003966
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003967 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003968
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003969 if (n > 0) {
3970 for (i = 0; i < (n - 1); i++)
3971 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3972 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3973 } else
3974 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003975
3976 return len;
3977}
3978
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003979static struct ibm_struct thermal_driver_data = {
3980 .name = "thermal",
3981 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003982 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003983};
3984
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003985/*************************************************************************
3986 * EC Dump subdriver
3987 */
3988
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003989static u8 ecdump_regs[256];
3990
3991static int ecdump_read(char *p)
3992{
3993 int len = 0;
3994 int i, j;
3995 u8 v;
3996
3997 len += sprintf(p + len, "EC "
3998 " +00 +01 +02 +03 +04 +05 +06 +07"
3999 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4000 for (i = 0; i < 256; i += 16) {
4001 len += sprintf(p + len, "EC 0x%02x:", i);
4002 for (j = 0; j < 16; j++) {
4003 if (!acpi_ec_read(i + j, &v))
4004 break;
4005 if (v != ecdump_regs[i + j])
4006 len += sprintf(p + len, " *%02x", v);
4007 else
4008 len += sprintf(p + len, " %02x", v);
4009 ecdump_regs[i + j] = v;
4010 }
4011 len += sprintf(p + len, "\n");
4012 if (j != 16)
4013 break;
4014 }
4015
4016 /* These are way too dangerous to advertise openly... */
4017#if 0
4018 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4019 " (<offset> is 00-ff, <value> is 00-ff)\n");
4020 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4021 " (<offset> is 00-ff, <value> is 0-255)\n");
4022#endif
4023 return len;
4024}
4025
4026static int ecdump_write(char *buf)
4027{
4028 char *cmd;
4029 int i, v;
4030
4031 while ((cmd = next_cmd(&buf))) {
4032 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4033 /* i and v set */
4034 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4035 /* i and v set */
4036 } else
4037 return -EINVAL;
4038 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4039 if (!acpi_ec_write(i, v))
4040 return -EIO;
4041 } else
4042 return -EINVAL;
4043 }
4044
4045 return 0;
4046}
4047
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004048static struct ibm_struct ecdump_driver_data = {
4049 .name = "ecdump",
4050 .read = ecdump_read,
4051 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004052 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004053};
4054
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004055/*************************************************************************
4056 * Backlight/brightness subdriver
4057 */
Holger Macht8acb0252006-10-20 14:30:28 -07004058
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004059#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4060
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004061static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004062static int brightness_offset = 0x31;
4063static int brightness_mode;
4064static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4065
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004066static struct mutex brightness_mutex;
4067
4068/*
4069 * ThinkPads can read brightness from two places: EC 0x31, or
4070 * CMOS NVRAM byte 0x5E, bits 0-3.
4071 */
4072static int brightness_get(struct backlight_device *bd)
4073{
4074 u8 lec = 0, lcmos = 0, level = 0;
4075
4076 if (brightness_mode & 1) {
4077 if (!acpi_ec_read(brightness_offset, &lec))
4078 return -EIO;
4079 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4080 level = lec;
4081 };
4082 if (brightness_mode & 2) {
4083 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4084 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4085 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4086 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4087 level = lcmos;
4088 }
4089
4090 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004091 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004092 "CMOS NVRAM (%u) and EC (%u) do not agree "
4093 "on display brightness level\n",
4094 (unsigned int) lcmos,
4095 (unsigned int) lec);
4096 return -EIO;
4097 }
4098
4099 return level;
4100}
4101
4102/* May return EINTR which can always be mapped to ERESTARTSYS */
4103static int brightness_set(int value)
4104{
4105 int cmos_cmd, inc, i, res;
4106 int current_value;
4107
4108 if (value > ((tp_features.bright_16levels)? 15 : 7))
4109 return -EINVAL;
4110
4111 res = mutex_lock_interruptible(&brightness_mutex);
4112 if (res < 0)
4113 return res;
4114
4115 current_value = brightness_get(NULL);
4116 if (current_value < 0) {
4117 res = current_value;
4118 goto errout;
4119 }
4120
4121 cmos_cmd = value > current_value ?
4122 TP_CMOS_BRIGHTNESS_UP :
4123 TP_CMOS_BRIGHTNESS_DOWN;
4124 inc = (value > current_value)? 1 : -1;
4125
4126 res = 0;
4127 for (i = current_value; i != value; i += inc) {
4128 if ((brightness_mode & 2) &&
4129 issue_thinkpad_cmos_command(cmos_cmd)) {
4130 res = -EIO;
4131 goto errout;
4132 }
4133 if ((brightness_mode & 1) &&
4134 !acpi_ec_write(brightness_offset, i + inc)) {
4135 res = -EIO;
4136 goto errout;;
4137 }
4138 }
4139
4140errout:
4141 mutex_unlock(&brightness_mutex);
4142 return res;
4143}
4144
4145/* sysfs backlight class ----------------------------------------------- */
4146
4147static int brightness_update_status(struct backlight_device *bd)
4148{
4149 /* it is the backlight class's job (caller) to handle
4150 * EINTR and other errors properly */
4151 return brightness_set(
4152 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4153 bd->props.power == FB_BLANK_UNBLANK) ?
4154 bd->props.brightness : 0);
4155}
Holger Macht8acb0252006-10-20 14:30:28 -07004156
Richard Purdie599a52d2007-02-10 23:07:48 +00004157static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004158 .get_brightness = brightness_get,
4159 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004160};
4161
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004162/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004163
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004164static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4165{
4166 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4167 union acpi_object *obj;
4168 int rc;
4169
4170 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4171 obj = (union acpi_object *)buffer.pointer;
4172 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004173 printk(TPACPI_ERR "Unknown BCLL data, "
4174 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004175 rc = 0;
4176 } else {
4177 rc = obj->package.count;
4178 }
4179 } else {
4180 return 0;
4181 }
4182
4183 kfree(buffer.pointer);
4184 return rc;
4185}
4186
4187static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4188 void *context, void **rv)
4189{
4190 char name[ACPI_PATH_SEGMENT_LENGTH];
4191 struct acpi_buffer buffer = { sizeof(name), &name };
4192
4193 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4194 !strncmp("BCLL", name, sizeof(name) - 1)) {
4195 if (tpacpi_query_bcll_levels(handle) == 16) {
4196 *rv = handle;
4197 return AE_CTRL_TERMINATE;
4198 } else {
4199 return AE_OK;
4200 }
4201 } else {
4202 return AE_OK;
4203 }
4204}
4205
4206static int __init brightness_check_levels(void)
4207{
4208 int status;
4209 void *found_node = NULL;
4210
4211 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004212 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004213 }
4214 if (!vid_handle)
4215 return 0;
4216
4217 /* Search for a BCLL package with 16 levels */
4218 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004219 brightness_find_bcll, NULL,
4220 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004221
4222 return (ACPI_SUCCESS(status) && found_node != NULL);
4223}
4224
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004225static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4226 void *context, void **rv)
4227{
4228 char name[ACPI_PATH_SEGMENT_LENGTH];
4229 struct acpi_buffer buffer = { sizeof(name), &name };
4230
4231 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4232 !strncmp("_BCL", name, sizeof(name) - 1)) {
4233 *rv = handle;
4234 return AE_CTRL_TERMINATE;
4235 } else {
4236 return AE_OK;
4237 }
4238}
4239
4240static int __init brightness_check_std_acpi_support(void)
4241{
4242 int status;
4243 void *found_node = NULL;
4244
4245 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004246 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004247 }
4248 if (!vid_handle)
4249 return 0;
4250
4251 /* Search for a _BCL method, but don't execute it */
4252 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004253 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004254
4255 return (ACPI_SUCCESS(status) && found_node != NULL);
4256}
4257
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004258static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004259{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004260 int b;
4261
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004262 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4263
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004264 mutex_init(&brightness_mutex);
4265
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004266 if (!brightness_enable) {
4267 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004268 "brightness support disabled by "
4269 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004270 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004271 } else if (brightness_enable > 1) {
4272 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004273 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004274 "standard ACPI backlight interface "
4275 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004276 return 1;
4277 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004278 }
4279
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004280 if (!brightness_mode) {
4281 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4282 brightness_mode = 2;
4283 else
4284 brightness_mode = 3;
4285
4286 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4287 brightness_mode);
4288 }
4289
4290 if (brightness_mode > 3)
4291 return -EINVAL;
4292
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004293 tp_features.bright_16levels =
4294 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4295 brightness_check_levels();
4296
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004297 b = brightness_get(NULL);
4298 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004299 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004300
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004301 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004302 printk(TPACPI_INFO
4303 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004304
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004305 ibm_backlight_device = backlight_device_register(
4306 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4307 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004308 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004309 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004310 return PTR_ERR(ibm_backlight_device);
4311 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004312 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004313
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004314 ibm_backlight_device->props.max_brightness =
4315 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004316 ibm_backlight_device->props.brightness = b;
4317 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004318
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004319 return 0;
4320}
4321
4322static void brightness_exit(void)
4323{
4324 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004325 vdbg_printk(TPACPI_DBG_EXIT,
4326 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004327 backlight_device_unregister(ibm_backlight_device);
4328 ibm_backlight_device = NULL;
4329 }
4330}
4331
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004332static int brightness_read(char *p)
4333{
4334 int len = 0;
4335 int level;
4336
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004337 level = brightness_get(NULL);
4338 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004339 len += sprintf(p + len, "level:\t\tunreadable\n");
4340 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004341 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004342 len += sprintf(p + len, "commands:\tup, down\n");
4343 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004344 " (<level> is 0-%d)\n",
4345 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004346 }
4347
4348 return len;
4349}
4350
4351static int brightness_write(char *buf)
4352{
4353 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004354 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004355 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004356 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004357
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004358 level = brightness_get(NULL);
4359 if (level < 0)
4360 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004361
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004362 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004363 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004364 if (level < max_level)
4365 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004366 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004367 if (level > 0)
4368 level--;
4369 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4370 level >= 0 && level <= max_level) {
4371 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004372 } else
4373 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004374 }
4375
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004376 /*
4377 * Now we know what the final level should be, so we try to set it.
4378 * Doing it this way makes the syscall restartable in case of EINTR
4379 */
4380 rc = brightness_set(level);
4381 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004382}
4383
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004384static struct ibm_struct brightness_driver_data = {
4385 .name = "brightness",
4386 .read = brightness_read,
4387 .write = brightness_write,
4388 .exit = brightness_exit,
4389};
4390
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004391/*************************************************************************
4392 * Volume subdriver
4393 */
4394
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004395static int volume_offset = 0x30;
4396
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004397static int volume_read(char *p)
4398{
4399 int len = 0;
4400 u8 level;
4401
4402 if (!acpi_ec_read(volume_offset, &level)) {
4403 len += sprintf(p + len, "level:\t\tunreadable\n");
4404 } else {
4405 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4406 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4407 len += sprintf(p + len, "commands:\tup, down, mute\n");
4408 len += sprintf(p + len, "commands:\tlevel <level>"
4409 " (<level> is 0-15)\n");
4410 }
4411
4412 return len;
4413}
4414
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004415static int volume_write(char *buf)
4416{
4417 int cmos_cmd, inc, i;
4418 u8 level, mute;
4419 int new_level, new_mute;
4420 char *cmd;
4421
4422 while ((cmd = next_cmd(&buf))) {
4423 if (!acpi_ec_read(volume_offset, &level))
4424 return -EIO;
4425 new_mute = mute = level & 0x40;
4426 new_level = level = level & 0xf;
4427
4428 if (strlencmp(cmd, "up") == 0) {
4429 if (mute)
4430 new_mute = 0;
4431 else
4432 new_level = level == 15 ? 15 : level + 1;
4433 } else if (strlencmp(cmd, "down") == 0) {
4434 if (mute)
4435 new_mute = 0;
4436 else
4437 new_level = level == 0 ? 0 : level - 1;
4438 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4439 new_level >= 0 && new_level <= 15) {
4440 /* new_level set */
4441 } else if (strlencmp(cmd, "mute") == 0) {
4442 new_mute = 0x40;
4443 } else
4444 return -EINVAL;
4445
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004446 if (new_level != level) {
4447 /* mute doesn't change */
4448
4449 cmos_cmd = (new_level > level) ?
4450 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004451 inc = new_level > level ? 1 : -1;
4452
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004453 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004454 !acpi_ec_write(volume_offset, level)))
4455 return -EIO;
4456
4457 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004458 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004459 !acpi_ec_write(volume_offset, i + inc))
4460 return -EIO;
4461
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004462 if (mute &&
4463 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4464 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004465 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004466 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004467 }
4468
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004469 if (new_mute != mute) {
4470 /* level doesn't change */
4471
4472 cmos_cmd = (new_mute) ?
4473 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004474
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004475 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004476 !acpi_ec_write(volume_offset, level + new_mute))
4477 return -EIO;
4478 }
4479 }
4480
4481 return 0;
4482}
4483
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004484static struct ibm_struct volume_driver_data = {
4485 .name = "volume",
4486 .read = volume_read,
4487 .write = volume_write,
4488};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004489
4490/*************************************************************************
4491 * Fan subdriver
4492 */
4493
4494/*
4495 * FAN ACCESS MODES
4496 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004497 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004498 * ACPI GFAN method: returns fan level
4499 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004500 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004501 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004502 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004503 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004504 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4505 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004506 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4507 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004508 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004509 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004510 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4511 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004512 *
4513 * Fan speed changes of any sort (including those caused by the
4514 * disengaged mode) are usually done slowly by the firmware as the
4515 * maximum ammount of fan duty cycle change per second seems to be
4516 * limited.
4517 *
4518 * Reading is not available if GFAN exists.
4519 * Writing is not available if SFAN exists.
4520 *
4521 * Bits
4522 * 7 automatic mode engaged;
4523 * (default operation mode of the ThinkPad)
4524 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004525 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004526 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004527 * the tachometer while the fan controller ramps up
4528 * the speed (which can take up to a few *minutes*).
4529 * Speeds up fan to 100% duty-cycle, which is far above
4530 * the standard RPM levels. It is not impossible that
4531 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004532 * 5-3 unused in some models. Extra bits for fan level
4533 * in others, but still useless as all values above
4534 * 7 map to the same speed as level 7 in these models.
4535 * 2-0 fan level (0..7 usually)
4536 * 0x00 = stop
4537 * 0x07 = max (set when temperatures critical)
4538 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004539 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004540 *
4541 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4542 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4543 * does so, its initial value is meaningless (0x07).
4544 *
4545 * For firmware bugs, refer to:
4546 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4547 *
4548 * ----
4549 *
4550 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4551 * Main fan tachometer reading (in RPM)
4552 *
4553 * This register is present on all ThinkPads with a new-style EC, and
4554 * it is known not to be present on the A21m/e, and T22, as there is
4555 * something else in offset 0x84 according to the ACPI DSDT. Other
4556 * ThinkPads from this same time period (and earlier) probably lack the
4557 * tachometer as well.
4558 *
4559 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4560 * was never fixed by IBM to report the EC firmware version string
4561 * probably support the tachometer (like the early X models), so
4562 * detecting it is quite hard. We need more data to know for sure.
4563 *
4564 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4565 * might result.
4566 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004567 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4568 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004569 *
4570 * For firmware bugs, refer to:
4571 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4572 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004573 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004574 * ThinkPad X31, X40, X41. Not available in the X60.
4575 *
4576 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4577 * high speed. ACPI DSDT seems to map these three speeds to levels
4578 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4579 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4580 *
4581 * The speeds are stored on handles
4582 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4583 *
4584 * There are three default speed sets, acessible as handles:
4585 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4586 *
4587 * ACPI DSDT switches which set is in use depending on various
4588 * factors.
4589 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004590 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004591 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4592 * but the ACPI tables just mention level 7.
4593 */
4594
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004595enum { /* Fan control constants */
4596 fan_status_offset = 0x2f, /* EC register 0x2f */
4597 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4598 * 0x84 must be read before 0x85 */
4599
4600 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4601 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4602
4603 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4604};
4605
4606enum fan_status_access_mode {
4607 TPACPI_FAN_NONE = 0, /* No fan status or control */
4608 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4609 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4610};
4611
4612enum fan_control_access_mode {
4613 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4614 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4615 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4616 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4617};
4618
4619enum fan_control_commands {
4620 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4621 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4622 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4623 * and also watchdog cmd */
4624};
4625
4626static int fan_control_allowed;
4627
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004628static enum fan_status_access_mode fan_status_access_mode;
4629static enum fan_control_access_mode fan_control_access_mode;
4630static enum fan_control_commands fan_control_commands;
4631
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004632static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004633static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004634static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004635
4636static struct mutex fan_mutex;
4637
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004638static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004639static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004640
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004641TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4642TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004643 "\\FSPD", /* 600e/x, 770e, 770x */
4644 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004645TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004646 "JFNS", /* 770x-JL */
4647 ); /* all others */
4648
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004649/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004650 * Call with fan_mutex held
4651 */
4652static void fan_update_desired_level(u8 status)
4653{
4654 if ((status &
4655 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4656 if (status > 7)
4657 fan_control_desired_level = 7;
4658 else
4659 fan_control_desired_level = status;
4660 }
4661}
4662
4663static int fan_get_status(u8 *status)
4664{
4665 u8 s;
4666
4667 /* TODO:
4668 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4669
4670 switch (fan_status_access_mode) {
4671 case TPACPI_FAN_RD_ACPI_GFAN:
4672 /* 570, 600e/x, 770e, 770x */
4673
4674 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4675 return -EIO;
4676
4677 if (likely(status))
4678 *status = s & 0x07;
4679
4680 break;
4681
4682 case TPACPI_FAN_RD_TPEC:
4683 /* all except 570, 600e/x, 770e, 770x */
4684 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4685 return -EIO;
4686
4687 if (likely(status))
4688 *status = s;
4689
4690 break;
4691
4692 default:
4693 return -ENXIO;
4694 }
4695
4696 return 0;
4697}
4698
4699static int fan_get_status_safe(u8 *status)
4700{
4701 int rc;
4702 u8 s;
4703
4704 if (mutex_lock_interruptible(&fan_mutex))
4705 return -ERESTARTSYS;
4706 rc = fan_get_status(&s);
4707 if (!rc)
4708 fan_update_desired_level(s);
4709 mutex_unlock(&fan_mutex);
4710
4711 if (status)
4712 *status = s;
4713
4714 return rc;
4715}
4716
4717static int fan_get_speed(unsigned int *speed)
4718{
4719 u8 hi, lo;
4720
4721 switch (fan_status_access_mode) {
4722 case TPACPI_FAN_RD_TPEC:
4723 /* all except 570, 600e/x, 770e, 770x */
4724 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4725 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4726 return -EIO;
4727
4728 if (likely(speed))
4729 *speed = (hi << 8) | lo;
4730
4731 break;
4732
4733 default:
4734 return -ENXIO;
4735 }
4736
4737 return 0;
4738}
4739
4740static int fan_set_level(int level)
4741{
4742 if (!fan_control_allowed)
4743 return -EPERM;
4744
4745 switch (fan_control_access_mode) {
4746 case TPACPI_FAN_WR_ACPI_SFAN:
4747 if (level >= 0 && level <= 7) {
4748 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4749 return -EIO;
4750 } else
4751 return -EINVAL;
4752 break;
4753
4754 case TPACPI_FAN_WR_ACPI_FANS:
4755 case TPACPI_FAN_WR_TPEC:
4756 if ((level != TP_EC_FAN_AUTO) &&
4757 (level != TP_EC_FAN_FULLSPEED) &&
4758 ((level < 0) || (level > 7)))
4759 return -EINVAL;
4760
4761 /* safety net should the EC not support AUTO
4762 * or FULLSPEED mode bits and just ignore them */
4763 if (level & TP_EC_FAN_FULLSPEED)
4764 level |= 7; /* safety min speed 7 */
4765 else if (level & TP_EC_FAN_FULLSPEED)
4766 level |= 4; /* safety min speed 4 */
4767
4768 if (!acpi_ec_write(fan_status_offset, level))
4769 return -EIO;
4770 else
4771 tp_features.fan_ctrl_status_undef = 0;
4772 break;
4773
4774 default:
4775 return -ENXIO;
4776 }
4777 return 0;
4778}
4779
4780static int fan_set_level_safe(int level)
4781{
4782 int rc;
4783
4784 if (!fan_control_allowed)
4785 return -EPERM;
4786
4787 if (mutex_lock_interruptible(&fan_mutex))
4788 return -ERESTARTSYS;
4789
4790 if (level == TPACPI_FAN_LAST_LEVEL)
4791 level = fan_control_desired_level;
4792
4793 rc = fan_set_level(level);
4794 if (!rc)
4795 fan_update_desired_level(level);
4796
4797 mutex_unlock(&fan_mutex);
4798 return rc;
4799}
4800
4801static int fan_set_enable(void)
4802{
4803 u8 s;
4804 int rc;
4805
4806 if (!fan_control_allowed)
4807 return -EPERM;
4808
4809 if (mutex_lock_interruptible(&fan_mutex))
4810 return -ERESTARTSYS;
4811
4812 switch (fan_control_access_mode) {
4813 case TPACPI_FAN_WR_ACPI_FANS:
4814 case TPACPI_FAN_WR_TPEC:
4815 rc = fan_get_status(&s);
4816 if (rc < 0)
4817 break;
4818
4819 /* Don't go out of emergency fan mode */
4820 if (s != 7) {
4821 s &= 0x07;
4822 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4823 }
4824
4825 if (!acpi_ec_write(fan_status_offset, s))
4826 rc = -EIO;
4827 else {
4828 tp_features.fan_ctrl_status_undef = 0;
4829 rc = 0;
4830 }
4831 break;
4832
4833 case TPACPI_FAN_WR_ACPI_SFAN:
4834 rc = fan_get_status(&s);
4835 if (rc < 0)
4836 break;
4837
4838 s &= 0x07;
4839
4840 /* Set fan to at least level 4 */
4841 s |= 4;
4842
4843 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004844 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004845 else
4846 rc = 0;
4847 break;
4848
4849 default:
4850 rc = -ENXIO;
4851 }
4852
4853 mutex_unlock(&fan_mutex);
4854 return rc;
4855}
4856
4857static int fan_set_disable(void)
4858{
4859 int rc;
4860
4861 if (!fan_control_allowed)
4862 return -EPERM;
4863
4864 if (mutex_lock_interruptible(&fan_mutex))
4865 return -ERESTARTSYS;
4866
4867 rc = 0;
4868 switch (fan_control_access_mode) {
4869 case TPACPI_FAN_WR_ACPI_FANS:
4870 case TPACPI_FAN_WR_TPEC:
4871 if (!acpi_ec_write(fan_status_offset, 0x00))
4872 rc = -EIO;
4873 else {
4874 fan_control_desired_level = 0;
4875 tp_features.fan_ctrl_status_undef = 0;
4876 }
4877 break;
4878
4879 case TPACPI_FAN_WR_ACPI_SFAN:
4880 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4881 rc = -EIO;
4882 else
4883 fan_control_desired_level = 0;
4884 break;
4885
4886 default:
4887 rc = -ENXIO;
4888 }
4889
4890
4891 mutex_unlock(&fan_mutex);
4892 return rc;
4893}
4894
4895static int fan_set_speed(int speed)
4896{
4897 int rc;
4898
4899 if (!fan_control_allowed)
4900 return -EPERM;
4901
4902 if (mutex_lock_interruptible(&fan_mutex))
4903 return -ERESTARTSYS;
4904
4905 rc = 0;
4906 switch (fan_control_access_mode) {
4907 case TPACPI_FAN_WR_ACPI_FANS:
4908 if (speed >= 0 && speed <= 65535) {
4909 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4910 speed, speed, speed))
4911 rc = -EIO;
4912 } else
4913 rc = -EINVAL;
4914 break;
4915
4916 default:
4917 rc = -ENXIO;
4918 }
4919
4920 mutex_unlock(&fan_mutex);
4921 return rc;
4922}
4923
4924static void fan_watchdog_reset(void)
4925{
4926 static int fan_watchdog_active;
4927
4928 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4929 return;
4930
4931 if (fan_watchdog_active)
4932 cancel_delayed_work(&fan_watchdog_task);
4933
4934 if (fan_watchdog_maxinterval > 0 &&
4935 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4936 fan_watchdog_active = 1;
4937 if (!schedule_delayed_work(&fan_watchdog_task,
4938 msecs_to_jiffies(fan_watchdog_maxinterval
4939 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004940 printk(TPACPI_ERR
4941 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004942 "watchdog will not trigger\n");
4943 }
4944 } else
4945 fan_watchdog_active = 0;
4946}
4947
4948static void fan_watchdog_fire(struct work_struct *ignored)
4949{
4950 int rc;
4951
4952 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4953 return;
4954
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004955 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004956 rc = fan_set_enable();
4957 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004958 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004959 "will try again later...\n", -rc);
4960 /* reschedule for later */
4961 fan_watchdog_reset();
4962 }
4963}
4964
4965/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004966 * SYSFS fan layout: hwmon compatible (device)
4967 *
4968 * pwm*_enable:
4969 * 0: "disengaged" mode
4970 * 1: manual mode
4971 * 2: native EC "auto" mode (recommended, hardware default)
4972 *
4973 * pwm*: set speed in manual mode, ignored otherwise.
4974 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4975 * interpolation.
4976 *
4977 * fan*_input: tachometer reading, RPM
4978 *
4979 *
4980 * SYSFS fan layout: extensions
4981 *
4982 * fan_watchdog (driver):
4983 * fan watchdog interval in seconds, 0 disables (default), max 120
4984 */
4985
4986/* sysfs fan pwm1_enable ----------------------------------------------- */
4987static ssize_t fan_pwm1_enable_show(struct device *dev,
4988 struct device_attribute *attr,
4989 char *buf)
4990{
4991 int res, mode;
4992 u8 status;
4993
4994 res = fan_get_status_safe(&status);
4995 if (res)
4996 return res;
4997
4998 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4999 if (status != fan_control_initial_status) {
5000 tp_features.fan_ctrl_status_undef = 0;
5001 } else {
5002 /* Return most likely status. In fact, it
5003 * might be the only possible status */
5004 status = TP_EC_FAN_AUTO;
5005 }
5006 }
5007
5008 if (status & TP_EC_FAN_FULLSPEED) {
5009 mode = 0;
5010 } else if (status & TP_EC_FAN_AUTO) {
5011 mode = 2;
5012 } else
5013 mode = 1;
5014
5015 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5016}
5017
5018static ssize_t fan_pwm1_enable_store(struct device *dev,
5019 struct device_attribute *attr,
5020 const char *buf, size_t count)
5021{
5022 unsigned long t;
5023 int res, level;
5024
5025 if (parse_strtoul(buf, 2, &t))
5026 return -EINVAL;
5027
5028 switch (t) {
5029 case 0:
5030 level = TP_EC_FAN_FULLSPEED;
5031 break;
5032 case 1:
5033 level = TPACPI_FAN_LAST_LEVEL;
5034 break;
5035 case 2:
5036 level = TP_EC_FAN_AUTO;
5037 break;
5038 case 3:
5039 /* reserved for software-controlled auto mode */
5040 return -ENOSYS;
5041 default:
5042 return -EINVAL;
5043 }
5044
5045 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005046 if (res == -ENXIO)
5047 return -EINVAL;
5048 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005049 return res;
5050
5051 fan_watchdog_reset();
5052
5053 return count;
5054}
5055
5056static struct device_attribute dev_attr_fan_pwm1_enable =
5057 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5058 fan_pwm1_enable_show, fan_pwm1_enable_store);
5059
5060/* sysfs fan pwm1 ------------------------------------------------------ */
5061static ssize_t fan_pwm1_show(struct device *dev,
5062 struct device_attribute *attr,
5063 char *buf)
5064{
5065 int res;
5066 u8 status;
5067
5068 res = fan_get_status_safe(&status);
5069 if (res)
5070 return res;
5071
5072 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5073 if (status != fan_control_initial_status) {
5074 tp_features.fan_ctrl_status_undef = 0;
5075 } else {
5076 status = TP_EC_FAN_AUTO;
5077 }
5078 }
5079
5080 if ((status &
5081 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5082 status = fan_control_desired_level;
5083
5084 if (status > 7)
5085 status = 7;
5086
5087 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5088}
5089
5090static ssize_t fan_pwm1_store(struct device *dev,
5091 struct device_attribute *attr,
5092 const char *buf, size_t count)
5093{
5094 unsigned long s;
5095 int rc;
5096 u8 status, newlevel;
5097
5098 if (parse_strtoul(buf, 255, &s))
5099 return -EINVAL;
5100
5101 /* scale down from 0-255 to 0-7 */
5102 newlevel = (s >> 5) & 0x07;
5103
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005104 if (mutex_lock_interruptible(&fan_mutex))
5105 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005106
5107 rc = fan_get_status(&status);
5108 if (!rc && (status &
5109 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5110 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005111 if (rc == -ENXIO)
5112 rc = -EINVAL;
5113 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005114 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005115 fan_watchdog_reset();
5116 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005117 }
5118
5119 mutex_unlock(&fan_mutex);
5120 return (rc)? rc : count;
5121}
5122
5123static struct device_attribute dev_attr_fan_pwm1 =
5124 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5125 fan_pwm1_show, fan_pwm1_store);
5126
5127/* sysfs fan fan1_input ------------------------------------------------ */
5128static ssize_t fan_fan1_input_show(struct device *dev,
5129 struct device_attribute *attr,
5130 char *buf)
5131{
5132 int res;
5133 unsigned int speed;
5134
5135 res = fan_get_speed(&speed);
5136 if (res < 0)
5137 return res;
5138
5139 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5140}
5141
5142static struct device_attribute dev_attr_fan_fan1_input =
5143 __ATTR(fan1_input, S_IRUGO,
5144 fan_fan1_input_show, NULL);
5145
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005146/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005147static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5148 char *buf)
5149{
5150 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5151}
5152
5153static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5154 const char *buf, size_t count)
5155{
5156 unsigned long t;
5157
5158 if (parse_strtoul(buf, 120, &t))
5159 return -EINVAL;
5160
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005161 if (!fan_control_allowed)
5162 return -EPERM;
5163
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005164 fan_watchdog_maxinterval = t;
5165 fan_watchdog_reset();
5166
5167 return count;
5168}
5169
5170static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5171 fan_fan_watchdog_show, fan_fan_watchdog_store);
5172
5173/* --------------------------------------------------------------------- */
5174static struct attribute *fan_attributes[] = {
5175 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5176 &dev_attr_fan_fan1_input.attr,
5177 NULL
5178};
5179
5180static const struct attribute_group fan_attr_group = {
5181 .attrs = fan_attributes,
5182};
5183
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005184static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005185{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005186 int rc;
5187
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005188 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5189
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005190 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005191 fan_status_access_mode = TPACPI_FAN_NONE;
5192 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005193 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005194 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005195 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005196 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005197
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005198 TPACPI_ACPIHANDLE_INIT(fans);
5199 TPACPI_ACPIHANDLE_INIT(gfan);
5200 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005201
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005202 if (gfan_handle) {
5203 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005204 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005205 } else {
5206 /* all other ThinkPads: note that even old-style
5207 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005208 if (likely(acpi_ec_read(fan_status_offset,
5209 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005210 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005211
5212 /* In some ThinkPads, neither the EC nor the ACPI
5213 * DSDT initialize the fan status, and it ends up
5214 * being set to 0x07 when it *could* be either
5215 * 0x07 or 0x80.
5216 *
5217 * Enable for TP-1Y (T43), TP-78 (R51e),
5218 * TP-76 (R52), TP-70 (T43, R52), which are known
5219 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005220 if (fan_control_initial_status == 0x07) {
5221 switch (thinkpad_id.ec_model) {
5222 case 0x5931: /* TP-1Y */
5223 case 0x3837: /* TP-78 */
5224 case 0x3637: /* TP-76 */
5225 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005226 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005227 "fan_init: initial fan status "
5228 "is unknown, assuming it is "
5229 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005230 tp_features.fan_ctrl_status_undef = 1;
5231 ;;
5232 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005233 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005234 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005235 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005236 "ThinkPad ACPI EC access misbehaving, "
5237 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005238 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005239 }
5240 }
5241
5242 if (sfan_handle) {
5243 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005244 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005245 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005246 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005247 } else {
5248 if (!gfan_handle) {
5249 /* gfan without sfan means no fan control */
5250 /* all other models implement TP EC 0x2f control */
5251
5252 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005253 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005254 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005255 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005256 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005257 TPACPI_FAN_CMD_SPEED |
5258 TPACPI_FAN_CMD_LEVEL |
5259 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005260 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005261 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005262 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005263 TPACPI_FAN_CMD_LEVEL |
5264 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005265 }
5266 }
5267 }
5268
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005269 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5270 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5271 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5272 fan_status_access_mode, fan_control_access_mode);
5273
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005274 /* fan control master switch */
5275 if (!fan_control_allowed) {
5276 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5277 fan_control_commands = 0;
5278 dbg_printk(TPACPI_DBG_INIT,
5279 "fan control features disabled by parameter\n");
5280 }
5281
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005282 /* update fan_control_desired_level */
5283 if (fan_status_access_mode != TPACPI_FAN_NONE)
5284 fan_get_status_safe(NULL);
5285
5286 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5287 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005288 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005289 &fan_attr_group);
5290 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005291 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005292 &driver_attr_fan_watchdog);
5293 if (rc < 0)
5294 return rc;
5295 return 0;
5296 } else
5297 return 1;
5298}
5299
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005300static void fan_exit(void)
5301{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005302 vdbg_printk(TPACPI_DBG_EXIT,
5303 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005304
5305 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005306 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005307 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5308 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005309
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005310 cancel_delayed_work(&fan_watchdog_task);
5311 flush_scheduled_work();
5312}
5313
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005314static int fan_read(char *p)
5315{
5316 int len = 0;
5317 int rc;
5318 u8 status;
5319 unsigned int speed = 0;
5320
5321 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005322 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005323 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005324 rc = fan_get_status_safe(&status);
5325 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005326 return rc;
5327
5328 len += sprintf(p + len, "status:\t\t%s\n"
5329 "level:\t\t%d\n",
5330 (status != 0) ? "enabled" : "disabled", status);
5331 break;
5332
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005333 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005334 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005335 rc = fan_get_status_safe(&status);
5336 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005337 return rc;
5338
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005339 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005340 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005341 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005342 else
5343 /* Return most likely status. In fact, it
5344 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005345 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005346 }
5347
5348 len += sprintf(p + len, "status:\t\t%s\n",
5349 (status != 0) ? "enabled" : "disabled");
5350
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005351 rc = fan_get_speed(&speed);
5352 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005353 return rc;
5354
5355 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5356
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005357 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005358 /* Disengaged mode takes precedence */
5359 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005360 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005361 len += sprintf(p + len, "level:\t\tauto\n");
5362 else
5363 len += sprintf(p + len, "level:\t\t%d\n", status);
5364 break;
5365
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005366 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005367 default:
5368 len += sprintf(p + len, "status:\t\tnot supported\n");
5369 }
5370
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005371 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005372 len += sprintf(p + len, "commands:\tlevel <level>");
5373
5374 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005375 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005376 len += sprintf(p + len, " (<level> is 0-7)\n");
5377 break;
5378
5379 default:
5380 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005381 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005382 break;
5383 }
5384 }
5385
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005386 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005387 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005388 "commands:\twatchdog <timeout> (<timeout> "
5389 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005390
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005391 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005392 len += sprintf(p + len, "commands:\tspeed <speed>"
5393 " (<speed> is 0-65535)\n");
5394
5395 return len;
5396}
5397
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005398static int fan_write_cmd_level(const char *cmd, int *rc)
5399{
5400 int level;
5401
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005402 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005403 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005404 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005405 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005406 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005407 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005408 return 0;
5409
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005410 *rc = fan_set_level_safe(level);
5411 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005412 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005413 "access mode %d", fan_control_access_mode);
5414
5415 return 1;
5416}
5417
5418static int fan_write_cmd_enable(const char *cmd, int *rc)
5419{
5420 if (strlencmp(cmd, "enable") != 0)
5421 return 0;
5422
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005423 *rc = fan_set_enable();
5424 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005425 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005426 "access mode %d", fan_control_access_mode);
5427
5428 return 1;
5429}
5430
5431static int fan_write_cmd_disable(const char *cmd, int *rc)
5432{
5433 if (strlencmp(cmd, "disable") != 0)
5434 return 0;
5435
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005436 *rc = fan_set_disable();
5437 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005438 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005439 "access mode %d", fan_control_access_mode);
5440
5441 return 1;
5442}
5443
5444static int fan_write_cmd_speed(const char *cmd, int *rc)
5445{
5446 int speed;
5447
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005448 /* TODO:
5449 * Support speed <low> <medium> <high> ? */
5450
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005451 if (sscanf(cmd, "speed %d", &speed) != 1)
5452 return 0;
5453
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005454 *rc = fan_set_speed(speed);
5455 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005456 printk(TPACPI_ERR "speed 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
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005462static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5463{
5464 int interval;
5465
5466 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5467 return 0;
5468
5469 if (interval < 0 || interval > 120)
5470 *rc = -EINVAL;
5471 else
5472 fan_watchdog_maxinterval = interval;
5473
5474 return 1;
5475}
5476
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005477static int fan_write(char *buf)
5478{
5479 char *cmd;
5480 int rc = 0;
5481
5482 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005483 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005484 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005485 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005486 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005487 fan_write_cmd_disable(cmd, &rc) ||
5488 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005489 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005490 fan_write_cmd_speed(cmd, &rc))
5491 )
5492 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005493 else if (!rc)
5494 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005495 }
5496
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005497 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005498}
5499
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005500static struct ibm_struct fan_driver_data = {
5501 .name = "fan",
5502 .read = fan_read,
5503 .write = fan_write,
5504 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005505};
5506
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005507/****************************************************************************
5508 ****************************************************************************
5509 *
5510 * Infrastructure
5511 *
5512 ****************************************************************************
5513 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005515/* sysfs name ---------------------------------------------------------- */
5516static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5517 struct device_attribute *attr,
5518 char *buf)
5519{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005520 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005521}
5522
5523static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5524 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5525
5526/* --------------------------------------------------------------------- */
5527
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005528/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005529static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005530
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005531/*
5532 * Module and infrastructure proble, init and exit handling
5533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005535static int force_load;
5536
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005537#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005538static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005539{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005540 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005541
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005542 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005543}
5544#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5545
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005546static void ibm_exit(struct ibm_struct *ibm)
5547{
5548 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5549
5550 list_del_init(&ibm->all_drivers);
5551
5552 if (ibm->flags.acpi_notify_installed) {
5553 dbg_printk(TPACPI_DBG_EXIT,
5554 "%s: acpi_remove_notify_handler\n", ibm->name);
5555 BUG_ON(!ibm->acpi);
5556 acpi_remove_notify_handler(*ibm->acpi->handle,
5557 ibm->acpi->type,
5558 dispatch_acpi_notify);
5559 ibm->flags.acpi_notify_installed = 0;
5560 ibm->flags.acpi_notify_installed = 0;
5561 }
5562
5563 if (ibm->flags.proc_created) {
5564 dbg_printk(TPACPI_DBG_EXIT,
5565 "%s: remove_proc_entry\n", ibm->name);
5566 remove_proc_entry(ibm->name, proc_dir);
5567 ibm->flags.proc_created = 0;
5568 }
5569
5570 if (ibm->flags.acpi_driver_registered) {
5571 dbg_printk(TPACPI_DBG_EXIT,
5572 "%s: acpi_bus_unregister_driver\n", ibm->name);
5573 BUG_ON(!ibm->acpi);
5574 acpi_bus_unregister_driver(ibm->acpi->driver);
5575 kfree(ibm->acpi->driver);
5576 ibm->acpi->driver = NULL;
5577 ibm->flags.acpi_driver_registered = 0;
5578 }
5579
5580 if (ibm->flags.init_called && ibm->exit) {
5581 ibm->exit();
5582 ibm->flags.init_called = 0;
5583 }
5584
5585 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5586}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005587
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005588static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
5590 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005591 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 struct proc_dir_entry *entry;
5593
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005594 BUG_ON(ibm == NULL);
5595
5596 INIT_LIST_HEAD(&ibm->all_drivers);
5597
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005598 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 return 0;
5600
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005601 dbg_printk(TPACPI_DBG_INIT,
5602 "probing for %s\n", ibm->name);
5603
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005604 if (iibm->init) {
5605 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005606 if (ret > 0)
5607 return 0; /* probe failed */
5608 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005610
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005611 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 }
5613
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005614 if (ibm->acpi) {
5615 if (ibm->acpi->hid) {
5616 ret = register_tpacpi_subdriver(ibm);
5617 if (ret)
5618 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005619 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005620
5621 if (ibm->acpi->notify) {
5622 ret = setup_acpi_notify(ibm);
5623 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005624 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005625 ibm->name);
5626 ret = 0;
5627 goto err_out;
5628 }
5629 if (ret < 0)
5630 goto err_out;
5631 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005632 }
5633
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005634 dbg_printk(TPACPI_DBG_INIT,
5635 "%s installed\n", ibm->name);
5636
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005637 if (ibm->read) {
5638 entry = create_proc_entry(ibm->name,
5639 S_IFREG | S_IRUGO | S_IWUSR,
5640 proc_dir);
5641 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005642 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005643 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005644 ret = -ENODEV;
5645 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005646 }
5647 entry->owner = THIS_MODULE;
5648 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005649 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005650 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005651 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005652 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005655 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005658
5659err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005660 dbg_printk(TPACPI_DBG_INIT,
5661 "%s: at error exit path with result %d\n",
5662 ibm->name, ret);
5663
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005664 ibm_exit(ibm);
5665 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666}
5667
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005668/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005670static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005671{
Jeff Garzik18552562007-10-03 15:15:40 -04005672 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005673 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005674
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005675 if (!tp)
5676 return;
5677
5678 memset(tp, 0, sizeof(*tp));
5679
5680 if (dmi_name_in_vendors("IBM"))
5681 tp->vendor = PCI_VENDOR_ID_IBM;
5682 else if (dmi_name_in_vendors("LENOVO"))
5683 tp->vendor = PCI_VENDOR_ID_LENOVO;
5684 else
5685 return;
5686
5687 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5688 GFP_KERNEL);
5689 if (!tp->bios_version_str)
5690 return;
5691 tp->bios_model = tp->bios_version_str[0]
5692 | (tp->bios_version_str[1] << 8);
5693
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005694 /*
5695 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5696 * X32 or newer, all Z series; Some models must have an
5697 * up-to-date BIOS or they will not be detected.
5698 *
5699 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5700 */
5701 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005702 if (sscanf(dev->name,
5703 "IBM ThinkPad Embedded Controller -[%17c",
5704 ec_fw_string) == 1) {
5705 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5706 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005707
5708 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5709 tp->ec_model = ec_fw_string[0]
5710 | (ec_fw_string[1] << 8);
5711 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005712 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005713 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005714
5715 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5716 GFP_KERNEL);
5717 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5718 kfree(tp->model_str);
5719 tp->model_str = NULL;
5720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721}
5722
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005723static int __init probe_for_thinkpad(void)
5724{
5725 int is_thinkpad;
5726
5727 if (acpi_disabled)
5728 return -ENODEV;
5729
5730 /*
5731 * Non-ancient models have better DMI tagging, but very old models
5732 * don't.
5733 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005734 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005735
5736 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005737 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005738 if (!ec_handle) {
5739 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005740 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005741 "Not yet supported ThinkPad detected!\n");
5742 return -ENODEV;
5743 }
5744
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005745 /*
5746 * Risks a regression on very old machines, but reduces potential
5747 * false positives a damn great deal
5748 */
5749 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005750 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005751
5752 if (!is_thinkpad && !force_load)
5753 return -ENODEV;
5754
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005755 return 0;
5756}
5757
5758
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005759/* Module init, exit, parameters */
5760
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005761static struct ibm_init_struct ibms_init[] __initdata = {
5762 {
5763 .init = thinkpad_acpi_driver_init,
5764 .data = &thinkpad_acpi_driver_data,
5765 },
5766 {
5767 .init = hotkey_init,
5768 .data = &hotkey_driver_data,
5769 },
5770 {
5771 .init = bluetooth_init,
5772 .data = &bluetooth_driver_data,
5773 },
5774 {
5775 .init = wan_init,
5776 .data = &wan_driver_data,
5777 },
5778 {
5779 .init = video_init,
5780 .data = &video_driver_data,
5781 },
5782 {
5783 .init = light_init,
5784 .data = &light_driver_data,
5785 },
5786#ifdef CONFIG_THINKPAD_ACPI_DOCK
5787 {
5788 .init = dock_init,
5789 .data = &dock_driver_data[0],
5790 },
5791 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005792 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005793 .data = &dock_driver_data[1],
5794 },
5795#endif
5796#ifdef CONFIG_THINKPAD_ACPI_BAY
5797 {
5798 .init = bay_init,
5799 .data = &bay_driver_data,
5800 },
5801#endif
5802 {
5803 .init = cmos_init,
5804 .data = &cmos_driver_data,
5805 },
5806 {
5807 .init = led_init,
5808 .data = &led_driver_data,
5809 },
5810 {
5811 .init = beep_init,
5812 .data = &beep_driver_data,
5813 },
5814 {
5815 .init = thermal_init,
5816 .data = &thermal_driver_data,
5817 },
5818 {
5819 .data = &ecdump_driver_data,
5820 },
5821 {
5822 .init = brightness_init,
5823 .data = &brightness_driver_data,
5824 },
5825 {
5826 .data = &volume_driver_data,
5827 },
5828 {
5829 .init = fan_init,
5830 .data = &fan_driver_data,
5831 },
5832};
5833
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005834static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5835{
5836 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005837 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005838
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005839 if (!kp || !kp->name || !val)
5840 return -EINVAL;
5841
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005842 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5843 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005844 WARN_ON(ibm == NULL);
5845
5846 if (!ibm || !ibm->name)
5847 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005848
5849 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5850 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005851 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005852 strcpy(ibms_init[i].param, val);
5853 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005854 return 0;
5855 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005856 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005857
5858 return -EINVAL;
5859}
5860
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005861module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005862MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005863 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005864
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005865module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005866MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005867
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005868module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005869MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005870 "Attempts to load the driver even on a "
5871 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005872
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005873module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005874MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005875 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005876
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005877module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005878MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005879 "Selects brightness control strategy: "
5880 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005881
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005882module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005883MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005884 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005885
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005886module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005887MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005888 "used for backwards compatibility with userspace, "
5889 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005890
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005891#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005892 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5893 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005894 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005895
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005896TPACPI_PARAM(hotkey);
5897TPACPI_PARAM(bluetooth);
5898TPACPI_PARAM(video);
5899TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005900#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005901TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005902#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005903#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005904TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005905#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005906TPACPI_PARAM(cmos);
5907TPACPI_PARAM(led);
5908TPACPI_PARAM(beep);
5909TPACPI_PARAM(ecdump);
5910TPACPI_PARAM(brightness);
5911TPACPI_PARAM(volume);
5912TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005913
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005914static void thinkpad_acpi_module_exit(void)
5915{
5916 struct ibm_struct *ibm, *itmp;
5917
5918 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5919
5920 list_for_each_entry_safe_reverse(ibm, itmp,
5921 &tpacpi_all_drivers,
5922 all_drivers) {
5923 ibm_exit(ibm);
5924 }
5925
5926 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5927
5928 if (tpacpi_inputdev) {
5929 if (tp_features.input_device_registered)
5930 input_unregister_device(tpacpi_inputdev);
5931 else
5932 input_free_device(tpacpi_inputdev);
5933 }
5934
5935 if (tpacpi_hwmon)
5936 hwmon_device_unregister(tpacpi_hwmon);
5937
5938 if (tp_features.sensors_pdev_attrs_registered)
5939 device_remove_file(&tpacpi_sensors_pdev->dev,
5940 &dev_attr_thinkpad_acpi_pdev_name);
5941 if (tpacpi_sensors_pdev)
5942 platform_device_unregister(tpacpi_sensors_pdev);
5943 if (tpacpi_pdev)
5944 platform_device_unregister(tpacpi_pdev);
5945
5946 if (tp_features.sensors_pdrv_attrs_registered)
5947 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5948 if (tp_features.platform_drv_attrs_registered)
5949 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5950
5951 if (tp_features.sensors_pdrv_registered)
5952 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5953
5954 if (tp_features.platform_drv_registered)
5955 platform_driver_unregister(&tpacpi_pdriver);
5956
5957 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005958 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005959
5960 kfree(thinkpad_id.bios_version_str);
5961 kfree(thinkpad_id.ec_version_str);
5962 kfree(thinkpad_id.model_str);
5963}
5964
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005965
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005966static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967{
5968 int ret, i;
5969
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005970 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5971
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005972 /* Parameter checking */
5973 if (hotkey_report_mode > 2)
5974 return -EINVAL;
5975
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005976 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005977
5978 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005979 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005980 if (ret) {
5981 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005982 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005985 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005986
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005987 TPACPI_ACPIHANDLE_INIT(ecrd);
5988 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005990 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005992 printk(TPACPI_ERR
5993 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005994 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 return -ENODEV;
5996 }
5997 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005998
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005999 ret = platform_driver_register(&tpacpi_pdriver);
6000 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006001 printk(TPACPI_ERR
6002 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006003 thinkpad_acpi_module_exit();
6004 return ret;
6005 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006006 tp_features.platform_drv_registered = 1;
6007
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006008 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6009 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006010 printk(TPACPI_ERR
6011 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006012 thinkpad_acpi_module_exit();
6013 return ret;
6014 }
6015 tp_features.sensors_pdrv_registered = 1;
6016
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006017 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006018 if (!ret) {
6019 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006020 ret = tpacpi_create_driver_attributes(
6021 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006022 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006023 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006024 printk(TPACPI_ERR
6025 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006026 thinkpad_acpi_module_exit();
6027 return ret;
6028 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006029 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006030
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006031
6032 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006033 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006034 NULL, 0);
6035 if (IS_ERR(tpacpi_pdev)) {
6036 ret = PTR_ERR(tpacpi_pdev);
6037 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006038 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006039 thinkpad_acpi_module_exit();
6040 return ret;
6041 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006042 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006043 TPACPI_HWMON_DRVR_NAME,
6044 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006045 if (IS_ERR(tpacpi_sensors_pdev)) {
6046 ret = PTR_ERR(tpacpi_sensors_pdev);
6047 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006048 printk(TPACPI_ERR
6049 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006050 thinkpad_acpi_module_exit();
6051 return ret;
6052 }
6053 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6054 &dev_attr_thinkpad_acpi_pdev_name);
6055 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006056 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006057 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006058 thinkpad_acpi_module_exit();
6059 return ret;
6060 }
6061 tp_features.sensors_pdev_attrs_registered = 1;
6062 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006063 if (IS_ERR(tpacpi_hwmon)) {
6064 ret = PTR_ERR(tpacpi_hwmon);
6065 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006066 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006067 thinkpad_acpi_module_exit();
6068 return ret;
6069 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006070 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006071 tpacpi_inputdev = input_allocate_device();
6072 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006073 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006074 thinkpad_acpi_module_exit();
6075 return -ENOMEM;
6076 } else {
6077 /* Prepare input device, but don't register */
6078 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006079 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006080 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006081 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6082 thinkpad_id.vendor :
6083 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006084 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6085 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6086 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006087 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6088 ret = ibm_init(&ibms_init[i]);
6089 if (ret >= 0 && *ibms_init[i].param)
6090 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006092 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 return ret;
6094 }
6095 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006096 ret = input_register_device(tpacpi_inputdev);
6097 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006098 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006099 thinkpad_acpi_module_exit();
6100 return ret;
6101 } else {
6102 tp_features.input_device_registered = 1;
6103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006105 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 return 0;
6107}
6108
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006109/* Please remove this in year 2009 */
6110MODULE_ALIAS("ibm_acpi");
6111
6112/*
6113 * DMI matching for module autoloading
6114 *
6115 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6116 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6117 *
6118 * Only models listed in thinkwiki will be supported, so add yours
6119 * if it is not there yet.
6120 */
6121#define IBM_BIOS_MODULE_ALIAS(__type) \
6122 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6123
6124/* Non-ancient thinkpads */
6125MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6126MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6127
6128/* Ancient thinkpad BIOSes have to be identified by
6129 * BIOS type or model number, and there are far less
6130 * BIOS types than model numbers... */
6131IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6132IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6133IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6134
6135MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006136MODULE_DESCRIPTION(TPACPI_DESC);
6137MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006138MODULE_LICENSE("GPL");
6139
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006140module_init(thinkpad_acpi_module_init);
6141module_exit(thinkpad_acpi_module_exit);