sony-laptop.c 14 KB

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