blob: 7ba1acad54025d1fd3cccf9ec9756345fe6bb27c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh6a2e2932008-01-08 13:02:56 -02006 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh1cee5cc2008-01-08 13:02:57 -020024#define TPACPI_VERSION "0.19"
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -020025#define TPACPI_SYSFS_VERSION 0x020200
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
57#include <linux/kthread.h>
58#include <linux/freezer.h>
59#include <linux/delay.h>
60
61#include <linux/nvram.h>
62#include <linux/proc_fs.h>
63#include <linux/sysfs.h>
64#include <linux/backlight.h>
65#include <linux/fb.h>
66#include <linux/platform_device.h>
67#include <linux/hwmon.h>
68#include <linux/hwmon-sysfs.h>
69#include <linux/input.h>
70#include <asm/uaccess.h>
71
72#include <linux/dmi.h>
73#include <linux/jiffies.h>
74#include <linux/workqueue.h>
75
76#include <acpi/acpi_drivers.h>
77#include <acpi/acnamesp.h>
78
79#include <linux/pci_ids.h>
80
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020081
82/* ThinkPad CMOS commands */
83#define TP_CMOS_VOLUME_DOWN 0
84#define TP_CMOS_VOLUME_UP 1
85#define TP_CMOS_VOLUME_MUTE 2
86#define TP_CMOS_BRIGHTNESS_UP 4
87#define TP_CMOS_BRIGHTNESS_DOWN 5
88
89/* NVRAM Addresses */
90enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
96};
97
98/* NVRAM bit masks */
99enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
113};
114
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200115/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200116#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200117
118/* Input IDs */
119#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120#define TPACPI_HKEY_INPUT_VERSION 0x4101
121
122
123/****************************************************************************
124 * Main driver
125 */
126
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200127#define TPACPI_NAME "thinkpad"
128#define TPACPI_DESC "ThinkPad ACPI Extras"
129#define TPACPI_FILE TPACPI_NAME "_acpi"
130#define TPACPI_URL "http://ibm-acpi.sf.net/"
131#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200133#define TPACPI_PROC_DIR "ibm"
134#define TPACPI_ACPI_EVENT_PREFIX "ibm"
135#define TPACPI_DRVR_NAME TPACPI_FILE
136#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200137
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200138#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200139
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200140/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200141#define TPACPI_LOG TPACPI_FILE ": "
142#define TPACPI_ERR KERN_ERR TPACPI_LOG
143#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144#define TPACPI_INFO KERN_INFO TPACPI_LOG
145#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200146
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200147#define TPACPI_DBG_ALL 0xffff
148#define TPACPI_DBG_ALL 0xffff
149#define TPACPI_DBG_INIT 0x0001
150#define TPACPI_DBG_EXIT 0x0002
151#define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200155#ifdef CONFIG_THINKPAD_ACPI_DEBUG
156#define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158static const char *str_supported(int is_supported);
159#else
160#define vdbg_printk(a_dbg_level, format, arg...)
161#endif
162
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200163#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200166
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200167
168/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200169 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200170 */
171
172struct ibm_struct;
173
174struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
177
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
182};
183
184struct ibm_struct {
185 char *name;
186
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200191 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200192
193 struct list_head all_drivers;
194
195 struct tp_acpi_drv_struct *acpi;
196
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
204};
205
206struct ibm_init_struct {
207 char param[32];
208
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
211};
212
213static struct {
214#ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219#endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
224 u32 light:1;
225 u32 light_status:1;
226 u32 bright_16levels:1;
227 u32 wan:1;
228 u32 fan_ctrl_status_undef:1;
229 u32 input_device_registered:1;
230 u32 platform_drv_registered:1;
231 u32 platform_drv_attrs_registered:1;
232 u32 sensors_pdrv_registered:1;
233 u32 sensors_pdrv_attrs_registered:1;
234 u32 sensors_pdev_attrs_registered:1;
235 u32 hotkey_poll_active:1;
236} tp_features;
237
238struct thinkpad_id_data {
239 unsigned int vendor; /* ThinkPad vendor:
240 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
241
242 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
243 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
244
245 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
246 u16 ec_model;
247
248 char *model_str;
249};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200250static struct thinkpad_id_data thinkpad_id;
251
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300252static enum {
253 TPACPI_LIFE_INIT = 0,
254 TPACPI_LIFE_RUNNING,
255 TPACPI_LIFE_EXITING,
256} tpacpi_lifecycle;
257
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200258static int experimental;
259static u32 dbg_level;
260
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300261/****************************************************************************
262 ****************************************************************************
263 *
264 * ACPI Helpers and device model
265 *
266 ****************************************************************************
267 ****************************************************************************/
268
269/*************************************************************************
270 * ACPI basic handles
271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300273static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200275#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 static acpi_handle object##_handle; \
277 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400278 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 static char *object##_paths[] = { paths }
280
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200281TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400282 "\\_SB.PCI.ISA.EC", /* 570 */
283 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
284 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
285 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
286 "\\_SB.PCI0.ICH3.EC0", /* R31 */
287 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300288 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200290TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
291TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200293TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
294 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400295 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
296 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300297 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400298
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200299TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400300 "^HKEY", /* R30, R31 */
301 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300302 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400303
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400304
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300305/*************************************************************************
306 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200307 */
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static int acpi_evalf(acpi_handle handle,
310 void *res, char *method, char *fmt, ...)
311{
312 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400313 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200314 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400315 struct acpi_buffer result, *resultp;
316 union acpi_object out_obj;
317 acpi_status status;
318 va_list ap;
319 char res_type;
320 int success;
321 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200324 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326 }
327
328 if (*fmt == 'q') {
329 quiet = 1;
330 fmt++;
331 } else
332 quiet = 0;
333
334 res_type = *(fmt++);
335
336 params.count = 0;
337 params.pointer = &in_objs[0];
338
339 va_start(ap, fmt);
340 while (*fmt) {
341 char c = *(fmt++);
342 switch (c) {
343 case 'd': /* int */
344 in_objs[params.count].integer.value = va_arg(ap, int);
345 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
346 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400347 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200349 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 "with invalid format character '%c'\n", c);
351 return 0;
352 }
353 }
354 va_end(ap);
355
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400356 if (res_type != 'v') {
357 result.length = sizeof(out_obj);
358 result.pointer = &out_obj;
359 resultp = &result;
360 } else
361 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400363 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400366 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (res)
368 *(int *)res = out_obj.integer.value;
369 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
370 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400371 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 success = status == AE_OK;
373 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400374 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200376 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 "with invalid format character '%c'\n", res_type);
378 return 0;
379 }
380
381 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200382 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 method, fmt0, status);
384
385 return success;
386}
387
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200388static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300389{
390 int v;
391
392 if (ecrd_handle) {
393 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
394 return 0;
395 *p = v;
396 } else {
397 if (ec_read(i, p) < 0)
398 return 0;
399 }
400
401 return 1;
402}
403
404static int acpi_ec_write(int i, u8 v)
405{
406 if (ecwr_handle) {
407 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
408 return 0;
409 } else {
410 if (ec_write(i, v) < 0)
411 return 0;
412 }
413
414 return 1;
415}
416
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200417#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300418static int _sta(acpi_handle handle)
419{
420 int status;
421
422 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
423 status = 0;
424
425 return status;
426}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200427#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300428
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300429static int issue_thinkpad_cmos_command(int cmos_cmd)
430{
431 if (!cmos_handle)
432 return -ENXIO;
433
434 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
435 return -EIO;
436
437 return 0;
438}
439
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300440/*************************************************************************
441 * ACPI device model
442 */
443
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200444#define TPACPI_ACPIHANDLE_INIT(object) \
445 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200446 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
447
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300448static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300449 acpi_handle *handle, acpi_handle parent,
450 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300451{
452 int i;
453 acpi_status status;
454
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300455 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
456 name);
457
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300458 for (i = 0; i < num_paths; i++) {
459 status = acpi_get_handle(parent, paths[i], handle);
460 if (ACPI_SUCCESS(status)) {
461 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300462 dbg_printk(TPACPI_DBG_INIT,
463 "Found ACPI handle %s for %s\n",
464 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300465 return;
466 }
467 }
468
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300469 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
470 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300471 *handle = NULL;
472}
473
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300474static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300475{
476 struct ibm_struct *ibm = data;
477
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300478 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
479 return;
480
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300481 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300482 return;
483
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300484 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300485}
486
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300487static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300488{
489 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300490 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300491
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300492 BUG_ON(!ibm->acpi);
493
494 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300495 return 0;
496
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300497 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300498 "setting up ACPI notify for %s\n", ibm->name);
499
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300500 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
501 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200502 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300503 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504 return -ENODEV;
505 }
506
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300507 acpi_driver_data(ibm->acpi->device) = ibm;
508 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200509 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300510 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300511
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300512 status = acpi_install_notify_handler(*ibm->acpi->handle,
513 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514 if (ACPI_FAILURE(status)) {
515 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200516 printk(TPACPI_NOTICE
517 "another device driver is already "
518 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200520 printk(TPACPI_ERR
521 "acpi_install_notify_handler(%s) failed: %d\n",
522 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523 }
524 return -ENODEV;
525 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300526 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300527 return 0;
528}
529
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300530static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300531{
532 return 0;
533}
534
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300535static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300537 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300538
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300539 dbg_printk(TPACPI_DBG_INIT,
540 "registering %s as an ACPI driver\n", ibm->name);
541
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300542 BUG_ON(!ibm->acpi);
543
544 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
545 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200546 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300547 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300548 }
549
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200550 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300551 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200552
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300553 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300554
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300555 rc = acpi_bus_register_driver(ibm->acpi->driver);
556 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200557 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200558 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300559 kfree(ibm->acpi->driver);
560 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300561 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300562 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300564 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300565}
566
567
568/****************************************************************************
569 ****************************************************************************
570 *
571 * Procfs Helpers
572 *
573 ****************************************************************************
574 ****************************************************************************/
575
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300576static int dispatch_procfs_read(char *page, char **start, off_t off,
577 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300578{
579 struct ibm_struct *ibm = data;
580 int len;
581
582 if (!ibm || !ibm->read)
583 return -EINVAL;
584
585 len = ibm->read(page);
586 if (len < 0)
587 return len;
588
589 if (len <= off + count)
590 *eof = 1;
591 *start = page + off;
592 len -= off;
593 if (len > count)
594 len = count;
595 if (len < 0)
596 len = 0;
597
598 return len;
599}
600
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300601static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200602 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300603 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300604{
605 struct ibm_struct *ibm = data;
606 char *kernbuf;
607 int ret;
608
609 if (!ibm || !ibm->write)
610 return -EINVAL;
611
612 kernbuf = kmalloc(count + 2, GFP_KERNEL);
613 if (!kernbuf)
614 return -ENOMEM;
615
616 if (copy_from_user(kernbuf, userbuf, count)) {
617 kfree(kernbuf);
618 return -EFAULT;
619 }
620
621 kernbuf[count] = 0;
622 strcat(kernbuf, ",");
623 ret = ibm->write(kernbuf);
624 if (ret == 0)
625 ret = count;
626
627 kfree(kernbuf);
628
629 return ret;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static char *next_cmd(char **cmds)
633{
634 char *start = *cmds;
635 char *end;
636
637 while ((end = strchr(start, ',')) && end == start)
638 start = end + 1;
639
640 if (!end)
641 return NULL;
642
643 *end = 0;
644 *cmds = end + 1;
645 return start;
646}
647
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300648
649/****************************************************************************
650 ****************************************************************************
651 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300652 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300653 *
654 ****************************************************************************
655 ****************************************************************************/
656
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300657static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300658static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700659static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300660static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300661static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200662static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300663
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200664static int tpacpi_suspend_handler(struct platform_device *pdev,
665 pm_message_t state)
666{
667 struct ibm_struct *ibm, *itmp;
668
669 list_for_each_entry_safe(ibm, itmp,
670 &tpacpi_all_drivers,
671 all_drivers) {
672 if (ibm->suspend)
673 (ibm->suspend)(state);
674 }
675
676 return 0;
677}
678
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300679static int tpacpi_resume_handler(struct platform_device *pdev)
680{
681 struct ibm_struct *ibm, *itmp;
682
683 list_for_each_entry_safe(ibm, itmp,
684 &tpacpi_all_drivers,
685 all_drivers) {
686 if (ibm->resume)
687 (ibm->resume)();
688 }
689
690 return 0;
691}
692
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300693static struct platform_driver tpacpi_pdriver = {
694 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200695 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300696 .owner = THIS_MODULE,
697 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200698 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300699 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300700};
701
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300702static struct platform_driver tpacpi_hwmon_pdriver = {
703 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200704 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300705 .owner = THIS_MODULE,
706 },
707};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300708
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300709/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300710 * sysfs support helpers
711 */
712
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200713struct attribute_set {
714 unsigned int members, max_members;
715 struct attribute_group group;
716};
717
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300718struct attribute_set_obj {
719 struct attribute_set s;
720 struct attribute *a;
721} __attribute__((packed));
722
723static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200724 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300725{
726 struct attribute_set_obj *sobj;
727
728 if (max_members == 0)
729 return NULL;
730
731 /* Allocates space for implicit NULL at the end too */
732 sobj = kzalloc(sizeof(struct attribute_set_obj) +
733 max_members * sizeof(struct attribute *),
734 GFP_KERNEL);
735 if (!sobj)
736 return NULL;
737 sobj->s.max_members = max_members;
738 sobj->s.group.attrs = &sobj->a;
739 sobj->s.group.name = name;
740
741 return &sobj->s;
742}
743
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200744#define destroy_attr_set(_set) \
745 kfree(_set);
746
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300747/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200748static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300749{
750 if (!s || !attr)
751 return -EINVAL;
752
753 if (s->members >= s->max_members)
754 return -ENOMEM;
755
756 s->group.attrs[s->members] = attr;
757 s->members++;
758
759 return 0;
760}
761
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200762static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300763 struct attribute **attr,
764 unsigned int count)
765{
766 int i, res;
767
768 for (i = 0; i < count; i++) {
769 res = add_to_attr_set(s, attr[i]);
770 if (res)
771 return res;
772 }
773
774 return 0;
775}
776
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200777static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300778{
779 sysfs_remove_group(kobj, &s->group);
780 destroy_attr_set(s);
781}
782
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200783#define register_attr_set_with_sysfs(_attr_set, _kobj) \
784 sysfs_create_group(_kobj, &_attr_set->group)
785
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300786static int parse_strtoul(const char *buf,
787 unsigned long max, unsigned long *value)
788{
789 char *endp;
790
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300791 while (*buf && isspace(*buf))
792 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300793 *value = simple_strtoul(buf, &endp, 0);
794 while (*endp && isspace(*endp))
795 endp++;
796 if (*endp || *value > max)
797 return -EINVAL;
798
799 return 0;
800}
801
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200802/*************************************************************************
803 * thinkpad-acpi driver attributes
804 */
805
806/* interface_version --------------------------------------------------- */
807static ssize_t tpacpi_driver_interface_version_show(
808 struct device_driver *drv,
809 char *buf)
810{
811 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
812}
813
814static DRIVER_ATTR(interface_version, S_IRUGO,
815 tpacpi_driver_interface_version_show, NULL);
816
817/* debug_level --------------------------------------------------------- */
818static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
819 char *buf)
820{
821 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
822}
823
824static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
825 const char *buf, size_t count)
826{
827 unsigned long t;
828
829 if (parse_strtoul(buf, 0xffff, &t))
830 return -EINVAL;
831
832 dbg_level = t;
833
834 return count;
835}
836
837static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
838 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
839
840/* version ------------------------------------------------------------- */
841static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
842 char *buf)
843{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200844 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
845 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200846}
847
848static DRIVER_ATTR(version, S_IRUGO,
849 tpacpi_driver_version_show, NULL);
850
851/* --------------------------------------------------------------------- */
852
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200853static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200854 &driver_attr_debug_level, &driver_attr_version,
855 &driver_attr_interface_version,
856};
857
858static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
859{
860 int i, res;
861
862 i = 0;
863 res = 0;
864 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
865 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
866 i++;
867 }
868
869 return res;
870}
871
872static void tpacpi_remove_driver_attributes(struct device_driver *drv)
873{
874 int i;
875
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200876 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200877 driver_remove_file(drv, tpacpi_driver_attributes[i]);
878}
879
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300880/****************************************************************************
881 ****************************************************************************
882 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300883 * Subdrivers
884 *
885 ****************************************************************************
886 ****************************************************************************/
887
888/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300889 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300890 */
891
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300892static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200894 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
895 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200897 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300898 (thinkpad_id.bios_version_str) ?
899 thinkpad_id.bios_version_str : "unknown",
900 (thinkpad_id.ec_version_str) ?
901 thinkpad_id.ec_version_str : "unknown");
902
903 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200904 printk(TPACPI_INFO "%s %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300905 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
906 "IBM" : ((thinkpad_id.vendor ==
907 PCI_VENDOR_ID_LENOVO) ?
908 "Lenovo" : "Unknown vendor"),
909 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return 0;
912}
913
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300914static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 int len = 0;
917
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200918 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
919 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return len;
922}
923
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300924static struct ibm_struct thinkpad_acpi_driver_data = {
925 .name = "driver",
926 .read = thinkpad_acpi_driver_read,
927};
928
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300929/*************************************************************************
930 * Hotkey subdriver
931 */
932
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200933enum { /* hot key scan codes (derived from ACPI DSDT) */
934 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
935 TP_ACPI_HOTKEYSCAN_FNF2,
936 TP_ACPI_HOTKEYSCAN_FNF3,
937 TP_ACPI_HOTKEYSCAN_FNF4,
938 TP_ACPI_HOTKEYSCAN_FNF5,
939 TP_ACPI_HOTKEYSCAN_FNF6,
940 TP_ACPI_HOTKEYSCAN_FNF7,
941 TP_ACPI_HOTKEYSCAN_FNF8,
942 TP_ACPI_HOTKEYSCAN_FNF9,
943 TP_ACPI_HOTKEYSCAN_FNF10,
944 TP_ACPI_HOTKEYSCAN_FNF11,
945 TP_ACPI_HOTKEYSCAN_FNF12,
946 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
947 TP_ACPI_HOTKEYSCAN_FNINSERT,
948 TP_ACPI_HOTKEYSCAN_FNDELETE,
949 TP_ACPI_HOTKEYSCAN_FNHOME,
950 TP_ACPI_HOTKEYSCAN_FNEND,
951 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
952 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
953 TP_ACPI_HOTKEYSCAN_FNSPACE,
954 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
955 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
956 TP_ACPI_HOTKEYSCAN_MUTE,
957 TP_ACPI_HOTKEYSCAN_THINKPAD,
958};
959
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200960enum { /* Keys available through NVRAM polling */
961 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
962 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
963};
964
965enum { /* Positions of some of the keys in hotkey masks */
966 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
967 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
968 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
969 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
970 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
971 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
972 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
973 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
974 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
975 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
976 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
977};
978
979enum { /* NVRAM to ACPI HKEY group map */
980 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
981 TP_ACPI_HKEY_ZOOM_MASK |
982 TP_ACPI_HKEY_DISPSWTCH_MASK |
983 TP_ACPI_HKEY_HIBERNATE_MASK,
984 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
985 TP_ACPI_HKEY_BRGHTDWN_MASK,
986 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
987 TP_ACPI_HKEY_VOLDWN_MASK |
988 TP_ACPI_HKEY_MUTE_MASK,
989};
990
991#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
992struct tp_nvram_state {
993 u16 thinkpad_toggle:1;
994 u16 zoom_toggle:1;
995 u16 display_toggle:1;
996 u16 thinklight_toggle:1;
997 u16 hibernate_toggle:1;
998 u16 displayexp_toggle:1;
999 u16 display_state:1;
1000 u16 brightness_toggle:1;
1001 u16 volume_toggle:1;
1002 u16 mute:1;
1003
1004 u8 brightness_level;
1005 u8 volume_level;
1006};
1007
1008static struct task_struct *tpacpi_hotkey_task;
1009static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1010static int hotkey_poll_freq = 10; /* Hz */
1011static struct mutex hotkey_thread_mutex;
1012static struct mutex hotkey_thread_data_mutex;
1013static unsigned int hotkey_config_change;
1014
1015#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1016
1017#define hotkey_source_mask 0U
1018
1019#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1020
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001021static struct mutex hotkey_mutex;
1022
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001023static enum { /* Reasons for waking up */
1024 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1025 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1026 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1027} hotkey_wakeup_reason;
1028
1029static int hotkey_autosleep_ack;
1030
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001031static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001032static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001033static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001034static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001035static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001036
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001037static unsigned int hotkey_report_mode;
1038
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001039static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001040
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001041static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001042
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001043#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1044#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001045 do { \
1046 mutex_lock(&hotkey_thread_data_mutex); \
1047 hotkey_config_change++; \
1048 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001049#define HOTKEY_CONFIG_CRITICAL_END \
1050 mutex_unlock(&hotkey_thread_data_mutex);
1051#else
1052#define HOTKEY_CONFIG_CRITICAL_START
1053#define HOTKEY_CONFIG_CRITICAL_END
1054#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1055
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001056static int hotkey_get_wlsw(int *status)
1057{
1058 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1059 return -EIO;
1060 return 0;
1061}
1062
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001063/*
1064 * Call with hotkey_mutex held
1065 */
1066static int hotkey_mask_get(void)
1067{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001068 u32 m = 0;
1069
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001070 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001071 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001072 return -EIO;
1073 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001074 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001075
1076 return 0;
1077}
1078
1079/*
1080 * Call with hotkey_mutex held
1081 */
1082static int hotkey_mask_set(u32 mask)
1083{
1084 int i;
1085 int rc = 0;
1086
1087 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001088 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001089 for (i = 0; i < 32; i++) {
1090 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001091 /* enable in firmware mask only keys not in NVRAM
1092 * mode, but enable the key in the cached hotkey_mask
1093 * regardless of mode, or the key will end up
1094 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001095 if (!acpi_evalf(hkey_handle,
1096 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001097 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001098 rc = -EIO;
1099 break;
1100 } else {
1101 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1102 }
1103 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001104 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001105
1106 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001107 if (!hotkey_mask_get() && !rc &&
1108 (hotkey_mask & ~hotkey_source_mask) !=
1109 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001110 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001111 "requested hot key mask 0x%08x, but "
1112 "firmware forced it to 0x%08x\n",
1113 mask, hotkey_mask);
1114 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001115 } else {
1116#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1117 HOTKEY_CONFIG_CRITICAL_START
1118 hotkey_mask = mask & hotkey_source_mask;
1119 HOTKEY_CONFIG_CRITICAL_END
1120 hotkey_mask_get();
1121 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001122 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001123 "requested hot key mask 0x%08x, "
1124 "forced to 0x%08x (NVRAM poll mask is "
1125 "0x%08x): no firmware mask support\n",
1126 mask, hotkey_mask, hotkey_source_mask);
1127 }
1128#else
1129 hotkey_mask_get();
1130 rc = -ENXIO;
1131#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001132 }
1133
1134 return rc;
1135}
1136
1137static int hotkey_status_get(int *status)
1138{
1139 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1140 return -EIO;
1141
1142 return 0;
1143}
1144
1145static int hotkey_status_set(int status)
1146{
1147 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1148 return -EIO;
1149
1150 return 0;
1151}
1152
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001153static void tpacpi_input_send_radiosw(void)
1154{
1155 int wlsw;
1156
1157 mutex_lock(&tpacpi_inputdev_send_mutex);
1158
1159 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1160 input_report_switch(tpacpi_inputdev,
1161 SW_RADIO, !!wlsw);
1162 input_sync(tpacpi_inputdev);
1163 }
1164
1165 mutex_unlock(&tpacpi_inputdev_send_mutex);
1166}
1167
1168static void tpacpi_input_send_key(unsigned int scancode)
1169{
1170 unsigned int keycode;
1171
1172 keycode = hotkey_keycode_map[scancode];
1173
1174 if (keycode != KEY_RESERVED) {
1175 mutex_lock(&tpacpi_inputdev_send_mutex);
1176
1177 input_report_key(tpacpi_inputdev, keycode, 1);
1178 if (keycode == KEY_UNKNOWN)
1179 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1180 scancode);
1181 input_sync(tpacpi_inputdev);
1182
1183 input_report_key(tpacpi_inputdev, keycode, 0);
1184 if (keycode == KEY_UNKNOWN)
1185 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1186 scancode);
1187 input_sync(tpacpi_inputdev);
1188
1189 mutex_unlock(&tpacpi_inputdev_send_mutex);
1190 }
1191}
1192
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001193#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1194static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1195
1196static void tpacpi_hotkey_send_key(unsigned int scancode)
1197{
1198 tpacpi_input_send_key(scancode);
1199 if (hotkey_report_mode < 2) {
1200 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1201 0x80, 0x1001 + scancode);
1202 }
1203}
1204
1205static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1206{
1207 u8 d;
1208
1209 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1210 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1211 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1212 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1213 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1214 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1215 }
1216 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1217 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1218 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1219 }
1220 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1221 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1222 n->displayexp_toggle =
1223 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1224 }
1225 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1226 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1227 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1228 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1229 n->brightness_toggle =
1230 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1231 }
1232 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1233 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1234 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1235 >> TP_NVRAM_POS_LEVEL_VOLUME;
1236 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1237 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1238 }
1239}
1240
1241#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001242 do { \
1243 if ((mask & (1 << __scancode)) && \
1244 oldn->__member != newn->__member) \
1245 tpacpi_hotkey_send_key(__scancode); \
1246 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001247
1248#define TPACPI_MAY_SEND_KEY(__scancode) \
1249 do { if (mask & (1 << __scancode)) \
1250 tpacpi_hotkey_send_key(__scancode); } while (0)
1251
1252static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001253 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001254 u32 mask)
1255{
1256 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1257 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1258 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1259 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1260
1261 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1262
1263 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1264
1265 /* handle volume */
1266 if (oldn->volume_toggle != newn->volume_toggle) {
1267 if (oldn->mute != newn->mute) {
1268 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1269 }
1270 if (oldn->volume_level > newn->volume_level) {
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1272 } else if (oldn->volume_level < newn->volume_level) {
1273 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1274 } else if (oldn->mute == newn->mute) {
1275 /* repeated key presses that didn't change state */
1276 if (newn->mute) {
1277 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1278 } else if (newn->volume_level != 0) {
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1280 } else {
1281 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1282 }
1283 }
1284 }
1285
1286 /* handle brightness */
1287 if (oldn->brightness_toggle != newn->brightness_toggle) {
1288 if (oldn->brightness_level < newn->brightness_level) {
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1290 } else if (oldn->brightness_level > newn->brightness_level) {
1291 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1292 } else {
1293 /* repeated key presses that didn't change state */
1294 if (newn->brightness_level != 0) {
1295 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1296 } else {
1297 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1298 }
1299 }
1300 }
1301}
1302
1303#undef TPACPI_COMPARE_KEY
1304#undef TPACPI_MAY_SEND_KEY
1305
1306static int hotkey_kthread(void *data)
1307{
1308 struct tp_nvram_state s[2];
1309 u32 mask;
1310 unsigned int si, so;
1311 unsigned long t;
1312 unsigned int change_detector, must_reset;
1313
1314 mutex_lock(&hotkey_thread_mutex);
1315
1316 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1317 goto exit;
1318
1319 set_freezable();
1320
1321 so = 0;
1322 si = 1;
1323 t = 0;
1324
1325 /* Initial state for compares */
1326 mutex_lock(&hotkey_thread_data_mutex);
1327 change_detector = hotkey_config_change;
1328 mask = hotkey_source_mask & hotkey_mask;
1329 mutex_unlock(&hotkey_thread_data_mutex);
1330 hotkey_read_nvram(&s[so], mask);
1331
1332 while (!kthread_should_stop() && hotkey_poll_freq) {
1333 if (t == 0)
1334 t = 1000/hotkey_poll_freq;
1335 t = msleep_interruptible(t);
1336 if (unlikely(kthread_should_stop()))
1337 break;
1338 must_reset = try_to_freeze();
1339 if (t > 0 && !must_reset)
1340 continue;
1341
1342 mutex_lock(&hotkey_thread_data_mutex);
1343 if (must_reset || hotkey_config_change != change_detector) {
1344 /* forget old state on thaw or config change */
1345 si = so;
1346 t = 0;
1347 change_detector = hotkey_config_change;
1348 }
1349 mask = hotkey_source_mask & hotkey_mask;
1350 mutex_unlock(&hotkey_thread_data_mutex);
1351
1352 if (likely(mask)) {
1353 hotkey_read_nvram(&s[si], mask);
1354 if (likely(si != so)) {
1355 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001356 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001357 }
1358 }
1359
1360 so = si;
1361 si ^= 1;
1362 }
1363
1364exit:
1365 mutex_unlock(&hotkey_thread_mutex);
1366 return 0;
1367}
1368
1369static void hotkey_poll_stop_sync(void)
1370{
1371 if (tpacpi_hotkey_task) {
1372 if (frozen(tpacpi_hotkey_task) ||
1373 freezing(tpacpi_hotkey_task))
1374 thaw_process(tpacpi_hotkey_task);
1375
1376 kthread_stop(tpacpi_hotkey_task);
1377 tpacpi_hotkey_task = NULL;
1378 mutex_lock(&hotkey_thread_mutex);
1379 /* at this point, the thread did exit */
1380 mutex_unlock(&hotkey_thread_mutex);
1381 }
1382}
1383
1384/* call with hotkey_mutex held */
1385static void hotkey_poll_setup(int may_warn)
1386{
1387 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1388 hotkey_poll_freq > 0 &&
1389 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1390 if (!tpacpi_hotkey_task) {
1391 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001392 NULL,
1393 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001394 if (IS_ERR(tpacpi_hotkey_task)) {
1395 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001396 printk(TPACPI_ERR
1397 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001398 "for hotkey polling\n");
1399 }
1400 }
1401 } else {
1402 hotkey_poll_stop_sync();
1403 if (may_warn &&
1404 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001405 printk(TPACPI_NOTICE
1406 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001407 "which is currently disabled\n",
1408 hotkey_source_mask);
1409 }
1410 }
1411}
1412
1413static void hotkey_poll_setup_safe(int may_warn)
1414{
1415 mutex_lock(&hotkey_mutex);
1416 hotkey_poll_setup(may_warn);
1417 mutex_unlock(&hotkey_mutex);
1418}
1419
1420static int hotkey_inputdev_open(struct input_dev *dev)
1421{
1422 switch (tpacpi_lifecycle) {
1423 case TPACPI_LIFE_INIT:
1424 /*
1425 * hotkey_init will call hotkey_poll_setup_safe
1426 * at the appropriate moment
1427 */
1428 return 0;
1429 case TPACPI_LIFE_EXITING:
1430 return -EBUSY;
1431 case TPACPI_LIFE_RUNNING:
1432 hotkey_poll_setup_safe(0);
1433 return 0;
1434 }
1435
1436 /* Should only happen if tpacpi_lifecycle is corrupt */
1437 BUG();
1438 return -EBUSY;
1439}
1440
1441static void hotkey_inputdev_close(struct input_dev *dev)
1442{
1443 /* disable hotkey polling when possible */
1444 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1445 hotkey_poll_setup_safe(0);
1446}
1447#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1448
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001449/* sysfs hotkey enable ------------------------------------------------- */
1450static ssize_t hotkey_enable_show(struct device *dev,
1451 struct device_attribute *attr,
1452 char *buf)
1453{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001454 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001455
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001456 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001457 if (res)
1458 return res;
1459
1460 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1461}
1462
1463static ssize_t hotkey_enable_store(struct device *dev,
1464 struct device_attribute *attr,
1465 const char *buf, size_t count)
1466{
1467 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001468 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001469
1470 if (parse_strtoul(buf, 1, &t))
1471 return -EINVAL;
1472
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001473 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001474
1475 return (res) ? res : count;
1476}
1477
1478static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001479 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001480 hotkey_enable_show, hotkey_enable_store);
1481
1482/* sysfs hotkey mask --------------------------------------------------- */
1483static ssize_t hotkey_mask_show(struct device *dev,
1484 struct device_attribute *attr,
1485 char *buf)
1486{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001487 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001488
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001489 if (mutex_lock_interruptible(&hotkey_mutex))
1490 return -ERESTARTSYS;
1491 res = hotkey_mask_get();
1492 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001493
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001494 return (res)?
1495 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001496}
1497
1498static ssize_t hotkey_mask_store(struct device *dev,
1499 struct device_attribute *attr,
1500 const char *buf, size_t count)
1501{
1502 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001503 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001504
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001505 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001506 return -EINVAL;
1507
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001508 if (mutex_lock_interruptible(&hotkey_mutex))
1509 return -ERESTARTSYS;
1510
1511 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001512
1513#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1514 hotkey_poll_setup(1);
1515#endif
1516
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001517 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001518
1519 return (res) ? res : count;
1520}
1521
1522static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001523 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001524 hotkey_mask_show, hotkey_mask_store);
1525
1526/* sysfs hotkey bios_enabled ------------------------------------------- */
1527static ssize_t hotkey_bios_enabled_show(struct device *dev,
1528 struct device_attribute *attr,
1529 char *buf)
1530{
1531 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1532}
1533
1534static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001535 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001536
1537/* sysfs hotkey bios_mask ---------------------------------------------- */
1538static ssize_t hotkey_bios_mask_show(struct device *dev,
1539 struct device_attribute *attr,
1540 char *buf)
1541{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001542 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001543}
1544
1545static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001546 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001547
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001548/* sysfs hotkey all_mask ----------------------------------------------- */
1549static ssize_t hotkey_all_mask_show(struct device *dev,
1550 struct device_attribute *attr,
1551 char *buf)
1552{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001553 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1554 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001555}
1556
1557static struct device_attribute dev_attr_hotkey_all_mask =
1558 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1559
1560/* sysfs hotkey recommended_mask --------------------------------------- */
1561static ssize_t hotkey_recommended_mask_show(struct device *dev,
1562 struct device_attribute *attr,
1563 char *buf)
1564{
1565 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001566 (hotkey_all_mask | hotkey_source_mask)
1567 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001568}
1569
1570static struct device_attribute dev_attr_hotkey_recommended_mask =
1571 __ATTR(hotkey_recommended_mask, S_IRUGO,
1572 hotkey_recommended_mask_show, NULL);
1573
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001574#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1575
1576/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1577static ssize_t hotkey_source_mask_show(struct device *dev,
1578 struct device_attribute *attr,
1579 char *buf)
1580{
1581 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1582}
1583
1584static ssize_t hotkey_source_mask_store(struct device *dev,
1585 struct device_attribute *attr,
1586 const char *buf, size_t count)
1587{
1588 unsigned long t;
1589
1590 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1591 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1592 return -EINVAL;
1593
1594 if (mutex_lock_interruptible(&hotkey_mutex))
1595 return -ERESTARTSYS;
1596
1597 HOTKEY_CONFIG_CRITICAL_START
1598 hotkey_source_mask = t;
1599 HOTKEY_CONFIG_CRITICAL_END
1600
1601 hotkey_poll_setup(1);
1602
1603 mutex_unlock(&hotkey_mutex);
1604
1605 return count;
1606}
1607
1608static struct device_attribute dev_attr_hotkey_source_mask =
1609 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1610 hotkey_source_mask_show, hotkey_source_mask_store);
1611
1612/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1613static ssize_t hotkey_poll_freq_show(struct device *dev,
1614 struct device_attribute *attr,
1615 char *buf)
1616{
1617 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1618}
1619
1620static ssize_t hotkey_poll_freq_store(struct device *dev,
1621 struct device_attribute *attr,
1622 const char *buf, size_t count)
1623{
1624 unsigned long t;
1625
1626 if (parse_strtoul(buf, 25, &t))
1627 return -EINVAL;
1628
1629 if (mutex_lock_interruptible(&hotkey_mutex))
1630 return -ERESTARTSYS;
1631
1632 hotkey_poll_freq = t;
1633
1634 hotkey_poll_setup(1);
1635 mutex_unlock(&hotkey_mutex);
1636
1637 return count;
1638}
1639
1640static struct device_attribute dev_attr_hotkey_poll_freq =
1641 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1642 hotkey_poll_freq_show, hotkey_poll_freq_store);
1643
1644#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1645
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001646/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001647static ssize_t hotkey_radio_sw_show(struct device *dev,
1648 struct device_attribute *attr,
1649 char *buf)
1650{
1651 int res, s;
1652 res = hotkey_get_wlsw(&s);
1653 if (res < 0)
1654 return res;
1655
1656 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1657}
1658
1659static struct device_attribute dev_attr_hotkey_radio_sw =
1660 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1661
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001662static void hotkey_radio_sw_notify_change(void)
1663{
1664 if (tp_features.hotkey_wlsw)
1665 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1666 "hotkey_radio_sw");
1667}
1668
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001669/* sysfs hotkey report_mode -------------------------------------------- */
1670static ssize_t hotkey_report_mode_show(struct device *dev,
1671 struct device_attribute *attr,
1672 char *buf)
1673{
1674 return snprintf(buf, PAGE_SIZE, "%d\n",
1675 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1676}
1677
1678static struct device_attribute dev_attr_hotkey_report_mode =
1679 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1680
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001681/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001682static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1683 struct device_attribute *attr,
1684 char *buf)
1685{
1686 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1687}
1688
1689static struct device_attribute dev_attr_hotkey_wakeup_reason =
1690 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1691
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001692void hotkey_wakeup_reason_notify_change(void)
1693{
1694 if (tp_features.hotkey_mask)
1695 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1696 "wakeup_reason");
1697}
1698
1699/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001700static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1701 struct device_attribute *attr,
1702 char *buf)
1703{
1704 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1705}
1706
1707static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1708 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1709 hotkey_wakeup_hotunplug_complete_show, NULL);
1710
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001711void hotkey_wakeup_hotunplug_complete_notify_change(void)
1712{
1713 if (tp_features.hotkey_mask)
1714 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1715 "wakeup_hotunplug_complete");
1716}
1717
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001718/* --------------------------------------------------------------------- */
1719
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001720static struct attribute *hotkey_attributes[] __initdata = {
1721 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001722 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001723 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001724#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1725 &dev_attr_hotkey_mask.attr,
1726 &dev_attr_hotkey_all_mask.attr,
1727 &dev_attr_hotkey_recommended_mask.attr,
1728 &dev_attr_hotkey_source_mask.attr,
1729 &dev_attr_hotkey_poll_freq.attr,
1730#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001731};
1732
1733static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001734 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001735#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1736 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001737 &dev_attr_hotkey_all_mask.attr,
1738 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001739#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001740 &dev_attr_hotkey_wakeup_reason.attr,
1741 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001742};
1743
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001744static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001745{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001746 /* Requirements for changing the default keymaps:
1747 *
1748 * 1. Many of the keys are mapped to KEY_RESERVED for very
1749 * good reasons. Do not change them unless you have deep
1750 * knowledge on the IBM and Lenovo ThinkPad firmware for
1751 * the various ThinkPad models. The driver behaves
1752 * differently for KEY_RESERVED: such keys have their
1753 * hot key mask *unset* in mask_recommended, and also
1754 * in the initial hot key mask programmed into the
1755 * firmware at driver load time, which means the firm-
1756 * ware may react very differently if you change them to
1757 * something else;
1758 *
1759 * 2. You must be subscribed to the linux-thinkpad and
1760 * ibm-acpi-devel mailing lists, and you should read the
1761 * list archives since 2007 if you want to change the
1762 * keymaps. This requirement exists so that you will
1763 * know the past history of problems with the thinkpad-
1764 * acpi driver keymaps, and also that you will be
1765 * listening to any bug reports;
1766 *
1767 * 3. Do not send thinkpad-acpi specific patches directly to
1768 * for merging, *ever*. Send them to the linux-acpi
1769 * mailinglist for comments. Merging is to be done only
1770 * through acpi-test and the ACPI maintainer.
1771 *
1772 * If the above is too much to ask, don't change the keymap.
1773 * Ask the thinkpad-acpi maintainer to do it, instead.
1774 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001775 static u16 ibm_keycode_map[] __initdata = {
1776 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1777 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1778 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1779 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001780
1781 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001782 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1783 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1784 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001785
1786 /* brightness: firmware always reacts to them, unless
1787 * X.org did some tricks in the radeon BIOS scratch
1788 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001789 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001790 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001791
1792 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001793 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001794
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001795 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1796 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001797
1798 /* Volume: firmware always react to it and reprograms
1799 * the built-in *extra* mixer. Never map it to control
1800 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001801 KEY_RESERVED, /* 0x14: VOLUME UP */
1802 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1803 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001804
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001805 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001806
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001807 /* (assignments unknown, please report if found) */
1808 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1809 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1810 };
1811 static u16 lenovo_keycode_map[] __initdata = {
1812 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1813 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1814 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1815 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001816
1817 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001818 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1819 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1820 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001821
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001822 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001823 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001824
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001825 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001826
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001827 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1828 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001829
1830 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1831 * react to it and reprograms the built-in *extra* mixer.
1832 * Never map it to control another mixer by default.
1833 *
1834 * T60?, T61, R60?, R61: firmware and EC tries to send
1835 * these over the regular keyboard, so these are no-ops,
1836 * but there are still weird bugs re. MUTE, so do not
1837 * change unless you get test reports from all Lenovo
1838 * models. May cause the BIOS to interfere with the
1839 * HDA mixer.
1840 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001841 KEY_RESERVED, /* 0x14: VOLUME UP */
1842 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1843 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001844
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001845 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001846
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001847 /* (assignments unknown, please report if found) */
1848 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1849 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1850 };
1851
1852#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1853#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1854#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1855
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001856 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001857 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001858 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001859
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001860 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1861
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001862 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001863 BUG_ON(tpacpi_inputdev->open != NULL ||
1864 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001865
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001866 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001867 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001868
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001869#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1870 mutex_init(&hotkey_thread_mutex);
1871 mutex_init(&hotkey_thread_data_mutex);
1872#endif
1873
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001874 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001875 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001876
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001877 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001878 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001879
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001880 if (tp_features.hotkey) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001881 hotkey_dev_attributes = create_attr_set(12, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001882 if (!hotkey_dev_attributes)
1883 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001884 res = add_many_to_attr_set(hotkey_dev_attributes,
1885 hotkey_attributes,
1886 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001887 if (res)
1888 return res;
1889
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001890 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001891 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1892 for HKEY interface version 0x100 */
1893 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1894 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001895 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001896 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001897 printk(TPACPI_ERR "please report this to %s\n",
1898 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001899 } else {
1900 /*
1901 * MHKV 0x100 in A31, R40, R40e,
1902 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001903 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001904 tp_features.hotkey_mask = 1;
1905 }
1906 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001907
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001908 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001909 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001910
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001911 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001912 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001913 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001914 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001915 "missing MHKA handler, "
1916 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001917 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001918 /* FN+F12, FN+F4, FN+F3 */
1919 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001920 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001921 }
1922
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001923 /* hotkey_source_mask *must* be zero for
1924 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001925 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001926 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001927 res = hotkey_mask_get();
1928 hotkey_orig_mask = hotkey_mask;
1929 if (!res) {
1930 res = add_many_to_attr_set(
1931 hotkey_dev_attributes,
1932 hotkey_mask_attributes,
1933 ARRAY_SIZE(hotkey_mask_attributes));
1934 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001935 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001936
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001937#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1938 if (tp_features.hotkey_mask) {
1939 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1940 & ~hotkey_all_mask;
1941 } else {
1942 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1943 }
1944
1945 vdbg_printk(TPACPI_DBG_INIT,
1946 "hotkey source mask 0x%08x, polling freq %d\n",
1947 hotkey_source_mask, hotkey_poll_freq);
1948#endif
1949
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001950 /* Not all thinkpads have a hardware radio switch */
1951 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1952 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001953 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001954 "radio switch found; radios are %s\n",
1955 enabled(status, 0));
1956 res = add_to_attr_set(hotkey_dev_attributes,
1957 &dev_attr_hotkey_radio_sw.attr);
1958 }
1959
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001960 if (!res)
1961 res = register_attr_set_with_sysfs(
1962 hotkey_dev_attributes,
1963 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001964 if (res)
1965 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001966
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001967 /* Set up key map */
1968
1969 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1970 GFP_KERNEL);
1971 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001972 printk(TPACPI_ERR
1973 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001974 return -ENOMEM;
1975 }
1976
1977 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1978 dbg_printk(TPACPI_DBG_INIT,
1979 "using Lenovo default hot key map\n");
1980 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1981 TPACPI_HOTKEY_MAP_SIZE);
1982 } else {
1983 dbg_printk(TPACPI_DBG_INIT,
1984 "using IBM default hot key map\n");
1985 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1986 TPACPI_HOTKEY_MAP_SIZE);
1987 }
1988
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001989 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1990 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1991 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001992 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1993 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1994 tpacpi_inputdev->keycode = hotkey_keycode_map;
1995 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001996 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1997 set_bit(hotkey_keycode_map[i],
1998 tpacpi_inputdev->keybit);
1999 } else {
2000 if (i < sizeof(hotkey_reserved_mask)*8)
2001 hotkey_reserved_mask |= 1 << i;
2002 }
2003 }
2004
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002005 if (tp_features.hotkey_wlsw) {
2006 set_bit(EV_SW, tpacpi_inputdev->evbit);
2007 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2008 }
2009
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002010 dbg_printk(TPACPI_DBG_INIT,
2011 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002012 res = hotkey_status_set(1);
2013 if (res)
2014 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002015 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2016 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002017 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002018 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002019 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002020
2021 dbg_printk(TPACPI_DBG_INIT,
2022 "legacy hot key reporting over procfs %s\n",
2023 (hotkey_report_mode < 2) ?
2024 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002025
2026#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2027 tpacpi_inputdev->open = &hotkey_inputdev_open;
2028 tpacpi_inputdev->close = &hotkey_inputdev_close;
2029
2030 hotkey_poll_setup_safe(1);
2031#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002032 }
2033
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002034 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002035}
2036
2037static void hotkey_exit(void)
2038{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002039#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2040 hotkey_poll_stop_sync();
2041#endif
2042
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002043 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002044 dbg_printk(TPACPI_DBG_EXIT,
2045 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002046 /* no short-circuit boolean operator below! */
2047 if ((hotkey_mask_set(hotkey_orig_mask) |
2048 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002049 printk(TPACPI_ERR
2050 "failed to restore hot key mask "
2051 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002052 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002053
2054 if (hotkey_dev_attributes) {
2055 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2056 hotkey_dev_attributes = NULL;
2057 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002058}
2059
2060static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2061{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002062 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002063 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002064 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002065 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002066 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002067
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002068 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002069 printk(TPACPI_ERR
2070 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002071 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002072 acpi_bus_generate_netlink_event(
2073 ibm->acpi->device->pnp.device_class,
2074 ibm->acpi->device->dev.bus_id,
2075 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002076 return;
2077 }
2078
2079 while (1) {
2080 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002081 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002082 return;
2083 }
2084
2085 if (hkey == 0) {
2086 /* queue empty */
2087 return;
2088 }
2089
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002090 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002091 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002092 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002093
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002094 switch (hkey >> 12) {
2095 case 1:
2096 /* 0x1000-0x1FFF: key presses */
2097 scancode = hkey & 0xfff;
2098 if (scancode > 0 && scancode < 0x21) {
2099 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002100 if (!(hotkey_source_mask & (1 << scancode))) {
2101 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002102 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002103 } else {
2104 ignore_acpi_ev = 1;
2105 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002106 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002107 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002108 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002109 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002110 case 2:
2111 /* Wakeup reason */
2112 switch (hkey) {
2113 case 0x2304: /* suspend, undock */
2114 case 0x2404: /* hibernation, undock */
2115 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2116 ignore_acpi_ev = 1;
2117 break;
2118 case 0x2305: /* suspend, bay eject */
2119 case 0x2405: /* hibernation, bay eject */
2120 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2121 ignore_acpi_ev = 1;
2122 break;
2123 default:
2124 unk_ev = 1;
2125 }
2126 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2127 printk(TPACPI_INFO
2128 "woke up due to a hot-unplug "
2129 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002130 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002131 }
2132 break;
2133 case 3:
2134 /* bay-related wakeups */
2135 if (hkey == 0x3003) {
2136 hotkey_autosleep_ack = 1;
2137 printk(TPACPI_INFO
2138 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002139 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002140 } else {
2141 unk_ev = 1;
2142 }
2143 break;
2144 case 4:
2145 /* dock-related wakeups */
2146 if (hkey == 0x4003) {
2147 hotkey_autosleep_ack = 1;
2148 printk(TPACPI_INFO
2149 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002150 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002151 } else {
2152 unk_ev = 1;
2153 }
2154 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002155 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002156 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002157 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002158 case 0x5010: /* Lenovo new BIOS: brightness changed */
2159 case 0x5009: /* X61t: swivel up (tablet mode) */
2160 case 0x500a: /* X61t: swivel down (normal mode) */
2161 case 0x500b: /* X61t: tablet pen inserted into bay */
2162 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002163 break;
2164 case 0x5001:
2165 case 0x5002:
2166 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002167 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002168 break;
2169 default:
2170 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002171 }
2172 break;
2173 case 7:
2174 /* 0x7000-0x7FFF: misc */
2175 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2176 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002177 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002178 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002179 break;
2180 }
2181 /* fallthrough to default */
2182 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002183 unk_ev = 1;
2184 }
2185 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002186 printk(TPACPI_NOTICE
2187 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002188 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002189
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002190 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002191 if (!ignore_acpi_ev &&
2192 (send_acpi_ev || hotkey_report_mode < 2)) {
2193 acpi_bus_generate_proc_event(ibm->acpi->device,
2194 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002195 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002196
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002197 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002198 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002199 acpi_bus_generate_netlink_event(
2200 ibm->acpi->device->pnp.device_class,
2201 ibm->acpi->device->dev.bus_id,
2202 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002203 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002204 }
2205}
2206
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002207static void hotkey_suspend(pm_message_t state)
2208{
2209 /* Do these on suspend, we get the events on early resume! */
2210 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2211 hotkey_autosleep_ack = 0;
2212}
2213
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002214static void hotkey_resume(void)
2215{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002216 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002217 printk(TPACPI_ERR
2218 "error while trying to read hot key mask "
2219 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002220 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002221 hotkey_radio_sw_notify_change();
2222 hotkey_wakeup_reason_notify_change();
2223 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002224#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2225 hotkey_poll_setup_safe(0);
2226#endif
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002227}
2228
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002229/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002230static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002232 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 int len = 0;
2234
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002235 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002236 len += sprintf(p + len, "status:\t\tnot supported\n");
2237 return len;
2238 }
2239
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002240 if (mutex_lock_interruptible(&hotkey_mutex))
2241 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002242 res = hotkey_status_get(&status);
2243 if (!res)
2244 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002245 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002246 if (res)
2247 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002250 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002251 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 len += sprintf(p + len,
2253 "commands:\tenable, disable, reset, <mask>\n");
2254 } else {
2255 len += sprintf(p + len, "mask:\t\tnot supported\n");
2256 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2257 }
2258
2259 return len;
2260}
2261
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002262static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002264 int res, status;
2265 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002268 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return -ENODEV;
2270
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002271 if (mutex_lock_interruptible(&hotkey_mutex))
2272 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002273
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002274 status = -1;
2275 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002276
2277 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 while ((cmd = next_cmd(&buf))) {
2279 if (strlencmp(cmd, "enable") == 0) {
2280 status = 1;
2281 } else if (strlencmp(cmd, "disable") == 0) {
2282 status = 0;
2283 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002284 status = hotkey_orig_status;
2285 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2287 /* mask set */
2288 } else if (sscanf(cmd, "%x", &mask) == 1) {
2289 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002290 } else {
2291 res = -EINVAL;
2292 goto errexit;
2293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002295 if (status != -1)
2296 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002298 if (!res && mask != hotkey_mask)
2299 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002301errexit:
2302 mutex_unlock(&hotkey_mutex);
2303 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002306static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002307 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002308 {"", 0},
2309};
2310
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002311static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002312 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002313 .notify = hotkey_notify,
2314 .handle = &hkey_handle,
2315 .type = ACPI_DEVICE_NOTIFY,
2316};
2317
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002318static struct ibm_struct hotkey_driver_data = {
2319 .name = "hotkey",
2320 .read = hotkey_read,
2321 .write = hotkey_write,
2322 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002323 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002324 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002325 .acpi = &ibm_hotkey_acpidriver,
2326};
2327
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002328/*************************************************************************
2329 * Bluetooth subdriver
2330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002332enum {
2333 /* ACPI GBDC/SBDC bits */
2334 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2335 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2336 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2337};
2338
2339static int bluetooth_get_radiosw(void);
2340static int bluetooth_set_radiosw(int radio_on);
2341
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002342/* sysfs bluetooth enable ---------------------------------------------- */
2343static ssize_t bluetooth_enable_show(struct device *dev,
2344 struct device_attribute *attr,
2345 char *buf)
2346{
2347 int status;
2348
2349 status = bluetooth_get_radiosw();
2350 if (status < 0)
2351 return status;
2352
2353 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2354}
2355
2356static ssize_t bluetooth_enable_store(struct device *dev,
2357 struct device_attribute *attr,
2358 const char *buf, size_t count)
2359{
2360 unsigned long t;
2361 int res;
2362
2363 if (parse_strtoul(buf, 1, &t))
2364 return -EINVAL;
2365
2366 res = bluetooth_set_radiosw(t);
2367
2368 return (res) ? res : count;
2369}
2370
2371static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002372 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002373 bluetooth_enable_show, bluetooth_enable_store);
2374
2375/* --------------------------------------------------------------------- */
2376
2377static struct attribute *bluetooth_attributes[] = {
2378 &dev_attr_bluetooth_enable.attr,
2379 NULL
2380};
2381
2382static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002383 .attrs = bluetooth_attributes,
2384};
2385
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002386static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002388 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002389 int status = 0;
2390
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002391 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2392
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002393 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002394
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002395 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2396 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002397 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002398 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002400 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2401 str_supported(tp_features.bluetooth),
2402 status);
2403
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002404 if (tp_features.bluetooth) {
2405 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2406 /* no bluetooth hardware present in system */
2407 tp_features.bluetooth = 0;
2408 dbg_printk(TPACPI_DBG_INIT,
2409 "bluetooth hardware not installed\n");
2410 } else {
2411 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2412 &bluetooth_attr_group);
2413 if (res)
2414 return res;
2415 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002416 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002417
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002418 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002421static void bluetooth_exit(void)
2422{
2423 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2424 &bluetooth_attr_group);
2425}
2426
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002427static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
2429 int status;
2430
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002431 if (!tp_features.bluetooth)
2432 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002434 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2435 return -EIO;
2436
2437 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2438}
2439
2440static int bluetooth_set_radiosw(int radio_on)
2441{
2442 int status;
2443
2444 if (!tp_features.bluetooth)
2445 return -ENODEV;
2446
2447 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2448 return -EIO;
2449 if (radio_on)
2450 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2451 else
2452 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2453 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2454 return -EIO;
2455
2456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002459/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002460static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
2462 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002463 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002465 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002468 len += sprintf(p + len, "status:\t\t%s\n",
2469 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 len += sprintf(p + len, "commands:\tenable, disable\n");
2471 }
2472
2473 return len;
2474}
2475
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002476static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002480 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 while ((cmd = next_cmd(&buf))) {
2484 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002485 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002487 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 } else
2489 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return 0;
2493}
2494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002495static struct ibm_struct bluetooth_driver_data = {
2496 .name = "bluetooth",
2497 .read = bluetooth_read,
2498 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002499 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002500};
2501
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002502/*************************************************************************
2503 * Wan subdriver
2504 */
2505
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002506enum {
2507 /* ACPI GWAN/SWAN bits */
2508 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2509 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2510 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2511};
2512
2513static int wan_get_radiosw(void);
2514static int wan_set_radiosw(int radio_on);
2515
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002516/* sysfs wan enable ---------------------------------------------------- */
2517static ssize_t wan_enable_show(struct device *dev,
2518 struct device_attribute *attr,
2519 char *buf)
2520{
2521 int status;
2522
2523 status = wan_get_radiosw();
2524 if (status < 0)
2525 return status;
2526
2527 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2528}
2529
2530static ssize_t wan_enable_store(struct device *dev,
2531 struct device_attribute *attr,
2532 const char *buf, size_t count)
2533{
2534 unsigned long t;
2535 int res;
2536
2537 if (parse_strtoul(buf, 1, &t))
2538 return -EINVAL;
2539
2540 res = wan_set_radiosw(t);
2541
2542 return (res) ? res : count;
2543}
2544
2545static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002546 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002547 wan_enable_show, wan_enable_store);
2548
2549/* --------------------------------------------------------------------- */
2550
2551static struct attribute *wan_attributes[] = {
2552 &dev_attr_wan_enable.attr,
2553 NULL
2554};
2555
2556static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002557 .attrs = wan_attributes,
2558};
2559
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002560static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002561{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002562 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002563 int status = 0;
2564
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002565 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2566
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002567 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002568
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002569 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002570 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002571
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002572 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2573 str_supported(tp_features.wan),
2574 status);
2575
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002576 if (tp_features.wan) {
2577 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2578 /* no wan hardware present in system */
2579 tp_features.wan = 0;
2580 dbg_printk(TPACPI_DBG_INIT,
2581 "wan hardware not installed\n");
2582 } else {
2583 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2584 &wan_attr_group);
2585 if (res)
2586 return res;
2587 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002588 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002589
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002590 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002591}
2592
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002593static void wan_exit(void)
2594{
2595 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2596 &wan_attr_group);
2597}
2598
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002599static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002600{
2601 int status;
2602
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002603 if (!tp_features.wan)
2604 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002605
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002606 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2607 return -EIO;
2608
2609 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2610}
2611
2612static int wan_set_radiosw(int radio_on)
2613{
2614 int status;
2615
2616 if (!tp_features.wan)
2617 return -ENODEV;
2618
2619 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2620 return -EIO;
2621 if (radio_on)
2622 status |= TP_ACPI_WANCARD_RADIOSSW;
2623 else
2624 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2625 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2626 return -EIO;
2627
2628 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002629}
2630
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002631/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002632static int wan_read(char *p)
2633{
2634 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002635 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002636
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002637 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002638 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002639 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002640 len += sprintf(p + len, "status:\t\t%s\n",
2641 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002642 len += sprintf(p + len, "commands:\tenable, disable\n");
2643 }
2644
2645 return len;
2646}
2647
2648static int wan_write(char *buf)
2649{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002650 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002651
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002652 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002653 return -ENODEV;
2654
2655 while ((cmd = next_cmd(&buf))) {
2656 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002657 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002658 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002659 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002660 } else
2661 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002662 }
2663
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002664 return 0;
2665}
2666
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002667static struct ibm_struct wan_driver_data = {
2668 .name = "wan",
2669 .read = wan_read,
2670 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002671 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002672 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002673};
2674
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002675/*************************************************************************
2676 * Video subdriver
2677 */
2678
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002679enum video_access_mode {
2680 TPACPI_VIDEO_NONE = 0,
2681 TPACPI_VIDEO_570, /* 570 */
2682 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2683 TPACPI_VIDEO_NEW, /* all others */
2684};
2685
2686enum { /* video status flags, based on VIDEO_570 */
2687 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2688 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2689 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2690};
2691
2692enum { /* TPACPI_VIDEO_570 constants */
2693 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2694 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2695 * video_status_flags */
2696 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2697 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2698};
2699
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002700static enum video_access_mode video_supported;
2701static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002702
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002703static int video_autosw_get(void);
2704static int video_autosw_set(int enable);
2705
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002706TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002707 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2708 "\\_SB.PCI0.VID0", /* 770e */
2709 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2710 "\\_SB.PCI0.AGP.VID", /* all others */
2711 ); /* R30, R31 */
2712
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002713TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002714
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002715static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002717 int ivga;
2718
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002719 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2720
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002721 TPACPI_ACPIHANDLE_INIT(vid);
2722 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002723
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002724 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2725 /* G41, assume IVGA doesn't change */
2726 vid_handle = vid2_handle;
2727
2728 if (!vid_handle)
2729 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002730 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002731 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2732 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002733 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002734 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2735 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002736 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002737 else
2738 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002739 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002741 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2742 str_supported(video_supported != TPACPI_VIDEO_NONE),
2743 video_supported);
2744
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002745 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002748static void video_exit(void)
2749{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002750 dbg_printk(TPACPI_DBG_EXIT,
2751 "restoring original video autoswitch mode\n");
2752 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002753 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002754 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002755}
2756
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002757static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 int status = 0;
2760 int i;
2761
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002762 switch (video_supported) {
2763 case TPACPI_VIDEO_570:
2764 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2765 TP_ACPI_VIDEO_570_PHSCMD))
2766 return -EIO;
2767 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2768 break;
2769 case TPACPI_VIDEO_770:
2770 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2771 return -EIO;
2772 if (i)
2773 status |= TP_ACPI_VIDEO_S_LCD;
2774 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2775 return -EIO;
2776 if (i)
2777 status |= TP_ACPI_VIDEO_S_CRT;
2778 break;
2779 case TPACPI_VIDEO_NEW:
2780 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2781 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2782 return -EIO;
2783 if (i)
2784 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002786 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2787 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2788 return -EIO;
2789 if (i)
2790 status |= TP_ACPI_VIDEO_S_LCD;
2791 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2792 return -EIO;
2793 if (i)
2794 status |= TP_ACPI_VIDEO_S_DVI;
2795 break;
2796 default:
2797 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 return status;
2801}
2802
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002803static int video_outputsw_set(int status)
2804{
2805 int autosw;
2806 int res = 0;
2807
2808 switch (video_supported) {
2809 case TPACPI_VIDEO_570:
2810 res = acpi_evalf(NULL, NULL,
2811 "\\_SB.PHS2", "vdd",
2812 TP_ACPI_VIDEO_570_PHS2CMD,
2813 status | TP_ACPI_VIDEO_570_PHS2SET);
2814 break;
2815 case TPACPI_VIDEO_770:
2816 autosw = video_autosw_get();
2817 if (autosw < 0)
2818 return autosw;
2819
2820 res = video_autosw_set(1);
2821 if (res)
2822 return res;
2823 res = acpi_evalf(vid_handle, NULL,
2824 "ASWT", "vdd", status * 0x100, 0);
2825 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002826 printk(TPACPI_ERR
2827 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002828 return -EIO;
2829 }
2830 break;
2831 case TPACPI_VIDEO_NEW:
2832 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002833 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002834 break;
2835 default:
2836 return -ENOSYS;
2837 }
2838
2839 return (res)? 0 : -EIO;
2840}
2841
2842static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002844 int autosw = 0;
2845
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002846 switch (video_supported) {
2847 case TPACPI_VIDEO_570:
2848 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2849 return -EIO;
2850 break;
2851 case TPACPI_VIDEO_770:
2852 case TPACPI_VIDEO_NEW:
2853 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2854 return -EIO;
2855 break;
2856 default:
2857 return -ENOSYS;
2858 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002859
2860 return autosw & 1;
2861}
2862
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002863static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002864{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002865 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002866 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002867 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002868}
2869
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002870static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002871{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002872 int autosw = video_autosw_get();
2873 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002874
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002875 if (autosw < 0)
2876 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002877
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002878 switch (video_supported) {
2879 case TPACPI_VIDEO_570:
2880 res = video_autosw_set(1);
2881 if (res)
2882 return res;
2883 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2884 break;
2885 case TPACPI_VIDEO_770:
2886 case TPACPI_VIDEO_NEW:
2887 res = video_autosw_set(1);
2888 if (res)
2889 return res;
2890 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2891 break;
2892 default:
2893 return -ENOSYS;
2894 }
2895 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002896 printk(TPACPI_ERR
2897 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002898 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002899 }
2900
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002901 return (res)? 0 : -EIO;
2902}
2903
2904static int video_expand_toggle(void)
2905{
2906 switch (video_supported) {
2907 case TPACPI_VIDEO_570:
2908 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2909 0 : -EIO;
2910 case TPACPI_VIDEO_770:
2911 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2912 0 : -EIO;
2913 case TPACPI_VIDEO_NEW:
2914 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2915 0 : -EIO;
2916 default:
2917 return -ENOSYS;
2918 }
2919 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002920}
2921
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002922static int video_read(char *p)
2923{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002924 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002925 int len = 0;
2926
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002927 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002928 len += sprintf(p + len, "status:\t\tnot supported\n");
2929 return len;
2930 }
2931
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002932 status = video_outputsw_get();
2933 if (status < 0)
2934 return status;
2935
2936 autosw = video_autosw_get();
2937 if (autosw < 0)
2938 return autosw;
2939
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002940 len += sprintf(p + len, "status:\t\tsupported\n");
2941 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2942 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002943 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002944 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2945 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2946 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2947 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002948 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002949 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2950 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2951 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2952
2953 return len;
2954}
2955
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002956static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
2958 char *cmd;
2959 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002960 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002962 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002963 return -ENODEV;
2964
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002965 enable = 0;
2966 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 while ((cmd = next_cmd(&buf))) {
2969 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002970 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002972 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002974 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002976 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002977 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002978 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002979 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002980 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002981 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002982 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002984 res = video_autosw_set(1);
2985 if (res)
2986 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002988 res = video_autosw_set(0);
2989 if (res)
2990 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002992 res = video_outputsw_cycle();
2993 if (res)
2994 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002996 res = video_expand_toggle();
2997 if (res)
2998 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 } else
3000 return -EINVAL;
3001 }
3002
3003 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003004 status = video_outputsw_get();
3005 if (status < 0)
3006 return status;
3007 res = video_outputsw_set((status & ~disable) | enable);
3008 if (res)
3009 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
3011
3012 return 0;
3013}
3014
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003015static struct ibm_struct video_driver_data = {
3016 .name = "video",
3017 .read = video_read,
3018 .write = video_write,
3019 .exit = video_exit,
3020};
3021
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003022/*************************************************************************
3023 * Light (thinklight) subdriver
3024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003026TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3027TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003028
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003029static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003031 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3032
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003033 TPACPI_ACPIHANDLE_INIT(ledb);
3034 TPACPI_ACPIHANDLE_INIT(lght);
3035 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003036
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003037 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003038 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003039
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003040 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003041 /* light status not supported on
3042 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003043 tp_features.light_status =
3044 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003046 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003047 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003048
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003049 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003052static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 int len = 0;
3055 int status = 0;
3056
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003057 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003058 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003059 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003060 len += sprintf(p + len, "status:\t\tunknown\n");
3061 len += sprintf(p + len, "commands:\ton, off\n");
3062 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3064 return -EIO;
3065 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003066 len += sprintf(p + len, "commands:\ton, off\n");
3067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 return len;
3070}
3071
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003072static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
3074 int cmos_cmd, lght_cmd;
3075 char *cmd;
3076 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003077
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003078 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003079 return -ENODEV;
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 while ((cmd = next_cmd(&buf))) {
3082 if (strlencmp(cmd, "on") == 0) {
3083 cmos_cmd = 0x0c;
3084 lght_cmd = 1;
3085 } else if (strlencmp(cmd, "off") == 0) {
3086 cmos_cmd = 0x0d;
3087 lght_cmd = 0;
3088 } else
3089 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003092 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3093 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (!success)
3095 return -EIO;
3096 }
3097
3098 return 0;
3099}
3100
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003101static struct ibm_struct light_driver_data = {
3102 .name = "light",
3103 .read = light_read,
3104 .write = light_write,
3105};
3106
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003107/*************************************************************************
3108 * Dock subdriver
3109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003111#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003112
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003113static void dock_notify(struct ibm_struct *ibm, u32 event);
3114static int dock_read(char *p);
3115static int dock_write(char *buf);
3116
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003117TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003118 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3119 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3120 "\\_SB.PCI.ISA.SLCE", /* 570 */
3121 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3122
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003123/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003124TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003125
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003126static const struct acpi_device_id ibm_pci_device_ids[] = {
3127 {PCI_ROOT_HID_STRING, 0},
3128 {"", 0},
3129};
3130
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003131static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3132 {
3133 .notify = dock_notify,
3134 .handle = &dock_handle,
3135 .type = ACPI_SYSTEM_NOTIFY,
3136 },
3137 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003138 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3139 * We just use it to get notifications of dock hotplug
3140 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003141 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003142 .notify = dock_notify,
3143 .handle = &pci_handle,
3144 .type = ACPI_SYSTEM_NOTIFY,
3145 },
3146};
3147
3148static struct ibm_struct dock_driver_data[2] = {
3149 {
3150 .name = "dock",
3151 .read = dock_read,
3152 .write = dock_write,
3153 .acpi = &ibm_dock_acpidriver[0],
3154 },
3155 {
3156 .name = "dock",
3157 .acpi = &ibm_dock_acpidriver[1],
3158 },
3159};
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161#define dock_docked() (_sta(dock_handle) & 1)
3162
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003163static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003164{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003165 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3166
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003167 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003168
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003169 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3170 str_supported(dock_handle != NULL));
3171
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003172 return (dock_handle)? 0 : 1;
3173}
3174
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003175static int __init dock_init2(struct ibm_init_struct *iibm)
3176{
3177 int dock2_needed;
3178
3179 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3180
3181 if (dock_driver_data[0].flags.acpi_driver_registered &&
3182 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003183 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003184 dock2_needed = (pci_handle != NULL);
3185 vdbg_printk(TPACPI_DBG_INIT,
3186 "dock PCI handler for the TP 570 is %s\n",
3187 str_supported(dock2_needed));
3188 } else {
3189 vdbg_printk(TPACPI_DBG_INIT,
3190 "dock subdriver part 2 not required\n");
3191 dock2_needed = 0;
3192 }
3193
3194 return (dock2_needed)? 0 : 1;
3195}
3196
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003197static void dock_notify(struct ibm_struct *ibm, u32 event)
3198{
3199 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003200 int pci = ibm->acpi->hid && ibm->acpi->device &&
3201 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003202 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003203
3204 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003205 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003206 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003207 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003208 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003209 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003210 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003211 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003212 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003213 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003214 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003215 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003216 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003217 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003218 }
Len Brown14e04fb2007-08-23 15:20:26 -04003219 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003220 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3221 ibm->acpi->device->dev.bus_id,
3222 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003223}
3224
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003225static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 int len = 0;
3228 int docked = dock_docked();
3229
3230 if (!dock_handle)
3231 len += sprintf(p + len, "status:\t\tnot supported\n");
3232 else if (!docked)
3233 len += sprintf(p + len, "status:\t\tundocked\n");
3234 else {
3235 len += sprintf(p + len, "status:\t\tdocked\n");
3236 len += sprintf(p + len, "commands:\tdock, undock\n");
3237 }
3238
3239 return len;
3240}
3241
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003242static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
3244 char *cmd;
3245
3246 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003247 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 while ((cmd = next_cmd(&buf))) {
3250 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003251 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3252 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return -EIO;
3254 } else if (strlencmp(cmd, "dock") == 0) {
3255 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3256 return -EIO;
3257 } else
3258 return -EINVAL;
3259 }
3260
3261 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003264#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003266/*************************************************************************
3267 * Bay subdriver
3268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003270#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003271
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003272TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003273 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3274 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3275 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3276 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003277TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003278 "_EJ0", /* all others */
3279 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003280TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003281 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3282 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003283TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003284 "_EJ0", /* 770x */
3285 ); /* all others */
3286
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003287static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003289 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3290
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003291 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003292 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003293 TPACPI_ACPIHANDLE_INIT(bay_ej);
3294 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003295 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003296 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003297
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003298 tp_features.bay_status = bay_handle &&
3299 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3300 tp_features.bay_status2 = bay2_handle &&
3301 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003302
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003303 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3304 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3305 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3306 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003308 vdbg_printk(TPACPI_DBG_INIT,
3309 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003310 str_supported(tp_features.bay_status),
3311 str_supported(tp_features.bay_eject),
3312 str_supported(tp_features.bay_status2),
3313 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003314
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003315 return (tp_features.bay_status || tp_features.bay_eject ||
3316 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003319static void bay_notify(struct ibm_struct *ibm, u32 event)
3320{
Len Brown14e04fb2007-08-23 15:20:26 -04003321 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003322 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3323 ibm->acpi->device->dev.bus_id,
3324 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003325}
3326
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003327#define bay_occupied(b) (_sta(b##_handle) & 1)
3328
3329static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
3331 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003332 int occupied = bay_occupied(bay);
3333 int occupied2 = bay_occupied(bay2);
3334 int eject, eject2;
3335
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003336 len += sprintf(p + len, "status:\t\t%s\n",
3337 tp_features.bay_status ?
3338 (occupied ? "occupied" : "unoccupied") :
3339 "not supported");
3340 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003341 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3342 "occupied" : "unoccupied");
3343
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003344 eject = tp_features.bay_eject && occupied;
3345 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003346
3347 if (eject && eject2)
3348 len += sprintf(p + len, "commands:\teject, eject2\n");
3349 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003351 else if (eject2)
3352 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
3354 return len;
3355}
3356
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003357static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
3359 char *cmd;
3360
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003361 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003362 return -ENODEV;
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003365 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003366 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3367 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003368 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003369 strlencmp(cmd, "eject2") == 0) {
3370 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 return -EIO;
3372 } else
3373 return -EINVAL;
3374 }
3375
3376 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003377}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003378
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003379static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3380 .notify = bay_notify,
3381 .handle = &bay_handle,
3382 .type = ACPI_SYSTEM_NOTIFY,
3383};
3384
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003385static struct ibm_struct bay_driver_data = {
3386 .name = "bay",
3387 .read = bay_read,
3388 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003389 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003390};
3391
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003392#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003394/*************************************************************************
3395 * CMOS subdriver
3396 */
3397
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003398/* sysfs cmos_command -------------------------------------------------- */
3399static ssize_t cmos_command_store(struct device *dev,
3400 struct device_attribute *attr,
3401 const char *buf, size_t count)
3402{
3403 unsigned long cmos_cmd;
3404 int res;
3405
3406 if (parse_strtoul(buf, 21, &cmos_cmd))
3407 return -EINVAL;
3408
3409 res = issue_thinkpad_cmos_command(cmos_cmd);
3410 return (res)? res : count;
3411}
3412
3413static struct device_attribute dev_attr_cmos_command =
3414 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3415
3416/* --------------------------------------------------------------------- */
3417
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003418static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003419{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003420 int res;
3421
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003422 vdbg_printk(TPACPI_DBG_INIT,
3423 "initializing cmos commands subdriver\n");
3424
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003425 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003426
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003427 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3428 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003429
3430 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3431 if (res)
3432 return res;
3433
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003434 return (cmos_handle)? 0 : 1;
3435}
3436
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003437static void cmos_exit(void)
3438{
3439 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3440}
3441
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003442static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
3444 int len = 0;
3445
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003446 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3447 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if (!cmos_handle)
3449 len += sprintf(p + len, "status:\t\tnot supported\n");
3450 else {
3451 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003452 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 }
3454
3455 return len;
3456}
3457
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003458static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
3460 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003461 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
3463 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003464 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3465 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 /* cmos_cmd set */
3467 } else
3468 return -EINVAL;
3469
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003470 res = issue_thinkpad_cmos_command(cmos_cmd);
3471 if (res)
3472 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 }
3474
3475 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003476}
3477
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003478static struct ibm_struct cmos_driver_data = {
3479 .name = "cmos",
3480 .read = cmos_read,
3481 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003482 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003483};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003484
3485/*************************************************************************
3486 * LED subdriver
3487 */
3488
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003489enum led_access_mode {
3490 TPACPI_LED_NONE = 0,
3491 TPACPI_LED_570, /* 570 */
3492 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3493 TPACPI_LED_NEW, /* all others */
3494};
3495
3496enum { /* For TPACPI_LED_OLD */
3497 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3498 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3499 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3500};
3501
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003502static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003503
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003504TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003505 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3506 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003507 "LED", /* all others */
3508 ); /* R30, R31 */
3509
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003510static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003511{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003512 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3513
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003514 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003515
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003516 if (!led_handle)
3517 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003518 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003519 else if (strlencmp(led_path, "SLED") == 0)
3520 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003521 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003522 else if (strlencmp(led_path, "SYSL") == 0)
3523 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003524 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003525 else
3526 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003527 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003528
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003529 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3530 str_supported(led_supported), led_supported);
3531
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003532 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003533}
3534
3535#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3536
3537static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
3539 int len = 0;
3540
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003541 if (!led_supported) {
3542 len += sprintf(p + len, "status:\t\tnot supported\n");
3543 return len;
3544 }
3545 len += sprintf(p + len, "status:\t\tsupported\n");
3546
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003547 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003548 /* 570 */
3549 int i, status;
3550 for (i = 0; i < 8; i++) {
3551 if (!acpi_evalf(ec_handle,
3552 &status, "GLED", "dd", 1 << i))
3553 return -EIO;
3554 len += sprintf(p + len, "%d:\t\t%s\n",
3555 i, led_status(status));
3556 }
3557 }
3558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003560 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
3562 return len;
3563}
3564
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003565/* off, on, blink */
3566static const int led_sled_arg1[] = { 0, 1, 3 };
3567static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3568static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3569static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3570
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003571static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
3573 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003574 int led, ind, ret;
3575
3576 if (!led_supported)
3577 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003580 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return -EINVAL;
3582
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003583 if (strstr(cmd, "off")) {
3584 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003586 ind = 1;
3587 } else if (strstr(cmd, "blink")) {
3588 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 } else
3590 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003591
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003592 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003593 /* 570 */
3594 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003596 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003598 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003599 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3600 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003601 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003602 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003603 ret = ec_write(TPACPI_LED_EC_HLBL,
3604 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003605 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003606 ret = ec_write(TPACPI_LED_EC_HLCL,
3607 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003608 if (ret < 0)
3609 return ret;
3610 } else {
3611 /* all others */
3612 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3613 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
3617
3618 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003619}
3620
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003621static struct ibm_struct led_driver_data = {
3622 .name = "led",
3623 .read = led_read,
3624 .write = led_write,
3625};
3626
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003627/*************************************************************************
3628 * Beep subdriver
3629 */
3630
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003631TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003632
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003633static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003634{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003635 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3636
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003637 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003638
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003639 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3640 str_supported(beep_handle != NULL));
3641
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003642 return (beep_handle)? 0 : 1;
3643}
3644
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003645static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
3647 int len = 0;
3648
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003649 if (!beep_handle)
3650 len += sprintf(p + len, "status:\t\tnot supported\n");
3651 else {
3652 len += sprintf(p + len, "status:\t\tsupported\n");
3653 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
3656 return len;
3657}
3658
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003659static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
3661 char *cmd;
3662 int beep_cmd;
3663
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003664 if (!beep_handle)
3665 return -ENODEV;
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003668 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3669 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 /* beep_cmd set */
3671 } else
3672 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003673 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return -EIO;
3675 }
3676
3677 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003678}
3679
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003680static struct ibm_struct beep_driver_data = {
3681 .name = "beep",
3682 .read = beep_read,
3683 .write = beep_write,
3684};
3685
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003686/*************************************************************************
3687 * Thermal subdriver
3688 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003689
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003690enum thermal_access_mode {
3691 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3692 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3693 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3694 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3695 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3696};
3697
3698enum { /* TPACPI_THERMAL_TPEC_* */
3699 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3700 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3701 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3702};
3703
3704#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3705struct ibm_thermal_sensors_struct {
3706 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3707};
3708
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003709static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003710
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003711/* idx is zero-based */
3712static int thermal_get_sensor(int idx, s32 *value)
3713{
3714 int t;
3715 s8 tmp;
3716 char tmpi[5];
3717
3718 t = TP_EC_THERMAL_TMP0;
3719
3720 switch (thermal_read_mode) {
3721#if TPACPI_MAX_THERMAL_SENSORS >= 16
3722 case TPACPI_THERMAL_TPEC_16:
3723 if (idx >= 8 && idx <= 15) {
3724 t = TP_EC_THERMAL_TMP8;
3725 idx -= 8;
3726 }
3727 /* fallthrough */
3728#endif
3729 case TPACPI_THERMAL_TPEC_8:
3730 if (idx <= 7) {
3731 if (!acpi_ec_read(t + idx, &tmp))
3732 return -EIO;
3733 *value = tmp * 1000;
3734 return 0;
3735 }
3736 break;
3737
3738 case TPACPI_THERMAL_ACPI_UPDT:
3739 if (idx <= 7) {
3740 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3741 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3742 return -EIO;
3743 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3744 return -EIO;
3745 *value = (t - 2732) * 100;
3746 return 0;
3747 }
3748 break;
3749
3750 case TPACPI_THERMAL_ACPI_TMP07:
3751 if (idx <= 7) {
3752 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3753 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3754 return -EIO;
3755 if (t > 127 || t < -127)
3756 t = TP_EC_THERMAL_TMP_NA;
3757 *value = t * 1000;
3758 return 0;
3759 }
3760 break;
3761
3762 case TPACPI_THERMAL_NONE:
3763 default:
3764 return -ENOSYS;
3765 }
3766
3767 return -EINVAL;
3768}
3769
3770static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3771{
3772 int res, i;
3773 int n;
3774
3775 n = 8;
3776 i = 0;
3777
3778 if (!s)
3779 return -EINVAL;
3780
3781 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3782 n = 16;
3783
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003784 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003785 res = thermal_get_sensor(i, &s->temp[i]);
3786 if (res)
3787 return res;
3788 }
3789
3790 return n;
3791}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003792
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003793/* sysfs temp##_input -------------------------------------------------- */
3794
3795static ssize_t thermal_temp_input_show(struct device *dev,
3796 struct device_attribute *attr,
3797 char *buf)
3798{
3799 struct sensor_device_attribute *sensor_attr =
3800 to_sensor_dev_attr(attr);
3801 int idx = sensor_attr->index;
3802 s32 value;
3803 int res;
3804
3805 res = thermal_get_sensor(idx, &value);
3806 if (res)
3807 return res;
3808 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3809 return -ENXIO;
3810
3811 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3812}
3813
3814#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003815 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3816 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003817
3818static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3819 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3820 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3821 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3822 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3823 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3824 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3825 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3826 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3827 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3828 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3829 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3830 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3831 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3832 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3833 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3834 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3835};
3836
3837#define THERMAL_ATTRS(X) \
3838 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3839
3840static struct attribute *thermal_temp_input_attr[] = {
3841 THERMAL_ATTRS(8),
3842 THERMAL_ATTRS(9),
3843 THERMAL_ATTRS(10),
3844 THERMAL_ATTRS(11),
3845 THERMAL_ATTRS(12),
3846 THERMAL_ATTRS(13),
3847 THERMAL_ATTRS(14),
3848 THERMAL_ATTRS(15),
3849 THERMAL_ATTRS(0),
3850 THERMAL_ATTRS(1),
3851 THERMAL_ATTRS(2),
3852 THERMAL_ATTRS(3),
3853 THERMAL_ATTRS(4),
3854 THERMAL_ATTRS(5),
3855 THERMAL_ATTRS(6),
3856 THERMAL_ATTRS(7),
3857 NULL
3858};
3859
3860static const struct attribute_group thermal_temp_input16_group = {
3861 .attrs = thermal_temp_input_attr
3862};
3863
3864static const struct attribute_group thermal_temp_input8_group = {
3865 .attrs = &thermal_temp_input_attr[8]
3866};
3867
3868#undef THERMAL_SENSOR_ATTR_TEMP
3869#undef THERMAL_ATTRS
3870
3871/* --------------------------------------------------------------------- */
3872
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003873static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003874{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003875 u8 t, ta1, ta2;
3876 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003877 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003878 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003879
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003880 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3881
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003882 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003883
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003884 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003885 /*
3886 * Direct EC access mode: sensors at registers
3887 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3888 * non-implemented, thermal sensors return 0x80 when
3889 * not available
3890 */
3891
3892 ta1 = ta2 = 0;
3893 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003894 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003895 ta1 |= t;
3896 } else {
3897 ta1 = 0;
3898 break;
3899 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003900 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003901 ta2 |= t;
3902 } else {
3903 ta1 = 0;
3904 break;
3905 }
3906 }
3907 if (ta1 == 0) {
3908 /* This is sheer paranoia, but we handle it anyway */
3909 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003910 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003911 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003912 "falling back to ACPI TMPx access "
3913 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003914 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003915 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003916 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003917 "ThinkPad ACPI EC access misbehaving, "
3918 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003919 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003920 }
3921 } else {
3922 thermal_read_mode =
3923 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003924 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003925 }
3926 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003927 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3928 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003929 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003930 } else {
3931 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003932 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003933 }
3934 } else {
3935 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003936 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003937 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003938
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003939 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3940 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3941 thermal_read_mode);
3942
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003943 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003944 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003945 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003946 &thermal_temp_input16_group);
3947 if (res)
3948 return res;
3949 break;
3950 case TPACPI_THERMAL_TPEC_8:
3951 case TPACPI_THERMAL_ACPI_TMP07:
3952 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003953 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003954 &thermal_temp_input8_group);
3955 if (res)
3956 return res;
3957 break;
3958 case TPACPI_THERMAL_NONE:
3959 default:
3960 return 1;
3961 }
3962
3963 return 0;
3964}
3965
3966static void thermal_exit(void)
3967{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003968 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003969 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003970 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003971 &thermal_temp_input16_group);
3972 break;
3973 case TPACPI_THERMAL_TPEC_8:
3974 case TPACPI_THERMAL_ACPI_TMP07:
3975 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003976 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003977 &thermal_temp_input16_group);
3978 break;
3979 case TPACPI_THERMAL_NONE:
3980 default:
3981 break;
3982 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003983}
3984
3985static int thermal_read(char *p)
3986{
3987 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003988 int n, i;
3989 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003990
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003991 n = thermal_get_sensors(&t);
3992 if (unlikely(n < 0))
3993 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003994
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003995 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003996
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003997 if (n > 0) {
3998 for (i = 0; i < (n - 1); i++)
3999 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4000 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4001 } else
4002 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004003
4004 return len;
4005}
4006
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004007static struct ibm_struct thermal_driver_data = {
4008 .name = "thermal",
4009 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004010 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004011};
4012
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004013/*************************************************************************
4014 * EC Dump subdriver
4015 */
4016
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004017static u8 ecdump_regs[256];
4018
4019static int ecdump_read(char *p)
4020{
4021 int len = 0;
4022 int i, j;
4023 u8 v;
4024
4025 len += sprintf(p + len, "EC "
4026 " +00 +01 +02 +03 +04 +05 +06 +07"
4027 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4028 for (i = 0; i < 256; i += 16) {
4029 len += sprintf(p + len, "EC 0x%02x:", i);
4030 for (j = 0; j < 16; j++) {
4031 if (!acpi_ec_read(i + j, &v))
4032 break;
4033 if (v != ecdump_regs[i + j])
4034 len += sprintf(p + len, " *%02x", v);
4035 else
4036 len += sprintf(p + len, " %02x", v);
4037 ecdump_regs[i + j] = v;
4038 }
4039 len += sprintf(p + len, "\n");
4040 if (j != 16)
4041 break;
4042 }
4043
4044 /* These are way too dangerous to advertise openly... */
4045#if 0
4046 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4047 " (<offset> is 00-ff, <value> is 00-ff)\n");
4048 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4049 " (<offset> is 00-ff, <value> is 0-255)\n");
4050#endif
4051 return len;
4052}
4053
4054static int ecdump_write(char *buf)
4055{
4056 char *cmd;
4057 int i, v;
4058
4059 while ((cmd = next_cmd(&buf))) {
4060 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4061 /* i and v set */
4062 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4063 /* i and v set */
4064 } else
4065 return -EINVAL;
4066 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4067 if (!acpi_ec_write(i, v))
4068 return -EIO;
4069 } else
4070 return -EINVAL;
4071 }
4072
4073 return 0;
4074}
4075
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004076static struct ibm_struct ecdump_driver_data = {
4077 .name = "ecdump",
4078 .read = ecdump_read,
4079 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004080 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004081};
4082
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004083/*************************************************************************
4084 * Backlight/brightness subdriver
4085 */
Holger Macht8acb0252006-10-20 14:30:28 -07004086
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004087#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4088
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004089static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004090static int brightness_offset = 0x31;
4091static int brightness_mode;
4092static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4093
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004094static struct mutex brightness_mutex;
4095
4096/*
4097 * ThinkPads can read brightness from two places: EC 0x31, or
4098 * CMOS NVRAM byte 0x5E, bits 0-3.
4099 */
4100static int brightness_get(struct backlight_device *bd)
4101{
4102 u8 lec = 0, lcmos = 0, level = 0;
4103
4104 if (brightness_mode & 1) {
4105 if (!acpi_ec_read(brightness_offset, &lec))
4106 return -EIO;
4107 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4108 level = lec;
4109 };
4110 if (brightness_mode & 2) {
4111 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4112 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4113 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4114 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4115 level = lcmos;
4116 }
4117
4118 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004119 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004120 "CMOS NVRAM (%u) and EC (%u) do not agree "
4121 "on display brightness level\n",
4122 (unsigned int) lcmos,
4123 (unsigned int) lec);
4124 return -EIO;
4125 }
4126
4127 return level;
4128}
4129
4130/* May return EINTR which can always be mapped to ERESTARTSYS */
4131static int brightness_set(int value)
4132{
4133 int cmos_cmd, inc, i, res;
4134 int current_value;
4135
4136 if (value > ((tp_features.bright_16levels)? 15 : 7))
4137 return -EINVAL;
4138
4139 res = mutex_lock_interruptible(&brightness_mutex);
4140 if (res < 0)
4141 return res;
4142
4143 current_value = brightness_get(NULL);
4144 if (current_value < 0) {
4145 res = current_value;
4146 goto errout;
4147 }
4148
4149 cmos_cmd = value > current_value ?
4150 TP_CMOS_BRIGHTNESS_UP :
4151 TP_CMOS_BRIGHTNESS_DOWN;
4152 inc = (value > current_value)? 1 : -1;
4153
4154 res = 0;
4155 for (i = current_value; i != value; i += inc) {
4156 if ((brightness_mode & 2) &&
4157 issue_thinkpad_cmos_command(cmos_cmd)) {
4158 res = -EIO;
4159 goto errout;
4160 }
4161 if ((brightness_mode & 1) &&
4162 !acpi_ec_write(brightness_offset, i + inc)) {
4163 res = -EIO;
4164 goto errout;;
4165 }
4166 }
4167
4168errout:
4169 mutex_unlock(&brightness_mutex);
4170 return res;
4171}
4172
4173/* sysfs backlight class ----------------------------------------------- */
4174
4175static int brightness_update_status(struct backlight_device *bd)
4176{
4177 /* it is the backlight class's job (caller) to handle
4178 * EINTR and other errors properly */
4179 return brightness_set(
4180 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4181 bd->props.power == FB_BLANK_UNBLANK) ?
4182 bd->props.brightness : 0);
4183}
Holger Macht8acb0252006-10-20 14:30:28 -07004184
Richard Purdie599a52d2007-02-10 23:07:48 +00004185static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004186 .get_brightness = brightness_get,
4187 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004188};
4189
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004190/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004191
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004192static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4193{
4194 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4195 union acpi_object *obj;
4196 int rc;
4197
4198 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4199 obj = (union acpi_object *)buffer.pointer;
4200 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004201 printk(TPACPI_ERR "Unknown BCLL data, "
4202 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004203 rc = 0;
4204 } else {
4205 rc = obj->package.count;
4206 }
4207 } else {
4208 return 0;
4209 }
4210
4211 kfree(buffer.pointer);
4212 return rc;
4213}
4214
4215static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4216 void *context, void **rv)
4217{
4218 char name[ACPI_PATH_SEGMENT_LENGTH];
4219 struct acpi_buffer buffer = { sizeof(name), &name };
4220
4221 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4222 !strncmp("BCLL", name, sizeof(name) - 1)) {
4223 if (tpacpi_query_bcll_levels(handle) == 16) {
4224 *rv = handle;
4225 return AE_CTRL_TERMINATE;
4226 } else {
4227 return AE_OK;
4228 }
4229 } else {
4230 return AE_OK;
4231 }
4232}
4233
4234static int __init brightness_check_levels(void)
4235{
4236 int status;
4237 void *found_node = NULL;
4238
4239 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004240 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004241 }
4242 if (!vid_handle)
4243 return 0;
4244
4245 /* Search for a BCLL package with 16 levels */
4246 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004247 brightness_find_bcll, NULL,
4248 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004249
4250 return (ACPI_SUCCESS(status) && found_node != NULL);
4251}
4252
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004253static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4254 void *context, void **rv)
4255{
4256 char name[ACPI_PATH_SEGMENT_LENGTH];
4257 struct acpi_buffer buffer = { sizeof(name), &name };
4258
4259 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4260 !strncmp("_BCL", name, sizeof(name) - 1)) {
4261 *rv = handle;
4262 return AE_CTRL_TERMINATE;
4263 } else {
4264 return AE_OK;
4265 }
4266}
4267
4268static int __init brightness_check_std_acpi_support(void)
4269{
4270 int status;
4271 void *found_node = NULL;
4272
4273 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004274 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004275 }
4276 if (!vid_handle)
4277 return 0;
4278
4279 /* Search for a _BCL method, but don't execute it */
4280 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004281 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004282
4283 return (ACPI_SUCCESS(status) && found_node != NULL);
4284}
4285
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004286static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004287{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004288 int b;
4289
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004290 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4291
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004292 mutex_init(&brightness_mutex);
4293
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004294 if (!brightness_enable) {
4295 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004296 "brightness support disabled by "
4297 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004298 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004299 } else if (brightness_enable > 1) {
4300 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004301 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004302 "standard ACPI backlight interface "
4303 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004304 return 1;
4305 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004306 }
4307
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004308 if (!brightness_mode) {
4309 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4310 brightness_mode = 2;
4311 else
4312 brightness_mode = 3;
4313
4314 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4315 brightness_mode);
4316 }
4317
4318 if (brightness_mode > 3)
4319 return -EINVAL;
4320
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004321 tp_features.bright_16levels =
4322 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4323 brightness_check_levels();
4324
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004325 b = brightness_get(NULL);
4326 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004327 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004328
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004329 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004330 printk(TPACPI_INFO
4331 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004332
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004333 ibm_backlight_device = backlight_device_register(
4334 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4335 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004336 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004337 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004338 return PTR_ERR(ibm_backlight_device);
4339 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004340 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004341
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004342 ibm_backlight_device->props.max_brightness =
4343 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004344 ibm_backlight_device->props.brightness = b;
4345 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004346
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004347 return 0;
4348}
4349
4350static void brightness_exit(void)
4351{
4352 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004353 vdbg_printk(TPACPI_DBG_EXIT,
4354 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004355 backlight_device_unregister(ibm_backlight_device);
4356 ibm_backlight_device = NULL;
4357 }
4358}
4359
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004360static int brightness_read(char *p)
4361{
4362 int len = 0;
4363 int level;
4364
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004365 level = brightness_get(NULL);
4366 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004367 len += sprintf(p + len, "level:\t\tunreadable\n");
4368 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004369 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004370 len += sprintf(p + len, "commands:\tup, down\n");
4371 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004372 " (<level> is 0-%d)\n",
4373 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004374 }
4375
4376 return len;
4377}
4378
4379static int brightness_write(char *buf)
4380{
4381 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004382 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004383 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004384 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004385
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004386 level = brightness_get(NULL);
4387 if (level < 0)
4388 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004389
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004390 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004391 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004392 if (level < max_level)
4393 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004394 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004395 if (level > 0)
4396 level--;
4397 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4398 level >= 0 && level <= max_level) {
4399 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004400 } else
4401 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004402 }
4403
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004404 /*
4405 * Now we know what the final level should be, so we try to set it.
4406 * Doing it this way makes the syscall restartable in case of EINTR
4407 */
4408 rc = brightness_set(level);
4409 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004410}
4411
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004412static struct ibm_struct brightness_driver_data = {
4413 .name = "brightness",
4414 .read = brightness_read,
4415 .write = brightness_write,
4416 .exit = brightness_exit,
4417};
4418
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004419/*************************************************************************
4420 * Volume subdriver
4421 */
4422
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004423static int volume_offset = 0x30;
4424
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004425static int volume_read(char *p)
4426{
4427 int len = 0;
4428 u8 level;
4429
4430 if (!acpi_ec_read(volume_offset, &level)) {
4431 len += sprintf(p + len, "level:\t\tunreadable\n");
4432 } else {
4433 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4434 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4435 len += sprintf(p + len, "commands:\tup, down, mute\n");
4436 len += sprintf(p + len, "commands:\tlevel <level>"
4437 " (<level> is 0-15)\n");
4438 }
4439
4440 return len;
4441}
4442
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004443static int volume_write(char *buf)
4444{
4445 int cmos_cmd, inc, i;
4446 u8 level, mute;
4447 int new_level, new_mute;
4448 char *cmd;
4449
4450 while ((cmd = next_cmd(&buf))) {
4451 if (!acpi_ec_read(volume_offset, &level))
4452 return -EIO;
4453 new_mute = mute = level & 0x40;
4454 new_level = level = level & 0xf;
4455
4456 if (strlencmp(cmd, "up") == 0) {
4457 if (mute)
4458 new_mute = 0;
4459 else
4460 new_level = level == 15 ? 15 : level + 1;
4461 } else if (strlencmp(cmd, "down") == 0) {
4462 if (mute)
4463 new_mute = 0;
4464 else
4465 new_level = level == 0 ? 0 : level - 1;
4466 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4467 new_level >= 0 && new_level <= 15) {
4468 /* new_level set */
4469 } else if (strlencmp(cmd, "mute") == 0) {
4470 new_mute = 0x40;
4471 } else
4472 return -EINVAL;
4473
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004474 if (new_level != level) {
4475 /* mute doesn't change */
4476
4477 cmos_cmd = (new_level > level) ?
4478 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004479 inc = new_level > level ? 1 : -1;
4480
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004481 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004482 !acpi_ec_write(volume_offset, level)))
4483 return -EIO;
4484
4485 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004486 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004487 !acpi_ec_write(volume_offset, i + inc))
4488 return -EIO;
4489
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004490 if (mute &&
4491 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4492 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004493 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004494 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004495 }
4496
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004497 if (new_mute != mute) {
4498 /* level doesn't change */
4499
4500 cmos_cmd = (new_mute) ?
4501 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004502
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004503 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004504 !acpi_ec_write(volume_offset, level + new_mute))
4505 return -EIO;
4506 }
4507 }
4508
4509 return 0;
4510}
4511
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004512static struct ibm_struct volume_driver_data = {
4513 .name = "volume",
4514 .read = volume_read,
4515 .write = volume_write,
4516};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004517
4518/*************************************************************************
4519 * Fan subdriver
4520 */
4521
4522/*
4523 * FAN ACCESS MODES
4524 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004525 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004526 * ACPI GFAN method: returns fan level
4527 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004528 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004529 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004530 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004531 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004532 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4533 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004534 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4535 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004536 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004537 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004538 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4539 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004540 *
4541 * Fan speed changes of any sort (including those caused by the
4542 * disengaged mode) are usually done slowly by the firmware as the
4543 * maximum ammount of fan duty cycle change per second seems to be
4544 * limited.
4545 *
4546 * Reading is not available if GFAN exists.
4547 * Writing is not available if SFAN exists.
4548 *
4549 * Bits
4550 * 7 automatic mode engaged;
4551 * (default operation mode of the ThinkPad)
4552 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004553 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004554 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004555 * the tachometer while the fan controller ramps up
4556 * the speed (which can take up to a few *minutes*).
4557 * Speeds up fan to 100% duty-cycle, which is far above
4558 * the standard RPM levels. It is not impossible that
4559 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004560 * 5-3 unused in some models. Extra bits for fan level
4561 * in others, but still useless as all values above
4562 * 7 map to the same speed as level 7 in these models.
4563 * 2-0 fan level (0..7 usually)
4564 * 0x00 = stop
4565 * 0x07 = max (set when temperatures critical)
4566 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004567 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004568 *
4569 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4570 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4571 * does so, its initial value is meaningless (0x07).
4572 *
4573 * For firmware bugs, refer to:
4574 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4575 *
4576 * ----
4577 *
4578 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4579 * Main fan tachometer reading (in RPM)
4580 *
4581 * This register is present on all ThinkPads with a new-style EC, and
4582 * it is known not to be present on the A21m/e, and T22, as there is
4583 * something else in offset 0x84 according to the ACPI DSDT. Other
4584 * ThinkPads from this same time period (and earlier) probably lack the
4585 * tachometer as well.
4586 *
4587 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4588 * was never fixed by IBM to report the EC firmware version string
4589 * probably support the tachometer (like the early X models), so
4590 * detecting it is quite hard. We need more data to know for sure.
4591 *
4592 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4593 * might result.
4594 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004595 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4596 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004597 *
4598 * For firmware bugs, refer to:
4599 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4600 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004601 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004602 * ThinkPad X31, X40, X41. Not available in the X60.
4603 *
4604 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4605 * high speed. ACPI DSDT seems to map these three speeds to levels
4606 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4607 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4608 *
4609 * The speeds are stored on handles
4610 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4611 *
4612 * There are three default speed sets, acessible as handles:
4613 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4614 *
4615 * ACPI DSDT switches which set is in use depending on various
4616 * factors.
4617 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004618 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004619 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4620 * but the ACPI tables just mention level 7.
4621 */
4622
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004623enum { /* Fan control constants */
4624 fan_status_offset = 0x2f, /* EC register 0x2f */
4625 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4626 * 0x84 must be read before 0x85 */
4627
4628 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4629 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4630
4631 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4632};
4633
4634enum fan_status_access_mode {
4635 TPACPI_FAN_NONE = 0, /* No fan status or control */
4636 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4637 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4638};
4639
4640enum fan_control_access_mode {
4641 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4642 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4643 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4644 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4645};
4646
4647enum fan_control_commands {
4648 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4649 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4650 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4651 * and also watchdog cmd */
4652};
4653
4654static int fan_control_allowed;
4655
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004656static enum fan_status_access_mode fan_status_access_mode;
4657static enum fan_control_access_mode fan_control_access_mode;
4658static enum fan_control_commands fan_control_commands;
4659
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004660static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004661static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004662static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004663
4664static struct mutex fan_mutex;
4665
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004666static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004667static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004668
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004669TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4670TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004671 "\\FSPD", /* 600e/x, 770e, 770x */
4672 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004673TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004674 "JFNS", /* 770x-JL */
4675 ); /* all others */
4676
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004677/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004678 * Call with fan_mutex held
4679 */
4680static void fan_update_desired_level(u8 status)
4681{
4682 if ((status &
4683 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4684 if (status > 7)
4685 fan_control_desired_level = 7;
4686 else
4687 fan_control_desired_level = status;
4688 }
4689}
4690
4691static int fan_get_status(u8 *status)
4692{
4693 u8 s;
4694
4695 /* TODO:
4696 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4697
4698 switch (fan_status_access_mode) {
4699 case TPACPI_FAN_RD_ACPI_GFAN:
4700 /* 570, 600e/x, 770e, 770x */
4701
4702 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4703 return -EIO;
4704
4705 if (likely(status))
4706 *status = s & 0x07;
4707
4708 break;
4709
4710 case TPACPI_FAN_RD_TPEC:
4711 /* all except 570, 600e/x, 770e, 770x */
4712 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4713 return -EIO;
4714
4715 if (likely(status))
4716 *status = s;
4717
4718 break;
4719
4720 default:
4721 return -ENXIO;
4722 }
4723
4724 return 0;
4725}
4726
4727static int fan_get_status_safe(u8 *status)
4728{
4729 int rc;
4730 u8 s;
4731
4732 if (mutex_lock_interruptible(&fan_mutex))
4733 return -ERESTARTSYS;
4734 rc = fan_get_status(&s);
4735 if (!rc)
4736 fan_update_desired_level(s);
4737 mutex_unlock(&fan_mutex);
4738
4739 if (status)
4740 *status = s;
4741
4742 return rc;
4743}
4744
4745static int fan_get_speed(unsigned int *speed)
4746{
4747 u8 hi, lo;
4748
4749 switch (fan_status_access_mode) {
4750 case TPACPI_FAN_RD_TPEC:
4751 /* all except 570, 600e/x, 770e, 770x */
4752 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4753 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4754 return -EIO;
4755
4756 if (likely(speed))
4757 *speed = (hi << 8) | lo;
4758
4759 break;
4760
4761 default:
4762 return -ENXIO;
4763 }
4764
4765 return 0;
4766}
4767
4768static int fan_set_level(int level)
4769{
4770 if (!fan_control_allowed)
4771 return -EPERM;
4772
4773 switch (fan_control_access_mode) {
4774 case TPACPI_FAN_WR_ACPI_SFAN:
4775 if (level >= 0 && level <= 7) {
4776 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4777 return -EIO;
4778 } else
4779 return -EINVAL;
4780 break;
4781
4782 case TPACPI_FAN_WR_ACPI_FANS:
4783 case TPACPI_FAN_WR_TPEC:
4784 if ((level != TP_EC_FAN_AUTO) &&
4785 (level != TP_EC_FAN_FULLSPEED) &&
4786 ((level < 0) || (level > 7)))
4787 return -EINVAL;
4788
4789 /* safety net should the EC not support AUTO
4790 * or FULLSPEED mode bits and just ignore them */
4791 if (level & TP_EC_FAN_FULLSPEED)
4792 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01004793 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004794 level |= 4; /* safety min speed 4 */
4795
4796 if (!acpi_ec_write(fan_status_offset, level))
4797 return -EIO;
4798 else
4799 tp_features.fan_ctrl_status_undef = 0;
4800 break;
4801
4802 default:
4803 return -ENXIO;
4804 }
4805 return 0;
4806}
4807
4808static int fan_set_level_safe(int level)
4809{
4810 int rc;
4811
4812 if (!fan_control_allowed)
4813 return -EPERM;
4814
4815 if (mutex_lock_interruptible(&fan_mutex))
4816 return -ERESTARTSYS;
4817
4818 if (level == TPACPI_FAN_LAST_LEVEL)
4819 level = fan_control_desired_level;
4820
4821 rc = fan_set_level(level);
4822 if (!rc)
4823 fan_update_desired_level(level);
4824
4825 mutex_unlock(&fan_mutex);
4826 return rc;
4827}
4828
4829static int fan_set_enable(void)
4830{
4831 u8 s;
4832 int rc;
4833
4834 if (!fan_control_allowed)
4835 return -EPERM;
4836
4837 if (mutex_lock_interruptible(&fan_mutex))
4838 return -ERESTARTSYS;
4839
4840 switch (fan_control_access_mode) {
4841 case TPACPI_FAN_WR_ACPI_FANS:
4842 case TPACPI_FAN_WR_TPEC:
4843 rc = fan_get_status(&s);
4844 if (rc < 0)
4845 break;
4846
4847 /* Don't go out of emergency fan mode */
4848 if (s != 7) {
4849 s &= 0x07;
4850 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4851 }
4852
4853 if (!acpi_ec_write(fan_status_offset, s))
4854 rc = -EIO;
4855 else {
4856 tp_features.fan_ctrl_status_undef = 0;
4857 rc = 0;
4858 }
4859 break;
4860
4861 case TPACPI_FAN_WR_ACPI_SFAN:
4862 rc = fan_get_status(&s);
4863 if (rc < 0)
4864 break;
4865
4866 s &= 0x07;
4867
4868 /* Set fan to at least level 4 */
4869 s |= 4;
4870
4871 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004872 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004873 else
4874 rc = 0;
4875 break;
4876
4877 default:
4878 rc = -ENXIO;
4879 }
4880
4881 mutex_unlock(&fan_mutex);
4882 return rc;
4883}
4884
4885static int fan_set_disable(void)
4886{
4887 int rc;
4888
4889 if (!fan_control_allowed)
4890 return -EPERM;
4891
4892 if (mutex_lock_interruptible(&fan_mutex))
4893 return -ERESTARTSYS;
4894
4895 rc = 0;
4896 switch (fan_control_access_mode) {
4897 case TPACPI_FAN_WR_ACPI_FANS:
4898 case TPACPI_FAN_WR_TPEC:
4899 if (!acpi_ec_write(fan_status_offset, 0x00))
4900 rc = -EIO;
4901 else {
4902 fan_control_desired_level = 0;
4903 tp_features.fan_ctrl_status_undef = 0;
4904 }
4905 break;
4906
4907 case TPACPI_FAN_WR_ACPI_SFAN:
4908 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4909 rc = -EIO;
4910 else
4911 fan_control_desired_level = 0;
4912 break;
4913
4914 default:
4915 rc = -ENXIO;
4916 }
4917
4918
4919 mutex_unlock(&fan_mutex);
4920 return rc;
4921}
4922
4923static int fan_set_speed(int speed)
4924{
4925 int rc;
4926
4927 if (!fan_control_allowed)
4928 return -EPERM;
4929
4930 if (mutex_lock_interruptible(&fan_mutex))
4931 return -ERESTARTSYS;
4932
4933 rc = 0;
4934 switch (fan_control_access_mode) {
4935 case TPACPI_FAN_WR_ACPI_FANS:
4936 if (speed >= 0 && speed <= 65535) {
4937 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4938 speed, speed, speed))
4939 rc = -EIO;
4940 } else
4941 rc = -EINVAL;
4942 break;
4943
4944 default:
4945 rc = -ENXIO;
4946 }
4947
4948 mutex_unlock(&fan_mutex);
4949 return rc;
4950}
4951
4952static void fan_watchdog_reset(void)
4953{
4954 static int fan_watchdog_active;
4955
4956 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4957 return;
4958
4959 if (fan_watchdog_active)
4960 cancel_delayed_work(&fan_watchdog_task);
4961
4962 if (fan_watchdog_maxinterval > 0 &&
4963 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4964 fan_watchdog_active = 1;
4965 if (!schedule_delayed_work(&fan_watchdog_task,
4966 msecs_to_jiffies(fan_watchdog_maxinterval
4967 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004968 printk(TPACPI_ERR
4969 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004970 "watchdog will not trigger\n");
4971 }
4972 } else
4973 fan_watchdog_active = 0;
4974}
4975
4976static void fan_watchdog_fire(struct work_struct *ignored)
4977{
4978 int rc;
4979
4980 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4981 return;
4982
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004983 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004984 rc = fan_set_enable();
4985 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004986 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004987 "will try again later...\n", -rc);
4988 /* reschedule for later */
4989 fan_watchdog_reset();
4990 }
4991}
4992
4993/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004994 * SYSFS fan layout: hwmon compatible (device)
4995 *
4996 * pwm*_enable:
4997 * 0: "disengaged" mode
4998 * 1: manual mode
4999 * 2: native EC "auto" mode (recommended, hardware default)
5000 *
5001 * pwm*: set speed in manual mode, ignored otherwise.
5002 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5003 * interpolation.
5004 *
5005 * fan*_input: tachometer reading, RPM
5006 *
5007 *
5008 * SYSFS fan layout: extensions
5009 *
5010 * fan_watchdog (driver):
5011 * fan watchdog interval in seconds, 0 disables (default), max 120
5012 */
5013
5014/* sysfs fan pwm1_enable ----------------------------------------------- */
5015static ssize_t fan_pwm1_enable_show(struct device *dev,
5016 struct device_attribute *attr,
5017 char *buf)
5018{
5019 int res, mode;
5020 u8 status;
5021
5022 res = fan_get_status_safe(&status);
5023 if (res)
5024 return res;
5025
5026 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5027 if (status != fan_control_initial_status) {
5028 tp_features.fan_ctrl_status_undef = 0;
5029 } else {
5030 /* Return most likely status. In fact, it
5031 * might be the only possible status */
5032 status = TP_EC_FAN_AUTO;
5033 }
5034 }
5035
5036 if (status & TP_EC_FAN_FULLSPEED) {
5037 mode = 0;
5038 } else if (status & TP_EC_FAN_AUTO) {
5039 mode = 2;
5040 } else
5041 mode = 1;
5042
5043 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5044}
5045
5046static ssize_t fan_pwm1_enable_store(struct device *dev,
5047 struct device_attribute *attr,
5048 const char *buf, size_t count)
5049{
5050 unsigned long t;
5051 int res, level;
5052
5053 if (parse_strtoul(buf, 2, &t))
5054 return -EINVAL;
5055
5056 switch (t) {
5057 case 0:
5058 level = TP_EC_FAN_FULLSPEED;
5059 break;
5060 case 1:
5061 level = TPACPI_FAN_LAST_LEVEL;
5062 break;
5063 case 2:
5064 level = TP_EC_FAN_AUTO;
5065 break;
5066 case 3:
5067 /* reserved for software-controlled auto mode */
5068 return -ENOSYS;
5069 default:
5070 return -EINVAL;
5071 }
5072
5073 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005074 if (res == -ENXIO)
5075 return -EINVAL;
5076 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005077 return res;
5078
5079 fan_watchdog_reset();
5080
5081 return count;
5082}
5083
5084static struct device_attribute dev_attr_fan_pwm1_enable =
5085 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5086 fan_pwm1_enable_show, fan_pwm1_enable_store);
5087
5088/* sysfs fan pwm1 ------------------------------------------------------ */
5089static ssize_t fan_pwm1_show(struct device *dev,
5090 struct device_attribute *attr,
5091 char *buf)
5092{
5093 int res;
5094 u8 status;
5095
5096 res = fan_get_status_safe(&status);
5097 if (res)
5098 return res;
5099
5100 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5101 if (status != fan_control_initial_status) {
5102 tp_features.fan_ctrl_status_undef = 0;
5103 } else {
5104 status = TP_EC_FAN_AUTO;
5105 }
5106 }
5107
5108 if ((status &
5109 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5110 status = fan_control_desired_level;
5111
5112 if (status > 7)
5113 status = 7;
5114
5115 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5116}
5117
5118static ssize_t fan_pwm1_store(struct device *dev,
5119 struct device_attribute *attr,
5120 const char *buf, size_t count)
5121{
5122 unsigned long s;
5123 int rc;
5124 u8 status, newlevel;
5125
5126 if (parse_strtoul(buf, 255, &s))
5127 return -EINVAL;
5128
5129 /* scale down from 0-255 to 0-7 */
5130 newlevel = (s >> 5) & 0x07;
5131
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005132 if (mutex_lock_interruptible(&fan_mutex))
5133 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005134
5135 rc = fan_get_status(&status);
5136 if (!rc && (status &
5137 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5138 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005139 if (rc == -ENXIO)
5140 rc = -EINVAL;
5141 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005142 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005143 fan_watchdog_reset();
5144 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005145 }
5146
5147 mutex_unlock(&fan_mutex);
5148 return (rc)? rc : count;
5149}
5150
5151static struct device_attribute dev_attr_fan_pwm1 =
5152 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5153 fan_pwm1_show, fan_pwm1_store);
5154
5155/* sysfs fan fan1_input ------------------------------------------------ */
5156static ssize_t fan_fan1_input_show(struct device *dev,
5157 struct device_attribute *attr,
5158 char *buf)
5159{
5160 int res;
5161 unsigned int speed;
5162
5163 res = fan_get_speed(&speed);
5164 if (res < 0)
5165 return res;
5166
5167 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5168}
5169
5170static struct device_attribute dev_attr_fan_fan1_input =
5171 __ATTR(fan1_input, S_IRUGO,
5172 fan_fan1_input_show, NULL);
5173
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005174/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005175static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5176 char *buf)
5177{
5178 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5179}
5180
5181static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5182 const char *buf, size_t count)
5183{
5184 unsigned long t;
5185
5186 if (parse_strtoul(buf, 120, &t))
5187 return -EINVAL;
5188
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005189 if (!fan_control_allowed)
5190 return -EPERM;
5191
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005192 fan_watchdog_maxinterval = t;
5193 fan_watchdog_reset();
5194
5195 return count;
5196}
5197
5198static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5199 fan_fan_watchdog_show, fan_fan_watchdog_store);
5200
5201/* --------------------------------------------------------------------- */
5202static struct attribute *fan_attributes[] = {
5203 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5204 &dev_attr_fan_fan1_input.attr,
5205 NULL
5206};
5207
5208static const struct attribute_group fan_attr_group = {
5209 .attrs = fan_attributes,
5210};
5211
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005212static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005213{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005214 int rc;
5215
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005216 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5217
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005218 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005219 fan_status_access_mode = TPACPI_FAN_NONE;
5220 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005221 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005222 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005223 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005224 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005225
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005226 TPACPI_ACPIHANDLE_INIT(fans);
5227 TPACPI_ACPIHANDLE_INIT(gfan);
5228 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005229
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005230 if (gfan_handle) {
5231 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005232 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005233 } else {
5234 /* all other ThinkPads: note that even old-style
5235 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005236 if (likely(acpi_ec_read(fan_status_offset,
5237 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005238 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005239
5240 /* In some ThinkPads, neither the EC nor the ACPI
5241 * DSDT initialize the fan status, and it ends up
5242 * being set to 0x07 when it *could* be either
5243 * 0x07 or 0x80.
5244 *
5245 * Enable for TP-1Y (T43), TP-78 (R51e),
5246 * TP-76 (R52), TP-70 (T43, R52), which are known
5247 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005248 if (fan_control_initial_status == 0x07) {
5249 switch (thinkpad_id.ec_model) {
5250 case 0x5931: /* TP-1Y */
5251 case 0x3837: /* TP-78 */
5252 case 0x3637: /* TP-76 */
5253 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005254 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005255 "fan_init: initial fan status "
5256 "is unknown, assuming it is "
5257 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005258 tp_features.fan_ctrl_status_undef = 1;
5259 ;;
5260 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005261 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005262 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005263 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005264 "ThinkPad ACPI EC access misbehaving, "
5265 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005266 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005267 }
5268 }
5269
5270 if (sfan_handle) {
5271 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005272 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005273 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005274 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005275 } else {
5276 if (!gfan_handle) {
5277 /* gfan without sfan means no fan control */
5278 /* all other models implement TP EC 0x2f control */
5279
5280 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005281 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005282 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005283 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005284 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005285 TPACPI_FAN_CMD_SPEED |
5286 TPACPI_FAN_CMD_LEVEL |
5287 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005288 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005289 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005290 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005291 TPACPI_FAN_CMD_LEVEL |
5292 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005293 }
5294 }
5295 }
5296
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005297 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5298 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5299 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5300 fan_status_access_mode, fan_control_access_mode);
5301
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005302 /* fan control master switch */
5303 if (!fan_control_allowed) {
5304 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5305 fan_control_commands = 0;
5306 dbg_printk(TPACPI_DBG_INIT,
5307 "fan control features disabled by parameter\n");
5308 }
5309
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005310 /* update fan_control_desired_level */
5311 if (fan_status_access_mode != TPACPI_FAN_NONE)
5312 fan_get_status_safe(NULL);
5313
5314 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5315 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005316 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005317 &fan_attr_group);
5318 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005319 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005320 &driver_attr_fan_watchdog);
5321 if (rc < 0)
5322 return rc;
5323 return 0;
5324 } else
5325 return 1;
5326}
5327
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005328static void fan_exit(void)
5329{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005330 vdbg_printk(TPACPI_DBG_EXIT,
5331 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005332
5333 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005334 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005335 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5336 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005337
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005338 cancel_delayed_work(&fan_watchdog_task);
5339 flush_scheduled_work();
5340}
5341
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005342static int fan_read(char *p)
5343{
5344 int len = 0;
5345 int rc;
5346 u8 status;
5347 unsigned int speed = 0;
5348
5349 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005350 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005351 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005352 rc = fan_get_status_safe(&status);
5353 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005354 return rc;
5355
5356 len += sprintf(p + len, "status:\t\t%s\n"
5357 "level:\t\t%d\n",
5358 (status != 0) ? "enabled" : "disabled", status);
5359 break;
5360
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005361 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005362 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005363 rc = fan_get_status_safe(&status);
5364 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005365 return rc;
5366
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005367 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005368 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005369 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005370 else
5371 /* Return most likely status. In fact, it
5372 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005373 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005374 }
5375
5376 len += sprintf(p + len, "status:\t\t%s\n",
5377 (status != 0) ? "enabled" : "disabled");
5378
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005379 rc = fan_get_speed(&speed);
5380 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005381 return rc;
5382
5383 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5384
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005385 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005386 /* Disengaged mode takes precedence */
5387 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005388 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005389 len += sprintf(p + len, "level:\t\tauto\n");
5390 else
5391 len += sprintf(p + len, "level:\t\t%d\n", status);
5392 break;
5393
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005394 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005395 default:
5396 len += sprintf(p + len, "status:\t\tnot supported\n");
5397 }
5398
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005399 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005400 len += sprintf(p + len, "commands:\tlevel <level>");
5401
5402 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005403 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005404 len += sprintf(p + len, " (<level> is 0-7)\n");
5405 break;
5406
5407 default:
5408 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005409 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005410 break;
5411 }
5412 }
5413
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005414 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005415 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005416 "commands:\twatchdog <timeout> (<timeout> "
5417 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005418
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005419 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005420 len += sprintf(p + len, "commands:\tspeed <speed>"
5421 " (<speed> is 0-65535)\n");
5422
5423 return len;
5424}
5425
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005426static int fan_write_cmd_level(const char *cmd, int *rc)
5427{
5428 int level;
5429
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005430 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005431 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005432 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005433 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005434 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005435 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005436 return 0;
5437
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005438 *rc = fan_set_level_safe(level);
5439 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005440 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005441 "access mode %d", fan_control_access_mode);
5442
5443 return 1;
5444}
5445
5446static int fan_write_cmd_enable(const char *cmd, int *rc)
5447{
5448 if (strlencmp(cmd, "enable") != 0)
5449 return 0;
5450
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005451 *rc = fan_set_enable();
5452 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005453 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005454 "access mode %d", fan_control_access_mode);
5455
5456 return 1;
5457}
5458
5459static int fan_write_cmd_disable(const char *cmd, int *rc)
5460{
5461 if (strlencmp(cmd, "disable") != 0)
5462 return 0;
5463
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005464 *rc = fan_set_disable();
5465 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005466 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005467 "access mode %d", fan_control_access_mode);
5468
5469 return 1;
5470}
5471
5472static int fan_write_cmd_speed(const char *cmd, int *rc)
5473{
5474 int speed;
5475
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005476 /* TODO:
5477 * Support speed <low> <medium> <high> ? */
5478
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005479 if (sscanf(cmd, "speed %d", &speed) != 1)
5480 return 0;
5481
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005482 *rc = fan_set_speed(speed);
5483 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005484 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005485 "access mode %d", fan_control_access_mode);
5486
5487 return 1;
5488}
5489
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005490static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5491{
5492 int interval;
5493
5494 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5495 return 0;
5496
5497 if (interval < 0 || interval > 120)
5498 *rc = -EINVAL;
5499 else
5500 fan_watchdog_maxinterval = interval;
5501
5502 return 1;
5503}
5504
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005505static int fan_write(char *buf)
5506{
5507 char *cmd;
5508 int rc = 0;
5509
5510 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005511 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005512 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005513 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005514 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005515 fan_write_cmd_disable(cmd, &rc) ||
5516 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005517 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005518 fan_write_cmd_speed(cmd, &rc))
5519 )
5520 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005521 else if (!rc)
5522 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005523 }
5524
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005525 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005526}
5527
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005528static struct ibm_struct fan_driver_data = {
5529 .name = "fan",
5530 .read = fan_read,
5531 .write = fan_write,
5532 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005533};
5534
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005535/****************************************************************************
5536 ****************************************************************************
5537 *
5538 * Infrastructure
5539 *
5540 ****************************************************************************
5541 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005543/* sysfs name ---------------------------------------------------------- */
5544static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5545 struct device_attribute *attr,
5546 char *buf)
5547{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005548 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005549}
5550
5551static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5552 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5553
5554/* --------------------------------------------------------------------- */
5555
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005556/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005557static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005558
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005559/*
5560 * Module and infrastructure proble, init and exit handling
5561 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005563static int force_load;
5564
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005565#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005566static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005567{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005568 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005569
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005570 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005571}
5572#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5573
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005574static void ibm_exit(struct ibm_struct *ibm)
5575{
5576 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5577
5578 list_del_init(&ibm->all_drivers);
5579
5580 if (ibm->flags.acpi_notify_installed) {
5581 dbg_printk(TPACPI_DBG_EXIT,
5582 "%s: acpi_remove_notify_handler\n", ibm->name);
5583 BUG_ON(!ibm->acpi);
5584 acpi_remove_notify_handler(*ibm->acpi->handle,
5585 ibm->acpi->type,
5586 dispatch_acpi_notify);
5587 ibm->flags.acpi_notify_installed = 0;
5588 ibm->flags.acpi_notify_installed = 0;
5589 }
5590
5591 if (ibm->flags.proc_created) {
5592 dbg_printk(TPACPI_DBG_EXIT,
5593 "%s: remove_proc_entry\n", ibm->name);
5594 remove_proc_entry(ibm->name, proc_dir);
5595 ibm->flags.proc_created = 0;
5596 }
5597
5598 if (ibm->flags.acpi_driver_registered) {
5599 dbg_printk(TPACPI_DBG_EXIT,
5600 "%s: acpi_bus_unregister_driver\n", ibm->name);
5601 BUG_ON(!ibm->acpi);
5602 acpi_bus_unregister_driver(ibm->acpi->driver);
5603 kfree(ibm->acpi->driver);
5604 ibm->acpi->driver = NULL;
5605 ibm->flags.acpi_driver_registered = 0;
5606 }
5607
5608 if (ibm->flags.init_called && ibm->exit) {
5609 ibm->exit();
5610 ibm->flags.init_called = 0;
5611 }
5612
5613 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5614}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005615
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005616static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617{
5618 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005619 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 struct proc_dir_entry *entry;
5621
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005622 BUG_ON(ibm == NULL);
5623
5624 INIT_LIST_HEAD(&ibm->all_drivers);
5625
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005626 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return 0;
5628
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005629 dbg_printk(TPACPI_DBG_INIT,
5630 "probing for %s\n", ibm->name);
5631
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005632 if (iibm->init) {
5633 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005634 if (ret > 0)
5635 return 0; /* probe failed */
5636 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005638
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005639 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 }
5641
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005642 if (ibm->acpi) {
5643 if (ibm->acpi->hid) {
5644 ret = register_tpacpi_subdriver(ibm);
5645 if (ret)
5646 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005647 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005648
5649 if (ibm->acpi->notify) {
5650 ret = setup_acpi_notify(ibm);
5651 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005652 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005653 ibm->name);
5654 ret = 0;
5655 goto err_out;
5656 }
5657 if (ret < 0)
5658 goto err_out;
5659 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005660 }
5661
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005662 dbg_printk(TPACPI_DBG_INIT,
5663 "%s installed\n", ibm->name);
5664
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005665 if (ibm->read) {
5666 entry = create_proc_entry(ibm->name,
5667 S_IFREG | S_IRUGO | S_IWUSR,
5668 proc_dir);
5669 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005670 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005671 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005672 ret = -ENODEV;
5673 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005674 }
5675 entry->owner = THIS_MODULE;
5676 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005677 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005678 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005679 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005680 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005683 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5684
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005686
5687err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005688 dbg_printk(TPACPI_DBG_INIT,
5689 "%s: at error exit path with result %d\n",
5690 ibm->name, ret);
5691
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005692 ibm_exit(ibm);
5693 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694}
5695
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005696/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005698static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005699{
Jeff Garzik18552562007-10-03 15:15:40 -04005700 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005701 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005702
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005703 if (!tp)
5704 return;
5705
5706 memset(tp, 0, sizeof(*tp));
5707
5708 if (dmi_name_in_vendors("IBM"))
5709 tp->vendor = PCI_VENDOR_ID_IBM;
5710 else if (dmi_name_in_vendors("LENOVO"))
5711 tp->vendor = PCI_VENDOR_ID_LENOVO;
5712 else
5713 return;
5714
5715 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5716 GFP_KERNEL);
5717 if (!tp->bios_version_str)
5718 return;
5719 tp->bios_model = tp->bios_version_str[0]
5720 | (tp->bios_version_str[1] << 8);
5721
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005722 /*
5723 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5724 * X32 or newer, all Z series; Some models must have an
5725 * up-to-date BIOS or they will not be detected.
5726 *
5727 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5728 */
5729 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005730 if (sscanf(dev->name,
5731 "IBM ThinkPad Embedded Controller -[%17c",
5732 ec_fw_string) == 1) {
5733 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5734 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005735
5736 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5737 tp->ec_model = ec_fw_string[0]
5738 | (ec_fw_string[1] << 8);
5739 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005740 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005741 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005742
5743 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5744 GFP_KERNEL);
5745 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5746 kfree(tp->model_str);
5747 tp->model_str = NULL;
5748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749}
5750
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005751static int __init probe_for_thinkpad(void)
5752{
5753 int is_thinkpad;
5754
5755 if (acpi_disabled)
5756 return -ENODEV;
5757
5758 /*
5759 * Non-ancient models have better DMI tagging, but very old models
5760 * don't.
5761 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005762 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005763
5764 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005765 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005766 if (!ec_handle) {
5767 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005768 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005769 "Not yet supported ThinkPad detected!\n");
5770 return -ENODEV;
5771 }
5772
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005773 /*
5774 * Risks a regression on very old machines, but reduces potential
5775 * false positives a damn great deal
5776 */
5777 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005778 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005779
5780 if (!is_thinkpad && !force_load)
5781 return -ENODEV;
5782
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005783 return 0;
5784}
5785
5786
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005787/* Module init, exit, parameters */
5788
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005789static struct ibm_init_struct ibms_init[] __initdata = {
5790 {
5791 .init = thinkpad_acpi_driver_init,
5792 .data = &thinkpad_acpi_driver_data,
5793 },
5794 {
5795 .init = hotkey_init,
5796 .data = &hotkey_driver_data,
5797 },
5798 {
5799 .init = bluetooth_init,
5800 .data = &bluetooth_driver_data,
5801 },
5802 {
5803 .init = wan_init,
5804 .data = &wan_driver_data,
5805 },
5806 {
5807 .init = video_init,
5808 .data = &video_driver_data,
5809 },
5810 {
5811 .init = light_init,
5812 .data = &light_driver_data,
5813 },
5814#ifdef CONFIG_THINKPAD_ACPI_DOCK
5815 {
5816 .init = dock_init,
5817 .data = &dock_driver_data[0],
5818 },
5819 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005820 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005821 .data = &dock_driver_data[1],
5822 },
5823#endif
5824#ifdef CONFIG_THINKPAD_ACPI_BAY
5825 {
5826 .init = bay_init,
5827 .data = &bay_driver_data,
5828 },
5829#endif
5830 {
5831 .init = cmos_init,
5832 .data = &cmos_driver_data,
5833 },
5834 {
5835 .init = led_init,
5836 .data = &led_driver_data,
5837 },
5838 {
5839 .init = beep_init,
5840 .data = &beep_driver_data,
5841 },
5842 {
5843 .init = thermal_init,
5844 .data = &thermal_driver_data,
5845 },
5846 {
5847 .data = &ecdump_driver_data,
5848 },
5849 {
5850 .init = brightness_init,
5851 .data = &brightness_driver_data,
5852 },
5853 {
5854 .data = &volume_driver_data,
5855 },
5856 {
5857 .init = fan_init,
5858 .data = &fan_driver_data,
5859 },
5860};
5861
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005862static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5863{
5864 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005865 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005866
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005867 if (!kp || !kp->name || !val)
5868 return -EINVAL;
5869
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005870 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5871 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005872 WARN_ON(ibm == NULL);
5873
5874 if (!ibm || !ibm->name)
5875 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005876
5877 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5878 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005879 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005880 strcpy(ibms_init[i].param, val);
5881 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005882 return 0;
5883 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005884 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005885
5886 return -EINVAL;
5887}
5888
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005889module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005890MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005891 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005892
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005893module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005894MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005895
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005896module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005897MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005898 "Attempts to load the driver even on a "
5899 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005900
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005901module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005902MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005903 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005904
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005905module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005906MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005907 "Selects brightness control strategy: "
5908 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005909
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005910module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005911MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005912 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005913
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005914module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005915MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005916 "used for backwards compatibility with userspace, "
5917 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005918
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005919#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005920 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5921 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005922 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005923
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005924TPACPI_PARAM(hotkey);
5925TPACPI_PARAM(bluetooth);
5926TPACPI_PARAM(video);
5927TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005928#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005929TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005930#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005931#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005932TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005933#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005934TPACPI_PARAM(cmos);
5935TPACPI_PARAM(led);
5936TPACPI_PARAM(beep);
5937TPACPI_PARAM(ecdump);
5938TPACPI_PARAM(brightness);
5939TPACPI_PARAM(volume);
5940TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005941
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005942static void thinkpad_acpi_module_exit(void)
5943{
5944 struct ibm_struct *ibm, *itmp;
5945
5946 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5947
5948 list_for_each_entry_safe_reverse(ibm, itmp,
5949 &tpacpi_all_drivers,
5950 all_drivers) {
5951 ibm_exit(ibm);
5952 }
5953
5954 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5955
5956 if (tpacpi_inputdev) {
5957 if (tp_features.input_device_registered)
5958 input_unregister_device(tpacpi_inputdev);
5959 else
5960 input_free_device(tpacpi_inputdev);
5961 }
5962
5963 if (tpacpi_hwmon)
5964 hwmon_device_unregister(tpacpi_hwmon);
5965
5966 if (tp_features.sensors_pdev_attrs_registered)
5967 device_remove_file(&tpacpi_sensors_pdev->dev,
5968 &dev_attr_thinkpad_acpi_pdev_name);
5969 if (tpacpi_sensors_pdev)
5970 platform_device_unregister(tpacpi_sensors_pdev);
5971 if (tpacpi_pdev)
5972 platform_device_unregister(tpacpi_pdev);
5973
5974 if (tp_features.sensors_pdrv_attrs_registered)
5975 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5976 if (tp_features.platform_drv_attrs_registered)
5977 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5978
5979 if (tp_features.sensors_pdrv_registered)
5980 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5981
5982 if (tp_features.platform_drv_registered)
5983 platform_driver_unregister(&tpacpi_pdriver);
5984
5985 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005986 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005987
5988 kfree(thinkpad_id.bios_version_str);
5989 kfree(thinkpad_id.ec_version_str);
5990 kfree(thinkpad_id.model_str);
5991}
5992
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005993
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005994static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995{
5996 int ret, i;
5997
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005998 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5999
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006000 /* Parameter checking */
6001 if (hotkey_report_mode > 2)
6002 return -EINVAL;
6003
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006004 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006005
6006 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006007 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006008 if (ret) {
6009 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006010 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006013 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006014
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006015 TPACPI_ACPIHANDLE_INIT(ecrd);
6016 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006018 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006020 printk(TPACPI_ERR
6021 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006022 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 return -ENODEV;
6024 }
6025 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006026
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006027 ret = platform_driver_register(&tpacpi_pdriver);
6028 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006029 printk(TPACPI_ERR
6030 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006031 thinkpad_acpi_module_exit();
6032 return ret;
6033 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006034 tp_features.platform_drv_registered = 1;
6035
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006036 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6037 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006038 printk(TPACPI_ERR
6039 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006040 thinkpad_acpi_module_exit();
6041 return ret;
6042 }
6043 tp_features.sensors_pdrv_registered = 1;
6044
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006045 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006046 if (!ret) {
6047 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006048 ret = tpacpi_create_driver_attributes(
6049 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006050 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006051 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006052 printk(TPACPI_ERR
6053 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006054 thinkpad_acpi_module_exit();
6055 return ret;
6056 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006057 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006058
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006059
6060 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006061 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006062 NULL, 0);
6063 if (IS_ERR(tpacpi_pdev)) {
6064 ret = PTR_ERR(tpacpi_pdev);
6065 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006066 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006067 thinkpad_acpi_module_exit();
6068 return ret;
6069 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006070 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006071 TPACPI_HWMON_DRVR_NAME,
6072 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006073 if (IS_ERR(tpacpi_sensors_pdev)) {
6074 ret = PTR_ERR(tpacpi_sensors_pdev);
6075 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006076 printk(TPACPI_ERR
6077 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006078 thinkpad_acpi_module_exit();
6079 return ret;
6080 }
6081 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6082 &dev_attr_thinkpad_acpi_pdev_name);
6083 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006084 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006085 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006086 thinkpad_acpi_module_exit();
6087 return ret;
6088 }
6089 tp_features.sensors_pdev_attrs_registered = 1;
6090 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006091 if (IS_ERR(tpacpi_hwmon)) {
6092 ret = PTR_ERR(tpacpi_hwmon);
6093 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006094 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006095 thinkpad_acpi_module_exit();
6096 return ret;
6097 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006098 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006099 tpacpi_inputdev = input_allocate_device();
6100 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006101 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006102 thinkpad_acpi_module_exit();
6103 return -ENOMEM;
6104 } else {
6105 /* Prepare input device, but don't register */
6106 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006107 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006108 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006109 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6110 thinkpad_id.vendor :
6111 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006112 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6113 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6114 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006115 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6116 ret = ibm_init(&ibms_init[i]);
6117 if (ret >= 0 && *ibms_init[i].param)
6118 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006120 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 return ret;
6122 }
6123 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006124 ret = input_register_device(tpacpi_inputdev);
6125 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006126 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006127 thinkpad_acpi_module_exit();
6128 return ret;
6129 } else {
6130 tp_features.input_device_registered = 1;
6131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006133 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 return 0;
6135}
6136
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006137/* Please remove this in year 2009 */
6138MODULE_ALIAS("ibm_acpi");
6139
6140/*
6141 * DMI matching for module autoloading
6142 *
6143 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6144 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6145 *
6146 * Only models listed in thinkwiki will be supported, so add yours
6147 * if it is not there yet.
6148 */
6149#define IBM_BIOS_MODULE_ALIAS(__type) \
6150 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6151
6152/* Non-ancient thinkpads */
6153MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6154MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6155
6156/* Ancient thinkpad BIOSes have to be identified by
6157 * BIOS type or model number, and there are far less
6158 * BIOS types than model numbers... */
6159IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6160IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6161IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6162
6163MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006164MODULE_DESCRIPTION(TPACPI_DESC);
6165MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006166MODULE_LICENSE("GPL");
6167
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006168module_init(thinkpad_acpi_module_init);
6169module_exit(thinkpad_acpi_module_exit);