ACPI: thinkpad-acpi: prepare for bluetooth and wwan rfkill support
[linux-2.6-block.git] / drivers / misc / thinkpad_acpi.c
CommitLineData
1da177e4 1/*
643f12db 2 * thinkpad_acpi.c - ThinkPad ACPI Extras
1da177e4
LT
3 *
4 *
78f81cc4 5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6a2e293c 6 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
1da177e4
LT
7 *
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
a62bc916
HMH
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
78f81cc4
BD
22 */
23
68f12ae5 24#define TPACPI_VERSION "0.20"
50ebec09 25#define TPACPI_SYSFS_VERSION 0x020200
78f81cc4
BD
26
27/*
1da177e4 28 * Changelog:
4b45cc07
HMH
29 * 2007-10-20 changelog trimmed down
30 *
643f12db
HMH
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
f9ff43a6
HMH
33 *
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.
837ca6dd 37 *
78f81cc4
BD
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
4b45cc07
HMH
40 *
41 * 2005-01-16 0.9 use MODULE_VERSION
78f81cc4
BD
42 * 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>
1da177e4
LT
48 */
49
0c78039f
HMH
50#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>
4fa6811b 70#include <linux/leds.h>
0c78039f
HMH
71#include <asm/uaccess.h>
72
73#include <linux/dmi.h>
74#include <linux/jiffies.h>
75#include <linux/workqueue.h>
76
77#include <acpi/acpi_drivers.h>
78#include <acpi/acnamesp.h>
79
80#include <linux/pci_ids.h>
81
0c78039f
HMH
82
83/* ThinkPad CMOS commands */
84#define TP_CMOS_VOLUME_DOWN 0
85#define TP_CMOS_VOLUME_UP 1
86#define TP_CMOS_VOLUME_MUTE 2
87#define TP_CMOS_BRIGHTNESS_UP 4
88#define TP_CMOS_BRIGHTNESS_DOWN 5
4fa6811b
HMH
89#define TP_CMOS_THINKLIGHT_ON 12
90#define TP_CMOS_THINKLIGHT_OFF 13
0c78039f
HMH
91
92/* NVRAM Addresses */
93enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
99};
100
101/* NVRAM bit masks */
102enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
116};
117
b21a15f6 118/* ACPI HIDs */
e0c7dfe7 119#define TPACPI_ACPI_HKEY_HID "IBM0068"
b21a15f6
HMH
120
121/* Input IDs */
122#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123#define TPACPI_HKEY_INPUT_VERSION 0x4101
124
125
126/****************************************************************************
127 * Main driver
128 */
129
e0c7dfe7
HMH
130#define TPACPI_NAME "thinkpad"
131#define TPACPI_DESC "ThinkPad ACPI Extras"
132#define TPACPI_FILE TPACPI_NAME "_acpi"
133#define TPACPI_URL "http://ibm-acpi.sf.net/"
134#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
b21a15f6 135
e0c7dfe7
HMH
136#define TPACPI_PROC_DIR "ibm"
137#define TPACPI_ACPI_EVENT_PREFIX "ibm"
138#define TPACPI_DRVR_NAME TPACPI_FILE
95e57ab2 139#define TPACPI_DRVR_SHORTNAME "tpacpi"
e0c7dfe7 140#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
b21a15f6 141
95e57ab2 142#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
e0e3c061 143#define TPACPI_WORKQUEUE_NAME "ktpacpid"
95e57ab2 144
e0c7dfe7 145#define TPACPI_MAX_ACPI_ARGS 3
b21a15f6 146
0c78039f 147/* Debugging */
e0c7dfe7
HMH
148#define TPACPI_LOG TPACPI_FILE ": "
149#define TPACPI_ERR KERN_ERR TPACPI_LOG
150#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
151#define TPACPI_INFO KERN_INFO TPACPI_LOG
152#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
b21a15f6 153
0c78039f
HMH
154#define TPACPI_DBG_ALL 0xffff
155#define TPACPI_DBG_ALL 0xffff
156#define TPACPI_DBG_INIT 0x0001
157#define TPACPI_DBG_EXIT 0x0002
158#define dbg_printk(a_dbg_level, format, arg...) \
159 do { if (dbg_level & a_dbg_level) \
35ff8b9f
HMH
160 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
161 } while (0)
0c78039f
HMH
162#ifdef CONFIG_THINKPAD_ACPI_DEBUG
163#define vdbg_printk(a_dbg_level, format, arg...) \
164 dbg_printk(a_dbg_level, format, ## arg)
165static const char *str_supported(int is_supported);
166#else
167#define vdbg_printk(a_dbg_level, format, arg...)
168#endif
169
35ff8b9f
HMH
170#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
171#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
172#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
0c78039f 173
0c78039f
HMH
174
175/****************************************************************************
b21a15f6 176 * Driver-wide structs and misc. variables
0c78039f
HMH
177 */
178
179struct ibm_struct;
180
181struct tp_acpi_drv_struct {
182 const struct acpi_device_id *hid;
183 struct acpi_driver *driver;
184
185 void (*notify) (struct ibm_struct *, u32);
186 acpi_handle *handle;
187 u32 type;
188 struct acpi_device *device;
189};
190
191struct ibm_struct {
192 char *name;
193
194 int (*read) (char *);
195 int (*write) (char *);
196 void (*exit) (void);
197 void (*resume) (void);
083f1760 198 void (*suspend) (pm_message_t state);
0c78039f
HMH
199
200 struct list_head all_drivers;
201
202 struct tp_acpi_drv_struct *acpi;
203
204 struct {
205 u8 acpi_driver_registered:1;
206 u8 acpi_notify_installed:1;
207 u8 proc_created:1;
208 u8 init_called:1;
209 u8 experimental:1;
210 } flags;
211};
212
213struct ibm_init_struct {
214 char param[32];
215
216 int (*init) (struct ibm_init_struct *);
217 struct ibm_struct *data;
218};
219
220static struct {
221#ifdef CONFIG_THINKPAD_ACPI_BAY
222 u32 bay_status:1;
223 u32 bay_eject:1;
224 u32 bay_status2:1;
225 u32 bay_eject2:1;
226#endif
227 u32 bluetooth:1;
228 u32 hotkey:1;
229 u32 hotkey_mask:1;
230 u32 hotkey_wlsw:1;
6c231bd5 231 u32 hotkey_tablet:1;
0c78039f
HMH
232 u32 light:1;
233 u32 light_status:1;
234 u32 bright_16levels:1;
b5972796 235 u32 bright_acpimode:1;
0c78039f
HMH
236 u32 wan:1;
237 u32 fan_ctrl_status_undef:1;
238 u32 input_device_registered:1;
239 u32 platform_drv_registered:1;
240 u32 platform_drv_attrs_registered:1;
241 u32 sensors_pdrv_registered:1;
242 u32 sensors_pdrv_attrs_registered:1;
243 u32 sensors_pdev_attrs_registered:1;
244 u32 hotkey_poll_active:1;
245} tp_features;
246
92889022
HMH
247static struct {
248 u16 hotkey_mask_ff:1;
2d5e94d7 249 u16 bright_cmos_ec_unsync:1;
92889022
HMH
250} tp_warned;
251
0c78039f
HMH
252struct thinkpad_id_data {
253 unsigned int vendor; /* ThinkPad vendor:
254 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
255
256 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
257 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
258
259 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
260 u16 ec_model;
261
8c74adbc
HMH
262 char *model_str; /* ThinkPad T43 */
263 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
0c78039f 264};
0c78039f
HMH
265static struct thinkpad_id_data thinkpad_id;
266
8fef502e
HMH
267static enum {
268 TPACPI_LIFE_INIT = 0,
269 TPACPI_LIFE_RUNNING,
270 TPACPI_LIFE_EXITING,
271} tpacpi_lifecycle;
272
b21a15f6
HMH
273static int experimental;
274static u32 dbg_level;
275
e0e3c061
HMH
276static struct workqueue_struct *tpacpi_wq;
277
4fa6811b
HMH
278/* Special LED class that can defer work */
279struct tpacpi_led_classdev {
280 struct led_classdev led_classdev;
281 struct work_struct work;
282 enum led_brightness new_brightness;
af116101 283 unsigned int led;
4fa6811b
HMH
284};
285
56b6aeb0
HMH
286/****************************************************************************
287 ****************************************************************************
288 *
289 * ACPI Helpers and device model
290 *
291 ****************************************************************************
292 ****************************************************************************/
293
294/*************************************************************************
295 * ACPI basic handles
296 */
1da177e4 297
94954cc6 298static acpi_handle root_handle;
1da177e4 299
e0c7dfe7 300#define TPACPI_HANDLE(object, parent, paths...) \
1da177e4
LT
301 static acpi_handle object##_handle; \
302 static acpi_handle *object##_parent = &parent##_handle; \
78f81cc4 303 static char *object##_path; \
1da177e4
LT
304 static char *object##_paths[] = { paths }
305
e0c7dfe7 306TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
78f81cc4
BD
307 "\\_SB.PCI.ISA.EC", /* 570 */
308 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
309 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
310 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
311 "\\_SB.PCI0.ICH3.EC0", /* R31 */
312 "\\_SB.PCI0.LPC.EC", /* all others */
56b6aeb0 313 );
78f81cc4 314
e0c7dfe7
HMH
315TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
316TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
78f81cc4 317
35ff8b9f
HMH
318TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
319 /* T4x, X31, X40 */
78f81cc4
BD
320 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
321 "\\CMS", /* R40, R40e */
56b6aeb0 322 ); /* all others */
78f81cc4 323
e0c7dfe7 324TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
78f81cc4
BD
325 "^HKEY", /* R30, R31 */
326 "HKEY", /* all others */
56b6aeb0 327 ); /* 570 */
78f81cc4 328
d7c1d17d
HMH
329TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
330 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
331 "\\_SB.PCI0.VID0", /* 770e */
332 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
333 "\\_SB.PCI0.AGP.VID", /* all others */
334 ); /* R30, R31 */
335
78f81cc4 336
56b6aeb0
HMH
337/*************************************************************************
338 * ACPI helpers
a8b7a662
HMH
339 */
340
1da177e4
LT
341static int acpi_evalf(acpi_handle handle,
342 void *res, char *method, char *fmt, ...)
343{
344 char *fmt0 = fmt;
78f81cc4 345 struct acpi_object_list params;
e0c7dfe7 346 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
78f81cc4
BD
347 struct acpi_buffer result, *resultp;
348 union acpi_object out_obj;
349 acpi_status status;
350 va_list ap;
351 char res_type;
352 int success;
353 int quiet;
1da177e4
LT
354
355 if (!*fmt) {
e0c7dfe7 356 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
1da177e4
LT
357 return 0;
358 }
359
360 if (*fmt == 'q') {
361 quiet = 1;
362 fmt++;
363 } else
364 quiet = 0;
365
366 res_type = *(fmt++);
367
368 params.count = 0;
369 params.pointer = &in_objs[0];
370
371 va_start(ap, fmt);
372 while (*fmt) {
373 char c = *(fmt++);
374 switch (c) {
375 case 'd': /* int */
376 in_objs[params.count].integer.value = va_arg(ap, int);
377 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
378 break;
78f81cc4 379 /* add more types as needed */
1da177e4 380 default:
e0c7dfe7 381 printk(TPACPI_ERR "acpi_evalf() called "
1da177e4
LT
382 "with invalid format character '%c'\n", c);
383 return 0;
384 }
385 }
386 va_end(ap);
387
78f81cc4
BD
388 if (res_type != 'v') {
389 result.length = sizeof(out_obj);
390 result.pointer = &out_obj;
391 resultp = &result;
392 } else
393 resultp = NULL;
1da177e4 394
78f81cc4 395 status = acpi_evaluate_object(handle, method, &params, resultp);
1da177e4
LT
396
397 switch (res_type) {
78f81cc4 398 case 'd': /* int */
1da177e4
LT
399 if (res)
400 *(int *)res = out_obj.integer.value;
401 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
402 break;
78f81cc4 403 case 'v': /* void */
1da177e4
LT
404 success = status == AE_OK;
405 break;
78f81cc4 406 /* add more types as needed */
1da177e4 407 default:
e0c7dfe7 408 printk(TPACPI_ERR "acpi_evalf() called "
1da177e4
LT
409 "with invalid format character '%c'\n", res_type);
410 return 0;
411 }
412
56b6aeb0 413 if (!success && !quiet)
e0c7dfe7 414 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
56b6aeb0
HMH
415 method, fmt0, status);
416
417 return success;
418}
419
35ff8b9f 420static int acpi_ec_read(int i, u8 *p)
56b6aeb0
HMH
421{
422 int v;
423
424 if (ecrd_handle) {
425 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
426 return 0;
427 *p = v;
428 } else {
429 if (ec_read(i, p) < 0)
430 return 0;
431 }
432
433 return 1;
434}
435
436static int acpi_ec_write(int i, u8 v)
437{
438 if (ecwr_handle) {
439 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
440 return 0;
441 } else {
442 if (ec_write(i, v) < 0)
443 return 0;
444 }
445
446 return 1;
447}
448
013c40e4 449#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
56b6aeb0
HMH
450static int _sta(acpi_handle handle)
451{
452 int status;
453
454 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
455 status = 0;
456
457 return status;
458}
013c40e4 459#endif
56b6aeb0 460
c9bea99c
HMH
461static int issue_thinkpad_cmos_command(int cmos_cmd)
462{
463 if (!cmos_handle)
464 return -ENXIO;
465
466 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
467 return -EIO;
468
469 return 0;
470}
471
56b6aeb0
HMH
472/*************************************************************************
473 * ACPI device model
474 */
475
35ff8b9f
HMH
476#define TPACPI_ACPIHANDLE_INIT(object) \
477 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
f74a27d4
HMH
478 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
479
8d376cd6 480static void drv_acpi_handle_init(char *name,
5fba344c
HMH
481 acpi_handle *handle, acpi_handle parent,
482 char **paths, int num_paths, char **path)
56b6aeb0
HMH
483{
484 int i;
485 acpi_status status;
486
5ae930e6
HMH
487 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
488 name);
489
56b6aeb0
HMH
490 for (i = 0; i < num_paths; i++) {
491 status = acpi_get_handle(parent, paths[i], handle);
492 if (ACPI_SUCCESS(status)) {
493 *path = paths[i];
5ae930e6
HMH
494 dbg_printk(TPACPI_DBG_INIT,
495 "Found ACPI handle %s for %s\n",
496 *path, name);
56b6aeb0
HMH
497 return;
498 }
499 }
500
5ae930e6
HMH
501 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
502 name);
56b6aeb0
HMH
503 *handle = NULL;
504}
505
8d376cd6 506static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
56b6aeb0
HMH
507{
508 struct ibm_struct *ibm = data;
509
8fef502e
HMH
510 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
511 return;
512
8d376cd6 513 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
56b6aeb0
HMH
514 return;
515
8d376cd6 516 ibm->acpi->notify(ibm, event);
56b6aeb0
HMH
517}
518
8d376cd6 519static int __init setup_acpi_notify(struct ibm_struct *ibm)
56b6aeb0
HMH
520{
521 acpi_status status;
5ae930e6 522 int rc;
56b6aeb0 523
8d376cd6
HMH
524 BUG_ON(!ibm->acpi);
525
526 if (!*ibm->acpi->handle)
56b6aeb0
HMH
527 return 0;
528
5ae930e6 529 vdbg_printk(TPACPI_DBG_INIT,
fe08bc4b
HMH
530 "setting up ACPI notify for %s\n", ibm->name);
531
5ae930e6
HMH
532 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
533 if (rc < 0) {
e0c7dfe7 534 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
5ae930e6 535 ibm->name, rc);
56b6aeb0
HMH
536 return -ENODEV;
537 }
538
8d376cd6
HMH
539 acpi_driver_data(ibm->acpi->device) = ibm;
540 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
e0c7dfe7 541 TPACPI_ACPI_EVENT_PREFIX,
643f12db 542 ibm->name);
56b6aeb0 543
8d376cd6
HMH
544 status = acpi_install_notify_handler(*ibm->acpi->handle,
545 ibm->acpi->type, dispatch_acpi_notify, ibm);
56b6aeb0
HMH
546 if (ACPI_FAILURE(status)) {
547 if (status == AE_ALREADY_EXISTS) {
35ff8b9f
HMH
548 printk(TPACPI_NOTICE
549 "another device driver is already "
550 "handling %s events\n", ibm->name);
56b6aeb0 551 } else {
35ff8b9f
HMH
552 printk(TPACPI_ERR
553 "acpi_install_notify_handler(%s) failed: %d\n",
554 ibm->name, status);
56b6aeb0
HMH
555 }
556 return -ENODEV;
557 }
8d376cd6 558 ibm->flags.acpi_notify_installed = 1;
56b6aeb0
HMH
559 return 0;
560}
561
8d376cd6 562static int __init tpacpi_device_add(struct acpi_device *device)
56b6aeb0
HMH
563{
564 return 0;
565}
566
6700121b 567static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
56b6aeb0 568{
5ae930e6 569 int rc;
56b6aeb0 570
fe08bc4b
HMH
571 dbg_printk(TPACPI_DBG_INIT,
572 "registering %s as an ACPI driver\n", ibm->name);
573
8d376cd6
HMH
574 BUG_ON(!ibm->acpi);
575
576 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
577 if (!ibm->acpi->driver) {
e0c7dfe7 578 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
5fba344c 579 return -ENOMEM;
56b6aeb0
HMH
580 }
581
e0c7dfe7 582 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
8d376cd6 583 ibm->acpi->driver->ids = ibm->acpi->hid;
1ba90e3a 584
8d376cd6 585 ibm->acpi->driver->ops.add = &tpacpi_device_add;
56b6aeb0 586
5ae930e6
HMH
587 rc = acpi_bus_register_driver(ibm->acpi->driver);
588 if (rc < 0) {
e0c7dfe7 589 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
1ba90e3a 590 ibm->name, rc);
8d376cd6
HMH
591 kfree(ibm->acpi->driver);
592 ibm->acpi->driver = NULL;
5ae930e6 593 } else if (!rc)
8d376cd6 594 ibm->flags.acpi_driver_registered = 1;
56b6aeb0 595
5ae930e6 596 return rc;
56b6aeb0
HMH
597}
598
599
600/****************************************************************************
601 ****************************************************************************
602 *
603 * Procfs Helpers
604 *
605 ****************************************************************************
606 ****************************************************************************/
607
8d376cd6
HMH
608static int dispatch_procfs_read(char *page, char **start, off_t off,
609 int count, int *eof, void *data)
56b6aeb0
HMH
610{
611 struct ibm_struct *ibm = data;
612 int len;
613
614 if (!ibm || !ibm->read)
615 return -EINVAL;
616
617 len = ibm->read(page);
618 if (len < 0)
619 return len;
620
621 if (len <= off + count)
622 *eof = 1;
623 *start = page + off;
624 len -= off;
625 if (len > count)
626 len = count;
627 if (len < 0)
628 len = 0;
629
630 return len;
631}
632
8d376cd6 633static int dispatch_procfs_write(struct file *file,
35ff8b9f 634 const char __user *userbuf,
8d376cd6 635 unsigned long count, void *data)
56b6aeb0
HMH
636{
637 struct ibm_struct *ibm = data;
638 char *kernbuf;
639 int ret;
640
641 if (!ibm || !ibm->write)
642 return -EINVAL;
643
644 kernbuf = kmalloc(count + 2, GFP_KERNEL);
645 if (!kernbuf)
646 return -ENOMEM;
1da177e4 647
56b6aeb0
HMH
648 if (copy_from_user(kernbuf, userbuf, count)) {
649 kfree(kernbuf);
650 return -EFAULT;
651 }
1da177e4 652
56b6aeb0
HMH
653 kernbuf[count] = 0;
654 strcat(kernbuf, ",");
655 ret = ibm->write(kernbuf);
656 if (ret == 0)
657 ret = count;
1da177e4 658
56b6aeb0
HMH
659 kfree(kernbuf);
660
661 return ret;
1da177e4
LT
662}
663
664static char *next_cmd(char **cmds)
665{
666 char *start = *cmds;
667 char *end;
668
669 while ((end = strchr(start, ',')) && end == start)
670 start = end + 1;
671
672 if (!end)
673 return NULL;
674
675 *end = 0;
676 *cmds = end + 1;
677 return start;
678}
679
56b6aeb0 680
54ae1501
HMH
681/****************************************************************************
682 ****************************************************************************
683 *
7f5d1cd6 684 * Device model: input, hwmon and platform
54ae1501
HMH
685 *
686 ****************************************************************************
687 ****************************************************************************/
688
94954cc6 689static struct platform_device *tpacpi_pdev;
7fd40029 690static struct platform_device *tpacpi_sensors_pdev;
1beeffe4 691static struct device *tpacpi_hwmon;
7f5d1cd6 692static struct input_dev *tpacpi_inputdev;
8523ed6f 693static struct mutex tpacpi_inputdev_send_mutex;
b21a15f6 694static LIST_HEAD(tpacpi_all_drivers);
e295e850 695
083f1760
HMH
696static int tpacpi_suspend_handler(struct platform_device *pdev,
697 pm_message_t state)
698{
699 struct ibm_struct *ibm, *itmp;
700
701 list_for_each_entry_safe(ibm, itmp,
702 &tpacpi_all_drivers,
703 all_drivers) {
704 if (ibm->suspend)
705 (ibm->suspend)(state);
706 }
707
708 return 0;
709}
710
e295e850
HMH
711static int tpacpi_resume_handler(struct platform_device *pdev)
712{
713 struct ibm_struct *ibm, *itmp;
714
715 list_for_each_entry_safe(ibm, itmp,
716 &tpacpi_all_drivers,
717 all_drivers) {
718 if (ibm->resume)
719 (ibm->resume)();
720 }
721
722 return 0;
723}
724
54ae1501
HMH
725static struct platform_driver tpacpi_pdriver = {
726 .driver = {
e0c7dfe7 727 .name = TPACPI_DRVR_NAME,
54ae1501
HMH
728 .owner = THIS_MODULE,
729 },
083f1760 730 .suspend = tpacpi_suspend_handler,
e295e850 731 .resume = tpacpi_resume_handler,
54ae1501
HMH
732};
733
7fd40029
HMH
734static struct platform_driver tpacpi_hwmon_pdriver = {
735 .driver = {
e0c7dfe7 736 .name = TPACPI_HWMON_DRVR_NAME,
7fd40029
HMH
737 .owner = THIS_MODULE,
738 },
739};
54ae1501 740
176750d6 741/*************************************************************************
b21a15f6 742 * sysfs support helpers
176750d6
HMH
743 */
744
b21a15f6
HMH
745struct attribute_set {
746 unsigned int members, max_members;
747 struct attribute_group group;
176750d6
HMH
748};
749
7252374a
HMH
750struct attribute_set_obj {
751 struct attribute_set s;
752 struct attribute *a;
753} __attribute__((packed));
754
755static struct attribute_set *create_attr_set(unsigned int max_members,
35ff8b9f 756 const char *name)
7252374a
HMH
757{
758 struct attribute_set_obj *sobj;
759
760 if (max_members == 0)
761 return NULL;
762
763 /* Allocates space for implicit NULL at the end too */
764 sobj = kzalloc(sizeof(struct attribute_set_obj) +
765 max_members * sizeof(struct attribute *),
766 GFP_KERNEL);
767 if (!sobj)
768 return NULL;
769 sobj->s.max_members = max_members;
770 sobj->s.group.attrs = &sobj->a;
771 sobj->s.group.name = name;
772
773 return &sobj->s;
774}
775
f74a27d4
HMH
776#define destroy_attr_set(_set) \
777 kfree(_set);
778
7252374a 779/* not multi-threaded safe, use it in a single thread per set */
35ff8b9f 780static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
7252374a
HMH
781{
782 if (!s || !attr)
783 return -EINVAL;
784
785 if (s->members >= s->max_members)
786 return -ENOMEM;
787
788 s->group.attrs[s->members] = attr;
789 s->members++;
790
791 return 0;
792}
793
35ff8b9f 794static int add_many_to_attr_set(struct attribute_set *s,
7252374a
HMH
795 struct attribute **attr,
796 unsigned int count)
797{
798 int i, res;
799
800 for (i = 0; i < count; i++) {
801 res = add_to_attr_set(s, attr[i]);
802 if (res)
803 return res;
804 }
805
806 return 0;
807}
808
35ff8b9f 809static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
7252374a
HMH
810{
811 sysfs_remove_group(kobj, &s->group);
812 destroy_attr_set(s);
813}
814
f74a27d4
HMH
815#define register_attr_set_with_sysfs(_attr_set, _kobj) \
816 sysfs_create_group(_kobj, &_attr_set->group)
817
7252374a
HMH
818static int parse_strtoul(const char *buf,
819 unsigned long max, unsigned long *value)
820{
821 char *endp;
822
32afbf07
HMH
823 while (*buf && isspace(*buf))
824 buf++;
7252374a
HMH
825 *value = simple_strtoul(buf, &endp, 0);
826 while (*endp && isspace(*endp))
827 endp++;
828 if (*endp || *value > max)
829 return -EINVAL;
830
831 return 0;
832}
833
b5972796
HMH
834static int __init tpacpi_query_bcl_levels(acpi_handle handle)
835{
836 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
837 union acpi_object *obj;
838 int rc;
839
840 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
841 obj = (union acpi_object *)buffer.pointer;
842 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
843 printk(TPACPI_ERR "Unknown _BCL data, "
844 "please report this to %s\n", TPACPI_MAIL);
845 rc = 0;
846 } else {
847 rc = obj->package.count;
848 }
849 } else {
850 return 0;
851 }
852
853 kfree(buffer.pointer);
854 return rc;
855}
856
857static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
858 u32 lvl, void *context, void **rv)
859{
860 char name[ACPI_PATH_SEGMENT_LENGTH];
861 struct acpi_buffer buffer = { sizeof(name), &name };
862
863 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
864 !strncmp("_BCL", name, sizeof(name) - 1)) {
865 BUG_ON(!rv || !*rv);
866 **(int **)rv = tpacpi_query_bcl_levels(handle);
867 return AE_CTRL_TERMINATE;
868 } else {
869 return AE_OK;
870 }
871}
872
873/*
874 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
875 */
876static int __init tpacpi_check_std_acpi_brightness_support(void)
877{
878 int status;
879 int bcl_levels = 0;
880 void *bcl_ptr = &bcl_levels;
881
882 if (!vid_handle) {
883 TPACPI_ACPIHANDLE_INIT(vid);
884 }
885 if (!vid_handle)
886 return 0;
887
888 /*
889 * Search for a _BCL method, and execute it. This is safe on all
890 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
891 * BIOS in ACPI backlight control mode. We do NOT have to care
892 * about calling the _BCL method in an enabled video device, any
893 * will do for our purposes.
894 */
895
896 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
897 tpacpi_acpi_walk_find_bcl, NULL,
898 &bcl_ptr);
899
900 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
901 tp_features.bright_acpimode = 1;
902 return (bcl_levels - 2);
903 }
904
905 return 0;
906}
907
56b6aeb0 908/*************************************************************************
b21a15f6 909 * thinkpad-acpi driver attributes
56b6aeb0
HMH
910 */
911
b21a15f6
HMH
912/* interface_version --------------------------------------------------- */
913static ssize_t tpacpi_driver_interface_version_show(
914 struct device_driver *drv,
915 char *buf)
1da177e4 916{
b21a15f6
HMH
917 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
918}
919
920static DRIVER_ATTR(interface_version, S_IRUGO,
921 tpacpi_driver_interface_version_show, NULL);
922
923/* debug_level --------------------------------------------------------- */
924static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
925 char *buf)
926{
927 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
928}
929
930static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
931 const char *buf, size_t count)
932{
933 unsigned long t;
934
935 if (parse_strtoul(buf, 0xffff, &t))
936 return -EINVAL;
937
938 dbg_level = t;
939
940 return count;
941}
942
943static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
944 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
945
946/* version ------------------------------------------------------------- */
947static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
948 char *buf)
949{
35ff8b9f
HMH
950 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
951 TPACPI_DESC, TPACPI_VERSION);
b21a15f6
HMH
952}
953
954static DRIVER_ATTR(version, S_IRUGO,
955 tpacpi_driver_version_show, NULL);
956
957/* --------------------------------------------------------------------- */
958
35ff8b9f 959static struct driver_attribute *tpacpi_driver_attributes[] = {
b21a15f6
HMH
960 &driver_attr_debug_level, &driver_attr_version,
961 &driver_attr_interface_version,
962};
963
964static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
965{
966 int i, res;
967
968 i = 0;
969 res = 0;
970 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
971 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
972 i++;
973 }
974
975 return res;
976}
977
978static void tpacpi_remove_driver_attributes(struct device_driver *drv)
979{
980 int i;
981
35ff8b9f 982 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
b21a15f6
HMH
983 driver_remove_file(drv, tpacpi_driver_attributes[i]);
984}
985
986/****************************************************************************
987 ****************************************************************************
988 *
989 * Subdrivers
990 *
991 ****************************************************************************
992 ****************************************************************************/
993
994/*************************************************************************
995 * thinkpad-acpi init subdriver
996 */
997
998static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
999{
e0c7dfe7
HMH
1000 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1001 printk(TPACPI_INFO "%s\n", TPACPI_URL);
b21a15f6 1002
e0c7dfe7 1003 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
b21a15f6
HMH
1004 (thinkpad_id.bios_version_str) ?
1005 thinkpad_id.bios_version_str : "unknown",
1006 (thinkpad_id.ec_version_str) ?
1007 thinkpad_id.ec_version_str : "unknown");
d5a2f2f1
HMH
1008
1009 if (thinkpad_id.vendor && thinkpad_id.model_str)
8c74adbc 1010 printk(TPACPI_INFO "%s %s, model %s\n",
d5a2f2f1
HMH
1011 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1012 "IBM" : ((thinkpad_id.vendor ==
1013 PCI_VENDOR_ID_LENOVO) ?
1014 "Lenovo" : "Unknown vendor"),
8c74adbc
HMH
1015 thinkpad_id.model_str,
1016 (thinkpad_id.nummodel_str) ?
1017 thinkpad_id.nummodel_str : "unknown");
3945ac36 1018
1da177e4
LT
1019 return 0;
1020}
1021
643f12db 1022static int thinkpad_acpi_driver_read(char *p)
1da177e4
LT
1023{
1024 int len = 0;
1025
e0c7dfe7
HMH
1026 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1027 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1da177e4
LT
1028
1029 return len;
1030}
1031
a5763f22
HMH
1032static struct ibm_struct thinkpad_acpi_driver_data = {
1033 .name = "driver",
1034 .read = thinkpad_acpi_driver_read,
1035};
1036
56b6aeb0
HMH
1037/*************************************************************************
1038 * Hotkey subdriver
1039 */
1040
f74a27d4
HMH
1041enum { /* hot key scan codes (derived from ACPI DSDT) */
1042 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1043 TP_ACPI_HOTKEYSCAN_FNF2,
1044 TP_ACPI_HOTKEYSCAN_FNF3,
1045 TP_ACPI_HOTKEYSCAN_FNF4,
1046 TP_ACPI_HOTKEYSCAN_FNF5,
1047 TP_ACPI_HOTKEYSCAN_FNF6,
1048 TP_ACPI_HOTKEYSCAN_FNF7,
1049 TP_ACPI_HOTKEYSCAN_FNF8,
1050 TP_ACPI_HOTKEYSCAN_FNF9,
1051 TP_ACPI_HOTKEYSCAN_FNF10,
1052 TP_ACPI_HOTKEYSCAN_FNF11,
1053 TP_ACPI_HOTKEYSCAN_FNF12,
1054 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1055 TP_ACPI_HOTKEYSCAN_FNINSERT,
1056 TP_ACPI_HOTKEYSCAN_FNDELETE,
1057 TP_ACPI_HOTKEYSCAN_FNHOME,
1058 TP_ACPI_HOTKEYSCAN_FNEND,
1059 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1060 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1061 TP_ACPI_HOTKEYSCAN_FNSPACE,
1062 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1063 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1064 TP_ACPI_HOTKEYSCAN_MUTE,
1065 TP_ACPI_HOTKEYSCAN_THINKPAD,
1066};
1067
01e88f25
HMH
1068enum { /* Keys available through NVRAM polling */
1069 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1070 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1071};
1072
1073enum { /* Positions of some of the keys in hotkey masks */
1074 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1075 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1076 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1077 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1078 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1079 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1080 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1081 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1082 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1083 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1084 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1085};
1086
1087enum { /* NVRAM to ACPI HKEY group map */
1088 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1089 TP_ACPI_HKEY_ZOOM_MASK |
1090 TP_ACPI_HKEY_DISPSWTCH_MASK |
1091 TP_ACPI_HKEY_HIBERNATE_MASK,
1092 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1093 TP_ACPI_HKEY_BRGHTDWN_MASK,
1094 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1095 TP_ACPI_HKEY_VOLDWN_MASK |
1096 TP_ACPI_HKEY_MUTE_MASK,
1097};
1098
1099#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1100struct tp_nvram_state {
1101 u16 thinkpad_toggle:1;
1102 u16 zoom_toggle:1;
1103 u16 display_toggle:1;
1104 u16 thinklight_toggle:1;
1105 u16 hibernate_toggle:1;
1106 u16 displayexp_toggle:1;
1107 u16 display_state:1;
1108 u16 brightness_toggle:1;
1109 u16 volume_toggle:1;
1110 u16 mute:1;
1111
1112 u8 brightness_level;
1113 u8 volume_level;
1114};
1115
1116static struct task_struct *tpacpi_hotkey_task;
1117static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1118static int hotkey_poll_freq = 10; /* Hz */
1119static struct mutex hotkey_thread_mutex;
1120static struct mutex hotkey_thread_data_mutex;
1121static unsigned int hotkey_config_change;
1122
1123#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1124
1125#define hotkey_source_mask 0U
1126
1127#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1128
f74a27d4
HMH
1129static struct mutex hotkey_mutex;
1130
a713b4d7
HMH
1131static enum { /* Reasons for waking up */
1132 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1133 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1134 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1135} hotkey_wakeup_reason;
1136
1137static int hotkey_autosleep_ack;
1138
78f81cc4 1139static int hotkey_orig_status;
ae92bd17 1140static u32 hotkey_orig_mask;
9b010de5 1141static u32 hotkey_all_mask;
6a38abbf 1142static u32 hotkey_reserved_mask;
b2c985e7 1143static u32 hotkey_mask;
6a38abbf 1144
b21a15f6
HMH
1145static unsigned int hotkey_report_mode;
1146
edf0e0e5 1147static u16 *hotkey_keycode_map;
78f81cc4 1148
94954cc6 1149static struct attribute_set *hotkey_dev_attributes;
a0416420 1150
01e88f25
HMH
1151#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1152#define HOTKEY_CONFIG_CRITICAL_START \
35ff8b9f
HMH
1153 do { \
1154 mutex_lock(&hotkey_thread_data_mutex); \
1155 hotkey_config_change++; \
1156 } while (0);
01e88f25
HMH
1157#define HOTKEY_CONFIG_CRITICAL_END \
1158 mutex_unlock(&hotkey_thread_data_mutex);
1159#else
1160#define HOTKEY_CONFIG_CRITICAL_START
1161#define HOTKEY_CONFIG_CRITICAL_END
1162#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1163
6c231bd5
HMH
1164/* HKEY.MHKG() return bits */
1165#define TP_HOTKEY_TABLET_MASK (1 << 3)
1166
74941a69
HMH
1167static int hotkey_get_wlsw(int *status)
1168{
1169 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1170 return -EIO;
1171 return 0;
1172}
1173
6c231bd5
HMH
1174static int hotkey_get_tablet_mode(int *status)
1175{
1176 int s;
1177
1178 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1179 return -EIO;
1180
cee47f5a
HMH
1181 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1182 return 0;
6c231bd5
HMH
1183}
1184
b2c985e7
HMH
1185/*
1186 * Call with hotkey_mutex held
1187 */
1188static int hotkey_mask_get(void)
1189{
01e88f25
HMH
1190 u32 m = 0;
1191
b2c985e7 1192 if (tp_features.hotkey_mask) {
01e88f25 1193 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
b2c985e7
HMH
1194 return -EIO;
1195 }
01e88f25 1196 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
b2c985e7
HMH
1197
1198 return 0;
1199}
1200
1201/*
1202 * Call with hotkey_mutex held
1203 */
1204static int hotkey_mask_set(u32 mask)
1205{
1206 int i;
1207 int rc = 0;
1208
1209 if (tp_features.hotkey_mask) {
92889022
HMH
1210 if (!tp_warned.hotkey_mask_ff &&
1211 (mask == 0xffff || mask == 0xffffff ||
1212 mask == 0xffffffff)) {
1213 tp_warned.hotkey_mask_ff = 1;
1214 printk(TPACPI_NOTICE
1215 "setting the hotkey mask to 0x%08x is likely "
1216 "not the best way to go about it\n", mask);
1217 printk(TPACPI_NOTICE
1218 "please consider using the driver defaults, "
1219 "and refer to up-to-date thinkpad-acpi "
1220 "documentation\n");
1221 }
1222
01e88f25 1223 HOTKEY_CONFIG_CRITICAL_START
b2c985e7
HMH
1224 for (i = 0; i < 32; i++) {
1225 u32 m = 1 << i;
01e88f25
HMH
1226 /* enable in firmware mask only keys not in NVRAM
1227 * mode, but enable the key in the cached hotkey_mask
1228 * regardless of mode, or the key will end up
1229 * disabled by hotkey_mask_get() */
b2c985e7
HMH
1230 if (!acpi_evalf(hkey_handle,
1231 NULL, "MHKM", "vdd", i + 1,
01e88f25 1232 !!((mask & ~hotkey_source_mask) & m))) {
b2c985e7
HMH
1233 rc = -EIO;
1234 break;
1235 } else {
1236 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1237 }
1238 }
01e88f25 1239 HOTKEY_CONFIG_CRITICAL_END
b2c985e7
HMH
1240
1241 /* hotkey_mask_get must be called unconditionally below */
01e88f25
HMH
1242 if (!hotkey_mask_get() && !rc &&
1243 (hotkey_mask & ~hotkey_source_mask) !=
1244 (mask & ~hotkey_source_mask)) {
e0c7dfe7 1245 printk(TPACPI_NOTICE
b2c985e7
HMH
1246 "requested hot key mask 0x%08x, but "
1247 "firmware forced it to 0x%08x\n",
1248 mask, hotkey_mask);
1249 }
01e88f25
HMH
1250 } else {
1251#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1252 HOTKEY_CONFIG_CRITICAL_START
1253 hotkey_mask = mask & hotkey_source_mask;
1254 HOTKEY_CONFIG_CRITICAL_END
1255 hotkey_mask_get();
1256 if (hotkey_mask != mask) {
e0c7dfe7 1257 printk(TPACPI_NOTICE
01e88f25
HMH
1258 "requested hot key mask 0x%08x, "
1259 "forced to 0x%08x (NVRAM poll mask is "
1260 "0x%08x): no firmware mask support\n",
1261 mask, hotkey_mask, hotkey_source_mask);
1262 }
1263#else
1264 hotkey_mask_get();
1265 rc = -ENXIO;
1266#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
b2c985e7
HMH
1267 }
1268
1269 return rc;
1270}
1271
1272static int hotkey_status_get(int *status)
1273{
1274 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1275 return -EIO;
1276
1277 return 0;
1278}
1279
1280static int hotkey_status_set(int status)
1281{
1282 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1283 return -EIO;
1284
1285 return 0;
1286}
1287
6c231bd5 1288static void tpacpi_input_send_tabletsw(void)
b3ec6f91 1289{
6c231bd5 1290 int state;
b3ec6f91 1291
6c231bd5
HMH
1292 if (tp_features.hotkey_tablet &&
1293 !hotkey_get_tablet_mode(&state)) {
1294 mutex_lock(&tpacpi_inputdev_send_mutex);
b3ec6f91 1295
6c231bd5
HMH
1296 input_report_switch(tpacpi_inputdev,
1297 SW_TABLET_MODE, !!state);
1298 input_sync(tpacpi_inputdev);
1299
1300 mutex_unlock(&tpacpi_inputdev_send_mutex);
1301 }
b3ec6f91
HMH
1302}
1303
b7c8c200
HMH
1304static void tpacpi_input_send_key(unsigned int scancode)
1305{
1306 unsigned int keycode;
1307
1308 keycode = hotkey_keycode_map[scancode];
1309
1310 if (keycode != KEY_RESERVED) {
1311 mutex_lock(&tpacpi_inputdev_send_mutex);
1312
1313 input_report_key(tpacpi_inputdev, keycode, 1);
1314 if (keycode == KEY_UNKNOWN)
1315 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1316 scancode);
1317 input_sync(tpacpi_inputdev);
1318
1319 input_report_key(tpacpi_inputdev, keycode, 0);
1320 if (keycode == KEY_UNKNOWN)
1321 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1322 scancode);
1323 input_sync(tpacpi_inputdev);
1324
1325 mutex_unlock(&tpacpi_inputdev_send_mutex);
1326 }
1327}
1328
01e88f25
HMH
1329#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1330static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1331
1332static void tpacpi_hotkey_send_key(unsigned int scancode)
1333{
1334 tpacpi_input_send_key(scancode);
1335 if (hotkey_report_mode < 2) {
1336 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1337 0x80, 0x1001 + scancode);
1338 }
1339}
1340
1341static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1342{
1343 u8 d;
1344
1345 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1346 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1347 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1348 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1349 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1350 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1351 }
1352 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1353 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1354 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1355 }
1356 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1357 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1358 n->displayexp_toggle =
1359 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1360 }
1361 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1362 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1363 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1364 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1365 n->brightness_toggle =
1366 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1367 }
1368 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1369 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1370 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1371 >> TP_NVRAM_POS_LEVEL_VOLUME;
1372 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1373 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1374 }
1375}
1376
1377#define TPACPI_COMPARE_KEY(__scancode, __member) \
35ff8b9f
HMH
1378 do { \
1379 if ((mask & (1 << __scancode)) && \
1380 oldn->__member != newn->__member) \
1381 tpacpi_hotkey_send_key(__scancode); \
1382 } while (0)
01e88f25
HMH
1383
1384#define TPACPI_MAY_SEND_KEY(__scancode) \
1385 do { if (mask & (1 << __scancode)) \
1386 tpacpi_hotkey_send_key(__scancode); } while (0)
1387
1388static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
35ff8b9f 1389 struct tp_nvram_state *newn,
01e88f25
HMH
1390 u32 mask)
1391{
1392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1394 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1396
1397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1398
1399 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1400
1401 /* handle volume */
1402 if (oldn->volume_toggle != newn->volume_toggle) {
1403 if (oldn->mute != newn->mute) {
1404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1405 }
1406 if (oldn->volume_level > newn->volume_level) {
1407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1408 } else if (oldn->volume_level < newn->volume_level) {
1409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1410 } else if (oldn->mute == newn->mute) {
1411 /* repeated key presses that didn't change state */
1412 if (newn->mute) {
1413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1414 } else if (newn->volume_level != 0) {
1415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1416 } else {
1417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1418 }
1419 }
1420 }
1421
1422 /* handle brightness */
1423 if (oldn->brightness_toggle != newn->brightness_toggle) {
1424 if (oldn->brightness_level < newn->brightness_level) {
1425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1426 } else if (oldn->brightness_level > newn->brightness_level) {
1427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1428 } else {
1429 /* repeated key presses that didn't change state */
1430 if (newn->brightness_level != 0) {
1431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1432 } else {
1433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1434 }
1435 }
1436 }
1437}
1438
1439#undef TPACPI_COMPARE_KEY
1440#undef TPACPI_MAY_SEND_KEY
1441
1442static int hotkey_kthread(void *data)
1443{
1444 struct tp_nvram_state s[2];
1445 u32 mask;
1446 unsigned int si, so;
1447 unsigned long t;
1448 unsigned int change_detector, must_reset;
1449
1450 mutex_lock(&hotkey_thread_mutex);
1451
1452 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1453 goto exit;
1454
1455 set_freezable();
1456
1457 so = 0;
1458 si = 1;
1459 t = 0;
1460
1461 /* Initial state for compares */
1462 mutex_lock(&hotkey_thread_data_mutex);
1463 change_detector = hotkey_config_change;
1464 mask = hotkey_source_mask & hotkey_mask;
1465 mutex_unlock(&hotkey_thread_data_mutex);
1466 hotkey_read_nvram(&s[so], mask);
1467
1468 while (!kthread_should_stop() && hotkey_poll_freq) {
1469 if (t == 0)
1470 t = 1000/hotkey_poll_freq;
1471 t = msleep_interruptible(t);
1472 if (unlikely(kthread_should_stop()))
1473 break;
1474 must_reset = try_to_freeze();
1475 if (t > 0 && !must_reset)
1476 continue;
1477
1478 mutex_lock(&hotkey_thread_data_mutex);
1479 if (must_reset || hotkey_config_change != change_detector) {
1480 /* forget old state on thaw or config change */
1481 si = so;
1482 t = 0;
1483 change_detector = hotkey_config_change;
1484 }
1485 mask = hotkey_source_mask & hotkey_mask;
1486 mutex_unlock(&hotkey_thread_data_mutex);
1487
1488 if (likely(mask)) {
1489 hotkey_read_nvram(&s[si], mask);
1490 if (likely(si != so)) {
1491 hotkey_compare_and_issue_event(&s[so], &s[si],
35ff8b9f 1492 mask);
01e88f25
HMH
1493 }
1494 }
1495
1496 so = si;
1497 si ^= 1;
1498 }
1499
1500exit:
1501 mutex_unlock(&hotkey_thread_mutex);
1502 return 0;
1503}
1504
1505static void hotkey_poll_stop_sync(void)
1506{
1507 if (tpacpi_hotkey_task) {
1508 if (frozen(tpacpi_hotkey_task) ||
1509 freezing(tpacpi_hotkey_task))
1510 thaw_process(tpacpi_hotkey_task);
1511
1512 kthread_stop(tpacpi_hotkey_task);
1513 tpacpi_hotkey_task = NULL;
1514 mutex_lock(&hotkey_thread_mutex);
1515 /* at this point, the thread did exit */
1516 mutex_unlock(&hotkey_thread_mutex);
1517 }
1518}
1519
1520/* call with hotkey_mutex held */
1521static void hotkey_poll_setup(int may_warn)
1522{
1523 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1524 hotkey_poll_freq > 0 &&
1525 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1526 if (!tpacpi_hotkey_task) {
1527 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
95e57ab2 1528 NULL, TPACPI_NVRAM_KTHREAD_NAME);
01e88f25
HMH
1529 if (IS_ERR(tpacpi_hotkey_task)) {
1530 tpacpi_hotkey_task = NULL;
35ff8b9f
HMH
1531 printk(TPACPI_ERR
1532 "could not create kernel thread "
01e88f25
HMH
1533 "for hotkey polling\n");
1534 }
1535 }
1536 } else {
1537 hotkey_poll_stop_sync();
1538 if (may_warn &&
1539 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
35ff8b9f
HMH
1540 printk(TPACPI_NOTICE
1541 "hot keys 0x%08x require polling, "
01e88f25
HMH
1542 "which is currently disabled\n",
1543 hotkey_source_mask);
1544 }
1545 }
1546}
1547
1548static void hotkey_poll_setup_safe(int may_warn)
1549{
1550 mutex_lock(&hotkey_mutex);
1551 hotkey_poll_setup(may_warn);
1552 mutex_unlock(&hotkey_mutex);
1553}
1554
1bc6b9cd
HMH
1555#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1556
1557static void hotkey_poll_setup_safe(int __unused)
1558{
1559}
1560
1561#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1562
01e88f25
HMH
1563static int hotkey_inputdev_open(struct input_dev *dev)
1564{
1565 switch (tpacpi_lifecycle) {
1566 case TPACPI_LIFE_INIT:
1567 /*
1568 * hotkey_init will call hotkey_poll_setup_safe
1569 * at the appropriate moment
1570 */
1571 return 0;
1572 case TPACPI_LIFE_EXITING:
1573 return -EBUSY;
1574 case TPACPI_LIFE_RUNNING:
1575 hotkey_poll_setup_safe(0);
1576 return 0;
1577 }
1578
1579 /* Should only happen if tpacpi_lifecycle is corrupt */
1580 BUG();
1581 return -EBUSY;
1582}
1583
1584static void hotkey_inputdev_close(struct input_dev *dev)
1585{
1586 /* disable hotkey polling when possible */
1587 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1588 hotkey_poll_setup_safe(0);
1589}
01e88f25 1590
a0416420
HMH
1591/* sysfs hotkey enable ------------------------------------------------- */
1592static ssize_t hotkey_enable_show(struct device *dev,
1593 struct device_attribute *attr,
1594 char *buf)
1595{
ae92bd17 1596 int res, status;
a0416420 1597
b2c985e7 1598 res = hotkey_status_get(&status);
a0416420
HMH
1599 if (res)
1600 return res;
1601
1602 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1603}
1604
1605static ssize_t hotkey_enable_store(struct device *dev,
1606 struct device_attribute *attr,
1607 const char *buf, size_t count)
1608{
1609 unsigned long t;
b2c985e7 1610 int res;
a0416420
HMH
1611
1612 if (parse_strtoul(buf, 1, &t))
1613 return -EINVAL;
1614
b2c985e7 1615 res = hotkey_status_set(t);
a0416420
HMH
1616
1617 return (res) ? res : count;
1618}
1619
1620static struct device_attribute dev_attr_hotkey_enable =
cc4c24e1 1621 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
a0416420
HMH
1622 hotkey_enable_show, hotkey_enable_store);
1623
1624/* sysfs hotkey mask --------------------------------------------------- */
1625static ssize_t hotkey_mask_show(struct device *dev,
1626 struct device_attribute *attr,
1627 char *buf)
1628{
b2c985e7 1629 int res;
a0416420 1630
b2c985e7
HMH
1631 if (mutex_lock_interruptible(&hotkey_mutex))
1632 return -ERESTARTSYS;
1633 res = hotkey_mask_get();
1634 mutex_unlock(&hotkey_mutex);
a0416420 1635
b2c985e7
HMH
1636 return (res)?
1637 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
a0416420
HMH
1638}
1639
1640static ssize_t hotkey_mask_store(struct device *dev,
1641 struct device_attribute *attr,
1642 const char *buf, size_t count)
1643{
1644 unsigned long t;
b2c985e7 1645 int res;
a0416420 1646
ae92bd17 1647 if (parse_strtoul(buf, 0xffffffffUL, &t))
a0416420
HMH
1648 return -EINVAL;
1649
b2c985e7
HMH
1650 if (mutex_lock_interruptible(&hotkey_mutex))
1651 return -ERESTARTSYS;
1652
1653 res = hotkey_mask_set(t);
01e88f25
HMH
1654
1655#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1656 hotkey_poll_setup(1);
1657#endif
1658
b2c985e7 1659 mutex_unlock(&hotkey_mutex);
a0416420
HMH
1660
1661 return (res) ? res : count;
1662}
1663
1664static struct device_attribute dev_attr_hotkey_mask =
cc4c24e1 1665 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
a0416420
HMH
1666 hotkey_mask_show, hotkey_mask_store);
1667
1668/* sysfs hotkey bios_enabled ------------------------------------------- */
1669static ssize_t hotkey_bios_enabled_show(struct device *dev,
1670 struct device_attribute *attr,
1671 char *buf)
1672{
1673 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1674}
1675
1676static struct device_attribute dev_attr_hotkey_bios_enabled =
cc4c24e1 1677 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
a0416420
HMH
1678
1679/* sysfs hotkey bios_mask ---------------------------------------------- */
1680static ssize_t hotkey_bios_mask_show(struct device *dev,
1681 struct device_attribute *attr,
1682 char *buf)
1683{
ae92bd17 1684 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
a0416420
HMH
1685}
1686
1687static struct device_attribute dev_attr_hotkey_bios_mask =
cc4c24e1 1688 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
a0416420 1689
9b010de5
HMH
1690/* sysfs hotkey all_mask ----------------------------------------------- */
1691static ssize_t hotkey_all_mask_show(struct device *dev,
1692 struct device_attribute *attr,
1693 char *buf)
1694{
01e88f25
HMH
1695 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1696 hotkey_all_mask | hotkey_source_mask);
9b010de5
HMH
1697}
1698
1699static struct device_attribute dev_attr_hotkey_all_mask =
1700 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1701
1702/* sysfs hotkey recommended_mask --------------------------------------- */
1703static ssize_t hotkey_recommended_mask_show(struct device *dev,
1704 struct device_attribute *attr,
1705 char *buf)
1706{
1707 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
01e88f25
HMH
1708 (hotkey_all_mask | hotkey_source_mask)
1709 & ~hotkey_reserved_mask);
9b010de5
HMH
1710}
1711
1712static struct device_attribute dev_attr_hotkey_recommended_mask =
1713 __ATTR(hotkey_recommended_mask, S_IRUGO,
1714 hotkey_recommended_mask_show, NULL);
1715
01e88f25
HMH
1716#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1717
1718/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1719static ssize_t hotkey_source_mask_show(struct device *dev,
1720 struct device_attribute *attr,
1721 char *buf)
1722{
1723 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1724}
1725
1726static ssize_t hotkey_source_mask_store(struct device *dev,
1727 struct device_attribute *attr,
1728 const char *buf, size_t count)
1729{
1730 unsigned long t;
1731
1732 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1733 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1734 return -EINVAL;
1735
1736 if (mutex_lock_interruptible(&hotkey_mutex))
1737 return -ERESTARTSYS;
1738
1739 HOTKEY_CONFIG_CRITICAL_START
1740 hotkey_source_mask = t;
1741 HOTKEY_CONFIG_CRITICAL_END
1742
1743 hotkey_poll_setup(1);
1744
1745 mutex_unlock(&hotkey_mutex);
1746
1747 return count;
1748}
1749
1750static struct device_attribute dev_attr_hotkey_source_mask =
1751 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1752 hotkey_source_mask_show, hotkey_source_mask_store);
1753
1754/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1755static ssize_t hotkey_poll_freq_show(struct device *dev,
1756 struct device_attribute *attr,
1757 char *buf)
1758{
1759 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1760}
1761
1762static ssize_t hotkey_poll_freq_store(struct device *dev,
1763 struct device_attribute *attr,
1764 const char *buf, size_t count)
1765{
1766 unsigned long t;
1767
1768 if (parse_strtoul(buf, 25, &t))
1769 return -EINVAL;
1770
1771 if (mutex_lock_interruptible(&hotkey_mutex))
1772 return -ERESTARTSYS;
1773
1774 hotkey_poll_freq = t;
1775
1776 hotkey_poll_setup(1);
1777 mutex_unlock(&hotkey_mutex);
1778
1779 return count;
1780}
1781
1782static struct device_attribute dev_attr_hotkey_poll_freq =
1783 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1784 hotkey_poll_freq_show, hotkey_poll_freq_store);
1785
1786#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1787
50ebec09 1788/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
74941a69
HMH
1789static ssize_t hotkey_radio_sw_show(struct device *dev,
1790 struct device_attribute *attr,
1791 char *buf)
1792{
1793 int res, s;
1794 res = hotkey_get_wlsw(&s);
1795 if (res < 0)
1796 return res;
1797
1798 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1799}
1800
1801static struct device_attribute dev_attr_hotkey_radio_sw =
1802 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1803
50ebec09
HMH
1804static void hotkey_radio_sw_notify_change(void)
1805{
1806 if (tp_features.hotkey_wlsw)
1807 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1808 "hotkey_radio_sw");
1809}
1810
6c231bd5
HMH
1811/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1812static ssize_t hotkey_tablet_mode_show(struct device *dev,
1813 struct device_attribute *attr,
1814 char *buf)
1815{
1816 int res, s;
1817 res = hotkey_get_tablet_mode(&s);
1818 if (res < 0)
1819 return res;
1820
1821 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1822}
1823
1824static struct device_attribute dev_attr_hotkey_tablet_mode =
1825 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1826
1827static void hotkey_tablet_mode_notify_change(void)
1828{
1829 if (tp_features.hotkey_tablet)
1830 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1831 "hotkey_tablet_mode");
1832}
1833
ff80f137
HMH
1834/* sysfs hotkey report_mode -------------------------------------------- */
1835static ssize_t hotkey_report_mode_show(struct device *dev,
1836 struct device_attribute *attr,
1837 char *buf)
1838{
1839 return snprintf(buf, PAGE_SIZE, "%d\n",
1840 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1841}
1842
1843static struct device_attribute dev_attr_hotkey_report_mode =
1844 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1845
50ebec09 1846/* sysfs wakeup reason (pollable) -------------------------------------- */
a713b4d7
HMH
1847static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1848 struct device_attribute *attr,
1849 char *buf)
1850{
1851 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1852}
1853
1854static struct device_attribute dev_attr_hotkey_wakeup_reason =
1855 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1856
1d5a2b54 1857static void hotkey_wakeup_reason_notify_change(void)
50ebec09
HMH
1858{
1859 if (tp_features.hotkey_mask)
1860 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1861 "wakeup_reason");
1862}
1863
1864/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
a713b4d7
HMH
1865static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1866 struct device_attribute *attr,
1867 char *buf)
1868{
1869 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1870}
1871
1872static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1873 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1874 hotkey_wakeup_hotunplug_complete_show, NULL);
1875
1d5a2b54 1876static void hotkey_wakeup_hotunplug_complete_notify_change(void)
50ebec09
HMH
1877{
1878 if (tp_features.hotkey_mask)
1879 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1880 "wakeup_hotunplug_complete");
1881}
1882
a0416420
HMH
1883/* --------------------------------------------------------------------- */
1884
ff80f137
HMH
1885static struct attribute *hotkey_attributes[] __initdata = {
1886 &dev_attr_hotkey_enable.attr,
01e88f25 1887 &dev_attr_hotkey_bios_enabled.attr,
ff80f137 1888 &dev_attr_hotkey_report_mode.attr,
01e88f25
HMH
1889#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1890 &dev_attr_hotkey_mask.attr,
1891 &dev_attr_hotkey_all_mask.attr,
1892 &dev_attr_hotkey_recommended_mask.attr,
1893 &dev_attr_hotkey_source_mask.attr,
1894 &dev_attr_hotkey_poll_freq.attr,
1895#endif
ff80f137
HMH
1896};
1897
1898static struct attribute *hotkey_mask_attributes[] __initdata = {
a0416420 1899 &dev_attr_hotkey_bios_mask.attr,
01e88f25
HMH
1900#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1901 &dev_attr_hotkey_mask.attr,
9b010de5
HMH
1902 &dev_attr_hotkey_all_mask.attr,
1903 &dev_attr_hotkey_recommended_mask.attr,
01e88f25 1904#endif
a713b4d7
HMH
1905 &dev_attr_hotkey_wakeup_reason.attr,
1906 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
a0416420
HMH
1907};
1908
733e27c1
HMH
1909static void tpacpi_send_radiosw_update(void)
1910{
1911 int wlsw;
1912
1913 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1914 mutex_lock(&tpacpi_inputdev_send_mutex);
1915
1916 input_report_switch(tpacpi_inputdev,
1917 SW_RFKILL_ALL, !!wlsw);
1918 input_sync(tpacpi_inputdev);
1919
1920 mutex_unlock(&tpacpi_inputdev_send_mutex);
1921 }
1922 hotkey_radio_sw_notify_change();
1923}
1924
9c0a76e1
HMH
1925static void hotkey_exit(void)
1926{
1927#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1928 hotkey_poll_stop_sync();
1929#endif
1930
1931 if (hotkey_dev_attributes)
1932 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1933
1934 kfree(hotkey_keycode_map);
1935
1936 if (tp_features.hotkey) {
1937 dbg_printk(TPACPI_DBG_EXIT,
1938 "restoring original hot key mask\n");
1939 /* no short-circuit boolean operator below! */
1940 if ((hotkey_mask_set(hotkey_orig_mask) |
1941 hotkey_status_set(hotkey_orig_status)) != 0)
1942 printk(TPACPI_ERR
1943 "failed to restore hot key mask "
1944 "to BIOS defaults\n");
1945 }
1946}
1947
a5763f22 1948static int __init hotkey_init(struct ibm_init_struct *iibm)
56b6aeb0 1949{
0f089147
HMH
1950 /* Requirements for changing the default keymaps:
1951 *
1952 * 1. Many of the keys are mapped to KEY_RESERVED for very
1953 * good reasons. Do not change them unless you have deep
1954 * knowledge on the IBM and Lenovo ThinkPad firmware for
1955 * the various ThinkPad models. The driver behaves
1956 * differently for KEY_RESERVED: such keys have their
1957 * hot key mask *unset* in mask_recommended, and also
1958 * in the initial hot key mask programmed into the
1959 * firmware at driver load time, which means the firm-
1960 * ware may react very differently if you change them to
1961 * something else;
1962 *
1963 * 2. You must be subscribed to the linux-thinkpad and
1964 * ibm-acpi-devel mailing lists, and you should read the
1965 * list archives since 2007 if you want to change the
1966 * keymaps. This requirement exists so that you will
1967 * know the past history of problems with the thinkpad-
1968 * acpi driver keymaps, and also that you will be
1969 * listening to any bug reports;
1970 *
1971 * 3. Do not send thinkpad-acpi specific patches directly to
1972 * for merging, *ever*. Send them to the linux-acpi
1973 * mailinglist for comments. Merging is to be done only
1974 * through acpi-test and the ACPI maintainer.
1975 *
1976 * If the above is too much to ask, don't change the keymap.
1977 * Ask the thinkpad-acpi maintainer to do it, instead.
1978 */
edf0e0e5
HMH
1979 static u16 ibm_keycode_map[] __initdata = {
1980 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1981 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1982 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1983 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
0f089147
HMH
1984
1985 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
edf0e0e5
HMH
1986 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1987 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1988 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
0f089147
HMH
1989
1990 /* brightness: firmware always reacts to them, unless
1991 * X.org did some tricks in the radeon BIOS scratch
1992 * registers of *some* models */
e927c08d 1993 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
e927c08d 1994 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
0f089147
HMH
1995
1996 /* Thinklight: firmware always react to it */
edf0e0e5 1997 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
0f089147 1998
edf0e0e5
HMH
1999 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2000 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
0f089147
HMH
2001
2002 /* Volume: firmware always react to it and reprograms
2003 * the built-in *extra* mixer. Never map it to control
2004 * another mixer by default. */
e927c08d
HMH
2005 KEY_RESERVED, /* 0x14: VOLUME UP */
2006 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2007 KEY_RESERVED, /* 0x16: MUTE */
0f089147 2008
edf0e0e5 2009 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
0f089147 2010
edf0e0e5
HMH
2011 /* (assignments unknown, please report if found) */
2012 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2013 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2014 };
2015 static u16 lenovo_keycode_map[] __initdata = {
2016 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2017 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2018 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2019 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
0f089147
HMH
2020
2021 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
edf0e0e5
HMH
2022 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2023 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2024 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
0f089147 2025
b5972796
HMH
2026 /* These either have to go through ACPI video, or
2027 * act like in the IBM ThinkPads, so don't ever
2028 * enable them by default */
56a185b4 2029 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
56a185b4 2030 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
0f089147 2031
edf0e0e5 2032 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
0f089147 2033
edf0e0e5
HMH
2034 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2035 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
0f089147
HMH
2036
2037 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2038 * react to it and reprograms the built-in *extra* mixer.
2039 * Never map it to control another mixer by default.
2040 *
2041 * T60?, T61, R60?, R61: firmware and EC tries to send
2042 * these over the regular keyboard, so these are no-ops,
2043 * but there are still weird bugs re. MUTE, so do not
2044 * change unless you get test reports from all Lenovo
2045 * models. May cause the BIOS to interfere with the
2046 * HDA mixer.
2047 */
e927c08d
HMH
2048 KEY_RESERVED, /* 0x14: VOLUME UP */
2049 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2050 KEY_RESERVED, /* 0x16: MUTE */
0f089147 2051
edf0e0e5 2052 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
0f089147 2053
edf0e0e5
HMH
2054 /* (assignments unknown, please report if found) */
2055 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2056 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2057 };
2058
2059#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2060#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2061#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2062
6a38abbf 2063 int res, i;
74941a69 2064 int status;
1b6521dc 2065 int hkeyv;
b86c4722 2066
fe08bc4b
HMH
2067 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2068
6a38abbf 2069 BUG_ON(!tpacpi_inputdev);
01e88f25
HMH
2070 BUG_ON(tpacpi_inputdev->open != NULL ||
2071 tpacpi_inputdev->close != NULL);
6a38abbf 2072
e0c7dfe7 2073 TPACPI_ACPIHANDLE_INIT(hkey);
40ca9fdf 2074 mutex_init(&hotkey_mutex);
5fba344c 2075
01e88f25
HMH
2076#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2077 mutex_init(&hotkey_thread_mutex);
2078 mutex_init(&hotkey_thread_data_mutex);
2079#endif
2080
56b6aeb0 2081 /* hotkey not supported on 570 */
d8fd94d9 2082 tp_features.hotkey = hkey_handle != NULL;
56b6aeb0 2083
fe08bc4b 2084 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
d8fd94d9 2085 str_supported(tp_features.hotkey));
fe08bc4b 2086
9c0a76e1
HMH
2087 if (!tp_features.hotkey)
2088 return 1;
a0416420 2089
9c0a76e1
HMH
2090 hotkey_dev_attributes = create_attr_set(13, NULL);
2091 if (!hotkey_dev_attributes)
2092 return -ENOMEM;
2093 res = add_many_to_attr_set(hotkey_dev_attributes,
2094 hotkey_attributes,
2095 ARRAY_SIZE(hotkey_attributes));
2096 if (res)
2097 goto err_exit;
2098
2099 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2100 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2101 for HKEY interface version 0x100 */
2102 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2103 if ((hkeyv >> 8) != 1) {
2104 printk(TPACPI_ERR "unknown version of the "
2105 "HKEY interface: 0x%x\n", hkeyv);
2106 printk(TPACPI_ERR "please report this to %s\n",
2107 TPACPI_MAIL);
2108 } else {
2109 /*
2110 * MHKV 0x100 in A31, R40, R40e,
2111 * T4x, X31, and later
2112 */
2113 tp_features.hotkey_mask = 1;
1b6521dc 2114 }
9c0a76e1 2115 }
56b6aeb0 2116
9c0a76e1
HMH
2117 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2118 str_supported(tp_features.hotkey_mask));
fe08bc4b 2119
9c0a76e1
HMH
2120 if (tp_features.hotkey_mask) {
2121 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2122 "MHKA", "qd")) {
2123 printk(TPACPI_ERR
2124 "missing MHKA handler, "
2125 "please report this to %s\n",
2126 TPACPI_MAIL);
2127 /* FN+F12, FN+F4, FN+F3 */
2128 hotkey_all_mask = 0x080cU;
9b010de5 2129 }
9c0a76e1 2130 }
9b010de5 2131
9c0a76e1
HMH
2132 /* hotkey_source_mask *must* be zero for
2133 * the first hotkey_mask_get */
2134 res = hotkey_status_get(&hotkey_orig_status);
2135 if (res)
2136 goto err_exit;
2137
2138 if (tp_features.hotkey_mask) {
2139 res = hotkey_mask_get();
2140 if (res)
2141 goto err_exit;
2142
2143 hotkey_orig_mask = hotkey_mask;
2144 res = add_many_to_attr_set(
2145 hotkey_dev_attributes,
2146 hotkey_mask_attributes,
2147 ARRAY_SIZE(hotkey_mask_attributes));
2148 if (res)
2149 goto err_exit;
2150 }
74941a69 2151
01e88f25 2152#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
9c0a76e1
HMH
2153 if (tp_features.hotkey_mask) {
2154 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2155 & ~hotkey_all_mask;
2156 } else {
2157 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2158 }
01e88f25 2159
9c0a76e1
HMH
2160 vdbg_printk(TPACPI_DBG_INIT,
2161 "hotkey source mask 0x%08x, polling freq %d\n",
2162 hotkey_source_mask, hotkey_poll_freq);
01e88f25
HMH
2163#endif
2164
9c0a76e1
HMH
2165 /* Not all thinkpads have a hardware radio switch */
2166 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2167 tp_features.hotkey_wlsw = 1;
2168 printk(TPACPI_INFO
2169 "radio switch found; radios are %s\n",
2170 enabled(status, 0));
3a872080
HMH
2171 }
2172 if (tp_features.hotkey_wlsw)
9c0a76e1
HMH
2173 res = add_to_attr_set(hotkey_dev_attributes,
2174 &dev_attr_hotkey_radio_sw.attr);
74941a69 2175
9c0a76e1
HMH
2176 /* For X41t, X60t, X61t Tablets... */
2177 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2178 tp_features.hotkey_tablet = 1;
2179 printk(TPACPI_INFO
2180 "possible tablet mode switch found; "
2181 "ThinkPad in %s mode\n",
2182 (status & TP_HOTKEY_TABLET_MASK)?
2183 "tablet" : "laptop");
2184 res = add_to_attr_set(hotkey_dev_attributes,
2185 &dev_attr_hotkey_tablet_mode.attr);
2186 }
6c231bd5 2187
9c0a76e1
HMH
2188 if (!res)
2189 res = register_attr_set_with_sysfs(
2190 hotkey_dev_attributes,
2191 &tpacpi_pdev->dev.kobj);
2192 if (res)
2193 goto err_exit;
6a38abbf 2194
9c0a76e1 2195 /* Set up key map */
edf0e0e5 2196
9c0a76e1
HMH
2197 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2198 GFP_KERNEL);
2199 if (!hotkey_keycode_map) {
2200 printk(TPACPI_ERR
2201 "failed to allocate memory for key map\n");
2202 res = -ENOMEM;
2203 goto err_exit;
2204 }
edf0e0e5 2205
9c0a76e1
HMH
2206 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2207 dbg_printk(TPACPI_DBG_INIT,
2208 "using Lenovo default hot key map\n");
2209 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2210 TPACPI_HOTKEY_MAP_SIZE);
2211 } else {
2212 dbg_printk(TPACPI_DBG_INIT,
2213 "using IBM default hot key map\n");
2214 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2215 TPACPI_HOTKEY_MAP_SIZE);
2216 }
edf0e0e5 2217
9c0a76e1
HMH
2218 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2219 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2220 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2221 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2222 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2223 tpacpi_inputdev->keycode = hotkey_keycode_map;
2224 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2225 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2226 set_bit(hotkey_keycode_map[i],
2227 tpacpi_inputdev->keybit);
2228 } else {
2229 if (i < sizeof(hotkey_reserved_mask)*8)
2230 hotkey_reserved_mask |= 1 << i;
6a38abbf 2231 }
9c0a76e1 2232 }
6a38abbf 2233
9c0a76e1
HMH
2234 if (tp_features.hotkey_wlsw) {
2235 set_bit(EV_SW, tpacpi_inputdev->evbit);
2236 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2237 }
2238 if (tp_features.hotkey_tablet) {
2239 set_bit(EV_SW, tpacpi_inputdev->evbit);
2240 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2241 }
5c29d58f 2242
9c0a76e1
HMH
2243 /* Do not issue duplicate brightness change events to
2244 * userspace */
2245 if (!tp_features.bright_acpimode)
2246 /* update bright_acpimode... */
2247 tpacpi_check_std_acpi_brightness_support();
b5972796 2248
9c0a76e1
HMH
2249 if (tp_features.bright_acpimode) {
2250 printk(TPACPI_INFO
2251 "This ThinkPad has standard ACPI backlight "
2252 "brightness control, supported by the ACPI "
2253 "video driver\n");
2254 printk(TPACPI_NOTICE
2255 "Disabling thinkpad-acpi brightness events "
2256 "by default...\n");
2257
2258 /* The hotkey_reserved_mask change below is not
2259 * necessary while the keys are at KEY_RESERVED in the
2260 * default map, but better safe than sorry, leave it
2261 * here as a marker of what we have to do, especially
2262 * when we finally become able to set this at runtime
2263 * on response to X.org requests */
2264 hotkey_reserved_mask |=
2265 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2266 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2267 }
ff80f137 2268
9c0a76e1
HMH
2269 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2270 res = hotkey_status_set(1);
2271 if (res) {
2272 hotkey_exit();
2273 return res;
2274 }
2275 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2276 & ~hotkey_reserved_mask)
2277 | hotkey_orig_mask);
2278 if (res < 0 && res != -ENXIO) {
2279 hotkey_exit();
2280 return res;
2281 }
01e88f25 2282
9c0a76e1
HMH
2283 dbg_printk(TPACPI_DBG_INIT,
2284 "legacy hot key reporting over procfs %s\n",
2285 (hotkey_report_mode < 2) ?
2286 "enabled" : "disabled");
01e88f25 2287
9c0a76e1
HMH
2288 tpacpi_inputdev->open = &hotkey_inputdev_open;
2289 tpacpi_inputdev->close = &hotkey_inputdev_close;
56b6aeb0 2290
9c0a76e1 2291 hotkey_poll_setup_safe(1);
733e27c1 2292 tpacpi_send_radiosw_update();
9c0a76e1 2293 tpacpi_input_send_tabletsw();
56b6aeb0 2294
9c0a76e1 2295 return 0;
01e88f25 2296
9c0a76e1
HMH
2297err_exit:
2298 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2299 hotkey_dev_attributes = NULL;
a0416420 2300
9c0a76e1 2301 return (res < 0)? res : 1;
56b6aeb0
HMH
2302}
2303
2304static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2305{
6a38abbf 2306 u32 hkey;
b7c8c200 2307 unsigned int scancode;
3eea123d 2308 int send_acpi_ev;
3e5ce914 2309 int ignore_acpi_ev;
3b64b51d 2310 int unk_ev;
3eea123d
HMH
2311
2312 if (event != 0x80) {
35ff8b9f
HMH
2313 printk(TPACPI_ERR
2314 "unknown HKEY notification event %d\n", event);
3eea123d 2315 /* forward it to userspace, maybe it knows how to handle it */
35ff8b9f
HMH
2316 acpi_bus_generate_netlink_event(
2317 ibm->acpi->device->pnp.device_class,
2318 ibm->acpi->device->dev.bus_id,
2319 event, 0);
3eea123d
HMH
2320 return;
2321 }
2322
2323 while (1) {
2324 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
e0c7dfe7 2325 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3eea123d
HMH
2326 return;
2327 }
2328
2329 if (hkey == 0) {
2330 /* queue empty */
2331 return;
2332 }
2333
3b64b51d 2334 send_acpi_ev = 1;
3e5ce914 2335 ignore_acpi_ev = 0;
3b64b51d 2336 unk_ev = 0;
56b6aeb0 2337
ff80f137
HMH
2338 switch (hkey >> 12) {
2339 case 1:
2340 /* 0x1000-0x1FFF: key presses */
2341 scancode = hkey & 0xfff;
2342 if (scancode > 0 && scancode < 0x21) {
2343 scancode--;
01e88f25
HMH
2344 if (!(hotkey_source_mask & (1 << scancode))) {
2345 tpacpi_input_send_key(scancode);
3b64b51d 2346 send_acpi_ev = 0;
01e88f25
HMH
2347 } else {
2348 ignore_acpi_ev = 1;
2349 }
ff80f137 2350 } else {
3b64b51d 2351 unk_ev = 1;
ff80f137
HMH
2352 }
2353 break;
a713b4d7
HMH
2354 case 2:
2355 /* Wakeup reason */
2356 switch (hkey) {
2357 case 0x2304: /* suspend, undock */
2358 case 0x2404: /* hibernation, undock */
2359 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2360 ignore_acpi_ev = 1;
2361 break;
2362 case 0x2305: /* suspend, bay eject */
2363 case 0x2405: /* hibernation, bay eject */
2364 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2365 ignore_acpi_ev = 1;
2366 break;
2367 default:
2368 unk_ev = 1;
2369 }
2370 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2371 printk(TPACPI_INFO
2372 "woke up due to a hot-unplug "
2373 "request...\n");
50ebec09 2374 hotkey_wakeup_reason_notify_change();
a713b4d7
HMH
2375 }
2376 break;
2377 case 3:
2378 /* bay-related wakeups */
2379 if (hkey == 0x3003) {
2380 hotkey_autosleep_ack = 1;
2381 printk(TPACPI_INFO
2382 "bay ejected\n");
50ebec09 2383 hotkey_wakeup_hotunplug_complete_notify_change();
a713b4d7
HMH
2384 } else {
2385 unk_ev = 1;
2386 }
2387 break;
2388 case 4:
2389 /* dock-related wakeups */
2390 if (hkey == 0x4003) {
2391 hotkey_autosleep_ack = 1;
2392 printk(TPACPI_INFO
2393 "undocked\n");
50ebec09 2394 hotkey_wakeup_hotunplug_complete_notify_change();
a713b4d7
HMH
2395 } else {
2396 unk_ev = 1;
2397 }
2398 break;
ff80f137 2399 case 5:
d1edb2b5 2400 /* 0x5000-0x5FFF: human interface helpers */
3b64b51d 2401 switch (hkey) {
d1edb2b5 2402 case 0x5010: /* Lenovo new BIOS: brightness changed */
d1edb2b5
HMH
2403 case 0x500b: /* X61t: tablet pen inserted into bay */
2404 case 0x500c: /* X61t: tablet pen removed from bay */
3b64b51d 2405 break;
6c231bd5
HMH
2406 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2407 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2408 tpacpi_input_send_tabletsw();
2409 hotkey_tablet_mode_notify_change();
b3ec6f91
HMH
2410 send_acpi_ev = 0;
2411 break;
3b64b51d
HMH
2412 case 0x5001:
2413 case 0x5002:
2414 /* LID switch events. Do not propagate */
3e5ce914 2415 ignore_acpi_ev = 1;
3b64b51d
HMH
2416 break;
2417 default:
2418 unk_ev = 1;
ff80f137
HMH
2419 }
2420 break;
2421 case 7:
2422 /* 0x7000-0x7FFF: misc */
2423 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
733e27c1 2424 tpacpi_send_radiosw_update();
3b64b51d 2425 send_acpi_ev = 0;
6a38abbf 2426 break;
6a38abbf 2427 }
ff80f137
HMH
2428 /* fallthrough to default */
2429 default:
3b64b51d
HMH
2430 unk_ev = 1;
2431 }
2432 if (unk_ev) {
35ff8b9f
HMH
2433 printk(TPACPI_NOTICE
2434 "unhandled HKEY event 0x%04x\n", hkey);
6a38abbf 2435 }
ff80f137 2436
3eea123d 2437 /* Legacy events */
35ff8b9f
HMH
2438 if (!ignore_acpi_ev &&
2439 (send_acpi_ev || hotkey_report_mode < 2)) {
2440 acpi_bus_generate_proc_event(ibm->acpi->device,
2441 event, hkey);
3e5ce914 2442 }
ff80f137 2443
3eea123d 2444 /* netlink events */
3e5ce914 2445 if (!ignore_acpi_ev && send_acpi_ev) {
35ff8b9f
HMH
2446 acpi_bus_generate_netlink_event(
2447 ibm->acpi->device->pnp.device_class,
2448 ibm->acpi->device->dev.bus_id,
2449 event, hkey);
3eea123d 2450 }
56b6aeb0
HMH
2451 }
2452}
2453
a713b4d7
HMH
2454static void hotkey_suspend(pm_message_t state)
2455{
2456 /* Do these on suspend, we get the events on early resume! */
2457 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2458 hotkey_autosleep_ack = 0;
2459}
2460
5c29d58f
HMH
2461static void hotkey_resume(void)
2462{
b2c985e7 2463 if (hotkey_mask_get())
35ff8b9f
HMH
2464 printk(TPACPI_ERR
2465 "error while trying to read hot key mask "
2466 "from firmware\n");
733e27c1 2467 tpacpi_send_radiosw_update();
6c231bd5 2468 hotkey_tablet_mode_notify_change();
50ebec09
HMH
2469 hotkey_wakeup_reason_notify_change();
2470 hotkey_wakeup_hotunplug_complete_notify_change();
01e88f25 2471 hotkey_poll_setup_safe(0);
5c29d58f
HMH
2472}
2473
a0416420 2474/* procfs -------------------------------------------------------------- */
78f81cc4 2475static int hotkey_read(char *p)
1da177e4 2476{
ae92bd17 2477 int res, status;
1da177e4
LT
2478 int len = 0;
2479
d8fd94d9 2480 if (!tp_features.hotkey) {
78f81cc4
BD
2481 len += sprintf(p + len, "status:\t\tnot supported\n");
2482 return len;
2483 }
2484
fc589a3c
HMH
2485 if (mutex_lock_interruptible(&hotkey_mutex))
2486 return -ERESTARTSYS;
b2c985e7
HMH
2487 res = hotkey_status_get(&status);
2488 if (!res)
2489 res = hotkey_mask_get();
40ca9fdf 2490 mutex_unlock(&hotkey_mutex);
b86c4722
HMH
2491 if (res)
2492 return res;
1da177e4
LT
2493
2494 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
d8fd94d9 2495 if (tp_features.hotkey_mask) {
b2c985e7 2496 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
1da177e4
LT
2497 len += sprintf(p + len,
2498 "commands:\tenable, disable, reset, <mask>\n");
2499 } else {
2500 len += sprintf(p + len, "mask:\t\tnot supported\n");
2501 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2502 }
2503
2504 return len;
2505}
2506
78f81cc4 2507static int hotkey_write(char *buf)
1da177e4 2508{
ae92bd17
HMH
2509 int res, status;
2510 u32 mask;
1da177e4 2511 char *cmd;
1da177e4 2512
d8fd94d9 2513 if (!tp_features.hotkey)
1da177e4
LT
2514 return -ENODEV;
2515
fc589a3c
HMH
2516 if (mutex_lock_interruptible(&hotkey_mutex))
2517 return -ERESTARTSYS;
40ca9fdf 2518
b2c985e7
HMH
2519 status = -1;
2520 mask = hotkey_mask;
78f81cc4 2521
40ca9fdf 2522 res = 0;
1da177e4
LT
2523 while ((cmd = next_cmd(&buf))) {
2524 if (strlencmp(cmd, "enable") == 0) {
2525 status = 1;
2526 } else if (strlencmp(cmd, "disable") == 0) {
2527 status = 0;
2528 } else if (strlencmp(cmd, "reset") == 0) {
78f81cc4
BD
2529 status = hotkey_orig_status;
2530 mask = hotkey_orig_mask;
1da177e4
LT
2531 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2532 /* mask set */
2533 } else if (sscanf(cmd, "%x", &mask) == 1) {
2534 /* mask set */
40ca9fdf
HMH
2535 } else {
2536 res = -EINVAL;
2537 goto errexit;
2538 }
1da177e4 2539 }
b2c985e7
HMH
2540 if (status != -1)
2541 res = hotkey_status_set(status);
1da177e4 2542
b2c985e7
HMH
2543 if (!res && mask != hotkey_mask)
2544 res = hotkey_mask_set(mask);
1da177e4 2545
40ca9fdf
HMH
2546errexit:
2547 mutex_unlock(&hotkey_mutex);
2548 return res;
78f81cc4 2549}
1da177e4 2550
1ba90e3a 2551static const struct acpi_device_id ibm_htk_device_ids[] = {
e0c7dfe7 2552 {TPACPI_ACPI_HKEY_HID, 0},
1ba90e3a
TR
2553 {"", 0},
2554};
2555
8d376cd6 2556static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1ba90e3a 2557 .hid = ibm_htk_device_ids,
8d376cd6
HMH
2558 .notify = hotkey_notify,
2559 .handle = &hkey_handle,
2560 .type = ACPI_DEVICE_NOTIFY,
2561};
2562
a5763f22
HMH
2563static struct ibm_struct hotkey_driver_data = {
2564 .name = "hotkey",
a5763f22
HMH
2565 .read = hotkey_read,
2566 .write = hotkey_write,
2567 .exit = hotkey_exit,
5c29d58f 2568 .resume = hotkey_resume,
a713b4d7 2569 .suspend = hotkey_suspend,
8d376cd6 2570 .acpi = &ibm_hotkey_acpidriver,
a5763f22
HMH
2571};
2572
56b6aeb0
HMH
2573/*************************************************************************
2574 * Bluetooth subdriver
2575 */
1da177e4 2576
f74a27d4
HMH
2577enum {
2578 /* ACPI GBDC/SBDC bits */
2579 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2580 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2581 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2582};
2583
07431ec8
HMH
2584static int bluetooth_get_radiosw(void)
2585{
2586 int status;
2587
2588 if (!tp_features.bluetooth)
2589 return -ENODEV;
2590
2591 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2592 return -EIO;
2593
2594 return (status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0;
2595}
2596
2597static int bluetooth_set_radiosw(int radio_on)
2598{
2599 int status;
2600
2601 if (!tp_features.bluetooth)
2602 return -ENODEV;
2603
2604 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2605 return -EIO;
2606 if (radio_on)
2607 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2608 else
2609 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2610 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2611 return -EIO;
2612
2613 return 0;
2614}
f74a27d4 2615
d3a6ade4
HMH
2616/* sysfs bluetooth enable ---------------------------------------------- */
2617static ssize_t bluetooth_enable_show(struct device *dev,
2618 struct device_attribute *attr,
2619 char *buf)
2620{
2621 int status;
2622
2623 status = bluetooth_get_radiosw();
2624 if (status < 0)
2625 return status;
2626
2627 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2628}
2629
2630static ssize_t bluetooth_enable_store(struct device *dev,
2631 struct device_attribute *attr,
2632 const char *buf, size_t count)
2633{
2634 unsigned long t;
2635 int res;
2636
2637 if (parse_strtoul(buf, 1, &t))
2638 return -EINVAL;
2639
2640 res = bluetooth_set_radiosw(t);
2641
2642 return (res) ? res : count;
2643}
2644
2645static struct device_attribute dev_attr_bluetooth_enable =
cc4c24e1 2646 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
d3a6ade4
HMH
2647 bluetooth_enable_show, bluetooth_enable_store);
2648
2649/* --------------------------------------------------------------------- */
2650
2651static struct attribute *bluetooth_attributes[] = {
2652 &dev_attr_bluetooth_enable.attr,
2653 NULL
2654};
2655
2656static const struct attribute_group bluetooth_attr_group = {
d3a6ade4
HMH
2657 .attrs = bluetooth_attributes,
2658};
2659
07431ec8
HMH
2660static void bluetooth_exit(void)
2661{
2662 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2663 &bluetooth_attr_group);
2664}
2665
a5763f22 2666static int __init bluetooth_init(struct ibm_init_struct *iibm)
1da177e4 2667{
d3a6ade4 2668 int res;
d6fdd1e9
HMH
2669 int status = 0;
2670
fe08bc4b
HMH
2671 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2672
e0c7dfe7 2673 TPACPI_ACPIHANDLE_INIT(hkey);
5fba344c 2674
78f81cc4
BD
2675 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2676 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
d8fd94d9 2677 tp_features.bluetooth = hkey_handle &&
d6fdd1e9
HMH
2678 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2679
2680 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2681 str_supported(tp_features.bluetooth),
2682 status);
2683
3a872080
HMH
2684 if (tp_features.bluetooth &&
2685 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2686 /* no bluetooth hardware present in system */
2687 tp_features.bluetooth = 0;
2688 dbg_printk(TPACPI_DBG_INIT,
2689 "bluetooth hardware not installed\n");
2690 }
2691
d3a6ade4 2692 if (tp_features.bluetooth) {
3a872080
HMH
2693 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2694 &bluetooth_attr_group);
2695 if (res)
2696 return res;
d6fdd1e9 2697 }
fe08bc4b 2698
d8fd94d9 2699 return (tp_features.bluetooth)? 0 : 1;
1da177e4
LT
2700}
2701
d3a6ade4 2702/* procfs -------------------------------------------------------------- */
78f81cc4 2703static int bluetooth_read(char *p)
1da177e4
LT
2704{
2705 int len = 0;
d6fdd1e9 2706 int status = bluetooth_get_radiosw();
1da177e4 2707
d8fd94d9 2708 if (!tp_features.bluetooth)
1da177e4 2709 len += sprintf(p + len, "status:\t\tnot supported\n");
1da177e4 2710 else {
d6fdd1e9
HMH
2711 len += sprintf(p + len, "status:\t\t%s\n",
2712 (status)? "enabled" : "disabled");
1da177e4
LT
2713 len += sprintf(p + len, "commands:\tenable, disable\n");
2714 }
2715
2716 return len;
2717}
2718
78f81cc4 2719static int bluetooth_write(char *buf)
1da177e4 2720{
1da177e4 2721 char *cmd;
1da177e4 2722
d8fd94d9 2723 if (!tp_features.bluetooth)
78f81cc4 2724 return -ENODEV;
1da177e4
LT
2725
2726 while ((cmd = next_cmd(&buf))) {
2727 if (strlencmp(cmd, "enable") == 0) {
d6fdd1e9 2728 bluetooth_set_radiosw(1);
1da177e4 2729 } else if (strlencmp(cmd, "disable") == 0) {
d6fdd1e9 2730 bluetooth_set_radiosw(0);
1da177e4
LT
2731 } else
2732 return -EINVAL;
1da177e4
LT
2733 }
2734
1da177e4
LT
2735 return 0;
2736}
2737
a5763f22
HMH
2738static struct ibm_struct bluetooth_driver_data = {
2739 .name = "bluetooth",
2740 .read = bluetooth_read,
2741 .write = bluetooth_write,
d3a6ade4 2742 .exit = bluetooth_exit,
a5763f22
HMH
2743};
2744
56b6aeb0
HMH
2745/*************************************************************************
2746 * Wan subdriver
2747 */
2748
f74a27d4
HMH
2749enum {
2750 /* ACPI GWAN/SWAN bits */
2751 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2752 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2753 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2754};
2755
07431ec8
HMH
2756static int wan_get_radiosw(void)
2757{
2758 int status;
2759
2760 if (!tp_features.wan)
2761 return -ENODEV;
2762
2763 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2764 return -EIO;
2765
2766 return (status & TP_ACPI_WANCARD_RADIOSSW) != 0;
2767}
2768
2769static int wan_set_radiosw(int radio_on)
2770{
2771 int status;
2772
2773 if (!tp_features.wan)
2774 return -ENODEV;
2775
2776 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2777 return -EIO;
2778 if (radio_on)
2779 status |= TP_ACPI_WANCARD_RADIOSSW;
2780 else
2781 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2782 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2783 return -EIO;
2784
2785 return 0;
2786}
f74a27d4 2787
d3a6ade4
HMH
2788/* sysfs wan enable ---------------------------------------------------- */
2789static ssize_t wan_enable_show(struct device *dev,
2790 struct device_attribute *attr,
2791 char *buf)
2792{
2793 int status;
2794
2795 status = wan_get_radiosw();
2796 if (status < 0)
2797 return status;
2798
2799 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2800}
2801
2802static ssize_t wan_enable_store(struct device *dev,
2803 struct device_attribute *attr,
2804 const char *buf, size_t count)
2805{
2806 unsigned long t;
2807 int res;
2808
2809 if (parse_strtoul(buf, 1, &t))
2810 return -EINVAL;
2811
2812 res = wan_set_radiosw(t);
2813
2814 return (res) ? res : count;
2815}
2816
2817static struct device_attribute dev_attr_wan_enable =
cc4c24e1 2818 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
d3a6ade4
HMH
2819 wan_enable_show, wan_enable_store);
2820
2821/* --------------------------------------------------------------------- */
2822
2823static struct attribute *wan_attributes[] = {
2824 &dev_attr_wan_enable.attr,
2825 NULL
2826};
2827
2828static const struct attribute_group wan_attr_group = {
d3a6ade4
HMH
2829 .attrs = wan_attributes,
2830};
2831
07431ec8
HMH
2832static void wan_exit(void)
2833{
2834 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2835 &wan_attr_group);
2836}
2837
a5763f22 2838static int __init wan_init(struct ibm_init_struct *iibm)
42adb53c 2839{
d3a6ade4 2840 int res;
d6fdd1e9
HMH
2841 int status = 0;
2842
fe08bc4b
HMH
2843 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2844
e0c7dfe7 2845 TPACPI_ACPIHANDLE_INIT(hkey);
5fba344c 2846
d8fd94d9 2847 tp_features.wan = hkey_handle &&
d6fdd1e9
HMH
2848 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2849
2850 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2851 str_supported(tp_features.wan),
2852 status);
2853
3a872080
HMH
2854 if (tp_features.wan &&
2855 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
2856 /* no wan hardware present in system */
2857 tp_features.wan = 0;
2858 dbg_printk(TPACPI_DBG_INIT,
2859 "wan hardware not installed\n");
2860 }
2861
d3a6ade4 2862 if (tp_features.wan) {
3a872080
HMH
2863 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2864 &wan_attr_group);
2865 if (res)
2866 return res;
d6fdd1e9 2867 }
fe08bc4b 2868
d8fd94d9 2869 return (tp_features.wan)? 0 : 1;
42adb53c
JF
2870}
2871
d3a6ade4 2872/* procfs -------------------------------------------------------------- */
42adb53c
JF
2873static int wan_read(char *p)
2874{
2875 int len = 0;
d6fdd1e9 2876 int status = wan_get_radiosw();
42adb53c 2877
d8fd94d9 2878 if (!tp_features.wan)
42adb53c 2879 len += sprintf(p + len, "status:\t\tnot supported\n");
42adb53c 2880 else {
d6fdd1e9
HMH
2881 len += sprintf(p + len, "status:\t\t%s\n",
2882 (status)? "enabled" : "disabled");
42adb53c
JF
2883 len += sprintf(p + len, "commands:\tenable, disable\n");
2884 }
2885
2886 return len;
2887}
2888
2889static int wan_write(char *buf)
2890{
42adb53c 2891 char *cmd;
42adb53c 2892
d8fd94d9 2893 if (!tp_features.wan)
42adb53c
JF
2894 return -ENODEV;
2895
2896 while ((cmd = next_cmd(&buf))) {
2897 if (strlencmp(cmd, "enable") == 0) {
d6fdd1e9 2898 wan_set_radiosw(1);
42adb53c 2899 } else if (strlencmp(cmd, "disable") == 0) {
d6fdd1e9 2900 wan_set_radiosw(0);
42adb53c
JF
2901 } else
2902 return -EINVAL;
42adb53c
JF
2903 }
2904
42adb53c
JF
2905 return 0;
2906}
2907
a5763f22
HMH
2908static struct ibm_struct wan_driver_data = {
2909 .name = "wan",
2910 .read = wan_read,
2911 .write = wan_write,
d3a6ade4 2912 .exit = wan_exit,
92641177 2913 .flags.experimental = 1,
a5763f22
HMH
2914};
2915
56b6aeb0
HMH
2916/*************************************************************************
2917 * Video subdriver
2918 */
2919
d7c1d17d
HMH
2920#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2921
f74a27d4
HMH
2922enum video_access_mode {
2923 TPACPI_VIDEO_NONE = 0,
2924 TPACPI_VIDEO_570, /* 570 */
2925 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2926 TPACPI_VIDEO_NEW, /* all others */
2927};
2928
2929enum { /* video status flags, based on VIDEO_570 */
2930 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2931 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2932 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2933};
2934
2935enum { /* TPACPI_VIDEO_570 constants */
2936 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2937 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2938 * video_status_flags */
2939 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2940 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2941};
2942
9a8e1738
HMH
2943static enum video_access_mode video_supported;
2944static int video_orig_autosw;
78f81cc4 2945
f74a27d4
HMH
2946static int video_autosw_get(void);
2947static int video_autosw_set(int enable);
2948
e0c7dfe7 2949TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
56b6aeb0 2950
a5763f22 2951static int __init video_init(struct ibm_init_struct *iibm)
1da177e4 2952{
78f81cc4
BD
2953 int ivga;
2954
fe08bc4b
HMH
2955 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2956
e0c7dfe7
HMH
2957 TPACPI_ACPIHANDLE_INIT(vid);
2958 TPACPI_ACPIHANDLE_INIT(vid2);
5fba344c 2959
78f81cc4
BD
2960 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2961 /* G41, assume IVGA doesn't change */
2962 vid_handle = vid2_handle;
2963
2964 if (!vid_handle)
2965 /* video switching not supported on R30, R31 */
efa27145 2966 video_supported = TPACPI_VIDEO_NONE;
78f81cc4
BD
2967 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2968 /* 570 */
efa27145 2969 video_supported = TPACPI_VIDEO_570;
78f81cc4
BD
2970 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2971 /* 600e/x, 770e, 770x */
efa27145 2972 video_supported = TPACPI_VIDEO_770;
78f81cc4
BD
2973 else
2974 /* all others */
efa27145 2975 video_supported = TPACPI_VIDEO_NEW;
1da177e4 2976
fe08bc4b
HMH
2977 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2978 str_supported(video_supported != TPACPI_VIDEO_NONE),
2979 video_supported);
2980
5fba344c 2981 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1da177e4
LT
2982}
2983
56b6aeb0
HMH
2984static void video_exit(void)
2985{
83f34724
HMH
2986 dbg_printk(TPACPI_DBG_EXIT,
2987 "restoring original video autoswitch mode\n");
2988 if (video_autosw_set(video_orig_autosw))
e0c7dfe7 2989 printk(TPACPI_ERR "error while trying to restore original "
83f34724 2990 "video autoswitch mode\n");
56b6aeb0
HMH
2991}
2992
83f34724 2993static int video_outputsw_get(void)
1da177e4
LT
2994{
2995 int status = 0;
2996 int i;
2997
83f34724
HMH
2998 switch (video_supported) {
2999 case TPACPI_VIDEO_570:
3000 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3001 TP_ACPI_VIDEO_570_PHSCMD))
3002 return -EIO;
3003 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3004 break;
3005 case TPACPI_VIDEO_770:
3006 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3007 return -EIO;
3008 if (i)
3009 status |= TP_ACPI_VIDEO_S_LCD;
3010 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3011 return -EIO;
3012 if (i)
3013 status |= TP_ACPI_VIDEO_S_CRT;
3014 break;
3015 case TPACPI_VIDEO_NEW:
3016 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3017 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3018 return -EIO;
3019 if (i)
3020 status |= TP_ACPI_VIDEO_S_CRT;
3021
3022 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3023 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3024 return -EIO;
3025 if (i)
3026 status |= TP_ACPI_VIDEO_S_LCD;
3027 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3028 return -EIO;
3029 if (i)
3030 status |= TP_ACPI_VIDEO_S_DVI;
3031 break;
3032 default:
3033 return -ENOSYS;
78f81cc4
BD
3034 }
3035
3036 return status;
3037}
1da177e4 3038
83f34724 3039static int video_outputsw_set(int status)
78f81cc4 3040{
83f34724
HMH
3041 int autosw;
3042 int res = 0;
3043
3044 switch (video_supported) {
3045 case TPACPI_VIDEO_570:
3046 res = acpi_evalf(NULL, NULL,
3047 "\\_SB.PHS2", "vdd",
3048 TP_ACPI_VIDEO_570_PHS2CMD,
3049 status | TP_ACPI_VIDEO_570_PHS2SET);
3050 break;
3051 case TPACPI_VIDEO_770:
3052 autosw = video_autosw_get();
3053 if (autosw < 0)
3054 return autosw;
1da177e4 3055
83f34724
HMH
3056 res = video_autosw_set(1);
3057 if (res)
3058 return res;
3059 res = acpi_evalf(vid_handle, NULL,
3060 "ASWT", "vdd", status * 0x100, 0);
3061 if (!autosw && video_autosw_set(autosw)) {
35ff8b9f
HMH
3062 printk(TPACPI_ERR
3063 "video auto-switch left enabled due to error\n");
83f34724
HMH
3064 return -EIO;
3065 }
3066 break;
3067 case TPACPI_VIDEO_NEW:
3068 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
35ff8b9f 3069 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
83f34724
HMH
3070 break;
3071 default:
3072 return -ENOSYS;
3073 }
1da177e4 3074
83f34724 3075 return (res)? 0 : -EIO;
1da177e4
LT
3076}
3077
83f34724 3078static int video_autosw_get(void)
78f81cc4 3079{
83f34724 3080 int autosw = 0;
78f81cc4 3081
83f34724
HMH
3082 switch (video_supported) {
3083 case TPACPI_VIDEO_570:
3084 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3085 return -EIO;
3086 break;
3087 case TPACPI_VIDEO_770:
3088 case TPACPI_VIDEO_NEW:
3089 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3090 return -EIO;
3091 break;
3092 default:
3093 return -ENOSYS;
3094 }
78f81cc4 3095
83f34724 3096 return autosw & 1;
78f81cc4
BD
3097}
3098
83f34724 3099static int video_autosw_set(int enable)
78f81cc4 3100{
83f34724
HMH
3101 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3102 return -EIO;
3103 return 0;
78f81cc4
BD
3104}
3105
83f34724 3106static int video_outputsw_cycle(void)
78f81cc4 3107{
83f34724
HMH
3108 int autosw = video_autosw_get();
3109 int res;
78f81cc4 3110
83f34724
HMH
3111 if (autosw < 0)
3112 return autosw;
78f81cc4 3113
83f34724
HMH
3114 switch (video_supported) {
3115 case TPACPI_VIDEO_570:
3116 res = video_autosw_set(1);
3117 if (res)
3118 return res;
3119 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3120 break;
3121 case TPACPI_VIDEO_770:
3122 case TPACPI_VIDEO_NEW:
3123 res = video_autosw_set(1);
3124 if (res)
3125 return res;
3126 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3127 break;
3128 default:
3129 return -ENOSYS;
3130 }
3131 if (!autosw && video_autosw_set(autosw)) {
35ff8b9f
HMH
3132 printk(TPACPI_ERR
3133 "video auto-switch left enabled due to error\n");
83f34724 3134 return -EIO;
78f81cc4
BD
3135 }
3136
83f34724
HMH
3137 return (res)? 0 : -EIO;
3138}
3139
3140static int video_expand_toggle(void)
3141{
3142 switch (video_supported) {
3143 case TPACPI_VIDEO_570:
3144 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3145 0 : -EIO;
3146 case TPACPI_VIDEO_770:
3147 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3148 0 : -EIO;
3149 case TPACPI_VIDEO_NEW:
3150 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3151 0 : -EIO;
3152 default:
3153 return -ENOSYS;
3154 }
3155 /* not reached */
78f81cc4
BD
3156}
3157
56b6aeb0
HMH
3158static int video_read(char *p)
3159{
83f34724 3160 int status, autosw;
56b6aeb0
HMH
3161 int len = 0;
3162
83f34724 3163 if (video_supported == TPACPI_VIDEO_NONE) {
56b6aeb0
HMH
3164 len += sprintf(p + len, "status:\t\tnot supported\n");
3165 return len;
3166 }
3167
83f34724
HMH
3168 status = video_outputsw_get();
3169 if (status < 0)
3170 return status;
3171
3172 autosw = video_autosw_get();
3173 if (autosw < 0)
3174 return autosw;
3175
56b6aeb0
HMH
3176 len += sprintf(p + len, "status:\t\tsupported\n");
3177 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3178 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
efa27145 3179 if (video_supported == TPACPI_VIDEO_NEW)
56b6aeb0
HMH
3180 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3181 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3182 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3183 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
efa27145 3184 if (video_supported == TPACPI_VIDEO_NEW)
56b6aeb0
HMH
3185 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3186 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3187 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3188
3189 return len;
3190}
3191
78f81cc4 3192static int video_write(char *buf)
1da177e4
LT
3193{
3194 char *cmd;
3195 int enable, disable, status;
83f34724 3196 int res;
1da177e4 3197
83f34724 3198 if (video_supported == TPACPI_VIDEO_NONE)
78f81cc4
BD
3199 return -ENODEV;
3200
83f34724
HMH
3201 enable = 0;
3202 disable = 0;
1da177e4
LT
3203
3204 while ((cmd = next_cmd(&buf))) {
3205 if (strlencmp(cmd, "lcd_enable") == 0) {
83f34724 3206 enable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 3207 } else if (strlencmp(cmd, "lcd_disable") == 0) {
83f34724 3208 disable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 3209 } else if (strlencmp(cmd, "crt_enable") == 0) {
83f34724 3210 enable |= TP_ACPI_VIDEO_S_CRT;
1da177e4 3211 } else if (strlencmp(cmd, "crt_disable") == 0) {
83f34724 3212 disable |= TP_ACPI_VIDEO_S_CRT;
efa27145 3213 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 3214 strlencmp(cmd, "dvi_enable") == 0) {
83f34724 3215 enable |= TP_ACPI_VIDEO_S_DVI;
efa27145 3216 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 3217 strlencmp(cmd, "dvi_disable") == 0) {
83f34724 3218 disable |= TP_ACPI_VIDEO_S_DVI;
1da177e4 3219 } else if (strlencmp(cmd, "auto_enable") == 0) {
83f34724
HMH
3220 res = video_autosw_set(1);
3221 if (res)
3222 return res;
1da177e4 3223 } else if (strlencmp(cmd, "auto_disable") == 0) {
83f34724
HMH
3224 res = video_autosw_set(0);
3225 if (res)
3226 return res;
1da177e4 3227 } else if (strlencmp(cmd, "video_switch") == 0) {
83f34724
HMH
3228 res = video_outputsw_cycle();
3229 if (res)
3230 return res;
1da177e4 3231 } else if (strlencmp(cmd, "expand_toggle") == 0) {
83f34724
HMH
3232 res = video_expand_toggle();
3233 if (res)
3234 return res;
1da177e4
LT
3235 } else
3236 return -EINVAL;
3237 }
3238
3239 if (enable || disable) {
83f34724
HMH
3240 status = video_outputsw_get();
3241 if (status < 0)
3242 return status;
3243 res = video_outputsw_set((status & ~disable) | enable);
3244 if (res)
3245 return res;
1da177e4
LT
3246 }
3247
3248 return 0;
3249}
3250
a5763f22
HMH
3251static struct ibm_struct video_driver_data = {
3252 .name = "video",
3253 .read = video_read,
3254 .write = video_write,
3255 .exit = video_exit,
3256};
3257
d7c1d17d
HMH
3258#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3259
56b6aeb0
HMH
3260/*************************************************************************
3261 * Light (thinklight) subdriver
3262 */
1da177e4 3263
e0c7dfe7
HMH
3264TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3265TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
56b6aeb0 3266
4fa6811b
HMH
3267static int light_get_status(void)
3268{
3269 int status = 0;
3270
3271 if (tp_features.light_status) {
3272 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3273 return -EIO;
3274 return (!!status);
3275 }
3276
3277 return -ENXIO;
3278}
3279
3280static int light_set_status(int status)
3281{
3282 int rc;
3283
3284 if (tp_features.light) {
3285 if (cmos_handle) {
3286 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3287 (status)?
3288 TP_CMOS_THINKLIGHT_ON :
3289 TP_CMOS_THINKLIGHT_OFF);
3290 } else {
3291 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3292 (status)? 1 : 0);
3293 }
3294 return (rc)? 0 : -EIO;
3295 }
3296
3297 return -ENXIO;
3298}
3299
e306501d
HMH
3300static void light_set_status_worker(struct work_struct *work)
3301{
3302 struct tpacpi_led_classdev *data =
3303 container_of(work, struct tpacpi_led_classdev, work);
3304
3305 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3306 light_set_status((data->new_brightness != LED_OFF));
3307}
3308
3309static void light_sysfs_set(struct led_classdev *led_cdev,
3310 enum led_brightness brightness)
3311{
3312 struct tpacpi_led_classdev *data =
3313 container_of(led_cdev,
3314 struct tpacpi_led_classdev,
3315 led_classdev);
3316 data->new_brightness = brightness;
e0e3c061 3317 queue_work(tpacpi_wq, &data->work);
e306501d
HMH
3318}
3319
3320static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3321{
3322 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3323}
3324
3325static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3326 .led_classdev = {
3327 .name = "tpacpi::thinklight",
3328 .brightness_set = &light_sysfs_set,
3329 .brightness_get = &light_sysfs_get,
3330 }
3331};
3332
a5763f22 3333static int __init light_init(struct ibm_init_struct *iibm)
1da177e4 3334{
9c0a76e1 3335 int rc;
e306501d 3336
fe08bc4b
HMH
3337 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3338
e0c7dfe7
HMH
3339 TPACPI_ACPIHANDLE_INIT(ledb);
3340 TPACPI_ACPIHANDLE_INIT(lght);
3341 TPACPI_ACPIHANDLE_INIT(cmos);
e306501d 3342 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
5fba344c 3343
78f81cc4 3344 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
d8fd94d9 3345 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
78f81cc4 3346
d8fd94d9 3347 if (tp_features.light)
78f81cc4
BD
3348 /* light status not supported on
3349 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
d8fd94d9
HMH
3350 tp_features.light_status =
3351 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1da177e4 3352
9c0a76e1
HMH
3353 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3354 str_supported(tp_features.light),
3355 str_supported(tp_features.light_status));
fe08bc4b 3356
9c0a76e1
HMH
3357 if (!tp_features.light)
3358 return 1;
3359
3360 rc = led_classdev_register(&tpacpi_pdev->dev,
3361 &tpacpi_led_thinklight.led_classdev);
e306501d
HMH
3362
3363 if (rc < 0) {
3364 tp_features.light = 0;
3365 tp_features.light_status = 0;
9c0a76e1
HMH
3366 } else {
3367 rc = 0;
e306501d 3368 }
9c0a76e1 3369
e306501d
HMH
3370 return rc;
3371}
3372
3373static void light_exit(void)
3374{
3375 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3376 if (work_pending(&tpacpi_led_thinklight.work))
e0e3c061 3377 flush_workqueue(tpacpi_wq);
1da177e4
LT
3378}
3379
78f81cc4 3380static int light_read(char *p)
1da177e4
LT
3381{
3382 int len = 0;
4fa6811b 3383 int status;
1da177e4 3384
d8fd94d9 3385 if (!tp_features.light) {
78f81cc4 3386 len += sprintf(p + len, "status:\t\tnot supported\n");
d8fd94d9 3387 } else if (!tp_features.light_status) {
78f81cc4
BD
3388 len += sprintf(p + len, "status:\t\tunknown\n");
3389 len += sprintf(p + len, "commands:\ton, off\n");
3390 } else {
4fa6811b
HMH
3391 status = light_get_status();
3392 if (status < 0)
3393 return status;
1da177e4 3394 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
78f81cc4
BD
3395 len += sprintf(p + len, "commands:\ton, off\n");
3396 }
1da177e4
LT
3397
3398 return len;
3399}
3400
78f81cc4 3401static int light_write(char *buf)
1da177e4 3402{
1da177e4 3403 char *cmd;
4fa6811b 3404 int newstatus = 0;
78f81cc4 3405
d8fd94d9 3406 if (!tp_features.light)
78f81cc4
BD
3407 return -ENODEV;
3408
1da177e4
LT
3409 while ((cmd = next_cmd(&buf))) {
3410 if (strlencmp(cmd, "on") == 0) {
4fa6811b 3411 newstatus = 1;
1da177e4 3412 } else if (strlencmp(cmd, "off") == 0) {
4fa6811b 3413 newstatus = 0;
1da177e4
LT
3414 } else
3415 return -EINVAL;
1da177e4
LT
3416 }
3417
4fa6811b 3418 return light_set_status(newstatus);
1da177e4
LT
3419}
3420
a5763f22
HMH
3421static struct ibm_struct light_driver_data = {
3422 .name = "light",
3423 .read = light_read,
3424 .write = light_write,
e306501d 3425 .exit = light_exit,
a5763f22
HMH
3426};
3427
56b6aeb0
HMH
3428/*************************************************************************
3429 * Dock subdriver
3430 */
1da177e4 3431
85998248 3432#ifdef CONFIG_THINKPAD_ACPI_DOCK
56b6aeb0 3433
f74a27d4
HMH
3434static void dock_notify(struct ibm_struct *ibm, u32 event);
3435static int dock_read(char *p);
3436static int dock_write(char *buf);
3437
e0c7dfe7 3438TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
56b6aeb0
HMH
3439 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3440 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3441 "\\_SB.PCI.ISA.SLCE", /* 570 */
3442 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3443
5fba344c 3444/* don't list other alternatives as we install a notify handler on the 570 */
e0c7dfe7 3445TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
5fba344c 3446
1ba90e3a
TR
3447static const struct acpi_device_id ibm_pci_device_ids[] = {
3448 {PCI_ROOT_HID_STRING, 0},
3449 {"", 0},
3450};
3451
d94a7f16
HMH
3452static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3453 {
3454 .notify = dock_notify,
3455 .handle = &dock_handle,
3456 .type = ACPI_SYSTEM_NOTIFY,
3457 },
3458 {
996fba08
HMH
3459 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3460 * We just use it to get notifications of dock hotplug
3461 * in very old thinkpads */
1ba90e3a 3462 .hid = ibm_pci_device_ids,
d94a7f16
HMH
3463 .notify = dock_notify,
3464 .handle = &pci_handle,
3465 .type = ACPI_SYSTEM_NOTIFY,
3466 },
3467};
3468
3469static struct ibm_struct dock_driver_data[2] = {
3470 {
3471 .name = "dock",
3472 .read = dock_read,
3473 .write = dock_write,
3474 .acpi = &ibm_dock_acpidriver[0],
3475 },
3476 {
3477 .name = "dock",
3478 .acpi = &ibm_dock_acpidriver[1],
3479 },
3480};
3481
1da177e4
LT
3482#define dock_docked() (_sta(dock_handle) & 1)
3483
a5763f22 3484static int __init dock_init(struct ibm_init_struct *iibm)
5fba344c 3485{
fe08bc4b
HMH
3486 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3487
e0c7dfe7 3488 TPACPI_ACPIHANDLE_INIT(dock);
5fba344c 3489
fe08bc4b
HMH
3490 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3491 str_supported(dock_handle != NULL));
3492
5fba344c
HMH
3493 return (dock_handle)? 0 : 1;
3494}
3495
d94a7f16
HMH
3496static int __init dock_init2(struct ibm_init_struct *iibm)
3497{
3498 int dock2_needed;
3499
3500 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3501
3502 if (dock_driver_data[0].flags.acpi_driver_registered &&
3503 dock_driver_data[0].flags.acpi_notify_installed) {
e0c7dfe7 3504 TPACPI_ACPIHANDLE_INIT(pci);
d94a7f16
HMH
3505 dock2_needed = (pci_handle != NULL);
3506 vdbg_printk(TPACPI_DBG_INIT,
3507 "dock PCI handler for the TP 570 is %s\n",
3508 str_supported(dock2_needed));
3509 } else {
3510 vdbg_printk(TPACPI_DBG_INIT,
3511 "dock subdriver part 2 not required\n");
3512 dock2_needed = 0;
3513 }
3514
3515 return (dock2_needed)? 0 : 1;
3516}
3517
56b6aeb0
HMH
3518static void dock_notify(struct ibm_struct *ibm, u32 event)
3519{
3520 int docked = dock_docked();
1ba90e3a
TR
3521 int pci = ibm->acpi->hid && ibm->acpi->device &&
3522 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
962ce8ca 3523 int data;
56b6aeb0
HMH
3524
3525 if (event == 1 && !pci) /* 570 */
962ce8ca 3526 data = 1; /* button */
56b6aeb0 3527 else if (event == 1 && pci) /* 570 */
962ce8ca 3528 data = 3; /* dock */
56b6aeb0 3529 else if (event == 3 && docked)
962ce8ca 3530 data = 1; /* button */
56b6aeb0 3531 else if (event == 3 && !docked)
962ce8ca 3532 data = 2; /* undock */
56b6aeb0 3533 else if (event == 0 && docked)
962ce8ca 3534 data = 3; /* dock */
56b6aeb0 3535 else {
e0c7dfe7 3536 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
56b6aeb0 3537 event, _sta(dock_handle));
962ce8ca 3538 data = 0; /* unknown */
56b6aeb0 3539 }
14e04fb3 3540 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
962ce8ca
ZR
3541 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3542 ibm->acpi->device->dev.bus_id,
3543 event, data);
56b6aeb0
HMH
3544}
3545
78f81cc4 3546static int dock_read(char *p)
1da177e4
LT
3547{
3548 int len = 0;
3549 int docked = dock_docked();
3550
3551 if (!dock_handle)
3552 len += sprintf(p + len, "status:\t\tnot supported\n");
3553 else if (!docked)
3554 len += sprintf(p + len, "status:\t\tundocked\n");
3555 else {
3556 len += sprintf(p + len, "status:\t\tdocked\n");
3557 len += sprintf(p + len, "commands:\tdock, undock\n");
3558 }
3559
3560 return len;
3561}
3562
78f81cc4 3563static int dock_write(char *buf)
1da177e4
LT
3564{
3565 char *cmd;
3566
3567 if (!dock_docked())
78f81cc4 3568 return -ENODEV;
1da177e4
LT
3569
3570 while ((cmd = next_cmd(&buf))) {
3571 if (strlencmp(cmd, "undock") == 0) {
78f81cc4
BD
3572 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3573 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
56b6aeb0
HMH
3574 return -EIO;
3575 } else if (strlencmp(cmd, "dock") == 0) {
3576 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3577 return -EIO;
3578 } else
3579 return -EINVAL;
1da177e4 3580 }
56b6aeb0
HMH
3581
3582 return 0;
1da177e4 3583}
56b6aeb0 3584
85998248 3585#endif /* CONFIG_THINKPAD_ACPI_DOCK */
56b6aeb0
HMH
3586
3587/*************************************************************************
3588 * Bay subdriver
3589 */
1da177e4 3590
85998248 3591#ifdef CONFIG_THINKPAD_ACPI_BAY
f74a27d4 3592
e0c7dfe7 3593TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
56b6aeb0
HMH
3594 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3595 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3596 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3597 ); /* A21e, R30, R31 */
e0c7dfe7 3598TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
56b6aeb0
HMH
3599 "_EJ0", /* all others */
3600 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
e0c7dfe7 3601TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
56b6aeb0
HMH
3602 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3603 ); /* all others */
e0c7dfe7 3604TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
56b6aeb0
HMH
3605 "_EJ0", /* 770x */
3606 ); /* all others */
3607
a5763f22 3608static int __init bay_init(struct ibm_init_struct *iibm)
1da177e4 3609{
fe08bc4b
HMH
3610 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3611
e0c7dfe7 3612 TPACPI_ACPIHANDLE_INIT(bay);
5fba344c 3613 if (bay_handle)
e0c7dfe7
HMH
3614 TPACPI_ACPIHANDLE_INIT(bay_ej);
3615 TPACPI_ACPIHANDLE_INIT(bay2);
5fba344c 3616 if (bay2_handle)
e0c7dfe7 3617 TPACPI_ACPIHANDLE_INIT(bay2_ej);
5fba344c 3618
d8fd94d9
HMH
3619 tp_features.bay_status = bay_handle &&
3620 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3621 tp_features.bay_status2 = bay2_handle &&
3622 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
78f81cc4 3623
d8fd94d9
HMH
3624 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3625 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3626 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3627 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1da177e4 3628
fe08bc4b
HMH
3629 vdbg_printk(TPACPI_DBG_INIT,
3630 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
d8fd94d9
HMH
3631 str_supported(tp_features.bay_status),
3632 str_supported(tp_features.bay_eject),
3633 str_supported(tp_features.bay_status2),
3634 str_supported(tp_features.bay_eject2));
fe08bc4b 3635
d8fd94d9
HMH
3636 return (tp_features.bay_status || tp_features.bay_eject ||
3637 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1da177e4
LT
3638}
3639
56b6aeb0
HMH
3640static void bay_notify(struct ibm_struct *ibm, u32 event)
3641{
14e04fb3 3642 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
962ce8ca
ZR
3643 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3644 ibm->acpi->device->dev.bus_id,
3645 event, 0);
56b6aeb0
HMH
3646}
3647
78f81cc4
BD
3648#define bay_occupied(b) (_sta(b##_handle) & 1)
3649
3650static int bay_read(char *p)
1da177e4
LT
3651{
3652 int len = 0;
78f81cc4
BD
3653 int occupied = bay_occupied(bay);
3654 int occupied2 = bay_occupied(bay2);
3655 int eject, eject2;
3656
d8fd94d9
HMH
3657 len += sprintf(p + len, "status:\t\t%s\n",
3658 tp_features.bay_status ?
3659 (occupied ? "occupied" : "unoccupied") :
3660 "not supported");
3661 if (tp_features.bay_status2)
78f81cc4
BD
3662 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3663 "occupied" : "unoccupied");
3664
d8fd94d9
HMH
3665 eject = tp_features.bay_eject && occupied;
3666 eject2 = tp_features.bay_eject2 && occupied2;
78f81cc4
BD
3667
3668 if (eject && eject2)
3669 len += sprintf(p + len, "commands:\teject, eject2\n");
3670 else if (eject)
1da177e4 3671 len += sprintf(p + len, "commands:\teject\n");
78f81cc4
BD
3672 else if (eject2)
3673 len += sprintf(p + len, "commands:\teject2\n");
1da177e4
LT
3674
3675 return len;
3676}
3677
78f81cc4 3678static int bay_write(char *buf)
1da177e4
LT
3679{
3680 char *cmd;
3681
d8fd94d9 3682 if (!tp_features.bay_eject && !tp_features.bay_eject2)
78f81cc4
BD
3683 return -ENODEV;
3684
1da177e4 3685 while ((cmd = next_cmd(&buf))) {
d8fd94d9 3686 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
78f81cc4
BD
3687 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3688 return -EIO;
d8fd94d9 3689 } else if (tp_features.bay_eject2 &&
78f81cc4
BD
3690 strlencmp(cmd, "eject2") == 0) {
3691 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1da177e4
LT
3692 return -EIO;
3693 } else
3694 return -EINVAL;
3695 }
3696
3697 return 0;
78f81cc4 3698}
a5763f22 3699
8d376cd6
HMH
3700static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3701 .notify = bay_notify,
3702 .handle = &bay_handle,
3703 .type = ACPI_SYSTEM_NOTIFY,
3704};
3705
a5763f22
HMH
3706static struct ibm_struct bay_driver_data = {
3707 .name = "bay",
3708 .read = bay_read,
3709 .write = bay_write,
8d376cd6 3710 .acpi = &ibm_bay_acpidriver,
a5763f22
HMH
3711};
3712
85998248 3713#endif /* CONFIG_THINKPAD_ACPI_BAY */
1da177e4 3714
56b6aeb0
HMH
3715/*************************************************************************
3716 * CMOS subdriver
3717 */
3718
b616004c
HMH
3719/* sysfs cmos_command -------------------------------------------------- */
3720static ssize_t cmos_command_store(struct device *dev,
3721 struct device_attribute *attr,
3722 const char *buf, size_t count)
3723{
3724 unsigned long cmos_cmd;
3725 int res;
3726
3727 if (parse_strtoul(buf, 21, &cmos_cmd))
3728 return -EINVAL;
3729
3730 res = issue_thinkpad_cmos_command(cmos_cmd);
3731 return (res)? res : count;
3732}
3733
3734static struct device_attribute dev_attr_cmos_command =
3735 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3736
3737/* --------------------------------------------------------------------- */
3738
a5763f22 3739static int __init cmos_init(struct ibm_init_struct *iibm)
5fba344c 3740{
b616004c
HMH
3741 int res;
3742
fe08bc4b
HMH
3743 vdbg_printk(TPACPI_DBG_INIT,
3744 "initializing cmos commands subdriver\n");
3745
e0c7dfe7 3746 TPACPI_ACPIHANDLE_INIT(cmos);
5fba344c 3747
fe08bc4b
HMH
3748 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3749 str_supported(cmos_handle != NULL));
b616004c
HMH
3750
3751 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3752 if (res)
3753 return res;
3754
5fba344c
HMH
3755 return (cmos_handle)? 0 : 1;
3756}
3757
b616004c
HMH
3758static void cmos_exit(void)
3759{
3760 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3761}
3762
78f81cc4 3763static int cmos_read(char *p)
1da177e4
LT
3764{
3765 int len = 0;
3766
78f81cc4
BD
3767 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3768 R30, R31, T20-22, X20-21 */
1da177e4
LT
3769 if (!cmos_handle)
3770 len += sprintf(p + len, "status:\t\tnot supported\n");
3771 else {
3772 len += sprintf(p + len, "status:\t\tsupported\n");
78f81cc4 3773 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1da177e4
LT
3774 }
3775
3776 return len;
3777}
3778
78f81cc4 3779static int cmos_write(char *buf)
1da177e4
LT
3780{
3781 char *cmd;
c9bea99c 3782 int cmos_cmd, res;
1da177e4
LT
3783
3784 while ((cmd = next_cmd(&buf))) {
78f81cc4
BD
3785 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3786 cmos_cmd >= 0 && cmos_cmd <= 21) {
1da177e4
LT
3787 /* cmos_cmd set */
3788 } else
3789 return -EINVAL;
3790
c9bea99c
HMH
3791 res = issue_thinkpad_cmos_command(cmos_cmd);
3792 if (res)
3793 return res;
1da177e4
LT
3794 }
3795
3796 return 0;
78f81cc4
BD
3797}
3798
a5763f22
HMH
3799static struct ibm_struct cmos_driver_data = {
3800 .name = "cmos",
3801 .read = cmos_read,
3802 .write = cmos_write,
b616004c 3803 .exit = cmos_exit,
a5763f22 3804};
56b6aeb0
HMH
3805
3806/*************************************************************************
3807 * LED subdriver
3808 */
3809
f74a27d4
HMH
3810enum led_access_mode {
3811 TPACPI_LED_NONE = 0,
3812 TPACPI_LED_570, /* 570 */
3813 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3814 TPACPI_LED_NEW, /* all others */
3815};
3816
3817enum { /* For TPACPI_LED_OLD */
3818 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3819 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3820 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3821};
3822
4fa6811b
HMH
3823enum led_status_t {
3824 TPACPI_LED_OFF = 0,
3825 TPACPI_LED_ON,
3826 TPACPI_LED_BLINK,
3827};
3828
9a8e1738 3829static enum led_access_mode led_supported;
78f81cc4 3830
e0c7dfe7 3831TPACPI_HANDLE(led, ec, "SLED", /* 570 */
35ff8b9f
HMH
3832 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3833 /* T20-22, X20-21 */
56b6aeb0
HMH
3834 "LED", /* all others */
3835 ); /* R30, R31 */
3836
af116101
HMH
3837#define TPACPI_LED_NUMLEDS 8
3838static struct tpacpi_led_classdev *tpacpi_leds;
3839static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
e3aa51fe 3840static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
af116101
HMH
3841 /* there's a limit of 19 chars + NULL before 2.6.26 */
3842 "tpacpi::power",
3843 "tpacpi:orange:batt",
3844 "tpacpi:green:batt",
3845 "tpacpi::dock_active",
3846 "tpacpi::bay_active",
3847 "tpacpi::dock_batt",
3848 "tpacpi::unknown_led",
3849 "tpacpi::standby",
3850};
3851
24e45bbe 3852static int led_get_status(const unsigned int led)
4fa6811b
HMH
3853{
3854 int status;
af116101 3855 enum led_status_t led_s;
4fa6811b
HMH
3856
3857 switch (led_supported) {
3858 case TPACPI_LED_570:
3859 if (!acpi_evalf(ec_handle,
3860 &status, "GLED", "dd", 1 << led))
3861 return -EIO;
af116101 3862 led_s = (status == 0)?
4fa6811b
HMH
3863 TPACPI_LED_OFF :
3864 ((status == 1)?
3865 TPACPI_LED_ON :
3866 TPACPI_LED_BLINK);
af116101
HMH
3867 tpacpi_led_state_cache[led] = led_s;
3868 return led_s;
4fa6811b
HMH
3869 default:
3870 return -ENXIO;
3871 }
3872
3873 /* not reached */
3874}
3875
24e45bbe
HMH
3876static int led_set_status(const unsigned int led,
3877 const enum led_status_t ledstatus)
4fa6811b
HMH
3878{
3879 /* off, on, blink. Index is led_status_t */
24e45bbe
HMH
3880 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
3881 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4fa6811b
HMH
3882
3883 int rc = 0;
3884
3885 switch (led_supported) {
3886 case TPACPI_LED_570:
24e45bbe
HMH
3887 /* 570 */
3888 if (led > 7)
3889 return -EINVAL;
3890 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3891 (1 << led), led_sled_arg1[ledstatus]))
3892 rc = -EIO;
3893 break;
4fa6811b 3894 case TPACPI_LED_OLD:
24e45bbe
HMH
3895 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3896 if (led > 7)
3897 return -EINVAL;
3898 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
3899 if (rc >= 0)
3900 rc = ec_write(TPACPI_LED_EC_HLBL,
3901 (ledstatus == TPACPI_LED_BLINK) << led);
3902 if (rc >= 0)
3903 rc = ec_write(TPACPI_LED_EC_HLCL,
3904 (ledstatus != TPACPI_LED_OFF) << led);
3905 break;
4fa6811b 3906 case TPACPI_LED_NEW:
24e45bbe
HMH
3907 /* all others */
3908 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3909 led, led_led_arg1[ledstatus]))
3910 rc = -EIO;
3911 break;
4fa6811b
HMH
3912 default:
3913 rc = -ENXIO;
3914 }
3915
af116101
HMH
3916 if (!rc)
3917 tpacpi_led_state_cache[led] = ledstatus;
3918
3919 return rc;
3920}
3921
3922static void led_sysfs_set_status(unsigned int led,
3923 enum led_brightness brightness)
3924{
3925 led_set_status(led,
3926 (brightness == LED_OFF) ?
3927 TPACPI_LED_OFF :
3928 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
3929 TPACPI_LED_BLINK : TPACPI_LED_ON);
3930}
3931
3932static void led_set_status_worker(struct work_struct *work)
3933{
3934 struct tpacpi_led_classdev *data =
3935 container_of(work, struct tpacpi_led_classdev, work);
3936
3937 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3938 led_sysfs_set_status(data->led, data->new_brightness);
3939}
3940
3941static void led_sysfs_set(struct led_classdev *led_cdev,
3942 enum led_brightness brightness)
3943{
3944 struct tpacpi_led_classdev *data = container_of(led_cdev,
3945 struct tpacpi_led_classdev, led_classdev);
3946
3947 data->new_brightness = brightness;
e0e3c061 3948 queue_work(tpacpi_wq, &data->work);
af116101
HMH
3949}
3950
3951static int led_sysfs_blink_set(struct led_classdev *led_cdev,
3952 unsigned long *delay_on, unsigned long *delay_off)
3953{
3954 struct tpacpi_led_classdev *data = container_of(led_cdev,
3955 struct tpacpi_led_classdev, led_classdev);
3956
3957 /* Can we choose the flash rate? */
3958 if (*delay_on == 0 && *delay_off == 0) {
3959 /* yes. set them to the hardware blink rate (1 Hz) */
3960 *delay_on = 500; /* ms */
3961 *delay_off = 500; /* ms */
3962 } else if ((*delay_on != 500) || (*delay_off != 500))
3963 return -EINVAL;
3964
3965 data->new_brightness = TPACPI_LED_BLINK;
e0e3c061 3966 queue_work(tpacpi_wq, &data->work);
af116101
HMH
3967
3968 return 0;
3969}
3970
3971static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
3972{
3973 int rc;
3974
3975 struct tpacpi_led_classdev *data = container_of(led_cdev,
3976 struct tpacpi_led_classdev, led_classdev);
3977
3978 rc = led_get_status(data->led);
3979
3980 if (rc == TPACPI_LED_OFF || rc < 0)
3981 rc = LED_OFF; /* no error handling in led class :( */
3982 else
3983 rc = LED_FULL;
3984
4fa6811b
HMH
3985 return rc;
3986}
3987
af116101
HMH
3988static void led_exit(void)
3989{
3990 unsigned int i;
3991
3992 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
3993 if (tpacpi_leds[i].led_classdev.name)
3994 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
3995 }
3996
3997 kfree(tpacpi_leds);
af116101
HMH
3998}
3999
a5763f22 4000static int __init led_init(struct ibm_init_struct *iibm)
78f81cc4 4001{
af116101
HMH
4002 unsigned int i;
4003 int rc;
4004
fe08bc4b
HMH
4005 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4006
e0c7dfe7 4007 TPACPI_ACPIHANDLE_INIT(led);
5fba344c 4008
78f81cc4
BD
4009 if (!led_handle)
4010 /* led not supported on R30, R31 */
efa27145 4011 led_supported = TPACPI_LED_NONE;
78f81cc4
BD
4012 else if (strlencmp(led_path, "SLED") == 0)
4013 /* 570 */
efa27145 4014 led_supported = TPACPI_LED_570;
78f81cc4
BD
4015 else if (strlencmp(led_path, "SYSL") == 0)
4016 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
efa27145 4017 led_supported = TPACPI_LED_OLD;
78f81cc4
BD
4018 else
4019 /* all others */
efa27145 4020 led_supported = TPACPI_LED_NEW;
78f81cc4 4021
fe08bc4b
HMH
4022 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4023 str_supported(led_supported), led_supported);
4024
af116101
HMH
4025 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4026 GFP_KERNEL);
4027 if (!tpacpi_leds) {
4028 printk(TPACPI_ERR "Out of memory for LED data\n");
4029 return -ENOMEM;
4030 }
4031
4032 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4033 tpacpi_leds[i].led = i;
4034
4035 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4036 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4037 if (led_supported == TPACPI_LED_570)
4038 tpacpi_leds[i].led_classdev.brightness_get =
4039 &led_sysfs_get;
4040
4041 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4042
4043 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4044
4045 rc = led_classdev_register(&tpacpi_pdev->dev,
4046 &tpacpi_leds[i].led_classdev);
4047 if (rc < 0) {
4048 tpacpi_leds[i].led_classdev.name = NULL;
4049 led_exit();
4050 return rc;
4051 }
4052 }
4053
5fba344c 4054 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
78f81cc4
BD
4055}
4056
4fa6811b
HMH
4057#define str_led_status(s) \
4058 ((s) == TPACPI_LED_OFF ? "off" : \
4059 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
78f81cc4
BD
4060
4061static int led_read(char *p)
1da177e4
LT
4062{
4063 int len = 0;
4064
78f81cc4
BD
4065 if (!led_supported) {
4066 len += sprintf(p + len, "status:\t\tnot supported\n");
4067 return len;
4068 }
4069 len += sprintf(p + len, "status:\t\tsupported\n");
4070
efa27145 4071 if (led_supported == TPACPI_LED_570) {
78f81cc4
BD
4072 /* 570 */
4073 int i, status;
4074 for (i = 0; i < 8; i++) {
4fa6811b
HMH
4075 status = led_get_status(i);
4076 if (status < 0)
78f81cc4
BD
4077 return -EIO;
4078 len += sprintf(p + len, "%d:\t\t%s\n",
4fa6811b 4079 i, str_led_status(status));
78f81cc4
BD
4080 }
4081 }
4082
1da177e4 4083 len += sprintf(p + len, "commands:\t"
78f81cc4 4084 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1da177e4
LT
4085
4086 return len;
4087}
4088
78f81cc4 4089static int led_write(char *buf)
1da177e4
LT
4090{
4091 char *cmd;
4fa6811b
HMH
4092 int led, rc;
4093 enum led_status_t s;
78f81cc4
BD
4094
4095 if (!led_supported)
4096 return -ENODEV;
1da177e4
LT
4097
4098 while ((cmd = next_cmd(&buf))) {
78f81cc4 4099 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1da177e4
LT
4100 return -EINVAL;
4101
78f81cc4 4102 if (strstr(cmd, "off")) {
4fa6811b 4103 s = TPACPI_LED_OFF;
1da177e4 4104 } else if (strstr(cmd, "on")) {
4fa6811b 4105 s = TPACPI_LED_ON;
78f81cc4 4106 } else if (strstr(cmd, "blink")) {
4fa6811b 4107 s = TPACPI_LED_BLINK;
78f81cc4 4108 } else {
4fa6811b 4109 return -EINVAL;
78f81cc4 4110 }
4fa6811b
HMH
4111
4112 rc = led_set_status(led, s);
4113 if (rc < 0)
4114 return rc;
78f81cc4
BD
4115 }
4116
4117 return 0;
4118}
4119
a5763f22
HMH
4120static struct ibm_struct led_driver_data = {
4121 .name = "led",
4122 .read = led_read,
4123 .write = led_write,
af116101 4124 .exit = led_exit,
a5763f22
HMH
4125};
4126
56b6aeb0
HMH
4127/*************************************************************************
4128 * Beep subdriver
4129 */
4130
e0c7dfe7 4131TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
56b6aeb0 4132
a5763f22 4133static int __init beep_init(struct ibm_init_struct *iibm)
5fba344c 4134{
fe08bc4b
HMH
4135 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4136
e0c7dfe7 4137 TPACPI_ACPIHANDLE_INIT(beep);
5fba344c 4138
fe08bc4b
HMH
4139 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4140 str_supported(beep_handle != NULL));
4141
5fba344c
HMH
4142 return (beep_handle)? 0 : 1;
4143}
4144
78f81cc4
BD
4145static int beep_read(char *p)
4146{
4147 int len = 0;
4148
4149 if (!beep_handle)
4150 len += sprintf(p + len, "status:\t\tnot supported\n");
4151 else {
4152 len += sprintf(p + len, "status:\t\tsupported\n");
4153 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4154 }
4155
4156 return len;
4157}
4158
4159static int beep_write(char *buf)
4160{
4161 char *cmd;
4162 int beep_cmd;
4163
4164 if (!beep_handle)
4165 return -ENODEV;
4166
4167 while ((cmd = next_cmd(&buf))) {
4168 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4169 beep_cmd >= 0 && beep_cmd <= 17) {
4170 /* beep_cmd set */
4171 } else
4172 return -EINVAL;
4173 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4174 return -EIO;
4175 }
4176
4177 return 0;
4178}
4179
a5763f22
HMH
4180static struct ibm_struct beep_driver_data = {
4181 .name = "beep",
4182 .read = beep_read,
4183 .write = beep_write,
4184};
4185
56b6aeb0
HMH
4186/*************************************************************************
4187 * Thermal subdriver
4188 */
78f81cc4 4189
f74a27d4
HMH
4190enum thermal_access_mode {
4191 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4192 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4193 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4194 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4195 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4196};
4197
4198enum { /* TPACPI_THERMAL_TPEC_* */
4199 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4200 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4201 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4202};
4203
4204#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4205struct ibm_thermal_sensors_struct {
4206 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4207};
4208
a26f878a 4209static enum thermal_access_mode thermal_read_mode;
78f81cc4 4210
b21a15f6
HMH
4211/* idx is zero-based */
4212static int thermal_get_sensor(int idx, s32 *value)
4213{
4214 int t;
4215 s8 tmp;
4216 char tmpi[5];
4217
4218 t = TP_EC_THERMAL_TMP0;
4219
4220 switch (thermal_read_mode) {
4221#if TPACPI_MAX_THERMAL_SENSORS >= 16
4222 case TPACPI_THERMAL_TPEC_16:
4223 if (idx >= 8 && idx <= 15) {
4224 t = TP_EC_THERMAL_TMP8;
4225 idx -= 8;
4226 }
4227 /* fallthrough */
4228#endif
4229 case TPACPI_THERMAL_TPEC_8:
4230 if (idx <= 7) {
4231 if (!acpi_ec_read(t + idx, &tmp))
4232 return -EIO;
4233 *value = tmp * 1000;
4234 return 0;
4235 }
4236 break;
4237
4238 case TPACPI_THERMAL_ACPI_UPDT:
4239 if (idx <= 7) {
4240 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4241 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4242 return -EIO;
4243 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4244 return -EIO;
4245 *value = (t - 2732) * 100;
4246 return 0;
4247 }
4248 break;
4249
4250 case TPACPI_THERMAL_ACPI_TMP07:
4251 if (idx <= 7) {
4252 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4253 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4254 return -EIO;
4255 if (t > 127 || t < -127)
4256 t = TP_EC_THERMAL_TMP_NA;
4257 *value = t * 1000;
4258 return 0;
4259 }
4260 break;
4261
4262 case TPACPI_THERMAL_NONE:
4263 default:
4264 return -ENOSYS;
4265 }
4266
4267 return -EINVAL;
4268}
4269
4270static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4271{
4272 int res, i;
4273 int n;
4274
4275 n = 8;
4276 i = 0;
4277
4278 if (!s)
4279 return -EINVAL;
4280
4281 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4282 n = 16;
4283
35ff8b9f 4284 for (i = 0 ; i < n; i++) {
b21a15f6
HMH
4285 res = thermal_get_sensor(i, &s->temp[i]);
4286 if (res)
4287 return res;
4288 }
4289
4290 return n;
4291}
f74a27d4 4292
2c37aa4e
HMH
4293/* sysfs temp##_input -------------------------------------------------- */
4294
4295static ssize_t thermal_temp_input_show(struct device *dev,
4296 struct device_attribute *attr,
4297 char *buf)
4298{
4299 struct sensor_device_attribute *sensor_attr =
4300 to_sensor_dev_attr(attr);
4301 int idx = sensor_attr->index;
4302 s32 value;
4303 int res;
4304
4305 res = thermal_get_sensor(idx, &value);
4306 if (res)
4307 return res;
4308 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4309 return -ENXIO;
4310
4311 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4312}
4313
4314#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
35ff8b9f
HMH
4315 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4316 thermal_temp_input_show, NULL, _idxB)
2c37aa4e
HMH
4317
4318static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4319 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4320 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4321 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4322 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4323 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4324 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4325 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4326 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4327 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4328 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4329 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4330 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4331 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4332 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4333 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4334 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4335};
4336
4337#define THERMAL_ATTRS(X) \
4338 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4339
4340static struct attribute *thermal_temp_input_attr[] = {
4341 THERMAL_ATTRS(8),
4342 THERMAL_ATTRS(9),
4343 THERMAL_ATTRS(10),
4344 THERMAL_ATTRS(11),
4345 THERMAL_ATTRS(12),
4346 THERMAL_ATTRS(13),
4347 THERMAL_ATTRS(14),
4348 THERMAL_ATTRS(15),
4349 THERMAL_ATTRS(0),
4350 THERMAL_ATTRS(1),
4351 THERMAL_ATTRS(2),
4352 THERMAL_ATTRS(3),
4353 THERMAL_ATTRS(4),
4354 THERMAL_ATTRS(5),
4355 THERMAL_ATTRS(6),
4356 THERMAL_ATTRS(7),
4357 NULL
4358};
4359
4360static const struct attribute_group thermal_temp_input16_group = {
4361 .attrs = thermal_temp_input_attr
4362};
4363
4364static const struct attribute_group thermal_temp_input8_group = {
4365 .attrs = &thermal_temp_input_attr[8]
4366};
4367
4368#undef THERMAL_SENSOR_ATTR_TEMP
4369#undef THERMAL_ATTRS
4370
4371/* --------------------------------------------------------------------- */
4372
a5763f22 4373static int __init thermal_init(struct ibm_init_struct *iibm)
78f81cc4 4374{
60eb0b35
HMH
4375 u8 t, ta1, ta2;
4376 int i;
5fba344c 4377 int acpi_tmp7;
2c37aa4e 4378 int res;
5fba344c 4379
fe08bc4b
HMH
4380 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4381
5fba344c 4382 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
60eb0b35 4383
3d6f99ca 4384 if (thinkpad_id.ec_model) {
60eb0b35
HMH
4385 /*
4386 * Direct EC access mode: sensors at registers
4387 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4388 * non-implemented, thermal sensors return 0x80 when
4389 * not available
4390 */
78f81cc4 4391
60eb0b35
HMH
4392 ta1 = ta2 = 0;
4393 for (i = 0; i < 8; i++) {
04cc862c 4394 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
60eb0b35
HMH
4395 ta1 |= t;
4396 } else {
4397 ta1 = 0;
4398 break;
4399 }
04cc862c 4400 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
60eb0b35
HMH
4401 ta2 |= t;
4402 } else {
4403 ta1 = 0;
4404 break;
4405 }
4406 }
4407 if (ta1 == 0) {
4408 /* This is sheer paranoia, but we handle it anyway */
4409 if (acpi_tmp7) {
e0c7dfe7 4410 printk(TPACPI_ERR
60eb0b35 4411 "ThinkPad ACPI EC access misbehaving, "
35ff8b9f
HMH
4412 "falling back to ACPI TMPx access "
4413 "mode\n");
efa27145 4414 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
60eb0b35 4415 } else {
e0c7dfe7 4416 printk(TPACPI_ERR
60eb0b35
HMH
4417 "ThinkPad ACPI EC access misbehaving, "
4418 "disabling thermal sensors access\n");
efa27145 4419 thermal_read_mode = TPACPI_THERMAL_NONE;
60eb0b35
HMH
4420 }
4421 } else {
4422 thermal_read_mode =
4423 (ta2 != 0) ?
efa27145 4424 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
60eb0b35
HMH
4425 }
4426 } else if (acpi_tmp7) {
a26f878a
HMH
4427 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4428 /* 600e/x, 770e, 770x */
efa27145 4429 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
a26f878a
HMH
4430 } else {
4431 /* Standard ACPI TMPx access, max 8 sensors */
efa27145 4432 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
a26f878a
HMH
4433 }
4434 } else {
4435 /* temperatures not supported on 570, G4x, R30, R31, R32 */
efa27145 4436 thermal_read_mode = TPACPI_THERMAL_NONE;
a26f878a 4437 }
78f81cc4 4438
fe08bc4b
HMH
4439 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4440 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4441 thermal_read_mode);
4442
35ff8b9f 4443 switch (thermal_read_mode) {
2c37aa4e 4444 case TPACPI_THERMAL_TPEC_16:
7fd40029 4445 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
4446 &thermal_temp_input16_group);
4447 if (res)
4448 return res;
4449 break;
4450 case TPACPI_THERMAL_TPEC_8:
4451 case TPACPI_THERMAL_ACPI_TMP07:
4452 case TPACPI_THERMAL_ACPI_UPDT:
7fd40029 4453 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
4454 &thermal_temp_input8_group);
4455 if (res)
4456 return res;
4457 break;
4458 case TPACPI_THERMAL_NONE:
4459 default:
4460 return 1;
4461 }
4462
4463 return 0;
4464}
4465
4466static void thermal_exit(void)
4467{
35ff8b9f 4468 switch (thermal_read_mode) {
2c37aa4e 4469 case TPACPI_THERMAL_TPEC_16:
7fd40029 4470 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
4471 &thermal_temp_input16_group);
4472 break;
4473 case TPACPI_THERMAL_TPEC_8:
4474 case TPACPI_THERMAL_ACPI_TMP07:
4475 case TPACPI_THERMAL_ACPI_UPDT:
7fd40029 4476 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
4477 &thermal_temp_input16_group);
4478 break;
4479 case TPACPI_THERMAL_NONE:
4480 default:
4481 break;
4482 }
78f81cc4
BD
4483}
4484
a26f878a
HMH
4485static int thermal_read(char *p)
4486{
4487 int len = 0;
4488 int n, i;
4489 struct ibm_thermal_sensors_struct t;
4490
4491 n = thermal_get_sensors(&t);
4492 if (unlikely(n < 0))
4493 return n;
4494
4495 len += sprintf(p + len, "temperatures:\t");
4496
4497 if (n > 0) {
4498 for (i = 0; i < (n - 1); i++)
4499 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4500 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4501 } else
4502 len += sprintf(p + len, "not supported\n");
78f81cc4
BD
4503
4504 return len;
4505}
4506
a5763f22
HMH
4507static struct ibm_struct thermal_driver_data = {
4508 .name = "thermal",
4509 .read = thermal_read,
2c37aa4e 4510 .exit = thermal_exit,
a5763f22
HMH
4511};
4512
56b6aeb0
HMH
4513/*************************************************************************
4514 * EC Dump subdriver
4515 */
4516
78f81cc4
BD
4517static u8 ecdump_regs[256];
4518
4519static int ecdump_read(char *p)
4520{
4521 int len = 0;
4522 int i, j;
4523 u8 v;
4524
4525 len += sprintf(p + len, "EC "
4526 " +00 +01 +02 +03 +04 +05 +06 +07"
4527 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4528 for (i = 0; i < 256; i += 16) {
4529 len += sprintf(p + len, "EC 0x%02x:", i);
4530 for (j = 0; j < 16; j++) {
4531 if (!acpi_ec_read(i + j, &v))
4532 break;
4533 if (v != ecdump_regs[i + j])
4534 len += sprintf(p + len, " *%02x", v);
4535 else
4536 len += sprintf(p + len, " %02x", v);
4537 ecdump_regs[i + j] = v;
4538 }
4539 len += sprintf(p + len, "\n");
4540 if (j != 16)
4541 break;
4542 }
4543
4544 /* These are way too dangerous to advertise openly... */
4545#if 0
4546 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4547 " (<offset> is 00-ff, <value> is 00-ff)\n");
4548 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4549 " (<offset> is 00-ff, <value> is 0-255)\n");
4550#endif
4551 return len;
4552}
4553
4554static int ecdump_write(char *buf)
4555{
4556 char *cmd;
4557 int i, v;
4558
4559 while ((cmd = next_cmd(&buf))) {
4560 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4561 /* i and v set */
4562 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4563 /* i and v set */
4564 } else
4565 return -EINVAL;
4566 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4567 if (!acpi_ec_write(i, v))
1da177e4
LT
4568 return -EIO;
4569 } else
4570 return -EINVAL;
4571 }
4572
4573 return 0;
78f81cc4
BD
4574}
4575
a5763f22
HMH
4576static struct ibm_struct ecdump_driver_data = {
4577 .name = "ecdump",
4578 .read = ecdump_read,
4579 .write = ecdump_write,
92641177 4580 .flags.experimental = 1,
a5763f22
HMH
4581};
4582
56b6aeb0
HMH
4583/*************************************************************************
4584 * Backlight/brightness subdriver
4585 */
4586
f74a27d4
HMH
4587#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4588
e11aecf1
HMH
4589enum {
4590 TP_EC_BACKLIGHT = 0x31,
4591
4592 /* TP_EC_BACKLIGHT bitmasks */
4593 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4594 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4595 TP_EC_BACKLIGHT_MAPSW = 0x20,
4596};
4597
94954cc6 4598static struct backlight_device *ibm_backlight_device;
f74a27d4
HMH
4599static int brightness_mode;
4600static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4601
b21a15f6 4602static struct mutex brightness_mutex;
56b6aeb0 4603
b21a15f6
HMH
4604/*
4605 * ThinkPads can read brightness from two places: EC 0x31, or
4606 * CMOS NVRAM byte 0x5E, bits 0-3.
e11aecf1
HMH
4607 *
4608 * EC 0x31 has the following layout
4609 * Bit 7: unknown function
4610 * Bit 6: unknown function
4611 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4612 * Bit 4: must be set to zero to avoid problems
4613 * Bit 3-0: backlight brightness level
4614 *
4615 * brightness_get_raw returns status data in the EC 0x31 layout
b21a15f6 4616 */
e11aecf1 4617static int brightness_get_raw(int *status)
b21a15f6
HMH
4618{
4619 u8 lec = 0, lcmos = 0, level = 0;
4620
4621 if (brightness_mode & 1) {
e11aecf1 4622 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
b21a15f6 4623 return -EIO;
e11aecf1 4624 level = lec & TP_EC_BACKLIGHT_LVLMSK;
b21a15f6
HMH
4625 };
4626 if (brightness_mode & 2) {
4627 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4628 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4629 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4630 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4631 level = lcmos;
4632 }
4633
2d5e94d7 4634 if (brightness_mode == 3) {
e11aecf1
HMH
4635 *status = lec; /* Prefer EC, CMOS is just a backing store */
4636 lec &= TP_EC_BACKLIGHT_LVLMSK;
2d5e94d7
HMH
4637 if (lec == lcmos)
4638 tp_warned.bright_cmos_ec_unsync = 0;
4639 else {
4640 if (!tp_warned.bright_cmos_ec_unsync) {
4641 printk(TPACPI_ERR
4642 "CMOS NVRAM (%u) and EC (%u) do not "
4643 "agree on display brightness level\n",
4644 (unsigned int) lcmos,
4645 (unsigned int) lec);
4646 tp_warned.bright_cmos_ec_unsync = 1;
4647 }
4648 return -EIO;
4649 }
e11aecf1
HMH
4650 } else {
4651 *status = level;
b21a15f6
HMH
4652 }
4653
e11aecf1 4654 return 0;
b21a15f6
HMH
4655}
4656
4657/* May return EINTR which can always be mapped to ERESTARTSYS */
4658static int brightness_set(int value)
4659{
4660 int cmos_cmd, inc, i, res;
4661 int current_value;
e11aecf1 4662 int command_bits;
b21a15f6 4663
e11aecf1
HMH
4664 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4665 value < 0)
b21a15f6
HMH
4666 return -EINVAL;
4667
4668 res = mutex_lock_interruptible(&brightness_mutex);
4669 if (res < 0)
4670 return res;
4671
e11aecf1
HMH
4672 res = brightness_get_raw(&current_value);
4673 if (res < 0)
b21a15f6 4674 goto errout;
e11aecf1
HMH
4675
4676 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4677 current_value &= TP_EC_BACKLIGHT_LVLMSK;
b21a15f6
HMH
4678
4679 cmos_cmd = value > current_value ?
4680 TP_CMOS_BRIGHTNESS_UP :
4681 TP_CMOS_BRIGHTNESS_DOWN;
4682 inc = (value > current_value)? 1 : -1;
4683
4684 res = 0;
4685 for (i = current_value; i != value; i += inc) {
4686 if ((brightness_mode & 2) &&
4687 issue_thinkpad_cmos_command(cmos_cmd)) {
4688 res = -EIO;
4689 goto errout;
4690 }
4691 if ((brightness_mode & 1) &&
e11aecf1
HMH
4692 !acpi_ec_write(TP_EC_BACKLIGHT,
4693 (i + inc) | command_bits)) {
b21a15f6
HMH
4694 res = -EIO;
4695 goto errout;;
4696 }
4697 }
4698
4699errout:
4700 mutex_unlock(&brightness_mutex);
4701 return res;
4702}
4703
4704/* sysfs backlight class ----------------------------------------------- */
4705
4706static int brightness_update_status(struct backlight_device *bd)
4707{
4708 /* it is the backlight class's job (caller) to handle
4709 * EINTR and other errors properly */
4710 return brightness_set(
4711 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4712 bd->props.power == FB_BLANK_UNBLANK) ?
4713 bd->props.brightness : 0);
4714}
4715
e11aecf1 4716static int brightness_get(struct backlight_device *bd)
a3f104c0 4717{
e11aecf1 4718 int status, res;
a3f104c0 4719
e11aecf1
HMH
4720 res = brightness_get_raw(&status);
4721 if (res < 0)
4722 return 0; /* FIXME: teach backlight about error handling */
e11e211a 4723
e11aecf1 4724 return status & TP_EC_BACKLIGHT_LVLMSK;
e11e211a
HMH
4725}
4726
b21a15f6 4727static struct backlight_ops ibm_backlight_data = {
35ff8b9f
HMH
4728 .get_brightness = brightness_get,
4729 .update_status = brightness_update_status,
56b6aeb0 4730};
e11e211a 4731
b21a15f6 4732/* --------------------------------------------------------------------- */
e11e211a 4733
a5763f22 4734static int __init brightness_init(struct ibm_init_struct *iibm)
56b6aeb0
HMH
4735{
4736 int b;
4737
fe08bc4b
HMH
4738 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4739
f432255e
HMH
4740 mutex_init(&brightness_mutex);
4741
b5972796
HMH
4742 /*
4743 * We always attempt to detect acpi support, so as to switch
4744 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4745 * going to publish a backlight interface
4746 */
4747 b = tpacpi_check_std_acpi_brightness_support();
4748 if (b > 0) {
4749 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4750 printk(TPACPI_NOTICE
4751 "Lenovo BIOS switched to ACPI backlight "
4752 "control mode\n");
4753 }
4754 if (brightness_enable > 1) {
e0c7dfe7 4755 printk(TPACPI_NOTICE
35ff8b9f
HMH
4756 "standard ACPI backlight interface "
4757 "available, not loading native one...\n");
e11e211a
HMH
4758 return 1;
4759 }
87cc537a
HMH
4760 }
4761
b5972796
HMH
4762 if (!brightness_enable) {
4763 dbg_printk(TPACPI_DBG_INIT,
4764 "brightness support disabled by "
4765 "module parameter\n");
4766 return 1;
4767 }
4768
4769 if (b > 16) {
4770 printk(TPACPI_ERR
4771 "Unsupported brightness interface, "
4772 "please contact %s\n", TPACPI_MAIL);
4773 return 1;
4774 }
4775 if (b == 16)
4776 tp_features.bright_16levels = 1;
4777
24d3b774
HMH
4778 if (!brightness_mode) {
4779 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4780 brightness_mode = 2;
4781 else
4782 brightness_mode = 3;
4783
4784 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4785 brightness_mode);
4786 }
4787
4788 if (brightness_mode > 3)
4789 return -EINVAL;
4790
e11aecf1 4791 if (brightness_get_raw(&b) < 0)
24d3b774 4792 return 1;
56b6aeb0 4793
a3f104c0 4794 if (tp_features.bright_16levels)
35ff8b9f
HMH
4795 printk(TPACPI_INFO
4796 "detected a 16-level brightness capable ThinkPad\n");
a3f104c0 4797
7d5a015e
HMH
4798 ibm_backlight_device = backlight_device_register(
4799 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4800 &ibm_backlight_data);
56b6aeb0 4801 if (IS_ERR(ibm_backlight_device)) {
e0c7dfe7 4802 printk(TPACPI_ERR "Could not register backlight device\n");
56b6aeb0
HMH
4803 return PTR_ERR(ibm_backlight_device);
4804 }
fe08bc4b 4805 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
56b6aeb0 4806
a3f104c0
HMH
4807 ibm_backlight_device->props.max_brightness =
4808 (tp_features.bright_16levels)? 15 : 7;
e11aecf1 4809 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
56b6aeb0
HMH
4810 backlight_update_status(ibm_backlight_device);
4811
4812 return 0;
4813}
4814
4815static void brightness_exit(void)
4816{
4817 if (ibm_backlight_device) {
fe08bc4b
HMH
4818 vdbg_printk(TPACPI_DBG_EXIT,
4819 "calling backlight_device_unregister()\n");
56b6aeb0 4820 backlight_device_unregister(ibm_backlight_device);
56b6aeb0
HMH
4821 }
4822}
4823
56b6aeb0
HMH
4824static int brightness_read(char *p)
4825{
4826 int len = 0;
4827 int level;
4828
35ff8b9f
HMH
4829 level = brightness_get(NULL);
4830 if (level < 0) {
56b6aeb0
HMH
4831 len += sprintf(p + len, "level:\t\tunreadable\n");
4832 } else {
a3f104c0 4833 len += sprintf(p + len, "level:\t\t%d\n", level);
56b6aeb0
HMH
4834 len += sprintf(p + len, "commands:\tup, down\n");
4835 len += sprintf(p + len, "commands:\tlevel <level>"
a3f104c0
HMH
4836 " (<level> is 0-%d)\n",
4837 (tp_features.bright_16levels) ? 15 : 7);
56b6aeb0
HMH
4838 }
4839
4840 return len;
4841}
4842
8acb0250
HM
4843static int brightness_write(char *buf)
4844{
4845 int level;
4273af8d 4846 int rc;
1da177e4 4847 char *cmd;
a3f104c0 4848 int max_level = (tp_features.bright_16levels) ? 15 : 7;
1da177e4 4849
4273af8d
HMH
4850 level = brightness_get(NULL);
4851 if (level < 0)
4852 return level;
78f81cc4 4853
4273af8d 4854 while ((cmd = next_cmd(&buf))) {
78f81cc4 4855 if (strlencmp(cmd, "up") == 0) {
4273af8d
HMH
4856 if (level < max_level)
4857 level++;
78f81cc4 4858 } else if (strlencmp(cmd, "down") == 0) {
4273af8d
HMH
4859 if (level > 0)
4860 level--;
4861 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4862 level >= 0 && level <= max_level) {
4863 /* new level set */
78f81cc4
BD
4864 } else
4865 return -EINVAL;
78f81cc4
BD
4866 }
4867
4273af8d
HMH
4868 /*
4869 * Now we know what the final level should be, so we try to set it.
4870 * Doing it this way makes the syscall restartable in case of EINTR
4871 */
4872 rc = brightness_set(level);
4873 return (rc == -EINTR)? ERESTARTSYS : rc;
78f81cc4
BD
4874}
4875
a5763f22
HMH
4876static struct ibm_struct brightness_driver_data = {
4877 .name = "brightness",
4878 .read = brightness_read,
4879 .write = brightness_write,
4880 .exit = brightness_exit,
4881};
4882
56b6aeb0
HMH
4883/*************************************************************************
4884 * Volume subdriver
4885 */
fb87a811 4886
f74a27d4
HMH
4887static int volume_offset = 0x30;
4888
78f81cc4
BD
4889static int volume_read(char *p)
4890{
4891 int len = 0;
4892 u8 level;
4893
4894 if (!acpi_ec_read(volume_offset, &level)) {
4895 len += sprintf(p + len, "level:\t\tunreadable\n");
4896 } else {
4897 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4898 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4899 len += sprintf(p + len, "commands:\tup, down, mute\n");
4900 len += sprintf(p + len, "commands:\tlevel <level>"
4901 " (<level> is 0-15)\n");
4902 }
4903
4904 return len;
4905}
4906
78f81cc4
BD
4907static int volume_write(char *buf)
4908{
4909 int cmos_cmd, inc, i;
4910 u8 level, mute;
4911 int new_level, new_mute;
4912 char *cmd;
4913
4914 while ((cmd = next_cmd(&buf))) {
4915 if (!acpi_ec_read(volume_offset, &level))
4916 return -EIO;
4917 new_mute = mute = level & 0x40;
4918 new_level = level = level & 0xf;
4919
4920 if (strlencmp(cmd, "up") == 0) {
4921 if (mute)
4922 new_mute = 0;
4923 else
4924 new_level = level == 15 ? 15 : level + 1;
4925 } else if (strlencmp(cmd, "down") == 0) {
4926 if (mute)
4927 new_mute = 0;
4928 else
4929 new_level = level == 0 ? 0 : level - 1;
4930 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4931 new_level >= 0 && new_level <= 15) {
4932 /* new_level set */
4933 } else if (strlencmp(cmd, "mute") == 0) {
4934 new_mute = 0x40;
1da177e4
LT
4935 } else
4936 return -EINVAL;
4937
35ff8b9f
HMH
4938 if (new_level != level) {
4939 /* mute doesn't change */
4940
4941 cmos_cmd = (new_level > level) ?
4942 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
78f81cc4
BD
4943 inc = new_level > level ? 1 : -1;
4944
c9bea99c 4945 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
78f81cc4
BD
4946 !acpi_ec_write(volume_offset, level)))
4947 return -EIO;
4948
4949 for (i = level; i != new_level; i += inc)
c9bea99c 4950 if (issue_thinkpad_cmos_command(cmos_cmd) ||
78f81cc4
BD
4951 !acpi_ec_write(volume_offset, i + inc))
4952 return -EIO;
4953
35ff8b9f
HMH
4954 if (mute &&
4955 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4956 !acpi_ec_write(volume_offset, new_level + mute))) {
78f81cc4 4957 return -EIO;
35ff8b9f 4958 }
78f81cc4
BD
4959 }
4960
35ff8b9f
HMH
4961 if (new_mute != mute) {
4962 /* level doesn't change */
4963
4964 cmos_cmd = (new_mute) ?
4965 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
78f81cc4 4966
c9bea99c 4967 if (issue_thinkpad_cmos_command(cmos_cmd) ||
78f81cc4
BD
4968 !acpi_ec_write(volume_offset, level + new_mute))
4969 return -EIO;
4970 }
4971 }
4972
4973 return 0;
4974}
4975
a5763f22
HMH
4976static struct ibm_struct volume_driver_data = {
4977 .name = "volume",
4978 .read = volume_read,
4979 .write = volume_write,
4980};
56b6aeb0
HMH
4981
4982/*************************************************************************
4983 * Fan subdriver
4984 */
4985
4986/*
4987 * FAN ACCESS MODES
4988 *
efa27145 4989 * TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0
HMH
4990 * ACPI GFAN method: returns fan level
4991 *
efa27145 4992 * see TPACPI_FAN_WR_ACPI_SFAN
f51d1a39 4993 * EC 0x2f (HFSP) not available if GFAN exists
56b6aeb0 4994 *
efa27145 4995 * TPACPI_FAN_WR_ACPI_SFAN:
56b6aeb0
HMH
4996 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4997 *
f51d1a39
HMH
4998 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4999 * it for writing.
56b6aeb0 5000 *
efa27145 5001 * TPACPI_FAN_WR_TPEC:
f51d1a39
HMH
5002 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5003 * Supported on almost all ThinkPads
56b6aeb0
HMH
5004 *
5005 * Fan speed changes of any sort (including those caused by the
5006 * disengaged mode) are usually done slowly by the firmware as the
5007 * maximum ammount of fan duty cycle change per second seems to be
5008 * limited.
5009 *
5010 * Reading is not available if GFAN exists.
5011 * Writing is not available if SFAN exists.
5012 *
5013 * Bits
5014 * 7 automatic mode engaged;
5015 * (default operation mode of the ThinkPad)
5016 * fan level is ignored in this mode.
f51d1a39 5017 * 6 full speed mode (takes precedence over bit 7);
56b6aeb0 5018 * not available on all thinkpads. May disable
f51d1a39
HMH
5019 * the tachometer while the fan controller ramps up
5020 * the speed (which can take up to a few *minutes*).
5021 * Speeds up fan to 100% duty-cycle, which is far above
5022 * the standard RPM levels. It is not impossible that
5023 * it could cause hardware damage.
56b6aeb0
HMH
5024 * 5-3 unused in some models. Extra bits for fan level
5025 * in others, but still useless as all values above
5026 * 7 map to the same speed as level 7 in these models.
5027 * 2-0 fan level (0..7 usually)
5028 * 0x00 = stop
5029 * 0x07 = max (set when temperatures critical)
5030 * Some ThinkPads may have other levels, see
efa27145 5031 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
56b6aeb0
HMH
5032 *
5033 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5034 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5035 * does so, its initial value is meaningless (0x07).
5036 *
5037 * For firmware bugs, refer to:
5038 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5039 *
5040 * ----
5041 *
5042 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5043 * Main fan tachometer reading (in RPM)
5044 *
5045 * This register is present on all ThinkPads with a new-style EC, and
5046 * it is known not to be present on the A21m/e, and T22, as there is
5047 * something else in offset 0x84 according to the ACPI DSDT. Other
5048 * ThinkPads from this same time period (and earlier) probably lack the
5049 * tachometer as well.
5050 *
5051 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5052 * was never fixed by IBM to report the EC firmware version string
5053 * probably support the tachometer (like the early X models), so
5054 * detecting it is quite hard. We need more data to know for sure.
5055 *
5056 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5057 * might result.
5058 *
f51d1a39
HMH
5059 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5060 * mode.
56b6aeb0
HMH
5061 *
5062 * For firmware bugs, refer to:
5063 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5064 *
efa27145 5065 * TPACPI_FAN_WR_ACPI_FANS:
56b6aeb0
HMH
5066 * ThinkPad X31, X40, X41. Not available in the X60.
5067 *
5068 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5069 * high speed. ACPI DSDT seems to map these three speeds to levels
5070 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5071 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5072 *
5073 * The speeds are stored on handles
5074 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5075 *
5076 * There are three default speed sets, acessible as handles:
5077 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5078 *
5079 * ACPI DSDT switches which set is in use depending on various
5080 * factors.
5081 *
efa27145 5082 * TPACPI_FAN_WR_TPEC is also available and should be used to
56b6aeb0
HMH
5083 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5084 * but the ACPI tables just mention level 7.
5085 */
5086
f74a27d4
HMH
5087enum { /* Fan control constants */
5088 fan_status_offset = 0x2f, /* EC register 0x2f */
5089 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5090 * 0x84 must be read before 0x85 */
5091
5092 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5093 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5094
5095 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5096};
5097
5098enum fan_status_access_mode {
5099 TPACPI_FAN_NONE = 0, /* No fan status or control */
5100 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5101 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5102};
5103
5104enum fan_control_access_mode {
5105 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5106 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5107 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5108 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5109};
5110
5111enum fan_control_commands {
5112 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5113 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5114 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5115 * and also watchdog cmd */
5116};
5117
5118static int fan_control_allowed;
5119
69ba91cb
HMH
5120static enum fan_status_access_mode fan_status_access_mode;
5121static enum fan_control_access_mode fan_control_access_mode;
5122static enum fan_control_commands fan_control_commands;
78f81cc4 5123
778b4d74 5124static u8 fan_control_initial_status;
fe98a52c 5125static u8 fan_control_desired_level;
f74a27d4
HMH
5126static int fan_watchdog_maxinterval;
5127
5128static struct mutex fan_mutex;
778b4d74 5129
25c68a33 5130static void fan_watchdog_fire(struct work_struct *ignored);
25c68a33 5131static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
16663a87 5132
e0c7dfe7
HMH
5133TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5134TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
56b6aeb0
HMH
5135 "\\FSPD", /* 600e/x, 770e, 770x */
5136 ); /* all others */
e0c7dfe7 5137TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
56b6aeb0
HMH
5138 "JFNS", /* 770x-JL */
5139 ); /* all others */
5140
fe98a52c 5141/*
b21a15f6 5142 * Call with fan_mutex held
fe98a52c 5143 */
b21a15f6 5144static void fan_update_desired_level(u8 status)
fe98a52c 5145{
b21a15f6
HMH
5146 if ((status &
5147 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5148 if (status > 7)
5149 fan_control_desired_level = 7;
5150 else
5151 fan_control_desired_level = status;
5152 }
5153}
fe98a52c 5154
b21a15f6
HMH
5155static int fan_get_status(u8 *status)
5156{
5157 u8 s;
fe98a52c 5158
b21a15f6
HMH
5159 /* TODO:
5160 * Add TPACPI_FAN_RD_ACPI_FANS ? */
fe98a52c 5161
b21a15f6
HMH
5162 switch (fan_status_access_mode) {
5163 case TPACPI_FAN_RD_ACPI_GFAN:
5164 /* 570, 600e/x, 770e, 770x */
fe98a52c 5165
b21a15f6
HMH
5166 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5167 return -EIO;
fe98a52c 5168
b21a15f6
HMH
5169 if (likely(status))
5170 *status = s & 0x07;
fe98a52c 5171
b21a15f6
HMH
5172 break;
5173
5174 case TPACPI_FAN_RD_TPEC:
5175 /* all except 570, 600e/x, 770e, 770x */
5176 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5177 return -EIO;
5178
5179 if (likely(status))
5180 *status = s;
fe98a52c 5181
fe98a52c 5182 break;
b21a15f6 5183
fe98a52c 5184 default:
b21a15f6 5185 return -ENXIO;
fe98a52c
HMH
5186 }
5187
b21a15f6
HMH
5188 return 0;
5189}
fe98a52c 5190
b21a15f6
HMH
5191static int fan_get_status_safe(u8 *status)
5192{
5193 int rc;
5194 u8 s;
fe98a52c 5195
b21a15f6
HMH
5196 if (mutex_lock_interruptible(&fan_mutex))
5197 return -ERESTARTSYS;
5198 rc = fan_get_status(&s);
5199 if (!rc)
5200 fan_update_desired_level(s);
5201 mutex_unlock(&fan_mutex);
fe98a52c 5202
b21a15f6
HMH
5203 if (status)
5204 *status = s;
fe98a52c 5205
b21a15f6
HMH
5206 return rc;
5207}
5208
5209static int fan_get_speed(unsigned int *speed)
fe98a52c 5210{
b21a15f6 5211 u8 hi, lo;
fe98a52c 5212
b21a15f6
HMH
5213 switch (fan_status_access_mode) {
5214 case TPACPI_FAN_RD_TPEC:
5215 /* all except 570, 600e/x, 770e, 770x */
5216 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5217 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5218 return -EIO;
fe98a52c 5219
b21a15f6
HMH
5220 if (likely(speed))
5221 *speed = (hi << 8) | lo;
fe98a52c 5222
b21a15f6 5223 break;
fe98a52c 5224
b21a15f6
HMH
5225 default:
5226 return -ENXIO;
5227 }
fe98a52c 5228
b21a15f6 5229 return 0;
fe98a52c
HMH
5230}
5231
b21a15f6 5232static int fan_set_level(int level)
fe98a52c 5233{
b21a15f6
HMH
5234 if (!fan_control_allowed)
5235 return -EPERM;
fe98a52c 5236
b21a15f6
HMH
5237 switch (fan_control_access_mode) {
5238 case TPACPI_FAN_WR_ACPI_SFAN:
5239 if (level >= 0 && level <= 7) {
5240 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5241 return -EIO;
5242 } else
5243 return -EINVAL;
5244 break;
fe98a52c 5245
b21a15f6
HMH
5246 case TPACPI_FAN_WR_ACPI_FANS:
5247 case TPACPI_FAN_WR_TPEC:
5248 if ((level != TP_EC_FAN_AUTO) &&
5249 (level != TP_EC_FAN_FULLSPEED) &&
5250 ((level < 0) || (level > 7)))
5251 return -EINVAL;
fe98a52c 5252
b21a15f6
HMH
5253 /* safety net should the EC not support AUTO
5254 * or FULLSPEED mode bits and just ignore them */
5255 if (level & TP_EC_FAN_FULLSPEED)
5256 level |= 7; /* safety min speed 7 */
547266e4 5257 else if (level & TP_EC_FAN_AUTO)
b21a15f6 5258 level |= 4; /* safety min speed 4 */
fe98a52c 5259
b21a15f6
HMH
5260 if (!acpi_ec_write(fan_status_offset, level))
5261 return -EIO;
5262 else
5263 tp_features.fan_ctrl_status_undef = 0;
5264 break;
fe98a52c 5265
b21a15f6
HMH
5266 default:
5267 return -ENXIO;
5268 }
5269 return 0;
fe98a52c
HMH
5270}
5271
b21a15f6 5272static int fan_set_level_safe(int level)
fe98a52c 5273{
b21a15f6 5274 int rc;
fe98a52c 5275
b21a15f6
HMH
5276 if (!fan_control_allowed)
5277 return -EPERM;
fe98a52c 5278
b21a15f6
HMH
5279 if (mutex_lock_interruptible(&fan_mutex))
5280 return -ERESTARTSYS;
fe98a52c 5281
b21a15f6
HMH
5282 if (level == TPACPI_FAN_LAST_LEVEL)
5283 level = fan_control_desired_level;
fe98a52c 5284
b21a15f6
HMH
5285 rc = fan_set_level(level);
5286 if (!rc)
5287 fan_update_desired_level(level);
5288
5289 mutex_unlock(&fan_mutex);
5290 return rc;
fe98a52c
HMH
5291}
5292
b21a15f6 5293static int fan_set_enable(void)
fe98a52c 5294{
b21a15f6
HMH
5295 u8 s;
5296 int rc;
fe98a52c 5297
ecf2a80a
HMH
5298 if (!fan_control_allowed)
5299 return -EPERM;
5300
b21a15f6
HMH
5301 if (mutex_lock_interruptible(&fan_mutex))
5302 return -ERESTARTSYS;
fe98a52c 5303
b21a15f6
HMH
5304 switch (fan_control_access_mode) {
5305 case TPACPI_FAN_WR_ACPI_FANS:
5306 case TPACPI_FAN_WR_TPEC:
5307 rc = fan_get_status(&s);
5308 if (rc < 0)
5309 break;
fe98a52c 5310
b21a15f6
HMH
5311 /* Don't go out of emergency fan mode */
5312 if (s != 7) {
5313 s &= 0x07;
5314 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5315 }
fe98a52c 5316
b21a15f6
HMH
5317 if (!acpi_ec_write(fan_status_offset, s))
5318 rc = -EIO;
5319 else {
5320 tp_features.fan_ctrl_status_undef = 0;
5321 rc = 0;
5322 }
5323 break;
fe98a52c 5324
b21a15f6
HMH
5325 case TPACPI_FAN_WR_ACPI_SFAN:
5326 rc = fan_get_status(&s);
5327 if (rc < 0)
5328 break;
fe98a52c 5329
b21a15f6 5330 s &= 0x07;
fe98a52c 5331
b21a15f6
HMH
5332 /* Set fan to at least level 4 */
5333 s |= 4;
fe08bc4b 5334
b21a15f6 5335 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
35ff8b9f 5336 rc = -EIO;
b21a15f6
HMH
5337 else
5338 rc = 0;
5339 break;
78f81cc4 5340
b21a15f6
HMH
5341 default:
5342 rc = -ENXIO;
5343 }
5fba344c 5344
b21a15f6
HMH
5345 mutex_unlock(&fan_mutex);
5346 return rc;
69ba91cb 5347}
78f81cc4 5348
b21a15f6 5349static int fan_set_disable(void)
78f81cc4 5350{
b21a15f6 5351 int rc;
c52f0aa5 5352
b21a15f6
HMH
5353 if (!fan_control_allowed)
5354 return -EPERM;
78f81cc4 5355
b21a15f6
HMH
5356 if (mutex_lock_interruptible(&fan_mutex))
5357 return -ERESTARTSYS;
3ef8a609 5358
b21a15f6
HMH
5359 rc = 0;
5360 switch (fan_control_access_mode) {
5361 case TPACPI_FAN_WR_ACPI_FANS:
5362 case TPACPI_FAN_WR_TPEC:
5363 if (!acpi_ec_write(fan_status_offset, 0x00))
5364 rc = -EIO;
5365 else {
5366 fan_control_desired_level = 0;
5367 tp_features.fan_ctrl_status_undef = 0;
5368 }
3ef8a609
HMH
5369 break;
5370
b21a15f6
HMH
5371 case TPACPI_FAN_WR_ACPI_SFAN:
5372 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5373 rc = -EIO;
5374 else
5375 fan_control_desired_level = 0;
c52f0aa5
HMH
5376 break;
5377
5378 default:
b21a15f6 5379 rc = -ENXIO;
78f81cc4
BD
5380 }
5381
fe98a52c 5382
fe98a52c 5383 mutex_unlock(&fan_mutex);
fe98a52c
HMH
5384 return rc;
5385}
5386
b21a15f6 5387static int fan_set_speed(int speed)
c52f0aa5 5388{
b21a15f6 5389 int rc;
c52f0aa5 5390
b21a15f6
HMH
5391 if (!fan_control_allowed)
5392 return -EPERM;
3ef8a609 5393
b21a15f6
HMH
5394 if (mutex_lock_interruptible(&fan_mutex))
5395 return -ERESTARTSYS;
c52f0aa5 5396
b21a15f6
HMH
5397 rc = 0;
5398 switch (fan_control_access_mode) {
5399 case TPACPI_FAN_WR_ACPI_FANS:
5400 if (speed >= 0 && speed <= 65535) {
5401 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5402 speed, speed, speed))
5403 rc = -EIO;
5404 } else
5405 rc = -EINVAL;
c52f0aa5
HMH
5406 break;
5407
5408 default:
b21a15f6 5409 rc = -ENXIO;
c52f0aa5
HMH
5410 }
5411
b21a15f6
HMH
5412 mutex_unlock(&fan_mutex);
5413 return rc;
16663a87
HMH
5414}
5415
5416static void fan_watchdog_reset(void)
5417{
94954cc6 5418 static int fan_watchdog_active;
16663a87 5419
4985cd0a
HMH
5420 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5421 return;
5422
16663a87
HMH
5423 if (fan_watchdog_active)
5424 cancel_delayed_work(&fan_watchdog_task);
5425
8fef502e
HMH
5426 if (fan_watchdog_maxinterval > 0 &&
5427 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
16663a87 5428 fan_watchdog_active = 1;
e0e3c061 5429 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
16663a87
HMH
5430 msecs_to_jiffies(fan_watchdog_maxinterval
5431 * 1000))) {
35ff8b9f 5432 printk(TPACPI_ERR
e0e3c061 5433 "failed to queue the fan watchdog, "
16663a87
HMH
5434 "watchdog will not trigger\n");
5435 }
5436 } else
5437 fan_watchdog_active = 0;
5438}
5439
b21a15f6 5440static void fan_watchdog_fire(struct work_struct *ignored)
78f81cc4 5441{
b21a15f6 5442 int rc;
18ad7996 5443
b21a15f6
HMH
5444 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5445 return;
a12095c2 5446
e0c7dfe7 5447 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
b21a15f6
HMH
5448 rc = fan_set_enable();
5449 if (rc < 0) {
e0c7dfe7 5450 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
b21a15f6
HMH
5451 "will try again later...\n", -rc);
5452 /* reschedule for later */
5453 fan_watchdog_reset();
5454 }
5455}
eaa7571b 5456
b21a15f6
HMH
5457/*
5458 * SYSFS fan layout: hwmon compatible (device)
5459 *
5460 * pwm*_enable:
5461 * 0: "disengaged" mode
5462 * 1: manual mode
5463 * 2: native EC "auto" mode (recommended, hardware default)
5464 *
5465 * pwm*: set speed in manual mode, ignored otherwise.
5466 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5467 * interpolation.
5468 *
5469 * fan*_input: tachometer reading, RPM
5470 *
5471 *
5472 * SYSFS fan layout: extensions
5473 *
5474 * fan_watchdog (driver):
5475 * fan watchdog interval in seconds, 0 disables (default), max 120
5476 */
5477
5478/* sysfs fan pwm1_enable ----------------------------------------------- */
5479static ssize_t fan_pwm1_enable_show(struct device *dev,
5480 struct device_attribute *attr,
5481 char *buf)
5482{
5483 int res, mode;
5484 u8 status;
5485
5486 res = fan_get_status_safe(&status);
5487 if (res)
5488 return res;
5489
5490 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5491 if (status != fan_control_initial_status) {
d8fd94d9 5492 tp_features.fan_ctrl_status_undef = 0;
b21a15f6
HMH
5493 } else {
5494 /* Return most likely status. In fact, it
5495 * might be the only possible status */
5496 status = TP_EC_FAN_AUTO;
5497 }
5498 }
5499
5500 if (status & TP_EC_FAN_FULLSPEED) {
5501 mode = 0;
5502 } else if (status & TP_EC_FAN_AUTO) {
5503 mode = 2;
5504 } else
5505 mode = 1;
5506
5507 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5508}
a12095c2 5509
b21a15f6
HMH
5510static ssize_t fan_pwm1_enable_store(struct device *dev,
5511 struct device_attribute *attr,
5512 const char *buf, size_t count)
5513{
5514 unsigned long t;
5515 int res, level;
5516
5517 if (parse_strtoul(buf, 2, &t))
5518 return -EINVAL;
5519
5520 switch (t) {
5521 case 0:
5522 level = TP_EC_FAN_FULLSPEED;
5523 break;
5524 case 1:
5525 level = TPACPI_FAN_LAST_LEVEL;
5526 break;
5527 case 2:
5528 level = TP_EC_FAN_AUTO;
5529 break;
5530 case 3:
5531 /* reserved for software-controlled auto mode */
5532 return -ENOSYS;
18ad7996 5533 default:
b21a15f6 5534 return -EINVAL;
18ad7996 5535 }
b21a15f6
HMH
5536
5537 res = fan_set_level_safe(level);
5538 if (res == -ENXIO)
5539 return -EINVAL;
5540 else if (res < 0)
5541 return res;
5542
5543 fan_watchdog_reset();
5544
5545 return count;
18ad7996
HMH
5546}
5547
b21a15f6
HMH
5548static struct device_attribute dev_attr_fan_pwm1_enable =
5549 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5550 fan_pwm1_enable_show, fan_pwm1_enable_store);
5551
5552/* sysfs fan pwm1 ------------------------------------------------------ */
5553static ssize_t fan_pwm1_show(struct device *dev,
5554 struct device_attribute *attr,
5555 char *buf)
fe98a52c 5556{
b21a15f6
HMH
5557 int res;
5558 u8 status;
fe98a52c 5559
b21a15f6
HMH
5560 res = fan_get_status_safe(&status);
5561 if (res)
5562 return res;
ecf2a80a 5563
b21a15f6
HMH
5564 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5565 if (status != fan_control_initial_status) {
5566 tp_features.fan_ctrl_status_undef = 0;
5567 } else {
5568 status = TP_EC_FAN_AUTO;
5569 }
5570 }
fe98a52c 5571
b21a15f6
HMH
5572 if ((status &
5573 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5574 status = fan_control_desired_level;
fe98a52c 5575
b21a15f6
HMH
5576 if (status > 7)
5577 status = 7;
fe98a52c 5578
b21a15f6 5579 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
fe98a52c
HMH
5580}
5581
b21a15f6
HMH
5582static ssize_t fan_pwm1_store(struct device *dev,
5583 struct device_attribute *attr,
5584 const char *buf, size_t count)
18ad7996 5585{
b21a15f6 5586 unsigned long s;
1c6a334e 5587 int rc;
b21a15f6 5588 u8 status, newlevel;
1c6a334e 5589
b21a15f6
HMH
5590 if (parse_strtoul(buf, 255, &s))
5591 return -EINVAL;
5592
5593 /* scale down from 0-255 to 0-7 */
5594 newlevel = (s >> 5) & 0x07;
ecf2a80a 5595
fc589a3c
HMH
5596 if (mutex_lock_interruptible(&fan_mutex))
5597 return -ERESTARTSYS;
40ca9fdf 5598
b21a15f6
HMH
5599 rc = fan_get_status(&status);
5600 if (!rc && (status &
5601 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5602 rc = fan_set_level(newlevel);
5603 if (rc == -ENXIO)
5604 rc = -EINVAL;
5605 else if (!rc) {
5606 fan_update_desired_level(newlevel);
5607 fan_watchdog_reset();
eaa7571b 5608 }
b21a15f6 5609 }
1c6a334e 5610
b21a15f6
HMH
5611 mutex_unlock(&fan_mutex);
5612 return (rc)? rc : count;
5613}
1c6a334e 5614
b21a15f6
HMH
5615static struct device_attribute dev_attr_fan_pwm1 =
5616 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5617 fan_pwm1_show, fan_pwm1_store);
1c6a334e 5618
b21a15f6
HMH
5619/* sysfs fan fan1_input ------------------------------------------------ */
5620static ssize_t fan_fan1_input_show(struct device *dev,
5621 struct device_attribute *attr,
5622 char *buf)
5623{
5624 int res;
5625 unsigned int speed;
1c6a334e 5626
b21a15f6
HMH
5627 res = fan_get_speed(&speed);
5628 if (res < 0)
5629 return res;
1c6a334e 5630
b21a15f6
HMH
5631 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5632}
18ad7996 5633
b21a15f6
HMH
5634static struct device_attribute dev_attr_fan_fan1_input =
5635 __ATTR(fan1_input, S_IRUGO,
5636 fan_fan1_input_show, NULL);
40ca9fdf 5637
b21a15f6
HMH
5638/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5639static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5640 char *buf)
5641{
5642 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
18ad7996
HMH
5643}
5644
b21a15f6
HMH
5645static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5646 const char *buf, size_t count)
18ad7996 5647{
b21a15f6
HMH
5648 unsigned long t;
5649
5650 if (parse_strtoul(buf, 120, &t))
5651 return -EINVAL;
40ca9fdf 5652
ecf2a80a
HMH
5653 if (!fan_control_allowed)
5654 return -EPERM;
5655
b21a15f6
HMH
5656 fan_watchdog_maxinterval = t;
5657 fan_watchdog_reset();
40ca9fdf 5658
b21a15f6
HMH
5659 return count;
5660}
5661
5662static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5663 fan_fan_watchdog_show, fan_fan_watchdog_store);
5664
5665/* --------------------------------------------------------------------- */
5666static struct attribute *fan_attributes[] = {
5667 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5668 &dev_attr_fan_fan1_input.attr,
5669 NULL
5670};
5671
5672static const struct attribute_group fan_attr_group = {
5673 .attrs = fan_attributes,
5674};
5675
5676static int __init fan_init(struct ibm_init_struct *iibm)
5677{
5678 int rc;
5679
5680 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5681
5682 mutex_init(&fan_mutex);
5683 fan_status_access_mode = TPACPI_FAN_NONE;
5684 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5685 fan_control_commands = 0;
5686 fan_watchdog_maxinterval = 0;
5687 tp_features.fan_ctrl_status_undef = 0;
5688 fan_control_desired_level = 7;
5689
e0c7dfe7
HMH
5690 TPACPI_ACPIHANDLE_INIT(fans);
5691 TPACPI_ACPIHANDLE_INIT(gfan);
5692 TPACPI_ACPIHANDLE_INIT(sfan);
b21a15f6
HMH
5693
5694 if (gfan_handle) {
5695 /* 570, 600e/x, 770e, 770x */
5696 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5697 } else {
5698 /* all other ThinkPads: note that even old-style
5699 * ThinkPad ECs supports the fan control register */
5700 if (likely(acpi_ec_read(fan_status_offset,
5701 &fan_control_initial_status))) {
5702 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5703
5704 /* In some ThinkPads, neither the EC nor the ACPI
5705 * DSDT initialize the fan status, and it ends up
5706 * being set to 0x07 when it *could* be either
5707 * 0x07 or 0x80.
5708 *
5709 * Enable for TP-1Y (T43), TP-78 (R51e),
5710 * TP-76 (R52), TP-70 (T43, R52), which are known
5711 * to be buggy. */
5712 if (fan_control_initial_status == 0x07) {
5713 switch (thinkpad_id.ec_model) {
5714 case 0x5931: /* TP-1Y */
5715 case 0x3837: /* TP-78 */
5716 case 0x3637: /* TP-76 */
5717 case 0x3037: /* TP-70 */
e0c7dfe7 5718 printk(TPACPI_NOTICE
35ff8b9f
HMH
5719 "fan_init: initial fan status "
5720 "is unknown, assuming it is "
5721 "in auto mode\n");
b21a15f6
HMH
5722 tp_features.fan_ctrl_status_undef = 1;
5723 ;;
5724 }
5725 }
5726 } else {
e0c7dfe7 5727 printk(TPACPI_ERR
b21a15f6
HMH
5728 "ThinkPad ACPI EC access misbehaving, "
5729 "fan status and control unavailable\n");
5730 return 1;
5731 }
5732 }
5733
5734 if (sfan_handle) {
5735 /* 570, 770x-JL */
5736 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5737 fan_control_commands |=
5738 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5739 } else {
5740 if (!gfan_handle) {
5741 /* gfan without sfan means no fan control */
5742 /* all other models implement TP EC 0x2f control */
5743
5744 if (fans_handle) {
5745 /* X31, X40, X41 */
5746 fan_control_access_mode =
5747 TPACPI_FAN_WR_ACPI_FANS;
5748 fan_control_commands |=
5749 TPACPI_FAN_CMD_SPEED |
5750 TPACPI_FAN_CMD_LEVEL |
5751 TPACPI_FAN_CMD_ENABLE;
5752 } else {
5753 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5754 fan_control_commands |=
5755 TPACPI_FAN_CMD_LEVEL |
5756 TPACPI_FAN_CMD_ENABLE;
5757 }
fe98a52c 5758 }
b21a15f6 5759 }
18ad7996 5760
b21a15f6
HMH
5761 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5762 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5763 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5764 fan_status_access_mode, fan_control_access_mode);
1c6a334e 5765
b21a15f6
HMH
5766 /* fan control master switch */
5767 if (!fan_control_allowed) {
5768 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5769 fan_control_commands = 0;
5770 dbg_printk(TPACPI_DBG_INIT,
5771 "fan control features disabled by parameter\n");
18ad7996 5772 }
40ca9fdf 5773
b21a15f6
HMH
5774 /* update fan_control_desired_level */
5775 if (fan_status_access_mode != TPACPI_FAN_NONE)
5776 fan_get_status_safe(NULL);
fe98a52c 5777
b21a15f6
HMH
5778 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5779 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5780 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5781 &fan_attr_group);
b21a15f6
HMH
5782 if (rc < 0)
5783 return rc;
9c0a76e1
HMH
5784
5785 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5786 &driver_attr_fan_watchdog);
5787 if (rc < 0) {
5788 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5789 &fan_attr_group);
5790 return rc;
5791 }
b21a15f6
HMH
5792 return 0;
5793 } else
5794 return 1;
56b6aeb0
HMH
5795}
5796
b21a15f6 5797static void fan_exit(void)
56b6aeb0 5798{
35ff8b9f
HMH
5799 vdbg_printk(TPACPI_DBG_EXIT,
5800 "cancelling any pending fan watchdog tasks\n");
56b6aeb0 5801
b21a15f6
HMH
5802 /* FIXME: can we really do this unconditionally? */
5803 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
35ff8b9f
HMH
5804 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5805 &driver_attr_fan_watchdog);
40ca9fdf 5806
b21a15f6 5807 cancel_delayed_work(&fan_watchdog_task);
e0e3c061 5808 flush_workqueue(tpacpi_wq);
56b6aeb0
HMH
5809}
5810
5811static int fan_read(char *p)
5812{
5813 int len = 0;
5814 int rc;
5815 u8 status;
5816 unsigned int speed = 0;
5817
5818 switch (fan_status_access_mode) {
efa27145 5819 case TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0 5820 /* 570, 600e/x, 770e, 770x */
35ff8b9f
HMH
5821 rc = fan_get_status_safe(&status);
5822 if (rc < 0)
56b6aeb0
HMH
5823 return rc;
5824
5825 len += sprintf(p + len, "status:\t\t%s\n"
5826 "level:\t\t%d\n",
5827 (status != 0) ? "enabled" : "disabled", status);
5828 break;
5829
efa27145 5830 case TPACPI_FAN_RD_TPEC:
56b6aeb0 5831 /* all except 570, 600e/x, 770e, 770x */
35ff8b9f
HMH
5832 rc = fan_get_status_safe(&status);
5833 if (rc < 0)
56b6aeb0
HMH
5834 return rc;
5835
d8fd94d9 5836 if (unlikely(tp_features.fan_ctrl_status_undef)) {
56b6aeb0 5837 if (status != fan_control_initial_status)
d8fd94d9 5838 tp_features.fan_ctrl_status_undef = 0;
56b6aeb0
HMH
5839 else
5840 /* Return most likely status. In fact, it
5841 * might be the only possible status */
efa27145 5842 status = TP_EC_FAN_AUTO;
56b6aeb0
HMH
5843 }
5844
5845 len += sprintf(p + len, "status:\t\t%s\n",
5846 (status != 0) ? "enabled" : "disabled");
5847
35ff8b9f
HMH
5848 rc = fan_get_speed(&speed);
5849 if (rc < 0)
56b6aeb0
HMH
5850 return rc;
5851
5852 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5853
efa27145 5854 if (status & TP_EC_FAN_FULLSPEED)
56b6aeb0
HMH
5855 /* Disengaged mode takes precedence */
5856 len += sprintf(p + len, "level:\t\tdisengaged\n");
efa27145 5857 else if (status & TP_EC_FAN_AUTO)
56b6aeb0
HMH
5858 len += sprintf(p + len, "level:\t\tauto\n");
5859 else
5860 len += sprintf(p + len, "level:\t\t%d\n", status);
5861 break;
5862
efa27145 5863 case TPACPI_FAN_NONE:
56b6aeb0
HMH
5864 default:
5865 len += sprintf(p + len, "status:\t\tnot supported\n");
5866 }
5867
efa27145 5868 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
56b6aeb0
HMH
5869 len += sprintf(p + len, "commands:\tlevel <level>");
5870
5871 switch (fan_control_access_mode) {
efa27145 5872 case TPACPI_FAN_WR_ACPI_SFAN:
56b6aeb0
HMH
5873 len += sprintf(p + len, " (<level> is 0-7)\n");
5874 break;
5875
5876 default:
5877 len += sprintf(p + len, " (<level> is 0-7, "
fe98a52c 5878 "auto, disengaged, full-speed)\n");
56b6aeb0
HMH
5879 break;
5880 }
5881 }
18ad7996 5882
efa27145 5883 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
56b6aeb0 5884 len += sprintf(p + len, "commands:\tenable, disable\n"
35ff8b9f
HMH
5885 "commands:\twatchdog <timeout> (<timeout> "
5886 "is 0 (off), 1-120 (seconds))\n");
1da177e4 5887
efa27145 5888 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
56b6aeb0
HMH
5889 len += sprintf(p + len, "commands:\tspeed <speed>"
5890 " (<speed> is 0-65535)\n");
5891
5892 return len;
78f81cc4
BD
5893}
5894
18ad7996
HMH
5895static int fan_write_cmd_level(const char *cmd, int *rc)
5896{
5897 int level;
5898
a12095c2 5899 if (strlencmp(cmd, "level auto") == 0)
efa27145 5900 level = TP_EC_FAN_AUTO;
fe98a52c 5901 else if ((strlencmp(cmd, "level disengaged") == 0) |
35ff8b9f 5902 (strlencmp(cmd, "level full-speed") == 0))
efa27145 5903 level = TP_EC_FAN_FULLSPEED;
a12095c2 5904 else if (sscanf(cmd, "level %d", &level) != 1)
18ad7996
HMH
5905 return 0;
5906
35ff8b9f
HMH
5907 *rc = fan_set_level_safe(level);
5908 if (*rc == -ENXIO)
e0c7dfe7 5909 printk(TPACPI_ERR "level command accepted for unsupported "
18ad7996
HMH
5910 "access mode %d", fan_control_access_mode);
5911
5912 return 1;
5913}
5914
5915static int fan_write_cmd_enable(const char *cmd, int *rc)
5916{
5917 if (strlencmp(cmd, "enable") != 0)
5918 return 0;
5919
35ff8b9f
HMH
5920 *rc = fan_set_enable();
5921 if (*rc == -ENXIO)
e0c7dfe7 5922 printk(TPACPI_ERR "enable command accepted for unsupported "
18ad7996
HMH
5923 "access mode %d", fan_control_access_mode);
5924
5925 return 1;
5926}
5927
5928static int fan_write_cmd_disable(const char *cmd, int *rc)
5929{
5930 if (strlencmp(cmd, "disable") != 0)
5931 return 0;
5932
35ff8b9f
HMH
5933 *rc = fan_set_disable();
5934 if (*rc == -ENXIO)
e0c7dfe7 5935 printk(TPACPI_ERR "disable command accepted for unsupported "
18ad7996
HMH
5936 "access mode %d", fan_control_access_mode);
5937
5938 return 1;
5939}
5940
5941static int fan_write_cmd_speed(const char *cmd, int *rc)
5942{
5943 int speed;
5944
a8b7a662
HMH
5945 /* TODO:
5946 * Support speed <low> <medium> <high> ? */
5947
18ad7996
HMH
5948 if (sscanf(cmd, "speed %d", &speed) != 1)
5949 return 0;
5950
35ff8b9f
HMH
5951 *rc = fan_set_speed(speed);
5952 if (*rc == -ENXIO)
e0c7dfe7 5953 printk(TPACPI_ERR "speed command accepted for unsupported "
18ad7996
HMH
5954 "access mode %d", fan_control_access_mode);
5955
5956 return 1;
5957}
5958
16663a87
HMH
5959static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5960{
5961 int interval;
5962
5963 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5964 return 0;
5965
5966 if (interval < 0 || interval > 120)
5967 *rc = -EINVAL;
5968 else
5969 fan_watchdog_maxinterval = interval;
5970
5971 return 1;
5972}
5973
18ad7996
HMH
5974static int fan_write(char *buf)
5975{
5976 char *cmd;
5977 int rc = 0;
5978
5979 while (!rc && (cmd = next_cmd(&buf))) {
efa27145 5980 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
18ad7996 5981 fan_write_cmd_level(cmd, &rc)) &&
efa27145 5982 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
18ad7996 5983 (fan_write_cmd_enable(cmd, &rc) ||
16663a87
HMH
5984 fan_write_cmd_disable(cmd, &rc) ||
5985 fan_write_cmd_watchdog(cmd, &rc))) &&
efa27145 5986 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
18ad7996
HMH
5987 fan_write_cmd_speed(cmd, &rc))
5988 )
5989 rc = -EINVAL;
16663a87
HMH
5990 else if (!rc)
5991 fan_watchdog_reset();
18ad7996
HMH
5992 }
5993
5994 return rc;
5995}
5996
a5763f22
HMH
5997static struct ibm_struct fan_driver_data = {
5998 .name = "fan",
5999 .read = fan_read,
6000 .write = fan_write,
6001 .exit = fan_exit,
a5763f22
HMH
6002};
6003
56b6aeb0
HMH
6004/****************************************************************************
6005 ****************************************************************************
6006 *
6007 * Infrastructure
6008 *
6009 ****************************************************************************
6010 ****************************************************************************/
6011
7fd40029
HMH
6012/* sysfs name ---------------------------------------------------------- */
6013static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6014 struct device_attribute *attr,
6015 char *buf)
6016{
e0c7dfe7 6017 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7fd40029
HMH
6018}
6019
6020static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6021 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6022
6023/* --------------------------------------------------------------------- */
6024
56b6aeb0 6025/* /proc support */
94954cc6 6026static struct proc_dir_entry *proc_dir;
16663a87 6027
56b6aeb0
HMH
6028/*
6029 * Module and infrastructure proble, init and exit handling
6030 */
1da177e4 6031
b21a15f6
HMH
6032static int force_load;
6033
fe08bc4b 6034#ifdef CONFIG_THINKPAD_ACPI_DEBUG
a5763f22 6035static const char * __init str_supported(int is_supported)
fe08bc4b 6036{
a5763f22 6037 static char text_unsupported[] __initdata = "not supported";
fe08bc4b 6038
a5763f22 6039 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
fe08bc4b
HMH
6040}
6041#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6042
b21a15f6
HMH
6043static void ibm_exit(struct ibm_struct *ibm)
6044{
6045 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6046
6047 list_del_init(&ibm->all_drivers);
6048
6049 if (ibm->flags.acpi_notify_installed) {
6050 dbg_printk(TPACPI_DBG_EXIT,
6051 "%s: acpi_remove_notify_handler\n", ibm->name);
6052 BUG_ON(!ibm->acpi);
6053 acpi_remove_notify_handler(*ibm->acpi->handle,
6054 ibm->acpi->type,
6055 dispatch_acpi_notify);
6056 ibm->flags.acpi_notify_installed = 0;
6057 ibm->flags.acpi_notify_installed = 0;
6058 }
6059
6060 if (ibm->flags.proc_created) {
6061 dbg_printk(TPACPI_DBG_EXIT,
6062 "%s: remove_proc_entry\n", ibm->name);
6063 remove_proc_entry(ibm->name, proc_dir);
6064 ibm->flags.proc_created = 0;
6065 }
6066
6067 if (ibm->flags.acpi_driver_registered) {
6068 dbg_printk(TPACPI_DBG_EXIT,
6069 "%s: acpi_bus_unregister_driver\n", ibm->name);
6070 BUG_ON(!ibm->acpi);
6071 acpi_bus_unregister_driver(ibm->acpi->driver);
6072 kfree(ibm->acpi->driver);
6073 ibm->acpi->driver = NULL;
6074 ibm->flags.acpi_driver_registered = 0;
6075 }
6076
6077 if (ibm->flags.init_called && ibm->exit) {
6078 ibm->exit();
6079 ibm->flags.init_called = 0;
6080 }
6081
6082 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6083}
f74a27d4 6084
a5763f22 6085static int __init ibm_init(struct ibm_init_struct *iibm)
1da177e4
LT
6086{
6087 int ret;
a5763f22 6088 struct ibm_struct *ibm = iibm->data;
1da177e4
LT
6089 struct proc_dir_entry *entry;
6090
a5763f22
HMH
6091 BUG_ON(ibm == NULL);
6092
6093 INIT_LIST_HEAD(&ibm->all_drivers);
6094
92641177 6095 if (ibm->flags.experimental && !experimental)
1da177e4
LT
6096 return 0;
6097
fe08bc4b
HMH
6098 dbg_printk(TPACPI_DBG_INIT,
6099 "probing for %s\n", ibm->name);
6100
a5763f22
HMH
6101 if (iibm->init) {
6102 ret = iibm->init(iibm);
5fba344c
HMH
6103 if (ret > 0)
6104 return 0; /* probe failed */
6105 if (ret)
1da177e4 6106 return ret;
a5763f22 6107
92641177 6108 ibm->flags.init_called = 1;
1da177e4
LT
6109 }
6110
8d376cd6
HMH
6111 if (ibm->acpi) {
6112 if (ibm->acpi->hid) {
6113 ret = register_tpacpi_subdriver(ibm);
6114 if (ret)
6115 goto err_out;
6116 }
5fba344c 6117
8d376cd6
HMH
6118 if (ibm->acpi->notify) {
6119 ret = setup_acpi_notify(ibm);
6120 if (ret == -ENODEV) {
e0c7dfe7 6121 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8d376cd6
HMH
6122 ibm->name);
6123 ret = 0;
6124 goto err_out;
6125 }
6126 if (ret < 0)
6127 goto err_out;
5fba344c 6128 }
5fba344c
HMH
6129 }
6130
fe08bc4b
HMH
6131 dbg_printk(TPACPI_DBG_INIT,
6132 "%s installed\n", ibm->name);
6133
78f81cc4
BD
6134 if (ibm->read) {
6135 entry = create_proc_entry(ibm->name,
6136 S_IFREG | S_IRUGO | S_IWUSR,
6137 proc_dir);
6138 if (!entry) {
e0c7dfe7 6139 printk(TPACPI_ERR "unable to create proc entry %s\n",
78f81cc4 6140 ibm->name);
5fba344c
HMH
6141 ret = -ENODEV;
6142 goto err_out;
78f81cc4
BD
6143 }
6144 entry->owner = THIS_MODULE;
6145 entry->data = ibm;
8d376cd6 6146 entry->read_proc = &dispatch_procfs_read;
78f81cc4 6147 if (ibm->write)
8d376cd6 6148 entry->write_proc = &dispatch_procfs_write;
92641177 6149 ibm->flags.proc_created = 1;
78f81cc4 6150 }
1da177e4 6151
a5763f22
HMH
6152 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6153
1da177e4 6154 return 0;
5fba344c
HMH
6155
6156err_out:
fe08bc4b
HMH
6157 dbg_printk(TPACPI_DBG_INIT,
6158 "%s: at error exit path with result %d\n",
6159 ibm->name, ret);
6160
5fba344c
HMH
6161 ibm_exit(ibm);
6162 return (ret < 0)? ret : 0;
1da177e4
LT
6163}
6164
56b6aeb0
HMH
6165/* Probing */
6166
d5a2f2f1 6167static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
1da177e4 6168{
1855256c 6169 const struct dmi_device *dev = NULL;
56b6aeb0 6170 char ec_fw_string[18];
1da177e4 6171
d5a2f2f1
HMH
6172 if (!tp)
6173 return;
6174
6175 memset(tp, 0, sizeof(*tp));
6176
6177 if (dmi_name_in_vendors("IBM"))
6178 tp->vendor = PCI_VENDOR_ID_IBM;
6179 else if (dmi_name_in_vendors("LENOVO"))
6180 tp->vendor = PCI_VENDOR_ID_LENOVO;
6181 else
6182 return;
6183
6184 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
6185 GFP_KERNEL);
6186 if (!tp->bios_version_str)
6187 return;
6188 tp->bios_model = tp->bios_version_str[0]
6189 | (tp->bios_version_str[1] << 8);
6190
56b6aeb0
HMH
6191 /*
6192 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6193 * X32 or newer, all Z series; Some models must have an
6194 * up-to-date BIOS or they will not be detected.
6195 *
6196 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6197 */
6198 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6199 if (sscanf(dev->name,
6200 "IBM ThinkPad Embedded Controller -[%17c",
6201 ec_fw_string) == 1) {
6202 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6203 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
d5a2f2f1
HMH
6204
6205 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6206 tp->ec_model = ec_fw_string[0]
6207 | (ec_fw_string[1] << 8);
6208 break;
78f81cc4 6209 }
1da177e4 6210 }
d5a2f2f1
HMH
6211
6212 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6213 GFP_KERNEL);
90fe17f4 6214 if (tp->model_str && strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
d5a2f2f1
HMH
6215 kfree(tp->model_str);
6216 tp->model_str = NULL;
6217 }
8c74adbc
HMH
6218
6219 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6220 GFP_KERNEL);
1da177e4
LT
6221}
6222
5fba344c
HMH
6223static int __init probe_for_thinkpad(void)
6224{
6225 int is_thinkpad;
6226
6227 if (acpi_disabled)
6228 return -ENODEV;
6229
6230 /*
6231 * Non-ancient models have better DMI tagging, but very old models
6232 * don't.
6233 */
d5a2f2f1 6234 is_thinkpad = (thinkpad_id.model_str != NULL);
5fba344c
HMH
6235
6236 /* ec is required because many other handles are relative to it */
e0c7dfe7 6237 TPACPI_ACPIHANDLE_INIT(ec);
5fba344c
HMH
6238 if (!ec_handle) {
6239 if (is_thinkpad)
e0c7dfe7 6240 printk(TPACPI_ERR
5fba344c
HMH
6241 "Not yet supported ThinkPad detected!\n");
6242 return -ENODEV;
6243 }
6244
0dcef77c
HMH
6245 /*
6246 * Risks a regression on very old machines, but reduces potential
6247 * false positives a damn great deal
6248 */
6249 if (!is_thinkpad)
d5a2f2f1 6250 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
0dcef77c
HMH
6251
6252 if (!is_thinkpad && !force_load)
6253 return -ENODEV;
6254
5fba344c
HMH
6255 return 0;
6256}
6257
6258
56b6aeb0 6259/* Module init, exit, parameters */
1da177e4 6260
a5763f22
HMH
6261static struct ibm_init_struct ibms_init[] __initdata = {
6262 {
6263 .init = thinkpad_acpi_driver_init,
6264 .data = &thinkpad_acpi_driver_data,
6265 },
6266 {
6267 .init = hotkey_init,
6268 .data = &hotkey_driver_data,
6269 },
6270 {
6271 .init = bluetooth_init,
6272 .data = &bluetooth_driver_data,
6273 },
6274 {
6275 .init = wan_init,
6276 .data = &wan_driver_data,
6277 },
d7c1d17d 6278#ifdef CONFIG_THINKPAD_ACPI_VIDEO
a5763f22
HMH
6279 {
6280 .init = video_init,
6281 .data = &video_driver_data,
6282 },
d7c1d17d 6283#endif
a5763f22
HMH
6284 {
6285 .init = light_init,
6286 .data = &light_driver_data,
6287 },
6288#ifdef CONFIG_THINKPAD_ACPI_DOCK
6289 {
6290 .init = dock_init,
6291 .data = &dock_driver_data[0],
6292 },
6293 {
d94a7f16 6294 .init = dock_init2,
a5763f22
HMH
6295 .data = &dock_driver_data[1],
6296 },
6297#endif
6298#ifdef CONFIG_THINKPAD_ACPI_BAY
6299 {
6300 .init = bay_init,
6301 .data = &bay_driver_data,
6302 },
6303#endif
6304 {
6305 .init = cmos_init,
6306 .data = &cmos_driver_data,
6307 },
6308 {
6309 .init = led_init,
6310 .data = &led_driver_data,
6311 },
6312 {
6313 .init = beep_init,
6314 .data = &beep_driver_data,
6315 },
6316 {
6317 .init = thermal_init,
6318 .data = &thermal_driver_data,
6319 },
6320 {
6321 .data = &ecdump_driver_data,
6322 },
6323 {
6324 .init = brightness_init,
6325 .data = &brightness_driver_data,
6326 },
6327 {
6328 .data = &volume_driver_data,
6329 },
6330 {
6331 .init = fan_init,
6332 .data = &fan_driver_data,
6333 },
6334};
6335
3945ac36 6336static int __init set_ibm_param(const char *val, struct kernel_param *kp)
1da177e4
LT
6337{
6338 unsigned int i;
a5763f22 6339 struct ibm_struct *ibm;
1da177e4 6340
59f91ff1
HMH
6341 if (!kp || !kp->name || !val)
6342 return -EINVAL;
6343
a5763f22
HMH
6344 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6345 ibm = ibms_init[i].data;
59f91ff1
HMH
6346 WARN_ON(ibm == NULL);
6347
6348 if (!ibm || !ibm->name)
6349 continue;
a5763f22
HMH
6350
6351 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6352 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
78f81cc4 6353 return -ENOSPC;
a5763f22
HMH
6354 strcpy(ibms_init[i].param, val);
6355 strcat(ibms_init[i].param, ",");
78f81cc4
BD
6356 return 0;
6357 }
a5763f22 6358 }
1da177e4 6359
1da177e4
LT
6360 return -EINVAL;
6361}
6362
56b6aeb0 6363module_param(experimental, int, 0);
f68080f8 6364MODULE_PARM_DESC(experimental,
35ff8b9f 6365 "Enables experimental features when non-zero");
56b6aeb0 6366
132ce091 6367module_param_named(debug, dbg_level, uint, 0);
f68080f8 6368MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
132ce091 6369
86cc9445 6370module_param(force_load, bool, 0);
f68080f8 6371MODULE_PARM_DESC(force_load,
35ff8b9f
HMH
6372 "Attempts to load the driver even on a "
6373 "mis-identified ThinkPad when true");
0dcef77c 6374
86cc9445 6375module_param_named(fan_control, fan_control_allowed, bool, 0);
f68080f8 6376MODULE_PARM_DESC(fan_control,
35ff8b9f 6377 "Enables setting fan parameters features when true");
ecf2a80a 6378
24d3b774 6379module_param_named(brightness_mode, brightness_mode, int, 0);
f68080f8 6380MODULE_PARM_DESC(brightness_mode,
35ff8b9f
HMH
6381 "Selects brightness control strategy: "
6382 "0=auto, 1=EC, 2=CMOS, 3=both");
24d3b774 6383
87cc537a 6384module_param(brightness_enable, uint, 0);
f68080f8 6385MODULE_PARM_DESC(brightness_enable,
35ff8b9f 6386 "Enables backlight control when 1, disables when 0");
87cc537a 6387
ff80f137 6388module_param(hotkey_report_mode, uint, 0);
f68080f8 6389MODULE_PARM_DESC(hotkey_report_mode,
35ff8b9f
HMH
6390 "used for backwards compatibility with userspace, "
6391 "see documentation");
ff80f137 6392
e0c7dfe7 6393#define TPACPI_PARAM(feature) \
f68080f8 6394 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
cbb14842 6395 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
35ff8b9f 6396 "at module load, see documentation")
1da177e4 6397
e0c7dfe7
HMH
6398TPACPI_PARAM(hotkey);
6399TPACPI_PARAM(bluetooth);
6400TPACPI_PARAM(video);
6401TPACPI_PARAM(light);
85998248 6402#ifdef CONFIG_THINKPAD_ACPI_DOCK
e0c7dfe7 6403TPACPI_PARAM(dock);
63e5f248 6404#endif
85998248 6405#ifdef CONFIG_THINKPAD_ACPI_BAY
e0c7dfe7 6406TPACPI_PARAM(bay);
85998248 6407#endif /* CONFIG_THINKPAD_ACPI_BAY */
e0c7dfe7
HMH
6408TPACPI_PARAM(cmos);
6409TPACPI_PARAM(led);
6410TPACPI_PARAM(beep);
6411TPACPI_PARAM(ecdump);
6412TPACPI_PARAM(brightness);
6413TPACPI_PARAM(volume);
6414TPACPI_PARAM(fan);
78f81cc4 6415
b21a15f6
HMH
6416static void thinkpad_acpi_module_exit(void)
6417{
6418 struct ibm_struct *ibm, *itmp;
6419
6420 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6421
6422 list_for_each_entry_safe_reverse(ibm, itmp,
6423 &tpacpi_all_drivers,
6424 all_drivers) {
6425 ibm_exit(ibm);
6426 }
6427
6428 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6429
6430 if (tpacpi_inputdev) {
6431 if (tp_features.input_device_registered)
6432 input_unregister_device(tpacpi_inputdev);
6433 else
6434 input_free_device(tpacpi_inputdev);
6435 }
6436
6437 if (tpacpi_hwmon)
6438 hwmon_device_unregister(tpacpi_hwmon);
6439
6440 if (tp_features.sensors_pdev_attrs_registered)
6441 device_remove_file(&tpacpi_sensors_pdev->dev,
6442 &dev_attr_thinkpad_acpi_pdev_name);
6443 if (tpacpi_sensors_pdev)
6444 platform_device_unregister(tpacpi_sensors_pdev);
6445 if (tpacpi_pdev)
6446 platform_device_unregister(tpacpi_pdev);
6447
6448 if (tp_features.sensors_pdrv_attrs_registered)
6449 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6450 if (tp_features.platform_drv_attrs_registered)
6451 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6452
6453 if (tp_features.sensors_pdrv_registered)
6454 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6455
6456 if (tp_features.platform_drv_registered)
6457 platform_driver_unregister(&tpacpi_pdriver);
6458
6459 if (proc_dir)
e0c7dfe7 6460 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
b21a15f6 6461
e0e3c061
HMH
6462 if (tpacpi_wq)
6463 destroy_workqueue(tpacpi_wq);
6464
b21a15f6
HMH
6465 kfree(thinkpad_id.bios_version_str);
6466 kfree(thinkpad_id.ec_version_str);
6467 kfree(thinkpad_id.model_str);
6468}
6469
f74a27d4 6470
1def7115 6471static int __init thinkpad_acpi_module_init(void)
1da177e4
LT
6472{
6473 int ret, i;
6474
8fef502e
HMH
6475 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6476
ff80f137
HMH
6477 /* Parameter checking */
6478 if (hotkey_report_mode > 2)
6479 return -EINVAL;
6480
54ae1501 6481 /* Driver-level probe */
d5a2f2f1
HMH
6482
6483 get_thinkpad_model_data(&thinkpad_id);
5fba344c 6484 ret = probe_for_thinkpad();
d5a2f2f1
HMH
6485 if (ret) {
6486 thinkpad_acpi_module_exit();
5fba344c 6487 return ret;
d5a2f2f1 6488 }
1da177e4 6489
54ae1501 6490 /* Driver initialization */
d5a2f2f1 6491
e0c7dfe7
HMH
6492 TPACPI_ACPIHANDLE_INIT(ecrd);
6493 TPACPI_ACPIHANDLE_INIT(ecwr);
1da177e4 6494
e0e3c061
HMH
6495 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6496 if (!tpacpi_wq) {
6497 thinkpad_acpi_module_exit();
6498 return -ENOMEM;
6499 }
6500
e0c7dfe7 6501 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
1da177e4 6502 if (!proc_dir) {
35ff8b9f
HMH
6503 printk(TPACPI_ERR
6504 "unable to create proc dir " TPACPI_PROC_DIR);
1def7115 6505 thinkpad_acpi_module_exit();
1da177e4
LT
6506 return -ENODEV;
6507 }
6508 proc_dir->owner = THIS_MODULE;
78f81cc4 6509
54ae1501
HMH
6510 ret = platform_driver_register(&tpacpi_pdriver);
6511 if (ret) {
35ff8b9f
HMH
6512 printk(TPACPI_ERR
6513 "unable to register main platform driver\n");
54ae1501
HMH
6514 thinkpad_acpi_module_exit();
6515 return ret;
6516 }
ac36393d
HMH
6517 tp_features.platform_drv_registered = 1;
6518
7fd40029
HMH
6519 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6520 if (ret) {
35ff8b9f
HMH
6521 printk(TPACPI_ERR
6522 "unable to register hwmon platform driver\n");
7fd40029
HMH
6523 thinkpad_acpi_module_exit();
6524 return ret;
6525 }
6526 tp_features.sensors_pdrv_registered = 1;
6527
176750d6 6528 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
2369cc94
HMH
6529 if (!ret) {
6530 tp_features.platform_drv_attrs_registered = 1;
35ff8b9f
HMH
6531 ret = tpacpi_create_driver_attributes(
6532 &tpacpi_hwmon_pdriver.driver);
2369cc94 6533 }
176750d6 6534 if (ret) {
35ff8b9f
HMH
6535 printk(TPACPI_ERR
6536 "unable to create sysfs driver attributes\n");
176750d6
HMH
6537 thinkpad_acpi_module_exit();
6538 return ret;
6539 }
2369cc94 6540 tp_features.sensors_pdrv_attrs_registered = 1;
176750d6 6541
54ae1501
HMH
6542
6543 /* Device initialization */
e0c7dfe7 6544 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
54ae1501
HMH
6545 NULL, 0);
6546 if (IS_ERR(tpacpi_pdev)) {
6547 ret = PTR_ERR(tpacpi_pdev);
6548 tpacpi_pdev = NULL;
e0c7dfe7 6549 printk(TPACPI_ERR "unable to register platform device\n");
54ae1501
HMH
6550 thinkpad_acpi_module_exit();
6551 return ret;
6552 }
7fd40029 6553 tpacpi_sensors_pdev = platform_device_register_simple(
35ff8b9f
HMH
6554 TPACPI_HWMON_DRVR_NAME,
6555 -1, NULL, 0);
7fd40029
HMH
6556 if (IS_ERR(tpacpi_sensors_pdev)) {
6557 ret = PTR_ERR(tpacpi_sensors_pdev);
6558 tpacpi_sensors_pdev = NULL;
35ff8b9f
HMH
6559 printk(TPACPI_ERR
6560 "unable to register hwmon platform device\n");
7fd40029
HMH
6561 thinkpad_acpi_module_exit();
6562 return ret;
6563 }
6564 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6565 &dev_attr_thinkpad_acpi_pdev_name);
6566 if (ret) {
e0c7dfe7 6567 printk(TPACPI_ERR
35ff8b9f 6568 "unable to create sysfs hwmon device attributes\n");
7fd40029
HMH
6569 thinkpad_acpi_module_exit();
6570 return ret;
6571 }
6572 tp_features.sensors_pdev_attrs_registered = 1;
6573 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
54ae1501
HMH
6574 if (IS_ERR(tpacpi_hwmon)) {
6575 ret = PTR_ERR(tpacpi_hwmon);
6576 tpacpi_hwmon = NULL;
e0c7dfe7 6577 printk(TPACPI_ERR "unable to register hwmon device\n");
54ae1501
HMH
6578 thinkpad_acpi_module_exit();
6579 return ret;
6580 }
8523ed6f 6581 mutex_init(&tpacpi_inputdev_send_mutex);
7f5d1cd6
HMH
6582 tpacpi_inputdev = input_allocate_device();
6583 if (!tpacpi_inputdev) {
e0c7dfe7 6584 printk(TPACPI_ERR "unable to allocate input device\n");
7f5d1cd6
HMH
6585 thinkpad_acpi_module_exit();
6586 return -ENOMEM;
6587 } else {
6588 /* Prepare input device, but don't register */
6589 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
e0c7dfe7 6590 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7f5d1cd6 6591 tpacpi_inputdev->id.bustype = BUS_HOST;
edf0e0e5
HMH
6592 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6593 thinkpad_id.vendor :
6594 PCI_VENDOR_ID_IBM;
7f5d1cd6
HMH
6595 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6596 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6597 }
a5763f22
HMH
6598 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6599 ret = ibm_init(&ibms_init[i]);
6600 if (ret >= 0 && *ibms_init[i].param)
6601 ret = ibms_init[i].data->write(ibms_init[i].param);
1da177e4 6602 if (ret < 0) {
1def7115 6603 thinkpad_acpi_module_exit();
1da177e4
LT
6604 return ret;
6605 }
6606 }
7f5d1cd6
HMH
6607 ret = input_register_device(tpacpi_inputdev);
6608 if (ret < 0) {
e0c7dfe7 6609 printk(TPACPI_ERR "unable to register input device\n");
7f5d1cd6
HMH
6610 thinkpad_acpi_module_exit();
6611 return ret;
6612 } else {
6613 tp_features.input_device_registered = 1;
6614 }
1da177e4 6615
8fef502e 6616 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
1da177e4
LT
6617 return 0;
6618}
6619
f68080f8
HMH
6620/* Please remove this in year 2009 */
6621MODULE_ALIAS("ibm_acpi");
6622
95e57ab2
HMH
6623MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6624
f68080f8
HMH
6625/*
6626 * DMI matching for module autoloading
6627 *
6628 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6629 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6630 *
6631 * Only models listed in thinkwiki will be supported, so add yours
6632 * if it is not there yet.
6633 */
6634#define IBM_BIOS_MODULE_ALIAS(__type) \
6635 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6636
6637/* Non-ancient thinkpads */
6638MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6639MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6640
6641/* Ancient thinkpad BIOSes have to be identified by
6642 * BIOS type or model number, and there are far less
6643 * BIOS types than model numbers... */
6644IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6645IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6646IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6647
6648MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
e0c7dfe7
HMH
6649MODULE_DESCRIPTION(TPACPI_DESC);
6650MODULE_VERSION(TPACPI_VERSION);
f68080f8
HMH
6651MODULE_LICENSE("GPL");
6652
1def7115
HMH
6653module_init(thinkpad_acpi_module_init);
6654module_exit(thinkpad_acpi_module_exit);