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