Merge ../to-linus
[pandora-kernel.git] / drivers / acpi / ibm_acpi.c
1 /*
2  *  ibm_acpi.c - IBM ThinkPad ACPI Extras
3  *
4  *
5  *  Copyright (C) 2004 Borislav Deianov
6  *
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
20  *
21  *  Changelog:
22  *
23  *  2004-08-09  0.1     initial release, support for X series
24  *  2004-08-14  0.2     support for T series, X20
25  *                      bluetooth enable/disable
26  *                      hotkey events disabled by default
27  *                      removed fan control, currently useless
28  *  2004-08-17  0.3     support for R40
29  *                      lcd off, brightness control
30  *                      thinklight on/off
31  *  2004-09-16  0.4     support for module parameters
32  *                      hotkey mask can be prefixed by 0x
33  *                      video output switching
34  *                      video expansion control
35  *                      ultrabay eject support
36  *                      removed lcd brightness/on/off control, didn't work
37  *  2004-10-18  0.5     thinklight support on A21e, G40, R32, T20, T21, X20
38  *                      proc file format changed
39  *                      video_switch command
40  *                      experimental cmos control
41  *                      experimental led control
42  *                      experimental acpi sounds
43  *  2004-10-19  0.6     use acpi_bus_register_driver() to claim HKEY device
44  *  2004-10-23  0.7     fix module loading on A21e, A22p, T20, T21, X20
45  *                      fix LED control on A21e
46  *  2004-11-08  0.8     fix init error case, don't return from a macro
47  *                              thanks to Chris Wright <chrisw@osdl.org>
48  */
49
50 #define IBM_VERSION "0.8"
51
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/proc_fs.h>
57 #include <asm/uaccess.h>
58
59 #include <acpi/acpi_drivers.h>
60 #include <acpi/acnamesp.h>
61
62 #define IBM_NAME "ibm"
63 #define IBM_DESC "IBM ThinkPad ACPI Extras"
64 #define IBM_FILE "ibm_acpi"
65 #define IBM_URL "http://ibm-acpi.sf.net/"
66
67 #define IBM_DIR IBM_NAME
68
69 #define IBM_LOG IBM_FILE ": "
70 #define IBM_ERR    KERN_ERR    IBM_LOG
71 #define IBM_NOTICE KERN_NOTICE IBM_LOG
72 #define IBM_INFO   KERN_INFO   IBM_LOG
73 #define IBM_DEBUG  KERN_DEBUG  IBM_LOG
74
75 #define IBM_MAX_ACPI_ARGS 3
76
77 #define __unused __attribute__ ((unused))
78
79 static int experimental;
80 module_param(experimental, int, 0);
81
82 static acpi_handle root_handle = NULL;
83
84 #define IBM_HANDLE(object, parent, paths...)                    \
85         static acpi_handle  object##_handle;                    \
86         static acpi_handle *object##_parent = &parent##_handle; \
87         static char        *object##_paths[] = { paths }
88
89 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC",       /* A21e, A22p, T20, T21, X20 */
90            "\\_SB.PCI0.LPC.EC", /* all others */
91     );
92
93 IBM_HANDLE(vid, root, "\\_SB.PCI0.VID", /* A21e, G40, X30, X40 */
94            "\\_SB.PCI0.AGP.VID",        /* all others */
95     );
96
97 IBM_HANDLE(cmos, root, "\\UCMS",        /* R50, R50p, R51, T4x, X31, X40 */
98            "\\CMOS",            /* A3x, G40, R32, T23, T30, X22, X24, X30 */
99            "\\CMS",             /* R40, R40e */
100     );                          /* A21e, A22p, T20, T21, X20 */
101
102 IBM_HANDLE(dock, root, "\\_SB.GDCK",    /* X30, X31, X40 */
103            "\\_SB.PCI0.DOCK",   /* A22p, T20, T21, X20 */
104            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
105     );                          /* A21e, G40, R32, R40, R40e */
106
107 IBM_HANDLE(bay, root, "\\_SB.PCI0.IDE0.SCND.MSTR");     /* all except A21e */
108 IBM_HANDLE(bayej, root, "\\_SB.PCI0.IDE0.SCND.MSTR._EJ0");      /* all except A2x, A3x */
109
110 IBM_HANDLE(lght, root, "\\LGHT");       /* A21e, A22p, T20, T21, X20 */
111 IBM_HANDLE(hkey, ec, "HKEY");   /* all */
112 IBM_HANDLE(led, ec, "LED");     /* all except A21e, A22p, T20, T21, X20 */
113 IBM_HANDLE(sysl, ec, "SYSL");   /* A21e, A22p, T20, T21, X20 */
114 IBM_HANDLE(bled, ec, "BLED");   /* A22p, T20, T21, X20 */
115 IBM_HANDLE(beep, ec, "BEEP");   /* all models */
116
117 struct ibm_struct {
118         char *name;
119
120         char *hid;
121         struct acpi_driver *driver;
122
123         int (*init) (struct ibm_struct *);
124         int (*read) (struct ibm_struct *, char *);
125         int (*write) (struct ibm_struct *, char *);
126         void (*exit) (struct ibm_struct *);
127
128         void (*notify) (struct ibm_struct *, u32);
129         acpi_handle *handle;
130         int type;
131         struct acpi_device *device;
132
133         int driver_registered;
134         int proc_created;
135         int init_called;
136         int notify_installed;
137
138         int supported;
139         union {
140                 struct {
141                         int status;
142                         int mask;
143                 } hotkey;
144                 struct {
145                         int autoswitch;
146                 } video;
147         } state;
148
149         int experimental;
150 };
151
152 static struct proc_dir_entry *proc_dir = NULL;
153
154 #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
155 #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
156 #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
157
158 static int acpi_evalf(acpi_handle handle,
159                       void *res, char *method, char *fmt, ...)
160 {
161         char *fmt0 = fmt;
162         struct acpi_object_list params;
163         union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
164         struct acpi_buffer result;
165         union acpi_object out_obj;
166         acpi_status status;
167         va_list ap;
168         char res_type;
169         int success;
170         int quiet;
171
172         if (!*fmt) {
173                 printk(IBM_ERR "acpi_evalf() called with empty format\n");
174                 return 0;
175         }
176
177         if (*fmt == 'q') {
178                 quiet = 1;
179                 fmt++;
180         } else
181                 quiet = 0;
182
183         res_type = *(fmt++);
184
185         params.count = 0;
186         params.pointer = &in_objs[0];
187
188         va_start(ap, fmt);
189         while (*fmt) {
190                 char c = *(fmt++);
191                 switch (c) {
192                 case 'd':       /* int */
193                         in_objs[params.count].integer.value = va_arg(ap, int);
194                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
195                         break;
196                         /* add more types as needed */
197                 default:
198                         printk(IBM_ERR "acpi_evalf() called "
199                                "with invalid format character '%c'\n", c);
200                         return 0;
201                 }
202         }
203         va_end(ap);
204
205         result.length = sizeof(out_obj);
206         result.pointer = &out_obj;
207
208         status = acpi_evaluate_object(handle, method, &params, &result);
209
210         switch (res_type) {
211         case 'd':               /* int */
212                 if (res)
213                         *(int *)res = out_obj.integer.value;
214                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
215                 break;
216         case 'v':               /* void */
217                 success = status == AE_OK;
218                 break;
219                 /* add more types as needed */
220         default:
221                 printk(IBM_ERR "acpi_evalf() called "
222                        "with invalid format character '%c'\n", res_type);
223                 return 0;
224         }
225
226         if (!success && !quiet)
227                 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
228                        method, fmt0, status);
229
230         return success;
231 }
232
233 static void __unused acpi_print_int(acpi_handle handle, char *method)
234 {
235         int i;
236
237         if (acpi_evalf(handle, &i, method, "d"))
238                 printk(IBM_INFO "%s = 0x%x\n", method, i);
239         else
240                 printk(IBM_ERR "error calling %s\n", method);
241 }
242
243 static char *next_cmd(char **cmds)
244 {
245         char *start = *cmds;
246         char *end;
247
248         while ((end = strchr(start, ',')) && end == start)
249                 start = end + 1;
250
251         if (!end)
252                 return NULL;
253
254         *end = 0;
255         *cmds = end + 1;
256         return start;
257 }
258
259 static int driver_init(struct ibm_struct *ibm)
260 {
261         printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
262         printk(IBM_INFO "%s\n", IBM_URL);
263
264         return 0;
265 }
266
267 static int driver_read(struct ibm_struct *ibm, char *p)
268 {
269         int len = 0;
270
271         len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
272         len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
273
274         return len;
275 }
276
277 static int hotkey_get(struct ibm_struct *ibm, int *status, int *mask)
278 {
279         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
280                 return -EIO;
281         if (ibm->supported) {
282                 if (!acpi_evalf(hkey_handle, mask, "DHKN", "qd"))
283                         return -EIO;
284         } else {
285                 *mask = ibm->state.hotkey.mask;
286         }
287         return 0;
288 }
289
290 static int hotkey_set(struct ibm_struct *ibm, int status, int mask)
291 {
292         int i;
293
294         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
295                 return -EIO;
296
297         if (!ibm->supported)
298                 return 0;
299
300         for (i = 0; i < 32; i++) {
301                 int bit = ((1 << i) & mask) != 0;
302                 if (!acpi_evalf(hkey_handle, NULL, "MHKM", "vdd", i + 1, bit))
303                         return -EIO;
304         }
305
306         return 0;
307 }
308
309 static int hotkey_init(struct ibm_struct *ibm)
310 {
311         int ret;
312
313         ibm->supported = 1;
314         ret = hotkey_get(ibm,
315                          &ibm->state.hotkey.status, &ibm->state.hotkey.mask);
316         if (ret < 0) {
317                 /* mask not supported on A21e, A22p, T20, T21, X20, X22, X24 */
318                 ibm->supported = 0;
319                 ret = hotkey_get(ibm,
320                                  &ibm->state.hotkey.status,
321                                  &ibm->state.hotkey.mask);
322         }
323
324         return ret;
325 }
326
327 static int hotkey_read(struct ibm_struct *ibm, char *p)
328 {
329         int status, mask;
330         int len = 0;
331
332         if (hotkey_get(ibm, &status, &mask) < 0)
333                 return -EIO;
334
335         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
336         if (ibm->supported) {
337                 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
338                 len += sprintf(p + len,
339                                "commands:\tenable, disable, reset, <mask>\n");
340         } else {
341                 len += sprintf(p + len, "mask:\t\tnot supported\n");
342                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
343         }
344
345         return len;
346 }
347
348 static int hotkey_write(struct ibm_struct *ibm, char *buf)
349 {
350         int status, mask;
351         char *cmd;
352         int do_cmd = 0;
353
354         if (hotkey_get(ibm, &status, &mask) < 0)
355                 return -ENODEV;
356
357         while ((cmd = next_cmd(&buf))) {
358                 if (strlencmp(cmd, "enable") == 0) {
359                         status = 1;
360                 } else if (strlencmp(cmd, "disable") == 0) {
361                         status = 0;
362                 } else if (strlencmp(cmd, "reset") == 0) {
363                         status = ibm->state.hotkey.status;
364                         mask = ibm->state.hotkey.mask;
365                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
366                         /* mask set */
367                 } else if (sscanf(cmd, "%x", &mask) == 1) {
368                         /* mask set */
369                 } else
370                         return -EINVAL;
371                 do_cmd = 1;
372         }
373
374         if (do_cmd && hotkey_set(ibm, status, mask) < 0)
375                 return -EIO;
376
377         return 0;
378 }
379
380 static void hotkey_exit(struct ibm_struct *ibm)
381 {
382         hotkey_set(ibm, ibm->state.hotkey.status, ibm->state.hotkey.mask);
383 }
384
385 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
386 {
387         int hkey;
388
389         if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
390                 acpi_bus_generate_event(ibm->device, event, hkey);
391         else {
392                 printk(IBM_ERR "unknown hotkey event %d\n", event);
393                 acpi_bus_generate_event(ibm->device, event, 0);
394         }
395 }
396
397 static int bluetooth_init(struct ibm_struct *ibm)
398 {
399         /* bluetooth not supported on A21e, G40, T20, T21, X20 */
400         ibm->supported = acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
401
402         return 0;
403 }
404
405 static int bluetooth_status(struct ibm_struct *ibm)
406 {
407         int status;
408
409         if (!ibm->supported || !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
410                 status = 0;
411
412         return status;
413 }
414
415 static int bluetooth_read(struct ibm_struct *ibm, char *p)
416 {
417         int len = 0;
418         int status = bluetooth_status(ibm);
419
420         if (!ibm->supported)
421                 len += sprintf(p + len, "status:\t\tnot supported\n");
422         else if (!(status & 1))
423                 len += sprintf(p + len, "status:\t\tnot installed\n");
424         else {
425                 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
426                 len += sprintf(p + len, "commands:\tenable, disable\n");
427         }
428
429         return len;
430 }
431
432 static int bluetooth_write(struct ibm_struct *ibm, char *buf)
433 {
434         int status = bluetooth_status(ibm);
435         char *cmd;
436         int do_cmd = 0;
437
438         if (!ibm->supported)
439                 return -EINVAL;
440
441         while ((cmd = next_cmd(&buf))) {
442                 if (strlencmp(cmd, "enable") == 0) {
443                         status |= 2;
444                 } else if (strlencmp(cmd, "disable") == 0) {
445                         status &= ~2;
446                 } else
447                         return -EINVAL;
448                 do_cmd = 1;
449         }
450
451         if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
452                 return -EIO;
453
454         return 0;
455 }
456
457 static int video_init(struct ibm_struct *ibm)
458 {
459         if (!acpi_evalf(vid_handle, &ibm->state.video.autoswitch, "^VDEE", "d"))
460                 return -ENODEV;
461
462         return 0;
463 }
464
465 static int video_status(struct ibm_struct *ibm)
466 {
467         int status = 0;
468         int i;
469
470         acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
471         if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
472                 status |= 0x02 * i;
473
474         acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
475         if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
476                 status |= 0x01 * i;
477         if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
478                 status |= 0x08 * i;
479
480         if (acpi_evalf(vid_handle, &i, "^VDEE", "d"))
481                 status |= 0x10 * (i & 1);
482
483         return status;
484 }
485
486 static int video_read(struct ibm_struct *ibm, char *p)
487 {
488         int status = video_status(ibm);
489         int len = 0;
490
491         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
492         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
493         len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
494         len += sprintf(p + len, "auto:\t\t%s\n", enabled(status, 4));
495         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable, "
496                        "crt_enable, crt_disable\n");
497         len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable, "
498                        "auto_enable, auto_disable\n");
499         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
500
501         return len;
502 }
503
504 static int video_write(struct ibm_struct *ibm, char *buf)
505 {
506         char *cmd;
507         int enable, disable, status;
508
509         enable = disable = 0;
510
511         while ((cmd = next_cmd(&buf))) {
512                 if (strlencmp(cmd, "lcd_enable") == 0) {
513                         enable |= 0x01;
514                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
515                         disable |= 0x01;
516                 } else if (strlencmp(cmd, "crt_enable") == 0) {
517                         enable |= 0x02;
518                 } else if (strlencmp(cmd, "crt_disable") == 0) {
519                         disable |= 0x02;
520                 } else if (strlencmp(cmd, "dvi_enable") == 0) {
521                         enable |= 0x08;
522                 } else if (strlencmp(cmd, "dvi_disable") == 0) {
523                         disable |= 0x08;
524                 } else if (strlencmp(cmd, "auto_enable") == 0) {
525                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
526                                 return -EIO;
527                 } else if (strlencmp(cmd, "auto_disable") == 0) {
528                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
529                                 return -EIO;
530                 } else if (strlencmp(cmd, "video_switch") == 0) {
531                         int autoswitch;
532                         if (!acpi_evalf(vid_handle, &autoswitch, "^VDEE", "d"))
533                                 return -EIO;
534                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
535                                 return -EIO;
536                         if (!acpi_evalf(vid_handle, NULL, "VSWT", "v"))
537                                 return -EIO;
538                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd",
539                                         autoswitch))
540                                 return -EIO;
541                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
542                         if (!acpi_evalf(NULL, NULL, "\\VEXP", "v"))
543                                 return -EIO;
544                 } else
545                         return -EINVAL;
546         }
547
548         if (enable || disable) {
549                 status = (video_status(ibm) & 0x0f & ~disable) | enable;
550                 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80))
551                         return -EIO;
552                 if (!acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1))
553                         return -EIO;
554         }
555
556         return 0;
557 }
558
559 static void video_exit(struct ibm_struct *ibm)
560 {
561         acpi_evalf(vid_handle, NULL, "_DOS", "vd", ibm->state.video.autoswitch);
562 }
563
564 static int light_init(struct ibm_struct *ibm)
565 {
566         /* kblt not supported on G40, R32, X20 */
567         ibm->supported = acpi_evalf(ec_handle, NULL, "KBLT", "qv");
568
569         return 0;
570 }
571
572 static int light_read(struct ibm_struct *ibm, char *p)
573 {
574         int len = 0;
575         int status = 0;
576
577         if (ibm->supported) {
578                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
579                         return -EIO;
580                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
581         } else
582                 len += sprintf(p + len, "status:\t\tunknown\n");
583
584         len += sprintf(p + len, "commands:\ton, off\n");
585
586         return len;
587 }
588
589 static int light_write(struct ibm_struct *ibm, char *buf)
590 {
591         int cmos_cmd, lght_cmd;
592         char *cmd;
593         int success;
594
595         while ((cmd = next_cmd(&buf))) {
596                 if (strlencmp(cmd, "on") == 0) {
597                         cmos_cmd = 0x0c;
598                         lght_cmd = 1;
599                 } else if (strlencmp(cmd, "off") == 0) {
600                         cmos_cmd = 0x0d;
601                         lght_cmd = 0;
602                 } else
603                         return -EINVAL;
604
605                 success = cmos_handle ?
606                     acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
607                     acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
608                 if (!success)
609                         return -EIO;
610         }
611
612         return 0;
613 }
614
615 static int _sta(acpi_handle handle)
616 {
617         int status;
618
619         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
620                 status = 0;
621
622         return status;
623 }
624
625 #define dock_docked() (_sta(dock_handle) & 1)
626
627 static int dock_read(struct ibm_struct *ibm, char *p)
628 {
629         int len = 0;
630         int docked = dock_docked();
631
632         if (!dock_handle)
633                 len += sprintf(p + len, "status:\t\tnot supported\n");
634         else if (!docked)
635                 len += sprintf(p + len, "status:\t\tundocked\n");
636         else {
637                 len += sprintf(p + len, "status:\t\tdocked\n");
638                 len += sprintf(p + len, "commands:\tdock, undock\n");
639         }
640
641         return len;
642 }
643
644 static int dock_write(struct ibm_struct *ibm, char *buf)
645 {
646         char *cmd;
647
648         if (!dock_docked())
649                 return -EINVAL;
650
651         while ((cmd = next_cmd(&buf))) {
652                 if (strlencmp(cmd, "undock") == 0) {
653                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0))
654                                 return -EIO;
655                         if (!acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
656                                 return -EIO;
657                 } else if (strlencmp(cmd, "dock") == 0) {
658                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
659                                 return -EIO;
660                 } else
661                         return -EINVAL;
662         }
663
664         return 0;
665 }
666
667 static void dock_notify(struct ibm_struct *ibm, u32 event)
668 {
669         int docked = dock_docked();
670
671         if (event == 3 && docked)
672                 acpi_bus_generate_event(ibm->device, event, 1); /* button */
673         else if (event == 3 && !docked)
674                 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
675         else if (event == 0 && docked)
676                 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
677         else {
678                 printk(IBM_ERR "unknown dock event %d, status %d\n",
679                        event, _sta(dock_handle));
680                 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
681         }
682 }
683
684 #define bay_occupied() (_sta(bay_handle) & 1)
685
686 static int bay_init(struct ibm_struct *ibm)
687 {
688         /* bay not supported on A21e, A22p, A31, A31p, G40, R32, R40e */
689         ibm->supported = bay_handle && bayej_handle &&
690             acpi_evalf(bay_handle, NULL, "_STA", "qv");
691
692         return 0;
693 }
694
695 static int bay_read(struct ibm_struct *ibm, char *p)
696 {
697         int len = 0;
698         int occupied = bay_occupied();
699
700         if (!ibm->supported)
701                 len += sprintf(p + len, "status:\t\tnot supported\n");
702         else if (!occupied)
703                 len += sprintf(p + len, "status:\t\tunoccupied\n");
704         else {
705                 len += sprintf(p + len, "status:\t\toccupied\n");
706                 len += sprintf(p + len, "commands:\teject\n");
707         }
708
709         return len;
710 }
711
712 static int bay_write(struct ibm_struct *ibm, char *buf)
713 {
714         char *cmd;
715
716         while ((cmd = next_cmd(&buf))) {
717                 if (strlencmp(cmd, "eject") == 0) {
718                         if (!ibm->supported ||
719                             !acpi_evalf(bay_handle, NULL, "_EJ0", "vd", 1))
720                                 return -EIO;
721                 } else
722                         return -EINVAL;
723         }
724
725         return 0;
726 }
727
728 static void bay_notify(struct ibm_struct *ibm, u32 event)
729 {
730         acpi_bus_generate_event(ibm->device, event, 0);
731 }
732
733 static int cmos_read(struct ibm_struct *ibm, char *p)
734 {
735         int len = 0;
736
737         /* cmos not supported on A21e, A22p, T20, T21, X20 */
738         if (!cmos_handle)
739                 len += sprintf(p + len, "status:\t\tnot supported\n");
740         else {
741                 len += sprintf(p + len, "status:\t\tsupported\n");
742                 len += sprintf(p + len, "commands:\t<int>\n");
743         }
744
745         return len;
746 }
747
748 static int cmos_write(struct ibm_struct *ibm, char *buf)
749 {
750         char *cmd;
751         int cmos_cmd;
752
753         if (!cmos_handle)
754                 return -EINVAL;
755
756         while ((cmd = next_cmd(&buf))) {
757                 if (sscanf(cmd, "%u", &cmos_cmd) == 1) {
758                         /* cmos_cmd set */
759                 } else
760                         return -EINVAL;
761
762                 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
763                         return -EIO;
764         }
765
766         return 0;
767 }
768
769 static int led_read(struct ibm_struct *ibm, char *p)
770 {
771         int len = 0;
772
773         len += sprintf(p + len, "commands:\t"
774                        "<int> on, <int> off, <int> blink\n");
775
776         return len;
777 }
778
779 static int led_write(struct ibm_struct *ibm, char *buf)
780 {
781         char *cmd;
782         unsigned int led;
783         int led_cmd, sysl_cmd, bled_a, bled_b;
784
785         while ((cmd = next_cmd(&buf))) {
786                 if (sscanf(cmd, "%u", &led) != 1)
787                         return -EINVAL;
788
789                 if (strstr(cmd, "blink")) {
790                         led_cmd = 0xc0;
791                         sysl_cmd = 2;
792                         bled_a = 2;
793                         bled_b = 1;
794                 } else if (strstr(cmd, "on")) {
795                         led_cmd = 0x80;
796                         sysl_cmd = 1;
797                         bled_a = 2;
798                         bled_b = 0;
799                 } else if (strstr(cmd, "off")) {
800                         led_cmd = sysl_cmd = bled_a = bled_b = 0;
801                 } else
802                         return -EINVAL;
803
804                 if (led_handle) {
805                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
806                                         led, led_cmd))
807                                 return -EIO;
808                 } else if (led < 2) {
809                         if (acpi_evalf(sysl_handle, NULL, NULL, "vdd",
810                                        led, sysl_cmd))
811                                 return -EIO;
812                 } else if (led == 2 && bled_handle) {
813                         if (acpi_evalf(bled_handle, NULL, NULL, "vdd",
814                                        bled_a, bled_b))
815                                 return -EIO;
816                 } else
817                         return -EINVAL;
818         }
819
820         return 0;
821 }
822
823 static int beep_read(struct ibm_struct *ibm, char *p)
824 {
825         int len = 0;
826
827         len += sprintf(p + len, "commands:\t<int>\n");
828
829         return len;
830 }
831
832 static int beep_write(struct ibm_struct *ibm, char *buf)
833 {
834         char *cmd;
835         int beep_cmd;
836
837         while ((cmd = next_cmd(&buf))) {
838                 if (sscanf(cmd, "%u", &beep_cmd) == 1) {
839                         /* beep_cmd set */
840                 } else
841                         return -EINVAL;
842
843                 if (!acpi_evalf(beep_handle, NULL, NULL, "vd", beep_cmd))
844                         return -EIO;
845         }
846
847         return 0;
848 }
849
850 static struct ibm_struct ibms[] = {
851         {
852          .name = "driver",
853          .init = driver_init,
854          .read = driver_read,
855          },
856         {
857          .name = "hotkey",
858          .hid = "IBM0068",
859          .init = hotkey_init,
860          .read = hotkey_read,
861          .write = hotkey_write,
862          .exit = hotkey_exit,
863          .notify = hotkey_notify,
864          .handle = &hkey_handle,
865          .type = ACPI_DEVICE_NOTIFY,
866          },
867         {
868          .name = "bluetooth",
869          .init = bluetooth_init,
870          .read = bluetooth_read,
871          .write = bluetooth_write,
872          },
873         {
874          .name = "video",
875          .init = video_init,
876          .read = video_read,
877          .write = video_write,
878          .exit = video_exit,
879          },
880         {
881          .name = "light",
882          .init = light_init,
883          .read = light_read,
884          .write = light_write,
885          },
886         {
887          .name = "dock",
888          .read = dock_read,
889          .write = dock_write,
890          .notify = dock_notify,
891          .handle = &dock_handle,
892          .type = ACPI_SYSTEM_NOTIFY,
893          },
894         {
895          .name = "bay",
896          .init = bay_init,
897          .read = bay_read,
898          .write = bay_write,
899          .notify = bay_notify,
900          .handle = &bay_handle,
901          .type = ACPI_SYSTEM_NOTIFY,
902          },
903         {
904          .name = "cmos",
905          .read = cmos_read,
906          .write = cmos_write,
907          .experimental = 1,
908          },
909         {
910          .name = "led",
911          .read = led_read,
912          .write = led_write,
913          .experimental = 1,
914          },
915         {
916          .name = "beep",
917          .read = beep_read,
918          .write = beep_write,
919          .experimental = 1,
920          },
921 };
922
923 #define NUM_IBMS (sizeof(ibms)/sizeof(ibms[0]))
924
925 static int dispatch_read(char *page, char **start, off_t off, int count,
926                          int *eof, void *data)
927 {
928         struct ibm_struct *ibm = (struct ibm_struct *)data;
929         int len;
930
931         if (!ibm || !ibm->read)
932                 return -EINVAL;
933
934         len = ibm->read(ibm, page);
935         if (len < 0)
936                 return len;
937
938         if (len <= off + count)
939                 *eof = 1;
940         *start = page + off;
941         len -= off;
942         if (len > count)
943                 len = count;
944         if (len < 0)
945                 len = 0;
946
947         return len;
948 }
949
950 static int dispatch_write(struct file *file, const char __user * userbuf,
951                           unsigned long count, void *data)
952 {
953         struct ibm_struct *ibm = (struct ibm_struct *)data;
954         char *kernbuf;
955         int ret;
956
957         if (!ibm || !ibm->write)
958                 return -EINVAL;
959
960         kernbuf = kmalloc(count + 2, GFP_KERNEL);
961         if (!kernbuf)
962                 return -ENOMEM;
963
964         if (copy_from_user(kernbuf, userbuf, count)) {
965                 kfree(kernbuf);
966                 return -EFAULT;
967         }
968
969         kernbuf[count] = 0;
970         strcat(kernbuf, ",");
971         ret = ibm->write(ibm, kernbuf);
972         if (ret == 0)
973                 ret = count;
974
975         kfree(kernbuf);
976
977         return ret;
978 }
979
980 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
981 {
982         struct ibm_struct *ibm = (struct ibm_struct *)data;
983
984         if (!ibm || !ibm->notify)
985                 return;
986
987         ibm->notify(ibm, event);
988 }
989
990 static int setup_notify(struct ibm_struct *ibm)
991 {
992         acpi_status status;
993         int ret;
994
995         if (!*ibm->handle)
996                 return 0;
997
998         ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
999         if (ret < 0) {
1000                 printk(IBM_ERR "%s device not present\n", ibm->name);
1001                 return 0;
1002         }
1003
1004         acpi_driver_data(ibm->device) = ibm;
1005         sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
1006
1007         status = acpi_install_notify_handler(*ibm->handle, ibm->type,
1008                                              dispatch_notify, ibm);
1009         if (ACPI_FAILURE(status)) {
1010                 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
1011                        ibm->name, status);
1012                 return -ENODEV;
1013         }
1014
1015         ibm->notify_installed = 1;
1016
1017         return 0;
1018 }
1019
1020 static int ibmacpi_device_add(struct acpi_device *device)
1021 {
1022         return 0;
1023 }
1024
1025 static int register_driver(struct ibm_struct *ibm)
1026 {
1027         int ret;
1028
1029         ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
1030         if (!ibm->driver) {
1031                 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
1032                 return -1;
1033         }
1034
1035         memset(ibm->driver, 0, sizeof(struct acpi_driver));
1036         sprintf(ibm->driver->name, "%s/%s", IBM_NAME, ibm->name);
1037         ibm->driver->ids = ibm->hid;
1038         ibm->driver->ops.add = &ibmacpi_device_add;
1039
1040         ret = acpi_bus_register_driver(ibm->driver);
1041         if (ret < 0) {
1042                 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
1043                        ibm->hid, ret);
1044                 kfree(ibm->driver);
1045         }
1046
1047         return ret;
1048 }
1049
1050 static int ibm_init(struct ibm_struct *ibm)
1051 {
1052         int ret;
1053         struct proc_dir_entry *entry;
1054
1055         if (ibm->experimental && !experimental)
1056                 return 0;
1057
1058         if (ibm->hid) {
1059                 ret = register_driver(ibm);
1060                 if (ret < 0)
1061                         return ret;
1062                 ibm->driver_registered = 1;
1063         }
1064
1065         if (ibm->init) {
1066                 ret = ibm->init(ibm);
1067                 if (ret != 0)
1068                         return ret;
1069                 ibm->init_called = 1;
1070         }
1071
1072         entry = create_proc_entry(ibm->name, S_IFREG | S_IRUGO | S_IWUSR,
1073                                   proc_dir);
1074         if (!entry) {
1075                 printk(IBM_ERR "unable to create proc entry %s\n", ibm->name);
1076                 return -ENODEV;
1077         }
1078         entry->owner = THIS_MODULE;
1079         ibm->proc_created = 1;
1080
1081         entry->data = ibm;
1082         if (ibm->read)
1083                 entry->read_proc = &dispatch_read;
1084         if (ibm->write)
1085                 entry->write_proc = &dispatch_write;
1086
1087         if (ibm->notify) {
1088                 ret = setup_notify(ibm);
1089                 if (ret < 0)
1090                         return ret;
1091         }
1092
1093         return 0;
1094 }
1095
1096 static void ibm_exit(struct ibm_struct *ibm)
1097 {
1098         if (ibm->notify_installed)
1099                 acpi_remove_notify_handler(*ibm->handle, ibm->type,
1100                                            dispatch_notify);
1101
1102         if (ibm->proc_created)
1103                 remove_proc_entry(ibm->name, proc_dir);
1104
1105         if (ibm->init_called && ibm->exit)
1106                 ibm->exit(ibm);
1107
1108         if (ibm->driver_registered) {
1109                 acpi_bus_unregister_driver(ibm->driver);
1110                 kfree(ibm->driver);
1111         }
1112 }
1113
1114 static int ibm_handle_init(char *name,
1115                            acpi_handle * handle, acpi_handle parent,
1116                            char **paths, int num_paths, int required)
1117 {
1118         int i;
1119         acpi_status status;
1120
1121         for (i = 0; i < num_paths; i++) {
1122                 status = acpi_get_handle(parent, paths[i], handle);
1123                 if (ACPI_SUCCESS(status))
1124                         return 0;
1125         }
1126
1127         *handle = NULL;
1128
1129         if (required) {
1130                 printk(IBM_ERR "%s object not found\n", name);
1131                 return -1;
1132         }
1133
1134         return 0;
1135 }
1136
1137 #define IBM_HANDLE_INIT(object, required)                               \
1138         ibm_handle_init(#object, &object##_handle, *object##_parent,    \
1139                 object##_paths, sizeof(object##_paths)/sizeof(char*), required)
1140
1141 static int set_ibm_param(const char *val, struct kernel_param *kp)
1142 {
1143         unsigned int i;
1144         char arg_with_comma[32];
1145
1146         if (strlen(val) > 30)
1147                 return -ENOSPC;
1148
1149         strcpy(arg_with_comma, val);
1150         strcat(arg_with_comma, ",");
1151
1152         for (i = 0; i < NUM_IBMS; i++)
1153                 if (strcmp(ibms[i].name, kp->name) == 0)
1154                         return ibms[i].write(&ibms[i], arg_with_comma);
1155         BUG();
1156         return -EINVAL;
1157 }
1158
1159 #define IBM_PARAM(feature) \
1160         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
1161
1162 static void acpi_ibm_exit(void)
1163 {
1164         int i;
1165
1166         for (i = NUM_IBMS - 1; i >= 0; i--)
1167                 ibm_exit(&ibms[i]);
1168
1169         remove_proc_entry(IBM_DIR, acpi_root_dir);
1170 }
1171
1172 static int __init acpi_ibm_init(void)
1173 {
1174         int ret, i;
1175
1176         if (acpi_disabled)
1177                 return -ENODEV;
1178
1179         if (!acpi_specific_hotkey_enabled) {
1180                 printk(IBM_ERR "Using generic hotkey driver\n");
1181                 return -ENODEV;
1182         }
1183         /* these handles are required */
1184         if (IBM_HANDLE_INIT(ec, 1) < 0 ||
1185             IBM_HANDLE_INIT(hkey, 1) < 0 ||
1186             IBM_HANDLE_INIT(vid, 1) < 0 || IBM_HANDLE_INIT(beep, 1) < 0)
1187                 return -ENODEV;
1188
1189         /* these handles have alternatives */
1190         IBM_HANDLE_INIT(lght, 0);
1191         if (IBM_HANDLE_INIT(cmos, !lght_handle) < 0)
1192                 return -ENODEV;
1193         IBM_HANDLE_INIT(sysl, 0);
1194         if (IBM_HANDLE_INIT(led, !sysl_handle) < 0)
1195                 return -ENODEV;
1196
1197         /* these handles are not required */
1198         IBM_HANDLE_INIT(dock, 0);
1199         IBM_HANDLE_INIT(bay, 0);
1200         IBM_HANDLE_INIT(bayej, 0);
1201         IBM_HANDLE_INIT(bled, 0);
1202
1203         proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
1204         if (!proc_dir) {
1205                 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
1206                 return -ENODEV;
1207         }
1208         proc_dir->owner = THIS_MODULE;
1209
1210         for (i = 0; i < NUM_IBMS; i++) {
1211                 ret = ibm_init(&ibms[i]);
1212                 if (ret < 0) {
1213                         acpi_ibm_exit();
1214                         return ret;
1215                 }
1216         }
1217
1218         return 0;
1219 }
1220
1221 module_init(acpi_ibm_init);
1222 module_exit(acpi_ibm_exit);
1223
1224 MODULE_AUTHOR("Borislav Deianov");
1225 MODULE_DESCRIPTION(IBM_DESC);
1226 MODULE_LICENSE("GPL");
1227
1228 IBM_PARAM(hotkey);
1229 IBM_PARAM(bluetooth);
1230 IBM_PARAM(video);
1231 IBM_PARAM(light);
1232 IBM_PARAM(dock);
1233 IBM_PARAM(bay);
1234 IBM_PARAM(cmos);
1235 IBM_PARAM(led);
1236 IBM_PARAM(beep);