blob: e67189a361a23e2ad5e72d9413d04d727f0e44ae [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>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +01005 * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
Stelian Pop7f09c432007-01-13 23:04:31 +01006 *
7 * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8 * which are copyrighted by their respective authors.
9 *
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.
14 *
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.
19 *
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.
23 *
24 */
25
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>
Alessandro Guido50f62af2007-01-13 23:04:34 +010031#include <linux/backlight.h>
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010032#include <linux/platform_device.h>
Alessandro Guido50f62af2007-01-13 23:04:34 +010033#include <linux/err.h>
Stelian Pop7f09c432007-01-13 23:04:31 +010034#include <acpi/acpi_drivers.h>
35#include <acpi/acpi_bus.h>
36#include <asm/uaccess.h>
37
malattia@linux.it59b19102007-04-09 10:19:04 +020038#define SONY_NC_CLASS "sony"
39#define SONY_NC_HID "SNY5001"
40#define SONY_NC_DRIVER_NAME "ACPI Sony Notebook Control Driver v0.4"
Alessandro Guido50f62af2007-01-13 23:04:34 +010041
42/* the device uses 1-based values, while the backlight subsystem uses
43 0-based values */
44#define SONY_MAX_BRIGHTNESS 8
Stelian Pop7f09c432007-01-13 23:04:31 +010045
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010046#define LOG_PFX KERN_WARNING "sony-laptop: "
Stelian Pop7f09c432007-01-13 23:04:31 +010047
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010048MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it59b19102007-04-09 10:19:04 +020049MODULE_DESCRIPTION(SONY_NC_DRIVER_NAME);
Stelian Pop7f09c432007-01-13 23:04:31 +010050MODULE_LICENSE("GPL");
51
52static int debug;
53module_param(debug, int, 0);
54MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050055 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010056
malattia@linux.it59b19102007-04-09 10:19:04 +020057static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -050058 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +020059static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -050060 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +010061static int boolean_validate(const int, const int);
62static int brightness_default_validate(const int, const int);
63
64#define SNC_VALIDATE_IN 0
65#define SNC_VALIDATE_OUT 1
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010066
malattia@linux.it59b19102007-04-09 10:19:04 +020067struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -050068 char *name; /* name of the entry */
69 char **acpiget; /* names of the ACPI get function */
70 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +010071 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -050072 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 */
Stelian Pop7f09c432007-01-13 23:04:31 +010076};
77
malattia@linux.it59b19102007-04-09 10:19:04 +020078#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010079 static char *snc_##_name[] = { _values, NULL }
80
malattia@linux.it59b19102007-04-09 10:19:04 +020081#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010082 { \
83 .name = __stringify(_name), \
84 .acpiget = _getters, \
85 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +010086 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010087 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +020088 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010089 }
90
malattia@linux.it59b19102007-04-09 10:19:04 +020091#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010092
malattia@linux.it59b19102007-04-09 10:19:04 +020093SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010094
malattia@linux.it59b19102007-04-09 10:19:04 +020095SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
96SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010097
malattia@linux.it59b19102007-04-09 10:19:04 +020098SNC_HANDLE_NAMES(cdpower_get, "GCDP");
99SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100100
malattia@linux.it59b19102007-04-09 10:19:04 +0200101SNC_HANDLE_NAMES(audiopower_get, "GAZP");
102SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100103
malattia@linux.it59b19102007-04-09 10:19:04 +0200104SNC_HANDLE_NAMES(lanpower_get, "GLNP");
105SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100106
malattia@linux.it59b19102007-04-09 10:19:04 +0200107SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100108
malattia@linux.it59b19102007-04-09 10:19:04 +0200109SNC_HANDLE_NAMES(CTR_get, "GCTR");
110SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100111
malattia@linux.it59b19102007-04-09 10:19:04 +0200112SNC_HANDLE_NAMES(PCR_get, "GPCR");
113SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100114
malattia@linux.it59b19102007-04-09 10:19:04 +0200115SNC_HANDLE_NAMES(CMI_get, "GCMI");
116SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100117
malattia@linux.it59b19102007-04-09 10:19:04 +0200118static struct sony_nc_value sony_nc_values[] = {
119 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100120 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200121 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
122 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
123 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100124 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200125 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100126 boolean_validate, 1),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100127 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200128 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
129 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
130 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
131 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
132 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100133};
134
malattia@linux.it59b19102007-04-09 10:19:04 +0200135static acpi_handle sony_nc_acpi_handle;
136static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100137
Mattia Dongilid78865c2007-02-07 20:01:56 +0100138/*
139 * acpi_evaluate_object wrappers
140 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100141static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
142{
143 struct acpi_buffer output;
144 union acpi_object out_obj;
145 acpi_status status;
146
147 output.length = sizeof(out_obj);
148 output.pointer = &out_obj;
149
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;
153 return 0;
154 }
155
156 printk(LOG_PFX "acpi_callreadfunc failed\n");
157
158 return -1;
159}
160
161static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
162 int *result)
163{
164 struct acpi_object_list params;
165 union acpi_object in_obj;
166 struct acpi_buffer output;
167 union acpi_object out_obj;
168 acpi_status status;
169
170 params.count = 1;
171 params.pointer = &in_obj;
172 in_obj.type = ACPI_TYPE_INTEGER;
173 in_obj.integer.value = value;
174
175 output.length = sizeof(out_obj);
176 output.pointer = &out_obj;
177
178 status = acpi_evaluate_object(handle, name, &params, &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 "
183 "return type\n");
184 return -1;
185 }
186 *result = out_obj.integer.value;
187 }
188 return 0;
189 }
190
191 printk(LOG_PFX "acpi_evaluate_object failed\n");
192
193 return -1;
194}
195
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100196/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200197 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100198 */
199
200/* brightness_default_validate:
201 *
202 * manipulate input output values to keep consistency with the
203 * backlight framework for which brightness values are 0-based.
204 */
205static int brightness_default_validate(const int direction, const int value)
206{
207 switch (direction) {
208 case SNC_VALIDATE_OUT:
209 return value - 1;
210 case SNC_VALIDATE_IN:
211 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
212 return value + 1;
213 }
214 return -EINVAL;
215}
216
217/* boolean_validate:
218 *
219 * on input validate boolean values 0/1, on output just pass the
220 * received value.
221 */
222static int boolean_validate(const int direction, const int value)
223{
224 if (direction == SNC_VALIDATE_IN) {
225 if (value != 0 && value != 1)
226 return -EINVAL;
227 }
228 return value;
229}
230
231/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200232 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100233 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200234static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500235 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100236{
Stelian Pop7f09c432007-01-13 23:04:31 +0100237 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200238 struct sony_nc_value *item =
239 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100240
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100241 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100242 return -EIO;
243
malattia@linux.it59b19102007-04-09 10:19:04 +0200244 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100245 return -EIO;
246
Mattia Dongili156c2212007-02-12 22:01:07 +0100247 if (item->validate)
248 value = item->validate(SNC_VALIDATE_OUT, value);
249
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100250 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100251}
252
malattia@linux.it59b19102007-04-09 10:19:04 +0200253static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500254 struct device_attribute *attr,
255 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100256{
Stelian Pop7f09c432007-01-13 23:04:31 +0100257 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200258 struct sony_nc_value *item =
259 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100260
261 if (!item->acpiset)
262 return -EIO;
263
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100264 if (count > 31)
265 return -EINVAL;
266
267 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100268
Mattia Dongili156c2212007-02-12 22:01:07 +0100269 if (item->validate)
270 value = item->validate(SNC_VALIDATE_IN, value);
271
272 if (value < 0)
273 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100274
malattia@linux.it59b19102007-04-09 10:19:04 +0200275 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100276 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100277 item->value = value;
278 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100279 return count;
280}
281
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100282/*
283 * Platform device
284 */
285static struct platform_driver sncpf_driver = {
286 .driver = {
Len Browna02d1c12007-02-07 15:34:02 -0500287 .name = "sony-laptop",
288 .owner = THIS_MODULE,
289 }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100290};
291static struct platform_device *sncpf_device;
292
malattia@linux.it59b19102007-04-09 10:19:04 +0200293static int sony_nc_pf_add(void)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100294{
295 acpi_handle handle;
malattia@linux.it59b19102007-04-09 10:19:04 +0200296 struct sony_nc_value *item;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100297 int ret = 0;
298
299 ret = platform_driver_register(&sncpf_driver);
300 if (ret)
301 goto out;
302
303 sncpf_device = platform_device_alloc("sony-laptop", -1);
304 if (!sncpf_device) {
305 ret = -ENOMEM;
306 goto out_platform_registered;
307 }
308
309 ret = platform_device_add(sncpf_device);
310 if (ret)
311 goto out_platform_alloced;
312
malattia@linux.it59b19102007-04-09 10:19:04 +0200313 for (item = sony_nc_values; item->name; ++item) {
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100314
315 if (!debug && item->debug)
316 continue;
317
318 /* find the available acpiget as described in the DSDT */
319 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200320 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
Len Browna02d1c12007-02-07 15:34:02 -0500321 *item->acpiget,
322 &handle))) {
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100323 if (debug)
324 printk(LOG_PFX "Found %s getter: %s\n",
Len Browna02d1c12007-02-07 15:34:02 -0500325 item->name, *item->acpiget);
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100326 item->devattr.attr.mode |= S_IRUGO;
327 break;
328 }
329 }
330
331 /* find the available acpiset as described in the DSDT */
332 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200333 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
Len Browna02d1c12007-02-07 15:34:02 -0500334 *item->acpiset,
335 &handle))) {
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100336 if (debug)
337 printk(LOG_PFX "Found %s setter: %s\n",
Len Browna02d1c12007-02-07 15:34:02 -0500338 item->name, *item->acpiset);
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100339 item->devattr.attr.mode |= S_IWUSR;
340 break;
341 }
342 }
343
Len Browna02d1c12007-02-07 15:34:02 -0500344 if (item->devattr.attr.mode != 0) {
345 ret =
346 device_create_file(&sncpf_device->dev,
347 &item->devattr);
348 if (ret)
349 goto out_sysfs;
350 }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100351 }
352
353 return 0;
354
Len Browna02d1c12007-02-07 15:34:02 -0500355 out_sysfs:
malattia@linux.it59b19102007-04-09 10:19:04 +0200356 for (item = sony_nc_values; item->name; ++item) {
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100357 device_remove_file(&sncpf_device->dev, &item->devattr);
358 }
359 platform_device_del(sncpf_device);
Len Browna02d1c12007-02-07 15:34:02 -0500360 out_platform_alloced:
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100361 platform_device_put(sncpf_device);
Len Browna02d1c12007-02-07 15:34:02 -0500362 out_platform_registered:
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100363 platform_driver_unregister(&sncpf_driver);
Len Browna02d1c12007-02-07 15:34:02 -0500364 out:
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100365 return ret;
366}
367
malattia@linux.it59b19102007-04-09 10:19:04 +0200368static void sony_nc_pf_remove(void)
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100369{
malattia@linux.it59b19102007-04-09 10:19:04 +0200370 struct sony_nc_value *item;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100371
malattia@linux.it59b19102007-04-09 10:19:04 +0200372 for (item = sony_nc_values; item->name; ++item) {
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100373 device_remove_file(&sncpf_device->dev, &item->devattr);
374 }
375
376 platform_device_del(sncpf_device);
377 platform_device_put(sncpf_device);
378 platform_driver_unregister(&sncpf_driver);
379}
380
Mattia Dongilid78865c2007-02-07 20:01:56 +0100381/*
382 * Backlight device
383 */
384static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100385{
malattia@linux.it59b19102007-04-09 10:19:04 +0200386 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000387 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100388}
389
Mattia Dongilid78865c2007-02-07 20:01:56 +0100390static int sony_backlight_get_brightness(struct backlight_device *bd)
391{
392 int value;
393
malattia@linux.it59b19102007-04-09 10:19:04 +0200394 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100395 return 0;
396 /* brightness levels are 1-based, while backlight ones are 0-based */
397 return value - 1;
398}
399
400static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000401static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500402 .update_status = sony_backlight_update_status,
403 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100404};
405
406/*
407 * ACPI callbacks
408 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100409static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
410{
Stelian Popc5611622007-01-13 23:04:37 +0100411 if (debug)
412 printk(LOG_PFX "sony_acpi_notify, event: %d\n", event);
malattia@linux.it59b19102007-04-09 10:19:04 +0200413 acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
Stelian Pop7f09c432007-01-13 23:04:31 +0100414}
415
416static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
417 void *context, void **return_value)
418{
419 struct acpi_namespace_node *node;
420 union acpi_operand_object *operand;
421
Len Browna02d1c12007-02-07 15:34:02 -0500422 node = (struct acpi_namespace_node *)handle;
423 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100424
425 printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
426 (u32) operand->method.param_count);
427
428 return AE_OK;
429}
430
Mattia Dongilid78865c2007-02-07 20:01:56 +0100431/*
432 * ACPI device
433 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200434static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100435{
malattia@linux.it59b19102007-04-09 10:19:04 +0200436 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100437
malattia@linux.it59b19102007-04-09 10:19:04 +0200438 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100439 int ret;
440
441 if (!item->valid)
442 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200443 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500444 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100445 if (ret < 0) {
446 printk("%s: %d\n", __FUNCTION__, ret);
447 break;
448 }
449 }
450 return 0;
451}
452
malattia@linux.it59b19102007-04-09 10:19:04 +0200453static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100454{
455 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800456 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100457 acpi_handle handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100458
malattia@linux.it59b19102007-04-09 10:19:04 +0200459 sony_nc_acpi_device = device;
Stelian Popc5611622007-01-13 23:04:37 +0100460
malattia@linux.it59b19102007-04-09 10:19:04 +0200461 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100462
Stelian Pop7f09c432007-01-13 23:04:31 +0100463 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200464 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100465 1, sony_walk_callback, NULL, NULL);
466 if (ACPI_FAILURE(status)) {
467 printk(LOG_PFX "unable to walk acpi resources\n");
468 result = -ENODEV;
469 goto outwalk;
470 }
Stelian Popc5611622007-01-13 23:04:37 +0100471 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100472
malattia@linux.it59b19102007-04-09 10:19:04 +0200473 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100474 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -0500475 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +0100476 if (ACPI_FAILURE(status)) {
477 printk(LOG_PFX "unable to install notify handler\n");
478 result = -ENODEV;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100479 goto outwalk;
Stelian Pop7f09c432007-01-13 23:04:31 +0100480 }
481
malattia@linux.it59b19102007-04-09 10:19:04 +0200482 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +0100483 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -0500484 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +0000485 &sony_backlight_ops);
Mattia Dongili7df03b822007-01-13 23:04:41 +0100486
Len Browna02d1c12007-02-07 15:34:02 -0500487 if (IS_ERR(sony_backlight_device)) {
Mattia Dongili91fbc1d2007-02-07 20:01:53 +0100488 printk(LOG_PFX "unable to register backlight device\n");
Mattia Dongili7df03b822007-01-13 23:04:41 +0100489 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +0000490 } else {
491 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -0500492 sony_backlight_get_brightness
493 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +0000494 sony_backlight_device->props.max_brightness =
495 SONY_MAX_BRIGHTNESS - 1;
496 }
497
Alessandro Guido50f62af2007-01-13 23:04:34 +0100498 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100499
malattia@linux.it59b19102007-04-09 10:19:04 +0200500 if (sony_nc_pf_add())
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100501 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +0100502
malattia@linux.it59b19102007-04-09 10:19:04 +0200503 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100504
505 return 0;
506
Len Browna02d1c12007-02-07 15:34:02 -0500507 outbacklight:
Mattia Dongili7df03b822007-01-13 23:04:41 +0100508 if (sony_backlight_device)
509 backlight_device_unregister(sony_backlight_device);
510
malattia@linux.it59b19102007-04-09 10:19:04 +0200511 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100512 ACPI_DEVICE_NOTIFY,
513 sony_acpi_notify);
514 if (ACPI_FAILURE(status))
515 printk(LOG_PFX "unable to remove notify handler\n");
Len Browna02d1c12007-02-07 15:34:02 -0500516 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +0100517 return result;
518}
519
malattia@linux.it59b19102007-04-09 10:19:04 +0200520static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +0100521{
522 acpi_status status;
Stelian Pop7f09c432007-01-13 23:04:31 +0100523
Alessandro Guido50f62af2007-01-13 23:04:34 +0100524 if (sony_backlight_device)
525 backlight_device_unregister(sony_backlight_device);
526
malattia@linux.it59b19102007-04-09 10:19:04 +0200527 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +0100528
malattia@linux.it59b19102007-04-09 10:19:04 +0200529 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100530 ACPI_DEVICE_NOTIFY,
531 sony_acpi_notify);
532 if (ACPI_FAILURE(status))
533 printk(LOG_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100534
malattia@linux.it59b19102007-04-09 10:19:04 +0200535 sony_nc_pf_remove();
Stelian Pop7f09c432007-01-13 23:04:31 +0100536
malattia@linux.it59b19102007-04-09 10:19:04 +0200537 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100538
539 return 0;
540}
541
malattia@linux.it59b19102007-04-09 10:19:04 +0200542static struct acpi_driver sony_nc_driver = {
543 .name = SONY_NC_DRIVER_NAME,
544 .class = SONY_NC_CLASS,
545 .ids = SONY_NC_HID,
Len Browna02d1c12007-02-07 15:34:02 -0500546 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +0200547 .add = sony_nc_add,
548 .remove = sony_nc_remove,
549 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -0500550 },
Andrew Morton3f4f4612007-01-13 23:04:32 +0100551};
552
malattia@linux.it59b19102007-04-09 10:19:04 +0200553static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +0100554{
malattia@linux.it59b19102007-04-09 10:19:04 +0200555 return acpi_bus_register_driver(&sony_nc_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +0100556}
557
malattia@linux.it59b19102007-04-09 10:19:04 +0200558static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +0100559{
malattia@linux.it59b19102007-04-09 10:19:04 +0200560 acpi_bus_unregister_driver(&sony_nc_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +0100561}
562
malattia@linux.it59b19102007-04-09 10:19:04 +0200563module_init(sony_laptop_init);
564module_exit(sony_laptop_exit);