2 * Samsung Laptop driver
4 * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
5 * Copyright (C) 2009,2011 Novell Inc.
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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 <acpi/video.h>
33 * This driver is needed because a number of Samsung laptops do not hook
34 * their control settings through ACPI. So we have to poke around in the
35 * BIOS to do things like brightness values, and "special" key controls.
39 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
40 * be reserved by the BIOS (which really doesn't make much sense), we tell
41 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
43 #define MAX_BRIGHT 0x07
46 #define SABI_IFACE_MAIN 0x00
47 #define SABI_IFACE_SUB 0x02
48 #define SABI_IFACE_COMPLETE 0x04
49 #define SABI_IFACE_DATA 0x05
51 #define WL_STATUS_WLAN 0x0
52 #define WL_STATUS_BT 0x2
54 /* Structure get/set data using sabi */
67 struct sabi_header_offsets
{
76 struct sabi_commands
{
78 * Brightness is 0 - 8, as described above.
79 * Value 0 is for the BIOS to use
86 * 0x00 - wireless is off
87 * 0x01 - wireless is on
91 * TODO, verify 3G is correct, that doesn't seem right...
93 u16 get_wireless_button
;
94 u16 set_wireless_button
;
96 /* 0 is off, 1 is on */
101 * 0x80 or 0x00 - no action
102 * 0x81 - recovery key pressed
104 u16 get_recovery_mode
;
105 u16 set_recovery_mode
;
108 * on seclinux: 0 is low, 1 is high,
109 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
111 u16 get_performance_level
;
112 u16 set_performance_level
;
114 /* 0x80 is off, 0x81 is on */
115 u16 get_battery_life_extender
;
116 u16 set_battery_life_extender
;
118 /* 0x80 is off, 0x81 is on */
122 /* the first byte is for bluetooth and the third one is for wlan */
123 u16 get_wireless_status
;
124 u16 set_wireless_status
;
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 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 .kbd_backlight
= 0xFFFF,
201 .performance_levels
= {
218 .test_string
= "SwSmi@",
220 .main_function
= 0x5843,
228 .data_segment
= 0x07,
232 .get_brightness
= 0x10,
233 .set_brightness
= 0x11,
235 .get_wireless_button
= 0x12,
236 .set_wireless_button
= 0x13,
238 .get_backlight
= 0x2d,
239 .set_backlight
= 0x2e,
241 .get_recovery_mode
= 0xff,
242 .set_recovery_mode
= 0xff,
244 .get_performance_level
= 0x31,
245 .set_performance_level
= 0x32,
247 .get_battery_life_extender
= 0x65,
248 .set_battery_life_extender
= 0x66,
250 .get_usb_charge
= 0x67,
251 .set_usb_charge
= 0x68,
253 .get_wireless_status
= 0x69,
254 .set_wireless_status
= 0x6a,
256 .kbd_backlight
= 0x78,
261 .performance_levels
= {
283 * samsung-laptop/ - debugfs root directory
284 * f0000_segment - dump f0000 segment
285 * command - current command
286 * data - current data
287 * d0, d1, d2, d3 - data fields
288 * call - call SABI using command and data
290 * This allow to call arbitrary sabi commands wihout
291 * modifying the driver at all.
292 * For example, setting the keyboard backlight brightness to 5
294 * echo 0x78 > command
302 struct samsung_laptop_debug
{
304 struct sabi_data data
;
307 struct debugfs_blob_wrapper f0000_wrapper
;
308 struct debugfs_blob_wrapper data_wrapper
;
309 struct debugfs_blob_wrapper sdiag_wrapper
;
312 struct samsung_laptop
;
314 struct samsung_rfkill
{
315 struct samsung_laptop
*samsung
;
316 struct rfkill
*rfkill
;
317 enum rfkill_type type
;
320 struct samsung_laptop
{
321 const struct sabi_config
*config
;
324 void __iomem
*sabi_iface
;
325 void __iomem
*f0000_segment
;
327 struct mutex sabi_mutex
;
329 struct platform_device
*platform_device
;
330 struct backlight_device
*backlight_device
;
332 struct samsung_rfkill wlan
;
333 struct samsung_rfkill bluetooth
;
335 struct led_classdev kbd_led
;
337 struct workqueue_struct
*led_workqueue
;
338 struct work_struct kbd_led_work
;
340 struct samsung_laptop_debug debug
;
341 struct samsung_quirks
*quirks
;
343 bool handle_backlight
;
344 bool has_stepping_quirk
;
349 struct samsung_quirks
{
350 bool broken_acpi_video
;
353 static struct samsung_quirks samsung_unknown
= {};
355 static struct samsung_quirks samsung_broken_acpi_video
= {
356 .broken_acpi_video
= true,
360 module_param(force
, bool, 0);
361 MODULE_PARM_DESC(force
,
362 "Disable the DMI check and forces the driver to be loaded");
365 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
366 MODULE_PARM_DESC(debug
, "Debug enabled or not");
368 static int sabi_command(struct samsung_laptop
*samsung
, u16 command
,
369 struct sabi_data
*in
,
370 struct sabi_data
*out
)
372 const struct sabi_config
*config
= samsung
->config
;
374 u16 port
= readw(samsung
->sabi
+ config
->header_offsets
.port
);
375 u8 complete
, iface_data
;
377 mutex_lock(&samsung
->sabi_mutex
);
381 pr_info("SABI command:0x%04x "
382 "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
383 command
, in
->d0
, in
->d1
, in
->d2
, in
->d3
);
385 pr_info("SABI command:0x%04x", command
);
388 /* enable memory to be able to write to it */
389 outb(readb(samsung
->sabi
+ config
->header_offsets
.en_mem
), port
);
391 /* write out the command */
392 writew(config
->main_function
, samsung
->sabi_iface
+ SABI_IFACE_MAIN
);
393 writew(command
, samsung
->sabi_iface
+ SABI_IFACE_SUB
);
394 writeb(0, samsung
->sabi_iface
+ SABI_IFACE_COMPLETE
);
396 writel(in
->d0
, samsung
->sabi_iface
+ SABI_IFACE_DATA
);
397 writel(in
->d1
, samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 4);
398 writew(in
->d2
, samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 8);
399 writeb(in
->d3
, samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 10);
401 outb(readb(samsung
->sabi
+ config
->header_offsets
.iface_func
), port
);
403 /* write protect memory to make it safe */
404 outb(readb(samsung
->sabi
+ config
->header_offsets
.re_mem
), port
);
406 /* see if the command actually succeeded */
407 complete
= readb(samsung
->sabi_iface
+ SABI_IFACE_COMPLETE
);
408 iface_data
= readb(samsung
->sabi_iface
+ SABI_IFACE_DATA
);
410 /* iface_data = 0xFF happens when a command is not known
411 * so we only add a warning in debug mode since we will
412 * probably issue some unknown command at startup to find
413 * out which features are supported */
414 if (complete
!= 0xaa || (iface_data
== 0xff && debug
))
415 pr_warn("SABI command 0x%04x failed with"
416 " completion flag 0x%02x and interface data 0x%02x",
417 command
, complete
, iface_data
);
419 if (complete
!= 0xaa || iface_data
== 0xff) {
425 out
->d0
= readl(samsung
->sabi_iface
+ SABI_IFACE_DATA
);
426 out
->d1
= readl(samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 4);
427 out
->d2
= readw(samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 2);
428 out
->d3
= readb(samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 1);
432 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
433 out
->d0
, out
->d1
, out
->d2
, out
->d3
);
437 mutex_unlock(&samsung
->sabi_mutex
);
441 /* simple wrappers usable with most commands */
442 static int sabi_set_commandb(struct samsung_laptop
*samsung
,
443 u16 command
, u8 data
)
445 struct sabi_data in
= { { { .d0
= 0, .d1
= 0, .d2
= 0, .d3
= 0 } } };
448 return sabi_command(samsung
, command
, &in
, NULL
);
451 static int read_brightness(struct samsung_laptop
*samsung
)
453 const struct sabi_config
*config
= samsung
->config
;
454 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
455 struct sabi_data sretval
;
456 int user_brightness
= 0;
459 retval
= sabi_command(samsung
, commands
->get_brightness
,
464 user_brightness
= sretval
.data
[0];
465 if (user_brightness
> config
->min_brightness
)
466 user_brightness
-= config
->min_brightness
;
470 return user_brightness
;
473 static void set_brightness(struct samsung_laptop
*samsung
, u8 user_brightness
)
475 const struct sabi_config
*config
= samsung
->config
;
476 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
477 u8 user_level
= user_brightness
+ config
->min_brightness
;
479 if (samsung
->has_stepping_quirk
&& user_level
!= 0) {
481 * short circuit if the specified level is what's already set
482 * to prevent the screen from flickering needlessly
484 if (user_brightness
== read_brightness(samsung
))
487 sabi_set_commandb(samsung
, commands
->set_brightness
, 0);
490 sabi_set_commandb(samsung
, commands
->set_brightness
, user_level
);
493 static int get_brightness(struct backlight_device
*bd
)
495 struct samsung_laptop
*samsung
= bl_get_data(bd
);
497 return read_brightness(samsung
);
500 static void check_for_stepping_quirk(struct samsung_laptop
*samsung
)
504 int orig_level
= read_brightness(samsung
);
507 * Some laptops exhibit the strange behaviour of stepping toward
508 * (rather than setting) the brightness except when changing to/from
509 * brightness level 0. This behaviour is checked for here and worked
510 * around in set_brightness.
514 set_brightness(samsung
, 1);
516 initial_level
= read_brightness(samsung
);
518 if (initial_level
<= 2)
519 check_level
= initial_level
+ 2;
521 check_level
= initial_level
- 2;
523 samsung
->has_stepping_quirk
= false;
524 set_brightness(samsung
, check_level
);
526 if (read_brightness(samsung
) != check_level
) {
527 samsung
->has_stepping_quirk
= true;
528 pr_info("enabled workaround for brightness stepping quirk\n");
531 set_brightness(samsung
, orig_level
);
534 static int update_status(struct backlight_device
*bd
)
536 struct samsung_laptop
*samsung
= bl_get_data(bd
);
537 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
539 set_brightness(samsung
, bd
->props
.brightness
);
541 if (bd
->props
.power
== FB_BLANK_UNBLANK
)
542 sabi_set_commandb(samsung
, commands
->set_backlight
, 1);
544 sabi_set_commandb(samsung
, commands
->set_backlight
, 0);
549 static const struct backlight_ops backlight_ops
= {
550 .get_brightness
= get_brightness
,
551 .update_status
= update_status
,
554 static int seclinux_rfkill_set(void *data
, bool blocked
)
556 struct samsung_rfkill
*srfkill
= data
;
557 struct samsung_laptop
*samsung
= srfkill
->samsung
;
558 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
560 return sabi_set_commandb(samsung
, commands
->set_wireless_button
,
564 static struct rfkill_ops seclinux_rfkill_ops
= {
565 .set_block
= seclinux_rfkill_set
,
568 static int swsmi_wireless_status(struct samsung_laptop
*samsung
,
569 struct sabi_data
*data
)
571 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
573 return sabi_command(samsung
, commands
->get_wireless_status
,
577 static int swsmi_rfkill_set(void *priv
, bool blocked
)
579 struct samsung_rfkill
*srfkill
= priv
;
580 struct samsung_laptop
*samsung
= srfkill
->samsung
;
581 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
582 struct sabi_data data
;
585 ret
= swsmi_wireless_status(samsung
, &data
);
589 /* Don't set the state for non-present devices */
590 for (i
= 0; i
< 4; i
++)
591 if (data
.data
[i
] == 0x02)
594 if (srfkill
->type
== RFKILL_TYPE_WLAN
)
595 data
.data
[WL_STATUS_WLAN
] = !blocked
;
596 else if (srfkill
->type
== RFKILL_TYPE_BLUETOOTH
)
597 data
.data
[WL_STATUS_BT
] = !blocked
;
599 return sabi_command(samsung
, commands
->set_wireless_status
,
603 static void swsmi_rfkill_query(struct rfkill
*rfkill
, void *priv
)
605 struct samsung_rfkill
*srfkill
= priv
;
606 struct samsung_laptop
*samsung
= srfkill
->samsung
;
607 struct sabi_data data
;
610 ret
= swsmi_wireless_status(samsung
, &data
);
614 if (srfkill
->type
== RFKILL_TYPE_WLAN
)
615 ret
= data
.data
[WL_STATUS_WLAN
];
616 else if (srfkill
->type
== RFKILL_TYPE_BLUETOOTH
)
617 ret
= data
.data
[WL_STATUS_BT
];
621 rfkill_set_sw_state(rfkill
, !ret
);
624 static struct rfkill_ops swsmi_rfkill_ops
= {
625 .set_block
= swsmi_rfkill_set
,
626 .query
= swsmi_rfkill_query
,
629 static ssize_t
get_performance_level(struct device
*dev
,
630 struct device_attribute
*attr
, char *buf
)
632 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
633 const struct sabi_config
*config
= samsung
->config
;
634 const struct sabi_commands
*commands
= &config
->commands
;
635 struct sabi_data sretval
;
640 retval
= sabi_command(samsung
, commands
->get_performance_level
,
645 /* The logic is backwards, yeah, lots of fun... */
646 for (i
= 0; config
->performance_levels
[i
].name
; ++i
) {
647 if (sretval
.data
[0] == config
->performance_levels
[i
].value
)
648 return sprintf(buf
, "%s\n", config
->performance_levels
[i
].name
);
650 return sprintf(buf
, "%s\n", "unknown");
653 static ssize_t
set_performance_level(struct device
*dev
,
654 struct device_attribute
*attr
, const char *buf
,
657 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
658 const struct sabi_config
*config
= samsung
->config
;
659 const struct sabi_commands
*commands
= &config
->commands
;
665 for (i
= 0; config
->performance_levels
[i
].name
; ++i
) {
666 const struct sabi_performance_level
*level
=
667 &config
->performance_levels
[i
];
668 if (!strncasecmp(level
->name
, buf
, strlen(level
->name
))) {
669 sabi_set_commandb(samsung
,
670 commands
->set_performance_level
,
676 if (!config
->performance_levels
[i
].name
)
682 static DEVICE_ATTR(performance_level
, S_IWUSR
| S_IRUGO
,
683 get_performance_level
, set_performance_level
);
685 static int read_battery_life_extender(struct samsung_laptop
*samsung
)
687 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
688 struct sabi_data data
;
691 if (commands
->get_battery_life_extender
== 0xFFFF)
694 memset(&data
, 0, sizeof(data
));
696 retval
= sabi_command(samsung
, commands
->get_battery_life_extender
,
702 if (data
.data
[0] != 0 && data
.data
[0] != 1)
708 static int write_battery_life_extender(struct samsung_laptop
*samsung
,
711 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
712 struct sabi_data data
;
714 memset(&data
, 0, sizeof(data
));
715 data
.data
[0] = 0x80 | enabled
;
716 return sabi_command(samsung
, commands
->set_battery_life_extender
,
720 static ssize_t
get_battery_life_extender(struct device
*dev
,
721 struct device_attribute
*attr
,
724 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
727 ret
= read_battery_life_extender(samsung
);
731 return sprintf(buf
, "%d\n", ret
);
734 static ssize_t
set_battery_life_extender(struct device
*dev
,
735 struct device_attribute
*attr
,
736 const char *buf
, size_t count
)
738 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
741 if (!count
|| sscanf(buf
, "%i", &value
) != 1)
744 ret
= write_battery_life_extender(samsung
, !!value
);
751 static DEVICE_ATTR(battery_life_extender
, S_IWUSR
| S_IRUGO
,
752 get_battery_life_extender
, set_battery_life_extender
);
754 static int read_usb_charge(struct samsung_laptop
*samsung
)
756 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
757 struct sabi_data data
;
760 if (commands
->get_usb_charge
== 0xFFFF)
763 memset(&data
, 0, sizeof(data
));
765 retval
= sabi_command(samsung
, commands
->get_usb_charge
,
771 if (data
.data
[0] != 0 && data
.data
[0] != 1)
777 static int write_usb_charge(struct samsung_laptop
*samsung
,
780 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
781 struct sabi_data data
;
783 memset(&data
, 0, sizeof(data
));
784 data
.data
[0] = 0x80 | enabled
;
785 return sabi_command(samsung
, commands
->set_usb_charge
,
789 static ssize_t
get_usb_charge(struct device
*dev
,
790 struct device_attribute
*attr
,
793 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
796 ret
= read_usb_charge(samsung
);
800 return sprintf(buf
, "%d\n", ret
);
803 static ssize_t
set_usb_charge(struct device
*dev
,
804 struct device_attribute
*attr
,
805 const char *buf
, size_t count
)
807 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
810 if (!count
|| sscanf(buf
, "%i", &value
) != 1)
813 ret
= write_usb_charge(samsung
, !!value
);
820 static DEVICE_ATTR(usb_charge
, S_IWUSR
| S_IRUGO
,
821 get_usb_charge
, set_usb_charge
);
823 static struct attribute
*platform_attributes
[] = {
824 &dev_attr_performance_level
.attr
,
825 &dev_attr_battery_life_extender
.attr
,
826 &dev_attr_usb_charge
.attr
,
830 static int find_signature(void __iomem
*memcheck
, const char *testStr
)
835 for (loca
= 0; loca
< 0xffff; loca
++) {
836 char temp
= readb(memcheck
+ loca
);
838 if (temp
== testStr
[i
]) {
839 if (i
== strlen(testStr
)-1)
849 static void samsung_rfkill_exit(struct samsung_laptop
*samsung
)
851 if (samsung
->wlan
.rfkill
) {
852 rfkill_unregister(samsung
->wlan
.rfkill
);
853 rfkill_destroy(samsung
->wlan
.rfkill
);
854 samsung
->wlan
.rfkill
= NULL
;
856 if (samsung
->bluetooth
.rfkill
) {
857 rfkill_unregister(samsung
->bluetooth
.rfkill
);
858 rfkill_destroy(samsung
->bluetooth
.rfkill
);
859 samsung
->bluetooth
.rfkill
= NULL
;
863 static int samsung_new_rfkill(struct samsung_laptop
*samsung
,
864 struct samsung_rfkill
*arfkill
,
865 const char *name
, enum rfkill_type type
,
866 const struct rfkill_ops
*ops
,
869 struct rfkill
**rfkill
= &arfkill
->rfkill
;
872 arfkill
->type
= type
;
873 arfkill
->samsung
= samsung
;
875 *rfkill
= rfkill_alloc(name
, &samsung
->platform_device
->dev
,
882 rfkill_init_sw_state(*rfkill
, blocked
);
884 ret
= rfkill_register(*rfkill
);
886 rfkill_destroy(*rfkill
);
893 static int __init
samsung_rfkill_init_seclinux(struct samsung_laptop
*samsung
)
895 return samsung_new_rfkill(samsung
, &samsung
->wlan
, "samsung-wlan",
896 RFKILL_TYPE_WLAN
, &seclinux_rfkill_ops
, -1);
899 static int __init
samsung_rfkill_init_swsmi(struct samsung_laptop
*samsung
)
901 struct sabi_data data
;
904 ret
= swsmi_wireless_status(samsung
, &data
);
906 /* Some swsmi laptops use the old seclinux way to control
907 * wireless devices */
909 ret
= samsung_rfkill_init_seclinux(samsung
);
913 /* 0x02 seems to mean that the device is no present/available */
915 if (data
.data
[WL_STATUS_WLAN
] != 0x02)
916 ret
= samsung_new_rfkill(samsung
, &samsung
->wlan
,
920 !data
.data
[WL_STATUS_WLAN
]);
924 if (data
.data
[WL_STATUS_BT
] != 0x02)
925 ret
= samsung_new_rfkill(samsung
, &samsung
->bluetooth
,
927 RFKILL_TYPE_BLUETOOTH
,
929 !data
.data
[WL_STATUS_BT
]);
935 samsung_rfkill_exit(samsung
);
940 static int __init
samsung_rfkill_init(struct samsung_laptop
*samsung
)
942 if (samsung
->config
->sabi_version
== 2)
943 return samsung_rfkill_init_seclinux(samsung
);
944 if (samsung
->config
->sabi_version
== 3)
945 return samsung_rfkill_init_swsmi(samsung
);
949 static int kbd_backlight_enable(struct samsung_laptop
*samsung
)
951 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
952 struct sabi_data data
;
955 if (commands
->kbd_backlight
== 0xFFFF)
958 memset(&data
, 0, sizeof(data
));
960 retval
= sabi_command(samsung
, commands
->kbd_backlight
,
966 if (data
.d0
!= 0xccdd)
971 static int kbd_backlight_read(struct samsung_laptop
*samsung
)
973 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
974 struct sabi_data data
;
977 memset(&data
, 0, sizeof(data
));
979 retval
= sabi_command(samsung
, commands
->kbd_backlight
,
988 static int kbd_backlight_write(struct samsung_laptop
*samsung
, int brightness
)
990 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
991 struct sabi_data data
;
993 memset(&data
, 0, sizeof(data
));
994 data
.d0
= 0x82 | ((brightness
& 0xFF) << 8);
995 return sabi_command(samsung
, commands
->kbd_backlight
,
999 static void kbd_led_update(struct work_struct
*work
)
1001 struct samsung_laptop
*samsung
;
1003 samsung
= container_of(work
, struct samsung_laptop
, kbd_led_work
);
1004 kbd_backlight_write(samsung
, samsung
->kbd_led_wk
);
1007 static void kbd_led_set(struct led_classdev
*led_cdev
,
1008 enum led_brightness value
)
1010 struct samsung_laptop
*samsung
;
1012 samsung
= container_of(led_cdev
, struct samsung_laptop
, kbd_led
);
1014 if (value
> samsung
->kbd_led
.max_brightness
)
1015 value
= samsung
->kbd_led
.max_brightness
;
1019 samsung
->kbd_led_wk
= value
;
1020 queue_work(samsung
->led_workqueue
, &samsung
->kbd_led_work
);
1023 static enum led_brightness
kbd_led_get(struct led_classdev
*led_cdev
)
1025 struct samsung_laptop
*samsung
;
1027 samsung
= container_of(led_cdev
, struct samsung_laptop
, kbd_led
);
1028 return kbd_backlight_read(samsung
);
1031 static void samsung_leds_exit(struct samsung_laptop
*samsung
)
1033 if (!IS_ERR_OR_NULL(samsung
->kbd_led
.dev
))
1034 led_classdev_unregister(&samsung
->kbd_led
);
1035 if (samsung
->led_workqueue
)
1036 destroy_workqueue(samsung
->led_workqueue
);
1039 static int __init
samsung_leds_init(struct samsung_laptop
*samsung
)
1043 samsung
->led_workqueue
= create_singlethread_workqueue("led_workqueue");
1044 if (!samsung
->led_workqueue
)
1047 if (kbd_backlight_enable(samsung
) >= 0) {
1048 INIT_WORK(&samsung
->kbd_led_work
, kbd_led_update
);
1050 samsung
->kbd_led
.name
= "samsung::kbd_backlight";
1051 samsung
->kbd_led
.brightness_set
= kbd_led_set
;
1052 samsung
->kbd_led
.brightness_get
= kbd_led_get
;
1053 samsung
->kbd_led
.max_brightness
= 8;
1055 ret
= led_classdev_register(&samsung
->platform_device
->dev
,
1060 samsung_leds_exit(samsung
);
1065 static void samsung_backlight_exit(struct samsung_laptop
*samsung
)
1067 if (samsung
->backlight_device
) {
1068 backlight_device_unregister(samsung
->backlight_device
);
1069 samsung
->backlight_device
= NULL
;
1073 static int __init
samsung_backlight_init(struct samsung_laptop
*samsung
)
1075 struct backlight_device
*bd
;
1076 struct backlight_properties props
;
1078 if (!samsung
->handle_backlight
)
1081 memset(&props
, 0, sizeof(struct backlight_properties
));
1082 props
.type
= BACKLIGHT_PLATFORM
;
1083 props
.max_brightness
= samsung
->config
->max_brightness
-
1084 samsung
->config
->min_brightness
;
1086 bd
= backlight_device_register("samsung",
1087 &samsung
->platform_device
->dev
,
1088 samsung
, &backlight_ops
,
1093 samsung
->backlight_device
= bd
;
1094 samsung
->backlight_device
->props
.brightness
= read_brightness(samsung
);
1095 samsung
->backlight_device
->props
.power
= FB_BLANK_UNBLANK
;
1096 backlight_update_status(samsung
->backlight_device
);
1101 static umode_t
samsung_sysfs_is_visible(struct kobject
*kobj
,
1102 struct attribute
*attr
, int idx
)
1104 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1105 struct platform_device
*pdev
= to_platform_device(dev
);
1106 struct samsung_laptop
*samsung
= platform_get_drvdata(pdev
);
1109 if (attr
== &dev_attr_performance_level
.attr
)
1110 ok
= !!samsung
->config
->performance_levels
[0].name
;
1111 if (attr
== &dev_attr_battery_life_extender
.attr
)
1112 ok
= !!(read_battery_life_extender(samsung
) >= 0);
1113 if (attr
== &dev_attr_usb_charge
.attr
)
1114 ok
= !!(read_usb_charge(samsung
) >= 0);
1116 return ok
? attr
->mode
: 0;
1119 static struct attribute_group platform_attribute_group
= {
1120 .is_visible
= samsung_sysfs_is_visible
,
1121 .attrs
= platform_attributes
1124 static void samsung_sysfs_exit(struct samsung_laptop
*samsung
)
1126 struct platform_device
*device
= samsung
->platform_device
;
1128 sysfs_remove_group(&device
->dev
.kobj
, &platform_attribute_group
);
1131 static int __init
samsung_sysfs_init(struct samsung_laptop
*samsung
)
1133 struct platform_device
*device
= samsung
->platform_device
;
1135 return sysfs_create_group(&device
->dev
.kobj
, &platform_attribute_group
);
1139 static int show_call(struct seq_file
*m
, void *data
)
1141 struct samsung_laptop
*samsung
= m
->private;
1142 struct sabi_data
*sdata
= &samsung
->debug
.data
;
1145 seq_printf(m
, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1146 samsung
->debug
.command
,
1147 sdata
->d0
, sdata
->d1
, sdata
->d2
, sdata
->d3
);
1149 ret
= sabi_command(samsung
, samsung
->debug
.command
, sdata
, sdata
);
1152 seq_printf(m
, "SABI command 0x%04x failed\n",
1153 samsung
->debug
.command
);
1157 seq_printf(m
, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1158 sdata
->d0
, sdata
->d1
, sdata
->d2
, sdata
->d3
);
1162 static int samsung_debugfs_open(struct inode
*inode
, struct file
*file
)
1164 return single_open(file
, show_call
, inode
->i_private
);
1167 static const struct file_operations samsung_laptop_call_io_ops
= {
1168 .owner
= THIS_MODULE
,
1169 .open
= samsung_debugfs_open
,
1171 .llseek
= seq_lseek
,
1172 .release
= single_release
,
1175 static void samsung_debugfs_exit(struct samsung_laptop
*samsung
)
1177 debugfs_remove_recursive(samsung
->debug
.root
);
1180 static int samsung_debugfs_init(struct samsung_laptop
*samsung
)
1182 struct dentry
*dent
;
1184 samsung
->debug
.root
= debugfs_create_dir("samsung-laptop", NULL
);
1185 if (!samsung
->debug
.root
) {
1186 pr_err("failed to create debugfs directory");
1190 samsung
->debug
.f0000_wrapper
.data
= samsung
->f0000_segment
;
1191 samsung
->debug
.f0000_wrapper
.size
= 0xffff;
1193 samsung
->debug
.data_wrapper
.data
= &samsung
->debug
.data
;
1194 samsung
->debug
.data_wrapper
.size
= sizeof(samsung
->debug
.data
);
1196 samsung
->debug
.sdiag_wrapper
.data
= samsung
->sdiag
;
1197 samsung
->debug
.sdiag_wrapper
.size
= strlen(samsung
->sdiag
);
1199 dent
= debugfs_create_u16("command", S_IRUGO
| S_IWUSR
,
1200 samsung
->debug
.root
, &samsung
->debug
.command
);
1204 dent
= debugfs_create_u32("d0", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1205 &samsung
->debug
.data
.d0
);
1209 dent
= debugfs_create_u32("d1", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1210 &samsung
->debug
.data
.d1
);
1214 dent
= debugfs_create_u16("d2", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1215 &samsung
->debug
.data
.d2
);
1219 dent
= debugfs_create_u8("d3", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1220 &samsung
->debug
.data
.d3
);
1224 dent
= debugfs_create_blob("data", S_IRUGO
| S_IWUSR
,
1225 samsung
->debug
.root
,
1226 &samsung
->debug
.data_wrapper
);
1230 dent
= debugfs_create_blob("f0000_segment", S_IRUSR
| S_IWUSR
,
1231 samsung
->debug
.root
,
1232 &samsung
->debug
.f0000_wrapper
);
1236 dent
= debugfs_create_file("call", S_IFREG
| S_IRUGO
,
1237 samsung
->debug
.root
, samsung
,
1238 &samsung_laptop_call_io_ops
);
1242 dent
= debugfs_create_blob("sdiag", S_IRUGO
| S_IWUSR
,
1243 samsung
->debug
.root
,
1244 &samsung
->debug
.sdiag_wrapper
);
1251 samsung_debugfs_exit(samsung
);
1255 static void samsung_sabi_exit(struct samsung_laptop
*samsung
)
1257 const struct sabi_config
*config
= samsung
->config
;
1259 /* Turn off "Linux" mode in the BIOS */
1260 if (config
&& config
->commands
.set_linux
!= 0xff)
1261 sabi_set_commandb(samsung
, config
->commands
.set_linux
, 0x80);
1263 if (samsung
->sabi_iface
) {
1264 iounmap(samsung
->sabi_iface
);
1265 samsung
->sabi_iface
= NULL
;
1267 if (samsung
->f0000_segment
) {
1268 iounmap(samsung
->f0000_segment
);
1269 samsung
->f0000_segment
= NULL
;
1272 samsung
->config
= NULL
;
1275 static __init
void samsung_sabi_infos(struct samsung_laptop
*samsung
, int loca
,
1276 unsigned int ifaceP
)
1278 const struct sabi_config
*config
= samsung
->config
;
1280 printk(KERN_DEBUG
"This computer supports SABI==%x\n",
1281 loca
+ 0xf0000 - 6);
1283 printk(KERN_DEBUG
"SABI header:\n");
1284 printk(KERN_DEBUG
" SMI Port Number = 0x%04x\n",
1285 readw(samsung
->sabi
+ config
->header_offsets
.port
));
1286 printk(KERN_DEBUG
" SMI Interface Function = 0x%02x\n",
1287 readb(samsung
->sabi
+ config
->header_offsets
.iface_func
));
1288 printk(KERN_DEBUG
" SMI enable memory buffer = 0x%02x\n",
1289 readb(samsung
->sabi
+ config
->header_offsets
.en_mem
));
1290 printk(KERN_DEBUG
" SMI restore memory buffer = 0x%02x\n",
1291 readb(samsung
->sabi
+ config
->header_offsets
.re_mem
));
1292 printk(KERN_DEBUG
" SABI data offset = 0x%04x\n",
1293 readw(samsung
->sabi
+ config
->header_offsets
.data_offset
));
1294 printk(KERN_DEBUG
" SABI data segment = 0x%04x\n",
1295 readw(samsung
->sabi
+ config
->header_offsets
.data_segment
));
1297 printk(KERN_DEBUG
" SABI pointer = 0x%08x\n", ifaceP
);
1300 static void __init
samsung_sabi_diag(struct samsung_laptop
*samsung
)
1302 int loca
= find_signature(samsung
->f0000_segment
, "SDiaG@");
1309 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1311 * Product name: 90X3A
1312 * BIOS Version: 07HL
1315 for (i
= 0; loca
< 0xffff && i
< sizeof(samsung
->sdiag
) - 1; loca
++) {
1316 char temp
= readb(samsung
->f0000_segment
+ loca
);
1318 if (isalnum(temp
) || temp
== '/' || temp
== '-')
1319 samsung
->sdiag
[i
++] = temp
;
1324 if (debug
&& samsung
->sdiag
[0])
1325 pr_info("sdiag: %s", samsung
->sdiag
);
1328 static int __init
samsung_sabi_init(struct samsung_laptop
*samsung
)
1330 const struct sabi_config
*config
= NULL
;
1331 const struct sabi_commands
*commands
;
1332 unsigned int ifaceP
;
1337 samsung
->f0000_segment
= ioremap_nocache(0xf0000, 0xffff);
1338 if (!samsung
->f0000_segment
) {
1340 pr_err("Can't map the segment at 0xf0000\n");
1345 samsung_sabi_diag(samsung
);
1347 /* Try to find one of the signatures in memory to find the header */
1348 for (i
= 0; sabi_configs
[i
].test_string
!= 0; ++i
) {
1349 samsung
->config
= &sabi_configs
[i
];
1350 loca
= find_signature(samsung
->f0000_segment
,
1351 samsung
->config
->test_string
);
1356 if (loca
== 0xffff) {
1358 pr_err("This computer does not support SABI\n");
1363 config
= samsung
->config
;
1364 commands
= &config
->commands
;
1366 /* point to the SMI port Number */
1368 samsung
->sabi
= (samsung
->f0000_segment
+ loca
);
1370 /* Get a pointer to the SABI Interface */
1371 ifaceP
= (readw(samsung
->sabi
+ config
->header_offsets
.data_segment
) & 0x0ffff) << 4;
1372 ifaceP
+= readw(samsung
->sabi
+ config
->header_offsets
.data_offset
) & 0x0ffff;
1375 samsung_sabi_infos(samsung
, loca
, ifaceP
);
1377 samsung
->sabi_iface
= ioremap_nocache(ifaceP
, 16);
1378 if (!samsung
->sabi_iface
) {
1379 pr_err("Can't remap %x\n", ifaceP
);
1384 /* Turn on "Linux" mode in the BIOS */
1385 if (commands
->set_linux
!= 0xff) {
1386 int retval
= sabi_set_commandb(samsung
,
1387 commands
->set_linux
, 0x81);
1389 pr_warn("Linux mode was not set!\n");
1395 /* Check for stepping quirk */
1396 if (samsung
->handle_backlight
)
1397 check_for_stepping_quirk(samsung
);
1399 pr_info("detected SABI interface: %s\n",
1400 samsung
->config
->test_string
);
1404 samsung_sabi_exit(samsung
);
1409 static void samsung_platform_exit(struct samsung_laptop
*samsung
)
1411 if (samsung
->platform_device
) {
1412 platform_device_unregister(samsung
->platform_device
);
1413 samsung
->platform_device
= NULL
;
1417 static int __init
samsung_platform_init(struct samsung_laptop
*samsung
)
1419 struct platform_device
*pdev
;
1421 pdev
= platform_device_register_simple("samsung", -1, NULL
, 0);
1423 return PTR_ERR(pdev
);
1425 samsung
->platform_device
= pdev
;
1426 platform_set_drvdata(samsung
->platform_device
, samsung
);
1430 static struct samsung_quirks
*quirks
;
1432 static int __init
samsung_dmi_matched(const struct dmi_system_id
*d
)
1434 quirks
= d
->driver_data
;
1438 static struct dmi_system_id __initdata samsung_dmi_table
[] = {
1441 DMI_MATCH(DMI_SYS_VENDOR
,
1442 "SAMSUNG ELECTRONICS CO., LTD."),
1443 DMI_MATCH(DMI_CHASSIS_TYPE
, "8"), /* Portable */
1448 DMI_MATCH(DMI_SYS_VENDOR
,
1449 "SAMSUNG ELECTRONICS CO., LTD."),
1450 DMI_MATCH(DMI_CHASSIS_TYPE
, "9"), /* Laptop */
1455 DMI_MATCH(DMI_SYS_VENDOR
,
1456 "SAMSUNG ELECTRONICS CO., LTD."),
1457 DMI_MATCH(DMI_CHASSIS_TYPE
, "10"), /* Notebook */
1462 DMI_MATCH(DMI_SYS_VENDOR
,
1463 "SAMSUNG ELECTRONICS CO., LTD."),
1464 DMI_MATCH(DMI_CHASSIS_TYPE
, "14"), /* Sub-Notebook */
1467 /* DMI ids for laptops with bad Chassis Type */
1471 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1472 DMI_MATCH(DMI_PRODUCT_NAME
, "R40/R41"),
1473 DMI_MATCH(DMI_BOARD_NAME
, "R40/R41"),
1476 /* Specific DMI ids for laptop with quirks */
1478 .callback
= samsung_dmi_matched
,
1481 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1482 DMI_MATCH(DMI_PRODUCT_NAME
, "N150P"),
1483 DMI_MATCH(DMI_BOARD_NAME
, "N150P"),
1485 .driver_data
= &samsung_broken_acpi_video
,
1488 .callback
= samsung_dmi_matched
,
1489 .ident
= "N145P/N250P/N260P",
1491 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1492 DMI_MATCH(DMI_PRODUCT_NAME
, "N145P/N250P/N260P"),
1493 DMI_MATCH(DMI_BOARD_NAME
, "N145P/N250P/N260P"),
1495 .driver_data
= &samsung_broken_acpi_video
,
1498 .callback
= samsung_dmi_matched
,
1499 .ident
= "N150/N210/N220",
1501 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1502 DMI_MATCH(DMI_PRODUCT_NAME
, "N150/N210/N220"),
1503 DMI_MATCH(DMI_BOARD_NAME
, "N150/N210/N220"),
1505 .driver_data
= &samsung_broken_acpi_video
,
1508 .callback
= samsung_dmi_matched
,
1509 .ident
= "NF110/NF210/NF310",
1511 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1512 DMI_MATCH(DMI_PRODUCT_NAME
, "NF110/NF210/NF310"),
1513 DMI_MATCH(DMI_BOARD_NAME
, "NF110/NF210/NF310"),
1515 .driver_data
= &samsung_broken_acpi_video
,
1518 .callback
= samsung_dmi_matched
,
1521 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1522 DMI_MATCH(DMI_PRODUCT_NAME
, "X360"),
1523 DMI_MATCH(DMI_BOARD_NAME
, "X360"),
1525 .driver_data
= &samsung_broken_acpi_video
,
1528 .callback
= samsung_dmi_matched
,
1531 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1532 DMI_MATCH(DMI_PRODUCT_NAME
, "N250P"),
1533 DMI_MATCH(DMI_BOARD_NAME
, "N250P"),
1535 .driver_data
= &samsung_broken_acpi_video
,
1539 MODULE_DEVICE_TABLE(dmi
, samsung_dmi_table
);
1541 static struct platform_device
*samsung_platform_device
;
1543 static int __init
samsung_init(void)
1545 struct samsung_laptop
*samsung
;
1548 if (efi_enabled(EFI_BOOT
))
1551 quirks
= &samsung_unknown
;
1552 if (!force
&& !dmi_check_system(samsung_dmi_table
))
1555 samsung
= kzalloc(sizeof(*samsung
), GFP_KERNEL
);
1559 mutex_init(&samsung
->sabi_mutex
);
1560 samsung
->handle_backlight
= true;
1561 samsung
->quirks
= quirks
;
1565 if (samsung
->quirks
->broken_acpi_video
)
1566 acpi_video_dmi_promote_vendor();
1568 /* Don't handle backlight here if the acpi video already handle it */
1569 if (acpi_video_backlight_support()) {
1570 samsung
->handle_backlight
= false;
1571 } else if (samsung
->quirks
->broken_acpi_video
) {
1572 pr_info("Disabling ACPI video driver\n");
1573 acpi_video_unregister();
1577 ret
= samsung_platform_init(samsung
);
1579 goto error_platform
;
1581 ret
= samsung_sabi_init(samsung
);
1586 /* Only log that if we are really on a sabi platform */
1587 if (acpi_video_backlight_support())
1588 pr_info("Backlight controlled by ACPI video driver\n");
1591 ret
= samsung_sysfs_init(samsung
);
1595 ret
= samsung_backlight_init(samsung
);
1597 goto error_backlight
;
1599 ret
= samsung_rfkill_init(samsung
);
1603 ret
= samsung_leds_init(samsung
);
1607 ret
= samsung_debugfs_init(samsung
);
1611 samsung_platform_device
= samsung
->platform_device
;
1615 samsung_leds_exit(samsung
);
1617 samsung_rfkill_exit(samsung
);
1619 samsung_backlight_exit(samsung
);
1621 samsung_sysfs_exit(samsung
);
1623 samsung_sabi_exit(samsung
);
1625 samsung_platform_exit(samsung
);
1631 static void __exit
samsung_exit(void)
1633 struct samsung_laptop
*samsung
;
1635 samsung
= platform_get_drvdata(samsung_platform_device
);
1637 samsung_debugfs_exit(samsung
);
1638 samsung_leds_exit(samsung
);
1639 samsung_rfkill_exit(samsung
);
1640 samsung_backlight_exit(samsung
);
1641 samsung_sysfs_exit(samsung
);
1642 samsung_sabi_exit(samsung
);
1643 samsung_platform_exit(samsung
);
1646 samsung_platform_device
= NULL
;
1649 module_init(samsung_init
);
1650 module_exit(samsung_exit
);
1652 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1653 MODULE_DESCRIPTION("Samsung Backlight driver");
1654 MODULE_LICENSE("GPL");