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 <acpi/video.h>
32 * This driver is needed because a number of Samsung laptops do not hook
33 * their control settings through ACPI. So we have to poke around in the
34 * BIOS to do things like brightness values, and "special" key controls.
38 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
39 * be reserved by the BIOS (which really doesn't make much sense), we tell
40 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
42 #define MAX_BRIGHT 0x07
45 #define SABI_IFACE_MAIN 0x00
46 #define SABI_IFACE_SUB 0x02
47 #define SABI_IFACE_COMPLETE 0x04
48 #define SABI_IFACE_DATA 0x05
50 #define WL_STATUS_WLAN 0x0
51 #define WL_STATUS_BT 0x2
53 /* Structure get/set data using sabi */
66 struct sabi_header_offsets
{
75 struct sabi_commands
{
77 * Brightness is 0 - 8, as described above.
78 * Value 0 is for the BIOS to use
85 * 0x00 - wireless is off
86 * 0x01 - wireless is on
90 * TODO, verify 3G is correct, that doesn't seem right...
92 u16 get_wireless_button
;
93 u16 set_wireless_button
;
95 /* 0 is off, 1 is on */
100 * 0x80 or 0x00 - no action
101 * 0x81 - recovery key pressed
103 u16 get_recovery_mode
;
104 u16 set_recovery_mode
;
107 * on seclinux: 0 is low, 1 is high,
108 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
110 u16 get_performance_level
;
111 u16 set_performance_level
;
113 /* 0x80 is off, 0x81 is on */
114 u16 get_battery_life_extender
;
115 u16 set_battery_life_extender
;
117 /* 0x80 is off, 0x81 is on */
121 /* the first byte is for bluetooth and the third one is for wlan */
122 u16 get_wireless_status
;
123 u16 set_wireless_status
;
125 /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
129 * Tell the BIOS that Linux is running on this machine.
130 * 81 is on, 80 is off
135 struct sabi_performance_level
{
142 const char *test_string
;
144 const struct sabi_header_offsets header_offsets
;
145 const struct sabi_commands commands
;
146 const struct sabi_performance_level performance_levels
[4];
151 static const struct sabi_config sabi_configs
[] = {
153 /* I don't know if it is really 2, but it it is
154 * less than 3 anyway */
157 .test_string
= "SECLINUX",
159 .main_function
= 0x4c49,
167 .data_segment
= 0x07,
171 .get_brightness
= 0x00,
172 .set_brightness
= 0x01,
174 .get_wireless_button
= 0x02,
175 .set_wireless_button
= 0x03,
177 .get_backlight
= 0x04,
178 .set_backlight
= 0x05,
180 .get_recovery_mode
= 0x06,
181 .set_recovery_mode
= 0x07,
183 .get_performance_level
= 0x08,
184 .set_performance_level
= 0x09,
186 .get_battery_life_extender
= 0xFFFF,
187 .set_battery_life_extender
= 0xFFFF,
189 .get_usb_charge
= 0xFFFF,
190 .set_usb_charge
= 0xFFFF,
192 .get_wireless_status
= 0xFFFF,
193 .set_wireless_status
= 0xFFFF,
195 .kbd_backlight
= 0xFFFF,
200 .performance_levels
= {
217 .test_string
= "SwSmi@",
219 .main_function
= 0x5843,
227 .data_segment
= 0x07,
231 .get_brightness
= 0x10,
232 .set_brightness
= 0x11,
234 .get_wireless_button
= 0x12,
235 .set_wireless_button
= 0x13,
237 .get_backlight
= 0x2d,
238 .set_backlight
= 0x2e,
240 .get_recovery_mode
= 0xff,
241 .set_recovery_mode
= 0xff,
243 .get_performance_level
= 0x31,
244 .set_performance_level
= 0x32,
246 .get_battery_life_extender
= 0x65,
247 .set_battery_life_extender
= 0x66,
249 .get_usb_charge
= 0x67,
250 .set_usb_charge
= 0x68,
252 .get_wireless_status
= 0x69,
253 .set_wireless_status
= 0x6a,
255 .kbd_backlight
= 0x78,
260 .performance_levels
= {
282 * samsung-laptop/ - debugfs root directory
283 * f0000_segment - dump f0000 segment
284 * command - current command
285 * data - current data
286 * d0, d1, d2, d3 - data fields
287 * call - call SABI using command and data
289 * This allow to call arbitrary sabi commands wihout
290 * modifying the driver at all.
291 * For example, setting the keyboard backlight brightness to 5
293 * echo 0x78 > command
301 struct samsung_laptop_debug
{
303 struct sabi_data data
;
306 struct debugfs_blob_wrapper f0000_wrapper
;
307 struct debugfs_blob_wrapper data_wrapper
;
308 struct debugfs_blob_wrapper sdiag_wrapper
;
311 struct samsung_laptop
;
313 struct samsung_rfkill
{
314 struct samsung_laptop
*samsung
;
315 struct rfkill
*rfkill
;
316 enum rfkill_type type
;
319 struct samsung_laptop
{
320 const struct sabi_config
*config
;
323 void __iomem
*sabi_iface
;
324 void __iomem
*f0000_segment
;
326 struct mutex sabi_mutex
;
328 struct platform_device
*platform_device
;
329 struct backlight_device
*backlight_device
;
331 struct samsung_rfkill wlan
;
332 struct samsung_rfkill bluetooth
;
334 struct led_classdev kbd_led
;
336 struct workqueue_struct
*led_workqueue
;
337 struct work_struct kbd_led_work
;
339 struct samsung_laptop_debug debug
;
340 struct samsung_quirks
*quirks
;
342 bool handle_backlight
;
343 bool has_stepping_quirk
;
348 struct samsung_quirks
{
349 bool broken_acpi_video
;
352 static struct samsung_quirks samsung_unknown
= {};
354 static struct samsung_quirks samsung_broken_acpi_video
= {
355 .broken_acpi_video
= true,
359 module_param(force
, bool, 0);
360 MODULE_PARM_DESC(force
,
361 "Disable the DMI check and forces the driver to be loaded");
364 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
365 MODULE_PARM_DESC(debug
, "Debug enabled or not");
367 static int sabi_command(struct samsung_laptop
*samsung
, u16 command
,
368 struct sabi_data
*in
,
369 struct sabi_data
*out
)
371 const struct sabi_config
*config
= samsung
->config
;
373 u16 port
= readw(samsung
->sabi
+ config
->header_offsets
.port
);
374 u8 complete
, iface_data
;
376 mutex_lock(&samsung
->sabi_mutex
);
380 pr_info("SABI command:0x%04x "
381 "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
382 command
, in
->d0
, in
->d1
, in
->d2
, in
->d3
);
384 pr_info("SABI command:0x%04x", command
);
387 /* enable memory to be able to write to it */
388 outb(readb(samsung
->sabi
+ config
->header_offsets
.en_mem
), port
);
390 /* write out the command */
391 writew(config
->main_function
, samsung
->sabi_iface
+ SABI_IFACE_MAIN
);
392 writew(command
, samsung
->sabi_iface
+ SABI_IFACE_SUB
);
393 writeb(0, samsung
->sabi_iface
+ SABI_IFACE_COMPLETE
);
395 writel(in
->d0
, samsung
->sabi_iface
+ SABI_IFACE_DATA
);
396 writel(in
->d1
, samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 4);
397 writew(in
->d2
, samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 8);
398 writeb(in
->d3
, samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 10);
400 outb(readb(samsung
->sabi
+ config
->header_offsets
.iface_func
), port
);
402 /* write protect memory to make it safe */
403 outb(readb(samsung
->sabi
+ config
->header_offsets
.re_mem
), port
);
405 /* see if the command actually succeeded */
406 complete
= readb(samsung
->sabi_iface
+ SABI_IFACE_COMPLETE
);
407 iface_data
= readb(samsung
->sabi_iface
+ SABI_IFACE_DATA
);
409 /* iface_data = 0xFF happens when a command is not known
410 * so we only add a warning in debug mode since we will
411 * probably issue some unknown command at startup to find
412 * out which features are supported */
413 if (complete
!= 0xaa || (iface_data
== 0xff && debug
))
414 pr_warn("SABI command 0x%04x failed with"
415 " completion flag 0x%02x and interface data 0x%02x",
416 command
, complete
, iface_data
);
418 if (complete
!= 0xaa || iface_data
== 0xff) {
424 out
->d0
= readl(samsung
->sabi_iface
+ SABI_IFACE_DATA
);
425 out
->d1
= readl(samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 4);
426 out
->d2
= readw(samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 2);
427 out
->d3
= readb(samsung
->sabi_iface
+ SABI_IFACE_DATA
+ 1);
431 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
432 out
->d0
, out
->d1
, out
->d2
, out
->d3
);
436 mutex_unlock(&samsung
->sabi_mutex
);
440 /* simple wrappers usable with most commands */
441 static int sabi_set_commandb(struct samsung_laptop
*samsung
,
442 u16 command
, u8 data
)
444 struct sabi_data in
= { { { .d0
= 0, .d1
= 0, .d2
= 0, .d3
= 0 } } };
447 return sabi_command(samsung
, command
, &in
, NULL
);
450 static int read_brightness(struct samsung_laptop
*samsung
)
452 const struct sabi_config
*config
= samsung
->config
;
453 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
454 struct sabi_data sretval
;
455 int user_brightness
= 0;
458 retval
= sabi_command(samsung
, commands
->get_brightness
,
463 user_brightness
= sretval
.data
[0];
464 if (user_brightness
> config
->min_brightness
)
465 user_brightness
-= config
->min_brightness
;
469 return user_brightness
;
472 static void set_brightness(struct samsung_laptop
*samsung
, u8 user_brightness
)
474 const struct sabi_config
*config
= samsung
->config
;
475 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
476 u8 user_level
= user_brightness
+ config
->min_brightness
;
478 if (samsung
->has_stepping_quirk
&& user_level
!= 0) {
480 * short circuit if the specified level is what's already set
481 * to prevent the screen from flickering needlessly
483 if (user_brightness
== read_brightness(samsung
))
486 sabi_set_commandb(samsung
, commands
->set_brightness
, 0);
489 sabi_set_commandb(samsung
, commands
->set_brightness
, user_level
);
492 static int get_brightness(struct backlight_device
*bd
)
494 struct samsung_laptop
*samsung
= bl_get_data(bd
);
496 return read_brightness(samsung
);
499 static void check_for_stepping_quirk(struct samsung_laptop
*samsung
)
503 int orig_level
= read_brightness(samsung
);
506 * Some laptops exhibit the strange behaviour of stepping toward
507 * (rather than setting) the brightness except when changing to/from
508 * brightness level 0. This behaviour is checked for here and worked
509 * around in set_brightness.
513 set_brightness(samsung
, 1);
515 initial_level
= read_brightness(samsung
);
517 if (initial_level
<= 2)
518 check_level
= initial_level
+ 2;
520 check_level
= initial_level
- 2;
522 samsung
->has_stepping_quirk
= false;
523 set_brightness(samsung
, check_level
);
525 if (read_brightness(samsung
) != check_level
) {
526 samsung
->has_stepping_quirk
= true;
527 pr_info("enabled workaround for brightness stepping quirk\n");
530 set_brightness(samsung
, orig_level
);
533 static int update_status(struct backlight_device
*bd
)
535 struct samsung_laptop
*samsung
= bl_get_data(bd
);
536 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
538 set_brightness(samsung
, bd
->props
.brightness
);
540 if (bd
->props
.power
== FB_BLANK_UNBLANK
)
541 sabi_set_commandb(samsung
, commands
->set_backlight
, 1);
543 sabi_set_commandb(samsung
, commands
->set_backlight
, 0);
548 static const struct backlight_ops backlight_ops
= {
549 .get_brightness
= get_brightness
,
550 .update_status
= update_status
,
553 static int seclinux_rfkill_set(void *data
, bool blocked
)
555 struct samsung_rfkill
*srfkill
= data
;
556 struct samsung_laptop
*samsung
= srfkill
->samsung
;
557 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
559 return sabi_set_commandb(samsung
, commands
->set_wireless_button
,
563 static struct rfkill_ops seclinux_rfkill_ops
= {
564 .set_block
= seclinux_rfkill_set
,
567 static int swsmi_wireless_status(struct samsung_laptop
*samsung
,
568 struct sabi_data
*data
)
570 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
572 return sabi_command(samsung
, commands
->get_wireless_status
,
576 static int swsmi_rfkill_set(void *priv
, bool blocked
)
578 struct samsung_rfkill
*srfkill
= priv
;
579 struct samsung_laptop
*samsung
= srfkill
->samsung
;
580 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
581 struct sabi_data data
;
584 ret
= swsmi_wireless_status(samsung
, &data
);
588 /* Don't set the state for non-present devices */
589 for (i
= 0; i
< 4; i
++)
590 if (data
.data
[i
] == 0x02)
593 if (srfkill
->type
== RFKILL_TYPE_WLAN
)
594 data
.data
[WL_STATUS_WLAN
] = !blocked
;
595 else if (srfkill
->type
== RFKILL_TYPE_BLUETOOTH
)
596 data
.data
[WL_STATUS_BT
] = !blocked
;
598 return sabi_command(samsung
, commands
->set_wireless_status
,
602 static void swsmi_rfkill_query(struct rfkill
*rfkill
, void *priv
)
604 struct samsung_rfkill
*srfkill
= priv
;
605 struct samsung_laptop
*samsung
= srfkill
->samsung
;
606 struct sabi_data data
;
609 ret
= swsmi_wireless_status(samsung
, &data
);
613 if (srfkill
->type
== RFKILL_TYPE_WLAN
)
614 ret
= data
.data
[WL_STATUS_WLAN
];
615 else if (srfkill
->type
== RFKILL_TYPE_BLUETOOTH
)
616 ret
= data
.data
[WL_STATUS_BT
];
620 rfkill_set_sw_state(rfkill
, !ret
);
623 static struct rfkill_ops swsmi_rfkill_ops
= {
624 .set_block
= swsmi_rfkill_set
,
625 .query
= swsmi_rfkill_query
,
628 static ssize_t
get_performance_level(struct device
*dev
,
629 struct device_attribute
*attr
, char *buf
)
631 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
632 const struct sabi_config
*config
= samsung
->config
;
633 const struct sabi_commands
*commands
= &config
->commands
;
634 struct sabi_data sretval
;
639 retval
= sabi_command(samsung
, commands
->get_performance_level
,
644 /* The logic is backwards, yeah, lots of fun... */
645 for (i
= 0; config
->performance_levels
[i
].name
; ++i
) {
646 if (sretval
.data
[0] == config
->performance_levels
[i
].value
)
647 return sprintf(buf
, "%s\n", config
->performance_levels
[i
].name
);
649 return sprintf(buf
, "%s\n", "unknown");
652 static ssize_t
set_performance_level(struct device
*dev
,
653 struct device_attribute
*attr
, const char *buf
,
656 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
657 const struct sabi_config
*config
= samsung
->config
;
658 const struct sabi_commands
*commands
= &config
->commands
;
664 for (i
= 0; config
->performance_levels
[i
].name
; ++i
) {
665 const struct sabi_performance_level
*level
=
666 &config
->performance_levels
[i
];
667 if (!strncasecmp(level
->name
, buf
, strlen(level
->name
))) {
668 sabi_set_commandb(samsung
,
669 commands
->set_performance_level
,
675 if (!config
->performance_levels
[i
].name
)
681 static DEVICE_ATTR(performance_level
, S_IWUSR
| S_IRUGO
,
682 get_performance_level
, set_performance_level
);
684 static int read_battery_life_extender(struct samsung_laptop
*samsung
)
686 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
687 struct sabi_data data
;
690 if (commands
->get_battery_life_extender
== 0xFFFF)
693 memset(&data
, 0, sizeof(data
));
695 retval
= sabi_command(samsung
, commands
->get_battery_life_extender
,
701 if (data
.data
[0] != 0 && data
.data
[0] != 1)
707 static int write_battery_life_extender(struct samsung_laptop
*samsung
,
710 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
711 struct sabi_data data
;
713 memset(&data
, 0, sizeof(data
));
714 data
.data
[0] = 0x80 | enabled
;
715 return sabi_command(samsung
, commands
->set_battery_life_extender
,
719 static ssize_t
get_battery_life_extender(struct device
*dev
,
720 struct device_attribute
*attr
,
723 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
726 ret
= read_battery_life_extender(samsung
);
730 return sprintf(buf
, "%d\n", ret
);
733 static ssize_t
set_battery_life_extender(struct device
*dev
,
734 struct device_attribute
*attr
,
735 const char *buf
, size_t count
)
737 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
740 if (!count
|| sscanf(buf
, "%i", &value
) != 1)
743 ret
= write_battery_life_extender(samsung
, !!value
);
750 static DEVICE_ATTR(battery_life_extender
, S_IWUSR
| S_IRUGO
,
751 get_battery_life_extender
, set_battery_life_extender
);
753 static int read_usb_charge(struct samsung_laptop
*samsung
)
755 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
756 struct sabi_data data
;
759 if (commands
->get_usb_charge
== 0xFFFF)
762 memset(&data
, 0, sizeof(data
));
764 retval
= sabi_command(samsung
, commands
->get_usb_charge
,
770 if (data
.data
[0] != 0 && data
.data
[0] != 1)
776 static int write_usb_charge(struct samsung_laptop
*samsung
,
779 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
780 struct sabi_data data
;
782 memset(&data
, 0, sizeof(data
));
783 data
.data
[0] = 0x80 | enabled
;
784 return sabi_command(samsung
, commands
->set_usb_charge
,
788 static ssize_t
get_usb_charge(struct device
*dev
,
789 struct device_attribute
*attr
,
792 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
795 ret
= read_usb_charge(samsung
);
799 return sprintf(buf
, "%d\n", ret
);
802 static ssize_t
set_usb_charge(struct device
*dev
,
803 struct device_attribute
*attr
,
804 const char *buf
, size_t count
)
806 struct samsung_laptop
*samsung
= dev_get_drvdata(dev
);
809 if (!count
|| sscanf(buf
, "%i", &value
) != 1)
812 ret
= write_usb_charge(samsung
, !!value
);
819 static DEVICE_ATTR(usb_charge
, S_IWUSR
| S_IRUGO
,
820 get_usb_charge
, set_usb_charge
);
822 static struct attribute
*platform_attributes
[] = {
823 &dev_attr_performance_level
.attr
,
824 &dev_attr_battery_life_extender
.attr
,
825 &dev_attr_usb_charge
.attr
,
829 static int find_signature(void __iomem
*memcheck
, const char *testStr
)
834 for (loca
= 0; loca
< 0xffff; loca
++) {
835 char temp
= readb(memcheck
+ loca
);
837 if (temp
== testStr
[i
]) {
838 if (i
== strlen(testStr
)-1)
848 static void samsung_rfkill_exit(struct samsung_laptop
*samsung
)
850 if (samsung
->wlan
.rfkill
) {
851 rfkill_unregister(samsung
->wlan
.rfkill
);
852 rfkill_destroy(samsung
->wlan
.rfkill
);
853 samsung
->wlan
.rfkill
= NULL
;
855 if (samsung
->bluetooth
.rfkill
) {
856 rfkill_unregister(samsung
->bluetooth
.rfkill
);
857 rfkill_destroy(samsung
->bluetooth
.rfkill
);
858 samsung
->bluetooth
.rfkill
= NULL
;
862 static int samsung_new_rfkill(struct samsung_laptop
*samsung
,
863 struct samsung_rfkill
*arfkill
,
864 const char *name
, enum rfkill_type type
,
865 const struct rfkill_ops
*ops
,
868 struct rfkill
**rfkill
= &arfkill
->rfkill
;
871 arfkill
->type
= type
;
872 arfkill
->samsung
= samsung
;
874 *rfkill
= rfkill_alloc(name
, &samsung
->platform_device
->dev
,
881 rfkill_init_sw_state(*rfkill
, blocked
);
883 ret
= rfkill_register(*rfkill
);
885 rfkill_destroy(*rfkill
);
892 static int __init
samsung_rfkill_init_seclinux(struct samsung_laptop
*samsung
)
894 return samsung_new_rfkill(samsung
, &samsung
->wlan
, "samsung-wlan",
895 RFKILL_TYPE_WLAN
, &seclinux_rfkill_ops
, -1);
898 static int __init
samsung_rfkill_init_swsmi(struct samsung_laptop
*samsung
)
900 struct sabi_data data
;
903 ret
= swsmi_wireless_status(samsung
, &data
);
905 /* Some swsmi laptops use the old seclinux way to control
906 * wireless devices */
908 ret
= samsung_rfkill_init_seclinux(samsung
);
912 /* 0x02 seems to mean that the device is no present/available */
914 if (data
.data
[WL_STATUS_WLAN
] != 0x02)
915 ret
= samsung_new_rfkill(samsung
, &samsung
->wlan
,
919 !data
.data
[WL_STATUS_WLAN
]);
923 if (data
.data
[WL_STATUS_BT
] != 0x02)
924 ret
= samsung_new_rfkill(samsung
, &samsung
->bluetooth
,
926 RFKILL_TYPE_BLUETOOTH
,
928 !data
.data
[WL_STATUS_BT
]);
934 samsung_rfkill_exit(samsung
);
939 static int __init
samsung_rfkill_init(struct samsung_laptop
*samsung
)
941 if (samsung
->config
->sabi_version
== 2)
942 return samsung_rfkill_init_seclinux(samsung
);
943 if (samsung
->config
->sabi_version
== 3)
944 return samsung_rfkill_init_swsmi(samsung
);
948 static int kbd_backlight_enable(struct samsung_laptop
*samsung
)
950 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
951 struct sabi_data data
;
954 if (commands
->kbd_backlight
== 0xFFFF)
957 memset(&data
, 0, sizeof(data
));
959 retval
= sabi_command(samsung
, commands
->kbd_backlight
,
965 if (data
.d0
!= 0xccdd)
970 static int kbd_backlight_read(struct samsung_laptop
*samsung
)
972 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
973 struct sabi_data data
;
976 memset(&data
, 0, sizeof(data
));
978 retval
= sabi_command(samsung
, commands
->kbd_backlight
,
987 static int kbd_backlight_write(struct samsung_laptop
*samsung
, int brightness
)
989 const struct sabi_commands
*commands
= &samsung
->config
->commands
;
990 struct sabi_data data
;
992 memset(&data
, 0, sizeof(data
));
993 data
.d0
= 0x82 | ((brightness
& 0xFF) << 8);
994 return sabi_command(samsung
, commands
->kbd_backlight
,
998 static void kbd_led_update(struct work_struct
*work
)
1000 struct samsung_laptop
*samsung
;
1002 samsung
= container_of(work
, struct samsung_laptop
, kbd_led_work
);
1003 kbd_backlight_write(samsung
, samsung
->kbd_led_wk
);
1006 static void kbd_led_set(struct led_classdev
*led_cdev
,
1007 enum led_brightness value
)
1009 struct samsung_laptop
*samsung
;
1011 samsung
= container_of(led_cdev
, struct samsung_laptop
, kbd_led
);
1013 if (value
> samsung
->kbd_led
.max_brightness
)
1014 value
= samsung
->kbd_led
.max_brightness
;
1018 samsung
->kbd_led_wk
= value
;
1019 queue_work(samsung
->led_workqueue
, &samsung
->kbd_led_work
);
1022 static enum led_brightness
kbd_led_get(struct led_classdev
*led_cdev
)
1024 struct samsung_laptop
*samsung
;
1026 samsung
= container_of(led_cdev
, struct samsung_laptop
, kbd_led
);
1027 return kbd_backlight_read(samsung
);
1030 static void samsung_leds_exit(struct samsung_laptop
*samsung
)
1032 if (!IS_ERR_OR_NULL(samsung
->kbd_led
.dev
))
1033 led_classdev_unregister(&samsung
->kbd_led
);
1034 if (samsung
->led_workqueue
)
1035 destroy_workqueue(samsung
->led_workqueue
);
1038 static int __init
samsung_leds_init(struct samsung_laptop
*samsung
)
1042 samsung
->led_workqueue
= create_singlethread_workqueue("led_workqueue");
1043 if (!samsung
->led_workqueue
)
1046 if (kbd_backlight_enable(samsung
) >= 0) {
1047 INIT_WORK(&samsung
->kbd_led_work
, kbd_led_update
);
1049 samsung
->kbd_led
.name
= "samsung::kbd_backlight";
1050 samsung
->kbd_led
.brightness_set
= kbd_led_set
;
1051 samsung
->kbd_led
.brightness_get
= kbd_led_get
;
1052 samsung
->kbd_led
.max_brightness
= 8;
1054 ret
= led_classdev_register(&samsung
->platform_device
->dev
,
1059 samsung_leds_exit(samsung
);
1064 static void samsung_backlight_exit(struct samsung_laptop
*samsung
)
1066 if (samsung
->backlight_device
) {
1067 backlight_device_unregister(samsung
->backlight_device
);
1068 samsung
->backlight_device
= NULL
;
1072 static int __init
samsung_backlight_init(struct samsung_laptop
*samsung
)
1074 struct backlight_device
*bd
;
1075 struct backlight_properties props
;
1077 if (!samsung
->handle_backlight
)
1080 memset(&props
, 0, sizeof(struct backlight_properties
));
1081 props
.type
= BACKLIGHT_PLATFORM
;
1082 props
.max_brightness
= samsung
->config
->max_brightness
-
1083 samsung
->config
->min_brightness
;
1085 bd
= backlight_device_register("samsung",
1086 &samsung
->platform_device
->dev
,
1087 samsung
, &backlight_ops
,
1092 samsung
->backlight_device
= bd
;
1093 samsung
->backlight_device
->props
.brightness
= read_brightness(samsung
);
1094 samsung
->backlight_device
->props
.power
= FB_BLANK_UNBLANK
;
1095 backlight_update_status(samsung
->backlight_device
);
1100 static umode_t
samsung_sysfs_is_visible(struct kobject
*kobj
,
1101 struct attribute
*attr
, int idx
)
1103 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1104 struct platform_device
*pdev
= to_platform_device(dev
);
1105 struct samsung_laptop
*samsung
= platform_get_drvdata(pdev
);
1108 if (attr
== &dev_attr_performance_level
.attr
)
1109 ok
= !!samsung
->config
->performance_levels
[0].name
;
1110 if (attr
== &dev_attr_battery_life_extender
.attr
)
1111 ok
= !!(read_battery_life_extender(samsung
) >= 0);
1112 if (attr
== &dev_attr_usb_charge
.attr
)
1113 ok
= !!(read_usb_charge(samsung
) >= 0);
1115 return ok
? attr
->mode
: 0;
1118 static struct attribute_group platform_attribute_group
= {
1119 .is_visible
= samsung_sysfs_is_visible
,
1120 .attrs
= platform_attributes
1123 static void samsung_sysfs_exit(struct samsung_laptop
*samsung
)
1125 struct platform_device
*device
= samsung
->platform_device
;
1127 sysfs_remove_group(&device
->dev
.kobj
, &platform_attribute_group
);
1130 static int __init
samsung_sysfs_init(struct samsung_laptop
*samsung
)
1132 struct platform_device
*device
= samsung
->platform_device
;
1134 return sysfs_create_group(&device
->dev
.kobj
, &platform_attribute_group
);
1138 static int show_call(struct seq_file
*m
, void *data
)
1140 struct samsung_laptop
*samsung
= m
->private;
1141 struct sabi_data
*sdata
= &samsung
->debug
.data
;
1144 seq_printf(m
, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1145 samsung
->debug
.command
,
1146 sdata
->d0
, sdata
->d1
, sdata
->d2
, sdata
->d3
);
1148 ret
= sabi_command(samsung
, samsung
->debug
.command
, sdata
, sdata
);
1151 seq_printf(m
, "SABI command 0x%04x failed\n",
1152 samsung
->debug
.command
);
1156 seq_printf(m
, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1157 sdata
->d0
, sdata
->d1
, sdata
->d2
, sdata
->d3
);
1161 static int samsung_debugfs_open(struct inode
*inode
, struct file
*file
)
1163 return single_open(file
, show_call
, inode
->i_private
);
1166 static const struct file_operations samsung_laptop_call_io_ops
= {
1167 .owner
= THIS_MODULE
,
1168 .open
= samsung_debugfs_open
,
1170 .llseek
= seq_lseek
,
1171 .release
= single_release
,
1174 static void samsung_debugfs_exit(struct samsung_laptop
*samsung
)
1176 debugfs_remove_recursive(samsung
->debug
.root
);
1179 static int samsung_debugfs_init(struct samsung_laptop
*samsung
)
1181 struct dentry
*dent
;
1183 samsung
->debug
.root
= debugfs_create_dir("samsung-laptop", NULL
);
1184 if (!samsung
->debug
.root
) {
1185 pr_err("failed to create debugfs directory");
1189 samsung
->debug
.f0000_wrapper
.data
= samsung
->f0000_segment
;
1190 samsung
->debug
.f0000_wrapper
.size
= 0xffff;
1192 samsung
->debug
.data_wrapper
.data
= &samsung
->debug
.data
;
1193 samsung
->debug
.data_wrapper
.size
= sizeof(samsung
->debug
.data
);
1195 samsung
->debug
.sdiag_wrapper
.data
= samsung
->sdiag
;
1196 samsung
->debug
.sdiag_wrapper
.size
= strlen(samsung
->sdiag
);
1198 dent
= debugfs_create_u16("command", S_IRUGO
| S_IWUSR
,
1199 samsung
->debug
.root
, &samsung
->debug
.command
);
1203 dent
= debugfs_create_u32("d0", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1204 &samsung
->debug
.data
.d0
);
1208 dent
= debugfs_create_u32("d1", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1209 &samsung
->debug
.data
.d1
);
1213 dent
= debugfs_create_u16("d2", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1214 &samsung
->debug
.data
.d2
);
1218 dent
= debugfs_create_u8("d3", S_IRUGO
| S_IWUSR
, samsung
->debug
.root
,
1219 &samsung
->debug
.data
.d3
);
1223 dent
= debugfs_create_blob("data", S_IRUGO
| S_IWUSR
,
1224 samsung
->debug
.root
,
1225 &samsung
->debug
.data_wrapper
);
1229 dent
= debugfs_create_blob("f0000_segment", S_IRUSR
| S_IWUSR
,
1230 samsung
->debug
.root
,
1231 &samsung
->debug
.f0000_wrapper
);
1235 dent
= debugfs_create_file("call", S_IFREG
| S_IRUGO
,
1236 samsung
->debug
.root
, samsung
,
1237 &samsung_laptop_call_io_ops
);
1241 dent
= debugfs_create_blob("sdiag", S_IRUGO
| S_IWUSR
,
1242 samsung
->debug
.root
,
1243 &samsung
->debug
.sdiag_wrapper
);
1250 samsung_debugfs_exit(samsung
);
1254 static void samsung_sabi_exit(struct samsung_laptop
*samsung
)
1256 const struct sabi_config
*config
= samsung
->config
;
1258 /* Turn off "Linux" mode in the BIOS */
1259 if (config
&& config
->commands
.set_linux
!= 0xff)
1260 sabi_set_commandb(samsung
, config
->commands
.set_linux
, 0x80);
1262 if (samsung
->sabi_iface
) {
1263 iounmap(samsung
->sabi_iface
);
1264 samsung
->sabi_iface
= NULL
;
1266 if (samsung
->f0000_segment
) {
1267 iounmap(samsung
->f0000_segment
);
1268 samsung
->f0000_segment
= NULL
;
1271 samsung
->config
= NULL
;
1274 static __init
void samsung_sabi_infos(struct samsung_laptop
*samsung
, int loca
,
1275 unsigned int ifaceP
)
1277 const struct sabi_config
*config
= samsung
->config
;
1279 printk(KERN_DEBUG
"This computer supports SABI==%x\n",
1280 loca
+ 0xf0000 - 6);
1282 printk(KERN_DEBUG
"SABI header:\n");
1283 printk(KERN_DEBUG
" SMI Port Number = 0x%04x\n",
1284 readw(samsung
->sabi
+ config
->header_offsets
.port
));
1285 printk(KERN_DEBUG
" SMI Interface Function = 0x%02x\n",
1286 readb(samsung
->sabi
+ config
->header_offsets
.iface_func
));
1287 printk(KERN_DEBUG
" SMI enable memory buffer = 0x%02x\n",
1288 readb(samsung
->sabi
+ config
->header_offsets
.en_mem
));
1289 printk(KERN_DEBUG
" SMI restore memory buffer = 0x%02x\n",
1290 readb(samsung
->sabi
+ config
->header_offsets
.re_mem
));
1291 printk(KERN_DEBUG
" SABI data offset = 0x%04x\n",
1292 readw(samsung
->sabi
+ config
->header_offsets
.data_offset
));
1293 printk(KERN_DEBUG
" SABI data segment = 0x%04x\n",
1294 readw(samsung
->sabi
+ config
->header_offsets
.data_segment
));
1296 printk(KERN_DEBUG
" SABI pointer = 0x%08x\n", ifaceP
);
1299 static void __init
samsung_sabi_diag(struct samsung_laptop
*samsung
)
1301 int loca
= find_signature(samsung
->f0000_segment
, "SDiaG@");
1308 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1310 * Product name: 90X3A
1311 * BIOS Version: 07HL
1314 for (i
= 0; loca
< 0xffff && i
< sizeof(samsung
->sdiag
) - 1; loca
++) {
1315 char temp
= readb(samsung
->f0000_segment
+ loca
);
1317 if (isalnum(temp
) || temp
== '/' || temp
== '-')
1318 samsung
->sdiag
[i
++] = temp
;
1323 if (debug
&& samsung
->sdiag
[0])
1324 pr_info("sdiag: %s", samsung
->sdiag
);
1327 static int __init
samsung_sabi_init(struct samsung_laptop
*samsung
)
1329 const struct sabi_config
*config
= NULL
;
1330 const struct sabi_commands
*commands
;
1331 unsigned int ifaceP
;
1336 samsung
->f0000_segment
= ioremap_nocache(0xf0000, 0xffff);
1337 if (!samsung
->f0000_segment
) {
1339 pr_err("Can't map the segment at 0xf0000\n");
1344 samsung_sabi_diag(samsung
);
1346 /* Try to find one of the signatures in memory to find the header */
1347 for (i
= 0; sabi_configs
[i
].test_string
!= 0; ++i
) {
1348 samsung
->config
= &sabi_configs
[i
];
1349 loca
= find_signature(samsung
->f0000_segment
,
1350 samsung
->config
->test_string
);
1355 if (loca
== 0xffff) {
1357 pr_err("This computer does not support SABI\n");
1362 config
= samsung
->config
;
1363 commands
= &config
->commands
;
1365 /* point to the SMI port Number */
1367 samsung
->sabi
= (samsung
->f0000_segment
+ loca
);
1369 /* Get a pointer to the SABI Interface */
1370 ifaceP
= (readw(samsung
->sabi
+ config
->header_offsets
.data_segment
) & 0x0ffff) << 4;
1371 ifaceP
+= readw(samsung
->sabi
+ config
->header_offsets
.data_offset
) & 0x0ffff;
1374 samsung_sabi_infos(samsung
, loca
, ifaceP
);
1376 samsung
->sabi_iface
= ioremap_nocache(ifaceP
, 16);
1377 if (!samsung
->sabi_iface
) {
1378 pr_err("Can't remap %x\n", ifaceP
);
1383 /* Turn on "Linux" mode in the BIOS */
1384 if (commands
->set_linux
!= 0xff) {
1385 int retval
= sabi_set_commandb(samsung
,
1386 commands
->set_linux
, 0x81);
1388 pr_warn("Linux mode was not set!\n");
1394 /* Check for stepping quirk */
1395 if (samsung
->handle_backlight
)
1396 check_for_stepping_quirk(samsung
);
1398 pr_info("detected SABI interface: %s\n",
1399 samsung
->config
->test_string
);
1403 samsung_sabi_exit(samsung
);
1408 static void samsung_platform_exit(struct samsung_laptop
*samsung
)
1410 if (samsung
->platform_device
) {
1411 platform_device_unregister(samsung
->platform_device
);
1412 samsung
->platform_device
= NULL
;
1416 static int __init
samsung_platform_init(struct samsung_laptop
*samsung
)
1418 struct platform_device
*pdev
;
1420 pdev
= platform_device_register_simple("samsung", -1, NULL
, 0);
1422 return PTR_ERR(pdev
);
1424 samsung
->platform_device
= pdev
;
1425 platform_set_drvdata(samsung
->platform_device
, samsung
);
1429 static struct samsung_quirks
*quirks
;
1431 static int __init
samsung_dmi_matched(const struct dmi_system_id
*d
)
1433 quirks
= d
->driver_data
;
1437 static struct dmi_system_id __initdata samsung_dmi_table
[] = {
1440 DMI_MATCH(DMI_SYS_VENDOR
,
1441 "SAMSUNG ELECTRONICS CO., LTD."),
1442 DMI_MATCH(DMI_CHASSIS_TYPE
, "8"), /* Portable */
1447 DMI_MATCH(DMI_SYS_VENDOR
,
1448 "SAMSUNG ELECTRONICS CO., LTD."),
1449 DMI_MATCH(DMI_CHASSIS_TYPE
, "9"), /* Laptop */
1454 DMI_MATCH(DMI_SYS_VENDOR
,
1455 "SAMSUNG ELECTRONICS CO., LTD."),
1456 DMI_MATCH(DMI_CHASSIS_TYPE
, "10"), /* Notebook */
1461 DMI_MATCH(DMI_SYS_VENDOR
,
1462 "SAMSUNG ELECTRONICS CO., LTD."),
1463 DMI_MATCH(DMI_CHASSIS_TYPE
, "14"), /* Sub-Notebook */
1466 /* DMI ids for laptops with bad Chassis Type */
1470 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1471 DMI_MATCH(DMI_PRODUCT_NAME
, "R40/R41"),
1472 DMI_MATCH(DMI_BOARD_NAME
, "R40/R41"),
1475 /* Specific DMI ids for laptop with quirks */
1477 .callback
= samsung_dmi_matched
,
1480 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1481 DMI_MATCH(DMI_PRODUCT_NAME
, "N150P"),
1482 DMI_MATCH(DMI_BOARD_NAME
, "N150P"),
1484 .driver_data
= &samsung_broken_acpi_video
,
1487 .callback
= samsung_dmi_matched
,
1488 .ident
= "N145P/N250P/N260P",
1490 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1491 DMI_MATCH(DMI_PRODUCT_NAME
, "N145P/N250P/N260P"),
1492 DMI_MATCH(DMI_BOARD_NAME
, "N145P/N250P/N260P"),
1494 .driver_data
= &samsung_broken_acpi_video
,
1497 .callback
= samsung_dmi_matched
,
1498 .ident
= "N150/N210/N220",
1500 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1501 DMI_MATCH(DMI_PRODUCT_NAME
, "N150/N210/N220"),
1502 DMI_MATCH(DMI_BOARD_NAME
, "N150/N210/N220"),
1504 .driver_data
= &samsung_broken_acpi_video
,
1507 .callback
= samsung_dmi_matched
,
1508 .ident
= "NF110/NF210/NF310",
1510 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1511 DMI_MATCH(DMI_PRODUCT_NAME
, "NF110/NF210/NF310"),
1512 DMI_MATCH(DMI_BOARD_NAME
, "NF110/NF210/NF310"),
1514 .driver_data
= &samsung_broken_acpi_video
,
1517 .callback
= samsung_dmi_matched
,
1520 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
1521 DMI_MATCH(DMI_PRODUCT_NAME
, "X360"),
1522 DMI_MATCH(DMI_BOARD_NAME
, "X360"),
1524 .driver_data
= &samsung_broken_acpi_video
,
1528 MODULE_DEVICE_TABLE(dmi
, samsung_dmi_table
);
1530 static struct platform_device
*samsung_platform_device
;
1532 static int __init
samsung_init(void)
1534 struct samsung_laptop
*samsung
;
1537 quirks
= &samsung_unknown
;
1538 if (!force
&& !dmi_check_system(samsung_dmi_table
))
1541 samsung
= kzalloc(sizeof(*samsung
), GFP_KERNEL
);
1545 mutex_init(&samsung
->sabi_mutex
);
1546 samsung
->handle_backlight
= true;
1547 samsung
->quirks
= quirks
;
1551 if (samsung
->quirks
->broken_acpi_video
)
1552 acpi_video_dmi_promote_vendor();
1554 /* Don't handle backlight here if the acpi video already handle it */
1555 if (acpi_video_backlight_support()) {
1556 samsung
->handle_backlight
= false;
1557 } else if (samsung
->quirks
->broken_acpi_video
) {
1558 pr_info("Disabling ACPI video driver\n");
1559 acpi_video_unregister();
1563 ret
= samsung_platform_init(samsung
);
1565 goto error_platform
;
1567 ret
= samsung_sabi_init(samsung
);
1572 /* Only log that if we are really on a sabi platform */
1573 if (acpi_video_backlight_support())
1574 pr_info("Backlight controlled by ACPI video driver\n");
1577 ret
= samsung_sysfs_init(samsung
);
1581 ret
= samsung_backlight_init(samsung
);
1583 goto error_backlight
;
1585 ret
= samsung_rfkill_init(samsung
);
1589 ret
= samsung_leds_init(samsung
);
1593 ret
= samsung_debugfs_init(samsung
);
1597 samsung_platform_device
= samsung
->platform_device
;
1601 samsung_leds_exit(samsung
);
1603 samsung_rfkill_exit(samsung
);
1605 samsung_backlight_exit(samsung
);
1607 samsung_sysfs_exit(samsung
);
1609 samsung_sabi_exit(samsung
);
1611 samsung_platform_exit(samsung
);
1617 static void __exit
samsung_exit(void)
1619 struct samsung_laptop
*samsung
;
1621 samsung
= platform_get_drvdata(samsung_platform_device
);
1623 samsung_debugfs_exit(samsung
);
1624 samsung_leds_exit(samsung
);
1625 samsung_rfkill_exit(samsung
);
1626 samsung_backlight_exit(samsung
);
1627 samsung_sysfs_exit(samsung
);
1628 samsung_sabi_exit(samsung
);
1629 samsung_platform_exit(samsung
);
1632 samsung_platform_device
= NULL
;
1635 module_init(samsung_init
);
1636 module_exit(samsung_exit
);
1638 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1639 MODULE_DESCRIPTION("Samsung Backlight driver");
1640 MODULE_LICENSE("GPL");