Skip to content

Commit 84d482f

Browse files
iksaifMatthew Garrett
authored andcommitted
samsung-laptop: add true rfkill support for swsmi
The wireless status get and get commands seems to use one byte per device. First byte is for wlan and third is for bluetooh, we will have to find what the other are for. Signed-off-by: Corentin Chary <corentincj@iksaif.net> Acked-by: Greg Kroah-Hartman <gregkh@suse.de> Signed-off-by: Matthew Garrett <mjg@redhat.com>
1 parent f674ebf commit 84d482f

1 file changed

Lines changed: 177 additions & 30 deletions

File tree

‎drivers/platform/x86/samsung-laptop.c‎

Lines changed: 177 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
#define SABI_IFACE_COMPLETE 0x04
4646
#define SABI_IFACE_DATA 0x05
4747

48+
#define WL_STATUS_WLAN 0x0
49+
#define WL_STATUS_BT 0x2
50+
4851
/* Structure get/set data using sabi */
4952
struct sabi_data {
5053
union {
@@ -113,6 +116,10 @@ struct sabi_commands {
113116
u16 get_usb_charge;
114117
u16 set_usb_charge;
115118

119+
/* the first byte is for bluetooth and the third one is for wlan */
120+
u16 get_wireless_status;
121+
u16 set_wireless_status;
122+
116123
/* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
117124
u16 kbd_backlight;
118125

@@ -129,6 +136,7 @@ struct sabi_performance_level {
129136
};
130137

131138
struct sabi_config {
139+
int sabi_version;
132140
const char *test_string;
133141
u16 main_function;
134142
const struct sabi_header_offsets header_offsets;
@@ -140,6 +148,10 @@ struct sabi_config {
140148

141149
static const struct sabi_config sabi_configs[] = {
142150
{
151+
/* I don't know if it is really 2, but it it is
152+
* less than 3 anyway */
153+
.sabi_version = 2,
154+
143155
.test_string = "SECLINUX",
144156

145157
.main_function = 0x4c49,
@@ -175,6 +187,9 @@ static const struct sabi_config sabi_configs[] = {
175187
.get_usb_charge = 0xFFFF,
176188
.set_usb_charge = 0xFFFF,
177189

190+
.get_wireless_status = 0xFFFF,
191+
.set_wireless_status = 0xFFFF,
192+
178193
.kbd_backlight = 0xFFFF,
179194

180195
.set_linux = 0x0a,
@@ -195,6 +210,8 @@ static const struct sabi_config sabi_configs[] = {
195210
.max_brightness = 8,
196211
},
197212
{
213+
.sabi_version = 3,
214+
198215
.test_string = "SwSmi@",
199216

200217
.main_function = 0x5843,
@@ -230,6 +247,9 @@ static const struct sabi_config sabi_configs[] = {
230247
.get_usb_charge = 0x67,
231248
.set_usb_charge = 0x68,
232249

250+
.get_wireless_status = 0x69,
251+
.set_wireless_status = 0x6a,
252+
233253
.kbd_backlight = 0x78,
234254

235255
.set_linux = 0xff,
@@ -285,6 +305,14 @@ struct samsung_laptop_debug {
285305
struct debugfs_blob_wrapper data_wrapper;
286306
};
287307

308+
struct samsung_laptop;
309+
310+
struct samsung_rfkill {
311+
struct samsung_laptop *samsung;
312+
struct rfkill *rfkill;
313+
enum rfkill_type type;
314+
};
315+
288316
struct samsung_laptop {
289317
const struct sabi_config *config;
290318

@@ -296,7 +324,9 @@ struct samsung_laptop {
296324

297325
struct platform_device *platform_device;
298326
struct backlight_device *backlight_device;
299-
struct rfkill *rfk;
327+
328+
struct samsung_rfkill wlan;
329+
struct samsung_rfkill bluetooth;
300330

301331
struct led_classdev kbd_led;
302332
int kbd_led_wk;
@@ -498,26 +528,78 @@ static const struct backlight_ops backlight_ops = {
498528
.update_status = update_status,
499529
};
500530

501-
static int rfkill_set(void *data, bool blocked)
531+
static int seclinux_rfkill_set(void *data, bool blocked)
502532
{
503533
struct samsung_laptop *samsung = data;
504534
const struct sabi_commands *commands = &samsung->config->commands;
505535

506-
/* Do something with blocked...*/
507-
/*
508-
* blocked == false is on
509-
* blocked == true is off
510-
*/
511-
if (blocked)
512-
sabi_set_commandb(samsung, commands->set_wireless_button, 0);
536+
return sabi_set_commandb(samsung, commands->set_wireless_button,
537+
!blocked);
538+
}
539+
540+
static struct rfkill_ops seclinux_rfkill_ops = {
541+
.set_block = seclinux_rfkill_set,
542+
};
543+
544+
static int swsmi_wireless_status(struct samsung_laptop *samsung,
545+
struct sabi_data *data)
546+
{
547+
const struct sabi_commands *commands = &samsung->config->commands;
548+
549+
return sabi_command(samsung, commands->get_wireless_status,
550+
NULL, data);
551+
}
552+
553+
static int swsmi_rfkill_set(void *priv, bool blocked)
554+
{
555+
struct samsung_rfkill *srfkill = priv;
556+
struct samsung_laptop *samsung = srfkill->samsung;
557+
const struct sabi_commands *commands = &samsung->config->commands;
558+
struct sabi_data data;
559+
int ret, i;
560+
561+
ret = swsmi_wireless_status(samsung, &data);
562+
if (ret)
563+
return ret;
564+
565+
/* Don't set the state for non-present devices */
566+
for (i = 0; i < 4; i++)
567+
if (data.data[i] == 0x02)
568+
data.data[1] = 0;
569+
570+
if (srfkill->type == RFKILL_TYPE_WLAN)
571+
data.data[WL_STATUS_WLAN] = !blocked;
572+
else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
573+
data.data[WL_STATUS_BT] = !blocked;
574+
575+
return sabi_command(samsung, commands->set_wireless_status,
576+
&data, &data);
577+
}
578+
579+
static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
580+
{
581+
struct samsung_rfkill *srfkill = priv;
582+
struct samsung_laptop *samsung = srfkill->samsung;
583+
struct sabi_data data;
584+
int ret;
585+
586+
ret = swsmi_wireless_status(samsung, &data);
587+
if (ret)
588+
return ;
589+
590+
if (srfkill->type == RFKILL_TYPE_WLAN)
591+
ret = data.data[WL_STATUS_WLAN];
592+
else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
593+
ret = data.data[WL_STATUS_BT];
513594
else
514-
sabi_set_commandb(samsung, commands->set_wireless_button, 1);
595+
return ;
515596

516-
return 0;
597+
rfkill_set_sw_state(rfkill, !ret);
517598
}
518599

519-
static struct rfkill_ops rfkill_ops = {
520-
.set_block = rfkill_set,
600+
static struct rfkill_ops swsmi_rfkill_ops = {
601+
.set_block = swsmi_rfkill_set,
602+
.query = swsmi_rfkill_query,
521603
};
522604

523605
static ssize_t get_performance_level(struct device *dev,
@@ -742,31 +824,96 @@ static int find_signature(void __iomem *memcheck, const char *testStr)
742824

743825
static void samsung_rfkill_exit(struct samsung_laptop *samsung)
744826
{
745-
if (samsung->rfk) {
746-
rfkill_unregister(samsung->rfk);
747-
rfkill_destroy(samsung->rfk);
748-
samsung->rfk = NULL;
827+
if (samsung->wlan.rfkill) {
828+
rfkill_unregister(samsung->wlan.rfkill);
829+
rfkill_destroy(samsung->wlan.rfkill);
830+
samsung->wlan.rfkill = NULL;
831+
}
832+
if (samsung->bluetooth.rfkill) {
833+
rfkill_unregister(samsung->bluetooth.rfkill);
834+
rfkill_destroy(samsung->bluetooth.rfkill);
835+
samsung->bluetooth.rfkill = NULL;
749836
}
750837
}
751838

752-
static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
839+
static int samsung_new_rfkill(struct samsung_laptop *samsung,
840+
struct samsung_rfkill *arfkill,
841+
const char *name, enum rfkill_type type,
842+
const struct rfkill_ops *ops,
843+
int blocked)
753844
{
754-
int retval;
845+
struct rfkill **rfkill = &arfkill->rfkill;
846+
int ret;
755847

756-
samsung->rfk = rfkill_alloc("samsung-wifi",
757-
&samsung->platform_device->dev,
758-
RFKILL_TYPE_WLAN,
759-
&rfkill_ops, samsung);
760-
if (!samsung->rfk)
761-
return -ENOMEM;
848+
arfkill->type = type;
849+
arfkill->samsung = samsung;
762850

763-
retval = rfkill_register(samsung->rfk);
764-
if (retval) {
765-
rfkill_destroy(samsung->rfk);
766-
samsung->rfk = NULL;
767-
return -ENODEV;
851+
*rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
852+
type, ops, arfkill);
853+
854+
if (!*rfkill)
855+
return -EINVAL;
856+
857+
if (blocked != -1)
858+
rfkill_init_sw_state(*rfkill, blocked);
859+
860+
ret = rfkill_register(*rfkill);
861+
if (ret) {
862+
rfkill_destroy(*rfkill);
863+
*rfkill = NULL;
864+
return ret;
768865
}
866+
return 0;
867+
}
868+
869+
static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
870+
{
871+
return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
872+
RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
873+
}
769874

875+
static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
876+
{
877+
struct sabi_data data;
878+
int ret;
879+
880+
ret = swsmi_wireless_status(samsung, &data);
881+
if (ret)
882+
return ret;
883+
884+
/* 0x02 seems to mean that the device is no present/available */
885+
886+
if (data.data[WL_STATUS_WLAN] != 0x02)
887+
ret = samsung_new_rfkill(samsung, &samsung->wlan,
888+
"samsung-wlan",
889+
RFKILL_TYPE_WLAN,
890+
&swsmi_rfkill_ops,
891+
!data.data[WL_STATUS_WLAN]);
892+
if (ret)
893+
goto exit;
894+
895+
if (data.data[WL_STATUS_BT] != 0x02)
896+
ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
897+
"samsung-bluetooth",
898+
RFKILL_TYPE_BLUETOOTH,
899+
&swsmi_rfkill_ops,
900+
!data.data[WL_STATUS_BT]);
901+
if (ret)
902+
goto exit;
903+
904+
exit:
905+
if (ret)
906+
samsung_rfkill_exit(samsung);
907+
908+
return ret;
909+
}
910+
911+
static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
912+
{
913+
if (samsung->config->sabi_version == 2)
914+
return samsung_rfkill_init_seclinux(samsung);
915+
if (samsung->config->sabi_version == 3)
916+
return samsung_rfkill_init_swsmi(samsung);
770917
return 0;
771918
}
772919

0 commit comments

Comments
 (0)