msi-laptop.c 10 KB

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