button.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * button.c - ACPI Button Driver
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/input.h>
  32. #include <acpi/acpi_bus.h>
  33. #include <acpi/acpi_drivers.h>
  34. #define ACPI_BUTTON_CLASS "button"
  35. #define ACPI_BUTTON_FILE_INFO "info"
  36. #define ACPI_BUTTON_FILE_STATE "state"
  37. #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
  38. #define ACPI_BUTTON_NOTIFY_STATUS 0x80
  39. #define ACPI_BUTTON_SUBCLASS_POWER "power"
  40. #define ACPI_BUTTON_HID_POWER "PNP0C0C"
  41. #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
  42. #define ACPI_BUTTON_TYPE_POWER 0x01
  43. #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
  44. #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
  45. #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
  46. #define ACPI_BUTTON_TYPE_SLEEP 0x03
  47. #define ACPI_BUTTON_SUBCLASS_LID "lid"
  48. #define ACPI_BUTTON_HID_LID "PNP0C0D"
  49. #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
  50. #define ACPI_BUTTON_TYPE_LID 0x05
  51. #define _COMPONENT ACPI_BUTTON_COMPONENT
  52. ACPI_MODULE_NAME("button");
  53. MODULE_AUTHOR("Paul Diefenbaugh");
  54. MODULE_DESCRIPTION("ACPI Button Driver");
  55. MODULE_LICENSE("GPL");
  56. static const struct acpi_device_id button_device_ids[] = {
  57. {ACPI_BUTTON_HID_LID, 0},
  58. {ACPI_BUTTON_HID_SLEEP, 0},
  59. {ACPI_BUTTON_HID_SLEEPF, 0},
  60. {ACPI_BUTTON_HID_POWER, 0},
  61. {ACPI_BUTTON_HID_POWERF, 0},
  62. {"", 0},
  63. };
  64. MODULE_DEVICE_TABLE(acpi, button_device_ids);
  65. static int acpi_button_add(struct acpi_device *device);
  66. static int acpi_button_remove(struct acpi_device *device, int type);
  67. static int acpi_button_resume(struct acpi_device *device);
  68. static void acpi_button_notify(struct acpi_device *device, u32 event);
  69. static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
  70. static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
  71. static struct acpi_driver acpi_button_driver = {
  72. .name = "button",
  73. .class = ACPI_BUTTON_CLASS,
  74. .ids = button_device_ids,
  75. .ops = {
  76. .add = acpi_button_add,
  77. .resume = acpi_button_resume,
  78. .remove = acpi_button_remove,
  79. .notify = acpi_button_notify,
  80. },
  81. };
  82. struct acpi_button {
  83. unsigned int type;
  84. struct input_dev *input;
  85. char phys[32]; /* for input device */
  86. unsigned long pushed;
  87. };
  88. static const struct file_operations acpi_button_info_fops = {
  89. .owner = THIS_MODULE,
  90. .open = acpi_button_info_open_fs,
  91. .read = seq_read,
  92. .llseek = seq_lseek,
  93. .release = single_release,
  94. };
  95. static const struct file_operations acpi_button_state_fops = {
  96. .owner = THIS_MODULE,
  97. .open = acpi_button_state_open_fs,
  98. .read = seq_read,
  99. .llseek = seq_lseek,
  100. .release = single_release,
  101. };
  102. /* --------------------------------------------------------------------------
  103. FS Interface (/proc)
  104. -------------------------------------------------------------------------- */
  105. static struct proc_dir_entry *acpi_button_dir;
  106. static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
  107. {
  108. struct acpi_device *device = seq->private;
  109. seq_printf(seq, "type: %s\n",
  110. acpi_device_name(device));
  111. return 0;
  112. }
  113. static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
  114. {
  115. return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
  116. }
  117. static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
  118. {
  119. struct acpi_device *device = seq->private;
  120. acpi_status status;
  121. unsigned long long state;
  122. status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
  123. seq_printf(seq, "state: %s\n",
  124. ACPI_FAILURE(status) ? "unsupported" :
  125. (state ? "open" : "closed"));
  126. return 0;
  127. }
  128. static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
  129. {
  130. return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
  131. }
  132. static struct proc_dir_entry *acpi_power_dir;
  133. static struct proc_dir_entry *acpi_sleep_dir;
  134. static struct proc_dir_entry *acpi_lid_dir;
  135. static int acpi_button_add_fs(struct acpi_device *device)
  136. {
  137. struct acpi_button *button = acpi_driver_data(device);
  138. struct proc_dir_entry *entry = NULL;
  139. switch (button->type) {
  140. case ACPI_BUTTON_TYPE_POWER:
  141. if (!acpi_power_dir)
  142. acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
  143. acpi_button_dir);
  144. entry = acpi_power_dir;
  145. break;
  146. case ACPI_BUTTON_TYPE_SLEEP:
  147. if (!acpi_sleep_dir)
  148. acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
  149. acpi_button_dir);
  150. entry = acpi_sleep_dir;
  151. break;
  152. case ACPI_BUTTON_TYPE_LID:
  153. if (!acpi_lid_dir)
  154. acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
  155. acpi_button_dir);
  156. entry = acpi_lid_dir;
  157. break;
  158. }
  159. if (!entry)
  160. return -ENODEV;
  161. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
  162. if (!acpi_device_dir(device))
  163. return -ENODEV;
  164. /* 'info' [R] */
  165. entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
  166. S_IRUGO, acpi_device_dir(device),
  167. &acpi_button_info_fops, device);
  168. if (!entry)
  169. return -ENODEV;
  170. /* show lid state [R] */
  171. if (button->type == ACPI_BUTTON_TYPE_LID) {
  172. entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
  173. S_IRUGO, acpi_device_dir(device),
  174. &acpi_button_state_fops, device);
  175. if (!entry)
  176. return -ENODEV;
  177. }
  178. return 0;
  179. }
  180. static int acpi_button_remove_fs(struct acpi_device *device)
  181. {
  182. struct acpi_button *button = acpi_driver_data(device);
  183. if (acpi_device_dir(device)) {
  184. if (button->type == ACPI_BUTTON_TYPE_LID)
  185. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  186. acpi_device_dir(device));
  187. remove_proc_entry(ACPI_BUTTON_FILE_INFO,
  188. acpi_device_dir(device));
  189. remove_proc_entry(acpi_device_bid(device),
  190. acpi_device_dir(device)->parent);
  191. acpi_device_dir(device) = NULL;
  192. }
  193. return 0;
  194. }
  195. /* --------------------------------------------------------------------------
  196. Driver Interface
  197. -------------------------------------------------------------------------- */
  198. static int acpi_lid_send_state(struct acpi_device *device)
  199. {
  200. struct acpi_button *button = acpi_driver_data(device);
  201. unsigned long long state;
  202. acpi_status status;
  203. status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
  204. if (ACPI_FAILURE(status))
  205. return -ENODEV;
  206. /* input layer checks if event is redundant */
  207. input_report_switch(button->input, SW_LID, !state);
  208. input_sync(button->input);
  209. return 0;
  210. }
  211. static void acpi_button_notify(struct acpi_device *device, u32 event)
  212. {
  213. struct acpi_button *button = acpi_driver_data(device);
  214. struct input_dev *input;
  215. switch (event) {
  216. case ACPI_FIXED_HARDWARE_EVENT:
  217. event = ACPI_BUTTON_NOTIFY_STATUS;
  218. /* fall through */
  219. case ACPI_BUTTON_NOTIFY_STATUS:
  220. input = button->input;
  221. if (button->type == ACPI_BUTTON_TYPE_LID) {
  222. acpi_lid_send_state(device);
  223. } else {
  224. int keycode = test_bit(KEY_SLEEP, input->keybit) ?
  225. KEY_SLEEP : KEY_POWER;
  226. input_report_key(input, keycode, 1);
  227. input_sync(input);
  228. input_report_key(input, keycode, 0);
  229. input_sync(input);
  230. }
  231. acpi_bus_generate_proc_event(device, event, ++button->pushed);
  232. break;
  233. default:
  234. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  235. "Unsupported event [0x%x]\n", event));
  236. break;
  237. }
  238. }
  239. static int acpi_button_resume(struct acpi_device *device)
  240. {
  241. struct acpi_button *button = acpi_driver_data(device);
  242. if (button->type == ACPI_BUTTON_TYPE_LID)
  243. return acpi_lid_send_state(device);
  244. return 0;
  245. }
  246. static int acpi_button_add(struct acpi_device *device)
  247. {
  248. struct acpi_button *button;
  249. struct input_dev *input;
  250. char *hid, *name, *class;
  251. int error;
  252. button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
  253. if (!button)
  254. return -ENOMEM;
  255. device->driver_data = button;
  256. button->input = input = input_allocate_device();
  257. if (!input) {
  258. error = -ENOMEM;
  259. goto err_free_button;
  260. }
  261. hid = acpi_device_hid(device);
  262. name = acpi_device_name(device);
  263. class = acpi_device_class(device);
  264. if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
  265. !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
  266. button->type = ACPI_BUTTON_TYPE_POWER;
  267. strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
  268. sprintf(class, "%s/%s",
  269. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  270. } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
  271. !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
  272. button->type = ACPI_BUTTON_TYPE_SLEEP;
  273. strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
  274. sprintf(class, "%s/%s",
  275. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  276. } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
  277. button->type = ACPI_BUTTON_TYPE_LID;
  278. strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
  279. sprintf(class, "%s/%s",
  280. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  281. } else {
  282. printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
  283. error = -ENODEV;
  284. goto err_free_input;
  285. }
  286. error = acpi_button_add_fs(device);
  287. if (error)
  288. goto err_free_input;
  289. snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
  290. input->name = name;
  291. input->phys = button->phys;
  292. input->id.bustype = BUS_HOST;
  293. input->id.product = button->type;
  294. input->dev.parent = &device->dev;
  295. switch (button->type) {
  296. case ACPI_BUTTON_TYPE_POWER:
  297. input->evbit[0] = BIT_MASK(EV_KEY);
  298. set_bit(KEY_POWER, input->keybit);
  299. break;
  300. case ACPI_BUTTON_TYPE_SLEEP:
  301. input->evbit[0] = BIT_MASK(EV_KEY);
  302. set_bit(KEY_SLEEP, input->keybit);
  303. break;
  304. case ACPI_BUTTON_TYPE_LID:
  305. input->evbit[0] = BIT_MASK(EV_SW);
  306. set_bit(SW_LID, input->swbit);
  307. break;
  308. }
  309. error = input_register_device(input);
  310. if (error)
  311. goto err_remove_fs;
  312. if (button->type == ACPI_BUTTON_TYPE_LID)
  313. acpi_lid_send_state(device);
  314. if (device->wakeup.flags.valid) {
  315. /* Button's GPE is run-wake GPE */
  316. acpi_set_gpe_type(device->wakeup.gpe_device,
  317. device->wakeup.gpe_number,
  318. ACPI_GPE_TYPE_WAKE_RUN);
  319. acpi_enable_gpe(device->wakeup.gpe_device,
  320. device->wakeup.gpe_number);
  321. device->wakeup.state.enabled = 1;
  322. }
  323. printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
  324. return 0;
  325. err_remove_fs:
  326. acpi_button_remove_fs(device);
  327. err_free_input:
  328. input_free_device(input);
  329. err_free_button:
  330. kfree(button);
  331. return error;
  332. }
  333. static int acpi_button_remove(struct acpi_device *device, int type)
  334. {
  335. struct acpi_button *button = acpi_driver_data(device);
  336. acpi_button_remove_fs(device);
  337. input_unregister_device(button->input);
  338. kfree(button);
  339. return 0;
  340. }
  341. static int __init acpi_button_init(void)
  342. {
  343. int result;
  344. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  345. if (!acpi_button_dir)
  346. return -ENODEV;
  347. result = acpi_bus_register_driver(&acpi_button_driver);
  348. if (result < 0) {
  349. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  350. return -ENODEV;
  351. }
  352. return 0;
  353. }
  354. static void __exit acpi_button_exit(void)
  355. {
  356. acpi_bus_unregister_driver(&acpi_button_driver);
  357. if (acpi_power_dir)
  358. remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
  359. if (acpi_sleep_dir)
  360. remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
  361. if (acpi_lid_dir)
  362. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  363. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  364. }
  365. module_init(acpi_button_init);
  366. module_exit(acpi_button_exit);