b7c67c87f47b96006428014e560bceca7be97925
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / platform / x86 / samsung-laptop.c
1 /*
2  * Samsung Laptop driver
3  *
4  * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
5  * Copyright (C) 2009,2011 Novell Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  *
11  */
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/delay.h>
18 #include <linux/pci.h>
19 #include <linux/backlight.h>
20 #include <linux/leds.h>
21 #include <linux/fb.h>
22 #include <linux/dmi.h>
23 #include <linux/platform_device.h>
24 #include <linux/rfkill.h>
25 #include <linux/acpi.h>
26 #include <linux/seq_file.h>
27 #include <linux/debugfs.h>
28
29 /*
30  * This driver is needed because a number of Samsung laptops do not hook
31  * their control settings through ACPI.  So we have to poke around in the
32  * BIOS to do things like brightness values, and "special" key controls.
33  */
34
35 /*
36  * We have 0 - 8 as valid brightness levels.  The specs say that level 0 should
37  * be reserved by the BIOS (which really doesn't make much sense), we tell
38  * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
39  */
40 #define MAX_BRIGHT      0x07
41
42
43 #define SABI_IFACE_MAIN                 0x00
44 #define SABI_IFACE_SUB                  0x02
45 #define SABI_IFACE_COMPLETE             0x04
46 #define SABI_IFACE_DATA                 0x05
47
48 #define WL_STATUS_WLAN                  0x0
49 #define WL_STATUS_BT                    0x2
50
51 /* Structure get/set data using sabi */
52 struct sabi_data {
53         union {
54                 struct {
55                         u32 d0;
56                         u32 d1;
57                         u16 d2;
58                         u8  d3;
59                 };
60                 u8 data[11];
61         };
62 };
63
64 struct sabi_header_offsets {
65         u8 port;
66         u8 re_mem;
67         u8 iface_func;
68         u8 en_mem;
69         u8 data_offset;
70         u8 data_segment;
71 };
72
73 struct sabi_commands {
74         /*
75          * Brightness is 0 - 8, as described above.
76          * Value 0 is for the BIOS to use
77          */
78         u16 get_brightness;
79         u16 set_brightness;
80
81         /*
82          * first byte:
83          * 0x00 - wireless is off
84          * 0x01 - wireless is on
85          * second byte:
86          * 0x02 - 3G is off
87          * 0x03 - 3G is on
88          * TODO, verify 3G is correct, that doesn't seem right...
89          */
90         u16 get_wireless_button;
91         u16 set_wireless_button;
92
93         /* 0 is off, 1 is on */
94         u16 get_backlight;
95         u16 set_backlight;
96
97         /*
98          * 0x80 or 0x00 - no action
99          * 0x81 - recovery key pressed
100          */
101         u16 get_recovery_mode;
102         u16 set_recovery_mode;
103
104         /*
105          * on seclinux: 0 is low, 1 is high,
106          * on swsmi: 0 is normal, 1 is silent, 2 is turbo
107          */
108         u16 get_performance_level;
109         u16 set_performance_level;
110
111         /* 0x80 is off, 0x81 is on */
112         u16 get_battery_life_extender;
113         u16 set_battery_life_extender;
114
115         /* 0x80 is off, 0x81 is on */
116         u16 get_usb_charge;
117         u16 set_usb_charge;
118
119         /* the first byte is for bluetooth and the third one is for wlan */
120         u16 get_wireless_status;
121         u16 set_wireless_status;
122
123         /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
124         u16 kbd_backlight;
125
126         /*
127          * Tell the BIOS that Linux is running on this machine.
128          * 81 is on, 80 is off
129          */
130         u16 set_linux;
131 };
132
133 struct sabi_performance_level {
134         const char *name;
135         u16 value;
136 };
137
138 struct sabi_config {
139         int sabi_version;
140         const char *test_string;
141         u16 main_function;
142         const struct sabi_header_offsets header_offsets;
143         const struct sabi_commands commands;
144         const struct sabi_performance_level performance_levels[4];
145         u8 min_brightness;
146         u8 max_brightness;
147 };
148
149 static const struct sabi_config sabi_configs[] = {
150         {
151                 /* I don't know if it is really 2, but it it is
152                  * less than 3 anyway */
153                 .sabi_version = 2,
154
155                 .test_string = "SECLINUX",
156
157                 .main_function = 0x4c49,
158
159                 .header_offsets = {
160                         .port = 0x00,
161                         .re_mem = 0x02,
162                         .iface_func = 0x03,
163                         .en_mem = 0x04,
164                         .data_offset = 0x05,
165                         .data_segment = 0x07,
166                 },
167
168                 .commands = {
169                         .get_brightness = 0x00,
170                         .set_brightness = 0x01,
171
172                         .get_wireless_button = 0x02,
173                         .set_wireless_button = 0x03,
174
175                         .get_backlight = 0x04,
176                         .set_backlight = 0x05,
177
178                         .get_recovery_mode = 0x06,
179                         .set_recovery_mode = 0x07,
180
181                         .get_performance_level = 0x08,
182                         .set_performance_level = 0x09,
183
184                         .get_battery_life_extender = 0xFFFF,
185                         .set_battery_life_extender = 0xFFFF,
186
187                         .get_usb_charge = 0xFFFF,
188                         .set_usb_charge = 0xFFFF,
189
190                         .get_wireless_status = 0xFFFF,
191                         .set_wireless_status = 0xFFFF,
192
193                         .kbd_backlight = 0xFFFF,
194
195                         .set_linux = 0x0a,
196                 },
197
198                 .performance_levels = {
199                         {
200                                 .name = "silent",
201                                 .value = 0,
202                         },
203                         {
204                                 .name = "normal",
205                                 .value = 1,
206                         },
207                         { },
208                 },
209                 .min_brightness = 1,
210                 .max_brightness = 8,
211         },
212         {
213                 .sabi_version = 3,
214
215                 .test_string = "SwSmi@",
216
217                 .main_function = 0x5843,
218
219                 .header_offsets = {
220                         .port = 0x00,
221                         .re_mem = 0x04,
222                         .iface_func = 0x02,
223                         .en_mem = 0x03,
224                         .data_offset = 0x05,
225                         .data_segment = 0x07,
226                 },
227
228                 .commands = {
229                         .get_brightness = 0x10,
230                         .set_brightness = 0x11,
231
232                         .get_wireless_button = 0x12,
233                         .set_wireless_button = 0x13,
234
235                         .get_backlight = 0x2d,
236                         .set_backlight = 0x2e,
237
238                         .get_recovery_mode = 0xff,
239                         .set_recovery_mode = 0xff,
240
241                         .get_performance_level = 0x31,
242                         .set_performance_level = 0x32,
243
244                         .get_battery_life_extender = 0x65,
245                         .set_battery_life_extender = 0x66,
246
247                         .get_usb_charge = 0x67,
248                         .set_usb_charge = 0x68,
249
250                         .get_wireless_status = 0x69,
251                         .set_wireless_status = 0x6a,
252
253                         .kbd_backlight = 0x78,
254
255                         .set_linux = 0xff,
256                 },
257
258                 .performance_levels = {
259                         {
260                                 .name = "normal",
261                                 .value = 0,
262                         },
263                         {
264                                 .name = "silent",
265                                 .value = 1,
266                         },
267                         {
268                                 .name = "overclock",
269                                 .value = 2,
270                         },
271                         { },
272                 },
273                 .min_brightness = 0,
274                 .max_brightness = 8,
275         },
276         { },
277 };
278
279 /*
280  * samsung-laptop/    - debugfs root directory
281  *   f0000_segment    - dump f0000 segment
282  *   command          - current command
283  *   data             - current data
284  *   d0, d1, d2, d3   - data fields
285  *   call             - call SABI using command and data
286  *
287  * This allow to call arbitrary sabi commands wihout
288  * modifying the driver at all.
289  * For example, setting the keyboard backlight brightness to 5
290  *
291  *  echo 0x78 > command
292  *  echo 0x0582 > d0
293  *  echo 0 > d1
294  *  echo 0 > d2
295  *  echo 0 > d3
296  *  cat call
297  */
298
299 struct samsung_laptop_debug {
300         struct dentry *root;
301         struct sabi_data data;
302         u16 command;
303
304         struct debugfs_blob_wrapper f0000_wrapper;
305         struct debugfs_blob_wrapper data_wrapper;
306         struct debugfs_blob_wrapper sdiag_wrapper;
307 };
308
309 struct samsung_laptop;
310
311 struct samsung_rfkill {
312         struct samsung_laptop *samsung;
313         struct rfkill *rfkill;
314         enum rfkill_type type;
315 };
316
317 struct samsung_laptop {
318         const struct sabi_config *config;
319
320         void __iomem *sabi;
321         void __iomem *sabi_iface;
322         void __iomem *f0000_segment;
323
324         struct mutex sabi_mutex;
325
326         struct platform_device *platform_device;
327         struct backlight_device *backlight_device;
328
329         struct samsung_rfkill wlan;
330         struct samsung_rfkill bluetooth;
331
332         struct led_classdev kbd_led;
333         int kbd_led_wk;
334         struct workqueue_struct *led_workqueue;
335         struct work_struct kbd_led_work;
336
337         struct samsung_laptop_debug debug;
338
339         bool handle_backlight;
340         bool has_stepping_quirk;
341
342         char sdiag[64];
343 };
344
345
346
347 static bool force;
348 module_param(force, bool, 0);
349 MODULE_PARM_DESC(force,
350                 "Disable the DMI check and forces the driver to be loaded");
351
352 static bool debug;
353 module_param(debug, bool, S_IRUGO | S_IWUSR);
354 MODULE_PARM_DESC(debug, "Debug enabled or not");
355
356 static int sabi_command(struct samsung_laptop *samsung, u16 command,
357                         struct sabi_data *in,
358                         struct sabi_data *out)
359 {
360         const struct sabi_config *config = samsung->config;
361         int ret = 0;
362         u16 port = readw(samsung->sabi + config->header_offsets.port);
363         u8 complete, iface_data;
364
365         mutex_lock(&samsung->sabi_mutex);
366
367         if (debug) {
368                 if (in)
369                         pr_info("SABI command:0x%04x "
370                                 "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
371                                 command, in->d0, in->d1, in->d2, in->d3);
372                 else
373                         pr_info("SABI command:0x%04x", command);
374         }
375
376         /* enable memory to be able to write to it */
377         outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
378
379         /* write out the command */
380         writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
381         writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
382         writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
383         if (in) {
384                 writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
385                 writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
386                 writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
387                 writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
388         }
389         outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
390
391         /* write protect memory to make it safe */
392         outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
393
394         /* see if the command actually succeeded */
395         complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
396         iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
397
398         /* iface_data = 0xFF happens when a command is not known
399          * so we only add a warning in debug mode since we will
400          * probably issue some unknown command at startup to find
401          * out which features are supported */
402         if (complete != 0xaa || (iface_data == 0xff && debug))
403                 pr_warn("SABI command 0x%04x failed with"
404                         " completion flag 0x%02x and interface data 0x%02x",
405                         command, complete, iface_data);
406
407         if (complete != 0xaa || iface_data == 0xff) {
408                 ret = -EINVAL;
409                 goto exit;
410         }
411
412         if (out) {
413                 out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
414                 out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
415                 out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
416                 out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
417         }
418
419         if (debug && out) {
420                 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
421                         out->d0, out->d1, out->d2, out->d3);
422         }
423
424 exit:
425         mutex_unlock(&samsung->sabi_mutex);
426         return ret;
427 }
428
429 /* simple wrappers usable with most commands */
430 static int sabi_set_commandb(struct samsung_laptop *samsung,
431                              u16 command, u8 data)
432 {
433         struct sabi_data in = { { { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 } } };
434
435         in.data[0] = data;
436         return sabi_command(samsung, command, &in, NULL);
437 }
438
439 static int read_brightness(struct samsung_laptop *samsung)
440 {
441         const struct sabi_config *config = samsung->config;
442         const struct sabi_commands *commands = &samsung->config->commands;
443         struct sabi_data sretval;
444         int user_brightness = 0;
445         int retval;
446
447         retval = sabi_command(samsung, commands->get_brightness,
448                               NULL, &sretval);
449         if (retval)
450                 return retval;
451
452         user_brightness = sretval.data[0];
453         if (user_brightness > config->min_brightness)
454                 user_brightness -= config->min_brightness;
455         else
456                 user_brightness = 0;
457
458         return user_brightness;
459 }
460
461 static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
462 {
463         const struct sabi_config *config = samsung->config;
464         const struct sabi_commands *commands = &samsung->config->commands;
465         u8 user_level = user_brightness + config->min_brightness;
466
467         if (samsung->has_stepping_quirk && user_level != 0) {
468                 /*
469                  * short circuit if the specified level is what's already set
470                  * to prevent the screen from flickering needlessly
471                  */
472                 if (user_brightness == read_brightness(samsung))
473                         return;
474
475                 sabi_set_commandb(samsung, commands->set_brightness, 0);
476         }
477
478         sabi_set_commandb(samsung, commands->set_brightness, user_level);
479 }
480
481 static int get_brightness(struct backlight_device *bd)
482 {
483         struct samsung_laptop *samsung = bl_get_data(bd);
484
485         return read_brightness(samsung);
486 }
487
488 static void check_for_stepping_quirk(struct samsung_laptop *samsung)
489 {
490         int initial_level;
491         int check_level;
492         int orig_level = read_brightness(samsung);
493
494         /*
495          * Some laptops exhibit the strange behaviour of stepping toward
496          * (rather than setting) the brightness except when changing to/from
497          * brightness level 0. This behaviour is checked for here and worked
498          * around in set_brightness.
499          */
500
501         if (orig_level == 0)
502                 set_brightness(samsung, 1);
503
504         initial_level = read_brightness(samsung);
505
506         if (initial_level <= 2)
507                 check_level = initial_level + 2;
508         else
509                 check_level = initial_level - 2;
510
511         samsung->has_stepping_quirk = false;
512         set_brightness(samsung, check_level);
513
514         if (read_brightness(samsung) != check_level) {
515                 samsung->has_stepping_quirk = true;
516                 pr_info("enabled workaround for brightness stepping quirk\n");
517         }
518
519         set_brightness(samsung, orig_level);
520 }
521
522 static int update_status(struct backlight_device *bd)
523 {
524         struct samsung_laptop *samsung = bl_get_data(bd);
525         const struct sabi_commands *commands = &samsung->config->commands;
526
527         set_brightness(samsung, bd->props.brightness);
528
529         if (bd->props.power == FB_BLANK_UNBLANK)
530                 sabi_set_commandb(samsung, commands->set_backlight, 1);
531         else
532                 sabi_set_commandb(samsung, commands->set_backlight, 0);
533
534         return 0;
535 }
536
537 static const struct backlight_ops backlight_ops = {
538         .get_brightness = get_brightness,
539         .update_status  = update_status,
540 };
541
542 static int seclinux_rfkill_set(void *data, bool blocked)
543 {
544         struct samsung_rfkill *srfkill = data;
545         struct samsung_laptop *samsung = srfkill->samsung;
546         const struct sabi_commands *commands = &samsung->config->commands;
547
548         return sabi_set_commandb(samsung, commands->set_wireless_button,
549                                  !blocked);
550 }
551
552 static struct rfkill_ops seclinux_rfkill_ops = {
553         .set_block = seclinux_rfkill_set,
554 };
555
556 static int swsmi_wireless_status(struct samsung_laptop *samsung,
557                                  struct sabi_data *data)
558 {
559         const struct sabi_commands *commands = &samsung->config->commands;
560
561         return sabi_command(samsung, commands->get_wireless_status,
562                             NULL, data);
563 }
564
565 static int swsmi_rfkill_set(void *priv, bool blocked)
566 {
567         struct samsung_rfkill *srfkill = priv;
568         struct samsung_laptop *samsung = srfkill->samsung;
569         const struct sabi_commands *commands = &samsung->config->commands;
570         struct sabi_data data;
571         int ret, i;
572
573         ret = swsmi_wireless_status(samsung, &data);
574         if (ret)
575                 return ret;
576
577         /* Don't set the state for non-present devices */
578         for (i = 0; i < 4; i++)
579                 if (data.data[i] == 0x02)
580                         data.data[1] = 0;
581
582         if (srfkill->type == RFKILL_TYPE_WLAN)
583                 data.data[WL_STATUS_WLAN] = !blocked;
584         else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
585                 data.data[WL_STATUS_BT] = !blocked;
586
587         return sabi_command(samsung, commands->set_wireless_status,
588                             &data, &data);
589 }
590
591 static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
592 {
593         struct samsung_rfkill *srfkill = priv;
594         struct samsung_laptop *samsung = srfkill->samsung;
595         struct sabi_data data;
596         int ret;
597
598         ret = swsmi_wireless_status(samsung, &data);
599         if (ret)
600                 return ;
601
602         if (srfkill->type == RFKILL_TYPE_WLAN)
603                 ret = data.data[WL_STATUS_WLAN];
604         else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
605                 ret = data.data[WL_STATUS_BT];
606         else
607                 return ;
608
609         rfkill_set_sw_state(rfkill, !ret);
610 }
611
612 static struct rfkill_ops swsmi_rfkill_ops = {
613         .set_block = swsmi_rfkill_set,
614         .query = swsmi_rfkill_query,
615 };
616
617 static ssize_t get_performance_level(struct device *dev,
618                                      struct device_attribute *attr, char *buf)
619 {
620         struct samsung_laptop *samsung = dev_get_drvdata(dev);
621         const struct sabi_config *config = samsung->config;
622         const struct sabi_commands *commands = &config->commands;
623         struct sabi_data sretval;
624         int retval;
625         int i;
626
627         /* Read the state */
628         retval = sabi_command(samsung, commands->get_performance_level,
629                               NULL, &sretval);
630         if (retval)
631                 return retval;
632
633         /* The logic is backwards, yeah, lots of fun... */
634         for (i = 0; config->performance_levels[i].name; ++i) {
635                 if (sretval.data[0] == config->performance_levels[i].value)
636                         return sprintf(buf, "%s\n", config->performance_levels[i].name);
637         }
638         return sprintf(buf, "%s\n", "unknown");
639 }
640
641 static ssize_t set_performance_level(struct device *dev,
642                                 struct device_attribute *attr, const char *buf,
643                                 size_t count)
644 {
645         struct samsung_laptop *samsung = dev_get_drvdata(dev);
646         const struct sabi_config *config = samsung->config;
647         const struct sabi_commands *commands = &config->commands;
648         int i;
649
650         if (count < 1)
651                 return count;
652
653         for (i = 0; config->performance_levels[i].name; ++i) {
654                 const struct sabi_performance_level *level =
655                         &config->performance_levels[i];
656                 if (!strncasecmp(level->name, buf, strlen(level->name))) {
657                         sabi_set_commandb(samsung,
658                                           commands->set_performance_level,
659                                           level->value);
660                         break;
661                 }
662         }
663
664         if (!config->performance_levels[i].name)
665                 return -EINVAL;
666
667         return count;
668 }
669
670 static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
671                    get_performance_level, set_performance_level);
672
673 static int read_battery_life_extender(struct samsung_laptop *samsung)
674 {
675         const struct sabi_commands *commands = &samsung->config->commands;
676         struct sabi_data data;
677         int retval;
678
679         if (commands->get_battery_life_extender == 0xFFFF)
680                 return -ENODEV;
681
682         memset(&data, 0, sizeof(data));
683         data.data[0] = 0x80;
684         retval = sabi_command(samsung, commands->get_battery_life_extender,
685                               &data, &data);
686
687         if (retval)
688                 return retval;
689
690         if (data.data[0] != 0 && data.data[0] != 1)
691                 return -ENODEV;
692
693         return data.data[0];
694 }
695
696 static int write_battery_life_extender(struct samsung_laptop *samsung,
697                                        int enabled)
698 {
699         const struct sabi_commands *commands = &samsung->config->commands;
700         struct sabi_data data;
701
702         memset(&data, 0, sizeof(data));
703         data.data[0] = 0x80 | enabled;
704         return sabi_command(samsung, commands->set_battery_life_extender,
705                             &data, NULL);
706 }
707
708 static ssize_t get_battery_life_extender(struct device *dev,
709                                          struct device_attribute *attr,
710                                          char *buf)
711 {
712         struct samsung_laptop *samsung = dev_get_drvdata(dev);
713         int ret;
714
715         ret = read_battery_life_extender(samsung);
716         if (ret < 0)
717                 return ret;
718
719         return sprintf(buf, "%d\n", ret);
720 }
721
722 static ssize_t set_battery_life_extender(struct device *dev,
723                                         struct device_attribute *attr,
724                                         const char *buf, size_t count)
725 {
726         struct samsung_laptop *samsung = dev_get_drvdata(dev);
727         int ret, value;
728
729         if (!count || sscanf(buf, "%i", &value) != 1)
730                 return -EINVAL;
731
732         ret = write_battery_life_extender(samsung, !!value);
733         if (ret < 0)
734                 return ret;
735
736         return count;
737 }
738
739 static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
740                    get_battery_life_extender, set_battery_life_extender);
741
742 static int read_usb_charge(struct samsung_laptop *samsung)
743 {
744         const struct sabi_commands *commands = &samsung->config->commands;
745         struct sabi_data data;
746         int retval;
747
748         if (commands->get_usb_charge == 0xFFFF)
749                 return -ENODEV;
750
751         memset(&data, 0, sizeof(data));
752         data.data[0] = 0x80;
753         retval = sabi_command(samsung, commands->get_usb_charge,
754                               &data, &data);
755
756         if (retval)
757                 return retval;
758
759         if (data.data[0] != 0 && data.data[0] != 1)
760                 return -ENODEV;
761
762         return data.data[0];
763 }
764
765 static int write_usb_charge(struct samsung_laptop *samsung,
766                             int enabled)
767 {
768         const struct sabi_commands *commands = &samsung->config->commands;
769         struct sabi_data data;
770
771         memset(&data, 0, sizeof(data));
772         data.data[0] = 0x80 | enabled;
773         return sabi_command(samsung, commands->set_usb_charge,
774                             &data, NULL);
775 }
776
777 static ssize_t get_usb_charge(struct device *dev,
778                               struct device_attribute *attr,
779                               char *buf)
780 {
781         struct samsung_laptop *samsung = dev_get_drvdata(dev);
782         int ret;
783
784         ret = read_usb_charge(samsung);
785         if (ret < 0)
786                 return ret;
787
788         return sprintf(buf, "%d\n", ret);
789 }
790
791 static ssize_t set_usb_charge(struct device *dev,
792                               struct device_attribute *attr,
793                               const char *buf, size_t count)
794 {
795         struct samsung_laptop *samsung = dev_get_drvdata(dev);
796         int ret, value;
797
798         if (!count || sscanf(buf, "%i", &value) != 1)
799                 return -EINVAL;
800
801         ret = write_usb_charge(samsung, !!value);
802         if (ret < 0)
803                 return ret;
804
805         return count;
806 }
807
808 static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
809                    get_usb_charge, set_usb_charge);
810
811 static struct attribute *platform_attributes[] = {
812         &dev_attr_performance_level.attr,
813         &dev_attr_battery_life_extender.attr,
814         &dev_attr_usb_charge.attr,
815         NULL
816 };
817
818 static int find_signature(void __iomem *memcheck, const char *testStr)
819 {
820         int i = 0;
821         int loca;
822
823         for (loca = 0; loca < 0xffff; loca++) {
824                 char temp = readb(memcheck + loca);
825
826                 if (temp == testStr[i]) {
827                         if (i == strlen(testStr)-1)
828                                 break;
829                         ++i;
830                 } else {
831                         i = 0;
832                 }
833         }
834         return loca;
835 }
836
837 static void samsung_rfkill_exit(struct samsung_laptop *samsung)
838 {
839         if (samsung->wlan.rfkill) {
840                 rfkill_unregister(samsung->wlan.rfkill);
841                 rfkill_destroy(samsung->wlan.rfkill);
842                 samsung->wlan.rfkill = NULL;
843         }
844         if (samsung->bluetooth.rfkill) {
845                 rfkill_unregister(samsung->bluetooth.rfkill);
846                 rfkill_destroy(samsung->bluetooth.rfkill);
847                 samsung->bluetooth.rfkill = NULL;
848         }
849 }
850
851 static int samsung_new_rfkill(struct samsung_laptop *samsung,
852                               struct samsung_rfkill *arfkill,
853                               const char *name, enum rfkill_type type,
854                               const struct rfkill_ops *ops,
855                               int blocked)
856 {
857         struct rfkill **rfkill = &arfkill->rfkill;
858         int ret;
859
860         arfkill->type = type;
861         arfkill->samsung = samsung;
862
863         *rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
864                                type, ops, arfkill);
865
866         if (!*rfkill)
867                 return -EINVAL;
868
869         if (blocked != -1)
870                 rfkill_init_sw_state(*rfkill, blocked);
871
872         ret = rfkill_register(*rfkill);
873         if (ret) {
874                 rfkill_destroy(*rfkill);
875                 *rfkill = NULL;
876                 return ret;
877         }
878         return 0;
879 }
880
881 static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
882 {
883         return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
884                                   RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
885 }
886
887 static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
888 {
889         struct sabi_data data;
890         int ret;
891
892         ret = swsmi_wireless_status(samsung, &data);
893         if (ret) {
894                 /* Some swsmi laptops use the old seclinux way to control
895                  * wireless devices */
896                 if (ret == -EINVAL)
897                         ret = samsung_rfkill_init_seclinux(samsung);
898                 return ret;
899         }
900
901         /* 0x02 seems to mean that the device is no present/available */
902
903         if (data.data[WL_STATUS_WLAN] != 0x02)
904                 ret = samsung_new_rfkill(samsung, &samsung->wlan,
905                                          "samsung-wlan",
906                                          RFKILL_TYPE_WLAN,
907                                          &swsmi_rfkill_ops,
908                                          !data.data[WL_STATUS_WLAN]);
909         if (ret)
910                 goto exit;
911
912         if (data.data[WL_STATUS_BT] != 0x02)
913                 ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
914                                          "samsung-bluetooth",
915                                          RFKILL_TYPE_BLUETOOTH,
916                                          &swsmi_rfkill_ops,
917                                          !data.data[WL_STATUS_BT]);
918         if (ret)
919                 goto exit;
920
921 exit:
922         if (ret)
923                 samsung_rfkill_exit(samsung);
924
925         return ret;
926 }
927
928 static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
929 {
930         if (samsung->config->sabi_version == 2)
931                 return samsung_rfkill_init_seclinux(samsung);
932         if (samsung->config->sabi_version == 3)
933                 return samsung_rfkill_init_swsmi(samsung);
934         return 0;
935 }
936
937 static int kbd_backlight_enable(struct samsung_laptop *samsung)
938 {
939         const struct sabi_commands *commands = &samsung->config->commands;
940         struct sabi_data data;
941         int retval;
942
943         if (commands->kbd_backlight == 0xFFFF)
944                 return -ENODEV;
945
946         memset(&data, 0, sizeof(data));
947         data.d0 = 0xaabb;
948         retval = sabi_command(samsung, commands->kbd_backlight,
949                               &data, &data);
950
951         if (retval)
952                 return retval;
953
954         if (data.d0 != 0xccdd)
955                 return -ENODEV;
956         return 0;
957 }
958
959 static int kbd_backlight_read(struct samsung_laptop *samsung)
960 {
961         const struct sabi_commands *commands = &samsung->config->commands;
962         struct sabi_data data;
963         int retval;
964
965         memset(&data, 0, sizeof(data));
966         data.data[0] = 0x81;
967         retval = sabi_command(samsung, commands->kbd_backlight,
968                               &data, &data);
969
970         if (retval)
971                 return retval;
972
973         return data.data[0];
974 }
975
976 static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
977 {
978         const struct sabi_commands *commands = &samsung->config->commands;
979         struct sabi_data data;
980
981         memset(&data, 0, sizeof(data));
982         data.d0 = 0x82 | ((brightness & 0xFF) << 8);
983         return sabi_command(samsung, commands->kbd_backlight,
984                             &data, NULL);
985 }
986
987 static void kbd_led_update(struct work_struct *work)
988 {
989         struct samsung_laptop *samsung;
990
991         samsung = container_of(work, struct samsung_laptop, kbd_led_work);
992         kbd_backlight_write(samsung, samsung->kbd_led_wk);
993 }
994
995 static void kbd_led_set(struct led_classdev *led_cdev,
996                         enum led_brightness value)
997 {
998         struct samsung_laptop *samsung;
999
1000         samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1001
1002         if (value > samsung->kbd_led.max_brightness)
1003                 value = samsung->kbd_led.max_brightness;
1004         else if (value < 0)
1005                 value = 0;
1006
1007         samsung->kbd_led_wk = value;
1008         queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
1009 }
1010
1011 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1012 {
1013         struct samsung_laptop *samsung;
1014
1015         samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1016         return kbd_backlight_read(samsung);
1017 }
1018
1019 static void samsung_leds_exit(struct samsung_laptop *samsung)
1020 {
1021         if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
1022                 led_classdev_unregister(&samsung->kbd_led);
1023         if (samsung->led_workqueue)
1024                 destroy_workqueue(samsung->led_workqueue);
1025 }
1026
1027 static int __init samsung_leds_init(struct samsung_laptop *samsung)
1028 {
1029         int ret = 0;
1030
1031         samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
1032         if (!samsung->led_workqueue)
1033                 return -ENOMEM;
1034
1035         if (kbd_backlight_enable(samsung) >= 0) {
1036                 INIT_WORK(&samsung->kbd_led_work, kbd_led_update);
1037
1038                 samsung->kbd_led.name = "samsung::kbd_backlight";
1039                 samsung->kbd_led.brightness_set = kbd_led_set;
1040                 samsung->kbd_led.brightness_get = kbd_led_get;
1041                 samsung->kbd_led.max_brightness = 8;
1042
1043                 ret = led_classdev_register(&samsung->platform_device->dev,
1044                                            &samsung->kbd_led);
1045         }
1046
1047         if (ret)
1048                 samsung_leds_exit(samsung);
1049
1050         return ret;
1051 }
1052
1053 static void samsung_backlight_exit(struct samsung_laptop *samsung)
1054 {
1055         if (samsung->backlight_device) {
1056                 backlight_device_unregister(samsung->backlight_device);
1057                 samsung->backlight_device = NULL;
1058         }
1059 }
1060
1061 static int __init samsung_backlight_init(struct samsung_laptop *samsung)
1062 {
1063         struct backlight_device *bd;
1064         struct backlight_properties props;
1065
1066         if (!samsung->handle_backlight)
1067                 return 0;
1068
1069         memset(&props, 0, sizeof(struct backlight_properties));
1070         props.type = BACKLIGHT_PLATFORM;
1071         props.max_brightness = samsung->config->max_brightness -
1072                 samsung->config->min_brightness;
1073
1074         bd = backlight_device_register("samsung",
1075                                        &samsung->platform_device->dev,
1076                                        samsung, &backlight_ops,
1077                                        &props);
1078         if (IS_ERR(bd))
1079                 return PTR_ERR(bd);
1080
1081         samsung->backlight_device = bd;
1082         samsung->backlight_device->props.brightness = read_brightness(samsung);
1083         samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
1084         backlight_update_status(samsung->backlight_device);
1085
1086         return 0;
1087 }
1088
1089 static mode_t samsung_sysfs_is_visible(struct kobject *kobj,
1090                                        struct attribute *attr, int idx)
1091 {
1092         struct device *dev = container_of(kobj, struct device, kobj);
1093         struct platform_device *pdev = to_platform_device(dev);
1094         struct samsung_laptop *samsung = platform_get_drvdata(pdev);
1095         bool ok = true;
1096
1097         if (attr == &dev_attr_performance_level.attr)
1098                 ok = !!samsung->config->performance_levels[0].name;
1099         if (attr == &dev_attr_battery_life_extender.attr)
1100                 ok = !!(read_battery_life_extender(samsung) >= 0);
1101         if (attr == &dev_attr_usb_charge.attr)
1102                 ok = !!(read_usb_charge(samsung) >= 0);
1103
1104         return ok ? attr->mode : 0;
1105 }
1106
1107 static struct attribute_group platform_attribute_group = {
1108         .is_visible = samsung_sysfs_is_visible,
1109         .attrs = platform_attributes
1110 };
1111
1112 static void samsung_sysfs_exit(struct samsung_laptop *samsung)
1113 {
1114         struct platform_device *device = samsung->platform_device;
1115
1116         sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1117 }
1118
1119 static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
1120 {
1121         struct platform_device *device = samsung->platform_device;
1122
1123         return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1124
1125 }
1126
1127 static int show_call(struct seq_file *m, void *data)
1128 {
1129         struct samsung_laptop *samsung = m->private;
1130         struct sabi_data *sdata = &samsung->debug.data;
1131         int ret;
1132
1133         seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1134                    samsung->debug.command,
1135                    sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1136
1137         ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
1138
1139         if (ret) {
1140                 seq_printf(m, "SABI command 0x%04x failed\n",
1141                            samsung->debug.command);
1142                 return ret;
1143         }
1144
1145         seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1146                    sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1147         return 0;
1148 }
1149
1150 static int samsung_debugfs_open(struct inode *inode, struct file *file)
1151 {
1152         return single_open(file, show_call, inode->i_private);
1153 }
1154
1155 static const struct file_operations samsung_laptop_call_io_ops = {
1156         .owner = THIS_MODULE,
1157         .open = samsung_debugfs_open,
1158         .read = seq_read,
1159         .llseek = seq_lseek,
1160         .release = single_release,
1161 };
1162
1163 static void samsung_debugfs_exit(struct samsung_laptop *samsung)
1164 {
1165         debugfs_remove_recursive(samsung->debug.root);
1166 }
1167
1168 static int samsung_debugfs_init(struct samsung_laptop *samsung)
1169 {
1170         struct dentry *dent;
1171
1172         samsung->debug.root = debugfs_create_dir("samsung-laptop", NULL);
1173         if (!samsung->debug.root) {
1174                 pr_err("failed to create debugfs directory");
1175                 goto error_debugfs;
1176         }
1177
1178         samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
1179         samsung->debug.f0000_wrapper.size = 0xffff;
1180
1181         samsung->debug.data_wrapper.data = &samsung->debug.data;
1182         samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
1183
1184         samsung->debug.sdiag_wrapper.data = samsung->sdiag;
1185         samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
1186
1187         dent = debugfs_create_u16("command", S_IRUGO | S_IWUSR,
1188                                   samsung->debug.root, &samsung->debug.command);
1189         if (!dent)
1190                 goto error_debugfs;
1191
1192         dent = debugfs_create_u32("d0", S_IRUGO | S_IWUSR, samsung->debug.root,
1193                                   &samsung->debug.data.d0);
1194         if (!dent)
1195                 goto error_debugfs;
1196
1197         dent = debugfs_create_u32("d1", S_IRUGO | S_IWUSR, samsung->debug.root,
1198                                   &samsung->debug.data.d1);
1199         if (!dent)
1200                 goto error_debugfs;
1201
1202         dent = debugfs_create_u16("d2", S_IRUGO | S_IWUSR, samsung->debug.root,
1203                                   &samsung->debug.data.d2);
1204         if (!dent)
1205                 goto error_debugfs;
1206
1207         dent = debugfs_create_u8("d3", S_IRUGO | S_IWUSR, samsung->debug.root,
1208                                  &samsung->debug.data.d3);
1209         if (!dent)
1210                 goto error_debugfs;
1211
1212         dent = debugfs_create_blob("data", S_IRUGO | S_IWUSR,
1213                                    samsung->debug.root,
1214                                    &samsung->debug.data_wrapper);
1215         if (!dent)
1216                 goto error_debugfs;
1217
1218         dent = debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR,
1219                                    samsung->debug.root,
1220                                    &samsung->debug.f0000_wrapper);
1221         if (!dent)
1222                 goto error_debugfs;
1223
1224         dent = debugfs_create_file("call", S_IFREG | S_IRUGO,
1225                                    samsung->debug.root, samsung,
1226                                    &samsung_laptop_call_io_ops);
1227         if (!dent)
1228                 goto error_debugfs;
1229
1230         dent = debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR,
1231                                    samsung->debug.root,
1232                                    &samsung->debug.sdiag_wrapper);
1233         if (!dent)
1234                 goto error_debugfs;
1235
1236         return 0;
1237
1238 error_debugfs:
1239         samsung_debugfs_exit(samsung);
1240         return -ENOMEM;
1241 }
1242
1243 static void samsung_sabi_exit(struct samsung_laptop *samsung)
1244 {
1245         const struct sabi_config *config = samsung->config;
1246
1247         /* Turn off "Linux" mode in the BIOS */
1248         if (config && config->commands.set_linux != 0xff)
1249                 sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
1250
1251         if (samsung->sabi_iface) {
1252                 iounmap(samsung->sabi_iface);
1253                 samsung->sabi_iface = NULL;
1254         }
1255         if (samsung->f0000_segment) {
1256                 iounmap(samsung->f0000_segment);
1257                 samsung->f0000_segment = NULL;
1258         }
1259
1260         samsung->config = NULL;
1261 }
1262
1263 static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
1264                                       unsigned int ifaceP)
1265 {
1266         const struct sabi_config *config = samsung->config;
1267
1268         printk(KERN_DEBUG "This computer supports SABI==%x\n",
1269                loca + 0xf0000 - 6);
1270
1271         printk(KERN_DEBUG "SABI header:\n");
1272         printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
1273                readw(samsung->sabi + config->header_offsets.port));
1274         printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
1275                readb(samsung->sabi + config->header_offsets.iface_func));
1276         printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
1277                readb(samsung->sabi + config->header_offsets.en_mem));
1278         printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
1279                readb(samsung->sabi + config->header_offsets.re_mem));
1280         printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
1281                readw(samsung->sabi + config->header_offsets.data_offset));
1282         printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
1283                readw(samsung->sabi + config->header_offsets.data_segment));
1284
1285         printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
1286 }
1287
1288 static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
1289 {
1290         int loca = find_signature(samsung->f0000_segment, "SDiaG@");
1291         int i;
1292
1293         if (loca == 0xffff)
1294                 return ;
1295
1296         /* Example:
1297          * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1298          *
1299          * Product name: 90X3A
1300          * BIOS Version: 07HL
1301          */
1302         loca += 1;
1303         for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
1304                 char temp = readb(samsung->f0000_segment + loca);
1305
1306                 if (isalnum(temp) || temp == '/' || temp == '-')
1307                         samsung->sdiag[i++] = temp;
1308                 else
1309                         break ;
1310         }
1311
1312         if (debug && samsung->sdiag[0])
1313                 pr_info("sdiag: %s", samsung->sdiag);
1314 }
1315
1316 static int __init samsung_sabi_init(struct samsung_laptop *samsung)
1317 {
1318         const struct sabi_config *config = NULL;
1319         const struct sabi_commands *commands;
1320         unsigned int ifaceP;
1321         int ret = 0;
1322         int i;
1323         int loca;
1324
1325         samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
1326         if (!samsung->f0000_segment) {
1327                 if (debug || force)
1328                         pr_err("Can't map the segment at 0xf0000\n");
1329                 ret = -EINVAL;
1330                 goto exit;
1331         }
1332
1333         samsung_sabi_diag(samsung);
1334
1335         /* Try to find one of the signatures in memory to find the header */
1336         for (i = 0; sabi_configs[i].test_string != 0; ++i) {
1337                 samsung->config = &sabi_configs[i];
1338                 loca = find_signature(samsung->f0000_segment,
1339                                       samsung->config->test_string);
1340                 if (loca != 0xffff)
1341                         break;
1342         }
1343
1344         if (loca == 0xffff) {
1345                 if (debug || force)
1346                         pr_err("This computer does not support SABI\n");
1347                 ret = -ENODEV;
1348                 goto exit;
1349         }
1350
1351         config = samsung->config;
1352         commands = &config->commands;
1353
1354         /* point to the SMI port Number */
1355         loca += 1;
1356         samsung->sabi = (samsung->f0000_segment + loca);
1357
1358         /* Get a pointer to the SABI Interface */
1359         ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
1360         ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
1361
1362         if (debug)
1363                 samsung_sabi_infos(samsung, loca, ifaceP);
1364
1365         samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
1366         if (!samsung->sabi_iface) {
1367                 pr_err("Can't remap %x\n", ifaceP);
1368                 ret = -EINVAL;
1369                 goto exit;
1370         }
1371
1372         /* Turn on "Linux" mode in the BIOS */
1373         if (commands->set_linux != 0xff) {
1374                 int retval = sabi_set_commandb(samsung,
1375                                                commands->set_linux, 0x81);
1376                 if (retval) {
1377                         pr_warn("Linux mode was not set!\n");
1378                         ret = -ENODEV;
1379                         goto exit;
1380                 }
1381         }
1382
1383         /* Check for stepping quirk */
1384         if (samsung->handle_backlight)
1385                 check_for_stepping_quirk(samsung);
1386
1387         pr_info("detected SABI interface: %s\n",
1388                 samsung->config->test_string);
1389
1390 exit:
1391         if (ret)
1392                 samsung_sabi_exit(samsung);
1393
1394         return ret;
1395 }
1396
1397 static void samsung_platform_exit(struct samsung_laptop *samsung)
1398 {
1399         if (samsung->platform_device) {
1400                 platform_device_unregister(samsung->platform_device);
1401                 samsung->platform_device = NULL;
1402         }
1403 }
1404
1405 static int __init samsung_platform_init(struct samsung_laptop *samsung)
1406 {
1407         struct platform_device *pdev;
1408
1409         pdev = platform_device_register_simple("samsung", -1, NULL, 0);
1410         if (IS_ERR(pdev))
1411                 return PTR_ERR(pdev);
1412
1413         samsung->platform_device = pdev;
1414         platform_set_drvdata(samsung->platform_device, samsung);
1415         return 0;
1416 }
1417
1418 static struct dmi_system_id __initdata samsung_dmi_table[] = {
1419         {
1420                 .matches = {
1421                         DMI_MATCH(DMI_SYS_VENDOR,
1422                                         "SAMSUNG ELECTRONICS CO., LTD."),
1423                         DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
1424                 },
1425         },
1426         {
1427                 .matches = {
1428                         DMI_MATCH(DMI_SYS_VENDOR,
1429                                         "SAMSUNG ELECTRONICS CO., LTD."),
1430                         DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
1431                 },
1432         },
1433         {
1434                 .matches = {
1435                         DMI_MATCH(DMI_SYS_VENDOR,
1436                                         "SAMSUNG ELECTRONICS CO., LTD."),
1437                         DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
1438                 },
1439         },
1440         {
1441                 .matches = {
1442                         DMI_MATCH(DMI_SYS_VENDOR,
1443                                         "SAMSUNG ELECTRONICS CO., LTD."),
1444                         DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
1445                 },
1446         },
1447         { },
1448 };
1449 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
1450
1451 static struct platform_device *samsung_platform_device;
1452
1453 static int __init samsung_init(void)
1454 {
1455         struct samsung_laptop *samsung;
1456         int ret;
1457
1458         if (!force && !dmi_check_system(samsung_dmi_table))
1459                 return -ENODEV;
1460
1461         samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
1462         if (!samsung)
1463                 return -ENOMEM;
1464
1465         mutex_init(&samsung->sabi_mutex);
1466         samsung->handle_backlight = true;
1467
1468 #ifdef CONFIG_ACPI
1469         /* Don't handle backlight here if the acpi video already handle it */
1470         if (acpi_video_backlight_support())
1471                 samsung->handle_backlight = false;
1472 #endif
1473         ret = samsung_platform_init(samsung);
1474         if (ret)
1475                 goto error_platform;
1476
1477         ret = samsung_sabi_init(samsung);
1478         if (ret)
1479                 goto error_sabi;
1480
1481 #ifdef CONFIG_ACPI
1482         /* Only log that if we are really on a sabi platform */
1483         if (acpi_video_backlight_support())
1484                 pr_info("Backlight controlled by ACPI video driver\n");
1485 #endif
1486
1487         ret = samsung_sysfs_init(samsung);
1488         if (ret)
1489                 goto error_sysfs;
1490
1491         ret = samsung_backlight_init(samsung);
1492         if (ret)
1493                 goto error_backlight;
1494
1495         ret = samsung_rfkill_init(samsung);
1496         if (ret)
1497                 goto error_rfkill;
1498
1499         ret = samsung_leds_init(samsung);
1500         if (ret)
1501                 goto error_leds;
1502
1503         ret = samsung_debugfs_init(samsung);
1504         if (ret)
1505                 goto error_debugfs;
1506
1507         samsung_platform_device = samsung->platform_device;
1508         return ret;
1509
1510 error_debugfs:
1511         samsung_leds_exit(samsung);
1512 error_leds:
1513         samsung_rfkill_exit(samsung);
1514 error_rfkill:
1515         samsung_backlight_exit(samsung);
1516 error_backlight:
1517         samsung_sysfs_exit(samsung);
1518 error_sysfs:
1519         samsung_sabi_exit(samsung);
1520 error_sabi:
1521         samsung_platform_exit(samsung);
1522 error_platform:
1523         kfree(samsung);
1524         return ret;
1525 }
1526
1527 static void __exit samsung_exit(void)
1528 {
1529         struct samsung_laptop *samsung;
1530
1531         samsung = platform_get_drvdata(samsung_platform_device);
1532
1533         samsung_debugfs_exit(samsung);
1534         samsung_leds_exit(samsung);
1535         samsung_rfkill_exit(samsung);
1536         samsung_backlight_exit(samsung);
1537         samsung_sysfs_exit(samsung);
1538         samsung_sabi_exit(samsung);
1539         samsung_platform_exit(samsung);
1540
1541         kfree(samsung);
1542         samsung_platform_device = NULL;
1543 }
1544
1545 module_init(samsung_init);
1546 module_exit(samsung_exit);
1547
1548 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1549 MODULE_DESCRIPTION("Samsung Backlight driver");
1550 MODULE_LICENSE("GPL");