sony_acpi.c 12 KB

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