msi-laptop.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*-*-linux-c-*-*/
  2. /*
  3. Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. /*
  18. * msi-laptop.c - MSI S270 laptop support. This laptop is sold under
  19. * various brands, including "Cytron/TCM/Medion/Tchibo MD96100".
  20. *
  21. * Driver also supports S271, S420 models.
  22. *
  23. * This driver exports a few files in /sys/devices/platform/msi-laptop-pf/:
  24. *
  25. * lcd_level - Screen brightness: contains a single integer in the
  26. * range 0..8. (rw)
  27. *
  28. * auto_brightness - Enable automatic brightness control: contains
  29. * either 0 or 1. If set to 1 the hardware adjusts the screen
  30. * brightness automatically when the power cord is
  31. * plugged/unplugged. (rw)
  32. *
  33. * wlan - WLAN subsystem enabled: contains either 0 or 1. (ro)
  34. *
  35. * bluetooth - Bluetooth subsystem enabled: contains either 0 or 1
  36. * Please note that this file is constantly 0 if no Bluetooth
  37. * hardware is available. (ro)
  38. *
  39. * In addition to these platform device attributes the driver
  40. * registers itself in the Linux backlight control subsystem and is
  41. * available to userspace under /sys/class/backlight/msi-laptop-bl/.
  42. *
  43. * This driver might work on other laptops produced by MSI. If you
  44. * want to try it you can pass force=1 as argument to the module which
  45. * will force it to load even when the DMI data doesn't identify the
  46. * laptop as MSI S270. YMMV.
  47. */
  48. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  49. #include <linux/module.h>
  50. #include <linux/kernel.h>
  51. #include <linux/init.h>
  52. #include <linux/acpi.h>
  53. #include <linux/dmi.h>
  54. #include <linux/backlight.h>
  55. #include <linux/platform_device.h>
  56. #include <linux/rfkill.h>
  57. #include <linux/i8042.h>
  58. #include <linux/input.h>
  59. #include <linux/input/sparse-keymap.h>
  60. #define MSI_DRIVER_VERSION "0.5"
  61. #define MSI_LCD_LEVEL_MAX 9
  62. #define MSI_EC_COMMAND_WIRELESS 0x10
  63. #define MSI_EC_COMMAND_LCD_LEVEL 0x11
  64. #define MSI_STANDARD_EC_COMMAND_ADDRESS 0x2e
  65. #define MSI_STANDARD_EC_BLUETOOTH_MASK (1 << 0)
  66. #define MSI_STANDARD_EC_WEBCAM_MASK (1 << 1)
  67. #define MSI_STANDARD_EC_WLAN_MASK (1 << 3)
  68. #define MSI_STANDARD_EC_3G_MASK (1 << 4)
  69. /* For set SCM load flag to disable BIOS fn key */
  70. #define MSI_STANDARD_EC_SCM_LOAD_ADDRESS 0x2d
  71. #define MSI_STANDARD_EC_SCM_LOAD_MASK (1 << 0)
  72. #define MSI_STANDARD_EC_TOUCHPAD_ADDRESS 0xe4
  73. #define MSI_STANDARD_EC_TOUCHPAD_MASK (1 << 4)
  74. #ifdef CONFIG_PM_SLEEP
  75. static int msi_laptop_resume(struct device *device);
  76. #endif
  77. static SIMPLE_DEV_PM_OPS(msi_laptop_pm, NULL, msi_laptop_resume);
  78. #define MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS 0x2f
  79. static bool force;
  80. module_param(force, bool, 0);
  81. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  82. static int auto_brightness;
  83. module_param(auto_brightness, int, 0);
  84. MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
  85. static const struct key_entry msi_laptop_keymap[] = {
  86. {KE_KEY, KEY_TOUCHPAD_ON, {KEY_TOUCHPAD_ON} }, /* Touch Pad On */
  87. {KE_KEY, KEY_TOUCHPAD_OFF, {KEY_TOUCHPAD_OFF} },/* Touch Pad On */
  88. {KE_END, 0}
  89. };
  90. static struct input_dev *msi_laptop_input_dev;
  91. static bool old_ec_model;
  92. static int wlan_s, bluetooth_s, threeg_s;
  93. static int threeg_exists;
  94. /* Some MSI 3G netbook only have one fn key to control Wlan/Bluetooth/3G,
  95. * those netbook will load the SCM (windows app) to disable the original
  96. * Wlan/Bluetooth control by BIOS when user press fn key, then control
  97. * Wlan/Bluetooth/3G by SCM (software control by OS). Without SCM, user
  98. * cann't on/off 3G module on those 3G netbook.
  99. * On Linux, msi-laptop driver will do the same thing to disable the
  100. * original BIOS control, then might need use HAL or other userland
  101. * application to do the software control that simulate with SCM.
  102. * e.g. MSI N034 netbook
  103. */
  104. static bool load_scm_model;
  105. static struct rfkill *rfk_wlan, *rfk_bluetooth, *rfk_threeg;
  106. /* Hardware access */
  107. static int set_lcd_level(int level)
  108. {
  109. u8 buf[2];
  110. if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
  111. return -EINVAL;
  112. buf[0] = 0x80;
  113. buf[1] = (u8) (level*31);
  114. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf),
  115. NULL, 0);
  116. }
  117. static int get_lcd_level(void)
  118. {
  119. u8 wdata = 0, rdata;
  120. int result;
  121. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1,
  122. &rdata, 1);
  123. if (result < 0)
  124. return result;
  125. return (int) rdata / 31;
  126. }
  127. static int get_auto_brightness(void)
  128. {
  129. u8 wdata = 4, 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 !!(rdata & 8);
  136. }
  137. static int set_auto_brightness(int enable)
  138. {
  139. u8 wdata[2], rdata;
  140. int result;
  141. wdata[0] = 4;
  142. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1,
  143. &rdata, 1);
  144. if (result < 0)
  145. return result;
  146. wdata[0] = 0x84;
  147. wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
  148. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2,
  149. NULL, 0);
  150. }
  151. static ssize_t set_device_state(const char *buf, size_t count, u8 mask)
  152. {
  153. int status;
  154. u8 wdata = 0, rdata;
  155. int result;
  156. if (sscanf(buf, "%i", &status) != 1 || (status < 0 || status > 1))
  157. return -EINVAL;
  158. /* read current device state */
  159. result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
  160. if (result < 0)
  161. return -EINVAL;
  162. if (!!(rdata & mask) != status) {
  163. /* reverse device bit */
  164. if (rdata & mask)
  165. wdata = rdata & ~mask;
  166. else
  167. wdata = rdata | mask;
  168. result = ec_write(MSI_STANDARD_EC_COMMAND_ADDRESS, wdata);
  169. if (result < 0)
  170. return -EINVAL;
  171. }
  172. return count;
  173. }
  174. static int get_wireless_state(int *wlan, int *bluetooth)
  175. {
  176. u8 wdata = 0, rdata;
  177. int result;
  178. result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1);
  179. if (result < 0)
  180. return -1;
  181. if (wlan)
  182. *wlan = !!(rdata & 8);
  183. if (bluetooth)
  184. *bluetooth = !!(rdata & 128);
  185. return 0;
  186. }
  187. static int get_wireless_state_ec_standard(void)
  188. {
  189. u8 rdata;
  190. int result;
  191. result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
  192. if (result < 0)
  193. return -1;
  194. wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);
  195. bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);
  196. threeg_s = !!(rdata & MSI_STANDARD_EC_3G_MASK);
  197. return 0;
  198. }
  199. static int get_threeg_exists(void)
  200. {
  201. u8 rdata;
  202. int result;
  203. result = ec_read(MSI_STANDARD_EC_DEVICES_EXISTS_ADDRESS, &rdata);
  204. if (result < 0)
  205. return -1;
  206. threeg_exists = !!(rdata & MSI_STANDARD_EC_3G_MASK);
  207. return 0;
  208. }
  209. /* Backlight device stuff */
  210. static int bl_get_brightness(struct backlight_device *b)
  211. {
  212. return get_lcd_level();
  213. }
  214. static int bl_update_status(struct backlight_device *b)
  215. {
  216. return set_lcd_level(b->props.brightness);
  217. }
  218. static const struct backlight_ops msibl_ops = {
  219. .get_brightness = bl_get_brightness,
  220. .update_status = bl_update_status,
  221. };
  222. static struct backlight_device *msibl_device;
  223. /* Platform device */
  224. static ssize_t show_wlan(struct device *dev,
  225. struct device_attribute *attr, char *buf)
  226. {
  227. int ret, enabled;
  228. if (old_ec_model) {
  229. ret = get_wireless_state(&enabled, NULL);
  230. } else {
  231. ret = get_wireless_state_ec_standard();
  232. enabled = wlan_s;
  233. }
  234. if (ret < 0)
  235. return ret;
  236. return sprintf(buf, "%i\n", enabled);
  237. }
  238. static ssize_t store_wlan(struct device *dev,
  239. struct device_attribute *attr, const char *buf, size_t count)
  240. {
  241. return set_device_state(buf, count, MSI_STANDARD_EC_WLAN_MASK);
  242. }
  243. static ssize_t show_bluetooth(struct device *dev,
  244. struct device_attribute *attr, char *buf)
  245. {
  246. int ret, enabled;
  247. if (old_ec_model) {
  248. ret = get_wireless_state(NULL, &enabled);
  249. } else {
  250. ret = get_wireless_state_ec_standard();
  251. enabled = bluetooth_s;
  252. }
  253. if (ret < 0)
  254. return ret;
  255. return sprintf(buf, "%i\n", enabled);
  256. }
  257. static ssize_t store_bluetooth(struct device *dev,
  258. struct device_attribute *attr, const char *buf, size_t count)
  259. {
  260. return set_device_state(buf, count, MSI_STANDARD_EC_BLUETOOTH_MASK);
  261. }
  262. static ssize_t show_threeg(struct device *dev,
  263. struct device_attribute *attr, char *buf)
  264. {
  265. int ret;
  266. /* old msi ec not support 3G */
  267. if (old_ec_model)
  268. return -1;
  269. ret = get_wireless_state_ec_standard();
  270. if (ret < 0)
  271. return ret;
  272. return sprintf(buf, "%i\n", threeg_s);
  273. }
  274. static ssize_t store_threeg(struct device *dev,
  275. struct device_attribute *attr, const char *buf, size_t count)
  276. {
  277. return set_device_state(buf, count, MSI_STANDARD_EC_3G_MASK);
  278. }
  279. static ssize_t show_lcd_level(struct device *dev,
  280. struct device_attribute *attr, char *buf)
  281. {
  282. int ret;
  283. ret = get_lcd_level();
  284. if (ret < 0)
  285. return ret;
  286. return sprintf(buf, "%i\n", ret);
  287. }
  288. static ssize_t store_lcd_level(struct device *dev,
  289. struct device_attribute *attr, const char *buf, size_t count)
  290. {
  291. int level, ret;
  292. if (sscanf(buf, "%i", &level) != 1 ||
  293. (level < 0 || level >= MSI_LCD_LEVEL_MAX))
  294. return -EINVAL;
  295. ret = set_lcd_level(level);
  296. if (ret < 0)
  297. return ret;
  298. return count;
  299. }
  300. static ssize_t show_auto_brightness(struct device *dev,
  301. struct device_attribute *attr, char *buf)
  302. {
  303. int ret;
  304. ret = get_auto_brightness();
  305. if (ret < 0)
  306. return ret;
  307. return sprintf(buf, "%i\n", ret);
  308. }
  309. static ssize_t store_auto_brightness(struct device *dev,
  310. struct device_attribute *attr, const char *buf, size_t count)
  311. {
  312. int enable, ret;
  313. if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
  314. return -EINVAL;
  315. ret = set_auto_brightness(enable);
  316. if (ret < 0)
  317. return ret;
  318. return count;
  319. }
  320. static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
  321. static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness,
  322. store_auto_brightness);
  323. static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
  324. static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
  325. static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
  326. static struct attribute *msipf_attributes[] = {
  327. &dev_attr_lcd_level.attr,
  328. &dev_attr_auto_brightness.attr,
  329. &dev_attr_bluetooth.attr,
  330. &dev_attr_wlan.attr,
  331. NULL
  332. };
  333. static struct attribute_group msipf_attribute_group = {
  334. .attrs = msipf_attributes
  335. };
  336. static struct platform_driver msipf_driver = {
  337. .driver = {
  338. .name = "msi-laptop-pf",
  339. .owner = THIS_MODULE,
  340. .pm = &msi_laptop_pm,
  341. },
  342. };
  343. static struct platform_device *msipf_device;
  344. /* Initialization */
  345. static int dmi_check_cb(const struct dmi_system_id *id)
  346. {
  347. pr_info("Identified laptop model '%s'\n", id->ident);
  348. return 1;
  349. }
  350. static struct dmi_system_id __initdata msi_dmi_table[] = {
  351. {
  352. .ident = "MSI S270",
  353. .matches = {
  354. DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
  355. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
  356. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  357. DMI_MATCH(DMI_CHASSIS_VENDOR,
  358. "MICRO-STAR INT'L CO.,LTD")
  359. },
  360. .callback = dmi_check_cb
  361. },
  362. {
  363. .ident = "MSI S271",
  364. .matches = {
  365. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  366. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
  367. DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
  368. DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
  369. },
  370. .callback = dmi_check_cb
  371. },
  372. {
  373. .ident = "MSI S420",
  374. .matches = {
  375. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  376. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
  377. DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
  378. DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
  379. },
  380. .callback = dmi_check_cb
  381. },
  382. {
  383. .ident = "Medion MD96100",
  384. .matches = {
  385. DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
  386. DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
  387. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  388. DMI_MATCH(DMI_CHASSIS_VENDOR,
  389. "MICRO-STAR INT'L CO.,LTD")
  390. },
  391. .callback = dmi_check_cb
  392. },
  393. { }
  394. };
  395. static struct dmi_system_id __initdata msi_load_scm_models_dmi_table[] = {
  396. {
  397. .ident = "MSI N034",
  398. .matches = {
  399. DMI_MATCH(DMI_SYS_VENDOR,
  400. "MICRO-STAR INTERNATIONAL CO., LTD"),
  401. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N034"),
  402. DMI_MATCH(DMI_CHASSIS_VENDOR,
  403. "MICRO-STAR INTERNATIONAL CO., LTD")
  404. },
  405. .callback = dmi_check_cb
  406. },
  407. {
  408. .ident = "MSI N051",
  409. .matches = {
  410. DMI_MATCH(DMI_SYS_VENDOR,
  411. "MICRO-STAR INTERNATIONAL CO., LTD"),
  412. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N051"),
  413. DMI_MATCH(DMI_CHASSIS_VENDOR,
  414. "MICRO-STAR INTERNATIONAL CO., LTD")
  415. },
  416. .callback = dmi_check_cb
  417. },
  418. {
  419. .ident = "MSI N014",
  420. .matches = {
  421. DMI_MATCH(DMI_SYS_VENDOR,
  422. "MICRO-STAR INTERNATIONAL CO., LTD"),
  423. DMI_MATCH(DMI_PRODUCT_NAME, "MS-N014"),
  424. },
  425. .callback = dmi_check_cb
  426. },
  427. {
  428. .ident = "MSI CR620",
  429. .matches = {
  430. DMI_MATCH(DMI_SYS_VENDOR,
  431. "Micro-Star International"),
  432. DMI_MATCH(DMI_PRODUCT_NAME, "CR620"),
  433. },
  434. .callback = dmi_check_cb
  435. },
  436. {
  437. .ident = "MSI U270",
  438. .matches = {
  439. DMI_MATCH(DMI_SYS_VENDOR,
  440. "Micro-Star International Co., Ltd."),
  441. DMI_MATCH(DMI_PRODUCT_NAME, "U270 series"),
  442. },
  443. .callback = dmi_check_cb
  444. },
  445. { }
  446. };
  447. static int rfkill_bluetooth_set(void *data, bool blocked)
  448. {
  449. /* Do something with blocked...*/
  450. /*
  451. * blocked == false is on
  452. * blocked == true is off
  453. */
  454. if (blocked)
  455. set_device_state("0", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
  456. else
  457. set_device_state("1", 0, MSI_STANDARD_EC_BLUETOOTH_MASK);
  458. return 0;
  459. }
  460. static int rfkill_wlan_set(void *data, bool blocked)
  461. {
  462. if (blocked)
  463. set_device_state("0", 0, MSI_STANDARD_EC_WLAN_MASK);
  464. else
  465. set_device_state("1", 0, MSI_STANDARD_EC_WLAN_MASK);
  466. return 0;
  467. }
  468. static int rfkill_threeg_set(void *data, bool blocked)
  469. {
  470. if (blocked)
  471. set_device_state("0", 0, MSI_STANDARD_EC_3G_MASK);
  472. else
  473. set_device_state("1", 0, MSI_STANDARD_EC_3G_MASK);
  474. return 0;
  475. }
  476. static const struct rfkill_ops rfkill_bluetooth_ops = {
  477. .set_block = rfkill_bluetooth_set
  478. };
  479. static const struct rfkill_ops rfkill_wlan_ops = {
  480. .set_block = rfkill_wlan_set
  481. };
  482. static const struct rfkill_ops rfkill_threeg_ops = {
  483. .set_block = rfkill_threeg_set
  484. };
  485. static void rfkill_cleanup(void)
  486. {
  487. if (rfk_bluetooth) {
  488. rfkill_unregister(rfk_bluetooth);
  489. rfkill_destroy(rfk_bluetooth);
  490. }
  491. if (rfk_threeg) {
  492. rfkill_unregister(rfk_threeg);
  493. rfkill_destroy(rfk_threeg);
  494. }
  495. if (rfk_wlan) {
  496. rfkill_unregister(rfk_wlan);
  497. rfkill_destroy(rfk_wlan);
  498. }
  499. }
  500. static void msi_update_rfkill(struct work_struct *ignored)
  501. {
  502. get_wireless_state_ec_standard();
  503. if (rfk_wlan)
  504. rfkill_set_sw_state(rfk_wlan, !wlan_s);
  505. if (rfk_bluetooth)
  506. rfkill_set_sw_state(rfk_bluetooth, !bluetooth_s);
  507. if (rfk_threeg)
  508. rfkill_set_sw_state(rfk_threeg, !threeg_s);
  509. }
  510. static DECLARE_DELAYED_WORK(msi_rfkill_work, msi_update_rfkill);
  511. static void msi_send_touchpad_key(struct work_struct *ignored)
  512. {
  513. u8 rdata;
  514. int result;
  515. result = ec_read(MSI_STANDARD_EC_TOUCHPAD_ADDRESS, &rdata);
  516. if (result < 0)
  517. return;
  518. sparse_keymap_report_event(msi_laptop_input_dev,
  519. (rdata & MSI_STANDARD_EC_TOUCHPAD_MASK) ?
  520. KEY_TOUCHPAD_ON : KEY_TOUCHPAD_OFF, 1, true);
  521. }
  522. static DECLARE_DELAYED_WORK(msi_touchpad_work, msi_send_touchpad_key);
  523. static bool msi_laptop_i8042_filter(unsigned char data, unsigned char str,
  524. struct serio *port)
  525. {
  526. static bool extended;
  527. if (str & 0x20)
  528. return false;
  529. /* 0x54 wwan, 0x62 bluetooth, 0x76 wlan, 0xE4 touchpad toggle*/
  530. if (unlikely(data == 0xe0)) {
  531. extended = true;
  532. return false;
  533. } else if (unlikely(extended)) {
  534. extended = false;
  535. switch (data) {
  536. case 0xE4:
  537. schedule_delayed_work(&msi_touchpad_work,
  538. round_jiffies_relative(0.5 * HZ));
  539. break;
  540. case 0x54:
  541. case 0x62:
  542. case 0x76:
  543. schedule_delayed_work(&msi_rfkill_work,
  544. round_jiffies_relative(0.5 * HZ));
  545. break;
  546. }
  547. }
  548. return false;
  549. }
  550. static void msi_init_rfkill(struct work_struct *ignored)
  551. {
  552. if (rfk_wlan) {
  553. rfkill_set_sw_state(rfk_wlan, !wlan_s);
  554. rfkill_wlan_set(NULL, !wlan_s);
  555. }
  556. if (rfk_bluetooth) {
  557. rfkill_set_sw_state(rfk_bluetooth, !bluetooth_s);
  558. rfkill_bluetooth_set(NULL, !bluetooth_s);
  559. }
  560. if (rfk_threeg) {
  561. rfkill_set_sw_state(rfk_threeg, !threeg_s);
  562. rfkill_threeg_set(NULL, !threeg_s);
  563. }
  564. }
  565. static DECLARE_DELAYED_WORK(msi_rfkill_init, msi_init_rfkill);
  566. static int rfkill_init(struct platform_device *sdev)
  567. {
  568. /* add rfkill */
  569. int retval;
  570. /* keep the hardware wireless state */
  571. get_wireless_state_ec_standard();
  572. rfk_bluetooth = rfkill_alloc("msi-bluetooth", &sdev->dev,
  573. RFKILL_TYPE_BLUETOOTH,
  574. &rfkill_bluetooth_ops, NULL);
  575. if (!rfk_bluetooth) {
  576. retval = -ENOMEM;
  577. goto err_bluetooth;
  578. }
  579. retval = rfkill_register(rfk_bluetooth);
  580. if (retval)
  581. goto err_bluetooth;
  582. rfk_wlan = rfkill_alloc("msi-wlan", &sdev->dev, RFKILL_TYPE_WLAN,
  583. &rfkill_wlan_ops, NULL);
  584. if (!rfk_wlan) {
  585. retval = -ENOMEM;
  586. goto err_wlan;
  587. }
  588. retval = rfkill_register(rfk_wlan);
  589. if (retval)
  590. goto err_wlan;
  591. if (threeg_exists) {
  592. rfk_threeg = rfkill_alloc("msi-threeg", &sdev->dev,
  593. RFKILL_TYPE_WWAN, &rfkill_threeg_ops, NULL);
  594. if (!rfk_threeg) {
  595. retval = -ENOMEM;
  596. goto err_threeg;
  597. }
  598. retval = rfkill_register(rfk_threeg);
  599. if (retval)
  600. goto err_threeg;
  601. }
  602. /* schedule to run rfkill state initial */
  603. schedule_delayed_work(&msi_rfkill_init,
  604. round_jiffies_relative(1 * HZ));
  605. return 0;
  606. err_threeg:
  607. rfkill_destroy(rfk_threeg);
  608. if (rfk_wlan)
  609. rfkill_unregister(rfk_wlan);
  610. err_wlan:
  611. rfkill_destroy(rfk_wlan);
  612. if (rfk_bluetooth)
  613. rfkill_unregister(rfk_bluetooth);
  614. err_bluetooth:
  615. rfkill_destroy(rfk_bluetooth);
  616. return retval;
  617. }
  618. #ifdef CONFIG_PM_SLEEP
  619. static int msi_laptop_resume(struct device *device)
  620. {
  621. u8 data;
  622. int result;
  623. if (!load_scm_model)
  624. return 0;
  625. /* set load SCM to disable hardware control by fn key */
  626. result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
  627. if (result < 0)
  628. return result;
  629. result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
  630. data | MSI_STANDARD_EC_SCM_LOAD_MASK);
  631. if (result < 0)
  632. return result;
  633. return 0;
  634. }
  635. #endif
  636. static int __init msi_laptop_input_setup(void)
  637. {
  638. int err;
  639. msi_laptop_input_dev = input_allocate_device();
  640. if (!msi_laptop_input_dev)
  641. return -ENOMEM;
  642. msi_laptop_input_dev->name = "MSI Laptop hotkeys";
  643. msi_laptop_input_dev->phys = "msi-laptop/input0";
  644. msi_laptop_input_dev->id.bustype = BUS_HOST;
  645. err = sparse_keymap_setup(msi_laptop_input_dev,
  646. msi_laptop_keymap, NULL);
  647. if (err)
  648. goto err_free_dev;
  649. err = input_register_device(msi_laptop_input_dev);
  650. if (err)
  651. goto err_free_keymap;
  652. return 0;
  653. err_free_keymap:
  654. sparse_keymap_free(msi_laptop_input_dev);
  655. err_free_dev:
  656. input_free_device(msi_laptop_input_dev);
  657. return err;
  658. }
  659. static void msi_laptop_input_destroy(void)
  660. {
  661. sparse_keymap_free(msi_laptop_input_dev);
  662. input_unregister_device(msi_laptop_input_dev);
  663. }
  664. static int __init load_scm_model_init(struct platform_device *sdev)
  665. {
  666. u8 data;
  667. int result;
  668. /* allow userland write sysfs file */
  669. dev_attr_bluetooth.store = store_bluetooth;
  670. dev_attr_wlan.store = store_wlan;
  671. dev_attr_threeg.store = store_threeg;
  672. dev_attr_bluetooth.attr.mode |= S_IWUSR;
  673. dev_attr_wlan.attr.mode |= S_IWUSR;
  674. dev_attr_threeg.attr.mode |= S_IWUSR;
  675. /* disable hardware control by fn key */
  676. result = ec_read(MSI_STANDARD_EC_SCM_LOAD_ADDRESS, &data);
  677. if (result < 0)
  678. return result;
  679. result = ec_write(MSI_STANDARD_EC_SCM_LOAD_ADDRESS,
  680. data | MSI_STANDARD_EC_SCM_LOAD_MASK);
  681. if (result < 0)
  682. return result;
  683. /* initial rfkill */
  684. result = rfkill_init(sdev);
  685. if (result < 0)
  686. goto fail_rfkill;
  687. /* setup input device */
  688. result = msi_laptop_input_setup();
  689. if (result)
  690. goto fail_input;
  691. result = i8042_install_filter(msi_laptop_i8042_filter);
  692. if (result) {
  693. pr_err("Unable to install key filter\n");
  694. goto fail_filter;
  695. }
  696. return 0;
  697. fail_filter:
  698. msi_laptop_input_destroy();
  699. fail_input:
  700. rfkill_cleanup();
  701. fail_rfkill:
  702. return result;
  703. }
  704. static int __init msi_init(void)
  705. {
  706. int ret;
  707. if (acpi_disabled)
  708. return -ENODEV;
  709. if (force || dmi_check_system(msi_dmi_table))
  710. old_ec_model = 1;
  711. if (!old_ec_model)
  712. get_threeg_exists();
  713. if (!old_ec_model && dmi_check_system(msi_load_scm_models_dmi_table))
  714. load_scm_model = 1;
  715. if (auto_brightness < 0 || auto_brightness > 2)
  716. return -EINVAL;
  717. /* Register backlight stuff */
  718. if (acpi_video_backlight_support()) {
  719. pr_info("Brightness ignored, must be controlled by ACPI video driver\n");
  720. } else {
  721. struct backlight_properties props;
  722. memset(&props, 0, sizeof(struct backlight_properties));
  723. props.type = BACKLIGHT_PLATFORM;
  724. props.max_brightness = MSI_LCD_LEVEL_MAX - 1;
  725. msibl_device = backlight_device_register("msi-laptop-bl", NULL,
  726. NULL, &msibl_ops,
  727. &props);
  728. if (IS_ERR(msibl_device))
  729. return PTR_ERR(msibl_device);
  730. }
  731. ret = platform_driver_register(&msipf_driver);
  732. if (ret)
  733. goto fail_backlight;
  734. /* Register platform stuff */
  735. msipf_device = platform_device_alloc("msi-laptop-pf", -1);
  736. if (!msipf_device) {
  737. ret = -ENOMEM;
  738. goto fail_platform_driver;
  739. }
  740. ret = platform_device_add(msipf_device);
  741. if (ret)
  742. goto fail_platform_device1;
  743. if (load_scm_model && (load_scm_model_init(msipf_device) < 0)) {
  744. ret = -EINVAL;
  745. goto fail_platform_device1;
  746. }
  747. ret = sysfs_create_group(&msipf_device->dev.kobj,
  748. &msipf_attribute_group);
  749. if (ret)
  750. goto fail_platform_device2;
  751. if (!old_ec_model) {
  752. if (threeg_exists)
  753. ret = device_create_file(&msipf_device->dev,
  754. &dev_attr_threeg);
  755. if (ret)
  756. goto fail_platform_device2;
  757. }
  758. /* Disable automatic brightness control by default because
  759. * this module was probably loaded to do brightness control in
  760. * software. */
  761. if (auto_brightness != 2)
  762. set_auto_brightness(auto_brightness);
  763. pr_info("driver " MSI_DRIVER_VERSION " successfully loaded\n");
  764. return 0;
  765. fail_platform_device2:
  766. if (load_scm_model) {
  767. i8042_remove_filter(msi_laptop_i8042_filter);
  768. cancel_delayed_work_sync(&msi_rfkill_work);
  769. rfkill_cleanup();
  770. }
  771. platform_device_del(msipf_device);
  772. fail_platform_device1:
  773. platform_device_put(msipf_device);
  774. fail_platform_driver:
  775. platform_driver_unregister(&msipf_driver);
  776. fail_backlight:
  777. backlight_device_unregister(msibl_device);
  778. return ret;
  779. }
  780. static void __exit msi_cleanup(void)
  781. {
  782. if (load_scm_model) {
  783. i8042_remove_filter(msi_laptop_i8042_filter);
  784. msi_laptop_input_destroy();
  785. cancel_delayed_work_sync(&msi_rfkill_work);
  786. rfkill_cleanup();
  787. }
  788. sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  789. if (!old_ec_model && threeg_exists)
  790. device_remove_file(&msipf_device->dev, &dev_attr_threeg);
  791. platform_device_unregister(msipf_device);
  792. platform_driver_unregister(&msipf_driver);
  793. backlight_device_unregister(msibl_device);
  794. /* Enable automatic brightness control again */
  795. if (auto_brightness != 2)
  796. set_auto_brightness(1);
  797. pr_info("driver unloaded\n");
  798. }
  799. module_init(msi_init);
  800. module_exit(msi_cleanup);
  801. MODULE_AUTHOR("Lennart Poettering");
  802. MODULE_DESCRIPTION("MSI Laptop Support");
  803. MODULE_VERSION(MSI_DRIVER_VERSION);
  804. MODULE_LICENSE("GPL");
  805. MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  806. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
  807. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  808. MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  809. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");
  810. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N051:*");
  811. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N014:*");
  812. MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnCR620:*");
  813. MODULE_ALIAS("dmi:*:svnMicro-StarInternational*:pnU270series:*");