blob: 7c1b418676b9b7ab20d62f32c804a96a88781cf3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ibm_acpi.c - IBM ThinkPad ACPI Extras
3 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -02006 * Copyright (C) 2006 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Borislav Deianov78f81cc2005-08-17 00:00:00 -040021 */
22
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020023#define IBM_VERSION "0.13"
Borislav Deianov78f81cc2005-08-17 00:00:00 -040024
25/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Changelog:
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020027 *
28 * 2006-11-22 0.13 new maintainer
29 * changelog now lives in git commit history, and will
30 * not be updated further in-file.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040031 *
32 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
33 * 2005-03-17 0.11 support for 600e, 770x
34 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
35 * support for 770e, G41
36 * G40 and G41 don't have a thinklight
37 * temperatures no longer experimental
38 * experimental brightness control
39 * experimental volume control
40 * experimental fan enable/disable
41 * 2005-01-16 0.10 fix module loading on R30, R31
42 * 2005-01-16 0.9 support for 570, R30, R31
43 * ultrabay support on A22p, A3x
44 * limit arg for cmos, led, beep, drop experimental status
45 * more capable led control on A21e, A22p, T20-22, X20
46 * experimental temperatures and fan speed
47 * experimental embedded controller register dump
48 * mark more functions as __init, drop incorrect __exit
49 * use MODULE_VERSION
50 * thanks to Henrik Brix Andersen <brix@gentoo.org>
51 * fix parameter passing on module loading
52 * thanks to Rusty Russell <rusty@rustcorp.com.au>
53 * thanks to Jim Radford <radford@blackbean.org>
54 * 2004-11-08 0.8 fix init error case, don't return from a macro
55 * thanks to Chris Wright <chrisw@osdl.org>
56 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
57 * fix led control on A21e
58 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
60 * proc file format changed
61 * video_switch command
62 * experimental cmos control
63 * experimental led control
64 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040065 * 2004-09-16 0.4 support for module parameters
66 * hotkey mask can be prefixed by 0x
67 * video output switching
68 * video expansion control
69 * ultrabay eject support
70 * removed lcd brightness/on/off control, didn't work
71 * 2004-08-17 0.3 support for R40
72 * lcd off, brightness control
73 * thinklight on/off
74 * 2004-08-14 0.2 support for T series, X20
75 * bluetooth enable/disable
76 * hotkey events disabled by default
77 * removed fan control, currently useless
78 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/kernel.h>
82#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/types.h>
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020085#include <linux/string.h>
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -020086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/proc_fs.h>
Holger Macht8acb0252006-10-20 14:30:28 -070088#include <linux/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <asm/uaccess.h>
90
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020091#include <linux/dmi.h>
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -020092#include <linux/jiffies.h>
93#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95#include <acpi/acpi_drivers.h>
96#include <acpi/acnamesp.h>
97
98#define IBM_NAME "ibm"
99#define IBM_DESC "IBM ThinkPad ACPI Extras"
100#define IBM_FILE "ibm_acpi"
101#define IBM_URL "http://ibm-acpi.sf.net/"
102
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -0200103MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400104MODULE_DESCRIPTION(IBM_DESC);
105MODULE_VERSION(IBM_VERSION);
106MODULE_LICENSE("GPL");
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define IBM_DIR IBM_NAME
109
110#define IBM_LOG IBM_FILE ": "
111#define IBM_ERR KERN_ERR IBM_LOG
112#define IBM_NOTICE KERN_NOTICE IBM_LOG
113#define IBM_INFO KERN_INFO IBM_LOG
114#define IBM_DEBUG KERN_DEBUG IBM_LOG
115
116#define IBM_MAX_ACPI_ARGS 3
117
118#define __unused __attribute__ ((unused))
119
120static int experimental;
121module_param(experimental, int, 0);
122
123static acpi_handle root_handle = NULL;
124
125#define IBM_HANDLE(object, parent, paths...) \
126 static acpi_handle object##_handle; \
127 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400128 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 static char *object##_paths[] = { paths }
130
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400131IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
132 "\\_SB.PCI.ISA.EC", /* 570 */
133 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
134 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
135 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
136 "\\_SB.PCI0.ICH3.EC0", /* R31 */
137 "\\_SB.PCI0.LPC.EC", /* all others */
138 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400140IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
141 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
142 "\\_SB.PCI0.VID0", /* 770e */
143 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
144 "\\_SB.PCI0.AGP.VID", /* all others */
145 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400147IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400149IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
150 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
151 "\\CMS", /* R40, R40e */
152 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800153#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400154IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
155 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
156 "\\_SB.PCI0.PCI1.DOCK", /* all others */
157 "\\_SB.PCI.ISA.SLCE", /* 570 */
158 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800159#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400160IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
161 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhe0298992006-11-25 16:35:09 -0200162 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400163 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
164 ); /* A21e, R30, R31 */
165
166IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
167 "_EJ0", /* all others */
168 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
169
170IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
171 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
172 ); /* all others */
173
174IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
175 "_EJ0", /* 770x */
176 ); /* all others */
177
178/* don't list other alternatives as we install a notify handler on the 570 */
179IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
180
181IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
182 "^HKEY", /* R30, R31 */
183 "HKEY", /* all others */
184 ); /* 570 */
185
186IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
187IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
188
189IBM_HANDLE(led, ec, "SLED", /* 570 */
190 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
191 "LED", /* all others */
192 ); /* R30, R31 */
193
194IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
195IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
196IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200197IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400198
199IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
200 "\\FSPD", /* 600e/x, 770e, 770x */
201 ); /* all others */
202
203IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
204 "JFNS", /* 770x-JL */
205 ); /* all others */
206
207#define IBM_HKEY_HID "IBM0068"
208#define IBM_PCI_HID "PNP0A03"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200210enum thermal_access_mode {
211 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
212 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
213 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200214 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
215 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200216};
217
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200218#define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200219struct ibm_thermal_sensors_struct {
220 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
221};
222
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200223/*
224 * FAN ACCESS MODES
225 *
226 * IBMACPI_FAN_RD_ACPI_GFAN:
227 * ACPI GFAN method: returns fan level
228 *
229 * see IBMACPI_FAN_WR_ACPI_SFAN
230 * EC 0x2f not available if GFAN exists
231 *
232 * IBMACPI_FAN_WR_ACPI_SFAN:
233 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
234 *
235 * EC 0x2f might be available *for reading*, but never for writing.
236 *
237 * IBMACPI_FAN_WR_TPEC:
238 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
239 * on almost all ThinkPads
240 *
241 * Fan speed changes of any sort (including those caused by the
242 * disengaged mode) are usually done slowly by the firmware as the
243 * maximum ammount of fan duty cycle change per second seems to be
244 * limited.
245 *
246 * Reading is not available if GFAN exists.
247 * Writing is not available if SFAN exists.
248 *
249 * Bits
250 * 7 automatic mode engaged;
251 * (default operation mode of the ThinkPad)
252 * fan level is ignored in this mode.
253 * 6 disengage mode (takes precedence over bit 7);
254 * not available on all thinkpads. May disable
255 * the tachometer, and speeds up fan to 100% duty-cycle,
256 * which speeds it up far above the standard RPM
257 * levels. It is not impossible that it could cause
258 * hardware damage.
259 * 5-3 unused in some models. Extra bits for fan level
260 * in others, but still useless as all values above
261 * 7 map to the same speed as level 7 in these models.
262 * 2-0 fan level (0..7 usually)
263 * 0x00 = stop
264 * 0x07 = max (set when temperatures critical)
265 * Some ThinkPads may have other levels, see
266 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
267 *
268 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
269 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
270 * does so, its initial value is meaningless (0x07).
271 *
272 * For firmware bugs, refer to:
273 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
274 *
275 * ----
276 *
277 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
278 * Main fan tachometer reading (in RPM)
279 *
280 * This register is present on all ThinkPads with a new-style EC, and
281 * it is known not to be present on the A21m/e, and T22, as there is
282 * something else in offset 0x84 according to the ACPI DSDT. Other
283 * ThinkPads from this same time period (and earlier) probably lack the
284 * tachometer as well.
285 *
286 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
287 * was never fixed by IBM to report the EC firmware version string
288 * probably support the tachometer (like the early X models), so
289 * detecting it is quite hard. We need more data to know for sure.
290 *
291 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
292 * might result.
293 *
294 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
295 * register is not invalidated in ThinkPads that disable tachometer
296 * readings. Thus, the tachometer readings go stale.
297 *
298 * For firmware bugs, refer to:
299 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
300 *
301 * IBMACPI_FAN_WR_ACPI_FANS:
302 * ThinkPad X31, X40, X41. Not available in the X60.
303 *
304 * FANS ACPI handle: takes three arguments: low speed, medium speed,
305 * high speed. ACPI DSDT seems to map these three speeds to levels
306 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
307 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
308 *
309 * The speeds are stored on handles
310 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
311 *
312 * There are three default speed sets, acessible as handles:
313 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
314 *
315 * ACPI DSDT switches which set is in use depending on various
316 * factors.
317 *
318 * IBMACPI_FAN_WR_TPEC is also available and should be used to
319 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
320 * but the ACPI tables just mention level 7.
321 */
322
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200323enum fan_status_access_mode {
324 IBMACPI_FAN_NONE = 0, /* No fan status or control */
325 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
326 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
327};
328
329enum fan_control_access_mode {
330 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
331 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
332 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
333 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
334};
335
336enum fan_control_commands {
337 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
338 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -0200339 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
340 * and also watchdog cmd */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200341};
342
343enum { /* Fan control constants */
344 fan_status_offset = 0x2f, /* EC register 0x2f */
345 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
346 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -0200347
348 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
349 * disengaged */
350 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
351 * control */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200352};
353
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -0200354static char *ibm_thinkpad_ec_found = NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356struct ibm_struct {
357 char *name;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400358 char param[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 char *hid;
361 struct acpi_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400363 int (*init) (void);
364 int (*read) (char *);
365 int (*write) (char *);
366 void (*exit) (void);
367
368 void (*notify) (struct ibm_struct *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 acpi_handle *handle;
370 int type;
371 struct acpi_device *device;
372
373 int driver_registered;
374 int proc_created;
375 int init_called;
376 int notify_installed;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int experimental;
379};
380
381static struct proc_dir_entry *proc_dir = NULL;
382
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -0200383static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
386#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
387#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
388
389static int acpi_evalf(acpi_handle handle,
390 void *res, char *method, char *fmt, ...)
391{
392 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400393 struct acpi_object_list params;
394 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
395 struct acpi_buffer result, *resultp;
396 union acpi_object out_obj;
397 acpi_status status;
398 va_list ap;
399 char res_type;
400 int success;
401 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (!*fmt) {
404 printk(IBM_ERR "acpi_evalf() called with empty format\n");
405 return 0;
406 }
407
408 if (*fmt == 'q') {
409 quiet = 1;
410 fmt++;
411 } else
412 quiet = 0;
413
414 res_type = *(fmt++);
415
416 params.count = 0;
417 params.pointer = &in_objs[0];
418
419 va_start(ap, fmt);
420 while (*fmt) {
421 char c = *(fmt++);
422 switch (c) {
423 case 'd': /* int */
424 in_objs[params.count].integer.value = va_arg(ap, int);
425 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
426 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400427 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 default:
429 printk(IBM_ERR "acpi_evalf() called "
430 "with invalid format character '%c'\n", c);
431 return 0;
432 }
433 }
434 va_end(ap);
435
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400436 if (res_type != 'v') {
437 result.length = sizeof(out_obj);
438 result.pointer = &out_obj;
439 resultp = &result;
440 } else
441 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400443 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400446 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (res)
448 *(int *)res = out_obj.integer.value;
449 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
450 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400451 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 success = status == AE_OK;
453 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400454 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 default:
456 printk(IBM_ERR "acpi_evalf() called "
457 "with invalid format character '%c'\n", res_type);
458 return 0;
459 }
460
461 if (!success && !quiet)
462 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
463 method, fmt0, status);
464
465 return success;
466}
467
468static void __unused acpi_print_int(acpi_handle handle, char *method)
469{
470 int i;
471
472 if (acpi_evalf(handle, &i, method, "d"))
473 printk(IBM_INFO "%s = 0x%x\n", method, i);
474 else
475 printk(IBM_ERR "error calling %s\n", method);
476}
477
478static char *next_cmd(char **cmds)
479{
480 char *start = *cmds;
481 char *end;
482
483 while ((end = strchr(start, ',')) && end == start)
484 start = end + 1;
485
486 if (!end)
487 return NULL;
488
489 *end = 0;
490 *cmds = end + 1;
491 return start;
492}
493
Adrian Bunk1f217822006-12-19 13:01:28 -0800494static int ibm_acpi_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
497 printk(IBM_INFO "%s\n", IBM_URL);
498
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200499 if (ibm_thinkpad_ec_found)
500 printk(IBM_INFO "ThinkPad EC firmware %s\n",
501 ibm_thinkpad_ec_found);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 0;
504}
505
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400506static int driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 int len = 0;
509
510 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
511 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
512
513 return len;
514}
515
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400516static int hotkey_supported;
517static int hotkey_mask_supported;
518static int hotkey_orig_status;
519static int hotkey_orig_mask;
520
521static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400524 return 0;
525
526 if (hotkey_mask_supported)
527 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
528 return 0;
529
530 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400533static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 int i;
536
537 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540 if (hotkey_mask_supported)
541 for (i = 0; i < 32; i++) {
542 int bit = ((1 << i) & mask) != 0;
543 if (!acpi_evalf(hkey_handle,
544 NULL, "MHKM", "vdd", i + 1, bit))
545 return 0;
546 }
547
548 return 1;
549}
550
551static int hotkey_init(void)
552{
553 /* hotkey not supported on 570 */
554 hotkey_supported = hkey_handle != NULL;
555
556 if (hotkey_supported) {
557 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
558 A30, R30, R31, T20-22, X20-21, X22-24 */
559 hotkey_mask_supported =
560 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
561
562 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
563 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 return 0;
567}
568
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400569static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 int status, mask;
572 int len = 0;
573
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400574 if (!hotkey_supported) {
575 len += sprintf(p + len, "status:\t\tnot supported\n");
576 return len;
577 }
578
579 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -EIO;
581
582 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400583 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
585 len += sprintf(p + len,
586 "commands:\tenable, disable, reset, <mask>\n");
587 } else {
588 len += sprintf(p + len, "mask:\t\tnot supported\n");
589 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
590 }
591
592 return len;
593}
594
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400595static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 int status, mask;
598 char *cmd;
599 int do_cmd = 0;
600
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400601 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -ENODEV;
603
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400604 if (!hotkey_get(&status, &mask))
605 return -EIO;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 while ((cmd = next_cmd(&buf))) {
608 if (strlencmp(cmd, "enable") == 0) {
609 status = 1;
610 } else if (strlencmp(cmd, "disable") == 0) {
611 status = 0;
612 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400613 status = hotkey_orig_status;
614 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
616 /* mask set */
617 } else if (sscanf(cmd, "%x", &mask) == 1) {
618 /* mask set */
619 } else
620 return -EINVAL;
621 do_cmd = 1;
622 }
623
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400624 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EIO;
626
627 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400628}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400630static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400632 if (hotkey_supported)
633 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636static void hotkey_notify(struct ibm_struct *ibm, u32 event)
637{
638 int hkey;
639
640 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
641 acpi_bus_generate_event(ibm->device, event, hkey);
642 else {
643 printk(IBM_ERR "unknown hotkey event %d\n", event);
644 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400648static int bluetooth_supported;
649
650static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400652 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
653 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
654 bluetooth_supported = hkey_handle &&
655 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 return 0;
658}
659
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400660static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 int status;
663
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400664 if (!bluetooth_supported ||
665 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 status = 0;
667
668 return status;
669}
670
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400671static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400674 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400676 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 len += sprintf(p + len, "status:\t\tnot supported\n");
678 else if (!(status & 1))
679 len += sprintf(p + len, "status:\t\tnot installed\n");
680 else {
681 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
682 len += sprintf(p + len, "commands:\tenable, disable\n");
683 }
684
685 return len;
686}
687
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400688static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400690 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 char *cmd;
692 int do_cmd = 0;
693
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400694 if (!bluetooth_supported)
695 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 while ((cmd = next_cmd(&buf))) {
698 if (strlencmp(cmd, "enable") == 0) {
699 status |= 2;
700 } else if (strlencmp(cmd, "disable") == 0) {
701 status &= ~2;
702 } else
703 return -EINVAL;
704 do_cmd = 1;
705 }
706
707 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400708 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 return 0;
711}
712
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400713static int wan_supported;
714
715static int wan_init(void)
716{
717 wan_supported = hkey_handle &&
718 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
719
720 return 0;
721}
722
723static int wan_status(void)
724{
725 int status;
726
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200727 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400728 status = 0;
729
730 return status;
731}
732
733static int wan_read(char *p)
734{
735 int len = 0;
736 int status = wan_status();
737
738 if (!wan_supported)
739 len += sprintf(p + len, "status:\t\tnot supported\n");
740 else if (!(status & 1))
741 len += sprintf(p + len, "status:\t\tnot installed\n");
742 else {
743 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
744 len += sprintf(p + len, "commands:\tenable, disable\n");
745 }
746
747 return len;
748}
749
750static int wan_write(char *buf)
751{
752 int status = wan_status();
753 char *cmd;
754 int do_cmd = 0;
755
756 if (!wan_supported)
757 return -ENODEV;
758
759 while ((cmd = next_cmd(&buf))) {
760 if (strlencmp(cmd, "enable") == 0) {
761 status |= 2;
762 } else if (strlencmp(cmd, "disable") == 0) {
763 status &= ~2;
764 } else
765 return -EINVAL;
766 do_cmd = 1;
767 }
768
769 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
770 return -EIO;
771
772 return 0;
773}
774
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200775enum video_access_mode {
776 IBMACPI_VIDEO_NONE = 0,
777 IBMACPI_VIDEO_570, /* 570 */
778 IBMACPI_VIDEO_770, /* 600e/x, 770e, 770x */
779 IBMACPI_VIDEO_NEW, /* all others */
780};
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400781
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200782static enum video_access_mode video_supported;
783static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400784
785static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400787 int ivga;
788
789 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
790 /* G41, assume IVGA doesn't change */
791 vid_handle = vid2_handle;
792
793 if (!vid_handle)
794 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200795 video_supported = IBMACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400796 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
797 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200798 video_supported = IBMACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400799 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
800 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200801 video_supported = IBMACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400802 else
803 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200804 video_supported = IBMACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 return 0;
807}
808
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400809static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 int status = 0;
812 int i;
813
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200814 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400815 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
816 status = i & 3;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200817 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400818 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
819 status |= 0x01 * i;
820 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
821 status |= 0x02 * i;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200822 } else if (video_supported == IBMACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400823 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
824 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
825 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400827 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
828 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
829 status |= 0x01 * i;
830 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
831 status |= 0x08 * i;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 return status;
835}
836
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400837static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400839 int autosw = 0;
840
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200841 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400842 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200843 else if (video_supported == IBMACPI_VIDEO_770 ||
844 video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400845 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
846
847 return autosw & 1;
848}
849
850static int video_read(char *p)
851{
852 int status = video_status();
853 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int len = 0;
855
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400856 if (!video_supported) {
857 len += sprintf(p + len, "status:\t\tnot supported\n");
858 return len;
859 }
860
861 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
863 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200864 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400865 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
866 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
867 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
868 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200869 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400870 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
871 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
873
874 return len;
875}
876
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400877static int video_switch(void)
878{
879 int autosw = video_autosw();
880 int ret;
881
882 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
883 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200884 ret = video_supported == IBMACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400885 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
886 acpi_evalf(vid_handle, NULL, "VSWT", "v");
887 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
888
889 return ret;
890}
891
892static int video_expand(void)
893{
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200894 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400895 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200896 else if (video_supported == IBMACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400897 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
898 else
899 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
900}
901
902static int video_switch2(int status)
903{
904 int ret;
905
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200906 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400907 ret = acpi_evalf(NULL, NULL,
908 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200909 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400910 int autosw = video_autosw();
911 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
912 return -EIO;
913
914 ret = acpi_evalf(vid_handle, NULL,
915 "ASWT", "vdd", status * 0x100, 0);
916
917 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
918 } else {
919 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
920 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
921 }
922
923 return ret;
924}
925
926static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 char *cmd;
929 int enable, disable, status;
930
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400931 if (!video_supported)
932 return -ENODEV;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 enable = disable = 0;
935
936 while ((cmd = next_cmd(&buf))) {
937 if (strlencmp(cmd, "lcd_enable") == 0) {
938 enable |= 0x01;
939 } else if (strlencmp(cmd, "lcd_disable") == 0) {
940 disable |= 0x01;
941 } else if (strlencmp(cmd, "crt_enable") == 0) {
942 enable |= 0x02;
943 } else if (strlencmp(cmd, "crt_disable") == 0) {
944 disable |= 0x02;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200945 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400946 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 enable |= 0x08;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200948 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400949 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 disable |= 0x08;
951 } else if (strlencmp(cmd, "auto_enable") == 0) {
952 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
953 return -EIO;
954 } else if (strlencmp(cmd, "auto_disable") == 0) {
955 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
956 return -EIO;
957 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400958 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return -EIO;
960 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400961 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -EIO;
963 } else
964 return -EINVAL;
965 }
966
967 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400968 status = (video_status() & 0x0f & ~disable) | enable;
969 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return -EIO;
971 }
972
973 return 0;
974}
975
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400976static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400978 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400981static int light_supported;
982static int light_status_supported;
983
984static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400986 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
987 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
988
989 if (light_supported)
990 /* light status not supported on
991 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
992 light_status_supported = acpi_evalf(ec_handle, NULL,
993 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 return 0;
996}
997
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400998static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 int len = 0;
1001 int status = 0;
1002
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001003 if (!light_supported) {
1004 len += sprintf(p + len, "status:\t\tnot supported\n");
1005 } else if (!light_status_supported) {
1006 len += sprintf(p + len, "status:\t\tunknown\n");
1007 len += sprintf(p + len, "commands:\ton, off\n");
1008 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1010 return -EIO;
1011 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001012 len += sprintf(p + len, "commands:\ton, off\n");
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 return len;
1016}
1017
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001018static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 int cmos_cmd, lght_cmd;
1021 char *cmd;
1022 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001023
1024 if (!light_supported)
1025 return -ENODEV;
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 while ((cmd = next_cmd(&buf))) {
1028 if (strlencmp(cmd, "on") == 0) {
1029 cmos_cmd = 0x0c;
1030 lght_cmd = 1;
1031 } else if (strlencmp(cmd, "off") == 0) {
1032 cmos_cmd = 0x0d;
1033 lght_cmd = 0;
1034 } else
1035 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001038 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1039 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!success)
1041 return -EIO;
1042 }
1043
1044 return 0;
1045}
1046
1047static int _sta(acpi_handle handle)
1048{
1049 int status;
1050
1051 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1052 status = 0;
1053
1054 return status;
1055}
Henrique de Moraes Holschuhd2fadbb2007-01-11 02:58:15 -05001056
Kristen Accardi63e5f242006-02-23 17:56:06 -08001057#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058#define dock_docked() (_sta(dock_handle) & 1)
1059
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001060static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 int len = 0;
1063 int docked = dock_docked();
1064
1065 if (!dock_handle)
1066 len += sprintf(p + len, "status:\t\tnot supported\n");
1067 else if (!docked)
1068 len += sprintf(p + len, "status:\t\tundocked\n");
1069 else {
1070 len += sprintf(p + len, "status:\t\tdocked\n");
1071 len += sprintf(p + len, "commands:\tdock, undock\n");
1072 }
1073
1074 return len;
1075}
1076
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001077static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 char *cmd;
1080
1081 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001082 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 while ((cmd = next_cmd(&buf))) {
1085 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001086 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1087 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -EIO;
1089 } else if (strlencmp(cmd, "dock") == 0) {
1090 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1091 return -EIO;
1092 } else
1093 return -EINVAL;
1094 }
1095
1096 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099static void dock_notify(struct ibm_struct *ibm, u32 event)
1100{
1101 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001102 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001104 if (event == 1 && !pci) /* 570 */
1105 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1106 else if (event == 1 && pci) /* 570 */
1107 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1108 else if (event == 3 && docked)
1109 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001111 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001113 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 else {
1115 printk(IBM_ERR "unknown dock event %d, status %d\n",
1116 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001117 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001122static int bay_status_supported;
1123static int bay_status2_supported;
1124static int bay_eject_supported;
1125static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001127static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001129 bay_status_supported = bay_handle &&
1130 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1131 bay_status2_supported = bay2_handle &&
1132 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1133
1134 bay_eject_supported = bay_handle && bay_ej_handle &&
1135 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1136 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1137 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 return 0;
1140}
1141
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001142#define bay_occupied(b) (_sta(b##_handle) & 1)
1143
1144static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001147 int occupied = bay_occupied(bay);
1148 int occupied2 = bay_occupied(bay2);
1149 int eject, eject2;
1150
1151 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1152 (occupied ? "occupied" : "unoccupied") :
1153 "not supported");
1154 if (bay_status2_supported)
1155 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1156 "occupied" : "unoccupied");
1157
1158 eject = bay_eject_supported && occupied;
1159 eject2 = bay_eject2_supported && occupied2;
1160
1161 if (eject && eject2)
1162 len += sprintf(p + len, "commands:\teject, eject2\n");
1163 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001165 else if (eject2)
1166 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 return len;
1169}
1170
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001171static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 char *cmd;
1174
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001175 if (!bay_eject_supported && !bay_eject2_supported)
1176 return -ENODEV;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001179 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1180 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1181 return -EIO;
1182 } else if (bay_eject2_supported &&
1183 strlencmp(cmd, "eject2") == 0) {
1184 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -EIO;
1186 } else
1187 return -EINVAL;
1188 }
1189
1190 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193static void bay_notify(struct ibm_struct *ibm, u32 event)
1194{
1195 acpi_bus_generate_event(ibm->device, event, 0);
1196}
1197
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001198static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 int len = 0;
1201
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001202 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1203 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (!cmos_handle)
1205 len += sprintf(p + len, "status:\t\tnot supported\n");
1206 else {
1207 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001208 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
1211 return len;
1212}
1213
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001214static int cmos_eval(int cmos_cmd)
1215{
1216 if (cmos_handle)
1217 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1218 else
1219 return 1;
1220}
1221
1222static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 char *cmd;
1225 int cmos_cmd;
1226
1227 if (!cmos_handle)
1228 return -EINVAL;
1229
1230 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001231 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1232 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* cmos_cmd set */
1234 } else
1235 return -EINVAL;
1236
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001237 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EIO;
1239 }
1240
1241 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001242}
1243
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001244enum led_access_mode {
1245 IBMACPI_LED_NONE = 0,
1246 IBMACPI_LED_570, /* 570 */
1247 IBMACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1248 IBMACPI_LED_NEW, /* all others */
1249};
1250static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001251
1252static int led_init(void)
1253{
1254 if (!led_handle)
1255 /* led not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001256 led_supported = IBMACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001257 else if (strlencmp(led_path, "SLED") == 0)
1258 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001259 led_supported = IBMACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001260 else if (strlencmp(led_path, "SYSL") == 0)
1261 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001262 led_supported = IBMACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001263 else
1264 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001265 led_supported = IBMACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001266
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
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001282 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001283 /* 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
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001331 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001332 /* 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;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001337 } else if (led_supported == IBMACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001338 /* 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");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001431
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001432 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;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001632
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001633 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{
Richard Purdie599a52d2007-02-10 23:07:48 +00001704 return brightness_set(bd->props.brightness);
Holger Macht8acb0252006-10-20 14:30:28 -07001705}
1706
Richard Purdie599a52d2007-02-10 23:07:48 +00001707static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001708 .get_brightness = brightness_get,
1709 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001710};
1711
1712static int brightness_init(void)
1713{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001714 int b;
1715
1716 b = brightness_get(NULL);
1717 if (b < 0)
1718 return b;
1719
Yu Luming519ab5f2006-12-19 12:56:15 -08001720 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001721 &ibm_backlight_data);
1722 if (IS_ERR(ibm_backlight_device)) {
1723 printk(IBM_ERR "Could not register backlight device\n");
1724 return PTR_ERR(ibm_backlight_device);
1725 }
1726
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001727 ibm_backlight_device->props.max_brightness = 7;
1728 ibm_backlight_device->props.brightness = b;
1729 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001730
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001731 return 0;
1732}
1733
1734static void brightness_exit(void)
1735{
1736 if (ibm_backlight_device) {
1737 backlight_device_unregister(ibm_backlight_device);
1738 ibm_backlight_device = NULL;
1739 }
1740}
1741
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001742static int volume_offset = 0x30;
1743
1744static int volume_read(char *p)
1745{
1746 int len = 0;
1747 u8 level;
1748
1749 if (!acpi_ec_read(volume_offset, &level)) {
1750 len += sprintf(p + len, "level:\t\tunreadable\n");
1751 } else {
1752 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1753 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1754 len += sprintf(p + len, "commands:\tup, down, mute\n");
1755 len += sprintf(p + len, "commands:\tlevel <level>"
1756 " (<level> is 0-15)\n");
1757 }
1758
1759 return len;
1760}
1761
1762#define VOLUME_DOWN 0
1763#define VOLUME_UP 1
1764#define VOLUME_MUTE 2
1765
1766static int volume_write(char *buf)
1767{
1768 int cmos_cmd, inc, i;
1769 u8 level, mute;
1770 int new_level, new_mute;
1771 char *cmd;
1772
1773 while ((cmd = next_cmd(&buf))) {
1774 if (!acpi_ec_read(volume_offset, &level))
1775 return -EIO;
1776 new_mute = mute = level & 0x40;
1777 new_level = level = level & 0xf;
1778
1779 if (strlencmp(cmd, "up") == 0) {
1780 if (mute)
1781 new_mute = 0;
1782 else
1783 new_level = level == 15 ? 15 : level + 1;
1784 } else if (strlencmp(cmd, "down") == 0) {
1785 if (mute)
1786 new_mute = 0;
1787 else
1788 new_level = level == 0 ? 0 : level - 1;
1789 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1790 new_level >= 0 && new_level <= 15) {
1791 /* new_level set */
1792 } else if (strlencmp(cmd, "mute") == 0) {
1793 new_mute = 0x40;
1794 } else
1795 return -EINVAL;
1796
1797 if (new_level != level) { /* mute doesn't change */
1798 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1799 inc = new_level > level ? 1 : -1;
1800
1801 if (mute && (!cmos_eval(cmos_cmd) ||
1802 !acpi_ec_write(volume_offset, level)))
1803 return -EIO;
1804
1805 for (i = level; i != new_level; i += inc)
1806 if (!cmos_eval(cmos_cmd) ||
1807 !acpi_ec_write(volume_offset, i + inc))
1808 return -EIO;
1809
1810 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1811 !acpi_ec_write(volume_offset,
1812 new_level + mute)))
1813 return -EIO;
1814 }
1815
1816 if (new_mute != mute) { /* level doesn't change */
1817 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1818
1819 if (!cmos_eval(cmos_cmd) ||
1820 !acpi_ec_write(volume_offset, level + new_mute))
1821 return -EIO;
1822 }
1823 }
1824
1825 return 0;
1826}
1827
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001828static enum fan_status_access_mode fan_status_access_mode;
1829static enum fan_control_access_mode fan_control_access_mode;
1830static enum fan_control_commands fan_control_commands;
1831
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001832static int fan_control_status_known;
1833static u8 fan_control_initial_status;
1834
Len Brown25c68a32006-12-08 04:43:41 -05001835static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001836static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05001837static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001838
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001839static int fan_init(void)
1840{
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001841 fan_status_access_mode = IBMACPI_FAN_NONE;
1842 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1843 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001844 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001845 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001846
1847 if (gfan_handle) {
1848 /* 570, 600e/x, 770e, 770x */
1849 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1850 } else {
1851 /* all other ThinkPads: note that even old-style
1852 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001853 if (likely(acpi_ec_read(fan_status_offset,
1854 &fan_control_initial_status))) {
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001855 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001856
1857 /* In some ThinkPads, neither the EC nor the ACPI
1858 * DSDT initialize the fan status, and it ends up
1859 * being set to 0x07 when it *could* be either
1860 * 0x07 or 0x80.
1861 *
1862 * Enable for TP-1Y (T43), TP-78 (R51e),
1863 * TP-76 (R52), TP-70 (T43, R52), which are known
1864 * to be buggy. */
1865 if (fan_control_initial_status == 0x07 &&
1866 ibm_thinkpad_ec_found &&
1867 ((ibm_thinkpad_ec_found[0] == '1' &&
1868 ibm_thinkpad_ec_found[1] == 'Y') ||
1869 (ibm_thinkpad_ec_found[0] == '7' &&
1870 (ibm_thinkpad_ec_found[1] == '6' ||
1871 ibm_thinkpad_ec_found[1] == '8' ||
1872 ibm_thinkpad_ec_found[1] == '0'))
1873 )) {
1874 printk(IBM_NOTICE
1875 "fan_init: initial fan status is "
1876 "unknown, assuming it is in auto "
1877 "mode\n");
1878 fan_control_status_known = 0;
1879 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001880 } else {
1881 printk(IBM_ERR
1882 "ThinkPad ACPI EC access misbehaving, "
1883 "fan status and control unavailable\n");
1884 return 0;
1885 }
1886 }
1887
1888 if (sfan_handle) {
1889 /* 570, 770x-JL */
1890 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02001891 fan_control_commands |=
1892 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001893 } else {
1894 if (!gfan_handle) {
1895 /* gfan without sfan means no fan control */
1896 /* all other models implement TP EC 0x2f control */
1897
1898 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001899 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001900 fan_control_access_mode =
1901 IBMACPI_FAN_WR_ACPI_FANS;
1902 fan_control_commands |=
1903 IBMACPI_FAN_CMD_SPEED |
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001904 IBMACPI_FAN_CMD_LEVEL |
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001905 IBMACPI_FAN_CMD_ENABLE;
1906 } else {
1907 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001908 fan_control_commands |=
1909 IBMACPI_FAN_CMD_LEVEL |
1910 IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001911 }
1912 }
1913 }
1914
1915 return 0;
1916}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001917
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001918static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001919{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001920 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001921
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001922 /* TODO:
1923 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1924
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001925 switch (fan_status_access_mode) {
1926 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001927 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001928
1929 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001930 return -EIO;
1931
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001932 if (likely(status))
1933 *status = s & 0x07;
1934
1935 break;
1936
1937 case IBMACPI_FAN_RD_TPEC:
1938 /* all except 570, 600e/x, 770e, 770x */
1939 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1940 return -EIO;
1941
1942 if (likely(status))
1943 *status = s;
1944
1945 break;
1946
1947 default:
1948 return -ENXIO;
1949 }
1950
1951 return 0;
1952}
1953
1954static int fan_get_speed(unsigned int *speed)
1955{
1956 u8 hi, lo;
1957
1958 switch (fan_status_access_mode) {
1959 case IBMACPI_FAN_RD_TPEC:
1960 /* all except 570, 600e/x, 770e, 770x */
1961 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1962 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1963 return -EIO;
1964
1965 if (likely(speed))
1966 *speed = (hi << 8) | lo;
1967
1968 break;
1969
1970 default:
1971 return -ENXIO;
1972 }
1973
1974 return 0;
1975}
1976
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001977static void fan_exit(void)
1978{
1979 cancel_delayed_work(&fan_watchdog_task);
1980 flush_scheduled_work();
1981}
1982
1983static void fan_watchdog_reset(void)
1984{
1985 static int fan_watchdog_active = 0;
1986
1987 if (fan_watchdog_active)
1988 cancel_delayed_work(&fan_watchdog_task);
1989
1990 if (fan_watchdog_maxinterval > 0) {
1991 fan_watchdog_active = 1;
1992 if (!schedule_delayed_work(&fan_watchdog_task,
1993 msecs_to_jiffies(fan_watchdog_maxinterval
1994 * 1000))) {
1995 printk(IBM_ERR "failed to schedule the fan watchdog, "
1996 "watchdog will not trigger\n");
1997 }
1998 } else
1999 fan_watchdog_active = 0;
2000}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002001
2002static int fan_read(char *p)
2003{
2004 int len = 0;
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002005 int rc;
2006 u8 status;
2007 unsigned int speed = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002008
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002009 switch (fan_status_access_mode) {
2010 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002011 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002012 if ((rc = fan_get_status(&status)) < 0)
2013 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002014
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002015 len += sprintf(p + len, "status:\t\t%s\n"
2016 "level:\t\t%d\n",
2017 (status != 0) ? "enabled" : "disabled", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002018 break;
2019
2020 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002021 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002022 if ((rc = fan_get_status(&status)) < 0)
2023 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002024
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002025 if (unlikely(!fan_control_status_known)) {
2026 if (status != fan_control_initial_status)
2027 fan_control_status_known = 1;
2028 else
2029 /* Return most likely status. In fact, it
2030 * might be the only possible status */
2031 status = IBMACPI_FAN_EC_AUTO;
2032 }
2033
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002034 len += sprintf(p + len, "status:\t\t%s\n",
2035 (status != 0) ? "enabled" : "disabled");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002036
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002037 /* No ThinkPad boots on disengaged mode, we can safely
2038 * assume the tachometer is online if fan control status
2039 * was unknown */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002040 if ((rc = fan_get_speed(&speed)) < 0)
2041 return rc;
2042
2043 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002044
2045 if (status & IBMACPI_FAN_EC_DISENGAGED)
2046 /* Disengaged mode takes precedence */
2047 len += sprintf(p + len, "level:\t\tdisengaged\n");
2048 else if (status & IBMACPI_FAN_EC_AUTO)
2049 len += sprintf(p + len, "level:\t\tauto\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002050 else
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002051 len += sprintf(p + len, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002052 break;
2053
2054 case IBMACPI_FAN_NONE:
2055 default:
2056 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002057 }
2058
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002059 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2060 len += sprintf(p + len, "commands:\tlevel <level>");
2061
2062 switch (fan_control_access_mode) {
2063 case IBMACPI_FAN_WR_ACPI_SFAN:
2064 len += sprintf(p + len, " (<level> is 0-7)\n");
2065 break;
2066
2067 default:
2068 len += sprintf(p + len, " (<level> is 0-7, "
2069 "auto, disengaged)\n");
2070 break;
2071 }
2072 }
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002073
2074 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002075 len += sprintf(p + len, "commands:\tenable, disable\n"
2076 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2077 "1-120 (seconds))\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002078
2079 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002080 len += sprintf(p + len, "commands:\tspeed <speed>"
2081 " (<speed> is 0-65535)\n");
2082
2083 return len;
2084}
2085
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002086static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002087{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002088 switch (fan_control_access_mode) {
2089 case IBMACPI_FAN_WR_ACPI_SFAN:
2090 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002091 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2092 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002093 } else
2094 return -EINVAL;
2095 break;
2096
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002097 case IBMACPI_FAN_WR_ACPI_FANS:
2098 case IBMACPI_FAN_WR_TPEC:
2099 if ((level != IBMACPI_FAN_EC_AUTO) &&
2100 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2101 ((level < 0) || (level > 7)))
2102 return -EINVAL;
2103
2104 if (!acpi_ec_write(fan_status_offset, level))
2105 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002106 else
2107 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002108 break;
2109
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002110 default:
2111 return -ENXIO;
2112 }
2113 return 0;
2114}
2115
2116static int fan_set_enable(void)
2117{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002118 u8 s;
2119 int rc;
2120
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002121 switch (fan_control_access_mode) {
2122 case IBMACPI_FAN_WR_ACPI_FANS:
2123 case IBMACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002124 if ((rc = fan_get_status(&s)) < 0)
2125 return rc;
2126
2127 /* Don't go out of emergency fan mode */
2128 if (s != 7)
2129 s = IBMACPI_FAN_EC_AUTO;
2130
2131 if (!acpi_ec_write(fan_status_offset, s))
2132 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002133 else
2134 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002135 break;
2136
2137 case IBMACPI_FAN_WR_ACPI_SFAN:
2138 if ((rc = fan_get_status(&s)) < 0)
2139 return rc;
2140
2141 s &= 0x07;
2142
2143 /* Set fan to at least level 4 */
2144 if (s < 4)
2145 s = 4;
2146
2147 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002148 return -EIO;
2149 break;
2150
2151 default:
2152 return -ENXIO;
2153 }
2154 return 0;
2155}
2156
2157static int fan_set_disable(void)
2158{
2159 switch (fan_control_access_mode) {
2160 case IBMACPI_FAN_WR_ACPI_FANS:
2161 case IBMACPI_FAN_WR_TPEC:
2162 if (!acpi_ec_write(fan_status_offset, 0x00))
2163 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002164 else
2165 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002166 break;
2167
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002168 case IBMACPI_FAN_WR_ACPI_SFAN:
2169 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2170 return -EIO;
2171 break;
2172
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002173 default:
2174 return -ENXIO;
2175 }
2176 return 0;
2177}
2178
2179static int fan_set_speed(int speed)
2180{
2181 switch (fan_control_access_mode) {
2182 case IBMACPI_FAN_WR_ACPI_FANS:
2183 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002184 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2185 speed, speed, speed))
2186 return -EIO;
2187 } else
2188 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002189 break;
2190
2191 default:
2192 return -ENXIO;
2193 }
2194 return 0;
2195}
2196
2197static int fan_write_cmd_level(const char *cmd, int *rc)
2198{
2199 int level;
2200
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002201 if (strlencmp(cmd, "level auto") == 0)
2202 level = IBMACPI_FAN_EC_AUTO;
2203 else if (strlencmp(cmd, "level disengaged") == 0)
2204 level = IBMACPI_FAN_EC_DISENGAGED;
2205 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002206 return 0;
2207
2208 if ((*rc = fan_set_level(level)) == -ENXIO)
2209 printk(IBM_ERR "level command accepted for unsupported "
2210 "access mode %d", fan_control_access_mode);
2211
2212 return 1;
2213}
2214
2215static int fan_write_cmd_enable(const char *cmd, int *rc)
2216{
2217 if (strlencmp(cmd, "enable") != 0)
2218 return 0;
2219
2220 if ((*rc = fan_set_enable()) == -ENXIO)
2221 printk(IBM_ERR "enable command accepted for unsupported "
2222 "access mode %d", fan_control_access_mode);
2223
2224 return 1;
2225}
2226
2227static int fan_write_cmd_disable(const char *cmd, int *rc)
2228{
2229 if (strlencmp(cmd, "disable") != 0)
2230 return 0;
2231
2232 if ((*rc = fan_set_disable()) == -ENXIO)
2233 printk(IBM_ERR "disable command accepted for unsupported "
2234 "access mode %d", fan_control_access_mode);
2235
2236 return 1;
2237}
2238
2239static int fan_write_cmd_speed(const char *cmd, int *rc)
2240{
2241 int speed;
2242
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002243 /* TODO:
2244 * Support speed <low> <medium> <high> ? */
2245
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002246 if (sscanf(cmd, "speed %d", &speed) != 1)
2247 return 0;
2248
2249 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2250 printk(IBM_ERR "speed command accepted for unsupported "
2251 "access mode %d", fan_control_access_mode);
2252
2253 return 1;
2254}
2255
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002256static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2257{
2258 int interval;
2259
2260 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2261 return 0;
2262
2263 if (interval < 0 || interval > 120)
2264 *rc = -EINVAL;
2265 else
2266 fan_watchdog_maxinterval = interval;
2267
2268 return 1;
2269}
2270
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002271static int fan_write(char *buf)
2272{
2273 char *cmd;
2274 int rc = 0;
2275
2276 while (!rc && (cmd = next_cmd(&buf))) {
2277 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2278 fan_write_cmd_level(cmd, &rc)) &&
2279 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2280 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002281 fan_write_cmd_disable(cmd, &rc) ||
2282 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002283 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2284 fan_write_cmd_speed(cmd, &rc))
2285 )
2286 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002287 else if (!rc)
2288 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002289 }
2290
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002291 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002292}
2293
Len Brown25c68a32006-12-08 04:43:41 -05002294static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002295{
2296 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2297 if (fan_set_enable()) {
2298 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2299 /* reschedule for later */
2300 fan_watchdog_reset();
2301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
2304static struct ibm_struct ibms[] = {
2305 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002306 .name = "driver",
Adrian Bunk1f217822006-12-19 13:01:28 -08002307 .init = ibm_acpi_driver_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002308 .read = driver_read,
2309 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002311 .name = "hotkey",
2312 .hid = IBM_HKEY_HID,
2313 .init = hotkey_init,
2314 .read = hotkey_read,
2315 .write = hotkey_write,
2316 .exit = hotkey_exit,
2317 .notify = hotkey_notify,
2318 .handle = &hkey_handle,
2319 .type = ACPI_DEVICE_NOTIFY,
2320 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002322 .name = "bluetooth",
2323 .init = bluetooth_init,
2324 .read = bluetooth_read,
2325 .write = bluetooth_write,
2326 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002328 .name = "wan",
2329 .init = wan_init,
2330 .read = wan_read,
2331 .write = wan_write,
2332 .experimental = 1,
2333 },
2334 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002335 .name = "video",
2336 .init = video_init,
2337 .read = video_read,
2338 .write = video_write,
2339 .exit = video_exit,
2340 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002342 .name = "light",
2343 .init = light_init,
2344 .read = light_read,
2345 .write = light_write,
2346 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002347#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002349 .name = "dock",
2350 .read = dock_read,
2351 .write = dock_write,
2352 .notify = dock_notify,
2353 .handle = &dock_handle,
2354 .type = ACPI_SYSTEM_NOTIFY,
2355 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002357 .name = "dock",
2358 .hid = IBM_PCI_HID,
2359 .notify = dock_notify,
2360 .handle = &pci_handle,
2361 .type = ACPI_SYSTEM_NOTIFY,
2362 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002365 .name = "bay",
2366 .init = bay_init,
2367 .read = bay_read,
2368 .write = bay_write,
2369 .notify = bay_notify,
2370 .handle = &bay_handle,
2371 .type = ACPI_SYSTEM_NOTIFY,
2372 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002374 .name = "cmos",
2375 .read = cmos_read,
2376 .write = cmos_write,
2377 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002379 .name = "led",
2380 .init = led_init,
2381 .read = led_read,
2382 .write = led_write,
2383 },
2384 {
2385 .name = "beep",
2386 .read = beep_read,
2387 .write = beep_write,
2388 },
2389 {
2390 .name = "thermal",
2391 .init = thermal_init,
2392 .read = thermal_read,
2393 },
2394 {
2395 .name = "ecdump",
2396 .read = ecdump_read,
2397 .write = ecdump_write,
2398 .experimental = 1,
2399 },
2400 {
2401 .name = "brightness",
2402 .read = brightness_read,
2403 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002404 .init = brightness_init,
2405 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002406 },
2407 {
2408 .name = "volume",
2409 .read = volume_read,
2410 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002411 },
2412 {
2413 .name = "fan",
2414 .read = fan_read,
2415 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002416 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002417 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002418 .experimental = 1,
2419 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422static int dispatch_read(char *page, char **start, off_t off, int count,
2423 int *eof, void *data)
2424{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002425 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (!ibm || !ibm->read)
2429 return -EINVAL;
2430
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002431 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (len < 0)
2433 return len;
2434
2435 if (len <= off + count)
2436 *eof = 1;
2437 *start = page + off;
2438 len -= off;
2439 if (len > count)
2440 len = count;
2441 if (len < 0)
2442 len = 0;
2443
2444 return len;
2445}
2446
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002447static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 unsigned long count, void *data)
2449{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002450 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 char *kernbuf;
2452 int ret;
2453
2454 if (!ibm || !ibm->write)
2455 return -EINVAL;
2456
2457 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2458 if (!kernbuf)
2459 return -ENOMEM;
2460
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002461 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002463 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
2465
2466 kernbuf[count] = 0;
2467 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002468 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (ret == 0)
2470 ret = count;
2471
2472 kfree(kernbuf);
2473
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002474 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2478{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002479 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 if (!ibm || !ibm->notify)
2482 return;
2483
2484 ibm->notify(ibm, event);
2485}
2486
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002487static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
2489 acpi_status status;
2490 int ret;
2491
2492 if (!*ibm->handle)
2493 return 0;
2494
2495 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2496 if (ret < 0) {
2497 printk(IBM_ERR "%s device not present\n", ibm->name);
2498 return 0;
2499 }
2500
2501 acpi_driver_data(ibm->device) = ibm;
2502 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2503
2504 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2505 dispatch_notify, ibm);
2506 if (ACPI_FAILURE(status)) {
2507 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2508 ibm->name, status);
2509 return -ENODEV;
2510 }
Alexey Starikovskiy4afaf542006-12-18 14:53:33 -03002511 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 return 0;
2513}
2514
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002515static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
2517 return 0;
2518}
2519
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002520static int __init register_driver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 int ret;
2523
Burman Yan36bcbec2006-12-19 12:56:11 -08002524 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (!ibm->driver) {
2526 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2527 return -1;
2528 }
2529
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002530 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002532 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 ret = acpi_bus_register_driver(ibm->driver);
2535 if (ret < 0) {
2536 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2537 ibm->hid, ret);
2538 kfree(ibm->driver);
2539 }
2540
2541 return ret;
2542}
2543
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002544static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
2546 int ret;
2547 struct proc_dir_entry *entry;
2548
2549 if (ibm->experimental && !experimental)
2550 return 0;
2551
2552 if (ibm->hid) {
2553 ret = register_driver(ibm);
2554 if (ret < 0)
2555 return ret;
2556 ibm->driver_registered = 1;
2557 }
2558
2559 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002560 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (ret != 0)
2562 return ret;
2563 ibm->init_called = 1;
2564 }
2565
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002566 if (ibm->read) {
2567 entry = create_proc_entry(ibm->name,
2568 S_IFREG | S_IRUGO | S_IWUSR,
2569 proc_dir);
2570 if (!entry) {
2571 printk(IBM_ERR "unable to create proc entry %s\n",
2572 ibm->name);
2573 return -ENODEV;
2574 }
2575 entry->owner = THIS_MODULE;
2576 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002578 if (ibm->write)
2579 entry->write_proc = &dispatch_write;
2580 ibm->proc_created = 1;
2581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 if (ibm->notify) {
2584 ret = setup_notify(ibm);
2585 if (ret < 0)
2586 return ret;
2587 }
2588
2589 return 0;
2590}
2591
2592static void ibm_exit(struct ibm_struct *ibm)
2593{
2594 if (ibm->notify_installed)
2595 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2596 dispatch_notify);
2597
2598 if (ibm->proc_created)
2599 remove_proc_entry(ibm->name, proc_dir);
2600
2601 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002602 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 if (ibm->driver_registered) {
2605 acpi_bus_unregister_driver(ibm->driver);
2606 kfree(ibm->driver);
2607 }
2608}
2609
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002610static void __init ibm_handle_init(char *name,
2611 acpi_handle * handle, acpi_handle parent,
2612 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
2614 int i;
2615 acpi_status status;
2616
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002617 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002619 if (ACPI_SUCCESS(status)) {
2620 *path = paths[i];
2621 return;
2622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002628#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002630 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002632static int __init set_ibm_param(const char *val, struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002636 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2637 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2638 if (strlen(val) > sizeof(ibms[i].param) - 2)
2639 return -ENOSPC;
2640 strcpy(ibms[i].param, val);
2641 strcat(ibms[i].param, ",");
2642 return 0;
2643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return -EINVAL;
2646}
2647
2648#define IBM_PARAM(feature) \
2649 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2650
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002651IBM_PARAM(hotkey);
2652IBM_PARAM(bluetooth);
2653IBM_PARAM(video);
2654IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002655#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002656IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002657#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002658IBM_PARAM(bay);
2659IBM_PARAM(cmos);
2660IBM_PARAM(led);
2661IBM_PARAM(beep);
2662IBM_PARAM(ecdump);
2663IBM_PARAM(brightness);
2664IBM_PARAM(volume);
2665IBM_PARAM(fan);
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667static void acpi_ibm_exit(void)
2668{
2669 int i;
2670
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002671 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 ibm_exit(&ibms[i]);
2673
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002674 if (proc_dir)
2675 remove_proc_entry(IBM_DIR, acpi_root_dir);
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002676
2677 if (ibm_thinkpad_ec_found)
2678 kfree(ibm_thinkpad_ec_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002681static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002682{
2683 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002684 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002685
2686 /*
2687 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2688 * X32 or newer, all Z series; Some models must have an
2689 * up-to-date BIOS or they will not be detected.
2690 *
2691 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2692 */
2693 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002694 if (sscanf(dev->name,
2695 "IBM ThinkPad Embedded Controller -[%17c",
2696 ec_fw_string) == 1) {
2697 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2698 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2699 return kstrdup(ec_fw_string, GFP_KERNEL);
2700 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002701 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002702 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
2705static int __init acpi_ibm_init(void)
2706{
2707 int ret, i;
2708
2709 if (acpi_disabled)
2710 return -ENODEV;
2711
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002712 /* ec is required because many other handles are relative to it */
2713 IBM_HANDLE_INIT(ec);
2714 if (!ec_handle) {
2715 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002719 /* Models with newer firmware report the EC in DMI */
2720 ibm_thinkpad_ec_found = check_dmi_for_ec();
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002723 IBM_HANDLE_INIT(vid);
2724 IBM_HANDLE_INIT(vid2);
2725 IBM_HANDLE_INIT(ledb);
2726 IBM_HANDLE_INIT(led);
2727 IBM_HANDLE_INIT(hkey);
2728 IBM_HANDLE_INIT(lght);
2729 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002730#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002731 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002732#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002733 IBM_HANDLE_INIT(pci);
2734 IBM_HANDLE_INIT(bay);
2735 if (bay_handle)
2736 IBM_HANDLE_INIT(bay_ej);
2737 IBM_HANDLE_INIT(bay2);
2738 if (bay2_handle)
2739 IBM_HANDLE_INIT(bay2_ej);
2740 IBM_HANDLE_INIT(beep);
2741 IBM_HANDLE_INIT(ecrd);
2742 IBM_HANDLE_INIT(ecwr);
2743 IBM_HANDLE_INIT(fans);
2744 IBM_HANDLE_INIT(gfan);
2745 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
2747 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2748 if (!proc_dir) {
2749 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002750 acpi_ibm_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return -ENODEV;
2752 }
2753 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002754
2755 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002757 if (ret >= 0 && *ibms[i].param)
2758 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 if (ret < 0) {
2760 acpi_ibm_exit();
2761 return ret;
2762 }
2763 }
2764
2765 return 0;
2766}
2767
2768module_init(acpi_ibm_init);
2769module_exit(acpi_ibm_exit);