button.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 (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. seq_printf(seq, "type: %s\n",
  115. acpi_device_name(button->device));
  116. return 0;
  117. }
  118. static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
  119. {
  120. return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
  121. }
  122. static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
  123. {
  124. struct acpi_button *button = seq->private;
  125. acpi_status status;
  126. unsigned long long state;
  127. status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
  128. seq_printf(seq, "state: %s\n",
  129. ACPI_FAILURE(status) ? "unsupported" :
  130. (state ? "open" : "closed"));
  131. return 0;
  132. }
  133. static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
  134. {
  135. return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
  136. }
  137. static struct proc_dir_entry *acpi_power_dir;
  138. static struct proc_dir_entry *acpi_sleep_dir;
  139. static struct proc_dir_entry *acpi_lid_dir;
  140. static int acpi_button_add_fs(struct acpi_device *device)
  141. {
  142. struct proc_dir_entry *entry = NULL;
  143. struct acpi_button *button;
  144. button = acpi_driver_data(device);
  145. switch (button->type) {
  146. case ACPI_BUTTON_TYPE_POWER:
  147. case ACPI_BUTTON_TYPE_POWERF:
  148. if (!acpi_power_dir)
  149. acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
  150. acpi_button_dir);
  151. entry = acpi_power_dir;
  152. break;
  153. case ACPI_BUTTON_TYPE_SLEEP:
  154. case ACPI_BUTTON_TYPE_SLEEPF:
  155. if (!acpi_sleep_dir)
  156. acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
  157. acpi_button_dir);
  158. entry = acpi_sleep_dir;
  159. break;
  160. case ACPI_BUTTON_TYPE_LID:
  161. if (!acpi_lid_dir)
  162. acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
  163. acpi_button_dir);
  164. entry = acpi_lid_dir;
  165. break;
  166. }
  167. if (!entry)
  168. return -ENODEV;
  169. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
  170. if (!acpi_device_dir(device))
  171. return -ENODEV;
  172. /* 'info' [R] */
  173. entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
  174. S_IRUGO, acpi_device_dir(device),
  175. &acpi_button_info_fops,
  176. acpi_driver_data(device));
  177. if (!entry)
  178. return -ENODEV;
  179. /* show lid state [R] */
  180. if (button->type == ACPI_BUTTON_TYPE_LID) {
  181. entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
  182. S_IRUGO, acpi_device_dir(device),
  183. &acpi_button_state_fops,
  184. acpi_driver_data(device));
  185. if (!entry)
  186. return -ENODEV;
  187. }
  188. return 0;
  189. }
  190. static int acpi_button_remove_fs(struct acpi_device *device)
  191. {
  192. struct acpi_button *button = acpi_driver_data(device);
  193. if (acpi_device_dir(device)) {
  194. if (button->type == ACPI_BUTTON_TYPE_LID)
  195. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  196. acpi_device_dir(device));
  197. remove_proc_entry(ACPI_BUTTON_FILE_INFO,
  198. acpi_device_dir(device));
  199. remove_proc_entry(acpi_device_bid(device),
  200. acpi_device_dir(device)->parent);
  201. acpi_device_dir(device) = NULL;
  202. }
  203. return 0;
  204. }
  205. /* --------------------------------------------------------------------------
  206. Driver Interface
  207. -------------------------------------------------------------------------- */
  208. static int acpi_lid_send_state(struct acpi_button *button)
  209. {
  210. unsigned long long state;
  211. acpi_status status;
  212. status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
  213. &state);
  214. if (ACPI_FAILURE(status))
  215. return -ENODEV;
  216. /* input layer checks if event is redundant */
  217. input_report_switch(button->input, SW_LID, !state);
  218. input_sync(button->input);
  219. return 0;
  220. }
  221. static void acpi_button_notify(struct acpi_device *device, u32 event)
  222. {
  223. struct acpi_button *button = acpi_driver_data(device);
  224. struct input_dev *input;
  225. switch (event) {
  226. case ACPI_FIXED_HARDWARE_EVENT:
  227. event = ACPI_BUTTON_NOTIFY_STATUS;
  228. /* fall through */
  229. case ACPI_BUTTON_NOTIFY_STATUS:
  230. input = button->input;
  231. if (button->type == ACPI_BUTTON_TYPE_LID) {
  232. acpi_lid_send_state(button);
  233. } else {
  234. int keycode = test_bit(KEY_SLEEP, input->keybit) ?
  235. KEY_SLEEP : KEY_POWER;
  236. input_report_key(input, keycode, 1);
  237. input_sync(input);
  238. input_report_key(input, keycode, 0);
  239. input_sync(input);
  240. }
  241. acpi_bus_generate_proc_event(button->device, event,
  242. ++button->pushed);
  243. break;
  244. default:
  245. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  246. "Unsupported event [0x%x]\n", event));
  247. break;
  248. }
  249. }
  250. static int acpi_button_resume(struct acpi_device *device)
  251. {
  252. struct acpi_button *button;
  253. button = acpi_driver_data(device);
  254. if (button->type == ACPI_BUTTON_TYPE_LID)
  255. return acpi_lid_send_state(button);
  256. return 0;
  257. }
  258. static int acpi_button_add(struct acpi_device *device)
  259. {
  260. int error;
  261. struct acpi_button *button;
  262. struct input_dev *input;
  263. button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
  264. if (!button)
  265. return -ENOMEM;
  266. button->device = device;
  267. device->driver_data = button;
  268. button->input = input = input_allocate_device();
  269. if (!input) {
  270. error = -ENOMEM;
  271. goto err_free_button;
  272. }
  273. /*
  274. * Determine the button type (via hid), as fixed-feature buttons
  275. * need to be handled a bit differently than generic-space.
  276. */
  277. if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
  278. button->type = ACPI_BUTTON_TYPE_POWER;
  279. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
  280. sprintf(acpi_device_class(device), "%s/%s",
  281. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  282. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
  283. button->type = ACPI_BUTTON_TYPE_POWERF;
  284. strcpy(acpi_device_name(device),
  285. ACPI_BUTTON_DEVICE_NAME_POWERF);
  286. sprintf(acpi_device_class(device), "%s/%s",
  287. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  288. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
  289. button->type = ACPI_BUTTON_TYPE_SLEEP;
  290. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
  291. sprintf(acpi_device_class(device), "%s/%s",
  292. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  293. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
  294. button->type = ACPI_BUTTON_TYPE_SLEEPF;
  295. strcpy(acpi_device_name(device),
  296. ACPI_BUTTON_DEVICE_NAME_SLEEPF);
  297. sprintf(acpi_device_class(device), "%s/%s",
  298. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  299. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
  300. button->type = ACPI_BUTTON_TYPE_LID;
  301. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
  302. sprintf(acpi_device_class(device), "%s/%s",
  303. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  304. } else {
  305. printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
  306. acpi_device_hid(device));
  307. error = -ENODEV;
  308. goto err_free_input;
  309. }
  310. error = acpi_button_add_fs(device);
  311. if (error)
  312. goto err_free_input;
  313. snprintf(button->phys, sizeof(button->phys),
  314. "%s/button/input0", acpi_device_hid(device));
  315. input->name = acpi_device_name(device);
  316. input->phys = button->phys;
  317. input->id.bustype = BUS_HOST;
  318. input->id.product = button->type;
  319. input->dev.parent = &device->dev;
  320. switch (button->type) {
  321. case ACPI_BUTTON_TYPE_POWER:
  322. case ACPI_BUTTON_TYPE_POWERF:
  323. input->evbit[0] = BIT_MASK(EV_KEY);
  324. set_bit(KEY_POWER, input->keybit);
  325. break;
  326. case ACPI_BUTTON_TYPE_SLEEP:
  327. case ACPI_BUTTON_TYPE_SLEEPF:
  328. input->evbit[0] = BIT_MASK(EV_KEY);
  329. set_bit(KEY_SLEEP, input->keybit);
  330. break;
  331. case ACPI_BUTTON_TYPE_LID:
  332. input->evbit[0] = BIT_MASK(EV_SW);
  333. set_bit(SW_LID, input->swbit);
  334. break;
  335. }
  336. error = input_register_device(input);
  337. if (error)
  338. goto err_remove_fs;
  339. if (button->type == ACPI_BUTTON_TYPE_LID)
  340. acpi_lid_send_state(button);
  341. if (device->wakeup.flags.valid) {
  342. /* Button's GPE is run-wake GPE */
  343. acpi_set_gpe_type(device->wakeup.gpe_device,
  344. device->wakeup.gpe_number,
  345. ACPI_GPE_TYPE_WAKE_RUN);
  346. acpi_enable_gpe(device->wakeup.gpe_device,
  347. device->wakeup.gpe_number);
  348. device->wakeup.state.enabled = 1;
  349. }
  350. printk(KERN_INFO PREFIX "%s [%s]\n",
  351. acpi_device_name(device), acpi_device_bid(device));
  352. return 0;
  353. err_remove_fs:
  354. acpi_button_remove_fs(device);
  355. err_free_input:
  356. input_free_device(input);
  357. err_free_button:
  358. kfree(button);
  359. return error;
  360. }
  361. static int acpi_button_remove(struct acpi_device *device, int type)
  362. {
  363. struct acpi_button *button;
  364. button = acpi_driver_data(device);
  365. acpi_button_remove_fs(device);
  366. input_unregister_device(button->input);
  367. kfree(button);
  368. return 0;
  369. }
  370. static int __init acpi_button_init(void)
  371. {
  372. int result;
  373. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  374. if (!acpi_button_dir)
  375. return -ENODEV;
  376. result = acpi_bus_register_driver(&acpi_button_driver);
  377. if (result < 0) {
  378. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  379. return -ENODEV;
  380. }
  381. return 0;
  382. }
  383. static void __exit acpi_button_exit(void)
  384. {
  385. acpi_bus_unregister_driver(&acpi_button_driver);
  386. if (acpi_power_dir)
  387. remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
  388. if (acpi_sleep_dir)
  389. remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
  390. if (acpi_lid_dir)
  391. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  392. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  393. }
  394. module_init(acpi_button_init);
  395. module_exit(acpi_button_exit);