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