1 // SPDX-License-Identifier: GPL-2.0-only
3 * Samsung Laptop driver
5 * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
6 * Copyright (C) 2009,2011 Novell Inc.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/pci.h>
15 #include <linux/backlight.h>
16 #include <linux/leds.h>
17 #include <linux/dmi.h>
18 #include <linux/platform_device.h>
19 #include <linux/rfkill.h>
20 #include <linux/acpi.h>
21 #include <linux/seq_file.h>
22 #include <linux/debugfs.h>
23 #include <linux/ctype.h>
24 #include <linux/efi.h>
25 #include <linux/suspend.h>
26 #include <acpi/video.h>
29 * This driver is needed because a number of Samsung laptops do not hook
30 * their control settings through ACPI. So we have to poke around in the
31 * BIOS to do things like brightness values, and "special" key controls.
35 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
36 * be reserved by the BIOS (which really doesn't make much sense), we tell
37 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
39 #define MAX_BRIGHT 0x07
42 #define SABI_IFACE_MAIN 0x00
43 #define SABI_IFACE_SUB 0x02
44 #define SABI_IFACE_COMPLETE 0x04
45 #define SABI_IFACE_DATA 0x05
47 #define WL_STATUS_WLAN 0x0
48 #define WL_STATUS_BT 0x2
50 /* Structure get/set data using sabi */
63 struct sabi_header_offsets
{
72 struct sabi_commands
{
74 * Brightness is 0 - 8, as described above.
75 * Value 0 is for the BIOS to use
82 * 0x00 - wireless is off
83 * 0x01 - wireless is on
87 * TODO, verify 3G is correct, that doesn't seem right...
89 u16 get_wireless_button
;
90 u16 set_wireless_button
;
92 /* 0 is off, 1 is on */
97 * 0x80 or 0x00 - no action
98 * 0x81 - recovery key pressed
100 u16 get_recovery_mode
;
101 u16 set_recovery_mode
;
104 * on seclinux: 0 is low, 1 is high,
105 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
107 u16 get_performance_level
;
108 u16 set_performance_level
;
110 /* 0x80 is off, 0x81 is on */
111 u16 get_battery_life_extender
;
112 u16 set_battery_life_extender
;
114 /* 0x80 is off, 0x81 is on */
118 /* the first byte is for bluetooth and the third one is for wlan */
119 u16 get_wireless_status
;
120 u16 set_wireless_status
;
122 /* 0x80 is off, 0x81 is on */
123 u16 get_lid_handling
;
124 u16 set_lid_handling
;
126 /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
130 * Tell the BIOS that Linux is running on this machine.
131 * 81 is on, 80 is off
136 struct sabi_performance_level
{
143 const char *test_string
;
145 const struct sabi_header_offsets header_offsets
;
146 const struct sabi_commands commands
;
147 const struct sabi_performance_level performance_levels
[4];
152 static const struct sabi_config sabi_configs
[] = {
154 /* I don't know if it is really 2, but it is
155 * less than 3 anyway */
158 .test_string
= "SECLINUX",
160 .main_function
= 0x4c49,
168 .data_segment
= 0x07,
172 .get_brightness
= 0x00,
173 .set_brightness
= 0x01,
175 .get_wireless_button
= 0x02,
176 .set_wireless_button
= 0x03,
178 .get_backlight
= 0x04,
179 .set_backlight
= 0x05,
181 .get_recovery_mode
= 0x06,
182 .set_recovery_mode
= 0x07,
184 .get_performance_level
= 0x08,
185 .set_performance_level
= 0x09,
187 .get_battery_life_extender
= 0xFFFF,
188 .set_battery_life_extender
= 0xFFFF,
190 .get_usb_charge
= 0xFFFF,
191 .set_usb_charge
= 0xFFFF,
193 .get_wireless_status
= 0xFFFF,
194 .set_wireless_status
= 0xFFFF,
196 .get_lid_handling
= 0xFFFF,
197 .set_lid_handling
= 0xFFFF,
199 .kbd_backlight
= 0xFFFF,
204 .performance_levels
= {
221 .test_string
= "SwSmi@",
223 .main_function
= 0x5843,
231 .data_segment
= 0x07,
235 .get_brightness
= 0x10,
236 .set_brightness
= 0x11,
238 .get_wireless_button
= 0x12,
239 .set_wireless_button
= 0x13,
241 .get_backlight
= 0x2d,
242 .set_backlight
= 0x2e,
244 .get_recovery_mode
= 0xff,
245 .set_recovery_mode
= 0xff,
247 .get_performance_level
= 0x31,
248 .set_performance_level
= 0x32,
250 .get_battery_life_extender
= 0x65,
251 .set_battery_life_extender
= 0x66,
253 .get_usb_charge
= 0x67,
254 .set_usb_charge
= 0x68,
256 .get_wireless_status
= 0x69,
257 .set_wireless_status
= 0x6a,
259 .get_lid_handling
= 0x6d,
260 .set_lid_handling
= 0x6e,
262 .kbd_backlight
= 0x78,
267 .performance_levels
= {
289 * samsung-laptop/ - debugfs root directory
290 * f0000_segment - dump f0000 segment
291 * command - current command
292 * data - current data
293 * d0, d1, d2, d3 - data fields
294 * call - call SABI using command and data
296 * This allow to call arbitrary sabi commands wihout
297 * modifying the driver at all.
298 * For example, setting the keyboard backlight brightness to 5
300 * echo 0x78 > command
308 struct samsung_laptop_debug
{
310 struct sabi_data data
;
313 struct debugfs_blob_wrapper f0000_wrapper
;
314 struct debugfs_blob_wrapper data_wrapper
;
315 struct debugfs_blob_wrapper sdiag_wrapper
;
318 struct samsung_laptop
;
320 struct samsung_rfkill
{
321 struct samsung_laptop
*samsung
;
322 struct rfkill
*rfkill
;
323 enum rfkill_type type
;
326 struct samsung_laptop
{
327 const struct sabi_config
*config
;
330 void __iomem
*sabi_iface
;
331 void __iomem
*f0000_segment
;
333 struct mutex sabi_mutex
;
335 struct platform_device
*platform_device
;
336 struct backlight_device
*backlight_device
;
338 struct samsung_rfkill wlan
;
339 struct samsung_rfkill bluetooth
;
341 struct led_classdev kbd_led
;
343 struct workqueue_struct
*led_workqueue
;
344 struct work_struct kbd_led_work
;
346 struct samsung_laptop_debug debug
;
347 struct samsung_quirks
*quirks
;
349 struct notifier_block pm_nb
;
351 bool handle_backlight
;
352 bool has_stepping_quirk
;
357 struct samsung_quirks
{
358 bool four_kbd_backlight_levels
;
359 bool enable_kbd_backlight
;
363 static struct samsung_quirks samsung_unknown
= {};
365 static struct samsung_quirks samsung_np740u3e
= {
366 .four_kbd_backlight_levels
= true,
367 .enable_kbd_backlight
= true,
370 static struct samsung_quirks samsung_lid_handling
= {
371 .lid_handling
= true,
375 module_param(force
, bool, 0);
376 MODULE_PARM_DESC(force
,
377 "Disable the DMI check and forces the driver to be loaded");
380 module_param(debug
, bool, 0644);
381 MODULE_PARM_DESC(debug
, "Debug enabled or not");
383 static int sabi_command(struct samsung_laptop
*samsung
, u16 command
,
384 struct sabi_data
*in
,
385 struct sabi_data
*out
)
387 const struct sabi_config
*config
= samsung
->config
;
389 u16 port
= readw(samsung
->sabi
+ config
->header_offsets
.port
);
390 u8 complete
, iface_data
;
392 mutex_lock(&samsung
->sabi_mutex
);
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
);
400 pr_info("SABI command:0x%04x", command
);
403 /* enable memory to be able to write to it */
404 outb(readb(samsung
->sabi
+ config
->header_offsets
.en_mem
), port
);
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
);
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);
416 outb(readb(samsung
->sabi
+ config
->header_offsets
.iface_func
), port
);
418 /* write protect memory to make it safe */
419 outb(readb(samsung
->sabi
+ config
->header_offsets
.re_mem
), port
);
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
);
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
);
434 if (complete
!= 0xaa || iface_data
== 0xff) {
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);
447 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
448 out
->d0
, out
->d1
, out
->d2
, out
->d3
);
452 mutex_unlock(&samsung
->sabi_mutex
);
456 /* simple wrappers usable with most commands */
457 static int sabi_set_commandb(struct samsung_laptop
*samsung
,
458 u16 command
, u8 data
)
460 struct sabi_data in
= { { { .d0
= 0, .d1
= 0, .d2
= 0, .d3
= 0 } } };
463 return sabi_command(samsung
, command
, &in
, NULL
);
466 static int read_brightness(struct samsung_laptop
*samsung
)
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;
474 retval
= sabi_command(samsung
, commands
->get_brightness
,
479 user_brightness
= sretval
.data
[0];
480 if (user_brightness
> config
->min_brightness
)
481 user_brightness
-= config
->min_brightness
;
485 return user_brightness
;
488 static void set_brightness(struct samsung_laptop
*samsung
, u8 user_brightness
)
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
;
494 if (samsung
->has_stepping_quirk
&& user_level
!= 0) {
496 * short circuit if the specified level is what's already set
497 * to prevent the screen from flickering needlessly
499 if (user_brightness
== read_brightness(samsung
))
502 sabi_set_commandb(samsung
, commands
->set_brightness
, 0);
505 sabi_set_commandb(samsung
, commands
->set_brightness
, user_level
);
508 static int get_brightness(struct backlight_device
*bd
)
510 struct samsung_laptop
*samsung
= bl_get_data(bd
);
512 return read_brightness(samsung
);
515 static void check_for_stepping_quirk(struct samsung_laptop
*samsung
)
519 int orig_level
= read_brightness(samsung
);
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.
529 set_brightness(samsung
, 1);
531 initial_level
= read_brightness(samsung
);
533 if (initial_level
<= 2)
534 check_level
= initial_level
+ 2;
536 check_level
= initial_level
- 2;
538 samsung
->has_stepping_quirk
= false;
539 set_brightness(samsung
, check_level
);
541 if (read_brightness(samsung
) != check_level
) {
542 samsung
->has_stepping_quirk
= true;
543 pr_info("enabled workaround for brightness stepping quirk\n");
546 set_brightness(samsung
, orig_level
);
549 static int update_status(struct backlight_device
*bd
)
551 struct samsung_laptop
*samsung
= bl_get_data(bd
);
552 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
554 set_brightness(samsung
, bd
->props
.brightness
);
556 if (bd
->props
.power
== BACKLIGHT_POWER_ON
)
557 sabi_set_commandb(samsung
, commands
->set_backlight
, 1);
559 sabi_set_commandb(samsung
, commands
->set_backlight
, 0);
564 static const struct backlight_ops backlight_ops
= {
565 .get_brightness
= get_brightness
,
566 .update_status
= update_status
,
569 static int seclinux_rfkill_set(void *data
, bool blocked
)
571 struct samsung_rfkill
*srfkill
= data
;
572 struct samsung_laptop
*samsung
= srfkill
->samsung
;
573 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
575 return sabi_set_commandb(samsung
, commands
->set_wireless_button
,
579 static const struct rfkill_ops seclinux_rfkill_ops
= {
580 .set_block
= seclinux_rfkill_set
,
583 static int swsmi_wireless_status(struct samsung_laptop
*samsung
,
584 struct sabi_data
*data
)
586 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
588 return sabi_command(samsung
, commands
->get_wireless_status
,
592 static int swsmi_rfkill_set(void *priv
, bool blocked
)
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
;
600 ret
= swsmi_wireless_status(samsung
, &data
);
604 /* Don't set the state for non-present devices */
605 for (i
= 0; i
< 4; i
++)
606 if (data
.data
[i
] == 0x02)
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
;
614 return sabi_command(samsung
, commands
->set_wireless_status
,
618 static void swsmi_rfkill_query(struct rfkill
*rfkill
, void *priv
)
620 struct samsung_rfkill
*srfkill
= priv
;
621 struct samsung_laptop
*samsung
= srfkill
->samsung
;
622 struct sabi_data data
;
625 ret
= swsmi_wireless_status(samsung
, &data
);
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
];
636 rfkill_set_sw_state(rfkill
, !ret
);
639 static const struct rfkill_ops swsmi_rfkill_ops
= {
640 .set_block
= swsmi_rfkill_set
,
641 .query
= swsmi_rfkill_query
,
644 static ssize_t
get_performance_level(struct device
*dev
,
645 struct device_attribute
*attr
, char *buf
)
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
;
655 retval
= sabi_command(samsung
, commands
->get_performance_level
,
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 sysfs_emit(buf
, "%s\n", config
->performance_levels
[i
].name
);
665 return sysfs_emit(buf
, "%s\n", "unknown");
668 static ssize_t
set_performance_level(struct device
*dev
,
669 struct device_attribute
*attr
, const char *buf
,
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
;
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
,
691 if (!config
->performance_levels
[i
].name
)
697 static DEVICE_ATTR(performance_level
, 0644,
698 get_performance_level
, set_performance_level
);
700 static int read_battery_life_extender(struct samsung_laptop
*samsung
)
702 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
703 struct sabi_data data
;
706 if (commands
->get_battery_life_extender
== 0xFFFF)
709 memset(&data
, 0, sizeof(data
));
711 retval
= sabi_command(samsung
, commands
->get_battery_life_extender
,
717 if (data
.data
[0] != 0 && data
.data
[0] != 1)
723 static int write_battery_life_extender(struct samsung_laptop
*samsung
,
726 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
727 struct sabi_data data
;
729 memset(&data
, 0, sizeof(data
));
730 data
.data
[0] = 0x80 | enabled
;
731 return sabi_command(samsung
, commands
->set_battery_life_extender
,
735 static ssize_t
get_battery_life_extender(struct device
*dev
,
736 struct device_attribute
*attr
,
739 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
742 ret
= read_battery_life_extender(samsung
);
746 return sysfs_emit(buf
, "%d\n", ret
);
749 static ssize_t
set_battery_life_extender(struct device
*dev
,
750 struct device_attribute
*attr
,
751 const char *buf
, size_t count
)
753 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
756 if (!count
|| kstrtoint(buf
, 0, &value
) != 0)
759 ret
= write_battery_life_extender(samsung
, !!value
);
766 static DEVICE_ATTR(battery_life_extender
, 0644,
767 get_battery_life_extender
, set_battery_life_extender
);
769 static int read_usb_charge(struct samsung_laptop
*samsung
)
771 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
772 struct sabi_data data
;
775 if (commands
->get_usb_charge
== 0xFFFF)
778 memset(&data
, 0, sizeof(data
));
780 retval
= sabi_command(samsung
, commands
->get_usb_charge
,
786 if (data
.data
[0] != 0 && data
.data
[0] != 1)
792 static int write_usb_charge(struct samsung_laptop
*samsung
,
795 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
796 struct sabi_data data
;
798 memset(&data
, 0, sizeof(data
));
799 data
.data
[0] = 0x80 | enabled
;
800 return sabi_command(samsung
, commands
->set_usb_charge
,
804 static ssize_t
get_usb_charge(struct device
*dev
,
805 struct device_attribute
*attr
,
808 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
811 ret
= read_usb_charge(samsung
);
815 return sysfs_emit(buf
, "%d\n", ret
);
818 static ssize_t
set_usb_charge(struct device
*dev
,
819 struct device_attribute
*attr
,
820 const char *buf
, size_t count
)
822 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
825 if (!count
|| kstrtoint(buf
, 0, &value
) != 0)
828 ret
= write_usb_charge(samsung
, !!value
);
835 static DEVICE_ATTR(usb_charge
, 0644,
836 get_usb_charge
, set_usb_charge
);
838 static int read_lid_handling(struct samsung_laptop
*samsung
)
840 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
841 struct sabi_data data
;
844 if (commands
->get_lid_handling
== 0xFFFF)
847 memset(&data
, 0, sizeof(data
));
848 retval
= sabi_command(samsung
, commands
->get_lid_handling
,
854 return data
.data
[0] & 0x1;
857 static int write_lid_handling(struct samsung_laptop
*samsung
,
860 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
861 struct sabi_data data
;
863 memset(&data
, 0, sizeof(data
));
864 data
.data
[0] = 0x80 | enabled
;
865 return sabi_command(samsung
, commands
->set_lid_handling
,
869 static ssize_t
get_lid_handling(struct device
*dev
,
870 struct device_attribute
*attr
,
873 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
876 ret
= read_lid_handling(samsung
);
880 return sysfs_emit(buf
, "%d\n", ret
);
883 static ssize_t
set_lid_handling(struct device
*dev
,
884 struct device_attribute
*attr
,
885 const char *buf
, size_t count
)
887 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
890 if (!count
|| kstrtoint(buf
, 0, &value
) != 0)
893 ret
= write_lid_handling(samsung
, !!value
);
900 static DEVICE_ATTR(lid_handling
, 0644,
901 get_lid_handling
, set_lid_handling
);
903 static struct attribute
*platform_attributes
[] = {
904 &dev_attr_performance_level
.attr
,
905 &dev_attr_battery_life_extender
.attr
,
906 &dev_attr_usb_charge
.attr
,
907 &dev_attr_lid_handling
.attr
,
911 static int find_signature(void __iomem
*memcheck
, const char *testStr
)
916 for (loca
= 0; loca
< 0xffff; loca
++) {
917 char temp
= readb(memcheck
+ loca
);
919 if (temp
== testStr
[i
]) {
920 if (i
== strlen(testStr
)-1)
930 static void samsung_rfkill_exit(struct samsung_laptop
*samsung
)
932 if (samsung
->wlan
.rfkill
) {
933 rfkill_unregister(samsung
->wlan
.rfkill
);
934 rfkill_destroy(samsung
->wlan
.rfkill
);
935 samsung
->wlan
.rfkill
= NULL
;
937 if (samsung
->bluetooth
.rfkill
) {
938 rfkill_unregister(samsung
->bluetooth
.rfkill
);
939 rfkill_destroy(samsung
->bluetooth
.rfkill
);
940 samsung
->bluetooth
.rfkill
= NULL
;
944 static int samsung_new_rfkill(struct samsung_laptop
*samsung
,
945 struct samsung_rfkill
*arfkill
,
946 const char *name
, enum rfkill_type type
,
947 const struct rfkill_ops
*ops
,
950 struct rfkill
**rfkill
= &arfkill
->rfkill
;
953 arfkill
->type
= type
;
954 arfkill
->samsung
= samsung
;
956 *rfkill
= rfkill_alloc(name
, &samsung
->platform_device
->dev
,
963 rfkill_init_sw_state(*rfkill
, blocked
);
965 ret
= rfkill_register(*rfkill
);
967 rfkill_destroy(*rfkill
);
974 static int __init
samsung_rfkill_init_seclinux(struct samsung_laptop
*samsung
)
976 return samsung_new_rfkill(samsung
, &samsung
->wlan
, "samsung-wlan",
977 RFKILL_TYPE_WLAN
, &seclinux_rfkill_ops
, -1);
980 static int __init
samsung_rfkill_init_swsmi(struct samsung_laptop
*samsung
)
982 struct sabi_data data
;
985 ret
= swsmi_wireless_status(samsung
, &data
);
987 /* Some swsmi laptops use the old seclinux way to control
988 * wireless devices */
990 ret
= samsung_rfkill_init_seclinux(samsung
);
994 /* 0x02 seems to mean that the device is no present/available */
996 if (data
.data
[WL_STATUS_WLAN
] != 0x02)
997 ret
= samsung_new_rfkill(samsung
, &samsung
->wlan
,
1001 !data
.data
[WL_STATUS_WLAN
]);
1005 if (data
.data
[WL_STATUS_BT
] != 0x02)
1006 ret
= samsung_new_rfkill(samsung
, &samsung
->bluetooth
,
1007 "samsung-bluetooth",
1008 RFKILL_TYPE_BLUETOOTH
,
1010 !data
.data
[WL_STATUS_BT
]);
1016 samsung_rfkill_exit(samsung
);
1021 static int __init
samsung_rfkill_init(struct samsung_laptop
*samsung
)
1023 if (samsung
->config
->sabi_version
== 2)
1024 return samsung_rfkill_init_seclinux(samsung
);
1025 if (samsung
->config
->sabi_version
== 3)
1026 return samsung_rfkill_init_swsmi(samsung
);
1030 static void samsung_lid_handling_exit(struct samsung_laptop
*samsung
)
1032 if (samsung
->quirks
->lid_handling
)
1033 write_lid_handling(samsung
, 0);
1036 static int __init
samsung_lid_handling_init(struct samsung_laptop
*samsung
)
1040 if (samsung
->quirks
->lid_handling
)
1041 retval
= write_lid_handling(samsung
, 1);
1046 static int kbd_backlight_enable(struct samsung_laptop
*samsung
)
1048 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
1049 struct sabi_data data
;
1052 if (commands
->kbd_backlight
== 0xFFFF)
1055 memset(&data
, 0, sizeof(data
));
1057 retval
= sabi_command(samsung
, commands
->kbd_backlight
,
1063 if (data
.d0
!= 0xccdd)
1068 static int kbd_backlight_read(struct samsung_laptop
*samsung
)
1070 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
1071 struct sabi_data data
;
1074 memset(&data
, 0, sizeof(data
));
1075 data
.data
[0] = 0x81;
1076 retval
= sabi_command(samsung
, commands
->kbd_backlight
,
1082 return data
.data
[0];
1085 static int kbd_backlight_write(struct samsung_laptop
*samsung
, int brightness
)
1087 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
1088 struct sabi_data data
;
1090 memset(&data
, 0, sizeof(data
));
1091 data
.d0
= 0x82 | ((brightness
& 0xFF) << 8);
1092 return sabi_command(samsung
, commands
->kbd_backlight
,
1096 static void kbd_led_update(struct work_struct
*work
)
1098 struct samsung_laptop
*samsung
;
1100 samsung
= container_of(work
, struct samsung_laptop
, kbd_led_work
);
1101 kbd_backlight_write(samsung
, samsung
->kbd_led_wk
);
1104 static void kbd_led_set(struct led_classdev
*led_cdev
,
1105 enum led_brightness value
)
1107 struct samsung_laptop
*samsung
;
1109 samsung
= container_of(led_cdev
, struct samsung_laptop
, kbd_led
);
1111 if (value
> samsung
->kbd_led
.max_brightness
)
1112 value
= samsung
->kbd_led
.max_brightness
;
1114 samsung
->kbd_led_wk
= value
;
1115 queue_work(samsung
->led_workqueue
, &samsung
->kbd_led_work
);
1118 static enum led_brightness
kbd_led_get(struct led_classdev
*led_cdev
)
1120 struct samsung_laptop
*samsung
;
1122 samsung
= container_of(led_cdev
, struct samsung_laptop
, kbd_led
);
1123 return kbd_backlight_read(samsung
);
1126 static void samsung_leds_exit(struct samsung_laptop
*samsung
)
1128 led_classdev_unregister(&samsung
->kbd_led
);
1129 if (samsung
->led_workqueue
)
1130 destroy_workqueue(samsung
->led_workqueue
);
1133 static int __init
samsung_leds_init(struct samsung_laptop
*samsung
)
1137 samsung
->led_workqueue
= create_singlethread_workqueue("led_workqueue");
1138 if (!samsung
->led_workqueue
)
1141 if (kbd_backlight_enable(samsung
) >= 0) {
1142 INIT_WORK(&samsung
->kbd_led_work
, kbd_led_update
);
1144 samsung
->kbd_led
.name
= "samsung::kbd_backlight";
1145 samsung
->kbd_led
.brightness_set
= kbd_led_set
;
1146 samsung
->kbd_led
.brightness_get
= kbd_led_get
;
1147 samsung
->kbd_led
.max_brightness
= 8;
1148 if (samsung
->quirks
->four_kbd_backlight_levels
)
1149 samsung
->kbd_led
.max_brightness
= 4;
1151 ret
= led_classdev_register(&samsung
->platform_device
->dev
,
1156 samsung_leds_exit(samsung
);
1161 static void samsung_backlight_exit(struct samsung_laptop
*samsung
)
1163 if (samsung
->backlight_device
) {
1164 backlight_device_unregister(samsung
->backlight_device
);
1165 samsung
->backlight_device
= NULL
;
1169 static int __init
samsung_backlight_init(struct samsung_laptop
*samsung
)
1171 struct backlight_device
*bd
;
1172 struct backlight_properties props
;
1174 if (!samsung
->handle_backlight
)
1177 memset(&props
, 0, sizeof(struct backlight_properties
));
1178 props
.type
= BACKLIGHT_PLATFORM
;
1179 props
.max_brightness
= samsung
->config
->max_brightness
-
1180 samsung
->config
->min_brightness
;
1182 bd
= backlight_device_register("samsung",
1183 &samsung
->platform_device
->dev
,
1184 samsung
, &backlight_ops
,
1189 samsung
->backlight_device
= bd
;
1190 samsung
->backlight_device
->props
.brightness
= read_brightness(samsung
);
1191 samsung
->backlight_device
->props
.power
= BACKLIGHT_POWER_ON
;
1192 backlight_update_status(samsung
->backlight_device
);
1197 static umode_t
samsung_sysfs_is_visible(struct kobject
*kobj
,
1198 struct attribute
*attr
, int idx
)
1200 struct device
*dev
= kobj_to_dev(kobj
);
1201 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
1204 if (attr
== &dev_attr_performance_level
.attr
)
1205 ok
= !!samsung
->config
->performance_levels
[0].name
;
1206 if (attr
== &dev_attr_battery_life_extender
.attr
)
1207 ok
= !!(read_battery_life_extender(samsung
) >= 0);
1208 if (attr
== &dev_attr_usb_charge
.attr
)
1209 ok
= !!(read_usb_charge(samsung
) >= 0);
1210 if (attr
== &dev_attr_lid_handling
.attr
)
1211 ok
= !!(read_lid_handling(samsung
) >= 0);
1213 return ok
? attr
->mode
: 0;
1216 static const struct attribute_group platform_attribute_group
= {
1217 .is_visible
= samsung_sysfs_is_visible
,
1218 .attrs
= platform_attributes
1221 static void samsung_sysfs_exit(struct samsung_laptop
*samsung
)
1223 struct platform_device
*device
= samsung
->platform_device
;
1225 sysfs_remove_group(&device
->dev
.kobj
, &platform_attribute_group
);
1228 static int __init
samsung_sysfs_init(struct samsung_laptop
*samsung
)
1230 struct platform_device
*device
= samsung
->platform_device
;
1232 return sysfs_create_group(&device
->dev
.kobj
, &platform_attribute_group
);
1236 static int samsung_laptop_call_show(struct seq_file
*m
, void *data
)
1238 struct samsung_laptop
*samsung
= m
->private;
1239 struct sabi_data
*sdata
= &samsung
->debug
.data
;
1242 seq_printf(m
, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1243 samsung
->debug
.command
,
1244 sdata
->d0
, sdata
->d1
, sdata
->d2
, sdata
->d3
);
1246 ret
= sabi_command(samsung
, samsung
->debug
.command
, sdata
, sdata
);
1249 seq_printf(m
, "SABI command 0x%04x failed\n",
1250 samsung
->debug
.command
);
1254 seq_printf(m
, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1255 sdata
->d0
, sdata
->d1
, sdata
->d2
, sdata
->d3
);
1258 DEFINE_SHOW_ATTRIBUTE(samsung_laptop_call
);
1260 static void samsung_debugfs_exit(struct samsung_laptop
*samsung
)
1262 debugfs_remove_recursive(samsung
->debug
.root
);
1265 static void samsung_debugfs_init(struct samsung_laptop
*samsung
)
1267 struct dentry
*root
;
1269 root
= debugfs_create_dir("samsung-laptop", NULL
);
1270 samsung
->debug
.root
= root
;
1272 samsung
->debug
.f0000_wrapper
.data
= samsung
->f0000_segment
;
1273 samsung
->debug
.f0000_wrapper
.size
= 0xffff;
1275 samsung
->debug
.data_wrapper
.data
= &samsung
->debug
.data
;
1276 samsung
->debug
.data_wrapper
.size
= sizeof(samsung
->debug
.data
);
1278 samsung
->debug
.sdiag_wrapper
.data
= samsung
->sdiag
;
1279 samsung
->debug
.sdiag_wrapper
.size
= strlen(samsung
->sdiag
);
1281 debugfs_create_u16("command", 0644, root
, &samsung
->debug
.command
);
1282 debugfs_create_u32("d0", 0644, root
, &samsung
->debug
.data
.d0
);
1283 debugfs_create_u32("d1", 0644, root
, &samsung
->debug
.data
.d1
);
1284 debugfs_create_u16("d2", 0644, root
, &samsung
->debug
.data
.d2
);
1285 debugfs_create_u8("d3", 0644, root
, &samsung
->debug
.data
.d3
);
1286 debugfs_create_blob("data", 0444, root
, &samsung
->debug
.data_wrapper
);
1287 debugfs_create_blob("f0000_segment", 0400, root
,
1288 &samsung
->debug
.f0000_wrapper
);
1289 debugfs_create_file("call", 0444, root
, samsung
,
1290 &samsung_laptop_call_fops
);
1291 debugfs_create_blob("sdiag", 0444, root
, &samsung
->debug
.sdiag_wrapper
);
1294 static void samsung_sabi_exit(struct samsung_laptop
*samsung
)
1296 const struct sabi_config
*config
= samsung
->config
;
1298 /* Turn off "Linux" mode in the BIOS */
1299 if (config
&& config
->commands
.set_linux
!= 0xff)
1300 sabi_set_commandb(samsung
, config
->commands
.set_linux
, 0x80);
1302 if (samsung
->sabi_iface
) {
1303 iounmap(samsung
->sabi_iface
);
1304 samsung
->sabi_iface
= NULL
;
1306 if (samsung
->f0000_segment
) {
1307 iounmap(samsung
->f0000_segment
);
1308 samsung
->f0000_segment
= NULL
;
1311 samsung
->config
= NULL
;
1314 static __init
void samsung_sabi_infos(struct samsung_laptop
*samsung
, int loca
,
1315 unsigned int ifaceP
)
1317 const struct sabi_config
*config
= samsung
->config
;
1319 printk(KERN_DEBUG
"This computer supports SABI==%x\n",
1320 loca
+ 0xf0000 - 6);
1322 printk(KERN_DEBUG
"SABI header:\n");
1323 printk(KERN_DEBUG
" SMI Port Number = 0x%04x\n",
1324 readw(samsung
->sabi
+ config
->header_offsets
.port
));
1325 printk(KERN_DEBUG
" SMI Interface Function = 0x%02x\n",
1326 readb(samsung
->sabi
+ config
->header_offsets
.iface_func
));
1327 printk(KERN_DEBUG
" SMI enable memory buffer = 0x%02x\n",
1328 readb(samsung
->sabi
+ config
->header_offsets
.en_mem
));
1329 printk(KERN_DEBUG
" SMI restore memory buffer = 0x%02x\n",
1330 readb(samsung
->sabi
+ config
->header_offsets
.re_mem
));
1331 printk(KERN_DEBUG
" SABI data offset = 0x%04x\n",
1332 readw(samsung
->sabi
+ config
->header_offsets
.data_offset
));
1333 printk(KERN_DEBUG
" SABI data segment = 0x%04x\n",
1334 readw(samsung
->sabi
+ config
->header_offsets
.data_segment
));
1336 printk(KERN_DEBUG
" SABI pointer = 0x%08x\n", ifaceP
);
1339 static void __init
samsung_sabi_diag(struct samsung_laptop
*samsung
)
1341 int loca
= find_signature(samsung
->f0000_segment
, "SDiaG@");
1348 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1350 * Product name: 90X3A
1351 * BIOS Version: 07HL
1354 for (i
= 0; loca
< 0xffff && i
< sizeof(samsung
->sdiag
) - 1; loca
++) {
1355 char temp
= readb(samsung
->f0000_segment
+ loca
);
1357 if (isalnum(temp
) || temp
== '/' || temp
== '-')
1358 samsung
->sdiag
[i
++] = temp
;
1363 if (debug
&& samsung
->sdiag
[0])
1364 pr_info("sdiag: %s", samsung
->sdiag
);
1367 static int __init
samsung_sabi_init(struct samsung_laptop
*samsung
)
1369 const struct sabi_config
*config
= NULL
;
1370 const struct sabi_commands
*commands
;
1371 unsigned int ifaceP
;
1376 samsung
->f0000_segment
= ioremap(0xf0000, 0xffff);
1377 if (!samsung
->f0000_segment
) {
1379 pr_err("Can't map the segment at 0xf0000\n");
1384 samsung_sabi_diag(samsung
);
1386 /* Try to find one of the signatures in memory to find the header */
1387 for (i
= 0; sabi_configs
[i
].test_string
!= NULL
; ++i
) {
1388 samsung
->config
= &sabi_configs
[i
];
1389 loca
= find_signature(samsung
->f0000_segment
,
1390 samsung
->config
->test_string
);
1395 if (loca
== 0xffff) {
1397 pr_err("This computer does not support SABI\n");
1402 config
= samsung
->config
;
1403 commands
= &config
->commands
;
1405 /* point to the SMI port Number */
1407 samsung
->sabi
= (samsung
->f0000_segment
+ loca
);
1409 /* Get a pointer to the SABI Interface */
1410 ifaceP
= (readw(samsung
->sabi
+ config
->header_offsets
.data_segment
) & 0x0ffff) << 4;
1411 ifaceP
+= readw(samsung
->sabi
+ config
->header_offsets
.data_offset
) & 0x0ffff;
1414 samsung_sabi_infos(samsung
, loca
, ifaceP
);
1416 samsung
->sabi_iface
= ioremap(ifaceP
, 16);
1417 if (!samsung
->sabi_iface
) {
1418 pr_err("Can't remap %x\n", ifaceP
);
1423 /* Turn on "Linux" mode in the BIOS */
1424 if (commands
->set_linux
!= 0xff) {
1425 int retval
= sabi_set_commandb(samsung
,
1426 commands
->set_linux
, 0x81);
1428 pr_warn("Linux mode was not set!\n");
1434 /* Check for stepping quirk */
1435 if (samsung
->handle_backlight
)
1436 check_for_stepping_quirk(samsung
);
1438 pr_info("detected SABI interface: %s\n",
1439 samsung
->config
->test_string
);
1443 samsung_sabi_exit(samsung
);
1448 static void samsung_platform_exit(struct samsung_laptop
*samsung
)
1450 if (samsung
->platform_device
) {
1451 platform_device_unregister(samsung
->platform_device
);
1452 samsung
->platform_device
= NULL
;
1456 static int samsung_pm_notification(struct notifier_block
*nb
,
1457 unsigned long val
, void *ptr
)
1459 struct samsung_laptop
*samsung
;
1461 samsung
= container_of(nb
, struct samsung_laptop
, pm_nb
);
1462 if (val
== PM_POST_HIBERNATION
&&
1463 samsung
->quirks
->enable_kbd_backlight
)
1464 kbd_backlight_enable(samsung
);
1466 if (val
== PM_POST_HIBERNATION
&& samsung
->quirks
->lid_handling
)
1467 write_lid_handling(samsung
, 1);
1472 static int __init
samsung_platform_init(struct samsung_laptop
*samsung
)
1474 struct platform_device
*pdev
;
1476 pdev
= platform_device_register_simple("samsung", PLATFORM_DEVID_NONE
, NULL
, 0);
1478 return PTR_ERR(pdev
);
1480 samsung
->platform_device
= pdev
;
1481 platform_set_drvdata(samsung
->platform_device
, samsung
);
1485 static struct samsung_quirks
*quirks
;
1487 static int __init
samsung_dmi_matched(const struct dmi_system_id
*d
)
1489 quirks
= d
->driver_data
;
1493 static const struct dmi_system_id samsung_dmi_table
[] __initconst
= {
1496 DMI_MATCH(DMI_SYS_VENDOR
,
1497 "SAMSUNG ELECTRONICS CO., LTD."),
1498 DMI_MATCH(DMI_CHASSIS_TYPE
, "8"), /* Portable */
1503 DMI_MATCH(DMI_SYS_VENDOR
,
1504 "SAMSUNG ELECTRONICS CO., LTD."),
1505 DMI_MATCH(DMI_CHASSIS_TYPE
, "9"), /* Laptop */
1510 DMI_MATCH(DMI_SYS_VENDOR
,
1511 "SAMSUNG ELECTRONICS CO., LTD."),
1512 DMI_MATCH(DMI_CHASSIS_TYPE
, "10"), /* Notebook */
1517 DMI_MATCH(DMI_SYS_VENDOR
,
1518 "SAMSUNG ELECTRONICS CO., LTD."),
1519 DMI_MATCH(DMI_CHASSIS_TYPE
, "14"), /* Sub-Notebook */
1522 /* DMI ids for laptops with bad Chassis Type */
1526 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1527 DMI_MATCH(DMI_PRODUCT_NAME
, "R40/R41"),
1528 DMI_MATCH(DMI_BOARD_NAME
, "R40/R41"),
1531 /* Specific DMI ids for laptop with quirks */
1533 .callback
= samsung_dmi_matched
,
1534 .ident
= "730U3E/740U3E",
1536 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1537 DMI_MATCH(DMI_PRODUCT_NAME
, "730U3E/740U3E"),
1539 .driver_data
= &samsung_np740u3e
,
1542 .callback
= samsung_dmi_matched
,
1543 .ident
= "300V3Z/300V4Z/300V5Z",
1545 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1546 DMI_MATCH(DMI_PRODUCT_NAME
, "300V3Z/300V4Z/300V5Z"),
1548 .driver_data
= &samsung_lid_handling
,
1552 MODULE_DEVICE_TABLE(dmi
, samsung_dmi_table
);
1554 static struct platform_device
*samsung_platform_device
;
1556 static int __init
samsung_init(void)
1558 struct samsung_laptop
*samsung
;
1561 if (efi_enabled(EFI_BOOT
))
1564 quirks
= &samsung_unknown
;
1565 if (!force
&& !dmi_check_system(samsung_dmi_table
))
1568 samsung
= kzalloc(sizeof(*samsung
), GFP_KERNEL
);
1572 mutex_init(&samsung
->sabi_mutex
);
1573 samsung
->handle_backlight
= true;
1574 samsung
->quirks
= quirks
;
1576 if (acpi_video_get_backlight_type() != acpi_backlight_vendor
)
1577 samsung
->handle_backlight
= false;
1579 ret
= samsung_platform_init(samsung
);
1581 goto error_platform
;
1583 ret
= samsung_sabi_init(samsung
);
1587 ret
= samsung_sysfs_init(samsung
);
1591 ret
= samsung_backlight_init(samsung
);
1593 goto error_backlight
;
1595 ret
= samsung_rfkill_init(samsung
);
1599 ret
= samsung_leds_init(samsung
);
1603 ret
= samsung_lid_handling_init(samsung
);
1605 goto error_lid_handling
;
1607 samsung_debugfs_init(samsung
);
1609 samsung
->pm_nb
.notifier_call
= samsung_pm_notification
;
1610 register_pm_notifier(&samsung
->pm_nb
);
1612 samsung_platform_device
= samsung
->platform_device
;
1616 samsung_leds_exit(samsung
);
1618 samsung_rfkill_exit(samsung
);
1620 samsung_backlight_exit(samsung
);
1622 samsung_sysfs_exit(samsung
);
1624 samsung_sabi_exit(samsung
);
1626 samsung_platform_exit(samsung
);
1632 static void __exit
samsung_exit(void)
1634 struct samsung_laptop
*samsung
;
1636 samsung
= platform_get_drvdata(samsung_platform_device
);
1637 unregister_pm_notifier(&samsung
->pm_nb
);
1639 samsung_debugfs_exit(samsung
);
1640 samsung_lid_handling_exit(samsung
);
1641 samsung_leds_exit(samsung
);
1642 samsung_rfkill_exit(samsung
);
1643 samsung_backlight_exit(samsung
);
1644 samsung_sysfs_exit(samsung
);
1645 samsung_sabi_exit(samsung
);
1646 samsung_platform_exit(samsung
);
1649 samsung_platform_device
= NULL
;
1652 module_init(samsung_init
);
1653 module_exit(samsung_exit
);
1655 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1656 MODULE_DESCRIPTION("Samsung Backlight driver");
1657 MODULE_LICENSE("GPL");