sony-laptop.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * ACPI Sony Notebook Control Driver (SNC)
  3. *
  4. * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
  5. * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
  6. *
  7. * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
  8. * which are copyrighted by their respective authors.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/backlight.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/err.h>
  33. #include <acpi/acpi_drivers.h>
  34. #include <acpi/acpi_bus.h>
  35. #include <asm/uaccess.h>
  36. #define ACPI_SNC_CLASS "sony"
  37. #define ACPI_SNC_HID "SNY5001"
  38. #define ACPI_SNC_DRIVER_NAME "ACPI Sony Notebook Control Driver v0.4"
  39. /* the device uses 1-based values, while the backlight subsystem uses
  40. 0-based values */
  41. #define SONY_MAX_BRIGHTNESS 8
  42. #define LOG_PFX KERN_WARNING "sony-laptop: "
  43. MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
  44. MODULE_DESCRIPTION(ACPI_SNC_DRIVER_NAME);
  45. MODULE_LICENSE("GPL");
  46. static int debug;
  47. module_param(debug, int, 0);
  48. MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
  49. "the development of this driver");
  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 ssize_t sony_acpi_show(struct device *, struct device_attribute *, char *);
  60. static ssize_t sony_acpi_store(struct device *, struct device_attribute *, const char *, size_t);
  61. struct sony_acpi_value {
  62. char *name; /* name of the entry */
  63. char **acpiget; /* names of the ACPI get function */
  64. char **acpiset; /* names of the ACPI set 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. struct device_attribute devattr; /* sysfs atribute */
  71. };
  72. #define HANDLE_NAMES(_name, _values...) \
  73. static char *snc_##_name[] = { _values, NULL }
  74. #define SONY_ACPI_VALUE(_name, _getters, _setters, _min, _max, _debug) \
  75. { \
  76. .name = __stringify(_name), \
  77. .acpiget = _getters, \
  78. .acpiset = _setters, \
  79. .min = _min, \
  80. .max = _max, \
  81. .debug = _debug, \
  82. .devattr = __ATTR(_name, 0, sony_acpi_show, sony_acpi_store), \
  83. }
  84. #define SONY_ACPI_VALUE_NULL { .name = NULL }
  85. HANDLE_NAMES(fnkey_get, "GHKE");
  86. HANDLE_NAMES(brightness_def_get, "GPBR");
  87. HANDLE_NAMES(brightness_def_set, "SPBR");
  88. HANDLE_NAMES(cdpower_get, "GCDP");
  89. HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
  90. HANDLE_NAMES(audiopower_get, "GAZP");
  91. HANDLE_NAMES(audiopower_set, "AZPW");
  92. HANDLE_NAMES(lanpower_get, "GLNP");
  93. HANDLE_NAMES(lanpower_set, "LNPW");
  94. HANDLE_NAMES(PID_get, "GPID");
  95. HANDLE_NAMES(CTR_get, "GCTR");
  96. HANDLE_NAMES(CTR_set, "SCTR");
  97. HANDLE_NAMES(PCR_get, "GPCR");
  98. HANDLE_NAMES(PCR_set, "SPCR");
  99. HANDLE_NAMES(CMI_get, "GCMI");
  100. HANDLE_NAMES(CMI_set, "SCMI");
  101. static struct sony_acpi_value sony_acpi_values[] = {
  102. SONY_ACPI_VALUE(brightness_default, snc_brightness_def_get, snc_brightness_def_set, 1, SONY_MAX_BRIGHTNESS, 0),
  103. SONY_ACPI_VALUE(fnkey, snc_fnkey_get, NULL, -1, -1, 0),
  104. SONY_ACPI_VALUE(cdpower, snc_cdpower_get, snc_cdpower_set, 0, 1, 0),
  105. SONY_ACPI_VALUE(audiopower, snc_audiopower_get, snc_audiopower_set, 0, 1, 0),
  106. SONY_ACPI_VALUE(lanpower, snc_lanpower_get, snc_lanpower_set, 0, 1, 1),
  107. /* unknown methods */
  108. SONY_ACPI_VALUE(PID, snc_PID_get, NULL, -1, -1, 1),
  109. SONY_ACPI_VALUE(CTR, snc_CTR_get, snc_CTR_set, -1, -1, 1),
  110. SONY_ACPI_VALUE(PCR, snc_PCR_get, snc_PCR_set, -1, -1, 1),
  111. SONY_ACPI_VALUE(CMI, snc_CMI_get, snc_CMI_set, -1, -1, 1),
  112. SONY_ACPI_VALUE_NULL
  113. };
  114. static acpi_handle sony_acpi_handle;
  115. static struct acpi_device *sony_acpi_acpi_device = NULL;
  116. static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
  117. {
  118. struct acpi_buffer output;
  119. union acpi_object out_obj;
  120. acpi_status status;
  121. output.length = sizeof(out_obj);
  122. output.pointer = &out_obj;
  123. status = acpi_evaluate_object(handle, name, NULL, &output);
  124. if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
  125. *result = out_obj.integer.value;
  126. return 0;
  127. }
  128. printk(LOG_PFX "acpi_callreadfunc failed\n");
  129. return -1;
  130. }
  131. static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
  132. int *result)
  133. {
  134. struct acpi_object_list params;
  135. union acpi_object in_obj;
  136. struct acpi_buffer output;
  137. union acpi_object out_obj;
  138. acpi_status status;
  139. params.count = 1;
  140. params.pointer = &in_obj;
  141. in_obj.type = ACPI_TYPE_INTEGER;
  142. in_obj.integer.value = value;
  143. output.length = sizeof(out_obj);
  144. output.pointer = &out_obj;
  145. status = acpi_evaluate_object(handle, name, &params, &output);
  146. if (status == AE_OK) {
  147. if (result != NULL) {
  148. if (out_obj.type != ACPI_TYPE_INTEGER) {
  149. printk(LOG_PFX "acpi_evaluate_object bad "
  150. "return type\n");
  151. return -1;
  152. }
  153. *result = out_obj.integer.value;
  154. }
  155. return 0;
  156. }
  157. printk(LOG_PFX "acpi_evaluate_object failed\n");
  158. return -1;
  159. }
  160. /*
  161. * Sysfs show/store common to all sony_acpi_values
  162. */
  163. static ssize_t sony_acpi_show(struct device *dev, struct device_attribute *attr,
  164. char *buffer)
  165. {
  166. int value;
  167. struct sony_acpi_value *item = container_of(attr, struct sony_acpi_value, devattr);
  168. if (!*item->acpiget)
  169. return -EIO;
  170. if (acpi_callgetfunc(sony_acpi_handle, *item->acpiget, &value) < 0)
  171. return -EIO;
  172. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  173. }
  174. static ssize_t sony_acpi_store(struct device *dev, struct device_attribute *attr,
  175. const char *buffer, size_t count)
  176. {
  177. int value;
  178. struct sony_acpi_value *item = container_of(attr, struct sony_acpi_value, devattr);
  179. if (!item->acpiset)
  180. return -EIO;
  181. if (count > 31)
  182. return -EINVAL;
  183. value = simple_strtoul(buffer, NULL, 10);
  184. if (item->min != -1 && value < item->min)
  185. return -EINVAL;
  186. if (item->max != -1 && value > item->max)
  187. return -EINVAL;
  188. if (acpi_callsetfunc(sony_acpi_handle, *item->acpiset, value, NULL) < 0)
  189. return -EIO;
  190. item->value = value;
  191. item->valid = 1;
  192. return count;
  193. }
  194. /*
  195. * Platform device
  196. */
  197. static struct platform_driver sncpf_driver = {
  198. .driver = {
  199. .name = "sony-laptop",
  200. .owner = THIS_MODULE,
  201. }
  202. };
  203. static struct platform_device *sncpf_device;
  204. static int sony_snc_pf_add(void)
  205. {
  206. acpi_handle handle;
  207. struct sony_acpi_value *item;
  208. int ret = 0;
  209. ret = platform_driver_register(&sncpf_driver);
  210. if (ret)
  211. goto out;
  212. sncpf_device = platform_device_alloc("sony-laptop", -1);
  213. if (!sncpf_device) {
  214. ret = -ENOMEM;
  215. goto out_platform_registered;
  216. }
  217. ret = platform_device_add(sncpf_device);
  218. if (ret)
  219. goto out_platform_alloced;
  220. for (item = sony_acpi_values; item->name; ++item) {
  221. if (!debug && item->debug)
  222. continue;
  223. /* find the available acpiget as described in the DSDT */
  224. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  225. if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle,
  226. *item->acpiget,
  227. &handle))) {
  228. if (debug)
  229. printk(LOG_PFX "Found %s getter: %s\n",
  230. item->name,
  231. *item->acpiget);
  232. item->devattr.attr.mode |= S_IRUGO;
  233. break;
  234. }
  235. }
  236. /* find the available acpiset as described in the DSDT */
  237. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  238. if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle,
  239. *item->acpiset,
  240. &handle))) {
  241. if (debug)
  242. printk(LOG_PFX "Found %s setter: %s\n",
  243. item->name,
  244. *item->acpiset);
  245. item->devattr.attr.mode |= S_IWUSR;
  246. break;
  247. }
  248. }
  249. if (item->devattr.attr.mode != 0) {
  250. ret = device_create_file(&sncpf_device->dev, &item->devattr);
  251. if (ret)
  252. goto out_sysfs;
  253. }
  254. }
  255. return 0;
  256. out_sysfs:
  257. for (item = sony_acpi_values; item->name; ++item) {
  258. device_remove_file(&sncpf_device->dev, &item->devattr);
  259. }
  260. platform_device_del(sncpf_device);
  261. out_platform_alloced:
  262. platform_device_put(sncpf_device);
  263. out_platform_registered:
  264. platform_driver_unregister(&sncpf_driver);
  265. out:
  266. return ret;
  267. }
  268. static void sony_snc_pf_remove(void)
  269. {
  270. struct sony_acpi_value *item;
  271. for (item = sony_acpi_values; item->name; ++item) {
  272. device_remove_file(&sncpf_device->dev, &item->devattr);
  273. }
  274. platform_device_del(sncpf_device);
  275. platform_device_put(sncpf_device);
  276. platform_driver_unregister(&sncpf_driver);
  277. }
  278. static int sony_acpi_resume(struct acpi_device *device)
  279. {
  280. struct sony_acpi_value *item;
  281. for (item = sony_acpi_values; item->name; item++) {
  282. int ret;
  283. if (!item->valid)
  284. continue;
  285. ret = acpi_callsetfunc(sony_acpi_handle, *item->acpiset,
  286. item->value, NULL);
  287. if (ret < 0) {
  288. printk("%s: %d\n", __FUNCTION__, ret);
  289. break;
  290. }
  291. }
  292. return 0;
  293. }
  294. static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
  295. {
  296. if (debug)
  297. printk(LOG_PFX "sony_acpi_notify, event: %d\n", event);
  298. acpi_bus_generate_event(sony_acpi_acpi_device, 1, event);
  299. }
  300. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  301. void *context, void **return_value)
  302. {
  303. struct acpi_namespace_node *node;
  304. union acpi_operand_object *operand;
  305. node = (struct acpi_namespace_node *) handle;
  306. operand = (union acpi_operand_object *) node->object;
  307. printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
  308. (u32) operand->method.param_count);
  309. return AE_OK;
  310. }
  311. static int sony_acpi_add(struct acpi_device *device)
  312. {
  313. acpi_status status;
  314. int result;
  315. acpi_handle handle;
  316. sony_acpi_acpi_device = device;
  317. sony_acpi_handle = device->handle;
  318. if (debug) {
  319. status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_acpi_handle,
  320. 1, sony_walk_callback, NULL, NULL);
  321. if (ACPI_FAILURE(status)) {
  322. printk(LOG_PFX "unable to walk acpi resources\n");
  323. result = -ENODEV;
  324. goto outwalk;
  325. }
  326. }
  327. status = acpi_install_notify_handler(sony_acpi_handle,
  328. ACPI_DEVICE_NOTIFY,
  329. sony_acpi_notify,
  330. NULL);
  331. if (ACPI_FAILURE(status)) {
  332. printk(LOG_PFX "unable to install notify handler\n");
  333. result = -ENODEV;
  334. goto outwalk;
  335. }
  336. if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle, "GBRT", &handle))) {
  337. sony_backlight_device = backlight_device_register("sony", NULL,
  338. NULL, &sony_backlight_properties);
  339. if (IS_ERR(sony_backlight_device)) {
  340. printk(LOG_PFX "unable to register backlight device\n");
  341. sony_backlight_device = NULL;
  342. }
  343. else
  344. sony_backlight_properties.brightness =
  345. sony_backlight_get_brightness(sony_backlight_device);
  346. }
  347. if (sony_snc_pf_add())
  348. goto outbacklight;
  349. printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully installed\n");
  350. return 0;
  351. outbacklight:
  352. if (sony_backlight_device)
  353. backlight_device_unregister(sony_backlight_device);
  354. status = acpi_remove_notify_handler(sony_acpi_handle,
  355. ACPI_DEVICE_NOTIFY,
  356. sony_acpi_notify);
  357. if (ACPI_FAILURE(status))
  358. printk(LOG_PFX "unable to remove notify handler\n");
  359. outwalk:
  360. return result;
  361. }
  362. static int sony_acpi_remove(struct acpi_device *device, int type)
  363. {
  364. acpi_status status;
  365. if (sony_backlight_device)
  366. backlight_device_unregister(sony_backlight_device);
  367. sony_acpi_acpi_device = NULL;
  368. status = acpi_remove_notify_handler(sony_acpi_handle,
  369. ACPI_DEVICE_NOTIFY,
  370. sony_acpi_notify);
  371. if (ACPI_FAILURE(status))
  372. printk(LOG_PFX "unable to remove notify handler\n");
  373. sony_snc_pf_remove();
  374. printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully removed\n");
  375. return 0;
  376. }
  377. static int sony_backlight_update_status(struct backlight_device *bd)
  378. {
  379. return acpi_callsetfunc(sony_acpi_handle, "SBRT",
  380. bd->props->brightness + 1,
  381. NULL);
  382. }
  383. static int sony_backlight_get_brightness(struct backlight_device *bd)
  384. {
  385. int value;
  386. if (acpi_callgetfunc(sony_acpi_handle, "GBRT", &value))
  387. return 0;
  388. /* brightness levels are 1-based, while backlight ones are 0-based */
  389. return value - 1;
  390. }
  391. static struct acpi_driver sony_acpi_driver = {
  392. .name = ACPI_SNC_DRIVER_NAME,
  393. .class = ACPI_SNC_CLASS,
  394. .ids = ACPI_SNC_HID,
  395. .ops = {
  396. .add = sony_acpi_add,
  397. .remove = sony_acpi_remove,
  398. .resume = sony_acpi_resume,
  399. },
  400. };
  401. static int __init sony_acpi_init(void)
  402. {
  403. return acpi_bus_register_driver(&sony_acpi_driver);
  404. }
  405. static void __exit sony_acpi_exit(void)
  406. {
  407. acpi_bus_unregister_driver(&sony_acpi_driver);
  408. }
  409. module_init(sony_acpi_init);
  410. module_exit(sony_acpi_exit);