4 Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * msi-laptop.c - MSI S270 laptop support. This laptop is sold under
24 * various brands, including "Cytron/TCM/Medion/Tchibo MD96100".
26 * Driver also supports S271, S420 models.
28 * This driver exports a few files in /sys/devices/platform/msi-laptop-pf/:
30 * lcd_level - Screen brightness: contains a single integer in the
33 * auto_brightness - Enable automatic brightness control: contains
34 * either 0 or 1. If set to 1 the hardware adjusts the screen
35 * brightness automatically when the power cord is
36 * plugged/unplugged. (rw)
38 * wlan - WLAN subsystem enabled: contains either 0 or 1. (ro)
40 * bluetooth - Bluetooth subsystem enabled: contains either 0 or 1
41 * Please note that this file is constantly 0 if no Bluetooth
42 * hardware is available. (ro)
44 * In addition to these platform device attributes the driver
45 * registers itself in the Linux backlight control subsystem and is
46 * available to userspace under /sys/class/backlight/msi-laptop-bl/.
48 * This driver might work on other laptops produced by MSI. If you
49 * want to try it you can pass force=1 as argument to the module which
50 * will force it to load even when the DMI data doesn't identify the
51 * laptop as MSI S270. YMMV.
54 #include <linux/module.h>
55 #include <linux/kernel.h>
56 #include <linux/init.h>
57 #include <linux/acpi.h>
58 #include <linux/dmi.h>
59 #include <linux/backlight.h>
60 #include <linux/platform_device.h>
61 #include <linux/rfkill.h>
63 #define MSI_DRIVER_VERSION "0.5"
65 #define MSI_LCD_LEVEL_MAX 9
67 #define MSI_EC_COMMAND_WIRELESS 0x10
68 #define MSI_EC_COMMAND_LCD_LEVEL 0x11
70 #define MSI_STANDARD_EC_COMMAND_ADDRESS 0x2e
71 #define MSI_STANDARD_EC_BLUETOOTH_MASK (1 << 0)
72 #define MSI_STANDARD_EC_WEBCAM_MASK (1 << 1)
73 #define MSI_STANDARD_EC_WLAN_MASK (1 << 3)
74 #define MSI_STANDARD_EC_3G_MASK (1 << 4)
76 /* For set SCM load flag to disable BIOS fn key */
77 #define MSI_STANDARD_EC_SCM_LOAD_ADDRESS 0x2d
78 #define MSI_STANDARD_EC_SCM_LOAD_MASK (1 << 0)
80 static int msi_laptop_resume(struct platform_device *device);
82 #define MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS 0x2f
85 module_param(force, bool, 0);
86 MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
88 static int auto_brightness;
89 module_param(auto_brightness, int, 0);
90 MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
92 static bool old_ec_model;
93 static int wlan_s, bluetooth_s, threeg_s;
94 static int threeg_exists;
96 /* Some MSI 3G netbook only have one fn key to control Wlan/Bluetooth/3G,
97 * those netbook will load the SCM (windows app) to disable the original
98 * Wlan/Bluetooth control by BIOS when user press fn key, then control
99 * Wlan/Bluetooth/3G by SCM (software control by OS). Without SCM, user
100 * cann't on/off 3G module on those 3G netbook.
101 * On Linux, msi-laptop driver will do the same thing to disable the
102 * original BIOS control, then might need use HAL or other userland
103 * application to do the software control that simulate with SCM.
104 * e.g. MSI N034 netbook
106 static bool load_scm_model;
107 static struct rfkill *rfk_wlan, *rfk_bluetooth, *rfk_threeg;
109 /* Hardware access */
111 static int set_lcd_level(int level)
115 if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
119 buf[1] = (u8) (level*31);
121 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf), NULL, 0, 1);
124 static int get_lcd_level(void)
129 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
133 return (int) rdata / 31;
136 static int get_auto_brightness(void)
141 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
145 return !!(rdata & 8);
148 static int set_auto_brightness(int enable)
155 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1, &rdata, 1, 1);
160 wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
162 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2, NULL, 0, 1);
165 static ssize_t set_device_state(const char *buf, size_t count, u8 mask)
171 if (sscanf(buf, "%i", &status) != 1 || (status < 0 || status > 1))
174 /* read current device state */
175 result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
179 if (!!(rdata & mask) != status) {
180 /* reverse device bit */
182 wdata = rdata & ~mask;
184 wdata = rdata | mask;
186 result = ec_write(MSI_STANDARD_EC_COMMAND_ADDRESS, wdata);
194 static int get_wireless_state(int *wlan, int *bluetooth)
199 result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1, 1);
204 *wlan = !!(rdata & 8);
207 *bluetooth = !!(rdata & 128);
212 static int get_wireless_state_ec_standard(void)
217 result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
221 wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);
223 bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);
225 threeg_s = !!(rdata & MSI_STANDARD_EC_3G_MASK);
230 static int get_threeg_exists(void)
235 result = ec_read(MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS, &rdata);
239 threeg_exists = !!(rdata & MSI_STANDARD_EC_3G_MASK);
244 /* Backlight device stuff */
246 static int bl_get_brightness(struct backlight_device *b)
248 return get_lcd_level();
252 static int bl_update_status(struct backlight_device *b)
254 return set_lcd_level(b->props.brightness);
257 static struct backlight_ops msibl_ops = {
258 .get_brightness = bl_get_brightness,
259 .update_status = bl_update_status,
262 static struct backlight_device *msibl_device;
264 /* Platform device */
266 static ssize_t show_wlan(struct device *dev,
267 struct device_attribute *attr, char *buf)
273 ret = get_wireless_state(&enabled, NULL);
275 ret = get_wireless_state_ec_standard();
281 return sprintf(buf, "%i\n", enabled);
284 static ssize_t store_wlan(struct device *dev,
285 struct device_attribute *attr, const char *buf, size_t count)
287 return set_device_state(buf, count, MSI_STANDARD_EC_WLAN_MASK);
290 static ssize_t show_bluetooth(struct device *dev,
291 struct device_attribute *attr, char *buf)
297 ret = get_wireless_state(NULL, &enabled);
299 ret = get_wireless_state_ec_standard();
300 enabled = bluetooth_s;
305 return sprintf(buf, "%i\n", enabled);
308 static ssize_t store_bluetooth(struct device *dev,
309 struct device_attribute *attr, const char *buf, size_t count)
311 return set_device_state(buf, count, MSI_STANDARD_EC_BLUETOOTH_MASK);
314 static ssize_t show_threeg(struct device *dev,
315 struct device_attribute *attr, char *buf)
320 /* old msi ec not support 3G */
324 ret = get_wireless_state_ec_standard();
328 return sprintf(buf, "%i\n", threeg_s);
331 static ssize_t store_threeg(struct device *dev,
332 struct device_attribute *attr, const char *buf, size_t count)
334 return set_device_state(buf, count, MSI_STANDARD_EC_3G_MASK);
337 static ssize_t show_lcd_level(struct device *dev,
338 struct device_attribute *attr, char *buf)
343 ret = get_lcd_level();
347 return sprintf(buf, "%i\n", ret);
350 static ssize_t store_lcd_level(struct device *dev,
351 struct device_attribute *attr, const char *buf, size_t count)
356 if (sscanf(buf, "%i", &level) != 1 || (level < 0 || level >= MSI_LCD_LEVEL_MAX))
359 ret = set_lcd_level(level);
366 static ssize_t show_auto_brightness(struct device *dev,
367 struct device_attribute *attr, char *buf)
372 ret = get_auto_brightness();
376 return sprintf(buf, "%i\n", ret);
379 static ssize_t store_auto_brightness(struct device *dev,
380 struct device_attribute *attr, const char *buf, size_t count)
385 if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
388 ret = set_auto_brightness(enable);
395 static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
396 static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness, store_auto_brightness);
397 static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
398 static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
399 static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
401 static struct attribute *msipf_attributes[] = {
402 &dev_attr_lcd_level.attr,
403 &dev_attr_auto_brightness.attr,
404 &dev_attr_bluetooth.attr,
409 static struct attribute_group msipf_attribute_group = {
410 .attrs = msipf_attributes
413 static struct platform_driver msipf_driver = {
415 .name = "msi-laptop-pf",
416 .owner = THIS_MODULE,
418 .resume = msi_laptop_resume,
421 static struct platform_device *msipf_device;
425 static int dmi_check_cb(const struct dmi_system_id *id)
427 printk("msi-laptop: Identified laptop model '%s'.\n", id->ident);
431 static struct dmi_system_id __initdata msi_dmi_table[] = {
435 DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
436 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
437 DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
438 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
440 .callback = dmi_check_cb
445 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
446 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
447 DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
448 DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
450 .callback = dmi_check_cb
455 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
456 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
457 DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
458 DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
460 .callback = dmi_check_cb
463 .ident = "Medion MD96100",
465 DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
466 DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
467 DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
468 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
470 .callback = dmi_check_cb
475 DMI_MATCH(DMI_SYS_VENDOR,
476 "MICRO-STAR INTERNATIONAL CO., LTD"),
477 DMI_MATCH(DMI_PRODUCT_NAME, "MS-N051"),
478 DMI_MATCH(DMI_CHASSIS_VENDOR,
479 "MICRO-STAR INTERNATIONAL CO., LTD")
481 .callback = dmi_check_cb
486 DMI_MATCH(DMI_SYS_VENDOR,
487 "MICRO-STAR INTERNATIONAL CO., LTD"),
488 DMI_MATCH(DMI_PRODUCT_NAME, "MS-N014"),
490 .callback = dmi_check_cb
493 .ident = "MSI CR620",
495 DMI_MATCH(DMI_SYS_VENDOR,
496 "Micro-Star International"),
497 DMI_MATCH(DMI_PRODUCT_NAME, "CR620"),
499 .callback = dmi_check_cb
504 static struct dmi_system_id __initdata msi_load_scm_models_dmi_table[] = {
508 DMI_MATCH(DMI_SYS_VENDOR,
509 "MICRO-STAR INTERNATIONAL CO., LTD"),
510 DMI_MATCH(DMI_PRODUCT_NAME, "MS-N034"),
511 DMI_MATCH(DMI_CHASSIS_VENDOR,
512 "MICRO-STAR INTERNATIONAL CO., LTD")
514 .callback = dmi_check_cb
519 static int rfkill_bluetooth_set(void *data, bool blocked)
521 /* Do something with blocked...*/
523 * blocked == false is on
524 * blocked == true is off
527 set_device_state("0", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
529 set_device_state("1", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
534 static int rfkill_wlan_set(void *data, bool blocked)
537 set_device_state("0", 0, MSI_STANDARD_EC_WLAN_MASK);
539 set_device_state("1", 0, MSI_STANDARD_EC_WLAN_MASK);
544 static int rfkill_threeg_set(void *data, bool blocked)
547 set_device_state("0", 0, MSI_STANDARD_EC_3G_MASK);
549 set_device_state("1", 0, MSI_STANDARD_EC_3G_MASK);
554 static struct rfkill_ops rfkill_bluetooth_ops = {
555 .set_block = rfkill_bluetooth_set
558 static struct rfkill_ops rfkill_wlan_ops = {
559 .set_block = rfkill_wlan_set
562 static struct rfkill_ops rfkill_threeg_ops = {
563 .set_block = rfkill_threeg_set
566 static void rfkill_cleanup(void)
569 rfkill_unregister(rfk_bluetooth);
570 rfkill_destroy(rfk_bluetooth);
574 rfkill_unregister(rfk_threeg);
575 rfkill_destroy(rfk_threeg);
579 rfkill_unregister(rfk_wlan);
580 rfkill_destroy(rfk_wlan);
584 static int rfkill_init(struct platform_device *sdev)
589 rfk_bluetooth = rfkill_alloc("msi-bluetooth", &sdev->dev,
590 RFKILL_TYPE_BLUETOOTH,
591 &rfkill_bluetooth_ops, NULL);
592 if (!rfk_bluetooth) {
596 retval = rfkill_register(rfk_bluetooth);
600 rfk_wlan = rfkill_alloc("msi-wlan", &sdev->dev, RFKILL_TYPE_WLAN,
601 &rfkill_wlan_ops, NULL);
606 retval = rfkill_register(rfk_wlan);
611 rfk_threeg = rfkill_alloc("msi-threeg", &sdev->dev,
612 RFKILL_TYPE_WWAN, &rfkill_threeg_ops, NULL);
617 retval = rfkill_register(rfk_threeg);
625 rfkill_destroy(rfk_threeg);
627 rfkill_unregister(rfk_wlan);
629 rfkill_destroy(rfk_wlan);
631 rfkill_unregister(rfk_bluetooth);
633 rfkill_destroy(rfk_bluetooth);
638 static int msi_laptop_resume(struct platform_device *device)
646 /* set load SCM to disable hardware control by fn key */
647 result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
651 result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
652 data | MSI_STANDARD_EC_SCM_LOAD_MASK);
659 static int load_scm_model_init(struct platform_device *sdev)
664 /* allow userland write sysfs file */
665 dev_attr_bluetooth.store = store_bluetooth;
666 dev_attr_wlan.store = store_wlan;
667 dev_attr_threeg.store = store_threeg;
668 dev_attr_bluetooth.attr.mode |= S_IWUSR;
669 dev_attr_wlan.attr.mode |= S_IWUSR;
670 dev_attr_threeg.attr.mode |= S_IWUSR;
672 /* disable hardware control by fn key */
673 result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
677 result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
678 data | MSI_STANDARD_EC_SCM_LOAD_MASK);
683 result = rfkill_init(sdev);
690 static int __init msi_init(void)
697 if (force || dmi_check_system(msi_dmi_table))
703 if (!old_ec_model && dmi_check_system(msi_load_scm_models_dmi_table))
706 if (auto_brightness < 0 || auto_brightness > 2)
709 /* Register backlight stuff */
711 if (acpi_video_backlight_support()) {
712 printk(KERN_INFO "MSI: Brightness ignored, must be controlled "
713 "by ACPI video driver\n");
715 struct backlight_properties props;
716 memset(&props, 0, sizeof(struct backlight_properties));
717 props.max_brightness = MSI_LCD_LEVEL_MAX - 1;
718 msibl_device = backlight_device_register("msi-laptop-bl", NULL,
721 if (IS_ERR(msibl_device))
722 return PTR_ERR(msibl_device);
725 ret = platform_driver_register(&msipf_driver);
729 /* Register platform stuff */
731 msipf_device = platform_device_alloc("msi-laptop-pf", -1);
734 goto fail_platform_driver;
737 ret = platform_device_add(msipf_device);
739 goto fail_platform_device1;
741 if (load_scm_model && (load_scm_model_init(msipf_device) < 0)) {
743 goto fail_platform_device1;
746 ret = sysfs_create_group(&msipf_device->dev.kobj, &msipf_attribute_group);
748 goto fail_platform_device2;
752 ret = device_create_file(&msipf_device->dev,
755 goto fail_platform_device2;
758 /* Disable automatic brightness control by default because
759 * this module was probably loaded to do brightness control in
762 if (auto_brightness != 2)
763 set_auto_brightness(auto_brightness);
765 printk(KERN_INFO "msi-laptop: driver "MSI_DRIVER_VERSION" successfully loaded.\n");
769 fail_platform_device2:
771 platform_device_del(msipf_device);
773 fail_platform_device1:
775 platform_device_put(msipf_device);
777 fail_platform_driver:
779 platform_driver_unregister(&msipf_driver);
783 backlight_device_unregister(msibl_device);
788 static void __exit msi_cleanup(void)
791 sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
792 if (!old_ec_model && threeg_exists)
793 device_remove_file(&msipf_device->dev, &dev_attr_threeg);
794 platform_device_unregister(msipf_device);
795 platform_driver_unregister(&msipf_driver);
796 backlight_device_unregister(msibl_device);
800 /* Enable automatic brightness control again */
801 if (auto_brightness != 2)
802 set_auto_brightness(1);
804 printk(KERN_INFO "msi-laptop: driver unloaded.\n");
807 module_init(msi_init);
808 module_exit(msi_cleanup);
810 MODULE_AUTHOR("Lennart Poettering");
811 MODULE_DESCRIPTION("MSI Laptop Support");
812 MODULE_VERSION(MSI_DRIVER_VERSION);
813 MODULE_LICENSE("GPL");
815 MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
816 MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
817 MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
818 MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
819 MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");
820 MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N051:*");
821 MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N014:*");
822 MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnCR620:*");