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