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