compal-laptop.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*-*-linux-c-*-*/
  2. /*
  3. Copyright (C) 2008 Cezary Jackiewicz <cezary.jackiewicz (at) gmail.com>
  4. based on MSI driver
  5. Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. 02110-1301, USA.
  18. */
  19. /*
  20. * comapl-laptop.c - Compal laptop support.
  21. *
  22. * This driver exports a few files in /sys/devices/platform/compal-laptop/:
  23. *
  24. * wlan - wlan subsystem state: contains 0 or 1 (rw)
  25. *
  26. * bluetooth - Bluetooth subsystem state: contains 0 or 1 (rw)
  27. *
  28. * raw - raw value taken from embedded controller register (ro)
  29. *
  30. * In addition to these platform device attributes the driver
  31. * registers itself in the Linux backlight control subsystem and is
  32. * available to userspace under /sys/class/backlight/compal-laptop/.
  33. *
  34. * This driver might work on other laptops produced by Compal. If you
  35. * want to try it you can pass force=1 as argument to the module which
  36. * will force it to load even when the DMI data doesn't identify the
  37. * laptop as FL9x.
  38. */
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/init.h>
  42. #include <linux/acpi.h>
  43. #include <linux/dmi.h>
  44. #include <linux/backlight.h>
  45. #include <linux/platform_device.h>
  46. #define COMPAL_DRIVER_VERSION "0.2.6"
  47. #define COMPAL_LCD_LEVEL_MAX 8
  48. #define COMPAL_EC_COMMAND_WIRELESS 0xBB
  49. #define COMPAL_EC_COMMAND_LCD_LEVEL 0xB9
  50. #define KILLSWITCH_MASK 0x10
  51. #define WLAN_MASK 0x01
  52. #define BT_MASK 0x02
  53. static int force;
  54. module_param(force, bool, 0);
  55. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  56. /* Hardware access */
  57. static int set_lcd_level(int level)
  58. {
  59. if (level < 0 || level >= COMPAL_LCD_LEVEL_MAX)
  60. return -EINVAL;
  61. ec_write(COMPAL_EC_COMMAND_LCD_LEVEL, level);
  62. return 0;
  63. }
  64. static int get_lcd_level(void)
  65. {
  66. u8 result;
  67. ec_read(COMPAL_EC_COMMAND_LCD_LEVEL, &result);
  68. return (int) result;
  69. }
  70. static int set_wlan_state(int state)
  71. {
  72. u8 result, value;
  73. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  74. if ((result & KILLSWITCH_MASK) == 0)
  75. return -EINVAL;
  76. else {
  77. if (state)
  78. value = (u8) (result | WLAN_MASK);
  79. else
  80. value = (u8) (result & ~WLAN_MASK);
  81. ec_write(COMPAL_EC_COMMAND_WIRELESS, value);
  82. }
  83. return 0;
  84. }
  85. static int set_bluetooth_state(int state)
  86. {
  87. u8 result, value;
  88. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  89. if ((result & KILLSWITCH_MASK) == 0)
  90. return -EINVAL;
  91. else {
  92. if (state)
  93. value = (u8) (result | BT_MASK);
  94. else
  95. value = (u8) (result & ~BT_MASK);
  96. ec_write(COMPAL_EC_COMMAND_WIRELESS, value);
  97. }
  98. return 0;
  99. }
  100. static int get_wireless_state(int *wlan, int *bluetooth)
  101. {
  102. u8 result;
  103. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  104. if (wlan) {
  105. if ((result & KILLSWITCH_MASK) == 0)
  106. *wlan = 0;
  107. else
  108. *wlan = result & WLAN_MASK;
  109. }
  110. if (bluetooth) {
  111. if ((result & KILLSWITCH_MASK) == 0)
  112. *bluetooth = 0;
  113. else
  114. *bluetooth = (result & BT_MASK) >> 1;
  115. }
  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 compalbl_ops = {
  128. .get_brightness = bl_get_brightness,
  129. .update_status = bl_update_status,
  130. };
  131. static struct backlight_device *compalbl_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_raw(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. u8 result;
  146. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  147. return sprintf(buf, "%i\n", result);
  148. }
  149. static ssize_t show_bluetooth(struct device *dev,
  150. struct device_attribute *attr, char *buf)
  151. {
  152. int ret, enabled;
  153. ret = get_wireless_state(NULL, &enabled);
  154. if (ret < 0)
  155. return ret;
  156. return sprintf(buf, "%i\n", enabled);
  157. }
  158. static ssize_t store_wlan_state(struct device *dev,
  159. struct device_attribute *attr, const char *buf, size_t count)
  160. {
  161. int state, ret;
  162. if (sscanf(buf, "%i", &state) != 1 || (state < 0 || state > 1))
  163. return -EINVAL;
  164. ret = set_wlan_state(state);
  165. if (ret < 0)
  166. return ret;
  167. return count;
  168. }
  169. static ssize_t store_bluetooth_state(struct device *dev,
  170. struct device_attribute *attr, const char *buf, size_t count)
  171. {
  172. int state, ret;
  173. if (sscanf(buf, "%i", &state) != 1 || (state < 0 || state > 1))
  174. return -EINVAL;
  175. ret = set_bluetooth_state(state);
  176. if (ret < 0)
  177. return ret;
  178. return count;
  179. }
  180. static DEVICE_ATTR(bluetooth, 0644, show_bluetooth, store_bluetooth_state);
  181. static DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan_state);
  182. static DEVICE_ATTR(raw, 0444, show_raw, NULL);
  183. static struct attribute *compal_attributes[] = {
  184. &dev_attr_bluetooth.attr,
  185. &dev_attr_wlan.attr,
  186. &dev_attr_raw.attr,
  187. NULL
  188. };
  189. static struct attribute_group compal_attribute_group = {
  190. .attrs = compal_attributes
  191. };
  192. static struct platform_driver compal_driver = {
  193. .driver = {
  194. .name = "compal-laptop",
  195. .owner = THIS_MODULE,
  196. }
  197. };
  198. static struct platform_device *compal_device;
  199. /* Initialization */
  200. static int dmi_check_cb(const struct dmi_system_id *id)
  201. {
  202. printk(KERN_INFO "compal-laptop: Identified laptop model '%s'.\n",
  203. id->ident);
  204. return 0;
  205. }
  206. static struct dmi_system_id __initdata compal_dmi_table[] = {
  207. {
  208. .ident = "FL90/IFL90",
  209. .matches = {
  210. DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
  211. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  212. },
  213. .callback = dmi_check_cb
  214. },
  215. {
  216. .ident = "FL90/IFL90",
  217. .matches = {
  218. DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
  219. DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
  220. },
  221. .callback = dmi_check_cb
  222. },
  223. {
  224. .ident = "FL91/IFL91",
  225. .matches = {
  226. DMI_MATCH(DMI_BOARD_NAME, "IFL91"),
  227. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  228. },
  229. .callback = dmi_check_cb
  230. },
  231. {
  232. .ident = "FL92/JFL92",
  233. .matches = {
  234. DMI_MATCH(DMI_BOARD_NAME, "JFL92"),
  235. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  236. },
  237. .callback = dmi_check_cb
  238. },
  239. {
  240. .ident = "FT00/IFT00",
  241. .matches = {
  242. DMI_MATCH(DMI_BOARD_NAME, "IFT00"),
  243. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  244. },
  245. .callback = dmi_check_cb
  246. },
  247. { }
  248. };
  249. static int __init compal_init(void)
  250. {
  251. int ret;
  252. if (acpi_disabled)
  253. return -ENODEV;
  254. if (!force && !dmi_check_system(compal_dmi_table))
  255. return -ENODEV;
  256. /* Register backlight stuff */
  257. if (!acpi_video_backlight_support()) {
  258. compalbl_device = backlight_device_register("compal-laptop", NULL, NULL,
  259. &compalbl_ops);
  260. if (IS_ERR(compalbl_device))
  261. return PTR_ERR(compalbl_device);
  262. compalbl_device->props.max_brightness = COMPAL_LCD_LEVEL_MAX-1;
  263. }
  264. ret = platform_driver_register(&compal_driver);
  265. if (ret)
  266. goto fail_backlight;
  267. /* Register platform stuff */
  268. compal_device = platform_device_alloc("compal-laptop", -1);
  269. if (!compal_device) {
  270. ret = -ENOMEM;
  271. goto fail_platform_driver;
  272. }
  273. ret = platform_device_add(compal_device);
  274. if (ret)
  275. goto fail_platform_device1;
  276. ret = sysfs_create_group(&compal_device->dev.kobj,
  277. &compal_attribute_group);
  278. if (ret)
  279. goto fail_platform_device2;
  280. printk(KERN_INFO "compal-laptop: driver "COMPAL_DRIVER_VERSION
  281. " successfully loaded.\n");
  282. return 0;
  283. fail_platform_device2:
  284. platform_device_del(compal_device);
  285. fail_platform_device1:
  286. platform_device_put(compal_device);
  287. fail_platform_driver:
  288. platform_driver_unregister(&compal_driver);
  289. fail_backlight:
  290. backlight_device_unregister(compalbl_device);
  291. return ret;
  292. }
  293. static void __exit compal_cleanup(void)
  294. {
  295. sysfs_remove_group(&compal_device->dev.kobj, &compal_attribute_group);
  296. platform_device_unregister(compal_device);
  297. platform_driver_unregister(&compal_driver);
  298. backlight_device_unregister(compalbl_device);
  299. printk(KERN_INFO "compal-laptop: driver unloaded.\n");
  300. }
  301. module_init(compal_init);
  302. module_exit(compal_cleanup);
  303. MODULE_AUTHOR("Cezary Jackiewicz");
  304. MODULE_DESCRIPTION("Compal Laptop Support");
  305. MODULE_VERSION(COMPAL_DRIVER_VERSION);
  306. MODULE_LICENSE("GPL");
  307. MODULE_ALIAS("dmi:*:rnIFL90:rvrIFT00:*");
  308. MODULE_ALIAS("dmi:*:rnIFL90:rvrREFERENCE:*");
  309. MODULE_ALIAS("dmi:*:rnIFL91:rvrIFT00:*");
  310. MODULE_ALIAS("dmi:*:rnJFL92:rvrIFT00:*");
  311. MODULE_ALIAS("dmi:*:rnIFT00:rvrIFT00:*");