ACPI: thinkpad-acpi: enable more hotkeys
[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
643f12db 24#define IBM_VERSION "0.14"
176750d6 25#define TPACPI_SYSFS_VERSION 0x000100
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
56b6aeb0
HMH
120/****************************************************************************
121 ****************************************************************************
122 *
123 * ACPI Helpers and device model
124 *
125 ****************************************************************************
126 ****************************************************************************/
127
128/*************************************************************************
129 * ACPI basic handles
130 */
1da177e4 131
94954cc6 132static acpi_handle root_handle;
1da177e4
LT
133
134#define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
78f81cc4 137 static char *object##_path; \
1da177e4
LT
138 static char *object##_paths[] = { paths }
139
78f81cc4
BD
140IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
56b6aeb0 147 );
78f81cc4 148
56b6aeb0
HMH
149IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
78f81cc4 151
56b6aeb0
HMH
152
153/*************************************************************************
154 * Misc ACPI handles
155 */
78f81cc4
BD
156
157IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
56b6aeb0 160 ); /* all others */
78f81cc4
BD
161
162IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
56b6aeb0 165 ); /* 570 */
78f81cc4 166
78f81cc4 167
56b6aeb0
HMH
168/*************************************************************************
169 * ACPI helpers
a8b7a662
HMH
170 */
171
1da177e4
LT
172static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
174{
175 char *fmt0 = fmt;
78f81cc4
BD
176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
180 acpi_status status;
181 va_list ap;
182 char res_type;
183 int success;
184 int quiet;
1da177e4
LT
185
186 if (!*fmt) {
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188 return 0;
189 }
190
191 if (*fmt == 'q') {
192 quiet = 1;
193 fmt++;
194 } else
195 quiet = 0;
196
197 res_type = *(fmt++);
198
199 params.count = 0;
200 params.pointer = &in_objs[0];
201
202 va_start(ap, fmt);
203 while (*fmt) {
204 char c = *(fmt++);
205 switch (c) {
206 case 'd': /* int */
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209 break;
78f81cc4 210 /* add more types as needed */
1da177e4
LT
211 default:
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
214 return 0;
215 }
216 }
217 va_end(ap);
218
78f81cc4
BD
219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
222 resultp = &result;
223 } else
224 resultp = NULL;
1da177e4 225
78f81cc4 226 status = acpi_evaluate_object(handle, method, &params, resultp);
1da177e4
LT
227
228 switch (res_type) {
78f81cc4 229 case 'd': /* int */
1da177e4
LT
230 if (res)
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233 break;
78f81cc4 234 case 'v': /* void */
1da177e4
LT
235 success = status == AE_OK;
236 break;
78f81cc4 237 /* add more types as needed */
1da177e4
LT
238 default:
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
241 return 0;
242 }
243
56b6aeb0
HMH
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
247
248 return success;
249}
250
251static void __unused acpi_print_int(acpi_handle handle, char *method)
252{
253 int i;
254
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
257 else
258 printk(IBM_ERR "error calling %s\n", method);
259}
260
261static int acpi_ec_read(int i, u8 * p)
262{
263 int v;
264
265 if (ecrd_handle) {
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267 return 0;
268 *p = v;
269 } else {
270 if (ec_read(i, p) < 0)
271 return 0;
272 }
273
274 return 1;
275}
276
277static int acpi_ec_write(int i, u8 v)
278{
279 if (ecwr_handle) {
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281 return 0;
282 } else {
283 if (ec_write(i, v) < 0)
284 return 0;
285 }
286
287 return 1;
288}
289
290static int _sta(acpi_handle handle)
291{
292 int status;
293
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295 status = 0;
296
297 return status;
298}
299
c9bea99c
HMH
300static int issue_thinkpad_cmos_command(int cmos_cmd)
301{
302 if (!cmos_handle)
303 return -ENXIO;
304
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306 return -EIO;
307
308 return 0;
309}
310
56b6aeb0
HMH
311/*************************************************************************
312 * ACPI device model
313 */
314
8d376cd6 315static void drv_acpi_handle_init(char *name,
5fba344c
HMH
316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
56b6aeb0
HMH
318{
319 int i;
320 acpi_status status;
321
5ae930e6
HMH
322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323 name);
324
56b6aeb0
HMH
325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
328 *path = paths[i];
5ae930e6
HMH
329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
331 *path, name);
56b6aeb0
HMH
332 return;
333 }
334 }
335
5ae930e6
HMH
336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337 name);
56b6aeb0
HMH
338 *handle = NULL;
339}
340
8d376cd6 341static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
56b6aeb0
HMH
342{
343 struct ibm_struct *ibm = data;
344
8d376cd6 345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
56b6aeb0
HMH
346 return;
347
8d376cd6 348 ibm->acpi->notify(ibm, event);
56b6aeb0
HMH
349}
350
8d376cd6 351static int __init setup_acpi_notify(struct ibm_struct *ibm)
56b6aeb0
HMH
352{
353 acpi_status status;
5ae930e6 354 int rc;
56b6aeb0 355
8d376cd6
HMH
356 BUG_ON(!ibm->acpi);
357
358 if (!*ibm->acpi->handle)
56b6aeb0
HMH
359 return 0;
360
5ae930e6 361 vdbg_printk(TPACPI_DBG_INIT,
fe08bc4b
HMH
362 "setting up ACPI notify for %s\n", ibm->name);
363
5ae930e6
HMH
364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365 if (rc < 0) {
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367 ibm->name, rc);
56b6aeb0
HMH
368 return -ENODEV;
369 }
370
8d376cd6
HMH
371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
643f12db
HMH
373 IBM_ACPI_EVENT_PREFIX,
374 ibm->name);
56b6aeb0 375
8d376cd6
HMH
376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
56b6aeb0
HMH
378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
381 ibm->name);
382 } else {
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384 ibm->name, status);
385 }
386 return -ENODEV;
387 }
8d376cd6 388 ibm->flags.acpi_notify_installed = 1;
56b6aeb0
HMH
389 return 0;
390}
391
8d376cd6 392static int __init tpacpi_device_add(struct acpi_device *device)
56b6aeb0
HMH
393{
394 return 0;
395}
396
6700121b 397static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
56b6aeb0 398{
5ae930e6 399 int rc;
56b6aeb0 400
fe08bc4b
HMH
401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
403
8d376cd6
HMH
404 BUG_ON(!ibm->acpi);
405
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
5fba344c
HMH
408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409 return -ENOMEM;
56b6aeb0
HMH
410 }
411
8d376cd6
HMH
412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
414 ibm->acpi->driver->ops.add = &tpacpi_device_add;
56b6aeb0 415
5ae930e6
HMH
416 rc = acpi_bus_register_driver(ibm->acpi->driver);
417 if (rc < 0) {
56b6aeb0 418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
5ae930e6 419 ibm->acpi->hid, rc);
8d376cd6
HMH
420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
5ae930e6 422 } else if (!rc)
8d376cd6 423 ibm->flags.acpi_driver_registered = 1;
56b6aeb0 424
5ae930e6 425 return rc;
56b6aeb0
HMH
426}
427
428
429/****************************************************************************
430 ****************************************************************************
431 *
432 * Procfs Helpers
433 *
434 ****************************************************************************
435 ****************************************************************************/
436
8d376cd6
HMH
437static int dispatch_procfs_read(char *page, char **start, off_t off,
438 int count, int *eof, void *data)
56b6aeb0
HMH
439{
440 struct ibm_struct *ibm = data;
441 int len;
442
443 if (!ibm || !ibm->read)
444 return -EINVAL;
445
446 len = ibm->read(page);
447 if (len < 0)
448 return len;
449
450 if (len <= off + count)
451 *eof = 1;
452 *start = page + off;
453 len -= off;
454 if (len > count)
455 len = count;
456 if (len < 0)
457 len = 0;
458
459 return len;
460}
461
8d376cd6
HMH
462static int dispatch_procfs_write(struct file *file,
463 const char __user * userbuf,
464 unsigned long count, void *data)
56b6aeb0
HMH
465{
466 struct ibm_struct *ibm = data;
467 char *kernbuf;
468 int ret;
469
470 if (!ibm || !ibm->write)
471 return -EINVAL;
472
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
474 if (!kernbuf)
475 return -ENOMEM;
1da177e4 476
56b6aeb0
HMH
477 if (copy_from_user(kernbuf, userbuf, count)) {
478 kfree(kernbuf);
479 return -EFAULT;
480 }
1da177e4 481
56b6aeb0
HMH
482 kernbuf[count] = 0;
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
485 if (ret == 0)
486 ret = count;
1da177e4 487
56b6aeb0
HMH
488 kfree(kernbuf);
489
490 return ret;
1da177e4
LT
491}
492
493static char *next_cmd(char **cmds)
494{
495 char *start = *cmds;
496 char *end;
497
498 while ((end = strchr(start, ',')) && end == start)
499 start = end + 1;
500
501 if (!end)
502 return NULL;
503
504 *end = 0;
505 *cmds = end + 1;
506 return start;
507}
508
56b6aeb0 509
54ae1501
HMH
510/****************************************************************************
511 ****************************************************************************
512 *
513 * Device model: hwmon and platform
514 *
515 ****************************************************************************
516 ****************************************************************************/
517
94954cc6
HMH
518static struct platform_device *tpacpi_pdev;
519static struct class_device *tpacpi_hwmon;
54ae1501
HMH
520
521static struct platform_driver tpacpi_pdriver = {
522 .driver = {
523 .name = IBM_DRVR_NAME,
524 .owner = THIS_MODULE,
525 },
526};
527
528
176750d6
HMH
529/*************************************************************************
530 * thinkpad-acpi driver attributes
531 */
532
533/* interface_version --------------------------------------------------- */
534static ssize_t tpacpi_driver_interface_version_show(
535 struct device_driver *drv,
536 char *buf)
537{
538 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
539}
540
541static DRIVER_ATTR(interface_version, S_IRUGO,
542 tpacpi_driver_interface_version_show, NULL);
543
544/* debug_level --------------------------------------------------------- */
545static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
546 char *buf)
547{
548 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
549}
550
551static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
552 const char *buf, size_t count)
553{
554 unsigned long t;
176750d6 555
7252374a 556 if (parse_strtoul(buf, 0xffff, &t))
176750d6
HMH
557 return -EINVAL;
558
559 dbg_level = t;
560
561 return count;
562}
563
564static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
565 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
566
567/* version ------------------------------------------------------------- */
568static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
569 char *buf)
570{
571 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
572}
573
574static DRIVER_ATTR(version, S_IRUGO,
575 tpacpi_driver_version_show, NULL);
576
577/* --------------------------------------------------------------------- */
578
579static struct driver_attribute* tpacpi_driver_attributes[] = {
580 &driver_attr_debug_level, &driver_attr_version,
581 &driver_attr_interface_version,
582};
583
584static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
585{
586 int i, res;
587
588 i = 0;
589 res = 0;
590 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
591 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
592 i++;
593 }
594
595 return res;
596}
597
598static void tpacpi_remove_driver_attributes(struct device_driver *drv)
599{
600 int i;
601
602 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
603 driver_remove_file(drv, tpacpi_driver_attributes[i]);
604}
605
7252374a
HMH
606/*************************************************************************
607 * sysfs support helpers
608 */
609
610struct attribute_set_obj {
611 struct attribute_set s;
612 struct attribute *a;
613} __attribute__((packed));
614
615static struct attribute_set *create_attr_set(unsigned int max_members,
616 const char* name)
617{
618 struct attribute_set_obj *sobj;
619
620 if (max_members == 0)
621 return NULL;
622
623 /* Allocates space for implicit NULL at the end too */
624 sobj = kzalloc(sizeof(struct attribute_set_obj) +
625 max_members * sizeof(struct attribute *),
626 GFP_KERNEL);
627 if (!sobj)
628 return NULL;
629 sobj->s.max_members = max_members;
630 sobj->s.group.attrs = &sobj->a;
631 sobj->s.group.name = name;
632
633 return &sobj->s;
634}
635
636/* not multi-threaded safe, use it in a single thread per set */
637static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
638{
639 if (!s || !attr)
640 return -EINVAL;
641
642 if (s->members >= s->max_members)
643 return -ENOMEM;
644
645 s->group.attrs[s->members] = attr;
646 s->members++;
647
648 return 0;
649}
650
651static int add_many_to_attr_set(struct attribute_set* s,
652 struct attribute **attr,
653 unsigned int count)
654{
655 int i, res;
656
657 for (i = 0; i < count; i++) {
658 res = add_to_attr_set(s, attr[i]);
659 if (res)
660 return res;
661 }
662
663 return 0;
664}
665
666static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
667{
668 sysfs_remove_group(kobj, &s->group);
669 destroy_attr_set(s);
670}
671
672static int parse_strtoul(const char *buf,
673 unsigned long max, unsigned long *value)
674{
675 char *endp;
676
677 *value = simple_strtoul(buf, &endp, 0);
678 while (*endp && isspace(*endp))
679 endp++;
680 if (*endp || *value > max)
681 return -EINVAL;
682
683 return 0;
684}
685
56b6aeb0
HMH
686/****************************************************************************
687 ****************************************************************************
688 *
689 * Subdrivers
690 *
691 ****************************************************************************
692 ****************************************************************************/
693
694/*************************************************************************
142cfc90 695 * thinkpad-acpi init subdriver
56b6aeb0
HMH
696 */
697
a5763f22 698static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1da177e4
LT
699{
700 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
701 printk(IBM_INFO "%s\n", IBM_URL);
702
3945ac36
HMH
703 if (ibm_thinkpad_ec_found)
704 printk(IBM_INFO "ThinkPad EC firmware %s\n",
705 ibm_thinkpad_ec_found);
706
1da177e4
LT
707 return 0;
708}
709
643f12db 710static int thinkpad_acpi_driver_read(char *p)
1da177e4
LT
711{
712 int len = 0;
713
714 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
715 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
716
717 return len;
718}
719
a5763f22
HMH
720static struct ibm_struct thinkpad_acpi_driver_data = {
721 .name = "driver",
722 .read = thinkpad_acpi_driver_read,
723};
724
56b6aeb0
HMH
725/*************************************************************************
726 * Hotkey subdriver
727 */
728
78f81cc4 729static int hotkey_orig_status;
ae92bd17 730static u32 hotkey_orig_mask;
78f81cc4 731
94954cc6 732static struct attribute_set *hotkey_dev_attributes;
a0416420
HMH
733
734/* sysfs hotkey enable ------------------------------------------------- */
735static ssize_t hotkey_enable_show(struct device *dev,
736 struct device_attribute *attr,
737 char *buf)
738{
ae92bd17
HMH
739 int res, status;
740 u32 mask;
a0416420
HMH
741
742 res = hotkey_get(&status, &mask);
743 if (res)
744 return res;
745
746 return snprintf(buf, PAGE_SIZE, "%d\n", status);
747}
748
749static ssize_t hotkey_enable_store(struct device *dev,
750 struct device_attribute *attr,
751 const char *buf, size_t count)
752{
753 unsigned long t;
ae92bd17
HMH
754 int res, status;
755 u32 mask;
a0416420
HMH
756
757 if (parse_strtoul(buf, 1, &t))
758 return -EINVAL;
759
760 res = hotkey_get(&status, &mask);
761 if (!res)
762 res = hotkey_set(t, mask);
763
764 return (res) ? res : count;
765}
766
767static struct device_attribute dev_attr_hotkey_enable =
cc4c24e1 768 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
a0416420
HMH
769 hotkey_enable_show, hotkey_enable_store);
770
771/* sysfs hotkey mask --------------------------------------------------- */
772static ssize_t hotkey_mask_show(struct device *dev,
773 struct device_attribute *attr,
774 char *buf)
775{
ae92bd17
HMH
776 int res, status;
777 u32 mask;
a0416420
HMH
778
779 res = hotkey_get(&status, &mask);
780 if (res)
781 return res;
782
ae92bd17 783 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
a0416420
HMH
784}
785
786static ssize_t hotkey_mask_store(struct device *dev,
787 struct device_attribute *attr,
788 const char *buf, size_t count)
789{
790 unsigned long t;
ae92bd17
HMH
791 int res, status;
792 u32 mask;
a0416420 793
ae92bd17 794 if (parse_strtoul(buf, 0xffffffffUL, &t))
a0416420
HMH
795 return -EINVAL;
796
797 res = hotkey_get(&status, &mask);
798 if (!res)
799 hotkey_set(status, t);
800
801 return (res) ? res : count;
802}
803
804static struct device_attribute dev_attr_hotkey_mask =
cc4c24e1 805 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
a0416420
HMH
806 hotkey_mask_show, hotkey_mask_store);
807
808/* sysfs hotkey bios_enabled ------------------------------------------- */
809static ssize_t hotkey_bios_enabled_show(struct device *dev,
810 struct device_attribute *attr,
811 char *buf)
812{
813 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
814}
815
816static struct device_attribute dev_attr_hotkey_bios_enabled =
cc4c24e1 817 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
a0416420
HMH
818
819/* sysfs hotkey bios_mask ---------------------------------------------- */
820static ssize_t hotkey_bios_mask_show(struct device *dev,
821 struct device_attribute *attr,
822 char *buf)
823{
ae92bd17 824 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
a0416420
HMH
825}
826
827static struct device_attribute dev_attr_hotkey_bios_mask =
cc4c24e1 828 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
a0416420
HMH
829
830/* --------------------------------------------------------------------- */
831
832static struct attribute *hotkey_mask_attributes[] = {
833 &dev_attr_hotkey_mask.attr,
834 &dev_attr_hotkey_bios_enabled.attr,
835 &dev_attr_hotkey_bios_mask.attr,
836};
837
a5763f22 838static int __init hotkey_init(struct ibm_init_struct *iibm)
56b6aeb0 839{
b86c4722
HMH
840 int res;
841
fe08bc4b
HMH
842 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
843
8d376cd6 844 IBM_ACPIHANDLE_INIT(hkey);
40ca9fdf 845 mutex_init(&hotkey_mutex);
5fba344c 846
56b6aeb0 847 /* hotkey not supported on 570 */
d8fd94d9 848 tp_features.hotkey = hkey_handle != NULL;
56b6aeb0 849
fe08bc4b 850 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
d8fd94d9 851 str_supported(tp_features.hotkey));
fe08bc4b 852
d8fd94d9 853 if (tp_features.hotkey) {
cc4c24e1 854 hotkey_dev_attributes = create_attr_set(4, NULL);
a0416420
HMH
855 if (!hotkey_dev_attributes)
856 return -ENOMEM;
857 res = add_to_attr_set(hotkey_dev_attributes,
858 &dev_attr_hotkey_enable.attr);
859 if (res)
860 return res;
861
56b6aeb0
HMH
862 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
863 A30, R30, R31, T20-22, X20-21, X22-24 */
d8fd94d9
HMH
864 tp_features.hotkey_mask =
865 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
56b6aeb0 866
fe08bc4b 867 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
d8fd94d9 868 str_supported(tp_features.hotkey_mask));
fe08bc4b 869
b86c4722 870 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
a0416420
HMH
871 if (!res && tp_features.hotkey_mask) {
872 res = add_many_to_attr_set(hotkey_dev_attributes,
873 hotkey_mask_attributes,
874 ARRAY_SIZE(hotkey_mask_attributes));
875 }
876 if (!res)
877 res = register_attr_set_with_sysfs(
878 hotkey_dev_attributes,
879 &tpacpi_pdev->dev.kobj);
880
b86c4722
HMH
881 if (res)
882 return res;
56b6aeb0
HMH
883 }
884
d8fd94d9 885 return (tp_features.hotkey)? 0 : 1;
56b6aeb0
HMH
886}
887
888static void hotkey_exit(void)
889{
b86c4722
HMH
890 int res;
891
d8fd94d9 892 if (tp_features.hotkey) {
fe08bc4b 893 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
b86c4722
HMH
894 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
895 if (res)
896 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
5fba344c 897 }
a0416420
HMH
898
899 if (hotkey_dev_attributes) {
900 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
901 hotkey_dev_attributes = NULL;
902 }
56b6aeb0
HMH
903}
904
905static void hotkey_notify(struct ibm_struct *ibm, u32 event)
906{
907 int hkey;
908
ae92bd17 909 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
8d376cd6 910 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
ae92bd17
HMH
911 } else {
912 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
8d376cd6 913 acpi_bus_generate_event(ibm->acpi->device, event, 0);
56b6aeb0
HMH
914 }
915}
916
40ca9fdf
HMH
917/*
918 * Call with hotkey_mutex held
919 */
ae92bd17 920static int hotkey_get(int *status, u32 *mask)
1da177e4
LT
921{
922 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
b86c4722 923 return -EIO;
78f81cc4 924
d8fd94d9 925 if (tp_features.hotkey_mask)
78f81cc4 926 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
b86c4722 927 return -EIO;
78f81cc4 928
b86c4722 929 return 0;
1da177e4
LT
930}
931
40ca9fdf
HMH
932/*
933 * Call with hotkey_mutex held
934 */
ae92bd17 935static int hotkey_set(int status, u32 mask)
1da177e4
LT
936{
937 int i;
938
939 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
b86c4722 940 return -EIO;
1da177e4 941
d8fd94d9 942 if (tp_features.hotkey_mask)
78f81cc4
BD
943 for (i = 0; i < 32; i++) {
944 int bit = ((1 << i) & mask) != 0;
945 if (!acpi_evalf(hkey_handle,
946 NULL, "MHKM", "vdd", i + 1, bit))
b86c4722 947 return -EIO;
78f81cc4 948 }
1da177e4 949
b86c4722 950 return 0;
1da177e4
LT
951}
952
a0416420 953/* procfs -------------------------------------------------------------- */
78f81cc4 954static int hotkey_read(char *p)
1da177e4 955{
ae92bd17
HMH
956 int res, status;
957 u32 mask;
1da177e4
LT
958 int len = 0;
959
d8fd94d9 960 if (!tp_features.hotkey) {
78f81cc4
BD
961 len += sprintf(p + len, "status:\t\tnot supported\n");
962 return len;
963 }
964
40ca9fdf
HMH
965 res = mutex_lock_interruptible(&hotkey_mutex);
966 if (res < 0)
967 return res;
b86c4722 968 res = hotkey_get(&status, &mask);
40ca9fdf 969 mutex_unlock(&hotkey_mutex);
b86c4722
HMH
970 if (res)
971 return res;
1da177e4
LT
972
973 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
d8fd94d9 974 if (tp_features.hotkey_mask) {
ae92bd17 975 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1da177e4
LT
976 len += sprintf(p + len,
977 "commands:\tenable, disable, reset, <mask>\n");
978 } else {
979 len += sprintf(p + len, "mask:\t\tnot supported\n");
980 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
981 }
982
983 return len;
984}
985
78f81cc4 986static int hotkey_write(char *buf)
1da177e4 987{
ae92bd17
HMH
988 int res, status;
989 u32 mask;
1da177e4
LT
990 char *cmd;
991 int do_cmd = 0;
992
d8fd94d9 993 if (!tp_features.hotkey)
1da177e4
LT
994 return -ENODEV;
995
40ca9fdf
HMH
996 res = mutex_lock_interruptible(&hotkey_mutex);
997 if (res < 0)
998 return res;
999
b86c4722
HMH
1000 res = hotkey_get(&status, &mask);
1001 if (res)
40ca9fdf 1002 goto errexit;
78f81cc4 1003
40ca9fdf 1004 res = 0;
1da177e4
LT
1005 while ((cmd = next_cmd(&buf))) {
1006 if (strlencmp(cmd, "enable") == 0) {
1007 status = 1;
1008 } else if (strlencmp(cmd, "disable") == 0) {
1009 status = 0;
1010 } else if (strlencmp(cmd, "reset") == 0) {
78f81cc4
BD
1011 status = hotkey_orig_status;
1012 mask = hotkey_orig_mask;
1da177e4
LT
1013 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1014 /* mask set */
1015 } else if (sscanf(cmd, "%x", &mask) == 1) {
1016 /* mask set */
40ca9fdf
HMH
1017 } else {
1018 res = -EINVAL;
1019 goto errexit;
1020 }
1da177e4
LT
1021 do_cmd = 1;
1022 }
1023
40ca9fdf 1024 if (do_cmd)
b86c4722 1025 res = hotkey_set(status, mask);
1da177e4 1026
40ca9fdf
HMH
1027errexit:
1028 mutex_unlock(&hotkey_mutex);
1029 return res;
78f81cc4 1030}
1da177e4 1031
8d376cd6
HMH
1032static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1033 .hid = IBM_HKEY_HID,
1034 .notify = hotkey_notify,
1035 .handle = &hkey_handle,
1036 .type = ACPI_DEVICE_NOTIFY,
1037};
1038
a5763f22
HMH
1039static struct ibm_struct hotkey_driver_data = {
1040 .name = "hotkey",
a5763f22
HMH
1041 .read = hotkey_read,
1042 .write = hotkey_write,
1043 .exit = hotkey_exit,
8d376cd6 1044 .acpi = &ibm_hotkey_acpidriver,
a5763f22
HMH
1045};
1046
56b6aeb0
HMH
1047/*************************************************************************
1048 * Bluetooth subdriver
1049 */
1da177e4 1050
d3a6ade4
HMH
1051/* sysfs bluetooth enable ---------------------------------------------- */
1052static ssize_t bluetooth_enable_show(struct device *dev,
1053 struct device_attribute *attr,
1054 char *buf)
1055{
1056 int status;
1057
1058 status = bluetooth_get_radiosw();
1059 if (status < 0)
1060 return status;
1061
1062 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1063}
1064
1065static ssize_t bluetooth_enable_store(struct device *dev,
1066 struct device_attribute *attr,
1067 const char *buf, size_t count)
1068{
1069 unsigned long t;
1070 int res;
1071
1072 if (parse_strtoul(buf, 1, &t))
1073 return -EINVAL;
1074
1075 res = bluetooth_set_radiosw(t);
1076
1077 return (res) ? res : count;
1078}
1079
1080static struct device_attribute dev_attr_bluetooth_enable =
cc4c24e1 1081 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
d3a6ade4
HMH
1082 bluetooth_enable_show, bluetooth_enable_store);
1083
1084/* --------------------------------------------------------------------- */
1085
1086static struct attribute *bluetooth_attributes[] = {
1087 &dev_attr_bluetooth_enable.attr,
1088 NULL
1089};
1090
1091static const struct attribute_group bluetooth_attr_group = {
d3a6ade4
HMH
1092 .attrs = bluetooth_attributes,
1093};
1094
a5763f22 1095static int __init bluetooth_init(struct ibm_init_struct *iibm)
1da177e4 1096{
d3a6ade4 1097 int res;
d6fdd1e9
HMH
1098 int status = 0;
1099
fe08bc4b
HMH
1100 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1101
8d376cd6 1102 IBM_ACPIHANDLE_INIT(hkey);
5fba344c 1103
78f81cc4
BD
1104 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1105 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
d8fd94d9 1106 tp_features.bluetooth = hkey_handle &&
d6fdd1e9
HMH
1107 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1108
1109 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1110 str_supported(tp_features.bluetooth),
1111 status);
1112
d3a6ade4
HMH
1113 if (tp_features.bluetooth) {
1114 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1115 /* no bluetooth hardware present in system */
1116 tp_features.bluetooth = 0;
1117 dbg_printk(TPACPI_DBG_INIT,
1118 "bluetooth hardware not installed\n");
1119 } else {
1120 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1121 &bluetooth_attr_group);
1122 if (res)
1123 return res;
1124 }
d6fdd1e9 1125 }
fe08bc4b 1126
d8fd94d9 1127 return (tp_features.bluetooth)? 0 : 1;
1da177e4
LT
1128}
1129
d3a6ade4
HMH
1130static void bluetooth_exit(void)
1131{
1132 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1133 &bluetooth_attr_group);
1134}
1135
d6fdd1e9 1136static int bluetooth_get_radiosw(void)
1da177e4
LT
1137{
1138 int status;
1139
d6fdd1e9
HMH
1140 if (!tp_features.bluetooth)
1141 return -ENODEV;
1da177e4 1142
d6fdd1e9
HMH
1143 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1144 return -EIO;
1145
1146 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1147}
1148
1149static int bluetooth_set_radiosw(int radio_on)
1150{
1151 int status;
1152
1153 if (!tp_features.bluetooth)
1154 return -ENODEV;
1155
1156 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1157 return -EIO;
1158 if (radio_on)
1159 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1160 else
1161 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1162 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1163 return -EIO;
1164
1165 return 0;
1da177e4
LT
1166}
1167
d3a6ade4 1168/* procfs -------------------------------------------------------------- */
78f81cc4 1169static int bluetooth_read(char *p)
1da177e4
LT
1170{
1171 int len = 0;
d6fdd1e9 1172 int status = bluetooth_get_radiosw();
1da177e4 1173
d8fd94d9 1174 if (!tp_features.bluetooth)
1da177e4 1175 len += sprintf(p + len, "status:\t\tnot supported\n");
1da177e4 1176 else {
d6fdd1e9
HMH
1177 len += sprintf(p + len, "status:\t\t%s\n",
1178 (status)? "enabled" : "disabled");
1da177e4
LT
1179 len += sprintf(p + len, "commands:\tenable, disable\n");
1180 }
1181
1182 return len;
1183}
1184
78f81cc4 1185static int bluetooth_write(char *buf)
1da177e4 1186{
1da177e4 1187 char *cmd;
1da177e4 1188
d8fd94d9 1189 if (!tp_features.bluetooth)
78f81cc4 1190 return -ENODEV;
1da177e4
LT
1191
1192 while ((cmd = next_cmd(&buf))) {
1193 if (strlencmp(cmd, "enable") == 0) {
d6fdd1e9 1194 bluetooth_set_radiosw(1);
1da177e4 1195 } else if (strlencmp(cmd, "disable") == 0) {
d6fdd1e9 1196 bluetooth_set_radiosw(0);
1da177e4
LT
1197 } else
1198 return -EINVAL;
1da177e4
LT
1199 }
1200
1da177e4
LT
1201 return 0;
1202}
1203
a5763f22
HMH
1204static struct ibm_struct bluetooth_driver_data = {
1205 .name = "bluetooth",
1206 .read = bluetooth_read,
1207 .write = bluetooth_write,
d3a6ade4 1208 .exit = bluetooth_exit,
a5763f22
HMH
1209};
1210
56b6aeb0
HMH
1211/*************************************************************************
1212 * Wan subdriver
1213 */
1214
d3a6ade4
HMH
1215/* sysfs wan enable ---------------------------------------------------- */
1216static ssize_t wan_enable_show(struct device *dev,
1217 struct device_attribute *attr,
1218 char *buf)
1219{
1220 int status;
1221
1222 status = wan_get_radiosw();
1223 if (status < 0)
1224 return status;
1225
1226 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1227}
1228
1229static ssize_t wan_enable_store(struct device *dev,
1230 struct device_attribute *attr,
1231 const char *buf, size_t count)
1232{
1233 unsigned long t;
1234 int res;
1235
1236 if (parse_strtoul(buf, 1, &t))
1237 return -EINVAL;
1238
1239 res = wan_set_radiosw(t);
1240
1241 return (res) ? res : count;
1242}
1243
1244static struct device_attribute dev_attr_wan_enable =
cc4c24e1 1245 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
d3a6ade4
HMH
1246 wan_enable_show, wan_enable_store);
1247
1248/* --------------------------------------------------------------------- */
1249
1250static struct attribute *wan_attributes[] = {
1251 &dev_attr_wan_enable.attr,
1252 NULL
1253};
1254
1255static const struct attribute_group wan_attr_group = {
d3a6ade4
HMH
1256 .attrs = wan_attributes,
1257};
1258
a5763f22 1259static int __init wan_init(struct ibm_init_struct *iibm)
42adb53c 1260{
d3a6ade4 1261 int res;
d6fdd1e9
HMH
1262 int status = 0;
1263
fe08bc4b
HMH
1264 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1265
8d376cd6 1266 IBM_ACPIHANDLE_INIT(hkey);
5fba344c 1267
d8fd94d9 1268 tp_features.wan = hkey_handle &&
d6fdd1e9
HMH
1269 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1270
1271 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1272 str_supported(tp_features.wan),
1273 status);
1274
d3a6ade4
HMH
1275 if (tp_features.wan) {
1276 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1277 /* no wan hardware present in system */
1278 tp_features.wan = 0;
1279 dbg_printk(TPACPI_DBG_INIT,
1280 "wan hardware not installed\n");
1281 } else {
1282 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1283 &wan_attr_group);
1284 if (res)
1285 return res;
1286 }
d6fdd1e9 1287 }
fe08bc4b 1288
d8fd94d9 1289 return (tp_features.wan)? 0 : 1;
42adb53c
JF
1290}
1291
d3a6ade4
HMH
1292static void wan_exit(void)
1293{
1294 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1295 &wan_attr_group);
1296}
1297
d6fdd1e9 1298static int wan_get_radiosw(void)
42adb53c
JF
1299{
1300 int status;
1301
d6fdd1e9
HMH
1302 if (!tp_features.wan)
1303 return -ENODEV;
42adb53c 1304
d6fdd1e9
HMH
1305 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1306 return -EIO;
1307
1308 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1309}
1310
1311static int wan_set_radiosw(int radio_on)
1312{
1313 int status;
1314
1315 if (!tp_features.wan)
1316 return -ENODEV;
1317
1318 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1319 return -EIO;
1320 if (radio_on)
1321 status |= TP_ACPI_WANCARD_RADIOSSW;
1322 else
1323 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1324 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1325 return -EIO;
1326
1327 return 0;
42adb53c
JF
1328}
1329
d3a6ade4 1330/* procfs -------------------------------------------------------------- */
42adb53c
JF
1331static int wan_read(char *p)
1332{
1333 int len = 0;
d6fdd1e9 1334 int status = wan_get_radiosw();
42adb53c 1335
d8fd94d9 1336 if (!tp_features.wan)
42adb53c 1337 len += sprintf(p + len, "status:\t\tnot supported\n");
42adb53c 1338 else {
d6fdd1e9
HMH
1339 len += sprintf(p + len, "status:\t\t%s\n",
1340 (status)? "enabled" : "disabled");
42adb53c
JF
1341 len += sprintf(p + len, "commands:\tenable, disable\n");
1342 }
1343
1344 return len;
1345}
1346
1347static int wan_write(char *buf)
1348{
42adb53c 1349 char *cmd;
42adb53c 1350
d8fd94d9 1351 if (!tp_features.wan)
42adb53c
JF
1352 return -ENODEV;
1353
1354 while ((cmd = next_cmd(&buf))) {
1355 if (strlencmp(cmd, "enable") == 0) {
d6fdd1e9 1356 wan_set_radiosw(1);
42adb53c 1357 } else if (strlencmp(cmd, "disable") == 0) {
d6fdd1e9 1358 wan_set_radiosw(0);
42adb53c
JF
1359 } else
1360 return -EINVAL;
42adb53c
JF
1361 }
1362
42adb53c
JF
1363 return 0;
1364}
1365
a5763f22
HMH
1366static struct ibm_struct wan_driver_data = {
1367 .name = "wan",
1368 .read = wan_read,
1369 .write = wan_write,
d3a6ade4 1370 .exit = wan_exit,
92641177 1371 .flags.experimental = 1,
a5763f22
HMH
1372};
1373
56b6aeb0
HMH
1374/*************************************************************************
1375 * Video subdriver
1376 */
1377
9a8e1738
HMH
1378static enum video_access_mode video_supported;
1379static int video_orig_autosw;
78f81cc4 1380
56b6aeb0
HMH
1381IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1382 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1383 "\\_SB.PCI0.VID0", /* 770e */
1384 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1385 "\\_SB.PCI0.AGP.VID", /* all others */
1386 ); /* R30, R31 */
1387
1388IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1389
a5763f22 1390static int __init video_init(struct ibm_init_struct *iibm)
1da177e4 1391{
78f81cc4
BD
1392 int ivga;
1393
fe08bc4b
HMH
1394 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1395
8d376cd6
HMH
1396 IBM_ACPIHANDLE_INIT(vid);
1397 IBM_ACPIHANDLE_INIT(vid2);
5fba344c 1398
78f81cc4
BD
1399 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1400 /* G41, assume IVGA doesn't change */
1401 vid_handle = vid2_handle;
1402
1403 if (!vid_handle)
1404 /* video switching not supported on R30, R31 */
efa27145 1405 video_supported = TPACPI_VIDEO_NONE;
78f81cc4
BD
1406 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1407 /* 570 */
efa27145 1408 video_supported = TPACPI_VIDEO_570;
78f81cc4
BD
1409 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1410 /* 600e/x, 770e, 770x */
efa27145 1411 video_supported = TPACPI_VIDEO_770;
78f81cc4
BD
1412 else
1413 /* all others */
efa27145 1414 video_supported = TPACPI_VIDEO_NEW;
1da177e4 1415
fe08bc4b
HMH
1416 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1417 str_supported(video_supported != TPACPI_VIDEO_NONE),
1418 video_supported);
1419
5fba344c 1420 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1da177e4
LT
1421}
1422
56b6aeb0
HMH
1423static void video_exit(void)
1424{
83f34724
HMH
1425 dbg_printk(TPACPI_DBG_EXIT,
1426 "restoring original video autoswitch mode\n");
1427 if (video_autosw_set(video_orig_autosw))
1428 printk(IBM_ERR "error while trying to restore original "
1429 "video autoswitch mode\n");
56b6aeb0
HMH
1430}
1431
83f34724 1432static int video_outputsw_get(void)
1da177e4
LT
1433{
1434 int status = 0;
1435 int i;
1436
83f34724
HMH
1437 switch (video_supported) {
1438 case TPACPI_VIDEO_570:
1439 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1440 TP_ACPI_VIDEO_570_PHSCMD))
1441 return -EIO;
1442 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1443 break;
1444 case TPACPI_VIDEO_770:
1445 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1446 return -EIO;
1447 if (i)
1448 status |= TP_ACPI_VIDEO_S_LCD;
1449 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1450 return -EIO;
1451 if (i)
1452 status |= TP_ACPI_VIDEO_S_CRT;
1453 break;
1454 case TPACPI_VIDEO_NEW:
1455 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1456 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1457 return -EIO;
1458 if (i)
1459 status |= TP_ACPI_VIDEO_S_CRT;
1460
1461 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1462 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1463 return -EIO;
1464 if (i)
1465 status |= TP_ACPI_VIDEO_S_LCD;
1466 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1467 return -EIO;
1468 if (i)
1469 status |= TP_ACPI_VIDEO_S_DVI;
1470 break;
1471 default:
1472 return -ENOSYS;
78f81cc4
BD
1473 }
1474
1475 return status;
1476}
1da177e4 1477
83f34724 1478static int video_outputsw_set(int status)
78f81cc4 1479{
83f34724
HMH
1480 int autosw;
1481 int res = 0;
1482
1483 switch (video_supported) {
1484 case TPACPI_VIDEO_570:
1485 res = acpi_evalf(NULL, NULL,
1486 "\\_SB.PHS2", "vdd",
1487 TP_ACPI_VIDEO_570_PHS2CMD,
1488 status | TP_ACPI_VIDEO_570_PHS2SET);
1489 break;
1490 case TPACPI_VIDEO_770:
1491 autosw = video_autosw_get();
1492 if (autosw < 0)
1493 return autosw;
1da177e4 1494
83f34724
HMH
1495 res = video_autosw_set(1);
1496 if (res)
1497 return res;
1498 res = acpi_evalf(vid_handle, NULL,
1499 "ASWT", "vdd", status * 0x100, 0);
1500 if (!autosw && video_autosw_set(autosw)) {
1501 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1502 return -EIO;
1503 }
1504 break;
1505 case TPACPI_VIDEO_NEW:
1506 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1507 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1508 break;
1509 default:
1510 return -ENOSYS;
1511 }
1da177e4 1512
83f34724 1513 return (res)? 0 : -EIO;
1da177e4
LT
1514}
1515
83f34724 1516static int video_autosw_get(void)
78f81cc4 1517{
83f34724 1518 int autosw = 0;
78f81cc4 1519
83f34724
HMH
1520 switch (video_supported) {
1521 case TPACPI_VIDEO_570:
1522 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1523 return -EIO;
1524 break;
1525 case TPACPI_VIDEO_770:
1526 case TPACPI_VIDEO_NEW:
1527 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1528 return -EIO;
1529 break;
1530 default:
1531 return -ENOSYS;
1532 }
78f81cc4 1533
83f34724 1534 return autosw & 1;
78f81cc4
BD
1535}
1536
83f34724 1537static int video_autosw_set(int enable)
78f81cc4 1538{
83f34724
HMH
1539 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1540 return -EIO;
1541 return 0;
78f81cc4
BD
1542}
1543
83f34724 1544static int video_outputsw_cycle(void)
78f81cc4 1545{
83f34724
HMH
1546 int autosw = video_autosw_get();
1547 int res;
78f81cc4 1548
83f34724
HMH
1549 if (autosw < 0)
1550 return autosw;
78f81cc4 1551
83f34724
HMH
1552 switch (video_supported) {
1553 case TPACPI_VIDEO_570:
1554 res = video_autosw_set(1);
1555 if (res)
1556 return res;
1557 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1558 break;
1559 case TPACPI_VIDEO_770:
1560 case TPACPI_VIDEO_NEW:
1561 res = video_autosw_set(1);
1562 if (res)
1563 return res;
1564 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1565 break;
1566 default:
1567 return -ENOSYS;
1568 }
1569 if (!autosw && video_autosw_set(autosw)) {
1570 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1571 return -EIO;
78f81cc4
BD
1572 }
1573
83f34724
HMH
1574 return (res)? 0 : -EIO;
1575}
1576
1577static int video_expand_toggle(void)
1578{
1579 switch (video_supported) {
1580 case TPACPI_VIDEO_570:
1581 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1582 0 : -EIO;
1583 case TPACPI_VIDEO_770:
1584 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1585 0 : -EIO;
1586 case TPACPI_VIDEO_NEW:
1587 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1588 0 : -EIO;
1589 default:
1590 return -ENOSYS;
1591 }
1592 /* not reached */
78f81cc4
BD
1593}
1594
56b6aeb0
HMH
1595static int video_read(char *p)
1596{
83f34724 1597 int status, autosw;
56b6aeb0
HMH
1598 int len = 0;
1599
83f34724 1600 if (video_supported == TPACPI_VIDEO_NONE) {
56b6aeb0
HMH
1601 len += sprintf(p + len, "status:\t\tnot supported\n");
1602 return len;
1603 }
1604
83f34724
HMH
1605 status = video_outputsw_get();
1606 if (status < 0)
1607 return status;
1608
1609 autosw = video_autosw_get();
1610 if (autosw < 0)
1611 return autosw;
1612
56b6aeb0
HMH
1613 len += sprintf(p + len, "status:\t\tsupported\n");
1614 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1615 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
efa27145 1616 if (video_supported == TPACPI_VIDEO_NEW)
56b6aeb0
HMH
1617 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1618 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1619 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1620 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
efa27145 1621 if (video_supported == TPACPI_VIDEO_NEW)
56b6aeb0
HMH
1622 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1623 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1624 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1625
1626 return len;
1627}
1628
78f81cc4 1629static int video_write(char *buf)
1da177e4
LT
1630{
1631 char *cmd;
1632 int enable, disable, status;
83f34724 1633 int res;
1da177e4 1634
83f34724 1635 if (video_supported == TPACPI_VIDEO_NONE)
78f81cc4
BD
1636 return -ENODEV;
1637
83f34724
HMH
1638 enable = 0;
1639 disable = 0;
1da177e4
LT
1640
1641 while ((cmd = next_cmd(&buf))) {
1642 if (strlencmp(cmd, "lcd_enable") == 0) {
83f34724 1643 enable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 1644 } else if (strlencmp(cmd, "lcd_disable") == 0) {
83f34724 1645 disable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 1646 } else if (strlencmp(cmd, "crt_enable") == 0) {
83f34724 1647 enable |= TP_ACPI_VIDEO_S_CRT;
1da177e4 1648 } else if (strlencmp(cmd, "crt_disable") == 0) {
83f34724 1649 disable |= TP_ACPI_VIDEO_S_CRT;
efa27145 1650 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 1651 strlencmp(cmd, "dvi_enable") == 0) {
83f34724 1652 enable |= TP_ACPI_VIDEO_S_DVI;
efa27145 1653 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 1654 strlencmp(cmd, "dvi_disable") == 0) {
83f34724 1655 disable |= TP_ACPI_VIDEO_S_DVI;
1da177e4 1656 } else if (strlencmp(cmd, "auto_enable") == 0) {
83f34724
HMH
1657 res = video_autosw_set(1);
1658 if (res)
1659 return res;
1da177e4 1660 } else if (strlencmp(cmd, "auto_disable") == 0) {
83f34724
HMH
1661 res = video_autosw_set(0);
1662 if (res)
1663 return res;
1da177e4 1664 } else if (strlencmp(cmd, "video_switch") == 0) {
83f34724
HMH
1665 res = video_outputsw_cycle();
1666 if (res)
1667 return res;
1da177e4 1668 } else if (strlencmp(cmd, "expand_toggle") == 0) {
83f34724
HMH
1669 res = video_expand_toggle();
1670 if (res)
1671 return res;
1da177e4
LT
1672 } else
1673 return -EINVAL;
1674 }
1675
1676 if (enable || disable) {
83f34724
HMH
1677 status = video_outputsw_get();
1678 if (status < 0)
1679 return status;
1680 res = video_outputsw_set((status & ~disable) | enable);
1681 if (res)
1682 return res;
1da177e4
LT
1683 }
1684
1685 return 0;
1686}
1687
a5763f22
HMH
1688static struct ibm_struct video_driver_data = {
1689 .name = "video",
1690 .read = video_read,
1691 .write = video_write,
1692 .exit = video_exit,
1693};
1694
56b6aeb0
HMH
1695/*************************************************************************
1696 * Light (thinklight) subdriver
1697 */
1da177e4 1698
56b6aeb0
HMH
1699IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1700IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1701
a5763f22 1702static int __init light_init(struct ibm_init_struct *iibm)
1da177e4 1703{
fe08bc4b
HMH
1704 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1705
8d376cd6
HMH
1706 IBM_ACPIHANDLE_INIT(ledb);
1707 IBM_ACPIHANDLE_INIT(lght);
1708 IBM_ACPIHANDLE_INIT(cmos);
5fba344c 1709
78f81cc4 1710 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
d8fd94d9 1711 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
78f81cc4 1712
d8fd94d9 1713 if (tp_features.light)
78f81cc4
BD
1714 /* light status not supported on
1715 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
d8fd94d9
HMH
1716 tp_features.light_status =
1717 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1da177e4 1718
fe08bc4b 1719 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
d8fd94d9 1720 str_supported(tp_features.light));
fe08bc4b 1721
d8fd94d9 1722 return (tp_features.light)? 0 : 1;
1da177e4
LT
1723}
1724
78f81cc4 1725static int light_read(char *p)
1da177e4
LT
1726{
1727 int len = 0;
1728 int status = 0;
1729
d8fd94d9 1730 if (!tp_features.light) {
78f81cc4 1731 len += sprintf(p + len, "status:\t\tnot supported\n");
d8fd94d9 1732 } else if (!tp_features.light_status) {
78f81cc4
BD
1733 len += sprintf(p + len, "status:\t\tunknown\n");
1734 len += sprintf(p + len, "commands:\ton, off\n");
1735 } else {
1da177e4
LT
1736 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1737 return -EIO;
1738 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
78f81cc4
BD
1739 len += sprintf(p + len, "commands:\ton, off\n");
1740 }
1da177e4
LT
1741
1742 return len;
1743}
1744
78f81cc4 1745static int light_write(char *buf)
1da177e4
LT
1746{
1747 int cmos_cmd, lght_cmd;
1748 char *cmd;
1749 int success;
78f81cc4 1750
d8fd94d9 1751 if (!tp_features.light)
78f81cc4
BD
1752 return -ENODEV;
1753
1da177e4
LT
1754 while ((cmd = next_cmd(&buf))) {
1755 if (strlencmp(cmd, "on") == 0) {
1756 cmos_cmd = 0x0c;
1757 lght_cmd = 1;
1758 } else if (strlencmp(cmd, "off") == 0) {
1759 cmos_cmd = 0x0d;
1760 lght_cmd = 0;
1761 } else
1762 return -EINVAL;
78f81cc4 1763
1da177e4 1764 success = cmos_handle ?
78f81cc4
BD
1765 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1766 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1da177e4
LT
1767 if (!success)
1768 return -EIO;
1769 }
1770
1771 return 0;
1772}
1773
a5763f22
HMH
1774static struct ibm_struct light_driver_data = {
1775 .name = "light",
1776 .read = light_read,
1777 .write = light_write,
1778};
1779
56b6aeb0
HMH
1780/*************************************************************************
1781 * Dock subdriver
1782 */
1da177e4 1783
85998248 1784#ifdef CONFIG_THINKPAD_ACPI_DOCK
56b6aeb0
HMH
1785
1786IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1787 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1788 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1789 "\\_SB.PCI.ISA.SLCE", /* 570 */
1790 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1791
5fba344c
HMH
1792/* don't list other alternatives as we install a notify handler on the 570 */
1793IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1794
d94a7f16
HMH
1795static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1796 {
1797 .notify = dock_notify,
1798 .handle = &dock_handle,
1799 .type = ACPI_SYSTEM_NOTIFY,
1800 },
1801 {
1802 .hid = IBM_PCI_HID,
1803 .notify = dock_notify,
1804 .handle = &pci_handle,
1805 .type = ACPI_SYSTEM_NOTIFY,
1806 },
1807};
1808
1809static struct ibm_struct dock_driver_data[2] = {
1810 {
1811 .name = "dock",
1812 .read = dock_read,
1813 .write = dock_write,
1814 .acpi = &ibm_dock_acpidriver[0],
1815 },
1816 {
1817 .name = "dock",
1818 .acpi = &ibm_dock_acpidriver[1],
1819 },
1820};
1821
1da177e4
LT
1822#define dock_docked() (_sta(dock_handle) & 1)
1823
a5763f22 1824static int __init dock_init(struct ibm_init_struct *iibm)
5fba344c 1825{
fe08bc4b
HMH
1826 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1827
8d376cd6 1828 IBM_ACPIHANDLE_INIT(dock);
5fba344c 1829
fe08bc4b
HMH
1830 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1831 str_supported(dock_handle != NULL));
1832
5fba344c
HMH
1833 return (dock_handle)? 0 : 1;
1834}
1835
d94a7f16
HMH
1836static int __init dock_init2(struct ibm_init_struct *iibm)
1837{
1838 int dock2_needed;
1839
1840 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
1841
1842 if (dock_driver_data[0].flags.acpi_driver_registered &&
1843 dock_driver_data[0].flags.acpi_notify_installed) {
1844 IBM_ACPIHANDLE_INIT(pci);
1845 dock2_needed = (pci_handle != NULL);
1846 vdbg_printk(TPACPI_DBG_INIT,
1847 "dock PCI handler for the TP 570 is %s\n",
1848 str_supported(dock2_needed));
1849 } else {
1850 vdbg_printk(TPACPI_DBG_INIT,
1851 "dock subdriver part 2 not required\n");
1852 dock2_needed = 0;
1853 }
1854
1855 return (dock2_needed)? 0 : 1;
1856}
1857
56b6aeb0
HMH
1858static void dock_notify(struct ibm_struct *ibm, u32 event)
1859{
1860 int docked = dock_docked();
8d376cd6 1861 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
56b6aeb0
HMH
1862
1863 if (event == 1 && !pci) /* 570 */
8d376cd6 1864 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
56b6aeb0 1865 else if (event == 1 && pci) /* 570 */
8d376cd6 1866 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
56b6aeb0 1867 else if (event == 3 && docked)
8d376cd6 1868 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
56b6aeb0 1869 else if (event == 3 && !docked)
8d376cd6 1870 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
56b6aeb0 1871 else if (event == 0 && docked)
8d376cd6 1872 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
56b6aeb0
HMH
1873 else {
1874 printk(IBM_ERR "unknown dock event %d, status %d\n",
1875 event, _sta(dock_handle));
8d376cd6 1876 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
56b6aeb0
HMH
1877 }
1878}
1879
78f81cc4 1880static int dock_read(char *p)
1da177e4
LT
1881{
1882 int len = 0;
1883 int docked = dock_docked();
1884
1885 if (!dock_handle)
1886 len += sprintf(p + len, "status:\t\tnot supported\n");
1887 else if (!docked)
1888 len += sprintf(p + len, "status:\t\tundocked\n");
1889 else {
1890 len += sprintf(p + len, "status:\t\tdocked\n");
1891 len += sprintf(p + len, "commands:\tdock, undock\n");
1892 }
1893
1894 return len;
1895}
1896
78f81cc4 1897static int dock_write(char *buf)
1da177e4
LT
1898{
1899 char *cmd;
1900
1901 if (!dock_docked())
78f81cc4 1902 return -ENODEV;
1da177e4
LT
1903
1904 while ((cmd = next_cmd(&buf))) {
1905 if (strlencmp(cmd, "undock") == 0) {
78f81cc4
BD
1906 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1907 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
56b6aeb0
HMH
1908 return -EIO;
1909 } else if (strlencmp(cmd, "dock") == 0) {
1910 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1911 return -EIO;
1912 } else
1913 return -EINVAL;
1da177e4 1914 }
56b6aeb0
HMH
1915
1916 return 0;
1da177e4 1917}
56b6aeb0 1918
85998248 1919#endif /* CONFIG_THINKPAD_ACPI_DOCK */
56b6aeb0
HMH
1920
1921/*************************************************************************
1922 * Bay subdriver
1923 */
1da177e4 1924
85998248 1925#ifdef CONFIG_THINKPAD_ACPI_BAY
56b6aeb0
HMH
1926IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1927 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1928 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1929 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1930 ); /* A21e, R30, R31 */
1931IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1932 "_EJ0", /* all others */
1933 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1934IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1935 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1936 ); /* all others */
1937IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1938 "_EJ0", /* 770x */
1939 ); /* all others */
1940
a5763f22 1941static int __init bay_init(struct ibm_init_struct *iibm)
1da177e4 1942{
fe08bc4b
HMH
1943 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1944
8d376cd6 1945 IBM_ACPIHANDLE_INIT(bay);
5fba344c 1946 if (bay_handle)
8d376cd6
HMH
1947 IBM_ACPIHANDLE_INIT(bay_ej);
1948 IBM_ACPIHANDLE_INIT(bay2);
5fba344c 1949 if (bay2_handle)
8d376cd6 1950 IBM_ACPIHANDLE_INIT(bay2_ej);
5fba344c 1951
d8fd94d9
HMH
1952 tp_features.bay_status = bay_handle &&
1953 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1954 tp_features.bay_status2 = bay2_handle &&
1955 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
78f81cc4 1956
d8fd94d9
HMH
1957 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1958 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1959 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1960 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1da177e4 1961
fe08bc4b
HMH
1962 vdbg_printk(TPACPI_DBG_INIT,
1963 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
d8fd94d9
HMH
1964 str_supported(tp_features.bay_status),
1965 str_supported(tp_features.bay_eject),
1966 str_supported(tp_features.bay_status2),
1967 str_supported(tp_features.bay_eject2));
fe08bc4b 1968
d8fd94d9
HMH
1969 return (tp_features.bay_status || tp_features.bay_eject ||
1970 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1da177e4
LT
1971}
1972
56b6aeb0
HMH
1973static void bay_notify(struct ibm_struct *ibm, u32 event)
1974{
8d376cd6 1975 acpi_bus_generate_event(ibm->acpi->device, event, 0);
56b6aeb0
HMH
1976}
1977
78f81cc4
BD
1978#define bay_occupied(b) (_sta(b##_handle) & 1)
1979
1980static int bay_read(char *p)
1da177e4
LT
1981{
1982 int len = 0;
78f81cc4
BD
1983 int occupied = bay_occupied(bay);
1984 int occupied2 = bay_occupied(bay2);
1985 int eject, eject2;
1986
d8fd94d9
HMH
1987 len += sprintf(p + len, "status:\t\t%s\n",
1988 tp_features.bay_status ?
1989 (occupied ? "occupied" : "unoccupied") :
1990 "not supported");
1991 if (tp_features.bay_status2)
78f81cc4
BD
1992 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1993 "occupied" : "unoccupied");
1994
d8fd94d9
HMH
1995 eject = tp_features.bay_eject && occupied;
1996 eject2 = tp_features.bay_eject2 && occupied2;
78f81cc4
BD
1997
1998 if (eject && eject2)
1999 len += sprintf(p + len, "commands:\teject, eject2\n");
2000 else if (eject)
1da177e4 2001 len += sprintf(p + len, "commands:\teject\n");
78f81cc4
BD
2002 else if (eject2)
2003 len += sprintf(p + len, "commands:\teject2\n");
1da177e4
LT
2004
2005 return len;
2006}
2007
78f81cc4 2008static int bay_write(char *buf)
1da177e4
LT
2009{
2010 char *cmd;
2011
d8fd94d9 2012 if (!tp_features.bay_eject && !tp_features.bay_eject2)
78f81cc4
BD
2013 return -ENODEV;
2014
1da177e4 2015 while ((cmd = next_cmd(&buf))) {
d8fd94d9 2016 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
78f81cc4
BD
2017 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2018 return -EIO;
d8fd94d9 2019 } else if (tp_features.bay_eject2 &&
78f81cc4
BD
2020 strlencmp(cmd, "eject2") == 0) {
2021 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1da177e4
LT
2022 return -EIO;
2023 } else
2024 return -EINVAL;
2025 }
2026
2027 return 0;
78f81cc4 2028}
a5763f22 2029
8d376cd6
HMH
2030static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2031 .notify = bay_notify,
2032 .handle = &bay_handle,
2033 .type = ACPI_SYSTEM_NOTIFY,
2034};
2035
a5763f22
HMH
2036static struct ibm_struct bay_driver_data = {
2037 .name = "bay",
2038 .read = bay_read,
2039 .write = bay_write,
8d376cd6 2040 .acpi = &ibm_bay_acpidriver,
a5763f22
HMH
2041};
2042
85998248 2043#endif /* CONFIG_THINKPAD_ACPI_BAY */
1da177e4 2044
56b6aeb0
HMH
2045/*************************************************************************
2046 * CMOS subdriver
2047 */
2048
b616004c
HMH
2049/* sysfs cmos_command -------------------------------------------------- */
2050static ssize_t cmos_command_store(struct device *dev,
2051 struct device_attribute *attr,
2052 const char *buf, size_t count)
2053{
2054 unsigned long cmos_cmd;
2055 int res;
2056
2057 if (parse_strtoul(buf, 21, &cmos_cmd))
2058 return -EINVAL;
2059
2060 res = issue_thinkpad_cmos_command(cmos_cmd);
2061 return (res)? res : count;
2062}
2063
2064static struct device_attribute dev_attr_cmos_command =
2065 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2066
2067/* --------------------------------------------------------------------- */
2068
a5763f22 2069static int __init cmos_init(struct ibm_init_struct *iibm)
5fba344c 2070{
b616004c
HMH
2071 int res;
2072
fe08bc4b
HMH
2073 vdbg_printk(TPACPI_DBG_INIT,
2074 "initializing cmos commands subdriver\n");
2075
8d376cd6 2076 IBM_ACPIHANDLE_INIT(cmos);
5fba344c 2077
fe08bc4b
HMH
2078 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2079 str_supported(cmos_handle != NULL));
b616004c
HMH
2080
2081 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2082 if (res)
2083 return res;
2084
5fba344c
HMH
2085 return (cmos_handle)? 0 : 1;
2086}
2087
b616004c
HMH
2088static void cmos_exit(void)
2089{
2090 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2091}
2092
78f81cc4 2093static int cmos_read(char *p)
1da177e4
LT
2094{
2095 int len = 0;
2096
78f81cc4
BD
2097 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2098 R30, R31, T20-22, X20-21 */
1da177e4
LT
2099 if (!cmos_handle)
2100 len += sprintf(p + len, "status:\t\tnot supported\n");
2101 else {
2102 len += sprintf(p + len, "status:\t\tsupported\n");
78f81cc4 2103 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1da177e4
LT
2104 }
2105
2106 return len;
2107}
2108
78f81cc4 2109static int cmos_write(char *buf)
1da177e4
LT
2110{
2111 char *cmd;
c9bea99c 2112 int cmos_cmd, res;
1da177e4
LT
2113
2114 while ((cmd = next_cmd(&buf))) {
78f81cc4
BD
2115 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2116 cmos_cmd >= 0 && cmos_cmd <= 21) {
1da177e4
LT
2117 /* cmos_cmd set */
2118 } else
2119 return -EINVAL;
2120
c9bea99c
HMH
2121 res = issue_thinkpad_cmos_command(cmos_cmd);
2122 if (res)
2123 return res;
1da177e4
LT
2124 }
2125
2126 return 0;
78f81cc4
BD
2127}
2128
a5763f22
HMH
2129static struct ibm_struct cmos_driver_data = {
2130 .name = "cmos",
2131 .read = cmos_read,
2132 .write = cmos_write,
b616004c 2133 .exit = cmos_exit,
a5763f22 2134};
56b6aeb0
HMH
2135
2136/*************************************************************************
2137 * LED subdriver
2138 */
2139
9a8e1738 2140static enum led_access_mode led_supported;
78f81cc4 2141
56b6aeb0
HMH
2142IBM_HANDLE(led, ec, "SLED", /* 570 */
2143 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2144 "LED", /* all others */
2145 ); /* R30, R31 */
2146
a5763f22 2147static int __init led_init(struct ibm_init_struct *iibm)
78f81cc4 2148{
fe08bc4b
HMH
2149 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2150
8d376cd6 2151 IBM_ACPIHANDLE_INIT(led);
5fba344c 2152
78f81cc4
BD
2153 if (!led_handle)
2154 /* led not supported on R30, R31 */
efa27145 2155 led_supported = TPACPI_LED_NONE;
78f81cc4
BD
2156 else if (strlencmp(led_path, "SLED") == 0)
2157 /* 570 */
efa27145 2158 led_supported = TPACPI_LED_570;
78f81cc4
BD
2159 else if (strlencmp(led_path, "SYSL") == 0)
2160 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
efa27145 2161 led_supported = TPACPI_LED_OLD;
78f81cc4
BD
2162 else
2163 /* all others */
efa27145 2164 led_supported = TPACPI_LED_NEW;
78f81cc4 2165
fe08bc4b
HMH
2166 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2167 str_supported(led_supported), led_supported);
2168
5fba344c 2169 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
78f81cc4
BD
2170}
2171
2172#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2173
2174static int led_read(char *p)
1da177e4
LT
2175{
2176 int len = 0;
2177
78f81cc4
BD
2178 if (!led_supported) {
2179 len += sprintf(p + len, "status:\t\tnot supported\n");
2180 return len;
2181 }
2182 len += sprintf(p + len, "status:\t\tsupported\n");
2183
efa27145 2184 if (led_supported == TPACPI_LED_570) {
78f81cc4
BD
2185 /* 570 */
2186 int i, status;
2187 for (i = 0; i < 8; i++) {
2188 if (!acpi_evalf(ec_handle,
2189 &status, "GLED", "dd", 1 << i))
2190 return -EIO;
2191 len += sprintf(p + len, "%d:\t\t%s\n",
2192 i, led_status(status));
2193 }
2194 }
2195
1da177e4 2196 len += sprintf(p + len, "commands:\t"
78f81cc4 2197 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1da177e4
LT
2198
2199 return len;
2200}
2201
78f81cc4
BD
2202/* off, on, blink */
2203static const int led_sled_arg1[] = { 0, 1, 3 };
2204static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2205static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2206static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2207
78f81cc4 2208static int led_write(char *buf)
1da177e4
LT
2209{
2210 char *cmd;
78f81cc4
BD
2211 int led, ind, ret;
2212
2213 if (!led_supported)
2214 return -ENODEV;
1da177e4
LT
2215
2216 while ((cmd = next_cmd(&buf))) {
78f81cc4 2217 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1da177e4
LT
2218 return -EINVAL;
2219
78f81cc4
BD
2220 if (strstr(cmd, "off")) {
2221 ind = 0;
1da177e4 2222 } else if (strstr(cmd, "on")) {
78f81cc4
BD
2223 ind = 1;
2224 } else if (strstr(cmd, "blink")) {
2225 ind = 2;
1da177e4
LT
2226 } else
2227 return -EINVAL;
78f81cc4 2228
efa27145 2229 if (led_supported == TPACPI_LED_570) {
78f81cc4
BD
2230 /* 570 */
2231 led = 1 << led;
1da177e4 2232 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
78f81cc4 2233 led, led_sled_arg1[ind]))
1da177e4 2234 return -EIO;
efa27145 2235 } else if (led_supported == TPACPI_LED_OLD) {
78f81cc4
BD
2236 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2237 led = 1 << led;
efa27145 2238 ret = ec_write(TPACPI_LED_EC_HLMS, led);
78f81cc4
BD
2239 if (ret >= 0)
2240 ret =
efa27145 2241 ec_write(TPACPI_LED_EC_HLBL,
e062e034 2242 led * led_exp_hlbl[ind]);
78f81cc4
BD
2243 if (ret >= 0)
2244 ret =
efa27145 2245 ec_write(TPACPI_LED_EC_HLCL,
e062e034 2246 led * led_exp_hlcl[ind]);
78f81cc4
BD
2247 if (ret < 0)
2248 return ret;
2249 } else {
2250 /* all others */
2251 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2252 led, led_led_arg1[ind]))
1da177e4 2253 return -EIO;
78f81cc4
BD
2254 }
2255 }
2256
2257 return 0;
2258}
2259
a5763f22
HMH
2260static struct ibm_struct led_driver_data = {
2261 .name = "led",
2262 .read = led_read,
2263 .write = led_write,
2264};
2265
56b6aeb0
HMH
2266/*************************************************************************
2267 * Beep subdriver
2268 */
2269
2270IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2271
a5763f22 2272static int __init beep_init(struct ibm_init_struct *iibm)
5fba344c 2273{
fe08bc4b
HMH
2274 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2275
8d376cd6 2276 IBM_ACPIHANDLE_INIT(beep);
5fba344c 2277
fe08bc4b
HMH
2278 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2279 str_supported(beep_handle != NULL));
2280
5fba344c
HMH
2281 return (beep_handle)? 0 : 1;
2282}
2283
78f81cc4
BD
2284static int beep_read(char *p)
2285{
2286 int len = 0;
2287
2288 if (!beep_handle)
2289 len += sprintf(p + len, "status:\t\tnot supported\n");
2290 else {
2291 len += sprintf(p + len, "status:\t\tsupported\n");
2292 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2293 }
2294
2295 return len;
2296}
2297
2298static int beep_write(char *buf)
2299{
2300 char *cmd;
2301 int beep_cmd;
2302
2303 if (!beep_handle)
2304 return -ENODEV;
2305
2306 while ((cmd = next_cmd(&buf))) {
2307 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2308 beep_cmd >= 0 && beep_cmd <= 17) {
2309 /* beep_cmd set */
2310 } else
2311 return -EINVAL;
2312 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2313 return -EIO;
2314 }
2315
2316 return 0;
2317}
2318
a5763f22
HMH
2319static struct ibm_struct beep_driver_data = {
2320 .name = "beep",
2321 .read = beep_read,
2322 .write = beep_write,
2323};
2324
56b6aeb0
HMH
2325/*************************************************************************
2326 * Thermal subdriver
2327 */
78f81cc4 2328
a26f878a 2329static enum thermal_access_mode thermal_read_mode;
78f81cc4 2330
2c37aa4e
HMH
2331/* sysfs temp##_input -------------------------------------------------- */
2332
2333static ssize_t thermal_temp_input_show(struct device *dev,
2334 struct device_attribute *attr,
2335 char *buf)
2336{
2337 struct sensor_device_attribute *sensor_attr =
2338 to_sensor_dev_attr(attr);
2339 int idx = sensor_attr->index;
2340 s32 value;
2341 int res;
2342
2343 res = thermal_get_sensor(idx, &value);
2344 if (res)
2345 return res;
2346 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2347 return -ENXIO;
2348
2349 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2350}
2351
2352#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2353 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2354
2355static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2356 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2357 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2358 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2359 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2360 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2361 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2362 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2363 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2364 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2365 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2366 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2367 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2368 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2369 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2370 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2371 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2372};
2373
2374#define THERMAL_ATTRS(X) \
2375 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2376
2377static struct attribute *thermal_temp_input_attr[] = {
2378 THERMAL_ATTRS(8),
2379 THERMAL_ATTRS(9),
2380 THERMAL_ATTRS(10),
2381 THERMAL_ATTRS(11),
2382 THERMAL_ATTRS(12),
2383 THERMAL_ATTRS(13),
2384 THERMAL_ATTRS(14),
2385 THERMAL_ATTRS(15),
2386 THERMAL_ATTRS(0),
2387 THERMAL_ATTRS(1),
2388 THERMAL_ATTRS(2),
2389 THERMAL_ATTRS(3),
2390 THERMAL_ATTRS(4),
2391 THERMAL_ATTRS(5),
2392 THERMAL_ATTRS(6),
2393 THERMAL_ATTRS(7),
2394 NULL
2395};
2396
2397static const struct attribute_group thermal_temp_input16_group = {
2398 .attrs = thermal_temp_input_attr
2399};
2400
2401static const struct attribute_group thermal_temp_input8_group = {
2402 .attrs = &thermal_temp_input_attr[8]
2403};
2404
2405#undef THERMAL_SENSOR_ATTR_TEMP
2406#undef THERMAL_ATTRS
2407
2408/* --------------------------------------------------------------------- */
2409
a5763f22 2410static int __init thermal_init(struct ibm_init_struct *iibm)
78f81cc4 2411{
60eb0b35
HMH
2412 u8 t, ta1, ta2;
2413 int i;
5fba344c 2414 int acpi_tmp7;
2c37aa4e 2415 int res;
5fba344c 2416
fe08bc4b
HMH
2417 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2418
5fba344c 2419 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
60eb0b35
HMH
2420
2421 if (ibm_thinkpad_ec_found && experimental) {
2422 /*
2423 * Direct EC access mode: sensors at registers
2424 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2425 * non-implemented, thermal sensors return 0x80 when
2426 * not available
2427 */
78f81cc4 2428
60eb0b35
HMH
2429 ta1 = ta2 = 0;
2430 for (i = 0; i < 8; i++) {
04cc862c 2431 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
60eb0b35
HMH
2432 ta1 |= t;
2433 } else {
2434 ta1 = 0;
2435 break;
2436 }
04cc862c 2437 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
60eb0b35
HMH
2438 ta2 |= t;
2439 } else {
2440 ta1 = 0;
2441 break;
2442 }
2443 }
2444 if (ta1 == 0) {
2445 /* This is sheer paranoia, but we handle it anyway */
2446 if (acpi_tmp7) {
2447 printk(IBM_ERR
2448 "ThinkPad ACPI EC access misbehaving, "
2449 "falling back to ACPI TMPx access mode\n");
efa27145 2450 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
60eb0b35
HMH
2451 } else {
2452 printk(IBM_ERR
2453 "ThinkPad ACPI EC access misbehaving, "
2454 "disabling thermal sensors access\n");
efa27145 2455 thermal_read_mode = TPACPI_THERMAL_NONE;
60eb0b35
HMH
2456 }
2457 } else {
2458 thermal_read_mode =
2459 (ta2 != 0) ?
efa27145 2460 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
60eb0b35
HMH
2461 }
2462 } else if (acpi_tmp7) {
a26f878a
HMH
2463 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2464 /* 600e/x, 770e, 770x */
efa27145 2465 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
a26f878a
HMH
2466 } else {
2467 /* Standard ACPI TMPx access, max 8 sensors */
efa27145 2468 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
a26f878a
HMH
2469 }
2470 } else {
2471 /* temperatures not supported on 570, G4x, R30, R31, R32 */
efa27145 2472 thermal_read_mode = TPACPI_THERMAL_NONE;
a26f878a 2473 }
78f81cc4 2474
fe08bc4b
HMH
2475 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2476 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2477 thermal_read_mode);
2478
2c37aa4e
HMH
2479 switch(thermal_read_mode) {
2480 case TPACPI_THERMAL_TPEC_16:
2481 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2482 &thermal_temp_input16_group);
2483 if (res)
2484 return res;
2485 break;
2486 case TPACPI_THERMAL_TPEC_8:
2487 case TPACPI_THERMAL_ACPI_TMP07:
2488 case TPACPI_THERMAL_ACPI_UPDT:
2489 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2490 &thermal_temp_input8_group);
2491 if (res)
2492 return res;
2493 break;
2494 case TPACPI_THERMAL_NONE:
2495 default:
2496 return 1;
2497 }
2498
2499 return 0;
2500}
2501
2502static void thermal_exit(void)
2503{
2504 switch(thermal_read_mode) {
2505 case TPACPI_THERMAL_TPEC_16:
2506 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2507 &thermal_temp_input16_group);
2508 break;
2509 case TPACPI_THERMAL_TPEC_8:
2510 case TPACPI_THERMAL_ACPI_TMP07:
2511 case TPACPI_THERMAL_ACPI_UPDT:
2512 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2513 &thermal_temp_input16_group);
2514 break;
2515 case TPACPI_THERMAL_NONE:
2516 default:
2517 break;
2518 }
78f81cc4
BD
2519}
2520
04cc862c
HMH
2521/* idx is zero-based */
2522static int thermal_get_sensor(int idx, s32 *value)
78f81cc4 2523{
04cc862c 2524 int t;
60eb0b35 2525 s8 tmp;
04cc862c 2526 char tmpi[5];
78f81cc4 2527
04cc862c 2528 t = TP_EC_THERMAL_TMP0;
78f81cc4 2529
a26f878a 2530 switch (thermal_read_mode) {
efa27145
HMH
2531#if TPACPI_MAX_THERMAL_SENSORS >= 16
2532 case TPACPI_THERMAL_TPEC_16:
04cc862c
HMH
2533 if (idx >= 8 && idx <= 15) {
2534 t = TP_EC_THERMAL_TMP8;
2535 idx -= 8;
60eb0b35
HMH
2536 }
2537 /* fallthrough */
2538#endif
efa27145 2539 case TPACPI_THERMAL_TPEC_8:
04cc862c
HMH
2540 if (idx <= 7) {
2541 if (!acpi_ec_read(t + idx, &tmp))
78f81cc4 2542 return -EIO;
04cc862c
HMH
2543 *value = tmp * 1000;
2544 return 0;
60eb0b35 2545 }
04cc862c 2546 break;
78f81cc4 2547
efa27145 2548 case TPACPI_THERMAL_ACPI_UPDT:
04cc862c
HMH
2549 if (idx <= 7) {
2550 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2551 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2552 return -EIO;
78f81cc4
BD
2553 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2554 return -EIO;
04cc862c
HMH
2555 *value = (t - 2732) * 100;
2556 return 0;
78f81cc4 2557 }
04cc862c 2558 break;
78f81cc4 2559
efa27145 2560 case TPACPI_THERMAL_ACPI_TMP07:
04cc862c
HMH
2561 if (idx <= 7) {
2562 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
78f81cc4
BD
2563 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2564 return -EIO;
04cc862c
HMH
2565 *value = t * 1000;
2566 return 0;
78f81cc4 2567 }
04cc862c 2568 break;
78f81cc4 2569
efa27145 2570 case TPACPI_THERMAL_NONE:
a26f878a 2571 default:
04cc862c
HMH
2572 return -ENOSYS;
2573 }
2574
2575 return -EINVAL;
2576}
2577
2578static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2579{
2580 int res, i;
2581 int n;
2582
2583 n = 8;
2584 i = 0;
2585
2586 if (!s)
2587 return -EINVAL;
2588
2589 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2590 n = 16;
2591
2592 for(i = 0 ; i < n; i++) {
2593 res = thermal_get_sensor(i, &s->temp[i]);
2594 if (res)
2595 return res;
78f81cc4 2596 }
04cc862c
HMH
2597
2598 return n;
a26f878a
HMH
2599}
2600
2601static int thermal_read(char *p)
2602{
2603 int len = 0;
2604 int n, i;
2605 struct ibm_thermal_sensors_struct t;
2606
2607 n = thermal_get_sensors(&t);
2608 if (unlikely(n < 0))
2609 return n;
2610
2611 len += sprintf(p + len, "temperatures:\t");
2612
2613 if (n > 0) {
2614 for (i = 0; i < (n - 1); i++)
2615 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2616 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2617 } else
2618 len += sprintf(p + len, "not supported\n");
78f81cc4
BD
2619
2620 return len;
2621}
2622
a5763f22
HMH
2623static struct ibm_struct thermal_driver_data = {
2624 .name = "thermal",
2625 .read = thermal_read,
2c37aa4e 2626 .exit = thermal_exit,
a5763f22
HMH
2627};
2628
56b6aeb0
HMH
2629/*************************************************************************
2630 * EC Dump subdriver
2631 */
2632
78f81cc4
BD
2633static u8 ecdump_regs[256];
2634
2635static int ecdump_read(char *p)
2636{
2637 int len = 0;
2638 int i, j;
2639 u8 v;
2640
2641 len += sprintf(p + len, "EC "
2642 " +00 +01 +02 +03 +04 +05 +06 +07"
2643 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2644 for (i = 0; i < 256; i += 16) {
2645 len += sprintf(p + len, "EC 0x%02x:", i);
2646 for (j = 0; j < 16; j++) {
2647 if (!acpi_ec_read(i + j, &v))
2648 break;
2649 if (v != ecdump_regs[i + j])
2650 len += sprintf(p + len, " *%02x", v);
2651 else
2652 len += sprintf(p + len, " %02x", v);
2653 ecdump_regs[i + j] = v;
2654 }
2655 len += sprintf(p + len, "\n");
2656 if (j != 16)
2657 break;
2658 }
2659
2660 /* These are way too dangerous to advertise openly... */
2661#if 0
2662 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2663 " (<offset> is 00-ff, <value> is 00-ff)\n");
2664 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2665 " (<offset> is 00-ff, <value> is 0-255)\n");
2666#endif
2667 return len;
2668}
2669
2670static int ecdump_write(char *buf)
2671{
2672 char *cmd;
2673 int i, v;
2674
2675 while ((cmd = next_cmd(&buf))) {
2676 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2677 /* i and v set */
2678 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2679 /* i and v set */
2680 } else
2681 return -EINVAL;
2682 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2683 if (!acpi_ec_write(i, v))
1da177e4
LT
2684 return -EIO;
2685 } else
2686 return -EINVAL;
2687 }
2688
2689 return 0;
78f81cc4
BD
2690}
2691
a5763f22
HMH
2692static struct ibm_struct ecdump_driver_data = {
2693 .name = "ecdump",
2694 .read = ecdump_read,
2695 .write = ecdump_write,
92641177 2696 .flags.experimental = 1,
a5763f22
HMH
2697};
2698
56b6aeb0
HMH
2699/*************************************************************************
2700 * Backlight/brightness subdriver
2701 */
2702
94954cc6 2703static struct backlight_device *ibm_backlight_device;
56b6aeb0
HMH
2704
2705static struct backlight_ops ibm_backlight_data = {
2706 .get_brightness = brightness_get,
2707 .update_status = brightness_update_status,
2708};
2709
a5763f22 2710static int __init brightness_init(struct ibm_init_struct *iibm)
56b6aeb0
HMH
2711{
2712 int b;
2713
fe08bc4b
HMH
2714 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2715
56b6aeb0
HMH
2716 b = brightness_get(NULL);
2717 if (b < 0)
2718 return b;
2719
7d5a015e
HMH
2720 ibm_backlight_device = backlight_device_register(
2721 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2722 &ibm_backlight_data);
56b6aeb0
HMH
2723 if (IS_ERR(ibm_backlight_device)) {
2724 printk(IBM_ERR "Could not register backlight device\n");
2725 return PTR_ERR(ibm_backlight_device);
2726 }
fe08bc4b 2727 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
56b6aeb0
HMH
2728
2729 ibm_backlight_device->props.max_brightness = 7;
2730 ibm_backlight_device->props.brightness = b;
2731 backlight_update_status(ibm_backlight_device);
2732
2733 return 0;
2734}
2735
2736static void brightness_exit(void)
2737{
2738 if (ibm_backlight_device) {
fe08bc4b
HMH
2739 vdbg_printk(TPACPI_DBG_EXIT,
2740 "calling backlight_device_unregister()\n");
56b6aeb0
HMH
2741 backlight_device_unregister(ibm_backlight_device);
2742 ibm_backlight_device = NULL;
2743 }
2744}
2745
2746static int brightness_update_status(struct backlight_device *bd)
2747{
2748 return brightness_set(
2749 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2750 bd->props.power == FB_BLANK_UNBLANK) ?
2751 bd->props.brightness : 0);
2752}
2753
8acb0250
HM
2754static int brightness_get(struct backlight_device *bd)
2755{
8d297264
HMH
2756 u8 level;
2757 if (!acpi_ec_read(brightness_offset, &level))
2758 return -EIO;
8acb0250 2759
8d297264 2760 level &= 0x7;
fb87a811 2761
8d297264 2762 return level;
8acb0250
HM
2763}
2764
8acb0250 2765static int brightness_set(int value)
1da177e4 2766{
78f81cc4 2767 int cmos_cmd, inc, i;
8acb0250
HM
2768 int current_value = brightness_get(NULL);
2769
2770 value &= 7;
2771
56b6aeb0 2772 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
8acb0250
HM
2773 inc = value > current_value ? 1 : -1;
2774 for (i = current_value; i != value; i += inc) {
c9bea99c 2775 if (issue_thinkpad_cmos_command(cmos_cmd))
8acb0250
HM
2776 return -EIO;
2777 if (!acpi_ec_write(brightness_offset, i + inc))
2778 return -EIO;
2779 }
2780
2781 return 0;
2782}
2783
56b6aeb0
HMH
2784static int brightness_read(char *p)
2785{
2786 int len = 0;
2787 int level;
2788
2789 if ((level = brightness_get(NULL)) < 0) {
2790 len += sprintf(p + len, "level:\t\tunreadable\n");
2791 } else {
2792 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2793 len += sprintf(p + len, "commands:\tup, down\n");
2794 len += sprintf(p + len, "commands:\tlevel <level>"
2795 " (<level> is 0-7)\n");
2796 }
2797
2798 return len;
2799}
2800
8acb0250
HM
2801static int brightness_write(char *buf)
2802{
2803 int level;
78f81cc4 2804 int new_level;
1da177e4 2805 char *cmd;
1da177e4
LT
2806
2807 while ((cmd = next_cmd(&buf))) {
8acb0250
HM
2808 if ((level = brightness_get(NULL)) < 0)
2809 return level;
78f81cc4
BD
2810 level &= 7;
2811
2812 if (strlencmp(cmd, "up") == 0) {
2813 new_level = level == 7 ? 7 : level + 1;
2814 } else if (strlencmp(cmd, "down") == 0) {
2815 new_level = level == 0 ? 0 : level - 1;
2816 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2817 new_level >= 0 && new_level <= 7) {
2818 /* new_level set */
2819 } else
2820 return -EINVAL;
2821
8acb0250 2822 brightness_set(new_level);
78f81cc4
BD
2823 }
2824
2825 return 0;
2826}
2827
a5763f22
HMH
2828static struct ibm_struct brightness_driver_data = {
2829 .name = "brightness",
2830 .read = brightness_read,
2831 .write = brightness_write,
2832 .exit = brightness_exit,
2833};
2834
56b6aeb0
HMH
2835/*************************************************************************
2836 * Volume subdriver
2837 */
fb87a811 2838
78f81cc4
BD
2839static int volume_read(char *p)
2840{
2841 int len = 0;
2842 u8 level;
2843
2844 if (!acpi_ec_read(volume_offset, &level)) {
2845 len += sprintf(p + len, "level:\t\tunreadable\n");
2846 } else {
2847 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2848 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2849 len += sprintf(p + len, "commands:\tup, down, mute\n");
2850 len += sprintf(p + len, "commands:\tlevel <level>"
2851 " (<level> is 0-15)\n");
2852 }
2853
2854 return len;
2855}
2856
78f81cc4
BD
2857static int volume_write(char *buf)
2858{
2859 int cmos_cmd, inc, i;
2860 u8 level, mute;
2861 int new_level, new_mute;
2862 char *cmd;
2863
2864 while ((cmd = next_cmd(&buf))) {
2865 if (!acpi_ec_read(volume_offset, &level))
2866 return -EIO;
2867 new_mute = mute = level & 0x40;
2868 new_level = level = level & 0xf;
2869
2870 if (strlencmp(cmd, "up") == 0) {
2871 if (mute)
2872 new_mute = 0;
2873 else
2874 new_level = level == 15 ? 15 : level + 1;
2875 } else if (strlencmp(cmd, "down") == 0) {
2876 if (mute)
2877 new_mute = 0;
2878 else
2879 new_level = level == 0 ? 0 : level - 1;
2880 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2881 new_level >= 0 && new_level <= 15) {
2882 /* new_level set */
2883 } else if (strlencmp(cmd, "mute") == 0) {
2884 new_mute = 0x40;
1da177e4
LT
2885 } else
2886 return -EINVAL;
2887
78f81cc4 2888 if (new_level != level) { /* mute doesn't change */
56b6aeb0 2889 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
78f81cc4
BD
2890 inc = new_level > level ? 1 : -1;
2891
c9bea99c 2892 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
78f81cc4
BD
2893 !acpi_ec_write(volume_offset, level)))
2894 return -EIO;
2895
2896 for (i = level; i != new_level; i += inc)
c9bea99c 2897 if (issue_thinkpad_cmos_command(cmos_cmd) ||
78f81cc4
BD
2898 !acpi_ec_write(volume_offset, i + inc))
2899 return -EIO;
2900
c9bea99c 2901 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
78f81cc4
BD
2902 !acpi_ec_write(volume_offset,
2903 new_level + mute)))
2904 return -EIO;
2905 }
2906
2907 if (new_mute != mute) { /* level doesn't change */
56b6aeb0 2908 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
78f81cc4 2909
c9bea99c 2910 if (issue_thinkpad_cmos_command(cmos_cmd) ||
78f81cc4
BD
2911 !acpi_ec_write(volume_offset, level + new_mute))
2912 return -EIO;
2913 }
2914 }
2915
2916 return 0;
2917}
2918
a5763f22
HMH
2919static struct ibm_struct volume_driver_data = {
2920 .name = "volume",
2921 .read = volume_read,
2922 .write = volume_write,
2923};
56b6aeb0
HMH
2924
2925/*************************************************************************
2926 * Fan subdriver
2927 */
2928
2929/*
2930 * FAN ACCESS MODES
2931 *
efa27145 2932 * TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0
HMH
2933 * ACPI GFAN method: returns fan level
2934 *
efa27145 2935 * see TPACPI_FAN_WR_ACPI_SFAN
f51d1a39 2936 * EC 0x2f (HFSP) not available if GFAN exists
56b6aeb0 2937 *
efa27145 2938 * TPACPI_FAN_WR_ACPI_SFAN:
56b6aeb0
HMH
2939 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2940 *
f51d1a39
HMH
2941 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2942 * it for writing.
56b6aeb0 2943 *
efa27145 2944 * TPACPI_FAN_WR_TPEC:
f51d1a39
HMH
2945 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2946 * Supported on almost all ThinkPads
56b6aeb0
HMH
2947 *
2948 * Fan speed changes of any sort (including those caused by the
2949 * disengaged mode) are usually done slowly by the firmware as the
2950 * maximum ammount of fan duty cycle change per second seems to be
2951 * limited.
2952 *
2953 * Reading is not available if GFAN exists.
2954 * Writing is not available if SFAN exists.
2955 *
2956 * Bits
2957 * 7 automatic mode engaged;
2958 * (default operation mode of the ThinkPad)
2959 * fan level is ignored in this mode.
f51d1a39 2960 * 6 full speed mode (takes precedence over bit 7);
56b6aeb0 2961 * not available on all thinkpads. May disable
f51d1a39
HMH
2962 * the tachometer while the fan controller ramps up
2963 * the speed (which can take up to a few *minutes*).
2964 * Speeds up fan to 100% duty-cycle, which is far above
2965 * the standard RPM levels. It is not impossible that
2966 * it could cause hardware damage.
56b6aeb0
HMH
2967 * 5-3 unused in some models. Extra bits for fan level
2968 * in others, but still useless as all values above
2969 * 7 map to the same speed as level 7 in these models.
2970 * 2-0 fan level (0..7 usually)
2971 * 0x00 = stop
2972 * 0x07 = max (set when temperatures critical)
2973 * Some ThinkPads may have other levels, see
efa27145 2974 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
56b6aeb0
HMH
2975 *
2976 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2977 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2978 * does so, its initial value is meaningless (0x07).
2979 *
2980 * For firmware bugs, refer to:
2981 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2982 *
2983 * ----
2984 *
2985 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2986 * Main fan tachometer reading (in RPM)
2987 *
2988 * This register is present on all ThinkPads with a new-style EC, and
2989 * it is known not to be present on the A21m/e, and T22, as there is
2990 * something else in offset 0x84 according to the ACPI DSDT. Other
2991 * ThinkPads from this same time period (and earlier) probably lack the
2992 * tachometer as well.
2993 *
2994 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2995 * was never fixed by IBM to report the EC firmware version string
2996 * probably support the tachometer (like the early X models), so
2997 * detecting it is quite hard. We need more data to know for sure.
2998 *
2999 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3000 * might result.
3001 *
f51d1a39
HMH
3002 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3003 * mode.
56b6aeb0
HMH
3004 *
3005 * For firmware bugs, refer to:
3006 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3007 *
efa27145 3008 * TPACPI_FAN_WR_ACPI_FANS:
56b6aeb0
HMH
3009 * ThinkPad X31, X40, X41. Not available in the X60.
3010 *
3011 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3012 * high speed. ACPI DSDT seems to map these three speeds to levels
3013 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3014 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3015 *
3016 * The speeds are stored on handles
3017 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3018 *
3019 * There are three default speed sets, acessible as handles:
3020 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3021 *
3022 * ACPI DSDT switches which set is in use depending on various
3023 * factors.
3024 *
efa27145 3025 * TPACPI_FAN_WR_TPEC is also available and should be used to
56b6aeb0
HMH
3026 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3027 * but the ACPI tables just mention level 7.
3028 */
3029
69ba91cb
HMH
3030static enum fan_status_access_mode fan_status_access_mode;
3031static enum fan_control_access_mode fan_control_access_mode;
3032static enum fan_control_commands fan_control_commands;
78f81cc4 3033
778b4d74 3034static u8 fan_control_initial_status;
fe98a52c 3035static u8 fan_control_desired_level;
778b4d74 3036
25c68a33 3037static void fan_watchdog_fire(struct work_struct *ignored);
16663a87 3038static int fan_watchdog_maxinterval;
25c68a33 3039static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
16663a87 3040
56b6aeb0
HMH
3041IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3042IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3043 "\\FSPD", /* 600e/x, 770e, 770x */
3044 ); /* all others */
3045IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3046 "JFNS", /* 770x-JL */
3047 ); /* all others */
3048
fe98a52c
HMH
3049/*
3050 * SYSFS fan layout: hwmon compatible (device)
3051 *
3052 * pwm*_enable:
3053 * 0: "disengaged" mode
3054 * 1: manual mode
3055 * 2: native EC "auto" mode (recommended, hardware default)
3056 *
3057 * pwm*: set speed in manual mode, ignored otherwise.
3058 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3059 * interpolation.
3060 *
3061 * fan*_input: tachometer reading, RPM
3062 *
3063 *
3064 * SYSFS fan layout: extensions
3065 *
3066 * fan_watchdog (driver):
3067 * fan watchdog interval in seconds, 0 disables (default), max 120
3068 */
3069
3070/* sysfs fan pwm1_enable ----------------------------------------------- */
3071static ssize_t fan_pwm1_enable_show(struct device *dev,
3072 struct device_attribute *attr,
3073 char *buf)
3074{
3075 int res, mode;
3076 u8 status;
3077
3078 res = fan_get_status_safe(&status);
3079 if (res)
3080 return res;
3081
3082 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3083 if (status != fan_control_initial_status) {
3084 tp_features.fan_ctrl_status_undef = 0;
3085 } else {
3086 /* Return most likely status. In fact, it
3087 * might be the only possible status */
3088 status = TP_EC_FAN_AUTO;
3089 }
3090 }
3091
3092 if (status & TP_EC_FAN_FULLSPEED) {
3093 mode = 0;
3094 } else if (status & TP_EC_FAN_AUTO) {
3095 mode = 2;
3096 } else
3097 mode = 1;
3098
3099 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3100}
3101
3102static ssize_t fan_pwm1_enable_store(struct device *dev,
3103 struct device_attribute *attr,
3104 const char *buf, size_t count)
3105{
3106 unsigned long t;
3107 int res, level;
3108
3109 if (parse_strtoul(buf, 2, &t))
3110 return -EINVAL;
3111
3112 switch (t) {
3113 case 0:
3114 level = TP_EC_FAN_FULLSPEED;
3115 break;
3116 case 1:
3117 level = TPACPI_FAN_LAST_LEVEL;
3118 break;
3119 case 2:
3120 level = TP_EC_FAN_AUTO;
3121 break;
3122 case 3:
3123 /* reserved for software-controlled auto mode */
3124 return -ENOSYS;
3125 default:
3126 return -EINVAL;
3127 }
3128
3129 res = fan_set_level_safe(level);
c573ddb9
HMH
3130 if (res == -ENXIO)
3131 return -EINVAL;
3132 else if (res < 0)
fe98a52c
HMH
3133 return res;
3134
3135 fan_watchdog_reset();
3136
3137 return count;
3138}
3139
3140static struct device_attribute dev_attr_fan_pwm1_enable =
3141 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3142 fan_pwm1_enable_show, fan_pwm1_enable_store);
3143
3144/* sysfs fan pwm1 ------------------------------------------------------ */
3145static ssize_t fan_pwm1_show(struct device *dev,
3146 struct device_attribute *attr,
3147 char *buf)
3148{
3149 int res;
3150 u8 status;
3151
3152 res = fan_get_status_safe(&status);
3153 if (res)
3154 return res;
3155
3156 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3157 if (status != fan_control_initial_status) {
3158 tp_features.fan_ctrl_status_undef = 0;
3159 } else {
3160 status = TP_EC_FAN_AUTO;
3161 }
3162 }
3163
3164 if ((status &
3165 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3166 status = fan_control_desired_level;
3167
3168 if (status > 7)
3169 status = 7;
3170
3171 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3172}
3173
3174static ssize_t fan_pwm1_store(struct device *dev,
3175 struct device_attribute *attr,
3176 const char *buf, size_t count)
3177{
3178 unsigned long s;
3179 int rc;
3180 u8 status, newlevel;
3181
3182 if (parse_strtoul(buf, 255, &s))
3183 return -EINVAL;
3184
3185 /* scale down from 0-255 to 0-7 */
3186 newlevel = (s >> 5) & 0x07;
3187
3188 rc = mutex_lock_interruptible(&fan_mutex);
3189 if (rc < 0)
3190 return rc;
3191
3192 rc = fan_get_status(&status);
3193 if (!rc && (status &
3194 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3195 rc = fan_set_level(newlevel);
c573ddb9
HMH
3196 if (rc == -ENXIO)
3197 rc = -EINVAL;
3198 else if (!rc) {
fe98a52c 3199 fan_update_desired_level(newlevel);
ca4ac2f4
HMH
3200 fan_watchdog_reset();
3201 }
fe98a52c
HMH
3202 }
3203
3204 mutex_unlock(&fan_mutex);
3205 return (rc)? rc : count;
3206}
3207
3208static struct device_attribute dev_attr_fan_pwm1 =
3209 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3210 fan_pwm1_show, fan_pwm1_store);
3211
3212/* sysfs fan fan1_input ------------------------------------------------ */
3213static ssize_t fan_fan1_input_show(struct device *dev,
3214 struct device_attribute *attr,
3215 char *buf)
3216{
3217 int res;
3218 unsigned int speed;
3219
3220 res = fan_get_speed(&speed);
3221 if (res < 0)
3222 return res;
3223
3224 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3225}
3226
3227static struct device_attribute dev_attr_fan_fan1_input =
3228 __ATTR(fan1_input, S_IRUGO,
3229 fan_fan1_input_show, NULL);
3230
3231/* sysfs fan fan_watchdog (driver) ------------------------------------- */
3232static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3233 char *buf)
3234{
3235 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3236}
3237
3238static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3239 const char *buf, size_t count)
3240{
3241 unsigned long t;
3242
3243 if (parse_strtoul(buf, 120, &t))
3244 return -EINVAL;
3245
ecf2a80a
HMH
3246 if (!fan_control_allowed)
3247 return -EPERM;
3248
fe98a52c
HMH
3249 fan_watchdog_maxinterval = t;
3250 fan_watchdog_reset();
3251
3252 return count;
3253}
3254
3255static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3256 fan_fan_watchdog_show, fan_fan_watchdog_store);
3257
3258/* --------------------------------------------------------------------- */
3259static struct attribute *fan_attributes[] = {
3260 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3261 &dev_attr_fan_fan1_input.attr,
3262 NULL
3263};
3264
3265static const struct attribute_group fan_attr_group = {
3266 .attrs = fan_attributes,
3267};
3268
a5763f22 3269static int __init fan_init(struct ibm_init_struct *iibm)
78f81cc4 3270{
fe98a52c
HMH
3271 int rc;
3272
fe08bc4b
HMH
3273 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3274
40ca9fdf 3275 mutex_init(&fan_mutex);
efa27145
HMH
3276 fan_status_access_mode = TPACPI_FAN_NONE;
3277 fan_control_access_mode = TPACPI_FAN_WR_NONE;
69ba91cb 3278 fan_control_commands = 0;
16663a87 3279 fan_watchdog_maxinterval = 0;
d8fd94d9 3280 tp_features.fan_ctrl_status_undef = 0;
fe98a52c 3281 fan_control_desired_level = 7;
78f81cc4 3282
8d376cd6
HMH
3283 IBM_ACPIHANDLE_INIT(fans);
3284 IBM_ACPIHANDLE_INIT(gfan);
3285 IBM_ACPIHANDLE_INIT(sfan);
5fba344c 3286
78f81cc4
BD
3287 if (gfan_handle) {
3288 /* 570, 600e/x, 770e, 770x */
efa27145 3289 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
69ba91cb
HMH
3290 } else {
3291 /* all other ThinkPads: note that even old-style
3292 * ThinkPad ECs supports the fan control register */
778b4d74
HMH
3293 if (likely(acpi_ec_read(fan_status_offset,
3294 &fan_control_initial_status))) {
efa27145 3295 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
778b4d74
HMH
3296
3297 /* In some ThinkPads, neither the EC nor the ACPI
3298 * DSDT initialize the fan status, and it ends up
3299 * being set to 0x07 when it *could* be either
3300 * 0x07 or 0x80.
3301 *
3302 * Enable for TP-1Y (T43), TP-78 (R51e),
3303 * TP-76 (R52), TP-70 (T43, R52), which are known
3304 * to be buggy. */
3305 if (fan_control_initial_status == 0x07 &&
3306 ibm_thinkpad_ec_found &&
3307 ((ibm_thinkpad_ec_found[0] == '1' &&
3308 ibm_thinkpad_ec_found[1] == 'Y') ||
3309 (ibm_thinkpad_ec_found[0] == '7' &&
3310 (ibm_thinkpad_ec_found[1] == '6' ||
3311 ibm_thinkpad_ec_found[1] == '8' ||
3312 ibm_thinkpad_ec_found[1] == '0'))
3313 )) {
3314 printk(IBM_NOTICE
3315 "fan_init: initial fan status is "
3316 "unknown, assuming it is in auto "
3317 "mode\n");
d8fd94d9 3318 tp_features.fan_ctrl_status_undef = 1;
778b4d74 3319 }
69ba91cb
HMH
3320 } else {
3321 printk(IBM_ERR
3322 "ThinkPad ACPI EC access misbehaving, "
3323 "fan status and control unavailable\n");
5fba344c 3324 return 1;
69ba91cb
HMH
3325 }
3326 }
78f81cc4 3327
69ba91cb
HMH
3328 if (sfan_handle) {
3329 /* 570, 770x-JL */
efa27145 3330 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
1c6a334e 3331 fan_control_commands |=
efa27145 3332 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
78f81cc4 3333 } else {
69ba91cb
HMH
3334 if (!gfan_handle) {
3335 /* gfan without sfan means no fan control */
3336 /* all other models implement TP EC 0x2f control */
3337
3338 if (fans_handle) {
a8b7a662 3339 /* X31, X40, X41 */
69ba91cb 3340 fan_control_access_mode =
efa27145 3341 TPACPI_FAN_WR_ACPI_FANS;
69ba91cb 3342 fan_control_commands |=
efa27145
HMH
3343 TPACPI_FAN_CMD_SPEED |
3344 TPACPI_FAN_CMD_LEVEL |
3345 TPACPI_FAN_CMD_ENABLE;
69ba91cb 3346 } else {
efa27145 3347 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
a12095c2 3348 fan_control_commands |=
efa27145
HMH
3349 TPACPI_FAN_CMD_LEVEL |
3350 TPACPI_FAN_CMD_ENABLE;
69ba91cb
HMH
3351 }
3352 }
3353 }
3354
fe08bc4b
HMH
3355 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3356 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3357 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3358 fan_status_access_mode, fan_control_access_mode);
3359
ecf2a80a
HMH
3360 /* fan control master switch */
3361 if (!fan_control_allowed) {
3362 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3363 fan_control_commands = 0;
3364 dbg_printk(TPACPI_DBG_INIT,
3365 "fan control features disabled by parameter\n");
3366 }
3367
fe98a52c
HMH
3368 /* update fan_control_desired_level */
3369 if (fan_status_access_mode != TPACPI_FAN_NONE)
3370 fan_get_status_safe(NULL);
3371
3372 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3373 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3374 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3375 &fan_attr_group);
3376 if (!(rc < 0))
3377 rc = driver_create_file(&tpacpi_pdriver.driver,
3378 &driver_attr_fan_watchdog);
3379 if (rc < 0)
3380 return rc;
3381 return 0;
3382 } else
3383 return 1;
3384}
3385
3386/*
3387 * Call with fan_mutex held
3388 */
3389static void fan_update_desired_level(u8 status)
3390{
3391 if ((status &
3392 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3393 if (status > 7)
3394 fan_control_desired_level = 7;
3395 else
3396 fan_control_desired_level = status;
3397 }
69ba91cb 3398}
78f81cc4 3399
c52f0aa5 3400static int fan_get_status(u8 *status)
78f81cc4 3401{
c52f0aa5 3402 u8 s;
78f81cc4 3403
a8b7a662 3404 /* TODO:
efa27145 3405 * Add TPACPI_FAN_RD_ACPI_FANS ? */
a8b7a662 3406
3ef8a609 3407 switch (fan_status_access_mode) {
efa27145 3408 case TPACPI_FAN_RD_ACPI_GFAN:
78f81cc4 3409 /* 570, 600e/x, 770e, 770x */
c52f0aa5
HMH
3410
3411 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
1da177e4 3412 return -EIO;
78f81cc4 3413
c52f0aa5
HMH
3414 if (likely(status))
3415 *status = s & 0x07;
3ef8a609
HMH
3416
3417 break;
3418
efa27145 3419 case TPACPI_FAN_RD_TPEC:
78f81cc4 3420 /* all except 570, 600e/x, 770e, 770x */
c52f0aa5 3421 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3ef8a609 3422 return -EIO;
78f81cc4 3423
c52f0aa5
HMH
3424 if (likely(status))
3425 *status = s;
3426
3427 break;
3428
3429 default:
3430 return -ENXIO;
78f81cc4
BD
3431 }
3432
c52f0aa5
HMH
3433 return 0;
3434}
3435
fe98a52c
HMH
3436static int fan_get_status_safe(u8 *status)
3437{
3438 int rc;
3439 u8 s;
3440
3441 rc = mutex_lock_interruptible(&fan_mutex);
3442 if (rc < 0)
3443 return rc;
3444 rc = fan_get_status(&s);
3445 if (!rc)
3446 fan_update_desired_level(s);
3447 mutex_unlock(&fan_mutex);
3448
3449 if (status)
3450 *status = s;
3451
3452 return rc;
3453}
3454
56b6aeb0
HMH
3455static void fan_exit(void)
3456{
99fba3f8 3457 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
fe98a52c
HMH
3458
3459 /* FIXME: can we really do this unconditionally? */
3460 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3461 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3462
56b6aeb0
HMH
3463 cancel_delayed_work(&fan_watchdog_task);
3464 flush_scheduled_work();
3465}
3466
c52f0aa5
HMH
3467static int fan_get_speed(unsigned int *speed)
3468{
3469 u8 hi, lo;
3470
3471 switch (fan_status_access_mode) {
efa27145 3472 case TPACPI_FAN_RD_TPEC:
78f81cc4 3473 /* all except 570, 600e/x, 770e, 770x */
3ef8a609
HMH
3474 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3475 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3476 return -EIO;
3ef8a609 3477
c52f0aa5
HMH
3478 if (likely(speed))
3479 *speed = (hi << 8) | lo;
3480
3481 break;
3482
3483 default:
3484 return -ENXIO;
3485 }
3486
3487 return 0;
3488}
3489
56b6aeb0 3490static void fan_watchdog_fire(struct work_struct *ignored)
16663a87 3491{
99fba3f8
HMH
3492 int rc;
3493
56b6aeb0 3494 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
99fba3f8
HMH
3495 rc = fan_set_enable();
3496 if (rc < 0) {
3497 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3498 "will try again later...\n", -rc);
56b6aeb0
HMH
3499 /* reschedule for later */
3500 fan_watchdog_reset();
3501 }
16663a87
HMH
3502}
3503
3504static void fan_watchdog_reset(void)
3505{
94954cc6 3506 static int fan_watchdog_active;
16663a87 3507
4985cd0a
HMH
3508 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3509 return;
3510
16663a87
HMH
3511 if (fan_watchdog_active)
3512 cancel_delayed_work(&fan_watchdog_task);
3513
3514 if (fan_watchdog_maxinterval > 0) {
3515 fan_watchdog_active = 1;
3516 if (!schedule_delayed_work(&fan_watchdog_task,
3517 msecs_to_jiffies(fan_watchdog_maxinterval
3518 * 1000))) {
3519 printk(IBM_ERR "failed to schedule the fan watchdog, "
3520 "watchdog will not trigger\n");
3521 }
3522 } else
3523 fan_watchdog_active = 0;
3524}
3525
18ad7996 3526static int fan_set_level(int level)
78f81cc4 3527{
ecf2a80a
HMH
3528 if (!fan_control_allowed)
3529 return -EPERM;
3530
18ad7996 3531 switch (fan_control_access_mode) {
efa27145 3532 case TPACPI_FAN_WR_ACPI_SFAN:
18ad7996 3533 if (level >= 0 && level <= 7) {
fe98a52c 3534 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
78f81cc4 3535 return -EIO;
18ad7996
HMH
3536 } else
3537 return -EINVAL;
3538 break;
3539
efa27145
HMH
3540 case TPACPI_FAN_WR_ACPI_FANS:
3541 case TPACPI_FAN_WR_TPEC:
3542 if ((level != TP_EC_FAN_AUTO) &&
3543 (level != TP_EC_FAN_FULLSPEED) &&
a12095c2
HMH
3544 ((level < 0) || (level > 7)))
3545 return -EINVAL;
3546
eaa7571b
HMH
3547 /* safety net should the EC not support AUTO
3548 * or FULLSPEED mode bits and just ignore them */
3549 if (level & TP_EC_FAN_FULLSPEED)
3550 level |= 7; /* safety min speed 7 */
3551 else if (level & TP_EC_FAN_FULLSPEED)
3552 level |= 4; /* safety min speed 4 */
3553
fe98a52c 3554 if (!acpi_ec_write(fan_status_offset, level))
a12095c2 3555 return -EIO;
778b4d74 3556 else
d8fd94d9 3557 tp_features.fan_ctrl_status_undef = 0;
a12095c2
HMH
3558 break;
3559
18ad7996
HMH
3560 default:
3561 return -ENXIO;
3562 }
3563 return 0;
3564}
3565
fe98a52c
HMH
3566static int fan_set_level_safe(int level)
3567{
3568 int rc;
3569
ecf2a80a
HMH
3570 if (!fan_control_allowed)
3571 return -EPERM;
3572
fe98a52c
HMH
3573 rc = mutex_lock_interruptible(&fan_mutex);
3574 if (rc < 0)
3575 return rc;
3576
3577 if (level == TPACPI_FAN_LAST_LEVEL)
3578 level = fan_control_desired_level;
3579
3580 rc = fan_set_level(level);
3581 if (!rc)
3582 fan_update_desired_level(level);
3583
3584 mutex_unlock(&fan_mutex);
3585 return rc;
3586}
3587
18ad7996
HMH
3588static int fan_set_enable(void)
3589{
1c6a334e
HMH
3590 u8 s;
3591 int rc;
3592
ecf2a80a
HMH
3593 if (!fan_control_allowed)
3594 return -EPERM;
3595
40ca9fdf
HMH
3596 rc = mutex_lock_interruptible(&fan_mutex);
3597 if (rc < 0)
3598 return rc;
3599
18ad7996 3600 switch (fan_control_access_mode) {
efa27145
HMH
3601 case TPACPI_FAN_WR_ACPI_FANS:
3602 case TPACPI_FAN_WR_TPEC:
40ca9fdf
HMH
3603 rc = fan_get_status(&s);
3604 if (rc < 0)
3605 break;
1c6a334e
HMH
3606
3607 /* Don't go out of emergency fan mode */
eaa7571b
HMH
3608 if (s != 7) {
3609 s &= 0x07;
3610 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3611 }
1c6a334e
HMH
3612
3613 if (!acpi_ec_write(fan_status_offset, s))
40ca9fdf
HMH
3614 rc = -EIO;
3615 else {
d8fd94d9 3616 tp_features.fan_ctrl_status_undef = 0;
40ca9fdf
HMH
3617 rc = 0;
3618 }
1c6a334e
HMH
3619 break;
3620
efa27145 3621 case TPACPI_FAN_WR_ACPI_SFAN:
40ca9fdf
HMH
3622 rc = fan_get_status(&s);
3623 if (rc < 0)
3624 break;
1c6a334e
HMH
3625
3626 s &= 0x07;
3627
3628 /* Set fan to at least level 4 */
eaa7571b 3629 s |= 4;
1c6a334e
HMH
3630
3631 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
40ca9fdf
HMH
3632 rc= -EIO;
3633 else
3634 rc = 0;
18ad7996
HMH
3635 break;
3636
3637 default:
40ca9fdf 3638 rc = -ENXIO;
18ad7996 3639 }
40ca9fdf
HMH
3640
3641 mutex_unlock(&fan_mutex);
3642 return rc;
18ad7996
HMH
3643}
3644
3645static int fan_set_disable(void)
3646{
40ca9fdf
HMH
3647 int rc;
3648
ecf2a80a
HMH
3649 if (!fan_control_allowed)
3650 return -EPERM;
3651
40ca9fdf
HMH
3652 rc = mutex_lock_interruptible(&fan_mutex);
3653 if (rc < 0)
3654 return rc;
3655
3656 rc = 0;
18ad7996 3657 switch (fan_control_access_mode) {
efa27145
HMH
3658 case TPACPI_FAN_WR_ACPI_FANS:
3659 case TPACPI_FAN_WR_TPEC:
18ad7996 3660 if (!acpi_ec_write(fan_status_offset, 0x00))
40ca9fdf 3661 rc = -EIO;
fe98a52c
HMH
3662 else {
3663 fan_control_desired_level = 0;
d8fd94d9 3664 tp_features.fan_ctrl_status_undef = 0;
fe98a52c 3665 }
18ad7996
HMH
3666 break;
3667
efa27145 3668 case TPACPI_FAN_WR_ACPI_SFAN:
1c6a334e 3669 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
40ca9fdf 3670 rc = -EIO;
fe98a52c
HMH
3671 else
3672 fan_control_desired_level = 0;
1c6a334e
HMH
3673 break;
3674
18ad7996 3675 default:
40ca9fdf 3676 rc = -ENXIO;
18ad7996 3677 }
40ca9fdf 3678
fe98a52c 3679
40ca9fdf
HMH
3680 mutex_unlock(&fan_mutex);
3681 return rc;
56b6aeb0
HMH
3682}
3683
3684static int fan_set_speed(int speed)
3685{
40ca9fdf
HMH
3686 int rc;
3687
ecf2a80a
HMH
3688 if (!fan_control_allowed)
3689 return -EPERM;
3690
40ca9fdf
HMH
3691 rc = mutex_lock_interruptible(&fan_mutex);
3692 if (rc < 0)
3693 return rc;
3694
3695 rc = 0;
56b6aeb0 3696 switch (fan_control_access_mode) {
efa27145 3697 case TPACPI_FAN_WR_ACPI_FANS:
56b6aeb0
HMH
3698 if (speed >= 0 && speed <= 65535) {
3699 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3700 speed, speed, speed))
40ca9fdf 3701 rc = -EIO;
56b6aeb0 3702 } else
40ca9fdf 3703 rc = -EINVAL;
56b6aeb0
HMH
3704 break;
3705
3706 default:
40ca9fdf 3707 rc = -ENXIO;
56b6aeb0 3708 }
40ca9fdf
HMH
3709
3710 mutex_unlock(&fan_mutex);
3711 return rc;
56b6aeb0
HMH
3712}
3713
3714static int fan_read(char *p)
3715{
3716 int len = 0;
3717 int rc;
3718 u8 status;
3719 unsigned int speed = 0;
3720
3721 switch (fan_status_access_mode) {
efa27145 3722 case TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0 3723 /* 570, 600e/x, 770e, 770x */
fe98a52c 3724 if ((rc = fan_get_status_safe(&status)) < 0)
56b6aeb0
HMH
3725 return rc;
3726
3727 len += sprintf(p + len, "status:\t\t%s\n"
3728 "level:\t\t%d\n",
3729 (status != 0) ? "enabled" : "disabled", status);
3730 break;
3731
efa27145 3732 case TPACPI_FAN_RD_TPEC:
56b6aeb0 3733 /* all except 570, 600e/x, 770e, 770x */
fe98a52c 3734 if ((rc = fan_get_status_safe(&status)) < 0)
56b6aeb0
HMH
3735 return rc;
3736
d8fd94d9 3737 if (unlikely(tp_features.fan_ctrl_status_undef)) {
56b6aeb0 3738 if (status != fan_control_initial_status)
d8fd94d9 3739 tp_features.fan_ctrl_status_undef = 0;
56b6aeb0
HMH
3740 else
3741 /* Return most likely status. In fact, it
3742 * might be the only possible status */
efa27145 3743 status = TP_EC_FAN_AUTO;
56b6aeb0
HMH
3744 }
3745
3746 len += sprintf(p + len, "status:\t\t%s\n",
3747 (status != 0) ? "enabled" : "disabled");
3748
56b6aeb0
HMH
3749 if ((rc = fan_get_speed(&speed)) < 0)
3750 return rc;
3751
3752 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3753
efa27145 3754 if (status & TP_EC_FAN_FULLSPEED)
56b6aeb0
HMH
3755 /* Disengaged mode takes precedence */
3756 len += sprintf(p + len, "level:\t\tdisengaged\n");
efa27145 3757 else if (status & TP_EC_FAN_AUTO)
56b6aeb0
HMH
3758 len += sprintf(p + len, "level:\t\tauto\n");
3759 else
3760 len += sprintf(p + len, "level:\t\t%d\n", status);
3761 break;
3762
efa27145 3763 case TPACPI_FAN_NONE:
56b6aeb0
HMH
3764 default:
3765 len += sprintf(p + len, "status:\t\tnot supported\n");
3766 }
3767
efa27145 3768 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
56b6aeb0
HMH
3769 len += sprintf(p + len, "commands:\tlevel <level>");
3770
3771 switch (fan_control_access_mode) {
efa27145 3772 case TPACPI_FAN_WR_ACPI_SFAN:
56b6aeb0
HMH
3773 len += sprintf(p + len, " (<level> is 0-7)\n");
3774 break;
3775
3776 default:
3777 len += sprintf(p + len, " (<level> is 0-7, "
fe98a52c 3778 "auto, disengaged, full-speed)\n");
56b6aeb0
HMH
3779 break;
3780 }
3781 }
18ad7996 3782
efa27145 3783 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
56b6aeb0
HMH
3784 len += sprintf(p + len, "commands:\tenable, disable\n"
3785 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3786 "1-120 (seconds))\n");
1da177e4 3787
efa27145 3788 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
56b6aeb0
HMH
3789 len += sprintf(p + len, "commands:\tspeed <speed>"
3790 " (<speed> is 0-65535)\n");
3791
3792 return len;
78f81cc4
BD
3793}
3794
18ad7996
HMH
3795static int fan_write_cmd_level(const char *cmd, int *rc)
3796{
3797 int level;
3798
a12095c2 3799 if (strlencmp(cmd, "level auto") == 0)
efa27145 3800 level = TP_EC_FAN_AUTO;
fe98a52c
HMH
3801 else if ((strlencmp(cmd, "level disengaged") == 0) |
3802 (strlencmp(cmd, "level full-speed") == 0))
efa27145 3803 level = TP_EC_FAN_FULLSPEED;
a12095c2 3804 else if (sscanf(cmd, "level %d", &level) != 1)
18ad7996
HMH
3805 return 0;
3806
fe98a52c 3807 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
18ad7996
HMH
3808 printk(IBM_ERR "level command accepted for unsupported "
3809 "access mode %d", fan_control_access_mode);
3810
3811 return 1;
3812}
3813
3814static int fan_write_cmd_enable(const char *cmd, int *rc)
3815{
3816 if (strlencmp(cmd, "enable") != 0)
3817 return 0;
3818
3819 if ((*rc = fan_set_enable()) == -ENXIO)
3820 printk(IBM_ERR "enable command accepted for unsupported "
3821 "access mode %d", fan_control_access_mode);
3822
3823 return 1;
3824}
3825
3826static int fan_write_cmd_disable(const char *cmd, int *rc)
3827{
3828 if (strlencmp(cmd, "disable") != 0)
3829 return 0;
3830
3831 if ((*rc = fan_set_disable()) == -ENXIO)
3832 printk(IBM_ERR "disable command accepted for unsupported "
3833 "access mode %d", fan_control_access_mode);
3834
3835 return 1;
3836}
3837
3838static int fan_write_cmd_speed(const char *cmd, int *rc)
3839{
3840 int speed;
3841
a8b7a662
HMH
3842 /* TODO:
3843 * Support speed <low> <medium> <high> ? */
3844
18ad7996
HMH
3845 if (sscanf(cmd, "speed %d", &speed) != 1)
3846 return 0;
3847
3848 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3849 printk(IBM_ERR "speed command accepted for unsupported "
3850 "access mode %d", fan_control_access_mode);
3851
3852 return 1;
3853}
3854
16663a87
HMH
3855static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3856{
3857 int interval;
3858
3859 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3860 return 0;
3861
3862 if (interval < 0 || interval > 120)
3863 *rc = -EINVAL;
3864 else
3865 fan_watchdog_maxinterval = interval;
3866
3867 return 1;
3868}
3869
18ad7996
HMH
3870static int fan_write(char *buf)
3871{
3872 char *cmd;
3873 int rc = 0;
3874
3875 while (!rc && (cmd = next_cmd(&buf))) {
efa27145 3876 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
18ad7996 3877 fan_write_cmd_level(cmd, &rc)) &&
efa27145 3878 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
18ad7996 3879 (fan_write_cmd_enable(cmd, &rc) ||
16663a87
HMH
3880 fan_write_cmd_disable(cmd, &rc) ||
3881 fan_write_cmd_watchdog(cmd, &rc))) &&
efa27145 3882 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
18ad7996
HMH
3883 fan_write_cmd_speed(cmd, &rc))
3884 )
3885 rc = -EINVAL;
16663a87
HMH
3886 else if (!rc)
3887 fan_watchdog_reset();
18ad7996
HMH
3888 }
3889
3890 return rc;
3891}
3892
a5763f22
HMH
3893static struct ibm_struct fan_driver_data = {
3894 .name = "fan",
3895 .read = fan_read,
3896 .write = fan_write,
3897 .exit = fan_exit,
a5763f22
HMH
3898};
3899
56b6aeb0
HMH
3900/****************************************************************************
3901 ****************************************************************************
3902 *
3903 * Infrastructure
3904 *
3905 ****************************************************************************
3906 ****************************************************************************/
3907
3908/* /proc support */
94954cc6 3909static struct proc_dir_entry *proc_dir;
16663a87 3910
56b6aeb0 3911/* Subdriver registry */
a5763f22
HMH
3912static LIST_HEAD(tpacpi_all_drivers);
3913
1da177e4 3914
56b6aeb0
HMH
3915/*
3916 * Module and infrastructure proble, init and exit handling
3917 */
1da177e4 3918
fe08bc4b 3919#ifdef CONFIG_THINKPAD_ACPI_DEBUG
a5763f22 3920static const char * __init str_supported(int is_supported)
fe08bc4b 3921{
a5763f22 3922 static char text_unsupported[] __initdata = "not supported";
fe08bc4b 3923
a5763f22 3924 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
fe08bc4b
HMH
3925}
3926#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3927
a5763f22 3928static int __init ibm_init(struct ibm_init_struct *iibm)
1da177e4
LT
3929{
3930 int ret;
a5763f22 3931 struct ibm_struct *ibm = iibm->data;
1da177e4
LT
3932 struct proc_dir_entry *entry;
3933
a5763f22
HMH
3934 BUG_ON(ibm == NULL);
3935
3936 INIT_LIST_HEAD(&ibm->all_drivers);
3937
92641177 3938 if (ibm->flags.experimental && !experimental)
1da177e4
LT
3939 return 0;
3940
fe08bc4b
HMH
3941 dbg_printk(TPACPI_DBG_INIT,
3942 "probing for %s\n", ibm->name);
3943
a5763f22
HMH
3944 if (iibm->init) {
3945 ret = iibm->init(iibm);
5fba344c
HMH
3946 if (ret > 0)
3947 return 0; /* probe failed */
3948 if (ret)
1da177e4 3949 return ret;
a5763f22 3950
92641177 3951 ibm->flags.init_called = 1;
1da177e4
LT
3952 }
3953
8d376cd6
HMH
3954 if (ibm->acpi) {
3955 if (ibm->acpi->hid) {
3956 ret = register_tpacpi_subdriver(ibm);
3957 if (ret)
3958 goto err_out;
3959 }
5fba344c 3960
8d376cd6
HMH
3961 if (ibm->acpi->notify) {
3962 ret = setup_acpi_notify(ibm);
3963 if (ret == -ENODEV) {
3964 printk(IBM_NOTICE "disabling subdriver %s\n",
3965 ibm->name);
3966 ret = 0;
3967 goto err_out;
3968 }
3969 if (ret < 0)
3970 goto err_out;
5fba344c 3971 }
5fba344c
HMH
3972 }
3973
fe08bc4b
HMH
3974 dbg_printk(TPACPI_DBG_INIT,
3975 "%s installed\n", ibm->name);
3976
78f81cc4
BD
3977 if (ibm->read) {
3978 entry = create_proc_entry(ibm->name,
3979 S_IFREG | S_IRUGO | S_IWUSR,
3980 proc_dir);
3981 if (!entry) {
3982 printk(IBM_ERR "unable to create proc entry %s\n",
3983 ibm->name);
5fba344c
HMH
3984 ret = -ENODEV;
3985 goto err_out;
78f81cc4
BD
3986 }
3987 entry->owner = THIS_MODULE;
3988 entry->data = ibm;
8d376cd6 3989 entry->read_proc = &dispatch_procfs_read;
78f81cc4 3990 if (ibm->write)
8d376cd6 3991 entry->write_proc = &dispatch_procfs_write;
92641177 3992 ibm->flags.proc_created = 1;
78f81cc4 3993 }
1da177e4 3994
a5763f22
HMH
3995 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3996
1da177e4 3997 return 0;
5fba344c
HMH
3998
3999err_out:
fe08bc4b
HMH
4000 dbg_printk(TPACPI_DBG_INIT,
4001 "%s: at error exit path with result %d\n",
4002 ibm->name, ret);
4003
5fba344c
HMH
4004 ibm_exit(ibm);
4005 return (ret < 0)? ret : 0;
1da177e4
LT
4006}
4007
4008static void ibm_exit(struct ibm_struct *ibm)
4009{
fe08bc4b 4010 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
a5763f22
HMH
4011
4012 list_del_init(&ibm->all_drivers);
4013
8d376cd6 4014 if (ibm->flags.acpi_notify_installed) {
fe08bc4b
HMH
4015 dbg_printk(TPACPI_DBG_EXIT,
4016 "%s: acpi_remove_notify_handler\n", ibm->name);
8d376cd6
HMH
4017 BUG_ON(!ibm->acpi);
4018 acpi_remove_notify_handler(*ibm->acpi->handle,
4019 ibm->acpi->type,
4020 dispatch_acpi_notify);
4021 ibm->flags.acpi_notify_installed = 0;
4022 ibm->flags.acpi_notify_installed = 0;
5fba344c 4023 }
1da177e4 4024
92641177 4025 if (ibm->flags.proc_created) {
fe08bc4b
HMH
4026 dbg_printk(TPACPI_DBG_EXIT,
4027 "%s: remove_proc_entry\n", ibm->name);
1da177e4 4028 remove_proc_entry(ibm->name, proc_dir);
92641177 4029 ibm->flags.proc_created = 0;
5fba344c 4030 }
1da177e4 4031
8d376cd6 4032 if (ibm->flags.acpi_driver_registered) {
fe08bc4b
HMH
4033 dbg_printk(TPACPI_DBG_EXIT,
4034 "%s: acpi_bus_unregister_driver\n", ibm->name);
8d376cd6
HMH
4035 BUG_ON(!ibm->acpi);
4036 acpi_bus_unregister_driver(ibm->acpi->driver);
4037 kfree(ibm->acpi->driver);
4038 ibm->acpi->driver = NULL;
4039 ibm->flags.acpi_driver_registered = 0;
5fba344c
HMH
4040 }
4041
92641177 4042 if (ibm->flags.init_called && ibm->exit) {
5fba344c 4043 ibm->exit();
92641177 4044 ibm->flags.init_called = 0;
1da177e4 4045 }
a5763f22
HMH
4046
4047 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
1da177e4
LT
4048}
4049
56b6aeb0
HMH
4050/* Probing */
4051
94954cc6 4052static char *ibm_thinkpad_ec_found;
56b6aeb0
HMH
4053
4054static char* __init check_dmi_for_ec(void)
1da177e4 4055{
56b6aeb0
HMH
4056 struct dmi_device *dev = NULL;
4057 char ec_fw_string[18];
1da177e4 4058
56b6aeb0
HMH
4059 /*
4060 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4061 * X32 or newer, all Z series; Some models must have an
4062 * up-to-date BIOS or they will not be detected.
4063 *
4064 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4065 */
4066 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4067 if (sscanf(dev->name,
4068 "IBM ThinkPad Embedded Controller -[%17c",
4069 ec_fw_string) == 1) {
4070 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4071 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4072 return kstrdup(ec_fw_string, GFP_KERNEL);
78f81cc4 4073 }
1da177e4 4074 }
56b6aeb0 4075 return NULL;
1da177e4
LT
4076}
4077
5fba344c
HMH
4078static int __init probe_for_thinkpad(void)
4079{
4080 int is_thinkpad;
4081
4082 if (acpi_disabled)
4083 return -ENODEV;
4084
4085 /*
4086 * Non-ancient models have better DMI tagging, but very old models
4087 * don't.
4088 */
4089 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4090
4091 /* ec is required because many other handles are relative to it */
8d376cd6 4092 IBM_ACPIHANDLE_INIT(ec);
5fba344c
HMH
4093 if (!ec_handle) {
4094 if (is_thinkpad)
4095 printk(IBM_ERR
4096 "Not yet supported ThinkPad detected!\n");
4097 return -ENODEV;
4098 }
4099
0dcef77c
HMH
4100 /*
4101 * Risks a regression on very old machines, but reduces potential
4102 * false positives a damn great deal
4103 */
4104 if (!is_thinkpad)
4105 is_thinkpad = dmi_name_in_vendors("IBM");
4106
4107 if (!is_thinkpad && !force_load)
4108 return -ENODEV;
4109
5fba344c
HMH
4110 return 0;
4111}
4112
4113
56b6aeb0 4114/* Module init, exit, parameters */
1da177e4 4115
a5763f22
HMH
4116static struct ibm_init_struct ibms_init[] __initdata = {
4117 {
4118 .init = thinkpad_acpi_driver_init,
4119 .data = &thinkpad_acpi_driver_data,
4120 },
4121 {
4122 .init = hotkey_init,
4123 .data = &hotkey_driver_data,
4124 },
4125 {
4126 .init = bluetooth_init,
4127 .data = &bluetooth_driver_data,
4128 },
4129 {
4130 .init = wan_init,
4131 .data = &wan_driver_data,
4132 },
4133 {
4134 .init = video_init,
4135 .data = &video_driver_data,
4136 },
4137 {
4138 .init = light_init,
4139 .data = &light_driver_data,
4140 },
4141#ifdef CONFIG_THINKPAD_ACPI_DOCK
4142 {
4143 .init = dock_init,
4144 .data = &dock_driver_data[0],
4145 },
4146 {
d94a7f16 4147 .init = dock_init2,
a5763f22
HMH
4148 .data = &dock_driver_data[1],
4149 },
4150#endif
4151#ifdef CONFIG_THINKPAD_ACPI_BAY
4152 {
4153 .init = bay_init,
4154 .data = &bay_driver_data,
4155 },
4156#endif
4157 {
4158 .init = cmos_init,
4159 .data = &cmos_driver_data,
4160 },
4161 {
4162 .init = led_init,
4163 .data = &led_driver_data,
4164 },
4165 {
4166 .init = beep_init,
4167 .data = &beep_driver_data,
4168 },
4169 {
4170 .init = thermal_init,
4171 .data = &thermal_driver_data,
4172 },
4173 {
4174 .data = &ecdump_driver_data,
4175 },
4176 {
4177 .init = brightness_init,
4178 .data = &brightness_driver_data,
4179 },
4180 {
4181 .data = &volume_driver_data,
4182 },
4183 {
4184 .init = fan_init,
4185 .data = &fan_driver_data,
4186 },
4187};
4188
3945ac36 4189static int __init set_ibm_param(const char *val, struct kernel_param *kp)
1da177e4
LT
4190{
4191 unsigned int i;
a5763f22 4192 struct ibm_struct *ibm;
1da177e4 4193
a5763f22
HMH
4194 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4195 ibm = ibms_init[i].data;
4196 BUG_ON(ibm == NULL);
4197
4198 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4199 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
78f81cc4 4200 return -ENOSPC;
a5763f22
HMH
4201 strcpy(ibms_init[i].param, val);
4202 strcat(ibms_init[i].param, ",");
78f81cc4
BD
4203 return 0;
4204 }
a5763f22 4205 }
1da177e4 4206
1da177e4
LT
4207 return -EINVAL;
4208}
4209
56b6aeb0
HMH
4210static int experimental;
4211module_param(experimental, int, 0);
4212
132ce091
HMH
4213static u32 dbg_level;
4214module_param_named(debug, dbg_level, uint, 0);
4215
0dcef77c
HMH
4216static int force_load;
4217module_param(force_load, int, 0);
4218
ecf2a80a
HMH
4219static int fan_control_allowed;
4220module_param_named(fan_control, fan_control_allowed, int, 0);
4221
1da177e4
LT
4222#define IBM_PARAM(feature) \
4223 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4224
78f81cc4
BD
4225IBM_PARAM(hotkey);
4226IBM_PARAM(bluetooth);
4227IBM_PARAM(video);
4228IBM_PARAM(light);
85998248 4229#ifdef CONFIG_THINKPAD_ACPI_DOCK
78f81cc4 4230IBM_PARAM(dock);
63e5f248 4231#endif
85998248 4232#ifdef CONFIG_THINKPAD_ACPI_BAY
78f81cc4 4233IBM_PARAM(bay);
85998248 4234#endif /* CONFIG_THINKPAD_ACPI_BAY */
78f81cc4
BD
4235IBM_PARAM(cmos);
4236IBM_PARAM(led);
4237IBM_PARAM(beep);
4238IBM_PARAM(ecdump);
4239IBM_PARAM(brightness);
4240IBM_PARAM(volume);
4241IBM_PARAM(fan);
4242
1def7115 4243static int __init thinkpad_acpi_module_init(void)
1da177e4
LT
4244{
4245 int ret, i;
4246
54ae1501 4247 /* Driver-level probe */
5fba344c
HMH
4248 ret = probe_for_thinkpad();
4249 if (ret)
4250 return ret;
1da177e4 4251
54ae1501 4252 /* Driver initialization */
60eb0b35 4253 ibm_thinkpad_ec_found = check_dmi_for_ec();
8d376cd6
HMH
4254 IBM_ACPIHANDLE_INIT(ecrd);
4255 IBM_ACPIHANDLE_INIT(ecwr);
1da177e4 4256
8d376cd6 4257 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
1da177e4 4258 if (!proc_dir) {
8d376cd6 4259 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
1def7115 4260 thinkpad_acpi_module_exit();
1da177e4
LT
4261 return -ENODEV;
4262 }
4263 proc_dir->owner = THIS_MODULE;
78f81cc4 4264
54ae1501
HMH
4265 ret = platform_driver_register(&tpacpi_pdriver);
4266 if (ret) {
4267 printk(IBM_ERR "unable to register platform driver\n");
4268 thinkpad_acpi_module_exit();
4269 return ret;
4270 }
176750d6
HMH
4271 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4272 if (ret) {
4273 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4274 thinkpad_acpi_module_exit();
4275 return ret;
4276 }
4277
54ae1501
HMH
4278
4279 /* Device initialization */
4280 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4281 NULL, 0);
4282 if (IS_ERR(tpacpi_pdev)) {
4283 ret = PTR_ERR(tpacpi_pdev);
4284 tpacpi_pdev = NULL;
4285 printk(IBM_ERR "unable to register platform device\n");
4286 thinkpad_acpi_module_exit();
4287 return ret;
4288 }
4289 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4290 if (IS_ERR(tpacpi_hwmon)) {
4291 ret = PTR_ERR(tpacpi_hwmon);
4292 tpacpi_hwmon = NULL;
4293 printk(IBM_ERR "unable to register hwmon device\n");
4294 thinkpad_acpi_module_exit();
4295 return ret;
4296 }
a5763f22
HMH
4297 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4298 ret = ibm_init(&ibms_init[i]);
4299 if (ret >= 0 && *ibms_init[i].param)
4300 ret = ibms_init[i].data->write(ibms_init[i].param);
1da177e4 4301 if (ret < 0) {
1def7115 4302 thinkpad_acpi_module_exit();
1da177e4
LT
4303 return ret;
4304 }
4305 }
4306
4307 return 0;
4308}
4309
1def7115 4310static void thinkpad_acpi_module_exit(void)
56b6aeb0 4311{
a5763f22
HMH
4312 struct ibm_struct *ibm, *itmp;
4313
4314 list_for_each_entry_safe_reverse(ibm, itmp,
4315 &tpacpi_all_drivers,
4316 all_drivers) {
4317 ibm_exit(ibm);
4318 }
56b6aeb0 4319
a5763f22 4320 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
56b6aeb0 4321
54ae1501
HMH
4322 if (tpacpi_hwmon)
4323 hwmon_device_unregister(tpacpi_hwmon);
4324
4325 if (tpacpi_pdev)
4326 platform_device_unregister(tpacpi_pdev);
4327
176750d6 4328 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
54ae1501
HMH
4329 platform_driver_unregister(&tpacpi_pdriver);
4330
56b6aeb0 4331 if (proc_dir)
8d376cd6 4332 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
56b6aeb0 4333
8d376cd6 4334 kfree(ibm_thinkpad_ec_found);
56b6aeb0
HMH
4335}
4336
1def7115
HMH
4337module_init(thinkpad_acpi_module_init);
4338module_exit(thinkpad_acpi_module_exit);
This page took 0.473739 seconds and 5 git commands to generate.