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);
83 module_param(force, bool, 0);
84 MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
86 static int auto_brightness;
87 module_param(auto_brightness, int, 0);
88 MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
90 static bool old_ec_model;
91 static int wlan_s, bluetooth_s, threeg_s;
93 /* Some MSI 3G netbook only have one fn key to control Wlan/Bluetooth/3G,
94 * those netbook will load the SCM (windows app) to disable the original
95 * Wlan/Bluetooth control by BIOS when user press fn key, then control
96 * Wlan/Bluetooth/3G by SCM (software control by OS). Without SCM, user
97 * cann't on/off 3G module on those 3G netbook.
98 * On Linux, msi-laptop driver will do the same thing to disable the
99 * original BIOS control, then might need use HAL or other userland
100 * application to do the software control that simulate with SCM.
101 * e.g. MSI N034 netbook
103 static bool load_scm_model;
104 static struct rfkill *rfk_wlan, *rfk_bluetooth, *rfk_threeg;
106 /* Hardware access */
108 static int set_lcd_level(int level)
112 if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
116 buf[1] = (u8) (level*31);
118 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf), NULL, 0, 1);
121 static int get_lcd_level(void)
126 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
130 return (int) rdata / 31;
133 static int get_auto_brightness(void)
138 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
142 return !!(rdata & 8);
145 static int set_auto_brightness(int enable)
152 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1, &rdata, 1, 1);
157 wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
159 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2, NULL, 0, 1);
162 static ssize_t set_device_state(const char *buf, size_t count, u8 mask)
168 if (sscanf(buf, "%i", &status) != 1 || (status < 0 || status > 1))
171 /* read current device state */
172 result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
176 if (!!(rdata & mask) != status) {
177 /* reverse device bit */
179 wdata = rdata & ~mask;
181 wdata = rdata | mask;
183 result = ec_write(MSI_STANDARD_EC_COMMAND_ADDRESS, wdata);
191 static int get_wireless_state(int *wlan, int *bluetooth)
196 result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1, 1);
201 *wlan = !!(rdata & 8);
204 *bluetooth = !!(rdata & 128);
209 static int get_wireless_state_ec_standard(void)
214 result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
218 wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);
220 bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);
222 threeg_s = !!(rdata & MSI_STANDARD_EC_3G_MASK);
227 /* Backlight device stuff */
229 static int bl_get_brightness(struct backlight_device *b)
231 return get_lcd_level();
235 static int bl_update_status(struct backlight_device *b)
237 return set_lcd_level(b->props.brightness);
240 static struct backlight_ops msibl_ops = {
241 .get_brightness = bl_get_brightness,
242 .update_status = bl_update_status,
245 static struct backlight_device *msibl_device;
247 /* Platform device */
249 static ssize_t show_wlan(struct device *dev,
250 struct device_attribute *attr, char *buf)
256 ret = get_wireless_state(&enabled, NULL);
258 ret = get_wireless_state_ec_standard();
264 return sprintf(buf, "%i\n", enabled);
267 static ssize_t store_wlan(struct device *dev,
268 struct device_attribute *attr, const char *buf, size_t count)
270 return set_device_state(buf, count, MSI_STANDARD_EC_WLAN_MASK);
273 static ssize_t show_bluetooth(struct device *dev,
274 struct device_attribute *attr, char *buf)
280 ret = get_wireless_state(NULL, &enabled);
282 ret = get_wireless_state_ec_standard();
283 enabled = bluetooth_s;
288 return sprintf(buf, "%i\n", enabled);
291 static ssize_t store_bluetooth(struct device *dev,
292 struct device_attribute *attr, const char *buf, size_t count)
294 return set_device_state(buf, count, MSI_STANDARD_EC_BLUETOOTH_MASK);
297 static ssize_t show_threeg(struct device *dev,
298 struct device_attribute *attr, char *buf)
303 /* old msi ec not support 3G */
307 ret = get_wireless_state_ec_standard();
311 return sprintf(buf, "%i\n", threeg_s);
314 static ssize_t store_threeg(struct device *dev,
315 struct device_attribute *attr, const char *buf, size_t count)
317 return set_device_state(buf, count, MSI_STANDARD_EC_3G_MASK);
320 static ssize_t show_lcd_level(struct device *dev,
321 struct device_attribute *attr, char *buf)
326 ret = get_lcd_level();
330 return sprintf(buf, "%i\n", ret);
333 static ssize_t store_lcd_level(struct device *dev,
334 struct device_attribute *attr, const char *buf, size_t count)
339 if (sscanf(buf, "%i", &level) != 1 || (level < 0 || level >= MSI_LCD_LEVEL_MAX))
342 ret = set_lcd_level(level);
349 static ssize_t show_auto_brightness(struct device *dev,
350 struct device_attribute *attr, char *buf)
355 ret = get_auto_brightness();
359 return sprintf(buf, "%i\n", ret);
362 static ssize_t store_auto_brightness(struct device *dev,
363 struct device_attribute *attr, const char *buf, size_t count)
368 if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
371 ret = set_auto_brightness(enable);
378 static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
379 static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness, store_auto_brightness);
380 static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
381 static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
382 static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
384 static struct attribute *msipf_attributes[] = {
385 &dev_attr_lcd_level.attr,
386 &dev_attr_auto_brightness.attr,
387 &dev_attr_bluetooth.attr,
392 static struct attribute_group msipf_attribute_group = {
393 .attrs = msipf_attributes
396 static struct platform_driver msipf_driver = {
398 .name = "msi-laptop-pf",
399 .owner = THIS_MODULE,
401 .resume = msi_laptop_resume,
404 static struct platform_device *msipf_device;
408 static int dmi_check_cb(const struct dmi_system_id *id)
410 printk("msi-laptop: Identified laptop model '%s'.\n", id->ident);
414 static struct dmi_system_id __initdata msi_dmi_table[] = {
418 DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
419 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
420 DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
421 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
423 .callback = dmi_check_cb
428 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
429 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
430 DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
431 DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
433 .callback = dmi_check_cb
438 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
439 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
440 DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
441 DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
443 .callback = dmi_check_cb
446 .ident = "Medion MD96100",
448 DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
449 DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
450 DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
451 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
453 .callback = dmi_check_cb
458 static struct dmi_system_id __initdata msi_load_scm_models_dmi_table[] = {
462 DMI_MATCH(DMI_SYS_VENDOR,
463 "MICRO-STAR INTERNATIONAL CO., LTD"),
464 DMI_MATCH(DMI_PRODUCT_NAME, "MS-N034"),
465 DMI_MATCH(DMI_CHASSIS_VENDOR,
466 "MICRO-STAR INTERNATIONAL CO., LTD")
468 .callback = dmi_check_cb
473 static int rfkill_bluetooth_set(void *data, bool blocked)
475 /* Do something with blocked...*/
477 * blocked == false is on
478 * blocked == true is off
481 set_device_state("0", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
483 set_device_state("1", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
488 static int rfkill_wlan_set(void *data, bool blocked)
491 set_device_state("0", 0, MSI_STANDARD_EC_WLAN_MASK);
493 set_device_state("1", 0, MSI_STANDARD_EC_WLAN_MASK);
498 static int rfkill_threeg_set(void *data, bool blocked)
501 set_device_state("0", 0, MSI_STANDARD_EC_3G_MASK);
503 set_device_state("1", 0, MSI_STANDARD_EC_3G_MASK);
508 static struct rfkill_ops rfkill_bluetooth_ops = {
509 .set_block = rfkill_bluetooth_set
512 static struct rfkill_ops rfkill_wlan_ops = {
513 .set_block = rfkill_wlan_set
516 static struct rfkill_ops rfkill_threeg_ops = {
517 .set_block = rfkill_threeg_set
520 static void rfkill_cleanup(void)
523 rfkill_unregister(rfk_bluetooth);
524 rfkill_destroy(rfk_bluetooth);
528 rfkill_unregister(rfk_threeg);
529 rfkill_destroy(rfk_threeg);
533 rfkill_unregister(rfk_wlan);
534 rfkill_destroy(rfk_wlan);
538 static int rfkill_init(struct platform_device *sdev)
543 rfk_bluetooth = rfkill_alloc("msi-bluetooth", &sdev->dev,
544 RFKILL_TYPE_BLUETOOTH,
545 &rfkill_bluetooth_ops, NULL);
546 if (!rfk_bluetooth) {
550 retval = rfkill_register(rfk_bluetooth);
554 rfk_wlan = rfkill_alloc("msi-wlan", &sdev->dev, RFKILL_TYPE_WLAN,
555 &rfkill_wlan_ops, NULL);
560 retval = rfkill_register(rfk_wlan);
564 rfk_threeg = rfkill_alloc("msi-threeg", &sdev->dev, RFKILL_TYPE_WWAN,
565 &rfkill_threeg_ops, NULL);
570 retval = rfkill_register(rfk_threeg);
577 rfkill_destroy(rfk_threeg);
579 rfkill_unregister(rfk_wlan);
581 rfkill_destroy(rfk_wlan);
583 rfkill_unregister(rfk_bluetooth);
585 rfkill_destroy(rfk_bluetooth);
590 static int msi_laptop_resume(struct platform_device *device)
598 /* set load SCM to disable hardware control by fn key */
599 result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
603 result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
604 data | MSI_STANDARD_EC_SCM_LOAD_MASK);
611 static int load_scm_model_init(struct platform_device *sdev)
616 /* allow userland write sysfs file */
617 dev_attr_bluetooth.store = store_bluetooth;
618 dev_attr_wlan.store = store_wlan;
619 dev_attr_threeg.store = store_threeg;
620 dev_attr_bluetooth.attr.mode |= S_IWUSR;
621 dev_attr_wlan.attr.mode |= S_IWUSR;
622 dev_attr_threeg.attr.mode |= S_IWUSR;
624 /* disable hardware control by fn key */
625 result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
629 result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
630 data | MSI_STANDARD_EC_SCM_LOAD_MASK);
635 result = rfkill_init(sdev);
642 static int __init msi_init(void)
649 if (force || dmi_check_system(msi_dmi_table))
652 if (!old_ec_model && dmi_check_system(msi_load_scm_models_dmi_table))
655 if (auto_brightness < 0 || auto_brightness > 2)
658 /* Register backlight stuff */
660 if (acpi_video_backlight_support()) {
661 printk(KERN_INFO "MSI: Brightness ignored, must be controlled "
662 "by ACPI video driver\n");
664 msibl_device = backlight_device_register("msi-laptop-bl", NULL,
666 if (IS_ERR(msibl_device))
667 return PTR_ERR(msibl_device);
668 msibl_device->props.max_brightness = MSI_LCD_LEVEL_MAX-1;
671 ret = platform_driver_register(&msipf_driver);
675 /* Register platform stuff */
677 msipf_device = platform_device_alloc("msi-laptop-pf", -1);
680 goto fail_platform_driver;
683 ret = platform_device_add(msipf_device);
685 goto fail_platform_device1;
687 if (load_scm_model && (load_scm_model_init(msipf_device) < 0)) {
689 goto fail_platform_device1;
692 ret = sysfs_create_group(&msipf_device->dev.kobj, &msipf_attribute_group);
694 goto fail_platform_device2;
697 ret = device_create_file(&msipf_device->dev, &dev_attr_threeg);
699 goto fail_platform_device2;
702 /* Disable automatic brightness control by default because
703 * this module was probably loaded to do brightness control in
706 if (auto_brightness != 2)
707 set_auto_brightness(auto_brightness);
709 printk(KERN_INFO "msi-laptop: driver "MSI_DRIVER_VERSION" successfully loaded.\n");
713 fail_platform_device2:
715 platform_device_del(msipf_device);
717 fail_platform_device1:
719 platform_device_put(msipf_device);
721 fail_platform_driver:
723 platform_driver_unregister(&msipf_driver);
727 backlight_device_unregister(msibl_device);
732 static void __exit msi_cleanup(void)
735 sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
737 device_remove_file(&msipf_device->dev, &dev_attr_threeg);
738 platform_device_unregister(msipf_device);
739 platform_driver_unregister(&msipf_driver);
740 backlight_device_unregister(msibl_device);
744 /* Enable automatic brightness control again */
745 if (auto_brightness != 2)
746 set_auto_brightness(1);
748 printk(KERN_INFO "msi-laptop: driver unloaded.\n");
751 module_init(msi_init);
752 module_exit(msi_cleanup);
754 MODULE_AUTHOR("Lennart Poettering");
755 MODULE_DESCRIPTION("MSI Laptop Support");
756 MODULE_VERSION(MSI_DRIVER_VERSION);
757 MODULE_LICENSE("GPL");
759 MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
760 MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
761 MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
762 MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
763 MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");