msi-laptop.c 26 KB

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