blob: bb269d0c677edbc4cadd268dc634e90b6120a80e [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;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200224 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200225 u32 light:1;
226 u32 light_status:1;
227 u32 bright_16levels:1;
228 u32 wan:1;
229 u32 fan_ctrl_status_undef:1;
230 u32 input_device_registered:1;
231 u32 platform_drv_registered:1;
232 u32 platform_drv_attrs_registered:1;
233 u32 sensors_pdrv_registered:1;
234 u32 sensors_pdrv_attrs_registered:1;
235 u32 sensors_pdev_attrs_registered:1;
236 u32 hotkey_poll_active:1;
237} tp_features;
238
239struct thinkpad_id_data {
240 unsigned int vendor; /* ThinkPad vendor:
241 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
242
243 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
244 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
245
246 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
247 u16 ec_model;
248
249 char *model_str;
250};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200251static struct thinkpad_id_data thinkpad_id;
252
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300253static enum {
254 TPACPI_LIFE_INIT = 0,
255 TPACPI_LIFE_RUNNING,
256 TPACPI_LIFE_EXITING,
257} tpacpi_lifecycle;
258
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200259static int experimental;
260static u32 dbg_level;
261
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300262/****************************************************************************
263 ****************************************************************************
264 *
265 * ACPI Helpers and device model
266 *
267 ****************************************************************************
268 ****************************************************************************/
269
270/*************************************************************************
271 * ACPI basic handles
272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300274static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200276#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 static acpi_handle object##_handle; \
278 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400279 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 static char *object##_paths[] = { paths }
281
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200282TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400283 "\\_SB.PCI.ISA.EC", /* 570 */
284 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
285 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
286 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
287 "\\_SB.PCI0.ICH3.EC0", /* R31 */
288 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300289 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200291TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
292TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200294TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
295 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400296 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
297 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300298 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400299
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200300TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400301 "^HKEY", /* R30, R31 */
302 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300303 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400304
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200305TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
306 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
307 "\\_SB.PCI0.VID0", /* 770e */
308 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
309 "\\_SB.PCI0.AGP.VID", /* all others */
310 ); /* R30, R31 */
311
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400312
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300313/*************************************************************************
314 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200315 */
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static int acpi_evalf(acpi_handle handle,
318 void *res, char *method, char *fmt, ...)
319{
320 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400321 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200322 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400323 struct acpi_buffer result, *resultp;
324 union acpi_object out_obj;
325 acpi_status status;
326 va_list ap;
327 char res_type;
328 int success;
329 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200332 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334 }
335
336 if (*fmt == 'q') {
337 quiet = 1;
338 fmt++;
339 } else
340 quiet = 0;
341
342 res_type = *(fmt++);
343
344 params.count = 0;
345 params.pointer = &in_objs[0];
346
347 va_start(ap, fmt);
348 while (*fmt) {
349 char c = *(fmt++);
350 switch (c) {
351 case 'd': /* int */
352 in_objs[params.count].integer.value = va_arg(ap, int);
353 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
354 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400355 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200357 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 "with invalid format character '%c'\n", c);
359 return 0;
360 }
361 }
362 va_end(ap);
363
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400364 if (res_type != 'v') {
365 result.length = sizeof(out_obj);
366 result.pointer = &out_obj;
367 resultp = &result;
368 } else
369 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400371 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400374 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (res)
376 *(int *)res = out_obj.integer.value;
377 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
378 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400379 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 success = status == AE_OK;
381 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400382 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200384 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 "with invalid format character '%c'\n", res_type);
386 return 0;
387 }
388
389 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200390 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 method, fmt0, status);
392
393 return success;
394}
395
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200396static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300397{
398 int v;
399
400 if (ecrd_handle) {
401 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
402 return 0;
403 *p = v;
404 } else {
405 if (ec_read(i, p) < 0)
406 return 0;
407 }
408
409 return 1;
410}
411
412static int acpi_ec_write(int i, u8 v)
413{
414 if (ecwr_handle) {
415 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
416 return 0;
417 } else {
418 if (ec_write(i, v) < 0)
419 return 0;
420 }
421
422 return 1;
423}
424
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200425#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300426static int _sta(acpi_handle handle)
427{
428 int status;
429
430 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
431 status = 0;
432
433 return status;
434}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200435#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300436
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300437static int issue_thinkpad_cmos_command(int cmos_cmd)
438{
439 if (!cmos_handle)
440 return -ENXIO;
441
442 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
443 return -EIO;
444
445 return 0;
446}
447
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300448/*************************************************************************
449 * ACPI device model
450 */
451
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200452#define TPACPI_ACPIHANDLE_INIT(object) \
453 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200454 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
455
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300456static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300457 acpi_handle *handle, acpi_handle parent,
458 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300459{
460 int i;
461 acpi_status status;
462
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300463 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
464 name);
465
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300466 for (i = 0; i < num_paths; i++) {
467 status = acpi_get_handle(parent, paths[i], handle);
468 if (ACPI_SUCCESS(status)) {
469 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300470 dbg_printk(TPACPI_DBG_INIT,
471 "Found ACPI handle %s for %s\n",
472 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300473 return;
474 }
475 }
476
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300477 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
478 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300479 *handle = NULL;
480}
481
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300482static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300483{
484 struct ibm_struct *ibm = data;
485
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300486 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
487 return;
488
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300489 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300490 return;
491
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300492 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300493}
494
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300495static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300496{
497 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300498 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300499
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300500 BUG_ON(!ibm->acpi);
501
502 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300503 return 0;
504
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300505 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300506 "setting up ACPI notify for %s\n", ibm->name);
507
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300508 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
509 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200510 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300511 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300512 return -ENODEV;
513 }
514
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300515 acpi_driver_data(ibm->acpi->device) = ibm;
516 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200517 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300518 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300520 status = acpi_install_notify_handler(*ibm->acpi->handle,
521 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300522 if (ACPI_FAILURE(status)) {
523 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200524 printk(TPACPI_NOTICE
525 "another device driver is already "
526 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300527 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200528 printk(TPACPI_ERR
529 "acpi_install_notify_handler(%s) failed: %d\n",
530 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300531 }
532 return -ENODEV;
533 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300534 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300535 return 0;
536}
537
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300538static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300539{
540 return 0;
541}
542
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300543static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300544{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300545 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300546
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300547 dbg_printk(TPACPI_DBG_INIT,
548 "registering %s as an ACPI driver\n", ibm->name);
549
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300550 BUG_ON(!ibm->acpi);
551
552 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
553 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200554 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300555 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300556 }
557
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200558 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300559 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200560
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300561 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300562
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300563 rc = acpi_bus_register_driver(ibm->acpi->driver);
564 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200565 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200566 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300567 kfree(ibm->acpi->driver);
568 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300569 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300570 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300571
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300572 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300573}
574
575
576/****************************************************************************
577 ****************************************************************************
578 *
579 * Procfs Helpers
580 *
581 ****************************************************************************
582 ****************************************************************************/
583
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300584static int dispatch_procfs_read(char *page, char **start, off_t off,
585 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300586{
587 struct ibm_struct *ibm = data;
588 int len;
589
590 if (!ibm || !ibm->read)
591 return -EINVAL;
592
593 len = ibm->read(page);
594 if (len < 0)
595 return len;
596
597 if (len <= off + count)
598 *eof = 1;
599 *start = page + off;
600 len -= off;
601 if (len > count)
602 len = count;
603 if (len < 0)
604 len = 0;
605
606 return len;
607}
608
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300609static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200610 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300611 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300612{
613 struct ibm_struct *ibm = data;
614 char *kernbuf;
615 int ret;
616
617 if (!ibm || !ibm->write)
618 return -EINVAL;
619
620 kernbuf = kmalloc(count + 2, GFP_KERNEL);
621 if (!kernbuf)
622 return -ENOMEM;
623
624 if (copy_from_user(kernbuf, userbuf, count)) {
625 kfree(kernbuf);
626 return -EFAULT;
627 }
628
629 kernbuf[count] = 0;
630 strcat(kernbuf, ",");
631 ret = ibm->write(kernbuf);
632 if (ret == 0)
633 ret = count;
634
635 kfree(kernbuf);
636
637 return ret;
638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640static char *next_cmd(char **cmds)
641{
642 char *start = *cmds;
643 char *end;
644
645 while ((end = strchr(start, ',')) && end == start)
646 start = end + 1;
647
648 if (!end)
649 return NULL;
650
651 *end = 0;
652 *cmds = end + 1;
653 return start;
654}
655
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300656
657/****************************************************************************
658 ****************************************************************************
659 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300660 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300661 *
662 ****************************************************************************
663 ****************************************************************************/
664
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300665static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300666static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700667static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300668static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300669static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200670static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300671
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200672static int tpacpi_suspend_handler(struct platform_device *pdev,
673 pm_message_t state)
674{
675 struct ibm_struct *ibm, *itmp;
676
677 list_for_each_entry_safe(ibm, itmp,
678 &tpacpi_all_drivers,
679 all_drivers) {
680 if (ibm->suspend)
681 (ibm->suspend)(state);
682 }
683
684 return 0;
685}
686
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300687static int tpacpi_resume_handler(struct platform_device *pdev)
688{
689 struct ibm_struct *ibm, *itmp;
690
691 list_for_each_entry_safe(ibm, itmp,
692 &tpacpi_all_drivers,
693 all_drivers) {
694 if (ibm->resume)
695 (ibm->resume)();
696 }
697
698 return 0;
699}
700
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300701static struct platform_driver tpacpi_pdriver = {
702 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200703 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300704 .owner = THIS_MODULE,
705 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200706 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300707 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300708};
709
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300710static struct platform_driver tpacpi_hwmon_pdriver = {
711 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200712 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300713 .owner = THIS_MODULE,
714 },
715};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300716
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300717/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300718 * sysfs support helpers
719 */
720
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200721struct attribute_set {
722 unsigned int members, max_members;
723 struct attribute_group group;
724};
725
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300726struct attribute_set_obj {
727 struct attribute_set s;
728 struct attribute *a;
729} __attribute__((packed));
730
731static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200732 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300733{
734 struct attribute_set_obj *sobj;
735
736 if (max_members == 0)
737 return NULL;
738
739 /* Allocates space for implicit NULL at the end too */
740 sobj = kzalloc(sizeof(struct attribute_set_obj) +
741 max_members * sizeof(struct attribute *),
742 GFP_KERNEL);
743 if (!sobj)
744 return NULL;
745 sobj->s.max_members = max_members;
746 sobj->s.group.attrs = &sobj->a;
747 sobj->s.group.name = name;
748
749 return &sobj->s;
750}
751
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200752#define destroy_attr_set(_set) \
753 kfree(_set);
754
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300755/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200756static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300757{
758 if (!s || !attr)
759 return -EINVAL;
760
761 if (s->members >= s->max_members)
762 return -ENOMEM;
763
764 s->group.attrs[s->members] = attr;
765 s->members++;
766
767 return 0;
768}
769
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200770static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300771 struct attribute **attr,
772 unsigned int count)
773{
774 int i, res;
775
776 for (i = 0; i < count; i++) {
777 res = add_to_attr_set(s, attr[i]);
778 if (res)
779 return res;
780 }
781
782 return 0;
783}
784
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200785static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300786{
787 sysfs_remove_group(kobj, &s->group);
788 destroy_attr_set(s);
789}
790
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200791#define register_attr_set_with_sysfs(_attr_set, _kobj) \
792 sysfs_create_group(_kobj, &_attr_set->group)
793
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300794static int parse_strtoul(const char *buf,
795 unsigned long max, unsigned long *value)
796{
797 char *endp;
798
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300799 while (*buf && isspace(*buf))
800 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300801 *value = simple_strtoul(buf, &endp, 0);
802 while (*endp && isspace(*endp))
803 endp++;
804 if (*endp || *value > max)
805 return -EINVAL;
806
807 return 0;
808}
809
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200810/*************************************************************************
811 * thinkpad-acpi driver attributes
812 */
813
814/* interface_version --------------------------------------------------- */
815static ssize_t tpacpi_driver_interface_version_show(
816 struct device_driver *drv,
817 char *buf)
818{
819 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
820}
821
822static DRIVER_ATTR(interface_version, S_IRUGO,
823 tpacpi_driver_interface_version_show, NULL);
824
825/* debug_level --------------------------------------------------------- */
826static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
827 char *buf)
828{
829 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
830}
831
832static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
833 const char *buf, size_t count)
834{
835 unsigned long t;
836
837 if (parse_strtoul(buf, 0xffff, &t))
838 return -EINVAL;
839
840 dbg_level = t;
841
842 return count;
843}
844
845static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
846 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
847
848/* version ------------------------------------------------------------- */
849static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
850 char *buf)
851{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200852 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
853 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200854}
855
856static DRIVER_ATTR(version, S_IRUGO,
857 tpacpi_driver_version_show, NULL);
858
859/* --------------------------------------------------------------------- */
860
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200861static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200862 &driver_attr_debug_level, &driver_attr_version,
863 &driver_attr_interface_version,
864};
865
866static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
867{
868 int i, res;
869
870 i = 0;
871 res = 0;
872 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
873 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
874 i++;
875 }
876
877 return res;
878}
879
880static void tpacpi_remove_driver_attributes(struct device_driver *drv)
881{
882 int i;
883
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200884 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200885 driver_remove_file(drv, tpacpi_driver_attributes[i]);
886}
887
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300888/****************************************************************************
889 ****************************************************************************
890 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300891 * Subdrivers
892 *
893 ****************************************************************************
894 ****************************************************************************/
895
896/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300897 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300898 */
899
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300900static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200902 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
903 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200905 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300906 (thinkpad_id.bios_version_str) ?
907 thinkpad_id.bios_version_str : "unknown",
908 (thinkpad_id.ec_version_str) ?
909 thinkpad_id.ec_version_str : "unknown");
910
911 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200912 printk(TPACPI_INFO "%s %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300913 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
914 "IBM" : ((thinkpad_id.vendor ==
915 PCI_VENDOR_ID_LENOVO) ?
916 "Lenovo" : "Unknown vendor"),
917 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 0;
920}
921
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300922static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 int len = 0;
925
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200926 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
927 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 return len;
930}
931
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300932static struct ibm_struct thinkpad_acpi_driver_data = {
933 .name = "driver",
934 .read = thinkpad_acpi_driver_read,
935};
936
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300937/*************************************************************************
938 * Hotkey subdriver
939 */
940
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200941enum { /* hot key scan codes (derived from ACPI DSDT) */
942 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
943 TP_ACPI_HOTKEYSCAN_FNF2,
944 TP_ACPI_HOTKEYSCAN_FNF3,
945 TP_ACPI_HOTKEYSCAN_FNF4,
946 TP_ACPI_HOTKEYSCAN_FNF5,
947 TP_ACPI_HOTKEYSCAN_FNF6,
948 TP_ACPI_HOTKEYSCAN_FNF7,
949 TP_ACPI_HOTKEYSCAN_FNF8,
950 TP_ACPI_HOTKEYSCAN_FNF9,
951 TP_ACPI_HOTKEYSCAN_FNF10,
952 TP_ACPI_HOTKEYSCAN_FNF11,
953 TP_ACPI_HOTKEYSCAN_FNF12,
954 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
955 TP_ACPI_HOTKEYSCAN_FNINSERT,
956 TP_ACPI_HOTKEYSCAN_FNDELETE,
957 TP_ACPI_HOTKEYSCAN_FNHOME,
958 TP_ACPI_HOTKEYSCAN_FNEND,
959 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
960 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
961 TP_ACPI_HOTKEYSCAN_FNSPACE,
962 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
963 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
964 TP_ACPI_HOTKEYSCAN_MUTE,
965 TP_ACPI_HOTKEYSCAN_THINKPAD,
966};
967
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200968enum { /* Keys available through NVRAM polling */
969 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
970 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
971};
972
973enum { /* Positions of some of the keys in hotkey masks */
974 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
975 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
976 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
977 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
978 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
979 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
980 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
981 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
982 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
983 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
984 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
985};
986
987enum { /* NVRAM to ACPI HKEY group map */
988 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
989 TP_ACPI_HKEY_ZOOM_MASK |
990 TP_ACPI_HKEY_DISPSWTCH_MASK |
991 TP_ACPI_HKEY_HIBERNATE_MASK,
992 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
993 TP_ACPI_HKEY_BRGHTDWN_MASK,
994 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
995 TP_ACPI_HKEY_VOLDWN_MASK |
996 TP_ACPI_HKEY_MUTE_MASK,
997};
998
999#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1000struct tp_nvram_state {
1001 u16 thinkpad_toggle:1;
1002 u16 zoom_toggle:1;
1003 u16 display_toggle:1;
1004 u16 thinklight_toggle:1;
1005 u16 hibernate_toggle:1;
1006 u16 displayexp_toggle:1;
1007 u16 display_state:1;
1008 u16 brightness_toggle:1;
1009 u16 volume_toggle:1;
1010 u16 mute:1;
1011
1012 u8 brightness_level;
1013 u8 volume_level;
1014};
1015
1016static struct task_struct *tpacpi_hotkey_task;
1017static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1018static int hotkey_poll_freq = 10; /* Hz */
1019static struct mutex hotkey_thread_mutex;
1020static struct mutex hotkey_thread_data_mutex;
1021static unsigned int hotkey_config_change;
1022
1023#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1024
1025#define hotkey_source_mask 0U
1026
1027#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1028
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001029static struct mutex hotkey_mutex;
1030
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001031static enum { /* Reasons for waking up */
1032 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1033 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1034 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1035} hotkey_wakeup_reason;
1036
1037static int hotkey_autosleep_ack;
1038
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001039static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001040static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001041static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001042static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001043static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001044
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001045static unsigned int hotkey_report_mode;
1046
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001047static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001048
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001049static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001050
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001051#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1052#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001053 do { \
1054 mutex_lock(&hotkey_thread_data_mutex); \
1055 hotkey_config_change++; \
1056 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001057#define HOTKEY_CONFIG_CRITICAL_END \
1058 mutex_unlock(&hotkey_thread_data_mutex);
1059#else
1060#define HOTKEY_CONFIG_CRITICAL_START
1061#define HOTKEY_CONFIG_CRITICAL_END
1062#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1063
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001064/* HKEY.MHKG() return bits */
1065#define TP_HOTKEY_TABLET_MASK (1 << 3)
1066
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001067static int hotkey_get_wlsw(int *status)
1068{
1069 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1070 return -EIO;
1071 return 0;
1072}
1073
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001074static int hotkey_get_tablet_mode(int *status)
1075{
1076 int s;
1077
1078 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1079 return -EIO;
1080
1081 return ((s & TP_HOTKEY_TABLET_MASK) != 0);
1082}
1083
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001084/*
1085 * Call with hotkey_mutex held
1086 */
1087static int hotkey_mask_get(void)
1088{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001089 u32 m = 0;
1090
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001091 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001092 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001093 return -EIO;
1094 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001095 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001096
1097 return 0;
1098}
1099
1100/*
1101 * Call with hotkey_mutex held
1102 */
1103static int hotkey_mask_set(u32 mask)
1104{
1105 int i;
1106 int rc = 0;
1107
1108 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001109 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001110 for (i = 0; i < 32; i++) {
1111 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001112 /* enable in firmware mask only keys not in NVRAM
1113 * mode, but enable the key in the cached hotkey_mask
1114 * regardless of mode, or the key will end up
1115 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001116 if (!acpi_evalf(hkey_handle,
1117 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001118 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001119 rc = -EIO;
1120 break;
1121 } else {
1122 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1123 }
1124 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001125 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001126
1127 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001128 if (!hotkey_mask_get() && !rc &&
1129 (hotkey_mask & ~hotkey_source_mask) !=
1130 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001131 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001132 "requested hot key mask 0x%08x, but "
1133 "firmware forced it to 0x%08x\n",
1134 mask, hotkey_mask);
1135 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001136 } else {
1137#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1138 HOTKEY_CONFIG_CRITICAL_START
1139 hotkey_mask = mask & hotkey_source_mask;
1140 HOTKEY_CONFIG_CRITICAL_END
1141 hotkey_mask_get();
1142 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001143 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001144 "requested hot key mask 0x%08x, "
1145 "forced to 0x%08x (NVRAM poll mask is "
1146 "0x%08x): no firmware mask support\n",
1147 mask, hotkey_mask, hotkey_source_mask);
1148 }
1149#else
1150 hotkey_mask_get();
1151 rc = -ENXIO;
1152#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001153 }
1154
1155 return rc;
1156}
1157
1158static int hotkey_status_get(int *status)
1159{
1160 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1161 return -EIO;
1162
1163 return 0;
1164}
1165
1166static int hotkey_status_set(int status)
1167{
1168 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1169 return -EIO;
1170
1171 return 0;
1172}
1173
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001174static void tpacpi_input_send_radiosw(void)
1175{
1176 int wlsw;
1177
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001178 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001179 mutex_lock(&tpacpi_inputdev_send_mutex);
1180
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001181 input_report_switch(tpacpi_inputdev,
1182 SW_RADIO, !!wlsw);
1183 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001184
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001185 mutex_unlock(&tpacpi_inputdev_send_mutex);
1186 }
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001187}
1188
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001189static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001190{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001191 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001192
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001193 if (tp_features.hotkey_tablet &&
1194 !hotkey_get_tablet_mode(&state)) {
1195 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001196
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001197 input_report_switch(tpacpi_inputdev,
1198 SW_TABLET_MODE, !!state);
1199 input_sync(tpacpi_inputdev);
1200
1201 mutex_unlock(&tpacpi_inputdev_send_mutex);
1202 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001203}
1204
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001205static void tpacpi_input_send_key(unsigned int scancode)
1206{
1207 unsigned int keycode;
1208
1209 keycode = hotkey_keycode_map[scancode];
1210
1211 if (keycode != KEY_RESERVED) {
1212 mutex_lock(&tpacpi_inputdev_send_mutex);
1213
1214 input_report_key(tpacpi_inputdev, keycode, 1);
1215 if (keycode == KEY_UNKNOWN)
1216 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1217 scancode);
1218 input_sync(tpacpi_inputdev);
1219
1220 input_report_key(tpacpi_inputdev, keycode, 0);
1221 if (keycode == KEY_UNKNOWN)
1222 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1223 scancode);
1224 input_sync(tpacpi_inputdev);
1225
1226 mutex_unlock(&tpacpi_inputdev_send_mutex);
1227 }
1228}
1229
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001230#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1231static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1232
1233static void tpacpi_hotkey_send_key(unsigned int scancode)
1234{
1235 tpacpi_input_send_key(scancode);
1236 if (hotkey_report_mode < 2) {
1237 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1238 0x80, 0x1001 + scancode);
1239 }
1240}
1241
1242static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1243{
1244 u8 d;
1245
1246 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1247 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1248 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1249 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1250 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1251 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1252 }
1253 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1254 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1255 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1256 }
1257 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1258 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1259 n->displayexp_toggle =
1260 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1261 }
1262 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1263 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1264 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1265 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1266 n->brightness_toggle =
1267 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1268 }
1269 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1270 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1271 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1272 >> TP_NVRAM_POS_LEVEL_VOLUME;
1273 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1274 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1275 }
1276}
1277
1278#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001279 do { \
1280 if ((mask & (1 << __scancode)) && \
1281 oldn->__member != newn->__member) \
1282 tpacpi_hotkey_send_key(__scancode); \
1283 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001284
1285#define TPACPI_MAY_SEND_KEY(__scancode) \
1286 do { if (mask & (1 << __scancode)) \
1287 tpacpi_hotkey_send_key(__scancode); } while (0)
1288
1289static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001290 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001291 u32 mask)
1292{
1293 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1294 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1295 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1296 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1297
1298 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1299
1300 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1301
1302 /* handle volume */
1303 if (oldn->volume_toggle != newn->volume_toggle) {
1304 if (oldn->mute != newn->mute) {
1305 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1306 }
1307 if (oldn->volume_level > newn->volume_level) {
1308 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1309 } else if (oldn->volume_level < newn->volume_level) {
1310 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1311 } else if (oldn->mute == newn->mute) {
1312 /* repeated key presses that didn't change state */
1313 if (newn->mute) {
1314 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1315 } else if (newn->volume_level != 0) {
1316 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1317 } else {
1318 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1319 }
1320 }
1321 }
1322
1323 /* handle brightness */
1324 if (oldn->brightness_toggle != newn->brightness_toggle) {
1325 if (oldn->brightness_level < newn->brightness_level) {
1326 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1327 } else if (oldn->brightness_level > newn->brightness_level) {
1328 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1329 } else {
1330 /* repeated key presses that didn't change state */
1331 if (newn->brightness_level != 0) {
1332 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1333 } else {
1334 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1335 }
1336 }
1337 }
1338}
1339
1340#undef TPACPI_COMPARE_KEY
1341#undef TPACPI_MAY_SEND_KEY
1342
1343static int hotkey_kthread(void *data)
1344{
1345 struct tp_nvram_state s[2];
1346 u32 mask;
1347 unsigned int si, so;
1348 unsigned long t;
1349 unsigned int change_detector, must_reset;
1350
1351 mutex_lock(&hotkey_thread_mutex);
1352
1353 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1354 goto exit;
1355
1356 set_freezable();
1357
1358 so = 0;
1359 si = 1;
1360 t = 0;
1361
1362 /* Initial state for compares */
1363 mutex_lock(&hotkey_thread_data_mutex);
1364 change_detector = hotkey_config_change;
1365 mask = hotkey_source_mask & hotkey_mask;
1366 mutex_unlock(&hotkey_thread_data_mutex);
1367 hotkey_read_nvram(&s[so], mask);
1368
1369 while (!kthread_should_stop() && hotkey_poll_freq) {
1370 if (t == 0)
1371 t = 1000/hotkey_poll_freq;
1372 t = msleep_interruptible(t);
1373 if (unlikely(kthread_should_stop()))
1374 break;
1375 must_reset = try_to_freeze();
1376 if (t > 0 && !must_reset)
1377 continue;
1378
1379 mutex_lock(&hotkey_thread_data_mutex);
1380 if (must_reset || hotkey_config_change != change_detector) {
1381 /* forget old state on thaw or config change */
1382 si = so;
1383 t = 0;
1384 change_detector = hotkey_config_change;
1385 }
1386 mask = hotkey_source_mask & hotkey_mask;
1387 mutex_unlock(&hotkey_thread_data_mutex);
1388
1389 if (likely(mask)) {
1390 hotkey_read_nvram(&s[si], mask);
1391 if (likely(si != so)) {
1392 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001393 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001394 }
1395 }
1396
1397 so = si;
1398 si ^= 1;
1399 }
1400
1401exit:
1402 mutex_unlock(&hotkey_thread_mutex);
1403 return 0;
1404}
1405
1406static void hotkey_poll_stop_sync(void)
1407{
1408 if (tpacpi_hotkey_task) {
1409 if (frozen(tpacpi_hotkey_task) ||
1410 freezing(tpacpi_hotkey_task))
1411 thaw_process(tpacpi_hotkey_task);
1412
1413 kthread_stop(tpacpi_hotkey_task);
1414 tpacpi_hotkey_task = NULL;
1415 mutex_lock(&hotkey_thread_mutex);
1416 /* at this point, the thread did exit */
1417 mutex_unlock(&hotkey_thread_mutex);
1418 }
1419}
1420
1421/* call with hotkey_mutex held */
1422static void hotkey_poll_setup(int may_warn)
1423{
1424 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1425 hotkey_poll_freq > 0 &&
1426 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1427 if (!tpacpi_hotkey_task) {
1428 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001429 NULL,
1430 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001431 if (IS_ERR(tpacpi_hotkey_task)) {
1432 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001433 printk(TPACPI_ERR
1434 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001435 "for hotkey polling\n");
1436 }
1437 }
1438 } else {
1439 hotkey_poll_stop_sync();
1440 if (may_warn &&
1441 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001442 printk(TPACPI_NOTICE
1443 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001444 "which is currently disabled\n",
1445 hotkey_source_mask);
1446 }
1447 }
1448}
1449
1450static void hotkey_poll_setup_safe(int may_warn)
1451{
1452 mutex_lock(&hotkey_mutex);
1453 hotkey_poll_setup(may_warn);
1454 mutex_unlock(&hotkey_mutex);
1455}
1456
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001457#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1458
1459static void hotkey_poll_setup_safe(int __unused)
1460{
1461}
1462
1463#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1464
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001465static int hotkey_inputdev_open(struct input_dev *dev)
1466{
1467 switch (tpacpi_lifecycle) {
1468 case TPACPI_LIFE_INIT:
1469 /*
1470 * hotkey_init will call hotkey_poll_setup_safe
1471 * at the appropriate moment
1472 */
1473 return 0;
1474 case TPACPI_LIFE_EXITING:
1475 return -EBUSY;
1476 case TPACPI_LIFE_RUNNING:
1477 hotkey_poll_setup_safe(0);
1478 return 0;
1479 }
1480
1481 /* Should only happen if tpacpi_lifecycle is corrupt */
1482 BUG();
1483 return -EBUSY;
1484}
1485
1486static void hotkey_inputdev_close(struct input_dev *dev)
1487{
1488 /* disable hotkey polling when possible */
1489 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1490 hotkey_poll_setup_safe(0);
1491}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001492
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001493/* sysfs hotkey enable ------------------------------------------------- */
1494static ssize_t hotkey_enable_show(struct device *dev,
1495 struct device_attribute *attr,
1496 char *buf)
1497{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001498 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001499
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001500 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001501 if (res)
1502 return res;
1503
1504 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1505}
1506
1507static ssize_t hotkey_enable_store(struct device *dev,
1508 struct device_attribute *attr,
1509 const char *buf, size_t count)
1510{
1511 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001512 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001513
1514 if (parse_strtoul(buf, 1, &t))
1515 return -EINVAL;
1516
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001517 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001518
1519 return (res) ? res : count;
1520}
1521
1522static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001523 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001524 hotkey_enable_show, hotkey_enable_store);
1525
1526/* sysfs hotkey mask --------------------------------------------------- */
1527static ssize_t hotkey_mask_show(struct device *dev,
1528 struct device_attribute *attr,
1529 char *buf)
1530{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001531 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001532
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001533 if (mutex_lock_interruptible(&hotkey_mutex))
1534 return -ERESTARTSYS;
1535 res = hotkey_mask_get();
1536 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001537
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001538 return (res)?
1539 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001540}
1541
1542static ssize_t hotkey_mask_store(struct device *dev,
1543 struct device_attribute *attr,
1544 const char *buf, size_t count)
1545{
1546 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001547 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001548
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001549 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001550 return -EINVAL;
1551
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001552 if (mutex_lock_interruptible(&hotkey_mutex))
1553 return -ERESTARTSYS;
1554
1555 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001556
1557#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1558 hotkey_poll_setup(1);
1559#endif
1560
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001561 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001562
1563 return (res) ? res : count;
1564}
1565
1566static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001567 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001568 hotkey_mask_show, hotkey_mask_store);
1569
1570/* sysfs hotkey bios_enabled ------------------------------------------- */
1571static ssize_t hotkey_bios_enabled_show(struct device *dev,
1572 struct device_attribute *attr,
1573 char *buf)
1574{
1575 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1576}
1577
1578static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001579 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001580
1581/* sysfs hotkey bios_mask ---------------------------------------------- */
1582static ssize_t hotkey_bios_mask_show(struct device *dev,
1583 struct device_attribute *attr,
1584 char *buf)
1585{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001586 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001587}
1588
1589static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001590 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001591
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001592/* sysfs hotkey all_mask ----------------------------------------------- */
1593static ssize_t hotkey_all_mask_show(struct device *dev,
1594 struct device_attribute *attr,
1595 char *buf)
1596{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001597 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1598 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001599}
1600
1601static struct device_attribute dev_attr_hotkey_all_mask =
1602 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1603
1604/* sysfs hotkey recommended_mask --------------------------------------- */
1605static ssize_t hotkey_recommended_mask_show(struct device *dev,
1606 struct device_attribute *attr,
1607 char *buf)
1608{
1609 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001610 (hotkey_all_mask | hotkey_source_mask)
1611 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001612}
1613
1614static struct device_attribute dev_attr_hotkey_recommended_mask =
1615 __ATTR(hotkey_recommended_mask, S_IRUGO,
1616 hotkey_recommended_mask_show, NULL);
1617
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001618#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1619
1620/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1621static ssize_t hotkey_source_mask_show(struct device *dev,
1622 struct device_attribute *attr,
1623 char *buf)
1624{
1625 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1626}
1627
1628static ssize_t hotkey_source_mask_store(struct device *dev,
1629 struct device_attribute *attr,
1630 const char *buf, size_t count)
1631{
1632 unsigned long t;
1633
1634 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1635 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1636 return -EINVAL;
1637
1638 if (mutex_lock_interruptible(&hotkey_mutex))
1639 return -ERESTARTSYS;
1640
1641 HOTKEY_CONFIG_CRITICAL_START
1642 hotkey_source_mask = t;
1643 HOTKEY_CONFIG_CRITICAL_END
1644
1645 hotkey_poll_setup(1);
1646
1647 mutex_unlock(&hotkey_mutex);
1648
1649 return count;
1650}
1651
1652static struct device_attribute dev_attr_hotkey_source_mask =
1653 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1654 hotkey_source_mask_show, hotkey_source_mask_store);
1655
1656/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1657static ssize_t hotkey_poll_freq_show(struct device *dev,
1658 struct device_attribute *attr,
1659 char *buf)
1660{
1661 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1662}
1663
1664static ssize_t hotkey_poll_freq_store(struct device *dev,
1665 struct device_attribute *attr,
1666 const char *buf, size_t count)
1667{
1668 unsigned long t;
1669
1670 if (parse_strtoul(buf, 25, &t))
1671 return -EINVAL;
1672
1673 if (mutex_lock_interruptible(&hotkey_mutex))
1674 return -ERESTARTSYS;
1675
1676 hotkey_poll_freq = t;
1677
1678 hotkey_poll_setup(1);
1679 mutex_unlock(&hotkey_mutex);
1680
1681 return count;
1682}
1683
1684static struct device_attribute dev_attr_hotkey_poll_freq =
1685 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1686 hotkey_poll_freq_show, hotkey_poll_freq_store);
1687
1688#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1689
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001690/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001691static ssize_t hotkey_radio_sw_show(struct device *dev,
1692 struct device_attribute *attr,
1693 char *buf)
1694{
1695 int res, s;
1696 res = hotkey_get_wlsw(&s);
1697 if (res < 0)
1698 return res;
1699
1700 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1701}
1702
1703static struct device_attribute dev_attr_hotkey_radio_sw =
1704 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1705
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001706static void hotkey_radio_sw_notify_change(void)
1707{
1708 if (tp_features.hotkey_wlsw)
1709 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1710 "hotkey_radio_sw");
1711}
1712
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001713/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1714static ssize_t hotkey_tablet_mode_show(struct device *dev,
1715 struct device_attribute *attr,
1716 char *buf)
1717{
1718 int res, s;
1719 res = hotkey_get_tablet_mode(&s);
1720 if (res < 0)
1721 return res;
1722
1723 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1724}
1725
1726static struct device_attribute dev_attr_hotkey_tablet_mode =
1727 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1728
1729static void hotkey_tablet_mode_notify_change(void)
1730{
1731 if (tp_features.hotkey_tablet)
1732 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1733 "hotkey_tablet_mode");
1734}
1735
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001736/* sysfs hotkey report_mode -------------------------------------------- */
1737static ssize_t hotkey_report_mode_show(struct device *dev,
1738 struct device_attribute *attr,
1739 char *buf)
1740{
1741 return snprintf(buf, PAGE_SIZE, "%d\n",
1742 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1743}
1744
1745static struct device_attribute dev_attr_hotkey_report_mode =
1746 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1747
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001748/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001749static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1750 struct device_attribute *attr,
1751 char *buf)
1752{
1753 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1754}
1755
1756static struct device_attribute dev_attr_hotkey_wakeup_reason =
1757 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1758
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001759static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001760{
1761 if (tp_features.hotkey_mask)
1762 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1763 "wakeup_reason");
1764}
1765
1766/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001767static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1768 struct device_attribute *attr,
1769 char *buf)
1770{
1771 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1772}
1773
1774static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1775 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1776 hotkey_wakeup_hotunplug_complete_show, NULL);
1777
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001778static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001779{
1780 if (tp_features.hotkey_mask)
1781 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1782 "wakeup_hotunplug_complete");
1783}
1784
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001785/* --------------------------------------------------------------------- */
1786
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001787static struct attribute *hotkey_attributes[] __initdata = {
1788 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001789 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001790 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001791#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1792 &dev_attr_hotkey_mask.attr,
1793 &dev_attr_hotkey_all_mask.attr,
1794 &dev_attr_hotkey_recommended_mask.attr,
1795 &dev_attr_hotkey_source_mask.attr,
1796 &dev_attr_hotkey_poll_freq.attr,
1797#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001798};
1799
1800static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001801 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001802#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1803 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001804 &dev_attr_hotkey_all_mask.attr,
1805 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001806#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001807 &dev_attr_hotkey_wakeup_reason.attr,
1808 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001809};
1810
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001811static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001812{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001813 /* Requirements for changing the default keymaps:
1814 *
1815 * 1. Many of the keys are mapped to KEY_RESERVED for very
1816 * good reasons. Do not change them unless you have deep
1817 * knowledge on the IBM and Lenovo ThinkPad firmware for
1818 * the various ThinkPad models. The driver behaves
1819 * differently for KEY_RESERVED: such keys have their
1820 * hot key mask *unset* in mask_recommended, and also
1821 * in the initial hot key mask programmed into the
1822 * firmware at driver load time, which means the firm-
1823 * ware may react very differently if you change them to
1824 * something else;
1825 *
1826 * 2. You must be subscribed to the linux-thinkpad and
1827 * ibm-acpi-devel mailing lists, and you should read the
1828 * list archives since 2007 if you want to change the
1829 * keymaps. This requirement exists so that you will
1830 * know the past history of problems with the thinkpad-
1831 * acpi driver keymaps, and also that you will be
1832 * listening to any bug reports;
1833 *
1834 * 3. Do not send thinkpad-acpi specific patches directly to
1835 * for merging, *ever*. Send them to the linux-acpi
1836 * mailinglist for comments. Merging is to be done only
1837 * through acpi-test and the ACPI maintainer.
1838 *
1839 * If the above is too much to ask, don't change the keymap.
1840 * Ask the thinkpad-acpi maintainer to do it, instead.
1841 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001842 static u16 ibm_keycode_map[] __initdata = {
1843 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1844 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1845 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1846 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001847
1848 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001849 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1850 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1851 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001852
1853 /* brightness: firmware always reacts to them, unless
1854 * X.org did some tricks in the radeon BIOS scratch
1855 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001856 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001857 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001858
1859 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001860 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001861
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001862 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1863 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001864
1865 /* Volume: firmware always react to it and reprograms
1866 * the built-in *extra* mixer. Never map it to control
1867 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001868 KEY_RESERVED, /* 0x14: VOLUME UP */
1869 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1870 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001871
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001872 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001873
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001874 /* (assignments unknown, please report if found) */
1875 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1876 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1877 };
1878 static u16 lenovo_keycode_map[] __initdata = {
1879 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1880 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1881 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1882 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001883
1884 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001885 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1886 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1887 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001888
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001889 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001890 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001891
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001892 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001893
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001894 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1895 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001896
1897 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1898 * react to it and reprograms the built-in *extra* mixer.
1899 * Never map it to control another mixer by default.
1900 *
1901 * T60?, T61, R60?, R61: firmware and EC tries to send
1902 * these over the regular keyboard, so these are no-ops,
1903 * but there are still weird bugs re. MUTE, so do not
1904 * change unless you get test reports from all Lenovo
1905 * models. May cause the BIOS to interfere with the
1906 * HDA mixer.
1907 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001908 KEY_RESERVED, /* 0x14: VOLUME UP */
1909 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1910 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001911
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001912 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001913
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001914 /* (assignments unknown, please report if found) */
1915 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1916 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1917 };
1918
1919#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1920#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1921#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1922
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001923 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001924 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001925 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001926
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001927 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1928
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001929 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001930 BUG_ON(tpacpi_inputdev->open != NULL ||
1931 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001932
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001933 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001934 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001935
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001936#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1937 mutex_init(&hotkey_thread_mutex);
1938 mutex_init(&hotkey_thread_data_mutex);
1939#endif
1940
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001941 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001942 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001943
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001944 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001945 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001946
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001947 if (tp_features.hotkey) {
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001948 hotkey_dev_attributes = create_attr_set(13, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001949 if (!hotkey_dev_attributes)
1950 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001951 res = add_many_to_attr_set(hotkey_dev_attributes,
1952 hotkey_attributes,
1953 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001954 if (res)
1955 return res;
1956
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001957 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001958 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1959 for HKEY interface version 0x100 */
1960 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1961 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001962 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001963 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001964 printk(TPACPI_ERR "please report this to %s\n",
1965 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001966 } else {
1967 /*
1968 * MHKV 0x100 in A31, R40, R40e,
1969 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001970 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001971 tp_features.hotkey_mask = 1;
1972 }
1973 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001974
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001975 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001976 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001977
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001978 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001979 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001980 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001981 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001982 "missing MHKA handler, "
1983 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001984 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001985 /* FN+F12, FN+F4, FN+F3 */
1986 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001987 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001988 }
1989
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001990 /* hotkey_source_mask *must* be zero for
1991 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001992 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001993 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001994 res = hotkey_mask_get();
1995 hotkey_orig_mask = hotkey_mask;
1996 if (!res) {
1997 res = add_many_to_attr_set(
1998 hotkey_dev_attributes,
1999 hotkey_mask_attributes,
2000 ARRAY_SIZE(hotkey_mask_attributes));
2001 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002002 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002003
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002004#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2005 if (tp_features.hotkey_mask) {
2006 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2007 & ~hotkey_all_mask;
2008 } else {
2009 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2010 }
2011
2012 vdbg_printk(TPACPI_DBG_INIT,
2013 "hotkey source mask 0x%08x, polling freq %d\n",
2014 hotkey_source_mask, hotkey_poll_freq);
2015#endif
2016
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002017 /* Not all thinkpads have a hardware radio switch */
2018 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2019 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002020 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002021 "radio switch found; radios are %s\n",
2022 enabled(status, 0));
2023 res = add_to_attr_set(hotkey_dev_attributes,
2024 &dev_attr_hotkey_radio_sw.attr);
2025 }
2026
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002027 /* For X41t, X60t, X61t Tablets... */
2028 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2029 tp_features.hotkey_tablet = 1;
2030 printk(TPACPI_INFO
2031 "possible tablet mode switch found; "
2032 "ThinkPad in %s mode\n",
2033 (status & TP_HOTKEY_TABLET_MASK)?
2034 "tablet" : "laptop");
2035 res = add_to_attr_set(hotkey_dev_attributes,
2036 &dev_attr_hotkey_tablet_mode.attr);
2037 }
2038
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002039 if (!res)
2040 res = register_attr_set_with_sysfs(
2041 hotkey_dev_attributes,
2042 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002043 if (res)
2044 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002045
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002046 /* Set up key map */
2047
2048 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2049 GFP_KERNEL);
2050 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002051 printk(TPACPI_ERR
2052 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002053 return -ENOMEM;
2054 }
2055
2056 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2057 dbg_printk(TPACPI_DBG_INIT,
2058 "using Lenovo default hot key map\n");
2059 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2060 TPACPI_HOTKEY_MAP_SIZE);
2061 } else {
2062 dbg_printk(TPACPI_DBG_INIT,
2063 "using IBM default hot key map\n");
2064 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2065 TPACPI_HOTKEY_MAP_SIZE);
2066 }
2067
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002068 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2069 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2070 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002071 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2072 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2073 tpacpi_inputdev->keycode = hotkey_keycode_map;
2074 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002075 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2076 set_bit(hotkey_keycode_map[i],
2077 tpacpi_inputdev->keybit);
2078 } else {
2079 if (i < sizeof(hotkey_reserved_mask)*8)
2080 hotkey_reserved_mask |= 1 << i;
2081 }
2082 }
2083
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002084 if (tp_features.hotkey_wlsw) {
2085 set_bit(EV_SW, tpacpi_inputdev->evbit);
2086 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2087 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002088 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002089 set_bit(EV_SW, tpacpi_inputdev->evbit);
2090 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2091 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002092
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002093 dbg_printk(TPACPI_DBG_INIT,
2094 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002095 res = hotkey_status_set(1);
2096 if (res)
2097 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002098 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2099 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002100 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002101 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002102 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002103
2104 dbg_printk(TPACPI_DBG_INIT,
2105 "legacy hot key reporting over procfs %s\n",
2106 (hotkey_report_mode < 2) ?
2107 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002108
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002109 tpacpi_inputdev->open = &hotkey_inputdev_open;
2110 tpacpi_inputdev->close = &hotkey_inputdev_close;
2111
2112 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002113 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002114 tpacpi_input_send_tabletsw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002115 }
2116
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002117 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002118}
2119
2120static void hotkey_exit(void)
2121{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002122#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2123 hotkey_poll_stop_sync();
2124#endif
2125
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002126 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002127 dbg_printk(TPACPI_DBG_EXIT,
2128 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002129 /* no short-circuit boolean operator below! */
2130 if ((hotkey_mask_set(hotkey_orig_mask) |
2131 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002132 printk(TPACPI_ERR
2133 "failed to restore hot key mask "
2134 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002135 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002136
2137 if (hotkey_dev_attributes) {
2138 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2139 hotkey_dev_attributes = NULL;
2140 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002141}
2142
2143static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2144{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002145 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002146 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002147 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002148 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002149 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002150
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002151 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002152 printk(TPACPI_ERR
2153 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002154 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002155 acpi_bus_generate_netlink_event(
2156 ibm->acpi->device->pnp.device_class,
2157 ibm->acpi->device->dev.bus_id,
2158 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002159 return;
2160 }
2161
2162 while (1) {
2163 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002164 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002165 return;
2166 }
2167
2168 if (hkey == 0) {
2169 /* queue empty */
2170 return;
2171 }
2172
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002173 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002174 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002175 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002176
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002177 switch (hkey >> 12) {
2178 case 1:
2179 /* 0x1000-0x1FFF: key presses */
2180 scancode = hkey & 0xfff;
2181 if (scancode > 0 && scancode < 0x21) {
2182 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002183 if (!(hotkey_source_mask & (1 << scancode))) {
2184 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002185 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002186 } else {
2187 ignore_acpi_ev = 1;
2188 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002189 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002190 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002191 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002192 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002193 case 2:
2194 /* Wakeup reason */
2195 switch (hkey) {
2196 case 0x2304: /* suspend, undock */
2197 case 0x2404: /* hibernation, undock */
2198 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2199 ignore_acpi_ev = 1;
2200 break;
2201 case 0x2305: /* suspend, bay eject */
2202 case 0x2405: /* hibernation, bay eject */
2203 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2204 ignore_acpi_ev = 1;
2205 break;
2206 default:
2207 unk_ev = 1;
2208 }
2209 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2210 printk(TPACPI_INFO
2211 "woke up due to a hot-unplug "
2212 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002213 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002214 }
2215 break;
2216 case 3:
2217 /* bay-related wakeups */
2218 if (hkey == 0x3003) {
2219 hotkey_autosleep_ack = 1;
2220 printk(TPACPI_INFO
2221 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002222 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002223 } else {
2224 unk_ev = 1;
2225 }
2226 break;
2227 case 4:
2228 /* dock-related wakeups */
2229 if (hkey == 0x4003) {
2230 hotkey_autosleep_ack = 1;
2231 printk(TPACPI_INFO
2232 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002233 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002234 } else {
2235 unk_ev = 1;
2236 }
2237 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002238 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002239 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002240 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002241 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002242 case 0x500b: /* X61t: tablet pen inserted into bay */
2243 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002244 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002245 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2246 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2247 tpacpi_input_send_tabletsw();
2248 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002249 send_acpi_ev = 0;
2250 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002251 case 0x5001:
2252 case 0x5002:
2253 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002254 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002255 break;
2256 default:
2257 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002258 }
2259 break;
2260 case 7:
2261 /* 0x7000-0x7FFF: misc */
2262 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2263 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002264 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002265 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002266 break;
2267 }
2268 /* fallthrough to default */
2269 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002270 unk_ev = 1;
2271 }
2272 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002273 printk(TPACPI_NOTICE
2274 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002275 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002276
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002277 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002278 if (!ignore_acpi_ev &&
2279 (send_acpi_ev || hotkey_report_mode < 2)) {
2280 acpi_bus_generate_proc_event(ibm->acpi->device,
2281 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002282 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002283
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002284 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002285 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002286 acpi_bus_generate_netlink_event(
2287 ibm->acpi->device->pnp.device_class,
2288 ibm->acpi->device->dev.bus_id,
2289 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002290 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002291 }
2292}
2293
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002294static void hotkey_suspend(pm_message_t state)
2295{
2296 /* Do these on suspend, we get the events on early resume! */
2297 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2298 hotkey_autosleep_ack = 0;
2299}
2300
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002301static void hotkey_resume(void)
2302{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002303 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002304 printk(TPACPI_ERR
2305 "error while trying to read hot key mask "
2306 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002307 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002308 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002309 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002310 hotkey_wakeup_reason_notify_change();
2311 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002312 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002313}
2314
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002315/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002316static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002318 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 int len = 0;
2320
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002321 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002322 len += sprintf(p + len, "status:\t\tnot supported\n");
2323 return len;
2324 }
2325
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002326 if (mutex_lock_interruptible(&hotkey_mutex))
2327 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002328 res = hotkey_status_get(&status);
2329 if (!res)
2330 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002331 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002332 if (res)
2333 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002336 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002337 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 len += sprintf(p + len,
2339 "commands:\tenable, disable, reset, <mask>\n");
2340 } else {
2341 len += sprintf(p + len, "mask:\t\tnot supported\n");
2342 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2343 }
2344
2345 return len;
2346}
2347
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002348static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002350 int res, status;
2351 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002354 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return -ENODEV;
2356
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002357 if (mutex_lock_interruptible(&hotkey_mutex))
2358 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002359
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002360 status = -1;
2361 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002362
2363 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 while ((cmd = next_cmd(&buf))) {
2365 if (strlencmp(cmd, "enable") == 0) {
2366 status = 1;
2367 } else if (strlencmp(cmd, "disable") == 0) {
2368 status = 0;
2369 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002370 status = hotkey_orig_status;
2371 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2373 /* mask set */
2374 } else if (sscanf(cmd, "%x", &mask) == 1) {
2375 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002376 } else {
2377 res = -EINVAL;
2378 goto errexit;
2379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002381 if (status != -1)
2382 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002384 if (!res && mask != hotkey_mask)
2385 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002387errexit:
2388 mutex_unlock(&hotkey_mutex);
2389 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002392static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002393 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002394 {"", 0},
2395};
2396
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002397static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002398 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002399 .notify = hotkey_notify,
2400 .handle = &hkey_handle,
2401 .type = ACPI_DEVICE_NOTIFY,
2402};
2403
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002404static struct ibm_struct hotkey_driver_data = {
2405 .name = "hotkey",
2406 .read = hotkey_read,
2407 .write = hotkey_write,
2408 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002409 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002410 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002411 .acpi = &ibm_hotkey_acpidriver,
2412};
2413
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002414/*************************************************************************
2415 * Bluetooth subdriver
2416 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002418enum {
2419 /* ACPI GBDC/SBDC bits */
2420 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2421 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2422 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2423};
2424
2425static int bluetooth_get_radiosw(void);
2426static int bluetooth_set_radiosw(int radio_on);
2427
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002428/* sysfs bluetooth enable ---------------------------------------------- */
2429static ssize_t bluetooth_enable_show(struct device *dev,
2430 struct device_attribute *attr,
2431 char *buf)
2432{
2433 int status;
2434
2435 status = bluetooth_get_radiosw();
2436 if (status < 0)
2437 return status;
2438
2439 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2440}
2441
2442static ssize_t bluetooth_enable_store(struct device *dev,
2443 struct device_attribute *attr,
2444 const char *buf, size_t count)
2445{
2446 unsigned long t;
2447 int res;
2448
2449 if (parse_strtoul(buf, 1, &t))
2450 return -EINVAL;
2451
2452 res = bluetooth_set_radiosw(t);
2453
2454 return (res) ? res : count;
2455}
2456
2457static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002458 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002459 bluetooth_enable_show, bluetooth_enable_store);
2460
2461/* --------------------------------------------------------------------- */
2462
2463static struct attribute *bluetooth_attributes[] = {
2464 &dev_attr_bluetooth_enable.attr,
2465 NULL
2466};
2467
2468static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002469 .attrs = bluetooth_attributes,
2470};
2471
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002472static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002474 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002475 int status = 0;
2476
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002477 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2478
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002479 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002480
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2482 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002483 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002484 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002486 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2487 str_supported(tp_features.bluetooth),
2488 status);
2489
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002490 if (tp_features.bluetooth) {
2491 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2492 /* no bluetooth hardware present in system */
2493 tp_features.bluetooth = 0;
2494 dbg_printk(TPACPI_DBG_INIT,
2495 "bluetooth hardware not installed\n");
2496 } else {
2497 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2498 &bluetooth_attr_group);
2499 if (res)
2500 return res;
2501 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002502 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002503
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002504 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002507static void bluetooth_exit(void)
2508{
2509 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2510 &bluetooth_attr_group);
2511}
2512
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002513static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 int status;
2516
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002517 if (!tp_features.bluetooth)
2518 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002520 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2521 return -EIO;
2522
2523 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2524}
2525
2526static int bluetooth_set_radiosw(int radio_on)
2527{
2528 int status;
2529
2530 if (!tp_features.bluetooth)
2531 return -ENODEV;
2532
2533 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2534 return -EIO;
2535 if (radio_on)
2536 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2537 else
2538 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2539 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2540 return -EIO;
2541
2542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002545/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002546static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002549 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002551 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002554 len += sprintf(p + len, "status:\t\t%s\n",
2555 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 len += sprintf(p + len, "commands:\tenable, disable\n");
2557 }
2558
2559 return len;
2560}
2561
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002562static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002566 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002567 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 while ((cmd = next_cmd(&buf))) {
2570 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002571 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002573 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 } else
2575 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return 0;
2579}
2580
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002581static struct ibm_struct bluetooth_driver_data = {
2582 .name = "bluetooth",
2583 .read = bluetooth_read,
2584 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002585 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002586};
2587
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002588/*************************************************************************
2589 * Wan subdriver
2590 */
2591
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002592enum {
2593 /* ACPI GWAN/SWAN bits */
2594 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2595 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2596 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2597};
2598
2599static int wan_get_radiosw(void);
2600static int wan_set_radiosw(int radio_on);
2601
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002602/* sysfs wan enable ---------------------------------------------------- */
2603static ssize_t wan_enable_show(struct device *dev,
2604 struct device_attribute *attr,
2605 char *buf)
2606{
2607 int status;
2608
2609 status = wan_get_radiosw();
2610 if (status < 0)
2611 return status;
2612
2613 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2614}
2615
2616static ssize_t wan_enable_store(struct device *dev,
2617 struct device_attribute *attr,
2618 const char *buf, size_t count)
2619{
2620 unsigned long t;
2621 int res;
2622
2623 if (parse_strtoul(buf, 1, &t))
2624 return -EINVAL;
2625
2626 res = wan_set_radiosw(t);
2627
2628 return (res) ? res : count;
2629}
2630
2631static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002632 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002633 wan_enable_show, wan_enable_store);
2634
2635/* --------------------------------------------------------------------- */
2636
2637static struct attribute *wan_attributes[] = {
2638 &dev_attr_wan_enable.attr,
2639 NULL
2640};
2641
2642static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002643 .attrs = wan_attributes,
2644};
2645
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002646static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002647{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002648 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002649 int status = 0;
2650
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002651 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2652
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002653 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002654
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002655 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002656 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002657
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002658 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2659 str_supported(tp_features.wan),
2660 status);
2661
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002662 if (tp_features.wan) {
2663 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2664 /* no wan hardware present in system */
2665 tp_features.wan = 0;
2666 dbg_printk(TPACPI_DBG_INIT,
2667 "wan hardware not installed\n");
2668 } else {
2669 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2670 &wan_attr_group);
2671 if (res)
2672 return res;
2673 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002674 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002675
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002676 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002677}
2678
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002679static void wan_exit(void)
2680{
2681 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2682 &wan_attr_group);
2683}
2684
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002685static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002686{
2687 int status;
2688
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002689 if (!tp_features.wan)
2690 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002691
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002692 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2693 return -EIO;
2694
2695 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2696}
2697
2698static int wan_set_radiosw(int radio_on)
2699{
2700 int status;
2701
2702 if (!tp_features.wan)
2703 return -ENODEV;
2704
2705 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2706 return -EIO;
2707 if (radio_on)
2708 status |= TP_ACPI_WANCARD_RADIOSSW;
2709 else
2710 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2711 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2712 return -EIO;
2713
2714 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002715}
2716
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002717/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002718static int wan_read(char *p)
2719{
2720 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002721 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002722
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002723 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002724 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002725 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002726 len += sprintf(p + len, "status:\t\t%s\n",
2727 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002728 len += sprintf(p + len, "commands:\tenable, disable\n");
2729 }
2730
2731 return len;
2732}
2733
2734static int wan_write(char *buf)
2735{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002736 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002737
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002738 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002739 return -ENODEV;
2740
2741 while ((cmd = next_cmd(&buf))) {
2742 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002743 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002744 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002745 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002746 } else
2747 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002748 }
2749
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002750 return 0;
2751}
2752
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002753static struct ibm_struct wan_driver_data = {
2754 .name = "wan",
2755 .read = wan_read,
2756 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002757 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002758 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002759};
2760
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002761/*************************************************************************
2762 * Video subdriver
2763 */
2764
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002765#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2766
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002767enum video_access_mode {
2768 TPACPI_VIDEO_NONE = 0,
2769 TPACPI_VIDEO_570, /* 570 */
2770 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2771 TPACPI_VIDEO_NEW, /* all others */
2772};
2773
2774enum { /* video status flags, based on VIDEO_570 */
2775 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2776 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2777 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2778};
2779
2780enum { /* TPACPI_VIDEO_570 constants */
2781 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2782 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2783 * video_status_flags */
2784 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2785 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2786};
2787
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002788static enum video_access_mode video_supported;
2789static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002790
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002791static int video_autosw_get(void);
2792static int video_autosw_set(int enable);
2793
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002794TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002795
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002796static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002798 int ivga;
2799
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002800 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2801
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002802 TPACPI_ACPIHANDLE_INIT(vid);
2803 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002804
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002805 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2806 /* G41, assume IVGA doesn't change */
2807 vid_handle = vid2_handle;
2808
2809 if (!vid_handle)
2810 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002811 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002812 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2813 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002814 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002815 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2816 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002817 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002818 else
2819 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002820 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002822 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2823 str_supported(video_supported != TPACPI_VIDEO_NONE),
2824 video_supported);
2825
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002826 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002829static void video_exit(void)
2830{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002831 dbg_printk(TPACPI_DBG_EXIT,
2832 "restoring original video autoswitch mode\n");
2833 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002834 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002835 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002836}
2837
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002838static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 int status = 0;
2841 int i;
2842
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002843 switch (video_supported) {
2844 case TPACPI_VIDEO_570:
2845 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2846 TP_ACPI_VIDEO_570_PHSCMD))
2847 return -EIO;
2848 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2849 break;
2850 case TPACPI_VIDEO_770:
2851 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2852 return -EIO;
2853 if (i)
2854 status |= TP_ACPI_VIDEO_S_LCD;
2855 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2856 return -EIO;
2857 if (i)
2858 status |= TP_ACPI_VIDEO_S_CRT;
2859 break;
2860 case TPACPI_VIDEO_NEW:
2861 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2862 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2863 return -EIO;
2864 if (i)
2865 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002867 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2868 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2869 return -EIO;
2870 if (i)
2871 status |= TP_ACPI_VIDEO_S_LCD;
2872 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2873 return -EIO;
2874 if (i)
2875 status |= TP_ACPI_VIDEO_S_DVI;
2876 break;
2877 default:
2878 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 return status;
2882}
2883
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002884static int video_outputsw_set(int status)
2885{
2886 int autosw;
2887 int res = 0;
2888
2889 switch (video_supported) {
2890 case TPACPI_VIDEO_570:
2891 res = acpi_evalf(NULL, NULL,
2892 "\\_SB.PHS2", "vdd",
2893 TP_ACPI_VIDEO_570_PHS2CMD,
2894 status | TP_ACPI_VIDEO_570_PHS2SET);
2895 break;
2896 case TPACPI_VIDEO_770:
2897 autosw = video_autosw_get();
2898 if (autosw < 0)
2899 return autosw;
2900
2901 res = video_autosw_set(1);
2902 if (res)
2903 return res;
2904 res = acpi_evalf(vid_handle, NULL,
2905 "ASWT", "vdd", status * 0x100, 0);
2906 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002907 printk(TPACPI_ERR
2908 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002909 return -EIO;
2910 }
2911 break;
2912 case TPACPI_VIDEO_NEW:
2913 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002914 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002915 break;
2916 default:
2917 return -ENOSYS;
2918 }
2919
2920 return (res)? 0 : -EIO;
2921}
2922
2923static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002925 int autosw = 0;
2926
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002927 switch (video_supported) {
2928 case TPACPI_VIDEO_570:
2929 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2930 return -EIO;
2931 break;
2932 case TPACPI_VIDEO_770:
2933 case TPACPI_VIDEO_NEW:
2934 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2935 return -EIO;
2936 break;
2937 default:
2938 return -ENOSYS;
2939 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002940
2941 return autosw & 1;
2942}
2943
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002944static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002945{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002946 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002947 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002948 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002949}
2950
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002951static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002952{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002953 int autosw = video_autosw_get();
2954 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002955
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002956 if (autosw < 0)
2957 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002958
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002959 switch (video_supported) {
2960 case TPACPI_VIDEO_570:
2961 res = video_autosw_set(1);
2962 if (res)
2963 return res;
2964 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2965 break;
2966 case TPACPI_VIDEO_770:
2967 case TPACPI_VIDEO_NEW:
2968 res = video_autosw_set(1);
2969 if (res)
2970 return res;
2971 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2972 break;
2973 default:
2974 return -ENOSYS;
2975 }
2976 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002977 printk(TPACPI_ERR
2978 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002979 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002980 }
2981
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002982 return (res)? 0 : -EIO;
2983}
2984
2985static int video_expand_toggle(void)
2986{
2987 switch (video_supported) {
2988 case TPACPI_VIDEO_570:
2989 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2990 0 : -EIO;
2991 case TPACPI_VIDEO_770:
2992 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2993 0 : -EIO;
2994 case TPACPI_VIDEO_NEW:
2995 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2996 0 : -EIO;
2997 default:
2998 return -ENOSYS;
2999 }
3000 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003001}
3002
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003003static int video_read(char *p)
3004{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003005 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003006 int len = 0;
3007
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003008 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003009 len += sprintf(p + len, "status:\t\tnot supported\n");
3010 return len;
3011 }
3012
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003013 status = video_outputsw_get();
3014 if (status < 0)
3015 return status;
3016
3017 autosw = video_autosw_get();
3018 if (autosw < 0)
3019 return autosw;
3020
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003021 len += sprintf(p + len, "status:\t\tsupported\n");
3022 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3023 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003024 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003025 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3026 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3027 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3028 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003029 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003030 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3031 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3032 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3033
3034 return len;
3035}
3036
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003037static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
3039 char *cmd;
3040 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003041 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003043 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003044 return -ENODEV;
3045
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003046 enable = 0;
3047 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 while ((cmd = next_cmd(&buf))) {
3050 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003051 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003053 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003055 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003057 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003058 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003059 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003060 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003061 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003062 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003063 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003065 res = video_autosw_set(1);
3066 if (res)
3067 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003069 res = video_autosw_set(0);
3070 if (res)
3071 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003073 res = video_outputsw_cycle();
3074 if (res)
3075 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003077 res = video_expand_toggle();
3078 if (res)
3079 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 } else
3081 return -EINVAL;
3082 }
3083
3084 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003085 status = video_outputsw_get();
3086 if (status < 0)
3087 return status;
3088 res = video_outputsw_set((status & ~disable) | enable);
3089 if (res)
3090 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092
3093 return 0;
3094}
3095
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003096static struct ibm_struct video_driver_data = {
3097 .name = "video",
3098 .read = video_read,
3099 .write = video_write,
3100 .exit = video_exit,
3101};
3102
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003103#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3104
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003105/*************************************************************************
3106 * Light (thinklight) subdriver
3107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003109TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3110TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003111
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003112static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003114 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3115
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003116 TPACPI_ACPIHANDLE_INIT(ledb);
3117 TPACPI_ACPIHANDLE_INIT(lght);
3118 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003119
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003120 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003121 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003122
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003123 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003124 /* light status not supported on
3125 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003126 tp_features.light_status =
3127 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003129 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003130 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003131
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003132 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133}
3134
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003135static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
3137 int len = 0;
3138 int status = 0;
3139
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003140 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003141 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003142 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003143 len += sprintf(p + len, "status:\t\tunknown\n");
3144 len += sprintf(p + len, "commands:\ton, off\n");
3145 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3147 return -EIO;
3148 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003149 len += sprintf(p + len, "commands:\ton, off\n");
3150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 return len;
3153}
3154
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003155static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
3157 int cmos_cmd, lght_cmd;
3158 char *cmd;
3159 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003160
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003161 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003162 return -ENODEV;
3163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 while ((cmd = next_cmd(&buf))) {
3165 if (strlencmp(cmd, "on") == 0) {
3166 cmos_cmd = 0x0c;
3167 lght_cmd = 1;
3168 } else if (strlencmp(cmd, "off") == 0) {
3169 cmos_cmd = 0x0d;
3170 lght_cmd = 0;
3171 } else
3172 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003175 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3176 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 if (!success)
3178 return -EIO;
3179 }
3180
3181 return 0;
3182}
3183
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003184static struct ibm_struct light_driver_data = {
3185 .name = "light",
3186 .read = light_read,
3187 .write = light_write,
3188};
3189
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003190/*************************************************************************
3191 * Dock subdriver
3192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003194#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003195
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003196static void dock_notify(struct ibm_struct *ibm, u32 event);
3197static int dock_read(char *p);
3198static int dock_write(char *buf);
3199
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003200TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003201 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3202 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3203 "\\_SB.PCI.ISA.SLCE", /* 570 */
3204 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3205
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003206/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003207TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003208
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003209static const struct acpi_device_id ibm_pci_device_ids[] = {
3210 {PCI_ROOT_HID_STRING, 0},
3211 {"", 0},
3212};
3213
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003214static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3215 {
3216 .notify = dock_notify,
3217 .handle = &dock_handle,
3218 .type = ACPI_SYSTEM_NOTIFY,
3219 },
3220 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003221 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3222 * We just use it to get notifications of dock hotplug
3223 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003224 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003225 .notify = dock_notify,
3226 .handle = &pci_handle,
3227 .type = ACPI_SYSTEM_NOTIFY,
3228 },
3229};
3230
3231static struct ibm_struct dock_driver_data[2] = {
3232 {
3233 .name = "dock",
3234 .read = dock_read,
3235 .write = dock_write,
3236 .acpi = &ibm_dock_acpidriver[0],
3237 },
3238 {
3239 .name = "dock",
3240 .acpi = &ibm_dock_acpidriver[1],
3241 },
3242};
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244#define dock_docked() (_sta(dock_handle) & 1)
3245
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003246static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003247{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003248 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3249
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003250 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003251
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003252 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3253 str_supported(dock_handle != NULL));
3254
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003255 return (dock_handle)? 0 : 1;
3256}
3257
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003258static int __init dock_init2(struct ibm_init_struct *iibm)
3259{
3260 int dock2_needed;
3261
3262 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3263
3264 if (dock_driver_data[0].flags.acpi_driver_registered &&
3265 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003266 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003267 dock2_needed = (pci_handle != NULL);
3268 vdbg_printk(TPACPI_DBG_INIT,
3269 "dock PCI handler for the TP 570 is %s\n",
3270 str_supported(dock2_needed));
3271 } else {
3272 vdbg_printk(TPACPI_DBG_INIT,
3273 "dock subdriver part 2 not required\n");
3274 dock2_needed = 0;
3275 }
3276
3277 return (dock2_needed)? 0 : 1;
3278}
3279
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003280static void dock_notify(struct ibm_struct *ibm, u32 event)
3281{
3282 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003283 int pci = ibm->acpi->hid && ibm->acpi->device &&
3284 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003285 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003286
3287 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003288 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003289 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003290 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003291 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003292 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003293 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003294 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003295 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003296 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003297 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003298 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003299 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003300 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003301 }
Len Brown14e04fb32007-08-23 15:20:26 -04003302 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003303 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3304 ibm->acpi->device->dev.bus_id,
3305 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003306}
3307
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003308static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
3310 int len = 0;
3311 int docked = dock_docked();
3312
3313 if (!dock_handle)
3314 len += sprintf(p + len, "status:\t\tnot supported\n");
3315 else if (!docked)
3316 len += sprintf(p + len, "status:\t\tundocked\n");
3317 else {
3318 len += sprintf(p + len, "status:\t\tdocked\n");
3319 len += sprintf(p + len, "commands:\tdock, undock\n");
3320 }
3321
3322 return len;
3323}
3324
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003325static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 char *cmd;
3328
3329 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003330 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
3332 while ((cmd = next_cmd(&buf))) {
3333 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003334 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3335 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return -EIO;
3337 } else if (strlencmp(cmd, "dock") == 0) {
3338 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3339 return -EIO;
3340 } else
3341 return -EINVAL;
3342 }
3343
3344 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003347#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003349/*************************************************************************
3350 * Bay subdriver
3351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003353#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003354
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003355TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003356 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3357 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3358 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3359 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003360TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003361 "_EJ0", /* all others */
3362 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003363TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003364 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3365 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003366TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003367 "_EJ0", /* 770x */
3368 ); /* all others */
3369
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003370static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003372 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3373
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003374 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003375 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003376 TPACPI_ACPIHANDLE_INIT(bay_ej);
3377 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003378 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003379 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003380
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003381 tp_features.bay_status = bay_handle &&
3382 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3383 tp_features.bay_status2 = bay2_handle &&
3384 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003385
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003386 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3387 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3388 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3389 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003391 vdbg_printk(TPACPI_DBG_INIT,
3392 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003393 str_supported(tp_features.bay_status),
3394 str_supported(tp_features.bay_eject),
3395 str_supported(tp_features.bay_status2),
3396 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003397
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003398 return (tp_features.bay_status || tp_features.bay_eject ||
3399 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003402static void bay_notify(struct ibm_struct *ibm, u32 event)
3403{
Len Brown14e04fb32007-08-23 15:20:26 -04003404 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003405 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3406 ibm->acpi->device->dev.bus_id,
3407 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003408}
3409
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003410#define bay_occupied(b) (_sta(b##_handle) & 1)
3411
3412static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413{
3414 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003415 int occupied = bay_occupied(bay);
3416 int occupied2 = bay_occupied(bay2);
3417 int eject, eject2;
3418
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003419 len += sprintf(p + len, "status:\t\t%s\n",
3420 tp_features.bay_status ?
3421 (occupied ? "occupied" : "unoccupied") :
3422 "not supported");
3423 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003424 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3425 "occupied" : "unoccupied");
3426
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003427 eject = tp_features.bay_eject && occupied;
3428 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003429
3430 if (eject && eject2)
3431 len += sprintf(p + len, "commands:\teject, eject2\n");
3432 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003434 else if (eject2)
3435 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 return len;
3438}
3439
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003440static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
3442 char *cmd;
3443
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003444 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003445 return -ENODEV;
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003448 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003449 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3450 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003451 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003452 strlencmp(cmd, "eject2") == 0) {
3453 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 return -EIO;
3455 } else
3456 return -EINVAL;
3457 }
3458
3459 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003460}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003461
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003462static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3463 .notify = bay_notify,
3464 .handle = &bay_handle,
3465 .type = ACPI_SYSTEM_NOTIFY,
3466};
3467
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003468static struct ibm_struct bay_driver_data = {
3469 .name = "bay",
3470 .read = bay_read,
3471 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003472 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003473};
3474
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003475#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003477/*************************************************************************
3478 * CMOS subdriver
3479 */
3480
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003481/* sysfs cmos_command -------------------------------------------------- */
3482static ssize_t cmos_command_store(struct device *dev,
3483 struct device_attribute *attr,
3484 const char *buf, size_t count)
3485{
3486 unsigned long cmos_cmd;
3487 int res;
3488
3489 if (parse_strtoul(buf, 21, &cmos_cmd))
3490 return -EINVAL;
3491
3492 res = issue_thinkpad_cmos_command(cmos_cmd);
3493 return (res)? res : count;
3494}
3495
3496static struct device_attribute dev_attr_cmos_command =
3497 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3498
3499/* --------------------------------------------------------------------- */
3500
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003501static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003502{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003503 int res;
3504
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003505 vdbg_printk(TPACPI_DBG_INIT,
3506 "initializing cmos commands subdriver\n");
3507
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003508 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003509
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003510 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3511 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003512
3513 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3514 if (res)
3515 return res;
3516
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003517 return (cmos_handle)? 0 : 1;
3518}
3519
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003520static void cmos_exit(void)
3521{
3522 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3523}
3524
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003525static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
3527 int len = 0;
3528
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003529 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3530 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 if (!cmos_handle)
3532 len += sprintf(p + len, "status:\t\tnot supported\n");
3533 else {
3534 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003535 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 }
3537
3538 return len;
3539}
3540
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003541static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003544 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003547 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3548 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 /* cmos_cmd set */
3550 } else
3551 return -EINVAL;
3552
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003553 res = issue_thinkpad_cmos_command(cmos_cmd);
3554 if (res)
3555 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 }
3557
3558 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003559}
3560
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003561static struct ibm_struct cmos_driver_data = {
3562 .name = "cmos",
3563 .read = cmos_read,
3564 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003565 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003566};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003567
3568/*************************************************************************
3569 * LED subdriver
3570 */
3571
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003572enum led_access_mode {
3573 TPACPI_LED_NONE = 0,
3574 TPACPI_LED_570, /* 570 */
3575 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3576 TPACPI_LED_NEW, /* all others */
3577};
3578
3579enum { /* For TPACPI_LED_OLD */
3580 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3581 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3582 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3583};
3584
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003585static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003586
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003587TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003588 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3589 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003590 "LED", /* all others */
3591 ); /* R30, R31 */
3592
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003593static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003594{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003595 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3596
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003597 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003598
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003599 if (!led_handle)
3600 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003601 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003602 else if (strlencmp(led_path, "SLED") == 0)
3603 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003604 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003605 else if (strlencmp(led_path, "SYSL") == 0)
3606 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003607 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003608 else
3609 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003610 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003611
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003612 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3613 str_supported(led_supported), led_supported);
3614
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003615 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003616}
3617
3618#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3619
3620static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
3622 int len = 0;
3623
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003624 if (!led_supported) {
3625 len += sprintf(p + len, "status:\t\tnot supported\n");
3626 return len;
3627 }
3628 len += sprintf(p + len, "status:\t\tsupported\n");
3629
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003630 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003631 /* 570 */
3632 int i, status;
3633 for (i = 0; i < 8; i++) {
3634 if (!acpi_evalf(ec_handle,
3635 &status, "GLED", "dd", 1 << i))
3636 return -EIO;
3637 len += sprintf(p + len, "%d:\t\t%s\n",
3638 i, led_status(status));
3639 }
3640 }
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003643 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
3645 return len;
3646}
3647
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003648/* off, on, blink */
3649static const int led_sled_arg1[] = { 0, 1, 3 };
3650static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3651static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3652static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3653
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003654static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
3656 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003657 int led, ind, ret;
3658
3659 if (!led_supported)
3660 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
3662 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003663 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return -EINVAL;
3665
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003666 if (strstr(cmd, "off")) {
3667 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003669 ind = 1;
3670 } else if (strstr(cmd, "blink")) {
3671 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 } else
3673 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003674
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003675 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003676 /* 570 */
3677 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003679 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003681 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003682 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3683 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003684 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003685 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003686 ret = ec_write(TPACPI_LED_EC_HLBL,
3687 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003688 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003689 ret = ec_write(TPACPI_LED_EC_HLCL,
3690 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003691 if (ret < 0)
3692 return ret;
3693 } else {
3694 /* all others */
3695 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3696 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 }
3700
3701 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003702}
3703
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003704static struct ibm_struct led_driver_data = {
3705 .name = "led",
3706 .read = led_read,
3707 .write = led_write,
3708};
3709
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003710/*************************************************************************
3711 * Beep subdriver
3712 */
3713
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003714TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003715
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003716static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003717{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003718 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3719
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003720 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003721
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003722 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3723 str_supported(beep_handle != NULL));
3724
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003725 return (beep_handle)? 0 : 1;
3726}
3727
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003728static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
3730 int len = 0;
3731
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003732 if (!beep_handle)
3733 len += sprintf(p + len, "status:\t\tnot supported\n");
3734 else {
3735 len += sprintf(p + len, "status:\t\tsupported\n");
3736 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
3739 return len;
3740}
3741
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003742static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743{
3744 char *cmd;
3745 int beep_cmd;
3746
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003747 if (!beep_handle)
3748 return -ENODEV;
3749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003751 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3752 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 /* beep_cmd set */
3754 } else
3755 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003756 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 return -EIO;
3758 }
3759
3760 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003761}
3762
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003763static struct ibm_struct beep_driver_data = {
3764 .name = "beep",
3765 .read = beep_read,
3766 .write = beep_write,
3767};
3768
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003769/*************************************************************************
3770 * Thermal subdriver
3771 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003772
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003773enum thermal_access_mode {
3774 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3775 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3776 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3777 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3778 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3779};
3780
3781enum { /* TPACPI_THERMAL_TPEC_* */
3782 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3783 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3784 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3785};
3786
3787#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3788struct ibm_thermal_sensors_struct {
3789 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3790};
3791
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003792static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003793
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003794/* idx is zero-based */
3795static int thermal_get_sensor(int idx, s32 *value)
3796{
3797 int t;
3798 s8 tmp;
3799 char tmpi[5];
3800
3801 t = TP_EC_THERMAL_TMP0;
3802
3803 switch (thermal_read_mode) {
3804#if TPACPI_MAX_THERMAL_SENSORS >= 16
3805 case TPACPI_THERMAL_TPEC_16:
3806 if (idx >= 8 && idx <= 15) {
3807 t = TP_EC_THERMAL_TMP8;
3808 idx -= 8;
3809 }
3810 /* fallthrough */
3811#endif
3812 case TPACPI_THERMAL_TPEC_8:
3813 if (idx <= 7) {
3814 if (!acpi_ec_read(t + idx, &tmp))
3815 return -EIO;
3816 *value = tmp * 1000;
3817 return 0;
3818 }
3819 break;
3820
3821 case TPACPI_THERMAL_ACPI_UPDT:
3822 if (idx <= 7) {
3823 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3824 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3825 return -EIO;
3826 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3827 return -EIO;
3828 *value = (t - 2732) * 100;
3829 return 0;
3830 }
3831 break;
3832
3833 case TPACPI_THERMAL_ACPI_TMP07:
3834 if (idx <= 7) {
3835 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3836 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3837 return -EIO;
3838 if (t > 127 || t < -127)
3839 t = TP_EC_THERMAL_TMP_NA;
3840 *value = t * 1000;
3841 return 0;
3842 }
3843 break;
3844
3845 case TPACPI_THERMAL_NONE:
3846 default:
3847 return -ENOSYS;
3848 }
3849
3850 return -EINVAL;
3851}
3852
3853static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3854{
3855 int res, i;
3856 int n;
3857
3858 n = 8;
3859 i = 0;
3860
3861 if (!s)
3862 return -EINVAL;
3863
3864 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3865 n = 16;
3866
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003867 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003868 res = thermal_get_sensor(i, &s->temp[i]);
3869 if (res)
3870 return res;
3871 }
3872
3873 return n;
3874}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003875
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003876/* sysfs temp##_input -------------------------------------------------- */
3877
3878static ssize_t thermal_temp_input_show(struct device *dev,
3879 struct device_attribute *attr,
3880 char *buf)
3881{
3882 struct sensor_device_attribute *sensor_attr =
3883 to_sensor_dev_attr(attr);
3884 int idx = sensor_attr->index;
3885 s32 value;
3886 int res;
3887
3888 res = thermal_get_sensor(idx, &value);
3889 if (res)
3890 return res;
3891 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3892 return -ENXIO;
3893
3894 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3895}
3896
3897#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003898 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3899 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003900
3901static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3902 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3903 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3904 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3905 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3906 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3907 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3908 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3909 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3910 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3911 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3912 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3913 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3914 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3915 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3916 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3917 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3918};
3919
3920#define THERMAL_ATTRS(X) \
3921 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3922
3923static struct attribute *thermal_temp_input_attr[] = {
3924 THERMAL_ATTRS(8),
3925 THERMAL_ATTRS(9),
3926 THERMAL_ATTRS(10),
3927 THERMAL_ATTRS(11),
3928 THERMAL_ATTRS(12),
3929 THERMAL_ATTRS(13),
3930 THERMAL_ATTRS(14),
3931 THERMAL_ATTRS(15),
3932 THERMAL_ATTRS(0),
3933 THERMAL_ATTRS(1),
3934 THERMAL_ATTRS(2),
3935 THERMAL_ATTRS(3),
3936 THERMAL_ATTRS(4),
3937 THERMAL_ATTRS(5),
3938 THERMAL_ATTRS(6),
3939 THERMAL_ATTRS(7),
3940 NULL
3941};
3942
3943static const struct attribute_group thermal_temp_input16_group = {
3944 .attrs = thermal_temp_input_attr
3945};
3946
3947static const struct attribute_group thermal_temp_input8_group = {
3948 .attrs = &thermal_temp_input_attr[8]
3949};
3950
3951#undef THERMAL_SENSOR_ATTR_TEMP
3952#undef THERMAL_ATTRS
3953
3954/* --------------------------------------------------------------------- */
3955
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003956static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003957{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003958 u8 t, ta1, ta2;
3959 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003960 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003961 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003962
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003963 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3964
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003965 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003966
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003967 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003968 /*
3969 * Direct EC access mode: sensors at registers
3970 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3971 * non-implemented, thermal sensors return 0x80 when
3972 * not available
3973 */
3974
3975 ta1 = ta2 = 0;
3976 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003977 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003978 ta1 |= t;
3979 } else {
3980 ta1 = 0;
3981 break;
3982 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003983 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003984 ta2 |= t;
3985 } else {
3986 ta1 = 0;
3987 break;
3988 }
3989 }
3990 if (ta1 == 0) {
3991 /* This is sheer paranoia, but we handle it anyway */
3992 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003993 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003994 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003995 "falling back to ACPI TMPx access "
3996 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003997 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003998 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003999 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004000 "ThinkPad ACPI EC access misbehaving, "
4001 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004002 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004003 }
4004 } else {
4005 thermal_read_mode =
4006 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004007 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004008 }
4009 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004010 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4011 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004012 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004013 } else {
4014 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004015 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004016 }
4017 } else {
4018 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004019 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004020 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004021
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004022 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4023 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4024 thermal_read_mode);
4025
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004026 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004027 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004028 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004029 &thermal_temp_input16_group);
4030 if (res)
4031 return res;
4032 break;
4033 case TPACPI_THERMAL_TPEC_8:
4034 case TPACPI_THERMAL_ACPI_TMP07:
4035 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004036 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004037 &thermal_temp_input8_group);
4038 if (res)
4039 return res;
4040 break;
4041 case TPACPI_THERMAL_NONE:
4042 default:
4043 return 1;
4044 }
4045
4046 return 0;
4047}
4048
4049static void thermal_exit(void)
4050{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004051 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004052 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004053 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004054 &thermal_temp_input16_group);
4055 break;
4056 case TPACPI_THERMAL_TPEC_8:
4057 case TPACPI_THERMAL_ACPI_TMP07:
4058 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004059 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004060 &thermal_temp_input16_group);
4061 break;
4062 case TPACPI_THERMAL_NONE:
4063 default:
4064 break;
4065 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004066}
4067
4068static int thermal_read(char *p)
4069{
4070 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004071 int n, i;
4072 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004073
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004074 n = thermal_get_sensors(&t);
4075 if (unlikely(n < 0))
4076 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004077
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004078 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004079
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004080 if (n > 0) {
4081 for (i = 0; i < (n - 1); i++)
4082 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4083 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4084 } else
4085 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004086
4087 return len;
4088}
4089
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004090static struct ibm_struct thermal_driver_data = {
4091 .name = "thermal",
4092 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004093 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004094};
4095
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004096/*************************************************************************
4097 * EC Dump subdriver
4098 */
4099
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004100static u8 ecdump_regs[256];
4101
4102static int ecdump_read(char *p)
4103{
4104 int len = 0;
4105 int i, j;
4106 u8 v;
4107
4108 len += sprintf(p + len, "EC "
4109 " +00 +01 +02 +03 +04 +05 +06 +07"
4110 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4111 for (i = 0; i < 256; i += 16) {
4112 len += sprintf(p + len, "EC 0x%02x:", i);
4113 for (j = 0; j < 16; j++) {
4114 if (!acpi_ec_read(i + j, &v))
4115 break;
4116 if (v != ecdump_regs[i + j])
4117 len += sprintf(p + len, " *%02x", v);
4118 else
4119 len += sprintf(p + len, " %02x", v);
4120 ecdump_regs[i + j] = v;
4121 }
4122 len += sprintf(p + len, "\n");
4123 if (j != 16)
4124 break;
4125 }
4126
4127 /* These are way too dangerous to advertise openly... */
4128#if 0
4129 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4130 " (<offset> is 00-ff, <value> is 00-ff)\n");
4131 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4132 " (<offset> is 00-ff, <value> is 0-255)\n");
4133#endif
4134 return len;
4135}
4136
4137static int ecdump_write(char *buf)
4138{
4139 char *cmd;
4140 int i, v;
4141
4142 while ((cmd = next_cmd(&buf))) {
4143 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4144 /* i and v set */
4145 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4146 /* i and v set */
4147 } else
4148 return -EINVAL;
4149 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4150 if (!acpi_ec_write(i, v))
4151 return -EIO;
4152 } else
4153 return -EINVAL;
4154 }
4155
4156 return 0;
4157}
4158
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004159static struct ibm_struct ecdump_driver_data = {
4160 .name = "ecdump",
4161 .read = ecdump_read,
4162 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004163 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004164};
4165
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004166/*************************************************************************
4167 * Backlight/brightness subdriver
4168 */
Holger Macht8acb0252006-10-20 14:30:28 -07004169
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004170#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4171
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004172static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004173static int brightness_offset = 0x31;
4174static int brightness_mode;
4175static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4176
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004177static struct mutex brightness_mutex;
4178
4179/*
4180 * ThinkPads can read brightness from two places: EC 0x31, or
4181 * CMOS NVRAM byte 0x5E, bits 0-3.
4182 */
4183static int brightness_get(struct backlight_device *bd)
4184{
4185 u8 lec = 0, lcmos = 0, level = 0;
4186
4187 if (brightness_mode & 1) {
4188 if (!acpi_ec_read(brightness_offset, &lec))
4189 return -EIO;
4190 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4191 level = lec;
4192 };
4193 if (brightness_mode & 2) {
4194 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4195 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4196 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4197 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4198 level = lcmos;
4199 }
4200
4201 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004202 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004203 "CMOS NVRAM (%u) and EC (%u) do not agree "
4204 "on display brightness level\n",
4205 (unsigned int) lcmos,
4206 (unsigned int) lec);
4207 return -EIO;
4208 }
4209
4210 return level;
4211}
4212
4213/* May return EINTR which can always be mapped to ERESTARTSYS */
4214static int brightness_set(int value)
4215{
4216 int cmos_cmd, inc, i, res;
4217 int current_value;
4218
4219 if (value > ((tp_features.bright_16levels)? 15 : 7))
4220 return -EINVAL;
4221
4222 res = mutex_lock_interruptible(&brightness_mutex);
4223 if (res < 0)
4224 return res;
4225
4226 current_value = brightness_get(NULL);
4227 if (current_value < 0) {
4228 res = current_value;
4229 goto errout;
4230 }
4231
4232 cmos_cmd = value > current_value ?
4233 TP_CMOS_BRIGHTNESS_UP :
4234 TP_CMOS_BRIGHTNESS_DOWN;
4235 inc = (value > current_value)? 1 : -1;
4236
4237 res = 0;
4238 for (i = current_value; i != value; i += inc) {
4239 if ((brightness_mode & 2) &&
4240 issue_thinkpad_cmos_command(cmos_cmd)) {
4241 res = -EIO;
4242 goto errout;
4243 }
4244 if ((brightness_mode & 1) &&
4245 !acpi_ec_write(brightness_offset, i + inc)) {
4246 res = -EIO;
4247 goto errout;;
4248 }
4249 }
4250
4251errout:
4252 mutex_unlock(&brightness_mutex);
4253 return res;
4254}
4255
4256/* sysfs backlight class ----------------------------------------------- */
4257
4258static int brightness_update_status(struct backlight_device *bd)
4259{
4260 /* it is the backlight class's job (caller) to handle
4261 * EINTR and other errors properly */
4262 return brightness_set(
4263 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4264 bd->props.power == FB_BLANK_UNBLANK) ?
4265 bd->props.brightness : 0);
4266}
Holger Macht8acb0252006-10-20 14:30:28 -07004267
Richard Purdie599a52d2007-02-10 23:07:48 +00004268static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004269 .get_brightness = brightness_get,
4270 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004271};
4272
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004273/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004274
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004275static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4276{
4277 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4278 union acpi_object *obj;
4279 int rc;
4280
4281 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4282 obj = (union acpi_object *)buffer.pointer;
4283 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004284 printk(TPACPI_ERR "Unknown BCLL data, "
4285 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004286 rc = 0;
4287 } else {
4288 rc = obj->package.count;
4289 }
4290 } else {
4291 return 0;
4292 }
4293
4294 kfree(buffer.pointer);
4295 return rc;
4296}
4297
4298static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4299 void *context, void **rv)
4300{
4301 char name[ACPI_PATH_SEGMENT_LENGTH];
4302 struct acpi_buffer buffer = { sizeof(name), &name };
4303
4304 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4305 !strncmp("BCLL", name, sizeof(name) - 1)) {
4306 if (tpacpi_query_bcll_levels(handle) == 16) {
4307 *rv = handle;
4308 return AE_CTRL_TERMINATE;
4309 } else {
4310 return AE_OK;
4311 }
4312 } else {
4313 return AE_OK;
4314 }
4315}
4316
4317static int __init brightness_check_levels(void)
4318{
4319 int status;
4320 void *found_node = NULL;
4321
4322 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004323 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004324 }
4325 if (!vid_handle)
4326 return 0;
4327
4328 /* Search for a BCLL package with 16 levels */
4329 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004330 brightness_find_bcll, NULL,
4331 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004332
4333 return (ACPI_SUCCESS(status) && found_node != NULL);
4334}
4335
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004336static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4337 void *context, void **rv)
4338{
4339 char name[ACPI_PATH_SEGMENT_LENGTH];
4340 struct acpi_buffer buffer = { sizeof(name), &name };
4341
4342 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4343 !strncmp("_BCL", name, sizeof(name) - 1)) {
4344 *rv = handle;
4345 return AE_CTRL_TERMINATE;
4346 } else {
4347 return AE_OK;
4348 }
4349}
4350
4351static int __init brightness_check_std_acpi_support(void)
4352{
4353 int status;
4354 void *found_node = NULL;
4355
4356 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004357 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004358 }
4359 if (!vid_handle)
4360 return 0;
4361
4362 /* Search for a _BCL method, but don't execute it */
4363 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004364 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004365
4366 return (ACPI_SUCCESS(status) && found_node != NULL);
4367}
4368
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004369static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004370{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004371 int b;
4372
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004373 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4374
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004375 mutex_init(&brightness_mutex);
4376
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004377 if (!brightness_enable) {
4378 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004379 "brightness support disabled by "
4380 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004381 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004382 } else if (brightness_enable > 1) {
4383 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004384 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004385 "standard ACPI backlight interface "
4386 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004387 return 1;
4388 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004389 }
4390
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004391 if (!brightness_mode) {
4392 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4393 brightness_mode = 2;
4394 else
4395 brightness_mode = 3;
4396
4397 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4398 brightness_mode);
4399 }
4400
4401 if (brightness_mode > 3)
4402 return -EINVAL;
4403
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004404 tp_features.bright_16levels =
4405 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4406 brightness_check_levels();
4407
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004408 b = brightness_get(NULL);
4409 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004410 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004411
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004412 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004413 printk(TPACPI_INFO
4414 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004415
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004416 ibm_backlight_device = backlight_device_register(
4417 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4418 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004419 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004420 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004421 return PTR_ERR(ibm_backlight_device);
4422 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004423 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004424
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004425 ibm_backlight_device->props.max_brightness =
4426 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004427 ibm_backlight_device->props.brightness = b;
4428 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004429
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004430 return 0;
4431}
4432
4433static void brightness_exit(void)
4434{
4435 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004436 vdbg_printk(TPACPI_DBG_EXIT,
4437 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004438 backlight_device_unregister(ibm_backlight_device);
4439 ibm_backlight_device = NULL;
4440 }
4441}
4442
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004443static int brightness_read(char *p)
4444{
4445 int len = 0;
4446 int level;
4447
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004448 level = brightness_get(NULL);
4449 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004450 len += sprintf(p + len, "level:\t\tunreadable\n");
4451 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004452 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004453 len += sprintf(p + len, "commands:\tup, down\n");
4454 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004455 " (<level> is 0-%d)\n",
4456 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004457 }
4458
4459 return len;
4460}
4461
4462static int brightness_write(char *buf)
4463{
4464 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004465 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004466 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004467 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004468
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004469 level = brightness_get(NULL);
4470 if (level < 0)
4471 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004472
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004473 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004474 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004475 if (level < max_level)
4476 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004477 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004478 if (level > 0)
4479 level--;
4480 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4481 level >= 0 && level <= max_level) {
4482 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004483 } else
4484 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004485 }
4486
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004487 /*
4488 * Now we know what the final level should be, so we try to set it.
4489 * Doing it this way makes the syscall restartable in case of EINTR
4490 */
4491 rc = brightness_set(level);
4492 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004493}
4494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004495static struct ibm_struct brightness_driver_data = {
4496 .name = "brightness",
4497 .read = brightness_read,
4498 .write = brightness_write,
4499 .exit = brightness_exit,
4500};
4501
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004502/*************************************************************************
4503 * Volume subdriver
4504 */
4505
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004506static int volume_offset = 0x30;
4507
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004508static int volume_read(char *p)
4509{
4510 int len = 0;
4511 u8 level;
4512
4513 if (!acpi_ec_read(volume_offset, &level)) {
4514 len += sprintf(p + len, "level:\t\tunreadable\n");
4515 } else {
4516 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4517 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4518 len += sprintf(p + len, "commands:\tup, down, mute\n");
4519 len += sprintf(p + len, "commands:\tlevel <level>"
4520 " (<level> is 0-15)\n");
4521 }
4522
4523 return len;
4524}
4525
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004526static int volume_write(char *buf)
4527{
4528 int cmos_cmd, inc, i;
4529 u8 level, mute;
4530 int new_level, new_mute;
4531 char *cmd;
4532
4533 while ((cmd = next_cmd(&buf))) {
4534 if (!acpi_ec_read(volume_offset, &level))
4535 return -EIO;
4536 new_mute = mute = level & 0x40;
4537 new_level = level = level & 0xf;
4538
4539 if (strlencmp(cmd, "up") == 0) {
4540 if (mute)
4541 new_mute = 0;
4542 else
4543 new_level = level == 15 ? 15 : level + 1;
4544 } else if (strlencmp(cmd, "down") == 0) {
4545 if (mute)
4546 new_mute = 0;
4547 else
4548 new_level = level == 0 ? 0 : level - 1;
4549 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4550 new_level >= 0 && new_level <= 15) {
4551 /* new_level set */
4552 } else if (strlencmp(cmd, "mute") == 0) {
4553 new_mute = 0x40;
4554 } else
4555 return -EINVAL;
4556
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004557 if (new_level != level) {
4558 /* mute doesn't change */
4559
4560 cmos_cmd = (new_level > level) ?
4561 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004562 inc = new_level > level ? 1 : -1;
4563
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004564 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004565 !acpi_ec_write(volume_offset, level)))
4566 return -EIO;
4567
4568 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004569 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004570 !acpi_ec_write(volume_offset, i + inc))
4571 return -EIO;
4572
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004573 if (mute &&
4574 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4575 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004576 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004577 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004578 }
4579
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004580 if (new_mute != mute) {
4581 /* level doesn't change */
4582
4583 cmos_cmd = (new_mute) ?
4584 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004585
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004586 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004587 !acpi_ec_write(volume_offset, level + new_mute))
4588 return -EIO;
4589 }
4590 }
4591
4592 return 0;
4593}
4594
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004595static struct ibm_struct volume_driver_data = {
4596 .name = "volume",
4597 .read = volume_read,
4598 .write = volume_write,
4599};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004600
4601/*************************************************************************
4602 * Fan subdriver
4603 */
4604
4605/*
4606 * FAN ACCESS MODES
4607 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004608 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004609 * ACPI GFAN method: returns fan level
4610 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004611 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004612 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004613 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004614 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004615 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4616 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004617 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4618 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004619 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004620 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004621 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4622 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004623 *
4624 * Fan speed changes of any sort (including those caused by the
4625 * disengaged mode) are usually done slowly by the firmware as the
4626 * maximum ammount of fan duty cycle change per second seems to be
4627 * limited.
4628 *
4629 * Reading is not available if GFAN exists.
4630 * Writing is not available if SFAN exists.
4631 *
4632 * Bits
4633 * 7 automatic mode engaged;
4634 * (default operation mode of the ThinkPad)
4635 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004636 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004637 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004638 * the tachometer while the fan controller ramps up
4639 * the speed (which can take up to a few *minutes*).
4640 * Speeds up fan to 100% duty-cycle, which is far above
4641 * the standard RPM levels. It is not impossible that
4642 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004643 * 5-3 unused in some models. Extra bits for fan level
4644 * in others, but still useless as all values above
4645 * 7 map to the same speed as level 7 in these models.
4646 * 2-0 fan level (0..7 usually)
4647 * 0x00 = stop
4648 * 0x07 = max (set when temperatures critical)
4649 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004650 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004651 *
4652 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4653 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4654 * does so, its initial value is meaningless (0x07).
4655 *
4656 * For firmware bugs, refer to:
4657 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4658 *
4659 * ----
4660 *
4661 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4662 * Main fan tachometer reading (in RPM)
4663 *
4664 * This register is present on all ThinkPads with a new-style EC, and
4665 * it is known not to be present on the A21m/e, and T22, as there is
4666 * something else in offset 0x84 according to the ACPI DSDT. Other
4667 * ThinkPads from this same time period (and earlier) probably lack the
4668 * tachometer as well.
4669 *
4670 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4671 * was never fixed by IBM to report the EC firmware version string
4672 * probably support the tachometer (like the early X models), so
4673 * detecting it is quite hard. We need more data to know for sure.
4674 *
4675 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4676 * might result.
4677 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004678 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4679 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004680 *
4681 * For firmware bugs, refer to:
4682 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4683 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004684 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004685 * ThinkPad X31, X40, X41. Not available in the X60.
4686 *
4687 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4688 * high speed. ACPI DSDT seems to map these three speeds to levels
4689 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4690 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4691 *
4692 * The speeds are stored on handles
4693 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4694 *
4695 * There are three default speed sets, acessible as handles:
4696 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4697 *
4698 * ACPI DSDT switches which set is in use depending on various
4699 * factors.
4700 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004701 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004702 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4703 * but the ACPI tables just mention level 7.
4704 */
4705
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004706enum { /* Fan control constants */
4707 fan_status_offset = 0x2f, /* EC register 0x2f */
4708 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4709 * 0x84 must be read before 0x85 */
4710
4711 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4712 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4713
4714 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4715};
4716
4717enum fan_status_access_mode {
4718 TPACPI_FAN_NONE = 0, /* No fan status or control */
4719 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4720 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4721};
4722
4723enum fan_control_access_mode {
4724 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4725 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4726 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4727 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4728};
4729
4730enum fan_control_commands {
4731 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4732 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4733 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4734 * and also watchdog cmd */
4735};
4736
4737static int fan_control_allowed;
4738
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004739static enum fan_status_access_mode fan_status_access_mode;
4740static enum fan_control_access_mode fan_control_access_mode;
4741static enum fan_control_commands fan_control_commands;
4742
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004743static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004744static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004745static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004746
4747static struct mutex fan_mutex;
4748
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004749static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004750static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004751
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004752TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4753TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004754 "\\FSPD", /* 600e/x, 770e, 770x */
4755 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004756TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004757 "JFNS", /* 770x-JL */
4758 ); /* all others */
4759
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004760/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004761 * Call with fan_mutex held
4762 */
4763static void fan_update_desired_level(u8 status)
4764{
4765 if ((status &
4766 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4767 if (status > 7)
4768 fan_control_desired_level = 7;
4769 else
4770 fan_control_desired_level = status;
4771 }
4772}
4773
4774static int fan_get_status(u8 *status)
4775{
4776 u8 s;
4777
4778 /* TODO:
4779 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4780
4781 switch (fan_status_access_mode) {
4782 case TPACPI_FAN_RD_ACPI_GFAN:
4783 /* 570, 600e/x, 770e, 770x */
4784
4785 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4786 return -EIO;
4787
4788 if (likely(status))
4789 *status = s & 0x07;
4790
4791 break;
4792
4793 case TPACPI_FAN_RD_TPEC:
4794 /* all except 570, 600e/x, 770e, 770x */
4795 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4796 return -EIO;
4797
4798 if (likely(status))
4799 *status = s;
4800
4801 break;
4802
4803 default:
4804 return -ENXIO;
4805 }
4806
4807 return 0;
4808}
4809
4810static int fan_get_status_safe(u8 *status)
4811{
4812 int rc;
4813 u8 s;
4814
4815 if (mutex_lock_interruptible(&fan_mutex))
4816 return -ERESTARTSYS;
4817 rc = fan_get_status(&s);
4818 if (!rc)
4819 fan_update_desired_level(s);
4820 mutex_unlock(&fan_mutex);
4821
4822 if (status)
4823 *status = s;
4824
4825 return rc;
4826}
4827
4828static int fan_get_speed(unsigned int *speed)
4829{
4830 u8 hi, lo;
4831
4832 switch (fan_status_access_mode) {
4833 case TPACPI_FAN_RD_TPEC:
4834 /* all except 570, 600e/x, 770e, 770x */
4835 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4836 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4837 return -EIO;
4838
4839 if (likely(speed))
4840 *speed = (hi << 8) | lo;
4841
4842 break;
4843
4844 default:
4845 return -ENXIO;
4846 }
4847
4848 return 0;
4849}
4850
4851static int fan_set_level(int level)
4852{
4853 if (!fan_control_allowed)
4854 return -EPERM;
4855
4856 switch (fan_control_access_mode) {
4857 case TPACPI_FAN_WR_ACPI_SFAN:
4858 if (level >= 0 && level <= 7) {
4859 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4860 return -EIO;
4861 } else
4862 return -EINVAL;
4863 break;
4864
4865 case TPACPI_FAN_WR_ACPI_FANS:
4866 case TPACPI_FAN_WR_TPEC:
4867 if ((level != TP_EC_FAN_AUTO) &&
4868 (level != TP_EC_FAN_FULLSPEED) &&
4869 ((level < 0) || (level > 7)))
4870 return -EINVAL;
4871
4872 /* safety net should the EC not support AUTO
4873 * or FULLSPEED mode bits and just ignore them */
4874 if (level & TP_EC_FAN_FULLSPEED)
4875 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01004876 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004877 level |= 4; /* safety min speed 4 */
4878
4879 if (!acpi_ec_write(fan_status_offset, level))
4880 return -EIO;
4881 else
4882 tp_features.fan_ctrl_status_undef = 0;
4883 break;
4884
4885 default:
4886 return -ENXIO;
4887 }
4888 return 0;
4889}
4890
4891static int fan_set_level_safe(int level)
4892{
4893 int rc;
4894
4895 if (!fan_control_allowed)
4896 return -EPERM;
4897
4898 if (mutex_lock_interruptible(&fan_mutex))
4899 return -ERESTARTSYS;
4900
4901 if (level == TPACPI_FAN_LAST_LEVEL)
4902 level = fan_control_desired_level;
4903
4904 rc = fan_set_level(level);
4905 if (!rc)
4906 fan_update_desired_level(level);
4907
4908 mutex_unlock(&fan_mutex);
4909 return rc;
4910}
4911
4912static int fan_set_enable(void)
4913{
4914 u8 s;
4915 int rc;
4916
4917 if (!fan_control_allowed)
4918 return -EPERM;
4919
4920 if (mutex_lock_interruptible(&fan_mutex))
4921 return -ERESTARTSYS;
4922
4923 switch (fan_control_access_mode) {
4924 case TPACPI_FAN_WR_ACPI_FANS:
4925 case TPACPI_FAN_WR_TPEC:
4926 rc = fan_get_status(&s);
4927 if (rc < 0)
4928 break;
4929
4930 /* Don't go out of emergency fan mode */
4931 if (s != 7) {
4932 s &= 0x07;
4933 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4934 }
4935
4936 if (!acpi_ec_write(fan_status_offset, s))
4937 rc = -EIO;
4938 else {
4939 tp_features.fan_ctrl_status_undef = 0;
4940 rc = 0;
4941 }
4942 break;
4943
4944 case TPACPI_FAN_WR_ACPI_SFAN:
4945 rc = fan_get_status(&s);
4946 if (rc < 0)
4947 break;
4948
4949 s &= 0x07;
4950
4951 /* Set fan to at least level 4 */
4952 s |= 4;
4953
4954 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004955 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004956 else
4957 rc = 0;
4958 break;
4959
4960 default:
4961 rc = -ENXIO;
4962 }
4963
4964 mutex_unlock(&fan_mutex);
4965 return rc;
4966}
4967
4968static int fan_set_disable(void)
4969{
4970 int rc;
4971
4972 if (!fan_control_allowed)
4973 return -EPERM;
4974
4975 if (mutex_lock_interruptible(&fan_mutex))
4976 return -ERESTARTSYS;
4977
4978 rc = 0;
4979 switch (fan_control_access_mode) {
4980 case TPACPI_FAN_WR_ACPI_FANS:
4981 case TPACPI_FAN_WR_TPEC:
4982 if (!acpi_ec_write(fan_status_offset, 0x00))
4983 rc = -EIO;
4984 else {
4985 fan_control_desired_level = 0;
4986 tp_features.fan_ctrl_status_undef = 0;
4987 }
4988 break;
4989
4990 case TPACPI_FAN_WR_ACPI_SFAN:
4991 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4992 rc = -EIO;
4993 else
4994 fan_control_desired_level = 0;
4995 break;
4996
4997 default:
4998 rc = -ENXIO;
4999 }
5000
5001
5002 mutex_unlock(&fan_mutex);
5003 return rc;
5004}
5005
5006static int fan_set_speed(int speed)
5007{
5008 int rc;
5009
5010 if (!fan_control_allowed)
5011 return -EPERM;
5012
5013 if (mutex_lock_interruptible(&fan_mutex))
5014 return -ERESTARTSYS;
5015
5016 rc = 0;
5017 switch (fan_control_access_mode) {
5018 case TPACPI_FAN_WR_ACPI_FANS:
5019 if (speed >= 0 && speed <= 65535) {
5020 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5021 speed, speed, speed))
5022 rc = -EIO;
5023 } else
5024 rc = -EINVAL;
5025 break;
5026
5027 default:
5028 rc = -ENXIO;
5029 }
5030
5031 mutex_unlock(&fan_mutex);
5032 return rc;
5033}
5034
5035static void fan_watchdog_reset(void)
5036{
5037 static int fan_watchdog_active;
5038
5039 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5040 return;
5041
5042 if (fan_watchdog_active)
5043 cancel_delayed_work(&fan_watchdog_task);
5044
5045 if (fan_watchdog_maxinterval > 0 &&
5046 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5047 fan_watchdog_active = 1;
5048 if (!schedule_delayed_work(&fan_watchdog_task,
5049 msecs_to_jiffies(fan_watchdog_maxinterval
5050 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005051 printk(TPACPI_ERR
5052 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005053 "watchdog will not trigger\n");
5054 }
5055 } else
5056 fan_watchdog_active = 0;
5057}
5058
5059static void fan_watchdog_fire(struct work_struct *ignored)
5060{
5061 int rc;
5062
5063 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5064 return;
5065
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005066 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005067 rc = fan_set_enable();
5068 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005069 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005070 "will try again later...\n", -rc);
5071 /* reschedule for later */
5072 fan_watchdog_reset();
5073 }
5074}
5075
5076/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005077 * SYSFS fan layout: hwmon compatible (device)
5078 *
5079 * pwm*_enable:
5080 * 0: "disengaged" mode
5081 * 1: manual mode
5082 * 2: native EC "auto" mode (recommended, hardware default)
5083 *
5084 * pwm*: set speed in manual mode, ignored otherwise.
5085 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5086 * interpolation.
5087 *
5088 * fan*_input: tachometer reading, RPM
5089 *
5090 *
5091 * SYSFS fan layout: extensions
5092 *
5093 * fan_watchdog (driver):
5094 * fan watchdog interval in seconds, 0 disables (default), max 120
5095 */
5096
5097/* sysfs fan pwm1_enable ----------------------------------------------- */
5098static ssize_t fan_pwm1_enable_show(struct device *dev,
5099 struct device_attribute *attr,
5100 char *buf)
5101{
5102 int res, mode;
5103 u8 status;
5104
5105 res = fan_get_status_safe(&status);
5106 if (res)
5107 return res;
5108
5109 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5110 if (status != fan_control_initial_status) {
5111 tp_features.fan_ctrl_status_undef = 0;
5112 } else {
5113 /* Return most likely status. In fact, it
5114 * might be the only possible status */
5115 status = TP_EC_FAN_AUTO;
5116 }
5117 }
5118
5119 if (status & TP_EC_FAN_FULLSPEED) {
5120 mode = 0;
5121 } else if (status & TP_EC_FAN_AUTO) {
5122 mode = 2;
5123 } else
5124 mode = 1;
5125
5126 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5127}
5128
5129static ssize_t fan_pwm1_enable_store(struct device *dev,
5130 struct device_attribute *attr,
5131 const char *buf, size_t count)
5132{
5133 unsigned long t;
5134 int res, level;
5135
5136 if (parse_strtoul(buf, 2, &t))
5137 return -EINVAL;
5138
5139 switch (t) {
5140 case 0:
5141 level = TP_EC_FAN_FULLSPEED;
5142 break;
5143 case 1:
5144 level = TPACPI_FAN_LAST_LEVEL;
5145 break;
5146 case 2:
5147 level = TP_EC_FAN_AUTO;
5148 break;
5149 case 3:
5150 /* reserved for software-controlled auto mode */
5151 return -ENOSYS;
5152 default:
5153 return -EINVAL;
5154 }
5155
5156 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005157 if (res == -ENXIO)
5158 return -EINVAL;
5159 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005160 return res;
5161
5162 fan_watchdog_reset();
5163
5164 return count;
5165}
5166
5167static struct device_attribute dev_attr_fan_pwm1_enable =
5168 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5169 fan_pwm1_enable_show, fan_pwm1_enable_store);
5170
5171/* sysfs fan pwm1 ------------------------------------------------------ */
5172static ssize_t fan_pwm1_show(struct device *dev,
5173 struct device_attribute *attr,
5174 char *buf)
5175{
5176 int res;
5177 u8 status;
5178
5179 res = fan_get_status_safe(&status);
5180 if (res)
5181 return res;
5182
5183 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5184 if (status != fan_control_initial_status) {
5185 tp_features.fan_ctrl_status_undef = 0;
5186 } else {
5187 status = TP_EC_FAN_AUTO;
5188 }
5189 }
5190
5191 if ((status &
5192 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5193 status = fan_control_desired_level;
5194
5195 if (status > 7)
5196 status = 7;
5197
5198 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5199}
5200
5201static ssize_t fan_pwm1_store(struct device *dev,
5202 struct device_attribute *attr,
5203 const char *buf, size_t count)
5204{
5205 unsigned long s;
5206 int rc;
5207 u8 status, newlevel;
5208
5209 if (parse_strtoul(buf, 255, &s))
5210 return -EINVAL;
5211
5212 /* scale down from 0-255 to 0-7 */
5213 newlevel = (s >> 5) & 0x07;
5214
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005215 if (mutex_lock_interruptible(&fan_mutex))
5216 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005217
5218 rc = fan_get_status(&status);
5219 if (!rc && (status &
5220 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5221 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005222 if (rc == -ENXIO)
5223 rc = -EINVAL;
5224 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005225 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005226 fan_watchdog_reset();
5227 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005228 }
5229
5230 mutex_unlock(&fan_mutex);
5231 return (rc)? rc : count;
5232}
5233
5234static struct device_attribute dev_attr_fan_pwm1 =
5235 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5236 fan_pwm1_show, fan_pwm1_store);
5237
5238/* sysfs fan fan1_input ------------------------------------------------ */
5239static ssize_t fan_fan1_input_show(struct device *dev,
5240 struct device_attribute *attr,
5241 char *buf)
5242{
5243 int res;
5244 unsigned int speed;
5245
5246 res = fan_get_speed(&speed);
5247 if (res < 0)
5248 return res;
5249
5250 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5251}
5252
5253static struct device_attribute dev_attr_fan_fan1_input =
5254 __ATTR(fan1_input, S_IRUGO,
5255 fan_fan1_input_show, NULL);
5256
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005257/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005258static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5259 char *buf)
5260{
5261 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5262}
5263
5264static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5265 const char *buf, size_t count)
5266{
5267 unsigned long t;
5268
5269 if (parse_strtoul(buf, 120, &t))
5270 return -EINVAL;
5271
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005272 if (!fan_control_allowed)
5273 return -EPERM;
5274
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005275 fan_watchdog_maxinterval = t;
5276 fan_watchdog_reset();
5277
5278 return count;
5279}
5280
5281static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5282 fan_fan_watchdog_show, fan_fan_watchdog_store);
5283
5284/* --------------------------------------------------------------------- */
5285static struct attribute *fan_attributes[] = {
5286 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5287 &dev_attr_fan_fan1_input.attr,
5288 NULL
5289};
5290
5291static const struct attribute_group fan_attr_group = {
5292 .attrs = fan_attributes,
5293};
5294
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005295static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005296{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005297 int rc;
5298
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005299 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5300
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005301 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005302 fan_status_access_mode = TPACPI_FAN_NONE;
5303 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005304 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005305 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005306 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005307 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005308
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005309 TPACPI_ACPIHANDLE_INIT(fans);
5310 TPACPI_ACPIHANDLE_INIT(gfan);
5311 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005312
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005313 if (gfan_handle) {
5314 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005315 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005316 } else {
5317 /* all other ThinkPads: note that even old-style
5318 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005319 if (likely(acpi_ec_read(fan_status_offset,
5320 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005321 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005322
5323 /* In some ThinkPads, neither the EC nor the ACPI
5324 * DSDT initialize the fan status, and it ends up
5325 * being set to 0x07 when it *could* be either
5326 * 0x07 or 0x80.
5327 *
5328 * Enable for TP-1Y (T43), TP-78 (R51e),
5329 * TP-76 (R52), TP-70 (T43, R52), which are known
5330 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005331 if (fan_control_initial_status == 0x07) {
5332 switch (thinkpad_id.ec_model) {
5333 case 0x5931: /* TP-1Y */
5334 case 0x3837: /* TP-78 */
5335 case 0x3637: /* TP-76 */
5336 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005337 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005338 "fan_init: initial fan status "
5339 "is unknown, assuming it is "
5340 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005341 tp_features.fan_ctrl_status_undef = 1;
5342 ;;
5343 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005344 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005345 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005346 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005347 "ThinkPad ACPI EC access misbehaving, "
5348 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005349 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005350 }
5351 }
5352
5353 if (sfan_handle) {
5354 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005355 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005356 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005357 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005358 } else {
5359 if (!gfan_handle) {
5360 /* gfan without sfan means no fan control */
5361 /* all other models implement TP EC 0x2f control */
5362
5363 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005364 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005365 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005366 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005367 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005368 TPACPI_FAN_CMD_SPEED |
5369 TPACPI_FAN_CMD_LEVEL |
5370 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005371 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005372 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005373 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005374 TPACPI_FAN_CMD_LEVEL |
5375 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005376 }
5377 }
5378 }
5379
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005380 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5381 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5382 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5383 fan_status_access_mode, fan_control_access_mode);
5384
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005385 /* fan control master switch */
5386 if (!fan_control_allowed) {
5387 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5388 fan_control_commands = 0;
5389 dbg_printk(TPACPI_DBG_INIT,
5390 "fan control features disabled by parameter\n");
5391 }
5392
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005393 /* update fan_control_desired_level */
5394 if (fan_status_access_mode != TPACPI_FAN_NONE)
5395 fan_get_status_safe(NULL);
5396
5397 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5398 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005399 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005400 &fan_attr_group);
5401 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005402 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005403 &driver_attr_fan_watchdog);
5404 if (rc < 0)
5405 return rc;
5406 return 0;
5407 } else
5408 return 1;
5409}
5410
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005411static void fan_exit(void)
5412{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005413 vdbg_printk(TPACPI_DBG_EXIT,
5414 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005415
5416 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005417 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005418 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5419 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005420
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005421 cancel_delayed_work(&fan_watchdog_task);
5422 flush_scheduled_work();
5423}
5424
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005425static int fan_read(char *p)
5426{
5427 int len = 0;
5428 int rc;
5429 u8 status;
5430 unsigned int speed = 0;
5431
5432 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005433 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005434 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005435 rc = fan_get_status_safe(&status);
5436 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005437 return rc;
5438
5439 len += sprintf(p + len, "status:\t\t%s\n"
5440 "level:\t\t%d\n",
5441 (status != 0) ? "enabled" : "disabled", status);
5442 break;
5443
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005444 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005445 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005446 rc = fan_get_status_safe(&status);
5447 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005448 return rc;
5449
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005450 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005451 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005452 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005453 else
5454 /* Return most likely status. In fact, it
5455 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005456 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005457 }
5458
5459 len += sprintf(p + len, "status:\t\t%s\n",
5460 (status != 0) ? "enabled" : "disabled");
5461
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005462 rc = fan_get_speed(&speed);
5463 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005464 return rc;
5465
5466 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5467
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005468 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005469 /* Disengaged mode takes precedence */
5470 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005471 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005472 len += sprintf(p + len, "level:\t\tauto\n");
5473 else
5474 len += sprintf(p + len, "level:\t\t%d\n", status);
5475 break;
5476
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005477 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005478 default:
5479 len += sprintf(p + len, "status:\t\tnot supported\n");
5480 }
5481
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005482 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005483 len += sprintf(p + len, "commands:\tlevel <level>");
5484
5485 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005486 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005487 len += sprintf(p + len, " (<level> is 0-7)\n");
5488 break;
5489
5490 default:
5491 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005492 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005493 break;
5494 }
5495 }
5496
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005497 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005498 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005499 "commands:\twatchdog <timeout> (<timeout> "
5500 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005501
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005502 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005503 len += sprintf(p + len, "commands:\tspeed <speed>"
5504 " (<speed> is 0-65535)\n");
5505
5506 return len;
5507}
5508
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005509static int fan_write_cmd_level(const char *cmd, int *rc)
5510{
5511 int level;
5512
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005513 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005514 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005515 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005516 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005517 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005518 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005519 return 0;
5520
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005521 *rc = fan_set_level_safe(level);
5522 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005523 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005524 "access mode %d", fan_control_access_mode);
5525
5526 return 1;
5527}
5528
5529static int fan_write_cmd_enable(const char *cmd, int *rc)
5530{
5531 if (strlencmp(cmd, "enable") != 0)
5532 return 0;
5533
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005534 *rc = fan_set_enable();
5535 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005536 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005537 "access mode %d", fan_control_access_mode);
5538
5539 return 1;
5540}
5541
5542static int fan_write_cmd_disable(const char *cmd, int *rc)
5543{
5544 if (strlencmp(cmd, "disable") != 0)
5545 return 0;
5546
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005547 *rc = fan_set_disable();
5548 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005549 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005550 "access mode %d", fan_control_access_mode);
5551
5552 return 1;
5553}
5554
5555static int fan_write_cmd_speed(const char *cmd, int *rc)
5556{
5557 int speed;
5558
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005559 /* TODO:
5560 * Support speed <low> <medium> <high> ? */
5561
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005562 if (sscanf(cmd, "speed %d", &speed) != 1)
5563 return 0;
5564
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005565 *rc = fan_set_speed(speed);
5566 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005567 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005568 "access mode %d", fan_control_access_mode);
5569
5570 return 1;
5571}
5572
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005573static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5574{
5575 int interval;
5576
5577 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5578 return 0;
5579
5580 if (interval < 0 || interval > 120)
5581 *rc = -EINVAL;
5582 else
5583 fan_watchdog_maxinterval = interval;
5584
5585 return 1;
5586}
5587
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005588static int fan_write(char *buf)
5589{
5590 char *cmd;
5591 int rc = 0;
5592
5593 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005594 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005595 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005596 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005597 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005598 fan_write_cmd_disable(cmd, &rc) ||
5599 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005600 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005601 fan_write_cmd_speed(cmd, &rc))
5602 )
5603 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005604 else if (!rc)
5605 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005606 }
5607
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005608 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005609}
5610
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005611static struct ibm_struct fan_driver_data = {
5612 .name = "fan",
5613 .read = fan_read,
5614 .write = fan_write,
5615 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005616};
5617
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005618/****************************************************************************
5619 ****************************************************************************
5620 *
5621 * Infrastructure
5622 *
5623 ****************************************************************************
5624 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005626/* sysfs name ---------------------------------------------------------- */
5627static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5628 struct device_attribute *attr,
5629 char *buf)
5630{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005631 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005632}
5633
5634static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5635 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5636
5637/* --------------------------------------------------------------------- */
5638
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005639/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005640static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005641
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005642/*
5643 * Module and infrastructure proble, init and exit handling
5644 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005646static int force_load;
5647
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005648#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005649static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005650{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005651 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005652
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005653 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005654}
5655#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5656
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005657static void ibm_exit(struct ibm_struct *ibm)
5658{
5659 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5660
5661 list_del_init(&ibm->all_drivers);
5662
5663 if (ibm->flags.acpi_notify_installed) {
5664 dbg_printk(TPACPI_DBG_EXIT,
5665 "%s: acpi_remove_notify_handler\n", ibm->name);
5666 BUG_ON(!ibm->acpi);
5667 acpi_remove_notify_handler(*ibm->acpi->handle,
5668 ibm->acpi->type,
5669 dispatch_acpi_notify);
5670 ibm->flags.acpi_notify_installed = 0;
5671 ibm->flags.acpi_notify_installed = 0;
5672 }
5673
5674 if (ibm->flags.proc_created) {
5675 dbg_printk(TPACPI_DBG_EXIT,
5676 "%s: remove_proc_entry\n", ibm->name);
5677 remove_proc_entry(ibm->name, proc_dir);
5678 ibm->flags.proc_created = 0;
5679 }
5680
5681 if (ibm->flags.acpi_driver_registered) {
5682 dbg_printk(TPACPI_DBG_EXIT,
5683 "%s: acpi_bus_unregister_driver\n", ibm->name);
5684 BUG_ON(!ibm->acpi);
5685 acpi_bus_unregister_driver(ibm->acpi->driver);
5686 kfree(ibm->acpi->driver);
5687 ibm->acpi->driver = NULL;
5688 ibm->flags.acpi_driver_registered = 0;
5689 }
5690
5691 if (ibm->flags.init_called && ibm->exit) {
5692 ibm->exit();
5693 ibm->flags.init_called = 0;
5694 }
5695
5696 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5697}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005698
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005699static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
5701 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005702 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 struct proc_dir_entry *entry;
5704
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005705 BUG_ON(ibm == NULL);
5706
5707 INIT_LIST_HEAD(&ibm->all_drivers);
5708
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005709 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 return 0;
5711
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005712 dbg_printk(TPACPI_DBG_INIT,
5713 "probing for %s\n", ibm->name);
5714
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005715 if (iibm->init) {
5716 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005717 if (ret > 0)
5718 return 0; /* probe failed */
5719 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005721
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005722 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 }
5724
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005725 if (ibm->acpi) {
5726 if (ibm->acpi->hid) {
5727 ret = register_tpacpi_subdriver(ibm);
5728 if (ret)
5729 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005730 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005731
5732 if (ibm->acpi->notify) {
5733 ret = setup_acpi_notify(ibm);
5734 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005735 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005736 ibm->name);
5737 ret = 0;
5738 goto err_out;
5739 }
5740 if (ret < 0)
5741 goto err_out;
5742 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005743 }
5744
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005745 dbg_printk(TPACPI_DBG_INIT,
5746 "%s installed\n", ibm->name);
5747
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005748 if (ibm->read) {
5749 entry = create_proc_entry(ibm->name,
5750 S_IFREG | S_IRUGO | S_IWUSR,
5751 proc_dir);
5752 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005753 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005754 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005755 ret = -ENODEV;
5756 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005757 }
5758 entry->owner = THIS_MODULE;
5759 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005760 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005761 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005762 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005763 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005766 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005769
5770err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005771 dbg_printk(TPACPI_DBG_INIT,
5772 "%s: at error exit path with result %d\n",
5773 ibm->name, ret);
5774
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005775 ibm_exit(ibm);
5776 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777}
5778
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005779/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005781static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005782{
Jeff Garzik18552562007-10-03 15:15:40 -04005783 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005784 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005785
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005786 if (!tp)
5787 return;
5788
5789 memset(tp, 0, sizeof(*tp));
5790
5791 if (dmi_name_in_vendors("IBM"))
5792 tp->vendor = PCI_VENDOR_ID_IBM;
5793 else if (dmi_name_in_vendors("LENOVO"))
5794 tp->vendor = PCI_VENDOR_ID_LENOVO;
5795 else
5796 return;
5797
5798 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5799 GFP_KERNEL);
5800 if (!tp->bios_version_str)
5801 return;
5802 tp->bios_model = tp->bios_version_str[0]
5803 | (tp->bios_version_str[1] << 8);
5804
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005805 /*
5806 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5807 * X32 or newer, all Z series; Some models must have an
5808 * up-to-date BIOS or they will not be detected.
5809 *
5810 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5811 */
5812 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005813 if (sscanf(dev->name,
5814 "IBM ThinkPad Embedded Controller -[%17c",
5815 ec_fw_string) == 1) {
5816 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5817 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005818
5819 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5820 tp->ec_model = ec_fw_string[0]
5821 | (ec_fw_string[1] << 8);
5822 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005823 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005824 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005825
5826 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5827 GFP_KERNEL);
5828 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5829 kfree(tp->model_str);
5830 tp->model_str = NULL;
5831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832}
5833
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005834static int __init probe_for_thinkpad(void)
5835{
5836 int is_thinkpad;
5837
5838 if (acpi_disabled)
5839 return -ENODEV;
5840
5841 /*
5842 * Non-ancient models have better DMI tagging, but very old models
5843 * don't.
5844 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005845 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005846
5847 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005848 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005849 if (!ec_handle) {
5850 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005851 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005852 "Not yet supported ThinkPad detected!\n");
5853 return -ENODEV;
5854 }
5855
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005856 /*
5857 * Risks a regression on very old machines, but reduces potential
5858 * false positives a damn great deal
5859 */
5860 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005861 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005862
5863 if (!is_thinkpad && !force_load)
5864 return -ENODEV;
5865
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005866 return 0;
5867}
5868
5869
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005870/* Module init, exit, parameters */
5871
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005872static struct ibm_init_struct ibms_init[] __initdata = {
5873 {
5874 .init = thinkpad_acpi_driver_init,
5875 .data = &thinkpad_acpi_driver_data,
5876 },
5877 {
5878 .init = hotkey_init,
5879 .data = &hotkey_driver_data,
5880 },
5881 {
5882 .init = bluetooth_init,
5883 .data = &bluetooth_driver_data,
5884 },
5885 {
5886 .init = wan_init,
5887 .data = &wan_driver_data,
5888 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005889#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005890 {
5891 .init = video_init,
5892 .data = &video_driver_data,
5893 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02005894#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005895 {
5896 .init = light_init,
5897 .data = &light_driver_data,
5898 },
5899#ifdef CONFIG_THINKPAD_ACPI_DOCK
5900 {
5901 .init = dock_init,
5902 .data = &dock_driver_data[0],
5903 },
5904 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005905 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005906 .data = &dock_driver_data[1],
5907 },
5908#endif
5909#ifdef CONFIG_THINKPAD_ACPI_BAY
5910 {
5911 .init = bay_init,
5912 .data = &bay_driver_data,
5913 },
5914#endif
5915 {
5916 .init = cmos_init,
5917 .data = &cmos_driver_data,
5918 },
5919 {
5920 .init = led_init,
5921 .data = &led_driver_data,
5922 },
5923 {
5924 .init = beep_init,
5925 .data = &beep_driver_data,
5926 },
5927 {
5928 .init = thermal_init,
5929 .data = &thermal_driver_data,
5930 },
5931 {
5932 .data = &ecdump_driver_data,
5933 },
5934 {
5935 .init = brightness_init,
5936 .data = &brightness_driver_data,
5937 },
5938 {
5939 .data = &volume_driver_data,
5940 },
5941 {
5942 .init = fan_init,
5943 .data = &fan_driver_data,
5944 },
5945};
5946
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005947static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5948{
5949 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005950 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005951
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005952 if (!kp || !kp->name || !val)
5953 return -EINVAL;
5954
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005955 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5956 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005957 WARN_ON(ibm == NULL);
5958
5959 if (!ibm || !ibm->name)
5960 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005961
5962 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5963 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005964 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005965 strcpy(ibms_init[i].param, val);
5966 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005967 return 0;
5968 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005969 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005970
5971 return -EINVAL;
5972}
5973
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005974module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005975MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005976 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005977
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005978module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005979MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005980
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005981module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005982MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005983 "Attempts to load the driver even on a "
5984 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005985
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005986module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005987MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005988 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005989
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005990module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005991MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005992 "Selects brightness control strategy: "
5993 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005994
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005995module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005996MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005997 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005998
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005999module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006000MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006001 "used for backwards compatibility with userspace, "
6002 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006003
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006004#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006005 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006006 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006007 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006008
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006009TPACPI_PARAM(hotkey);
6010TPACPI_PARAM(bluetooth);
6011TPACPI_PARAM(video);
6012TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006013#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006014TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006015#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006016#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006017TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006018#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006019TPACPI_PARAM(cmos);
6020TPACPI_PARAM(led);
6021TPACPI_PARAM(beep);
6022TPACPI_PARAM(ecdump);
6023TPACPI_PARAM(brightness);
6024TPACPI_PARAM(volume);
6025TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006026
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006027static void thinkpad_acpi_module_exit(void)
6028{
6029 struct ibm_struct *ibm, *itmp;
6030
6031 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6032
6033 list_for_each_entry_safe_reverse(ibm, itmp,
6034 &tpacpi_all_drivers,
6035 all_drivers) {
6036 ibm_exit(ibm);
6037 }
6038
6039 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6040
6041 if (tpacpi_inputdev) {
6042 if (tp_features.input_device_registered)
6043 input_unregister_device(tpacpi_inputdev);
6044 else
6045 input_free_device(tpacpi_inputdev);
6046 }
6047
6048 if (tpacpi_hwmon)
6049 hwmon_device_unregister(tpacpi_hwmon);
6050
6051 if (tp_features.sensors_pdev_attrs_registered)
6052 device_remove_file(&tpacpi_sensors_pdev->dev,
6053 &dev_attr_thinkpad_acpi_pdev_name);
6054 if (tpacpi_sensors_pdev)
6055 platform_device_unregister(tpacpi_sensors_pdev);
6056 if (tpacpi_pdev)
6057 platform_device_unregister(tpacpi_pdev);
6058
6059 if (tp_features.sensors_pdrv_attrs_registered)
6060 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6061 if (tp_features.platform_drv_attrs_registered)
6062 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6063
6064 if (tp_features.sensors_pdrv_registered)
6065 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6066
6067 if (tp_features.platform_drv_registered)
6068 platform_driver_unregister(&tpacpi_pdriver);
6069
6070 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006071 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006072
6073 kfree(thinkpad_id.bios_version_str);
6074 kfree(thinkpad_id.ec_version_str);
6075 kfree(thinkpad_id.model_str);
6076}
6077
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006078
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006079static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080{
6081 int ret, i;
6082
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006083 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6084
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006085 /* Parameter checking */
6086 if (hotkey_report_mode > 2)
6087 return -EINVAL;
6088
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006089 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006090
6091 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006092 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006093 if (ret) {
6094 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006095 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006098 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006099
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006100 TPACPI_ACPIHANDLE_INIT(ecrd);
6101 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006103 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006105 printk(TPACPI_ERR
6106 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006107 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 return -ENODEV;
6109 }
6110 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006111
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006112 ret = platform_driver_register(&tpacpi_pdriver);
6113 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006114 printk(TPACPI_ERR
6115 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006116 thinkpad_acpi_module_exit();
6117 return ret;
6118 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006119 tp_features.platform_drv_registered = 1;
6120
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006121 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6122 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006123 printk(TPACPI_ERR
6124 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006125 thinkpad_acpi_module_exit();
6126 return ret;
6127 }
6128 tp_features.sensors_pdrv_registered = 1;
6129
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006130 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006131 if (!ret) {
6132 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006133 ret = tpacpi_create_driver_attributes(
6134 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006135 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006136 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006137 printk(TPACPI_ERR
6138 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006139 thinkpad_acpi_module_exit();
6140 return ret;
6141 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006142 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006143
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006144
6145 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006146 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006147 NULL, 0);
6148 if (IS_ERR(tpacpi_pdev)) {
6149 ret = PTR_ERR(tpacpi_pdev);
6150 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006151 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006152 thinkpad_acpi_module_exit();
6153 return ret;
6154 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006155 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006156 TPACPI_HWMON_DRVR_NAME,
6157 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006158 if (IS_ERR(tpacpi_sensors_pdev)) {
6159 ret = PTR_ERR(tpacpi_sensors_pdev);
6160 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006161 printk(TPACPI_ERR
6162 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006163 thinkpad_acpi_module_exit();
6164 return ret;
6165 }
6166 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6167 &dev_attr_thinkpad_acpi_pdev_name);
6168 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006169 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006170 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006171 thinkpad_acpi_module_exit();
6172 return ret;
6173 }
6174 tp_features.sensors_pdev_attrs_registered = 1;
6175 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006176 if (IS_ERR(tpacpi_hwmon)) {
6177 ret = PTR_ERR(tpacpi_hwmon);
6178 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006179 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006180 thinkpad_acpi_module_exit();
6181 return ret;
6182 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006183 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006184 tpacpi_inputdev = input_allocate_device();
6185 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006186 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006187 thinkpad_acpi_module_exit();
6188 return -ENOMEM;
6189 } else {
6190 /* Prepare input device, but don't register */
6191 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006192 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006193 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006194 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6195 thinkpad_id.vendor :
6196 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006197 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6198 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6199 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006200 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6201 ret = ibm_init(&ibms_init[i]);
6202 if (ret >= 0 && *ibms_init[i].param)
6203 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006205 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 return ret;
6207 }
6208 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006209 ret = input_register_device(tpacpi_inputdev);
6210 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006211 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006212 thinkpad_acpi_module_exit();
6213 return ret;
6214 } else {
6215 tp_features.input_device_registered = 1;
6216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006218 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 return 0;
6220}
6221
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006222/* Please remove this in year 2009 */
6223MODULE_ALIAS("ibm_acpi");
6224
6225/*
6226 * DMI matching for module autoloading
6227 *
6228 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6229 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6230 *
6231 * Only models listed in thinkwiki will be supported, so add yours
6232 * if it is not there yet.
6233 */
6234#define IBM_BIOS_MODULE_ALIAS(__type) \
6235 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6236
6237/* Non-ancient thinkpads */
6238MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6239MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6240
6241/* Ancient thinkpad BIOSes have to be identified by
6242 * BIOS type or model number, and there are far less
6243 * BIOS types than model numbers... */
6244IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6245IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6246IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6247
6248MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006249MODULE_DESCRIPTION(TPACPI_DESC);
6250MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006251MODULE_LICENSE("GPL");
6252
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006253module_init(thinkpad_acpi_module_init);
6254module_exit(thinkpad_acpi_module_exit);