blob: 69122ad778f586584729202206068038dcbbd069 [file] [log] [blame]
Stelian Pop7f09c432007-01-13 23:04:31 +01001/*
2 * ACPI Sony Notebook Control Driver (SNC)
3 *
4 * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5 *
6 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
7 * which are copyrighted by their respective authors.
8 *
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.
13 *
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.
18 *
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.
22 *
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>
Alessandro Guido50f62af2007-01-13 23:04:34 +010030#include <linux/backlight.h>
31#include <linux/err.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010032#include <acpi/acpi_drivers.h>
33#include <acpi/acpi_bus.h>
34#include <asm/uaccess.h>
35
36#define ACPI_SNC_CLASS "sony"
37#define ACPI_SNC_HID "SNY5001"
Alessandro Guido50f62af2007-01-13 23:04:34 +010038#define ACPI_SNC_DRIVER_NAME "ACPI Sony Notebook Control Driver v0.3"
39
40/* the device uses 1-based values, while the backlight subsystem uses
41 0-based values */
42#define SONY_MAX_BRIGHTNESS 8
Stelian Pop7f09c432007-01-13 23:04:31 +010043
44#define LOG_PFX KERN_WARNING "sony_acpi: "
45
46MODULE_AUTHOR("Stelian Pop");
47MODULE_DESCRIPTION(ACPI_SNC_DRIVER_NAME);
48MODULE_LICENSE("GPL");
49
50static int debug;
51module_param(debug, int, 0);
52MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
53 "the development of this driver");
54
Stelian Pop7f09c432007-01-13 23:04:31 +010055static acpi_handle sony_acpi_handle;
56static struct proc_dir_entry *sony_acpi_dir;
Stelian Popc5611622007-01-13 23:04:37 +010057static struct acpi_device *sony_acpi_acpi_device = NULL;
Stelian Pop7f09c432007-01-13 23:04:31 +010058
Alessandro Guido50f62af2007-01-13 23:04:34 +010059static int sony_backlight_update_status(struct backlight_device *bd);
60static int sony_backlight_get_brightness(struct backlight_device *bd);
61static struct backlight_device *sony_backlight_device;
62static struct backlight_properties sony_backlight_properties = {
63 .owner = THIS_MODULE,
64 .update_status = sony_backlight_update_status,
65 .get_brightness = sony_backlight_get_brightness,
66 .max_brightness = SONY_MAX_BRIGHTNESS - 1,
67};
68
Stelian Pop7f09c432007-01-13 23:04:31 +010069static 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 */
Andrew Morton3f4f4612007-01-13 23:04:32 +010076 int value; /* current setting */
77 int valid; /* Has ever been set */
Stelian Pop7f09c432007-01-13 23:04:31 +010078 int debug; /* active only in debug mode ? */
79} sony_acpi_values[] = {
80 {
Alessandro Guido243e8b12007-01-13 23:04:35 +010081 .name = "brightness",
82 .acpiget = "GBRT",
83 .acpiset = "SBRT",
84 .min = 1,
85 .max = SONY_MAX_BRIGHTNESS,
86 .debug = 0,
87 },
88 {
Stelian Pop7f09c432007-01-13 23:04:31 +010089 .name = "brightness_default",
90 .acpiget = "GPBR",
91 .acpiset = "SPBR",
92 .min = 1,
Alessandro Guido50f62af2007-01-13 23:04:34 +010093 .max = SONY_MAX_BRIGHTNESS,
Stelian Pop7f09c432007-01-13 23:04:31 +010094 .debug = 0,
95 },
96 {
97 .name = "fnkey",
98 .acpiget = "GHKE",
99 .debug = 0,
100 },
101 {
102 .name = "cdpower",
103 .acpiget = "GCDP",
104 .acpiset = "SCDP",
105 .min = -1,
106 .max = -1,
107 .debug = 0,
108 },
109 {
110 .name = "PID",
111 .acpiget = "GPID",
112 .debug = 1,
113 },
114 {
115 .name = "CTR",
116 .acpiget = "GCTR",
117 .acpiset = "SCTR",
118 .min = -1,
119 .max = -1,
120 .debug = 1,
121 },
122 {
123 .name = "PCR",
124 .acpiget = "GPCR",
125 .acpiset = "SPCR",
126 .min = -1,
127 .max = -1,
128 .debug = 1,
129 },
130 {
131 .name = "CMI",
132 .acpiget = "GCMI",
133 .acpiset = "SCMI",
134 .min = -1,
135 .max = -1,
136 .debug = 1,
137 },
138 {
139 .name = NULL,
140 }
141};
142
143static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
144{
145 struct acpi_buffer output;
146 union acpi_object out_obj;
147 acpi_status status;
148
149 output.length = sizeof(out_obj);
150 output.pointer = &out_obj;
151
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;
155 return 0;
156 }
157
158 printk(LOG_PFX "acpi_callreadfunc failed\n");
159
160 return -1;
161}
162
163static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
164 int *result)
165{
166 struct acpi_object_list params;
167 union acpi_object in_obj;
168 struct acpi_buffer output;
169 union acpi_object out_obj;
170 acpi_status status;
171
172 params.count = 1;
173 params.pointer = &in_obj;
174 in_obj.type = ACPI_TYPE_INTEGER;
175 in_obj.integer.value = value;
176
177 output.length = sizeof(out_obj);
178 output.pointer = &out_obj;
179
180 status = acpi_evaluate_object(handle, name, &params, &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 "
185 "return type\n");
186 return -1;
187 }
188 *result = out_obj.integer.value;
189 }
190 return 0;
191 }
192
193 printk(LOG_PFX "acpi_evaluate_object failed\n");
194
195 return -1;
196}
197
198static int parse_buffer(const char __user *buffer, unsigned long count,
199 int *val) {
200 char s[32];
201 int ret;
202
203 if (count > 31)
204 return -EINVAL;
205 if (copy_from_user(s, buffer, count))
206 return -EFAULT;
207 s[count] = '\0';
208 ret = simple_strtoul(s, NULL, 10);
209 *val = ret;
210 return 0;
211}
212
213static int sony_acpi_read(char* page, char** start, off_t off, int count,
214 int* eof, void *data)
215{
216 struct sony_acpi_value *item = data;
217 int value;
218
219 if (!item->acpiget)
220 return -EIO;
221
222 if (acpi_callgetfunc(sony_acpi_handle, item->acpiget, &value) < 0)
223 return -EIO;
224
225 return sprintf(page, "%d\n", value);
226}
227
228static int sony_acpi_write(struct file *file, const char __user *buffer,
229 unsigned long count, void *data)
230{
231 struct sony_acpi_value *item = data;
232 int result;
233 int value;
234
235 if (!item->acpiset)
236 return -EIO;
237
238 if ((result = parse_buffer(buffer, count, &value)) < 0)
239 return result;
240
241 if (item->min != -1 && value < item->min)
242 return -EINVAL;
243 if (item->max != -1 && value > item->max)
244 return -EINVAL;
245
246 if (acpi_callsetfunc(sony_acpi_handle, item->acpiset, value, NULL) < 0)
247 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100248 item->value = value;
249 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100250 return count;
251}
252
Andrew Mortonfac35062007-01-13 23:04:33 +0100253static int sony_acpi_resume(struct acpi_device *device)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100254{
255 struct sony_acpi_value *item;
256
257 for (item = sony_acpi_values; item->name; item++) {
258 int ret;
259
260 if (!item->valid)
261 continue;
262 ret = acpi_callsetfunc(sony_acpi_handle, item->acpiset,
263 item->value, NULL);
264 if (ret < 0) {
265 printk("%s: %d\n", __FUNCTION__, ret);
266 break;
267 }
268 }
269 return 0;
270}
271
Stelian Pop7f09c432007-01-13 23:04:31 +0100272static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
273{
Stelian Popc5611622007-01-13 23:04:37 +0100274 if (debug)
275 printk(LOG_PFX "sony_acpi_notify, event: %d\n", event);
276 acpi_bus_generate_event(sony_acpi_acpi_device, 1, event);
Stelian Pop7f09c432007-01-13 23:04:31 +0100277}
278
279static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
280 void *context, void **return_value)
281{
282 struct acpi_namespace_node *node;
283 union acpi_operand_object *operand;
284
285 node = (struct acpi_namespace_node *) handle;
286 operand = (union acpi_operand_object *) node->object;
287
288 printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
289 (u32) operand->method.param_count);
290
291 return AE_OK;
292}
293
294static int sony_acpi_add(struct acpi_device *device)
295{
296 acpi_status status;
297 int result;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100298 acpi_handle handle;
Mattia Dongili05e2d822007-01-13 23:04:38 +0100299 mode_t proc_file_mode;
Stelian Pop7f09c432007-01-13 23:04:31 +0100300 struct sony_acpi_value *item;
301
Stelian Popc5611622007-01-13 23:04:37 +0100302 sony_acpi_acpi_device = device;
303
Stelian Pop7f09c432007-01-13 23:04:31 +0100304 sony_acpi_handle = device->handle;
305
306 acpi_driver_data(device) = NULL;
307 acpi_device_dir(device) = sony_acpi_dir;
308
309 if (debug) {
310 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_acpi_handle,
311 1, sony_walk_callback, NULL, NULL);
312 if (ACPI_FAILURE(status)) {
313 printk(LOG_PFX "unable to walk acpi resources\n");
314 result = -ENODEV;
315 goto outwalk;
316 }
Stelian Popc5611622007-01-13 23:04:37 +0100317 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100318
Stelian Popc5611622007-01-13 23:04:37 +0100319 status = acpi_install_notify_handler(sony_acpi_handle,
320 ACPI_DEVICE_NOTIFY,
321 sony_acpi_notify,
322 NULL);
323 if (ACPI_FAILURE(status)) {
324 printk(LOG_PFX "unable to install notify handler\n");
325 result = -ENODEV;
326 goto outnotify;
Stelian Pop7f09c432007-01-13 23:04:31 +0100327 }
328
Alessandro Guido50f62af2007-01-13 23:04:34 +0100329 if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle, "GBRT", &handle))) {
330 sony_backlight_device = backlight_device_register("sony", NULL,
Andrew Morton82c47732007-01-13 23:04:36 +0100331 NULL, &sony_backlight_properties);
Alessandro Guido50f62af2007-01-13 23:04:34 +0100332 if (IS_ERR(sony_backlight_device)) {
333 printk(LOG_PFX "unable to register backlight device\n");
334 }
335 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100336
Alessandro Guido50f62af2007-01-13 23:04:34 +0100337 for (item = sony_acpi_values; item->name; ++item) {
Mattia Dongili05e2d822007-01-13 23:04:38 +0100338 proc_file_mode = 0;
339
Stelian Pop7f09c432007-01-13 23:04:31 +0100340 if (!debug && item->debug)
341 continue;
342
343 if (item->acpiget &&
Mattia Dongili05e2d822007-01-13 23:04:38 +0100344 ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100345 item->acpiget, &handle)))
Mattia Dongili05e2d822007-01-13 23:04:38 +0100346 proc_file_mode = S_IRUSR;
347 else
348 printk(LOG_PFX "unable to get ACPI handle for %s (get)\n",
349 item->name);
Stelian Pop7f09c432007-01-13 23:04:31 +0100350
351 if (item->acpiset &&
Mattia Dongili05e2d822007-01-13 23:04:38 +0100352 ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100353 item->acpiset, &handle)))
Mattia Dongili05e2d822007-01-13 23:04:38 +0100354 proc_file_mode |= S_IWUSR;
355 else
356 printk(LOG_PFX "unable to get ACPI handle for %s (set)\n",
357 item->name);
Stelian Pop7f09c432007-01-13 23:04:31 +0100358
Mattia Dongili05e2d822007-01-13 23:04:38 +0100359 if (proc_file_mode == 0)
360 continue;
361
362 item->proc = create_proc_entry(item->name, proc_file_mode,
Stelian Pop7f09c432007-01-13 23:04:31 +0100363 acpi_device_dir(device));
364 if (!item->proc) {
365 printk(LOG_PFX "unable to create proc entry\n");
366 result = -EIO;
367 goto outproc;
368 }
369
370 item->proc->read_proc = sony_acpi_read;
371 item->proc->write_proc = sony_acpi_write;
372 item->proc->data = item;
373 item->proc->owner = THIS_MODULE;
374 }
375
376 printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully installed\n");
377
378 return 0;
379
380outproc:
Mattia Dongili05e2d822007-01-13 23:04:38 +0100381 for (item = sony_acpi_values; item->name; ++item)
382 if (item->proc)
383 remove_proc_entry(item->name, acpi_device_dir(device));
384outnotify:
Stelian Popc5611622007-01-13 23:04:37 +0100385 status = acpi_remove_notify_handler(sony_acpi_handle,
386 ACPI_DEVICE_NOTIFY,
387 sony_acpi_notify);
388 if (ACPI_FAILURE(status))
389 printk(LOG_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100390outwalk:
391 return result;
392}
393
Stelian Pop7f09c432007-01-13 23:04:31 +0100394static int sony_acpi_remove(struct acpi_device *device, int type)
395{
396 acpi_status status;
397 struct sony_acpi_value *item;
398
Alessandro Guido50f62af2007-01-13 23:04:34 +0100399 if (sony_backlight_device)
400 backlight_device_unregister(sony_backlight_device);
401
Stelian Popc5611622007-01-13 23:04:37 +0100402 sony_acpi_acpi_device = NULL;
403
404 status = acpi_remove_notify_handler(sony_acpi_handle,
405 ACPI_DEVICE_NOTIFY,
406 sony_acpi_notify);
407 if (ACPI_FAILURE(status))
408 printk(LOG_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100409
410 for (item = sony_acpi_values; item->name; ++item)
411 if (item->proc)
412 remove_proc_entry(item->name, acpi_device_dir(device));
413
414 printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully removed\n");
415
416 return 0;
417}
418
Alessandro Guido50f62af2007-01-13 23:04:34 +0100419static int sony_backlight_update_status(struct backlight_device *bd)
420{
421 return acpi_callsetfunc(sony_acpi_handle, "SBRT",
422 bd->props->brightness + 1,
423 NULL);
424}
425
426static int sony_backlight_get_brightness(struct backlight_device *bd)
427{
428 int value;
429
430 if (acpi_callgetfunc(sony_acpi_handle, "GBRT", &value))
431 return 0;
432 /* brightness levels are 1-based, while backlight ones are 0-based */
433 return value - 1;
434}
435
Andrew Morton3f4f4612007-01-13 23:04:32 +0100436static struct acpi_driver sony_acpi_driver = {
437 .name = ACPI_SNC_DRIVER_NAME,
438 .class = ACPI_SNC_CLASS,
439 .ids = ACPI_SNC_HID,
440 .ops = {
441 .add = sony_acpi_add,
442 .remove = sony_acpi_remove,
443 .resume = sony_acpi_resume,
444 },
445};
446
Stelian Pop7f09c432007-01-13 23:04:31 +0100447static int __init sony_acpi_init(void)
448{
449 int result;
450
451 sony_acpi_dir = proc_mkdir("sony", acpi_root_dir);
452 if (!sony_acpi_dir) {
453 printk(LOG_PFX "unable to create /proc entry\n");
454 return -ENODEV;
455 }
456 sony_acpi_dir->owner = THIS_MODULE;
457
458 result = acpi_bus_register_driver(&sony_acpi_driver);
459 if (result < 0) {
460 remove_proc_entry("sony", acpi_root_dir);
461 return -ENODEV;
462 }
463 return 0;
464}
465
466
467static void __exit sony_acpi_exit(void)
468{
469 acpi_bus_unregister_driver(&sony_acpi_driver);
470 remove_proc_entry("sony", acpi_root_dir);
471}
472
473module_init(sony_acpi_init);
474module_exit(sony_acpi_exit);