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