blob: 984ec81bd234f047c56bf4001f3a3053bea57aeb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ibm_acpi.c - IBM ThinkPad ACPI Extras
3 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -02006 * Copyright (C) 2006 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Borislav Deianov78f81cc2005-08-17 00:00:00 -040021 */
22
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020023#define IBM_VERSION "0.13"
Borislav Deianov78f81cc2005-08-17 00:00:00 -040024
25/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Changelog:
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020027 *
28 * 2006-11-22 0.13 new maintainer
29 * changelog now lives in git commit history, and will
30 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030031 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040032 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
33 * 2005-03-17 0.11 support for 600e, 770x
34 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
35 * support for 770e, G41
36 * G40 and G41 don't have a thinklight
37 * temperatures no longer experimental
38 * experimental brightness control
39 * experimental volume control
40 * experimental fan enable/disable
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030041 * 2005-01-16 0.10 fix module loading on R30, R31
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * 2005-01-16 0.9 support for 570, R30, R31
43 * ultrabay support on A22p, A3x
44 * limit arg for cmos, led, beep, drop experimental status
45 * more capable led control on A21e, A22p, T20-22, X20
46 * experimental temperatures and fan speed
47 * experimental embedded controller register dump
48 * mark more functions as __init, drop incorrect __exit
49 * use MODULE_VERSION
50 * thanks to Henrik Brix Andersen <brix@gentoo.org>
51 * fix parameter passing on module loading
52 * thanks to Rusty Russell <rusty@rustcorp.com.au>
53 * thanks to Jim Radford <radford@blackbean.org>
54 * 2004-11-08 0.8 fix init error case, don't return from a macro
55 * thanks to Chris Wright <chrisw@osdl.org>
56 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
57 * fix led control on A21e
58 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
60 * proc file format changed
61 * video_switch command
62 * experimental cmos control
63 * experimental led control
64 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040065 * 2004-09-16 0.4 support for module parameters
66 * hotkey mask can be prefixed by 0x
67 * video output switching
68 * video expansion control
69 * ultrabay eject support
70 * removed lcd brightness/on/off control, didn't work
71 * 2004-08-17 0.3 support for R40
72 * lcd off, brightness control
73 * thinklight on/off
74 * 2004-08-14 0.2 support for T series, X20
75 * bluetooth enable/disable
76 * hotkey events disabled by default
77 * removed fan control, currently useless
78 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
80
Henrique de Moraes Holschuh1406cdd2007-03-23 17:33:56 -030081#include "ibm_acpi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020083MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -040084MODULE_DESCRIPTION(IBM_DESC);
85MODULE_VERSION(IBM_VERSION);
86MODULE_LICENSE("GPL");
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define __unused __attribute__ ((unused))
89
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030090/****************************************************************************
91 ****************************************************************************
92 *
93 * ACPI Helpers and device model
94 *
95 ****************************************************************************
96 ****************************************************************************/
97
98/*************************************************************************
99 * ACPI basic handles
100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102static acpi_handle root_handle = NULL;
103
104#define IBM_HANDLE(object, parent, paths...) \
105 static acpi_handle object##_handle; \
106 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400107 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 static char *object##_paths[] = { paths }
109
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400110IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
111 "\\_SB.PCI.ISA.EC", /* 570 */
112 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
113 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
114 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
115 "\\_SB.PCI0.ICH3.EC0", /* R31 */
116 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300117 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300119IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
120IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300122
123/*************************************************************************
124 * Misc ACPI handles
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400127IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
128 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
129 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300130 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400131
132IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
133 "^HKEY", /* R30, R31 */
134 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300135 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400136
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400137
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300138/*************************************************************************
139 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200140 */
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int acpi_evalf(acpi_handle handle,
143 void *res, char *method, char *fmt, ...)
144{
145 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400146 struct acpi_object_list params;
147 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
148 struct acpi_buffer result, *resultp;
149 union acpi_object out_obj;
150 acpi_status status;
151 va_list ap;
152 char res_type;
153 int success;
154 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (!*fmt) {
157 printk(IBM_ERR "acpi_evalf() called with empty format\n");
158 return 0;
159 }
160
161 if (*fmt == 'q') {
162 quiet = 1;
163 fmt++;
164 } else
165 quiet = 0;
166
167 res_type = *(fmt++);
168
169 params.count = 0;
170 params.pointer = &in_objs[0];
171
172 va_start(ap, fmt);
173 while (*fmt) {
174 char c = *(fmt++);
175 switch (c) {
176 case 'd': /* int */
177 in_objs[params.count].integer.value = va_arg(ap, int);
178 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
179 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400180 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 default:
182 printk(IBM_ERR "acpi_evalf() called "
183 "with invalid format character '%c'\n", c);
184 return 0;
185 }
186 }
187 va_end(ap);
188
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400189 if (res_type != 'v') {
190 result.length = sizeof(out_obj);
191 result.pointer = &out_obj;
192 resultp = &result;
193 } else
194 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400196 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400199 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (res)
201 *(int *)res = out_obj.integer.value;
202 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
203 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400204 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 success = status == AE_OK;
206 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400207 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 default:
209 printk(IBM_ERR "acpi_evalf() called "
210 "with invalid format character '%c'\n", res_type);
211 return 0;
212 }
213
214 if (!success && !quiet)
215 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
216 method, fmt0, status);
217
218 return success;
219}
220
221static void __unused acpi_print_int(acpi_handle handle, char *method)
222{
223 int i;
224
225 if (acpi_evalf(handle, &i, method, "d"))
226 printk(IBM_INFO "%s = 0x%x\n", method, i);
227 else
228 printk(IBM_ERR "error calling %s\n", method);
229}
230
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300231static int acpi_ec_read(int i, u8 * p)
232{
233 int v;
234
235 if (ecrd_handle) {
236 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
237 return 0;
238 *p = v;
239 } else {
240 if (ec_read(i, p) < 0)
241 return 0;
242 }
243
244 return 1;
245}
246
247static int acpi_ec_write(int i, u8 v)
248{
249 if (ecwr_handle) {
250 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
251 return 0;
252 } else {
253 if (ec_write(i, v) < 0)
254 return 0;
255 }
256
257 return 1;
258}
259
260static int _sta(acpi_handle handle)
261{
262 int status;
263
264 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
265 status = 0;
266
267 return status;
268}
269
270/*************************************************************************
271 * ACPI device model
272 */
273
274static void __init ibm_handle_init(char *name,
275 acpi_handle * handle, acpi_handle parent,
276 char **paths, int num_paths, char **path)
277{
278 int i;
279 acpi_status status;
280
281 for (i = 0; i < num_paths; i++) {
282 status = acpi_get_handle(parent, paths[i], handle);
283 if (ACPI_SUCCESS(status)) {
284 *path = paths[i];
285 return;
286 }
287 }
288
289 *handle = NULL;
290}
291
292static void dispatch_notify(acpi_handle handle, u32 event, void *data)
293{
294 struct ibm_struct *ibm = data;
295
296 if (!ibm || !ibm->notify)
297 return;
298
299 ibm->notify(ibm, event);
300}
301
302static int __init setup_notify(struct ibm_struct *ibm)
303{
304 acpi_status status;
305 int ret;
306
307 if (!*ibm->handle)
308 return 0;
309
310 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
311 if (ret < 0) {
312 printk(IBM_ERR "%s device not present\n", ibm->name);
313 return -ENODEV;
314 }
315
316 acpi_driver_data(ibm->device) = ibm;
317 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
318
319 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
320 dispatch_notify, ibm);
321 if (ACPI_FAILURE(status)) {
322 if (status == AE_ALREADY_EXISTS) {
323 printk(IBM_NOTICE "another device driver is already handling %s events\n",
324 ibm->name);
325 } else {
326 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
327 ibm->name, status);
328 }
329 return -ENODEV;
330 }
331 ibm->notify_installed = 1;
332 return 0;
333}
334
335static int __init ibm_device_add(struct acpi_device *device)
336{
337 return 0;
338}
339
340static int __init register_ibmacpi_subdriver(struct ibm_struct *ibm)
341{
342 int ret;
343
344 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
345 if (!ibm->driver) {
346 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
347 return -1;
348 }
349
350 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
351 ibm->driver->ids = ibm->hid;
352 ibm->driver->ops.add = &ibm_device_add;
353
354 ret = acpi_bus_register_driver(ibm->driver);
355 if (ret < 0) {
356 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
357 ibm->hid, ret);
358 kfree(ibm->driver);
359 }
360
361 return ret;
362}
363
364
365/****************************************************************************
366 ****************************************************************************
367 *
368 * Procfs Helpers
369 *
370 ****************************************************************************
371 ****************************************************************************/
372
373static int dispatch_read(char *page, char **start, off_t off, int count,
374 int *eof, void *data)
375{
376 struct ibm_struct *ibm = data;
377 int len;
378
379 if (!ibm || !ibm->read)
380 return -EINVAL;
381
382 len = ibm->read(page);
383 if (len < 0)
384 return len;
385
386 if (len <= off + count)
387 *eof = 1;
388 *start = page + off;
389 len -= off;
390 if (len > count)
391 len = count;
392 if (len < 0)
393 len = 0;
394
395 return len;
396}
397
398static int dispatch_write(struct file *file, const char __user * userbuf,
399 unsigned long count, void *data)
400{
401 struct ibm_struct *ibm = data;
402 char *kernbuf;
403 int ret;
404
405 if (!ibm || !ibm->write)
406 return -EINVAL;
407
408 kernbuf = kmalloc(count + 2, GFP_KERNEL);
409 if (!kernbuf)
410 return -ENOMEM;
411
412 if (copy_from_user(kernbuf, userbuf, count)) {
413 kfree(kernbuf);
414 return -EFAULT;
415 }
416
417 kernbuf[count] = 0;
418 strcat(kernbuf, ",");
419 ret = ibm->write(kernbuf);
420 if (ret == 0)
421 ret = count;
422
423 kfree(kernbuf);
424
425 return ret;
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static char *next_cmd(char **cmds)
429{
430 char *start = *cmds;
431 char *end;
432
433 while ((end = strchr(start, ',')) && end == start)
434 start = end + 1;
435
436 if (!end)
437 return NULL;
438
439 *end = 0;
440 *cmds = end + 1;
441 return start;
442}
443
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300444
445/****************************************************************************
446 ****************************************************************************
447 *
448 * Subdrivers
449 *
450 ****************************************************************************
451 ****************************************************************************/
452
453/*************************************************************************
454 * ibm-acpi init subdriver
455 */
456
Adrian Bunk1f217822006-12-19 13:01:28 -0800457static int ibm_acpi_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
460 printk(IBM_INFO "%s\n", IBM_URL);
461
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200462 if (ibm_thinkpad_ec_found)
463 printk(IBM_INFO "ThinkPad EC firmware %s\n",
464 ibm_thinkpad_ec_found);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467}
468
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -0300469static int ibm_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 int len = 0;
472
473 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
474 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
475
476 return len;
477}
478
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300479/*************************************************************************
480 * Hotkey subdriver
481 */
482
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400483static int hotkey_supported;
484static int hotkey_mask_supported;
485static int hotkey_orig_status;
486static int hotkey_orig_mask;
487
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300488static int hotkey_init(void)
489{
490 /* hotkey not supported on 570 */
491 hotkey_supported = hkey_handle != NULL;
492
493 if (hotkey_supported) {
494 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
495 A30, R30, R31, T20-22, X20-21, X22-24 */
496 hotkey_mask_supported =
497 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
498
499 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
500 return -ENODEV;
501 }
502
503 return 0;
504}
505
506static void hotkey_exit(void)
507{
508 if (hotkey_supported)
509 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
510}
511
512static void hotkey_notify(struct ibm_struct *ibm, u32 event)
513{
514 int hkey;
515
516 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
517 acpi_bus_generate_event(ibm->device, event, hkey);
518 else {
519 printk(IBM_ERR "unknown hotkey event %d\n", event);
520 acpi_bus_generate_event(ibm->device, event, 0);
521 }
522}
523
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400524static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400527 return 0;
528
529 if (hotkey_mask_supported)
530 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
531 return 0;
532
533 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400536static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 int i;
539
540 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 0;
542
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543 if (hotkey_mask_supported)
544 for (i = 0; i < 32; i++) {
545 int bit = ((1 << i) & mask) != 0;
546 if (!acpi_evalf(hkey_handle,
547 NULL, "MHKM", "vdd", i + 1, bit))
548 return 0;
549 }
550
551 return 1;
552}
553
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400554static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 int status, mask;
557 int len = 0;
558
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400559 if (!hotkey_supported) {
560 len += sprintf(p + len, "status:\t\tnot supported\n");
561 return len;
562 }
563
564 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EIO;
566
567 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400568 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
570 len += sprintf(p + len,
571 "commands:\tenable, disable, reset, <mask>\n");
572 } else {
573 len += sprintf(p + len, "mask:\t\tnot supported\n");
574 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
575 }
576
577 return len;
578}
579
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400580static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 int status, mask;
583 char *cmd;
584 int do_cmd = 0;
585
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400586 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return -ENODEV;
588
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400589 if (!hotkey_get(&status, &mask))
590 return -EIO;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 while ((cmd = next_cmd(&buf))) {
593 if (strlencmp(cmd, "enable") == 0) {
594 status = 1;
595 } else if (strlencmp(cmd, "disable") == 0) {
596 status = 0;
597 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400598 status = hotkey_orig_status;
599 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
601 /* mask set */
602 } else if (sscanf(cmd, "%x", &mask) == 1) {
603 /* mask set */
604 } else
605 return -EINVAL;
606 do_cmd = 1;
607 }
608
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400609 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return -EIO;
611
612 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400613}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300615/*************************************************************************
616 * Bluetooth subdriver
617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400619static int bluetooth_supported;
620
621static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400623 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
624 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
625 bluetooth_supported = hkey_handle &&
626 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 return 0;
629}
630
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400631static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 int status;
634
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400635 if (!bluetooth_supported ||
636 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 status = 0;
638
639 return status;
640}
641
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400642static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400645 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400647 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 len += sprintf(p + len, "status:\t\tnot supported\n");
649 else if (!(status & 1))
650 len += sprintf(p + len, "status:\t\tnot installed\n");
651 else {
652 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
653 len += sprintf(p + len, "commands:\tenable, disable\n");
654 }
655
656 return len;
657}
658
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400659static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400661 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 char *cmd;
663 int do_cmd = 0;
664
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400665 if (!bluetooth_supported)
666 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 while ((cmd = next_cmd(&buf))) {
669 if (strlencmp(cmd, "enable") == 0) {
670 status |= 2;
671 } else if (strlencmp(cmd, "disable") == 0) {
672 status &= ~2;
673 } else
674 return -EINVAL;
675 do_cmd = 1;
676 }
677
678 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400679 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 return 0;
682}
683
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300684/*************************************************************************
685 * Wan subdriver
686 */
687
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400688static int wan_supported;
689
690static int wan_init(void)
691{
692 wan_supported = hkey_handle &&
693 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
694
695 return 0;
696}
697
698static int wan_status(void)
699{
700 int status;
701
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200702 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400703 status = 0;
704
705 return status;
706}
707
708static int wan_read(char *p)
709{
710 int len = 0;
711 int status = wan_status();
712
713 if (!wan_supported)
714 len += sprintf(p + len, "status:\t\tnot supported\n");
715 else if (!(status & 1))
716 len += sprintf(p + len, "status:\t\tnot installed\n");
717 else {
718 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
719 len += sprintf(p + len, "commands:\tenable, disable\n");
720 }
721
722 return len;
723}
724
725static int wan_write(char *buf)
726{
727 int status = wan_status();
728 char *cmd;
729 int do_cmd = 0;
730
731 if (!wan_supported)
732 return -ENODEV;
733
734 while ((cmd = next_cmd(&buf))) {
735 if (strlencmp(cmd, "enable") == 0) {
736 status |= 2;
737 } else if (strlencmp(cmd, "disable") == 0) {
738 status &= ~2;
739 } else
740 return -EINVAL;
741 do_cmd = 1;
742 }
743
744 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
745 return -EIO;
746
747 return 0;
748}
749
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300750/*************************************************************************
751 * Video subdriver
752 */
753
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200754static enum video_access_mode video_supported;
755static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400756
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300757IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
758 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
759 "\\_SB.PCI0.VID0", /* 770e */
760 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
761 "\\_SB.PCI0.AGP.VID", /* all others */
762 ); /* R30, R31 */
763
764IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
765
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400766static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400768 int ivga;
769
770 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
771 /* G41, assume IVGA doesn't change */
772 vid_handle = vid2_handle;
773
774 if (!vid_handle)
775 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200776 video_supported = IBMACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400777 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
778 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200779 video_supported = IBMACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400780 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
781 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200782 video_supported = IBMACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400783 else
784 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200785 video_supported = IBMACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 return 0;
788}
789
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300790static void video_exit(void)
791{
792 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
793}
794
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400795static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 int status = 0;
798 int i;
799
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200800 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400801 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
802 status = i & 3;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200803 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400804 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
805 status |= 0x01 * i;
806 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
807 status |= 0x02 * i;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200808 } else if (video_supported == IBMACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400809 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
810 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
811 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400813 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
814 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
815 status |= 0x01 * i;
816 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
817 status |= 0x08 * i;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 return status;
821}
822
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400823static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400825 int autosw = 0;
826
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200827 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400828 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200829 else if (video_supported == IBMACPI_VIDEO_770 ||
830 video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400831 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
832
833 return autosw & 1;
834}
835
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400836static int video_switch(void)
837{
838 int autosw = video_autosw();
839 int ret;
840
841 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
842 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200843 ret = video_supported == IBMACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400844 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
845 acpi_evalf(vid_handle, NULL, "VSWT", "v");
846 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
847
848 return ret;
849}
850
851static int video_expand(void)
852{
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200853 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400854 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200855 else if (video_supported == IBMACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400856 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
857 else
858 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
859}
860
861static int video_switch2(int status)
862{
863 int ret;
864
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200865 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400866 ret = acpi_evalf(NULL, NULL,
867 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200868 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400869 int autosw = video_autosw();
870 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
871 return -EIO;
872
873 ret = acpi_evalf(vid_handle, NULL,
874 "ASWT", "vdd", status * 0x100, 0);
875
876 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
877 } else {
878 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
879 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
880 }
881
882 return ret;
883}
884
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300885static int video_read(char *p)
886{
887 int status = video_status();
888 int autosw = video_autosw();
889 int len = 0;
890
891 if (!video_supported) {
892 len += sprintf(p + len, "status:\t\tnot supported\n");
893 return len;
894 }
895
896 len += sprintf(p + len, "status:\t\tsupported\n");
897 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
898 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
899 if (video_supported == IBMACPI_VIDEO_NEW)
900 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
901 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
902 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
903 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
904 if (video_supported == IBMACPI_VIDEO_NEW)
905 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
906 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
907 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
908
909 return len;
910}
911
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400912static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 char *cmd;
915 int enable, disable, status;
916
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400917 if (!video_supported)
918 return -ENODEV;
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 enable = disable = 0;
921
922 while ((cmd = next_cmd(&buf))) {
923 if (strlencmp(cmd, "lcd_enable") == 0) {
924 enable |= 0x01;
925 } else if (strlencmp(cmd, "lcd_disable") == 0) {
926 disable |= 0x01;
927 } else if (strlencmp(cmd, "crt_enable") == 0) {
928 enable |= 0x02;
929 } else if (strlencmp(cmd, "crt_disable") == 0) {
930 disable |= 0x02;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200931 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400932 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 enable |= 0x08;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200934 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400935 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 disable |= 0x08;
937 } else if (strlencmp(cmd, "auto_enable") == 0) {
938 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
939 return -EIO;
940 } else if (strlencmp(cmd, "auto_disable") == 0) {
941 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
942 return -EIO;
943 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400944 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -EIO;
946 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400947 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -EIO;
949 } else
950 return -EINVAL;
951 }
952
953 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400954 status = (video_status() & 0x0f & ~disable) | enable;
955 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -EIO;
957 }
958
959 return 0;
960}
961
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300962/*************************************************************************
963 * Light (thinklight) subdriver
964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400966static int light_supported;
967static int light_status_supported;
968
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300969IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
970IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
971
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400972static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400974 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
975 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
976
977 if (light_supported)
978 /* light status not supported on
979 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
980 light_status_supported = acpi_evalf(ec_handle, NULL,
981 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 return 0;
984}
985
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400986static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 int len = 0;
989 int status = 0;
990
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400991 if (!light_supported) {
992 len += sprintf(p + len, "status:\t\tnot supported\n");
993 } else if (!light_status_supported) {
994 len += sprintf(p + len, "status:\t\tunknown\n");
995 len += sprintf(p + len, "commands:\ton, off\n");
996 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
998 return -EIO;
999 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001000 len += sprintf(p + len, "commands:\ton, off\n");
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 return len;
1004}
1005
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001006static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 int cmos_cmd, lght_cmd;
1009 char *cmd;
1010 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001011
1012 if (!light_supported)
1013 return -ENODEV;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 while ((cmd = next_cmd(&buf))) {
1016 if (strlencmp(cmd, "on") == 0) {
1017 cmos_cmd = 0x0c;
1018 lght_cmd = 1;
1019 } else if (strlencmp(cmd, "off") == 0) {
1020 cmos_cmd = 0x0d;
1021 lght_cmd = 0;
1022 } else
1023 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001026 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1027 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (!success)
1029 return -EIO;
1030 }
1031
1032 return 0;
1033}
1034
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001035/*************************************************************************
1036 * Dock subdriver
1037 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001039/* don't list other alternatives as we install a notify handler on the 570 */
1040IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuhd2fadbb2007-01-11 02:58:15 -05001041
Kristen Accardi63e5f242006-02-23 17:56:06 -08001042#ifdef CONFIG_ACPI_IBM_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001043
1044IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1045 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1046 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1047 "\\_SB.PCI.ISA.SLCE", /* 570 */
1048 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#define dock_docked() (_sta(dock_handle) & 1)
1051
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001052static void dock_notify(struct ibm_struct *ibm, u32 event)
1053{
1054 int docked = dock_docked();
1055 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1056
1057 if (event == 1 && !pci) /* 570 */
1058 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1059 else if (event == 1 && pci) /* 570 */
1060 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1061 else if (event == 3 && docked)
1062 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1063 else if (event == 3 && !docked)
1064 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1065 else if (event == 0 && docked)
1066 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1067 else {
1068 printk(IBM_ERR "unknown dock event %d, status %d\n",
1069 event, _sta(dock_handle));
1070 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1071 }
1072}
1073
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001074static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 int len = 0;
1077 int docked = dock_docked();
1078
1079 if (!dock_handle)
1080 len += sprintf(p + len, "status:\t\tnot supported\n");
1081 else if (!docked)
1082 len += sprintf(p + len, "status:\t\tundocked\n");
1083 else {
1084 len += sprintf(p + len, "status:\t\tdocked\n");
1085 len += sprintf(p + len, "commands:\tdock, undock\n");
1086 }
1087
1088 return len;
1089}
1090
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001091static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 char *cmd;
1094
1095 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001096 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 while ((cmd = next_cmd(&buf))) {
1099 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001100 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1101 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return -EIO;
1103 } else if (strlencmp(cmd, "dock") == 0) {
1104 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1105 return -EIO;
1106 } else
1107 return -EINVAL;
1108 }
1109
1110 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001113#endif /* CONFIG_ACPI_IBM_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001115/*************************************************************************
1116 * Bay subdriver
1117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001119#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001120static int bay_status_supported;
1121static int bay_status2_supported;
1122static int bay_eject_supported;
1123static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001125IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1126 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1127 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1128 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1129 ); /* A21e, R30, R31 */
1130IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1131 "_EJ0", /* all others */
1132 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1133IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1134 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1135 ); /* all others */
1136IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1137 "_EJ0", /* 770x */
1138 ); /* all others */
1139
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001140static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001142 bay_status_supported = bay_handle &&
1143 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1144 bay_status2_supported = bay2_handle &&
1145 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1146
1147 bay_eject_supported = bay_handle && bay_ej_handle &&
1148 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1149 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1150 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 return 0;
1153}
1154
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001155static void bay_notify(struct ibm_struct *ibm, u32 event)
1156{
1157 acpi_bus_generate_event(ibm->device, event, 0);
1158}
1159
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001160#define bay_occupied(b) (_sta(b##_handle) & 1)
1161
1162static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001165 int occupied = bay_occupied(bay);
1166 int occupied2 = bay_occupied(bay2);
1167 int eject, eject2;
1168
1169 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1170 (occupied ? "occupied" : "unoccupied") :
1171 "not supported");
1172 if (bay_status2_supported)
1173 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1174 "occupied" : "unoccupied");
1175
1176 eject = bay_eject_supported && occupied;
1177 eject2 = bay_eject2_supported && occupied2;
1178
1179 if (eject && eject2)
1180 len += sprintf(p + len, "commands:\teject, eject2\n");
1181 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001183 else if (eject2)
1184 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 return len;
1187}
1188
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001189static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 char *cmd;
1192
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001193 if (!bay_eject_supported && !bay_eject2_supported)
1194 return -ENODEV;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001197 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1198 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1199 return -EIO;
1200 } else if (bay_eject2_supported &&
1201 strlencmp(cmd, "eject2") == 0) {
1202 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -EIO;
1204 } else
1205 return -EINVAL;
1206 }
1207
1208 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001209}
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001210#endif /* CONFIG_ACPI_IBM_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001212/*************************************************************************
1213 * CMOS subdriver
1214 */
1215
1216static int cmos_eval(int cmos_cmd)
1217{
1218 if (cmos_handle)
1219 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1220 else
1221 return 1;
1222}
1223
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001224static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 int len = 0;
1227
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001228 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1229 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (!cmos_handle)
1231 len += sprintf(p + len, "status:\t\tnot supported\n");
1232 else {
1233 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001234 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
1237 return len;
1238}
1239
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001240static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 char *cmd;
1243 int cmos_cmd;
1244
1245 if (!cmos_handle)
1246 return -EINVAL;
1247
1248 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001249 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1250 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /* cmos_cmd set */
1252 } else
1253 return -EINVAL;
1254
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001255 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return -EIO;
1257 }
1258
1259 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001260}
1261
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001262
1263/*************************************************************************
1264 * LED subdriver
1265 */
1266
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001267static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001268
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001269IBM_HANDLE(led, ec, "SLED", /* 570 */
1270 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1271 "LED", /* all others */
1272 ); /* R30, R31 */
1273
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001274static int led_init(void)
1275{
1276 if (!led_handle)
1277 /* led not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001278 led_supported = IBMACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001279 else if (strlencmp(led_path, "SLED") == 0)
1280 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001281 led_supported = IBMACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001282 else if (strlencmp(led_path, "SYSL") == 0)
1283 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001284 led_supported = IBMACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001285 else
1286 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001287 led_supported = IBMACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001288
1289 return 0;
1290}
1291
1292#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1293
1294static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 int len = 0;
1297
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001298 if (!led_supported) {
1299 len += sprintf(p + len, "status:\t\tnot supported\n");
1300 return len;
1301 }
1302 len += sprintf(p + len, "status:\t\tsupported\n");
1303
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001304 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001305 /* 570 */
1306 int i, status;
1307 for (i = 0; i < 8; i++) {
1308 if (!acpi_evalf(ec_handle,
1309 &status, "GLED", "dd", 1 << i))
1310 return -EIO;
1311 len += sprintf(p + len, "%d:\t\t%s\n",
1312 i, led_status(status));
1313 }
1314 }
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001317 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 return len;
1320}
1321
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001322/* off, on, blink */
1323static const int led_sled_arg1[] = { 0, 1, 3 };
1324static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1325static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1326static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1327
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001328static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001331 int led, ind, ret;
1332
1333 if (!led_supported)
1334 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001337 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -EINVAL;
1339
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001340 if (strstr(cmd, "off")) {
1341 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001343 ind = 1;
1344 } else if (strstr(cmd, "blink")) {
1345 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 } else
1347 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001348
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001349 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001350 /* 570 */
1351 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001353 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001355 } else if (led_supported == IBMACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001356 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1357 led = 1 << led;
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001358 ret = ec_write(IBMACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001359 if (ret >= 0)
1360 ret =
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001361 ec_write(IBMACPI_LED_EC_HLBL,
1362 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001363 if (ret >= 0)
1364 ret =
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001365 ec_write(IBMACPI_LED_EC_HLCL,
1366 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001367 if (ret < 0)
1368 return ret;
1369 } else {
1370 /* all others */
1371 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1372 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
1377 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001378}
1379
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001380/*************************************************************************
1381 * Beep subdriver
1382 */
1383
1384IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1385
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001386static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 int len = 0;
1389
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001390 if (!beep_handle)
1391 len += sprintf(p + len, "status:\t\tnot supported\n");
1392 else {
1393 len += sprintf(p + len, "status:\t\tsupported\n");
1394 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 return len;
1398}
1399
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001400static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 char *cmd;
1403 int beep_cmd;
1404
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001405 if (!beep_handle)
1406 return -ENODEV;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001409 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1410 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /* beep_cmd set */
1412 } else
1413 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001414 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return -EIO;
1416 }
1417
1418 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001419}
1420
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001421/*************************************************************************
1422 * Thermal subdriver
1423 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001424
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001425static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001426
1427static int thermal_init(void)
1428{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001429 u8 t, ta1, ta2;
1430 int i;
1431 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001432
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001433 if (ibm_thinkpad_ec_found && experimental) {
1434 /*
1435 * Direct EC access mode: sensors at registers
1436 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1437 * non-implemented, thermal sensors return 0x80 when
1438 * not available
1439 */
1440
1441 ta1 = ta2 = 0;
1442 for (i = 0; i < 8; i++) {
1443 if (likely(acpi_ec_read(0x78 + i, &t))) {
1444 ta1 |= t;
1445 } else {
1446 ta1 = 0;
1447 break;
1448 }
1449 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1450 ta2 |= t;
1451 } else {
1452 ta1 = 0;
1453 break;
1454 }
1455 }
1456 if (ta1 == 0) {
1457 /* This is sheer paranoia, but we handle it anyway */
1458 if (acpi_tmp7) {
1459 printk(IBM_ERR
1460 "ThinkPad ACPI EC access misbehaving, "
1461 "falling back to ACPI TMPx access mode\n");
1462 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1463 } else {
1464 printk(IBM_ERR
1465 "ThinkPad ACPI EC access misbehaving, "
1466 "disabling thermal sensors access\n");
1467 thermal_read_mode = IBMACPI_THERMAL_NONE;
1468 }
1469 } else {
1470 thermal_read_mode =
1471 (ta2 != 0) ?
1472 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1473 }
1474 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001475 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1476 /* 600e/x, 770e, 770x */
1477 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1478 } else {
1479 /* Standard ACPI TMPx access, max 8 sensors */
1480 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1481 }
1482 } else {
1483 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1484 thermal_read_mode = IBMACPI_THERMAL_NONE;
1485 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001486
1487 return 0;
1488}
1489
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001490static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1491{
1492 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001493 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001494 char tmpi[] = "TMPi";
1495
1496 if (!s)
1497 return -EINVAL;
1498
1499 switch (thermal_read_mode) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001500#if IBMACPI_MAX_THERMAL_SENSORS >= 16
1501 case IBMACPI_THERMAL_TPEC_16:
1502 for (i = 0; i < 8; i++) {
1503 if (!acpi_ec_read(0xC0 + i, &tmp))
1504 return -EIO;
1505 s->temp[i + 8] = tmp * 1000;
1506 }
1507 /* fallthrough */
1508#endif
1509 case IBMACPI_THERMAL_TPEC_8:
1510 for (i = 0; i < 8; i++) {
1511 if (!acpi_ec_read(0x78 + i, &tmp))
1512 return -EIO;
1513 s->temp[i] = tmp * 1000;
1514 }
1515 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1516
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001517 case IBMACPI_THERMAL_ACPI_UPDT:
1518 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1519 return -EIO;
1520 for (i = 0; i < 8; i++) {
1521 tmpi[3] = '0' + i;
1522 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1523 return -EIO;
1524 s->temp[i] = (t - 2732) * 100;
1525 }
1526 return 8;
1527
1528 case IBMACPI_THERMAL_ACPI_TMP07:
1529 for (i = 0; i < 8; i++) {
1530 tmpi[3] = '0' + i;
1531 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1532 return -EIO;
1533 s->temp[i] = t * 1000;
1534 }
1535 return 8;
1536
1537 case IBMACPI_THERMAL_NONE:
1538 default:
1539 return 0;
1540 }
1541}
1542
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001543static int thermal_read(char *p)
1544{
1545 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001546 int n, i;
1547 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001548
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001549 n = thermal_get_sensors(&t);
1550 if (unlikely(n < 0))
1551 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001552
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001553 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001554
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001555 if (n > 0) {
1556 for (i = 0; i < (n - 1); i++)
1557 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1558 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1559 } else
1560 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001561
1562 return len;
1563}
1564
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001565/*************************************************************************
1566 * EC Dump subdriver
1567 */
1568
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001569static u8 ecdump_regs[256];
1570
1571static int ecdump_read(char *p)
1572{
1573 int len = 0;
1574 int i, j;
1575 u8 v;
1576
1577 len += sprintf(p + len, "EC "
1578 " +00 +01 +02 +03 +04 +05 +06 +07"
1579 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1580 for (i = 0; i < 256; i += 16) {
1581 len += sprintf(p + len, "EC 0x%02x:", i);
1582 for (j = 0; j < 16; j++) {
1583 if (!acpi_ec_read(i + j, &v))
1584 break;
1585 if (v != ecdump_regs[i + j])
1586 len += sprintf(p + len, " *%02x", v);
1587 else
1588 len += sprintf(p + len, " %02x", v);
1589 ecdump_regs[i + j] = v;
1590 }
1591 len += sprintf(p + len, "\n");
1592 if (j != 16)
1593 break;
1594 }
1595
1596 /* These are way too dangerous to advertise openly... */
1597#if 0
1598 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1599 " (<offset> is 00-ff, <value> is 00-ff)\n");
1600 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1601 " (<offset> is 00-ff, <value> is 0-255)\n");
1602#endif
1603 return len;
1604}
1605
1606static int ecdump_write(char *buf)
1607{
1608 char *cmd;
1609 int i, v;
1610
1611 while ((cmd = next_cmd(&buf))) {
1612 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1613 /* i and v set */
1614 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1615 /* i and v set */
1616 } else
1617 return -EINVAL;
1618 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1619 if (!acpi_ec_write(i, v))
1620 return -EIO;
1621 } else
1622 return -EINVAL;
1623 }
1624
1625 return 0;
1626}
1627
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001628/*************************************************************************
1629 * Backlight/brightness subdriver
1630 */
Holger Macht8acb0252006-10-20 14:30:28 -07001631
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001632static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07001633
Richard Purdie599a52d2007-02-10 23:07:48 +00001634static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001635 .get_brightness = brightness_get,
1636 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001637};
1638
1639static int brightness_init(void)
1640{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001641 int b;
1642
1643 b = brightness_get(NULL);
1644 if (b < 0)
1645 return b;
1646
Yu Luming519ab5f2006-12-19 12:56:15 -08001647 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001648 &ibm_backlight_data);
1649 if (IS_ERR(ibm_backlight_device)) {
1650 printk(IBM_ERR "Could not register backlight device\n");
1651 return PTR_ERR(ibm_backlight_device);
1652 }
1653
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001654 ibm_backlight_device->props.max_brightness = 7;
1655 ibm_backlight_device->props.brightness = b;
1656 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001657
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001658 return 0;
1659}
1660
1661static void brightness_exit(void)
1662{
1663 if (ibm_backlight_device) {
1664 backlight_device_unregister(ibm_backlight_device);
1665 ibm_backlight_device = NULL;
1666 }
1667}
1668
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001669static int brightness_update_status(struct backlight_device *bd)
1670{
1671 return brightness_set(
1672 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
1673 bd->props.power == FB_BLANK_UNBLANK) ?
1674 bd->props.brightness : 0);
1675}
1676
1677static int brightness_get(struct backlight_device *bd)
1678{
1679 u8 level;
1680 if (!acpi_ec_read(brightness_offset, &level))
1681 return -EIO;
1682
1683 level &= 0x7;
1684
1685 return level;
1686}
1687
1688static int brightness_set(int value)
1689{
1690 int cmos_cmd, inc, i;
1691 int current_value = brightness_get(NULL);
1692
1693 value &= 7;
1694
1695 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1696 inc = value > current_value ? 1 : -1;
1697 for (i = current_value; i != value; i += inc) {
1698 if (!cmos_eval(cmos_cmd))
1699 return -EIO;
1700 if (!acpi_ec_write(brightness_offset, i + inc))
1701 return -EIO;
1702 }
1703
1704 return 0;
1705}
1706
1707static int brightness_read(char *p)
1708{
1709 int len = 0;
1710 int level;
1711
1712 if ((level = brightness_get(NULL)) < 0) {
1713 len += sprintf(p + len, "level:\t\tunreadable\n");
1714 } else {
1715 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1716 len += sprintf(p + len, "commands:\tup, down\n");
1717 len += sprintf(p + len, "commands:\tlevel <level>"
1718 " (<level> is 0-7)\n");
1719 }
1720
1721 return len;
1722}
1723
1724static int brightness_write(char *buf)
1725{
1726 int level;
1727 int new_level;
1728 char *cmd;
1729
1730 while ((cmd = next_cmd(&buf))) {
1731 if ((level = brightness_get(NULL)) < 0)
1732 return level;
1733 level &= 7;
1734
1735 if (strlencmp(cmd, "up") == 0) {
1736 new_level = level == 7 ? 7 : level + 1;
1737 } else if (strlencmp(cmd, "down") == 0) {
1738 new_level = level == 0 ? 0 : level - 1;
1739 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1740 new_level >= 0 && new_level <= 7) {
1741 /* new_level set */
1742 } else
1743 return -EINVAL;
1744
1745 brightness_set(new_level);
1746 }
1747
1748 return 0;
1749}
1750
1751/*************************************************************************
1752 * Volume subdriver
1753 */
1754
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001755static int volume_read(char *p)
1756{
1757 int len = 0;
1758 u8 level;
1759
1760 if (!acpi_ec_read(volume_offset, &level)) {
1761 len += sprintf(p + len, "level:\t\tunreadable\n");
1762 } else {
1763 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1764 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1765 len += sprintf(p + len, "commands:\tup, down, mute\n");
1766 len += sprintf(p + len, "commands:\tlevel <level>"
1767 " (<level> is 0-15)\n");
1768 }
1769
1770 return len;
1771}
1772
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001773static int volume_write(char *buf)
1774{
1775 int cmos_cmd, inc, i;
1776 u8 level, mute;
1777 int new_level, new_mute;
1778 char *cmd;
1779
1780 while ((cmd = next_cmd(&buf))) {
1781 if (!acpi_ec_read(volume_offset, &level))
1782 return -EIO;
1783 new_mute = mute = level & 0x40;
1784 new_level = level = level & 0xf;
1785
1786 if (strlencmp(cmd, "up") == 0) {
1787 if (mute)
1788 new_mute = 0;
1789 else
1790 new_level = level == 15 ? 15 : level + 1;
1791 } else if (strlencmp(cmd, "down") == 0) {
1792 if (mute)
1793 new_mute = 0;
1794 else
1795 new_level = level == 0 ? 0 : level - 1;
1796 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1797 new_level >= 0 && new_level <= 15) {
1798 /* new_level set */
1799 } else if (strlencmp(cmd, "mute") == 0) {
1800 new_mute = 0x40;
1801 } else
1802 return -EINVAL;
1803
1804 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001805 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001806 inc = new_level > level ? 1 : -1;
1807
1808 if (mute && (!cmos_eval(cmos_cmd) ||
1809 !acpi_ec_write(volume_offset, level)))
1810 return -EIO;
1811
1812 for (i = level; i != new_level; i += inc)
1813 if (!cmos_eval(cmos_cmd) ||
1814 !acpi_ec_write(volume_offset, i + inc))
1815 return -EIO;
1816
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001817 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001818 !acpi_ec_write(volume_offset,
1819 new_level + mute)))
1820 return -EIO;
1821 }
1822
1823 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001824 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001825
1826 if (!cmos_eval(cmos_cmd) ||
1827 !acpi_ec_write(volume_offset, level + new_mute))
1828 return -EIO;
1829 }
1830 }
1831
1832 return 0;
1833}
1834
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001835
1836/*************************************************************************
1837 * Fan subdriver
1838 */
1839
1840/*
1841 * FAN ACCESS MODES
1842 *
1843 * IBMACPI_FAN_RD_ACPI_GFAN:
1844 * ACPI GFAN method: returns fan level
1845 *
1846 * see IBMACPI_FAN_WR_ACPI_SFAN
1847 * EC 0x2f not available if GFAN exists
1848 *
1849 * IBMACPI_FAN_WR_ACPI_SFAN:
1850 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
1851 *
1852 * EC 0x2f might be available *for reading*, but never for writing.
1853 *
1854 * IBMACPI_FAN_WR_TPEC:
1855 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
1856 * on almost all ThinkPads
1857 *
1858 * Fan speed changes of any sort (including those caused by the
1859 * disengaged mode) are usually done slowly by the firmware as the
1860 * maximum ammount of fan duty cycle change per second seems to be
1861 * limited.
1862 *
1863 * Reading is not available if GFAN exists.
1864 * Writing is not available if SFAN exists.
1865 *
1866 * Bits
1867 * 7 automatic mode engaged;
1868 * (default operation mode of the ThinkPad)
1869 * fan level is ignored in this mode.
1870 * 6 disengage mode (takes precedence over bit 7);
1871 * not available on all thinkpads. May disable
1872 * the tachometer, and speeds up fan to 100% duty-cycle,
1873 * which speeds it up far above the standard RPM
1874 * levels. It is not impossible that it could cause
1875 * hardware damage.
1876 * 5-3 unused in some models. Extra bits for fan level
1877 * in others, but still useless as all values above
1878 * 7 map to the same speed as level 7 in these models.
1879 * 2-0 fan level (0..7 usually)
1880 * 0x00 = stop
1881 * 0x07 = max (set when temperatures critical)
1882 * Some ThinkPads may have other levels, see
1883 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
1884 *
1885 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
1886 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
1887 * does so, its initial value is meaningless (0x07).
1888 *
1889 * For firmware bugs, refer to:
1890 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1891 *
1892 * ----
1893 *
1894 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
1895 * Main fan tachometer reading (in RPM)
1896 *
1897 * This register is present on all ThinkPads with a new-style EC, and
1898 * it is known not to be present on the A21m/e, and T22, as there is
1899 * something else in offset 0x84 according to the ACPI DSDT. Other
1900 * ThinkPads from this same time period (and earlier) probably lack the
1901 * tachometer as well.
1902 *
1903 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
1904 * was never fixed by IBM to report the EC firmware version string
1905 * probably support the tachometer (like the early X models), so
1906 * detecting it is quite hard. We need more data to know for sure.
1907 *
1908 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
1909 * might result.
1910 *
1911 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
1912 * register is not invalidated in ThinkPads that disable tachometer
1913 * readings. Thus, the tachometer readings go stale.
1914 *
1915 * For firmware bugs, refer to:
1916 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1917 *
1918 * IBMACPI_FAN_WR_ACPI_FANS:
1919 * ThinkPad X31, X40, X41. Not available in the X60.
1920 *
1921 * FANS ACPI handle: takes three arguments: low speed, medium speed,
1922 * high speed. ACPI DSDT seems to map these three speeds to levels
1923 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
1924 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
1925 *
1926 * The speeds are stored on handles
1927 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
1928 *
1929 * There are three default speed sets, acessible as handles:
1930 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
1931 *
1932 * ACPI DSDT switches which set is in use depending on various
1933 * factors.
1934 *
1935 * IBMACPI_FAN_WR_TPEC is also available and should be used to
1936 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
1937 * but the ACPI tables just mention level 7.
1938 */
1939
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001940static enum fan_status_access_mode fan_status_access_mode;
1941static enum fan_control_access_mode fan_control_access_mode;
1942static enum fan_control_commands fan_control_commands;
1943
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001944static int fan_control_status_known;
1945static u8 fan_control_initial_status;
1946
Len Brown25c68a32006-12-08 04:43:41 -05001947static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001948static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05001949static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001950
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001951IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
1952IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
1953 "\\FSPD", /* 600e/x, 770e, 770x */
1954 ); /* all others */
1955IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
1956 "JFNS", /* 770x-JL */
1957 ); /* all others */
1958
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001959static int fan_init(void)
1960{
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001961 fan_status_access_mode = IBMACPI_FAN_NONE;
1962 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1963 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001964 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001965 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001966
1967 if (gfan_handle) {
1968 /* 570, 600e/x, 770e, 770x */
1969 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1970 } else {
1971 /* all other ThinkPads: note that even old-style
1972 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001973 if (likely(acpi_ec_read(fan_status_offset,
1974 &fan_control_initial_status))) {
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001975 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001976
1977 /* In some ThinkPads, neither the EC nor the ACPI
1978 * DSDT initialize the fan status, and it ends up
1979 * being set to 0x07 when it *could* be either
1980 * 0x07 or 0x80.
1981 *
1982 * Enable for TP-1Y (T43), TP-78 (R51e),
1983 * TP-76 (R52), TP-70 (T43, R52), which are known
1984 * to be buggy. */
1985 if (fan_control_initial_status == 0x07 &&
1986 ibm_thinkpad_ec_found &&
1987 ((ibm_thinkpad_ec_found[0] == '1' &&
1988 ibm_thinkpad_ec_found[1] == 'Y') ||
1989 (ibm_thinkpad_ec_found[0] == '7' &&
1990 (ibm_thinkpad_ec_found[1] == '6' ||
1991 ibm_thinkpad_ec_found[1] == '8' ||
1992 ibm_thinkpad_ec_found[1] == '0'))
1993 )) {
1994 printk(IBM_NOTICE
1995 "fan_init: initial fan status is "
1996 "unknown, assuming it is in auto "
1997 "mode\n");
1998 fan_control_status_known = 0;
1999 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002000 } else {
2001 printk(IBM_ERR
2002 "ThinkPad ACPI EC access misbehaving, "
2003 "fan status and control unavailable\n");
2004 return 0;
2005 }
2006 }
2007
2008 if (sfan_handle) {
2009 /* 570, 770x-JL */
2010 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002011 fan_control_commands |=
2012 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002013 } else {
2014 if (!gfan_handle) {
2015 /* gfan without sfan means no fan control */
2016 /* all other models implement TP EC 0x2f control */
2017
2018 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002019 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002020 fan_control_access_mode =
2021 IBMACPI_FAN_WR_ACPI_FANS;
2022 fan_control_commands |=
2023 IBMACPI_FAN_CMD_SPEED |
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002024 IBMACPI_FAN_CMD_LEVEL |
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002025 IBMACPI_FAN_CMD_ENABLE;
2026 } else {
2027 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002028 fan_control_commands |=
2029 IBMACPI_FAN_CMD_LEVEL |
2030 IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002031 }
2032 }
2033 }
2034
2035 return 0;
2036}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002037
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002038static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002039{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002040 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002041
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002042 /* TODO:
2043 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
2044
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002045 switch (fan_status_access_mode) {
2046 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002047 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002048
2049 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002050 return -EIO;
2051
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002052 if (likely(status))
2053 *status = s & 0x07;
2054
2055 break;
2056
2057 case IBMACPI_FAN_RD_TPEC:
2058 /* all except 570, 600e/x, 770e, 770x */
2059 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2060 return -EIO;
2061
2062 if (likely(status))
2063 *status = s;
2064
2065 break;
2066
2067 default:
2068 return -ENXIO;
2069 }
2070
2071 return 0;
2072}
2073
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002074static void fan_exit(void)
2075{
2076 cancel_delayed_work(&fan_watchdog_task);
2077 flush_scheduled_work();
2078}
2079
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002080static int fan_get_speed(unsigned int *speed)
2081{
2082 u8 hi, lo;
2083
2084 switch (fan_status_access_mode) {
2085 case IBMACPI_FAN_RD_TPEC:
2086 /* all except 570, 600e/x, 770e, 770x */
2087 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2088 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2089 return -EIO;
2090
2091 if (likely(speed))
2092 *speed = (hi << 8) | lo;
2093
2094 break;
2095
2096 default:
2097 return -ENXIO;
2098 }
2099
2100 return 0;
2101}
2102
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002103static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002104{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002105 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2106 if (fan_set_enable()) {
2107 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2108 /* reschedule for later */
2109 fan_watchdog_reset();
2110 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002111}
2112
2113static void fan_watchdog_reset(void)
2114{
2115 static int fan_watchdog_active = 0;
2116
2117 if (fan_watchdog_active)
2118 cancel_delayed_work(&fan_watchdog_task);
2119
2120 if (fan_watchdog_maxinterval > 0) {
2121 fan_watchdog_active = 1;
2122 if (!schedule_delayed_work(&fan_watchdog_task,
2123 msecs_to_jiffies(fan_watchdog_maxinterval
2124 * 1000))) {
2125 printk(IBM_ERR "failed to schedule the fan watchdog, "
2126 "watchdog will not trigger\n");
2127 }
2128 } else
2129 fan_watchdog_active = 0;
2130}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002131
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002132static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002133{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002134 switch (fan_control_access_mode) {
2135 case IBMACPI_FAN_WR_ACPI_SFAN:
2136 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002137 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2138 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002139 } else
2140 return -EINVAL;
2141 break;
2142
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002143 case IBMACPI_FAN_WR_ACPI_FANS:
2144 case IBMACPI_FAN_WR_TPEC:
2145 if ((level != IBMACPI_FAN_EC_AUTO) &&
2146 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2147 ((level < 0) || (level > 7)))
2148 return -EINVAL;
2149
2150 if (!acpi_ec_write(fan_status_offset, level))
2151 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002152 else
2153 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002154 break;
2155
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002156 default:
2157 return -ENXIO;
2158 }
2159 return 0;
2160}
2161
2162static int fan_set_enable(void)
2163{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002164 u8 s;
2165 int rc;
2166
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002167 switch (fan_control_access_mode) {
2168 case IBMACPI_FAN_WR_ACPI_FANS:
2169 case IBMACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002170 if ((rc = fan_get_status(&s)) < 0)
2171 return rc;
2172
2173 /* Don't go out of emergency fan mode */
2174 if (s != 7)
2175 s = IBMACPI_FAN_EC_AUTO;
2176
2177 if (!acpi_ec_write(fan_status_offset, s))
2178 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002179 else
2180 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002181 break;
2182
2183 case IBMACPI_FAN_WR_ACPI_SFAN:
2184 if ((rc = fan_get_status(&s)) < 0)
2185 return rc;
2186
2187 s &= 0x07;
2188
2189 /* Set fan to at least level 4 */
2190 if (s < 4)
2191 s = 4;
2192
2193 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002194 return -EIO;
2195 break;
2196
2197 default:
2198 return -ENXIO;
2199 }
2200 return 0;
2201}
2202
2203static int fan_set_disable(void)
2204{
2205 switch (fan_control_access_mode) {
2206 case IBMACPI_FAN_WR_ACPI_FANS:
2207 case IBMACPI_FAN_WR_TPEC:
2208 if (!acpi_ec_write(fan_status_offset, 0x00))
2209 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002210 else
2211 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002212 break;
2213
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002214 case IBMACPI_FAN_WR_ACPI_SFAN:
2215 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2216 return -EIO;
2217 break;
2218
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002219 default:
2220 return -ENXIO;
2221 }
2222 return 0;
2223}
2224
2225static int fan_set_speed(int speed)
2226{
2227 switch (fan_control_access_mode) {
2228 case IBMACPI_FAN_WR_ACPI_FANS:
2229 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002230 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2231 speed, speed, speed))
2232 return -EIO;
2233 } else
2234 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002235 break;
2236
2237 default:
2238 return -ENXIO;
2239 }
2240 return 0;
2241}
2242
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002243static int fan_read(char *p)
2244{
2245 int len = 0;
2246 int rc;
2247 u8 status;
2248 unsigned int speed = 0;
2249
2250 switch (fan_status_access_mode) {
2251 case IBMACPI_FAN_RD_ACPI_GFAN:
2252 /* 570, 600e/x, 770e, 770x */
2253 if ((rc = fan_get_status(&status)) < 0)
2254 return rc;
2255
2256 len += sprintf(p + len, "status:\t\t%s\n"
2257 "level:\t\t%d\n",
2258 (status != 0) ? "enabled" : "disabled", status);
2259 break;
2260
2261 case IBMACPI_FAN_RD_TPEC:
2262 /* all except 570, 600e/x, 770e, 770x */
2263 if ((rc = fan_get_status(&status)) < 0)
2264 return rc;
2265
2266 if (unlikely(!fan_control_status_known)) {
2267 if (status != fan_control_initial_status)
2268 fan_control_status_known = 1;
2269 else
2270 /* Return most likely status. In fact, it
2271 * might be the only possible status */
2272 status = IBMACPI_FAN_EC_AUTO;
2273 }
2274
2275 len += sprintf(p + len, "status:\t\t%s\n",
2276 (status != 0) ? "enabled" : "disabled");
2277
2278 /* No ThinkPad boots on disengaged mode, we can safely
2279 * assume the tachometer is online if fan control status
2280 * was unknown */
2281 if ((rc = fan_get_speed(&speed)) < 0)
2282 return rc;
2283
2284 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2285
2286 if (status & IBMACPI_FAN_EC_DISENGAGED)
2287 /* Disengaged mode takes precedence */
2288 len += sprintf(p + len, "level:\t\tdisengaged\n");
2289 else if (status & IBMACPI_FAN_EC_AUTO)
2290 len += sprintf(p + len, "level:\t\tauto\n");
2291 else
2292 len += sprintf(p + len, "level:\t\t%d\n", status);
2293 break;
2294
2295 case IBMACPI_FAN_NONE:
2296 default:
2297 len += sprintf(p + len, "status:\t\tnot supported\n");
2298 }
2299
2300 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2301 len += sprintf(p + len, "commands:\tlevel <level>");
2302
2303 switch (fan_control_access_mode) {
2304 case IBMACPI_FAN_WR_ACPI_SFAN:
2305 len += sprintf(p + len, " (<level> is 0-7)\n");
2306 break;
2307
2308 default:
2309 len += sprintf(p + len, " (<level> is 0-7, "
2310 "auto, disengaged)\n");
2311 break;
2312 }
2313 }
2314
2315 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
2316 len += sprintf(p + len, "commands:\tenable, disable\n"
2317 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2318 "1-120 (seconds))\n");
2319
2320 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
2321 len += sprintf(p + len, "commands:\tspeed <speed>"
2322 " (<speed> is 0-65535)\n");
2323
2324 return len;
2325}
2326
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002327static int fan_write_cmd_level(const char *cmd, int *rc)
2328{
2329 int level;
2330
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002331 if (strlencmp(cmd, "level auto") == 0)
2332 level = IBMACPI_FAN_EC_AUTO;
2333 else if (strlencmp(cmd, "level disengaged") == 0)
2334 level = IBMACPI_FAN_EC_DISENGAGED;
2335 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002336 return 0;
2337
2338 if ((*rc = fan_set_level(level)) == -ENXIO)
2339 printk(IBM_ERR "level command accepted for unsupported "
2340 "access mode %d", fan_control_access_mode);
2341
2342 return 1;
2343}
2344
2345static int fan_write_cmd_enable(const char *cmd, int *rc)
2346{
2347 if (strlencmp(cmd, "enable") != 0)
2348 return 0;
2349
2350 if ((*rc = fan_set_enable()) == -ENXIO)
2351 printk(IBM_ERR "enable command accepted for unsupported "
2352 "access mode %d", fan_control_access_mode);
2353
2354 return 1;
2355}
2356
2357static int fan_write_cmd_disable(const char *cmd, int *rc)
2358{
2359 if (strlencmp(cmd, "disable") != 0)
2360 return 0;
2361
2362 if ((*rc = fan_set_disable()) == -ENXIO)
2363 printk(IBM_ERR "disable command accepted for unsupported "
2364 "access mode %d", fan_control_access_mode);
2365
2366 return 1;
2367}
2368
2369static int fan_write_cmd_speed(const char *cmd, int *rc)
2370{
2371 int speed;
2372
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002373 /* TODO:
2374 * Support speed <low> <medium> <high> ? */
2375
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002376 if (sscanf(cmd, "speed %d", &speed) != 1)
2377 return 0;
2378
2379 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2380 printk(IBM_ERR "speed command accepted for unsupported "
2381 "access mode %d", fan_control_access_mode);
2382
2383 return 1;
2384}
2385
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002386static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2387{
2388 int interval;
2389
2390 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2391 return 0;
2392
2393 if (interval < 0 || interval > 120)
2394 *rc = -EINVAL;
2395 else
2396 fan_watchdog_maxinterval = interval;
2397
2398 return 1;
2399}
2400
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002401static int fan_write(char *buf)
2402{
2403 char *cmd;
2404 int rc = 0;
2405
2406 while (!rc && (cmd = next_cmd(&buf))) {
2407 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2408 fan_write_cmd_level(cmd, &rc)) &&
2409 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2410 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002411 fan_write_cmd_disable(cmd, &rc) ||
2412 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002413 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2414 fan_write_cmd_speed(cmd, &rc))
2415 )
2416 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002417 else if (!rc)
2418 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002419 }
2420
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002421 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002422}
2423
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002424/****************************************************************************
2425 ****************************************************************************
2426 *
2427 * Infrastructure
2428 *
2429 ****************************************************************************
2430 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002432/* /proc support */
2433static struct proc_dir_entry *proc_dir = NULL;
2434
2435/* Subdriver registry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436static struct ibm_struct ibms[] = {
2437 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002438 .name = "driver",
Adrian Bunk1f217822006-12-19 13:01:28 -08002439 .init = ibm_acpi_driver_init,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002440 .read = ibm_acpi_driver_read,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002441 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002443 .name = "hotkey",
2444 .hid = IBM_HKEY_HID,
2445 .init = hotkey_init,
2446 .read = hotkey_read,
2447 .write = hotkey_write,
2448 .exit = hotkey_exit,
2449 .notify = hotkey_notify,
2450 .handle = &hkey_handle,
2451 .type = ACPI_DEVICE_NOTIFY,
2452 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002454 .name = "bluetooth",
2455 .init = bluetooth_init,
2456 .read = bluetooth_read,
2457 .write = bluetooth_write,
2458 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002460 .name = "wan",
2461 .init = wan_init,
2462 .read = wan_read,
2463 .write = wan_write,
2464 .experimental = 1,
2465 },
2466 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002467 .name = "video",
2468 .init = video_init,
2469 .read = video_read,
2470 .write = video_write,
2471 .exit = video_exit,
2472 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002474 .name = "light",
2475 .init = light_init,
2476 .read = light_read,
2477 .write = light_write,
2478 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002479#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481 .name = "dock",
2482 .read = dock_read,
2483 .write = dock_write,
2484 .notify = dock_notify,
2485 .handle = &dock_handle,
2486 .type = ACPI_SYSTEM_NOTIFY,
2487 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002489 .name = "dock",
2490 .hid = IBM_PCI_HID,
2491 .notify = dock_notify,
2492 .handle = &pci_handle,
2493 .type = ACPI_SYSTEM_NOTIFY,
2494 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002495#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002496#ifdef CONFIG_ACPI_IBM_BAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002498 .name = "bay",
2499 .init = bay_init,
2500 .read = bay_read,
2501 .write = bay_write,
2502 .notify = bay_notify,
2503 .handle = &bay_handle,
2504 .type = ACPI_SYSTEM_NOTIFY,
2505 },
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002506#endif /* CONFIG_ACPI_IBM_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002508 .name = "cmos",
2509 .read = cmos_read,
2510 .write = cmos_write,
2511 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002513 .name = "led",
2514 .init = led_init,
2515 .read = led_read,
2516 .write = led_write,
2517 },
2518 {
2519 .name = "beep",
2520 .read = beep_read,
2521 .write = beep_write,
2522 },
2523 {
2524 .name = "thermal",
2525 .init = thermal_init,
2526 .read = thermal_read,
2527 },
2528 {
2529 .name = "ecdump",
2530 .read = ecdump_read,
2531 .write = ecdump_write,
2532 .experimental = 1,
2533 },
2534 {
2535 .name = "brightness",
2536 .read = brightness_read,
2537 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002538 .init = brightness_init,
2539 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002540 },
2541 {
2542 .name = "volume",
2543 .read = volume_read,
2544 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002545 },
2546 {
2547 .name = "fan",
2548 .read = fan_read,
2549 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002550 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002551 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002552 .experimental = 1,
2553 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002556/*
2557 * Module and infrastructure proble, init and exit handling
2558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002560static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 int ret;
2563 struct proc_dir_entry *entry;
2564
2565 if (ibm->experimental && !experimental)
2566 return 0;
2567
2568 if (ibm->hid) {
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002569 ret = register_ibmacpi_subdriver(ibm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (ret < 0)
2571 return ret;
2572 ibm->driver_registered = 1;
2573 }
2574
2575 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002576 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (ret != 0)
2578 return ret;
2579 ibm->init_called = 1;
2580 }
2581
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002582 if (ibm->read) {
2583 entry = create_proc_entry(ibm->name,
2584 S_IFREG | S_IRUGO | S_IWUSR,
2585 proc_dir);
2586 if (!entry) {
2587 printk(IBM_ERR "unable to create proc entry %s\n",
2588 ibm->name);
2589 return -ENODEV;
2590 }
2591 entry->owner = THIS_MODULE;
2592 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002594 if (ibm->write)
2595 entry->write_proc = &dispatch_write;
2596 ibm->proc_created = 1;
2597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 if (ibm->notify) {
2600 ret = setup_notify(ibm);
Henrique de Moraes Holschuh96e89af2007-03-15 16:15:06 -03002601 if (ret == -ENODEV) {
2602 printk(IBM_NOTICE "disabling subdriver %s\n",
2603 ibm->name);
2604 ibm_exit(ibm);
2605 return 0;
2606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (ret < 0)
2608 return ret;
2609 }
2610
2611 return 0;
2612}
2613
2614static void ibm_exit(struct ibm_struct *ibm)
2615{
2616 if (ibm->notify_installed)
2617 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2618 dispatch_notify);
2619
2620 if (ibm->proc_created)
2621 remove_proc_entry(ibm->name, proc_dir);
2622
2623 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002624 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 if (ibm->driver_registered) {
2627 acpi_bus_unregister_driver(ibm->driver);
2628 kfree(ibm->driver);
2629 }
2630}
2631
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002632/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002634static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002636static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002637{
2638 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002639 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002640
2641 /*
2642 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2643 * X32 or newer, all Z series; Some models must have an
2644 * up-to-date BIOS or they will not be detected.
2645 *
2646 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2647 */
2648 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002649 if (sscanf(dev->name,
2650 "IBM ThinkPad Embedded Controller -[%17c",
2651 ec_fw_string) == 1) {
2652 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2653 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2654 return kstrdup(ec_fw_string, GFP_KERNEL);
2655 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002656 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002657 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002660/* Module init, exit, parameters */
2661
2662static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2663{
2664 unsigned int i;
2665
2666 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2667 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2668 if (strlen(val) > sizeof(ibms[i].param) - 2)
2669 return -ENOSPC;
2670 strcpy(ibms[i].param, val);
2671 strcat(ibms[i].param, ",");
2672 return 0;
2673 }
2674
2675 return -EINVAL;
2676}
2677
2678static int experimental;
2679module_param(experimental, int, 0);
2680
2681#define IBM_PARAM(feature) \
2682 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2683
2684IBM_PARAM(hotkey);
2685IBM_PARAM(bluetooth);
2686IBM_PARAM(video);
2687IBM_PARAM(light);
2688#ifdef CONFIG_ACPI_IBM_DOCK
2689IBM_PARAM(dock);
2690#endif
2691#ifdef CONFIG_ACPI_IBM_BAY
2692IBM_PARAM(bay);
2693#endif /* CONFIG_ACPI_IBM_BAY */
2694IBM_PARAM(cmos);
2695IBM_PARAM(led);
2696IBM_PARAM(beep);
2697IBM_PARAM(ecdump);
2698IBM_PARAM(brightness);
2699IBM_PARAM(volume);
2700IBM_PARAM(fan);
2701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702static int __init acpi_ibm_init(void)
2703{
2704 int ret, i;
2705
2706 if (acpi_disabled)
2707 return -ENODEV;
2708
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002709 /* ec is required because many other handles are relative to it */
2710 IBM_HANDLE_INIT(ec);
2711 if (!ec_handle) {
2712 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002716 /* Models with newer firmware report the EC in DMI */
2717 ibm_thinkpad_ec_found = check_dmi_for_ec();
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002720 IBM_HANDLE_INIT(vid);
2721 IBM_HANDLE_INIT(vid2);
2722 IBM_HANDLE_INIT(ledb);
2723 IBM_HANDLE_INIT(led);
2724 IBM_HANDLE_INIT(hkey);
2725 IBM_HANDLE_INIT(lght);
2726 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002727#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002728 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002729#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002730 IBM_HANDLE_INIT(pci);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002731#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002732 IBM_HANDLE_INIT(bay);
2733 if (bay_handle)
2734 IBM_HANDLE_INIT(bay_ej);
2735 IBM_HANDLE_INIT(bay2);
2736 if (bay2_handle)
2737 IBM_HANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002738#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002739 IBM_HANDLE_INIT(beep);
2740 IBM_HANDLE_INIT(ecrd);
2741 IBM_HANDLE_INIT(ecwr);
2742 IBM_HANDLE_INIT(fans);
2743 IBM_HANDLE_INIT(gfan);
2744 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2747 if (!proc_dir) {
2748 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002749 acpi_ibm_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return -ENODEV;
2751 }
2752 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002753
2754 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002756 if (ret >= 0 && *ibms[i].param)
2757 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (ret < 0) {
2759 acpi_ibm_exit();
2760 return ret;
2761 }
2762 }
2763
2764 return 0;
2765}
2766
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002767static void acpi_ibm_exit(void)
2768{
2769 int i;
2770
2771 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2772 ibm_exit(&ibms[i]);
2773
2774 if (proc_dir)
2775 remove_proc_entry(IBM_DIR, acpi_root_dir);
2776
2777 if (ibm_thinkpad_ec_found)
2778 kfree(ibm_thinkpad_ec_found);
2779}
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781module_init(acpi_ibm_init);
2782module_exit(acpi_ibm_exit);