hotkey.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. enum conf_entry_enum {
  83. bus_handle = 0,
  84. bus_method = 1,
  85. action_handle = 2,
  86. method = 3,
  87. LAST_CONF_ENTRY
  88. };
  89. /* procdir we use */
  90. static struct proc_dir_entry *hotkey_proc_dir;
  91. static struct proc_dir_entry *hotkey_config;
  92. static struct proc_dir_entry *hotkey_poll_config;
  93. static struct proc_dir_entry *hotkey_action;
  94. static struct proc_dir_entry *hotkey_info;
  95. /* linkage for all type of hotkey */
  96. struct acpi_hotkey_link {
  97. struct list_head entries;
  98. int hotkey_type; /* event or polling based hotkey */
  99. int hotkey_standard_num; /* standardized hotkey(event) number */
  100. };
  101. /* event based hotkey */
  102. struct acpi_event_hotkey {
  103. struct acpi_hotkey_link hotkey_link;
  104. int flag;
  105. acpi_handle bus_handle; /* bus to install notify handler */
  106. int external_hotkey_num; /* external hotkey/event number */
  107. acpi_handle action_handle; /* acpi handle attached aml action method */
  108. char *action_method; /* action method */
  109. };
  110. /*
  111. * There are two ways to poll status
  112. * 1. directy call read_xxx method, without any arguments passed in
  113. * 2. call write_xxx method, with arguments passed in, you need
  114. * the result is saved in acpi_polling_hotkey.poll_result.
  115. * anthoer read command through polling interface.
  116. *
  117. */
  118. /* polling based hotkey */
  119. struct acpi_polling_hotkey {
  120. struct acpi_hotkey_link hotkey_link;
  121. int flag;
  122. acpi_handle poll_handle; /* acpi handle attached polling method */
  123. char *poll_method; /* poll method */
  124. acpi_handle action_handle; /* acpi handle attached action method */
  125. char *action_method; /* action method */
  126. union acpi_object *poll_result; /* polling_result */
  127. struct proc_dir_entry *proc;
  128. };
  129. /* hotkey object union */
  130. union acpi_hotkey {
  131. struct list_head entries;
  132. struct acpi_hotkey_link link;
  133. struct acpi_event_hotkey event_hotkey;
  134. struct acpi_polling_hotkey poll_hotkey;
  135. };
  136. /* hotkey object list */
  137. struct acpi_hotkey_list {
  138. struct list_head *entries;
  139. int count;
  140. };
  141. static int auto_hotkey_add(struct acpi_device *device);
  142. static int auto_hotkey_remove(struct acpi_device *device, int type);
  143. static struct acpi_driver hotkey_driver = {
  144. .name = ACPI_HOTK_NAME,
  145. .class = ACPI_HOTK_CLASS,
  146. .ids = ACPI_HOTK_HID,
  147. .ops = {
  148. .add = auto_hotkey_add,
  149. .remove = auto_hotkey_remove,
  150. },
  151. };
  152. static void free_hotkey_device(union acpi_hotkey *key);
  153. static void free_hotkey_buffer(union acpi_hotkey *key);
  154. static void free_poll_hotkey_buffer(union acpi_hotkey *key);
  155. static int hotkey_open_config(struct inode *inode, struct file *file);
  156. static int hotkey_poll_open_config(struct inode *inode, struct file *file);
  157. static ssize_t hotkey_write_config(struct file *file,
  158. const char __user * buffer,
  159. size_t count, loff_t * data);
  160. static int hotkey_info_open_fs(struct inode *inode, struct file *file);
  161. static int hotkey_action_open_fs(struct inode *inode, struct file *file);
  162. static ssize_t hotkey_execute_aml_method(struct file *file,
  163. const char __user * buffer,
  164. size_t count, loff_t * data);
  165. static int hotkey_config_seq_show(struct seq_file *seq, void *offset);
  166. static int hotkey_poll_config_seq_show(struct seq_file *seq, void *offset);
  167. static int hotkey_polling_open_fs(struct inode *inode, struct file *file);
  168. static union acpi_hotkey *get_hotkey_by_event(struct
  169. acpi_hotkey_list
  170. *hotkey_list, int event);
  171. /* event based config */
  172. static const struct file_operations hotkey_config_fops = {
  173. .open = hotkey_open_config,
  174. .read = seq_read,
  175. .write = hotkey_write_config,
  176. .llseek = seq_lseek,
  177. .release = single_release,
  178. };
  179. /* polling based config */
  180. static const struct file_operations hotkey_poll_config_fops = {
  181. .open = hotkey_poll_open_config,
  182. .read = seq_read,
  183. .write = hotkey_write_config,
  184. .llseek = seq_lseek,
  185. .release = single_release,
  186. };
  187. /* hotkey driver info */
  188. static const struct file_operations hotkey_info_fops = {
  189. .open = hotkey_info_open_fs,
  190. .read = seq_read,
  191. .llseek = seq_lseek,
  192. .release = single_release,
  193. };
  194. /* action */
  195. static const struct file_operations hotkey_action_fops = {
  196. .open = hotkey_action_open_fs,
  197. .read = seq_read,
  198. .write = hotkey_execute_aml_method,
  199. .llseek = seq_lseek,
  200. .release = single_release,
  201. };
  202. /* polling results */
  203. static const struct file_operations hotkey_polling_fops = {
  204. .open = hotkey_polling_open_fs,
  205. .read = seq_read,
  206. .llseek = seq_lseek,
  207. .release = single_release,
  208. };
  209. struct acpi_hotkey_list global_hotkey_list; /* link all ev or pl hotkey */
  210. struct list_head hotkey_entries; /* head of the list of hotkey_list */
  211. static int hotkey_info_seq_show(struct seq_file *seq, void *offset)
  212. {
  213. seq_printf(seq, "Hotkey generic driver ver: %s\n", HOTKEY_ACPI_VERSION);
  214. return 0;
  215. }
  216. static int hotkey_info_open_fs(struct inode *inode, struct file *file)
  217. {
  218. return single_open(file, hotkey_info_seq_show, PDE(inode)->data);
  219. }
  220. static char *format_result(union acpi_object *object)
  221. {
  222. char *buf;
  223. buf = kzalloc(RESULT_STR_LEN, GFP_KERNEL);
  224. if (!buf)
  225. return NULL;
  226. /* Now, just support integer type */
  227. if (object->type == ACPI_TYPE_INTEGER)
  228. sprintf(buf, "%d\n", (u32) object->integer.value);
  229. return buf;
  230. }
  231. static int hotkey_polling_seq_show(struct seq_file *seq, void *offset)
  232. {
  233. struct acpi_polling_hotkey *poll_hotkey = seq->private;
  234. char *buf;
  235. if (poll_hotkey->poll_result) {
  236. buf = format_result(poll_hotkey->poll_result);
  237. if (buf)
  238. seq_printf(seq, "%s", buf);
  239. kfree(buf);
  240. }
  241. return 0;
  242. }
  243. static int hotkey_polling_open_fs(struct inode *inode, struct file *file)
  244. {
  245. return single_open(file, hotkey_polling_seq_show, PDE(inode)->data);
  246. }
  247. static int hotkey_action_open_fs(struct inode *inode, struct file *file)
  248. {
  249. return single_open(file, hotkey_info_seq_show, PDE(inode)->data);
  250. }
  251. /* Mapping external hotkey number to standardized hotkey event num */
  252. static int hotkey_get_internal_event(int event, struct acpi_hotkey_list *list)
  253. {
  254. struct list_head *entries;
  255. int val = -1;
  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 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. if (acpi_bus_get_device(handle, &device))
  273. return;
  274. internal_event = hotkey_get_internal_event(event, &global_hotkey_list);
  275. acpi_bus_generate_event(device, internal_event, 0);
  276. return;
  277. }
  278. /* Need to invent automatically hotkey add method */
  279. static int auto_hotkey_add(struct acpi_device *device)
  280. {
  281. /* Implement me */
  282. return 0;
  283. }
  284. /* Need to invent automatically hotkey remove method */
  285. static int auto_hotkey_remove(struct acpi_device *device, int type)
  286. {
  287. /* Implement me */
  288. return 0;
  289. }
  290. /* Create a proc file for each polling method */
  291. static int create_polling_proc(union acpi_hotkey *device)
  292. {
  293. struct proc_dir_entry *proc;
  294. char proc_name[80];
  295. mode_t mode;
  296. mode = S_IFREG | S_IRUGO | S_IWUGO;
  297. sprintf(proc_name, "%d", device->link.hotkey_standard_num);
  298. /*
  299. strcat(proc_name, device->poll_hotkey.poll_method);
  300. */
  301. proc = create_proc_entry(proc_name, mode, hotkey_proc_dir);
  302. if (!proc) {
  303. return -ENODEV;
  304. } else {
  305. proc->proc_fops = &hotkey_polling_fops;
  306. proc->owner = THIS_MODULE;
  307. proc->data = device;
  308. proc->uid = 0;
  309. proc->gid = 0;
  310. device->poll_hotkey.proc = proc;
  311. }
  312. return 0;
  313. }
  314. static int hotkey_add(union acpi_hotkey *device)
  315. {
  316. int status = 0;
  317. struct acpi_device *dev = NULL;
  318. if (device->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  319. acpi_bus_get_device(device->event_hotkey.bus_handle, &dev);
  320. status = acpi_install_notify_handler(dev->handle,
  321. ACPI_DEVICE_NOTIFY,
  322. acpi_hotkey_notify_handler,
  323. dev);
  324. } else /* Add polling hotkey */
  325. create_polling_proc(device);
  326. global_hotkey_list.count++;
  327. list_add_tail(&device->link.entries, global_hotkey_list.entries);
  328. return status;
  329. }
  330. static int hotkey_remove(union acpi_hotkey *device)
  331. {
  332. struct list_head *entries, *next;
  333. list_for_each_safe(entries, next, global_hotkey_list.entries) {
  334. union acpi_hotkey *key =
  335. container_of(entries, union acpi_hotkey, entries);
  336. if (key->link.hotkey_standard_num ==
  337. device->link.hotkey_standard_num) {
  338. list_del(&key->link.entries);
  339. free_hotkey_device(key);
  340. global_hotkey_list.count--;
  341. break;
  342. }
  343. }
  344. kfree(device);
  345. return 0;
  346. }
  347. static int hotkey_update(union acpi_hotkey *key)
  348. {
  349. struct list_head *entries;
  350. list_for_each(entries, global_hotkey_list.entries) {
  351. union acpi_hotkey *tmp =
  352. container_of(entries, union acpi_hotkey, entries);
  353. if (tmp->link.hotkey_standard_num ==
  354. key->link.hotkey_standard_num) {
  355. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  356. free_hotkey_buffer(tmp);
  357. tmp->event_hotkey.bus_handle =
  358. key->event_hotkey.bus_handle;
  359. tmp->event_hotkey.external_hotkey_num =
  360. key->event_hotkey.external_hotkey_num;
  361. tmp->event_hotkey.action_handle =
  362. key->event_hotkey.action_handle;
  363. tmp->event_hotkey.action_method =
  364. key->event_hotkey.action_method;
  365. kfree(key);
  366. } else {
  367. /*
  368. char proc_name[80];
  369. sprintf(proc_name, "%d", tmp->link.hotkey_standard_num);
  370. strcat(proc_name, tmp->poll_hotkey.poll_method);
  371. remove_proc_entry(proc_name,hotkey_proc_dir);
  372. */
  373. free_poll_hotkey_buffer(tmp);
  374. tmp->poll_hotkey.poll_handle =
  375. key->poll_hotkey.poll_handle;
  376. tmp->poll_hotkey.poll_method =
  377. key->poll_hotkey.poll_method;
  378. tmp->poll_hotkey.action_handle =
  379. key->poll_hotkey.action_handle;
  380. tmp->poll_hotkey.action_method =
  381. key->poll_hotkey.action_method;
  382. tmp->poll_hotkey.poll_result =
  383. key->poll_hotkey.poll_result;
  384. /*
  385. create_polling_proc(tmp);
  386. */
  387. kfree(key);
  388. }
  389. return 0;
  390. break;
  391. }
  392. }
  393. return -ENODEV;
  394. }
  395. static void free_hotkey_device(union acpi_hotkey *key)
  396. {
  397. struct acpi_device *dev;
  398. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  399. acpi_bus_get_device(key->event_hotkey.bus_handle, &dev);
  400. if (dev->handle)
  401. acpi_remove_notify_handler(dev->handle,
  402. ACPI_DEVICE_NOTIFY,
  403. acpi_hotkey_notify_handler);
  404. free_hotkey_buffer(key);
  405. } else {
  406. char proc_name[80];
  407. sprintf(proc_name, "%d", key->link.hotkey_standard_num);
  408. /*
  409. strcat(proc_name, key->poll_hotkey.poll_method);
  410. */
  411. remove_proc_entry(proc_name, hotkey_proc_dir);
  412. free_poll_hotkey_buffer(key);
  413. }
  414. kfree(key);
  415. return;
  416. }
  417. static void free_hotkey_buffer(union acpi_hotkey *key)
  418. {
  419. /* key would never be null, action method could be */
  420. kfree(key->event_hotkey.action_method);
  421. }
  422. static void free_poll_hotkey_buffer(union acpi_hotkey *key)
  423. {
  424. /* key would never be null, others could be*/
  425. kfree(key->poll_hotkey.action_method);
  426. kfree(key->poll_hotkey.poll_method);
  427. kfree(key->poll_hotkey.poll_result);
  428. }
  429. static int
  430. init_hotkey_device(union acpi_hotkey *key, char **config_entry,
  431. int std_num, int external_num)
  432. {
  433. acpi_handle tmp_handle;
  434. acpi_status status = AE_OK;
  435. if (std_num < 0 || IS_POLL(std_num) || !key)
  436. goto do_fail;
  437. if (!config_entry[bus_handle] || !config_entry[action_handle]
  438. || !config_entry[method])
  439. goto do_fail;
  440. key->link.hotkey_type = ACPI_HOTKEY_EVENT;
  441. key->link.hotkey_standard_num = std_num;
  442. key->event_hotkey.flag = 0;
  443. key->event_hotkey.action_method = config_entry[method];
  444. status = acpi_get_handle(NULL, config_entry[bus_handle],
  445. &(key->event_hotkey.bus_handle));
  446. if (ACPI_FAILURE(status))
  447. goto do_fail_zero;
  448. key->event_hotkey.external_hotkey_num = external_num;
  449. status = acpi_get_handle(NULL, config_entry[action_handle],
  450. &(key->event_hotkey.action_handle));
  451. if (ACPI_FAILURE(status))
  452. goto do_fail_zero;
  453. status = acpi_get_handle(key->event_hotkey.action_handle,
  454. config_entry[method], &tmp_handle);
  455. if (ACPI_FAILURE(status))
  456. goto do_fail_zero;
  457. return AE_OK;
  458. do_fail_zero:
  459. key->event_hotkey.action_method = NULL;
  460. do_fail:
  461. return -ENODEV;
  462. }
  463. static int
  464. init_poll_hotkey_device(union acpi_hotkey *key, char **config_entry,
  465. int std_num)
  466. {
  467. acpi_status status = AE_OK;
  468. acpi_handle tmp_handle;
  469. if (std_num < 0 || IS_EVENT(std_num) || !key)
  470. goto do_fail;
  471. if (!config_entry[bus_handle] ||!config_entry[bus_method] ||
  472. !config_entry[action_handle] || !config_entry[method])
  473. goto do_fail;
  474. key->link.hotkey_type = ACPI_HOTKEY_POLLING;
  475. key->link.hotkey_standard_num = std_num;
  476. key->poll_hotkey.flag = 0;
  477. key->poll_hotkey.poll_method = config_entry[bus_method];
  478. key->poll_hotkey.action_method = config_entry[method];
  479. status = acpi_get_handle(NULL, config_entry[bus_handle],
  480. &(key->poll_hotkey.poll_handle));
  481. if (ACPI_FAILURE(status))
  482. goto do_fail_zero;
  483. status = acpi_get_handle(key->poll_hotkey.poll_handle,
  484. config_entry[bus_method], &tmp_handle);
  485. if (ACPI_FAILURE(status))
  486. goto do_fail_zero;
  487. status =
  488. acpi_get_handle(NULL, config_entry[action_handle],
  489. &(key->poll_hotkey.action_handle));
  490. if (ACPI_FAILURE(status))
  491. goto do_fail_zero;
  492. status = acpi_get_handle(key->poll_hotkey.action_handle,
  493. config_entry[method], &tmp_handle);
  494. if (ACPI_FAILURE(status))
  495. goto do_fail_zero;
  496. key->poll_hotkey.poll_result =
  497. kmalloc(sizeof(union acpi_object), GFP_KERNEL);
  498. if (!key->poll_hotkey.poll_result)
  499. goto do_fail_zero;
  500. return AE_OK;
  501. do_fail_zero:
  502. key->poll_hotkey.poll_method = NULL;
  503. key->poll_hotkey.action_method = NULL;
  504. do_fail:
  505. return -ENODEV;
  506. }
  507. static int hotkey_open_config(struct inode *inode, struct file *file)
  508. {
  509. return (single_open
  510. (file, hotkey_config_seq_show, PDE(inode)->data));
  511. }
  512. static int hotkey_poll_open_config(struct inode *inode, struct file *file)
  513. {
  514. return (single_open
  515. (file, hotkey_poll_config_seq_show, PDE(inode)->data));
  516. }
  517. static int hotkey_config_seq_show(struct seq_file *seq, void *offset)
  518. {
  519. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  520. struct list_head *entries;
  521. char bus_name[ACPI_PATHNAME_MAX] = { 0 };
  522. char action_name[ACPI_PATHNAME_MAX] = { 0 };
  523. struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
  524. struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };
  525. list_for_each(entries, hotkey_list->entries) {
  526. union acpi_hotkey *key =
  527. container_of(entries, union acpi_hotkey, entries);
  528. if (key->link.hotkey_type == ACPI_HOTKEY_EVENT) {
  529. acpi_get_name(key->event_hotkey.bus_handle,
  530. ACPI_NAME_TYPE_MAX, &bus);
  531. acpi_get_name(key->event_hotkey.action_handle,
  532. ACPI_NAME_TYPE_MAX, &act);
  533. seq_printf(seq, "%s:%s:%s:%d:%d\n", bus_name,
  534. action_name,
  535. key->event_hotkey.action_method,
  536. key->link.hotkey_standard_num,
  537. key->event_hotkey.external_hotkey_num);
  538. }
  539. }
  540. seq_puts(seq, "\n");
  541. return 0;
  542. }
  543. static int hotkey_poll_config_seq_show(struct seq_file *seq, void *offset)
  544. {
  545. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  546. struct list_head *entries;
  547. char bus_name[ACPI_PATHNAME_MAX] = { 0 };
  548. char action_name[ACPI_PATHNAME_MAX] = { 0 };
  549. struct acpi_buffer bus = { ACPI_PATHNAME_MAX, bus_name };
  550. struct acpi_buffer act = { ACPI_PATHNAME_MAX, action_name };
  551. list_for_each(entries, hotkey_list->entries) {
  552. union acpi_hotkey *key =
  553. container_of(entries, union acpi_hotkey, entries);
  554. if (key->link.hotkey_type == ACPI_HOTKEY_POLLING) {
  555. acpi_get_name(key->poll_hotkey.poll_handle,
  556. ACPI_NAME_TYPE_MAX, &bus);
  557. acpi_get_name(key->poll_hotkey.action_handle,
  558. ACPI_NAME_TYPE_MAX, &act);
  559. seq_printf(seq, "%s:%s:%s:%s:%d\n", bus_name,
  560. key->poll_hotkey.poll_method,
  561. action_name,
  562. key->poll_hotkey.action_method,
  563. key->link.hotkey_standard_num);
  564. }
  565. }
  566. seq_puts(seq, "\n");
  567. return 0;
  568. }
  569. static int
  570. get_parms(char *config_record, int *cmd, char **config_entry,
  571. int *internal_event_num, int *external_event_num)
  572. {
  573. /* the format of *config_record =
  574. * "1:\d+:*" : "cmd:internal_event_num"
  575. * "\d+:\w+:\w+:\w+:\w+:\d+:\d+" :
  576. * "cmd:bus_handle:bus_method:action_handle:method:internal_event_num:external_event_num"
  577. */
  578. char *tmp, *tmp1, count;
  579. int i;
  580. sscanf(config_record, "%d", cmd);
  581. if (*cmd == 1) {
  582. if (sscanf(config_record, "%d:%d", cmd, internal_event_num) !=
  583. 2)
  584. goto do_fail;
  585. else
  586. return (6);
  587. }
  588. tmp = strchr(config_record, ':');
  589. if (!tmp)
  590. goto do_fail;
  591. tmp++;
  592. for (i = 0; i < LAST_CONF_ENTRY; i++) {
  593. tmp1 = strchr(tmp, ':');
  594. if (!tmp1) {
  595. goto do_fail;
  596. }
  597. count = tmp1 - tmp;
  598. config_entry[i] = kzalloc(count + 1, GFP_KERNEL);
  599. if (!config_entry[i])
  600. goto handle_failure;
  601. strncpy(config_entry[i], tmp, count);
  602. tmp = tmp1 + 1;
  603. }
  604. if (sscanf(tmp, "%d:%d", internal_event_num, external_event_num) <= 0)
  605. goto handle_failure;
  606. if (!IS_OTHERS(*internal_event_num)) {
  607. return 6;
  608. }
  609. handle_failure:
  610. while (i-- > 0)
  611. kfree(config_entry[i]);
  612. do_fail:
  613. return -1;
  614. }
  615. /* count is length for one input record */
  616. static ssize_t hotkey_write_config(struct file *file,
  617. const char __user * buffer,
  618. size_t count, loff_t * data)
  619. {
  620. char *config_record = NULL;
  621. char *config_entry[LAST_CONF_ENTRY];
  622. int cmd, internal_event_num, external_event_num;
  623. int ret = 0;
  624. union acpi_hotkey *key = kzalloc(sizeof(union acpi_hotkey), GFP_KERNEL);
  625. if (!key)
  626. return -ENOMEM;
  627. config_record = kzalloc(count + 1, GFP_KERNEL);
  628. if (!config_record) {
  629. kfree(key);
  630. return -ENOMEM;
  631. }
  632. if (copy_from_user(config_record, buffer, count)) {
  633. kfree(config_record);
  634. kfree(key);
  635. printk(KERN_ERR PREFIX "Invalid data\n");
  636. return -EINVAL;
  637. }
  638. ret = get_parms(config_record, &cmd, config_entry,
  639. &internal_event_num, &external_event_num);
  640. kfree(config_record);
  641. if (ret != 6) {
  642. printk(KERN_ERR PREFIX "Invalid data format ret=%d\n", ret);
  643. return -EINVAL;
  644. }
  645. if (cmd == 1) {
  646. union acpi_hotkey *tmp = NULL;
  647. tmp = get_hotkey_by_event(&global_hotkey_list,
  648. internal_event_num);
  649. if (!tmp)
  650. printk(KERN_ERR PREFIX "Invalid key\n");
  651. else
  652. memcpy(key, tmp, sizeof(union acpi_hotkey));
  653. goto cont_cmd;
  654. }
  655. if (IS_EVENT(internal_event_num)) {
  656. if (init_hotkey_device(key, config_entry,
  657. internal_event_num, external_event_num))
  658. goto init_hotkey_fail;
  659. } else {
  660. if (init_poll_hotkey_device(key, config_entry,
  661. internal_event_num))
  662. goto init_poll_hotkey_fail;
  663. }
  664. cont_cmd:
  665. switch (cmd) {
  666. case 0:
  667. if (get_hotkey_by_event(&global_hotkey_list,
  668. key->link.hotkey_standard_num))
  669. goto fail_out;
  670. else
  671. hotkey_add(key);
  672. break;
  673. case 1:
  674. hotkey_remove(key);
  675. break;
  676. case 2:
  677. /* key is kfree()ed if matched*/
  678. if (hotkey_update(key))
  679. goto fail_out;
  680. break;
  681. default:
  682. goto fail_out;
  683. break;
  684. }
  685. return count;
  686. init_poll_hotkey_fail: /* failed init_poll_hotkey_device */
  687. kfree(config_entry[bus_method]);
  688. config_entry[bus_method] = NULL;
  689. init_hotkey_fail: /* failed init_hotkey_device */
  690. kfree(config_entry[method]);
  691. fail_out:
  692. kfree(config_entry[bus_handle]);
  693. kfree(config_entry[action_handle]);
  694. /* No double free since elements =NULL for error cases */
  695. if (IS_EVENT(internal_event_num)) {
  696. if (config_entry[bus_method])
  697. kfree(config_entry[bus_method]);
  698. free_hotkey_buffer(key); /* frees [method] */
  699. } else
  700. free_poll_hotkey_buffer(key); /* frees [bus_method]+[method] */
  701. kfree(key);
  702. printk(KERN_ERR PREFIX "invalid key\n");
  703. return -EINVAL;
  704. }
  705. /*
  706. * This function evaluates an ACPI method, given an int as parameter, the
  707. * method is searched within the scope of the handle, can be NULL. The output
  708. * of the method is written is output, which can also be NULL
  709. *
  710. * returns 1 if write is successful, 0 else.
  711. */
  712. static int write_acpi_int(acpi_handle handle, const char *method, int val,
  713. struct acpi_buffer *output)
  714. {
  715. struct acpi_object_list params; /* list of input parameters (an int here) */
  716. union acpi_object in_obj; /* the only param we use */
  717. acpi_status status;
  718. params.count = 1;
  719. params.pointer = &in_obj;
  720. in_obj.type = ACPI_TYPE_INTEGER;
  721. in_obj.integer.value = val;
  722. status = acpi_evaluate_object(handle, (char *)method, &params, output);
  723. return (status == AE_OK);
  724. }
  725. static int read_acpi_int(acpi_handle handle, const char *method,
  726. union acpi_object *val)
  727. {
  728. struct acpi_buffer output;
  729. union acpi_object out_obj;
  730. acpi_status status;
  731. output.length = sizeof(out_obj);
  732. output.pointer = &out_obj;
  733. status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
  734. if (val) {
  735. val->integer.value = out_obj.integer.value;
  736. val->type = out_obj.type;
  737. } else
  738. printk(KERN_ERR PREFIX "null val pointer\n");
  739. return ((status == AE_OK)
  740. && (out_obj.type == ACPI_TYPE_INTEGER));
  741. }
  742. static union acpi_hotkey *get_hotkey_by_event(struct
  743. acpi_hotkey_list
  744. *hotkey_list, int event)
  745. {
  746. struct list_head *entries;
  747. list_for_each(entries, hotkey_list->entries) {
  748. union acpi_hotkey *key =
  749. container_of(entries, union acpi_hotkey, entries);
  750. if (key->link.hotkey_standard_num == event) {
  751. return (key);
  752. }
  753. }
  754. return (NULL);
  755. }
  756. /*
  757. * user call AML method interface:
  758. * Call convention:
  759. * echo "event_num: arg type : value"
  760. * example: echo "1:1:30" > /proc/acpi/action
  761. * Just support 1 integer arg passing to AML method
  762. */
  763. static ssize_t hotkey_execute_aml_method(struct file *file,
  764. const char __user * buffer,
  765. size_t count, loff_t * data)
  766. {
  767. struct acpi_hotkey_list *hotkey_list = &global_hotkey_list;
  768. char *arg;
  769. int event, method_type, type, value;
  770. union acpi_hotkey *key;
  771. arg = kzalloc(count + 1, GFP_KERNEL);
  772. if (!arg)
  773. return -ENOMEM;
  774. if (copy_from_user(arg, buffer, count)) {
  775. kfree(arg);
  776. printk(KERN_ERR PREFIX "Invalid argument 2\n");
  777. return -EINVAL;
  778. }
  779. if (sscanf(arg, "%d:%d:%d:%d", &event, &method_type, &type, &value) !=
  780. 4) {
  781. kfree(arg);
  782. printk(KERN_ERR PREFIX "Invalid argument 3\n");
  783. return -EINVAL;
  784. }
  785. kfree(arg);
  786. if (type == ACPI_TYPE_INTEGER) {
  787. key = get_hotkey_by_event(hotkey_list, event);
  788. if (!key)
  789. goto do_fail;
  790. if (IS_EVENT(event))
  791. write_acpi_int(key->event_hotkey.action_handle,
  792. key->event_hotkey.action_method, value,
  793. NULL);
  794. else if (IS_POLL(event)) {
  795. if (method_type == POLL_METHOD)
  796. read_acpi_int(key->poll_hotkey.poll_handle,
  797. key->poll_hotkey.poll_method,
  798. key->poll_hotkey.poll_result);
  799. else if (method_type == ACTION_METHOD)
  800. write_acpi_int(key->poll_hotkey.action_handle,
  801. key->poll_hotkey.action_method,
  802. value, NULL);
  803. else
  804. goto do_fail;
  805. }
  806. } else {
  807. printk(KERN_WARNING "Not supported\n");
  808. return -EINVAL;
  809. }
  810. return count;
  811. do_fail:
  812. return -EINVAL;
  813. }
  814. static int __init hotkey_init(void)
  815. {
  816. int result;
  817. mode_t mode = S_IFREG | S_IRUGO | S_IWUGO;
  818. if (acpi_disabled)
  819. return -ENODEV;
  820. if (acpi_specific_hotkey_enabled) {
  821. printk("Using specific hotkey driver\n");
  822. return -ENODEV;
  823. }
  824. hotkey_proc_dir = proc_mkdir(HOTKEY_PROC, acpi_root_dir);
  825. if (!hotkey_proc_dir) {
  826. return (-ENODEV);
  827. }
  828. hotkey_proc_dir->owner = THIS_MODULE;
  829. hotkey_config =
  830. create_proc_entry(HOTKEY_EV_CONFIG, mode, hotkey_proc_dir);
  831. if (!hotkey_config) {
  832. goto do_fail1;
  833. } else {
  834. hotkey_config->proc_fops = &hotkey_config_fops;
  835. hotkey_config->data = &global_hotkey_list;
  836. hotkey_config->owner = THIS_MODULE;
  837. hotkey_config->uid = 0;
  838. hotkey_config->gid = 0;
  839. }
  840. hotkey_poll_config =
  841. create_proc_entry(HOTKEY_PL_CONFIG, mode, hotkey_proc_dir);
  842. if (!hotkey_poll_config) {
  843. goto do_fail2;
  844. } else {
  845. hotkey_poll_config->proc_fops = &hotkey_poll_config_fops;
  846. hotkey_poll_config->data = &global_hotkey_list;
  847. hotkey_poll_config->owner = THIS_MODULE;
  848. hotkey_poll_config->uid = 0;
  849. hotkey_poll_config->gid = 0;
  850. }
  851. hotkey_action = create_proc_entry(HOTKEY_ACTION, mode, hotkey_proc_dir);
  852. if (!hotkey_action) {
  853. goto do_fail3;
  854. } else {
  855. hotkey_action->proc_fops = &hotkey_action_fops;
  856. hotkey_action->owner = THIS_MODULE;
  857. hotkey_action->uid = 0;
  858. hotkey_action->gid = 0;
  859. }
  860. hotkey_info = create_proc_entry(HOTKEY_INFO, mode, hotkey_proc_dir);
  861. if (!hotkey_info) {
  862. goto do_fail4;
  863. } else {
  864. hotkey_info->proc_fops = &hotkey_info_fops;
  865. hotkey_info->owner = THIS_MODULE;
  866. hotkey_info->uid = 0;
  867. hotkey_info->gid = 0;
  868. }
  869. result = acpi_bus_register_driver(&hotkey_driver);
  870. if (result < 0)
  871. goto do_fail5;
  872. global_hotkey_list.count = 0;
  873. global_hotkey_list.entries = &hotkey_entries;
  874. INIT_LIST_HEAD(&hotkey_entries);
  875. return (0);
  876. do_fail5:
  877. remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
  878. do_fail4:
  879. remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
  880. do_fail3:
  881. remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
  882. do_fail2:
  883. remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
  884. do_fail1:
  885. remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
  886. return (-ENODEV);
  887. }
  888. static void __exit hotkey_exit(void)
  889. {
  890. struct list_head *entries, *next;
  891. list_for_each_safe(entries, next, global_hotkey_list.entries) {
  892. union acpi_hotkey *key =
  893. container_of(entries, union acpi_hotkey, entries);
  894. acpi_os_wait_events_complete(NULL);
  895. list_del(&key->link.entries);
  896. global_hotkey_list.count--;
  897. free_hotkey_device(key);
  898. }
  899. acpi_bus_unregister_driver(&hotkey_driver);
  900. remove_proc_entry(HOTKEY_EV_CONFIG, hotkey_proc_dir);
  901. remove_proc_entry(HOTKEY_PL_CONFIG, hotkey_proc_dir);
  902. remove_proc_entry(HOTKEY_ACTION, hotkey_proc_dir);
  903. remove_proc_entry(HOTKEY_INFO, hotkey_proc_dir);
  904. remove_proc_entry(HOTKEY_PROC, acpi_root_dir);
  905. return;
  906. }
  907. module_init(hotkey_init);
  908. module_exit(hotkey_exit);