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