hotkey.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. strncpy(*action_handle, tmp, count);
  628. *(*action_handle + count) = 0;
  629. tmp = tmp1;
  630. tmp++;
  631. tmp1 = strchr(tmp, ':');
  632. if (!tmp1)
  633. goto do_fail;
  634. count = tmp1 - tmp;
  635. *method = (char *)kmalloc(count + 1, GFP_KERNEL);
  636. if (!*method)
  637. goto do_fail;
  638. strncpy(*method, tmp, count);
  639. *(*method + count) = 0;
  640. if (sscanf(tmp1 + 1, "%d:%d", internal_event_num, external_event_num) <=
  641. 0)
  642. goto do_fail;
  643. return_VALUE(6);
  644. do_fail:
  645. return_VALUE(-1);
  646. }
  647. /* count is length for one input record */
  648. static ssize_t hotkey_write_config(struct file *file,
  649. const char __user * buffer,
  650. size_t count, loff_t * data)
  651. {
  652. char *config_record = NULL;
  653. char *bus_handle = NULL;
  654. char *bus_method = NULL;
  655. char *action_handle = NULL;
  656. char *method = NULL;
  657. int cmd, internal_event_num, external_event_num;
  658. int ret = 0;
  659. union acpi_hotkey *key = NULL;
  660. ACPI_FUNCTION_TRACE(("hotkey_write_config"));
  661. config_record = (char *)kmalloc(count + 1, GFP_KERNEL);
  662. if (!config_record)
  663. return_VALUE(-ENOMEM);
  664. if (copy_from_user(config_record, buffer, count)) {
  665. kfree(config_record);
  666. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data \n"));
  667. return_VALUE(-EINVAL);
  668. }
  669. config_record[count] = 0;
  670. ret = get_parms(config_record,
  671. &cmd,
  672. &bus_handle,
  673. &bus_method,
  674. &action_handle,
  675. &method, &internal_event_num, &external_event_num);
  676. kfree(config_record);
  677. if (IS_OTHERS(internal_event_num))
  678. goto do_fail;
  679. if (ret != 6) {
  680. do_fail:
  681. kfree(bus_handle);
  682. kfree(bus_method);
  683. kfree(action_handle);
  684. kfree(method);
  685. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  686. "Invalid data format ret=%d\n", ret));
  687. return_VALUE(-EINVAL);
  688. }
  689. key = kmalloc(sizeof(union acpi_hotkey), GFP_KERNEL);
  690. if (!key)
  691. goto do_fail;
  692. memset(key, 0, sizeof(union acpi_hotkey));
  693. if (cmd == 1) {
  694. union acpi_hotkey *tmp = NULL;
  695. tmp = get_hotkey_by_event(&global_hotkey_list,
  696. internal_event_num);
  697. if (!tmp)
  698. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid key"));
  699. else
  700. memcpy(key, tmp, sizeof(union acpi_hotkey));
  701. goto cont_cmd;
  702. }
  703. if (IS_EVENT(internal_event_num)) {
  704. kfree(bus_method);
  705. ret = init_hotkey_device(key, bus_handle, action_handle, method,
  706. internal_event_num,
  707. external_event_num);
  708. } else
  709. ret = init_poll_hotkey_device(key, bus_handle, bus_method,
  710. action_handle, method,
  711. internal_event_num);
  712. if (ret) {
  713. kfree(bus_handle);
  714. kfree(action_handle);
  715. if (IS_EVENT(internal_event_num))
  716. free_hotkey_buffer(key);
  717. else
  718. free_poll_hotkey_buffer(key);
  719. kfree(key);
  720. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid hotkey \n"));
  721. return_VALUE(-EINVAL);
  722. }
  723. cont_cmd:
  724. kfree(bus_handle);
  725. kfree(action_handle);
  726. switch (cmd) {
  727. case 0:
  728. if (get_hotkey_by_event
  729. (&global_hotkey_list, key->link.hotkey_standard_num))
  730. goto fail_out;
  731. else
  732. hotkey_add(key);
  733. break;
  734. case 1:
  735. hotkey_remove(key);
  736. break;
  737. case 2:
  738. if (hotkey_update(key))
  739. goto fail_out;
  740. break;
  741. default:
  742. goto fail_out;
  743. break;
  744. }
  745. return_VALUE(count);
  746. fail_out:
  747. if (IS_EVENT(internal_event_num))
  748. free_hotkey_buffer(key);
  749. else
  750. free_poll_hotkey_buffer(key);
  751. kfree(key);
  752. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "invalid key\n"));
  753. return_VALUE(-EINVAL);
  754. }
  755. /*
  756. * This function evaluates an ACPI method, given an int as parameter, the
  757. * method is searched within the scope of the handle, can be NULL. The output
  758. * of the method is written is output, which can also be NULL
  759. *
  760. * returns 1 if write is successful, 0 else.
  761. */
  762. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  763. struct acpi_buffer *output)
  764. {
  765. struct acpi_object_list params; /* list of input parameters (an int here) */
  766. union acpi_object in_obj; /* the only param we use */
  767. acpi_status status;
  768. ACPI_FUNCTION_TRACE("write_acpi_int");
  769. params.count = 1;
  770. params.pointer = &in_obj;
  771. in_obj.type = ACPI_TYPE_INTEGER;
  772. in_obj.integer.value = val;
  773. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  774. return_VALUE(status == AE_OK);
  775. }
  776. static int read_acpi_int(acpi_handle handle, const char *method,
  777. union acpi_object *val)
  778. {
  779. struct acpi_buffer output;
  780. union acpi_object out_obj;
  781. acpi_status status;
  782. ACPI_FUNCTION_TRACE("read_acpi_int");
  783. output.length = sizeof(out_obj);
  784. output.pointer = &out_obj;
  785. status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
  786. if (val) {
  787. val->integer.value = out_obj.integer.value;
  788. val->type = out_obj.type;
  789. } else
  790. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "null val pointer"));
  791. return_VALUE((status == AE_OK)
  792. && (out_obj.type == ACPI_TYPE_INTEGER));
  793. }
  794. static union acpi_hotkey *get_hotkey_by_event(struct
  795. acpi_hotkey_list
  796. *hotkey_list, int event)
  797. {
  798. struct list_head *entries;
  799. list_for_each(entries, hotkey_list->entries) {
  800. union acpi_hotkey *key =
  801. container_of(entries, union acpi_hotkey, entries);
  802. if (key->link.hotkey_standard_num == event) {
  803. return (key);
  804. }
  805. }
  806. return (NULL);
  807. }
  808. /*
  809. * user call AML method interface:
  810. * Call convention:
  811. * echo "event_num: arg type : value"
  812. * example: echo "1:1:30" > /proc/acpi/action
  813. * Just support 1 integer arg passing to AML method
  814. */
  815. static ssize_t hotkey_execute_aml_method(struct file *file,
  816. const char __user * buffer,
  817. size_t count, loff_t * data)
  818. {
  819. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  820. char *arg;
  821. int event, method_type, type, value;
  822. union acpi_hotkey *key;
  823. ACPI_FUNCTION_TRACE("hotkey_execte_aml_method");
  824. arg = (char *)kmalloc(count + 1, GFP_KERNEL);
  825. if (!arg)
  826. return_VALUE(-ENOMEM);
  827. arg[count] = 0;
  828. if (copy_from_user(arg, buffer, count)) {
  829. kfree(arg);
  830. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument 2"));
  831. return_VALUE(-EINVAL);
  832. }
  833. if (sscanf(arg, "%d:%d:%d:%d", &event, &method_type, &type, &value) !=
  834. 4) {
  835. kfree(arg);
  836. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument 3"));
  837. return_VALUE(-EINVAL);
  838. }
  839. kfree(arg);
  840. if (type == ACPI_TYPE_INTEGER) {
  841. key = get_hotkey_by_event(hotkey_list, event);
  842. if (!key)
  843. goto do_fail;
  844. if (IS_EVENT(event))
  845. write_acpi_int(key->event_hotkey.action_handle,
  846. key->event_hotkey.action_method, value,
  847. NULL);
  848. else if (IS_POLL(event)) {
  849. if (method_type == POLL_METHOD)
  850. read_acpi_int(key->poll_hotkey.poll_handle,
  851. key->poll_hotkey.poll_method,
  852. key->poll_hotkey.poll_result);
  853. else if (method_type == ACTION_METHOD)
  854. write_acpi_int(key->poll_hotkey.action_handle,
  855. key->poll_hotkey.action_method,
  856. value, NULL);
  857. else
  858. goto do_fail;
  859. }
  860. } else {
  861. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Not supported"));
  862. return_VALUE(-EINVAL);
  863. }
  864. return_VALUE(count);
  865. do_fail:
  866. return_VALUE(-EINVAL);
  867. }
  868. static int __init hotkey_init(void)
  869. {
  870. int result;
  871. mode_t mode = S_IFREG | S_IRUGO | S_IWUGO;
  872. ACPI_FUNCTION_TRACE("hotkey_init");
  873. if (acpi_disabled)
  874. return -ENODEV;
  875. if (acpi_specific_hotkey_enabled) {
  876. printk("Using specific hotkey driver\n");
  877. return -ENODEV;
  878. }
  879. hotkey_proc_dir = proc_mkdir(HOTKEY_PROC, acpi_root_dir);
  880. if (!hotkey_proc_dir) {
  881. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  882. "Hotkey: Unable to create %s entry\n",
  883. HOTKEY_PROC));
  884. return (-ENODEV);
  885. }
  886. hotkey_proc_dir->owner = THIS_MODULE;
  887. hotkey_config =
  888. create_proc_entry(HOTKEY_EV_CONFIG, mode, hotkey_proc_dir);
  889. if (!hotkey_config) {
  890. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  891. "Hotkey: Unable to create %s entry\n",
  892. HOTKEY_EV_CONFIG));
  893. goto do_fail1;
  894. } else {
  895. hotkey_config->proc_fops = &hotkey_config_fops;
  896. hotkey_config->data = &global_hotkey_list;
  897. hotkey_config->owner = THIS_MODULE;
  898. hotkey_config->uid = 0;
  899. hotkey_config->gid = 0;
  900. }
  901. hotkey_poll_config =
  902. create_proc_entry(HOTKEY_PL_CONFIG, mode, hotkey_proc_dir);
  903. if (!hotkey_poll_config) {
  904. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  905. "Hotkey: Unable to create %s entry\n",
  906. HOTKEY_EV_CONFIG));
  907. goto do_fail2;
  908. } else {
  909. hotkey_poll_config->proc_fops = &hotkey_poll_config_fops;
  910. hotkey_poll_config->data = &global_hotkey_list;
  911. hotkey_poll_config->owner = THIS_MODULE;
  912. hotkey_poll_config->uid = 0;
  913. hotkey_poll_config->gid = 0;
  914. }
  915. hotkey_action = create_proc_entry(HOTKEY_ACTION, mode, hotkey_proc_dir);
  916. if (!hotkey_action) {
  917. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  918. "Hotkey: Unable to create %s entry\n",
  919. HOTKEY_ACTION));
  920. goto do_fail3;
  921. } else {
  922. hotkey_action->proc_fops = &hotkey_action_fops;
  923. hotkey_action->owner = THIS_MODULE;
  924. hotkey_action->uid = 0;
  925. hotkey_action->gid = 0;
  926. }
  927. hotkey_info = create_proc_entry(HOTKEY_INFO, mode, hotkey_proc_dir);
  928. if (!hotkey_info) {
  929. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  930. "Hotkey: Unable to create %s entry\n",
  931. HOTKEY_INFO));
  932. goto do_fail4;
  933. } else {
  934. hotkey_info->proc_fops = &hotkey_info_fops;
  935. hotkey_info->owner = THIS_MODULE;
  936. hotkey_info->uid = 0;
  937. hotkey_info->gid = 0;
  938. }
  939. result = acpi_bus_register_driver(&hotkey_driver);
  940. if (result < 0)
  941. goto do_fail5;
  942. global_hotkey_list.count = 0;
  943. global_hotkey_list.entries = &hotkey_entries;
  944. INIT_LIST_HEAD(&hotkey_entries);
  945. return (0);
  946. do_fail5:
  947. remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
  948. do_fail4:
  949. remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
  950. do_fail3:
  951. remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
  952. do_fail2:
  953. remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
  954. do_fail1:
  955. remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
  956. return (-ENODEV);
  957. }
  958. static void __exit hotkey_exit(void)
  959. {
  960. struct list_head *entries, *next;
  961. ACPI_FUNCTION_TRACE("hotkey_exit");
  962. list_for_each_safe(entries, next, global_hotkey_list.entries) {
  963. union acpi_hotkey *key =
  964. container_of(entries, union acpi_hotkey, entries);
  965. acpi_os_wait_events_complete(NULL);
  966. list_del(&key->link.entries);
  967. global_hotkey_list.count--;
  968. free_hotkey_device(key);
  969. }
  970. acpi_bus_unregister_driver(&hotkey_driver);
  971. remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
  972. remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
  973. remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
  974. remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
  975. remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
  976. return;
  977. }
  978. module_init(hotkey_init);
  979. module_exit(hotkey_exit);