sony_acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 <linux/backlight.h>
  30. #include <linux/err.h>
  31. #include <acpi/acpi_drivers.h>
  32. #include <acpi/acpi_bus.h>
  33. #include <asm/uaccess.h>
  34. #define ACPI_SNC_CLASS "sony"
  35. #define ACPI_SNC_HID "SNY5001"
  36. #define ACPI_SNC_DRIVER_NAME "ACPI Sony Notebook Control Driver v0.3"
  37. /* the device uses 1-based values, while the backlight subsystem uses
  38. 0-based values */
  39. #define SONY_MAX_BRIGHTNESS 8
  40. #define LOG_PFX KERN_WARNING "sony_acpi: "
  41. MODULE_AUTHOR("Stelian Pop");
  42. MODULE_DESCRIPTION(ACPI_SNC_DRIVER_NAME);
  43. MODULE_LICENSE("GPL");
  44. static int debug;
  45. module_param(debug, int, 0);
  46. MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
  47. "the development of this driver");
  48. static acpi_handle sony_acpi_handle;
  49. static struct proc_dir_entry *sony_acpi_dir;
  50. static int sony_backlight_update_status(struct backlight_device *bd);
  51. static int sony_backlight_get_brightness(struct backlight_device *bd);
  52. static struct backlight_device *sony_backlight_device;
  53. static struct backlight_properties sony_backlight_properties = {
  54. .owner = THIS_MODULE,
  55. .update_status = sony_backlight_update_status,
  56. .get_brightness = sony_backlight_get_brightness,
  57. .max_brightness = SONY_MAX_BRIGHTNESS - 1,
  58. };
  59. static struct sony_acpi_value {
  60. char *name; /* name of the entry */
  61. struct proc_dir_entry *proc; /* /proc entry */
  62. char *acpiget;/* name of the ACPI get function */
  63. char *acpiset;/* name of the ACPI get function */
  64. int min; /* minimum allowed value or -1 */
  65. int max; /* maximum allowed value or -1 */
  66. int value; /* current setting */
  67. int valid; /* Has ever been set */
  68. int debug; /* active only in debug mode ? */
  69. } sony_acpi_values[] = {
  70. {
  71. .name = "brightness_default",
  72. .acpiget = "GPBR",
  73. .acpiset = "SPBR",
  74. .min = 1,
  75. .max = SONY_MAX_BRIGHTNESS,
  76. .debug = 0,
  77. },
  78. {
  79. .name = "fnkey",
  80. .acpiget = "GHKE",
  81. .debug = 0,
  82. },
  83. {
  84. .name = "cdpower",
  85. .acpiget = "GCDP",
  86. .acpiset = "SCDP",
  87. .min = -1,
  88. .max = -1,
  89. .debug = 0,
  90. },
  91. {
  92. .name = "PID",
  93. .acpiget = "GPID",
  94. .debug = 1,
  95. },
  96. {
  97. .name = "CTR",
  98. .acpiget = "GCTR",
  99. .acpiset = "SCTR",
  100. .min = -1,
  101. .max = -1,
  102. .debug = 1,
  103. },
  104. {
  105. .name = "PCR",
  106. .acpiget = "GPCR",
  107. .acpiset = "SPCR",
  108. .min = -1,
  109. .max = -1,
  110. .debug = 1,
  111. },
  112. {
  113. .name = "CMI",
  114. .acpiget = "GCMI",
  115. .acpiset = "SCMI",
  116. .min = -1,
  117. .max = -1,
  118. .debug = 1,
  119. },
  120. {
  121. .name = NULL,
  122. }
  123. };
  124. static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
  125. {
  126. struct acpi_buffer output;
  127. union acpi_object out_obj;
  128. acpi_status status;
  129. output.length = sizeof(out_obj);
  130. output.pointer = &out_obj;
  131. status = acpi_evaluate_object(handle, name, NULL, &output);
  132. if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
  133. *result = out_obj.integer.value;
  134. return 0;
  135. }
  136. printk(LOG_PFX "acpi_callreadfunc failed\n");
  137. return -1;
  138. }
  139. static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
  140. int *result)
  141. {
  142. struct acpi_object_list params;
  143. union acpi_object in_obj;
  144. struct acpi_buffer output;
  145. union acpi_object out_obj;
  146. acpi_status status;
  147. params.count = 1;
  148. params.pointer = &in_obj;
  149. in_obj.type = ACPI_TYPE_INTEGER;
  150. in_obj.integer.value = value;
  151. output.length = sizeof(out_obj);
  152. output.pointer = &out_obj;
  153. status = acpi_evaluate_object(handle, name, &params, &output);
  154. if (status == AE_OK) {
  155. if (result != NULL) {
  156. if (out_obj.type != ACPI_TYPE_INTEGER) {
  157. printk(LOG_PFX "acpi_evaluate_object bad "
  158. "return type\n");
  159. return -1;
  160. }
  161. *result = out_obj.integer.value;
  162. }
  163. return 0;
  164. }
  165. printk(LOG_PFX "acpi_evaluate_object failed\n");
  166. return -1;
  167. }
  168. static int parse_buffer(const char __user *buffer, unsigned long count,
  169. int *val) {
  170. char s[32];
  171. int ret;
  172. if (count > 31)
  173. return -EINVAL;
  174. if (copy_from_user(s, buffer, count))
  175. return -EFAULT;
  176. s[count] = '\0';
  177. ret = simple_strtoul(s, NULL, 10);
  178. *val = ret;
  179. return 0;
  180. }
  181. static int sony_acpi_read(char* page, char** start, off_t off, int count,
  182. int* eof, void *data)
  183. {
  184. struct sony_acpi_value *item = data;
  185. int value;
  186. if (!item->acpiget)
  187. return -EIO;
  188. if (acpi_callgetfunc(sony_acpi_handle, item->acpiget, &value) < 0)
  189. return -EIO;
  190. return sprintf(page, "%d\n", value);
  191. }
  192. static int sony_acpi_write(struct file *file, const char __user *buffer,
  193. unsigned long count, void *data)
  194. {
  195. struct sony_acpi_value *item = data;
  196. int result;
  197. int value;
  198. if (!item->acpiset)
  199. return -EIO;
  200. if ((result = parse_buffer(buffer, count, &value)) < 0)
  201. return result;
  202. if (item->min != -1 && value < item->min)
  203. return -EINVAL;
  204. if (item->max != -1 && value > item->max)
  205. return -EINVAL;
  206. if (acpi_callsetfunc(sony_acpi_handle, item->acpiset, value, NULL) < 0)
  207. return -EIO;
  208. item->value = value;
  209. item->valid = 1;
  210. return count;
  211. }
  212. static int sony_acpi_resume(struct acpi_device *device)
  213. {
  214. struct sony_acpi_value *item;
  215. for (item = sony_acpi_values; item->name; item++) {
  216. int ret;
  217. if (!item->valid)
  218. continue;
  219. ret = acpi_callsetfunc(sony_acpi_handle, item->acpiset,
  220. item->value, NULL);
  221. if (ret < 0) {
  222. printk("%s: %d\n", __FUNCTION__, ret);
  223. break;
  224. }
  225. }
  226. return 0;
  227. }
  228. static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
  229. {
  230. printk(LOG_PFX "sony_acpi_notify\n");
  231. }
  232. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  233. void *context, void **return_value)
  234. {
  235. struct acpi_namespace_node *node;
  236. union acpi_operand_object *operand;
  237. node = (struct acpi_namespace_node *) handle;
  238. operand = (union acpi_operand_object *) node->object;
  239. printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
  240. (u32) operand->method.param_count);
  241. return AE_OK;
  242. }
  243. static int sony_acpi_add(struct acpi_device *device)
  244. {
  245. acpi_status status;
  246. int result;
  247. acpi_handle handle;
  248. struct sony_acpi_value *item;
  249. sony_acpi_handle = device->handle;
  250. acpi_driver_data(device) = NULL;
  251. acpi_device_dir(device) = sony_acpi_dir;
  252. if (debug) {
  253. status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_acpi_handle,
  254. 1, sony_walk_callback, NULL, NULL);
  255. if (ACPI_FAILURE(status)) {
  256. printk(LOG_PFX "unable to walk acpi resources\n");
  257. result = -ENODEV;
  258. goto outwalk;
  259. }
  260. status = acpi_install_notify_handler(sony_acpi_handle,
  261. ACPI_DEVICE_NOTIFY,
  262. sony_acpi_notify,
  263. NULL);
  264. if (ACPI_FAILURE(status)) {
  265. printk(LOG_PFX "unable to install notify handler\n");
  266. result = -ENODEV;
  267. goto outnotify;
  268. }
  269. }
  270. if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle, "GBRT", &handle))) {
  271. sony_backlight_device = backlight_device_register("sony", NULL,
  272. &sony_backlight_properties);
  273. if (IS_ERR(sony_backlight_device)) {
  274. printk(LOG_PFX "unable to register backlight device\n");
  275. }
  276. }
  277. for (item = sony_acpi_values; item->name; ++item) {
  278. if (!debug && item->debug)
  279. continue;
  280. if (item->acpiget &&
  281. ACPI_FAILURE(acpi_get_handle(sony_acpi_handle,
  282. item->acpiget, &handle)))
  283. continue;
  284. if (item->acpiset &&
  285. ACPI_FAILURE(acpi_get_handle(sony_acpi_handle,
  286. item->acpiset, &handle)))
  287. continue;
  288. item->proc = create_proc_entry(item->name, 0600,
  289. acpi_device_dir(device));
  290. if (!item->proc) {
  291. printk(LOG_PFX "unable to create proc entry\n");
  292. result = -EIO;
  293. goto outproc;
  294. }
  295. item->proc->read_proc = sony_acpi_read;
  296. item->proc->write_proc = sony_acpi_write;
  297. item->proc->data = item;
  298. item->proc->owner = THIS_MODULE;
  299. }
  300. printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully installed\n");
  301. return 0;
  302. outproc:
  303. if (debug) {
  304. status = acpi_remove_notify_handler(sony_acpi_handle,
  305. ACPI_DEVICE_NOTIFY,
  306. sony_acpi_notify);
  307. if (ACPI_FAILURE(status))
  308. printk(LOG_PFX "unable to remove notify handler\n");
  309. }
  310. outnotify:
  311. for (item = sony_acpi_values; item->name; ++item)
  312. if (item->proc)
  313. remove_proc_entry(item->name, acpi_device_dir(device));
  314. outwalk:
  315. return result;
  316. }
  317. static int sony_acpi_remove(struct acpi_device *device, int type)
  318. {
  319. acpi_status status;
  320. struct sony_acpi_value *item;
  321. if (sony_backlight_device)
  322. backlight_device_unregister(sony_backlight_device);
  323. if (debug) {
  324. status = acpi_remove_notify_handler(sony_acpi_handle,
  325. ACPI_DEVICE_NOTIFY,
  326. sony_acpi_notify);
  327. if (ACPI_FAILURE(status))
  328. printk(LOG_PFX "unable to remove notify handler\n");
  329. }
  330. for (item = sony_acpi_values; item->name; ++item)
  331. if (item->proc)
  332. remove_proc_entry(item->name, acpi_device_dir(device));
  333. printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully removed\n");
  334. return 0;
  335. }
  336. static int sony_backlight_update_status(struct backlight_device *bd)
  337. {
  338. return acpi_callsetfunc(sony_acpi_handle, "SBRT",
  339. bd->props->brightness + 1,
  340. NULL);
  341. }
  342. static int sony_backlight_get_brightness(struct backlight_device *bd)
  343. {
  344. int value;
  345. if (acpi_callgetfunc(sony_acpi_handle, "GBRT", &value))
  346. return 0;
  347. /* brightness levels are 1-based, while backlight ones are 0-based */
  348. return value - 1;
  349. }
  350. static struct acpi_driver sony_acpi_driver = {
  351. .name = ACPI_SNC_DRIVER_NAME,
  352. .class = ACPI_SNC_CLASS,
  353. .ids = ACPI_SNC_HID,
  354. .ops = {
  355. .add = sony_acpi_add,
  356. .remove = sony_acpi_remove,
  357. .resume = sony_acpi_resume,
  358. },
  359. };
  360. static int __init sony_acpi_init(void)
  361. {
  362. int result;
  363. sony_acpi_dir = proc_mkdir("sony", acpi_root_dir);
  364. if (!sony_acpi_dir) {
  365. printk(LOG_PFX "unable to create /proc entry\n");
  366. return -ENODEV;
  367. }
  368. sony_acpi_dir->owner = THIS_MODULE;
  369. result = acpi_bus_register_driver(&sony_acpi_driver);
  370. if (result < 0) {
  371. remove_proc_entry("sony", acpi_root_dir);
  372. return -ENODEV;
  373. }
  374. return 0;
  375. }
  376. static void __exit sony_acpi_exit(void)
  377. {
  378. acpi_bus_unregister_driver(&sony_acpi_driver);
  379. remove_proc_entry("sony", acpi_root_dir);
  380. }
  381. module_init(sony_acpi_init);
  382. module_exit(sony_acpi_exit);