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 * This driver exports a few files in /sys/devices/platform/msi-laptop-pf/:
28 * lcd_level - Screen brightness: contains a single integer in the
31 * auto_brightness - Enable automatic brightness control: contains
32 * either 0 or 1. If set to 1 the hardware adjusts the screen
33 * brightness automatically when the power cord is
34 * plugged/unplugged. (rw)
36 * wlan - WLAN subsystem enabled: contains either 0 or 1. (ro)
38 * bluetooth - Bluetooth subsystem enabled: contains either 0 or 1
39 * Please note that this file is constantly 0 if no Bluetooth
40 * hardware is available. (ro)
42 * In addition to these platform device attributes the driver
43 * registers itself in the Linux backlight control subsystem and is
44 * available to userspace under /sys/class/backlight/msi-laptop-bl/.
46 * This driver might work on other laptops produced by MSI. If you
47 * want to try it you can pass force=1 as argument to the module which
48 * will force it to load even when the DMI data doesn't identify the
49 * laptop as MSI S270. YMMV.
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/init.h>
55 #include <linux/acpi.h>
56 #include <linux/dmi.h>
57 #include <linux/backlight.h>
58 #include <linux/platform_device.h>
59 #include <linux/autoconf.h>
61 #define MSI_DRIVER_VERSION "0.5"
63 #define MSI_LCD_LEVEL_MAX 9
65 #define MSI_EC_COMMAND_WIRELESS 0x10
66 #define MSI_EC_COMMAND_LCD_LEVEL 0x11
69 module_param(force, bool, 0);
70 MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
72 static int auto_brightness;
73 module_param(auto_brightness, int, 0);
74 MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
78 static int set_lcd_level(int level)
82 if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
86 buf[1] = (u8) (level*31);
88 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf), NULL, 0, 1);
91 static int get_lcd_level(void)
96 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
100 return (int) rdata / 31;
103 static int get_auto_brightness(void)
108 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
112 return !!(rdata & 8);
115 static int set_auto_brightness(int enable)
122 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1, &rdata, 1, 1);
127 wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
129 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2, NULL, 0, 1);
132 static int get_wireless_state(int *wlan, int *bluetooth)
137 result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1, 1);
142 *wlan = !!(rdata & 8);
145 *bluetooth = !!(rdata & 128);
150 /* Backlight device stuff */
152 static int bl_get_brightness(struct backlight_device *b)
154 return get_lcd_level();
158 static int bl_update_status(struct backlight_device *b)
160 return set_lcd_level(b->props.brightness);
163 static struct backlight_ops msibl_ops = {
164 .get_brightness = bl_get_brightness,
165 .update_status = bl_update_status,
168 static struct backlight_device *msibl_device;
170 /* Platform device */
172 static ssize_t show_wlan(struct device *dev,
173 struct device_attribute *attr, char *buf)
178 ret = get_wireless_state(&enabled, NULL);
182 return sprintf(buf, "%i\n", enabled);
185 static ssize_t show_bluetooth(struct device *dev,
186 struct device_attribute *attr, char *buf)
191 ret = get_wireless_state(NULL, &enabled);
195 return sprintf(buf, "%i\n", enabled);
198 static ssize_t show_lcd_level(struct device *dev,
199 struct device_attribute *attr, char *buf)
204 ret = get_lcd_level();
208 return sprintf(buf, "%i\n", ret);
211 static ssize_t store_lcd_level(struct device *dev,
212 struct device_attribute *attr, const char *buf, size_t count)
217 if (sscanf(buf, "%i", &level) != 1 || (level < 0 || level >= MSI_LCD_LEVEL_MAX))
220 ret = set_lcd_level(level);
227 static ssize_t show_auto_brightness(struct device *dev,
228 struct device_attribute *attr, char *buf)
233 ret = get_auto_brightness();
237 return sprintf(buf, "%i\n", ret);
240 static ssize_t store_auto_brightness(struct device *dev,
241 struct device_attribute *attr, const char *buf, size_t count)
246 if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
249 ret = set_auto_brightness(enable);
256 static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
257 static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness, store_auto_brightness);
258 static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
259 static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
261 static struct attribute *msipf_attributes[] = {
262 &dev_attr_lcd_level.attr,
263 &dev_attr_auto_brightness.attr,
264 &dev_attr_bluetooth.attr,
269 static struct attribute_group msipf_attribute_group = {
270 .attrs = msipf_attributes
273 static struct platform_driver msipf_driver = {
275 .name = "msi-laptop-pf",
276 .owner = THIS_MODULE,
280 static struct platform_device *msipf_device;
284 static struct dmi_system_id __initdata msi_dmi_table[] = {
288 DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
289 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
293 .ident = "Medion MD96100",
295 DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
296 DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
303 static int __init msi_init(void)
310 if (!force && !dmi_check_system(msi_dmi_table))
313 if (auto_brightness < 0 || auto_brightness > 2)
316 /* Register backlight stuff */
318 msibl_device = backlight_device_register("msi-laptop-bl", NULL, NULL,
320 if (IS_ERR(msibl_device))
321 return PTR_ERR(msibl_device);
323 msibl_device->props.max_brightness = MSI_LCD_LEVEL_MAX-1,
325 ret = platform_driver_register(&msipf_driver);
329 /* Register platform stuff */
331 msipf_device = platform_device_alloc("msi-laptop-pf", -1);
334 goto fail_platform_driver;
337 ret = platform_device_add(msipf_device);
339 goto fail_platform_device1;
341 ret = sysfs_create_group(&msipf_device->dev.kobj, &msipf_attribute_group);
343 goto fail_platform_device2;
345 /* Disable automatic brightness control by default because
346 * this module was probably loaded to do brightness control in
349 if (auto_brightness != 2)
350 set_auto_brightness(auto_brightness);
352 printk(KERN_INFO "msi-laptop: driver "MSI_DRIVER_VERSION" successfully loaded.\n");
356 fail_platform_device2:
358 platform_device_del(msipf_device);
360 fail_platform_device1:
362 platform_device_put(msipf_device);
364 fail_platform_driver:
366 platform_driver_unregister(&msipf_driver);
370 backlight_device_unregister(msibl_device);
375 static void __exit msi_cleanup(void)
378 sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
379 platform_device_unregister(msipf_device);
380 platform_driver_unregister(&msipf_driver);
381 backlight_device_unregister(msibl_device);
383 /* Enable automatic brightness control again */
384 if (auto_brightness != 2)
385 set_auto_brightness(1);
387 printk(KERN_INFO "msi-laptop: driver unloaded.\n");
390 module_init(msi_init);
391 module_exit(msi_cleanup);
393 MODULE_AUTHOR("Lennart Poettering");
394 MODULE_DESCRIPTION("MSI Laptop Support");
395 MODULE_VERSION(MSI_DRIVER_VERSION);
396 MODULE_LICENSE("GPL");