blob: 9275dfc130f220a54e393c6b436a30ae8888112a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ibm_acpi.c - IBM ThinkPad ACPI Extras
3 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Borislav Deianov78f81cc2005-08-17 00:00:00 -040020 */
21
22#define IBM_VERSION "0.12a"
23
24/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * Changelog:
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026 *
27 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
28 * 2005-03-17 0.11 support for 600e, 770x
29 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
30 * support for 770e, G41
31 * G40 and G41 don't have a thinklight
32 * temperatures no longer experimental
33 * experimental brightness control
34 * experimental volume control
35 * experimental fan enable/disable
36 * 2005-01-16 0.10 fix module loading on R30, R31
37 * 2005-01-16 0.9 support for 570, R30, R31
38 * ultrabay support on A22p, A3x
39 * limit arg for cmos, led, beep, drop experimental status
40 * more capable led control on A21e, A22p, T20-22, X20
41 * experimental temperatures and fan speed
42 * experimental embedded controller register dump
43 * mark more functions as __init, drop incorrect __exit
44 * use MODULE_VERSION
45 * thanks to Henrik Brix Andersen <brix@gentoo.org>
46 * fix parameter passing on module loading
47 * thanks to Rusty Russell <rusty@rustcorp.com.au>
48 * thanks to Jim Radford <radford@blackbean.org>
49 * 2004-11-08 0.8 fix init error case, don't return from a macro
50 * thanks to Chris Wright <chrisw@osdl.org>
51 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
52 * fix led control on A21e
53 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
55 * proc file format changed
56 * video_switch command
57 * experimental cmos control
58 * experimental led control
59 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040060 * 2004-09-16 0.4 support for module parameters
61 * hotkey mask can be prefixed by 0x
62 * video output switching
63 * video expansion control
64 * ultrabay eject support
65 * removed lcd brightness/on/off control, didn't work
66 * 2004-08-17 0.3 support for R40
67 * lcd off, brightness control
68 * thinklight on/off
69 * 2004-08-14 0.2 support for T series, X20
70 * bluetooth enable/disable
71 * hotkey events disabled by default
72 * removed fan control, currently useless
73 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/kernel.h>
77#include <linux/module.h>
78#include <linux/init.h>
79#include <linux/types.h>
80#include <linux/proc_fs.h>
Holger Macht8acb0252006-10-20 14:30:28 -070081#include <linux/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <asm/uaccess.h>
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020083#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#include <acpi/acpi_drivers.h>
86#include <acpi/acnamesp.h>
87
88#define IBM_NAME "ibm"
89#define IBM_DESC "IBM ThinkPad ACPI Extras"
90#define IBM_FILE "ibm_acpi"
91#define IBM_URL "http://ibm-acpi.sf.net/"
92
Borislav Deianov78f81cc2005-08-17 00:00:00 -040093MODULE_AUTHOR("Borislav Deianov");
94MODULE_DESCRIPTION(IBM_DESC);
95MODULE_VERSION(IBM_VERSION);
96MODULE_LICENSE("GPL");
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define IBM_DIR IBM_NAME
99
100#define IBM_LOG IBM_FILE ": "
101#define IBM_ERR KERN_ERR IBM_LOG
102#define IBM_NOTICE KERN_NOTICE IBM_LOG
103#define IBM_INFO KERN_INFO IBM_LOG
104#define IBM_DEBUG KERN_DEBUG IBM_LOG
105
106#define IBM_MAX_ACPI_ARGS 3
107
108#define __unused __attribute__ ((unused))
109
110static int experimental;
111module_param(experimental, int, 0);
112
113static acpi_handle root_handle = NULL;
114
115#define IBM_HANDLE(object, parent, paths...) \
116 static acpi_handle object##_handle; \
117 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400118 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 static char *object##_paths[] = { paths }
120
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400121/*
122 * The following models are supported to various degrees:
123 *
124 * 570, 600e, 600x, 770e, 770x
125 * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
126 * G40, G41
127 * R30, R31, R32, R40, R40e, R50, R50e, R50p, R51
128 * T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p, T43
129 * X20, X21, X22, X23, X24, X30, X31, X40
130 *
131 * The following models have no supported features:
132 *
133 * 240, 240x, i1400
134 *
135 * Still missing DSDTs for the following models:
136 *
137 * A20p, A22e, A22m
138 * R52
139 * S31
140 * T43p
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
144 "\\_SB.PCI.ISA.EC", /* 570 */
145 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
146 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
147 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
148 "\\_SB.PCI0.ICH3.EC0", /* R31 */
149 "\\_SB.PCI0.LPC.EC", /* all others */
150 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400152IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
153 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
154 "\\_SB.PCI0.VID0", /* 770e */
155 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
156 "\\_SB.PCI0.AGP.VID", /* all others */
157 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400159IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400161IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
162 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
163 "\\CMS", /* R40, R40e */
164 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800165#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400166IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
167 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
168 "\\_SB.PCI0.PCI1.DOCK", /* all others */
169 "\\_SB.PCI.ISA.SLCE", /* 570 */
170 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800171#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400172IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
173 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
174 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
175 ); /* A21e, R30, R31 */
176
177IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
178 "_EJ0", /* all others */
179 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
180
181IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
182 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
183 ); /* all others */
184
185IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
186 "_EJ0", /* 770x */
187 ); /* all others */
188
189/* don't list other alternatives as we install a notify handler on the 570 */
190IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
191
192IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
193 "^HKEY", /* R30, R31 */
194 "HKEY", /* all others */
195 ); /* 570 */
196
197IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
198IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
199
200IBM_HANDLE(led, ec, "SLED", /* 570 */
201 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
202 "LED", /* all others */
203 ); /* R30, R31 */
204
205IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
206IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
207IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
208IBM_HANDLE(fans, ec, "FANS"); /* X31, X40 */
209
210IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
211 "\\FSPD", /* 600e/x, 770e, 770x */
212 ); /* all others */
213
214IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
215 "JFNS", /* 770x-JL */
216 ); /* all others */
217
218#define IBM_HKEY_HID "IBM0068"
219#define IBM_PCI_HID "PNP0A03"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200221enum thermal_access_mode {
222 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
223 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
224 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200225 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
226 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200227};
228
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200229#define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200230struct ibm_thermal_sensors_struct {
231 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
232};
233
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200234enum fan_status_access_mode {
235 IBMACPI_FAN_NONE = 0, /* No fan status or control */
236 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
237 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
238};
239
240enum fan_control_access_mode {
241 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
242 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
243 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
244 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
245};
246
247enum fan_control_commands {
248 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
249 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
250 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd */
251};
252
253enum { /* Fan control constants */
254 fan_status_offset = 0x2f, /* EC register 0x2f */
255 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
256 * 0x84 must be read before 0x85 */
257};
258
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200259static int ibm_thinkpad_ec_found;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261struct ibm_struct {
262 char *name;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400263 char param[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 char *hid;
266 struct acpi_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400268 int (*init) (void);
269 int (*read) (char *);
270 int (*write) (char *);
271 void (*exit) (void);
272
273 void (*notify) (struct ibm_struct *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 acpi_handle *handle;
275 int type;
276 struct acpi_device *device;
277
278 int driver_registered;
279 int proc_created;
280 int init_called;
281 int notify_installed;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int experimental;
284};
285
286static struct proc_dir_entry *proc_dir = NULL;
287
Holger Macht8acb0252006-10-20 14:30:28 -0700288static struct backlight_device *ibm_backlight_device;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
291#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
292#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
293
294static int acpi_evalf(acpi_handle handle,
295 void *res, char *method, char *fmt, ...)
296{
297 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400298 struct acpi_object_list params;
299 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
300 struct acpi_buffer result, *resultp;
301 union acpi_object out_obj;
302 acpi_status status;
303 va_list ap;
304 char res_type;
305 int success;
306 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (!*fmt) {
309 printk(IBM_ERR "acpi_evalf() called with empty format\n");
310 return 0;
311 }
312
313 if (*fmt == 'q') {
314 quiet = 1;
315 fmt++;
316 } else
317 quiet = 0;
318
319 res_type = *(fmt++);
320
321 params.count = 0;
322 params.pointer = &in_objs[0];
323
324 va_start(ap, fmt);
325 while (*fmt) {
326 char c = *(fmt++);
327 switch (c) {
328 case 'd': /* int */
329 in_objs[params.count].integer.value = va_arg(ap, int);
330 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
331 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400332 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 default:
334 printk(IBM_ERR "acpi_evalf() called "
335 "with invalid format character '%c'\n", c);
336 return 0;
337 }
338 }
339 va_end(ap);
340
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400341 if (res_type != 'v') {
342 result.length = sizeof(out_obj);
343 result.pointer = &out_obj;
344 resultp = &result;
345 } else
346 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400348 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400351 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (res)
353 *(int *)res = out_obj.integer.value;
354 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
355 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400356 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 success = status == AE_OK;
358 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400359 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 default:
361 printk(IBM_ERR "acpi_evalf() called "
362 "with invalid format character '%c'\n", res_type);
363 return 0;
364 }
365
366 if (!success && !quiet)
367 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
368 method, fmt0, status);
369
370 return success;
371}
372
373static void __unused acpi_print_int(acpi_handle handle, char *method)
374{
375 int i;
376
377 if (acpi_evalf(handle, &i, method, "d"))
378 printk(IBM_INFO "%s = 0x%x\n", method, i);
379 else
380 printk(IBM_ERR "error calling %s\n", method);
381}
382
383static char *next_cmd(char **cmds)
384{
385 char *start = *cmds;
386 char *end;
387
388 while ((end = strchr(start, ',')) && end == start)
389 start = end + 1;
390
391 if (!end)
392 return NULL;
393
394 *end = 0;
395 *cmds = end + 1;
396 return start;
397}
398
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400399static int driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
402 printk(IBM_INFO "%s\n", IBM_URL);
403
404 return 0;
405}
406
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400407static int driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 int len = 0;
410
411 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
412 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
413
414 return len;
415}
416
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400417static int hotkey_supported;
418static int hotkey_mask_supported;
419static int hotkey_orig_status;
420static int hotkey_orig_mask;
421
422static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400425 return 0;
426
427 if (hotkey_mask_supported)
428 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
429 return 0;
430
431 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400434static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 int i;
437
438 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400441 if (hotkey_mask_supported)
442 for (i = 0; i < 32; i++) {
443 int bit = ((1 << i) & mask) != 0;
444 if (!acpi_evalf(hkey_handle,
445 NULL, "MHKM", "vdd", i + 1, bit))
446 return 0;
447 }
448
449 return 1;
450}
451
452static int hotkey_init(void)
453{
454 /* hotkey not supported on 570 */
455 hotkey_supported = hkey_handle != NULL;
456
457 if (hotkey_supported) {
458 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
459 A30, R30, R31, T20-22, X20-21, X22-24 */
460 hotkey_mask_supported =
461 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
462
463 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
464 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
467 return 0;
468}
469
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400470static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int status, mask;
473 int len = 0;
474
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400475 if (!hotkey_supported) {
476 len += sprintf(p + len, "status:\t\tnot supported\n");
477 return len;
478 }
479
480 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return -EIO;
482
483 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400484 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
486 len += sprintf(p + len,
487 "commands:\tenable, disable, reset, <mask>\n");
488 } else {
489 len += sprintf(p + len, "mask:\t\tnot supported\n");
490 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
491 }
492
493 return len;
494}
495
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400496static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 int status, mask;
499 char *cmd;
500 int do_cmd = 0;
501
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400502 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -ENODEV;
504
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400505 if (!hotkey_get(&status, &mask))
506 return -EIO;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 while ((cmd = next_cmd(&buf))) {
509 if (strlencmp(cmd, "enable") == 0) {
510 status = 1;
511 } else if (strlencmp(cmd, "disable") == 0) {
512 status = 0;
513 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400514 status = hotkey_orig_status;
515 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
517 /* mask set */
518 } else if (sscanf(cmd, "%x", &mask) == 1) {
519 /* mask set */
520 } else
521 return -EINVAL;
522 do_cmd = 1;
523 }
524
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400525 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -EIO;
527
528 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400531static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400533 if (hotkey_supported)
534 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static void hotkey_notify(struct ibm_struct *ibm, u32 event)
538{
539 int hkey;
540
541 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
542 acpi_bus_generate_event(ibm->device, event, hkey);
543 else {
544 printk(IBM_ERR "unknown hotkey event %d\n", event);
545 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400549static int bluetooth_supported;
550
551static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400553 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
554 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
555 bluetooth_supported = hkey_handle &&
556 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 return 0;
559}
560
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400561static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 int status;
564
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400565 if (!bluetooth_supported ||
566 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 status = 0;
568
569 return status;
570}
571
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400572static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400575 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400577 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 len += sprintf(p + len, "status:\t\tnot supported\n");
579 else if (!(status & 1))
580 len += sprintf(p + len, "status:\t\tnot installed\n");
581 else {
582 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
583 len += sprintf(p + len, "commands:\tenable, disable\n");
584 }
585
586 return len;
587}
588
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400589static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400591 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 char *cmd;
593 int do_cmd = 0;
594
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400595 if (!bluetooth_supported)
596 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 while ((cmd = next_cmd(&buf))) {
599 if (strlencmp(cmd, "enable") == 0) {
600 status |= 2;
601 } else if (strlencmp(cmd, "disable") == 0) {
602 status &= ~2;
603 } else
604 return -EINVAL;
605 do_cmd = 1;
606 }
607
608 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400609 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 return 0;
612}
613
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400614static int wan_supported;
615
616static int wan_init(void)
617{
618 wan_supported = hkey_handle &&
619 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
620
621 return 0;
622}
623
624static int wan_status(void)
625{
626 int status;
627
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200628 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400629 status = 0;
630
631 return status;
632}
633
634static int wan_read(char *p)
635{
636 int len = 0;
637 int status = wan_status();
638
639 if (!wan_supported)
640 len += sprintf(p + len, "status:\t\tnot supported\n");
641 else if (!(status & 1))
642 len += sprintf(p + len, "status:\t\tnot installed\n");
643 else {
644 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
645 len += sprintf(p + len, "commands:\tenable, disable\n");
646 }
647
648 return len;
649}
650
651static int wan_write(char *buf)
652{
653 int status = wan_status();
654 char *cmd;
655 int do_cmd = 0;
656
657 if (!wan_supported)
658 return -ENODEV;
659
660 while ((cmd = next_cmd(&buf))) {
661 if (strlencmp(cmd, "enable") == 0) {
662 status |= 2;
663 } else if (strlencmp(cmd, "disable") == 0) {
664 status &= ~2;
665 } else
666 return -EINVAL;
667 do_cmd = 1;
668 }
669
670 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
671 return -EIO;
672
673 return 0;
674}
675
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400676static int video_supported;
677static int video_orig_autosw;
678
679#define VIDEO_570 1
680#define VIDEO_770 2
681#define VIDEO_NEW 3
682
683static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400685 int ivga;
686
687 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
688 /* G41, assume IVGA doesn't change */
689 vid_handle = vid2_handle;
690
691 if (!vid_handle)
692 /* video switching not supported on R30, R31 */
693 video_supported = 0;
694 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
695 /* 570 */
696 video_supported = VIDEO_570;
697 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
698 /* 600e/x, 770e, 770x */
699 video_supported = VIDEO_770;
700 else
701 /* all others */
702 video_supported = VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 return 0;
705}
706
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400707static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 int status = 0;
710 int i;
711
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400712 if (video_supported == VIDEO_570) {
713 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
714 status = i & 3;
715 } else if (video_supported == VIDEO_770) {
716 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
717 status |= 0x01 * i;
718 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
719 status |= 0x02 * i;
720 } else if (video_supported == VIDEO_NEW) {
721 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
722 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
723 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400725 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
726 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
727 status |= 0x01 * i;
728 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
729 status |= 0x08 * i;
730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 return status;
733}
734
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400735static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400737 int autosw = 0;
738
739 if (video_supported == VIDEO_570)
740 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
741 else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
742 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
743
744 return autosw & 1;
745}
746
747static int video_read(char *p)
748{
749 int status = video_status();
750 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int len = 0;
752
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400753 if (!video_supported) {
754 len += sprintf(p + len, "status:\t\tnot supported\n");
755 return len;
756 }
757
758 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
760 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400761 if (video_supported == VIDEO_NEW)
762 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
763 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
764 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
765 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
766 if (video_supported == VIDEO_NEW)
767 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
768 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
770
771 return len;
772}
773
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400774static int video_switch(void)
775{
776 int autosw = video_autosw();
777 int ret;
778
779 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
780 return -EIO;
781 ret = video_supported == VIDEO_570 ?
782 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
783 acpi_evalf(vid_handle, NULL, "VSWT", "v");
784 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
785
786 return ret;
787}
788
789static int video_expand(void)
790{
791 if (video_supported == VIDEO_570)
792 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
793 else if (video_supported == VIDEO_770)
794 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
795 else
796 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
797}
798
799static int video_switch2(int status)
800{
801 int ret;
802
803 if (video_supported == VIDEO_570) {
804 ret = acpi_evalf(NULL, NULL,
805 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
806 } else if (video_supported == VIDEO_770) {
807 int autosw = video_autosw();
808 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
809 return -EIO;
810
811 ret = acpi_evalf(vid_handle, NULL,
812 "ASWT", "vdd", status * 0x100, 0);
813
814 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
815 } else {
816 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
817 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
818 }
819
820 return ret;
821}
822
823static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 char *cmd;
826 int enable, disable, status;
827
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400828 if (!video_supported)
829 return -ENODEV;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 enable = disable = 0;
832
833 while ((cmd = next_cmd(&buf))) {
834 if (strlencmp(cmd, "lcd_enable") == 0) {
835 enable |= 0x01;
836 } else if (strlencmp(cmd, "lcd_disable") == 0) {
837 disable |= 0x01;
838 } else if (strlencmp(cmd, "crt_enable") == 0) {
839 enable |= 0x02;
840 } else if (strlencmp(cmd, "crt_disable") == 0) {
841 disable |= 0x02;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400842 } else if (video_supported == VIDEO_NEW &&
843 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 enable |= 0x08;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400845 } else if (video_supported == VIDEO_NEW &&
846 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 disable |= 0x08;
848 } else if (strlencmp(cmd, "auto_enable") == 0) {
849 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
850 return -EIO;
851 } else if (strlencmp(cmd, "auto_disable") == 0) {
852 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
853 return -EIO;
854 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400855 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return -EIO;
857 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400858 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return -EIO;
860 } else
861 return -EINVAL;
862 }
863
864 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400865 status = (video_status() & 0x0f & ~disable) | enable;
866 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -EIO;
868 }
869
870 return 0;
871}
872
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400873static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400875 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400878static int light_supported;
879static int light_status_supported;
880
881static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400883 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
884 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
885
886 if (light_supported)
887 /* light status not supported on
888 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
889 light_status_supported = acpi_evalf(ec_handle, NULL,
890 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 return 0;
893}
894
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400895static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 int len = 0;
898 int status = 0;
899
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400900 if (!light_supported) {
901 len += sprintf(p + len, "status:\t\tnot supported\n");
902 } else if (!light_status_supported) {
903 len += sprintf(p + len, "status:\t\tunknown\n");
904 len += sprintf(p + len, "commands:\ton, off\n");
905 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
907 return -EIO;
908 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400909 len += sprintf(p + len, "commands:\ton, off\n");
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 return len;
913}
914
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400915static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 int cmos_cmd, lght_cmd;
918 char *cmd;
919 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400920
921 if (!light_supported)
922 return -ENODEV;
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 while ((cmd = next_cmd(&buf))) {
925 if (strlencmp(cmd, "on") == 0) {
926 cmos_cmd = 0x0c;
927 lght_cmd = 1;
928 } else if (strlencmp(cmd, "off") == 0) {
929 cmos_cmd = 0x0d;
930 lght_cmd = 0;
931 } else
932 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400935 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
936 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!success)
938 return -EIO;
939 }
940
941 return 0;
942}
943
944static int _sta(acpi_handle handle)
945{
946 int status;
947
948 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
949 status = 0;
950
951 return status;
952}
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200953
Kristen Accardi63e5f242006-02-23 17:56:06 -0800954#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955#define dock_docked() (_sta(dock_handle) & 1)
956
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400957static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 int len = 0;
960 int docked = dock_docked();
961
962 if (!dock_handle)
963 len += sprintf(p + len, "status:\t\tnot supported\n");
964 else if (!docked)
965 len += sprintf(p + len, "status:\t\tundocked\n");
966 else {
967 len += sprintf(p + len, "status:\t\tdocked\n");
968 len += sprintf(p + len, "commands:\tdock, undock\n");
969 }
970
971 return len;
972}
973
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400974static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 char *cmd;
977
978 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400979 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 while ((cmd = next_cmd(&buf))) {
982 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400983 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
984 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -EIO;
986 } else if (strlencmp(cmd, "dock") == 0) {
987 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
988 return -EIO;
989 } else
990 return -EINVAL;
991 }
992
993 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400994}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996static void dock_notify(struct ibm_struct *ibm, u32 event)
997{
998 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400999 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001001 if (event == 1 && !pci) /* 570 */
1002 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1003 else if (event == 1 && pci) /* 570 */
1004 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1005 else if (event == 3 && docked)
1006 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001008 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001010 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 else {
1012 printk(IBM_ERR "unknown dock event %d, status %d\n",
1013 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001014 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001017#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001019static int bay_status_supported;
1020static int bay_status2_supported;
1021static int bay_eject_supported;
1022static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001024static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001026 bay_status_supported = bay_handle &&
1027 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1028 bay_status2_supported = bay2_handle &&
1029 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1030
1031 bay_eject_supported = bay_handle && bay_ej_handle &&
1032 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1033 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1034 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 return 0;
1037}
1038
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001039#define bay_occupied(b) (_sta(b##_handle) & 1)
1040
1041static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001044 int occupied = bay_occupied(bay);
1045 int occupied2 = bay_occupied(bay2);
1046 int eject, eject2;
1047
1048 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1049 (occupied ? "occupied" : "unoccupied") :
1050 "not supported");
1051 if (bay_status2_supported)
1052 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1053 "occupied" : "unoccupied");
1054
1055 eject = bay_eject_supported && occupied;
1056 eject2 = bay_eject2_supported && occupied2;
1057
1058 if (eject && eject2)
1059 len += sprintf(p + len, "commands:\teject, eject2\n");
1060 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001062 else if (eject2)
1063 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 return len;
1066}
1067
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001068static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 char *cmd;
1071
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001072 if (!bay_eject_supported && !bay_eject2_supported)
1073 return -ENODEV;
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001076 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1077 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1078 return -EIO;
1079 } else if (bay_eject2_supported &&
1080 strlencmp(cmd, "eject2") == 0) {
1081 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -EIO;
1083 } else
1084 return -EINVAL;
1085 }
1086
1087 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001088}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090static void bay_notify(struct ibm_struct *ibm, u32 event)
1091{
1092 acpi_bus_generate_event(ibm->device, event, 0);
1093}
1094
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001095static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 int len = 0;
1098
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001099 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1100 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (!cmos_handle)
1102 len += sprintf(p + len, "status:\t\tnot supported\n");
1103 else {
1104 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001105 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
1108 return len;
1109}
1110
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001111static int cmos_eval(int cmos_cmd)
1112{
1113 if (cmos_handle)
1114 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1115 else
1116 return 1;
1117}
1118
1119static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 char *cmd;
1122 int cmos_cmd;
1123
1124 if (!cmos_handle)
1125 return -EINVAL;
1126
1127 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001128 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1129 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* cmos_cmd set */
1131 } else
1132 return -EINVAL;
1133
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001134 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return -EIO;
1136 }
1137
1138 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001139}
1140
1141static int led_supported;
1142
1143#define LED_570 1
1144#define LED_OLD 2
1145#define LED_NEW 3
1146
1147static int led_init(void)
1148{
1149 if (!led_handle)
1150 /* led not supported on R30, R31 */
1151 led_supported = 0;
1152 else if (strlencmp(led_path, "SLED") == 0)
1153 /* 570 */
1154 led_supported = LED_570;
1155 else if (strlencmp(led_path, "SYSL") == 0)
1156 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1157 led_supported = LED_OLD;
1158 else
1159 /* all others */
1160 led_supported = LED_NEW;
1161
1162 return 0;
1163}
1164
1165#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1166
1167static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 int len = 0;
1170
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001171 if (!led_supported) {
1172 len += sprintf(p + len, "status:\t\tnot supported\n");
1173 return len;
1174 }
1175 len += sprintf(p + len, "status:\t\tsupported\n");
1176
1177 if (led_supported == LED_570) {
1178 /* 570 */
1179 int i, status;
1180 for (i = 0; i < 8; i++) {
1181 if (!acpi_evalf(ec_handle,
1182 &status, "GLED", "dd", 1 << i))
1183 return -EIO;
1184 len += sprintf(p + len, "%d:\t\t%s\n",
1185 i, led_status(status));
1186 }
1187 }
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001190 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 return len;
1193}
1194
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001195/* off, on, blink */
1196static const int led_sled_arg1[] = { 0, 1, 3 };
1197static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1198static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1199static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1200
1201#define EC_HLCL 0x0c
1202#define EC_HLBL 0x0d
1203#define EC_HLMS 0x0e
1204
1205static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001208 int led, ind, ret;
1209
1210 if (!led_supported)
1211 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001214 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return -EINVAL;
1216
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001217 if (strstr(cmd, "off")) {
1218 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001220 ind = 1;
1221 } else if (strstr(cmd, "blink")) {
1222 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 } else
1224 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001225
1226 if (led_supported == LED_570) {
1227 /* 570 */
1228 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001230 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001232 } else if (led_supported == LED_OLD) {
1233 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1234 led = 1 << led;
1235 ret = ec_write(EC_HLMS, led);
1236 if (ret >= 0)
1237 ret =
1238 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1239 if (ret >= 0)
1240 ret =
1241 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1242 if (ret < 0)
1243 return ret;
1244 } else {
1245 /* all others */
1246 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1247 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
1252 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001253}
1254
1255static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 int len = 0;
1258
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001259 if (!beep_handle)
1260 len += sprintf(p + len, "status:\t\tnot supported\n");
1261 else {
1262 len += sprintf(p + len, "status:\t\tsupported\n");
1263 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 return len;
1267}
1268
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001269static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 char *cmd;
1272 int beep_cmd;
1273
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001274 if (!beep_handle)
1275 return -ENODEV;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001278 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1279 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* beep_cmd set */
1281 } else
1282 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001283 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -EIO;
1285 }
1286
1287 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001288}
1289
1290static int acpi_ec_read(int i, u8 * p)
1291{
1292 int v;
1293
1294 if (ecrd_handle) {
1295 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1296 return 0;
1297 *p = v;
1298 } else {
1299 if (ec_read(i, p) < 0)
1300 return 0;
1301 }
1302
1303 return 1;
1304}
1305
1306static int acpi_ec_write(int i, u8 v)
1307{
1308 if (ecwr_handle) {
1309 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1310 return 0;
1311 } else {
1312 if (ec_write(i, v) < 0)
1313 return 0;
1314 }
1315
1316 return 1;
1317}
1318
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001319static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001320
1321static int thermal_init(void)
1322{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001323 u8 t, ta1, ta2;
1324 int i;
1325 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1326
1327 if (ibm_thinkpad_ec_found && experimental) {
1328 /*
1329 * Direct EC access mode: sensors at registers
1330 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1331 * non-implemented, thermal sensors return 0x80 when
1332 * not available
1333 */
1334
1335 ta1 = ta2 = 0;
1336 for (i = 0; i < 8; i++) {
1337 if (likely(acpi_ec_read(0x78 + i, &t))) {
1338 ta1 |= t;
1339 } else {
1340 ta1 = 0;
1341 break;
1342 }
1343 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1344 ta2 |= t;
1345 } else {
1346 ta1 = 0;
1347 break;
1348 }
1349 }
1350 if (ta1 == 0) {
1351 /* This is sheer paranoia, but we handle it anyway */
1352 if (acpi_tmp7) {
1353 printk(IBM_ERR
1354 "ThinkPad ACPI EC access misbehaving, "
1355 "falling back to ACPI TMPx access mode\n");
1356 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1357 } else {
1358 printk(IBM_ERR
1359 "ThinkPad ACPI EC access misbehaving, "
1360 "disabling thermal sensors access\n");
1361 thermal_read_mode = IBMACPI_THERMAL_NONE;
1362 }
1363 } else {
1364 thermal_read_mode =
1365 (ta2 != 0) ?
1366 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1367 }
1368 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001369 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1370 /* 600e/x, 770e, 770x */
1371 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1372 } else {
1373 /* Standard ACPI TMPx access, max 8 sensors */
1374 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1375 }
1376 } else {
1377 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1378 thermal_read_mode = IBMACPI_THERMAL_NONE;
1379 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001380
1381 return 0;
1382}
1383
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001384static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1385{
1386 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001387 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001388 char tmpi[] = "TMPi";
1389
1390 if (!s)
1391 return -EINVAL;
1392
1393 switch (thermal_read_mode) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001394#if IBMACPI_MAX_THERMAL_SENSORS >= 16
1395 case IBMACPI_THERMAL_TPEC_16:
1396 for (i = 0; i < 8; i++) {
1397 if (!acpi_ec_read(0xC0 + i, &tmp))
1398 return -EIO;
1399 s->temp[i + 8] = tmp * 1000;
1400 }
1401 /* fallthrough */
1402#endif
1403 case IBMACPI_THERMAL_TPEC_8:
1404 for (i = 0; i < 8; i++) {
1405 if (!acpi_ec_read(0x78 + i, &tmp))
1406 return -EIO;
1407 s->temp[i] = tmp * 1000;
1408 }
1409 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1410
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001411 case IBMACPI_THERMAL_ACPI_UPDT:
1412 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1413 return -EIO;
1414 for (i = 0; i < 8; i++) {
1415 tmpi[3] = '0' + i;
1416 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1417 return -EIO;
1418 s->temp[i] = (t - 2732) * 100;
1419 }
1420 return 8;
1421
1422 case IBMACPI_THERMAL_ACPI_TMP07:
1423 for (i = 0; i < 8; i++) {
1424 tmpi[3] = '0' + i;
1425 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1426 return -EIO;
1427 s->temp[i] = t * 1000;
1428 }
1429 return 8;
1430
1431 case IBMACPI_THERMAL_NONE:
1432 default:
1433 return 0;
1434 }
1435}
1436
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001437static int thermal_read(char *p)
1438{
1439 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001440 int n, i;
1441 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001442
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001443 n = thermal_get_sensors(&t);
1444 if (unlikely(n < 0))
1445 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001446
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001447 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001448
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001449 if (n > 0) {
1450 for (i = 0; i < (n - 1); i++)
1451 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1452 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1453 } else
1454 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001455
1456 return len;
1457}
1458
1459static u8 ecdump_regs[256];
1460
1461static int ecdump_read(char *p)
1462{
1463 int len = 0;
1464 int i, j;
1465 u8 v;
1466
1467 len += sprintf(p + len, "EC "
1468 " +00 +01 +02 +03 +04 +05 +06 +07"
1469 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1470 for (i = 0; i < 256; i += 16) {
1471 len += sprintf(p + len, "EC 0x%02x:", i);
1472 for (j = 0; j < 16; j++) {
1473 if (!acpi_ec_read(i + j, &v))
1474 break;
1475 if (v != ecdump_regs[i + j])
1476 len += sprintf(p + len, " *%02x", v);
1477 else
1478 len += sprintf(p + len, " %02x", v);
1479 ecdump_regs[i + j] = v;
1480 }
1481 len += sprintf(p + len, "\n");
1482 if (j != 16)
1483 break;
1484 }
1485
1486 /* These are way too dangerous to advertise openly... */
1487#if 0
1488 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1489 " (<offset> is 00-ff, <value> is 00-ff)\n");
1490 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1491 " (<offset> is 00-ff, <value> is 0-255)\n");
1492#endif
1493 return len;
1494}
1495
1496static int ecdump_write(char *buf)
1497{
1498 char *cmd;
1499 int i, v;
1500
1501 while ((cmd = next_cmd(&buf))) {
1502 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1503 /* i and v set */
1504 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1505 /* i and v set */
1506 } else
1507 return -EINVAL;
1508 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1509 if (!acpi_ec_write(i, v))
1510 return -EIO;
1511 } else
1512 return -EINVAL;
1513 }
1514
1515 return 0;
1516}
1517
1518static int brightness_offset = 0x31;
1519
Holger Macht8acb0252006-10-20 14:30:28 -07001520static int brightness_get(struct backlight_device *bd)
1521{
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001522 u8 level;
1523 if (!acpi_ec_read(brightness_offset, &level))
1524 return -EIO;
Holger Macht8acb0252006-10-20 14:30:28 -07001525
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001526 level &= 0x7;
1527 return level;
Holger Macht8acb0252006-10-20 14:30:28 -07001528}
1529
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001530static int brightness_read(char *p)
1531{
1532 int len = 0;
Holger Macht8acb0252006-10-20 14:30:28 -07001533 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001534
Holger Macht8acb0252006-10-20 14:30:28 -07001535 if ((level = brightness_get(NULL)) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001536 len += sprintf(p + len, "level:\t\tunreadable\n");
1537 } else {
1538 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1539 len += sprintf(p + len, "commands:\tup, down\n");
1540 len += sprintf(p + len, "commands:\tlevel <level>"
1541 " (<level> is 0-7)\n");
1542 }
1543
1544 return len;
1545}
1546
1547#define BRIGHTNESS_UP 4
1548#define BRIGHTNESS_DOWN 5
1549
Holger Macht8acb0252006-10-20 14:30:28 -07001550static int brightness_set(int value)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001551{
1552 int cmos_cmd, inc, i;
Holger Macht8acb0252006-10-20 14:30:28 -07001553 int current_value = brightness_get(NULL);
1554
1555 value &= 7;
1556
1557 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1558 inc = value > current_value ? 1 : -1;
1559 for (i = current_value; i != value; i += inc) {
1560 if (!cmos_eval(cmos_cmd))
1561 return -EIO;
1562 if (!acpi_ec_write(brightness_offset, i + inc))
1563 return -EIO;
1564 }
1565
1566 return 0;
1567}
1568
1569static int brightness_write(char *buf)
1570{
1571 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001572 int new_level;
1573 char *cmd;
1574
1575 while ((cmd = next_cmd(&buf))) {
Holger Macht8acb0252006-10-20 14:30:28 -07001576 if ((level = brightness_get(NULL)) < 0)
1577 return level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001578 level &= 7;
1579
1580 if (strlencmp(cmd, "up") == 0) {
1581 new_level = level == 7 ? 7 : level + 1;
1582 } else if (strlencmp(cmd, "down") == 0) {
1583 new_level = level == 0 ? 0 : level - 1;
1584 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1585 new_level >= 0 && new_level <= 7) {
1586 /* new_level set */
1587 } else
1588 return -EINVAL;
1589
Holger Macht8acb0252006-10-20 14:30:28 -07001590 brightness_set(new_level);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001591 }
1592
1593 return 0;
1594}
1595
Holger Macht8acb0252006-10-20 14:30:28 -07001596static int brightness_update_status(struct backlight_device *bd)
1597{
1598 return brightness_set(bd->props->brightness);
1599}
1600
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001601static int volume_offset = 0x30;
1602
1603static int volume_read(char *p)
1604{
1605 int len = 0;
1606 u8 level;
1607
1608 if (!acpi_ec_read(volume_offset, &level)) {
1609 len += sprintf(p + len, "level:\t\tunreadable\n");
1610 } else {
1611 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1612 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1613 len += sprintf(p + len, "commands:\tup, down, mute\n");
1614 len += sprintf(p + len, "commands:\tlevel <level>"
1615 " (<level> is 0-15)\n");
1616 }
1617
1618 return len;
1619}
1620
1621#define VOLUME_DOWN 0
1622#define VOLUME_UP 1
1623#define VOLUME_MUTE 2
1624
1625static int volume_write(char *buf)
1626{
1627 int cmos_cmd, inc, i;
1628 u8 level, mute;
1629 int new_level, new_mute;
1630 char *cmd;
1631
1632 while ((cmd = next_cmd(&buf))) {
1633 if (!acpi_ec_read(volume_offset, &level))
1634 return -EIO;
1635 new_mute = mute = level & 0x40;
1636 new_level = level = level & 0xf;
1637
1638 if (strlencmp(cmd, "up") == 0) {
1639 if (mute)
1640 new_mute = 0;
1641 else
1642 new_level = level == 15 ? 15 : level + 1;
1643 } else if (strlencmp(cmd, "down") == 0) {
1644 if (mute)
1645 new_mute = 0;
1646 else
1647 new_level = level == 0 ? 0 : level - 1;
1648 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1649 new_level >= 0 && new_level <= 15) {
1650 /* new_level set */
1651 } else if (strlencmp(cmd, "mute") == 0) {
1652 new_mute = 0x40;
1653 } else
1654 return -EINVAL;
1655
1656 if (new_level != level) { /* mute doesn't change */
1657 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1658 inc = new_level > level ? 1 : -1;
1659
1660 if (mute && (!cmos_eval(cmos_cmd) ||
1661 !acpi_ec_write(volume_offset, level)))
1662 return -EIO;
1663
1664 for (i = level; i != new_level; i += inc)
1665 if (!cmos_eval(cmos_cmd) ||
1666 !acpi_ec_write(volume_offset, i + inc))
1667 return -EIO;
1668
1669 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1670 !acpi_ec_write(volume_offset,
1671 new_level + mute)))
1672 return -EIO;
1673 }
1674
1675 if (new_mute != mute) { /* level doesn't change */
1676 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1677
1678 if (!cmos_eval(cmos_cmd) ||
1679 !acpi_ec_write(volume_offset, level + new_mute))
1680 return -EIO;
1681 }
1682 }
1683
1684 return 0;
1685}
1686
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001687static enum fan_status_access_mode fan_status_access_mode;
1688static enum fan_control_access_mode fan_control_access_mode;
1689static enum fan_control_commands fan_control_commands;
1690
1691static int fan_init(void)
1692{
1693 u8 status;
1694
1695 fan_status_access_mode = IBMACPI_FAN_NONE;
1696 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1697 fan_control_commands = 0;
1698
1699 if (gfan_handle) {
1700 /* 570, 600e/x, 770e, 770x */
1701 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1702 } else {
1703 /* all other ThinkPads: note that even old-style
1704 * ThinkPad ECs supports the fan control register */
1705 if (likely(acpi_ec_read(fan_status_offset, &status))) {
1706 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1707 } else {
1708 printk(IBM_ERR
1709 "ThinkPad ACPI EC access misbehaving, "
1710 "fan status and control unavailable\n");
1711 return 0;
1712 }
1713 }
1714
1715 if (sfan_handle) {
1716 /* 570, 770x-JL */
1717 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
1718 fan_control_commands |= IBMACPI_FAN_CMD_LEVEL;
1719 } else {
1720 if (!gfan_handle) {
1721 /* gfan without sfan means no fan control */
1722 /* all other models implement TP EC 0x2f control */
1723
1724 if (fans_handle) {
1725 /* X31, X40 */
1726 fan_control_access_mode =
1727 IBMACPI_FAN_WR_ACPI_FANS;
1728 fan_control_commands |=
1729 IBMACPI_FAN_CMD_SPEED |
1730 IBMACPI_FAN_CMD_ENABLE;
1731 } else {
1732 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
1733 fan_control_commands |= IBMACPI_FAN_CMD_ENABLE;
1734 }
1735 }
1736 }
1737
1738 return 0;
1739}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001740
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001741static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001742{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001743 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001744
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001745 switch (fan_status_access_mode) {
1746 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001747 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001748
1749 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001750 return -EIO;
1751
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001752 if (likely(status))
1753 *status = s & 0x07;
1754
1755 break;
1756
1757 case IBMACPI_FAN_RD_TPEC:
1758 /* all except 570, 600e/x, 770e, 770x */
1759 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1760 return -EIO;
1761
1762 if (likely(status))
1763 *status = s;
1764
1765 break;
1766
1767 default:
1768 return -ENXIO;
1769 }
1770
1771 return 0;
1772}
1773
1774static int fan_get_speed(unsigned int *speed)
1775{
1776 u8 hi, lo;
1777
1778 switch (fan_status_access_mode) {
1779 case IBMACPI_FAN_RD_TPEC:
1780 /* all except 570, 600e/x, 770e, 770x */
1781 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1782 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1783 return -EIO;
1784
1785 if (likely(speed))
1786 *speed = (hi << 8) | lo;
1787
1788 break;
1789
1790 default:
1791 return -ENXIO;
1792 }
1793
1794 return 0;
1795}
1796
1797static int fan_read(char *p)
1798{
1799 int len = 0;
1800 int rc;
1801 u8 status;
1802 unsigned int speed = 0;
1803
1804 switch (fan_status_access_mode) {
1805 case IBMACPI_FAN_RD_ACPI_GFAN:
1806 /* 570, 600e/x, 770e, 770x */
1807 if ((rc = fan_get_status(&status)) < 0)
1808 return rc;
1809
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001810 len += sprintf(p + len, "level:\t\t%d\n", status);
1811
1812 break;
1813
1814 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001815 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001816 if ((rc = fan_get_status(&status)) < 0)
1817 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001818
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001819 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 7));
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001820
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001821 if ((rc = fan_get_speed(&speed)) < 0)
1822 return rc;
1823
1824 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001825 break;
1826
1827 case IBMACPI_FAN_NONE:
1828 default:
1829 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001830 }
1831
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001832 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001833 len += sprintf(p + len, "commands:\tlevel <level>"
1834 " (<level> is 0-7)\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001835
1836 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001837 len += sprintf(p + len, "commands:\tenable, disable\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001838
1839 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001840 len += sprintf(p + len, "commands:\tspeed <speed>"
1841 " (<speed> is 0-65535)\n");
1842
1843 return len;
1844}
1845
1846static int fan_write(char *buf)
1847{
1848 char *cmd;
1849 int level, speed;
1850
1851 while ((cmd = next_cmd(&buf))) {
1852 if (sfan_handle &&
1853 sscanf(cmd, "level %d", &level) == 1 &&
1854 level >= 0 && level <= 7) {
1855 /* 570, 770x-JL */
1856 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
1857 return -EIO;
1858 } else if (!gfan_handle && strlencmp(cmd, "enable") == 0) {
1859 /* all except 570, 600e/x, 770e, 770x */
1860 if (!acpi_ec_write(fan_status_offset, 0x80))
1861 return -EIO;
1862 } else if (!gfan_handle && strlencmp(cmd, "disable") == 0) {
1863 /* all except 570, 600e/x, 770e, 770x */
1864 if (!acpi_ec_write(fan_status_offset, 0x00))
1865 return -EIO;
1866 } else if (fans_handle &&
1867 sscanf(cmd, "speed %d", &speed) == 1 &&
1868 speed >= 0 && speed <= 65535) {
1869 /* X31, X40 */
1870 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
1871 speed, speed, speed))
1872 return -EIO;
1873 } else
1874 return -EINVAL;
1875 }
1876
1877 return 0;
1878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880static struct ibm_struct ibms[] = {
1881 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001882 .name = "driver",
1883 .init = driver_init,
1884 .read = driver_read,
1885 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001887 .name = "hotkey",
1888 .hid = IBM_HKEY_HID,
1889 .init = hotkey_init,
1890 .read = hotkey_read,
1891 .write = hotkey_write,
1892 .exit = hotkey_exit,
1893 .notify = hotkey_notify,
1894 .handle = &hkey_handle,
1895 .type = ACPI_DEVICE_NOTIFY,
1896 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001898 .name = "bluetooth",
1899 .init = bluetooth_init,
1900 .read = bluetooth_read,
1901 .write = bluetooth_write,
1902 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001904 .name = "wan",
1905 .init = wan_init,
1906 .read = wan_read,
1907 .write = wan_write,
1908 .experimental = 1,
1909 },
1910 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001911 .name = "video",
1912 .init = video_init,
1913 .read = video_read,
1914 .write = video_write,
1915 .exit = video_exit,
1916 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001918 .name = "light",
1919 .init = light_init,
1920 .read = light_read,
1921 .write = light_write,
1922 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08001923#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001925 .name = "dock",
1926 .read = dock_read,
1927 .write = dock_write,
1928 .notify = dock_notify,
1929 .handle = &dock_handle,
1930 .type = ACPI_SYSTEM_NOTIFY,
1931 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001933 .name = "dock",
1934 .hid = IBM_PCI_HID,
1935 .notify = dock_notify,
1936 .handle = &pci_handle,
1937 .type = ACPI_SYSTEM_NOTIFY,
1938 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08001939#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001941 .name = "bay",
1942 .init = bay_init,
1943 .read = bay_read,
1944 .write = bay_write,
1945 .notify = bay_notify,
1946 .handle = &bay_handle,
1947 .type = ACPI_SYSTEM_NOTIFY,
1948 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001950 .name = "cmos",
1951 .read = cmos_read,
1952 .write = cmos_write,
1953 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001955 .name = "led",
1956 .init = led_init,
1957 .read = led_read,
1958 .write = led_write,
1959 },
1960 {
1961 .name = "beep",
1962 .read = beep_read,
1963 .write = beep_write,
1964 },
1965 {
1966 .name = "thermal",
1967 .init = thermal_init,
1968 .read = thermal_read,
1969 },
1970 {
1971 .name = "ecdump",
1972 .read = ecdump_read,
1973 .write = ecdump_write,
1974 .experimental = 1,
1975 },
1976 {
1977 .name = "brightness",
1978 .read = brightness_read,
1979 .write = brightness_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001980 },
1981 {
1982 .name = "volume",
1983 .read = volume_read,
1984 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001985 },
1986 {
1987 .name = "fan",
1988 .read = fan_read,
1989 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001990 .init = fan_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001991 .experimental = 1,
1992 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995static int dispatch_read(char *page, char **start, off_t off, int count,
1996 int *eof, void *data)
1997{
1998 struct ibm_struct *ibm = (struct ibm_struct *)data;
1999 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (!ibm || !ibm->read)
2002 return -EINVAL;
2003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002004 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (len < 0)
2006 return len;
2007
2008 if (len <= off + count)
2009 *eof = 1;
2010 *start = page + off;
2011 len -= off;
2012 if (len > count)
2013 len = count;
2014 if (len < 0)
2015 len = 0;
2016
2017 return len;
2018}
2019
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002020static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 unsigned long count, void *data)
2022{
2023 struct ibm_struct *ibm = (struct ibm_struct *)data;
2024 char *kernbuf;
2025 int ret;
2026
2027 if (!ibm || !ibm->write)
2028 return -EINVAL;
2029
2030 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2031 if (!kernbuf)
2032 return -ENOMEM;
2033
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002034 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002036 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038
2039 kernbuf[count] = 0;
2040 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002041 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (ret == 0)
2043 ret = count;
2044
2045 kfree(kernbuf);
2046
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002047 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2051{
2052 struct ibm_struct *ibm = (struct ibm_struct *)data;
2053
2054 if (!ibm || !ibm->notify)
2055 return;
2056
2057 ibm->notify(ibm, event);
2058}
2059
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002060static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 acpi_status status;
2063 int ret;
2064
2065 if (!*ibm->handle)
2066 return 0;
2067
2068 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2069 if (ret < 0) {
2070 printk(IBM_ERR "%s device not present\n", ibm->name);
2071 return 0;
2072 }
2073
2074 acpi_driver_data(ibm->device) = ibm;
2075 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2076
2077 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2078 dispatch_notify, ibm);
2079 if (ACPI_FAILURE(status)) {
2080 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2081 ibm->name, status);
2082 return -ENODEV;
2083 }
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return 0;
2086}
2087
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002088static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090 return 0;
2091}
2092
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002093static int __init register_driver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 int ret;
2096
2097 ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2098 if (!ibm->driver) {
2099 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2100 return -1;
2101 }
2102
2103 memset(ibm->driver, 0, sizeof(struct acpi_driver));
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002104 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002106 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 ret = acpi_bus_register_driver(ibm->driver);
2109 if (ret < 0) {
2110 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2111 ibm->hid, ret);
2112 kfree(ibm->driver);
2113 }
2114
2115 return ret;
2116}
2117
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002118static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 int ret;
2121 struct proc_dir_entry *entry;
2122
2123 if (ibm->experimental && !experimental)
2124 return 0;
2125
2126 if (ibm->hid) {
2127 ret = register_driver(ibm);
2128 if (ret < 0)
2129 return ret;
2130 ibm->driver_registered = 1;
2131 }
2132
2133 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002134 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (ret != 0)
2136 return ret;
2137 ibm->init_called = 1;
2138 }
2139
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002140 if (ibm->read) {
2141 entry = create_proc_entry(ibm->name,
2142 S_IFREG | S_IRUGO | S_IWUSR,
2143 proc_dir);
2144 if (!entry) {
2145 printk(IBM_ERR "unable to create proc entry %s\n",
2146 ibm->name);
2147 return -ENODEV;
2148 }
2149 entry->owner = THIS_MODULE;
2150 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002152 if (ibm->write)
2153 entry->write_proc = &dispatch_write;
2154 ibm->proc_created = 1;
2155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 if (ibm->notify) {
2158 ret = setup_notify(ibm);
2159 if (ret < 0)
2160 return ret;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002161 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163
2164 return 0;
2165}
2166
2167static void ibm_exit(struct ibm_struct *ibm)
2168{
2169 if (ibm->notify_installed)
2170 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2171 dispatch_notify);
2172
2173 if (ibm->proc_created)
2174 remove_proc_entry(ibm->name, proc_dir);
2175
2176 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002177 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 if (ibm->driver_registered) {
2180 acpi_bus_unregister_driver(ibm->driver);
2181 kfree(ibm->driver);
2182 }
2183}
2184
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002185static void __init ibm_handle_init(char *name,
2186 acpi_handle * handle, acpi_handle parent,
2187 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 int i;
2190 acpi_status status;
2191
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002192 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002194 if (ACPI_SUCCESS(status)) {
2195 *path = paths[i];
2196 return;
2197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002203#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002205 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207static int set_ibm_param(const char *val, struct kernel_param *kp)
2208{
2209 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002211 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2212 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2213 if (strlen(val) > sizeof(ibms[i].param) - 2)
2214 return -ENOSPC;
2215 strcpy(ibms[i].param, val);
2216 strcat(ibms[i].param, ",");
2217 return 0;
2218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return -EINVAL;
2221}
2222
2223#define IBM_PARAM(feature) \
2224 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2225
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002226IBM_PARAM(hotkey);
2227IBM_PARAM(bluetooth);
2228IBM_PARAM(video);
2229IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002230#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002231IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002232#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002233IBM_PARAM(bay);
2234IBM_PARAM(cmos);
2235IBM_PARAM(led);
2236IBM_PARAM(beep);
2237IBM_PARAM(ecdump);
2238IBM_PARAM(brightness);
2239IBM_PARAM(volume);
2240IBM_PARAM(fan);
2241
Holger Macht8acb0252006-10-20 14:30:28 -07002242static struct backlight_properties ibm_backlight_data = {
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02002243 .owner = THIS_MODULE,
2244 .get_brightness = brightness_get,
2245 .update_status = brightness_update_status,
2246 .max_brightness = 7,
Holger Macht8acb0252006-10-20 14:30:28 -07002247};
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249static void acpi_ibm_exit(void)
2250{
2251 int i;
2252
Holger Macht8acb0252006-10-20 14:30:28 -07002253 if (ibm_backlight_device)
2254 backlight_device_unregister(ibm_backlight_device);
2255
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002256 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 ibm_exit(&ibms[i]);
2258
2259 remove_proc_entry(IBM_DIR, acpi_root_dir);
2260}
2261
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002262static int __init check_dmi_for_ec(void)
2263{
2264 struct dmi_device *dev = NULL;
2265
2266 /*
2267 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2268 * X32 or newer, all Z series; Some models must have an
2269 * up-to-date BIOS or they will not be detected.
2270 *
2271 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2272 */
2273 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2274 if (strstr(dev->name, "IBM ThinkPad Embedded Controller"))
2275 return 1;
2276 }
2277 return 0;
2278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280static int __init acpi_ibm_init(void)
2281{
2282 int ret, i;
2283
2284 if (acpi_disabled)
2285 return -ENODEV;
2286
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002287 if (!acpi_specific_hotkey_enabled) {
2288 printk(IBM_ERR "using generic hotkey driver\n");
2289 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -05002290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002292 /* ec is required because many other handles are relative to it */
2293 IBM_HANDLE_INIT(ec);
2294 if (!ec_handle) {
2295 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002299 /* Models with newer firmware report the EC in DMI */
2300 ibm_thinkpad_ec_found = check_dmi_for_ec();
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002303 IBM_HANDLE_INIT(vid);
2304 IBM_HANDLE_INIT(vid2);
2305 IBM_HANDLE_INIT(ledb);
2306 IBM_HANDLE_INIT(led);
2307 IBM_HANDLE_INIT(hkey);
2308 IBM_HANDLE_INIT(lght);
2309 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002310#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002311 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002312#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002313 IBM_HANDLE_INIT(pci);
2314 IBM_HANDLE_INIT(bay);
2315 if (bay_handle)
2316 IBM_HANDLE_INIT(bay_ej);
2317 IBM_HANDLE_INIT(bay2);
2318 if (bay2_handle)
2319 IBM_HANDLE_INIT(bay2_ej);
2320 IBM_HANDLE_INIT(beep);
2321 IBM_HANDLE_INIT(ecrd);
2322 IBM_HANDLE_INIT(ecwr);
2323 IBM_HANDLE_INIT(fans);
2324 IBM_HANDLE_INIT(gfan);
2325 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2328 if (!proc_dir) {
2329 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2330 return -ENODEV;
2331 }
2332 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002333
2334 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002336 if (ret >= 0 && *ibms[i].param)
2337 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (ret < 0) {
2339 acpi_ibm_exit();
2340 return ret;
2341 }
2342 }
2343
Holger Macht8acb0252006-10-20 14:30:28 -07002344 ibm_backlight_device = backlight_device_register("ibm", NULL,
2345 &ibm_backlight_data);
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02002346 if (IS_ERR(ibm_backlight_device)) {
Holger Macht8acb0252006-10-20 14:30:28 -07002347 printk(IBM_ERR "Could not register ibm backlight device\n");
2348 ibm_backlight_device = NULL;
2349 acpi_ibm_exit();
2350 }
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return 0;
2353}
2354
2355module_init(acpi_ibm_init);
2356module_exit(acpi_ibm_exit);