blob: b6ad2ed5fd6ce1dd76d75212abcc484a860da513 [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>
80#include <linux/proc_fs.h>
Holger Macht8acb0252006-10-20 14:30:28 -070081#include <linux/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <asm/uaccess.h>
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020083#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#include <acpi/acpi_drivers.h>
86#include <acpi/acnamesp.h>
87
88#define IBM_NAME "ibm"
89#define IBM_DESC "IBM ThinkPad ACPI Extras"
90#define IBM_FILE "ibm_acpi"
91#define IBM_URL "http://ibm-acpi.sf.net/"
92
Borislav Deianov78f81cc2005-08-17 00:00:00 -040093MODULE_AUTHOR("Borislav Deianov");
94MODULE_DESCRIPTION(IBM_DESC);
95MODULE_VERSION(IBM_VERSION);
96MODULE_LICENSE("GPL");
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define IBM_DIR IBM_NAME
99
100#define IBM_LOG IBM_FILE ": "
101#define IBM_ERR KERN_ERR IBM_LOG
102#define IBM_NOTICE KERN_NOTICE IBM_LOG
103#define IBM_INFO KERN_INFO IBM_LOG
104#define IBM_DEBUG KERN_DEBUG IBM_LOG
105
106#define IBM_MAX_ACPI_ARGS 3
107
108#define __unused __attribute__ ((unused))
109
110static int experimental;
111module_param(experimental, int, 0);
112
113static acpi_handle root_handle = NULL;
114
115#define IBM_HANDLE(object, parent, paths...) \
116 static acpi_handle object##_handle; \
117 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400118 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 static char *object##_paths[] = { paths }
120
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400121/*
122 * The following models are supported to various degrees:
123 *
124 * 570, 600e, 600x, 770e, 770x
125 * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
126 * G40, G41
127 * R30, R31, R32, R40, R40e, R50, R50e, R50p, R51
128 * T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p, T43
129 * X20, X21, X22, X23, X24, X30, X31, X40
130 *
131 * The following models have no supported features:
132 *
133 * 240, 240x, i1400
134 *
135 * Still missing DSDTs for the following models:
136 *
137 * A20p, A22e, A22m
138 * R52
139 * S31
140 * T43p
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
144 "\\_SB.PCI.ISA.EC", /* 570 */
145 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
146 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
147 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
148 "\\_SB.PCI0.ICH3.EC0", /* R31 */
149 "\\_SB.PCI0.LPC.EC", /* all others */
150 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400152IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
153 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
154 "\\_SB.PCI0.VID0", /* 770e */
155 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
156 "\\_SB.PCI0.AGP.VID", /* all others */
157 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400159IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400161IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
162 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
163 "\\CMS", /* R40, R40e */
164 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800165#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400166IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
167 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
168 "\\_SB.PCI0.PCI1.DOCK", /* all others */
169 "\\_SB.PCI.ISA.SLCE", /* 570 */
170 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800171#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400172IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
173 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
174 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
175 ); /* A21e, R30, R31 */
176
177IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
178 "_EJ0", /* all others */
179 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
180
181IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
182 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
183 ); /* all others */
184
185IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
186 "_EJ0", /* 770x */
187 ); /* all others */
188
189/* don't list other alternatives as we install a notify handler on the 570 */
190IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
191
192IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
193 "^HKEY", /* R30, R31 */
194 "HKEY", /* all others */
195 ); /* 570 */
196
197IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
198IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
199
200IBM_HANDLE(led, ec, "SLED", /* 570 */
201 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
202 "LED", /* all others */
203 ); /* R30, R31 */
204
205IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
206IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
207IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200208IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400209
210IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
211 "\\FSPD", /* 600e/x, 770e, 770x */
212 ); /* all others */
213
214IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
215 "JFNS", /* 770x-JL */
216 ); /* all others */
217
218#define IBM_HKEY_HID "IBM0068"
219#define IBM_PCI_HID "PNP0A03"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200221enum thermal_access_mode {
222 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
223 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
224 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200225 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
226 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200227};
228
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200229#define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200230struct ibm_thermal_sensors_struct {
231 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
232};
233
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200234/*
235 * FAN ACCESS MODES
236 *
237 * IBMACPI_FAN_RD_ACPI_GFAN:
238 * ACPI GFAN method: returns fan level
239 *
240 * see IBMACPI_FAN_WR_ACPI_SFAN
241 * EC 0x2f not available if GFAN exists
242 *
243 * IBMACPI_FAN_WR_ACPI_SFAN:
244 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
245 *
246 * EC 0x2f might be available *for reading*, but never for writing.
247 *
248 * IBMACPI_FAN_WR_TPEC:
249 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
250 * on almost all ThinkPads
251 *
252 * Fan speed changes of any sort (including those caused by the
253 * disengaged mode) are usually done slowly by the firmware as the
254 * maximum ammount of fan duty cycle change per second seems to be
255 * limited.
256 *
257 * Reading is not available if GFAN exists.
258 * Writing is not available if SFAN exists.
259 *
260 * Bits
261 * 7 automatic mode engaged;
262 * (default operation mode of the ThinkPad)
263 * fan level is ignored in this mode.
264 * 6 disengage mode (takes precedence over bit 7);
265 * not available on all thinkpads. May disable
266 * the tachometer, and speeds up fan to 100% duty-cycle,
267 * which speeds it up far above the standard RPM
268 * levels. It is not impossible that it could cause
269 * hardware damage.
270 * 5-3 unused in some models. Extra bits for fan level
271 * in others, but still useless as all values above
272 * 7 map to the same speed as level 7 in these models.
273 * 2-0 fan level (0..7 usually)
274 * 0x00 = stop
275 * 0x07 = max (set when temperatures critical)
276 * Some ThinkPads may have other levels, see
277 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
278 *
279 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
280 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
281 * does so, its initial value is meaningless (0x07).
282 *
283 * For firmware bugs, refer to:
284 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
285 *
286 * ----
287 *
288 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
289 * Main fan tachometer reading (in RPM)
290 *
291 * This register is present on all ThinkPads with a new-style EC, and
292 * it is known not to be present on the A21m/e, and T22, as there is
293 * something else in offset 0x84 according to the ACPI DSDT. Other
294 * ThinkPads from this same time period (and earlier) probably lack the
295 * tachometer as well.
296 *
297 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
298 * was never fixed by IBM to report the EC firmware version string
299 * probably support the tachometer (like the early X models), so
300 * detecting it is quite hard. We need more data to know for sure.
301 *
302 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
303 * might result.
304 *
305 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
306 * register is not invalidated in ThinkPads that disable tachometer
307 * readings. Thus, the tachometer readings go stale.
308 *
309 * For firmware bugs, refer to:
310 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
311 *
312 * IBMACPI_FAN_WR_ACPI_FANS:
313 * ThinkPad X31, X40, X41. Not available in the X60.
314 *
315 * FANS ACPI handle: takes three arguments: low speed, medium speed,
316 * high speed. ACPI DSDT seems to map these three speeds to levels
317 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
318 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
319 *
320 * The speeds are stored on handles
321 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
322 *
323 * There are three default speed sets, acessible as handles:
324 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
325 *
326 * ACPI DSDT switches which set is in use depending on various
327 * factors.
328 *
329 * IBMACPI_FAN_WR_TPEC is also available and should be used to
330 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
331 * but the ACPI tables just mention level 7.
332 */
333
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200334enum fan_status_access_mode {
335 IBMACPI_FAN_NONE = 0, /* No fan status or control */
336 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
337 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
338};
339
340enum fan_control_access_mode {
341 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
342 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
343 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
344 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
345};
346
347enum fan_control_commands {
348 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
349 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
350 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd */
351};
352
353enum { /* Fan control constants */
354 fan_status_offset = 0x2f, /* EC register 0x2f */
355 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
356 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -0200357
358 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
359 * disengaged */
360 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
361 * control */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200362};
363
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200364static int ibm_thinkpad_ec_found;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366struct ibm_struct {
367 char *name;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400368 char param[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 char *hid;
371 struct acpi_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400373 int (*init) (void);
374 int (*read) (char *);
375 int (*write) (char *);
376 void (*exit) (void);
377
378 void (*notify) (struct ibm_struct *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 acpi_handle *handle;
380 int type;
381 struct acpi_device *device;
382
383 int driver_registered;
384 int proc_created;
385 int init_called;
386 int notify_installed;
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int experimental;
389};
390
391static struct proc_dir_entry *proc_dir = NULL;
392
Holger Macht8acb0252006-10-20 14:30:28 -0700393static struct backlight_device *ibm_backlight_device;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
396#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
397#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
398
399static int acpi_evalf(acpi_handle handle,
400 void *res, char *method, char *fmt, ...)
401{
402 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400403 struct acpi_object_list params;
404 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
405 struct acpi_buffer result, *resultp;
406 union acpi_object out_obj;
407 acpi_status status;
408 va_list ap;
409 char res_type;
410 int success;
411 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (!*fmt) {
414 printk(IBM_ERR "acpi_evalf() called with empty format\n");
415 return 0;
416 }
417
418 if (*fmt == 'q') {
419 quiet = 1;
420 fmt++;
421 } else
422 quiet = 0;
423
424 res_type = *(fmt++);
425
426 params.count = 0;
427 params.pointer = &in_objs[0];
428
429 va_start(ap, fmt);
430 while (*fmt) {
431 char c = *(fmt++);
432 switch (c) {
433 case 'd': /* int */
434 in_objs[params.count].integer.value = va_arg(ap, int);
435 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
436 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400437 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 default:
439 printk(IBM_ERR "acpi_evalf() called "
440 "with invalid format character '%c'\n", c);
441 return 0;
442 }
443 }
444 va_end(ap);
445
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400446 if (res_type != 'v') {
447 result.length = sizeof(out_obj);
448 result.pointer = &out_obj;
449 resultp = &result;
450 } else
451 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400453 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400456 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (res)
458 *(int *)res = out_obj.integer.value;
459 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
460 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400461 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 success = status == AE_OK;
463 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400464 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 default:
466 printk(IBM_ERR "acpi_evalf() called "
467 "with invalid format character '%c'\n", res_type);
468 return 0;
469 }
470
471 if (!success && !quiet)
472 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
473 method, fmt0, status);
474
475 return success;
476}
477
478static void __unused acpi_print_int(acpi_handle handle, char *method)
479{
480 int i;
481
482 if (acpi_evalf(handle, &i, method, "d"))
483 printk(IBM_INFO "%s = 0x%x\n", method, i);
484 else
485 printk(IBM_ERR "error calling %s\n", method);
486}
487
488static char *next_cmd(char **cmds)
489{
490 char *start = *cmds;
491 char *end;
492
493 while ((end = strchr(start, ',')) && end == start)
494 start = end + 1;
495
496 if (!end)
497 return NULL;
498
499 *end = 0;
500 *cmds = end + 1;
501 return start;
502}
503
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400504static int driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
507 printk(IBM_INFO "%s\n", IBM_URL);
508
509 return 0;
510}
511
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400512static int driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 int len = 0;
515
516 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
517 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
518
519 return len;
520}
521
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400522static int hotkey_supported;
523static int hotkey_mask_supported;
524static int hotkey_orig_status;
525static int hotkey_orig_mask;
526
527static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400530 return 0;
531
532 if (hotkey_mask_supported)
533 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
534 return 0;
535
536 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400539static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 int i;
542
543 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return 0;
545
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400546 if (hotkey_mask_supported)
547 for (i = 0; i < 32; i++) {
548 int bit = ((1 << i) & mask) != 0;
549 if (!acpi_evalf(hkey_handle,
550 NULL, "MHKM", "vdd", i + 1, bit))
551 return 0;
552 }
553
554 return 1;
555}
556
557static int hotkey_init(void)
558{
559 /* hotkey not supported on 570 */
560 hotkey_supported = hkey_handle != NULL;
561
562 if (hotkey_supported) {
563 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
564 A30, R30, R31, T20-22, X20-21, X22-24 */
565 hotkey_mask_supported =
566 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
567
568 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
569 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571
572 return 0;
573}
574
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400575static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 int status, mask;
578 int len = 0;
579
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400580 if (!hotkey_supported) {
581 len += sprintf(p + len, "status:\t\tnot supported\n");
582 return len;
583 }
584
585 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return -EIO;
587
588 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400589 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
591 len += sprintf(p + len,
592 "commands:\tenable, disable, reset, <mask>\n");
593 } else {
594 len += sprintf(p + len, "mask:\t\tnot supported\n");
595 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
596 }
597
598 return len;
599}
600
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400601static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 int status, mask;
604 char *cmd;
605 int do_cmd = 0;
606
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400607 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return -ENODEV;
609
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400610 if (!hotkey_get(&status, &mask))
611 return -EIO;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 while ((cmd = next_cmd(&buf))) {
614 if (strlencmp(cmd, "enable") == 0) {
615 status = 1;
616 } else if (strlencmp(cmd, "disable") == 0) {
617 status = 0;
618 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400619 status = hotkey_orig_status;
620 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
622 /* mask set */
623 } else if (sscanf(cmd, "%x", &mask) == 1) {
624 /* mask set */
625 } else
626 return -EINVAL;
627 do_cmd = 1;
628 }
629
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400630 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return -EIO;
632
633 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400634}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400636static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400638 if (hotkey_supported)
639 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static void hotkey_notify(struct ibm_struct *ibm, u32 event)
643{
644 int hkey;
645
646 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
647 acpi_bus_generate_event(ibm->device, event, hkey);
648 else {
649 printk(IBM_ERR "unknown hotkey event %d\n", event);
650 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400654static int bluetooth_supported;
655
656static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400658 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
659 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
660 bluetooth_supported = hkey_handle &&
661 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 return 0;
664}
665
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400666static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 int status;
669
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400670 if (!bluetooth_supported ||
671 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 status = 0;
673
674 return status;
675}
676
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400677static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400680 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400682 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 len += sprintf(p + len, "status:\t\tnot supported\n");
684 else if (!(status & 1))
685 len += sprintf(p + len, "status:\t\tnot installed\n");
686 else {
687 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
688 len += sprintf(p + len, "commands:\tenable, disable\n");
689 }
690
691 return len;
692}
693
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400694static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400696 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 char *cmd;
698 int do_cmd = 0;
699
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400700 if (!bluetooth_supported)
701 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 while ((cmd = next_cmd(&buf))) {
704 if (strlencmp(cmd, "enable") == 0) {
705 status |= 2;
706 } else if (strlencmp(cmd, "disable") == 0) {
707 status &= ~2;
708 } else
709 return -EINVAL;
710 do_cmd = 1;
711 }
712
713 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400714 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 return 0;
717}
718
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400719static int wan_supported;
720
721static int wan_init(void)
722{
723 wan_supported = hkey_handle &&
724 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
725
726 return 0;
727}
728
729static int wan_status(void)
730{
731 int status;
732
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200733 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400734 status = 0;
735
736 return status;
737}
738
739static int wan_read(char *p)
740{
741 int len = 0;
742 int status = wan_status();
743
744 if (!wan_supported)
745 len += sprintf(p + len, "status:\t\tnot supported\n");
746 else if (!(status & 1))
747 len += sprintf(p + len, "status:\t\tnot installed\n");
748 else {
749 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
750 len += sprintf(p + len, "commands:\tenable, disable\n");
751 }
752
753 return len;
754}
755
756static int wan_write(char *buf)
757{
758 int status = wan_status();
759 char *cmd;
760 int do_cmd = 0;
761
762 if (!wan_supported)
763 return -ENODEV;
764
765 while ((cmd = next_cmd(&buf))) {
766 if (strlencmp(cmd, "enable") == 0) {
767 status |= 2;
768 } else if (strlencmp(cmd, "disable") == 0) {
769 status &= ~2;
770 } else
771 return -EINVAL;
772 do_cmd = 1;
773 }
774
775 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
776 return -EIO;
777
778 return 0;
779}
780
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400781static int video_supported;
782static int video_orig_autosw;
783
784#define VIDEO_570 1
785#define VIDEO_770 2
786#define VIDEO_NEW 3
787
788static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400790 int ivga;
791
792 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
793 /* G41, assume IVGA doesn't change */
794 vid_handle = vid2_handle;
795
796 if (!vid_handle)
797 /* video switching not supported on R30, R31 */
798 video_supported = 0;
799 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
800 /* 570 */
801 video_supported = VIDEO_570;
802 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
803 /* 600e/x, 770e, 770x */
804 video_supported = VIDEO_770;
805 else
806 /* all others */
807 video_supported = VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 return 0;
810}
811
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400812static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 int status = 0;
815 int i;
816
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400817 if (video_supported == VIDEO_570) {
818 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
819 status = i & 3;
820 } else if (video_supported == VIDEO_770) {
821 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
822 status |= 0x01 * i;
823 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
824 status |= 0x02 * i;
825 } else if (video_supported == VIDEO_NEW) {
826 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
827 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
828 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400830 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
831 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
832 status |= 0x01 * i;
833 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
834 status |= 0x08 * i;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 return status;
838}
839
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400840static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400842 int autosw = 0;
843
844 if (video_supported == VIDEO_570)
845 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
846 else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
847 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
848
849 return autosw & 1;
850}
851
852static int video_read(char *p)
853{
854 int status = video_status();
855 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 int len = 0;
857
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400858 if (!video_supported) {
859 len += sprintf(p + len, "status:\t\tnot supported\n");
860 return len;
861 }
862
863 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
865 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400866 if (video_supported == VIDEO_NEW)
867 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
868 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
869 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
870 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
871 if (video_supported == VIDEO_NEW)
872 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
873 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
875
876 return len;
877}
878
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400879static int video_switch(void)
880{
881 int autosw = video_autosw();
882 int ret;
883
884 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
885 return -EIO;
886 ret = video_supported == VIDEO_570 ?
887 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
888 acpi_evalf(vid_handle, NULL, "VSWT", "v");
889 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
890
891 return ret;
892}
893
894static int video_expand(void)
895{
896 if (video_supported == VIDEO_570)
897 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
898 else if (video_supported == VIDEO_770)
899 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
900 else
901 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
902}
903
904static int video_switch2(int status)
905{
906 int ret;
907
908 if (video_supported == VIDEO_570) {
909 ret = acpi_evalf(NULL, NULL,
910 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
911 } else if (video_supported == VIDEO_770) {
912 int autosw = video_autosw();
913 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
914 return -EIO;
915
916 ret = acpi_evalf(vid_handle, NULL,
917 "ASWT", "vdd", status * 0x100, 0);
918
919 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
920 } else {
921 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
922 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
923 }
924
925 return ret;
926}
927
928static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 char *cmd;
931 int enable, disable, status;
932
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400933 if (!video_supported)
934 return -ENODEV;
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 enable = disable = 0;
937
938 while ((cmd = next_cmd(&buf))) {
939 if (strlencmp(cmd, "lcd_enable") == 0) {
940 enable |= 0x01;
941 } else if (strlencmp(cmd, "lcd_disable") == 0) {
942 disable |= 0x01;
943 } else if (strlencmp(cmd, "crt_enable") == 0) {
944 enable |= 0x02;
945 } else if (strlencmp(cmd, "crt_disable") == 0) {
946 disable |= 0x02;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400947 } else if (video_supported == VIDEO_NEW &&
948 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 enable |= 0x08;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400950 } else if (video_supported == VIDEO_NEW &&
951 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 disable |= 0x08;
953 } else if (strlencmp(cmd, "auto_enable") == 0) {
954 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
955 return -EIO;
956 } else if (strlencmp(cmd, "auto_disable") == 0) {
957 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
958 return -EIO;
959 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400960 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -EIO;
962 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400963 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -EIO;
965 } else
966 return -EINVAL;
967 }
968
969 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400970 status = (video_status() & 0x0f & ~disable) | enable;
971 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return -EIO;
973 }
974
975 return 0;
976}
977
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400978static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400980 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400983static int light_supported;
984static int light_status_supported;
985
986static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400988 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
989 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
990
991 if (light_supported)
992 /* light status not supported on
993 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
994 light_status_supported = acpi_evalf(ec_handle, NULL,
995 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 return 0;
998}
999
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001000static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 int len = 0;
1003 int status = 0;
1004
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001005 if (!light_supported) {
1006 len += sprintf(p + len, "status:\t\tnot supported\n");
1007 } else if (!light_status_supported) {
1008 len += sprintf(p + len, "status:\t\tunknown\n");
1009 len += sprintf(p + len, "commands:\ton, off\n");
1010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1012 return -EIO;
1013 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001014 len += sprintf(p + len, "commands:\ton, off\n");
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 return len;
1018}
1019
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001020static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 int cmos_cmd, lght_cmd;
1023 char *cmd;
1024 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001025
1026 if (!light_supported)
1027 return -ENODEV;
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 while ((cmd = next_cmd(&buf))) {
1030 if (strlencmp(cmd, "on") == 0) {
1031 cmos_cmd = 0x0c;
1032 lght_cmd = 1;
1033 } else if (strlencmp(cmd, "off") == 0) {
1034 cmos_cmd = 0x0d;
1035 lght_cmd = 0;
1036 } else
1037 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001040 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1041 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!success)
1043 return -EIO;
1044 }
1045
1046 return 0;
1047}
1048
1049static int _sta(acpi_handle handle)
1050{
1051 int status;
1052
1053 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1054 status = 0;
1055
1056 return status;
1057}
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001058
Kristen Accardi63e5f242006-02-23 17:56:06 -08001059#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060#define dock_docked() (_sta(dock_handle) & 1)
1061
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001062static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 int len = 0;
1065 int docked = dock_docked();
1066
1067 if (!dock_handle)
1068 len += sprintf(p + len, "status:\t\tnot supported\n");
1069 else if (!docked)
1070 len += sprintf(p + len, "status:\t\tundocked\n");
1071 else {
1072 len += sprintf(p + len, "status:\t\tdocked\n");
1073 len += sprintf(p + len, "commands:\tdock, undock\n");
1074 }
1075
1076 return len;
1077}
1078
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001079static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 char *cmd;
1082
1083 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001084 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 while ((cmd = next_cmd(&buf))) {
1087 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001088 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1089 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -EIO;
1091 } else if (strlencmp(cmd, "dock") == 0) {
1092 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1093 return -EIO;
1094 } else
1095 return -EINVAL;
1096 }
1097
1098 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101static void dock_notify(struct ibm_struct *ibm, u32 event)
1102{
1103 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001104 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001106 if (event == 1 && !pci) /* 570 */
1107 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1108 else if (event == 1 && pci) /* 570 */
1109 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1110 else if (event == 3 && docked)
1111 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001113 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001115 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 else {
1117 printk(IBM_ERR "unknown dock event %d, status %d\n",
1118 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001119 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001124static int bay_status_supported;
1125static int bay_status2_supported;
1126static int bay_eject_supported;
1127static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001129static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001131 bay_status_supported = bay_handle &&
1132 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1133 bay_status2_supported = bay2_handle &&
1134 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1135
1136 bay_eject_supported = bay_handle && bay_ej_handle &&
1137 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1138 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1139 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 return 0;
1142}
1143
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001144#define bay_occupied(b) (_sta(b##_handle) & 1)
1145
1146static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001149 int occupied = bay_occupied(bay);
1150 int occupied2 = bay_occupied(bay2);
1151 int eject, eject2;
1152
1153 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1154 (occupied ? "occupied" : "unoccupied") :
1155 "not supported");
1156 if (bay_status2_supported)
1157 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1158 "occupied" : "unoccupied");
1159
1160 eject = bay_eject_supported && occupied;
1161 eject2 = bay_eject2_supported && occupied2;
1162
1163 if (eject && eject2)
1164 len += sprintf(p + len, "commands:\teject, eject2\n");
1165 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001167 else if (eject2)
1168 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 return len;
1171}
1172
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001173static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 char *cmd;
1176
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001177 if (!bay_eject_supported && !bay_eject2_supported)
1178 return -ENODEV;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001181 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1182 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1183 return -EIO;
1184 } else if (bay_eject2_supported &&
1185 strlencmp(cmd, "eject2") == 0) {
1186 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -EIO;
1188 } else
1189 return -EINVAL;
1190 }
1191
1192 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001193}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195static void bay_notify(struct ibm_struct *ibm, u32 event)
1196{
1197 acpi_bus_generate_event(ibm->device, event, 0);
1198}
1199
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001200static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 int len = 0;
1203
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001204 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1205 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (!cmos_handle)
1207 len += sprintf(p + len, "status:\t\tnot supported\n");
1208 else {
1209 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001210 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212
1213 return len;
1214}
1215
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001216static int cmos_eval(int cmos_cmd)
1217{
1218 if (cmos_handle)
1219 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1220 else
1221 return 1;
1222}
1223
1224static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 char *cmd;
1227 int cmos_cmd;
1228
1229 if (!cmos_handle)
1230 return -EINVAL;
1231
1232 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001233 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1234 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /* cmos_cmd set */
1236 } else
1237 return -EINVAL;
1238
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001239 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return -EIO;
1241 }
1242
1243 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001244}
1245
1246static int led_supported;
1247
1248#define LED_570 1
1249#define LED_OLD 2
1250#define LED_NEW 3
1251
1252static int led_init(void)
1253{
1254 if (!led_handle)
1255 /* led not supported on R30, R31 */
1256 led_supported = 0;
1257 else if (strlencmp(led_path, "SLED") == 0)
1258 /* 570 */
1259 led_supported = LED_570;
1260 else if (strlencmp(led_path, "SYSL") == 0)
1261 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1262 led_supported = LED_OLD;
1263 else
1264 /* all others */
1265 led_supported = LED_NEW;
1266
1267 return 0;
1268}
1269
1270#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1271
1272static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 int len = 0;
1275
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001276 if (!led_supported) {
1277 len += sprintf(p + len, "status:\t\tnot supported\n");
1278 return len;
1279 }
1280 len += sprintf(p + len, "status:\t\tsupported\n");
1281
1282 if (led_supported == LED_570) {
1283 /* 570 */
1284 int i, status;
1285 for (i = 0; i < 8; i++) {
1286 if (!acpi_evalf(ec_handle,
1287 &status, "GLED", "dd", 1 << i))
1288 return -EIO;
1289 len += sprintf(p + len, "%d:\t\t%s\n",
1290 i, led_status(status));
1291 }
1292 }
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001295 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 return len;
1298}
1299
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001300/* off, on, blink */
1301static const int led_sled_arg1[] = { 0, 1, 3 };
1302static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1303static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1304static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1305
1306#define EC_HLCL 0x0c
1307#define EC_HLBL 0x0d
1308#define EC_HLMS 0x0e
1309
1310static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001313 int led, ind, ret;
1314
1315 if (!led_supported)
1316 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001319 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return -EINVAL;
1321
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001322 if (strstr(cmd, "off")) {
1323 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001325 ind = 1;
1326 } else if (strstr(cmd, "blink")) {
1327 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 } else
1329 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001330
1331 if (led_supported == LED_570) {
1332 /* 570 */
1333 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001335 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001337 } else if (led_supported == LED_OLD) {
1338 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1339 led = 1 << led;
1340 ret = ec_write(EC_HLMS, led);
1341 if (ret >= 0)
1342 ret =
1343 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1344 if (ret >= 0)
1345 ret =
1346 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1347 if (ret < 0)
1348 return ret;
1349 } else {
1350 /* all others */
1351 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1352 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356
1357 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001358}
1359
1360static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 int len = 0;
1363
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001364 if (!beep_handle)
1365 len += sprintf(p + len, "status:\t\tnot supported\n");
1366 else {
1367 len += sprintf(p + len, "status:\t\tsupported\n");
1368 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 return len;
1372}
1373
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001374static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 char *cmd;
1377 int beep_cmd;
1378
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001379 if (!beep_handle)
1380 return -ENODEV;
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001383 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1384 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* beep_cmd set */
1386 } else
1387 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001388 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return -EIO;
1390 }
1391
1392 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001393}
1394
1395static int acpi_ec_read(int i, u8 * p)
1396{
1397 int v;
1398
1399 if (ecrd_handle) {
1400 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1401 return 0;
1402 *p = v;
1403 } else {
1404 if (ec_read(i, p) < 0)
1405 return 0;
1406 }
1407
1408 return 1;
1409}
1410
1411static int acpi_ec_write(int i, u8 v)
1412{
1413 if (ecwr_handle) {
1414 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1415 return 0;
1416 } else {
1417 if (ec_write(i, v) < 0)
1418 return 0;
1419 }
1420
1421 return 1;
1422}
1423
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001424static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001425
1426static int thermal_init(void)
1427{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001428 u8 t, ta1, ta2;
1429 int i;
1430 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1431
1432 if (ibm_thinkpad_ec_found && experimental) {
1433 /*
1434 * Direct EC access mode: sensors at registers
1435 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1436 * non-implemented, thermal sensors return 0x80 when
1437 * not available
1438 */
1439
1440 ta1 = ta2 = 0;
1441 for (i = 0; i < 8; i++) {
1442 if (likely(acpi_ec_read(0x78 + i, &t))) {
1443 ta1 |= t;
1444 } else {
1445 ta1 = 0;
1446 break;
1447 }
1448 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1449 ta2 |= t;
1450 } else {
1451 ta1 = 0;
1452 break;
1453 }
1454 }
1455 if (ta1 == 0) {
1456 /* This is sheer paranoia, but we handle it anyway */
1457 if (acpi_tmp7) {
1458 printk(IBM_ERR
1459 "ThinkPad ACPI EC access misbehaving, "
1460 "falling back to ACPI TMPx access mode\n");
1461 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1462 } else {
1463 printk(IBM_ERR
1464 "ThinkPad ACPI EC access misbehaving, "
1465 "disabling thermal sensors access\n");
1466 thermal_read_mode = IBMACPI_THERMAL_NONE;
1467 }
1468 } else {
1469 thermal_read_mode =
1470 (ta2 != 0) ?
1471 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1472 }
1473 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001474 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1475 /* 600e/x, 770e, 770x */
1476 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1477 } else {
1478 /* Standard ACPI TMPx access, max 8 sensors */
1479 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1480 }
1481 } else {
1482 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1483 thermal_read_mode = IBMACPI_THERMAL_NONE;
1484 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001485
1486 return 0;
1487}
1488
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001489static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1490{
1491 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001492 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001493 char tmpi[] = "TMPi";
1494
1495 if (!s)
1496 return -EINVAL;
1497
1498 switch (thermal_read_mode) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001499#if IBMACPI_MAX_THERMAL_SENSORS >= 16
1500 case IBMACPI_THERMAL_TPEC_16:
1501 for (i = 0; i < 8; i++) {
1502 if (!acpi_ec_read(0xC0 + i, &tmp))
1503 return -EIO;
1504 s->temp[i + 8] = tmp * 1000;
1505 }
1506 /* fallthrough */
1507#endif
1508 case IBMACPI_THERMAL_TPEC_8:
1509 for (i = 0; i < 8; i++) {
1510 if (!acpi_ec_read(0x78 + i, &tmp))
1511 return -EIO;
1512 s->temp[i] = tmp * 1000;
1513 }
1514 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1515
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001516 case IBMACPI_THERMAL_ACPI_UPDT:
1517 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1518 return -EIO;
1519 for (i = 0; i < 8; i++) {
1520 tmpi[3] = '0' + i;
1521 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1522 return -EIO;
1523 s->temp[i] = (t - 2732) * 100;
1524 }
1525 return 8;
1526
1527 case IBMACPI_THERMAL_ACPI_TMP07:
1528 for (i = 0; i < 8; i++) {
1529 tmpi[3] = '0' + i;
1530 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1531 return -EIO;
1532 s->temp[i] = t * 1000;
1533 }
1534 return 8;
1535
1536 case IBMACPI_THERMAL_NONE:
1537 default:
1538 return 0;
1539 }
1540}
1541
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001542static int thermal_read(char *p)
1543{
1544 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001545 int n, i;
1546 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001547
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001548 n = thermal_get_sensors(&t);
1549 if (unlikely(n < 0))
1550 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001551
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001552 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001553
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001554 if (n > 0) {
1555 for (i = 0; i < (n - 1); i++)
1556 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1557 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1558 } else
1559 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001560
1561 return len;
1562}
1563
1564static u8 ecdump_regs[256];
1565
1566static int ecdump_read(char *p)
1567{
1568 int len = 0;
1569 int i, j;
1570 u8 v;
1571
1572 len += sprintf(p + len, "EC "
1573 " +00 +01 +02 +03 +04 +05 +06 +07"
1574 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1575 for (i = 0; i < 256; i += 16) {
1576 len += sprintf(p + len, "EC 0x%02x:", i);
1577 for (j = 0; j < 16; j++) {
1578 if (!acpi_ec_read(i + j, &v))
1579 break;
1580 if (v != ecdump_regs[i + j])
1581 len += sprintf(p + len, " *%02x", v);
1582 else
1583 len += sprintf(p + len, " %02x", v);
1584 ecdump_regs[i + j] = v;
1585 }
1586 len += sprintf(p + len, "\n");
1587 if (j != 16)
1588 break;
1589 }
1590
1591 /* These are way too dangerous to advertise openly... */
1592#if 0
1593 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1594 " (<offset> is 00-ff, <value> is 00-ff)\n");
1595 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1596 " (<offset> is 00-ff, <value> is 0-255)\n");
1597#endif
1598 return len;
1599}
1600
1601static int ecdump_write(char *buf)
1602{
1603 char *cmd;
1604 int i, v;
1605
1606 while ((cmd = next_cmd(&buf))) {
1607 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1608 /* i and v set */
1609 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1610 /* i and v set */
1611 } else
1612 return -EINVAL;
1613 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1614 if (!acpi_ec_write(i, v))
1615 return -EIO;
1616 } else
1617 return -EINVAL;
1618 }
1619
1620 return 0;
1621}
1622
1623static int brightness_offset = 0x31;
1624
Holger Macht8acb0252006-10-20 14:30:28 -07001625static int brightness_get(struct backlight_device *bd)
1626{
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001627 u8 level;
1628 if (!acpi_ec_read(brightness_offset, &level))
1629 return -EIO;
Holger Macht8acb0252006-10-20 14:30:28 -07001630
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001631 level &= 0x7;
1632 return level;
Holger Macht8acb0252006-10-20 14:30:28 -07001633}
1634
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001635static int brightness_read(char *p)
1636{
1637 int len = 0;
Holger Macht8acb0252006-10-20 14:30:28 -07001638 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001639
Holger Macht8acb0252006-10-20 14:30:28 -07001640 if ((level = brightness_get(NULL)) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001641 len += sprintf(p + len, "level:\t\tunreadable\n");
1642 } else {
1643 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1644 len += sprintf(p + len, "commands:\tup, down\n");
1645 len += sprintf(p + len, "commands:\tlevel <level>"
1646 " (<level> is 0-7)\n");
1647 }
1648
1649 return len;
1650}
1651
1652#define BRIGHTNESS_UP 4
1653#define BRIGHTNESS_DOWN 5
1654
Holger Macht8acb0252006-10-20 14:30:28 -07001655static int brightness_set(int value)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001656{
1657 int cmos_cmd, inc, i;
Holger Macht8acb0252006-10-20 14:30:28 -07001658 int current_value = brightness_get(NULL);
1659
1660 value &= 7;
1661
1662 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1663 inc = value > current_value ? 1 : -1;
1664 for (i = current_value; i != value; i += inc) {
1665 if (!cmos_eval(cmos_cmd))
1666 return -EIO;
1667 if (!acpi_ec_write(brightness_offset, i + inc))
1668 return -EIO;
1669 }
1670
1671 return 0;
1672}
1673
1674static int brightness_write(char *buf)
1675{
1676 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001677 int new_level;
1678 char *cmd;
1679
1680 while ((cmd = next_cmd(&buf))) {
Holger Macht8acb0252006-10-20 14:30:28 -07001681 if ((level = brightness_get(NULL)) < 0)
1682 return level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001683 level &= 7;
1684
1685 if (strlencmp(cmd, "up") == 0) {
1686 new_level = level == 7 ? 7 : level + 1;
1687 } else if (strlencmp(cmd, "down") == 0) {
1688 new_level = level == 0 ? 0 : level - 1;
1689 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1690 new_level >= 0 && new_level <= 7) {
1691 /* new_level set */
1692 } else
1693 return -EINVAL;
1694
Holger Macht8acb0252006-10-20 14:30:28 -07001695 brightness_set(new_level);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001696 }
1697
1698 return 0;
1699}
1700
Holger Macht8acb0252006-10-20 14:30:28 -07001701static int brightness_update_status(struct backlight_device *bd)
1702{
1703 return brightness_set(bd->props->brightness);
1704}
1705
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001706static int volume_offset = 0x30;
1707
1708static int volume_read(char *p)
1709{
1710 int len = 0;
1711 u8 level;
1712
1713 if (!acpi_ec_read(volume_offset, &level)) {
1714 len += sprintf(p + len, "level:\t\tunreadable\n");
1715 } else {
1716 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1717 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1718 len += sprintf(p + len, "commands:\tup, down, mute\n");
1719 len += sprintf(p + len, "commands:\tlevel <level>"
1720 " (<level> is 0-15)\n");
1721 }
1722
1723 return len;
1724}
1725
1726#define VOLUME_DOWN 0
1727#define VOLUME_UP 1
1728#define VOLUME_MUTE 2
1729
1730static int volume_write(char *buf)
1731{
1732 int cmos_cmd, inc, i;
1733 u8 level, mute;
1734 int new_level, new_mute;
1735 char *cmd;
1736
1737 while ((cmd = next_cmd(&buf))) {
1738 if (!acpi_ec_read(volume_offset, &level))
1739 return -EIO;
1740 new_mute = mute = level & 0x40;
1741 new_level = level = level & 0xf;
1742
1743 if (strlencmp(cmd, "up") == 0) {
1744 if (mute)
1745 new_mute = 0;
1746 else
1747 new_level = level == 15 ? 15 : level + 1;
1748 } else if (strlencmp(cmd, "down") == 0) {
1749 if (mute)
1750 new_mute = 0;
1751 else
1752 new_level = level == 0 ? 0 : level - 1;
1753 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1754 new_level >= 0 && new_level <= 15) {
1755 /* new_level set */
1756 } else if (strlencmp(cmd, "mute") == 0) {
1757 new_mute = 0x40;
1758 } else
1759 return -EINVAL;
1760
1761 if (new_level != level) { /* mute doesn't change */
1762 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1763 inc = new_level > level ? 1 : -1;
1764
1765 if (mute && (!cmos_eval(cmos_cmd) ||
1766 !acpi_ec_write(volume_offset, level)))
1767 return -EIO;
1768
1769 for (i = level; i != new_level; i += inc)
1770 if (!cmos_eval(cmos_cmd) ||
1771 !acpi_ec_write(volume_offset, i + inc))
1772 return -EIO;
1773
1774 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1775 !acpi_ec_write(volume_offset,
1776 new_level + mute)))
1777 return -EIO;
1778 }
1779
1780 if (new_mute != mute) { /* level doesn't change */
1781 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1782
1783 if (!cmos_eval(cmos_cmd) ||
1784 !acpi_ec_write(volume_offset, level + new_mute))
1785 return -EIO;
1786 }
1787 }
1788
1789 return 0;
1790}
1791
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001792static enum fan_status_access_mode fan_status_access_mode;
1793static enum fan_control_access_mode fan_control_access_mode;
1794static enum fan_control_commands fan_control_commands;
1795
1796static int fan_init(void)
1797{
1798 u8 status;
1799
1800 fan_status_access_mode = IBMACPI_FAN_NONE;
1801 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1802 fan_control_commands = 0;
1803
1804 if (gfan_handle) {
1805 /* 570, 600e/x, 770e, 770x */
1806 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1807 } else {
1808 /* all other ThinkPads: note that even old-style
1809 * ThinkPad ECs supports the fan control register */
1810 if (likely(acpi_ec_read(fan_status_offset, &status))) {
1811 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1812 } else {
1813 printk(IBM_ERR
1814 "ThinkPad ACPI EC access misbehaving, "
1815 "fan status and control unavailable\n");
1816 return 0;
1817 }
1818 }
1819
1820 if (sfan_handle) {
1821 /* 570, 770x-JL */
1822 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
1823 fan_control_commands |= IBMACPI_FAN_CMD_LEVEL;
1824 } else {
1825 if (!gfan_handle) {
1826 /* gfan without sfan means no fan control */
1827 /* all other models implement TP EC 0x2f control */
1828
1829 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001830 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001831 fan_control_access_mode =
1832 IBMACPI_FAN_WR_ACPI_FANS;
1833 fan_control_commands |=
1834 IBMACPI_FAN_CMD_SPEED |
1835 IBMACPI_FAN_CMD_ENABLE;
1836 } else {
1837 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
1838 fan_control_commands |= IBMACPI_FAN_CMD_ENABLE;
1839 }
1840 }
1841 }
1842
1843 return 0;
1844}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001845
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001846static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001847{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001848 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001849
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001850 /* TODO:
1851 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1852
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001853 switch (fan_status_access_mode) {
1854 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001855 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001856
1857 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001858 return -EIO;
1859
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001860 if (likely(status))
1861 *status = s & 0x07;
1862
1863 break;
1864
1865 case IBMACPI_FAN_RD_TPEC:
1866 /* all except 570, 600e/x, 770e, 770x */
1867 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1868 return -EIO;
1869
1870 if (likely(status))
1871 *status = s;
1872
1873 break;
1874
1875 default:
1876 return -ENXIO;
1877 }
1878
1879 return 0;
1880}
1881
1882static int fan_get_speed(unsigned int *speed)
1883{
1884 u8 hi, lo;
1885
1886 switch (fan_status_access_mode) {
1887 case IBMACPI_FAN_RD_TPEC:
1888 /* all except 570, 600e/x, 770e, 770x */
1889 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1890 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1891 return -EIO;
1892
1893 if (likely(speed))
1894 *speed = (hi << 8) | lo;
1895
1896 break;
1897
1898 default:
1899 return -ENXIO;
1900 }
1901
1902 return 0;
1903}
1904
1905static int fan_read(char *p)
1906{
1907 int len = 0;
1908 int rc;
1909 u8 status;
1910 unsigned int speed = 0;
1911
1912 switch (fan_status_access_mode) {
1913 case IBMACPI_FAN_RD_ACPI_GFAN:
1914 /* 570, 600e/x, 770e, 770x */
1915 if ((rc = fan_get_status(&status)) < 0)
1916 return rc;
1917
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001918 len += sprintf(p + len, "status:\t\t%s\n"
1919 "level:\t\t%d\n",
1920 (status != 0) ? "enabled" : "disabled", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001921 break;
1922
1923 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001924 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001925 if ((rc = fan_get_status(&status)) < 0)
1926 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001927
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001928 len += sprintf(p + len, "status:\t\t%s\n",
1929 (status != 0) ? "enabled" : "disabled");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001930
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001931 if ((rc = fan_get_speed(&speed)) < 0)
1932 return rc;
1933
1934 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001935
1936 if (status & IBMACPI_FAN_EC_DISENGAGED)
1937 /* Disengaged mode takes precedence */
1938 len += sprintf(p + len, "level:\t\tdisengaged\n");
1939 else if (status & IBMACPI_FAN_EC_AUTO)
1940 len += sprintf(p + len, "level:\t\tauto\n");
1941 else
1942 len += sprintf(p + len, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001943 break;
1944
1945 case IBMACPI_FAN_NONE:
1946 default:
1947 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001948 }
1949
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001950 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001951 len += sprintf(p + len, "commands:\tlevel <level>"
1952 " (<level> is 0-7)\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001953
1954 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001955 len += sprintf(p + len, "commands:\tenable, disable\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001956
1957 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001958 len += sprintf(p + len, "commands:\tspeed <speed>"
1959 " (<speed> is 0-65535)\n");
1960
1961 return len;
1962}
1963
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02001964static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001965{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02001966 switch (fan_control_access_mode) {
1967 case IBMACPI_FAN_WR_ACPI_SFAN:
1968 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001969 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
1970 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02001971 } else
1972 return -EINVAL;
1973 break;
1974
1975 default:
1976 return -ENXIO;
1977 }
1978 return 0;
1979}
1980
1981static int fan_set_enable(void)
1982{
1983 switch (fan_control_access_mode) {
1984 case IBMACPI_FAN_WR_ACPI_FANS:
1985 case IBMACPI_FAN_WR_TPEC:
1986 if (!acpi_ec_write(fan_status_offset, 0x80))
1987 return -EIO;
1988 break;
1989
1990 default:
1991 return -ENXIO;
1992 }
1993 return 0;
1994}
1995
1996static int fan_set_disable(void)
1997{
1998 switch (fan_control_access_mode) {
1999 case IBMACPI_FAN_WR_ACPI_FANS:
2000 case IBMACPI_FAN_WR_TPEC:
2001 if (!acpi_ec_write(fan_status_offset, 0x00))
2002 return -EIO;
2003 break;
2004
2005 default:
2006 return -ENXIO;
2007 }
2008 return 0;
2009}
2010
2011static int fan_set_speed(int speed)
2012{
2013 switch (fan_control_access_mode) {
2014 case IBMACPI_FAN_WR_ACPI_FANS:
2015 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002016 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2017 speed, speed, speed))
2018 return -EIO;
2019 } else
2020 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002021 break;
2022
2023 default:
2024 return -ENXIO;
2025 }
2026 return 0;
2027}
2028
2029static int fan_write_cmd_level(const char *cmd, int *rc)
2030{
2031 int level;
2032
2033 if (sscanf(cmd, "level %d", &level) != 1)
2034 return 0;
2035
2036 if ((*rc = fan_set_level(level)) == -ENXIO)
2037 printk(IBM_ERR "level command accepted for unsupported "
2038 "access mode %d", fan_control_access_mode);
2039
2040 return 1;
2041}
2042
2043static int fan_write_cmd_enable(const char *cmd, int *rc)
2044{
2045 if (strlencmp(cmd, "enable") != 0)
2046 return 0;
2047
2048 if ((*rc = fan_set_enable()) == -ENXIO)
2049 printk(IBM_ERR "enable command accepted for unsupported "
2050 "access mode %d", fan_control_access_mode);
2051
2052 return 1;
2053}
2054
2055static int fan_write_cmd_disable(const char *cmd, int *rc)
2056{
2057 if (strlencmp(cmd, "disable") != 0)
2058 return 0;
2059
2060 if ((*rc = fan_set_disable()) == -ENXIO)
2061 printk(IBM_ERR "disable command accepted for unsupported "
2062 "access mode %d", fan_control_access_mode);
2063
2064 return 1;
2065}
2066
2067static int fan_write_cmd_speed(const char *cmd, int *rc)
2068{
2069 int speed;
2070
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002071 /* TODO:
2072 * Support speed <low> <medium> <high> ? */
2073
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002074 if (sscanf(cmd, "speed %d", &speed) != 1)
2075 return 0;
2076
2077 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2078 printk(IBM_ERR "speed command accepted for unsupported "
2079 "access mode %d", fan_control_access_mode);
2080
2081 return 1;
2082}
2083
2084static int fan_write(char *buf)
2085{
2086 char *cmd;
2087 int rc = 0;
2088
2089 while (!rc && (cmd = next_cmd(&buf))) {
2090 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2091 fan_write_cmd_level(cmd, &rc)) &&
2092 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2093 (fan_write_cmd_enable(cmd, &rc) ||
2094 fan_write_cmd_disable(cmd, &rc))) &&
2095 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2096 fan_write_cmd_speed(cmd, &rc))
2097 )
2098 rc = -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002099 }
2100
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002101 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002102}
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104static struct ibm_struct ibms[] = {
2105 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002106 .name = "driver",
2107 .init = driver_init,
2108 .read = driver_read,
2109 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002111 .name = "hotkey",
2112 .hid = IBM_HKEY_HID,
2113 .init = hotkey_init,
2114 .read = hotkey_read,
2115 .write = hotkey_write,
2116 .exit = hotkey_exit,
2117 .notify = hotkey_notify,
2118 .handle = &hkey_handle,
2119 .type = ACPI_DEVICE_NOTIFY,
2120 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002122 .name = "bluetooth",
2123 .init = bluetooth_init,
2124 .read = bluetooth_read,
2125 .write = bluetooth_write,
2126 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002128 .name = "wan",
2129 .init = wan_init,
2130 .read = wan_read,
2131 .write = wan_write,
2132 .experimental = 1,
2133 },
2134 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002135 .name = "video",
2136 .init = video_init,
2137 .read = video_read,
2138 .write = video_write,
2139 .exit = video_exit,
2140 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002142 .name = "light",
2143 .init = light_init,
2144 .read = light_read,
2145 .write = light_write,
2146 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002147#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002149 .name = "dock",
2150 .read = dock_read,
2151 .write = dock_write,
2152 .notify = dock_notify,
2153 .handle = &dock_handle,
2154 .type = ACPI_SYSTEM_NOTIFY,
2155 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002157 .name = "dock",
2158 .hid = IBM_PCI_HID,
2159 .notify = dock_notify,
2160 .handle = &pci_handle,
2161 .type = ACPI_SYSTEM_NOTIFY,
2162 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002165 .name = "bay",
2166 .init = bay_init,
2167 .read = bay_read,
2168 .write = bay_write,
2169 .notify = bay_notify,
2170 .handle = &bay_handle,
2171 .type = ACPI_SYSTEM_NOTIFY,
2172 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002174 .name = "cmos",
2175 .read = cmos_read,
2176 .write = cmos_write,
2177 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002179 .name = "led",
2180 .init = led_init,
2181 .read = led_read,
2182 .write = led_write,
2183 },
2184 {
2185 .name = "beep",
2186 .read = beep_read,
2187 .write = beep_write,
2188 },
2189 {
2190 .name = "thermal",
2191 .init = thermal_init,
2192 .read = thermal_read,
2193 },
2194 {
2195 .name = "ecdump",
2196 .read = ecdump_read,
2197 .write = ecdump_write,
2198 .experimental = 1,
2199 },
2200 {
2201 .name = "brightness",
2202 .read = brightness_read,
2203 .write = brightness_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002204 },
2205 {
2206 .name = "volume",
2207 .read = volume_read,
2208 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002209 },
2210 {
2211 .name = "fan",
2212 .read = fan_read,
2213 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002214 .init = fan_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002215 .experimental = 1,
2216 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219static int dispatch_read(char *page, char **start, off_t off, int count,
2220 int *eof, void *data)
2221{
2222 struct ibm_struct *ibm = (struct ibm_struct *)data;
2223 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (!ibm || !ibm->read)
2226 return -EINVAL;
2227
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002228 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (len < 0)
2230 return len;
2231
2232 if (len <= off + count)
2233 *eof = 1;
2234 *start = page + off;
2235 len -= off;
2236 if (len > count)
2237 len = count;
2238 if (len < 0)
2239 len = 0;
2240
2241 return len;
2242}
2243
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002244static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 unsigned long count, void *data)
2246{
2247 struct ibm_struct *ibm = (struct ibm_struct *)data;
2248 char *kernbuf;
2249 int ret;
2250
2251 if (!ibm || !ibm->write)
2252 return -EINVAL;
2253
2254 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2255 if (!kernbuf)
2256 return -ENOMEM;
2257
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002258 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002260 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262
2263 kernbuf[count] = 0;
2264 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002265 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (ret == 0)
2267 ret = count;
2268
2269 kfree(kernbuf);
2270
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002271 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
2274static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2275{
2276 struct ibm_struct *ibm = (struct ibm_struct *)data;
2277
2278 if (!ibm || !ibm->notify)
2279 return;
2280
2281 ibm->notify(ibm, event);
2282}
2283
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002284static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
2286 acpi_status status;
2287 int ret;
2288
2289 if (!*ibm->handle)
2290 return 0;
2291
2292 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2293 if (ret < 0) {
2294 printk(IBM_ERR "%s device not present\n", ibm->name);
2295 return 0;
2296 }
2297
2298 acpi_driver_data(ibm->device) = ibm;
2299 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2300
2301 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2302 dispatch_notify, ibm);
2303 if (ACPI_FAILURE(status)) {
2304 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2305 ibm->name, status);
2306 return -ENODEV;
2307 }
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
2310}
2311
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002312static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 return 0;
2315}
2316
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002317static int __init register_driver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 int ret;
2320
2321 ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2322 if (!ibm->driver) {
2323 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2324 return -1;
2325 }
2326
2327 memset(ibm->driver, 0, sizeof(struct acpi_driver));
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002328 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002330 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 ret = acpi_bus_register_driver(ibm->driver);
2333 if (ret < 0) {
2334 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2335 ibm->hid, ret);
2336 kfree(ibm->driver);
2337 }
2338
2339 return ret;
2340}
2341
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002342static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 int ret;
2345 struct proc_dir_entry *entry;
2346
2347 if (ibm->experimental && !experimental)
2348 return 0;
2349
2350 if (ibm->hid) {
2351 ret = register_driver(ibm);
2352 if (ret < 0)
2353 return ret;
2354 ibm->driver_registered = 1;
2355 }
2356
2357 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002358 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (ret != 0)
2360 return ret;
2361 ibm->init_called = 1;
2362 }
2363
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002364 if (ibm->read) {
2365 entry = create_proc_entry(ibm->name,
2366 S_IFREG | S_IRUGO | S_IWUSR,
2367 proc_dir);
2368 if (!entry) {
2369 printk(IBM_ERR "unable to create proc entry %s\n",
2370 ibm->name);
2371 return -ENODEV;
2372 }
2373 entry->owner = THIS_MODULE;
2374 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002376 if (ibm->write)
2377 entry->write_proc = &dispatch_write;
2378 ibm->proc_created = 1;
2379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 if (ibm->notify) {
2382 ret = setup_notify(ibm);
2383 if (ret < 0)
2384 return ret;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002385 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
2388 return 0;
2389}
2390
2391static void ibm_exit(struct ibm_struct *ibm)
2392{
2393 if (ibm->notify_installed)
2394 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2395 dispatch_notify);
2396
2397 if (ibm->proc_created)
2398 remove_proc_entry(ibm->name, proc_dir);
2399
2400 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002401 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 if (ibm->driver_registered) {
2404 acpi_bus_unregister_driver(ibm->driver);
2405 kfree(ibm->driver);
2406 }
2407}
2408
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002409static void __init ibm_handle_init(char *name,
2410 acpi_handle * handle, acpi_handle parent,
2411 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
2413 int i;
2414 acpi_status status;
2415
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002416 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002418 if (ACPI_SUCCESS(status)) {
2419 *path = paths[i];
2420 return;
2421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002427#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002429 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431static int set_ibm_param(const char *val, struct kernel_param *kp)
2432{
2433 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002435 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2436 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2437 if (strlen(val) > sizeof(ibms[i].param) - 2)
2438 return -ENOSPC;
2439 strcpy(ibms[i].param, val);
2440 strcat(ibms[i].param, ",");
2441 return 0;
2442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return -EINVAL;
2445}
2446
2447#define IBM_PARAM(feature) \
2448 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2449
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002450IBM_PARAM(hotkey);
2451IBM_PARAM(bluetooth);
2452IBM_PARAM(video);
2453IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002454#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002455IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002456#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002457IBM_PARAM(bay);
2458IBM_PARAM(cmos);
2459IBM_PARAM(led);
2460IBM_PARAM(beep);
2461IBM_PARAM(ecdump);
2462IBM_PARAM(brightness);
2463IBM_PARAM(volume);
2464IBM_PARAM(fan);
2465
Holger Macht8acb0252006-10-20 14:30:28 -07002466static struct backlight_properties ibm_backlight_data = {
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02002467 .owner = THIS_MODULE,
2468 .get_brightness = brightness_get,
2469 .update_status = brightness_update_status,
2470 .max_brightness = 7,
Holger Macht8acb0252006-10-20 14:30:28 -07002471};
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473static void acpi_ibm_exit(void)
2474{
2475 int i;
2476
Holger Macht8acb0252006-10-20 14:30:28 -07002477 if (ibm_backlight_device)
2478 backlight_device_unregister(ibm_backlight_device);
2479
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002480 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 ibm_exit(&ibms[i]);
2482
2483 remove_proc_entry(IBM_DIR, acpi_root_dir);
2484}
2485
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002486static int __init check_dmi_for_ec(void)
2487{
2488 struct dmi_device *dev = NULL;
2489
2490 /*
2491 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2492 * X32 or newer, all Z series; Some models must have an
2493 * up-to-date BIOS or they will not be detected.
2494 *
2495 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2496 */
2497 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2498 if (strstr(dev->name, "IBM ThinkPad Embedded Controller"))
2499 return 1;
2500 }
2501 return 0;
2502}
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504static int __init acpi_ibm_init(void)
2505{
2506 int ret, i;
2507
2508 if (acpi_disabled)
2509 return -ENODEV;
2510
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002511 if (!acpi_specific_hotkey_enabled) {
2512 printk(IBM_ERR "using generic hotkey driver\n");
2513 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -05002514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002516 /* ec is required because many other handles are relative to it */
2517 IBM_HANDLE_INIT(ec);
2518 if (!ec_handle) {
2519 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002523 /* Models with newer firmware report the EC in DMI */
2524 ibm_thinkpad_ec_found = check_dmi_for_ec();
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002527 IBM_HANDLE_INIT(vid);
2528 IBM_HANDLE_INIT(vid2);
2529 IBM_HANDLE_INIT(ledb);
2530 IBM_HANDLE_INIT(led);
2531 IBM_HANDLE_INIT(hkey);
2532 IBM_HANDLE_INIT(lght);
2533 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002534#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002535 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002536#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002537 IBM_HANDLE_INIT(pci);
2538 IBM_HANDLE_INIT(bay);
2539 if (bay_handle)
2540 IBM_HANDLE_INIT(bay_ej);
2541 IBM_HANDLE_INIT(bay2);
2542 if (bay2_handle)
2543 IBM_HANDLE_INIT(bay2_ej);
2544 IBM_HANDLE_INIT(beep);
2545 IBM_HANDLE_INIT(ecrd);
2546 IBM_HANDLE_INIT(ecwr);
2547 IBM_HANDLE_INIT(fans);
2548 IBM_HANDLE_INIT(gfan);
2549 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2552 if (!proc_dir) {
2553 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2554 return -ENODEV;
2555 }
2556 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002557
2558 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002560 if (ret >= 0 && *ibms[i].param)
2561 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if (ret < 0) {
2563 acpi_ibm_exit();
2564 return ret;
2565 }
2566 }
2567
Holger Macht8acb0252006-10-20 14:30:28 -07002568 ibm_backlight_device = backlight_device_register("ibm", NULL,
2569 &ibm_backlight_data);
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02002570 if (IS_ERR(ibm_backlight_device)) {
Holger Macht8acb0252006-10-20 14:30:28 -07002571 printk(IBM_ERR "Could not register ibm backlight device\n");
2572 ibm_backlight_device = NULL;
2573 acpi_ibm_exit();
2574 }
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return 0;
2577}
2578
2579module_init(acpi_ibm_init);
2580module_exit(acpi_ibm_exit);