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 SONY_NC_CLASS "sony"
39 #define SONY_NC_HID "SNY5001"
40 #define SONY_NC_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(SONY_NC_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 /*********** Platform Device ***********/
59 static atomic_t sony_pf_users = ATOMIC_INIT(0);
60 static struct platform_driver sony_pf_driver = {
62 .name = "sony-laptop",
66 static struct platform_device *sony_pf_device;
68 static int sony_pf_add(void)
72 /* don't run again if already initialized */
73 if (atomic_add_return(1, &sony_pf_users) > 1)
76 ret = platform_driver_register(&sony_pf_driver);
80 sony_pf_device = platform_device_alloc("sony-laptop", -1);
81 if (!sony_pf_device) {
83 goto out_platform_registered;
86 ret = platform_device_add(sony_pf_device);
88 goto out_platform_alloced;
93 platform_device_put(sony_pf_device);
94 sony_pf_device = NULL;
95 out_platform_registered:
96 platform_driver_unregister(&sony_pf_driver);
98 atomic_dec(&sony_pf_users);
102 static void sony_pf_remove(void)
104 /* deregister only after the last user has gone */
105 if (!atomic_dec_and_test(&sony_pf_users))
108 platform_device_del(sony_pf_device);
109 platform_device_put(sony_pf_device);
110 platform_driver_unregister(&sony_pf_driver);
113 /*********** SNC (SNY5001) Device ***********/
115 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
117 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
118 const char *, size_t);
119 static int boolean_validate(const int, const int);
120 static int brightness_default_validate(const int, const int);
122 #define SNC_VALIDATE_IN 0
123 #define SNC_VALIDATE_OUT 1
125 struct sony_nc_value {
126 char *name; /* name of the entry */
127 char **acpiget; /* names of the ACPI get function */
128 char **acpiset; /* names of the ACPI set function */
129 int (*validate)(const int, const int); /* input/output validation */
130 int value; /* current setting */
131 int valid; /* Has ever been set */
132 int debug; /* active only in debug mode ? */
133 struct device_attribute devattr; /* sysfs atribute */
136 #define SNC_HANDLE_NAMES(_name, _values...) \
137 static char *snc_##_name[] = { _values, NULL }
139 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
141 .name = __stringify(_name), \
142 .acpiget = _getters, \
143 .acpiset = _setters, \
144 .validate = _validate, \
146 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
149 #define SNC_HANDLE_NULL { .name = NULL }
151 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
153 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
154 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
156 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
157 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
159 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
160 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
162 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
163 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
165 SNC_HANDLE_NAMES(PID_get, "GPID");
167 SNC_HANDLE_NAMES(CTR_get, "GCTR");
168 SNC_HANDLE_NAMES(CTR_set, "SCTR");
170 SNC_HANDLE_NAMES(PCR_get, "GPCR");
171 SNC_HANDLE_NAMES(PCR_set, "SPCR");
173 SNC_HANDLE_NAMES(CMI_get, "GCMI");
174 SNC_HANDLE_NAMES(CMI_set, "SCMI");
176 static struct sony_nc_value sony_nc_values[] = {
177 SNC_HANDLE(brightness_default, snc_brightness_def_get,
178 snc_brightness_def_set, brightness_default_validate, 0),
179 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
180 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
181 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
182 boolean_validate, 0),
183 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
184 boolean_validate, 1),
185 /* unknown methods */
186 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
187 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
188 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
189 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
193 static acpi_handle sony_nc_acpi_handle;
194 static struct acpi_device *sony_nc_acpi_device = NULL;
197 * acpi_evaluate_object wrappers
199 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
201 struct acpi_buffer output;
202 union acpi_object out_obj;
205 output.length = sizeof(out_obj);
206 output.pointer = &out_obj;
208 status = acpi_evaluate_object(handle, name, NULL, &output);
209 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
210 *result = out_obj.integer.value;
214 printk(LOG_PFX "acpi_callreadfunc failed\n");
219 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
222 struct acpi_object_list params;
223 union acpi_object in_obj;
224 struct acpi_buffer output;
225 union acpi_object out_obj;
229 params.pointer = &in_obj;
230 in_obj.type = ACPI_TYPE_INTEGER;
231 in_obj.integer.value = value;
233 output.length = sizeof(out_obj);
234 output.pointer = &out_obj;
236 status = acpi_evaluate_object(handle, name, ¶ms, &output);
237 if (status == AE_OK) {
238 if (result != NULL) {
239 if (out_obj.type != ACPI_TYPE_INTEGER) {
240 printk(LOG_PFX "acpi_evaluate_object bad "
244 *result = out_obj.integer.value;
249 printk(LOG_PFX "acpi_evaluate_object failed\n");
255 * sony_nc_values input/output validate functions
258 /* brightness_default_validate:
260 * manipulate input output values to keep consistency with the
261 * backlight framework for which brightness values are 0-based.
263 static int brightness_default_validate(const int direction, const int value)
266 case SNC_VALIDATE_OUT:
268 case SNC_VALIDATE_IN:
269 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
277 * on input validate boolean values 0/1, on output just pass the
280 static int boolean_validate(const int direction, const int value)
282 if (direction == SNC_VALIDATE_IN) {
283 if (value != 0 && value != 1)
290 * Sysfs show/store common to all sony_nc_values
292 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
296 struct sony_nc_value *item =
297 container_of(attr, struct sony_nc_value, devattr);
302 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
306 value = item->validate(SNC_VALIDATE_OUT, value);
308 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
311 static ssize_t sony_nc_sysfs_store(struct device *dev,
312 struct device_attribute *attr,
313 const char *buffer, size_t count)
316 struct sony_nc_value *item =
317 container_of(attr, struct sony_nc_value, devattr);
325 value = simple_strtoul(buffer, NULL, 10);
328 value = item->validate(SNC_VALIDATE_IN, value);
333 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
344 static int sony_backlight_update_status(struct backlight_device *bd)
346 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
347 bd->props.brightness + 1, NULL);
350 static int sony_backlight_get_brightness(struct backlight_device *bd)
354 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
356 /* brightness levels are 1-based, while backlight ones are 0-based */
360 static struct backlight_device *sony_backlight_device;
361 static struct backlight_ops sony_backlight_ops = {
362 .update_status = sony_backlight_update_status,
363 .get_brightness = sony_backlight_get_brightness,
369 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
372 printk(LOG_PFX "sony_acpi_notify, event: %d\n", event);
373 acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
376 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
377 void *context, void **return_value)
379 struct acpi_namespace_node *node;
380 union acpi_operand_object *operand;
382 node = (struct acpi_namespace_node *)handle;
383 operand = (union acpi_operand_object *)node->object;
385 printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
386 (u32) operand->method.param_count);
394 static int sony_nc_resume(struct acpi_device *device)
396 struct sony_nc_value *item;
398 for (item = sony_nc_values; item->name; item++) {
403 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
406 printk("%s: %d\n", __FUNCTION__, ret);
413 static int sony_nc_add(struct acpi_device *device)
418 struct sony_nc_value *item;
420 sony_nc_acpi_device = device;
422 sony_nc_acpi_handle = device->handle;
425 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
426 1, sony_walk_callback, NULL, NULL);
427 if (ACPI_FAILURE(status)) {
428 printk(LOG_PFX "unable to walk acpi resources\n");
434 status = acpi_install_notify_handler(sony_nc_acpi_handle,
436 sony_acpi_notify, NULL);
437 if (ACPI_FAILURE(status)) {
438 printk(LOG_PFX "unable to install notify handler\n");
443 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
444 sony_backlight_device = backlight_device_register("sony", NULL,
446 &sony_backlight_ops);
448 if (IS_ERR(sony_backlight_device)) {
449 printk(LOG_PFX "unable to register backlight device\n");
450 sony_backlight_device = NULL;
452 sony_backlight_device->props.brightness =
453 sony_backlight_get_brightness
454 (sony_backlight_device);
455 sony_backlight_device->props.max_brightness =
456 SONY_MAX_BRIGHTNESS - 1;
464 /* create sony_pf sysfs attributes related to the SNC device */
465 for (item = sony_nc_values; item->name; ++item) {
467 if (!debug && item->debug)
470 /* find the available acpiget as described in the DSDT */
471 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
472 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
476 printk(LOG_PFX "Found %s getter: %s\n",
477 item->name, *item->acpiget);
478 item->devattr.attr.mode |= S_IRUGO;
483 /* find the available acpiset as described in the DSDT */
484 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
485 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
489 printk(LOG_PFX "Found %s setter: %s\n",
490 item->name, *item->acpiset);
491 item->devattr.attr.mode |= S_IWUSR;
496 if (item->devattr.attr.mode != 0) {
498 device_create_file(&sony_pf_device->dev,
505 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n");
510 for (item = sony_nc_values; item->name; ++item) {
511 device_remove_file(&sony_pf_device->dev, &item->devattr);
515 if (sony_backlight_device)
516 backlight_device_unregister(sony_backlight_device);
518 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
521 if (ACPI_FAILURE(status))
522 printk(LOG_PFX "unable to remove notify handler\n");
527 static int sony_nc_remove(struct acpi_device *device, int type)
530 struct sony_nc_value *item;
532 if (sony_backlight_device)
533 backlight_device_unregister(sony_backlight_device);
535 sony_nc_acpi_device = NULL;
537 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
540 if (ACPI_FAILURE(status))
541 printk(LOG_PFX "unable to remove notify handler\n");
543 for (item = sony_nc_values; item->name; ++item) {
544 device_remove_file(&sony_pf_device->dev, &item->devattr);
549 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n");
554 static struct acpi_driver sony_nc_driver = {
555 .name = SONY_NC_DRIVER_NAME,
556 .class = SONY_NC_CLASS,
560 .remove = sony_nc_remove,
561 .resume = sony_nc_resume,
565 static int __init sony_laptop_init(void)
567 return acpi_bus_register_driver(&sony_nc_driver);
570 static void __exit sony_laptop_exit(void)
572 acpi_bus_unregister_driver(&sony_nc_driver);
575 module_init(sony_laptop_init);
576 module_exit(sony_laptop_exit);