blob: dd6fa81fa8684dd627649c103716da638f5bd004 [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);
191
192 struct list_head all_drivers;
193
194 struct tp_acpi_drv_struct *acpi;
195
196 struct {
197 u8 acpi_driver_registered:1;
198 u8 acpi_notify_installed:1;
199 u8 proc_created:1;
200 u8 init_called:1;
201 u8 experimental:1;
202 } flags;
203};
204
205struct ibm_init_struct {
206 char param[32];
207
208 int (*init) (struct ibm_init_struct *);
209 struct ibm_struct *data;
210};
211
212static struct {
213#ifdef CONFIG_THINKPAD_ACPI_BAY
214 u32 bay_status:1;
215 u32 bay_eject:1;
216 u32 bay_status2:1;
217 u32 bay_eject2:1;
218#endif
219 u32 bluetooth:1;
220 u32 hotkey:1;
221 u32 hotkey_mask:1;
222 u32 hotkey_wlsw:1;
223 u32 light:1;
224 u32 light_status:1;
225 u32 bright_16levels:1;
226 u32 wan:1;
227 u32 fan_ctrl_status_undef:1;
228 u32 input_device_registered:1;
229 u32 platform_drv_registered:1;
230 u32 platform_drv_attrs_registered:1;
231 u32 sensors_pdrv_registered:1;
232 u32 sensors_pdrv_attrs_registered:1;
233 u32 sensors_pdev_attrs_registered:1;
234 u32 hotkey_poll_active:1;
235} tp_features;
236
237struct thinkpad_id_data {
238 unsigned int vendor; /* ThinkPad vendor:
239 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
240
241 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
242 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
243
244 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
245 u16 ec_model;
246
247 char *model_str;
248};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200249static struct thinkpad_id_data thinkpad_id;
250
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300251static enum {
252 TPACPI_LIFE_INIT = 0,
253 TPACPI_LIFE_RUNNING,
254 TPACPI_LIFE_EXITING,
255} tpacpi_lifecycle;
256
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200257static int experimental;
258static u32 dbg_level;
259
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300260/****************************************************************************
261 ****************************************************************************
262 *
263 * ACPI Helpers and device model
264 *
265 ****************************************************************************
266 ****************************************************************************/
267
268/*************************************************************************
269 * ACPI basic handles
270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300272static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200274#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 static acpi_handle object##_handle; \
276 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400277 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 static char *object##_paths[] = { paths }
279
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200280TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400281 "\\_SB.PCI.ISA.EC", /* 570 */
282 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
283 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
284 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
285 "\\_SB.PCI0.ICH3.EC0", /* R31 */
286 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300287 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200289TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
290TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200292TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
293 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400294 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
295 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300296 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400297
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200298TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400299 "^HKEY", /* R30, R31 */
300 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300301 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400302
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400303
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300304/*************************************************************************
305 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200306 */
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static int acpi_evalf(acpi_handle handle,
309 void *res, char *method, char *fmt, ...)
310{
311 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400312 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200313 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400314 struct acpi_buffer result, *resultp;
315 union acpi_object out_obj;
316 acpi_status status;
317 va_list ap;
318 char res_type;
319 int success;
320 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200323 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
325 }
326
327 if (*fmt == 'q') {
328 quiet = 1;
329 fmt++;
330 } else
331 quiet = 0;
332
333 res_type = *(fmt++);
334
335 params.count = 0;
336 params.pointer = &in_objs[0];
337
338 va_start(ap, fmt);
339 while (*fmt) {
340 char c = *(fmt++);
341 switch (c) {
342 case 'd': /* int */
343 in_objs[params.count].integer.value = va_arg(ap, int);
344 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
345 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400346 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200348 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 "with invalid format character '%c'\n", c);
350 return 0;
351 }
352 }
353 va_end(ap);
354
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400355 if (res_type != 'v') {
356 result.length = sizeof(out_obj);
357 result.pointer = &out_obj;
358 resultp = &result;
359 } else
360 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400362 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400365 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (res)
367 *(int *)res = out_obj.integer.value;
368 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
369 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400370 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 success = status == AE_OK;
372 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400373 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200375 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 "with invalid format character '%c'\n", res_type);
377 return 0;
378 }
379
380 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200381 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 method, fmt0, status);
383
384 return success;
385}
386
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200387static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300388{
389 int v;
390
391 if (ecrd_handle) {
392 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
393 return 0;
394 *p = v;
395 } else {
396 if (ec_read(i, p) < 0)
397 return 0;
398 }
399
400 return 1;
401}
402
403static int acpi_ec_write(int i, u8 v)
404{
405 if (ecwr_handle) {
406 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
407 return 0;
408 } else {
409 if (ec_write(i, v) < 0)
410 return 0;
411 }
412
413 return 1;
414}
415
416static int _sta(acpi_handle handle)
417{
418 int status;
419
420 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
421 status = 0;
422
423 return status;
424}
425
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300426static int issue_thinkpad_cmos_command(int cmos_cmd)
427{
428 if (!cmos_handle)
429 return -ENXIO;
430
431 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
432 return -EIO;
433
434 return 0;
435}
436
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300437/*************************************************************************
438 * ACPI device model
439 */
440
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200441#define TPACPI_ACPIHANDLE_INIT(object) \
442 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200443 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
444
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300445static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300446 acpi_handle *handle, acpi_handle parent,
447 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300448{
449 int i;
450 acpi_status status;
451
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300452 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
453 name);
454
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300455 for (i = 0; i < num_paths; i++) {
456 status = acpi_get_handle(parent, paths[i], handle);
457 if (ACPI_SUCCESS(status)) {
458 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300459 dbg_printk(TPACPI_DBG_INIT,
460 "Found ACPI handle %s for %s\n",
461 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300462 return;
463 }
464 }
465
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300466 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
467 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300468 *handle = NULL;
469}
470
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300471static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300472{
473 struct ibm_struct *ibm = data;
474
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300475 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
476 return;
477
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300478 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300479 return;
480
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300481 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300482}
483
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300484static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300485{
486 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300487 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300488
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300489 BUG_ON(!ibm->acpi);
490
491 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300492 return 0;
493
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300494 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300495 "setting up ACPI notify for %s\n", ibm->name);
496
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300497 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
498 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200499 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300500 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300501 return -ENODEV;
502 }
503
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300504 acpi_driver_data(ibm->acpi->device) = ibm;
505 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200506 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300507 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300508
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300509 status = acpi_install_notify_handler(*ibm->acpi->handle,
510 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300511 if (ACPI_FAILURE(status)) {
512 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200513 printk(TPACPI_NOTICE
514 "another device driver is already "
515 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300516 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200517 printk(TPACPI_ERR
518 "acpi_install_notify_handler(%s) failed: %d\n",
519 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300520 }
521 return -ENODEV;
522 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300523 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300524 return 0;
525}
526
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300527static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300528{
529 return 0;
530}
531
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300532static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300534 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300535
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300536 dbg_printk(TPACPI_DBG_INIT,
537 "registering %s as an ACPI driver\n", ibm->name);
538
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300539 BUG_ON(!ibm->acpi);
540
541 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
542 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200543 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300544 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300545 }
546
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200547 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300548 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200549
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300550 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300551
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300552 rc = acpi_bus_register_driver(ibm->acpi->driver);
553 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200554 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200555 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300556 kfree(ibm->acpi->driver);
557 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300558 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300559 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300560
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300561 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300562}
563
564
565/****************************************************************************
566 ****************************************************************************
567 *
568 * Procfs Helpers
569 *
570 ****************************************************************************
571 ****************************************************************************/
572
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300573static int dispatch_procfs_read(char *page, char **start, off_t off,
574 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300575{
576 struct ibm_struct *ibm = data;
577 int len;
578
579 if (!ibm || !ibm->read)
580 return -EINVAL;
581
582 len = ibm->read(page);
583 if (len < 0)
584 return len;
585
586 if (len <= off + count)
587 *eof = 1;
588 *start = page + off;
589 len -= off;
590 if (len > count)
591 len = count;
592 if (len < 0)
593 len = 0;
594
595 return len;
596}
597
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300598static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200599 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300600 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300601{
602 struct ibm_struct *ibm = data;
603 char *kernbuf;
604 int ret;
605
606 if (!ibm || !ibm->write)
607 return -EINVAL;
608
609 kernbuf = kmalloc(count + 2, GFP_KERNEL);
610 if (!kernbuf)
611 return -ENOMEM;
612
613 if (copy_from_user(kernbuf, userbuf, count)) {
614 kfree(kernbuf);
615 return -EFAULT;
616 }
617
618 kernbuf[count] = 0;
619 strcat(kernbuf, ",");
620 ret = ibm->write(kernbuf);
621 if (ret == 0)
622 ret = count;
623
624 kfree(kernbuf);
625
626 return ret;
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static char *next_cmd(char **cmds)
630{
631 char *start = *cmds;
632 char *end;
633
634 while ((end = strchr(start, ',')) && end == start)
635 start = end + 1;
636
637 if (!end)
638 return NULL;
639
640 *end = 0;
641 *cmds = end + 1;
642 return start;
643}
644
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300645
646/****************************************************************************
647 ****************************************************************************
648 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300649 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300650 *
651 ****************************************************************************
652 ****************************************************************************/
653
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300654static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300655static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700656static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300657static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300658static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200659static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300660
661static int tpacpi_resume_handler(struct platform_device *pdev)
662{
663 struct ibm_struct *ibm, *itmp;
664
665 list_for_each_entry_safe(ibm, itmp,
666 &tpacpi_all_drivers,
667 all_drivers) {
668 if (ibm->resume)
669 (ibm->resume)();
670 }
671
672 return 0;
673}
674
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300675static struct platform_driver tpacpi_pdriver = {
676 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200677 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300678 .owner = THIS_MODULE,
679 },
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300680 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300681};
682
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300683static struct platform_driver tpacpi_hwmon_pdriver = {
684 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200685 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300686 .owner = THIS_MODULE,
687 },
688};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300689
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300690/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300691 * sysfs support helpers
692 */
693
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200694struct attribute_set {
695 unsigned int members, max_members;
696 struct attribute_group group;
697};
698
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300699struct attribute_set_obj {
700 struct attribute_set s;
701 struct attribute *a;
702} __attribute__((packed));
703
704static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200705 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300706{
707 struct attribute_set_obj *sobj;
708
709 if (max_members == 0)
710 return NULL;
711
712 /* Allocates space for implicit NULL at the end too */
713 sobj = kzalloc(sizeof(struct attribute_set_obj) +
714 max_members * sizeof(struct attribute *),
715 GFP_KERNEL);
716 if (!sobj)
717 return NULL;
718 sobj->s.max_members = max_members;
719 sobj->s.group.attrs = &sobj->a;
720 sobj->s.group.name = name;
721
722 return &sobj->s;
723}
724
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200725#define destroy_attr_set(_set) \
726 kfree(_set);
727
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300728/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200729static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300730{
731 if (!s || !attr)
732 return -EINVAL;
733
734 if (s->members >= s->max_members)
735 return -ENOMEM;
736
737 s->group.attrs[s->members] = attr;
738 s->members++;
739
740 return 0;
741}
742
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200743static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300744 struct attribute **attr,
745 unsigned int count)
746{
747 int i, res;
748
749 for (i = 0; i < count; i++) {
750 res = add_to_attr_set(s, attr[i]);
751 if (res)
752 return res;
753 }
754
755 return 0;
756}
757
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200758static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300759{
760 sysfs_remove_group(kobj, &s->group);
761 destroy_attr_set(s);
762}
763
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200764#define register_attr_set_with_sysfs(_attr_set, _kobj) \
765 sysfs_create_group(_kobj, &_attr_set->group)
766
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300767static int parse_strtoul(const char *buf,
768 unsigned long max, unsigned long *value)
769{
770 char *endp;
771
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300772 while (*buf && isspace(*buf))
773 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300774 *value = simple_strtoul(buf, &endp, 0);
775 while (*endp && isspace(*endp))
776 endp++;
777 if (*endp || *value > max)
778 return -EINVAL;
779
780 return 0;
781}
782
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200783/*************************************************************************
784 * thinkpad-acpi driver attributes
785 */
786
787/* interface_version --------------------------------------------------- */
788static ssize_t tpacpi_driver_interface_version_show(
789 struct device_driver *drv,
790 char *buf)
791{
792 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
793}
794
795static DRIVER_ATTR(interface_version, S_IRUGO,
796 tpacpi_driver_interface_version_show, NULL);
797
798/* debug_level --------------------------------------------------------- */
799static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
800 char *buf)
801{
802 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
803}
804
805static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
806 const char *buf, size_t count)
807{
808 unsigned long t;
809
810 if (parse_strtoul(buf, 0xffff, &t))
811 return -EINVAL;
812
813 dbg_level = t;
814
815 return count;
816}
817
818static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
819 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
820
821/* version ------------------------------------------------------------- */
822static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
823 char *buf)
824{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200825 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
826 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200827}
828
829static DRIVER_ATTR(version, S_IRUGO,
830 tpacpi_driver_version_show, NULL);
831
832/* --------------------------------------------------------------------- */
833
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200834static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200835 &driver_attr_debug_level, &driver_attr_version,
836 &driver_attr_interface_version,
837};
838
839static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
840{
841 int i, res;
842
843 i = 0;
844 res = 0;
845 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
846 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
847 i++;
848 }
849
850 return res;
851}
852
853static void tpacpi_remove_driver_attributes(struct device_driver *drv)
854{
855 int i;
856
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200857 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200858 driver_remove_file(drv, tpacpi_driver_attributes[i]);
859}
860
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300861/****************************************************************************
862 ****************************************************************************
863 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300864 * Subdrivers
865 *
866 ****************************************************************************
867 ****************************************************************************/
868
869/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300870 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300871 */
872
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300873static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200875 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
876 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200878 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300879 (thinkpad_id.bios_version_str) ?
880 thinkpad_id.bios_version_str : "unknown",
881 (thinkpad_id.ec_version_str) ?
882 thinkpad_id.ec_version_str : "unknown");
883
884 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200885 printk(TPACPI_INFO "%s %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300886 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
887 "IBM" : ((thinkpad_id.vendor ==
888 PCI_VENDOR_ID_LENOVO) ?
889 "Lenovo" : "Unknown vendor"),
890 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return 0;
893}
894
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300895static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 int len = 0;
898
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200899 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
900 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 return len;
903}
904
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300905static struct ibm_struct thinkpad_acpi_driver_data = {
906 .name = "driver",
907 .read = thinkpad_acpi_driver_read,
908};
909
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300910/*************************************************************************
911 * Hotkey subdriver
912 */
913
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200914enum { /* hot key scan codes (derived from ACPI DSDT) */
915 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
916 TP_ACPI_HOTKEYSCAN_FNF2,
917 TP_ACPI_HOTKEYSCAN_FNF3,
918 TP_ACPI_HOTKEYSCAN_FNF4,
919 TP_ACPI_HOTKEYSCAN_FNF5,
920 TP_ACPI_HOTKEYSCAN_FNF6,
921 TP_ACPI_HOTKEYSCAN_FNF7,
922 TP_ACPI_HOTKEYSCAN_FNF8,
923 TP_ACPI_HOTKEYSCAN_FNF9,
924 TP_ACPI_HOTKEYSCAN_FNF10,
925 TP_ACPI_HOTKEYSCAN_FNF11,
926 TP_ACPI_HOTKEYSCAN_FNF12,
927 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
928 TP_ACPI_HOTKEYSCAN_FNINSERT,
929 TP_ACPI_HOTKEYSCAN_FNDELETE,
930 TP_ACPI_HOTKEYSCAN_FNHOME,
931 TP_ACPI_HOTKEYSCAN_FNEND,
932 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
933 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
934 TP_ACPI_HOTKEYSCAN_FNSPACE,
935 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
936 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
937 TP_ACPI_HOTKEYSCAN_MUTE,
938 TP_ACPI_HOTKEYSCAN_THINKPAD,
939};
940
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200941enum { /* Keys available through NVRAM polling */
942 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
943 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
944};
945
946enum { /* Positions of some of the keys in hotkey masks */
947 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
948 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
949 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
950 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
951 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
952 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
953 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
954 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
955 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
956 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
957 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
958};
959
960enum { /* NVRAM to ACPI HKEY group map */
961 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
962 TP_ACPI_HKEY_ZOOM_MASK |
963 TP_ACPI_HKEY_DISPSWTCH_MASK |
964 TP_ACPI_HKEY_HIBERNATE_MASK,
965 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
966 TP_ACPI_HKEY_BRGHTDWN_MASK,
967 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
968 TP_ACPI_HKEY_VOLDWN_MASK |
969 TP_ACPI_HKEY_MUTE_MASK,
970};
971
972#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
973struct tp_nvram_state {
974 u16 thinkpad_toggle:1;
975 u16 zoom_toggle:1;
976 u16 display_toggle:1;
977 u16 thinklight_toggle:1;
978 u16 hibernate_toggle:1;
979 u16 displayexp_toggle:1;
980 u16 display_state:1;
981 u16 brightness_toggle:1;
982 u16 volume_toggle:1;
983 u16 mute:1;
984
985 u8 brightness_level;
986 u8 volume_level;
987};
988
989static struct task_struct *tpacpi_hotkey_task;
990static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
991static int hotkey_poll_freq = 10; /* Hz */
992static struct mutex hotkey_thread_mutex;
993static struct mutex hotkey_thread_data_mutex;
994static unsigned int hotkey_config_change;
995
996#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
997
998#define hotkey_source_mask 0U
999
1000#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1001
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001002static struct mutex hotkey_mutex;
1003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001004static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001005static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001006static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001007static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001008static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001009
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001010static unsigned int hotkey_report_mode;
1011
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001012static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001013
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001014static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001015
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001016#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1017#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001018 do { \
1019 mutex_lock(&hotkey_thread_data_mutex); \
1020 hotkey_config_change++; \
1021 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001022#define HOTKEY_CONFIG_CRITICAL_END \
1023 mutex_unlock(&hotkey_thread_data_mutex);
1024#else
1025#define HOTKEY_CONFIG_CRITICAL_START
1026#define HOTKEY_CONFIG_CRITICAL_END
1027#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1028
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001029static int hotkey_get_wlsw(int *status)
1030{
1031 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1032 return -EIO;
1033 return 0;
1034}
1035
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001036/*
1037 * Call with hotkey_mutex held
1038 */
1039static int hotkey_mask_get(void)
1040{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001041 u32 m = 0;
1042
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001043 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001044 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001045 return -EIO;
1046 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001047 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001048
1049 return 0;
1050}
1051
1052/*
1053 * Call with hotkey_mutex held
1054 */
1055static int hotkey_mask_set(u32 mask)
1056{
1057 int i;
1058 int rc = 0;
1059
1060 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001061 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001062 for (i = 0; i < 32; i++) {
1063 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001064 /* enable in firmware mask only keys not in NVRAM
1065 * mode, but enable the key in the cached hotkey_mask
1066 * regardless of mode, or the key will end up
1067 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001068 if (!acpi_evalf(hkey_handle,
1069 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001070 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001071 rc = -EIO;
1072 break;
1073 } else {
1074 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1075 }
1076 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001077 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001078
1079 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001080 if (!hotkey_mask_get() && !rc &&
1081 (hotkey_mask & ~hotkey_source_mask) !=
1082 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001083 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001084 "requested hot key mask 0x%08x, but "
1085 "firmware forced it to 0x%08x\n",
1086 mask, hotkey_mask);
1087 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001088 } else {
1089#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1090 HOTKEY_CONFIG_CRITICAL_START
1091 hotkey_mask = mask & hotkey_source_mask;
1092 HOTKEY_CONFIG_CRITICAL_END
1093 hotkey_mask_get();
1094 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001095 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001096 "requested hot key mask 0x%08x, "
1097 "forced to 0x%08x (NVRAM poll mask is "
1098 "0x%08x): no firmware mask support\n",
1099 mask, hotkey_mask, hotkey_source_mask);
1100 }
1101#else
1102 hotkey_mask_get();
1103 rc = -ENXIO;
1104#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001105 }
1106
1107 return rc;
1108}
1109
1110static int hotkey_status_get(int *status)
1111{
1112 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1113 return -EIO;
1114
1115 return 0;
1116}
1117
1118static int hotkey_status_set(int status)
1119{
1120 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1121 return -EIO;
1122
1123 return 0;
1124}
1125
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001126static void tpacpi_input_send_radiosw(void)
1127{
1128 int wlsw;
1129
1130 mutex_lock(&tpacpi_inputdev_send_mutex);
1131
1132 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1133 input_report_switch(tpacpi_inputdev,
1134 SW_RADIO, !!wlsw);
1135 input_sync(tpacpi_inputdev);
1136 }
1137
1138 mutex_unlock(&tpacpi_inputdev_send_mutex);
1139}
1140
1141static void tpacpi_input_send_key(unsigned int scancode)
1142{
1143 unsigned int keycode;
1144
1145 keycode = hotkey_keycode_map[scancode];
1146
1147 if (keycode != KEY_RESERVED) {
1148 mutex_lock(&tpacpi_inputdev_send_mutex);
1149
1150 input_report_key(tpacpi_inputdev, keycode, 1);
1151 if (keycode == KEY_UNKNOWN)
1152 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1153 scancode);
1154 input_sync(tpacpi_inputdev);
1155
1156 input_report_key(tpacpi_inputdev, keycode, 0);
1157 if (keycode == KEY_UNKNOWN)
1158 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1159 scancode);
1160 input_sync(tpacpi_inputdev);
1161
1162 mutex_unlock(&tpacpi_inputdev_send_mutex);
1163 }
1164}
1165
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001166#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1167static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1168
1169static void tpacpi_hotkey_send_key(unsigned int scancode)
1170{
1171 tpacpi_input_send_key(scancode);
1172 if (hotkey_report_mode < 2) {
1173 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1174 0x80, 0x1001 + scancode);
1175 }
1176}
1177
1178static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1179{
1180 u8 d;
1181
1182 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1183 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1184 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1185 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1186 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1187 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1188 }
1189 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1190 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1191 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1192 }
1193 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1194 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1195 n->displayexp_toggle =
1196 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1197 }
1198 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1199 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1200 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1201 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1202 n->brightness_toggle =
1203 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1204 }
1205 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1206 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1207 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1208 >> TP_NVRAM_POS_LEVEL_VOLUME;
1209 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1210 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1211 }
1212}
1213
1214#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001215 do { \
1216 if ((mask & (1 << __scancode)) && \
1217 oldn->__member != newn->__member) \
1218 tpacpi_hotkey_send_key(__scancode); \
1219 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001220
1221#define TPACPI_MAY_SEND_KEY(__scancode) \
1222 do { if (mask & (1 << __scancode)) \
1223 tpacpi_hotkey_send_key(__scancode); } while (0)
1224
1225static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001226 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001227 u32 mask)
1228{
1229 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1230 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1231 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1232 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1233
1234 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1235
1236 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1237
1238 /* handle volume */
1239 if (oldn->volume_toggle != newn->volume_toggle) {
1240 if (oldn->mute != newn->mute) {
1241 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1242 }
1243 if (oldn->volume_level > newn->volume_level) {
1244 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1245 } else if (oldn->volume_level < newn->volume_level) {
1246 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1247 } else if (oldn->mute == newn->mute) {
1248 /* repeated key presses that didn't change state */
1249 if (newn->mute) {
1250 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1251 } else if (newn->volume_level != 0) {
1252 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1253 } else {
1254 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1255 }
1256 }
1257 }
1258
1259 /* handle brightness */
1260 if (oldn->brightness_toggle != newn->brightness_toggle) {
1261 if (oldn->brightness_level < newn->brightness_level) {
1262 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1263 } else if (oldn->brightness_level > newn->brightness_level) {
1264 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1265 } else {
1266 /* repeated key presses that didn't change state */
1267 if (newn->brightness_level != 0) {
1268 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1269 } else {
1270 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1271 }
1272 }
1273 }
1274}
1275
1276#undef TPACPI_COMPARE_KEY
1277#undef TPACPI_MAY_SEND_KEY
1278
1279static int hotkey_kthread(void *data)
1280{
1281 struct tp_nvram_state s[2];
1282 u32 mask;
1283 unsigned int si, so;
1284 unsigned long t;
1285 unsigned int change_detector, must_reset;
1286
1287 mutex_lock(&hotkey_thread_mutex);
1288
1289 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1290 goto exit;
1291
1292 set_freezable();
1293
1294 so = 0;
1295 si = 1;
1296 t = 0;
1297
1298 /* Initial state for compares */
1299 mutex_lock(&hotkey_thread_data_mutex);
1300 change_detector = hotkey_config_change;
1301 mask = hotkey_source_mask & hotkey_mask;
1302 mutex_unlock(&hotkey_thread_data_mutex);
1303 hotkey_read_nvram(&s[so], mask);
1304
1305 while (!kthread_should_stop() && hotkey_poll_freq) {
1306 if (t == 0)
1307 t = 1000/hotkey_poll_freq;
1308 t = msleep_interruptible(t);
1309 if (unlikely(kthread_should_stop()))
1310 break;
1311 must_reset = try_to_freeze();
1312 if (t > 0 && !must_reset)
1313 continue;
1314
1315 mutex_lock(&hotkey_thread_data_mutex);
1316 if (must_reset || hotkey_config_change != change_detector) {
1317 /* forget old state on thaw or config change */
1318 si = so;
1319 t = 0;
1320 change_detector = hotkey_config_change;
1321 }
1322 mask = hotkey_source_mask & hotkey_mask;
1323 mutex_unlock(&hotkey_thread_data_mutex);
1324
1325 if (likely(mask)) {
1326 hotkey_read_nvram(&s[si], mask);
1327 if (likely(si != so)) {
1328 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001329 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001330 }
1331 }
1332
1333 so = si;
1334 si ^= 1;
1335 }
1336
1337exit:
1338 mutex_unlock(&hotkey_thread_mutex);
1339 return 0;
1340}
1341
1342static void hotkey_poll_stop_sync(void)
1343{
1344 if (tpacpi_hotkey_task) {
1345 if (frozen(tpacpi_hotkey_task) ||
1346 freezing(tpacpi_hotkey_task))
1347 thaw_process(tpacpi_hotkey_task);
1348
1349 kthread_stop(tpacpi_hotkey_task);
1350 tpacpi_hotkey_task = NULL;
1351 mutex_lock(&hotkey_thread_mutex);
1352 /* at this point, the thread did exit */
1353 mutex_unlock(&hotkey_thread_mutex);
1354 }
1355}
1356
1357/* call with hotkey_mutex held */
1358static void hotkey_poll_setup(int may_warn)
1359{
1360 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1361 hotkey_poll_freq > 0 &&
1362 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1363 if (!tpacpi_hotkey_task) {
1364 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001365 NULL,
1366 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001367 if (IS_ERR(tpacpi_hotkey_task)) {
1368 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001369 printk(TPACPI_ERR
1370 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001371 "for hotkey polling\n");
1372 }
1373 }
1374 } else {
1375 hotkey_poll_stop_sync();
1376 if (may_warn &&
1377 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001378 printk(TPACPI_NOTICE
1379 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001380 "which is currently disabled\n",
1381 hotkey_source_mask);
1382 }
1383 }
1384}
1385
1386static void hotkey_poll_setup_safe(int may_warn)
1387{
1388 mutex_lock(&hotkey_mutex);
1389 hotkey_poll_setup(may_warn);
1390 mutex_unlock(&hotkey_mutex);
1391}
1392
1393static int hotkey_inputdev_open(struct input_dev *dev)
1394{
1395 switch (tpacpi_lifecycle) {
1396 case TPACPI_LIFE_INIT:
1397 /*
1398 * hotkey_init will call hotkey_poll_setup_safe
1399 * at the appropriate moment
1400 */
1401 return 0;
1402 case TPACPI_LIFE_EXITING:
1403 return -EBUSY;
1404 case TPACPI_LIFE_RUNNING:
1405 hotkey_poll_setup_safe(0);
1406 return 0;
1407 }
1408
1409 /* Should only happen if tpacpi_lifecycle is corrupt */
1410 BUG();
1411 return -EBUSY;
1412}
1413
1414static void hotkey_inputdev_close(struct input_dev *dev)
1415{
1416 /* disable hotkey polling when possible */
1417 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1418 hotkey_poll_setup_safe(0);
1419}
1420#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1421
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001422/* sysfs hotkey enable ------------------------------------------------- */
1423static ssize_t hotkey_enable_show(struct device *dev,
1424 struct device_attribute *attr,
1425 char *buf)
1426{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001427 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001428
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001429 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001430 if (res)
1431 return res;
1432
1433 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1434}
1435
1436static ssize_t hotkey_enable_store(struct device *dev,
1437 struct device_attribute *attr,
1438 const char *buf, size_t count)
1439{
1440 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001441 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001442
1443 if (parse_strtoul(buf, 1, &t))
1444 return -EINVAL;
1445
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001446 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001447
1448 return (res) ? res : count;
1449}
1450
1451static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001452 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001453 hotkey_enable_show, hotkey_enable_store);
1454
1455/* sysfs hotkey mask --------------------------------------------------- */
1456static ssize_t hotkey_mask_show(struct device *dev,
1457 struct device_attribute *attr,
1458 char *buf)
1459{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001460 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001461
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001462 if (mutex_lock_interruptible(&hotkey_mutex))
1463 return -ERESTARTSYS;
1464 res = hotkey_mask_get();
1465 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001466
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001467 return (res)?
1468 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001469}
1470
1471static ssize_t hotkey_mask_store(struct device *dev,
1472 struct device_attribute *attr,
1473 const char *buf, size_t count)
1474{
1475 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001476 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001477
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001478 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001479 return -EINVAL;
1480
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001481 if (mutex_lock_interruptible(&hotkey_mutex))
1482 return -ERESTARTSYS;
1483
1484 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001485
1486#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1487 hotkey_poll_setup(1);
1488#endif
1489
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001490 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001491
1492 return (res) ? res : count;
1493}
1494
1495static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001496 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001497 hotkey_mask_show, hotkey_mask_store);
1498
1499/* sysfs hotkey bios_enabled ------------------------------------------- */
1500static ssize_t hotkey_bios_enabled_show(struct device *dev,
1501 struct device_attribute *attr,
1502 char *buf)
1503{
1504 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1505}
1506
1507static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001508 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001509
1510/* sysfs hotkey bios_mask ---------------------------------------------- */
1511static ssize_t hotkey_bios_mask_show(struct device *dev,
1512 struct device_attribute *attr,
1513 char *buf)
1514{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001515 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001516}
1517
1518static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001519 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001520
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001521/* sysfs hotkey all_mask ----------------------------------------------- */
1522static ssize_t hotkey_all_mask_show(struct device *dev,
1523 struct device_attribute *attr,
1524 char *buf)
1525{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001526 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1527 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001528}
1529
1530static struct device_attribute dev_attr_hotkey_all_mask =
1531 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1532
1533/* sysfs hotkey recommended_mask --------------------------------------- */
1534static ssize_t hotkey_recommended_mask_show(struct device *dev,
1535 struct device_attribute *attr,
1536 char *buf)
1537{
1538 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001539 (hotkey_all_mask | hotkey_source_mask)
1540 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001541}
1542
1543static struct device_attribute dev_attr_hotkey_recommended_mask =
1544 __ATTR(hotkey_recommended_mask, S_IRUGO,
1545 hotkey_recommended_mask_show, NULL);
1546
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001547#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1548
1549/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1550static ssize_t hotkey_source_mask_show(struct device *dev,
1551 struct device_attribute *attr,
1552 char *buf)
1553{
1554 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1555}
1556
1557static ssize_t hotkey_source_mask_store(struct device *dev,
1558 struct device_attribute *attr,
1559 const char *buf, size_t count)
1560{
1561 unsigned long t;
1562
1563 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1564 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1565 return -EINVAL;
1566
1567 if (mutex_lock_interruptible(&hotkey_mutex))
1568 return -ERESTARTSYS;
1569
1570 HOTKEY_CONFIG_CRITICAL_START
1571 hotkey_source_mask = t;
1572 HOTKEY_CONFIG_CRITICAL_END
1573
1574 hotkey_poll_setup(1);
1575
1576 mutex_unlock(&hotkey_mutex);
1577
1578 return count;
1579}
1580
1581static struct device_attribute dev_attr_hotkey_source_mask =
1582 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1583 hotkey_source_mask_show, hotkey_source_mask_store);
1584
1585/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1586static ssize_t hotkey_poll_freq_show(struct device *dev,
1587 struct device_attribute *attr,
1588 char *buf)
1589{
1590 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1591}
1592
1593static ssize_t hotkey_poll_freq_store(struct device *dev,
1594 struct device_attribute *attr,
1595 const char *buf, size_t count)
1596{
1597 unsigned long t;
1598
1599 if (parse_strtoul(buf, 25, &t))
1600 return -EINVAL;
1601
1602 if (mutex_lock_interruptible(&hotkey_mutex))
1603 return -ERESTARTSYS;
1604
1605 hotkey_poll_freq = t;
1606
1607 hotkey_poll_setup(1);
1608 mutex_unlock(&hotkey_mutex);
1609
1610 return count;
1611}
1612
1613static struct device_attribute dev_attr_hotkey_poll_freq =
1614 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1615 hotkey_poll_freq_show, hotkey_poll_freq_store);
1616
1617#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1618
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001619/* sysfs hotkey radio_sw ----------------------------------------------- */
1620static ssize_t hotkey_radio_sw_show(struct device *dev,
1621 struct device_attribute *attr,
1622 char *buf)
1623{
1624 int res, s;
1625 res = hotkey_get_wlsw(&s);
1626 if (res < 0)
1627 return res;
1628
1629 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1630}
1631
1632static struct device_attribute dev_attr_hotkey_radio_sw =
1633 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1634
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001635/* sysfs hotkey report_mode -------------------------------------------- */
1636static ssize_t hotkey_report_mode_show(struct device *dev,
1637 struct device_attribute *attr,
1638 char *buf)
1639{
1640 return snprintf(buf, PAGE_SIZE, "%d\n",
1641 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1642}
1643
1644static struct device_attribute dev_attr_hotkey_report_mode =
1645 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1646
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001647/* --------------------------------------------------------------------- */
1648
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001649static struct attribute *hotkey_attributes[] __initdata = {
1650 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001651 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001652 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001653#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1654 &dev_attr_hotkey_mask.attr,
1655 &dev_attr_hotkey_all_mask.attr,
1656 &dev_attr_hotkey_recommended_mask.attr,
1657 &dev_attr_hotkey_source_mask.attr,
1658 &dev_attr_hotkey_poll_freq.attr,
1659#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001660};
1661
1662static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001663 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001664#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1665 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001666 &dev_attr_hotkey_all_mask.attr,
1667 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001668#endif
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001669};
1670
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001671static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001672{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001673 /* Requirements for changing the default keymaps:
1674 *
1675 * 1. Many of the keys are mapped to KEY_RESERVED for very
1676 * good reasons. Do not change them unless you have deep
1677 * knowledge on the IBM and Lenovo ThinkPad firmware for
1678 * the various ThinkPad models. The driver behaves
1679 * differently for KEY_RESERVED: such keys have their
1680 * hot key mask *unset* in mask_recommended, and also
1681 * in the initial hot key mask programmed into the
1682 * firmware at driver load time, which means the firm-
1683 * ware may react very differently if you change them to
1684 * something else;
1685 *
1686 * 2. You must be subscribed to the linux-thinkpad and
1687 * ibm-acpi-devel mailing lists, and you should read the
1688 * list archives since 2007 if you want to change the
1689 * keymaps. This requirement exists so that you will
1690 * know the past history of problems with the thinkpad-
1691 * acpi driver keymaps, and also that you will be
1692 * listening to any bug reports;
1693 *
1694 * 3. Do not send thinkpad-acpi specific patches directly to
1695 * for merging, *ever*. Send them to the linux-acpi
1696 * mailinglist for comments. Merging is to be done only
1697 * through acpi-test and the ACPI maintainer.
1698 *
1699 * If the above is too much to ask, don't change the keymap.
1700 * Ask the thinkpad-acpi maintainer to do it, instead.
1701 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001702 static u16 ibm_keycode_map[] __initdata = {
1703 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1704 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1705 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1706 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001707
1708 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001709 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1710 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1711 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001712
1713 /* brightness: firmware always reacts to them, unless
1714 * X.org did some tricks in the radeon BIOS scratch
1715 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001716 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001717 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001718
1719 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001720 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001721
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001722 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1723 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001724
1725 /* Volume: firmware always react to it and reprograms
1726 * the built-in *extra* mixer. Never map it to control
1727 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001728 KEY_RESERVED, /* 0x14: VOLUME UP */
1729 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1730 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001731
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001732 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001733
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001734 /* (assignments unknown, please report if found) */
1735 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1736 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1737 };
1738 static u16 lenovo_keycode_map[] __initdata = {
1739 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1740 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1741 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1742 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001743
1744 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001745 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1746 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1747 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001748
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001749 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001750 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001751
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001752 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001753
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001754 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1755 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001756
1757 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1758 * react to it and reprograms the built-in *extra* mixer.
1759 * Never map it to control another mixer by default.
1760 *
1761 * T60?, T61, R60?, R61: firmware and EC tries to send
1762 * these over the regular keyboard, so these are no-ops,
1763 * but there are still weird bugs re. MUTE, so do not
1764 * change unless you get test reports from all Lenovo
1765 * models. May cause the BIOS to interfere with the
1766 * HDA mixer.
1767 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001768 KEY_RESERVED, /* 0x14: VOLUME UP */
1769 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1770 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001771
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001772 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001773
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001774 /* (assignments unknown, please report if found) */
1775 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1776 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1777 };
1778
1779#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1780#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1781#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1782
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001783 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001784 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001785 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001786
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001787 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1788
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001789 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001790 BUG_ON(tpacpi_inputdev->open != NULL ||
1791 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001792
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001793 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001794 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001795
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001796#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1797 mutex_init(&hotkey_thread_mutex);
1798 mutex_init(&hotkey_thread_data_mutex);
1799#endif
1800
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001801 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001802 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001803
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001804 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001805 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001806
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001807 if (tp_features.hotkey) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001808 hotkey_dev_attributes = create_attr_set(10, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001809 if (!hotkey_dev_attributes)
1810 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001811 res = add_many_to_attr_set(hotkey_dev_attributes,
1812 hotkey_attributes,
1813 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001814 if (res)
1815 return res;
1816
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001817 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001818 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1819 for HKEY interface version 0x100 */
1820 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1821 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001822 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001823 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001824 printk(TPACPI_ERR "please report this to %s\n",
1825 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001826 } else {
1827 /*
1828 * MHKV 0x100 in A31, R40, R40e,
1829 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001830 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001831 tp_features.hotkey_mask = 1;
1832 }
1833 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001834
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001835 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001836 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001837
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001838 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001839 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001840 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001841 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001842 "missing MHKA handler, "
1843 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001844 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001845 /* FN+F12, FN+F4, FN+F3 */
1846 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001847 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001848 }
1849
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001850 /* hotkey_source_mask *must* be zero for
1851 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001852 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001853 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001854 res = hotkey_mask_get();
1855 hotkey_orig_mask = hotkey_mask;
1856 if (!res) {
1857 res = add_many_to_attr_set(
1858 hotkey_dev_attributes,
1859 hotkey_mask_attributes,
1860 ARRAY_SIZE(hotkey_mask_attributes));
1861 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001862 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001863
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001864#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1865 if (tp_features.hotkey_mask) {
1866 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1867 & ~hotkey_all_mask;
1868 } else {
1869 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1870 }
1871
1872 vdbg_printk(TPACPI_DBG_INIT,
1873 "hotkey source mask 0x%08x, polling freq %d\n",
1874 hotkey_source_mask, hotkey_poll_freq);
1875#endif
1876
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001877 /* Not all thinkpads have a hardware radio switch */
1878 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1879 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001880 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001881 "radio switch found; radios are %s\n",
1882 enabled(status, 0));
1883 res = add_to_attr_set(hotkey_dev_attributes,
1884 &dev_attr_hotkey_radio_sw.attr);
1885 }
1886
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001887 if (!res)
1888 res = register_attr_set_with_sysfs(
1889 hotkey_dev_attributes,
1890 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001891 if (res)
1892 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001893
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001894 /* Set up key map */
1895
1896 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1897 GFP_KERNEL);
1898 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001899 printk(TPACPI_ERR
1900 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001901 return -ENOMEM;
1902 }
1903
1904 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1905 dbg_printk(TPACPI_DBG_INIT,
1906 "using Lenovo default hot key map\n");
1907 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1908 TPACPI_HOTKEY_MAP_SIZE);
1909 } else {
1910 dbg_printk(TPACPI_DBG_INIT,
1911 "using IBM default hot key map\n");
1912 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1913 TPACPI_HOTKEY_MAP_SIZE);
1914 }
1915
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001916 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1917 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1918 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001919 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1920 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1921 tpacpi_inputdev->keycode = hotkey_keycode_map;
1922 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001923 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1924 set_bit(hotkey_keycode_map[i],
1925 tpacpi_inputdev->keybit);
1926 } else {
1927 if (i < sizeof(hotkey_reserved_mask)*8)
1928 hotkey_reserved_mask |= 1 << i;
1929 }
1930 }
1931
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001932 if (tp_features.hotkey_wlsw) {
1933 set_bit(EV_SW, tpacpi_inputdev->evbit);
1934 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1935 }
1936
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001937 dbg_printk(TPACPI_DBG_INIT,
1938 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001939 res = hotkey_status_set(1);
1940 if (res)
1941 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001942 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1943 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001944 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001945 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001946 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001947
1948 dbg_printk(TPACPI_DBG_INIT,
1949 "legacy hot key reporting over procfs %s\n",
1950 (hotkey_report_mode < 2) ?
1951 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001952
1953#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1954 tpacpi_inputdev->open = &hotkey_inputdev_open;
1955 tpacpi_inputdev->close = &hotkey_inputdev_close;
1956
1957 hotkey_poll_setup_safe(1);
1958#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001959 }
1960
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001961 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001962}
1963
1964static void hotkey_exit(void)
1965{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001966#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1967 hotkey_poll_stop_sync();
1968#endif
1969
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001970 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001971 dbg_printk(TPACPI_DBG_EXIT,
1972 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001973 /* no short-circuit boolean operator below! */
1974 if ((hotkey_mask_set(hotkey_orig_mask) |
1975 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001976 printk(TPACPI_ERR
1977 "failed to restore hot key mask "
1978 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001979 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001980
1981 if (hotkey_dev_attributes) {
1982 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1983 hotkey_dev_attributes = NULL;
1984 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001985}
1986
1987static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1988{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001989 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001990 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001991 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001992 int ignore_acpi_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001993
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001994 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001995 printk(TPACPI_ERR
1996 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001997 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001998 acpi_bus_generate_netlink_event(
1999 ibm->acpi->device->pnp.device_class,
2000 ibm->acpi->device->dev.bus_id,
2001 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002002 return;
2003 }
2004
2005 while (1) {
2006 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002007 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002008 return;
2009 }
2010
2011 if (hkey == 0) {
2012 /* queue empty */
2013 return;
2014 }
2015
2016 send_acpi_ev = 0;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002017 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002018
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002019 switch (hkey >> 12) {
2020 case 1:
2021 /* 0x1000-0x1FFF: key presses */
2022 scancode = hkey & 0xfff;
2023 if (scancode > 0 && scancode < 0x21) {
2024 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002025 if (!(hotkey_source_mask & (1 << scancode))) {
2026 tpacpi_input_send_key(scancode);
2027 } else {
2028 ignore_acpi_ev = 1;
2029 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002030 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002031 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002032 "hotkey 0x%04x out of range "
2033 "for keyboard map\n", hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002034 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002035 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002036 break;
2037 case 5:
2038 /* 0x5000-0x5FFF: LID */
2039 /* we don't handle it through this path, just
2040 * eat up known LID events */
2041 if (hkey != 0x5001 && hkey != 0x5002) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002042 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002043 "unknown LID-related HKEY event: "
2044 "0x%04x\n", hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002045 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002046 } else {
2047 ignore_acpi_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002048 }
2049 break;
2050 case 7:
2051 /* 0x7000-0x7FFF: misc */
2052 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2053 tpacpi_input_send_radiosw();
2054 break;
2055 }
2056 /* fallthrough to default */
2057 default:
2058 /* case 2: dock-related */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002059 /* 0x2305 - T43 waking up due to bay lever
2060 * eject while aslept */
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002061 /* case 3: ultra-bay related. maybe bay in dock? */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002062 /* 0x3003 - T43 after wake up by bay lever
2063 * eject (0x2305) */
2064 printk(TPACPI_NOTICE
2065 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002066 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002067 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002068
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002069 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002070 if (!ignore_acpi_ev &&
2071 (send_acpi_ev || hotkey_report_mode < 2)) {
2072 acpi_bus_generate_proc_event(ibm->acpi->device,
2073 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002074 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002075
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002076 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002077 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002078 acpi_bus_generate_netlink_event(
2079 ibm->acpi->device->pnp.device_class,
2080 ibm->acpi->device->dev.bus_id,
2081 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002082 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002083 }
2084}
2085
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002086static void hotkey_resume(void)
2087{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002088 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002089 printk(TPACPI_ERR
2090 "error while trying to read hot key mask "
2091 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002092 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002093#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2094 hotkey_poll_setup_safe(0);
2095#endif
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002096}
2097
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002098/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002099static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002101 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 int len = 0;
2103
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002104 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002105 len += sprintf(p + len, "status:\t\tnot supported\n");
2106 return len;
2107 }
2108
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002109 if (mutex_lock_interruptible(&hotkey_mutex))
2110 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002111 res = hotkey_status_get(&status);
2112 if (!res)
2113 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002114 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002115 if (res)
2116 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002119 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002120 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 len += sprintf(p + len,
2122 "commands:\tenable, disable, reset, <mask>\n");
2123 } else {
2124 len += sprintf(p + len, "mask:\t\tnot supported\n");
2125 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2126 }
2127
2128 return len;
2129}
2130
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002131static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002133 int res, status;
2134 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002137 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return -ENODEV;
2139
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002140 if (mutex_lock_interruptible(&hotkey_mutex))
2141 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002142
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002143 status = -1;
2144 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002145
2146 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 while ((cmd = next_cmd(&buf))) {
2148 if (strlencmp(cmd, "enable") == 0) {
2149 status = 1;
2150 } else if (strlencmp(cmd, "disable") == 0) {
2151 status = 0;
2152 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002153 status = hotkey_orig_status;
2154 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2156 /* mask set */
2157 } else if (sscanf(cmd, "%x", &mask) == 1) {
2158 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002159 } else {
2160 res = -EINVAL;
2161 goto errexit;
2162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002164 if (status != -1)
2165 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002167 if (!res && mask != hotkey_mask)
2168 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002170errexit:
2171 mutex_unlock(&hotkey_mutex);
2172 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002175static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002176 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002177 {"", 0},
2178};
2179
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002180static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002181 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002182 .notify = hotkey_notify,
2183 .handle = &hkey_handle,
2184 .type = ACPI_DEVICE_NOTIFY,
2185};
2186
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002187static struct ibm_struct hotkey_driver_data = {
2188 .name = "hotkey",
2189 .read = hotkey_read,
2190 .write = hotkey_write,
2191 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002192 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002193 .acpi = &ibm_hotkey_acpidriver,
2194};
2195
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002196/*************************************************************************
2197 * Bluetooth subdriver
2198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002200enum {
2201 /* ACPI GBDC/SBDC bits */
2202 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2203 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2204 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2205};
2206
2207static int bluetooth_get_radiosw(void);
2208static int bluetooth_set_radiosw(int radio_on);
2209
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002210/* sysfs bluetooth enable ---------------------------------------------- */
2211static ssize_t bluetooth_enable_show(struct device *dev,
2212 struct device_attribute *attr,
2213 char *buf)
2214{
2215 int status;
2216
2217 status = bluetooth_get_radiosw();
2218 if (status < 0)
2219 return status;
2220
2221 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2222}
2223
2224static ssize_t bluetooth_enable_store(struct device *dev,
2225 struct device_attribute *attr,
2226 const char *buf, size_t count)
2227{
2228 unsigned long t;
2229 int res;
2230
2231 if (parse_strtoul(buf, 1, &t))
2232 return -EINVAL;
2233
2234 res = bluetooth_set_radiosw(t);
2235
2236 return (res) ? res : count;
2237}
2238
2239static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002240 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002241 bluetooth_enable_show, bluetooth_enable_store);
2242
2243/* --------------------------------------------------------------------- */
2244
2245static struct attribute *bluetooth_attributes[] = {
2246 &dev_attr_bluetooth_enable.attr,
2247 NULL
2248};
2249
2250static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002251 .attrs = bluetooth_attributes,
2252};
2253
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002254static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002256 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002257 int status = 0;
2258
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002259 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2260
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002261 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002262
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002263 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2264 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002265 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002266 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002268 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2269 str_supported(tp_features.bluetooth),
2270 status);
2271
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002272 if (tp_features.bluetooth) {
2273 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2274 /* no bluetooth hardware present in system */
2275 tp_features.bluetooth = 0;
2276 dbg_printk(TPACPI_DBG_INIT,
2277 "bluetooth hardware not installed\n");
2278 } else {
2279 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2280 &bluetooth_attr_group);
2281 if (res)
2282 return res;
2283 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002284 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002285
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002286 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002289static void bluetooth_exit(void)
2290{
2291 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2292 &bluetooth_attr_group);
2293}
2294
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002295static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 int status;
2298
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002299 if (!tp_features.bluetooth)
2300 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002302 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2303 return -EIO;
2304
2305 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2306}
2307
2308static int bluetooth_set_radiosw(int radio_on)
2309{
2310 int status;
2311
2312 if (!tp_features.bluetooth)
2313 return -ENODEV;
2314
2315 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2316 return -EIO;
2317 if (radio_on)
2318 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2319 else
2320 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2321 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2322 return -EIO;
2323
2324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002327/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002328static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002331 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002333 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002336 len += sprintf(p + len, "status:\t\t%s\n",
2337 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 len += sprintf(p + len, "commands:\tenable, disable\n");
2339 }
2340
2341 return len;
2342}
2343
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002344static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002348 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002349 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 while ((cmd = next_cmd(&buf))) {
2352 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002353 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002355 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 } else
2357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return 0;
2361}
2362
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002363static struct ibm_struct bluetooth_driver_data = {
2364 .name = "bluetooth",
2365 .read = bluetooth_read,
2366 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002367 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002368};
2369
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002370/*************************************************************************
2371 * Wan subdriver
2372 */
2373
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002374enum {
2375 /* ACPI GWAN/SWAN bits */
2376 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2377 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2378 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2379};
2380
2381static int wan_get_radiosw(void);
2382static int wan_set_radiosw(int radio_on);
2383
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002384/* sysfs wan enable ---------------------------------------------------- */
2385static ssize_t wan_enable_show(struct device *dev,
2386 struct device_attribute *attr,
2387 char *buf)
2388{
2389 int status;
2390
2391 status = wan_get_radiosw();
2392 if (status < 0)
2393 return status;
2394
2395 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2396}
2397
2398static ssize_t wan_enable_store(struct device *dev,
2399 struct device_attribute *attr,
2400 const char *buf, size_t count)
2401{
2402 unsigned long t;
2403 int res;
2404
2405 if (parse_strtoul(buf, 1, &t))
2406 return -EINVAL;
2407
2408 res = wan_set_radiosw(t);
2409
2410 return (res) ? res : count;
2411}
2412
2413static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002414 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002415 wan_enable_show, wan_enable_store);
2416
2417/* --------------------------------------------------------------------- */
2418
2419static struct attribute *wan_attributes[] = {
2420 &dev_attr_wan_enable.attr,
2421 NULL
2422};
2423
2424static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002425 .attrs = wan_attributes,
2426};
2427
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002428static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002429{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002430 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002431 int status = 0;
2432
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002433 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2434
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002435 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002436
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002437 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002438 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002439
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002440 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2441 str_supported(tp_features.wan),
2442 status);
2443
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002444 if (tp_features.wan) {
2445 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2446 /* no wan hardware present in system */
2447 tp_features.wan = 0;
2448 dbg_printk(TPACPI_DBG_INIT,
2449 "wan hardware not installed\n");
2450 } else {
2451 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2452 &wan_attr_group);
2453 if (res)
2454 return res;
2455 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002456 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002457
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002458 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002459}
2460
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002461static void wan_exit(void)
2462{
2463 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2464 &wan_attr_group);
2465}
2466
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002467static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002468{
2469 int status;
2470
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002471 if (!tp_features.wan)
2472 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002473
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002474 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2475 return -EIO;
2476
2477 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2478}
2479
2480static int wan_set_radiosw(int radio_on)
2481{
2482 int status;
2483
2484 if (!tp_features.wan)
2485 return -ENODEV;
2486
2487 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2488 return -EIO;
2489 if (radio_on)
2490 status |= TP_ACPI_WANCARD_RADIOSSW;
2491 else
2492 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2493 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2494 return -EIO;
2495
2496 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002497}
2498
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002499/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002500static int wan_read(char *p)
2501{
2502 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002503 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002504
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002505 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002506 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002507 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002508 len += sprintf(p + len, "status:\t\t%s\n",
2509 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002510 len += sprintf(p + len, "commands:\tenable, disable\n");
2511 }
2512
2513 return len;
2514}
2515
2516static int wan_write(char *buf)
2517{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002518 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002519
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002520 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002521 return -ENODEV;
2522
2523 while ((cmd = next_cmd(&buf))) {
2524 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002525 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002526 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002527 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002528 } else
2529 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002530 }
2531
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002532 return 0;
2533}
2534
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002535static struct ibm_struct wan_driver_data = {
2536 .name = "wan",
2537 .read = wan_read,
2538 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002539 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002540 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002541};
2542
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002543/*************************************************************************
2544 * Video subdriver
2545 */
2546
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002547enum video_access_mode {
2548 TPACPI_VIDEO_NONE = 0,
2549 TPACPI_VIDEO_570, /* 570 */
2550 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2551 TPACPI_VIDEO_NEW, /* all others */
2552};
2553
2554enum { /* video status flags, based on VIDEO_570 */
2555 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2556 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2557 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2558};
2559
2560enum { /* TPACPI_VIDEO_570 constants */
2561 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2562 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2563 * video_status_flags */
2564 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2565 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2566};
2567
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002568static enum video_access_mode video_supported;
2569static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002570
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002571static int video_autosw_get(void);
2572static int video_autosw_set(int enable);
2573
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002574TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002575 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2576 "\\_SB.PCI0.VID0", /* 770e */
2577 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2578 "\\_SB.PCI0.AGP.VID", /* all others */
2579 ); /* R30, R31 */
2580
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002581TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002582
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002583static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002585 int ivga;
2586
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002587 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2588
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002589 TPACPI_ACPIHANDLE_INIT(vid);
2590 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002591
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002592 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2593 /* G41, assume IVGA doesn't change */
2594 vid_handle = vid2_handle;
2595
2596 if (!vid_handle)
2597 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002598 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002599 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2600 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002601 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002602 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2603 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002604 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002605 else
2606 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002607 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002609 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2610 str_supported(video_supported != TPACPI_VIDEO_NONE),
2611 video_supported);
2612
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002613 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002616static void video_exit(void)
2617{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002618 dbg_printk(TPACPI_DBG_EXIT,
2619 "restoring original video autoswitch mode\n");
2620 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002621 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002622 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002623}
2624
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002625static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
2627 int status = 0;
2628 int i;
2629
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002630 switch (video_supported) {
2631 case TPACPI_VIDEO_570:
2632 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2633 TP_ACPI_VIDEO_570_PHSCMD))
2634 return -EIO;
2635 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2636 break;
2637 case TPACPI_VIDEO_770:
2638 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2639 return -EIO;
2640 if (i)
2641 status |= TP_ACPI_VIDEO_S_LCD;
2642 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2643 return -EIO;
2644 if (i)
2645 status |= TP_ACPI_VIDEO_S_CRT;
2646 break;
2647 case TPACPI_VIDEO_NEW:
2648 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2649 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2650 return -EIO;
2651 if (i)
2652 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002654 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2655 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2656 return -EIO;
2657 if (i)
2658 status |= TP_ACPI_VIDEO_S_LCD;
2659 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2660 return -EIO;
2661 if (i)
2662 status |= TP_ACPI_VIDEO_S_DVI;
2663 break;
2664 default:
2665 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 return status;
2669}
2670
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002671static int video_outputsw_set(int status)
2672{
2673 int autosw;
2674 int res = 0;
2675
2676 switch (video_supported) {
2677 case TPACPI_VIDEO_570:
2678 res = acpi_evalf(NULL, NULL,
2679 "\\_SB.PHS2", "vdd",
2680 TP_ACPI_VIDEO_570_PHS2CMD,
2681 status | TP_ACPI_VIDEO_570_PHS2SET);
2682 break;
2683 case TPACPI_VIDEO_770:
2684 autosw = video_autosw_get();
2685 if (autosw < 0)
2686 return autosw;
2687
2688 res = video_autosw_set(1);
2689 if (res)
2690 return res;
2691 res = acpi_evalf(vid_handle, NULL,
2692 "ASWT", "vdd", status * 0x100, 0);
2693 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002694 printk(TPACPI_ERR
2695 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002696 return -EIO;
2697 }
2698 break;
2699 case TPACPI_VIDEO_NEW:
2700 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002701 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002702 break;
2703 default:
2704 return -ENOSYS;
2705 }
2706
2707 return (res)? 0 : -EIO;
2708}
2709
2710static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002712 int autosw = 0;
2713
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002714 switch (video_supported) {
2715 case TPACPI_VIDEO_570:
2716 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2717 return -EIO;
2718 break;
2719 case TPACPI_VIDEO_770:
2720 case TPACPI_VIDEO_NEW:
2721 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2722 return -EIO;
2723 break;
2724 default:
2725 return -ENOSYS;
2726 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002727
2728 return autosw & 1;
2729}
2730
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002731static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002732{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002733 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002734 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002735 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002736}
2737
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002738static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002739{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002740 int autosw = video_autosw_get();
2741 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002742
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002743 if (autosw < 0)
2744 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002745
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002746 switch (video_supported) {
2747 case TPACPI_VIDEO_570:
2748 res = video_autosw_set(1);
2749 if (res)
2750 return res;
2751 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2752 break;
2753 case TPACPI_VIDEO_770:
2754 case TPACPI_VIDEO_NEW:
2755 res = video_autosw_set(1);
2756 if (res)
2757 return res;
2758 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2759 break;
2760 default:
2761 return -ENOSYS;
2762 }
2763 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002764 printk(TPACPI_ERR
2765 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002766 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002767 }
2768
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002769 return (res)? 0 : -EIO;
2770}
2771
2772static int video_expand_toggle(void)
2773{
2774 switch (video_supported) {
2775 case TPACPI_VIDEO_570:
2776 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2777 0 : -EIO;
2778 case TPACPI_VIDEO_770:
2779 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2780 0 : -EIO;
2781 case TPACPI_VIDEO_NEW:
2782 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2783 0 : -EIO;
2784 default:
2785 return -ENOSYS;
2786 }
2787 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002788}
2789
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002790static int video_read(char *p)
2791{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002792 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002793 int len = 0;
2794
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002795 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002796 len += sprintf(p + len, "status:\t\tnot supported\n");
2797 return len;
2798 }
2799
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002800 status = video_outputsw_get();
2801 if (status < 0)
2802 return status;
2803
2804 autosw = video_autosw_get();
2805 if (autosw < 0)
2806 return autosw;
2807
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002808 len += sprintf(p + len, "status:\t\tsupported\n");
2809 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2810 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002811 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002812 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2813 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2814 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2815 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002816 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002817 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2818 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2819 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2820
2821 return len;
2822}
2823
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002824static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
2826 char *cmd;
2827 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002828 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002830 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002831 return -ENODEV;
2832
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002833 enable = 0;
2834 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 while ((cmd = next_cmd(&buf))) {
2837 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002838 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002840 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002842 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002844 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002845 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002846 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002847 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002848 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002849 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002850 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002852 res = video_autosw_set(1);
2853 if (res)
2854 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002856 res = video_autosw_set(0);
2857 if (res)
2858 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002860 res = video_outputsw_cycle();
2861 if (res)
2862 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002864 res = video_expand_toggle();
2865 if (res)
2866 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 } else
2868 return -EINVAL;
2869 }
2870
2871 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002872 status = video_outputsw_get();
2873 if (status < 0)
2874 return status;
2875 res = video_outputsw_set((status & ~disable) | enable);
2876 if (res)
2877 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
2879
2880 return 0;
2881}
2882
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002883static struct ibm_struct video_driver_data = {
2884 .name = "video",
2885 .read = video_read,
2886 .write = video_write,
2887 .exit = video_exit,
2888};
2889
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002890/*************************************************************************
2891 * Light (thinklight) subdriver
2892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002894TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2895TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002896
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002897static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002899 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2900
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002901 TPACPI_ACPIHANDLE_INIT(ledb);
2902 TPACPI_ACPIHANDLE_INIT(lght);
2903 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002904
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002905 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002906 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002907
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002908 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002909 /* light status not supported on
2910 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002911 tp_features.light_status =
2912 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002914 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002915 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002916
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002917 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
2919
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002920static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
2922 int len = 0;
2923 int status = 0;
2924
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002925 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002926 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002927 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002928 len += sprintf(p + len, "status:\t\tunknown\n");
2929 len += sprintf(p + len, "commands:\ton, off\n");
2930 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2932 return -EIO;
2933 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002934 len += sprintf(p + len, "commands:\ton, off\n");
2935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937 return len;
2938}
2939
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002940static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
2942 int cmos_cmd, lght_cmd;
2943 char *cmd;
2944 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002945
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002946 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002947 return -ENODEV;
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 while ((cmd = next_cmd(&buf))) {
2950 if (strlencmp(cmd, "on") == 0) {
2951 cmos_cmd = 0x0c;
2952 lght_cmd = 1;
2953 } else if (strlencmp(cmd, "off") == 0) {
2954 cmos_cmd = 0x0d;
2955 lght_cmd = 0;
2956 } else
2957 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002960 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2961 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (!success)
2963 return -EIO;
2964 }
2965
2966 return 0;
2967}
2968
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002969static struct ibm_struct light_driver_data = {
2970 .name = "light",
2971 .read = light_read,
2972 .write = light_write,
2973};
2974
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002975/*************************************************************************
2976 * Dock subdriver
2977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002979#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002980
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002981static void dock_notify(struct ibm_struct *ibm, u32 event);
2982static int dock_read(char *p);
2983static int dock_write(char *buf);
2984
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002985TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002986 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2987 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2988 "\\_SB.PCI.ISA.SLCE", /* 570 */
2989 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2990
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002991/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002992TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002993
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002994static const struct acpi_device_id ibm_pci_device_ids[] = {
2995 {PCI_ROOT_HID_STRING, 0},
2996 {"", 0},
2997};
2998
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002999static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3000 {
3001 .notify = dock_notify,
3002 .handle = &dock_handle,
3003 .type = ACPI_SYSTEM_NOTIFY,
3004 },
3005 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003006 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3007 * We just use it to get notifications of dock hotplug
3008 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003009 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003010 .notify = dock_notify,
3011 .handle = &pci_handle,
3012 .type = ACPI_SYSTEM_NOTIFY,
3013 },
3014};
3015
3016static struct ibm_struct dock_driver_data[2] = {
3017 {
3018 .name = "dock",
3019 .read = dock_read,
3020 .write = dock_write,
3021 .acpi = &ibm_dock_acpidriver[0],
3022 },
3023 {
3024 .name = "dock",
3025 .acpi = &ibm_dock_acpidriver[1],
3026 },
3027};
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029#define dock_docked() (_sta(dock_handle) & 1)
3030
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003031static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003032{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003033 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3034
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003035 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003036
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003037 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3038 str_supported(dock_handle != NULL));
3039
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003040 return (dock_handle)? 0 : 1;
3041}
3042
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003043static int __init dock_init2(struct ibm_init_struct *iibm)
3044{
3045 int dock2_needed;
3046
3047 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3048
3049 if (dock_driver_data[0].flags.acpi_driver_registered &&
3050 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003051 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003052 dock2_needed = (pci_handle != NULL);
3053 vdbg_printk(TPACPI_DBG_INIT,
3054 "dock PCI handler for the TP 570 is %s\n",
3055 str_supported(dock2_needed));
3056 } else {
3057 vdbg_printk(TPACPI_DBG_INIT,
3058 "dock subdriver part 2 not required\n");
3059 dock2_needed = 0;
3060 }
3061
3062 return (dock2_needed)? 0 : 1;
3063}
3064
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003065static void dock_notify(struct ibm_struct *ibm, u32 event)
3066{
3067 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003068 int pci = ibm->acpi->hid && ibm->acpi->device &&
3069 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003070 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003071
3072 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003073 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003074 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003075 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003076 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003077 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003078 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003079 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003080 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003081 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003082 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003083 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003084 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003085 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003086 }
Len Brown14e04fb2007-08-23 15:20:26 -04003087 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003088 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3089 ibm->acpi->device->dev.bus_id,
3090 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003091}
3092
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003093static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 int len = 0;
3096 int docked = dock_docked();
3097
3098 if (!dock_handle)
3099 len += sprintf(p + len, "status:\t\tnot supported\n");
3100 else if (!docked)
3101 len += sprintf(p + len, "status:\t\tundocked\n");
3102 else {
3103 len += sprintf(p + len, "status:\t\tdocked\n");
3104 len += sprintf(p + len, "commands:\tdock, undock\n");
3105 }
3106
3107 return len;
3108}
3109
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003110static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 char *cmd;
3113
3114 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003115 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 while ((cmd = next_cmd(&buf))) {
3118 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003119 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3120 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return -EIO;
3122 } else if (strlencmp(cmd, "dock") == 0) {
3123 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3124 return -EIO;
3125 } else
3126 return -EINVAL;
3127 }
3128
3129 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003132#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003134/*************************************************************************
3135 * Bay subdriver
3136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003138#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003139
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003140TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003141 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3142 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3143 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3144 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003145TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003146 "_EJ0", /* all others */
3147 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003148TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003149 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3150 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003151TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003152 "_EJ0", /* 770x */
3153 ); /* all others */
3154
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003155static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003157 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3158
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003159 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003160 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003161 TPACPI_ACPIHANDLE_INIT(bay_ej);
3162 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003163 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003164 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003165
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003166 tp_features.bay_status = bay_handle &&
3167 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3168 tp_features.bay_status2 = bay2_handle &&
3169 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003170
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003171 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3172 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3173 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3174 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003176 vdbg_printk(TPACPI_DBG_INIT,
3177 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003178 str_supported(tp_features.bay_status),
3179 str_supported(tp_features.bay_eject),
3180 str_supported(tp_features.bay_status2),
3181 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003182
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003183 return (tp_features.bay_status || tp_features.bay_eject ||
3184 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003187static void bay_notify(struct ibm_struct *ibm, u32 event)
3188{
Len Brown14e04fb2007-08-23 15:20:26 -04003189 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003190 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3191 ibm->acpi->device->dev.bus_id,
3192 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003193}
3194
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003195#define bay_occupied(b) (_sta(b##_handle) & 1)
3196
3197static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
3199 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003200 int occupied = bay_occupied(bay);
3201 int occupied2 = bay_occupied(bay2);
3202 int eject, eject2;
3203
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003204 len += sprintf(p + len, "status:\t\t%s\n",
3205 tp_features.bay_status ?
3206 (occupied ? "occupied" : "unoccupied") :
3207 "not supported");
3208 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003209 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3210 "occupied" : "unoccupied");
3211
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003212 eject = tp_features.bay_eject && occupied;
3213 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003214
3215 if (eject && eject2)
3216 len += sprintf(p + len, "commands:\teject, eject2\n");
3217 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003219 else if (eject2)
3220 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 return len;
3223}
3224
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003225static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 char *cmd;
3228
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003229 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003230 return -ENODEV;
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003233 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003234 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3235 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003236 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003237 strlencmp(cmd, "eject2") == 0) {
3238 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return -EIO;
3240 } else
3241 return -EINVAL;
3242 }
3243
3244 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003245}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003246
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003247static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3248 .notify = bay_notify,
3249 .handle = &bay_handle,
3250 .type = ACPI_SYSTEM_NOTIFY,
3251};
3252
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003253static struct ibm_struct bay_driver_data = {
3254 .name = "bay",
3255 .read = bay_read,
3256 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003257 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003258};
3259
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003260#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003262/*************************************************************************
3263 * CMOS subdriver
3264 */
3265
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003266/* sysfs cmos_command -------------------------------------------------- */
3267static ssize_t cmos_command_store(struct device *dev,
3268 struct device_attribute *attr,
3269 const char *buf, size_t count)
3270{
3271 unsigned long cmos_cmd;
3272 int res;
3273
3274 if (parse_strtoul(buf, 21, &cmos_cmd))
3275 return -EINVAL;
3276
3277 res = issue_thinkpad_cmos_command(cmos_cmd);
3278 return (res)? res : count;
3279}
3280
3281static struct device_attribute dev_attr_cmos_command =
3282 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3283
3284/* --------------------------------------------------------------------- */
3285
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003286static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003287{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003288 int res;
3289
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003290 vdbg_printk(TPACPI_DBG_INIT,
3291 "initializing cmos commands subdriver\n");
3292
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003293 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003294
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003295 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3296 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003297
3298 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3299 if (res)
3300 return res;
3301
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003302 return (cmos_handle)? 0 : 1;
3303}
3304
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003305static void cmos_exit(void)
3306{
3307 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3308}
3309
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003310static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
3312 int len = 0;
3313
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003314 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3315 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (!cmos_handle)
3317 len += sprintf(p + len, "status:\t\tnot supported\n");
3318 else {
3319 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003320 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
3322
3323 return len;
3324}
3325
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003326static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
3328 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003329 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
3331 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003332 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3333 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 /* cmos_cmd set */
3335 } else
3336 return -EINVAL;
3337
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003338 res = issue_thinkpad_cmos_command(cmos_cmd);
3339 if (res)
3340 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
3342
3343 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003344}
3345
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003346static struct ibm_struct cmos_driver_data = {
3347 .name = "cmos",
3348 .read = cmos_read,
3349 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003350 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003351};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003352
3353/*************************************************************************
3354 * LED subdriver
3355 */
3356
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003357enum led_access_mode {
3358 TPACPI_LED_NONE = 0,
3359 TPACPI_LED_570, /* 570 */
3360 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3361 TPACPI_LED_NEW, /* all others */
3362};
3363
3364enum { /* For TPACPI_LED_OLD */
3365 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3366 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3367 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3368};
3369
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003370static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003371
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003372TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003373 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3374 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003375 "LED", /* all others */
3376 ); /* R30, R31 */
3377
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003378static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003379{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003380 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3381
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003382 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003383
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003384 if (!led_handle)
3385 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003386 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003387 else if (strlencmp(led_path, "SLED") == 0)
3388 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003389 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003390 else if (strlencmp(led_path, "SYSL") == 0)
3391 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003392 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003393 else
3394 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003395 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003396
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003397 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3398 str_supported(led_supported), led_supported);
3399
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003400 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003401}
3402
3403#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3404
3405static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
3407 int len = 0;
3408
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003409 if (!led_supported) {
3410 len += sprintf(p + len, "status:\t\tnot supported\n");
3411 return len;
3412 }
3413 len += sprintf(p + len, "status:\t\tsupported\n");
3414
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003415 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003416 /* 570 */
3417 int i, status;
3418 for (i = 0; i < 8; i++) {
3419 if (!acpi_evalf(ec_handle,
3420 &status, "GLED", "dd", 1 << i))
3421 return -EIO;
3422 len += sprintf(p + len, "%d:\t\t%s\n",
3423 i, led_status(status));
3424 }
3425 }
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003428 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
3430 return len;
3431}
3432
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003433/* off, on, blink */
3434static const int led_sled_arg1[] = { 0, 1, 3 };
3435static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3436static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3437static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3438
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003439static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
3441 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003442 int led, ind, ret;
3443
3444 if (!led_supported)
3445 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003448 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 return -EINVAL;
3450
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003451 if (strstr(cmd, "off")) {
3452 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003454 ind = 1;
3455 } else if (strstr(cmd, "blink")) {
3456 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 } else
3458 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003459
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003460 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003461 /* 570 */
3462 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003464 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003466 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003467 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3468 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003469 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003470 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003471 ret = ec_write(TPACPI_LED_EC_HLBL,
3472 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003473 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003474 ret = ec_write(TPACPI_LED_EC_HLCL,
3475 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003476 if (ret < 0)
3477 return ret;
3478 } else {
3479 /* all others */
3480 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3481 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
3485
3486 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003487}
3488
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003489static struct ibm_struct led_driver_data = {
3490 .name = "led",
3491 .read = led_read,
3492 .write = led_write,
3493};
3494
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003495/*************************************************************************
3496 * Beep subdriver
3497 */
3498
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003499TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003500
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003501static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003502{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003503 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3504
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003505 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003506
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003507 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3508 str_supported(beep_handle != NULL));
3509
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003510 return (beep_handle)? 0 : 1;
3511}
3512
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003513static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
3515 int len = 0;
3516
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003517 if (!beep_handle)
3518 len += sprintf(p + len, "status:\t\tnot supported\n");
3519 else {
3520 len += sprintf(p + len, "status:\t\tsupported\n");
3521 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
3524 return len;
3525}
3526
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003527static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
3529 char *cmd;
3530 int beep_cmd;
3531
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003532 if (!beep_handle)
3533 return -ENODEV;
3534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003536 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3537 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 /* beep_cmd set */
3539 } else
3540 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003541 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 return -EIO;
3543 }
3544
3545 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003546}
3547
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003548static struct ibm_struct beep_driver_data = {
3549 .name = "beep",
3550 .read = beep_read,
3551 .write = beep_write,
3552};
3553
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003554/*************************************************************************
3555 * Thermal subdriver
3556 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003557
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003558enum thermal_access_mode {
3559 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3560 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3561 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3562 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3563 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3564};
3565
3566enum { /* TPACPI_THERMAL_TPEC_* */
3567 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3568 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3569 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3570};
3571
3572#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3573struct ibm_thermal_sensors_struct {
3574 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3575};
3576
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003577static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003578
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003579/* idx is zero-based */
3580static int thermal_get_sensor(int idx, s32 *value)
3581{
3582 int t;
3583 s8 tmp;
3584 char tmpi[5];
3585
3586 t = TP_EC_THERMAL_TMP0;
3587
3588 switch (thermal_read_mode) {
3589#if TPACPI_MAX_THERMAL_SENSORS >= 16
3590 case TPACPI_THERMAL_TPEC_16:
3591 if (idx >= 8 && idx <= 15) {
3592 t = TP_EC_THERMAL_TMP8;
3593 idx -= 8;
3594 }
3595 /* fallthrough */
3596#endif
3597 case TPACPI_THERMAL_TPEC_8:
3598 if (idx <= 7) {
3599 if (!acpi_ec_read(t + idx, &tmp))
3600 return -EIO;
3601 *value = tmp * 1000;
3602 return 0;
3603 }
3604 break;
3605
3606 case TPACPI_THERMAL_ACPI_UPDT:
3607 if (idx <= 7) {
3608 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3609 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3610 return -EIO;
3611 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3612 return -EIO;
3613 *value = (t - 2732) * 100;
3614 return 0;
3615 }
3616 break;
3617
3618 case TPACPI_THERMAL_ACPI_TMP07:
3619 if (idx <= 7) {
3620 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3621 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3622 return -EIO;
3623 if (t > 127 || t < -127)
3624 t = TP_EC_THERMAL_TMP_NA;
3625 *value = t * 1000;
3626 return 0;
3627 }
3628 break;
3629
3630 case TPACPI_THERMAL_NONE:
3631 default:
3632 return -ENOSYS;
3633 }
3634
3635 return -EINVAL;
3636}
3637
3638static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3639{
3640 int res, i;
3641 int n;
3642
3643 n = 8;
3644 i = 0;
3645
3646 if (!s)
3647 return -EINVAL;
3648
3649 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3650 n = 16;
3651
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003652 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003653 res = thermal_get_sensor(i, &s->temp[i]);
3654 if (res)
3655 return res;
3656 }
3657
3658 return n;
3659}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003660
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003661/* sysfs temp##_input -------------------------------------------------- */
3662
3663static ssize_t thermal_temp_input_show(struct device *dev,
3664 struct device_attribute *attr,
3665 char *buf)
3666{
3667 struct sensor_device_attribute *sensor_attr =
3668 to_sensor_dev_attr(attr);
3669 int idx = sensor_attr->index;
3670 s32 value;
3671 int res;
3672
3673 res = thermal_get_sensor(idx, &value);
3674 if (res)
3675 return res;
3676 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3677 return -ENXIO;
3678
3679 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3680}
3681
3682#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003683 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3684 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003685
3686static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3687 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3688 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3689 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3690 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3691 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3692 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3693 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3694 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3695 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3696 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3697 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3698 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3699 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3700 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3701 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3702 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3703};
3704
3705#define THERMAL_ATTRS(X) \
3706 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3707
3708static struct attribute *thermal_temp_input_attr[] = {
3709 THERMAL_ATTRS(8),
3710 THERMAL_ATTRS(9),
3711 THERMAL_ATTRS(10),
3712 THERMAL_ATTRS(11),
3713 THERMAL_ATTRS(12),
3714 THERMAL_ATTRS(13),
3715 THERMAL_ATTRS(14),
3716 THERMAL_ATTRS(15),
3717 THERMAL_ATTRS(0),
3718 THERMAL_ATTRS(1),
3719 THERMAL_ATTRS(2),
3720 THERMAL_ATTRS(3),
3721 THERMAL_ATTRS(4),
3722 THERMAL_ATTRS(5),
3723 THERMAL_ATTRS(6),
3724 THERMAL_ATTRS(7),
3725 NULL
3726};
3727
3728static const struct attribute_group thermal_temp_input16_group = {
3729 .attrs = thermal_temp_input_attr
3730};
3731
3732static const struct attribute_group thermal_temp_input8_group = {
3733 .attrs = &thermal_temp_input_attr[8]
3734};
3735
3736#undef THERMAL_SENSOR_ATTR_TEMP
3737#undef THERMAL_ATTRS
3738
3739/* --------------------------------------------------------------------- */
3740
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003741static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003742{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003743 u8 t, ta1, ta2;
3744 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003745 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003746 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003747
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003748 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3749
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003750 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003751
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003752 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003753 /*
3754 * Direct EC access mode: sensors at registers
3755 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3756 * non-implemented, thermal sensors return 0x80 when
3757 * not available
3758 */
3759
3760 ta1 = ta2 = 0;
3761 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003762 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003763 ta1 |= t;
3764 } else {
3765 ta1 = 0;
3766 break;
3767 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003768 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003769 ta2 |= t;
3770 } else {
3771 ta1 = 0;
3772 break;
3773 }
3774 }
3775 if (ta1 == 0) {
3776 /* This is sheer paranoia, but we handle it anyway */
3777 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003778 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003779 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003780 "falling back to ACPI TMPx access "
3781 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003782 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003783 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003784 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003785 "ThinkPad ACPI EC access misbehaving, "
3786 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003787 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003788 }
3789 } else {
3790 thermal_read_mode =
3791 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003792 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003793 }
3794 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003795 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3796 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003797 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003798 } else {
3799 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003800 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003801 }
3802 } else {
3803 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003804 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003805 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003806
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003807 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3808 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3809 thermal_read_mode);
3810
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003811 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003812 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003813 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003814 &thermal_temp_input16_group);
3815 if (res)
3816 return res;
3817 break;
3818 case TPACPI_THERMAL_TPEC_8:
3819 case TPACPI_THERMAL_ACPI_TMP07:
3820 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003821 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003822 &thermal_temp_input8_group);
3823 if (res)
3824 return res;
3825 break;
3826 case TPACPI_THERMAL_NONE:
3827 default:
3828 return 1;
3829 }
3830
3831 return 0;
3832}
3833
3834static void thermal_exit(void)
3835{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003836 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003837 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003838 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003839 &thermal_temp_input16_group);
3840 break;
3841 case TPACPI_THERMAL_TPEC_8:
3842 case TPACPI_THERMAL_ACPI_TMP07:
3843 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003844 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003845 &thermal_temp_input16_group);
3846 break;
3847 case TPACPI_THERMAL_NONE:
3848 default:
3849 break;
3850 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003851}
3852
3853static int thermal_read(char *p)
3854{
3855 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003856 int n, i;
3857 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003858
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003859 n = thermal_get_sensors(&t);
3860 if (unlikely(n < 0))
3861 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003862
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003863 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003864
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003865 if (n > 0) {
3866 for (i = 0; i < (n - 1); i++)
3867 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3868 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3869 } else
3870 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003871
3872 return len;
3873}
3874
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003875static struct ibm_struct thermal_driver_data = {
3876 .name = "thermal",
3877 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003878 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003879};
3880
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003881/*************************************************************************
3882 * EC Dump subdriver
3883 */
3884
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003885static u8 ecdump_regs[256];
3886
3887static int ecdump_read(char *p)
3888{
3889 int len = 0;
3890 int i, j;
3891 u8 v;
3892
3893 len += sprintf(p + len, "EC "
3894 " +00 +01 +02 +03 +04 +05 +06 +07"
3895 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3896 for (i = 0; i < 256; i += 16) {
3897 len += sprintf(p + len, "EC 0x%02x:", i);
3898 for (j = 0; j < 16; j++) {
3899 if (!acpi_ec_read(i + j, &v))
3900 break;
3901 if (v != ecdump_regs[i + j])
3902 len += sprintf(p + len, " *%02x", v);
3903 else
3904 len += sprintf(p + len, " %02x", v);
3905 ecdump_regs[i + j] = v;
3906 }
3907 len += sprintf(p + len, "\n");
3908 if (j != 16)
3909 break;
3910 }
3911
3912 /* These are way too dangerous to advertise openly... */
3913#if 0
3914 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3915 " (<offset> is 00-ff, <value> is 00-ff)\n");
3916 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3917 " (<offset> is 00-ff, <value> is 0-255)\n");
3918#endif
3919 return len;
3920}
3921
3922static int ecdump_write(char *buf)
3923{
3924 char *cmd;
3925 int i, v;
3926
3927 while ((cmd = next_cmd(&buf))) {
3928 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3929 /* i and v set */
3930 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3931 /* i and v set */
3932 } else
3933 return -EINVAL;
3934 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3935 if (!acpi_ec_write(i, v))
3936 return -EIO;
3937 } else
3938 return -EINVAL;
3939 }
3940
3941 return 0;
3942}
3943
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003944static struct ibm_struct ecdump_driver_data = {
3945 .name = "ecdump",
3946 .read = ecdump_read,
3947 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003948 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003949};
3950
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003951/*************************************************************************
3952 * Backlight/brightness subdriver
3953 */
Holger Macht8acb0252006-10-20 14:30:28 -07003954
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003955#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
3956
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003957static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003958static int brightness_offset = 0x31;
3959static int brightness_mode;
3960static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
3961
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003962static struct mutex brightness_mutex;
3963
3964/*
3965 * ThinkPads can read brightness from two places: EC 0x31, or
3966 * CMOS NVRAM byte 0x5E, bits 0-3.
3967 */
3968static int brightness_get(struct backlight_device *bd)
3969{
3970 u8 lec = 0, lcmos = 0, level = 0;
3971
3972 if (brightness_mode & 1) {
3973 if (!acpi_ec_read(brightness_offset, &lec))
3974 return -EIO;
3975 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3976 level = lec;
3977 };
3978 if (brightness_mode & 2) {
3979 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3980 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3981 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3982 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
3983 level = lcmos;
3984 }
3985
3986 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003987 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003988 "CMOS NVRAM (%u) and EC (%u) do not agree "
3989 "on display brightness level\n",
3990 (unsigned int) lcmos,
3991 (unsigned int) lec);
3992 return -EIO;
3993 }
3994
3995 return level;
3996}
3997
3998/* May return EINTR which can always be mapped to ERESTARTSYS */
3999static int brightness_set(int value)
4000{
4001 int cmos_cmd, inc, i, res;
4002 int current_value;
4003
4004 if (value > ((tp_features.bright_16levels)? 15 : 7))
4005 return -EINVAL;
4006
4007 res = mutex_lock_interruptible(&brightness_mutex);
4008 if (res < 0)
4009 return res;
4010
4011 current_value = brightness_get(NULL);
4012 if (current_value < 0) {
4013 res = current_value;
4014 goto errout;
4015 }
4016
4017 cmos_cmd = value > current_value ?
4018 TP_CMOS_BRIGHTNESS_UP :
4019 TP_CMOS_BRIGHTNESS_DOWN;
4020 inc = (value > current_value)? 1 : -1;
4021
4022 res = 0;
4023 for (i = current_value; i != value; i += inc) {
4024 if ((brightness_mode & 2) &&
4025 issue_thinkpad_cmos_command(cmos_cmd)) {
4026 res = -EIO;
4027 goto errout;
4028 }
4029 if ((brightness_mode & 1) &&
4030 !acpi_ec_write(brightness_offset, i + inc)) {
4031 res = -EIO;
4032 goto errout;;
4033 }
4034 }
4035
4036errout:
4037 mutex_unlock(&brightness_mutex);
4038 return res;
4039}
4040
4041/* sysfs backlight class ----------------------------------------------- */
4042
4043static int brightness_update_status(struct backlight_device *bd)
4044{
4045 /* it is the backlight class's job (caller) to handle
4046 * EINTR and other errors properly */
4047 return brightness_set(
4048 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4049 bd->props.power == FB_BLANK_UNBLANK) ?
4050 bd->props.brightness : 0);
4051}
Holger Macht8acb0252006-10-20 14:30:28 -07004052
Richard Purdie599a52d2007-02-10 23:07:48 +00004053static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004054 .get_brightness = brightness_get,
4055 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004056};
4057
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004058/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004059
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004060static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4061{
4062 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4063 union acpi_object *obj;
4064 int rc;
4065
4066 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4067 obj = (union acpi_object *)buffer.pointer;
4068 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004069 printk(TPACPI_ERR "Unknown BCLL data, "
4070 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004071 rc = 0;
4072 } else {
4073 rc = obj->package.count;
4074 }
4075 } else {
4076 return 0;
4077 }
4078
4079 kfree(buffer.pointer);
4080 return rc;
4081}
4082
4083static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4084 void *context, void **rv)
4085{
4086 char name[ACPI_PATH_SEGMENT_LENGTH];
4087 struct acpi_buffer buffer = { sizeof(name), &name };
4088
4089 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4090 !strncmp("BCLL", name, sizeof(name) - 1)) {
4091 if (tpacpi_query_bcll_levels(handle) == 16) {
4092 *rv = handle;
4093 return AE_CTRL_TERMINATE;
4094 } else {
4095 return AE_OK;
4096 }
4097 } else {
4098 return AE_OK;
4099 }
4100}
4101
4102static int __init brightness_check_levels(void)
4103{
4104 int status;
4105 void *found_node = NULL;
4106
4107 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004108 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004109 }
4110 if (!vid_handle)
4111 return 0;
4112
4113 /* Search for a BCLL package with 16 levels */
4114 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004115 brightness_find_bcll, NULL,
4116 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004117
4118 return (ACPI_SUCCESS(status) && found_node != NULL);
4119}
4120
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004121static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4122 void *context, void **rv)
4123{
4124 char name[ACPI_PATH_SEGMENT_LENGTH];
4125 struct acpi_buffer buffer = { sizeof(name), &name };
4126
4127 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4128 !strncmp("_BCL", name, sizeof(name) - 1)) {
4129 *rv = handle;
4130 return AE_CTRL_TERMINATE;
4131 } else {
4132 return AE_OK;
4133 }
4134}
4135
4136static int __init brightness_check_std_acpi_support(void)
4137{
4138 int status;
4139 void *found_node = NULL;
4140
4141 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004142 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004143 }
4144 if (!vid_handle)
4145 return 0;
4146
4147 /* Search for a _BCL method, but don't execute it */
4148 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004149 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004150
4151 return (ACPI_SUCCESS(status) && found_node != NULL);
4152}
4153
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004154static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004155{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004156 int b;
4157
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004158 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4159
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004160 mutex_init(&brightness_mutex);
4161
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004162 if (!brightness_enable) {
4163 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004164 "brightness support disabled by "
4165 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004166 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004167 } else if (brightness_enable > 1) {
4168 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004169 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004170 "standard ACPI backlight interface "
4171 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004172 return 1;
4173 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004174 }
4175
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004176 if (!brightness_mode) {
4177 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4178 brightness_mode = 2;
4179 else
4180 brightness_mode = 3;
4181
4182 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4183 brightness_mode);
4184 }
4185
4186 if (brightness_mode > 3)
4187 return -EINVAL;
4188
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004189 tp_features.bright_16levels =
4190 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4191 brightness_check_levels();
4192
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004193 b = brightness_get(NULL);
4194 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004195 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004196
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004197 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004198 printk(TPACPI_INFO
4199 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004200
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004201 ibm_backlight_device = backlight_device_register(
4202 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4203 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004204 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004205 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004206 return PTR_ERR(ibm_backlight_device);
4207 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004208 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004209
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004210 ibm_backlight_device->props.max_brightness =
4211 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004212 ibm_backlight_device->props.brightness = b;
4213 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004214
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004215 return 0;
4216}
4217
4218static void brightness_exit(void)
4219{
4220 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004221 vdbg_printk(TPACPI_DBG_EXIT,
4222 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004223 backlight_device_unregister(ibm_backlight_device);
4224 ibm_backlight_device = NULL;
4225 }
4226}
4227
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004228static int brightness_read(char *p)
4229{
4230 int len = 0;
4231 int level;
4232
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004233 level = brightness_get(NULL);
4234 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004235 len += sprintf(p + len, "level:\t\tunreadable\n");
4236 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004237 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004238 len += sprintf(p + len, "commands:\tup, down\n");
4239 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004240 " (<level> is 0-%d)\n",
4241 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004242 }
4243
4244 return len;
4245}
4246
4247static int brightness_write(char *buf)
4248{
4249 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004250 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004251 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004252 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004253
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004254 level = brightness_get(NULL);
4255 if (level < 0)
4256 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004257
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004258 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004259 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004260 if (level < max_level)
4261 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004262 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004263 if (level > 0)
4264 level--;
4265 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4266 level >= 0 && level <= max_level) {
4267 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004268 } else
4269 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004270 }
4271
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004272 /*
4273 * Now we know what the final level should be, so we try to set it.
4274 * Doing it this way makes the syscall restartable in case of EINTR
4275 */
4276 rc = brightness_set(level);
4277 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004278}
4279
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004280static struct ibm_struct brightness_driver_data = {
4281 .name = "brightness",
4282 .read = brightness_read,
4283 .write = brightness_write,
4284 .exit = brightness_exit,
4285};
4286
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004287/*************************************************************************
4288 * Volume subdriver
4289 */
4290
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004291static int volume_offset = 0x30;
4292
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004293static int volume_read(char *p)
4294{
4295 int len = 0;
4296 u8 level;
4297
4298 if (!acpi_ec_read(volume_offset, &level)) {
4299 len += sprintf(p + len, "level:\t\tunreadable\n");
4300 } else {
4301 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4302 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4303 len += sprintf(p + len, "commands:\tup, down, mute\n");
4304 len += sprintf(p + len, "commands:\tlevel <level>"
4305 " (<level> is 0-15)\n");
4306 }
4307
4308 return len;
4309}
4310
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004311static int volume_write(char *buf)
4312{
4313 int cmos_cmd, inc, i;
4314 u8 level, mute;
4315 int new_level, new_mute;
4316 char *cmd;
4317
4318 while ((cmd = next_cmd(&buf))) {
4319 if (!acpi_ec_read(volume_offset, &level))
4320 return -EIO;
4321 new_mute = mute = level & 0x40;
4322 new_level = level = level & 0xf;
4323
4324 if (strlencmp(cmd, "up") == 0) {
4325 if (mute)
4326 new_mute = 0;
4327 else
4328 new_level = level == 15 ? 15 : level + 1;
4329 } else if (strlencmp(cmd, "down") == 0) {
4330 if (mute)
4331 new_mute = 0;
4332 else
4333 new_level = level == 0 ? 0 : level - 1;
4334 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4335 new_level >= 0 && new_level <= 15) {
4336 /* new_level set */
4337 } else if (strlencmp(cmd, "mute") == 0) {
4338 new_mute = 0x40;
4339 } else
4340 return -EINVAL;
4341
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004342 if (new_level != level) {
4343 /* mute doesn't change */
4344
4345 cmos_cmd = (new_level > level) ?
4346 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004347 inc = new_level > level ? 1 : -1;
4348
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004349 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004350 !acpi_ec_write(volume_offset, level)))
4351 return -EIO;
4352
4353 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004354 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004355 !acpi_ec_write(volume_offset, i + inc))
4356 return -EIO;
4357
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004358 if (mute &&
4359 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4360 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004361 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004362 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004363 }
4364
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004365 if (new_mute != mute) {
4366 /* level doesn't change */
4367
4368 cmos_cmd = (new_mute) ?
4369 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004370
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004371 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004372 !acpi_ec_write(volume_offset, level + new_mute))
4373 return -EIO;
4374 }
4375 }
4376
4377 return 0;
4378}
4379
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004380static struct ibm_struct volume_driver_data = {
4381 .name = "volume",
4382 .read = volume_read,
4383 .write = volume_write,
4384};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004385
4386/*************************************************************************
4387 * Fan subdriver
4388 */
4389
4390/*
4391 * FAN ACCESS MODES
4392 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004393 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004394 * ACPI GFAN method: returns fan level
4395 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004396 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004397 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004398 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004399 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004400 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4401 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004402 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4403 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004404 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004405 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004406 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4407 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004408 *
4409 * Fan speed changes of any sort (including those caused by the
4410 * disengaged mode) are usually done slowly by the firmware as the
4411 * maximum ammount of fan duty cycle change per second seems to be
4412 * limited.
4413 *
4414 * Reading is not available if GFAN exists.
4415 * Writing is not available if SFAN exists.
4416 *
4417 * Bits
4418 * 7 automatic mode engaged;
4419 * (default operation mode of the ThinkPad)
4420 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004421 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004422 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004423 * the tachometer while the fan controller ramps up
4424 * the speed (which can take up to a few *minutes*).
4425 * Speeds up fan to 100% duty-cycle, which is far above
4426 * the standard RPM levels. It is not impossible that
4427 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004428 * 5-3 unused in some models. Extra bits for fan level
4429 * in others, but still useless as all values above
4430 * 7 map to the same speed as level 7 in these models.
4431 * 2-0 fan level (0..7 usually)
4432 * 0x00 = stop
4433 * 0x07 = max (set when temperatures critical)
4434 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004435 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004436 *
4437 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4438 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4439 * does so, its initial value is meaningless (0x07).
4440 *
4441 * For firmware bugs, refer to:
4442 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4443 *
4444 * ----
4445 *
4446 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4447 * Main fan tachometer reading (in RPM)
4448 *
4449 * This register is present on all ThinkPads with a new-style EC, and
4450 * it is known not to be present on the A21m/e, and T22, as there is
4451 * something else in offset 0x84 according to the ACPI DSDT. Other
4452 * ThinkPads from this same time period (and earlier) probably lack the
4453 * tachometer as well.
4454 *
4455 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4456 * was never fixed by IBM to report the EC firmware version string
4457 * probably support the tachometer (like the early X models), so
4458 * detecting it is quite hard. We need more data to know for sure.
4459 *
4460 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4461 * might result.
4462 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004463 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4464 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004465 *
4466 * For firmware bugs, refer to:
4467 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4468 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004469 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004470 * ThinkPad X31, X40, X41. Not available in the X60.
4471 *
4472 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4473 * high speed. ACPI DSDT seems to map these three speeds to levels
4474 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4475 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4476 *
4477 * The speeds are stored on handles
4478 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4479 *
4480 * There are three default speed sets, acessible as handles:
4481 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4482 *
4483 * ACPI DSDT switches which set is in use depending on various
4484 * factors.
4485 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004486 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004487 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4488 * but the ACPI tables just mention level 7.
4489 */
4490
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004491enum { /* Fan control constants */
4492 fan_status_offset = 0x2f, /* EC register 0x2f */
4493 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4494 * 0x84 must be read before 0x85 */
4495
4496 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4497 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4498
4499 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4500};
4501
4502enum fan_status_access_mode {
4503 TPACPI_FAN_NONE = 0, /* No fan status or control */
4504 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4505 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4506};
4507
4508enum fan_control_access_mode {
4509 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4510 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4511 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4512 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4513};
4514
4515enum fan_control_commands {
4516 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4517 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4518 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4519 * and also watchdog cmd */
4520};
4521
4522static int fan_control_allowed;
4523
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004524static enum fan_status_access_mode fan_status_access_mode;
4525static enum fan_control_access_mode fan_control_access_mode;
4526static enum fan_control_commands fan_control_commands;
4527
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004528static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004529static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004530static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004531
4532static struct mutex fan_mutex;
4533
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004534static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004535static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004536
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004537TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4538TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004539 "\\FSPD", /* 600e/x, 770e, 770x */
4540 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004541TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004542 "JFNS", /* 770x-JL */
4543 ); /* all others */
4544
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004545/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004546 * Call with fan_mutex held
4547 */
4548static void fan_update_desired_level(u8 status)
4549{
4550 if ((status &
4551 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4552 if (status > 7)
4553 fan_control_desired_level = 7;
4554 else
4555 fan_control_desired_level = status;
4556 }
4557}
4558
4559static int fan_get_status(u8 *status)
4560{
4561 u8 s;
4562
4563 /* TODO:
4564 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4565
4566 switch (fan_status_access_mode) {
4567 case TPACPI_FAN_RD_ACPI_GFAN:
4568 /* 570, 600e/x, 770e, 770x */
4569
4570 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4571 return -EIO;
4572
4573 if (likely(status))
4574 *status = s & 0x07;
4575
4576 break;
4577
4578 case TPACPI_FAN_RD_TPEC:
4579 /* all except 570, 600e/x, 770e, 770x */
4580 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4581 return -EIO;
4582
4583 if (likely(status))
4584 *status = s;
4585
4586 break;
4587
4588 default:
4589 return -ENXIO;
4590 }
4591
4592 return 0;
4593}
4594
4595static int fan_get_status_safe(u8 *status)
4596{
4597 int rc;
4598 u8 s;
4599
4600 if (mutex_lock_interruptible(&fan_mutex))
4601 return -ERESTARTSYS;
4602 rc = fan_get_status(&s);
4603 if (!rc)
4604 fan_update_desired_level(s);
4605 mutex_unlock(&fan_mutex);
4606
4607 if (status)
4608 *status = s;
4609
4610 return rc;
4611}
4612
4613static int fan_get_speed(unsigned int *speed)
4614{
4615 u8 hi, lo;
4616
4617 switch (fan_status_access_mode) {
4618 case TPACPI_FAN_RD_TPEC:
4619 /* all except 570, 600e/x, 770e, 770x */
4620 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4621 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4622 return -EIO;
4623
4624 if (likely(speed))
4625 *speed = (hi << 8) | lo;
4626
4627 break;
4628
4629 default:
4630 return -ENXIO;
4631 }
4632
4633 return 0;
4634}
4635
4636static int fan_set_level(int level)
4637{
4638 if (!fan_control_allowed)
4639 return -EPERM;
4640
4641 switch (fan_control_access_mode) {
4642 case TPACPI_FAN_WR_ACPI_SFAN:
4643 if (level >= 0 && level <= 7) {
4644 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4645 return -EIO;
4646 } else
4647 return -EINVAL;
4648 break;
4649
4650 case TPACPI_FAN_WR_ACPI_FANS:
4651 case TPACPI_FAN_WR_TPEC:
4652 if ((level != TP_EC_FAN_AUTO) &&
4653 (level != TP_EC_FAN_FULLSPEED) &&
4654 ((level < 0) || (level > 7)))
4655 return -EINVAL;
4656
4657 /* safety net should the EC not support AUTO
4658 * or FULLSPEED mode bits and just ignore them */
4659 if (level & TP_EC_FAN_FULLSPEED)
4660 level |= 7; /* safety min speed 7 */
4661 else if (level & TP_EC_FAN_FULLSPEED)
4662 level |= 4; /* safety min speed 4 */
4663
4664 if (!acpi_ec_write(fan_status_offset, level))
4665 return -EIO;
4666 else
4667 tp_features.fan_ctrl_status_undef = 0;
4668 break;
4669
4670 default:
4671 return -ENXIO;
4672 }
4673 return 0;
4674}
4675
4676static int fan_set_level_safe(int level)
4677{
4678 int rc;
4679
4680 if (!fan_control_allowed)
4681 return -EPERM;
4682
4683 if (mutex_lock_interruptible(&fan_mutex))
4684 return -ERESTARTSYS;
4685
4686 if (level == TPACPI_FAN_LAST_LEVEL)
4687 level = fan_control_desired_level;
4688
4689 rc = fan_set_level(level);
4690 if (!rc)
4691 fan_update_desired_level(level);
4692
4693 mutex_unlock(&fan_mutex);
4694 return rc;
4695}
4696
4697static int fan_set_enable(void)
4698{
4699 u8 s;
4700 int rc;
4701
4702 if (!fan_control_allowed)
4703 return -EPERM;
4704
4705 if (mutex_lock_interruptible(&fan_mutex))
4706 return -ERESTARTSYS;
4707
4708 switch (fan_control_access_mode) {
4709 case TPACPI_FAN_WR_ACPI_FANS:
4710 case TPACPI_FAN_WR_TPEC:
4711 rc = fan_get_status(&s);
4712 if (rc < 0)
4713 break;
4714
4715 /* Don't go out of emergency fan mode */
4716 if (s != 7) {
4717 s &= 0x07;
4718 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4719 }
4720
4721 if (!acpi_ec_write(fan_status_offset, s))
4722 rc = -EIO;
4723 else {
4724 tp_features.fan_ctrl_status_undef = 0;
4725 rc = 0;
4726 }
4727 break;
4728
4729 case TPACPI_FAN_WR_ACPI_SFAN:
4730 rc = fan_get_status(&s);
4731 if (rc < 0)
4732 break;
4733
4734 s &= 0x07;
4735
4736 /* Set fan to at least level 4 */
4737 s |= 4;
4738
4739 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004740 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004741 else
4742 rc = 0;
4743 break;
4744
4745 default:
4746 rc = -ENXIO;
4747 }
4748
4749 mutex_unlock(&fan_mutex);
4750 return rc;
4751}
4752
4753static int fan_set_disable(void)
4754{
4755 int rc;
4756
4757 if (!fan_control_allowed)
4758 return -EPERM;
4759
4760 if (mutex_lock_interruptible(&fan_mutex))
4761 return -ERESTARTSYS;
4762
4763 rc = 0;
4764 switch (fan_control_access_mode) {
4765 case TPACPI_FAN_WR_ACPI_FANS:
4766 case TPACPI_FAN_WR_TPEC:
4767 if (!acpi_ec_write(fan_status_offset, 0x00))
4768 rc = -EIO;
4769 else {
4770 fan_control_desired_level = 0;
4771 tp_features.fan_ctrl_status_undef = 0;
4772 }
4773 break;
4774
4775 case TPACPI_FAN_WR_ACPI_SFAN:
4776 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4777 rc = -EIO;
4778 else
4779 fan_control_desired_level = 0;
4780 break;
4781
4782 default:
4783 rc = -ENXIO;
4784 }
4785
4786
4787 mutex_unlock(&fan_mutex);
4788 return rc;
4789}
4790
4791static int fan_set_speed(int speed)
4792{
4793 int rc;
4794
4795 if (!fan_control_allowed)
4796 return -EPERM;
4797
4798 if (mutex_lock_interruptible(&fan_mutex))
4799 return -ERESTARTSYS;
4800
4801 rc = 0;
4802 switch (fan_control_access_mode) {
4803 case TPACPI_FAN_WR_ACPI_FANS:
4804 if (speed >= 0 && speed <= 65535) {
4805 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4806 speed, speed, speed))
4807 rc = -EIO;
4808 } else
4809 rc = -EINVAL;
4810 break;
4811
4812 default:
4813 rc = -ENXIO;
4814 }
4815
4816 mutex_unlock(&fan_mutex);
4817 return rc;
4818}
4819
4820static void fan_watchdog_reset(void)
4821{
4822 static int fan_watchdog_active;
4823
4824 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4825 return;
4826
4827 if (fan_watchdog_active)
4828 cancel_delayed_work(&fan_watchdog_task);
4829
4830 if (fan_watchdog_maxinterval > 0 &&
4831 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4832 fan_watchdog_active = 1;
4833 if (!schedule_delayed_work(&fan_watchdog_task,
4834 msecs_to_jiffies(fan_watchdog_maxinterval
4835 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004836 printk(TPACPI_ERR
4837 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004838 "watchdog will not trigger\n");
4839 }
4840 } else
4841 fan_watchdog_active = 0;
4842}
4843
4844static void fan_watchdog_fire(struct work_struct *ignored)
4845{
4846 int rc;
4847
4848 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4849 return;
4850
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004851 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004852 rc = fan_set_enable();
4853 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004854 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004855 "will try again later...\n", -rc);
4856 /* reschedule for later */
4857 fan_watchdog_reset();
4858 }
4859}
4860
4861/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004862 * SYSFS fan layout: hwmon compatible (device)
4863 *
4864 * pwm*_enable:
4865 * 0: "disengaged" mode
4866 * 1: manual mode
4867 * 2: native EC "auto" mode (recommended, hardware default)
4868 *
4869 * pwm*: set speed in manual mode, ignored otherwise.
4870 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4871 * interpolation.
4872 *
4873 * fan*_input: tachometer reading, RPM
4874 *
4875 *
4876 * SYSFS fan layout: extensions
4877 *
4878 * fan_watchdog (driver):
4879 * fan watchdog interval in seconds, 0 disables (default), max 120
4880 */
4881
4882/* sysfs fan pwm1_enable ----------------------------------------------- */
4883static ssize_t fan_pwm1_enable_show(struct device *dev,
4884 struct device_attribute *attr,
4885 char *buf)
4886{
4887 int res, mode;
4888 u8 status;
4889
4890 res = fan_get_status_safe(&status);
4891 if (res)
4892 return res;
4893
4894 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4895 if (status != fan_control_initial_status) {
4896 tp_features.fan_ctrl_status_undef = 0;
4897 } else {
4898 /* Return most likely status. In fact, it
4899 * might be the only possible status */
4900 status = TP_EC_FAN_AUTO;
4901 }
4902 }
4903
4904 if (status & TP_EC_FAN_FULLSPEED) {
4905 mode = 0;
4906 } else if (status & TP_EC_FAN_AUTO) {
4907 mode = 2;
4908 } else
4909 mode = 1;
4910
4911 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4912}
4913
4914static ssize_t fan_pwm1_enable_store(struct device *dev,
4915 struct device_attribute *attr,
4916 const char *buf, size_t count)
4917{
4918 unsigned long t;
4919 int res, level;
4920
4921 if (parse_strtoul(buf, 2, &t))
4922 return -EINVAL;
4923
4924 switch (t) {
4925 case 0:
4926 level = TP_EC_FAN_FULLSPEED;
4927 break;
4928 case 1:
4929 level = TPACPI_FAN_LAST_LEVEL;
4930 break;
4931 case 2:
4932 level = TP_EC_FAN_AUTO;
4933 break;
4934 case 3:
4935 /* reserved for software-controlled auto mode */
4936 return -ENOSYS;
4937 default:
4938 return -EINVAL;
4939 }
4940
4941 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03004942 if (res == -ENXIO)
4943 return -EINVAL;
4944 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004945 return res;
4946
4947 fan_watchdog_reset();
4948
4949 return count;
4950}
4951
4952static struct device_attribute dev_attr_fan_pwm1_enable =
4953 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4954 fan_pwm1_enable_show, fan_pwm1_enable_store);
4955
4956/* sysfs fan pwm1 ------------------------------------------------------ */
4957static ssize_t fan_pwm1_show(struct device *dev,
4958 struct device_attribute *attr,
4959 char *buf)
4960{
4961 int res;
4962 u8 status;
4963
4964 res = fan_get_status_safe(&status);
4965 if (res)
4966 return res;
4967
4968 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4969 if (status != fan_control_initial_status) {
4970 tp_features.fan_ctrl_status_undef = 0;
4971 } else {
4972 status = TP_EC_FAN_AUTO;
4973 }
4974 }
4975
4976 if ((status &
4977 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4978 status = fan_control_desired_level;
4979
4980 if (status > 7)
4981 status = 7;
4982
4983 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
4984}
4985
4986static ssize_t fan_pwm1_store(struct device *dev,
4987 struct device_attribute *attr,
4988 const char *buf, size_t count)
4989{
4990 unsigned long s;
4991 int rc;
4992 u8 status, newlevel;
4993
4994 if (parse_strtoul(buf, 255, &s))
4995 return -EINVAL;
4996
4997 /* scale down from 0-255 to 0-7 */
4998 newlevel = (s >> 5) & 0x07;
4999
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005000 if (mutex_lock_interruptible(&fan_mutex))
5001 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005002
5003 rc = fan_get_status(&status);
5004 if (!rc && (status &
5005 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5006 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005007 if (rc == -ENXIO)
5008 rc = -EINVAL;
5009 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005010 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005011 fan_watchdog_reset();
5012 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005013 }
5014
5015 mutex_unlock(&fan_mutex);
5016 return (rc)? rc : count;
5017}
5018
5019static struct device_attribute dev_attr_fan_pwm1 =
5020 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5021 fan_pwm1_show, fan_pwm1_store);
5022
5023/* sysfs fan fan1_input ------------------------------------------------ */
5024static ssize_t fan_fan1_input_show(struct device *dev,
5025 struct device_attribute *attr,
5026 char *buf)
5027{
5028 int res;
5029 unsigned int speed;
5030
5031 res = fan_get_speed(&speed);
5032 if (res < 0)
5033 return res;
5034
5035 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5036}
5037
5038static struct device_attribute dev_attr_fan_fan1_input =
5039 __ATTR(fan1_input, S_IRUGO,
5040 fan_fan1_input_show, NULL);
5041
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005042/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005043static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5044 char *buf)
5045{
5046 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5047}
5048
5049static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5050 const char *buf, size_t count)
5051{
5052 unsigned long t;
5053
5054 if (parse_strtoul(buf, 120, &t))
5055 return -EINVAL;
5056
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005057 if (!fan_control_allowed)
5058 return -EPERM;
5059
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005060 fan_watchdog_maxinterval = t;
5061 fan_watchdog_reset();
5062
5063 return count;
5064}
5065
5066static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5067 fan_fan_watchdog_show, fan_fan_watchdog_store);
5068
5069/* --------------------------------------------------------------------- */
5070static struct attribute *fan_attributes[] = {
5071 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5072 &dev_attr_fan_fan1_input.attr,
5073 NULL
5074};
5075
5076static const struct attribute_group fan_attr_group = {
5077 .attrs = fan_attributes,
5078};
5079
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005080static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005081{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005082 int rc;
5083
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005084 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5085
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005086 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005087 fan_status_access_mode = TPACPI_FAN_NONE;
5088 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005089 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005090 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005091 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005092 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005093
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005094 TPACPI_ACPIHANDLE_INIT(fans);
5095 TPACPI_ACPIHANDLE_INIT(gfan);
5096 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005097
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005098 if (gfan_handle) {
5099 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005100 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005101 } else {
5102 /* all other ThinkPads: note that even old-style
5103 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005104 if (likely(acpi_ec_read(fan_status_offset,
5105 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005106 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005107
5108 /* In some ThinkPads, neither the EC nor the ACPI
5109 * DSDT initialize the fan status, and it ends up
5110 * being set to 0x07 when it *could* be either
5111 * 0x07 or 0x80.
5112 *
5113 * Enable for TP-1Y (T43), TP-78 (R51e),
5114 * TP-76 (R52), TP-70 (T43, R52), which are known
5115 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005116 if (fan_control_initial_status == 0x07) {
5117 switch (thinkpad_id.ec_model) {
5118 case 0x5931: /* TP-1Y */
5119 case 0x3837: /* TP-78 */
5120 case 0x3637: /* TP-76 */
5121 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005122 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005123 "fan_init: initial fan status "
5124 "is unknown, assuming it is "
5125 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005126 tp_features.fan_ctrl_status_undef = 1;
5127 ;;
5128 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005129 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005130 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005131 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005132 "ThinkPad ACPI EC access misbehaving, "
5133 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005134 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005135 }
5136 }
5137
5138 if (sfan_handle) {
5139 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005140 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005141 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005142 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005143 } else {
5144 if (!gfan_handle) {
5145 /* gfan without sfan means no fan control */
5146 /* all other models implement TP EC 0x2f control */
5147
5148 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005149 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005150 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005151 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005152 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005153 TPACPI_FAN_CMD_SPEED |
5154 TPACPI_FAN_CMD_LEVEL |
5155 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005156 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005157 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005158 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005159 TPACPI_FAN_CMD_LEVEL |
5160 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005161 }
5162 }
5163 }
5164
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005165 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5166 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5167 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5168 fan_status_access_mode, fan_control_access_mode);
5169
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005170 /* fan control master switch */
5171 if (!fan_control_allowed) {
5172 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5173 fan_control_commands = 0;
5174 dbg_printk(TPACPI_DBG_INIT,
5175 "fan control features disabled by parameter\n");
5176 }
5177
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005178 /* update fan_control_desired_level */
5179 if (fan_status_access_mode != TPACPI_FAN_NONE)
5180 fan_get_status_safe(NULL);
5181
5182 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5183 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005184 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005185 &fan_attr_group);
5186 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005187 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005188 &driver_attr_fan_watchdog);
5189 if (rc < 0)
5190 return rc;
5191 return 0;
5192 } else
5193 return 1;
5194}
5195
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005196static void fan_exit(void)
5197{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005198 vdbg_printk(TPACPI_DBG_EXIT,
5199 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005200
5201 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005202 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005203 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5204 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005205
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005206 cancel_delayed_work(&fan_watchdog_task);
5207 flush_scheduled_work();
5208}
5209
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005210static int fan_read(char *p)
5211{
5212 int len = 0;
5213 int rc;
5214 u8 status;
5215 unsigned int speed = 0;
5216
5217 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005218 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005219 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005220 rc = fan_get_status_safe(&status);
5221 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005222 return rc;
5223
5224 len += sprintf(p + len, "status:\t\t%s\n"
5225 "level:\t\t%d\n",
5226 (status != 0) ? "enabled" : "disabled", status);
5227 break;
5228
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005229 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005230 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005231 rc = fan_get_status_safe(&status);
5232 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005233 return rc;
5234
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005235 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005236 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005237 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005238 else
5239 /* Return most likely status. In fact, it
5240 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005241 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005242 }
5243
5244 len += sprintf(p + len, "status:\t\t%s\n",
5245 (status != 0) ? "enabled" : "disabled");
5246
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005247 rc = fan_get_speed(&speed);
5248 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005249 return rc;
5250
5251 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5252
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005253 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005254 /* Disengaged mode takes precedence */
5255 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005256 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005257 len += sprintf(p + len, "level:\t\tauto\n");
5258 else
5259 len += sprintf(p + len, "level:\t\t%d\n", status);
5260 break;
5261
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005262 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005263 default:
5264 len += sprintf(p + len, "status:\t\tnot supported\n");
5265 }
5266
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005267 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005268 len += sprintf(p + len, "commands:\tlevel <level>");
5269
5270 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005271 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005272 len += sprintf(p + len, " (<level> is 0-7)\n");
5273 break;
5274
5275 default:
5276 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005277 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005278 break;
5279 }
5280 }
5281
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005282 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005283 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005284 "commands:\twatchdog <timeout> (<timeout> "
5285 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005286
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005287 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005288 len += sprintf(p + len, "commands:\tspeed <speed>"
5289 " (<speed> is 0-65535)\n");
5290
5291 return len;
5292}
5293
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005294static int fan_write_cmd_level(const char *cmd, int *rc)
5295{
5296 int level;
5297
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005298 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005299 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005300 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005301 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005302 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005303 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005304 return 0;
5305
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005306 *rc = fan_set_level_safe(level);
5307 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005308 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005309 "access mode %d", fan_control_access_mode);
5310
5311 return 1;
5312}
5313
5314static int fan_write_cmd_enable(const char *cmd, int *rc)
5315{
5316 if (strlencmp(cmd, "enable") != 0)
5317 return 0;
5318
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005319 *rc = fan_set_enable();
5320 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005321 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005322 "access mode %d", fan_control_access_mode);
5323
5324 return 1;
5325}
5326
5327static int fan_write_cmd_disable(const char *cmd, int *rc)
5328{
5329 if (strlencmp(cmd, "disable") != 0)
5330 return 0;
5331
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005332 *rc = fan_set_disable();
5333 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005334 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005335 "access mode %d", fan_control_access_mode);
5336
5337 return 1;
5338}
5339
5340static int fan_write_cmd_speed(const char *cmd, int *rc)
5341{
5342 int speed;
5343
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005344 /* TODO:
5345 * Support speed <low> <medium> <high> ? */
5346
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005347 if (sscanf(cmd, "speed %d", &speed) != 1)
5348 return 0;
5349
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005350 *rc = fan_set_speed(speed);
5351 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005352 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005353 "access mode %d", fan_control_access_mode);
5354
5355 return 1;
5356}
5357
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005358static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5359{
5360 int interval;
5361
5362 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5363 return 0;
5364
5365 if (interval < 0 || interval > 120)
5366 *rc = -EINVAL;
5367 else
5368 fan_watchdog_maxinterval = interval;
5369
5370 return 1;
5371}
5372
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005373static int fan_write(char *buf)
5374{
5375 char *cmd;
5376 int rc = 0;
5377
5378 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005379 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005380 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005381 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005382 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005383 fan_write_cmd_disable(cmd, &rc) ||
5384 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005385 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005386 fan_write_cmd_speed(cmd, &rc))
5387 )
5388 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005389 else if (!rc)
5390 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005391 }
5392
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005393 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005394}
5395
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005396static struct ibm_struct fan_driver_data = {
5397 .name = "fan",
5398 .read = fan_read,
5399 .write = fan_write,
5400 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005401};
5402
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005403/****************************************************************************
5404 ****************************************************************************
5405 *
5406 * Infrastructure
5407 *
5408 ****************************************************************************
5409 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005411/* sysfs name ---------------------------------------------------------- */
5412static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5413 struct device_attribute *attr,
5414 char *buf)
5415{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005416 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005417}
5418
5419static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5420 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5421
5422/* --------------------------------------------------------------------- */
5423
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005424/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005425static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005426
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005427/*
5428 * Module and infrastructure proble, init and exit handling
5429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005431static int force_load;
5432
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005433#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005434static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005435{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005436 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005437
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005438 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005439}
5440#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5441
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005442static void ibm_exit(struct ibm_struct *ibm)
5443{
5444 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5445
5446 list_del_init(&ibm->all_drivers);
5447
5448 if (ibm->flags.acpi_notify_installed) {
5449 dbg_printk(TPACPI_DBG_EXIT,
5450 "%s: acpi_remove_notify_handler\n", ibm->name);
5451 BUG_ON(!ibm->acpi);
5452 acpi_remove_notify_handler(*ibm->acpi->handle,
5453 ibm->acpi->type,
5454 dispatch_acpi_notify);
5455 ibm->flags.acpi_notify_installed = 0;
5456 ibm->flags.acpi_notify_installed = 0;
5457 }
5458
5459 if (ibm->flags.proc_created) {
5460 dbg_printk(TPACPI_DBG_EXIT,
5461 "%s: remove_proc_entry\n", ibm->name);
5462 remove_proc_entry(ibm->name, proc_dir);
5463 ibm->flags.proc_created = 0;
5464 }
5465
5466 if (ibm->flags.acpi_driver_registered) {
5467 dbg_printk(TPACPI_DBG_EXIT,
5468 "%s: acpi_bus_unregister_driver\n", ibm->name);
5469 BUG_ON(!ibm->acpi);
5470 acpi_bus_unregister_driver(ibm->acpi->driver);
5471 kfree(ibm->acpi->driver);
5472 ibm->acpi->driver = NULL;
5473 ibm->flags.acpi_driver_registered = 0;
5474 }
5475
5476 if (ibm->flags.init_called && ibm->exit) {
5477 ibm->exit();
5478 ibm->flags.init_called = 0;
5479 }
5480
5481 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5482}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005483
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005484static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485{
5486 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005487 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 struct proc_dir_entry *entry;
5489
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005490 BUG_ON(ibm == NULL);
5491
5492 INIT_LIST_HEAD(&ibm->all_drivers);
5493
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005494 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 return 0;
5496
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005497 dbg_printk(TPACPI_DBG_INIT,
5498 "probing for %s\n", ibm->name);
5499
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005500 if (iibm->init) {
5501 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005502 if (ret > 0)
5503 return 0; /* probe failed */
5504 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005506
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005507 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 }
5509
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005510 if (ibm->acpi) {
5511 if (ibm->acpi->hid) {
5512 ret = register_tpacpi_subdriver(ibm);
5513 if (ret)
5514 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005515 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005516
5517 if (ibm->acpi->notify) {
5518 ret = setup_acpi_notify(ibm);
5519 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005520 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005521 ibm->name);
5522 ret = 0;
5523 goto err_out;
5524 }
5525 if (ret < 0)
5526 goto err_out;
5527 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005528 }
5529
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005530 dbg_printk(TPACPI_DBG_INIT,
5531 "%s installed\n", ibm->name);
5532
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005533 if (ibm->read) {
5534 entry = create_proc_entry(ibm->name,
5535 S_IFREG | S_IRUGO | S_IWUSR,
5536 proc_dir);
5537 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005538 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005539 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005540 ret = -ENODEV;
5541 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005542 }
5543 entry->owner = THIS_MODULE;
5544 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005545 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005546 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005547 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005548 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005551 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5552
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005554
5555err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005556 dbg_printk(TPACPI_DBG_INIT,
5557 "%s: at error exit path with result %d\n",
5558 ibm->name, ret);
5559
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005560 ibm_exit(ibm);
5561 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562}
5563
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005564/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005566static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005567{
Jeff Garzik18552562007-10-03 15:15:40 -04005568 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005569 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005570
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005571 if (!tp)
5572 return;
5573
5574 memset(tp, 0, sizeof(*tp));
5575
5576 if (dmi_name_in_vendors("IBM"))
5577 tp->vendor = PCI_VENDOR_ID_IBM;
5578 else if (dmi_name_in_vendors("LENOVO"))
5579 tp->vendor = PCI_VENDOR_ID_LENOVO;
5580 else
5581 return;
5582
5583 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5584 GFP_KERNEL);
5585 if (!tp->bios_version_str)
5586 return;
5587 tp->bios_model = tp->bios_version_str[0]
5588 | (tp->bios_version_str[1] << 8);
5589
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005590 /*
5591 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5592 * X32 or newer, all Z series; Some models must have an
5593 * up-to-date BIOS or they will not be detected.
5594 *
5595 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5596 */
5597 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005598 if (sscanf(dev->name,
5599 "IBM ThinkPad Embedded Controller -[%17c",
5600 ec_fw_string) == 1) {
5601 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5602 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005603
5604 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5605 tp->ec_model = ec_fw_string[0]
5606 | (ec_fw_string[1] << 8);
5607 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005608 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005609 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005610
5611 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5612 GFP_KERNEL);
5613 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5614 kfree(tp->model_str);
5615 tp->model_str = NULL;
5616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617}
5618
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005619static int __init probe_for_thinkpad(void)
5620{
5621 int is_thinkpad;
5622
5623 if (acpi_disabled)
5624 return -ENODEV;
5625
5626 /*
5627 * Non-ancient models have better DMI tagging, but very old models
5628 * don't.
5629 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005630 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005631
5632 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005633 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005634 if (!ec_handle) {
5635 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005636 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005637 "Not yet supported ThinkPad detected!\n");
5638 return -ENODEV;
5639 }
5640
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005641 /*
5642 * Risks a regression on very old machines, but reduces potential
5643 * false positives a damn great deal
5644 */
5645 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005646 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005647
5648 if (!is_thinkpad && !force_load)
5649 return -ENODEV;
5650
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005651 return 0;
5652}
5653
5654
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005655/* Module init, exit, parameters */
5656
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005657static struct ibm_init_struct ibms_init[] __initdata = {
5658 {
5659 .init = thinkpad_acpi_driver_init,
5660 .data = &thinkpad_acpi_driver_data,
5661 },
5662 {
5663 .init = hotkey_init,
5664 .data = &hotkey_driver_data,
5665 },
5666 {
5667 .init = bluetooth_init,
5668 .data = &bluetooth_driver_data,
5669 },
5670 {
5671 .init = wan_init,
5672 .data = &wan_driver_data,
5673 },
5674 {
5675 .init = video_init,
5676 .data = &video_driver_data,
5677 },
5678 {
5679 .init = light_init,
5680 .data = &light_driver_data,
5681 },
5682#ifdef CONFIG_THINKPAD_ACPI_DOCK
5683 {
5684 .init = dock_init,
5685 .data = &dock_driver_data[0],
5686 },
5687 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005688 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005689 .data = &dock_driver_data[1],
5690 },
5691#endif
5692#ifdef CONFIG_THINKPAD_ACPI_BAY
5693 {
5694 .init = bay_init,
5695 .data = &bay_driver_data,
5696 },
5697#endif
5698 {
5699 .init = cmos_init,
5700 .data = &cmos_driver_data,
5701 },
5702 {
5703 .init = led_init,
5704 .data = &led_driver_data,
5705 },
5706 {
5707 .init = beep_init,
5708 .data = &beep_driver_data,
5709 },
5710 {
5711 .init = thermal_init,
5712 .data = &thermal_driver_data,
5713 },
5714 {
5715 .data = &ecdump_driver_data,
5716 },
5717 {
5718 .init = brightness_init,
5719 .data = &brightness_driver_data,
5720 },
5721 {
5722 .data = &volume_driver_data,
5723 },
5724 {
5725 .init = fan_init,
5726 .data = &fan_driver_data,
5727 },
5728};
5729
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005730static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5731{
5732 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005733 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005734
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005735 if (!kp || !kp->name || !val)
5736 return -EINVAL;
5737
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005738 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5739 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005740 WARN_ON(ibm == NULL);
5741
5742 if (!ibm || !ibm->name)
5743 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005744
5745 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5746 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005747 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005748 strcpy(ibms_init[i].param, val);
5749 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005750 return 0;
5751 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005752 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005753
5754 return -EINVAL;
5755}
5756
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005757module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005758MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005759 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005760
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005761module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005762MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005763
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005764module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005765MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005766 "Attempts to load the driver even on a "
5767 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005768
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005769module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005770MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005771 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005772
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005773module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005774MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005775 "Selects brightness control strategy: "
5776 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005777
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005778module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005779MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005780 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005781
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005782module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005783MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005784 "used for backwards compatibility with userspace, "
5785 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005786
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005787#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005788 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5789 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005790 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005791
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005792TPACPI_PARAM(hotkey);
5793TPACPI_PARAM(bluetooth);
5794TPACPI_PARAM(video);
5795TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005796#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005797TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005798#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005799#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005800TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005801#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005802TPACPI_PARAM(cmos);
5803TPACPI_PARAM(led);
5804TPACPI_PARAM(beep);
5805TPACPI_PARAM(ecdump);
5806TPACPI_PARAM(brightness);
5807TPACPI_PARAM(volume);
5808TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005809
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005810static void thinkpad_acpi_module_exit(void)
5811{
5812 struct ibm_struct *ibm, *itmp;
5813
5814 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5815
5816 list_for_each_entry_safe_reverse(ibm, itmp,
5817 &tpacpi_all_drivers,
5818 all_drivers) {
5819 ibm_exit(ibm);
5820 }
5821
5822 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5823
5824 if (tpacpi_inputdev) {
5825 if (tp_features.input_device_registered)
5826 input_unregister_device(tpacpi_inputdev);
5827 else
5828 input_free_device(tpacpi_inputdev);
5829 }
5830
5831 if (tpacpi_hwmon)
5832 hwmon_device_unregister(tpacpi_hwmon);
5833
5834 if (tp_features.sensors_pdev_attrs_registered)
5835 device_remove_file(&tpacpi_sensors_pdev->dev,
5836 &dev_attr_thinkpad_acpi_pdev_name);
5837 if (tpacpi_sensors_pdev)
5838 platform_device_unregister(tpacpi_sensors_pdev);
5839 if (tpacpi_pdev)
5840 platform_device_unregister(tpacpi_pdev);
5841
5842 if (tp_features.sensors_pdrv_attrs_registered)
5843 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5844 if (tp_features.platform_drv_attrs_registered)
5845 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5846
5847 if (tp_features.sensors_pdrv_registered)
5848 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5849
5850 if (tp_features.platform_drv_registered)
5851 platform_driver_unregister(&tpacpi_pdriver);
5852
5853 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005854 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005855
5856 kfree(thinkpad_id.bios_version_str);
5857 kfree(thinkpad_id.ec_version_str);
5858 kfree(thinkpad_id.model_str);
5859}
5860
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005861
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005862static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863{
5864 int ret, i;
5865
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005866 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5867
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005868 /* Parameter checking */
5869 if (hotkey_report_mode > 2)
5870 return -EINVAL;
5871
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005872 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005873
5874 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005875 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005876 if (ret) {
5877 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005878 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005881 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005882
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005883 TPACPI_ACPIHANDLE_INIT(ecrd);
5884 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005886 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005888 printk(TPACPI_ERR
5889 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005890 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 return -ENODEV;
5892 }
5893 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005894
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005895 ret = platform_driver_register(&tpacpi_pdriver);
5896 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005897 printk(TPACPI_ERR
5898 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005899 thinkpad_acpi_module_exit();
5900 return ret;
5901 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005902 tp_features.platform_drv_registered = 1;
5903
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005904 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5905 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005906 printk(TPACPI_ERR
5907 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005908 thinkpad_acpi_module_exit();
5909 return ret;
5910 }
5911 tp_features.sensors_pdrv_registered = 1;
5912
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005913 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005914 if (!ret) {
5915 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005916 ret = tpacpi_create_driver_attributes(
5917 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005918 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005919 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005920 printk(TPACPI_ERR
5921 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005922 thinkpad_acpi_module_exit();
5923 return ret;
5924 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005925 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005926
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005927
5928 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005929 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005930 NULL, 0);
5931 if (IS_ERR(tpacpi_pdev)) {
5932 ret = PTR_ERR(tpacpi_pdev);
5933 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005934 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005935 thinkpad_acpi_module_exit();
5936 return ret;
5937 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005938 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005939 TPACPI_HWMON_DRVR_NAME,
5940 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005941 if (IS_ERR(tpacpi_sensors_pdev)) {
5942 ret = PTR_ERR(tpacpi_sensors_pdev);
5943 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005944 printk(TPACPI_ERR
5945 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005946 thinkpad_acpi_module_exit();
5947 return ret;
5948 }
5949 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5950 &dev_attr_thinkpad_acpi_pdev_name);
5951 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005952 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005953 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005954 thinkpad_acpi_module_exit();
5955 return ret;
5956 }
5957 tp_features.sensors_pdev_attrs_registered = 1;
5958 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005959 if (IS_ERR(tpacpi_hwmon)) {
5960 ret = PTR_ERR(tpacpi_hwmon);
5961 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005962 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005963 thinkpad_acpi_module_exit();
5964 return ret;
5965 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03005966 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005967 tpacpi_inputdev = input_allocate_device();
5968 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005969 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005970 thinkpad_acpi_module_exit();
5971 return -ENOMEM;
5972 } else {
5973 /* Prepare input device, but don't register */
5974 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005975 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005976 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03005977 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5978 thinkpad_id.vendor :
5979 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005980 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5981 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5982 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005983 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5984 ret = ibm_init(&ibms_init[i]);
5985 if (ret >= 0 && *ibms_init[i].param)
5986 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005988 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 return ret;
5990 }
5991 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005992 ret = input_register_device(tpacpi_inputdev);
5993 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005994 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005995 thinkpad_acpi_module_exit();
5996 return ret;
5997 } else {
5998 tp_features.input_device_registered = 1;
5999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006001 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 return 0;
6003}
6004
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006005/* Please remove this in year 2009 */
6006MODULE_ALIAS("ibm_acpi");
6007
6008/*
6009 * DMI matching for module autoloading
6010 *
6011 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6012 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6013 *
6014 * Only models listed in thinkwiki will be supported, so add yours
6015 * if it is not there yet.
6016 */
6017#define IBM_BIOS_MODULE_ALIAS(__type) \
6018 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6019
6020/* Non-ancient thinkpads */
6021MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6022MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6023
6024/* Ancient thinkpad BIOSes have to be identified by
6025 * BIOS type or model number, and there are far less
6026 * BIOS types than model numbers... */
6027IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6028IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6029IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6030
6031MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006032MODULE_DESCRIPTION(TPACPI_DESC);
6033MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006034MODULE_LICENSE("GPL");
6035
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006036module_init(thinkpad_acpi_module_init);
6037module_exit(thinkpad_acpi_module_exit);