msi-laptop.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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 const struct attribute_group msipf_attribute_group = {
  433. .attrs = msipf_attributes
  434. };
  435. static const 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 const struct dmi_system_id msi_dmi_table[] __initconst = {
  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_dev;
  793. return 0;
  794. err_free_dev:
  795. input_free_device(msi_laptop_input_dev);
  796. return err;
  797. }
  798. static int __init load_scm_model_init(struct platform_device *sdev)
  799. {
  800. u8 data;
  801. int result;
  802. if (!quirks->ec_read_only) {
  803. /* allow userland write sysfs file */
  804. dev_attr_bluetooth.store = store_bluetooth;
  805. dev_attr_wlan.store = store_wlan;
  806. dev_attr_threeg.store = store_threeg;
  807. dev_attr_bluetooth.attr.mode |= S_IWUSR;
  808. dev_attr_wlan.attr.mode |= S_IWUSR;
  809. dev_attr_threeg.attr.mode |= S_IWUSR;
  810. }
  811. /* disable hardware control by fn key */
  812. result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
  813. if (result < 0)
  814. return result;
  815. result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
  816. data | MSI_STANDARD_EC_SCM_LOAD_MASK);
  817. if (result < 0)
  818. return result;
  819. /* initial rfkill */
  820. result = rfkill_init(sdev);
  821. if (result < 0)
  822. goto fail_rfkill;
  823. /* setup input device */
  824. result = msi_laptop_input_setup();
  825. if (result)
  826. goto fail_input;
  827. result = i8042_install_filter(msi_laptop_i8042_filter);
  828. if (result) {
  829. pr_err("Unable to install key filter\n");
  830. goto fail_filter;
  831. }
  832. return 0;
  833. fail_filter:
  834. input_unregister_device(msi_laptop_input_dev);
  835. fail_input:
  836. rfkill_cleanup();
  837. fail_rfkill:
  838. return result;
  839. }
  840. static int __init msi_init(void)
  841. {
  842. int ret;
  843. if (acpi_disabled)
  844. return -ENODEV;
  845. dmi_check_system(msi_dmi_table);
  846. if (!quirks)
  847. /* quirks may be NULL if no match in DMI table */
  848. quirks = &quirk_load_scm_model;
  849. if (force)
  850. quirks = &quirk_old_ec_model;
  851. if (!quirks->old_ec_model)
  852. get_threeg_exists();
  853. if (auto_brightness < 0 || auto_brightness > 2)
  854. return -EINVAL;
  855. /* Register backlight stuff */
  856. if (quirks->old_ec_model ||
  857. acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  858. struct backlight_properties props;
  859. memset(&props, 0, sizeof(struct backlight_properties));
  860. props.type = BACKLIGHT_PLATFORM;
  861. props.max_brightness = MSI_LCD_LEVEL_MAX - 1;
  862. msibl_device = backlight_device_register("msi-laptop-bl", NULL,
  863. NULL, &msibl_ops,
  864. &props);
  865. if (IS_ERR(msibl_device))
  866. return PTR_ERR(msibl_device);
  867. }
  868. ret = platform_driver_register(&msipf_driver);
  869. if (ret)
  870. goto fail_backlight;
  871. /* Register platform stuff */
  872. msipf_device = platform_device_alloc("msi-laptop-pf", -1);
  873. if (!msipf_device) {
  874. ret = -ENOMEM;
  875. goto fail_platform_driver;
  876. }
  877. ret = platform_device_add(msipf_device);
  878. if (ret)
  879. goto fail_device_add;
  880. if (quirks->load_scm_model && (load_scm_model_init(msipf_device) < 0)) {
  881. ret = -EINVAL;
  882. goto fail_scm_model_init;
  883. }
  884. ret = sysfs_create_group(&msipf_device->dev.kobj,
  885. &msipf_attribute_group);
  886. if (ret)
  887. goto fail_create_group;
  888. if (!quirks->old_ec_model) {
  889. if (threeg_exists)
  890. ret = device_create_file(&msipf_device->dev,
  891. &dev_attr_threeg);
  892. if (ret)
  893. goto fail_create_attr;
  894. } else {
  895. ret = sysfs_create_group(&msipf_device->dev.kobj,
  896. &msipf_old_attribute_group);
  897. if (ret)
  898. goto fail_create_attr;
  899. /* Disable automatic brightness control by default because
  900. * this module was probably loaded to do brightness control in
  901. * software. */
  902. if (auto_brightness != 2)
  903. set_auto_brightness(auto_brightness);
  904. }
  905. pr_info("driver " MSI_DRIVER_VERSION " successfully loaded\n");
  906. return 0;
  907. fail_create_attr:
  908. sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  909. fail_create_group:
  910. if (quirks->load_scm_model) {
  911. i8042_remove_filter(msi_laptop_i8042_filter);
  912. cancel_delayed_work_sync(&msi_rfkill_dwork);
  913. cancel_work_sync(&msi_rfkill_work);
  914. rfkill_cleanup();
  915. }
  916. fail_scm_model_init:
  917. platform_device_del(msipf_device);
  918. fail_device_add:
  919. platform_device_put(msipf_device);
  920. fail_platform_driver:
  921. platform_driver_unregister(&msipf_driver);
  922. fail_backlight:
  923. backlight_device_unregister(msibl_device);
  924. return ret;
  925. }
  926. static void __exit msi_cleanup(void)
  927. {
  928. if (quirks->load_scm_model) {
  929. i8042_remove_filter(msi_laptop_i8042_filter);
  930. input_unregister_device(msi_laptop_input_dev);
  931. cancel_delayed_work_sync(&msi_rfkill_dwork);
  932. cancel_work_sync(&msi_rfkill_work);
  933. rfkill_cleanup();
  934. }
  935. sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  936. if (!quirks->old_ec_model && threeg_exists)
  937. device_remove_file(&msipf_device->dev, &dev_attr_threeg);
  938. platform_device_unregister(msipf_device);
  939. platform_driver_unregister(&msipf_driver);
  940. backlight_device_unregister(msibl_device);
  941. if (quirks->old_ec_model) {
  942. /* Enable automatic brightness control again */
  943. if (auto_brightness != 2)
  944. set_auto_brightness(1);
  945. }
  946. pr_info("driver unloaded\n");
  947. }
  948. module_init(msi_init);
  949. module_exit(msi_cleanup);
  950. MODULE_AUTHOR("Lennart Poettering");
  951. MODULE_DESCRIPTION("MSI Laptop Support");
  952. MODULE_VERSION(MSI_DRIVER_VERSION);
  953. MODULE_LICENSE("GPL");
  954. MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  955. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
  956. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  957. MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  958. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");
  959. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N051:*");
  960. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N014:*");
  961. MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnCR620:*");
  962. MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnU270series:*");
  963. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnU90/U100:*");