2 * ACPI Sony Notebook Control Driver (SNC)
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5 * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
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.
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.
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.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/backlight.h>
32 #include <linux/platform_device.h>
33 #include <linux/err.h>
34 #include <acpi/acpi_drivers.h>
35 #include <acpi/acpi_bus.h>
36 #include <asm/uaccess.h>
38 #define ACPI_SNC_CLASS "sony"
39 #define ACPI_SNC_HID "SNY5001"
40 #define ACPI_SNC_DRIVER_NAME "ACPI Sony Notebook Control Driver v0.4"
42 /* the device uses 1-based values, while the backlight subsystem uses
44 #define SONY_MAX_BRIGHTNESS 8
46 #define LOG_PFX KERN_WARNING "sony-laptop: "
48 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
49 MODULE_DESCRIPTION(ACPI_SNC_DRIVER_NAME
);
50 MODULE_LICENSE("GPL");
53 module_param(debug
, int, 0);
54 MODULE_PARM_DESC(debug
, "set this to 1 (and RTFM) if you want to help "
55 "the development of this driver");
57 static ssize_t
sony_acpi_show(struct device
*, struct device_attribute
*,
59 static ssize_t
sony_acpi_store(struct device
*, struct device_attribute
*,
60 const char *, size_t);
61 static int boolean_validate(const int, const int);
62 static int brightness_default_validate(const int, const int);
64 #define SNC_VALIDATE_IN 0
65 #define SNC_VALIDATE_OUT 1
67 struct sony_acpi_value
{
68 char *name
; /* name of the entry */
69 char **acpiget
; /* names of the ACPI get function */
70 char **acpiset
; /* names of the ACPI set function */
71 int (*validate
)(const int, const int); /* input/output validation */
72 int value
; /* current setting */
73 int valid
; /* Has ever been set */
74 int debug
; /* active only in debug mode ? */
75 struct device_attribute devattr
; /* sysfs atribute */
78 #define HANDLE_NAMES(_name, _values...) \
79 static char *snc_##_name[] = { _values, NULL }
81 #define SONY_ACPI_VALUE(_name, _getters, _setters, _validate, _debug) \
83 .name = __stringify(_name), \
84 .acpiget = _getters, \
85 .acpiset = _setters, \
86 .validate = _validate, \
88 .devattr = __ATTR(_name, 0, sony_acpi_show, sony_acpi_store), \
91 #define SONY_ACPI_VALUE_NULL { .name = NULL }
93 HANDLE_NAMES(fnkey_get
, "GHKE");
95 HANDLE_NAMES(brightness_def_get
, "GPBR");
96 HANDLE_NAMES(brightness_def_set
, "SPBR");
98 HANDLE_NAMES(cdpower_get
, "GCDP");
99 HANDLE_NAMES(cdpower_set
, "SCDP", "CDPW");
101 HANDLE_NAMES(audiopower_get
, "GAZP");
102 HANDLE_NAMES(audiopower_set
, "AZPW");
104 HANDLE_NAMES(lanpower_get
, "GLNP");
105 HANDLE_NAMES(lanpower_set
, "LNPW");
107 HANDLE_NAMES(PID_get
, "GPID");
109 HANDLE_NAMES(CTR_get
, "GCTR");
110 HANDLE_NAMES(CTR_set
, "SCTR");
112 HANDLE_NAMES(PCR_get
, "GPCR");
113 HANDLE_NAMES(PCR_set
, "SPCR");
115 HANDLE_NAMES(CMI_get
, "GCMI");
116 HANDLE_NAMES(CMI_set
, "SCMI");
118 static struct sony_acpi_value sony_acpi_values
[] = {
119 SONY_ACPI_VALUE(brightness_default
, snc_brightness_def_get
,
120 snc_brightness_def_set
, brightness_default_validate
, 0),
121 SONY_ACPI_VALUE(fnkey
, snc_fnkey_get
, NULL
, NULL
, 0),
122 SONY_ACPI_VALUE(cdpower
, snc_cdpower_get
, snc_cdpower_set
, boolean_validate
, 0),
123 SONY_ACPI_VALUE(audiopower
, snc_audiopower_get
, snc_audiopower_set
,
124 boolean_validate
, 0),
125 SONY_ACPI_VALUE(lanpower
, snc_lanpower_get
, snc_lanpower_set
,
126 boolean_validate
, 1),
127 /* unknown methods */
128 SONY_ACPI_VALUE(PID
, snc_PID_get
, NULL
, NULL
, 1),
129 SONY_ACPI_VALUE(CTR
, snc_CTR_get
, snc_CTR_set
, NULL
, 1),
130 SONY_ACPI_VALUE(PCR
, snc_PCR_get
, snc_PCR_set
, NULL
, 1),
131 SONY_ACPI_VALUE(CMI
, snc_CMI_get
, snc_CMI_set
, NULL
, 1),
135 static acpi_handle sony_acpi_handle
;
136 static struct acpi_device
*sony_acpi_acpi_device
= NULL
;
139 * acpi_evaluate_object wrappers
141 static int acpi_callgetfunc(acpi_handle handle
, char *name
, int *result
)
143 struct acpi_buffer output
;
144 union acpi_object out_obj
;
147 output
.length
= sizeof(out_obj
);
148 output
.pointer
= &out_obj
;
150 status
= acpi_evaluate_object(handle
, name
, NULL
, &output
);
151 if ((status
== AE_OK
) && (out_obj
.type
== ACPI_TYPE_INTEGER
)) {
152 *result
= out_obj
.integer
.value
;
156 printk(LOG_PFX
"acpi_callreadfunc failed\n");
161 static int acpi_callsetfunc(acpi_handle handle
, char *name
, int value
,
164 struct acpi_object_list params
;
165 union acpi_object in_obj
;
166 struct acpi_buffer output
;
167 union acpi_object out_obj
;
171 params
.pointer
= &in_obj
;
172 in_obj
.type
= ACPI_TYPE_INTEGER
;
173 in_obj
.integer
.value
= value
;
175 output
.length
= sizeof(out_obj
);
176 output
.pointer
= &out_obj
;
178 status
= acpi_evaluate_object(handle
, name
, ¶ms
, &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 "
186 *result
= out_obj
.integer
.value
;
191 printk(LOG_PFX
"acpi_evaluate_object failed\n");
197 * sony_acpi_values input/output validate functions
200 /* brightness_default_validate:
202 * manipulate input output values to keep consistency with the
203 * backlight framework for which brightness values are 0-based.
205 static int brightness_default_validate(const int direction
, const int value
)
208 case SNC_VALIDATE_OUT
:
210 case SNC_VALIDATE_IN
:
211 if (value
>= 0 && value
< SONY_MAX_BRIGHTNESS
)
219 * on input validate boolean values 0/1, on output just pass the
222 static int boolean_validate(const int direction
, const int value
)
224 if (direction
== SNC_VALIDATE_IN
) {
225 if (value
!= 0 && value
!= 1)
232 * Sysfs show/store common to all sony_acpi_values
234 static ssize_t
sony_acpi_show(struct device
*dev
, struct device_attribute
*attr
,
238 struct sony_acpi_value
*item
=
239 container_of(attr
, struct sony_acpi_value
, devattr
);
244 if (acpi_callgetfunc(sony_acpi_handle
, *item
->acpiget
, &value
) < 0)
248 value
= item
->validate(SNC_VALIDATE_OUT
, value
);
250 return snprintf(buffer
, PAGE_SIZE
, "%d\n", value
);
253 static ssize_t
sony_acpi_store(struct device
*dev
,
254 struct device_attribute
*attr
,
255 const char *buffer
, size_t count
)
258 struct sony_acpi_value
*item
=
259 container_of(attr
, struct sony_acpi_value
, devattr
);
267 value
= simple_strtoul(buffer
, NULL
, 10);
270 value
= item
->validate(SNC_VALIDATE_IN
, value
);
275 if (acpi_callsetfunc(sony_acpi_handle
, *item
->acpiset
, value
, NULL
) < 0)
285 static struct platform_driver sncpf_driver
= {
287 .name
= "sony-laptop",
288 .owner
= THIS_MODULE
,
291 static struct platform_device
*sncpf_device
;
293 static int sony_snc_pf_add(void)
296 struct sony_acpi_value
*item
;
299 ret
= platform_driver_register(&sncpf_driver
);
303 sncpf_device
= platform_device_alloc("sony-laptop", -1);
306 goto out_platform_registered
;
309 ret
= platform_device_add(sncpf_device
);
311 goto out_platform_alloced
;
313 for (item
= sony_acpi_values
; item
->name
; ++item
) {
315 if (!debug
&& item
->debug
)
318 /* find the available acpiget as described in the DSDT */
319 for (; item
->acpiget
&& *item
->acpiget
; ++item
->acpiget
) {
320 if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle
,
324 printk(LOG_PFX
"Found %s getter: %s\n",
325 item
->name
, *item
->acpiget
);
326 item
->devattr
.attr
.mode
|= S_IRUGO
;
331 /* find the available acpiset as described in the DSDT */
332 for (; item
->acpiset
&& *item
->acpiset
; ++item
->acpiset
) {
333 if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle
,
337 printk(LOG_PFX
"Found %s setter: %s\n",
338 item
->name
, *item
->acpiset
);
339 item
->devattr
.attr
.mode
|= S_IWUSR
;
344 if (item
->devattr
.attr
.mode
!= 0) {
346 device_create_file(&sncpf_device
->dev
,
356 for (item
= sony_acpi_values
; item
->name
; ++item
) {
357 device_remove_file(&sncpf_device
->dev
, &item
->devattr
);
359 platform_device_del(sncpf_device
);
360 out_platform_alloced
:
361 platform_device_put(sncpf_device
);
362 out_platform_registered
:
363 platform_driver_unregister(&sncpf_driver
);
368 static void sony_snc_pf_remove(void)
370 struct sony_acpi_value
*item
;
372 for (item
= sony_acpi_values
; item
->name
; ++item
) {
373 device_remove_file(&sncpf_device
->dev
, &item
->devattr
);
376 platform_device_del(sncpf_device
);
377 platform_device_put(sncpf_device
);
378 platform_driver_unregister(&sncpf_driver
);
384 static int sony_backlight_update_status(struct backlight_device
*bd
)
386 return acpi_callsetfunc(sony_acpi_handle
, "SBRT",
387 bd
->props
.brightness
+ 1, NULL
);
390 static int sony_backlight_get_brightness(struct backlight_device
*bd
)
394 if (acpi_callgetfunc(sony_acpi_handle
, "GBRT", &value
))
396 /* brightness levels are 1-based, while backlight ones are 0-based */
400 static struct backlight_device
*sony_backlight_device
;
401 static struct backlight_ops sony_backlight_ops
= {
402 .update_status
= sony_backlight_update_status
,
403 .get_brightness
= sony_backlight_get_brightness
,
409 static void sony_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
412 printk(LOG_PFX
"sony_acpi_notify, event: %d\n", event
);
413 acpi_bus_generate_event(sony_acpi_acpi_device
, 1, event
);
416 static acpi_status
sony_walk_callback(acpi_handle handle
, u32 level
,
417 void *context
, void **return_value
)
419 struct acpi_namespace_node
*node
;
420 union acpi_operand_object
*operand
;
422 node
= (struct acpi_namespace_node
*)handle
;
423 operand
= (union acpi_operand_object
*)node
->object
;
425 printk(LOG_PFX
"method: name: %4.4s, args %X\n", node
->name
.ascii
,
426 (u32
) operand
->method
.param_count
);
434 static int sony_acpi_resume(struct acpi_device
*device
)
436 struct sony_acpi_value
*item
;
438 for (item
= sony_acpi_values
; item
->name
; item
++) {
443 ret
= acpi_callsetfunc(sony_acpi_handle
, *item
->acpiset
,
446 printk("%s: %d\n", __FUNCTION__
, ret
);
453 static int sony_acpi_add(struct acpi_device
*device
)
459 sony_acpi_acpi_device
= device
;
461 sony_acpi_handle
= device
->handle
;
464 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, sony_acpi_handle
,
465 1, sony_walk_callback
, NULL
, NULL
);
466 if (ACPI_FAILURE(status
)) {
467 printk(LOG_PFX
"unable to walk acpi resources\n");
473 status
= acpi_install_notify_handler(sony_acpi_handle
,
475 sony_acpi_notify
, NULL
);
476 if (ACPI_FAILURE(status
)) {
477 printk(LOG_PFX
"unable to install notify handler\n");
482 if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle
, "GBRT", &handle
))) {
483 sony_backlight_device
= backlight_device_register("sony", NULL
,
485 &sony_backlight_ops
);
487 if (IS_ERR(sony_backlight_device
)) {
488 printk(LOG_PFX
"unable to register backlight device\n");
489 sony_backlight_device
= NULL
;
491 sony_backlight_device
->props
.brightness
=
492 sony_backlight_get_brightness
493 (sony_backlight_device
);
494 sony_backlight_device
->props
.max_brightness
=
495 SONY_MAX_BRIGHTNESS
- 1;
500 if (sony_snc_pf_add())
503 printk(KERN_INFO ACPI_SNC_DRIVER_NAME
" successfully installed\n");
508 if (sony_backlight_device
)
509 backlight_device_unregister(sony_backlight_device
);
511 status
= acpi_remove_notify_handler(sony_acpi_handle
,
514 if (ACPI_FAILURE(status
))
515 printk(LOG_PFX
"unable to remove notify handler\n");
520 static int sony_acpi_remove(struct acpi_device
*device
, int type
)
524 if (sony_backlight_device
)
525 backlight_device_unregister(sony_backlight_device
);
527 sony_acpi_acpi_device
= NULL
;
529 status
= acpi_remove_notify_handler(sony_acpi_handle
,
532 if (ACPI_FAILURE(status
))
533 printk(LOG_PFX
"unable to remove notify handler\n");
535 sony_snc_pf_remove();
537 printk(KERN_INFO ACPI_SNC_DRIVER_NAME
" successfully removed\n");
542 static struct acpi_driver sony_acpi_driver
= {
543 .name
= ACPI_SNC_DRIVER_NAME
,
544 .class = ACPI_SNC_CLASS
,
547 .add
= sony_acpi_add
,
548 .remove
= sony_acpi_remove
,
549 .resume
= sony_acpi_resume
,
553 static int __init
sony_acpi_init(void)
555 return acpi_bus_register_driver(&sony_acpi_driver
);
558 static void __exit
sony_acpi_exit(void)
560 acpi_bus_unregister_driver(&sony_acpi_driver
);
563 module_init(sony_acpi_init
);
564 module_exit(sony_acpi_exit
);