blob: 56743c5a04390a9a0edd9bdf5f806807fda6dafb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Borislav Deianov78f81cc2005-08-17 00:00:00 -040020 */
21
22#define IBM_VERSION "0.12a"
23
24/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * Changelog:
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026 *
27 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
28 * 2005-03-17 0.11 support for 600e, 770x
29 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
30 * support for 770e, G41
31 * G40 and G41 don't have a thinklight
32 * temperatures no longer experimental
33 * experimental brightness control
34 * experimental volume control
35 * experimental fan enable/disable
36 * 2005-01-16 0.10 fix module loading on R30, R31
37 * 2005-01-16 0.9 support for 570, R30, R31
38 * ultrabay support on A22p, A3x
39 * limit arg for cmos, led, beep, drop experimental status
40 * more capable led control on A21e, A22p, T20-22, X20
41 * experimental temperatures and fan speed
42 * experimental embedded controller register dump
43 * mark more functions as __init, drop incorrect __exit
44 * use MODULE_VERSION
45 * thanks to Henrik Brix Andersen <brix@gentoo.org>
46 * fix parameter passing on module loading
47 * thanks to Rusty Russell <rusty@rustcorp.com.au>
48 * thanks to Jim Radford <radford@blackbean.org>
49 * 2004-11-08 0.8 fix init error case, don't return from a macro
50 * thanks to Chris Wright <chrisw@osdl.org>
51 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
52 * fix led control on A21e
53 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
55 * proc file format changed
56 * video_switch command
57 * experimental cmos control
58 * experimental led control
59 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040060 * 2004-09-16 0.4 support for module parameters
61 * hotkey mask can be prefixed by 0x
62 * video output switching
63 * video expansion control
64 * ultrabay eject support
65 * removed lcd brightness/on/off control, didn't work
66 * 2004-08-17 0.3 support for R40
67 * lcd off, brightness control
68 * thinklight on/off
69 * 2004-08-14 0.2 support for T series, X20
70 * bluetooth enable/disable
71 * hotkey events disabled by default
72 * removed fan control, currently useless
73 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/kernel.h>
77#include <linux/module.h>
78#include <linux/init.h>
79#include <linux/types.h>
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020080#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/proc_fs.h>
Holger Macht8acb0252006-10-20 14:30:28 -070082#include <linux/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <asm/uaccess.h>
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020084#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include <acpi/acpi_drivers.h>
87#include <acpi/acnamesp.h>
88
89#define IBM_NAME "ibm"
90#define IBM_DESC "IBM ThinkPad ACPI Extras"
91#define IBM_FILE "ibm_acpi"
92#define IBM_URL "http://ibm-acpi.sf.net/"
93
Borislav Deianov78f81cc2005-08-17 00:00:00 -040094MODULE_AUTHOR("Borislav Deianov");
95MODULE_DESCRIPTION(IBM_DESC);
96MODULE_VERSION(IBM_VERSION);
97MODULE_LICENSE("GPL");
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define IBM_DIR IBM_NAME
100
101#define IBM_LOG IBM_FILE ": "
102#define IBM_ERR KERN_ERR IBM_LOG
103#define IBM_NOTICE KERN_NOTICE IBM_LOG
104#define IBM_INFO KERN_INFO IBM_LOG
105#define IBM_DEBUG KERN_DEBUG IBM_LOG
106
107#define IBM_MAX_ACPI_ARGS 3
108
109#define __unused __attribute__ ((unused))
110
111static int experimental;
112module_param(experimental, int, 0);
113
114static acpi_handle root_handle = NULL;
115
116#define IBM_HANDLE(object, parent, paths...) \
117 static acpi_handle object##_handle; \
118 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400119 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 static char *object##_paths[] = { paths }
121
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400122/*
123 * The following models are supported to various degrees:
124 *
125 * 570, 600e, 600x, 770e, 770x
126 * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
127 * G40, G41
128 * R30, R31, R32, R40, R40e, R50, R50e, R50p, R51
129 * T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p, T43
130 * X20, X21, X22, X23, X24, X30, X31, X40
131 *
132 * The following models have no supported features:
133 *
134 * 240, 240x, i1400
135 *
136 * Still missing DSDTs for the following models:
137 *
138 * A20p, A22e, A22m
139 * R52
140 * S31
141 * T43p
142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400144IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
145 "\\_SB.PCI.ISA.EC", /* 570 */
146 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
147 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
148 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
149 "\\_SB.PCI0.ICH3.EC0", /* R31 */
150 "\\_SB.PCI0.LPC.EC", /* all others */
151 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400153IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
154 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
155 "\\_SB.PCI0.VID0", /* 770e */
156 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
157 "\\_SB.PCI0.AGP.VID", /* all others */
158 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400160IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400162IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
163 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
164 "\\CMS", /* R40, R40e */
165 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800166#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400167IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
168 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
169 "\\_SB.PCI0.PCI1.DOCK", /* all others */
170 "\\_SB.PCI.ISA.SLCE", /* 570 */
171 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800172#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400173IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
174 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
175 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
176 ); /* A21e, R30, R31 */
177
178IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
179 "_EJ0", /* all others */
180 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
181
182IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
183 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
184 ); /* all others */
185
186IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
187 "_EJ0", /* 770x */
188 ); /* all others */
189
190/* don't list other alternatives as we install a notify handler on the 570 */
191IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
192
193IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
194 "^HKEY", /* R30, R31 */
195 "HKEY", /* all others */
196 ); /* 570 */
197
198IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
199IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
200
201IBM_HANDLE(led, ec, "SLED", /* 570 */
202 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
203 "LED", /* all others */
204 ); /* R30, R31 */
205
206IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
207IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
208IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200209IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400210
211IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
212 "\\FSPD", /* 600e/x, 770e, 770x */
213 ); /* all others */
214
215IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
216 "JFNS", /* 770x-JL */
217 ); /* all others */
218
219#define IBM_HKEY_HID "IBM0068"
220#define IBM_PCI_HID "PNP0A03"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200222enum thermal_access_mode {
223 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
224 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
225 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200226 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
227 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200228};
229
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200230#define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200231struct ibm_thermal_sensors_struct {
232 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
233};
234
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200235/*
236 * FAN ACCESS MODES
237 *
238 * IBMACPI_FAN_RD_ACPI_GFAN:
239 * ACPI GFAN method: returns fan level
240 *
241 * see IBMACPI_FAN_WR_ACPI_SFAN
242 * EC 0x2f not available if GFAN exists
243 *
244 * IBMACPI_FAN_WR_ACPI_SFAN:
245 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
246 *
247 * EC 0x2f might be available *for reading*, but never for writing.
248 *
249 * IBMACPI_FAN_WR_TPEC:
250 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
251 * on almost all ThinkPads
252 *
253 * Fan speed changes of any sort (including those caused by the
254 * disengaged mode) are usually done slowly by the firmware as the
255 * maximum ammount of fan duty cycle change per second seems to be
256 * limited.
257 *
258 * Reading is not available if GFAN exists.
259 * Writing is not available if SFAN exists.
260 *
261 * Bits
262 * 7 automatic mode engaged;
263 * (default operation mode of the ThinkPad)
264 * fan level is ignored in this mode.
265 * 6 disengage mode (takes precedence over bit 7);
266 * not available on all thinkpads. May disable
267 * the tachometer, and speeds up fan to 100% duty-cycle,
268 * which speeds it up far above the standard RPM
269 * levels. It is not impossible that it could cause
270 * hardware damage.
271 * 5-3 unused in some models. Extra bits for fan level
272 * in others, but still useless as all values above
273 * 7 map to the same speed as level 7 in these models.
274 * 2-0 fan level (0..7 usually)
275 * 0x00 = stop
276 * 0x07 = max (set when temperatures critical)
277 * Some ThinkPads may have other levels, see
278 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
279 *
280 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
281 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
282 * does so, its initial value is meaningless (0x07).
283 *
284 * For firmware bugs, refer to:
285 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
286 *
287 * ----
288 *
289 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
290 * Main fan tachometer reading (in RPM)
291 *
292 * This register is present on all ThinkPads with a new-style EC, and
293 * it is known not to be present on the A21m/e, and T22, as there is
294 * something else in offset 0x84 according to the ACPI DSDT. Other
295 * ThinkPads from this same time period (and earlier) probably lack the
296 * tachometer as well.
297 *
298 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
299 * was never fixed by IBM to report the EC firmware version string
300 * probably support the tachometer (like the early X models), so
301 * detecting it is quite hard. We need more data to know for sure.
302 *
303 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
304 * might result.
305 *
306 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
307 * register is not invalidated in ThinkPads that disable tachometer
308 * readings. Thus, the tachometer readings go stale.
309 *
310 * For firmware bugs, refer to:
311 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
312 *
313 * IBMACPI_FAN_WR_ACPI_FANS:
314 * ThinkPad X31, X40, X41. Not available in the X60.
315 *
316 * FANS ACPI handle: takes three arguments: low speed, medium speed,
317 * high speed. ACPI DSDT seems to map these three speeds to levels
318 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
319 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
320 *
321 * The speeds are stored on handles
322 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
323 *
324 * There are three default speed sets, acessible as handles:
325 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
326 *
327 * ACPI DSDT switches which set is in use depending on various
328 * factors.
329 *
330 * IBMACPI_FAN_WR_TPEC is also available and should be used to
331 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
332 * but the ACPI tables just mention level 7.
333 */
334
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200335enum fan_status_access_mode {
336 IBMACPI_FAN_NONE = 0, /* No fan status or control */
337 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
338 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
339};
340
341enum fan_control_access_mode {
342 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
343 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
344 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
345 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
346};
347
348enum fan_control_commands {
349 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
350 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
351 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd */
352};
353
354enum { /* Fan control constants */
355 fan_status_offset = 0x2f, /* EC register 0x2f */
356 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
357 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -0200358
359 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
360 * disengaged */
361 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
362 * control */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200363};
364
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -0200365static char *ibm_thinkpad_ec_found = NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367struct ibm_struct {
368 char *name;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400369 char param[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 char *hid;
372 struct acpi_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400374 int (*init) (void);
375 int (*read) (char *);
376 int (*write) (char *);
377 void (*exit) (void);
378
379 void (*notify) (struct ibm_struct *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 acpi_handle *handle;
381 int type;
382 struct acpi_device *device;
383
384 int driver_registered;
385 int proc_created;
386 int init_called;
387 int notify_installed;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 int experimental;
390};
391
392static struct proc_dir_entry *proc_dir = NULL;
393
Holger Macht8acb0252006-10-20 14:30:28 -0700394static struct backlight_device *ibm_backlight_device;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
397#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
398#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
399
400static int acpi_evalf(acpi_handle handle,
401 void *res, char *method, char *fmt, ...)
402{
403 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400404 struct acpi_object_list params;
405 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
406 struct acpi_buffer result, *resultp;
407 union acpi_object out_obj;
408 acpi_status status;
409 va_list ap;
410 char res_type;
411 int success;
412 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (!*fmt) {
415 printk(IBM_ERR "acpi_evalf() called with empty format\n");
416 return 0;
417 }
418
419 if (*fmt == 'q') {
420 quiet = 1;
421 fmt++;
422 } else
423 quiet = 0;
424
425 res_type = *(fmt++);
426
427 params.count = 0;
428 params.pointer = &in_objs[0];
429
430 va_start(ap, fmt);
431 while (*fmt) {
432 char c = *(fmt++);
433 switch (c) {
434 case 'd': /* int */
435 in_objs[params.count].integer.value = va_arg(ap, int);
436 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
437 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400438 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 default:
440 printk(IBM_ERR "acpi_evalf() called "
441 "with invalid format character '%c'\n", c);
442 return 0;
443 }
444 }
445 va_end(ap);
446
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400447 if (res_type != 'v') {
448 result.length = sizeof(out_obj);
449 result.pointer = &out_obj;
450 resultp = &result;
451 } else
452 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400454 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400457 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (res)
459 *(int *)res = out_obj.integer.value;
460 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
461 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400462 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 success = status == AE_OK;
464 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400465 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 default:
467 printk(IBM_ERR "acpi_evalf() called "
468 "with invalid format character '%c'\n", res_type);
469 return 0;
470 }
471
472 if (!success && !quiet)
473 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
474 method, fmt0, status);
475
476 return success;
477}
478
479static void __unused acpi_print_int(acpi_handle handle, char *method)
480{
481 int i;
482
483 if (acpi_evalf(handle, &i, method, "d"))
484 printk(IBM_INFO "%s = 0x%x\n", method, i);
485 else
486 printk(IBM_ERR "error calling %s\n", method);
487}
488
489static char *next_cmd(char **cmds)
490{
491 char *start = *cmds;
492 char *end;
493
494 while ((end = strchr(start, ',')) && end == start)
495 start = end + 1;
496
497 if (!end)
498 return NULL;
499
500 *end = 0;
501 *cmds = end + 1;
502 return start;
503}
504
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400505static int driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
508 printk(IBM_INFO "%s\n", IBM_URL);
509
510 return 0;
511}
512
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400513static int driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 int len = 0;
516
517 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
518 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
519
520 return len;
521}
522
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400523static int hotkey_supported;
524static int hotkey_mask_supported;
525static int hotkey_orig_status;
526static int hotkey_orig_mask;
527
528static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400531 return 0;
532
533 if (hotkey_mask_supported)
534 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
535 return 0;
536
537 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 int i;
543
544 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
546
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400547 if (hotkey_mask_supported)
548 for (i = 0; i < 32; i++) {
549 int bit = ((1 << i) & mask) != 0;
550 if (!acpi_evalf(hkey_handle,
551 NULL, "MHKM", "vdd", i + 1, bit))
552 return 0;
553 }
554
555 return 1;
556}
557
558static int hotkey_init(void)
559{
560 /* hotkey not supported on 570 */
561 hotkey_supported = hkey_handle != NULL;
562
563 if (hotkey_supported) {
564 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
565 A30, R30, R31, T20-22, X20-21, X22-24 */
566 hotkey_mask_supported =
567 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
568
569 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
570 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
573 return 0;
574}
575
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400576static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 int status, mask;
579 int len = 0;
580
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400581 if (!hotkey_supported) {
582 len += sprintf(p + len, "status:\t\tnot supported\n");
583 return len;
584 }
585
586 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return -EIO;
588
589 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400590 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
592 len += sprintf(p + len,
593 "commands:\tenable, disable, reset, <mask>\n");
594 } else {
595 len += sprintf(p + len, "mask:\t\tnot supported\n");
596 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
597 }
598
599 return len;
600}
601
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400602static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 int status, mask;
605 char *cmd;
606 int do_cmd = 0;
607
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400608 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -ENODEV;
610
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400611 if (!hotkey_get(&status, &mask))
612 return -EIO;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 while ((cmd = next_cmd(&buf))) {
615 if (strlencmp(cmd, "enable") == 0) {
616 status = 1;
617 } else if (strlencmp(cmd, "disable") == 0) {
618 status = 0;
619 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400620 status = hotkey_orig_status;
621 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
623 /* mask set */
624 } else if (sscanf(cmd, "%x", &mask) == 1) {
625 /* mask set */
626 } else
627 return -EINVAL;
628 do_cmd = 1;
629 }
630
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400631 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -EIO;
633
634 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400637static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400639 if (hotkey_supported)
640 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static void hotkey_notify(struct ibm_struct *ibm, u32 event)
644{
645 int hkey;
646
647 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
648 acpi_bus_generate_event(ibm->device, event, hkey);
649 else {
650 printk(IBM_ERR "unknown hotkey event %d\n", event);
651 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400655static int bluetooth_supported;
656
657static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400659 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
660 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
661 bluetooth_supported = hkey_handle &&
662 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 return 0;
665}
666
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400667static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 int status;
670
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400671 if (!bluetooth_supported ||
672 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 status = 0;
674
675 return status;
676}
677
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400678static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400681 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400683 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 len += sprintf(p + len, "status:\t\tnot supported\n");
685 else if (!(status & 1))
686 len += sprintf(p + len, "status:\t\tnot installed\n");
687 else {
688 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
689 len += sprintf(p + len, "commands:\tenable, disable\n");
690 }
691
692 return len;
693}
694
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400695static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400697 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 char *cmd;
699 int do_cmd = 0;
700
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400701 if (!bluetooth_supported)
702 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 while ((cmd = next_cmd(&buf))) {
705 if (strlencmp(cmd, "enable") == 0) {
706 status |= 2;
707 } else if (strlencmp(cmd, "disable") == 0) {
708 status &= ~2;
709 } else
710 return -EINVAL;
711 do_cmd = 1;
712 }
713
714 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400715 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 return 0;
718}
719
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400720static int wan_supported;
721
722static int wan_init(void)
723{
724 wan_supported = hkey_handle &&
725 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
726
727 return 0;
728}
729
730static int wan_status(void)
731{
732 int status;
733
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200734 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400735 status = 0;
736
737 return status;
738}
739
740static int wan_read(char *p)
741{
742 int len = 0;
743 int status = wan_status();
744
745 if (!wan_supported)
746 len += sprintf(p + len, "status:\t\tnot supported\n");
747 else if (!(status & 1))
748 len += sprintf(p + len, "status:\t\tnot installed\n");
749 else {
750 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
751 len += sprintf(p + len, "commands:\tenable, disable\n");
752 }
753
754 return len;
755}
756
757static int wan_write(char *buf)
758{
759 int status = wan_status();
760 char *cmd;
761 int do_cmd = 0;
762
763 if (!wan_supported)
764 return -ENODEV;
765
766 while ((cmd = next_cmd(&buf))) {
767 if (strlencmp(cmd, "enable") == 0) {
768 status |= 2;
769 } else if (strlencmp(cmd, "disable") == 0) {
770 status &= ~2;
771 } else
772 return -EINVAL;
773 do_cmd = 1;
774 }
775
776 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
777 return -EIO;
778
779 return 0;
780}
781
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400782static int video_supported;
783static int video_orig_autosw;
784
785#define VIDEO_570 1
786#define VIDEO_770 2
787#define VIDEO_NEW 3
788
789static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400791 int ivga;
792
793 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
794 /* G41, assume IVGA doesn't change */
795 vid_handle = vid2_handle;
796
797 if (!vid_handle)
798 /* video switching not supported on R30, R31 */
799 video_supported = 0;
800 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
801 /* 570 */
802 video_supported = VIDEO_570;
803 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
804 /* 600e/x, 770e, 770x */
805 video_supported = VIDEO_770;
806 else
807 /* all others */
808 video_supported = VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 return 0;
811}
812
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400813static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 int status = 0;
816 int i;
817
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400818 if (video_supported == VIDEO_570) {
819 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
820 status = i & 3;
821 } else if (video_supported == VIDEO_770) {
822 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
823 status |= 0x01 * i;
824 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
825 status |= 0x02 * i;
826 } else if (video_supported == VIDEO_NEW) {
827 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
828 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
829 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400831 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
832 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
833 status |= 0x01 * i;
834 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
835 status |= 0x08 * i;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 return status;
839}
840
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400841static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400843 int autosw = 0;
844
845 if (video_supported == VIDEO_570)
846 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
847 else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
848 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
849
850 return autosw & 1;
851}
852
853static int video_read(char *p)
854{
855 int status = video_status();
856 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int len = 0;
858
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400859 if (!video_supported) {
860 len += sprintf(p + len, "status:\t\tnot supported\n");
861 return len;
862 }
863
864 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
866 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400867 if (video_supported == VIDEO_NEW)
868 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
869 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
870 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
871 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
872 if (video_supported == VIDEO_NEW)
873 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
874 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
876
877 return len;
878}
879
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400880static int video_switch(void)
881{
882 int autosw = video_autosw();
883 int ret;
884
885 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
886 return -EIO;
887 ret = video_supported == VIDEO_570 ?
888 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
889 acpi_evalf(vid_handle, NULL, "VSWT", "v");
890 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
891
892 return ret;
893}
894
895static int video_expand(void)
896{
897 if (video_supported == VIDEO_570)
898 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
899 else if (video_supported == VIDEO_770)
900 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
901 else
902 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
903}
904
905static int video_switch2(int status)
906{
907 int ret;
908
909 if (video_supported == VIDEO_570) {
910 ret = acpi_evalf(NULL, NULL,
911 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
912 } else if (video_supported == VIDEO_770) {
913 int autosw = video_autosw();
914 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
915 return -EIO;
916
917 ret = acpi_evalf(vid_handle, NULL,
918 "ASWT", "vdd", status * 0x100, 0);
919
920 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
921 } else {
922 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
923 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
924 }
925
926 return ret;
927}
928
929static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 char *cmd;
932 int enable, disable, status;
933
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400934 if (!video_supported)
935 return -ENODEV;
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 enable = disable = 0;
938
939 while ((cmd = next_cmd(&buf))) {
940 if (strlencmp(cmd, "lcd_enable") == 0) {
941 enable |= 0x01;
942 } else if (strlencmp(cmd, "lcd_disable") == 0) {
943 disable |= 0x01;
944 } else if (strlencmp(cmd, "crt_enable") == 0) {
945 enable |= 0x02;
946 } else if (strlencmp(cmd, "crt_disable") == 0) {
947 disable |= 0x02;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400948 } else if (video_supported == VIDEO_NEW &&
949 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 enable |= 0x08;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400951 } else if (video_supported == VIDEO_NEW &&
952 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 disable |= 0x08;
954 } else if (strlencmp(cmd, "auto_enable") == 0) {
955 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
956 return -EIO;
957 } else if (strlencmp(cmd, "auto_disable") == 0) {
958 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
959 return -EIO;
960 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400961 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -EIO;
963 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400964 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -EIO;
966 } else
967 return -EINVAL;
968 }
969
970 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400971 status = (video_status() & 0x0f & ~disable) | enable;
972 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return -EIO;
974 }
975
976 return 0;
977}
978
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400979static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400981 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400984static int light_supported;
985static int light_status_supported;
986
987static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400989 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
990 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
991
992 if (light_supported)
993 /* light status not supported on
994 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
995 light_status_supported = acpi_evalf(ec_handle, NULL,
996 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 return 0;
999}
1000
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001001static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 int len = 0;
1004 int status = 0;
1005
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001006 if (!light_supported) {
1007 len += sprintf(p + len, "status:\t\tnot supported\n");
1008 } else if (!light_status_supported) {
1009 len += sprintf(p + len, "status:\t\tunknown\n");
1010 len += sprintf(p + len, "commands:\ton, off\n");
1011 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1013 return -EIO;
1014 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001015 len += sprintf(p + len, "commands:\ton, off\n");
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 return len;
1019}
1020
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001021static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 int cmos_cmd, lght_cmd;
1024 char *cmd;
1025 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001026
1027 if (!light_supported)
1028 return -ENODEV;
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 while ((cmd = next_cmd(&buf))) {
1031 if (strlencmp(cmd, "on") == 0) {
1032 cmos_cmd = 0x0c;
1033 lght_cmd = 1;
1034 } else if (strlencmp(cmd, "off") == 0) {
1035 cmos_cmd = 0x0d;
1036 lght_cmd = 0;
1037 } else
1038 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001041 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1042 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!success)
1044 return -EIO;
1045 }
1046
1047 return 0;
1048}
1049
1050static int _sta(acpi_handle handle)
1051{
1052 int status;
1053
1054 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1055 status = 0;
1056
1057 return status;
1058}
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001059
Kristen Accardi63e5f242006-02-23 17:56:06 -08001060#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061#define dock_docked() (_sta(dock_handle) & 1)
1062
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001063static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 int len = 0;
1066 int docked = dock_docked();
1067
1068 if (!dock_handle)
1069 len += sprintf(p + len, "status:\t\tnot supported\n");
1070 else if (!docked)
1071 len += sprintf(p + len, "status:\t\tundocked\n");
1072 else {
1073 len += sprintf(p + len, "status:\t\tdocked\n");
1074 len += sprintf(p + len, "commands:\tdock, undock\n");
1075 }
1076
1077 return len;
1078}
1079
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001080static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 char *cmd;
1083
1084 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001085 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 while ((cmd = next_cmd(&buf))) {
1088 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001089 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1090 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return -EIO;
1092 } else if (strlencmp(cmd, "dock") == 0) {
1093 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1094 return -EIO;
1095 } else
1096 return -EINVAL;
1097 }
1098
1099 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102static void dock_notify(struct ibm_struct *ibm, u32 event)
1103{
1104 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001105 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001107 if (event == 1 && !pci) /* 570 */
1108 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1109 else if (event == 1 && pci) /* 570 */
1110 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1111 else if (event == 3 && docked)
1112 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001114 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001116 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 else {
1118 printk(IBM_ERR "unknown dock event %d, status %d\n",
1119 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001120 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001123#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001125static int bay_status_supported;
1126static int bay_status2_supported;
1127static int bay_eject_supported;
1128static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001130static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001132 bay_status_supported = bay_handle &&
1133 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1134 bay_status2_supported = bay2_handle &&
1135 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1136
1137 bay_eject_supported = bay_handle && bay_ej_handle &&
1138 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1139 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1140 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 return 0;
1143}
1144
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001145#define bay_occupied(b) (_sta(b##_handle) & 1)
1146
1147static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001150 int occupied = bay_occupied(bay);
1151 int occupied2 = bay_occupied(bay2);
1152 int eject, eject2;
1153
1154 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1155 (occupied ? "occupied" : "unoccupied") :
1156 "not supported");
1157 if (bay_status2_supported)
1158 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1159 "occupied" : "unoccupied");
1160
1161 eject = bay_eject_supported && occupied;
1162 eject2 = bay_eject2_supported && occupied2;
1163
1164 if (eject && eject2)
1165 len += sprintf(p + len, "commands:\teject, eject2\n");
1166 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001168 else if (eject2)
1169 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 return len;
1172}
1173
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001174static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 char *cmd;
1177
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001178 if (!bay_eject_supported && !bay_eject2_supported)
1179 return -ENODEV;
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001182 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1183 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1184 return -EIO;
1185 } else if (bay_eject2_supported &&
1186 strlencmp(cmd, "eject2") == 0) {
1187 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return -EIO;
1189 } else
1190 return -EINVAL;
1191 }
1192
1193 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196static void bay_notify(struct ibm_struct *ibm, u32 event)
1197{
1198 acpi_bus_generate_event(ibm->device, event, 0);
1199}
1200
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001201static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 int len = 0;
1204
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001205 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1206 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (!cmos_handle)
1208 len += sprintf(p + len, "status:\t\tnot supported\n");
1209 else {
1210 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001211 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
1214 return len;
1215}
1216
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001217static int cmos_eval(int cmos_cmd)
1218{
1219 if (cmos_handle)
1220 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1221 else
1222 return 1;
1223}
1224
1225static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 char *cmd;
1228 int cmos_cmd;
1229
1230 if (!cmos_handle)
1231 return -EINVAL;
1232
1233 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001234 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1235 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* cmos_cmd set */
1237 } else
1238 return -EINVAL;
1239
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001240 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -EIO;
1242 }
1243
1244 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001245}
1246
1247static int led_supported;
1248
1249#define LED_570 1
1250#define LED_OLD 2
1251#define LED_NEW 3
1252
1253static int led_init(void)
1254{
1255 if (!led_handle)
1256 /* led not supported on R30, R31 */
1257 led_supported = 0;
1258 else if (strlencmp(led_path, "SLED") == 0)
1259 /* 570 */
1260 led_supported = LED_570;
1261 else if (strlencmp(led_path, "SYSL") == 0)
1262 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1263 led_supported = LED_OLD;
1264 else
1265 /* all others */
1266 led_supported = LED_NEW;
1267
1268 return 0;
1269}
1270
1271#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1272
1273static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 int len = 0;
1276
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001277 if (!led_supported) {
1278 len += sprintf(p + len, "status:\t\tnot supported\n");
1279 return len;
1280 }
1281 len += sprintf(p + len, "status:\t\tsupported\n");
1282
1283 if (led_supported == LED_570) {
1284 /* 570 */
1285 int i, status;
1286 for (i = 0; i < 8; i++) {
1287 if (!acpi_evalf(ec_handle,
1288 &status, "GLED", "dd", 1 << i))
1289 return -EIO;
1290 len += sprintf(p + len, "%d:\t\t%s\n",
1291 i, led_status(status));
1292 }
1293 }
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001296 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 return len;
1299}
1300
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001301/* off, on, blink */
1302static const int led_sled_arg1[] = { 0, 1, 3 };
1303static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1304static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1305static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1306
1307#define EC_HLCL 0x0c
1308#define EC_HLBL 0x0d
1309#define EC_HLMS 0x0e
1310
1311static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001314 int led, ind, ret;
1315
1316 if (!led_supported)
1317 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001320 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return -EINVAL;
1322
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001323 if (strstr(cmd, "off")) {
1324 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001326 ind = 1;
1327 } else if (strstr(cmd, "blink")) {
1328 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 } else
1330 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001331
1332 if (led_supported == LED_570) {
1333 /* 570 */
1334 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001336 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001338 } else if (led_supported == LED_OLD) {
1339 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1340 led = 1 << led;
1341 ret = ec_write(EC_HLMS, led);
1342 if (ret >= 0)
1343 ret =
1344 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1345 if (ret >= 0)
1346 ret =
1347 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1348 if (ret < 0)
1349 return ret;
1350 } else {
1351 /* all others */
1352 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1353 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
1358 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001359}
1360
1361static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 int len = 0;
1364
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001365 if (!beep_handle)
1366 len += sprintf(p + len, "status:\t\tnot supported\n");
1367 else {
1368 len += sprintf(p + len, "status:\t\tsupported\n");
1369 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 return len;
1373}
1374
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001375static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 char *cmd;
1378 int beep_cmd;
1379
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001380 if (!beep_handle)
1381 return -ENODEV;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001384 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1385 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* beep_cmd set */
1387 } else
1388 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001389 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EIO;
1391 }
1392
1393 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001394}
1395
1396static int acpi_ec_read(int i, u8 * p)
1397{
1398 int v;
1399
1400 if (ecrd_handle) {
1401 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1402 return 0;
1403 *p = v;
1404 } else {
1405 if (ec_read(i, p) < 0)
1406 return 0;
1407 }
1408
1409 return 1;
1410}
1411
1412static int acpi_ec_write(int i, u8 v)
1413{
1414 if (ecwr_handle) {
1415 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1416 return 0;
1417 } else {
1418 if (ec_write(i, v) < 0)
1419 return 0;
1420 }
1421
1422 return 1;
1423}
1424
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");
1432
1433 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
1565static u8 ecdump_regs[256];
1566
1567static int ecdump_read(char *p)
1568{
1569 int len = 0;
1570 int i, j;
1571 u8 v;
1572
1573 len += sprintf(p + len, "EC "
1574 " +00 +01 +02 +03 +04 +05 +06 +07"
1575 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1576 for (i = 0; i < 256; i += 16) {
1577 len += sprintf(p + len, "EC 0x%02x:", i);
1578 for (j = 0; j < 16; j++) {
1579 if (!acpi_ec_read(i + j, &v))
1580 break;
1581 if (v != ecdump_regs[i + j])
1582 len += sprintf(p + len, " *%02x", v);
1583 else
1584 len += sprintf(p + len, " %02x", v);
1585 ecdump_regs[i + j] = v;
1586 }
1587 len += sprintf(p + len, "\n");
1588 if (j != 16)
1589 break;
1590 }
1591
1592 /* These are way too dangerous to advertise openly... */
1593#if 0
1594 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1595 " (<offset> is 00-ff, <value> is 00-ff)\n");
1596 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1597 " (<offset> is 00-ff, <value> is 0-255)\n");
1598#endif
1599 return len;
1600}
1601
1602static int ecdump_write(char *buf)
1603{
1604 char *cmd;
1605 int i, v;
1606
1607 while ((cmd = next_cmd(&buf))) {
1608 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1609 /* i and v set */
1610 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1611 /* i and v set */
1612 } else
1613 return -EINVAL;
1614 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1615 if (!acpi_ec_write(i, v))
1616 return -EIO;
1617 } else
1618 return -EINVAL;
1619 }
1620
1621 return 0;
1622}
1623
1624static int brightness_offset = 0x31;
1625
Holger Macht8acb0252006-10-20 14:30:28 -07001626static int brightness_get(struct backlight_device *bd)
1627{
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001628 u8 level;
1629 if (!acpi_ec_read(brightness_offset, &level))
1630 return -EIO;
Holger Macht8acb0252006-10-20 14:30:28 -07001631
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001632 level &= 0x7;
1633 return level;
Holger Macht8acb0252006-10-20 14:30:28 -07001634}
1635
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001636static int brightness_read(char *p)
1637{
1638 int len = 0;
Holger Macht8acb0252006-10-20 14:30:28 -07001639 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001640
Holger Macht8acb0252006-10-20 14:30:28 -07001641 if ((level = brightness_get(NULL)) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001642 len += sprintf(p + len, "level:\t\tunreadable\n");
1643 } else {
1644 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1645 len += sprintf(p + len, "commands:\tup, down\n");
1646 len += sprintf(p + len, "commands:\tlevel <level>"
1647 " (<level> is 0-7)\n");
1648 }
1649
1650 return len;
1651}
1652
1653#define BRIGHTNESS_UP 4
1654#define BRIGHTNESS_DOWN 5
1655
Holger Macht8acb0252006-10-20 14:30:28 -07001656static int brightness_set(int value)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001657{
1658 int cmos_cmd, inc, i;
Holger Macht8acb0252006-10-20 14:30:28 -07001659 int current_value = brightness_get(NULL);
1660
1661 value &= 7;
1662
1663 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1664 inc = value > current_value ? 1 : -1;
1665 for (i = current_value; i != value; i += inc) {
1666 if (!cmos_eval(cmos_cmd))
1667 return -EIO;
1668 if (!acpi_ec_write(brightness_offset, i + inc))
1669 return -EIO;
1670 }
1671
1672 return 0;
1673}
1674
1675static int brightness_write(char *buf)
1676{
1677 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001678 int new_level;
1679 char *cmd;
1680
1681 while ((cmd = next_cmd(&buf))) {
Holger Macht8acb0252006-10-20 14:30:28 -07001682 if ((level = brightness_get(NULL)) < 0)
1683 return level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001684 level &= 7;
1685
1686 if (strlencmp(cmd, "up") == 0) {
1687 new_level = level == 7 ? 7 : level + 1;
1688 } else if (strlencmp(cmd, "down") == 0) {
1689 new_level = level == 0 ? 0 : level - 1;
1690 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1691 new_level >= 0 && new_level <= 7) {
1692 /* new_level set */
1693 } else
1694 return -EINVAL;
1695
Holger Macht8acb0252006-10-20 14:30:28 -07001696 brightness_set(new_level);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001697 }
1698
1699 return 0;
1700}
1701
Holger Macht8acb0252006-10-20 14:30:28 -07001702static int brightness_update_status(struct backlight_device *bd)
1703{
1704 return brightness_set(bd->props->brightness);
1705}
1706
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001707static int volume_offset = 0x30;
1708
1709static int volume_read(char *p)
1710{
1711 int len = 0;
1712 u8 level;
1713
1714 if (!acpi_ec_read(volume_offset, &level)) {
1715 len += sprintf(p + len, "level:\t\tunreadable\n");
1716 } else {
1717 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1718 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1719 len += sprintf(p + len, "commands:\tup, down, mute\n");
1720 len += sprintf(p + len, "commands:\tlevel <level>"
1721 " (<level> is 0-15)\n");
1722 }
1723
1724 return len;
1725}
1726
1727#define VOLUME_DOWN 0
1728#define VOLUME_UP 1
1729#define VOLUME_MUTE 2
1730
1731static int volume_write(char *buf)
1732{
1733 int cmos_cmd, inc, i;
1734 u8 level, mute;
1735 int new_level, new_mute;
1736 char *cmd;
1737
1738 while ((cmd = next_cmd(&buf))) {
1739 if (!acpi_ec_read(volume_offset, &level))
1740 return -EIO;
1741 new_mute = mute = level & 0x40;
1742 new_level = level = level & 0xf;
1743
1744 if (strlencmp(cmd, "up") == 0) {
1745 if (mute)
1746 new_mute = 0;
1747 else
1748 new_level = level == 15 ? 15 : level + 1;
1749 } else if (strlencmp(cmd, "down") == 0) {
1750 if (mute)
1751 new_mute = 0;
1752 else
1753 new_level = level == 0 ? 0 : level - 1;
1754 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1755 new_level >= 0 && new_level <= 15) {
1756 /* new_level set */
1757 } else if (strlencmp(cmd, "mute") == 0) {
1758 new_mute = 0x40;
1759 } else
1760 return -EINVAL;
1761
1762 if (new_level != level) { /* mute doesn't change */
1763 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1764 inc = new_level > level ? 1 : -1;
1765
1766 if (mute && (!cmos_eval(cmos_cmd) ||
1767 !acpi_ec_write(volume_offset, level)))
1768 return -EIO;
1769
1770 for (i = level; i != new_level; i += inc)
1771 if (!cmos_eval(cmos_cmd) ||
1772 !acpi_ec_write(volume_offset, i + inc))
1773 return -EIO;
1774
1775 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1776 !acpi_ec_write(volume_offset,
1777 new_level + mute)))
1778 return -EIO;
1779 }
1780
1781 if (new_mute != mute) { /* level doesn't change */
1782 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1783
1784 if (!cmos_eval(cmos_cmd) ||
1785 !acpi_ec_write(volume_offset, level + new_mute))
1786 return -EIO;
1787 }
1788 }
1789
1790 return 0;
1791}
1792
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001793static enum fan_status_access_mode fan_status_access_mode;
1794static enum fan_control_access_mode fan_control_access_mode;
1795static enum fan_control_commands fan_control_commands;
1796
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001797static int fan_control_status_known;
1798static u8 fan_control_initial_status;
1799
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001800static int fan_init(void)
1801{
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001802 fan_status_access_mode = IBMACPI_FAN_NONE;
1803 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1804 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001805 fan_control_status_known = 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001806
1807 if (gfan_handle) {
1808 /* 570, 600e/x, 770e, 770x */
1809 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1810 } else {
1811 /* all other ThinkPads: note that even old-style
1812 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001813 if (likely(acpi_ec_read(fan_status_offset,
1814 &fan_control_initial_status))) {
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001815 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001816
1817 /* In some ThinkPads, neither the EC nor the ACPI
1818 * DSDT initialize the fan status, and it ends up
1819 * being set to 0x07 when it *could* be either
1820 * 0x07 or 0x80.
1821 *
1822 * Enable for TP-1Y (T43), TP-78 (R51e),
1823 * TP-76 (R52), TP-70 (T43, R52), which are known
1824 * to be buggy. */
1825 if (fan_control_initial_status == 0x07 &&
1826 ibm_thinkpad_ec_found &&
1827 ((ibm_thinkpad_ec_found[0] == '1' &&
1828 ibm_thinkpad_ec_found[1] == 'Y') ||
1829 (ibm_thinkpad_ec_found[0] == '7' &&
1830 (ibm_thinkpad_ec_found[1] == '6' ||
1831 ibm_thinkpad_ec_found[1] == '8' ||
1832 ibm_thinkpad_ec_found[1] == '0'))
1833 )) {
1834 printk(IBM_NOTICE
1835 "fan_init: initial fan status is "
1836 "unknown, assuming it is in auto "
1837 "mode\n");
1838 fan_control_status_known = 0;
1839 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001840 } else {
1841 printk(IBM_ERR
1842 "ThinkPad ACPI EC access misbehaving, "
1843 "fan status and control unavailable\n");
1844 return 0;
1845 }
1846 }
1847
1848 if (sfan_handle) {
1849 /* 570, 770x-JL */
1850 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02001851 fan_control_commands |=
1852 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001853 } else {
1854 if (!gfan_handle) {
1855 /* gfan without sfan means no fan control */
1856 /* all other models implement TP EC 0x2f control */
1857
1858 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001859 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001860 fan_control_access_mode =
1861 IBMACPI_FAN_WR_ACPI_FANS;
1862 fan_control_commands |=
1863 IBMACPI_FAN_CMD_SPEED |
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001864 IBMACPI_FAN_CMD_LEVEL |
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001865 IBMACPI_FAN_CMD_ENABLE;
1866 } else {
1867 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001868 fan_control_commands |=
1869 IBMACPI_FAN_CMD_LEVEL |
1870 IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001871 }
1872 }
1873 }
1874
1875 return 0;
1876}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001877
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001878static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001879{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001880 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001881
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001882 /* TODO:
1883 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1884
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001885 switch (fan_status_access_mode) {
1886 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001887 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001888
1889 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001890 return -EIO;
1891
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001892 if (likely(status))
1893 *status = s & 0x07;
1894
1895 break;
1896
1897 case IBMACPI_FAN_RD_TPEC:
1898 /* all except 570, 600e/x, 770e, 770x */
1899 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1900 return -EIO;
1901
1902 if (likely(status))
1903 *status = s;
1904
1905 break;
1906
1907 default:
1908 return -ENXIO;
1909 }
1910
1911 return 0;
1912}
1913
1914static int fan_get_speed(unsigned int *speed)
1915{
1916 u8 hi, lo;
1917
1918 switch (fan_status_access_mode) {
1919 case IBMACPI_FAN_RD_TPEC:
1920 /* all except 570, 600e/x, 770e, 770x */
1921 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1922 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1923 return -EIO;
1924
1925 if (likely(speed))
1926 *speed = (hi << 8) | lo;
1927
1928 break;
1929
1930 default:
1931 return -ENXIO;
1932 }
1933
1934 return 0;
1935}
1936
1937static int fan_read(char *p)
1938{
1939 int len = 0;
1940 int rc;
1941 u8 status;
1942 unsigned int speed = 0;
1943
1944 switch (fan_status_access_mode) {
1945 case IBMACPI_FAN_RD_ACPI_GFAN:
1946 /* 570, 600e/x, 770e, 770x */
1947 if ((rc = fan_get_status(&status)) < 0)
1948 return rc;
1949
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001950 len += sprintf(p + len, "status:\t\t%s\n"
1951 "level:\t\t%d\n",
1952 (status != 0) ? "enabled" : "disabled", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001953 break;
1954
1955 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001956 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001957 if ((rc = fan_get_status(&status)) < 0)
1958 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001959
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001960 if (unlikely(!fan_control_status_known)) {
1961 if (status != fan_control_initial_status)
1962 fan_control_status_known = 1;
1963 else
1964 /* Return most likely status. In fact, it
1965 * might be the only possible status */
1966 status = IBMACPI_FAN_EC_AUTO;
1967 }
1968
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001969 len += sprintf(p + len, "status:\t\t%s\n",
1970 (status != 0) ? "enabled" : "disabled");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001971
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001972 /* No ThinkPad boots on disengaged mode, we can safely
1973 * assume the tachometer is online if fan control status
1974 * was unknown */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001975 if ((rc = fan_get_speed(&speed)) < 0)
1976 return rc;
1977
1978 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001979
1980 if (status & IBMACPI_FAN_EC_DISENGAGED)
1981 /* Disengaged mode takes precedence */
1982 len += sprintf(p + len, "level:\t\tdisengaged\n");
1983 else if (status & IBMACPI_FAN_EC_AUTO)
1984 len += sprintf(p + len, "level:\t\tauto\n");
1985 else
1986 len += sprintf(p + len, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001987 break;
1988
1989 case IBMACPI_FAN_NONE:
1990 default:
1991 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001992 }
1993
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001994 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
1995 len += sprintf(p + len, "commands:\tlevel <level>");
1996
1997 switch (fan_control_access_mode) {
1998 case IBMACPI_FAN_WR_ACPI_SFAN:
1999 len += sprintf(p + len, " (<level> is 0-7)\n");
2000 break;
2001
2002 default:
2003 len += sprintf(p + len, " (<level> is 0-7, "
2004 "auto, disengaged)\n");
2005 break;
2006 }
2007 }
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002008
2009 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002010 len += sprintf(p + len, "commands:\tenable, disable\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002011
2012 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002013 len += sprintf(p + len, "commands:\tspeed <speed>"
2014 " (<speed> is 0-65535)\n");
2015
2016 return len;
2017}
2018
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002019static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002020{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002021 switch (fan_control_access_mode) {
2022 case IBMACPI_FAN_WR_ACPI_SFAN:
2023 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002024 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2025 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002026 } else
2027 return -EINVAL;
2028 break;
2029
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002030 case IBMACPI_FAN_WR_ACPI_FANS:
2031 case IBMACPI_FAN_WR_TPEC:
2032 if ((level != IBMACPI_FAN_EC_AUTO) &&
2033 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2034 ((level < 0) || (level > 7)))
2035 return -EINVAL;
2036
2037 if (!acpi_ec_write(fan_status_offset, level))
2038 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002039 else
2040 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002041 break;
2042
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002043 default:
2044 return -ENXIO;
2045 }
2046 return 0;
2047}
2048
2049static int fan_set_enable(void)
2050{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002051 u8 s;
2052 int rc;
2053
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002054 switch (fan_control_access_mode) {
2055 case IBMACPI_FAN_WR_ACPI_FANS:
2056 case IBMACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002057 if ((rc = fan_get_status(&s)) < 0)
2058 return rc;
2059
2060 /* Don't go out of emergency fan mode */
2061 if (s != 7)
2062 s = IBMACPI_FAN_EC_AUTO;
2063
2064 if (!acpi_ec_write(fan_status_offset, s))
2065 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002066 else
2067 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002068 break;
2069
2070 case IBMACPI_FAN_WR_ACPI_SFAN:
2071 if ((rc = fan_get_status(&s)) < 0)
2072 return rc;
2073
2074 s &= 0x07;
2075
2076 /* Set fan to at least level 4 */
2077 if (s < 4)
2078 s = 4;
2079
2080 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002081 return -EIO;
2082 break;
2083
2084 default:
2085 return -ENXIO;
2086 }
2087 return 0;
2088}
2089
2090static int fan_set_disable(void)
2091{
2092 switch (fan_control_access_mode) {
2093 case IBMACPI_FAN_WR_ACPI_FANS:
2094 case IBMACPI_FAN_WR_TPEC:
2095 if (!acpi_ec_write(fan_status_offset, 0x00))
2096 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002097 else
2098 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002099 break;
2100
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002101 case IBMACPI_FAN_WR_ACPI_SFAN:
2102 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2103 return -EIO;
2104 break;
2105
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002106 default:
2107 return -ENXIO;
2108 }
2109 return 0;
2110}
2111
2112static int fan_set_speed(int speed)
2113{
2114 switch (fan_control_access_mode) {
2115 case IBMACPI_FAN_WR_ACPI_FANS:
2116 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002117 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2118 speed, speed, speed))
2119 return -EIO;
2120 } else
2121 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002122 break;
2123
2124 default:
2125 return -ENXIO;
2126 }
2127 return 0;
2128}
2129
2130static int fan_write_cmd_level(const char *cmd, int *rc)
2131{
2132 int level;
2133
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002134 if (strlencmp(cmd, "level auto") == 0)
2135 level = IBMACPI_FAN_EC_AUTO;
2136 else if (strlencmp(cmd, "level disengaged") == 0)
2137 level = IBMACPI_FAN_EC_DISENGAGED;
2138 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002139 return 0;
2140
2141 if ((*rc = fan_set_level(level)) == -ENXIO)
2142 printk(IBM_ERR "level command accepted for unsupported "
2143 "access mode %d", fan_control_access_mode);
2144
2145 return 1;
2146}
2147
2148static int fan_write_cmd_enable(const char *cmd, int *rc)
2149{
2150 if (strlencmp(cmd, "enable") != 0)
2151 return 0;
2152
2153 if ((*rc = fan_set_enable()) == -ENXIO)
2154 printk(IBM_ERR "enable command accepted for unsupported "
2155 "access mode %d", fan_control_access_mode);
2156
2157 return 1;
2158}
2159
2160static int fan_write_cmd_disable(const char *cmd, int *rc)
2161{
2162 if (strlencmp(cmd, "disable") != 0)
2163 return 0;
2164
2165 if ((*rc = fan_set_disable()) == -ENXIO)
2166 printk(IBM_ERR "disable command accepted for unsupported "
2167 "access mode %d", fan_control_access_mode);
2168
2169 return 1;
2170}
2171
2172static int fan_write_cmd_speed(const char *cmd, int *rc)
2173{
2174 int speed;
2175
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002176 /* TODO:
2177 * Support speed <low> <medium> <high> ? */
2178
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002179 if (sscanf(cmd, "speed %d", &speed) != 1)
2180 return 0;
2181
2182 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2183 printk(IBM_ERR "speed command accepted for unsupported "
2184 "access mode %d", fan_control_access_mode);
2185
2186 return 1;
2187}
2188
2189static int fan_write(char *buf)
2190{
2191 char *cmd;
2192 int rc = 0;
2193
2194 while (!rc && (cmd = next_cmd(&buf))) {
2195 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2196 fan_write_cmd_level(cmd, &rc)) &&
2197 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2198 (fan_write_cmd_enable(cmd, &rc) ||
2199 fan_write_cmd_disable(cmd, &rc))) &&
2200 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2201 fan_write_cmd_speed(cmd, &rc))
2202 )
2203 rc = -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002204 }
2205
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002206 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002207}
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209static struct ibm_struct ibms[] = {
2210 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002211 .name = "driver",
2212 .init = driver_init,
2213 .read = driver_read,
2214 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002216 .name = "hotkey",
2217 .hid = IBM_HKEY_HID,
2218 .init = hotkey_init,
2219 .read = hotkey_read,
2220 .write = hotkey_write,
2221 .exit = hotkey_exit,
2222 .notify = hotkey_notify,
2223 .handle = &hkey_handle,
2224 .type = ACPI_DEVICE_NOTIFY,
2225 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002227 .name = "bluetooth",
2228 .init = bluetooth_init,
2229 .read = bluetooth_read,
2230 .write = bluetooth_write,
2231 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002233 .name = "wan",
2234 .init = wan_init,
2235 .read = wan_read,
2236 .write = wan_write,
2237 .experimental = 1,
2238 },
2239 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002240 .name = "video",
2241 .init = video_init,
2242 .read = video_read,
2243 .write = video_write,
2244 .exit = video_exit,
2245 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002247 .name = "light",
2248 .init = light_init,
2249 .read = light_read,
2250 .write = light_write,
2251 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002252#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002254 .name = "dock",
2255 .read = dock_read,
2256 .write = dock_write,
2257 .notify = dock_notify,
2258 .handle = &dock_handle,
2259 .type = ACPI_SYSTEM_NOTIFY,
2260 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002262 .name = "dock",
2263 .hid = IBM_PCI_HID,
2264 .notify = dock_notify,
2265 .handle = &pci_handle,
2266 .type = ACPI_SYSTEM_NOTIFY,
2267 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002270 .name = "bay",
2271 .init = bay_init,
2272 .read = bay_read,
2273 .write = bay_write,
2274 .notify = bay_notify,
2275 .handle = &bay_handle,
2276 .type = ACPI_SYSTEM_NOTIFY,
2277 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002279 .name = "cmos",
2280 .read = cmos_read,
2281 .write = cmos_write,
2282 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002284 .name = "led",
2285 .init = led_init,
2286 .read = led_read,
2287 .write = led_write,
2288 },
2289 {
2290 .name = "beep",
2291 .read = beep_read,
2292 .write = beep_write,
2293 },
2294 {
2295 .name = "thermal",
2296 .init = thermal_init,
2297 .read = thermal_read,
2298 },
2299 {
2300 .name = "ecdump",
2301 .read = ecdump_read,
2302 .write = ecdump_write,
2303 .experimental = 1,
2304 },
2305 {
2306 .name = "brightness",
2307 .read = brightness_read,
2308 .write = brightness_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002309 },
2310 {
2311 .name = "volume",
2312 .read = volume_read,
2313 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002314 },
2315 {
2316 .name = "fan",
2317 .read = fan_read,
2318 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002319 .init = fan_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002320 .experimental = 1,
2321 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324static int dispatch_read(char *page, char **start, off_t off, int count,
2325 int *eof, void *data)
2326{
2327 struct ibm_struct *ibm = (struct ibm_struct *)data;
2328 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (!ibm || !ibm->read)
2331 return -EINVAL;
2332
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002333 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (len < 0)
2335 return len;
2336
2337 if (len <= off + count)
2338 *eof = 1;
2339 *start = page + off;
2340 len -= off;
2341 if (len > count)
2342 len = count;
2343 if (len < 0)
2344 len = 0;
2345
2346 return len;
2347}
2348
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002349static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 unsigned long count, void *data)
2351{
2352 struct ibm_struct *ibm = (struct ibm_struct *)data;
2353 char *kernbuf;
2354 int ret;
2355
2356 if (!ibm || !ibm->write)
2357 return -EINVAL;
2358
2359 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2360 if (!kernbuf)
2361 return -ENOMEM;
2362
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002363 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002365 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367
2368 kernbuf[count] = 0;
2369 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002370 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (ret == 0)
2372 ret = count;
2373
2374 kfree(kernbuf);
2375
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002376 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
2379static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2380{
2381 struct ibm_struct *ibm = (struct ibm_struct *)data;
2382
2383 if (!ibm || !ibm->notify)
2384 return;
2385
2386 ibm->notify(ibm, event);
2387}
2388
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002389static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 acpi_status status;
2392 int ret;
2393
2394 if (!*ibm->handle)
2395 return 0;
2396
2397 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2398 if (ret < 0) {
2399 printk(IBM_ERR "%s device not present\n", ibm->name);
2400 return 0;
2401 }
2402
2403 acpi_driver_data(ibm->device) = ibm;
2404 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2405
2406 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2407 dispatch_notify, ibm);
2408 if (ACPI_FAILURE(status)) {
2409 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2410 ibm->name, status);
2411 return -ENODEV;
2412 }
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return 0;
2415}
2416
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002417static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
2419 return 0;
2420}
2421
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002422static int __init register_driver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 int ret;
2425
2426 ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2427 if (!ibm->driver) {
2428 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2429 return -1;
2430 }
2431
2432 memset(ibm->driver, 0, sizeof(struct acpi_driver));
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002433 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002435 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 ret = acpi_bus_register_driver(ibm->driver);
2438 if (ret < 0) {
2439 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2440 ibm->hid, ret);
2441 kfree(ibm->driver);
2442 }
2443
2444 return ret;
2445}
2446
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002447static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 int ret;
2450 struct proc_dir_entry *entry;
2451
2452 if (ibm->experimental && !experimental)
2453 return 0;
2454
2455 if (ibm->hid) {
2456 ret = register_driver(ibm);
2457 if (ret < 0)
2458 return ret;
2459 ibm->driver_registered = 1;
2460 }
2461
2462 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002463 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (ret != 0)
2465 return ret;
2466 ibm->init_called = 1;
2467 }
2468
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002469 if (ibm->read) {
2470 entry = create_proc_entry(ibm->name,
2471 S_IFREG | S_IRUGO | S_IWUSR,
2472 proc_dir);
2473 if (!entry) {
2474 printk(IBM_ERR "unable to create proc entry %s\n",
2475 ibm->name);
2476 return -ENODEV;
2477 }
2478 entry->owner = THIS_MODULE;
2479 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481 if (ibm->write)
2482 entry->write_proc = &dispatch_write;
2483 ibm->proc_created = 1;
2484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 if (ibm->notify) {
2487 ret = setup_notify(ibm);
2488 if (ret < 0)
2489 return ret;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002490 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492
2493 return 0;
2494}
2495
2496static void ibm_exit(struct ibm_struct *ibm)
2497{
2498 if (ibm->notify_installed)
2499 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2500 dispatch_notify);
2501
2502 if (ibm->proc_created)
2503 remove_proc_entry(ibm->name, proc_dir);
2504
2505 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002506 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508 if (ibm->driver_registered) {
2509 acpi_bus_unregister_driver(ibm->driver);
2510 kfree(ibm->driver);
2511 }
2512}
2513
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002514static void __init ibm_handle_init(char *name,
2515 acpi_handle * handle, acpi_handle parent,
2516 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
2518 int i;
2519 acpi_status status;
2520
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002521 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002523 if (ACPI_SUCCESS(status)) {
2524 *path = paths[i];
2525 return;
2526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002532#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002534 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536static int set_ibm_param(const char *val, struct kernel_param *kp)
2537{
2538 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002540 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2541 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2542 if (strlen(val) > sizeof(ibms[i].param) - 2)
2543 return -ENOSPC;
2544 strcpy(ibms[i].param, val);
2545 strcat(ibms[i].param, ",");
2546 return 0;
2547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return -EINVAL;
2550}
2551
2552#define IBM_PARAM(feature) \
2553 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2554
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002555IBM_PARAM(hotkey);
2556IBM_PARAM(bluetooth);
2557IBM_PARAM(video);
2558IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002559#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002560IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002561#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002562IBM_PARAM(bay);
2563IBM_PARAM(cmos);
2564IBM_PARAM(led);
2565IBM_PARAM(beep);
2566IBM_PARAM(ecdump);
2567IBM_PARAM(brightness);
2568IBM_PARAM(volume);
2569IBM_PARAM(fan);
2570
Holger Macht8acb0252006-10-20 14:30:28 -07002571static struct backlight_properties ibm_backlight_data = {
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02002572 .owner = THIS_MODULE,
2573 .get_brightness = brightness_get,
2574 .update_status = brightness_update_status,
2575 .max_brightness = 7,
Holger Macht8acb0252006-10-20 14:30:28 -07002576};
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static void acpi_ibm_exit(void)
2579{
2580 int i;
2581
Holger Macht8acb0252006-10-20 14:30:28 -07002582 if (ibm_backlight_device)
2583 backlight_device_unregister(ibm_backlight_device);
2584
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002585 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 ibm_exit(&ibms[i]);
2587
2588 remove_proc_entry(IBM_DIR, acpi_root_dir);
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002589
2590 if (ibm_thinkpad_ec_found)
2591 kfree(ibm_thinkpad_ec_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002594static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002595{
2596 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002597 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002598
2599 /*
2600 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2601 * X32 or newer, all Z series; Some models must have an
2602 * up-to-date BIOS or they will not be detected.
2603 *
2604 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2605 */
2606 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002607 if (sscanf(dev->name,
2608 "IBM ThinkPad Embedded Controller -[%17c",
2609 ec_fw_string) == 1) {
2610 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2611 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2612 return kstrdup(ec_fw_string, GFP_KERNEL);
2613 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002614 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002615 return NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002616}
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618static int __init acpi_ibm_init(void)
2619{
2620 int ret, i;
2621
2622 if (acpi_disabled)
2623 return -ENODEV;
2624
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002625 if (!acpi_specific_hotkey_enabled) {
2626 printk(IBM_ERR "using generic hotkey driver\n");
2627 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -05002628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002630 /* ec is required because many other handles are relative to it */
2631 IBM_HANDLE_INIT(ec);
2632 if (!ec_handle) {
2633 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002637 /* Models with newer firmware report the EC in DMI */
2638 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002639 if (ibm_thinkpad_ec_found)
2640 printk(IBM_INFO "ThinkPad EC firmware %s\n",
2641 ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002644 IBM_HANDLE_INIT(vid);
2645 IBM_HANDLE_INIT(vid2);
2646 IBM_HANDLE_INIT(ledb);
2647 IBM_HANDLE_INIT(led);
2648 IBM_HANDLE_INIT(hkey);
2649 IBM_HANDLE_INIT(lght);
2650 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002651#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002652 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002653#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002654 IBM_HANDLE_INIT(pci);
2655 IBM_HANDLE_INIT(bay);
2656 if (bay_handle)
2657 IBM_HANDLE_INIT(bay_ej);
2658 IBM_HANDLE_INIT(bay2);
2659 if (bay2_handle)
2660 IBM_HANDLE_INIT(bay2_ej);
2661 IBM_HANDLE_INIT(beep);
2662 IBM_HANDLE_INIT(ecrd);
2663 IBM_HANDLE_INIT(ecwr);
2664 IBM_HANDLE_INIT(fans);
2665 IBM_HANDLE_INIT(gfan);
2666 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2669 if (!proc_dir) {
2670 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2671 return -ENODEV;
2672 }
2673 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002674
2675 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002677 if (ret >= 0 && *ibms[i].param)
2678 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (ret < 0) {
2680 acpi_ibm_exit();
2681 return ret;
2682 }
2683 }
2684
Holger Macht8acb0252006-10-20 14:30:28 -07002685 ibm_backlight_device = backlight_device_register("ibm", NULL,
2686 &ibm_backlight_data);
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02002687 if (IS_ERR(ibm_backlight_device)) {
Holger Macht8acb0252006-10-20 14:30:28 -07002688 printk(IBM_ERR "Could not register ibm backlight device\n");
2689 ibm_backlight_device = NULL;
2690 acpi_ibm_exit();
2691 }
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return 0;
2694}
2695
2696module_init(acpi_ibm_init);
2697module_exit(acpi_ibm_exit);