sony_acpi.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * ACPI Sony Notebook Control Driver (SNC)
  3. *
  4. * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
  5. *
  6. * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
  7. * which are copyrighted by their respective authors.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <acpi/acpi_drivers.h>
  30. #include <acpi/acpi_bus.h>
  31. #include <asm/uaccess.h>
  32. #define ACPI_SNC_CLASS "sony"
  33. #define ACPI_SNC_HID "SNY5001"
  34. #define ACPI_SNC_DRIVER_NAME "ACPI Sony Notebook Control Driver v0.2"
  35. #define LOG_PFX KERN_WARNING "sony_acpi: "
  36. MODULE_AUTHOR("Stelian Pop");
  37. MODULE_DESCRIPTION(ACPI_SNC_DRIVER_NAME);
  38. MODULE_LICENSE("GPL");
  39. static int debug;
  40. module_param(debug, int, 0);
  41. MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
  42. "the development of this driver");
  43. static int sony_acpi_add (struct acpi_device *device);
  44. static int sony_acpi_remove (struct acpi_device *device, int type);
  45. static struct acpi_driver sony_acpi_driver = {
  46. .name = ACPI_SNC_DRIVER_NAME,
  47. .class = ACPI_SNC_CLASS,
  48. .ids = ACPI_SNC_HID,
  49. .ops = {
  50. .add = sony_acpi_add,
  51. .remove = sony_acpi_remove,
  52. },
  53. };
  54. static acpi_handle sony_acpi_handle;
  55. static struct proc_dir_entry *sony_acpi_dir;
  56. static struct sony_acpi_value {
  57. char *name; /* name of the entry */
  58. struct proc_dir_entry *proc; /* /proc entry */
  59. char *acpiget;/* name of the ACPI get function */
  60. char *acpiset;/* name of the ACPI get function */
  61. int min; /* minimum allowed value or -1 */
  62. int max; /* maximum allowed value or -1 */
  63. int debug; /* active only in debug mode ? */
  64. } sony_acpi_values[] = {
  65. {
  66. .name = "brightness",
  67. .acpiget = "GBRT",
  68. .acpiset = "SBRT",
  69. .min = 1,
  70. .max = 8,
  71. .debug = 0,
  72. },
  73. {
  74. .name = "brightness_default",
  75. .acpiget = "GPBR",
  76. .acpiset = "SPBR",
  77. .min = 1,
  78. .max = 8,
  79. .debug = 0,
  80. },
  81. {
  82. .name = "fnkey",
  83. .acpiget = "GHKE",
  84. .debug = 0,
  85. },
  86. {
  87. .name = "cdpower",
  88. .acpiget = "GCDP",
  89. .acpiset = "SCDP",
  90. .min = -1,
  91. .max = -1,
  92. .debug = 0,
  93. },
  94. {
  95. .name = "PID",
  96. .acpiget = "GPID",
  97. .debug = 1,
  98. },
  99. {
  100. .name = "CTR",
  101. .acpiget = "GCTR",
  102. .acpiset = "SCTR",
  103. .min = -1,
  104. .max = -1,
  105. .debug = 1,
  106. },
  107. {
  108. .name = "PCR",
  109. .acpiget = "GPCR",
  110. .acpiset = "SPCR",
  111. .min = -1,
  112. .max = -1,
  113. .debug = 1,
  114. },
  115. {
  116. .name = "CMI",
  117. .acpiget = "GCMI",
  118. .acpiset = "SCMI",
  119. .min = -1,
  120. .max = -1,
  121. .debug = 1,
  122. },
  123. {
  124. .name = NULL,
  125. }
  126. };
  127. static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
  128. {
  129. struct acpi_buffer output;
  130. union acpi_object out_obj;
  131. acpi_status status;
  132. output.length = sizeof(out_obj);
  133. output.pointer = &out_obj;
  134. status = acpi_evaluate_object(handle, name, NULL, &output);
  135. if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
  136. *result = out_obj.integer.value;
  137. return 0;
  138. }
  139. printk(LOG_PFX "acpi_callreadfunc failed\n");
  140. return -1;
  141. }
  142. static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
  143. int *result)
  144. {
  145. struct acpi_object_list params;
  146. union acpi_object in_obj;
  147. struct acpi_buffer output;
  148. union acpi_object out_obj;
  149. acpi_status status;
  150. params.count = 1;
  151. params.pointer = &in_obj;
  152. in_obj.type = ACPI_TYPE_INTEGER;
  153. in_obj.integer.value = value;
  154. output.length = sizeof(out_obj);
  155. output.pointer = &out_obj;
  156. status = acpi_evaluate_object(handle, name, &params, &output);
  157. if (status == AE_OK) {
  158. if (result != NULL) {
  159. if (out_obj.type != ACPI_TYPE_INTEGER) {
  160. printk(LOG_PFX "acpi_evaluate_object bad "
  161. "return type\n");
  162. return -1;
  163. }
  164. *result = out_obj.integer.value;
  165. }
  166. return 0;
  167. }
  168. printk(LOG_PFX "acpi_evaluate_object failed\n");
  169. return -1;
  170. }
  171. static int parse_buffer(const char __user *buffer, unsigned long count,
  172. int *val) {
  173. char s[32];
  174. int ret;
  175. if (count > 31)
  176. return -EINVAL;
  177. if (copy_from_user(s, buffer, count))
  178. return -EFAULT;
  179. s[count] = '\0';
  180. ret = simple_strtoul(s, NULL, 10);
  181. *val = ret;
  182. return 0;
  183. }
  184. static int sony_acpi_read(char* page, char** start, off_t off, int count,
  185. int* eof, void *data)
  186. {
  187. struct sony_acpi_value *item = data;
  188. int value;
  189. if (!item->acpiget)
  190. return -EIO;
  191. if (acpi_callgetfunc(sony_acpi_handle, item->acpiget, &value) < 0)
  192. return -EIO;
  193. return sprintf(page, "%d\n", value);
  194. }
  195. static int sony_acpi_write(struct file *file, const char __user *buffer,
  196. unsigned long count, void *data)
  197. {
  198. struct sony_acpi_value *item = data;
  199. int result;
  200. int value;
  201. if (!item->acpiset)
  202. return -EIO;
  203. if ((result = parse_buffer(buffer, count, &value)) < 0)
  204. return result;
  205. if (item->min != -1 && value < item->min)
  206. return -EINVAL;
  207. if (item->max != -1 && value > item->max)
  208. return -EINVAL;
  209. if (acpi_callsetfunc(sony_acpi_handle, item->acpiset, value, NULL) < 0)
  210. return -EIO;
  211. return count;
  212. }
  213. static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
  214. {
  215. printk(LOG_PFX "sony_acpi_notify\n");
  216. }
  217. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  218. void *context, void **return_value)
  219. {
  220. struct acpi_namespace_node *node;
  221. union acpi_operand_object *operand;
  222. node = (struct acpi_namespace_node *) handle;
  223. operand = (union acpi_operand_object *) node->object;
  224. printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
  225. (u32) operand->method.param_count);
  226. return AE_OK;
  227. }
  228. static int sony_acpi_add(struct acpi_device *device)
  229. {
  230. acpi_status status;
  231. int result;
  232. struct sony_acpi_value *item;
  233. sony_acpi_handle = device->handle;
  234. acpi_driver_data(device) = NULL;
  235. acpi_device_dir(device) = sony_acpi_dir;
  236. if (debug) {
  237. status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_acpi_handle,
  238. 1, sony_walk_callback, NULL, NULL);
  239. if (ACPI_FAILURE(status)) {
  240. printk(LOG_PFX "unable to walk acpi resources\n");
  241. result = -ENODEV;
  242. goto outwalk;
  243. }
  244. status = acpi_install_notify_handler(sony_acpi_handle,
  245. ACPI_DEVICE_NOTIFY,
  246. sony_acpi_notify,
  247. NULL);
  248. if (ACPI_FAILURE(status)) {
  249. printk(LOG_PFX "unable to install notify handler\n");
  250. result = -ENODEV;
  251. goto outnotify;
  252. }
  253. }
  254. for (item = sony_acpi_values; item->name; ++item) {
  255. acpi_handle handle;
  256. if (!debug && item->debug)
  257. continue;
  258. if (item->acpiget &&
  259. ACPI_FAILURE(acpi_get_handle(sony_acpi_handle,
  260. item->acpiget, &handle)))
  261. continue;
  262. if (item->acpiset &&
  263. ACPI_FAILURE(acpi_get_handle(sony_acpi_handle,
  264. item->acpiset, &handle)))
  265. continue;
  266. item->proc = create_proc_entry(item->name, 0600,
  267. acpi_device_dir(device));
  268. if (!item->proc) {
  269. printk(LOG_PFX "unable to create proc entry\n");
  270. result = -EIO;
  271. goto outproc;
  272. }
  273. item->proc->read_proc = sony_acpi_read;
  274. item->proc->write_proc = sony_acpi_write;
  275. item->proc->data = item;
  276. item->proc->owner = THIS_MODULE;
  277. }
  278. printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully installed\n");
  279. return 0;
  280. outproc:
  281. if (debug) {
  282. status = acpi_remove_notify_handler(sony_acpi_handle,
  283. ACPI_DEVICE_NOTIFY,
  284. sony_acpi_notify);
  285. if (ACPI_FAILURE(status))
  286. printk(LOG_PFX "unable to remove notify handler\n");
  287. }
  288. outnotify:
  289. for (item = sony_acpi_values; item->name; ++item)
  290. if (item->proc)
  291. remove_proc_entry(item->name, acpi_device_dir(device));
  292. outwalk:
  293. return result;
  294. }
  295. static int sony_acpi_remove(struct acpi_device *device, int type)
  296. {
  297. acpi_status status;
  298. struct sony_acpi_value *item;
  299. if (debug) {
  300. status = acpi_remove_notify_handler(sony_acpi_handle,
  301. ACPI_DEVICE_NOTIFY,
  302. sony_acpi_notify);
  303. if (ACPI_FAILURE(status))
  304. printk(LOG_PFX "unable to remove notify handler\n");
  305. }
  306. for (item = sony_acpi_values; item->name; ++item)
  307. if (item->proc)
  308. remove_proc_entry(item->name, acpi_device_dir(device));
  309. printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully removed\n");
  310. return 0;
  311. }
  312. static int __init sony_acpi_init(void)
  313. {
  314. int result;
  315. sony_acpi_dir = proc_mkdir("sony", acpi_root_dir);
  316. if (!sony_acpi_dir) {
  317. printk(LOG_PFX "unable to create /proc entry\n");
  318. return -ENODEV;
  319. }
  320. sony_acpi_dir->owner = THIS_MODULE;
  321. result = acpi_bus_register_driver(&sony_acpi_driver);
  322. if (result < 0) {
  323. remove_proc_entry("sony", acpi_root_dir);
  324. return -ENODEV;
  325. }
  326. return 0;
  327. }
  328. static void __exit sony_acpi_exit(void)
  329. {
  330. acpi_bus_unregister_driver(&sony_acpi_driver);
  331. remove_proc_entry("sony", acpi_root_dir);
  332. }
  333. module_init(sony_acpi_init);
  334. module_exit(sony_acpi_exit);