blob: f4755370dd52e4800d9e9049238e631b36ec1288 [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.itb9a218b2007-04-09 10:19:06 +020038#define LOG_PFX KERN_WARNING "sony-laptop: "
39#define dprintk(msg...) do { \
40 if (debug) printk(KERN_WARNING LOG_PFX msg); \
41} while (0)
42
malattia@linux.it59b19102007-04-09 10:19:04 +020043#define SONY_NC_CLASS "sony"
44#define SONY_NC_HID "SNY5001"
45#define SONY_NC_DRIVER_NAME "ACPI Sony Notebook Control Driver v0.4"
Alessandro Guido50f62af2007-01-13 23:04:34 +010046
47/* the device uses 1-based values, while the backlight subsystem uses
48 0-based values */
49#define SONY_MAX_BRIGHTNESS 8
Stelian Pop7f09c432007-01-13 23:04:31 +010050
Mattia Dongilied3aa4b2007-02-07 20:01:54 +010051MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
malattia@linux.it59b19102007-04-09 10:19:04 +020052MODULE_DESCRIPTION(SONY_NC_DRIVER_NAME);
Stelian Pop7f09c432007-01-13 23:04:31 +010053MODULE_LICENSE("GPL");
54
55static int debug;
56module_param(debug, int, 0);
57MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
Len Browna02d1c12007-02-07 15:34:02 -050058 "the development of this driver");
Stelian Pop7f09c432007-01-13 23:04:31 +010059
malattia@linux.it56b87562007-04-09 10:19:05 +020060/*********** Platform Device ***********/
61
62static atomic_t sony_pf_users = ATOMIC_INIT(0);
63static struct platform_driver sony_pf_driver = {
64 .driver = {
65 .name = "sony-laptop",
66 .owner = THIS_MODULE,
67 }
68};
69static struct platform_device *sony_pf_device;
70
71static int sony_pf_add(void)
72{
73 int ret = 0;
74
75 /* don't run again if already initialized */
76 if (atomic_add_return(1, &sony_pf_users) > 1)
77 return 0;
78
79 ret = platform_driver_register(&sony_pf_driver);
80 if (ret)
81 goto out;
82
83 sony_pf_device = platform_device_alloc("sony-laptop", -1);
84 if (!sony_pf_device) {
85 ret = -ENOMEM;
86 goto out_platform_registered;
87 }
88
89 ret = platform_device_add(sony_pf_device);
90 if (ret)
91 goto out_platform_alloced;
92
93 return 0;
94
95 out_platform_alloced:
96 platform_device_put(sony_pf_device);
97 sony_pf_device = NULL;
98 out_platform_registered:
99 platform_driver_unregister(&sony_pf_driver);
100 out:
101 atomic_dec(&sony_pf_users);
102 return ret;
103}
104
105static void sony_pf_remove(void)
106{
107 /* deregister only after the last user has gone */
108 if (!atomic_dec_and_test(&sony_pf_users))
109 return;
110
111 platform_device_del(sony_pf_device);
112 platform_device_put(sony_pf_device);
113 platform_driver_unregister(&sony_pf_driver);
114}
115
116/*********** SNC (SNY5001) Device ***********/
117
malattia@linux.it59b19102007-04-09 10:19:04 +0200118static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500119 char *);
malattia@linux.it59b19102007-04-09 10:19:04 +0200120static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
Len Browna02d1c12007-02-07 15:34:02 -0500121 const char *, size_t);
Mattia Dongili156c2212007-02-12 22:01:07 +0100122static int boolean_validate(const int, const int);
123static int brightness_default_validate(const int, const int);
124
125#define SNC_VALIDATE_IN 0
126#define SNC_VALIDATE_OUT 1
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100127
malattia@linux.it59b19102007-04-09 10:19:04 +0200128struct sony_nc_value {
Len Browna02d1c12007-02-07 15:34:02 -0500129 char *name; /* name of the entry */
130 char **acpiget; /* names of the ACPI get function */
131 char **acpiset; /* names of the ACPI set function */
Mattia Dongili156c2212007-02-12 22:01:07 +0100132 int (*validate)(const int, const int); /* input/output validation */
Len Browna02d1c12007-02-07 15:34:02 -0500133 int value; /* current setting */
134 int valid; /* Has ever been set */
135 int debug; /* active only in debug mode ? */
136 struct device_attribute devattr; /* sysfs atribute */
Stelian Pop7f09c432007-01-13 23:04:31 +0100137};
138
malattia@linux.it59b19102007-04-09 10:19:04 +0200139#define SNC_HANDLE_NAMES(_name, _values...) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100140 static char *snc_##_name[] = { _values, NULL }
141
malattia@linux.it59b19102007-04-09 10:19:04 +0200142#define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100143 { \
144 .name = __stringify(_name), \
145 .acpiget = _getters, \
146 .acpiset = _setters, \
Mattia Dongili156c2212007-02-12 22:01:07 +0100147 .validate = _validate, \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100148 .debug = _debug, \
malattia@linux.it59b19102007-04-09 10:19:04 +0200149 .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100150 }
151
malattia@linux.it59b19102007-04-09 10:19:04 +0200152#define SNC_HANDLE_NULL { .name = NULL }
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100153
malattia@linux.it59b19102007-04-09 10:19:04 +0200154SNC_HANDLE_NAMES(fnkey_get, "GHKE");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100155
malattia@linux.it59b19102007-04-09 10:19:04 +0200156SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
157SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100158
malattia@linux.it59b19102007-04-09 10:19:04 +0200159SNC_HANDLE_NAMES(cdpower_get, "GCDP");
160SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100161
malattia@linux.it59b19102007-04-09 10:19:04 +0200162SNC_HANDLE_NAMES(audiopower_get, "GAZP");
163SNC_HANDLE_NAMES(audiopower_set, "AZPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100164
malattia@linux.it59b19102007-04-09 10:19:04 +0200165SNC_HANDLE_NAMES(lanpower_get, "GLNP");
166SNC_HANDLE_NAMES(lanpower_set, "LNPW");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100167
malattia@linux.it59b19102007-04-09 10:19:04 +0200168SNC_HANDLE_NAMES(PID_get, "GPID");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100169
malattia@linux.it59b19102007-04-09 10:19:04 +0200170SNC_HANDLE_NAMES(CTR_get, "GCTR");
171SNC_HANDLE_NAMES(CTR_set, "SCTR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100172
malattia@linux.it59b19102007-04-09 10:19:04 +0200173SNC_HANDLE_NAMES(PCR_get, "GPCR");
174SNC_HANDLE_NAMES(PCR_set, "SPCR");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100175
malattia@linux.it59b19102007-04-09 10:19:04 +0200176SNC_HANDLE_NAMES(CMI_get, "GCMI");
177SNC_HANDLE_NAMES(CMI_set, "SCMI");
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100178
malattia@linux.it59b19102007-04-09 10:19:04 +0200179static struct sony_nc_value sony_nc_values[] = {
180 SNC_HANDLE(brightness_default, snc_brightness_def_get,
Mattia Dongili156c2212007-02-12 22:01:07 +0100181 snc_brightness_def_set, brightness_default_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200182 SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
183 SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
184 SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100185 boolean_validate, 0),
malattia@linux.it59b19102007-04-09 10:19:04 +0200186 SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
Mattia Dongili156c2212007-02-12 22:01:07 +0100187 boolean_validate, 1),
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100188 /* unknown methods */
malattia@linux.it59b19102007-04-09 10:19:04 +0200189 SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
190 SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
191 SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
192 SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
193 SNC_HANDLE_NULL
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100194};
195
malattia@linux.it59b19102007-04-09 10:19:04 +0200196static acpi_handle sony_nc_acpi_handle;
197static struct acpi_device *sony_nc_acpi_device = NULL;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100198
Mattia Dongilid78865c2007-02-07 20:01:56 +0100199/*
200 * acpi_evaluate_object wrappers
201 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100202static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
203{
204 struct acpi_buffer output;
205 union acpi_object out_obj;
206 acpi_status status;
207
208 output.length = sizeof(out_obj);
209 output.pointer = &out_obj;
210
211 status = acpi_evaluate_object(handle, name, NULL, &output);
212 if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
213 *result = out_obj.integer.value;
214 return 0;
215 }
216
217 printk(LOG_PFX "acpi_callreadfunc failed\n");
218
219 return -1;
220}
221
222static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
223 int *result)
224{
225 struct acpi_object_list params;
226 union acpi_object in_obj;
227 struct acpi_buffer output;
228 union acpi_object out_obj;
229 acpi_status status;
230
231 params.count = 1;
232 params.pointer = &in_obj;
233 in_obj.type = ACPI_TYPE_INTEGER;
234 in_obj.integer.value = value;
235
236 output.length = sizeof(out_obj);
237 output.pointer = &out_obj;
238
239 status = acpi_evaluate_object(handle, name, &params, &output);
240 if (status == AE_OK) {
241 if (result != NULL) {
242 if (out_obj.type != ACPI_TYPE_INTEGER) {
243 printk(LOG_PFX "acpi_evaluate_object bad "
244 "return type\n");
245 return -1;
246 }
247 *result = out_obj.integer.value;
248 }
249 return 0;
250 }
251
252 printk(LOG_PFX "acpi_evaluate_object failed\n");
253
254 return -1;
255}
256
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100257/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200258 * sony_nc_values input/output validate functions
Mattia Dongili156c2212007-02-12 22:01:07 +0100259 */
260
261/* brightness_default_validate:
262 *
263 * manipulate input output values to keep consistency with the
264 * backlight framework for which brightness values are 0-based.
265 */
266static int brightness_default_validate(const int direction, const int value)
267{
268 switch (direction) {
269 case SNC_VALIDATE_OUT:
270 return value - 1;
271 case SNC_VALIDATE_IN:
272 if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
273 return value + 1;
274 }
275 return -EINVAL;
276}
277
278/* boolean_validate:
279 *
280 * on input validate boolean values 0/1, on output just pass the
281 * received value.
282 */
283static int boolean_validate(const int direction, const int value)
284{
285 if (direction == SNC_VALIDATE_IN) {
286 if (value != 0 && value != 1)
287 return -EINVAL;
288 }
289 return value;
290}
291
292/*
malattia@linux.it59b19102007-04-09 10:19:04 +0200293 * Sysfs show/store common to all sony_nc_values
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100294 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200295static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
Len Browna02d1c12007-02-07 15:34:02 -0500296 char *buffer)
Stelian Pop7f09c432007-01-13 23:04:31 +0100297{
Stelian Pop7f09c432007-01-13 23:04:31 +0100298 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200299 struct sony_nc_value *item =
300 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100301
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100302 if (!*item->acpiget)
Stelian Pop7f09c432007-01-13 23:04:31 +0100303 return -EIO;
304
malattia@linux.it59b19102007-04-09 10:19:04 +0200305 if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100306 return -EIO;
307
Mattia Dongili156c2212007-02-12 22:01:07 +0100308 if (item->validate)
309 value = item->validate(SNC_VALIDATE_OUT, value);
310
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100311 return snprintf(buffer, PAGE_SIZE, "%d\n", value);
Stelian Pop7f09c432007-01-13 23:04:31 +0100312}
313
malattia@linux.it59b19102007-04-09 10:19:04 +0200314static ssize_t sony_nc_sysfs_store(struct device *dev,
Len Browna02d1c12007-02-07 15:34:02 -0500315 struct device_attribute *attr,
316 const char *buffer, size_t count)
Stelian Pop7f09c432007-01-13 23:04:31 +0100317{
Stelian Pop7f09c432007-01-13 23:04:31 +0100318 int value;
malattia@linux.it59b19102007-04-09 10:19:04 +0200319 struct sony_nc_value *item =
320 container_of(attr, struct sony_nc_value, devattr);
Stelian Pop7f09c432007-01-13 23:04:31 +0100321
322 if (!item->acpiset)
323 return -EIO;
324
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100325 if (count > 31)
326 return -EINVAL;
327
328 value = simple_strtoul(buffer, NULL, 10);
Stelian Pop7f09c432007-01-13 23:04:31 +0100329
Mattia Dongili156c2212007-02-12 22:01:07 +0100330 if (item->validate)
331 value = item->validate(SNC_VALIDATE_IN, value);
332
333 if (value < 0)
334 return value;
Stelian Pop7f09c432007-01-13 23:04:31 +0100335
malattia@linux.it59b19102007-04-09 10:19:04 +0200336 if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
Stelian Pop7f09c432007-01-13 23:04:31 +0100337 return -EIO;
Andrew Morton3f4f4612007-01-13 23:04:32 +0100338 item->value = value;
339 item->valid = 1;
Stelian Pop7f09c432007-01-13 23:04:31 +0100340 return count;
341}
342
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100343
Mattia Dongilid78865c2007-02-07 20:01:56 +0100344/*
345 * Backlight device
346 */
347static int sony_backlight_update_status(struct backlight_device *bd)
Andrew Morton3f4f4612007-01-13 23:04:32 +0100348{
malattia@linux.it59b19102007-04-09 10:19:04 +0200349 return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
Richard Purdie321709c2007-02-10 15:04:08 +0000350 bd->props.brightness + 1, NULL);
Andrew Morton3f4f4612007-01-13 23:04:32 +0100351}
352
Mattia Dongilid78865c2007-02-07 20:01:56 +0100353static int sony_backlight_get_brightness(struct backlight_device *bd)
354{
355 int value;
356
malattia@linux.it59b19102007-04-09 10:19:04 +0200357 if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
Mattia Dongilid78865c2007-02-07 20:01:56 +0100358 return 0;
359 /* brightness levels are 1-based, while backlight ones are 0-based */
360 return value - 1;
361}
362
363static struct backlight_device *sony_backlight_device;
Richard Purdie321709c2007-02-10 15:04:08 +0000364static struct backlight_ops sony_backlight_ops = {
Len Browna02d1c12007-02-07 15:34:02 -0500365 .update_status = sony_backlight_update_status,
366 .get_brightness = sony_backlight_get_brightness,
Mattia Dongilid78865c2007-02-07 20:01:56 +0100367};
368
369/*
370 * ACPI callbacks
371 */
Stelian Pop7f09c432007-01-13 23:04:31 +0100372static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
373{
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200374 dprintk("sony_acpi_notify, event: %d\n", event);
malattia@linux.it59b19102007-04-09 10:19:04 +0200375 acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
Stelian Pop7f09c432007-01-13 23:04:31 +0100376}
377
378static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
379 void *context, void **return_value)
380{
381 struct acpi_namespace_node *node;
382 union acpi_operand_object *operand;
383
Len Browna02d1c12007-02-07 15:34:02 -0500384 node = (struct acpi_namespace_node *)handle;
385 operand = (union acpi_operand_object *)node->object;
Stelian Pop7f09c432007-01-13 23:04:31 +0100386
387 printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
388 (u32) operand->method.param_count);
389
390 return AE_OK;
391}
392
Mattia Dongilid78865c2007-02-07 20:01:56 +0100393/*
394 * ACPI device
395 */
malattia@linux.it59b19102007-04-09 10:19:04 +0200396static int sony_nc_resume(struct acpi_device *device)
Mattia Dongilid78865c2007-02-07 20:01:56 +0100397{
malattia@linux.it59b19102007-04-09 10:19:04 +0200398 struct sony_nc_value *item;
Mattia Dongilid78865c2007-02-07 20:01:56 +0100399
malattia@linux.it59b19102007-04-09 10:19:04 +0200400 for (item = sony_nc_values; item->name; item++) {
Mattia Dongilid78865c2007-02-07 20:01:56 +0100401 int ret;
402
403 if (!item->valid)
404 continue;
malattia@linux.it59b19102007-04-09 10:19:04 +0200405 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
Len Browna02d1c12007-02-07 15:34:02 -0500406 item->value, NULL);
Mattia Dongilid78865c2007-02-07 20:01:56 +0100407 if (ret < 0) {
408 printk("%s: %d\n", __FUNCTION__, ret);
409 break;
410 }
411 }
412 return 0;
413}
414
malattia@linux.it59b19102007-04-09 10:19:04 +0200415static int sony_nc_add(struct acpi_device *device)
Stelian Pop7f09c432007-01-13 23:04:31 +0100416{
417 acpi_status status;
Andrew Morton8607c672007-03-06 02:29:42 -0800418 int result = 0;
Alessandro Guido50f62af2007-01-13 23:04:34 +0100419 acpi_handle handle;
malattia@linux.it56b87562007-04-09 10:19:05 +0200420 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100421
malattia@linux.it59b19102007-04-09 10:19:04 +0200422 sony_nc_acpi_device = device;
Stelian Popc5611622007-01-13 23:04:37 +0100423
malattia@linux.it59b19102007-04-09 10:19:04 +0200424 sony_nc_acpi_handle = device->handle;
Stelian Pop7f09c432007-01-13 23:04:31 +0100425
Stelian Pop7f09c432007-01-13 23:04:31 +0100426 if (debug) {
malattia@linux.it59b19102007-04-09 10:19:04 +0200427 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
Stelian Pop7f09c432007-01-13 23:04:31 +0100428 1, sony_walk_callback, NULL, NULL);
429 if (ACPI_FAILURE(status)) {
430 printk(LOG_PFX "unable to walk acpi resources\n");
431 result = -ENODEV;
432 goto outwalk;
433 }
Stelian Popc5611622007-01-13 23:04:37 +0100434 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100435
malattia@linux.it59b19102007-04-09 10:19:04 +0200436 status = acpi_install_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100437 ACPI_DEVICE_NOTIFY,
Len Browna02d1c12007-02-07 15:34:02 -0500438 sony_acpi_notify, NULL);
Stelian Popc5611622007-01-13 23:04:37 +0100439 if (ACPI_FAILURE(status)) {
440 printk(LOG_PFX "unable to install notify handler\n");
441 result = -ENODEV;
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100442 goto outwalk;
Stelian Pop7f09c432007-01-13 23:04:31 +0100443 }
444
malattia@linux.it59b19102007-04-09 10:19:04 +0200445 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
Alessandro Guido50f62af2007-01-13 23:04:34 +0100446 sony_backlight_device = backlight_device_register("sony", NULL,
Len Browna02d1c12007-02-07 15:34:02 -0500447 NULL,
Richard Purdie321709c2007-02-10 15:04:08 +0000448 &sony_backlight_ops);
Mattia Dongili7df03b822007-01-13 23:04:41 +0100449
Len Browna02d1c12007-02-07 15:34:02 -0500450 if (IS_ERR(sony_backlight_device)) {
Mattia Dongili91fbc1d2007-02-07 20:01:53 +0100451 printk(LOG_PFX "unable to register backlight device\n");
Mattia Dongili7df03b822007-01-13 23:04:41 +0100452 sony_backlight_device = NULL;
Richard Purdie321709c2007-02-10 15:04:08 +0000453 } else {
454 sony_backlight_device->props.brightness =
Len Browna02d1c12007-02-07 15:34:02 -0500455 sony_backlight_get_brightness
456 (sony_backlight_device);
Richard Purdie321709c2007-02-10 15:04:08 +0000457 sony_backlight_device->props.max_brightness =
458 SONY_MAX_BRIGHTNESS - 1;
459 }
460
Alessandro Guido50f62af2007-01-13 23:04:34 +0100461 }
Stelian Pop7f09c432007-01-13 23:04:31 +0100462
malattia@linux.it56b87562007-04-09 10:19:05 +0200463 if (sony_pf_add())
Mattia Dongilied3aa4b2007-02-07 20:01:54 +0100464 goto outbacklight;
Stelian Pop7f09c432007-01-13 23:04:31 +0100465
malattia@linux.it56b87562007-04-09 10:19:05 +0200466 /* create sony_pf sysfs attributes related to the SNC device */
467 for (item = sony_nc_values; item->name; ++item) {
468
469 if (!debug && item->debug)
470 continue;
471
472 /* find the available acpiget as described in the DSDT */
473 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
474 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
475 *item->acpiget,
476 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200477 dprintk("Found %s getter: %s\n",
478 item->name, *item->acpiget);
malattia@linux.it56b87562007-04-09 10:19:05 +0200479 item->devattr.attr.mode |= S_IRUGO;
480 break;
481 }
482 }
483
484 /* find the available acpiset as described in the DSDT */
485 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
486 if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
487 *item->acpiset,
488 &handle))) {
malattia@linux.itb9a218b2007-04-09 10:19:06 +0200489 dprintk("Found %s setter: %s\n",
490 item->name, *item->acpiset);
malattia@linux.it56b87562007-04-09 10:19:05 +0200491 item->devattr.attr.mode |= S_IWUSR;
492 break;
493 }
494 }
495
496 if (item->devattr.attr.mode != 0) {
497 result =
498 device_create_file(&sony_pf_device->dev,
499 &item->devattr);
500 if (result)
501 goto out_sysfs;
502 }
503 }
504
malattia@linux.it59b19102007-04-09 10:19:04 +0200505 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100506
507 return 0;
508
malattia@linux.it56b87562007-04-09 10:19:05 +0200509 out_sysfs:
510 for (item = sony_nc_values; item->name; ++item) {
511 device_remove_file(&sony_pf_device->dev, &item->devattr);
512 }
513 sony_pf_remove();
Len Browna02d1c12007-02-07 15:34:02 -0500514 outbacklight:
Mattia Dongili7df03b822007-01-13 23:04:41 +0100515 if (sony_backlight_device)
516 backlight_device_unregister(sony_backlight_device);
517
malattia@linux.it59b19102007-04-09 10:19:04 +0200518 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100519 ACPI_DEVICE_NOTIFY,
520 sony_acpi_notify);
521 if (ACPI_FAILURE(status))
522 printk(LOG_PFX "unable to remove notify handler\n");
Len Browna02d1c12007-02-07 15:34:02 -0500523 outwalk:
Stelian Pop7f09c432007-01-13 23:04:31 +0100524 return result;
525}
526
malattia@linux.it59b19102007-04-09 10:19:04 +0200527static int sony_nc_remove(struct acpi_device *device, int type)
Stelian Pop7f09c432007-01-13 23:04:31 +0100528{
529 acpi_status status;
malattia@linux.it56b87562007-04-09 10:19:05 +0200530 struct sony_nc_value *item;
Stelian Pop7f09c432007-01-13 23:04:31 +0100531
Alessandro Guido50f62af2007-01-13 23:04:34 +0100532 if (sony_backlight_device)
533 backlight_device_unregister(sony_backlight_device);
534
malattia@linux.it59b19102007-04-09 10:19:04 +0200535 sony_nc_acpi_device = NULL;
Stelian Popc5611622007-01-13 23:04:37 +0100536
malattia@linux.it59b19102007-04-09 10:19:04 +0200537 status = acpi_remove_notify_handler(sony_nc_acpi_handle,
Stelian Popc5611622007-01-13 23:04:37 +0100538 ACPI_DEVICE_NOTIFY,
539 sony_acpi_notify);
540 if (ACPI_FAILURE(status))
541 printk(LOG_PFX "unable to remove notify handler\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100542
malattia@linux.it56b87562007-04-09 10:19:05 +0200543 for (item = sony_nc_values; item->name; ++item) {
544 device_remove_file(&sony_pf_device->dev, &item->devattr);
545 }
546
547 sony_pf_remove();
Stelian Pop7f09c432007-01-13 23:04:31 +0100548
malattia@linux.it59b19102007-04-09 10:19:04 +0200549 printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n");
Stelian Pop7f09c432007-01-13 23:04:31 +0100550
551 return 0;
552}
553
malattia@linux.it59b19102007-04-09 10:19:04 +0200554static struct acpi_driver sony_nc_driver = {
555 .name = SONY_NC_DRIVER_NAME,
556 .class = SONY_NC_CLASS,
557 .ids = SONY_NC_HID,
Len Browna02d1c12007-02-07 15:34:02 -0500558 .ops = {
malattia@linux.it59b19102007-04-09 10:19:04 +0200559 .add = sony_nc_add,
560 .remove = sony_nc_remove,
561 .resume = sony_nc_resume,
Len Browna02d1c12007-02-07 15:34:02 -0500562 },
Andrew Morton3f4f4612007-01-13 23:04:32 +0100563};
564
malattia@linux.it59b19102007-04-09 10:19:04 +0200565static int __init sony_laptop_init(void)
Stelian Pop7f09c432007-01-13 23:04:31 +0100566{
malattia@linux.it59b19102007-04-09 10:19:04 +0200567 return acpi_bus_register_driver(&sony_nc_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +0100568}
569
malattia@linux.it59b19102007-04-09 10:19:04 +0200570static void __exit sony_laptop_exit(void)
Stelian Pop7f09c432007-01-13 23:04:31 +0100571{
malattia@linux.it59b19102007-04-09 10:19:04 +0200572 acpi_bus_unregister_driver(&sony_nc_driver);
Stelian Pop7f09c432007-01-13 23:04:31 +0100573}
574
malattia@linux.it59b19102007-04-09 10:19:04 +0200575module_init(sony_laptop_init);
576module_exit(sony_laptop_exit);