2 * ACPI Sony Notebook Control Driver (SNC)
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
6 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
7 * which are copyrighted by their respective authors.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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/err.h>
32 #include <acpi/acpi_drivers.h>
33 #include <acpi/acpi_bus.h>
34 #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.3"
40 /* the device uses 1-based values, while the backlight subsystem uses
42 #define SONY_MAX_BRIGHTNESS 8
44 #define LOG_PFX KERN_WARNING "sony_acpi: "
46 MODULE_AUTHOR("Stelian Pop");
47 MODULE_DESCRIPTION(ACPI_SNC_DRIVER_NAME);
48 MODULE_LICENSE("GPL");
51 module_param(debug, int, 0);
52 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
53 "the development of this driver");
55 static acpi_handle sony_acpi_handle;
56 static struct proc_dir_entry *sony_acpi_dir;
57 static struct acpi_device *sony_acpi_acpi_device = NULL;
59 static int sony_backlight_update_status(struct backlight_device *bd);
60 static int sony_backlight_get_brightness(struct backlight_device *bd);
61 static struct backlight_device *sony_backlight_device;
62 static struct backlight_properties sony_backlight_properties = {
64 .update_status = sony_backlight_update_status,
65 .get_brightness = sony_backlight_get_brightness,
66 .max_brightness = SONY_MAX_BRIGHTNESS - 1,
69 static struct sony_acpi_value {
70 char *name; /* name of the entry */
71 struct proc_dir_entry *proc; /* /proc entry */
72 char *acpiget;/* name of the ACPI get function */
73 char *acpiset;/* name of the ACPI get function */
74 int min; /* minimum allowed value or -1 */
75 int max; /* maximum allowed value or -1 */
76 int value; /* current setting */
77 int valid; /* Has ever been set */
78 int debug; /* active only in debug mode ? */
79 } sony_acpi_values[] = {
85 .max = SONY_MAX_BRIGHTNESS,
89 .name = "brightness_default",
93 .max = SONY_MAX_BRIGHTNESS,
143 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
145 struct acpi_buffer output;
146 union acpi_object out_obj;
149 output.length = sizeof(out_obj);
150 output.pointer = &out_obj;
152 status = acpi_evaluate_object(handle, name, NULL, &output);
153 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
154 *result = out_obj.integer.value;
158 printk(LOG_PFX "acpi_callreadfunc failed\n");
163 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
166 struct acpi_object_list params;
167 union acpi_object in_obj;
168 struct acpi_buffer output;
169 union acpi_object out_obj;
173 params.pointer = &in_obj;
174 in_obj.type = ACPI_TYPE_INTEGER;
175 in_obj.integer.value = value;
177 output.length = sizeof(out_obj);
178 output.pointer = &out_obj;
180 status = acpi_evaluate_object(handle, name, ¶ms, &output);
181 if (status == AE_OK) {
182 if (result != NULL) {
183 if (out_obj.type != ACPI_TYPE_INTEGER) {
184 printk(LOG_PFX "acpi_evaluate_object bad "
188 *result = out_obj.integer.value;
193 printk(LOG_PFX "acpi_evaluate_object failed\n");
198 static int parse_buffer(const char __user *buffer, unsigned long count,
205 if (copy_from_user(s, buffer, count))
208 ret = simple_strtoul(s, NULL, 10);
213 static int sony_acpi_read(char* page, char** start, off_t off, int count,
214 int* eof, void *data)
216 struct sony_acpi_value *item = data;
222 if (acpi_callgetfunc(sony_acpi_handle, item->acpiget, &value) < 0)
225 return sprintf(page, "%d\n", value);
228 static int sony_acpi_write(struct file *file, const char __user *buffer,
229 unsigned long count, void *data)
231 struct sony_acpi_value *item = data;
238 if ((result = parse_buffer(buffer, count, &value)) < 0)
241 if (item->min != -1 && value < item->min)
243 if (item->max != -1 && value > item->max)
246 if (acpi_callsetfunc(sony_acpi_handle, item->acpiset, value, NULL) < 0)
253 static int sony_acpi_resume(struct acpi_device *device)
255 struct sony_acpi_value *item;
257 for (item = sony_acpi_values; item->name; item++) {
262 ret = acpi_callsetfunc(sony_acpi_handle, item->acpiset,
265 printk("%s: %d\n", __FUNCTION__, ret);
272 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
275 printk(LOG_PFX "sony_acpi_notify, event: %d\n", event);
276 acpi_bus_generate_event(sony_acpi_acpi_device, 1, event);
279 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
280 void *context, void **return_value)
282 struct acpi_namespace_node *node;
283 union acpi_operand_object *operand;
285 node = (struct acpi_namespace_node *) handle;
286 operand = (union acpi_operand_object *) node->object;
288 printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
289 (u32) operand->method.param_count);
294 static int sony_acpi_add(struct acpi_device *device)
299 struct sony_acpi_value *item;
301 sony_acpi_acpi_device = device;
303 sony_acpi_handle = device->handle;
305 acpi_driver_data(device) = NULL;
306 acpi_device_dir(device) = sony_acpi_dir;
309 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_acpi_handle,
310 1, sony_walk_callback, NULL, NULL);
311 if (ACPI_FAILURE(status)) {
312 printk(LOG_PFX "unable to walk acpi resources\n");
318 status = acpi_install_notify_handler(sony_acpi_handle,
322 if (ACPI_FAILURE(status)) {
323 printk(LOG_PFX "unable to install notify handler\n");
328 if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle, "GBRT", &handle))) {
329 sony_backlight_device = backlight_device_register("sony", NULL,
330 NULL, &sony_backlight_properties);
331 if (IS_ERR(sony_backlight_device)) {
332 printk(LOG_PFX "unable to register backlight device\n");
336 for (item = sony_acpi_values; item->name; ++item) {
337 if (!debug && item->debug)
341 ACPI_FAILURE(acpi_get_handle(sony_acpi_handle,
342 item->acpiget, &handle)))
346 ACPI_FAILURE(acpi_get_handle(sony_acpi_handle,
347 item->acpiset, &handle)))
350 item->proc = create_proc_entry(item->name, 0666,
351 acpi_device_dir(device));
353 printk(LOG_PFX "unable to create proc entry\n");
358 item->proc->read_proc = sony_acpi_read;
359 item->proc->write_proc = sony_acpi_write;
360 item->proc->data = item;
361 item->proc->owner = THIS_MODULE;
364 printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully installed\n");
369 status = acpi_remove_notify_handler(sony_acpi_handle,
372 if (ACPI_FAILURE(status))
373 printk(LOG_PFX "unable to remove notify handler\n");
375 for (item = sony_acpi_values; item->name; ++item)
377 remove_proc_entry(item->name, acpi_device_dir(device));
382 static int sony_acpi_remove(struct acpi_device *device, int type)
385 struct sony_acpi_value *item;
387 if (sony_backlight_device)
388 backlight_device_unregister(sony_backlight_device);
390 sony_acpi_acpi_device = NULL;
392 status = acpi_remove_notify_handler(sony_acpi_handle,
395 if (ACPI_FAILURE(status))
396 printk(LOG_PFX "unable to remove notify handler\n");
398 for (item = sony_acpi_values; item->name; ++item)
400 remove_proc_entry(item->name, acpi_device_dir(device));
402 printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully removed\n");
407 static int sony_backlight_update_status(struct backlight_device *bd)
409 return acpi_callsetfunc(sony_acpi_handle, "SBRT",
410 bd->props->brightness + 1,
414 static int sony_backlight_get_brightness(struct backlight_device *bd)
418 if (acpi_callgetfunc(sony_acpi_handle, "GBRT", &value))
420 /* brightness levels are 1-based, while backlight ones are 0-based */
424 static struct acpi_driver sony_acpi_driver = {
425 .name = ACPI_SNC_DRIVER_NAME,
426 .class = ACPI_SNC_CLASS,
429 .add = sony_acpi_add,
430 .remove = sony_acpi_remove,
431 .resume = sony_acpi_resume,
435 static int __init sony_acpi_init(void)
439 sony_acpi_dir = proc_mkdir("sony", acpi_root_dir);
440 if (!sony_acpi_dir) {
441 printk(LOG_PFX "unable to create /proc entry\n");
444 sony_acpi_dir->owner = THIS_MODULE;
446 result = acpi_bus_register_driver(&sony_acpi_driver);
448 remove_proc_entry("sony", acpi_root_dir);
455 static void __exit sony_acpi_exit(void)
457 acpi_bus_unregister_driver(&sony_acpi_driver);
458 remove_proc_entry("sony", acpi_root_dir);
461 module_init(sony_acpi_init);
462 module_exit(sony_acpi_exit);