msi-laptop.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*-*-linux-c-*-*/
  3. /*
  4. Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
  5. */
  6. /*
  7. * msi-laptop.c - MSI S270 laptop support. This laptop is sold under
  8. * various brands, including "Cytron/TCM/Medion/Tchibo MD96100".
  9. *
  10. * Driver also supports S271, S420 models.
  11. *
  12. * This driver exports a few files in /sys/devices/platform/msi-laptop-pf/:
  13. *
  14. * lcd_level - Screen brightness: contains a single integer in the
  15. * range 0..8. (rw)
  16. *
  17. * auto_brightness - Enable automatic brightness control: contains
  18. * either 0 or 1. If set to 1 the hardware adjusts the screen
  19. * brightness automatically when the power cord is
  20. * plugged/unplugged. (rw)
  21. *
  22. * wlan - WLAN subsystem enabled: contains either 0 or 1. (ro)
  23. *
  24. * bluetooth - Bluetooth subsystem enabled: contains either 0 or 1
  25. * Please note that this file is constantly 0 if no Bluetooth
  26. * hardware is available. (ro)
  27. *
  28. * In addition to these platform device attributes the driver
  29. * registers itself in the Linux backlight control subsystem and is
  30. * available to userspace under /sys/class/backlight/msi-laptop-bl/.
  31. *
  32. * This driver might work on other laptops produced by MSI. If you
  33. * want to try it you can pass force=1 as argument to the module which
  34. * will force it to load even when the DMI data doesn't identify the
  35. * laptop as MSI S270. YMMV.
  36. */
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <linux/module.h>
  39. #include <linux/kernel.h>
  40. #include <linux/init.h>
  41. #include <linux/acpi.h>
  42. #include <linux/dmi.h>
  43. #include <linux/backlight.h>
  44. #include <linux/platform_device.h>
  45. #include <linux/rfkill.h>
  46. #include <linux/i8042.h>
  47. #include <linux/input.h>
  48. #include <linux/input/sparse-keymap.h>
  49. #include <acpi/video.h>
  50. #define MSI_DRIVER_VERSION "0.5"
  51. #define MSI_LCD_LEVEL_MAX 9
  52. #define MSI_EC_COMMAND_WIRELESS 0x10
  53. #define MSI_EC_COMMAND_LCD_LEVEL 0x11
  54. #define MSI_STANDARD_EC_COMMAND_ADDRESS 0x2e
  55. #define MSI_STANDARD_EC_BLUETOOTH_MASK (1 << 0)
  56. #define MSI_STANDARD_EC_WEBCAM_MASK (1 << 1)
  57. #define MSI_STANDARD_EC_WLAN_MASK (1 << 3)
  58. #define MSI_STANDARD_EC_3G_MASK (1 << 4)
  59. /* For set SCM load flag to disable BIOS fn key */
  60. #define MSI_STANDARD_EC_SCM_LOAD_ADDRESS 0x2d
  61. #define MSI_STANDARD_EC_SCM_LOAD_MASK (1 << 0)
  62. #define MSI_STANDARD_EC_FUNCTIONS_ADDRESS 0xe4
  63. /* Power LED is orange - Turbo mode */
  64. #define MSI_STANDARD_EC_TURBO_MASK (1 << 1)
  65. /* Power LED is green - ECO mode */
  66. #define MSI_STANDARD_EC_ECO_MASK (1 << 3)
  67. /* Touchpad is turned on */
  68. #define MSI_STANDARD_EC_TOUCHPAD_MASK (1 << 4)
  69. /* If this bit != bit 1, turbo mode can't be toggled */
  70. #define MSI_STANDARD_EC_TURBO_COOLDOWN_MASK (1 << 7)
  71. #define MSI_STANDARD_EC_FAN_ADDRESS 0x33
  72. /* If zero, fan rotates at maximal speed */
  73. #define MSI_STANDARD_EC_AUTOFAN_MASK (1 << 0)
  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 ssize_t show_touchpad(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. u8 rdata;
  337. int result;
  338. result = ec_read(MSI_STANDARD_EC_FUNCTIONS_ADDRESS, &rdata);
  339. if (result < 0)
  340. return result;
  341. return sprintf(buf, "%i\n", !!(rdata & MSI_STANDARD_EC_TOUCHPAD_MASK));
  342. }
  343. static ssize_t show_turbo(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_TURBO_MASK));
  352. }
  353. static ssize_t show_eco(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_ECO_MASK));
  362. }
  363. static ssize_t show_turbo_cooldown(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_TURBO_MASK)) |
  372. (!!(rdata & MSI_STANDARD_EC_TURBO_COOLDOWN_MASK) << 1));
  373. }
  374. static ssize_t show_auto_fan(struct device *dev,
  375. struct device_attribute *attr, char *buf)
  376. {
  377. u8 rdata;
  378. int result;
  379. result = ec_read(MSI_STANDARD_EC_FAN_ADDRESS, &rdata);
  380. if (result < 0)
  381. return result;
  382. return sprintf(buf, "%i\n", !!(rdata & MSI_STANDARD_EC_AUTOFAN_MASK));
  383. }
  384. static ssize_t store_auto_fan(struct device *dev,
  385. struct device_attribute *attr, const char *buf, size_t count)
  386. {
  387. int enable, result;
  388. if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
  389. return -EINVAL;
  390. result = ec_write(MSI_STANDARD_EC_FAN_ADDRESS, enable);
  391. if (result < 0)
  392. return result;
  393. return count;
  394. }
  395. static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
  396. static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness,
  397. store_auto_brightness);
  398. static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
  399. static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
  400. static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
  401. static DEVICE_ATTR(touchpad, 0444, show_touchpad, NULL);
  402. static DEVICE_ATTR(turbo_mode, 0444, show_turbo, NULL);
  403. static DEVICE_ATTR(eco_mode, 0444, show_eco, NULL);
  404. static DEVICE_ATTR(turbo_cooldown, 0444, show_turbo_cooldown, NULL);
  405. static DEVICE_ATTR(auto_fan, 0644, show_auto_fan, store_auto_fan);
  406. static struct attribute *msipf_attributes[] = {
  407. &dev_attr_bluetooth.attr,
  408. &dev_attr_wlan.attr,
  409. &dev_attr_touchpad.attr,
  410. &dev_attr_turbo_mode.attr,
  411. &dev_attr_eco_mode.attr,
  412. &dev_attr_turbo_cooldown.attr,
  413. &dev_attr_auto_fan.attr,
  414. NULL
  415. };
  416. static struct attribute *msipf_old_attributes[] = {
  417. &dev_attr_lcd_level.attr,
  418. &dev_attr_auto_brightness.attr,
  419. NULL
  420. };
  421. static const struct attribute_group msipf_attribute_group = {
  422. .attrs = msipf_attributes
  423. };
  424. static const struct attribute_group msipf_old_attribute_group = {
  425. .attrs = msipf_old_attributes
  426. };
  427. static struct platform_driver msipf_driver = {
  428. .driver = {
  429. .name = "msi-laptop-pf",
  430. .pm = &msi_laptop_pm,
  431. },
  432. };
  433. static struct platform_device *msipf_device;
  434. /* Initialization */
  435. static struct quirk_entry quirk_old_ec_model = {
  436. .old_ec_model = true,
  437. };
  438. static struct quirk_entry quirk_load_scm_model = {
  439. .load_scm_model = true,
  440. .ec_delay = true,
  441. };
  442. static struct quirk_entry quirk_load_scm_ro_model = {
  443. .load_scm_model = true,
  444. .ec_read_only = true,
  445. };
  446. static int dmi_check_cb(const struct dmi_system_id *dmi)
  447. {
  448. pr_info("Identified laptop model '%s'\n", dmi->ident);
  449. quirks = dmi->driver_data;
  450. return 1;
  451. }
  452. static const struct dmi_system_id msi_dmi_table[] __initconst = {
  453. {
  454. .ident = "MSI S270",
  455. .matches = {
  456. DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
  457. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
  458. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  459. DMI_MATCH(DMI_CHASSIS_VENDOR,
  460. "MICRO-STAR INT'L CO.,LTD")
  461. },
  462. .driver_data = &quirk_old_ec_model,
  463. .callback = dmi_check_cb
  464. },
  465. {
  466. .ident = "MSI S271",
  467. .matches = {
  468. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  469. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
  470. DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
  471. DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
  472. },
  473. .driver_data = &quirk_old_ec_model,
  474. .callback = dmi_check_cb
  475. },
  476. {
  477. .ident = "MSI S420",
  478. .matches = {
  479. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  480. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
  481. DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
  482. DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
  483. },
  484. .driver_data = &quirk_old_ec_model,
  485. .callback = dmi_check_cb
  486. },
  487. {
  488. .ident = "Medion MD96100",
  489. .matches = {
  490. DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
  491. DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
  492. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  493. DMI_MATCH(DMI_CHASSIS_VENDOR,
  494. "MICRO-STAR INT'L CO.,LTD")
  495. },
  496. .driver_data = &quirk_old_ec_model,
  497. .callback = dmi_check_cb
  498. },
  499. {
  500. .ident = "MSI N034",
  501. .matches = {
  502. DMI_MATCH(DMI_SYS_VENDOR,
  503. "MICRO-STAR INTERNATIONAL CO., LTD"),
  504. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N034"),
  505. DMI_MATCH(DMI_CHASSIS_VENDOR,
  506. "MICRO-STAR INTERNATIONAL CO., LTD")
  507. },
  508. .driver_data = &quirk_load_scm_model,
  509. .callback = dmi_check_cb
  510. },
  511. {
  512. .ident = "MSI N051",
  513. .matches = {
  514. DMI_MATCH(DMI_SYS_VENDOR,
  515. "MICRO-STAR INTERNATIONAL CO., LTD"),
  516. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N051"),
  517. DMI_MATCH(DMI_CHASSIS_VENDOR,
  518. "MICRO-STAR INTERNATIONAL CO., LTD")
  519. },
  520. .driver_data = &quirk_load_scm_model,
  521. .callback = dmi_check_cb
  522. },
  523. {
  524. .ident = "MSI N014",
  525. .matches = {
  526. DMI_MATCH(DMI_SYS_VENDOR,
  527. "MICRO-STAR INTERNATIONAL CO., LTD"),
  528. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N014"),
  529. },
  530. .driver_data = &quirk_load_scm_model,
  531. .callback = dmi_check_cb
  532. },
  533. {
  534. .ident = "MSI CR620",
  535. .matches = {
  536. DMI_MATCH(DMI_SYS_VENDOR,
  537. "Micro-Star International"),
  538. DMI_MATCH(DMI_PRODUCT_NAME, "CR620"),
  539. },
  540. .driver_data = &quirk_load_scm_model,
  541. .callback = dmi_check_cb
  542. },
  543. {
  544. .ident = "MSI U270",
  545. .matches = {
  546. DMI_MATCH(DMI_SYS_VENDOR,
  547. "Micro-Star International Co., Ltd."),
  548. DMI_MATCH(DMI_PRODUCT_NAME, "U270 series"),
  549. },
  550. .driver_data = &quirk_load_scm_model,
  551. .callback = dmi_check_cb
  552. },
  553. {
  554. .ident = "MSI U90/U100",
  555. .matches = {
  556. DMI_MATCH(DMI_SYS_VENDOR,
  557. "MICRO-STAR INTERNATIONAL CO., LTD"),
  558. DMI_MATCH(DMI_PRODUCT_NAME, "U90/U100"),
  559. },
  560. .driver_data = &quirk_load_scm_ro_model,
  561. .callback = dmi_check_cb
  562. },
  563. { }
  564. };
  565. static int rfkill_bluetooth_set(void *data, bool blocked)
  566. {
  567. /* Do something with blocked...*/
  568. /*
  569. * blocked == false is on
  570. * blocked == true is off
  571. */
  572. int result = set_device_state(blocked ? "0" : "1", 0,
  573. MSI_STANDARD_EC_BLUETOOTH_MASK);
  574. return min(result, 0);
  575. }
  576. static int rfkill_wlan_set(void *data, bool blocked)
  577. {
  578. int result = set_device_state(blocked ? "0" : "1", 0,
  579. MSI_STANDARD_EC_WLAN_MASK);
  580. return min(result, 0);
  581. }
  582. static int rfkill_threeg_set(void *data, bool blocked)
  583. {
  584. int result = set_device_state(blocked ? "0" : "1", 0,
  585. MSI_STANDARD_EC_3G_MASK);
  586. return min(result, 0);
  587. }
  588. static const struct rfkill_ops rfkill_bluetooth_ops = {
  589. .set_block = rfkill_bluetooth_set
  590. };
  591. static const struct rfkill_ops rfkill_wlan_ops = {
  592. .set_block = rfkill_wlan_set
  593. };
  594. static const struct rfkill_ops rfkill_threeg_ops = {
  595. .set_block = rfkill_threeg_set
  596. };
  597. static void rfkill_cleanup(void)
  598. {
  599. if (rfk_bluetooth) {
  600. rfkill_unregister(rfk_bluetooth);
  601. rfkill_destroy(rfk_bluetooth);
  602. }
  603. if (rfk_threeg) {
  604. rfkill_unregister(rfk_threeg);
  605. rfkill_destroy(rfk_threeg);
  606. }
  607. if (rfk_wlan) {
  608. rfkill_unregister(rfk_wlan);
  609. rfkill_destroy(rfk_wlan);
  610. }
  611. }
  612. static bool msi_rfkill_set_state(struct rfkill *rfkill, bool blocked)
  613. {
  614. if (quirks->ec_read_only)
  615. return rfkill_set_hw_state(rfkill, blocked);
  616. else
  617. return rfkill_set_sw_state(rfkill, blocked);
  618. }
  619. static void msi_update_rfkill(struct work_struct *ignored)
  620. {
  621. get_wireless_state_ec_standard();
  622. if (rfk_wlan)
  623. msi_rfkill_set_state(rfk_wlan, !wlan_s);
  624. if (rfk_bluetooth)
  625. msi_rfkill_set_state(rfk_bluetooth, !bluetooth_s);
  626. if (rfk_threeg)
  627. msi_rfkill_set_state(rfk_threeg, !threeg_s);
  628. }
  629. static DECLARE_DELAYED_WORK(msi_rfkill_dwork, msi_update_rfkill);
  630. static DECLARE_WORK(msi_rfkill_work, msi_update_rfkill);
  631. static void msi_send_touchpad_key(struct work_struct *ignored)
  632. {
  633. u8 rdata;
  634. int result;
  635. result = ec_read(MSI_STANDARD_EC_FUNCTIONS_ADDRESS, &rdata);
  636. if (result < 0)
  637. return;
  638. sparse_keymap_report_event(msi_laptop_input_dev,
  639. (rdata & MSI_STANDARD_EC_TOUCHPAD_MASK) ?
  640. KEY_TOUCHPAD_ON : KEY_TOUCHPAD_OFF, 1, true);
  641. }
  642. static DECLARE_DELAYED_WORK(msi_touchpad_dwork, msi_send_touchpad_key);
  643. static DECLARE_WORK(msi_touchpad_work, msi_send_touchpad_key);
  644. static bool msi_laptop_i8042_filter(unsigned char data, unsigned char str,
  645. struct serio *port)
  646. {
  647. static bool extended;
  648. if (str & I8042_STR_AUXDATA)
  649. return false;
  650. /* 0x54 wwan, 0x62 bluetooth, 0x76 wlan, 0xE4 touchpad toggle*/
  651. if (unlikely(data == 0xe0)) {
  652. extended = true;
  653. return false;
  654. } else if (unlikely(extended)) {
  655. extended = false;
  656. switch (data) {
  657. case 0xE4:
  658. if (quirks->ec_delay) {
  659. schedule_delayed_work(&msi_touchpad_dwork,
  660. round_jiffies_relative(0.5 * HZ));
  661. } else
  662. schedule_work(&msi_touchpad_work);
  663. break;
  664. case 0x54:
  665. case 0x62:
  666. case 0x76:
  667. if (quirks->ec_delay) {
  668. schedule_delayed_work(&msi_rfkill_dwork,
  669. round_jiffies_relative(0.5 * HZ));
  670. } else
  671. schedule_work(&msi_rfkill_work);
  672. break;
  673. }
  674. }
  675. return false;
  676. }
  677. static void msi_init_rfkill(struct work_struct *ignored)
  678. {
  679. if (rfk_wlan) {
  680. rfkill_set_sw_state(rfk_wlan, !wlan_s);
  681. rfkill_wlan_set(NULL, !wlan_s);
  682. }
  683. if (rfk_bluetooth) {
  684. rfkill_set_sw_state(rfk_bluetooth, !bluetooth_s);
  685. rfkill_bluetooth_set(NULL, !bluetooth_s);
  686. }
  687. if (rfk_threeg) {
  688. rfkill_set_sw_state(rfk_threeg, !threeg_s);
  689. rfkill_threeg_set(NULL, !threeg_s);
  690. }
  691. }
  692. static DECLARE_DELAYED_WORK(msi_rfkill_init, msi_init_rfkill);
  693. static int rfkill_init(struct platform_device *sdev)
  694. {
  695. /* add rfkill */
  696. int retval;
  697. /* keep the hardware wireless state */
  698. get_wireless_state_ec_standard();
  699. rfk_bluetooth = rfkill_alloc("msi-bluetooth", &sdev->dev,
  700. RFKILL_TYPE_BLUETOOTH,
  701. &rfkill_bluetooth_ops, NULL);
  702. if (!rfk_bluetooth) {
  703. retval = -ENOMEM;
  704. goto err_bluetooth;
  705. }
  706. retval = rfkill_register(rfk_bluetooth);
  707. if (retval)
  708. goto err_bluetooth;
  709. rfk_wlan = rfkill_alloc("msi-wlan", &sdev->dev, RFKILL_TYPE_WLAN,
  710. &rfkill_wlan_ops, NULL);
  711. if (!rfk_wlan) {
  712. retval = -ENOMEM;
  713. goto err_wlan;
  714. }
  715. retval = rfkill_register(rfk_wlan);
  716. if (retval)
  717. goto err_wlan;
  718. if (threeg_exists) {
  719. rfk_threeg = rfkill_alloc("msi-threeg", &sdev->dev,
  720. RFKILL_TYPE_WWAN, &rfkill_threeg_ops, NULL);
  721. if (!rfk_threeg) {
  722. retval = -ENOMEM;
  723. goto err_threeg;
  724. }
  725. retval = rfkill_register(rfk_threeg);
  726. if (retval)
  727. goto err_threeg;
  728. }
  729. /* schedule to run rfkill state initial */
  730. if (quirks->ec_delay) {
  731. schedule_delayed_work(&msi_rfkill_init,
  732. round_jiffies_relative(1 * HZ));
  733. } else
  734. schedule_work(&msi_rfkill_work);
  735. return 0;
  736. err_threeg:
  737. rfkill_destroy(rfk_threeg);
  738. if (rfk_wlan)
  739. rfkill_unregister(rfk_wlan);
  740. err_wlan:
  741. rfkill_destroy(rfk_wlan);
  742. if (rfk_bluetooth)
  743. rfkill_unregister(rfk_bluetooth);
  744. err_bluetooth:
  745. rfkill_destroy(rfk_bluetooth);
  746. return retval;
  747. }
  748. #ifdef CONFIG_PM_SLEEP
  749. static int msi_laptop_resume(struct device *device)
  750. {
  751. u8 data;
  752. int result;
  753. if (!quirks->load_scm_model)
  754. return 0;
  755. /* set load SCM to disable hardware control by fn key */
  756. result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
  757. if (result < 0)
  758. return result;
  759. result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
  760. data | MSI_STANDARD_EC_SCM_LOAD_MASK);
  761. if (result < 0)
  762. return result;
  763. return 0;
  764. }
  765. #endif
  766. static int __init msi_laptop_input_setup(void)
  767. {
  768. int err;
  769. msi_laptop_input_dev = input_allocate_device();
  770. if (!msi_laptop_input_dev)
  771. return -ENOMEM;
  772. msi_laptop_input_dev->name = "MSI Laptop hotkeys";
  773. msi_laptop_input_dev->phys = "msi-laptop/input0";
  774. msi_laptop_input_dev->id.bustype = BUS_HOST;
  775. err = sparse_keymap_setup(msi_laptop_input_dev,
  776. msi_laptop_keymap, NULL);
  777. if (err)
  778. goto err_free_dev;
  779. err = input_register_device(msi_laptop_input_dev);
  780. if (err)
  781. goto err_free_dev;
  782. return 0;
  783. err_free_dev:
  784. input_free_device(msi_laptop_input_dev);
  785. return err;
  786. }
  787. static int __init load_scm_model_init(struct platform_device *sdev)
  788. {
  789. u8 data;
  790. int result;
  791. if (!quirks->ec_read_only) {
  792. /* allow userland write sysfs file */
  793. dev_attr_bluetooth.store = store_bluetooth;
  794. dev_attr_wlan.store = store_wlan;
  795. dev_attr_threeg.store = store_threeg;
  796. dev_attr_bluetooth.attr.mode |= S_IWUSR;
  797. dev_attr_wlan.attr.mode |= S_IWUSR;
  798. dev_attr_threeg.attr.mode |= S_IWUSR;
  799. }
  800. /* disable hardware control by fn key */
  801. result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
  802. if (result < 0)
  803. return result;
  804. result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
  805. data | MSI_STANDARD_EC_SCM_LOAD_MASK);
  806. if (result < 0)
  807. return result;
  808. /* initial rfkill */
  809. result = rfkill_init(sdev);
  810. if (result < 0)
  811. goto fail_rfkill;
  812. /* setup input device */
  813. result = msi_laptop_input_setup();
  814. if (result)
  815. goto fail_input;
  816. result = i8042_install_filter(msi_laptop_i8042_filter);
  817. if (result) {
  818. pr_err("Unable to install key filter\n");
  819. goto fail_filter;
  820. }
  821. return 0;
  822. fail_filter:
  823. input_unregister_device(msi_laptop_input_dev);
  824. fail_input:
  825. rfkill_cleanup();
  826. fail_rfkill:
  827. return result;
  828. }
  829. static int __init msi_init(void)
  830. {
  831. int ret;
  832. if (acpi_disabled)
  833. return -ENODEV;
  834. dmi_check_system(msi_dmi_table);
  835. if (!quirks)
  836. /* quirks may be NULL if no match in DMI table */
  837. quirks = &quirk_load_scm_model;
  838. if (force)
  839. quirks = &quirk_old_ec_model;
  840. if (!quirks->old_ec_model)
  841. get_threeg_exists();
  842. if (auto_brightness < 0 || auto_brightness > 2)
  843. return -EINVAL;
  844. /* Register backlight stuff */
  845. if (quirks->old_ec_model ||
  846. acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  847. struct backlight_properties props;
  848. memset(&props, 0, sizeof(struct backlight_properties));
  849. props.type = BACKLIGHT_PLATFORM;
  850. props.max_brightness = MSI_LCD_LEVEL_MAX - 1;
  851. msibl_device = backlight_device_register("msi-laptop-bl", NULL,
  852. NULL, &msibl_ops,
  853. &props);
  854. if (IS_ERR(msibl_device))
  855. return PTR_ERR(msibl_device);
  856. }
  857. ret = platform_driver_register(&msipf_driver);
  858. if (ret)
  859. goto fail_backlight;
  860. /* Register platform stuff */
  861. msipf_device = platform_device_alloc("msi-laptop-pf", -1);
  862. if (!msipf_device) {
  863. ret = -ENOMEM;
  864. goto fail_platform_driver;
  865. }
  866. ret = platform_device_add(msipf_device);
  867. if (ret)
  868. goto fail_device_add;
  869. if (quirks->load_scm_model && (load_scm_model_init(msipf_device) < 0)) {
  870. ret = -EINVAL;
  871. goto fail_scm_model_init;
  872. }
  873. ret = sysfs_create_group(&msipf_device->dev.kobj,
  874. &msipf_attribute_group);
  875. if (ret)
  876. goto fail_create_group;
  877. if (!quirks->old_ec_model) {
  878. if (threeg_exists)
  879. ret = device_create_file(&msipf_device->dev,
  880. &dev_attr_threeg);
  881. if (ret)
  882. goto fail_create_attr;
  883. } else {
  884. ret = sysfs_create_group(&msipf_device->dev.kobj,
  885. &msipf_old_attribute_group);
  886. if (ret)
  887. goto fail_create_attr;
  888. /* Disable automatic brightness control by default because
  889. * this module was probably loaded to do brightness control in
  890. * software. */
  891. if (auto_brightness != 2)
  892. set_auto_brightness(auto_brightness);
  893. }
  894. pr_info("driver " MSI_DRIVER_VERSION " successfully loaded\n");
  895. return 0;
  896. fail_create_attr:
  897. sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  898. fail_create_group:
  899. if (quirks->load_scm_model) {
  900. i8042_remove_filter(msi_laptop_i8042_filter);
  901. cancel_delayed_work_sync(&msi_rfkill_dwork);
  902. cancel_work_sync(&msi_rfkill_work);
  903. rfkill_cleanup();
  904. }
  905. fail_scm_model_init:
  906. platform_device_del(msipf_device);
  907. fail_device_add:
  908. platform_device_put(msipf_device);
  909. fail_platform_driver:
  910. platform_driver_unregister(&msipf_driver);
  911. fail_backlight:
  912. backlight_device_unregister(msibl_device);
  913. return ret;
  914. }
  915. static void __exit msi_cleanup(void)
  916. {
  917. if (quirks->load_scm_model) {
  918. i8042_remove_filter(msi_laptop_i8042_filter);
  919. input_unregister_device(msi_laptop_input_dev);
  920. cancel_delayed_work_sync(&msi_rfkill_dwork);
  921. cancel_work_sync(&msi_rfkill_work);
  922. rfkill_cleanup();
  923. }
  924. sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  925. if (!quirks->old_ec_model && threeg_exists)
  926. device_remove_file(&msipf_device->dev, &dev_attr_threeg);
  927. platform_device_unregister(msipf_device);
  928. platform_driver_unregister(&msipf_driver);
  929. backlight_device_unregister(msibl_device);
  930. if (quirks->old_ec_model) {
  931. /* Enable automatic brightness control again */
  932. if (auto_brightness != 2)
  933. set_auto_brightness(1);
  934. }
  935. pr_info("driver unloaded\n");
  936. }
  937. module_init(msi_init);
  938. module_exit(msi_cleanup);
  939. MODULE_AUTHOR("Lennart Poettering");
  940. MODULE_DESCRIPTION("MSI Laptop Support");
  941. MODULE_VERSION(MSI_DRIVER_VERSION);
  942. MODULE_LICENSE("GPL");
  943. MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  944. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
  945. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  946. MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  947. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");
  948. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N051:*");
  949. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N014:*");
  950. MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnCR620:*");
  951. MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnU270series:*");
  952. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnU90/U100:*");