hotkey.c 29 KB

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