Merge branch 'panasonic' into test
[linux-2.6.git] / drivers / misc / fujitsu-laptop.c
1 /*-*-linux-c-*-*/
2
3 /*
4   Copyright (C) 2007,2008 Jonathan Woithe <jwoithe@physics.adelaide.edu.au>
5   Copyright (C) 2008 Peter Gruber <nokos@gmx.net>
6   Based on earlier work:
7     Copyright (C) 2003 Shane Spencer <shane@bogomip.com>
8     Adrian Yee <brewt-fujitsu@brewt.org>
9
10   Templated from msi-laptop.c and thinkpad_acpi.c which is copyright
11   by its respective authors.
12
13   This program is free software; you can redistribute it and/or modify
14   it under the terms of the GNU General Public License as published by
15   the Free Software Foundation; either version 2 of the License, or
16   (at your option) any later version.
17
18   This program is distributed in the hope that it will be useful, but
19   WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   General Public License for more details.
22
23   You should have received a copy of the GNU General Public License
24   along with this program; if not, write to the Free Software
25   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26   02110-1301, USA.
27  */
28
29 /*
30  * fujitsu-laptop.c - Fujitsu laptop support, providing access to additional
31  * features made available on a range of Fujitsu laptops including the
32  * P2xxx/P5xxx/S6xxx/S7xxx series.
33  *
34  * This driver exports a few files in /sys/devices/platform/fujitsu-laptop/;
35  * others may be added at a later date.
36  *
37  *   lcd_level - Screen brightness: contains a single integer in the
38  *   range 0..7. (rw)
39  *
40  * In addition to these platform device attributes the driver
41  * registers itself in the Linux backlight control subsystem and is
42  * available to userspace under /sys/class/backlight/fujitsu-laptop/.
43  *
44  * Hotkeys present on certain Fujitsu laptops (eg: the S6xxx series) are
45  * also supported by this driver.
46  *
47  * This driver has been tested on a Fujitsu Lifebook S6410, S7020 and
48  * P8010.  It should work on most P-series and S-series Lifebooks, but
49  * YMMV.
50  *
51  * The module parameter use_alt_lcd_levels switches between different ACPI
52  * brightness controls which are used by different Fujitsu laptops.  In most
53  * cases the correct method is automatically detected. "use_alt_lcd_levels=1"
54  * is applicable for a Fujitsu Lifebook S6410 if autodetection fails.
55  *
56  */
57
58 #include <linux/module.h>
59 #include <linux/kernel.h>
60 #include <linux/init.h>
61 #include <linux/acpi.h>
62 #include <linux/dmi.h>
63 #include <linux/backlight.h>
64 #include <linux/input.h>
65 #include <linux/kfifo.h>
66 #include <linux/video_output.h>
67 #include <linux/platform_device.h>
68
69 #define FUJITSU_DRIVER_VERSION "0.4.3"
70
71 #define FUJITSU_LCD_N_LEVELS 8
72
73 #define ACPI_FUJITSU_CLASS              "fujitsu"
74 #define ACPI_FUJITSU_HID                "FUJ02B1"
75 #define ACPI_FUJITSU_DRIVER_NAME        "Fujitsu laptop FUJ02B1 ACPI brightness driver"
76 #define ACPI_FUJITSU_DEVICE_NAME        "Fujitsu FUJ02B1"
77 #define ACPI_FUJITSU_HOTKEY_HID         "FUJ02E3"
78 #define ACPI_FUJITSU_HOTKEY_DRIVER_NAME "Fujitsu laptop FUJ02E3 ACPI hotkeys driver"
79 #define ACPI_FUJITSU_HOTKEY_DEVICE_NAME "Fujitsu FUJ02E3"
80
81 #define ACPI_FUJITSU_NOTIFY_CODE1     0x80
82
83 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS     0x86
84 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS     0x87
85
86 /* Hotkey details */
87 #define KEY1_CODE       0x410   /* codes for the keys in the GIRB register */
88 #define KEY2_CODE       0x411
89 #define KEY3_CODE       0x412
90 #define KEY4_CODE       0x413
91
92 #define MAX_HOTKEY_RINGBUFFER_SIZE 100
93 #define RINGBUFFERSIZE 40
94
95 /* Debugging */
96 #define FUJLAPTOP_LOG      ACPI_FUJITSU_HID ": "
97 #define FUJLAPTOP_ERR      KERN_ERR FUJLAPTOP_LOG
98 #define FUJLAPTOP_NOTICE   KERN_NOTICE FUJLAPTOP_LOG
99 #define FUJLAPTOP_INFO     KERN_INFO FUJLAPTOP_LOG
100 #define FUJLAPTOP_DEBUG    KERN_DEBUG FUJLAPTOP_LOG
101
102 #define FUJLAPTOP_DBG_ALL         0xffff
103 #define FUJLAPTOP_DBG_ERROR       0x0001
104 #define FUJLAPTOP_DBG_WARN        0x0002
105 #define FUJLAPTOP_DBG_INFO        0x0004
106 #define FUJLAPTOP_DBG_TRACE       0x0008
107
108 #define dbg_printk(a_dbg_level, format, arg...) \
109         do { if (dbg_level & a_dbg_level) \
110                 printk(FUJLAPTOP_DEBUG "%s: " format, __func__ , ## arg); \
111         } while (0)
112 #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
113 #define vdbg_printk(a_dbg_level, format, arg...) \
114         dbg_printk(a_dbg_level, format, ## arg)
115 #else
116 #define vdbg_printk(a_dbg_level, format, arg...)
117 #endif
118
119 /* Device controlling the backlight and associated keys */
120 struct fujitsu_t {
121         acpi_handle acpi_handle;
122         struct acpi_device *dev;
123         struct input_dev *input;
124         char phys[32];
125         struct backlight_device *bl_device;
126         struct platform_device *pf_device;
127         int keycode1, keycode2, keycode3, keycode4;
128
129         unsigned int max_brightness;
130         unsigned int brightness_changed;
131         unsigned int brightness_level;
132 };
133
134 static struct fujitsu_t *fujitsu;
135 static int use_alt_lcd_levels = -1;
136 static int disable_brightness_keys = -1;
137 static int disable_brightness_adjust = -1;
138
139 /* Device used to access other hotkeys on the laptop */
140 struct fujitsu_hotkey_t {
141         acpi_handle acpi_handle;
142         struct acpi_device *dev;
143         struct input_dev *input;
144         char phys[32];
145         struct platform_device *pf_device;
146         struct kfifo *fifo;
147         spinlock_t fifo_lock;
148
149         unsigned int irb;       /* info about the pressed buttons */
150 };
151
152 static struct fujitsu_hotkey_t *fujitsu_hotkey;
153
154 static void acpi_fujitsu_hotkey_notify(acpi_handle handle, u32 event,
155                                        void *data);
156
157 #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
158 static u32 dbg_level = 0x03;
159 #endif
160
161 static void acpi_fujitsu_notify(acpi_handle handle, u32 event, void *data);
162
163 /* Hardware access for LCD brightness control */
164
165 static int set_lcd_level(int level)
166 {
167         acpi_status status = AE_OK;
168         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
169         struct acpi_object_list arg_list = { 1, &arg0 };
170         acpi_handle handle = NULL;
171
172         vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via SBLL [%d]\n",
173                     level);
174
175         if (level < 0 || level >= fujitsu->max_brightness)
176                 return -EINVAL;
177
178         if (!fujitsu)
179                 return -EINVAL;
180
181         status = acpi_get_handle(fujitsu->acpi_handle, "SBLL", &handle);
182         if (ACPI_FAILURE(status)) {
183                 vdbg_printk(FUJLAPTOP_DBG_ERROR, "SBLL not present\n");
184                 return -ENODEV;
185         }
186
187         arg0.integer.value = level;
188
189         status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
190         if (ACPI_FAILURE(status))
191                 return -ENODEV;
192
193         return 0;
194 }
195
196 static int set_lcd_level_alt(int level)
197 {
198         acpi_status status = AE_OK;
199         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
200         struct acpi_object_list arg_list = { 1, &arg0 };
201         acpi_handle handle = NULL;
202
203         vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via SBL2 [%d]\n",
204                     level);
205
206         if (level < 0 || level >= fujitsu->max_brightness)
207                 return -EINVAL;
208
209         if (!fujitsu)
210                 return -EINVAL;
211
212         status = acpi_get_handle(fujitsu->acpi_handle, "SBL2", &handle);
213         if (ACPI_FAILURE(status)) {
214                 vdbg_printk(FUJLAPTOP_DBG_ERROR, "SBL2 not present\n");
215                 return -ENODEV;
216         }
217
218         arg0.integer.value = level;
219
220         status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
221         if (ACPI_FAILURE(status))
222                 return -ENODEV;
223
224         return 0;
225 }
226
227 static int get_lcd_level(void)
228 {
229         unsigned long long state = 0;
230         acpi_status status = AE_OK;
231
232         vdbg_printk(FUJLAPTOP_DBG_TRACE, "get lcd level via GBLL\n");
233
234         status =
235             acpi_evaluate_integer(fujitsu->acpi_handle, "GBLL", NULL, &state);
236         if (status < 0)
237                 return status;
238
239         fujitsu->brightness_level = state & 0x0fffffff;
240
241         if (state & 0x80000000)
242                 fujitsu->brightness_changed = 1;
243         else
244                 fujitsu->brightness_changed = 0;
245
246         return fujitsu->brightness_level;
247 }
248
249 static int get_max_brightness(void)
250 {
251         unsigned long long state = 0;
252         acpi_status status = AE_OK;
253
254         vdbg_printk(FUJLAPTOP_DBG_TRACE, "get max lcd level via RBLL\n");
255
256         status =
257             acpi_evaluate_integer(fujitsu->acpi_handle, "RBLL", NULL, &state);
258         if (status < 0)
259                 return status;
260
261         fujitsu->max_brightness = state;
262
263         return fujitsu->max_brightness;
264 }
265
266 static int get_lcd_level_alt(void)
267 {
268         unsigned long long state = 0;
269         acpi_status status = AE_OK;
270
271         vdbg_printk(FUJLAPTOP_DBG_TRACE, "get lcd level via GBLS\n");
272
273         status =
274             acpi_evaluate_integer(fujitsu->acpi_handle, "GBLS", NULL, &state);
275         if (status < 0)
276                 return status;
277
278         fujitsu->brightness_level = state & 0x0fffffff;
279
280         if (state & 0x80000000)
281                 fujitsu->brightness_changed = 1;
282         else
283                 fujitsu->brightness_changed = 0;
284
285         return fujitsu->brightness_level;
286 }
287
288 /* Backlight device stuff */
289
290 static int bl_get_brightness(struct backlight_device *b)
291 {
292         if (use_alt_lcd_levels)
293                 return get_lcd_level_alt();
294         else
295                 return get_lcd_level();
296 }
297
298 static int bl_update_status(struct backlight_device *b)
299 {
300         if (use_alt_lcd_levels)
301                 return set_lcd_level_alt(b->props.brightness);
302         else
303                 return set_lcd_level(b->props.brightness);
304 }
305
306 static struct backlight_ops fujitsubl_ops = {
307         .get_brightness = bl_get_brightness,
308         .update_status = bl_update_status,
309 };
310
311 /* Platform LCD brightness device */
312
313 static ssize_t
314 show_max_brightness(struct device *dev,
315                     struct device_attribute *attr, char *buf)
316 {
317
318         int ret;
319
320         ret = get_max_brightness();
321         if (ret < 0)
322                 return ret;
323
324         return sprintf(buf, "%i\n", ret);
325 }
326
327 static ssize_t
328 show_brightness_changed(struct device *dev,
329                         struct device_attribute *attr, char *buf)
330 {
331
332         int ret;
333
334         ret = fujitsu->brightness_changed;
335         if (ret < 0)
336                 return ret;
337
338         return sprintf(buf, "%i\n", ret);
339 }
340
341 static ssize_t show_lcd_level(struct device *dev,
342                               struct device_attribute *attr, char *buf)
343 {
344
345         int ret;
346
347         if (use_alt_lcd_levels)
348                 ret = get_lcd_level_alt();
349         else
350                 ret = get_lcd_level();
351         if (ret < 0)
352                 return ret;
353
354         return sprintf(buf, "%i\n", ret);
355 }
356
357 static ssize_t store_lcd_level(struct device *dev,
358                                struct device_attribute *attr, const char *buf,
359                                size_t count)
360 {
361
362         int level, ret;
363
364         if (sscanf(buf, "%i", &level) != 1
365             || (level < 0 || level >= fujitsu->max_brightness))
366                 return -EINVAL;
367
368         if (use_alt_lcd_levels)
369                 ret = set_lcd_level_alt(level);
370         else
371                 ret = set_lcd_level(level);
372         if (ret < 0)
373                 return ret;
374
375         if (use_alt_lcd_levels)
376                 ret = get_lcd_level_alt();
377         else
378                 ret = get_lcd_level();
379         if (ret < 0)
380                 return ret;
381
382         return count;
383 }
384
385 /* Hardware access for hotkey device */
386
387 static int get_irb(void)
388 {
389         unsigned long long state = 0;
390         acpi_status status = AE_OK;
391
392         vdbg_printk(FUJLAPTOP_DBG_TRACE, "Get irb\n");
393
394         status =
395             acpi_evaluate_integer(fujitsu_hotkey->acpi_handle, "GIRB", NULL,
396                                   &state);
397         if (status < 0)
398                 return status;
399
400         fujitsu_hotkey->irb = state;
401
402         return fujitsu_hotkey->irb;
403 }
404
405 static ssize_t
406 ignore_store(struct device *dev,
407              struct device_attribute *attr, const char *buf, size_t count)
408 {
409         return count;
410 }
411
412 static DEVICE_ATTR(max_brightness, 0444, show_max_brightness, ignore_store);
413 static DEVICE_ATTR(brightness_changed, 0444, show_brightness_changed,
414                    ignore_store);
415 static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
416
417 static struct attribute *fujitsupf_attributes[] = {
418         &dev_attr_brightness_changed.attr,
419         &dev_attr_max_brightness.attr,
420         &dev_attr_lcd_level.attr,
421         NULL
422 };
423
424 static struct attribute_group fujitsupf_attribute_group = {
425         .attrs = fujitsupf_attributes
426 };
427
428 static struct platform_driver fujitsupf_driver = {
429         .driver = {
430                    .name = "fujitsu-laptop",
431                    .owner = THIS_MODULE,
432                    }
433 };
434
435 static void dmi_check_cb_common(const struct dmi_system_id *id)
436 {
437         acpi_handle handle;
438         int have_blnf;
439         printk(KERN_INFO "fujitsu-laptop: Identified laptop model '%s'.\n",
440                id->ident);
441         have_blnf = ACPI_SUCCESS
442             (acpi_get_handle(NULL, "\\_SB.PCI0.GFX0.LCD.BLNF", &handle));
443         if (use_alt_lcd_levels == -1) {
444                 vdbg_printk(FUJLAPTOP_DBG_TRACE, "auto-detecting usealt\n");
445                 use_alt_lcd_levels = 1;
446         }
447         if (disable_brightness_keys == -1) {
448                 vdbg_printk(FUJLAPTOP_DBG_TRACE,
449                             "auto-detecting disable_keys\n");
450                 disable_brightness_keys = have_blnf ? 1 : 0;
451         }
452         if (disable_brightness_adjust == -1) {
453                 vdbg_printk(FUJLAPTOP_DBG_TRACE,
454                             "auto-detecting disable_adjust\n");
455                 disable_brightness_adjust = have_blnf ? 0 : 1;
456         }
457 }
458
459 static int dmi_check_cb_s6410(const struct dmi_system_id *id)
460 {
461         dmi_check_cb_common(id);
462         fujitsu->keycode1 = KEY_SCREENLOCK;     /* "Lock" */
463         fujitsu->keycode2 = KEY_HELP;   /* "Mobility Center" */
464         return 0;
465 }
466
467 static int dmi_check_cb_p8010(const struct dmi_system_id *id)
468 {
469         dmi_check_cb_common(id);
470         fujitsu->keycode1 = KEY_HELP;   /* "Support" */
471         fujitsu->keycode3 = KEY_SWITCHVIDEOMODE;        /* "Presentation" */
472         fujitsu->keycode4 = KEY_WWW;    /* "Internet" */
473         return 0;
474 }
475
476 static struct dmi_system_id __initdata fujitsu_dmi_table[] = {
477         {
478          .ident = "Fujitsu Siemens S6410",
479          .matches = {
480                      DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
481                      DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
482                      },
483          .callback = dmi_check_cb_s6410},
484         {
485          .ident = "Fujitsu LifeBook P8010",
486          .matches = {
487                      DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
488                      DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook P8010"),
489                      },
490          .callback = dmi_check_cb_p8010},
491         {}
492 };
493
494 /* ACPI device for LCD brightness control */
495
496 static int acpi_fujitsu_add(struct acpi_device *device)
497 {
498         acpi_status status;
499         acpi_handle handle;
500         int result = 0;
501         int state = 0;
502         struct input_dev *input;
503         int error;
504
505         if (!device)
506                 return -EINVAL;
507
508         fujitsu->acpi_handle = device->handle;
509         sprintf(acpi_device_name(device), "%s", ACPI_FUJITSU_DEVICE_NAME);
510         sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
511         device->driver_data = fujitsu;
512
513         status = acpi_install_notify_handler(device->handle,
514                                              ACPI_DEVICE_NOTIFY,
515                                              acpi_fujitsu_notify, fujitsu);
516
517         if (ACPI_FAILURE(status)) {
518                 printk(KERN_ERR "Error installing notify handler\n");
519                 error = -ENODEV;
520                 goto err_stop;
521         }
522
523         fujitsu->input = input = input_allocate_device();
524         if (!input) {
525                 error = -ENOMEM;
526                 goto err_uninstall_notify;
527         }
528
529         snprintf(fujitsu->phys, sizeof(fujitsu->phys),
530                  "%s/video/input0", acpi_device_hid(device));
531
532         input->name = acpi_device_name(device);
533         input->phys = fujitsu->phys;
534         input->id.bustype = BUS_HOST;
535         input->id.product = 0x06;
536         input->dev.parent = &device->dev;
537         input->evbit[0] = BIT(EV_KEY);
538         set_bit(KEY_BRIGHTNESSUP, input->keybit);
539         set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
540         set_bit(KEY_UNKNOWN, input->keybit);
541
542         error = input_register_device(input);
543         if (error)
544                 goto err_free_input_dev;
545
546         result = acpi_bus_get_power(fujitsu->acpi_handle, &state);
547         if (result) {
548                 printk(KERN_ERR "Error reading power state\n");
549                 goto end;
550         }
551
552         printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
553                acpi_device_name(device), acpi_device_bid(device),
554                !device->power.state ? "on" : "off");
555
556         fujitsu->dev = device;
557
558         if (ACPI_SUCCESS
559             (acpi_get_handle(device->handle, METHOD_NAME__INI, &handle))) {
560                 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
561                 if (ACPI_FAILURE
562                     (acpi_evaluate_object
563                      (device->handle, METHOD_NAME__INI, NULL, NULL)))
564                         printk(KERN_ERR "_INI Method failed\n");
565         }
566
567         /* do config (detect defaults) */
568         use_alt_lcd_levels = use_alt_lcd_levels == 1 ? 1 : 0;
569         disable_brightness_keys = disable_brightness_keys == 1 ? 1 : 0;
570         disable_brightness_adjust = disable_brightness_adjust == 1 ? 1 : 0;
571         vdbg_printk(FUJLAPTOP_DBG_INFO,
572                     "config: [alt interface: %d], [key disable: %d], [adjust disable: %d]\n",
573                     use_alt_lcd_levels, disable_brightness_keys,
574                     disable_brightness_adjust);
575
576         if (get_max_brightness() <= 0)
577                 fujitsu->max_brightness = FUJITSU_LCD_N_LEVELS;
578         if (use_alt_lcd_levels)
579                 get_lcd_level_alt();
580         else
581                 get_lcd_level();
582
583         return result;
584
585 end:
586 err_free_input_dev:
587         input_free_device(input);
588 err_uninstall_notify:
589         acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
590                                    acpi_fujitsu_notify);
591 err_stop:
592
593         return result;
594 }
595
596 static int acpi_fujitsu_remove(struct acpi_device *device, int type)
597 {
598         acpi_status status;
599         struct fujitsu_t *fujitsu = NULL;
600
601         if (!device || !acpi_driver_data(device))
602                 return -EINVAL;
603
604         fujitsu = acpi_driver_data(device);
605
606         status = acpi_remove_notify_handler(fujitsu->acpi_handle,
607                                             ACPI_DEVICE_NOTIFY,
608                                             acpi_fujitsu_notify);
609
610         if (!device || !acpi_driver_data(device))
611                 return -EINVAL;
612
613         fujitsu->acpi_handle = NULL;
614
615         return 0;
616 }
617
618 /* Brightness notify */
619
620 static void acpi_fujitsu_notify(acpi_handle handle, u32 event, void *data)
621 {
622         struct input_dev *input;
623         int keycode;
624         int oldb, newb;
625
626         input = fujitsu->input;
627
628         switch (event) {
629         case ACPI_FUJITSU_NOTIFY_CODE1:
630                 keycode = 0;
631                 oldb = fujitsu->brightness_level;
632                 get_lcd_level();  /* the alt version always yields changed */
633                 newb = fujitsu->brightness_level;
634
635                 vdbg_printk(FUJLAPTOP_DBG_TRACE,
636                             "brightness button event [%i -> %i (%i)]\n",
637                             oldb, newb, fujitsu->brightness_changed);
638
639                 if (oldb == newb && fujitsu->brightness_changed) {
640                         keycode = 0;
641                         if (disable_brightness_keys != 1) {
642                                 if (oldb == 0) {
643                                         acpi_bus_generate_proc_event
644                                             (fujitsu->dev,
645                                              ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS,
646                                              0);
647                                         keycode = KEY_BRIGHTNESSDOWN;
648                                 } else if (oldb ==
649                                            (fujitsu->max_brightness) - 1) {
650                                         acpi_bus_generate_proc_event
651                                             (fujitsu->dev,
652                                              ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS,
653                                              0);
654                                         keycode = KEY_BRIGHTNESSUP;
655                                 }
656                         }
657                 } else if (oldb < newb) {
658                         if (disable_brightness_adjust != 1) {
659                                 if (use_alt_lcd_levels)
660                                         set_lcd_level_alt(newb);
661                                 else
662                                         set_lcd_level(newb);
663                         }
664                         if (disable_brightness_keys != 1) {
665                                 acpi_bus_generate_proc_event(fujitsu->dev,
666                                         ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS, 0);
667                                 keycode = KEY_BRIGHTNESSUP;
668                         }
669                 } else if (oldb > newb) {
670                         if (disable_brightness_adjust != 1) {
671                                 if (use_alt_lcd_levels)
672                                         set_lcd_level_alt(newb);
673                                 else
674                                         set_lcd_level(newb);
675                         }
676                         if (disable_brightness_keys != 1) {
677                                 acpi_bus_generate_proc_event(fujitsu->dev,
678                                         ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS, 0);
679                                 keycode = KEY_BRIGHTNESSDOWN;
680                         }
681                 } else {
682                         keycode = KEY_UNKNOWN;
683                 }
684                 break;
685         default:
686                 keycode = KEY_UNKNOWN;
687                 vdbg_printk(FUJLAPTOP_DBG_WARN,
688                             "unsupported event [0x%x]\n", event);
689                 break;
690         }
691
692         if (keycode != 0) {
693                 input_report_key(input, keycode, 1);
694                 input_sync(input);
695                 input_report_key(input, keycode, 0);
696                 input_sync(input);
697         }
698
699         return;
700 }
701
702 /* ACPI device for hotkey handling */
703
704 static int acpi_fujitsu_hotkey_add(struct acpi_device *device)
705 {
706         acpi_status status;
707         acpi_handle handle;
708         int result = 0;
709         int state = 0;
710         struct input_dev *input;
711         int error;
712         int i;
713
714         if (!device)
715                 return -EINVAL;
716
717         fujitsu_hotkey->acpi_handle = device->handle;
718         sprintf(acpi_device_name(device), "%s",
719                 ACPI_FUJITSU_HOTKEY_DEVICE_NAME);
720         sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
721         device->driver_data = fujitsu_hotkey;
722
723         status = acpi_install_notify_handler(device->handle,
724                                              ACPI_DEVICE_NOTIFY,
725                                              acpi_fujitsu_hotkey_notify,
726                                              fujitsu_hotkey);
727
728         if (ACPI_FAILURE(status)) {
729                 printk(KERN_ERR "Error installing notify handler\n");
730                 error = -ENODEV;
731                 goto err_stop;
732         }
733
734         /* kfifo */
735         spin_lock_init(&fujitsu_hotkey->fifo_lock);
736         fujitsu_hotkey->fifo =
737             kfifo_alloc(RINGBUFFERSIZE * sizeof(int), GFP_KERNEL,
738                         &fujitsu_hotkey->fifo_lock);
739         if (IS_ERR(fujitsu_hotkey->fifo)) {
740                 printk(KERN_ERR "kfifo_alloc failed\n");
741                 error = PTR_ERR(fujitsu_hotkey->fifo);
742                 goto err_stop;
743         }
744
745         fujitsu_hotkey->input = input = input_allocate_device();
746         if (!input) {
747                 error = -ENOMEM;
748                 goto err_uninstall_notify;
749         }
750
751         snprintf(fujitsu_hotkey->phys, sizeof(fujitsu_hotkey->phys),
752                  "%s/video/input0", acpi_device_hid(device));
753
754         input->name = acpi_device_name(device);
755         input->phys = fujitsu_hotkey->phys;
756         input->id.bustype = BUS_HOST;
757         input->id.product = 0x06;
758         input->dev.parent = &device->dev;
759         input->evbit[0] = BIT(EV_KEY);
760         set_bit(fujitsu->keycode1, input->keybit);
761         set_bit(fujitsu->keycode2, input->keybit);
762         set_bit(fujitsu->keycode3, input->keybit);
763         set_bit(fujitsu->keycode4, input->keybit);
764         set_bit(KEY_UNKNOWN, input->keybit);
765
766         error = input_register_device(input);
767         if (error)
768                 goto err_free_input_dev;
769
770         result = acpi_bus_get_power(fujitsu_hotkey->acpi_handle, &state);
771         if (result) {
772                 printk(KERN_ERR "Error reading power state\n");
773                 goto end;
774         }
775
776         printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
777                acpi_device_name(device), acpi_device_bid(device),
778                !device->power.state ? "on" : "off");
779
780         fujitsu_hotkey->dev = device;
781
782         if (ACPI_SUCCESS
783             (acpi_get_handle(device->handle, METHOD_NAME__INI, &handle))) {
784                 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
785                 if (ACPI_FAILURE
786                     (acpi_evaluate_object
787                      (device->handle, METHOD_NAME__INI, NULL, NULL)))
788                         printk(KERN_ERR "_INI Method failed\n");
789         }
790
791         i = 0;                  /* Discard hotkey ringbuffer */
792         while (get_irb() != 0 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE) ;
793         vdbg_printk(FUJLAPTOP_DBG_INFO, "Discarded %i ringbuffer entries\n", i);
794
795         return result;
796
797 end:
798 err_free_input_dev:
799         input_free_device(input);
800 err_uninstall_notify:
801         acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
802                                    acpi_fujitsu_hotkey_notify);
803         kfifo_free(fujitsu_hotkey->fifo);
804 err_stop:
805
806         return result;
807 }
808
809 static int acpi_fujitsu_hotkey_remove(struct acpi_device *device, int type)
810 {
811         acpi_status status;
812         struct fujitsu_hotkey_t *fujitsu_hotkey = NULL;
813
814         if (!device || !acpi_driver_data(device))
815                 return -EINVAL;
816
817         fujitsu_hotkey = acpi_driver_data(device);
818
819         status = acpi_remove_notify_handler(fujitsu_hotkey->acpi_handle,
820                                             ACPI_DEVICE_NOTIFY,
821                                             acpi_fujitsu_hotkey_notify);
822
823         fujitsu_hotkey->acpi_handle = NULL;
824
825         kfifo_free(fujitsu_hotkey->fifo);
826
827         return 0;
828 }
829
830 static void acpi_fujitsu_hotkey_notify(acpi_handle handle, u32 event,
831                                        void *data)
832 {
833         struct input_dev *input;
834         int keycode, keycode_r;
835         unsigned int irb = 1;
836         int i, status;
837
838         input = fujitsu_hotkey->input;
839
840         vdbg_printk(FUJLAPTOP_DBG_TRACE, "Hotkey event\n");
841
842         switch (event) {
843         case ACPI_FUJITSU_NOTIFY_CODE1:
844                 i = 0;
845                 while ((irb = get_irb()) != 0
846                        && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE) {
847                         vdbg_printk(FUJLAPTOP_DBG_TRACE, "GIRB result [%x]\n",
848                                     irb);
849
850                         switch (irb & 0x4ff) {
851                         case KEY1_CODE:
852                                 keycode = fujitsu->keycode1;
853                                 break;
854                         case KEY2_CODE:
855                                 keycode = fujitsu->keycode2;
856                                 break;
857                         case KEY3_CODE:
858                                 keycode = fujitsu->keycode3;
859                                 break;
860                         case KEY4_CODE:
861                                 keycode = fujitsu->keycode4;
862                                 break;
863                         case 0:
864                                 keycode = 0;
865                                 break;
866                         default:
867                                 vdbg_printk(FUJLAPTOP_DBG_WARN,
868                                             "Unknown GIRB result [%x]\n", irb);
869                                 keycode = -1;
870                                 break;
871                         }
872                         if (keycode > 0) {
873                                 vdbg_printk(FUJLAPTOP_DBG_TRACE,
874                                         "Push keycode into ringbuffer [%d]\n",
875                                         keycode);
876                                 status = kfifo_put(fujitsu_hotkey->fifo,
877                                                    (unsigned char *)&keycode,
878                                                    sizeof(keycode));
879                                 if (status != sizeof(keycode)) {
880                                         vdbg_printk(FUJLAPTOP_DBG_WARN,
881                                             "Could not push keycode [0x%x]\n",
882                                             keycode);
883                                 } else {
884                                         input_report_key(input, keycode, 1);
885                                         input_sync(input);
886                                 }
887                         } else if (keycode == 0) {
888                                 while ((status =
889                                         kfifo_get
890                                         (fujitsu_hotkey->fifo, (unsigned char *)
891                                          &keycode_r,
892                                          sizeof
893                                          (keycode_r))) == sizeof(keycode_r)) {
894                                         input_report_key(input, keycode_r, 0);
895                                         input_sync(input);
896                                         vdbg_printk(FUJLAPTOP_DBG_TRACE,
897                                           "Pop keycode from ringbuffer [%d]\n",
898                                           keycode_r);
899                                 }
900                         }
901                 }
902
903                 break;
904         default:
905                 keycode = KEY_UNKNOWN;
906                 vdbg_printk(FUJLAPTOP_DBG_WARN,
907                             "Unsupported event [0x%x]\n", event);
908                 input_report_key(input, keycode, 1);
909                 input_sync(input);
910                 input_report_key(input, keycode, 0);
911                 input_sync(input);
912                 break;
913         }
914
915         return;
916 }
917
918 /* Initialization */
919
920 static const struct acpi_device_id fujitsu_device_ids[] = {
921         {ACPI_FUJITSU_HID, 0},
922         {"", 0},
923 };
924
925 static struct acpi_driver acpi_fujitsu_driver = {
926         .name = ACPI_FUJITSU_DRIVER_NAME,
927         .class = ACPI_FUJITSU_CLASS,
928         .ids = fujitsu_device_ids,
929         .ops = {
930                 .add = acpi_fujitsu_add,
931                 .remove = acpi_fujitsu_remove,
932                 },
933 };
934
935 static const struct acpi_device_id fujitsu_hotkey_device_ids[] = {
936         {ACPI_FUJITSU_HOTKEY_HID, 0},
937         {"", 0},
938 };
939
940 static struct acpi_driver acpi_fujitsu_hotkey_driver = {
941         .name = ACPI_FUJITSU_HOTKEY_DRIVER_NAME,
942         .class = ACPI_FUJITSU_CLASS,
943         .ids = fujitsu_hotkey_device_ids,
944         .ops = {
945                 .add = acpi_fujitsu_hotkey_add,
946                 .remove = acpi_fujitsu_hotkey_remove,
947                 },
948 };
949
950 static int __init fujitsu_init(void)
951 {
952         int ret, result, max_brightness;
953
954         if (acpi_disabled)
955                 return -ENODEV;
956
957         fujitsu = kmalloc(sizeof(struct fujitsu_t), GFP_KERNEL);
958         if (!fujitsu)
959                 return -ENOMEM;
960         memset(fujitsu, 0, sizeof(struct fujitsu_t));
961         fujitsu->keycode1 = KEY_PROG1;
962         fujitsu->keycode2 = KEY_PROG2;
963         fujitsu->keycode3 = KEY_PROG3;
964         fujitsu->keycode4 = KEY_PROG4;
965         dmi_check_system(fujitsu_dmi_table);
966
967         result = acpi_bus_register_driver(&acpi_fujitsu_driver);
968         if (result < 0) {
969                 ret = -ENODEV;
970                 goto fail_acpi;
971         }
972
973         /* Register platform stuff */
974
975         fujitsu->pf_device = platform_device_alloc("fujitsu-laptop", -1);
976         if (!fujitsu->pf_device) {
977                 ret = -ENOMEM;
978                 goto fail_platform_driver;
979         }
980
981         ret = platform_device_add(fujitsu->pf_device);
982         if (ret)
983                 goto fail_platform_device1;
984
985         ret =
986             sysfs_create_group(&fujitsu->pf_device->dev.kobj,
987                                &fujitsupf_attribute_group);
988         if (ret)
989                 goto fail_platform_device2;
990
991         /* Register backlight stuff */
992
993         fujitsu->bl_device =
994             backlight_device_register("fujitsu-laptop", NULL, NULL,
995                                       &fujitsubl_ops);
996         if (IS_ERR(fujitsu->bl_device))
997                 return PTR_ERR(fujitsu->bl_device);
998
999         max_brightness = fujitsu->max_brightness;
1000
1001         fujitsu->bl_device->props.max_brightness = max_brightness - 1;
1002         fujitsu->bl_device->props.brightness = fujitsu->brightness_level;
1003
1004         ret = platform_driver_register(&fujitsupf_driver);
1005         if (ret)
1006                 goto fail_backlight;
1007
1008         /* Register hotkey driver */
1009
1010         fujitsu_hotkey = kmalloc(sizeof(struct fujitsu_hotkey_t), GFP_KERNEL);
1011         if (!fujitsu_hotkey) {
1012                 ret = -ENOMEM;
1013                 goto fail_hotkey;
1014         }
1015         memset(fujitsu_hotkey, 0, sizeof(struct fujitsu_hotkey_t));
1016
1017         result = acpi_bus_register_driver(&acpi_fujitsu_hotkey_driver);
1018         if (result < 0) {
1019                 ret = -ENODEV;
1020                 goto fail_hotkey1;
1021         }
1022
1023         printk(KERN_INFO "fujitsu-laptop: driver " FUJITSU_DRIVER_VERSION
1024                " successfully loaded.\n");
1025
1026         return 0;
1027
1028 fail_hotkey1:
1029
1030         kfree(fujitsu_hotkey);
1031
1032 fail_hotkey:
1033
1034         platform_driver_unregister(&fujitsupf_driver);
1035
1036 fail_backlight:
1037
1038         backlight_device_unregister(fujitsu->bl_device);
1039
1040 fail_platform_device2:
1041
1042         platform_device_del(fujitsu->pf_device);
1043
1044 fail_platform_device1:
1045
1046         platform_device_put(fujitsu->pf_device);
1047
1048 fail_platform_driver:
1049
1050         acpi_bus_unregister_driver(&acpi_fujitsu_driver);
1051
1052 fail_acpi:
1053
1054         kfree(fujitsu);
1055
1056         return ret;
1057 }
1058
1059 static void __exit fujitsu_cleanup(void)
1060 {
1061         sysfs_remove_group(&fujitsu->pf_device->dev.kobj,
1062                            &fujitsupf_attribute_group);
1063         platform_device_unregister(fujitsu->pf_device);
1064         platform_driver_unregister(&fujitsupf_driver);
1065         backlight_device_unregister(fujitsu->bl_device);
1066
1067         acpi_bus_unregister_driver(&acpi_fujitsu_driver);
1068
1069         kfree(fujitsu);
1070
1071         acpi_bus_unregister_driver(&acpi_fujitsu_hotkey_driver);
1072
1073         kfree(fujitsu_hotkey);
1074
1075         printk(KERN_INFO "fujitsu-laptop: driver unloaded.\n");
1076 }
1077
1078 module_init(fujitsu_init);
1079 module_exit(fujitsu_cleanup);
1080
1081 module_param(use_alt_lcd_levels, uint, 0644);
1082 MODULE_PARM_DESC(use_alt_lcd_levels,
1083                  "Use alternative interface for lcd_levels (needed for Lifebook s6410).");
1084 module_param(disable_brightness_keys, uint, 0644);
1085 MODULE_PARM_DESC(disable_brightness_keys,
1086                  "Disable brightness keys (eg. if they are already handled by the generic ACPI_VIDEO device).");
1087 module_param(disable_brightness_adjust, uint, 0644);
1088 MODULE_PARM_DESC(disable_brightness_adjust, "Disable brightness adjustment .");
1089 #ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
1090 module_param_named(debug, dbg_level, uint, 0644);
1091 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
1092 #endif
1093
1094 MODULE_AUTHOR("Jonathan Woithe, Peter Gruber");
1095 MODULE_DESCRIPTION("Fujitsu laptop extras support");
1096 MODULE_VERSION(FUJITSU_DRIVER_VERSION);
1097 MODULE_LICENSE("GPL");
1098
1099 MODULE_ALIAS("dmi:*:svnFUJITSUSIEMENS:*:pvr:rvnFUJITSU:rnFJNB1D3:*:cvrS6410:*");
1100 MODULE_ALIAS("dmi:*:svnFUJITSU:*:pvr:rvnFUJITSU:rnFJNB19C:*:cvrS7020:*");
1101
1102 static struct pnp_device_id pnp_ids[] = {
1103         {.id = "FUJ02bf"},
1104         {.id = "FUJ02B1"},
1105         {.id = "FUJ02E3"},
1106         {.id = ""}
1107 };
1108
1109 MODULE_DEVICE_TABLE(pnp, pnp_ids);