button.c 13 KB

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