button.c 13 KB

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