blob: 34bec2e26843a90949abf87781bbb57b90ffe9e5 [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>
Lee, Chun-Yi472ea122010-01-22 00:15:59 +080061#include <linux/rfkill.h>
Lennart Poettering8c4c7312006-10-06 01:27:02 -040062
63#define MSI_DRIVER_VERSION "0.5"
64
65#define MSI_LCD_LEVEL_MAX 9
66
67#define MSI_EC_COMMAND_WIRELESS 0x10
68#define MSI_EC_COMMAND_LCD_LEVEL 0x11
69
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +080070#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)
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +080074#define MSI_STANDARD_EC_3G_MASK (1 << 4)
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +080075
Lee, Chun-Yi472ea122010-01-22 00:15:59 +080076/* 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)
79
Lee, Chun-Yiec766272010-01-27 00:13:45 +080080static int msi_laptop_resume(struct platform_device *device);
81
Lee, Chun-Yie22388e2010-01-27 12:23:00 +080082#define MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS 0x2f
83
Lennart Poettering8c4c7312006-10-06 01:27:02 -040084static int force;
85module_param(force, bool, 0);
86MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
87
88static int auto_brightness;
89module_param(auto_brightness, int, 0);
90MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
91
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +080092static bool old_ec_model;
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +080093static int wlan_s, bluetooth_s, threeg_s;
Lee, Chun-Yie22388e2010-01-27 12:23:00 +080094static int threeg_exists;
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +080095
Lee, Chun-Yi472ea122010-01-22 00:15:59 +080096/* 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
105 */
106static bool load_scm_model;
107static struct rfkill *rfk_wlan, *rfk_bluetooth, *rfk_threeg;
108
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400109/* Hardware access */
110
111static int set_lcd_level(int level)
112{
113 u8 buf[2];
114
115 if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
116 return -EINVAL;
117
118 buf[0] = 0x80;
119 buf[1] = (u8) (level*31);
120
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200121 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf), NULL, 0, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400122}
123
124static int get_lcd_level(void)
125{
126 u8 wdata = 0, rdata;
127 int result;
128
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200129 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400130 if (result < 0)
131 return result;
132
133 return (int) rdata / 31;
134}
135
136static int get_auto_brightness(void)
137{
138 u8 wdata = 4, rdata;
139 int result;
140
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200141 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400142 if (result < 0)
143 return result;
144
145 return !!(rdata & 8);
146}
147
148static int set_auto_brightness(int enable)
149{
150 u8 wdata[2], rdata;
151 int result;
152
153 wdata[0] = 4;
154
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200155 result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1, &rdata, 1, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400156 if (result < 0)
157 return result;
158
159 wdata[0] = 0x84;
160 wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
161
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200162 return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2, NULL, 0, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400163}
164
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800165static ssize_t set_device_state(const char *buf, size_t count, u8 mask)
166{
167 int status;
168 u8 wdata = 0, rdata;
169 int result;
170
171 if (sscanf(buf, "%i", &status) != 1 || (status < 0 || status > 1))
172 return -EINVAL;
173
174 /* read current device state */
175 result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
176 if (result < 0)
177 return -EINVAL;
178
179 if (!!(rdata & mask) != status) {
180 /* reverse device bit */
181 if (rdata & mask)
182 wdata = rdata & ~mask;
183 else
184 wdata = rdata | mask;
185
186 result = ec_write(MSI_STANDARD_EC_COMMAND_ADDRESS, wdata);
187 if (result < 0)
188 return -EINVAL;
189 }
190
191 return count;
192}
193
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400194static int get_wireless_state(int *wlan, int *bluetooth)
195{
196 u8 wdata = 0, rdata;
197 int result;
198
Lennart Poettering00eb43a2007-05-04 14:16:19 +0200199 result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1, 1);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400200 if (result < 0)
201 return -1;
202
203 if (wlan)
204 *wlan = !!(rdata & 8);
205
206 if (bluetooth)
207 *bluetooth = !!(rdata & 128);
208
209 return 0;
210}
211
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800212static int get_wireless_state_ec_standard(void)
213{
214 u8 rdata;
215 int result;
216
217 result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
218 if (result < 0)
219 return -1;
220
221 wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);
222
223 bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);
224
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800225 threeg_s = !!(rdata & MSI_STANDARD_EC_3G_MASK);
226
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800227 return 0;
228}
229
Lee, Chun-Yie22388e2010-01-27 12:23:00 +0800230static int get_threeg_exists(void)
231{
232 u8 rdata;
233 int result;
234
235 result = ec_read(MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS, &rdata);
236 if (result < 0)
237 return -1;
238
239 threeg_exists = !!(rdata & MSI_STANDARD_EC_3G_MASK);
240
241 return 0;
242}
243
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400244/* Backlight device stuff */
245
246static int bl_get_brightness(struct backlight_device *b)
247{
248 return get_lcd_level();
249}
250
251
252static int bl_update_status(struct backlight_device *b)
253{
Richard Purdie599a52d2007-02-10 23:07:48 +0000254 return set_lcd_level(b->props.brightness);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400255}
256
Richard Purdie599a52d2007-02-10 23:07:48 +0000257static struct backlight_ops msibl_ops = {
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400258 .get_brightness = bl_get_brightness,
259 .update_status = bl_update_status,
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400260};
261
262static struct backlight_device *msibl_device;
263
264/* Platform device */
265
266static ssize_t show_wlan(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
269
270 int ret, enabled;
271
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800272 if (old_ec_model) {
273 ret = get_wireless_state(&enabled, NULL);
274 } else {
275 ret = get_wireless_state_ec_standard();
276 enabled = wlan_s;
277 }
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400278 if (ret < 0)
279 return ret;
280
281 return sprintf(buf, "%i\n", enabled);
282}
283
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800284static ssize_t store_wlan(struct device *dev,
285 struct device_attribute *attr, const char *buf, size_t count)
286{
287 return set_device_state(buf, count, MSI_STANDARD_EC_WLAN_MASK);
288}
289
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400290static ssize_t show_bluetooth(struct device *dev,
291 struct device_attribute *attr, char *buf)
292{
293
294 int ret, enabled;
295
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800296 if (old_ec_model) {
297 ret = get_wireless_state(NULL, &enabled);
298 } else {
299 ret = get_wireless_state_ec_standard();
300 enabled = bluetooth_s;
301 }
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400302 if (ret < 0)
303 return ret;
304
305 return sprintf(buf, "%i\n", enabled);
306}
307
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800308static ssize_t store_bluetooth(struct device *dev,
309 struct device_attribute *attr, const char *buf, size_t count)
310{
311 return set_device_state(buf, count, MSI_STANDARD_EC_BLUETOOTH_MASK);
312}
313
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800314static ssize_t show_threeg(struct device *dev,
315 struct device_attribute *attr, char *buf)
316{
317
318 int ret;
319
320 /* old msi ec not support 3G */
321 if (old_ec_model)
322 return -1;
323
324 ret = get_wireless_state_ec_standard();
325 if (ret < 0)
326 return ret;
327
328 return sprintf(buf, "%i\n", threeg_s);
329}
330
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800331static ssize_t store_threeg(struct device *dev,
332 struct device_attribute *attr, const char *buf, size_t count)
333{
334 return set_device_state(buf, count, MSI_STANDARD_EC_3G_MASK);
335}
336
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400337static ssize_t show_lcd_level(struct device *dev,
338 struct device_attribute *attr, char *buf)
339{
340
341 int ret;
342
343 ret = get_lcd_level();
344 if (ret < 0)
345 return ret;
346
347 return sprintf(buf, "%i\n", ret);
348}
349
350static ssize_t store_lcd_level(struct device *dev,
351 struct device_attribute *attr, const char *buf, size_t count)
352{
353
354 int level, ret;
355
356 if (sscanf(buf, "%i", &level) != 1 || (level < 0 || level >= MSI_LCD_LEVEL_MAX))
357 return -EINVAL;
358
359 ret = set_lcd_level(level);
360 if (ret < 0)
361 return ret;
362
363 return count;
364}
365
366static ssize_t show_auto_brightness(struct device *dev,
367 struct device_attribute *attr, char *buf)
368{
369
370 int ret;
371
372 ret = get_auto_brightness();
373 if (ret < 0)
374 return ret;
375
376 return sprintf(buf, "%i\n", ret);
377}
378
379static ssize_t store_auto_brightness(struct device *dev,
380 struct device_attribute *attr, const char *buf, size_t count)
381{
382
383 int enable, ret;
384
385 if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
386 return -EINVAL;
387
388 ret = set_auto_brightness(enable);
389 if (ret < 0)
390 return ret;
391
392 return count;
393}
394
395static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
396static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness, store_auto_brightness);
397static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
398static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800399static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400400
401static struct attribute *msipf_attributes[] = {
402 &dev_attr_lcd_level.attr,
403 &dev_attr_auto_brightness.attr,
404 &dev_attr_bluetooth.attr,
405 &dev_attr_wlan.attr,
406 NULL
407};
408
409static struct attribute_group msipf_attribute_group = {
410 .attrs = msipf_attributes
411};
412
413static struct platform_driver msipf_driver = {
414 .driver = {
415 .name = "msi-laptop-pf",
416 .owner = THIS_MODULE,
Lee, Chun-Yiec766272010-01-27 00:13:45 +0800417 },
418 .resume = msi_laptop_resume,
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400419};
420
421static struct platform_device *msipf_device;
422
423/* Initialization */
424
Jeff Garzik18552562007-10-03 15:15:40 -0400425static int dmi_check_cb(const struct dmi_system_id *id)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200426{
427 printk("msi-laptop: Identified laptop model '%s'.\n", id->ident);
428 return 0;
429}
430
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400431static struct dmi_system_id __initdata msi_dmi_table[] = {
432 {
433 .ident = "MSI S270",
434 .matches = {
435 DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
436 DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200437 DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
438 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
439 },
440 .callback = dmi_check_cb
441 },
442 {
443 .ident = "MSI S271",
444 .matches = {
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")
449 },
450 .callback = dmi_check_cb
451 },
452 {
453 .ident = "MSI S420",
454 .matches = {
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")
459 },
460 .callback = dmi_check_cb
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400461 },
462 {
463 .ident = "Medion MD96100",
464 .matches = {
465 DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
466 DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200467 DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
468 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
469 },
470 .callback = dmi_check_cb
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400471 },
Lee, Chun-Yid0a4aa22010-05-12 09:58:07 -0700472 {
473 .ident = "MSI N051",
474 .matches = {
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")
480 },
481 .callback = dmi_check_cb
482 },
483 {
484 .ident = "MSI N014",
485 .matches = {
486 DMI_MATCH(DMI_SYS_VENDOR,
487 "MICRO-STAR INTERNATIONAL CO., LTD"),
488 DMI_MATCH(DMI_PRODUCT_NAME, "MS-N014"),
489 },
490 .callback = dmi_check_cb
491 },
Lee, Chun-Yi1f27e172010-05-12 09:58:08 -0700492 {
493 .ident = "MSI CR620",
494 .matches = {
495 DMI_MATCH(DMI_SYS_VENDOR,
496 "Micro-Star International"),
497 DMI_MATCH(DMI_PRODUCT_NAME, "CR620"),
498 },
499 .callback = dmi_check_cb
500 },
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400501 { }
502};
503
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800504static struct dmi_system_id __initdata msi_load_scm_models_dmi_table[] = {
505 {
506 .ident = "MSI N034",
507 .matches = {
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")
513 },
514 .callback = dmi_check_cb
515 },
516 { }
517};
518
519static int rfkill_bluetooth_set(void *data, bool blocked)
520{
521 /* Do something with blocked...*/
522 /*
523 * blocked == false is on
524 * blocked == true is off
525 */
526 if (blocked)
527 set_device_state("0", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
528 else
529 set_device_state("1", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
530
531 return 0;
532}
533
534static int rfkill_wlan_set(void *data, bool blocked)
535{
536 if (blocked)
537 set_device_state("0", 0, MSI_STANDARD_EC_WLAN_MASK);
538 else
539 set_device_state("1", 0, MSI_STANDARD_EC_WLAN_MASK);
540
541 return 0;
542}
543
544static int rfkill_threeg_set(void *data, bool blocked)
545{
546 if (blocked)
547 set_device_state("0", 0, MSI_STANDARD_EC_3G_MASK);
548 else
549 set_device_state("1", 0, MSI_STANDARD_EC_3G_MASK);
550
551 return 0;
552}
553
554static struct rfkill_ops rfkill_bluetooth_ops = {
555 .set_block = rfkill_bluetooth_set
556};
557
558static struct rfkill_ops rfkill_wlan_ops = {
559 .set_block = rfkill_wlan_set
560};
561
562static struct rfkill_ops rfkill_threeg_ops = {
563 .set_block = rfkill_threeg_set
564};
565
566static void rfkill_cleanup(void)
567{
568 if (rfk_bluetooth) {
569 rfkill_unregister(rfk_bluetooth);
570 rfkill_destroy(rfk_bluetooth);
571 }
572
573 if (rfk_threeg) {
574 rfkill_unregister(rfk_threeg);
575 rfkill_destroy(rfk_threeg);
576 }
577
578 if (rfk_wlan) {
579 rfkill_unregister(rfk_wlan);
580 rfkill_destroy(rfk_wlan);
581 }
582}
583
Lee, Chun-Yi3bb97022010-05-12 09:58:09 -0700584static void msi_init_rfkill(struct work_struct *ignored)
585{
586 if (rfk_wlan) {
587 rfkill_set_sw_state(rfk_wlan, !wlan_s);
588 rfkill_wlan_set(NULL, !wlan_s);
589 }
590 if (rfk_bluetooth) {
591 rfkill_set_sw_state(rfk_bluetooth, !bluetooth_s);
592 rfkill_bluetooth_set(NULL, !bluetooth_s);
593 }
594 if (rfk_threeg) {
595 rfkill_set_sw_state(rfk_threeg, !threeg_s);
596 rfkill_threeg_set(NULL, !threeg_s);
597 }
598}
599static DECLARE_DELAYED_WORK(msi_rfkill_init, msi_init_rfkill);
600
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800601static int rfkill_init(struct platform_device *sdev)
602{
603 /* add rfkill */
604 int retval;
605
Lee, Chun-Yi3bb97022010-05-12 09:58:09 -0700606 /* keep the hardware wireless state */
607 get_wireless_state_ec_standard();
608
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800609 rfk_bluetooth = rfkill_alloc("msi-bluetooth", &sdev->dev,
610 RFKILL_TYPE_BLUETOOTH,
611 &rfkill_bluetooth_ops, NULL);
612 if (!rfk_bluetooth) {
613 retval = -ENOMEM;
614 goto err_bluetooth;
615 }
616 retval = rfkill_register(rfk_bluetooth);
617 if (retval)
618 goto err_bluetooth;
619
620 rfk_wlan = rfkill_alloc("msi-wlan", &sdev->dev, RFKILL_TYPE_WLAN,
621 &rfkill_wlan_ops, NULL);
622 if (!rfk_wlan) {
623 retval = -ENOMEM;
624 goto err_wlan;
625 }
626 retval = rfkill_register(rfk_wlan);
627 if (retval)
628 goto err_wlan;
629
Lee, Chun-Yie22388e2010-01-27 12:23:00 +0800630 if (threeg_exists) {
631 rfk_threeg = rfkill_alloc("msi-threeg", &sdev->dev,
632 RFKILL_TYPE_WWAN, &rfkill_threeg_ops, NULL);
633 if (!rfk_threeg) {
634 retval = -ENOMEM;
635 goto err_threeg;
636 }
637 retval = rfkill_register(rfk_threeg);
638 if (retval)
639 goto err_threeg;
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800640 }
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800641
Lee, Chun-Yi3bb97022010-05-12 09:58:09 -0700642 /* schedule to run rfkill state initial */
643 schedule_delayed_work(&msi_rfkill_init,
644 round_jiffies_relative(1 * HZ));
645
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800646 return 0;
647
648err_threeg:
649 rfkill_destroy(rfk_threeg);
650 if (rfk_wlan)
651 rfkill_unregister(rfk_wlan);
652err_wlan:
653 rfkill_destroy(rfk_wlan);
654 if (rfk_bluetooth)
655 rfkill_unregister(rfk_bluetooth);
656err_bluetooth:
657 rfkill_destroy(rfk_bluetooth);
658
659 return retval;
660}
661
Lee, Chun-Yiec766272010-01-27 00:13:45 +0800662static int msi_laptop_resume(struct platform_device *device)
663{
664 u8 data;
665 int result;
666
667 if (!load_scm_model)
668 return 0;
669
670 /* set load SCM to disable hardware control by fn key */
671 result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
672 if (result < 0)
673 return result;
674
675 result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
676 data | MSI_STANDARD_EC_SCM_LOAD_MASK);
677 if (result < 0)
678 return result;
679
680 return 0;
681}
682
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800683static int load_scm_model_init(struct platform_device *sdev)
684{
685 u8 data;
686 int result;
687
688 /* allow userland write sysfs file */
689 dev_attr_bluetooth.store = store_bluetooth;
690 dev_attr_wlan.store = store_wlan;
691 dev_attr_threeg.store = store_threeg;
692 dev_attr_bluetooth.attr.mode |= S_IWUSR;
693 dev_attr_wlan.attr.mode |= S_IWUSR;
694 dev_attr_threeg.attr.mode |= S_IWUSR;
695
696 /* disable hardware control by fn key */
697 result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
698 if (result < 0)
699 return result;
700
701 result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
702 data | MSI_STANDARD_EC_SCM_LOAD_MASK);
703 if (result < 0)
704 return result;
705
706 /* initial rfkill */
707 result = rfkill_init(sdev);
708 if (result < 0)
709 return result;
710
711 return 0;
712}
713
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400714static int __init msi_init(void)
715{
716 int ret;
717
718 if (acpi_disabled)
719 return -ENODEV;
720
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800721 if (force || dmi_check_system(msi_dmi_table))
722 old_ec_model = 1;
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400723
Lee, Chun-Yie22388e2010-01-27 12:23:00 +0800724 if (!old_ec_model)
725 get_threeg_exists();
726
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800727 if (!old_ec_model && dmi_check_system(msi_load_scm_models_dmi_table))
728 load_scm_model = 1;
729
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400730 if (auto_brightness < 0 || auto_brightness > 2)
731 return -EINVAL;
732
733 /* Register backlight stuff */
734
Thomas Renningera598c822008-08-01 17:38:01 +0200735 if (acpi_video_backlight_support()) {
736 printk(KERN_INFO "MSI: Brightness ignored, must be controlled "
737 "by ACPI video driver\n");
738 } else {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500739 struct backlight_properties props;
740 memset(&props, 0, sizeof(struct backlight_properties));
741 props.max_brightness = MSI_LCD_LEVEL_MAX - 1;
Thomas Renningera598c822008-08-01 17:38:01 +0200742 msibl_device = backlight_device_register("msi-laptop-bl", NULL,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500743 NULL, &msibl_ops,
744 &props);
Thomas Renningera598c822008-08-01 17:38:01 +0200745 if (IS_ERR(msibl_device))
746 return PTR_ERR(msibl_device);
Thomas Renningera598c822008-08-01 17:38:01 +0200747 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000748
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400749 ret = platform_driver_register(&msipf_driver);
750 if (ret)
751 goto fail_backlight;
752
753 /* Register platform stuff */
754
755 msipf_device = platform_device_alloc("msi-laptop-pf", -1);
756 if (!msipf_device) {
757 ret = -ENOMEM;
758 goto fail_platform_driver;
759 }
760
761 ret = platform_device_add(msipf_device);
762 if (ret)
763 goto fail_platform_device1;
764
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800765 if (load_scm_model && (load_scm_model_init(msipf_device) < 0)) {
766 ret = -EINVAL;
767 goto fail_platform_device1;
768 }
769
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400770 ret = sysfs_create_group(&msipf_device->dev.kobj, &msipf_attribute_group);
771 if (ret)
772 goto fail_platform_device2;
773
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800774 if (!old_ec_model) {
Lee, Chun-Yie22388e2010-01-27 12:23:00 +0800775 if (threeg_exists)
776 ret = device_create_file(&msipf_device->dev,
777 &dev_attr_threeg);
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800778 if (ret)
779 goto fail_platform_device2;
780 }
781
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400782 /* Disable automatic brightness control by default because
783 * this module was probably loaded to do brightness control in
784 * software. */
785
786 if (auto_brightness != 2)
787 set_auto_brightness(auto_brightness);
788
789 printk(KERN_INFO "msi-laptop: driver "MSI_DRIVER_VERSION" successfully loaded.\n");
790
791 return 0;
792
793fail_platform_device2:
794
795 platform_device_del(msipf_device);
796
797fail_platform_device1:
798
799 platform_device_put(msipf_device);
800
801fail_platform_driver:
802
803 platform_driver_unregister(&msipf_driver);
804
805fail_backlight:
806
807 backlight_device_unregister(msibl_device);
808
809 return ret;
810}
811
812static void __exit msi_cleanup(void)
813{
814
815 sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
Lee, Chun-Yie22388e2010-01-27 12:23:00 +0800816 if (!old_ec_model && threeg_exists)
Lee, Chun-Yifc0dc4c2010-01-09 23:17:07 +0800817 device_remove_file(&msipf_device->dev, &dev_attr_threeg);
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400818 platform_device_unregister(msipf_device);
819 platform_driver_unregister(&msipf_driver);
820 backlight_device_unregister(msibl_device);
821
Lee, Chun-Yi472ea122010-01-22 00:15:59 +0800822 rfkill_cleanup();
823
Lennart Poettering8c4c7312006-10-06 01:27:02 -0400824 /* Enable automatic brightness control again */
825 if (auto_brightness != 2)
826 set_auto_brightness(1);
827
828 printk(KERN_INFO "msi-laptop: driver unloaded.\n");
829}
830
831module_init(msi_init);
832module_exit(msi_cleanup);
833
834MODULE_AUTHOR("Lennart Poettering");
835MODULE_DESCRIPTION("MSI Laptop Support");
836MODULE_VERSION(MSI_DRIVER_VERSION);
837MODULE_LICENSE("GPL");
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200838
839MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
840MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
841MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
842MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
Lee, Chun-Yi46d0e9e2010-01-09 21:16:52 +0800843MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");
Lee, Chun-Yid0a4aa22010-05-12 09:58:07 -0700844MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N051:*");
845MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N014:*");
Lee, Chun-Yi1f27e172010-05-12 09:58:08 -0700846MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnCR620:*");