asus-laptop.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * asus-laptop.c - Asus Laptop Support
  3. *
  4. *
  5. * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
  6. * Copyright (C) 2006 Corentin Chary
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *
  23. * The development page for this driver is located at
  24. * http://sourceforge.net/projects/acpi4asus/
  25. *
  26. * Credits:
  27. * Pontus Fuchs - Helper functions, cleanup
  28. * Johann Wiesner - Small compile fixes
  29. * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
  30. * Eric Burghard - LED display support for W1N
  31. * Josh Green - Light Sens support
  32. * Thomas Tuttle - His first patch for led support was very helpfull
  33. *
  34. */
  35. #include <linux/autoconf.h>
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/err.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/platform_device.h>
  43. #include <acpi/acpi_drivers.h>
  44. #include <acpi/acpi_bus.h>
  45. #include <asm/uaccess.h>
  46. #define ASUS_LAPTOP_VERSION "0.40"
  47. #define ASUS_HOTK_NAME "Asus Laptop Support"
  48. #define ASUS_HOTK_CLASS "hotkey"
  49. #define ASUS_HOTK_DEVICE_NAME "Hotkey"
  50. #define ASUS_HOTK_HID "ATK0100"
  51. #define ASUS_HOTK_FILE "asus-laptop"
  52. #define ASUS_HOTK_PREFIX "\\_SB.ATKD."
  53. #define ASUS_LOG ASUS_HOTK_FILE ": "
  54. #define ASUS_ERR KERN_ERR ASUS_LOG
  55. #define ASUS_WARNING KERN_WARNING ASUS_LOG
  56. #define ASUS_NOTICE KERN_NOTICE ASUS_LOG
  57. #define ASUS_INFO KERN_INFO ASUS_LOG
  58. #define ASUS_DEBUG KERN_DEBUG ASUS_LOG
  59. MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
  60. MODULE_DESCRIPTION(ASUS_HOTK_NAME);
  61. MODULE_LICENSE("GPL");
  62. #define ASUS_HANDLE(object, paths...) \
  63. static acpi_handle object##_handle = NULL; \
  64. static char *object##_paths[] = { paths }
  65. /*
  66. * This is the main structure, we can use it to store anything interesting
  67. * about the hotk device
  68. */
  69. struct asus_hotk {
  70. char *name; //laptop name
  71. struct acpi_device *device; //the device we are in
  72. acpi_handle handle; //the handle of the hotk device
  73. char status; //status of the hotk, for LEDs, ...
  74. u16 event_count[128]; //count for each event TODO make this better
  75. };
  76. /*
  77. * This header is made available to allow proper configuration given model,
  78. * revision number , ... this info cannot go in struct asus_hotk because it is
  79. * available before the hotk
  80. */
  81. static struct acpi_table_header *asus_info;
  82. /* The actual device the driver binds to */
  83. static struct asus_hotk *hotk;
  84. /*
  85. * The hotkey driver declaration
  86. */
  87. static int asus_hotk_add(struct acpi_device *device);
  88. static int asus_hotk_remove(struct acpi_device *device, int type);
  89. static struct acpi_driver asus_hotk_driver = {
  90. .name = ASUS_HOTK_NAME,
  91. .class = ASUS_HOTK_CLASS,
  92. .ids = ASUS_HOTK_HID,
  93. .ops = {
  94. .add = asus_hotk_add,
  95. .remove = asus_hotk_remove,
  96. },
  97. };
  98. /*
  99. * This function evaluates an ACPI method, given an int as parameter, the
  100. * method is searched within the scope of the handle, can be NULL. The output
  101. * of the method is written is output, which can also be NULL
  102. *
  103. * returns 1 if write is successful, 0 else.
  104. */
  105. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  106. struct acpi_buffer *output)
  107. {
  108. struct acpi_object_list params; //list of input parameters (an int here)
  109. union acpi_object in_obj; //the only param we use
  110. acpi_status status;
  111. params.count = 1;
  112. params.pointer = &in_obj;
  113. in_obj.type = ACPI_TYPE_INTEGER;
  114. in_obj.integer.value = val;
  115. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  116. return (status == AE_OK);
  117. }
  118. static int read_acpi_int(acpi_handle handle, const char *method, int *val,
  119. struct acpi_object_list *params)
  120. {
  121. struct acpi_buffer output;
  122. union acpi_object out_obj;
  123. acpi_status status;
  124. output.length = sizeof(out_obj);
  125. output.pointer = &out_obj;
  126. status = acpi_evaluate_object(handle, (char *)method, params, &output);
  127. *val = out_obj.integer.value;
  128. return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
  129. }
  130. /*
  131. * Platform device handlers
  132. */
  133. /*
  134. * We write our info in page, we begin at offset off and cannot write more
  135. * than count bytes. We set eof to 1 if we handle those 2 values. We return the
  136. * number of bytes written in page
  137. */
  138. static ssize_t show_infos(struct device *dev,
  139. struct device_attribute *attr, char *page)
  140. {
  141. int len = 0;
  142. int temp;
  143. char buf[16]; //enough for all info
  144. /*
  145. * We use the easy way, we don't care of off and count, so we don't set eof
  146. * to 1
  147. */
  148. len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
  149. len += sprintf(page + len, "Model reference : %s\n", hotk->name);
  150. /*
  151. * The SFUN method probably allows the original driver to get the list
  152. * of features supported by a given model. For now, 0x0100 or 0x0800
  153. * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
  154. * The significance of others is yet to be found.
  155. */
  156. if (read_acpi_int(hotk->handle, "SFUN", &temp, NULL))
  157. len +=
  158. sprintf(page + len, "SFUN value : 0x%04x\n", temp);
  159. /*
  160. * Another value for userspace: the ASYM method returns 0x02 for
  161. * battery low and 0x04 for battery critical, its readings tend to be
  162. * more accurate than those provided by _BST.
  163. * Note: since not all the laptops provide this method, errors are
  164. * silently ignored.
  165. */
  166. if (read_acpi_int(hotk->handle, "ASYM", &temp, NULL))
  167. len +=
  168. sprintf(page + len, "ASYM value : 0x%04x\n", temp);
  169. if (asus_info) {
  170. snprintf(buf, 16, "%d", asus_info->length);
  171. len += sprintf(page + len, "DSDT length : %s\n", buf);
  172. snprintf(buf, 16, "%d", asus_info->checksum);
  173. len += sprintf(page + len, "DSDT checksum : %s\n", buf);
  174. snprintf(buf, 16, "%d", asus_info->revision);
  175. len += sprintf(page + len, "DSDT revision : %s\n", buf);
  176. snprintf(buf, 7, "%s", asus_info->oem_id);
  177. len += sprintf(page + len, "OEM id : %s\n", buf);
  178. snprintf(buf, 9, "%s", asus_info->oem_table_id);
  179. len += sprintf(page + len, "OEM table id : %s\n", buf);
  180. snprintf(buf, 16, "%x", asus_info->oem_revision);
  181. len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
  182. snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
  183. len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
  184. snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
  185. len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
  186. }
  187. return len;
  188. }
  189. static int parse_arg(const char *buf, unsigned long count, int *val)
  190. {
  191. if (!count)
  192. return 0;
  193. if (count > 31)
  194. return -EINVAL;
  195. if (sscanf(buf, "%i", val) != 1)
  196. return -EINVAL;
  197. return count;
  198. }
  199. static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
  200. {
  201. /* TODO Find a better way to handle events count. */
  202. if (!hotk)
  203. return;
  204. acpi_bus_generate_event(hotk->device, event,
  205. hotk->event_count[event % 128]++);
  206. return;
  207. }
  208. #define ASUS_CREATE_DEVICE_ATTR(_name) \
  209. struct device_attribute dev_attr_##_name = { \
  210. .attr = { \
  211. .name = __stringify(_name), \
  212. .mode = 0, \
  213. .owner = THIS_MODULE }, \
  214. .show = NULL, \
  215. .store = NULL, \
  216. }
  217. #define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
  218. do { \
  219. dev_attr_##_name.attr.mode = _mode; \
  220. dev_attr_##_name.show = _show; \
  221. dev_attr_##_name.store = _store; \
  222. } while(0)
  223. static ASUS_CREATE_DEVICE_ATTR(infos);
  224. static struct attribute *asuspf_attributes[] = {
  225. &dev_attr_infos.attr,
  226. NULL
  227. };
  228. static struct attribute_group asuspf_attribute_group = {
  229. .attrs = asuspf_attributes
  230. };
  231. static struct platform_driver asuspf_driver = {
  232. .driver = {
  233. .name = ASUS_HOTK_FILE,
  234. .owner = THIS_MODULE,
  235. }
  236. };
  237. static struct platform_device *asuspf_device;
  238. static void asus_hotk_add_fs(void)
  239. {
  240. ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
  241. }
  242. static int asus_handle_init(char *name, acpi_handle *handle,
  243. char **paths, int num_paths)
  244. {
  245. int i;
  246. acpi_status status;
  247. for (i = 0; i < num_paths; i++) {
  248. status = acpi_get_handle(NULL, paths[i], handle);
  249. if (ACPI_SUCCESS(status))
  250. return 0;
  251. }
  252. *handle = NULL;
  253. return -ENODEV;
  254. }
  255. #define ASUS_HANDLE_INIT(object) \
  256. asus_handle_init(#object, &object##_handle, object##_paths, \
  257. ARRAY_SIZE(object##_paths))
  258. /*
  259. * This function is used to initialize the hotk with right values. In this
  260. * method, we can make all the detection we want, and modify the hotk struct
  261. */
  262. static int asus_hotk_get_info(void)
  263. {
  264. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  265. struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
  266. union acpi_object *model = NULL;
  267. int bsts_result;
  268. char *string = NULL;
  269. acpi_status status;
  270. /*
  271. * Get DSDT headers early enough to allow for differentiating between
  272. * models, but late enough to allow acpi_bus_register_driver() to fail
  273. * before doing anything ACPI-specific. Should we encounter a machine,
  274. * which needs special handling (i.e. its hotkey device has a different
  275. * HID), this bit will be moved. A global variable asus_info contains
  276. * the DSDT header.
  277. */
  278. status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
  279. if (ACPI_FAILURE(status))
  280. printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
  281. else
  282. asus_info = dsdt.pointer;
  283. /* We have to write 0 on init this far for all ASUS models */
  284. if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
  285. printk(ASUS_ERR "Hotkey initialization failed\n");
  286. return -ENODEV;
  287. }
  288. /* This needs to be called for some laptops to init properly */
  289. if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result, NULL))
  290. printk(ASUS_WARNING "Error calling BSTS\n");
  291. else if (bsts_result)
  292. printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
  293. bsts_result);
  294. /*
  295. * Try to match the object returned by INIT to the specific model.
  296. * Handle every possible object (or the lack of thereof) the DSDT
  297. * writers might throw at us. When in trouble, we pass NULL to
  298. * asus_model_match() and try something completely different.
  299. */
  300. if (buffer.pointer) {
  301. model = buffer.pointer;
  302. switch (model->type) {
  303. case ACPI_TYPE_STRING:
  304. string = model->string.pointer;
  305. break;
  306. case ACPI_TYPE_BUFFER:
  307. string = model->buffer.pointer;
  308. break;
  309. default:
  310. string = "";
  311. break;
  312. }
  313. }
  314. hotk->name = kstrdup(string, GFP_KERNEL);
  315. if (!hotk->name)
  316. return -ENOMEM;
  317. if(*string)
  318. printk(ASUS_NOTICE " %s model detected\n", string);
  319. kfree(model);
  320. return AE_OK;
  321. }
  322. static int asus_hotk_check(void)
  323. {
  324. int result = 0;
  325. result = acpi_bus_get_status(hotk->device);
  326. if (result)
  327. return result;
  328. if (hotk->device->status.present) {
  329. result = asus_hotk_get_info();
  330. } else {
  331. printk(ASUS_ERR "Hotkey device not present, aborting\n");
  332. return -EINVAL;
  333. }
  334. return result;
  335. }
  336. static int asus_hotk_found;
  337. static int asus_hotk_add(struct acpi_device *device)
  338. {
  339. acpi_status status = AE_OK;
  340. int result;
  341. if (!device)
  342. return -EINVAL;
  343. printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
  344. ASUS_LAPTOP_VERSION);
  345. hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
  346. if (!hotk)
  347. return -ENOMEM;
  348. memset(hotk, 0, sizeof(struct asus_hotk));
  349. hotk->handle = device->handle;
  350. strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
  351. strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
  352. acpi_driver_data(device) = hotk;
  353. hotk->device = device;
  354. result = asus_hotk_check();
  355. if (result)
  356. goto end;
  357. asus_hotk_add_fs();
  358. /*
  359. * We install the handler, it will receive the hotk in parameter, so, we
  360. * could add other data to the hotk struct
  361. */
  362. status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
  363. asus_hotk_notify, hotk);
  364. if (ACPI_FAILURE(status))
  365. printk(ASUS_ERR "Error installing notify handler\n");
  366. asus_hotk_found = 1;
  367. end:
  368. if (result) {
  369. kfree(hotk->name);
  370. kfree(hotk);
  371. }
  372. return result;
  373. }
  374. static int asus_hotk_remove(struct acpi_device *device, int type)
  375. {
  376. acpi_status status = 0;
  377. if (!device || !acpi_driver_data(device))
  378. return -EINVAL;
  379. status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
  380. asus_hotk_notify);
  381. if (ACPI_FAILURE(status))
  382. printk(ASUS_ERR "Error removing notify handler\n");
  383. kfree(hotk->name);
  384. kfree(hotk);
  385. return 0;
  386. }
  387. static void __exit asus_laptop_exit(void)
  388. {
  389. acpi_bus_unregister_driver(&asus_hotk_driver);
  390. sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
  391. platform_device_unregister(asuspf_device);
  392. platform_driver_unregister(&asuspf_driver);
  393. kfree(asus_info);
  394. }
  395. static int __init asus_laptop_init(void)
  396. {
  397. int result;
  398. if (acpi_disabled)
  399. return -ENODEV;
  400. if (!acpi_specific_hotkey_enabled) {
  401. printk(ASUS_ERR "Using generic hotkey driver\n");
  402. return -ENODEV;
  403. }
  404. result = acpi_bus_register_driver(&asus_hotk_driver);
  405. if (result < 0)
  406. return result;
  407. /*
  408. * This is a bit of a kludge. We only want this module loaded
  409. * for ASUS systems, but there's currently no way to probe the
  410. * ACPI namespace for ASUS HIDs. So we just return failure if
  411. * we didn't find one, which will cause the module to be
  412. * unloaded.
  413. */
  414. if (!asus_hotk_found) {
  415. acpi_bus_unregister_driver(&asus_hotk_driver);
  416. return -ENODEV;
  417. }
  418. /* Register platform stuff */
  419. result = platform_driver_register(&asuspf_driver);
  420. if (result)
  421. goto fail_platform_driver;
  422. asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
  423. if (!asuspf_device) {
  424. result = -ENOMEM;
  425. goto fail_platform_device1;
  426. }
  427. result = platform_device_add(asuspf_device);
  428. if (result)
  429. goto fail_platform_device2;
  430. result = sysfs_create_group(&asuspf_device->dev.kobj,
  431. &asuspf_attribute_group);
  432. if (result)
  433. goto fail_sysfs;
  434. return 0;
  435. fail_sysfs:
  436. platform_device_del(asuspf_device);
  437. fail_platform_device2:
  438. platform_device_put(asuspf_device);
  439. fail_platform_device1:
  440. platform_driver_unregister(&asuspf_driver);
  441. fail_platform_driver:
  442. return result;
  443. }
  444. module_init(asus_laptop_init);
  445. module_exit(asus_laptop_exit);