Loading...
1/*
2 * Samsung Laptop driver
3 *
4 * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
5 * Copyright (C) 2009,2011 Novell Inc.
6 *
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.
10 *
11 */
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
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/fb.h>
21#include <linux/dmi.h>
22#include <linux/platform_device.h>
23#include <linux/rfkill.h>
24
25/*
26 * This driver is needed because a number of Samsung laptops do not hook
27 * their control settings through ACPI. So we have to poke around in the
28 * BIOS to do things like brightness values, and "special" key controls.
29 */
30
31/*
32 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
33 * be reserved by the BIOS (which really doesn't make much sense), we tell
34 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
35 */
36#define MAX_BRIGHT 0x07
37
38
39#define SABI_IFACE_MAIN 0x00
40#define SABI_IFACE_SUB 0x02
41#define SABI_IFACE_COMPLETE 0x04
42#define SABI_IFACE_DATA 0x05
43
44/* Structure to get data back to the calling function */
45struct sabi_retval {
46 u8 retval[20];
47};
48
49struct sabi_header_offsets {
50 u8 port;
51 u8 re_mem;
52 u8 iface_func;
53 u8 en_mem;
54 u8 data_offset;
55 u8 data_segment;
56};
57
58struct sabi_commands {
59 /*
60 * Brightness is 0 - 8, as described above.
61 * Value 0 is for the BIOS to use
62 */
63 u8 get_brightness;
64 u8 set_brightness;
65
66 /*
67 * first byte:
68 * 0x00 - wireless is off
69 * 0x01 - wireless is on
70 * second byte:
71 * 0x02 - 3G is off
72 * 0x03 - 3G is on
73 * TODO, verify 3G is correct, that doesn't seem right...
74 */
75 u8 get_wireless_button;
76 u8 set_wireless_button;
77
78 /* 0 is off, 1 is on */
79 u8 get_backlight;
80 u8 set_backlight;
81
82 /*
83 * 0x80 or 0x00 - no action
84 * 0x81 - recovery key pressed
85 */
86 u8 get_recovery_mode;
87 u8 set_recovery_mode;
88
89 /*
90 * on seclinux: 0 is low, 1 is high,
91 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
92 */
93 u8 get_performance_level;
94 u8 set_performance_level;
95
96 /*
97 * Tell the BIOS that Linux is running on this machine.
98 * 81 is on, 80 is off
99 */
100 u8 set_linux;
101};
102
103struct sabi_performance_level {
104 const char *name;
105 u8 value;
106};
107
108struct sabi_config {
109 const char *test_string;
110 u16 main_function;
111 const struct sabi_header_offsets header_offsets;
112 const struct sabi_commands commands;
113 const struct sabi_performance_level performance_levels[4];
114 u8 min_brightness;
115 u8 max_brightness;
116};
117
118static const struct sabi_config sabi_configs[] = {
119 {
120 .test_string = "SECLINUX",
121
122 .main_function = 0x4c49,
123
124 .header_offsets = {
125 .port = 0x00,
126 .re_mem = 0x02,
127 .iface_func = 0x03,
128 .en_mem = 0x04,
129 .data_offset = 0x05,
130 .data_segment = 0x07,
131 },
132
133 .commands = {
134 .get_brightness = 0x00,
135 .set_brightness = 0x01,
136
137 .get_wireless_button = 0x02,
138 .set_wireless_button = 0x03,
139
140 .get_backlight = 0x04,
141 .set_backlight = 0x05,
142
143 .get_recovery_mode = 0x06,
144 .set_recovery_mode = 0x07,
145
146 .get_performance_level = 0x08,
147 .set_performance_level = 0x09,
148
149 .set_linux = 0x0a,
150 },
151
152 .performance_levels = {
153 {
154 .name = "silent",
155 .value = 0,
156 },
157 {
158 .name = "normal",
159 .value = 1,
160 },
161 { },
162 },
163 .min_brightness = 1,
164 .max_brightness = 8,
165 },
166 {
167 .test_string = "SwSmi@",
168
169 .main_function = 0x5843,
170
171 .header_offsets = {
172 .port = 0x00,
173 .re_mem = 0x04,
174 .iface_func = 0x02,
175 .en_mem = 0x03,
176 .data_offset = 0x05,
177 .data_segment = 0x07,
178 },
179
180 .commands = {
181 .get_brightness = 0x10,
182 .set_brightness = 0x11,
183
184 .get_wireless_button = 0x12,
185 .set_wireless_button = 0x13,
186
187 .get_backlight = 0x2d,
188 .set_backlight = 0x2e,
189
190 .get_recovery_mode = 0xff,
191 .set_recovery_mode = 0xff,
192
193 .get_performance_level = 0x31,
194 .set_performance_level = 0x32,
195
196 .set_linux = 0xff,
197 },
198
199 .performance_levels = {
200 {
201 .name = "normal",
202 .value = 0,
203 },
204 {
205 .name = "silent",
206 .value = 1,
207 },
208 {
209 .name = "overclock",
210 .value = 2,
211 },
212 { },
213 },
214 .min_brightness = 0,
215 .max_brightness = 8,
216 },
217 { },
218};
219
220static const struct sabi_config *sabi_config;
221
222static void __iomem *sabi;
223static void __iomem *sabi_iface;
224static void __iomem *f0000_segment;
225static struct backlight_device *backlight_device;
226static struct mutex sabi_mutex;
227static struct platform_device *sdev;
228static struct rfkill *rfk;
229
230static int force;
231module_param(force, bool, 0);
232MODULE_PARM_DESC(force,
233 "Disable the DMI check and forces the driver to be loaded");
234
235static int debug;
236module_param(debug, bool, S_IRUGO | S_IWUSR);
237MODULE_PARM_DESC(debug, "Debug enabled or not");
238
239static int sabi_get_command(u8 command, struct sabi_retval *sretval)
240{
241 int retval = 0;
242 u16 port = readw(sabi + sabi_config->header_offsets.port);
243 u8 complete, iface_data;
244
245 mutex_lock(&sabi_mutex);
246
247 /* enable memory to be able to write to it */
248 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
249
250 /* write out the command */
251 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
252 writew(command, sabi_iface + SABI_IFACE_SUB);
253 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
254 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
255
256 /* write protect memory to make it safe */
257 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
258
259 /* see if the command actually succeeded */
260 complete = readb(sabi_iface + SABI_IFACE_COMPLETE);
261 iface_data = readb(sabi_iface + SABI_IFACE_DATA);
262 if (complete != 0xaa || iface_data == 0xff) {
263 pr_warn("SABI get command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
264 command, complete, iface_data);
265 retval = -EINVAL;
266 goto exit;
267 }
268 /*
269 * Save off the data into a structure so the caller use it.
270 * Right now we only want the first 4 bytes,
271 * There are commands that need more, but not for the ones we
272 * currently care about.
273 */
274 sretval->retval[0] = readb(sabi_iface + SABI_IFACE_DATA);
275 sretval->retval[1] = readb(sabi_iface + SABI_IFACE_DATA + 1);
276 sretval->retval[2] = readb(sabi_iface + SABI_IFACE_DATA + 2);
277 sretval->retval[3] = readb(sabi_iface + SABI_IFACE_DATA + 3);
278
279exit:
280 mutex_unlock(&sabi_mutex);
281 return retval;
282
283}
284
285static int sabi_set_command(u8 command, u8 data)
286{
287 int retval = 0;
288 u16 port = readw(sabi + sabi_config->header_offsets.port);
289 u8 complete, iface_data;
290
291 mutex_lock(&sabi_mutex);
292
293 /* enable memory to be able to write to it */
294 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
295
296 /* write out the command */
297 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
298 writew(command, sabi_iface + SABI_IFACE_SUB);
299 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
300 writeb(data, sabi_iface + SABI_IFACE_DATA);
301 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
302
303 /* write protect memory to make it safe */
304 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
305
306 /* see if the command actually succeeded */
307 complete = readb(sabi_iface + SABI_IFACE_COMPLETE);
308 iface_data = readb(sabi_iface + SABI_IFACE_DATA);
309 if (complete != 0xaa || iface_data == 0xff) {
310 pr_warn("SABI set command 0x%02x failed with completion flag 0x%02x and data 0x%02x\n",
311 command, complete, iface_data);
312 retval = -EINVAL;
313 }
314
315 mutex_unlock(&sabi_mutex);
316 return retval;
317}
318
319static void test_backlight(void)
320{
321 struct sabi_retval sretval;
322
323 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
324 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
325
326 sabi_set_command(sabi_config->commands.set_backlight, 0);
327 printk(KERN_DEBUG "backlight should be off\n");
328
329 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
330 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
331
332 msleep(1000);
333
334 sabi_set_command(sabi_config->commands.set_backlight, 1);
335 printk(KERN_DEBUG "backlight should be on\n");
336
337 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
338 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
339}
340
341static void test_wireless(void)
342{
343 struct sabi_retval sretval;
344
345 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
346 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
347
348 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
349 printk(KERN_DEBUG "wireless led should be off\n");
350
351 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
352 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
353
354 msleep(1000);
355
356 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
357 printk(KERN_DEBUG "wireless led should be on\n");
358
359 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
360 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
361}
362
363static u8 read_brightness(void)
364{
365 struct sabi_retval sretval;
366 int user_brightness = 0;
367 int retval;
368
369 retval = sabi_get_command(sabi_config->commands.get_brightness,
370 &sretval);
371 if (!retval) {
372 user_brightness = sretval.retval[0];
373 if (user_brightness != 0)
374 user_brightness -= sabi_config->min_brightness;
375 }
376 return user_brightness;
377}
378
379static void set_brightness(u8 user_brightness)
380{
381 u8 user_level = user_brightness - sabi_config->min_brightness;
382
383 sabi_set_command(sabi_config->commands.set_brightness, user_level);
384}
385
386static int get_brightness(struct backlight_device *bd)
387{
388 return (int)read_brightness();
389}
390
391static int update_status(struct backlight_device *bd)
392{
393 set_brightness(bd->props.brightness);
394
395 if (bd->props.power == FB_BLANK_UNBLANK)
396 sabi_set_command(sabi_config->commands.set_backlight, 1);
397 else
398 sabi_set_command(sabi_config->commands.set_backlight, 0);
399 return 0;
400}
401
402static const struct backlight_ops backlight_ops = {
403 .get_brightness = get_brightness,
404 .update_status = update_status,
405};
406
407static int rfkill_set(void *data, bool blocked)
408{
409 /* Do something with blocked...*/
410 /*
411 * blocked == false is on
412 * blocked == true is off
413 */
414 if (blocked)
415 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
416 else
417 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
418
419 return 0;
420}
421
422static struct rfkill_ops rfkill_ops = {
423 .set_block = rfkill_set,
424};
425
426static int init_wireless(struct platform_device *sdev)
427{
428 int retval;
429
430 rfk = rfkill_alloc("samsung-wifi", &sdev->dev, RFKILL_TYPE_WLAN,
431 &rfkill_ops, NULL);
432 if (!rfk)
433 return -ENOMEM;
434
435 retval = rfkill_register(rfk);
436 if (retval) {
437 rfkill_destroy(rfk);
438 return -ENODEV;
439 }
440
441 return 0;
442}
443
444static void destroy_wireless(void)
445{
446 rfkill_unregister(rfk);
447 rfkill_destroy(rfk);
448}
449
450static ssize_t get_performance_level(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
453 struct sabi_retval sretval;
454 int retval;
455 int i;
456
457 /* Read the state */
458 retval = sabi_get_command(sabi_config->commands.get_performance_level,
459 &sretval);
460 if (retval)
461 return retval;
462
463 /* The logic is backwards, yeah, lots of fun... */
464 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
465 if (sretval.retval[0] == sabi_config->performance_levels[i].value)
466 return sprintf(buf, "%s\n", sabi_config->performance_levels[i].name);
467 }
468 return sprintf(buf, "%s\n", "unknown");
469}
470
471static ssize_t set_performance_level(struct device *dev,
472 struct device_attribute *attr, const char *buf,
473 size_t count)
474{
475 if (count >= 1) {
476 int i;
477 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
478 const struct sabi_performance_level *level =
479 &sabi_config->performance_levels[i];
480 if (!strncasecmp(level->name, buf, strlen(level->name))) {
481 sabi_set_command(sabi_config->commands.set_performance_level,
482 level->value);
483 break;
484 }
485 }
486 if (!sabi_config->performance_levels[i].name)
487 return -EINVAL;
488 }
489 return count;
490}
491static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
492 get_performance_level, set_performance_level);
493
494
495static int __init dmi_check_cb(const struct dmi_system_id *id)
496{
497 pr_info("found laptop model '%s'\n",
498 id->ident);
499 return 1;
500}
501
502static struct dmi_system_id __initdata samsung_dmi_table[] = {
503 {
504 .ident = "N128",
505 .matches = {
506 DMI_MATCH(DMI_SYS_VENDOR,
507 "SAMSUNG ELECTRONICS CO., LTD."),
508 DMI_MATCH(DMI_PRODUCT_NAME, "N128"),
509 DMI_MATCH(DMI_BOARD_NAME, "N128"),
510 },
511 .callback = dmi_check_cb,
512 },
513 {
514 .ident = "N130",
515 .matches = {
516 DMI_MATCH(DMI_SYS_VENDOR,
517 "SAMSUNG ELECTRONICS CO., LTD."),
518 DMI_MATCH(DMI_PRODUCT_NAME, "N130"),
519 DMI_MATCH(DMI_BOARD_NAME, "N130"),
520 },
521 .callback = dmi_check_cb,
522 },
523 {
524 .ident = "N510",
525 .matches = {
526 DMI_MATCH(DMI_SYS_VENDOR,
527 "SAMSUNG ELECTRONICS CO., LTD."),
528 DMI_MATCH(DMI_PRODUCT_NAME, "N510"),
529 DMI_MATCH(DMI_BOARD_NAME, "N510"),
530 },
531 .callback = dmi_check_cb,
532 },
533 {
534 .ident = "X125",
535 .matches = {
536 DMI_MATCH(DMI_SYS_VENDOR,
537 "SAMSUNG ELECTRONICS CO., LTD."),
538 DMI_MATCH(DMI_PRODUCT_NAME, "X125"),
539 DMI_MATCH(DMI_BOARD_NAME, "X125"),
540 },
541 .callback = dmi_check_cb,
542 },
543 {
544 .ident = "X120/X170",
545 .matches = {
546 DMI_MATCH(DMI_SYS_VENDOR,
547 "SAMSUNG ELECTRONICS CO., LTD."),
548 DMI_MATCH(DMI_PRODUCT_NAME, "X120/X170"),
549 DMI_MATCH(DMI_BOARD_NAME, "X120/X170"),
550 },
551 .callback = dmi_check_cb,
552 },
553 {
554 .ident = "NC10",
555 .matches = {
556 DMI_MATCH(DMI_SYS_VENDOR,
557 "SAMSUNG ELECTRONICS CO., LTD."),
558 DMI_MATCH(DMI_PRODUCT_NAME, "NC10"),
559 DMI_MATCH(DMI_BOARD_NAME, "NC10"),
560 },
561 .callback = dmi_check_cb,
562 },
563 {
564 .ident = "NP-Q45",
565 .matches = {
566 DMI_MATCH(DMI_SYS_VENDOR,
567 "SAMSUNG ELECTRONICS CO., LTD."),
568 DMI_MATCH(DMI_PRODUCT_NAME, "SQ45S70S"),
569 DMI_MATCH(DMI_BOARD_NAME, "SQ45S70S"),
570 },
571 .callback = dmi_check_cb,
572 },
573 {
574 .ident = "X360",
575 .matches = {
576 DMI_MATCH(DMI_SYS_VENDOR,
577 "SAMSUNG ELECTRONICS CO., LTD."),
578 DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
579 DMI_MATCH(DMI_BOARD_NAME, "X360"),
580 },
581 .callback = dmi_check_cb,
582 },
583 {
584 .ident = "R410 Plus",
585 .matches = {
586 DMI_MATCH(DMI_SYS_VENDOR,
587 "SAMSUNG ELECTRONICS CO., LTD."),
588 DMI_MATCH(DMI_PRODUCT_NAME, "R410P"),
589 DMI_MATCH(DMI_BOARD_NAME, "R460"),
590 },
591 .callback = dmi_check_cb,
592 },
593 {
594 .ident = "R518",
595 .matches = {
596 DMI_MATCH(DMI_SYS_VENDOR,
597 "SAMSUNG ELECTRONICS CO., LTD."),
598 DMI_MATCH(DMI_PRODUCT_NAME, "R518"),
599 DMI_MATCH(DMI_BOARD_NAME, "R518"),
600 },
601 .callback = dmi_check_cb,
602 },
603 {
604 .ident = "R519/R719",
605 .matches = {
606 DMI_MATCH(DMI_SYS_VENDOR,
607 "SAMSUNG ELECTRONICS CO., LTD."),
608 DMI_MATCH(DMI_PRODUCT_NAME, "R519/R719"),
609 DMI_MATCH(DMI_BOARD_NAME, "R519/R719"),
610 },
611 .callback = dmi_check_cb,
612 },
613 {
614 .ident = "N150/N210/N220",
615 .matches = {
616 DMI_MATCH(DMI_SYS_VENDOR,
617 "SAMSUNG ELECTRONICS CO., LTD."),
618 DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220"),
619 DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220"),
620 },
621 .callback = dmi_check_cb,
622 },
623 {
624 .ident = "N150/N210/N220/N230",
625 .matches = {
626 DMI_MATCH(DMI_SYS_VENDOR,
627 "SAMSUNG ELECTRONICS CO., LTD."),
628 DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220/N230"),
629 DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220/N230"),
630 },
631 .callback = dmi_check_cb,
632 },
633 {
634 .ident = "N150P/N210P/N220P",
635 .matches = {
636 DMI_MATCH(DMI_SYS_VENDOR,
637 "SAMSUNG ELECTRONICS CO., LTD."),
638 DMI_MATCH(DMI_PRODUCT_NAME, "N150P/N210P/N220P"),
639 DMI_MATCH(DMI_BOARD_NAME, "N150P/N210P/N220P"),
640 },
641 .callback = dmi_check_cb,
642 },
643 {
644 .ident = "R530/R730",
645 .matches = {
646 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
647 DMI_MATCH(DMI_PRODUCT_NAME, "R530/R730"),
648 DMI_MATCH(DMI_BOARD_NAME, "R530/R730"),
649 },
650 .callback = dmi_check_cb,
651 },
652 {
653 .ident = "NF110/NF210/NF310",
654 .matches = {
655 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
656 DMI_MATCH(DMI_PRODUCT_NAME, "NF110/NF210/NF310"),
657 DMI_MATCH(DMI_BOARD_NAME, "NF110/NF210/NF310"),
658 },
659 .callback = dmi_check_cb,
660 },
661 {
662 .ident = "N145P/N250P/N260P",
663 .matches = {
664 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
665 DMI_MATCH(DMI_PRODUCT_NAME, "N145P/N250P/N260P"),
666 DMI_MATCH(DMI_BOARD_NAME, "N145P/N250P/N260P"),
667 },
668 .callback = dmi_check_cb,
669 },
670 {
671 .ident = "R70/R71",
672 .matches = {
673 DMI_MATCH(DMI_SYS_VENDOR,
674 "SAMSUNG ELECTRONICS CO., LTD."),
675 DMI_MATCH(DMI_PRODUCT_NAME, "R70/R71"),
676 DMI_MATCH(DMI_BOARD_NAME, "R70/R71"),
677 },
678 .callback = dmi_check_cb,
679 },
680 {
681 .ident = "P460",
682 .matches = {
683 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
684 DMI_MATCH(DMI_PRODUCT_NAME, "P460"),
685 DMI_MATCH(DMI_BOARD_NAME, "P460"),
686 },
687 .callback = dmi_check_cb,
688 },
689 { },
690};
691MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
692
693static int find_signature(void __iomem *memcheck, const char *testStr)
694{
695 int i = 0;
696 int loca;
697
698 for (loca = 0; loca < 0xffff; loca++) {
699 char temp = readb(memcheck + loca);
700
701 if (temp == testStr[i]) {
702 if (i == strlen(testStr)-1)
703 break;
704 ++i;
705 } else {
706 i = 0;
707 }
708 }
709 return loca;
710}
711
712static int __init samsung_init(void)
713{
714 struct backlight_properties props;
715 struct sabi_retval sretval;
716 unsigned int ifaceP;
717 int i;
718 int loca;
719 int retval;
720
721 mutex_init(&sabi_mutex);
722
723 if (!force && !dmi_check_system(samsung_dmi_table))
724 return -ENODEV;
725
726 f0000_segment = ioremap_nocache(0xf0000, 0xffff);
727 if (!f0000_segment) {
728 pr_err("Can't map the segment at 0xf0000\n");
729 return -EINVAL;
730 }
731
732 /* Try to find one of the signatures in memory to find the header */
733 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
734 sabi_config = &sabi_configs[i];
735 loca = find_signature(f0000_segment, sabi_config->test_string);
736 if (loca != 0xffff)
737 break;
738 }
739
740 if (loca == 0xffff) {
741 pr_err("This computer does not support SABI\n");
742 goto error_no_signature;
743 }
744
745 /* point to the SMI port Number */
746 loca += 1;
747 sabi = (f0000_segment + loca);
748
749 if (debug) {
750 printk(KERN_DEBUG "This computer supports SABI==%x\n",
751 loca + 0xf0000 - 6);
752 printk(KERN_DEBUG "SABI header:\n");
753 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
754 readw(sabi + sabi_config->header_offsets.port));
755 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
756 readb(sabi + sabi_config->header_offsets.iface_func));
757 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
758 readb(sabi + sabi_config->header_offsets.en_mem));
759 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
760 readb(sabi + sabi_config->header_offsets.re_mem));
761 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
762 readw(sabi + sabi_config->header_offsets.data_offset));
763 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
764 readw(sabi + sabi_config->header_offsets.data_segment));
765 }
766
767 /* Get a pointer to the SABI Interface */
768 ifaceP = (readw(sabi + sabi_config->header_offsets.data_segment) & 0x0ffff) << 4;
769 ifaceP += readw(sabi + sabi_config->header_offsets.data_offset) & 0x0ffff;
770 sabi_iface = ioremap_nocache(ifaceP, 16);
771 if (!sabi_iface) {
772 pr_err("Can't remap %x\n", ifaceP);
773 goto exit;
774 }
775 if (debug) {
776 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
777 printk(KERN_DEBUG "sabi_iface = %p\n", sabi_iface);
778
779 test_backlight();
780 test_wireless();
781
782 retval = sabi_get_command(sabi_config->commands.get_brightness,
783 &sretval);
784 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]);
785 }
786
787 /* Turn on "Linux" mode in the BIOS */
788 if (sabi_config->commands.set_linux != 0xff) {
789 retval = sabi_set_command(sabi_config->commands.set_linux,
790 0x81);
791 if (retval) {
792 pr_warn("Linux mode was not set!\n");
793 goto error_no_platform;
794 }
795 }
796
797 /* knock up a platform device to hang stuff off of */
798 sdev = platform_device_register_simple("samsung", -1, NULL, 0);
799 if (IS_ERR(sdev))
800 goto error_no_platform;
801
802 /* create a backlight device to talk to this one */
803 memset(&props, 0, sizeof(struct backlight_properties));
804 props.type = BACKLIGHT_PLATFORM;
805 props.max_brightness = sabi_config->max_brightness;
806 backlight_device = backlight_device_register("samsung", &sdev->dev,
807 NULL, &backlight_ops,
808 &props);
809 if (IS_ERR(backlight_device))
810 goto error_no_backlight;
811
812 backlight_device->props.brightness = read_brightness();
813 backlight_device->props.power = FB_BLANK_UNBLANK;
814 backlight_update_status(backlight_device);
815
816 retval = init_wireless(sdev);
817 if (retval)
818 goto error_no_rfk;
819
820 retval = device_create_file(&sdev->dev, &dev_attr_performance_level);
821 if (retval)
822 goto error_file_create;
823
824exit:
825 return 0;
826
827error_file_create:
828 destroy_wireless();
829
830error_no_rfk:
831 backlight_device_unregister(backlight_device);
832
833error_no_backlight:
834 platform_device_unregister(sdev);
835
836error_no_platform:
837 iounmap(sabi_iface);
838
839error_no_signature:
840 iounmap(f0000_segment);
841 return -EINVAL;
842}
843
844static void __exit samsung_exit(void)
845{
846 /* Turn off "Linux" mode in the BIOS */
847 if (sabi_config->commands.set_linux != 0xff)
848 sabi_set_command(sabi_config->commands.set_linux, 0x80);
849
850 device_remove_file(&sdev->dev, &dev_attr_performance_level);
851 backlight_device_unregister(backlight_device);
852 destroy_wireless();
853 iounmap(sabi_iface);
854 iounmap(f0000_segment);
855 platform_device_unregister(sdev);
856}
857
858module_init(samsung_init);
859module_exit(samsung_exit);
860
861MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
862MODULE_DESCRIPTION("Samsung Backlight driver");
863MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Samsung Laptop driver
4 *
5 * Copyright (C) 2009,2011 Greg Kroah-Hartman (gregkh@suse.de)
6 * Copyright (C) 2009,2011 Novell Inc.
7 */
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/delay.h>
14#include <linux/pci.h>
15#include <linux/backlight.h>
16#include <linux/leds.h>
17#include <linux/fb.h>
18#include <linux/dmi.h>
19#include <linux/platform_device.h>
20#include <linux/rfkill.h>
21#include <linux/acpi.h>
22#include <linux/seq_file.h>
23#include <linux/debugfs.h>
24#include <linux/ctype.h>
25#include <linux/efi.h>
26#include <linux/suspend.h>
27#include <acpi/video.h>
28
29/*
30 * This driver is needed because a number of Samsung laptops do not hook
31 * their control settings through ACPI. So we have to poke around in the
32 * BIOS to do things like brightness values, and "special" key controls.
33 */
34
35/*
36 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
37 * be reserved by the BIOS (which really doesn't make much sense), we tell
38 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
39 */
40#define MAX_BRIGHT 0x07
41
42
43#define SABI_IFACE_MAIN 0x00
44#define SABI_IFACE_SUB 0x02
45#define SABI_IFACE_COMPLETE 0x04
46#define SABI_IFACE_DATA 0x05
47
48#define WL_STATUS_WLAN 0x0
49#define WL_STATUS_BT 0x2
50
51/* Structure get/set data using sabi */
52struct sabi_data {
53 union {
54 struct {
55 u32 d0;
56 u32 d1;
57 u16 d2;
58 u8 d3;
59 };
60 u8 data[11];
61 };
62};
63
64struct sabi_header_offsets {
65 u8 port;
66 u8 re_mem;
67 u8 iface_func;
68 u8 en_mem;
69 u8 data_offset;
70 u8 data_segment;
71};
72
73struct sabi_commands {
74 /*
75 * Brightness is 0 - 8, as described above.
76 * Value 0 is for the BIOS to use
77 */
78 u16 get_brightness;
79 u16 set_brightness;
80
81 /*
82 * first byte:
83 * 0x00 - wireless is off
84 * 0x01 - wireless is on
85 * second byte:
86 * 0x02 - 3G is off
87 * 0x03 - 3G is on
88 * TODO, verify 3G is correct, that doesn't seem right...
89 */
90 u16 get_wireless_button;
91 u16 set_wireless_button;
92
93 /* 0 is off, 1 is on */
94 u16 get_backlight;
95 u16 set_backlight;
96
97 /*
98 * 0x80 or 0x00 - no action
99 * 0x81 - recovery key pressed
100 */
101 u16 get_recovery_mode;
102 u16 set_recovery_mode;
103
104 /*
105 * on seclinux: 0 is low, 1 is high,
106 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
107 */
108 u16 get_performance_level;
109 u16 set_performance_level;
110
111 /* 0x80 is off, 0x81 is on */
112 u16 get_battery_life_extender;
113 u16 set_battery_life_extender;
114
115 /* 0x80 is off, 0x81 is on */
116 u16 get_usb_charge;
117 u16 set_usb_charge;
118
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
123 /* 0x80 is off, 0x81 is on */
124 u16 get_lid_handling;
125 u16 set_lid_handling;
126
127 /* 0x81 to read, (0x82 | level << 8) to set, 0xaabb to enable */
128 u16 kbd_backlight;
129
130 /*
131 * Tell the BIOS that Linux is running on this machine.
132 * 81 is on, 80 is off
133 */
134 u16 set_linux;
135};
136
137struct sabi_performance_level {
138 const char *name;
139 u16 value;
140};
141
142struct sabi_config {
143 int sabi_version;
144 const char *test_string;
145 u16 main_function;
146 const struct sabi_header_offsets header_offsets;
147 const struct sabi_commands commands;
148 const struct sabi_performance_level performance_levels[4];
149 u8 min_brightness;
150 u8 max_brightness;
151};
152
153static const struct sabi_config sabi_configs[] = {
154 {
155 /* I don't know if it is really 2, but it it is
156 * less than 3 anyway */
157 .sabi_version = 2,
158
159 .test_string = "SECLINUX",
160
161 .main_function = 0x4c49,
162
163 .header_offsets = {
164 .port = 0x00,
165 .re_mem = 0x02,
166 .iface_func = 0x03,
167 .en_mem = 0x04,
168 .data_offset = 0x05,
169 .data_segment = 0x07,
170 },
171
172 .commands = {
173 .get_brightness = 0x00,
174 .set_brightness = 0x01,
175
176 .get_wireless_button = 0x02,
177 .set_wireless_button = 0x03,
178
179 .get_backlight = 0x04,
180 .set_backlight = 0x05,
181
182 .get_recovery_mode = 0x06,
183 .set_recovery_mode = 0x07,
184
185 .get_performance_level = 0x08,
186 .set_performance_level = 0x09,
187
188 .get_battery_life_extender = 0xFFFF,
189 .set_battery_life_extender = 0xFFFF,
190
191 .get_usb_charge = 0xFFFF,
192 .set_usb_charge = 0xFFFF,
193
194 .get_wireless_status = 0xFFFF,
195 .set_wireless_status = 0xFFFF,
196
197 .get_lid_handling = 0xFFFF,
198 .set_lid_handling = 0xFFFF,
199
200 .kbd_backlight = 0xFFFF,
201
202 .set_linux = 0x0a,
203 },
204
205 .performance_levels = {
206 {
207 .name = "silent",
208 .value = 0,
209 },
210 {
211 .name = "normal",
212 .value = 1,
213 },
214 { },
215 },
216 .min_brightness = 1,
217 .max_brightness = 8,
218 },
219 {
220 .sabi_version = 3,
221
222 .test_string = "SwSmi@",
223
224 .main_function = 0x5843,
225
226 .header_offsets = {
227 .port = 0x00,
228 .re_mem = 0x04,
229 .iface_func = 0x02,
230 .en_mem = 0x03,
231 .data_offset = 0x05,
232 .data_segment = 0x07,
233 },
234
235 .commands = {
236 .get_brightness = 0x10,
237 .set_brightness = 0x11,
238
239 .get_wireless_button = 0x12,
240 .set_wireless_button = 0x13,
241
242 .get_backlight = 0x2d,
243 .set_backlight = 0x2e,
244
245 .get_recovery_mode = 0xff,
246 .set_recovery_mode = 0xff,
247
248 .get_performance_level = 0x31,
249 .set_performance_level = 0x32,
250
251 .get_battery_life_extender = 0x65,
252 .set_battery_life_extender = 0x66,
253
254 .get_usb_charge = 0x67,
255 .set_usb_charge = 0x68,
256
257 .get_wireless_status = 0x69,
258 .set_wireless_status = 0x6a,
259
260 .get_lid_handling = 0x6d,
261 .set_lid_handling = 0x6e,
262
263 .kbd_backlight = 0x78,
264
265 .set_linux = 0xff,
266 },
267
268 .performance_levels = {
269 {
270 .name = "normal",
271 .value = 0,
272 },
273 {
274 .name = "silent",
275 .value = 1,
276 },
277 {
278 .name = "overclock",
279 .value = 2,
280 },
281 { },
282 },
283 .min_brightness = 0,
284 .max_brightness = 8,
285 },
286 { },
287};
288
289/*
290 * samsung-laptop/ - debugfs root directory
291 * f0000_segment - dump f0000 segment
292 * command - current command
293 * data - current data
294 * d0, d1, d2, d3 - data fields
295 * call - call SABI using command and data
296 *
297 * This allow to call arbitrary sabi commands wihout
298 * modifying the driver at all.
299 * For example, setting the keyboard backlight brightness to 5
300 *
301 * echo 0x78 > command
302 * echo 0x0582 > d0
303 * echo 0 > d1
304 * echo 0 > d2
305 * echo 0 > d3
306 * cat call
307 */
308
309struct samsung_laptop_debug {
310 struct dentry *root;
311 struct sabi_data data;
312 u16 command;
313
314 struct debugfs_blob_wrapper f0000_wrapper;
315 struct debugfs_blob_wrapper data_wrapper;
316 struct debugfs_blob_wrapper sdiag_wrapper;
317};
318
319struct samsung_laptop;
320
321struct samsung_rfkill {
322 struct samsung_laptop *samsung;
323 struct rfkill *rfkill;
324 enum rfkill_type type;
325};
326
327struct samsung_laptop {
328 const struct sabi_config *config;
329
330 void __iomem *sabi;
331 void __iomem *sabi_iface;
332 void __iomem *f0000_segment;
333
334 struct mutex sabi_mutex;
335
336 struct platform_device *platform_device;
337 struct backlight_device *backlight_device;
338
339 struct samsung_rfkill wlan;
340 struct samsung_rfkill bluetooth;
341
342 struct led_classdev kbd_led;
343 int kbd_led_wk;
344 struct workqueue_struct *led_workqueue;
345 struct work_struct kbd_led_work;
346
347 struct samsung_laptop_debug debug;
348 struct samsung_quirks *quirks;
349
350 struct notifier_block pm_nb;
351
352 bool handle_backlight;
353 bool has_stepping_quirk;
354
355 char sdiag[64];
356};
357
358struct samsung_quirks {
359 bool broken_acpi_video;
360 bool four_kbd_backlight_levels;
361 bool enable_kbd_backlight;
362 bool use_native_backlight;
363 bool lid_handling;
364};
365
366static struct samsung_quirks samsung_unknown = {};
367
368static struct samsung_quirks samsung_broken_acpi_video = {
369 .broken_acpi_video = true,
370};
371
372static struct samsung_quirks samsung_use_native_backlight = {
373 .use_native_backlight = true,
374};
375
376static struct samsung_quirks samsung_np740u3e = {
377 .four_kbd_backlight_levels = true,
378 .enable_kbd_backlight = true,
379};
380
381static struct samsung_quirks samsung_lid_handling = {
382 .lid_handling = true,
383};
384
385static bool force;
386module_param(force, bool, 0);
387MODULE_PARM_DESC(force,
388 "Disable the DMI check and forces the driver to be loaded");
389
390static bool debug;
391module_param(debug, bool, S_IRUGO | S_IWUSR);
392MODULE_PARM_DESC(debug, "Debug enabled or not");
393
394static int sabi_command(struct samsung_laptop *samsung, u16 command,
395 struct sabi_data *in,
396 struct sabi_data *out)
397{
398 const struct sabi_config *config = samsung->config;
399 int ret = 0;
400 u16 port = readw(samsung->sabi + config->header_offsets.port);
401 u8 complete, iface_data;
402
403 mutex_lock(&samsung->sabi_mutex);
404
405 if (debug) {
406 if (in)
407 pr_info("SABI command:0x%04x "
408 "data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
409 command, in->d0, in->d1, in->d2, in->d3);
410 else
411 pr_info("SABI command:0x%04x", command);
412 }
413
414 /* enable memory to be able to write to it */
415 outb(readb(samsung->sabi + config->header_offsets.en_mem), port);
416
417 /* write out the command */
418 writew(config->main_function, samsung->sabi_iface + SABI_IFACE_MAIN);
419 writew(command, samsung->sabi_iface + SABI_IFACE_SUB);
420 writeb(0, samsung->sabi_iface + SABI_IFACE_COMPLETE);
421 if (in) {
422 writel(in->d0, samsung->sabi_iface + SABI_IFACE_DATA);
423 writel(in->d1, samsung->sabi_iface + SABI_IFACE_DATA + 4);
424 writew(in->d2, samsung->sabi_iface + SABI_IFACE_DATA + 8);
425 writeb(in->d3, samsung->sabi_iface + SABI_IFACE_DATA + 10);
426 }
427 outb(readb(samsung->sabi + config->header_offsets.iface_func), port);
428
429 /* write protect memory to make it safe */
430 outb(readb(samsung->sabi + config->header_offsets.re_mem), port);
431
432 /* see if the command actually succeeded */
433 complete = readb(samsung->sabi_iface + SABI_IFACE_COMPLETE);
434 iface_data = readb(samsung->sabi_iface + SABI_IFACE_DATA);
435
436 /* iface_data = 0xFF happens when a command is not known
437 * so we only add a warning in debug mode since we will
438 * probably issue some unknown command at startup to find
439 * out which features are supported */
440 if (complete != 0xaa || (iface_data == 0xff && debug))
441 pr_warn("SABI command 0x%04x failed with"
442 " completion flag 0x%02x and interface data 0x%02x",
443 command, complete, iface_data);
444
445 if (complete != 0xaa || iface_data == 0xff) {
446 ret = -EINVAL;
447 goto exit;
448 }
449
450 if (out) {
451 out->d0 = readl(samsung->sabi_iface + SABI_IFACE_DATA);
452 out->d1 = readl(samsung->sabi_iface + SABI_IFACE_DATA + 4);
453 out->d2 = readw(samsung->sabi_iface + SABI_IFACE_DATA + 2);
454 out->d3 = readb(samsung->sabi_iface + SABI_IFACE_DATA + 1);
455 }
456
457 if (debug && out) {
458 pr_info("SABI return data:{0x%08x, 0x%08x, 0x%04x, 0x%02x}",
459 out->d0, out->d1, out->d2, out->d3);
460 }
461
462exit:
463 mutex_unlock(&samsung->sabi_mutex);
464 return ret;
465}
466
467/* simple wrappers usable with most commands */
468static int sabi_set_commandb(struct samsung_laptop *samsung,
469 u16 command, u8 data)
470{
471 struct sabi_data in = { { { .d0 = 0, .d1 = 0, .d2 = 0, .d3 = 0 } } };
472
473 in.data[0] = data;
474 return sabi_command(samsung, command, &in, NULL);
475}
476
477static int read_brightness(struct samsung_laptop *samsung)
478{
479 const struct sabi_config *config = samsung->config;
480 const struct sabi_commands *commands = &samsung->config->commands;
481 struct sabi_data sretval;
482 int user_brightness = 0;
483 int retval;
484
485 retval = sabi_command(samsung, commands->get_brightness,
486 NULL, &sretval);
487 if (retval)
488 return retval;
489
490 user_brightness = sretval.data[0];
491 if (user_brightness > config->min_brightness)
492 user_brightness -= config->min_brightness;
493 else
494 user_brightness = 0;
495
496 return user_brightness;
497}
498
499static void set_brightness(struct samsung_laptop *samsung, u8 user_brightness)
500{
501 const struct sabi_config *config = samsung->config;
502 const struct sabi_commands *commands = &samsung->config->commands;
503 u8 user_level = user_brightness + config->min_brightness;
504
505 if (samsung->has_stepping_quirk && user_level != 0) {
506 /*
507 * short circuit if the specified level is what's already set
508 * to prevent the screen from flickering needlessly
509 */
510 if (user_brightness == read_brightness(samsung))
511 return;
512
513 sabi_set_commandb(samsung, commands->set_brightness, 0);
514 }
515
516 sabi_set_commandb(samsung, commands->set_brightness, user_level);
517}
518
519static int get_brightness(struct backlight_device *bd)
520{
521 struct samsung_laptop *samsung = bl_get_data(bd);
522
523 return read_brightness(samsung);
524}
525
526static void check_for_stepping_quirk(struct samsung_laptop *samsung)
527{
528 int initial_level;
529 int check_level;
530 int orig_level = read_brightness(samsung);
531
532 /*
533 * Some laptops exhibit the strange behaviour of stepping toward
534 * (rather than setting) the brightness except when changing to/from
535 * brightness level 0. This behaviour is checked for here and worked
536 * around in set_brightness.
537 */
538
539 if (orig_level == 0)
540 set_brightness(samsung, 1);
541
542 initial_level = read_brightness(samsung);
543
544 if (initial_level <= 2)
545 check_level = initial_level + 2;
546 else
547 check_level = initial_level - 2;
548
549 samsung->has_stepping_quirk = false;
550 set_brightness(samsung, check_level);
551
552 if (read_brightness(samsung) != check_level) {
553 samsung->has_stepping_quirk = true;
554 pr_info("enabled workaround for brightness stepping quirk\n");
555 }
556
557 set_brightness(samsung, orig_level);
558}
559
560static int update_status(struct backlight_device *bd)
561{
562 struct samsung_laptop *samsung = bl_get_data(bd);
563 const struct sabi_commands *commands = &samsung->config->commands;
564
565 set_brightness(samsung, bd->props.brightness);
566
567 if (bd->props.power == FB_BLANK_UNBLANK)
568 sabi_set_commandb(samsung, commands->set_backlight, 1);
569 else
570 sabi_set_commandb(samsung, commands->set_backlight, 0);
571
572 return 0;
573}
574
575static const struct backlight_ops backlight_ops = {
576 .get_brightness = get_brightness,
577 .update_status = update_status,
578};
579
580static int seclinux_rfkill_set(void *data, bool blocked)
581{
582 struct samsung_rfkill *srfkill = data;
583 struct samsung_laptop *samsung = srfkill->samsung;
584 const struct sabi_commands *commands = &samsung->config->commands;
585
586 return sabi_set_commandb(samsung, commands->set_wireless_button,
587 !blocked);
588}
589
590static const struct rfkill_ops seclinux_rfkill_ops = {
591 .set_block = seclinux_rfkill_set,
592};
593
594static int swsmi_wireless_status(struct samsung_laptop *samsung,
595 struct sabi_data *data)
596{
597 const struct sabi_commands *commands = &samsung->config->commands;
598
599 return sabi_command(samsung, commands->get_wireless_status,
600 NULL, data);
601}
602
603static int swsmi_rfkill_set(void *priv, bool blocked)
604{
605 struct samsung_rfkill *srfkill = priv;
606 struct samsung_laptop *samsung = srfkill->samsung;
607 const struct sabi_commands *commands = &samsung->config->commands;
608 struct sabi_data data;
609 int ret, i;
610
611 ret = swsmi_wireless_status(samsung, &data);
612 if (ret)
613 return ret;
614
615 /* Don't set the state for non-present devices */
616 for (i = 0; i < 4; i++)
617 if (data.data[i] == 0x02)
618 data.data[1] = 0;
619
620 if (srfkill->type == RFKILL_TYPE_WLAN)
621 data.data[WL_STATUS_WLAN] = !blocked;
622 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
623 data.data[WL_STATUS_BT] = !blocked;
624
625 return sabi_command(samsung, commands->set_wireless_status,
626 &data, &data);
627}
628
629static void swsmi_rfkill_query(struct rfkill *rfkill, void *priv)
630{
631 struct samsung_rfkill *srfkill = priv;
632 struct samsung_laptop *samsung = srfkill->samsung;
633 struct sabi_data data;
634 int ret;
635
636 ret = swsmi_wireless_status(samsung, &data);
637 if (ret)
638 return ;
639
640 if (srfkill->type == RFKILL_TYPE_WLAN)
641 ret = data.data[WL_STATUS_WLAN];
642 else if (srfkill->type == RFKILL_TYPE_BLUETOOTH)
643 ret = data.data[WL_STATUS_BT];
644 else
645 return ;
646
647 rfkill_set_sw_state(rfkill, !ret);
648}
649
650static const struct rfkill_ops swsmi_rfkill_ops = {
651 .set_block = swsmi_rfkill_set,
652 .query = swsmi_rfkill_query,
653};
654
655static ssize_t get_performance_level(struct device *dev,
656 struct device_attribute *attr, char *buf)
657{
658 struct samsung_laptop *samsung = dev_get_drvdata(dev);
659 const struct sabi_config *config = samsung->config;
660 const struct sabi_commands *commands = &config->commands;
661 struct sabi_data sretval;
662 int retval;
663 int i;
664
665 /* Read the state */
666 retval = sabi_command(samsung, commands->get_performance_level,
667 NULL, &sretval);
668 if (retval)
669 return retval;
670
671 /* The logic is backwards, yeah, lots of fun... */
672 for (i = 0; config->performance_levels[i].name; ++i) {
673 if (sretval.data[0] == config->performance_levels[i].value)
674 return sprintf(buf, "%s\n", config->performance_levels[i].name);
675 }
676 return sprintf(buf, "%s\n", "unknown");
677}
678
679static ssize_t set_performance_level(struct device *dev,
680 struct device_attribute *attr, const char *buf,
681 size_t count)
682{
683 struct samsung_laptop *samsung = dev_get_drvdata(dev);
684 const struct sabi_config *config = samsung->config;
685 const struct sabi_commands *commands = &config->commands;
686 int i;
687
688 if (count < 1)
689 return count;
690
691 for (i = 0; config->performance_levels[i].name; ++i) {
692 const struct sabi_performance_level *level =
693 &config->performance_levels[i];
694 if (!strncasecmp(level->name, buf, strlen(level->name))) {
695 sabi_set_commandb(samsung,
696 commands->set_performance_level,
697 level->value);
698 break;
699 }
700 }
701
702 if (!config->performance_levels[i].name)
703 return -EINVAL;
704
705 return count;
706}
707
708static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
709 get_performance_level, set_performance_level);
710
711static int read_battery_life_extender(struct samsung_laptop *samsung)
712{
713 const struct sabi_commands *commands = &samsung->config->commands;
714 struct sabi_data data;
715 int retval;
716
717 if (commands->get_battery_life_extender == 0xFFFF)
718 return -ENODEV;
719
720 memset(&data, 0, sizeof(data));
721 data.data[0] = 0x80;
722 retval = sabi_command(samsung, commands->get_battery_life_extender,
723 &data, &data);
724
725 if (retval)
726 return retval;
727
728 if (data.data[0] != 0 && data.data[0] != 1)
729 return -ENODEV;
730
731 return data.data[0];
732}
733
734static int write_battery_life_extender(struct samsung_laptop *samsung,
735 int enabled)
736{
737 const struct sabi_commands *commands = &samsung->config->commands;
738 struct sabi_data data;
739
740 memset(&data, 0, sizeof(data));
741 data.data[0] = 0x80 | enabled;
742 return sabi_command(samsung, commands->set_battery_life_extender,
743 &data, NULL);
744}
745
746static ssize_t get_battery_life_extender(struct device *dev,
747 struct device_attribute *attr,
748 char *buf)
749{
750 struct samsung_laptop *samsung = dev_get_drvdata(dev);
751 int ret;
752
753 ret = read_battery_life_extender(samsung);
754 if (ret < 0)
755 return ret;
756
757 return sprintf(buf, "%d\n", ret);
758}
759
760static ssize_t set_battery_life_extender(struct device *dev,
761 struct device_attribute *attr,
762 const char *buf, size_t count)
763{
764 struct samsung_laptop *samsung = dev_get_drvdata(dev);
765 int ret, value;
766
767 if (!count || kstrtoint(buf, 0, &value) != 0)
768 return -EINVAL;
769
770 ret = write_battery_life_extender(samsung, !!value);
771 if (ret < 0)
772 return ret;
773
774 return count;
775}
776
777static DEVICE_ATTR(battery_life_extender, S_IWUSR | S_IRUGO,
778 get_battery_life_extender, set_battery_life_extender);
779
780static int read_usb_charge(struct samsung_laptop *samsung)
781{
782 const struct sabi_commands *commands = &samsung->config->commands;
783 struct sabi_data data;
784 int retval;
785
786 if (commands->get_usb_charge == 0xFFFF)
787 return -ENODEV;
788
789 memset(&data, 0, sizeof(data));
790 data.data[0] = 0x80;
791 retval = sabi_command(samsung, commands->get_usb_charge,
792 &data, &data);
793
794 if (retval)
795 return retval;
796
797 if (data.data[0] != 0 && data.data[0] != 1)
798 return -ENODEV;
799
800 return data.data[0];
801}
802
803static int write_usb_charge(struct samsung_laptop *samsung,
804 int enabled)
805{
806 const struct sabi_commands *commands = &samsung->config->commands;
807 struct sabi_data data;
808
809 memset(&data, 0, sizeof(data));
810 data.data[0] = 0x80 | enabled;
811 return sabi_command(samsung, commands->set_usb_charge,
812 &data, NULL);
813}
814
815static ssize_t get_usb_charge(struct device *dev,
816 struct device_attribute *attr,
817 char *buf)
818{
819 struct samsung_laptop *samsung = dev_get_drvdata(dev);
820 int ret;
821
822 ret = read_usb_charge(samsung);
823 if (ret < 0)
824 return ret;
825
826 return sprintf(buf, "%d\n", ret);
827}
828
829static ssize_t set_usb_charge(struct device *dev,
830 struct device_attribute *attr,
831 const char *buf, size_t count)
832{
833 struct samsung_laptop *samsung = dev_get_drvdata(dev);
834 int ret, value;
835
836 if (!count || kstrtoint(buf, 0, &value) != 0)
837 return -EINVAL;
838
839 ret = write_usb_charge(samsung, !!value);
840 if (ret < 0)
841 return ret;
842
843 return count;
844}
845
846static DEVICE_ATTR(usb_charge, S_IWUSR | S_IRUGO,
847 get_usb_charge, set_usb_charge);
848
849static int read_lid_handling(struct samsung_laptop *samsung)
850{
851 const struct sabi_commands *commands = &samsung->config->commands;
852 struct sabi_data data;
853 int retval;
854
855 if (commands->get_lid_handling == 0xFFFF)
856 return -ENODEV;
857
858 memset(&data, 0, sizeof(data));
859 retval = sabi_command(samsung, commands->get_lid_handling,
860 &data, &data);
861
862 if (retval)
863 return retval;
864
865 return data.data[0] & 0x1;
866}
867
868static int write_lid_handling(struct samsung_laptop *samsung,
869 int enabled)
870{
871 const struct sabi_commands *commands = &samsung->config->commands;
872 struct sabi_data data;
873
874 memset(&data, 0, sizeof(data));
875 data.data[0] = 0x80 | enabled;
876 return sabi_command(samsung, commands->set_lid_handling,
877 &data, NULL);
878}
879
880static ssize_t get_lid_handling(struct device *dev,
881 struct device_attribute *attr,
882 char *buf)
883{
884 struct samsung_laptop *samsung = dev_get_drvdata(dev);
885 int ret;
886
887 ret = read_lid_handling(samsung);
888 if (ret < 0)
889 return ret;
890
891 return sprintf(buf, "%d\n", ret);
892}
893
894static ssize_t set_lid_handling(struct device *dev,
895 struct device_attribute *attr,
896 const char *buf, size_t count)
897{
898 struct samsung_laptop *samsung = dev_get_drvdata(dev);
899 int ret, value;
900
901 if (!count || kstrtoint(buf, 0, &value) != 0)
902 return -EINVAL;
903
904 ret = write_lid_handling(samsung, !!value);
905 if (ret < 0)
906 return ret;
907
908 return count;
909}
910
911static DEVICE_ATTR(lid_handling, S_IWUSR | S_IRUGO,
912 get_lid_handling, set_lid_handling);
913
914static struct attribute *platform_attributes[] = {
915 &dev_attr_performance_level.attr,
916 &dev_attr_battery_life_extender.attr,
917 &dev_attr_usb_charge.attr,
918 &dev_attr_lid_handling.attr,
919 NULL
920};
921
922static int find_signature(void __iomem *memcheck, const char *testStr)
923{
924 int i = 0;
925 int loca;
926
927 for (loca = 0; loca < 0xffff; loca++) {
928 char temp = readb(memcheck + loca);
929
930 if (temp == testStr[i]) {
931 if (i == strlen(testStr)-1)
932 break;
933 ++i;
934 } else {
935 i = 0;
936 }
937 }
938 return loca;
939}
940
941static void samsung_rfkill_exit(struct samsung_laptop *samsung)
942{
943 if (samsung->wlan.rfkill) {
944 rfkill_unregister(samsung->wlan.rfkill);
945 rfkill_destroy(samsung->wlan.rfkill);
946 samsung->wlan.rfkill = NULL;
947 }
948 if (samsung->bluetooth.rfkill) {
949 rfkill_unregister(samsung->bluetooth.rfkill);
950 rfkill_destroy(samsung->bluetooth.rfkill);
951 samsung->bluetooth.rfkill = NULL;
952 }
953}
954
955static int samsung_new_rfkill(struct samsung_laptop *samsung,
956 struct samsung_rfkill *arfkill,
957 const char *name, enum rfkill_type type,
958 const struct rfkill_ops *ops,
959 int blocked)
960{
961 struct rfkill **rfkill = &arfkill->rfkill;
962 int ret;
963
964 arfkill->type = type;
965 arfkill->samsung = samsung;
966
967 *rfkill = rfkill_alloc(name, &samsung->platform_device->dev,
968 type, ops, arfkill);
969
970 if (!*rfkill)
971 return -EINVAL;
972
973 if (blocked != -1)
974 rfkill_init_sw_state(*rfkill, blocked);
975
976 ret = rfkill_register(*rfkill);
977 if (ret) {
978 rfkill_destroy(*rfkill);
979 *rfkill = NULL;
980 return ret;
981 }
982 return 0;
983}
984
985static int __init samsung_rfkill_init_seclinux(struct samsung_laptop *samsung)
986{
987 return samsung_new_rfkill(samsung, &samsung->wlan, "samsung-wlan",
988 RFKILL_TYPE_WLAN, &seclinux_rfkill_ops, -1);
989}
990
991static int __init samsung_rfkill_init_swsmi(struct samsung_laptop *samsung)
992{
993 struct sabi_data data;
994 int ret;
995
996 ret = swsmi_wireless_status(samsung, &data);
997 if (ret) {
998 /* Some swsmi laptops use the old seclinux way to control
999 * wireless devices */
1000 if (ret == -EINVAL)
1001 ret = samsung_rfkill_init_seclinux(samsung);
1002 return ret;
1003 }
1004
1005 /* 0x02 seems to mean that the device is no present/available */
1006
1007 if (data.data[WL_STATUS_WLAN] != 0x02)
1008 ret = samsung_new_rfkill(samsung, &samsung->wlan,
1009 "samsung-wlan",
1010 RFKILL_TYPE_WLAN,
1011 &swsmi_rfkill_ops,
1012 !data.data[WL_STATUS_WLAN]);
1013 if (ret)
1014 goto exit;
1015
1016 if (data.data[WL_STATUS_BT] != 0x02)
1017 ret = samsung_new_rfkill(samsung, &samsung->bluetooth,
1018 "samsung-bluetooth",
1019 RFKILL_TYPE_BLUETOOTH,
1020 &swsmi_rfkill_ops,
1021 !data.data[WL_STATUS_BT]);
1022 if (ret)
1023 goto exit;
1024
1025exit:
1026 if (ret)
1027 samsung_rfkill_exit(samsung);
1028
1029 return ret;
1030}
1031
1032static int __init samsung_rfkill_init(struct samsung_laptop *samsung)
1033{
1034 if (samsung->config->sabi_version == 2)
1035 return samsung_rfkill_init_seclinux(samsung);
1036 if (samsung->config->sabi_version == 3)
1037 return samsung_rfkill_init_swsmi(samsung);
1038 return 0;
1039}
1040
1041static void samsung_lid_handling_exit(struct samsung_laptop *samsung)
1042{
1043 if (samsung->quirks->lid_handling)
1044 write_lid_handling(samsung, 0);
1045}
1046
1047static int __init samsung_lid_handling_init(struct samsung_laptop *samsung)
1048{
1049 int retval = 0;
1050
1051 if (samsung->quirks->lid_handling)
1052 retval = write_lid_handling(samsung, 1);
1053
1054 return retval;
1055}
1056
1057static int kbd_backlight_enable(struct samsung_laptop *samsung)
1058{
1059 const struct sabi_commands *commands = &samsung->config->commands;
1060 struct sabi_data data;
1061 int retval;
1062
1063 if (commands->kbd_backlight == 0xFFFF)
1064 return -ENODEV;
1065
1066 memset(&data, 0, sizeof(data));
1067 data.d0 = 0xaabb;
1068 retval = sabi_command(samsung, commands->kbd_backlight,
1069 &data, &data);
1070
1071 if (retval)
1072 return retval;
1073
1074 if (data.d0 != 0xccdd)
1075 return -ENODEV;
1076 return 0;
1077}
1078
1079static int kbd_backlight_read(struct samsung_laptop *samsung)
1080{
1081 const struct sabi_commands *commands = &samsung->config->commands;
1082 struct sabi_data data;
1083 int retval;
1084
1085 memset(&data, 0, sizeof(data));
1086 data.data[0] = 0x81;
1087 retval = sabi_command(samsung, commands->kbd_backlight,
1088 &data, &data);
1089
1090 if (retval)
1091 return retval;
1092
1093 return data.data[0];
1094}
1095
1096static int kbd_backlight_write(struct samsung_laptop *samsung, int brightness)
1097{
1098 const struct sabi_commands *commands = &samsung->config->commands;
1099 struct sabi_data data;
1100
1101 memset(&data, 0, sizeof(data));
1102 data.d0 = 0x82 | ((brightness & 0xFF) << 8);
1103 return sabi_command(samsung, commands->kbd_backlight,
1104 &data, NULL);
1105}
1106
1107static void kbd_led_update(struct work_struct *work)
1108{
1109 struct samsung_laptop *samsung;
1110
1111 samsung = container_of(work, struct samsung_laptop, kbd_led_work);
1112 kbd_backlight_write(samsung, samsung->kbd_led_wk);
1113}
1114
1115static void kbd_led_set(struct led_classdev *led_cdev,
1116 enum led_brightness value)
1117{
1118 struct samsung_laptop *samsung;
1119
1120 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1121
1122 if (value > samsung->kbd_led.max_brightness)
1123 value = samsung->kbd_led.max_brightness;
1124 else if (value < 0)
1125 value = 0;
1126
1127 samsung->kbd_led_wk = value;
1128 queue_work(samsung->led_workqueue, &samsung->kbd_led_work);
1129}
1130
1131static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1132{
1133 struct samsung_laptop *samsung;
1134
1135 samsung = container_of(led_cdev, struct samsung_laptop, kbd_led);
1136 return kbd_backlight_read(samsung);
1137}
1138
1139static void samsung_leds_exit(struct samsung_laptop *samsung)
1140{
1141 if (!IS_ERR_OR_NULL(samsung->kbd_led.dev))
1142 led_classdev_unregister(&samsung->kbd_led);
1143 if (samsung->led_workqueue)
1144 destroy_workqueue(samsung->led_workqueue);
1145}
1146
1147static int __init samsung_leds_init(struct samsung_laptop *samsung)
1148{
1149 int ret = 0;
1150
1151 samsung->led_workqueue = create_singlethread_workqueue("led_workqueue");
1152 if (!samsung->led_workqueue)
1153 return -ENOMEM;
1154
1155 if (kbd_backlight_enable(samsung) >= 0) {
1156 INIT_WORK(&samsung->kbd_led_work, kbd_led_update);
1157
1158 samsung->kbd_led.name = "samsung::kbd_backlight";
1159 samsung->kbd_led.brightness_set = kbd_led_set;
1160 samsung->kbd_led.brightness_get = kbd_led_get;
1161 samsung->kbd_led.max_brightness = 8;
1162 if (samsung->quirks->four_kbd_backlight_levels)
1163 samsung->kbd_led.max_brightness = 4;
1164
1165 ret = led_classdev_register(&samsung->platform_device->dev,
1166 &samsung->kbd_led);
1167 }
1168
1169 if (ret)
1170 samsung_leds_exit(samsung);
1171
1172 return ret;
1173}
1174
1175static void samsung_backlight_exit(struct samsung_laptop *samsung)
1176{
1177 if (samsung->backlight_device) {
1178 backlight_device_unregister(samsung->backlight_device);
1179 samsung->backlight_device = NULL;
1180 }
1181}
1182
1183static int __init samsung_backlight_init(struct samsung_laptop *samsung)
1184{
1185 struct backlight_device *bd;
1186 struct backlight_properties props;
1187
1188 if (!samsung->handle_backlight)
1189 return 0;
1190
1191 memset(&props, 0, sizeof(struct backlight_properties));
1192 props.type = BACKLIGHT_PLATFORM;
1193 props.max_brightness = samsung->config->max_brightness -
1194 samsung->config->min_brightness;
1195
1196 bd = backlight_device_register("samsung",
1197 &samsung->platform_device->dev,
1198 samsung, &backlight_ops,
1199 &props);
1200 if (IS_ERR(bd))
1201 return PTR_ERR(bd);
1202
1203 samsung->backlight_device = bd;
1204 samsung->backlight_device->props.brightness = read_brightness(samsung);
1205 samsung->backlight_device->props.power = FB_BLANK_UNBLANK;
1206 backlight_update_status(samsung->backlight_device);
1207
1208 return 0;
1209}
1210
1211static umode_t samsung_sysfs_is_visible(struct kobject *kobj,
1212 struct attribute *attr, int idx)
1213{
1214 struct device *dev = container_of(kobj, struct device, kobj);
1215 struct samsung_laptop *samsung = dev_get_drvdata(dev);
1216 bool ok = true;
1217
1218 if (attr == &dev_attr_performance_level.attr)
1219 ok = !!samsung->config->performance_levels[0].name;
1220 if (attr == &dev_attr_battery_life_extender.attr)
1221 ok = !!(read_battery_life_extender(samsung) >= 0);
1222 if (attr == &dev_attr_usb_charge.attr)
1223 ok = !!(read_usb_charge(samsung) >= 0);
1224 if (attr == &dev_attr_lid_handling.attr)
1225 ok = !!(read_lid_handling(samsung) >= 0);
1226
1227 return ok ? attr->mode : 0;
1228}
1229
1230static const struct attribute_group platform_attribute_group = {
1231 .is_visible = samsung_sysfs_is_visible,
1232 .attrs = platform_attributes
1233};
1234
1235static void samsung_sysfs_exit(struct samsung_laptop *samsung)
1236{
1237 struct platform_device *device = samsung->platform_device;
1238
1239 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1240}
1241
1242static int __init samsung_sysfs_init(struct samsung_laptop *samsung)
1243{
1244 struct platform_device *device = samsung->platform_device;
1245
1246 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1247
1248}
1249
1250static int samsung_laptop_call_show(struct seq_file *m, void *data)
1251{
1252 struct samsung_laptop *samsung = m->private;
1253 struct sabi_data *sdata = &samsung->debug.data;
1254 int ret;
1255
1256 seq_printf(m, "SABI 0x%04x {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1257 samsung->debug.command,
1258 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1259
1260 ret = sabi_command(samsung, samsung->debug.command, sdata, sdata);
1261
1262 if (ret) {
1263 seq_printf(m, "SABI command 0x%04x failed\n",
1264 samsung->debug.command);
1265 return ret;
1266 }
1267
1268 seq_printf(m, "SABI {0x%08x, 0x%08x, 0x%04x, 0x%02x}\n",
1269 sdata->d0, sdata->d1, sdata->d2, sdata->d3);
1270 return 0;
1271}
1272DEFINE_SHOW_ATTRIBUTE(samsung_laptop_call);
1273
1274static void samsung_debugfs_exit(struct samsung_laptop *samsung)
1275{
1276 debugfs_remove_recursive(samsung->debug.root);
1277}
1278
1279static void samsung_debugfs_init(struct samsung_laptop *samsung)
1280{
1281 struct dentry *root;
1282
1283 root = debugfs_create_dir("samsung-laptop", NULL);
1284 samsung->debug.root = root;
1285
1286 samsung->debug.f0000_wrapper.data = samsung->f0000_segment;
1287 samsung->debug.f0000_wrapper.size = 0xffff;
1288
1289 samsung->debug.data_wrapper.data = &samsung->debug.data;
1290 samsung->debug.data_wrapper.size = sizeof(samsung->debug.data);
1291
1292 samsung->debug.sdiag_wrapper.data = samsung->sdiag;
1293 samsung->debug.sdiag_wrapper.size = strlen(samsung->sdiag);
1294
1295 debugfs_create_u16("command", S_IRUGO | S_IWUSR, root,
1296 &samsung->debug.command);
1297 debugfs_create_u32("d0", S_IRUGO | S_IWUSR, root,
1298 &samsung->debug.data.d0);
1299 debugfs_create_u32("d1", S_IRUGO | S_IWUSR, root,
1300 &samsung->debug.data.d1);
1301 debugfs_create_u16("d2", S_IRUGO | S_IWUSR, root,
1302 &samsung->debug.data.d2);
1303 debugfs_create_u8("d3", S_IRUGO | S_IWUSR, root,
1304 &samsung->debug.data.d3);
1305 debugfs_create_blob("data", S_IRUGO | S_IWUSR, root,
1306 &samsung->debug.data_wrapper);
1307 debugfs_create_blob("f0000_segment", S_IRUSR | S_IWUSR, root,
1308 &samsung->debug.f0000_wrapper);
1309 debugfs_create_file("call", S_IFREG | S_IRUGO, root, samsung,
1310 &samsung_laptop_call_fops);
1311 debugfs_create_blob("sdiag", S_IRUGO | S_IWUSR, root,
1312 &samsung->debug.sdiag_wrapper);
1313}
1314
1315static void samsung_sabi_exit(struct samsung_laptop *samsung)
1316{
1317 const struct sabi_config *config = samsung->config;
1318
1319 /* Turn off "Linux" mode in the BIOS */
1320 if (config && config->commands.set_linux != 0xff)
1321 sabi_set_commandb(samsung, config->commands.set_linux, 0x80);
1322
1323 if (samsung->sabi_iface) {
1324 iounmap(samsung->sabi_iface);
1325 samsung->sabi_iface = NULL;
1326 }
1327 if (samsung->f0000_segment) {
1328 iounmap(samsung->f0000_segment);
1329 samsung->f0000_segment = NULL;
1330 }
1331
1332 samsung->config = NULL;
1333}
1334
1335static __init void samsung_sabi_infos(struct samsung_laptop *samsung, int loca,
1336 unsigned int ifaceP)
1337{
1338 const struct sabi_config *config = samsung->config;
1339
1340 printk(KERN_DEBUG "This computer supports SABI==%x\n",
1341 loca + 0xf0000 - 6);
1342
1343 printk(KERN_DEBUG "SABI header:\n");
1344 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
1345 readw(samsung->sabi + config->header_offsets.port));
1346 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
1347 readb(samsung->sabi + config->header_offsets.iface_func));
1348 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
1349 readb(samsung->sabi + config->header_offsets.en_mem));
1350 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
1351 readb(samsung->sabi + config->header_offsets.re_mem));
1352 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
1353 readw(samsung->sabi + config->header_offsets.data_offset));
1354 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
1355 readw(samsung->sabi + config->header_offsets.data_segment));
1356
1357 printk(KERN_DEBUG " SABI pointer = 0x%08x\n", ifaceP);
1358}
1359
1360static void __init samsung_sabi_diag(struct samsung_laptop *samsung)
1361{
1362 int loca = find_signature(samsung->f0000_segment, "SDiaG@");
1363 int i;
1364
1365 if (loca == 0xffff)
1366 return ;
1367
1368 /* Example:
1369 * Ident: @SDiaG@686XX-N90X3A/966-SEC-07HL-S90X3A
1370 *
1371 * Product name: 90X3A
1372 * BIOS Version: 07HL
1373 */
1374 loca += 1;
1375 for (i = 0; loca < 0xffff && i < sizeof(samsung->sdiag) - 1; loca++) {
1376 char temp = readb(samsung->f0000_segment + loca);
1377
1378 if (isalnum(temp) || temp == '/' || temp == '-')
1379 samsung->sdiag[i++] = temp;
1380 else
1381 break ;
1382 }
1383
1384 if (debug && samsung->sdiag[0])
1385 pr_info("sdiag: %s", samsung->sdiag);
1386}
1387
1388static int __init samsung_sabi_init(struct samsung_laptop *samsung)
1389{
1390 const struct sabi_config *config = NULL;
1391 const struct sabi_commands *commands;
1392 unsigned int ifaceP;
1393 int loca = 0xffff;
1394 int ret = 0;
1395 int i;
1396
1397 samsung->f0000_segment = ioremap_nocache(0xf0000, 0xffff);
1398 if (!samsung->f0000_segment) {
1399 if (debug || force)
1400 pr_err("Can't map the segment at 0xf0000\n");
1401 ret = -EINVAL;
1402 goto exit;
1403 }
1404
1405 samsung_sabi_diag(samsung);
1406
1407 /* Try to find one of the signatures in memory to find the header */
1408 for (i = 0; sabi_configs[i].test_string != NULL; ++i) {
1409 samsung->config = &sabi_configs[i];
1410 loca = find_signature(samsung->f0000_segment,
1411 samsung->config->test_string);
1412 if (loca != 0xffff)
1413 break;
1414 }
1415
1416 if (loca == 0xffff) {
1417 if (debug || force)
1418 pr_err("This computer does not support SABI\n");
1419 ret = -ENODEV;
1420 goto exit;
1421 }
1422
1423 config = samsung->config;
1424 commands = &config->commands;
1425
1426 /* point to the SMI port Number */
1427 loca += 1;
1428 samsung->sabi = (samsung->f0000_segment + loca);
1429
1430 /* Get a pointer to the SABI Interface */
1431 ifaceP = (readw(samsung->sabi + config->header_offsets.data_segment) & 0x0ffff) << 4;
1432 ifaceP += readw(samsung->sabi + config->header_offsets.data_offset) & 0x0ffff;
1433
1434 if (debug)
1435 samsung_sabi_infos(samsung, loca, ifaceP);
1436
1437 samsung->sabi_iface = ioremap_nocache(ifaceP, 16);
1438 if (!samsung->sabi_iface) {
1439 pr_err("Can't remap %x\n", ifaceP);
1440 ret = -EINVAL;
1441 goto exit;
1442 }
1443
1444 /* Turn on "Linux" mode in the BIOS */
1445 if (commands->set_linux != 0xff) {
1446 int retval = sabi_set_commandb(samsung,
1447 commands->set_linux, 0x81);
1448 if (retval) {
1449 pr_warn("Linux mode was not set!\n");
1450 ret = -ENODEV;
1451 goto exit;
1452 }
1453 }
1454
1455 /* Check for stepping quirk */
1456 if (samsung->handle_backlight)
1457 check_for_stepping_quirk(samsung);
1458
1459 pr_info("detected SABI interface: %s\n",
1460 samsung->config->test_string);
1461
1462exit:
1463 if (ret)
1464 samsung_sabi_exit(samsung);
1465
1466 return ret;
1467}
1468
1469static void samsung_platform_exit(struct samsung_laptop *samsung)
1470{
1471 if (samsung->platform_device) {
1472 platform_device_unregister(samsung->platform_device);
1473 samsung->platform_device = NULL;
1474 }
1475}
1476
1477static int samsung_pm_notification(struct notifier_block *nb,
1478 unsigned long val, void *ptr)
1479{
1480 struct samsung_laptop *samsung;
1481
1482 samsung = container_of(nb, struct samsung_laptop, pm_nb);
1483 if (val == PM_POST_HIBERNATION &&
1484 samsung->quirks->enable_kbd_backlight)
1485 kbd_backlight_enable(samsung);
1486
1487 if (val == PM_POST_HIBERNATION && samsung->quirks->lid_handling)
1488 write_lid_handling(samsung, 1);
1489
1490 return 0;
1491}
1492
1493static int __init samsung_platform_init(struct samsung_laptop *samsung)
1494{
1495 struct platform_device *pdev;
1496
1497 pdev = platform_device_register_simple("samsung", -1, NULL, 0);
1498 if (IS_ERR(pdev))
1499 return PTR_ERR(pdev);
1500
1501 samsung->platform_device = pdev;
1502 platform_set_drvdata(samsung->platform_device, samsung);
1503 return 0;
1504}
1505
1506static struct samsung_quirks *quirks;
1507
1508static int __init samsung_dmi_matched(const struct dmi_system_id *d)
1509{
1510 quirks = d->driver_data;
1511 return 0;
1512}
1513
1514static const struct dmi_system_id samsung_dmi_table[] __initconst = {
1515 {
1516 .matches = {
1517 DMI_MATCH(DMI_SYS_VENDOR,
1518 "SAMSUNG ELECTRONICS CO., LTD."),
1519 DMI_MATCH(DMI_CHASSIS_TYPE, "8"), /* Portable */
1520 },
1521 },
1522 {
1523 .matches = {
1524 DMI_MATCH(DMI_SYS_VENDOR,
1525 "SAMSUNG ELECTRONICS CO., LTD."),
1526 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /* Laptop */
1527 },
1528 },
1529 {
1530 .matches = {
1531 DMI_MATCH(DMI_SYS_VENDOR,
1532 "SAMSUNG ELECTRONICS CO., LTD."),
1533 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /* Notebook */
1534 },
1535 },
1536 {
1537 .matches = {
1538 DMI_MATCH(DMI_SYS_VENDOR,
1539 "SAMSUNG ELECTRONICS CO., LTD."),
1540 DMI_MATCH(DMI_CHASSIS_TYPE, "14"), /* Sub-Notebook */
1541 },
1542 },
1543 /* DMI ids for laptops with bad Chassis Type */
1544 {
1545 .ident = "R40/R41",
1546 .matches = {
1547 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1548 DMI_MATCH(DMI_PRODUCT_NAME, "R40/R41"),
1549 DMI_MATCH(DMI_BOARD_NAME, "R40/R41"),
1550 },
1551 },
1552 /* Specific DMI ids for laptop with quirks */
1553 {
1554 .callback = samsung_dmi_matched,
1555 .ident = "N150P",
1556 .matches = {
1557 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1558 DMI_MATCH(DMI_PRODUCT_NAME, "N150P"),
1559 DMI_MATCH(DMI_BOARD_NAME, "N150P"),
1560 },
1561 .driver_data = &samsung_use_native_backlight,
1562 },
1563 {
1564 .callback = samsung_dmi_matched,
1565 .ident = "N145P/N250P/N260P",
1566 .matches = {
1567 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1568 DMI_MATCH(DMI_PRODUCT_NAME, "N145P/N250P/N260P"),
1569 DMI_MATCH(DMI_BOARD_NAME, "N145P/N250P/N260P"),
1570 },
1571 .driver_data = &samsung_use_native_backlight,
1572 },
1573 {
1574 .callback = samsung_dmi_matched,
1575 .ident = "N150/N210/N220",
1576 .matches = {
1577 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1578 DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220"),
1579 DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220"),
1580 },
1581 .driver_data = &samsung_broken_acpi_video,
1582 },
1583 {
1584 .callback = samsung_dmi_matched,
1585 .ident = "NF110/NF210/NF310",
1586 .matches = {
1587 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1588 DMI_MATCH(DMI_PRODUCT_NAME, "NF110/NF210/NF310"),
1589 DMI_MATCH(DMI_BOARD_NAME, "NF110/NF210/NF310"),
1590 },
1591 .driver_data = &samsung_broken_acpi_video,
1592 },
1593 {
1594 .callback = samsung_dmi_matched,
1595 .ident = "X360",
1596 .matches = {
1597 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1598 DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
1599 DMI_MATCH(DMI_BOARD_NAME, "X360"),
1600 },
1601 .driver_data = &samsung_broken_acpi_video,
1602 },
1603 {
1604 .callback = samsung_dmi_matched,
1605 .ident = "N250P",
1606 .matches = {
1607 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1608 DMI_MATCH(DMI_PRODUCT_NAME, "N250P"),
1609 DMI_MATCH(DMI_BOARD_NAME, "N250P"),
1610 },
1611 .driver_data = &samsung_use_native_backlight,
1612 },
1613 {
1614 .callback = samsung_dmi_matched,
1615 .ident = "NC210",
1616 .matches = {
1617 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1618 DMI_MATCH(DMI_PRODUCT_NAME, "NC210/NC110"),
1619 DMI_MATCH(DMI_BOARD_NAME, "NC210/NC110"),
1620 },
1621 .driver_data = &samsung_broken_acpi_video,
1622 },
1623 {
1624 .callback = samsung_dmi_matched,
1625 .ident = "730U3E/740U3E",
1626 .matches = {
1627 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1628 DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
1629 },
1630 .driver_data = &samsung_np740u3e,
1631 },
1632 {
1633 .callback = samsung_dmi_matched,
1634 .ident = "300V3Z/300V4Z/300V5Z",
1635 .matches = {
1636 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
1637 DMI_MATCH(DMI_PRODUCT_NAME, "300V3Z/300V4Z/300V5Z"),
1638 },
1639 .driver_data = &samsung_lid_handling,
1640 },
1641 { },
1642};
1643MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
1644
1645static struct platform_device *samsung_platform_device;
1646
1647static int __init samsung_init(void)
1648{
1649 struct samsung_laptop *samsung;
1650 int ret;
1651
1652 if (efi_enabled(EFI_BOOT))
1653 return -ENODEV;
1654
1655 quirks = &samsung_unknown;
1656 if (!force && !dmi_check_system(samsung_dmi_table))
1657 return -ENODEV;
1658
1659 samsung = kzalloc(sizeof(*samsung), GFP_KERNEL);
1660 if (!samsung)
1661 return -ENOMEM;
1662
1663 mutex_init(&samsung->sabi_mutex);
1664 samsung->handle_backlight = true;
1665 samsung->quirks = quirks;
1666
1667#ifdef CONFIG_ACPI
1668 if (samsung->quirks->broken_acpi_video)
1669 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
1670 if (samsung->quirks->use_native_backlight)
1671 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
1672
1673 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
1674 samsung->handle_backlight = false;
1675#endif
1676
1677 ret = samsung_platform_init(samsung);
1678 if (ret)
1679 goto error_platform;
1680
1681 ret = samsung_sabi_init(samsung);
1682 if (ret)
1683 goto error_sabi;
1684
1685 ret = samsung_sysfs_init(samsung);
1686 if (ret)
1687 goto error_sysfs;
1688
1689 ret = samsung_backlight_init(samsung);
1690 if (ret)
1691 goto error_backlight;
1692
1693 ret = samsung_rfkill_init(samsung);
1694 if (ret)
1695 goto error_rfkill;
1696
1697 ret = samsung_leds_init(samsung);
1698 if (ret)
1699 goto error_leds;
1700
1701 ret = samsung_lid_handling_init(samsung);
1702 if (ret)
1703 goto error_lid_handling;
1704
1705 samsung_debugfs_init(samsung);
1706
1707 samsung->pm_nb.notifier_call = samsung_pm_notification;
1708 register_pm_notifier(&samsung->pm_nb);
1709
1710 samsung_platform_device = samsung->platform_device;
1711 return ret;
1712
1713error_lid_handling:
1714 samsung_leds_exit(samsung);
1715error_leds:
1716 samsung_rfkill_exit(samsung);
1717error_rfkill:
1718 samsung_backlight_exit(samsung);
1719error_backlight:
1720 samsung_sysfs_exit(samsung);
1721error_sysfs:
1722 samsung_sabi_exit(samsung);
1723error_sabi:
1724 samsung_platform_exit(samsung);
1725error_platform:
1726 kfree(samsung);
1727 return ret;
1728}
1729
1730static void __exit samsung_exit(void)
1731{
1732 struct samsung_laptop *samsung;
1733
1734 samsung = platform_get_drvdata(samsung_platform_device);
1735 unregister_pm_notifier(&samsung->pm_nb);
1736
1737 samsung_debugfs_exit(samsung);
1738 samsung_lid_handling_exit(samsung);
1739 samsung_leds_exit(samsung);
1740 samsung_rfkill_exit(samsung);
1741 samsung_backlight_exit(samsung);
1742 samsung_sysfs_exit(samsung);
1743 samsung_sabi_exit(samsung);
1744 samsung_platform_exit(samsung);
1745
1746 kfree(samsung);
1747 samsung_platform_device = NULL;
1748}
1749
1750module_init(samsung_init);
1751module_exit(samsung_exit);
1752
1753MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
1754MODULE_DESCRIPTION("Samsung Backlight driver");
1755MODULE_LICENSE("GPL");