msi-laptop.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*-*-linux-c-*-*/
  2. /*
  3. Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. /*
  18. * msi-laptop.c - MSI S270 laptop support. This laptop is sold under
  19. * various brands, including "Cytron/TCM/Medion/Tchibo MD96100".
  20. *
  21. * Driver also supports S271, S420 models.
  22. *
  23. * This driver exports a few files in /sys/devices/platform/msi-laptop-pf/:
  24. *
  25. * lcd_level - Screen brightness: contains a single integer in the
  26. * range 0..8. (rw)
  27. *
  28. * auto_brightness - Enable automatic brightness control: contains
  29. * either 0 or 1. If set to 1 the hardware adjusts the screen
  30. * brightness automatically when the power cord is
  31. * plugged/unplugged. (rw)
  32. *
  33. * wlan - WLAN subsystem enabled: contains either 0 or 1. (ro)
  34. *
  35. * bluetooth - Bluetooth subsystem enabled: contains either 0 or 1
  36. * Please note that this file is constantly 0 if no Bluetooth
  37. * hardware is available. (ro)
  38. *
  39. * In addition to these platform device attributes the driver
  40. * registers itself in the Linux backlight control subsystem and is
  41. * available to userspace under /sys/class/backlight/msi-laptop-bl/.
  42. *
  43. * This driver might work on other laptops produced by MSI. If you
  44. * want to try it you can pass force=1 as argument to the module which
  45. * will force it to load even when the DMI data doesn't identify the
  46. * laptop as MSI S270. YMMV.
  47. */
  48. #include <linux/module.h>
  49. #include <linux/kernel.h>
  50. #include <linux/init.h>
  51. #include <linux/acpi.h>
  52. #include <linux/dmi.h>
  53. #include <linux/backlight.h>
  54. #include <linux/platform_device.h>
  55. #include <linux/rfkill.h>
  56. #include <linux/i8042.h>
  57. #define MSI_DRIVER_VERSION "0.5"
  58. #define MSI_LCD_LEVEL_MAX 9
  59. #define MSI_EC_COMMAND_WIRELESS 0x10
  60. #define MSI_EC_COMMAND_LCD_LEVEL 0x11
  61. #define MSI_STANDARD_EC_COMMAND_ADDRESS 0x2e
  62. #define MSI_STANDARD_EC_BLUETOOTH_MASK (1 << 0)
  63. #define MSI_STANDARD_EC_WEBCAM_MASK (1 << 1)
  64. #define MSI_STANDARD_EC_WLAN_MASK (1 << 3)
  65. #define MSI_STANDARD_EC_3G_MASK (1 << 4)
  66. /* For set SCM load flag to disable BIOS fn key */
  67. #define MSI_STANDARD_EC_SCM_LOAD_ADDRESS 0x2d
  68. #define MSI_STANDARD_EC_SCM_LOAD_MASK (1 << 0)
  69. static int msi_laptop_resume(struct platform_device *device);
  70. #define MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS 0x2f
  71. static int force;
  72. module_param(force, bool, 0);
  73. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  74. static int auto_brightness;
  75. module_param(auto_brightness, int, 0);
  76. MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
  77. static bool old_ec_model;
  78. static int wlan_s, bluetooth_s, threeg_s;
  79. static int threeg_exists;
  80. /* Some MSI 3G netbook only have one fn key to control Wlan/Bluetooth/3G,
  81. * those netbook will load the SCM (windows app) to disable the original
  82. * Wlan/Bluetooth control by BIOS when user press fn key, then control
  83. * Wlan/Bluetooth/3G by SCM (software control by OS). Without SCM, user
  84. * cann't on/off 3G module on those 3G netbook.
  85. * On Linux, msi-laptop driver will do the same thing to disable the
  86. * original BIOS control, then might need use HAL or other userland
  87. * application to do the software control that simulate with SCM.
  88. * e.g. MSI N034 netbook
  89. */
  90. static bool load_scm_model;
  91. static struct rfkill *rfk_wlan, *rfk_bluetooth, *rfk_threeg;
  92. /* Hardware access */
  93. static int set_lcd_level(int level)
  94. {
  95. u8 buf[2];
  96. if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
  97. return -EINVAL;
  98. buf[0] = 0x80;
  99. buf[1] = (u8) (level*31);
  100. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf),
  101. NULL, 0, 1);
  102. }
  103. static int get_lcd_level(void)
  104. {
  105. u8 wdata = 0, rdata;
  106. int result;
  107. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1,
  108. &rdata, 1, 1);
  109. if (result < 0)
  110. return result;
  111. return (int) rdata / 31;
  112. }
  113. static int get_auto_brightness(void)
  114. {
  115. u8 wdata = 4, rdata;
  116. int result;
  117. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1,
  118. &rdata, 1, 1);
  119. if (result < 0)
  120. return result;
  121. return !!(rdata & 8);
  122. }
  123. static int set_auto_brightness(int enable)
  124. {
  125. u8 wdata[2], rdata;
  126. int result;
  127. wdata[0] = 4;
  128. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1,
  129. &rdata, 1, 1);
  130. if (result < 0)
  131. return result;
  132. wdata[0] = 0x84;
  133. wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
  134. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2,
  135. NULL, 0, 1);
  136. }
  137. static ssize_t set_device_state(const char *buf, size_t count, u8 mask)
  138. {
  139. int status;
  140. u8 wdata = 0, rdata;
  141. int result;
  142. if (sscanf(buf, "%i", &status) != 1 || (status < 0 || status > 1))
  143. return -EINVAL;
  144. /* read current device state */
  145. result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
  146. if (result < 0)
  147. return -EINVAL;
  148. if (!!(rdata & mask) != status) {
  149. /* reverse device bit */
  150. if (rdata & mask)
  151. wdata = rdata & ~mask;
  152. else
  153. wdata = rdata | mask;
  154. result = ec_write(MSI_STANDARD_EC_COMMAND_ADDRESS, wdata);
  155. if (result < 0)
  156. return -EINVAL;
  157. }
  158. return count;
  159. }
  160. static int get_wireless_state(int *wlan, int *bluetooth)
  161. {
  162. u8 wdata = 0, rdata;
  163. int result;
  164. result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1, 1);
  165. if (result < 0)
  166. return -1;
  167. if (wlan)
  168. *wlan = !!(rdata & 8);
  169. if (bluetooth)
  170. *bluetooth = !!(rdata & 128);
  171. return 0;
  172. }
  173. static int get_wireless_state_ec_standard(void)
  174. {
  175. u8 rdata;
  176. int result;
  177. result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
  178. if (result < 0)
  179. return -1;
  180. wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);
  181. bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);
  182. threeg_s = !!(rdata & MSI_STANDARD_EC_3G_MASK);
  183. return 0;
  184. }
  185. static int get_threeg_exists(void)
  186. {
  187. u8 rdata;
  188. int result;
  189. result = ec_read(MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS, &rdata);
  190. if (result < 0)
  191. return -1;
  192. threeg_exists = !!(rdata & MSI_STANDARD_EC_3G_MASK);
  193. return 0;
  194. }
  195. /* Backlight device stuff */
  196. static int bl_get_brightness(struct backlight_device *b)
  197. {
  198. return get_lcd_level();
  199. }
  200. static int bl_update_status(struct backlight_device *b)
  201. {
  202. return set_lcd_level(b->props.brightness);
  203. }
  204. static const struct backlight_ops msibl_ops = {
  205. .get_brightness = bl_get_brightness,
  206. .update_status = bl_update_status,
  207. };
  208. static struct backlight_device *msibl_device;
  209. /* Platform device */
  210. static ssize_t show_wlan(struct device *dev,
  211. struct device_attribute *attr, char *buf)
  212. {
  213. int ret, enabled;
  214. if (old_ec_model) {
  215. ret = get_wireless_state(&enabled, NULL);
  216. } else {
  217. ret = get_wireless_state_ec_standard();
  218. enabled = wlan_s;
  219. }
  220. if (ret < 0)
  221. return ret;
  222. return sprintf(buf, "%i\n", enabled);
  223. }
  224. static ssize_t store_wlan(struct device *dev,
  225. struct device_attribute *attr, const char *buf, size_t count)
  226. {
  227. return set_device_state(buf, count, MSI_STANDARD_EC_WLAN_MASK);
  228. }
  229. static ssize_t show_bluetooth(struct device *dev,
  230. struct device_attribute *attr, char *buf)
  231. {
  232. int ret, enabled;
  233. if (old_ec_model) {
  234. ret = get_wireless_state(NULL, &enabled);
  235. } else {
  236. ret = get_wireless_state_ec_standard();
  237. enabled = bluetooth_s;
  238. }
  239. if (ret < 0)
  240. return ret;
  241. return sprintf(buf, "%i\n", enabled);
  242. }
  243. static ssize_t store_bluetooth(struct device *dev,
  244. struct device_attribute *attr, const char *buf, size_t count)
  245. {
  246. return set_device_state(buf, count, MSI_STANDARD_EC_BLUETOOTH_MASK);
  247. }
  248. static ssize_t show_threeg(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. int ret;
  252. /* old msi ec not support 3G */
  253. if (old_ec_model)
  254. return -1;
  255. ret = get_wireless_state_ec_standard();
  256. if (ret < 0)
  257. return ret;
  258. return sprintf(buf, "%i\n", threeg_s);
  259. }
  260. static ssize_t store_threeg(struct device *dev,
  261. struct device_attribute *attr, const char *buf, size_t count)
  262. {
  263. return set_device_state(buf, count, MSI_STANDARD_EC_3G_MASK);
  264. }
  265. static ssize_t show_lcd_level(struct device *dev,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. int ret;
  269. ret = get_lcd_level();
  270. if (ret < 0)
  271. return ret;
  272. return sprintf(buf, "%i\n", ret);
  273. }
  274. static ssize_t store_lcd_level(struct device *dev,
  275. struct device_attribute *attr, const char *buf, size_t count)
  276. {
  277. int level, ret;
  278. if (sscanf(buf, "%i", &level) != 1 ||
  279. (level < 0 || level >= MSI_LCD_LEVEL_MAX))
  280. return -EINVAL;
  281. ret = set_lcd_level(level);
  282. if (ret < 0)
  283. return ret;
  284. return count;
  285. }
  286. static ssize_t show_auto_brightness(struct device *dev,
  287. struct device_attribute *attr, char *buf)
  288. {
  289. int ret;
  290. ret = get_auto_brightness();
  291. if (ret < 0)
  292. return ret;
  293. return sprintf(buf, "%i\n", ret);
  294. }
  295. static ssize_t store_auto_brightness(struct device *dev,
  296. struct device_attribute *attr, const char *buf, size_t count)
  297. {
  298. int enable, ret;
  299. if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
  300. return -EINVAL;
  301. ret = set_auto_brightness(enable);
  302. if (ret < 0)
  303. return ret;
  304. return count;
  305. }
  306. static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
  307. static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness,
  308. store_auto_brightness);
  309. static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
  310. static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
  311. static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
  312. static struct attribute *msipf_attributes[] = {
  313. &dev_attr_lcd_level.attr,
  314. &dev_attr_auto_brightness.attr,
  315. &dev_attr_bluetooth.attr,
  316. &dev_attr_wlan.attr,
  317. NULL
  318. };
  319. static struct attribute_group msipf_attribute_group = {
  320. .attrs = msipf_attributes
  321. };
  322. static struct platform_driver msipf_driver = {
  323. .driver = {
  324. .name = "msi-laptop-pf",
  325. .owner = THIS_MODULE,
  326. },
  327. .resume = msi_laptop_resume,
  328. };
  329. static struct platform_device *msipf_device;
  330. /* Initialization */
  331. static int dmi_check_cb(const struct dmi_system_id *id)
  332. {
  333. printk(KERN_INFO "msi-laptop: Identified laptop model '%s'.\n",
  334. id->ident);
  335. return 1;
  336. }
  337. static struct dmi_system_id __initdata msi_dmi_table[] = {
  338. {
  339. .ident = "MSI S270",
  340. .matches = {
  341. DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
  342. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
  343. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  344. DMI_MATCH(DMI_CHASSIS_VENDOR,
  345. "MICRO-STAR INT'L CO.,LTD")
  346. },
  347. .callback = dmi_check_cb
  348. },
  349. {
  350. .ident = "MSI S271",
  351. .matches = {
  352. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  353. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
  354. DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
  355. DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
  356. },
  357. .callback = dmi_check_cb
  358. },
  359. {
  360. .ident = "MSI S420",
  361. .matches = {
  362. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  363. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
  364. DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
  365. DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
  366. },
  367. .callback = dmi_check_cb
  368. },
  369. {
  370. .ident = "Medion MD96100",
  371. .matches = {
  372. DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
  373. DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
  374. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  375. DMI_MATCH(DMI_CHASSIS_VENDOR,
  376. "MICRO-STAR INT'L CO.,LTD")
  377. },
  378. .callback = dmi_check_cb
  379. },
  380. { }
  381. };
  382. static struct dmi_system_id __initdata msi_load_scm_models_dmi_table[] = {
  383. {
  384. .ident = "MSI N034",
  385. .matches = {
  386. DMI_MATCH(DMI_SYS_VENDOR,
  387. "MICRO-STAR INTERNATIONAL CO., LTD"),
  388. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N034"),
  389. DMI_MATCH(DMI_CHASSIS_VENDOR,
  390. "MICRO-STAR INTERNATIONAL CO., LTD")
  391. },
  392. .callback = dmi_check_cb
  393. },
  394. {
  395. .ident = "MSI N051",
  396. .matches = {
  397. DMI_MATCH(DMI_SYS_VENDOR,
  398. "MICRO-STAR INTERNATIONAL CO., LTD"),
  399. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N051"),
  400. DMI_MATCH(DMI_CHASSIS_VENDOR,
  401. "MICRO-STAR INTERNATIONAL CO., LTD")
  402. },
  403. .callback = dmi_check_cb
  404. },
  405. {
  406. .ident = "MSI N014",
  407. .matches = {
  408. DMI_MATCH(DMI_SYS_VENDOR,
  409. "MICRO-STAR INTERNATIONAL CO., LTD"),
  410. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N014"),
  411. },
  412. .callback = dmi_check_cb
  413. },
  414. {
  415. .ident = "MSI CR620",
  416. .matches = {
  417. DMI_MATCH(DMI_SYS_VENDOR,
  418. "Micro-Star International"),
  419. DMI_MATCH(DMI_PRODUCT_NAME, "CR620"),
  420. },
  421. .callback = dmi_check_cb
  422. },
  423. { }
  424. };
  425. static int rfkill_bluetooth_set(void *data, bool blocked)
  426. {
  427. /* Do something with blocked...*/
  428. /*
  429. * blocked == false is on
  430. * blocked == true is off
  431. */
  432. if (blocked)
  433. set_device_state("0", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
  434. else
  435. set_device_state("1", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
  436. return 0;
  437. }
  438. static int rfkill_wlan_set(void *data, bool blocked)
  439. {
  440. if (blocked)
  441. set_device_state("0", 0, MSI_STANDARD_EC_WLAN_MASK);
  442. else
  443. set_device_state("1", 0, MSI_STANDARD_EC_WLAN_MASK);
  444. return 0;
  445. }
  446. static int rfkill_threeg_set(void *data, bool blocked)
  447. {
  448. if (blocked)
  449. set_device_state("0", 0, MSI_STANDARD_EC_3G_MASK);
  450. else
  451. set_device_state("1", 0, MSI_STANDARD_EC_3G_MASK);
  452. return 0;
  453. }
  454. static const struct rfkill_ops rfkill_bluetooth_ops = {
  455. .set_block = rfkill_bluetooth_set
  456. };
  457. static const struct rfkill_ops rfkill_wlan_ops = {
  458. .set_block = rfkill_wlan_set
  459. };
  460. static const struct rfkill_ops rfkill_threeg_ops = {
  461. .set_block = rfkill_threeg_set
  462. };
  463. static void rfkill_cleanup(void)
  464. {
  465. if (rfk_bluetooth) {
  466. rfkill_unregister(rfk_bluetooth);
  467. rfkill_destroy(rfk_bluetooth);
  468. }
  469. if (rfk_threeg) {
  470. rfkill_unregister(rfk_threeg);
  471. rfkill_destroy(rfk_threeg);
  472. }
  473. if (rfk_wlan) {
  474. rfkill_unregister(rfk_wlan);
  475. rfkill_destroy(rfk_wlan);
  476. }
  477. }
  478. static void msi_update_rfkill(struct work_struct *ignored)
  479. {
  480. get_wireless_state_ec_standard();
  481. if (rfk_wlan)
  482. rfkill_set_sw_state(rfk_wlan, !wlan_s);
  483. if (rfk_bluetooth)
  484. rfkill_set_sw_state(rfk_bluetooth, !bluetooth_s);
  485. if (rfk_threeg)
  486. rfkill_set_sw_state(rfk_threeg, !threeg_s);
  487. }
  488. static DECLARE_DELAYED_WORK(msi_rfkill_work, msi_update_rfkill);
  489. static bool msi_laptop_i8042_filter(unsigned char data, unsigned char str,
  490. struct serio *port)
  491. {
  492. static bool extended;
  493. if (str & 0x20)
  494. return false;
  495. /* 0x54 wwan, 0x62 bluetooth, 0x76 wlan*/
  496. if (unlikely(data == 0xe0)) {
  497. extended = true;
  498. return false;
  499. } else if (unlikely(extended)) {
  500. switch (data) {
  501. case 0x54:
  502. case 0x62:
  503. case 0x76:
  504. schedule_delayed_work(&msi_rfkill_work,
  505. round_jiffies_relative(0.5 * HZ));
  506. break;
  507. }
  508. extended = false;
  509. }
  510. return false;
  511. }
  512. static void msi_init_rfkill(struct work_struct *ignored)
  513. {
  514. if (rfk_wlan) {
  515. rfkill_set_sw_state(rfk_wlan, !wlan_s);
  516. rfkill_wlan_set(NULL, !wlan_s);
  517. }
  518. if (rfk_bluetooth) {
  519. rfkill_set_sw_state(rfk_bluetooth, !bluetooth_s);
  520. rfkill_bluetooth_set(NULL, !bluetooth_s);
  521. }
  522. if (rfk_threeg) {
  523. rfkill_set_sw_state(rfk_threeg, !threeg_s);
  524. rfkill_threeg_set(NULL, !threeg_s);
  525. }
  526. }
  527. static DECLARE_DELAYED_WORK(msi_rfkill_init, msi_init_rfkill);
  528. static int rfkill_init(struct platform_device *sdev)
  529. {
  530. /* add rfkill */
  531. int retval;
  532. /* keep the hardware wireless state */
  533. get_wireless_state_ec_standard();
  534. rfk_bluetooth = rfkill_alloc("msi-bluetooth", &sdev->dev,
  535. RFKILL_TYPE_BLUETOOTH,
  536. &rfkill_bluetooth_ops, NULL);
  537. if (!rfk_bluetooth) {
  538. retval = -ENOMEM;
  539. goto err_bluetooth;
  540. }
  541. retval = rfkill_register(rfk_bluetooth);
  542. if (retval)
  543. goto err_bluetooth;
  544. rfk_wlan = rfkill_alloc("msi-wlan", &sdev->dev, RFKILL_TYPE_WLAN,
  545. &rfkill_wlan_ops, NULL);
  546. if (!rfk_wlan) {
  547. retval = -ENOMEM;
  548. goto err_wlan;
  549. }
  550. retval = rfkill_register(rfk_wlan);
  551. if (retval)
  552. goto err_wlan;
  553. if (threeg_exists) {
  554. rfk_threeg = rfkill_alloc("msi-threeg", &sdev->dev,
  555. RFKILL_TYPE_WWAN, &rfkill_threeg_ops, NULL);
  556. if (!rfk_threeg) {
  557. retval = -ENOMEM;
  558. goto err_threeg;
  559. }
  560. retval = rfkill_register(rfk_threeg);
  561. if (retval)
  562. goto err_threeg;
  563. }
  564. /* schedule to run rfkill state initial */
  565. schedule_delayed_work(&msi_rfkill_init,
  566. round_jiffies_relative(1 * HZ));
  567. return 0;
  568. err_threeg:
  569. rfkill_destroy(rfk_threeg);
  570. if (rfk_wlan)
  571. rfkill_unregister(rfk_wlan);
  572. err_wlan:
  573. rfkill_destroy(rfk_wlan);
  574. if (rfk_bluetooth)
  575. rfkill_unregister(rfk_bluetooth);
  576. err_bluetooth:
  577. rfkill_destroy(rfk_bluetooth);
  578. return retval;
  579. }
  580. static int msi_laptop_resume(struct platform_device *device)
  581. {
  582. u8 data;
  583. int result;
  584. if (!load_scm_model)
  585. return 0;
  586. /* set load SCM to disable hardware control by fn key */
  587. result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
  588. if (result < 0)
  589. return result;
  590. result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
  591. data | MSI_STANDARD_EC_SCM_LOAD_MASK);
  592. if (result < 0)
  593. return result;
  594. return 0;
  595. }
  596. static int load_scm_model_init(struct platform_device *sdev)
  597. {
  598. u8 data;
  599. int result;
  600. /* allow userland write sysfs file */
  601. dev_attr_bluetooth.store = store_bluetooth;
  602. dev_attr_wlan.store = store_wlan;
  603. dev_attr_threeg.store = store_threeg;
  604. dev_attr_bluetooth.attr.mode |= S_IWUSR;
  605. dev_attr_wlan.attr.mode |= S_IWUSR;
  606. dev_attr_threeg.attr.mode |= S_IWUSR;
  607. /* disable hardware control by fn key */
  608. result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
  609. if (result < 0)
  610. return result;
  611. result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
  612. data | MSI_STANDARD_EC_SCM_LOAD_MASK);
  613. if (result < 0)
  614. return result;
  615. /* initial rfkill */
  616. result = rfkill_init(sdev);
  617. if (result < 0)
  618. goto fail_rfkill;
  619. result = i8042_install_filter(msi_laptop_i8042_filter);
  620. if (result) {
  621. printk(KERN_ERR
  622. "msi-laptop: Unable to install key filter\n");
  623. goto fail_filter;
  624. }
  625. return 0;
  626. fail_filter:
  627. rfkill_cleanup();
  628. fail_rfkill:
  629. return result;
  630. }
  631. static int __init msi_init(void)
  632. {
  633. int ret;
  634. if (acpi_disabled)
  635. return -ENODEV;
  636. if (force || dmi_check_system(msi_dmi_table))
  637. old_ec_model = 1;
  638. if (!old_ec_model)
  639. get_threeg_exists();
  640. if (!old_ec_model && dmi_check_system(msi_load_scm_models_dmi_table))
  641. load_scm_model = 1;
  642. if (auto_brightness < 0 || auto_brightness > 2)
  643. return -EINVAL;
  644. /* Register backlight stuff */
  645. if (acpi_video_backlight_support()) {
  646. printk(KERN_INFO "MSI: Brightness ignored, must be controlled "
  647. "by ACPI video driver\n");
  648. } else {
  649. struct backlight_properties props;
  650. memset(&props, 0, sizeof(struct backlight_properties));
  651. props.max_brightness = MSI_LCD_LEVEL_MAX - 1;
  652. msibl_device = backlight_device_register("msi-laptop-bl", NULL,
  653. NULL, &msibl_ops,
  654. &props);
  655. if (IS_ERR(msibl_device))
  656. return PTR_ERR(msibl_device);
  657. }
  658. ret = platform_driver_register(&msipf_driver);
  659. if (ret)
  660. goto fail_backlight;
  661. /* Register platform stuff */
  662. msipf_device = platform_device_alloc("msi-laptop-pf", -1);
  663. if (!msipf_device) {
  664. ret = -ENOMEM;
  665. goto fail_platform_driver;
  666. }
  667. ret = platform_device_add(msipf_device);
  668. if (ret)
  669. goto fail_platform_device1;
  670. if (load_scm_model && (load_scm_model_init(msipf_device) < 0)) {
  671. ret = -EINVAL;
  672. goto fail_platform_device1;
  673. }
  674. ret = sysfs_create_group(&msipf_device->dev.kobj,
  675. &msipf_attribute_group);
  676. if (ret)
  677. goto fail_platform_device2;
  678. if (!old_ec_model) {
  679. if (threeg_exists)
  680. ret = device_create_file(&msipf_device->dev,
  681. &dev_attr_threeg);
  682. if (ret)
  683. goto fail_platform_device2;
  684. }
  685. /* Disable automatic brightness control by default because
  686. * this module was probably loaded to do brightness control in
  687. * software. */
  688. if (auto_brightness != 2)
  689. set_auto_brightness(auto_brightness);
  690. printk(KERN_INFO "msi-laptop: driver "MSI_DRIVER_VERSION" successfully loaded.\n");
  691. return 0;
  692. fail_platform_device2:
  693. if (load_scm_model) {
  694. i8042_remove_filter(msi_laptop_i8042_filter);
  695. cancel_delayed_work_sync(&msi_rfkill_work);
  696. rfkill_cleanup();
  697. }
  698. platform_device_del(msipf_device);
  699. fail_platform_device1:
  700. platform_device_put(msipf_device);
  701. fail_platform_driver:
  702. platform_driver_unregister(&msipf_driver);
  703. fail_backlight:
  704. backlight_device_unregister(msibl_device);
  705. return ret;
  706. }
  707. static void __exit msi_cleanup(void)
  708. {
  709. if (load_scm_model) {
  710. i8042_remove_filter(msi_laptop_i8042_filter);
  711. cancel_delayed_work_sync(&msi_rfkill_work);
  712. rfkill_cleanup();
  713. }
  714. sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  715. if (!old_ec_model && threeg_exists)
  716. device_remove_file(&msipf_device->dev, &dev_attr_threeg);
  717. platform_device_unregister(msipf_device);
  718. platform_driver_unregister(&msipf_driver);
  719. backlight_device_unregister(msibl_device);
  720. /* Enable automatic brightness control again */
  721. if (auto_brightness != 2)
  722. set_auto_brightness(1);
  723. printk(KERN_INFO "msi-laptop: driver unloaded.\n");
  724. }
  725. module_init(msi_init);
  726. module_exit(msi_cleanup);
  727. MODULE_AUTHOR("Lennart Poettering");
  728. MODULE_DESCRIPTION("MSI Laptop Support");
  729. MODULE_VERSION(MSI_DRIVER_VERSION);
  730. MODULE_LICENSE("GPL");
  731. MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  732. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
  733. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  734. MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  735. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");
  736. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N051:*");
  737. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N014:*");
  738. MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnCR620:*");