blob: c6c25a460c9cb4a7a5201961fc44724531ad792d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -03006 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -020024#define TPACPI_VERSION "0.18"
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -020025#define TPACPI_SYSFS_VERSION 0x020101
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
57#include <linux/kthread.h>
58#include <linux/freezer.h>
59#include <linux/delay.h>
60
61#include <linux/nvram.h>
62#include <linux/proc_fs.h>
63#include <linux/sysfs.h>
64#include <linux/backlight.h>
65#include <linux/fb.h>
66#include <linux/platform_device.h>
67#include <linux/hwmon.h>
68#include <linux/hwmon-sysfs.h>
69#include <linux/input.h>
70#include <asm/uaccess.h>
71
72#include <linux/dmi.h>
73#include <linux/jiffies.h>
74#include <linux/workqueue.h>
75
76#include <acpi/acpi_drivers.h>
77#include <acpi/acnamesp.h>
78
79#include <linux/pci_ids.h>
80
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020081
82/* ThinkPad CMOS commands */
83#define TP_CMOS_VOLUME_DOWN 0
84#define TP_CMOS_VOLUME_UP 1
85#define TP_CMOS_VOLUME_MUTE 2
86#define TP_CMOS_BRIGHTNESS_UP 4
87#define TP_CMOS_BRIGHTNESS_DOWN 5
88
89/* NVRAM Addresses */
90enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
96};
97
98/* NVRAM bit masks */
99enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
113};
114
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200115/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200116#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200117
118/* Input IDs */
119#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120#define TPACPI_HKEY_INPUT_VERSION 0x4101
121
122
123/****************************************************************************
124 * Main driver
125 */
126
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200127#define TPACPI_NAME "thinkpad"
128#define TPACPI_DESC "ThinkPad ACPI Extras"
129#define TPACPI_FILE TPACPI_NAME "_acpi"
130#define TPACPI_URL "http://ibm-acpi.sf.net/"
131#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200132
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200133#define TPACPI_PROC_DIR "ibm"
134#define TPACPI_ACPI_EVENT_PREFIX "ibm"
135#define TPACPI_DRVR_NAME TPACPI_FILE
136#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200137
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200138#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200139
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200140/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200141#define TPACPI_LOG TPACPI_FILE ": "
142#define TPACPI_ERR KERN_ERR TPACPI_LOG
143#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144#define TPACPI_INFO KERN_INFO TPACPI_LOG
145#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200146
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200147#define TPACPI_DBG_ALL 0xffff
148#define TPACPI_DBG_ALL 0xffff
149#define TPACPI_DBG_INIT 0x0001
150#define TPACPI_DBG_EXIT 0x0002
151#define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200155#ifdef CONFIG_THINKPAD_ACPI_DEBUG
156#define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158static const char *str_supported(int is_supported);
159#else
160#define vdbg_printk(a_dbg_level, format, arg...)
161#endif
162
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200163#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200166
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200167
168/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200169 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200170 */
171
172struct ibm_struct;
173
174struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
177
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
182};
183
184struct ibm_struct {
185 char *name;
186
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200191 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200192
193 struct list_head all_drivers;
194
195 struct tp_acpi_drv_struct *acpi;
196
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
204};
205
206struct ibm_init_struct {
207 char param[32];
208
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
211};
212
213static struct {
214#ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219#endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
224 u32 light:1;
225 u32 light_status:1;
226 u32 bright_16levels:1;
227 u32 wan:1;
228 u32 fan_ctrl_status_undef:1;
229 u32 input_device_registered:1;
230 u32 platform_drv_registered:1;
231 u32 platform_drv_attrs_registered:1;
232 u32 sensors_pdrv_registered:1;
233 u32 sensors_pdrv_attrs_registered:1;
234 u32 sensors_pdev_attrs_registered:1;
235 u32 hotkey_poll_active:1;
236} tp_features;
237
238struct thinkpad_id_data {
239 unsigned int vendor; /* ThinkPad vendor:
240 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
241
242 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
243 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
244
245 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
246 u16 ec_model;
247
248 char *model_str;
249};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200250static struct thinkpad_id_data thinkpad_id;
251
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300252static enum {
253 TPACPI_LIFE_INIT = 0,
254 TPACPI_LIFE_RUNNING,
255 TPACPI_LIFE_EXITING,
256} tpacpi_lifecycle;
257
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200258static int experimental;
259static u32 dbg_level;
260
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300261/****************************************************************************
262 ****************************************************************************
263 *
264 * ACPI Helpers and device model
265 *
266 ****************************************************************************
267 ****************************************************************************/
268
269/*************************************************************************
270 * ACPI basic handles
271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300273static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200275#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 static acpi_handle object##_handle; \
277 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400278 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 static char *object##_paths[] = { paths }
280
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200281TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400282 "\\_SB.PCI.ISA.EC", /* 570 */
283 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
284 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
285 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
286 "\\_SB.PCI0.ICH3.EC0", /* R31 */
287 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300288 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200290TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
291TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200293TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
294 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400295 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
296 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300297 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400298
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200299TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400300 "^HKEY", /* R30, R31 */
301 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300302 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400303
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400304
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300305/*************************************************************************
306 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200307 */
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static int acpi_evalf(acpi_handle handle,
310 void *res, char *method, char *fmt, ...)
311{
312 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400313 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200314 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400315 struct acpi_buffer result, *resultp;
316 union acpi_object out_obj;
317 acpi_status status;
318 va_list ap;
319 char res_type;
320 int success;
321 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200324 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326 }
327
328 if (*fmt == 'q') {
329 quiet = 1;
330 fmt++;
331 } else
332 quiet = 0;
333
334 res_type = *(fmt++);
335
336 params.count = 0;
337 params.pointer = &in_objs[0];
338
339 va_start(ap, fmt);
340 while (*fmt) {
341 char c = *(fmt++);
342 switch (c) {
343 case 'd': /* int */
344 in_objs[params.count].integer.value = va_arg(ap, int);
345 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
346 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400347 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200349 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 "with invalid format character '%c'\n", c);
351 return 0;
352 }
353 }
354 va_end(ap);
355
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400356 if (res_type != 'v') {
357 result.length = sizeof(out_obj);
358 result.pointer = &out_obj;
359 resultp = &result;
360 } else
361 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400363 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400366 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (res)
368 *(int *)res = out_obj.integer.value;
369 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
370 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400371 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 success = status == AE_OK;
373 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400374 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200376 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 "with invalid format character '%c'\n", res_type);
378 return 0;
379 }
380
381 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200382 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 method, fmt0, status);
384
385 return success;
386}
387
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200388static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300389{
390 int v;
391
392 if (ecrd_handle) {
393 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
394 return 0;
395 *p = v;
396 } else {
397 if (ec_read(i, p) < 0)
398 return 0;
399 }
400
401 return 1;
402}
403
404static int acpi_ec_write(int i, u8 v)
405{
406 if (ecwr_handle) {
407 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
408 return 0;
409 } else {
410 if (ec_write(i, v) < 0)
411 return 0;
412 }
413
414 return 1;
415}
416
417static int _sta(acpi_handle handle)
418{
419 int status;
420
421 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
422 status = 0;
423
424 return status;
425}
426
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300427static int issue_thinkpad_cmos_command(int cmos_cmd)
428{
429 if (!cmos_handle)
430 return -ENXIO;
431
432 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
433 return -EIO;
434
435 return 0;
436}
437
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300438/*************************************************************************
439 * ACPI device model
440 */
441
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200442#define TPACPI_ACPIHANDLE_INIT(object) \
443 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200444 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
445
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300446static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300447 acpi_handle *handle, acpi_handle parent,
448 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300449{
450 int i;
451 acpi_status status;
452
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300453 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
454 name);
455
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300456 for (i = 0; i < num_paths; i++) {
457 status = acpi_get_handle(parent, paths[i], handle);
458 if (ACPI_SUCCESS(status)) {
459 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300460 dbg_printk(TPACPI_DBG_INIT,
461 "Found ACPI handle %s for %s\n",
462 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300463 return;
464 }
465 }
466
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300467 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
468 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300469 *handle = NULL;
470}
471
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300472static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300473{
474 struct ibm_struct *ibm = data;
475
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300476 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
477 return;
478
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300479 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300480 return;
481
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300482 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300483}
484
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300485static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300486{
487 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300488 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300489
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300490 BUG_ON(!ibm->acpi);
491
492 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300493 return 0;
494
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300495 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300496 "setting up ACPI notify for %s\n", ibm->name);
497
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300498 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
499 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200500 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300501 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300502 return -ENODEV;
503 }
504
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300505 acpi_driver_data(ibm->acpi->device) = ibm;
506 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200507 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300508 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300509
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300510 status = acpi_install_notify_handler(*ibm->acpi->handle,
511 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300512 if (ACPI_FAILURE(status)) {
513 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200514 printk(TPACPI_NOTICE
515 "another device driver is already "
516 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300517 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200518 printk(TPACPI_ERR
519 "acpi_install_notify_handler(%s) failed: %d\n",
520 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300521 }
522 return -ENODEV;
523 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300524 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300525 return 0;
526}
527
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300528static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300529{
530 return 0;
531}
532
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300533static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300534{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300535 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300537 dbg_printk(TPACPI_DBG_INIT,
538 "registering %s as an ACPI driver\n", ibm->name);
539
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300540 BUG_ON(!ibm->acpi);
541
542 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
543 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200544 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300545 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300546 }
547
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200548 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300549 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200550
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300551 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300552
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300553 rc = acpi_bus_register_driver(ibm->acpi->driver);
554 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200555 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200556 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300557 kfree(ibm->acpi->driver);
558 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300559 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300560 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300561
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300562 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563}
564
565
566/****************************************************************************
567 ****************************************************************************
568 *
569 * Procfs Helpers
570 *
571 ****************************************************************************
572 ****************************************************************************/
573
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300574static int dispatch_procfs_read(char *page, char **start, off_t off,
575 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300576{
577 struct ibm_struct *ibm = data;
578 int len;
579
580 if (!ibm || !ibm->read)
581 return -EINVAL;
582
583 len = ibm->read(page);
584 if (len < 0)
585 return len;
586
587 if (len <= off + count)
588 *eof = 1;
589 *start = page + off;
590 len -= off;
591 if (len > count)
592 len = count;
593 if (len < 0)
594 len = 0;
595
596 return len;
597}
598
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300599static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200600 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300601 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300602{
603 struct ibm_struct *ibm = data;
604 char *kernbuf;
605 int ret;
606
607 if (!ibm || !ibm->write)
608 return -EINVAL;
609
610 kernbuf = kmalloc(count + 2, GFP_KERNEL);
611 if (!kernbuf)
612 return -ENOMEM;
613
614 if (copy_from_user(kernbuf, userbuf, count)) {
615 kfree(kernbuf);
616 return -EFAULT;
617 }
618
619 kernbuf[count] = 0;
620 strcat(kernbuf, ",");
621 ret = ibm->write(kernbuf);
622 if (ret == 0)
623 ret = count;
624
625 kfree(kernbuf);
626
627 return ret;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static char *next_cmd(char **cmds)
631{
632 char *start = *cmds;
633 char *end;
634
635 while ((end = strchr(start, ',')) && end == start)
636 start = end + 1;
637
638 if (!end)
639 return NULL;
640
641 *end = 0;
642 *cmds = end + 1;
643 return start;
644}
645
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300646
647/****************************************************************************
648 ****************************************************************************
649 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300650 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300651 *
652 ****************************************************************************
653 ****************************************************************************/
654
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300655static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300656static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700657static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300658static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300659static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200660static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300661
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200662static int tpacpi_suspend_handler(struct platform_device *pdev,
663 pm_message_t state)
664{
665 struct ibm_struct *ibm, *itmp;
666
667 list_for_each_entry_safe(ibm, itmp,
668 &tpacpi_all_drivers,
669 all_drivers) {
670 if (ibm->suspend)
671 (ibm->suspend)(state);
672 }
673
674 return 0;
675}
676
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300677static int tpacpi_resume_handler(struct platform_device *pdev)
678{
679 struct ibm_struct *ibm, *itmp;
680
681 list_for_each_entry_safe(ibm, itmp,
682 &tpacpi_all_drivers,
683 all_drivers) {
684 if (ibm->resume)
685 (ibm->resume)();
686 }
687
688 return 0;
689}
690
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300691static struct platform_driver tpacpi_pdriver = {
692 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200693 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300694 .owner = THIS_MODULE,
695 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200696 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300697 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300698};
699
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300700static struct platform_driver tpacpi_hwmon_pdriver = {
701 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200702 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300703 .owner = THIS_MODULE,
704 },
705};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300706
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300707/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300708 * sysfs support helpers
709 */
710
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200711struct attribute_set {
712 unsigned int members, max_members;
713 struct attribute_group group;
714};
715
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300716struct attribute_set_obj {
717 struct attribute_set s;
718 struct attribute *a;
719} __attribute__((packed));
720
721static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200722 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300723{
724 struct attribute_set_obj *sobj;
725
726 if (max_members == 0)
727 return NULL;
728
729 /* Allocates space for implicit NULL at the end too */
730 sobj = kzalloc(sizeof(struct attribute_set_obj) +
731 max_members * sizeof(struct attribute *),
732 GFP_KERNEL);
733 if (!sobj)
734 return NULL;
735 sobj->s.max_members = max_members;
736 sobj->s.group.attrs = &sobj->a;
737 sobj->s.group.name = name;
738
739 return &sobj->s;
740}
741
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200742#define destroy_attr_set(_set) \
743 kfree(_set);
744
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300745/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200746static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300747{
748 if (!s || !attr)
749 return -EINVAL;
750
751 if (s->members >= s->max_members)
752 return -ENOMEM;
753
754 s->group.attrs[s->members] = attr;
755 s->members++;
756
757 return 0;
758}
759
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200760static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300761 struct attribute **attr,
762 unsigned int count)
763{
764 int i, res;
765
766 for (i = 0; i < count; i++) {
767 res = add_to_attr_set(s, attr[i]);
768 if (res)
769 return res;
770 }
771
772 return 0;
773}
774
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200775static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300776{
777 sysfs_remove_group(kobj, &s->group);
778 destroy_attr_set(s);
779}
780
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200781#define register_attr_set_with_sysfs(_attr_set, _kobj) \
782 sysfs_create_group(_kobj, &_attr_set->group)
783
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300784static int parse_strtoul(const char *buf,
785 unsigned long max, unsigned long *value)
786{
787 char *endp;
788
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300789 while (*buf && isspace(*buf))
790 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300791 *value = simple_strtoul(buf, &endp, 0);
792 while (*endp && isspace(*endp))
793 endp++;
794 if (*endp || *value > max)
795 return -EINVAL;
796
797 return 0;
798}
799
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200800/*************************************************************************
801 * thinkpad-acpi driver attributes
802 */
803
804/* interface_version --------------------------------------------------- */
805static ssize_t tpacpi_driver_interface_version_show(
806 struct device_driver *drv,
807 char *buf)
808{
809 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
810}
811
812static DRIVER_ATTR(interface_version, S_IRUGO,
813 tpacpi_driver_interface_version_show, NULL);
814
815/* debug_level --------------------------------------------------------- */
816static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
817 char *buf)
818{
819 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
820}
821
822static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
823 const char *buf, size_t count)
824{
825 unsigned long t;
826
827 if (parse_strtoul(buf, 0xffff, &t))
828 return -EINVAL;
829
830 dbg_level = t;
831
832 return count;
833}
834
835static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
836 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
837
838/* version ------------------------------------------------------------- */
839static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
840 char *buf)
841{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200842 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
843 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200844}
845
846static DRIVER_ATTR(version, S_IRUGO,
847 tpacpi_driver_version_show, NULL);
848
849/* --------------------------------------------------------------------- */
850
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200851static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200852 &driver_attr_debug_level, &driver_attr_version,
853 &driver_attr_interface_version,
854};
855
856static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
857{
858 int i, res;
859
860 i = 0;
861 res = 0;
862 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
863 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
864 i++;
865 }
866
867 return res;
868}
869
870static void tpacpi_remove_driver_attributes(struct device_driver *drv)
871{
872 int i;
873
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200874 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200875 driver_remove_file(drv, tpacpi_driver_attributes[i]);
876}
877
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300878/****************************************************************************
879 ****************************************************************************
880 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300881 * Subdrivers
882 *
883 ****************************************************************************
884 ****************************************************************************/
885
886/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300887 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300888 */
889
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300890static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200892 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
893 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200895 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300896 (thinkpad_id.bios_version_str) ?
897 thinkpad_id.bios_version_str : "unknown",
898 (thinkpad_id.ec_version_str) ?
899 thinkpad_id.ec_version_str : "unknown");
900
901 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200902 printk(TPACPI_INFO "%s %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300903 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
904 "IBM" : ((thinkpad_id.vendor ==
905 PCI_VENDOR_ID_LENOVO) ?
906 "Lenovo" : "Unknown vendor"),
907 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return 0;
910}
911
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300912static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 int len = 0;
915
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200916 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
917 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 return len;
920}
921
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300922static struct ibm_struct thinkpad_acpi_driver_data = {
923 .name = "driver",
924 .read = thinkpad_acpi_driver_read,
925};
926
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300927/*************************************************************************
928 * Hotkey subdriver
929 */
930
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200931enum { /* hot key scan codes (derived from ACPI DSDT) */
932 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
933 TP_ACPI_HOTKEYSCAN_FNF2,
934 TP_ACPI_HOTKEYSCAN_FNF3,
935 TP_ACPI_HOTKEYSCAN_FNF4,
936 TP_ACPI_HOTKEYSCAN_FNF5,
937 TP_ACPI_HOTKEYSCAN_FNF6,
938 TP_ACPI_HOTKEYSCAN_FNF7,
939 TP_ACPI_HOTKEYSCAN_FNF8,
940 TP_ACPI_HOTKEYSCAN_FNF9,
941 TP_ACPI_HOTKEYSCAN_FNF10,
942 TP_ACPI_HOTKEYSCAN_FNF11,
943 TP_ACPI_HOTKEYSCAN_FNF12,
944 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
945 TP_ACPI_HOTKEYSCAN_FNINSERT,
946 TP_ACPI_HOTKEYSCAN_FNDELETE,
947 TP_ACPI_HOTKEYSCAN_FNHOME,
948 TP_ACPI_HOTKEYSCAN_FNEND,
949 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
950 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
951 TP_ACPI_HOTKEYSCAN_FNSPACE,
952 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
953 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
954 TP_ACPI_HOTKEYSCAN_MUTE,
955 TP_ACPI_HOTKEYSCAN_THINKPAD,
956};
957
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200958enum { /* Keys available through NVRAM polling */
959 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
960 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
961};
962
963enum { /* Positions of some of the keys in hotkey masks */
964 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
965 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
966 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
967 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
968 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
969 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
970 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
971 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
972 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
973 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
974 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
975};
976
977enum { /* NVRAM to ACPI HKEY group map */
978 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
979 TP_ACPI_HKEY_ZOOM_MASK |
980 TP_ACPI_HKEY_DISPSWTCH_MASK |
981 TP_ACPI_HKEY_HIBERNATE_MASK,
982 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
983 TP_ACPI_HKEY_BRGHTDWN_MASK,
984 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
985 TP_ACPI_HKEY_VOLDWN_MASK |
986 TP_ACPI_HKEY_MUTE_MASK,
987};
988
989#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
990struct tp_nvram_state {
991 u16 thinkpad_toggle:1;
992 u16 zoom_toggle:1;
993 u16 display_toggle:1;
994 u16 thinklight_toggle:1;
995 u16 hibernate_toggle:1;
996 u16 displayexp_toggle:1;
997 u16 display_state:1;
998 u16 brightness_toggle:1;
999 u16 volume_toggle:1;
1000 u16 mute:1;
1001
1002 u8 brightness_level;
1003 u8 volume_level;
1004};
1005
1006static struct task_struct *tpacpi_hotkey_task;
1007static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1008static int hotkey_poll_freq = 10; /* Hz */
1009static struct mutex hotkey_thread_mutex;
1010static struct mutex hotkey_thread_data_mutex;
1011static unsigned int hotkey_config_change;
1012
1013#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1014
1015#define hotkey_source_mask 0U
1016
1017#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1018
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001019static struct mutex hotkey_mutex;
1020
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001021static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001022static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001023static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001024static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001025static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001026
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001027static unsigned int hotkey_report_mode;
1028
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001029static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001030
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001031static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001032
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001033#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1034#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001035 do { \
1036 mutex_lock(&hotkey_thread_data_mutex); \
1037 hotkey_config_change++; \
1038 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001039#define HOTKEY_CONFIG_CRITICAL_END \
1040 mutex_unlock(&hotkey_thread_data_mutex);
1041#else
1042#define HOTKEY_CONFIG_CRITICAL_START
1043#define HOTKEY_CONFIG_CRITICAL_END
1044#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1045
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001046static int hotkey_get_wlsw(int *status)
1047{
1048 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1049 return -EIO;
1050 return 0;
1051}
1052
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001053/*
1054 * Call with hotkey_mutex held
1055 */
1056static int hotkey_mask_get(void)
1057{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001058 u32 m = 0;
1059
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001060 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001061 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001062 return -EIO;
1063 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001064 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001065
1066 return 0;
1067}
1068
1069/*
1070 * Call with hotkey_mutex held
1071 */
1072static int hotkey_mask_set(u32 mask)
1073{
1074 int i;
1075 int rc = 0;
1076
1077 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001078 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001079 for (i = 0; i < 32; i++) {
1080 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001081 /* enable in firmware mask only keys not in NVRAM
1082 * mode, but enable the key in the cached hotkey_mask
1083 * regardless of mode, or the key will end up
1084 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001085 if (!acpi_evalf(hkey_handle,
1086 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001087 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001088 rc = -EIO;
1089 break;
1090 } else {
1091 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1092 }
1093 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001094 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001095
1096 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001097 if (!hotkey_mask_get() && !rc &&
1098 (hotkey_mask & ~hotkey_source_mask) !=
1099 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001100 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001101 "requested hot key mask 0x%08x, but "
1102 "firmware forced it to 0x%08x\n",
1103 mask, hotkey_mask);
1104 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001105 } else {
1106#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1107 HOTKEY_CONFIG_CRITICAL_START
1108 hotkey_mask = mask & hotkey_source_mask;
1109 HOTKEY_CONFIG_CRITICAL_END
1110 hotkey_mask_get();
1111 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001112 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001113 "requested hot key mask 0x%08x, "
1114 "forced to 0x%08x (NVRAM poll mask is "
1115 "0x%08x): no firmware mask support\n",
1116 mask, hotkey_mask, hotkey_source_mask);
1117 }
1118#else
1119 hotkey_mask_get();
1120 rc = -ENXIO;
1121#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001122 }
1123
1124 return rc;
1125}
1126
1127static int hotkey_status_get(int *status)
1128{
1129 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1130 return -EIO;
1131
1132 return 0;
1133}
1134
1135static int hotkey_status_set(int status)
1136{
1137 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1138 return -EIO;
1139
1140 return 0;
1141}
1142
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001143static void tpacpi_input_send_radiosw(void)
1144{
1145 int wlsw;
1146
1147 mutex_lock(&tpacpi_inputdev_send_mutex);
1148
1149 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1150 input_report_switch(tpacpi_inputdev,
1151 SW_RADIO, !!wlsw);
1152 input_sync(tpacpi_inputdev);
1153 }
1154
1155 mutex_unlock(&tpacpi_inputdev_send_mutex);
1156}
1157
1158static void tpacpi_input_send_key(unsigned int scancode)
1159{
1160 unsigned int keycode;
1161
1162 keycode = hotkey_keycode_map[scancode];
1163
1164 if (keycode != KEY_RESERVED) {
1165 mutex_lock(&tpacpi_inputdev_send_mutex);
1166
1167 input_report_key(tpacpi_inputdev, keycode, 1);
1168 if (keycode == KEY_UNKNOWN)
1169 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1170 scancode);
1171 input_sync(tpacpi_inputdev);
1172
1173 input_report_key(tpacpi_inputdev, keycode, 0);
1174 if (keycode == KEY_UNKNOWN)
1175 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1176 scancode);
1177 input_sync(tpacpi_inputdev);
1178
1179 mutex_unlock(&tpacpi_inputdev_send_mutex);
1180 }
1181}
1182
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001183#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1184static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1185
1186static void tpacpi_hotkey_send_key(unsigned int scancode)
1187{
1188 tpacpi_input_send_key(scancode);
1189 if (hotkey_report_mode < 2) {
1190 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1191 0x80, 0x1001 + scancode);
1192 }
1193}
1194
1195static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1196{
1197 u8 d;
1198
1199 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1200 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1201 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1202 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1203 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1204 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1205 }
1206 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1207 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1208 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1209 }
1210 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1211 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1212 n->displayexp_toggle =
1213 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1214 }
1215 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1216 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1217 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1218 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1219 n->brightness_toggle =
1220 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1221 }
1222 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1223 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1224 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1225 >> TP_NVRAM_POS_LEVEL_VOLUME;
1226 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1227 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1228 }
1229}
1230
1231#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001232 do { \
1233 if ((mask & (1 << __scancode)) && \
1234 oldn->__member != newn->__member) \
1235 tpacpi_hotkey_send_key(__scancode); \
1236 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001237
1238#define TPACPI_MAY_SEND_KEY(__scancode) \
1239 do { if (mask & (1 << __scancode)) \
1240 tpacpi_hotkey_send_key(__scancode); } while (0)
1241
1242static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001243 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001244 u32 mask)
1245{
1246 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1247 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1248 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1249 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1250
1251 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1252
1253 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1254
1255 /* handle volume */
1256 if (oldn->volume_toggle != newn->volume_toggle) {
1257 if (oldn->mute != newn->mute) {
1258 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1259 }
1260 if (oldn->volume_level > newn->volume_level) {
1261 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1262 } else if (oldn->volume_level < newn->volume_level) {
1263 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1264 } else if (oldn->mute == newn->mute) {
1265 /* repeated key presses that didn't change state */
1266 if (newn->mute) {
1267 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1268 } else if (newn->volume_level != 0) {
1269 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1270 } else {
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1272 }
1273 }
1274 }
1275
1276 /* handle brightness */
1277 if (oldn->brightness_toggle != newn->brightness_toggle) {
1278 if (oldn->brightness_level < newn->brightness_level) {
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1280 } else if (oldn->brightness_level > newn->brightness_level) {
1281 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1282 } else {
1283 /* repeated key presses that didn't change state */
1284 if (newn->brightness_level != 0) {
1285 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1286 } else {
1287 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1288 }
1289 }
1290 }
1291}
1292
1293#undef TPACPI_COMPARE_KEY
1294#undef TPACPI_MAY_SEND_KEY
1295
1296static int hotkey_kthread(void *data)
1297{
1298 struct tp_nvram_state s[2];
1299 u32 mask;
1300 unsigned int si, so;
1301 unsigned long t;
1302 unsigned int change_detector, must_reset;
1303
1304 mutex_lock(&hotkey_thread_mutex);
1305
1306 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1307 goto exit;
1308
1309 set_freezable();
1310
1311 so = 0;
1312 si = 1;
1313 t = 0;
1314
1315 /* Initial state for compares */
1316 mutex_lock(&hotkey_thread_data_mutex);
1317 change_detector = hotkey_config_change;
1318 mask = hotkey_source_mask & hotkey_mask;
1319 mutex_unlock(&hotkey_thread_data_mutex);
1320 hotkey_read_nvram(&s[so], mask);
1321
1322 while (!kthread_should_stop() && hotkey_poll_freq) {
1323 if (t == 0)
1324 t = 1000/hotkey_poll_freq;
1325 t = msleep_interruptible(t);
1326 if (unlikely(kthread_should_stop()))
1327 break;
1328 must_reset = try_to_freeze();
1329 if (t > 0 && !must_reset)
1330 continue;
1331
1332 mutex_lock(&hotkey_thread_data_mutex);
1333 if (must_reset || hotkey_config_change != change_detector) {
1334 /* forget old state on thaw or config change */
1335 si = so;
1336 t = 0;
1337 change_detector = hotkey_config_change;
1338 }
1339 mask = hotkey_source_mask & hotkey_mask;
1340 mutex_unlock(&hotkey_thread_data_mutex);
1341
1342 if (likely(mask)) {
1343 hotkey_read_nvram(&s[si], mask);
1344 if (likely(si != so)) {
1345 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001346 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001347 }
1348 }
1349
1350 so = si;
1351 si ^= 1;
1352 }
1353
1354exit:
1355 mutex_unlock(&hotkey_thread_mutex);
1356 return 0;
1357}
1358
1359static void hotkey_poll_stop_sync(void)
1360{
1361 if (tpacpi_hotkey_task) {
1362 if (frozen(tpacpi_hotkey_task) ||
1363 freezing(tpacpi_hotkey_task))
1364 thaw_process(tpacpi_hotkey_task);
1365
1366 kthread_stop(tpacpi_hotkey_task);
1367 tpacpi_hotkey_task = NULL;
1368 mutex_lock(&hotkey_thread_mutex);
1369 /* at this point, the thread did exit */
1370 mutex_unlock(&hotkey_thread_mutex);
1371 }
1372}
1373
1374/* call with hotkey_mutex held */
1375static void hotkey_poll_setup(int may_warn)
1376{
1377 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1378 hotkey_poll_freq > 0 &&
1379 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1380 if (!tpacpi_hotkey_task) {
1381 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001382 NULL,
1383 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001384 if (IS_ERR(tpacpi_hotkey_task)) {
1385 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001386 printk(TPACPI_ERR
1387 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001388 "for hotkey polling\n");
1389 }
1390 }
1391 } else {
1392 hotkey_poll_stop_sync();
1393 if (may_warn &&
1394 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001395 printk(TPACPI_NOTICE
1396 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001397 "which is currently disabled\n",
1398 hotkey_source_mask);
1399 }
1400 }
1401}
1402
1403static void hotkey_poll_setup_safe(int may_warn)
1404{
1405 mutex_lock(&hotkey_mutex);
1406 hotkey_poll_setup(may_warn);
1407 mutex_unlock(&hotkey_mutex);
1408}
1409
1410static int hotkey_inputdev_open(struct input_dev *dev)
1411{
1412 switch (tpacpi_lifecycle) {
1413 case TPACPI_LIFE_INIT:
1414 /*
1415 * hotkey_init will call hotkey_poll_setup_safe
1416 * at the appropriate moment
1417 */
1418 return 0;
1419 case TPACPI_LIFE_EXITING:
1420 return -EBUSY;
1421 case TPACPI_LIFE_RUNNING:
1422 hotkey_poll_setup_safe(0);
1423 return 0;
1424 }
1425
1426 /* Should only happen if tpacpi_lifecycle is corrupt */
1427 BUG();
1428 return -EBUSY;
1429}
1430
1431static void hotkey_inputdev_close(struct input_dev *dev)
1432{
1433 /* disable hotkey polling when possible */
1434 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1435 hotkey_poll_setup_safe(0);
1436}
1437#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1438
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001439/* sysfs hotkey enable ------------------------------------------------- */
1440static ssize_t hotkey_enable_show(struct device *dev,
1441 struct device_attribute *attr,
1442 char *buf)
1443{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001444 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001445
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001446 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001447 if (res)
1448 return res;
1449
1450 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1451}
1452
1453static ssize_t hotkey_enable_store(struct device *dev,
1454 struct device_attribute *attr,
1455 const char *buf, size_t count)
1456{
1457 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001458 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001459
1460 if (parse_strtoul(buf, 1, &t))
1461 return -EINVAL;
1462
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001463 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001464
1465 return (res) ? res : count;
1466}
1467
1468static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001469 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001470 hotkey_enable_show, hotkey_enable_store);
1471
1472/* sysfs hotkey mask --------------------------------------------------- */
1473static ssize_t hotkey_mask_show(struct device *dev,
1474 struct device_attribute *attr,
1475 char *buf)
1476{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001477 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001478
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001479 if (mutex_lock_interruptible(&hotkey_mutex))
1480 return -ERESTARTSYS;
1481 res = hotkey_mask_get();
1482 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001483
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001484 return (res)?
1485 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001486}
1487
1488static ssize_t hotkey_mask_store(struct device *dev,
1489 struct device_attribute *attr,
1490 const char *buf, size_t count)
1491{
1492 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001493 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001494
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001495 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001496 return -EINVAL;
1497
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001498 if (mutex_lock_interruptible(&hotkey_mutex))
1499 return -ERESTARTSYS;
1500
1501 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001502
1503#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1504 hotkey_poll_setup(1);
1505#endif
1506
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001507 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001508
1509 return (res) ? res : count;
1510}
1511
1512static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001513 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001514 hotkey_mask_show, hotkey_mask_store);
1515
1516/* sysfs hotkey bios_enabled ------------------------------------------- */
1517static ssize_t hotkey_bios_enabled_show(struct device *dev,
1518 struct device_attribute *attr,
1519 char *buf)
1520{
1521 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1522}
1523
1524static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001525 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001526
1527/* sysfs hotkey bios_mask ---------------------------------------------- */
1528static ssize_t hotkey_bios_mask_show(struct device *dev,
1529 struct device_attribute *attr,
1530 char *buf)
1531{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001532 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001533}
1534
1535static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001536 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001537
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001538/* sysfs hotkey all_mask ----------------------------------------------- */
1539static ssize_t hotkey_all_mask_show(struct device *dev,
1540 struct device_attribute *attr,
1541 char *buf)
1542{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001543 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1544 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001545}
1546
1547static struct device_attribute dev_attr_hotkey_all_mask =
1548 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1549
1550/* sysfs hotkey recommended_mask --------------------------------------- */
1551static ssize_t hotkey_recommended_mask_show(struct device *dev,
1552 struct device_attribute *attr,
1553 char *buf)
1554{
1555 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001556 (hotkey_all_mask | hotkey_source_mask)
1557 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001558}
1559
1560static struct device_attribute dev_attr_hotkey_recommended_mask =
1561 __ATTR(hotkey_recommended_mask, S_IRUGO,
1562 hotkey_recommended_mask_show, NULL);
1563
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001564#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1565
1566/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1567static ssize_t hotkey_source_mask_show(struct device *dev,
1568 struct device_attribute *attr,
1569 char *buf)
1570{
1571 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1572}
1573
1574static ssize_t hotkey_source_mask_store(struct device *dev,
1575 struct device_attribute *attr,
1576 const char *buf, size_t count)
1577{
1578 unsigned long t;
1579
1580 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1581 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1582 return -EINVAL;
1583
1584 if (mutex_lock_interruptible(&hotkey_mutex))
1585 return -ERESTARTSYS;
1586
1587 HOTKEY_CONFIG_CRITICAL_START
1588 hotkey_source_mask = t;
1589 HOTKEY_CONFIG_CRITICAL_END
1590
1591 hotkey_poll_setup(1);
1592
1593 mutex_unlock(&hotkey_mutex);
1594
1595 return count;
1596}
1597
1598static struct device_attribute dev_attr_hotkey_source_mask =
1599 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1600 hotkey_source_mask_show, hotkey_source_mask_store);
1601
1602/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1603static ssize_t hotkey_poll_freq_show(struct device *dev,
1604 struct device_attribute *attr,
1605 char *buf)
1606{
1607 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1608}
1609
1610static ssize_t hotkey_poll_freq_store(struct device *dev,
1611 struct device_attribute *attr,
1612 const char *buf, size_t count)
1613{
1614 unsigned long t;
1615
1616 if (parse_strtoul(buf, 25, &t))
1617 return -EINVAL;
1618
1619 if (mutex_lock_interruptible(&hotkey_mutex))
1620 return -ERESTARTSYS;
1621
1622 hotkey_poll_freq = t;
1623
1624 hotkey_poll_setup(1);
1625 mutex_unlock(&hotkey_mutex);
1626
1627 return count;
1628}
1629
1630static struct device_attribute dev_attr_hotkey_poll_freq =
1631 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1632 hotkey_poll_freq_show, hotkey_poll_freq_store);
1633
1634#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1635
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001636/* sysfs hotkey radio_sw ----------------------------------------------- */
1637static ssize_t hotkey_radio_sw_show(struct device *dev,
1638 struct device_attribute *attr,
1639 char *buf)
1640{
1641 int res, s;
1642 res = hotkey_get_wlsw(&s);
1643 if (res < 0)
1644 return res;
1645
1646 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1647}
1648
1649static struct device_attribute dev_attr_hotkey_radio_sw =
1650 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1651
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001652/* sysfs hotkey report_mode -------------------------------------------- */
1653static ssize_t hotkey_report_mode_show(struct device *dev,
1654 struct device_attribute *attr,
1655 char *buf)
1656{
1657 return snprintf(buf, PAGE_SIZE, "%d\n",
1658 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1659}
1660
1661static struct device_attribute dev_attr_hotkey_report_mode =
1662 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1663
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001664/* --------------------------------------------------------------------- */
1665
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001666static struct attribute *hotkey_attributes[] __initdata = {
1667 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001668 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001669 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001670#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1671 &dev_attr_hotkey_mask.attr,
1672 &dev_attr_hotkey_all_mask.attr,
1673 &dev_attr_hotkey_recommended_mask.attr,
1674 &dev_attr_hotkey_source_mask.attr,
1675 &dev_attr_hotkey_poll_freq.attr,
1676#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001677};
1678
1679static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001680 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001681#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1682 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001683 &dev_attr_hotkey_all_mask.attr,
1684 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001685#endif
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001686};
1687
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001688static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001689{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001690 /* Requirements for changing the default keymaps:
1691 *
1692 * 1. Many of the keys are mapped to KEY_RESERVED for very
1693 * good reasons. Do not change them unless you have deep
1694 * knowledge on the IBM and Lenovo ThinkPad firmware for
1695 * the various ThinkPad models. The driver behaves
1696 * differently for KEY_RESERVED: such keys have their
1697 * hot key mask *unset* in mask_recommended, and also
1698 * in the initial hot key mask programmed into the
1699 * firmware at driver load time, which means the firm-
1700 * ware may react very differently if you change them to
1701 * something else;
1702 *
1703 * 2. You must be subscribed to the linux-thinkpad and
1704 * ibm-acpi-devel mailing lists, and you should read the
1705 * list archives since 2007 if you want to change the
1706 * keymaps. This requirement exists so that you will
1707 * know the past history of problems with the thinkpad-
1708 * acpi driver keymaps, and also that you will be
1709 * listening to any bug reports;
1710 *
1711 * 3. Do not send thinkpad-acpi specific patches directly to
1712 * for merging, *ever*. Send them to the linux-acpi
1713 * mailinglist for comments. Merging is to be done only
1714 * through acpi-test and the ACPI maintainer.
1715 *
1716 * If the above is too much to ask, don't change the keymap.
1717 * Ask the thinkpad-acpi maintainer to do it, instead.
1718 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001719 static u16 ibm_keycode_map[] __initdata = {
1720 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1721 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1722 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1723 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001724
1725 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001726 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1727 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1728 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001729
1730 /* brightness: firmware always reacts to them, unless
1731 * X.org did some tricks in the radeon BIOS scratch
1732 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001733 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001734 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001735
1736 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001737 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001738
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001739 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1740 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001741
1742 /* Volume: firmware always react to it and reprograms
1743 * the built-in *extra* mixer. Never map it to control
1744 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001745 KEY_RESERVED, /* 0x14: VOLUME UP */
1746 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1747 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001748
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001749 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001750
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001751 /* (assignments unknown, please report if found) */
1752 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1753 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1754 };
1755 static u16 lenovo_keycode_map[] __initdata = {
1756 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1757 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1758 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1759 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001760
1761 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001762 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1763 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1764 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001765
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001766 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001767 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001768
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001769 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001770
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001771 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1772 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001773
1774 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1775 * react to it and reprograms the built-in *extra* mixer.
1776 * Never map it to control another mixer by default.
1777 *
1778 * T60?, T61, R60?, R61: firmware and EC tries to send
1779 * these over the regular keyboard, so these are no-ops,
1780 * but there are still weird bugs re. MUTE, so do not
1781 * change unless you get test reports from all Lenovo
1782 * models. May cause the BIOS to interfere with the
1783 * HDA mixer.
1784 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001785 KEY_RESERVED, /* 0x14: VOLUME UP */
1786 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1787 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001788
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001789 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001790
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001791 /* (assignments unknown, please report if found) */
1792 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1793 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1794 };
1795
1796#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1797#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1798#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1799
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001800 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001801 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001802 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001803
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001804 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1805
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001806 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001807 BUG_ON(tpacpi_inputdev->open != NULL ||
1808 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001809
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001810 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001811 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001812
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001813#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1814 mutex_init(&hotkey_thread_mutex);
1815 mutex_init(&hotkey_thread_data_mutex);
1816#endif
1817
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001818 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001819 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001820
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001821 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001822 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001823
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001824 if (tp_features.hotkey) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001825 hotkey_dev_attributes = create_attr_set(10, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001826 if (!hotkey_dev_attributes)
1827 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001828 res = add_many_to_attr_set(hotkey_dev_attributes,
1829 hotkey_attributes,
1830 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001831 if (res)
1832 return res;
1833
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001834 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001835 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1836 for HKEY interface version 0x100 */
1837 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1838 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001839 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001840 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001841 printk(TPACPI_ERR "please report this to %s\n",
1842 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001843 } else {
1844 /*
1845 * MHKV 0x100 in A31, R40, R40e,
1846 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001847 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001848 tp_features.hotkey_mask = 1;
1849 }
1850 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001851
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001852 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001853 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001854
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001855 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001856 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001857 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001858 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001859 "missing MHKA handler, "
1860 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001861 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001862 /* FN+F12, FN+F4, FN+F3 */
1863 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001864 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001865 }
1866
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001867 /* hotkey_source_mask *must* be zero for
1868 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001869 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001870 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001871 res = hotkey_mask_get();
1872 hotkey_orig_mask = hotkey_mask;
1873 if (!res) {
1874 res = add_many_to_attr_set(
1875 hotkey_dev_attributes,
1876 hotkey_mask_attributes,
1877 ARRAY_SIZE(hotkey_mask_attributes));
1878 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001879 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001880
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001881#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1882 if (tp_features.hotkey_mask) {
1883 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1884 & ~hotkey_all_mask;
1885 } else {
1886 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1887 }
1888
1889 vdbg_printk(TPACPI_DBG_INIT,
1890 "hotkey source mask 0x%08x, polling freq %d\n",
1891 hotkey_source_mask, hotkey_poll_freq);
1892#endif
1893
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001894 /* Not all thinkpads have a hardware radio switch */
1895 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1896 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001897 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001898 "radio switch found; radios are %s\n",
1899 enabled(status, 0));
1900 res = add_to_attr_set(hotkey_dev_attributes,
1901 &dev_attr_hotkey_radio_sw.attr);
1902 }
1903
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001904 if (!res)
1905 res = register_attr_set_with_sysfs(
1906 hotkey_dev_attributes,
1907 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001908 if (res)
1909 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001910
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001911 /* Set up key map */
1912
1913 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1914 GFP_KERNEL);
1915 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001916 printk(TPACPI_ERR
1917 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001918 return -ENOMEM;
1919 }
1920
1921 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1922 dbg_printk(TPACPI_DBG_INIT,
1923 "using Lenovo default hot key map\n");
1924 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1925 TPACPI_HOTKEY_MAP_SIZE);
1926 } else {
1927 dbg_printk(TPACPI_DBG_INIT,
1928 "using IBM default hot key map\n");
1929 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1930 TPACPI_HOTKEY_MAP_SIZE);
1931 }
1932
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001933 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1934 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1935 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001936 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1937 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1938 tpacpi_inputdev->keycode = hotkey_keycode_map;
1939 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001940 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1941 set_bit(hotkey_keycode_map[i],
1942 tpacpi_inputdev->keybit);
1943 } else {
1944 if (i < sizeof(hotkey_reserved_mask)*8)
1945 hotkey_reserved_mask |= 1 << i;
1946 }
1947 }
1948
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001949 if (tp_features.hotkey_wlsw) {
1950 set_bit(EV_SW, tpacpi_inputdev->evbit);
1951 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1952 }
1953
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001954 dbg_printk(TPACPI_DBG_INIT,
1955 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001956 res = hotkey_status_set(1);
1957 if (res)
1958 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001959 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1960 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001961 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001962 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001963 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001964
1965 dbg_printk(TPACPI_DBG_INIT,
1966 "legacy hot key reporting over procfs %s\n",
1967 (hotkey_report_mode < 2) ?
1968 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001969
1970#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1971 tpacpi_inputdev->open = &hotkey_inputdev_open;
1972 tpacpi_inputdev->close = &hotkey_inputdev_close;
1973
1974 hotkey_poll_setup_safe(1);
1975#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001976 }
1977
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001978 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001979}
1980
1981static void hotkey_exit(void)
1982{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001983#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1984 hotkey_poll_stop_sync();
1985#endif
1986
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001987 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001988 dbg_printk(TPACPI_DBG_EXIT,
1989 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001990 /* no short-circuit boolean operator below! */
1991 if ((hotkey_mask_set(hotkey_orig_mask) |
1992 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001993 printk(TPACPI_ERR
1994 "failed to restore hot key mask "
1995 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001996 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001997
1998 if (hotkey_dev_attributes) {
1999 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2000 hotkey_dev_attributes = NULL;
2001 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002002}
2003
2004static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2005{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002006 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002007 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002008 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002009 int ignore_acpi_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002010
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002011 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002012 printk(TPACPI_ERR
2013 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002014 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002015 acpi_bus_generate_netlink_event(
2016 ibm->acpi->device->pnp.device_class,
2017 ibm->acpi->device->dev.bus_id,
2018 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002019 return;
2020 }
2021
2022 while (1) {
2023 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002024 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002025 return;
2026 }
2027
2028 if (hkey == 0) {
2029 /* queue empty */
2030 return;
2031 }
2032
2033 send_acpi_ev = 0;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002034 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002035
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002036 switch (hkey >> 12) {
2037 case 1:
2038 /* 0x1000-0x1FFF: key presses */
2039 scancode = hkey & 0xfff;
2040 if (scancode > 0 && scancode < 0x21) {
2041 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002042 if (!(hotkey_source_mask & (1 << scancode))) {
2043 tpacpi_input_send_key(scancode);
2044 } else {
2045 ignore_acpi_ev = 1;
2046 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002047 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002048 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002049 "hotkey 0x%04x out of range "
2050 "for keyboard map\n", hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002051 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002052 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002053 break;
2054 case 5:
2055 /* 0x5000-0x5FFF: LID */
2056 /* we don't handle it through this path, just
2057 * eat up known LID events */
2058 if (hkey != 0x5001 && hkey != 0x5002) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002059 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002060 "unknown LID-related HKEY event: "
2061 "0x%04x\n", hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002062 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002063 } else {
2064 ignore_acpi_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002065 }
2066 break;
2067 case 7:
2068 /* 0x7000-0x7FFF: misc */
2069 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2070 tpacpi_input_send_radiosw();
2071 break;
2072 }
2073 /* fallthrough to default */
2074 default:
2075 /* case 2: dock-related */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002076 /* 0x2305 - T43 waking up due to bay lever
2077 * eject while aslept */
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002078 /* case 3: ultra-bay related. maybe bay in dock? */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002079 /* 0x3003 - T43 after wake up by bay lever
2080 * eject (0x2305) */
2081 printk(TPACPI_NOTICE
2082 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002083 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002084 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002085
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002086 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002087 if (!ignore_acpi_ev &&
2088 (send_acpi_ev || hotkey_report_mode < 2)) {
2089 acpi_bus_generate_proc_event(ibm->acpi->device,
2090 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002091 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002092
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002093 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002094 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002095 acpi_bus_generate_netlink_event(
2096 ibm->acpi->device->pnp.device_class,
2097 ibm->acpi->device->dev.bus_id,
2098 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002099 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002100 }
2101}
2102
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002103static void hotkey_resume(void)
2104{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002105 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002106 printk(TPACPI_ERR
2107 "error while trying to read hot key mask "
2108 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002109 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002110#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2111 hotkey_poll_setup_safe(0);
2112#endif
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002113}
2114
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002115/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002116static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002118 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 int len = 0;
2120
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002121 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002122 len += sprintf(p + len, "status:\t\tnot supported\n");
2123 return len;
2124 }
2125
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002126 if (mutex_lock_interruptible(&hotkey_mutex))
2127 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002128 res = hotkey_status_get(&status);
2129 if (!res)
2130 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002131 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002132 if (res)
2133 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002136 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002137 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 len += sprintf(p + len,
2139 "commands:\tenable, disable, reset, <mask>\n");
2140 } else {
2141 len += sprintf(p + len, "mask:\t\tnot supported\n");
2142 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2143 }
2144
2145 return len;
2146}
2147
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002148static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002150 int res, status;
2151 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002154 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return -ENODEV;
2156
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002157 if (mutex_lock_interruptible(&hotkey_mutex))
2158 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002159
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002160 status = -1;
2161 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002162
2163 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 while ((cmd = next_cmd(&buf))) {
2165 if (strlencmp(cmd, "enable") == 0) {
2166 status = 1;
2167 } else if (strlencmp(cmd, "disable") == 0) {
2168 status = 0;
2169 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002170 status = hotkey_orig_status;
2171 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2173 /* mask set */
2174 } else if (sscanf(cmd, "%x", &mask) == 1) {
2175 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002176 } else {
2177 res = -EINVAL;
2178 goto errexit;
2179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002181 if (status != -1)
2182 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002184 if (!res && mask != hotkey_mask)
2185 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002187errexit:
2188 mutex_unlock(&hotkey_mutex);
2189 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002192static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002193 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002194 {"", 0},
2195};
2196
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002197static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002198 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002199 .notify = hotkey_notify,
2200 .handle = &hkey_handle,
2201 .type = ACPI_DEVICE_NOTIFY,
2202};
2203
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002204static struct ibm_struct hotkey_driver_data = {
2205 .name = "hotkey",
2206 .read = hotkey_read,
2207 .write = hotkey_write,
2208 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002209 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002210 .acpi = &ibm_hotkey_acpidriver,
2211};
2212
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002213/*************************************************************************
2214 * Bluetooth subdriver
2215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002217enum {
2218 /* ACPI GBDC/SBDC bits */
2219 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2220 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2221 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2222};
2223
2224static int bluetooth_get_radiosw(void);
2225static int bluetooth_set_radiosw(int radio_on);
2226
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002227/* sysfs bluetooth enable ---------------------------------------------- */
2228static ssize_t bluetooth_enable_show(struct device *dev,
2229 struct device_attribute *attr,
2230 char *buf)
2231{
2232 int status;
2233
2234 status = bluetooth_get_radiosw();
2235 if (status < 0)
2236 return status;
2237
2238 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2239}
2240
2241static ssize_t bluetooth_enable_store(struct device *dev,
2242 struct device_attribute *attr,
2243 const char *buf, size_t count)
2244{
2245 unsigned long t;
2246 int res;
2247
2248 if (parse_strtoul(buf, 1, &t))
2249 return -EINVAL;
2250
2251 res = bluetooth_set_radiosw(t);
2252
2253 return (res) ? res : count;
2254}
2255
2256static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002257 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002258 bluetooth_enable_show, bluetooth_enable_store);
2259
2260/* --------------------------------------------------------------------- */
2261
2262static struct attribute *bluetooth_attributes[] = {
2263 &dev_attr_bluetooth_enable.attr,
2264 NULL
2265};
2266
2267static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002268 .attrs = bluetooth_attributes,
2269};
2270
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002271static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002273 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002274 int status = 0;
2275
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002276 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2277
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002278 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002279
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002280 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2281 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002282 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002283 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002285 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2286 str_supported(tp_features.bluetooth),
2287 status);
2288
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002289 if (tp_features.bluetooth) {
2290 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2291 /* no bluetooth hardware present in system */
2292 tp_features.bluetooth = 0;
2293 dbg_printk(TPACPI_DBG_INIT,
2294 "bluetooth hardware not installed\n");
2295 } else {
2296 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2297 &bluetooth_attr_group);
2298 if (res)
2299 return res;
2300 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002301 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002302
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002303 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002306static void bluetooth_exit(void)
2307{
2308 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2309 &bluetooth_attr_group);
2310}
2311
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002312static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 int status;
2315
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002316 if (!tp_features.bluetooth)
2317 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002319 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2320 return -EIO;
2321
2322 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2323}
2324
2325static int bluetooth_set_radiosw(int radio_on)
2326{
2327 int status;
2328
2329 if (!tp_features.bluetooth)
2330 return -ENODEV;
2331
2332 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2333 return -EIO;
2334 if (radio_on)
2335 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2336 else
2337 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2338 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2339 return -EIO;
2340
2341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002344/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002345static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002348 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002350 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002353 len += sprintf(p + len, "status:\t\t%s\n",
2354 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 len += sprintf(p + len, "commands:\tenable, disable\n");
2356 }
2357
2358 return len;
2359}
2360
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002361static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002365 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002366 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 while ((cmd = next_cmd(&buf))) {
2369 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002370 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002372 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 } else
2374 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return 0;
2378}
2379
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002380static struct ibm_struct bluetooth_driver_data = {
2381 .name = "bluetooth",
2382 .read = bluetooth_read,
2383 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002384 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002385};
2386
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002387/*************************************************************************
2388 * Wan subdriver
2389 */
2390
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002391enum {
2392 /* ACPI GWAN/SWAN bits */
2393 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2394 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2395 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2396};
2397
2398static int wan_get_radiosw(void);
2399static int wan_set_radiosw(int radio_on);
2400
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002401/* sysfs wan enable ---------------------------------------------------- */
2402static ssize_t wan_enable_show(struct device *dev,
2403 struct device_attribute *attr,
2404 char *buf)
2405{
2406 int status;
2407
2408 status = wan_get_radiosw();
2409 if (status < 0)
2410 return status;
2411
2412 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2413}
2414
2415static ssize_t wan_enable_store(struct device *dev,
2416 struct device_attribute *attr,
2417 const char *buf, size_t count)
2418{
2419 unsigned long t;
2420 int res;
2421
2422 if (parse_strtoul(buf, 1, &t))
2423 return -EINVAL;
2424
2425 res = wan_set_radiosw(t);
2426
2427 return (res) ? res : count;
2428}
2429
2430static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002431 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002432 wan_enable_show, wan_enable_store);
2433
2434/* --------------------------------------------------------------------- */
2435
2436static struct attribute *wan_attributes[] = {
2437 &dev_attr_wan_enable.attr,
2438 NULL
2439};
2440
2441static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002442 .attrs = wan_attributes,
2443};
2444
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002445static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002446{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002447 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002448 int status = 0;
2449
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002450 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2451
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002452 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002453
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002454 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002455 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002456
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002457 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2458 str_supported(tp_features.wan),
2459 status);
2460
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002461 if (tp_features.wan) {
2462 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2463 /* no wan hardware present in system */
2464 tp_features.wan = 0;
2465 dbg_printk(TPACPI_DBG_INIT,
2466 "wan hardware not installed\n");
2467 } else {
2468 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2469 &wan_attr_group);
2470 if (res)
2471 return res;
2472 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002473 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002474
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002475 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002476}
2477
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002478static void wan_exit(void)
2479{
2480 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2481 &wan_attr_group);
2482}
2483
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002484static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002485{
2486 int status;
2487
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002488 if (!tp_features.wan)
2489 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002490
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002491 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2492 return -EIO;
2493
2494 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2495}
2496
2497static int wan_set_radiosw(int radio_on)
2498{
2499 int status;
2500
2501 if (!tp_features.wan)
2502 return -ENODEV;
2503
2504 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2505 return -EIO;
2506 if (radio_on)
2507 status |= TP_ACPI_WANCARD_RADIOSSW;
2508 else
2509 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2510 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2511 return -EIO;
2512
2513 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002514}
2515
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002516/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002517static int wan_read(char *p)
2518{
2519 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002520 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002521
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002522 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002523 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002524 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002525 len += sprintf(p + len, "status:\t\t%s\n",
2526 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002527 len += sprintf(p + len, "commands:\tenable, disable\n");
2528 }
2529
2530 return len;
2531}
2532
2533static int wan_write(char *buf)
2534{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002535 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002536
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002537 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002538 return -ENODEV;
2539
2540 while ((cmd = next_cmd(&buf))) {
2541 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002542 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002543 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002544 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002545 } else
2546 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002547 }
2548
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002549 return 0;
2550}
2551
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002552static struct ibm_struct wan_driver_data = {
2553 .name = "wan",
2554 .read = wan_read,
2555 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002556 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002557 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002558};
2559
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002560/*************************************************************************
2561 * Video subdriver
2562 */
2563
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002564enum video_access_mode {
2565 TPACPI_VIDEO_NONE = 0,
2566 TPACPI_VIDEO_570, /* 570 */
2567 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2568 TPACPI_VIDEO_NEW, /* all others */
2569};
2570
2571enum { /* video status flags, based on VIDEO_570 */
2572 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2573 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2574 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2575};
2576
2577enum { /* TPACPI_VIDEO_570 constants */
2578 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2579 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2580 * video_status_flags */
2581 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2582 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2583};
2584
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002585static enum video_access_mode video_supported;
2586static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002587
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002588static int video_autosw_get(void);
2589static int video_autosw_set(int enable);
2590
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002591TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002592 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2593 "\\_SB.PCI0.VID0", /* 770e */
2594 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2595 "\\_SB.PCI0.AGP.VID", /* all others */
2596 ); /* R30, R31 */
2597
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002598TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002599
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002600static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002602 int ivga;
2603
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002604 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2605
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002606 TPACPI_ACPIHANDLE_INIT(vid);
2607 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002608
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002609 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2610 /* G41, assume IVGA doesn't change */
2611 vid_handle = vid2_handle;
2612
2613 if (!vid_handle)
2614 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002615 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002616 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2617 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002618 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002619 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2620 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002621 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002622 else
2623 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002624 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002626 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2627 str_supported(video_supported != TPACPI_VIDEO_NONE),
2628 video_supported);
2629
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002630 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002633static void video_exit(void)
2634{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002635 dbg_printk(TPACPI_DBG_EXIT,
2636 "restoring original video autoswitch mode\n");
2637 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002638 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002639 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002640}
2641
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002642static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
2644 int status = 0;
2645 int i;
2646
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002647 switch (video_supported) {
2648 case TPACPI_VIDEO_570:
2649 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2650 TP_ACPI_VIDEO_570_PHSCMD))
2651 return -EIO;
2652 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2653 break;
2654 case TPACPI_VIDEO_770:
2655 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2656 return -EIO;
2657 if (i)
2658 status |= TP_ACPI_VIDEO_S_LCD;
2659 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2660 return -EIO;
2661 if (i)
2662 status |= TP_ACPI_VIDEO_S_CRT;
2663 break;
2664 case TPACPI_VIDEO_NEW:
2665 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2666 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2667 return -EIO;
2668 if (i)
2669 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002671 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2672 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2673 return -EIO;
2674 if (i)
2675 status |= TP_ACPI_VIDEO_S_LCD;
2676 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2677 return -EIO;
2678 if (i)
2679 status |= TP_ACPI_VIDEO_S_DVI;
2680 break;
2681 default:
2682 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 return status;
2686}
2687
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002688static int video_outputsw_set(int status)
2689{
2690 int autosw;
2691 int res = 0;
2692
2693 switch (video_supported) {
2694 case TPACPI_VIDEO_570:
2695 res = acpi_evalf(NULL, NULL,
2696 "\\_SB.PHS2", "vdd",
2697 TP_ACPI_VIDEO_570_PHS2CMD,
2698 status | TP_ACPI_VIDEO_570_PHS2SET);
2699 break;
2700 case TPACPI_VIDEO_770:
2701 autosw = video_autosw_get();
2702 if (autosw < 0)
2703 return autosw;
2704
2705 res = video_autosw_set(1);
2706 if (res)
2707 return res;
2708 res = acpi_evalf(vid_handle, NULL,
2709 "ASWT", "vdd", status * 0x100, 0);
2710 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002711 printk(TPACPI_ERR
2712 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002713 return -EIO;
2714 }
2715 break;
2716 case TPACPI_VIDEO_NEW:
2717 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002718 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002719 break;
2720 default:
2721 return -ENOSYS;
2722 }
2723
2724 return (res)? 0 : -EIO;
2725}
2726
2727static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002729 int autosw = 0;
2730
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002731 switch (video_supported) {
2732 case TPACPI_VIDEO_570:
2733 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2734 return -EIO;
2735 break;
2736 case TPACPI_VIDEO_770:
2737 case TPACPI_VIDEO_NEW:
2738 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2739 return -EIO;
2740 break;
2741 default:
2742 return -ENOSYS;
2743 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002744
2745 return autosw & 1;
2746}
2747
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002748static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002749{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002750 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002751 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002752 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002753}
2754
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002755static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002756{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002757 int autosw = video_autosw_get();
2758 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002759
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002760 if (autosw < 0)
2761 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002762
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002763 switch (video_supported) {
2764 case TPACPI_VIDEO_570:
2765 res = video_autosw_set(1);
2766 if (res)
2767 return res;
2768 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2769 break;
2770 case TPACPI_VIDEO_770:
2771 case TPACPI_VIDEO_NEW:
2772 res = video_autosw_set(1);
2773 if (res)
2774 return res;
2775 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2776 break;
2777 default:
2778 return -ENOSYS;
2779 }
2780 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002781 printk(TPACPI_ERR
2782 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002783 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002784 }
2785
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002786 return (res)? 0 : -EIO;
2787}
2788
2789static int video_expand_toggle(void)
2790{
2791 switch (video_supported) {
2792 case TPACPI_VIDEO_570:
2793 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2794 0 : -EIO;
2795 case TPACPI_VIDEO_770:
2796 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2797 0 : -EIO;
2798 case TPACPI_VIDEO_NEW:
2799 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2800 0 : -EIO;
2801 default:
2802 return -ENOSYS;
2803 }
2804 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002805}
2806
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002807static int video_read(char *p)
2808{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002809 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002810 int len = 0;
2811
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002812 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002813 len += sprintf(p + len, "status:\t\tnot supported\n");
2814 return len;
2815 }
2816
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002817 status = video_outputsw_get();
2818 if (status < 0)
2819 return status;
2820
2821 autosw = video_autosw_get();
2822 if (autosw < 0)
2823 return autosw;
2824
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002825 len += sprintf(p + len, "status:\t\tsupported\n");
2826 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2827 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002828 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002829 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2830 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2831 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2832 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002833 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002834 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2835 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2836 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2837
2838 return len;
2839}
2840
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002841static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 char *cmd;
2844 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002845 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002847 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002848 return -ENODEV;
2849
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002850 enable = 0;
2851 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853 while ((cmd = next_cmd(&buf))) {
2854 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002855 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002857 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002859 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002861 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002862 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002863 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002864 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002865 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002866 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002867 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002869 res = video_autosw_set(1);
2870 if (res)
2871 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002873 res = video_autosw_set(0);
2874 if (res)
2875 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002877 res = video_outputsw_cycle();
2878 if (res)
2879 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002881 res = video_expand_toggle();
2882 if (res)
2883 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 } else
2885 return -EINVAL;
2886 }
2887
2888 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002889 status = video_outputsw_get();
2890 if (status < 0)
2891 return status;
2892 res = video_outputsw_set((status & ~disable) | enable);
2893 if (res)
2894 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
2896
2897 return 0;
2898}
2899
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002900static struct ibm_struct video_driver_data = {
2901 .name = "video",
2902 .read = video_read,
2903 .write = video_write,
2904 .exit = video_exit,
2905};
2906
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002907/*************************************************************************
2908 * Light (thinklight) subdriver
2909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002911TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2912TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002913
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002914static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002916 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2917
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002918 TPACPI_ACPIHANDLE_INIT(ledb);
2919 TPACPI_ACPIHANDLE_INIT(lght);
2920 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002921
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002922 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002923 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002924
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002925 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002926 /* light status not supported on
2927 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002928 tp_features.light_status =
2929 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002931 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002932 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002933
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002934 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935}
2936
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002937static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 int len = 0;
2940 int status = 0;
2941
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002942 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002943 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002944 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002945 len += sprintf(p + len, "status:\t\tunknown\n");
2946 len += sprintf(p + len, "commands:\ton, off\n");
2947 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2949 return -EIO;
2950 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002951 len += sprintf(p + len, "commands:\ton, off\n");
2952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954 return len;
2955}
2956
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002957static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
2959 int cmos_cmd, lght_cmd;
2960 char *cmd;
2961 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002962
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002963 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002964 return -ENODEV;
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 while ((cmd = next_cmd(&buf))) {
2967 if (strlencmp(cmd, "on") == 0) {
2968 cmos_cmd = 0x0c;
2969 lght_cmd = 1;
2970 } else if (strlencmp(cmd, "off") == 0) {
2971 cmos_cmd = 0x0d;
2972 lght_cmd = 0;
2973 } else
2974 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002977 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2978 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (!success)
2980 return -EIO;
2981 }
2982
2983 return 0;
2984}
2985
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002986static struct ibm_struct light_driver_data = {
2987 .name = "light",
2988 .read = light_read,
2989 .write = light_write,
2990};
2991
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002992/*************************************************************************
2993 * Dock subdriver
2994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002996#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002997
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002998static void dock_notify(struct ibm_struct *ibm, u32 event);
2999static int dock_read(char *p);
3000static int dock_write(char *buf);
3001
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003002TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003003 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3004 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3005 "\\_SB.PCI.ISA.SLCE", /* 570 */
3006 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3007
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003008/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003009TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003010
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003011static const struct acpi_device_id ibm_pci_device_ids[] = {
3012 {PCI_ROOT_HID_STRING, 0},
3013 {"", 0},
3014};
3015
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003016static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3017 {
3018 .notify = dock_notify,
3019 .handle = &dock_handle,
3020 .type = ACPI_SYSTEM_NOTIFY,
3021 },
3022 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003023 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3024 * We just use it to get notifications of dock hotplug
3025 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003026 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003027 .notify = dock_notify,
3028 .handle = &pci_handle,
3029 .type = ACPI_SYSTEM_NOTIFY,
3030 },
3031};
3032
3033static struct ibm_struct dock_driver_data[2] = {
3034 {
3035 .name = "dock",
3036 .read = dock_read,
3037 .write = dock_write,
3038 .acpi = &ibm_dock_acpidriver[0],
3039 },
3040 {
3041 .name = "dock",
3042 .acpi = &ibm_dock_acpidriver[1],
3043 },
3044};
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046#define dock_docked() (_sta(dock_handle) & 1)
3047
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003048static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003049{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003050 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3051
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003052 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003053
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003054 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3055 str_supported(dock_handle != NULL));
3056
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003057 return (dock_handle)? 0 : 1;
3058}
3059
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003060static int __init dock_init2(struct ibm_init_struct *iibm)
3061{
3062 int dock2_needed;
3063
3064 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3065
3066 if (dock_driver_data[0].flags.acpi_driver_registered &&
3067 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003068 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003069 dock2_needed = (pci_handle != NULL);
3070 vdbg_printk(TPACPI_DBG_INIT,
3071 "dock PCI handler for the TP 570 is %s\n",
3072 str_supported(dock2_needed));
3073 } else {
3074 vdbg_printk(TPACPI_DBG_INIT,
3075 "dock subdriver part 2 not required\n");
3076 dock2_needed = 0;
3077 }
3078
3079 return (dock2_needed)? 0 : 1;
3080}
3081
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003082static void dock_notify(struct ibm_struct *ibm, u32 event)
3083{
3084 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003085 int pci = ibm->acpi->hid && ibm->acpi->device &&
3086 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003087 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003088
3089 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003090 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003091 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003092 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003093 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003094 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003095 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003096 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003097 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003098 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003099 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003100 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003101 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003102 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003103 }
Len Brown14e04fb32007-08-23 15:20:26 -04003104 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003105 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3106 ibm->acpi->device->dev.bus_id,
3107 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003108}
3109
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003110static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 int len = 0;
3113 int docked = dock_docked();
3114
3115 if (!dock_handle)
3116 len += sprintf(p + len, "status:\t\tnot supported\n");
3117 else if (!docked)
3118 len += sprintf(p + len, "status:\t\tundocked\n");
3119 else {
3120 len += sprintf(p + len, "status:\t\tdocked\n");
3121 len += sprintf(p + len, "commands:\tdock, undock\n");
3122 }
3123
3124 return len;
3125}
3126
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003127static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
3129 char *cmd;
3130
3131 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003132 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 while ((cmd = next_cmd(&buf))) {
3135 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003136 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3137 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return -EIO;
3139 } else if (strlencmp(cmd, "dock") == 0) {
3140 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3141 return -EIO;
3142 } else
3143 return -EINVAL;
3144 }
3145
3146 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003149#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003151/*************************************************************************
3152 * Bay subdriver
3153 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003155#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003156
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003157TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003158 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3159 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3160 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3161 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003162TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003163 "_EJ0", /* all others */
3164 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003165TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003166 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3167 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003168TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003169 "_EJ0", /* 770x */
3170 ); /* all others */
3171
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003172static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003174 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3175
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003176 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003177 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003178 TPACPI_ACPIHANDLE_INIT(bay_ej);
3179 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003180 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003181 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003182
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003183 tp_features.bay_status = bay_handle &&
3184 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3185 tp_features.bay_status2 = bay2_handle &&
3186 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003187
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003188 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3189 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3190 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3191 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003193 vdbg_printk(TPACPI_DBG_INIT,
3194 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003195 str_supported(tp_features.bay_status),
3196 str_supported(tp_features.bay_eject),
3197 str_supported(tp_features.bay_status2),
3198 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003199
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003200 return (tp_features.bay_status || tp_features.bay_eject ||
3201 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003204static void bay_notify(struct ibm_struct *ibm, u32 event)
3205{
Len Brown14e04fb32007-08-23 15:20:26 -04003206 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003207 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3208 ibm->acpi->device->dev.bus_id,
3209 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003210}
3211
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003212#define bay_occupied(b) (_sta(b##_handle) & 1)
3213
3214static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003217 int occupied = bay_occupied(bay);
3218 int occupied2 = bay_occupied(bay2);
3219 int eject, eject2;
3220
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003221 len += sprintf(p + len, "status:\t\t%s\n",
3222 tp_features.bay_status ?
3223 (occupied ? "occupied" : "unoccupied") :
3224 "not supported");
3225 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003226 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3227 "occupied" : "unoccupied");
3228
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003229 eject = tp_features.bay_eject && occupied;
3230 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003231
3232 if (eject && eject2)
3233 len += sprintf(p + len, "commands:\teject, eject2\n");
3234 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003236 else if (eject2)
3237 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239 return len;
3240}
3241
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003242static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
3244 char *cmd;
3245
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003246 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003247 return -ENODEV;
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003250 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003251 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3252 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003253 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003254 strlencmp(cmd, "eject2") == 0) {
3255 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return -EIO;
3257 } else
3258 return -EINVAL;
3259 }
3260
3261 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003262}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003263
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003264static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3265 .notify = bay_notify,
3266 .handle = &bay_handle,
3267 .type = ACPI_SYSTEM_NOTIFY,
3268};
3269
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003270static struct ibm_struct bay_driver_data = {
3271 .name = "bay",
3272 .read = bay_read,
3273 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003274 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003275};
3276
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003277#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003279/*************************************************************************
3280 * CMOS subdriver
3281 */
3282
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003283/* sysfs cmos_command -------------------------------------------------- */
3284static ssize_t cmos_command_store(struct device *dev,
3285 struct device_attribute *attr,
3286 const char *buf, size_t count)
3287{
3288 unsigned long cmos_cmd;
3289 int res;
3290
3291 if (parse_strtoul(buf, 21, &cmos_cmd))
3292 return -EINVAL;
3293
3294 res = issue_thinkpad_cmos_command(cmos_cmd);
3295 return (res)? res : count;
3296}
3297
3298static struct device_attribute dev_attr_cmos_command =
3299 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3300
3301/* --------------------------------------------------------------------- */
3302
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003303static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003304{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003305 int res;
3306
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003307 vdbg_printk(TPACPI_DBG_INIT,
3308 "initializing cmos commands subdriver\n");
3309
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003310 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003311
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003312 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3313 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003314
3315 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3316 if (res)
3317 return res;
3318
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003319 return (cmos_handle)? 0 : 1;
3320}
3321
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003322static void cmos_exit(void)
3323{
3324 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3325}
3326
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003327static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
3329 int len = 0;
3330
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003331 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3332 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 if (!cmos_handle)
3334 len += sprintf(p + len, "status:\t\tnot supported\n");
3335 else {
3336 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003337 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 }
3339
3340 return len;
3341}
3342
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003343static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
3345 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003346 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003349 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3350 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 /* cmos_cmd set */
3352 } else
3353 return -EINVAL;
3354
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003355 res = issue_thinkpad_cmos_command(cmos_cmd);
3356 if (res)
3357 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
3359
3360 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003361}
3362
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003363static struct ibm_struct cmos_driver_data = {
3364 .name = "cmos",
3365 .read = cmos_read,
3366 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003367 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003368};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003369
3370/*************************************************************************
3371 * LED subdriver
3372 */
3373
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003374enum led_access_mode {
3375 TPACPI_LED_NONE = 0,
3376 TPACPI_LED_570, /* 570 */
3377 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3378 TPACPI_LED_NEW, /* all others */
3379};
3380
3381enum { /* For TPACPI_LED_OLD */
3382 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3383 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3384 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3385};
3386
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003387static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003388
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003389TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003390 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3391 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003392 "LED", /* all others */
3393 ); /* R30, R31 */
3394
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003395static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003396{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003397 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3398
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003399 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003400
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003401 if (!led_handle)
3402 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003403 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003404 else if (strlencmp(led_path, "SLED") == 0)
3405 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003406 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003407 else if (strlencmp(led_path, "SYSL") == 0)
3408 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003409 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003410 else
3411 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003412 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003413
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003414 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3415 str_supported(led_supported), led_supported);
3416
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003417 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003418}
3419
3420#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3421
3422static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
3424 int len = 0;
3425
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003426 if (!led_supported) {
3427 len += sprintf(p + len, "status:\t\tnot supported\n");
3428 return len;
3429 }
3430 len += sprintf(p + len, "status:\t\tsupported\n");
3431
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003432 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003433 /* 570 */
3434 int i, status;
3435 for (i = 0; i < 8; i++) {
3436 if (!acpi_evalf(ec_handle,
3437 &status, "GLED", "dd", 1 << i))
3438 return -EIO;
3439 len += sprintf(p + len, "%d:\t\t%s\n",
3440 i, led_status(status));
3441 }
3442 }
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003445 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447 return len;
3448}
3449
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003450/* off, on, blink */
3451static const int led_sled_arg1[] = { 0, 1, 3 };
3452static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3453static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3454static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3455
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003456static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
3458 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003459 int led, ind, ret;
3460
3461 if (!led_supported)
3462 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003465 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return -EINVAL;
3467
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003468 if (strstr(cmd, "off")) {
3469 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003471 ind = 1;
3472 } else if (strstr(cmd, "blink")) {
3473 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 } else
3475 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003476
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003477 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003478 /* 570 */
3479 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003481 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003483 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003484 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3485 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003486 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003487 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003488 ret = ec_write(TPACPI_LED_EC_HLBL,
3489 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003490 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003491 ret = ec_write(TPACPI_LED_EC_HLCL,
3492 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003493 if (ret < 0)
3494 return ret;
3495 } else {
3496 /* all others */
3497 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3498 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 }
3502
3503 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003504}
3505
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003506static struct ibm_struct led_driver_data = {
3507 .name = "led",
3508 .read = led_read,
3509 .write = led_write,
3510};
3511
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003512/*************************************************************************
3513 * Beep subdriver
3514 */
3515
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003516TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003517
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003518static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003519{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003520 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3521
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003522 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003523
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003524 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3525 str_supported(beep_handle != NULL));
3526
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003527 return (beep_handle)? 0 : 1;
3528}
3529
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003530static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
3532 int len = 0;
3533
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003534 if (!beep_handle)
3535 len += sprintf(p + len, "status:\t\tnot supported\n");
3536 else {
3537 len += sprintf(p + len, "status:\t\tsupported\n");
3538 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 return len;
3542}
3543
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003544static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
3546 char *cmd;
3547 int beep_cmd;
3548
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003549 if (!beep_handle)
3550 return -ENODEV;
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003553 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3554 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 /* beep_cmd set */
3556 } else
3557 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003558 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return -EIO;
3560 }
3561
3562 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003563}
3564
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003565static struct ibm_struct beep_driver_data = {
3566 .name = "beep",
3567 .read = beep_read,
3568 .write = beep_write,
3569};
3570
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003571/*************************************************************************
3572 * Thermal subdriver
3573 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003574
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003575enum thermal_access_mode {
3576 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3577 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3578 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3579 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3580 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3581};
3582
3583enum { /* TPACPI_THERMAL_TPEC_* */
3584 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3585 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3586 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3587};
3588
3589#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3590struct ibm_thermal_sensors_struct {
3591 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3592};
3593
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003594static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003595
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003596/* idx is zero-based */
3597static int thermal_get_sensor(int idx, s32 *value)
3598{
3599 int t;
3600 s8 tmp;
3601 char tmpi[5];
3602
3603 t = TP_EC_THERMAL_TMP0;
3604
3605 switch (thermal_read_mode) {
3606#if TPACPI_MAX_THERMAL_SENSORS >= 16
3607 case TPACPI_THERMAL_TPEC_16:
3608 if (idx >= 8 && idx <= 15) {
3609 t = TP_EC_THERMAL_TMP8;
3610 idx -= 8;
3611 }
3612 /* fallthrough */
3613#endif
3614 case TPACPI_THERMAL_TPEC_8:
3615 if (idx <= 7) {
3616 if (!acpi_ec_read(t + idx, &tmp))
3617 return -EIO;
3618 *value = tmp * 1000;
3619 return 0;
3620 }
3621 break;
3622
3623 case TPACPI_THERMAL_ACPI_UPDT:
3624 if (idx <= 7) {
3625 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3626 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3627 return -EIO;
3628 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3629 return -EIO;
3630 *value = (t - 2732) * 100;
3631 return 0;
3632 }
3633 break;
3634
3635 case TPACPI_THERMAL_ACPI_TMP07:
3636 if (idx <= 7) {
3637 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3638 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3639 return -EIO;
3640 if (t > 127 || t < -127)
3641 t = TP_EC_THERMAL_TMP_NA;
3642 *value = t * 1000;
3643 return 0;
3644 }
3645 break;
3646
3647 case TPACPI_THERMAL_NONE:
3648 default:
3649 return -ENOSYS;
3650 }
3651
3652 return -EINVAL;
3653}
3654
3655static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3656{
3657 int res, i;
3658 int n;
3659
3660 n = 8;
3661 i = 0;
3662
3663 if (!s)
3664 return -EINVAL;
3665
3666 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3667 n = 16;
3668
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003669 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003670 res = thermal_get_sensor(i, &s->temp[i]);
3671 if (res)
3672 return res;
3673 }
3674
3675 return n;
3676}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003677
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003678/* sysfs temp##_input -------------------------------------------------- */
3679
3680static ssize_t thermal_temp_input_show(struct device *dev,
3681 struct device_attribute *attr,
3682 char *buf)
3683{
3684 struct sensor_device_attribute *sensor_attr =
3685 to_sensor_dev_attr(attr);
3686 int idx = sensor_attr->index;
3687 s32 value;
3688 int res;
3689
3690 res = thermal_get_sensor(idx, &value);
3691 if (res)
3692 return res;
3693 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3694 return -ENXIO;
3695
3696 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3697}
3698
3699#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003700 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3701 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003702
3703static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3704 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3705 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3706 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3707 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3708 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3709 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3710 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3711 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3712 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3713 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3714 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3715 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3716 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3717 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3718 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3719 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3720};
3721
3722#define THERMAL_ATTRS(X) \
3723 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3724
3725static struct attribute *thermal_temp_input_attr[] = {
3726 THERMAL_ATTRS(8),
3727 THERMAL_ATTRS(9),
3728 THERMAL_ATTRS(10),
3729 THERMAL_ATTRS(11),
3730 THERMAL_ATTRS(12),
3731 THERMAL_ATTRS(13),
3732 THERMAL_ATTRS(14),
3733 THERMAL_ATTRS(15),
3734 THERMAL_ATTRS(0),
3735 THERMAL_ATTRS(1),
3736 THERMAL_ATTRS(2),
3737 THERMAL_ATTRS(3),
3738 THERMAL_ATTRS(4),
3739 THERMAL_ATTRS(5),
3740 THERMAL_ATTRS(6),
3741 THERMAL_ATTRS(7),
3742 NULL
3743};
3744
3745static const struct attribute_group thermal_temp_input16_group = {
3746 .attrs = thermal_temp_input_attr
3747};
3748
3749static const struct attribute_group thermal_temp_input8_group = {
3750 .attrs = &thermal_temp_input_attr[8]
3751};
3752
3753#undef THERMAL_SENSOR_ATTR_TEMP
3754#undef THERMAL_ATTRS
3755
3756/* --------------------------------------------------------------------- */
3757
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003758static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003759{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003760 u8 t, ta1, ta2;
3761 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003762 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003763 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003764
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003765 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3766
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003767 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003768
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003769 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003770 /*
3771 * Direct EC access mode: sensors at registers
3772 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3773 * non-implemented, thermal sensors return 0x80 when
3774 * not available
3775 */
3776
3777 ta1 = ta2 = 0;
3778 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003779 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003780 ta1 |= t;
3781 } else {
3782 ta1 = 0;
3783 break;
3784 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003785 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003786 ta2 |= t;
3787 } else {
3788 ta1 = 0;
3789 break;
3790 }
3791 }
3792 if (ta1 == 0) {
3793 /* This is sheer paranoia, but we handle it anyway */
3794 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003795 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003796 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003797 "falling back to ACPI TMPx access "
3798 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003799 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003800 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003801 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003802 "ThinkPad ACPI EC access misbehaving, "
3803 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003804 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003805 }
3806 } else {
3807 thermal_read_mode =
3808 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003809 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003810 }
3811 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003812 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3813 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003814 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003815 } else {
3816 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003817 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003818 }
3819 } else {
3820 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003821 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003822 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003823
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003824 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3825 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3826 thermal_read_mode);
3827
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003828 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003829 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003830 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003831 &thermal_temp_input16_group);
3832 if (res)
3833 return res;
3834 break;
3835 case TPACPI_THERMAL_TPEC_8:
3836 case TPACPI_THERMAL_ACPI_TMP07:
3837 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003838 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003839 &thermal_temp_input8_group);
3840 if (res)
3841 return res;
3842 break;
3843 case TPACPI_THERMAL_NONE:
3844 default:
3845 return 1;
3846 }
3847
3848 return 0;
3849}
3850
3851static void thermal_exit(void)
3852{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003853 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003854 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003855 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003856 &thermal_temp_input16_group);
3857 break;
3858 case TPACPI_THERMAL_TPEC_8:
3859 case TPACPI_THERMAL_ACPI_TMP07:
3860 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003861 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003862 &thermal_temp_input16_group);
3863 break;
3864 case TPACPI_THERMAL_NONE:
3865 default:
3866 break;
3867 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003868}
3869
3870static int thermal_read(char *p)
3871{
3872 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003873 int n, i;
3874 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003875
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003876 n = thermal_get_sensors(&t);
3877 if (unlikely(n < 0))
3878 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003879
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003880 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003881
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003882 if (n > 0) {
3883 for (i = 0; i < (n - 1); i++)
3884 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3885 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3886 } else
3887 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003888
3889 return len;
3890}
3891
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003892static struct ibm_struct thermal_driver_data = {
3893 .name = "thermal",
3894 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003895 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003896};
3897
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003898/*************************************************************************
3899 * EC Dump subdriver
3900 */
3901
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003902static u8 ecdump_regs[256];
3903
3904static int ecdump_read(char *p)
3905{
3906 int len = 0;
3907 int i, j;
3908 u8 v;
3909
3910 len += sprintf(p + len, "EC "
3911 " +00 +01 +02 +03 +04 +05 +06 +07"
3912 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3913 for (i = 0; i < 256; i += 16) {
3914 len += sprintf(p + len, "EC 0x%02x:", i);
3915 for (j = 0; j < 16; j++) {
3916 if (!acpi_ec_read(i + j, &v))
3917 break;
3918 if (v != ecdump_regs[i + j])
3919 len += sprintf(p + len, " *%02x", v);
3920 else
3921 len += sprintf(p + len, " %02x", v);
3922 ecdump_regs[i + j] = v;
3923 }
3924 len += sprintf(p + len, "\n");
3925 if (j != 16)
3926 break;
3927 }
3928
3929 /* These are way too dangerous to advertise openly... */
3930#if 0
3931 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3932 " (<offset> is 00-ff, <value> is 00-ff)\n");
3933 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3934 " (<offset> is 00-ff, <value> is 0-255)\n");
3935#endif
3936 return len;
3937}
3938
3939static int ecdump_write(char *buf)
3940{
3941 char *cmd;
3942 int i, v;
3943
3944 while ((cmd = next_cmd(&buf))) {
3945 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3946 /* i and v set */
3947 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3948 /* i and v set */
3949 } else
3950 return -EINVAL;
3951 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3952 if (!acpi_ec_write(i, v))
3953 return -EIO;
3954 } else
3955 return -EINVAL;
3956 }
3957
3958 return 0;
3959}
3960
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003961static struct ibm_struct ecdump_driver_data = {
3962 .name = "ecdump",
3963 .read = ecdump_read,
3964 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003965 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003966};
3967
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003968/*************************************************************************
3969 * Backlight/brightness subdriver
3970 */
Holger Macht8acb0252006-10-20 14:30:28 -07003971
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003972#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
3973
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003974static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003975static int brightness_offset = 0x31;
3976static int brightness_mode;
3977static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
3978
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003979static struct mutex brightness_mutex;
3980
3981/*
3982 * ThinkPads can read brightness from two places: EC 0x31, or
3983 * CMOS NVRAM byte 0x5E, bits 0-3.
3984 */
3985static int brightness_get(struct backlight_device *bd)
3986{
3987 u8 lec = 0, lcmos = 0, level = 0;
3988
3989 if (brightness_mode & 1) {
3990 if (!acpi_ec_read(brightness_offset, &lec))
3991 return -EIO;
3992 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3993 level = lec;
3994 };
3995 if (brightness_mode & 2) {
3996 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3997 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3998 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3999 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4000 level = lcmos;
4001 }
4002
4003 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004004 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004005 "CMOS NVRAM (%u) and EC (%u) do not agree "
4006 "on display brightness level\n",
4007 (unsigned int) lcmos,
4008 (unsigned int) lec);
4009 return -EIO;
4010 }
4011
4012 return level;
4013}
4014
4015/* May return EINTR which can always be mapped to ERESTARTSYS */
4016static int brightness_set(int value)
4017{
4018 int cmos_cmd, inc, i, res;
4019 int current_value;
4020
4021 if (value > ((tp_features.bright_16levels)? 15 : 7))
4022 return -EINVAL;
4023
4024 res = mutex_lock_interruptible(&brightness_mutex);
4025 if (res < 0)
4026 return res;
4027
4028 current_value = brightness_get(NULL);
4029 if (current_value < 0) {
4030 res = current_value;
4031 goto errout;
4032 }
4033
4034 cmos_cmd = value > current_value ?
4035 TP_CMOS_BRIGHTNESS_UP :
4036 TP_CMOS_BRIGHTNESS_DOWN;
4037 inc = (value > current_value)? 1 : -1;
4038
4039 res = 0;
4040 for (i = current_value; i != value; i += inc) {
4041 if ((brightness_mode & 2) &&
4042 issue_thinkpad_cmos_command(cmos_cmd)) {
4043 res = -EIO;
4044 goto errout;
4045 }
4046 if ((brightness_mode & 1) &&
4047 !acpi_ec_write(brightness_offset, i + inc)) {
4048 res = -EIO;
4049 goto errout;;
4050 }
4051 }
4052
4053errout:
4054 mutex_unlock(&brightness_mutex);
4055 return res;
4056}
4057
4058/* sysfs backlight class ----------------------------------------------- */
4059
4060static int brightness_update_status(struct backlight_device *bd)
4061{
4062 /* it is the backlight class's job (caller) to handle
4063 * EINTR and other errors properly */
4064 return brightness_set(
4065 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4066 bd->props.power == FB_BLANK_UNBLANK) ?
4067 bd->props.brightness : 0);
4068}
Holger Macht8acb0252006-10-20 14:30:28 -07004069
Richard Purdie599a52d2007-02-10 23:07:48 +00004070static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004071 .get_brightness = brightness_get,
4072 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004073};
4074
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004075/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004076
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004077static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4078{
4079 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4080 union acpi_object *obj;
4081 int rc;
4082
4083 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4084 obj = (union acpi_object *)buffer.pointer;
4085 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004086 printk(TPACPI_ERR "Unknown BCLL data, "
4087 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004088 rc = 0;
4089 } else {
4090 rc = obj->package.count;
4091 }
4092 } else {
4093 return 0;
4094 }
4095
4096 kfree(buffer.pointer);
4097 return rc;
4098}
4099
4100static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4101 void *context, void **rv)
4102{
4103 char name[ACPI_PATH_SEGMENT_LENGTH];
4104 struct acpi_buffer buffer = { sizeof(name), &name };
4105
4106 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4107 !strncmp("BCLL", name, sizeof(name) - 1)) {
4108 if (tpacpi_query_bcll_levels(handle) == 16) {
4109 *rv = handle;
4110 return AE_CTRL_TERMINATE;
4111 } else {
4112 return AE_OK;
4113 }
4114 } else {
4115 return AE_OK;
4116 }
4117}
4118
4119static int __init brightness_check_levels(void)
4120{
4121 int status;
4122 void *found_node = NULL;
4123
4124 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004125 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004126 }
4127 if (!vid_handle)
4128 return 0;
4129
4130 /* Search for a BCLL package with 16 levels */
4131 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004132 brightness_find_bcll, NULL,
4133 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004134
4135 return (ACPI_SUCCESS(status) && found_node != NULL);
4136}
4137
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004138static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4139 void *context, void **rv)
4140{
4141 char name[ACPI_PATH_SEGMENT_LENGTH];
4142 struct acpi_buffer buffer = { sizeof(name), &name };
4143
4144 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4145 !strncmp("_BCL", name, sizeof(name) - 1)) {
4146 *rv = handle;
4147 return AE_CTRL_TERMINATE;
4148 } else {
4149 return AE_OK;
4150 }
4151}
4152
4153static int __init brightness_check_std_acpi_support(void)
4154{
4155 int status;
4156 void *found_node = NULL;
4157
4158 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004159 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004160 }
4161 if (!vid_handle)
4162 return 0;
4163
4164 /* Search for a _BCL method, but don't execute it */
4165 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004166 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004167
4168 return (ACPI_SUCCESS(status) && found_node != NULL);
4169}
4170
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004171static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004172{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004173 int b;
4174
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004175 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4176
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004177 mutex_init(&brightness_mutex);
4178
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004179 if (!brightness_enable) {
4180 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004181 "brightness support disabled by "
4182 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004183 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004184 } else if (brightness_enable > 1) {
4185 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004186 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004187 "standard ACPI backlight interface "
4188 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004189 return 1;
4190 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004191 }
4192
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004193 if (!brightness_mode) {
4194 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4195 brightness_mode = 2;
4196 else
4197 brightness_mode = 3;
4198
4199 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4200 brightness_mode);
4201 }
4202
4203 if (brightness_mode > 3)
4204 return -EINVAL;
4205
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004206 tp_features.bright_16levels =
4207 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4208 brightness_check_levels();
4209
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004210 b = brightness_get(NULL);
4211 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004212 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004213
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004214 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004215 printk(TPACPI_INFO
4216 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004217
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004218 ibm_backlight_device = backlight_device_register(
4219 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4220 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004221 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004222 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004223 return PTR_ERR(ibm_backlight_device);
4224 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004225 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004226
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004227 ibm_backlight_device->props.max_brightness =
4228 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004229 ibm_backlight_device->props.brightness = b;
4230 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004231
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004232 return 0;
4233}
4234
4235static void brightness_exit(void)
4236{
4237 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004238 vdbg_printk(TPACPI_DBG_EXIT,
4239 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004240 backlight_device_unregister(ibm_backlight_device);
4241 ibm_backlight_device = NULL;
4242 }
4243}
4244
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004245static int brightness_read(char *p)
4246{
4247 int len = 0;
4248 int level;
4249
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004250 level = brightness_get(NULL);
4251 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004252 len += sprintf(p + len, "level:\t\tunreadable\n");
4253 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004254 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004255 len += sprintf(p + len, "commands:\tup, down\n");
4256 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004257 " (<level> is 0-%d)\n",
4258 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004259 }
4260
4261 return len;
4262}
4263
4264static int brightness_write(char *buf)
4265{
4266 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004267 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004268 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004269 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004270
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004271 level = brightness_get(NULL);
4272 if (level < 0)
4273 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004274
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004275 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004276 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004277 if (level < max_level)
4278 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004279 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004280 if (level > 0)
4281 level--;
4282 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4283 level >= 0 && level <= max_level) {
4284 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004285 } else
4286 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004287 }
4288
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004289 /*
4290 * Now we know what the final level should be, so we try to set it.
4291 * Doing it this way makes the syscall restartable in case of EINTR
4292 */
4293 rc = brightness_set(level);
4294 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004295}
4296
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004297static struct ibm_struct brightness_driver_data = {
4298 .name = "brightness",
4299 .read = brightness_read,
4300 .write = brightness_write,
4301 .exit = brightness_exit,
4302};
4303
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004304/*************************************************************************
4305 * Volume subdriver
4306 */
4307
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004308static int volume_offset = 0x30;
4309
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004310static int volume_read(char *p)
4311{
4312 int len = 0;
4313 u8 level;
4314
4315 if (!acpi_ec_read(volume_offset, &level)) {
4316 len += sprintf(p + len, "level:\t\tunreadable\n");
4317 } else {
4318 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4319 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4320 len += sprintf(p + len, "commands:\tup, down, mute\n");
4321 len += sprintf(p + len, "commands:\tlevel <level>"
4322 " (<level> is 0-15)\n");
4323 }
4324
4325 return len;
4326}
4327
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004328static int volume_write(char *buf)
4329{
4330 int cmos_cmd, inc, i;
4331 u8 level, mute;
4332 int new_level, new_mute;
4333 char *cmd;
4334
4335 while ((cmd = next_cmd(&buf))) {
4336 if (!acpi_ec_read(volume_offset, &level))
4337 return -EIO;
4338 new_mute = mute = level & 0x40;
4339 new_level = level = level & 0xf;
4340
4341 if (strlencmp(cmd, "up") == 0) {
4342 if (mute)
4343 new_mute = 0;
4344 else
4345 new_level = level == 15 ? 15 : level + 1;
4346 } else if (strlencmp(cmd, "down") == 0) {
4347 if (mute)
4348 new_mute = 0;
4349 else
4350 new_level = level == 0 ? 0 : level - 1;
4351 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4352 new_level >= 0 && new_level <= 15) {
4353 /* new_level set */
4354 } else if (strlencmp(cmd, "mute") == 0) {
4355 new_mute = 0x40;
4356 } else
4357 return -EINVAL;
4358
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004359 if (new_level != level) {
4360 /* mute doesn't change */
4361
4362 cmos_cmd = (new_level > level) ?
4363 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004364 inc = new_level > level ? 1 : -1;
4365
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004366 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004367 !acpi_ec_write(volume_offset, level)))
4368 return -EIO;
4369
4370 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004371 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004372 !acpi_ec_write(volume_offset, i + inc))
4373 return -EIO;
4374
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004375 if (mute &&
4376 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4377 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004378 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004379 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004380 }
4381
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004382 if (new_mute != mute) {
4383 /* level doesn't change */
4384
4385 cmos_cmd = (new_mute) ?
4386 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004387
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004388 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004389 !acpi_ec_write(volume_offset, level + new_mute))
4390 return -EIO;
4391 }
4392 }
4393
4394 return 0;
4395}
4396
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004397static struct ibm_struct volume_driver_data = {
4398 .name = "volume",
4399 .read = volume_read,
4400 .write = volume_write,
4401};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004402
4403/*************************************************************************
4404 * Fan subdriver
4405 */
4406
4407/*
4408 * FAN ACCESS MODES
4409 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004410 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004411 * ACPI GFAN method: returns fan level
4412 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004413 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004414 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004415 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004416 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004417 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4418 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004419 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4420 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004421 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004422 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004423 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4424 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004425 *
4426 * Fan speed changes of any sort (including those caused by the
4427 * disengaged mode) are usually done slowly by the firmware as the
4428 * maximum ammount of fan duty cycle change per second seems to be
4429 * limited.
4430 *
4431 * Reading is not available if GFAN exists.
4432 * Writing is not available if SFAN exists.
4433 *
4434 * Bits
4435 * 7 automatic mode engaged;
4436 * (default operation mode of the ThinkPad)
4437 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004438 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004439 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004440 * the tachometer while the fan controller ramps up
4441 * the speed (which can take up to a few *minutes*).
4442 * Speeds up fan to 100% duty-cycle, which is far above
4443 * the standard RPM levels. It is not impossible that
4444 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004445 * 5-3 unused in some models. Extra bits for fan level
4446 * in others, but still useless as all values above
4447 * 7 map to the same speed as level 7 in these models.
4448 * 2-0 fan level (0..7 usually)
4449 * 0x00 = stop
4450 * 0x07 = max (set when temperatures critical)
4451 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004452 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004453 *
4454 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4455 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4456 * does so, its initial value is meaningless (0x07).
4457 *
4458 * For firmware bugs, refer to:
4459 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4460 *
4461 * ----
4462 *
4463 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4464 * Main fan tachometer reading (in RPM)
4465 *
4466 * This register is present on all ThinkPads with a new-style EC, and
4467 * it is known not to be present on the A21m/e, and T22, as there is
4468 * something else in offset 0x84 according to the ACPI DSDT. Other
4469 * ThinkPads from this same time period (and earlier) probably lack the
4470 * tachometer as well.
4471 *
4472 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4473 * was never fixed by IBM to report the EC firmware version string
4474 * probably support the tachometer (like the early X models), so
4475 * detecting it is quite hard. We need more data to know for sure.
4476 *
4477 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4478 * might result.
4479 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004480 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4481 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004482 *
4483 * For firmware bugs, refer to:
4484 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4485 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004486 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004487 * ThinkPad X31, X40, X41. Not available in the X60.
4488 *
4489 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4490 * high speed. ACPI DSDT seems to map these three speeds to levels
4491 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4492 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4493 *
4494 * The speeds are stored on handles
4495 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4496 *
4497 * There are three default speed sets, acessible as handles:
4498 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4499 *
4500 * ACPI DSDT switches which set is in use depending on various
4501 * factors.
4502 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004503 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004504 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4505 * but the ACPI tables just mention level 7.
4506 */
4507
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004508enum { /* Fan control constants */
4509 fan_status_offset = 0x2f, /* EC register 0x2f */
4510 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4511 * 0x84 must be read before 0x85 */
4512
4513 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4514 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4515
4516 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4517};
4518
4519enum fan_status_access_mode {
4520 TPACPI_FAN_NONE = 0, /* No fan status or control */
4521 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4522 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4523};
4524
4525enum fan_control_access_mode {
4526 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4527 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4528 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4529 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4530};
4531
4532enum fan_control_commands {
4533 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4534 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4535 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4536 * and also watchdog cmd */
4537};
4538
4539static int fan_control_allowed;
4540
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004541static enum fan_status_access_mode fan_status_access_mode;
4542static enum fan_control_access_mode fan_control_access_mode;
4543static enum fan_control_commands fan_control_commands;
4544
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004545static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004546static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004547static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004548
4549static struct mutex fan_mutex;
4550
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004551static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004552static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004553
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004554TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4555TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004556 "\\FSPD", /* 600e/x, 770e, 770x */
4557 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004558TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004559 "JFNS", /* 770x-JL */
4560 ); /* all others */
4561
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004562/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004563 * Call with fan_mutex held
4564 */
4565static void fan_update_desired_level(u8 status)
4566{
4567 if ((status &
4568 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4569 if (status > 7)
4570 fan_control_desired_level = 7;
4571 else
4572 fan_control_desired_level = status;
4573 }
4574}
4575
4576static int fan_get_status(u8 *status)
4577{
4578 u8 s;
4579
4580 /* TODO:
4581 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4582
4583 switch (fan_status_access_mode) {
4584 case TPACPI_FAN_RD_ACPI_GFAN:
4585 /* 570, 600e/x, 770e, 770x */
4586
4587 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4588 return -EIO;
4589
4590 if (likely(status))
4591 *status = s & 0x07;
4592
4593 break;
4594
4595 case TPACPI_FAN_RD_TPEC:
4596 /* all except 570, 600e/x, 770e, 770x */
4597 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4598 return -EIO;
4599
4600 if (likely(status))
4601 *status = s;
4602
4603 break;
4604
4605 default:
4606 return -ENXIO;
4607 }
4608
4609 return 0;
4610}
4611
4612static int fan_get_status_safe(u8 *status)
4613{
4614 int rc;
4615 u8 s;
4616
4617 if (mutex_lock_interruptible(&fan_mutex))
4618 return -ERESTARTSYS;
4619 rc = fan_get_status(&s);
4620 if (!rc)
4621 fan_update_desired_level(s);
4622 mutex_unlock(&fan_mutex);
4623
4624 if (status)
4625 *status = s;
4626
4627 return rc;
4628}
4629
4630static int fan_get_speed(unsigned int *speed)
4631{
4632 u8 hi, lo;
4633
4634 switch (fan_status_access_mode) {
4635 case TPACPI_FAN_RD_TPEC:
4636 /* all except 570, 600e/x, 770e, 770x */
4637 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4638 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4639 return -EIO;
4640
4641 if (likely(speed))
4642 *speed = (hi << 8) | lo;
4643
4644 break;
4645
4646 default:
4647 return -ENXIO;
4648 }
4649
4650 return 0;
4651}
4652
4653static int fan_set_level(int level)
4654{
4655 if (!fan_control_allowed)
4656 return -EPERM;
4657
4658 switch (fan_control_access_mode) {
4659 case TPACPI_FAN_WR_ACPI_SFAN:
4660 if (level >= 0 && level <= 7) {
4661 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4662 return -EIO;
4663 } else
4664 return -EINVAL;
4665 break;
4666
4667 case TPACPI_FAN_WR_ACPI_FANS:
4668 case TPACPI_FAN_WR_TPEC:
4669 if ((level != TP_EC_FAN_AUTO) &&
4670 (level != TP_EC_FAN_FULLSPEED) &&
4671 ((level < 0) || (level > 7)))
4672 return -EINVAL;
4673
4674 /* safety net should the EC not support AUTO
4675 * or FULLSPEED mode bits and just ignore them */
4676 if (level & TP_EC_FAN_FULLSPEED)
4677 level |= 7; /* safety min speed 7 */
4678 else if (level & TP_EC_FAN_FULLSPEED)
4679 level |= 4; /* safety min speed 4 */
4680
4681 if (!acpi_ec_write(fan_status_offset, level))
4682 return -EIO;
4683 else
4684 tp_features.fan_ctrl_status_undef = 0;
4685 break;
4686
4687 default:
4688 return -ENXIO;
4689 }
4690 return 0;
4691}
4692
4693static int fan_set_level_safe(int level)
4694{
4695 int rc;
4696
4697 if (!fan_control_allowed)
4698 return -EPERM;
4699
4700 if (mutex_lock_interruptible(&fan_mutex))
4701 return -ERESTARTSYS;
4702
4703 if (level == TPACPI_FAN_LAST_LEVEL)
4704 level = fan_control_desired_level;
4705
4706 rc = fan_set_level(level);
4707 if (!rc)
4708 fan_update_desired_level(level);
4709
4710 mutex_unlock(&fan_mutex);
4711 return rc;
4712}
4713
4714static int fan_set_enable(void)
4715{
4716 u8 s;
4717 int rc;
4718
4719 if (!fan_control_allowed)
4720 return -EPERM;
4721
4722 if (mutex_lock_interruptible(&fan_mutex))
4723 return -ERESTARTSYS;
4724
4725 switch (fan_control_access_mode) {
4726 case TPACPI_FAN_WR_ACPI_FANS:
4727 case TPACPI_FAN_WR_TPEC:
4728 rc = fan_get_status(&s);
4729 if (rc < 0)
4730 break;
4731
4732 /* Don't go out of emergency fan mode */
4733 if (s != 7) {
4734 s &= 0x07;
4735 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4736 }
4737
4738 if (!acpi_ec_write(fan_status_offset, s))
4739 rc = -EIO;
4740 else {
4741 tp_features.fan_ctrl_status_undef = 0;
4742 rc = 0;
4743 }
4744 break;
4745
4746 case TPACPI_FAN_WR_ACPI_SFAN:
4747 rc = fan_get_status(&s);
4748 if (rc < 0)
4749 break;
4750
4751 s &= 0x07;
4752
4753 /* Set fan to at least level 4 */
4754 s |= 4;
4755
4756 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004757 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004758 else
4759 rc = 0;
4760 break;
4761
4762 default:
4763 rc = -ENXIO;
4764 }
4765
4766 mutex_unlock(&fan_mutex);
4767 return rc;
4768}
4769
4770static int fan_set_disable(void)
4771{
4772 int rc;
4773
4774 if (!fan_control_allowed)
4775 return -EPERM;
4776
4777 if (mutex_lock_interruptible(&fan_mutex))
4778 return -ERESTARTSYS;
4779
4780 rc = 0;
4781 switch (fan_control_access_mode) {
4782 case TPACPI_FAN_WR_ACPI_FANS:
4783 case TPACPI_FAN_WR_TPEC:
4784 if (!acpi_ec_write(fan_status_offset, 0x00))
4785 rc = -EIO;
4786 else {
4787 fan_control_desired_level = 0;
4788 tp_features.fan_ctrl_status_undef = 0;
4789 }
4790 break;
4791
4792 case TPACPI_FAN_WR_ACPI_SFAN:
4793 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4794 rc = -EIO;
4795 else
4796 fan_control_desired_level = 0;
4797 break;
4798
4799 default:
4800 rc = -ENXIO;
4801 }
4802
4803
4804 mutex_unlock(&fan_mutex);
4805 return rc;
4806}
4807
4808static int fan_set_speed(int speed)
4809{
4810 int rc;
4811
4812 if (!fan_control_allowed)
4813 return -EPERM;
4814
4815 if (mutex_lock_interruptible(&fan_mutex))
4816 return -ERESTARTSYS;
4817
4818 rc = 0;
4819 switch (fan_control_access_mode) {
4820 case TPACPI_FAN_WR_ACPI_FANS:
4821 if (speed >= 0 && speed <= 65535) {
4822 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4823 speed, speed, speed))
4824 rc = -EIO;
4825 } else
4826 rc = -EINVAL;
4827 break;
4828
4829 default:
4830 rc = -ENXIO;
4831 }
4832
4833 mutex_unlock(&fan_mutex);
4834 return rc;
4835}
4836
4837static void fan_watchdog_reset(void)
4838{
4839 static int fan_watchdog_active;
4840
4841 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4842 return;
4843
4844 if (fan_watchdog_active)
4845 cancel_delayed_work(&fan_watchdog_task);
4846
4847 if (fan_watchdog_maxinterval > 0 &&
4848 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4849 fan_watchdog_active = 1;
4850 if (!schedule_delayed_work(&fan_watchdog_task,
4851 msecs_to_jiffies(fan_watchdog_maxinterval
4852 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004853 printk(TPACPI_ERR
4854 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004855 "watchdog will not trigger\n");
4856 }
4857 } else
4858 fan_watchdog_active = 0;
4859}
4860
4861static void fan_watchdog_fire(struct work_struct *ignored)
4862{
4863 int rc;
4864
4865 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4866 return;
4867
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004868 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004869 rc = fan_set_enable();
4870 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004871 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004872 "will try again later...\n", -rc);
4873 /* reschedule for later */
4874 fan_watchdog_reset();
4875 }
4876}
4877
4878/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004879 * SYSFS fan layout: hwmon compatible (device)
4880 *
4881 * pwm*_enable:
4882 * 0: "disengaged" mode
4883 * 1: manual mode
4884 * 2: native EC "auto" mode (recommended, hardware default)
4885 *
4886 * pwm*: set speed in manual mode, ignored otherwise.
4887 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4888 * interpolation.
4889 *
4890 * fan*_input: tachometer reading, RPM
4891 *
4892 *
4893 * SYSFS fan layout: extensions
4894 *
4895 * fan_watchdog (driver):
4896 * fan watchdog interval in seconds, 0 disables (default), max 120
4897 */
4898
4899/* sysfs fan pwm1_enable ----------------------------------------------- */
4900static ssize_t fan_pwm1_enable_show(struct device *dev,
4901 struct device_attribute *attr,
4902 char *buf)
4903{
4904 int res, mode;
4905 u8 status;
4906
4907 res = fan_get_status_safe(&status);
4908 if (res)
4909 return res;
4910
4911 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4912 if (status != fan_control_initial_status) {
4913 tp_features.fan_ctrl_status_undef = 0;
4914 } else {
4915 /* Return most likely status. In fact, it
4916 * might be the only possible status */
4917 status = TP_EC_FAN_AUTO;
4918 }
4919 }
4920
4921 if (status & TP_EC_FAN_FULLSPEED) {
4922 mode = 0;
4923 } else if (status & TP_EC_FAN_AUTO) {
4924 mode = 2;
4925 } else
4926 mode = 1;
4927
4928 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4929}
4930
4931static ssize_t fan_pwm1_enable_store(struct device *dev,
4932 struct device_attribute *attr,
4933 const char *buf, size_t count)
4934{
4935 unsigned long t;
4936 int res, level;
4937
4938 if (parse_strtoul(buf, 2, &t))
4939 return -EINVAL;
4940
4941 switch (t) {
4942 case 0:
4943 level = TP_EC_FAN_FULLSPEED;
4944 break;
4945 case 1:
4946 level = TPACPI_FAN_LAST_LEVEL;
4947 break;
4948 case 2:
4949 level = TP_EC_FAN_AUTO;
4950 break;
4951 case 3:
4952 /* reserved for software-controlled auto mode */
4953 return -ENOSYS;
4954 default:
4955 return -EINVAL;
4956 }
4957
4958 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03004959 if (res == -ENXIO)
4960 return -EINVAL;
4961 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004962 return res;
4963
4964 fan_watchdog_reset();
4965
4966 return count;
4967}
4968
4969static struct device_attribute dev_attr_fan_pwm1_enable =
4970 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4971 fan_pwm1_enable_show, fan_pwm1_enable_store);
4972
4973/* sysfs fan pwm1 ------------------------------------------------------ */
4974static ssize_t fan_pwm1_show(struct device *dev,
4975 struct device_attribute *attr,
4976 char *buf)
4977{
4978 int res;
4979 u8 status;
4980
4981 res = fan_get_status_safe(&status);
4982 if (res)
4983 return res;
4984
4985 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4986 if (status != fan_control_initial_status) {
4987 tp_features.fan_ctrl_status_undef = 0;
4988 } else {
4989 status = TP_EC_FAN_AUTO;
4990 }
4991 }
4992
4993 if ((status &
4994 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4995 status = fan_control_desired_level;
4996
4997 if (status > 7)
4998 status = 7;
4999
5000 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5001}
5002
5003static ssize_t fan_pwm1_store(struct device *dev,
5004 struct device_attribute *attr,
5005 const char *buf, size_t count)
5006{
5007 unsigned long s;
5008 int rc;
5009 u8 status, newlevel;
5010
5011 if (parse_strtoul(buf, 255, &s))
5012 return -EINVAL;
5013
5014 /* scale down from 0-255 to 0-7 */
5015 newlevel = (s >> 5) & 0x07;
5016
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005017 if (mutex_lock_interruptible(&fan_mutex))
5018 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005019
5020 rc = fan_get_status(&status);
5021 if (!rc && (status &
5022 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5023 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005024 if (rc == -ENXIO)
5025 rc = -EINVAL;
5026 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005027 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005028 fan_watchdog_reset();
5029 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005030 }
5031
5032 mutex_unlock(&fan_mutex);
5033 return (rc)? rc : count;
5034}
5035
5036static struct device_attribute dev_attr_fan_pwm1 =
5037 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5038 fan_pwm1_show, fan_pwm1_store);
5039
5040/* sysfs fan fan1_input ------------------------------------------------ */
5041static ssize_t fan_fan1_input_show(struct device *dev,
5042 struct device_attribute *attr,
5043 char *buf)
5044{
5045 int res;
5046 unsigned int speed;
5047
5048 res = fan_get_speed(&speed);
5049 if (res < 0)
5050 return res;
5051
5052 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5053}
5054
5055static struct device_attribute dev_attr_fan_fan1_input =
5056 __ATTR(fan1_input, S_IRUGO,
5057 fan_fan1_input_show, NULL);
5058
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005059/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005060static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5061 char *buf)
5062{
5063 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5064}
5065
5066static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5067 const char *buf, size_t count)
5068{
5069 unsigned long t;
5070
5071 if (parse_strtoul(buf, 120, &t))
5072 return -EINVAL;
5073
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005074 if (!fan_control_allowed)
5075 return -EPERM;
5076
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005077 fan_watchdog_maxinterval = t;
5078 fan_watchdog_reset();
5079
5080 return count;
5081}
5082
5083static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5084 fan_fan_watchdog_show, fan_fan_watchdog_store);
5085
5086/* --------------------------------------------------------------------- */
5087static struct attribute *fan_attributes[] = {
5088 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5089 &dev_attr_fan_fan1_input.attr,
5090 NULL
5091};
5092
5093static const struct attribute_group fan_attr_group = {
5094 .attrs = fan_attributes,
5095};
5096
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005097static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005098{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005099 int rc;
5100
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005101 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5102
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005103 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005104 fan_status_access_mode = TPACPI_FAN_NONE;
5105 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005106 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005107 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005108 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005109 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005110
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005111 TPACPI_ACPIHANDLE_INIT(fans);
5112 TPACPI_ACPIHANDLE_INIT(gfan);
5113 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005114
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005115 if (gfan_handle) {
5116 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005117 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005118 } else {
5119 /* all other ThinkPads: note that even old-style
5120 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005121 if (likely(acpi_ec_read(fan_status_offset,
5122 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005123 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005124
5125 /* In some ThinkPads, neither the EC nor the ACPI
5126 * DSDT initialize the fan status, and it ends up
5127 * being set to 0x07 when it *could* be either
5128 * 0x07 or 0x80.
5129 *
5130 * Enable for TP-1Y (T43), TP-78 (R51e),
5131 * TP-76 (R52), TP-70 (T43, R52), which are known
5132 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005133 if (fan_control_initial_status == 0x07) {
5134 switch (thinkpad_id.ec_model) {
5135 case 0x5931: /* TP-1Y */
5136 case 0x3837: /* TP-78 */
5137 case 0x3637: /* TP-76 */
5138 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005139 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005140 "fan_init: initial fan status "
5141 "is unknown, assuming it is "
5142 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005143 tp_features.fan_ctrl_status_undef = 1;
5144 ;;
5145 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005146 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005147 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005148 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005149 "ThinkPad ACPI EC access misbehaving, "
5150 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005151 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005152 }
5153 }
5154
5155 if (sfan_handle) {
5156 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005157 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005158 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005159 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005160 } else {
5161 if (!gfan_handle) {
5162 /* gfan without sfan means no fan control */
5163 /* all other models implement TP EC 0x2f control */
5164
5165 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005166 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005167 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005168 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005169 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005170 TPACPI_FAN_CMD_SPEED |
5171 TPACPI_FAN_CMD_LEVEL |
5172 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005173 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005174 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005175 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005176 TPACPI_FAN_CMD_LEVEL |
5177 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005178 }
5179 }
5180 }
5181
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005182 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5183 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5184 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5185 fan_status_access_mode, fan_control_access_mode);
5186
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005187 /* fan control master switch */
5188 if (!fan_control_allowed) {
5189 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5190 fan_control_commands = 0;
5191 dbg_printk(TPACPI_DBG_INIT,
5192 "fan control features disabled by parameter\n");
5193 }
5194
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005195 /* update fan_control_desired_level */
5196 if (fan_status_access_mode != TPACPI_FAN_NONE)
5197 fan_get_status_safe(NULL);
5198
5199 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5200 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005201 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005202 &fan_attr_group);
5203 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005204 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005205 &driver_attr_fan_watchdog);
5206 if (rc < 0)
5207 return rc;
5208 return 0;
5209 } else
5210 return 1;
5211}
5212
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005213static void fan_exit(void)
5214{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005215 vdbg_printk(TPACPI_DBG_EXIT,
5216 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005217
5218 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005219 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005220 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5221 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005222
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005223 cancel_delayed_work(&fan_watchdog_task);
5224 flush_scheduled_work();
5225}
5226
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005227static int fan_read(char *p)
5228{
5229 int len = 0;
5230 int rc;
5231 u8 status;
5232 unsigned int speed = 0;
5233
5234 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005235 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005236 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005237 rc = fan_get_status_safe(&status);
5238 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005239 return rc;
5240
5241 len += sprintf(p + len, "status:\t\t%s\n"
5242 "level:\t\t%d\n",
5243 (status != 0) ? "enabled" : "disabled", status);
5244 break;
5245
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005246 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005247 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005248 rc = fan_get_status_safe(&status);
5249 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005250 return rc;
5251
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005252 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005253 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005254 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005255 else
5256 /* Return most likely status. In fact, it
5257 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005258 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005259 }
5260
5261 len += sprintf(p + len, "status:\t\t%s\n",
5262 (status != 0) ? "enabled" : "disabled");
5263
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005264 rc = fan_get_speed(&speed);
5265 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005266 return rc;
5267
5268 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5269
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005270 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005271 /* Disengaged mode takes precedence */
5272 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005273 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005274 len += sprintf(p + len, "level:\t\tauto\n");
5275 else
5276 len += sprintf(p + len, "level:\t\t%d\n", status);
5277 break;
5278
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005279 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005280 default:
5281 len += sprintf(p + len, "status:\t\tnot supported\n");
5282 }
5283
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005284 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005285 len += sprintf(p + len, "commands:\tlevel <level>");
5286
5287 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005288 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005289 len += sprintf(p + len, " (<level> is 0-7)\n");
5290 break;
5291
5292 default:
5293 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005294 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005295 break;
5296 }
5297 }
5298
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005299 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005300 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005301 "commands:\twatchdog <timeout> (<timeout> "
5302 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005303
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005304 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005305 len += sprintf(p + len, "commands:\tspeed <speed>"
5306 " (<speed> is 0-65535)\n");
5307
5308 return len;
5309}
5310
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005311static int fan_write_cmd_level(const char *cmd, int *rc)
5312{
5313 int level;
5314
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005315 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005316 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005317 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005318 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005319 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005320 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005321 return 0;
5322
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005323 *rc = fan_set_level_safe(level);
5324 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005325 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005326 "access mode %d", fan_control_access_mode);
5327
5328 return 1;
5329}
5330
5331static int fan_write_cmd_enable(const char *cmd, int *rc)
5332{
5333 if (strlencmp(cmd, "enable") != 0)
5334 return 0;
5335
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005336 *rc = fan_set_enable();
5337 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005338 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005339 "access mode %d", fan_control_access_mode);
5340
5341 return 1;
5342}
5343
5344static int fan_write_cmd_disable(const char *cmd, int *rc)
5345{
5346 if (strlencmp(cmd, "disable") != 0)
5347 return 0;
5348
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005349 *rc = fan_set_disable();
5350 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005351 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005352 "access mode %d", fan_control_access_mode);
5353
5354 return 1;
5355}
5356
5357static int fan_write_cmd_speed(const char *cmd, int *rc)
5358{
5359 int speed;
5360
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005361 /* TODO:
5362 * Support speed <low> <medium> <high> ? */
5363
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005364 if (sscanf(cmd, "speed %d", &speed) != 1)
5365 return 0;
5366
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005367 *rc = fan_set_speed(speed);
5368 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005369 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005370 "access mode %d", fan_control_access_mode);
5371
5372 return 1;
5373}
5374
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005375static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5376{
5377 int interval;
5378
5379 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5380 return 0;
5381
5382 if (interval < 0 || interval > 120)
5383 *rc = -EINVAL;
5384 else
5385 fan_watchdog_maxinterval = interval;
5386
5387 return 1;
5388}
5389
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005390static int fan_write(char *buf)
5391{
5392 char *cmd;
5393 int rc = 0;
5394
5395 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005396 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005397 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005398 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005399 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005400 fan_write_cmd_disable(cmd, &rc) ||
5401 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005402 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005403 fan_write_cmd_speed(cmd, &rc))
5404 )
5405 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005406 else if (!rc)
5407 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005408 }
5409
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005410 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005411}
5412
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005413static struct ibm_struct fan_driver_data = {
5414 .name = "fan",
5415 .read = fan_read,
5416 .write = fan_write,
5417 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005418};
5419
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005420/****************************************************************************
5421 ****************************************************************************
5422 *
5423 * Infrastructure
5424 *
5425 ****************************************************************************
5426 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005428/* sysfs name ---------------------------------------------------------- */
5429static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5430 struct device_attribute *attr,
5431 char *buf)
5432{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005433 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005434}
5435
5436static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5437 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5438
5439/* --------------------------------------------------------------------- */
5440
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005441/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005442static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005443
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005444/*
5445 * Module and infrastructure proble, init and exit handling
5446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005448static int force_load;
5449
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005450#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005451static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005452{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005453 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005454
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005455 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005456}
5457#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5458
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005459static void ibm_exit(struct ibm_struct *ibm)
5460{
5461 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5462
5463 list_del_init(&ibm->all_drivers);
5464
5465 if (ibm->flags.acpi_notify_installed) {
5466 dbg_printk(TPACPI_DBG_EXIT,
5467 "%s: acpi_remove_notify_handler\n", ibm->name);
5468 BUG_ON(!ibm->acpi);
5469 acpi_remove_notify_handler(*ibm->acpi->handle,
5470 ibm->acpi->type,
5471 dispatch_acpi_notify);
5472 ibm->flags.acpi_notify_installed = 0;
5473 ibm->flags.acpi_notify_installed = 0;
5474 }
5475
5476 if (ibm->flags.proc_created) {
5477 dbg_printk(TPACPI_DBG_EXIT,
5478 "%s: remove_proc_entry\n", ibm->name);
5479 remove_proc_entry(ibm->name, proc_dir);
5480 ibm->flags.proc_created = 0;
5481 }
5482
5483 if (ibm->flags.acpi_driver_registered) {
5484 dbg_printk(TPACPI_DBG_EXIT,
5485 "%s: acpi_bus_unregister_driver\n", ibm->name);
5486 BUG_ON(!ibm->acpi);
5487 acpi_bus_unregister_driver(ibm->acpi->driver);
5488 kfree(ibm->acpi->driver);
5489 ibm->acpi->driver = NULL;
5490 ibm->flags.acpi_driver_registered = 0;
5491 }
5492
5493 if (ibm->flags.init_called && ibm->exit) {
5494 ibm->exit();
5495 ibm->flags.init_called = 0;
5496 }
5497
5498 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5499}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005500
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005501static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502{
5503 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005504 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 struct proc_dir_entry *entry;
5506
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005507 BUG_ON(ibm == NULL);
5508
5509 INIT_LIST_HEAD(&ibm->all_drivers);
5510
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005511 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 return 0;
5513
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005514 dbg_printk(TPACPI_DBG_INIT,
5515 "probing for %s\n", ibm->name);
5516
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005517 if (iibm->init) {
5518 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005519 if (ret > 0)
5520 return 0; /* probe failed */
5521 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005523
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005524 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 }
5526
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005527 if (ibm->acpi) {
5528 if (ibm->acpi->hid) {
5529 ret = register_tpacpi_subdriver(ibm);
5530 if (ret)
5531 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005532 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005533
5534 if (ibm->acpi->notify) {
5535 ret = setup_acpi_notify(ibm);
5536 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005537 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005538 ibm->name);
5539 ret = 0;
5540 goto err_out;
5541 }
5542 if (ret < 0)
5543 goto err_out;
5544 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005545 }
5546
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005547 dbg_printk(TPACPI_DBG_INIT,
5548 "%s installed\n", ibm->name);
5549
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005550 if (ibm->read) {
5551 entry = create_proc_entry(ibm->name,
5552 S_IFREG | S_IRUGO | S_IWUSR,
5553 proc_dir);
5554 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005555 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005556 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005557 ret = -ENODEV;
5558 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005559 }
5560 entry->owner = THIS_MODULE;
5561 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005562 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005563 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005564 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005565 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005568 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5569
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005571
5572err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005573 dbg_printk(TPACPI_DBG_INIT,
5574 "%s: at error exit path with result %d\n",
5575 ibm->name, ret);
5576
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005577 ibm_exit(ibm);
5578 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579}
5580
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005581/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005583static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005584{
Jeff Garzik18552562007-10-03 15:15:40 -04005585 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005586 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005587
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005588 if (!tp)
5589 return;
5590
5591 memset(tp, 0, sizeof(*tp));
5592
5593 if (dmi_name_in_vendors("IBM"))
5594 tp->vendor = PCI_VENDOR_ID_IBM;
5595 else if (dmi_name_in_vendors("LENOVO"))
5596 tp->vendor = PCI_VENDOR_ID_LENOVO;
5597 else
5598 return;
5599
5600 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5601 GFP_KERNEL);
5602 if (!tp->bios_version_str)
5603 return;
5604 tp->bios_model = tp->bios_version_str[0]
5605 | (tp->bios_version_str[1] << 8);
5606
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005607 /*
5608 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5609 * X32 or newer, all Z series; Some models must have an
5610 * up-to-date BIOS or they will not be detected.
5611 *
5612 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5613 */
5614 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005615 if (sscanf(dev->name,
5616 "IBM ThinkPad Embedded Controller -[%17c",
5617 ec_fw_string) == 1) {
5618 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5619 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005620
5621 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5622 tp->ec_model = ec_fw_string[0]
5623 | (ec_fw_string[1] << 8);
5624 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005625 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005626 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005627
5628 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5629 GFP_KERNEL);
5630 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5631 kfree(tp->model_str);
5632 tp->model_str = NULL;
5633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634}
5635
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005636static int __init probe_for_thinkpad(void)
5637{
5638 int is_thinkpad;
5639
5640 if (acpi_disabled)
5641 return -ENODEV;
5642
5643 /*
5644 * Non-ancient models have better DMI tagging, but very old models
5645 * don't.
5646 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005647 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005648
5649 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005650 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005651 if (!ec_handle) {
5652 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005653 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005654 "Not yet supported ThinkPad detected!\n");
5655 return -ENODEV;
5656 }
5657
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005658 /*
5659 * Risks a regression on very old machines, but reduces potential
5660 * false positives a damn great deal
5661 */
5662 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005663 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005664
5665 if (!is_thinkpad && !force_load)
5666 return -ENODEV;
5667
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005668 return 0;
5669}
5670
5671
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005672/* Module init, exit, parameters */
5673
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005674static struct ibm_init_struct ibms_init[] __initdata = {
5675 {
5676 .init = thinkpad_acpi_driver_init,
5677 .data = &thinkpad_acpi_driver_data,
5678 },
5679 {
5680 .init = hotkey_init,
5681 .data = &hotkey_driver_data,
5682 },
5683 {
5684 .init = bluetooth_init,
5685 .data = &bluetooth_driver_data,
5686 },
5687 {
5688 .init = wan_init,
5689 .data = &wan_driver_data,
5690 },
5691 {
5692 .init = video_init,
5693 .data = &video_driver_data,
5694 },
5695 {
5696 .init = light_init,
5697 .data = &light_driver_data,
5698 },
5699#ifdef CONFIG_THINKPAD_ACPI_DOCK
5700 {
5701 .init = dock_init,
5702 .data = &dock_driver_data[0],
5703 },
5704 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005705 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005706 .data = &dock_driver_data[1],
5707 },
5708#endif
5709#ifdef CONFIG_THINKPAD_ACPI_BAY
5710 {
5711 .init = bay_init,
5712 .data = &bay_driver_data,
5713 },
5714#endif
5715 {
5716 .init = cmos_init,
5717 .data = &cmos_driver_data,
5718 },
5719 {
5720 .init = led_init,
5721 .data = &led_driver_data,
5722 },
5723 {
5724 .init = beep_init,
5725 .data = &beep_driver_data,
5726 },
5727 {
5728 .init = thermal_init,
5729 .data = &thermal_driver_data,
5730 },
5731 {
5732 .data = &ecdump_driver_data,
5733 },
5734 {
5735 .init = brightness_init,
5736 .data = &brightness_driver_data,
5737 },
5738 {
5739 .data = &volume_driver_data,
5740 },
5741 {
5742 .init = fan_init,
5743 .data = &fan_driver_data,
5744 },
5745};
5746
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005747static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5748{
5749 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005750 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005751
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005752 if (!kp || !kp->name || !val)
5753 return -EINVAL;
5754
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005755 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5756 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005757 WARN_ON(ibm == NULL);
5758
5759 if (!ibm || !ibm->name)
5760 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005761
5762 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5763 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005764 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005765 strcpy(ibms_init[i].param, val);
5766 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005767 return 0;
5768 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005769 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005770
5771 return -EINVAL;
5772}
5773
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005774module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005775MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005776 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005777
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005778module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005779MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005780
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005781module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005782MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005783 "Attempts to load the driver even on a "
5784 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005785
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005786module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005787MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005788 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005789
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005790module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005791MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005792 "Selects brightness control strategy: "
5793 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005794
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005795module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005796MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005797 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005798
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005799module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005800MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005801 "used for backwards compatibility with userspace, "
5802 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005803
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005804#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005805 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5806 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005807 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005808
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005809TPACPI_PARAM(hotkey);
5810TPACPI_PARAM(bluetooth);
5811TPACPI_PARAM(video);
5812TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005813#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005814TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005815#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005816#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005817TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005818#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005819TPACPI_PARAM(cmos);
5820TPACPI_PARAM(led);
5821TPACPI_PARAM(beep);
5822TPACPI_PARAM(ecdump);
5823TPACPI_PARAM(brightness);
5824TPACPI_PARAM(volume);
5825TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005826
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005827static void thinkpad_acpi_module_exit(void)
5828{
5829 struct ibm_struct *ibm, *itmp;
5830
5831 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5832
5833 list_for_each_entry_safe_reverse(ibm, itmp,
5834 &tpacpi_all_drivers,
5835 all_drivers) {
5836 ibm_exit(ibm);
5837 }
5838
5839 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5840
5841 if (tpacpi_inputdev) {
5842 if (tp_features.input_device_registered)
5843 input_unregister_device(tpacpi_inputdev);
5844 else
5845 input_free_device(tpacpi_inputdev);
5846 }
5847
5848 if (tpacpi_hwmon)
5849 hwmon_device_unregister(tpacpi_hwmon);
5850
5851 if (tp_features.sensors_pdev_attrs_registered)
5852 device_remove_file(&tpacpi_sensors_pdev->dev,
5853 &dev_attr_thinkpad_acpi_pdev_name);
5854 if (tpacpi_sensors_pdev)
5855 platform_device_unregister(tpacpi_sensors_pdev);
5856 if (tpacpi_pdev)
5857 platform_device_unregister(tpacpi_pdev);
5858
5859 if (tp_features.sensors_pdrv_attrs_registered)
5860 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5861 if (tp_features.platform_drv_attrs_registered)
5862 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5863
5864 if (tp_features.sensors_pdrv_registered)
5865 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5866
5867 if (tp_features.platform_drv_registered)
5868 platform_driver_unregister(&tpacpi_pdriver);
5869
5870 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005871 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005872
5873 kfree(thinkpad_id.bios_version_str);
5874 kfree(thinkpad_id.ec_version_str);
5875 kfree(thinkpad_id.model_str);
5876}
5877
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005878
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005879static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880{
5881 int ret, i;
5882
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005883 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5884
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005885 /* Parameter checking */
5886 if (hotkey_report_mode > 2)
5887 return -EINVAL;
5888
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005889 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005890
5891 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005892 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005893 if (ret) {
5894 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005895 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005898 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005899
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005900 TPACPI_ACPIHANDLE_INIT(ecrd);
5901 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005903 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005905 printk(TPACPI_ERR
5906 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005907 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 return -ENODEV;
5909 }
5910 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005911
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005912 ret = platform_driver_register(&tpacpi_pdriver);
5913 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005914 printk(TPACPI_ERR
5915 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005916 thinkpad_acpi_module_exit();
5917 return ret;
5918 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005919 tp_features.platform_drv_registered = 1;
5920
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005921 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5922 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005923 printk(TPACPI_ERR
5924 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005925 thinkpad_acpi_module_exit();
5926 return ret;
5927 }
5928 tp_features.sensors_pdrv_registered = 1;
5929
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005930 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005931 if (!ret) {
5932 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005933 ret = tpacpi_create_driver_attributes(
5934 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005935 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005936 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005937 printk(TPACPI_ERR
5938 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005939 thinkpad_acpi_module_exit();
5940 return ret;
5941 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005942 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005943
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005944
5945 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005946 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005947 NULL, 0);
5948 if (IS_ERR(tpacpi_pdev)) {
5949 ret = PTR_ERR(tpacpi_pdev);
5950 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005951 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005952 thinkpad_acpi_module_exit();
5953 return ret;
5954 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005955 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005956 TPACPI_HWMON_DRVR_NAME,
5957 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005958 if (IS_ERR(tpacpi_sensors_pdev)) {
5959 ret = PTR_ERR(tpacpi_sensors_pdev);
5960 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005961 printk(TPACPI_ERR
5962 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005963 thinkpad_acpi_module_exit();
5964 return ret;
5965 }
5966 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5967 &dev_attr_thinkpad_acpi_pdev_name);
5968 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005969 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005970 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005971 thinkpad_acpi_module_exit();
5972 return ret;
5973 }
5974 tp_features.sensors_pdev_attrs_registered = 1;
5975 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005976 if (IS_ERR(tpacpi_hwmon)) {
5977 ret = PTR_ERR(tpacpi_hwmon);
5978 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005979 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005980 thinkpad_acpi_module_exit();
5981 return ret;
5982 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03005983 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005984 tpacpi_inputdev = input_allocate_device();
5985 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005986 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005987 thinkpad_acpi_module_exit();
5988 return -ENOMEM;
5989 } else {
5990 /* Prepare input device, but don't register */
5991 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005992 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005993 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03005994 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5995 thinkpad_id.vendor :
5996 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005997 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5998 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5999 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006000 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6001 ret = ibm_init(&ibms_init[i]);
6002 if (ret >= 0 && *ibms_init[i].param)
6003 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006005 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 return ret;
6007 }
6008 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006009 ret = input_register_device(tpacpi_inputdev);
6010 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006011 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006012 thinkpad_acpi_module_exit();
6013 return ret;
6014 } else {
6015 tp_features.input_device_registered = 1;
6016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006018 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 return 0;
6020}
6021
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006022/* Please remove this in year 2009 */
6023MODULE_ALIAS("ibm_acpi");
6024
6025/*
6026 * DMI matching for module autoloading
6027 *
6028 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6029 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6030 *
6031 * Only models listed in thinkwiki will be supported, so add yours
6032 * if it is not there yet.
6033 */
6034#define IBM_BIOS_MODULE_ALIAS(__type) \
6035 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6036
6037/* Non-ancient thinkpads */
6038MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6039MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6040
6041/* Ancient thinkpad BIOSes have to be identified by
6042 * BIOS type or model number, and there are far less
6043 * BIOS types than model numbers... */
6044IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6045IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6046IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6047
6048MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006049MODULE_DESCRIPTION(TPACPI_DESC);
6050MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006051MODULE_LICENSE("GPL");
6052
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006053module_init(thinkpad_acpi_module_init);
6054module_exit(thinkpad_acpi_module_exit);