hotkey.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. return_VALUE(-ENODEV);
  306. } else {
  307. proc->proc_fops = &hotkey_polling_fops;
  308. proc->owner = THIS_MODULE;
  309. proc->data = device;
  310. proc->uid = 0;
  311. proc->gid = 0;
  312. device->poll_hotkey.proc = proc;
  313. }
  314. return_VALUE(0);
  315. }
  316. static int hotkey_add(union acpi_hotkey *device)
  317. {
  318. int status = 0;
  319. struct acpi_device *dev = NULL;
  320. ACPI_FUNCTION_TRACE("hotkey_add");
  321. if (device->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  322. acpi_bus_get_device(device->event_hotkey.bus_handle, &dev);
  323. status = acpi_install_notify_handler(dev->handle,
  324. ACPI_DEVICE_NOTIFY,
  325. acpi_hotkey_notify_handler,
  326. dev);
  327. } else /* Add polling hotkey */
  328. create_polling_proc(device);
  329. global_hotkey_list.count++;
  330. list_add_tail(&device->link.entries, global_hotkey_list.entries);
  331. return_VALUE(status);
  332. }
  333. static int hotkey_remove(union acpi_hotkey *device)
  334. {
  335. struct list_head *entries, *next;
  336. ACPI_FUNCTION_TRACE("hotkey_remove");
  337. list_for_each_safe(entries, next, global_hotkey_list.entries) {
  338. union acpi_hotkey *key =
  339. container_of(entries, union acpi_hotkey, entries);
  340. if (key->link.hotkey_standard_num ==
  341. device->link.hotkey_standard_num) {
  342. list_del(&key->link.entries);
  343. free_hotkey_device(key);
  344. global_hotkey_list.count--;
  345. break;
  346. }
  347. }
  348. kfree(device);
  349. return_VALUE(0);
  350. }
  351. static int hotkey_update(union acpi_hotkey *key)
  352. {
  353. struct list_head *entries;
  354. ACPI_FUNCTION_TRACE("hotkey_update");
  355. list_for_each(entries, global_hotkey_list.entries) {
  356. union acpi_hotkey *tmp =
  357. container_of(entries, union acpi_hotkey, entries);
  358. if (tmp->link.hotkey_standard_num ==
  359. key->link.hotkey_standard_num) {
  360. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  361. free_hotkey_buffer(tmp);
  362. tmp->event_hotkey.bus_handle =
  363. key->event_hotkey.bus_handle;
  364. tmp->event_hotkey.external_hotkey_num =
  365. key->event_hotkey.external_hotkey_num;
  366. tmp->event_hotkey.action_handle =
  367. key->event_hotkey.action_handle;
  368. tmp->event_hotkey.action_method =
  369. key->event_hotkey.action_method;
  370. kfree(key);
  371. } else {
  372. /*
  373. char proc_name[80];
  374. sprintf(proc_name, "%d", tmp->link.hotkey_standard_num);
  375. strcat(proc_name, tmp->poll_hotkey.poll_method);
  376. remove_proc_entry(proc_name,hotkey_proc_dir);
  377. */
  378. free_poll_hotkey_buffer(tmp);
  379. tmp->poll_hotkey.poll_handle =
  380. key->poll_hotkey.poll_handle;
  381. tmp->poll_hotkey.poll_method =
  382. key->poll_hotkey.poll_method;
  383. tmp->poll_hotkey.action_handle =
  384. key->poll_hotkey.action_handle;
  385. tmp->poll_hotkey.action_method =
  386. key->poll_hotkey.action_method;
  387. tmp->poll_hotkey.poll_result =
  388. key->poll_hotkey.poll_result;
  389. /*
  390. create_polling_proc(tmp);
  391. */
  392. kfree(key);
  393. }
  394. return_VALUE(0);
  395. break;
  396. }
  397. }
  398. return_VALUE(-ENODEV);
  399. }
  400. static void free_hotkey_device(union acpi_hotkey *key)
  401. {
  402. struct acpi_device *dev;
  403. ACPI_FUNCTION_TRACE("free_hotkey_device");
  404. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  405. acpi_bus_get_device(key->event_hotkey.bus_handle, &dev);
  406. if (dev->handle)
  407. acpi_remove_notify_handler(dev->handle,
  408. ACPI_DEVICE_NOTIFY,
  409. acpi_hotkey_notify_handler);
  410. free_hotkey_buffer(key);
  411. } else {
  412. char proc_name[80];
  413. sprintf(proc_name, "%d", key->link.hotkey_standard_num);
  414. /*
  415. strcat(proc_name, key->poll_hotkey.poll_method);
  416. */
  417. remove_proc_entry(proc_name, hotkey_proc_dir);
  418. free_poll_hotkey_buffer(key);
  419. }
  420. kfree(key);
  421. return_VOID;
  422. }
  423. static void free_hotkey_buffer(union acpi_hotkey *key)
  424. {
  425. kfree(key->event_hotkey.action_method);
  426. }
  427. static void free_poll_hotkey_buffer(union acpi_hotkey *key)
  428. {
  429. kfree(key->poll_hotkey.action_method);
  430. kfree(key->poll_hotkey.poll_method);
  431. kfree(key->poll_hotkey.poll_result);
  432. }
  433. static int
  434. init_hotkey_device(union acpi_hotkey *key, char *bus_str, char *action_str,
  435. char *method, int std_num, int external_num)
  436. {
  437. acpi_handle tmp_handle;
  438. acpi_status status = AE_OK;
  439. ACPI_FUNCTION_TRACE("init_hotkey_device");
  440. if (std_num < 0 || IS_POLL(std_num) || !key)
  441. goto do_fail;
  442. if (!bus_str || !action_str || !method)
  443. goto do_fail;
  444. key->link.hotkey_type = ACPI_HOTKEY_EVENT;
  445. key->link.hotkey_standard_num = std_num;
  446. key->event_hotkey.flag = 0;
  447. key->event_hotkey.action_method = method;
  448. status =
  449. acpi_get_handle(NULL, bus_str, &(key->event_hotkey.bus_handle));
  450. if (ACPI_FAILURE(status))
  451. goto do_fail;
  452. key->event_hotkey.external_hotkey_num = external_num;
  453. status =
  454. acpi_get_handle(NULL, action_str,
  455. &(key->event_hotkey.action_handle));
  456. if (ACPI_FAILURE(status))
  457. goto do_fail;
  458. status = acpi_get_handle(key->event_hotkey.action_handle,
  459. method, &tmp_handle);
  460. if (ACPI_FAILURE(status))
  461. goto do_fail;
  462. return_VALUE(AE_OK);
  463. do_fail:
  464. return_VALUE(-ENODEV);
  465. }
  466. static int
  467. init_poll_hotkey_device(union acpi_hotkey *key,
  468. char *poll_str,
  469. char *poll_method,
  470. char *action_str, char *action_method, int std_num)
  471. {
  472. acpi_status status = AE_OK;
  473. acpi_handle tmp_handle;
  474. ACPI_FUNCTION_TRACE("init_poll_hotkey_device");
  475. if (std_num < 0 || IS_EVENT(std_num) || !key)
  476. goto do_fail;
  477. if (!poll_str || !poll_method || !action_str || !action_method)
  478. goto do_fail;
  479. key->link.hotkey_type = ACPI_HOTKEY_POLLING;
  480. key->link.hotkey_standard_num = std_num;
  481. key->poll_hotkey.flag = 0;
  482. key->poll_hotkey.poll_method = poll_method;
  483. key->poll_hotkey.action_method = action_method;
  484. status =
  485. acpi_get_handle(NULL, poll_str, &(key->poll_hotkey.poll_handle));
  486. if (ACPI_FAILURE(status))
  487. goto do_fail;
  488. status = acpi_get_handle(key->poll_hotkey.poll_handle,
  489. poll_method, &tmp_handle);
  490. if (ACPI_FAILURE(status))
  491. goto do_fail;
  492. status =
  493. acpi_get_handle(NULL, action_str,
  494. &(key->poll_hotkey.action_handle));
  495. if (ACPI_FAILURE(status))
  496. goto do_fail;
  497. status = acpi_get_handle(key->poll_hotkey.action_handle,
  498. action_method, &tmp_handle);
  499. if (ACPI_FAILURE(status))
  500. goto do_fail;
  501. key->poll_hotkey.poll_result =
  502. (union acpi_object *)kmalloc(sizeof(union acpi_object), GFP_KERNEL);
  503. if (!key->poll_hotkey.poll_result)
  504. goto do_fail;
  505. return_VALUE(AE_OK);
  506. do_fail:
  507. return_VALUE(-ENODEV);
  508. }
  509. static int hotkey_open_config(struct inode *inode, struct file *file)
  510. {
  511. ACPI_FUNCTION_TRACE("hotkey_open_config");
  512. return_VALUE(single_open
  513. (file, hotkey_config_seq_show, PDE(inode)->data));
  514. }
  515. static int hotkey_poll_open_config(struct inode *inode, struct file *file)
  516. {
  517. ACPI_FUNCTION_TRACE("hotkey_poll_open_config");
  518. return_VALUE(single_open
  519. (file, hotkey_poll_config_seq_show, PDE(inode)->data));
  520. }
  521. static int hotkey_config_seq_show(struct seq_file *seq, void *offset)
  522. {
  523. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  524. struct list_head *entries;
  525. char bus_name[ACPI_PATHNAME_MAX] = { 0 };
  526. char action_name[ACPI_PATHNAME_MAX] = { 0 };
  527. struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
  528. struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };
  529. ACPI_FUNCTION_TRACE(("hotkey_config_seq_show"));
  530. list_for_each(entries, hotkey_list->entries) {
  531. union acpi_hotkey *key =
  532. container_of(entries, union acpi_hotkey, entries);
  533. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  534. acpi_get_name(key->event_hotkey.bus_handle,
  535. ACPI_NAME_TYPE_MAX, &bus);
  536. acpi_get_name(key->event_hotkey.action_handle,
  537. ACPI_NAME_TYPE_MAX, &act);
  538. seq_printf(seq, "%s:%s:%s:%d:%d\n", bus_name,
  539. action_name,
  540. key->event_hotkey.action_method,
  541. key->link.hotkey_standard_num,
  542. key->event_hotkey.external_hotkey_num);
  543. }
  544. }
  545. seq_puts(seq, "\n");
  546. return_VALUE(0);
  547. }
  548. static int hotkey_poll_config_seq_show(struct seq_file *seq, void *offset)
  549. {
  550. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  551. struct list_head *entries;
  552. char bus_name[ACPI_PATHNAME_MAX] = { 0 };
  553. char action_name[ACPI_PATHNAME_MAX] = { 0 };
  554. struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
  555. struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };
  556. ACPI_FUNCTION_TRACE(("hotkey_config_seq_show"));
  557. list_for_each(entries, hotkey_list->entries) {
  558. union acpi_hotkey *key =
  559. container_of(entries, union acpi_hotkey, entries);
  560. if (key->link.hotkey_type == ACPI_HOTKEY_POLLING) {
  561. acpi_get_name(key->poll_hotkey.poll_handle,
  562. ACPI_NAME_TYPE_MAX, &bus);
  563. acpi_get_name(key->poll_hotkey.action_handle,
  564. ACPI_NAME_TYPE_MAX, &act);
  565. seq_printf(seq, "%s:%s:%s:%s:%d\n", bus_name,
  566. key->poll_hotkey.poll_method,
  567. action_name,
  568. key->poll_hotkey.action_method,
  569. key->link.hotkey_standard_num);
  570. }
  571. }
  572. seq_puts(seq, "\n");
  573. return_VALUE(0);
  574. }
  575. static int
  576. get_parms(char *config_record,
  577. int *cmd,
  578. char **bus_handle,
  579. char **bus_method,
  580. char **action_handle,
  581. char **method, int *internal_event_num, int *external_event_num)
  582. {
  583. char *tmp, *tmp1, count;
  584. ACPI_FUNCTION_TRACE(("get_parms"));
  585. sscanf(config_record, "%d", cmd);
  586. if (*cmd == 1) {
  587. if (sscanf(config_record, "%d:%d", cmd, internal_event_num) !=
  588. 2)
  589. goto do_fail;
  590. else
  591. return (6);
  592. }
  593. tmp = strchr(config_record, ':');
  594. if (!tmp)
  595. goto do_fail;
  596. tmp++;
  597. tmp1 = strchr(tmp, ':');
  598. if (!tmp1)
  599. goto do_fail;
  600. count = tmp1 - tmp;
  601. *bus_handle = (char *)kmalloc(count + 1, GFP_KERNEL);
  602. if (!*bus_handle)
  603. goto do_fail;
  604. strncpy(*bus_handle, tmp, count);
  605. *(*bus_handle + count) = 0;
  606. tmp = tmp1;
  607. tmp++;
  608. tmp1 = strchr(tmp, ':');
  609. if (!tmp1)
  610. goto do_fail;
  611. count = tmp1 - tmp;
  612. *bus_method = (char *)kmalloc(count + 1, GFP_KERNEL);
  613. if (!*bus_method)
  614. goto do_fail;
  615. strncpy(*bus_method, tmp, count);
  616. *(*bus_method + count) = 0;
  617. tmp = tmp1;
  618. tmp++;
  619. tmp1 = strchr(tmp, ':');
  620. if (!tmp1)
  621. goto do_fail;
  622. count = tmp1 - tmp;
  623. *action_handle = (char *)kmalloc(count + 1, GFP_KERNEL);
  624. if (!*action_handle)
  625. goto do_fail;
  626. strncpy(*action_handle, tmp, count);
  627. *(*action_handle + count) = 0;
  628. tmp = tmp1;
  629. tmp++;
  630. tmp1 = strchr(tmp, ':');
  631. if (!tmp1)
  632. goto do_fail;
  633. count = tmp1 - tmp;
  634. *method = (char *)kmalloc(count + 1, GFP_KERNEL);
  635. if (!*method)
  636. goto do_fail;
  637. strncpy(*method, tmp, count);
  638. *(*method + count) = 0;
  639. if (sscanf(tmp1 + 1, "%d:%d", internal_event_num, external_event_num) <=
  640. 0)
  641. goto do_fail;
  642. return_VALUE(6);
  643. do_fail:
  644. return_VALUE(-1);
  645. }
  646. /* count is length for one input record */
  647. static ssize_t hotkey_write_config(struct file *file,
  648. const char __user * buffer,
  649. size_t count, loff_t * data)
  650. {
  651. char *config_record = NULL;
  652. char *bus_handle = NULL;
  653. char *bus_method = NULL;
  654. char *action_handle = NULL;
  655. char *method = NULL;
  656. int cmd, internal_event_num, external_event_num;
  657. int ret = 0;
  658. union acpi_hotkey *key = NULL;
  659. ACPI_FUNCTION_TRACE(("hotkey_write_config"));
  660. config_record = (char *)kmalloc(count + 1, GFP_KERNEL);
  661. if (!config_record)
  662. return_VALUE(-ENOMEM);
  663. if (copy_from_user(config_record, buffer, count)) {
  664. kfree(config_record);
  665. ACPI_ERROR((AE_INFO, "Invalid data"));
  666. return_VALUE(-EINVAL);
  667. }
  668. config_record[count] = 0;
  669. ret = get_parms(config_record,
  670. &cmd,
  671. &bus_handle,
  672. &bus_method,
  673. &action_handle,
  674. &method, &internal_event_num, &external_event_num);
  675. kfree(config_record);
  676. if (IS_OTHERS(internal_event_num))
  677. goto do_fail;
  678. if (ret != 6) {
  679. do_fail:
  680. kfree(bus_handle);
  681. kfree(bus_method);
  682. kfree(action_handle);
  683. kfree(method);
  684. ACPI_ERROR((AE_INFO, "Invalid data format ret=%d", ret));
  685. return_VALUE(-EINVAL);
  686. }
  687. key = kmalloc(sizeof(union acpi_hotkey), GFP_KERNEL);
  688. if (!key)
  689. goto do_fail;
  690. memset(key, 0, sizeof(union acpi_hotkey));
  691. if (cmd == 1) {
  692. union acpi_hotkey *tmp = NULL;
  693. tmp = get_hotkey_by_event(&global_hotkey_list,
  694. internal_event_num);
  695. if (!tmp)
  696. ACPI_ERROR((AE_INFO, "Invalid key"));
  697. else
  698. memcpy(key, tmp, sizeof(union acpi_hotkey));
  699. goto cont_cmd;
  700. }
  701. if (IS_EVENT(internal_event_num)) {
  702. kfree(bus_method);
  703. ret = init_hotkey_device(key, bus_handle, action_handle, method,
  704. internal_event_num,
  705. external_event_num);
  706. } else
  707. ret = init_poll_hotkey_device(key, bus_handle, bus_method,
  708. action_handle, method,
  709. internal_event_num);
  710. if (ret) {
  711. kfree(bus_handle);
  712. kfree(action_handle);
  713. if (IS_EVENT(internal_event_num))
  714. free_hotkey_buffer(key);
  715. else
  716. free_poll_hotkey_buffer(key);
  717. kfree(key);
  718. ACPI_ERROR((AE_INFO, "Invalid hotkey"));
  719. return_VALUE(-EINVAL);
  720. }
  721. cont_cmd:
  722. kfree(bus_handle);
  723. kfree(action_handle);
  724. switch (cmd) {
  725. case 0:
  726. if (get_hotkey_by_event
  727. (&global_hotkey_list, key->link.hotkey_standard_num))
  728. goto fail_out;
  729. else
  730. hotkey_add(key);
  731. break;
  732. case 1:
  733. hotkey_remove(key);
  734. break;
  735. case 2:
  736. if (hotkey_update(key))
  737. goto fail_out;
  738. break;
  739. default:
  740. goto fail_out;
  741. break;
  742. }
  743. return_VALUE(count);
  744. fail_out:
  745. if (IS_EVENT(internal_event_num))
  746. free_hotkey_buffer(key);
  747. else
  748. free_poll_hotkey_buffer(key);
  749. kfree(key);
  750. ACPI_ERROR((AE_INFO, "invalid key"));
  751. return_VALUE(-EINVAL);
  752. }
  753. /*
  754. * This function evaluates an ACPI method, given an int as parameter, the
  755. * method is searched within the scope of the handle, can be NULL. The output
  756. * of the method is written is output, which can also be NULL
  757. *
  758. * returns 1 if write is successful, 0 else.
  759. */
  760. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  761. struct acpi_buffer *output)
  762. {
  763. struct acpi_object_list params; /* list of input parameters (an int here) */
  764. union acpi_object in_obj; /* the only param we use */
  765. acpi_status status;
  766. ACPI_FUNCTION_TRACE("write_acpi_int");
  767. params.count = 1;
  768. params.pointer = &in_obj;
  769. in_obj.type = ACPI_TYPE_INTEGER;
  770. in_obj.integer.value = val;
  771. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  772. return_VALUE(status == AE_OK);
  773. }
  774. static int read_acpi_int(acpi_handle handle, const char *method,
  775. union acpi_object *val)
  776. {
  777. struct acpi_buffer output;
  778. union acpi_object out_obj;
  779. acpi_status status;
  780. ACPI_FUNCTION_TRACE("read_acpi_int");
  781. output.length = sizeof(out_obj);
  782. output.pointer = &out_obj;
  783. status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
  784. if (val) {
  785. val->integer.value = out_obj.integer.value;
  786. val->type = out_obj.type;
  787. } else
  788. ACPI_ERROR((AE_INFO, "null val pointer"));
  789. return_VALUE((status == AE_OK)
  790. && (out_obj.type == ACPI_TYPE_INTEGER));
  791. }
  792. static union acpi_hotkey *get_hotkey_by_event(struct
  793. acpi_hotkey_list
  794. *hotkey_list, int event)
  795. {
  796. struct list_head *entries;
  797. list_for_each(entries, hotkey_list->entries) {
  798. union acpi_hotkey *key =
  799. container_of(entries, union acpi_hotkey, entries);
  800. if (key->link.hotkey_standard_num == event) {
  801. return (key);
  802. }
  803. }
  804. return (NULL);
  805. }
  806. /*
  807. * user call AML method interface:
  808. * Call convention:
  809. * echo "event_num: arg type : value"
  810. * example: echo "1:1:30" > /proc/acpi/action
  811. * Just support 1 integer arg passing to AML method
  812. */
  813. static ssize_t hotkey_execute_aml_method(struct file *file,
  814. const char __user * buffer,
  815. size_t count, loff_t * data)
  816. {
  817. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  818. char *arg;
  819. int event, method_type, type, value;
  820. union acpi_hotkey *key;
  821. ACPI_FUNCTION_TRACE("hotkey_execte_aml_method");
  822. arg = (char *)kmalloc(count + 1, GFP_KERNEL);
  823. if (!arg)
  824. return_VALUE(-ENOMEM);
  825. arg[count] = 0;
  826. if (copy_from_user(arg, buffer, count)) {
  827. kfree(arg);
  828. ACPI_ERROR((AE_INFO, "Invalid argument 2"));
  829. return_VALUE(-EINVAL);
  830. }
  831. if (sscanf(arg, "%d:%d:%d:%d", &event, &method_type, &type, &value) !=
  832. 4) {
  833. kfree(arg);
  834. ACPI_ERROR((AE_INFO, "Invalid argument 3"));
  835. return_VALUE(-EINVAL);
  836. }
  837. kfree(arg);
  838. if (type == ACPI_TYPE_INTEGER) {
  839. key = get_hotkey_by_event(hotkey_list, event);
  840. if (!key)
  841. goto do_fail;
  842. if (IS_EVENT(event))
  843. write_acpi_int(key->event_hotkey.action_handle,
  844. key->event_hotkey.action_method, value,
  845. NULL);
  846. else if (IS_POLL(event)) {
  847. if (method_type == POLL_METHOD)
  848. read_acpi_int(key->poll_hotkey.poll_handle,
  849. key->poll_hotkey.poll_method,
  850. key->poll_hotkey.poll_result);
  851. else if (method_type == ACTION_METHOD)
  852. write_acpi_int(key->poll_hotkey.action_handle,
  853. key->poll_hotkey.action_method,
  854. value, NULL);
  855. else
  856. goto do_fail;
  857. }
  858. } else {
  859. ACPI_WARNING((AE_INFO, "Not supported"));
  860. return_VALUE(-EINVAL);
  861. }
  862. return_VALUE(count);
  863. do_fail:
  864. return_VALUE(-EINVAL);
  865. }
  866. static int __init hotkey_init(void)
  867. {
  868. int result;
  869. mode_t mode = S_IFREG | S_IRUGO | S_IWUGO;
  870. ACPI_FUNCTION_TRACE("hotkey_init");
  871. if (acpi_disabled)
  872. return -ENODEV;
  873. if (acpi_specific_hotkey_enabled) {
  874. printk("Using specific hotkey driver\n");
  875. return -ENODEV;
  876. }
  877. hotkey_proc_dir = proc_mkdir(HOTKEY_PROC, acpi_root_dir);
  878. if (!hotkey_proc_dir) {
  879. return (-ENODEV);
  880. }
  881. hotkey_proc_dir->owner = THIS_MODULE;
  882. hotkey_config =
  883. create_proc_entry(HOTKEY_EV_CONFIG, mode, hotkey_proc_dir);
  884. if (!hotkey_config) {
  885. goto do_fail1;
  886. } else {
  887. hotkey_config->proc_fops = &hotkey_config_fops;
  888. hotkey_config->data = &global_hotkey_list;
  889. hotkey_config->owner = THIS_MODULE;
  890. hotkey_config->uid = 0;
  891. hotkey_config->gid = 0;
  892. }
  893. hotkey_poll_config =
  894. create_proc_entry(HOTKEY_PL_CONFIG, mode, hotkey_proc_dir);
  895. if (!hotkey_poll_config) {
  896. goto do_fail2;
  897. } else {
  898. hotkey_poll_config->proc_fops = &hotkey_poll_config_fops;
  899. hotkey_poll_config->data = &global_hotkey_list;
  900. hotkey_poll_config->owner = THIS_MODULE;
  901. hotkey_poll_config->uid = 0;
  902. hotkey_poll_config->gid = 0;
  903. }
  904. hotkey_action = create_proc_entry(HOTKEY_ACTION, mode, hotkey_proc_dir);
  905. if (!hotkey_action) {
  906. goto do_fail3;
  907. } else {
  908. hotkey_action->proc_fops = &hotkey_action_fops;
  909. hotkey_action->owner = THIS_MODULE;
  910. hotkey_action->uid = 0;
  911. hotkey_action->gid = 0;
  912. }
  913. hotkey_info = create_proc_entry(HOTKEY_INFO, mode, hotkey_proc_dir);
  914. if (!hotkey_info) {
  915. goto do_fail4;
  916. } else {
  917. hotkey_info->proc_fops = &hotkey_info_fops;
  918. hotkey_info->owner = THIS_MODULE;
  919. hotkey_info->uid = 0;
  920. hotkey_info->gid = 0;
  921. }
  922. result = acpi_bus_register_driver(&hotkey_driver);
  923. if (result < 0)
  924. goto do_fail5;
  925. global_hotkey_list.count = 0;
  926. global_hotkey_list.entries = &hotkey_entries;
  927. INIT_LIST_HEAD(&hotkey_entries);
  928. return (0);
  929. do_fail5:
  930. remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
  931. do_fail4:
  932. remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
  933. do_fail3:
  934. remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
  935. do_fail2:
  936. remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
  937. do_fail1:
  938. remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
  939. return (-ENODEV);
  940. }
  941. static void __exit hotkey_exit(void)
  942. {
  943. struct list_head *entries, *next;
  944. ACPI_FUNCTION_TRACE("hotkey_exit");
  945. list_for_each_safe(entries, next, global_hotkey_list.entries) {
  946. union acpi_hotkey *key =
  947. container_of(entries, union acpi_hotkey, entries);
  948. acpi_os_wait_events_complete(NULL);
  949. list_del(&key->link.entries);
  950. global_hotkey_list.count--;
  951. free_hotkey_device(key);
  952. }
  953. acpi_bus_unregister_driver(&hotkey_driver);
  954. remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
  955. remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
  956. remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
  957. remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
  958. remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
  959. return;
  960. }
  961. module_init(hotkey_init);
  962. module_exit(hotkey_exit);