/
samsung-laptop.c
1654 lines (1346 loc) · 39.6 KB
/
samsung-laptop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Samsung Laptop driver
*
* Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
* Copyright (C) 2009,2011 Novell Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/backlight.h>
#include <linux/leds.h>
#include <linux/fb.h>
#include <linux/dmi.h>
#include <linux/platform_device.h>
#include <linux/rfkill.h>
#include <linux/acpi.h>
#include <linux/seq_file.h>
#include <linux/debugfs.h>
#include <linux/ctype.h>
#include <linux/efi.h>
#include <acpi/video.h>
/*
* This driver is needed because a number of Samsung laptops do not hook
* their control settings through ACPI. So we have to poke around in the
* BIOS to do things like brightness values, and "special" key controls.
*/
/*
* We have 0 - 8 as valid brightness levels. The specs say that level 0 should
* be reserved by the BIOS (which really doesn't make much sense), we tell
* userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
*/
#define MAX_BRIGHT 0x07
#define SABI_IFACE_MAIN 0x00
#define SABI_IFACE_SUB 0x02
#define SABI_IFACE_COMPLETE 0x04
#define SABI_IFACE_DATA 0x05
#define WL_STATUS_WLAN 0x0
#define WL_STATUS_BT 0x2
/* Structure get/set data using sabi */
struct sabi_data {
union {
struct {
u32 d0;
u32 d1;
u16 d2;
u8 d3;
};
u8 data[11];
};
};
struct sabi_header_offsets {
u8 port;
u8 re_mem;
u8 iface_func;
u8 en_mem;
u8 data_offset;
u8 data_segment;
};
struct sabi_commands {
/*
* Brightness is 0 - 8, as described above.
* Value 0 is for the BIOS to use
*/
u16 get_brightness;
u16 set_brightness;
/*
* first byte:
* 0x00 - wireless is off
* 0x01 - wireless is on
* second byte:
* 0x02 - 3G is off
* 0x03 - 3G is on
* TODO, verify 3G is correct, that doesn't seem right...
*/
u16 get_wireless_button;
u16 set_wireless_button;
/* 0 is off, 1 is on */
u16 get_backlight;
u16 set_backlight;
/*
* 0x80 or 0x00 - no action
* 0x81 - recovery key pressed
*/
u16 get_recovery_mode;
u16 set_recovery_mode;
/*
* on seclinux: 0 is low, 1 is high,
* on swsmi: 0 is normal, 1 is silent, 2 is turbo
*/
u16 get_performance_level;
u16 set_performance_level;
/* 0x80 is off, 0x81 is on */
u16 get_battery_life_extender;
u16 set_battery_life_extender;
/* 0x80 is off, 0x81 is on */
u16 get_usb_charge;
u16 set_usb_charge;
/* the first byte is for bluetooth and the third one is for wlan */
u16 get_wireless_status;
u16 set_wireless_status;
/* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
u16 kbd_backlight;
/*
* Tell the BIOS that Linux is running on this machine.
* 81 is on, 80 is off
*/
u16 set_linux;
};
struct sabi_performance_level {
const char *name;
u16 value;
};
struct sabi_config {
int sabi_version;
const char *test_string;
u16 main_function;
const struct sabi_header_offsets header_offsets;
const struct sabi_commands commands;
const struct sabi_performance_level performance_levels[4];
u8 min_brightness;
u8 max_brightness;
};
static const struct sabi_config sabi_configs[] = {
{
/* I don't know if it is really 2, but it it is
* less than 3 anyway */
.sabi_version = 2,
.test_string = "SECLINUX",
.main_function = 0x4c49,
.header_offsets = {
.port = 0x00,
.re_mem = 0x02,
.iface_func = 0x03,
.en_mem = 0x04,
.data_offset = 0x05,
.data_segment = 0x07,
},
.commands = {
.get_brightness = 0x00,
.set_brightness = 0x01,
.get_wireless_button = 0x02,
.set_wireless_button = 0x03,
.get_backlight = 0x04,
.set_backlight = 0x05,
.get_recovery_mode = 0x06,
.set_recovery_mode = 0x07,
.get_performance_level = 0x08,
.set_performance_level = 0x09,
.get_battery_life_extender = 0xFFFF,
.set_battery_life_extender = 0xFFFF,
.get_usb_charge = 0xFFFF,
.set_usb_charge = 0xFFFF,
.get_wireless_status = 0xFFFF,
.set_wireless_status = 0xFFFF,
.kbd_backlight = 0xFFFF,
.set_linux = 0x0a,
},
.performance_levels = {
{
.name = "silent",
.value = 0,
},
{
.name = "normal",
.value = 1,
},
{ },
},
.min_brightness = 1,
.max_brightness = 8,
},
{
.sabi_version = 3,
.test_string = "SwSmi@",
.main_function = 0x5843,
.header_offsets = {
.port = 0x00,
.re_mem = 0x04,
.iface_func = 0x02,
.en_mem = 0x03,
.data_offset = 0x05,
.data_segment = 0x07,
},
.commands = {
.get_brightness = 0x10,
.set_brightness = 0x11,
.get_wireless_button = 0x12,
.set_wireless_button = 0x13,
.get_backlight = 0x2d,
.set_backlight = 0x2e,
.get_recovery_mode = 0xff,
.set_recovery_mode = 0xff,
.get_performance_level = 0x31,
.set_performance_level = 0x32,
.get_battery_life_extender = 0x65,
.set_battery_life_extender = 0x66,
.get_usb_charge = 0x67,
.set_usb_charge = 0x68,
.get_wireless_status = 0x69,
.set_wireless_status = 0x6a,
.kbd_backlight = 0x78,
.set_linux = 0xff,
},
.performance_levels = {
{
.name = "normal",
.value = 0,
},
{
.name = "silent",
.value = 1,
},
{
.name = "overclock",
.value = 2,
},
{ },
},
.min_brightness = 0,
.max_brightness = 8,
},
{ },
};
/*
* samsung-laptop/ - debugfs root directory
* f0000_segment - dump f0000 segment
* command - current command
* data - current data
* d0, d1, d2, d3 - data fields
* call - call SABI using command and data
*
* This allow to call arbitrary sabi commands wihout
* modifying the driver at all.
* For example, setting the keyboard backlight brightness to 5
*
* echo 0x78 > command
* echo 0x0582 > d0
* echo 0 > d1
* echo 0 > d2
* echo 0 > d3
* cat call
*/
struct samsung_laptop_debug {
struct dentry *root;
struct sabi_data data;
u16 command;
struct debugfs_blob_wrapper f0000_wrapper;
struct debugfs_blob_wrapper data_wrapper;
struct debugfs_blob_wrapper sdiag_wrapper;
};
struct samsung_laptop;
struct samsung_rfkill {
struct samsung_laptop *samsung;
struct rfkill *rfkill;
enum rfkill_type type;
};
struct samsung_laptop {
const struct sabi_config *config;
void __iomem *sabi;
void __iomem *sabi_iface;
void __iomem *f0000_segment;
struct mutex sabi_mutex;
struct platform_device *platform_device;
struct backlight_device *backlight_device;
struct samsung_rfkill wlan;
struct samsung_rfkill bluetooth;
struct led_classdev kbd_led;
int kbd_led_wk;
struct workqueue_struct *led_workqueue;
struct work_struct kbd_led_work;
struct samsung_laptop_debug debug;
struct samsung_quirks *quirks;
bool handle_backlight;
bool has_stepping_quirk;
char sdiag[64];
};
struct samsung_quirks {
bool broken_acpi_video;
};
static struct samsung_quirks samsung_unknown = {};
static struct samsung_quirks samsung_broken_acpi_video = {
.broken_acpi_video = true,
};
static bool force;
module_param(force, bool, 0);
MODULE_PARM_DESC(force,
"Disable the DMI check and forces the driver to be loaded");
static bool debug;
module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");
static int sabi_command(struct samsung_laptop *samsung, u16 command,
struct sabi_data *in,
struct sabi_data *out)
{
const struct sabi_config *config = samsung->config;
int ret = 0;
u16 port = readw(samsung->sabi + config->header_offsets.port);
u8 complete, iface_data;
mutex_lock(&samsung->sabi_mutex);
if (debug) {
if (in)
pr_info("SABI command:0x%04x "
"data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
command, in->d0, in->d1, in->d2, in->d3);
else
pr_info("SABI command:0x%04x", command);
}
/* enable memory to be able to write to it */
outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
/* write out the command */
writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
if (in) {
writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
}
outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
/* write protect memory to make it safe */
outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
/* see if the command actually succeeded */
complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
/* iface_data = 0xFF happens when a command is not known
* so we only add a warning in debug mode since we will
* probably issue some unknown command at startup to find
* out which features are supported */
if (complete != 0xaa || (iface_data == 0xff && debug))
pr_warn("SABI command 0x%04x failed with"
" completion flag 0x%02x and interface data 0x%02x",
command, complete, iface_data);
if (complete != 0xaa || iface_data == 0xff) {
ret = -EINVAL;
goto exit;
}
if (out) {
out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
}
if (debug && out) {
pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
out->d0, out->d1, out->d2, out->d3);
}
exit:
mutex_unlock(&samsung->sabi_mutex);
return ret;
}
/* simple wrappers usable with most commands */
static int sabi_set_commandb(struct samsung_laptop *samsung,
u16 command, u8 data)
{
struct sabi_data in = { { { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 } } };
in.data[0] = data;
return sabi_command(samsung, command, &in, NULL);
}
static int read_brightness(struct samsung_laptop *samsung)
{
const struct sabi_config *config = samsung->config;
const struct sabi_commands *commands = &samsung->config->commands;
struct sabi_data sretval;
int user_brightness = 0;
int retval;
retval = sabi_command(samsung, commands->get_brightness,
NULL, &sretval);
if (retval)
return retval;
user_brightness = sretval.data[0];
if (user_brightness > config->min_brightness)
user_brightness -= config->min_brightness;
else
user_brightness = 0;
return user_brightness;
}
static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
{
const struct sabi_config *config = samsung->config;
const struct sabi_commands *commands = &samsung->config->commands;
u8 user_level = user_brightness + config->min_brightness;
if (samsung->has_stepping_quirk && user_level != 0) {
/*
* short circuit if the specified level is what's already set
* to prevent the screen from flickering needlessly
*/
if (user_brightness == read_brightness(samsung))
return;
sabi_set_commandb(samsung, commands->set_brightness, 0);
}
sabi_set_commandb(samsung, commands->set_brightness, user_level);
}
static int get_brightness(struct backlight_device *bd)
{
struct samsung_laptop *samsung = bl_get_data(bd);
return read_brightness(samsung);
}
static void check_for_stepping_quirk(struct samsung_laptop *samsung)
{
int initial_level;
int check_level;
int orig_level = read_brightness(samsung);
/*
* Some laptops exhibit the strange behaviour of stepping toward
* (rather than setting) the brightness except when changing to/from
* brightness level 0. This behaviour is checked for here and worked
* around in set_brightness.
*/
if (orig_level == 0)
set_brightness(samsung, 1);
initial_level = read_brightness(samsung);
if (initial_level <= 2)
check_level = initial_level + 2;
else
check_level = initial_level - 2;
samsung->has_stepping_quirk = false;
set_brightness(samsung, check_level);
if (read_brightness(samsung) != check_level) {
samsung->has_stepping_quirk = true;
pr_info("enabled workaround for brightness stepping quirk\n");
}
set_brightness(samsung, orig_level);
}
static int update_status(struct backlight_device *bd)
{
struct samsung_laptop *samsung = bl_get_data(bd);
const struct sabi_commands *commands = &samsung->config->commands;
set_brightness(samsung, bd->props.brightness);
if (bd->props.power == FB_BLANK_UNBLANK)
sabi_set_commandb(samsung, commands->set_backlight, 1);
else
sabi_set_commandb(samsung, commands->set_backlight, 0);
return 0;
}
static const struct backlight_ops backlight_ops = {
.get_brightness = get_brightness,
.update_status = update_status,
};
static int seclinux_rfkill_set(void *data, bool blocked)
{
struct samsung_rfkill *srfkill = data;
struct samsung_laptop *samsung = srfkill->samsung;
const struct sabi_commands *commands = &samsung->config->commands;
return sabi_set_commandb(samsung, commands->set_wireless_button,
!blocked);
}
static struct rfkill_ops seclinux_rfkill_ops = {
.set_block = seclinux_rfkill_set,
};
static int swsmi_wireless_status(struct samsung_laptop *samsung,
struct sabi_data *data)
{
const struct sabi_commands *commands = &samsung->config->commands;
return sabi_command(samsung, commands->get_wireless_status,
NULL, data);
}
static int swsmi_rfkill_set(void *priv, bool blocked)
{
struct samsung_rfkill *srfkill = priv;
struct samsung_laptop *samsung = srfkill->samsung;
const struct sabi_commands *commands = &samsung->config->commands;
struct sabi_data data;
int ret, i;
ret = swsmi_wireless_status(samsung, &data);
if (ret)
return ret;
/* Don't set the state for non-present devices */
for (i = 0; i < 4; i++)
if (data.data[i] == 0x02)
data.data[1] = 0;
if (srfkill->type == RFKILL_TYPE_WLAN)
data.data[WL_STATUS_WLAN] = !blocked;
else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
data.data[WL_STATUS_BT] = !blocked;
return sabi_command(samsung, commands->set_wireless_status,
&data, &data);
}
static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
{
struct samsung_rfkill *srfkill = priv;
struct samsung_laptop *samsung = srfkill->samsung;
struct sabi_data data;
int ret;
ret = swsmi_wireless_status(samsung, &data);
if (ret)
return ;
if (srfkill->type == RFKILL_TYPE_WLAN)
ret = data.data[WL_STATUS_WLAN];
else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
ret = data.data[WL_STATUS_BT];
else
return ;
rfkill_set_sw_state(rfkill, !ret);
}
static struct rfkill_ops swsmi_rfkill_ops = {
.set_block = swsmi_rfkill_set,
.query = swsmi_rfkill_query,
};
static ssize_t get_performance_level(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct samsung_laptop *samsung = dev_get_drvdata(dev);
const struct sabi_config *config = samsung->config;
const struct sabi_commands *commands = &config->commands;
struct sabi_data sretval;
int retval;
int i;
/* Read the state */
retval = sabi_command(samsung, commands->get_performance_level,
NULL, &sretval);
if (retval)
return retval;
/* The logic is backwards, yeah, lots of fun... */
for (i = 0; config->performance_levels[i].name; ++i) {
if (sretval.data[0] == config->performance_levels[i].value)
return sprintf(buf, "%s\n", config->performance_levels[i].name);
}
return sprintf(buf, "%s\n", "unknown");
}
static ssize_t set_performance_level(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t count)
{
struct samsung_laptop *samsung = dev_get_drvdata(dev);
const struct sabi_config *config = samsung->config;
const struct sabi_commands *commands = &config->commands;
int i;
if (count < 1)
return count;
for (i = 0; config->performance_levels[i].name; ++i) {
const struct sabi_performance_level *level =
&config->performance_levels[i];
if (!strncasecmp(level->name, buf, strlen(level->name))) {
sabi_set_commandb(samsung,
commands->set_performance_level,
level->value);
break;
}
}
if (!config->performance_levels[i].name)
return -EINVAL;
return count;
}
static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
get_performance_level, set_performance_level);
static int read_battery_life_extender(struct samsung_laptop *samsung)
{
const struct sabi_commands *commands = &samsung->config->commands;
struct sabi_data data;
int retval;
if (commands->get_battery_life_extender == 0xFFFF)
return -ENODEV;
memset(&data, 0, sizeof(data));
data.data[0] = 0x80;
retval = sabi_command(samsung, commands->get_battery_life_extender,
&data, &data);
if (retval)
return retval;
if (data.data[0] != 0 && data.data[0] != 1)
return -ENODEV;
return data.data[0];
}
static int write_battery_life_extender(struct samsung_laptop *samsung,
int enabled)
{
const struct sabi_commands *commands = &samsung->config->commands;
struct sabi_data data;
memset(&data, 0, sizeof(data));
data.data[0] = 0x80 | enabled;
return sabi_command(samsung, commands->set_battery_life_extender,
&data, NULL);
}
static ssize_t get_battery_life_extender(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct samsung_laptop *samsung = dev_get_drvdata(dev);
int ret;
ret = read_battery_life_extender(samsung);
if (ret < 0)
return ret;
return sprintf(buf, "%d\n", ret);
}
static ssize_t set_battery_life_extender(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct samsung_laptop *samsung = dev_get_drvdata(dev);
int ret, value;
if (!count || sscanf(buf, "%i", &value) != 1)
return -EINVAL;
ret = write_battery_life_extender(samsung, !!value);
if (ret < 0)
return ret;
return count;
}
static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
get_battery_life_extender, set_battery_life_extender);
static int read_usb_charge(struct samsung_laptop *samsung)
{
const struct sabi_commands *commands = &samsung->config->commands;
struct sabi_data data;
int retval;
if (commands->get_usb_charge == 0xFFFF)
return -ENODEV;
memset(&data, 0, sizeof(data));
data.data[0] = 0x80;
retval = sabi_command(samsung, commands->get_usb_charge,
&data, &data);
if (retval)
return retval;
if (data.data[0] != 0 && data.data[0] != 1)
return -ENODEV;
return data.data[0];
}
static int write_usb_charge(struct samsung_laptop *samsung,
int enabled)
{
const struct sabi_commands *commands = &samsung->config->commands;
struct sabi_data data;
memset(&data, 0, sizeof(data));
data.data[0] = 0x80 | enabled;
return sabi_command(samsung, commands->set_usb_charge,
&data, NULL);
}
static ssize_t get_usb_charge(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct samsung_laptop *samsung = dev_get_drvdata(dev);
int ret;
ret = read_usb_charge(samsung);
if (ret < 0)
return ret;
return sprintf(buf, "%d\n", ret);
}
static ssize_t set_usb_charge(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct samsung_laptop *samsung = dev_get_drvdata(dev);
int ret, value;
if (!count || sscanf(buf, "%i", &value) != 1)
return -EINVAL;
ret = write_usb_charge(samsung, !!value);
if (ret < 0)
return ret;
return count;
}
static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
get_usb_charge, set_usb_charge);
static struct attribute *platform_attributes[] = {
&dev_attr_performance_level.attr,
&dev_attr_battery_life_extender.attr,
&dev_attr_usb_charge.attr,
NULL
};
static int find_signature(void __iomem *memcheck, const char *testStr)
{
int i = 0;
int loca;
for (loca = 0; loca < 0xffff; loca++) {
char temp = readb(memcheck + loca);
if (temp == testStr[i]) {
if (i == strlen(testStr)-1)
break;
++i;
} else {
i = 0;
}
}
return loca;
}
static void samsung_rfkill_exit(struct samsung_laptop *samsung)
{
if (samsung->wlan.rfkill) {
rfkill_unregister(samsung->wlan.rfkill);
rfkill_destroy(samsung->wlan.rfkill);
samsung->wlan.rfkill = NULL;
}
if (samsung->bluetooth.rfkill) {
rfkill_unregister(samsung->bluetooth.rfkill);
rfkill_destroy(samsung->bluetooth.rfkill);
samsung->bluetooth.rfkill = NULL;
}
}
static int samsung_new_rfkill(struct samsung_laptop *samsung,
struct samsung_rfkill *arfkill,
const char *name, enum rfkill_type type,
const struct rfkill_ops *ops,
int blocked)
{
struct rfkill **rfkill = &arfkill->rfkill;
int ret;
arfkill->type = type;
arfkill->samsung = samsung;
*rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
type, ops, arfkill);
if (!*rfkill)
return -EINVAL;
if (blocked != -1)
rfkill_init_sw_state(*rfkill, blocked);
ret = rfkill_register(*rfkill);
if (ret) {
rfkill_destroy(*rfkill);
*rfkill = NULL;
return ret;
}
return 0;
}
static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
{
return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
}
static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
{
struct sabi_data data;
int ret;
ret = swsmi_wireless_status(samsung, &data);
if (ret) {
/* Some swsmi laptops use the old seclinux way to control
* wireless devices */
if (ret == -EINVAL)
ret = samsung_rfkill_init_seclinux(samsung);
return ret;
}
/* 0x02 seems to mean that the device is no present/available */
if (data.data[WL_STATUS_WLAN] != 0x02)
ret = samsung_new_rfkill(samsung, &samsung->wlan,
"samsung-wlan",
RFKILL_TYPE_WLAN,
&swsmi_rfkill_ops,
!data.data[WL_STATUS_WLAN]);
if (ret)
goto exit;
if (data.data[WL_STATUS_BT] != 0x02)
ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
"samsung-bluetooth",
RFKILL_TYPE_BLUETOOTH,
&swsmi_rfkill_ops,
!data.data[WL_STATUS_BT]);
if (ret)
goto exit;
exit:
if (ret)
samsung_rfkill_exit(samsung);
return ret;
}
static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
{
if (samsung->config->sabi_version == 2)
return samsung_rfkill_init_seclinux(samsung);
if (samsung->config->sabi_version == 3)
return samsung_rfkill_init_swsmi(samsung);
return 0;
}
static int kbd_backlight_enable(struct samsung_laptop *samsung)
{
const struct sabi_commands *commands = &samsung->config->commands;
struct sabi_data data;
int retval;
if (commands->kbd_backlight == 0xFFFF)
return -ENODEV;
memset(&data, 0, sizeof(data));
data.d0 = 0xaabb;
retval = sabi_command(samsung, commands->kbd_backlight,
&data, &data);
if (retval)
return retval;
if (data.d0 != 0xccdd)
return -ENODEV;
return 0;
}
static int kbd_backlight_read(struct samsung_laptop *samsung)
{
const struct sabi_commands *commands = &samsung->config->commands;
struct sabi_data data;
int retval;
memset(&data, 0, sizeof(data));
data.data[0] = 0x81;
retval = sabi_command(samsung, commands->kbd_backlight,
&data, &data);
if (retval)
return retval;
return data.data[0];
}
static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
{
const struct sabi_commands *commands = &samsung->config->commands;
struct sabi_data data;
memset(&data, 0, sizeof(data));
data.d0 = 0x82 | ((brightness & 0xFF) << 8);
return sabi_command(samsung, commands->kbd_backlight,
&data, NULL);
}
static void kbd_led_update(struct work_struct *work)
{