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