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