Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ACPI Sony Notebook Control Driver (SNC) |
| 3 | * |
| 4 | * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net> |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 5 | * Copyright (C) 2007 Mattia Dongili <malattia@linux.it> |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 6 | * |
| 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 Guido | 50f62af | 2007-01-13 23:04:34 +0100 | [diff] [blame] | 31 | #include <linux/backlight.h> |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 32 | #include <linux/platform_device.h> |
Alessandro Guido | 50f62af | 2007-01-13 23:04:34 +0100 | [diff] [blame] | 33 | #include <linux/err.h> |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 34 | #include <acpi/acpi_drivers.h> |
| 35 | #include <acpi/acpi_bus.h> |
| 36 | #include <asm/uaccess.h> |
| 37 | |
malattia@linux.it | b9a218b | 2007-04-09 10:19:06 +0200 | [diff] [blame^] | 38 | #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.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 43 | #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 Guido | 50f62af | 2007-01-13 23:04:34 +0100 | [diff] [blame] | 46 | |
| 47 | /* the device uses 1-based values, while the backlight subsystem uses |
| 48 | 0-based values */ |
| 49 | #define SONY_MAX_BRIGHTNESS 8 |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 50 | |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 51 | MODULE_AUTHOR("Stelian Pop, Mattia Dongili"); |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 52 | MODULE_DESCRIPTION(SONY_NC_DRIVER_NAME); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 53 | MODULE_LICENSE("GPL"); |
| 54 | |
| 55 | static int debug; |
| 56 | module_param(debug, int, 0); |
| 57 | MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help " |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 58 | "the development of this driver"); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 59 | |
malattia@linux.it | 56b8756 | 2007-04-09 10:19:05 +0200 | [diff] [blame] | 60 | /*********** Platform Device ***********/ |
| 61 | |
| 62 | static atomic_t sony_pf_users = ATOMIC_INIT(0); |
| 63 | static struct platform_driver sony_pf_driver = { |
| 64 | .driver = { |
| 65 | .name = "sony-laptop", |
| 66 | .owner = THIS_MODULE, |
| 67 | } |
| 68 | }; |
| 69 | static struct platform_device *sony_pf_device; |
| 70 | |
| 71 | static 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 | |
| 105 | static 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.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 118 | static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *, |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 119 | char *); |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 120 | static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *, |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 121 | const char *, size_t); |
Mattia Dongili | 156c221 | 2007-02-12 22:01:07 +0100 | [diff] [blame] | 122 | static int boolean_validate(const int, const int); |
| 123 | static int brightness_default_validate(const int, const int); |
| 124 | |
| 125 | #define SNC_VALIDATE_IN 0 |
| 126 | #define SNC_VALIDATE_OUT 1 |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 127 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 128 | struct sony_nc_value { |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 129 | 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 Dongili | 156c221 | 2007-02-12 22:01:07 +0100 | [diff] [blame] | 132 | int (*validate)(const int, const int); /* input/output validation */ |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 133 | 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 Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 139 | #define SNC_HANDLE_NAMES(_name, _values...) \ |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 140 | static char *snc_##_name[] = { _values, NULL } |
| 141 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 142 | #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \ |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 143 | { \ |
| 144 | .name = __stringify(_name), \ |
| 145 | .acpiget = _getters, \ |
| 146 | .acpiset = _setters, \ |
Mattia Dongili | 156c221 | 2007-02-12 22:01:07 +0100 | [diff] [blame] | 147 | .validate = _validate, \ |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 148 | .debug = _debug, \ |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 149 | .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \ |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 150 | } |
| 151 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 152 | #define SNC_HANDLE_NULL { .name = NULL } |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 153 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 154 | SNC_HANDLE_NAMES(fnkey_get, "GHKE"); |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 155 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 156 | SNC_HANDLE_NAMES(brightness_def_get, "GPBR"); |
| 157 | SNC_HANDLE_NAMES(brightness_def_set, "SPBR"); |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 158 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 159 | SNC_HANDLE_NAMES(cdpower_get, "GCDP"); |
| 160 | SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW"); |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 161 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 162 | SNC_HANDLE_NAMES(audiopower_get, "GAZP"); |
| 163 | SNC_HANDLE_NAMES(audiopower_set, "AZPW"); |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 164 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 165 | SNC_HANDLE_NAMES(lanpower_get, "GLNP"); |
| 166 | SNC_HANDLE_NAMES(lanpower_set, "LNPW"); |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 167 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 168 | SNC_HANDLE_NAMES(PID_get, "GPID"); |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 169 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 170 | SNC_HANDLE_NAMES(CTR_get, "GCTR"); |
| 171 | SNC_HANDLE_NAMES(CTR_set, "SCTR"); |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 172 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 173 | SNC_HANDLE_NAMES(PCR_get, "GPCR"); |
| 174 | SNC_HANDLE_NAMES(PCR_set, "SPCR"); |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 175 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 176 | SNC_HANDLE_NAMES(CMI_get, "GCMI"); |
| 177 | SNC_HANDLE_NAMES(CMI_set, "SCMI"); |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 178 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 179 | static struct sony_nc_value sony_nc_values[] = { |
| 180 | SNC_HANDLE(brightness_default, snc_brightness_def_get, |
Mattia Dongili | 156c221 | 2007-02-12 22:01:07 +0100 | [diff] [blame] | 181 | snc_brightness_def_set, brightness_default_validate, 0), |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 182 | 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 Dongili | 156c221 | 2007-02-12 22:01:07 +0100 | [diff] [blame] | 185 | boolean_validate, 0), |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 186 | SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set, |
Mattia Dongili | 156c221 | 2007-02-12 22:01:07 +0100 | [diff] [blame] | 187 | boolean_validate, 1), |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 188 | /* unknown methods */ |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 189 | 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 Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 194 | }; |
| 195 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 196 | static acpi_handle sony_nc_acpi_handle; |
| 197 | static struct acpi_device *sony_nc_acpi_device = NULL; |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 198 | |
Mattia Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 199 | /* |
| 200 | * acpi_evaluate_object wrappers |
| 201 | */ |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 202 | static 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 | |
| 222 | static 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, ¶ms, &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 Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 257 | /* |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 258 | * sony_nc_values input/output validate functions |
Mattia Dongili | 156c221 | 2007-02-12 22:01:07 +0100 | [diff] [blame] | 259 | */ |
| 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 | */ |
| 266 | static 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 | */ |
| 283 | static 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.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 293 | * Sysfs show/store common to all sony_nc_values |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 294 | */ |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 295 | static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr, |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 296 | char *buffer) |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 297 | { |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 298 | int value; |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 299 | struct sony_nc_value *item = |
| 300 | container_of(attr, struct sony_nc_value, devattr); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 301 | |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 302 | if (!*item->acpiget) |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 303 | return -EIO; |
| 304 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 305 | if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0) |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 306 | return -EIO; |
| 307 | |
Mattia Dongili | 156c221 | 2007-02-12 22:01:07 +0100 | [diff] [blame] | 308 | if (item->validate) |
| 309 | value = item->validate(SNC_VALIDATE_OUT, value); |
| 310 | |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 311 | return snprintf(buffer, PAGE_SIZE, "%d\n", value); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 312 | } |
| 313 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 314 | static ssize_t sony_nc_sysfs_store(struct device *dev, |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 315 | struct device_attribute *attr, |
| 316 | const char *buffer, size_t count) |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 317 | { |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 318 | int value; |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 319 | struct sony_nc_value *item = |
| 320 | container_of(attr, struct sony_nc_value, devattr); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 321 | |
| 322 | if (!item->acpiset) |
| 323 | return -EIO; |
| 324 | |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 325 | if (count > 31) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | value = simple_strtoul(buffer, NULL, 10); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 329 | |
Mattia Dongili | 156c221 | 2007-02-12 22:01:07 +0100 | [diff] [blame] | 330 | if (item->validate) |
| 331 | value = item->validate(SNC_VALIDATE_IN, value); |
| 332 | |
| 333 | if (value < 0) |
| 334 | return value; |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 335 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 336 | if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0) |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 337 | return -EIO; |
Andrew Morton | 3f4f461 | 2007-01-13 23:04:32 +0100 | [diff] [blame] | 338 | item->value = value; |
| 339 | item->valid = 1; |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 340 | return count; |
| 341 | } |
| 342 | |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 343 | |
Mattia Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 344 | /* |
| 345 | * Backlight device |
| 346 | */ |
| 347 | static int sony_backlight_update_status(struct backlight_device *bd) |
Andrew Morton | 3f4f461 | 2007-01-13 23:04:32 +0100 | [diff] [blame] | 348 | { |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 349 | return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT", |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 350 | bd->props.brightness + 1, NULL); |
Andrew Morton | 3f4f461 | 2007-01-13 23:04:32 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Mattia Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 353 | static int sony_backlight_get_brightness(struct backlight_device *bd) |
| 354 | { |
| 355 | int value; |
| 356 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 357 | if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) |
Mattia Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 358 | return 0; |
| 359 | /* brightness levels are 1-based, while backlight ones are 0-based */ |
| 360 | return value - 1; |
| 361 | } |
| 362 | |
| 363 | static struct backlight_device *sony_backlight_device; |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 364 | static struct backlight_ops sony_backlight_ops = { |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 365 | .update_status = sony_backlight_update_status, |
| 366 | .get_brightness = sony_backlight_get_brightness, |
Mattia Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 367 | }; |
| 368 | |
| 369 | /* |
| 370 | * ACPI callbacks |
| 371 | */ |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 372 | static void sony_acpi_notify(acpi_handle handle, u32 event, void *data) |
| 373 | { |
malattia@linux.it | b9a218b | 2007-04-09 10:19:06 +0200 | [diff] [blame^] | 374 | dprintk("sony_acpi_notify, event: %d\n", event); |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 375 | acpi_bus_generate_event(sony_nc_acpi_device, 1, event); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static 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 Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 384 | node = (struct acpi_namespace_node *)handle; |
| 385 | operand = (union acpi_operand_object *)node->object; |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 386 | |
| 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 Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 393 | /* |
| 394 | * ACPI device |
| 395 | */ |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 396 | static int sony_nc_resume(struct acpi_device *device) |
Mattia Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 397 | { |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 398 | struct sony_nc_value *item; |
Mattia Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 399 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 400 | for (item = sony_nc_values; item->name; item++) { |
Mattia Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 401 | int ret; |
| 402 | |
| 403 | if (!item->valid) |
| 404 | continue; |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 405 | ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 406 | item->value, NULL); |
Mattia Dongili | d78865c | 2007-02-07 20:01:56 +0100 | [diff] [blame] | 407 | if (ret < 0) { |
| 408 | printk("%s: %d\n", __FUNCTION__, ret); |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 415 | static int sony_nc_add(struct acpi_device *device) |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 416 | { |
| 417 | acpi_status status; |
Andrew Morton | 8607c67 | 2007-03-06 02:29:42 -0800 | [diff] [blame] | 418 | int result = 0; |
Alessandro Guido | 50f62af | 2007-01-13 23:04:34 +0100 | [diff] [blame] | 419 | acpi_handle handle; |
malattia@linux.it | 56b8756 | 2007-04-09 10:19:05 +0200 | [diff] [blame] | 420 | struct sony_nc_value *item; |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 421 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 422 | sony_nc_acpi_device = device; |
Stelian Pop | c561162 | 2007-01-13 23:04:37 +0100 | [diff] [blame] | 423 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 424 | sony_nc_acpi_handle = device->handle; |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 425 | |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 426 | if (debug) { |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 427 | status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle, |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 428 | 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 Pop | c561162 | 2007-01-13 23:04:37 +0100 | [diff] [blame] | 434 | } |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 435 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 436 | status = acpi_install_notify_handler(sony_nc_acpi_handle, |
Stelian Pop | c561162 | 2007-01-13 23:04:37 +0100 | [diff] [blame] | 437 | ACPI_DEVICE_NOTIFY, |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 438 | sony_acpi_notify, NULL); |
Stelian Pop | c561162 | 2007-01-13 23:04:37 +0100 | [diff] [blame] | 439 | if (ACPI_FAILURE(status)) { |
| 440 | printk(LOG_PFX "unable to install notify handler\n"); |
| 441 | result = -ENODEV; |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 442 | goto outwalk; |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 443 | } |
| 444 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 445 | if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) { |
Alessandro Guido | 50f62af | 2007-01-13 23:04:34 +0100 | [diff] [blame] | 446 | sony_backlight_device = backlight_device_register("sony", NULL, |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 447 | NULL, |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 448 | &sony_backlight_ops); |
Mattia Dongili | 7df03b82 | 2007-01-13 23:04:41 +0100 | [diff] [blame] | 449 | |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 450 | if (IS_ERR(sony_backlight_device)) { |
Mattia Dongili | 91fbc1d | 2007-02-07 20:01:53 +0100 | [diff] [blame] | 451 | printk(LOG_PFX "unable to register backlight device\n"); |
Mattia Dongili | 7df03b82 | 2007-01-13 23:04:41 +0100 | [diff] [blame] | 452 | sony_backlight_device = NULL; |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 453 | } else { |
| 454 | sony_backlight_device->props.brightness = |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 455 | sony_backlight_get_brightness |
| 456 | (sony_backlight_device); |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 457 | sony_backlight_device->props.max_brightness = |
| 458 | SONY_MAX_BRIGHTNESS - 1; |
| 459 | } |
| 460 | |
Alessandro Guido | 50f62af | 2007-01-13 23:04:34 +0100 | [diff] [blame] | 461 | } |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 462 | |
malattia@linux.it | 56b8756 | 2007-04-09 10:19:05 +0200 | [diff] [blame] | 463 | if (sony_pf_add()) |
Mattia Dongili | ed3aa4b | 2007-02-07 20:01:54 +0100 | [diff] [blame] | 464 | goto outbacklight; |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 465 | |
malattia@linux.it | 56b8756 | 2007-04-09 10:19:05 +0200 | [diff] [blame] | 466 | /* 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.it | b9a218b | 2007-04-09 10:19:06 +0200 | [diff] [blame^] | 477 | dprintk("Found %s getter: %s\n", |
| 478 | item->name, *item->acpiget); |
malattia@linux.it | 56b8756 | 2007-04-09 10:19:05 +0200 | [diff] [blame] | 479 | 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.it | b9a218b | 2007-04-09 10:19:06 +0200 | [diff] [blame^] | 489 | dprintk("Found %s setter: %s\n", |
| 490 | item->name, *item->acpiset); |
malattia@linux.it | 56b8756 | 2007-04-09 10:19:05 +0200 | [diff] [blame] | 491 | 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.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 505 | printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n"); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 506 | |
| 507 | return 0; |
| 508 | |
malattia@linux.it | 56b8756 | 2007-04-09 10:19:05 +0200 | [diff] [blame] | 509 | 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 Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 514 | outbacklight: |
Mattia Dongili | 7df03b82 | 2007-01-13 23:04:41 +0100 | [diff] [blame] | 515 | if (sony_backlight_device) |
| 516 | backlight_device_unregister(sony_backlight_device); |
| 517 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 518 | status = acpi_remove_notify_handler(sony_nc_acpi_handle, |
Stelian Pop | c561162 | 2007-01-13 23:04:37 +0100 | [diff] [blame] | 519 | ACPI_DEVICE_NOTIFY, |
| 520 | sony_acpi_notify); |
| 521 | if (ACPI_FAILURE(status)) |
| 522 | printk(LOG_PFX "unable to remove notify handler\n"); |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 523 | outwalk: |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 524 | return result; |
| 525 | } |
| 526 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 527 | static int sony_nc_remove(struct acpi_device *device, int type) |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 528 | { |
| 529 | acpi_status status; |
malattia@linux.it | 56b8756 | 2007-04-09 10:19:05 +0200 | [diff] [blame] | 530 | struct sony_nc_value *item; |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 531 | |
Alessandro Guido | 50f62af | 2007-01-13 23:04:34 +0100 | [diff] [blame] | 532 | if (sony_backlight_device) |
| 533 | backlight_device_unregister(sony_backlight_device); |
| 534 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 535 | sony_nc_acpi_device = NULL; |
Stelian Pop | c561162 | 2007-01-13 23:04:37 +0100 | [diff] [blame] | 536 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 537 | status = acpi_remove_notify_handler(sony_nc_acpi_handle, |
Stelian Pop | c561162 | 2007-01-13 23:04:37 +0100 | [diff] [blame] | 538 | ACPI_DEVICE_NOTIFY, |
| 539 | sony_acpi_notify); |
| 540 | if (ACPI_FAILURE(status)) |
| 541 | printk(LOG_PFX "unable to remove notify handler\n"); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 542 | |
malattia@linux.it | 56b8756 | 2007-04-09 10:19:05 +0200 | [diff] [blame] | 543 | 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 Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 548 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 549 | printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n"); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 554 | static struct acpi_driver sony_nc_driver = { |
| 555 | .name = SONY_NC_DRIVER_NAME, |
| 556 | .class = SONY_NC_CLASS, |
| 557 | .ids = SONY_NC_HID, |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 558 | .ops = { |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 559 | .add = sony_nc_add, |
| 560 | .remove = sony_nc_remove, |
| 561 | .resume = sony_nc_resume, |
Len Brown | a02d1c1 | 2007-02-07 15:34:02 -0500 | [diff] [blame] | 562 | }, |
Andrew Morton | 3f4f461 | 2007-01-13 23:04:32 +0100 | [diff] [blame] | 563 | }; |
| 564 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 565 | static int __init sony_laptop_init(void) |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 566 | { |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 567 | return acpi_bus_register_driver(&sony_nc_driver); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 568 | } |
| 569 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 570 | static void __exit sony_laptop_exit(void) |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 571 | { |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 572 | acpi_bus_unregister_driver(&sony_nc_driver); |
Stelian Pop | 7f09c43 | 2007-01-13 23:04:31 +0100 | [diff] [blame] | 573 | } |
| 574 | |
malattia@linux.it | 59b1910 | 2007-04-09 10:19:04 +0200 | [diff] [blame] | 575 | module_init(sony_laptop_init); |
| 576 | module_exit(sony_laptop_exit); |