sony-laptop.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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_properties sony_backlight_properties = {
  334. .owner = THIS_MODULE,
  335. .update_status = sony_backlight_update_status,
  336. .get_brightness = sony_backlight_get_brightness,
  337. .max_brightness = SONY_MAX_BRIGHTNESS - 1,
  338. };
  339. /*
  340. * ACPI callbacks
  341. */
  342. static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
  343. {
  344. if (debug)
  345. printk(LOG_PFX "sony_acpi_notify, event: %d\n", event);
  346. acpi_bus_generate_event(sony_acpi_acpi_device, 1, event);
  347. }
  348. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  349. void *context, void **return_value)
  350. {
  351. struct acpi_namespace_node *node;
  352. union acpi_operand_object *operand;
  353. node = (struct acpi_namespace_node *)handle;
  354. operand = (union acpi_operand_object *)node->object;
  355. printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
  356. (u32) operand->method.param_count);
  357. return AE_OK;
  358. }
  359. /*
  360. * ACPI device
  361. */
  362. static int sony_acpi_resume(struct acpi_device *device)
  363. {
  364. struct sony_acpi_value *item;
  365. for (item = sony_acpi_values; item->name; item++) {
  366. int ret;
  367. if (!item->valid)
  368. continue;
  369. ret = acpi_callsetfunc(sony_acpi_handle, *item->acpiset,
  370. item->value, NULL);
  371. if (ret < 0) {
  372. printk("%s: %d\n", __FUNCTION__, ret);
  373. break;
  374. }
  375. }
  376. return 0;
  377. }
  378. static int sony_acpi_add(struct acpi_device *device)
  379. {
  380. acpi_status status;
  381. int result;
  382. acpi_handle handle;
  383. sony_acpi_acpi_device = device;
  384. sony_acpi_handle = device->handle;
  385. if (debug) {
  386. status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_acpi_handle,
  387. 1, sony_walk_callback, NULL, NULL);
  388. if (ACPI_FAILURE(status)) {
  389. printk(LOG_PFX "unable to walk acpi resources\n");
  390. result = -ENODEV;
  391. goto outwalk;
  392. }
  393. }
  394. status = acpi_install_notify_handler(sony_acpi_handle,
  395. ACPI_DEVICE_NOTIFY,
  396. sony_acpi_notify, NULL);
  397. if (ACPI_FAILURE(status)) {
  398. printk(LOG_PFX "unable to install notify handler\n");
  399. result = -ENODEV;
  400. goto outwalk;
  401. }
  402. if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle, "GBRT", &handle))) {
  403. sony_backlight_device = backlight_device_register("sony", NULL,
  404. NULL,
  405. &sony_backlight_properties);
  406. if (IS_ERR(sony_backlight_device)) {
  407. printk(LOG_PFX "unable to register backlight device\n");
  408. sony_backlight_device = NULL;
  409. } else
  410. sony_backlight_properties.brightness =
  411. sony_backlight_get_brightness
  412. (sony_backlight_device);
  413. }
  414. if (sony_snc_pf_add())
  415. goto outbacklight;
  416. printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully installed\n");
  417. return 0;
  418. outbacklight:
  419. if (sony_backlight_device)
  420. backlight_device_unregister(sony_backlight_device);
  421. status = acpi_remove_notify_handler(sony_acpi_handle,
  422. ACPI_DEVICE_NOTIFY,
  423. sony_acpi_notify);
  424. if (ACPI_FAILURE(status))
  425. printk(LOG_PFX "unable to remove notify handler\n");
  426. outwalk:
  427. return result;
  428. }
  429. static int sony_acpi_remove(struct acpi_device *device, int type)
  430. {
  431. acpi_status status;
  432. if (sony_backlight_device)
  433. backlight_device_unregister(sony_backlight_device);
  434. sony_acpi_acpi_device = NULL;
  435. status = acpi_remove_notify_handler(sony_acpi_handle,
  436. ACPI_DEVICE_NOTIFY,
  437. sony_acpi_notify);
  438. if (ACPI_FAILURE(status))
  439. printk(LOG_PFX "unable to remove notify handler\n");
  440. sony_snc_pf_remove();
  441. printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully removed\n");
  442. return 0;
  443. }
  444. static struct acpi_driver sony_acpi_driver = {
  445. .name = ACPI_SNC_DRIVER_NAME,
  446. .class = ACPI_SNC_CLASS,
  447. .ids = ACPI_SNC_HID,
  448. .ops = {
  449. .add = sony_acpi_add,
  450. .remove = sony_acpi_remove,
  451. .resume = sony_acpi_resume,
  452. },
  453. };
  454. static int __init sony_acpi_init(void)
  455. {
  456. return acpi_bus_register_driver(&sony_acpi_driver);
  457. }
  458. static void __exit sony_acpi_exit(void)
  459. {
  460. acpi_bus_unregister_driver(&sony_acpi_driver);
  461. }
  462. module_init(sony_acpi_init);
  463. module_exit(sony_acpi_exit);