msi-laptop.c 28 KB

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