hotkey.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /*
  2. * hotkey.c - ACPI Hotkey Driver ($Revision: 0.2 $)
  3. *
  4. * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/sched.h>
  30. #include <linux/kmod.h>
  31. #include <linux/seq_file.h>
  32. #include <acpi/acpi_drivers.h>
  33. #include <acpi/acpi_bus.h>
  34. #include <asm/uaccess.h>
  35. #define HOTKEY_ACPI_VERSION "0.1"
  36. #define HOTKEY_PROC "hotkey"
  37. #define HOTKEY_EV_CONFIG "event_config"
  38. #define HOTKEY_PL_CONFIG "poll_config"
  39. #define HOTKEY_ACTION "action"
  40. #define HOTKEY_INFO "info"
  41. #define ACPI_HOTK_NAME "Generic Hotkey Driver"
  42. #define ACPI_HOTK_CLASS "Hotkey"
  43. #define ACPI_HOTK_DEVICE_NAME "Hotkey"
  44. #define ACPI_HOTK_HID "Unknown?"
  45. #define ACPI_HOTKEY_COMPONENT 0x20000000
  46. #define ACPI_HOTKEY_EVENT 0x1
  47. #define ACPI_HOTKEY_POLLING 0x2
  48. #define ACPI_UNDEFINED_EVENT 0xf
  49. #define RESULT_STR_LEN 80
  50. #define ACTION_METHOD 0
  51. #define POLL_METHOD 1
  52. #define IS_EVENT(e) ((e) <= 10000 && (e) >0)
  53. #define IS_POLL(e) ((e) > 10000)
  54. #define IS_OTHERS(e) ((e)<=0 || (e)>=20000)
  55. #define _COMPONENT ACPI_HOTKEY_COMPONENT
  56. ACPI_MODULE_NAME("acpi_hotkey")
  57. MODULE_AUTHOR("luming.yu@intel.com");
  58. MODULE_DESCRIPTION(ACPI_HOTK_NAME);
  59. MODULE_LICENSE("GPL");
  60. /* standardized internal hotkey number/event */
  61. enum {
  62. /* Video Extension event */
  63. HK_EVENT_CYCLE_OUTPUT_DEVICE = 0x80,
  64. HK_EVENT_OUTPUT_DEVICE_STATUS_CHANGE,
  65. HK_EVENT_CYCLE_DISPLAY_OUTPUT,
  66. HK_EVENT_NEXT_DISPLAY_OUTPUT,
  67. HK_EVENT_PREVIOUS_DISPLAY_OUTPUT,
  68. HK_EVENT_CYCLE_BRIGHTNESS,
  69. HK_EVENT_INCREASE_BRIGHTNESS,
  70. HK_EVENT_DECREASE_BRIGHTNESS,
  71. HK_EVENT_ZERO_BRIGHTNESS,
  72. HK_EVENT_DISPLAY_DEVICE_OFF,
  73. /* Snd Card event */
  74. HK_EVENT_VOLUME_MUTE,
  75. HK_EVENT_VOLUME_INCLREASE,
  76. HK_EVENT_VOLUME_DECREASE,
  77. /* running state control */
  78. HK_EVENT_ENTERRING_S3,
  79. HK_EVENT_ENTERRING_S4,
  80. HK_EVENT_ENTERRING_S5,
  81. };
  82. /* procdir we use */
  83. static struct proc_dir_entry *hotkey_proc_dir;
  84. static struct proc_dir_entry *hotkey_config;
  85. static struct proc_dir_entry *hotkey_poll_config;
  86. static struct proc_dir_entry *hotkey_action;
  87. static struct proc_dir_entry *hotkey_info;
  88. /* linkage for all type of hotkey */
  89. struct acpi_hotkey_link {
  90. struct list_head entries;
  91. int hotkey_type; /* event or polling based hotkey */
  92. int hotkey_standard_num; /* standardized hotkey(event) number */
  93. };
  94. /* event based hotkey */
  95. struct acpi_event_hotkey {
  96. struct acpi_hotkey_link hotkey_link;
  97. int flag;
  98. acpi_handle bus_handle; /* bus to install notify handler */
  99. int external_hotkey_num; /* external hotkey/event number */
  100. acpi_handle action_handle; /* acpi handle attached aml action method */
  101. char *action_method; /* action method */
  102. };
  103. /*
  104. * There are two ways to poll status
  105. * 1. directy call read_xxx method, without any arguments passed in
  106. * 2. call write_xxx method, with arguments passed in, you need
  107. * the result is saved in acpi_polling_hotkey.poll_result.
  108. * anthoer read command through polling interface.
  109. *
  110. */
  111. /* polling based hotkey */
  112. struct acpi_polling_hotkey {
  113. struct acpi_hotkey_link hotkey_link;
  114. int flag;
  115. acpi_handle poll_handle; /* acpi handle attached polling method */
  116. char *poll_method; /* poll method */
  117. acpi_handle action_handle; /* acpi handle attached action method */
  118. char *action_method; /* action method */
  119. union acpi_object *poll_result; /* polling_result */
  120. struct proc_dir_entry *proc;
  121. };
  122. /* hotkey object union */
  123. union acpi_hotkey {
  124. struct list_head entries;
  125. struct acpi_hotkey_link link;
  126. struct acpi_event_hotkey event_hotkey;
  127. struct acpi_polling_hotkey poll_hotkey;
  128. };
  129. /* hotkey object list */
  130. struct acpi_hotkey_list {
  131. struct list_head *entries;
  132. int count;
  133. };
  134. static int auto_hotkey_add(struct acpi_device *device);
  135. static int auto_hotkey_remove(struct acpi_device *device, int type);
  136. static struct acpi_driver hotkey_driver = {
  137. .name = ACPI_HOTK_NAME,
  138. .class = ACPI_HOTK_CLASS,
  139. .ids = ACPI_HOTK_HID,
  140. .ops = {
  141. .add = auto_hotkey_add,
  142. .remove = auto_hotkey_remove,
  143. },
  144. };
  145. static void free_hotkey_device(union acpi_hotkey *key);
  146. static void free_hotkey_buffer(union acpi_hotkey *key);
  147. static void free_poll_hotkey_buffer(union acpi_hotkey *key);
  148. static int hotkey_open_config(struct inode *inode, struct file *file);
  149. static int hotkey_poll_open_config(struct inode *inode, struct file *file);
  150. static ssize_t hotkey_write_config(struct file *file,
  151. const char __user * buffer,
  152. size_t count, loff_t * data);
  153. static int hotkey_info_open_fs(struct inode *inode, struct file *file);
  154. static int hotkey_action_open_fs(struct inode *inode, struct file *file);
  155. static ssize_t hotkey_execute_aml_method(struct file *file,
  156. const char __user * buffer,
  157. size_t count, loff_t * data);
  158. static int hotkey_config_seq_show(struct seq_file *seq, void *offset);
  159. static int hotkey_poll_config_seq_show(struct seq_file *seq, void *offset);
  160. static int hotkey_polling_open_fs(struct inode *inode, struct file *file);
  161. static union acpi_hotkey *get_hotkey_by_event(struct
  162. acpi_hotkey_list
  163. *hotkey_list, int event);
  164. /* event based config */
  165. static struct file_operations hotkey_config_fops = {
  166. .open = hotkey_open_config,
  167. .read = seq_read,
  168. .write = hotkey_write_config,
  169. .llseek = seq_lseek,
  170. .release = single_release,
  171. };
  172. /* polling based config */
  173. static struct file_operations hotkey_poll_config_fops = {
  174. .open = hotkey_poll_open_config,
  175. .read = seq_read,
  176. .write = hotkey_write_config,
  177. .llseek = seq_lseek,
  178. .release = single_release,
  179. };
  180. /* hotkey driver info */
  181. static struct file_operations hotkey_info_fops = {
  182. .open = hotkey_info_open_fs,
  183. .read = seq_read,
  184. .llseek = seq_lseek,
  185. .release = single_release,
  186. };
  187. /* action */
  188. static struct file_operations hotkey_action_fops = {
  189. .open = hotkey_action_open_fs,
  190. .read = seq_read,
  191. .write = hotkey_execute_aml_method,
  192. .llseek = seq_lseek,
  193. .release = single_release,
  194. };
  195. /* polling results */
  196. static struct file_operations hotkey_polling_fops = {
  197. .open = hotkey_polling_open_fs,
  198. .read = seq_read,
  199. .llseek = seq_lseek,
  200. .release = single_release,
  201. };
  202. struct acpi_hotkey_list global_hotkey_list; /* link all ev or pl hotkey */
  203. struct list_head hotkey_entries; /* head of the list of hotkey_list */
  204. static int hotkey_info_seq_show(struct seq_file *seq, void *offset)
  205. {
  206. ACPI_FUNCTION_TRACE("hotkey_info_seq_show");
  207. seq_printf(seq, "Hotkey generic driver ver: %s\n", HOTKEY_ACPI_VERSION);
  208. return_VALUE(0);
  209. }
  210. static int hotkey_info_open_fs(struct inode *inode, struct file *file)
  211. {
  212. return single_open(file, hotkey_info_seq_show, PDE(inode)->data);
  213. }
  214. static char *format_result(union acpi_object *object)
  215. {
  216. char *buf = NULL;
  217. buf = (char *)kmalloc(RESULT_STR_LEN, GFP_KERNEL);
  218. if (buf)
  219. memset(buf, 0, RESULT_STR_LEN);
  220. else
  221. goto do_fail;
  222. /* Now, just support integer type */
  223. if (object->type == ACPI_TYPE_INTEGER)
  224. sprintf(buf, "%d\n", (u32) object->integer.value);
  225. do_fail:
  226. return (buf);
  227. }
  228. static int hotkey_polling_seq_show(struct seq_file *seq, void *offset)
  229. {
  230. struct acpi_polling_hotkey *poll_hotkey =
  231. (struct acpi_polling_hotkey *)seq->private;
  232. char *buf;
  233. ACPI_FUNCTION_TRACE("hotkey_polling_seq_show");
  234. if (poll_hotkey->poll_result) {
  235. buf = format_result(poll_hotkey->poll_result);
  236. if (buf)
  237. seq_printf(seq, "%s", buf);
  238. kfree(buf);
  239. }
  240. return_VALUE(0);
  241. }
  242. static int hotkey_polling_open_fs(struct inode *inode, struct file *file)
  243. {
  244. return single_open(file, hotkey_polling_seq_show, PDE(inode)->data);
  245. }
  246. static int hotkey_action_open_fs(struct inode *inode, struct file *file)
  247. {
  248. return single_open(file, hotkey_info_seq_show, PDE(inode)->data);
  249. }
  250. /* Mapping external hotkey number to standardized hotkey event num */
  251. static int hotkey_get_internal_event(int event, struct acpi_hotkey_list *list)
  252. {
  253. struct list_head *entries;
  254. int val = -1;
  255. ACPI_FUNCTION_TRACE("hotkey_get_internal_event");
  256. list_for_each(entries, list->entries) {
  257. union acpi_hotkey *key =
  258. container_of(entries, union acpi_hotkey, entries);
  259. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT
  260. && key->event_hotkey.external_hotkey_num == event) {
  261. val = key->link.hotkey_standard_num;
  262. break;
  263. }
  264. }
  265. return_VALUE(val);
  266. }
  267. static void
  268. acpi_hotkey_notify_handler(acpi_handle handle, u32 event, void *data)
  269. {
  270. struct acpi_device *device = NULL;
  271. u32 internal_event;
  272. ACPI_FUNCTION_TRACE("acpi_hotkey_notify_handler");
  273. if (acpi_bus_get_device(handle, &device))
  274. return_VOID;
  275. internal_event = hotkey_get_internal_event(event, &global_hotkey_list);
  276. acpi_bus_generate_event(device, internal_event, 0);
  277. return_VOID;
  278. }
  279. /* Need to invent automatically hotkey add method */
  280. static int auto_hotkey_add(struct acpi_device *device)
  281. {
  282. /* Implement me */
  283. return 0;
  284. }
  285. /* Need to invent automatically hotkey remove method */
  286. static int auto_hotkey_remove(struct acpi_device *device, int type)
  287. {
  288. /* Implement me */
  289. return 0;
  290. }
  291. /* Create a proc file for each polling method */
  292. static int create_polling_proc(union acpi_hotkey *device)
  293. {
  294. struct proc_dir_entry *proc;
  295. char proc_name[80];
  296. mode_t mode;
  297. ACPI_FUNCTION_TRACE("create_polling_proc");
  298. mode = S_IFREG | S_IRUGO | S_IWUGO;
  299. sprintf(proc_name, "%d", device->link.hotkey_standard_num);
  300. /*
  301. strcat(proc_name, device->poll_hotkey.poll_method);
  302. */
  303. proc = create_proc_entry(proc_name, mode, hotkey_proc_dir);
  304. if (!proc) {
  305. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  306. "Hotkey: Unable to create %s entry\n",
  307. device->poll_hotkey.poll_method));
  308. return_VALUE(-ENODEV);
  309. } else {
  310. proc->proc_fops = &hotkey_polling_fops;
  311. proc->owner = THIS_MODULE;
  312. proc->data = device;
  313. proc->uid = 0;
  314. proc->gid = 0;
  315. device->poll_hotkey.proc = proc;
  316. }
  317. return_VALUE(0);
  318. }
  319. static int hotkey_add(union acpi_hotkey *device)
  320. {
  321. int status = 0;
  322. struct acpi_device *dev = NULL;
  323. ACPI_FUNCTION_TRACE("hotkey_add");
  324. if (device->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  325. acpi_bus_get_device(device->event_hotkey.bus_handle, &dev);
  326. status = acpi_install_notify_handler(dev->handle,
  327. ACPI_DEVICE_NOTIFY,
  328. acpi_hotkey_notify_handler,
  329. dev);
  330. } else /* Add polling hotkey */
  331. create_polling_proc(device);
  332. global_hotkey_list.count++;
  333. list_add_tail(&device->link.entries, global_hotkey_list.entries);
  334. return_VALUE(status);
  335. }
  336. static int hotkey_remove(union acpi_hotkey *device)
  337. {
  338. struct list_head *entries, *next;
  339. ACPI_FUNCTION_TRACE("hotkey_remove");
  340. list_for_each_safe(entries, next, global_hotkey_list.entries) {
  341. union acpi_hotkey *key =
  342. container_of(entries, union acpi_hotkey, entries);
  343. if (key->link.hotkey_standard_num ==
  344. device->link.hotkey_standard_num) {
  345. list_del(&key->link.entries);
  346. free_hotkey_device(key);
  347. global_hotkey_list.count--;
  348. break;
  349. }
  350. }
  351. kfree(device);
  352. return_VALUE(0);
  353. }
  354. static int hotkey_update(union acpi_hotkey *key)
  355. {
  356. struct list_head *entries;
  357. ACPI_FUNCTION_TRACE("hotkey_update");
  358. list_for_each(entries, global_hotkey_list.entries) {
  359. union acpi_hotkey *tmp =
  360. container_of(entries, union acpi_hotkey, entries);
  361. if (tmp->link.hotkey_standard_num ==
  362. key->link.hotkey_standard_num) {
  363. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  364. free_hotkey_buffer(tmp);
  365. tmp->event_hotkey.bus_handle =
  366. key->event_hotkey.bus_handle;
  367. tmp->event_hotkey.external_hotkey_num =
  368. key->event_hotkey.external_hotkey_num;
  369. tmp->event_hotkey.action_handle =
  370. key->event_hotkey.action_handle;
  371. tmp->event_hotkey.action_method =
  372. key->event_hotkey.action_method;
  373. kfree(key);
  374. } else {
  375. /*
  376. char proc_name[80];
  377. sprintf(proc_name, "%d", tmp->link.hotkey_standard_num);
  378. strcat(proc_name, tmp->poll_hotkey.poll_method);
  379. remove_proc_entry(proc_name,hotkey_proc_dir);
  380. */
  381. free_poll_hotkey_buffer(tmp);
  382. tmp->poll_hotkey.poll_handle =
  383. key->poll_hotkey.poll_handle;
  384. tmp->poll_hotkey.poll_method =
  385. key->poll_hotkey.poll_method;
  386. tmp->poll_hotkey.action_handle =
  387. key->poll_hotkey.action_handle;
  388. tmp->poll_hotkey.action_method =
  389. key->poll_hotkey.action_method;
  390. tmp->poll_hotkey.poll_result =
  391. key->poll_hotkey.poll_result;
  392. /*
  393. create_polling_proc(tmp);
  394. */
  395. kfree(key);
  396. }
  397. return_VALUE(0);
  398. break;
  399. }
  400. }
  401. return_VALUE(-ENODEV);
  402. }
  403. static void free_hotkey_device(union acpi_hotkey *key)
  404. {
  405. struct acpi_device *dev;
  406. ACPI_FUNCTION_TRACE("free_hotkey_device");
  407. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  408. acpi_bus_get_device(key->event_hotkey.bus_handle, &dev);
  409. if (dev->handle)
  410. acpi_remove_notify_handler(dev->handle,
  411. ACPI_DEVICE_NOTIFY,
  412. acpi_hotkey_notify_handler);
  413. free_hotkey_buffer(key);
  414. } else {
  415. char proc_name[80];
  416. sprintf(proc_name, "%d", key->link.hotkey_standard_num);
  417. /*
  418. strcat(proc_name, key->poll_hotkey.poll_method);
  419. */
  420. remove_proc_entry(proc_name, hotkey_proc_dir);
  421. free_poll_hotkey_buffer(key);
  422. }
  423. kfree(key);
  424. return_VOID;
  425. }
  426. static void free_hotkey_buffer(union acpi_hotkey *key)
  427. {
  428. kfree(key->event_hotkey.action_method);
  429. }
  430. static void free_poll_hotkey_buffer(union acpi_hotkey *key)
  431. {
  432. kfree(key->poll_hotkey.action_method);
  433. kfree(key->poll_hotkey.poll_method);
  434. kfree(key->poll_hotkey.poll_result);
  435. }
  436. static int
  437. init_hotkey_device(union acpi_hotkey *key, char *bus_str, char *action_str,
  438. char *method, int std_num, int external_num)
  439. {
  440. acpi_handle tmp_handle;
  441. acpi_status status = AE_OK;
  442. ACPI_FUNCTION_TRACE("init_hotkey_device");
  443. if (std_num < 0 || IS_POLL(std_num) || !key)
  444. goto do_fail;
  445. if (!bus_str || !action_str || !method)
  446. goto do_fail;
  447. key->link.hotkey_type = ACPI_HOTKEY_EVENT;
  448. key->link.hotkey_standard_num = std_num;
  449. key->event_hotkey.flag = 0;
  450. key->event_hotkey.action_method = method;
  451. status =
  452. acpi_get_handle(NULL, bus_str, &(key->event_hotkey.bus_handle));
  453. if (ACPI_FAILURE(status))
  454. goto do_fail;
  455. key->event_hotkey.external_hotkey_num = external_num;
  456. status =
  457. acpi_get_handle(NULL, action_str,
  458. &(key->event_hotkey.action_handle));
  459. if (ACPI_FAILURE(status))
  460. goto do_fail;
  461. status = acpi_get_handle(key->event_hotkey.action_handle,
  462. method, &tmp_handle);
  463. if (ACPI_FAILURE(status))
  464. goto do_fail;
  465. return_VALUE(AE_OK);
  466. do_fail:
  467. return_VALUE(-ENODEV);
  468. }
  469. static int
  470. init_poll_hotkey_device(union acpi_hotkey *key,
  471. char *poll_str,
  472. char *poll_method,
  473. char *action_str, char *action_method, int std_num)
  474. {
  475. acpi_status status = AE_OK;
  476. acpi_handle tmp_handle;
  477. ACPI_FUNCTION_TRACE("init_poll_hotkey_device");
  478. if (std_num < 0 || IS_EVENT(std_num) || !key)
  479. goto do_fail;
  480. if (!poll_str || !poll_method || !action_str || !action_method)
  481. goto do_fail;
  482. key->link.hotkey_type = ACPI_HOTKEY_POLLING;
  483. key->link.hotkey_standard_num = std_num;
  484. key->poll_hotkey.flag = 0;
  485. key->poll_hotkey.poll_method = poll_method;
  486. key->poll_hotkey.action_method = action_method;
  487. status =
  488. acpi_get_handle(NULL, poll_str, &(key->poll_hotkey.poll_handle));
  489. if (ACPI_FAILURE(status))
  490. goto do_fail;
  491. status = acpi_get_handle(key->poll_hotkey.poll_handle,
  492. poll_method, &tmp_handle);
  493. if (ACPI_FAILURE(status))
  494. goto do_fail;
  495. status =
  496. acpi_get_handle(NULL, action_str,
  497. &(key->poll_hotkey.action_handle));
  498. if (ACPI_FAILURE(status))
  499. goto do_fail;
  500. status = acpi_get_handle(key->poll_hotkey.action_handle,
  501. action_method, &tmp_handle);
  502. if (ACPI_FAILURE(status))
  503. goto do_fail;
  504. key->poll_hotkey.poll_result =
  505. (union acpi_object *)kmalloc(sizeof(union acpi_object), GFP_KERNEL);
  506. if (!key->poll_hotkey.poll_result)
  507. goto do_fail;
  508. return_VALUE(AE_OK);
  509. do_fail:
  510. return_VALUE(-ENODEV);
  511. }
  512. static int hotkey_open_config(struct inode *inode, struct file *file)
  513. {
  514. ACPI_FUNCTION_TRACE("hotkey_open_config");
  515. return_VALUE(single_open
  516. (file, hotkey_config_seq_show, PDE(inode)->data));
  517. }
  518. static int hotkey_poll_open_config(struct inode *inode, struct file *file)
  519. {
  520. ACPI_FUNCTION_TRACE("hotkey_poll_open_config");
  521. return_VALUE(single_open
  522. (file, hotkey_poll_config_seq_show, PDE(inode)->data));
  523. }
  524. static int hotkey_config_seq_show(struct seq_file *seq, void *offset)
  525. {
  526. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  527. struct list_head *entries;
  528. char bus_name[ACPI_PATHNAME_MAX] = { 0 };
  529. char action_name[ACPI_PATHNAME_MAX] = { 0 };
  530. struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
  531. struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };
  532. ACPI_FUNCTION_TRACE(("hotkey_config_seq_show"));
  533. list_for_each(entries, hotkey_list->entries) {
  534. union acpi_hotkey *key =
  535. container_of(entries, union acpi_hotkey, entries);
  536. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  537. acpi_get_name(key->event_hotkey.bus_handle,
  538. ACPI_NAME_TYPE_MAX, &bus);
  539. acpi_get_name(key->event_hotkey.action_handle,
  540. ACPI_NAME_TYPE_MAX, &act);
  541. seq_printf(seq, "%s:%s:%s:%d:%d\n", bus_name,
  542. action_name,
  543. key->event_hotkey.action_method,
  544. key->link.hotkey_standard_num,
  545. key->event_hotkey.external_hotkey_num);
  546. }
  547. }
  548. seq_puts(seq, "\n");
  549. return_VALUE(0);
  550. }
  551. static int hotkey_poll_config_seq_show(struct seq_file *seq, void *offset)
  552. {
  553. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  554. struct list_head *entries;
  555. char bus_name[ACPI_PATHNAME_MAX] = { 0 };
  556. char action_name[ACPI_PATHNAME_MAX] = { 0 };
  557. struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
  558. struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };
  559. ACPI_FUNCTION_TRACE(("hotkey_config_seq_show"));
  560. list_for_each(entries, hotkey_list->entries) {
  561. union acpi_hotkey *key =
  562. container_of(entries, union acpi_hotkey, entries);
  563. if (key->link.hotkey_type == ACPI_HOTKEY_POLLING) {
  564. acpi_get_name(key->poll_hotkey.poll_handle,
  565. ACPI_NAME_TYPE_MAX, &bus);
  566. acpi_get_name(key->poll_hotkey.action_handle,
  567. ACPI_NAME_TYPE_MAX, &act);
  568. seq_printf(seq, "%s:%s:%s:%s:%d\n", bus_name,
  569. key->poll_hotkey.poll_method,
  570. action_name,
  571. key->poll_hotkey.action_method,
  572. key->link.hotkey_standard_num);
  573. }
  574. }
  575. seq_puts(seq, "\n");
  576. return_VALUE(0);
  577. }
  578. static int
  579. get_parms(char *config_record,
  580. int *cmd,
  581. char **bus_handle,
  582. char **bus_method,
  583. char **action_handle,
  584. char **method, int *internal_event_num, int *external_event_num)
  585. {
  586. char *tmp, *tmp1, count;
  587. ACPI_FUNCTION_TRACE(("get_parms"));
  588. sscanf(config_record, "%d", cmd);
  589. if (*cmd == 1) {
  590. if (sscanf(config_record, "%d:%d", cmd, internal_event_num) !=
  591. 2)
  592. goto do_fail;
  593. else
  594. return (6);
  595. }
  596. tmp = strchr(config_record, ':');
  597. if (!tmp)
  598. goto do_fail;
  599. tmp++;
  600. tmp1 = strchr(tmp, ':');
  601. if (!tmp1)
  602. goto do_fail;
  603. count = tmp1 - tmp;
  604. *bus_handle = (char *)kmalloc(count + 1, GFP_KERNEL);
  605. if (!*bus_handle)
  606. goto do_fail;
  607. strncpy(*bus_handle, tmp, count);
  608. *(*bus_handle + count) = 0;
  609. tmp = tmp1;
  610. tmp++;
  611. tmp1 = strchr(tmp, ':');
  612. if (!tmp1)
  613. goto do_fail;
  614. count = tmp1 - tmp;
  615. *bus_method = (char *)kmalloc(count + 1, GFP_KERNEL);
  616. if (!*bus_method)
  617. goto do_fail;
  618. strncpy(*bus_method, tmp, count);
  619. *(*bus_method + count) = 0;
  620. tmp = tmp1;
  621. tmp++;
  622. tmp1 = strchr(tmp, ':');
  623. if (!tmp1)
  624. goto do_fail;
  625. count = tmp1 - tmp;
  626. *action_handle = (char *)kmalloc(count + 1, GFP_KERNEL);
  627. if (!*action_handle)
  628. goto do_fail;
  629. strncpy(*action_handle, tmp, count);
  630. *(*action_handle + count) = 0;
  631. tmp = tmp1;
  632. tmp++;
  633. tmp1 = strchr(tmp, ':');
  634. if (!tmp1)
  635. goto do_fail;
  636. count = tmp1 - tmp;
  637. *method = (char *)kmalloc(count + 1, GFP_KERNEL);
  638. if (!*method)
  639. goto do_fail;
  640. strncpy(*method, tmp, count);
  641. *(*method + count) = 0;
  642. if (sscanf(tmp1 + 1, "%d:%d", internal_event_num, external_event_num) <=
  643. 0)
  644. goto do_fail;
  645. return_VALUE(6);
  646. do_fail:
  647. return_VALUE(-1);
  648. }
  649. /* count is length for one input record */
  650. static ssize_t hotkey_write_config(struct file *file,
  651. const char __user * buffer,
  652. size_t count, loff_t * data)
  653. {
  654. char *config_record = NULL;
  655. char *bus_handle = NULL;
  656. char *bus_method = NULL;
  657. char *action_handle = NULL;
  658. char *method = NULL;
  659. int cmd, internal_event_num, external_event_num;
  660. int ret = 0;
  661. union acpi_hotkey *key = NULL;
  662. ACPI_FUNCTION_TRACE(("hotkey_write_config"));
  663. config_record = (char *)kmalloc(count + 1, GFP_KERNEL);
  664. if (!config_record)
  665. return_VALUE(-ENOMEM);
  666. if (copy_from_user(config_record, buffer, count)) {
  667. kfree(config_record);
  668. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data \n"));
  669. return_VALUE(-EINVAL);
  670. }
  671. config_record[count] = 0;
  672. ret = get_parms(config_record,
  673. &cmd,
  674. &bus_handle,
  675. &bus_method,
  676. &action_handle,
  677. &method, &internal_event_num, &external_event_num);
  678. kfree(config_record);
  679. if (IS_OTHERS(internal_event_num))
  680. goto do_fail;
  681. if (ret != 6) {
  682. do_fail:
  683. kfree(bus_handle);
  684. kfree(bus_method);
  685. kfree(action_handle);
  686. kfree(method);
  687. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  688. "Invalid data format ret=%d\n", ret));
  689. return_VALUE(-EINVAL);
  690. }
  691. key = kmalloc(sizeof(union acpi_hotkey), GFP_KERNEL);
  692. if (!key)
  693. goto do_fail;
  694. memset(key, 0, sizeof(union acpi_hotkey));
  695. if (cmd == 1) {
  696. union acpi_hotkey *tmp = NULL;
  697. tmp = get_hotkey_by_event(&global_hotkey_list,
  698. internal_event_num);
  699. if (!tmp)
  700. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid key"));
  701. else
  702. memcpy(key, tmp, sizeof(union acpi_hotkey));
  703. goto cont_cmd;
  704. }
  705. if (IS_EVENT(internal_event_num)) {
  706. kfree(bus_method);
  707. ret = init_hotkey_device(key, bus_handle, action_handle, method,
  708. internal_event_num,
  709. external_event_num);
  710. } else
  711. ret = init_poll_hotkey_device(key, bus_handle, bus_method,
  712. action_handle, method,
  713. internal_event_num);
  714. if (ret) {
  715. kfree(bus_handle);
  716. kfree(action_handle);
  717. if (IS_EVENT(internal_event_num))
  718. free_hotkey_buffer(key);
  719. else
  720. free_poll_hotkey_buffer(key);
  721. kfree(key);
  722. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid hotkey \n"));
  723. return_VALUE(-EINVAL);
  724. }
  725. cont_cmd:
  726. kfree(bus_handle);
  727. kfree(action_handle);
  728. switch (cmd) {
  729. case 0:
  730. if (get_hotkey_by_event
  731. (&global_hotkey_list, key->link.hotkey_standard_num))
  732. goto fail_out;
  733. else
  734. hotkey_add(key);
  735. break;
  736. case 1:
  737. hotkey_remove(key);
  738. break;
  739. case 2:
  740. if (hotkey_update(key))
  741. goto fail_out;
  742. break;
  743. default:
  744. goto fail_out;
  745. break;
  746. }
  747. return_VALUE(count);
  748. fail_out:
  749. if (IS_EVENT(internal_event_num))
  750. free_hotkey_buffer(key);
  751. else
  752. free_poll_hotkey_buffer(key);
  753. kfree(key);
  754. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "invalid key\n"));
  755. return_VALUE(-EINVAL);
  756. }
  757. /*
  758. * This function evaluates an ACPI method, given an int as parameter, the
  759. * method is searched within the scope of the handle, can be NULL. The output
  760. * of the method is written is output, which can also be NULL
  761. *
  762. * returns 1 if write is successful, 0 else.
  763. */
  764. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  765. struct acpi_buffer *output)
  766. {
  767. struct acpi_object_list params; /* list of input parameters (an int here) */
  768. union acpi_object in_obj; /* the only param we use */
  769. acpi_status status;
  770. ACPI_FUNCTION_TRACE("write_acpi_int");
  771. params.count = 1;
  772. params.pointer = &in_obj;
  773. in_obj.type = ACPI_TYPE_INTEGER;
  774. in_obj.integer.value = val;
  775. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  776. return_VALUE(status == AE_OK);
  777. }
  778. static int read_acpi_int(acpi_handle handle, const char *method,
  779. union acpi_object *val)
  780. {
  781. struct acpi_buffer output;
  782. union acpi_object out_obj;
  783. acpi_status status;
  784. ACPI_FUNCTION_TRACE("read_acpi_int");
  785. output.length = sizeof(out_obj);
  786. output.pointer = &out_obj;
  787. status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
  788. if (val) {
  789. val->integer.value = out_obj.integer.value;
  790. val->type = out_obj.type;
  791. } else
  792. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "null val pointer"));
  793. return_VALUE((status == AE_OK)
  794. && (out_obj.type == ACPI_TYPE_INTEGER));
  795. }
  796. static union acpi_hotkey *get_hotkey_by_event(struct
  797. acpi_hotkey_list
  798. *hotkey_list, int event)
  799. {
  800. struct list_head *entries;
  801. list_for_each(entries, hotkey_list->entries) {
  802. union acpi_hotkey *key =
  803. container_of(entries, union acpi_hotkey, entries);
  804. if (key->link.hotkey_standard_num == event) {
  805. return (key);
  806. }
  807. }
  808. return (NULL);
  809. }
  810. /*
  811. * user call AML method interface:
  812. * Call convention:
  813. * echo "event_num: arg type : value"
  814. * example: echo "1:1:30" > /proc/acpi/action
  815. * Just support 1 integer arg passing to AML method
  816. */
  817. static ssize_t hotkey_execute_aml_method(struct file *file,
  818. const char __user * buffer,
  819. size_t count, loff_t * data)
  820. {
  821. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  822. char *arg;
  823. int event, method_type, type, value;
  824. union acpi_hotkey *key;
  825. ACPI_FUNCTION_TRACE("hotkey_execte_aml_method");
  826. arg = (char *)kmalloc(count + 1, GFP_KERNEL);
  827. if (!arg)
  828. return_VALUE(-ENOMEM);
  829. arg[count] = 0;
  830. if (copy_from_user(arg, buffer, count)) {
  831. kfree(arg);
  832. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument 2"));
  833. return_VALUE(-EINVAL);
  834. }
  835. if (sscanf(arg, "%d:%d:%d:%d", &event, &method_type, &type, &value) !=
  836. 4) {
  837. kfree(arg);
  838. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument 3"));
  839. return_VALUE(-EINVAL);
  840. }
  841. kfree(arg);
  842. if (type == ACPI_TYPE_INTEGER) {
  843. key = get_hotkey_by_event(hotkey_list, event);
  844. if (!key)
  845. goto do_fail;
  846. if (IS_EVENT(event))
  847. write_acpi_int(key->event_hotkey.action_handle,
  848. key->event_hotkey.action_method, value,
  849. NULL);
  850. else if (IS_POLL(event)) {
  851. if (method_type == POLL_METHOD)
  852. read_acpi_int(key->poll_hotkey.poll_handle,
  853. key->poll_hotkey.poll_method,
  854. key->poll_hotkey.poll_result);
  855. else if (method_type == ACTION_METHOD)
  856. write_acpi_int(key->poll_hotkey.action_handle,
  857. key->poll_hotkey.action_method,
  858. value, NULL);
  859. else
  860. goto do_fail;
  861. }
  862. } else {
  863. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Not supported"));
  864. return_VALUE(-EINVAL);
  865. }
  866. return_VALUE(count);
  867. do_fail:
  868. return_VALUE(-EINVAL);
  869. }
  870. static int __init hotkey_init(void)
  871. {
  872. int result;
  873. mode_t mode = S_IFREG | S_IRUGO | S_IWUGO;
  874. ACPI_FUNCTION_TRACE("hotkey_init");
  875. if (acpi_disabled)
  876. return -ENODEV;
  877. if (acpi_specific_hotkey_enabled) {
  878. printk("Using specific hotkey driver\n");
  879. return -ENODEV;
  880. }
  881. hotkey_proc_dir = proc_mkdir(HOTKEY_PROC, acpi_root_dir);
  882. if (!hotkey_proc_dir) {
  883. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  884. "Hotkey: Unable to create %s entry\n",
  885. HOTKEY_PROC));
  886. return (-ENODEV);
  887. }
  888. hotkey_proc_dir->owner = THIS_MODULE;
  889. hotkey_config =
  890. create_proc_entry(HOTKEY_EV_CONFIG, mode, hotkey_proc_dir);
  891. if (!hotkey_config) {
  892. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  893. "Hotkey: Unable to create %s entry\n",
  894. HOTKEY_EV_CONFIG));
  895. goto do_fail1;
  896. } else {
  897. hotkey_config->proc_fops = &hotkey_config_fops;
  898. hotkey_config->data = &global_hotkey_list;
  899. hotkey_config->owner = THIS_MODULE;
  900. hotkey_config->uid = 0;
  901. hotkey_config->gid = 0;
  902. }
  903. hotkey_poll_config =
  904. create_proc_entry(HOTKEY_PL_CONFIG, mode, hotkey_proc_dir);
  905. if (!hotkey_poll_config) {
  906. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  907. "Hotkey: Unable to create %s entry\n",
  908. HOTKEY_EV_CONFIG));
  909. goto do_fail2;
  910. } else {
  911. hotkey_poll_config->proc_fops = &hotkey_poll_config_fops;
  912. hotkey_poll_config->data = &global_hotkey_list;
  913. hotkey_poll_config->owner = THIS_MODULE;
  914. hotkey_poll_config->uid = 0;
  915. hotkey_poll_config->gid = 0;
  916. }
  917. hotkey_action = create_proc_entry(HOTKEY_ACTION, mode, hotkey_proc_dir);
  918. if (!hotkey_action) {
  919. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  920. "Hotkey: Unable to create %s entry\n",
  921. HOTKEY_ACTION));
  922. goto do_fail3;
  923. } else {
  924. hotkey_action->proc_fops = &hotkey_action_fops;
  925. hotkey_action->owner = THIS_MODULE;
  926. hotkey_action->uid = 0;
  927. hotkey_action->gid = 0;
  928. }
  929. hotkey_info = create_proc_entry(HOTKEY_INFO, mode, hotkey_proc_dir);
  930. if (!hotkey_info) {
  931. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  932. "Hotkey: Unable to create %s entry\n",
  933. HOTKEY_INFO));
  934. goto do_fail4;
  935. } else {
  936. hotkey_info->proc_fops = &hotkey_info_fops;
  937. hotkey_info->owner = THIS_MODULE;
  938. hotkey_info->uid = 0;
  939. hotkey_info->gid = 0;
  940. }
  941. result = acpi_bus_register_driver(&hotkey_driver);
  942. if (result < 0)
  943. goto do_fail5;
  944. global_hotkey_list.count = 0;
  945. global_hotkey_list.entries = &hotkey_entries;
  946. INIT_LIST_HEAD(&hotkey_entries);
  947. return (0);
  948. do_fail5:
  949. remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
  950. do_fail4:
  951. remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
  952. do_fail3:
  953. remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
  954. do_fail2:
  955. remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
  956. do_fail1:
  957. remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
  958. return (-ENODEV);
  959. }
  960. static void __exit hotkey_exit(void)
  961. {
  962. struct list_head *entries, *next;
  963. ACPI_FUNCTION_TRACE("hotkey_exit");
  964. list_for_each_safe(entries, next, global_hotkey_list.entries) {
  965. union acpi_hotkey *key =
  966. container_of(entries, union acpi_hotkey, entries);
  967. acpi_os_wait_events_complete(NULL);
  968. list_del(&key->link.entries);
  969. global_hotkey_list.count--;
  970. free_hotkey_device(key);
  971. }
  972. acpi_bus_unregister_driver(&hotkey_driver);
  973. remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
  974. remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
  975. remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
  976. remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
  977. remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
  978. return;
  979. }
  980. module_init(hotkey_init);
  981. module_exit(hotkey_exit);