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