button.c 12 KB

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