blob: 9b0235dc5308df13191ce8697271c7bc61aa621c [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 Holschuh94954cc2007-07-18 23:45:27 -0300273static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200275#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 static acpi_handle object##_handle; \
277 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400278 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 static char *object##_paths[] = { paths }
280
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200281TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400282 "\\_SB.PCI.ISA.EC", /* 570 */
283 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
284 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
285 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
286 "\\_SB.PCI0.ICH3.EC0", /* R31 */
287 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300288 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200290TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
291TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200293TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
294 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400295 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
296 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300297 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400298
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200299TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400300 "^HKEY", /* R30, R31 */
301 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300302 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400303
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400304
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300305/*************************************************************************
306 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200307 */
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static int acpi_evalf(acpi_handle handle,
310 void *res, char *method, char *fmt, ...)
311{
312 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400313 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200314 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400315 struct acpi_buffer result, *resultp;
316 union acpi_object out_obj;
317 acpi_status status;
318 va_list ap;
319 char res_type;
320 int success;
321 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200324 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326 }
327
328 if (*fmt == 'q') {
329 quiet = 1;
330 fmt++;
331 } else
332 quiet = 0;
333
334 res_type = *(fmt++);
335
336 params.count = 0;
337 params.pointer = &in_objs[0];
338
339 va_start(ap, fmt);
340 while (*fmt) {
341 char c = *(fmt++);
342 switch (c) {
343 case 'd': /* int */
344 in_objs[params.count].integer.value = va_arg(ap, int);
345 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
346 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400347 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200349 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 "with invalid format character '%c'\n", c);
351 return 0;
352 }
353 }
354 va_end(ap);
355
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400356 if (res_type != 'v') {
357 result.length = sizeof(out_obj);
358 result.pointer = &out_obj;
359 resultp = &result;
360 } else
361 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400363 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400366 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (res)
368 *(int *)res = out_obj.integer.value;
369 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
370 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400371 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 success = status == AE_OK;
373 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400374 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200376 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 "with invalid format character '%c'\n", res_type);
378 return 0;
379 }
380
381 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200382 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 method, fmt0, status);
384
385 return success;
386}
387
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200388static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300389{
390 int v;
391
392 if (ecrd_handle) {
393 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
394 return 0;
395 *p = v;
396 } else {
397 if (ec_read(i, p) < 0)
398 return 0;
399 }
400
401 return 1;
402}
403
404static int acpi_ec_write(int i, u8 v)
405{
406 if (ecwr_handle) {
407 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
408 return 0;
409 } else {
410 if (ec_write(i, v) < 0)
411 return 0;
412 }
413
414 return 1;
415}
416
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 Holschuh94954cc2007-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
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001021static enum { /* Reasons for waking up */
1022 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1023 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1024 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1025} hotkey_wakeup_reason;
1026
1027static int hotkey_autosleep_ack;
1028
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001029static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001030static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001031static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001032static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001033static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001034
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001035static unsigned int hotkey_report_mode;
1036
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001037static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001038
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03001039static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001040
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001041#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1042#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001043 do { \
1044 mutex_lock(&hotkey_thread_data_mutex); \
1045 hotkey_config_change++; \
1046 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001047#define HOTKEY_CONFIG_CRITICAL_END \
1048 mutex_unlock(&hotkey_thread_data_mutex);
1049#else
1050#define HOTKEY_CONFIG_CRITICAL_START
1051#define HOTKEY_CONFIG_CRITICAL_END
1052#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1053
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001054static int hotkey_get_wlsw(int *status)
1055{
1056 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1057 return -EIO;
1058 return 0;
1059}
1060
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001061/*
1062 * Call with hotkey_mutex held
1063 */
1064static int hotkey_mask_get(void)
1065{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001066 u32 m = 0;
1067
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001068 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001069 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001070 return -EIO;
1071 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001072 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001073
1074 return 0;
1075}
1076
1077/*
1078 * Call with hotkey_mutex held
1079 */
1080static int hotkey_mask_set(u32 mask)
1081{
1082 int i;
1083 int rc = 0;
1084
1085 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001086 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001087 for (i = 0; i < 32; i++) {
1088 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001089 /* enable in firmware mask only keys not in NVRAM
1090 * mode, but enable the key in the cached hotkey_mask
1091 * regardless of mode, or the key will end up
1092 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001093 if (!acpi_evalf(hkey_handle,
1094 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001095 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001096 rc = -EIO;
1097 break;
1098 } else {
1099 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1100 }
1101 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001102 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001103
1104 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001105 if (!hotkey_mask_get() && !rc &&
1106 (hotkey_mask & ~hotkey_source_mask) !=
1107 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001108 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001109 "requested hot key mask 0x%08x, but "
1110 "firmware forced it to 0x%08x\n",
1111 mask, hotkey_mask);
1112 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001113 } else {
1114#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1115 HOTKEY_CONFIG_CRITICAL_START
1116 hotkey_mask = mask & hotkey_source_mask;
1117 HOTKEY_CONFIG_CRITICAL_END
1118 hotkey_mask_get();
1119 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001120 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001121 "requested hot key mask 0x%08x, "
1122 "forced to 0x%08x (NVRAM poll mask is "
1123 "0x%08x): no firmware mask support\n",
1124 mask, hotkey_mask, hotkey_source_mask);
1125 }
1126#else
1127 hotkey_mask_get();
1128 rc = -ENXIO;
1129#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001130 }
1131
1132 return rc;
1133}
1134
1135static int hotkey_status_get(int *status)
1136{
1137 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1138 return -EIO;
1139
1140 return 0;
1141}
1142
1143static int hotkey_status_set(int status)
1144{
1145 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1146 return -EIO;
1147
1148 return 0;
1149}
1150
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001151static void tpacpi_input_send_radiosw(void)
1152{
1153 int wlsw;
1154
1155 mutex_lock(&tpacpi_inputdev_send_mutex);
1156
1157 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1158 input_report_switch(tpacpi_inputdev,
1159 SW_RADIO, !!wlsw);
1160 input_sync(tpacpi_inputdev);
1161 }
1162
1163 mutex_unlock(&tpacpi_inputdev_send_mutex);
1164}
1165
1166static void tpacpi_input_send_key(unsigned int scancode)
1167{
1168 unsigned int keycode;
1169
1170 keycode = hotkey_keycode_map[scancode];
1171
1172 if (keycode != KEY_RESERVED) {
1173 mutex_lock(&tpacpi_inputdev_send_mutex);
1174
1175 input_report_key(tpacpi_inputdev, keycode, 1);
1176 if (keycode == KEY_UNKNOWN)
1177 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1178 scancode);
1179 input_sync(tpacpi_inputdev);
1180
1181 input_report_key(tpacpi_inputdev, keycode, 0);
1182 if (keycode == KEY_UNKNOWN)
1183 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1184 scancode);
1185 input_sync(tpacpi_inputdev);
1186
1187 mutex_unlock(&tpacpi_inputdev_send_mutex);
1188 }
1189}
1190
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001191#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1192static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1193
1194static void tpacpi_hotkey_send_key(unsigned int scancode)
1195{
1196 tpacpi_input_send_key(scancode);
1197 if (hotkey_report_mode < 2) {
1198 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1199 0x80, 0x1001 + scancode);
1200 }
1201}
1202
1203static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1204{
1205 u8 d;
1206
1207 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1208 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1209 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1210 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1211 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1212 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1213 }
1214 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1215 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1216 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1217 }
1218 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1219 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1220 n->displayexp_toggle =
1221 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1222 }
1223 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1224 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1225 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1226 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1227 n->brightness_toggle =
1228 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1229 }
1230 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1231 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1232 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1233 >> TP_NVRAM_POS_LEVEL_VOLUME;
1234 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1235 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1236 }
1237}
1238
1239#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001240 do { \
1241 if ((mask & (1 << __scancode)) && \
1242 oldn->__member != newn->__member) \
1243 tpacpi_hotkey_send_key(__scancode); \
1244 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001245
1246#define TPACPI_MAY_SEND_KEY(__scancode) \
1247 do { if (mask & (1 << __scancode)) \
1248 tpacpi_hotkey_send_key(__scancode); } while (0)
1249
1250static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001251 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001252 u32 mask)
1253{
1254 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1255 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1256 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1257 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1258
1259 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1260
1261 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1262
1263 /* handle volume */
1264 if (oldn->volume_toggle != newn->volume_toggle) {
1265 if (oldn->mute != newn->mute) {
1266 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1267 }
1268 if (oldn->volume_level > newn->volume_level) {
1269 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1270 } else if (oldn->volume_level < newn->volume_level) {
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1272 } else if (oldn->mute == newn->mute) {
1273 /* repeated key presses that didn't change state */
1274 if (newn->mute) {
1275 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1276 } else if (newn->volume_level != 0) {
1277 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1278 } else {
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1280 }
1281 }
1282 }
1283
1284 /* handle brightness */
1285 if (oldn->brightness_toggle != newn->brightness_toggle) {
1286 if (oldn->brightness_level < newn->brightness_level) {
1287 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1288 } else if (oldn->brightness_level > newn->brightness_level) {
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1290 } else {
1291 /* repeated key presses that didn't change state */
1292 if (newn->brightness_level != 0) {
1293 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1294 } else {
1295 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1296 }
1297 }
1298 }
1299}
1300
1301#undef TPACPI_COMPARE_KEY
1302#undef TPACPI_MAY_SEND_KEY
1303
1304static int hotkey_kthread(void *data)
1305{
1306 struct tp_nvram_state s[2];
1307 u32 mask;
1308 unsigned int si, so;
1309 unsigned long t;
1310 unsigned int change_detector, must_reset;
1311
1312 mutex_lock(&hotkey_thread_mutex);
1313
1314 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1315 goto exit;
1316
1317 set_freezable();
1318
1319 so = 0;
1320 si = 1;
1321 t = 0;
1322
1323 /* Initial state for compares */
1324 mutex_lock(&hotkey_thread_data_mutex);
1325 change_detector = hotkey_config_change;
1326 mask = hotkey_source_mask & hotkey_mask;
1327 mutex_unlock(&hotkey_thread_data_mutex);
1328 hotkey_read_nvram(&s[so], mask);
1329
1330 while (!kthread_should_stop() && hotkey_poll_freq) {
1331 if (t == 0)
1332 t = 1000/hotkey_poll_freq;
1333 t = msleep_interruptible(t);
1334 if (unlikely(kthread_should_stop()))
1335 break;
1336 must_reset = try_to_freeze();
1337 if (t > 0 && !must_reset)
1338 continue;
1339
1340 mutex_lock(&hotkey_thread_data_mutex);
1341 if (must_reset || hotkey_config_change != change_detector) {
1342 /* forget old state on thaw or config change */
1343 si = so;
1344 t = 0;
1345 change_detector = hotkey_config_change;
1346 }
1347 mask = hotkey_source_mask & hotkey_mask;
1348 mutex_unlock(&hotkey_thread_data_mutex);
1349
1350 if (likely(mask)) {
1351 hotkey_read_nvram(&s[si], mask);
1352 if (likely(si != so)) {
1353 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001354 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001355 }
1356 }
1357
1358 so = si;
1359 si ^= 1;
1360 }
1361
1362exit:
1363 mutex_unlock(&hotkey_thread_mutex);
1364 return 0;
1365}
1366
1367static void hotkey_poll_stop_sync(void)
1368{
1369 if (tpacpi_hotkey_task) {
1370 if (frozen(tpacpi_hotkey_task) ||
1371 freezing(tpacpi_hotkey_task))
1372 thaw_process(tpacpi_hotkey_task);
1373
1374 kthread_stop(tpacpi_hotkey_task);
1375 tpacpi_hotkey_task = NULL;
1376 mutex_lock(&hotkey_thread_mutex);
1377 /* at this point, the thread did exit */
1378 mutex_unlock(&hotkey_thread_mutex);
1379 }
1380}
1381
1382/* call with hotkey_mutex held */
1383static void hotkey_poll_setup(int may_warn)
1384{
1385 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1386 hotkey_poll_freq > 0 &&
1387 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1388 if (!tpacpi_hotkey_task) {
1389 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001390 NULL,
1391 TPACPI_FILE "d");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001392 if (IS_ERR(tpacpi_hotkey_task)) {
1393 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001394 printk(TPACPI_ERR
1395 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001396 "for hotkey polling\n");
1397 }
1398 }
1399 } else {
1400 hotkey_poll_stop_sync();
1401 if (may_warn &&
1402 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001403 printk(TPACPI_NOTICE
1404 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001405 "which is currently disabled\n",
1406 hotkey_source_mask);
1407 }
1408 }
1409}
1410
1411static void hotkey_poll_setup_safe(int may_warn)
1412{
1413 mutex_lock(&hotkey_mutex);
1414 hotkey_poll_setup(may_warn);
1415 mutex_unlock(&hotkey_mutex);
1416}
1417
1418static int hotkey_inputdev_open(struct input_dev *dev)
1419{
1420 switch (tpacpi_lifecycle) {
1421 case TPACPI_LIFE_INIT:
1422 /*
1423 * hotkey_init will call hotkey_poll_setup_safe
1424 * at the appropriate moment
1425 */
1426 return 0;
1427 case TPACPI_LIFE_EXITING:
1428 return -EBUSY;
1429 case TPACPI_LIFE_RUNNING:
1430 hotkey_poll_setup_safe(0);
1431 return 0;
1432 }
1433
1434 /* Should only happen if tpacpi_lifecycle is corrupt */
1435 BUG();
1436 return -EBUSY;
1437}
1438
1439static void hotkey_inputdev_close(struct input_dev *dev)
1440{
1441 /* disable hotkey polling when possible */
1442 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1443 hotkey_poll_setup_safe(0);
1444}
1445#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1446
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001447/* sysfs hotkey enable ------------------------------------------------- */
1448static ssize_t hotkey_enable_show(struct device *dev,
1449 struct device_attribute *attr,
1450 char *buf)
1451{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001452 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001453
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001454 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001455 if (res)
1456 return res;
1457
1458 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1459}
1460
1461static ssize_t hotkey_enable_store(struct device *dev,
1462 struct device_attribute *attr,
1463 const char *buf, size_t count)
1464{
1465 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001466 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001467
1468 if (parse_strtoul(buf, 1, &t))
1469 return -EINVAL;
1470
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001471 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001472
1473 return (res) ? res : count;
1474}
1475
1476static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001477 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001478 hotkey_enable_show, hotkey_enable_store);
1479
1480/* sysfs hotkey mask --------------------------------------------------- */
1481static ssize_t hotkey_mask_show(struct device *dev,
1482 struct device_attribute *attr,
1483 char *buf)
1484{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001485 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001486
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001487 if (mutex_lock_interruptible(&hotkey_mutex))
1488 return -ERESTARTSYS;
1489 res = hotkey_mask_get();
1490 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001491
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001492 return (res)?
1493 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001494}
1495
1496static ssize_t hotkey_mask_store(struct device *dev,
1497 struct device_attribute *attr,
1498 const char *buf, size_t count)
1499{
1500 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001501 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001502
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001503 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001504 return -EINVAL;
1505
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001506 if (mutex_lock_interruptible(&hotkey_mutex))
1507 return -ERESTARTSYS;
1508
1509 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001510
1511#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1512 hotkey_poll_setup(1);
1513#endif
1514
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001515 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001516
1517 return (res) ? res : count;
1518}
1519
1520static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001521 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001522 hotkey_mask_show, hotkey_mask_store);
1523
1524/* sysfs hotkey bios_enabled ------------------------------------------- */
1525static ssize_t hotkey_bios_enabled_show(struct device *dev,
1526 struct device_attribute *attr,
1527 char *buf)
1528{
1529 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1530}
1531
1532static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001533 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001534
1535/* sysfs hotkey bios_mask ---------------------------------------------- */
1536static ssize_t hotkey_bios_mask_show(struct device *dev,
1537 struct device_attribute *attr,
1538 char *buf)
1539{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001540 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001541}
1542
1543static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001544 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001545
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001546/* sysfs hotkey all_mask ----------------------------------------------- */
1547static ssize_t hotkey_all_mask_show(struct device *dev,
1548 struct device_attribute *attr,
1549 char *buf)
1550{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001551 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1552 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001553}
1554
1555static struct device_attribute dev_attr_hotkey_all_mask =
1556 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1557
1558/* sysfs hotkey recommended_mask --------------------------------------- */
1559static ssize_t hotkey_recommended_mask_show(struct device *dev,
1560 struct device_attribute *attr,
1561 char *buf)
1562{
1563 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001564 (hotkey_all_mask | hotkey_source_mask)
1565 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001566}
1567
1568static struct device_attribute dev_attr_hotkey_recommended_mask =
1569 __ATTR(hotkey_recommended_mask, S_IRUGO,
1570 hotkey_recommended_mask_show, NULL);
1571
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001572#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1573
1574/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1575static ssize_t hotkey_source_mask_show(struct device *dev,
1576 struct device_attribute *attr,
1577 char *buf)
1578{
1579 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1580}
1581
1582static ssize_t hotkey_source_mask_store(struct device *dev,
1583 struct device_attribute *attr,
1584 const char *buf, size_t count)
1585{
1586 unsigned long t;
1587
1588 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1589 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1590 return -EINVAL;
1591
1592 if (mutex_lock_interruptible(&hotkey_mutex))
1593 return -ERESTARTSYS;
1594
1595 HOTKEY_CONFIG_CRITICAL_START
1596 hotkey_source_mask = t;
1597 HOTKEY_CONFIG_CRITICAL_END
1598
1599 hotkey_poll_setup(1);
1600
1601 mutex_unlock(&hotkey_mutex);
1602
1603 return count;
1604}
1605
1606static struct device_attribute dev_attr_hotkey_source_mask =
1607 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1608 hotkey_source_mask_show, hotkey_source_mask_store);
1609
1610/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1611static ssize_t hotkey_poll_freq_show(struct device *dev,
1612 struct device_attribute *attr,
1613 char *buf)
1614{
1615 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1616}
1617
1618static ssize_t hotkey_poll_freq_store(struct device *dev,
1619 struct device_attribute *attr,
1620 const char *buf, size_t count)
1621{
1622 unsigned long t;
1623
1624 if (parse_strtoul(buf, 25, &t))
1625 return -EINVAL;
1626
1627 if (mutex_lock_interruptible(&hotkey_mutex))
1628 return -ERESTARTSYS;
1629
1630 hotkey_poll_freq = t;
1631
1632 hotkey_poll_setup(1);
1633 mutex_unlock(&hotkey_mutex);
1634
1635 return count;
1636}
1637
1638static struct device_attribute dev_attr_hotkey_poll_freq =
1639 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1640 hotkey_poll_freq_show, hotkey_poll_freq_store);
1641
1642#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1643
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001644/* sysfs hotkey radio_sw ----------------------------------------------- */
1645static ssize_t hotkey_radio_sw_show(struct device *dev,
1646 struct device_attribute *attr,
1647 char *buf)
1648{
1649 int res, s;
1650 res = hotkey_get_wlsw(&s);
1651 if (res < 0)
1652 return res;
1653
1654 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1655}
1656
1657static struct device_attribute dev_attr_hotkey_radio_sw =
1658 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1659
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001660/* sysfs hotkey report_mode -------------------------------------------- */
1661static ssize_t hotkey_report_mode_show(struct device *dev,
1662 struct device_attribute *attr,
1663 char *buf)
1664{
1665 return snprintf(buf, PAGE_SIZE, "%d\n",
1666 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1667}
1668
1669static struct device_attribute dev_attr_hotkey_report_mode =
1670 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1671
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001672/* sysfs wakeup reason ------------------------------------------------- */
1673static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1674 struct device_attribute *attr,
1675 char *buf)
1676{
1677 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1678}
1679
1680static struct device_attribute dev_attr_hotkey_wakeup_reason =
1681 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1682
1683/* sysfs wakeup hotunplug_complete ------------------------------------- */
1684static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1685 struct device_attribute *attr,
1686 char *buf)
1687{
1688 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1689}
1690
1691static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1692 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1693 hotkey_wakeup_hotunplug_complete_show, NULL);
1694
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001695/* --------------------------------------------------------------------- */
1696
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001697static struct attribute *hotkey_attributes[] __initdata = {
1698 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001699 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001700 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001701#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1702 &dev_attr_hotkey_mask.attr,
1703 &dev_attr_hotkey_all_mask.attr,
1704 &dev_attr_hotkey_recommended_mask.attr,
1705 &dev_attr_hotkey_source_mask.attr,
1706 &dev_attr_hotkey_poll_freq.attr,
1707#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001708};
1709
1710static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001711 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001712#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1713 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001714 &dev_attr_hotkey_all_mask.attr,
1715 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001716#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001717 &dev_attr_hotkey_wakeup_reason.attr,
1718 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001719};
1720
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001721static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001722{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001723 /* Requirements for changing the default keymaps:
1724 *
1725 * 1. Many of the keys are mapped to KEY_RESERVED for very
1726 * good reasons. Do not change them unless you have deep
1727 * knowledge on the IBM and Lenovo ThinkPad firmware for
1728 * the various ThinkPad models. The driver behaves
1729 * differently for KEY_RESERVED: such keys have their
1730 * hot key mask *unset* in mask_recommended, and also
1731 * in the initial hot key mask programmed into the
1732 * firmware at driver load time, which means the firm-
1733 * ware may react very differently if you change them to
1734 * something else;
1735 *
1736 * 2. You must be subscribed to the linux-thinkpad and
1737 * ibm-acpi-devel mailing lists, and you should read the
1738 * list archives since 2007 if you want to change the
1739 * keymaps. This requirement exists so that you will
1740 * know the past history of problems with the thinkpad-
1741 * acpi driver keymaps, and also that you will be
1742 * listening to any bug reports;
1743 *
1744 * 3. Do not send thinkpad-acpi specific patches directly to
1745 * for merging, *ever*. Send them to the linux-acpi
1746 * mailinglist for comments. Merging is to be done only
1747 * through acpi-test and the ACPI maintainer.
1748 *
1749 * If the above is too much to ask, don't change the keymap.
1750 * Ask the thinkpad-acpi maintainer to do it, instead.
1751 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001752 static u16 ibm_keycode_map[] __initdata = {
1753 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1754 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1755 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1756 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001757
1758 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001759 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1760 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1761 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001762
1763 /* brightness: firmware always reacts to them, unless
1764 * X.org did some tricks in the radeon BIOS scratch
1765 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001766 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001767 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001768
1769 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001770 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001771
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001772 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1773 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001774
1775 /* Volume: firmware always react to it and reprograms
1776 * the built-in *extra* mixer. Never map it to control
1777 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001778 KEY_RESERVED, /* 0x14: VOLUME UP */
1779 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1780 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001781
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001782 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001783
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001784 /* (assignments unknown, please report if found) */
1785 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1786 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1787 };
1788 static u16 lenovo_keycode_map[] __initdata = {
1789 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1790 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1791 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1792 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001793
1794 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001795 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1796 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1797 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001798
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001799 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001800 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001801
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001802 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001803
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001804 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1805 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001806
1807 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1808 * react to it and reprograms the built-in *extra* mixer.
1809 * Never map it to control another mixer by default.
1810 *
1811 * T60?, T61, R60?, R61: firmware and EC tries to send
1812 * these over the regular keyboard, so these are no-ops,
1813 * but there are still weird bugs re. MUTE, so do not
1814 * change unless you get test reports from all Lenovo
1815 * models. May cause the BIOS to interfere with the
1816 * HDA mixer.
1817 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001818 KEY_RESERVED, /* 0x14: VOLUME UP */
1819 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1820 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001821
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001822 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001823
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001824 /* (assignments unknown, please report if found) */
1825 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1826 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1827 };
1828
1829#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1830#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1831#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1832
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001833 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001834 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001835 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001836
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001837 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1838
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001839 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001840 BUG_ON(tpacpi_inputdev->open != NULL ||
1841 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001842
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001843 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001844 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001845
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001846#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1847 mutex_init(&hotkey_thread_mutex);
1848 mutex_init(&hotkey_thread_data_mutex);
1849#endif
1850
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001851 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001852 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001853
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001854 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001855 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001856
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001857 if (tp_features.hotkey) {
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001858 hotkey_dev_attributes = create_attr_set(12, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001859 if (!hotkey_dev_attributes)
1860 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001861 res = add_many_to_attr_set(hotkey_dev_attributes,
1862 hotkey_attributes,
1863 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001864 if (res)
1865 return res;
1866
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001867 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001868 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1869 for HKEY interface version 0x100 */
1870 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1871 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001872 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001873 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001874 printk(TPACPI_ERR "please report this to %s\n",
1875 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001876 } else {
1877 /*
1878 * MHKV 0x100 in A31, R40, R40e,
1879 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001880 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001881 tp_features.hotkey_mask = 1;
1882 }
1883 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001884
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001885 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001886 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001887
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001888 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001889 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001890 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001891 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001892 "missing MHKA handler, "
1893 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001894 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001895 /* FN+F12, FN+F4, FN+F3 */
1896 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001897 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001898 }
1899
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001900 /* hotkey_source_mask *must* be zero for
1901 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001902 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001903 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001904 res = hotkey_mask_get();
1905 hotkey_orig_mask = hotkey_mask;
1906 if (!res) {
1907 res = add_many_to_attr_set(
1908 hotkey_dev_attributes,
1909 hotkey_mask_attributes,
1910 ARRAY_SIZE(hotkey_mask_attributes));
1911 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001912 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001913
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001914#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1915 if (tp_features.hotkey_mask) {
1916 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1917 & ~hotkey_all_mask;
1918 } else {
1919 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1920 }
1921
1922 vdbg_printk(TPACPI_DBG_INIT,
1923 "hotkey source mask 0x%08x, polling freq %d\n",
1924 hotkey_source_mask, hotkey_poll_freq);
1925#endif
1926
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001927 /* Not all thinkpads have a hardware radio switch */
1928 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1929 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001930 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001931 "radio switch found; radios are %s\n",
1932 enabled(status, 0));
1933 res = add_to_attr_set(hotkey_dev_attributes,
1934 &dev_attr_hotkey_radio_sw.attr);
1935 }
1936
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001937 if (!res)
1938 res = register_attr_set_with_sysfs(
1939 hotkey_dev_attributes,
1940 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001941 if (res)
1942 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001943
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001944 /* Set up key map */
1945
1946 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1947 GFP_KERNEL);
1948 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001949 printk(TPACPI_ERR
1950 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001951 return -ENOMEM;
1952 }
1953
1954 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1955 dbg_printk(TPACPI_DBG_INIT,
1956 "using Lenovo default hot key map\n");
1957 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1958 TPACPI_HOTKEY_MAP_SIZE);
1959 } else {
1960 dbg_printk(TPACPI_DBG_INIT,
1961 "using IBM default hot key map\n");
1962 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1963 TPACPI_HOTKEY_MAP_SIZE);
1964 }
1965
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001966 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1967 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1968 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001969 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1970 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1971 tpacpi_inputdev->keycode = hotkey_keycode_map;
1972 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001973 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1974 set_bit(hotkey_keycode_map[i],
1975 tpacpi_inputdev->keybit);
1976 } else {
1977 if (i < sizeof(hotkey_reserved_mask)*8)
1978 hotkey_reserved_mask |= 1 << i;
1979 }
1980 }
1981
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001982 if (tp_features.hotkey_wlsw) {
1983 set_bit(EV_SW, tpacpi_inputdev->evbit);
1984 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1985 }
1986
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001987 dbg_printk(TPACPI_DBG_INIT,
1988 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001989 res = hotkey_status_set(1);
1990 if (res)
1991 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001992 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1993 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001994 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001995 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001996 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001997
1998 dbg_printk(TPACPI_DBG_INIT,
1999 "legacy hot key reporting over procfs %s\n",
2000 (hotkey_report_mode < 2) ?
2001 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002002
2003#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2004 tpacpi_inputdev->open = &hotkey_inputdev_open;
2005 tpacpi_inputdev->close = &hotkey_inputdev_close;
2006
2007 hotkey_poll_setup_safe(1);
2008#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002009 }
2010
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002011 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002012}
2013
2014static void hotkey_exit(void)
2015{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002016#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2017 hotkey_poll_stop_sync();
2018#endif
2019
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002020 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002021 dbg_printk(TPACPI_DBG_EXIT,
2022 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002023 /* no short-circuit boolean operator below! */
2024 if ((hotkey_mask_set(hotkey_orig_mask) |
2025 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002026 printk(TPACPI_ERR
2027 "failed to restore hot key mask "
2028 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002029 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002030
2031 if (hotkey_dev_attributes) {
2032 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2033 hotkey_dev_attributes = NULL;
2034 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002035}
2036
2037static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2038{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002039 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002040 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002041 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002042 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002043 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002044
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002045 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002046 printk(TPACPI_ERR
2047 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002048 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002049 acpi_bus_generate_netlink_event(
2050 ibm->acpi->device->pnp.device_class,
2051 ibm->acpi->device->dev.bus_id,
2052 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002053 return;
2054 }
2055
2056 while (1) {
2057 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002058 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002059 return;
2060 }
2061
2062 if (hkey == 0) {
2063 /* queue empty */
2064 return;
2065 }
2066
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002067 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002068 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002069 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002070
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002071 switch (hkey >> 12) {
2072 case 1:
2073 /* 0x1000-0x1FFF: key presses */
2074 scancode = hkey & 0xfff;
2075 if (scancode > 0 && scancode < 0x21) {
2076 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002077 if (!(hotkey_source_mask & (1 << scancode))) {
2078 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002079 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002080 } else {
2081 ignore_acpi_ev = 1;
2082 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002083 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002084 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002085 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002086 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002087 case 2:
2088 /* Wakeup reason */
2089 switch (hkey) {
2090 case 0x2304: /* suspend, undock */
2091 case 0x2404: /* hibernation, undock */
2092 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2093 ignore_acpi_ev = 1;
2094 break;
2095 case 0x2305: /* suspend, bay eject */
2096 case 0x2405: /* hibernation, bay eject */
2097 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2098 ignore_acpi_ev = 1;
2099 break;
2100 default:
2101 unk_ev = 1;
2102 }
2103 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2104 printk(TPACPI_INFO
2105 "woke up due to a hot-unplug "
2106 "request...\n");
2107 }
2108 break;
2109 case 3:
2110 /* bay-related wakeups */
2111 if (hkey == 0x3003) {
2112 hotkey_autosleep_ack = 1;
2113 printk(TPACPI_INFO
2114 "bay ejected\n");
2115 } else {
2116 unk_ev = 1;
2117 }
2118 break;
2119 case 4:
2120 /* dock-related wakeups */
2121 if (hkey == 0x4003) {
2122 hotkey_autosleep_ack = 1;
2123 printk(TPACPI_INFO
2124 "undocked\n");
2125 } else {
2126 unk_ev = 1;
2127 }
2128 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002129 case 5:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002130 /* 0x5000-0x5FFF: On screen display helpers */
2131 switch (hkey) {
2132 case 0x5010:
2133 /* Lenovo Vista BIOS: brightness changed */
2134 break;
2135 case 0x5001:
2136 case 0x5002:
2137 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002138 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002139 break;
2140 default:
2141 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002142 }
2143 break;
2144 case 7:
2145 /* 0x7000-0x7FFF: misc */
2146 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2147 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002148 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002149 break;
2150 }
2151 /* fallthrough to default */
2152 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002153 unk_ev = 1;
2154 }
2155 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002156 printk(TPACPI_NOTICE
2157 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002158 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002159
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002160 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002161 if (!ignore_acpi_ev &&
2162 (send_acpi_ev || hotkey_report_mode < 2)) {
2163 acpi_bus_generate_proc_event(ibm->acpi->device,
2164 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002165 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002166
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002167 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002168 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002169 acpi_bus_generate_netlink_event(
2170 ibm->acpi->device->pnp.device_class,
2171 ibm->acpi->device->dev.bus_id,
2172 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002173 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002174 }
2175}
2176
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002177static void hotkey_suspend(pm_message_t state)
2178{
2179 /* Do these on suspend, we get the events on early resume! */
2180 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2181 hotkey_autosleep_ack = 0;
2182}
2183
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002184static void hotkey_resume(void)
2185{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002186 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002187 printk(TPACPI_ERR
2188 "error while trying to read hot key mask "
2189 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002190 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002191#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2192 hotkey_poll_setup_safe(0);
2193#endif
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002194}
2195
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002196/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002197static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002199 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 int len = 0;
2201
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002202 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002203 len += sprintf(p + len, "status:\t\tnot supported\n");
2204 return len;
2205 }
2206
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002207 if (mutex_lock_interruptible(&hotkey_mutex))
2208 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002209 res = hotkey_status_get(&status);
2210 if (!res)
2211 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002212 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002213 if (res)
2214 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002217 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002218 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 len += sprintf(p + len,
2220 "commands:\tenable, disable, reset, <mask>\n");
2221 } else {
2222 len += sprintf(p + len, "mask:\t\tnot supported\n");
2223 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2224 }
2225
2226 return len;
2227}
2228
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002229static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002231 int res, status;
2232 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002235 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return -ENODEV;
2237
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002238 if (mutex_lock_interruptible(&hotkey_mutex))
2239 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002240
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002241 status = -1;
2242 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002243
2244 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 while ((cmd = next_cmd(&buf))) {
2246 if (strlencmp(cmd, "enable") == 0) {
2247 status = 1;
2248 } else if (strlencmp(cmd, "disable") == 0) {
2249 status = 0;
2250 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002251 status = hotkey_orig_status;
2252 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2254 /* mask set */
2255 } else if (sscanf(cmd, "%x", &mask) == 1) {
2256 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002257 } else {
2258 res = -EINVAL;
2259 goto errexit;
2260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002262 if (status != -1)
2263 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002265 if (!res && mask != hotkey_mask)
2266 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002268errexit:
2269 mutex_unlock(&hotkey_mutex);
2270 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002271}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002273static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002274 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002275 {"", 0},
2276};
2277
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002278static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002279 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002280 .notify = hotkey_notify,
2281 .handle = &hkey_handle,
2282 .type = ACPI_DEVICE_NOTIFY,
2283};
2284
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002285static struct ibm_struct hotkey_driver_data = {
2286 .name = "hotkey",
2287 .read = hotkey_read,
2288 .write = hotkey_write,
2289 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002290 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002291 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002292 .acpi = &ibm_hotkey_acpidriver,
2293};
2294
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002295/*************************************************************************
2296 * Bluetooth subdriver
2297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002299enum {
2300 /* ACPI GBDC/SBDC bits */
2301 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2302 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2303 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2304};
2305
2306static int bluetooth_get_radiosw(void);
2307static int bluetooth_set_radiosw(int radio_on);
2308
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002309/* sysfs bluetooth enable ---------------------------------------------- */
2310static ssize_t bluetooth_enable_show(struct device *dev,
2311 struct device_attribute *attr,
2312 char *buf)
2313{
2314 int status;
2315
2316 status = bluetooth_get_radiosw();
2317 if (status < 0)
2318 return status;
2319
2320 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2321}
2322
2323static ssize_t bluetooth_enable_store(struct device *dev,
2324 struct device_attribute *attr,
2325 const char *buf, size_t count)
2326{
2327 unsigned long t;
2328 int res;
2329
2330 if (parse_strtoul(buf, 1, &t))
2331 return -EINVAL;
2332
2333 res = bluetooth_set_radiosw(t);
2334
2335 return (res) ? res : count;
2336}
2337
2338static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002339 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002340 bluetooth_enable_show, bluetooth_enable_store);
2341
2342/* --------------------------------------------------------------------- */
2343
2344static struct attribute *bluetooth_attributes[] = {
2345 &dev_attr_bluetooth_enable.attr,
2346 NULL
2347};
2348
2349static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002350 .attrs = bluetooth_attributes,
2351};
2352
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002353static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002355 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002356 int status = 0;
2357
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002358 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2359
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002360 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002361
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002362 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2363 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002364 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002365 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002367 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2368 str_supported(tp_features.bluetooth),
2369 status);
2370
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002371 if (tp_features.bluetooth) {
2372 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2373 /* no bluetooth hardware present in system */
2374 tp_features.bluetooth = 0;
2375 dbg_printk(TPACPI_DBG_INIT,
2376 "bluetooth hardware not installed\n");
2377 } else {
2378 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2379 &bluetooth_attr_group);
2380 if (res)
2381 return res;
2382 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002383 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002384
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002385 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002388static void bluetooth_exit(void)
2389{
2390 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2391 &bluetooth_attr_group);
2392}
2393
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002394static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
2396 int status;
2397
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002398 if (!tp_features.bluetooth)
2399 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002401 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2402 return -EIO;
2403
2404 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2405}
2406
2407static int bluetooth_set_radiosw(int radio_on)
2408{
2409 int status;
2410
2411 if (!tp_features.bluetooth)
2412 return -ENODEV;
2413
2414 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2415 return -EIO;
2416 if (radio_on)
2417 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2418 else
2419 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2420 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2421 return -EIO;
2422
2423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002426/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002427static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
2429 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002430 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002432 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002435 len += sprintf(p + len, "status:\t\t%s\n",
2436 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 len += sprintf(p + len, "commands:\tenable, disable\n");
2438 }
2439
2440 return len;
2441}
2442
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002443static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002447 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002448 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 while ((cmd = next_cmd(&buf))) {
2451 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002452 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002454 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 } else
2456 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return 0;
2460}
2461
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002462static struct ibm_struct bluetooth_driver_data = {
2463 .name = "bluetooth",
2464 .read = bluetooth_read,
2465 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002466 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002467};
2468
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002469/*************************************************************************
2470 * Wan subdriver
2471 */
2472
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002473enum {
2474 /* ACPI GWAN/SWAN bits */
2475 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2476 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2477 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2478};
2479
2480static int wan_get_radiosw(void);
2481static int wan_set_radiosw(int radio_on);
2482
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002483/* sysfs wan enable ---------------------------------------------------- */
2484static ssize_t wan_enable_show(struct device *dev,
2485 struct device_attribute *attr,
2486 char *buf)
2487{
2488 int status;
2489
2490 status = wan_get_radiosw();
2491 if (status < 0)
2492 return status;
2493
2494 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2495}
2496
2497static ssize_t wan_enable_store(struct device *dev,
2498 struct device_attribute *attr,
2499 const char *buf, size_t count)
2500{
2501 unsigned long t;
2502 int res;
2503
2504 if (parse_strtoul(buf, 1, &t))
2505 return -EINVAL;
2506
2507 res = wan_set_radiosw(t);
2508
2509 return (res) ? res : count;
2510}
2511
2512static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002513 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002514 wan_enable_show, wan_enable_store);
2515
2516/* --------------------------------------------------------------------- */
2517
2518static struct attribute *wan_attributes[] = {
2519 &dev_attr_wan_enable.attr,
2520 NULL
2521};
2522
2523static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002524 .attrs = wan_attributes,
2525};
2526
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002527static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002528{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002529 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002530 int status = 0;
2531
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002532 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2533
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002534 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002535
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002536 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002537 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002538
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002539 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2540 str_supported(tp_features.wan),
2541 status);
2542
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002543 if (tp_features.wan) {
2544 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2545 /* no wan hardware present in system */
2546 tp_features.wan = 0;
2547 dbg_printk(TPACPI_DBG_INIT,
2548 "wan hardware not installed\n");
2549 } else {
2550 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2551 &wan_attr_group);
2552 if (res)
2553 return res;
2554 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002555 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002556
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002557 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002558}
2559
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002560static void wan_exit(void)
2561{
2562 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2563 &wan_attr_group);
2564}
2565
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002566static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002567{
2568 int status;
2569
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002570 if (!tp_features.wan)
2571 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002572
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002573 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2574 return -EIO;
2575
2576 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2577}
2578
2579static int wan_set_radiosw(int radio_on)
2580{
2581 int status;
2582
2583 if (!tp_features.wan)
2584 return -ENODEV;
2585
2586 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2587 return -EIO;
2588 if (radio_on)
2589 status |= TP_ACPI_WANCARD_RADIOSSW;
2590 else
2591 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2592 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2593 return -EIO;
2594
2595 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002596}
2597
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002598/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002599static int wan_read(char *p)
2600{
2601 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002602 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002603
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002604 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002605 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002606 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002607 len += sprintf(p + len, "status:\t\t%s\n",
2608 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002609 len += sprintf(p + len, "commands:\tenable, disable\n");
2610 }
2611
2612 return len;
2613}
2614
2615static int wan_write(char *buf)
2616{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002617 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002618
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002619 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002620 return -ENODEV;
2621
2622 while ((cmd = next_cmd(&buf))) {
2623 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002624 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002625 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002626 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002627 } else
2628 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002629 }
2630
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002631 return 0;
2632}
2633
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002634static struct ibm_struct wan_driver_data = {
2635 .name = "wan",
2636 .read = wan_read,
2637 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002638 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002639 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002640};
2641
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002642/*************************************************************************
2643 * Video subdriver
2644 */
2645
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002646enum video_access_mode {
2647 TPACPI_VIDEO_NONE = 0,
2648 TPACPI_VIDEO_570, /* 570 */
2649 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2650 TPACPI_VIDEO_NEW, /* all others */
2651};
2652
2653enum { /* video status flags, based on VIDEO_570 */
2654 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2655 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2656 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2657};
2658
2659enum { /* TPACPI_VIDEO_570 constants */
2660 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2661 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2662 * video_status_flags */
2663 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2664 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2665};
2666
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002667static enum video_access_mode video_supported;
2668static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002669
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002670static int video_autosw_get(void);
2671static int video_autosw_set(int enable);
2672
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002673TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002674 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2675 "\\_SB.PCI0.VID0", /* 770e */
2676 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2677 "\\_SB.PCI0.AGP.VID", /* all others */
2678 ); /* R30, R31 */
2679
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002680TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002681
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002682static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002684 int ivga;
2685
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002686 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2687
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002688 TPACPI_ACPIHANDLE_INIT(vid);
2689 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002690
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002691 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2692 /* G41, assume IVGA doesn't change */
2693 vid_handle = vid2_handle;
2694
2695 if (!vid_handle)
2696 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002697 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002698 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2699 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002700 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002701 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2702 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002703 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002704 else
2705 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002706 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002708 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2709 str_supported(video_supported != TPACPI_VIDEO_NONE),
2710 video_supported);
2711
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002712 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002715static void video_exit(void)
2716{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002717 dbg_printk(TPACPI_DBG_EXIT,
2718 "restoring original video autoswitch mode\n");
2719 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002720 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002721 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002722}
2723
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002724static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 int status = 0;
2727 int i;
2728
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002729 switch (video_supported) {
2730 case TPACPI_VIDEO_570:
2731 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2732 TP_ACPI_VIDEO_570_PHSCMD))
2733 return -EIO;
2734 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2735 break;
2736 case TPACPI_VIDEO_770:
2737 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2738 return -EIO;
2739 if (i)
2740 status |= TP_ACPI_VIDEO_S_LCD;
2741 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2742 return -EIO;
2743 if (i)
2744 status |= TP_ACPI_VIDEO_S_CRT;
2745 break;
2746 case TPACPI_VIDEO_NEW:
2747 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2748 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2749 return -EIO;
2750 if (i)
2751 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002753 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2754 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2755 return -EIO;
2756 if (i)
2757 status |= TP_ACPI_VIDEO_S_LCD;
2758 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2759 return -EIO;
2760 if (i)
2761 status |= TP_ACPI_VIDEO_S_DVI;
2762 break;
2763 default:
2764 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 return status;
2768}
2769
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002770static int video_outputsw_set(int status)
2771{
2772 int autosw;
2773 int res = 0;
2774
2775 switch (video_supported) {
2776 case TPACPI_VIDEO_570:
2777 res = acpi_evalf(NULL, NULL,
2778 "\\_SB.PHS2", "vdd",
2779 TP_ACPI_VIDEO_570_PHS2CMD,
2780 status | TP_ACPI_VIDEO_570_PHS2SET);
2781 break;
2782 case TPACPI_VIDEO_770:
2783 autosw = video_autosw_get();
2784 if (autosw < 0)
2785 return autosw;
2786
2787 res = video_autosw_set(1);
2788 if (res)
2789 return res;
2790 res = acpi_evalf(vid_handle, NULL,
2791 "ASWT", "vdd", status * 0x100, 0);
2792 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002793 printk(TPACPI_ERR
2794 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002795 return -EIO;
2796 }
2797 break;
2798 case TPACPI_VIDEO_NEW:
2799 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002800 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002801 break;
2802 default:
2803 return -ENOSYS;
2804 }
2805
2806 return (res)? 0 : -EIO;
2807}
2808
2809static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002811 int autosw = 0;
2812
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002813 switch (video_supported) {
2814 case TPACPI_VIDEO_570:
2815 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2816 return -EIO;
2817 break;
2818 case TPACPI_VIDEO_770:
2819 case TPACPI_VIDEO_NEW:
2820 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2821 return -EIO;
2822 break;
2823 default:
2824 return -ENOSYS;
2825 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002826
2827 return autosw & 1;
2828}
2829
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002830static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002831{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002832 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002833 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002834 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002835}
2836
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002837static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002838{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002839 int autosw = video_autosw_get();
2840 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002841
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002842 if (autosw < 0)
2843 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002844
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002845 switch (video_supported) {
2846 case TPACPI_VIDEO_570:
2847 res = video_autosw_set(1);
2848 if (res)
2849 return res;
2850 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2851 break;
2852 case TPACPI_VIDEO_770:
2853 case TPACPI_VIDEO_NEW:
2854 res = video_autosw_set(1);
2855 if (res)
2856 return res;
2857 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2858 break;
2859 default:
2860 return -ENOSYS;
2861 }
2862 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002863 printk(TPACPI_ERR
2864 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002865 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002866 }
2867
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002868 return (res)? 0 : -EIO;
2869}
2870
2871static int video_expand_toggle(void)
2872{
2873 switch (video_supported) {
2874 case TPACPI_VIDEO_570:
2875 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2876 0 : -EIO;
2877 case TPACPI_VIDEO_770:
2878 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2879 0 : -EIO;
2880 case TPACPI_VIDEO_NEW:
2881 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2882 0 : -EIO;
2883 default:
2884 return -ENOSYS;
2885 }
2886 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002887}
2888
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002889static int video_read(char *p)
2890{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002891 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002892 int len = 0;
2893
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002894 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002895 len += sprintf(p + len, "status:\t\tnot supported\n");
2896 return len;
2897 }
2898
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002899 status = video_outputsw_get();
2900 if (status < 0)
2901 return status;
2902
2903 autosw = video_autosw_get();
2904 if (autosw < 0)
2905 return autosw;
2906
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002907 len += sprintf(p + len, "status:\t\tsupported\n");
2908 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2909 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002910 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002911 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2912 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2913 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2914 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002915 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002916 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2917 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2918 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2919
2920 return len;
2921}
2922
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002923static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
2925 char *cmd;
2926 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002927 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002929 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002930 return -ENODEV;
2931
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002932 enable = 0;
2933 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 while ((cmd = next_cmd(&buf))) {
2936 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002937 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002939 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002941 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002943 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002944 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002945 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002946 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002947 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002948 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002949 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002951 res = video_autosw_set(1);
2952 if (res)
2953 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002955 res = video_autosw_set(0);
2956 if (res)
2957 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002959 res = video_outputsw_cycle();
2960 if (res)
2961 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002963 res = video_expand_toggle();
2964 if (res)
2965 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 } else
2967 return -EINVAL;
2968 }
2969
2970 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002971 status = video_outputsw_get();
2972 if (status < 0)
2973 return status;
2974 res = video_outputsw_set((status & ~disable) | enable);
2975 if (res)
2976 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
2978
2979 return 0;
2980}
2981
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002982static struct ibm_struct video_driver_data = {
2983 .name = "video",
2984 .read = video_read,
2985 .write = video_write,
2986 .exit = video_exit,
2987};
2988
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002989/*************************************************************************
2990 * Light (thinklight) subdriver
2991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002993TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2994TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002995
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002996static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002998 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2999
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003000 TPACPI_ACPIHANDLE_INIT(ledb);
3001 TPACPI_ACPIHANDLE_INIT(lght);
3002 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003004 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003005 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003006
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003007 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003008 /* light status not supported on
3009 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003010 tp_features.light_status =
3011 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003013 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003014 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003015
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003016 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003019static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
3021 int len = 0;
3022 int status = 0;
3023
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003024 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003025 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003026 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003027 len += sprintf(p + len, "status:\t\tunknown\n");
3028 len += sprintf(p + len, "commands:\ton, off\n");
3029 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3031 return -EIO;
3032 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003033 len += sprintf(p + len, "commands:\ton, off\n");
3034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 return len;
3037}
3038
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003039static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
3041 int cmos_cmd, lght_cmd;
3042 char *cmd;
3043 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003044
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003045 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003046 return -ENODEV;
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 while ((cmd = next_cmd(&buf))) {
3049 if (strlencmp(cmd, "on") == 0) {
3050 cmos_cmd = 0x0c;
3051 lght_cmd = 1;
3052 } else if (strlencmp(cmd, "off") == 0) {
3053 cmos_cmd = 0x0d;
3054 lght_cmd = 0;
3055 } else
3056 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003059 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3060 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (!success)
3062 return -EIO;
3063 }
3064
3065 return 0;
3066}
3067
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003068static struct ibm_struct light_driver_data = {
3069 .name = "light",
3070 .read = light_read,
3071 .write = light_write,
3072};
3073
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003074/*************************************************************************
3075 * Dock subdriver
3076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003078#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003079
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003080static void dock_notify(struct ibm_struct *ibm, u32 event);
3081static int dock_read(char *p);
3082static int dock_write(char *buf);
3083
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003084TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003085 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3086 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3087 "\\_SB.PCI.ISA.SLCE", /* 570 */
3088 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3089
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003090/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003091TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003092
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003093static const struct acpi_device_id ibm_pci_device_ids[] = {
3094 {PCI_ROOT_HID_STRING, 0},
3095 {"", 0},
3096};
3097
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003098static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3099 {
3100 .notify = dock_notify,
3101 .handle = &dock_handle,
3102 .type = ACPI_SYSTEM_NOTIFY,
3103 },
3104 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003105 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3106 * We just use it to get notifications of dock hotplug
3107 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003108 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003109 .notify = dock_notify,
3110 .handle = &pci_handle,
3111 .type = ACPI_SYSTEM_NOTIFY,
3112 },
3113};
3114
3115static struct ibm_struct dock_driver_data[2] = {
3116 {
3117 .name = "dock",
3118 .read = dock_read,
3119 .write = dock_write,
3120 .acpi = &ibm_dock_acpidriver[0],
3121 },
3122 {
3123 .name = "dock",
3124 .acpi = &ibm_dock_acpidriver[1],
3125 },
3126};
3127
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128#define dock_docked() (_sta(dock_handle) & 1)
3129
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003130static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003131{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003132 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3133
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003134 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003135
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003136 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3137 str_supported(dock_handle != NULL));
3138
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003139 return (dock_handle)? 0 : 1;
3140}
3141
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003142static int __init dock_init2(struct ibm_init_struct *iibm)
3143{
3144 int dock2_needed;
3145
3146 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3147
3148 if (dock_driver_data[0].flags.acpi_driver_registered &&
3149 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003150 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003151 dock2_needed = (pci_handle != NULL);
3152 vdbg_printk(TPACPI_DBG_INIT,
3153 "dock PCI handler for the TP 570 is %s\n",
3154 str_supported(dock2_needed));
3155 } else {
3156 vdbg_printk(TPACPI_DBG_INIT,
3157 "dock subdriver part 2 not required\n");
3158 dock2_needed = 0;
3159 }
3160
3161 return (dock2_needed)? 0 : 1;
3162}
3163
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003164static void dock_notify(struct ibm_struct *ibm, u32 event)
3165{
3166 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003167 int pci = ibm->acpi->hid && ibm->acpi->device &&
3168 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003169 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003170
3171 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003172 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003173 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003174 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003175 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003176 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003177 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003178 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003179 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003180 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003181 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003182 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003183 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003184 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003185 }
Len Brown14e04fb2007-08-23 15:20:26 -04003186 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003187 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3188 ibm->acpi->device->dev.bus_id,
3189 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003190}
3191
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003192static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 int len = 0;
3195 int docked = dock_docked();
3196
3197 if (!dock_handle)
3198 len += sprintf(p + len, "status:\t\tnot supported\n");
3199 else if (!docked)
3200 len += sprintf(p + len, "status:\t\tundocked\n");
3201 else {
3202 len += sprintf(p + len, "status:\t\tdocked\n");
3203 len += sprintf(p + len, "commands:\tdock, undock\n");
3204 }
3205
3206 return len;
3207}
3208
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003209static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
3211 char *cmd;
3212
3213 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003214 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
3216 while ((cmd = next_cmd(&buf))) {
3217 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003218 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3219 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return -EIO;
3221 } else if (strlencmp(cmd, "dock") == 0) {
3222 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3223 return -EIO;
3224 } else
3225 return -EINVAL;
3226 }
3227
3228 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003231#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003233/*************************************************************************
3234 * Bay subdriver
3235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003237#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003238
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003239TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003240 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3241 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3242 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3243 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003244TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003245 "_EJ0", /* all others */
3246 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003247TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003248 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3249 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003250TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003251 "_EJ0", /* 770x */
3252 ); /* all others */
3253
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003254static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003256 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3257
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003258 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003259 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003260 TPACPI_ACPIHANDLE_INIT(bay_ej);
3261 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003262 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003263 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003264
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003265 tp_features.bay_status = bay_handle &&
3266 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3267 tp_features.bay_status2 = bay2_handle &&
3268 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003269
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003270 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3271 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3272 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3273 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003275 vdbg_printk(TPACPI_DBG_INIT,
3276 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003277 str_supported(tp_features.bay_status),
3278 str_supported(tp_features.bay_eject),
3279 str_supported(tp_features.bay_status2),
3280 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003281
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003282 return (tp_features.bay_status || tp_features.bay_eject ||
3283 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003286static void bay_notify(struct ibm_struct *ibm, u32 event)
3287{
Len Brown14e04fb2007-08-23 15:20:26 -04003288 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003289 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3290 ibm->acpi->device->dev.bus_id,
3291 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003292}
3293
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003294#define bay_occupied(b) (_sta(b##_handle) & 1)
3295
3296static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
3298 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003299 int occupied = bay_occupied(bay);
3300 int occupied2 = bay_occupied(bay2);
3301 int eject, eject2;
3302
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003303 len += sprintf(p + len, "status:\t\t%s\n",
3304 tp_features.bay_status ?
3305 (occupied ? "occupied" : "unoccupied") :
3306 "not supported");
3307 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003308 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3309 "occupied" : "unoccupied");
3310
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003311 eject = tp_features.bay_eject && occupied;
3312 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003313
3314 if (eject && eject2)
3315 len += sprintf(p + len, "commands:\teject, eject2\n");
3316 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003318 else if (eject2)
3319 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
3321 return len;
3322}
3323
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003324static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 char *cmd;
3327
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003328 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003329 return -ENODEV;
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003332 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003333 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3334 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003335 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003336 strlencmp(cmd, "eject2") == 0) {
3337 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return -EIO;
3339 } else
3340 return -EINVAL;
3341 }
3342
3343 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003344}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003345
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003346static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3347 .notify = bay_notify,
3348 .handle = &bay_handle,
3349 .type = ACPI_SYSTEM_NOTIFY,
3350};
3351
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003352static struct ibm_struct bay_driver_data = {
3353 .name = "bay",
3354 .read = bay_read,
3355 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003356 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003357};
3358
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003359#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003361/*************************************************************************
3362 * CMOS subdriver
3363 */
3364
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003365/* sysfs cmos_command -------------------------------------------------- */
3366static ssize_t cmos_command_store(struct device *dev,
3367 struct device_attribute *attr,
3368 const char *buf, size_t count)
3369{
3370 unsigned long cmos_cmd;
3371 int res;
3372
3373 if (parse_strtoul(buf, 21, &cmos_cmd))
3374 return -EINVAL;
3375
3376 res = issue_thinkpad_cmos_command(cmos_cmd);
3377 return (res)? res : count;
3378}
3379
3380static struct device_attribute dev_attr_cmos_command =
3381 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3382
3383/* --------------------------------------------------------------------- */
3384
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003385static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003386{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003387 int res;
3388
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003389 vdbg_printk(TPACPI_DBG_INIT,
3390 "initializing cmos commands subdriver\n");
3391
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003392 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003393
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003394 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3395 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003396
3397 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3398 if (res)
3399 return res;
3400
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003401 return (cmos_handle)? 0 : 1;
3402}
3403
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003404static void cmos_exit(void)
3405{
3406 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3407}
3408
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003409static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 int len = 0;
3412
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003413 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3414 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 if (!cmos_handle)
3416 len += sprintf(p + len, "status:\t\tnot supported\n");
3417 else {
3418 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003419 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 }
3421
3422 return len;
3423}
3424
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003425static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
3427 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003428 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
3430 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003431 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3432 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 /* cmos_cmd set */
3434 } else
3435 return -EINVAL;
3436
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003437 res = issue_thinkpad_cmos_command(cmos_cmd);
3438 if (res)
3439 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
3441
3442 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003443}
3444
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003445static struct ibm_struct cmos_driver_data = {
3446 .name = "cmos",
3447 .read = cmos_read,
3448 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003449 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003450};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003451
3452/*************************************************************************
3453 * LED subdriver
3454 */
3455
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003456enum led_access_mode {
3457 TPACPI_LED_NONE = 0,
3458 TPACPI_LED_570, /* 570 */
3459 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3460 TPACPI_LED_NEW, /* all others */
3461};
3462
3463enum { /* For TPACPI_LED_OLD */
3464 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3465 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3466 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3467};
3468
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003469static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003470
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003471TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003472 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3473 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003474 "LED", /* all others */
3475 ); /* R30, R31 */
3476
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003477static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003478{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003479 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3480
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003481 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003482
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003483 if (!led_handle)
3484 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003485 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003486 else if (strlencmp(led_path, "SLED") == 0)
3487 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003488 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003489 else if (strlencmp(led_path, "SYSL") == 0)
3490 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003491 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003492 else
3493 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003494 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003495
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003496 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3497 str_supported(led_supported), led_supported);
3498
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003499 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003500}
3501
3502#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3503
3504static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
3506 int len = 0;
3507
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003508 if (!led_supported) {
3509 len += sprintf(p + len, "status:\t\tnot supported\n");
3510 return len;
3511 }
3512 len += sprintf(p + len, "status:\t\tsupported\n");
3513
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003514 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003515 /* 570 */
3516 int i, status;
3517 for (i = 0; i < 8; i++) {
3518 if (!acpi_evalf(ec_handle,
3519 &status, "GLED", "dd", 1 << i))
3520 return -EIO;
3521 len += sprintf(p + len, "%d:\t\t%s\n",
3522 i, led_status(status));
3523 }
3524 }
3525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003527 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
3529 return len;
3530}
3531
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003532/* off, on, blink */
3533static const int led_sled_arg1[] = { 0, 1, 3 };
3534static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3535static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3536static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3537
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003538static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
3540 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003541 int led, ind, ret;
3542
3543 if (!led_supported)
3544 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003547 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return -EINVAL;
3549
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003550 if (strstr(cmd, "off")) {
3551 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003553 ind = 1;
3554 } else if (strstr(cmd, "blink")) {
3555 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 } else
3557 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003558
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003559 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003560 /* 570 */
3561 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003563 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003565 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003566 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3567 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003568 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003569 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003570 ret = ec_write(TPACPI_LED_EC_HLBL,
3571 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003572 if (ret >= 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003573 ret = ec_write(TPACPI_LED_EC_HLCL,
3574 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003575 if (ret < 0)
3576 return ret;
3577 } else {
3578 /* all others */
3579 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3580 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
3584
3585 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003586}
3587
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003588static struct ibm_struct led_driver_data = {
3589 .name = "led",
3590 .read = led_read,
3591 .write = led_write,
3592};
3593
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003594/*************************************************************************
3595 * Beep subdriver
3596 */
3597
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003598TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003599
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003600static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003601{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003602 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3603
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003604 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003605
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003606 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3607 str_supported(beep_handle != NULL));
3608
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003609 return (beep_handle)? 0 : 1;
3610}
3611
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003612static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
3614 int len = 0;
3615
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003616 if (!beep_handle)
3617 len += sprintf(p + len, "status:\t\tnot supported\n");
3618 else {
3619 len += sprintf(p + len, "status:\t\tsupported\n");
3620 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623 return len;
3624}
3625
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003626static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 char *cmd;
3629 int beep_cmd;
3630
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003631 if (!beep_handle)
3632 return -ENODEV;
3633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003635 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3636 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 /* beep_cmd set */
3638 } else
3639 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003640 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 return -EIO;
3642 }
3643
3644 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003645}
3646
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003647static struct ibm_struct beep_driver_data = {
3648 .name = "beep",
3649 .read = beep_read,
3650 .write = beep_write,
3651};
3652
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003653/*************************************************************************
3654 * Thermal subdriver
3655 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003656
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003657enum thermal_access_mode {
3658 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3659 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3660 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3661 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3662 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3663};
3664
3665enum { /* TPACPI_THERMAL_TPEC_* */
3666 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3667 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3668 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3669};
3670
3671#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3672struct ibm_thermal_sensors_struct {
3673 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3674};
3675
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003676static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003677
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003678/* idx is zero-based */
3679static int thermal_get_sensor(int idx, s32 *value)
3680{
3681 int t;
3682 s8 tmp;
3683 char tmpi[5];
3684
3685 t = TP_EC_THERMAL_TMP0;
3686
3687 switch (thermal_read_mode) {
3688#if TPACPI_MAX_THERMAL_SENSORS >= 16
3689 case TPACPI_THERMAL_TPEC_16:
3690 if (idx >= 8 && idx <= 15) {
3691 t = TP_EC_THERMAL_TMP8;
3692 idx -= 8;
3693 }
3694 /* fallthrough */
3695#endif
3696 case TPACPI_THERMAL_TPEC_8:
3697 if (idx <= 7) {
3698 if (!acpi_ec_read(t + idx, &tmp))
3699 return -EIO;
3700 *value = tmp * 1000;
3701 return 0;
3702 }
3703 break;
3704
3705 case TPACPI_THERMAL_ACPI_UPDT:
3706 if (idx <= 7) {
3707 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3708 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3709 return -EIO;
3710 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3711 return -EIO;
3712 *value = (t - 2732) * 100;
3713 return 0;
3714 }
3715 break;
3716
3717 case TPACPI_THERMAL_ACPI_TMP07:
3718 if (idx <= 7) {
3719 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3720 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3721 return -EIO;
3722 if (t > 127 || t < -127)
3723 t = TP_EC_THERMAL_TMP_NA;
3724 *value = t * 1000;
3725 return 0;
3726 }
3727 break;
3728
3729 case TPACPI_THERMAL_NONE:
3730 default:
3731 return -ENOSYS;
3732 }
3733
3734 return -EINVAL;
3735}
3736
3737static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3738{
3739 int res, i;
3740 int n;
3741
3742 n = 8;
3743 i = 0;
3744
3745 if (!s)
3746 return -EINVAL;
3747
3748 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3749 n = 16;
3750
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003751 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003752 res = thermal_get_sensor(i, &s->temp[i]);
3753 if (res)
3754 return res;
3755 }
3756
3757 return n;
3758}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003759
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003760/* sysfs temp##_input -------------------------------------------------- */
3761
3762static ssize_t thermal_temp_input_show(struct device *dev,
3763 struct device_attribute *attr,
3764 char *buf)
3765{
3766 struct sensor_device_attribute *sensor_attr =
3767 to_sensor_dev_attr(attr);
3768 int idx = sensor_attr->index;
3769 s32 value;
3770 int res;
3771
3772 res = thermal_get_sensor(idx, &value);
3773 if (res)
3774 return res;
3775 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3776 return -ENXIO;
3777
3778 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3779}
3780
3781#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003782 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3783 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003784
3785static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3786 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3787 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3788 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3789 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3790 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3791 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3792 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3793 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3794 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3795 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3796 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3797 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3798 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3799 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3800 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3801 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3802};
3803
3804#define THERMAL_ATTRS(X) \
3805 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3806
3807static struct attribute *thermal_temp_input_attr[] = {
3808 THERMAL_ATTRS(8),
3809 THERMAL_ATTRS(9),
3810 THERMAL_ATTRS(10),
3811 THERMAL_ATTRS(11),
3812 THERMAL_ATTRS(12),
3813 THERMAL_ATTRS(13),
3814 THERMAL_ATTRS(14),
3815 THERMAL_ATTRS(15),
3816 THERMAL_ATTRS(0),
3817 THERMAL_ATTRS(1),
3818 THERMAL_ATTRS(2),
3819 THERMAL_ATTRS(3),
3820 THERMAL_ATTRS(4),
3821 THERMAL_ATTRS(5),
3822 THERMAL_ATTRS(6),
3823 THERMAL_ATTRS(7),
3824 NULL
3825};
3826
3827static const struct attribute_group thermal_temp_input16_group = {
3828 .attrs = thermal_temp_input_attr
3829};
3830
3831static const struct attribute_group thermal_temp_input8_group = {
3832 .attrs = &thermal_temp_input_attr[8]
3833};
3834
3835#undef THERMAL_SENSOR_ATTR_TEMP
3836#undef THERMAL_ATTRS
3837
3838/* --------------------------------------------------------------------- */
3839
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003840static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003841{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003842 u8 t, ta1, ta2;
3843 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003844 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003845 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003846
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003847 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3848
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003849 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003850
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003851 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003852 /*
3853 * Direct EC access mode: sensors at registers
3854 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3855 * non-implemented, thermal sensors return 0x80 when
3856 * not available
3857 */
3858
3859 ta1 = ta2 = 0;
3860 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003861 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003862 ta1 |= t;
3863 } else {
3864 ta1 = 0;
3865 break;
3866 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003867 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003868 ta2 |= t;
3869 } else {
3870 ta1 = 0;
3871 break;
3872 }
3873 }
3874 if (ta1 == 0) {
3875 /* This is sheer paranoia, but we handle it anyway */
3876 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003877 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003878 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003879 "falling back to ACPI TMPx access "
3880 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003881 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003882 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003883 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003884 "ThinkPad ACPI EC access misbehaving, "
3885 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003886 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003887 }
3888 } else {
3889 thermal_read_mode =
3890 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003891 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003892 }
3893 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003894 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3895 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003896 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003897 } else {
3898 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003899 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003900 }
3901 } else {
3902 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003903 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003904 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003905
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003906 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3907 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3908 thermal_read_mode);
3909
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003910 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003911 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003912 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003913 &thermal_temp_input16_group);
3914 if (res)
3915 return res;
3916 break;
3917 case TPACPI_THERMAL_TPEC_8:
3918 case TPACPI_THERMAL_ACPI_TMP07:
3919 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003920 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003921 &thermal_temp_input8_group);
3922 if (res)
3923 return res;
3924 break;
3925 case TPACPI_THERMAL_NONE:
3926 default:
3927 return 1;
3928 }
3929
3930 return 0;
3931}
3932
3933static void thermal_exit(void)
3934{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003935 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003936 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003937 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003938 &thermal_temp_input16_group);
3939 break;
3940 case TPACPI_THERMAL_TPEC_8:
3941 case TPACPI_THERMAL_ACPI_TMP07:
3942 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003943 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003944 &thermal_temp_input16_group);
3945 break;
3946 case TPACPI_THERMAL_NONE:
3947 default:
3948 break;
3949 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003950}
3951
3952static int thermal_read(char *p)
3953{
3954 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003955 int n, i;
3956 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003957
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003958 n = thermal_get_sensors(&t);
3959 if (unlikely(n < 0))
3960 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003961
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003962 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003963
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003964 if (n > 0) {
3965 for (i = 0; i < (n - 1); i++)
3966 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3967 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3968 } else
3969 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003970
3971 return len;
3972}
3973
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003974static struct ibm_struct thermal_driver_data = {
3975 .name = "thermal",
3976 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003977 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003978};
3979
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003980/*************************************************************************
3981 * EC Dump subdriver
3982 */
3983
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003984static u8 ecdump_regs[256];
3985
3986static int ecdump_read(char *p)
3987{
3988 int len = 0;
3989 int i, j;
3990 u8 v;
3991
3992 len += sprintf(p + len, "EC "
3993 " +00 +01 +02 +03 +04 +05 +06 +07"
3994 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3995 for (i = 0; i < 256; i += 16) {
3996 len += sprintf(p + len, "EC 0x%02x:", i);
3997 for (j = 0; j < 16; j++) {
3998 if (!acpi_ec_read(i + j, &v))
3999 break;
4000 if (v != ecdump_regs[i + j])
4001 len += sprintf(p + len, " *%02x", v);
4002 else
4003 len += sprintf(p + len, " %02x", v);
4004 ecdump_regs[i + j] = v;
4005 }
4006 len += sprintf(p + len, "\n");
4007 if (j != 16)
4008 break;
4009 }
4010
4011 /* These are way too dangerous to advertise openly... */
4012#if 0
4013 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4014 " (<offset> is 00-ff, <value> is 00-ff)\n");
4015 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4016 " (<offset> is 00-ff, <value> is 0-255)\n");
4017#endif
4018 return len;
4019}
4020
4021static int ecdump_write(char *buf)
4022{
4023 char *cmd;
4024 int i, v;
4025
4026 while ((cmd = next_cmd(&buf))) {
4027 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4028 /* i and v set */
4029 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4030 /* i and v set */
4031 } else
4032 return -EINVAL;
4033 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4034 if (!acpi_ec_write(i, v))
4035 return -EIO;
4036 } else
4037 return -EINVAL;
4038 }
4039
4040 return 0;
4041}
4042
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004043static struct ibm_struct ecdump_driver_data = {
4044 .name = "ecdump",
4045 .read = ecdump_read,
4046 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004047 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004048};
4049
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004050/*************************************************************************
4051 * Backlight/brightness subdriver
4052 */
Holger Macht8acb0252006-10-20 14:30:28 -07004053
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004054#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4055
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004056static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004057static int brightness_offset = 0x31;
4058static int brightness_mode;
4059static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4060
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004061static struct mutex brightness_mutex;
4062
4063/*
4064 * ThinkPads can read brightness from two places: EC 0x31, or
4065 * CMOS NVRAM byte 0x5E, bits 0-3.
4066 */
4067static int brightness_get(struct backlight_device *bd)
4068{
4069 u8 lec = 0, lcmos = 0, level = 0;
4070
4071 if (brightness_mode & 1) {
4072 if (!acpi_ec_read(brightness_offset, &lec))
4073 return -EIO;
4074 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4075 level = lec;
4076 };
4077 if (brightness_mode & 2) {
4078 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4079 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4080 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4081 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4082 level = lcmos;
4083 }
4084
4085 if (brightness_mode == 3 && lec != lcmos) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004086 printk(TPACPI_ERR
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004087 "CMOS NVRAM (%u) and EC (%u) do not agree "
4088 "on display brightness level\n",
4089 (unsigned int) lcmos,
4090 (unsigned int) lec);
4091 return -EIO;
4092 }
4093
4094 return level;
4095}
4096
4097/* May return EINTR which can always be mapped to ERESTARTSYS */
4098static int brightness_set(int value)
4099{
4100 int cmos_cmd, inc, i, res;
4101 int current_value;
4102
4103 if (value > ((tp_features.bright_16levels)? 15 : 7))
4104 return -EINVAL;
4105
4106 res = mutex_lock_interruptible(&brightness_mutex);
4107 if (res < 0)
4108 return res;
4109
4110 current_value = brightness_get(NULL);
4111 if (current_value < 0) {
4112 res = current_value;
4113 goto errout;
4114 }
4115
4116 cmos_cmd = value > current_value ?
4117 TP_CMOS_BRIGHTNESS_UP :
4118 TP_CMOS_BRIGHTNESS_DOWN;
4119 inc = (value > current_value)? 1 : -1;
4120
4121 res = 0;
4122 for (i = current_value; i != value; i += inc) {
4123 if ((brightness_mode & 2) &&
4124 issue_thinkpad_cmos_command(cmos_cmd)) {
4125 res = -EIO;
4126 goto errout;
4127 }
4128 if ((brightness_mode & 1) &&
4129 !acpi_ec_write(brightness_offset, i + inc)) {
4130 res = -EIO;
4131 goto errout;;
4132 }
4133 }
4134
4135errout:
4136 mutex_unlock(&brightness_mutex);
4137 return res;
4138}
4139
4140/* sysfs backlight class ----------------------------------------------- */
4141
4142static int brightness_update_status(struct backlight_device *bd)
4143{
4144 /* it is the backlight class's job (caller) to handle
4145 * EINTR and other errors properly */
4146 return brightness_set(
4147 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4148 bd->props.power == FB_BLANK_UNBLANK) ?
4149 bd->props.brightness : 0);
4150}
Holger Macht8acb0252006-10-20 14:30:28 -07004151
Richard Purdie599a52d2007-02-10 23:07:48 +00004152static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004153 .get_brightness = brightness_get,
4154 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004155};
4156
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004157/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004158
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004159static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4160{
4161 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4162 union acpi_object *obj;
4163 int rc;
4164
4165 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4166 obj = (union acpi_object *)buffer.pointer;
4167 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004168 printk(TPACPI_ERR "Unknown BCLL data, "
4169 "please report this to %s\n", TPACPI_MAIL);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004170 rc = 0;
4171 } else {
4172 rc = obj->package.count;
4173 }
4174 } else {
4175 return 0;
4176 }
4177
4178 kfree(buffer.pointer);
4179 return rc;
4180}
4181
4182static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4183 void *context, void **rv)
4184{
4185 char name[ACPI_PATH_SEGMENT_LENGTH];
4186 struct acpi_buffer buffer = { sizeof(name), &name };
4187
4188 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4189 !strncmp("BCLL", name, sizeof(name) - 1)) {
4190 if (tpacpi_query_bcll_levels(handle) == 16) {
4191 *rv = handle;
4192 return AE_CTRL_TERMINATE;
4193 } else {
4194 return AE_OK;
4195 }
4196 } else {
4197 return AE_OK;
4198 }
4199}
4200
4201static int __init brightness_check_levels(void)
4202{
4203 int status;
4204 void *found_node = NULL;
4205
4206 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004207 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004208 }
4209 if (!vid_handle)
4210 return 0;
4211
4212 /* Search for a BCLL package with 16 levels */
4213 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004214 brightness_find_bcll, NULL,
4215 &found_node);
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004216
4217 return (ACPI_SUCCESS(status) && found_node != NULL);
4218}
4219
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004220static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4221 void *context, void **rv)
4222{
4223 char name[ACPI_PATH_SEGMENT_LENGTH];
4224 struct acpi_buffer buffer = { sizeof(name), &name };
4225
4226 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4227 !strncmp("_BCL", name, sizeof(name) - 1)) {
4228 *rv = handle;
4229 return AE_CTRL_TERMINATE;
4230 } else {
4231 return AE_OK;
4232 }
4233}
4234
4235static int __init brightness_check_std_acpi_support(void)
4236{
4237 int status;
4238 void *found_node = NULL;
4239
4240 if (!vid_handle) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004241 TPACPI_ACPIHANDLE_INIT(vid);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004242 }
4243 if (!vid_handle)
4244 return 0;
4245
4246 /* Search for a _BCL method, but don't execute it */
4247 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004248 brightness_find_bcl, NULL, &found_node);
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004249
4250 return (ACPI_SUCCESS(status) && found_node != NULL);
4251}
4252
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004253static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004254{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004255 int b;
4256
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004257 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4258
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004259 mutex_init(&brightness_mutex);
4260
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004261 if (!brightness_enable) {
4262 dbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004263 "brightness support disabled by "
4264 "module parameter\n");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004265 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004266 } else if (brightness_enable > 1) {
4267 if (brightness_check_std_acpi_support()) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004268 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004269 "standard ACPI backlight interface "
4270 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004271 return 1;
4272 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004273 }
4274
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004275 if (!brightness_mode) {
4276 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4277 brightness_mode = 2;
4278 else
4279 brightness_mode = 3;
4280
4281 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4282 brightness_mode);
4283 }
4284
4285 if (brightness_mode > 3)
4286 return -EINVAL;
4287
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004288 tp_features.bright_16levels =
4289 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4290 brightness_check_levels();
4291
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004292 b = brightness_get(NULL);
4293 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004294 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004295
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004296 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004297 printk(TPACPI_INFO
4298 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004299
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004300 ibm_backlight_device = backlight_device_register(
4301 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4302 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004303 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004304 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004305 return PTR_ERR(ibm_backlight_device);
4306 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004307 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004308
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004309 ibm_backlight_device->props.max_brightness =
4310 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004311 ibm_backlight_device->props.brightness = b;
4312 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004313
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004314 return 0;
4315}
4316
4317static void brightness_exit(void)
4318{
4319 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004320 vdbg_printk(TPACPI_DBG_EXIT,
4321 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004322 backlight_device_unregister(ibm_backlight_device);
4323 ibm_backlight_device = NULL;
4324 }
4325}
4326
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004327static int brightness_read(char *p)
4328{
4329 int len = 0;
4330 int level;
4331
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004332 level = brightness_get(NULL);
4333 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004334 len += sprintf(p + len, "level:\t\tunreadable\n");
4335 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004336 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004337 len += sprintf(p + len, "commands:\tup, down\n");
4338 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004339 " (<level> is 0-%d)\n",
4340 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004341 }
4342
4343 return len;
4344}
4345
4346static int brightness_write(char *buf)
4347{
4348 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004349 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004350 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004351 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004352
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004353 level = brightness_get(NULL);
4354 if (level < 0)
4355 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004356
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004357 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004358 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004359 if (level < max_level)
4360 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004361 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004362 if (level > 0)
4363 level--;
4364 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4365 level >= 0 && level <= max_level) {
4366 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004367 } else
4368 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004369 }
4370
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004371 /*
4372 * Now we know what the final level should be, so we try to set it.
4373 * Doing it this way makes the syscall restartable in case of EINTR
4374 */
4375 rc = brightness_set(level);
4376 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004377}
4378
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004379static struct ibm_struct brightness_driver_data = {
4380 .name = "brightness",
4381 .read = brightness_read,
4382 .write = brightness_write,
4383 .exit = brightness_exit,
4384};
4385
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004386/*************************************************************************
4387 * Volume subdriver
4388 */
4389
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004390static int volume_offset = 0x30;
4391
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004392static int volume_read(char *p)
4393{
4394 int len = 0;
4395 u8 level;
4396
4397 if (!acpi_ec_read(volume_offset, &level)) {
4398 len += sprintf(p + len, "level:\t\tunreadable\n");
4399 } else {
4400 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4401 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4402 len += sprintf(p + len, "commands:\tup, down, mute\n");
4403 len += sprintf(p + len, "commands:\tlevel <level>"
4404 " (<level> is 0-15)\n");
4405 }
4406
4407 return len;
4408}
4409
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004410static int volume_write(char *buf)
4411{
4412 int cmos_cmd, inc, i;
4413 u8 level, mute;
4414 int new_level, new_mute;
4415 char *cmd;
4416
4417 while ((cmd = next_cmd(&buf))) {
4418 if (!acpi_ec_read(volume_offset, &level))
4419 return -EIO;
4420 new_mute = mute = level & 0x40;
4421 new_level = level = level & 0xf;
4422
4423 if (strlencmp(cmd, "up") == 0) {
4424 if (mute)
4425 new_mute = 0;
4426 else
4427 new_level = level == 15 ? 15 : level + 1;
4428 } else if (strlencmp(cmd, "down") == 0) {
4429 if (mute)
4430 new_mute = 0;
4431 else
4432 new_level = level == 0 ? 0 : level - 1;
4433 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4434 new_level >= 0 && new_level <= 15) {
4435 /* new_level set */
4436 } else if (strlencmp(cmd, "mute") == 0) {
4437 new_mute = 0x40;
4438 } else
4439 return -EINVAL;
4440
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004441 if (new_level != level) {
4442 /* mute doesn't change */
4443
4444 cmos_cmd = (new_level > level) ?
4445 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004446 inc = new_level > level ? 1 : -1;
4447
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004448 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004449 !acpi_ec_write(volume_offset, level)))
4450 return -EIO;
4451
4452 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004453 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004454 !acpi_ec_write(volume_offset, i + inc))
4455 return -EIO;
4456
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004457 if (mute &&
4458 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4459 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004460 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004461 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004462 }
4463
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004464 if (new_mute != mute) {
4465 /* level doesn't change */
4466
4467 cmos_cmd = (new_mute) ?
4468 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004469
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004470 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004471 !acpi_ec_write(volume_offset, level + new_mute))
4472 return -EIO;
4473 }
4474 }
4475
4476 return 0;
4477}
4478
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004479static struct ibm_struct volume_driver_data = {
4480 .name = "volume",
4481 .read = volume_read,
4482 .write = volume_write,
4483};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004484
4485/*************************************************************************
4486 * Fan subdriver
4487 */
4488
4489/*
4490 * FAN ACCESS MODES
4491 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004492 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004493 * ACPI GFAN method: returns fan level
4494 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004495 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004496 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004497 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004498 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004499 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4500 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004501 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4502 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004503 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004504 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004505 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4506 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004507 *
4508 * Fan speed changes of any sort (including those caused by the
4509 * disengaged mode) are usually done slowly by the firmware as the
4510 * maximum ammount of fan duty cycle change per second seems to be
4511 * limited.
4512 *
4513 * Reading is not available if GFAN exists.
4514 * Writing is not available if SFAN exists.
4515 *
4516 * Bits
4517 * 7 automatic mode engaged;
4518 * (default operation mode of the ThinkPad)
4519 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004520 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004521 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004522 * the tachometer while the fan controller ramps up
4523 * the speed (which can take up to a few *minutes*).
4524 * Speeds up fan to 100% duty-cycle, which is far above
4525 * the standard RPM levels. It is not impossible that
4526 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004527 * 5-3 unused in some models. Extra bits for fan level
4528 * in others, but still useless as all values above
4529 * 7 map to the same speed as level 7 in these models.
4530 * 2-0 fan level (0..7 usually)
4531 * 0x00 = stop
4532 * 0x07 = max (set when temperatures critical)
4533 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004534 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004535 *
4536 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4537 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4538 * does so, its initial value is meaningless (0x07).
4539 *
4540 * For firmware bugs, refer to:
4541 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4542 *
4543 * ----
4544 *
4545 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4546 * Main fan tachometer reading (in RPM)
4547 *
4548 * This register is present on all ThinkPads with a new-style EC, and
4549 * it is known not to be present on the A21m/e, and T22, as there is
4550 * something else in offset 0x84 according to the ACPI DSDT. Other
4551 * ThinkPads from this same time period (and earlier) probably lack the
4552 * tachometer as well.
4553 *
4554 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4555 * was never fixed by IBM to report the EC firmware version string
4556 * probably support the tachometer (like the early X models), so
4557 * detecting it is quite hard. We need more data to know for sure.
4558 *
4559 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4560 * might result.
4561 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004562 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4563 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004564 *
4565 * For firmware bugs, refer to:
4566 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4567 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004568 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004569 * ThinkPad X31, X40, X41. Not available in the X60.
4570 *
4571 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4572 * high speed. ACPI DSDT seems to map these three speeds to levels
4573 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4574 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4575 *
4576 * The speeds are stored on handles
4577 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4578 *
4579 * There are three default speed sets, acessible as handles:
4580 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4581 *
4582 * ACPI DSDT switches which set is in use depending on various
4583 * factors.
4584 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004585 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004586 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4587 * but the ACPI tables just mention level 7.
4588 */
4589
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004590enum { /* Fan control constants */
4591 fan_status_offset = 0x2f, /* EC register 0x2f */
4592 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4593 * 0x84 must be read before 0x85 */
4594
4595 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4596 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4597
4598 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4599};
4600
4601enum fan_status_access_mode {
4602 TPACPI_FAN_NONE = 0, /* No fan status or control */
4603 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4604 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4605};
4606
4607enum fan_control_access_mode {
4608 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4609 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4610 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4611 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4612};
4613
4614enum fan_control_commands {
4615 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4616 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4617 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4618 * and also watchdog cmd */
4619};
4620
4621static int fan_control_allowed;
4622
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004623static enum fan_status_access_mode fan_status_access_mode;
4624static enum fan_control_access_mode fan_control_access_mode;
4625static enum fan_control_commands fan_control_commands;
4626
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004627static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004628static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004629static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004630
4631static struct mutex fan_mutex;
4632
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004633static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004634static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004635
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004636TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4637TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004638 "\\FSPD", /* 600e/x, 770e, 770x */
4639 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004640TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004641 "JFNS", /* 770x-JL */
4642 ); /* all others */
4643
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004644/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004645 * Call with fan_mutex held
4646 */
4647static void fan_update_desired_level(u8 status)
4648{
4649 if ((status &
4650 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4651 if (status > 7)
4652 fan_control_desired_level = 7;
4653 else
4654 fan_control_desired_level = status;
4655 }
4656}
4657
4658static int fan_get_status(u8 *status)
4659{
4660 u8 s;
4661
4662 /* TODO:
4663 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4664
4665 switch (fan_status_access_mode) {
4666 case TPACPI_FAN_RD_ACPI_GFAN:
4667 /* 570, 600e/x, 770e, 770x */
4668
4669 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4670 return -EIO;
4671
4672 if (likely(status))
4673 *status = s & 0x07;
4674
4675 break;
4676
4677 case TPACPI_FAN_RD_TPEC:
4678 /* all except 570, 600e/x, 770e, 770x */
4679 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4680 return -EIO;
4681
4682 if (likely(status))
4683 *status = s;
4684
4685 break;
4686
4687 default:
4688 return -ENXIO;
4689 }
4690
4691 return 0;
4692}
4693
4694static int fan_get_status_safe(u8 *status)
4695{
4696 int rc;
4697 u8 s;
4698
4699 if (mutex_lock_interruptible(&fan_mutex))
4700 return -ERESTARTSYS;
4701 rc = fan_get_status(&s);
4702 if (!rc)
4703 fan_update_desired_level(s);
4704 mutex_unlock(&fan_mutex);
4705
4706 if (status)
4707 *status = s;
4708
4709 return rc;
4710}
4711
4712static int fan_get_speed(unsigned int *speed)
4713{
4714 u8 hi, lo;
4715
4716 switch (fan_status_access_mode) {
4717 case TPACPI_FAN_RD_TPEC:
4718 /* all except 570, 600e/x, 770e, 770x */
4719 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4720 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4721 return -EIO;
4722
4723 if (likely(speed))
4724 *speed = (hi << 8) | lo;
4725
4726 break;
4727
4728 default:
4729 return -ENXIO;
4730 }
4731
4732 return 0;
4733}
4734
4735static int fan_set_level(int level)
4736{
4737 if (!fan_control_allowed)
4738 return -EPERM;
4739
4740 switch (fan_control_access_mode) {
4741 case TPACPI_FAN_WR_ACPI_SFAN:
4742 if (level >= 0 && level <= 7) {
4743 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4744 return -EIO;
4745 } else
4746 return -EINVAL;
4747 break;
4748
4749 case TPACPI_FAN_WR_ACPI_FANS:
4750 case TPACPI_FAN_WR_TPEC:
4751 if ((level != TP_EC_FAN_AUTO) &&
4752 (level != TP_EC_FAN_FULLSPEED) &&
4753 ((level < 0) || (level > 7)))
4754 return -EINVAL;
4755
4756 /* safety net should the EC not support AUTO
4757 * or FULLSPEED mode bits and just ignore them */
4758 if (level & TP_EC_FAN_FULLSPEED)
4759 level |= 7; /* safety min speed 7 */
4760 else if (level & TP_EC_FAN_FULLSPEED)
4761 level |= 4; /* safety min speed 4 */
4762
4763 if (!acpi_ec_write(fan_status_offset, level))
4764 return -EIO;
4765 else
4766 tp_features.fan_ctrl_status_undef = 0;
4767 break;
4768
4769 default:
4770 return -ENXIO;
4771 }
4772 return 0;
4773}
4774
4775static int fan_set_level_safe(int level)
4776{
4777 int rc;
4778
4779 if (!fan_control_allowed)
4780 return -EPERM;
4781
4782 if (mutex_lock_interruptible(&fan_mutex))
4783 return -ERESTARTSYS;
4784
4785 if (level == TPACPI_FAN_LAST_LEVEL)
4786 level = fan_control_desired_level;
4787
4788 rc = fan_set_level(level);
4789 if (!rc)
4790 fan_update_desired_level(level);
4791
4792 mutex_unlock(&fan_mutex);
4793 return rc;
4794}
4795
4796static int fan_set_enable(void)
4797{
4798 u8 s;
4799 int rc;
4800
4801 if (!fan_control_allowed)
4802 return -EPERM;
4803
4804 if (mutex_lock_interruptible(&fan_mutex))
4805 return -ERESTARTSYS;
4806
4807 switch (fan_control_access_mode) {
4808 case TPACPI_FAN_WR_ACPI_FANS:
4809 case TPACPI_FAN_WR_TPEC:
4810 rc = fan_get_status(&s);
4811 if (rc < 0)
4812 break;
4813
4814 /* Don't go out of emergency fan mode */
4815 if (s != 7) {
4816 s &= 0x07;
4817 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4818 }
4819
4820 if (!acpi_ec_write(fan_status_offset, s))
4821 rc = -EIO;
4822 else {
4823 tp_features.fan_ctrl_status_undef = 0;
4824 rc = 0;
4825 }
4826 break;
4827
4828 case TPACPI_FAN_WR_ACPI_SFAN:
4829 rc = fan_get_status(&s);
4830 if (rc < 0)
4831 break;
4832
4833 s &= 0x07;
4834
4835 /* Set fan to at least level 4 */
4836 s |= 4;
4837
4838 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004839 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004840 else
4841 rc = 0;
4842 break;
4843
4844 default:
4845 rc = -ENXIO;
4846 }
4847
4848 mutex_unlock(&fan_mutex);
4849 return rc;
4850}
4851
4852static int fan_set_disable(void)
4853{
4854 int rc;
4855
4856 if (!fan_control_allowed)
4857 return -EPERM;
4858
4859 if (mutex_lock_interruptible(&fan_mutex))
4860 return -ERESTARTSYS;
4861
4862 rc = 0;
4863 switch (fan_control_access_mode) {
4864 case TPACPI_FAN_WR_ACPI_FANS:
4865 case TPACPI_FAN_WR_TPEC:
4866 if (!acpi_ec_write(fan_status_offset, 0x00))
4867 rc = -EIO;
4868 else {
4869 fan_control_desired_level = 0;
4870 tp_features.fan_ctrl_status_undef = 0;
4871 }
4872 break;
4873
4874 case TPACPI_FAN_WR_ACPI_SFAN:
4875 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4876 rc = -EIO;
4877 else
4878 fan_control_desired_level = 0;
4879 break;
4880
4881 default:
4882 rc = -ENXIO;
4883 }
4884
4885
4886 mutex_unlock(&fan_mutex);
4887 return rc;
4888}
4889
4890static int fan_set_speed(int speed)
4891{
4892 int rc;
4893
4894 if (!fan_control_allowed)
4895 return -EPERM;
4896
4897 if (mutex_lock_interruptible(&fan_mutex))
4898 return -ERESTARTSYS;
4899
4900 rc = 0;
4901 switch (fan_control_access_mode) {
4902 case TPACPI_FAN_WR_ACPI_FANS:
4903 if (speed >= 0 && speed <= 65535) {
4904 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4905 speed, speed, speed))
4906 rc = -EIO;
4907 } else
4908 rc = -EINVAL;
4909 break;
4910
4911 default:
4912 rc = -ENXIO;
4913 }
4914
4915 mutex_unlock(&fan_mutex);
4916 return rc;
4917}
4918
4919static void fan_watchdog_reset(void)
4920{
4921 static int fan_watchdog_active;
4922
4923 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4924 return;
4925
4926 if (fan_watchdog_active)
4927 cancel_delayed_work(&fan_watchdog_task);
4928
4929 if (fan_watchdog_maxinterval > 0 &&
4930 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4931 fan_watchdog_active = 1;
4932 if (!schedule_delayed_work(&fan_watchdog_task,
4933 msecs_to_jiffies(fan_watchdog_maxinterval
4934 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004935 printk(TPACPI_ERR
4936 "failed to schedule the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004937 "watchdog will not trigger\n");
4938 }
4939 } else
4940 fan_watchdog_active = 0;
4941}
4942
4943static void fan_watchdog_fire(struct work_struct *ignored)
4944{
4945 int rc;
4946
4947 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4948 return;
4949
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004950 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004951 rc = fan_set_enable();
4952 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004953 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004954 "will try again later...\n", -rc);
4955 /* reschedule for later */
4956 fan_watchdog_reset();
4957 }
4958}
4959
4960/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004961 * SYSFS fan layout: hwmon compatible (device)
4962 *
4963 * pwm*_enable:
4964 * 0: "disengaged" mode
4965 * 1: manual mode
4966 * 2: native EC "auto" mode (recommended, hardware default)
4967 *
4968 * pwm*: set speed in manual mode, ignored otherwise.
4969 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4970 * interpolation.
4971 *
4972 * fan*_input: tachometer reading, RPM
4973 *
4974 *
4975 * SYSFS fan layout: extensions
4976 *
4977 * fan_watchdog (driver):
4978 * fan watchdog interval in seconds, 0 disables (default), max 120
4979 */
4980
4981/* sysfs fan pwm1_enable ----------------------------------------------- */
4982static ssize_t fan_pwm1_enable_show(struct device *dev,
4983 struct device_attribute *attr,
4984 char *buf)
4985{
4986 int res, mode;
4987 u8 status;
4988
4989 res = fan_get_status_safe(&status);
4990 if (res)
4991 return res;
4992
4993 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4994 if (status != fan_control_initial_status) {
4995 tp_features.fan_ctrl_status_undef = 0;
4996 } else {
4997 /* Return most likely status. In fact, it
4998 * might be the only possible status */
4999 status = TP_EC_FAN_AUTO;
5000 }
5001 }
5002
5003 if (status & TP_EC_FAN_FULLSPEED) {
5004 mode = 0;
5005 } else if (status & TP_EC_FAN_AUTO) {
5006 mode = 2;
5007 } else
5008 mode = 1;
5009
5010 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5011}
5012
5013static ssize_t fan_pwm1_enable_store(struct device *dev,
5014 struct device_attribute *attr,
5015 const char *buf, size_t count)
5016{
5017 unsigned long t;
5018 int res, level;
5019
5020 if (parse_strtoul(buf, 2, &t))
5021 return -EINVAL;
5022
5023 switch (t) {
5024 case 0:
5025 level = TP_EC_FAN_FULLSPEED;
5026 break;
5027 case 1:
5028 level = TPACPI_FAN_LAST_LEVEL;
5029 break;
5030 case 2:
5031 level = TP_EC_FAN_AUTO;
5032 break;
5033 case 3:
5034 /* reserved for software-controlled auto mode */
5035 return -ENOSYS;
5036 default:
5037 return -EINVAL;
5038 }
5039
5040 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005041 if (res == -ENXIO)
5042 return -EINVAL;
5043 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005044 return res;
5045
5046 fan_watchdog_reset();
5047
5048 return count;
5049}
5050
5051static struct device_attribute dev_attr_fan_pwm1_enable =
5052 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5053 fan_pwm1_enable_show, fan_pwm1_enable_store);
5054
5055/* sysfs fan pwm1 ------------------------------------------------------ */
5056static ssize_t fan_pwm1_show(struct device *dev,
5057 struct device_attribute *attr,
5058 char *buf)
5059{
5060 int res;
5061 u8 status;
5062
5063 res = fan_get_status_safe(&status);
5064 if (res)
5065 return res;
5066
5067 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5068 if (status != fan_control_initial_status) {
5069 tp_features.fan_ctrl_status_undef = 0;
5070 } else {
5071 status = TP_EC_FAN_AUTO;
5072 }
5073 }
5074
5075 if ((status &
5076 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5077 status = fan_control_desired_level;
5078
5079 if (status > 7)
5080 status = 7;
5081
5082 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5083}
5084
5085static ssize_t fan_pwm1_store(struct device *dev,
5086 struct device_attribute *attr,
5087 const char *buf, size_t count)
5088{
5089 unsigned long s;
5090 int rc;
5091 u8 status, newlevel;
5092
5093 if (parse_strtoul(buf, 255, &s))
5094 return -EINVAL;
5095
5096 /* scale down from 0-255 to 0-7 */
5097 newlevel = (s >> 5) & 0x07;
5098
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005099 if (mutex_lock_interruptible(&fan_mutex))
5100 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005101
5102 rc = fan_get_status(&status);
5103 if (!rc && (status &
5104 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5105 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005106 if (rc == -ENXIO)
5107 rc = -EINVAL;
5108 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005109 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005110 fan_watchdog_reset();
5111 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005112 }
5113
5114 mutex_unlock(&fan_mutex);
5115 return (rc)? rc : count;
5116}
5117
5118static struct device_attribute dev_attr_fan_pwm1 =
5119 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5120 fan_pwm1_show, fan_pwm1_store);
5121
5122/* sysfs fan fan1_input ------------------------------------------------ */
5123static ssize_t fan_fan1_input_show(struct device *dev,
5124 struct device_attribute *attr,
5125 char *buf)
5126{
5127 int res;
5128 unsigned int speed;
5129
5130 res = fan_get_speed(&speed);
5131 if (res < 0)
5132 return res;
5133
5134 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5135}
5136
5137static struct device_attribute dev_attr_fan_fan1_input =
5138 __ATTR(fan1_input, S_IRUGO,
5139 fan_fan1_input_show, NULL);
5140
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005141/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005142static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5143 char *buf)
5144{
5145 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5146}
5147
5148static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5149 const char *buf, size_t count)
5150{
5151 unsigned long t;
5152
5153 if (parse_strtoul(buf, 120, &t))
5154 return -EINVAL;
5155
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005156 if (!fan_control_allowed)
5157 return -EPERM;
5158
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005159 fan_watchdog_maxinterval = t;
5160 fan_watchdog_reset();
5161
5162 return count;
5163}
5164
5165static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5166 fan_fan_watchdog_show, fan_fan_watchdog_store);
5167
5168/* --------------------------------------------------------------------- */
5169static struct attribute *fan_attributes[] = {
5170 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5171 &dev_attr_fan_fan1_input.attr,
5172 NULL
5173};
5174
5175static const struct attribute_group fan_attr_group = {
5176 .attrs = fan_attributes,
5177};
5178
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005179static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005180{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005181 int rc;
5182
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005183 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5184
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005185 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005186 fan_status_access_mode = TPACPI_FAN_NONE;
5187 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005188 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005189 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005190 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005191 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005192
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005193 TPACPI_ACPIHANDLE_INIT(fans);
5194 TPACPI_ACPIHANDLE_INIT(gfan);
5195 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005196
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005197 if (gfan_handle) {
5198 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005199 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005200 } else {
5201 /* all other ThinkPads: note that even old-style
5202 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005203 if (likely(acpi_ec_read(fan_status_offset,
5204 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005205 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005206
5207 /* In some ThinkPads, neither the EC nor the ACPI
5208 * DSDT initialize the fan status, and it ends up
5209 * being set to 0x07 when it *could* be either
5210 * 0x07 or 0x80.
5211 *
5212 * Enable for TP-1Y (T43), TP-78 (R51e),
5213 * TP-76 (R52), TP-70 (T43, R52), which are known
5214 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005215 if (fan_control_initial_status == 0x07) {
5216 switch (thinkpad_id.ec_model) {
5217 case 0x5931: /* TP-1Y */
5218 case 0x3837: /* TP-78 */
5219 case 0x3637: /* TP-76 */
5220 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005221 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005222 "fan_init: initial fan status "
5223 "is unknown, assuming it is "
5224 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005225 tp_features.fan_ctrl_status_undef = 1;
5226 ;;
5227 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005228 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005229 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005230 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005231 "ThinkPad ACPI EC access misbehaving, "
5232 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005233 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005234 }
5235 }
5236
5237 if (sfan_handle) {
5238 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005239 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005240 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005241 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005242 } else {
5243 if (!gfan_handle) {
5244 /* gfan without sfan means no fan control */
5245 /* all other models implement TP EC 0x2f control */
5246
5247 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005248 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005249 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005250 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005251 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005252 TPACPI_FAN_CMD_SPEED |
5253 TPACPI_FAN_CMD_LEVEL |
5254 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005255 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005256 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005257 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005258 TPACPI_FAN_CMD_LEVEL |
5259 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005260 }
5261 }
5262 }
5263
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005264 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5265 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5266 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5267 fan_status_access_mode, fan_control_access_mode);
5268
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005269 /* fan control master switch */
5270 if (!fan_control_allowed) {
5271 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5272 fan_control_commands = 0;
5273 dbg_printk(TPACPI_DBG_INIT,
5274 "fan control features disabled by parameter\n");
5275 }
5276
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005277 /* update fan_control_desired_level */
5278 if (fan_status_access_mode != TPACPI_FAN_NONE)
5279 fan_get_status_safe(NULL);
5280
5281 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5282 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005283 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005284 &fan_attr_group);
5285 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005286 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005287 &driver_attr_fan_watchdog);
5288 if (rc < 0)
5289 return rc;
5290 return 0;
5291 } else
5292 return 1;
5293}
5294
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005295static void fan_exit(void)
5296{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005297 vdbg_printk(TPACPI_DBG_EXIT,
5298 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005299
5300 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005301 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005302 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5303 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005304
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005305 cancel_delayed_work(&fan_watchdog_task);
5306 flush_scheduled_work();
5307}
5308
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005309static int fan_read(char *p)
5310{
5311 int len = 0;
5312 int rc;
5313 u8 status;
5314 unsigned int speed = 0;
5315
5316 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005317 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005318 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005319 rc = fan_get_status_safe(&status);
5320 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005321 return rc;
5322
5323 len += sprintf(p + len, "status:\t\t%s\n"
5324 "level:\t\t%d\n",
5325 (status != 0) ? "enabled" : "disabled", status);
5326 break;
5327
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005328 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005329 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005330 rc = fan_get_status_safe(&status);
5331 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005332 return rc;
5333
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005334 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005335 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005336 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005337 else
5338 /* Return most likely status. In fact, it
5339 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005340 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005341 }
5342
5343 len += sprintf(p + len, "status:\t\t%s\n",
5344 (status != 0) ? "enabled" : "disabled");
5345
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005346 rc = fan_get_speed(&speed);
5347 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005348 return rc;
5349
5350 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5351
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005352 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005353 /* Disengaged mode takes precedence */
5354 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005355 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005356 len += sprintf(p + len, "level:\t\tauto\n");
5357 else
5358 len += sprintf(p + len, "level:\t\t%d\n", status);
5359 break;
5360
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005361 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005362 default:
5363 len += sprintf(p + len, "status:\t\tnot supported\n");
5364 }
5365
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005366 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005367 len += sprintf(p + len, "commands:\tlevel <level>");
5368
5369 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005370 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005371 len += sprintf(p + len, " (<level> is 0-7)\n");
5372 break;
5373
5374 default:
5375 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005376 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005377 break;
5378 }
5379 }
5380
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005381 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005382 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005383 "commands:\twatchdog <timeout> (<timeout> "
5384 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005385
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005386 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005387 len += sprintf(p + len, "commands:\tspeed <speed>"
5388 " (<speed> is 0-65535)\n");
5389
5390 return len;
5391}
5392
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005393static int fan_write_cmd_level(const char *cmd, int *rc)
5394{
5395 int level;
5396
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005397 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005398 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005399 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005400 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005401 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005402 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005403 return 0;
5404
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005405 *rc = fan_set_level_safe(level);
5406 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005407 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005408 "access mode %d", fan_control_access_mode);
5409
5410 return 1;
5411}
5412
5413static int fan_write_cmd_enable(const char *cmd, int *rc)
5414{
5415 if (strlencmp(cmd, "enable") != 0)
5416 return 0;
5417
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005418 *rc = fan_set_enable();
5419 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005420 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005421 "access mode %d", fan_control_access_mode);
5422
5423 return 1;
5424}
5425
5426static int fan_write_cmd_disable(const char *cmd, int *rc)
5427{
5428 if (strlencmp(cmd, "disable") != 0)
5429 return 0;
5430
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005431 *rc = fan_set_disable();
5432 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005433 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005434 "access mode %d", fan_control_access_mode);
5435
5436 return 1;
5437}
5438
5439static int fan_write_cmd_speed(const char *cmd, int *rc)
5440{
5441 int speed;
5442
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005443 /* TODO:
5444 * Support speed <low> <medium> <high> ? */
5445
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005446 if (sscanf(cmd, "speed %d", &speed) != 1)
5447 return 0;
5448
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005449 *rc = fan_set_speed(speed);
5450 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005451 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005452 "access mode %d", fan_control_access_mode);
5453
5454 return 1;
5455}
5456
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005457static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5458{
5459 int interval;
5460
5461 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5462 return 0;
5463
5464 if (interval < 0 || interval > 120)
5465 *rc = -EINVAL;
5466 else
5467 fan_watchdog_maxinterval = interval;
5468
5469 return 1;
5470}
5471
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005472static int fan_write(char *buf)
5473{
5474 char *cmd;
5475 int rc = 0;
5476
5477 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005478 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005479 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005480 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005481 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005482 fan_write_cmd_disable(cmd, &rc) ||
5483 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005484 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005485 fan_write_cmd_speed(cmd, &rc))
5486 )
5487 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005488 else if (!rc)
5489 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005490 }
5491
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005492 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005493}
5494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005495static struct ibm_struct fan_driver_data = {
5496 .name = "fan",
5497 .read = fan_read,
5498 .write = fan_write,
5499 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005500};
5501
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005502/****************************************************************************
5503 ****************************************************************************
5504 *
5505 * Infrastructure
5506 *
5507 ****************************************************************************
5508 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005510/* sysfs name ---------------------------------------------------------- */
5511static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5512 struct device_attribute *attr,
5513 char *buf)
5514{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005515 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005516}
5517
5518static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5519 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5520
5521/* --------------------------------------------------------------------- */
5522
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005523/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03005524static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005525
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005526/*
5527 * Module and infrastructure proble, init and exit handling
5528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005530static int force_load;
5531
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005532#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005533static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005534{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005535 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005536
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005537 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005538}
5539#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5540
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005541static void ibm_exit(struct ibm_struct *ibm)
5542{
5543 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5544
5545 list_del_init(&ibm->all_drivers);
5546
5547 if (ibm->flags.acpi_notify_installed) {
5548 dbg_printk(TPACPI_DBG_EXIT,
5549 "%s: acpi_remove_notify_handler\n", ibm->name);
5550 BUG_ON(!ibm->acpi);
5551 acpi_remove_notify_handler(*ibm->acpi->handle,
5552 ibm->acpi->type,
5553 dispatch_acpi_notify);
5554 ibm->flags.acpi_notify_installed = 0;
5555 ibm->flags.acpi_notify_installed = 0;
5556 }
5557
5558 if (ibm->flags.proc_created) {
5559 dbg_printk(TPACPI_DBG_EXIT,
5560 "%s: remove_proc_entry\n", ibm->name);
5561 remove_proc_entry(ibm->name, proc_dir);
5562 ibm->flags.proc_created = 0;
5563 }
5564
5565 if (ibm->flags.acpi_driver_registered) {
5566 dbg_printk(TPACPI_DBG_EXIT,
5567 "%s: acpi_bus_unregister_driver\n", ibm->name);
5568 BUG_ON(!ibm->acpi);
5569 acpi_bus_unregister_driver(ibm->acpi->driver);
5570 kfree(ibm->acpi->driver);
5571 ibm->acpi->driver = NULL;
5572 ibm->flags.acpi_driver_registered = 0;
5573 }
5574
5575 if (ibm->flags.init_called && ibm->exit) {
5576 ibm->exit();
5577 ibm->flags.init_called = 0;
5578 }
5579
5580 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5581}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005582
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005583static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584{
5585 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005586 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 struct proc_dir_entry *entry;
5588
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005589 BUG_ON(ibm == NULL);
5590
5591 INIT_LIST_HEAD(&ibm->all_drivers);
5592
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005593 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 return 0;
5595
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005596 dbg_printk(TPACPI_DBG_INIT,
5597 "probing for %s\n", ibm->name);
5598
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005599 if (iibm->init) {
5600 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005601 if (ret > 0)
5602 return 0; /* probe failed */
5603 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005605
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005606 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 }
5608
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005609 if (ibm->acpi) {
5610 if (ibm->acpi->hid) {
5611 ret = register_tpacpi_subdriver(ibm);
5612 if (ret)
5613 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005614 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005615
5616 if (ibm->acpi->notify) {
5617 ret = setup_acpi_notify(ibm);
5618 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005619 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005620 ibm->name);
5621 ret = 0;
5622 goto err_out;
5623 }
5624 if (ret < 0)
5625 goto err_out;
5626 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005627 }
5628
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005629 dbg_printk(TPACPI_DBG_INIT,
5630 "%s installed\n", ibm->name);
5631
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005632 if (ibm->read) {
5633 entry = create_proc_entry(ibm->name,
5634 S_IFREG | S_IRUGO | S_IWUSR,
5635 proc_dir);
5636 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005637 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005638 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005639 ret = -ENODEV;
5640 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005641 }
5642 entry->owner = THIS_MODULE;
5643 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005644 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005645 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005646 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005647 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005650 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005653
5654err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005655 dbg_printk(TPACPI_DBG_INIT,
5656 "%s: at error exit path with result %d\n",
5657 ibm->name, ret);
5658
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005659 ibm_exit(ibm);
5660 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661}
5662
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005663/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005665static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005666{
Jeff Garzik18552562007-10-03 15:15:40 -04005667 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005668 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005669
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005670 if (!tp)
5671 return;
5672
5673 memset(tp, 0, sizeof(*tp));
5674
5675 if (dmi_name_in_vendors("IBM"))
5676 tp->vendor = PCI_VENDOR_ID_IBM;
5677 else if (dmi_name_in_vendors("LENOVO"))
5678 tp->vendor = PCI_VENDOR_ID_LENOVO;
5679 else
5680 return;
5681
5682 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5683 GFP_KERNEL);
5684 if (!tp->bios_version_str)
5685 return;
5686 tp->bios_model = tp->bios_version_str[0]
5687 | (tp->bios_version_str[1] << 8);
5688
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005689 /*
5690 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5691 * X32 or newer, all Z series; Some models must have an
5692 * up-to-date BIOS or they will not be detected.
5693 *
5694 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5695 */
5696 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005697 if (sscanf(dev->name,
5698 "IBM ThinkPad Embedded Controller -[%17c",
5699 ec_fw_string) == 1) {
5700 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5701 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005702
5703 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5704 tp->ec_model = ec_fw_string[0]
5705 | (ec_fw_string[1] << 8);
5706 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005707 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005708 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005709
5710 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5711 GFP_KERNEL);
5712 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5713 kfree(tp->model_str);
5714 tp->model_str = NULL;
5715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005718static int __init probe_for_thinkpad(void)
5719{
5720 int is_thinkpad;
5721
5722 if (acpi_disabled)
5723 return -ENODEV;
5724
5725 /*
5726 * Non-ancient models have better DMI tagging, but very old models
5727 * don't.
5728 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005729 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005730
5731 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005732 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005733 if (!ec_handle) {
5734 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005735 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005736 "Not yet supported ThinkPad detected!\n");
5737 return -ENODEV;
5738 }
5739
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005740 /*
5741 * Risks a regression on very old machines, but reduces potential
5742 * false positives a damn great deal
5743 */
5744 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005745 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005746
5747 if (!is_thinkpad && !force_load)
5748 return -ENODEV;
5749
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005750 return 0;
5751}
5752
5753
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005754/* Module init, exit, parameters */
5755
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005756static struct ibm_init_struct ibms_init[] __initdata = {
5757 {
5758 .init = thinkpad_acpi_driver_init,
5759 .data = &thinkpad_acpi_driver_data,
5760 },
5761 {
5762 .init = hotkey_init,
5763 .data = &hotkey_driver_data,
5764 },
5765 {
5766 .init = bluetooth_init,
5767 .data = &bluetooth_driver_data,
5768 },
5769 {
5770 .init = wan_init,
5771 .data = &wan_driver_data,
5772 },
5773 {
5774 .init = video_init,
5775 .data = &video_driver_data,
5776 },
5777 {
5778 .init = light_init,
5779 .data = &light_driver_data,
5780 },
5781#ifdef CONFIG_THINKPAD_ACPI_DOCK
5782 {
5783 .init = dock_init,
5784 .data = &dock_driver_data[0],
5785 },
5786 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005787 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005788 .data = &dock_driver_data[1],
5789 },
5790#endif
5791#ifdef CONFIG_THINKPAD_ACPI_BAY
5792 {
5793 .init = bay_init,
5794 .data = &bay_driver_data,
5795 },
5796#endif
5797 {
5798 .init = cmos_init,
5799 .data = &cmos_driver_data,
5800 },
5801 {
5802 .init = led_init,
5803 .data = &led_driver_data,
5804 },
5805 {
5806 .init = beep_init,
5807 .data = &beep_driver_data,
5808 },
5809 {
5810 .init = thermal_init,
5811 .data = &thermal_driver_data,
5812 },
5813 {
5814 .data = &ecdump_driver_data,
5815 },
5816 {
5817 .init = brightness_init,
5818 .data = &brightness_driver_data,
5819 },
5820 {
5821 .data = &volume_driver_data,
5822 },
5823 {
5824 .init = fan_init,
5825 .data = &fan_driver_data,
5826 },
5827};
5828
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005829static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5830{
5831 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005832 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005833
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005834 if (!kp || !kp->name || !val)
5835 return -EINVAL;
5836
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005837 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5838 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005839 WARN_ON(ibm == NULL);
5840
5841 if (!ibm || !ibm->name)
5842 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005843
5844 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5845 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005846 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005847 strcpy(ibms_init[i].param, val);
5848 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005849 return 0;
5850 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005851 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005852
5853 return -EINVAL;
5854}
5855
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005856module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005857MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005858 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005859
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005860module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005861MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005862
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005863module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005864MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005865 "Attempts to load the driver even on a "
5866 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005867
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005868module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005869MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005870 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005871
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005872module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005873MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005874 "Selects brightness control strategy: "
5875 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005876
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005877module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005878MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005879 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005880
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005881module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005882MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005883 "used for backwards compatibility with userspace, "
5884 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005885
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005886#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02005887 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5888 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005889 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005890
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005891TPACPI_PARAM(hotkey);
5892TPACPI_PARAM(bluetooth);
5893TPACPI_PARAM(video);
5894TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005895#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005896TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005897#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005898#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005899TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005900#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005901TPACPI_PARAM(cmos);
5902TPACPI_PARAM(led);
5903TPACPI_PARAM(beep);
5904TPACPI_PARAM(ecdump);
5905TPACPI_PARAM(brightness);
5906TPACPI_PARAM(volume);
5907TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005908
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005909static void thinkpad_acpi_module_exit(void)
5910{
5911 struct ibm_struct *ibm, *itmp;
5912
5913 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5914
5915 list_for_each_entry_safe_reverse(ibm, itmp,
5916 &tpacpi_all_drivers,
5917 all_drivers) {
5918 ibm_exit(ibm);
5919 }
5920
5921 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5922
5923 if (tpacpi_inputdev) {
5924 if (tp_features.input_device_registered)
5925 input_unregister_device(tpacpi_inputdev);
5926 else
5927 input_free_device(tpacpi_inputdev);
5928 }
5929
5930 if (tpacpi_hwmon)
5931 hwmon_device_unregister(tpacpi_hwmon);
5932
5933 if (tp_features.sensors_pdev_attrs_registered)
5934 device_remove_file(&tpacpi_sensors_pdev->dev,
5935 &dev_attr_thinkpad_acpi_pdev_name);
5936 if (tpacpi_sensors_pdev)
5937 platform_device_unregister(tpacpi_sensors_pdev);
5938 if (tpacpi_pdev)
5939 platform_device_unregister(tpacpi_pdev);
5940
5941 if (tp_features.sensors_pdrv_attrs_registered)
5942 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5943 if (tp_features.platform_drv_attrs_registered)
5944 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5945
5946 if (tp_features.sensors_pdrv_registered)
5947 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5948
5949 if (tp_features.platform_drv_registered)
5950 platform_driver_unregister(&tpacpi_pdriver);
5951
5952 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005953 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005954
5955 kfree(thinkpad_id.bios_version_str);
5956 kfree(thinkpad_id.ec_version_str);
5957 kfree(thinkpad_id.model_str);
5958}
5959
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005960
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005961static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962{
5963 int ret, i;
5964
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005965 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5966
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005967 /* Parameter checking */
5968 if (hotkey_report_mode > 2)
5969 return -EINVAL;
5970
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005971 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005972
5973 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005974 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005975 if (ret) {
5976 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005977 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005980 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005981
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005982 TPACPI_ACPIHANDLE_INIT(ecrd);
5983 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005985 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005987 printk(TPACPI_ERR
5988 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005989 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 return -ENODEV;
5991 }
5992 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005993
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005994 ret = platform_driver_register(&tpacpi_pdriver);
5995 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005996 printk(TPACPI_ERR
5997 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005998 thinkpad_acpi_module_exit();
5999 return ret;
6000 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006001 tp_features.platform_drv_registered = 1;
6002
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006003 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6004 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006005 printk(TPACPI_ERR
6006 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006007 thinkpad_acpi_module_exit();
6008 return ret;
6009 }
6010 tp_features.sensors_pdrv_registered = 1;
6011
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006012 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006013 if (!ret) {
6014 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006015 ret = tpacpi_create_driver_attributes(
6016 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006017 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006018 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006019 printk(TPACPI_ERR
6020 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006021 thinkpad_acpi_module_exit();
6022 return ret;
6023 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006024 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006025
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006026
6027 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006028 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006029 NULL, 0);
6030 if (IS_ERR(tpacpi_pdev)) {
6031 ret = PTR_ERR(tpacpi_pdev);
6032 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006033 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006034 thinkpad_acpi_module_exit();
6035 return ret;
6036 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006037 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006038 TPACPI_HWMON_DRVR_NAME,
6039 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006040 if (IS_ERR(tpacpi_sensors_pdev)) {
6041 ret = PTR_ERR(tpacpi_sensors_pdev);
6042 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006043 printk(TPACPI_ERR
6044 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006045 thinkpad_acpi_module_exit();
6046 return ret;
6047 }
6048 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6049 &dev_attr_thinkpad_acpi_pdev_name);
6050 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006051 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006052 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006053 thinkpad_acpi_module_exit();
6054 return ret;
6055 }
6056 tp_features.sensors_pdev_attrs_registered = 1;
6057 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006058 if (IS_ERR(tpacpi_hwmon)) {
6059 ret = PTR_ERR(tpacpi_hwmon);
6060 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006061 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006062 thinkpad_acpi_module_exit();
6063 return ret;
6064 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006065 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006066 tpacpi_inputdev = input_allocate_device();
6067 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006068 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006069 thinkpad_acpi_module_exit();
6070 return -ENOMEM;
6071 } else {
6072 /* Prepare input device, but don't register */
6073 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006074 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006075 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006076 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6077 thinkpad_id.vendor :
6078 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006079 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6080 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6081 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006082 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6083 ret = ibm_init(&ibms_init[i]);
6084 if (ret >= 0 && *ibms_init[i].param)
6085 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006087 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 return ret;
6089 }
6090 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006091 ret = input_register_device(tpacpi_inputdev);
6092 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006093 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006094 thinkpad_acpi_module_exit();
6095 return ret;
6096 } else {
6097 tp_features.input_device_registered = 1;
6098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006100 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 return 0;
6102}
6103
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006104/* Please remove this in year 2009 */
6105MODULE_ALIAS("ibm_acpi");
6106
6107/*
6108 * DMI matching for module autoloading
6109 *
6110 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6111 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6112 *
6113 * Only models listed in thinkwiki will be supported, so add yours
6114 * if it is not there yet.
6115 */
6116#define IBM_BIOS_MODULE_ALIAS(__type) \
6117 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6118
6119/* Non-ancient thinkpads */
6120MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6121MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6122
6123/* Ancient thinkpad BIOSes have to be identified by
6124 * BIOS type or model number, and there are far less
6125 * BIOS types than model numbers... */
6126IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6127IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6128IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6129
6130MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006131MODULE_DESCRIPTION(TPACPI_DESC);
6132MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006133MODULE_LICENSE("GPL");
6134
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006135module_init(thinkpad_acpi_module_init);
6136module_exit(thinkpad_acpi_module_exit);