blob: 1784d5588d41b2280a07510f3018c25876e38abd [file] [log] [blame]
Lennart Poettering8c4c7312006-10-06 01:27:02 -04001/*-*-linux-c-*-*/
2
3/*
4 Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
5
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.
10
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.
15
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
19 02110-1301, USA.
20 */
21
22/*
23 * msi-laptop.c - MSI S270 laptop support. This laptop is sold under
24 * various brands, including "Cytron/TCM/Medion/Tchibo MD96100".
25 *
Lennart Poettering4f5c7912007-05-08 22:07:02 +020026 * Driver also supports S271, S420 models.
27 *
Lennart Poettering8c4c7312006-10-06 01:27:02 -040028 * This driver exports a few files in /sys/devices/platform/msi-laptop-pf/:
29 *
30 * lcd_level - Screen brightness: contains a single integer in the
31 * range 0..8. (rw)
32 *
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)
37 *
38 * wlan - WLAN subsystem enabled: contains either 0 or 1. (ro)
39 *
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)
43 *
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/.
47 *
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.
52 */
53
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>
Lennart Poettering8c4c7312006-10-06 01:27:02 -040061
62#define MSI_DRIVER_VERSION "0.5"
63
64#define MSI_LCD_LEVEL_MAX 9
65
66#define MSI_EC_COMMAND_WIRELESS 0x10
67#define MSI_EC_COMMAND_LCD_LEVEL 0x11
68
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +080069#define MSI_STANDARD_EC_COMMAND_ADDRESS 0x2e
70#define MSI_STANDARD_EC_BLUETOOTH_MASK (1 << 0)
71#define MSI_STANDARD_EC_WEBCAM_MASK (1 << 1)
72#define MSI_STANDARD_EC_WLAN_MASK (1 << 3)
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +080073#define MSI_STANDARD_EC_3G_MASK (1 << 4)
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +080074
Lennart Poettering8c4c7312006-10-06 01:27:02 -040075static int force;
76module_param(force, bool, 0);
77MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
78
79static int auto_brightness;
80module_param(auto_brightness, int, 0);
81MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
82
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +080083static bool old_ec_model;
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +080084static int wlan_s, bluetooth_s, threeg_s;
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +080085
Lennart Poettering8c4c7312006-10-06 01:27:02 -040086/* Hardware access */
87
88static int set_lcd_level(int level)
89{
90 u8 buf[2];
91
92 if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
93 return -EINVAL;
94
95 buf[0] = 0x80;
96 buf[1] = (u8) (level*31);
97
Lennart Poettering00eb43a2007-05-04 14:16:19 +020098 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf), NULL, 0, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -040099}
100
101static int get_lcd_level(void)
102{
103 u8 wdata = 0, rdata;
104 int result;
105
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200106 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400107 if (result < 0)
108 return result;
109
110 return (int) rdata / 31;
111}
112
113static int get_auto_brightness(void)
114{
115 u8 wdata = 4, rdata;
116 int result;
117
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200118 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400119 if (result < 0)
120 return result;
121
122 return !!(rdata & 8);
123}
124
125static int set_auto_brightness(int enable)
126{
127 u8 wdata[2], rdata;
128 int result;
129
130 wdata[0] = 4;
131
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200132 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1, &rdata, 1, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400133 if (result < 0)
134 return result;
135
136 wdata[0] = 0x84;
137 wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
138
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200139 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2, NULL, 0, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400140}
141
142static int get_wireless_state(int *wlan, int *bluetooth)
143{
144 u8 wdata = 0, rdata;
145 int result;
146
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200147 result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400148 if (result < 0)
149 return -1;
150
151 if (wlan)
152 *wlan = !!(rdata & 8);
153
154 if (bluetooth)
155 *bluetooth = !!(rdata & 128);
156
157 return 0;
158}
159
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800160static int get_wireless_state_ec_standard(void)
161{
162 u8 rdata;
163 int result;
164
165 result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
166 if (result < 0)
167 return -1;
168
169 wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);
170
171 bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);
172
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800173 threeg_s = !!(rdata & MSI_STANDARD_EC_3G_MASK);
174
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800175 return 0;
176}
177
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400178/* Backlight device stuff */
179
180static int bl_get_brightness(struct backlight_device *b)
181{
182 return get_lcd_level();
183}
184
185
186static int bl_update_status(struct backlight_device *b)
187{
Richard Purdie599a52d2007-02-10 23:07:48 +0000188 return set_lcd_level(b->props.brightness);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400189}
190
Richard Purdie599a52d2007-02-10 23:07:48 +0000191static struct backlight_ops msibl_ops = {
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400192 .get_brightness = bl_get_brightness,
193 .update_status = bl_update_status,
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400194};
195
196static struct backlight_device *msibl_device;
197
198/* Platform device */
199
200static ssize_t show_wlan(struct device *dev,
201 struct device_attribute *attr, char *buf)
202{
203
204 int ret, enabled;
205
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800206 if (old_ec_model) {
207 ret = get_wireless_state(&enabled, NULL);
208 } else {
209 ret = get_wireless_state_ec_standard();
210 enabled = wlan_s;
211 }
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400212 if (ret < 0)
213 return ret;
214
215 return sprintf(buf, "%i\n", enabled);
216}
217
218static ssize_t show_bluetooth(struct device *dev,
219 struct device_attribute *attr, char *buf)
220{
221
222 int ret, enabled;
223
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800224 if (old_ec_model) {
225 ret = get_wireless_state(NULL, &enabled);
226 } else {
227 ret = get_wireless_state_ec_standard();
228 enabled = bluetooth_s;
229 }
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400230 if (ret < 0)
231 return ret;
232
233 return sprintf(buf, "%i\n", enabled);
234}
235
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800236static ssize_t show_threeg(struct device *dev,
237 struct device_attribute *attr, char *buf)
238{
239
240 int ret;
241
242 /* old msi ec not support 3G */
243 if (old_ec_model)
244 return -1;
245
246 ret = get_wireless_state_ec_standard();
247 if (ret < 0)
248 return ret;
249
250 return sprintf(buf, "%i\n", threeg_s);
251}
252
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400253static ssize_t show_lcd_level(struct device *dev,
254 struct device_attribute *attr, char *buf)
255{
256
257 int ret;
258
259 ret = get_lcd_level();
260 if (ret < 0)
261 return ret;
262
263 return sprintf(buf, "%i\n", ret);
264}
265
266static ssize_t store_lcd_level(struct device *dev,
267 struct device_attribute *attr, const char *buf, size_t count)
268{
269
270 int level, ret;
271
272 if (sscanf(buf, "%i", &level) != 1 || (level < 0 || level >= MSI_LCD_LEVEL_MAX))
273 return -EINVAL;
274
275 ret = set_lcd_level(level);
276 if (ret < 0)
277 return ret;
278
279 return count;
280}
281
282static ssize_t show_auto_brightness(struct device *dev,
283 struct device_attribute *attr, char *buf)
284{
285
286 int ret;
287
288 ret = get_auto_brightness();
289 if (ret < 0)
290 return ret;
291
292 return sprintf(buf, "%i\n", ret);
293}
294
295static ssize_t store_auto_brightness(struct device *dev,
296 struct device_attribute *attr, const char *buf, size_t count)
297{
298
299 int enable, ret;
300
301 if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
302 return -EINVAL;
303
304 ret = set_auto_brightness(enable);
305 if (ret < 0)
306 return ret;
307
308 return count;
309}
310
311static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
312static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness, store_auto_brightness);
313static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
314static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800315static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400316
317static struct attribute *msipf_attributes[] = {
318 &dev_attr_lcd_level.attr,
319 &dev_attr_auto_brightness.attr,
320 &dev_attr_bluetooth.attr,
321 &dev_attr_wlan.attr,
322 NULL
323};
324
325static struct attribute_group msipf_attribute_group = {
326 .attrs = msipf_attributes
327};
328
329static struct platform_driver msipf_driver = {
330 .driver = {
331 .name = "msi-laptop-pf",
332 .owner = THIS_MODULE,
333 }
334};
335
336static struct platform_device *msipf_device;
337
338/* Initialization */
339
Jeff Garzik18552562007-10-03 15:15:40 -0400340static int dmi_check_cb(const struct dmi_system_id *id)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200341{
342 printk("msi-laptop: Identified laptop model '%s'.\n", id->ident);
343 return 0;
344}
345
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400346static struct dmi_system_id __initdata msi_dmi_table[] = {
347 {
348 .ident = "MSI S270",
349 .matches = {
350 DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
351 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200352 DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
353 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
354 },
355 .callback = dmi_check_cb
356 },
357 {
358 .ident = "MSI S271",
359 .matches = {
360 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
361 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
362 DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
363 DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
364 },
365 .callback = dmi_check_cb
366 },
367 {
368 .ident = "MSI S420",
369 .matches = {
370 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
371 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
372 DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
373 DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
374 },
375 .callback = dmi_check_cb
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400376 },
377 {
378 .ident = "Medion MD96100",
379 .matches = {
380 DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
381 DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200382 DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
383 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
384 },
385 .callback = dmi_check_cb
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400386 },
387 { }
388};
389
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400390static int __init msi_init(void)
391{
392 int ret;
393
394 if (acpi_disabled)
395 return -ENODEV;
396
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800397 if (force || dmi_check_system(msi_dmi_table))
398 old_ec_model = 1;
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400399
400 if (auto_brightness < 0 || auto_brightness > 2)
401 return -EINVAL;
402
403 /* Register backlight stuff */
404
Thomas Renningera598c822008-08-01 17:38:01 +0200405 if (acpi_video_backlight_support()) {
406 printk(KERN_INFO "MSI: Brightness ignored, must be controlled "
407 "by ACPI video driver\n");
408 } else {
409 msibl_device = backlight_device_register("msi-laptop-bl", NULL,
410 NULL, &msibl_ops);
411 if (IS_ERR(msibl_device))
412 return PTR_ERR(msibl_device);
413 msibl_device->props.max_brightness = MSI_LCD_LEVEL_MAX-1;
414 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000415
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400416 ret = platform_driver_register(&msipf_driver);
417 if (ret)
418 goto fail_backlight;
419
420 /* Register platform stuff */
421
422 msipf_device = platform_device_alloc("msi-laptop-pf", -1);
423 if (!msipf_device) {
424 ret = -ENOMEM;
425 goto fail_platform_driver;
426 }
427
428 ret = platform_device_add(msipf_device);
429 if (ret)
430 goto fail_platform_device1;
431
432 ret = sysfs_create_group(&msipf_device->dev.kobj, &msipf_attribute_group);
433 if (ret)
434 goto fail_platform_device2;
435
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800436 if (!old_ec_model) {
437 ret = device_create_file(&msipf_device->dev, &dev_attr_threeg);
438 if (ret)
439 goto fail_platform_device2;
440 }
441
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400442 /* Disable automatic brightness control by default because
443 * this module was probably loaded to do brightness control in
444 * software. */
445
446 if (auto_brightness != 2)
447 set_auto_brightness(auto_brightness);
448
449 printk(KERN_INFO "msi-laptop: driver "MSI_DRIVER_VERSION" successfully loaded.\n");
450
451 return 0;
452
453fail_platform_device2:
454
455 platform_device_del(msipf_device);
456
457fail_platform_device1:
458
459 platform_device_put(msipf_device);
460
461fail_platform_driver:
462
463 platform_driver_unregister(&msipf_driver);
464
465fail_backlight:
466
467 backlight_device_unregister(msibl_device);
468
469 return ret;
470}
471
472static void __exit msi_cleanup(void)
473{
474
475 sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800476 if (!old_ec_model)
477 device_remove_file(&msipf_device->dev, &dev_attr_threeg);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400478 platform_device_unregister(msipf_device);
479 platform_driver_unregister(&msipf_driver);
480 backlight_device_unregister(msibl_device);
481
482 /* Enable automatic brightness control again */
483 if (auto_brightness != 2)
484 set_auto_brightness(1);
485
486 printk(KERN_INFO "msi-laptop: driver unloaded.\n");
487}
488
489module_init(msi_init);
490module_exit(msi_cleanup);
491
492MODULE_AUTHOR("Lennart Poettering");
493MODULE_DESCRIPTION("MSI Laptop Support");
494MODULE_VERSION(MSI_DRIVER_VERSION);
495MODULE_LICENSE("GPL");
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200496
497MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
498MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
499MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
500MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800501MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");