compal-laptop.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #include <linux/autoconf.h>
  47. #define COMPAL_DRIVER_VERSION "0.2.6"
  48. #define COMPAL_LCD_LEVEL_MAX 8
  49. #define COMPAL_EC_COMMAND_WIRELESS 0xBB
  50. #define COMPAL_EC_COMMAND_LCD_LEVEL 0xB9
  51. #define KILLSWITCH_MASK 0x10
  52. #define WLAN_MASK 0x01
  53. #define BT_MASK 0x02
  54. static int force;
  55. module_param(force, bool, 0);
  56. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  57. /* Hardware access */
  58. static int set_lcd_level(int level)
  59. {
  60. if (level < 0 || level >= COMPAL_LCD_LEVEL_MAX)
  61. return -EINVAL;
  62. ec_write(COMPAL_EC_COMMAND_LCD_LEVEL, level);
  63. return 0;
  64. }
  65. static int get_lcd_level(void)
  66. {
  67. u8 result;
  68. ec_read(COMPAL_EC_COMMAND_LCD_LEVEL, &result);
  69. return (int) result;
  70. }
  71. static int set_wlan_state(int state)
  72. {
  73. u8 result, value;
  74. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  75. if ((result & KILLSWITCH_MASK) == 0)
  76. return -EINVAL;
  77. else {
  78. if (state)
  79. value = (u8) (result | WLAN_MASK);
  80. else
  81. value = (u8) (result & ~WLAN_MASK);
  82. ec_write(COMPAL_EC_COMMAND_WIRELESS, value);
  83. }
  84. return 0;
  85. }
  86. static int set_bluetooth_state(int state)
  87. {
  88. u8 result, value;
  89. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  90. if ((result & KILLSWITCH_MASK) == 0)
  91. return -EINVAL;
  92. else {
  93. if (state)
  94. value = (u8) (result | BT_MASK);
  95. else
  96. value = (u8) (result & ~BT_MASK);
  97. ec_write(COMPAL_EC_COMMAND_WIRELESS, value);
  98. }
  99. return 0;
  100. }
  101. static int get_wireless_state(int *wlan, int *bluetooth)
  102. {
  103. u8 result;
  104. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  105. if (wlan) {
  106. if ((result & KILLSWITCH_MASK) == 0)
  107. *wlan = 0;
  108. else
  109. *wlan = result & WLAN_MASK;
  110. }
  111. if (bluetooth) {
  112. if ((result & KILLSWITCH_MASK) == 0)
  113. *bluetooth = 0;
  114. else
  115. *bluetooth = (result & BT_MASK) >> 1;
  116. }
  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 compalbl_ops = {
  129. .get_brightness = bl_get_brightness,
  130. .update_status = bl_update_status,
  131. };
  132. static struct backlight_device *compalbl_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_raw(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. u8 result;
  147. ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
  148. return sprintf(buf, "%i\n", result);
  149. }
  150. static ssize_t show_bluetooth(struct device *dev,
  151. struct device_attribute *attr, char *buf)
  152. {
  153. int ret, enabled;
  154. ret = get_wireless_state(NULL, &enabled);
  155. if (ret < 0)
  156. return ret;
  157. return sprintf(buf, "%i\n", enabled);
  158. }
  159. static ssize_t store_wlan_state(struct device *dev,
  160. struct device_attribute *attr, const char *buf, size_t count)
  161. {
  162. int state, ret;
  163. if (sscanf(buf, "%i", &state) != 1 || (state < 0 || state > 1))
  164. return -EINVAL;
  165. ret = set_wlan_state(state);
  166. if (ret < 0)
  167. return ret;
  168. return count;
  169. }
  170. static ssize_t store_bluetooth_state(struct device *dev,
  171. struct device_attribute *attr, const char *buf, size_t count)
  172. {
  173. int state, ret;
  174. if (sscanf(buf, "%i", &state) != 1 || (state < 0 || state > 1))
  175. return -EINVAL;
  176. ret = set_bluetooth_state(state);
  177. if (ret < 0)
  178. return ret;
  179. return count;
  180. }
  181. static DEVICE_ATTR(bluetooth, 0644, show_bluetooth, store_bluetooth_state);
  182. static DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan_state);
  183. static DEVICE_ATTR(raw, 0444, show_raw, NULL);
  184. static struct attribute *compal_attributes[] = {
  185. &dev_attr_bluetooth.attr,
  186. &dev_attr_wlan.attr,
  187. &dev_attr_raw.attr,
  188. NULL
  189. };
  190. static struct attribute_group compal_attribute_group = {
  191. .attrs = compal_attributes
  192. };
  193. static struct platform_driver compal_driver = {
  194. .driver = {
  195. .name = "compal-laptop",
  196. .owner = THIS_MODULE,
  197. }
  198. };
  199. static struct platform_device *compal_device;
  200. /* Initialization */
  201. static int dmi_check_cb(const struct dmi_system_id *id)
  202. {
  203. printk(KERN_INFO "compal-laptop: Identified laptop model '%s'.\n",
  204. id->ident);
  205. return 0;
  206. }
  207. static struct dmi_system_id __initdata compal_dmi_table[] = {
  208. {
  209. .ident = "FL90/IFL90",
  210. .matches = {
  211. DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
  212. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  213. },
  214. .callback = dmi_check_cb
  215. },
  216. {
  217. .ident = "FL90/IFL90",
  218. .matches = {
  219. DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
  220. DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
  221. },
  222. .callback = dmi_check_cb
  223. },
  224. {
  225. .ident = "FL91/IFL91",
  226. .matches = {
  227. DMI_MATCH(DMI_BOARD_NAME, "IFL91"),
  228. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  229. },
  230. .callback = dmi_check_cb
  231. },
  232. {
  233. .ident = "FL92/JFL92",
  234. .matches = {
  235. DMI_MATCH(DMI_BOARD_NAME, "JFL92"),
  236. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  237. },
  238. .callback = dmi_check_cb
  239. },
  240. {
  241. .ident = "FT00/IFT00",
  242. .matches = {
  243. DMI_MATCH(DMI_BOARD_NAME, "IFT00"),
  244. DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
  245. },
  246. .callback = dmi_check_cb
  247. },
  248. { }
  249. };
  250. static int __init compal_init(void)
  251. {
  252. int ret;
  253. if (acpi_disabled)
  254. return -ENODEV;
  255. if (!force && !dmi_check_system(compal_dmi_table))
  256. return -ENODEV;
  257. /* Register backlight stuff */
  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. ret = platform_driver_register(&compal_driver);
  264. if (ret)
  265. goto fail_backlight;
  266. /* Register platform stuff */
  267. compal_device = platform_device_alloc("compal-laptop", -1);
  268. if (!compal_device) {
  269. ret = -ENOMEM;
  270. goto fail_platform_driver;
  271. }
  272. ret = platform_device_add(compal_device);
  273. if (ret)
  274. goto fail_platform_device1;
  275. ret = sysfs_create_group(&compal_device->dev.kobj,
  276. &compal_attribute_group);
  277. if (ret)
  278. goto fail_platform_device2;
  279. printk(KERN_INFO "compal-laptop: driver "COMPAL_DRIVER_VERSION
  280. " successfully loaded.\n");
  281. return 0;
  282. fail_platform_device2:
  283. platform_device_del(compal_device);
  284. fail_platform_device1:
  285. platform_device_put(compal_device);
  286. fail_platform_driver:
  287. platform_driver_unregister(&compal_driver);
  288. fail_backlight:
  289. backlight_device_unregister(compalbl_device);
  290. return ret;
  291. }
  292. static void __exit compal_cleanup(void)
  293. {
  294. sysfs_remove_group(&compal_device->dev.kobj, &compal_attribute_group);
  295. platform_device_unregister(compal_device);
  296. platform_driver_unregister(&compal_driver);
  297. backlight_device_unregister(compalbl_device);
  298. printk(KERN_INFO "compal-laptop: driver unloaded.\n");
  299. }
  300. module_init(compal_init);
  301. module_exit(compal_cleanup);
  302. MODULE_AUTHOR("Cezary Jackiewicz");
  303. MODULE_DESCRIPTION("Compal Laptop Support");
  304. MODULE_VERSION(COMPAL_DRIVER_VERSION);
  305. MODULE_LICENSE("GPL");
  306. MODULE_ALIAS("dmi:*:rnIFL90:rvrIFT00:*");
  307. MODULE_ALIAS("dmi:*:rnIFL90:rvrREFERENCE:*");
  308. MODULE_ALIAS("dmi:*:rnIFL91:rvrIFT00:*");
  309. MODULE_ALIAS("dmi:*:rnJFL92:rvrIFT00:*");
  310. MODULE_ALIAS("dmi:*:rnIFT00:rvrIFT00:*");