sony-laptop.c 13 KB

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