msi-laptop.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. * This driver exports a few files in /sys/devices/platform/msi-laptop-pf/:
  22. *
  23. * lcd_level - Screen brightness: contains a single integer in the
  24. * range 0..8. (rw)
  25. *
  26. * auto_brightness - Enable automatic brightness control: contains
  27. * either 0 or 1. If set to 1 the hardware adjusts the screen
  28. * brightness automatically when the power cord is
  29. * plugged/unplugged. (rw)
  30. *
  31. * wlan - WLAN subsystem enabled: contains either 0 or 1. (ro)
  32. *
  33. * bluetooth - Bluetooth subsystem enabled: contains either 0 or 1
  34. * Please note that this file is constantly 0 if no Bluetooth
  35. * hardware is available. (ro)
  36. *
  37. * In addition to these platform device attributes the driver
  38. * registers itself in the Linux backlight control subsystem and is
  39. * available to userspace under /sys/class/backlight/msi-laptop-bl/.
  40. *
  41. * This driver might work on other laptops produced by MSI. If you
  42. * want to try it you can pass force=1 as argument to the module which
  43. * will force it to load even when the DMI data doesn't identify the
  44. * laptop as MSI S270. YMMV.
  45. */
  46. #include <linux/module.h>
  47. #include <linux/kernel.h>
  48. #include <linux/init.h>
  49. #include <linux/acpi.h>
  50. #include <linux/dmi.h>
  51. #include <linux/backlight.h>
  52. #include <linux/platform_device.h>
  53. #include <linux/autoconf.h>
  54. #define MSI_DRIVER_VERSION "0.5"
  55. #define MSI_LCD_LEVEL_MAX 9
  56. #define MSI_EC_COMMAND_WIRELESS 0x10
  57. #define MSI_EC_COMMAND_LCD_LEVEL 0x11
  58. static int force;
  59. module_param(force, bool, 0);
  60. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  61. static int auto_brightness;
  62. module_param(auto_brightness, int, 0);
  63. MODULE_PARM_DESC(auto_brightness, "Enable automatic brightness control (0: disabled; 1: enabled; 2: don't touch)");
  64. /* Hardware access */
  65. static int set_lcd_level(int level)
  66. {
  67. u8 buf[2];
  68. if (level < 0 || level >= MSI_LCD_LEVEL_MAX)
  69. return -EINVAL;
  70. buf[0] = 0x80;
  71. buf[1] = (u8) (level*31);
  72. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, buf, sizeof(buf), NULL, 0);
  73. }
  74. static int get_lcd_level(void)
  75. {
  76. u8 wdata = 0, rdata;
  77. int result;
  78. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1);
  79. if (result < 0)
  80. return result;
  81. return (int) rdata / 31;
  82. }
  83. static int get_auto_brightness(void)
  84. {
  85. u8 wdata = 4, rdata;
  86. int result;
  87. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, &wdata, 1, &rdata, 1);
  88. if (result < 0)
  89. return result;
  90. return !!(rdata & 8);
  91. }
  92. static int set_auto_brightness(int enable)
  93. {
  94. u8 wdata[2], rdata;
  95. int result;
  96. wdata[0] = 4;
  97. result = ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 1, &rdata, 1);
  98. if (result < 0)
  99. return result;
  100. wdata[0] = 0x84;
  101. wdata[1] = (rdata & 0xF7) | (enable ? 8 : 0);
  102. return ec_transaction(MSI_EC_COMMAND_LCD_LEVEL, wdata, 2, NULL, 0);
  103. }
  104. static int get_wireless_state(int *wlan, int *bluetooth)
  105. {
  106. u8 wdata = 0, rdata;
  107. int result;
  108. result = ec_transaction(MSI_EC_COMMAND_WIRELESS, &wdata, 1, &rdata, 1);
  109. if (result < 0)
  110. return -1;
  111. if (wlan)
  112. *wlan = !!(rdata & 8);
  113. if (bluetooth)
  114. *bluetooth = !!(rdata & 128);
  115. return 0;
  116. }
  117. /* Backlight device stuff */
  118. static int bl_get_brightness(struct backlight_device *b)
  119. {
  120. return get_lcd_level();
  121. }
  122. static int bl_update_status(struct backlight_device *b)
  123. {
  124. return set_lcd_level(b->props->brightness);
  125. }
  126. static struct backlight_properties msibl_props = {
  127. .owner = THIS_MODULE,
  128. .get_brightness = bl_get_brightness,
  129. .update_status = bl_update_status,
  130. .max_brightness = MSI_LCD_LEVEL_MAX-1,
  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 struct dmi_system_id __initdata msi_dmi_table[] = {
  215. {
  216. .ident = "MSI S270",
  217. .matches = {
  218. DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT'L CO.,LTD"),
  219. DMI_MATCH(DMI_PRODUCT_NAME, "MS-1013"),
  220. }
  221. },
  222. {
  223. .ident = "Medion MD96100",
  224. .matches = {
  225. DMI_MATCH(DMI_SYS_VENDOR, "NOTEBOOK"),
  226. DMI_MATCH(DMI_PRODUCT_NAME, "SAM2000"),
  227. }
  228. },
  229. { }
  230. };
  231. static int __init msi_init(void)
  232. {
  233. int ret;
  234. if (acpi_disabled)
  235. return -ENODEV;
  236. if (!force && !dmi_check_system(msi_dmi_table))
  237. return -ENODEV;
  238. if (auto_brightness < 0 || auto_brightness > 2)
  239. return -EINVAL;
  240. /* Register backlight stuff */
  241. msibl_device = backlight_device_register("msi-laptop-bl", NULL, &msibl_props);
  242. if (IS_ERR(msibl_device))
  243. return PTR_ERR(msibl_device);
  244. ret = platform_driver_register(&msipf_driver);
  245. if (ret)
  246. goto fail_backlight;
  247. /* Register platform stuff */
  248. msipf_device = platform_device_alloc("msi-laptop-pf", -1);
  249. if (!msipf_device) {
  250. ret = -ENOMEM;
  251. goto fail_platform_driver;
  252. }
  253. ret = platform_device_add(msipf_device);
  254. if (ret)
  255. goto fail_platform_device1;
  256. ret = sysfs_create_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  257. if (ret)
  258. goto fail_platform_device2;
  259. /* Disable automatic brightness control by default because
  260. * this module was probably loaded to do brightness control in
  261. * software. */
  262. if (auto_brightness != 2)
  263. set_auto_brightness(auto_brightness);
  264. printk(KERN_INFO "msi-laptop: driver "MSI_DRIVER_VERSION" successfully loaded.\n");
  265. return 0;
  266. fail_platform_device2:
  267. platform_device_del(msipf_device);
  268. fail_platform_device1:
  269. platform_device_put(msipf_device);
  270. fail_platform_driver:
  271. platform_driver_unregister(&msipf_driver);
  272. fail_backlight:
  273. backlight_device_unregister(msibl_device);
  274. return ret;
  275. }
  276. static void __exit msi_cleanup(void)
  277. {
  278. sysfs_remove_group(&msipf_device->dev.kobj, &msipf_attribute_group);
  279. platform_device_unregister(msipf_device);
  280. platform_driver_unregister(&msipf_driver);
  281. backlight_device_unregister(msibl_device);
  282. /* Enable automatic brightness control again */
  283. if (auto_brightness != 2)
  284. set_auto_brightness(1);
  285. printk(KERN_INFO "msi-laptop: driver unloaded.\n");
  286. }
  287. module_init(msi_init);
  288. module_exit(msi_cleanup);
  289. MODULE_AUTHOR("Lennart Poettering");
  290. MODULE_DESCRIPTION("MSI Laptop Support");
  291. MODULE_VERSION(MSI_DRIVER_VERSION);
  292. MODULE_LICENSE("GPL");