quickstart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * quickstart.c - ACPI Direct App Launch driver
  3. *
  4. *
  5. * Copyright (C) 2007-2010 Angelo Arrifano <miknix@gmail.com>
  6. *
  7. * Information gathered from disassebled dsdt and from here:
  8. * <http://www.microsoft.com/whdc/system/platform/firmware/DirAppLaunch.mspx>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #define QUICKSTART_VERSION "1.03"
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <acpi/acpi_drivers.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/input.h>
  33. MODULE_AUTHOR("Angelo Arrifano");
  34. MODULE_DESCRIPTION("ACPI Direct App Launch driver");
  35. MODULE_LICENSE("GPL");
  36. #define QUICKSTART_ACPI_DEVICE_NAME "quickstart"
  37. #define QUICKSTART_ACPI_CLASS "quickstart"
  38. #define QUICKSTART_ACPI_HID "PNP0C32"
  39. #define QUICKSTART_PF_DRIVER_NAME "quickstart"
  40. #define QUICKSTART_PF_DEVICE_NAME "quickstart"
  41. #define QUICKSTART_PF_DEVATTR_NAME "pressed_button"
  42. #define QUICKSTART_MAX_BTN_NAME_LEN 16
  43. /* There will be two events:
  44. * 0x02 - A hot button was pressed while device was off/sleeping.
  45. * 0x80 - A hot button was pressed while device was up. */
  46. #define QUICKSTART_EVENT_WAKE 0x02
  47. #define QUICKSTART_EVENT_RUNTIME 0x80
  48. struct quickstart_btn {
  49. char *name;
  50. unsigned int id;
  51. struct quickstart_btn *next;
  52. };
  53. static struct quickstart_driver_data {
  54. struct quickstart_btn *btn_lst;
  55. struct quickstart_btn *pressed;
  56. } quickstart_data;
  57. /* ACPI driver Structs */
  58. struct quickstart_acpi {
  59. struct acpi_device *device;
  60. struct quickstart_btn *btn;
  61. };
  62. static int quickstart_acpi_add(struct acpi_device *device);
  63. static int quickstart_acpi_remove(struct acpi_device *device, int type);
  64. static const struct acpi_device_id quickstart_device_ids[] = {
  65. {QUICKSTART_ACPI_HID, 0},
  66. {"", 0},
  67. };
  68. static struct acpi_driver quickstart_acpi_driver = {
  69. .name = "quickstart",
  70. .class = QUICKSTART_ACPI_CLASS,
  71. .ids = quickstart_device_ids,
  72. .ops = {
  73. .add = quickstart_acpi_add,
  74. .remove = quickstart_acpi_remove,
  75. },
  76. };
  77. /* Input device structs */
  78. struct input_dev *quickstart_input;
  79. /* Platform driver structs */
  80. static ssize_t buttons_show(struct device *dev,
  81. struct device_attribute *attr,
  82. char *buf);
  83. static ssize_t pressed_button_show(struct device *dev,
  84. struct device_attribute *attr,
  85. char *buf);
  86. static ssize_t pressed_button_store(struct device *dev,
  87. struct device_attribute *attr,
  88. const char *buf,
  89. size_t count);
  90. static DEVICE_ATTR(pressed_button, 0666, pressed_button_show,
  91. pressed_button_store);
  92. static DEVICE_ATTR(buttons, 0444, buttons_show, NULL);
  93. static struct platform_device *pf_device;
  94. static struct platform_driver pf_driver = {
  95. .driver = {
  96. .name = QUICKSTART_PF_DRIVER_NAME,
  97. .owner = THIS_MODULE,
  98. }
  99. };
  100. /*
  101. * Platform driver functions
  102. */
  103. static ssize_t buttons_show(struct device *dev,
  104. struct device_attribute *attr,
  105. char *buf)
  106. {
  107. int count = 0;
  108. struct quickstart_btn *ptr = quickstart_data.btn_lst;
  109. if (!ptr)
  110. return snprintf(buf, PAGE_SIZE, "none");
  111. while (ptr && (count < PAGE_SIZE)) {
  112. if (ptr->name) {
  113. count += snprintf(buf + count,
  114. PAGE_SIZE - count,
  115. "%d\t%s\n", ptr->id, ptr->name);
  116. }
  117. ptr = ptr->next;
  118. }
  119. return count;
  120. }
  121. static ssize_t pressed_button_show(struct device *dev,
  122. struct device_attribute *attr,
  123. char *buf)
  124. {
  125. return snprintf(buf, PAGE_SIZE, "%s\n",
  126. (quickstart_data.pressed ?
  127. quickstart_data.pressed->name : "none"));
  128. }
  129. static ssize_t pressed_button_store(struct device *dev,
  130. struct device_attribute *attr,
  131. const char *buf, size_t count)
  132. {
  133. if (count < 2)
  134. return -EINVAL;
  135. if (strncasecmp(buf, "none", 4) != 0)
  136. return -EINVAL;
  137. quickstart_data.pressed = NULL;
  138. return count;
  139. }
  140. /* Hotstart Helper functions */
  141. static int quickstart_btnlst_add(struct quickstart_btn **data)
  142. {
  143. struct quickstart_btn **ptr = &quickstart_data.btn_lst;
  144. while (*ptr)
  145. ptr = &((*ptr)->next);
  146. *ptr = kzalloc(sizeof(struct quickstart_btn), GFP_KERNEL);
  147. if (!*ptr) {
  148. *data = NULL;
  149. return -ENOMEM;
  150. }
  151. *data = *ptr;
  152. return 0;
  153. }
  154. static void quickstart_btnlst_del(struct quickstart_btn *data)
  155. {
  156. struct quickstart_btn **ptr = &quickstart_data.btn_lst;
  157. if (!data)
  158. return;
  159. while (*ptr) {
  160. if (*ptr == data) {
  161. *ptr = (*ptr)->next;
  162. kfree(data);
  163. return;
  164. }
  165. ptr = &((*ptr)->next);
  166. }
  167. return;
  168. }
  169. static void quickstart_btnlst_free(void)
  170. {
  171. struct quickstart_btn *ptr = quickstart_data.btn_lst;
  172. struct quickstart_btn *lptr = NULL;
  173. while (ptr) {
  174. lptr = ptr;
  175. ptr = ptr->next;
  176. kfree(lptr->name);
  177. kfree(lptr);
  178. }
  179. return;
  180. }
  181. /* ACPI Driver functions */
  182. static void quickstart_acpi_notify(acpi_handle handle, u32 event, void *data)
  183. {
  184. struct quickstart_acpi *quickstart = data;
  185. if (!quickstart)
  186. return;
  187. if (event == QUICKSTART_EVENT_WAKE)
  188. quickstart_data.pressed = quickstart->btn;
  189. else if (event == QUICKSTART_EVENT_RUNTIME) {
  190. input_report_key(quickstart_input, quickstart->btn->id, 1);
  191. input_sync(quickstart_input);
  192. input_report_key(quickstart_input, quickstart->btn->id, 0);
  193. input_sync(quickstart_input);
  194. }
  195. return;
  196. }
  197. static void quickstart_acpi_ghid(struct quickstart_acpi *quickstart)
  198. {
  199. acpi_status status;
  200. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  201. uint32_t usageid = 0;
  202. if (!quickstart)
  203. return;
  204. /* This returns a buffer telling the button usage ID,
  205. * and triggers pending notify events (The ones before booting). */
  206. status = acpi_evaluate_object(quickstart->device->handle,
  207. "GHID", NULL, &buffer);
  208. if (ACPI_FAILURE(status) || !buffer.pointer) {
  209. printk(KERN_ERR "quickstart: %s GHID method failed.\n",
  210. quickstart->btn->name);
  211. return;
  212. }
  213. if (buffer.length < 8)
  214. return;
  215. /* <<The GHID method can return a BYTE, WORD, or DWORD.
  216. * The value must be encoded in little-endian byte
  217. * order (least significant byte first).>> */
  218. usageid = *((uint32_t *)(buffer.pointer + (buffer.length - 8)));
  219. quickstart->btn->id = usageid;
  220. kfree(buffer.pointer);
  221. }
  222. static int quickstart_acpi_config(struct quickstart_acpi *quickstart, char *bid)
  223. {
  224. int len = strlen(bid);
  225. int ret;
  226. /* Add button to list */
  227. ret = quickstart_btnlst_add(&quickstart->btn);
  228. if (ret)
  229. return ret;
  230. quickstart->btn->name = kzalloc(len + 1, GFP_KERNEL);
  231. if (!quickstart->btn->name) {
  232. quickstart_btnlst_free();
  233. return -ENOMEM;
  234. }
  235. strcpy(quickstart->btn->name, bid);
  236. return 0;
  237. }
  238. static int quickstart_acpi_add(struct acpi_device *device)
  239. {
  240. int ret = 0;
  241. acpi_status status = AE_OK;
  242. struct quickstart_acpi *quickstart = NULL;
  243. if (!device)
  244. return -EINVAL;
  245. quickstart = kzalloc(sizeof(struct quickstart_acpi), GFP_KERNEL);
  246. if (!quickstart)
  247. return -ENOMEM;
  248. quickstart->device = device;
  249. strcpy(acpi_device_name(device), QUICKSTART_ACPI_DEVICE_NAME);
  250. strcpy(acpi_device_class(device), QUICKSTART_ACPI_CLASS);
  251. device->driver_data = quickstart;
  252. /* Add button to list and initialize some stuff */
  253. ret = quickstart_acpi_config(quickstart, acpi_device_bid(device));
  254. if (ret)
  255. goto fail_config;
  256. status = acpi_install_notify_handler(device->handle,
  257. ACPI_ALL_NOTIFY,
  258. quickstart_acpi_notify,
  259. quickstart);
  260. if (ACPI_FAILURE(status)) {
  261. printk(KERN_ERR "quickstart: Notify handler install error\n");
  262. ret = -ENODEV;
  263. goto fail_installnotify;
  264. }
  265. quickstart_acpi_ghid(quickstart);
  266. return 0;
  267. fail_installnotify:
  268. quickstart_btnlst_del(quickstart->btn);
  269. fail_config:
  270. kfree(quickstart);
  271. return ret;
  272. }
  273. static int quickstart_acpi_remove(struct acpi_device *device, int type)
  274. {
  275. acpi_status status = 0;
  276. struct quickstart_acpi *quickstart = NULL;
  277. if (!device || !acpi_driver_data(device))
  278. return -EINVAL;
  279. quickstart = acpi_driver_data(device);
  280. status = acpi_remove_notify_handler(device->handle,
  281. ACPI_ALL_NOTIFY,
  282. quickstart_acpi_notify);
  283. if (ACPI_FAILURE(status))
  284. printk(KERN_ERR "quickstart: Error removing notify handler\n");
  285. kfree(quickstart);
  286. return 0;
  287. }
  288. /* Module functions */
  289. static void quickstart_exit(void)
  290. {
  291. input_unregister_device(quickstart_input);
  292. device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
  293. device_remove_file(&pf_device->dev, &dev_attr_buttons);
  294. platform_device_unregister(pf_device);
  295. platform_driver_unregister(&pf_driver);
  296. acpi_bus_unregister_driver(&quickstart_acpi_driver);
  297. quickstart_btnlst_free();
  298. return;
  299. }
  300. static int __init quickstart_init_input(void)
  301. {
  302. struct quickstart_btn **ptr = &quickstart_data.btn_lst;
  303. int count;
  304. int ret;
  305. quickstart_input = input_allocate_device();
  306. if (!quickstart_input)
  307. return -ENOMEM;
  308. quickstart_input->name = "Quickstart ACPI Buttons";
  309. quickstart_input->id.bustype = BUS_HOST;
  310. while (*ptr) {
  311. count++;
  312. set_bit(EV_KEY, quickstart_input->evbit);
  313. set_bit((*ptr)->id, quickstart_input->keybit);
  314. ptr = &((*ptr)->next);
  315. }
  316. ret = input_register_device(quickstart_input);
  317. if (ret) {
  318. input_free_device(quickstart_input);
  319. return ret;
  320. }
  321. return 0;
  322. }
  323. static int __init quickstart_init(void)
  324. {
  325. int ret;
  326. /* ACPI Check */
  327. if (acpi_disabled)
  328. return -ENODEV;
  329. /* ACPI driver register */
  330. ret = acpi_bus_register_driver(&quickstart_acpi_driver);
  331. if (ret)
  332. return ret;
  333. /* If existing bus with no devices */
  334. if (!quickstart_data.btn_lst) {
  335. ret = -ENODEV;
  336. goto fail_pfdrv_reg;
  337. }
  338. /* Platform driver register */
  339. ret = platform_driver_register(&pf_driver);
  340. if (ret)
  341. goto fail_pfdrv_reg;
  342. /* Platform device register */
  343. pf_device = platform_device_alloc(QUICKSTART_PF_DEVICE_NAME, -1);
  344. if (!pf_device) {
  345. ret = -ENOMEM;
  346. goto fail_pfdev_alloc;
  347. }
  348. ret = platform_device_add(pf_device);
  349. if (ret)
  350. goto fail_pfdev_add;
  351. /* Create device sysfs file */
  352. ret = device_create_file(&pf_device->dev, &dev_attr_pressed_button);
  353. if (ret)
  354. goto fail_dev_file;
  355. ret = device_create_file(&pf_device->dev, &dev_attr_buttons);
  356. if (ret)
  357. goto fail_dev_file2;
  358. /* Input device */
  359. ret = quickstart_init_input();
  360. if (ret)
  361. goto fail_input;
  362. printk(KERN_INFO "quickstart: ACPI Direct App Launch ver %s\n",
  363. QUICKSTART_VERSION);
  364. return 0;
  365. fail_input:
  366. device_remove_file(&pf_device->dev, &dev_attr_buttons);
  367. fail_dev_file2:
  368. device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
  369. fail_dev_file:
  370. platform_device_del(pf_device);
  371. fail_pfdev_add:
  372. platform_device_put(pf_device);
  373. fail_pfdev_alloc:
  374. platform_driver_unregister(&pf_driver);
  375. fail_pfdrv_reg:
  376. acpi_bus_unregister_driver(&quickstart_acpi_driver);
  377. return ret;
  378. }
  379. module_init(quickstart_init);
  380. module_exit(quickstart_exit);