msi-laptop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. #include <linux/module.h>
  49. #include <linux/kernel.h>
  50. #include <linux/init.h>
  51. #include <linux/acpi.h>
  52. #include <linux/dmi.h>
  53. #include <linux/backlight.h>
  54. #include <linux/platform_device.h>
  55. #define MSI_DRIVER_VERSION "0.5"
  56. #define MSI_LCD_LEVEL_MAX 9
  57. #define MSI_EC_COMMAND_WIRELESS 0x10
  58. #define MSI_EC_COMMAND_LCD_LEVEL 0x11
  59. #define MSI_STANDARD_EC_COMMAND_ADDRESS 0x2e
  60. #define MSI_STANDARD_EC_BLUETOOTH_MASK (1 << 0)
  61. #define MSI_STANDARD_EC_WEBCAM_MASK (1 << 1)
  62. #define MSI_STANDARD_EC_WLAN_MASK (1 << 3)
  63. #define MSI_STANDARD_EC_3G_MASK (1 << 4)
  64. static int force;
  65. module_param(force, bool, 0);
  66. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  67. static int auto_brightness;
  68. module_param(auto_brightness, int, 0);
  69. MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
  70. static bool old_ec_model;
  71. static int wlan_s, bluetooth_s, threeg_s;
  72. /* Hardware access */
  73. static int set_lcd_level(int level)
  74. {
  75. u8 buf[2];
  76. if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
  77. return -EINVAL;
  78. buf[0] = 0x80;
  79. buf[1] = (u8) (level*31);
  80. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf), NULL, 0, 1);
  81. }
  82. static int get_lcd_level(void)
  83. {
  84. u8 wdata = 0, rdata;
  85. int result;
  86. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
  87. if (result < 0)
  88. return result;
  89. return (int) rdata / 31;
  90. }
  91. static int get_auto_brightness(void)
  92. {
  93. u8 wdata = 4, rdata;
  94. int result;
  95. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1, 1);
  96. if (result < 0)
  97. return result;
  98. return !!(rdata & 8);
  99. }
  100. static int set_auto_brightness(int enable)
  101. {
  102. u8 wdata[2], rdata;
  103. int result;
  104. wdata[0] = 4;
  105. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1, &rdata, 1, 1);
  106. if (result < 0)
  107. return result;
  108. wdata[0] = 0x84;
  109. wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
  110. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2, NULL, 0, 1);
  111. }
  112. static int get_wireless_state(int *wlan, int *bluetooth)
  113. {
  114. u8 wdata = 0, rdata;
  115. int result;
  116. result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1, 1);
  117. if (result < 0)
  118. return -1;
  119. if (wlan)
  120. *wlan = !!(rdata & 8);
  121. if (bluetooth)
  122. *bluetooth = !!(rdata & 128);
  123. return 0;
  124. }
  125. static int get_wireless_state_ec_standard(void)
  126. {
  127. u8 rdata;
  128. int result;
  129. result = ec_read(MSI_STANDARD_EC_COMMAND_ADDRESS, &rdata);
  130. if (result < 0)
  131. return -1;
  132. wlan_s = !!(rdata & MSI_STANDARD_EC_WLAN_MASK);
  133. bluetooth_s = !!(rdata & MSI_STANDARD_EC_BLUETOOTH_MASK);
  134. threeg_s = !!(rdata & MSI_STANDARD_EC_3G_MASK);
  135. return 0;
  136. }
  137. /* Backlight device stuff */
  138. static int bl_get_brightness(struct backlight_device *b)
  139. {
  140. return get_lcd_level();
  141. }
  142. static int bl_update_status(struct backlight_device *b)
  143. {
  144. return set_lcd_level(b->props.brightness);
  145. }
  146. static struct backlight_ops msibl_ops = {
  147. .get_brightness = bl_get_brightness,
  148. .update_status = bl_update_status,
  149. };
  150. static struct backlight_device *msibl_device;
  151. /* Platform device */
  152. static ssize_t show_wlan(struct device *dev,
  153. struct device_attribute *attr, char *buf)
  154. {
  155. int ret, enabled;
  156. if (old_ec_model) {
  157. ret = get_wireless_state(&enabled, NULL);
  158. } else {
  159. ret = get_wireless_state_ec_standard();
  160. enabled = wlan_s;
  161. }
  162. if (ret < 0)
  163. return ret;
  164. return sprintf(buf, "%i\n", enabled);
  165. }
  166. static ssize_t show_bluetooth(struct device *dev,
  167. struct device_attribute *attr, char *buf)
  168. {
  169. int ret, enabled;
  170. if (old_ec_model) {
  171. ret = get_wireless_state(NULL, &enabled);
  172. } else {
  173. ret = get_wireless_state_ec_standard();
  174. enabled = bluetooth_s;
  175. }
  176. if (ret < 0)
  177. return ret;
  178. return sprintf(buf, "%i\n", enabled);
  179. }
  180. static ssize_t show_threeg(struct device *dev,
  181. struct device_attribute *attr, char *buf)
  182. {
  183. int ret;
  184. /* old msi ec not support 3G */
  185. if (old_ec_model)
  186. return -1;
  187. ret = get_wireless_state_ec_standard();
  188. if (ret < 0)
  189. return ret;
  190. return sprintf(buf, "%i\n", threeg_s);
  191. }
  192. static ssize_t show_lcd_level(struct device *dev,
  193. struct device_attribute *attr, char *buf)
  194. {
  195. int ret;
  196. ret = get_lcd_level();
  197. if (ret < 0)
  198. return ret;
  199. return sprintf(buf, "%i\n", ret);
  200. }
  201. static ssize_t store_lcd_level(struct device *dev,
  202. struct device_attribute *attr, const char *buf, size_t count)
  203. {
  204. int level, ret;
  205. if (sscanf(buf, "%i", &level) != 1 || (level < 0 || level >= MSI_LCD_LEVEL_MAX))
  206. return -EINVAL;
  207. ret = set_lcd_level(level);
  208. if (ret < 0)
  209. return ret;
  210. return count;
  211. }
  212. static ssize_t show_auto_brightness(struct device *dev,
  213. struct device_attribute *attr, char *buf)
  214. {
  215. int ret;
  216. ret = get_auto_brightness();
  217. if (ret < 0)
  218. return ret;
  219. return sprintf(buf, "%i\n", ret);
  220. }
  221. static ssize_t store_auto_brightness(struct device *dev,
  222. struct device_attribute *attr, const char *buf, size_t count)
  223. {
  224. int enable, ret;
  225. if (sscanf(buf, "%i", &enable) != 1 || (enable != (enable & 1)))
  226. return -EINVAL;
  227. ret = set_auto_brightness(enable);
  228. if (ret < 0)
  229. return ret;
  230. return count;
  231. }
  232. static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
  233. static DEVICE_ATTR(auto_brightness, 0644, show_auto_brightness, store_auto_brightness);
  234. static DEVICE_ATTR(bluetooth, 0444, show_bluetooth, NULL);
  235. static DEVICE_ATTR(wlan, 0444, show_wlan, NULL);
  236. static DEVICE_ATTR(threeg, 0444, show_threeg, NULL);
  237. static struct attribute *msipf_attributes[] = {
  238. &dev_attr_lcd_level.attr,
  239. &dev_attr_auto_brightness.attr,
  240. &dev_attr_bluetooth.attr,
  241. &dev_attr_wlan.attr,
  242. NULL
  243. };
  244. static struct attribute_group msipf_attribute_group = {
  245. .attrs = msipf_attributes
  246. };
  247. static struct platform_driver msipf_driver = {
  248. .driver = {
  249. .name = "msi-laptop-pf",
  250. .owner = THIS_MODULE,
  251. }
  252. };
  253. static struct platform_device *msipf_device;
  254. /* Initialization */
  255. static int dmi_check_cb(const struct dmi_system_id *id)
  256. {
  257. printk("msi-laptop: Identified laptop model '%s'.\n", id->ident);
  258. return 0;
  259. }
  260. static struct dmi_system_id __initdata msi_dmi_table[] = {
  261. {
  262. .ident = "MSI S270",
  263. .matches = {
  264. DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
  265. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
  266. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  267. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
  268. },
  269. .callback = dmi_check_cb
  270. },
  271. {
  272. .ident = "MSI S271",
  273. .matches = {
  274. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  275. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1058"),
  276. DMI_MATCH(DMI_PRODUCT_VERSION, "0581"),
  277. DMI_MATCH(DMI_BOARD_NAME, "MS-1058")
  278. },
  279. .callback = dmi_check_cb
  280. },
  281. {
  282. .ident = "MSI S420",
  283. .matches = {
  284. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
  285. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1412"),
  286. DMI_MATCH(DMI_BOARD_VENDOR, "MSI"),
  287. DMI_MATCH(DMI_BOARD_NAME, "MS-1412")
  288. },
  289. .callback = dmi_check_cb
  290. },
  291. {
  292. .ident = "Medion MD96100",
  293. .matches = {
  294. DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
  295. DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
  296. DMI_MATCH(DMI_PRODUCT_VERSION, "0131"),
  297. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR INT'L CO.,LTD")
  298. },
  299. .callback = dmi_check_cb
  300. },
  301. { }
  302. };
  303. static int __init msi_init(void)
  304. {
  305. int ret;
  306. if (acpi_disabled)
  307. return -ENODEV;
  308. if (force || dmi_check_system(msi_dmi_table))
  309. old_ec_model = 1;
  310. if (auto_brightness < 0 || auto_brightness > 2)
  311. return -EINVAL;
  312. /* Register backlight stuff */
  313. if (acpi_video_backlight_support()) {
  314. printk(KERN_INFO "MSI: Brightness ignored, must be controlled "
  315. "by ACPI video driver\n");
  316. } else {
  317. msibl_device = backlight_device_register("msi-laptop-bl", NULL,
  318. NULL, &msibl_ops);
  319. if (IS_ERR(msibl_device))
  320. return PTR_ERR(msibl_device);
  321. msibl_device->props.max_brightness = MSI_LCD_LEVEL_MAX-1;
  322. }
  323. ret = platform_driver_register(&msipf_driver);
  324. if (ret)
  325. goto fail_backlight;
  326. /* Register platform stuff */
  327. msipf_device = platform_device_alloc("msi-laptop-pf", -1);
  328. if (!msipf_device) {
  329. ret = -ENOMEM;
  330. goto fail_platform_driver;
  331. }
  332. ret = platform_device_add(msipf_device);
  333. if (ret)
  334. goto fail_platform_device1;
  335. ret = sysfs_create_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  336. if (ret)
  337. goto fail_platform_device2;
  338. if (!old_ec_model) {
  339. ret = device_create_file(&msipf_device->dev, &dev_attr_threeg);
  340. if (ret)
  341. goto fail_platform_device2;
  342. }
  343. /* Disable automatic brightness control by default because
  344. * this module was probably loaded to do brightness control in
  345. * software. */
  346. if (auto_brightness != 2)
  347. set_auto_brightness(auto_brightness);
  348. printk(KERN_INFO "msi-laptop: driver "MSI_DRIVER_VERSION" successfully loaded.\n");
  349. return 0;
  350. fail_platform_device2:
  351. platform_device_del(msipf_device);
  352. fail_platform_device1:
  353. platform_device_put(msipf_device);
  354. fail_platform_driver:
  355. platform_driver_unregister(&msipf_driver);
  356. fail_backlight:
  357. backlight_device_unregister(msibl_device);
  358. return ret;
  359. }
  360. static void __exit msi_cleanup(void)
  361. {
  362. sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  363. if (!old_ec_model)
  364. device_remove_file(&msipf_device->dev, &dev_attr_threeg);
  365. platform_device_unregister(msipf_device);
  366. platform_driver_unregister(&msipf_driver);
  367. backlight_device_unregister(msibl_device);
  368. /* Enable automatic brightness control again */
  369. if (auto_brightness != 2)
  370. set_auto_brightness(1);
  371. printk(KERN_INFO "msi-laptop: driver unloaded.\n");
  372. }
  373. module_init(msi_init);
  374. module_exit(msi_cleanup);
  375. MODULE_AUTHOR("Lennart Poettering");
  376. MODULE_DESCRIPTION("MSI Laptop Support");
  377. MODULE_VERSION(MSI_DRIVER_VERSION);
  378. MODULE_LICENSE("GPL");
  379. MODULE_ALIAS("dmi:*:svnMICRO-STARINT'LCO.,LTD:pnMS-1013:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  380. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1058:pvr0581:rvnMSI:rnMS-1058:*:ct10:*");
  381. MODULE_ALIAS("dmi:*:svnMicro-StarInternational:pnMS-1412:*:rvnMSI:rnMS-1412:*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  382. MODULE_ALIAS("dmi:*:svnNOTEBOOK:pnSAM2000:pvr0131*:cvnMICRO-STARINT'LCO.,LTD:ct10:*");
  383. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-N034:*");