Staging: samsung-laptop: add a bunch more laptop DMI signatures
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / samsung-laptop / samsung-laptop.c
blob51ec6216b1ea46b3a93801fb04595b60c5fa12f2
1 /*
2 * Samsung Laptop driver
4 * Copyright (C) 2009 Greg Kroah-Hartman (gregkh@suse.de)
5 * Copyright (C) 2009 Novell Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/pci.h>
17 #include <linux/backlight.h>
18 #include <linux/fb.h>
19 #include <linux/dmi.h>
20 #include <linux/platform_device.h>
21 #include <linux/rfkill.h>
24 * This driver is needed because a number of Samsung laptops do not hook
25 * their control settings through ACPI. So we have to poke around in the
26 * BIOS to do things like brightness values, and "special" key controls.
30 * We have 0 - 8 as valid brightness levels. The specs say that level 0 should
31 * be reserved by the BIOS (which really doesn't make much sense), we tell
32 * userspace that the value is 0 - 7 and then just tell the hardware 1 - 8
34 #define MAX_BRIGHT 0x07
37 #define SABI_IFACE_MAIN 0x00
38 #define SABI_IFACE_SUB 0x02
39 #define SABI_IFACE_COMPLETE 0x04
40 #define SABI_IFACE_DATA 0x05
42 /* Structure to get data back to the calling function */
43 struct sabi_retval {
44 u8 retval[20];
47 struct sabi_header_offsets {
48 u8 port;
49 u8 re_mem;
50 u8 iface_func;
51 u8 en_mem;
52 u8 data_offset;
53 u8 data_segment;
56 struct sabi_commands {
58 * Brightness is 0 - 8, as described above.
59 * Value 0 is for the BIOS to use
61 u8 get_brightness;
62 u8 set_brightness;
65 * first byte:
66 * 0x00 - wireless is off
67 * 0x01 - wireless is on
68 * second byte:
69 * 0x02 - 3G is off
70 * 0x03 - 3G is on
71 * TODO, verify 3G is correct, that doesn't seem right...
73 u8 get_wireless_button;
74 u8 set_wireless_button;
76 /* 0 is off, 1 is on */
77 u8 get_backlight;
78 u8 set_backlight;
81 * 0x80 or 0x00 - no action
82 * 0x81 - recovery key pressed
84 u8 get_recovery_mode;
85 u8 set_recovery_mode;
88 * on seclinux: 0 is low, 1 is high,
89 * on swsmi: 0 is normal, 1 is silent, 2 is turbo
91 u8 get_performance_level;
92 u8 set_performance_level;
95 * Tell the BIOS that Linux is running on this machine.
96 * 81 is on, 80 is off
98 u8 set_linux;
101 struct sabi_performance_level {
102 const char *name;
103 u8 value;
106 struct sabi_config {
107 const char *test_string;
108 u16 main_function;
109 struct sabi_header_offsets header_offsets;
110 struct sabi_commands commands;
111 struct sabi_performance_level performance_levels[4];
114 static struct sabi_config sabi_configs[] = {
116 .test_string = "SECLINUX",
118 .main_function = 0x4c59,
120 .header_offsets = {
121 .port = 0x00,
122 .re_mem = 0x02,
123 .iface_func = 0x03,
124 .en_mem = 0x04,
125 .data_offset = 0x05,
126 .data_segment = 0x07,
129 .commands = {
130 .get_brightness = 0x00,
131 .set_brightness = 0x01,
133 .get_wireless_button = 0x02,
134 .set_wireless_button = 0x03,
136 .get_backlight = 0x04,
137 .set_backlight = 0x05,
139 .get_recovery_mode = 0x06,
140 .set_recovery_mode = 0x07,
142 .get_performance_level = 0x08,
143 .set_performance_level = 0x09,
145 .set_linux = 0x0a,
148 .performance_levels = {
150 .name = "silent",
151 .value = 0,
154 .name = "normal",
155 .value = 1,
157 { },
161 .test_string = "SwSmi@",
163 .main_function = 0x5843,
165 .header_offsets = {
166 .port = 0x00,
167 .re_mem = 0x04,
168 .iface_func = 0x02,
169 .en_mem = 0x03,
170 .data_offset = 0x05,
171 .data_segment = 0x07,
174 .commands = {
175 .get_brightness = 0x10,
176 .set_brightness = 0x11,
178 .get_wireless_button = 0x12,
179 .set_wireless_button = 0x13,
181 .get_backlight = 0x2d,
182 .set_backlight = 0x2e,
184 .get_recovery_mode = 0xff,
185 .set_recovery_mode = 0xff,
187 .get_performance_level = 0x31,
188 .set_performance_level = 0x32,
190 .set_linux = 0xff,
193 .performance_levels = {
195 .name = "normal",
196 .value = 0,
199 .name = "silent",
200 .value = 1,
203 .name = "overclock",
204 .value = 2,
206 { },
209 { },
212 static struct sabi_config *sabi_config;
214 static void __iomem *sabi;
215 static void __iomem *sabi_iface;
216 static void __iomem *f0000_segment;
217 static struct backlight_device *backlight_device;
218 static struct mutex sabi_mutex;
219 static struct platform_device *sdev;
220 static struct rfkill *rfk;
222 static int force;
223 module_param(force, bool, 0);
224 MODULE_PARM_DESC(force,
225 "Disable the DMI check and forces the driver to be loaded");
227 static int debug;
228 module_param(debug, bool, S_IRUGO | S_IWUSR);
229 MODULE_PARM_DESC(debug, "Debug enabled or not");
231 static int sabi_get_command(u8 command, struct sabi_retval *sretval)
233 int retval = 0;
234 u16 port = readw(sabi + sabi_config->header_offsets.port);
236 mutex_lock(&sabi_mutex);
238 /* enable memory to be able to write to it */
239 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
241 /* write out the command */
242 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
243 writew(command, sabi_iface + SABI_IFACE_SUB);
244 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
245 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
247 /* write protect memory to make it safe */
248 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
250 /* see if the command actually succeeded */
251 if (readb(sabi_iface + SABI_IFACE_COMPLETE) == 0xaa &&
252 readb(sabi_iface + SABI_IFACE_DATA) != 0xff) {
254 * It did!
255 * Save off the data into a structure so the caller use it.
256 * Right now we only care about the first 4 bytes,
257 * I suppose there are commands that need more, but I don't
258 * know about them.
260 sretval->retval[0] = readb(sabi_iface + SABI_IFACE_DATA);
261 sretval->retval[1] = readb(sabi_iface + SABI_IFACE_DATA + 1);
262 sretval->retval[2] = readb(sabi_iface + SABI_IFACE_DATA + 2);
263 sretval->retval[3] = readb(sabi_iface + SABI_IFACE_DATA + 3);
264 goto exit;
267 /* Something bad happened, so report it and error out */
268 printk(KERN_WARNING "SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
269 command, readb(sabi_iface + SABI_IFACE_COMPLETE),
270 readb(sabi_iface + SABI_IFACE_DATA));
271 retval = -EINVAL;
272 exit:
273 mutex_unlock(&sabi_mutex);
274 return retval;
278 static int sabi_set_command(u8 command, u8 data)
280 int retval = 0;
281 u16 port = readw(sabi + sabi_config->header_offsets.port);
283 mutex_lock(&sabi_mutex);
285 /* enable memory to be able to write to it */
286 outb(readb(sabi + sabi_config->header_offsets.en_mem), port);
288 /* write out the command */
289 writew(sabi_config->main_function, sabi_iface + SABI_IFACE_MAIN);
290 writew(command, sabi_iface + SABI_IFACE_SUB);
291 writeb(0, sabi_iface + SABI_IFACE_COMPLETE);
292 writeb(data, sabi_iface + SABI_IFACE_DATA);
293 outb(readb(sabi + sabi_config->header_offsets.iface_func), port);
295 /* write protect memory to make it safe */
296 outb(readb(sabi + sabi_config->header_offsets.re_mem), port);
298 /* see if the command actually succeeded */
299 if (readb(sabi_iface + SABI_IFACE_COMPLETE) == 0xaa &&
300 readb(sabi_iface + SABI_IFACE_DATA) != 0xff) {
301 /* it did! */
302 goto exit;
305 /* Something bad happened, so report it and error out */
306 printk(KERN_WARNING "SABI command 0x%02x failed with completion flag 0x%02x and output 0x%02x\n",
307 command, readb(sabi_iface + SABI_IFACE_COMPLETE),
308 readb(sabi_iface + SABI_IFACE_DATA));
309 retval = -EINVAL;
310 exit:
311 mutex_unlock(&sabi_mutex);
312 return retval;
315 static void test_backlight(void)
317 struct sabi_retval sretval;
319 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
320 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
322 sabi_set_command(sabi_config->commands.set_backlight, 0);
323 printk(KERN_DEBUG "backlight should be off\n");
325 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
326 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
328 msleep(1000);
330 sabi_set_command(sabi_config->commands.set_backlight, 1);
331 printk(KERN_DEBUG "backlight should be on\n");
333 sabi_get_command(sabi_config->commands.get_backlight, &sretval);
334 printk(KERN_DEBUG "backlight = 0x%02x\n", sretval.retval[0]);
337 static void test_wireless(void)
339 struct sabi_retval sretval;
341 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
342 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
344 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
345 printk(KERN_DEBUG "wireless led should be off\n");
347 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
348 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
350 msleep(1000);
352 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
353 printk(KERN_DEBUG "wireless led should be on\n");
355 sabi_get_command(sabi_config->commands.get_wireless_button, &sretval);
356 printk(KERN_DEBUG "wireless led = 0x%02x\n", sretval.retval[0]);
359 static u8 read_brightness(void)
361 struct sabi_retval sretval;
362 int user_brightness = 0;
363 int retval;
365 retval = sabi_get_command(sabi_config->commands.get_brightness,
366 &sretval);
367 if (!retval)
368 user_brightness = sretval.retval[0];
369 if (user_brightness != 0)
370 --user_brightness;
371 return user_brightness;
374 static void set_brightness(u8 user_brightness)
376 sabi_set_command(sabi_config->commands.set_brightness,
377 user_brightness + 1);
380 static int get_brightness(struct backlight_device *bd)
382 return (int)read_brightness();
385 static int update_status(struct backlight_device *bd)
387 set_brightness(bd->props.brightness);
389 if (bd->props.power == FB_BLANK_UNBLANK)
390 sabi_set_command(sabi_config->commands.set_backlight, 1);
391 else
392 sabi_set_command(sabi_config->commands.set_backlight, 0);
393 return 0;
396 static const struct backlight_ops backlight_ops = {
397 .get_brightness = get_brightness,
398 .update_status = update_status,
401 static int rfkill_set(void *data, bool blocked)
403 /* Do something with blocked...*/
405 * blocked == false is on
406 * blocked == true is off
408 if (blocked)
409 sabi_set_command(sabi_config->commands.set_wireless_button, 0);
410 else
411 sabi_set_command(sabi_config->commands.set_wireless_button, 1);
413 return 0;
416 static struct rfkill_ops rfkill_ops = {
417 .set_block = rfkill_set,
420 static int init_wireless(struct platform_device *sdev)
422 int retval;
424 rfk = rfkill_alloc("samsung-wifi", &sdev->dev, RFKILL_TYPE_WLAN,
425 &rfkill_ops, NULL);
426 if (!rfk)
427 return -ENOMEM;
429 retval = rfkill_register(rfk);
430 if (retval) {
431 rfkill_destroy(rfk);
432 return -ENODEV;
435 return 0;
438 static void destroy_wireless(void)
440 rfkill_unregister(rfk);
441 rfkill_destroy(rfk);
444 static ssize_t get_performance_level(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct sabi_retval sretval;
448 int retval;
449 int i;
451 /* Read the state */
452 retval = sabi_get_command(sabi_config->commands.get_performance_level,
453 &sretval);
454 if (retval)
455 return retval;
457 /* The logic is backwards, yeah, lots of fun... */
458 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
459 if (sretval.retval[0] == sabi_config->performance_levels[i].value)
460 return sprintf(buf, "%s\n", sabi_config->performance_levels[i].name);
462 return sprintf(buf, "%s\n", "unknown");
465 static ssize_t set_performance_level(struct device *dev,
466 struct device_attribute *attr, const char *buf,
467 size_t count)
469 if (count >= 1) {
470 int i;
471 for (i = 0; sabi_config->performance_levels[i].name; ++i) {
472 struct sabi_performance_level *level =
473 &sabi_config->performance_levels[i];
474 if (!strncasecmp(level->name, buf, strlen(level->name))) {
475 sabi_set_command(sabi_config->commands.set_performance_level,
476 level->value);
477 break;
480 if (!sabi_config->performance_levels[i].name)
481 return -EINVAL;
483 return count;
485 static DEVICE_ATTR(performance_level, S_IWUSR | S_IRUGO,
486 get_performance_level, set_performance_level);
489 static int __init dmi_check_cb(const struct dmi_system_id *id)
491 printk(KERN_INFO KBUILD_MODNAME ": found laptop model '%s'\n",
492 id->ident);
493 return 0;
496 static struct dmi_system_id __initdata samsung_dmi_table[] = {
498 .ident = "N128",
499 .matches = {
500 DMI_MATCH(DMI_SYS_VENDOR,
501 "SAMSUNG ELECTRONICS CO., LTD."),
502 DMI_MATCH(DMI_PRODUCT_NAME, "N128"),
503 DMI_MATCH(DMI_BOARD_NAME, "N128"),
505 .callback = dmi_check_cb,
508 .ident = "N130",
509 .matches = {
510 DMI_MATCH(DMI_SYS_VENDOR,
511 "SAMSUNG ELECTRONICS CO., LTD."),
512 DMI_MATCH(DMI_PRODUCT_NAME, "N130"),
513 DMI_MATCH(DMI_BOARD_NAME, "N130"),
515 .callback = dmi_check_cb,
518 .ident = "X125",
519 .matches = {
520 DMI_MATCH(DMI_SYS_VENDOR,
521 "SAMSUNG ELECTRONICS CO., LTD."),
522 DMI_MATCH(DMI_PRODUCT_NAME, "X125"),
523 DMI_MATCH(DMI_BOARD_NAME, "X125"),
525 .callback = dmi_check_cb,
528 .ident = "NC10",
529 .matches = {
530 DMI_MATCH(DMI_SYS_VENDOR,
531 "SAMSUNG ELECTRONICS CO., LTD."),
532 DMI_MATCH(DMI_PRODUCT_NAME, "NC10"),
533 DMI_MATCH(DMI_BOARD_NAME, "NC10"),
535 .callback = dmi_check_cb,
538 .ident = "NP-Q45",
539 .matches = {
540 DMI_MATCH(DMI_SYS_VENDOR,
541 "SAMSUNG ELECTRONICS CO., LTD."),
542 DMI_MATCH(DMI_PRODUCT_NAME, "SQ45S70S"),
543 DMI_MATCH(DMI_BOARD_NAME, "SQ45S70S"),
545 .callback = dmi_check_cb,
548 .ident = "X360",
549 .matches = {
550 DMI_MATCH(DMI_SYS_VENDOR,
551 "SAMSUNG ELECTRONICS CO., LTD."),
552 DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
553 DMI_MATCH(DMI_BOARD_NAME, "X360"),
555 .callback = dmi_check_cb,
558 .ident = "R518",
559 .matches = {
560 DMI_MATCH(DMI_SYS_VENDOR,
561 "SAMSUNG ELECTRONICS CO., LTD."),
562 DMI_MATCH(DMI_PRODUCT_NAME, "R518"),
563 DMI_MATCH(DMI_BOARD_NAME, "R518"),
565 .callback = dmi_check_cb,
568 .ident = "N150/N210/N220",
569 .matches = {
570 DMI_MATCH(DMI_SYS_VENDOR,
571 "SAMSUNG ELECTRONICS CO., LTD."),
572 DMI_MATCH(DMI_PRODUCT_NAME, "N150/N210/N220"),
573 DMI_MATCH(DMI_BOARD_NAME, "N150/N210/N220"),
575 .callback = dmi_check_cb,
578 .ident = "R530/R730",
579 .matches = {
580 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
581 DMI_MATCH(DMI_PRODUCT_NAME, "R530/R730"),
582 DMI_MATCH(DMI_BOARD_NAME, "R530/R730"),
584 .callback = dmi_check_cb,
587 .ident = "NF110/NF210/NF310",
588 .matches = {
589 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
590 DMI_MATCH(DMI_PRODUCT_NAME, "NF110/NF210/NF310"),
591 DMI_MATCH(DMI_BOARD_NAME, "NF110/NF210/NF310"),
593 .callback = dmi_check_cb,
595 { },
597 MODULE_DEVICE_TABLE(dmi, samsung_dmi_table);
599 static int find_signature(void __iomem *memcheck, const char *testStr)
601 int i = 0;
602 int loca;
604 for (loca = 0; loca < 0xffff; loca++) {
605 char temp = readb(memcheck + loca);
607 if (temp == testStr[i]) {
608 if (i == strlen(testStr)-1)
609 break;
610 ++i;
611 } else {
612 i = 0;
615 return loca;
618 static int __init samsung_init(void)
620 struct backlight_properties props;
621 struct sabi_retval sretval;
622 unsigned int ifaceP;
623 int i;
624 int loca;
625 int retval;
627 mutex_init(&sabi_mutex);
629 if (!force && !dmi_check_system(samsung_dmi_table))
630 return -ENODEV;
632 f0000_segment = ioremap(0xf0000, 0xffff);
633 if (!f0000_segment) {
634 printk(KERN_ERR "Can't map the segment at 0xf0000\n");
635 return -EINVAL;
638 /* Try to find one of the signatures in memory to find the header */
639 for (i = 0; sabi_configs[i].test_string != 0; ++i) {
640 sabi_config = &sabi_configs[i];
641 loca = find_signature(f0000_segment, sabi_config->test_string);
642 if (loca != 0xffff)
643 break;
646 if (loca == 0xffff) {
647 printk(KERN_ERR "This computer does not support SABI\n");
648 goto error_no_signature;
651 /* point to the SMI port Number */
652 loca += 1;
653 sabi = (f0000_segment + loca);
655 if (debug) {
656 printk(KERN_DEBUG "This computer supports SABI==%x\n",
657 loca + 0xf0000 - 6);
658 printk(KERN_DEBUG "SABI header:\n");
659 printk(KERN_DEBUG " SMI Port Number = 0x%04x\n",
660 readw(sabi + sabi_config->header_offsets.port));
661 printk(KERN_DEBUG " SMI Interface Function = 0x%02x\n",
662 readb(sabi + sabi_config->header_offsets.iface_func));
663 printk(KERN_DEBUG " SMI enable memory buffer = 0x%02x\n",
664 readb(sabi + sabi_config->header_offsets.en_mem));
665 printk(KERN_DEBUG " SMI restore memory buffer = 0x%02x\n",
666 readb(sabi + sabi_config->header_offsets.re_mem));
667 printk(KERN_DEBUG " SABI data offset = 0x%04x\n",
668 readw(sabi + sabi_config->header_offsets.data_offset));
669 printk(KERN_DEBUG " SABI data segment = 0x%04x\n",
670 readw(sabi + sabi_config->header_offsets.data_segment));
673 /* Get a pointer to the SABI Interface */
674 ifaceP = (readw(sabi + sabi_config->header_offsets.data_segment) & 0x0ffff) << 4;
675 ifaceP += readw(sabi + sabi_config->header_offsets.data_offset) & 0x0ffff;
676 sabi_iface = ioremap(ifaceP, 16);
677 if (!sabi_iface) {
678 printk(KERN_ERR "Can't remap %x\n", ifaceP);
679 goto exit;
681 if (debug) {
682 printk(KERN_DEBUG "ifaceP = 0x%08x\n", ifaceP);
683 printk(KERN_DEBUG "sabi_iface = %p\n", sabi_iface);
685 test_backlight();
686 test_wireless();
688 retval = sabi_get_command(sabi_config->commands.get_brightness,
689 &sretval);
690 printk(KERN_DEBUG "brightness = 0x%02x\n", sretval.retval[0]);
693 /* Turn on "Linux" mode in the BIOS */
694 if (sabi_config->commands.set_linux != 0xff) {
695 retval = sabi_set_command(sabi_config->commands.set_linux,
696 0x81);
697 if (retval) {
698 printk(KERN_ERR KBUILD_MODNAME ": Linux mode was not set!\n");
699 goto error_no_platform;
703 /* knock up a platform device to hang stuff off of */
704 sdev = platform_device_register_simple("samsung", -1, NULL, 0);
705 if (IS_ERR(sdev))
706 goto error_no_platform;
708 /* create a backlight device to talk to this one */
709 memset(&props, 0, sizeof(struct backlight_properties));
710 props.max_brightness = MAX_BRIGHT;
711 backlight_device = backlight_device_register("samsung", &sdev->dev,
712 NULL, &backlight_ops,
713 &props);
714 if (IS_ERR(backlight_device))
715 goto error_no_backlight;
717 backlight_device->props.brightness = read_brightness();
718 backlight_device->props.power = FB_BLANK_UNBLANK;
719 backlight_update_status(backlight_device);
721 retval = init_wireless(sdev);
722 if (retval)
723 goto error_no_rfk;
725 retval = device_create_file(&sdev->dev, &dev_attr_performance_level);
726 if (retval)
727 goto error_file_create;
729 exit:
730 return 0;
732 error_file_create:
733 destroy_wireless();
735 error_no_rfk:
736 backlight_device_unregister(backlight_device);
738 error_no_backlight:
739 platform_device_unregister(sdev);
741 error_no_platform:
742 iounmap(sabi_iface);
744 error_no_signature:
745 iounmap(f0000_segment);
746 return -EINVAL;
749 static void __exit samsung_exit(void)
751 /* Turn off "Linux" mode in the BIOS */
752 if (sabi_config->commands.set_linux != 0xff)
753 sabi_set_command(sabi_config->commands.set_linux, 0x80);
755 device_remove_file(&sdev->dev, &dev_attr_performance_level);
756 backlight_device_unregister(backlight_device);
757 destroy_wireless();
758 iounmap(sabi_iface);
759 iounmap(f0000_segment);
760 platform_device_unregister(sdev);
763 module_init(samsung_init);
764 module_exit(samsung_exit);
766 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@suse.de>");
767 MODULE_DESCRIPTION("Samsung Backlight driver");
768 MODULE_LICENSE("GPL");