msi-laptop.c 28 KB

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