]>
Commit | Line | Data |
---|---|---|
7f09c432 SP |
1 | /* |
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> | |
50f62afb AG |
30 | #include <linux/backlight.h> |
31 | #include <linux/err.h> | |
7f09c432 SP |
32 | #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" | |
50f62afb AG |
38 | #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 | |
7f09c432 SP |
43 | |
44 | #define LOG_PFX KERN_WARNING "sony_acpi: " | |
45 | ||
46 | MODULE_AUTHOR("Stelian Pop"); | |
47 | MODULE_DESCRIPTION(ACPI_SNC_DRIVER_NAME); | |
48 | MODULE_LICENSE("GPL"); | |
49 | ||
50 | static int debug; | |
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"); | |
54 | ||
7f09c432 SP |
55 | static acpi_handle sony_acpi_handle; |
56 | static struct proc_dir_entry *sony_acpi_dir; | |
57 | ||
50f62afb AG |
58 | static int sony_backlight_update_status(struct backlight_device *bd); |
59 | static int sony_backlight_get_brightness(struct backlight_device *bd); | |
60 | static struct backlight_device *sony_backlight_device; | |
61 | static struct backlight_properties sony_backlight_properties = { | |
62 | .owner = THIS_MODULE, | |
63 | .update_status = sony_backlight_update_status, | |
64 | .get_brightness = sony_backlight_get_brightness, | |
65 | .max_brightness = SONY_MAX_BRIGHTNESS - 1, | |
66 | }; | |
67 | ||
7f09c432 SP |
68 | static struct sony_acpi_value { |
69 | char *name; /* name of the entry */ | |
70 | struct proc_dir_entry *proc; /* /proc entry */ | |
71 | char *acpiget;/* name of the ACPI get function */ | |
72 | char *acpiset;/* name of the ACPI get function */ | |
73 | int min; /* minimum allowed value or -1 */ | |
74 | int max; /* maximum allowed value or -1 */ | |
3f4f461f AM |
75 | int value; /* current setting */ |
76 | int valid; /* Has ever been set */ | |
7f09c432 SP |
77 | int debug; /* active only in debug mode ? */ |
78 | } sony_acpi_values[] = { | |
243e8b19 AG |
79 | { |
80 | .name = "brightness", | |
81 | .acpiget = "GBRT", | |
82 | .acpiset = "SBRT", | |
83 | .min = 1, | |
84 | .max = SONY_MAX_BRIGHTNESS, | |
85 | .debug = 0, | |
86 | }, | |
7f09c432 SP |
87 | { |
88 | .name = "brightness_default", | |
89 | .acpiget = "GPBR", | |
90 | .acpiset = "SPBR", | |
91 | .min = 1, | |
50f62afb | 92 | .max = SONY_MAX_BRIGHTNESS, |
7f09c432 SP |
93 | .debug = 0, |
94 | }, | |
95 | { | |
96 | .name = "fnkey", | |
97 | .acpiget = "GHKE", | |
98 | .debug = 0, | |
99 | }, | |
100 | { | |
101 | .name = "cdpower", | |
102 | .acpiget = "GCDP", | |
103 | .acpiset = "SCDP", | |
104 | .min = -1, | |
105 | .max = -1, | |
106 | .debug = 0, | |
107 | }, | |
108 | { | |
109 | .name = "PID", | |
110 | .acpiget = "GPID", | |
111 | .debug = 1, | |
112 | }, | |
113 | { | |
114 | .name = "CTR", | |
115 | .acpiget = "GCTR", | |
116 | .acpiset = "SCTR", | |
117 | .min = -1, | |
118 | .max = -1, | |
119 | .debug = 1, | |
120 | }, | |
121 | { | |
122 | .name = "PCR", | |
123 | .acpiget = "GPCR", | |
124 | .acpiset = "SPCR", | |
125 | .min = -1, | |
126 | .max = -1, | |
127 | .debug = 1, | |
128 | }, | |
129 | { | |
130 | .name = "CMI", | |
131 | .acpiget = "GCMI", | |
132 | .acpiset = "SCMI", | |
133 | .min = -1, | |
134 | .max = -1, | |
135 | .debug = 1, | |
136 | }, | |
137 | { | |
138 | .name = NULL, | |
139 | } | |
140 | }; | |
141 | ||
142 | static int acpi_callgetfunc(acpi_handle handle, char *name, int *result) | |
143 | { | |
144 | struct acpi_buffer output; | |
145 | union acpi_object out_obj; | |
146 | acpi_status status; | |
147 | ||
148 | output.length = sizeof(out_obj); | |
149 | output.pointer = &out_obj; | |
150 | ||
151 | status = acpi_evaluate_object(handle, name, NULL, &output); | |
152 | if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) { | |
153 | *result = out_obj.integer.value; | |
154 | return 0; | |
155 | } | |
156 | ||
157 | printk(LOG_PFX "acpi_callreadfunc failed\n"); | |
158 | ||
159 | return -1; | |
160 | } | |
161 | ||
162 | static int acpi_callsetfunc(acpi_handle handle, char *name, int value, | |
163 | int *result) | |
164 | { | |
165 | struct acpi_object_list params; | |
166 | union acpi_object in_obj; | |
167 | struct acpi_buffer output; | |
168 | union acpi_object out_obj; | |
169 | acpi_status status; | |
170 | ||
171 | params.count = 1; | |
172 | params.pointer = &in_obj; | |
173 | in_obj.type = ACPI_TYPE_INTEGER; | |
174 | in_obj.integer.value = value; | |
175 | ||
176 | output.length = sizeof(out_obj); | |
177 | output.pointer = &out_obj; | |
178 | ||
179 | status = acpi_evaluate_object(handle, name, ¶ms, &output); | |
180 | if (status == AE_OK) { | |
181 | if (result != NULL) { | |
182 | if (out_obj.type != ACPI_TYPE_INTEGER) { | |
183 | printk(LOG_PFX "acpi_evaluate_object bad " | |
184 | "return type\n"); | |
185 | return -1; | |
186 | } | |
187 | *result = out_obj.integer.value; | |
188 | } | |
189 | return 0; | |
190 | } | |
191 | ||
192 | printk(LOG_PFX "acpi_evaluate_object failed\n"); | |
193 | ||
194 | return -1; | |
195 | } | |
196 | ||
197 | static int parse_buffer(const char __user *buffer, unsigned long count, | |
198 | int *val) { | |
199 | char s[32]; | |
200 | int ret; | |
201 | ||
202 | if (count > 31) | |
203 | return -EINVAL; | |
204 | if (copy_from_user(s, buffer, count)) | |
205 | return -EFAULT; | |
206 | s[count] = '\0'; | |
207 | ret = simple_strtoul(s, NULL, 10); | |
208 | *val = ret; | |
209 | return 0; | |
210 | } | |
211 | ||
212 | static int sony_acpi_read(char* page, char** start, off_t off, int count, | |
213 | int* eof, void *data) | |
214 | { | |
215 | struct sony_acpi_value *item = data; | |
216 | int value; | |
217 | ||
218 | if (!item->acpiget) | |
219 | return -EIO; | |
220 | ||
221 | if (acpi_callgetfunc(sony_acpi_handle, item->acpiget, &value) < 0) | |
222 | return -EIO; | |
223 | ||
224 | return sprintf(page, "%d\n", value); | |
225 | } | |
226 | ||
227 | static int sony_acpi_write(struct file *file, const char __user *buffer, | |
228 | unsigned long count, void *data) | |
229 | { | |
230 | struct sony_acpi_value *item = data; | |
231 | int result; | |
232 | int value; | |
233 | ||
234 | if (!item->acpiset) | |
235 | return -EIO; | |
236 | ||
237 | if ((result = parse_buffer(buffer, count, &value)) < 0) | |
238 | return result; | |
239 | ||
240 | if (item->min != -1 && value < item->min) | |
241 | return -EINVAL; | |
242 | if (item->max != -1 && value > item->max) | |
243 | return -EINVAL; | |
244 | ||
245 | if (acpi_callsetfunc(sony_acpi_handle, item->acpiset, value, NULL) < 0) | |
246 | return -EIO; | |
3f4f461f AM |
247 | item->value = value; |
248 | item->valid = 1; | |
7f09c432 SP |
249 | return count; |
250 | } | |
251 | ||
fac35061 | 252 | static int sony_acpi_resume(struct acpi_device *device) |
3f4f461f AM |
253 | { |
254 | struct sony_acpi_value *item; | |
255 | ||
256 | for (item = sony_acpi_values; item->name; item++) { | |
257 | int ret; | |
258 | ||
259 | if (!item->valid) | |
260 | continue; | |
261 | ret = acpi_callsetfunc(sony_acpi_handle, item->acpiset, | |
262 | item->value, NULL); | |
263 | if (ret < 0) { | |
264 | printk("%s: %d\n", __FUNCTION__, ret); | |
265 | break; | |
266 | } | |
267 | } | |
268 | return 0; | |
269 | } | |
270 | ||
7f09c432 SP |
271 | static void sony_acpi_notify(acpi_handle handle, u32 event, void *data) |
272 | { | |
273 | printk(LOG_PFX "sony_acpi_notify\n"); | |
274 | } | |
275 | ||
276 | static acpi_status sony_walk_callback(acpi_handle handle, u32 level, | |
277 | void *context, void **return_value) | |
278 | { | |
279 | struct acpi_namespace_node *node; | |
280 | union acpi_operand_object *operand; | |
281 | ||
282 | node = (struct acpi_namespace_node *) handle; | |
283 | operand = (union acpi_operand_object *) node->object; | |
284 | ||
285 | printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii, | |
286 | (u32) operand->method.param_count); | |
287 | ||
288 | return AE_OK; | |
289 | } | |
290 | ||
291 | static int sony_acpi_add(struct acpi_device *device) | |
292 | { | |
293 | acpi_status status; | |
294 | int result; | |
50f62afb | 295 | acpi_handle handle; |
7f09c432 SP |
296 | struct sony_acpi_value *item; |
297 | ||
298 | sony_acpi_handle = device->handle; | |
299 | ||
300 | acpi_driver_data(device) = NULL; | |
301 | acpi_device_dir(device) = sony_acpi_dir; | |
302 | ||
303 | if (debug) { | |
304 | status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_acpi_handle, | |
305 | 1, sony_walk_callback, NULL, NULL); | |
306 | if (ACPI_FAILURE(status)) { | |
307 | printk(LOG_PFX "unable to walk acpi resources\n"); | |
308 | result = -ENODEV; | |
309 | goto outwalk; | |
310 | } | |
311 | ||
312 | status = acpi_install_notify_handler(sony_acpi_handle, | |
313 | ACPI_DEVICE_NOTIFY, | |
314 | sony_acpi_notify, | |
315 | NULL); | |
316 | if (ACPI_FAILURE(status)) { | |
317 | printk(LOG_PFX "unable to install notify handler\n"); | |
318 | result = -ENODEV; | |
319 | goto outnotify; | |
320 | } | |
321 | } | |
322 | ||
50f62afb AG |
323 | if (ACPI_SUCCESS(acpi_get_handle(sony_acpi_handle, "GBRT", &handle))) { |
324 | sony_backlight_device = backlight_device_register("sony", NULL, | |
82c47731 | 325 | NULL, &sony_backlight_properties); |
50f62afb AG |
326 | if (IS_ERR(sony_backlight_device)) { |
327 | printk(LOG_PFX "unable to register backlight device\n"); | |
328 | } | |
329 | } | |
7f09c432 | 330 | |
50f62afb | 331 | for (item = sony_acpi_values; item->name; ++item) { |
7f09c432 SP |
332 | if (!debug && item->debug) |
333 | continue; | |
334 | ||
335 | if (item->acpiget && | |
336 | ACPI_FAILURE(acpi_get_handle(sony_acpi_handle, | |
337 | item->acpiget, &handle))) | |
338 | continue; | |
339 | ||
340 | if (item->acpiset && | |
341 | ACPI_FAILURE(acpi_get_handle(sony_acpi_handle, | |
342 | item->acpiset, &handle))) | |
343 | continue; | |
344 | ||
345 | item->proc = create_proc_entry(item->name, 0600, | |
346 | acpi_device_dir(device)); | |
347 | if (!item->proc) { | |
348 | printk(LOG_PFX "unable to create proc entry\n"); | |
349 | result = -EIO; | |
350 | goto outproc; | |
351 | } | |
352 | ||
353 | item->proc->read_proc = sony_acpi_read; | |
354 | item->proc->write_proc = sony_acpi_write; | |
355 | item->proc->data = item; | |
356 | item->proc->owner = THIS_MODULE; | |
357 | } | |
358 | ||
359 | printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully installed\n"); | |
360 | ||
361 | return 0; | |
362 | ||
363 | outproc: | |
364 | if (debug) { | |
365 | status = acpi_remove_notify_handler(sony_acpi_handle, | |
366 | ACPI_DEVICE_NOTIFY, | |
367 | sony_acpi_notify); | |
368 | if (ACPI_FAILURE(status)) | |
369 | printk(LOG_PFX "unable to remove notify handler\n"); | |
370 | } | |
371 | outnotify: | |
372 | for (item = sony_acpi_values; item->name; ++item) | |
373 | if (item->proc) | |
374 | remove_proc_entry(item->name, acpi_device_dir(device)); | |
375 | outwalk: | |
376 | return result; | |
377 | } | |
378 | ||
7f09c432 SP |
379 | static int sony_acpi_remove(struct acpi_device *device, int type) |
380 | { | |
381 | acpi_status status; | |
382 | struct sony_acpi_value *item; | |
383 | ||
50f62afb AG |
384 | if (sony_backlight_device) |
385 | backlight_device_unregister(sony_backlight_device); | |
386 | ||
7f09c432 SP |
387 | if (debug) { |
388 | status = acpi_remove_notify_handler(sony_acpi_handle, | |
389 | ACPI_DEVICE_NOTIFY, | |
390 | sony_acpi_notify); | |
391 | if (ACPI_FAILURE(status)) | |
392 | printk(LOG_PFX "unable to remove notify handler\n"); | |
393 | } | |
394 | ||
395 | for (item = sony_acpi_values; item->name; ++item) | |
396 | if (item->proc) | |
397 | remove_proc_entry(item->name, acpi_device_dir(device)); | |
398 | ||
399 | printk(KERN_INFO ACPI_SNC_DRIVER_NAME " successfully removed\n"); | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
50f62afb AG |
404 | static int sony_backlight_update_status(struct backlight_device *bd) |
405 | { | |
406 | return acpi_callsetfunc(sony_acpi_handle, "SBRT", | |
407 | bd->props->brightness + 1, | |
408 | NULL); | |
409 | } | |
410 | ||
411 | static int sony_backlight_get_brightness(struct backlight_device *bd) | |
412 | { | |
413 | int value; | |
414 | ||
415 | if (acpi_callgetfunc(sony_acpi_handle, "GBRT", &value)) | |
416 | return 0; | |
417 | /* brightness levels are 1-based, while backlight ones are 0-based */ | |
418 | return value - 1; | |
419 | } | |
420 | ||
3f4f461f AM |
421 | static struct acpi_driver sony_acpi_driver = { |
422 | .name = ACPI_SNC_DRIVER_NAME, | |
423 | .class = ACPI_SNC_CLASS, | |
424 | .ids = ACPI_SNC_HID, | |
425 | .ops = { | |
426 | .add = sony_acpi_add, | |
427 | .remove = sony_acpi_remove, | |
428 | .resume = sony_acpi_resume, | |
429 | }, | |
430 | }; | |
431 | ||
7f09c432 SP |
432 | static int __init sony_acpi_init(void) |
433 | { | |
434 | int result; | |
435 | ||
436 | sony_acpi_dir = proc_mkdir("sony", acpi_root_dir); | |
437 | if (!sony_acpi_dir) { | |
438 | printk(LOG_PFX "unable to create /proc entry\n"); | |
439 | return -ENODEV; | |
440 | } | |
441 | sony_acpi_dir->owner = THIS_MODULE; | |
442 | ||
443 | result = acpi_bus_register_driver(&sony_acpi_driver); | |
444 | if (result < 0) { | |
445 | remove_proc_entry("sony", acpi_root_dir); | |
446 | return -ENODEV; | |
447 | } | |
448 | return 0; | |
449 | } | |
450 | ||
451 | ||
452 | static void __exit sony_acpi_exit(void) | |
453 | { | |
454 | acpi_bus_unregister_driver(&sony_acpi_driver); | |
455 | remove_proc_entry("sony", acpi_root_dir); | |
456 | } | |
457 | ||
458 | module_init(sony_acpi_init); | |
459 | module_exit(sony_acpi_exit); |