msi-laptop.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  49. #include <linux/module.h>
  50. #include <linux/kernel.h>
  51. #include <linux/init.h>
  52. #include <linux/acpi.h>
  53. #include <linux/dmi.h>
  54. #include <linux/backlight.h>
  55. #include <linux/platform_device.h>
  56. #include <linux/rfkill.h>
  57. #include <linux/i8042.h>
  58. #include <linux/input.h>
  59. #include <linux/input/sparse-keymap.h>
  60. #define MSI_DRIVER_VERSION "0.5"
  61. #define MSI_LCD_LEVEL_MAX 9
  62. #define MSI_EC_COMMAND_WIRELESS 0x10
  63. #define MSI_EC_COMMAND_LCD_LEVEL 0x11
  64. #define MSI_STANDARD_EC_COMMAND_ADDRESS 0x2e
  65. #define MSI_STANDARD_EC_BLUETOOTH_MASK (1 << 0)
  66. #define MSI_STANDARD_EC_WEBCAM_MASK (1 << 1)
  67. #define MSI_STANDARD_EC_WLAN_MASK (1 << 3)
  68. #define MSI_STANDARD_EC_3G_MASK (1 << 4)
  69. /* For set SCM load flag to disable BIOS fn key */
  70. #define MSI_STANDARD_EC_SCM_LOAD_ADDRESS 0x2d
  71. #define MSI_STANDARD_EC_SCM_LOAD_MASK (1 << 0)
  72. #define MSI_STANDARD_EC_TOUCHPAD_ADDRESS 0xe4
  73. #define MSI_STANDARD_EC_TOUCHPAD_MASK (1 << 4)
  74. #ifdef CONFIG_PM_SLEEP
  75. static int msi_laptop_resume(struct device *device);
  76. #endif
  77. static SIMPLE_DEV_PM_OPS(msi_laptop_pm, NULL, msi_laptop_resume);
  78. #define MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS 0x2f
  79. static bool force;
  80. module_param(force, bool, 0);
  81. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  82. static int auto_brightness;
  83. module_param(auto_brightness, int, 0);
  84. MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
  85. static const struct key_entry msi_laptop_keymap[] = {
  86. {KE_KEY, KEY_TOUCHPAD_ON, {KEY_TOUCHPAD_ON} }, /* Touch Pad On */
  87. {KE_KEY, KEY_TOUCHPAD_OFF, {KEY_TOUCHPAD_OFF} },/* Touch Pad On */
  88. {KE_END, 0}
  89. };
  90. static struct input_dev *msi_laptop_input_dev;
  91. static int wlan_s, bluetooth_s, threeg_s;
  92. static int threeg_exists;
  93. static struct rfkill *rfk_wlan, *rfk_bluetooth, *rfk_threeg;
  94. /* MSI laptop quirks */
  95. struct quirk_entry {
  96. bool old_ec_model;
  97. /* Some MSI 3G netbook only have one fn key to control
  98. * Wlan/Bluetooth/3G, those netbook will load the SCM (windows app) to
  99. * disable the original Wlan/Bluetooth control by BIOS when user press
  100. * fn key, then control Wlan/Bluetooth/3G by SCM (software control by
  101. * OS). Without SCM, user cann't on/off 3G module on those 3G netbook.
  102. * On Linux, msi-laptop driver will do the same thing to disable the
  103. * original BIOS control, then might need use HAL or other userland
  104. * application to do the software control that simulate with SCM.
  105. * e.g. MSI N034 netbook
  106. */
  107. bool load_scm_model;
  108. /* Some MSI laptops need delay before reading from EC */
  109. bool ec_delay;
  110. /* Some MSI Wind netbooks (e.g. MSI Wind U100) need loading SCM to get
  111. * some features working (e.g. ECO mode), but we cannot change
  112. * Wlan/Bluetooth state in software and we can only read its state.
  113. */
  114. bool ec_read_only;
  115. };
  116. static struct quirk_entry *quirks;
  117. /* Hardware access */
  118. static int set_lcd_level(int level)
  119. {
  120. u8 buf[2];
  121. if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
  122. return -EINVAL;
  123. buf[0] = 0x80;
  124. buf[1] = (u8) (level*31);
  125. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf),
  126. NULL, 0);
  127. }
  128. static int get_lcd_level(void)
  129. {
  130. u8 wdata = 0, rdata;
  131. int result;
  132. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1,
  133. &rdata, 1);
  134. if (result < 0)
  135. return result;
  136. return (int) rdata / 31;
  137. }
  138. static int get_auto_brightness(void)
  139. {
  140. u8 wdata = 4, rdata;
  141. int result;
  142. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1,
  143. &rdata, 1);
  144. if (result < 0)
  145. return result;
  146. return !!(rdata & 8);
  147. }
  148. static int set_auto_brightness(int enable)
  149. {
  150. u8 wdata[2], rdata;
  151. int result;
  152. wdata[0] = 4;
  153. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1,
  154. &rdata, 1);
  155. if (result < 0)
  156. return result;
  157. wdata[0] = 0x84;
  158. wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
  159. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2,
  160. NULL, 0);
  161. }
  162. static ssize_t set_device_state(const char *buf, size_t count, u8 mask)
  163. {
  164. int status;
  165. u8 wdata = 0, rdata;
  166. int result;
  167. if (sscanf(buf, "%i", &status) != 1 || (status < 0 || status > 1))
  168. return -EINVAL;
  169. if (quirks->ec_read_only)
  170. return -EOPNOTSUPP;
  171. /* read current device state */
  172. result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
  173. if (result < 0)
  174. return result;
  175. if (!!(rdata & mask) != status) {
  176. /* reverse device bit */
  177. if (rdata & mask)
  178. wdata = rdata & ~mask;
  179. else
  180. wdata = rdata | mask;
  181. result = ec_write(MSI_STANDARD_EC_COMMAND_ADDRESS, wdata);
  182. if (result < 0)
  183. return result;
  184. }
  185. return count;
  186. }
  187. static int get_wireless_state(int *wlan, int *bluetooth)
  188. {
  189. u8 wdata = 0, rdata;
  190. int result;
  191. result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1);
  192. if (result < 0)
  193. return result;
  194. if (wlan)
  195. *wlan = !!(rdata & 8);
  196. if (bluetooth)
  197. *bluetooth = !!(rdata & 128);
  198. return 0;
  199. }
  200. static int get_wireless_state_ec_standard(void)
  201. {
  202. u8 rdata;
  203. int result;
  204. result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
  205. if (result < 0)
  206. return result;
  207. wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);
  208. bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);
  209. threeg_s = !!(rdata & MSI_STANDARD_EC_3G_MASK);
  210. return 0;
  211. }
  212. static int get_threeg_exists(void)
  213. {
  214. u8 rdata;
  215. int result;
  216. result = ec_read(MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS, &rdata);
  217. if (result < 0)
  218. return result;
  219. threeg_exists = !!(rdata & MSI_STANDARD_EC_3G_MASK);
  220. return 0;
  221. }
  222. /* Backlight device stuff */
  223. static int bl_get_brightness(struct backlight_device *b)
  224. {
  225. return get_lcd_level();
  226. }
  227. static int bl_update_status(struct backlight_device *b)
  228. {
  229. return set_lcd_level(b->props.brightness);
  230. }
  231. static const struct backlight_ops msibl_ops = {
  232. .get_brightness = bl_get_brightness,
  233. .update_status = bl_update_status,
  234. };
  235. static struct backlight_device *msibl_device;
  236. /* Platform device */
  237. static ssize_t show_wlan(struct device *dev,
  238. struct device_attribute *attr, char *buf)
  239. {
  240. int ret, enabled = 0;
  241. if (quirks->old_ec_model) {
  242. ret = get_wireless_state(&enabled, NULL);
  243. } else {
  244. ret = get_wireless_state_ec_standard();
  245. enabled = wlan_s;
  246. }
  247. if (ret < 0)
  248. return ret;
  249. return sprintf(buf, "%i\n", enabled);
  250. }
  251. static ssize_t store_wlan(struct device *dev,
  252. struct device_attribute *attr, const char *buf, size_t count)
  253. {
  254. return set_device_state(buf, count, MSI_STANDARD_EC_WLAN_MASK);
  255. }
  256. static ssize_t show_bluetooth(struct device *dev,
  257. struct device_attribute *attr, char *buf)
  258. {
  259. int ret, enabled = 0;
  260. if (quirks->old_ec_model) {
  261. ret = get_wireless_state(NULL, &enabled);
  262. } else {
  263. ret = get_wireless_state_ec_standard();
  264. enabled = bluetooth_s;
  265. }
  266. if (ret < 0)
  267. return ret;
  268. return sprintf(buf, "%i\n", enabled);
  269. }
  270. static ssize_t store_bluetooth(struct device *dev,
  271. struct device_attribute *attr, const char *buf, size_t count)
  272. {
  273. return set_device_state(buf, count, MSI_STANDARD_EC_BLUETOOTH_MASK);
  274. }
  275. static ssize_t show_threeg(struct device *dev,
  276. struct device_attribute *attr, char *buf)
  277. {
  278. int ret;
  279. /* old msi ec not support 3G */
  280. if (quirks->old_ec_model)
  281. return -ENODEV;
  282. ret = get_wireless_state_ec_standard();
  283. if (ret < 0)
  284. return ret;
  285. return sprintf(buf, "%i\n", threeg_s);
  286. }
  287. static ssize_t store_threeg(struct device *dev,
  288. struct device_attribute *attr, const char *buf, size_t count)
  289. {
  290. return set_device_state(buf, count, MSI_STANDARD_EC_3G_MASK);
  291. }
  292. static ssize_t show_lcd_level(struct device *dev,
  293. struct device_attribute *attr, char *buf)
  294. {
  295. int ret;
  296. ret = get_lcd_level();
  297. if (ret < 0)
  298. return ret;
  299. return sprintf(buf, "%i\n", ret);
  300. }
  301. static ssize_t store_lcd_level(struct device *dev,
  302. struct device_attribute *attr, const char *buf, size_t count)
  303. {
  304. int level, ret;
  305. if (sscanf(buf, "%i", &level) != 1 ||
  306. (level < 0 || level >= MSI_LCD_LEVEL_MAX))
  307. return -EINVAL;
  308. ret = set_lcd_level(level);
  309. if (ret < 0)
  310. return ret;
  311. return count;
  312. }
  313. static ssize_t show_auto_brightness(struct device *dev,
  314. struct device_attribute *attr, char *buf)
  315. {
  316. int ret;
  317. ret = get_auto_brightness();
  318. if (ret < 0)
  319. return ret;
  320. return sprintf(buf, "%i\n", ret);
  321. }
  322. static ssize_t store_auto_brightness(struct device *dev,
  323. struct device_attribute *attr, const char *buf, size_t count)
  324. {
  325. int enable, ret;
  326. if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
  327. return -EINVAL;
  328. ret = set_auto_brightness(enable);
  329. if (ret < 0)
  330. return ret;
  331. return count;
  332. }
  333. static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
  334. static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness,
  335. store_auto_brightness);
  336. static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
  337. static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
  338. static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
  339. static struct attribute *msipf_attributes[] = {
  340. &dev_attr_lcd_level.attr,
  341. &dev_attr_auto_brightness.attr,
  342. &dev_attr_bluetooth.attr,
  343. &dev_attr_wlan.attr,
  344. NULL
  345. };
  346. static struct attribute_group msipf_attribute_group = {
  347. .attrs = msipf_attributes
  348. };
  349. static struct platform_driver msipf_driver = {
  350. .driver = {
  351. .name = "msi-laptop-pf",
  352. .owner = THIS_MODULE,
  353. .pm = &msi_laptop_pm,
  354. },
  355. };
  356. static struct platform_device *msipf_device;
  357. /* Initialization */
  358. static struct quirk_entry quirk_old_ec_model = {
  359. .old_ec_model = true,
  360. };
  361. static struct quirk_entry quirk_load_scm_model = {
  362. .load_scm_model = true,
  363. .ec_delay = true,
  364. };
  365. static struct quirk_entry quirk_load_scm_ro_model = {
  366. .load_scm_model = true,
  367. .ec_read_only = true,
  368. };
  369. static int dmi_check_cb(const struct dmi_system_id *dmi)
  370. {
  371. pr_info("Identified laptop model '%s'\n", dmi->ident);
  372. quirks = dmi->driver_data;
  373. return 1;
  374. }
  375. static struct dmi_system_id __initdata msi_dmi_table[] = {
  376. {
  377. .ident = "MSI S270",
  378. .matches = {
  379. DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
  380. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
  381. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  382. DMI_MATCH(DMI_CHASSIS_VENDOR,
  383. "MICRO-STAR INT'L CO.,LTD")
  384. },
  385. .driver_data = &quirk_old_ec_model,
  386. .callback = dmi_check_cb
  387. },
  388. {
  389. .ident = "MSI S271",
  390. .matches = {
  391. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  392. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
  393. DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
  394. DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
  395. },
  396. .driver_data = &quirk_old_ec_model,
  397. .callback = dmi_check_cb
  398. },
  399. {
  400. .ident = "MSI S420",
  401. .matches = {
  402. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  403. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
  404. DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
  405. DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
  406. },
  407. .driver_data = &quirk_old_ec_model,
  408. .callback = dmi_check_cb
  409. },
  410. {
  411. .ident = "Medion MD96100",
  412. .matches = {
  413. DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
  414. DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
  415. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  416. DMI_MATCH(DMI_CHASSIS_VENDOR,
  417. "MICRO-STAR INT'L CO.,LTD")
  418. },
  419. .driver_data = &quirk_old_ec_model,
  420. .callback = dmi_check_cb
  421. },
  422. {
  423. .ident = "MSI N034",
  424. .matches = {
  425. DMI_MATCH(DMI_SYS_VENDOR,
  426. "MICRO-STAR INTERNATIONAL CO., LTD"),
  427. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N034"),
  428. DMI_MATCH(DMI_CHASSIS_VENDOR,
  429. "MICRO-STAR INTERNATIONAL CO., LTD")
  430. },
  431. .driver_data = &quirk_load_scm_model,
  432. .callback = dmi_check_cb
  433. },
  434. {
  435. .ident = "MSI N051",
  436. .matches = {
  437. DMI_MATCH(DMI_SYS_VENDOR,
  438. "MICRO-STAR INTERNATIONAL CO., LTD"),
  439. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N051"),
  440. DMI_MATCH(DMI_CHASSIS_VENDOR,
  441. "MICRO-STAR INTERNATIONAL CO., LTD")
  442. },
  443. .driver_data = &quirk_load_scm_model,
  444. .callback = dmi_check_cb
  445. },
  446. {
  447. .ident = "MSI N014",
  448. .matches = {
  449. DMI_MATCH(DMI_SYS_VENDOR,
  450. "MICRO-STAR INTERNATIONAL CO., LTD"),
  451. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N014"),
  452. },
  453. .driver_data = &quirk_load_scm_model,
  454. .callback = dmi_check_cb
  455. },
  456. {
  457. .ident = "MSI CR620",
  458. .matches = {
  459. DMI_MATCH(DMI_SYS_VENDOR,
  460. "Micro-Star International"),
  461. DMI_MATCH(DMI_PRODUCT_NAME, "CR620"),
  462. },
  463. .driver_data = &quirk_load_scm_model,
  464. .callback = dmi_check_cb
  465. },
  466. {
  467. .ident = "MSI U270",
  468. .matches = {
  469. DMI_MATCH(DMI_SYS_VENDOR,
  470. "Micro-Star International Co., Ltd."),
  471. DMI_MATCH(DMI_PRODUCT_NAME, "U270 series"),
  472. },
  473. .driver_data = &quirk_load_scm_model,
  474. .callback = dmi_check_cb
  475. },
  476. { }
  477. };
  478. static int rfkill_bluetooth_set(void *data, bool blocked)
  479. {
  480. /* Do something with blocked...*/
  481. /*
  482. * blocked == false is on
  483. * blocked == true is off
  484. */
  485. int result = set_device_state(blocked ? "0" : "1", 0,
  486. MSI_STANDARD_EC_BLUETOOTH_MASK);
  487. return min(result, 0);
  488. }
  489. static int rfkill_wlan_set(void *data, bool blocked)
  490. {
  491. int result = set_device_state(blocked ? "0" : "1", 0,
  492. MSI_STANDARD_EC_WLAN_MASK);
  493. return min(result, 0);
  494. }
  495. static int rfkill_threeg_set(void *data, bool blocked)
  496. {
  497. int result = set_device_state(blocked ? "0" : "1", 0,
  498. MSI_STANDARD_EC_3G_MASK);
  499. return min(result, 0);
  500. }
  501. static const struct rfkill_ops rfkill_bluetooth_ops = {
  502. .set_block = rfkill_bluetooth_set
  503. };
  504. static const struct rfkill_ops rfkill_wlan_ops = {
  505. .set_block = rfkill_wlan_set
  506. };
  507. static const struct rfkill_ops rfkill_threeg_ops = {
  508. .set_block = rfkill_threeg_set
  509. };
  510. static void rfkill_cleanup(void)
  511. {
  512. if (rfk_bluetooth) {
  513. rfkill_unregister(rfk_bluetooth);
  514. rfkill_destroy(rfk_bluetooth);
  515. }
  516. if (rfk_threeg) {
  517. rfkill_unregister(rfk_threeg);
  518. rfkill_destroy(rfk_threeg);
  519. }
  520. if (rfk_wlan) {
  521. rfkill_unregister(rfk_wlan);
  522. rfkill_destroy(rfk_wlan);
  523. }
  524. }
  525. static bool msi_rfkill_set_state(struct rfkill *rfkill, bool blocked)
  526. {
  527. if (quirks->ec_read_only)
  528. return rfkill_set_hw_state(rfkill, blocked);
  529. else
  530. return rfkill_set_sw_state(rfkill, blocked);
  531. }
  532. static void msi_update_rfkill(struct work_struct *ignored)
  533. {
  534. get_wireless_state_ec_standard();
  535. if (rfk_wlan)
  536. msi_rfkill_set_state(rfk_wlan, !wlan_s);
  537. if (rfk_bluetooth)
  538. msi_rfkill_set_state(rfk_bluetooth, !bluetooth_s);
  539. if (rfk_threeg)
  540. msi_rfkill_set_state(rfk_threeg, !threeg_s);
  541. }
  542. static DECLARE_DELAYED_WORK(msi_rfkill_dwork, msi_update_rfkill);
  543. static DECLARE_WORK(msi_rfkill_work, msi_update_rfkill);
  544. static void msi_send_touchpad_key(struct work_struct *ignored)
  545. {
  546. u8 rdata;
  547. int result;
  548. result = ec_read(MSI_STANDARD_EC_TOUCHPAD_ADDRESS, &rdata);
  549. if (result < 0)
  550. return;
  551. sparse_keymap_report_event(msi_laptop_input_dev,
  552. (rdata & MSI_STANDARD_EC_TOUCHPAD_MASK) ?
  553. KEY_TOUCHPAD_ON : KEY_TOUCHPAD_OFF, 1, true);
  554. }
  555. static DECLARE_DELAYED_WORK(msi_touchpad_dwork, msi_send_touchpad_key);
  556. static DECLARE_WORK(msi_touchpad_work, msi_send_touchpad_key);
  557. static bool msi_laptop_i8042_filter(unsigned char data, unsigned char str,
  558. struct serio *port)
  559. {
  560. static bool extended;
  561. if (str & 0x20)
  562. return false;
  563. /* 0x54 wwan, 0x62 bluetooth, 0x76 wlan, 0xE4 touchpad toggle*/
  564. if (unlikely(data == 0xe0)) {
  565. extended = true;
  566. return false;
  567. } else if (unlikely(extended)) {
  568. extended = false;
  569. switch (data) {
  570. case 0xE4:
  571. if (quirks->ec_delay) {
  572. schedule_delayed_work(&msi_touchpad_dwork,
  573. round_jiffies_relative(0.5 * HZ));
  574. } else
  575. schedule_work(&msi_touchpad_work);
  576. break;
  577. case 0x54:
  578. case 0x62:
  579. case 0x76:
  580. if (quirks->ec_delay) {
  581. schedule_delayed_work(&msi_rfkill_dwork,
  582. round_jiffies_relative(0.5 * HZ));
  583. } else
  584. schedule_work(&msi_rfkill_work);
  585. break;
  586. }
  587. }
  588. return false;
  589. }
  590. static void msi_init_rfkill(struct work_struct *ignored)
  591. {
  592. if (rfk_wlan) {
  593. rfkill_set_sw_state(rfk_wlan, !wlan_s);
  594. rfkill_wlan_set(NULL, !wlan_s);
  595. }
  596. if (rfk_bluetooth) {
  597. rfkill_set_sw_state(rfk_bluetooth, !bluetooth_s);
  598. rfkill_bluetooth_set(NULL, !bluetooth_s);
  599. }
  600. if (rfk_threeg) {
  601. rfkill_set_sw_state(rfk_threeg, !threeg_s);
  602. rfkill_threeg_set(NULL, !threeg_s);
  603. }
  604. }
  605. static DECLARE_DELAYED_WORK(msi_rfkill_init, msi_init_rfkill);
  606. static int rfkill_init(struct platform_device *sdev)
  607. {
  608. /* add rfkill */
  609. int retval;
  610. /* keep the hardware wireless state */
  611. get_wireless_state_ec_standard();
  612. rfk_bluetooth = rfkill_alloc("msi-bluetooth", &sdev->dev,
  613. RFKILL_TYPE_BLUETOOTH,
  614. &rfkill_bluetooth_ops, NULL);
  615. if (!rfk_bluetooth) {
  616. retval = -ENOMEM;
  617. goto err_bluetooth;
  618. }
  619. retval = rfkill_register(rfk_bluetooth);
  620. if (retval)
  621. goto err_bluetooth;
  622. rfk_wlan = rfkill_alloc("msi-wlan", &sdev->dev, RFKILL_TYPE_WLAN,
  623. &rfkill_wlan_ops, NULL);
  624. if (!rfk_wlan) {
  625. retval = -ENOMEM;
  626. goto err_wlan;
  627. }
  628. retval = rfkill_register(rfk_wlan);
  629. if (retval)
  630. goto err_wlan;
  631. if (threeg_exists) {
  632. rfk_threeg = rfkill_alloc("msi-threeg", &sdev->dev,
  633. RFKILL_TYPE_WWAN, &rfkill_threeg_ops, NULL);
  634. if (!rfk_threeg) {
  635. retval = -ENOMEM;
  636. goto err_threeg;
  637. }
  638. retval = rfkill_register(rfk_threeg);
  639. if (retval)
  640. goto err_threeg;
  641. }
  642. /* schedule to run rfkill state initial */
  643. if (quirks->ec_delay) {
  644. schedule_delayed_work(&msi_rfkill_init,
  645. round_jiffies_relative(1 * HZ));
  646. } else
  647. schedule_work(&msi_rfkill_work);
  648. return 0;
  649. err_threeg:
  650. rfkill_destroy(rfk_threeg);
  651. if (rfk_wlan)
  652. rfkill_unregister(rfk_wlan);
  653. err_wlan:
  654. rfkill_destroy(rfk_wlan);
  655. if (rfk_bluetooth)
  656. rfkill_unregister(rfk_bluetooth);
  657. err_bluetooth:
  658. rfkill_destroy(rfk_bluetooth);
  659. return retval;
  660. }
  661. #ifdef CONFIG_PM_SLEEP
  662. static int msi_laptop_resume(struct device *device)
  663. {
  664. u8 data;
  665. int result;
  666. if (!quirks->load_scm_model)
  667. return 0;
  668. /* set load SCM to disable hardware control by fn key */
  669. result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
  670. if (result < 0)
  671. return result;
  672. result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
  673. data | MSI_STANDARD_EC_SCM_LOAD_MASK);
  674. if (result < 0)
  675. return result;
  676. return 0;
  677. }
  678. #endif
  679. static int __init msi_laptop_input_setup(void)
  680. {
  681. int err;
  682. msi_laptop_input_dev = input_allocate_device();
  683. if (!msi_laptop_input_dev)
  684. return -ENOMEM;
  685. msi_laptop_input_dev->name = "MSI Laptop hotkeys";
  686. msi_laptop_input_dev->phys = "msi-laptop/input0";
  687. msi_laptop_input_dev->id.bustype = BUS_HOST;
  688. err = sparse_keymap_setup(msi_laptop_input_dev,
  689. msi_laptop_keymap, NULL);
  690. if (err)
  691. goto err_free_dev;
  692. err = input_register_device(msi_laptop_input_dev);
  693. if (err)
  694. goto err_free_keymap;
  695. return 0;
  696. err_free_keymap:
  697. sparse_keymap_free(msi_laptop_input_dev);
  698. err_free_dev:
  699. input_free_device(msi_laptop_input_dev);
  700. return err;
  701. }
  702. static void msi_laptop_input_destroy(void)
  703. {
  704. sparse_keymap_free(msi_laptop_input_dev);
  705. input_unregister_device(msi_laptop_input_dev);
  706. }
  707. static int __init load_scm_model_init(struct platform_device *sdev)
  708. {
  709. u8 data;
  710. int result;
  711. if (!quirks->ec_read_only) {
  712. /* allow userland write sysfs file */
  713. dev_attr_bluetooth.store = store_bluetooth;
  714. dev_attr_wlan.store = store_wlan;
  715. dev_attr_threeg.store = store_threeg;
  716. dev_attr_bluetooth.attr.mode |= S_IWUSR;
  717. dev_attr_wlan.attr.mode |= S_IWUSR;
  718. dev_attr_threeg.attr.mode |= S_IWUSR;
  719. }
  720. /* disable hardware control by fn key */
  721. result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
  722. if (result < 0)
  723. return result;
  724. result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
  725. data | MSI_STANDARD_EC_SCM_LOAD_MASK);
  726. if (result < 0)
  727. return result;
  728. /* initial rfkill */
  729. result = rfkill_init(sdev);
  730. if (result < 0)
  731. goto fail_rfkill;
  732. /* setup input device */
  733. result = msi_laptop_input_setup();
  734. if (result)
  735. goto fail_input;
  736. result = i8042_install_filter(msi_laptop_i8042_filter);
  737. if (result) {
  738. pr_err("Unable to install key filter\n");
  739. goto fail_filter;
  740. }
  741. return 0;
  742. fail_filter:
  743. msi_laptop_input_destroy();
  744. fail_input:
  745. rfkill_cleanup();
  746. fail_rfkill:
  747. return result;
  748. }
  749. static int __init msi_init(void)
  750. {
  751. int ret;
  752. if (acpi_disabled)
  753. return -ENODEV;
  754. dmi_check_system(msi_dmi_table);
  755. if (!quirks)
  756. /* quirks may be NULL if no match in DMI table */
  757. quirks = &quirk_load_scm_model;
  758. if (force)
  759. quirks = &quirk_old_ec_model;
  760. if (!quirks->old_ec_model)
  761. get_threeg_exists();
  762. if (auto_brightness < 0 || auto_brightness > 2)
  763. return -EINVAL;
  764. /* Register backlight stuff */
  765. if (acpi_video_backlight_support()) {
  766. pr_info("Brightness ignored, must be controlled by ACPI video driver\n");
  767. } else {
  768. struct backlight_properties props;
  769. memset(&props, 0, sizeof(struct backlight_properties));
  770. props.type = BACKLIGHT_PLATFORM;
  771. props.max_brightness = MSI_LCD_LEVEL_MAX - 1;
  772. msibl_device = backlight_device_register("msi-laptop-bl", NULL,
  773. NULL, &msibl_ops,
  774. &props);
  775. if (IS_ERR(msibl_device))
  776. return PTR_ERR(msibl_device);
  777. }
  778. ret = platform_driver_register(&msipf_driver);
  779. if (ret)
  780. goto fail_backlight;
  781. /* Register platform stuff */
  782. msipf_device = platform_device_alloc("msi-laptop-pf", -1);
  783. if (!msipf_device) {
  784. ret = -ENOMEM;
  785. goto fail_platform_driver;
  786. }
  787. ret = platform_device_add(msipf_device);
  788. if (ret)
  789. goto fail_platform_device1;
  790. if (quirks->load_scm_model && (load_scm_model_init(msipf_device) < 0)) {
  791. ret = -EINVAL;
  792. goto fail_platform_device1;
  793. }
  794. ret = sysfs_create_group(&msipf_device->dev.kobj,
  795. &msipf_attribute_group);
  796. if (ret)
  797. goto fail_platform_device2;
  798. if (!quirks->old_ec_model) {
  799. if (threeg_exists)
  800. ret = device_create_file(&msipf_device->dev,
  801. &dev_attr_threeg);
  802. if (ret)
  803. goto fail_platform_device2;
  804. }
  805. /* Disable automatic brightness control by default because
  806. * this module was probably loaded to do brightness control in
  807. * software. */
  808. if (auto_brightness != 2)
  809. set_auto_brightness(auto_brightness);
  810. pr_info("driver " MSI_DRIVER_VERSION " successfully loaded\n");
  811. return 0;
  812. fail_platform_device2:
  813. if (quirks->load_scm_model) {
  814. i8042_remove_filter(msi_laptop_i8042_filter);
  815. cancel_delayed_work_sync(&msi_rfkill_dwork);
  816. cancel_work_sync(&msi_rfkill_work);
  817. rfkill_cleanup();
  818. }
  819. platform_device_del(msipf_device);
  820. fail_platform_device1:
  821. platform_device_put(msipf_device);
  822. fail_platform_driver:
  823. platform_driver_unregister(&msipf_driver);
  824. fail_backlight:
  825. backlight_device_unregister(msibl_device);
  826. return ret;
  827. }
  828. static void __exit msi_cleanup(void)
  829. {
  830. if (quirks->load_scm_model) {
  831. i8042_remove_filter(msi_laptop_i8042_filter);
  832. msi_laptop_input_destroy();
  833. cancel_delayed_work_sync(&msi_rfkill_dwork);
  834. cancel_work_sync(&msi_rfkill_work);
  835. rfkill_cleanup();
  836. }
  837. sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  838. if (!quirks->old_ec_model && threeg_exists)
  839. device_remove_file(&msipf_device->dev, &dev_attr_threeg);
  840. platform_device_unregister(msipf_device);
  841. platform_driver_unregister(&msipf_driver);
  842. backlight_device_unregister(msibl_device);
  843. /* Enable automatic brightness control again */
  844. if (auto_brightness != 2)
  845. set_auto_brightness(1);
  846. pr_info("driver unloaded\n");
  847. }
  848. module_init(msi_init);
  849. module_exit(msi_cleanup);
  850. MODULE_AUTHOR("Lennart Poettering");
  851. MODULE_DESCRIPTION("MSI Laptop Support");
  852. MODULE_VERSION(MSI_DRIVER_VERSION);
  853. MODULE_LICENSE("GPL");
  854. MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  855. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
  856. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  857. MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  858. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");
  859. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N051:*");
  860. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N014:*");
  861. MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnCR620:*");
  862. MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnU270series:*");