msi-laptop.c 10 KB

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